From: Guenter Roeck <linux@roeck-us.net>
To: Hardware Monitoring <linux-hwmon@vger.kernel.org>
Cc: Guenter Roeck <linux@roeck-us.net>
Subject: [PATCH 4/7] hwmon: (g762) Use bit operations
Date: Thu, 4 Jul 2024 14:37:09 -0700 [thread overview]
Message-ID: <20240704213712.2699553-5-linux@roeck-us.net> (raw)
In-Reply-To: <20240704213712.2699553-1-linux@roeck-us.net>
Use bit operations to make the code easier to read and avoid duplication.
While at it, improve column alignment in defines.
No functional change.
Signed-off-by: Guenter Roeck <linux@roeck-us.net>
---
drivers/hwmon/g762.c | 130 +++++++++++++------------------------------
1 file changed, 40 insertions(+), 90 deletions(-)
diff --git a/drivers/hwmon/g762.c b/drivers/hwmon/g762.c
index 37d8a45610a3..59077e54d47e 100644
--- a/drivers/hwmon/g762.c
+++ b/drivers/hwmon/g762.c
@@ -27,6 +27,8 @@
* http://www.gmt.com.tw/product/datasheet/EDS-762_3.pdf
*/
+#include <linux/bits.h>
+#include <linux/bitfield.h>
#include <linux/clk.h>
#include <linux/device.h>
#include <linux/err.h>
@@ -59,35 +61,32 @@ enum g762_regs {
};
/* Config register bits */
-#define G762_REG_FAN_CMD1_DET_FAN_FAIL 0x80 /* enable fan_fail signal */
-#define G762_REG_FAN_CMD1_DET_FAN_OOC 0x40 /* enable fan_out_of_control */
-#define G762_REG_FAN_CMD1_OUT_MODE 0x20 /* out mode: PWM or DC */
-#define G762_REG_FAN_CMD1_FAN_MODE 0x10 /* fan mode: closed/open-loop */
-#define G762_REG_FAN_CMD1_CLK_DIV_ID1 0x08 /* clock divisor value */
-#define G762_REG_FAN_CMD1_CLK_DIV_ID0 0x04
-#define G762_REG_FAN_CMD1_PWM_POLARITY 0x02 /* PWM polarity */
-#define G762_REG_FAN_CMD1_PULSE_PER_REV 0x01 /* pulse per fan revolution */
+#define G762_REG_FAN_CMD1_DET_FAN_FAIL BIT(7) /* enable fan_fail signal */
+#define G762_REG_FAN_CMD1_DET_FAN_OOC BIT(6) /* enable fan_out_of_control */
+#define G762_REG_FAN_CMD1_OUT_MODE BIT(5) /* out mode: PWM or DC */
+#define G762_REG_FAN_CMD1_FAN_MODE BIT(4) /* fan mode: closed/open-loop */
+#define G762_REG_FAN_CMD1_CLK_DIV_MASK GENMASK(3, 2)
+#define G762_REG_FAN_CMD1_PWM_POLARITY BIT(1) /* PWM polarity */
+#define G762_REG_FAN_CMD1_PULSE_PER_REV BIT(0) /* pulse per fan revolution */
-#define G762_REG_FAN_CMD2_GEAR_MODE_1 0x08 /* fan gear mode */
-#define G762_REG_FAN_CMD2_GEAR_MODE_0 0x04
-#define G762_REG_FAN_CMD2_FAN_STARTV_1 0x02 /* fan startup voltage */
-#define G762_REG_FAN_CMD2_FAN_STARTV_0 0x01
+#define G762_REG_FAN_CMD2_GEAR_MASK GENMASK(3, 2)
+#define G762_REG_FAN_CMD2_FAN_STARTV_MASK GENMASK(1, 0) /* fan startup voltage */
-#define G762_REG_FAN_STA_FAIL 0x02 /* fan fail */
-#define G762_REG_FAN_STA_OOC 0x01 /* fan out of control */
+#define G762_REG_FAN_STA_FAIL BIT(1) /* fan fail */
+#define G762_REG_FAN_STA_OOC BIT(0) /* fan out of control */
/* Config register values */
-#define G762_OUT_MODE_PWM 1
-#define G762_OUT_MODE_DC 0
+#define G762_OUT_MODE_PWM 1
+#define G762_OUT_MODE_DC 0
-#define G762_FAN_MODE_CLOSED_LOOP 2
-#define G762_FAN_MODE_OPEN_LOOP 1
+#define G762_FAN_MODE_CLOSED_LOOP 2
+#define G762_FAN_MODE_OPEN_LOOP 1
-#define G762_PWM_POLARITY_NEGATIVE 1
-#define G762_PWM_POLARITY_POSITIVE 0
+#define G762_PWM_POLARITY_NEGATIVE 1
+#define G762_PWM_POLARITY_POSITIVE 0
/* Register data is read (and cached) at most once per second. */
-#define G762_UPDATE_INTERVAL HZ
+#define G762_UPDATE_INTERVAL HZ
/*
* Extract pulse count per fan revolution value (2 or 4) from given
@@ -101,16 +100,14 @@ enum g762_regs {
* register value.
*/
#define G762_CLKDIV_FROM_REG(reg) \
- (1 << (((reg) & (G762_REG_FAN_CMD1_CLK_DIV_ID0 | \
- G762_REG_FAN_CMD1_CLK_DIV_ID1)) >> 2))
+ BIT(FIELD_GET(G762_REG_FAN_CMD1_CLK_DIV_MASK, reg))
/*
- * Extract fan gear mode multiplier value (0, 2 or 4) from given
+ * Extract fan gear mode multiplier value (1, 2 or 4) from given
* FAN_CMD2 register value.
*/
#define G762_GEARMULT_FROM_REG(reg) \
- (1 << (((reg) & (G762_REG_FAN_CMD2_GEAR_MODE_0 | \
- G762_REG_FAN_CMD2_GEAR_MODE_1)) >> 2))
+ BIT(FIELD_GET(G762_REG_FAN_CMD2_GEAR_MASK, reg))
struct g762_data {
struct i2c_client *client;
@@ -303,32 +300,15 @@ static int do_set_fan_div(struct device *dev, unsigned long val)
if (IS_ERR(data))
return PTR_ERR(data);
+ if (hweight_long(val) != 1 || val > 8)
+ return -EINVAL;
+
mutex_lock(&data->update_lock);
- switch (val) {
- case 1:
- data->fan_cmd1 &= ~G762_REG_FAN_CMD1_CLK_DIV_ID0;
- data->fan_cmd1 &= ~G762_REG_FAN_CMD1_CLK_DIV_ID1;
- break;
- case 2:
- data->fan_cmd1 |= G762_REG_FAN_CMD1_CLK_DIV_ID0;
- data->fan_cmd1 &= ~G762_REG_FAN_CMD1_CLK_DIV_ID1;
- break;
- case 4:
- data->fan_cmd1 &= ~G762_REG_FAN_CMD1_CLK_DIV_ID0;
- data->fan_cmd1 |= G762_REG_FAN_CMD1_CLK_DIV_ID1;
- break;
- case 8:
- data->fan_cmd1 |= G762_REG_FAN_CMD1_CLK_DIV_ID0;
- data->fan_cmd1 |= G762_REG_FAN_CMD1_CLK_DIV_ID1;
- break;
- default:
- ret = -EINVAL;
- goto out;
- }
+ data->fan_cmd1 &= ~G762_REG_FAN_CMD1_CLK_DIV_MASK;
+ data->fan_cmd1 |= FIELD_PREP(G762_REG_FAN_CMD1_CLK_DIV_MASK, __ffs(val));
ret = i2c_smbus_write_byte_data(data->client, G762_REG_FAN_CMD1,
data->fan_cmd1);
data->valid = false;
- out:
mutex_unlock(&data->update_lock);
return ret;
@@ -343,28 +323,15 @@ static int do_set_fan_gear_mode(struct device *dev, unsigned long val)
if (IS_ERR(data))
return PTR_ERR(data);
+ if (val > 2)
+ return -EINVAL;
+
mutex_lock(&data->update_lock);
- switch (val) {
- case 0:
- data->fan_cmd2 &= ~G762_REG_FAN_CMD2_GEAR_MODE_0;
- data->fan_cmd2 &= ~G762_REG_FAN_CMD2_GEAR_MODE_1;
- break;
- case 1:
- data->fan_cmd2 |= G762_REG_FAN_CMD2_GEAR_MODE_0;
- data->fan_cmd2 &= ~G762_REG_FAN_CMD2_GEAR_MODE_1;
- break;
- case 2:
- data->fan_cmd2 &= ~G762_REG_FAN_CMD2_GEAR_MODE_0;
- data->fan_cmd2 |= G762_REG_FAN_CMD2_GEAR_MODE_1;
- break;
- default:
- ret = -EINVAL;
- goto out;
- }
+ data->fan_cmd2 &= ~G762_REG_FAN_CMD2_GEAR_MASK;
+ data->fan_cmd2 |= FIELD_PREP(G762_REG_FAN_CMD2_GEAR_MASK, val);
ret = i2c_smbus_write_byte_data(data->client, G762_REG_FAN_CMD2,
data->fan_cmd2);
data->valid = false;
- out:
mutex_unlock(&data->update_lock);
return ret;
@@ -526,32 +493,15 @@ static int do_set_fan_startv(struct device *dev, unsigned long val)
if (IS_ERR(data))
return PTR_ERR(data);
+ if (val > 3)
+ return -EINVAL;
+
mutex_lock(&data->update_lock);
- switch (val) {
- case 0:
- data->fan_cmd2 &= ~G762_REG_FAN_CMD2_FAN_STARTV_0;
- data->fan_cmd2 &= ~G762_REG_FAN_CMD2_FAN_STARTV_1;
- break;
- case 1:
- data->fan_cmd2 |= G762_REG_FAN_CMD2_FAN_STARTV_0;
- data->fan_cmd2 &= ~G762_REG_FAN_CMD2_FAN_STARTV_1;
- break;
- case 2:
- data->fan_cmd2 &= ~G762_REG_FAN_CMD2_FAN_STARTV_0;
- data->fan_cmd2 |= G762_REG_FAN_CMD2_FAN_STARTV_1;
- break;
- case 3:
- data->fan_cmd2 |= G762_REG_FAN_CMD2_FAN_STARTV_0;
- data->fan_cmd2 |= G762_REG_FAN_CMD2_FAN_STARTV_1;
- break;
- default:
- ret = -EINVAL;
- goto out;
- }
+ data->fan_cmd2 &= ~G762_REG_FAN_CMD2_FAN_STARTV_MASK;
+ data->fan_cmd2 |= FIELD_PREP(G762_REG_FAN_CMD2_FAN_STARTV_MASK, val);
ret = i2c_smbus_write_byte_data(data->client, G762_REG_FAN_CMD2,
data->fan_cmd2);
data->valid = false;
- out:
mutex_unlock(&data->update_lock);
return ret;
@@ -722,7 +672,7 @@ static ssize_t fan1_div_show(struct device *dev, struct device_attribute *da,
if (IS_ERR(data))
return PTR_ERR(data);
- return sprintf(buf, "%d\n", G762_CLKDIV_FROM_REG(data->fan_cmd1));
+ return sprintf(buf, "%ld\n", G762_CLKDIV_FROM_REG(data->fan_cmd1));
}
static ssize_t fan1_div_store(struct device *dev, struct device_attribute *da,
@@ -753,7 +703,7 @@ static ssize_t fan1_pulses_show(struct device *dev,
if (IS_ERR(data))
return PTR_ERR(data);
- return sprintf(buf, "%d\n", G762_PULSE_FROM_REG(data->fan_cmd1));
+ return sprintf(buf, "%ld\n", G762_PULSE_FROM_REG(data->fan_cmd1));
}
static ssize_t fan1_pulses_store(struct device *dev,
--
2.39.2
next prev parent reply other threads:[~2024-07-04 21:37 UTC|newest]
Thread overview: 8+ messages / expand[flat|nested] mbox.gz Atom feed top
2024-07-04 21:37 [PATCH 0/7] hwmon: (g762) Convert to with_info API Guenter Roeck
2024-07-04 21:37 ` [PATCH 1/7] hwmon: (g762) Simplify clock initialization Guenter Roeck
2024-07-04 21:37 ` [PATCH 2/7] hwmon: (g762) Drop platform data support Guenter Roeck
2024-07-04 21:37 ` [PATCH 3/7] hwmon: (g762) Reorder include files to be in alphabetic order Guenter Roeck
2024-07-04 21:37 ` Guenter Roeck [this message]
2024-07-04 21:37 ` [PATCH 5/7] hwmon: (g762) Make chip configuration devicetree independent Guenter Roeck
2024-07-04 21:37 ` [PATCH 6/7] hwmon: (g762) Convert to use regmap Guenter Roeck
2024-07-04 21:37 ` [PATCH 7/7] hwmon: (g762) Convert to with_info API Guenter Roeck
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=20240704213712.2699553-5-linux@roeck-us.net \
--to=linux@roeck-us.net \
--cc=linux-hwmon@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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox