2 * lm90.c - Part of lm_sensors, Linux kernel modules for hardware
4 * Copyright (C) 2003-2009 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
};
100 I2C_CLIENT_INSMOD_8(lm90
, adm1032
, lm99
, lm86
, max6657
, adt7461
, max6680
,
107 #define LM90_REG_R_MAN_ID 0xFE
108 #define LM90_REG_R_CHIP_ID 0xFF
109 #define LM90_REG_R_CONFIG1 0x03
110 #define LM90_REG_W_CONFIG1 0x09
111 #define LM90_REG_R_CONFIG2 0xBF
112 #define LM90_REG_W_CONFIG2 0xBF
113 #define LM90_REG_R_CONVRATE 0x04
114 #define LM90_REG_W_CONVRATE 0x0A
115 #define LM90_REG_R_STATUS 0x02
116 #define LM90_REG_R_LOCAL_TEMP 0x00
117 #define LM90_REG_R_LOCAL_HIGH 0x05
118 #define LM90_REG_W_LOCAL_HIGH 0x0B
119 #define LM90_REG_R_LOCAL_LOW 0x06
120 #define LM90_REG_W_LOCAL_LOW 0x0C
121 #define LM90_REG_R_LOCAL_CRIT 0x20
122 #define LM90_REG_W_LOCAL_CRIT 0x20
123 #define LM90_REG_R_REMOTE_TEMPH 0x01
124 #define LM90_REG_R_REMOTE_TEMPL 0x10
125 #define LM90_REG_R_REMOTE_OFFSH 0x11
126 #define LM90_REG_W_REMOTE_OFFSH 0x11
127 #define LM90_REG_R_REMOTE_OFFSL 0x12
128 #define LM90_REG_W_REMOTE_OFFSL 0x12
129 #define LM90_REG_R_REMOTE_HIGHH 0x07
130 #define LM90_REG_W_REMOTE_HIGHH 0x0D
131 #define LM90_REG_R_REMOTE_HIGHL 0x13
132 #define LM90_REG_W_REMOTE_HIGHL 0x13
133 #define LM90_REG_R_REMOTE_LOWH 0x08
134 #define LM90_REG_W_REMOTE_LOWH 0x0E
135 #define LM90_REG_R_REMOTE_LOWL 0x14
136 #define LM90_REG_W_REMOTE_LOWL 0x14
137 #define LM90_REG_R_REMOTE_CRIT 0x19
138 #define LM90_REG_W_REMOTE_CRIT 0x19
139 #define LM90_REG_R_TCRIT_HYST 0x21
140 #define LM90_REG_W_TCRIT_HYST 0x21
142 /* MAX6646/6647/6649/6657/6658/6659 registers */
144 #define MAX6657_REG_R_LOCAL_TEMPL 0x11
149 #define LM90_FLAG_ADT7461_EXT 0x01 /* ADT7461 extended mode */
152 * Functions declaration
155 static int lm90_detect(struct i2c_client
*client
, int kind
,
156 struct i2c_board_info
*info
);
157 static int lm90_probe(struct i2c_client
*client
,
158 const struct i2c_device_id
*id
);
159 static void lm90_init_client(struct i2c_client
*client
);
160 static int lm90_remove(struct i2c_client
*client
);
161 static struct lm90_data
*lm90_update_device(struct device
*dev
);
164 * Driver data (common to all clients)
167 static const struct i2c_device_id lm90_id
[] = {
168 { "adm1032", adm1032
},
169 { "adt7461", adt7461
},
174 { "max6646", max6646
},
175 { "max6647", max6646
},
176 { "max6649", max6646
},
177 { "max6657", max6657
},
178 { "max6658", max6657
},
179 { "max6659", max6657
},
180 { "max6680", max6680
},
181 { "max6681", max6680
},
184 MODULE_DEVICE_TABLE(i2c
, lm90_id
);
186 static struct i2c_driver lm90_driver
= {
187 .class = I2C_CLASS_HWMON
,
192 .remove
= lm90_remove
,
194 .detect
= lm90_detect
,
195 .address_data
= &addr_data
,
199 * Client data (each client gets its own)
203 struct device
*hwmon_dev
;
204 struct mutex update_lock
;
205 char valid
; /* zero until following fields are valid */
206 unsigned long last_updated
; /* in jiffies */
210 /* registers values */
211 s8 temp8
[4]; /* 0: local low limit
213 2: local critical limit
214 3: remote critical limit */
215 s16 temp11
[5]; /* 0: remote input
218 3: remote offset (except max6646 and max6657)
221 u8 alarms
; /* bitvector */
226 * For local temperatures and limits, critical limits and the hysteresis
227 * value, the LM90 uses signed 8-bit values with LSB = 1 degree Celsius.
228 * For remote temperatures and limits, it uses signed 11-bit values with
229 * LSB = 0.125 degree Celsius, left-justified in 16-bit registers. Some
230 * Maxim chips use unsigned values.
233 static inline int temp_from_s8(s8 val
)
238 static inline int temp_from_u8(u8 val
)
243 static inline int temp_from_s16(s16 val
)
245 return val
/ 32 * 125;
248 static inline int temp_from_u16(u16 val
)
250 return val
/ 32 * 125;
253 static s8
temp_to_s8(long val
)
260 return (val
- 500) / 1000;
261 return (val
+ 500) / 1000;
264 static u8
temp_to_u8(long val
)
270 return (val
+ 500) / 1000;
273 static s16
temp_to_s16(long val
)
280 return (val
- 62) / 125 * 32;
281 return (val
+ 62) / 125 * 32;
284 static u8
hyst_to_reg(long val
)
290 return (val
+ 500) / 1000;
294 * ADT7461 in compatibility mode is almost identical to LM90 except that
295 * attempts to write values that are outside the range 0 < temp < 127 are
296 * treated as the boundary value.
298 * ADT7461 in "extended mode" operation uses unsigned integers offset by
299 * 64 (e.g., 0 -> -64 degC). The range is restricted to -64..191 degC.
301 static inline int temp_from_u8_adt7461(struct lm90_data
*data
, u8 val
)
303 if (data
->flags
& LM90_FLAG_ADT7461_EXT
)
304 return (val
- 64) * 1000;
306 return temp_from_s8(val
);
309 static inline int temp_from_u16_adt7461(struct lm90_data
*data
, u16 val
)
311 if (data
->flags
& LM90_FLAG_ADT7461_EXT
)
312 return (val
- 0x4000) / 64 * 250;
314 return temp_from_s16(val
);
317 static u8
temp_to_u8_adt7461(struct lm90_data
*data
, long val
)
319 if (data
->flags
& LM90_FLAG_ADT7461_EXT
) {
324 return (val
+ 500 + 64000) / 1000;
330 return (val
+ 500) / 1000;
334 static u16
temp_to_u16_adt7461(struct lm90_data
*data
, long val
)
336 if (data
->flags
& LM90_FLAG_ADT7461_EXT
) {
341 return (val
+ 64000 + 125) / 250 * 64;
347 return (val
+ 125) / 250 * 64;
355 static ssize_t
show_temp8(struct device
*dev
, struct device_attribute
*devattr
,
358 struct sensor_device_attribute
*attr
= to_sensor_dev_attr(devattr
);
359 struct lm90_data
*data
= lm90_update_device(dev
);
362 if (data
->kind
== adt7461
)
363 temp
= temp_from_u8_adt7461(data
, data
->temp8
[attr
->index
]);
364 else if (data
->kind
== max6646
)
365 temp
= temp_from_u8(data
->temp8
[attr
->index
]);
367 temp
= temp_from_s8(data
->temp8
[attr
->index
]);
369 /* +16 degrees offset for temp2 for the LM99 */
370 if (data
->kind
== lm99
&& attr
->index
== 3)
373 return sprintf(buf
, "%d\n", temp
);
376 static ssize_t
set_temp8(struct device
*dev
, struct device_attribute
*devattr
,
377 const char *buf
, size_t count
)
379 static const u8 reg
[4] = {
380 LM90_REG_W_LOCAL_LOW
,
381 LM90_REG_W_LOCAL_HIGH
,
382 LM90_REG_W_LOCAL_CRIT
,
383 LM90_REG_W_REMOTE_CRIT
,
386 struct sensor_device_attribute
*attr
= to_sensor_dev_attr(devattr
);
387 struct i2c_client
*client
= to_i2c_client(dev
);
388 struct lm90_data
*data
= i2c_get_clientdata(client
);
389 long val
= simple_strtol(buf
, NULL
, 10);
390 int nr
= attr
->index
;
392 /* +16 degrees offset for temp2 for the LM99 */
393 if (data
->kind
== lm99
&& attr
->index
== 3)
396 mutex_lock(&data
->update_lock
);
397 if (data
->kind
== adt7461
)
398 data
->temp8
[nr
] = temp_to_u8_adt7461(data
, val
);
399 else if (data
->kind
== max6646
)
400 data
->temp8
[nr
] = temp_to_u8(val
);
402 data
->temp8
[nr
] = temp_to_s8(val
);
403 i2c_smbus_write_byte_data(client
, reg
[nr
], data
->temp8
[nr
]);
404 mutex_unlock(&data
->update_lock
);
408 static ssize_t
show_temp11(struct device
*dev
, struct device_attribute
*devattr
,
411 struct sensor_device_attribute
*attr
= to_sensor_dev_attr(devattr
);
412 struct lm90_data
*data
= lm90_update_device(dev
);
415 if (data
->kind
== adt7461
)
416 temp
= temp_from_u16_adt7461(data
, data
->temp11
[attr
->index
]);
417 else if (data
->kind
== max6646
)
418 temp
= temp_from_u16(data
->temp11
[attr
->index
]);
420 temp
= temp_from_s16(data
->temp11
[attr
->index
]);
422 /* +16 degrees offset for temp2 for the LM99 */
423 if (data
->kind
== lm99
&& attr
->index
<= 2)
426 return sprintf(buf
, "%d\n", temp
);
429 static ssize_t
set_temp11(struct device
*dev
, struct device_attribute
*devattr
,
430 const char *buf
, size_t count
)
432 static const u8 reg
[6] = {
433 LM90_REG_W_REMOTE_LOWH
,
434 LM90_REG_W_REMOTE_LOWL
,
435 LM90_REG_W_REMOTE_HIGHH
,
436 LM90_REG_W_REMOTE_HIGHL
,
437 LM90_REG_W_REMOTE_OFFSH
,
438 LM90_REG_W_REMOTE_OFFSL
,
441 struct sensor_device_attribute
*attr
= to_sensor_dev_attr(devattr
);
442 struct i2c_client
*client
= to_i2c_client(dev
);
443 struct lm90_data
*data
= i2c_get_clientdata(client
);
444 long val
= simple_strtol(buf
, NULL
, 10);
445 int nr
= attr
->index
;
447 /* +16 degrees offset for temp2 for the LM99 */
448 if (data
->kind
== lm99
&& attr
->index
<= 2)
451 mutex_lock(&data
->update_lock
);
452 if (data
->kind
== adt7461
)
453 data
->temp11
[nr
] = temp_to_u16_adt7461(data
, val
);
454 else if (data
->kind
== max6657
|| data
->kind
== max6680
)
455 data
->temp11
[nr
] = temp_to_s8(val
) << 8;
456 else if (data
->kind
== max6646
)
457 data
->temp11
[nr
] = temp_to_u8(val
) << 8;
459 data
->temp11
[nr
] = temp_to_s16(val
);
461 i2c_smbus_write_byte_data(client
, reg
[(nr
- 1) * 2],
462 data
->temp11
[nr
] >> 8);
463 if (data
->kind
!= max6657
&& data
->kind
!= max6680
464 && data
->kind
!= max6646
)
465 i2c_smbus_write_byte_data(client
, reg
[(nr
- 1) * 2 + 1],
466 data
->temp11
[nr
] & 0xff);
467 mutex_unlock(&data
->update_lock
);
471 static ssize_t
show_temphyst(struct device
*dev
, struct device_attribute
*devattr
,
474 struct sensor_device_attribute
*attr
= to_sensor_dev_attr(devattr
);
475 struct lm90_data
*data
= lm90_update_device(dev
);
478 if (data
->kind
== adt7461
)
479 temp
= temp_from_u8_adt7461(data
, data
->temp8
[attr
->index
]);
480 else if (data
->kind
== max6646
)
481 temp
= temp_from_u8(data
->temp8
[attr
->index
]);
483 temp
= temp_from_s8(data
->temp8
[attr
->index
]);
485 /* +16 degrees offset for temp2 for the LM99 */
486 if (data
->kind
== lm99
&& attr
->index
== 3)
489 return sprintf(buf
, "%d\n", temp
- temp_from_s8(data
->temp_hyst
));
492 static ssize_t
set_temphyst(struct device
*dev
, struct device_attribute
*dummy
,
493 const char *buf
, size_t count
)
495 struct i2c_client
*client
= to_i2c_client(dev
);
496 struct lm90_data
*data
= i2c_get_clientdata(client
);
497 long val
= simple_strtol(buf
, NULL
, 10);
500 mutex_lock(&data
->update_lock
);
501 if (data
->kind
== adt7461
)
502 temp
= temp_from_u8_adt7461(data
, data
->temp8
[2]);
503 else if (data
->kind
== max6646
)
504 temp
= temp_from_u8(data
->temp8
[2]);
506 temp
= temp_from_s8(data
->temp8
[2]);
508 data
->temp_hyst
= hyst_to_reg(temp
- val
);
509 i2c_smbus_write_byte_data(client
, LM90_REG_W_TCRIT_HYST
,
511 mutex_unlock(&data
->update_lock
);
515 static ssize_t
show_alarms(struct device
*dev
, struct device_attribute
*dummy
,
518 struct lm90_data
*data
= lm90_update_device(dev
);
519 return sprintf(buf
, "%d\n", data
->alarms
);
522 static ssize_t
show_alarm(struct device
*dev
, struct device_attribute
525 struct sensor_device_attribute
*attr
= to_sensor_dev_attr(devattr
);
526 struct lm90_data
*data
= lm90_update_device(dev
);
527 int bitnr
= attr
->index
;
529 return sprintf(buf
, "%d\n", (data
->alarms
>> bitnr
) & 1);
532 static SENSOR_DEVICE_ATTR(temp1_input
, S_IRUGO
, show_temp11
, NULL
, 4);
533 static SENSOR_DEVICE_ATTR(temp2_input
, S_IRUGO
, show_temp11
, NULL
, 0);
534 static SENSOR_DEVICE_ATTR(temp1_min
, S_IWUSR
| S_IRUGO
, show_temp8
,
536 static SENSOR_DEVICE_ATTR(temp2_min
, S_IWUSR
| S_IRUGO
, show_temp11
,
538 static SENSOR_DEVICE_ATTR(temp1_max
, S_IWUSR
| S_IRUGO
, show_temp8
,
540 static SENSOR_DEVICE_ATTR(temp2_max
, S_IWUSR
| S_IRUGO
, show_temp11
,
542 static SENSOR_DEVICE_ATTR(temp1_crit
, S_IWUSR
| S_IRUGO
, show_temp8
,
544 static SENSOR_DEVICE_ATTR(temp2_crit
, S_IWUSR
| S_IRUGO
, show_temp8
,
546 static SENSOR_DEVICE_ATTR(temp1_crit_hyst
, S_IWUSR
| S_IRUGO
, show_temphyst
,
548 static SENSOR_DEVICE_ATTR(temp2_crit_hyst
, S_IRUGO
, show_temphyst
, NULL
, 3);
549 static SENSOR_DEVICE_ATTR(temp2_offset
, S_IWUSR
| S_IRUGO
, show_temp11
,
552 /* Individual alarm files */
553 static SENSOR_DEVICE_ATTR(temp1_crit_alarm
, S_IRUGO
, show_alarm
, NULL
, 0);
554 static SENSOR_DEVICE_ATTR(temp2_crit_alarm
, S_IRUGO
, show_alarm
, NULL
, 1);
555 static SENSOR_DEVICE_ATTR(temp2_fault
, S_IRUGO
, show_alarm
, NULL
, 2);
556 static SENSOR_DEVICE_ATTR(temp2_min_alarm
, S_IRUGO
, show_alarm
, NULL
, 3);
557 static SENSOR_DEVICE_ATTR(temp2_max_alarm
, S_IRUGO
, show_alarm
, NULL
, 4);
558 static SENSOR_DEVICE_ATTR(temp1_min_alarm
, S_IRUGO
, show_alarm
, NULL
, 5);
559 static SENSOR_DEVICE_ATTR(temp1_max_alarm
, S_IRUGO
, show_alarm
, NULL
, 6);
560 /* Raw alarm file for compatibility */
561 static DEVICE_ATTR(alarms
, S_IRUGO
, show_alarms
, NULL
);
563 static struct attribute
*lm90_attributes
[] = {
564 &sensor_dev_attr_temp1_input
.dev_attr
.attr
,
565 &sensor_dev_attr_temp2_input
.dev_attr
.attr
,
566 &sensor_dev_attr_temp1_min
.dev_attr
.attr
,
567 &sensor_dev_attr_temp2_min
.dev_attr
.attr
,
568 &sensor_dev_attr_temp1_max
.dev_attr
.attr
,
569 &sensor_dev_attr_temp2_max
.dev_attr
.attr
,
570 &sensor_dev_attr_temp1_crit
.dev_attr
.attr
,
571 &sensor_dev_attr_temp2_crit
.dev_attr
.attr
,
572 &sensor_dev_attr_temp1_crit_hyst
.dev_attr
.attr
,
573 &sensor_dev_attr_temp2_crit_hyst
.dev_attr
.attr
,
575 &sensor_dev_attr_temp1_crit_alarm
.dev_attr
.attr
,
576 &sensor_dev_attr_temp2_crit_alarm
.dev_attr
.attr
,
577 &sensor_dev_attr_temp2_fault
.dev_attr
.attr
,
578 &sensor_dev_attr_temp2_min_alarm
.dev_attr
.attr
,
579 &sensor_dev_attr_temp2_max_alarm
.dev_attr
.attr
,
580 &sensor_dev_attr_temp1_min_alarm
.dev_attr
.attr
,
581 &sensor_dev_attr_temp1_max_alarm
.dev_attr
.attr
,
582 &dev_attr_alarms
.attr
,
586 static const struct attribute_group lm90_group
= {
587 .attrs
= lm90_attributes
,
590 /* pec used for ADM1032 only */
591 static ssize_t
show_pec(struct device
*dev
, struct device_attribute
*dummy
,
594 struct i2c_client
*client
= to_i2c_client(dev
);
595 return sprintf(buf
, "%d\n", !!(client
->flags
& I2C_CLIENT_PEC
));
598 static ssize_t
set_pec(struct device
*dev
, struct device_attribute
*dummy
,
599 const char *buf
, size_t count
)
601 struct i2c_client
*client
= to_i2c_client(dev
);
602 long val
= simple_strtol(buf
, NULL
, 10);
606 client
->flags
&= ~I2C_CLIENT_PEC
;
609 client
->flags
|= I2C_CLIENT_PEC
;
618 static DEVICE_ATTR(pec
, S_IWUSR
| S_IRUGO
, show_pec
, set_pec
);
624 /* The ADM1032 supports PEC but not on write byte transactions, so we need
625 to explicitly ask for a transaction without PEC. */
626 static inline s32
adm1032_write_byte(struct i2c_client
*client
, u8 value
)
628 return i2c_smbus_xfer(client
->adapter
, client
->addr
,
629 client
->flags
& ~I2C_CLIENT_PEC
,
630 I2C_SMBUS_WRITE
, value
, I2C_SMBUS_BYTE
, NULL
);
633 /* It is assumed that client->update_lock is held (unless we are in
634 detection or initialization steps). This matters when PEC is enabled,
635 because we don't want the address pointer to change between the write
636 byte and the read byte transactions. */
637 static int lm90_read_reg(struct i2c_client
* client
, u8 reg
, u8
*value
)
641 if (client
->flags
& I2C_CLIENT_PEC
) {
642 err
= adm1032_write_byte(client
, reg
);
644 err
= i2c_smbus_read_byte(client
);
646 err
= i2c_smbus_read_byte_data(client
, reg
);
649 dev_warn(&client
->dev
, "Register %#02x read failed (%d)\n",
658 /* Return 0 if detection is successful, -ENODEV otherwise */
659 static int lm90_detect(struct i2c_client
*new_client
, int kind
,
660 struct i2c_board_info
*info
)
662 struct i2c_adapter
*adapter
= new_client
->adapter
;
663 int address
= new_client
->addr
;
664 const char *name
= NULL
;
665 int man_id
, chip_id
, reg_config1
, reg_convrate
;
667 if (!i2c_check_functionality(adapter
, I2C_FUNC_SMBUS_BYTE_DATA
))
670 /* detection and identification */
671 if ((man_id
= i2c_smbus_read_byte_data(new_client
,
672 LM90_REG_R_MAN_ID
)) < 0
673 || (chip_id
= i2c_smbus_read_byte_data(new_client
,
674 LM90_REG_R_CHIP_ID
)) < 0
675 || (reg_config1
= i2c_smbus_read_byte_data(new_client
,
676 LM90_REG_R_CONFIG1
)) < 0
677 || (reg_convrate
= i2c_smbus_read_byte_data(new_client
,
678 LM90_REG_R_CONVRATE
)) < 0)
681 if ((address
== 0x4C || address
== 0x4D)
682 && man_id
== 0x01) { /* National Semiconductor */
685 reg_config2
= i2c_smbus_read_byte_data(new_client
,
690 if ((reg_config1
& 0x2A) == 0x00
691 && (reg_config2
& 0xF8) == 0x00
692 && reg_convrate
<= 0x09) {
694 && (chip_id
& 0xF0) == 0x20) { /* LM90 */
697 if ((chip_id
& 0xF0) == 0x30) { /* LM89/LM99 */
699 dev_info(&adapter
->dev
,
700 "Assuming LM99 chip at 0x%02x\n",
702 dev_info(&adapter
->dev
,
703 "If it is an LM89, instantiate it "
704 "with the new_device sysfs "
708 && (chip_id
& 0xF0) == 0x10) { /* LM86 */
713 if ((address
== 0x4C || address
== 0x4D)
714 && man_id
== 0x41) { /* Analog Devices */
715 if ((chip_id
& 0xF0) == 0x40 /* ADM1032 */
716 && (reg_config1
& 0x3F) == 0x00
717 && reg_convrate
<= 0x0A) {
719 /* The ADM1032 supports PEC, but only if combined
720 transactions are not used. */
721 if (i2c_check_functionality(adapter
,
722 I2C_FUNC_SMBUS_BYTE
))
723 info
->flags
|= I2C_CLIENT_PEC
;
725 if (chip_id
== 0x51 /* ADT7461 */
726 && (reg_config1
& 0x1B) == 0x00
727 && reg_convrate
<= 0x0A) {
731 if (man_id
== 0x4D) { /* Maxim */
733 * The MAX6657, MAX6658 and MAX6659 do NOT have a chip_id
734 * register. Reading from that address will return the last
735 * read value, which in our case is those of the man_id
736 * register. Likewise, the config1 register seems to lack a
737 * low nibble, so the value will be those of the previous
738 * read, so in our case those of the man_id register.
740 if (chip_id
== man_id
741 && (address
== 0x4C || address
== 0x4D)
742 && (reg_config1
& 0x1F) == (man_id
& 0x0F)
743 && reg_convrate
<= 0x09) {
747 * The chip_id register of the MAX6680 and MAX6681 holds the
748 * revision of the chip. The lowest bit of the config1 register
749 * is unused and should return zero when read, so should the
750 * second to last bit of config1 (software reset).
753 && (reg_config1
& 0x03) == 0x00
754 && reg_convrate
<= 0x07) {
758 * The chip_id register of the MAX6646/6647/6649 holds the
759 * revision of the chip. The lowest 6 bits of the config1
760 * register are unused and should return zero when read.
763 && (reg_config1
& 0x3f) == 0x00
764 && reg_convrate
<= 0x07) {
769 if (!name
) { /* identification failed */
770 dev_dbg(&adapter
->dev
,
771 "Unsupported chip at 0x%02x (man_id=0x%02X, "
772 "chip_id=0x%02X)\n", address
, man_id
, chip_id
);
776 strlcpy(info
->type
, name
, I2C_NAME_SIZE
);
781 static int lm90_probe(struct i2c_client
*new_client
,
782 const struct i2c_device_id
*id
)
784 struct i2c_adapter
*adapter
= to_i2c_adapter(new_client
->dev
.parent
);
785 struct lm90_data
*data
;
788 data
= kzalloc(sizeof(struct lm90_data
), GFP_KERNEL
);
793 i2c_set_clientdata(new_client
, data
);
794 mutex_init(&data
->update_lock
);
796 /* Set the device type */
797 data
->kind
= id
->driver_data
;
798 if (data
->kind
== adm1032
) {
799 if (!i2c_check_functionality(adapter
, I2C_FUNC_SMBUS_BYTE
))
800 new_client
->flags
&= ~I2C_CLIENT_PEC
;
803 /* Initialize the LM90 chip */
804 lm90_init_client(new_client
);
806 /* Register sysfs hooks */
807 if ((err
= sysfs_create_group(&new_client
->dev
.kobj
, &lm90_group
)))
809 if (new_client
->flags
& I2C_CLIENT_PEC
) {
810 if ((err
= device_create_file(&new_client
->dev
,
812 goto exit_remove_files
;
814 if (data
->kind
!= max6657
&& data
->kind
!= max6646
) {
815 if ((err
= device_create_file(&new_client
->dev
,
816 &sensor_dev_attr_temp2_offset
.dev_attr
)))
817 goto exit_remove_files
;
820 data
->hwmon_dev
= hwmon_device_register(&new_client
->dev
);
821 if (IS_ERR(data
->hwmon_dev
)) {
822 err
= PTR_ERR(data
->hwmon_dev
);
823 goto exit_remove_files
;
829 sysfs_remove_group(&new_client
->dev
.kobj
, &lm90_group
);
830 device_remove_file(&new_client
->dev
, &dev_attr_pec
);
837 static void lm90_init_client(struct i2c_client
*client
)
839 u8 config
, config_orig
;
840 struct lm90_data
*data
= i2c_get_clientdata(client
);
843 * Start the conversions.
845 i2c_smbus_write_byte_data(client
, LM90_REG_W_CONVRATE
,
847 if (lm90_read_reg(client
, LM90_REG_R_CONFIG1
, &config
) < 0) {
848 dev_warn(&client
->dev
, "Initialization failed!\n");
851 config_orig
= config
;
853 /* Check Temperature Range Select */
854 if (data
->kind
== adt7461
) {
856 data
->flags
|= LM90_FLAG_ADT7461_EXT
;
860 * Put MAX6680/MAX8881 into extended resolution (bit 0x10,
861 * 0.125 degree resolution) and range (0x08, extend range
862 * to -64 degree) mode for the remote temperature sensor.
864 if (data
->kind
== max6680
) {
868 config
&= 0xBF; /* run */
869 if (config
!= config_orig
) /* Only write if changed */
870 i2c_smbus_write_byte_data(client
, LM90_REG_W_CONFIG1
, config
);
873 static int lm90_remove(struct i2c_client
*client
)
875 struct lm90_data
*data
= i2c_get_clientdata(client
);
877 hwmon_device_unregister(data
->hwmon_dev
);
878 sysfs_remove_group(&client
->dev
.kobj
, &lm90_group
);
879 device_remove_file(&client
->dev
, &dev_attr_pec
);
880 if (data
->kind
!= max6657
&& data
->kind
!= max6646
)
881 device_remove_file(&client
->dev
,
882 &sensor_dev_attr_temp2_offset
.dev_attr
);
888 static int lm90_read16(struct i2c_client
*client
, u8 regh
, u8 regl
, u16
*value
)
894 * There is a trick here. We have to read two registers to have the
895 * sensor temperature, but we have to beware a conversion could occur
896 * inbetween the readings. The datasheet says we should either use
897 * the one-shot conversion register, which we don't want to do
898 * (disables hardware monitoring) or monitor the busy bit, which is
899 * impossible (we can't read the values and monitor that bit at the
900 * exact same time). So the solution used here is to read the high
901 * byte once, then the low byte, then the high byte again. If the new
902 * high byte matches the old one, then we have a valid reading. Else
903 * we have to read the low byte again, and now we believe we have a
906 if ((err
= lm90_read_reg(client
, regh
, &oldh
))
907 || (err
= lm90_read_reg(client
, regl
, &l
))
908 || (err
= lm90_read_reg(client
, regh
, &newh
)))
911 err
= lm90_read_reg(client
, regl
, &l
);
915 *value
= (newh
<< 8) | l
;
920 static struct lm90_data
*lm90_update_device(struct device
*dev
)
922 struct i2c_client
*client
= to_i2c_client(dev
);
923 struct lm90_data
*data
= i2c_get_clientdata(client
);
925 mutex_lock(&data
->update_lock
);
927 if (time_after(jiffies
, data
->last_updated
+ HZ
* 2) || !data
->valid
) {
930 dev_dbg(&client
->dev
, "Updating lm90 data.\n");
931 lm90_read_reg(client
, LM90_REG_R_LOCAL_LOW
, &data
->temp8
[0]);
932 lm90_read_reg(client
, LM90_REG_R_LOCAL_HIGH
, &data
->temp8
[1]);
933 lm90_read_reg(client
, LM90_REG_R_LOCAL_CRIT
, &data
->temp8
[2]);
934 lm90_read_reg(client
, LM90_REG_R_REMOTE_CRIT
, &data
->temp8
[3]);
935 lm90_read_reg(client
, LM90_REG_R_TCRIT_HYST
, &data
->temp_hyst
);
937 if (data
->kind
== max6657
|| data
->kind
== max6646
) {
938 lm90_read16(client
, LM90_REG_R_LOCAL_TEMP
,
939 MAX6657_REG_R_LOCAL_TEMPL
,
942 if (lm90_read_reg(client
, LM90_REG_R_LOCAL_TEMP
,
944 data
->temp11
[4] = h
<< 8;
946 lm90_read16(client
, LM90_REG_R_REMOTE_TEMPH
,
947 LM90_REG_R_REMOTE_TEMPL
, &data
->temp11
[0]);
949 if (lm90_read_reg(client
, LM90_REG_R_REMOTE_LOWH
, &h
) == 0) {
950 data
->temp11
[1] = h
<< 8;
951 if (data
->kind
!= max6657
&& data
->kind
!= max6680
952 && data
->kind
!= max6646
953 && lm90_read_reg(client
, LM90_REG_R_REMOTE_LOWL
,
955 data
->temp11
[1] |= l
;
957 if (lm90_read_reg(client
, LM90_REG_R_REMOTE_HIGHH
, &h
) == 0) {
958 data
->temp11
[2] = h
<< 8;
959 if (data
->kind
!= max6657
&& data
->kind
!= max6680
960 && data
->kind
!= max6646
961 && lm90_read_reg(client
, LM90_REG_R_REMOTE_HIGHL
,
963 data
->temp11
[2] |= l
;
966 if (data
->kind
!= max6657
&& data
->kind
!= max6646
) {
967 if (lm90_read_reg(client
, LM90_REG_R_REMOTE_OFFSH
,
969 && lm90_read_reg(client
, LM90_REG_R_REMOTE_OFFSL
,
971 data
->temp11
[3] = (h
<< 8) | l
;
973 lm90_read_reg(client
, LM90_REG_R_STATUS
, &data
->alarms
);
975 data
->last_updated
= jiffies
;
979 mutex_unlock(&data
->update_lock
);
984 static int __init
sensors_lm90_init(void)
986 return i2c_add_driver(&lm90_driver
);
989 static void __exit
sensors_lm90_exit(void)
991 i2c_del_driver(&lm90_driver
);
994 MODULE_AUTHOR("Jean Delvare <khali@linux-fr.org>");
995 MODULE_DESCRIPTION("LM90/ADM1032 driver");
996 MODULE_LICENSE("GPL");
998 module_init(sensors_lm90_init
);
999 module_exit(sensors_lm90_exit
);