1 /*
2  * SPDX-FileCopyrightText: Copyright (c) 2023 Carl Zeiss Meditec AG
3  * SPDX-FileCopyrightText: Copyright (c) 2024 Jilay Sandeep Pandya
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 #define DT_DRV_COMPAT adi_adltc2990
8 
9 #include <stdbool.h>
10 
11 #include <zephyr/sys/util.h>
12 #include <zephyr/drivers/i2c.h>
13 #include <zephyr/drivers/sensor/adltc2990.h>
14 
15 #include "adltc2990_reg.h"
16 #include "adltc2990_internal.h"
17 
18 #include <zephyr/logging/log.h>
19 LOG_MODULE_REGISTER(adltc2990, CONFIG_SENSOR_LOG_LEVEL);
20 
adltc2990_get_v1_v2_measurement_modes(uint8_t mode_4_3,uint8_t mode_2_0)21 static enum adltc2990_monitoring_type adltc2990_get_v1_v2_measurement_modes(uint8_t mode_4_3,
22 									    uint8_t mode_2_0)
23 {
24 	if (mode_2_0 > ADLTC2990_MODE_2_0_MAX_VALUE || mode_4_3 > ADLTC2990_MODE_4_3_MAX_VALUE) {
25 		LOG_ERR("Invalid Measurement Mode");
26 		return -EINVAL;
27 	}
28 	if (mode_4_3 == ADLTC2990_MEASURE_INTERNAL_TEMPERATURE_ONLY ||
29 	    mode_4_3 == ADLTC2990_MEASURE_PINS_V3_V4_ONLY) {
30 		return NOTHING;
31 	}
32 
33 	enum adltc2990_monitoring_type type = NOTHING;
34 
35 	switch (mode_2_0) {
36 	case ADLTC2990_MODE_V1_V2_TR2:
37 	case ADLTC2990_MODE_V1_V2_V3_V4:
38 		type = VOLTAGE_SINGLEENDED;
39 		break;
40 
41 	case ADLTC2990_MODE_V1_MINUS_V2_TR2:
42 	case ADLTC2990_MODE_V1_MINUS_V2_V3_V4:
43 	case ADLTC2990_MODE_V1_MINUS_V2_V3_MINUS_V4:
44 		type = VOLTAGE_DIFFERENTIAL;
45 		break;
46 
47 	case ADLTC2990_MODE_TR1_V3_V4:
48 	case ADLTC2990_MODE_TR1_V3_MINUS_V4:
49 	case ADLTC2990_MODE_TR1_TR2:
50 		type = TEMPERATURE;
51 		break;
52 
53 	default:
54 		break;
55 	}
56 	return type;
57 }
58 
adltc2990_get_v3_v4_measurement_modes(uint8_t mode_4_3,uint8_t mode_2_0)59 static enum adltc2990_monitoring_type adltc2990_get_v3_v4_measurement_modes(uint8_t mode_4_3,
60 									    uint8_t mode_2_0)
61 {
62 	if (mode_2_0 > ADLTC2990_MODE_2_0_MAX_VALUE || mode_4_3 > ADLTC2990_MODE_4_3_MAX_VALUE) {
63 		LOG_ERR("Invalid Measurement Mode");
64 		return -EINVAL;
65 	}
66 	if (mode_4_3 == ADLTC2990_MEASURE_INTERNAL_TEMPERATURE_ONLY ||
67 	    mode_4_3 == ADLTC2990_MEASURE_PINS_V1_V2_ONLY) {
68 		return NOTHING;
69 	}
70 
71 	enum adltc2990_monitoring_type type = NOTHING;
72 
73 	switch (mode_2_0) {
74 	case ADLTC2990_MODE_V1_V2_TR2:
75 	case ADLTC2990_MODE_V1_MINUS_V2_TR2:
76 	case ADLTC2990_MODE_TR1_TR2:
77 		type = TEMPERATURE;
78 		break;
79 
80 	case ADLTC2990_MODE_V1_MINUS_V2_V3_V4:
81 	case ADLTC2990_MODE_TR1_V3_V4:
82 	case ADLTC2990_MODE_V1_V2_V3_V4:
83 		type = VOLTAGE_SINGLEENDED;
84 		break;
85 
86 	case ADLTC2990_MODE_TR1_V3_MINUS_V4:
87 	case ADLTC2990_MODE_V1_MINUS_V2_V3_MINUS_V4:
88 		type = VOLTAGE_DIFFERENTIAL;
89 		break;
90 
91 	default:
92 		break;
93 	}
94 	return type;
95 }
96 
adltc2990_is_busy(const struct device * dev,bool * is_busy)97 int adltc2990_is_busy(const struct device *dev, bool *is_busy)
98 {
99 	const struct adltc2990_config *cfg = dev->config;
100 	uint8_t status_reg = 0;
101 
102 	if (i2c_reg_read_byte_dt(&cfg->bus, ADLTC2990_REG_STATUS, &status_reg)) {
103 		return -EIO;
104 	}
105 
106 	*is_busy = status_reg & BIT(0);
107 
108 	return 0;
109 }
110 
adltc2990_get_v1_v2_val(const struct device * dev,struct sensor_value * val,uint8_t num_values,uint8_t * const offset_index)111 static void adltc2990_get_v1_v2_val(const struct device *dev, struct sensor_value *val,
112 				    uint8_t num_values, uint8_t *const offset_index)
113 {
114 	struct adltc2990_data *data = dev->data;
115 
116 	for (uint8_t index = 0; index < num_values; index++) {
117 		val[index].val1 = data->pins_v1_v2_values[index] / 1000000;
118 		val[index].val2 = data->pins_v1_v2_values[index] % 1000000;
119 		*offset_index = index + 1;
120 	}
121 }
122 
adltc2990_get_v3_v4_val(const struct device * dev,struct sensor_value * val,uint8_t num_values,uint8_t const * const offset)123 static void adltc2990_get_v3_v4_val(const struct device *dev, struct sensor_value *val,
124 				    uint8_t num_values, uint8_t const *const offset)
125 {
126 	struct adltc2990_data *data = dev->data;
127 
128 	uint8_t offset_index = *offset;
129 
130 	for (uint8_t index = 0; index < num_values; index++) {
131 		val[index + offset_index].val1 = data->pins_v3_v4_values[index] / 1000000;
132 		val[index + offset_index].val2 = data->pins_v3_v4_values[index] % 1000000;
133 	}
134 }
135 
adltc2990_trigger_measurement(const struct device * dev,enum adltc2990_acquisition_format format)136 int adltc2990_trigger_measurement(const struct device *dev,
137 				  enum adltc2990_acquisition_format format)
138 {
139 	const struct adltc2990_config *cfg = dev->config;
140 	struct adltc2990_data *data = dev->data;
141 
142 	if (data->acq_format == format) {
143 		goto trigger_conversion;
144 	}
145 
146 	data->acq_format = format;
147 	uint8_t ctrl_reg_setting;
148 
149 	if (i2c_reg_read_byte_dt(&cfg->bus, ADLTC2990_REG_CONTROL, &ctrl_reg_setting)) {
150 		LOG_ERR("reading control register failed.");
151 		return -EIO;
152 	}
153 
154 	ctrl_reg_setting |= format << 6;
155 	if (i2c_reg_write_byte_dt(&cfg->bus, ADLTC2990_REG_CONTROL, ctrl_reg_setting)) {
156 		LOG_ERR("configuring for single bus failed.");
157 		return -EIO;
158 	}
159 
160 trigger_conversion:
161 	return i2c_reg_write_byte_dt(&cfg->bus, ADLTC2990_REG_TRIGGER, 0x1);
162 }
163 
adltc2990_fetch_property_value(const struct device * dev,enum adltc2990_monitoring_type type,enum adltc2990_monitor_pins pin,int32_t * output)164 static int adltc2990_fetch_property_value(const struct device *dev,
165 					  enum adltc2990_monitoring_type type,
166 					  enum adltc2990_monitor_pins pin, int32_t *output)
167 {
168 	const struct adltc2990_config *cfg = dev->config;
169 
170 	uint8_t msb_value = 0, lsb_value = 0;
171 	uint8_t msb_address, lsb_address;
172 
173 	switch (pin) {
174 	case V1:
175 		msb_address = ADLTC2990_REG_V1_MSB;
176 		lsb_address = ADLTC2990_REG_V1_LSB;
177 		break;
178 
179 	case V2:
180 		msb_address = ADLTC2990_REG_V2_MSB;
181 		lsb_address = ADLTC2990_REG_V2_LSB;
182 		break;
183 
184 	case V3:
185 		msb_address = ADLTC2990_REG_V3_MSB;
186 		lsb_address = ADLTC2990_REG_V3_LSB;
187 		break;
188 
189 	case V4:
190 		msb_address = ADLTC2990_REG_V4_MSB;
191 		lsb_address = ADLTC2990_REG_V4_LSB;
192 		break;
193 
194 	case INTERNAL_TEMPERATURE:
195 		msb_address = ADLTC2990_REG_INTERNAL_TEMP_MSB;
196 		lsb_address = ADLTC2990_REG_INTERNAL_TEMP_LSB;
197 		break;
198 
199 	case SUPPLY_VOLTAGE:
200 		msb_address = ADLTC2990_REG_VCC_MSB;
201 		lsb_address = ADLTC2990_REG_VCC_LSB;
202 		break;
203 
204 	default:
205 		LOG_ERR("Trying to access illegal register");
206 		return -EINVAL;
207 	}
208 
209 	if (i2c_reg_read_byte_dt(&cfg->bus, msb_address, &msb_value)) {
210 		return -EIO;
211 	}
212 
213 	if (i2c_reg_read_byte_dt(&cfg->bus, lsb_address, &lsb_value)) {
214 		return -EIO;
215 	}
216 
217 	uint16_t conversion_factor;
218 	uint8_t negative_bit_index = 14U, sensor_val_divisor = 100U;
219 
220 	if (type == VOLTAGE_SINGLEENDED) {
221 		conversion_factor = ADLTC2990_VOLTAGE_SINGLEENDED_CONVERSION_FACTOR;
222 	} else if (type == VOLTAGE_DIFFERENTIAL) {
223 		conversion_factor = ADLTC2990_VOLTAGE_DIFFERENTIAL_CONVERSION_FACTOR;
224 	} else if (type == TEMPERATURE) {
225 		conversion_factor = ADLTC2990_TEMPERATURE_CONVERSION_FACTOR;
226 		if (cfg->temp_format == ADLTC2990_TEMPERATURE_FORMAT_CELSIUS) {
227 			negative_bit_index = 12U;
228 		}
229 		sensor_val_divisor = 1U;
230 	} else {
231 		LOG_ERR("unknown type");
232 		return -EINVAL;
233 	}
234 
235 	int16_t value = (msb_value << 8) + lsb_value;
236 
237 	int32_t voltage_value = (value << (31 - negative_bit_index)) >> (31 - negative_bit_index);
238 
239 	*output = (voltage_value * conversion_factor) / sensor_val_divisor;
240 
241 	return 0;
242 }
243 
adltc2990_init(const struct device * dev)244 static int adltc2990_init(const struct device *dev)
245 {
246 	const struct adltc2990_config *cfg = dev->config;
247 	struct adltc2990_data *data = dev->data;
248 	int err;
249 
250 	if (!i2c_is_ready_dt(&cfg->bus)) {
251 		LOG_ERR("I2C bus %s not ready", cfg->bus.bus->name);
252 		return -ENODEV;
253 	}
254 
255 	const uint8_t ctrl_reg_setting = cfg->temp_format << 7 | data->acq_format << 6 | 0 << 5 |
256 					 cfg->measurement_mode[1] << 3 | cfg->measurement_mode[0];
257 
258 	LOG_DBG("Setting Control Register to: 0x%x", ctrl_reg_setting);
259 	if (i2c_reg_write_byte_dt(&cfg->bus, ADLTC2990_REG_CONTROL, ctrl_reg_setting)) {
260 		LOG_ERR("configuring for single bus failed.");
261 		return -EIO;
262 	}
263 
264 	err = adltc2990_trigger_measurement(dev, data->acq_format);
265 	if (err < 0) {
266 		LOG_ERR("triggering measurement failed: %d", err);
267 	}
268 
269 	LOG_INF("Initializing ADLTC2990 with name %s", dev->name);
270 	return 0;
271 }
272 
fetch_pin_differential_voltage_value(const struct device * dev,const enum adltc2990_monitoring_type mode,const enum adltc2990_monitor_pins pin)273 static int fetch_pin_differential_voltage_value(const struct device *dev,
274 						const enum adltc2990_monitoring_type mode,
275 						const enum adltc2990_monitor_pins pin)
276 {
277 	if (mode != VOLTAGE_DIFFERENTIAL) {
278 		LOG_DBG("Pin is not configured to measure voltage differential");
279 		return 0;
280 	}
281 
282 	struct adltc2990_data *data = dev->data;
283 	int32_t value;
284 	int ret;
285 
286 	ret = adltc2990_fetch_property_value(dev, VOLTAGE_DIFFERENTIAL, pin, &value);
287 	if (ret) {
288 		return ret;
289 	}
290 
291 	switch (pin) {
292 	case V1:
293 	case V2:
294 		data->pins_v1_v2_values[0] = value;
295 		break;
296 
297 	case V3:
298 	case V4:
299 		data->pins_v3_v4_values[0] = value;
300 	default:
301 		break;
302 	}
303 
304 	return 0;
305 }
306 
fetch_pin_single_ended_voltage_value(const struct device * dev,const enum adltc2990_monitoring_type mode,const enum adltc2990_monitor_pins pin_1,const enum adltc2990_monitor_pins pin_2)307 static int fetch_pin_single_ended_voltage_value(const struct device *dev,
308 						const enum adltc2990_monitoring_type mode,
309 						const enum adltc2990_monitor_pins pin_1,
310 						const enum adltc2990_monitor_pins pin_2)
311 {
312 	if (mode != VOLTAGE_SINGLEENDED) {
313 		LOG_DBG("Pin is not configured to measure voltage single ended");
314 		return 0;
315 	}
316 
317 	const struct adltc2990_config *cfg = dev->config;
318 	struct adltc2990_data *data = dev->data;
319 	int value_1, value_2, ret;
320 
321 	ret = adltc2990_fetch_property_value(dev, VOLTAGE_SINGLEENDED, pin_1, &value_1);
322 	if (ret) {
323 		return ret;
324 	}
325 
326 	ret = adltc2990_fetch_property_value(dev, VOLTAGE_SINGLEENDED, pin_2, &value_2);
327 	if (ret) {
328 		return ret;
329 	}
330 
331 	if (pin_1 == V1 && pin_2 == V2) {
332 
333 		uint32_t v1_r1 = cfg->pins_v1_v2.voltage_divider_resistors.v1_r1_r2[0];
334 		uint32_t v1_r2 = cfg->pins_v1_v2.voltage_divider_resistors.v1_r1_r2[1];
335 
336 		float voltage_divider_ratio = (v1_r1 + v1_r2) / (float)v1_r2;
337 
338 		data->pins_v1_v2_values[0] = value_1 * voltage_divider_ratio;
339 
340 		uint32_t v2_r1 = cfg->pins_v1_v2.voltage_divider_resistors.v2_r1_r2[0];
341 		uint32_t v2_r2 = cfg->pins_v1_v2.voltage_divider_resistors.v2_r1_r2[1];
342 
343 		voltage_divider_ratio = (v2_r1 + v2_r2) / (float)v2_r2;
344 
345 		data->pins_v1_v2_values[1] = value_2 * voltage_divider_ratio;
346 
347 	} else if (pin_1 == V3 && pin_2 == V4) {
348 
349 		uint32_t v3_r1 = cfg->pins_v3_v4.voltage_divider_resistors.v3_r1_r2[0];
350 		uint32_t v3_r2 = cfg->pins_v3_v4.voltage_divider_resistors.v3_r1_r2[1];
351 
352 		float voltage_divider_ratio = (v3_r1 + v3_r2) / (float)v3_r2;
353 
354 		data->pins_v3_v4_values[0] = value_1 * voltage_divider_ratio;
355 
356 		uint32_t v4_r1 = cfg->pins_v3_v4.voltage_divider_resistors.v4_r1_r2[0];
357 		uint32_t v4_r2 = cfg->pins_v3_v4.voltage_divider_resistors.v4_r1_r2[1];
358 
359 		voltage_divider_ratio = (v4_r1 + v4_r2) / (float)v4_r2;
360 
361 		data->pins_v3_v4_values[1] = value_2 * voltage_divider_ratio;
362 
363 	} else {
364 		LOG_ERR("Invalid pin configuration");
365 		return -EINVAL;
366 	}
367 	return 0;
368 }
369 
fetch_pin_temperature_value(const struct device * dev,const enum adltc2990_monitoring_type mode,const enum adltc2990_monitor_pins pin)370 static int fetch_pin_temperature_value(const struct device *dev,
371 				       const enum adltc2990_monitoring_type mode,
372 				       const enum adltc2990_monitor_pins pin)
373 {
374 	if (mode != TEMPERATURE) {
375 		LOG_DBG("Pin is not configured to measure temperature");
376 		return 0;
377 	}
378 
379 	struct adltc2990_data *data = dev->data;
380 	int32_t value;
381 	int ret;
382 
383 	ret = adltc2990_fetch_property_value(dev, TEMPERATURE, pin, &value);
384 	if (ret) {
385 		return ret;
386 	}
387 
388 	switch (pin) {
389 	case V1:
390 	case V2:
391 		data->pins_v1_v2_values[0] = value;
392 		break;
393 
394 	case V3:
395 	case V4:
396 		data->pins_v3_v4_values[0] = value;
397 	default:
398 		break;
399 	}
400 
401 	return 0;
402 }
403 
fetch_pin_current_value(const struct device * dev,const enum adltc2990_monitoring_type mode,const enum adltc2990_monitor_pins pin)404 static int fetch_pin_current_value(const struct device *dev,
405 				   const enum adltc2990_monitoring_type mode,
406 				   const enum adltc2990_monitor_pins pin)
407 {
408 	int ret;
409 
410 	ret = fetch_pin_differential_voltage_value(dev, mode, pin);
411 	if (ret) {
412 		return ret;
413 	}
414 
415 	const struct adltc2990_config *cfg = dev->config;
416 	struct adltc2990_data *data = dev->data;
417 
418 	switch (pin) {
419 	case V1:
420 	case V2:
421 		data->pins_v1_v2_values[0] *= (ADLTC2990_MICROOHM_CONVERSION_FACTOR /
422 					       (float)cfg->pins_v1_v2.pins_current_resistor);
423 		break;
424 
425 	case V3:
426 	case V4:
427 		data->pins_v3_v4_values[0] *= (ADLTC2990_MICROOHM_CONVERSION_FACTOR /
428 					       (float)cfg->pins_v3_v4.pins_current_resistor);
429 		break;
430 
431 	default:
432 		break;
433 	}
434 
435 	return 0;
436 }
437 
adltc2990_sample_fetch(const struct device * dev,enum sensor_channel chan)438 static int adltc2990_sample_fetch(const struct device *dev, enum sensor_channel chan)
439 {
440 	struct adltc2990_data *data = dev->data;
441 	const struct adltc2990_config *cfg = dev->config;
442 	enum adltc2990_monitoring_type mode_v1_v2 = adltc2990_get_v1_v2_measurement_modes(
443 		cfg->measurement_mode[1], cfg->measurement_mode[0]);
444 	enum adltc2990_monitoring_type mode_v3_v4 = adltc2990_get_v3_v4_measurement_modes(
445 		cfg->measurement_mode[1], cfg->measurement_mode[0]);
446 	int ret;
447 	int32_t value;
448 
449 	switch (chan) {
450 	case SENSOR_CHAN_DIE_TEMP:
451 		ret = adltc2990_fetch_property_value(dev, TEMPERATURE, INTERNAL_TEMPERATURE,
452 						     &value);
453 		if (ret) {
454 			return ret;
455 		}
456 		data->internal_temperature = value;
457 		break;
458 
459 	case SENSOR_CHAN_CURRENT:
460 		if (!(mode_v1_v2 == VOLTAGE_DIFFERENTIAL || mode_v3_v4 == VOLTAGE_DIFFERENTIAL)) {
461 			LOG_ERR("Sensor is not configured to measure Current");
462 			return -EINVAL;
463 		}
464 
465 		ret = fetch_pin_current_value(dev, mode_v1_v2, V1);
466 		if (ret) {
467 			return ret;
468 		}
469 
470 		ret = fetch_pin_current_value(dev, mode_v3_v4, V3);
471 		if (ret) {
472 			return ret;
473 		}
474 
475 		break;
476 
477 	case SENSOR_CHAN_VOLTAGE:
478 		ret = adltc2990_fetch_property_value(dev, VOLTAGE_SINGLEENDED, SUPPLY_VOLTAGE,
479 						     &value);
480 		if (ret) {
481 			return ret;
482 		}
483 		data->supply_voltage = value + 2500000;
484 
485 		ret = fetch_pin_differential_voltage_value(dev, mode_v1_v2, V1);
486 		if (ret) {
487 			return ret;
488 		}
489 
490 		ret = fetch_pin_differential_voltage_value(dev, mode_v3_v4, V3);
491 		if (ret) {
492 			return ret;
493 		}
494 
495 		ret = fetch_pin_single_ended_voltage_value(dev, mode_v1_v2, V1, V2);
496 		if (ret) {
497 			return ret;
498 		}
499 
500 		ret = fetch_pin_single_ended_voltage_value(dev, mode_v3_v4, V3, V4);
501 		if (ret) {
502 			return ret;
503 		}
504 
505 		break;
506 
507 	case SENSOR_CHAN_AMBIENT_TEMP:
508 		if (!(mode_v1_v2 == TEMPERATURE || mode_v3_v4 == TEMPERATURE)) {
509 			LOG_ERR("Sensor is not configured to measure Ambient Temperature");
510 			return -EINVAL;
511 		}
512 
513 		ret = fetch_pin_temperature_value(dev, mode_v1_v2, V1);
514 		if (ret) {
515 			return ret;
516 		}
517 
518 		ret = fetch_pin_temperature_value(dev, mode_v3_v4, V3);
519 		if (ret) {
520 			return ret;
521 		}
522 		break;
523 
524 	default:
525 		LOG_ERR("does not measure channel: %d", chan);
526 		return -ENOTSUP;
527 	}
528 
529 	return 0;
530 }
531 
adltc2990_channel_get(const struct device * dev,enum sensor_channel chan,struct sensor_value * val)532 static int adltc2990_channel_get(const struct device *dev, enum sensor_channel chan,
533 				 struct sensor_value *val)
534 {
535 	if (val == NULL) {
536 		LOG_ERR("Argument of type sensor_value* cannot be null ");
537 		return -EINVAL;
538 	}
539 	struct adltc2990_data *data = dev->data;
540 	const struct adltc2990_config *cfg = dev->config;
541 	enum adltc2990_monitoring_type mode_v1_v2 = adltc2990_get_v1_v2_measurement_modes(
542 		cfg->measurement_mode[1], cfg->measurement_mode[0]);
543 	enum adltc2990_monitoring_type mode_v3_v4 = adltc2990_get_v3_v4_measurement_modes(
544 		cfg->measurement_mode[1], cfg->measurement_mode[0]);
545 
546 	uint8_t offset_index = 0, num_values_v1_v2 = 0, num_values_v3_v4 = 0;
547 
548 	switch (chan) {
549 	case SENSOR_CHAN_DIE_TEMP:
550 		val->val1 = (data->internal_temperature) / 1000000;
551 		val->val2 = (data->internal_temperature) % 1000000;
552 		LOG_DBG("Internal Temperature Value is:%d.%d", val->val1, val->val2);
553 		break;
554 
555 	case SENSOR_CHAN_VOLTAGE:
556 		if (mode_v1_v2 == VOLTAGE_SINGLEENDED) {
557 			LOG_DBG("Getting V1,V2");
558 			num_values_v1_v2 = ADLTC2990_VOLTAGE_SINGLE_ENDED_VALUES;
559 		} else if (mode_v1_v2 == VOLTAGE_DIFFERENTIAL) {
560 			LOG_DBG("Getting V3-V4");
561 			num_values_v1_v2 = ADLTC2990_VOLTAGE_DIFF_VALUES;
562 		}
563 		if (mode_v3_v4 == VOLTAGE_SINGLEENDED) {
564 			LOG_DBG("Getting V3,V4");
565 			num_values_v3_v4 = ADLTC2990_VOLTAGE_SINGLE_ENDED_VALUES;
566 		} else if (mode_v3_v4 == VOLTAGE_DIFFERENTIAL) {
567 			LOG_DBG("Getting V3-V4");
568 			num_values_v3_v4 = ADLTC2990_VOLTAGE_DIFF_VALUES;
569 		}
570 		/* Add VCC to the last index */
571 		val[num_values_v1_v2 + num_values_v3_v4].val1 = data->supply_voltage / 1000000;
572 		val[num_values_v1_v2 + num_values_v3_v4].val2 = data->supply_voltage % 1000000;
573 		break;
574 
575 	case SENSOR_CHAN_CURRENT:
576 		if (!(mode_v1_v2 == VOLTAGE_DIFFERENTIAL || mode_v3_v4 == VOLTAGE_DIFFERENTIAL)) {
577 			LOG_ERR("Sensor is not configured to measure Current");
578 			return -EINVAL;
579 		}
580 		if (mode_v1_v2 == VOLTAGE_DIFFERENTIAL && mode_v3_v4 == VOLTAGE_DIFFERENTIAL) {
581 			LOG_DBG("Getting I12 and I34");
582 			num_values_v1_v2 = ADLTC2990_CURRENT_VALUES;
583 			num_values_v3_v4 = ADLTC2990_CURRENT_VALUES;
584 		} else if (mode_v1_v2 == VOLTAGE_DIFFERENTIAL) {
585 			LOG_DBG("Getting I12");
586 			num_values_v1_v2 = ADLTC2990_CURRENT_VALUES;
587 		} else if (mode_v3_v4 == VOLTAGE_DIFFERENTIAL) {
588 			LOG_DBG("Getting I34");
589 			num_values_v3_v4 = ADLTC2990_CURRENT_VALUES;
590 		}
591 		break;
592 
593 	case SENSOR_CHAN_AMBIENT_TEMP:
594 		if (!(mode_v1_v2 == TEMPERATURE || mode_v3_v4 == TEMPERATURE)) {
595 			LOG_ERR("Sensor is not configured to measure Ambient Temperature");
596 			return -EINVAL;
597 		}
598 		if (mode_v1_v2 == TEMPERATURE && mode_v3_v4 == TEMPERATURE) {
599 			LOG_DBG("Getting T12 and T34");
600 			num_values_v1_v2 = ADLTC2990_TEMP_VALUES;
601 			num_values_v3_v4 = ADLTC2990_TEMP_VALUES;
602 		} else if (mode_v1_v2 == TEMPERATURE) {
603 			LOG_DBG("Getting T12");
604 			num_values_v1_v2 = ADLTC2990_TEMP_VALUES;
605 		} else if (mode_v3_v4 == TEMPERATURE) {
606 			LOG_DBG("Getting T34");
607 			num_values_v3_v4 = ADLTC2990_TEMP_VALUES;
608 		}
609 		break;
610 
611 	default:
612 		return -ENOTSUP;
613 	}
614 
615 	adltc2990_get_v1_v2_val(dev, val, num_values_v1_v2, &offset_index);
616 	adltc2990_get_v3_v4_val(dev, val, num_values_v3_v4, &offset_index);
617 	return 0;
618 }
619 
620 static DEVICE_API(sensor, adltc2990_driver_api) = {
621 	.sample_fetch = adltc2990_sample_fetch,
622 	.channel_get = adltc2990_channel_get,
623 };
624 
625 #define ADLTC2990_DEFINE(inst)                                                                     \
626 	static struct adltc2990_data adltc2990_data_##inst = {                                     \
627 		.acq_format = DT_INST_PROP(inst, acquistion_format),                               \
628 	};                                                                                         \
629 	static const struct adltc2990_config adltc2990_config_##inst = {                           \
630 		.bus = I2C_DT_SPEC_INST_GET(inst),                                                 \
631 		.temp_format = DT_INST_PROP(inst, temperature_format),                             \
632 		.measurement_mode = DT_INST_PROP(inst, measurement_mode),                          \
633 		.pins_v1_v2.pins_current_resistor =                                                \
634 			DT_INST_PROP_OR(inst, pins_v1_v2_current_resistor, 1),                     \
635 		.pins_v1_v2.voltage_divider_resistors.v1_r1_r2 =                                   \
636 			DT_INST_PROP_OR(inst, pin_v1_voltage_divider_resistors, NULL),             \
637 		.pins_v1_v2.voltage_divider_resistors.v2_r1_r2 =                                   \
638 			DT_INST_PROP_OR(inst, pin_v2_voltage_divider_resistors, NULL),             \
639 		.pins_v3_v4.pins_current_resistor =                                                \
640 			DT_INST_PROP_OR(inst, pins_v3_v4_current_resistor, 1),                     \
641 		.pins_v3_v4.voltage_divider_resistors.v3_r1_r2 =                                   \
642 			DT_INST_PROP_OR(inst, pin_v3_voltage_divider_resistors, NULL),             \
643 		.pins_v3_v4.voltage_divider_resistors.v4_r1_r2 =                                   \
644 			DT_INST_PROP_OR(inst, pin_v4_voltage_divider_resistors, NULL)};            \
645                                                                                                    \
646 	SENSOR_DEVICE_DT_INST_DEFINE(inst, adltc2990_init, NULL, &adltc2990_data_##inst,           \
647 				     &adltc2990_config_##inst, POST_KERNEL,                        \
648 				     CONFIG_SENSOR_INIT_PRIORITY, &adltc2990_driver_api);
649 
650 DT_INST_FOREACH_STATUS_OKAY(ADLTC2990_DEFINE)
651