hwmon: (jc42) Convert function macros into functions

Convert function macros into functions to make the code
easier to read and reduce code size.

Signed-off-by: Guenter Roeck <linux@roeck-us.net>
This commit is contained in:
Guenter Roeck 2014-04-15 21:44:20 -07:00
parent d397276b0c
commit 10192bc654

View File

@ -161,6 +161,21 @@ static struct jc42_chips jc42_chips[] = {
{ STM_MANID, STTS3000_DEVID, STTS3000_DEVID_MASK }, { STM_MANID, STTS3000_DEVID, STTS3000_DEVID_MASK },
}; };
enum temp_index {
t_input = 0,
t_crit,
t_min,
t_max,
t_num_temp
};
static const u8 temp_regs[t_num_temp] = {
[t_input] = JC42_REG_TEMP,
[t_crit] = JC42_REG_TEMP_CRITICAL,
[t_min] = JC42_REG_TEMP_LOWER,
[t_max] = JC42_REG_TEMP_UPPER,
};
/* Each client has this additional data */ /* Each client has this additional data */
struct jc42_data { struct jc42_data {
struct i2c_client *client; struct i2c_client *client;
@ -170,10 +185,7 @@ struct jc42_data {
unsigned long last_updated; /* In jiffies */ unsigned long last_updated; /* In jiffies */
u16 orig_config; /* original configuration */ u16 orig_config; /* original configuration */
u16 config; /* current configuration */ u16 config; /* current configuration */
u16 temp_input; /* Temperatures */ u16 temp[t_num_temp];/* Temperatures */
u16 temp_crit;
u16 temp_min;
u16 temp_max;
}; };
#define JC42_TEMP_MIN_EXTENDED (-40000) #define JC42_TEMP_MIN_EXTENDED (-40000)
@ -207,40 +219,19 @@ static struct jc42_data *jc42_update_device(struct device *dev)
struct jc42_data *data = dev_get_drvdata(dev); struct jc42_data *data = dev_get_drvdata(dev);
struct i2c_client *client = data->client; struct i2c_client *client = data->client;
struct jc42_data *ret = data; struct jc42_data *ret = data;
int val; int i, val;
mutex_lock(&data->update_lock); mutex_lock(&data->update_lock);
if (time_after(jiffies, data->last_updated + HZ) || !data->valid) { if (time_after(jiffies, data->last_updated + HZ) || !data->valid) {
val = i2c_smbus_read_word_swapped(client, JC42_REG_TEMP); for (i = 0; i < t_num_temp; i++) {
if (val < 0) { val = i2c_smbus_read_word_swapped(client, temp_regs[i]);
ret = ERR_PTR(val); if (val < 0) {
goto abort; ret = ERR_PTR(val);
goto abort;
}
data->temp[i] = val;
} }
data->temp_input = val;
val = i2c_smbus_read_word_swapped(client,
JC42_REG_TEMP_CRITICAL);
if (val < 0) {
ret = ERR_PTR(val);
goto abort;
}
data->temp_crit = val;
val = i2c_smbus_read_word_swapped(client, JC42_REG_TEMP_LOWER);
if (val < 0) {
ret = ERR_PTR(val);
goto abort;
}
data->temp_min = val;
val = i2c_smbus_read_word_swapped(client, JC42_REG_TEMP_UPPER);
if (val < 0) {
ret = ERR_PTR(val);
goto abort;
}
data->temp_max = val;
data->last_updated = jiffies; data->last_updated = jiffies;
data->valid = true; data->valid = true;
} }
@ -249,80 +240,56 @@ static struct jc42_data *jc42_update_device(struct device *dev)
return ret; return ret;
} }
/* sysfs stuff */ /* sysfs functions */
/* read routines for temperature limits */ static ssize_t show_temp(struct device *dev, struct device_attribute *devattr,
#define show(value) \ char *buf)
static ssize_t show_##value(struct device *dev, \ {
struct device_attribute *attr, \ struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
char *buf) \ struct jc42_data *data = jc42_update_device(dev);
{ \ if (IS_ERR(data))
struct jc42_data *data = jc42_update_device(dev); \ return PTR_ERR(data);
if (IS_ERR(data)) \ return sprintf(buf, "%d\n",
return PTR_ERR(data); \ jc42_temp_from_reg(data->temp[attr->index]));
return sprintf(buf, "%d\n", jc42_temp_from_reg(data->value)); \
} }
show(temp_input); static ssize_t show_temp_hyst(struct device *dev,
show(temp_crit); struct device_attribute *devattr, char *buf)
show(temp_min);
show(temp_max);
/* read routines for hysteresis values */
static ssize_t show_temp_crit_hyst(struct device *dev,
struct device_attribute *attr, char *buf)
{ {
struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
struct jc42_data *data = jc42_update_device(dev); struct jc42_data *data = jc42_update_device(dev);
int temp, hyst; int temp, hyst;
if (IS_ERR(data)) if (IS_ERR(data))
return PTR_ERR(data); return PTR_ERR(data);
temp = jc42_temp_from_reg(data->temp_crit); temp = jc42_temp_from_reg(data->temp[attr->index]);
hyst = jc42_hysteresis[(data->config & JC42_CFG_HYST_MASK) hyst = jc42_hysteresis[(data->config & JC42_CFG_HYST_MASK)
>> JC42_CFG_HYST_SHIFT]; >> JC42_CFG_HYST_SHIFT];
return sprintf(buf, "%d\n", temp - hyst); return sprintf(buf, "%d\n", temp - hyst);
} }
static ssize_t show_temp_max_hyst(struct device *dev, static ssize_t set_temp(struct device *dev, struct device_attribute *devattr,
struct device_attribute *attr, char *buf) const char *buf, size_t count)
{ {
struct jc42_data *data = jc42_update_device(dev); struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
int temp, hyst; struct jc42_data *data = dev_get_drvdata(dev);
int err, ret = count;
int nr = attr->index;
long val;
if (IS_ERR(data)) if (kstrtol(buf, 10, &val) < 0)
return PTR_ERR(data); return -EINVAL;
mutex_lock(&data->update_lock);
temp = jc42_temp_from_reg(data->temp_max); data->temp[nr] = jc42_temp_to_reg(val, data->extended);
hyst = jc42_hysteresis[(data->config & JC42_CFG_HYST_MASK) err = i2c_smbus_write_word_swapped(data->client, temp_regs[nr],
>> JC42_CFG_HYST_SHIFT]; data->temp[nr]);
return sprintf(buf, "%d\n", temp - hyst); if (err < 0)
ret = err;
mutex_unlock(&data->update_lock);
return ret;
} }
/* write routines */
#define set(value, reg) \
static ssize_t set_##value(struct device *dev, \
struct device_attribute *attr, \
const char *buf, size_t count) \
{ \
struct jc42_data *data = dev_get_drvdata(dev); \
int err, ret = count; \
long val; \
if (kstrtol(buf, 10, &val) < 0) \
return -EINVAL; \
mutex_lock(&data->update_lock); \
data->value = jc42_temp_to_reg(val, data->extended); \
err = i2c_smbus_write_word_swapped(data->client, reg, data->value); \
if (err < 0) \
ret = err; \
mutex_unlock(&data->update_lock); \
return ret; \
}
set(temp_min, JC42_REG_TEMP_LOWER);
set(temp_max, JC42_REG_TEMP_UPPER);
set(temp_crit, JC42_REG_TEMP_CRITICAL);
/* /*
* JC42.4 compliant chips only support four hysteresis values. * JC42.4 compliant chips only support four hysteresis values.
* Pick best choice and go from there. * Pick best choice and go from there.
@ -340,7 +307,7 @@ static ssize_t set_temp_crit_hyst(struct device *dev,
if (kstrtoul(buf, 10, &val) < 0) if (kstrtoul(buf, 10, &val) < 0)
return -EINVAL; return -EINVAL;
diff = jc42_temp_from_reg(data->temp_crit) - val; diff = jc42_temp_from_reg(data->temp[t_crit]) - val;
hyst = 0; hyst = 0;
if (diff > 0) { if (diff > 0) {
if (diff < 2250) if (diff < 2250)
@ -372,25 +339,20 @@ static ssize_t show_alarm(struct device *dev,
if (IS_ERR(data)) if (IS_ERR(data))
return PTR_ERR(data); return PTR_ERR(data);
val = data->temp_input; val = data->temp[t_input];
if (bit != JC42_ALARM_CRIT_BIT && (data->config & JC42_CFG_CRIT_ONLY)) if (bit != JC42_ALARM_CRIT_BIT && (data->config & JC42_CFG_CRIT_ONLY))
val = 0; val = 0;
return sprintf(buf, "%u\n", (val >> bit) & 1); return sprintf(buf, "%u\n", (val >> bit) & 1);
} }
static DEVICE_ATTR(temp1_input, S_IRUGO, static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, show_temp, NULL, t_input);
show_temp_input, NULL); static SENSOR_DEVICE_ATTR(temp1_crit, S_IRUGO, show_temp, set_temp, t_crit);
static DEVICE_ATTR(temp1_crit, S_IRUGO, static SENSOR_DEVICE_ATTR(temp1_min, S_IRUGO, show_temp, set_temp, t_min);
show_temp_crit, set_temp_crit); static SENSOR_DEVICE_ATTR(temp1_max, S_IRUGO, show_temp, set_temp, t_max);
static DEVICE_ATTR(temp1_min, S_IRUGO,
show_temp_min, set_temp_min);
static DEVICE_ATTR(temp1_max, S_IRUGO,
show_temp_max, set_temp_max);
static DEVICE_ATTR(temp1_crit_hyst, S_IRUGO, static SENSOR_DEVICE_ATTR(temp1_crit_hyst, S_IRUGO, show_temp_hyst,
show_temp_crit_hyst, set_temp_crit_hyst); set_temp_crit_hyst, t_crit);
static DEVICE_ATTR(temp1_max_hyst, S_IRUGO, static SENSOR_DEVICE_ATTR(temp1_max_hyst, S_IRUGO, show_temp_hyst, NULL, t_max);
show_temp_max_hyst, NULL);
static SENSOR_DEVICE_ATTR(temp1_crit_alarm, S_IRUGO, show_alarm, NULL, static SENSOR_DEVICE_ATTR(temp1_crit_alarm, S_IRUGO, show_alarm, NULL,
JC42_ALARM_CRIT_BIT); JC42_ALARM_CRIT_BIT);
@ -400,12 +362,12 @@ static SENSOR_DEVICE_ATTR(temp1_max_alarm, S_IRUGO, show_alarm, NULL,
JC42_ALARM_MAX_BIT); JC42_ALARM_MAX_BIT);
static struct attribute *jc42_attributes[] = { static struct attribute *jc42_attributes[] = {
&dev_attr_temp1_input.attr, &sensor_dev_attr_temp1_input.dev_attr.attr,
&dev_attr_temp1_crit.attr, &sensor_dev_attr_temp1_crit.dev_attr.attr,
&dev_attr_temp1_min.attr, &sensor_dev_attr_temp1_min.dev_attr.attr,
&dev_attr_temp1_max.attr, &sensor_dev_attr_temp1_max.dev_attr.attr,
&dev_attr_temp1_crit_hyst.attr, &sensor_dev_attr_temp1_crit_hyst.dev_attr.attr,
&dev_attr_temp1_max_hyst.attr, &sensor_dev_attr_temp1_max_hyst.dev_attr.attr,
&sensor_dev_attr_temp1_crit_alarm.dev_attr.attr, &sensor_dev_attr_temp1_crit_alarm.dev_attr.attr,
&sensor_dev_attr_temp1_min_alarm.dev_attr.attr, &sensor_dev_attr_temp1_min_alarm.dev_attr.attr,
&sensor_dev_attr_temp1_max_alarm.dev_attr.attr, &sensor_dev_attr_temp1_max_alarm.dev_attr.attr,
@ -420,12 +382,12 @@ static umode_t jc42_attribute_mode(struct kobject *kobj,
unsigned int config = data->config; unsigned int config = data->config;
bool readonly; bool readonly;
if (attr == &dev_attr_temp1_crit.attr) if (attr == &sensor_dev_attr_temp1_crit.dev_attr.attr)
readonly = config & JC42_CFG_TCRIT_LOCK; readonly = config & JC42_CFG_TCRIT_LOCK;
else if (attr == &dev_attr_temp1_min.attr || else if (attr == &sensor_dev_attr_temp1_min.dev_attr.attr ||
attr == &dev_attr_temp1_max.attr) attr == &sensor_dev_attr_temp1_max.dev_attr.attr)
readonly = config & JC42_CFG_EVENT_LOCK; readonly = config & JC42_CFG_EVENT_LOCK;
else if (attr == &dev_attr_temp1_crit_hyst.attr) else if (attr == &sensor_dev_attr_temp1_crit_hyst.dev_attr.attr)
readonly = config & (JC42_CFG_EVENT_LOCK | JC42_CFG_TCRIT_LOCK); readonly = config & (JC42_CFG_EVENT_LOCK | JC42_CFG_TCRIT_LOCK);
else else
readonly = true; readonly = true;