diff options
author | Jean Delvare <khali@linux-fr.org> | 2007-05-08 11:22:01 -0400 |
---|---|---|
committer | Jean Delvare <khali@hyperion.delvare> | 2007-05-08 11:22:01 -0400 |
commit | 9ca8e40c8414d25e880b587cbd4d130750c49588 (patch) | |
tree | 4dc4c8c4e3afdee1817bf4edf197e7ef5fa973cf | |
parent | 247dde4cdded4e4c332622b7c2860254e61cf5ce (diff) |
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: Jean Delvare <khali@linux-fr.org>
-rw-r--r-- | drivers/hwmon/lm75.c | 82 |
1 files changed, 44 insertions, 38 deletions
diff --git a/drivers/hwmon/lm75.c b/drivers/hwmon/lm75.c index 7c65b8bb6d72..a40166ffad12 100644 --- a/drivers/hwmon/lm75.c +++ b/drivers/hwmon/lm75.c | |||
@@ -24,6 +24,7 @@ | |||
24 | #include <linux/jiffies.h> | 24 | #include <linux/jiffies.h> |
25 | #include <linux/i2c.h> | 25 | #include <linux/i2c.h> |
26 | #include <linux/hwmon.h> | 26 | #include <linux/hwmon.h> |
27 | #include <linux/hwmon-sysfs.h> | ||
27 | #include <linux/err.h> | 28 | #include <linux/err.h> |
28 | #include <linux/mutex.h> | 29 | #include <linux/mutex.h> |
29 | #include "lm75.h" | 30 | #include "lm75.h" |
@@ -39,10 +40,12 @@ I2C_CLIENT_INSMOD_1(lm75); | |||
39 | /* Many LM75 constants specified below */ | 40 | /* Many LM75 constants specified below */ |
40 | 41 | ||
41 | /* The LM75 registers */ | 42 | /* The LM75 registers */ |
42 | #define LM75_REG_TEMP 0x00 | ||
43 | #define LM75_REG_CONF 0x01 | 43 | #define LM75_REG_CONF 0x01 |
44 | #define LM75_REG_TEMP_HYST 0x02 | 44 | static const u8 LM75_REG_TEMP[3] = { |
45 | #define LM75_REG_TEMP_OS 0x03 | 45 | 0x00, /* input */ |
46 | 0x03, /* max */ | ||
47 | 0x02, /* hyst */ | ||
48 | }; | ||
46 | 49 | ||
47 | /* Each client has this additional data */ | 50 | /* Each client has this additional data */ |
48 | struct lm75_data { | 51 | struct lm75_data { |
@@ -51,9 +54,10 @@ struct lm75_data { | |||
51 | struct mutex update_lock; | 54 | struct mutex update_lock; |
52 | char valid; /* !=0 if following fields are valid */ | 55 | char valid; /* !=0 if following fields are valid */ |
53 | unsigned long last_updated; /* In jiffies */ | 56 | unsigned long last_updated; /* In jiffies */ |
54 | u16 temp_input; /* Register values */ | 57 | u16 temp[3]; /* Register values, |
55 | u16 temp_max; | 58 | 0 = input |
56 | u16 temp_hyst; | 59 | 1 = max |
60 | 2 = hyst */ | ||
57 | }; | 61 | }; |
58 | 62 | ||
59 | static int lm75_attach_adapter(struct i2c_adapter *adapter); | 63 | static int lm75_attach_adapter(struct i2c_adapter *adapter); |
@@ -75,35 +79,36 @@ static struct i2c_driver lm75_driver = { | |||
75 | .detach_client = lm75_detach_client, | 79 | .detach_client = lm75_detach_client, |
76 | }; | 80 | }; |
77 | 81 | ||
78 | #define show(value) \ | 82 | static ssize_t show_temp(struct device *dev, struct device_attribute *da, |
79 | static ssize_t show_##value(struct device *dev, struct device_attribute *attr, char *buf) \ | 83 | char *buf) |
80 | { \ | 84 | { |
81 | struct lm75_data *data = lm75_update_device(dev); \ | 85 | struct sensor_device_attribute *attr = to_sensor_dev_attr(da); |
82 | return sprintf(buf, "%d\n", LM75_TEMP_FROM_REG(data->value)); \ | 86 | struct lm75_data *data = lm75_update_device(dev); |
87 | return sprintf(buf, "%d\n", | ||
88 | LM75_TEMP_FROM_REG(data->temp[attr->index])); | ||
83 | } | 89 | } |
84 | show(temp_max); | 90 | |
85 | show(temp_hyst); | 91 | static ssize_t set_temp(struct device *dev, struct device_attribute *da, |
86 | show(temp_input); | 92 | const char *buf, size_t count) |
87 | 93 | { | |
88 | #define set(value, reg) \ | 94 | struct sensor_device_attribute *attr = to_sensor_dev_attr(da); |
89 | static ssize_t set_##value(struct device *dev, struct device_attribute *attr, const char *buf, size_t count) \ | 95 | struct i2c_client *client = to_i2c_client(dev); |
90 | { \ | 96 | struct lm75_data *data = i2c_get_clientdata(client); |
91 | struct i2c_client *client = to_i2c_client(dev); \ | 97 | int nr = attr->index; |
92 | struct lm75_data *data = i2c_get_clientdata(client); \ | 98 | unsigned long temp = simple_strtoul(buf, NULL, 10); |
93 | int temp = simple_strtoul(buf, NULL, 10); \ | 99 | |
94 | \ | 100 | mutex_lock(&data->update_lock); |
95 | mutex_lock(&data->update_lock); \ | 101 | data->temp[nr] = LM75_TEMP_TO_REG(temp); |
96 | data->value = LM75_TEMP_TO_REG(temp); \ | 102 | lm75_write_value(client, LM75_REG_TEMP[nr], data->temp[nr]); |
97 | lm75_write_value(client, reg, data->value); \ | 103 | mutex_unlock(&data->update_lock); |
98 | mutex_unlock(&data->update_lock); \ | 104 | return count; |
99 | return count; \ | ||
100 | } | 105 | } |
101 | set(temp_max, LM75_REG_TEMP_OS); | ||
102 | set(temp_hyst, LM75_REG_TEMP_HYST); | ||
103 | 106 | ||
104 | static DEVICE_ATTR(temp1_max, S_IWUSR | S_IRUGO, show_temp_max, set_temp_max); | 107 | static SENSOR_DEVICE_ATTR(temp1_max, S_IWUSR | S_IRUGO, |
105 | static DEVICE_ATTR(temp1_max_hyst, S_IWUSR | S_IRUGO, show_temp_hyst, set_temp_hyst); | 108 | show_temp, set_temp, 1); |
106 | static DEVICE_ATTR(temp1_input, S_IRUGO, show_temp_input, NULL); | 109 | static SENSOR_DEVICE_ATTR(temp1_max_hyst, S_IWUSR | S_IRUGO, |
110 | show_temp, set_temp, 2); | ||
111 | static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, show_temp, NULL, 0); | ||
107 | 112 | ||
108 | static int lm75_attach_adapter(struct i2c_adapter *adapter) | 113 | static int lm75_attach_adapter(struct i2c_adapter *adapter) |
109 | { | 114 | { |
@@ -113,9 +118,9 @@ static int lm75_attach_adapter(struct i2c_adapter *adapter) | |||
113 | } | 118 | } |
114 | 119 | ||
115 | static struct attribute *lm75_attributes[] = { | 120 | static struct attribute *lm75_attributes[] = { |
116 | &dev_attr_temp1_input.attr, | 121 | &sensor_dev_attr_temp1_input.dev_attr.attr, |
117 | &dev_attr_temp1_max.attr, | 122 | &sensor_dev_attr_temp1_max.dev_attr.attr, |
118 | &dev_attr_temp1_max_hyst.attr, | 123 | &sensor_dev_attr_temp1_max_hyst.dev_attr.attr, |
119 | 124 | ||
120 | NULL | 125 | NULL |
121 | }; | 126 | }; |
@@ -283,11 +288,12 @@ static struct lm75_data *lm75_update_device(struct device *dev) | |||
283 | 288 | ||
284 | if (time_after(jiffies, data->last_updated + HZ + HZ / 2) | 289 | if (time_after(jiffies, data->last_updated + HZ + HZ / 2) |
285 | || !data->valid) { | 290 | || !data->valid) { |
291 | int i; | ||
286 | dev_dbg(&client->dev, "Starting lm75 update\n"); | 292 | dev_dbg(&client->dev, "Starting lm75 update\n"); |
287 | 293 | ||
288 | data->temp_input = lm75_read_value(client, LM75_REG_TEMP); | 294 | for (i = 0; i < ARRAY_SIZE(data->temp); i++) |
289 | data->temp_max = lm75_read_value(client, LM75_REG_TEMP_OS); | 295 | data->temp[i] = lm75_read_value(client, |
290 | data->temp_hyst = lm75_read_value(client, LM75_REG_TEMP_HYST); | 296 | LM75_REG_TEMP[i]); |
291 | data->last_updated = jiffies; | 297 | data->last_updated = jiffies; |
292 | data->valid = 1; | 298 | data->valid = 1; |
293 | } | 299 | } |