Skip to content

Commit 9711bcd

Browse files
committed
hwmon: (lm80) Convert temperature display function macros into functions
Convert temperature display function macros into functions to reduce code size and improve code readability. Code size reduction is about 2k on x86_64. Signed-off-by: Guenter Roeck <linux@roeck-us.net>
1 parent 688a317 commit 9711bcd

File tree

1 file changed

+59
-75
lines changed

1 file changed

+59
-75
lines changed

drivers/hwmon/lm80.c

+59-75
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,23 @@ static inline unsigned char FAN_TO_REG(unsigned rpm, unsigned div)
9292

9393
#define DIV_FROM_REG(val) (1 << (val))
9494

95+
enum temp_index {
96+
t_input = 0,
97+
t_hot_max,
98+
t_hot_hyst,
99+
t_os_max,
100+
t_os_hyst,
101+
t_num_temp
102+
};
103+
104+
static const u8 temp_regs[t_num_temp] = {
105+
[t_input] = LM80_REG_TEMP,
106+
[t_hot_max] = LM80_REG_TEMP_HOT_MAX,
107+
[t_hot_hyst] = LM80_REG_TEMP_HOT_HYST,
108+
[t_os_max] = LM80_REG_TEMP_OS_MAX,
109+
[t_os_hyst] = LM80_REG_TEMP_OS_HYST,
110+
};
111+
95112
/*
96113
* Client data (each client gets its own)
97114
*/
@@ -109,11 +126,7 @@ struct lm80_data {
109126
u8 fan[2]; /* Register value */
110127
u8 fan_min[2]; /* Register value */
111128
u8 fan_div[2]; /* Register encoding, shifted right */
112-
s16 temp; /* Register values */
113-
s16 temp_hot_max; /* Register value, left shifted */
114-
s16 temp_hot_hyst; /* Register value, left shifted */
115-
s16 temp_os_max; /* Register value, left shifted */
116-
s16 temp_os_hyst; /* Register value, left shifted */
129+
s16 temp[t_num_temp]; /* Register values, normalized to 16 bit */
117130
u16 alarms; /* Register encoding, combined */
118131
};
119132

@@ -288,50 +301,34 @@ static ssize_t set_fan_div(struct device *dev, struct device_attribute *attr,
288301
return count;
289302
}
290303

291-
static ssize_t show_temp_input1(struct device *dev,
292-
struct device_attribute *attr, char *buf)
304+
static ssize_t show_temp(struct device *dev, struct device_attribute *devattr,
305+
char *buf)
293306
{
307+
struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
294308
struct lm80_data *data = lm80_update_device(dev);
295309
if (IS_ERR(data))
296310
return PTR_ERR(data);
297-
return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp));
311+
return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp[attr->index]));
298312
}
299313

300-
#define show_temp(suffix, value) \
301-
static ssize_t show_temp_##suffix(struct device *dev, \
302-
struct device_attribute *attr, char *buf) \
303-
{ \
304-
struct lm80_data *data = lm80_update_device(dev); \
305-
if (IS_ERR(data)) \
306-
return PTR_ERR(data); \
307-
return sprintf(buf, "%d\n", TEMP_FROM_REG(data->value)); \
308-
}
309-
show_temp(hot_max, temp_hot_max);
310-
show_temp(hot_hyst, temp_hot_hyst);
311-
show_temp(os_max, temp_os_max);
312-
show_temp(os_hyst, temp_os_hyst);
314+
static ssize_t set_temp(struct device *dev, struct device_attribute *devattr,
315+
const char *buf, size_t count)
316+
{
317+
struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
318+
struct lm80_data *data = dev_get_drvdata(dev);
319+
struct i2c_client *client = data->client;
320+
int nr = attr->index;
321+
long val;
322+
int err = kstrtol(buf, 10, &val);
323+
if (err < 0)
324+
return err;
313325

314-
#define set_temp(suffix, value, reg) \
315-
static ssize_t set_temp_##suffix(struct device *dev, \
316-
struct device_attribute *attr, const char *buf, size_t count) \
317-
{ \
318-
struct lm80_data *data = dev_get_drvdata(dev); \
319-
struct i2c_client *client = data->client; \
320-
long val; \
321-
int err = kstrtol(buf, 10, &val); \
322-
if (err < 0) \
323-
return err; \
324-
\
325-
mutex_lock(&data->update_lock); \
326-
data->value = TEMP_TO_REG(val); \
327-
lm80_write_value(client, reg, data->value >> 8); \
328-
mutex_unlock(&data->update_lock); \
329-
return count; \
326+
mutex_lock(&data->update_lock);
327+
data->temp[nr] = TEMP_TO_REG(val);
328+
lm80_write_value(client, temp_regs[nr], data->temp[nr] >> 8);
329+
mutex_unlock(&data->update_lock);
330+
return count;
330331
}
331-
set_temp(hot_max, temp_hot_max, LM80_REG_TEMP_HOT_MAX);
332-
set_temp(hot_hyst, temp_hot_hyst, LM80_REG_TEMP_HOT_HYST);
333-
set_temp(os_max, temp_os_max, LM80_REG_TEMP_OS_MAX);
334-
set_temp(os_hyst, temp_os_hyst, LM80_REG_TEMP_OS_HYST);
335332

336333
static ssize_t show_alarms(struct device *dev, struct device_attribute *attr,
337334
char *buf)
@@ -397,15 +394,15 @@ static SENSOR_DEVICE_ATTR(fan1_div, S_IWUSR | S_IRUGO,
397394
show_fan_div, set_fan_div, 0);
398395
static SENSOR_DEVICE_ATTR(fan2_div, S_IWUSR | S_IRUGO,
399396
show_fan_div, set_fan_div, 1);
400-
static DEVICE_ATTR(temp1_input, S_IRUGO, show_temp_input1, NULL);
401-
static DEVICE_ATTR(temp1_max, S_IWUSR | S_IRUGO, show_temp_hot_max,
402-
set_temp_hot_max);
403-
static DEVICE_ATTR(temp1_max_hyst, S_IWUSR | S_IRUGO, show_temp_hot_hyst,
404-
set_temp_hot_hyst);
405-
static DEVICE_ATTR(temp1_crit, S_IWUSR | S_IRUGO, show_temp_os_max,
406-
set_temp_os_max);
407-
static DEVICE_ATTR(temp1_crit_hyst, S_IWUSR | S_IRUGO, show_temp_os_hyst,
408-
set_temp_os_hyst);
397+
static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, show_temp, NULL, t_input);
398+
static SENSOR_DEVICE_ATTR(temp1_max, S_IWUSR | S_IRUGO, show_temp,
399+
set_temp, t_hot_max);
400+
static SENSOR_DEVICE_ATTR(temp1_max_hyst, S_IWUSR | S_IRUGO, show_temp,
401+
set_temp, t_hot_hyst);
402+
static SENSOR_DEVICE_ATTR(temp1_crit, S_IWUSR | S_IRUGO, show_temp,
403+
set_temp, t_os_max);
404+
static SENSOR_DEVICE_ATTR(temp1_crit_hyst, S_IWUSR | S_IRUGO, show_temp,
405+
set_temp, t_os_hyst);
409406
static DEVICE_ATTR(alarms, S_IRUGO, show_alarms, NULL);
410407
static SENSOR_DEVICE_ATTR(in0_alarm, S_IRUGO, show_alarm, NULL, 0);
411408
static SENSOR_DEVICE_ATTR(in1_alarm, S_IRUGO, show_alarm, NULL, 1);
@@ -451,11 +448,11 @@ static struct attribute *lm80_attrs[] = {
451448
&sensor_dev_attr_fan2_input.dev_attr.attr,
452449
&sensor_dev_attr_fan1_div.dev_attr.attr,
453450
&sensor_dev_attr_fan2_div.dev_attr.attr,
454-
&dev_attr_temp1_input.attr,
455-
&dev_attr_temp1_max.attr,
456-
&dev_attr_temp1_max_hyst.attr,
457-
&dev_attr_temp1_crit.attr,
458-
&dev_attr_temp1_crit_hyst.attr,
451+
&sensor_dev_attr_temp1_input.dev_attr.attr,
452+
&sensor_dev_attr_temp1_max.dev_attr.attr,
453+
&sensor_dev_attr_temp1_max_hyst.dev_attr.attr,
454+
&sensor_dev_attr_temp1_crit.dev_attr.attr,
455+
&sensor_dev_attr_temp1_crit_hyst.dev_attr.attr,
459456
&dev_attr_alarms.attr,
460457
&sensor_dev_attr_in0_alarm.dev_attr.attr,
461458
&sensor_dev_attr_in1_alarm.dev_attr.attr,
@@ -630,27 +627,14 @@ static struct lm80_data *lm80_update_device(struct device *dev)
630627
rv = lm80_read_value(client, LM80_REG_RES);
631628
if (rv < 0)
632629
goto abort;
633-
data->temp = (prev_rv << 8) | (rv & 0xf0);
630+
data->temp[t_input] = (prev_rv << 8) | (rv & 0xf0);
634631

635-
rv = lm80_read_value(client, LM80_REG_TEMP_OS_MAX);
636-
if (rv < 0)
637-
goto abort;
638-
data->temp_os_max = rv << 8;
639-
640-
rv = lm80_read_value(client, LM80_REG_TEMP_OS_HYST);
641-
if (rv < 0)
642-
goto abort;
643-
data->temp_os_hyst = rv << 8;
644-
645-
rv = lm80_read_value(client, LM80_REG_TEMP_HOT_MAX);
646-
if (rv < 0)
647-
goto abort;
648-
data->temp_hot_max = rv << 8;
649-
650-
rv = lm80_read_value(client, LM80_REG_TEMP_HOT_HYST);
651-
if (rv < 0)
652-
goto abort;
653-
data->temp_hot_hyst = rv << 8;
632+
for (i = t_input + 1; i < t_num_temp; i++) {
633+
rv = lm80_read_value(client, temp_regs[i]);
634+
if (rv < 0)
635+
goto abort;
636+
data->temp[i] = rv << 8;
637+
}
654638

655639
rv = lm80_read_value(client, LM80_REG_FANDIV);
656640
if (rv < 0)

0 commit comments

Comments
 (0)