Skip to content

Commit

Permalink
hwmon: (lm85) Clean up the handling of additional resolution bits
Browse files Browse the repository at this point in the history
The way the lm85 driver currently handles the additional resolution
bits some chips have for the voltage and temperature measurements is
quite complex. This is my attempt to make it simpler.

Signed-off-by: Jean Delvare <khali@linux-fr.org>
Acked-by: Juerg Haefliger <juergh at gmail.com>
Signed-off-by: Mark M. Hoffman <mhoffman@lightlink.com>
  • Loading branch information
Jean Delvare authored and Mark M. Hoffman committed Oct 10, 2007
1 parent bf76e9d commit 5a4d3ef
Showing 1 changed file with 12 additions and 20 deletions.
32 changes: 12 additions & 20 deletions drivers/hwmon/lm85.c
Original file line number Diff line number Diff line change
Expand Up @@ -139,10 +139,10 @@ static int lm85_scaling[] = { /* .001 Volts */
#define INS_TO_REG(n,val) \
SENSORS_LIMIT(SCALE(val,lm85_scaling[n],192),0,255)

#define INSEXT_FROM_REG(n,val,ext,scale) \
SCALE((val)*(scale) + (ext),192*(scale),lm85_scaling[n])
#define INSEXT_FROM_REG(n,val,ext) \
SCALE(((val) << 4) + (ext), 192 << 4, lm85_scaling[n])

#define INS_FROM_REG(n,val) INSEXT_FROM_REG(n,val,0,1)
#define INS_FROM_REG(n,val) SCALE((val), 192, lm85_scaling[n])

/* FAN speed is measured using 90kHz clock */
#define FAN_TO_REG(val) (SENSORS_LIMIT( (val)<=0?0: 5400000/(val),0,65534))
Expand All @@ -151,10 +151,9 @@ static int lm85_scaling[] = { /* .001 Volts */
/* Temperature is reported in .001 degC increments */
#define TEMP_TO_REG(val) \
SENSORS_LIMIT(SCALE(val,1000,1),-127,127)
#define TEMPEXT_FROM_REG(val,ext,scale) \
SCALE((val)*scale + (ext),scale,1000)
#define TEMP_FROM_REG(val) \
TEMPEXT_FROM_REG(val,0,1)
#define TEMPEXT_FROM_REG(val,ext) \
SCALE(((val) << 4) + (ext), 16, 1000)
#define TEMP_FROM_REG(val) ((val) * 1000)

#define PWM_TO_REG(val) (SENSORS_LIMIT(val,0,255))
#define PWM_FROM_REG(val) (val)
Expand Down Expand Up @@ -334,7 +333,6 @@ struct lm85_data {
u8 tach_mode; /* Register encoding, combined */
u8 temp_ext[3]; /* Decoded values */
u8 in_ext[8]; /* Decoded values */
u8 adc_scale; /* ADC Extended bits scaling factor */
u8 fan_ppr; /* Register value */
u8 smooth[3]; /* Register encoding */
u8 vid; /* Register value */
Expand Down Expand Up @@ -541,8 +539,7 @@ static ssize_t show_in(struct device *dev, struct device_attribute *attr,
struct lm85_data *data = lm85_update_device(dev);
return sprintf( buf, "%d\n", INSEXT_FROM_REG(nr,
data->in[nr],
data->in_ext[nr],
data->adc_scale) );
data->in_ext[nr]));
}

static ssize_t show_in_min(struct device *dev, struct device_attribute *attr,
Expand Down Expand Up @@ -616,8 +613,7 @@ static ssize_t show_temp(struct device *dev, struct device_attribute *attr,
int nr = to_sensor_dev_attr(attr)->index;
struct lm85_data *data = lm85_update_device(dev);
return sprintf(buf,"%d\n", TEMPEXT_FROM_REG(data->temp[nr],
data->temp_ext[nr],
data->adc_scale) );
data->temp_ext[nr]));
}

static ssize_t show_temp_min(struct device *dev, struct device_attribute *attr,
Expand Down Expand Up @@ -1394,6 +1390,8 @@ static struct lm85_data *lm85_update_device(struct device *dev)

/* Have to read extended bits first to "freeze" the
* more significant bits that are read later.
* There are 2 additional resolution bits per channel and we
* have room for 4, so we shift them to the left.
*/
if ( (data->type == adm1027) || (data->type == adt7463) ) {
int ext1 = lm85_read_value(client,
Expand All @@ -1403,18 +1401,12 @@ static struct lm85_data *lm85_update_device(struct device *dev)
int val = (ext1 << 8) + ext2;

for(i = 0; i <= 4; i++)
data->in_ext[i] = (val>>(i * 2))&0x03;
data->in_ext[i] = ((val>>(i * 2))&0x03) << 2;

for(i = 0; i <= 2; i++)
data->temp_ext[i] = (val>>((i + 5) * 2))&0x03;
data->temp_ext[i] = (val>>((i + 4) * 2))&0x0c;
}

/* adc_scale is 2^(number of LSBs). There are 4 extra bits in
the emc6d102 and 2 in the adt7463 and adm1027. In all
other chips ext is always 0 and the value of scale is
irrelevant. So it is left in 4*/
data->adc_scale = (data->type == emc6d102 ) ? 16 : 4;

data->vid = lm85_read_value(client, LM85_REG_VID);

for (i = 0; i <= 3; ++i) {
Expand Down

0 comments on commit 5a4d3ef

Please sign in to comment.