diff options
author | Jean Delvare <khali@linux-fr.org> | 2013-05-04 08:49:36 -0400 |
---|---|---|
committer | Jean Delvare <khali@endymion.delvare> | 2013-05-04 08:49:36 -0400 |
commit | 87d0621ae2bdf2e2c60aadbbcb8b6c680777c1bf (patch) | |
tree | 6b082b1f43cc4acc9d255478886c7f7d911660d8 /drivers/hwmon/lm75.c | |
parent | 8a5c5cc61748642a1276ef94611e21a60f0796ab (diff) |
hwmon: (lm75) Prepare to support per-chip resolution and sample time
Prepare the lm75 driver to support per-chip resolution and sample
time. For now we only make the code generic enough to support it, but
we still use the same, unchanged resolution (9-bit) and sample time
(1.5 s) for all chips.
Signed-off-by: Jean Delvare <khali@linux-fr.org>
Acked-by: Guenter Roeck <linux@roeck-us.net>
Diffstat (limited to 'drivers/hwmon/lm75.c')
-rw-r--r-- | drivers/hwmon/lm75.c | 31 |
1 files changed, 26 insertions, 5 deletions
diff --git a/drivers/hwmon/lm75.c b/drivers/hwmon/lm75.c index 4670fbef6a9c..559e675db3c8 100644 --- a/drivers/hwmon/lm75.c +++ b/drivers/hwmon/lm75.c | |||
@@ -71,9 +71,12 @@ struct lm75_data { | |||
71 | struct device *hwmon_dev; | 71 | struct device *hwmon_dev; |
72 | struct mutex update_lock; | 72 | struct mutex update_lock; |
73 | u8 orig_conf; | 73 | u8 orig_conf; |
74 | u8 resolution; /* In bits, between 9 and 12 */ | ||
75 | u8 resolution_limits; | ||
74 | char valid; /* !=0 if registers are valid */ | 76 | char valid; /* !=0 if registers are valid */ |
75 | unsigned long last_updated; /* In jiffies */ | 77 | unsigned long last_updated; /* In jiffies */ |
76 | u16 temp[3]; /* Register values, | 78 | unsigned long sample_time; /* In jiffies */ |
79 | s16 temp[3]; /* Register values, | ||
77 | 0 = input | 80 | 0 = input |
78 | 1 = max | 81 | 1 = max |
79 | 2 = hyst */ | 82 | 2 = hyst */ |
@@ -93,12 +96,15 @@ static ssize_t show_temp(struct device *dev, struct device_attribute *da, | |||
93 | { | 96 | { |
94 | struct sensor_device_attribute *attr = to_sensor_dev_attr(da); | 97 | struct sensor_device_attribute *attr = to_sensor_dev_attr(da); |
95 | struct lm75_data *data = lm75_update_device(dev); | 98 | struct lm75_data *data = lm75_update_device(dev); |
99 | long temp; | ||
96 | 100 | ||
97 | if (IS_ERR(data)) | 101 | if (IS_ERR(data)) |
98 | return PTR_ERR(data); | 102 | return PTR_ERR(data); |
99 | 103 | ||
100 | return sprintf(buf, "%d\n", | 104 | temp = ((data->temp[attr->index] >> (16 - data->resolution)) * 1000) |
101 | LM75_TEMP_FROM_REG(data->temp[attr->index])); | 105 | >> (data->resolution - 8); |
106 | |||
107 | return sprintf(buf, "%ld\n", temp); | ||
102 | } | 108 | } |
103 | 109 | ||
104 | static ssize_t set_temp(struct device *dev, struct device_attribute *da, | 110 | static ssize_t set_temp(struct device *dev, struct device_attribute *da, |
@@ -110,13 +116,25 @@ static ssize_t set_temp(struct device *dev, struct device_attribute *da, | |||
110 | int nr = attr->index; | 116 | int nr = attr->index; |
111 | long temp; | 117 | long temp; |
112 | int error; | 118 | int error; |
119 | u8 resolution; | ||
113 | 120 | ||
114 | error = kstrtol(buf, 10, &temp); | 121 | error = kstrtol(buf, 10, &temp); |
115 | if (error) | 122 | if (error) |
116 | return error; | 123 | return error; |
117 | 124 | ||
125 | /* | ||
126 | * Resolution of limit registers is assumed to be the same as the | ||
127 | * temperature input register resolution unless given explicitly. | ||
128 | */ | ||
129 | if (attr->index && data->resolution_limits) | ||
130 | resolution = data->resolution_limits; | ||
131 | else | ||
132 | resolution = data->resolution; | ||
133 | |||
118 | mutex_lock(&data->update_lock); | 134 | mutex_lock(&data->update_lock); |
119 | data->temp[nr] = LM75_TEMP_TO_REG(temp); | 135 | temp = clamp_val(temp, LM75_TEMP_MIN, LM75_TEMP_MAX); |
136 | data->temp[nr] = DIV_ROUND_CLOSEST(temp << (resolution - 8), | ||
137 | 1000) << (16 - resolution); | ||
120 | lm75_write_value(client, LM75_REG_TEMP[nr], data->temp[nr]); | 138 | lm75_write_value(client, LM75_REG_TEMP[nr], data->temp[nr]); |
121 | mutex_unlock(&data->update_lock); | 139 | mutex_unlock(&data->update_lock); |
122 | return count; | 140 | return count; |
@@ -190,6 +208,9 @@ lm75_probe(struct i2c_client *client, const struct i2c_device_id *id) | |||
190 | break; | 208 | break; |
191 | } | 209 | } |
192 | 210 | ||
211 | data->resolution = 9; | ||
212 | data->sample_time = HZ + HZ / 2; | ||
213 | |||
193 | /* configure as specified */ | 214 | /* configure as specified */ |
194 | status = lm75_read_value(client, LM75_REG_CONF); | 215 | status = lm75_read_value(client, LM75_REG_CONF); |
195 | if (status < 0) { | 216 | if (status < 0) { |
@@ -427,7 +448,7 @@ static struct lm75_data *lm75_update_device(struct device *dev) | |||
427 | 448 | ||
428 | mutex_lock(&data->update_lock); | 449 | mutex_lock(&data->update_lock); |
429 | 450 | ||
430 | if (time_after(jiffies, data->last_updated + HZ + HZ / 2) | 451 | if (time_after(jiffies, data->last_updated + data->sample_time) |
431 | || !data->valid) { | 452 | || !data->valid) { |
432 | int i; | 453 | int i; |
433 | dev_dbg(&client->dev, "Starting lm75 update\n"); | 454 | dev_dbg(&client->dev, "Starting lm75 update\n"); |