All of lore.kernel.org
 help / color / mirror / Atom feed
From: Jean Delvare <khali@linux-fr.org>
To: lm-sensors@vger.kernel.org
Subject: [lm-sensors] [PATCH 09/10] hwmon: (lm85) Select the closest PWM
Date: Sat, 12 Apr 2008 18:05:20 +0000	[thread overview]
Message-ID: <20080412200520.2485fd30@hyperion.delvare> (raw)

The LM85 and compatible chips only support 8 arbitrary PWM frequencies.
The algorithm to pick one of them based on the user input is not
optimum. Improve it to always pick the closest supported frequency.

Signed-off-by: Jean Delvare <khali@linux-fr.org>
---
The new algorithm is exactly the same as the one used in
RANGE_TO_REG().

 drivers/hwmon/lm85.c |   16 ++++++++++++----
 1 file changed, 12 insertions(+), 4 deletions(-)

--- linux-2.6.25-rc8.orig/drivers/hwmon/lm85.c	2008-04-03 18:16:31.000000000 +0200
+++ linux-2.6.25-rc8/drivers/hwmon/lm85.c	2008-04-03 18:45:54.000000000 +0200
@@ -202,10 +202,18 @@ static int FREQ_TO_REG(int freq)
 
 	if (freq >= lm85_freq_map[7])
 		return 7;
-	for (i = 0; i < 7; ++i)
-		if (freq <= lm85_freq_map[i])
-			break;
-	return i;
+
+	/* Find the closest match */
+	for (i = 6; i >= 0; --i) {
+		if (freq >= lm85_freq_map[i]) {
+			if ((lm85_freq_map[i + 1] - freq) <
+					(freq - lm85_freq_map[i]))
+				return i + 1;
+			return i;
+		}
+	}
+
+	return 0;
 }
 #define FREQ_FROM_REG(val)	lm85_freq_map[(val) & 0x07]
 

-- 
Jean Delvare

_______________________________________________
lm-sensors mailing list
lm-sensors@lm-sensors.org
http://lists.lm-sensors.org/mailman/listinfo/lm-sensors

                 reply	other threads:[~2008-04-12 18:05 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

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=20080412200520.2485fd30@hyperion.delvare \
    --to=khali@linux-fr.org \
    --cc=lm-sensors@vger.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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.