1 /*
2  * Copyright (c) 2017 Intel Corporation
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 #define DT_DRV_COMPAT st_lis2dh
8 
9 
10 #include <zephyr/init.h>
11 #include <zephyr/sys/byteorder.h>
12 #include <zephyr/sys/__assert.h>
13 #include <zephyr/logging/log.h>
14 #include <zephyr/pm/device.h>
15 
16 LOG_MODULE_REGISTER(lis2dh, CONFIG_SENSOR_LOG_LEVEL);
17 #include "lis2dh.h"
18 
19 #define ACCEL_SCALE(sensitivity)			\
20 	((SENSOR_G * (sensitivity) >> 14) / 100)
21 
22 /*
23  * Use values for low-power mode in DS "Mechanical (Sensor) characteristics",
24  * multiplied by 100.
25  */
26 static uint32_t lis2dh_reg_val_to_scale[] = {
27 	ACCEL_SCALE(1600),
28 	ACCEL_SCALE(3200),
29 	ACCEL_SCALE(6400),
30 	ACCEL_SCALE(19200),
31 };
32 
lis2dh_convert(int16_t raw_val,uint32_t scale,struct sensor_value * val)33 static void lis2dh_convert(int16_t raw_val, uint32_t scale,
34 			   struct sensor_value *val)
35 {
36 	int32_t converted_val;
37 
38 	/*
39 	 * maximum converted value we can get is: max(raw_val) * max(scale)
40 	 *	max(raw_val >> 4) = +/- 2^11
41 	 *	max(scale) = 114921
42 	 *	max(converted_val) = 235358208 which is less than 2^31
43 	 */
44 	converted_val = (raw_val >> 4) * scale;
45 	val->val1 = converted_val / 1000000;
46 	val->val2 = converted_val % 1000000;
47 }
48 
lis2dh_sample_fetch_temp(const struct device * dev)49 static int lis2dh_sample_fetch_temp(const struct device *dev)
50 {
51 	int ret = -ENOTSUP;
52 
53 #ifdef CONFIG_LIS2DH_MEASURE_TEMPERATURE
54 	struct lis2dh_data *lis2dh = dev->data;
55 	const struct lis2dh_config *cfg = dev->config;
56 	uint8_t raw[sizeof(uint16_t)];
57 
58 	ret = lis2dh->hw_tf->read_data(dev, cfg->temperature.dout_addr, raw,
59 				       sizeof(raw));
60 
61 	if (ret < 0) {
62 		LOG_WRN("Failed to fetch raw temperature sample");
63 		ret = -EIO;
64 	} else {
65 		/*
66 		 * The result contains a delta value for the
67 		 * temperature that must be added to the reference temperature set
68 		 * for your board to return an absolute temperature in Celsius.
69 		 *
70 		 * The data is left aligned.  Fixed point after first 8 bits.
71 		 */
72 		lis2dh->temperature.val1 = (int32_t)((int8_t)raw[1]);
73 		if (cfg->temperature.fractional_bits == 0) {
74 			lis2dh->temperature.val2 = 0;
75 		} else {
76 			lis2dh->temperature.val2 =
77 				(raw[0] >> (8 - cfg->temperature.fractional_bits));
78 			lis2dh->temperature.val2 = (lis2dh->temperature.val2 * 1000000);
79 			lis2dh->temperature.val2 >>= cfg->temperature.fractional_bits;
80 			if (lis2dh->temperature.val1 < 0) {
81 				lis2dh->temperature.val2 *= -1;
82 			}
83 		}
84 	}
85 #else
86 	LOG_WRN("Temperature measurement disabled");
87 #endif
88 
89 	return ret;
90 }
91 
lis2dh_channel_get(const struct device * dev,enum sensor_channel chan,struct sensor_value * val)92 static int lis2dh_channel_get(const struct device *dev,
93 			      enum sensor_channel chan,
94 			      struct sensor_value *val)
95 {
96 	struct lis2dh_data *lis2dh = dev->data;
97 	int ofs_start;
98 	int ofs_end;
99 	int i;
100 
101 	switch (chan) {
102 	case SENSOR_CHAN_ACCEL_X:
103 		ofs_start = ofs_end = 0;
104 		break;
105 	case SENSOR_CHAN_ACCEL_Y:
106 		ofs_start = ofs_end = 1;
107 		break;
108 	case SENSOR_CHAN_ACCEL_Z:
109 		ofs_start = ofs_end = 2;
110 		break;
111 	case SENSOR_CHAN_ACCEL_XYZ:
112 		ofs_start = 0;
113 		ofs_end = 2;
114 		break;
115 #ifdef CONFIG_LIS2DH_MEASURE_TEMPERATURE
116 	case SENSOR_CHAN_DIE_TEMP:
117 		memcpy(val, &lis2dh->temperature, sizeof(*val));
118 		return 0;
119 #endif
120 	default:
121 		return -ENOTSUP;
122 	}
123 
124 	for (i = ofs_start; i <= ofs_end; i++, val++) {
125 		lis2dh_convert(lis2dh->sample.xyz[i], lis2dh->scale, val);
126 	}
127 
128 	return 0;
129 }
130 
lis2dh_fetch_xyz(const struct device * dev,enum sensor_channel chan)131 static int lis2dh_fetch_xyz(const struct device *dev,
132 			       enum sensor_channel chan)
133 {
134 	struct lis2dh_data *lis2dh = dev->data;
135 	int status = -ENODATA;
136 	size_t i;
137 	/*
138 	 * since status and all accel data register addresses are consecutive,
139 	 * a burst read can be used to read all the samples
140 	 */
141 	status = lis2dh->hw_tf->read_data(dev, LIS2DH_REG_STATUS,
142 					  lis2dh->sample.raw,
143 					  sizeof(lis2dh->sample.raw));
144 	if (status < 0) {
145 		LOG_WRN("Could not read accel axis data");
146 		return status;
147 	}
148 
149 	for (i = 0; i < (3 * sizeof(int16_t)); i += sizeof(int16_t)) {
150 		int16_t *sample =
151 			(int16_t *)&lis2dh->sample.raw[1 + i];
152 
153 		*sample = sys_le16_to_cpu(*sample);
154 	}
155 
156 	if (lis2dh->sample.status & LIS2DH_STATUS_DRDY_MASK) {
157 		status = 0;
158 	}
159 
160 	return status;
161 }
162 
lis2dh_sample_fetch(const struct device * dev,enum sensor_channel chan)163 static int lis2dh_sample_fetch(const struct device *dev,
164 			       enum sensor_channel chan)
165 {
166 	int status = -ENODATA;
167 
168 	if (chan == SENSOR_CHAN_ALL) {
169 		status = lis2dh_fetch_xyz(dev, chan);
170 #ifdef CONFIG_LIS2DH_MEASURE_TEMPERATURE
171 		if (status == 0) {
172 			status = lis2dh_sample_fetch_temp(dev);
173 		}
174 #endif
175 	} else if (chan == SENSOR_CHAN_ACCEL_XYZ) {
176 		status = lis2dh_fetch_xyz(dev, chan);
177 	} else if (chan == SENSOR_CHAN_DIE_TEMP) {
178 		status = lis2dh_sample_fetch_temp(dev);
179 	} else {
180 		__ASSERT(false, "Invalid sensor channel in fetch");
181 	}
182 
183 	return status;
184 }
185 
186 #ifdef CONFIG_LIS2DH_ODR_RUNTIME
187 /* 1620 & 5376 are low power only */
188 static const uint16_t lis2dh_odr_map[] = {0, 1, 10, 25, 50, 100, 200, 400, 1620,
189 				       1344, 5376};
190 
lis2dh_freq_to_odr_val(uint16_t freq)191 static int lis2dh_freq_to_odr_val(uint16_t freq)
192 {
193 	size_t i;
194 
195 	for (i = 0; i < ARRAY_SIZE(lis2dh_odr_map); i++) {
196 		if (freq == lis2dh_odr_map[i]) {
197 			return i;
198 		}
199 	}
200 
201 	return -EINVAL;
202 }
203 
lis2dh_acc_odr_set(const struct device * dev,uint16_t freq)204 static int lis2dh_acc_odr_set(const struct device *dev, uint16_t freq)
205 {
206 	int odr;
207 	int status;
208 	uint8_t value;
209 	struct lis2dh_data *data = dev->data;
210 
211 	odr = lis2dh_freq_to_odr_val(freq);
212 	if (odr < 0) {
213 		return odr;
214 	}
215 
216 	status = data->hw_tf->read_reg(dev, LIS2DH_REG_CTRL1, &value);
217 	if (status < 0) {
218 		return status;
219 	}
220 
221 	/* some odr values cannot be set in certain power modes */
222 	if ((value & LIS2DH_LP_EN_BIT_MASK) == 0U && odr == LIS2DH_ODR_8) {
223 		return -ENOTSUP;
224 	}
225 
226 	/* adjust odr index for LP enabled mode, see table above */
227 	if (((value & LIS2DH_LP_EN_BIT_MASK) == LIS2DH_LP_EN_BIT_MASK) &&
228 		(odr == LIS2DH_ODR_9 + 1)) {
229 		odr--;
230 	}
231 
232 	return data->hw_tf->write_reg(dev, LIS2DH_REG_CTRL1,
233 				      (value & ~LIS2DH_ODR_MASK) |
234 				      LIS2DH_ODR_RATE(odr));
235 }
236 #endif
237 
238 #ifdef CONFIG_LIS2DH_ACCEL_RANGE_RUNTIME
239 
240 #define LIS2DH_RANGE_IDX_TO_VALUE(idx)		(1 << ((idx) + 1))
241 #define LIS2DH_NUM_RANGES			4
242 
lis2dh_range_to_reg_val(uint16_t range)243 static int lis2dh_range_to_reg_val(uint16_t range)
244 {
245 	int i;
246 
247 	for (i = 0; i < LIS2DH_NUM_RANGES; i++) {
248 		if (range == LIS2DH_RANGE_IDX_TO_VALUE(i)) {
249 			return i;
250 		}
251 	}
252 
253 	return -EINVAL;
254 }
255 
lis2dh_acc_range_set(const struct device * dev,int32_t range)256 static int lis2dh_acc_range_set(const struct device *dev, int32_t range)
257 {
258 	struct lis2dh_data *lis2dh = dev->data;
259 	int fs;
260 
261 	fs = lis2dh_range_to_reg_val(range);
262 	if (fs < 0) {
263 		return fs;
264 	}
265 
266 	lis2dh->scale = lis2dh_reg_val_to_scale[fs];
267 
268 	return lis2dh->hw_tf->update_reg(dev, LIS2DH_REG_CTRL4,
269 					 LIS2DH_FS_MASK,
270 					 (fs << LIS2DH_FS_SHIFT));
271 }
272 #endif
273 
lis2dh_acc_config(const struct device * dev,enum sensor_channel chan,enum sensor_attribute attr,const struct sensor_value * val)274 static int lis2dh_acc_config(const struct device *dev,
275 			     enum sensor_channel chan,
276 			     enum sensor_attribute attr,
277 			     const struct sensor_value *val)
278 {
279 	switch (attr) {
280 #ifdef CONFIG_LIS2DH_ACCEL_RANGE_RUNTIME
281 	case SENSOR_ATTR_FULL_SCALE:
282 		return lis2dh_acc_range_set(dev, sensor_ms2_to_g(val));
283 #endif
284 #ifdef CONFIG_LIS2DH_ODR_RUNTIME
285 	case SENSOR_ATTR_SAMPLING_FREQUENCY:
286 		return lis2dh_acc_odr_set(dev, val->val1);
287 #endif
288 #if defined(CONFIG_LIS2DH_TRIGGER)
289 	case SENSOR_ATTR_SLOPE_TH:
290 	case SENSOR_ATTR_SLOPE_DUR:
291 		return lis2dh_acc_slope_config(dev, attr, val);
292 #endif
293 #ifdef CONFIG_LIS2DH_ACCEL_HP_FILTERS
294 	case SENSOR_ATTR_CONFIGURATION:
295 		return lis2dh_acc_hp_filter_set(dev, val->val1);
296 #endif
297 	default:
298 		LOG_DBG("Accel attribute not supported.");
299 		return -ENOTSUP;
300 	}
301 
302 	return 0;
303 }
304 
lis2dh_attr_set(const struct device * dev,enum sensor_channel chan,enum sensor_attribute attr,const struct sensor_value * val)305 static int lis2dh_attr_set(const struct device *dev, enum sensor_channel chan,
306 			   enum sensor_attribute attr,
307 			   const struct sensor_value *val)
308 {
309 	switch (chan) {
310 	case SENSOR_CHAN_ACCEL_X:
311 	case SENSOR_CHAN_ACCEL_Y:
312 	case SENSOR_CHAN_ACCEL_Z:
313 	case SENSOR_CHAN_ACCEL_XYZ:
314 		return lis2dh_acc_config(dev, chan, attr, val);
315 	default:
316 		LOG_WRN("attr_set() not supported on this channel.");
317 		return -ENOTSUP;
318 	}
319 
320 	return 0;
321 }
322 
323 static DEVICE_API(sensor, lis2dh_driver_api) = {
324 	.attr_set = lis2dh_attr_set,
325 #if CONFIG_LIS2DH_TRIGGER
326 	.trigger_set = lis2dh_trigger_set,
327 #endif
328 	.sample_fetch = lis2dh_sample_fetch,
329 	.channel_get = lis2dh_channel_get,
330 };
331 
lis2dh_init(const struct device * dev)332 int lis2dh_init(const struct device *dev)
333 {
334 	struct lis2dh_data *lis2dh = dev->data;
335 	const struct lis2dh_config *cfg = dev->config;
336 	int status;
337 	uint8_t id;
338 	uint8_t raw[6];
339 
340 	status = cfg->bus_init(dev);
341 	if (status < 0) {
342 		return status;
343 	}
344 
345 	status = lis2dh->hw_tf->read_reg(dev, LIS2DH_REG_WAI, &id);
346 	if (status < 0) {
347 		LOG_ERR("Failed to read chip id.");
348 		return status;
349 	}
350 
351 	if (id != LIS2DH_CHIP_ID) {
352 		LOG_ERR("Invalid chip ID: %02x\n", id);
353 		return -EINVAL;
354 	}
355 
356 	/* Fix LSM303AGR_ACCEL device scale values */
357 	if (cfg->hw.is_lsm303agr_dev) {
358 		lis2dh_reg_val_to_scale[0] = ACCEL_SCALE(1563);
359 		lis2dh_reg_val_to_scale[1] = ACCEL_SCALE(3126);
360 		lis2dh_reg_val_to_scale[2] = ACCEL_SCALE(6252);
361 		lis2dh_reg_val_to_scale[3] = ACCEL_SCALE(18758);
362 	}
363 
364 	if (cfg->hw.disc_pull_up) {
365 		status = lis2dh->hw_tf->update_reg(dev, LIS2DH_REG_CTRL0,
366 						   LIS2DH_SDO_PU_DISC_MASK,
367 						   LIS2DH_SDO_PU_DISC_MASK);
368 		if (status < 0) {
369 			LOG_ERR("Failed to disconnect SDO/SA0 pull-up.");
370 			return status;
371 		}
372 	}
373 
374 	/* Initialize control register ctrl1 to ctrl 6 to default boot values
375 	 * to avoid warm start/reset issues as the accelerometer has no reset
376 	 * pin. Register values are retained if power is not removed.
377 	 * Default values see LIS2DH documentation page 30, chapter 6.
378 	 */
379 	(void)memset(raw, 0, sizeof(raw));
380 	raw[0] = LIS2DH_ACCEL_EN_BITS;
381 
382 	status = lis2dh->hw_tf->write_data(dev, LIS2DH_REG_CTRL1, raw,
383 					   sizeof(raw));
384 
385 	if (status < 0) {
386 		LOG_ERR("Failed to reset ctrl registers.");
387 		return status;
388 	}
389 
390 	/* set full scale range and store it for later conversion */
391 	lis2dh->scale = lis2dh_reg_val_to_scale[LIS2DH_FS_IDX];
392 #ifdef CONFIG_LIS2DH_BLOCK_DATA_UPDATE
393 	status = lis2dh->hw_tf->write_reg(dev, LIS2DH_REG_CTRL4,
394 					  LIS2DH_FS_BITS | LIS2DH_HR_BIT | LIS2DH_CTRL4_BDU_BIT);
395 #else
396 	status = lis2dh->hw_tf->write_reg(dev, LIS2DH_REG_CTRL4, LIS2DH_FS_BITS | LIS2DH_HR_BIT);
397 #endif
398 
399 	if (status < 0) {
400 		LOG_ERR("Failed to set full scale ctrl register.");
401 		return status;
402 	}
403 
404 #ifdef CONFIG_LIS2DH_MEASURE_TEMPERATURE
405 	status = lis2dh->hw_tf->update_reg(dev, cfg->temperature.cfg_addr,
406 					   cfg->temperature.enable_mask,
407 					   cfg->temperature.enable_mask);
408 
409 	if (status < 0) {
410 		LOG_ERR("Failed to enable temperature measurement");
411 		return status;
412 	}
413 #endif
414 
415 #ifdef CONFIG_LIS2DH_TRIGGER
416 	if (cfg->gpio_drdy.port != NULL || cfg->gpio_int.port != NULL) {
417 		status = lis2dh_init_interrupt(dev);
418 		if (status < 0) {
419 			LOG_ERR("Failed to initialize interrupts.");
420 			return status;
421 		}
422 	}
423 #endif
424 
425 	LOG_INF("fs=%d, odr=0x%x lp_en=0x%x scale=%d", 1 << (LIS2DH_FS_IDX + 1), LIS2DH_ODR_IDX,
426 		(uint8_t)LIS2DH_LP_EN_BIT, lis2dh->scale);
427 
428 	/* enable accel measurements and set power mode and data rate */
429 	return lis2dh->hw_tf->write_reg(dev, LIS2DH_REG_CTRL1,
430 					LIS2DH_ACCEL_EN_BITS | LIS2DH_LP_EN_BIT |
431 					LIS2DH_ODR_BITS);
432 }
433 
434 #ifdef CONFIG_PM_DEVICE
lis2dh_pm_action(const struct device * dev,enum pm_device_action action)435 static int lis2dh_pm_action(const struct device *dev,
436 			    enum pm_device_action action)
437 {
438 	int status;
439 	struct lis2dh_data *lis2dh = dev->data;
440 	uint8_t regdata;
441 
442 	switch (action) {
443 	case PM_DEVICE_ACTION_RESUME:
444 		/* read REFERENCE register (see datasheet rev 6 section 8.9 footnote 1) */
445 		status = lis2dh->hw_tf->read_reg(dev, LIS2DH_REG_REFERENCE, &regdata);
446 		if (status < 0) {
447 			LOG_ERR("failed to read reg_reference");
448 			return status;
449 		}
450 
451 		/* Resume previous mode. */
452 		status = lis2dh->hw_tf->write_reg(dev, LIS2DH_REG_CTRL1,
453 						  lis2dh->reg_ctrl1_active_val);
454 		if (status < 0) {
455 			LOG_ERR("failed to write reg_crtl1");
456 			return status;
457 		}
458 		break;
459 	case PM_DEVICE_ACTION_SUSPEND:
460 		/* Store current mode, suspend. */
461 		status = lis2dh->hw_tf->read_reg(dev, LIS2DH_REG_CTRL1,
462 						 &lis2dh->reg_ctrl1_active_val);
463 		if (status < 0) {
464 			LOG_ERR("failed to read reg_crtl1");
465 			return status;
466 		}
467 		status = lis2dh->hw_tf->write_reg(dev, LIS2DH_REG_CTRL1,
468 						  LIS2DH_SUSPEND);
469 		if (status < 0) {
470 			LOG_ERR("failed to write reg_crtl1");
471 			return status;
472 		}
473 		break;
474 	default:
475 		return -ENOTSUP;
476 	}
477 
478 	return 0;
479 }
480 #endif /* CONFIG_PM_DEVICE */
481 
482 #if DT_NUM_INST_STATUS_OKAY(DT_DRV_COMPAT) == 0
483 #warning "LIS2DH driver enabled without any devices"
484 #endif
485 
486 /*
487  * Device creation macro, shared by LIS2DH_DEFINE_SPI() and
488  * LIS2DH_DEFINE_I2C().
489  */
490 
491 #define LIS2DH_DEVICE_INIT(inst)					\
492 	PM_DEVICE_DT_INST_DEFINE(inst, lis2dh_pm_action);		\
493 	SENSOR_DEVICE_DT_INST_DEFINE(inst,				\
494 			    lis2dh_init,				\
495 			    PM_DEVICE_DT_INST_GET(inst),		\
496 			    &lis2dh_data_##inst,			\
497 			    &lis2dh_config_##inst,			\
498 			    POST_KERNEL,				\
499 			    CONFIG_SENSOR_INIT_PRIORITY,		\
500 			    &lis2dh_driver_api);
501 
502 #define IS_LSM303AGR_DEV(inst) \
503 	DT_INST_NODE_HAS_COMPAT(inst, st_lsm303agr_accel)
504 
505 #define DISC_PULL_UP(inst) \
506 	DT_INST_PROP(inst, disconnect_sdo_sa0_pull_up)
507 
508 #define ANYM_ON_INT1(inst) \
509 	DT_INST_PROP(inst, anym_on_int1)
510 
511 #define ANYM_LATCH(inst) \
512 	!DT_INST_PROP(inst, anym_no_latch)
513 
514 #define ANYM_MODE(inst) \
515 	DT_INST_PROP(inst, anym_mode)
516 
517 #ifdef CONFIG_LIS2DH_TRIGGER
518 #define GPIO_DT_SPEC_INST_GET_BY_IDX_COND(id, prop, idx)		\
519 	COND_CODE_1(DT_INST_PROP_HAS_IDX(id, prop, idx),		\
520 		    (GPIO_DT_SPEC_INST_GET_BY_IDX(id, prop, idx)),	\
521 		    ({.port = NULL, .pin = 0, .dt_flags = 0}))
522 
523 #define LIS2DH_CFG_INT(inst)				\
524 	.gpio_drdy =							\
525 	    COND_CODE_1(ANYM_ON_INT1(inst),		\
526 		({.port = NULL, .pin = 0, .dt_flags = 0}),                  \
527 		(GPIO_DT_SPEC_INST_GET_BY_IDX_COND(inst, irq_gpios, 0))),	\
528 	.gpio_int =								\
529 	    COND_CODE_1(ANYM_ON_INT1(inst),		\
530 		(GPIO_DT_SPEC_INST_GET_BY_IDX_COND(inst, irq_gpios, 0)),	\
531 		(GPIO_DT_SPEC_INST_GET_BY_IDX_COND(inst, irq_gpios, 1))),	\
532 	.int1_mode = DT_INST_PROP(inst, int1_gpio_config),			\
533 	.int2_mode = DT_INST_PROP(inst, int2_gpio_config),
534 #else
535 #define LIS2DH_CFG_INT(inst)
536 #endif /* CONFIG_LIS2DH_TRIGGER */
537 
538 #ifdef CONFIG_LIS2DH_MEASURE_TEMPERATURE
539 /* The first 8 bits are the integer portion of the temperature.
540  * The result is left justified.  The remainder of the bits are
541  * the fractional part.
542  *
543  * LIS2DH has 8 total bits.
544  * LIS2DH12/LIS3DH have 10 bits unless they are in lower power mode.
545  * compat(lis2dh) cannot be used here because it is the base part.
546  */
547 #define FRACTIONAL_BITS(inst)	\
548 	(DT_INST_NODE_HAS_COMPAT(inst, st_lis2dh12) ||				\
549 	 DT_INST_NODE_HAS_COMPAT(inst, st_lis3dh)) ?				\
550 		      (IS_ENABLED(CONFIG_LIS2DH_OPER_MODE_LOW_POWER) ? 0 : 2) : \
551 		      0
552 
553 #define LIS2DH_CFG_TEMPERATURE(inst)	\
554 	.temperature = { .cfg_addr = 0x1F,	\
555 			 .enable_mask = 0xC0,		\
556 			 .dout_addr = 0x0C,			\
557 			 .fractional_bits = FRACTIONAL_BITS(inst) },
558 #else
559 #define LIS2DH_CFG_TEMPERATURE(inst)
560 #endif /* CONFIG_LIS2DH_MEASURE_TEMPERATURE */
561 
562 #define LIS2DH_CONFIG_SPI(inst)						\
563 	{								\
564 		.bus_init = lis2dh_spi_init,				\
565 		.bus_cfg = { .spi = SPI_DT_SPEC_INST_GET(inst,		\
566 					SPI_WORD_SET(8) |		\
567 					SPI_OP_MODE_MASTER |		\
568 					SPI_MODE_CPOL |			\
569 					SPI_MODE_CPHA,			\
570 					0) },				\
571 		.hw = { .is_lsm303agr_dev = IS_LSM303AGR_DEV(inst),	\
572 			.disc_pull_up = DISC_PULL_UP(inst),		\
573 			.anym_on_int1 = ANYM_ON_INT1(inst),		\
574 			.anym_latch = ANYM_LATCH(inst),			\
575 			.anym_mode = ANYM_MODE(inst), },		\
576 		LIS2DH_CFG_TEMPERATURE(inst)				\
577 		LIS2DH_CFG_INT(inst)					\
578 	}
579 
580 #define LIS2DH_DEFINE_SPI(inst)						\
581 	static struct lis2dh_data lis2dh_data_##inst;			\
582 	static const struct lis2dh_config lis2dh_config_##inst =	\
583 		LIS2DH_CONFIG_SPI(inst);				\
584 	LIS2DH_DEVICE_INIT(inst)
585 
586 /*
587  * Instantiation macros used when a device is on an I2C bus.
588  */
589 
590 #define LIS2DH_CONFIG_I2C(inst)						\
591 	{								\
592 		.bus_init = lis2dh_i2c_init,				\
593 		.bus_cfg = { .i2c = I2C_DT_SPEC_INST_GET(inst), },	\
594 		.hw = { .is_lsm303agr_dev = IS_LSM303AGR_DEV(inst),	\
595 			.disc_pull_up = DISC_PULL_UP(inst),		\
596 			.anym_on_int1 = ANYM_ON_INT1(inst),		\
597 			.anym_latch = ANYM_LATCH(inst),			\
598 			.anym_mode = ANYM_MODE(inst), },		\
599 		LIS2DH_CFG_TEMPERATURE(inst)				\
600 		LIS2DH_CFG_INT(inst)					\
601 	}
602 
603 #define LIS2DH_DEFINE_I2C(inst)						\
604 	static struct lis2dh_data lis2dh_data_##inst;			\
605 	static const struct lis2dh_config lis2dh_config_##inst =	\
606 		LIS2DH_CONFIG_I2C(inst);				\
607 	LIS2DH_DEVICE_INIT(inst)
608 /*
609  * Main instantiation macro. Use of COND_CODE_1() selects the right
610  * bus-specific macro at preprocessor time.
611  */
612 
613 #define LIS2DH_DEFINE(inst)						\
614 	COND_CODE_1(DT_INST_ON_BUS(inst, spi),				\
615 		    (LIS2DH_DEFINE_SPI(inst)),				\
616 		    (LIS2DH_DEFINE_I2C(inst)))
617 
618 DT_INST_FOREACH_STATUS_OKAY(LIS2DH_DEFINE)
619