devicetree.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Chris Packham <chris.packham@alliedtelesis.co.nz>
To: jdelvare@suse.com, linux@roeck-us.net, robh@kernel.org,
	krzk+dt@kernel.org, conor+dt@kernel.org, ukleinek@kernel.org
Cc: linux-hwmon@vger.kernel.org, devicetree@vger.kernel.org,
	linux-kernel@vger.kernel.org, linux-pwm@vger.kernel.org,
	Chris Packham <chris.packham@alliedtelesis.co.nz>
Subject: [PATCH v3 3/3] hwmon: (adt7475) Add support for configuring initial PWM state
Date: Mon, 20 May 2024 15:03:21 +1200	[thread overview]
Message-ID: <20240520030321.3756604-4-chris.packham@alliedtelesis.co.nz> (raw)
In-Reply-To: <20240520030321.3756604-1-chris.packham@alliedtelesis.co.nz>

By default the PWM duty cycle in hardware is 100%. On some systems this
can cause unwanted fan noise. Add the ability to specify the fan
connections and initial state of the PWMs via device properties.

Signed-off-by: Chris Packham <chris.packham@alliedtelesis.co.nz>
---

Notes:
    Changes in v3:
    - Use the pwm provider/consumer bindings
    Changes in v2:
    - Use correct device property string for frequency
    - Allow -EINVAL and only warn on error
    - Use a frequency of 0 to indicate that the hardware should be left as-is

 drivers/hwmon/adt7475.c | 63 +++++++++++++++++++++++++++++++++++++++++
 1 file changed, 63 insertions(+)

diff --git a/drivers/hwmon/adt7475.c b/drivers/hwmon/adt7475.c
index 4224ffb30483..2b8f39c1d76f 100644
--- a/drivers/hwmon/adt7475.c
+++ b/drivers/hwmon/adt7475.c
@@ -21,6 +21,8 @@
 #include <linux/of.h>
 #include <linux/util_macros.h>
 
+#include <dt-bindings/pwm/pwm.h>
+
 /* Indexes for the sysfs hooks */
 
 #define INPUT		0
@@ -1662,6 +1664,63 @@ static int adt7475_set_pwm_polarity(struct i2c_client *client)
 	return 0;
 }
 
+static int adt7475_fan_pwm_config(struct i2c_client *client)
+{
+	struct adt7475_data *data = i2c_get_clientdata(client);
+	struct fwnode_handle *child;
+	struct of_phandle_args args = {};
+	int ret, pwm, duty, freq, flags;
+	u8 val;
+
+	device_for_each_child_node(&client->dev, child) {
+		if (!is_of_node(child))
+			continue;
+
+		ret = of_parse_phandle_with_args(to_of_node(child), "pwms", "#pwm-cells", 0, &args);
+		if (ret)
+			return ret;
+
+		if (args.args_count != 4)
+			return -EINVAL;
+
+		pwm = args.args[0];
+		freq = find_closest(args.args[1], pwmfreq_table, ARRAY_SIZE(pwmfreq_table));
+		flags = args.args[2];
+		duty = clamp_val(args.args[3], 0, 0xFF);
+
+		if (pwm >= ADT7475_PWM_COUNT)
+			return -EINVAL;
+
+		ret = adt7475_read(PWM_CONFIG_REG(pwm));
+		if (ret < 0)
+			return ret;
+		val = ret;
+		if (flags & PWM_POLARITY_INVERTED)
+			val |= BIT(4);
+		else
+			val &= ~BIT(4);
+
+		ret = i2c_smbus_write_byte_data(client, PWM_CONFIG_REG(pwm), val);
+		if (ret)
+			return ret;
+
+		data->pwm[pwm][0] = duty;
+		ret = i2c_smbus_write_byte_data(client, PWM_REG(pwm), data->pwm[pwm][0]);
+		if (ret)
+			return ret;
+
+		data->range[pwm] = adt7475_read(TEMP_TRANGE_REG(pwm));
+		data->range[pwm] &= ~0xf;
+		data->range[pwm] |= freq;
+
+		ret = i2c_smbus_write_byte_data(client, TEMP_TRANGE_REG(pwm), data->range[pwm]);
+		if (ret)
+			return ret;
+	}
+
+	return 0;
+}
+
 static int adt7475_probe(struct i2c_client *client)
 {
 	enum chips chip;
@@ -1778,6 +1837,10 @@ static int adt7475_probe(struct i2c_client *client)
 	if (ret && ret != -EINVAL)
 		dev_warn(&client->dev, "Error configuring pwm polarity\n");
 
+	ret = adt7475_fan_pwm_config(client);
+	if (ret)
+		dev_warn(&client->dev, "Error %d configuring fan/pwm\n", ret);
+
 	/* Start monitoring */
 	switch (chip) {
 	case adt7475:
-- 
2.45.1


      parent reply	other threads:[~2024-05-20  3:03 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-05-20  3:03 [PATCH v3 0/3] hwmon: (adt7475) duty cycle configuration Chris Packham
2024-05-20  3:03 ` [PATCH v3 1/3] dt-bindings: hwmon: Add adt7475 fan/pwm properties Chris Packham
2024-05-20 16:49   ` Conor Dooley
2024-05-20 19:03     ` Guenter Roeck
2024-05-20 19:41       ` Conor Dooley
2024-05-20 20:55         ` Chris Packham
2024-05-20  3:03 ` [PATCH v3 2/3] dt-bindings: hwmon: adt7475: Deprecate adi,pwm-active-state Chris Packham
2024-05-20  3:03 ` Chris Packham [this message]

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=20240520030321.3756604-4-chris.packham@alliedtelesis.co.nz \
    --to=chris.packham@alliedtelesis.co.nz \
    --cc=conor+dt@kernel.org \
    --cc=devicetree@vger.kernel.org \
    --cc=jdelvare@suse.com \
    --cc=krzk+dt@kernel.org \
    --cc=linux-hwmon@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-pwm@vger.kernel.org \
    --cc=linux@roeck-us.net \
    --cc=robh@kernel.org \
    --cc=ukleinek@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;
as well as URLs for NNTP newsgroup(s).