hwmon: (lm80) Convert voltage display function macros into functions

Convert voltage display function macros into functions to reduce
code size and improve code readability.

Code size reduction is about 600 bytes on x86_64.

Signed-off-by: Guenter Roeck <linux@roeck-us.net>
This commit is contained in:
Guenter Roeck 2014-04-13 10:53:50 -07:00
parent 9711bcddde
commit 1adf3a3e1c
1 changed files with 77 additions and 73 deletions

View File

@ -109,6 +109,13 @@ static const u8 temp_regs[t_num_temp] = {
[t_os_hyst] = LM80_REG_TEMP_OS_HYST, [t_os_hyst] = LM80_REG_TEMP_OS_HYST,
}; };
enum in_index {
i_input = 0,
i_max,
i_min,
i_num_in
};
/* /*
* Client data (each client gets its own) * Client data (each client gets its own)
*/ */
@ -120,9 +127,7 @@ struct lm80_data {
char valid; /* !=0 if following fields are valid */ char valid; /* !=0 if following fields are valid */
unsigned long last_updated; /* In jiffies */ unsigned long last_updated; /* In jiffies */
u8 in[7]; /* Register value */ u8 in[i_num_in][7]; /* Register value, 1st index is enum in_index */
u8 in_max[7]; /* Register value */
u8 in_min[7]; /* Register value */
u8 fan[2]; /* Register value */ u8 fan[2]; /* Register value */
u8 fan_min[2]; /* Register value */ u8 fan_min[2]; /* Register value */
u8 fan_div[2]; /* Register encoding, shifted right */ u8 fan_div[2]; /* Register encoding, shifted right */
@ -168,40 +173,39 @@ static struct i2c_driver lm80_driver = {
* Sysfs stuff * Sysfs stuff
*/ */
#define show_in(suffix, value) \ static ssize_t show_in(struct device *dev, struct device_attribute *attr,
static ssize_t show_in_##suffix(struct device *dev, \ char *buf)
struct device_attribute *attr, char *buf) \ {
{ \ struct lm80_data *data = lm80_update_device(dev);
int nr = to_sensor_dev_attr(attr)->index; \ int index = to_sensor_dev_attr_2(attr)->index;
struct lm80_data *data = lm80_update_device(dev); \ int nr = to_sensor_dev_attr_2(attr)->nr;
if (IS_ERR(data)) \
return PTR_ERR(data); \
return sprintf(buf, "%d\n", IN_FROM_REG(data->value[nr])); \
}
show_in(min, in_min)
show_in(max, in_max)
show_in(input, in)
#define set_in(suffix, value, reg) \ if (IS_ERR(data))
static ssize_t set_in_##suffix(struct device *dev, \ return PTR_ERR(data);
struct device_attribute *attr, const char *buf, size_t count) \ return sprintf(buf, "%d\n", IN_FROM_REG(data->in[nr][index]));
{ \ }
int nr = to_sensor_dev_attr(attr)->index; \
struct lm80_data *data = dev_get_drvdata(dev); \ static ssize_t set_in(struct device *dev, struct device_attribute *attr,
struct i2c_client *client = data->client; \ const char *buf, size_t count)
long val; \ {
int err = kstrtol(buf, 10, &val); \ struct lm80_data *data = dev_get_drvdata(dev);
if (err < 0) \ struct i2c_client *client = data->client;
return err; \ int index = to_sensor_dev_attr_2(attr)->index;
\ int nr = to_sensor_dev_attr_2(attr)->nr;
mutex_lock(&data->update_lock);\ long val;
data->value[nr] = IN_TO_REG(val); \ u8 reg;
lm80_write_value(client, reg(nr), data->value[nr]); \ int err = kstrtol(buf, 10, &val);
mutex_unlock(&data->update_lock);\ if (err < 0)
return count; \ return err;
reg = nr == i_min ? LM80_REG_IN_MIN(index) : LM80_REG_IN_MAX(index);
mutex_lock(&data->update_lock);
data->in[nr][index] = IN_TO_REG(val);
lm80_write_value(client, reg, data->in[nr][index]);
mutex_unlock(&data->update_lock);
return count;
} }
set_in(min, in_min, LM80_REG_IN_MIN)
set_in(max, in_max, LM80_REG_IN_MAX)
#define show_fan(suffix, value) \ #define show_fan(suffix, value) \
static ssize_t show_fan_##suffix(struct device *dev, \ static ssize_t show_fan_##suffix(struct device *dev, \
@ -349,41 +353,41 @@ static ssize_t show_alarm(struct device *dev, struct device_attribute *attr,
return sprintf(buf, "%u\n", (data->alarms >> bitnr) & 1); return sprintf(buf, "%u\n", (data->alarms >> bitnr) & 1);
} }
static SENSOR_DEVICE_ATTR(in0_min, S_IWUSR | S_IRUGO, static SENSOR_DEVICE_ATTR_2(in0_min, S_IWUSR | S_IRUGO,
show_in_min, set_in_min, 0); show_in, set_in, i_min, 0);
static SENSOR_DEVICE_ATTR(in1_min, S_IWUSR | S_IRUGO, static SENSOR_DEVICE_ATTR_2(in1_min, S_IWUSR | S_IRUGO,
show_in_min, set_in_min, 1); show_in, set_in, i_min, 1);
static SENSOR_DEVICE_ATTR(in2_min, S_IWUSR | S_IRUGO, static SENSOR_DEVICE_ATTR_2(in2_min, S_IWUSR | S_IRUGO,
show_in_min, set_in_min, 2); show_in, set_in, i_min, 2);
static SENSOR_DEVICE_ATTR(in3_min, S_IWUSR | S_IRUGO, static SENSOR_DEVICE_ATTR_2(in3_min, S_IWUSR | S_IRUGO,
show_in_min, set_in_min, 3); show_in, set_in, i_min, 3);
static SENSOR_DEVICE_ATTR(in4_min, S_IWUSR | S_IRUGO, static SENSOR_DEVICE_ATTR_2(in4_min, S_IWUSR | S_IRUGO,
show_in_min, set_in_min, 4); show_in, set_in, i_min, 4);
static SENSOR_DEVICE_ATTR(in5_min, S_IWUSR | S_IRUGO, static SENSOR_DEVICE_ATTR_2(in5_min, S_IWUSR | S_IRUGO,
show_in_min, set_in_min, 5); show_in, set_in, i_min, 5);
static SENSOR_DEVICE_ATTR(in6_min, S_IWUSR | S_IRUGO, static SENSOR_DEVICE_ATTR_2(in6_min, S_IWUSR | S_IRUGO,
show_in_min, set_in_min, 6); show_in, set_in, i_min, 6);
static SENSOR_DEVICE_ATTR(in0_max, S_IWUSR | S_IRUGO, static SENSOR_DEVICE_ATTR_2(in0_max, S_IWUSR | S_IRUGO,
show_in_max, set_in_max, 0); show_in, set_in, i_max, 0);
static SENSOR_DEVICE_ATTR(in1_max, S_IWUSR | S_IRUGO, static SENSOR_DEVICE_ATTR_2(in1_max, S_IWUSR | S_IRUGO,
show_in_max, set_in_max, 1); show_in, set_in, i_max, 1);
static SENSOR_DEVICE_ATTR(in2_max, S_IWUSR | S_IRUGO, static SENSOR_DEVICE_ATTR_2(in2_max, S_IWUSR | S_IRUGO,
show_in_max, set_in_max, 2); show_in, set_in, i_max, 2);
static SENSOR_DEVICE_ATTR(in3_max, S_IWUSR | S_IRUGO, static SENSOR_DEVICE_ATTR_2(in3_max, S_IWUSR | S_IRUGO,
show_in_max, set_in_max, 3); show_in, set_in, i_max, 3);
static SENSOR_DEVICE_ATTR(in4_max, S_IWUSR | S_IRUGO, static SENSOR_DEVICE_ATTR_2(in4_max, S_IWUSR | S_IRUGO,
show_in_max, set_in_max, 4); show_in, set_in, i_max, 4);
static SENSOR_DEVICE_ATTR(in5_max, S_IWUSR | S_IRUGO, static SENSOR_DEVICE_ATTR_2(in5_max, S_IWUSR | S_IRUGO,
show_in_max, set_in_max, 5); show_in, set_in, i_max, 5);
static SENSOR_DEVICE_ATTR(in6_max, S_IWUSR | S_IRUGO, static SENSOR_DEVICE_ATTR_2(in6_max, S_IWUSR | S_IRUGO,
show_in_max, set_in_max, 6); show_in, set_in, i_max, 6);
static SENSOR_DEVICE_ATTR(in0_input, S_IRUGO, show_in_input, NULL, 0); static SENSOR_DEVICE_ATTR_2(in0_input, S_IRUGO, show_in, NULL, i_input, 0);
static SENSOR_DEVICE_ATTR(in1_input, S_IRUGO, show_in_input, NULL, 1); static SENSOR_DEVICE_ATTR_2(in1_input, S_IRUGO, show_in, NULL, i_input, 1);
static SENSOR_DEVICE_ATTR(in2_input, S_IRUGO, show_in_input, NULL, 2); static SENSOR_DEVICE_ATTR_2(in2_input, S_IRUGO, show_in, NULL, i_input, 2);
static SENSOR_DEVICE_ATTR(in3_input, S_IRUGO, show_in_input, NULL, 3); static SENSOR_DEVICE_ATTR_2(in3_input, S_IRUGO, show_in, NULL, i_input, 3);
static SENSOR_DEVICE_ATTR(in4_input, S_IRUGO, show_in_input, NULL, 4); static SENSOR_DEVICE_ATTR_2(in4_input, S_IRUGO, show_in, NULL, i_input, 4);
static SENSOR_DEVICE_ATTR(in5_input, S_IRUGO, show_in_input, NULL, 5); static SENSOR_DEVICE_ATTR_2(in5_input, S_IRUGO, show_in, NULL, i_input, 5);
static SENSOR_DEVICE_ATTR(in6_input, S_IRUGO, show_in_input, NULL, 6); static SENSOR_DEVICE_ATTR_2(in6_input, S_IRUGO, show_in, NULL, i_input, 6);
static SENSOR_DEVICE_ATTR(fan1_min, S_IWUSR | S_IRUGO, static SENSOR_DEVICE_ATTR(fan1_min, S_IWUSR | S_IRUGO,
show_fan_min, set_fan_min, 0); show_fan_min, set_fan_min, 0);
static SENSOR_DEVICE_ATTR(fan2_min, S_IWUSR | S_IRUGO, static SENSOR_DEVICE_ATTR(fan2_min, S_IWUSR | S_IRUGO,
@ -588,17 +592,17 @@ static struct lm80_data *lm80_update_device(struct device *dev)
rv = lm80_read_value(client, LM80_REG_IN(i)); rv = lm80_read_value(client, LM80_REG_IN(i));
if (rv < 0) if (rv < 0)
goto abort; goto abort;
data->in[i] = rv; data->in[i_input][i] = rv;
rv = lm80_read_value(client, LM80_REG_IN_MIN(i)); rv = lm80_read_value(client, LM80_REG_IN_MIN(i));
if (rv < 0) if (rv < 0)
goto abort; goto abort;
data->in_min[i] = rv; data->in[i_min][i] = rv;
rv = lm80_read_value(client, LM80_REG_IN_MAX(i)); rv = lm80_read_value(client, LM80_REG_IN_MAX(i));
if (rv < 0) if (rv < 0)
goto abort; goto abort;
data->in_max[i] = rv; data->in[i_max][i] = rv;
} }
rv = lm80_read_value(client, LM80_REG_FAN1); rv = lm80_read_value(client, LM80_REG_FAN1);