forked from Minki/linux
hwmon/w83781d: Clean up conversion macros
* Fix voltage rounding * Drop useless macros * Drop useless casts * Turn macros evaluating their parameters more than once into inline functions * Use signed variables for temperatures Signed-off-by: Jean Delvare <khali@linux-fr.org>
This commit is contained in:
parent
7666c13c62
commit
474d00a891
@ -164,12 +164,9 @@ static const u8 BIT_SCFG2[] = { 0x10, 0x20, 0x40 };
|
|||||||
#define W83781D_REG_RT_IDX 0x50
|
#define W83781D_REG_RT_IDX 0x50
|
||||||
#define W83781D_REG_RT_VAL 0x51
|
#define W83781D_REG_RT_VAL 0x51
|
||||||
|
|
||||||
/* Conversions. Rounding and limit checking is only done on the TO_REG
|
/* Conversions */
|
||||||
variants. Note that you should be a bit careful with which arguments
|
#define IN_TO_REG(val) SENSORS_LIMIT(((val) + 8) / 16, 0, 255)
|
||||||
these macros are called: arguments may be evaluated more than once.
|
#define IN_FROM_REG(val) ((val) * 16)
|
||||||
Fixing this is just not worth it. */
|
|
||||||
#define IN_TO_REG(val) (SENSORS_LIMIT((((val) * 10 + 8)/16),0,255))
|
|
||||||
#define IN_FROM_REG(val) (((val) * 16) / 10)
|
|
||||||
|
|
||||||
static inline u8
|
static inline u8
|
||||||
FAN_TO_REG(long rpm, int div)
|
FAN_TO_REG(long rpm, int div)
|
||||||
@ -180,24 +177,24 @@ FAN_TO_REG(long rpm, int div)
|
|||||||
return SENSORS_LIMIT((1350000 + rpm * div / 2) / (rpm * div), 1, 254);
|
return SENSORS_LIMIT((1350000 + rpm * div / 2) / (rpm * div), 1, 254);
|
||||||
}
|
}
|
||||||
|
|
||||||
#define FAN_FROM_REG(val,div) ((val) == 0 ? -1 : \
|
static inline long
|
||||||
((val) == 255 ? 0 : \
|
FAN_FROM_REG(u8 val, int div)
|
||||||
1350000 / ((val) * (div))))
|
{
|
||||||
|
if (val == 0)
|
||||||
|
return -1;
|
||||||
|
if (val == 255)
|
||||||
|
return 0;
|
||||||
|
return 1350000 / (val * div);
|
||||||
|
}
|
||||||
|
|
||||||
#define TEMP_TO_REG(val) (SENSORS_LIMIT(((val) < 0 ? (val)+0x100*1000 \
|
#define TEMP_TO_REG(val) SENSORS_LIMIT((val) / 1000, -127, 128)
|
||||||
: (val)) / 1000, 0, 0xff))
|
#define TEMP_FROM_REG(val) ((val) * 1000)
|
||||||
#define TEMP_FROM_REG(val) (((val) & 0x80 ? (val)-0x100 : (val)) * 1000)
|
|
||||||
|
|
||||||
#define PWM_FROM_REG(val) (val)
|
|
||||||
#define PWM_TO_REG(val) (SENSORS_LIMIT((val),0,255))
|
|
||||||
#define BEEP_MASK_FROM_REG(val,type) ((type) == as99127f ? \
|
#define BEEP_MASK_FROM_REG(val,type) ((type) == as99127f ? \
|
||||||
(val) ^ 0x7fff : (val))
|
(val) ^ 0x7fff : (val))
|
||||||
#define BEEP_MASK_TO_REG(val,type) ((type) == as99127f ? \
|
#define BEEP_MASK_TO_REG(val,type) ((type) == as99127f ? \
|
||||||
(~(val)) & 0x7fff : (val) & 0xffffff)
|
(~(val)) & 0x7fff : (val) & 0xffffff)
|
||||||
|
|
||||||
#define BEEP_ENABLE_TO_REG(val) ((val) ? 1 : 0)
|
|
||||||
#define BEEP_ENABLE_FROM_REG(val) ((val) ? 1 : 0)
|
|
||||||
|
|
||||||
#define DIV_FROM_REG(val) (1 << (val))
|
#define DIV_FROM_REG(val) (1 << (val))
|
||||||
|
|
||||||
static inline u8
|
static inline u8
|
||||||
@ -212,7 +209,7 @@ DIV_TO_REG(long val, enum chips type)
|
|||||||
break;
|
break;
|
||||||
val >>= 1;
|
val >>= 1;
|
||||||
}
|
}
|
||||||
return ((u8) i);
|
return i;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* There are some complications in a module like this. First off, W83781D chips
|
/* There are some complications in a module like this. First off, W83781D chips
|
||||||
@ -246,9 +243,9 @@ struct w83781d_data {
|
|||||||
u8 in_min[9]; /* Register value - 8 & 9 for 782D only */
|
u8 in_min[9]; /* Register value - 8 & 9 for 782D only */
|
||||||
u8 fan[3]; /* Register value */
|
u8 fan[3]; /* Register value */
|
||||||
u8 fan_min[3]; /* Register value */
|
u8 fan_min[3]; /* Register value */
|
||||||
u8 temp;
|
s8 temp; /* Register value */
|
||||||
u8 temp_max; /* Register value */
|
s8 temp_max; /* Register value */
|
||||||
u8 temp_max_hyst; /* Register value */
|
s8 temp_max_hyst; /* Register value */
|
||||||
u16 temp_add[2]; /* Register value */
|
u16 temp_add[2]; /* Register value */
|
||||||
u16 temp_max_add[2]; /* Register value */
|
u16 temp_max_add[2]; /* Register value */
|
||||||
u16 temp_max_hyst_add[2]; /* Register value */
|
u16 temp_max_hyst_add[2]; /* Register value */
|
||||||
@ -303,7 +300,7 @@ static struct platform_driver w83781d_isa_driver = {
|
|||||||
static ssize_t show_##reg (struct device *dev, char *buf, int nr) \
|
static ssize_t show_##reg (struct device *dev, char *buf, int nr) \
|
||||||
{ \
|
{ \
|
||||||
struct w83781d_data *data = w83781d_update_device(dev); \
|
struct w83781d_data *data = w83781d_update_device(dev); \
|
||||||
return sprintf(buf,"%ld\n", (long)IN_FROM_REG(data->reg[nr] * 10)); \
|
return sprintf(buf, "%ld\n", (long)IN_FROM_REG(data->reg[nr])); \
|
||||||
}
|
}
|
||||||
show_in_reg(in);
|
show_in_reg(in);
|
||||||
show_in_reg(in_min);
|
show_in_reg(in_min);
|
||||||
@ -316,7 +313,7 @@ static ssize_t store_in_##reg (struct device *dev, const char *buf, size_t count
|
|||||||
struct i2c_client *client = &data->client; \
|
struct i2c_client *client = &data->client; \
|
||||||
u32 val; \
|
u32 val; \
|
||||||
\
|
\
|
||||||
val = simple_strtoul(buf, NULL, 10) / 10; \
|
val = simple_strtoul(buf, NULL, 10); \
|
||||||
\
|
\
|
||||||
mutex_lock(&data->update_lock); \
|
mutex_lock(&data->update_lock); \
|
||||||
data->in_##reg[nr] = IN_TO_REG(val); \
|
data->in_##reg[nr] = IN_TO_REG(val); \
|
||||||
@ -534,8 +531,7 @@ static ssize_t show_beep_mask (struct device *dev, struct device_attribute *attr
|
|||||||
static ssize_t show_beep_enable (struct device *dev, struct device_attribute *attr, char *buf)
|
static ssize_t show_beep_enable (struct device *dev, struct device_attribute *attr, char *buf)
|
||||||
{
|
{
|
||||||
struct w83781d_data *data = w83781d_update_device(dev);
|
struct w83781d_data *data = w83781d_update_device(dev);
|
||||||
return sprintf(buf, "%ld\n",
|
return sprintf(buf, "%ld\n", (long)data->beep_enable);
|
||||||
(long)BEEP_ENABLE_FROM_REG(data->beep_enable));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#define BEEP_ENABLE 0 /* Store beep_enable */
|
#define BEEP_ENABLE 0 /* Store beep_enable */
|
||||||
@ -566,7 +562,7 @@ store_beep_reg(struct device *dev, const char *buf, size_t count,
|
|||||||
val2 = (data->beep_mask >> 8) & 0x7f;
|
val2 = (data->beep_mask >> 8) & 0x7f;
|
||||||
} else { /* We are storing beep_enable */
|
} else { /* We are storing beep_enable */
|
||||||
val2 = w83781d_read_value(client, W83781D_REG_BEEP_INTS2) & 0x7f;
|
val2 = w83781d_read_value(client, W83781D_REG_BEEP_INTS2) & 0x7f;
|
||||||
data->beep_enable = BEEP_ENABLE_TO_REG(val);
|
data->beep_enable = !!val;
|
||||||
}
|
}
|
||||||
|
|
||||||
w83781d_write_value(client, W83781D_REG_BEEP_INTS2,
|
w83781d_write_value(client, W83781D_REG_BEEP_INTS2,
|
||||||
@ -659,7 +655,7 @@ static ssize_t
|
|||||||
show_pwm_reg(struct device *dev, char *buf, int nr)
|
show_pwm_reg(struct device *dev, char *buf, int nr)
|
||||||
{
|
{
|
||||||
struct w83781d_data *data = w83781d_update_device(dev);
|
struct w83781d_data *data = w83781d_update_device(dev);
|
||||||
return sprintf(buf, "%ld\n", (long) PWM_FROM_REG(data->pwm[nr - 1]));
|
return sprintf(buf, "%ld\n", (long)data->pwm[nr - 1]);
|
||||||
}
|
}
|
||||||
|
|
||||||
static ssize_t
|
static ssize_t
|
||||||
@ -679,7 +675,7 @@ store_pwm_reg(struct device *dev, const char *buf, size_t count, int nr)
|
|||||||
val = simple_strtoul(buf, NULL, 10);
|
val = simple_strtoul(buf, NULL, 10);
|
||||||
|
|
||||||
mutex_lock(&data->update_lock);
|
mutex_lock(&data->update_lock);
|
||||||
data->pwm[nr - 1] = PWM_TO_REG(val);
|
data->pwm[nr - 1] = SENSORS_LIMIT(val, 0, 255);
|
||||||
w83781d_write_value(client, W83781D_REG_PWM(nr), data->pwm[nr - 1]);
|
w83781d_write_value(client, W83781D_REG_PWM(nr), data->pwm[nr - 1]);
|
||||||
mutex_unlock(&data->update_lock);
|
mutex_unlock(&data->update_lock);
|
||||||
return count;
|
return count;
|
||||||
|
Loading…
Reference in New Issue
Block a user