aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/hwmon/w83781d.c
diff options
context:
space:
mode:
authorJean Delvare <khali@linux-fr.org>2007-05-08 11:22:03 -0400
committerJean Delvare <khali@hyperion.delvare>2007-05-08 11:22:03 -0400
commit474d00a8912f56241cec6e1832ce390e87bfb243 (patch)
tree810be265406feb19e4348b4ba598bf01dde77f14 /drivers/hwmon/w83781d.c
parent7666c13c627fdc65e8057013893c183c3bafe59e (diff)
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>
Diffstat (limited to 'drivers/hwmon/w83781d.c')
-rw-r--r--drivers/hwmon/w83781d.c52
1 files changed, 24 insertions, 28 deletions
diff --git a/drivers/hwmon/w83781d.c b/drivers/hwmon/w83781d.c
index 96338ddd74a7..4f93d79a4308 100644
--- a/drivers/hwmon/w83781d.c
+++ b/drivers/hwmon/w83781d.c
@@ -164,12 +164,9 @@ static const u8 BIT_SCFG2[] = { 0x10, 0x20, 0x40 };
164#define W83781D_REG_RT_IDX 0x50 164#define W83781D_REG_RT_IDX 0x50
165#define W83781D_REG_RT_VAL 0x51 165#define W83781D_REG_RT_VAL 0x51
166 166
167/* Conversions. Rounding and limit checking is only done on the TO_REG 167/* Conversions */
168 variants. Note that you should be a bit careful with which arguments 168#define IN_TO_REG(val) SENSORS_LIMIT(((val) + 8) / 16, 0, 255)
169 these macros are called: arguments may be evaluated more than once. 169#define IN_FROM_REG(val) ((val) * 16)
170 Fixing this is just not worth it. */
171#define IN_TO_REG(val) (SENSORS_LIMIT((((val) * 10 + 8)/16),0,255))
172#define IN_FROM_REG(val) (((val) * 16) / 10)
173 170
174static inline u8 171static inline u8
175FAN_TO_REG(long rpm, int div) 172FAN_TO_REG(long rpm, int div)
@@ -180,24 +177,24 @@ FAN_TO_REG(long rpm, int div)
180 return SENSORS_LIMIT((1350000 + rpm * div / 2) / (rpm * div), 1, 254); 177 return SENSORS_LIMIT((1350000 + rpm * div / 2) / (rpm * div), 1, 254);
181} 178}
182 179
183#define FAN_FROM_REG(val,div) ((val) == 0 ? -1 : \ 180static inline long
184 ((val) == 255 ? 0 : \ 181FAN_FROM_REG(u8 val, int div)
185 1350000 / ((val) * (div)))) 182{
183 if (val == 0)
184 return -1;
185 if (val == 255)
186 return 0;
187 return 1350000 / (val * div);
188}
186 189
187#define TEMP_TO_REG(val) (SENSORS_LIMIT(((val) < 0 ? (val)+0x100*1000 \ 190#define TEMP_TO_REG(val) SENSORS_LIMIT((val) / 1000, -127, 128)
188 : (val)) / 1000, 0, 0xff)) 191#define TEMP_FROM_REG(val) ((val) * 1000)
189#define TEMP_FROM_REG(val) (((val) & 0x80 ? (val)-0x100 : (val)) * 1000)
190 192
191#define PWM_FROM_REG(val) (val)
192#define PWM_TO_REG(val) (SENSORS_LIMIT((val),0,255))
193#define BEEP_MASK_FROM_REG(val,type) ((type) == as99127f ? \ 193#define BEEP_MASK_FROM_REG(val,type) ((type) == as99127f ? \
194 (val) ^ 0x7fff : (val)) 194 (val) ^ 0x7fff : (val))
195#define BEEP_MASK_TO_REG(val,type) ((type) == as99127f ? \ 195#define BEEP_MASK_TO_REG(val,type) ((type) == as99127f ? \
196 (~(val)) & 0x7fff : (val) & 0xffffff) 196 (~(val)) & 0x7fff : (val) & 0xffffff)
197 197
198#define BEEP_ENABLE_TO_REG(val) ((val) ? 1 : 0)
199#define BEEP_ENABLE_FROM_REG(val) ((val) ? 1 : 0)
200
201#define DIV_FROM_REG(val) (1 << (val)) 198#define DIV_FROM_REG(val) (1 << (val))
202 199
203static inline u8 200static inline u8
@@ -212,7 +209,7 @@ DIV_TO_REG(long val, enum chips type)
212 break; 209 break;
213 val >>= 1; 210 val >>= 1;
214 } 211 }
215 return ((u8) i); 212 return i;
216} 213}
217 214
218/* There are some complications in a module like this. First off, W83781D chips 215/* There are some complications in a module like this. First off, W83781D chips
@@ -246,9 +243,9 @@ struct w83781d_data {
246 u8 in_min[9]; /* Register value - 8 & 9 for 782D only */ 243 u8 in_min[9]; /* Register value - 8 & 9 for 782D only */
247 u8 fan[3]; /* Register value */ 244 u8 fan[3]; /* Register value */
248 u8 fan_min[3]; /* Register value */ 245 u8 fan_min[3]; /* Register value */
249 u8 temp; 246 s8 temp; /* Register value */
250 u8 temp_max; /* Register value */ 247 s8 temp_max; /* Register value */
251 u8 temp_max_hyst; /* Register value */ 248 s8 temp_max_hyst; /* Register value */
252 u16 temp_add[2]; /* Register value */ 249 u16 temp_add[2]; /* Register value */
253 u16 temp_max_add[2]; /* Register value */ 250 u16 temp_max_add[2]; /* Register value */
254 u16 temp_max_hyst_add[2]; /* Register value */ 251 u16 temp_max_hyst_add[2]; /* Register value */
@@ -303,7 +300,7 @@ static struct platform_driver w83781d_isa_driver = {
303static ssize_t show_##reg (struct device *dev, char *buf, int nr) \ 300static ssize_t show_##reg (struct device *dev, char *buf, int nr) \
304{ \ 301{ \
305 struct w83781d_data *data = w83781d_update_device(dev); \ 302 struct w83781d_data *data = w83781d_update_device(dev); \
306 return sprintf(buf,"%ld\n", (long)IN_FROM_REG(data->reg[nr] * 10)); \ 303 return sprintf(buf, "%ld\n", (long)IN_FROM_REG(data->reg[nr])); \
307} 304}
308show_in_reg(in); 305show_in_reg(in);
309show_in_reg(in_min); 306show_in_reg(in_min);
@@ -316,7 +313,7 @@ static ssize_t store_in_##reg (struct device *dev, const char *buf, size_t count
316 struct i2c_client *client = &data->client; \ 313 struct i2c_client *client = &data->client; \
317 u32 val; \ 314 u32 val; \
318 \ 315 \
319 val = simple_strtoul(buf, NULL, 10) / 10; \ 316 val = simple_strtoul(buf, NULL, 10); \
320 \ 317 \
321 mutex_lock(&data->update_lock); \ 318 mutex_lock(&data->update_lock); \
322 data->in_##reg[nr] = IN_TO_REG(val); \ 319 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
534static ssize_t show_beep_enable (struct device *dev, struct device_attribute *attr, char *buf) 531static ssize_t show_beep_enable (struct device *dev, struct device_attribute *attr, char *buf)
535{ 532{
536 struct w83781d_data *data = w83781d_update_device(dev); 533 struct w83781d_data *data = w83781d_update_device(dev);
537 return sprintf(buf, "%ld\n", 534 return sprintf(buf, "%ld\n", (long)data->beep_enable);
538 (long)BEEP_ENABLE_FROM_REG(data->beep_enable));
539} 535}
540 536
541#define BEEP_ENABLE 0 /* Store beep_enable */ 537#define BEEP_ENABLE 0 /* Store beep_enable */
@@ -566,7 +562,7 @@ store_beep_reg(struct device *dev, const char *buf, size_t count,
566 val2 = (data->beep_mask >> 8) & 0x7f; 562 val2 = (data->beep_mask >> 8) & 0x7f;
567 } else { /* We are storing beep_enable */ 563 } else { /* We are storing beep_enable */
568 val2 = w83781d_read_value(client, W83781D_REG_BEEP_INTS2) & 0x7f; 564 val2 = w83781d_read_value(client, W83781D_REG_BEEP_INTS2) & 0x7f;
569 data->beep_enable = BEEP_ENABLE_TO_REG(val); 565 data->beep_enable = !!val;
570 } 566 }
571 567
572 w83781d_write_value(client, W83781D_REG_BEEP_INTS2, 568 w83781d_write_value(client, W83781D_REG_BEEP_INTS2,
@@ -659,7 +655,7 @@ static ssize_t
659show_pwm_reg(struct device *dev, char *buf, int nr) 655show_pwm_reg(struct device *dev, char *buf, int nr)
660{ 656{
661 struct w83781d_data *data = w83781d_update_device(dev); 657 struct w83781d_data *data = w83781d_update_device(dev);
662 return sprintf(buf, "%ld\n", (long) PWM_FROM_REG(data->pwm[nr - 1])); 658 return sprintf(buf, "%ld\n", (long)data->pwm[nr - 1]);
663} 659}
664 660
665static ssize_t 661static ssize_t
@@ -679,7 +675,7 @@ store_pwm_reg(struct device *dev, const char *buf, size_t count, int nr)
679 val = simple_strtoul(buf, NULL, 10); 675 val = simple_strtoul(buf, NULL, 10);
680 676
681 mutex_lock(&data->update_lock); 677 mutex_lock(&data->update_lock);
682 data->pwm[nr - 1] = PWM_TO_REG(val); 678 data->pwm[nr - 1] = SENSORS_LIMIT(val, 0, 255);
683 w83781d_write_value(client, W83781D_REG_PWM(nr), data->pwm[nr - 1]); 679 w83781d_write_value(client, W83781D_REG_PWM(nr), data->pwm[nr - 1]);
684 mutex_unlock(&data->update_lock); 680 mutex_unlock(&data->update_lock);
685 return count; 681 return count;