mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Delphine CC Chiu <Delphine_CC_Chiu@wiwynn.com>
To: patrick@stwcx.xyz, Delphine CC Chiu <Delphine_CC_Chiu@wiwynn.com>,
	Jean Delvare <jdelvare@suse.com>,
	Guenter Roeck <linux@roeck-us.net>
Cc: Rob Herring <robh+dt@kernel.org>,
	Krzysztof Kozlowski <krzysztof.kozlowski+dt@linaro.org>,
	Conor Dooley <conor+dt@kernel.org>,
	Jonathan Corbet <corbet@lwn.net>,
	linux-hwmon@vger.kernel.org, devicetree@vger.kernel.org,
	linux-kernel@vger.kernel.org, linux-doc@vger.kernel.org
Subject: [PATCH v4 1/3] hwmon: max31790: support to config PWM as TACH
Date: Thu,  2 Nov 2023 11:28:30 +0800	[thread overview]
Message-ID: <20231102032834.3827289-2-Delphine_CC_Chiu@wiwynn.com> (raw)
In-Reply-To: <20231102032834.3827289-1-Delphine_CC_Chiu@wiwynn.com>

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:
v4 - No change.
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 --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
 	 */
-- 
2.25.1


  reply	other threads:[~2023-11-02  3:29 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-11-02  3:28 [PATCH v4 0/3] " Delphine CC Chiu
2023-11-02  3:28 ` Delphine CC Chiu [this message]
2023-11-02  3:28 ` [PATCH v4 2/3] dt-bindings: hwmon: add MAX31790 Delphine CC Chiu
2023-11-02  3:28 ` [PATCH v4 3/3] hwmon: max31790: add fanN_enable for all fans Delphine CC Chiu

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20231102032834.3827289-2-Delphine_CC_Chiu@wiwynn.com \
    --to=delphine_cc_chiu@wiwynn.com \
    --cc=conor+dt@kernel.org \
    --cc=corbet@lwn.net \
    --cc=devicetree@vger.kernel.org \
    --cc=jdelvare@suse.com \
    --cc=krzysztof.kozlowski+dt@linaro.org \
    --cc=linux-doc@vger.kernel.org \
    --cc=linux-hwmon@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux@roeck-us.net \
    --cc=patrick@stwcx.xyz \
    --cc=robh+dt@kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®