2 * lm90.c - Part of lm_sensors, Linux kernel modules for hardware
4 * Copyright (C) 2003-2010 Jean Delvare <khali@linux-fr.org>
6 * Based on the lm83 driver. The LM90 is a sensor chip made by National
7 * Semiconductor. It reports up to two temperatures (its own plus up to
8 * one external one) with a 0.125 deg resolution (1 deg for local
9 * temperature) and a 3-4 deg accuracy.
11 * This driver also supports the LM89 and LM99, two other sensor chips
12 * made by National Semiconductor. Both have an increased remote
13 * temperature measurement accuracy (1 degree), and the LM99
14 * additionally shifts remote temperatures (measured and limits) by 16
15 * degrees, which allows for higher temperatures measurement.
16 * Note that there is no way to differentiate between both chips.
17 * When device is auto-detected, the driver will assume an LM99.
19 * This driver also supports the LM86, another sensor chip made by
20 * National Semiconductor. It is exactly similar to the LM90 except it
21 * has a higher accuracy.
23 * This driver also supports the ADM1032, a sensor chip made by Analog
24 * Devices. That chip is similar to the LM90, with a few differences
25 * that are not handled by this driver. Among others, it has a higher
26 * accuracy than the LM90, much like the LM86 does.
28 * This driver also supports the MAX6657, MAX6658 and MAX6659 sensor
29 * chips made by Maxim. These chips are similar to the LM86.
30 * Note that there is no easy way to differentiate between the three
31 * variants. The extra address and features of the MAX6659 are not
32 * supported by this driver. These chips lack the remote temperature
35 * This driver also supports the MAX6646, MAX6647, MAX6648, MAX6649 and
36 * MAX6692 chips made by Maxim. These are again similar to the LM86,
37 * but they use unsigned temperature values and can report temperatures
38 * from 0 to 145 degrees.
40 * This driver also supports the MAX6680 and MAX6681, two other sensor
41 * chips made by Maxim. These are quite similar to the other Maxim
42 * chips. The MAX6680 and MAX6681 only differ in the pinout so they can
43 * be treated identically.
45 * This driver also supports the ADT7461 chip from Analog Devices.
46 * It's supported in both compatibility and extended mode. It is mostly
47 * compatible with LM90 except for a data format difference for the
48 * temperature value registers.
50 * Since the LM90 was the first chipset supported by this driver, most
51 * comments will refer to this chipset, but are actually general and
52 * concern all supported chipsets, unless mentioned otherwise.
54 * This program is free software; you can redistribute it and/or modify
55 * it under the terms of the GNU General Public License as published by
56 * the Free Software Foundation; either version 2 of the License, or
57 * (at your option) any later version.
59 * This program is distributed in the hope that it will be useful,
60 * but WITHOUT ANY WARRANTY; without even the implied warranty of
61 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
62 * GNU General Public License for more details.
64 * You should have received a copy of the GNU General Public License
65 * along with this program; if not, write to the Free Software
66 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
69 #include <linux/module.h>
70 #include <linux/init.h>
71 #include <linux/slab.h>
72 #include <linux/jiffies.h>
73 #include <linux/i2c.h>
74 #include <linux/hwmon-sysfs.h>
75 #include <linux/hwmon.h>
76 #include <linux/err.h>
77 #include <linux/mutex.h>
78 #include <linux/sysfs.h>
82 * Address is fully defined internally and cannot be changed except for
83 * MAX6659, MAX6680 and MAX6681.
84 * LM86, LM89, LM90, LM99, ADM1032, ADM1032-1, ADT7461, MAX6649, MAX6657
85 * and MAX6658 have address 0x4c.
86 * ADM1032-2, ADT7461-2, LM89-1, LM99-1 and MAX6646 have address 0x4d.
87 * MAX6647 has address 0x4e.
88 * MAX6659 can have address 0x4c, 0x4d or 0x4e (unsupported).
89 * MAX6680 and MAX6681 can have address 0x18, 0x19, 0x1a, 0x29, 0x2a, 0x2b,
93 static const unsigned short normal_i2c
[] = {
94 0x18, 0x19, 0x1a, 0x29, 0x2a, 0x2b, 0x4c, 0x4d, 0x4e, I2C_CLIENT_END
};
96 enum chips
{ lm90
, adm1032
, lm99
, lm86
, max6657
, adt7461
, max6680
, max6646
,
103 #define LM90_REG_R_MAN_ID 0xFE
104 #define LM90_REG_R_CHIP_ID 0xFF
105 #define LM90_REG_R_CONFIG1 0x03
106 #define LM90_REG_W_CONFIG1 0x09
107 #define LM90_REG_R_CONFIG2 0xBF
108 #define LM90_REG_W_CONFIG2 0xBF
109 #define LM90_REG_R_CONVRATE 0x04
110 #define LM90_REG_W_CONVRATE 0x0A
111 #define LM90_REG_R_STATUS 0x02
112 #define LM90_REG_R_LOCAL_TEMP 0x00
113 #define LM90_REG_R_LOCAL_HIGH 0x05
114 #define LM90_REG_W_LOCAL_HIGH 0x0B
115 #define LM90_REG_R_LOCAL_LOW 0x06
116 #define LM90_REG_W_LOCAL_LOW 0x0C
117 #define LM90_REG_R_LOCAL_CRIT 0x20
118 #define LM90_REG_W_LOCAL_CRIT 0x20
119 #define LM90_REG_R_REMOTE_TEMPH 0x01
120 #define LM90_REG_R_REMOTE_TEMPL 0x10
121 #define LM90_REG_R_REMOTE_OFFSH 0x11
122 #define LM90_REG_W_REMOTE_OFFSH 0x11
123 #define LM90_REG_R_REMOTE_OFFSL 0x12
124 #define LM90_REG_W_REMOTE_OFFSL 0x12
125 #define LM90_REG_R_REMOTE_HIGHH 0x07
126 #define LM90_REG_W_REMOTE_HIGHH 0x0D
127 #define LM90_REG_R_REMOTE_HIGHL 0x13
128 #define LM90_REG_W_REMOTE_HIGHL 0x13
129 #define LM90_REG_R_REMOTE_LOWH 0x08
130 #define LM90_REG_W_REMOTE_LOWH 0x0E
131 #define LM90_REG_R_REMOTE_LOWL 0x14
132 #define LM90_REG_W_REMOTE_LOWL 0x14
133 #define LM90_REG_R_REMOTE_CRIT 0x19
134 #define LM90_REG_W_REMOTE_CRIT 0x19
135 #define LM90_REG_R_TCRIT_HYST 0x21
136 #define LM90_REG_W_TCRIT_HYST 0x21
138 /* MAX6646/6647/6649/6657/6658/6659 registers */
140 #define MAX6657_REG_R_LOCAL_TEMPL 0x11
145 #define LM90_FLAG_ADT7461_EXT 0x01 /* ADT7461 extended mode */
148 * Functions declaration
151 static int lm90_detect(struct i2c_client
*client
, struct i2c_board_info
*info
);
152 static int lm90_probe(struct i2c_client
*client
,
153 const struct i2c_device_id
*id
);
154 static void lm90_init_client(struct i2c_client
*client
);
155 static void lm90_alert(struct i2c_client
*client
, unsigned int flag
);
156 static int lm90_remove(struct i2c_client
*client
);
157 static struct lm90_data
*lm90_update_device(struct device
*dev
);
160 * Driver data (common to all clients)
163 static const struct i2c_device_id lm90_id
[] = {
164 { "adm1032", adm1032
},
165 { "adt7461", adt7461
},
170 { "max6646", max6646
},
171 { "max6647", max6646
},
172 { "max6649", max6646
},
173 { "max6657", max6657
},
174 { "max6658", max6657
},
175 { "max6659", max6657
},
176 { "max6680", max6680
},
177 { "max6681", max6680
},
178 { "w83l771", w83l771
},
181 MODULE_DEVICE_TABLE(i2c
, lm90_id
);
183 static struct i2c_driver lm90_driver
= {
184 .class = I2C_CLASS_HWMON
,
189 .remove
= lm90_remove
,
192 .detect
= lm90_detect
,
193 .address_list
= normal_i2c
,
197 * Client data (each client gets its own)
201 struct device
*hwmon_dev
;
202 struct mutex update_lock
;
203 char valid
; /* zero until following fields are valid */
204 unsigned long last_updated
; /* in jiffies */
208 u8 config_orig
; /* Original configuration register value */
209 u8 alert_alarms
; /* Which alarm bits trigger ALERT# */
211 /* registers values */
212 s8 temp8
[4]; /* 0: local low limit
214 2: local critical limit
215 3: remote critical limit */
216 s16 temp11
[5]; /* 0: remote input
219 3: remote offset (except max6646 and max6657)
222 u8 alarms
; /* bitvector */
227 * For local temperatures and limits, critical limits and the hysteresis
228 * value, the LM90 uses signed 8-bit values with LSB = 1 degree Celsius.
229 * For remote temperatures and limits, it uses signed 11-bit values with
230 * LSB = 0.125 degree Celsius, left-justified in 16-bit registers. Some
231 * Maxim chips use unsigned values.
234 static inline int temp_from_s8(s8 val
)
239 static inline int temp_from_u8(u8 val
)
244 static inline int temp_from_s16(s16 val
)
246 return val
/ 32 * 125;
249 static inline int temp_from_u16(u16 val
)
251 return val
/ 32 * 125;
254 static s8
temp_to_s8(long val
)
261 return (val
- 500) / 1000;
262 return (val
+ 500) / 1000;
265 static u8
temp_to_u8(long val
)
271 return (val
+ 500) / 1000;
274 static s16
temp_to_s16(long val
)
281 return (val
- 62) / 125 * 32;
282 return (val
+ 62) / 125 * 32;
285 static u8
hyst_to_reg(long val
)
291 return (val
+ 500) / 1000;
295 * ADT7461 in compatibility mode is almost identical to LM90 except that
296 * attempts to write values that are outside the range 0 < temp < 127 are
297 * treated as the boundary value.
299 * ADT7461 in "extended mode" operation uses unsigned integers offset by
300 * 64 (e.g., 0 -> -64 degC). The range is restricted to -64..191 degC.
302 static inline int temp_from_u8_adt7461(struct lm90_data
*data
, u8 val
)
304 if (data
->flags
& LM90_FLAG_ADT7461_EXT
)
305 return (val
- 64) * 1000;
307 return temp_from_s8(val
);
310 static inline int temp_from_u16_adt7461(struct lm90_data
*data
, u16 val
)
312 if (data
->flags
& LM90_FLAG_ADT7461_EXT
)
313 return (val
- 0x4000) / 64 * 250;
315 return temp_from_s16(val
);
318 static u8
temp_to_u8_adt7461(struct lm90_data
*data
, long val
)
320 if (data
->flags
& LM90_FLAG_ADT7461_EXT
) {
325 return (val
+ 500 + 64000) / 1000;
331 return (val
+ 500) / 1000;
335 static u16
temp_to_u16_adt7461(struct lm90_data
*data
, long val
)
337 if (data
->flags
& LM90_FLAG_ADT7461_EXT
) {
342 return (val
+ 64000 + 125) / 250 * 64;
348 return (val
+ 125) / 250 * 64;
356 static ssize_t
show_temp8(struct device
*dev
, struct device_attribute
*devattr
,
359 struct sensor_device_attribute
*attr
= to_sensor_dev_attr(devattr
);
360 struct lm90_data
*data
= lm90_update_device(dev
);
363 if (data
->kind
== adt7461
)
364 temp
= temp_from_u8_adt7461(data
, data
->temp8
[attr
->index
]);
365 else if (data
->kind
== max6646
)
366 temp
= temp_from_u8(data
->temp8
[attr
->index
]);
368 temp
= temp_from_s8(data
->temp8
[attr
->index
]);
370 /* +16 degrees offset for temp2 for the LM99 */
371 if (data
->kind
== lm99
&& attr
->index
== 3)
374 return sprintf(buf
, "%d\n", temp
);
377 static ssize_t
set_temp8(struct device
*dev
, struct device_attribute
*devattr
,
378 const char *buf
, size_t count
)
380 static const u8 reg
[4] = {
381 LM90_REG_W_LOCAL_LOW
,
382 LM90_REG_W_LOCAL_HIGH
,
383 LM90_REG_W_LOCAL_CRIT
,
384 LM90_REG_W_REMOTE_CRIT
,
387 struct sensor_device_attribute
*attr
= to_sensor_dev_attr(devattr
);
388 struct i2c_client
*client
= to_i2c_client(dev
);
389 struct lm90_data
*data
= i2c_get_clientdata(client
);
390 int nr
= attr
->index
;
394 err
= strict_strtol(buf
, 10, &val
);
398 /* +16 degrees offset for temp2 for the LM99 */
399 if (data
->kind
== lm99
&& attr
->index
== 3)
402 mutex_lock(&data
->update_lock
);
403 if (data
->kind
== adt7461
)
404 data
->temp8
[nr
] = temp_to_u8_adt7461(data
, val
);
405 else if (data
->kind
== max6646
)
406 data
->temp8
[nr
] = temp_to_u8(val
);
408 data
->temp8
[nr
] = temp_to_s8(val
);
409 i2c_smbus_write_byte_data(client
, reg
[nr
], data
->temp8
[nr
]);
410 mutex_unlock(&data
->update_lock
);
414 static ssize_t
show_temp11(struct device
*dev
, struct device_attribute
*devattr
,
417 struct sensor_device_attribute
*attr
= to_sensor_dev_attr(devattr
);
418 struct lm90_data
*data
= lm90_update_device(dev
);
421 if (data
->kind
== adt7461
)
422 temp
= temp_from_u16_adt7461(data
, data
->temp11
[attr
->index
]);
423 else if (data
->kind
== max6646
)
424 temp
= temp_from_u16(data
->temp11
[attr
->index
]);
426 temp
= temp_from_s16(data
->temp11
[attr
->index
]);
428 /* +16 degrees offset for temp2 for the LM99 */
429 if (data
->kind
== lm99
&& attr
->index
<= 2)
432 return sprintf(buf
, "%d\n", temp
);
435 static ssize_t
set_temp11(struct device
*dev
, struct device_attribute
*devattr
,
436 const char *buf
, size_t count
)
438 static const u8 reg
[6] = {
439 LM90_REG_W_REMOTE_LOWH
,
440 LM90_REG_W_REMOTE_LOWL
,
441 LM90_REG_W_REMOTE_HIGHH
,
442 LM90_REG_W_REMOTE_HIGHL
,
443 LM90_REG_W_REMOTE_OFFSH
,
444 LM90_REG_W_REMOTE_OFFSL
,
447 struct sensor_device_attribute
*attr
= to_sensor_dev_attr(devattr
);
448 struct i2c_client
*client
= to_i2c_client(dev
);
449 struct lm90_data
*data
= i2c_get_clientdata(client
);
450 int nr
= attr
->index
;
454 err
= strict_strtol(buf
, 10, &val
);
458 /* +16 degrees offset for temp2 for the LM99 */
459 if (data
->kind
== lm99
&& attr
->index
<= 2)
462 mutex_lock(&data
->update_lock
);
463 if (data
->kind
== adt7461
)
464 data
->temp11
[nr
] = temp_to_u16_adt7461(data
, val
);
465 else if (data
->kind
== max6657
|| data
->kind
== max6680
)
466 data
->temp11
[nr
] = temp_to_s8(val
) << 8;
467 else if (data
->kind
== max6646
)
468 data
->temp11
[nr
] = temp_to_u8(val
) << 8;
470 data
->temp11
[nr
] = temp_to_s16(val
);
472 i2c_smbus_write_byte_data(client
, reg
[(nr
- 1) * 2],
473 data
->temp11
[nr
] >> 8);
474 if (data
->kind
!= max6657
&& data
->kind
!= max6680
475 && data
->kind
!= max6646
)
476 i2c_smbus_write_byte_data(client
, reg
[(nr
- 1) * 2 + 1],
477 data
->temp11
[nr
] & 0xff);
478 mutex_unlock(&data
->update_lock
);
482 static ssize_t
show_temphyst(struct device
*dev
,
483 struct device_attribute
*devattr
,
486 struct sensor_device_attribute
*attr
= to_sensor_dev_attr(devattr
);
487 struct lm90_data
*data
= lm90_update_device(dev
);
490 if (data
->kind
== adt7461
)
491 temp
= temp_from_u8_adt7461(data
, data
->temp8
[attr
->index
]);
492 else if (data
->kind
== max6646
)
493 temp
= temp_from_u8(data
->temp8
[attr
->index
]);
495 temp
= temp_from_s8(data
->temp8
[attr
->index
]);
497 /* +16 degrees offset for temp2 for the LM99 */
498 if (data
->kind
== lm99
&& attr
->index
== 3)
501 return sprintf(buf
, "%d\n", temp
- temp_from_s8(data
->temp_hyst
));
504 static ssize_t
set_temphyst(struct device
*dev
, struct device_attribute
*dummy
,
505 const char *buf
, size_t count
)
507 struct i2c_client
*client
= to_i2c_client(dev
);
508 struct lm90_data
*data
= i2c_get_clientdata(client
);
513 err
= strict_strtol(buf
, 10, &val
);
517 mutex_lock(&data
->update_lock
);
518 if (data
->kind
== adt7461
)
519 temp
= temp_from_u8_adt7461(data
, data
->temp8
[2]);
520 else if (data
->kind
== max6646
)
521 temp
= temp_from_u8(data
->temp8
[2]);
523 temp
= temp_from_s8(data
->temp8
[2]);
525 data
->temp_hyst
= hyst_to_reg(temp
- val
);
526 i2c_smbus_write_byte_data(client
, LM90_REG_W_TCRIT_HYST
,
528 mutex_unlock(&data
->update_lock
);
532 static ssize_t
show_alarms(struct device
*dev
, struct device_attribute
*dummy
,
535 struct lm90_data
*data
= lm90_update_device(dev
);
536 return sprintf(buf
, "%d\n", data
->alarms
);
539 static ssize_t
show_alarm(struct device
*dev
, struct device_attribute
542 struct sensor_device_attribute
*attr
= to_sensor_dev_attr(devattr
);
543 struct lm90_data
*data
= lm90_update_device(dev
);
544 int bitnr
= attr
->index
;
546 return sprintf(buf
, "%d\n", (data
->alarms
>> bitnr
) & 1);
549 static SENSOR_DEVICE_ATTR(temp1_input
, S_IRUGO
, show_temp11
, NULL
, 4);
550 static SENSOR_DEVICE_ATTR(temp2_input
, S_IRUGO
, show_temp11
, NULL
, 0);
551 static SENSOR_DEVICE_ATTR(temp1_min
, S_IWUSR
| S_IRUGO
, show_temp8
,
553 static SENSOR_DEVICE_ATTR(temp2_min
, S_IWUSR
| S_IRUGO
, show_temp11
,
555 static SENSOR_DEVICE_ATTR(temp1_max
, S_IWUSR
| S_IRUGO
, show_temp8
,
557 static SENSOR_DEVICE_ATTR(temp2_max
, S_IWUSR
| S_IRUGO
, show_temp11
,
559 static SENSOR_DEVICE_ATTR(temp1_crit
, S_IWUSR
| S_IRUGO
, show_temp8
,
561 static SENSOR_DEVICE_ATTR(temp2_crit
, S_IWUSR
| S_IRUGO
, show_temp8
,
563 static SENSOR_DEVICE_ATTR(temp1_crit_hyst
, S_IWUSR
| S_IRUGO
, show_temphyst
,
565 static SENSOR_DEVICE_ATTR(temp2_crit_hyst
, S_IRUGO
, show_temphyst
, NULL
, 3);
566 static SENSOR_DEVICE_ATTR(temp2_offset
, S_IWUSR
| S_IRUGO
, show_temp11
,
569 /* Individual alarm files */
570 static SENSOR_DEVICE_ATTR(temp1_crit_alarm
, S_IRUGO
, show_alarm
, NULL
, 0);
571 static SENSOR_DEVICE_ATTR(temp2_crit_alarm
, S_IRUGO
, show_alarm
, NULL
, 1);
572 static SENSOR_DEVICE_ATTR(temp2_fault
, S_IRUGO
, show_alarm
, NULL
, 2);
573 static SENSOR_DEVICE_ATTR(temp2_min_alarm
, S_IRUGO
, show_alarm
, NULL
, 3);
574 static SENSOR_DEVICE_ATTR(temp2_max_alarm
, S_IRUGO
, show_alarm
, NULL
, 4);
575 static SENSOR_DEVICE_ATTR(temp1_min_alarm
, S_IRUGO
, show_alarm
, NULL
, 5);
576 static SENSOR_DEVICE_ATTR(temp1_max_alarm
, S_IRUGO
, show_alarm
, NULL
, 6);
577 /* Raw alarm file for compatibility */
578 static DEVICE_ATTR(alarms
, S_IRUGO
, show_alarms
, NULL
);
580 static struct attribute
*lm90_attributes
[] = {
581 &sensor_dev_attr_temp1_input
.dev_attr
.attr
,
582 &sensor_dev_attr_temp2_input
.dev_attr
.attr
,
583 &sensor_dev_attr_temp1_min
.dev_attr
.attr
,
584 &sensor_dev_attr_temp2_min
.dev_attr
.attr
,
585 &sensor_dev_attr_temp1_max
.dev_attr
.attr
,
586 &sensor_dev_attr_temp2_max
.dev_attr
.attr
,
587 &sensor_dev_attr_temp1_crit
.dev_attr
.attr
,
588 &sensor_dev_attr_temp2_crit
.dev_attr
.attr
,
589 &sensor_dev_attr_temp1_crit_hyst
.dev_attr
.attr
,
590 &sensor_dev_attr_temp2_crit_hyst
.dev_attr
.attr
,
592 &sensor_dev_attr_temp1_crit_alarm
.dev_attr
.attr
,
593 &sensor_dev_attr_temp2_crit_alarm
.dev_attr
.attr
,
594 &sensor_dev_attr_temp2_fault
.dev_attr
.attr
,
595 &sensor_dev_attr_temp2_min_alarm
.dev_attr
.attr
,
596 &sensor_dev_attr_temp2_max_alarm
.dev_attr
.attr
,
597 &sensor_dev_attr_temp1_min_alarm
.dev_attr
.attr
,
598 &sensor_dev_attr_temp1_max_alarm
.dev_attr
.attr
,
599 &dev_attr_alarms
.attr
,
603 static const struct attribute_group lm90_group
= {
604 .attrs
= lm90_attributes
,
607 /* pec used for ADM1032 only */
608 static ssize_t
show_pec(struct device
*dev
, struct device_attribute
*dummy
,
611 struct i2c_client
*client
= to_i2c_client(dev
);
612 return sprintf(buf
, "%d\n", !!(client
->flags
& I2C_CLIENT_PEC
));
615 static ssize_t
set_pec(struct device
*dev
, struct device_attribute
*dummy
,
616 const char *buf
, size_t count
)
618 struct i2c_client
*client
= to_i2c_client(dev
);
622 err
= strict_strtol(buf
, 10, &val
);
628 client
->flags
&= ~I2C_CLIENT_PEC
;
631 client
->flags
|= I2C_CLIENT_PEC
;
640 static DEVICE_ATTR(pec
, S_IWUSR
| S_IRUGO
, show_pec
, set_pec
);
647 * The ADM1032 supports PEC but not on write byte transactions, so we need
648 * to explicitly ask for a transaction without PEC.
650 static inline s32
adm1032_write_byte(struct i2c_client
*client
, u8 value
)
652 return i2c_smbus_xfer(client
->adapter
, client
->addr
,
653 client
->flags
& ~I2C_CLIENT_PEC
,
654 I2C_SMBUS_WRITE
, value
, I2C_SMBUS_BYTE
, NULL
);
658 * It is assumed that client->update_lock is held (unless we are in
659 * detection or initialization steps). This matters when PEC is enabled,
660 * because we don't want the address pointer to change between the write
661 * byte and the read byte transactions.
663 static int lm90_read_reg(struct i2c_client
*client
, u8 reg
, u8
*value
)
667 if (client
->flags
& I2C_CLIENT_PEC
) {
668 err
= adm1032_write_byte(client
, reg
);
670 err
= i2c_smbus_read_byte(client
);
672 err
= i2c_smbus_read_byte_data(client
, reg
);
675 dev_warn(&client
->dev
, "Register %#02x read failed (%d)\n",
684 /* Return 0 if detection is successful, -ENODEV otherwise */
685 static int lm90_detect(struct i2c_client
*new_client
,
686 struct i2c_board_info
*info
)
688 struct i2c_adapter
*adapter
= new_client
->adapter
;
689 int address
= new_client
->addr
;
690 const char *name
= NULL
;
691 int man_id
, chip_id
, reg_config1
, reg_convrate
;
693 if (!i2c_check_functionality(adapter
, I2C_FUNC_SMBUS_BYTE_DATA
))
696 /* detection and identification */
697 if ((man_id
= i2c_smbus_read_byte_data(new_client
,
698 LM90_REG_R_MAN_ID
)) < 0
699 || (chip_id
= i2c_smbus_read_byte_data(new_client
,
700 LM90_REG_R_CHIP_ID
)) < 0
701 || (reg_config1
= i2c_smbus_read_byte_data(new_client
,
702 LM90_REG_R_CONFIG1
)) < 0
703 || (reg_convrate
= i2c_smbus_read_byte_data(new_client
,
704 LM90_REG_R_CONVRATE
)) < 0)
707 if ((address
== 0x4C || address
== 0x4D)
708 && man_id
== 0x01) { /* National Semiconductor */
711 reg_config2
= i2c_smbus_read_byte_data(new_client
,
716 if ((reg_config1
& 0x2A) == 0x00
717 && (reg_config2
& 0xF8) == 0x00
718 && reg_convrate
<= 0x09) {
720 && (chip_id
& 0xF0) == 0x20) { /* LM90 */
723 if ((chip_id
& 0xF0) == 0x30) { /* LM89/LM99 */
725 dev_info(&adapter
->dev
,
726 "Assuming LM99 chip at 0x%02x\n",
728 dev_info(&adapter
->dev
,
729 "If it is an LM89, instantiate it "
730 "with the new_device sysfs "
734 && (chip_id
& 0xF0) == 0x10) { /* LM86 */
739 if ((address
== 0x4C || address
== 0x4D)
740 && man_id
== 0x41) { /* Analog Devices */
741 if ((chip_id
& 0xF0) == 0x40 /* ADM1032 */
742 && (reg_config1
& 0x3F) == 0x00
743 && reg_convrate
<= 0x0A) {
745 /* The ADM1032 supports PEC, but only if combined
746 transactions are not used. */
747 if (i2c_check_functionality(adapter
,
748 I2C_FUNC_SMBUS_BYTE
))
749 info
->flags
|= I2C_CLIENT_PEC
;
751 if (chip_id
== 0x51 /* ADT7461 */
752 && (reg_config1
& 0x1B) == 0x00
753 && reg_convrate
<= 0x0A) {
757 if (man_id
== 0x4D) { /* Maxim */
759 * The MAX6657, MAX6658 and MAX6659 do NOT have a chip_id
760 * register. Reading from that address will return the last
761 * read value, which in our case is those of the man_id
762 * register. Likewise, the config1 register seems to lack a
763 * low nibble, so the value will be those of the previous
764 * read, so in our case those of the man_id register.
766 if (chip_id
== man_id
767 && (address
== 0x4C || address
== 0x4D)
768 && (reg_config1
& 0x1F) == (man_id
& 0x0F)
769 && reg_convrate
<= 0x09) {
773 * The chip_id register of the MAX6680 and MAX6681 holds the
774 * revision of the chip. The lowest bit of the config1 register
775 * is unused and should return zero when read, so should the
776 * second to last bit of config1 (software reset).
779 && (reg_config1
& 0x03) == 0x00
780 && reg_convrate
<= 0x07) {
784 * The chip_id register of the MAX6646/6647/6649 holds the
785 * revision of the chip. The lowest 6 bits of the config1
786 * register are unused and should return zero when read.
789 && (reg_config1
& 0x3f) == 0x00
790 && reg_convrate
<= 0x07) {
795 && man_id
== 0x5C) { /* Winbond/Nuvoton */
796 if ((chip_id
& 0xFE) == 0x10 /* W83L771AWG/ASG */
797 && (reg_config1
& 0x2A) == 0x00
798 && reg_convrate
<= 0x08) {
803 if (!name
) { /* identification failed */
804 dev_dbg(&adapter
->dev
,
805 "Unsupported chip at 0x%02x (man_id=0x%02X, "
806 "chip_id=0x%02X)\n", address
, man_id
, chip_id
);
810 strlcpy(info
->type
, name
, I2C_NAME_SIZE
);
815 static int lm90_probe(struct i2c_client
*new_client
,
816 const struct i2c_device_id
*id
)
818 struct i2c_adapter
*adapter
= to_i2c_adapter(new_client
->dev
.parent
);
819 struct lm90_data
*data
;
822 data
= kzalloc(sizeof(struct lm90_data
), GFP_KERNEL
);
827 i2c_set_clientdata(new_client
, data
);
828 mutex_init(&data
->update_lock
);
830 /* Set the device type */
831 data
->kind
= id
->driver_data
;
832 if (data
->kind
== adm1032
) {
833 if (!i2c_check_functionality(adapter
, I2C_FUNC_SMBUS_BYTE
))
834 new_client
->flags
&= ~I2C_CLIENT_PEC
;
837 /* Different devices have different alarm bits triggering the
839 switch (data
->kind
) {
843 data
->alert_alarms
= 0x7b;
846 data
->alert_alarms
= 0x7c;
850 /* Initialize the LM90 chip */
851 lm90_init_client(new_client
);
853 /* Register sysfs hooks */
854 err
= sysfs_create_group(&new_client
->dev
.kobj
, &lm90_group
);
857 if (new_client
->flags
& I2C_CLIENT_PEC
) {
858 err
= device_create_file(&new_client
->dev
, &dev_attr_pec
);
860 goto exit_remove_files
;
862 if (data
->kind
!= max6657
&& data
->kind
!= max6646
) {
863 err
= device_create_file(&new_client
->dev
,
864 &sensor_dev_attr_temp2_offset
.dev_attr
);
866 goto exit_remove_files
;
869 data
->hwmon_dev
= hwmon_device_register(&new_client
->dev
);
870 if (IS_ERR(data
->hwmon_dev
)) {
871 err
= PTR_ERR(data
->hwmon_dev
);
872 goto exit_remove_files
;
878 sysfs_remove_group(&new_client
->dev
.kobj
, &lm90_group
);
879 device_remove_file(&new_client
->dev
, &dev_attr_pec
);
886 static void lm90_init_client(struct i2c_client
*client
)
889 struct lm90_data
*data
= i2c_get_clientdata(client
);
892 * Start the conversions.
894 i2c_smbus_write_byte_data(client
, LM90_REG_W_CONVRATE
,
896 if (lm90_read_reg(client
, LM90_REG_R_CONFIG1
, &config
) < 0) {
897 dev_warn(&client
->dev
, "Initialization failed!\n");
900 data
->config_orig
= config
;
902 /* Check Temperature Range Select */
903 if (data
->kind
== adt7461
) {
905 data
->flags
|= LM90_FLAG_ADT7461_EXT
;
909 * Put MAX6680/MAX8881 into extended resolution (bit 0x10,
910 * 0.125 degree resolution) and range (0x08, extend range
911 * to -64 degree) mode for the remote temperature sensor.
913 if (data
->kind
== max6680
)
916 config
&= 0xBF; /* run */
917 if (config
!= data
->config_orig
) /* Only write if changed */
918 i2c_smbus_write_byte_data(client
, LM90_REG_W_CONFIG1
, config
);
921 static int lm90_remove(struct i2c_client
*client
)
923 struct lm90_data
*data
= i2c_get_clientdata(client
);
925 hwmon_device_unregister(data
->hwmon_dev
);
926 sysfs_remove_group(&client
->dev
.kobj
, &lm90_group
);
927 device_remove_file(&client
->dev
, &dev_attr_pec
);
928 if (data
->kind
!= max6657
&& data
->kind
!= max6646
)
929 device_remove_file(&client
->dev
,
930 &sensor_dev_attr_temp2_offset
.dev_attr
);
932 /* Restore initial configuration */
933 i2c_smbus_write_byte_data(client
, LM90_REG_W_CONFIG1
,
940 static void lm90_alert(struct i2c_client
*client
, unsigned int flag
)
942 struct lm90_data
*data
= i2c_get_clientdata(client
);
945 lm90_read_reg(client
, LM90_REG_R_STATUS
, &alarms
);
946 if ((alarms
& 0x7f) == 0) {
947 dev_info(&client
->dev
, "Everything OK\n");
950 dev_warn(&client
->dev
,
951 "temp%d out of range, please check!\n", 1);
953 dev_warn(&client
->dev
,
954 "temp%d out of range, please check!\n", 2);
956 dev_warn(&client
->dev
,
957 "temp%d diode open, please check!\n", 2);
959 /* Disable ALERT# output, because these chips don't implement
960 SMBus alert correctly; they should only hold the alert line
962 if ((data
->kind
== adm1032
|| data
->kind
== adt7461
)
963 && (alarms
& data
->alert_alarms
)) {
964 dev_dbg(&client
->dev
, "Disabling ALERT#\n");
965 lm90_read_reg(client
, LM90_REG_R_CONFIG1
, &config
);
966 i2c_smbus_write_byte_data(client
, LM90_REG_W_CONFIG1
,
972 static int lm90_read16(struct i2c_client
*client
, u8 regh
, u8 regl
, u16
*value
)
978 * There is a trick here. We have to read two registers to have the
979 * sensor temperature, but we have to beware a conversion could occur
980 * inbetween the readings. The datasheet says we should either use
981 * the one-shot conversion register, which we don't want to do
982 * (disables hardware monitoring) or monitor the busy bit, which is
983 * impossible (we can't read the values and monitor that bit at the
984 * exact same time). So the solution used here is to read the high
985 * byte once, then the low byte, then the high byte again. If the new
986 * high byte matches the old one, then we have a valid reading. Else
987 * we have to read the low byte again, and now we believe we have a
990 if ((err
= lm90_read_reg(client
, regh
, &oldh
))
991 || (err
= lm90_read_reg(client
, regl
, &l
))
992 || (err
= lm90_read_reg(client
, regh
, &newh
)))
995 err
= lm90_read_reg(client
, regl
, &l
);
999 *value
= (newh
<< 8) | l
;
1004 static struct lm90_data
*lm90_update_device(struct device
*dev
)
1006 struct i2c_client
*client
= to_i2c_client(dev
);
1007 struct lm90_data
*data
= i2c_get_clientdata(client
);
1009 mutex_lock(&data
->update_lock
);
1011 if (time_after(jiffies
, data
->last_updated
+ HZ
/ 2 + HZ
/ 10)
1015 dev_dbg(&client
->dev
, "Updating lm90 data.\n");
1016 lm90_read_reg(client
, LM90_REG_R_LOCAL_LOW
, &data
->temp8
[0]);
1017 lm90_read_reg(client
, LM90_REG_R_LOCAL_HIGH
, &data
->temp8
[1]);
1018 lm90_read_reg(client
, LM90_REG_R_LOCAL_CRIT
, &data
->temp8
[2]);
1019 lm90_read_reg(client
, LM90_REG_R_REMOTE_CRIT
, &data
->temp8
[3]);
1020 lm90_read_reg(client
, LM90_REG_R_TCRIT_HYST
, &data
->temp_hyst
);
1022 if (data
->kind
== max6657
|| data
->kind
== max6646
) {
1023 lm90_read16(client
, LM90_REG_R_LOCAL_TEMP
,
1024 MAX6657_REG_R_LOCAL_TEMPL
,
1027 if (lm90_read_reg(client
, LM90_REG_R_LOCAL_TEMP
,
1029 data
->temp11
[4] = h
<< 8;
1031 lm90_read16(client
, LM90_REG_R_REMOTE_TEMPH
,
1032 LM90_REG_R_REMOTE_TEMPL
, &data
->temp11
[0]);
1034 if (lm90_read_reg(client
, LM90_REG_R_REMOTE_LOWH
, &h
) == 0) {
1035 data
->temp11
[1] = h
<< 8;
1036 if (data
->kind
!= max6657
&& data
->kind
!= max6680
1037 && data
->kind
!= max6646
1038 && lm90_read_reg(client
, LM90_REG_R_REMOTE_LOWL
,
1040 data
->temp11
[1] |= l
;
1042 if (lm90_read_reg(client
, LM90_REG_R_REMOTE_HIGHH
, &h
) == 0) {
1043 data
->temp11
[2] = h
<< 8;
1044 if (data
->kind
!= max6657
&& data
->kind
!= max6680
1045 && data
->kind
!= max6646
1046 && lm90_read_reg(client
, LM90_REG_R_REMOTE_HIGHL
,
1048 data
->temp11
[2] |= l
;
1051 if (data
->kind
!= max6657
&& data
->kind
!= max6646
) {
1052 if (lm90_read_reg(client
, LM90_REG_R_REMOTE_OFFSH
,
1054 && lm90_read_reg(client
, LM90_REG_R_REMOTE_OFFSL
,
1056 data
->temp11
[3] = (h
<< 8) | l
;
1058 lm90_read_reg(client
, LM90_REG_R_STATUS
, &data
->alarms
);
1060 /* Re-enable ALERT# output if it was originally enabled and
1061 * relevant alarms are all clear */
1062 if ((data
->config_orig
& 0x80) == 0
1063 && (data
->alarms
& data
->alert_alarms
) == 0) {
1066 lm90_read_reg(client
, LM90_REG_R_CONFIG1
, &config
);
1067 if (config
& 0x80) {
1068 dev_dbg(&client
->dev
, "Re-enabling ALERT#\n");
1069 i2c_smbus_write_byte_data(client
,
1075 data
->last_updated
= jiffies
;
1079 mutex_unlock(&data
->update_lock
);
1084 static int __init
sensors_lm90_init(void)
1086 return i2c_add_driver(&lm90_driver
);
1089 static void __exit
sensors_lm90_exit(void)
1091 i2c_del_driver(&lm90_driver
);
1094 MODULE_AUTHOR("Jean Delvare <khali@linux-fr.org>");
1095 MODULE_DESCRIPTION("LM90/ADM1032 driver");
1096 MODULE_LICENSE("GPL");
1098 module_init(sensors_lm90_init
);
1099 module_exit(sensors_lm90_exit
);