diff mbox series

[v3,1/2] hwmon: max31790: support to config PWM as TACH

Message ID 20231003031520.3091047-2-Delphine_CC_Chiu@wiwynn.com (mailing list archive)
State Superseded
Headers show
Series hwmon: max31790: support to config PWM as TACH | expand

Commit Message

Delphine CC Chiu Oct. 3, 2023, 3:15 a.m. UTC
The PWM outputs of max31790 could be used as tachometer inputs by
setting the fan configuration register, but the driver doesn't support
to config the PWM outputs as tachometer inputs currently.

Add a function to get properties of the setting of max31790 to config
PWM outputs as tachometer inputs before initializing max31790.

Signed-off-by: Delphine CC Chiu <Delphine_CC_Chiu@wiwynn.com>
---
Changelog:
v3 - Use 'sensor-type' in 'channel' node to config pwm as tach.
v2 - Remove unnecessary parentheses.
   - Add more error handling.
   - Change the type of "pwm-as-tach" from u8 to u32 to match binding
     document.
v1 - Support to config PWM as TACH
---
 drivers/hwmon/max31790.c | 83 ++++++++++++++++++++++++++++++++++++++++
 1 file changed, 83 insertions(+)
diff mbox series

Patch

diff --git a/drivers/hwmon/max31790.c b/drivers/hwmon/max31790.c
index 0cd44c1e998a..378ff32c7c1e 100644
--- a/drivers/hwmon/max31790.c
+++ b/drivers/hwmon/max31790.c
@@ -480,6 +480,85 @@  static const struct hwmon_chip_info max31790_chip_info = {
 	.info = max31790_info,
 };
 
+static int max31790_config_pwm_as_tach(struct i2c_client *client,
+				       struct device *dev,
+				       struct device_node *node)
+{
+	int ret, channel;
+	u32 reg;
+	u8 fan_config;
+	const char *type_str;
+
+	if (!node->name || of_node_cmp(node->name, "channel"))
+		return 0;
+
+	if (of_property_read_u32(node, "reg", &reg)) {
+		dev_err(dev, "Could not read reg value for '%s'\n",
+			node->full_name);
+		return -EINVAL;
+	}
+
+	if (reg > NR_CHANNEL || reg == 0) {
+		dev_err(dev, "Invalid reg (%u) in '%s'\n", reg,
+			node->full_name);
+		return -EINVAL;
+	}
+
+	channel = (int)reg - 1;
+
+	if (of_property_read_string(node, "sensor-type", &type_str)) {
+		dev_info(
+			dev,
+			"No sensor-type for '%s', use default setting as PWM output.\n",
+			node->full_name);
+		return 0;
+	}
+
+	if (!strcmp(type_str, "TACH")) {
+		ret = i2c_smbus_read_byte_data(
+			client, MAX31790_REG_FAN_CONFIG(channel));
+		if (ret < 0) {
+			dev_err(dev, "Read fan config for channel %d failed.\n",
+				channel);
+			return ret;
+		}
+		fan_config = ret;
+		fan_config |= (MAX31790_FAN_CFG_CTRL_MON |
+			       MAX31790_FAN_CFG_TACH_INPUT);
+
+		ret = i2c_smbus_write_byte_data(
+			client, MAX31790_REG_FAN_CONFIG(channel), fan_config);
+		if (ret < 0) {
+			dev_err(dev,
+				"Write fan config for channel %d failed.\n",
+				channel);
+			return ret;
+		}
+	}
+
+	return 0;
+}
+
+static int max31790_configure_channels(struct i2c_client *client,
+				       struct device *dev)
+{
+	struct device_node *node;
+	int err;
+
+	if (dev->of_node) {
+		for_each_child_of_node(dev->of_node, node) {
+			err = max31790_config_pwm_as_tach(client, dev, node);
+			if (err) {
+				dev_err(dev, "Configure for %s failed.\n",
+					node->full_name);
+				return err;
+			}
+		}
+	}
+
+	return 0;
+}
+
 static int max31790_init_client(struct i2c_client *client,
 				struct max31790_data *data)
 {
@@ -521,6 +600,10 @@  static int max31790_probe(struct i2c_client *client)
 	data->client = client;
 	mutex_init(&data->update_lock);
 
+	err = max31790_configure_channels(client, dev);
+	if (err)
+		dev_crit(dev, "Config PWM as TACH failed.\n");
+
 	/*
 	 * Initialize the max31790 chip
 	 */