1 /*
2  * Copyright (c) 2024 SILA Embedded Solutions GmbH
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 #define DT_DRV_COMPAT maxim_max31790_fan_fault
8 
9 #include <zephyr/drivers/mfd/max31790.h>
10 #include <zephyr/drivers/sensor.h>
11 #include <zephyr/drivers/sensor/max31790.h>
12 #include <zephyr/logging/log.h>
13 
14 #include "max31790_fan_fault.h"
15 
16 LOG_MODULE_REGISTER(MAX31790_FAN_FAULT, CONFIG_SENSOR_LOG_LEVEL);
17 
max31790_fan_fault_sample_fetch(const struct device * dev,enum sensor_channel chan)18 static int max31790_fan_fault_sample_fetch(const struct device *dev, enum sensor_channel chan)
19 {
20 	const struct max31790_fan_fault_config *config = dev->config;
21 	struct max31790_fan_fault_data *data = dev->data;
22 	int result;
23 	uint8_t value;
24 
25 	__ASSERT_NO_MSG(chan == SENSOR_CHAN_ALL);
26 
27 	result = i2c_reg_read_byte_dt(&config->i2c, MAX37190_REGISTER_FANFAULTSTATUS1, &value);
28 	if (result != 0) {
29 		return result;
30 	}
31 
32 	data->value = value & 0x3F;
33 
34 	return 0;
35 }
36 
max31790_fan_fault_channel_get(const struct device * dev,enum sensor_channel chan,struct sensor_value * val)37 static int max31790_fan_fault_channel_get(const struct device *dev, enum sensor_channel chan,
38 					  struct sensor_value *val)
39 {
40 	struct max31790_fan_fault_data *data = dev->data;
41 
42 	if ((enum sensor_channel_max31790)chan != SENSOR_CHAN_MAX31790_FAN_FAULT) {
43 		LOG_ERR("%s: requesting unsupported channel %i", dev->name, chan);
44 		return -ENOTSUP;
45 	}
46 
47 	val->val1 = data->value;
48 	val->val2 = 0;
49 	return 0;
50 }
51 
52 static DEVICE_API(sensor, max31790_fan_fault_api) = {
53 	.sample_fetch = max31790_fan_fault_sample_fetch,
54 	.channel_get = max31790_fan_fault_channel_get,
55 };
56 
max31790_fan_fault_init(const struct device * dev)57 static int max31790_fan_fault_init(const struct device *dev)
58 {
59 	const struct max31790_fan_fault_config *config = dev->config;
60 
61 	if (!i2c_is_ready_dt(&config->i2c)) {
62 		LOG_ERR("I2C device not ready");
63 		return -ENODEV;
64 	}
65 
66 	return 0;
67 }
68 
69 #define MAX31790_FAN_FAULT_INIT(inst)                                                              \
70 	static const struct max31790_fan_fault_config max31790_fan_fault_##inst##_config = {       \
71 		.i2c = I2C_DT_SPEC_GET(DT_INST_PARENT(inst)),                                      \
72 	};                                                                                         \
73                                                                                                    \
74 	static struct max31790_fan_fault_data max31790_fan_fault_##inst##_data;                    \
75                                                                                                    \
76 	SENSOR_DEVICE_DT_INST_DEFINE(inst, max31790_fan_fault_init, NULL,                          \
77 			      &max31790_fan_fault_##inst##_data,                                   \
78 			      &max31790_fan_fault_##inst##_config, POST_KERNEL,                    \
79 			      CONFIG_SENSOR_INIT_PRIORITY, &max31790_fan_fault_api);
80 
81 DT_INST_FOREACH_STATUS_OKAY(MAX31790_FAN_FAULT_INIT);
82