LKML Archive on lore.kernel.org
help / color / mirror / Atom feed
From: Bartosz Golaszewski <bgolaszewski@baylibre.com>
To: LKML <linux-kernel@vger.kernel.org>
Cc: Guenter Roeck <linux@roeck-us.net>,
	lm-sensors <lm-sensors@lm-sensors.org>,
	Andrew Morton <akpm@linux-foundation.org>,
	Steven Rostedt <srostedt@redhat.com>,
	Bartosz Golaszewski <bgolaszewski@baylibre.com>
Subject: [PATCH v2 3/4] hwmon: (lm85) replace x_TO_REG() functions with find_closest()
Date: Tue, 10 Mar 2015 18:27:21 +0100	[thread overview]
Message-ID: <1426008442-26236-4-git-send-email-bgolaszewski@baylibre.com> (raw)
In-Reply-To: <1426008442-26236-1-git-send-email-bgolaszewski@baylibre.com>

Replace RANGE_TO_REG() and FREQ_TO_REG() functions with calls
to find_closest().

Signed-off-by: Bartosz Golaszewski <bgolaszewski@baylibre.com>
---
 drivers/hwmon/lm85.c | 43 ++++++++++++-------------------------------
 1 file changed, 12 insertions(+), 31 deletions(-)

diff --git a/drivers/hwmon/lm85.c b/drivers/hwmon/lm85.c
index 2b4b419..a0c0050 100644
--- a/drivers/hwmon/lm85.c
+++ b/drivers/hwmon/lm85.c
@@ -188,18 +188,6 @@ static const int lm85_range_map[] = {
 	13300, 16000, 20000, 26600, 32000, 40000, 53300, 80000
 };
 
-static int RANGE_TO_REG(long range)
-{
-	int i;
-
-	/* Find the closest match */
-	for (i = 0; i < 15; ++i) {
-		if (range <= (lm85_range_map[i] + lm85_range_map[i + 1]) / 2)
-			break;
-	}
-
-	return i;
-}
 #define RANGE_FROM_REG(val)	lm85_range_map[(val) & 0x0f]
 
 /* These are the PWM frequency encodings */
@@ -209,17 +197,7 @@ static const int lm85_freq_map[8] = { /* 1 Hz */
 static const int adm1027_freq_map[8] = { /* 1 Hz */
 	11, 15, 22, 29, 35, 44, 59, 88
 };
-
-static int FREQ_TO_REG(const int *map, unsigned long freq)
-{
-	int i;
-
-	/* Find the closest match */
-	for (i = 0; i < 7; ++i)
-		if (freq <= (map[i] + map[i + 1]) / 2)
-			break;
-	return i;
-}
+#define FREQ_MAP_SIZE		8
 
 static int FREQ_FROM_REG(const int *map, u8 reg)
 {
@@ -828,7 +806,8 @@ static ssize_t set_pwm_freq(struct device *dev,
 		data->cfg5 &= ~ADT7468_HFPWM;
 		lm85_write_value(client, ADT7468_REG_CFG5, data->cfg5);
 	} else {					/* Low freq. mode */
-		data->pwm_freq[nr] = FREQ_TO_REG(data->freq_map, val);
+		data->pwm_freq[nr] = find_closest(val, data->freq_map,
+						  FREQ_MAP_SIZE);
 		lm85_write_value(client, LM85_REG_AFAN_RANGE(nr),
 				 (data->zone[nr].range << 4)
 				 | data->pwm_freq[nr]);
@@ -1186,7 +1165,7 @@ static ssize_t set_temp_auto_temp_min(struct device *dev,
 	int nr = to_sensor_dev_attr(attr)->index;
 	struct lm85_data *data = dev_get_drvdata(dev);
 	struct i2c_client *client = data->client;
-	long val;
+	long val, range;
 	int err;
 
 	err = kstrtol(buf, 10, &val);
@@ -1198,10 +1177,12 @@ static ssize_t set_temp_auto_temp_min(struct device *dev,
 	lm85_write_value(client, LM85_REG_AFAN_LIMIT(nr),
 		data->zone[nr].limit);
 
-/* Update temp_auto_max and temp_auto_range */
-	data->zone[nr].range = RANGE_TO_REG(
-		TEMP_FROM_REG(data->zone[nr].max_desired) -
-		TEMP_FROM_REG(data->zone[nr].limit));
+	/* Update temp_auto_max and temp_auto_range */
+	range = TEMP_FROM_REG(data->zone[nr].max_desired) -
+		TEMP_FROM_REG(data->zone[nr].limit);
+	data->zone[nr].range = find_closest(range, lm85_range_map,
+					    ARRAY_SIZE(lm85_range_map));
+
 	lm85_write_value(client, LM85_REG_AFAN_RANGE(nr),
 		((data->zone[nr].range & 0x0f) << 4)
 		| (data->pwm_freq[nr] & 0x07));
@@ -1236,8 +1217,8 @@ static ssize_t set_temp_auto_temp_max(struct device *dev,
 	mutex_lock(&data->update_lock);
 	min = TEMP_FROM_REG(data->zone[nr].limit);
 	data->zone[nr].max_desired = TEMP_TO_REG(val);
-	data->zone[nr].range = RANGE_TO_REG(
-		val - min);
+	data->zone[nr].range = find_closest(val - min, lm85_range_map,
+					    ARRAY_SIZE(lm85_range_map));
 	lm85_write_value(client, LM85_REG_AFAN_RANGE(nr),
 		((data->zone[nr].range & 0x0f) << 4)
 		| (data->pwm_freq[nr] & 0x07));
-- 
2.1.4


  parent reply	other threads:[~2015-03-10 17:28 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2015-03-10 17:27 [PATCH v2 0/4] kernel.h: find_closest() macro Bartosz Golaszewski
2015-03-10 17:27 ` [PATCH v2 1/4] kernel.h: add " Bartosz Golaszewski
2015-03-10 18:27   ` Joe Perches
2015-03-12 23:07   ` Andrew Morton
2015-03-10 17:27 ` [PATCH v2 2/4] hwmon: (ina2xx) replace ina226_avg_bits() with find_closest() Bartosz Golaszewski
2015-03-10 17:27 ` Bartosz Golaszewski [this message]
2015-03-13  8:53   ` [PATCH v2 3/4] hwmon: (lm85) replace x_TO_REG() functions " Rasmus Villemoes
2015-03-10 17:27 ` [PATCH v2 4/4] hwmon: (w83795) use find_closest_desc() in pwm_freq_to_reg() Bartosz Golaszewski

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=1426008442-26236-4-git-send-email-bgolaszewski@baylibre.com \
    --to=bgolaszewski@baylibre.com \
    --cc=akpm@linux-foundation.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux@roeck-us.net \
    --cc=lm-sensors@lm-sensors.org \
    --cc=srostedt@redhat.com \
    --subject='Re: [PATCH v2 3/4] hwmon: (lm85) replace x_TO_REG() functions with find_closest()' \
    /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

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).