diff options
author | Guenter Roeck <linux@roeck-us.net> | 2016-07-05 22:10:52 -0400 |
---|---|---|
committer | Guenter Roeck <linux@roeck-us.net> | 2016-09-09 00:34:18 -0400 |
commit | c0a4b9ec1b43ebb9d5001e3bf86f58d4ca0ffe18 (patch) | |
tree | 32040b8016e02757431a372c82535699f5d780e9 /drivers/hwmon/lm95245.c | |
parent | 3e9046281bd5deb180e0694b93e6169424582e3c (diff) |
hwmon: (lm95245) Use new hwmon registration API
Simplify code and reduce code size by using the new hwmon
registration API.
Other changes:
- Convert to use regmap, and drop local caching. This avoids reading
registers unnecessarily, and uses regmap for caching of non-volatile
registers.
- Add support for temp2_max, temp2_max_alarm, temp2_max_hyst, and
temp2_offset.
- Order include files alphabetically
- Drop FSF address
- Check errors from register read and write functions and report
to userspace.
- Accept negative hysteresis values. While unlikely, a maximum limit
_can_ be set to a value smaller than 31 degrees C, which makes negative
hysteresis values possible.
Signed-off-by: Guenter Roeck <linux@roeck-us.net>
Diffstat (limited to 'drivers/hwmon/lm95245.c')
-rw-r--r-- | drivers/hwmon/lm95245.c | 638 |
1 files changed, 379 insertions, 259 deletions
diff --git a/drivers/hwmon/lm95245.c b/drivers/hwmon/lm95245.c index e7aef4561c83..a3bfd88752ca 100644 --- a/drivers/hwmon/lm95245.c +++ b/drivers/hwmon/lm95245.c | |||
@@ -15,22 +15,16 @@ | |||
15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | 15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | 16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
17 | * GNU General Public License for more details. | 17 | * GNU General Public License for more details. |
18 | * | ||
19 | * You should have received a copy of the GNU General Public License | ||
20 | * along with this program; if not, write to the Free Software | ||
21 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. | ||
22 | */ | 18 | */ |
23 | 19 | ||
24 | #include <linux/module.h> | 20 | #include <linux/err.h> |
25 | #include <linux/init.h> | 21 | #include <linux/init.h> |
26 | #include <linux/slab.h> | ||
27 | #include <linux/jiffies.h> | ||
28 | #include <linux/i2c.h> | ||
29 | #include <linux/hwmon.h> | 22 | #include <linux/hwmon.h> |
30 | #include <linux/hwmon-sysfs.h> | 23 | #include <linux/i2c.h> |
31 | #include <linux/err.h> | 24 | #include <linux/module.h> |
32 | #include <linux/mutex.h> | 25 | #include <linux/mutex.h> |
33 | #include <linux/sysfs.h> | 26 | #include <linux/regmap.h> |
27 | #include <linux/slab.h> | ||
34 | 28 | ||
35 | static const unsigned short normal_i2c[] = { | 29 | static const unsigned short normal_i2c[] = { |
36 | 0x18, 0x19, 0x29, 0x4c, 0x4d, I2C_CLIENT_END }; | 30 | 0x18, 0x19, 0x29, 0x4c, 0x4d, I2C_CLIENT_END }; |
@@ -89,6 +83,7 @@ static const unsigned short normal_i2c[] = { | |||
89 | #define RATE_CR1000 0x02 | 83 | #define RATE_CR1000 0x02 |
90 | #define RATE_CR2500 0x03 | 84 | #define RATE_CR2500 0x03 |
91 | 85 | ||
86 | #define STATUS1_ROS 0x10 | ||
92 | #define STATUS1_DIODE_FAULT 0x04 | 87 | #define STATUS1_DIODE_FAULT 0x04 |
93 | #define STATUS1_RTCRIT 0x02 | 88 | #define STATUS1_RTCRIT 0x02 |
94 | #define STATUS1_LOC 0x01 | 89 | #define STATUS1_LOC 0x01 |
@@ -112,14 +107,9 @@ static const u8 lm95245_reg_address[] = { | |||
112 | 107 | ||
113 | /* Client data (each client gets its own) */ | 108 | /* Client data (each client gets its own) */ |
114 | struct lm95245_data { | 109 | struct lm95245_data { |
115 | struct i2c_client *client; | 110 | struct regmap *regmap; |
116 | struct mutex update_lock; | 111 | struct mutex update_lock; |
117 | unsigned long last_updated; /* in jiffies */ | 112 | int interval; /* in msecs */ |
118 | unsigned long interval; /* in msecs */ | ||
119 | bool valid; /* zero until following fields are valid */ | ||
120 | /* registers values */ | ||
121 | u8 regs[ARRAY_SIZE(lm95245_reg_address)]; | ||
122 | u8 config1, config2; | ||
123 | }; | 113 | }; |
124 | 114 | ||
125 | /* Conversions */ | 115 | /* Conversions */ |
@@ -135,60 +125,36 @@ static int temp_from_reg_signed(u8 val_h, u8 val_l) | |||
135 | return temp_from_reg_unsigned(val_h, val_l); | 125 | return temp_from_reg_unsigned(val_h, val_l); |
136 | } | 126 | } |
137 | 127 | ||
138 | static struct lm95245_data *lm95245_update_device(struct device *dev) | 128 | static int lm95245_read_conversion_rate(struct lm95245_data *data) |
139 | { | 129 | { |
140 | struct lm95245_data *data = dev_get_drvdata(dev); | 130 | unsigned int rate; |
141 | struct i2c_client *client = data->client; | 131 | int ret; |
142 | 132 | ||
143 | mutex_lock(&data->update_lock); | 133 | ret = regmap_read(data->regmap, LM95245_REG_RW_CONVERS_RATE, &rate); |
144 | 134 | if (ret < 0) | |
145 | if (time_after(jiffies, data->last_updated | 135 | return ret; |
146 | + msecs_to_jiffies(data->interval)) || !data->valid) { | ||
147 | int i; | ||
148 | |||
149 | for (i = 0; i < ARRAY_SIZE(lm95245_reg_address); i++) | ||
150 | data->regs[i] | ||
151 | = i2c_smbus_read_byte_data(client, | ||
152 | lm95245_reg_address[i]); | ||
153 | data->last_updated = jiffies; | ||
154 | data->valid = 1; | ||
155 | } | ||
156 | |||
157 | mutex_unlock(&data->update_lock); | ||
158 | |||
159 | return data; | ||
160 | } | ||
161 | |||
162 | static unsigned long lm95245_read_conversion_rate(struct i2c_client *client) | ||
163 | { | ||
164 | int rate; | ||
165 | unsigned long interval; | ||
166 | |||
167 | rate = i2c_smbus_read_byte_data(client, LM95245_REG_RW_CONVERS_RATE); | ||
168 | 136 | ||
169 | switch (rate) { | 137 | switch (rate) { |
170 | case RATE_CR0063: | 138 | case RATE_CR0063: |
171 | interval = 63; | 139 | data->interval = 63; |
172 | break; | 140 | break; |
173 | case RATE_CR0364: | 141 | case RATE_CR0364: |
174 | interval = 364; | 142 | data->interval = 364; |
175 | break; | 143 | break; |
176 | case RATE_CR1000: | 144 | case RATE_CR1000: |
177 | interval = 1000; | 145 | data->interval = 1000; |
178 | break; | 146 | break; |
179 | case RATE_CR2500: | 147 | case RATE_CR2500: |
180 | default: | 148 | default: |
181 | interval = 2500; | 149 | data->interval = 2500; |
182 | break; | 150 | break; |
183 | } | 151 | } |
184 | 152 | return 0; | |
185 | return interval; | ||
186 | } | 153 | } |
187 | 154 | ||
188 | static unsigned long lm95245_set_conversion_rate(struct i2c_client *client, | 155 | static int lm95245_set_conversion_rate(struct lm95245_data *data, long interval) |
189 | unsigned long interval) | ||
190 | { | 156 | { |
191 | int rate; | 157 | int ret, rate; |
192 | 158 | ||
193 | if (interval <= 63) { | 159 | if (interval <= 63) { |
194 | interval = 63; | 160 | interval = 63; |
@@ -204,221 +170,289 @@ static unsigned long lm95245_set_conversion_rate(struct i2c_client *client, | |||
204 | rate = RATE_CR2500; | 170 | rate = RATE_CR2500; |
205 | } | 171 | } |
206 | 172 | ||
207 | i2c_smbus_write_byte_data(client, LM95245_REG_RW_CONVERS_RATE, rate); | 173 | ret = regmap_write(data->regmap, LM95245_REG_RW_CONVERS_RATE, rate); |
174 | if (ret < 0) | ||
175 | return ret; | ||
208 | 176 | ||
209 | return interval; | 177 | data->interval = interval; |
210 | } | 178 | return 0; |
211 | |||
212 | /* Sysfs stuff */ | ||
213 | static ssize_t show_input(struct device *dev, struct device_attribute *attr, | ||
214 | char *buf) | ||
215 | { | ||
216 | struct lm95245_data *data = lm95245_update_device(dev); | ||
217 | int temp; | ||
218 | int index = to_sensor_dev_attr(attr)->index; | ||
219 | |||
220 | /* | ||
221 | * Index 0 (Local temp) is always signed | ||
222 | * Index 2 (Remote temp) has both signed and unsigned data | ||
223 | * use signed calculation for remote if signed bit is set | ||
224 | */ | ||
225 | if (index == 0 || data->regs[index] & 0x80) | ||
226 | temp = temp_from_reg_signed(data->regs[index], | ||
227 | data->regs[index + 1]); | ||
228 | else | ||
229 | temp = temp_from_reg_unsigned(data->regs[index + 2], | ||
230 | data->regs[index + 3]); | ||
231 | |||
232 | return snprintf(buf, PAGE_SIZE - 1, "%d\n", temp); | ||
233 | } | ||
234 | |||
235 | static ssize_t show_limit(struct device *dev, struct device_attribute *attr, | ||
236 | char *buf) | ||
237 | { | ||
238 | struct lm95245_data *data = lm95245_update_device(dev); | ||
239 | int index = to_sensor_dev_attr(attr)->index; | ||
240 | |||
241 | return snprintf(buf, PAGE_SIZE - 1, "%d\n", | ||
242 | data->regs[index] * 1000); | ||
243 | } | 179 | } |
244 | 180 | ||
245 | static ssize_t set_limit(struct device *dev, struct device_attribute *attr, | 181 | static int lm95245_read_temp(struct device *dev, u32 attr, int channel, |
246 | const char *buf, size_t count) | 182 | long *val) |
247 | { | 183 | { |
248 | struct lm95245_data *data = dev_get_drvdata(dev); | 184 | struct lm95245_data *data = dev_get_drvdata(dev); |
249 | int index = to_sensor_dev_attr(attr)->index; | 185 | struct regmap *regmap = data->regmap; |
250 | struct i2c_client *client = data->client; | 186 | int ret, regl, regh, regvall, regvalh; |
251 | unsigned long val; | 187 | |
252 | 188 | switch (attr) { | |
253 | if (kstrtoul(buf, 10, &val) < 0) | 189 | case hwmon_temp_input: |
254 | return -EINVAL; | 190 | regl = channel ? LM95245_REG_R_REMOTE_TEMPL_S : |
255 | 191 | LM95245_REG_R_LOCAL_TEMPL_S; | |
256 | val /= 1000; | 192 | regh = channel ? LM95245_REG_R_REMOTE_TEMPH_S : |
257 | 193 | LM95245_REG_R_LOCAL_TEMPH_S; | |
258 | val = clamp_val(val, 0, (index == 6 ? 127 : 255)); | 194 | ret = regmap_read(regmap, regl, ®vall); |
259 | 195 | if (ret < 0) | |
260 | mutex_lock(&data->update_lock); | 196 | return ret; |
261 | 197 | ret = regmap_read(regmap, regh, ®valh); | |
262 | data->valid = 0; | 198 | if (ret < 0) |
263 | 199 | return ret; | |
264 | i2c_smbus_write_byte_data(client, lm95245_reg_address[index], val); | 200 | /* |
265 | 201 | * Local temp is always signed. | |
266 | mutex_unlock(&data->update_lock); | 202 | * Remote temp has both signed and unsigned data. |
267 | 203 | * Use signed calculation for remote if signed bit is set | |
268 | return count; | 204 | * or if reported temperature is below signed limit. |
205 | */ | ||
206 | if (!channel || (regvalh & 0x80) || regvalh < 0x7f) { | ||
207 | *val = temp_from_reg_signed(regvalh, regvall); | ||
208 | return 0; | ||
209 | } | ||
210 | ret = regmap_read(regmap, LM95245_REG_R_REMOTE_TEMPL_U, | ||
211 | ®vall); | ||
212 | if (ret < 0) | ||
213 | return ret; | ||
214 | ret = regmap_read(regmap, LM95245_REG_R_REMOTE_TEMPH_U, | ||
215 | ®valh); | ||
216 | if (ret < 0) | ||
217 | return ret; | ||
218 | *val = temp_from_reg_unsigned(regvalh, regvall); | ||
219 | return 0; | ||
220 | case hwmon_temp_max: | ||
221 | ret = regmap_read(regmap, LM95245_REG_RW_REMOTE_OS_LIMIT, | ||
222 | ®valh); | ||
223 | if (ret < 0) | ||
224 | return ret; | ||
225 | *val = regvalh * 1000; | ||
226 | return 0; | ||
227 | case hwmon_temp_crit: | ||
228 | regh = channel ? LM95245_REG_RW_REMOTE_TCRIT_LIMIT : | ||
229 | LM95245_REG_RW_LOCAL_OS_TCRIT_LIMIT; | ||
230 | ret = regmap_read(regmap, regh, ®valh); | ||
231 | if (ret < 0) | ||
232 | return ret; | ||
233 | *val = regvalh * 1000; | ||
234 | return 0; | ||
235 | case hwmon_temp_max_hyst: | ||
236 | ret = regmap_read(regmap, LM95245_REG_RW_REMOTE_OS_LIMIT, | ||
237 | ®valh); | ||
238 | if (ret < 0) | ||
239 | return ret; | ||
240 | ret = regmap_read(regmap, LM95245_REG_RW_COMMON_HYSTERESIS, | ||
241 | ®vall); | ||
242 | if (ret < 0) | ||
243 | return ret; | ||
244 | *val = (regvalh - regvall) * 1000; | ||
245 | return 0; | ||
246 | case hwmon_temp_crit_hyst: | ||
247 | regh = channel ? LM95245_REG_RW_REMOTE_TCRIT_LIMIT : | ||
248 | LM95245_REG_RW_LOCAL_OS_TCRIT_LIMIT; | ||
249 | ret = regmap_read(regmap, regh, ®valh); | ||
250 | if (ret < 0) | ||
251 | return ret; | ||
252 | ret = regmap_read(regmap, LM95245_REG_RW_COMMON_HYSTERESIS, | ||
253 | ®vall); | ||
254 | if (ret < 0) | ||
255 | return ret; | ||
256 | *val = (regvalh - regvall) * 1000; | ||
257 | return 0; | ||
258 | case hwmon_temp_type: | ||
259 | ret = regmap_read(regmap, LM95245_REG_RW_CONFIG2, ®valh); | ||
260 | if (ret < 0) | ||
261 | return ret; | ||
262 | *val = (regvalh & CFG2_REMOTE_TT) ? 1 : 2; | ||
263 | return 0; | ||
264 | case hwmon_temp_offset: | ||
265 | ret = regmap_read(regmap, LM95245_REG_RW_REMOTE_OFFL, | ||
266 | ®vall); | ||
267 | if (ret < 0) | ||
268 | return ret; | ||
269 | ret = regmap_read(regmap, LM95245_REG_RW_REMOTE_OFFH, | ||
270 | ®valh); | ||
271 | if (ret < 0) | ||
272 | return ret; | ||
273 | *val = temp_from_reg_signed(regvalh, regvall); | ||
274 | return 0; | ||
275 | case hwmon_temp_max_alarm: | ||
276 | ret = regmap_read(regmap, LM95245_REG_R_STATUS1, ®valh); | ||
277 | if (ret < 0) | ||
278 | return ret; | ||
279 | *val = !!(regvalh & STATUS1_ROS); | ||
280 | return 0; | ||
281 | case hwmon_temp_crit_alarm: | ||
282 | ret = regmap_read(regmap, LM95245_REG_R_STATUS1, ®valh); | ||
283 | if (ret < 0) | ||
284 | return ret; | ||
285 | *val = !!(regvalh & (channel ? STATUS1_RTCRIT : STATUS1_LOC)); | ||
286 | return 0; | ||
287 | case hwmon_temp_fault: | ||
288 | ret = regmap_read(regmap, LM95245_REG_R_STATUS1, ®valh); | ||
289 | if (ret < 0) | ||
290 | return ret; | ||
291 | *val = !!(regvalh & STATUS1_DIODE_FAULT); | ||
292 | return 0; | ||
293 | default: | ||
294 | return -EOPNOTSUPP; | ||
295 | } | ||
269 | } | 296 | } |
270 | 297 | ||
271 | static ssize_t show_crit_hyst(struct device *dev, struct device_attribute *attr, | 298 | static int lm95245_write_temp(struct device *dev, u32 attr, int channel, |
272 | char *buf) | 299 | long val) |
273 | { | 300 | { |
274 | struct lm95245_data *data = lm95245_update_device(dev); | 301 | struct lm95245_data *data = dev_get_drvdata(dev); |
275 | int index = to_sensor_dev_attr(attr)->index; | 302 | struct regmap *regmap = data->regmap; |
276 | int hyst = data->regs[index] - data->regs[8]; | 303 | unsigned int regval; |
277 | 304 | int ret, reg; | |
278 | return snprintf(buf, PAGE_SIZE - 1, "%d\n", hyst * 1000); | 305 | |
306 | switch (attr) { | ||
307 | case hwmon_temp_max: | ||
308 | val = clamp_val(val / 1000, 0, 255); | ||
309 | ret = regmap_write(regmap, LM95245_REG_RW_REMOTE_OS_LIMIT, val); | ||
310 | return ret; | ||
311 | case hwmon_temp_crit: | ||
312 | reg = channel ? LM95245_REG_RW_REMOTE_TCRIT_LIMIT : | ||
313 | LM95245_REG_RW_LOCAL_OS_TCRIT_LIMIT; | ||
314 | val = clamp_val(val / 1000, 0, channel ? 255 : 127); | ||
315 | ret = regmap_write(regmap, reg, val); | ||
316 | return ret; | ||
317 | case hwmon_temp_crit_hyst: | ||
318 | mutex_lock(&data->update_lock); | ||
319 | ret = regmap_read(regmap, LM95245_REG_RW_LOCAL_OS_TCRIT_LIMIT, | ||
320 | ®val); | ||
321 | if (ret < 0) { | ||
322 | mutex_unlock(&data->update_lock); | ||
323 | return ret; | ||
324 | } | ||
325 | /* Clamp to reasonable range to prevent overflow */ | ||
326 | val = clamp_val(val, -1000000, 1000000); | ||
327 | val = regval - val / 1000; | ||
328 | val = clamp_val(val, 0, 31); | ||
329 | ret = regmap_write(regmap, LM95245_REG_RW_COMMON_HYSTERESIS, | ||
330 | val); | ||
331 | mutex_unlock(&data->update_lock); | ||
332 | return ret; | ||
333 | case hwmon_temp_offset: | ||
334 | val = clamp_val(val, -128000, 127875); | ||
335 | val = val * 256 / 1000; | ||
336 | mutex_lock(&data->update_lock); | ||
337 | ret = regmap_write(regmap, LM95245_REG_RW_REMOTE_OFFL, | ||
338 | val & 0xe0); | ||
339 | if (ret < 0) { | ||
340 | mutex_unlock(&data->update_lock); | ||
341 | return ret; | ||
342 | } | ||
343 | ret = regmap_write(regmap, LM95245_REG_RW_REMOTE_OFFH, | ||
344 | (val >> 8) & 0xff); | ||
345 | mutex_unlock(&data->update_lock); | ||
346 | return ret; | ||
347 | case hwmon_temp_type: | ||
348 | if (val != 1 && val != 2) | ||
349 | return -EINVAL; | ||
350 | ret = regmap_update_bits(regmap, LM95245_REG_RW_CONFIG2, | ||
351 | CFG2_REMOTE_TT, | ||
352 | val == 1 ? CFG2_REMOTE_TT : 0); | ||
353 | return ret; | ||
354 | default: | ||
355 | return -EOPNOTSUPP; | ||
356 | } | ||
279 | } | 357 | } |
280 | 358 | ||
281 | static ssize_t set_crit_hyst(struct device *dev, struct device_attribute *attr, | 359 | static int lm95245_read_chip(struct device *dev, u32 attr, int channel, |
282 | const char *buf, size_t count) | 360 | long *val) |
283 | { | 361 | { |
284 | struct lm95245_data *data = dev_get_drvdata(dev); | 362 | struct lm95245_data *data = dev_get_drvdata(dev); |
285 | int index = to_sensor_dev_attr(attr)->index; | ||
286 | struct i2c_client *client = data->client; | ||
287 | unsigned long val; | ||
288 | int hyst, limit; | ||
289 | |||
290 | if (kstrtoul(buf, 10, &val) < 0) | ||
291 | return -EINVAL; | ||
292 | |||
293 | mutex_lock(&data->update_lock); | ||
294 | |||
295 | limit = i2c_smbus_read_byte_data(client, lm95245_reg_address[index]); | ||
296 | hyst = limit - val / 1000; | ||
297 | hyst = clamp_val(hyst, 0, 31); | ||
298 | data->regs[8] = hyst; | ||
299 | 363 | ||
300 | /* shared crit hysteresis */ | 364 | switch (attr) { |
301 | i2c_smbus_write_byte_data(client, LM95245_REG_RW_COMMON_HYSTERESIS, | 365 | case hwmon_chip_update_interval: |
302 | hyst); | 366 | *val = data->interval; |
303 | 367 | return 0; | |
304 | mutex_unlock(&data->update_lock); | 368 | default: |
305 | 369 | return -EOPNOTSUPP; | |
306 | return count; | 370 | } |
307 | } | 371 | } |
308 | 372 | ||
309 | static ssize_t show_type(struct device *dev, struct device_attribute *attr, | 373 | static int lm95245_write_chip(struct device *dev, u32 attr, int channel, |
310 | char *buf) | 374 | long val) |
311 | { | 375 | { |
312 | struct lm95245_data *data = dev_get_drvdata(dev); | 376 | struct lm95245_data *data = dev_get_drvdata(dev); |
313 | 377 | int ret; | |
314 | return snprintf(buf, PAGE_SIZE - 1, | 378 | |
315 | data->config2 & CFG2_REMOTE_TT ? "1\n" : "2\n"); | 379 | switch (attr) { |
380 | case hwmon_chip_update_interval: | ||
381 | mutex_lock(&data->update_lock); | ||
382 | ret = lm95245_set_conversion_rate(data, val); | ||
383 | mutex_unlock(&data->update_lock); | ||
384 | return ret; | ||
385 | default: | ||
386 | return -EOPNOTSUPP; | ||
387 | } | ||
316 | } | 388 | } |
317 | 389 | ||
318 | static ssize_t set_type(struct device *dev, struct device_attribute *attr, | 390 | static int lm95245_read(struct device *dev, enum hwmon_sensor_types type, |
319 | const char *buf, size_t count) | 391 | u32 attr, int channel, long *val) |
320 | { | 392 | { |
321 | struct lm95245_data *data = dev_get_drvdata(dev); | 393 | switch (type) { |
322 | struct i2c_client *client = data->client; | 394 | case hwmon_chip: |
323 | unsigned long val; | 395 | return lm95245_read_chip(dev, attr, channel, val); |
324 | 396 | case hwmon_temp: | |
325 | if (kstrtoul(buf, 10, &val) < 0) | 397 | return lm95245_read_temp(dev, attr, channel, val); |
326 | return -EINVAL; | 398 | default: |
327 | if (val != 1 && val != 2) | 399 | return -EOPNOTSUPP; |
328 | return -EINVAL; | 400 | } |
329 | |||
330 | mutex_lock(&data->update_lock); | ||
331 | |||
332 | if (val == 1) | ||
333 | data->config2 |= CFG2_REMOTE_TT; | ||
334 | else | ||
335 | data->config2 &= ~CFG2_REMOTE_TT; | ||
336 | |||
337 | data->valid = 0; | ||
338 | |||
339 | i2c_smbus_write_byte_data(client, LM95245_REG_RW_CONFIG2, | ||
340 | data->config2); | ||
341 | |||
342 | mutex_unlock(&data->update_lock); | ||
343 | |||
344 | return count; | ||
345 | } | 401 | } |
346 | 402 | ||
347 | static ssize_t show_alarm(struct device *dev, struct device_attribute *attr, | 403 | static int lm95245_write(struct device *dev, enum hwmon_sensor_types type, |
348 | char *buf) | 404 | u32 attr, int channel, long val) |
349 | { | 405 | { |
350 | struct lm95245_data *data = lm95245_update_device(dev); | 406 | switch (type) { |
351 | int index = to_sensor_dev_attr(attr)->index; | 407 | case hwmon_chip: |
352 | 408 | return lm95245_write_chip(dev, attr, channel, val); | |
353 | return snprintf(buf, PAGE_SIZE - 1, "%d\n", | 409 | case hwmon_temp: |
354 | !!(data->regs[9] & index)); | 410 | return lm95245_write_temp(dev, attr, channel, val); |
411 | default: | ||
412 | return -EOPNOTSUPP; | ||
413 | } | ||
355 | } | 414 | } |
356 | 415 | ||
357 | static ssize_t show_interval(struct device *dev, struct device_attribute *attr, | 416 | static umode_t lm95245_temp_is_visible(const void *data, u32 attr, int channel) |
358 | char *buf) | ||
359 | { | 417 | { |
360 | struct lm95245_data *data = lm95245_update_device(dev); | 418 | switch (attr) { |
361 | 419 | case hwmon_temp_input: | |
362 | return snprintf(buf, PAGE_SIZE - 1, "%lu\n", data->interval); | 420 | case hwmon_temp_max_alarm: |
421 | case hwmon_temp_max_hyst: | ||
422 | case hwmon_temp_crit_alarm: | ||
423 | case hwmon_temp_fault: | ||
424 | return S_IRUGO; | ||
425 | case hwmon_temp_type: | ||
426 | case hwmon_temp_max: | ||
427 | case hwmon_temp_crit: | ||
428 | case hwmon_temp_offset: | ||
429 | return S_IRUGO | S_IWUSR; | ||
430 | case hwmon_temp_crit_hyst: | ||
431 | return (channel == 0) ? S_IRUGO | S_IWUSR : S_IRUGO; | ||
432 | default: | ||
433 | return 0; | ||
434 | } | ||
363 | } | 435 | } |
364 | 436 | ||
365 | static ssize_t set_interval(struct device *dev, struct device_attribute *attr, | 437 | static umode_t lm95245_is_visible(const void *data, |
366 | const char *buf, size_t count) | 438 | enum hwmon_sensor_types type, |
439 | u32 attr, int channel) | ||
367 | { | 440 | { |
368 | struct lm95245_data *data = dev_get_drvdata(dev); | 441 | switch (type) { |
369 | struct i2c_client *client = data->client; | 442 | case hwmon_chip: |
370 | unsigned long val; | 443 | switch (attr) { |
371 | 444 | case hwmon_chip_update_interval: | |
372 | if (kstrtoul(buf, 10, &val) < 0) | 445 | return S_IRUGO | S_IWUSR; |
373 | return -EINVAL; | 446 | default: |
374 | 447 | return 0; | |
375 | mutex_lock(&data->update_lock); | 448 | } |
376 | 449 | case hwmon_temp: | |
377 | data->interval = lm95245_set_conversion_rate(client, val); | 450 | return lm95245_temp_is_visible(data, attr, channel); |
378 | 451 | default: | |
379 | mutex_unlock(&data->update_lock); | 452 | return 0; |
380 | 453 | } | |
381 | return count; | ||
382 | } | 454 | } |
383 | 455 | ||
384 | static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, show_input, NULL, 0); | ||
385 | static SENSOR_DEVICE_ATTR(temp1_crit, S_IWUSR | S_IRUGO, show_limit, | ||
386 | set_limit, 6); | ||
387 | static SENSOR_DEVICE_ATTR(temp1_crit_hyst, S_IWUSR | S_IRUGO, show_crit_hyst, | ||
388 | set_crit_hyst, 6); | ||
389 | static SENSOR_DEVICE_ATTR(temp1_crit_alarm, S_IRUGO, show_alarm, NULL, | ||
390 | STATUS1_LOC); | ||
391 | |||
392 | static SENSOR_DEVICE_ATTR(temp2_input, S_IRUGO, show_input, NULL, 2); | ||
393 | static SENSOR_DEVICE_ATTR(temp2_crit, S_IWUSR | S_IRUGO, show_limit, | ||
394 | set_limit, 7); | ||
395 | static SENSOR_DEVICE_ATTR(temp2_crit_hyst, S_IRUGO, show_crit_hyst, NULL, 7); | ||
396 | static SENSOR_DEVICE_ATTR(temp2_crit_alarm, S_IRUGO, show_alarm, NULL, | ||
397 | STATUS1_RTCRIT); | ||
398 | static SENSOR_DEVICE_ATTR(temp2_type, S_IWUSR | S_IRUGO, show_type, | ||
399 | set_type, 0); | ||
400 | static SENSOR_DEVICE_ATTR(temp2_fault, S_IRUGO, show_alarm, NULL, | ||
401 | STATUS1_DIODE_FAULT); | ||
402 | |||
403 | static DEVICE_ATTR(update_interval, S_IWUSR | S_IRUGO, show_interval, | ||
404 | set_interval); | ||
405 | |||
406 | static struct attribute *lm95245_attrs[] = { | ||
407 | &sensor_dev_attr_temp1_input.dev_attr.attr, | ||
408 | &sensor_dev_attr_temp1_crit.dev_attr.attr, | ||
409 | &sensor_dev_attr_temp1_crit_hyst.dev_attr.attr, | ||
410 | &sensor_dev_attr_temp1_crit_alarm.dev_attr.attr, | ||
411 | &sensor_dev_attr_temp2_input.dev_attr.attr, | ||
412 | &sensor_dev_attr_temp2_crit.dev_attr.attr, | ||
413 | &sensor_dev_attr_temp2_crit_hyst.dev_attr.attr, | ||
414 | &sensor_dev_attr_temp2_crit_alarm.dev_attr.attr, | ||
415 | &sensor_dev_attr_temp2_type.dev_attr.attr, | ||
416 | &sensor_dev_attr_temp2_fault.dev_attr.attr, | ||
417 | &dev_attr_update_interval.attr, | ||
418 | NULL | ||
419 | }; | ||
420 | ATTRIBUTE_GROUPS(lm95245); | ||
421 | |||
422 | /* Return 0 if detection is successful, -ENODEV otherwise */ | 456 | /* Return 0 if detection is successful, -ENODEV otherwise */ |
423 | static int lm95245_detect(struct i2c_client *new_client, | 457 | static int lm95245_detect(struct i2c_client *new_client, |
424 | struct i2c_board_info *info) | 458 | struct i2c_board_info *info) |
@@ -453,44 +487,130 @@ static int lm95245_detect(struct i2c_client *new_client, | |||
453 | return 0; | 487 | return 0; |
454 | } | 488 | } |
455 | 489 | ||
456 | static void lm95245_init_client(struct i2c_client *client, | 490 | static int lm95245_init_client(struct lm95245_data *data) |
457 | struct lm95245_data *data) | ||
458 | { | 491 | { |
459 | data->interval = lm95245_read_conversion_rate(client); | 492 | int ret; |
460 | 493 | ||
461 | data->config1 = i2c_smbus_read_byte_data(client, | 494 | ret = lm95245_read_conversion_rate(data); |
462 | LM95245_REG_RW_CONFIG1); | 495 | if (ret < 0) |
463 | data->config2 = i2c_smbus_read_byte_data(client, | 496 | return ret; |
464 | LM95245_REG_RW_CONFIG2); | 497 | |
465 | 498 | return regmap_update_bits(data->regmap, LM95245_REG_RW_CONFIG1, | |
466 | if (data->config1 & CFG_STOP) { | 499 | CFG_STOP, 0); |
467 | /* Clear the standby bit */ | 500 | } |
468 | data->config1 &= ~CFG_STOP; | 501 | |
469 | i2c_smbus_write_byte_data(client, LM95245_REG_RW_CONFIG1, | 502 | static bool lm95245_is_writeable_reg(struct device *dev, unsigned int reg) |
470 | data->config1); | 503 | { |
504 | switch (reg) { | ||
505 | case LM95245_REG_RW_CONFIG1: | ||
506 | case LM95245_REG_RW_CONVERS_RATE: | ||
507 | case LM95245_REG_W_ONE_SHOT: | ||
508 | case LM95245_REG_RW_CONFIG2: | ||
509 | case LM95245_REG_RW_REMOTE_OFFH: | ||
510 | case LM95245_REG_RW_REMOTE_OFFL: | ||
511 | case LM95245_REG_RW_REMOTE_OS_LIMIT: | ||
512 | case LM95245_REG_RW_LOCAL_OS_TCRIT_LIMIT: | ||
513 | case LM95245_REG_RW_REMOTE_TCRIT_LIMIT: | ||
514 | case LM95245_REG_RW_COMMON_HYSTERESIS: | ||
515 | return true; | ||
516 | default: | ||
517 | return false; | ||
518 | } | ||
519 | } | ||
520 | |||
521 | static bool lm95245_is_volatile_reg(struct device *dev, unsigned int reg) | ||
522 | { | ||
523 | switch (reg) { | ||
524 | case LM95245_REG_R_STATUS1: | ||
525 | case LM95245_REG_R_STATUS2: | ||
526 | case LM95245_REG_R_LOCAL_TEMPH_S: | ||
527 | case LM95245_REG_R_LOCAL_TEMPL_S: | ||
528 | case LM95245_REG_R_REMOTE_TEMPH_S: | ||
529 | case LM95245_REG_R_REMOTE_TEMPL_S: | ||
530 | case LM95245_REG_R_REMOTE_TEMPH_U: | ||
531 | case LM95245_REG_R_REMOTE_TEMPL_U: | ||
532 | return true; | ||
533 | default: | ||
534 | return false; | ||
471 | } | 535 | } |
472 | } | 536 | } |
473 | 537 | ||
538 | static const struct regmap_config lm95245_regmap_config = { | ||
539 | .reg_bits = 8, | ||
540 | .val_bits = 8, | ||
541 | .writeable_reg = lm95245_is_writeable_reg, | ||
542 | .volatile_reg = lm95245_is_volatile_reg, | ||
543 | .cache_type = REGCACHE_RBTREE, | ||
544 | .use_single_rw = true, | ||
545 | }; | ||
546 | |||
547 | static const u32 lm95245_chip_config[] = { | ||
548 | HWMON_C_UPDATE_INTERVAL, | ||
549 | 0 | ||
550 | }; | ||
551 | |||
552 | static const struct hwmon_channel_info lm95245_chip = { | ||
553 | .type = hwmon_chip, | ||
554 | .config = lm95245_chip_config, | ||
555 | }; | ||
556 | |||
557 | static const u32 lm95245_temp_config[] = { | ||
558 | HWMON_T_INPUT | HWMON_T_CRIT | HWMON_T_CRIT_HYST | HWMON_T_CRIT_ALARM, | ||
559 | HWMON_T_INPUT | HWMON_T_MAX | HWMON_T_MAX_HYST | HWMON_T_CRIT | | ||
560 | HWMON_T_CRIT_HYST | HWMON_T_FAULT | HWMON_T_MAX_ALARM | | ||
561 | HWMON_T_CRIT_ALARM | HWMON_T_TYPE | HWMON_T_OFFSET, | ||
562 | 0 | ||
563 | }; | ||
564 | |||
565 | static const struct hwmon_channel_info lm95245_temp = { | ||
566 | .type = hwmon_temp, | ||
567 | .config = lm95245_temp_config, | ||
568 | }; | ||
569 | |||
570 | static const struct hwmon_channel_info *lm95245_info[] = { | ||
571 | &lm95245_chip, | ||
572 | &lm95245_temp, | ||
573 | NULL | ||
574 | }; | ||
575 | |||
576 | static const struct hwmon_ops lm95245_hwmon_ops = { | ||
577 | .is_visible = lm95245_is_visible, | ||
578 | .read = lm95245_read, | ||
579 | .write = lm95245_write, | ||
580 | }; | ||
581 | |||
582 | static const struct hwmon_chip_info lm95245_chip_info = { | ||
583 | .ops = &lm95245_hwmon_ops, | ||
584 | .info = lm95245_info, | ||
585 | }; | ||
586 | |||
474 | static int lm95245_probe(struct i2c_client *client, | 587 | static int lm95245_probe(struct i2c_client *client, |
475 | const struct i2c_device_id *id) | 588 | const struct i2c_device_id *id) |
476 | { | 589 | { |
477 | struct device *dev = &client->dev; | 590 | struct device *dev = &client->dev; |
478 | struct lm95245_data *data; | 591 | struct lm95245_data *data; |
479 | struct device *hwmon_dev; | 592 | struct device *hwmon_dev; |
593 | int ret; | ||
480 | 594 | ||
481 | data = devm_kzalloc(dev, sizeof(struct lm95245_data), GFP_KERNEL); | 595 | data = devm_kzalloc(dev, sizeof(struct lm95245_data), GFP_KERNEL); |
482 | if (!data) | 596 | if (!data) |
483 | return -ENOMEM; | 597 | return -ENOMEM; |
484 | 598 | ||
485 | data->client = client; | 599 | data->regmap = devm_regmap_init_i2c(client, &lm95245_regmap_config); |
600 | if (IS_ERR(data->regmap)) | ||
601 | return PTR_ERR(data->regmap); | ||
602 | |||
486 | mutex_init(&data->update_lock); | 603 | mutex_init(&data->update_lock); |
487 | 604 | ||
488 | /* Initialize the LM95245 chip */ | 605 | /* Initialize the LM95245 chip */ |
489 | lm95245_init_client(client, data); | 606 | ret = lm95245_init_client(data); |
490 | 607 | if (ret < 0) | |
491 | hwmon_dev = devm_hwmon_device_register_with_groups(dev, client->name, | 608 | return ret; |
492 | data, | 609 | |
493 | lm95245_groups); | 610 | hwmon_dev = devm_hwmon_device_register_with_info(dev, client->name, |
611 | data, | ||
612 | &lm95245_chip_info, | ||
613 | NULL); | ||
494 | return PTR_ERR_OR_ZERO(hwmon_dev); | 614 | return PTR_ERR_OR_ZERO(hwmon_dev); |
495 | } | 615 | } |
496 | 616 | ||