LM63: Add controls for configuring device
Hi Trent,
Redirecting to the correct mailing-list...
Quoted 2 lines · Trent Piepho
Add controls that configure settings normally done by the BIOS. Turned on with
CONFIG_SENSORS_LM63_SETUP
What system do you need this for?
Review
Quoted 7 lines
pwm1_polarity: 0 = low for fan off, 1 = low for fan on. Changing the(quoted diff) +0
-0polarity will adjust the pwm value to try to keep the fan at the same setting. E.g, if the fan is on and polarity is 0, changing polarity to 1 will keep the fan on by setting pwm1 to the opposite of what it was, as opposed to keeping pwm1 the same and causing the fan to turn off.
I am reluctant to accept this as a run-time option... It's a good thing that you preserve the fan speed, but still, one of the polarities is wrong, and wrong polarity doesn't bode well with automatic fan speed control, which the LM63 implements: you'll end up with either fan at full speed (bad) or stopped (worse). The polarity should really be set before the driver is loaded and never changed after that.
Quoted 3 lines
pwm1_enable: Add ability to set this control, which switches between
direct PWM control and temperature table look up.
That's pretty standard and should be included unconditionally. The only reason why it was missing so far is because automatic (temperature look-up) mode wasn't implemented yet.
Quoted 3 lines
fan1_enable: Enables/disables fan tach monitoring. Turning this on/off
will create/remove the other fan related sysfs files.
Again I am reluctant to make this a run-time selection, because you're not just enabling/disabling the tachometer function, you're also disabling/enabling the ALERT output which shares the same pin. Only one configuration is correct for the hardware setup, so it should be set once and for all before the driver is loaded.
Quoted 2 lines
conv_rate: Set the conversion rate, format is log2(frequency)
I fail to see the point of this one, given that the driver caches the data for 1 second anyway?
Quoted 3 lines
pwm1_auto_point[1-8]_temp and pwm_auto_point[1-8]_pwm: The lookup table
used for automatic fan PWM control based on temp2.
Yeah, good.
Review
Quoted 5 lines
pwm1_auto_hyst: The hysteresis to use in automatic fan PWM. When temp2 is(quoted diff) +0
-0greater than pwm_auto_pointN_temp, it will use pwm_auto_pointN_pwm. It will keep this until the temp gets pwm_auto_hyst degrees below pwm_auto_pointN_temp.
We avoid this kind of "general" file in sysfs because they don't follow the standard so applications aren't able to use them. The standard way to handle the case where a single register corresponds to multiple sysfs files is to create them all, and make only one of them writable. So in your case that would be:
pwm_auto_point1_temp_hyst: read/write
pwm_auto_point[2-8]_temp_hyst: read only
Review
Quoted 8 lines
temp2_offset: Offset applied to the temp2 reading, for adjusting to
different thermal diodes.
pwm1_freq: The frequency the PWM output operates at. It's set in Hz and(quoted diff) +0
-0supports ranges of 23..700 and 5806..180000. Setting the frequency will also adjust the internal pwm setting to try to keep the duty cycle the same.
These two are pretty standard as well by now, so I would be fine having them created unconditionally.
This basically leaves only two or three configuration bits (pwm1_polarity, fan1_enable and maybe conv_rate if you can explain) that would belong to CONFIG_SENSORS_LM63_SETUP. Quite frankly, I'd rather see you use i2cset to write them to the LM63 chip at boot time, rather than adding code to the lm63 driver, even as a separate option. You'll have to write some boot code either way. I am still commenting on these functions in your patch below if you want to improve the code for yourself, but I doubt it will ever make it upstream.
Quoted 7 lines
Signed-off-by Trent Piepho <[email protected]>--- drivers/hwmon/Kconfig | 15 ++ drivers/hwmon/lm63.c | 371 ++++++++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 381 insertions(+), 5 deletions(-)
Preliminary note: many lines over 80 columns in your patch, please fix.
Review:
Quoted 31 lines
drivers/hwmon/Kconfig +15
-0@@ -366,6 +366,21 @@ config SENSORS_LM63 This driver can also be built as a module. If so, the module will be called lm63. +config SENSORS_LM63_SETUP + boolean "Addition LM63 configuration controls" + depends on SENSORS_LM63 + default n + help + Adds additional controls to the LM63 driver to set things that + would normally be configured by the BIOS. + + One could use this if their BIOS was broken or othewise setting the + LM63 up in a sub-optimal manner. It's also useful for embedded + system with no BIOS, or for developers who are writing the BIOS. + + If the prospect of reading the LM63's data sheet is daunting to you, + then you should probably just leave this off. + config SENSORS_LM70 tristate "National Semiconductor LM70" depends on SPI_MASTER && EXPERIMENTALdrivers/hwmon/lm63.c +0
-0
drivers/hwmon/lm63.c I2C_CLIENT_INSMOD_1(lm63); · line 81
Quoted 5 lines
drivers/hwmon/lm63.c +2
-0@@ -77,6 +77,10 @@ I2C_CLIENT_INSMOD_1(lm63);#define LM63_REG_PWM_VALUE 0x4C#define LM63_REG_PWM_FREQ 0x4D +#define LM63_REG_PWM_AUTO_TEMP 0x50 +#define LM63_REG_PWM_AUTO_PWM 0x51
I'd rather define these as macros, as many other drivers do:
/* nr from 0 to 7 */
#define LM63_REG_PWM_AUTO_TEMP(nr) (0x50 + (nr) * 2)
#define LM63_REG_PWM_AUTO_PWM(nr) (0x51 + (nr) * 2)
Quoted 62 lines
drivers/hwmon/lm63.c +24
-3+#define LM63_REG_PWM_AUTO_HYST 0x4F +#define LM63_REG_LOCAL_TEMP 0x00#define LM63_REG_LOCAL_HIGH 0x05@@ -91,6 +95,8 @@ I2C_CLIENT_INSMOD_1(lm63);#define LM63_REG_REMOTE_TCRIT 0x19#define LM63_REG_REMOTE_TCRIT_HYST 0x21 +#define LM63_REG_CONV_RATE 0x04 +#define LM63_REG_ALERT_STATUS 0x02#define LM63_REG_ALERT_MASK 0x16@@ -123,6 +129,9 @@ I2C_CLIENT_INSMOD_1(lm63);#define HYST_TO_REG (val) ((val) <= 0 ? 0 : \ (val) >= 127000 ? 127 : \ ((val) + 500) / 1000) +#define HYST5_TO_REG (val) ((val) <= 0 ? 0 : \ + (val) >= 31000 ? 31 : \ + ((val) + 500) / 1000)/* * Functions declaration @@ -148,6 +157,11 @@ static struct i2c_driver lm63_driver = { .detach_client = lm63_detach_client, }; +#ifdef CONFIG_SENSORS_LM63_SETUP +#define NUM_TEMP11 4 +#else +#define NUM_TEMP11 3 +#endif /* * Client data (each client gets its own) */ @@ -168,9 +182,10 @@ struct lm63_data {s8 temp8[3 ];/* 0: local input 1: local high limit 2: remote critical limit */ -s16 temp11[3 ];/* 0: remote input -1: remote low limit -2: remote high limit */ +s16 temp11[NUM_TEMP11 ];/* 0: remote input +1: remote low limit +2: remote high limit +3: remote offset */ u8 temp2_crit_hyst;u8 alarms; };@@ -240,6 +255,272 @@ static ssize_t show_pwm1_enable(struct device *dev, struct device_attribute *dumreturn sprintf (buf,"%d\n" , data->config_fan &0x20 ?1 :2 ); } +#ifdef CONFIG_SENSORS_LM63_SETUP +static ssize_t set_pwm1_enable (struct device *dev,struct device_attribute *dummy, +const char *buf,size_t count) +{ +struct i2c_client *client =to_i2c_client (dev); +struct lm63_data *data =i2c_get_clientdata (client); +unsigned long val =simple_strtoul (buf,NULL ,0 ); +
drivers/hwmon/lm63.c static ssize_t show_pwm1_enable(struct device *dev, struct device_attribute *dum · line 275
Quoted 10 lines
drivers/hwmon/lm63.c +10
-0+mutex_lock (&data->update_lock); +switch (val) { +case 2 :/* Lookup Table mode */ + data->config_fan &= ~0x20 ; +break ; +case 1 :/* Direct mode */ + data->config_fan |=0x20 ; +break ; +default : +return -EINVAL ;
You're returning with a mutex held!
drivers/hwmon/lm63.c static ssize_t show_pwm1_enable(struct device *dev, struct device_attribute *dum · line 277
Quoted 2 lines
drivers/hwmon/lm63.c +2
-0+ } +i2c_smbus_write_byte_data (client,LM63_REG_CONFIG_FAN , data->config_fan);
You should not assume that data->config_fan was up-to-date. Do a proper read/modify/write cycle instead.
drivers/hwmon/lm63.c static ssize_t show_pwm1_enable(struct device *dev, struct device_attribute *dum · line 281
Quoted 4 lines
drivers/hwmon/lm63.c +4
-0+mutex_unlock (&data->update_lock); + +return count; +}
This function could change the permissions of the sysfs files that correspond to registers that go read/write or read-only. A few drivers are doing that already.
drivers/hwmon/lm63.c static ssize_t show_pwm1_enable(struct device *dev, struct device_attribute *dum · line 291
Quoted 10 lines
drivers/hwmon/lm63.c +10
-0+ +static ssize_t set_pwm1_polarity (struct device *dev,struct device_attribute *dummy, +const char *buf,size_t count) +{ +struct i2c_client *client =to_i2c_client (dev); +struct lm63_data *data =lm63_update_device (dev); +unsigned long val =simple_strtoul (buf,NULL ,0 ) ?0x10 :0 ; + +mutex_lock (&data->update_lock); +if ((data->config_fan &0x10 ) = val) {
Again you shouldn't assume that data->config_fan is up-to-date.
Inverting the condition would result in less duplicated code.
drivers/hwmon/lm63.c static ssize_t show_pwm1_enable(struct device *dev, struct device_attribute *dum · line 300
Quoted 9 lines
drivers/hwmon/lm63.c +9
-0+/* Already set to desired value */ +mutex_unlock (&data->update_lock); +return count; + } + + data->config_fan ^=0x10 ; + +/* Invert duty cycle, to keep fan at the same setting */ + data->pwm1_value =2 * data->pwm1_freq - data->pwm1_value;
Same for data->pwm1_value, it might not be up-to-date.
drivers/hwmon/lm63.c static ssize_t show_pwm1_enable(struct device *dev, struct device_attribute *dum · line 312
Quoted 12 lines
drivers/hwmon/lm63.c +12
-0+ +i2c_smbus_write_byte_data (client,LM63_REG_CONFIG_FAN , data->config_fan); +i2c_smbus_write_byte_data (client,LM63_REG_PWM_VALUE , data->pwm1_value); +mutex_unlock (&data->update_lock); + +return count; +} + +static ssize_t show_pwm1_polarity (struct device *dev, +struct device_attribute *dummy,char *buf) +{ +struct lm63_data *data =lm63_update_device (dev);
Why call lm63_update_device()? data->config_fan isn't updated by this function.
Quoted 15 lines
drivers/hwmon/lm63.c +15
-0+return sprintf (buf,"%d\n" , !!(data->config_fan &0x10 )); +} + +/* forward declaration */ +static const struct attribute_group lm63_group_fan1; + +static ssize_t set_fan1_enable (struct device *dev,struct device_attribute *dummy, +const char *buf,size_t count) +{ +struct i2c_client *client =to_i2c_client (dev); +struct lm63_data *data =i2c_get_clientdata (client); +unsigned long val =simple_strtoul (buf,NULL ,0 ); + +/* Remove the group if it's already there. In theory data->config +* will tell us if the the group is there or not, but this way is
drivers/hwmon/lm63.c static ssize_t show_pwm1_enable(struct device *dev, struct device_attribute *dum · line 337
Quoted 10 lines
drivers/hwmon/lm63.c +10
-0+* resistant to sysfs and the lm63 state getting out of sync +*/ +mutex_lock (&data->update_lock); +sysfs_remove_group (&client->dev.kobj, &lm63_group_fan1); +if (val) { +int err; + + err =sysfs_create_group (&client->dev.kobj, &lm63_group_fan1); +if (err <0 ) { +sysfs_remove_group (&client->dev.kobj, &lm63_group_fan1);
Useless: if sysfs_create_group() failed, there's nothing left to remove.
drivers/hwmon/lm63.c static ssize_t show_pwm1_enable(struct device *dev, struct device_attribute *dum · line 347
Quoted 10 lines
drivers/hwmon/lm63.c +10
-0+mutex_unlock (&data->update_lock); +return err; + } + + data->config |=0x04 ; + }else + data->config &= ~0x04 ; + +i2c_smbus_write_byte_data (client,LM63_REG_CONFIG1 , data->config); +mutex_unlock (&data->update_lock);
I don't like this... For one thing, removing files to create them right after is ugly. For another, libsensors scans sysfs for attributes at initialization time and doesn't expect attributes to be added or removed after that.
drivers/hwmon/lm63.c static ssize_t show_pwm1_enable(struct device *dev, struct device_attribute *dum · line 355
Quoted 8 lines
drivers/hwmon/lm63.c +8
-0+ +return count; +} + +static ssize_t show_fan1_enable (struct device *dev, +struct device_attribute *dummy,char *buf) +{ +struct lm63_data *data =lm63_update_device (dev);
Again there's no point in calling lm63_update_device(), unless you change it to read register LM63_REG_CONFIG_FAN and cache the result.
drivers/hwmon/lm63.c static ssize_t show_pwm1_enable(struct device *dev, struct device_attribute *dum · line 368
Quoted 13 lines
drivers/hwmon/lm63.c +13
-0+ +return sprintf (buf,"%d\n" , !!(data->config_fan &0x04 )); +} + +static ssize_t set_conv_rate (struct device *dev,struct device_attribute *dummy, +const char *buf,size_t count) +{ +struct i2c_client *client =to_i2c_client (dev); +long val =simple_strtol (buf,NULL ,0 ); + +/* Value is log2(rate) */ +if (val <-4 || val >5 ) +return -EINVAL ;
What a friendly unit ;)
drivers/hwmon/lm63.c static ssize_t show_pwm1_enable(struct device *dev, struct device_attribute *dum · line 378
Quoted 10 lines
drivers/hwmon/lm63.c +10
-0+ +i2c_smbus_write_byte_data (client,LM63_REG_CONV_RATE , val +4 ); +return count; +} + +static ssize_t show_conv_rate (struct device *dev, +struct device_attribute *dummy,char *buf) +{ +struct i2c_client *client =to_i2c_client (dev); +int data;
Better find another name. "data" is consistently used throughout the whole driver for something different.
drivers/hwmon/lm63.c static ssize_t show_pwm1_enable(struct device *dev, struct device_attribute *dum · line 380
Quoted 2 lines
drivers/hwmon/lm63.c +2
-0+ + data =i2c_smbus_read_byte_data (client,LM63_REG_CONV_RATE );
Because this isn't cached, you're giving users an easy way to saturate the SMBus. Either make the attribute readable by root only, or cache its value.
Quoted 4 lines
drivers/hwmon/lm63.c +4
-0+if (data >9 ) + data =9 ; +return sprintf (buf,"%d\n" , data -4 ); +}
drivers/hwmon/lm63.c static ssize_t show_pwm1_enable(struct device *dev, struct device_attribute *dum · line 394
Quoted 10 lines
drivers/hwmon/lm63.c +10
-0+ +static ssize_t set_pwm1_freq (struct device *dev,struct device_attribute *dummy, +const char *buf,size_t count) +{ +struct i2c_client *client =to_i2c_client (dev); +struct lm63_data *data =lm63_update_device (dev); +unsigned long val =simple_strtoul (buf,NULL ,0 ); +unsigned long freq; + +/* Only support the ranges [23, 700] and [5806, 180000] */
And even then... frequencies 180 kHz, 90 kHz, 700 Hz and 350 Hz are hardly usable due to the chip design. I don't really consider PWM steps of 50% or 25% as acceptable. In particular when using automatic fan speed control, this would probably be awful. I think this is worth a note in Documentation/hwmon/lm63. Maybe also output the resulting PWM step resolution to the kernel log?
drivers/hwmon/lm63.c static ssize_t show_pwm1_enable(struct device *dev, struct device_attribute *dum · line 398
Quoted 4 lines
drivers/hwmon/lm63.c +4
-0+if (val >180000 || val <23 ) +return -EINVAL ; +if (val >700 && val <5806 ) +return -EINVAL ;
For PWM frequency values, the drivers are supposed to pick the nearest available frequency, not to reject unsupported ones.
drivers/hwmon/lm63.c static ssize_t show_pwm1_enable(struct device *dev, struct device_attribute *dum · line 404
Quoted 6 lines
drivers/hwmon/lm63.c +6
-0+ +mutex_lock (&data->update_lock); + +if (val <=700 ) { + data->config_fan |=8 ; + freq =1400 ;
FWIW, the datasheet suggests that the frequency is actually 1406 Hz (see the Application Notes section 3.1)
Quoted 27 lines
drivers/hwmon/lm63.c +27
-0+ }else { + data->config_fan &= ~8 ; + freq =360000 ; + } + freq = (freq + val) / (val *2 ); + +/* Adjust PWM to same duty cycle at new frequency */ + data->pwm1_value = (data->pwm1_value * freq + data->pwm1_freq/2 ) / + data->pwm1_freq; + data->pwm1_freq = freq; + +i2c_smbus_write_byte_data (client,LM63_REG_PWM_FREQ , data->pwm1_freq); +i2c_smbus_write_byte_data (client,LM63_REG_CONFIG_FAN , data->config_fan); +i2c_smbus_write_byte_data (client,LM63_REG_PWM_VALUE , data->pwm1_value); + +mutex_unlock (&data->update_lock); + +return count; +} + +static ssize_t show_pwm1_freq (struct device *dev,struct device_attribute *dummy, +char *buf) +{ +struct lm63_data *data =lm63_update_device (dev); +unsigned int freq; + + freq = (data->config_fan &8 ) ?1400 :360000 ;
drivers/hwmon/lm63.c static ssize_t show_pwm1_enable(struct device *dev, struct device_attribute *dum · line 441
Quoted 10 lines
drivers/hwmon/lm63.c +10
-0+ freq = (freq + data->pwm1_freq) / (data->pwm1_freq *2 ); + +return sprintf (buf,"%u\n" , freq); +} + +static ssize_t set_pwm_auto_point_temp (struct device *dev, +struct device_attribute *devattr,const char *buf,size_t count) +{ +struct i2c_client *client =to_i2c_client (dev); +struct lm63_data *data =lm63_update_device (dev);
No reason to call lm63_update_device() AFAICS.
drivers/hwmon/lm63.c static ssize_t show_pwm1_enable(struct device *dev, struct device_attribute *dum · line 443
Quoted 2 lines
drivers/hwmon/lm63.c +2
-0+const struct sensor_device_attribute *attr =to_sensor_dev_attr (devattr); +const int nr = attr->index;
We now recommend the following, more compact construct:
const int nr = to_sensor_dev_attr(devattr)->index;
drivers/hwmon/lm63.c static ssize_t show_pwm1_enable(struct device *dev, struct device_attribute *dum · line 447
Quoted 4 lines
drivers/hwmon/lm63.c +4
-0+long val =simple_strtol (buf,NULL ,0 ); + +if (!(data->config_fan &0x20 ))/* registers are read-only */ +return -EPERM ;
So basically the user has to set PWM in manual mode first before he/she can change the points of the automatic mode. While it makes some sense, other chips / drivers don't have this limitation, so it might be confusing. This should be documented in Documentation/hwmon/lm63.
Quoted 5 lines
drivers/hwmon/lm63.c +5
-0+ +i2c_smbus_write_byte_data (client,LM63_REG_PWM_AUTO_TEMP + nr *2 , +HYST_TO_REG (val)); + +return count;
drivers/hwmon/lm63.c static ssize_t show_pwm1_enable(struct device *dev, struct device_attribute *dum · line 462
Quoted 10 lines
drivers/hwmon/lm63.c +10
-0+} + +static ssize_t show_pwm_auto_point_temp (struct device *dev, +struct device_attribute *devattr,char *buf) +{ +struct i2c_client *client =to_i2c_client (dev); +const struct sensor_device_attribute *attr =to_sensor_dev_attr (devattr); +const int nr = attr->index; +int data =i2c_smbus_read_byte_data (client, +LM63_REG_PWM_AUTO_TEMP + nr *2 );
Should be cached.
drivers/hwmon/lm63.c static ssize_t show_pwm1_enable(struct device *dev, struct device_attribute *dum · line 471
Quoted 9 lines
drivers/hwmon/lm63.c +9
-0+ +return sprintf (buf,"%d\n" ,TEMP8_FROM_REG (data)); +} + +static ssize_t set_pwm_auto_point_pwm (struct device *dev, +struct device_attribute *devattr,const char *buf,size_t count) +{ +struct i2c_client *client =to_i2c_client (dev); +struct lm63_data *data =lm63_update_device (dev);
No reason to call lm63_update_device() AFAICS.
drivers/hwmon/lm63.c static ssize_t show_pwm1_enable(struct device *dev, struct device_attribute *dum · line 484
Quoted 13 lines
drivers/hwmon/lm63.c +13
-0+const struct sensor_device_attribute *attr =to_sensor_dev_attr (devattr); +const int nr = attr->index; +long val =simple_strtol (buf,NULL ,0 ); + +if (!(data->config_fan &0x20 ))/* registers are read-only */ +return -EPERM ; + + val = val <=0 ?0 : val >=255 ?2 * data->pwm1_freq : + (val * data->pwm1_freq *2 +127 ) /255 ; +i2c_smbus_write_byte_data (client,LM63_REG_PWM_AUTO_PWM + nr *2 , val); + +return count; +}
This function looks a lot like set_pwm1(), it should be possible to share the code.
drivers/hwmon/lm63.c static ssize_t show_pwm1_enable(struct device *dev, struct device_attribute *dum · line 494
Quoted 10 lines
drivers/hwmon/lm63.c +10
-0+ +static ssize_t show_pwm_auto_point_pwm (struct device *dev, +struct device_attribute *devattr,char *buf) +{ +struct i2c_client *client =to_i2c_client (dev); +struct lm63_data *data =lm63_update_device (dev); +const struct sensor_device_attribute *attr =to_sensor_dev_attr (devattr); +const int nr = attr->index; +int val =i2c_smbus_read_byte_data (client, +LM63_REG_PWM_AUTO_PWM + nr *2 );
Should be cached.
drivers/hwmon/lm63.c static ssize_t show_pwm1_enable(struct device *dev, struct device_attribute *dum · line 499
Quoted 5 lines
drivers/hwmon/lm63.c +5
-0+ +return sprintf (buf,"%d\n" , val >=2 * data->pwm1_freq ?255 : + (val *255 + data->pwm1_freq) / (2 * data->pwm1_freq)); +} +
Here again this function looks a lot like show_pwm1(), so it should be possible to share the code.
drivers/hwmon/lm63.c static ssize_t show_pwm1_enable(struct device *dev, struct device_attribute *dum · line 506
Quoted 7 lines
drivers/hwmon/lm63.c +7
-0+static ssize_t show_pwm_auto_hyst (struct device *dev, +struct device_attribute *dummy,char *buf) +{ +struct i2c_client *client =to_i2c_client (dev); + +unsigned int val =i2c_smbus_read_byte_data (client, +LM63_REG_PWM_AUTO_HYST );
Should be cached.
Quoted 60 lines
drivers/hwmon/lm63.c +41
-1+ + return sprintf(buf, "%d\n", TEMP8_FROM_REG(val)); +} + +static ssize_t set_pwm_auto_hyst (struct device *dev,struct device_attribute *dummy, +const char *buf,size_t count) +{ +struct i2c_client *client =to_i2c_client (dev); +long val =simple_strtol (buf,NULL ,0 ); + +i2c_smbus_write_byte_data (client,LM63_REG_PWM_AUTO_HYST , +HYST5_TO_REG (val)); +return count; +} + +#endif /* CONFIG_SENSORS_LM63_SETUP */ +static ssize_t show_temp8 (struct device *dev,struct device_attribute *devattr,char *buf) {@@ -273,11 +554,15 @@ static ssize_t show_temp11(struct device *dev, struct device_attribute *devattr,static ssize_t set_temp11 (struct device *dev,struct device_attribute *devattr,const char *buf,size_t count) { -static const u8 reg[4 ] = { +static const u8 reg[] = {LM63_REG_REMOTE_LOW_MSB ,LM63_REG_REMOTE_LOW_LSB ,LM63_REG_REMOTE_HIGH_MSB ,LM63_REG_REMOTE_HIGH_LSB , +#ifdef CONFIG_SENSORS_LM63_SETUP +LM63_REG_REMOTE_OFFSET_MSB , +LM63_REG_REMOTE_OFFSET_LSB , +#endif };struct sensor_device_attribute *attr =to_sensor_dev_attr (devattr);@@ -341,12 +626,58 @@ static ssize_t show_alarm(struct device *dev, struct device_attribute *devattr,return sprintf (buf,"%u\n" , (data->alarms >> bitnr) &1 ); } + +#ifdef CONFIG_SENSORS_LM63_SETUP +/* Attributes for setting up the LM63's configuration, typically done by the +* BIOS. */ + +#define SENSOR_ATTR_PWM_AUTO_POINT_TEMP (n) \ + SENSOR_DEVICE_ATTR(pwm1_auto_point##n##_temp, S_IWUSR | S_IRUGO, \ + show_pwm_auto_point_temp, set_pwm_auto_point_temp, n-1) +#define SENSOR_ATTR_PWM_AUTO_POINT_PWM (n) \ + SENSOR_DEVICE_ATTR(pwm1_auto_point##n##_pwm, S_IWUSR | S_IRUGO, \ + show_pwm_auto_point_pwm, set_pwm_auto_point_pwm, n-1) + +SENSOR_ATTR_PWM_AUTO_POINT_TEMP (1 ); +SENSOR_ATTR_PWM_AUTO_POINT_TEMP (2 ); +SENSOR_ATTR_PWM_AUTO_POINT_TEMP (3 ); +SENSOR_ATTR_PWM_AUTO_POINT_TEMP (4 ); +SENSOR_ATTR_PWM_AUTO_POINT_TEMP (5 ); +SENSOR_ATTR_PWM_AUTO_POINT_TEMP (6 ); +SENSOR_ATTR_PWM_AUTO_POINT_TEMP (7 );
drivers/hwmon/lm63.c static ssize_t show_alarm(struct device *dev, struct device_attribute *devattr, · line 657
Quoted 10 lines
drivers/hwmon/lm63.c +10
-0+SENSOR_ATTR_PWM_AUTO_POINT_TEMP (8 ); + +SENSOR_ATTR_PWM_AUTO_POINT_PWM (1 ); +SENSOR_ATTR_PWM_AUTO_POINT_PWM (2 ); +SENSOR_ATTR_PWM_AUTO_POINT_PWM (3 ); +SENSOR_ATTR_PWM_AUTO_POINT_PWM (4 ); +SENSOR_ATTR_PWM_AUTO_POINT_PWM (5 ); +SENSOR_ATTR_PWM_AUTO_POINT_PWM (6 ); +SENSOR_ATTR_PWM_AUTO_POINT_PWM (7 ); +SENSOR_ATTR_PWM_AUTO_POINT_PWM (8 );
I don't see much value in having two separate macros for this. Just have SENSOR_ATTR_PWM_AUTO_POINT() create pairs of files?
Quoted 61 lines
drivers/hwmon/lm63.c +42
-1+ +static DEVICE_ATTR(pwm1_auto_hyst, S_IWUSR | S_IRUGO, show_pwm_auto_hyst, + set_pwm_auto_hyst); +static DEVICE_ATTR(pwm1_polarity, S_IWUSR | S_IRUGO, show_pwm1_polarity, + set_pwm1_polarity); +static DEVICE_ATTR(fan1_enable, S_IWUSR | S_IRUGO, show_fan1_enable, + set_fan1_enable); +static DEVICE_ATTR(pwm1_enable, S_IWUSR | S_IRUGO, show_pwm1_enable, + set_pwm1_enable); +static DEVICE_ATTR(conv_rate, S_IWUSR | S_IRUGO, show_conv_rate, + set_conv_rate); +static DEVICE_ATTR(pwm1_freq, S_IWUSR | S_IRUGO, show_pwm1_freq, set_pwm1_freq); +static SENSOR_DEVICE_ATTR(temp2_offset, S_IWUSR | S_IRUGO, show_temp11, + set_temp11, 3); +#else +static DEVICE_ATTR(pwm1_enable, S_IRUGO, show_pwm1_enable, NULL); +#endif + static SENSOR_DEVICE_ATTR(fan1_input, S_IRUGO, show_fan, NULL, 0); static SENSOR_DEVICE_ATTR(fan1_min, S_IWUSR | S_IRUGO, show_fan, set_fan, 1); static DEVICE_ATTR(pwm1, S_IWUSR | S_IRUGO, show_pwm1, set_pwm1); -static DEVICE_ATTR(pwm1_enable, S_IRUGO, show_pwm1_enable, NULL); static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, show_temp8, NULL, 0); static SENSOR_DEVICE_ATTR(temp1_max, S_IWUSR | S_IRUGO, show_temp8,@@ -388,6 +719,30 @@ static struct attribute *lm63_attributes[] = { &sensor_dev_attr_temp2_max_alarm.dev_attr.attr, &sensor_dev_attr_temp1_max_alarm.dev_attr.attr, &dev_attr_alarms.attr, +#ifdef CONFIG_SENSORS_LM63_SETUP + &dev_attr_pwm1_polarity.attr, + &dev_attr_fan1_enable.attr, + &dev_attr_conv_rate.attr, + &dev_attr_pwm1_freq.attr, + &dev_attr_pwm1_auto_hyst, + &sensor_dev_attr_temp2_offset.dev_attr.attr, + &sensor_dev_attr_pwm1_auto_point1_temp.dev_attr.attr, + &sensor_dev_attr_pwm1_auto_point2_temp.dev_attr.attr, + &sensor_dev_attr_pwm1_auto_point3_temp.dev_attr.attr, + &sensor_dev_attr_pwm1_auto_point4_temp.dev_attr.attr, + &sensor_dev_attr_pwm1_auto_point5_temp.dev_attr.attr, + &sensor_dev_attr_pwm1_auto_point6_temp.dev_attr.attr, + &sensor_dev_attr_pwm1_auto_point7_temp.dev_attr.attr, + &sensor_dev_attr_pwm1_auto_point8_temp.dev_attr.attr, + &sensor_dev_attr_pwm1_auto_point1_pwm.dev_attr.attr, + &sensor_dev_attr_pwm1_auto_point2_pwm.dev_attr.attr, + &sensor_dev_attr_pwm1_auto_point3_pwm.dev_attr.attr, + &sensor_dev_attr_pwm1_auto_point4_pwm.dev_attr.attr, + &sensor_dev_attr_pwm1_auto_point5_pwm.dev_attr.attr, + &sensor_dev_attr_pwm1_auto_point6_pwm.dev_attr.attr, + &sensor_dev_attr_pwm1_auto_point7_pwm.dev_attr.attr, + &sensor_dev_attr_pwm1_auto_point8_pwm.dev_attr.attr, +#endifNULL };@@ -617,6 +972,12 @@ static struct lm63_data *lm63_update_device(struct device *dev) LM63_REG_REMOTE_HIGH_MSB) << 8) | i2c_smbus_read_byte_data(client,
drivers/hwmon/lm63.c static struct lm63_data *lm63_update_device(struct device *dev) · line 983
Quoted 10 lines
drivers/hwmon/lm63.c +6
-0LM63_REG_REMOTE_HIGH_LSB); +#ifdef CONFIG_SENSORS_LM63_SETUP + data->temp11[3 ] = (i2c_smbus_read_byte_data (client, +LM63_REG_REMOTE_OFFSET_MSB ) <<8 ) + |i2c_smbus_read_byte_data (client, +LM63_REG_REMOTE_OFFSET_LSB ); +#endif data->temp8[2 ] =i2c_smbus_read_byte_data (client,LM63_REG_REMOTE_TCRIT ); data->temp2_crit_hyst = i2c_smbus_read_byte_data(client,
Thanks,
Signature · 6 lines
Jean Delvare _______________________________________________ lm-sensors mailing list [email protected] http://lists.lm-sensors.org/mailman/listinfo/lm-sensors
Re: [lm-sensors] [PATCH] LM63: Add controls for configuring deviceZ
Quoted 5 lines · Jean Delvare
Quoted 2 lines · Trent Piepho
Add controls that configure settings normally done by the BIOS. Turned on with
CONFIG_SENSORS_LM63_SETUPWhat system do you need this for?
A new embedded device. There is no bios, and we are the designers of the hardware. I want to configure the lm63 using Linux, because it's by far the easiest and most convenient way to do it. The hardware changes a lot, and the way I want the LM63 configured changes with it. And how it should be configured isn't exactly written in stone somewhere. I need to figure out what I want by changing things are seeing how it works.
I realize that some of these settings aren't something the average desktop user will want to change or see. But for an embedded developer, they are useful. That why I made it an config option. There are plenty of other options in the kernel designed for embedded systems.
Quoted 5 lines
Quoted 1 line
pwm1_polarity: 0 = low for fan off, 1 = low for fan on. Changing theI am reluctant to accept this as a run-time option... It's a good thing that you preserve the fan speed, but still, one of the polarities is wrong, and wrong polarity doesn't bode well with automatic fan speed
If the default LM63 polarity is wrong, how is it supposed to be changed? Suppose the correct polarity depends on the hardware revision? Or some kind of configuration setting? It's possible that the polarity of the lm63 will be controlled by a dip switch in the next hardware revision, as the polarity also controls whether the lm63 comes out of reset with the fan on or off, and if one can use pwm and fan speed monitoring at the same time.
One of the benefits of using embedded Linux vs something like vxworks is that linux has things like sysfs. You can debug your hardware just by reading and writing to easily named files with the shell. Beats needing to recompile and and re-flash an entire image to change anything, or typing in cryptic hex sequences to a debugger.
Quoted 3 lines
control, which the LM63 implements: you'll end up with either fan at full speed (bad) or stopped (worse). The polarity should really be set before the driver is loaded and never changed after that.
Set by what? Create a second lm63 setup only driver that does it? Put a copy of the LM63 driver into U-Boot and then add this setting to that driver? IMHO, it makes a lot more sense to add one more setting to a driver and infrastructure that is already there, than to create some entirely new system just to change one thing.
Quoted 8 lines
Quoted 2 lines
fan1_enable: Enables/disables fan tach monitoring. Turning this on/off
will create/remove the other fan related sysfs files.Again I am reluctant to make this a run-time selection, because you're not just enabling/disabling the tachometer function, you're also disabling/enabling the ALERT output which shares the same pin. Only one configuration is correct for the hardware setup, so it should be set once and for all before the driver is loaded.
Again, set by what? I can move one resistor on my board and change the lm63's connection from fan monitoring to alert output, so I want to be able to configure the lm63 as well. I want to see if both work, so I need to be able to change the lm63 setting.
Think of it from my perspective and anyone else who is designing hardware. There is some LM63 setting we want to be able to change. Do you add a small bit of code to a driver that's already written, that you're already loading, has all the infrastructure in place to make the code simple to write, and will present the same interface for changing this settings as changing all the other settings? Or do you create some entirely new system from scratch, that doesn't give you macros for register names or anything to start with, and gives a different interface for changing this one setting than is used to change everything else? And what about when you want to change these things while the system is running? If you don't change them with the lm63 driver, how does the lm63 driver cope with "something else" changing the settings out from under it?
Quoted 4 lines
Quoted 1 line
conv_rate: Set the conversion rate, format is log2(frequency)I fail to see the point of this one, given that the driver caches the data for 1 second anyway?
It might make sense to lower the frequency to 1 Hz then, no? I think the default is 16 or 32 Hz. I wonder how the conversion rate affects the automatic fan speed control? It doesn't trip instantly, there is some filtering that goes on. And the LM63's power consumption? I suppose one could test and measure these things, if only there was a convenient way to change the conversion rate and then observe the effect.....
Quoted 2 lines
pwm_auto_point1_temp_hyst: read/write
pwm_auto_point[2-8]_temp_hyst: read only
Why not make them all read/write? Then they could all be defined to use the same store function and not need to worry if they should be read only or not.
Quoted 5 lines
This basically leaves only two or three configuration bits (
pwm1_polarity,fan1_enableand maybeconv_rateif you can explain) that would belong toCONFIG_SENSORS_LM63_SETUP. Quite frankly, I'd rather see you use i2cset to write them to the LM63 chip at boot time, rather than adding code to the lm63 driver, even as a separate option. You'll have to
Well, why write a lm63 driver at all? Just use i2cset for everything. Looking up register values in the datasheet and converting them to hex bytes in your head gets old fast. It's also very convenient to put something like this:
echo $SYSCFG_PWM_POLARITY > /sys/class/hwmon/hwmon0/device/pwm1_polarity
In your startup script. Instead of having to include i2cset and i2cdump on your embedded device (there goes another sector of flash) and trying to implement a script that sets the necessary bit.
Quoted 1 line
Preliminary note: many lines over 80 columns in your patch, please fix.
I thought this was just a warning now. The long function names used in this driver make some lines that are just a few characters over 80 columns very ugly to wrap, which the existing driver code doesn't do either. For example, my function definition for set_pwm1_enable() is 83 character, but the existing one for show_pwm1_enable() is 84.
Quoted 4 lines
Review
Quoted 1 line
(quoted diff) +1
-0+ i2c_smbus_write_byte_data(client, LM63_REG_CONFIG_FAN, data->config_fan);You should not assume that
data->config_fanwas up-to-date. Do a proper read/modify/write cycle instead.
I thought lm63_update_device() would refresh it, but I see that's not the case.
Still, only a few registers in the LM63 are "volatile" and will be changed by the lm63 device. For those that aren't, it's just a waste to re-read them each time lm63_update_device() is called. They won't change and usually one doesn't care about their value anyway.
Now, one could change these registers with i2cset. But there is no way to have an *atomic* read/modify/write cycle with the i2c layer! Assuming that data->config_fan will remain valid for a 1 second cache is just as wrong as assuming it's always valid in the face of something from outside the driver modifying the lm63.
w.r.t config_fan, doesn't the existing code that uses it, e.g. show_pwm1_enable(), have the exact same problem? It calls lm63_update_device, even though that doesn't update config_fan. And then it prints config_fan without refreshing it.
Quoted 4 lines
Review
Quoted 2 lines
(quoted diff) +2
-0+ /* Invert duty cycle, to keep fan at the same setting */ + data->pwm1_value = 2 * data->pwm1_freq - data->pwm1_value;Same for
data->pwm1_value, it might not be up-to-date.
In this case I do call lm63_update_device(), which *does* update pwm1_value.
It should be up-to-date, depending on how you define up-to-date. Since there is no way to lock the i2c bus, there will *always* be a race condition if you allow something besides the lm63 driver to modify the lm63. IMHO, the only reasonable way to write the driver is to assume that nothing else will be driving the chip at the same time. If one can not assume this, then every single hwmon driver is broken. And since the buses and chips involved do not support atomic transactions of the type needed, there is no way to fix them.
Quoted 7 lines
Review
Quoted 4 lines
(quoted diff) +4
-0+static ssize_t show_pwm1_polarity(struct device *dev, + struct device_attribute *dummy, char *buf) +{ + struct lm63_data *data = lm63_update_device(dev);Why call
lm63_update_device()?data->config_fanisn't updated by this function.
I just copied the code from show_pwm1_enable()
Quoted 7 lines
Review
Quoted 2 lines
(quoted diff) +2
-0+ i2c_smbus_write_byte_data(client, LM63_REG_CONFIG1, data->config); + mutex_unlock(&data->update_lock);I don't like this... For one thing, removing files to create them right after is ugly. For another, libsensors scans sysfs for attributes at initialization time and doesn't expect attributes to be added or removed after that.
This isn't something that changes a lot, if ever, and it's the easiest way to do it. Otherwise it's a lot more work to become robust against sysfs and the config work becoming out of sync. I thought code simplicity and clarity were more important than speed in this case.
libsensors would be OK, if one sets fan1_enable before starting whatever app uses it, right?
Quoted 5 lines
Review
Quoted 3 lines
(quoted diff) +3
-0+ /* Value is log2(rate) */ + if (val < -4 || val > 5) + return -EINVAL;What a friendly unit ;)
Do you have a better unit that easily covers the range 1/16 to 32? And is easy to map to the closest power of two? I didn't see any other controls for a value like this to copy the units from. And I figured, why make the driver harder to write? If someone doesn't like the units, they can convert them in userspace.
Quoted 5 lines
Review
Quoted 1 line
(quoted diff) +1
-0+ data = i2c_smbus_read_byte_data(client, LM63_REG_CONV_RATE);Because this isn't cached, you're giving users an easy way to saturate the SMBus. Either make the attribute readable by root only, or cache its value.
I didn't want to add extra stuff to read in lm63_update_device(). Maybe I should just make this root readable only, it's the least amount of code.
Quoted 8 lines
Review
Quoted 1 line
(quoted diff) +1
-0+ /* Only support the ranges [23, 700] and [5806, 180000] */And even then... frequencies 180 kHz, 90 kHz, 700 Hz and 350 Hz are hardly usable due to the chip design. I don't really consider PWM steps of 50% or 25% as acceptable. In particular when using automatic fan speed control, this would probably be awful. I think this is worth a note in
Documentation/hwmon/lm63. Maybe also output the resulting PWM step resolution to the kernel log?
info or debug? Even when there are just steps of 0%, 50%, 100%, it's still useful. You could have a fan that is normally off, but can turn on in an emergency if the temp gets too high. A simple on/off control could be enough. I've also found that small fans don't like pwm much, they seem to either be off or nearly off, or on at full power.
Quoted 8 lines
Review
Quoted 5 lines
(quoted diff) +5
-0+ mutex_lock(&data->update_lock); + + if (val <= 700) { + data->config_fan |= 8; + freq = 1400;FWIW, the datasheet suggests that the frequency is actually 1406 Hz (see the Application Notes section 3.1)
You're right. I bet it's 360 kHz / 256 = 1406.25 Hz. From the AN, it somewhere between 1406.2 and 1406.4. It would probably give the most accurate results to use the higher frequency, then shift by 8. There's got to be an interesting way to use that relationship.
Quoted 9 lines
Review
Quoted 4 lines
(quoted diff) +4
-0+ long val = simple_strtol(buf, NULL, 0); + + if (!(data->config_fan & 0x20)) /* registers are read-only */ + return -EPERM;So basically the user has to set PWM in manual mode first before he/she can change the points of the automatic mode. While it makes some sense, other chips / drivers don't have this limitation, so it might be confusing. This should be documented in
Documentation/hwmon/lm63.
It's a chip limitation and it didn't seem like I should work around it in the driver, by turning auto fan mode off and back on. What if there is an error half way through and the fan mode gets left in manual mode, with the fan off? Better to only turn the automatic fan off by explicit user request.
Quoted 4 lines
Review
Quoted 2 lines
(quoted diff) +2
-0+ int data = i2c_smbus_read_byte_data(client, + LM63_REG_PWM_AUTO_TEMP + nr * 2);Should be cached.
Adding all these registers to be read each time update_device is called would be wasteful. They don't change on their own. Cache them once when the driver loads? Or have a lm63_update_pwmauto() function that just updates these registers?
Maybe there is a smbus function that can read them all in one go, I think the lm63 has an auto-incrementing index register. That would be more nice to use.
Quoted 4 lines
Quoted 1 line
+static
ssize_tshow_pwm_auto_point_pwm(struct device *dev,Here again this function looks a lot like
show_pwm1(), so it should be possible to share the code.
Indeed it should, if it's not going to be enabled by a configuration option. I didn't want to confuse the existing code by sticking ifdefs in it.
Quoted 1 line
Review
Quoted 1 line
(quoted diff) +1
-0+ &dev_attr_pwm1_auto_hyst,
I forgot the .attr on this line, since fixed.
_______________________________________________
lm-sensors mailing list
[email protected]
http://lists.lm-sensors.org/mailman/listinfo/lm-sensors
Hi Trent,
Quoted 19 lines · Trent Piepho
Quoted 5 lines · Jean Delvare
Quoted 2 lines · Trent Piepho
Add controls that configure settings normally done by the BIOS. Turned on with
CONFIG_SENSORS_LM63_SETUPWhat system do you need this for?
A new embedded device. There is no bios, and we are the designers of the hardware. I want to configure the lm63 using Linux, because it's by far the easiest and most convenient way to do it. The hardware changes a lot, and the way I want the LM63 configured changes with it. And how it should be configured isn't exactly written in stone somewhere. I need to figure out what I want by changing things are seeing how it works.
I realize that some of these settings aren't something the average desktop user will want to change or see. But for an embedded developer, they are useful. That why I made it an config option. There are plenty of other options in the kernel designed for embedded systems.
Quoted 7 lines
Quoted 5 lines
Quoted 1 line
pwm1_polarity: 0 = low for fan off, 1 = low for fan on. Changing theI am reluctant to accept this as a run-time option... It's a good thing that you preserve the fan speed, but still, one of the polarities is wrong, and wrong polarity doesn't bode well with automatic fan speed
If the default LM63 polarity is wrong, how is it supposed to be changed?
I'd say firmware, platform code, or i2cset, in this order of preference. Relying on user-space to configure a hardware monitoring chip is somewhat dangerous, user-space might as well never come up.
Quoted 5 lines
Suppose the correct polarity depends on the hardware revision? Or some kind of configuration setting? It's possible that the polarity of the lm63 will be controlled by a dip switch in the next hardware revision, as the polarity also controls whether the lm63 comes out of reset with the fan on or off, and if one can use pwm and fan speed monitoring at the same time.
I fail to see how this matters. You'll have to know what register values to change anyway. How we change them is an implementation detail.
Quoted 6 lines
One of the benefits of using embedded Linux vs something like vxworks is that linux has things like sysfs. You can debug your hardware just by reading and writing to easily named files with the shell. Beats needing to recompile and and re-flash an entire image to change anything, or typing in cryptic hex sequences to a debugger.
We have debugfs for that, I don't think that sysfs is meant for this kind of experiment.
Quoted 9 lines
Quoted 3 lines
control, which the LM63 implements: you'll end up with either fan at full speed (bad) or stopped (worse). The polarity should really be set before the driver is loaded and never changed after that.
Set by what? Create a second lm63 setup only driver that does it? Put a copy of the LM63 driver into U-Boot and then add this setting to that driver? IMHO, it makes a lot more sense to add one more setting to a driver and infrastructure that is already there, than to create some entirely new system just to change one thing.
i2cset is already there for a long time, and it has the advantage that you can use it to setup pretty much any I2C or SMBus device without writing dedicated kernel code.
Quoted 10 lines
Quoted 9 lines
Quoted 3 lines
fan1_enable: Enables/disables fan tach monitoring. Turning this on/off
will create/remove the other fan related sysfs files.Again I am reluctant to make this a run-time selection, because you're not just enabling/disabling the tachometer function, you're also disabling/enabling the ALERT output which shares the same pin. Only one configuration is correct for the hardware setup, so it should be set once and for all before the driver is loaded.
Quoted 4 lines
Again, set by what? I can move one resistor on my board and change the lm63's connection from fan monitoring to alert output, so I want to be able to configure the lm63 as well. I want to see if both work, so I need to be able to change the lm63 setting.
I'm not arguing the if, just the how.
Quoted 10 lines
Think of it from my perspective and anyone else who is designing hardware. There is some LM63 setting we want to be able to change. Do you add a small bit of code to a driver that's already written, that you're already loading, has all the infrastructure in place to make the code simple to write, and will present the same interface for changing this settings as changing all the other settings? Or do you create some entirely new system from scratch, that doesn't give you macros for register names or anything to start with, and gives a different interface for changing this one setting than is used to change everything else?
I understand your point of view. You went for the most easy and straightforward solution to solve the problem at hand (but not necessarily the cleanest one, see below.)
Me, I am thinking long-term. You're creating a precedent with CONFIG_SENSORS_LM63_SETUP. If one driver has this, I can see all the embedded developers and power users rush in and ask for something similar for their hardware monitoring driver. Not only that would be a mess in menuconfig, but more importantly this could cause some confusion with regards to the sysfs interface. We've been claiming that the sysfs interface to hardware monitoring devices was essentially safe to use, and that would no longer be always true.
We have also been claiming loudly that the sysfs interface to hardware monitoring devices was now standardized. If we want this to be still true, then all CONFIG_SENSORS_*_SETUP extensions would have to agree on standards for the extra setup functions. That would be a lot of work and I am definitely not willing to invest time in this. Alternatively, we could just say that the advanced setup sysfs files are not standard, but I can see this cause even more confusion when two drivers will offer sysfs files by the same name but expecting different conventions.
Your approach works now for the problem you're willing to solve. But even then it can be argued if this is the correct solution from an engineering point of view. You are adding code to a kernel driver for something that should be setup at device initialization time, but you give it a run-time interface. This means that you have to keep the code in memory forever while you really only need it once. Given that you are working on a system with limited resources, I thought that this is something you would care about.
Quoted 4 lines
And what about when you want to change
these things while the system is running? If you don't change them with the lm63 driver, how does the lm63 driver cope with "something else" changing the settings out from under it?
Changing the PWM polarity while the system is running? Hmmm ;) Note that all the hwmon drivers actually cope mostly well with changes happening behind their back, by design, as discussed later. But anyway, these are settings you simply shouldn't change at run-time. If you're debugging stuff, just unload the lm63 driver, run i2cset, and load the lm63 driver again.
Quoted 6 lines
Quoted 5 lines
Quoted 2 lines
conv_rate: Set the conversion rate, format is log2(frequency)I fail to see the point of this one, given that the driver caches the data for 1 second anyway?
Quoted 6 lines
It might make sense to lower the frequency to 1 Hz then, no? I think the default is 16 or 32 Hz. I wonder how the conversion rate affects the automatic fan speed control? It doesn't trip instantly, there is some filtering that goes on. And the LM63's power consumption? I suppose one could test and measure these things, if only there was a convenient way to change the conversion rate and then observe the effect.....
That's a tricky question. On the one hand, having a conversion rate different from 1 or 2 Hz doesn't make much sense due to the caching done by the driver. But on the other hand the refresh frequency has many other implications as you found out (alert output, automatic fan speed control, power consumption...) For this reason we decided to NOT change the conversion rate in the driver, assuming that the BIOS would have setup the conversion rate properly if it really matters to the system. If anything, we could adjust the cache lifetime depending on the conversion rate (with a safety minimum), but not the other way around.
Quoted 7 lines
Quoted 3 lines
pwm_auto_point1_temp_hyst: read/write
pwm_auto_point[2-8]_temp_hyst: read onlyWhy not make them all read/write? Then they could all be defined to use the same store function and not need to worry if they should be read only or not.
Making them all writable would confuse user-space. This would look like all hysteresis values can be set independently from each other. At least when only one hysteresis value can be set, it's visible that the other ones are computed automatically and cannot be changed on their own.
It's not that difficult to implement the way I suggested, just give it a try. Many other drivers do that already, see temp[1-4]_crit in the lm83 driver for example.
Quoted 8 lines
Quoted 6 lines
This basically leaves only two or three configuration bits (
pwm1_polarity,fan1_enableand maybeconv_rateif you can explain) that would belong toCONFIG_SENSORS_LM63_SETUP. Quite frankly, I'd rather see you use i2cset to write them to the LM63 chip at boot time, rather than adding code to the lm63 driver, even as a separate option. You'll have toWell, why write a lm63 driver at all? Just use i2cset for everything.
Excellent idea, please do that.
You know, I have been spending about 2 hours reviewing your patch originally, and now I'm spending again one hour just to reply to this mail. If you're not going to listen to my experience with hardware monitoring drivers, just tell me right away and I'll go spend my precious time in a more useful way.
Quoted 5 lines
Looking up register values in the datasheet and converting them to hex bytes in your head gets old fast. It's also very convenient to put something like this:
echo $SYSCFG_PWM_POLARITY >
/sys/class/hwmon/hwmon0/device/pwm1_polarity
Not very robust... If you have several hardware monitoring devices, your lm63 chip could as well be hwmon1.
Quoted 4 lines
In your startup script. Instead of having to include i2cset and i2cdump on your embedded device (there goes another sector of flash) and trying to implement a script that sets the necessary bit.
You shouldn't need i2cdump, i2cset (and the i2c-dev driver) should be enough. If anything, I'd rather add i2cget (but again in most cases i2cset should suffice.) i2cset weights about 18 kB, the i2c-dev driver about 6 kB, that's pretty small, and more importantly, both can be removed from memory after use, while the hardware monitoring drivers cannot.
Anyway, i2cset is probably not the best solution for an embedded solution either, see below for something better.
Quoted 13 lines
Quoted 4 lines
Review
Quoted 1 line
(quoted diff) +1
-0+ i2c_smbus_write_byte_data(client, LM63_REG_CONFIG_FAN, data->config_fan);You should not assume that
data->config_fanwas up-to-date. Do a proper read/modify/write cycle instead.I thought
lm63_update_device()would refresh it, but I see that's not the case.Still, only a few registers in the LM63 are "volatile" and will be changed by the lm63 device. For those that aren't, it's just a waste to re-read them each time
lm63_update_device()is called. They won't change and usually one doesn't care about their value anyway.
Quoted 5 lines
Now, one could change these registers with i2cset. But there is no way to have an *atomic* read/modify/write cycle with the i2c layer! Assuming that
data->config_fanwill remain valid for a 1 second cache is just as wrong as assuming it's always valid in the face of something from outside the driver modifying the lm63.
You're all correct, but this is a design decision and all hardware monitoring drivers are following it. The main reasons are that ACPI or SMM code could actually change the register values in our back, and also that multi-master I2C buses exist. So you will have to keep following this design decision for the time being.
I'm not going to discuss the pros and cons of this approach here, that's not the point. There is a discussion going on on the lm-sensors list about this:
http://lists.lm-sensors.org/pipermail/lm-sensors/2008-April/022813.html
Feel free to speak up there.
Quoted 5 lines
w.r.t
config_fan, doesn't the existing code that uses it, e.g.show_pwm1_enable(), have the exact same problem? It callslm63_update_device, even though that doesn't updateconfig_fan. And then it printsconfig_fanwithout refreshing it.
You're right, that's a bug in the current lm63 driver. Should be fixed; care to send a standalone patch?
<snip>
Quoted 9 lines
Quoted 4 lines
I don't like this... For one thing, removing files to create them right after is ugly. For another, libsensors scans sysfs for attributes at initialization time and doesn't expect attributes to be added or removed after that.
This isn't something that changes a lot, if ever, and it's the easiest way to do it. Otherwise it's a lot more work to become robust against sysfs and the config work becoming out of sync. I thought code simplicity and clarity were more important than speed in this case.
"A lot of work"? Store a bit in struct lm63_data to remember whether the files are created or not, and you're done.
Quoted 3 lines
libsensors would be OK, if one sets
fan1_enablebefore starting whatever app uses it, right?
Yes it would, but your design can't guarantee that fan1_enable won't change later. That's exactly the reason why I claim that your design is not correct.
Quoted 7 lines
Quoted 6 lines
Review
Quoted 4 lines
(quoted diff) +3
-0+ /* Value is log2(rate) */ + if (val < -4 || val > 5) + return -EINVAL;What a friendly unit ;)
Quoted 5 lines
Do you have a better unit that easily covers the range 1/16 to 32? And is easy to map to the closest power of two? I didn't see any other controls for a value like this to copy the units from. And I figured, why make the driver harder to write? If someone doesn't like the units, they can convert them in userspace.
See my rant above about non-standard sysfs interface files. Basically you're making the kernel code as simple as possible and rely on the user to read the device datasheet and convert from human value to register value. This goes against the sysfs interface design decisions.
If I had to find a standard way to implement this, I think I would name the file conv_interval and express the value in ms. This covers your immediate needs, and should be fine for other devices as well. Converting from ms to a register value in the lm63 driver shouldn't be that difficult, for example using the helpers in <linux/log2.h>.
Quoted 19 lines
Quoted 6 lines
Review
Quoted 2 lines
(quoted diff) +1
-0+ data = i2c_smbus_read_byte_data(client, LM63_REG_CONV_RATE);Because this isn't cached, you're giving users an easy way to saturate the SMBus. Either make the attribute readable by root only, or cache its value.
I didn't want to add extra stuff to read in
lm63_update_device(). Maybe I should just make this root readable only, it's the least amount of code.Quoted 8 lines
Review
Quoted 1 line
(quoted diff) +1
-0+ /* Only support the ranges [23, 700] and [5806, 180000] */And even then... frequencies 180 kHz, 90 kHz, 700 Hz and 350 Hz are hardly usable due to the chip design. I don't really consider PWM steps of 50% or 25% as acceptable. In particular when using automatic fan speed control, this would probably be awful. I think this is worth a note in
Documentation/hwmon/lm63. Maybe also output the resulting PWM step resolution to the kernel log?
Quoted 5 lines
info or debug? Even when there are just steps of 0%, 50%, 100%, it's still useful. You could have a fan that is normally off, but can turn on in an emergency if the temp gets too high. A simple on/off control could be enough. I've also found that small fans don't like pwm much, they seem to either be off or nearly off, or on at full power.
I'd make this an info message. As I understand it, users set the PWM frequency only once so it shouldn't spam the logs in practice, and I think it's a useful information.
I agree that even a simple on/off switch can be useful, but still, the way the LM63 is designed is rather unique and I expect this to surprise some users who are used to other hardware monitoring devices where the PWM step resolution doesn't depend on the PWM frequency. I was really only thinking out loud - there's obviously nothing you can do at the software level.
<snip>
Quoted 10 lines
Quoted 9 lines
Review
Quoted 4 lines
(quoted diff) +4
-0+ long val = simple_strtol(buf, NULL, 0); + + if (!(data->config_fan & 0x20)) /* registers are read-only */ + return -EPERM;So basically the user has to set PWM in manual mode first before he/she can change the points of the automatic mode. While it makes some sense, other chips / drivers don't have this limitation, so it might be confusing. This should be documented in
Documentation/hwmon/lm63.
Quoted 4 lines
It's a chip limitation and it didn't seem like I should work around it in the driver, by turning auto fan mode off and back on. What if there is an error half way through and the fan mode gets left in manual mode, with the fan off? Better to only turn the automatic fan off by explicit user request.
Again I was merely thinking out loud - I'm fine with your implementation. I'm only suggesting that you document the (again unique) behavior of the LM63 in this respect, to save some questions on the lm-sensors list later.
Quoted 10 lines
Quoted 5 lines
Review
Quoted 3 lines
(quoted diff) +2
-0+ int data = i2c_smbus_read_byte_data(client, + LM63_REG_PWM_AUTO_TEMP + nr * 2);Should be cached.
Adding all these registers to be read each time
update_deviceis called would be wasteful. They don't change on their own. Cache them once when the driver loads? Or have alm63_update_pwmauto()function that just updates these registers?
You can indeed have a separate cache for these values, some drivers do that already (the lm85 and w83793 drivers for example.)
Quoted 4 lines
Maybe there is a smbus function that can read them all in one go, I think the lm63 has an auto-incrementing index register. That would be more nice to use.
The LM63 datasheet is exceptionally scarce about which SMBus (or I2C) transactions are supported. As I don't have an LM63 chip, you'll have to try by yourself. I hope they would have mentioned it if the SMBus block reads were supported, so we can probably rule this one out. This leaves us with (by order of preference):
* I2C block read.
* SMBus word read, giving you two register values per transaction. I doubt this will work if I2C block read failed.
* SMBus send byte followed by N SMBus receive bytes (also known as continuous byte read). Note that this may NOT be faster than simple byte reads on some systems, and may even be slightly slower.
Of course the underlying I2C adapter and its driver have to support this. I2C block read isn't too widely supported, the other two are.
Back to the "where does the initialization go" problem...
Thinking about it, this seems to be a case where a new-style i2c driver would help. As this is an embedded system, presumably you have some dedicated platform code running at boot. The new i2c infrastructure added by David Brownell would let you instantiate your lm63 device explicitly from the platform code. The advantage is that you then can pass an arbitrary structure containing initialization values to the lm63 driver. In your case, this could include the PWM polarity, the conversion rate, the tachometer input / alert output setup, etc. This has all the advantages:
* You have access to the lm63 driver facilities (register names, etc.)
* Most of the setup code can go away after initialization if it is tagged properly.
* You don't define a user-space interface so it doesn't have to follow any standard and can be changed later as needed.
* You no longer rely on user-space for device initialization, so nothing can go wrong if user-space doesn't come up for some reason.
The f75375s driver does exactly this already. It's an hybrid driver, supporting both legacy mode (device probed automatically) and new-style mode (device instantiated from platform code). I think that's what you should do with the lm63 driver.
So basically the road map could be:
* Fix show_pwm1_enable().
* Add the functions which I said could be included unconditionally.
* Convert the lm63 driver to an hybrid driver.
* Instantiate the lm63 device from your platform code, with all the configuration parameters you want.
Signature · 6 lines
Jean Delvare _______________________________________________ lm-sensors mailing list [email protected] http://lists.lm-sensors.org/mailman/listinfo/lm-sensors
Quoted 3 lines · Jean Delvare
Quoted 1 line
If the default LM63 polarity is wrong, how is it supposed to be changed?
I'd say firmware, platform code, or i2cset, in this order of
What firmware?
Platform code, that's a lot harder to change than user space. You have to recompile and re-flash to do anything. It's also hardest to port across kernel versions.
i2cset, way too manual.
Quoted 2 lines
preference. Relying on user-space to configure a hardware monitoring chip is somewhat dangerous, user-space might as well never come up.
The kernel might not come up either. Generally if the kernel boots up to the point where i2c is around, userspace is will be ok. Setup from userspace adds very little risk, especially in production use, over setup in the kernel. Of course it would a lot better if the chip let you strap these options when it came out of reset.
Quoted 8 lines
Quoted 5 lines
Suppose the correct polarity depends on the hardware revision? Or some kind of configuration setting? It's possible that the polarity of the lm63 will be controlled by a dip switch in the next hardware revision, as the polarity also controls whether the lm63 comes out of reset with the fan on or off, and if one can use pwm and fan speed monitoring at the same time.
I fail to see how this matters. You'll have to know what register values to change anyway. How we change them is an implementation detail.
Because the code that knows what needs to be set runs from userspace, after the system has booted and the lm63 driver is loaded. At that point you can't set it with the firmware (if there was firmware) or platform code. If the setup needed in in a file, how can platform code read the contents of this file?
Quoted 4 lines
Me, I am thinking long-term. You're creating a precedent with
CONFIG_SENSORS_LM63_SETUP. If one driver has this, I can see all the embedded developers and power users rush in and ask for something similar for their hardware monitoring driver. Not only that would be a
What's wrong with embedded developers and power users? Linux isn't just for desktops.
Quoted 3 lines
confusion with regards to the sysfs interface. We've been claiming that the sysfs interface to hardware monitoring devices was essentially safe to use, and that would no longer be always true.
I'm not sure what's less safe here. With the existing interface I could set the pwm1 value to 0 and start a fire. The CPU would at least lose its magic smoke. I don't think there is any setting I added more dangerous than that.
Quoted 8 lines
We have also been claiming loudly that the sysfs interface to hardware monitoring devices was now standardized. If we want this to be still true, then all CONFIG_SENSORS_*_SETUP extensions would have to agree on standards for the extra setup functions. That would be a lot of work and I am definitely not willing to invest time in this. Alternatively, we could just say that the advanced setup sysfs files are not standard, but I can see this cause even more confusion when two drivers will offer sysfs files by the same name but expecting different conventions.
IMHO, trying to standardize every last feature and quirk just isn't worth it. You end up with a huge standard that doesn't work very well for anyone. The abilities defined for the hwmon sysfs can be standard, and if some device creates a new file not covered, that's not part of the standard. Sort of like V4L2 private controls.
If some software project only wants to work with standard interfaces, they just don't support the non-standard files. They are no worse off than if those extra features didn't exist in the first place.
Quoted 4 lines
give it a run-time interface. This means that you have to keep the code in memory forever while you really only need it once. Given that you are working on a system with limited resources, I thought that this is something you would care about.
Flash is by far the most limited resource and that gets used no matter where the code is. The question is what is least amount of code, and how much development effort is it worth for the savings involved.
Quoted 4 lines
happening behind their back, by design, as discussed later. But anyway, these are settings you simply shouldn't change at run-time. If you're debugging stuff, just unload the lm63 driver, run i2cset, and load the lm63 driver again.
Modules? That would cause the kernel code to spill into the root filesystem. And use i2cset? You're back to looking up hex codes in the datasheet. I can remember echo 1 > pwm1_polarity, I can't remember the i2cset command to change the polarity. Do you know it off the top of your head?
Quoted 7 lines
Quoted 6 lines
Quoted 4 lines
fan1_enableand maybeconv_rateif you can explain) that would belong toCONFIG_SENSORS_LM63_SETUP. Quite frankly, I'd rather see you use i2cset to write them to the LM63 chip at boot time, rather than adding code to the lm63 driver, even as a separate option. You'll have toWell, why write a lm63 driver at all? Just use i2cset for everything.
Quoted 7 lines
Excellent idea, please do that.
You know, I have been spending about 2 hours reviewing your patch originally, and now I'm spending again one hour just to reply to this mail. If you're not going to listen to my experience with hardware monitoring drivers, just tell me right away and I'll go spend my precious time in a more useful way.
And I appreciate that. I've spent several hours incorporating most of your suggestions.
But it seems that you're of the option that nothing that is primarily aimed at hardware developers or embedded systems is acceptable. I do not see how one can possibly develop embedded systems under those conditions.
Quoted 8 lines
Quoted 5 lines
Looking up register values in the datasheet and converting them to hex bytes in your head gets old fast. It's also very convenient to put something like this:
echo $SYSCFG_PWM_POLARITY >
/sys/class/hwmon/hwmon0/device/pwm1_polarityNot very robust... If you have several hardware monitoring devices, your lm63 chip could as well be hwmon1.
The nice thing about embedded systems is you know what you need to worry about and what you don't. What's more, your hypothetical i2cset command would be a lot less robust. How would you find out what bus and address the chip is on?
Quoted 10 lines
Quoted 3 lines
In your startup script. Instead of having to include i2cset and i2cdump on your embedded device (there goes another sector of flash) and trying to implement a script that sets the necessary bit.
You shouldn't need i2cdump, i2cset (and the i2c-dev driver) should be enough. If anything, I'd rather add i2cget (but again in most cases i2cset should suffice.) i2cset weights about 18 kB, the i2c-dev driver about 6 kB, that's pretty small, and more importantly, both can be removed from memory after use, while the hardware monitoring drivers cannot.
How do you do a read/modify/write of a single bit in a register with i2cset? And even if these utilities can be removed from memory, they can't be removed from flash.
Quoted 7 lines
Quoted 4 lines
This isn't something that changes a lot, if ever, and it's the easiest way to do it. Otherwise it's a lot more work to become robust against sysfs and the config work becoming out of sync. I thought code simplicity and clarity were more important than speed in this case.
"A lot of work"? Store a bit in struct
lm63_datato remember whether the files are created or not, and you're done.
That would still be several lines of code. If someone is turning the fan on over and over again when it's already on, they're just stupidly telling the driver to do nothing. So does it really matter how efficient the driver is at doing nothing?
Quoted 3 lines
I'd make this an info message. As I understand it, users set the PWM frequency only once so it shouldn't spam the logs in practice, and I think it's a useful information.
I ending up playing with this one a lot. Some fans do not like the slow pwm frequencies. And some frequencies produce an audible, and quite annoying, sound. Higher frequencies tend to be better than lower ones, but that reduces the number of pwm steps. Lots of trade-offs involved.
Quoted 12 lines
The LM63 datasheet is exceptionally scarce about which SMBus (or I2C) transactions are supported. As I don't have an LM63 chip, you'll have to try by yourself. I hope they would have mentioned it if the SMBus block reads were supported, so we can probably rule this one out. This leaves us with (by order of preference):
* I2C block read.
* SMBus word read, giving you two register values per transaction. I doubt this will work if I2C block read failed.
* SMBus send byte followed by N SMBus receive bytes (also known as continuous byte read). Note that this may NOT be faster than simple byte reads on some systems, and may even be slightly slower.
Quoted 2 lines
Of course the underlying I2C adapter and its driver have to support this. I2C block read isn't too widely supported, the other two are.
Hmm, sounds like a lot of trouble. I'm pretty sure that the i2c block read will work for me, but what if the adapter doesn't support it? It looks like the smbus layer doesn't emulate it for you with read_byte_data(s) if the adapter doesn't have it, so you've got do that yourself.
Quoted 18 lines
Back to the "where does the initialization go" problem...
Thinking about it, this seems to be a case where a new-style i2c driver would help. As this is an embedded system, presumably you have some dedicated platform code running at boot. The new i2c infrastructure added by David Brownell would let you instantiate your lm63 device explicitly from the platform code. The advantage is that you then can pass an arbitrary structure containing initialization values to the lm63 driver. In your case, this could include the PWM polarity, the conversion rate, the tachometer input / alert output setup, etc. This has all the advantages:
* You have access to the lm63 driver facilities (register names, etc.)
* Most of the setup code can go away after initialization if it is tagged properly.
* You don't define a user-space interface so it doesn't have to follow any standard and can be changed later as needed.
* You no longer rely on user-space for device initialization, so nothing can go wrong if user-space doesn't come up for some reason.
It does have some disadvantages:
* The decision of how to configure the device must be done in the kernel, in the platform code that runs in the early bootup phase. What if the proper setting is stored in a file, based on what fan the user connected? Or depends on a hardware revision that's stored somewhere that's difficult to access from the kernel at this point in the boot process. Say it's behind an FPGA, and the image for the FPGA is loaded from userspace after boot? It's far easier to get at and change configuration information from userspace than it is to compile it into the kernel.
* All the code to do this needs to be in the kernel, which is the hardest to keep in sync with new developments, because it changes the most. An initscript with a bunch of cat commands is much less likely to stop working after changing kernels than a kernel patch is to no longer apply cleanly and work. A lot more people have the skill to change a filename in a script than to update kernel patches.
* Nothing can be changed while the system is running. Us developers need tools too. I didn't add these controls because they were there, I added them because I wanted to change them. There are other controls I didn't add, because I haven't cared about them. If these controls are something I wanted to change, there is probably someone else who will too.
* And the big one. Every time you want to change something, you need to compile and re-flash an entire new kernel! If I want to tell someone the lm63 setup for their board with a different pwm connection setup, I can just give them an echo command to set it, with linux and sysfs. If it's platform code (or vxworks) I have to compile an entirely new kernel for them to flash. No one likes the vxworks way of doing things, and this would be re-creating that. It's just a nightmare to work with, for development, maintenance and support.
_______________________________________________
lm-sensors mailing list
[email protected]
http://lists.lm-sensors.org/mailman/listinfo/lm-sensors
Hi Trent,
I've voluntarily stripped the parts where I think my reply wouldn't add anything to the discussion.
Quoted 9 lines · Trent Piepho
Quoted 2 lines · Jean Delvare
I fail to see how this matters. You'll have to know what register values to change anyway. How we change them is an implementation detail.
Because the code that knows what needs to be set runs from userspace, after the system has booted and the lm63 driver is loaded. At that point you can't set it with the firmware (if there was firmware) or platform code. If the setup needed in in a file, how can platform code read the contents of this file?
Maybe you're thinking the wrong way around? You start from the fact that you made the configuration data available to user-space only, and from that you conclude that we must find a way to program the LM63 from user-space. You said that you were designing the system yourself, so presumably you have a lot of control over it. Can't you make it such that the configuration data is easily available from kernel-space, so that the hardware setup can happen where it belongs, in the kernel?
Quoted 2 lines
What's wrong with embedded developers and power users? Linux isn't just for desktops.
There's nothing wrong with embedded developers and power users, as long as we don't let them design the interfaces ;) Historically, lm-sensors was written by people with great electronics skills, but who didn't have desktop users (or, more generally speaking, wide use) in mind. The result is that it took us several years to design a new interface that is desktop-application-friendly and update the whole code accordingly. I'm just trying to avoid making the same mistake again.
Quoted 7 lines
Quoted 3 lines
confusion with regards to the sysfs interface. We've been claiming that the sysfs interface to hardware monitoring devices was essentially safe to use, and that would no longer be always true.
I'm not sure what's less safe here. With the existing interface I could set the pwm1 value to 0 and start a fire. The CPU would at least lose its magic smoke. I don't think there is any setting I added more dangerous than that.
Oh well, you're right. PWM settings are not safe.
Quoted 4 lines
(...)
And use i2cset? You're back to looking up hex codes in the datasheet. I can remember echo 1 >pwm1_polarity, I can't remember the i2cset command to change the polarity. Do you know it off the top of your head?
I can't, but why would I? That's your system, not mine. You'd write a script running the commands, properly documented, once and for all, so there's nothing you need to remember.
Oh, BTW, I can't remember, does pwm1_polarity = 1 mean "reverse polarity" or "high polarity"? See, it's not that clear either.
Quoted 3 lines
But it seems that you're of the option that nothing that is primarily aimed at hardware developers or embedded systems is acceptable. I do not see how one can possibly develop embedded systems under those conditions.
You are free to use local patches that help you during development. That doesn't mean that they need to make it into the upstream kernel. Just like you can use user-space tools which final users of your system won't need.
Quoted 7 lines
Quoted 3 lines
(...)
Not very robust... If you have several hardware monitoring devices, your lm63 chip could as well be hwmon1.The nice thing about embedded systems is you know what you need to worry about and what you don't. What's more, your hypothetical i2cset command would be a lot less robust. How would you find out what bus and address the chip is on?
That's the beauty of the new-style i2c infrastructure: you choose the i2c bus numbers yourself. So you actually know what bus the chip is on. As for the address, well the LM63 is hardwired at 0x4c so that's not an issue in your case. For a different chip, you would of course have to know the address, but I fail to see how this is a problem.
And if you would like i2cset to operate by bus name rather than bus number, well, I just happen to have implemented this last week:
http://lists.lm-sensors.org/pipermail/i2c/2008-April/003325.html
Quoted 2 lines
(...)
How do you do a read/modify/write of a single bit in a register with i2cset?
i2cset supports this since October 2004:
http://www.lm-sensors.org/changeset/2721
But anyway, what i2cset can do or not is hardly a problem. It can be improved if needed, or you can script around it, or you can build your own i2c-dev helper if you need.
Quoted 9 lines
Quoted 4 lines
(...)
I'd make this an info message. As I understand it, users set the PWM frequency only once so it shouldn't spam the logs in practice, and I think it's a useful information.I ending up playing with this one a lot. Some fans do not like the slow pwm frequencies. And some frequencies produce an audible, and quite annoying, sound. Higher frequencies tend to be better than lower ones, but that reduces the number of pwm steps. Lots of trade-offs involved.
Well, make it a debug message if you prefer, but then you should document the issue clearly in Documentation/hwmon/lm63.
Quoted 8 lines
Quoted 3 lines
(...)
Of course the underlying I2C adapter and its driver have to support this. I2C block read isn't too widely supported, the other two are.Hmm, sounds like a lot of trouble. I'm pretty sure that the i2c block read will work for me, but what if the adapter doesn't support it? It looks like the smbus layer doesn't emulate it for you with
read_byte_data(s)if the adapter doesn't have it, so you've got do that yourself.
Indeed, the smbus layer doesn't emulate it for you, and I am glad it doesn't! There are no semantics attached to the I2C transactions, so arbitrarily replacing a big one by several shorter ones would be plain wrong. In your case it may happen that an I2C block read of length N is equivalent to N SMBus byte reads, but for other devices both might produce different results.
Anyway, doing the loop yourself isn't that difficult, take a look at eeprom_update_client() in the eeprom driver for an example. Alternatively, you can make the extra feature only available if the underlying i2c adapter supports block transactions in the first place - but this will probably result in the same amount of code in the end. It's up to you to decide if the extra code is worth the speed gain.
Quoted 17 lines
Quoted 8 lines
Thinking about it, this seems to be a case where a new-style i2c driver would help. As this is an embedded system, presumably you have some dedicated platform code running at boot. The new i2c infrastructure added by David Brownell would let you instantiate your lm63 device explicitly from the platform code. The advantage is that you then can pass an arbitrary structure containing initialization values to the lm63 driver. In your case, this could include the PWM polarity, the conversion rate, the tachometer input / alert output setup, etc. This
Quoted 8 lines
has all the advantages:
* You have access to the lm63 driver facilities (register names, etc.)
* Most of the setup code can go away after initialization if it is tagged properly.
* You don't define a user-space interface so it doesn't have to follow any standard and can be changed later as needed.
* You no longer rely on user-space for device initialization, so nothing can go wrong if user-space doesn't come up for some reason.
Quoted 4 lines
It does have some disadvantages:
* The decision of how to configure the device must be done in the kernel, in the platform code that runs in the early bootup phase. What if the proper setting is stored in a file, based on what fan the user connected? Or
The settings which depend on what fan is connected (e.g. pwm1_freq) have sysfs files. That's something we already agreed upon. The remaining settings, which I say should go in the platform code and you say shouldn't, are things which only depend on hardware settings, which the user can't change. For this reason I don't see the problem of setting these things up in the kernel.
Quoted 5 lines
depends on a hardware revision that's stored somewhere that's difficult to access from the kernel at this point in the boot process. Say it's behind an FPGA, and the image for the FPGA is loaded from userspace after boot? It's far easier to get at and change configuration information from userspace than it is to compile it into the kernel.
If your design is such that you really need to rely on user-space to do low-level hardware configuration then I claim that your design is broken.
Quoted 7 lines
* All the code to do this needs to be in the kernel, which is the hardest to keep in sync with new developments, because it changes the most. An initscript with a bunch of cat commands is much less likely to stop working after changing kernels than a kernel patch is to no longer apply cleanly and work. A lot more people have the skill to change a filename in a script than to update kernel patches.
But there's zero reason why you'd need kernel patches if you get your code upstream (which it seems is what you are trying to do right now).
Quoted 6 lines
* Nothing can be changed while the system is running. Us developers need tools too. I didn't add these controls because they were there, I added them because I wanted to change them. There are other controls I didn't add, because I haven't cared about them. If these controls are something I wanted to change, there is probably someone else who will too.
Again, this is per design. Changing the pin usage or the fan polarity shouldn't happen at runtime, as it's intimately tied to the hardware. You developers have your local patches and i2cset if you really need that during the development stage.
Quoted 9 lines
* And the big one. Every time you want to change something, you need to compile and re-flash an entire new kernel! If I want to tell someone the lm63 setup for their board with a different pwm connection setup, I can just give them an echo command to set it, with linux and sysfs. If it's platform code (or vxworks) I have to compile an entirely new kernel for them to flash. No one likes the vxworks way of doing things, and this would be re-creating that. It's just a nightmare to work with, for development, maintenance and support.
I just don't get that. You only need to add platform data if there is new hardware. I can't believe that someone would be skilled enough to design and build new hardware, but wouldn't be able to add 3 lines of kernel code and flash the new kernel.
As a summary, you are free to ignore my suggestions. You just won't get my Acked-by on the patches which I think do the wrong thing. If you can find another developer to ack them, and if you ultimately manage to convince Mark M. Hoffman that your implementation is right, then all the best to you.
Signature · 6 lines
Jean Delvare _______________________________________________ lm-sensors mailing list [email protected] http://lists.lm-sensors.org/mailman/listinfo/lm-sensors
Quoted 8 lines · Jean Delvare
Quoted 6 lines · Trent Piepho
On Sun, 13 Apr 2008, Jean Delvare wrote:
Because the code that knows what needs to be set runs from userspace, after the system has booted and the lm63 driver is loaded. At that point you can't set it with the firmware (if there was firmware) or platform code. If the setup needed in in a file, how can platform code read the contents of this file?
Quoted 7 lines
Maybe you're thinking the wrong way around? You start from the fact that you made the configuration data available to user-space only, and from that you conclude that we must find a way to program the LM63 from user-space. You said that you were designing the system yourself, so presumably you have a lot of control over it. Can't you make it such that the configuration data is easily available from kernel-space, so that the hardware setup can happen where it belongs, in the kernel?
Well, no. Making data available to the kernel on early bootup is *a lot* harder than userspace. What would you do, write a new bootloader that had a flash filesystem driver that can mount a logging flash filesytem (which is not fast to mount, since it has to be scanned) just to read a config file and pass it to the kernel via some more firmware that would need to be written to create a flattened device tree? And even after writing and debugging all this, you're still stuck with doubling your bootup time by double-scanning the flash filesystem.
IMHO, doing system configuration later vs early is a better idea. And I'm not the only one who thinks so, from the initramfs docs:
The move to early userspace is necessary because finding and mounting the real root device is complex. Root partitions can span multiple devices (raid or separate journal). They can be out on the network (requiring dhcp, setting a specific MAC address, logging into a server, etc). They can live on removable media, with dynamically allocated major/minor numbers and persistent naming issues requiring a full udev implementation to sort out. They can be compressed, encrypted, copy-on-write, loopback mounted, strangely partitioned, and so on.
This kind of complexity (which inevitably includes policy) is rightly handled in userspace. Both klibc and busybox/uClibc are working on simple initramfs packages to drop into a kernel build.
The direction the kernel is going, IMHO rightly, is to do as much system configuration as possible from userspace. The only system configuration that needs to be compiled into the kernel is the minimum needed to get userspace up, and things like initramfs are there to get to userspace as soon as possible.
Quoted 4 lines
have desktop users (or, more generally speaking, wide use) in mind. The result is that it took us several years to design a new interface that is desktop-application-friendly and update the whole code accordingly. I'm just trying to avoid making the same mistake again.
By creating a desktop-application-friendly interface to begin with, or by not having an interface at all?
Quoted 5 lines
Quoted 3 lines
But it seems that you're of the option that nothing that is primarily aimed at hardware developers or embedded systems is acceptable. I do not see how one can possibly develop embedded systems under those conditions.
You are free to use local patches that help you during development.
It would have helped me even more if the code was already there because the last person who needed it sent it upstream.
Quoted 8 lines
Quoted 7 lines
Quoted 2 lines
Of course the underlying I2C adapter and its driver have to support this. I2C block read isn't too widely supported, the other two are.
Hmm, sounds like a lot of trouble. I'm pretty sure that the i2c block read will work for me, but what if the adapter doesn't support it? It looks like the smbus layer doesn't emulate it for you with
read_byte_data(s)if the adapter doesn't have it, so you've got do that yourself.
Quoted 6 lines
Indeed, the smbus layer doesn't emulate it for you, and I am glad it doesn't! There are no semantics attached to the I2C transactions, so arbitrarily replacing a big one by several shorter ones would be plain wrong. In your case it may happen that an I2C block read of length N is equivalent to N SMBus byte reads, but for other devices both might produce different results.
I was thinking of i2c_smbus_read_i2c_block_data(), not arbitrary i2c transactions. You have your basic smbus register read, were the master sends the register address then does a repeated start and reads a byte. That's one of the specific i2c transactions that smbus documents, and all some controllers and/or drivers support.
Now, a great many i2c/smbus devices have an auto incrementing index register, so if you read another byte you get the next register, and so on.
So, one could have a function called "read contiguous block of smbus registers", which is something a lot of drivers do, even if they just code it as a bunch of random reads. This function would use the most efficient transaction(s) supported by the controller, which could get all the registers one multi-byte read, or do individual smbus byte reads for each register, or two bytes at a time, depending on the controller's abilities.
Quoted 11 lines
Quoted 4 lines
It does have some disadvantages:
* The decision of how to configure the device must be done in the kernel, in the platform code that runs in the early bootup phase. What if the proper setting is stored in a file, based on what fan the user connected? OrThe settings which depend on what fan is connected (e.g.
pwm1_freq) have sysfs files. That's something we already agreed upon. The remaining settings, which I say should go in the platform code and you say shouldn't, are things which only depend on hardware settings, which the user can't change. For this reason I don't see the problem of setting these things up in the kernel.
No so. For instance, pwm control and fan speed monitoring are mutually exclusive. If the device is an a server room, fan noise may be irrelevant and monitoring fan operation with SNMP or something of that nature might be desirable. On the other hand, if the device is on a user's desk, quieting a noisy fan could by of the utmost importance and an the user is unlikely to have or care to use some network device health monitoring system.
So you see, fan1_enable depends not just on the fan connected, but where the device is located. How can one compile the device's location and the user's sensitivity to noise into the kernel?
Quoted 4 lines
I just don't get that. You only need to add platform data if there is new hardware. I can't believe that someone would be skilled enough to design and build new hardware, but wouldn't be able to add 3 lines of kernel code and flash the new kernel.
You'd be surprised at the changes a prototype undergoes during development and the different jobs of the people who want it to do something with it.
_______________________________________________
lm-sensors mailing list
[email protected]
http://lists.lm-sensors.org/mailman/listinfo/lm-sensors
Hi Trent,
Not sure if I should reply as it seems we're not going anywhere, but...
Quoted 7 lines · Trent Piepho
IMHO, doing system configuration later vs early is a better idea. And I'm not the only one who thinks so, from the initramfs docs:
(quoted diff) +0
-0The move to early userspace is necessary because finding and mounting the real root device is complex. Root partitions can span multiple devices (raid or separate journal). They can be
Review
Quoted 10 lines
(quoted diff) +0
-0out on the network (requiring dhcp, setting a specific MAC address, logging into a server, etc). They can live on removable media, with dynamically allocated major/minor numbers and persistent naming issues requiring a full udev implementation to sort out. They can be compressed, encrypted, copy-on-write, loopback mounted, strangely partitioned, and so on. This kind of complexity (which inevitably includes policy) is rightly handled in userspace. Both klibc and busybox/uClibc are working on simple initramfs packages to drop into a kernel build.
This sounds sane, but OTOH I can hardly find a relation between your quote above and the problem at hand.
Quoted 5 lines
The direction the kernel is going, IMHO rightly, is to do as much system configuration as possible from userspace. The only system configuration that needs to be compiled into the kernel is the minimum needed to get userspace up, and things like initramfs are there to get to userspace as soon as possible.
I don't see the kernel going into that direction that much, other than by delegating the creation of device nodes to udev. Things that are directly related to the hardware, like IRQ routing, is still handled inside the kernel.
Quoted 7 lines
Quoted 4 lines
have desktop users (or, more generally speaking, wide use) in mind. The result is that it took us several years to design a new interface that is desktop-application-friendly and update the whole code accordingly. I'm just trying to avoid making the same mistake again.
By creating a desktop-application-friendly interface to begin with, or by not having an interface at all?
By having an interface which user-space applications can build on, with consistent semantics, and which doesn't change every other month.
Quoted 4 lines
Quoted 1 line
You are free to use local patches that help you during development.
It would have helped me even more if the code was already there because the last person who needed it sent it upstream.
It would also help if you would send us a patch with the changes I said I agree should go into the kernel. That way the next person who will need features similar to what you needed would only have a small part to write instead of a big part.
Quoted 17 lines
Quoted 6 lines
Indeed, the smbus layer doesn't emulate it for you, and I am glad it doesn't! There are no semantics attached to the I2C transactions, so arbitrarily replacing a big one by several shorter ones would be plain wrong. In your case it may happen that an I2C block read of length N is equivalent to N SMBus byte reads, but for other devices both might produce different results.
I was thinking of
i2c_smbus_read_i2c_block_data(), not arbitrary i2c transactions. You have your basic smbus register read, were the master sends the register address then does a repeated start and reads a byte. That's one of the specific i2c transactions that smbus documents, and all some controllers and/or drivers support.Now, a great many i2c/smbus devices have an auto incrementing index register, so if you read another byte you get the next register, and so on.
Quoted 7 lines
So, one could have a function called "read contiguous block of smbus registers", which is something a lot of drivers do, even if they just code it as a bunch of random reads. This function would use the most efficient transaction(s) supported by the controller, which could get all the registers one multi-byte read, or do individual smbus byte reads for each register, or two bytes at a time, depending on the controller's abilities.
Which basically means moving part of the code from eeprom.c:eeprom_update_client() to i2c-core. If enough drivers need this, then sure, why not. Just send a patch. But first, please test that the LM63 really supports that.
But you should keep in mind that the problem isn't as easy at it looks. SMBus added semantics on top of certain arbitrary I2C transactions. "SMBus Read Byte" is one of these. I2C block reads, OTOH, aren't specified in SMBus (don't get fooled by the Linux function name), so "SMBus-compliant" devices aren't suppose to support them at all. Instead you have "SMBus Block Read", which has a different format, such that devices must explicitly support it - not like the auto-increment. "SMBus Receive Byte" and to some extent even "SMBus Read Word" do comply with the spirit of auto-increment, however the former isn't safe to use on multi-master buses (and not necessarily on single-master buses without proper locking, which didn't prevent us to use it in the eeprom driver though).
So the idea of trading SMBus Read Byte transactions for a more efficient I2C block read is smart and happens to work for some chips, but it is not relying on any specification. All this needs proper documentation if we make it a public function part of the i2c-core API.
Quoted 12 lines
Quoted 11 lines
Quoted 4 lines
It does have some disadvantages:
* The decision of how to configure the device must be done in the kernel, in the platform code that runs in the early bootup phase. What if the proper setting is stored in a file, based on what fan the user connected? OrThe settings which depend on what fan is connected (e.g.
pwm1_freq) have sysfs files. That's something we already agreed upon. The remaining settings, which I say should go in the platform code and you say shouldn't, are things which only depend on hardware settings, which the user can't change. For this reason I don't see the problem of setting these things up in the kernel.
Quoted 2 lines
No so. For instance, pwm control and fan speed monitoring are mutually exclusive.
How that? The way I read the LM63 datasheet, fan speed monitoring is exclusive with alert output, but PWM control is always available.
Quoted 10 lines
If the device is an a server room, fan noise may be irrelevant and monitoring fan operation with SNMP or something of that nature might be desirable. On the other hand, if the device is on a user's desk, quieting a noisy fan could by of the utmost importance and an the user is unlikely to have or care to use some network device health monitoring system.
So you see,
fan1_enabledepends not just on the fan connected, but where the device is located. How can one compile the device's location and the user's sensitivity to noise into the kernel?
In the case of the LM63, fan1_enable depends on whether the _hardware_ was designed to have a fan connected to the pin _or_ to have instead some device processing the alert signal. It's an either-or thing and it's decided by the hardware. Unless you solder new hardware live on your system, changing the value of fan1_enable for an LM63 at runtime just doesn't make sense.
Signature · 6 lines
Jean Delvare _______________________________________________ lm-sensors mailing list [email protected] http://lists.lm-sensors.org/mailman/listinfo/lm-sensors