hwmon: (lm92) Drop function macros
Guenter Roeck
maintainerPATCH v2 3/4 hwmon: (lm92) Drop function macros
Function macros obfuscate code and increase code size, so drop them.
---
v2: Drop comma after t_num_regs
Declare register array as const and with explicit size
drivers/hwmon/lm92.c | 165 +++++++++++++++++++++++++------------------------- 1 file changed, 82 insertions(+), 83 deletions(-)
drivers/hwmon/lm92.c +81 -83
@@ -89,6 +89,23 @@ static inline u8 ALARMS_FROM_REG(s16 reg)return reg &0x0007 ; } +enum temp_index { + t_input, + t_crit, + t_min, + t_max, + t_hyst, + t_num_regs +}; + +static const u8 regs[t_num_regs] = { + [t_input] =LM92_REG_TEMP , + [t_crit] =LM92_REG_TEMP_CRIT , + [t_min] =LM92_REG_TEMP_LOW , + [t_max] =LM92_REG_TEMP_HIGH , + [t_hyst] =LM92_REG_TEMP_HYST , +}; +/* Client data (each client gets its own) */ struct lm92_data {struct device *hwmon_dev;
@@ -97,10 +114,9 @@ struct lm92_data {unsigned long last_updated;/* in jiffies */ /* registers values */ -s16 temp1_input, temp1_crit, temp1_min, temp1_max, temp1_hyst; +s16 temp[t_num_regs];/* index with enum temp_index */ }; -/* * Sysfs attributes and callback functions */
@@ -109,23 +125,17 @@ static struct lm92_data *lm92_update_device(struct device *dev) {struct i2c_client *client =to_i2c_client (dev);struct lm92_data *data =i2c_get_clientdata (client); +int i;mutex_lock (&data->update_lock);if (time_after (jiffies, data->last_updated +HZ ) || !data->valid) {dev_dbg (&client->dev,"Updating lm92 data\n" ); - data->temp1_input =i2c_smbus_read_word_swapped (client, -LM92_REG_TEMP ); - data->temp1_hyst =i2c_smbus_read_word_swapped (client, -LM92_REG_TEMP_HYST ); - data->temp1_crit =i2c_smbus_read_word_swapped (client, -LM92_REG_TEMP_CRIT ); - data->temp1_min =i2c_smbus_read_word_swapped (client, -LM92_REG_TEMP_LOW ); - data->temp1_max =i2c_smbus_read_word_swapped (client, -LM92_REG_TEMP_HIGH ); - +for (i =0 ; i < t_num_regs; i++) { + data->temp[i] +i2c_smbus_read_word_swapped (client, regs[i]); + } data->last_updated = jiffies; data->valid =1 ; }
@@ -135,66 +145,56 @@ static struct lm92_data *lm92_update_device(struct device *dev)return data; } -#define show_temp (value) \ -static ssize_t show_##value(struct device *dev, struct device_attribute *attr, \ - char *buf) \ -{ \ - struct lm92_data *data = lm92_update_device(dev); \ - return sprintf(buf, "%d\n", TEMP_FROM_REG(data->value)); \ -} -show_temp (temp1_input); -show_temp (temp1_crit); -show_temp (temp1_min); -show_temp (temp1_max); - -#define set_temp (value, reg) \ -static ssize_t set_##value(struct device *dev, struct device_attribute *attr, \ - const char *buf, \ - size_t count) \ -{ \ - struct i2c_client *client = to_i2c_client(dev); \ - struct lm92_data *data = i2c_get_clientdata(client); \ - long val; \ - int err = kstrtol(buf, 10, &val); \ - if (err) \ - return err; \ -\ - mutex_lock(&data->update_lock); \ - data->value = TEMP_TO_REG(val); \ - i2c_smbus_write_word_swapped(client, reg, data->value); \ - mutex_unlock(&data->update_lock); \ - return count; \ +static ssize_t show_temp (struct device *dev,struct device_attribute *devattr, +char *buf) +{ +struct sensor_device_attribute *attr =to_sensor_dev_attr (devattr); +struct lm92_data *data =lm92_update_device (dev); + +return sprintf (buf,"%d\n" ,TEMP_FROM_REG (data->temp[attr->index])); } -set_temp (temp1_crit,LM92_REG_TEMP_CRIT ); -set_temp (temp1_min,LM92_REG_TEMP_LOW ); -set_temp (temp1_max,LM92_REG_TEMP_HIGH ); -static ssize_t show_temp1_crit_hyst (struct device *dev, -struct device_attribute *attr,char *buf) +static ssize_t set_temp (struct device *dev,struct device_attribute *devattr, +const char *buf,size_t count) { -struct lm92_data *data =lm92_update_device (dev); -return sprintf (buf,"%d\n" ,TEMP_FROM_REG (data->temp1_crit) - -TEMP_FROM_REG (data->temp1_hyst)); +struct sensor_device_attribute *attr =to_sensor_dev_attr (devattr); +struct i2c_client *client =to_i2c_client (dev); +struct lm92_data *data =i2c_get_clientdata (client); +int nr = attr->index; +long val; +int err =kstrtol (buf,10 , &val); +if (err) +return err; + +mutex_lock (&data->update_lock); + data->temp[nr] =TEMP_TO_REG (val); +i2c_smbus_write_word_swapped (client, regs[nr], data->temp[nr]); +mutex_unlock (&data->update_lock); +return count; } -static ssize_t show_temp1_max_hyst (struct device *dev, -struct device_attribute *attr,char *buf) + +static ssize_t show_temp_hyst (struct device *dev, +struct device_attribute *devattr,char *buf) { +struct sensor_device_attribute *attr =to_sensor_dev_attr (devattr);struct lm92_data *data =lm92_update_device (dev); -return sprintf (buf,"%d\n" ,TEMP_FROM_REG (data->temp1_max) - -TEMP_FROM_REG (data->temp1_hyst)); +return sprintf (buf,"%d\n" ,TEMP_FROM_REG (data->temp[attr->index]) + -TEMP_FROM_REG (data->temp[t_hyst])); } -static ssize_t show_temp1_min_hyst (struct device *dev, -struct device_attribute *attr,char *buf) + +static ssize_t show_temp_min_hyst (struct device *dev, +struct device_attribute *attr,char *buf) {struct lm92_data *data =lm92_update_device (dev); -return sprintf (buf,"%d\n" ,TEMP_FROM_REG (data->temp1_min) - +TEMP_FROM_REG (data->temp1_hyst)); +return sprintf (buf,"%d\n" ,TEMP_FROM_REG (data->temp[t_min]) + +TEMP_FROM_REG (data->temp[t_hyst])); } -static ssize_t set_temp1_crit_hyst (struct device *dev, -struct device_attribute *attr, -const char *buf,size_t count) +static ssize_t set_temp_hyst (struct device *dev, +struct device_attribute *devattr, +const char *buf,size_t count) { +struct sensor_device_attribute *attr =to_sensor_dev_attr (devattr);struct i2c_client *client =to_i2c_client (dev);struct lm92_data *data =i2c_get_clientdata (client);long val;
@@ -205,9 +205,9 @@ static ssize_t set_temp1_crit_hyst(struct device *dev,return err;mutex_lock (&data->update_lock); - data->temp1_hyst =TEMP_FROM_REG (data->temp1_crit) - val; + data->temp[t_hyst] =TEMP_FROM_REG (data->temp[attr->index]) - val;i2c_smbus_write_word_swapped (client,LM92_REG_TEMP_HYST , -TEMP_TO_REG (data->temp1_hyst)); +TEMP_TO_REG (data->temp[t_hyst]));mutex_unlock (&data->update_lock);return count; }
@@ -216,7 +216,7 @@ static ssize_t show_alarms(struct device *dev, struct device_attribute *attr, char *buf) {struct lm92_data *data =lm92_update_device (dev); -return sprintf (buf,"%d\n" ,ALARMS_FROM_REG (data->temp1_input)); +return sprintf (buf,"%d\n" ,ALARMS_FROM_REG (data->temp[t_input])); } static ssize_t show_alarm(struct device *dev, struct device_attribute *attr,
@@ -224,26 +224,25 @@ static ssize_t show_alarm(struct device *dev, struct device_attribute *attr, {int bitnr =to_sensor_dev_attr (attr)->index;struct lm92_data *data =lm92_update_device (dev); -return sprintf (buf,"%d\n" , (data->temp1_input >> bitnr) &1 ); +return sprintf (buf,"%d\n" , (data->temp[t_input] >> bitnr) &1 ); } -static DEVICE_ATTR(temp1_input, S_IRUGO, show_temp1_input, NULL); -static DEVICE_ATTR(temp1_crit, S_IWUSR | S_IRUGO, show_temp1_crit, - set_temp1_crit); -static DEVICE_ATTR(temp1_crit_hyst, S_IWUSR | S_IRUGO, show_temp1_crit_hyst, - set_temp1_crit_hyst); -static DEVICE_ATTR(temp1_min, S_IWUSR | S_IRUGO, show_temp1_min, - set_temp1_min); -static DEVICE_ATTR(temp1_min_hyst, S_IRUGO, show_temp1_min_hyst, NULL); -static DEVICE_ATTR(temp1_max, S_IWUSR | S_IRUGO, show_temp1_max, - set_temp1_max); -static DEVICE_ATTR(temp1_max_hyst, S_IRUGO, show_temp1_max_hyst, NULL); +static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, show_temp, NULL, t_input); +static SENSOR_DEVICE_ATTR(temp1_crit, S_IWUSR | S_IRUGO, show_temp, set_temp, + t_crit); +static SENSOR_DEVICE_ATTR(temp1_crit_hyst, S_IWUSR | S_IRUGO, show_temp_hyst, + set_temp_hyst, t_crit); +static SENSOR_DEVICE_ATTR(temp1_min, S_IWUSR | S_IRUGO, show_temp, set_temp, + t_min); +static DEVICE_ATTR(temp1_min_hyst, S_IRUGO, show_temp_min_hyst, NULL); +static SENSOR_DEVICE_ATTR(temp1_max, S_IWUSR | S_IRUGO, show_temp, set_temp, + t_max); +static SENSOR_DEVICE_ATTR(temp1_max_hyst, S_IRUGO, show_temp_hyst, NULL, t_max); static DEVICE_ATTR(alarms, S_IRUGO, show_alarms, NULL); static SENSOR_DEVICE_ATTR(temp1_crit_alarm, S_IRUGO, show_alarm, NULL, 2); static SENSOR_DEVICE_ATTR(temp1_min_alarm, S_IRUGO, show_alarm, NULL, 0); static SENSOR_DEVICE_ATTR(temp1_max_alarm, S_IRUGO, show_alarm, NULL, 1); -/* * Detection and registration */
@@ -316,13 +315,13 @@ static int max6635_check(struct i2c_client *client) }static struct attribute *lm92_attributes[] = { - &dev_attr_temp1_input.attr, - &dev_attr_temp1_crit.attr, - &dev_attr_temp1_crit_hyst.attr, - &dev_attr_temp1_min.attr, + &sensor_dev_attr_temp1_input.dev_attr.attr, + &sensor_dev_attr_temp1_crit.dev_attr.attr, + &sensor_dev_attr_temp1_crit_hyst.dev_attr.attr, + &sensor_dev_attr_temp1_min.dev_attr.attr, &dev_attr_temp1_min_hyst.attr, - &dev_attr_temp1_max.attr, - &dev_attr_temp1_max_hyst.attr, + &sensor_dev_attr_temp1_max.dev_attr.attr, + &sensor_dev_attr_temp1_max_hyst.dev_attr.attr, &dev_attr_alarms.attr, &sensor_dev_attr_temp1_crit_alarm.dev_attr.attr, &sensor_dev_attr_temp1_min_alarm.dev_attr.attr,
Signature · 7 lines
1.7.9.7 _______________________________________________ lm-sensors mailing list [email protected] http://lists.lm-sensors.org/mailman/listinfo/lm-sensors
Hi Guenter,
Quoted 12 lines · Guenter Roeck
Function macros obfuscate code and increase code size, so drop them.
Signed-off-by Guenter Roeck <[email protected]>---
v2: Drop comma aftert_num_regs
Declare register array as const and with explicit sizedrivers/hwmon/lm92.c | 165 +++++++++++++++++++++++++------------------------- 1 file changed, 82 insertions(+), 83 deletions(-)(...)
drivers/hwmon/lm92.c +2
-0+static ssize_t set_temp (struct device *dev,struct device_attribute *devattr, +const char *buf,size_t count)
drivers/hwmon/lm92.c static struct lm92_data *lm92_update_device(struct device *dev) · line 165
Quoted 10 lines
drivers/hwmon/lm92.c +6
-3{ -struct lm92_data *data =lm92_update_device (dev); -return sprintf (buf,"%d\n" ,TEMP_FROM_REG (data->temp1_crit) - -TEMP_FROM_REG (data->temp1_hyst)); +struct sensor_device_attribute *attr =to_sensor_dev_attr (devattr); +struct i2c_client *client =to_i2c_client (dev); +struct lm92_data *data =i2c_get_clientdata (client); +int nr = attr->index; +long val; +int err =kstrtol (buf,10 , &val);
I would separate variable declaration from actual code here, that would be easier to read and would be consistent with the code in set_temp_hyst().
drivers/hwmon/lm92.c static struct lm92_data *lm92_update_device(struct device *dev) · line 174
Quoted 9 lines
drivers/hwmon/lm92.c +8
-0+if (err) +return err; + +mutex_lock (&data->update_lock); + data->temp[nr] =TEMP_TO_REG (val); +i2c_smbus_write_word_swapped (client, regs[nr], data->temp[nr]); +mutex_unlock (&data->update_lock); +return count; }
Other than this it looks good to me, although I can't test. Do you happen to have a register dump from an LM92, MAX6635 or any compatible chip?
Signature · 7 lines
Jean Delvare SUSE L3 Support _______________________________________________ lm-sensors mailing list [email protected] http://lists.lm-sensors.org/mailman/listinfo/lm-sensors
Guenter Roeck
maintainerQuoted 21 lines · Jean Delvare
Hi Guenter,
Quoted 18 lines · Guenter Roeck
Function macros obfuscate code and increase code size, so drop them.
Signed-off-by Guenter Roeck <[email protected]>---
v2: Drop comma aftert_num_regs
Declare register array as const and with explicit sizedrivers/hwmon/lm92.c | 165 +++++++++++++++++++++++++------------------------- 1 file changed, 82 insertions(+), 83 deletions(-)(...)
drivers/hwmon/lm92.c +4
-3+static ssize_t set_temp (struct device *dev,struct device_attribute *devattr, +const char *buf,size_t count) { -struct lm92_data *data =lm92_update_device (dev); -return sprintf (buf,"%d\n" ,TEMP_FROM_REG (data->temp1_crit) - -TEMP_FROM_REG (data->temp1_hyst)); +struct sensor_device_attribute *attr =to_sensor_dev_attr (devattr); +struct i2c_client *client =to_i2c_client (dev);
Quoted 9 lines
drivers/hwmon/lm92.c static struct lm92_data *lm92_update_device(struct device *dev) · line 165
Quoted 4 lines
drivers/hwmon/lm92.c +4
-0+struct lm92_data *data =i2c_get_clientdata (client); +int nr = attr->index; +long val; +int err =kstrtol (buf,10 , &val);I would separate variable declaration from actual code here, that would be easier to read and would be consistent with the code in
set_temp_hyst().
Sure, I'll do that.
Quoted 5 lines
Quoted 5 lines
drivers/hwmon/lm92.c +5
-0+if (err) +return err; + +mutex_lock (&data->update_lock); + data->temp[nr] =TEMP_TO_REG (val);
Quoted 9 lines
drivers/hwmon/lm92.c static struct lm92_data *lm92_update_device(struct device *dev) · line 174
Quoted 4 lines
drivers/hwmon/lm92.c +3
-0+i2c_smbus_write_word_swapped (client, regs[nr], data->temp[nr]); +mutex_unlock (&data->update_lock); +return count; }Other than this it looks good to me, although I can't test. Do you happen to have a register dump from an LM92, MAX6635 or any compatible chip?
Not readily available, but I have a test board so I can create one. I'll do that tonight. Let me know if you need register dumps for any of the other chips I touched recently.
Quoted 2 lines
Reviewed-by Jean Delvare <[email protected]>
Thanks!
Guenter
_______________________________________________
lm-sensors mailing list
[email protected]
http://lists.lm-sensors.org/mailman/listinfo/lm-sensors
Guenter Roeck
maintainerQuoted 30 lines · Jean Delvare
Hi Guenter,
Quoted 12 lines · Guenter Roeck
Function macros obfuscate code and increase code size, so drop them.
Signed-off-by Guenter Roeck <[email protected]>---
v2: Drop comma aftert_num_regs
Declare register array as const and with explicit sizedrivers/hwmon/lm92.c | 165 +++++++++++++++++++++++++------------------------- 1 file changed, 82 insertions(+), 83 deletions(-)(...)
drivers/hwmon/lm92.c +2
-0+static ssize_t set_temp (struct device *dev,struct device_attribute *devattr, +const char *buf,size_t count)drivers/hwmon/lm92.c static struct lm92_data *lm92_update_device(struct device *dev) · line 165
Quoted 10 lines
drivers/hwmon/lm92.c +6
-3{ -struct lm92_data *data =lm92_update_device (dev); -return sprintf (buf,"%d\n" ,TEMP_FROM_REG (data->temp1_crit) - -TEMP_FROM_REG (data->temp1_hyst)); +struct sensor_device_attribute *attr =to_sensor_dev_attr (devattr); +struct i2c_client *client =to_i2c_client (dev); +struct lm92_data *data =i2c_get_clientdata (client); +int nr = attr->index; +long val; +int err =kstrtol (buf,10 , &val);I would separate variable declaration from actual code here, that would be easier to read and would be consistent with the code in
set_temp_hyst().
Quoted 14 lines
drivers/hwmon/lm92.c static struct lm92_data *lm92_update_device(struct device *dev) · line 174
Quoted 9 lines
drivers/hwmon/lm92.c +8
-0+if (err) +return err; + +mutex_lock (&data->update_lock); + data->temp[nr] =TEMP_TO_REG (val); +i2c_smbus_write_word_swapped (client, regs[nr], data->temp[nr]); +mutex_unlock (&data->update_lock); +return count; }Other than this it looks good to me, although I can't test. Do you happen to have a register dump from an LM92, MAX6635 or any compatible chip?
Here you are, for LM92:
0,8 1,9 2,a 3,b 4,c 5,d 6,e 7,f
00: 380c b800 0001 0028 0005 0020 0020 0180 08: 380c b800 0001 0028 0005 0020 0020 0180 10: 380c b800 0001 0028 0005 0020 0020 0180 18: 380c b800 0001 0028 0005 0020 0020 0180 20: 380c b800 0001 0028 0005 0020 0020 0180 28: 380c b800 0001 0028 0005 0020 0020 0180 30: 380c b800 0001 0028 0005 0020 0020 0180 38: 380c b800 0001 0028 0005 0020 0020 0180 40: 380c b800 0001 0028 0005 0020 0020 0180 48: 380c b800 0001 0028 0005 0020 0020 0180 50: 380c b800 0001 0028 0005 0020 0020 0180 58: 380c b800 0001 0028 0005 0020 0020 0180 60: 380c b800 0001 0028 0005 0020 0020 0180 68: 380c b800 0001 0028 0005 0020 0020 0180 70: 380c b800 0001 0028 0005 0020 0020 0180 78: 380c b800 0001 0028 0005 0020 0020 0180 80: 380c b800 0001 0028 0005 0020 0020 0180 88: 380c b800 0001 0028 0005 0020 0020 0180 90: 380c b800 0001 0028 0005 0020 0020 0180 98: 380c b800 0001 0028 0005 0020 0020 0180
a0: 380c b800 0001 0028 0005 0020 0020 0180
a8: 380c b800 0001 0028 0005 0020 0020 0180
b0: 380c b800 0001 0028 0005 0020 0020 0180
b8: 380c b800 0001 0028 0005 0020 0020 0180
c0: 380c b800 0001 0028 0005 0020 0020 0180
c8: 380c b800 0001 0028 0005 0020 0020 0180
d0: 380c b800 0001 0028 0005 0020 0020 0180
d8: 380c b800 0001 0028 0005 0020 0020 0180
e0: 380c b800 0001 0028 0005 0020 0020 0180
e8: 380c b800 0001 0028 0005 0020 0020 0180
f0: 380c b800 0001 0028 0005 0020 0020 0180
f8: 380c b800 0001 0028 0005 0020 0020 0180
Matching output from sensors command:
lm92-i2c-9-48
Adapter: i2c-diolan-u2c at bus 003 device 013
temp1: +24.1°C (low = +10.0°C)
(high = +64.0°C, hyst = +62.0°C)
(crit = +80.0°C, hyst = +78.0°C)Guenter
_______________________________________________
lm-sensors mailing list
[email protected]
http://lists.lm-sensors.org/mailman/listinfo/lm-sensors