1 /* ST Microelectronics LIS2DW12 3-axis accelerometer driver
2  *
3  * Copyright (c) 2019 STMicroelectronics
4  *
5  * SPDX-License-Identifier: Apache-2.0
6  *
7  * Datasheet:
8  * https://www.st.com/resource/en/datasheet/lis2dw12.pdf
9  */
10 
11 #define DT_DRV_COMPAT st_lis2dw12
12 
13 #include <zephyr/init.h>
14 #include <stdlib.h>
15 #include <zephyr/sys/__assert.h>
16 #include <zephyr/logging/log.h>
17 #include <zephyr/drivers/sensor.h>
18 
19 #if DT_ANY_INST_ON_BUS_STATUS_OKAY(spi)
20 #include <zephyr/drivers/spi.h>
21 #elif DT_ANY_INST_ON_BUS_STATUS_OKAY(i2c)
22 #include <zephyr/drivers/i2c.h>
23 #endif
24 
25 #include "lis2dw12.h"
26 
27 LOG_MODULE_REGISTER(LIS2DW12, CONFIG_SENSOR_LOG_LEVEL);
28 
29 /**
30  * lis2dw12_set_range - set full scale range for acc
31  * @dev: Pointer to instance of struct device (I2C or SPI)
32  * @range: Full scale range (2, 4, 8 and 16 G)
33  */
lis2dw12_set_range(const struct device * dev,uint8_t fs)34 static int lis2dw12_set_range(const struct device *dev, uint8_t fs)
35 {
36 	int err;
37 	struct lis2dw12_data *lis2dw12 = dev->data;
38 	const struct lis2dw12_device_config *cfg = dev->config;
39 	stmdev_ctx_t *ctx = (stmdev_ctx_t *)&cfg->ctx;
40 	uint8_t shift_gain = 0U;
41 
42 	err = lis2dw12_full_scale_set(ctx, fs);
43 
44 	if (cfg->pm == LIS2DW12_CONT_LOW_PWR_12bit) {
45 		shift_gain = LIS2DW12_SHFT_GAIN_NOLP1;
46 	}
47 
48 	if (!err) {
49 		/* save internally gain for optimization */
50 		lis2dw12->gain =
51 			LIS2DW12_FS_TO_GAIN(fs, shift_gain);
52 	}
53 
54 	return err;
55 }
56 
57 /**
58  * lis2dw12_set_odr - set new sampling frequency
59  * @dev: Pointer to instance of struct device (I2C or SPI)
60  * @odr: Output data rate
61  */
lis2dw12_set_odr(const struct device * dev,uint16_t odr)62 static int lis2dw12_set_odr(const struct device *dev, uint16_t odr)
63 {
64 	const struct lis2dw12_device_config *cfg = dev->config;
65 	stmdev_ctx_t *ctx = (stmdev_ctx_t *)&cfg->ctx;
66 	struct lis2dw12_data *lis2dw12 = dev->data;
67 	uint8_t val;
68 
69 	/* check if power off */
70 	if (odr == 0U) {
71 		return lis2dw12_data_rate_set(ctx, LIS2DW12_XL_ODR_OFF);
72 	}
73 
74 	val =  LIS2DW12_ODR_TO_REG(odr);
75 	if (val > LIS2DW12_XL_ODR_1k6Hz) {
76 		LOG_ERR("ODR too high");
77 		return -ENOTSUP;
78 	}
79 	lis2dw12->odr = odr;
80 	return lis2dw12_data_rate_set(ctx, val);
81 }
82 
lis2dw12_convert(struct sensor_value * val,int raw_val,float gain)83 static inline void lis2dw12_convert(struct sensor_value *val, int raw_val,
84 				    float gain)
85 {
86 	int64_t dval;
87 
88 	/* Gain is in ug/LSB */
89 	/* Convert to m/s^2 */
90 	dval = ((int64_t)raw_val * gain * SENSOR_G) / 1000000LL;
91 	val->val1 = dval / 1000000LL;
92 	val->val2 = dval % 1000000LL;
93 }
94 
lis2dw12_channel_get_temp(const struct device * dev,struct sensor_value * val)95 static inline void lis2dw12_channel_get_temp(const struct device *dev, struct sensor_value *val)
96 {
97 	struct lis2dw12_data *lis2dw12 = dev->data;
98 	int64_t dval_uc;
99 
100 	/* The calcul is in micro Celsius to keep it efficient */
101 	dval_uc = ((lis2dw12->temp >> LIS2DW12_SHIFT_TEMP) * LIS2DW12_TEMP_SCALE_FACTOR);
102 	dval_uc += 25000000;
103 
104 	/* switch to Celsius when we split the integer and fractional parts of the value */
105 	val->val1 = dval_uc / 1000000LL;
106 	val->val2 = dval_uc % 1000000LL;
107 }
108 
lis2dw12_channel_get_acc(const struct device * dev,enum sensor_channel chan,struct sensor_value * val)109 static inline void lis2dw12_channel_get_acc(const struct device *dev,
110 					     enum sensor_channel chan,
111 					     struct sensor_value *val)
112 {
113 	int i;
114 	uint8_t ofs_start, ofs_stop;
115 	struct lis2dw12_data *lis2dw12 = dev->data;
116 	struct sensor_value *pval = val;
117 
118 	switch (chan) {
119 	case SENSOR_CHAN_ACCEL_X:
120 		ofs_start = ofs_stop = 0U;
121 		break;
122 	case SENSOR_CHAN_ACCEL_Y:
123 		ofs_start = ofs_stop = 1U;
124 		break;
125 	case SENSOR_CHAN_ACCEL_Z:
126 		ofs_start = ofs_stop = 2U;
127 		break;
128 	default:
129 		ofs_start = 0U; ofs_stop = 2U;
130 		break;
131 	}
132 
133 	for (i = ofs_start; i <= ofs_stop ; i++) {
134 		lis2dw12_convert(pval++, lis2dw12->acc[i], lis2dw12->gain);
135 	}
136 }
137 
lis2dw12_channel_get_status(const struct device * dev,struct sensor_value * val)138 static inline void lis2dw12_channel_get_status(const struct device *dev,
139 					     struct sensor_value *val)
140 {
141 	const struct lis2dw12_device_config *cfg = dev->config;
142 	stmdev_ctx_t *ctx = (stmdev_ctx_t *)&cfg->ctx;
143 	lis2dw12_status_t status;
144 
145 	/* fetch manually the interrupt status reg */
146 	lis2dw12_status_reg_get(ctx, &status);
147 	val->val1 = (int32_t)*(uint8_t *)&status;
148 }
149 
lis2dw12_channel_get(const struct device * dev,enum sensor_channel chan,struct sensor_value * val)150 static int lis2dw12_channel_get(const struct device *dev,
151 				 enum sensor_channel chan,
152 				 struct sensor_value *val)
153 {
154 	switch (chan) {
155 	case SENSOR_CHAN_ACCEL_X:
156 	case SENSOR_CHAN_ACCEL_Y:
157 	case SENSOR_CHAN_ACCEL_Z:
158 	case SENSOR_CHAN_ACCEL_XYZ:
159 		lis2dw12_channel_get_acc(dev, chan, val);
160 		return 0;
161 	case SENSOR_CHAN_DIE_TEMP:
162 		lis2dw12_channel_get_temp(dev, val);
163 		return 0;
164 	case SENSOR_CHAN_LIS2DW12_INT_STATUS:
165 		lis2dw12_channel_get_status(dev, val);
166 		return 0;
167 	default:
168 		LOG_DBG("Channel not supported");
169 		break;
170 	}
171 
172 	return -ENOTSUP;
173 }
174 
lis2dw12_config(const struct device * dev,enum sensor_channel chan,enum sensor_attribute attr,const struct sensor_value * val)175 static int lis2dw12_config(const struct device *dev, enum sensor_channel chan,
176 			    enum sensor_attribute attr,
177 			    const struct sensor_value *val)
178 {
179 	switch (attr) {
180 	case SENSOR_ATTR_FULL_SCALE:
181 		return lis2dw12_set_range(dev,
182 				LIS2DW12_FS_TO_REG(sensor_ms2_to_g(val)));
183 	case SENSOR_ATTR_SAMPLING_FREQUENCY:
184 		return lis2dw12_set_odr(dev, val->val1);
185 	default:
186 		LOG_DBG("Acc attribute not supported");
187 		break;
188 	}
189 
190 	return -ENOTSUP;
191 }
192 
193 #if (CONFIG_LIS2DW12_SLEEP || CONFIG_LIS2DW12_WAKEUP)
194 
195 /* Converts a lis2dw12_fs_t range to its value in milli-g
196  * Range can be 2/4/8/16G
197  */
198 #define FS_RANGE_TO_MG(fs_range)	((2U << fs_range) * 1000U)
199 
200 /* Converts a range in mg to the lsb value for the WK_THS register
201  * For the reg value: 1 LSB = 1/64 of FS
202  * Range can be 2/4/8/16G
203  */
204 #define MG_TO_WK_THS_LSB(range_mg)	(range_mg / 64)
205 
206 /* Calculates the WK_THS reg value
207  * from the threshold in mg and the lsb value in mg
208  * with correct integer rounding
209  */
210 #define THRESHOLD_MG_TO_WK_THS_REG(thr_mg, lsb_mg) \
211 	((thr_mg + (lsb_mg / 2)) / lsb_mg)
212 
lis2dw12_attr_set_thresh(const struct device * dev,enum sensor_channel chan,enum sensor_attribute attr,const struct sensor_value * val)213 static int lis2dw12_attr_set_thresh(const struct device *dev,
214 					enum sensor_channel chan,
215 					enum sensor_attribute attr,
216 					const struct sensor_value *val)
217 {
218 	uint8_t reg;
219 	size_t ret;
220 	int lsb_mg;
221 	const struct lis2dw12_device_config *cfg = dev->config;
222 	stmdev_ctx_t *ctx = (stmdev_ctx_t *)&cfg->ctx;
223 
224 	LOG_DBG("%s on channel %d", __func__, chan);
225 
226 	/* can only be set for all directions at once */
227 	if (chan != SENSOR_CHAN_ACCEL_XYZ) {
228 		return -EINVAL;
229 	}
230 
231 	/* Configure wakeup threshold threshold. */
232 	lis2dw12_fs_t range;
233 	int err = lis2dw12_full_scale_get(ctx, &range);
234 
235 	if (err) {
236 		return err;
237 	}
238 
239 	uint32_t thr_mg = abs(sensor_ms2_to_mg(val));
240 
241 	/* Check maximum value: depends on current FS value */
242 	if (thr_mg >= FS_RANGE_TO_MG(range)) {
243 		return -EINVAL;
244 	}
245 
246 	/* The threshold is applied to both positive and negative data:
247 	 * for a wake-up interrupt generation at least one of the three axes must be
248 	 * bigger than the threshold.
249 	 */
250 	lsb_mg = MG_TO_WK_THS_LSB(FS_RANGE_TO_MG(range));
251 	reg = THRESHOLD_MG_TO_WK_THS_REG(thr_mg, lsb_mg);
252 
253 	LOG_DBG("Threshold %d mg -> fs: %u mg -> reg = %d LSBs",
254 			thr_mg, FS_RANGE_TO_MG(range), reg);
255 	ret = 0;
256 
257 	return lis2dw12_wkup_threshold_set(ctx, reg);
258 }
259 #endif
260 
261 #ifdef CONFIG_LIS2DW12_FREEFALL
lis2dw12_attr_set_ff_dur(const struct device * dev,enum sensor_channel chan,enum sensor_attribute attr,const struct sensor_value * val)262 static int lis2dw12_attr_set_ff_dur(const struct device *dev,
263 					enum sensor_channel chan,
264 					enum sensor_attribute attr,
265 					const struct sensor_value *val)
266 {
267 	int rc;
268 	uint16_t duration;
269 	const struct lis2dw12_device_config *cfg = dev->config;
270 	struct lis2dw12_data *lis2dw12 = dev->data;
271 	stmdev_ctx_t *ctx = (stmdev_ctx_t *)&cfg->ctx;
272 
273 	LOG_DBG("%s on channel %d", __func__, chan);
274 
275 	/* can only be set for all directions at once */
276 	if (chan != SENSOR_CHAN_ACCEL_XYZ) {
277 		return -EINVAL;
278 	}
279 
280 	/**
281 	 * The given duration in milliseconds with the val
282 	 * parameter is converted into register specific value.
283 	 */
284 	duration = (lis2dw12->odr * (uint16_t)sensor_value_to_double(val)) / 1000;
285 
286 	LOG_DBG("Freefall: duration is %d ms", (uint16_t)sensor_value_to_double(val));
287 	rc = lis2dw12_ff_dur_set(ctx, duration);
288 	if (rc != 0) {
289 		LOG_ERR("Failed to set freefall duration");
290 		return -EIO;
291 	}
292 	return rc;
293 }
294 #endif
295 
296 #ifdef CONFIG_LIS2DW12_SLEEP
lis2dw12_attr_set_act_mode(const struct device * dev,enum sensor_channel chan,enum sensor_attribute attr,const struct sensor_value * val)297 static int lis2dw12_attr_set_act_mode(const struct device *dev,
298 					enum sensor_channel chan,
299 					enum sensor_attribute attr,
300 					const struct sensor_value *val)
301 {
302 	const struct lis2dw12_device_config *cfg = dev->config;
303 	struct lis2dw12_data *lis2dw12 = dev->data;
304 	stmdev_ctx_t *ctx = (stmdev_ctx_t *)&cfg->ctx;
305 	lis2dw12_sleep_on_t sleep_val = val->val1 & 0x03U;
306 
307 	/* can only be set for all directions at once */
308 	if (chan != SENSOR_CHAN_ACCEL_XYZ) {
309 		return -EINVAL;
310 	}
311 
312 	return lis2dw12_act_mode_set(ctx, sleep_val);
313 }
314 #endif
315 
lis2dw12_attr_set(const struct device * dev,enum sensor_channel chan,enum sensor_attribute attr,const struct sensor_value * val)316 static int lis2dw12_attr_set(const struct device *dev,
317 			      enum sensor_channel chan,
318 			      enum sensor_attribute attr,
319 			      const struct sensor_value *val)
320 {
321 	switch (attr) {
322 #if (CONFIG_LIS2DW12_SLEEP || CONFIG_LIS2DW12_WAKEUP)
323 	case SENSOR_ATTR_UPPER_THRESH:
324 	case SENSOR_ATTR_LOWER_THRESH:
325 		return lis2dw12_attr_set_thresh(dev, chan, attr, val);
326 #endif
327 #ifdef CONFIG_LIS2DW12_FREEFALL
328 	case SENSOR_ATTR_FF_DUR:
329 		return lis2dw12_attr_set_ff_dur(dev, chan, attr, val);
330 #endif
331 #ifdef CONFIG_LIS2DW12_SLEEP
332 	case SENSOR_ATTR_FEATURE_MASK:
333 		return lis2dw12_attr_set_act_mode(dev, chan, attr, val);
334 #endif
335 	default:
336 		/* Do nothing */
337 		break;
338 	}
339 
340 
341 	switch (chan) {
342 	case SENSOR_CHAN_ACCEL_X:
343 	case SENSOR_CHAN_ACCEL_Y:
344 	case SENSOR_CHAN_ACCEL_Z:
345 	case SENSOR_CHAN_ACCEL_XYZ:
346 		return lis2dw12_config(dev, chan, attr, val);
347 	default:
348 		LOG_DBG("Attr not supported on %d channel", chan);
349 		break;
350 	}
351 
352 	return -ENOTSUP;
353 }
354 
lis2dw12_sample_fetch_accel(const struct device * dev)355 static int lis2dw12_sample_fetch_accel(const struct device *dev)
356 {
357 	struct lis2dw12_data *lis2dw12 = dev->data;
358 	const struct lis2dw12_device_config *cfg = dev->config;
359 	stmdev_ctx_t *ctx = (stmdev_ctx_t *)&cfg->ctx;
360 	uint8_t shift;
361 	int16_t buf[3];
362 
363 	/* fetch acceleration raw data sample */
364 	if (lis2dw12_acceleration_raw_get(ctx, buf) < 0) {
365 		LOG_DBG("Failed to fetch acceleration raw data sample");
366 		return -EIO;
367 	}
368 
369 	/* adjust to resolution */
370 	if (cfg->pm == LIS2DW12_CONT_LOW_PWR_12bit) {
371 		shift = LIS2DW12_SHIFT_PM1;
372 	} else {
373 		shift = LIS2DW12_SHIFT_PMOTHER;
374 	}
375 
376 	lis2dw12->acc[0] = buf[0] >> shift;
377 	lis2dw12->acc[1] = buf[1] >> shift;
378 	lis2dw12->acc[2] = buf[2] >> shift;
379 
380 	return 0;
381 }
382 
lis2dw12_sample_fetch_temp(const struct device * dev)383 static int lis2dw12_sample_fetch_temp(const struct device *dev)
384 {
385 	const struct lis2dw12_device_config *cfg = dev->config;
386 	stmdev_ctx_t *ctx = (stmdev_ctx_t *)&cfg->ctx;
387 	struct lis2dw12_data *data = dev->data;
388 
389 	/* fetch temperature raw data sample */
390 	if (lis2dw12_temperature_raw_get(ctx, &data->temp) < 0) {
391 		LOG_DBG("Failed to fetch temperature raw data sample");
392 		return -EIO;
393 	}
394 
395 	return 0;
396 }
397 
lis2dw12_sample_fetch(const struct device * dev,enum sensor_channel chan)398 static int lis2dw12_sample_fetch(const struct device *dev,
399 				 enum sensor_channel chan)
400 {
401 	switch (chan) {
402 	case SENSOR_CHAN_ACCEL_X:
403 	case SENSOR_CHAN_ACCEL_Y:
404 	case SENSOR_CHAN_ACCEL_Z:
405 	case SENSOR_CHAN_ACCEL_XYZ:
406 		lis2dw12_sample_fetch_accel(dev);
407 		break;
408 	case SENSOR_CHAN_DIE_TEMP:
409 		lis2dw12_sample_fetch_temp(dev);
410 		break;
411 	default:
412 		LOG_DBG("Channel not supported");
413 		return -ENOTSUP;
414 	}
415 
416 	return 0;
417 }
418 
419 static DEVICE_API(sensor, lis2dw12_driver_api) = {
420 	.attr_set = lis2dw12_attr_set,
421 #if CONFIG_LIS2DW12_TRIGGER
422 	.trigger_set = lis2dw12_trigger_set,
423 #endif /* CONFIG_LIS2DW12_TRIGGER */
424 	.sample_fetch = lis2dw12_sample_fetch,
425 	.channel_get = lis2dw12_channel_get,
426 };
427 
lis2dw12_set_power_mode(const struct device * dev,lis2dw12_mode_t pm)428 static int lis2dw12_set_power_mode(const struct device *dev,
429 				    lis2dw12_mode_t pm)
430 {
431 	const struct lis2dw12_device_config *cfg = dev->config;
432 	stmdev_ctx_t *ctx = (stmdev_ctx_t *)&cfg->ctx;
433 	uint8_t regval = LIS2DW12_CONT_LOW_PWR_12bit;
434 
435 	switch (pm) {
436 	case LIS2DW12_CONT_LOW_PWR_2:
437 	case LIS2DW12_CONT_LOW_PWR_3:
438 	case LIS2DW12_CONT_LOW_PWR_4:
439 	case LIS2DW12_HIGH_PERFORMANCE:
440 		regval = pm;
441 		break;
442 	default:
443 		LOG_DBG("Apply default Power Mode");
444 		break;
445 	}
446 
447 	return lis2dw12_write_reg(ctx, LIS2DW12_CTRL1, &regval, 1);
448 }
449 
lis2dw12_set_low_noise(const struct device * dev,bool low_noise)450 static int lis2dw12_set_low_noise(const struct device *dev,
451 				  bool low_noise)
452 {
453 	const struct lis2dw12_device_config *cfg = dev->config;
454 	stmdev_ctx_t *ctx = (stmdev_ctx_t *)&cfg->ctx;
455 	lis2dw12_ctrl6_t ctrl6;
456 	int ret;
457 
458 	ret = lis2dw12_read_reg(ctx, LIS2DW12_CTRL6, (uint8_t *)&ctrl6, 1);
459 	if (ret < 0) {
460 		return ret;
461 	}
462 	ctrl6.low_noise = low_noise;
463 	return lis2dw12_write_reg(ctx, LIS2DW12_CTRL6, (uint8_t *)&ctrl6, 1);
464 }
465 
lis2dw12_init(const struct device * dev)466 static int lis2dw12_init(const struct device *dev)
467 {
468 	const struct lis2dw12_device_config *cfg = dev->config;
469 	struct lis2dw12_data *lis2dw12 = dev->data;
470 	stmdev_ctx_t *ctx = (stmdev_ctx_t *)&cfg->ctx;
471 	uint8_t wai;
472 	int ret;
473 
474 	/* check chip ID */
475 	ret = lis2dw12_device_id_get(ctx, &wai);
476 	if (ret < 0) {
477 		LOG_ERR("Not able to read dev id");
478 		return ret;
479 	}
480 
481 	if (wai != LIS2DW12_ID) {
482 		LOG_ERR("Invalid chip ID");
483 		return -EINVAL;
484 	}
485 
486 	/* reset device */
487 	ret = lis2dw12_reset_set(ctx, PROPERTY_ENABLE);
488 	if (ret < 0) {
489 		return ret;
490 	}
491 
492 	k_busy_wait(100);
493 
494 	ret = lis2dw12_block_data_update_set(ctx, PROPERTY_ENABLE);
495 	if (ret < 0) {
496 		LOG_ERR("Not able to set BDU");
497 		return ret;
498 	}
499 
500 	/* set power mode */
501 	LOG_DBG("power-mode is %d", cfg->pm);
502 	ret = lis2dw12_set_power_mode(dev, cfg->pm);
503 	if (ret < 0) {
504 		return ret;
505 	}
506 
507 	LOG_DBG("low noise is %d", cfg->low_noise);
508 	ret = lis2dw12_set_low_noise(dev, cfg->low_noise);
509 	if (ret < 0) {
510 		LOG_ERR("Failed to configure low_noise");
511 		return ret;
512 	}
513 	/* set the output data rate */
514 	ret = lis2dw12_set_odr(dev, cfg->odr);
515 	if (ret < 0) {
516 		LOG_ERR("odr init error %d", cfg->odr);
517 		return ret;
518 	}
519 
520 	lis2dw12->odr = cfg->odr;
521 
522 	LOG_DBG("range is %d", cfg->range);
523 	ret = lis2dw12_set_range(dev, LIS2DW12_FS_TO_REG(cfg->range));
524 	if (ret < 0) {
525 		LOG_ERR("range init error %d", cfg->range);
526 		return ret;
527 	}
528 
529 	LOG_DBG("bandwidth filter is %u", (int)cfg->bw_filt);
530 	lis2dw12_filter_bandwidth_set(ctx, cfg->bw_filt);
531 
532 #ifdef CONFIG_LIS2DW12_TRIGGER
533 	ret = lis2dw12_init_interrupt(dev);
534 	if (ret < 0) {
535 		LOG_ERR("Failed to initialize interrupts");
536 		return ret;
537 	}
538 #endif /* CONFIG_LIS2DW12_TRIGGER */
539 
540 	LOG_DBG("high pass reference mode is %d", (int)cfg->hp_ref_mode);
541 	ret = lis2dw12_reference_mode_set(ctx, cfg->hp_ref_mode);
542 	if (ret < 0) {
543 		LOG_ERR("high pass reference mode config error %d", (int)cfg->hp_ref_mode);
544 		return ret;
545 	}
546 
547 	LOG_DBG("high pass filter path is %d", (int)cfg->hp_filter_path);
548 	lis2dw12_fds_t fds = cfg->hp_filter_path ?
549 		LIS2DW12_HIGH_PASS_ON_OUT : LIS2DW12_LPF_ON_OUT;
550 	ret = lis2dw12_filter_path_set(ctx, fds);
551 	if (ret < 0) {
552 		LOG_ERR("filter path config error %d", (int)cfg->hp_filter_path);
553 		return ret;
554 	}
555 
556 #ifdef CONFIG_LIS2DW12_WAKEUP
557 	ret = lis2dw12_wkup_dur_set(ctx, cfg->wakeup_duration);
558 	if (ret < 0) {
559 		LOG_ERR("wakeup duration config error %d", ret);
560 		return ret;
561 	}
562 #endif /* CONFIG_LIS2DW12_WAKEUP */
563 #ifdef CONFIG_LIS2DW12_SLEEP
564 	ret = lis2dw12_act_sleep_dur_set(ctx, cfg->sleep_duration);
565 	if (ret < 0) {
566 		LOG_ERR("sleep duration config error %d", ret);
567 		return ret;
568 	}
569 #endif /* CONFIG_LIS2DW12_SLEEP */
570 
571 	return 0;
572 }
573 
574 #if DT_NUM_INST_STATUS_OKAY(DT_DRV_COMPAT) == 0
575 #warning "LIS2DW12 driver enabled without any devices"
576 #endif
577 
578 /*
579  * Device creation macro, shared by LIS2DW12_DEFINE_SPI() and
580  * LIS2DW12_DEFINE_I2C().
581  */
582 
583 #define LIS2DW12_DEVICE_INIT(inst)					\
584 	SENSOR_DEVICE_DT_INST_DEFINE(inst,				\
585 			    lis2dw12_init,				\
586 			    NULL,					\
587 			    &lis2dw12_data_##inst,			\
588 			    &lis2dw12_config_##inst,			\
589 			    POST_KERNEL,				\
590 			    CONFIG_SENSOR_INIT_PRIORITY,		\
591 			    &lis2dw12_driver_api);
592 
593 /*
594  * Instantiation macros used when a device is on a SPI bus.
595  */
596 
597 #ifdef CONFIG_LIS2DW12_TAP
598 #define LIS2DW12_CONFIG_TAP(inst)					\
599 	.tap_mode = DT_INST_PROP(inst, tap_mode),			\
600 	.tap_threshold = DT_INST_PROP(inst, tap_threshold),		\
601 	.tap_shock = DT_INST_PROP(inst, tap_shock),			\
602 	.tap_latency = DT_INST_PROP(inst, tap_latency),			\
603 	.tap_quiet = DT_INST_PROP(inst, tap_quiet),
604 #else
605 #define LIS2DW12_CONFIG_TAP(inst)
606 #endif /* CONFIG_LIS2DW12_TAP */
607 
608 #ifdef CONFIG_LIS2DW12_FREEFALL
609 #define LIS2DW12_CONFIG_FREEFALL(inst)					\
610 	.freefall_duration = DT_INST_PROP(inst, ff_duration),	\
611 	.freefall_threshold = DT_INST_PROP(inst, ff_threshold),
612 #else
613 #define LIS2DW12_CONFIG_FREEFALL(inst)
614 #endif /* CONFIG_LIS2DW12_FREEFALL */
615 
616 #ifdef CONFIG_LIS2DW12_WAKEUP
617 #define LIS2DW12_CONFIG_WAKEUP(inst)					\
618 	.wakeup_duration = DT_INST_PROP(inst, wakeup_duration),
619 #else
620 #define LIS2DW12_CONFIG_WAKEUP(inst)
621 #endif
622 
623 #ifdef CONFIG_LIS2DW12_SLEEP
624 #define LIS2DW12_CONFIG_SLEEP(inst) \
625 	.sleep_duration = DT_INST_PROP(inst, sleep_duration),
626 #else
627 #define LIS2DW12_CONFIG_SLEEP(inst)
628 #endif
629 
630 #ifdef CONFIG_LIS2DW12_TRIGGER
631 #define LIS2DW12_CFG_IRQ(inst) \
632 	.gpio_int = GPIO_DT_SPEC_INST_GET(inst, irq_gpios),		\
633 	.int_pin = DT_INST_PROP(inst, int_pin),
634 #else
635 #define LIS2DW12_CFG_IRQ(inst)
636 #endif /* CONFIG_LIS2DW12_TRIGGER */
637 
638 #define LIS2DW12_CONFIG_COMMON(inst)					\
639 	.pm = DT_INST_PROP(inst, power_mode),				\
640 	.odr = DT_INST_PROP_OR(inst, odr, 12),				\
641 	.range = DT_INST_PROP(inst, range),				\
642 	.bw_filt = DT_INST_PROP(inst, bw_filt),				\
643 	.low_noise = DT_INST_PROP(inst, low_noise),			\
644 	.hp_filter_path = DT_INST_PROP(inst, hp_filter_path),		\
645 	.hp_ref_mode = DT_INST_PROP(inst, hp_ref_mode),			\
646 	.drdy_pulsed = DT_INST_PROP(inst, drdy_pulsed),			\
647 	LIS2DW12_CONFIG_TAP(inst)					\
648 	LIS2DW12_CONFIG_FREEFALL(inst)					\
649 	LIS2DW12_CONFIG_WAKEUP(inst)					\
650 	LIS2DW12_CONFIG_SLEEP(inst)					\
651 	COND_CODE_1(DT_INST_NODE_HAS_PROP(inst, irq_gpios),		\
652 			(LIS2DW12_CFG_IRQ(inst)), ())
653 
654 #define LIS2DW12_SPI_OPERATION (SPI_WORD_SET(8) |			\
655 				SPI_OP_MODE_MASTER |			\
656 				SPI_MODE_CPOL |				\
657 				SPI_MODE_CPHA)				\
658 
659 #define LIS2DW12_CONFIG_SPI(inst)					\
660 	{								\
661 		STMEMSC_CTX_SPI(&lis2dw12_config_##inst.stmemsc_cfg),	\
662 		.stmemsc_cfg = {					\
663 			.spi = SPI_DT_SPEC_INST_GET(inst,		\
664 					   LIS2DW12_SPI_OPERATION,	\
665 					   0),				\
666 		},							\
667 		LIS2DW12_CONFIG_COMMON(inst)				\
668 	}
669 
670 /*
671  * Instantiation macros used when a device is on an I2C bus.
672  */
673 
674 #define LIS2DW12_CONFIG_I2C(inst)					\
675 	{								\
676 		STMEMSC_CTX_I2C(&lis2dw12_config_##inst.stmemsc_cfg),	\
677 		.stmemsc_cfg = {					\
678 			.i2c = I2C_DT_SPEC_INST_GET(inst),		\
679 		},							\
680 		LIS2DW12_CONFIG_COMMON(inst)				\
681 	}
682 
683 /*
684  * Main instantiation macro. Use of COND_CODE_1() selects the right
685  * bus-specific macro at preprocessor time.
686  */
687 
688 #define LIS2DW12_DEFINE(inst)						\
689 	static struct lis2dw12_data lis2dw12_data_##inst;		\
690 	static const struct lis2dw12_device_config lis2dw12_config_##inst =	\
691 	COND_CODE_1(DT_INST_ON_BUS(inst, spi),				\
692 		    (LIS2DW12_CONFIG_SPI(inst)),			\
693 		    (LIS2DW12_CONFIG_I2C(inst)));			\
694 	LIS2DW12_DEVICE_INIT(inst)
695 
696 DT_INST_FOREACH_STATUS_OKAY(LIS2DW12_DEFINE)
697