@@ -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,
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(-)