Skip to contentSkip to conversation

it87 pwm patch for 2.6.6

PATCH Jonas Munsin 2 participants 10 messages May 19, 2005

lore
Share & tools
lore
#0 lore raw

Hello,

Attached is two incremental patches against drivers/i2c/it87.c, adding more support to the it87 fan controller driver in 2.6.6-bk2. Attached is also a small ruby script I use on my computer, just as an example.

it87_cleanups_jm1.patch:
a rename to reflect the datasheet name better, removal of a old comment that doesn't apply anymore to the 2.6.x line

it87_manual_and_auto_pwm_jm1.patch:
manual and auto pwm support in one largish patch (I started to split this one into two separate patches, but it was too much work and I got lazy..).

The work is based on Takeru KOMORIYA' patch:
http://archives.andrew.net.au/lm-sensors/msg07002.html

The auto fan speed API is written to match these messages:
http://archives.andrew.net.au/lm-sensors/msg07517.html
http://archives.andrew.net.au/lm-sensors/msg07519.html

The following two pecularities of the it87 chip didn't exactly match that API suggestion:

1. If the temperature of any programmed channel goes above fanX_auto_temp_max, all fans in sg-automatic mode turn on at maximum speed until the temperture comes down.

2. The fanX_auto_temp_channel variables are bitfields, but they can only have one bit on (i.e., the possible values are 1, 2 and 4).

Some other observations:

1. I wrote this reading the it8705f datasheet. I got the impression the other it87* chips work in the same way, but I haven't actually checked this.

2. The fan controllers can be in three different states, changes in settings effecting other states than the current one are rejected. An alternative would be to save them and write them to the it87 after a state change.

3. There are a lot of tunable settings files... The sysfs directory becomes quite cluttered. And the it87.c contains a lot of adminstrative overhead to set up all those files.

4. The fanX_auto_temp_* variables need to be mangled just like fanX_input (i.e., if you have a compute line in your sensors.conf, you need to do the same computation to values written to these files).

-------------- next part --------------

it87.c renamed from it87.c.orig +13 -17
@@ -128,7 +128,7 @@
 
 #define IT87_REG_FAN(nr)       (0x0d + (nr))
 #define IT87_REG_FAN_MIN(nr)   (0x10 + (nr))
-#define IT87_REG_FAN_CTRL      0x13
+#define IT87_REG_FAN_MAIN_CTRL 0x13
 
 #define IT87_REG_VIN(nr)       (0x20 + (nr))
 #define IT87_REG_TEMP(nr)      (0x29 + (nr))
@@ -159,9 +159,9 @@
 
 #define FAN_FROM_REG(val,div) ((val)=0?-1:(val)=255?0:1350000/((val)*(div)))
 
-#define TEMP_TO_REG(val) (SENSORS_LIMIT(((val)<0?(((val)-5)/10):\
-					((val)+5)/10),0,255))
-#define TEMP_FROM_REG(val) (((val)>0x80?(val)-0x100:(val))*10)
+#define TEMP_TO_REG(val) (SENSORS_LIMIT(((val)<0?(((val)-5)/(10*100)):\
+					((val)+5)/(10*100)),0,255))
+#define TEMP_FROM_REG(val) (((val)>0x80?(val)-0x100:(val))*10*100)
 
 #define VID_FROM_REG(val) ((val)=0x1f?0:(val)>=0x10?510-(val)*10:\
 				205-(val)*5)
@@ -325,24 +325,24 @@
 static ssize_t show_temp(struct device *dev, char *buf, int nr)
 {
 	struct it87_data *data = it87_update_device(dev);
-	return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp[nr])*100 );
+	return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp[nr]));
 }
 static ssize_t show_temp_max(struct device *dev, char *buf, int nr)
 {
 	struct it87_data *data = it87_update_device(dev);
-	return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_high[nr])*100);
+	return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_high[nr]));
 }
 static ssize_t show_temp_min(struct device *dev, char *buf, int nr)
 {
 	struct it87_data *data = it87_update_device(dev);
-	return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_low[nr])*100);
+	return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_low[nr]));
 }
 static ssize_t set_temp_max(struct device *dev, const char *buf, 
 		size_t count, int nr)
 {
 	struct i2c_client *client = to_i2c_client(dev);
 	struct it87_data *data = i2c_get_clientdata(client);
-	int val = simple_strtol(buf, NULL, 10)/100;
+	int val = simple_strtol(buf, NULL, 10);
 	data->temp_high[nr] = TEMP_TO_REG(val);
 	it87_write_value(client, IT87_REG_TEMP_HIGH(nr), data->temp_high[nr]);
 	return count;
@@ -352,7 +352,7 @@
 {
 	struct i2c_client *client = to_i2c_client(dev);
 	struct it87_data *data = i2c_get_clientdata(client);
-	int val = simple_strtol(buf, NULL, 10)/100;
+	int val = simple_strtol(buf, NULL, 10);
 	data->temp_low[nr] = TEMP_TO_REG(val);
 	it87_write_value(client, IT87_REG_TEMP_LOW(nr), data->temp_low[nr]);
 	return count;
@@ -773,9 +773,7 @@
    We don't want to lock the whole ISA bus, so we lock each client
    separately.
    We ignore the IT87 BUSY flag at this moment - it could lead to deadlocks,
-   would slow down the IT87 access and should not be necessary. 
-   There are some ugly typecasts here, but the good new is - they should
-   nowhere else be necessary! */
+   would slow down the IT87 access and should not be necessary. */
 static int it87_read_value(struct i2c_client *client, u8 reg)
 {
 	struct it87_data *data = i2c_get_clientdata(client);
@@ -795,9 +793,7 @@
    We don't want to lock the whole ISA bus, so we lock each client
    separately.
    We ignore the IT87 BUSY flag at this moment - it could lead to deadlocks,
-   would slow down the IT87 access and should not be necessary. 
-   There are some ugly typecasts here, but the good new is - they should
-   nowhere else be necessary! */
+   would slow down the IT87 access and should not be necessary. */
 static int it87_write_value(struct i2c_client *client, u8 reg, u8 value)
 {
 	struct it87_data *data = i2c_get_clientdata(client);
@@ -840,11 +836,11 @@
 	}
 
 	/* Check if tachometers are reset manually or by some reason */
-	tmp = it87_read_value(client, IT87_REG_FAN_CTRL);
+	tmp = it87_read_value(client, IT87_REG_FAN_MAIN_CTRL);
 	if ((tmp & 0x70) = 0) {
 		/* Enable all fan tachometers */
 		tmp = (tmp & 0x8f) | 0x70;
-		it87_write_value(client, IT87_REG_FAN_CTRL, tmp);
+		it87_write_value(client, IT87_REG_FAN_MAIN_CTRL, tmp);
 	}
 
 	/* Start monitoring */

-------------- next part --------------

it87.c renamed from it87.c.orig +529 -2

606 lines not on this page · raw message

#!/usr/bin/ruby

def set_value(file, val)

	Kernel.system("echo #{val} > #{file}")

end

def convert_cpu_temp(temp)

	((3*temp-128)*1000).to_i
end
def convert_mb_temp(temp)
	((temp)*1000).to_i

end

devdir="/sys/bus/i2c/devices/0-0290"

begin

	f = File.new(devdir, "r")
	f.close

rescue Errno::ENOENT
print "no driver module loaded?\n"
exit
end

Dir.chdir(devdir)

set_value("fan1_pwm_enable", 1)
set_value("fan1_auto_enable", 1)
set_value("fan1_auto_temp_channel", 1)

set_value("fan1_auto_temp_off", convert_cpu_temp(53)) set_value("fan1_auto_temp_min", convert_cpu_temp(55)) set_value("fan1_auto_pwm_min", 205) set_value("fan1_auto_temp_low", convert_cpu_temp(58)) set_value("fan1_auto_pwm_low", 220) set_value("fan1_auto_temp_medium", convert_cpu_temp(60)) set_value("fan1_auto_pwm_medium", 245) set_value("fan1_auto_temp_max", convert_cpu_temp(62))

set_value("fan2_pwm_enable", 1)
set_value("fan2_auto_enable", 1)
set_value("fan2_auto_temp_channel", 2)

offtemp = 37 # reach => off
ontemp = 42 # cross => on
panictemp = 45
set_value("fan2_auto_temp_off", convert_mb_temp(offtemp)) set_value("fan2_auto_temp_min", convert_mb_temp(ontemp)) set_value("fan2_auto_pwm_min", 255) set_value("fan2_auto_temp_low", convert_mb_temp(ontemp)) set_value("fan2_auto_pwm_low", 255) set_value("fan2_auto_temp_medium", convert_mb_temp(ontemp)) set_value("fan2_auto_pwm_medium", 255) set_value("fan2_auto_temp_max", convert_mb_temp(panictemp))

#1 lore raw

Hi Jonas,

Sorry for the delay.

Quoted 7 lines

Attached is two incremental patches against drivers/i2c/it87.c, adding more support to the it87 fan controller driver in 2.6.6-bk2. Attached is also a small ruby script I use on my computer, just as an example.

it87_cleanups_jm1.patch:
a rename to reflect the datasheet name better, removal of a old comment that doesn't apply anymore to the 2.6.x line

Interesting patch, see my comments below.

Quoted 4 lines

it87_manual_and_auto_pwm_jm1.patch:
manual and auto pwm support in one largish patch (I started to split this one into two separate patches, but it was too much work and I got lazy..).

One single 23k patch is too much IMHO and it would help if it was split. However, I remember that we had trouble doing this with Takeru.

Quoted 3 lines

The auto fan speed API is written to match these messages:
http://archives.andrew.net.au/lm-sensors/msg07517.html
http://archives.andrew.net.au/lm-sensors/msg07519.html

Note about this: I'm not proud of this interface, some points can probably be improved, so there is no guarantee that it won't change soon. But you had to start with something, of course...

Quoted 6 lines

The following two pecularities of the it87 chip didn't exactly match that API suggestion:

1. If the temperature of any programmed channel goes above fanX_auto_temp_max, all fans in sg-automatic mode turn on at maximum speed until the temperture comes down.

If this is how the chip behaves, you can't do anything. And it's not really against the API.

Quoted 2 lines

2. The fanX_auto_temp_channel variables are bitfields, but they can only have one bit on (i.e., the possible values are 1, 2 and 4).

The API says how to represent things. It doesn't pretend that all values are possible for every chip. Actually I expect most chips to behave like the it87, and that bitfield thing looks overkill. It was really made for the adm1031/adm1030 and I'm seriously thinking of changing this for something more simple.

Quoted 3 lines

3. There are a lot of tunable settings files... The sysfs directory becomes quite cluttered. And the it87.c contains a lot of adminstrative overhead to set up all those files.

It's not supposed to be a problem. This is how the whole interface was designed.

Quoted 3 lines

4. The fanX_auto_temp_* variables need to be mangled just like fanX_input (i.e., if you have a compute line in your sensors.conf, you need to do the same computation to values written to these files).

Unfortunately, it would require that libsensors knows about these files, and it doesn't. And I it's probably not the right time to add it, since the library is already severly overcrowded and would benefit a complete redesign for 2.6. So I have nothing to offer for now. I guess the problem is the same for the 2.4 driver?

About your code now. I only looked at the cleanup patch for now, sorry.

Moving the *100,/100 inside the conversion macros is right. There's no need to split the 10*100, just write 1000. The 100 was introduced in the first place because temperatures had a magnitude of 1 in 2.4 and 3 in 2.6. The correct fix would have been to change the macros, like you propose now.

You could fix IN_FROM_REG(val) too, while you're at it. It misses a +5 for correct rounding. It's fixed in 2.4, but was forgotten in 2.6.

Once done, you should send this patch to Greg KH for inclusion into the 2.6 tree. CC: the list and say that I reviewed it, and it should be fine.

Quick note about the second patch, although I didn't fully read it yet:

#define IT87_REG_OFF_TEMP(nr) ((nr=2)?0x70:((nr=1)?0x68:0x60))

can be instead written:

#define IT87_REG_OFF_TEMP(nr) (0x60 + (nr) * 8)

Same is true for all other macros you are introducing.

I'll read the rest tomorrow (hopefully).

Thanks.

Signature · 2 lines
Jean Delvare
http://khali.linux-fr.org/
#2 lore raw
In reply to Jonas Munsin

Hi Jonas,

I finished reviewing your patch.

Quoted 3 lines

1. I wrote this reading the it8705f datasheet. I got the impression the other it87* chips work in the same way, but I haven't actually checked this.

I checked your code against the IT8712F datasheet and it seems to match.

Quoted 4 lines

2. The fan controllers can be in three different states, changes in settings effecting other states than the current one are rejected. An alternative would be to save them and write them to the it87 after a state change.

Yes, I think you save them instead. The way it is in your patch, the user has to enable a mode first, and configure it after that. The natural order is obviously to program the mode first and enable it after that.

Quoted 3 lines

3. There are a lot of tunable settings files... The sysfs directory becomes quite cluttered. And the it87.c contains a lot of adminstrative overhead to set up all those files.

Some files could (and should) be removed. fanN_enable and fanN_auto_enable are not needed.

fanN_enable is ambiguous, it could either mean "enable fanN monitoring" or "enable fanN". We'll have 2.6 driver which can individually disable temperature or voltage channels, through files named tempN_enable and inN_enable. Your proposal for "fanN_enable" doesn't match this plan.

Fortunately, you don't really need this file. fanN_enable = 1 is
actually fanN_pwm_enable = 0, and fanN_enable = 0 is fanN_pwm_enable = 1
and fanN_pwm = 0. So the interface as defined now is already OK. You are
free to physically disable PWM mode when the user writes 0 to fanN_pwm 0 (much like audio mixers have hardware mute).

fanN_auto_enable = 0 can be replaced by fanN_auto_temp_channel = 0 as well.

I have some proposals to rework the proposed interface. I'll do this is a different post.

About your patch now. I'll start with general comments about choices you made when I don't agree.

1* You don't cache auto fan speed register values. They are physically accessed on every read. This is not correct, since any user can read these files. This opens the door to attacks (continuously reading from the chip may prevent it to trigger alarms, start fans etc...). Please treat these registers exactly like any other. They must be read in the device update function, and stored in the private data structure.

2* I'm skeptical about your it87_pwm_mode function. Wherever you call it, you never really need to know the mode. Instead, you want to know if the mode is (or is not) one given mode. Your approach is quite expensive IMHO. What about defining the following macros instead:

#define pwm_is_off(data,nr) (!((data)->fan_main_ctrl & (1 << (nr))))

#define pwm_is_software(data,nr) (((data)->fan_main_ctrl & (1 << (nr))) \
                                  && !(data)->pwm_mode[nr])
#define pwm_is_automatic(data,nr) (((data)->fan_main_ctrl & (1 << (nr))) \
                                   && (data)->pwm_mode[nr])

I think it would be more efficient (both in terms of code size and speed).

3* Why do you force PWM output to active high? People which have their fans physically set up for active low won't be able to use the driver. The BIOS is supposed to have configured this correctly, there's no need to change that. If you're worried about the fact that resetting the chip will lose this information, well, too bad. Resetting the chip is a bad idea and should never be done IMHO. What you may consider is preserving that configuration bit on reset - but heh it's no more a reset if we start preserving stuff ;) Or you may allow the user to chose a polarity through the init parameter value (or another one). But do *not* arbitrarily change that configuration bit by default.

A few technical comments about your actual code now. You are free to decide when I'm right and whan I'm not.

+static ssize_t show_fan_pwm_enable (struct device *dev, char *buf, int nr) +{
+ struct it87_data *data = it87_update_device(dev);
+ return sprintf(buf, "%d\n", (data->fan_main_ctrl & (1 << nr)) != 0);
+}

Can be written: (data->fan_main_ctrl >> nr) & 1, which should be more efficient.

+static ssize_t show_fan_enable (struct device *dev, char *buf, int nr) +{
+ struct it87_data *data = it87_update_device(dev);
+ return sprintf(buf, "%d\n", (data->fan_ctl & (1 << nr)) != 0);
+}

Ditto.

+static ssize_t show_fan_auto_temp_channel (struct device *dev, char *buf, int nr) +{
+ struct it87_data *data = it87_update_device(dev);
+ return sprintf(buf, "%d\n", data->pwm_tmpin[nr] ? (data->pwm_tmpin[nr] << 1) : 1 );
+}

No extra space before closing parenthesis please.

Why do you default to 1? 0 would seem more sensible.

+static ssize_t set_fan_pwm(struct device *dev, const char *buf,
+ size_t count, int nr)
+{
+ struct i2c_client *client = to_i2c_client(dev);
+ struct it87_data *data = i2c_get_clientdata(client);
+ int val = simple_strtol(buf, NULL, 10);
+
+ if (it87_pwm_mode(data, nr) != PWM_SOFTWARE)
+ return -1;
+ if (val < 0 || val > 255)
+ return -1;
+ data->pwm_value[nr] = PWM_TO_REG(val);
+ it87_write_value(client, IT87_REG_PWM(nr), data->pwm_value[nr]);
+ return count;
+}

Return -EINVAL on error, not just -1. Same for all other "set_fan" functions you introduced.

+static ssize_t set_fan_auto_pwm_max (struct device *dev, const char *buf,
+ size_t count, int nr)
+{
+ int val = simple_strtol(buf, NULL, 10);
+
+ if (val != 255)
+ return -1;
+ return count;
+}

Mwaaahaha. You're kidding, right?

If that value cannot be changed, then make the file read-only.

+ /* Check poloarity of FAN_CTL3-1 registers (which are low active after a reset) */

s/poloarity/polarity/
s/low active/active low/

Your patch doesn't look bad all in all, except the few design issues I mentioned above.

Sorry for the delay again.

Signature · 2 lines
Jean Delvare
http://khali.linux-fr.org/
#3 lore raw
In reply to Jonas Munsin
Quoted 1 line · Jean Delvare

Sorry for the delay.

No problem. Got a bit delayed myself with this reply.

Quoted 1 line

About your code now. I only looked at the cleanup patch for now, sorry.

[...]

Quoted 2 lines

You could fix IN_FROM_REG(val) too, while you're at it. It misses a +5 for correct rounding. It's fixed in 2.4, but was forgotten in 2.6.

Hmm. Are you sure? I'm a bit confused about the rounding issue here. I think IN_TO_REG(val) should be rounded, but not IN_FROM_REG(val). (I'm looking at the 2.6 version). Just like in the TEMP_TO_REG(val)/TEMP_FROM_REG(val) case (btw, there was a bug in my cleanup patch with TEMP_TO_REG and rounding, fixed it).

Attached is it87_cleanups_2.6.7-rc2-jm2.patch, same as the earlier one except for these rounding macros. I hope I got them right ;).

-------------- next part --------------

i2c/chips/it87.c renamed from i2c/chips/it87.c.orig.2.6.7-rc2 +20 -24
@@ -128,7 +128,7 @@
 
 #define IT87_REG_FAN(nr)       (0x0d + (nr))
 #define IT87_REG_FAN_MIN(nr)   (0x10 + (nr))
-#define IT87_REG_FAN_CTRL      0x13
+#define IT87_REG_FAN_MAIN_CTRL 0x13
 
 #define IT87_REG_VIN(nr)       (0x20 + (nr))
 #define IT87_REG_TEMP(nr)      (0x29 + (nr))
@@ -145,8 +145,8 @@
 
 #define IT87_REG_CHIPID        0x58
 
-#define IN_TO_REG(val)  (SENSORS_LIMIT((((val) * 10 + 8)/16),0,255))
-#define IN_FROM_REG(val) (((val) *  16) / 10)
+#define IN_TO_REG(val)  (SENSORS_LIMIT((((val) + 8)/16),0,255))
+#define IN_FROM_REG(val) ((val) * 16)
 
 static inline u8 FAN_TO_REG(long rpm, int div)
 {
@@ -159,9 +159,9 @@
 
 #define FAN_FROM_REG(val,div) ((val)=0?-1:(val)=255?0:1350000/((val)*(div)))
 
-#define TEMP_TO_REG(val) (SENSORS_LIMIT(((val)<0?(((val)-5)/10):\
-					((val)+5)/10),0,255))
-#define TEMP_FROM_REG(val) (((val)>0x80?(val)-0x100:(val))*10)
+#define TEMP_TO_REG(val) (SENSORS_LIMIT(((val)<0?(((val)-500)/1000):\
+					((val)+500)/1000),0,255))
+#define TEMP_FROM_REG(val) (((val)>0x80?(val)-0x100:(val))*1000)
 
 #define VID_FROM_REG(val) ((val)=0x1f?0:(val)>=0x10?510-(val)*10:\
 				205-(val)*5)
@@ -231,19 +231,19 @@
 static ssize_t show_in(struct device *dev, char *buf, int nr)
 {
 	struct it87_data *data = it87_update_device(dev);
-	return sprintf(buf, "%d\n", IN_FROM_REG(data->in[nr])*10 );
+	return sprintf(buf, "%d\n", IN_FROM_REG(data->in[nr]));
 }
 
 static ssize_t show_in_min(struct device *dev, char *buf, int nr)
 {
 	struct it87_data *data = it87_update_device(dev);
-	return sprintf(buf, "%d\n", IN_FROM_REG(data->in_min[nr])*10 );
+	return sprintf(buf, "%d\n", IN_FROM_REG(data->in_min[nr]));
 }
 
 static ssize_t show_in_max(struct device *dev, char *buf, int nr)
 {
 	struct it87_data *data = it87_update_device(dev);
-	return sprintf(buf, "%d\n", IN_FROM_REG(data->in_max[nr])*10 );
+	return sprintf(buf, "%d\n", IN_FROM_REG(data->in_max[nr]));
 }
 
 static ssize_t set_in_min(struct device *dev, const char *buf, 
@@ -251,7 +251,7 @@
 {
 	struct i2c_client *client = to_i2c_client(dev);
 	struct it87_data *data = i2c_get_clientdata(client);
-	unsigned long val = simple_strtoul(buf, NULL, 10)/10;
+	unsigned long val = simple_strtoul(buf, NULL, 10);
 	data->in_min[nr] = IN_TO_REG(val);
 	it87_write_value(client, IT87_REG_VIN_MIN(nr), 
 			data->in_min[nr]);
@@ -262,7 +262,7 @@
 {
 	struct i2c_client *client = to_i2c_client(dev);
 	struct it87_data *data = i2c_get_clientdata(client);
-	unsigned long val = simple_strtoul(buf, NULL, 10)/10;
+	unsigned long val = simple_strtoul(buf, NULL, 10);
 	data->in_max[nr] = IN_TO_REG(val);
 	it87_write_value(client, IT87_REG_VIN_MAX(nr), 
 			data->in_max[nr]);
@@ -325,24 +325,24 @@
 static ssize_t show_temp(struct device *dev, char *buf, int nr)
 {
 	struct it87_data *data = it87_update_device(dev);
-	return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp[nr])*100 );
+	return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp[nr]));
 }
 static ssize_t show_temp_max(struct device *dev, char *buf, int nr)
 {
 	struct it87_data *data = it87_update_device(dev);
-	return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_high[nr])*100);
+	return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_high[nr]));
 }
 static ssize_t show_temp_min(struct device *dev, char *buf, int nr)
 {
 	struct it87_data *data = it87_update_device(dev);
-	return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_low[nr])*100);
+	return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_low[nr]));
 }
 static ssize_t set_temp_max(struct device *dev, const char *buf, 
 		size_t count, int nr)
 {
 	struct i2c_client *client = to_i2c_client(dev);
 	struct it87_data *data = i2c_get_clientdata(client);
-	int val = simple_strtol(buf, NULL, 10)/100;
+	int val = simple_strtol(buf, NULL, 10);
 	data->temp_high[nr] = TEMP_TO_REG(val);
 	it87_write_value(client, IT87_REG_TEMP_HIGH(nr), data->temp_high[nr]);
 	return count;
@@ -352,7 +352,7 @@
 {
 	struct i2c_client *client = to_i2c_client(dev);
 	struct it87_data *data = i2c_get_clientdata(client);
-	int val = simple_strtol(buf, NULL, 10)/100;
+	int val = simple_strtol(buf, NULL, 10);
 	data->temp_low[nr] = TEMP_TO_REG(val);
 	it87_write_value(client, IT87_REG_TEMP_LOW(nr), data->temp_low[nr]);
 	return count;
@@ -773,9 +773,7 @@
    We don't want to lock the whole ISA bus, so we lock each client
    separately.
    We ignore the IT87 BUSY flag at this moment - it could lead to deadlocks,
-   would slow down the IT87 access and should not be necessary. 
-   There are some ugly typecasts here, but the good new is - they should
-   nowhere else be necessary! */
+   would slow down the IT87 access and should not be necessary. */
 static int it87_read_value(struct i2c_client *client, u8 reg)
 {
 	struct it87_data *data = i2c_get_clientdata(client);
@@ -795,9 +793,7 @@
    We don't want to lock the whole ISA bus, so we lock each client
    separately.
    We ignore the IT87 BUSY flag at this moment - it could lead to deadlocks,
-   would slow down the IT87 access and should not be necessary. 
-   There are some ugly typecasts here, but the good new is - they should
-   nowhere else be necessary! */
+   would slow down the IT87 access and should not be necessary. */
 static int it87_write_value(struct i2c_client *client, u8 reg, u8 value)
 {
 	struct it87_data *data = i2c_get_clientdata(client);
@@ -840,11 +836,11 @@
 	}
 
 	/* Check if tachometers are reset manually or by some reason */
-	tmp = it87_read_value(client, IT87_REG_FAN_CTRL);
+	tmp = it87_read_value(client, IT87_REG_FAN_MAIN_CTRL);
 	if ((tmp & 0x70) = 0) {
 		/* Enable all fan tachometers */
 		tmp = (tmp & 0x8f) | 0x70;
-		it87_write_value(client, IT87_REG_FAN_CTRL, tmp);
+		it87_write_value(client, IT87_REG_FAN_MAIN_CTRL, tmp);
 	}
 
 	/* Start monitoring */
#4 lore raw
In reply to Jonas Munsin
Quoted 9 lines
Quoted 3 lines

You could fix IN_FROM_REG(val) too, while you're at it. It misses a +5 for correct rounding. It's fixed in 2.4, but was forgotten in 2.6.

Hmm. Are you sure? I'm a bit confused about the rounding issue here. I think IN_TO_REG(val) should be rounded, but not IN_FROM_REG(val). (I'm looking at the 2.6 version). Just like in the TEMP_TO_REG(val)/TEMP_FROM_REG(val) case (btw, there was a bug in my cleanup patch with TEMP_TO_REG and rounding, fixed it).

It's not a matter of where the value come from, it's a matter of whether the conversion includes a division or not. Whenever you divide, you have to take care of proper rounding yourself because integer arithmetics won't.

IN_FROM_REG(val) was including a division by 10 so far, thus my comment about missing rounding. But since you also fixed this converter so as to prevent unneeded accuracy loss, no division is used anymore, so there is no rounding issue to be discussed.

Nice catching for TEMP_TO_REG, I completely missed that one. BTW, now that I took a longer look at it, I really wonder how a negative temperature limit could ever been set with this. Looks like it'll end up with 0. OTOH, asking for a temperature higher than 127.5 degrees will most likely lead to "random" negative values. This is probably true for both the 2.4 and the 2.6 driver as of now. Could you please test and confirm? Most likely nobody cares about setting negative limits, but while we're at cleaning up this part of the driver...

Quoted 2 lines

Attached is it87_cleanups_2.6.7-rc2-jm2.patch, same as the earlier one except for these rounding macros. I hope I got them right ;).

Yes, looks very good to me (except for my comment above about setting negative temperature limits).

Thanks.

Signature · 2 lines
Jean Delvare
http://khali.linux-fr.org/
#5 lore raw
In reply to Jonas Munsin
Quoted 8 lines · Jean Delvare

Nice catching for TEMP_TO_REG, I completely missed that one. BTW, now that I took a longer look at it, I really wonder how a negative temperature limit could ever been set with this. Looks like it'll end up with 0. OTOH, asking for a temperature higher than 127.5 degrees will most likely lead to "random" negative values. This is probably true for both the 2.4 and the 2.6 driver as of now. Could you please test and confirm? Most likely nobody cares about setting negative limits, but while we're at cleaning up this part of the driver...

Yep, testing (on 2.6) confirms this, fixed it.

Btw, "grep SENSORS_LIMIT drivers/i2c/chips/*.c|grep 127" shows that lm85.c has a rounding error in it's TEMP_TO_REG() macro (and probably in at least one other place).

Quoted 2 lines

Yes, looks very good to me (except for my comment above about setting negative temperature limits).

Ok, I'll send it to Greg KH then.

#6 lore raw
In reply to Jonas Munsin
Quoted 1 line · Jean Delvare

Care to provide a similar fix for 2.4?

As mentioned on IRC, I think I will stay away from 2.4.

Quoted 8 lines
Quoted 3 lines

Btw, "grep SENSORS_LIMIT drivers/i2c/chips/*.c|grep 127" shows that lm85.c has a rounding error in it's TEMP_TO_REG() macro (and probably in at least one other place).

Correct. BTW, the same grep shows that rounding is probably missing in EXTTEMP_TO_REG and OFFSET_TO_REG of the same driver. Feel free to submit a patch to Greg that fixes this. If a similar fix is required for 2.4 (and most likely it is), a patch will of course be welcome as well.

How's this?

i2c/chips/lm85.c renamed from i2c/chips/lm85.c.orig.2.6.7-rc2 +6 -3
@@ -153,10 +153,12 @@
 #define FAN_FROM_REG(val)	((val)=0?-1:(val)=0xffff?0:5400000/(val))
 
 /* Temperature is reported in .001 degC increments */
-#define TEMP_TO_REG(val)		(SENSORS_LIMIT(((val)+500)/1000,-127,127))
+#define TEMP_TO_REG(val)		(SENSORS_LIMIT(((val)<0?(((val)-500)/1000):\
+					((val)+500)/1000),-127,127))
 #define TEMPEXT_FROM_REG(val,ext)	((val)*1000 + (ext)*250)
 #define TEMP_FROM_REG(val)		(TEMPEXT_FROM_REG(val,0))
-#define EXTTEMP_TO_REG(val)		(SENSORS_LIMIT((val)/250,-127,127))
+#define EXTTEMP_TO_REG(val)		(SENSORS_LIMIT(((val)<0?(((val)-125)/250):\
+					((val)+125)/250),-127,127))
 
 #define PWM_TO_REG(val)			(SENSORS_LIMIT(val,0,255))
 #define PWM_FROM_REG(val)		(val)
@@ -290,7 +292,8 @@
 #define HYST_TO_REG(val) (SENSORS_LIMIT((-(val)+5)/10,0,15))
 #define HYST_FROM_REG(val) (-(val)*10)
 
-#define OFFSET_TO_REG(val) (SENSORS_LIMIT((val)/25,-127,127))
+#define OFFSET_TO_REG(val) (SENSORS_LIMIT(((val)<0?(((val)-12)/25):\
+					((val)+13)/25),-127,127))
 #define OFFSET_FROM_REG(val) ((val)*25)
 
 #define PPR_MASK(fan) (0x03<<(fan *2))
#7 lore raw
In reply to Jonas Munsin
Quoted 11 lines
Quoted 9 lines

Nice catching for TEMP_TO_REG, I completely missed that one. BTW, now that I took a longer look at it, I really wonder how a negative temperature limit could ever been set with this. Looks like it'll end up with 0. OTOH, asking for a temperature higher than 127.5 degrees will most likely lead to "random" negative values. This is probably true for both the 2.4 and the 2.6 driver as of now. Could you please test and confirm? Most likely nobody cares about setting negative limits, but while we're at cleaning up this part of the driver...

Yep, testing (on 2.6) confirms this, fixed it.

Care to provide a similar fix for 2.4?

Quoted 3 lines

Btw, "grep SENSORS_LIMIT drivers/i2c/chips/*.c|grep 127" shows that lm85.c has a rounding error in it's TEMP_TO_REG() macro (and probably in at least one other place).

Correct. BTW, the same grep shows that rounding is probably missing in EXTTEMP_TO_REG and OFFSET_TO_REG of the same driver. Feel free to submit a patch to Greg that fixes this. If a similar fix is required for 2.4 (and most likely it is), a patch will of course be welcome as well.

Thanks.

Signature · 2 lines
Jean Delvare
http://khali.linux-fr.org/
#8 lore raw
In reply to Jonas Munsin
Quoted 3 lines
Quoted 1 line

Care to provide a similar fix for 2.4?

As mentioned on IRC, I think I will stay away from 2.4.

OK, I'll try to take care of this then...

i2c/chips/lm85.c line 157

Quoted 11 lines

How's this?

i2c/chips/lm85.c renamed from i2c/chips/lm85.c.orig.2.6.7-rc2 +2 -1
@@ -153,10 +153,12 @@
 #define FAN_FROM_REG(val)	((val)=0?-1:(val)=0xffff?0:5400000/(val))
 
 /* Temperature is reported in .001 degC increments */
-#define TEMP_TO_REG(val)		(SENSORS_LIMIT(((val)+500)/1000,-127,127))
+#define TEMP_TO_REG(val)		(SENSORS_LIMIT(((val)<0?(((val)-500)/1000):\
+					((val)+500)/1000),-127,127))

That one is OK.

Quoted 9 lines
i2c/chips/lm85.c renamed from i2c/chips/lm85.c.orig.2.6.7-rc2 +2 -1
 #define TEMPEXT_FROM_REG(val,ext)	((val)*1000 + (ext)*250)
 #define TEMP_FROM_REG(val)		(TEMPEXT_FROM_REG(val,0))
-#define EXTTEMP_TO_REG(val)		(SENSORS_LIMIT((val)/250,-127,127))
+#define EXTTEMP_TO_REG(val)		(SENSORS_LIMIT(((val)<0?(((val)-125)/250):\
+					((val)+125)/250),-127,127))
 
 #define PWM_TO_REG(val)			(SENSORS_LIMIT(val,0,255))
 #define PWM_FROM_REG(val)		(val)

i2c/chips/lm85.c line 299

Quoted 9 lines
i2c/chips/lm85.c renamed from i2c/chips/lm85.c.orig.2.6.7-rc2 +2 -1
@@ -290,7 +292,8 @@
 #define HYST_TO_REG(val) (SENSORS_LIMIT((-(val)+5)/10,0,15))
 #define HYST_FROM_REG(val) (-(val)*10)
 
-#define OFFSET_TO_REG(val) (SENSORS_LIMIT((val)/25,-127,127))
+#define OFFSET_TO_REG(val) (SENSORS_LIMIT(((val)<0?(((val)-12)/25):\
+					((val)+13)/25),-127,127))
 #define OFFSET_FROM_REG(val) ((val)*25)
 
 #define PPR_MASK(fan) (0x03<<(fan *2))

But I really wonder about these ones. the /250 makes me guess that the temperature has a 0.25 degree resolution. So, limiting to -127,+127 means -31 to +31 degrees. It hardly makes sense. Same probably goes for the /25. Could you please check with the datasheet and code and confirm whether these -127,+127 limits make sense?

Thanks.

Signature · 2 lines
Jean Delvare
http://khali.linux-fr.org/
#9 lore raw
In reply to Jonas Munsin

Quoting myself:

Quoted 5 lines

But I really wonder about these ones. the /250 makes me guess that the temperature has a 0.25 degree resolution. So, limiting to -127,+127 means -31 to +31 degrees. It hardly makes sense. Same probably goes for the /25. Could you please check with the datasheet and code and confirm whether these -127,+127 limits make sense?

I just checked:

EXTTEMP_TO_REG is used on -32,+32 0.25 degree/bit registers, so it's OK (except that the low limit could be -128 instead of -127, but it doesn't really matter).

OFFSET_TO_REG is not used anywhere (neither is OFFSET_FROM_REG) so you can discard them completely.

Please update your patch and submit to Greg. I'll backport to 2.4 then.

Thanks.

Signature · 2 lines
Jean Delvare
http://khali.linux-fr.org/