1 /*
2  * Copyright (c) 2024 Bootlin
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 #define DT_DRV_COMPAT st_lsm9ds1
8 
9 #include <zephyr/drivers/sensor.h>
10 #include <zephyr/logging/log.h>
11 
12 #include "lsm9ds1.h"
13 #include <stmemsc.h>
14 
15 LOG_MODULE_REGISTER(LSM9DS1, CONFIG_SENSOR_LOG_LEVEL);
16 
17 /* Sensitivity of the accelerometer, indexed by the raw full scale value. Unit is µg/ LSB */
18 static const uint16_t lsm9ds1_accel_fs_sens[] = {61, 732, 122, 244};
19 
20 /*
21  * Sensitivity of the gyroscope, indexed by the raw full scale value.
22  * The value here is just a factor applied to GAIN_UNIT_G, as the sensitivity is
23  * proportional to the full scale size.
24  * The index 2 is never used : the value `0` is just a placeholder.
25  */
26 static const uint16_t lsm9ds1_gyro_fs_sens[] = {1, 2, 0, 8};
27 
28 /*
29  * Values of the different sampling frequencies of the accelerometer, indexed by the raw odr
30  * value that the sensor understands.
31  */
32 static const uint16_t lsm9ds1_odr_map[] = {0, 10, 50, 119, 238, 476, 952};
33 
34 /*
35  * Value of the different sampling frequencies of the gyroscope, indexed by the raw odr value
36  * that the sensor understands.
37  */
38 static const uint16_t lsm9ds1_gyro_odr_map[] = {0, 15, 59, 119, 238, 476, 952};
39 
lsm9ds1_reboot(const struct device * dev)40 static int lsm9ds1_reboot(const struct device *dev)
41 {
42 	const struct lsm9ds1_config *cfg = dev->config;
43 	stmdev_ctx_t *ctx = (stmdev_ctx_t *)&cfg->ctx;
44 	lsm9ds1_ctrl_reg8_t ctrl8_reg;
45 	int ret;
46 
47 	ret = lsm9ds1_read_reg(ctx, LSM9DS1_CTRL_REG8, (uint8_t *)&ctrl8_reg, 1);
48 	if (ret < 0) {
49 		return ret;
50 	}
51 
52 	ctrl8_reg.boot = 1;
53 
54 	ret = lsm9ds1_write_reg(ctx, LSM9DS1_CTRL_REG8, (uint8_t *)&ctrl8_reg, 1);
55 	if (ret < 0) {
56 		return ret;
57 	}
58 
59 	k_msleep(50);
60 
61 	return 0;
62 }
63 
lsm9ds1_accel_range_to_fs_val(int32_t range)64 static int lsm9ds1_accel_range_to_fs_val(int32_t range)
65 {
66 	switch (range) {
67 	case 2:
68 		return LSM9DS1_2g;
69 	case 4:
70 		return LSM9DS1_4g;
71 	case 8:
72 		return LSM9DS1_8g;
73 	case 16:
74 		return LSM9DS1_16g;
75 	default:
76 		return -EINVAL;
77 	}
78 }
79 
lsm9ds1_gyro_range_to_fs_val(int32_t range)80 static int lsm9ds1_gyro_range_to_fs_val(int32_t range)
81 {
82 	switch (range) {
83 	case 245:
84 		return LSM9DS1_245dps;
85 	case 500:
86 		return LSM9DS1_500dps;
87 	case 2000:
88 		return LSM9DS1_2000dps;
89 	default:
90 		return -EINVAL;
91 	}
92 }
93 
lsm9ds1_accel_fs_val_to_gain(int fs)94 static int lsm9ds1_accel_fs_val_to_gain(int fs)
95 {
96 	return lsm9ds1_accel_fs_sens[fs];
97 }
98 
lsm9ds1_accel_freq_to_odr_val(uint16_t freq)99 static int lsm9ds1_accel_freq_to_odr_val(uint16_t freq)
100 {
101 	size_t i;
102 
103 	for (i = 0; i < ARRAY_SIZE(lsm9ds1_odr_map); i++) {
104 		if (freq <= lsm9ds1_odr_map[i]) {
105 			return i;
106 		}
107 	}
108 
109 	return -EINVAL;
110 }
111 
lsm9ds1_gyro_freq_to_odr_val(uint16_t freq)112 static int lsm9ds1_gyro_freq_to_odr_val(uint16_t freq)
113 {
114 	size_t i;
115 
116 	for (i = 0; i < ARRAY_SIZE(lsm9ds1_gyro_odr_map); i++) {
117 		if (freq <= lsm9ds1_gyro_odr_map[i]) {
118 			return i;
119 		}
120 	}
121 
122 	return -EINVAL;
123 }
124 
lsm9ds1_accel_set_odr_raw(const struct device * dev,uint8_t odr)125 static int lsm9ds1_accel_set_odr_raw(const struct device *dev, uint8_t odr)
126 {
127 	const struct lsm9ds1_config *cfg = dev->config;
128 	stmdev_ctx_t *ctx = (stmdev_ctx_t *)&cfg->ctx;
129 	struct lsm9ds1_data *data = dev->data;
130 	int ret;
131 
132 	lsm9ds1_ctrl_reg6_xl_t ctrl_reg6_xl;
133 
134 	ret = lsm9ds1_read_reg(ctx, LSM9DS1_CTRL_REG6_XL, (uint8_t *)&ctrl_reg6_xl, 1);
135 	if (ret < 0) {
136 		return ret;
137 	}
138 
139 	ctrl_reg6_xl.odr_xl = odr;
140 
141 	ret = lsm9ds1_write_reg(ctx, LSM9DS1_CTRL_REG6_XL, (uint8_t *)&ctrl_reg6_xl, 1);
142 	if (ret < 0) {
143 		return ret;
144 	}
145 
146 	data->accel_odr = odr;
147 
148 	return 0;
149 }
150 
lsm9ds1_gyro_set_odr_raw(const struct device * dev,uint8_t odr)151 static int lsm9ds1_gyro_set_odr_raw(const struct device *dev, uint8_t odr)
152 {
153 	const struct lsm9ds1_config *cfg = dev->config;
154 	stmdev_ctx_t *ctx = (stmdev_ctx_t *)&cfg->ctx;
155 	struct lsm9ds1_data *data = dev->data;
156 	int ret;
157 
158 	lsm9ds1_ctrl_reg1_g_t ctrl_reg1;
159 
160 	ret = lsm9ds1_read_reg(ctx, LSM9DS1_CTRL_REG1_G, (uint8_t *)&ctrl_reg1, 1);
161 	if (ret < 0) {
162 		return ret;
163 	}
164 
165 	ctrl_reg1.odr_g = odr;
166 
167 	ret = lsm9ds1_write_reg(ctx, LSM9DS1_CTRL_REG1_G, (uint8_t *)&ctrl_reg1, 1);
168 	if (ret < 0) {
169 		return ret;
170 	}
171 
172 	data->gyro_odr = odr;
173 	return 0;
174 }
175 
lsm9ds1_gyro_odr_set(const struct device * dev,uint16_t freq)176 static int lsm9ds1_gyro_odr_set(const struct device *dev, uint16_t freq)
177 {
178 	struct lsm9ds1_data *data = dev->data;
179 	int odr;
180 	int ret;
181 
182 	odr = lsm9ds1_gyro_freq_to_odr_val(freq);
183 
184 	if (odr == data->gyro_odr) {
185 		return 0;
186 	}
187 
188 	LOG_INF("You are also changing the odr of the accelerometer");
189 
190 	ret = lsm9ds1_gyro_set_odr_raw(dev, odr);
191 	if (ret < 0) {
192 		LOG_DBG("failed to set gyroscope sampling rate");
193 		return ret;
194 	}
195 
196 	/*
197 	 * When the gyroscope is on, the value of the accelerometer odr must be
198 	 * the same as the value of the gyroscope.
199 	 */
200 
201 	ret = lsm9ds1_accel_set_odr_raw(dev, odr);
202 	if (ret < 0) {
203 		LOG_ERR("failed to set accelerometer sampling rate");
204 		return ret;
205 	}
206 
207 	return 0;
208 }
209 
lsm9ds1_accel_odr_set(const struct device * dev,uint16_t freq)210 static int lsm9ds1_accel_odr_set(const struct device *dev, uint16_t freq)
211 {
212 	const struct lsm9ds1_config *cfg = dev->config;
213 	stmdev_ctx_t *ctx = (stmdev_ctx_t *)&cfg->ctx;
214 	struct lsm9ds1_data *data = dev->data;
215 	int odr, ret;
216 	lsm9ds1_imu_odr_t old_odr;
217 
218 	ret = lsm9ds1_imu_data_rate_get(ctx, &old_odr);
219 	if (ret < 0) {
220 		return ret;
221 	}
222 
223 	/*
224 	 * The gyroscope is on :
225 	 * we have to change the odr on both the accelerometer and the gyroscope
226 	 */
227 	if (old_odr & GYRO_ODR_MASK) {
228 
229 		odr = lsm9ds1_gyro_freq_to_odr_val(freq);
230 
231 		if (odr == data->gyro_odr) {
232 			return 0;
233 		}
234 
235 		LOG_INF("You are also changing the odr of the gyroscope");
236 
237 		ret = lsm9ds1_accel_set_odr_raw(dev, odr);
238 		if (ret < 0) {
239 			LOG_DBG("failed to set accelerometer sampling rate");
240 			return ret;
241 		}
242 
243 		ret = lsm9ds1_gyro_set_odr_raw(dev, odr);
244 		if (ret < 0) {
245 			LOG_ERR("failed to set gyroscope sampling rate");
246 			return ret;
247 		}
248 
249 	/* The gyroscope is off, we have to change the odr of just the accelerometer */
250 	} else {
251 
252 		odr = lsm9ds1_accel_freq_to_odr_val(freq);
253 
254 		if (odr == data->accel_odr) {
255 			return 0;
256 		}
257 
258 		if (odr < 0) {
259 			return odr;
260 		}
261 
262 		ret = lsm9ds1_accel_set_odr_raw(dev, odr);
263 		if (ret < 0) {
264 			LOG_DBG("failed to set accelerometer sampling rate");
265 			return ret;
266 		}
267 	}
268 	return 0;
269 }
270 
lsm9ds1_accel_range_set(const struct device * dev,int32_t range)271 static int lsm9ds1_accel_range_set(const struct device *dev, int32_t range)
272 {
273 	int fs;
274 	struct lsm9ds1_data *data = dev->data;
275 	const struct lsm9ds1_config *cfg = dev->config;
276 	stmdev_ctx_t *ctx = (stmdev_ctx_t *)&cfg->ctx;
277 	int ret;
278 
279 	fs = lsm9ds1_accel_range_to_fs_val(range);
280 	if (fs < 0) {
281 		LOG_DBG("full scale value not supported");
282 		return fs;
283 	}
284 
285 	ret = lsm9ds1_xl_full_scale_set(ctx, fs);
286 	if (ret < 0) {
287 		LOG_DBG("failed to set accelerometer full-scale");
288 		return ret;
289 	}
290 
291 	data->acc_gain = lsm9ds1_accel_fs_val_to_gain(fs);
292 	return 0;
293 }
294 
lsm9ds1_gyro_range_set(const struct device * dev,int32_t range)295 static int lsm9ds1_gyro_range_set(const struct device *dev, int32_t range)
296 {
297 	int fs;
298 	struct lsm9ds1_data *data = dev->data;
299 	const struct lsm9ds1_config *cfg = dev->config;
300 	stmdev_ctx_t *ctx = (stmdev_ctx_t *)&cfg->ctx;
301 	int ret;
302 
303 	fs = lsm9ds1_gyro_range_to_fs_val(range);
304 	if (fs < 0) {
305 		return fs;
306 	}
307 
308 	ret = lsm9ds1_gy_full_scale_set(ctx, fs);
309 	if (ret < 0) {
310 		LOG_DBG("failed to set gyroscope full-scale");
311 		return ret;
312 	}
313 
314 	data->gyro_gain = (lsm9ds1_gyro_fs_sens[fs] * GAIN_UNIT_G);
315 	return 0;
316 }
317 
lsm9ds1_accel_config(const struct device * dev,enum sensor_channel chan,enum sensor_attribute attr,const struct sensor_value * val)318 static int lsm9ds1_accel_config(const struct device *dev, enum sensor_channel chan,
319 				enum sensor_attribute attr, const struct sensor_value *val)
320 {
321 	switch (attr) {
322 	case SENSOR_ATTR_FULL_SCALE:
323 		return lsm9ds1_accel_range_set(dev, sensor_ms2_to_g(val));
324 	case SENSOR_ATTR_SAMPLING_FREQUENCY:
325 		return lsm9ds1_accel_odr_set(dev, val->val1);
326 	default:
327 		LOG_DBG("Accel attribute not supported.");
328 		return -ENOTSUP;
329 	}
330 
331 	return 0;
332 }
333 
lsm9ds1_gyro_config(const struct device * dev,enum sensor_channel chan,enum sensor_attribute attr,const struct sensor_value * val)334 static int lsm9ds1_gyro_config(const struct device *dev, enum sensor_channel chan,
335 			       enum sensor_attribute attr, const struct sensor_value *val)
336 {
337 	switch (attr) {
338 	case SENSOR_ATTR_FULL_SCALE:
339 		return lsm9ds1_gyro_range_set(dev, sensor_rad_to_degrees(val));
340 	case SENSOR_ATTR_SAMPLING_FREQUENCY:
341 		return lsm9ds1_gyro_odr_set(dev, val->val1);
342 	default:
343 		LOG_DBG("Gyro attribute not supported.");
344 		return -ENOTSUP;
345 	}
346 
347 	return 0;
348 }
349 
lsm9ds1_attr_set(const struct device * dev,enum sensor_channel chan,enum sensor_attribute attr,const struct sensor_value * val)350 static int lsm9ds1_attr_set(const struct device *dev, enum sensor_channel chan,
351 			    enum sensor_attribute attr, const struct sensor_value *val)
352 {
353 	switch (chan) {
354 	case SENSOR_CHAN_ACCEL_XYZ:
355 		return lsm9ds1_accel_config(dev, chan, attr, val);
356 	case SENSOR_CHAN_GYRO_XYZ:
357 		return lsm9ds1_gyro_config(dev, chan, attr, val);
358 	default:
359 		LOG_WRN("attr_set() not supported on this channel.");
360 		return -ENOTSUP;
361 	}
362 	return 0;
363 }
364 
lsm9ds1_sample_fetch_accel(const struct device * dev)365 static int lsm9ds1_sample_fetch_accel(const struct device *dev)
366 {
367 	const struct lsm9ds1_config *cfg = dev->config;
368 	stmdev_ctx_t *ctx = (stmdev_ctx_t *)&cfg->ctx;
369 	struct lsm9ds1_data *data = dev->data;
370 	int ret;
371 
372 	ret = lsm9ds1_acceleration_raw_get(ctx, data->acc);
373 	if (ret < 0) {
374 		LOG_DBG("Failed to read sample");
375 		return ret;
376 	}
377 
378 	return 0;
379 }
380 
lsm9ds1_sample_fetch_gyro(const struct device * dev)381 static int lsm9ds1_sample_fetch_gyro(const struct device *dev)
382 {
383 	const struct lsm9ds1_config *cfg = dev->config;
384 	stmdev_ctx_t *ctx = (stmdev_ctx_t *)&cfg->ctx;
385 	struct lsm9ds1_data *data = dev->data;
386 	int ret;
387 
388 	ret = lsm9ds1_angular_rate_raw_get(ctx, data->gyro);
389 	if (ret < 0) {
390 		LOG_DBG("Failed to read sample");
391 		return ret;
392 	}
393 	return 0;
394 }
395 
396 #if IS_ENABLED(CONFIG_LSM9DS1_ENABLE_TEMP)
lsm9ds1_sample_fetch_temp(const struct device * dev)397 static int lsm9ds1_sample_fetch_temp(const struct device *dev)
398 {
399 	const struct lsm9ds1_config *cfg = dev->config;
400 	stmdev_ctx_t *ctx = (stmdev_ctx_t *)&cfg->ctx;
401 	struct lsm9ds1_data *data = dev->data;
402 	int ret;
403 
404 	ret = lsm9ds1_temperature_raw_get(ctx, &data->temp_sample);
405 	if (ret < 0) {
406 		LOG_DBG("Failed to read sample");
407 		return ret;
408 	}
409 
410 	return 0;
411 }
412 #endif
413 
lsm9ds1_sample_fetch(const struct device * dev,enum sensor_channel chan)414 static int lsm9ds1_sample_fetch(const struct device *dev, enum sensor_channel chan)
415 {
416 	switch (chan) {
417 	case SENSOR_CHAN_ACCEL_XYZ:
418 		lsm9ds1_sample_fetch_accel(dev);
419 		break;
420 	case SENSOR_CHAN_GYRO_XYZ:
421 		lsm9ds1_sample_fetch_gyro(dev);
422 		break;
423 #if IS_ENABLED(CONFIG_LSM9DS1_ENABLE_TEMP)
424 	case SENSOR_CHAN_DIE_TEMP:
425 		lsm9ds1_sample_fetch_temp(dev);
426 		break;
427 #endif
428 	case SENSOR_CHAN_ALL:
429 		lsm9ds1_sample_fetch_accel(dev);
430 		lsm9ds1_sample_fetch_gyro(dev);
431 #if IS_ENABLED(CONFIG_LSM9DS1_ENABLE_TEMP)
432 		lsm9ds1_sample_fetch_temp(dev);
433 #endif
434 		break;
435 	default:
436 		return -ENOTSUP;
437 	}
438 	return 0;
439 }
440 
lsm9ds1_accel_convert(struct sensor_value * val,int raw_val,uint32_t sensitivity)441 static inline void lsm9ds1_accel_convert(struct sensor_value *val, int raw_val,
442 					 uint32_t sensitivity)
443 {
444 	/* Sensitivity is exposed in ug/LSB */
445 	/* Convert to m/s^2 */
446 	sensor_ug_to_ms2(raw_val * sensitivity, val);
447 }
448 
lsm9ds1_accel_get_channel(enum sensor_channel chan,struct sensor_value * val,struct lsm9ds1_data * data,uint32_t sensitivity)449 static inline int lsm9ds1_accel_get_channel(enum sensor_channel chan, struct sensor_value *val,
450 					    struct lsm9ds1_data *data, uint32_t sensitivity)
451 {
452 	uint8_t i;
453 
454 	switch (chan) {
455 	case SENSOR_CHAN_ACCEL_X:
456 		lsm9ds1_accel_convert(val, data->acc[0], sensitivity);
457 		break;
458 	case SENSOR_CHAN_ACCEL_Y:
459 		lsm9ds1_accel_convert(val, data->acc[1], sensitivity);
460 		break;
461 	case SENSOR_CHAN_ACCEL_Z:
462 		lsm9ds1_accel_convert(val, data->acc[2], sensitivity);
463 		break;
464 	case SENSOR_CHAN_ACCEL_XYZ:
465 		for (i = 0; i < 3; i++) {
466 			lsm9ds1_accel_convert(val++, data->acc[i], sensitivity);
467 		}
468 		break;
469 	default:
470 		return -ENOTSUP;
471 	}
472 
473 	return 0;
474 }
475 
lsm9ds1_accel_channel_get(enum sensor_channel chan,struct sensor_value * val,struct lsm9ds1_data * data)476 static int lsm9ds1_accel_channel_get(enum sensor_channel chan, struct sensor_value *val,
477 				     struct lsm9ds1_data *data)
478 {
479 	return lsm9ds1_accel_get_channel(chan, val, data, data->acc_gain);
480 }
481 
lsm9ds1_gyro_convert(struct sensor_value * val,int raw_val,uint32_t sensitivity)482 static inline void lsm9ds1_gyro_convert(struct sensor_value *val, int raw_val, uint32_t sensitivity)
483 {
484 	/* Sensitivity is exposed in udps/LSB */
485 	/* Convert to rad/s */
486 	sensor_10udegrees_to_rad((raw_val * (int32_t)sensitivity) / 10, val);
487 }
488 
lsm9ds1_gyro_get_channel(enum sensor_channel chan,struct sensor_value * val,struct lsm9ds1_data * data,uint32_t sensitivity)489 static inline int lsm9ds1_gyro_get_channel(enum sensor_channel chan, struct sensor_value *val,
490 					   struct lsm9ds1_data *data, uint32_t sensitivity)
491 {
492 	uint8_t i;
493 
494 	switch (chan) {
495 	case SENSOR_CHAN_GYRO_X:
496 		lsm9ds1_gyro_convert(val, data->gyro[0], sensitivity);
497 		break;
498 	case SENSOR_CHAN_GYRO_Y:
499 		lsm9ds1_gyro_convert(val, data->gyro[1], sensitivity);
500 		break;
501 	case SENSOR_CHAN_GYRO_Z:
502 		lsm9ds1_gyro_convert(val, data->gyro[2], sensitivity);
503 		break;
504 	case SENSOR_CHAN_GYRO_XYZ:
505 		for (i = 0; i < 3; i++) {
506 			lsm9ds1_gyro_convert(val++, data->gyro[i], sensitivity);
507 		}
508 		break;
509 	default:
510 		return -ENOTSUP;
511 	}
512 
513 	return 0;
514 }
515 
lsm9ds1_gyro_channel_get(enum sensor_channel chan,struct sensor_value * val,struct lsm9ds1_data * data)516 static int lsm9ds1_gyro_channel_get(enum sensor_channel chan, struct sensor_value *val,
517 				    struct lsm9ds1_data *data)
518 {
519 	return lsm9ds1_gyro_get_channel(chan, val, data, data->gyro_gain);
520 }
521 
522 #if IS_ENABLED(CONFIG_LSM9DS1_ENABLE_TEMP)
lsm9ds1_temp_channel_get(struct sensor_value * val,struct lsm9ds1_data * data)523 static void lsm9ds1_temp_channel_get(struct sensor_value *val, struct lsm9ds1_data *data)
524 {
525 	val->val1 = data->temp_sample / TEMP_SENSITIVITY + TEMP_OFFSET;
526 	val->val2 = (data->temp_sample % TEMP_SENSITIVITY) * (1000000 / TEMP_SENSITIVITY);
527 }
528 #endif
529 
lsm9ds1_channel_get(const struct device * dev,enum sensor_channel chan,struct sensor_value * val)530 static int lsm9ds1_channel_get(const struct device *dev, enum sensor_channel chan,
531 			       struct sensor_value *val)
532 {
533 	struct lsm9ds1_data *data = dev->data;
534 
535 	switch (chan) {
536 	case SENSOR_CHAN_ACCEL_X:
537 	case SENSOR_CHAN_ACCEL_Y:
538 	case SENSOR_CHAN_ACCEL_Z:
539 	case SENSOR_CHAN_ACCEL_XYZ:
540 		lsm9ds1_accel_channel_get(chan, val, data);
541 		break;
542 	case SENSOR_CHAN_GYRO_X:
543 	case SENSOR_CHAN_GYRO_Y:
544 	case SENSOR_CHAN_GYRO_Z:
545 	case SENSOR_CHAN_GYRO_XYZ:
546 		lsm9ds1_gyro_channel_get(chan, val, data);
547 		break;
548 #if IS_ENABLED(CONFIG_LSM9DS1_ENABLE_TEMP)
549 	case SENSOR_CHAN_DIE_TEMP:
550 		lsm9ds1_temp_channel_get(val, data);
551 		break;
552 #endif
553 	default:
554 		return -ENOTSUP;
555 	}
556 
557 	return 0;
558 }
559 
560 static DEVICE_API(sensor, lsm9ds1_api_funcs) = {
561 	.sample_fetch = lsm9ds1_sample_fetch,
562 	.channel_get = lsm9ds1_channel_get,
563 	.attr_set = lsm9ds1_attr_set,
564 };
565 
lsm9ds1_init(const struct device * dev)566 static int lsm9ds1_init(const struct device *dev)
567 {
568 	const struct lsm9ds1_config *cfg = dev->config;
569 	stmdev_ctx_t *ctx = (stmdev_ctx_t *)&cfg->ctx;
570 	struct lsm9ds1_data *lsm9ds1 = dev->data;
571 	uint8_t chip_id, fs;
572 	int ret;
573 
574 	ret = lsm9ds1_reboot(dev);
575 	if (ret < 0) {
576 		LOG_ERR("Failed to reboot device");
577 		return ret;
578 	}
579 
580 	ret = lsm9ds1_read_reg(ctx, LSM9DS1_WHO_AM_I, &chip_id, 1);
581 	if (ret < 0) {
582 		LOG_ERR("failed reading chip id");
583 		return ret;
584 	}
585 
586 	if (chip_id != LSM9DS1_IMU_ID) {
587 		LOG_ERR("Invalid ID : got %x", chip_id);
588 		return -EIO;
589 	}
590 	LOG_DBG("chip_id : %x", chip_id);
591 
592 	LOG_DBG("output data rate is %d\n", cfg->imu_odr);
593 	ret = lsm9ds1_imu_data_rate_set(ctx, cfg->imu_odr);
594 	if (ret < 0) {
595 		LOG_ERR("failed to set IMU odr");
596 		return ret;
597 	}
598 
599 	fs = cfg->accel_range;
600 	LOG_DBG("accel range is %d\n", fs);
601 	ret = lsm9ds1_xl_full_scale_set(ctx, fs);
602 	if (ret < 0) {
603 		LOG_ERR("failed to set accelerometer range %d", fs);
604 		return ret;
605 	}
606 
607 	lsm9ds1->acc_gain = lsm9ds1_accel_fs_val_to_gain(fs);
608 
609 	fs = cfg->gyro_range;
610 	LOG_DBG("gyro range is %d", fs);
611 	ret = lsm9ds1_gy_full_scale_set(ctx, fs);
612 	if (ret < 0) {
613 		LOG_ERR("failed to set gyroscope range %d\n", fs);
614 		return ret;
615 	}
616 	lsm9ds1->gyro_gain = (lsm9ds1_gyro_fs_sens[fs] * GAIN_UNIT_G);
617 
618 	return 0;
619 }
620 
621 #define LSM9DS1_CONFIG_COMMON(inst)                                                                \
622 	.imu_odr = DT_INST_PROP(inst, imu_odr),							   \
623 	.accel_range = DT_INST_PROP(inst, accel_range),						   \
624 	.gyro_range = DT_INST_PROP(inst, gyro_range),
625 
626 /*
627  * Instantiation macros used when a device is on an I2C bus.
628  */
629 
630 #define LSM9DS1_CONFIG_I2C(inst)                                                                   \
631 	{                                                                                          \
632 		STMEMSC_CTX_I2C(&lsm9ds1_config_##inst.stmemsc_cfg),                               \
633 		.stmemsc_cfg =                                                                     \
634 			{                                                                          \
635 				.i2c = I2C_DT_SPEC_INST_GET(inst),                                 \
636 			},                                                                         \
637 		LSM9DS1_CONFIG_COMMON(inst)                                                        \
638 	}
639 
640 #define LSM9DS1_DEFINE(inst)                                                                       \
641 	static struct lsm9ds1_data lsm9ds1_data_##inst = {                                         \
642 		.acc_gain = 0,                                                                     \
643 	};                                                                                         \
644                                                                                                    \
645 	static struct lsm9ds1_config lsm9ds1_config_##inst = LSM9DS1_CONFIG_I2C(inst);             \
646                                                                                                    \
647 	SENSOR_DEVICE_DT_INST_DEFINE(inst, lsm9ds1_init, NULL, &lsm9ds1_data_##inst,               \
648 				     &lsm9ds1_config_##inst, POST_KERNEL,                          \
649 				     CONFIG_SENSOR_INIT_PRIORITY, &lsm9ds1_api_funcs);
650 
651 DT_INST_FOREACH_STATUS_OKAY(LSM9DS1_DEFINE);
652