2 * lm80.c - From lm_sensors, Linux kernel modules for hardware
4 * Copyright (C) 1998, 1999 Frodo Looijaard <frodol@dds.nl>
5 * and Philip Edelbrock <phil@netroedge.com>
7 * Ported to Linux 2.6 by Tiago Sousa <mirage@kaotik.org>
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
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.
24 #include <linux/module.h>
25 #include <linux/init.h>
26 #include <linux/slab.h>
27 #include <linux/jiffies.h>
28 #include <linux/i2c.h>
29 #include <linux/hwmon.h>
30 #include <linux/hwmon-sysfs.h>
31 #include <linux/err.h>
32 #include <linux/mutex.h>
34 /* Addresses to scan */
35 static const unsigned short normal_i2c
[] = { 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d,
36 0x2e, 0x2f, I2C_CLIENT_END
};
38 /* Many LM80 constants specified below */
40 /* The LM80 registers */
41 #define LM80_REG_IN_MAX(nr) (0x2a + (nr) * 2)
42 #define LM80_REG_IN_MIN(nr) (0x2b + (nr) * 2)
43 #define LM80_REG_IN(nr) (0x20 + (nr))
45 #define LM80_REG_FAN1 0x28
46 #define LM80_REG_FAN2 0x29
47 #define LM80_REG_FAN_MIN(nr) (0x3b + (nr))
49 #define LM80_REG_TEMP 0x27
50 #define LM80_REG_TEMP_HOT_MAX 0x38
51 #define LM80_REG_TEMP_HOT_HYST 0x39
52 #define LM80_REG_TEMP_OS_MAX 0x3a
53 #define LM80_REG_TEMP_OS_HYST 0x3b
55 #define LM80_REG_CONFIG 0x00
56 #define LM80_REG_ALARM1 0x01
57 #define LM80_REG_ALARM2 0x02
58 #define LM80_REG_MASK1 0x03
59 #define LM80_REG_MASK2 0x04
60 #define LM80_REG_FANDIV 0x05
61 #define LM80_REG_RES 0x06
64 /* Conversions. Rounding and limit checking is only done on the TO_REG
65 variants. Note that you should be a bit careful with which arguments
66 these macros are called: arguments may be evaluated more than once.
67 Fixing this is just not worth it. */
69 #define IN_TO_REG(val) (SENSORS_LIMIT(((val) + 5) / 10, 0, 255))
70 #define IN_FROM_REG(val) ((val) * 10)
72 static inline unsigned char FAN_TO_REG(unsigned rpm
, unsigned div
)
76 rpm
= SENSORS_LIMIT(rpm
, 1, 1000000);
77 return SENSORS_LIMIT((1350000 + rpm
* div
/ 2) / (rpm
* div
), 1, 254);
80 #define FAN_FROM_REG(val, div) ((val) == 0 ? -1 : \
81 (val) == 255 ? 0 : 1350000/((div) * (val)))
83 static inline long TEMP_FROM_REG(u16 temp
)
89 res
= 625 * (long) temp
;
91 res
= ((long) temp
- 0x01000) * 625;
96 #define TEMP_LIMIT_FROM_REG(val) (((val) > 0x80 ? \
97 (val) - 0x100 : (val)) * 1000)
99 #define TEMP_LIMIT_TO_REG(val) SENSORS_LIMIT((val) < 0 ? \
100 ((val) - 500) / 1000 : ((val) + 500) / 1000, 0, 255)
102 #define DIV_FROM_REG(val) (1 << (val))
105 * Client data (each client gets its own)
109 struct device
*hwmon_dev
;
110 struct mutex update_lock
;
111 char error
; /* !=0 if error occurred during last update */
112 char valid
; /* !=0 if following fields are valid */
113 unsigned long last_updated
; /* In jiffies */
115 u8 in
[7]; /* Register value */
116 u8 in_max
[7]; /* Register value */
117 u8 in_min
[7]; /* Register value */
118 u8 fan
[2]; /* Register value */
119 u8 fan_min
[2]; /* Register value */
120 u8 fan_div
[2]; /* Register encoding, shifted right */
121 u16 temp
; /* Register values, shifted right */
122 u8 temp_hot_max
; /* Register value */
123 u8 temp_hot_hyst
; /* Register value */
124 u8 temp_os_max
; /* Register value */
125 u8 temp_os_hyst
; /* Register value */
126 u16 alarms
; /* Register encoding, combined */
130 * Functions declaration
133 static int lm80_probe(struct i2c_client
*client
,
134 const struct i2c_device_id
*id
);
135 static int lm80_detect(struct i2c_client
*client
, struct i2c_board_info
*info
);
136 static void lm80_init_client(struct i2c_client
*client
);
137 static int lm80_remove(struct i2c_client
*client
);
138 static struct lm80_data
*lm80_update_device(struct device
*dev
);
139 static int lm80_read_value(struct i2c_client
*client
, u8 reg
);
140 static int lm80_write_value(struct i2c_client
*client
, u8 reg
, u8 value
);
143 * Driver data (common to all clients)
146 static const struct i2c_device_id lm80_id
[] = {
150 MODULE_DEVICE_TABLE(i2c
, lm80_id
);
152 static struct i2c_driver lm80_driver
= {
153 .class = I2C_CLASS_HWMON
,
158 .remove
= lm80_remove
,
160 .detect
= lm80_detect
,
161 .address_list
= normal_i2c
,
168 #define show_in(suffix, value) \
169 static ssize_t show_in_##suffix(struct device *dev, \
170 struct device_attribute *attr, char *buf) \
172 int nr = to_sensor_dev_attr(attr)->index; \
173 struct lm80_data *data = lm80_update_device(dev); \
175 return PTR_ERR(data); \
176 return sprintf(buf, "%d\n", IN_FROM_REG(data->value[nr])); \
182 #define set_in(suffix, value, reg) \
183 static ssize_t set_in_##suffix(struct device *dev, \
184 struct device_attribute *attr, const char *buf, size_t count) \
186 int nr = to_sensor_dev_attr(attr)->index; \
187 struct i2c_client *client = to_i2c_client(dev); \
188 struct lm80_data *data = i2c_get_clientdata(client); \
190 int err = kstrtol(buf, 10, &val); \
194 mutex_lock(&data->update_lock);\
195 data->value[nr] = IN_TO_REG(val); \
196 lm80_write_value(client, reg(nr), data->value[nr]); \
197 mutex_unlock(&data->update_lock);\
200 set_in(min
, in_min
, LM80_REG_IN_MIN
)
201 set_in(max
, in_max
, LM80_REG_IN_MAX
)
203 #define show_fan(suffix, value) \
204 static ssize_t show_fan_##suffix(struct device *dev, \
205 struct device_attribute *attr, char *buf) \
207 int nr = to_sensor_dev_attr(attr)->index; \
208 struct lm80_data *data = lm80_update_device(dev); \
210 return PTR_ERR(data); \
211 return sprintf(buf, "%d\n", FAN_FROM_REG(data->value[nr], \
212 DIV_FROM_REG(data->fan_div[nr]))); \
214 show_fan(min
, fan_min
)
217 static ssize_t
show_fan_div(struct device
*dev
, struct device_attribute
*attr
,
220 int nr
= to_sensor_dev_attr(attr
)->index
;
221 struct lm80_data
*data
= lm80_update_device(dev
);
223 return PTR_ERR(data
);
224 return sprintf(buf
, "%d\n", DIV_FROM_REG(data
->fan_div
[nr
]));
227 static ssize_t
set_fan_min(struct device
*dev
, struct device_attribute
*attr
,
228 const char *buf
, size_t count
)
230 int nr
= to_sensor_dev_attr(attr
)->index
;
231 struct i2c_client
*client
= to_i2c_client(dev
);
232 struct lm80_data
*data
= i2c_get_clientdata(client
);
234 int err
= kstrtoul(buf
, 10, &val
);
238 mutex_lock(&data
->update_lock
);
239 data
->fan_min
[nr
] = FAN_TO_REG(val
, DIV_FROM_REG(data
->fan_div
[nr
]));
240 lm80_write_value(client
, LM80_REG_FAN_MIN(nr
+ 1), data
->fan_min
[nr
]);
241 mutex_unlock(&data
->update_lock
);
245 /* Note: we save and restore the fan minimum here, because its value is
246 determined in part by the fan divisor. This follows the principle of
247 least surprise; the user doesn't expect the fan minimum to change just
248 because the divisor changed. */
249 static ssize_t
set_fan_div(struct device
*dev
, struct device_attribute
*attr
,
250 const char *buf
, size_t count
)
252 int nr
= to_sensor_dev_attr(attr
)->index
;
253 struct i2c_client
*client
= to_i2c_client(dev
);
254 struct lm80_data
*data
= i2c_get_clientdata(client
);
255 unsigned long min
, val
;
257 int err
= kstrtoul(buf
, 10, &val
);
262 mutex_lock(&data
->update_lock
);
263 min
= FAN_FROM_REG(data
->fan_min
[nr
],
264 DIV_FROM_REG(data
->fan_div
[nr
]));
268 data
->fan_div
[nr
] = 0;
271 data
->fan_div
[nr
] = 1;
274 data
->fan_div
[nr
] = 2;
277 data
->fan_div
[nr
] = 3;
280 dev_err(&client
->dev
, "fan_div value %ld not "
281 "supported. Choose one of 1, 2, 4 or 8!\n", val
);
282 mutex_unlock(&data
->update_lock
);
286 reg
= (lm80_read_value(client
, LM80_REG_FANDIV
) & ~(3 << (2 * (nr
+ 1))))
287 | (data
->fan_div
[nr
] << (2 * (nr
+ 1)));
288 lm80_write_value(client
, LM80_REG_FANDIV
, reg
);
290 /* Restore fan_min */
291 data
->fan_min
[nr
] = FAN_TO_REG(min
, DIV_FROM_REG(data
->fan_div
[nr
]));
292 lm80_write_value(client
, LM80_REG_FAN_MIN(nr
+ 1), data
->fan_min
[nr
]);
293 mutex_unlock(&data
->update_lock
);
298 static ssize_t
show_temp_input1(struct device
*dev
,
299 struct device_attribute
*attr
, char *buf
)
301 struct lm80_data
*data
= lm80_update_device(dev
);
303 return PTR_ERR(data
);
304 return sprintf(buf
, "%ld\n", TEMP_FROM_REG(data
->temp
));
307 #define show_temp(suffix, value) \
308 static ssize_t show_temp_##suffix(struct device *dev, \
309 struct device_attribute *attr, char *buf) \
311 struct lm80_data *data = lm80_update_device(dev); \
313 return PTR_ERR(data); \
314 return sprintf(buf, "%d\n", TEMP_LIMIT_FROM_REG(data->value)); \
316 show_temp(hot_max
, temp_hot_max
);
317 show_temp(hot_hyst
, temp_hot_hyst
);
318 show_temp(os_max
, temp_os_max
);
319 show_temp(os_hyst
, temp_os_hyst
);
321 #define set_temp(suffix, value, reg) \
322 static ssize_t set_temp_##suffix(struct device *dev, \
323 struct device_attribute *attr, const char *buf, size_t count) \
325 struct i2c_client *client = to_i2c_client(dev); \
326 struct lm80_data *data = i2c_get_clientdata(client); \
328 int err = kstrtol(buf, 10, &val); \
332 mutex_lock(&data->update_lock); \
333 data->value = TEMP_LIMIT_TO_REG(val); \
334 lm80_write_value(client, reg, data->value); \
335 mutex_unlock(&data->update_lock); \
338 set_temp(hot_max
, temp_hot_max
, LM80_REG_TEMP_HOT_MAX
);
339 set_temp(hot_hyst
, temp_hot_hyst
, LM80_REG_TEMP_HOT_HYST
);
340 set_temp(os_max
, temp_os_max
, LM80_REG_TEMP_OS_MAX
);
341 set_temp(os_hyst
, temp_os_hyst
, LM80_REG_TEMP_OS_HYST
);
343 static ssize_t
show_alarms(struct device
*dev
, struct device_attribute
*attr
,
346 struct lm80_data
*data
= lm80_update_device(dev
);
348 return PTR_ERR(data
);
349 return sprintf(buf
, "%u\n", data
->alarms
);
352 static ssize_t
show_alarm(struct device
*dev
, struct device_attribute
*attr
,
355 int bitnr
= to_sensor_dev_attr(attr
)->index
;
356 struct lm80_data
*data
= lm80_update_device(dev
);
358 return PTR_ERR(data
);
359 return sprintf(buf
, "%u\n", (data
->alarms
>> bitnr
) & 1);
362 static SENSOR_DEVICE_ATTR(in0_min
, S_IWUSR
| S_IRUGO
,
363 show_in_min
, set_in_min
, 0);
364 static SENSOR_DEVICE_ATTR(in1_min
, S_IWUSR
| S_IRUGO
,
365 show_in_min
, set_in_min
, 1);
366 static SENSOR_DEVICE_ATTR(in2_min
, S_IWUSR
| S_IRUGO
,
367 show_in_min
, set_in_min
, 2);
368 static SENSOR_DEVICE_ATTR(in3_min
, S_IWUSR
| S_IRUGO
,
369 show_in_min
, set_in_min
, 3);
370 static SENSOR_DEVICE_ATTR(in4_min
, S_IWUSR
| S_IRUGO
,
371 show_in_min
, set_in_min
, 4);
372 static SENSOR_DEVICE_ATTR(in5_min
, S_IWUSR
| S_IRUGO
,
373 show_in_min
, set_in_min
, 5);
374 static SENSOR_DEVICE_ATTR(in6_min
, S_IWUSR
| S_IRUGO
,
375 show_in_min
, set_in_min
, 6);
376 static SENSOR_DEVICE_ATTR(in0_max
, S_IWUSR
| S_IRUGO
,
377 show_in_max
, set_in_max
, 0);
378 static SENSOR_DEVICE_ATTR(in1_max
, S_IWUSR
| S_IRUGO
,
379 show_in_max
, set_in_max
, 1);
380 static SENSOR_DEVICE_ATTR(in2_max
, S_IWUSR
| S_IRUGO
,
381 show_in_max
, set_in_max
, 2);
382 static SENSOR_DEVICE_ATTR(in3_max
, S_IWUSR
| S_IRUGO
,
383 show_in_max
, set_in_max
, 3);
384 static SENSOR_DEVICE_ATTR(in4_max
, S_IWUSR
| S_IRUGO
,
385 show_in_max
, set_in_max
, 4);
386 static SENSOR_DEVICE_ATTR(in5_max
, S_IWUSR
| S_IRUGO
,
387 show_in_max
, set_in_max
, 5);
388 static SENSOR_DEVICE_ATTR(in6_max
, S_IWUSR
| S_IRUGO
,
389 show_in_max
, set_in_max
, 6);
390 static SENSOR_DEVICE_ATTR(in0_input
, S_IRUGO
, show_in_input
, NULL
, 0);
391 static SENSOR_DEVICE_ATTR(in1_input
, S_IRUGO
, show_in_input
, NULL
, 1);
392 static SENSOR_DEVICE_ATTR(in2_input
, S_IRUGO
, show_in_input
, NULL
, 2);
393 static SENSOR_DEVICE_ATTR(in3_input
, S_IRUGO
, show_in_input
, NULL
, 3);
394 static SENSOR_DEVICE_ATTR(in4_input
, S_IRUGO
, show_in_input
, NULL
, 4);
395 static SENSOR_DEVICE_ATTR(in5_input
, S_IRUGO
, show_in_input
, NULL
, 5);
396 static SENSOR_DEVICE_ATTR(in6_input
, S_IRUGO
, show_in_input
, NULL
, 6);
397 static SENSOR_DEVICE_ATTR(fan1_min
, S_IWUSR
| S_IRUGO
,
398 show_fan_min
, set_fan_min
, 0);
399 static SENSOR_DEVICE_ATTR(fan2_min
, S_IWUSR
| S_IRUGO
,
400 show_fan_min
, set_fan_min
, 1);
401 static SENSOR_DEVICE_ATTR(fan1_input
, S_IRUGO
, show_fan_input
, NULL
, 0);
402 static SENSOR_DEVICE_ATTR(fan2_input
, S_IRUGO
, show_fan_input
, NULL
, 1);
403 static SENSOR_DEVICE_ATTR(fan1_div
, S_IWUSR
| S_IRUGO
,
404 show_fan_div
, set_fan_div
, 0);
405 static SENSOR_DEVICE_ATTR(fan2_div
, S_IWUSR
| S_IRUGO
,
406 show_fan_div
, set_fan_div
, 1);
407 static DEVICE_ATTR(temp1_input
, S_IRUGO
, show_temp_input1
, NULL
);
408 static DEVICE_ATTR(temp1_max
, S_IWUSR
| S_IRUGO
, show_temp_hot_max
,
410 static DEVICE_ATTR(temp1_max_hyst
, S_IWUSR
| S_IRUGO
, show_temp_hot_hyst
,
412 static DEVICE_ATTR(temp1_crit
, S_IWUSR
| S_IRUGO
, show_temp_os_max
,
414 static DEVICE_ATTR(temp1_crit_hyst
, S_IWUSR
| S_IRUGO
, show_temp_os_hyst
,
416 static DEVICE_ATTR(alarms
, S_IRUGO
, show_alarms
, NULL
);
417 static SENSOR_DEVICE_ATTR(in0_alarm
, S_IRUGO
, show_alarm
, NULL
, 0);
418 static SENSOR_DEVICE_ATTR(in1_alarm
, S_IRUGO
, show_alarm
, NULL
, 1);
419 static SENSOR_DEVICE_ATTR(in2_alarm
, S_IRUGO
, show_alarm
, NULL
, 2);
420 static SENSOR_DEVICE_ATTR(in3_alarm
, S_IRUGO
, show_alarm
, NULL
, 3);
421 static SENSOR_DEVICE_ATTR(in4_alarm
, S_IRUGO
, show_alarm
, NULL
, 4);
422 static SENSOR_DEVICE_ATTR(in5_alarm
, S_IRUGO
, show_alarm
, NULL
, 5);
423 static SENSOR_DEVICE_ATTR(in6_alarm
, S_IRUGO
, show_alarm
, NULL
, 6);
424 static SENSOR_DEVICE_ATTR(fan1_alarm
, S_IRUGO
, show_alarm
, NULL
, 10);
425 static SENSOR_DEVICE_ATTR(fan2_alarm
, S_IRUGO
, show_alarm
, NULL
, 11);
426 static SENSOR_DEVICE_ATTR(temp1_max_alarm
, S_IRUGO
, show_alarm
, NULL
, 8);
427 static SENSOR_DEVICE_ATTR(temp1_crit_alarm
, S_IRUGO
, show_alarm
, NULL
, 13);
433 static struct attribute
*lm80_attributes
[] = {
434 &sensor_dev_attr_in0_min
.dev_attr
.attr
,
435 &sensor_dev_attr_in1_min
.dev_attr
.attr
,
436 &sensor_dev_attr_in2_min
.dev_attr
.attr
,
437 &sensor_dev_attr_in3_min
.dev_attr
.attr
,
438 &sensor_dev_attr_in4_min
.dev_attr
.attr
,
439 &sensor_dev_attr_in5_min
.dev_attr
.attr
,
440 &sensor_dev_attr_in6_min
.dev_attr
.attr
,
441 &sensor_dev_attr_in0_max
.dev_attr
.attr
,
442 &sensor_dev_attr_in1_max
.dev_attr
.attr
,
443 &sensor_dev_attr_in2_max
.dev_attr
.attr
,
444 &sensor_dev_attr_in3_max
.dev_attr
.attr
,
445 &sensor_dev_attr_in4_max
.dev_attr
.attr
,
446 &sensor_dev_attr_in5_max
.dev_attr
.attr
,
447 &sensor_dev_attr_in6_max
.dev_attr
.attr
,
448 &sensor_dev_attr_in0_input
.dev_attr
.attr
,
449 &sensor_dev_attr_in1_input
.dev_attr
.attr
,
450 &sensor_dev_attr_in2_input
.dev_attr
.attr
,
451 &sensor_dev_attr_in3_input
.dev_attr
.attr
,
452 &sensor_dev_attr_in4_input
.dev_attr
.attr
,
453 &sensor_dev_attr_in5_input
.dev_attr
.attr
,
454 &sensor_dev_attr_in6_input
.dev_attr
.attr
,
455 &sensor_dev_attr_fan1_min
.dev_attr
.attr
,
456 &sensor_dev_attr_fan2_min
.dev_attr
.attr
,
457 &sensor_dev_attr_fan1_input
.dev_attr
.attr
,
458 &sensor_dev_attr_fan2_input
.dev_attr
.attr
,
459 &sensor_dev_attr_fan1_div
.dev_attr
.attr
,
460 &sensor_dev_attr_fan2_div
.dev_attr
.attr
,
461 &dev_attr_temp1_input
.attr
,
462 &dev_attr_temp1_max
.attr
,
463 &dev_attr_temp1_max_hyst
.attr
,
464 &dev_attr_temp1_crit
.attr
,
465 &dev_attr_temp1_crit_hyst
.attr
,
466 &dev_attr_alarms
.attr
,
467 &sensor_dev_attr_in0_alarm
.dev_attr
.attr
,
468 &sensor_dev_attr_in1_alarm
.dev_attr
.attr
,
469 &sensor_dev_attr_in2_alarm
.dev_attr
.attr
,
470 &sensor_dev_attr_in3_alarm
.dev_attr
.attr
,
471 &sensor_dev_attr_in4_alarm
.dev_attr
.attr
,
472 &sensor_dev_attr_in5_alarm
.dev_attr
.attr
,
473 &sensor_dev_attr_in6_alarm
.dev_attr
.attr
,
474 &sensor_dev_attr_fan1_alarm
.dev_attr
.attr
,
475 &sensor_dev_attr_fan2_alarm
.dev_attr
.attr
,
476 &sensor_dev_attr_temp1_max_alarm
.dev_attr
.attr
,
477 &sensor_dev_attr_temp1_crit_alarm
.dev_attr
.attr
,
481 static const struct attribute_group lm80_group
= {
482 .attrs
= lm80_attributes
,
485 /* Return 0 if detection is successful, -ENODEV otherwise */
486 static int lm80_detect(struct i2c_client
*client
, struct i2c_board_info
*info
)
488 struct i2c_adapter
*adapter
= client
->adapter
;
491 if (!i2c_check_functionality(adapter
, I2C_FUNC_SMBUS_BYTE_DATA
))
494 /* Now, we do the remaining detection. It is lousy. */
495 if (lm80_read_value(client
, LM80_REG_ALARM2
) & 0xc0)
497 for (i
= 0x2a; i
<= 0x3d; i
++) {
498 cur
= i2c_smbus_read_byte_data(client
, i
);
499 if ((i2c_smbus_read_byte_data(client
, i
+ 0x40) != cur
)
500 || (i2c_smbus_read_byte_data(client
, i
+ 0x80) != cur
)
501 || (i2c_smbus_read_byte_data(client
, i
+ 0xc0) != cur
))
505 strlcpy(info
->type
, "lm80", I2C_NAME_SIZE
);
510 static int lm80_probe(struct i2c_client
*client
,
511 const struct i2c_device_id
*id
)
513 struct lm80_data
*data
;
516 data
= kzalloc(sizeof(struct lm80_data
), GFP_KERNEL
);
522 i2c_set_clientdata(client
, data
);
523 mutex_init(&data
->update_lock
);
525 /* Initialize the LM80 chip */
526 lm80_init_client(client
);
528 /* A few vars need to be filled upon startup */
529 data
->fan_min
[0] = lm80_read_value(client
, LM80_REG_FAN_MIN(1));
530 data
->fan_min
[1] = lm80_read_value(client
, LM80_REG_FAN_MIN(2));
532 /* Register sysfs hooks */
533 err
= sysfs_create_group(&client
->dev
.kobj
, &lm80_group
);
537 data
->hwmon_dev
= hwmon_device_register(&client
->dev
);
538 if (IS_ERR(data
->hwmon_dev
)) {
539 err
= PTR_ERR(data
->hwmon_dev
);
546 sysfs_remove_group(&client
->dev
.kobj
, &lm80_group
);
553 static int lm80_remove(struct i2c_client
*client
)
555 struct lm80_data
*data
= i2c_get_clientdata(client
);
557 hwmon_device_unregister(data
->hwmon_dev
);
558 sysfs_remove_group(&client
->dev
.kobj
, &lm80_group
);
564 static int lm80_read_value(struct i2c_client
*client
, u8 reg
)
566 return i2c_smbus_read_byte_data(client
, reg
);
569 static int lm80_write_value(struct i2c_client
*client
, u8 reg
, u8 value
)
571 return i2c_smbus_write_byte_data(client
, reg
, value
);
574 /* Called when we have found a new LM80. */
575 static void lm80_init_client(struct i2c_client
*client
)
577 /* Reset all except Watchdog values and last conversion values
578 This sets fan-divs to 2, among others. This makes most other
579 initializations unnecessary */
580 lm80_write_value(client
, LM80_REG_CONFIG
, 0x80);
581 /* Set 11-bit temperature resolution */
582 lm80_write_value(client
, LM80_REG_RES
, 0x08);
584 /* Start monitoring */
585 lm80_write_value(client
, LM80_REG_CONFIG
, 0x01);
588 static struct lm80_data
*lm80_update_device(struct device
*dev
)
590 struct i2c_client
*client
= to_i2c_client(dev
);
591 struct lm80_data
*data
= i2c_get_clientdata(client
);
595 struct lm80_data
*ret
= data
;
597 mutex_lock(&data
->update_lock
);
600 lm80_init_client(client
);
602 if (time_after(jiffies
, data
->last_updated
+ 2 * HZ
) || !data
->valid
) {
603 dev_dbg(&client
->dev
, "Starting lm80 update\n");
604 for (i
= 0; i
<= 6; i
++) {
605 rv
= lm80_read_value(client
, LM80_REG_IN(i
));
610 rv
= lm80_read_value(client
, LM80_REG_IN_MIN(i
));
613 data
->in_min
[i
] = rv
;
615 rv
= lm80_read_value(client
, LM80_REG_IN_MAX(i
));
618 data
->in_max
[i
] = rv
;
621 rv
= lm80_read_value(client
, LM80_REG_FAN1
);
626 rv
= lm80_read_value(client
, LM80_REG_FAN_MIN(1));
629 data
->fan_min
[0] = rv
;
631 rv
= lm80_read_value(client
, LM80_REG_FAN2
);
636 rv
= lm80_read_value(client
, LM80_REG_FAN_MIN(2));
639 data
->fan_min
[1] = rv
;
641 prev_rv
= rv
= lm80_read_value(client
, LM80_REG_TEMP
);
644 rv
= lm80_read_value(client
, LM80_REG_RES
);
647 data
->temp
= (prev_rv
<< 8) | (rv
& 0xf0);
649 rv
= lm80_read_value(client
, LM80_REG_TEMP_OS_MAX
);
652 data
->temp_os_max
= rv
;
654 rv
= lm80_read_value(client
, LM80_REG_TEMP_OS_HYST
);
657 data
->temp_os_hyst
= rv
;
659 rv
= lm80_read_value(client
, LM80_REG_TEMP_HOT_MAX
);
662 data
->temp_hot_max
= rv
;
664 rv
= lm80_read_value(client
, LM80_REG_TEMP_HOT_HYST
);
667 data
->temp_hot_hyst
= rv
;
669 rv
= lm80_read_value(client
, LM80_REG_FANDIV
);
672 data
->fan_div
[0] = (rv
>> 2) & 0x03;
673 data
->fan_div
[1] = (rv
>> 4) & 0x03;
675 prev_rv
= rv
= lm80_read_value(client
, LM80_REG_ALARM1
);
678 rv
= lm80_read_value(client
, LM80_REG_ALARM2
);
681 data
->alarms
= prev_rv
+ (rv
<< 8);
683 data
->last_updated
= jiffies
;
695 mutex_unlock(&data
->update_lock
);
700 module_i2c_driver(lm80_driver
);
702 MODULE_AUTHOR("Frodo Looijaard <frodol@dds.nl> and "
703 "Philip Edelbrock <phil@netroedge.com>");
704 MODULE_DESCRIPTION("LM80 driver");
705 MODULE_LICENSE("GPL");