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 Celcius to keep it efficient */
101 dval_uc = ((lis2dw12->temp >> LIS2DW12_SHIFT_TEMP) * LIS2DW12_TEMP_SCALE_FACTOR);
102 dval_uc += 25000000;
103
104 /* switch to Celcius 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
sensor_ms2_to_mg(const struct sensor_value * ms2)194 static inline int32_t sensor_ms2_to_mg(const struct sensor_value *ms2)
195 {
196 int64_t nano_ms2 = (ms2->val1 * 1000000LL + ms2->val2) * 1000LL;
197
198 if (nano_ms2 > 0) {
199 return (nano_ms2 + SENSOR_G / 2) / SENSOR_G;
200 } else {
201 return (nano_ms2 - SENSOR_G / 2) / SENSOR_G;
202 }
203 }
204
205 #if (CONFIG_LIS2DW12_SLEEP || CONFIG_LIS2DW12_WAKEUP)
206
207 /* Converts a lis2dw12_fs_t range to its value in milli-g
208 * Range can be 2/4/8/16G
209 */
210 #define FS_RANGE_TO_MG(fs_range) ((2U << fs_range) * 1000U)
211
212 /* Converts a range in mg to the lsb value for the WK_THS register
213 * For the reg value: 1 LSB = 1/64 of FS
214 * Range can be 2/4/8/16G
215 */
216 #define MG_TO_WK_THS_LSB(range_mg) (range_mg / 64)
217
218 /* Calculates the WK_THS reg value
219 * from the threshold in mg and the lsb value in mg
220 * with correct integer rounding
221 */
222 #define THRESHOLD_MG_TO_WK_THS_REG(thr_mg, lsb_mg) \
223 ((thr_mg + (lsb_mg / 2)) / lsb_mg)
224
lis2dw12_attr_set_thresh(const struct device * dev,enum sensor_channel chan,enum sensor_attribute attr,const struct sensor_value * val)225 static int lis2dw12_attr_set_thresh(const struct device *dev,
226 enum sensor_channel chan,
227 enum sensor_attribute attr,
228 const struct sensor_value *val)
229 {
230 uint8_t reg;
231 size_t ret;
232 int lsb_mg;
233 const struct lis2dw12_device_config *cfg = dev->config;
234 stmdev_ctx_t *ctx = (stmdev_ctx_t *)&cfg->ctx;
235
236 LOG_DBG("%s on channel %d", __func__, chan);
237
238 /* can only be set for all directions at once */
239 if (chan != SENSOR_CHAN_ACCEL_XYZ) {
240 return -EINVAL;
241 }
242
243 /* Configure wakeup threshold threshold. */
244 lis2dw12_fs_t range;
245 int err = lis2dw12_full_scale_get(ctx, &range);
246
247 if (err) {
248 return err;
249 }
250
251 uint32_t thr_mg = abs(sensor_ms2_to_mg(val));
252
253 /* Check maximum value: depends on current FS value */
254 if (thr_mg >= FS_RANGE_TO_MG(range)) {
255 return -EINVAL;
256 }
257
258 /* The threshold is applied to both positive and negative data:
259 * for a wake-up interrupt generation at least one of the three axes must be
260 * bigger than the threshold.
261 */
262 lsb_mg = MG_TO_WK_THS_LSB(FS_RANGE_TO_MG(range));
263 reg = THRESHOLD_MG_TO_WK_THS_REG(thr_mg, lsb_mg);
264
265 LOG_DBG("Threshold %d mg -> fs: %u mg -> reg = %d LSBs",
266 thr_mg, FS_RANGE_TO_MG(range), reg);
267 ret = 0;
268
269 return lis2dw12_wkup_threshold_set(ctx, reg);
270 }
271 #endif
272
273 #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)274 static int lis2dw12_attr_set_ff_dur(const struct device *dev,
275 enum sensor_channel chan,
276 enum sensor_attribute attr,
277 const struct sensor_value *val)
278 {
279 int rc;
280 uint16_t duration;
281 const struct lis2dw12_device_config *cfg = dev->config;
282 struct lis2dw12_data *lis2dw12 = dev->data;
283 stmdev_ctx_t *ctx = (stmdev_ctx_t *)&cfg->ctx;
284
285 LOG_DBG("%s on channel %d", __func__, chan);
286
287 /* can only be set for all directions at once */
288 if (chan != SENSOR_CHAN_ACCEL_XYZ) {
289 return -EINVAL;
290 }
291
292 /**
293 * The given duration in milliseconds with the val
294 * parameter is converted into register specific value.
295 */
296 duration = (lis2dw12->odr * (uint16_t)sensor_value_to_double(val)) / 1000;
297
298 LOG_DBG("Freefall: duration is %d ms", (uint16_t)sensor_value_to_double(val));
299 rc = lis2dw12_ff_dur_set(ctx, duration);
300 if (rc != 0) {
301 LOG_ERR("Failed to set freefall duration");
302 return -EIO;
303 }
304 return rc;
305 }
306 #endif
307
308 #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)309 static int lis2dw12_attr_set_act_mode(const struct device *dev,
310 enum sensor_channel chan,
311 enum sensor_attribute attr,
312 const struct sensor_value *val)
313 {
314 const struct lis2dw12_device_config *cfg = dev->config;
315 struct lis2dw12_data *lis2dw12 = dev->data;
316 stmdev_ctx_t *ctx = (stmdev_ctx_t *)&cfg->ctx;
317 lis2dw12_sleep_on_t sleep_val = val->val1 & 0x03U;
318
319 /* can only be set for all directions at once */
320 if (chan != SENSOR_CHAN_ACCEL_XYZ) {
321 return -EINVAL;
322 }
323
324 return lis2dw12_act_mode_set(ctx, sleep_val);
325 }
326 #endif
327
lis2dw12_attr_set(const struct device * dev,enum sensor_channel chan,enum sensor_attribute attr,const struct sensor_value * val)328 static int lis2dw12_attr_set(const struct device *dev,
329 enum sensor_channel chan,
330 enum sensor_attribute attr,
331 const struct sensor_value *val)
332 {
333 switch (attr) {
334 #if (CONFIG_LIS2DW12_SLEEP || CONFIG_LIS2DW12_WAKEUP)
335 case SENSOR_ATTR_UPPER_THRESH:
336 case SENSOR_ATTR_LOWER_THRESH:
337 return lis2dw12_attr_set_thresh(dev, chan, attr, val);
338 #endif
339 #ifdef CONFIG_LIS2DW12_FREEFALL
340 case SENSOR_ATTR_FF_DUR:
341 return lis2dw12_attr_set_ff_dur(dev, chan, attr, val);
342 #endif
343 #ifdef CONFIG_LIS2DW12_SLEEP
344 case SENSOR_ATTR_FEATURE_MASK:
345 return lis2dw12_attr_set_act_mode(dev, chan, attr, val);
346 #endif
347 default:
348 /* Do nothing */
349 break;
350 }
351
352
353 switch (chan) {
354 case SENSOR_CHAN_ACCEL_X:
355 case SENSOR_CHAN_ACCEL_Y:
356 case SENSOR_CHAN_ACCEL_Z:
357 case SENSOR_CHAN_ACCEL_XYZ:
358 return lis2dw12_config(dev, chan, attr, val);
359 default:
360 LOG_DBG("Attr not supported on %d channel", chan);
361 break;
362 }
363
364 return -ENOTSUP;
365 }
366
lis2dw12_sample_fetch_accel(const struct device * dev)367 static int lis2dw12_sample_fetch_accel(const struct device *dev)
368 {
369 struct lis2dw12_data *lis2dw12 = dev->data;
370 const struct lis2dw12_device_config *cfg = dev->config;
371 stmdev_ctx_t *ctx = (stmdev_ctx_t *)&cfg->ctx;
372 uint8_t shift;
373 int16_t buf[3];
374
375 /* fetch acceleration raw data sample */
376 if (lis2dw12_acceleration_raw_get(ctx, buf) < 0) {
377 LOG_DBG("Failed to fetch acceleration raw data sample");
378 return -EIO;
379 }
380
381 /* adjust to resolution */
382 if (cfg->pm == LIS2DW12_CONT_LOW_PWR_12bit) {
383 shift = LIS2DW12_SHIFT_PM1;
384 } else {
385 shift = LIS2DW12_SHIFT_PMOTHER;
386 }
387
388 lis2dw12->acc[0] = buf[0] >> shift;
389 lis2dw12->acc[1] = buf[1] >> shift;
390 lis2dw12->acc[2] = buf[2] >> shift;
391
392 return 0;
393 }
394
lis2dw12_sample_fetch_temp(const struct device * dev)395 static int lis2dw12_sample_fetch_temp(const struct device *dev)
396 {
397 const struct lis2dw12_device_config *cfg = dev->config;
398 stmdev_ctx_t *ctx = (stmdev_ctx_t *)&cfg->ctx;
399 struct lis2dw12_data *data = dev->data;
400
401 /* fetch temperature raw data sample */
402 if (lis2dw12_temperature_raw_get(ctx, &data->temp) < 0) {
403 LOG_DBG("Failed to fetch temperature raw data sample");
404 return -EIO;
405 }
406
407 return 0;
408 }
409
lis2dw12_sample_fetch(const struct device * dev,enum sensor_channel chan)410 static int lis2dw12_sample_fetch(const struct device *dev,
411 enum sensor_channel chan)
412 {
413 switch (chan) {
414 case SENSOR_CHAN_ACCEL_X:
415 case SENSOR_CHAN_ACCEL_Y:
416 case SENSOR_CHAN_ACCEL_Z:
417 case SENSOR_CHAN_ACCEL_XYZ:
418 lis2dw12_sample_fetch_accel(dev);
419 break;
420 case SENSOR_CHAN_DIE_TEMP:
421 lis2dw12_sample_fetch_temp(dev);
422 break;
423 default:
424 LOG_DBG("Channel not supported");
425 return -ENOTSUP;
426 }
427
428 return 0;
429 }
430
431 static DEVICE_API(sensor, lis2dw12_driver_api) = {
432 .attr_set = lis2dw12_attr_set,
433 #if CONFIG_LIS2DW12_TRIGGER
434 .trigger_set = lis2dw12_trigger_set,
435 #endif /* CONFIG_LIS2DW12_TRIGGER */
436 .sample_fetch = lis2dw12_sample_fetch,
437 .channel_get = lis2dw12_channel_get,
438 };
439
lis2dw12_set_power_mode(const struct device * dev,lis2dw12_mode_t pm)440 static int lis2dw12_set_power_mode(const struct device *dev,
441 lis2dw12_mode_t pm)
442 {
443 const struct lis2dw12_device_config *cfg = dev->config;
444 stmdev_ctx_t *ctx = (stmdev_ctx_t *)&cfg->ctx;
445 uint8_t regval = LIS2DW12_CONT_LOW_PWR_12bit;
446
447 switch (pm) {
448 case LIS2DW12_CONT_LOW_PWR_2:
449 case LIS2DW12_CONT_LOW_PWR_3:
450 case LIS2DW12_CONT_LOW_PWR_4:
451 case LIS2DW12_HIGH_PERFORMANCE:
452 regval = pm;
453 break;
454 default:
455 LOG_DBG("Apply default Power Mode");
456 break;
457 }
458
459 return lis2dw12_write_reg(ctx, LIS2DW12_CTRL1, ®val, 1);
460 }
461
lis2dw12_set_low_noise(const struct device * dev,bool low_noise)462 static int lis2dw12_set_low_noise(const struct device *dev,
463 bool low_noise)
464 {
465 const struct lis2dw12_device_config *cfg = dev->config;
466 stmdev_ctx_t *ctx = (stmdev_ctx_t *)&cfg->ctx;
467 lis2dw12_ctrl6_t ctrl6;
468 int ret;
469
470 ret = lis2dw12_read_reg(ctx, LIS2DW12_CTRL6, (uint8_t *)&ctrl6, 1);
471 if (ret < 0) {
472 return ret;
473 }
474 ctrl6.low_noise = low_noise;
475 return lis2dw12_write_reg(ctx, LIS2DW12_CTRL6, (uint8_t *)&ctrl6, 1);
476 }
477
lis2dw12_init(const struct device * dev)478 static int lis2dw12_init(const struct device *dev)
479 {
480 const struct lis2dw12_device_config *cfg = dev->config;
481 struct lis2dw12_data *lis2dw12 = dev->data;
482 stmdev_ctx_t *ctx = (stmdev_ctx_t *)&cfg->ctx;
483 uint8_t wai;
484 int ret;
485
486 /* check chip ID */
487 ret = lis2dw12_device_id_get(ctx, &wai);
488 if (ret < 0) {
489 LOG_ERR("Not able to read dev id");
490 return ret;
491 }
492
493 if (wai != LIS2DW12_ID) {
494 LOG_ERR("Invalid chip ID");
495 return -EINVAL;
496 }
497
498 /* reset device */
499 ret = lis2dw12_reset_set(ctx, PROPERTY_ENABLE);
500 if (ret < 0) {
501 return ret;
502 }
503
504 k_busy_wait(100);
505
506 ret = lis2dw12_block_data_update_set(ctx, PROPERTY_ENABLE);
507 if (ret < 0) {
508 LOG_ERR("Not able to set BDU");
509 return ret;
510 }
511
512 /* set power mode */
513 LOG_DBG("power-mode is %d", cfg->pm);
514 ret = lis2dw12_set_power_mode(dev, cfg->pm);
515 if (ret < 0) {
516 return ret;
517 }
518
519 LOG_DBG("low noise is %d", cfg->low_noise);
520 ret = lis2dw12_set_low_noise(dev, cfg->low_noise);
521 if (ret < 0) {
522 LOG_ERR("Failed to configure low_noise");
523 return ret;
524 }
525 /* set the output data rate */
526 ret = lis2dw12_set_odr(dev, cfg->odr);
527 if (ret < 0) {
528 LOG_ERR("odr init error %d", cfg->odr);
529 return ret;
530 }
531
532 lis2dw12->odr = cfg->odr;
533
534 LOG_DBG("range is %d", cfg->range);
535 ret = lis2dw12_set_range(dev, LIS2DW12_FS_TO_REG(cfg->range));
536 if (ret < 0) {
537 LOG_ERR("range init error %d", cfg->range);
538 return ret;
539 }
540
541 LOG_DBG("bandwidth filter is %u", (int)cfg->bw_filt);
542 lis2dw12_filter_bandwidth_set(ctx, cfg->bw_filt);
543
544 #ifdef CONFIG_LIS2DW12_TRIGGER
545 ret = lis2dw12_init_interrupt(dev);
546 if (ret < 0) {
547 LOG_ERR("Failed to initialize interrupts");
548 return ret;
549 }
550 #endif /* CONFIG_LIS2DW12_TRIGGER */
551
552 LOG_DBG("high pass reference mode is %d", (int)cfg->hp_ref_mode);
553 ret = lis2dw12_reference_mode_set(ctx, cfg->hp_ref_mode);
554 if (ret < 0) {
555 LOG_ERR("high pass reference mode config error %d", (int)cfg->hp_ref_mode);
556 return ret;
557 }
558
559 LOG_DBG("high pass filter path is %d", (int)cfg->hp_filter_path);
560 lis2dw12_fds_t fds = cfg->hp_filter_path ?
561 LIS2DW12_HIGH_PASS_ON_OUT : LIS2DW12_LPF_ON_OUT;
562 ret = lis2dw12_filter_path_set(ctx, fds);
563 if (ret < 0) {
564 LOG_ERR("filter path config error %d", (int)cfg->hp_filter_path);
565 return ret;
566 }
567
568 #ifdef CONFIG_LIS2DW12_WAKEUP
569 ret = lis2dw12_wkup_dur_set(ctx, cfg->wakeup_duration);
570 if (ret < 0) {
571 LOG_ERR("wakeup duration config error %d", ret);
572 return ret;
573 }
574 #endif /* CONFIG_LIS2DW12_WAKEUP */
575 #ifdef CONFIG_LIS2DW12_SLEEP
576 ret = lis2dw12_act_sleep_dur_set(ctx, cfg->sleep_duration);
577 if (ret < 0) {
578 LOG_ERR("sleep duration config error %d", ret);
579 return ret;
580 }
581 #endif /* CONFIG_LIS2DW12_SLEEP */
582
583 return 0;
584 }
585
586 #if DT_NUM_INST_STATUS_OKAY(DT_DRV_COMPAT) == 0
587 #warning "LIS2DW12 driver enabled without any devices"
588 #endif
589
590 /*
591 * Device creation macro, shared by LIS2DW12_DEFINE_SPI() and
592 * LIS2DW12_DEFINE_I2C().
593 */
594
595 #define LIS2DW12_DEVICE_INIT(inst) \
596 SENSOR_DEVICE_DT_INST_DEFINE(inst, \
597 lis2dw12_init, \
598 NULL, \
599 &lis2dw12_data_##inst, \
600 &lis2dw12_config_##inst, \
601 POST_KERNEL, \
602 CONFIG_SENSOR_INIT_PRIORITY, \
603 &lis2dw12_driver_api);
604
605 /*
606 * Instantiation macros used when a device is on a SPI bus.
607 */
608
609 #ifdef CONFIG_LIS2DW12_TAP
610 #define LIS2DW12_CONFIG_TAP(inst) \
611 .tap_mode = DT_INST_PROP(inst, tap_mode), \
612 .tap_threshold = DT_INST_PROP(inst, tap_threshold), \
613 .tap_shock = DT_INST_PROP(inst, tap_shock), \
614 .tap_latency = DT_INST_PROP(inst, tap_latency), \
615 .tap_quiet = DT_INST_PROP(inst, tap_quiet),
616 #else
617 #define LIS2DW12_CONFIG_TAP(inst)
618 #endif /* CONFIG_LIS2DW12_TAP */
619
620 #ifdef CONFIG_LIS2DW12_FREEFALL
621 #define LIS2DW12_CONFIG_FREEFALL(inst) \
622 .freefall_duration = DT_INST_PROP(inst, ff_duration), \
623 .freefall_threshold = DT_INST_PROP(inst, ff_threshold),
624 #else
625 #define LIS2DW12_CONFIG_FREEFALL(inst)
626 #endif /* CONFIG_LIS2DW12_FREEFALL */
627
628 #ifdef CONFIG_LIS2DW12_WAKEUP
629 #define LIS2DW12_CONFIG_WAKEUP(inst) \
630 .wakeup_duration = DT_INST_PROP(inst, wakeup_duration),
631 #else
632 #define LIS2DW12_CONFIG_WAKEUP(inst)
633 #endif
634
635 #ifdef CONFIG_LIS2DW12_SLEEP
636 #define LIS2DW12_CONFIG_SLEEP(inst) \
637 .sleep_duration = DT_INST_PROP(inst, sleep_duration),
638 #else
639 #define LIS2DW12_CONFIG_SLEEP(inst)
640 #endif
641
642 #ifdef CONFIG_LIS2DW12_TRIGGER
643 #define LIS2DW12_CFG_IRQ(inst) \
644 .gpio_int = GPIO_DT_SPEC_INST_GET(inst, irq_gpios), \
645 .int_pin = DT_INST_PROP(inst, int_pin),
646 #else
647 #define LIS2DW12_CFG_IRQ(inst)
648 #endif /* CONFIG_LIS2DW12_TRIGGER */
649
650 #define LIS2DW12_CONFIG_COMMON(inst) \
651 .pm = DT_INST_PROP(inst, power_mode), \
652 .odr = DT_INST_PROP_OR(inst, odr, 12), \
653 .range = DT_INST_PROP(inst, range), \
654 .bw_filt = DT_INST_PROP(inst, bw_filt), \
655 .low_noise = DT_INST_PROP(inst, low_noise), \
656 .hp_filter_path = DT_INST_PROP(inst, hp_filter_path), \
657 .hp_ref_mode = DT_INST_PROP(inst, hp_ref_mode), \
658 .drdy_pulsed = DT_INST_PROP(inst, drdy_pulsed), \
659 LIS2DW12_CONFIG_TAP(inst) \
660 LIS2DW12_CONFIG_FREEFALL(inst) \
661 LIS2DW12_CONFIG_WAKEUP(inst) \
662 LIS2DW12_CONFIG_SLEEP(inst) \
663 COND_CODE_1(DT_INST_NODE_HAS_PROP(inst, irq_gpios), \
664 (LIS2DW12_CFG_IRQ(inst)), ())
665
666 #define LIS2DW12_SPI_OPERATION (SPI_WORD_SET(8) | \
667 SPI_OP_MODE_MASTER | \
668 SPI_MODE_CPOL | \
669 SPI_MODE_CPHA) \
670
671 #define LIS2DW12_CONFIG_SPI(inst) \
672 { \
673 STMEMSC_CTX_SPI(&lis2dw12_config_##inst.stmemsc_cfg), \
674 .stmemsc_cfg = { \
675 .spi = SPI_DT_SPEC_INST_GET(inst, \
676 LIS2DW12_SPI_OPERATION, \
677 0), \
678 }, \
679 LIS2DW12_CONFIG_COMMON(inst) \
680 }
681
682 /*
683 * Instantiation macros used when a device is on an I2C bus.
684 */
685
686 #define LIS2DW12_CONFIG_I2C(inst) \
687 { \
688 STMEMSC_CTX_I2C(&lis2dw12_config_##inst.stmemsc_cfg), \
689 .stmemsc_cfg = { \
690 .i2c = I2C_DT_SPEC_INST_GET(inst), \
691 }, \
692 LIS2DW12_CONFIG_COMMON(inst) \
693 }
694
695 /*
696 * Main instantiation macro. Use of COND_CODE_1() selects the right
697 * bus-specific macro at preprocessor time.
698 */
699
700 #define LIS2DW12_DEFINE(inst) \
701 static struct lis2dw12_data lis2dw12_data_##inst; \
702 static const struct lis2dw12_device_config lis2dw12_config_##inst = \
703 COND_CODE_1(DT_INST_ON_BUS(inst, spi), \
704 (LIS2DW12_CONFIG_SPI(inst)), \
705 (LIS2DW12_CONFIG_I2C(inst))); \
706 LIS2DW12_DEVICE_INIT(inst)
707
708 DT_INST_FOREACH_STATUS_OKAY(LIS2DW12_DEFINE)
709