Skip to contentSkip to conversation

Add a driver for the ADT7475 thermal sensor (resend 3)

lm-sensors Jordan Crouse 3 participants 5 messages Sep 26, 2008 – Sep 29, 2008

Subsystems (3)
lore
Share & tools
lore
#0 lore raw
Quoted 53 lines · Hans de Goede
Quoted 17 lines · Jean Delvare

Hi Hans, Jordan,

Quoted 14 lines · Hans de Goede
Quoted 11 lines · Jordan Crouse

Okay - I have generated a new patch. I implemented all of your suggestions - the only one I had any concerns about was the hystersis (hystersis makes more sense to me as an offset rather then an absolute), but consistancy among hwmon drivers is rather more important.

There is one possible issue in the patch - I pulled the decimal point from the pwmX_freq numbers since I wasn't sure if we wanted to express the number in milihertz. If we do, then it is an easy fix.

Compile tested and run on an ADT7475 platform.

Hi Jordan et all,

Quoted 8 lines
Quoted 5 lines

I've given this a quick review (not as thorough as I would have liked to do but I simply don't have enough time for a really thorough review) and I've found no issues. So this patch is now:

Am I the only one seeing these warnings at build time?

No as said I didn't have all that much time, so I just reviewed it I didn't actually try to build it (I assumed Jordan alreayd build it).

Quoted 4 lines

CC [M] drivers/hwmon/adt7475.o
drivers/hwmon/adt7475.c: In function ‘adt7475_update_device’:
drivers/hwmon/adt7475.c:1043: warning: array subscript is above array bounds

<snip>

Quoted 5 lines

Apparently struct adt7475_data should be changed from

	s16 temp[6][3];
to
	s16 temp[7][3];

Looking closer at the code: you are completely right

Also while looking closer:

This snippet in adt7475_update_device() :

         data->temp[HYSTERSIS][0] = (u16) adt7475_read(REG_REMOTE1_HYSTERSIS);
         data->temp[HYSTERSIS][1] = (u16) adt7475_read(REG_REMOTE1_HYSTERSIS);
         data->temp[HYSTERSIS][2] = (u16) adt7475_read(REG_REMOTE2_HYSTERSIS);
Quoted 6 lines

Should be rewritten to:

         data->temp[HYSTERSIS][0] = (u16) adt7475_read(REG_REMOTE1_HYSTERSIS);
         data->temp[HYSTERSIS][1] = data->temp[HYSTERSIS][0];
         data->temp[HYSTERSIS][2] = (u16) adt7475_read(REG_REMOTE2_HYSTERSIS);

To avoid 1 (slow) unnecessary i2c transaction.

New patch attached that addreses these concerns, thanks.

Jordan

Signature · 3 lines
Jordan Crouse
Systems Software Development Engineer 
Advanced Micro Devices, Inc.

[PATCH] hwmon: Add a driver for the ADT7475 thermal sensor

From: Jordan Crouse <[email protected]>

HWMON driver for the ADT7475 thermal sensor.

---

 Documentation/hwmon/adt7475 |   90 +++
 drivers/hwmon/Kconfig       |   10 
 drivers/hwmon/Makefile      |    2 
 drivers/hwmon/adt7475.c     | 1140 +++++++++++++++++++++++++++++++++++++++++++
 4 files changed, 1242 insertions(+), 0 deletions(-)
Documentation/hwmon/adt7475 new file +90 -0
new file mode 100644
index 0000000..ec760c8
@@ -0,0 +1,90 @@
+This describes the interface for the ADT7475 driver:
+
+(there are 4 fans, numbered fan1 to fan4):
+
+fanX_input              Read the current speed of the fan (in RPMs)
+fanX_min                Read/write the minimum speed of the fan.  Dropping
+	   		below this sets an alarm.
+
+(there are three PWMs, numbered pwm1 to pwm3):
+
+pwmX                    Read/write the current duty cycle of the PWM.  Writes
+     			only have effect when auto mode is turned off (see
+			below).  Range is 0 - 255.
+
+pwmX_enable             Fan speed control method:
+
+		        0 - No control (fan at full speed)
+			1 - Manual fan speed control (using pwm[1-*])
+			2 - Automatic fan speed control
+
+pwmX_auto_channels_temp	Select which channels affect this PWM
+			
+			1 - TEMP1 controls PWM
+			2 - TEMP2 controls PWM
+			4 - TEMP3 controls PWM
+			6 - TEMP2 and TEMP3 control PWM
+			7 - All three inputs control PWM
+
+pwmX_freq               Read/write the PWM frequency in Hz. The number
+			should be one of the following:
+
+		        11 Hz
+		        14 Hz
+		        22 Hz
+		        29 Hz
+		        35 Hz
+		        44 Hz
+		        58 Hz
+		        88 Hz
+
+pwmX_max                Read/write the maximum PWM duty cycle.  The PWM
+                        duty cycle will never exceed this.
+
+pwmX_min                Read/write the minimum PWM duty cycle in automatic mode
+
+(there are three temperature settings numbered temp1 to temp3):
+
+tempX_input             Read the current temperature.  The value is in milli
+			degrees of Celsius.
+
+tempX_max               Read/write the upper temperature limit - exceeding this
+	                will cause an alarm.
+
+tempX_min               Read/write the lower temperature limit - exceeding this
+                        will cause an alarm.
+
+tempX_offset            Read/write the temperature adjustment offset
+
+tempX_crit              Read/write the THERM limit for remote1.  Exceeding this
+                        causes the chip to force the processor off.
+
+tempX_auto_point1_temp 	Read/write the minimum temperature where the fans will
+                        turn on in automatic mode.
+
+tempX_auto_point2_temp  Read/write the maximum temperature over which the fans
+			will run in automatic mode.  tempX_auto_point1_temp
+			and tempX_auto_point2_temp together define the
+			range of automatic control.
+
+tempX_crit_hyst 	set the temperature value below crit where the
+                        fans will stay on - this helps drive the temperature
+                        low enough so it doesn't stay near the edge and
+                        cause THERM to keep tripping.
+
+tempX_alarm		Read a 1 if the max/min alarm is set
+tempX_crit_alarm	Read a 1 if the critical limit is exceeded
+tempX_fault		Read a 1 if either temp1 or temp3 diode has a fault
+
+(There are two voltage settings, in1 and in2):
+
+inX_input               Read the current voltage on VCC.  Value is in
+			millivolts.
+
+inX_min                 read/write the minimum voltage limit.
+			Dropping below this causes an alarm.
+
+inX_max   		read/write the maximum voltage limit.
+			Exceeding this causes an alarm.
+
+inX_alarm		Read a 1 if the max/min alarm is set.
drivers/hwmon/Kconfig +10 -0
index d402e8d..8a1baf5 100644
@@ -179,6 +179,16 @@ config SENSORS_ADT7473
 	  This driver can also be built as a module. If so, the module
 	  will be called adt7473.
 
+config SENSORS_ADT7475
+	tristate "Analog Devices ADT7475"
+	depends on I2C && EXPERIMENTAL
+	help
+	  If you say yes here you get support for the Analog Devices
+	  ADT7475 temperature monitoring chips.
+
+	  This driver can also be build as a module.  If so, the module
+	  will be called adt7475.
+
 config SENSORS_K8TEMP
 	tristate "AMD Athlon64/FX or Opteron temperature sensor"
 	depends on X86 && PCI && EXPERIMENTAL
drivers/hwmon/Makefile +2 -0
index 950134a..424094c 100644
@@ -27,6 +27,8 @@ obj-$(CONFIG_SENSORS_ADM9240)	+= adm9240.o
 obj-$(CONFIG_SENSORS_ADS7828)	+= ads7828.o
 obj-$(CONFIG_SENSORS_ADT7470)	+= adt7470.o
 obj-$(CONFIG_SENSORS_ADT7473)	+= adt7473.o
+obj-$(CONFIG_SENSORS_ADT7475)   += adt7475.o
+
 obj-$(CONFIG_SENSORS_APPLESMC)	+= applesmc.o
 obj-$(CONFIG_SENSORS_AMS)	+= ams/
 obj-$(CONFIG_SENSORS_ATXP1)	+= atxp1.o
drivers/hwmon/adt7475.c new file +1140 -0

1,140 lines not on this page · raw message

_______________________________________________
lm-sensors mailing list
[email protected]
http://lists.lm-sensors.org/mailman/listinfo/lm-sensors

#1 lore raw

Re: [lm-sensors] Add a driver for the ADT7475 thermal sensor

Hi Jordan,

This time I've done a more thorough review, here is a quite long list of remarks I would like you to take care of in one last revision, then this can go upstream (through Jean or Andrew, Jean ?).

1) As Andrew Morten already noted as comment to some other patch, its better to make complex macros like this one:
+#define TEMP2REG(val) ((val) <= -128000 ? -128 : \

+       (val) >= 127000 ? 127 : \
+       (val) < 0 ? ((val) - 500) / 1000 : \
+       ((val) + 500) / 1000)

static functions rather then macros all the ? constructions will lead to interesting code and if you make it a function the compiler will usually create significant smaller code

2) This ofcourse is wrong:

+       data->temp[HYSTERSIS][0] = (u16) adt7475_read(REG_REMOTE1_HYSTERSIS);
+       data->temp[HYSTERSIS][1] = data->temp[HYSTERSIS][0];
+       data->temp[HYSTERSIS][1] = (u16) adt7475_read(REG_REMOTE1_HYSTERSIS);
+       data->temp[HYSTERSIS][2] = (u16) adt7475_read(REG_REMOTE2_HYSTERSIS);
    The second line starting with "data->temp[HYSTERSIS][1]" should be removed.

3) This macro isn't used anywhere:
+#define RANGE 7

And you may want to split the macro's in 2 blocks with comments that the first ones are used as array indices for various arrays, and the second ones are for special cases (and must be higher then the max array index).

4) The order in which you read/write the low and high byte is different for read/write word:

+static u16 adt7475_read_word(struct i2c_client *client, int reg) +{
+ u16 val;
+
+ val = i2c_smbus_read_byte_data(client, reg);
+ val |= (i2c_smbus_read_byte_data(client, reg + 1) << 8);
+
+ return val;
+}
+
+static void adt7475_write_word(struct i2c_client *client, int reg, u16 val) +{
+ i2c_smbus_write_byte_data(client, reg + 1, val >> 8);
+ i2c_smbus_write_byte_data(client, reg, val & 0xFF);
+}

The datasheet mandates the order as done in read, and says nothing about write, so I would like to suggest to use the read order for write too.

5) Your find_nearest function is wrong:

+static int find_nearest(int val, int *array, int size) +{
+ int i;
+
+ if (val < array[0])
+ return 0;
+
+ if (val > array[size - 1])
+ return array[size - 1];
+
+ for (i = 0; i < size - 1; i++) {
+ int a, b;
+
+ if (val > array[i + 1])
+ return array[size - 1];
+
+ for (i = 0; i < size - 1; i++) {
+ int a, b;
+
+ if (val > array[i + 1])
+ continue;
+
+ a = val - array[i];
+ b = array[i + 1] - val;
+
+ return (a >= b) ? i : i + 1;
+ }
+
+ return 0;
+}

Now lets assume that this gets called with an array which contains 1, 5 and 10 and that val = 7, then when i = 1 we get beyond the if "(val > array[i + 1])" check, then a becomes 7 - 5 = 2 and b becomes 10 - 7 = 3, since ! (2 >= 3) it will return i + 1 = 2, which corresponds to 10, but it should have returned 1 as 7 is closer to 5 then to 10, if we make val 8 it will return 1 where it should have returned 2 this time, so you need the reverse the a >= b check.

6) In quite a few functions you are addressing arrays out of bounds:
+static ssize_t show_voltage(struct device *dev, struct device_attribute *attr,
+ char *buf)
+{

+       struct adt7475_data *data = adt7475_update_device(dev);
+       struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
+       unsigned short val = data->voltage[sattr->nr][sattr->index];

+
+ switch (sattr->nr) {
+ case ALARM:

The problem here is that:
+ unsigned short val = data->voltage[sattr->nr][sattr->index];

Will get executed even if sattr->nr = ALARM (which it can be) and ALARM is 9 soe you are addressing a 3x3 array with [9][x], which is out of bounds. Now in this case this cannot hurt as you cannot get outside of you device data struct here, but you *really* shouldn't be relying on stuff like that, you should never, *ever* address an array out of bounds.

7) In many set functions you do not clamp the value gotten from the user before converting it, so during conversion it could wrap (multiple times) resulting in some almost random value getting set, example:

+static ssize_t set_voltage(struct device *dev, struct device_attribute *attr,
+ const char *buf, size_t count) {
+

+       struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
+       struct i2c_client *client = to_i2c_client(dev);
+       struct adt7475_data *data = i2c_get_clientdata(client);
+       long val = simple_strtol(buf, NULL, 10);
+       unsigned char reg;

+
+ mutex_lock(&data->lock);
+
+ data->voltage[sattr->nr][sattr->index] + sattr->index ? VCC2REG(val) : VCCP2REG(val);

Please clamp val here using SENSORS_LIMIT, note you must use SENSORS_LIMIT return value, example:
v = SENSORS_LIMIT(v, -128, 127);

Also in the few places where you do already clamp, please replace your own clamping code with SENSORS_LIMIT

8) Please check that you are using the same basic unit everywhere, for example with temp hysteresis in the show function you correcty do:
+ ret = sprintf(buf, "%d\n",
+ CONV2TEMP(data->temp[THERM][sattr->index])
+ - (out * 1000));

Where out is the register value.
But in the temp hysteresis store you do:
+ val = CONV2TEMP(data->temp[THERM][sattr->index]) - val;
+
+ if (sattr->index != 1) {
+ data->temp[HYSTERSIS][sattr->index] &= 0xF0;
+ data->temp[HYSTERSIS][sattr->index] |= (val & 0xF) << 4
+ } else {

So here the value written by the user has to be the current THERM setting in millidegrees minus the hysteresis he wants in normal celciuses so lets say THERM is 75 degrees so 75000, and he wants to set a hysteris of 5 degrees he should pass in 74995, which should of course be 70000.

And again you need to clamp here, and clamp intelligently given the substraction involved.

9) In some set_xxx functions you do not use update_device to get the data but instead write:
+ struct adt7475_data *data = i2c_get_clientdata(client);

1) Please be consistent about which one you use in set_xxx functions
2) if you use i2c_get_clientdata() you must make sure that update_device() has been called atleast once before registering any sysfs files, so that the set_xxxx functions can never work on the basis of uninitialised data

10) in show_temp() all reported values should be in millidegrees celcius, currently offset is not in millidegrees celcius

11) set_pwm_ctrl set_pwm_chan need better error checking. They should check the value they get passed is not negative, and set_pwm_chan should check for unsupported combos. I guess the best would be to do no checking and just pass the old chan and the new ctrl or vica versa to hw_set_pwm() and then return -EINVAL from hw_set_pwm on not valid combo's (and do storing of the values in the data struct also in hw_set_pwm when things are ok).

Regards,

Hans

_______________________________________________
lm-sensors mailing list
[email protected]
http://lists.lm-sensors.org/mailman/listinfo/lm-sensors

#2 lore raw

Re: [lm-sensors] Add a driver for the ADT7475 thermal sensor

In reply to Jordan Crouse

Hi Hand, Jordan,

Quoted 5 lines · Hans de Goede

Hi Jordan,

This time I've done a more thorough review, here is a quite long list of remarks I would like you to take care of in one last revision, then this can go upstream (through Jean or Andrew, Jean ?).

Thanks a lot for doing that, Hans. I wanted to do it myself during my trip to Portland, but in the end I could never find the time. I only took a couple notes on the driver.

When ready, this patch can go though either myself or Andrew, that doesn't really matter. Whatever works. I'm pretty busy myself these days, if you couldn't tell.

Review

Quoted 12 lines

1) As Andrew Morten already noted as comment to some other patch, its better to make complex macros like this one:

(quoted diff) +4 -0
+#define TEMP2REG(val) ((val) <= -128000 ? -128 : \
+       (val) >= 127000 ? 127 : \
+       (val) < 0 ? ((val) - 500) / 1000 : \
+       ((val) + 500) / 1000)
 
    static functions rather then macros all the ? constructions will lead to
    interesting code and if you make it a function the compiler will usually
    create significant smaller code

I totally second this.

Review

Quoted 7 lines

2) This ofcourse is wrong:

(quoted diff) +4 -0
+       data->temp[HYSTERSIS][0] = (u16) adt7475_read(REG_REMOTE1_HYSTERSIS);
+       data->temp[HYSTERSIS][1] = data->temp[HYSTERSIS][0];
+       data->temp[HYSTERSIS][1] = (u16) adt7475_read(REG_REMOTE1_HYSTERSIS);
+       data->temp[HYSTERSIS][2] = (u16) adt7475_read(REG_REMOTE2_HYSTERSIS);
 
    The second line starting with "data->temp[HYSTERSIS][1]" should be removed.

This is of course correct.

Quoted 9 lines

3) This macro isn't used anywhere:
+#define RANGE 7

And you may want to split the macro's in 2 blocks with comments that the first ones are used as array indices for various arrays, and the second ones are for special cases (and must be higher then the max array index).

And this isn't the only one. See this local patch I applied on top of Jordan's patch:

---
 drivers/hwmon/adt7475.c |   23 -----------------------
 1 file changed, 23 deletions(-)
drivers/hwmon/adt7475.c +0 -23
@@ -25,10 +25,8 @@
 #define CONTROL   3
 #define OFFSET    3
 #define AUTOMIN   4
-#define FREQ      4
 #define THERM     5
 #define HYSTERSIS 6
-#define RANGE     7
 #define ALARM     9
 #define CRIT_ALARM 10
 #define FAULT      11
@@ -45,13 +43,9 @@
 #define REG_DEVID               0x3D
 #define REG_VENDID              0x3E
 
-#define REG_CONFIG1             0x40
 #define REG_STATUS1             0x41
 #define REG_STATUS2             0x42
 
-/* Not all the alarm bits are enabled - mask the ones we don't use */
-#define ALARM_MASK              0xFE76
-
 #define REG_VOLTAGE_MIN_BASE    0x46
 #define REG_VOLTAGE_MAX_BASE    0x47
 
@@ -64,9 +58,6 @@
 
 #define REG_TEMP_TRANGE_BASE    0x5F
 
-#define REG_ACOUSTICS1          0x62
-#define REG_ACOUSTICS2          0x63
-
 #define REG_PWM_MIN_BASE        0x64
 
 #define REG_TEMP_TMIN_BASE      0x67
@@ -77,22 +68,13 @@
 
 #define REG_TEMP_OFFSET_BASE    0x70
 
-#define REG_CONFIG2             0x73
-#define REG_INTERRUPT_MASK1     0x74
-#define REG_INTERRUPT_MASK2     0x75
 #define REG_EXTEND1             0x76
 #define REG_EXTEND2             0x77
-#define REG_CONFIG3             0x78
-#define REG_THERM_TIMER_STATUS  0x79
-#define REG_THERM_TIMER_LIMIT   0x7A
-#define REG_TACH_PULSES         0x7B
 #define REG_CONFIG5             0x7C
 
 #define CONFIG5_TWOSCOMP        0x01
 #define CONFIG5_TEMPOFFSET      0x02
 
-#define REG_CONFIG4             0x7D
-
 /* ADT7475 Settings */
 
 #define ADT7475_VOLTAGE_COUNT   2
@@ -100,11 +82,6 @@
 #define ADT7475_TACH_COUNT	4
 #define ADT7475_PWM_COUNT       3
 
-/* 7475 specific registers */
-
-#define REG_CONFIG6             0x10
-#define REG_CONFIG7             0x11
-
 
 /* Macro to read the registers */
 

And the driver still builds! So, 23 unused defines total.

Quoted 11 lines

4) The order in which you read/write the low and high byte is different for read/write word:

(quoted diff) +8 -0
+static u16 adt7475_read_word(struct i2c_client *client, int reg)
+{
+       u16 val;
+
+       val = i2c_smbus_read_byte_data(client, reg);
+       val |= (i2c_smbus_read_byte_data(client, reg + 1) << 8);
+
+       return val;

Review

Quoted 10 lines
(quoted diff) +7 -0
+}
+
+static void adt7475_write_word(struct i2c_client *client, int reg, u16 val)
+{
+       i2c_smbus_write_byte_data(client, reg + 1, val >> 8);
+       i2c_smbus_write_byte_data(client, reg, val & 0xFF);
+}
 
    The datasheet mandates the order as done in read, and says nothing about
    write, so I would like to suggest to use the read order for write too.

I don't think we care about write order. Read order matters on registers which are continuously updated by the chip. Reading in the recommended order allows the chip to latch the register values to ensure consistent readings. But for writes, there is no such problem, so any order should do.

Quoted 54 lines

5) Your find_nearest function is wrong:

(quoted diff) +30 -0
+static int find_nearest(int val, int *array, int size)
+{
+       int i;
+
+       if (val < array[0])
+               return 0;
+
+       if (val > array[size - 1])
+               return array[size - 1];
+
+       for (i = 0; i < size - 1; i++) {
+               int a, b;
+
+               if (val > array[i + 1])
+               return array[size - 1];
+
+       for (i = 0; i < size - 1; i++) {
+               int a, b;
+
+               if (val > array[i + 1])
+                       continue;
+
+               a = val - array[i];
+               b = array[i + 1] - val;
+
+               return (a >= b) ? i : i + 1;
+       }
+
+       return 0;
+}

Now lets assume that this gets called with an array which contains 1, 5 and 10 and that val = 7, then when i = 1 we get beyond the if "(val > array[i + 1])" check, then a becomes 7 - 5 = 2 and b becomes 10 - 7 = 3, since ! (2 >= 3) it will return i + 1 = 2, which corresponds to 10, but it should have returned 1 as 7 is closer to 5 then to 10, if we make val 8 it will return 1 where it should have returned 2 this time, so you need the reverse the a >= b check.

6) In quite a few functions you are addressing arrays out of bounds:

(quoted diff) +9 -0
+static ssize_t show_voltage(struct device *dev, struct device_attribute *attr,
+                           char *buf)
+{
+       struct adt7475_data *data = adt7475_update_device(dev);
+       struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
+       unsigned short val = data->voltage[sattr->nr][sattr->index];
+
+       switch (sattr->nr) {
+       case ALARM:
Quoted 8 lines

The problem here is that:
+ unsigned short val = data->voltage[sattr->nr][sattr->index];

Will get executed even if sattr->nr = ALARM (which it can be) and ALARM is 9 soe you are addressing a 3x3 array with [9][x], which is out of bounds. Now in this case this cannot hurt as you cannot get outside of you device data struct here, but you *really* shouldn't be relying on stuff like that, you should never, *ever* address an array out of bounds.

Yes, I had noticed this problem too. This needs to be fixed everywhere.

Quoted 51 lines

7) In many set functions you do not clamp the value gotten from the user before converting it, so during conversion it could wrap (multiple times) resulting in some almost random value getting set, example:

(quoted diff) +12 -0
+static ssize_t set_voltage(struct device *dev, struct device_attribute *attr,
+                          const char *buf, size_t count) {
+
+       struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
+       struct i2c_client *client = to_i2c_client(dev);
+       struct adt7475_data *data = i2c_get_clientdata(client);
+       long val = simple_strtol(buf, NULL, 10);
+       unsigned char reg;
+
+       mutex_lock(&data->lock);
+
+       data->voltage[sattr->nr][sattr->index] > +               sattr->index ? VCC2REG(val) : VCCP2REG(val);

Please clamp val here using SENSORS_LIMIT, note you must use SENSORS_LIMIT return value, example:
v = SENSORS_LIMIT(v, -128, 127);

Also in the few places where you do already clamp, please replace your own clamping code with SENSORS_LIMIT

8) Please check that you are using the same basic unit everywhere, for example with temp hysteresis in the show function you correcty do:

(quoted diff) +3 -0
+               ret = sprintf(buf, "%d\n",
+                       CONV2TEMP(data->temp[THERM][sattr->index])
+                       - (out * 1000));

Where out is the register value.
But in the temp hysteresis store you do:

(quoted diff) +6 -0
+               val = CONV2TEMP(data->temp[THERM][sattr->index]) - val;
+
+               if (sattr->index != 1) {
+                       data->temp[HYSTERSIS][sattr->index] &= 0xF0;
+                       data->temp[HYSTERSIS][sattr->index] |= (val & 0xF) << 4
+               } else {

So here the value written by the user has to be the current THERM setting in millidegrees minus the hysteresis he wants in normal celciuses so lets say THERM is 75 degrees so 75000, and he wants to set a hysteris of 5 degrees he should pass in 74995, which should of course be 70000.

And again you need to clamp here, and clamp intelligently given the substraction involved.

Quoted 5 lines

9) In some set_xxx functions you do not use update_device to get the data but instead write:
+ struct adt7475_data *data = i2c_get_clientdata(client);

1) Please be consistent about which one you use in set_xxx functions

Set functions should never call the update_device function.

Quoted 3 lines

2) if you use i2c_get_clientdata() you must make sure that update_device() has been called atleast once before registering any sysfs files, so that the set_xxxx functions can never work on the basis of uninitialised data

This is discouraged as well (makes loading the driver slow.) The recommended practice is to initialize the few registers values that may be needed during initialization. There typically aren't that many so it doesn't duplicate too much code. But even then, relying on potentially old cached values is not so good, so the best thing "set" functions can do is read everything they need directly from the chip.

Quoted 6 lines

10) in show_temp() all reported values should be in millidegrees celcius, currently offset is not in millidegrees celcius

Quoted 7 lines

11) set_pwm_ctrl set_pwm_chan need better error checking. They should check the value they get passed is not negative, and set_pwm_chan should check for unsupported combos. I guess the best would be to do no checking and just pass the old chan and the new ctrl or vica versa to hw_set_pwm() and then return -EINVAL from hw_set_pwm on not valid combo's (and do storing of the values in the data struct also in hw_set_pwm when things are ok).

12)

+ /* Convert the tach reading into RPMs */
+
+ #define TACH2RPM(val) ((90000 * 60) / (val))
+ #define RPM2TACH(val) ((90000 * 60) / (val))

What happens if val = 0?

13)

+ #define OFF64_REG2TEMP(val) (((((val) >> 2) - 64) * 1000) + (((val) & 3) * 250))

Is it just me or is this strictly equivalent to the much simpler:

#define OFF64_REG2TEMP(val) (((val) - (64 << 2)) * 250)

(Which should be an inline function anyway - see 1))

Signature · 6 lines
Jean Delvare

_______________________________________________
lm-sensors mailing list
[email protected]
http://lists.lm-sensors.org/mailman/listinfo/lm-sensors
#3 lore raw

Re: [lm-sensors] Add a driver for the ADT7475 thermal sensor

In reply to Jordan Crouse
Quoted 5 lines · Hans de Goede

Hi Jordan,

This time I've done a more thorough review, here is a quite long list of remarks I would like you to take care of in one last revision, then this can go upstream (through Jean or Andrew, Jean ?).

Thanks for your time.

drivers/hwmon/adt7475.c

Quoted 10 lines

1) As Andrew Morten already noted as comment to some other patch, its better to make complex macros like this one:

drivers/hwmon/adt7475.c new file +4 -0
+#define TEMP2REG(val) ((val) <= -128000 ? -128 : \
+       (val) >= 127000 ? 127 : \
+       (val) < 0 ? ((val) - 500) / 1000 : \
+       ((val) + 500) / 1000)
 
    static functions rather then macros all the ? constructions will lead to
    interesting code and if you make it a function the compiler will usually
    create significant smaller code

I have turned all the macros into function. Am I right in saying that the current kernel policy is to not mark them as inline and let the compiler decide?

Quoted 3 lines

And you may want to split the macro's in 2 blocks with comments that the first ones are used as array indices for various arrays, and the second ones are for special cases (and must be higher then the max array index).

Yeah - the problem is that the indexes are serving double duty - their most important function is to be unique identifiers for the sysfs files - its just happy that they can also serve as indexes in the array. I have made the comments.

drivers/hwmon/adt7475.c

Quoted 8 lines
drivers/hwmon/adt7475.c new file +5 -0
+static void adt7475_write_word(struct i2c_client *client, int reg, u16 val)
+{
+       i2c_smbus_write_byte_data(client, reg + 1, val >> 8);
+       i2c_smbus_write_byte_data(client, reg, val & 0xFF);
+}
 
    The datasheet mandates the order as done in read, and says nothing about
    write, so I would like to suggest to use the read order for write too.

I'm following what Jean said here. Typically, it is safer to write the upper byte first, since that will at least write the upper level bits to the hardware.

Quoted 3 lines

Please clamp val here using SENSORS_LIMIT, note you must use SENSORS_LIMIT return value, example:
v = SENSORS_LIMIT(v, -128, 127);

I have used SENSORS_LIMIT everywhere it makes sense.

Quoted 11 lines

8) Please check that you are using the same basic unit everywhere, for example Where out is the register value.
But in the temp hysteresis store you do:

drivers/hwmon/adt7475.c new file +6 -0
+               val = CONV2TEMP(data->temp[THERM][sattr->index]) - val;
+
+               if (sattr->index != 1) {
+                       data->temp[HYSTERSIS][sattr->index] &= 0xF0;
+                       data->temp[HYSTERSIS][sattr->index] |= (val & 0xF) << 4
+               } else {
Quoted 4 lines

So here the value written by the user has to be the current THERM setting in millidegrees minus the hysteresis he wants in normal celciuses so lets say THERM is 75 degrees so 75000, and he wants to set a hysteris of 5 degrees he should pass in 74995, which should of course be 70000.

No - the hystersis number is expected to be 5000 to the user. However this function was wrong in that it wasn't converting the hystersis back to the value needed by the register.

Quoted 7 lines

11) set_pwm_ctrl set_pwm_chan need better error checking. They should check the value they get passed is not negative, and set_pwm_chan should check for unsupported combos. I guess the best would be to do no checking and just pass the old chan and the new ctrl or vica versa to hw_set_pwm() and then return -EINVAL from hw_set_pwm on not valid combo's (and do storing of the values in the data struct also in hw_set_pwm when things are ok).

I did the bounds checking in the individual functions - it just makes things easier then trying to make hw_set_pwm do bounds checking as well as calculating the value.

I'll send out a new driver at some point - this one changed enough that I'll have to put it through some heavy testing.

Jordan

Signature · 9 lines
Jordan Crouse
Systems Software Development Engineer 
Advanced Micro Devices, Inc.


_______________________________________________
lm-sensors mailing list
[email protected]
http://lists.lm-sensors.org/mailman/listinfo/lm-sensors
#4 lore raw

Re: [lm-sensors] Add a driver for the ADT7475 thermal sensor

In reply to Jordan Crouse

Hi Jordan,

Quoted 8 lines · Jordan Crouse
Quoted 7 lines · Hans de Goede

1) As Andrew Morten already noted as comment to some other patch, its better to make complex macros like this one:

drivers/hwmon/adt7475.c new file +4 -0
+#define TEMP2REG(val) ((val) <= -128000 ? -128 : \
+       (val) >= 127000 ? 127 : \
+       (val) < 0 ? ((val) - 500) / 1000 : \
+       ((val) + 500) / 1000)
Quoted 7 lines
Quoted 3 lines

static functions rather then macros all the ? constructions will lead to interesting code and if you make it a function the compiler will usually create significant smaller code

I have turned all the macros into function. Am I right in saying that the current kernel policy is to not mark them as inline and let the compiler decide?

I am happy to see inline keywords in the code. Different versions of gcc have different levels of smartness when it comes to auto-inlining. Some versions apparently have an algorithm as simple as "inline everything that's used only once", which doesn't work at all for our drivers. As long as you do not force the inlining of large functions, there's really no drawback to using the inline keyword.

Signature · 6 lines
Jean Delvare

_______________________________________________
lm-sensors mailing list
[email protected]
http://lists.lm-sensors.org/mailman/listinfo/lm-sensors