Commit 9ca8e40c authored by Jean Delvare's avatar Jean Delvare Committed by Jean Delvare

hwmon/lm75: Use dynamic sysfs callbacks

This lets us get rid of macro-generated functions and shrinks the
driver size by about 8%.
Signed-off-by: default avatarJean Delvare <khali@linux-fr.org>
parent 247dde4c
...@@ -24,6 +24,7 @@ ...@@ -24,6 +24,7 @@
#include <linux/jiffies.h> #include <linux/jiffies.h>
#include <linux/i2c.h> #include <linux/i2c.h>
#include <linux/hwmon.h> #include <linux/hwmon.h>
#include <linux/hwmon-sysfs.h>
#include <linux/err.h> #include <linux/err.h>
#include <linux/mutex.h> #include <linux/mutex.h>
#include "lm75.h" #include "lm75.h"
...@@ -39,10 +40,12 @@ I2C_CLIENT_INSMOD_1(lm75); ...@@ -39,10 +40,12 @@ I2C_CLIENT_INSMOD_1(lm75);
/* Many LM75 constants specified below */ /* Many LM75 constants specified below */
/* The LM75 registers */ /* The LM75 registers */
#define LM75_REG_TEMP 0x00
#define LM75_REG_CONF 0x01 #define LM75_REG_CONF 0x01
#define LM75_REG_TEMP_HYST 0x02 static const u8 LM75_REG_TEMP[3] = {
#define LM75_REG_TEMP_OS 0x03 0x00, /* input */
0x03, /* max */
0x02, /* hyst */
};
/* Each client has this additional data */ /* Each client has this additional data */
struct lm75_data { struct lm75_data {
...@@ -51,9 +54,10 @@ struct lm75_data { ...@@ -51,9 +54,10 @@ struct lm75_data {
struct mutex update_lock; struct mutex update_lock;
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 */
u16 temp_input; /* Register values */ u16 temp[3]; /* Register values,
u16 temp_max; 0 = input
u16 temp_hyst; 1 = max
2 = hyst */
}; };
static int lm75_attach_adapter(struct i2c_adapter *adapter); static int lm75_attach_adapter(struct i2c_adapter *adapter);
...@@ -75,35 +79,36 @@ static struct i2c_driver lm75_driver = { ...@@ -75,35 +79,36 @@ static struct i2c_driver lm75_driver = {
.detach_client = lm75_detach_client, .detach_client = lm75_detach_client,
}; };
#define show(value) \ static ssize_t show_temp(struct device *dev, struct device_attribute *da,
static ssize_t show_##value(struct device *dev, struct device_attribute *attr, char *buf) \ char *buf)
{ \ {
struct lm75_data *data = lm75_update_device(dev); \ struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
return sprintf(buf, "%d\n", LM75_TEMP_FROM_REG(data->value)); \ struct lm75_data *data = lm75_update_device(dev);
return sprintf(buf, "%d\n",
LM75_TEMP_FROM_REG(data->temp[attr->index]));
} }
show(temp_max);
show(temp_hyst); static ssize_t set_temp(struct device *dev, struct device_attribute *da,
show(temp_input); const char *buf, size_t count)
{
#define set(value, reg) \ struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
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 lm75_data *data = i2c_get_clientdata(client);
struct i2c_client *client = to_i2c_client(dev); \ int nr = attr->index;
struct lm75_data *data = i2c_get_clientdata(client); \ unsigned long temp = simple_strtoul(buf, NULL, 10);
int temp = simple_strtoul(buf, NULL, 10); \
\ mutex_lock(&data->update_lock);
mutex_lock(&data->update_lock); \ data->temp[nr] = LM75_TEMP_TO_REG(temp);
data->value = LM75_TEMP_TO_REG(temp); \ lm75_write_value(client, LM75_REG_TEMP[nr], data->temp[nr]);
lm75_write_value(client, reg, data->value); \ mutex_unlock(&data->update_lock);
mutex_unlock(&data->update_lock); \ return count;
return count; \
} }
set(temp_max, LM75_REG_TEMP_OS);
set(temp_hyst, LM75_REG_TEMP_HYST);
static DEVICE_ATTR(temp1_max, S_IWUSR | S_IRUGO, show_temp_max, set_temp_max); static SENSOR_DEVICE_ATTR(temp1_max, S_IWUSR | S_IRUGO,
static DEVICE_ATTR(temp1_max_hyst, S_IWUSR | S_IRUGO, show_temp_hyst, set_temp_hyst); show_temp, set_temp, 1);
static DEVICE_ATTR(temp1_input, S_IRUGO, show_temp_input, NULL); static SENSOR_DEVICE_ATTR(temp1_max_hyst, S_IWUSR | S_IRUGO,
show_temp, set_temp, 2);
static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, show_temp, NULL, 0);
static int lm75_attach_adapter(struct i2c_adapter *adapter) static int lm75_attach_adapter(struct i2c_adapter *adapter)
{ {
...@@ -113,9 +118,9 @@ static int lm75_attach_adapter(struct i2c_adapter *adapter) ...@@ -113,9 +118,9 @@ static int lm75_attach_adapter(struct i2c_adapter *adapter)
} }
static struct attribute *lm75_attributes[] = { static struct attribute *lm75_attributes[] = {
&dev_attr_temp1_input.attr, &sensor_dev_attr_temp1_input.dev_attr.attr,
&dev_attr_temp1_max.attr, &sensor_dev_attr_temp1_max.dev_attr.attr,
&dev_attr_temp1_max_hyst.attr, &sensor_dev_attr_temp1_max_hyst.dev_attr.attr,
NULL NULL
}; };
...@@ -283,11 +288,12 @@ static struct lm75_data *lm75_update_device(struct device *dev) ...@@ -283,11 +288,12 @@ static struct lm75_data *lm75_update_device(struct device *dev)
if (time_after(jiffies, data->last_updated + HZ + HZ / 2) if (time_after(jiffies, data->last_updated + HZ + HZ / 2)
|| !data->valid) { || !data->valid) {
int i;
dev_dbg(&client->dev, "Starting lm75 update\n"); dev_dbg(&client->dev, "Starting lm75 update\n");
data->temp_input = lm75_read_value(client, LM75_REG_TEMP); for (i = 0; i < ARRAY_SIZE(data->temp); i++)
data->temp_max = lm75_read_value(client, LM75_REG_TEMP_OS); data->temp[i] = lm75_read_value(client,
data->temp_hyst = lm75_read_value(client, LM75_REG_TEMP_HYST); LM75_REG_TEMP[i]);
data->last_updated = jiffies; data->last_updated = jiffies;
data->valid = 1; data->valid = 1;
} }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment