linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: "Václav Kubernát" <kubernat@cesnet.cz>
To: unlisted-recipients:; (no To-header on input)
Cc: "Václav Kubernát" <kubernat@cesnet.cz>,
	"Jean Delvare" <jdelvare@suse.com>,
	"Guenter Roeck" <linux@roeck-us.net>,
	"Jonathan Corbet" <corbet@lwn.net>,
	linux-hwmon@vger.kernel.org, linux-doc@vger.kernel.org,
	linux-kernel@vger.kernel.org
Subject: [PATCH 3/7] hwmon: (max31790) Allow setting pulses
Date: Thu,  4 Mar 2021 11:58:26 +0100	[thread overview]
Message-ID: <20210304105830.507176-3-kubernat@cesnet.cz> (raw)
In-Reply-To: <20210304105830.507176-1-kubernat@cesnet.cz>

In the old code, the value calculated RPM_FROM_REG is misleading. The
left-hand side of the division is correct (as per the datasheet, page
11). The misleading part is the right-hand side: the datasheet says it
is should be "number of pulses * TACH count". The TACH count is the
value of the register which is a 11-bit left-justified value. This means
that the register value should be shifted by 5 if we want the actual
value. However, in the old code, the value is shifted by 4 with no way
to set the pulses per revolution. This essentially means, that the
default pulses per revolution is 2, because shifting the right-hand side
one less bit means that the final value is doubled by 2. In the end,
what happens is, that the old code works as if fan*_pulses had the
default value of 2 all the time. This is somewhat correct, but in my
opinion the intention isn't entirely clear, at first glance, shifting by
4 instead of 5 seems like a bug (after one checks the datasheet).

Pulses per revolution should be a configurable because otherwise there's
no way to correctly calculate the RPM of the fan. This patch adds the
option to set pulses per revolution.

The hwmon documentation for fan*_pulses says that it shouldn't be
present unless the chip has a register to save this value. This seems
non-sensical, because setting the pulses is essential to properly
calculate RPM. The value is saved inside the driver's data structure.

Signed-off-by: Václav Kubernát <kubernat@cesnet.cz>
---
 Documentation/hwmon/max31790.rst |  1 +
 drivers/hwmon/max31790.c         | 57 +++++++++++++++++++++-----------
 2 files changed, 39 insertions(+), 19 deletions(-)

diff --git a/Documentation/hwmon/max31790.rst b/Documentation/hwmon/max31790.rst
index 8979c8a02cd1..8d86698b25de 100644
--- a/Documentation/hwmon/max31790.rst
+++ b/Documentation/hwmon/max31790.rst
@@ -36,6 +36,7 @@ Sysfs entries
 
 ================== === =============================================================
 fan[1-12]_enable   RW  enable fan speed monitoring
+fan[1-12]_pulses   RW  pulses per fan revolution (default: 2)
 fan[1-12]_input    RO  fan tachometer speed in RPM
 fan[1-12]_fault    RO  fan experienced fault
 fan[1-6]_target    RW  desired fan speed in RPM
diff --git a/drivers/hwmon/max31790.c b/drivers/hwmon/max31790.c
index eca5ec615734..74a81e5e3383 100644
--- a/drivers/hwmon/max31790.c
+++ b/drivers/hwmon/max31790.c
@@ -41,10 +41,12 @@
 #define FAN_RPM_MAX			7864320
 #define MAX_PWM				0XFF80
 
-#define RPM_FROM_REG(reg, sr)		(((reg) >> 4) ? \
-					 ((60 * (sr) * 8192) / ((reg) >> 4)) : \
-					 FAN_RPM_MAX)
-#define RPM_TO_REG(rpm, sr)		((60 * (sr) * 8192) / ((rpm) * 2))
+#define RPM_FROM_REG(reg, sr, pulses) \
+	(((reg) >> 5) ? \
+	 ((60 * (sr) * 8192) / ((reg) >> 5) / (pulses)) : \
+	 FAN_RPM_MAX)
+#define RPM_TO_REG(rpm, sr, pulses) \
+	((60 * (sr) * 8192) / ((rpm) * (pulses)))
 
 #define NR_CHANNEL			6
 
@@ -81,6 +83,7 @@ struct max31790_data {
 	bool valid; /* zero until following fields are valid */
 	unsigned long last_updated; /* in jiffies */
 	bool full_speed[NR_CHANNEL];
+	u8 pulses[NR_CHANNEL];
 
 	/* register values */
 	u8 fan_config[NR_CHANNEL];
@@ -217,12 +220,16 @@ static int max31790_read_fan(struct device *dev, u32 attr, int channel,
 	switch (attr) {
 	case hwmon_fan_input:
 		sr = get_tach_period(data->fan_dynamics[channel]);
-		rpm = RPM_FROM_REG(data->tach[channel], sr);
+		rpm = RPM_FROM_REG(data->tach[channel],
+				   sr,
+				   data->pulses[channel]);
 		*val = rpm;
 		return 0;
 	case hwmon_fan_target:
 		sr = get_tach_period(data->fan_dynamics[channel]);
-		rpm = RPM_FROM_REG(data->target_count[channel], sr);
+		rpm = RPM_FROM_REG(data->target_count[channel],
+				   sr,
+				   data->pulses[channel]);
 		*val = rpm;
 		return 0;
 	case hwmon_fan_fault:
@@ -231,6 +238,9 @@ static int max31790_read_fan(struct device *dev, u32 attr, int channel,
 	case hwmon_fan_enable:
 		*val = !!(data->fan_config[channel] & MAX31790_FAN_CFG_TACH_INPUT_EN);
 		return 0;
+	case hwmon_fan_pulses:
+		*val = data->pulses[channel];
+		return 0;
 	default:
 		return -EOPNOTSUPP;
 	}
@@ -264,7 +274,7 @@ static int max31790_write_fan(struct device *dev, u32 attr, int channel,
 			break;
 
 		sr = get_tach_period(data->fan_dynamics[channel]);
-		target_count = RPM_TO_REG(val, sr);
+		target_count = RPM_TO_REG(val, sr, data->pulses[channel]);
 		target_count = clamp_val(target_count, 0x1, 0x7FF);
 
 		data->target_count[channel] = target_count << 5;
@@ -285,6 +295,9 @@ static int max31790_write_fan(struct device *dev, u32 attr, int channel,
 				   MAX31790_REG_FAN_CONFIG(channel),
 				   data->fan_config[channel]);
 		break;
+	case hwmon_fan_pulses:
+		data->pulses[channel] = val;
+		break;
 	default:
 		err = -EOPNOTSUPP;
 		break;
@@ -317,6 +330,10 @@ static umode_t max31790_fan_is_visible(const void *_data, u32 attr, int channel)
 		    (fan_config & MAX31790_FAN_CFG_TACH_INPUT))
 			return 0644;
 		return 0;
+	case hwmon_fan_pulses:
+		if (channel < NR_CHANNEL)
+			return 0644;
+		return 0;
 	default:
 		return 0;
 	}
@@ -486,18 +503,18 @@ static umode_t max31790_is_visible(const void *data,
 
 static const struct hwmon_channel_info *max31790_info[] = {
 	HWMON_CHANNEL_INFO(fan,
-		HWMON_F_ENABLE | HWMON_F_INPUT | HWMON_F_TARGET | HWMON_F_FAULT,
-		HWMON_F_ENABLE | HWMON_F_INPUT | HWMON_F_TARGET | HWMON_F_FAULT,
-		HWMON_F_ENABLE | HWMON_F_INPUT | HWMON_F_TARGET | HWMON_F_FAULT,
-		HWMON_F_ENABLE | HWMON_F_INPUT | HWMON_F_TARGET | HWMON_F_FAULT,
-		HWMON_F_ENABLE | HWMON_F_INPUT | HWMON_F_TARGET | HWMON_F_FAULT,
-		HWMON_F_ENABLE | HWMON_F_INPUT | HWMON_F_TARGET | HWMON_F_FAULT,
-		HWMON_F_ENABLE | HWMON_F_INPUT | HWMON_F_FAULT,
-		HWMON_F_ENABLE | HWMON_F_INPUT | HWMON_F_FAULT,
-		HWMON_F_ENABLE | HWMON_F_INPUT | HWMON_F_FAULT,
-		HWMON_F_ENABLE | HWMON_F_INPUT | HWMON_F_FAULT,
-		HWMON_F_ENABLE | HWMON_F_INPUT | HWMON_F_FAULT,
-		HWMON_F_ENABLE | HWMON_F_INPUT | HWMON_F_FAULT),
+		HWMON_F_PULSES | HWMON_F_ENABLE | HWMON_F_INPUT | HWMON_F_TARGET | HWMON_F_FAULT,
+		HWMON_F_PULSES | HWMON_F_ENABLE | HWMON_F_INPUT | HWMON_F_TARGET | HWMON_F_FAULT,
+		HWMON_F_PULSES | HWMON_F_ENABLE | HWMON_F_INPUT | HWMON_F_TARGET | HWMON_F_FAULT,
+		HWMON_F_PULSES | HWMON_F_ENABLE | HWMON_F_INPUT | HWMON_F_TARGET | HWMON_F_FAULT,
+		HWMON_F_PULSES | HWMON_F_ENABLE | HWMON_F_INPUT | HWMON_F_TARGET | HWMON_F_FAULT,
+		HWMON_F_PULSES | HWMON_F_ENABLE | HWMON_F_INPUT | HWMON_F_TARGET | HWMON_F_FAULT,
+		HWMON_F_PULSES | HWMON_F_ENABLE | HWMON_F_INPUT | HWMON_F_FAULT,
+		HWMON_F_PULSES | HWMON_F_ENABLE | HWMON_F_INPUT | HWMON_F_FAULT,
+		HWMON_F_PULSES | HWMON_F_ENABLE | HWMON_F_INPUT | HWMON_F_FAULT,
+		HWMON_F_PULSES | HWMON_F_ENABLE | HWMON_F_INPUT | HWMON_F_FAULT,
+		HWMON_F_PULSES | HWMON_F_ENABLE | HWMON_F_INPUT | HWMON_F_FAULT,
+		HWMON_F_PULSES | HWMON_F_ENABLE | HWMON_F_INPUT | HWMON_F_FAULT),
 	HWMON_CHANNEL_INFO(pwm,
 		HWMON_PWM_INPUT | HWMON_PWM_ENABLE,
 		HWMON_PWM_INPUT | HWMON_PWM_ENABLE,
@@ -534,6 +551,8 @@ static int max31790_init_client(struct regmap *regmap,
 
 		data->full_speed[i] = false;
 
+		data->pulses[i] = 2;
+
 		rv = regmap_read(regmap,
 				 MAX31790_REG_FAN_DYNAMICS(i),
 				 &val);
-- 
2.30.1


  parent reply	other threads:[~2021-03-04 11:01 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-03-04 10:58 [PATCH 1/7] hwmon: (max31790) Rework to use regmap Václav Kubernát
2021-03-04 10:58 ` [PATCH 2/7] hwmon: (max31790) Fix and split pwm*_enable Václav Kubernát
2021-03-04 10:58 ` Václav Kubernát [this message]
2021-03-05 12:08   ` [PATCH 3/7] hwmon: (max31790) Allow setting pulses Jan Kundrát
2021-03-08  9:31     ` Václav Kubernát
2021-03-04 10:58 ` [PATCH 4/7] hwmon: (max31790) Show 0 RPM/fault when input disabled Václav Kubernát
2021-03-04 10:58 ` [PATCH 5/7] hwmon: (max31790) Refactor HWMON_CHANNEL_INFO Václav Kubernát
2021-03-04 10:58 ` [PATCH 6/7] hwmon: (max31790) Allow setting fan*_div Václav Kubernát
2021-03-04 10:58 ` [PATCH 7/7] hwmon: (max31790) Update documentation Václav Kubernát

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=20210304105830.507176-3-kubernat@cesnet.cz \
    --to=kubernat@cesnet.cz \
    --cc=corbet@lwn.net \
    --cc=jdelvare@suse.com \
    --cc=linux-doc@vger.kernel.org \
    --cc=linux-hwmon@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux@roeck-us.net \
    /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).