1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * lm90.c - Part of lm_sensors, Linux kernel modules for hardware
5 * Copyright (C) 2003-2010 Jean Delvare <jdelvare@suse.de>
7 * Based on the lm83 driver. The LM90 is a sensor chip made by National
8 * Semiconductor. It reports up to two temperatures (its own plus up to
9 * one external one) with a 0.125 deg resolution (1 deg for local
10 * temperature) and a 3-4 deg accuracy.
12 * This driver also supports the LM89 and LM99, two other sensor chips
13 * made by National Semiconductor. Both have an increased remote
14 * temperature measurement accuracy (1 degree), and the LM99
15 * additionally shifts remote temperatures (measured and limits) by 16
16 * degrees, which allows for higher temperatures measurement.
17 * Note that there is no way to differentiate between both chips.
18 * When device is auto-detected, the driver will assume an LM99.
20 * This driver also supports the LM86, another sensor chip made by
21 * National Semiconductor. It is exactly similar to the LM90 except it
22 * has a higher accuracy.
24 * This driver also supports the ADM1032, a sensor chip made by Analog
25 * Devices. That chip is similar to the LM90, with a few differences
26 * that are not handled by this driver. Among others, it has a higher
27 * accuracy than the LM90, much like the LM86 does.
29 * This driver also supports the MAX6657, MAX6658 and MAX6659 sensor
30 * chips made by Maxim. These chips are similar to the LM86.
31 * Note that there is no easy way to differentiate between the three
32 * variants. We use the device address to detect MAX6659, which will result
33 * in a detection as max6657 if it is on address 0x4c. The extra address
34 * and features of the MAX6659 are only supported if the chip is configured
35 * explicitly as max6659, or if its address is not 0x4c.
36 * These chips lack the remote temperature offset feature.
38 * This driver also supports the MAX6646, MAX6647, MAX6648, MAX6649 and
39 * MAX6692 chips made by Maxim. These are again similar to the LM86,
40 * but they use unsigned temperature values and can report temperatures
41 * from 0 to 145 degrees.
43 * This driver also supports the MAX6680 and MAX6681, two other sensor
44 * chips made by Maxim. These are quite similar to the other Maxim
45 * chips. The MAX6680 and MAX6681 only differ in the pinout so they can
46 * be treated identically.
48 * This driver also supports the MAX6695 and MAX6696, two other sensor
49 * chips made by Maxim. These are also quite similar to other Maxim
50 * chips, but support three temperature sensors instead of two. MAX6695
51 * and MAX6696 only differ in the pinout so they can be treated identically.
53 * This driver also supports ADT7461 and ADT7461A from Analog Devices as well as
54 * NCT1008 from ON Semiconductor. The chips are supported in both compatibility
55 * and extended mode. They are mostly compatible with LM90 except for a data
56 * format difference for the temperature value registers.
58 * This driver also supports the SA56004 from Philips. This device is
59 * pin-compatible with the LM86, the ED/EDP parts are also address-compatible.
61 * This driver also supports the G781 from GMT. This device is compatible
64 * This driver also supports TMP451 from Texas Instruments. This device is
65 * supported in both compatibility and extended mode. It's mostly compatible
66 * with ADT7461 except for local temperature low byte register and max
69 * Since the LM90 was the first chipset supported by this driver, most
70 * comments will refer to this chipset, but are actually general and
71 * concern all supported chipsets, unless mentioned otherwise.
74 #include <linux/module.h>
75 #include <linux/init.h>
76 #include <linux/slab.h>
77 #include <linux/jiffies.h>
78 #include <linux/i2c.h>
79 #include <linux/hwmon.h>
80 #include <linux/err.h>
81 #include <linux/mutex.h>
82 #include <linux/of_device.h>
83 #include <linux/sysfs.h>
84 #include <linux/interrupt.h>
85 #include <linux/regulator/consumer.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, ADT7461A, MAX6649,
92 * MAX6657, MAX6658, NCT1008 and W83L771 have address 0x4c.
93 * ADM1032-2, ADT7461-2, ADT7461A-2, LM89-1, LM99-1, MAX6646, and NCT1008D
95 * MAX6647 has address 0x4e.
96 * MAX6659 can have address 0x4c, 0x4d or 0x4e.
97 * MAX6680 and MAX6681 can have address 0x18, 0x19, 0x1a, 0x29, 0x2a, 0x2b,
99 * SA56004 can have address 0x48 through 0x4F.
102 static const unsigned short normal_i2c
[] = {
103 0x18, 0x19, 0x1a, 0x29, 0x2a, 0x2b, 0x48, 0x49, 0x4a, 0x4b, 0x4c,
104 0x4d, 0x4e, 0x4f, I2C_CLIENT_END
};
106 enum chips
{ lm90
, adm1032
, lm99
, lm86
, max6657
, max6659
, adt7461
, max6680
,
107 max6646
, w83l771
, max6696
, sa56004
, g781
, tmp451
};
113 #define LM90_REG_R_MAN_ID 0xFE
114 #define LM90_REG_R_CHIP_ID 0xFF
115 #define LM90_REG_R_CONFIG1 0x03
116 #define LM90_REG_W_CONFIG1 0x09
117 #define LM90_REG_R_CONFIG2 0xBF
118 #define LM90_REG_W_CONFIG2 0xBF
119 #define LM90_REG_R_CONVRATE 0x04
120 #define LM90_REG_W_CONVRATE 0x0A
121 #define LM90_REG_R_STATUS 0x02
122 #define LM90_REG_R_LOCAL_TEMP 0x00
123 #define LM90_REG_R_LOCAL_HIGH 0x05
124 #define LM90_REG_W_LOCAL_HIGH 0x0B
125 #define LM90_REG_R_LOCAL_LOW 0x06
126 #define LM90_REG_W_LOCAL_LOW 0x0C
127 #define LM90_REG_R_LOCAL_CRIT 0x20
128 #define LM90_REG_W_LOCAL_CRIT 0x20
129 #define LM90_REG_R_REMOTE_TEMPH 0x01
130 #define LM90_REG_R_REMOTE_TEMPL 0x10
131 #define LM90_REG_R_REMOTE_OFFSH 0x11
132 #define LM90_REG_W_REMOTE_OFFSH 0x11
133 #define LM90_REG_R_REMOTE_OFFSL 0x12
134 #define LM90_REG_W_REMOTE_OFFSL 0x12
135 #define LM90_REG_R_REMOTE_HIGHH 0x07
136 #define LM90_REG_W_REMOTE_HIGHH 0x0D
137 #define LM90_REG_R_REMOTE_HIGHL 0x13
138 #define LM90_REG_W_REMOTE_HIGHL 0x13
139 #define LM90_REG_R_REMOTE_LOWH 0x08
140 #define LM90_REG_W_REMOTE_LOWH 0x0E
141 #define LM90_REG_R_REMOTE_LOWL 0x14
142 #define LM90_REG_W_REMOTE_LOWL 0x14
143 #define LM90_REG_R_REMOTE_CRIT 0x19
144 #define LM90_REG_W_REMOTE_CRIT 0x19
145 #define LM90_REG_R_TCRIT_HYST 0x21
146 #define LM90_REG_W_TCRIT_HYST 0x21
148 /* MAX6646/6647/6649/6657/6658/6659/6695/6696 registers */
150 #define MAX6657_REG_R_LOCAL_TEMPL 0x11
151 #define MAX6696_REG_R_STATUS2 0x12
152 #define MAX6659_REG_R_REMOTE_EMERG 0x16
153 #define MAX6659_REG_W_REMOTE_EMERG 0x16
154 #define MAX6659_REG_R_LOCAL_EMERG 0x17
155 #define MAX6659_REG_W_LOCAL_EMERG 0x17
157 /* SA56004 registers */
159 #define SA56004_REG_R_LOCAL_TEMPL 0x22
161 #define LM90_MAX_CONVRATE_MS 16000 /* Maximum conversion rate in ms */
163 /* TMP451 registers */
164 #define TMP451_REG_R_LOCAL_TEMPL 0x15
169 #define LM90_FLAG_ADT7461_EXT (1 << 0) /* ADT7461 extended mode */
170 /* Device features */
171 #define LM90_HAVE_OFFSET (1 << 1) /* temperature offset register */
172 #define LM90_HAVE_REM_LIMIT_EXT (1 << 3) /* extended remote limit */
173 #define LM90_HAVE_EMERGENCY (1 << 4) /* 3rd upper (emergency) limit */
174 #define LM90_HAVE_EMERGENCY_ALARM (1 << 5)/* emergency alarm */
175 #define LM90_HAVE_TEMP3 (1 << 6) /* 3rd temperature sensor */
176 #define LM90_HAVE_BROKEN_ALERT (1 << 7) /* Broken alert */
177 #define LM90_PAUSE_FOR_CONFIG (1 << 8) /* Pause conversion for config */
180 #define LM90_STATUS_LTHRM (1 << 0) /* local THERM limit tripped */
181 #define LM90_STATUS_RTHRM (1 << 1) /* remote THERM limit tripped */
182 #define LM90_STATUS_ROPEN (1 << 2) /* remote is an open circuit */
183 #define LM90_STATUS_RLOW (1 << 3) /* remote low temp limit tripped */
184 #define LM90_STATUS_RHIGH (1 << 4) /* remote high temp limit tripped */
185 #define LM90_STATUS_LLOW (1 << 5) /* local low temp limit tripped */
186 #define LM90_STATUS_LHIGH (1 << 6) /* local high temp limit tripped */
188 #define MAX6696_STATUS2_R2THRM (1 << 1) /* remote2 THERM limit tripped */
189 #define MAX6696_STATUS2_R2OPEN (1 << 2) /* remote2 is an open circuit */
190 #define MAX6696_STATUS2_R2LOW (1 << 3) /* remote2 low temp limit tripped */
191 #define MAX6696_STATUS2_R2HIGH (1 << 4) /* remote2 high temp limit tripped */
192 #define MAX6696_STATUS2_ROT2 (1 << 5) /* remote emergency limit tripped */
193 #define MAX6696_STATUS2_R2OT2 (1 << 6) /* remote2 emergency limit tripped */
194 #define MAX6696_STATUS2_LOT2 (1 << 7) /* local emergency limit tripped */
197 * Driver data (common to all clients)
200 static const struct i2c_device_id lm90_id
[] = {
201 { "adm1032", adm1032
},
202 { "adt7461", adt7461
},
203 { "adt7461a", adt7461
},
209 { "max6646", max6646
},
210 { "max6647", max6646
},
211 { "max6649", max6646
},
212 { "max6657", max6657
},
213 { "max6658", max6657
},
214 { "max6659", max6659
},
215 { "max6680", max6680
},
216 { "max6681", max6680
},
217 { "max6695", max6696
},
218 { "max6696", max6696
},
219 { "nct1008", adt7461
},
220 { "w83l771", w83l771
},
221 { "sa56004", sa56004
},
222 { "tmp451", tmp451
},
225 MODULE_DEVICE_TABLE(i2c
, lm90_id
);
227 static const struct of_device_id __maybe_unused lm90_of_match
[] = {
229 .compatible
= "adi,adm1032",
230 .data
= (void *)adm1032
233 .compatible
= "adi,adt7461",
234 .data
= (void *)adt7461
237 .compatible
= "adi,adt7461a",
238 .data
= (void *)adt7461
241 .compatible
= "gmt,g781",
245 .compatible
= "national,lm90",
249 .compatible
= "national,lm86",
253 .compatible
= "national,lm89",
257 .compatible
= "national,lm99",
261 .compatible
= "dallas,max6646",
262 .data
= (void *)max6646
265 .compatible
= "dallas,max6647",
266 .data
= (void *)max6646
269 .compatible
= "dallas,max6649",
270 .data
= (void *)max6646
273 .compatible
= "dallas,max6657",
274 .data
= (void *)max6657
277 .compatible
= "dallas,max6658",
278 .data
= (void *)max6657
281 .compatible
= "dallas,max6659",
282 .data
= (void *)max6659
285 .compatible
= "dallas,max6680",
286 .data
= (void *)max6680
289 .compatible
= "dallas,max6681",
290 .data
= (void *)max6680
293 .compatible
= "dallas,max6695",
294 .data
= (void *)max6696
297 .compatible
= "dallas,max6696",
298 .data
= (void *)max6696
301 .compatible
= "onnn,nct1008",
302 .data
= (void *)adt7461
305 .compatible
= "winbond,w83l771",
306 .data
= (void *)w83l771
309 .compatible
= "nxp,sa56004",
310 .data
= (void *)sa56004
313 .compatible
= "ti,tmp451",
314 .data
= (void *)tmp451
318 MODULE_DEVICE_TABLE(of
, lm90_of_match
);
321 * chip type specific parameters
324 u32 flags
; /* Capabilities */
325 u16 alert_alarms
; /* Which alarm bits trigger ALERT# */
326 /* Upper 8 bits for max6695/96 */
327 u8 max_convrate
; /* Maximum conversion rate register value */
328 u8 reg_local_ext
; /* Extended local temp register (optional) */
331 static const struct lm90_params lm90_params
[] = {
333 .flags
= LM90_HAVE_OFFSET
| LM90_HAVE_REM_LIMIT_EXT
334 | LM90_HAVE_BROKEN_ALERT
,
335 .alert_alarms
= 0x7c,
339 .flags
= LM90_HAVE_OFFSET
| LM90_HAVE_REM_LIMIT_EXT
340 | LM90_HAVE_BROKEN_ALERT
,
341 .alert_alarms
= 0x7c,
345 .flags
= LM90_HAVE_OFFSET
| LM90_HAVE_REM_LIMIT_EXT
346 | LM90_HAVE_BROKEN_ALERT
,
347 .alert_alarms
= 0x7c,
351 .flags
= LM90_HAVE_OFFSET
| LM90_HAVE_REM_LIMIT_EXT
,
352 .alert_alarms
= 0x7b,
356 .flags
= LM90_HAVE_OFFSET
| LM90_HAVE_REM_LIMIT_EXT
,
357 .alert_alarms
= 0x7b,
361 .flags
= LM90_HAVE_OFFSET
| LM90_HAVE_REM_LIMIT_EXT
,
362 .alert_alarms
= 0x7b,
366 .alert_alarms
= 0x7c,
368 .reg_local_ext
= MAX6657_REG_R_LOCAL_TEMPL
,
371 .flags
= LM90_PAUSE_FOR_CONFIG
,
372 .alert_alarms
= 0x7c,
374 .reg_local_ext
= MAX6657_REG_R_LOCAL_TEMPL
,
377 .flags
= LM90_HAVE_EMERGENCY
,
378 .alert_alarms
= 0x7c,
380 .reg_local_ext
= MAX6657_REG_R_LOCAL_TEMPL
,
383 .flags
= LM90_HAVE_OFFSET
,
384 .alert_alarms
= 0x7c,
388 .flags
= LM90_HAVE_EMERGENCY
389 | LM90_HAVE_EMERGENCY_ALARM
| LM90_HAVE_TEMP3
,
390 .alert_alarms
= 0x1c7c,
392 .reg_local_ext
= MAX6657_REG_R_LOCAL_TEMPL
,
395 .flags
= LM90_HAVE_OFFSET
| LM90_HAVE_REM_LIMIT_EXT
,
396 .alert_alarms
= 0x7c,
400 .flags
= LM90_HAVE_OFFSET
| LM90_HAVE_REM_LIMIT_EXT
,
401 .alert_alarms
= 0x7b,
403 .reg_local_ext
= SA56004_REG_R_LOCAL_TEMPL
,
406 .flags
= LM90_HAVE_OFFSET
| LM90_HAVE_REM_LIMIT_EXT
407 | LM90_HAVE_BROKEN_ALERT
,
408 .alert_alarms
= 0x7c,
410 .reg_local_ext
= TMP451_REG_R_LOCAL_TEMPL
,
415 * TEMP8 register index
417 enum lm90_temp8_reg_index
{
422 LOCAL_EMERG
, /* max6659 and max6695/96 */
423 REMOTE_EMERG
, /* max6659 and max6695/96 */
424 REMOTE2_CRIT
, /* max6695/96 only */
425 REMOTE2_EMERG
, /* max6695/96 only */
430 * TEMP11 register index
432 enum lm90_temp11_reg_index
{
436 REMOTE_OFFSET
, /* except max6646, max6657/58/59, and max6695/96 */
438 REMOTE2_TEMP
, /* max6695/96 only */
439 REMOTE2_LOW
, /* max6695/96 only */
440 REMOTE2_HIGH
, /* max6695/96 only */
445 * Client data (each client gets its own)
449 struct i2c_client
*client
;
450 u32 channel_config
[4];
451 struct hwmon_channel_info temp_info
;
452 const struct hwmon_channel_info
*info
[3];
453 struct hwmon_chip_info chip
;
454 struct mutex update_lock
;
455 bool valid
; /* true if register values are valid */
456 unsigned long last_updated
; /* in jiffies */
460 unsigned int update_interval
; /* in milliseconds */
462 u8 config
; /* Current configuration register value */
463 u8 config_orig
; /* Original configuration register value */
464 u8 convrate_orig
; /* Original conversion rate register value */
465 u16 alert_alarms
; /* Which alarm bits trigger ALERT# */
466 /* Upper 8 bits for max6695/96 */
467 u8 max_convrate
; /* Maximum conversion rate */
468 u8 reg_local_ext
; /* local extension register offset */
470 /* registers values */
471 s8 temp8
[TEMP8_REG_NUM
];
472 s16 temp11
[TEMP11_REG_NUM
];
474 u16 alarms
; /* bitvector (upper 8 bits for max6695/96) */
482 * The ADM1032 supports PEC but not on write byte transactions, so we need
483 * to explicitly ask for a transaction without PEC.
485 static inline s32
adm1032_write_byte(struct i2c_client
*client
, u8 value
)
487 return i2c_smbus_xfer(client
->adapter
, client
->addr
,
488 client
->flags
& ~I2C_CLIENT_PEC
,
489 I2C_SMBUS_WRITE
, value
, I2C_SMBUS_BYTE
, NULL
);
493 * It is assumed that client->update_lock is held (unless we are in
494 * detection or initialization steps). This matters when PEC is enabled,
495 * because we don't want the address pointer to change between the write
496 * byte and the read byte transactions.
498 static int lm90_read_reg(struct i2c_client
*client
, u8 reg
)
502 if (client
->flags
& I2C_CLIENT_PEC
) {
503 err
= adm1032_write_byte(client
, reg
);
505 err
= i2c_smbus_read_byte(client
);
507 err
= i2c_smbus_read_byte_data(client
, reg
);
512 static int lm90_read16(struct i2c_client
*client
, u8 regh
, u8 regl
)
517 * There is a trick here. We have to read two registers to have the
518 * sensor temperature, but we have to beware a conversion could occur
519 * between the readings. The datasheet says we should either use
520 * the one-shot conversion register, which we don't want to do
521 * (disables hardware monitoring) or monitor the busy bit, which is
522 * impossible (we can't read the values and monitor that bit at the
523 * exact same time). So the solution used here is to read the high
524 * byte once, then the low byte, then the high byte again. If the new
525 * high byte matches the old one, then we have a valid reading. Else
526 * we have to read the low byte again, and now we believe we have a
529 oldh
= lm90_read_reg(client
, regh
);
532 l
= lm90_read_reg(client
, regl
);
535 newh
= lm90_read_reg(client
, regh
);
539 l
= lm90_read_reg(client
, regl
);
543 return (newh
<< 8) | l
;
546 static int lm90_update_confreg(struct lm90_data
*data
, u8 config
)
548 if (data
->config
!= config
) {
551 err
= i2c_smbus_write_byte_data(data
->client
,
556 data
->config
= config
;
562 * client->update_lock must be held when calling this function (unless we are
563 * in detection or initialization steps), and while a remote channel other
564 * than channel 0 is selected. Also, calling code must make sure to re-select
565 * external channel 0 before releasing the lock. This is necessary because
566 * various registers have different meanings as a result of selecting a
567 * non-default remote channel.
569 static int lm90_select_remote_channel(struct lm90_data
*data
, int channel
)
573 if (data
->kind
== max6696
) {
574 u8 config
= data
->config
& ~0x08;
578 err
= lm90_update_confreg(data
, config
);
583 static int lm90_write_convrate(struct lm90_data
*data
, int val
)
585 u8 config
= data
->config
;
588 /* Save config and pause conversion */
589 if (data
->flags
& LM90_PAUSE_FOR_CONFIG
) {
590 err
= lm90_update_confreg(data
, config
| 0x40);
596 err
= i2c_smbus_write_byte_data(data
->client
, LM90_REG_W_CONVRATE
, val
);
598 /* Revert change to config */
599 lm90_update_confreg(data
, config
);
605 * Set conversion rate.
606 * client->update_lock must be held when calling this function (unless we are
607 * in detection or initialization steps).
609 static int lm90_set_convrate(struct i2c_client
*client
, struct lm90_data
*data
,
610 unsigned int interval
)
612 unsigned int update_interval
;
615 /* Shift calculations to avoid rounding errors */
618 /* find the nearest update rate */
619 for (i
= 0, update_interval
= LM90_MAX_CONVRATE_MS
<< 6;
620 i
< data
->max_convrate
; i
++, update_interval
>>= 1)
621 if (interval
>= update_interval
* 3 / 4)
624 err
= lm90_write_convrate(data
, i
);
625 data
->update_interval
= DIV_ROUND_CLOSEST(update_interval
, 64);
629 static int lm90_update_limits(struct device
*dev
)
631 struct lm90_data
*data
= dev_get_drvdata(dev
);
632 struct i2c_client
*client
= data
->client
;
635 val
= lm90_read_reg(client
, LM90_REG_R_LOCAL_CRIT
);
638 data
->temp8
[LOCAL_CRIT
] = val
;
640 val
= lm90_read_reg(client
, LM90_REG_R_REMOTE_CRIT
);
643 data
->temp8
[REMOTE_CRIT
] = val
;
645 val
= lm90_read_reg(client
, LM90_REG_R_TCRIT_HYST
);
648 data
->temp_hyst
= val
;
650 val
= lm90_read_reg(client
, LM90_REG_R_REMOTE_LOWH
);
653 data
->temp11
[REMOTE_LOW
] = val
<< 8;
655 if (data
->flags
& LM90_HAVE_REM_LIMIT_EXT
) {
656 val
= lm90_read_reg(client
, LM90_REG_R_REMOTE_LOWL
);
659 data
->temp11
[REMOTE_LOW
] |= val
;
662 val
= lm90_read_reg(client
, LM90_REG_R_REMOTE_HIGHH
);
665 data
->temp11
[REMOTE_HIGH
] = val
<< 8;
667 if (data
->flags
& LM90_HAVE_REM_LIMIT_EXT
) {
668 val
= lm90_read_reg(client
, LM90_REG_R_REMOTE_HIGHL
);
671 data
->temp11
[REMOTE_HIGH
] |= val
;
674 if (data
->flags
& LM90_HAVE_OFFSET
) {
675 val
= lm90_read16(client
, LM90_REG_R_REMOTE_OFFSH
,
676 LM90_REG_R_REMOTE_OFFSL
);
679 data
->temp11
[REMOTE_OFFSET
] = val
;
682 if (data
->flags
& LM90_HAVE_EMERGENCY
) {
683 val
= lm90_read_reg(client
, MAX6659_REG_R_LOCAL_EMERG
);
686 data
->temp8
[LOCAL_EMERG
] = val
;
688 val
= lm90_read_reg(client
, MAX6659_REG_R_REMOTE_EMERG
);
691 data
->temp8
[REMOTE_EMERG
] = val
;
694 if (data
->kind
== max6696
) {
695 val
= lm90_select_remote_channel(data
, 1);
699 val
= lm90_read_reg(client
, LM90_REG_R_REMOTE_CRIT
);
702 data
->temp8
[REMOTE2_CRIT
] = val
;
704 val
= lm90_read_reg(client
, MAX6659_REG_R_REMOTE_EMERG
);
707 data
->temp8
[REMOTE2_EMERG
] = val
;
709 val
= lm90_read_reg(client
, LM90_REG_R_REMOTE_LOWH
);
712 data
->temp11
[REMOTE2_LOW
] = val
<< 8;
714 val
= lm90_read_reg(client
, LM90_REG_R_REMOTE_HIGHH
);
717 data
->temp11
[REMOTE2_HIGH
] = val
<< 8;
719 lm90_select_remote_channel(data
, 0);
725 static int lm90_update_device(struct device
*dev
)
727 struct lm90_data
*data
= dev_get_drvdata(dev
);
728 struct i2c_client
*client
= data
->client
;
729 unsigned long next_update
;
733 val
= lm90_update_limits(dev
);
738 next_update
= data
->last_updated
+
739 msecs_to_jiffies(data
->update_interval
);
740 if (time_after(jiffies
, next_update
) || !data
->valid
) {
741 dev_dbg(&client
->dev
, "Updating lm90 data.\n");
745 val
= lm90_read_reg(client
, LM90_REG_R_LOCAL_LOW
);
748 data
->temp8
[LOCAL_LOW
] = val
;
750 val
= lm90_read_reg(client
, LM90_REG_R_LOCAL_HIGH
);
753 data
->temp8
[LOCAL_HIGH
] = val
;
755 if (data
->reg_local_ext
) {
756 val
= lm90_read16(client
, LM90_REG_R_LOCAL_TEMP
,
757 data
->reg_local_ext
);
760 data
->temp11
[LOCAL_TEMP
] = val
;
762 val
= lm90_read_reg(client
, LM90_REG_R_LOCAL_TEMP
);
765 data
->temp11
[LOCAL_TEMP
] = val
<< 8;
767 val
= lm90_read16(client
, LM90_REG_R_REMOTE_TEMPH
,
768 LM90_REG_R_REMOTE_TEMPL
);
771 data
->temp11
[REMOTE_TEMP
] = val
;
773 val
= lm90_read_reg(client
, LM90_REG_R_STATUS
);
776 data
->alarms
= val
; /* lower 8 bit of alarms */
778 if (data
->kind
== max6696
) {
779 val
= lm90_select_remote_channel(data
, 1);
783 val
= lm90_read16(client
, LM90_REG_R_REMOTE_TEMPH
,
784 LM90_REG_R_REMOTE_TEMPL
);
786 lm90_select_remote_channel(data
, 0);
789 data
->temp11
[REMOTE2_TEMP
] = val
;
791 lm90_select_remote_channel(data
, 0);
793 val
= lm90_read_reg(client
, MAX6696_REG_R_STATUS2
);
796 data
->alarms
|= val
<< 8;
800 * Re-enable ALERT# output if it was originally enabled and
801 * relevant alarms are all clear
803 if (!(data
->config_orig
& 0x80) &&
804 !(data
->alarms
& data
->alert_alarms
)) {
805 if (data
->config
& 0x80) {
806 dev_dbg(&client
->dev
, "Re-enabling ALERT#\n");
807 lm90_update_confreg(data
, data
->config
& ~0x80);
811 data
->last_updated
= jiffies
;
820 * For local temperatures and limits, critical limits and the hysteresis
821 * value, the LM90 uses signed 8-bit values with LSB = 1 degree Celsius.
822 * For remote temperatures and limits, it uses signed 11-bit values with
823 * LSB = 0.125 degree Celsius, left-justified in 16-bit registers. Some
824 * Maxim chips use unsigned values.
827 static inline int temp_from_s8(s8 val
)
832 static inline int temp_from_u8(u8 val
)
837 static inline int temp_from_s16(s16 val
)
839 return val
/ 32 * 125;
842 static inline int temp_from_u16(u16 val
)
844 return val
/ 32 * 125;
847 static s8
temp_to_s8(long val
)
854 return (val
- 500) / 1000;
855 return (val
+ 500) / 1000;
858 static u8
temp_to_u8(long val
)
864 return (val
+ 500) / 1000;
867 static s16
temp_to_s16(long val
)
874 return (val
- 62) / 125 * 32;
875 return (val
+ 62) / 125 * 32;
878 static u8
hyst_to_reg(long val
)
884 return (val
+ 500) / 1000;
888 * ADT7461 in compatibility mode is almost identical to LM90 except that
889 * attempts to write values that are outside the range 0 < temp < 127 are
890 * treated as the boundary value.
892 * ADT7461 in "extended mode" operation uses unsigned integers offset by
893 * 64 (e.g., 0 -> -64 degC). The range is restricted to -64..191 degC.
895 static inline int temp_from_u8_adt7461(struct lm90_data
*data
, u8 val
)
897 if (data
->flags
& LM90_FLAG_ADT7461_EXT
)
898 return (val
- 64) * 1000;
899 return temp_from_s8(val
);
902 static inline int temp_from_u16_adt7461(struct lm90_data
*data
, u16 val
)
904 if (data
->flags
& LM90_FLAG_ADT7461_EXT
)
905 return (val
- 0x4000) / 64 * 250;
906 return temp_from_s16(val
);
909 static u8
temp_to_u8_adt7461(struct lm90_data
*data
, long val
)
911 if (data
->flags
& LM90_FLAG_ADT7461_EXT
) {
916 return (val
+ 500 + 64000) / 1000;
922 return (val
+ 500) / 1000;
925 static u16
temp_to_u16_adt7461(struct lm90_data
*data
, long val
)
927 if (data
->flags
& LM90_FLAG_ADT7461_EXT
) {
932 return (val
+ 64000 + 125) / 250 * 64;
938 return (val
+ 125) / 250 * 64;
941 /* pec used for ADM1032 only */
942 static ssize_t
pec_show(struct device
*dev
, struct device_attribute
*dummy
,
945 struct i2c_client
*client
= to_i2c_client(dev
);
947 return sprintf(buf
, "%d\n", !!(client
->flags
& I2C_CLIENT_PEC
));
950 static ssize_t
pec_store(struct device
*dev
, struct device_attribute
*dummy
,
951 const char *buf
, size_t count
)
953 struct i2c_client
*client
= to_i2c_client(dev
);
957 err
= kstrtol(buf
, 10, &val
);
963 client
->flags
&= ~I2C_CLIENT_PEC
;
966 client
->flags
|= I2C_CLIENT_PEC
;
975 static DEVICE_ATTR_RW(pec
);
977 static int lm90_get_temp11(struct lm90_data
*data
, int index
)
979 s16 temp11
= data
->temp11
[index
];
982 if (data
->kind
== adt7461
|| data
->kind
== tmp451
)
983 temp
= temp_from_u16_adt7461(data
, temp11
);
984 else if (data
->kind
== max6646
)
985 temp
= temp_from_u16(temp11
);
987 temp
= temp_from_s16(temp11
);
989 /* +16 degrees offset for temp2 for the LM99 */
990 if (data
->kind
== lm99
&& index
<= 2)
996 static int lm90_set_temp11(struct lm90_data
*data
, int index
, long val
)
1002 [REMOTE_LOW
] = { LM90_REG_W_REMOTE_LOWH
, LM90_REG_W_REMOTE_LOWL
},
1003 [REMOTE_HIGH
] = { LM90_REG_W_REMOTE_HIGHH
, LM90_REG_W_REMOTE_HIGHL
},
1004 [REMOTE_OFFSET
] = { LM90_REG_W_REMOTE_OFFSH
, LM90_REG_W_REMOTE_OFFSL
},
1005 [REMOTE2_LOW
] = { LM90_REG_W_REMOTE_LOWH
, LM90_REG_W_REMOTE_LOWL
},
1006 [REMOTE2_HIGH
] = { LM90_REG_W_REMOTE_HIGHH
, LM90_REG_W_REMOTE_HIGHL
}
1008 struct i2c_client
*client
= data
->client
;
1009 struct reg
*regp
= ®
[index
];
1012 /* +16 degrees offset for temp2 for the LM99 */
1013 if (data
->kind
== lm99
&& index
<= 2)
1016 if (data
->kind
== adt7461
|| data
->kind
== tmp451
)
1017 data
->temp11
[index
] = temp_to_u16_adt7461(data
, val
);
1018 else if (data
->kind
== max6646
)
1019 data
->temp11
[index
] = temp_to_u8(val
) << 8;
1020 else if (data
->flags
& LM90_HAVE_REM_LIMIT_EXT
)
1021 data
->temp11
[index
] = temp_to_s16(val
);
1023 data
->temp11
[index
] = temp_to_s8(val
) << 8;
1025 lm90_select_remote_channel(data
, index
>= 3);
1026 err
= i2c_smbus_write_byte_data(client
, regp
->high
,
1027 data
->temp11
[index
] >> 8);
1030 if (data
->flags
& LM90_HAVE_REM_LIMIT_EXT
)
1031 err
= i2c_smbus_write_byte_data(client
, regp
->low
,
1032 data
->temp11
[index
] & 0xff);
1034 lm90_select_remote_channel(data
, 0);
1038 static int lm90_get_temp8(struct lm90_data
*data
, int index
)
1040 s8 temp8
= data
->temp8
[index
];
1043 if (data
->kind
== adt7461
|| data
->kind
== tmp451
)
1044 temp
= temp_from_u8_adt7461(data
, temp8
);
1045 else if (data
->kind
== max6646
)
1046 temp
= temp_from_u8(temp8
);
1048 temp
= temp_from_s8(temp8
);
1050 /* +16 degrees offset for temp2 for the LM99 */
1051 if (data
->kind
== lm99
&& index
== 3)
1057 static int lm90_set_temp8(struct lm90_data
*data
, int index
, long val
)
1059 static const u8 reg
[TEMP8_REG_NUM
] = {
1060 LM90_REG_W_LOCAL_LOW
,
1061 LM90_REG_W_LOCAL_HIGH
,
1062 LM90_REG_W_LOCAL_CRIT
,
1063 LM90_REG_W_REMOTE_CRIT
,
1064 MAX6659_REG_W_LOCAL_EMERG
,
1065 MAX6659_REG_W_REMOTE_EMERG
,
1066 LM90_REG_W_REMOTE_CRIT
,
1067 MAX6659_REG_W_REMOTE_EMERG
,
1069 struct i2c_client
*client
= data
->client
;
1072 /* +16 degrees offset for temp2 for the LM99 */
1073 if (data
->kind
== lm99
&& index
== 3)
1076 if (data
->kind
== adt7461
|| data
->kind
== tmp451
)
1077 data
->temp8
[index
] = temp_to_u8_adt7461(data
, val
);
1078 else if (data
->kind
== max6646
)
1079 data
->temp8
[index
] = temp_to_u8(val
);
1081 data
->temp8
[index
] = temp_to_s8(val
);
1083 lm90_select_remote_channel(data
, index
>= 6);
1084 err
= i2c_smbus_write_byte_data(client
, reg
[index
], data
->temp8
[index
]);
1085 lm90_select_remote_channel(data
, 0);
1090 static int lm90_get_temphyst(struct lm90_data
*data
, int index
)
1094 if (data
->kind
== adt7461
|| data
->kind
== tmp451
)
1095 temp
= temp_from_u8_adt7461(data
, data
->temp8
[index
]);
1096 else if (data
->kind
== max6646
)
1097 temp
= temp_from_u8(data
->temp8
[index
]);
1099 temp
= temp_from_s8(data
->temp8
[index
]);
1101 /* +16 degrees offset for temp2 for the LM99 */
1102 if (data
->kind
== lm99
&& index
== 3)
1105 return temp
- temp_from_s8(data
->temp_hyst
);
1108 static int lm90_set_temphyst(struct lm90_data
*data
, long val
)
1110 struct i2c_client
*client
= data
->client
;
1114 if (data
->kind
== adt7461
|| data
->kind
== tmp451
)
1115 temp
= temp_from_u8_adt7461(data
, data
->temp8
[LOCAL_CRIT
]);
1116 else if (data
->kind
== max6646
)
1117 temp
= temp_from_u8(data
->temp8
[LOCAL_CRIT
]);
1119 temp
= temp_from_s8(data
->temp8
[LOCAL_CRIT
]);
1121 data
->temp_hyst
= hyst_to_reg(temp
- val
);
1122 err
= i2c_smbus_write_byte_data(client
, LM90_REG_W_TCRIT_HYST
,
1127 static const u8 lm90_temp_index
[3] = {
1128 LOCAL_TEMP
, REMOTE_TEMP
, REMOTE2_TEMP
1131 static const u8 lm90_temp_min_index
[3] = {
1132 LOCAL_LOW
, REMOTE_LOW
, REMOTE2_LOW
1135 static const u8 lm90_temp_max_index
[3] = {
1136 LOCAL_HIGH
, REMOTE_HIGH
, REMOTE2_HIGH
1139 static const u8 lm90_temp_crit_index
[3] = {
1140 LOCAL_CRIT
, REMOTE_CRIT
, REMOTE2_CRIT
1143 static const u8 lm90_temp_emerg_index
[3] = {
1144 LOCAL_EMERG
, REMOTE_EMERG
, REMOTE2_EMERG
1147 static const u8 lm90_min_alarm_bits
[3] = { 5, 3, 11 };
1148 static const u8 lm90_max_alarm_bits
[3] = { 6, 4, 12 };
1149 static const u8 lm90_crit_alarm_bits
[3] = { 0, 1, 9 };
1150 static const u8 lm90_emergency_alarm_bits
[3] = { 15, 13, 14 };
1151 static const u8 lm90_fault_bits
[3] = { 0, 2, 10 };
1153 static int lm90_temp_read(struct device
*dev
, u32 attr
, int channel
, long *val
)
1155 struct lm90_data
*data
= dev_get_drvdata(dev
);
1158 mutex_lock(&data
->update_lock
);
1159 err
= lm90_update_device(dev
);
1160 mutex_unlock(&data
->update_lock
);
1165 case hwmon_temp_input
:
1166 *val
= lm90_get_temp11(data
, lm90_temp_index
[channel
]);
1168 case hwmon_temp_min_alarm
:
1169 *val
= (data
->alarms
>> lm90_min_alarm_bits
[channel
]) & 1;
1171 case hwmon_temp_max_alarm
:
1172 *val
= (data
->alarms
>> lm90_max_alarm_bits
[channel
]) & 1;
1174 case hwmon_temp_crit_alarm
:
1175 *val
= (data
->alarms
>> lm90_crit_alarm_bits
[channel
]) & 1;
1177 case hwmon_temp_emergency_alarm
:
1178 *val
= (data
->alarms
>> lm90_emergency_alarm_bits
[channel
]) & 1;
1180 case hwmon_temp_fault
:
1181 *val
= (data
->alarms
>> lm90_fault_bits
[channel
]) & 1;
1183 case hwmon_temp_min
:
1185 *val
= lm90_get_temp8(data
,
1186 lm90_temp_min_index
[channel
]);
1188 *val
= lm90_get_temp11(data
,
1189 lm90_temp_min_index
[channel
]);
1191 case hwmon_temp_max
:
1193 *val
= lm90_get_temp8(data
,
1194 lm90_temp_max_index
[channel
]);
1196 *val
= lm90_get_temp11(data
,
1197 lm90_temp_max_index
[channel
]);
1199 case hwmon_temp_crit
:
1200 *val
= lm90_get_temp8(data
, lm90_temp_crit_index
[channel
]);
1202 case hwmon_temp_crit_hyst
:
1203 *val
= lm90_get_temphyst(data
, lm90_temp_crit_index
[channel
]);
1205 case hwmon_temp_emergency
:
1206 *val
= lm90_get_temp8(data
, lm90_temp_emerg_index
[channel
]);
1208 case hwmon_temp_emergency_hyst
:
1209 *val
= lm90_get_temphyst(data
, lm90_temp_emerg_index
[channel
]);
1211 case hwmon_temp_offset
:
1212 *val
= lm90_get_temp11(data
, REMOTE_OFFSET
);
1220 static int lm90_temp_write(struct device
*dev
, u32 attr
, int channel
, long val
)
1222 struct lm90_data
*data
= dev_get_drvdata(dev
);
1225 mutex_lock(&data
->update_lock
);
1227 err
= lm90_update_device(dev
);
1232 case hwmon_temp_min
:
1234 err
= lm90_set_temp8(data
,
1235 lm90_temp_min_index
[channel
],
1238 err
= lm90_set_temp11(data
,
1239 lm90_temp_min_index
[channel
],
1242 case hwmon_temp_max
:
1244 err
= lm90_set_temp8(data
,
1245 lm90_temp_max_index
[channel
],
1248 err
= lm90_set_temp11(data
,
1249 lm90_temp_max_index
[channel
],
1252 case hwmon_temp_crit
:
1253 err
= lm90_set_temp8(data
, lm90_temp_crit_index
[channel
], val
);
1255 case hwmon_temp_crit_hyst
:
1256 err
= lm90_set_temphyst(data
, val
);
1258 case hwmon_temp_emergency
:
1259 err
= lm90_set_temp8(data
, lm90_temp_emerg_index
[channel
], val
);
1261 case hwmon_temp_offset
:
1262 err
= lm90_set_temp11(data
, REMOTE_OFFSET
, val
);
1269 mutex_unlock(&data
->update_lock
);
1274 static umode_t
lm90_temp_is_visible(const void *data
, u32 attr
, int channel
)
1277 case hwmon_temp_input
:
1278 case hwmon_temp_min_alarm
:
1279 case hwmon_temp_max_alarm
:
1280 case hwmon_temp_crit_alarm
:
1281 case hwmon_temp_emergency_alarm
:
1282 case hwmon_temp_emergency_hyst
:
1283 case hwmon_temp_fault
:
1285 case hwmon_temp_min
:
1286 case hwmon_temp_max
:
1287 case hwmon_temp_crit
:
1288 case hwmon_temp_emergency
:
1289 case hwmon_temp_offset
:
1291 case hwmon_temp_crit_hyst
:
1300 static int lm90_chip_read(struct device
*dev
, u32 attr
, int channel
, long *val
)
1302 struct lm90_data
*data
= dev_get_drvdata(dev
);
1305 mutex_lock(&data
->update_lock
);
1306 err
= lm90_update_device(dev
);
1307 mutex_unlock(&data
->update_lock
);
1312 case hwmon_chip_update_interval
:
1313 *val
= data
->update_interval
;
1315 case hwmon_chip_alarms
:
1316 *val
= data
->alarms
;
1325 static int lm90_chip_write(struct device
*dev
, u32 attr
, int channel
, long val
)
1327 struct lm90_data
*data
= dev_get_drvdata(dev
);
1328 struct i2c_client
*client
= data
->client
;
1331 mutex_lock(&data
->update_lock
);
1333 err
= lm90_update_device(dev
);
1338 case hwmon_chip_update_interval
:
1339 err
= lm90_set_convrate(client
, data
,
1340 clamp_val(val
, 0, 100000));
1347 mutex_unlock(&data
->update_lock
);
1352 static umode_t
lm90_chip_is_visible(const void *data
, u32 attr
, int channel
)
1355 case hwmon_chip_update_interval
:
1357 case hwmon_chip_alarms
:
1364 static int lm90_read(struct device
*dev
, enum hwmon_sensor_types type
,
1365 u32 attr
, int channel
, long *val
)
1369 return lm90_chip_read(dev
, attr
, channel
, val
);
1371 return lm90_temp_read(dev
, attr
, channel
, val
);
1377 static int lm90_write(struct device
*dev
, enum hwmon_sensor_types type
,
1378 u32 attr
, int channel
, long val
)
1382 return lm90_chip_write(dev
, attr
, channel
, val
);
1384 return lm90_temp_write(dev
, attr
, channel
, val
);
1390 static umode_t
lm90_is_visible(const void *data
, enum hwmon_sensor_types type
,
1391 u32 attr
, int channel
)
1395 return lm90_chip_is_visible(data
, attr
, channel
);
1397 return lm90_temp_is_visible(data
, attr
, channel
);
1403 /* Return 0 if detection is successful, -ENODEV otherwise */
1404 static int lm90_detect(struct i2c_client
*client
,
1405 struct i2c_board_info
*info
)
1407 struct i2c_adapter
*adapter
= client
->adapter
;
1408 int address
= client
->addr
;
1409 const char *name
= NULL
;
1410 int man_id
, chip_id
, config1
, config2
, convrate
;
1412 if (!i2c_check_functionality(adapter
, I2C_FUNC_SMBUS_BYTE_DATA
))
1415 /* detection and identification */
1416 man_id
= i2c_smbus_read_byte_data(client
, LM90_REG_R_MAN_ID
);
1417 chip_id
= i2c_smbus_read_byte_data(client
, LM90_REG_R_CHIP_ID
);
1418 config1
= i2c_smbus_read_byte_data(client
, LM90_REG_R_CONFIG1
);
1419 convrate
= i2c_smbus_read_byte_data(client
, LM90_REG_R_CONVRATE
);
1420 if (man_id
< 0 || chip_id
< 0 || config1
< 0 || convrate
< 0)
1423 if (man_id
== 0x01 || man_id
== 0x5C || man_id
== 0x41) {
1424 config2
= i2c_smbus_read_byte_data(client
, LM90_REG_R_CONFIG2
);
1428 config2
= 0; /* Make compiler happy */
1430 if ((address
== 0x4C || address
== 0x4D)
1431 && man_id
== 0x01) { /* National Semiconductor */
1432 if ((config1
& 0x2A) == 0x00
1433 && (config2
& 0xF8) == 0x00
1434 && convrate
<= 0x09) {
1436 && (chip_id
& 0xF0) == 0x20) { /* LM90 */
1439 if ((chip_id
& 0xF0) == 0x30) { /* LM89/LM99 */
1441 dev_info(&adapter
->dev
,
1442 "Assuming LM99 chip at 0x%02x\n",
1444 dev_info(&adapter
->dev
,
1445 "If it is an LM89, instantiate it "
1446 "with the new_device sysfs "
1450 && (chip_id
& 0xF0) == 0x10) { /* LM86 */
1455 if ((address
== 0x4C || address
== 0x4D)
1456 && man_id
== 0x41) { /* Analog Devices */
1457 if ((chip_id
& 0xF0) == 0x40 /* ADM1032 */
1458 && (config1
& 0x3F) == 0x00
1459 && convrate
<= 0x0A) {
1462 * The ADM1032 supports PEC, but only if combined
1463 * transactions are not used.
1465 if (i2c_check_functionality(adapter
,
1466 I2C_FUNC_SMBUS_BYTE
))
1467 info
->flags
|= I2C_CLIENT_PEC
;
1469 if (chip_id
== 0x51 /* ADT7461 */
1470 && (config1
& 0x1B) == 0x00
1471 && convrate
<= 0x0A) {
1474 if (chip_id
== 0x57 /* ADT7461A, NCT1008 */
1475 && (config1
& 0x1B) == 0x00
1476 && convrate
<= 0x0A) {
1480 if (man_id
== 0x4D) { /* Maxim */
1481 int emerg
, emerg2
, status2
;
1484 * We read MAX6659_REG_R_REMOTE_EMERG twice, and re-read
1485 * LM90_REG_R_MAN_ID in between. If MAX6659_REG_R_REMOTE_EMERG
1486 * exists, both readings will reflect the same value. Otherwise,
1487 * the readings will be different.
1489 emerg
= i2c_smbus_read_byte_data(client
,
1490 MAX6659_REG_R_REMOTE_EMERG
);
1491 man_id
= i2c_smbus_read_byte_data(client
,
1493 emerg2
= i2c_smbus_read_byte_data(client
,
1494 MAX6659_REG_R_REMOTE_EMERG
);
1495 status2
= i2c_smbus_read_byte_data(client
,
1496 MAX6696_REG_R_STATUS2
);
1497 if (emerg
< 0 || man_id
< 0 || emerg2
< 0 || status2
< 0)
1501 * The MAX6657, MAX6658 and MAX6659 do NOT have a chip_id
1502 * register. Reading from that address will return the last
1503 * read value, which in our case is those of the man_id
1504 * register. Likewise, the config1 register seems to lack a
1505 * low nibble, so the value will be those of the previous
1506 * read, so in our case those of the man_id register.
1507 * MAX6659 has a third set of upper temperature limit registers.
1508 * Those registers also return values on MAX6657 and MAX6658,
1509 * thus the only way to detect MAX6659 is by its address.
1510 * For this reason it will be mis-detected as MAX6657 if its
1513 if (chip_id
== man_id
1514 && (address
== 0x4C || address
== 0x4D || address
== 0x4E)
1515 && (config1
& 0x1F) == (man_id
& 0x0F)
1516 && convrate
<= 0x09) {
1517 if (address
== 0x4C)
1523 * Even though MAX6695 and MAX6696 do not have a chip ID
1524 * register, reading it returns 0x01. Bit 4 of the config1
1525 * register is unused and should return zero when read. Bit 0 of
1526 * the status2 register is unused and should return zero when
1529 * MAX6695 and MAX6696 have an additional set of temperature
1530 * limit registers. We can detect those chips by checking if
1531 * one of those registers exists.
1534 && (config1
& 0x10) == 0x00
1535 && (status2
& 0x01) == 0x00
1537 && convrate
<= 0x07) {
1541 * The chip_id register of the MAX6680 and MAX6681 holds the
1542 * revision of the chip. The lowest bit of the config1 register
1543 * is unused and should return zero when read, so should the
1544 * second to last bit of config1 (software reset).
1547 && (config1
& 0x03) == 0x00
1548 && convrate
<= 0x07) {
1552 * The chip_id register of the MAX6646/6647/6649 holds the
1553 * revision of the chip. The lowest 6 bits of the config1
1554 * register are unused and should return zero when read.
1557 && (config1
& 0x3f) == 0x00
1558 && convrate
<= 0x07) {
1563 && man_id
== 0x5C) { /* Winbond/Nuvoton */
1564 if ((config1
& 0x2A) == 0x00
1565 && (config2
& 0xF8) == 0x00) {
1566 if (chip_id
== 0x01 /* W83L771W/G */
1567 && convrate
<= 0x09) {
1570 if ((chip_id
& 0xFE) == 0x10 /* W83L771AWG/ASG */
1571 && convrate
<= 0x08) {
1576 if (address
>= 0x48 && address
<= 0x4F
1577 && man_id
== 0xA1) { /* NXP Semiconductor/Philips */
1579 && (config1
& 0x2A) == 0x00
1580 && (config2
& 0xFE) == 0x00
1581 && convrate
<= 0x09) {
1585 if ((address
== 0x4C || address
== 0x4D)
1586 && man_id
== 0x47) { /* GMT */
1587 if (chip_id
== 0x01 /* G781 */
1588 && (config1
& 0x3F) == 0x00
1589 && convrate
<= 0x08)
1593 && man_id
== 0x55) { /* Texas Instruments */
1596 local_ext
= i2c_smbus_read_byte_data(client
,
1597 TMP451_REG_R_LOCAL_TEMPL
);
1599 if (chip_id
== 0x00 /* TMP451 */
1600 && (config1
& 0x1B) == 0x00
1602 && (local_ext
& 0x0F) == 0x00)
1606 if (!name
) { /* identification failed */
1607 dev_dbg(&adapter
->dev
,
1608 "Unsupported chip at 0x%02x (man_id=0x%02X, "
1609 "chip_id=0x%02X)\n", address
, man_id
, chip_id
);
1613 strlcpy(info
->type
, name
, I2C_NAME_SIZE
);
1618 static void lm90_restore_conf(void *_data
)
1620 struct lm90_data
*data
= _data
;
1621 struct i2c_client
*client
= data
->client
;
1623 /* Restore initial configuration */
1624 lm90_write_convrate(data
, data
->convrate_orig
);
1625 i2c_smbus_write_byte_data(client
, LM90_REG_W_CONFIG1
,
1629 static int lm90_init_client(struct i2c_client
*client
, struct lm90_data
*data
)
1631 int config
, convrate
;
1633 convrate
= lm90_read_reg(client
, LM90_REG_R_CONVRATE
);
1636 data
->convrate_orig
= convrate
;
1639 * Start the conversions.
1641 config
= lm90_read_reg(client
, LM90_REG_R_CONFIG1
);
1644 data
->config_orig
= config
;
1645 data
->config
= config
;
1647 lm90_set_convrate(client
, data
, 500); /* 500ms; 2Hz conversion rate */
1649 /* Check Temperature Range Select */
1650 if (data
->kind
== adt7461
|| data
->kind
== tmp451
) {
1652 data
->flags
|= LM90_FLAG_ADT7461_EXT
;
1656 * Put MAX6680/MAX8881 into extended resolution (bit 0x10,
1657 * 0.125 degree resolution) and range (0x08, extend range
1658 * to -64 degree) mode for the remote temperature sensor.
1660 if (data
->kind
== max6680
)
1664 * Select external channel 0 for max6695/96
1666 if (data
->kind
== max6696
)
1669 config
&= 0xBF; /* run */
1670 lm90_update_confreg(data
, config
);
1672 return devm_add_action_or_reset(&client
->dev
, lm90_restore_conf
, data
);
1675 static bool lm90_is_tripped(struct i2c_client
*client
, u16
*status
)
1677 struct lm90_data
*data
= i2c_get_clientdata(client
);
1680 st
= lm90_read_reg(client
, LM90_REG_R_STATUS
);
1684 if (data
->kind
== max6696
) {
1685 st2
= lm90_read_reg(client
, MAX6696_REG_R_STATUS2
);
1690 *status
= st
| (st2
<< 8);
1692 if ((st
& 0x7f) == 0 && (st2
& 0xfe) == 0)
1695 if ((st
& (LM90_STATUS_LLOW
| LM90_STATUS_LHIGH
| LM90_STATUS_LTHRM
)) ||
1696 (st2
& MAX6696_STATUS2_LOT2
))
1697 dev_warn(&client
->dev
,
1698 "temp%d out of range, please check!\n", 1);
1699 if ((st
& (LM90_STATUS_RLOW
| LM90_STATUS_RHIGH
| LM90_STATUS_RTHRM
)) ||
1700 (st2
& MAX6696_STATUS2_ROT2
))
1701 dev_warn(&client
->dev
,
1702 "temp%d out of range, please check!\n", 2);
1703 if (st
& LM90_STATUS_ROPEN
)
1704 dev_warn(&client
->dev
,
1705 "temp%d diode open, please check!\n", 2);
1706 if (st2
& (MAX6696_STATUS2_R2LOW
| MAX6696_STATUS2_R2HIGH
|
1707 MAX6696_STATUS2_R2THRM
| MAX6696_STATUS2_R2OT2
))
1708 dev_warn(&client
->dev
,
1709 "temp%d out of range, please check!\n", 3);
1710 if (st2
& MAX6696_STATUS2_R2OPEN
)
1711 dev_warn(&client
->dev
,
1712 "temp%d diode open, please check!\n", 3);
1717 static irqreturn_t
lm90_irq_thread(int irq
, void *dev_id
)
1719 struct i2c_client
*client
= dev_id
;
1722 if (lm90_is_tripped(client
, &status
))
1728 static void lm90_remove_pec(void *dev
)
1730 device_remove_file(dev
, &dev_attr_pec
);
1733 static void lm90_regulator_disable(void *regulator
)
1735 regulator_disable(regulator
);
1739 static const struct hwmon_ops lm90_ops
= {
1740 .is_visible
= lm90_is_visible
,
1742 .write
= lm90_write
,
1745 static int lm90_probe(struct i2c_client
*client
,
1746 const struct i2c_device_id
*id
)
1748 struct device
*dev
= &client
->dev
;
1749 struct i2c_adapter
*adapter
= client
->adapter
;
1750 struct hwmon_channel_info
*info
;
1751 struct regulator
*regulator
;
1752 struct device
*hwmon_dev
;
1753 struct lm90_data
*data
;
1756 regulator
= devm_regulator_get(dev
, "vcc");
1757 if (IS_ERR(regulator
))
1758 return PTR_ERR(regulator
);
1760 err
= regulator_enable(regulator
);
1762 dev_err(dev
, "Failed to enable regulator: %d\n", err
);
1766 err
= devm_add_action_or_reset(dev
, lm90_regulator_disable
, regulator
);
1770 data
= devm_kzalloc(dev
, sizeof(struct lm90_data
), GFP_KERNEL
);
1774 data
->client
= client
;
1775 i2c_set_clientdata(client
, data
);
1776 mutex_init(&data
->update_lock
);
1778 /* Set the device type */
1779 if (client
->dev
.of_node
)
1780 data
->kind
= (enum chips
)of_device_get_match_data(&client
->dev
);
1782 data
->kind
= id
->driver_data
;
1783 if (data
->kind
== adm1032
) {
1784 if (!i2c_check_functionality(adapter
, I2C_FUNC_SMBUS_BYTE
))
1785 client
->flags
&= ~I2C_CLIENT_PEC
;
1789 * Different devices have different alarm bits triggering the
1792 data
->alert_alarms
= lm90_params
[data
->kind
].alert_alarms
;
1794 /* Set chip capabilities */
1795 data
->flags
= lm90_params
[data
->kind
].flags
;
1797 data
->chip
.ops
= &lm90_ops
;
1798 data
->chip
.info
= data
->info
;
1800 data
->info
[0] = HWMON_CHANNEL_INFO(chip
,
1801 HWMON_C_REGISTER_TZ
| HWMON_C_UPDATE_INTERVAL
| HWMON_C_ALARMS
);
1802 data
->info
[1] = &data
->temp_info
;
1804 info
= &data
->temp_info
;
1805 info
->type
= hwmon_temp
;
1806 info
->config
= data
->channel_config
;
1808 data
->channel_config
[0] = HWMON_T_INPUT
| HWMON_T_MIN
| HWMON_T_MAX
|
1809 HWMON_T_CRIT
| HWMON_T_CRIT_HYST
| HWMON_T_MIN_ALARM
|
1810 HWMON_T_MAX_ALARM
| HWMON_T_CRIT_ALARM
;
1811 data
->channel_config
[1] = HWMON_T_INPUT
| HWMON_T_MIN
| HWMON_T_MAX
|
1812 HWMON_T_CRIT
| HWMON_T_CRIT_HYST
| HWMON_T_MIN_ALARM
|
1813 HWMON_T_MAX_ALARM
| HWMON_T_CRIT_ALARM
| HWMON_T_FAULT
;
1815 if (data
->flags
& LM90_HAVE_OFFSET
)
1816 data
->channel_config
[1] |= HWMON_T_OFFSET
;
1818 if (data
->flags
& LM90_HAVE_EMERGENCY
) {
1819 data
->channel_config
[0] |= HWMON_T_EMERGENCY
|
1820 HWMON_T_EMERGENCY_HYST
;
1821 data
->channel_config
[1] |= HWMON_T_EMERGENCY
|
1822 HWMON_T_EMERGENCY_HYST
;
1825 if (data
->flags
& LM90_HAVE_EMERGENCY_ALARM
) {
1826 data
->channel_config
[0] |= HWMON_T_EMERGENCY_ALARM
;
1827 data
->channel_config
[1] |= HWMON_T_EMERGENCY_ALARM
;
1830 if (data
->flags
& LM90_HAVE_TEMP3
) {
1831 data
->channel_config
[2] = HWMON_T_INPUT
|
1832 HWMON_T_MIN
| HWMON_T_MAX
|
1833 HWMON_T_CRIT
| HWMON_T_CRIT_HYST
|
1834 HWMON_T_EMERGENCY
| HWMON_T_EMERGENCY_HYST
|
1835 HWMON_T_MIN_ALARM
| HWMON_T_MAX_ALARM
|
1836 HWMON_T_CRIT_ALARM
| HWMON_T_EMERGENCY_ALARM
|
1840 data
->reg_local_ext
= lm90_params
[data
->kind
].reg_local_ext
;
1842 /* Set maximum conversion rate */
1843 data
->max_convrate
= lm90_params
[data
->kind
].max_convrate
;
1845 /* Initialize the LM90 chip */
1846 err
= lm90_init_client(client
, data
);
1848 dev_err(dev
, "Failed to initialize device\n");
1853 * The 'pec' attribute is attached to the i2c device and thus created
1856 if (client
->flags
& I2C_CLIENT_PEC
) {
1857 err
= device_create_file(dev
, &dev_attr_pec
);
1860 err
= devm_add_action_or_reset(dev
, lm90_remove_pec
, dev
);
1865 hwmon_dev
= devm_hwmon_device_register_with_info(dev
, client
->name
,
1868 if (IS_ERR(hwmon_dev
))
1869 return PTR_ERR(hwmon_dev
);
1872 dev_dbg(dev
, "IRQ: %d\n", client
->irq
);
1873 err
= devm_request_threaded_irq(dev
, client
->irq
,
1874 NULL
, lm90_irq_thread
,
1875 IRQF_TRIGGER_LOW
| IRQF_ONESHOT
,
1878 dev_err(dev
, "cannot request IRQ %d\n", client
->irq
);
1886 static void lm90_alert(struct i2c_client
*client
, enum i2c_alert_protocol type
,
1891 if (type
!= I2C_PROTOCOL_SMBUS_ALERT
)
1894 if (lm90_is_tripped(client
, &alarms
)) {
1896 * Disable ALERT# output, because these chips don't implement
1897 * SMBus alert correctly; they should only hold the alert line
1900 struct lm90_data
*data
= i2c_get_clientdata(client
);
1902 if ((data
->flags
& LM90_HAVE_BROKEN_ALERT
) &&
1903 (alarms
& data
->alert_alarms
)) {
1904 dev_dbg(&client
->dev
, "Disabling ALERT#\n");
1905 lm90_update_confreg(data
, data
->config
| 0x80);
1908 dev_info(&client
->dev
, "Everything OK\n");
1912 static struct i2c_driver lm90_driver
= {
1913 .class = I2C_CLASS_HWMON
,
1916 .of_match_table
= of_match_ptr(lm90_of_match
),
1918 .probe
= lm90_probe
,
1919 .alert
= lm90_alert
,
1920 .id_table
= lm90_id
,
1921 .detect
= lm90_detect
,
1922 .address_list
= normal_i2c
,
1925 module_i2c_driver(lm90_driver
);
1927 MODULE_AUTHOR("Jean Delvare <jdelvare@suse.de>");
1928 MODULE_DESCRIPTION("LM90/ADM1032 driver");
1929 MODULE_LICENSE("GPL");