hwmon: (lm75) Handle cleanup with devm_add_action
[deliverable/linux.git] / drivers / hwmon / lm75.c
1 /*
2 * lm75.c - Part of lm_sensors, Linux kernel modules for hardware
3 * monitoring
4 * Copyright (c) 1998, 1999 Frodo Looijaard <frodol@dds.nl>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19 */
20
21 #include <linux/module.h>
22 #include <linux/init.h>
23 #include <linux/slab.h>
24 #include <linux/jiffies.h>
25 #include <linux/i2c.h>
26 #include <linux/hwmon.h>
27 #include <linux/hwmon-sysfs.h>
28 #include <linux/err.h>
29 #include <linux/mutex.h>
30 #include <linux/of.h>
31 #include <linux/thermal.h>
32 #include "lm75.h"
33
34
35 /*
36 * This driver handles the LM75 and compatible digital temperature sensors.
37 */
38
39 enum lm75_type { /* keep sorted in alphabetical order */
40 adt75,
41 ds1775,
42 ds75,
43 ds7505,
44 g751,
45 lm75,
46 lm75a,
47 lm75b,
48 max6625,
49 max6626,
50 mcp980x,
51 stds75,
52 tcn75,
53 tmp100,
54 tmp101,
55 tmp105,
56 tmp112,
57 tmp175,
58 tmp275,
59 tmp75,
60 tmp75c,
61 };
62
63 /* Addresses scanned */
64 static const unsigned short normal_i2c[] = { 0x48, 0x49, 0x4a, 0x4b, 0x4c,
65 0x4d, 0x4e, 0x4f, I2C_CLIENT_END };
66
67
68 /* The LM75 registers */
69 #define LM75_REG_CONF 0x01
70 static const u8 LM75_REG_TEMP[3] = {
71 0x00, /* input */
72 0x03, /* max */
73 0x02, /* hyst */
74 };
75
76 /* Each client has this additional data */
77 struct lm75_data {
78 struct i2c_client *client;
79 struct mutex update_lock;
80 u8 orig_conf;
81 u8 resolution; /* In bits, between 9 and 12 */
82 u8 resolution_limits;
83 char valid; /* !=0 if registers are valid */
84 unsigned long last_updated; /* In jiffies */
85 unsigned long sample_time; /* In jiffies */
86 s16 temp[3]; /* Register values,
87 0 = input
88 1 = max
89 2 = hyst */
90 };
91
92 static int lm75_read_value(struct i2c_client *client, u8 reg);
93 static int lm75_write_value(struct i2c_client *client, u8 reg, u16 value);
94 static struct lm75_data *lm75_update_device(struct device *dev);
95
96
97 /*-----------------------------------------------------------------------*/
98
99 static inline long lm75_reg_to_mc(s16 temp, u8 resolution)
100 {
101 return ((temp >> (16 - resolution)) * 1000) >> (resolution - 8);
102 }
103
104 /* sysfs attributes for hwmon */
105
106 static int lm75_read_temp(void *dev, int *temp)
107 {
108 struct lm75_data *data = lm75_update_device(dev);
109
110 if (IS_ERR(data))
111 return PTR_ERR(data);
112
113 *temp = lm75_reg_to_mc(data->temp[0], data->resolution);
114
115 return 0;
116 }
117
118 static ssize_t show_temp(struct device *dev, struct device_attribute *da,
119 char *buf)
120 {
121 struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
122 struct lm75_data *data = lm75_update_device(dev);
123
124 if (IS_ERR(data))
125 return PTR_ERR(data);
126
127 return sprintf(buf, "%ld\n", lm75_reg_to_mc(data->temp[attr->index],
128 data->resolution));
129 }
130
131 static ssize_t set_temp(struct device *dev, struct device_attribute *da,
132 const char *buf, size_t count)
133 {
134 struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
135 struct lm75_data *data = dev_get_drvdata(dev);
136 struct i2c_client *client = data->client;
137 int nr = attr->index;
138 long temp;
139 int error;
140 u8 resolution;
141
142 error = kstrtol(buf, 10, &temp);
143 if (error)
144 return error;
145
146 /*
147 * Resolution of limit registers is assumed to be the same as the
148 * temperature input register resolution unless given explicitly.
149 */
150 if (attr->index && data->resolution_limits)
151 resolution = data->resolution_limits;
152 else
153 resolution = data->resolution;
154
155 mutex_lock(&data->update_lock);
156 temp = clamp_val(temp, LM75_TEMP_MIN, LM75_TEMP_MAX);
157 data->temp[nr] = DIV_ROUND_CLOSEST(temp << (resolution - 8),
158 1000) << (16 - resolution);
159 lm75_write_value(client, LM75_REG_TEMP[nr], data->temp[nr]);
160 mutex_unlock(&data->update_lock);
161 return count;
162 }
163
164 static SENSOR_DEVICE_ATTR(temp1_max, S_IWUSR | S_IRUGO,
165 show_temp, set_temp, 1);
166 static SENSOR_DEVICE_ATTR(temp1_max_hyst, S_IWUSR | S_IRUGO,
167 show_temp, set_temp, 2);
168 static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, show_temp, NULL, 0);
169
170 static struct attribute *lm75_attrs[] = {
171 &sensor_dev_attr_temp1_input.dev_attr.attr,
172 &sensor_dev_attr_temp1_max.dev_attr.attr,
173 &sensor_dev_attr_temp1_max_hyst.dev_attr.attr,
174
175 NULL
176 };
177 ATTRIBUTE_GROUPS(lm75);
178
179 static const struct thermal_zone_of_device_ops lm75_of_thermal_ops = {
180 .get_temp = lm75_read_temp,
181 };
182
183 /*-----------------------------------------------------------------------*/
184
185 /* device probe and removal */
186
187 static void lm75_remove(void *data)
188 {
189 struct lm75_data *lm75 = data;
190 struct i2c_client *client = lm75->client;
191
192 i2c_smbus_write_byte_data(client, LM75_REG_CONF, lm75->orig_conf);
193 }
194
195 static int
196 lm75_probe(struct i2c_client *client, const struct i2c_device_id *id)
197 {
198 struct device *dev = &client->dev;
199 struct device *hwmon_dev;
200 struct lm75_data *data;
201 int status;
202 u8 set_mask, clr_mask;
203 int new;
204 enum lm75_type kind = id->driver_data;
205
206 if (!i2c_check_functionality(client->adapter,
207 I2C_FUNC_SMBUS_BYTE_DATA | I2C_FUNC_SMBUS_WORD_DATA))
208 return -EIO;
209
210 data = devm_kzalloc(dev, sizeof(struct lm75_data), GFP_KERNEL);
211 if (!data)
212 return -ENOMEM;
213
214 data->client = client;
215 i2c_set_clientdata(client, data);
216 mutex_init(&data->update_lock);
217
218 /* Set to LM75 resolution (9 bits, 1/2 degree C) and range.
219 * Then tweak to be more precise when appropriate.
220 */
221 set_mask = 0;
222 clr_mask = LM75_SHUTDOWN; /* continuous conversions */
223
224 switch (kind) {
225 case adt75:
226 clr_mask |= 1 << 5; /* not one-shot mode */
227 data->resolution = 12;
228 data->sample_time = HZ / 8;
229 break;
230 case ds1775:
231 case ds75:
232 case stds75:
233 clr_mask |= 3 << 5;
234 set_mask |= 2 << 5; /* 11-bit mode */
235 data->resolution = 11;
236 data->sample_time = HZ;
237 break;
238 case ds7505:
239 set_mask |= 3 << 5; /* 12-bit mode */
240 data->resolution = 12;
241 data->sample_time = HZ / 4;
242 break;
243 case g751:
244 case lm75:
245 case lm75a:
246 data->resolution = 9;
247 data->sample_time = HZ / 2;
248 break;
249 case lm75b:
250 data->resolution = 11;
251 data->sample_time = HZ / 4;
252 break;
253 case max6625:
254 data->resolution = 9;
255 data->sample_time = HZ / 4;
256 break;
257 case max6626:
258 data->resolution = 12;
259 data->resolution_limits = 9;
260 data->sample_time = HZ / 4;
261 break;
262 case tcn75:
263 data->resolution = 9;
264 data->sample_time = HZ / 8;
265 break;
266 case mcp980x:
267 data->resolution_limits = 9;
268 /* fall through */
269 case tmp100:
270 case tmp101:
271 set_mask |= 3 << 5; /* 12-bit mode */
272 data->resolution = 12;
273 data->sample_time = HZ;
274 clr_mask |= 1 << 7; /* not one-shot mode */
275 break;
276 case tmp112:
277 set_mask |= 3 << 5; /* 12-bit mode */
278 clr_mask |= 1 << 7; /* not one-shot mode */
279 data->resolution = 12;
280 data->sample_time = HZ / 4;
281 break;
282 case tmp105:
283 case tmp175:
284 case tmp275:
285 case tmp75:
286 set_mask |= 3 << 5; /* 12-bit mode */
287 clr_mask |= 1 << 7; /* not one-shot mode */
288 data->resolution = 12;
289 data->sample_time = HZ / 2;
290 break;
291 case tmp75c:
292 clr_mask |= 1 << 5; /* not one-shot mode */
293 data->resolution = 12;
294 data->sample_time = HZ / 4;
295 break;
296 }
297
298 /* configure as specified */
299 status = lm75_read_value(client, LM75_REG_CONF);
300 if (status < 0) {
301 dev_dbg(dev, "Can't read config? %d\n", status);
302 return status;
303 }
304 data->orig_conf = status;
305 new = status & ~clr_mask;
306 new |= set_mask;
307 if (status != new)
308 lm75_write_value(client, LM75_REG_CONF, new);
309
310 devm_add_action(dev, lm75_remove, data);
311
312 dev_dbg(dev, "Config %02x\n", new);
313
314 hwmon_dev = devm_hwmon_device_register_with_groups(dev, client->name,
315 data, lm75_groups);
316 if (IS_ERR(hwmon_dev))
317 return PTR_ERR(hwmon_dev);
318
319 devm_thermal_zone_of_sensor_register(hwmon_dev, 0,
320 hwmon_dev,
321 &lm75_of_thermal_ops);
322
323 dev_info(dev, "%s: sensor '%s'\n", dev_name(hwmon_dev), client->name);
324
325 return 0;
326 }
327
328 static const struct i2c_device_id lm75_ids[] = {
329 { "adt75", adt75, },
330 { "ds1775", ds1775, },
331 { "ds75", ds75, },
332 { "ds7505", ds7505, },
333 { "g751", g751, },
334 { "lm75", lm75, },
335 { "lm75a", lm75a, },
336 { "lm75b", lm75b, },
337 { "max6625", max6625, },
338 { "max6626", max6626, },
339 { "mcp980x", mcp980x, },
340 { "stds75", stds75, },
341 { "tcn75", tcn75, },
342 { "tmp100", tmp100, },
343 { "tmp101", tmp101, },
344 { "tmp105", tmp105, },
345 { "tmp112", tmp112, },
346 { "tmp175", tmp175, },
347 { "tmp275", tmp275, },
348 { "tmp75", tmp75, },
349 { "tmp75c", tmp75c, },
350 { /* LIST END */ }
351 };
352 MODULE_DEVICE_TABLE(i2c, lm75_ids);
353
354 #define LM75A_ID 0xA1
355
356 /* Return 0 if detection is successful, -ENODEV otherwise */
357 static int lm75_detect(struct i2c_client *new_client,
358 struct i2c_board_info *info)
359 {
360 struct i2c_adapter *adapter = new_client->adapter;
361 int i;
362 int conf, hyst, os;
363 bool is_lm75a = 0;
364
365 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA |
366 I2C_FUNC_SMBUS_WORD_DATA))
367 return -ENODEV;
368
369 /*
370 * Now, we do the remaining detection. There is no identification-
371 * dedicated register so we have to rely on several tricks:
372 * unused bits, registers cycling over 8-address boundaries,
373 * addresses 0x04-0x07 returning the last read value.
374 * The cycling+unused addresses combination is not tested,
375 * since it would significantly slow the detection down and would
376 * hardly add any value.
377 *
378 * The National Semiconductor LM75A is different than earlier
379 * LM75s. It has an ID byte of 0xaX (where X is the chip
380 * revision, with 1 being the only revision in existence) in
381 * register 7, and unused registers return 0xff rather than the
382 * last read value.
383 *
384 * Note that this function only detects the original National
385 * Semiconductor LM75 and the LM75A. Clones from other vendors
386 * aren't detected, on purpose, because they are typically never
387 * found on PC hardware. They are found on embedded designs where
388 * they can be instantiated explicitly so detection is not needed.
389 * The absence of identification registers on all these clones
390 * would make their exhaustive detection very difficult and weak,
391 * and odds are that the driver would bind to unsupported devices.
392 */
393
394 /* Unused bits */
395 conf = i2c_smbus_read_byte_data(new_client, 1);
396 if (conf & 0xe0)
397 return -ENODEV;
398
399 /* First check for LM75A */
400 if (i2c_smbus_read_byte_data(new_client, 7) == LM75A_ID) {
401 /* LM75A returns 0xff on unused registers so
402 just to be sure we check for that too. */
403 if (i2c_smbus_read_byte_data(new_client, 4) != 0xff
404 || i2c_smbus_read_byte_data(new_client, 5) != 0xff
405 || i2c_smbus_read_byte_data(new_client, 6) != 0xff)
406 return -ENODEV;
407 is_lm75a = 1;
408 hyst = i2c_smbus_read_byte_data(new_client, 2);
409 os = i2c_smbus_read_byte_data(new_client, 3);
410 } else { /* Traditional style LM75 detection */
411 /* Unused addresses */
412 hyst = i2c_smbus_read_byte_data(new_client, 2);
413 if (i2c_smbus_read_byte_data(new_client, 4) != hyst
414 || i2c_smbus_read_byte_data(new_client, 5) != hyst
415 || i2c_smbus_read_byte_data(new_client, 6) != hyst
416 || i2c_smbus_read_byte_data(new_client, 7) != hyst)
417 return -ENODEV;
418 os = i2c_smbus_read_byte_data(new_client, 3);
419 if (i2c_smbus_read_byte_data(new_client, 4) != os
420 || i2c_smbus_read_byte_data(new_client, 5) != os
421 || i2c_smbus_read_byte_data(new_client, 6) != os
422 || i2c_smbus_read_byte_data(new_client, 7) != os)
423 return -ENODEV;
424 }
425 /*
426 * It is very unlikely that this is a LM75 if both
427 * hysteresis and temperature limit registers are 0.
428 */
429 if (hyst == 0 && os == 0)
430 return -ENODEV;
431
432 /* Addresses cycling */
433 for (i = 8; i <= 248; i += 40) {
434 if (i2c_smbus_read_byte_data(new_client, i + 1) != conf
435 || i2c_smbus_read_byte_data(new_client, i + 2) != hyst
436 || i2c_smbus_read_byte_data(new_client, i + 3) != os)
437 return -ENODEV;
438 if (is_lm75a && i2c_smbus_read_byte_data(new_client, i + 7)
439 != LM75A_ID)
440 return -ENODEV;
441 }
442
443 strlcpy(info->type, is_lm75a ? "lm75a" : "lm75", I2C_NAME_SIZE);
444
445 return 0;
446 }
447
448 #ifdef CONFIG_PM
449 static int lm75_suspend(struct device *dev)
450 {
451 int status;
452 struct i2c_client *client = to_i2c_client(dev);
453 status = lm75_read_value(client, LM75_REG_CONF);
454 if (status < 0) {
455 dev_dbg(&client->dev, "Can't read config? %d\n", status);
456 return status;
457 }
458 status = status | LM75_SHUTDOWN;
459 lm75_write_value(client, LM75_REG_CONF, status);
460 return 0;
461 }
462
463 static int lm75_resume(struct device *dev)
464 {
465 int status;
466 struct i2c_client *client = to_i2c_client(dev);
467 status = lm75_read_value(client, LM75_REG_CONF);
468 if (status < 0) {
469 dev_dbg(&client->dev, "Can't read config? %d\n", status);
470 return status;
471 }
472 status = status & ~LM75_SHUTDOWN;
473 lm75_write_value(client, LM75_REG_CONF, status);
474 return 0;
475 }
476
477 static const struct dev_pm_ops lm75_dev_pm_ops = {
478 .suspend = lm75_suspend,
479 .resume = lm75_resume,
480 };
481 #define LM75_DEV_PM_OPS (&lm75_dev_pm_ops)
482 #else
483 #define LM75_DEV_PM_OPS NULL
484 #endif /* CONFIG_PM */
485
486 static struct i2c_driver lm75_driver = {
487 .class = I2C_CLASS_HWMON,
488 .driver = {
489 .name = "lm75",
490 .pm = LM75_DEV_PM_OPS,
491 },
492 .probe = lm75_probe,
493 .id_table = lm75_ids,
494 .detect = lm75_detect,
495 .address_list = normal_i2c,
496 };
497
498 /*-----------------------------------------------------------------------*/
499
500 /* register access */
501
502 /*
503 * All registers are word-sized, except for the configuration register.
504 * LM75 uses a high-byte first convention, which is exactly opposite to
505 * the SMBus standard.
506 */
507 static int lm75_read_value(struct i2c_client *client, u8 reg)
508 {
509 if (reg == LM75_REG_CONF)
510 return i2c_smbus_read_byte_data(client, reg);
511 else
512 return i2c_smbus_read_word_swapped(client, reg);
513 }
514
515 static int lm75_write_value(struct i2c_client *client, u8 reg, u16 value)
516 {
517 if (reg == LM75_REG_CONF)
518 return i2c_smbus_write_byte_data(client, reg, value);
519 else
520 return i2c_smbus_write_word_swapped(client, reg, value);
521 }
522
523 static struct lm75_data *lm75_update_device(struct device *dev)
524 {
525 struct lm75_data *data = dev_get_drvdata(dev);
526 struct i2c_client *client = data->client;
527 struct lm75_data *ret = data;
528
529 mutex_lock(&data->update_lock);
530
531 if (time_after(jiffies, data->last_updated + data->sample_time)
532 || !data->valid) {
533 int i;
534 dev_dbg(&client->dev, "Starting lm75 update\n");
535
536 for (i = 0; i < ARRAY_SIZE(data->temp); i++) {
537 int status;
538
539 status = lm75_read_value(client, LM75_REG_TEMP[i]);
540 if (unlikely(status < 0)) {
541 dev_dbg(dev,
542 "LM75: Failed to read value: reg %d, error %d\n",
543 LM75_REG_TEMP[i], status);
544 ret = ERR_PTR(status);
545 data->valid = 0;
546 goto abort;
547 }
548 data->temp[i] = status;
549 }
550 data->last_updated = jiffies;
551 data->valid = 1;
552 }
553
554 abort:
555 mutex_unlock(&data->update_lock);
556 return ret;
557 }
558
559 module_i2c_driver(lm75_driver);
560
561 MODULE_AUTHOR("Frodo Looijaard <frodol@dds.nl>");
562 MODULE_DESCRIPTION("LM75 driver");
563 MODULE_LICENSE("GPL");
This page took 0.064112 seconds and 6 git commands to generate.