Linux-Hwmon 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
Cc: linux-hwmon@vger.kernel.org, devicetree@vger.kernel.org,
	linux-kernel@vger.kernel.org,
	Chris Packham <chris.packham@alliedtelesis.co.nz>
Subject: [PATCH v2 2/2] hwmon: (adt7475) Add support for configuring initial PWM duty cycle
Date: Thu,  9 May 2024 09:55:04 +1200	[thread overview]
Message-ID: <20240508215504.300580-3-chris.packham@alliedtelesis.co.nz> (raw)
In-Reply-To: <20240508215504.300580-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 take an initial PWM
duty cycle and frequency via device properties.

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

Notes:
    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 | 58 +++++++++++++++++++++++++++++++++++++++++
 1 file changed, 58 insertions(+)

diff --git a/drivers/hwmon/adt7475.c b/drivers/hwmon/adt7475.c
index 4224ffb30483..14bd618488f8 100644
--- a/drivers/hwmon/adt7475.c
+++ b/drivers/hwmon/adt7475.c
@@ -1662,6 +1662,56 @@ static int adt7475_set_pwm_polarity(struct i2c_client *client)
 	return 0;
 }
 
+static int adt7475_set_pwm_initial_freq(struct i2c_client *client)
+{
+	int ret, out, i;
+	u32 freqs[ADT7475_PWM_COUNT];
+	int data;
+
+	ret = device_property_read_u32_array(&client->dev,
+					     "adi,pwm-initial-frequency", freqs,
+					     ARRAY_SIZE(freqs));
+	if (ret)
+		return ret;
+
+	for (i = 0; i < ADT7475_PWM_COUNT; i++) {
+		if (!freqs[i])
+			continue;
+		out = find_closest(freqs[i], pwmfreq_table, ARRAY_SIZE(pwmfreq_table));
+		data = adt7475_read(TEMP_TRANGE_REG(i));
+		if (data < 0)
+			return data;
+		data &= ~0xf;
+		data |= out;
+
+		ret = i2c_smbus_write_byte_data(client, TEMP_TRANGE_REG(i), data);
+		if (ret)
+			return ret;
+	}
+
+	return 0;
+}
+
+static int adt7475_set_pwm_initial_duty(struct i2c_client *client)
+{
+	int ret, i;
+	u32 dutys[ADT7475_PWM_COUNT];
+
+	ret = device_property_read_u32_array(&client->dev,
+					     "adi,pwm-initial-duty-cycle", dutys,
+					     ARRAY_SIZE(dutys));
+	if (ret)
+		return ret;
+
+	for (i = 0; i < ADT7475_PWM_COUNT; i++) {
+		ret = i2c_smbus_write_byte_data(client, PWM_MAX_REG(i), dutys[i] & 0xff);
+		if (ret)
+			return ret;
+	}
+
+	return 0;
+}
+
 static int adt7475_probe(struct i2c_client *client)
 {
 	enum chips chip;
@@ -1778,6 +1828,14 @@ static int adt7475_probe(struct i2c_client *client)
 	if (ret && ret != -EINVAL)
 		dev_warn(&client->dev, "Error configuring pwm polarity\n");
 
+	ret = adt7475_set_pwm_initial_freq(client);
+	if (ret && ret != -EINVAL)
+		dev_warn(&client->dev, "Error configuring pwm frequency\n");
+
+	ret = adt7475_set_pwm_initial_duty(client);
+	if (ret && ret != -EINVAL)
+		dev_warn(&client->dev, "Error configuring pwm duty cycle\n");
+
 	/* Start monitoring */
 	switch (chip) {
 	case adt7475:
-- 
2.43.2


      parent reply	other threads:[~2024-05-08 21:55 UTC|newest]

Thread overview: 14+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-05-08 21:55 [PATCH v2 0/2] hwmon: (adt7475) duty cycle configuration Chris Packham
2024-05-08 21:55 ` [PATCH v2 1/2] dt-bindings: hwmon: Document adt7475 PWM initial duty cycle Chris Packham
2024-05-09  7:06   ` Krzysztof Kozlowski
2024-05-09 13:25     ` Guenter Roeck
2024-05-09 18:19     ` Chris Packham
2024-05-10  3:36       ` Guenter Roeck
2024-05-10 15:51         ` Chris Packham
2024-05-12 16:58           ` Guenter Roeck
2024-05-17  1:09             ` Chris Packham
2024-05-17 17:00               ` Conor Dooley
2024-05-17 17:02                 ` Conor Dooley
2024-05-17 17:26                   ` Guenter Roeck
2024-05-17 17:39                   ` Conor Dooley
2024-05-08 21:55 ` 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=20240508215504.300580-3-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@roeck-us.net \
    --cc=robh@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 read-only IMAP folder(s) and NNTP newsgroup(s).