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. We use the device address to detect MAX6659, which will result
32 * in a detection as max6657 if it is on address 0x4c. The extra address
33 * and features of the MAX6659 are only supported if the chip is configured
34 * explicitly as max6659, or if its address is not 0x4c.
35 * These chips lack the remote temperature offset feature.
37 * This driver also supports the MAX6646, MAX6647, MAX6648, MAX6649 and
38 * MAX6692 chips made by Maxim. These are again similar to the LM86,
39 * but they use unsigned temperature values and can report temperatures
40 * from 0 to 145 degrees.
42 * This driver also supports the MAX6680 and MAX6681, two other sensor
43 * chips made by Maxim. These are quite similar to the other Maxim
44 * chips. The MAX6680 and MAX6681 only differ in the pinout so they can
45 * be treated identically.
47 * This driver also supports the MAX6695 and MAX6696, two other sensor
48 * chips made by Maxim. These are also quite similar to other Maxim
49 * chips, but support three temperature sensors instead of two. MAX6695
50 * and MAX6696 only differ in the pinout so they can be treated identically.
52 * This driver also supports the ADT7461 chip from Analog Devices.
53 * It's supported in both compatibility and extended mode. It is mostly
54 * compatible with LM90 except for a data format difference for the
55 * temperature value registers.
57 * Since the LM90 was the first chipset supported by this driver, most
58 * comments will refer to this chipset, but are actually general and
59 * concern all supported chipsets, unless mentioned otherwise.
61 * This program is free software; you can redistribute it and/or modify
62 * it under the terms of the GNU General Public License as published by
63 * the Free Software Foundation; either version 2 of the License, or
64 * (at your option) any later version.
66 * This program is distributed in the hope that it will be useful,
67 * but WITHOUT ANY WARRANTY; without even the implied warranty of
68 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
69 * GNU General Public License for more details.
71 * You should have received a copy of the GNU General Public License
72 * along with this program; if not, write to the Free Software
73 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
76 #include <linux/module.h>
77 #include <linux/init.h>
78 #include <linux/slab.h>
79 #include <linux/jiffies.h>
80 #include <linux/i2c.h>
81 #include <linux/hwmon-sysfs.h>
82 #include <linux/hwmon.h>
83 #include <linux/err.h>
84 #include <linux/mutex.h>
85 #include <linux/sysfs.h>
89 * Address is fully defined internally and cannot be changed except for
90 * MAX6659, MAX6680 and MAX6681.
91 * LM86, LM89, LM90, LM99, ADM1032, ADM1032-1, ADT7461, MAX6649, MAX6657,
92 * MAX6658 and W83L771 have address 0x4c.
93 * ADM1032-2, ADT7461-2, LM89-1, LM99-1 and MAX6646 have address 0x4d.
94 * MAX6647 has address 0x4e.
95 * MAX6659 can have address 0x4c, 0x4d or 0x4e.
96 * MAX6680 and MAX6681 can have address 0x18, 0x19, 0x1a, 0x29, 0x2a, 0x2b,
100 static const unsigned short normal_i2c
[] = {
101 0x18, 0x19, 0x1a, 0x29, 0x2a, 0x2b, 0x4c, 0x4d, 0x4e, I2C_CLIENT_END
};
103 enum chips
{ lm90
, adm1032
, lm99
, lm86
, max6657
, max6659
, adt7461
, max6680
,
104 max6646
, w83l771
, max6696
};
110 #define LM90_REG_R_MAN_ID 0xFE
111 #define LM90_REG_R_CHIP_ID 0xFF
112 #define LM90_REG_R_CONFIG1 0x03
113 #define LM90_REG_W_CONFIG1 0x09
114 #define LM90_REG_R_CONFIG2 0xBF
115 #define LM90_REG_W_CONFIG2 0xBF
116 #define LM90_REG_R_CONVRATE 0x04
117 #define LM90_REG_W_CONVRATE 0x0A
118 #define LM90_REG_R_STATUS 0x02
119 #define LM90_REG_R_LOCAL_TEMP 0x00
120 #define LM90_REG_R_LOCAL_HIGH 0x05
121 #define LM90_REG_W_LOCAL_HIGH 0x0B
122 #define LM90_REG_R_LOCAL_LOW 0x06
123 #define LM90_REG_W_LOCAL_LOW 0x0C
124 #define LM90_REG_R_LOCAL_CRIT 0x20
125 #define LM90_REG_W_LOCAL_CRIT 0x20
126 #define LM90_REG_R_REMOTE_TEMPH 0x01
127 #define LM90_REG_R_REMOTE_TEMPL 0x10
128 #define LM90_REG_R_REMOTE_OFFSH 0x11
129 #define LM90_REG_W_REMOTE_OFFSH 0x11
130 #define LM90_REG_R_REMOTE_OFFSL 0x12
131 #define LM90_REG_W_REMOTE_OFFSL 0x12
132 #define LM90_REG_R_REMOTE_HIGHH 0x07
133 #define LM90_REG_W_REMOTE_HIGHH 0x0D
134 #define LM90_REG_R_REMOTE_HIGHL 0x13
135 #define LM90_REG_W_REMOTE_HIGHL 0x13
136 #define LM90_REG_R_REMOTE_LOWH 0x08
137 #define LM90_REG_W_REMOTE_LOWH 0x0E
138 #define LM90_REG_R_REMOTE_LOWL 0x14
139 #define LM90_REG_W_REMOTE_LOWL 0x14
140 #define LM90_REG_R_REMOTE_CRIT 0x19
141 #define LM90_REG_W_REMOTE_CRIT 0x19
142 #define LM90_REG_R_TCRIT_HYST 0x21
143 #define LM90_REG_W_TCRIT_HYST 0x21
145 /* MAX6646/6647/6649/6657/6658/6659/6695/6696 registers */
147 #define MAX6657_REG_R_LOCAL_TEMPL 0x11
148 #define MAX6696_REG_R_STATUS2 0x12
149 #define MAX6659_REG_R_REMOTE_EMERG 0x16
150 #define MAX6659_REG_W_REMOTE_EMERG 0x16
151 #define MAX6659_REG_R_LOCAL_EMERG 0x17
152 #define MAX6659_REG_W_LOCAL_EMERG 0x17
154 #define LM90_DEF_CONVRATE_RVAL 6 /* Def conversion rate register value */
155 #define LM90_MAX_CONVRATE_MS 16000 /* Maximum conversion rate in ms */
160 #define LM90_FLAG_ADT7461_EXT (1 << 0) /* ADT7461 extended mode */
161 /* Device features */
162 #define LM90_HAVE_OFFSET (1 << 1) /* temperature offset register */
163 #define LM90_HAVE_LOCAL_EXT (1 << 2) /* extended local temperature */
164 #define LM90_HAVE_REM_LIMIT_EXT (1 << 3) /* extended remote limit */
165 #define LM90_HAVE_EMERGENCY (1 << 4) /* 3rd upper (emergency) limit */
166 #define LM90_HAVE_EMERGENCY_ALARM (1 << 5)/* emergency alarm */
167 #define LM90_HAVE_TEMP3 (1 << 6) /* 3rd temperature sensor */
168 #define LM90_HAVE_BROKEN_ALERT (1 << 7) /* Broken alert */
171 * Driver data (common to all clients)
174 static const struct i2c_device_id lm90_id
[] = {
175 { "adm1032", adm1032
},
176 { "adt7461", adt7461
},
181 { "max6646", max6646
},
182 { "max6647", max6646
},
183 { "max6649", max6646
},
184 { "max6657", max6657
},
185 { "max6658", max6657
},
186 { "max6659", max6659
},
187 { "max6680", max6680
},
188 { "max6681", max6680
},
189 { "max6695", max6696
},
190 { "max6696", max6696
},
191 { "w83l771", w83l771
},
194 MODULE_DEVICE_TABLE(i2c
, lm90_id
);
197 * chip type specific parameters
200 u32 flags
; /* Capabilities */
201 u16 alert_alarms
; /* Which alarm bits trigger ALERT# */
202 /* Upper 8 bits for max6695/96 */
203 u8 max_convrate
; /* Maximum conversion rate register value */
206 static const struct lm90_params lm90_params
[] = {
208 .flags
= LM90_HAVE_OFFSET
| LM90_HAVE_REM_LIMIT_EXT
209 | LM90_HAVE_BROKEN_ALERT
,
210 .alert_alarms
= 0x7c,
214 .flags
= LM90_HAVE_OFFSET
| LM90_HAVE_REM_LIMIT_EXT
215 | LM90_HAVE_BROKEN_ALERT
,
216 .alert_alarms
= 0x7c,
220 .flags
= LM90_HAVE_OFFSET
| LM90_HAVE_REM_LIMIT_EXT
,
221 .alert_alarms
= 0x7b,
225 .flags
= LM90_HAVE_OFFSET
| LM90_HAVE_REM_LIMIT_EXT
,
226 .alert_alarms
= 0x7b,
230 .flags
= LM90_HAVE_OFFSET
| LM90_HAVE_REM_LIMIT_EXT
,
231 .alert_alarms
= 0x7b,
235 .flags
= LM90_HAVE_LOCAL_EXT
,
236 .alert_alarms
= 0x7c,
240 .flags
= LM90_HAVE_LOCAL_EXT
,
241 .alert_alarms
= 0x7c,
245 .flags
= LM90_HAVE_LOCAL_EXT
| LM90_HAVE_EMERGENCY
,
246 .alert_alarms
= 0x7c,
250 .flags
= LM90_HAVE_OFFSET
,
251 .alert_alarms
= 0x7c,
255 .flags
= LM90_HAVE_LOCAL_EXT
| LM90_HAVE_EMERGENCY
256 | LM90_HAVE_EMERGENCY_ALARM
| LM90_HAVE_TEMP3
,
257 .alert_alarms
= 0x187c,
261 .flags
= LM90_HAVE_OFFSET
| LM90_HAVE_REM_LIMIT_EXT
,
262 .alert_alarms
= 0x7c,
268 * Client data (each client gets its own)
272 struct device
*hwmon_dev
;
273 struct mutex update_lock
;
274 char valid
; /* zero until following fields are valid */
275 unsigned long last_updated
; /* in jiffies */
279 int update_interval
; /* in milliseconds */
281 u8 config_orig
; /* Original configuration register value */
282 u8 convrate_orig
; /* Original conversion rate register value */
283 u16 alert_alarms
; /* Which alarm bits trigger ALERT# */
284 /* Upper 8 bits for max6695/96 */
285 u8 max_convrate
; /* Maximum conversion rate */
287 /* registers values */
288 s8 temp8
[8]; /* 0: local low limit
290 2: local critical limit
291 3: remote critical limit
292 4: local emergency limit (max6659 and max6695/96)
293 5: remote emergency limit (max6659 and max6695/96)
294 6: remote 2 critical limit (max6695/96 only)
295 7: remote 2 emergency limit (max6695/96 only) */
296 s16 temp11
[8]; /* 0: remote input
299 3: remote offset (except max6646, max6657/58/59,
302 5: remote 2 input (max6695/96 only)
303 6: remote 2 low limit (max6695/96 only)
304 7: remote 2 high limit (ma6695/96 only) */
306 u16 alarms
; /* bitvector (upper 8 bits for max6695/96) */
314 * The ADM1032 supports PEC but not on write byte transactions, so we need
315 * to explicitly ask for a transaction without PEC.
317 static inline s32
adm1032_write_byte(struct i2c_client
*client
, u8 value
)
319 return i2c_smbus_xfer(client
->adapter
, client
->addr
,
320 client
->flags
& ~I2C_CLIENT_PEC
,
321 I2C_SMBUS_WRITE
, value
, I2C_SMBUS_BYTE
, NULL
);
325 * It is assumed that client->update_lock is held (unless we are in
326 * detection or initialization steps). This matters when PEC is enabled,
327 * because we don't want the address pointer to change between the write
328 * byte and the read byte transactions.
330 static int lm90_read_reg(struct i2c_client
*client
, u8 reg
, u8
*value
)
334 if (client
->flags
& I2C_CLIENT_PEC
) {
335 err
= adm1032_write_byte(client
, reg
);
337 err
= i2c_smbus_read_byte(client
);
339 err
= i2c_smbus_read_byte_data(client
, reg
);
342 dev_warn(&client
->dev
, "Register %#02x read failed (%d)\n",
351 static int lm90_read16(struct i2c_client
*client
, u8 regh
, u8 regl
, u16
*value
)
357 * There is a trick here. We have to read two registers to have the
358 * sensor temperature, but we have to beware a conversion could occur
359 * between the readings. The datasheet says we should either use
360 * the one-shot conversion register, which we don't want to do
361 * (disables hardware monitoring) or monitor the busy bit, which is
362 * impossible (we can't read the values and monitor that bit at the
363 * exact same time). So the solution used here is to read the high
364 * byte once, then the low byte, then the high byte again. If the new
365 * high byte matches the old one, then we have a valid reading. Else
366 * we have to read the low byte again, and now we believe we have a
369 if ((err
= lm90_read_reg(client
, regh
, &oldh
))
370 || (err
= lm90_read_reg(client
, regl
, &l
))
371 || (err
= lm90_read_reg(client
, regh
, &newh
)))
374 err
= lm90_read_reg(client
, regl
, &l
);
378 *value
= (newh
<< 8) | l
;
384 * client->update_lock must be held when calling this function (unless we are
385 * in detection or initialization steps), and while a remote channel other
386 * than channel 0 is selected. Also, calling code must make sure to re-select
387 * external channel 0 before releasing the lock. This is necessary because
388 * various registers have different meanings as a result of selecting a
389 * non-default remote channel.
391 static inline void lm90_select_remote_channel(struct i2c_client
*client
,
392 struct lm90_data
*data
,
397 if (data
->kind
== max6696
) {
398 lm90_read_reg(client
, LM90_REG_R_CONFIG1
, &config
);
402 i2c_smbus_write_byte_data(client
, LM90_REG_W_CONFIG1
,
408 * Set conversion rate.
409 * client->update_lock must be held when calling this function (unless we are
410 * in detection or initialization steps).
412 static void lm90_set_convrate(struct i2c_client
*client
, struct lm90_data
*data
,
413 unsigned int interval
)
416 unsigned int update_interval
;
418 /* Shift calculations to avoid rounding errors */
421 /* find the nearest update rate */
422 for (i
= 0, update_interval
= LM90_MAX_CONVRATE_MS
<< 6;
423 i
< data
->max_convrate
; i
++, update_interval
>>= 1)
424 if (interval
>= update_interval
* 3 / 4)
427 i2c_smbus_write_byte_data(client
, LM90_REG_W_CONVRATE
, i
);
428 data
->update_interval
= DIV_ROUND_CLOSEST(update_interval
, 64);
431 static struct lm90_data
*lm90_update_device(struct device
*dev
)
433 struct i2c_client
*client
= to_i2c_client(dev
);
434 struct lm90_data
*data
= i2c_get_clientdata(client
);
435 unsigned long next_update
;
437 mutex_lock(&data
->update_lock
);
439 next_update
= data
->last_updated
440 + msecs_to_jiffies(data
->update_interval
) + 1;
441 if (time_after(jiffies
, next_update
) || !data
->valid
) {
445 dev_dbg(&client
->dev
, "Updating lm90 data.\n");
446 lm90_read_reg(client
, LM90_REG_R_LOCAL_LOW
, &data
->temp8
[0]);
447 lm90_read_reg(client
, LM90_REG_R_LOCAL_HIGH
, &data
->temp8
[1]);
448 lm90_read_reg(client
, LM90_REG_R_LOCAL_CRIT
, &data
->temp8
[2]);
449 lm90_read_reg(client
, LM90_REG_R_REMOTE_CRIT
, &data
->temp8
[3]);
450 lm90_read_reg(client
, LM90_REG_R_TCRIT_HYST
, &data
->temp_hyst
);
452 if (data
->flags
& LM90_HAVE_LOCAL_EXT
) {
453 lm90_read16(client
, LM90_REG_R_LOCAL_TEMP
,
454 MAX6657_REG_R_LOCAL_TEMPL
,
457 if (lm90_read_reg(client
, LM90_REG_R_LOCAL_TEMP
,
459 data
->temp11
[4] = h
<< 8;
461 lm90_read16(client
, LM90_REG_R_REMOTE_TEMPH
,
462 LM90_REG_R_REMOTE_TEMPL
, &data
->temp11
[0]);
464 if (lm90_read_reg(client
, LM90_REG_R_REMOTE_LOWH
, &h
) == 0) {
465 data
->temp11
[1] = h
<< 8;
466 if ((data
->flags
& LM90_HAVE_REM_LIMIT_EXT
)
467 && lm90_read_reg(client
, LM90_REG_R_REMOTE_LOWL
,
469 data
->temp11
[1] |= l
;
471 if (lm90_read_reg(client
, LM90_REG_R_REMOTE_HIGHH
, &h
) == 0) {
472 data
->temp11
[2] = h
<< 8;
473 if ((data
->flags
& LM90_HAVE_REM_LIMIT_EXT
)
474 && lm90_read_reg(client
, LM90_REG_R_REMOTE_HIGHL
,
476 data
->temp11
[2] |= l
;
479 if (data
->flags
& LM90_HAVE_OFFSET
) {
480 if (lm90_read_reg(client
, LM90_REG_R_REMOTE_OFFSH
,
482 && lm90_read_reg(client
, LM90_REG_R_REMOTE_OFFSL
,
484 data
->temp11
[3] = (h
<< 8) | l
;
486 if (data
->flags
& LM90_HAVE_EMERGENCY
) {
487 lm90_read_reg(client
, MAX6659_REG_R_LOCAL_EMERG
,
489 lm90_read_reg(client
, MAX6659_REG_R_REMOTE_EMERG
,
492 lm90_read_reg(client
, LM90_REG_R_STATUS
, &alarms
);
493 data
->alarms
= alarms
; /* save as 16 bit value */
495 if (data
->kind
== max6696
) {
496 lm90_select_remote_channel(client
, data
, 1);
497 lm90_read_reg(client
, LM90_REG_R_REMOTE_CRIT
,
499 lm90_read_reg(client
, MAX6659_REG_R_REMOTE_EMERG
,
501 lm90_read16(client
, LM90_REG_R_REMOTE_TEMPH
,
502 LM90_REG_R_REMOTE_TEMPL
, &data
->temp11
[5]);
503 if (!lm90_read_reg(client
, LM90_REG_R_REMOTE_LOWH
, &h
))
504 data
->temp11
[6] = h
<< 8;
505 if (!lm90_read_reg(client
, LM90_REG_R_REMOTE_HIGHH
, &h
))
506 data
->temp11
[7] = h
<< 8;
507 lm90_select_remote_channel(client
, data
, 0);
509 if (!lm90_read_reg(client
, MAX6696_REG_R_STATUS2
,
511 data
->alarms
|= alarms
<< 8;
514 /* Re-enable ALERT# output if it was originally enabled and
515 * relevant alarms are all clear */
516 if ((data
->config_orig
& 0x80) == 0
517 && (data
->alarms
& data
->alert_alarms
) == 0) {
520 lm90_read_reg(client
, LM90_REG_R_CONFIG1
, &config
);
522 dev_dbg(&client
->dev
, "Re-enabling ALERT#\n");
523 i2c_smbus_write_byte_data(client
,
529 data
->last_updated
= jiffies
;
533 mutex_unlock(&data
->update_lock
);
540 * For local temperatures and limits, critical limits and the hysteresis
541 * value, the LM90 uses signed 8-bit values with LSB = 1 degree Celsius.
542 * For remote temperatures and limits, it uses signed 11-bit values with
543 * LSB = 0.125 degree Celsius, left-justified in 16-bit registers. Some
544 * Maxim chips use unsigned values.
547 static inline int temp_from_s8(s8 val
)
552 static inline int temp_from_u8(u8 val
)
557 static inline int temp_from_s16(s16 val
)
559 return val
/ 32 * 125;
562 static inline int temp_from_u16(u16 val
)
564 return val
/ 32 * 125;
567 static s8
temp_to_s8(long val
)
574 return (val
- 500) / 1000;
575 return (val
+ 500) / 1000;
578 static u8
temp_to_u8(long val
)
584 return (val
+ 500) / 1000;
587 static s16
temp_to_s16(long val
)
594 return (val
- 62) / 125 * 32;
595 return (val
+ 62) / 125 * 32;
598 static u8
hyst_to_reg(long val
)
604 return (val
+ 500) / 1000;
608 * ADT7461 in compatibility mode is almost identical to LM90 except that
609 * attempts to write values that are outside the range 0 < temp < 127 are
610 * treated as the boundary value.
612 * ADT7461 in "extended mode" operation uses unsigned integers offset by
613 * 64 (e.g., 0 -> -64 degC). The range is restricted to -64..191 degC.
615 static inline int temp_from_u8_adt7461(struct lm90_data
*data
, u8 val
)
617 if (data
->flags
& LM90_FLAG_ADT7461_EXT
)
618 return (val
- 64) * 1000;
620 return temp_from_s8(val
);
623 static inline int temp_from_u16_adt7461(struct lm90_data
*data
, u16 val
)
625 if (data
->flags
& LM90_FLAG_ADT7461_EXT
)
626 return (val
- 0x4000) / 64 * 250;
628 return temp_from_s16(val
);
631 static u8
temp_to_u8_adt7461(struct lm90_data
*data
, long val
)
633 if (data
->flags
& LM90_FLAG_ADT7461_EXT
) {
638 return (val
+ 500 + 64000) / 1000;
644 return (val
+ 500) / 1000;
648 static u16
temp_to_u16_adt7461(struct lm90_data
*data
, long val
)
650 if (data
->flags
& LM90_FLAG_ADT7461_EXT
) {
655 return (val
+ 64000 + 125) / 250 * 64;
661 return (val
+ 125) / 250 * 64;
669 static ssize_t
show_temp8(struct device
*dev
, struct device_attribute
*devattr
,
672 struct sensor_device_attribute
*attr
= to_sensor_dev_attr(devattr
);
673 struct lm90_data
*data
= lm90_update_device(dev
);
676 if (data
->kind
== adt7461
)
677 temp
= temp_from_u8_adt7461(data
, data
->temp8
[attr
->index
]);
678 else if (data
->kind
== max6646
)
679 temp
= temp_from_u8(data
->temp8
[attr
->index
]);
681 temp
= temp_from_s8(data
->temp8
[attr
->index
]);
683 /* +16 degrees offset for temp2 for the LM99 */
684 if (data
->kind
== lm99
&& attr
->index
== 3)
687 return sprintf(buf
, "%d\n", temp
);
690 static ssize_t
set_temp8(struct device
*dev
, struct device_attribute
*devattr
,
691 const char *buf
, size_t count
)
693 static const u8 reg
[8] = {
694 LM90_REG_W_LOCAL_LOW
,
695 LM90_REG_W_LOCAL_HIGH
,
696 LM90_REG_W_LOCAL_CRIT
,
697 LM90_REG_W_REMOTE_CRIT
,
698 MAX6659_REG_W_LOCAL_EMERG
,
699 MAX6659_REG_W_REMOTE_EMERG
,
700 LM90_REG_W_REMOTE_CRIT
,
701 MAX6659_REG_W_REMOTE_EMERG
,
704 struct sensor_device_attribute
*attr
= to_sensor_dev_attr(devattr
);
705 struct i2c_client
*client
= to_i2c_client(dev
);
706 struct lm90_data
*data
= i2c_get_clientdata(client
);
707 int nr
= attr
->index
;
711 err
= strict_strtol(buf
, 10, &val
);
715 /* +16 degrees offset for temp2 for the LM99 */
716 if (data
->kind
== lm99
&& attr
->index
== 3)
719 mutex_lock(&data
->update_lock
);
720 if (data
->kind
== adt7461
)
721 data
->temp8
[nr
] = temp_to_u8_adt7461(data
, val
);
722 else if (data
->kind
== max6646
)
723 data
->temp8
[nr
] = temp_to_u8(val
);
725 data
->temp8
[nr
] = temp_to_s8(val
);
727 lm90_select_remote_channel(client
, data
, nr
>= 6);
728 i2c_smbus_write_byte_data(client
, reg
[nr
], data
->temp8
[nr
]);
729 lm90_select_remote_channel(client
, data
, 0);
731 mutex_unlock(&data
->update_lock
);
735 static ssize_t
show_temp11(struct device
*dev
, struct device_attribute
*devattr
,
738 struct sensor_device_attribute_2
*attr
= to_sensor_dev_attr_2(devattr
);
739 struct lm90_data
*data
= lm90_update_device(dev
);
742 if (data
->kind
== adt7461
)
743 temp
= temp_from_u16_adt7461(data
, data
->temp11
[attr
->index
]);
744 else if (data
->kind
== max6646
)
745 temp
= temp_from_u16(data
->temp11
[attr
->index
]);
747 temp
= temp_from_s16(data
->temp11
[attr
->index
]);
749 /* +16 degrees offset for temp2 for the LM99 */
750 if (data
->kind
== lm99
&& attr
->index
<= 2)
753 return sprintf(buf
, "%d\n", temp
);
756 static ssize_t
set_temp11(struct device
*dev
, struct device_attribute
*devattr
,
757 const char *buf
, size_t count
)
764 { LM90_REG_W_REMOTE_LOWH
, LM90_REG_W_REMOTE_LOWL
, 0 },
765 { LM90_REG_W_REMOTE_HIGHH
, LM90_REG_W_REMOTE_HIGHL
, 0 },
766 { LM90_REG_W_REMOTE_OFFSH
, LM90_REG_W_REMOTE_OFFSL
, 0 },
767 { LM90_REG_W_REMOTE_LOWH
, LM90_REG_W_REMOTE_LOWL
, 1 },
768 { LM90_REG_W_REMOTE_HIGHH
, LM90_REG_W_REMOTE_HIGHL
, 1 }
771 struct sensor_device_attribute_2
*attr
= to_sensor_dev_attr_2(devattr
);
772 struct i2c_client
*client
= to_i2c_client(dev
);
773 struct lm90_data
*data
= i2c_get_clientdata(client
);
775 int index
= attr
->index
;
779 err
= strict_strtol(buf
, 10, &val
);
783 /* +16 degrees offset for temp2 for the LM99 */
784 if (data
->kind
== lm99
&& index
<= 2)
787 mutex_lock(&data
->update_lock
);
788 if (data
->kind
== adt7461
)
789 data
->temp11
[index
] = temp_to_u16_adt7461(data
, val
);
790 else if (data
->kind
== max6646
)
791 data
->temp11
[index
] = temp_to_u8(val
) << 8;
792 else if (data
->flags
& LM90_HAVE_REM_LIMIT_EXT
)
793 data
->temp11
[index
] = temp_to_s16(val
);
795 data
->temp11
[index
] = temp_to_s8(val
) << 8;
797 lm90_select_remote_channel(client
, data
, reg
[nr
].channel
);
798 i2c_smbus_write_byte_data(client
, reg
[nr
].high
,
799 data
->temp11
[index
] >> 8);
800 if (data
->flags
& LM90_HAVE_REM_LIMIT_EXT
)
801 i2c_smbus_write_byte_data(client
, reg
[nr
].low
,
802 data
->temp11
[index
] & 0xff);
803 lm90_select_remote_channel(client
, data
, 0);
805 mutex_unlock(&data
->update_lock
);
809 static ssize_t
show_temphyst(struct device
*dev
,
810 struct device_attribute
*devattr
,
813 struct sensor_device_attribute
*attr
= to_sensor_dev_attr(devattr
);
814 struct lm90_data
*data
= lm90_update_device(dev
);
817 if (data
->kind
== adt7461
)
818 temp
= temp_from_u8_adt7461(data
, data
->temp8
[attr
->index
]);
819 else if (data
->kind
== max6646
)
820 temp
= temp_from_u8(data
->temp8
[attr
->index
]);
822 temp
= temp_from_s8(data
->temp8
[attr
->index
]);
824 /* +16 degrees offset for temp2 for the LM99 */
825 if (data
->kind
== lm99
&& attr
->index
== 3)
828 return sprintf(buf
, "%d\n", temp
- temp_from_s8(data
->temp_hyst
));
831 static ssize_t
set_temphyst(struct device
*dev
, struct device_attribute
*dummy
,
832 const char *buf
, size_t count
)
834 struct i2c_client
*client
= to_i2c_client(dev
);
835 struct lm90_data
*data
= i2c_get_clientdata(client
);
840 err
= strict_strtol(buf
, 10, &val
);
844 mutex_lock(&data
->update_lock
);
845 if (data
->kind
== adt7461
)
846 temp
= temp_from_u8_adt7461(data
, data
->temp8
[2]);
847 else if (data
->kind
== max6646
)
848 temp
= temp_from_u8(data
->temp8
[2]);
850 temp
= temp_from_s8(data
->temp8
[2]);
852 data
->temp_hyst
= hyst_to_reg(temp
- val
);
853 i2c_smbus_write_byte_data(client
, LM90_REG_W_TCRIT_HYST
,
855 mutex_unlock(&data
->update_lock
);
859 static ssize_t
show_alarms(struct device
*dev
, struct device_attribute
*dummy
,
862 struct lm90_data
*data
= lm90_update_device(dev
);
863 return sprintf(buf
, "%d\n", data
->alarms
);
866 static ssize_t
show_alarm(struct device
*dev
, struct device_attribute
869 struct sensor_device_attribute
*attr
= to_sensor_dev_attr(devattr
);
870 struct lm90_data
*data
= lm90_update_device(dev
);
871 int bitnr
= attr
->index
;
873 return sprintf(buf
, "%d\n", (data
->alarms
>> bitnr
) & 1);
876 static ssize_t
show_update_interval(struct device
*dev
,
877 struct device_attribute
*attr
, char *buf
)
879 struct lm90_data
*data
= dev_get_drvdata(dev
);
881 return sprintf(buf
, "%u\n", data
->update_interval
);
884 static ssize_t
set_update_interval(struct device
*dev
,
885 struct device_attribute
*attr
,
886 const char *buf
, size_t count
)
888 struct i2c_client
*client
= to_i2c_client(dev
);
889 struct lm90_data
*data
= i2c_get_clientdata(client
);
893 err
= strict_strtoul(buf
, 10, &val
);
897 mutex_lock(&data
->update_lock
);
898 lm90_set_convrate(client
, data
, val
);
899 mutex_unlock(&data
->update_lock
);
904 static SENSOR_DEVICE_ATTR_2(temp1_input
, S_IRUGO
, show_temp11
, NULL
, 0, 4);
905 static SENSOR_DEVICE_ATTR_2(temp2_input
, S_IRUGO
, show_temp11
, NULL
, 0, 0);
906 static SENSOR_DEVICE_ATTR(temp1_min
, S_IWUSR
| S_IRUGO
, show_temp8
,
908 static SENSOR_DEVICE_ATTR_2(temp2_min
, S_IWUSR
| S_IRUGO
, show_temp11
,
910 static SENSOR_DEVICE_ATTR(temp1_max
, S_IWUSR
| S_IRUGO
, show_temp8
,
912 static SENSOR_DEVICE_ATTR_2(temp2_max
, S_IWUSR
| S_IRUGO
, show_temp11
,
914 static SENSOR_DEVICE_ATTR(temp1_crit
, S_IWUSR
| S_IRUGO
, show_temp8
,
916 static SENSOR_DEVICE_ATTR(temp2_crit
, S_IWUSR
| S_IRUGO
, show_temp8
,
918 static SENSOR_DEVICE_ATTR(temp1_crit_hyst
, S_IWUSR
| S_IRUGO
, show_temphyst
,
920 static SENSOR_DEVICE_ATTR(temp2_crit_hyst
, S_IRUGO
, show_temphyst
, NULL
, 3);
921 static SENSOR_DEVICE_ATTR_2(temp2_offset
, S_IWUSR
| S_IRUGO
, show_temp11
,
924 /* Individual alarm files */
925 static SENSOR_DEVICE_ATTR(temp1_crit_alarm
, S_IRUGO
, show_alarm
, NULL
, 0);
926 static SENSOR_DEVICE_ATTR(temp2_crit_alarm
, S_IRUGO
, show_alarm
, NULL
, 1);
927 static SENSOR_DEVICE_ATTR(temp2_fault
, S_IRUGO
, show_alarm
, NULL
, 2);
928 static SENSOR_DEVICE_ATTR(temp2_min_alarm
, S_IRUGO
, show_alarm
, NULL
, 3);
929 static SENSOR_DEVICE_ATTR(temp2_max_alarm
, S_IRUGO
, show_alarm
, NULL
, 4);
930 static SENSOR_DEVICE_ATTR(temp1_min_alarm
, S_IRUGO
, show_alarm
, NULL
, 5);
931 static SENSOR_DEVICE_ATTR(temp1_max_alarm
, S_IRUGO
, show_alarm
, NULL
, 6);
932 /* Raw alarm file for compatibility */
933 static DEVICE_ATTR(alarms
, S_IRUGO
, show_alarms
, NULL
);
935 static DEVICE_ATTR(update_interval
, S_IRUGO
| S_IWUSR
, show_update_interval
,
936 set_update_interval
);
938 static struct attribute
*lm90_attributes
[] = {
939 &sensor_dev_attr_temp1_input
.dev_attr
.attr
,
940 &sensor_dev_attr_temp2_input
.dev_attr
.attr
,
941 &sensor_dev_attr_temp1_min
.dev_attr
.attr
,
942 &sensor_dev_attr_temp2_min
.dev_attr
.attr
,
943 &sensor_dev_attr_temp1_max
.dev_attr
.attr
,
944 &sensor_dev_attr_temp2_max
.dev_attr
.attr
,
945 &sensor_dev_attr_temp1_crit
.dev_attr
.attr
,
946 &sensor_dev_attr_temp2_crit
.dev_attr
.attr
,
947 &sensor_dev_attr_temp1_crit_hyst
.dev_attr
.attr
,
948 &sensor_dev_attr_temp2_crit_hyst
.dev_attr
.attr
,
950 &sensor_dev_attr_temp1_crit_alarm
.dev_attr
.attr
,
951 &sensor_dev_attr_temp2_crit_alarm
.dev_attr
.attr
,
952 &sensor_dev_attr_temp2_fault
.dev_attr
.attr
,
953 &sensor_dev_attr_temp2_min_alarm
.dev_attr
.attr
,
954 &sensor_dev_attr_temp2_max_alarm
.dev_attr
.attr
,
955 &sensor_dev_attr_temp1_min_alarm
.dev_attr
.attr
,
956 &sensor_dev_attr_temp1_max_alarm
.dev_attr
.attr
,
957 &dev_attr_alarms
.attr
,
958 &dev_attr_update_interval
.attr
,
962 static const struct attribute_group lm90_group
= {
963 .attrs
= lm90_attributes
,
967 * Additional attributes for devices with emergency sensors
969 static SENSOR_DEVICE_ATTR(temp1_emergency
, S_IWUSR
| S_IRUGO
, show_temp8
,
971 static SENSOR_DEVICE_ATTR(temp2_emergency
, S_IWUSR
| S_IRUGO
, show_temp8
,
973 static SENSOR_DEVICE_ATTR(temp1_emergency_hyst
, S_IRUGO
, show_temphyst
,
975 static SENSOR_DEVICE_ATTR(temp2_emergency_hyst
, S_IRUGO
, show_temphyst
,
978 static struct attribute
*lm90_emergency_attributes
[] = {
979 &sensor_dev_attr_temp1_emergency
.dev_attr
.attr
,
980 &sensor_dev_attr_temp2_emergency
.dev_attr
.attr
,
981 &sensor_dev_attr_temp1_emergency_hyst
.dev_attr
.attr
,
982 &sensor_dev_attr_temp2_emergency_hyst
.dev_attr
.attr
,
986 static const struct attribute_group lm90_emergency_group
= {
987 .attrs
= lm90_emergency_attributes
,
990 static SENSOR_DEVICE_ATTR(temp1_emergency_alarm
, S_IRUGO
, show_alarm
, NULL
, 15);
991 static SENSOR_DEVICE_ATTR(temp2_emergency_alarm
, S_IRUGO
, show_alarm
, NULL
, 13);
993 static struct attribute
*lm90_emergency_alarm_attributes
[] = {
994 &sensor_dev_attr_temp1_emergency_alarm
.dev_attr
.attr
,
995 &sensor_dev_attr_temp2_emergency_alarm
.dev_attr
.attr
,
999 static const struct attribute_group lm90_emergency_alarm_group
= {
1000 .attrs
= lm90_emergency_alarm_attributes
,
1004 * Additional attributes for devices with 3 temperature sensors
1006 static SENSOR_DEVICE_ATTR_2(temp3_input
, S_IRUGO
, show_temp11
, NULL
, 0, 5);
1007 static SENSOR_DEVICE_ATTR_2(temp3_min
, S_IWUSR
| S_IRUGO
, show_temp11
,
1009 static SENSOR_DEVICE_ATTR_2(temp3_max
, S_IWUSR
| S_IRUGO
, show_temp11
,
1011 static SENSOR_DEVICE_ATTR(temp3_crit
, S_IWUSR
| S_IRUGO
, show_temp8
,
1013 static SENSOR_DEVICE_ATTR(temp3_crit_hyst
, S_IRUGO
, show_temphyst
, NULL
, 6);
1014 static SENSOR_DEVICE_ATTR(temp3_emergency
, S_IWUSR
| S_IRUGO
, show_temp8
,
1016 static SENSOR_DEVICE_ATTR(temp3_emergency_hyst
, S_IRUGO
, show_temphyst
,
1019 static SENSOR_DEVICE_ATTR(temp3_crit_alarm
, S_IRUGO
, show_alarm
, NULL
, 9);
1020 static SENSOR_DEVICE_ATTR(temp3_fault
, S_IRUGO
, show_alarm
, NULL
, 10);
1021 static SENSOR_DEVICE_ATTR(temp3_min_alarm
, S_IRUGO
, show_alarm
, NULL
, 11);
1022 static SENSOR_DEVICE_ATTR(temp3_max_alarm
, S_IRUGO
, show_alarm
, NULL
, 12);
1023 static SENSOR_DEVICE_ATTR(temp3_emergency_alarm
, S_IRUGO
, show_alarm
, NULL
, 14);
1025 static struct attribute
*lm90_temp3_attributes
[] = {
1026 &sensor_dev_attr_temp3_input
.dev_attr
.attr
,
1027 &sensor_dev_attr_temp3_min
.dev_attr
.attr
,
1028 &sensor_dev_attr_temp3_max
.dev_attr
.attr
,
1029 &sensor_dev_attr_temp3_crit
.dev_attr
.attr
,
1030 &sensor_dev_attr_temp3_crit_hyst
.dev_attr
.attr
,
1031 &sensor_dev_attr_temp3_emergency
.dev_attr
.attr
,
1032 &sensor_dev_attr_temp3_emergency_hyst
.dev_attr
.attr
,
1034 &sensor_dev_attr_temp3_fault
.dev_attr
.attr
,
1035 &sensor_dev_attr_temp3_min_alarm
.dev_attr
.attr
,
1036 &sensor_dev_attr_temp3_max_alarm
.dev_attr
.attr
,
1037 &sensor_dev_attr_temp3_crit_alarm
.dev_attr
.attr
,
1038 &sensor_dev_attr_temp3_emergency_alarm
.dev_attr
.attr
,
1042 static const struct attribute_group lm90_temp3_group
= {
1043 .attrs
= lm90_temp3_attributes
,
1046 /* pec used for ADM1032 only */
1047 static ssize_t
show_pec(struct device
*dev
, struct device_attribute
*dummy
,
1050 struct i2c_client
*client
= to_i2c_client(dev
);
1051 return sprintf(buf
, "%d\n", !!(client
->flags
& I2C_CLIENT_PEC
));
1054 static ssize_t
set_pec(struct device
*dev
, struct device_attribute
*dummy
,
1055 const char *buf
, size_t count
)
1057 struct i2c_client
*client
= to_i2c_client(dev
);
1061 err
= strict_strtol(buf
, 10, &val
);
1067 client
->flags
&= ~I2C_CLIENT_PEC
;
1070 client
->flags
|= I2C_CLIENT_PEC
;
1079 static DEVICE_ATTR(pec
, S_IWUSR
| S_IRUGO
, show_pec
, set_pec
);
1085 /* Return 0 if detection is successful, -ENODEV otherwise */
1086 static int lm90_detect(struct i2c_client
*new_client
,
1087 struct i2c_board_info
*info
)
1089 struct i2c_adapter
*adapter
= new_client
->adapter
;
1090 int address
= new_client
->addr
;
1091 const char *name
= NULL
;
1092 int man_id
, chip_id
, reg_config1
, reg_convrate
;
1094 if (!i2c_check_functionality(adapter
, I2C_FUNC_SMBUS_BYTE_DATA
))
1097 /* detection and identification */
1098 if ((man_id
= i2c_smbus_read_byte_data(new_client
,
1099 LM90_REG_R_MAN_ID
)) < 0
1100 || (chip_id
= i2c_smbus_read_byte_data(new_client
,
1101 LM90_REG_R_CHIP_ID
)) < 0
1102 || (reg_config1
= i2c_smbus_read_byte_data(new_client
,
1103 LM90_REG_R_CONFIG1
)) < 0
1104 || (reg_convrate
= i2c_smbus_read_byte_data(new_client
,
1105 LM90_REG_R_CONVRATE
)) < 0)
1108 if ((address
== 0x4C || address
== 0x4D)
1109 && man_id
== 0x01) { /* National Semiconductor */
1112 reg_config2
= i2c_smbus_read_byte_data(new_client
,
1113 LM90_REG_R_CONFIG2
);
1114 if (reg_config2
< 0)
1117 if ((reg_config1
& 0x2A) == 0x00
1118 && (reg_config2
& 0xF8) == 0x00
1119 && reg_convrate
<= 0x09) {
1121 && (chip_id
& 0xF0) == 0x20) { /* LM90 */
1124 if ((chip_id
& 0xF0) == 0x30) { /* LM89/LM99 */
1126 dev_info(&adapter
->dev
,
1127 "Assuming LM99 chip at 0x%02x\n",
1129 dev_info(&adapter
->dev
,
1130 "If it is an LM89, instantiate it "
1131 "with the new_device sysfs "
1135 && (chip_id
& 0xF0) == 0x10) { /* LM86 */
1140 if ((address
== 0x4C || address
== 0x4D)
1141 && man_id
== 0x41) { /* Analog Devices */
1142 if ((chip_id
& 0xF0) == 0x40 /* ADM1032 */
1143 && (reg_config1
& 0x3F) == 0x00
1144 && reg_convrate
<= 0x0A) {
1146 /* The ADM1032 supports PEC, but only if combined
1147 transactions are not used. */
1148 if (i2c_check_functionality(adapter
,
1149 I2C_FUNC_SMBUS_BYTE
))
1150 info
->flags
|= I2C_CLIENT_PEC
;
1152 if (chip_id
== 0x51 /* ADT7461 */
1153 && (reg_config1
& 0x1B) == 0x00
1154 && reg_convrate
<= 0x0A) {
1158 if (man_id
== 0x4D) { /* Maxim */
1159 int reg_emerg
, reg_emerg2
, reg_status2
;
1162 * We read MAX6659_REG_R_REMOTE_EMERG twice, and re-read
1163 * LM90_REG_R_MAN_ID in between. If MAX6659_REG_R_REMOTE_EMERG
1164 * exists, both readings will reflect the same value. Otherwise,
1165 * the readings will be different.
1167 if ((reg_emerg
= i2c_smbus_read_byte_data(new_client
,
1168 MAX6659_REG_R_REMOTE_EMERG
)) < 0
1169 || i2c_smbus_read_byte_data(new_client
, LM90_REG_R_MAN_ID
) < 0
1170 || (reg_emerg2
= i2c_smbus_read_byte_data(new_client
,
1171 MAX6659_REG_R_REMOTE_EMERG
)) < 0
1172 || (reg_status2
= i2c_smbus_read_byte_data(new_client
,
1173 MAX6696_REG_R_STATUS2
)) < 0)
1177 * The MAX6657, MAX6658 and MAX6659 do NOT have a chip_id
1178 * register. Reading from that address will return the last
1179 * read value, which in our case is those of the man_id
1180 * register. Likewise, the config1 register seems to lack a
1181 * low nibble, so the value will be those of the previous
1182 * read, so in our case those of the man_id register.
1183 * MAX6659 has a third set of upper temperature limit registers.
1184 * Those registers also return values on MAX6657 and MAX6658,
1185 * thus the only way to detect MAX6659 is by its address.
1186 * For this reason it will be mis-detected as MAX6657 if its
1189 if (chip_id
== man_id
1190 && (address
== 0x4C || address
== 0x4D || address
== 0x4E)
1191 && (reg_config1
& 0x1F) == (man_id
& 0x0F)
1192 && reg_convrate
<= 0x09) {
1193 if (address
== 0x4C)
1199 * Even though MAX6695 and MAX6696 do not have a chip ID
1200 * register, reading it returns 0x01. Bit 4 of the config1
1201 * register is unused and should return zero when read. Bit 0 of
1202 * the status2 register is unused and should return zero when
1205 * MAX6695 and MAX6696 have an additional set of temperature
1206 * limit registers. We can detect those chips by checking if
1207 * one of those registers exists.
1210 && (reg_config1
& 0x10) == 0x00
1211 && (reg_status2
& 0x01) == 0x00
1212 && reg_emerg
== reg_emerg2
1213 && reg_convrate
<= 0x07) {
1217 * The chip_id register of the MAX6680 and MAX6681 holds the
1218 * revision of the chip. The lowest bit of the config1 register
1219 * is unused and should return zero when read, so should the
1220 * second to last bit of config1 (software reset).
1223 && (reg_config1
& 0x03) == 0x00
1224 && reg_convrate
<= 0x07) {
1228 * The chip_id register of the MAX6646/6647/6649 holds the
1229 * revision of the chip. The lowest 6 bits of the config1
1230 * register are unused and should return zero when read.
1233 && (reg_config1
& 0x3f) == 0x00
1234 && reg_convrate
<= 0x07) {
1239 && man_id
== 0x5C) { /* Winbond/Nuvoton */
1242 reg_config2
= i2c_smbus_read_byte_data(new_client
,
1243 LM90_REG_R_CONFIG2
);
1244 if (reg_config2
< 0)
1247 if ((reg_config1
& 0x2A) == 0x00
1248 && (reg_config2
& 0xF8) == 0x00) {
1249 if (chip_id
== 0x01 /* W83L771W/G */
1250 && reg_convrate
<= 0x09) {
1253 if ((chip_id
& 0xFE) == 0x10 /* W83L771AWG/ASG */
1254 && reg_convrate
<= 0x08) {
1260 if (!name
) { /* identification failed */
1261 dev_dbg(&adapter
->dev
,
1262 "Unsupported chip at 0x%02x (man_id=0x%02X, "
1263 "chip_id=0x%02X)\n", address
, man_id
, chip_id
);
1267 strlcpy(info
->type
, name
, I2C_NAME_SIZE
);
1272 static void lm90_remove_files(struct i2c_client
*client
, struct lm90_data
*data
)
1274 if (data
->flags
& LM90_HAVE_TEMP3
)
1275 sysfs_remove_group(&client
->dev
.kobj
, &lm90_temp3_group
);
1276 if (data
->flags
& LM90_HAVE_EMERGENCY_ALARM
)
1277 sysfs_remove_group(&client
->dev
.kobj
,
1278 &lm90_emergency_alarm_group
);
1279 if (data
->flags
& LM90_HAVE_EMERGENCY
)
1280 sysfs_remove_group(&client
->dev
.kobj
,
1281 &lm90_emergency_group
);
1282 if (data
->flags
& LM90_HAVE_OFFSET
)
1283 device_remove_file(&client
->dev
,
1284 &sensor_dev_attr_temp2_offset
.dev_attr
);
1285 device_remove_file(&client
->dev
, &dev_attr_pec
);
1286 sysfs_remove_group(&client
->dev
.kobj
, &lm90_group
);
1289 static void lm90_init_client(struct i2c_client
*client
)
1291 u8 config
, convrate
;
1292 struct lm90_data
*data
= i2c_get_clientdata(client
);
1294 if (lm90_read_reg(client
, LM90_REG_R_CONVRATE
, &convrate
) < 0) {
1295 dev_warn(&client
->dev
, "Failed to read convrate register!\n");
1296 convrate
= LM90_DEF_CONVRATE_RVAL
;
1298 data
->convrate_orig
= convrate
;
1301 * Start the conversions.
1303 lm90_set_convrate(client
, data
, 500); /* 500ms; 2Hz conversion rate */
1304 if (lm90_read_reg(client
, LM90_REG_R_CONFIG1
, &config
) < 0) {
1305 dev_warn(&client
->dev
, "Initialization failed!\n");
1308 data
->config_orig
= config
;
1310 /* Check Temperature Range Select */
1311 if (data
->kind
== adt7461
) {
1313 data
->flags
|= LM90_FLAG_ADT7461_EXT
;
1317 * Put MAX6680/MAX8881 into extended resolution (bit 0x10,
1318 * 0.125 degree resolution) and range (0x08, extend range
1319 * to -64 degree) mode for the remote temperature sensor.
1321 if (data
->kind
== max6680
)
1325 * Select external channel 0 for max6695/96
1327 if (data
->kind
== max6696
)
1330 config
&= 0xBF; /* run */
1331 if (config
!= data
->config_orig
) /* Only write if changed */
1332 i2c_smbus_write_byte_data(client
, LM90_REG_W_CONFIG1
, config
);
1335 static int lm90_probe(struct i2c_client
*new_client
,
1336 const struct i2c_device_id
*id
)
1338 struct i2c_adapter
*adapter
= to_i2c_adapter(new_client
->dev
.parent
);
1339 struct lm90_data
*data
;
1342 data
= kzalloc(sizeof(struct lm90_data
), GFP_KERNEL
);
1347 i2c_set_clientdata(new_client
, data
);
1348 mutex_init(&data
->update_lock
);
1350 /* Set the device type */
1351 data
->kind
= id
->driver_data
;
1352 if (data
->kind
== adm1032
) {
1353 if (!i2c_check_functionality(adapter
, I2C_FUNC_SMBUS_BYTE
))
1354 new_client
->flags
&= ~I2C_CLIENT_PEC
;
1357 /* Different devices have different alarm bits triggering the
1359 data
->alert_alarms
= lm90_params
[data
->kind
].alert_alarms
;
1361 /* Set chip capabilities */
1362 data
->flags
= lm90_params
[data
->kind
].flags
;
1364 /* Set maximum conversion rate */
1365 data
->max_convrate
= lm90_params
[data
->kind
].max_convrate
;
1367 /* Initialize the LM90 chip */
1368 lm90_init_client(new_client
);
1370 /* Register sysfs hooks */
1371 err
= sysfs_create_group(&new_client
->dev
.kobj
, &lm90_group
);
1374 if (new_client
->flags
& I2C_CLIENT_PEC
) {
1375 err
= device_create_file(&new_client
->dev
, &dev_attr_pec
);
1377 goto exit_remove_files
;
1379 if (data
->flags
& LM90_HAVE_OFFSET
) {
1380 err
= device_create_file(&new_client
->dev
,
1381 &sensor_dev_attr_temp2_offset
.dev_attr
);
1383 goto exit_remove_files
;
1385 if (data
->flags
& LM90_HAVE_EMERGENCY
) {
1386 err
= sysfs_create_group(&new_client
->dev
.kobj
,
1387 &lm90_emergency_group
);
1389 goto exit_remove_files
;
1391 if (data
->flags
& LM90_HAVE_EMERGENCY_ALARM
) {
1392 err
= sysfs_create_group(&new_client
->dev
.kobj
,
1393 &lm90_emergency_alarm_group
);
1395 goto exit_remove_files
;
1397 if (data
->flags
& LM90_HAVE_TEMP3
) {
1398 err
= sysfs_create_group(&new_client
->dev
.kobj
,
1401 goto exit_remove_files
;
1404 data
->hwmon_dev
= hwmon_device_register(&new_client
->dev
);
1405 if (IS_ERR(data
->hwmon_dev
)) {
1406 err
= PTR_ERR(data
->hwmon_dev
);
1407 goto exit_remove_files
;
1413 lm90_remove_files(new_client
, data
);
1420 static int lm90_remove(struct i2c_client
*client
)
1422 struct lm90_data
*data
= i2c_get_clientdata(client
);
1424 hwmon_device_unregister(data
->hwmon_dev
);
1425 lm90_remove_files(client
, data
);
1427 /* Restore initial configuration */
1428 i2c_smbus_write_byte_data(client
, LM90_REG_W_CONVRATE
,
1429 data
->convrate_orig
);
1430 i2c_smbus_write_byte_data(client
, LM90_REG_W_CONFIG1
,
1437 static void lm90_alert(struct i2c_client
*client
, unsigned int flag
)
1439 struct lm90_data
*data
= i2c_get_clientdata(client
);
1440 u8 config
, alarms
, alarms2
= 0;
1442 lm90_read_reg(client
, LM90_REG_R_STATUS
, &alarms
);
1444 if (data
->kind
== max6696
)
1445 lm90_read_reg(client
, MAX6696_REG_R_STATUS2
, &alarms2
);
1447 if ((alarms
& 0x7f) == 0 && (alarms2
& 0xfe) == 0) {
1448 dev_info(&client
->dev
, "Everything OK\n");
1451 dev_warn(&client
->dev
,
1452 "temp%d out of range, please check!\n", 1);
1454 dev_warn(&client
->dev
,
1455 "temp%d out of range, please check!\n", 2);
1457 dev_warn(&client
->dev
,
1458 "temp%d diode open, please check!\n", 2);
1461 dev_warn(&client
->dev
,
1462 "temp%d out of range, please check!\n", 3);
1464 /* Disable ALERT# output, because these chips don't implement
1465 SMBus alert correctly; they should only hold the alert line
1467 if ((data
->flags
& LM90_HAVE_BROKEN_ALERT
)
1468 && (alarms
& data
->alert_alarms
)) {
1469 dev_dbg(&client
->dev
, "Disabling ALERT#\n");
1470 lm90_read_reg(client
, LM90_REG_R_CONFIG1
, &config
);
1471 i2c_smbus_write_byte_data(client
, LM90_REG_W_CONFIG1
,
1477 static struct i2c_driver lm90_driver
= {
1478 .class = I2C_CLASS_HWMON
,
1482 .probe
= lm90_probe
,
1483 .remove
= lm90_remove
,
1484 .alert
= lm90_alert
,
1485 .id_table
= lm90_id
,
1486 .detect
= lm90_detect
,
1487 .address_list
= normal_i2c
,
1490 static int __init
sensors_lm90_init(void)
1492 return i2c_add_driver(&lm90_driver
);
1495 static void __exit
sensors_lm90_exit(void)
1497 i2c_del_driver(&lm90_driver
);
1500 MODULE_AUTHOR("Jean Delvare <khali@linux-fr.org>");
1501 MODULE_DESCRIPTION("LM90/ADM1032 driver");
1502 MODULE_LICENSE("GPL");
1504 module_init(sensors_lm90_init
);
1505 module_exit(sensors_lm90_exit
);