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 MAX6654 chip made by Maxim. This chip can be
39 * at 9 different addresses, similar to MAX6680/MAX6681. The MAX6654 is similar
40 * to MAX6657/MAX6658/MAX6659, but does not support critical temperature
41 * limits. Extended range is available by setting the configuration register
42 * accordingly, and is done during initialization. Extended precision is only
43 * available at conversion rates of 1 Hz and slower. Note that extended
44 * precision is not enabled by default, as this driver initializes all chips
47 * This driver also supports the MAX6646, MAX6647, MAX6648, MAX6649 and
48 * MAX6692 chips made by Maxim. These are again similar to the LM86,
49 * but they use unsigned temperature values and can report temperatures
50 * from 0 to 145 degrees.
52 * This driver also supports the MAX6680 and MAX6681, two other sensor
53 * chips made by Maxim. These are quite similar to the other Maxim
54 * chips. The MAX6680 and MAX6681 only differ in the pinout so they can
55 * be treated identically.
57 * This driver also supports the MAX6695 and MAX6696, two other sensor
58 * chips made by Maxim. These are also quite similar to other Maxim
59 * chips, but support three temperature sensors instead of two. MAX6695
60 * and MAX6696 only differ in the pinout so they can be treated identically.
62 * This driver also supports ADT7461 and ADT7461A from Analog Devices as well as
63 * NCT1008 from ON Semiconductor. The chips are supported in both compatibility
64 * and extended mode. They are mostly compatible with LM90 except for a data
65 * format difference for the temperature value registers.
67 * This driver also supports the SA56004 from Philips. This device is
68 * pin-compatible with the LM86, the ED/EDP parts are also address-compatible.
70 * This driver also supports the G781 from GMT. This device is compatible
73 * This driver also supports TMP451 and TMP461 from Texas Instruments.
74 * Those devices are supported in both compatibility and extended mode.
75 * They are mostly compatible with ADT7461 except for local temperature
76 * low byte register and max conversion rate.
78 * Since the LM90 was the first chipset supported by this driver, most
79 * comments will refer to this chipset, but are actually general and
80 * concern all supported chipsets, unless mentioned otherwise.
83 #include <linux/module.h>
84 #include <linux/init.h>
85 #include <linux/slab.h>
86 #include <linux/jiffies.h>
87 #include <linux/i2c.h>
88 #include <linux/hwmon.h>
89 #include <linux/err.h>
90 #include <linux/mutex.h>
91 #include <linux/of_device.h>
92 #include <linux/sysfs.h>
93 #include <linux/interrupt.h>
94 #include <linux/regulator/consumer.h>
98 * Address is fully defined internally and cannot be changed except for
99 * MAX6659, MAX6680 and MAX6681.
100 * LM86, LM89, LM90, LM99, ADM1032, ADM1032-1, ADT7461, ADT7461A, MAX6649,
101 * MAX6657, MAX6658, NCT1008 and W83L771 have address 0x4c.
102 * ADM1032-2, ADT7461-2, ADT7461A-2, LM89-1, LM99-1, MAX6646, and NCT1008D
104 * MAX6647 has address 0x4e.
105 * MAX6659 can have address 0x4c, 0x4d or 0x4e.
106 * MAX6654, MAX6680, and MAX6681 can have address 0x18, 0x19, 0x1a, 0x29,
107 * 0x2a, 0x2b, 0x4c, 0x4d or 0x4e.
108 * SA56004 can have address 0x48 through 0x4F.
111 static const unsigned short normal_i2c
[] = {
112 0x18, 0x19, 0x1a, 0x29, 0x2a, 0x2b, 0x48, 0x49, 0x4a, 0x4b, 0x4c,
113 0x4d, 0x4e, 0x4f, I2C_CLIENT_END
};
115 enum chips
{ lm90
, adm1032
, lm99
, lm86
, max6657
, max6659
, adt7461
, max6680
,
116 max6646
, w83l771
, max6696
, sa56004
, g781
, tmp451
, tmp461
, max6654
};
122 #define LM90_REG_R_MAN_ID 0xFE
123 #define LM90_REG_R_CHIP_ID 0xFF
124 #define LM90_REG_R_CONFIG1 0x03
125 #define LM90_REG_W_CONFIG1 0x09
126 #define LM90_REG_R_CONFIG2 0xBF
127 #define LM90_REG_W_CONFIG2 0xBF
128 #define LM90_REG_R_CONVRATE 0x04
129 #define LM90_REG_W_CONVRATE 0x0A
130 #define LM90_REG_R_STATUS 0x02
131 #define LM90_REG_R_LOCAL_TEMP 0x00
132 #define LM90_REG_R_LOCAL_HIGH 0x05
133 #define LM90_REG_W_LOCAL_HIGH 0x0B
134 #define LM90_REG_R_LOCAL_LOW 0x06
135 #define LM90_REG_W_LOCAL_LOW 0x0C
136 #define LM90_REG_R_LOCAL_CRIT 0x20
137 #define LM90_REG_W_LOCAL_CRIT 0x20
138 #define LM90_REG_R_REMOTE_TEMPH 0x01
139 #define LM90_REG_R_REMOTE_TEMPL 0x10
140 #define LM90_REG_R_REMOTE_OFFSH 0x11
141 #define LM90_REG_W_REMOTE_OFFSH 0x11
142 #define LM90_REG_R_REMOTE_OFFSL 0x12
143 #define LM90_REG_W_REMOTE_OFFSL 0x12
144 #define LM90_REG_R_REMOTE_HIGHH 0x07
145 #define LM90_REG_W_REMOTE_HIGHH 0x0D
146 #define LM90_REG_R_REMOTE_HIGHL 0x13
147 #define LM90_REG_W_REMOTE_HIGHL 0x13
148 #define LM90_REG_R_REMOTE_LOWH 0x08
149 #define LM90_REG_W_REMOTE_LOWH 0x0E
150 #define LM90_REG_R_REMOTE_LOWL 0x14
151 #define LM90_REG_W_REMOTE_LOWL 0x14
152 #define LM90_REG_R_REMOTE_CRIT 0x19
153 #define LM90_REG_W_REMOTE_CRIT 0x19
154 #define LM90_REG_R_TCRIT_HYST 0x21
155 #define LM90_REG_W_TCRIT_HYST 0x21
157 /* MAX6646/6647/6649/6654/6657/6658/6659/6695/6696 registers */
159 #define MAX6657_REG_R_LOCAL_TEMPL 0x11
160 #define MAX6696_REG_R_STATUS2 0x12
161 #define MAX6659_REG_R_REMOTE_EMERG 0x16
162 #define MAX6659_REG_W_REMOTE_EMERG 0x16
163 #define MAX6659_REG_R_LOCAL_EMERG 0x17
164 #define MAX6659_REG_W_LOCAL_EMERG 0x17
166 /* SA56004 registers */
168 #define SA56004_REG_R_LOCAL_TEMPL 0x22
170 #define LM90_MAX_CONVRATE_MS 16000 /* Maximum conversion rate in ms */
172 /* TMP451/TMP461 registers */
173 #define TMP451_REG_R_LOCAL_TEMPL 0x15
174 #define TMP451_REG_CONALERT 0x22
176 #define TMP461_REG_CHEN 0x16
177 #define TMP461_REG_DFC 0x24
182 #define LM90_FLAG_ADT7461_EXT (1 << 0) /* ADT7461 extended mode */
183 /* Device features */
184 #define LM90_HAVE_OFFSET (1 << 1) /* temperature offset register */
185 #define LM90_HAVE_REM_LIMIT_EXT (1 << 3) /* extended remote limit */
186 #define LM90_HAVE_EMERGENCY (1 << 4) /* 3rd upper (emergency) limit */
187 #define LM90_HAVE_EMERGENCY_ALARM (1 << 5)/* emergency alarm */
188 #define LM90_HAVE_TEMP3 (1 << 6) /* 3rd temperature sensor */
189 #define LM90_HAVE_BROKEN_ALERT (1 << 7) /* Broken alert */
190 #define LM90_HAVE_EXTENDED_TEMP (1 << 8) /* extended temperature support*/
191 #define LM90_PAUSE_FOR_CONFIG (1 << 9) /* Pause conversion for config */
192 #define LM90_HAVE_CRIT (1 << 10)/* Chip supports CRIT/OVERT register */
193 #define LM90_HAVE_CRIT_ALRM_SWP (1 << 11)/* critical alarm bits swapped */
196 #define LM90_STATUS_LTHRM (1 << 0) /* local THERM limit tripped */
197 #define LM90_STATUS_RTHRM (1 << 1) /* remote THERM limit tripped */
198 #define LM90_STATUS_ROPEN (1 << 2) /* remote is an open circuit */
199 #define LM90_STATUS_RLOW (1 << 3) /* remote low temp limit tripped */
200 #define LM90_STATUS_RHIGH (1 << 4) /* remote high temp limit tripped */
201 #define LM90_STATUS_LLOW (1 << 5) /* local low temp limit tripped */
202 #define LM90_STATUS_LHIGH (1 << 6) /* local high temp limit tripped */
203 #define LM90_STATUS_BUSY (1 << 7) /* conversion is ongoing */
205 #define MAX6696_STATUS2_R2THRM (1 << 1) /* remote2 THERM limit tripped */
206 #define MAX6696_STATUS2_R2OPEN (1 << 2) /* remote2 is an open circuit */
207 #define MAX6696_STATUS2_R2LOW (1 << 3) /* remote2 low temp limit tripped */
208 #define MAX6696_STATUS2_R2HIGH (1 << 4) /* remote2 high temp limit tripped */
209 #define MAX6696_STATUS2_ROT2 (1 << 5) /* remote emergency limit tripped */
210 #define MAX6696_STATUS2_R2OT2 (1 << 6) /* remote2 emergency limit tripped */
211 #define MAX6696_STATUS2_LOT2 (1 << 7) /* local emergency limit tripped */
214 * Driver data (common to all clients)
217 static const struct i2c_device_id lm90_id
[] = {
218 { "adm1032", adm1032
},
219 { "adt7461", adt7461
},
220 { "adt7461a", adt7461
},
226 { "max6646", max6646
},
227 { "max6647", max6646
},
228 { "max6649", max6646
},
229 { "max6654", max6654
},
230 { "max6657", max6657
},
231 { "max6658", max6657
},
232 { "max6659", max6659
},
233 { "max6680", max6680
},
234 { "max6681", max6680
},
235 { "max6695", max6696
},
236 { "max6696", max6696
},
237 { "nct1008", adt7461
},
238 { "w83l771", w83l771
},
239 { "sa56004", sa56004
},
240 { "tmp451", tmp451
},
241 { "tmp461", tmp461
},
244 MODULE_DEVICE_TABLE(i2c
, lm90_id
);
246 static const struct of_device_id __maybe_unused lm90_of_match
[] = {
248 .compatible
= "adi,adm1032",
249 .data
= (void *)adm1032
252 .compatible
= "adi,adt7461",
253 .data
= (void *)adt7461
256 .compatible
= "adi,adt7461a",
257 .data
= (void *)adt7461
260 .compatible
= "gmt,g781",
264 .compatible
= "national,lm90",
268 .compatible
= "national,lm86",
272 .compatible
= "national,lm89",
276 .compatible
= "national,lm99",
280 .compatible
= "dallas,max6646",
281 .data
= (void *)max6646
284 .compatible
= "dallas,max6647",
285 .data
= (void *)max6646
288 .compatible
= "dallas,max6649",
289 .data
= (void *)max6646
292 .compatible
= "dallas,max6654",
293 .data
= (void *)max6654
296 .compatible
= "dallas,max6657",
297 .data
= (void *)max6657
300 .compatible
= "dallas,max6658",
301 .data
= (void *)max6657
304 .compatible
= "dallas,max6659",
305 .data
= (void *)max6659
308 .compatible
= "dallas,max6680",
309 .data
= (void *)max6680
312 .compatible
= "dallas,max6681",
313 .data
= (void *)max6680
316 .compatible
= "dallas,max6695",
317 .data
= (void *)max6696
320 .compatible
= "dallas,max6696",
321 .data
= (void *)max6696
324 .compatible
= "onnn,nct1008",
325 .data
= (void *)adt7461
328 .compatible
= "winbond,w83l771",
329 .data
= (void *)w83l771
332 .compatible
= "nxp,sa56004",
333 .data
= (void *)sa56004
336 .compatible
= "ti,tmp451",
337 .data
= (void *)tmp451
340 .compatible
= "ti,tmp461",
341 .data
= (void *)tmp461
345 MODULE_DEVICE_TABLE(of
, lm90_of_match
);
348 * chip type specific parameters
351 u32 flags
; /* Capabilities */
352 u16 alert_alarms
; /* Which alarm bits trigger ALERT# */
353 /* Upper 8 bits for max6695/96 */
354 u8 max_convrate
; /* Maximum conversion rate register value */
355 u8 reg_local_ext
; /* Extended local temp register (optional) */
358 static const struct lm90_params lm90_params
[] = {
360 .flags
= LM90_HAVE_OFFSET
| LM90_HAVE_REM_LIMIT_EXT
361 | LM90_HAVE_BROKEN_ALERT
| LM90_HAVE_CRIT
,
362 .alert_alarms
= 0x7c,
366 .flags
= LM90_HAVE_OFFSET
| LM90_HAVE_REM_LIMIT_EXT
367 | LM90_HAVE_BROKEN_ALERT
| LM90_HAVE_EXTENDED_TEMP
369 .alert_alarms
= 0x7c,
373 .flags
= LM90_HAVE_OFFSET
| LM90_HAVE_REM_LIMIT_EXT
374 | LM90_HAVE_BROKEN_ALERT
| LM90_HAVE_CRIT
,
375 .alert_alarms
= 0x7c,
379 .flags
= LM90_HAVE_OFFSET
| LM90_HAVE_REM_LIMIT_EXT
381 .alert_alarms
= 0x7b,
385 .flags
= LM90_HAVE_OFFSET
| LM90_HAVE_REM_LIMIT_EXT
387 .alert_alarms
= 0x7b,
391 .flags
= LM90_HAVE_OFFSET
| LM90_HAVE_REM_LIMIT_EXT
393 .alert_alarms
= 0x7b,
397 .flags
= LM90_HAVE_CRIT
| LM90_HAVE_BROKEN_ALERT
,
398 .alert_alarms
= 0x7c,
400 .reg_local_ext
= MAX6657_REG_R_LOCAL_TEMPL
,
403 .alert_alarms
= 0x7c,
405 .reg_local_ext
= MAX6657_REG_R_LOCAL_TEMPL
,
408 .flags
= LM90_PAUSE_FOR_CONFIG
| LM90_HAVE_CRIT
,
409 .alert_alarms
= 0x7c,
411 .reg_local_ext
= MAX6657_REG_R_LOCAL_TEMPL
,
414 .flags
= LM90_HAVE_EMERGENCY
| LM90_HAVE_CRIT
,
415 .alert_alarms
= 0x7c,
417 .reg_local_ext
= MAX6657_REG_R_LOCAL_TEMPL
,
420 .flags
= LM90_HAVE_OFFSET
| LM90_HAVE_CRIT
421 | LM90_HAVE_CRIT_ALRM_SWP
| LM90_HAVE_BROKEN_ALERT
,
422 .alert_alarms
= 0x7c,
426 .flags
= LM90_HAVE_EMERGENCY
427 | LM90_HAVE_EMERGENCY_ALARM
| LM90_HAVE_TEMP3
| LM90_HAVE_CRIT
,
428 .alert_alarms
= 0x1c7c,
430 .reg_local_ext
= MAX6657_REG_R_LOCAL_TEMPL
,
433 .flags
= LM90_HAVE_OFFSET
| LM90_HAVE_REM_LIMIT_EXT
| LM90_HAVE_CRIT
,
434 .alert_alarms
= 0x7c,
438 .flags
= LM90_HAVE_OFFSET
| LM90_HAVE_REM_LIMIT_EXT
| LM90_HAVE_CRIT
,
439 .alert_alarms
= 0x7b,
441 .reg_local_ext
= SA56004_REG_R_LOCAL_TEMPL
,
444 .flags
= LM90_HAVE_OFFSET
| LM90_HAVE_REM_LIMIT_EXT
445 | LM90_HAVE_BROKEN_ALERT
| LM90_HAVE_EXTENDED_TEMP
| LM90_HAVE_CRIT
,
446 .alert_alarms
= 0x7c,
448 .reg_local_ext
= TMP451_REG_R_LOCAL_TEMPL
,
451 .flags
= LM90_HAVE_OFFSET
| LM90_HAVE_REM_LIMIT_EXT
452 | LM90_HAVE_BROKEN_ALERT
| LM90_HAVE_EXTENDED_TEMP
| LM90_HAVE_CRIT
,
453 .alert_alarms
= 0x7c,
455 .reg_local_ext
= TMP451_REG_R_LOCAL_TEMPL
,
460 * TEMP8 register index
462 enum lm90_temp8_reg_index
{
467 LOCAL_EMERG
, /* max6659 and max6695/96 */
468 REMOTE_EMERG
, /* max6659 and max6695/96 */
469 REMOTE2_CRIT
, /* max6695/96 only */
470 REMOTE2_EMERG
, /* max6695/96 only */
475 * TEMP11 register index
477 enum lm90_temp11_reg_index
{
481 REMOTE_OFFSET
, /* except max6646, max6657/58/59, and max6695/96 */
483 REMOTE2_TEMP
, /* max6695/96 only */
484 REMOTE2_LOW
, /* max6695/96 only */
485 REMOTE2_HIGH
, /* max6695/96 only */
490 * Client data (each client gets its own)
494 struct i2c_client
*client
;
495 struct device
*hwmon_dev
;
496 u32 channel_config
[4];
497 struct hwmon_channel_info temp_info
;
498 const struct hwmon_channel_info
*info
[3];
499 struct hwmon_chip_info chip
;
500 struct mutex update_lock
;
501 bool valid
; /* true if register values are valid */
502 unsigned long last_updated
; /* in jiffies */
506 unsigned int update_interval
; /* in milliseconds */
508 u8 config
; /* Current configuration register value */
509 u8 config_orig
; /* Original configuration register value */
510 u8 convrate_orig
; /* Original conversion rate register value */
511 u16 alert_alarms
; /* Which alarm bits trigger ALERT# */
512 /* Upper 8 bits for max6695/96 */
513 u8 max_convrate
; /* Maximum conversion rate */
514 u8 reg_local_ext
; /* local extension register offset */
516 /* registers values */
517 s8 temp8
[TEMP8_REG_NUM
];
518 s16 temp11
[TEMP11_REG_NUM
];
520 u16 alarms
; /* bitvector (upper 8 bits for max6695/96) */
528 * The ADM1032 supports PEC but not on write byte transactions, so we need
529 * to explicitly ask for a transaction without PEC.
531 static inline s32
adm1032_write_byte(struct i2c_client
*client
, u8 value
)
533 return i2c_smbus_xfer(client
->adapter
, client
->addr
,
534 client
->flags
& ~I2C_CLIENT_PEC
,
535 I2C_SMBUS_WRITE
, value
, I2C_SMBUS_BYTE
, NULL
);
539 * It is assumed that client->update_lock is held (unless we are in
540 * detection or initialization steps). This matters when PEC is enabled,
541 * because we don't want the address pointer to change between the write
542 * byte and the read byte transactions.
544 static int lm90_read_reg(struct i2c_client
*client
, u8 reg
)
548 if (client
->flags
& I2C_CLIENT_PEC
) {
549 err
= adm1032_write_byte(client
, reg
);
551 err
= i2c_smbus_read_byte(client
);
553 err
= i2c_smbus_read_byte_data(client
, reg
);
558 static int lm90_read16(struct i2c_client
*client
, u8 regh
, u8 regl
)
563 * There is a trick here. We have to read two registers to have the
564 * sensor temperature, but we have to beware a conversion could occur
565 * between the readings. The datasheet says we should either use
566 * the one-shot conversion register, which we don't want to do
567 * (disables hardware monitoring) or monitor the busy bit, which is
568 * impossible (we can't read the values and monitor that bit at the
569 * exact same time). So the solution used here is to read the high
570 * byte once, then the low byte, then the high byte again. If the new
571 * high byte matches the old one, then we have a valid reading. Else
572 * we have to read the low byte again, and now we believe we have a
575 oldh
= lm90_read_reg(client
, regh
);
578 l
= lm90_read_reg(client
, regl
);
581 newh
= lm90_read_reg(client
, regh
);
585 l
= lm90_read_reg(client
, regl
);
589 return (newh
<< 8) | l
;
592 static int lm90_update_confreg(struct lm90_data
*data
, u8 config
)
594 if (data
->config
!= config
) {
597 err
= i2c_smbus_write_byte_data(data
->client
,
602 data
->config
= config
;
608 * client->update_lock must be held when calling this function (unless we are
609 * in detection or initialization steps), and while a remote channel other
610 * than channel 0 is selected. Also, calling code must make sure to re-select
611 * external channel 0 before releasing the lock. This is necessary because
612 * various registers have different meanings as a result of selecting a
613 * non-default remote channel.
615 static int lm90_select_remote_channel(struct lm90_data
*data
, int channel
)
619 if (data
->kind
== max6696
) {
620 u8 config
= data
->config
& ~0x08;
624 err
= lm90_update_confreg(data
, config
);
629 static int lm90_write_convrate(struct lm90_data
*data
, int val
)
631 u8 config
= data
->config
;
634 /* Save config and pause conversion */
635 if (data
->flags
& LM90_PAUSE_FOR_CONFIG
) {
636 err
= lm90_update_confreg(data
, config
| 0x40);
642 err
= i2c_smbus_write_byte_data(data
->client
, LM90_REG_W_CONVRATE
, val
);
644 /* Revert change to config */
645 lm90_update_confreg(data
, config
);
651 * Set conversion rate.
652 * client->update_lock must be held when calling this function (unless we are
653 * in detection or initialization steps).
655 static int lm90_set_convrate(struct i2c_client
*client
, struct lm90_data
*data
,
656 unsigned int interval
)
658 unsigned int update_interval
;
661 /* Shift calculations to avoid rounding errors */
664 /* find the nearest update rate */
665 for (i
= 0, update_interval
= LM90_MAX_CONVRATE_MS
<< 6;
666 i
< data
->max_convrate
; i
++, update_interval
>>= 1)
667 if (interval
>= update_interval
* 3 / 4)
670 err
= lm90_write_convrate(data
, i
);
671 data
->update_interval
= DIV_ROUND_CLOSEST(update_interval
, 64);
675 static int lm90_update_limits(struct device
*dev
)
677 struct lm90_data
*data
= dev_get_drvdata(dev
);
678 struct i2c_client
*client
= data
->client
;
681 if (data
->flags
& LM90_HAVE_CRIT
) {
682 val
= lm90_read_reg(client
, LM90_REG_R_LOCAL_CRIT
);
685 data
->temp8
[LOCAL_CRIT
] = val
;
687 val
= lm90_read_reg(client
, LM90_REG_R_REMOTE_CRIT
);
690 data
->temp8
[REMOTE_CRIT
] = val
;
692 val
= lm90_read_reg(client
, LM90_REG_R_TCRIT_HYST
);
695 data
->temp_hyst
= val
;
698 val
= lm90_read_reg(client
, LM90_REG_R_REMOTE_LOWH
);
701 data
->temp11
[REMOTE_LOW
] = val
<< 8;
703 if (data
->flags
& LM90_HAVE_REM_LIMIT_EXT
) {
704 val
= lm90_read_reg(client
, LM90_REG_R_REMOTE_LOWL
);
707 data
->temp11
[REMOTE_LOW
] |= val
;
710 val
= lm90_read_reg(client
, LM90_REG_R_REMOTE_HIGHH
);
713 data
->temp11
[REMOTE_HIGH
] = val
<< 8;
715 if (data
->flags
& LM90_HAVE_REM_LIMIT_EXT
) {
716 val
= lm90_read_reg(client
, LM90_REG_R_REMOTE_HIGHL
);
719 data
->temp11
[REMOTE_HIGH
] |= val
;
722 if (data
->flags
& LM90_HAVE_OFFSET
) {
723 val
= lm90_read16(client
, LM90_REG_R_REMOTE_OFFSH
,
724 LM90_REG_R_REMOTE_OFFSL
);
727 data
->temp11
[REMOTE_OFFSET
] = val
;
730 if (data
->flags
& LM90_HAVE_EMERGENCY
) {
731 val
= lm90_read_reg(client
, MAX6659_REG_R_LOCAL_EMERG
);
734 data
->temp8
[LOCAL_EMERG
] = val
;
736 val
= lm90_read_reg(client
, MAX6659_REG_R_REMOTE_EMERG
);
739 data
->temp8
[REMOTE_EMERG
] = val
;
742 if (data
->kind
== max6696
) {
743 val
= lm90_select_remote_channel(data
, 1);
747 val
= lm90_read_reg(client
, LM90_REG_R_REMOTE_CRIT
);
750 data
->temp8
[REMOTE2_CRIT
] = val
;
752 val
= lm90_read_reg(client
, MAX6659_REG_R_REMOTE_EMERG
);
755 data
->temp8
[REMOTE2_EMERG
] = val
;
757 val
= lm90_read_reg(client
, LM90_REG_R_REMOTE_LOWH
);
760 data
->temp11
[REMOTE2_LOW
] = val
<< 8;
762 val
= lm90_read_reg(client
, LM90_REG_R_REMOTE_HIGHH
);
765 data
->temp11
[REMOTE2_HIGH
] = val
<< 8;
767 lm90_select_remote_channel(data
, 0);
773 static int lm90_update_device(struct device
*dev
)
775 struct lm90_data
*data
= dev_get_drvdata(dev
);
776 struct i2c_client
*client
= data
->client
;
777 unsigned long next_update
;
781 val
= lm90_update_limits(dev
);
786 next_update
= data
->last_updated
+
787 msecs_to_jiffies(data
->update_interval
);
788 if (time_after(jiffies
, next_update
) || !data
->valid
) {
789 dev_dbg(&client
->dev
, "Updating lm90 data.\n");
793 val
= lm90_read_reg(client
, LM90_REG_R_LOCAL_LOW
);
796 data
->temp8
[LOCAL_LOW
] = val
;
798 val
= lm90_read_reg(client
, LM90_REG_R_LOCAL_HIGH
);
801 data
->temp8
[LOCAL_HIGH
] = val
;
803 if (data
->reg_local_ext
) {
804 val
= lm90_read16(client
, LM90_REG_R_LOCAL_TEMP
,
805 data
->reg_local_ext
);
808 data
->temp11
[LOCAL_TEMP
] = val
;
810 val
= lm90_read_reg(client
, LM90_REG_R_LOCAL_TEMP
);
813 data
->temp11
[LOCAL_TEMP
] = val
<< 8;
815 val
= lm90_read16(client
, LM90_REG_R_REMOTE_TEMPH
,
816 LM90_REG_R_REMOTE_TEMPL
);
819 data
->temp11
[REMOTE_TEMP
] = val
;
821 val
= lm90_read_reg(client
, LM90_REG_R_STATUS
);
824 data
->alarms
= val
& ~LM90_STATUS_BUSY
;
826 if (data
->kind
== max6696
) {
827 val
= lm90_select_remote_channel(data
, 1);
831 val
= lm90_read16(client
, LM90_REG_R_REMOTE_TEMPH
,
832 LM90_REG_R_REMOTE_TEMPL
);
834 lm90_select_remote_channel(data
, 0);
837 data
->temp11
[REMOTE2_TEMP
] = val
;
839 lm90_select_remote_channel(data
, 0);
841 val
= lm90_read_reg(client
, MAX6696_REG_R_STATUS2
);
844 data
->alarms
|= val
<< 8;
848 * Re-enable ALERT# output if it was originally enabled and
849 * relevant alarms are all clear
851 if (!(data
->config_orig
& 0x80) &&
852 !(data
->alarms
& data
->alert_alarms
)) {
853 if (data
->config
& 0x80) {
854 dev_dbg(&client
->dev
, "Re-enabling ALERT#\n");
855 lm90_update_confreg(data
, data
->config
& ~0x80);
859 data
->last_updated
= jiffies
;
868 * For local temperatures and limits, critical limits and the hysteresis
869 * value, the LM90 uses signed 8-bit values with LSB = 1 degree Celsius.
870 * For remote temperatures and limits, it uses signed 11-bit values with
871 * LSB = 0.125 degree Celsius, left-justified in 16-bit registers. Some
872 * Maxim chips use unsigned values.
875 static inline int temp_from_s8(s8 val
)
880 static inline int temp_from_u8(u8 val
)
885 static inline int temp_from_s16(s16 val
)
887 return val
/ 32 * 125;
890 static inline int temp_from_u16(u16 val
)
892 return val
/ 32 * 125;
895 static s8
temp_to_s8(long val
)
902 return (val
- 500) / 1000;
903 return (val
+ 500) / 1000;
906 static u8
temp_to_u8(long val
)
912 return (val
+ 500) / 1000;
915 static s16
temp_to_s16(long val
)
922 return (val
- 62) / 125 * 32;
923 return (val
+ 62) / 125 * 32;
926 static u8
hyst_to_reg(long val
)
932 return (val
+ 500) / 1000;
936 * ADT7461 in compatibility mode is almost identical to LM90 except that
937 * attempts to write values that are outside the range 0 < temp < 127 are
938 * treated as the boundary value.
940 * ADT7461 in "extended mode" operation uses unsigned integers offset by
941 * 64 (e.g., 0 -> -64 degC). The range is restricted to -64..191 degC.
943 static inline int temp_from_u8_adt7461(struct lm90_data
*data
, u8 val
)
945 if (data
->flags
& LM90_FLAG_ADT7461_EXT
)
946 return (val
- 64) * 1000;
947 return temp_from_s8(val
);
950 static inline int temp_from_u16_adt7461(struct lm90_data
*data
, u16 val
)
952 if (data
->flags
& LM90_FLAG_ADT7461_EXT
)
953 return (val
- 0x4000) / 64 * 250;
954 return temp_from_s16(val
);
957 static u8
temp_to_u8_adt7461(struct lm90_data
*data
, long val
)
959 if (data
->flags
& LM90_FLAG_ADT7461_EXT
) {
964 return (val
+ 500 + 64000) / 1000;
970 return (val
+ 500) / 1000;
973 static u16
temp_to_u16_adt7461(struct lm90_data
*data
, long val
)
975 if (data
->flags
& LM90_FLAG_ADT7461_EXT
) {
980 return (val
+ 64000 + 125) / 250 * 64;
986 return (val
+ 125) / 250 * 64;
989 /* pec used for ADM1032 only */
990 static ssize_t
pec_show(struct device
*dev
, struct device_attribute
*dummy
,
993 struct i2c_client
*client
= to_i2c_client(dev
);
995 return sprintf(buf
, "%d\n", !!(client
->flags
& I2C_CLIENT_PEC
));
998 static ssize_t
pec_store(struct device
*dev
, struct device_attribute
*dummy
,
999 const char *buf
, size_t count
)
1001 struct i2c_client
*client
= to_i2c_client(dev
);
1005 err
= kstrtol(buf
, 10, &val
);
1011 client
->flags
&= ~I2C_CLIENT_PEC
;
1014 client
->flags
|= I2C_CLIENT_PEC
;
1023 static DEVICE_ATTR_RW(pec
);
1025 static int lm90_get_temp11(struct lm90_data
*data
, int index
)
1027 s16 temp11
= data
->temp11
[index
];
1030 if (data
->flags
& LM90_HAVE_EXTENDED_TEMP
)
1031 temp
= temp_from_u16_adt7461(data
, temp11
);
1032 else if (data
->kind
== max6646
)
1033 temp
= temp_from_u16(temp11
);
1035 temp
= temp_from_s16(temp11
);
1037 /* +16 degrees offset for temp2 for the LM99 */
1038 if (data
->kind
== lm99
&& index
<= 2)
1044 static int lm90_set_temp11(struct lm90_data
*data
, int index
, long val
)
1050 [REMOTE_LOW
] = { LM90_REG_W_REMOTE_LOWH
, LM90_REG_W_REMOTE_LOWL
},
1051 [REMOTE_HIGH
] = { LM90_REG_W_REMOTE_HIGHH
, LM90_REG_W_REMOTE_HIGHL
},
1052 [REMOTE_OFFSET
] = { LM90_REG_W_REMOTE_OFFSH
, LM90_REG_W_REMOTE_OFFSL
},
1053 [REMOTE2_LOW
] = { LM90_REG_W_REMOTE_LOWH
, LM90_REG_W_REMOTE_LOWL
},
1054 [REMOTE2_HIGH
] = { LM90_REG_W_REMOTE_HIGHH
, LM90_REG_W_REMOTE_HIGHL
}
1056 struct i2c_client
*client
= data
->client
;
1057 struct reg
*regp
= ®
[index
];
1060 /* +16 degrees offset for temp2 for the LM99 */
1061 if (data
->kind
== lm99
&& index
<= 2) {
1062 /* prevent integer underflow */
1063 val
= max(val
, -128000l);
1067 if (data
->flags
& LM90_HAVE_EXTENDED_TEMP
)
1068 data
->temp11
[index
] = temp_to_u16_adt7461(data
, val
);
1069 else if (data
->kind
== max6646
)
1070 data
->temp11
[index
] = temp_to_u8(val
) << 8;
1071 else if (data
->flags
& LM90_HAVE_REM_LIMIT_EXT
)
1072 data
->temp11
[index
] = temp_to_s16(val
);
1074 data
->temp11
[index
] = temp_to_s8(val
) << 8;
1076 lm90_select_remote_channel(data
, index
>= 3);
1077 err
= i2c_smbus_write_byte_data(client
, regp
->high
,
1078 data
->temp11
[index
] >> 8);
1081 if (data
->flags
& LM90_HAVE_REM_LIMIT_EXT
)
1082 err
= i2c_smbus_write_byte_data(client
, regp
->low
,
1083 data
->temp11
[index
] & 0xff);
1085 lm90_select_remote_channel(data
, 0);
1089 static int lm90_get_temp8(struct lm90_data
*data
, int index
)
1091 s8 temp8
= data
->temp8
[index
];
1094 if (data
->flags
& LM90_HAVE_EXTENDED_TEMP
)
1095 temp
= temp_from_u8_adt7461(data
, temp8
);
1096 else if (data
->kind
== max6646
)
1097 temp
= temp_from_u8(temp8
);
1099 temp
= temp_from_s8(temp8
);
1101 /* +16 degrees offset for temp2 for the LM99 */
1102 if (data
->kind
== lm99
&& index
== 3)
1108 static int lm90_set_temp8(struct lm90_data
*data
, int index
, long val
)
1110 static const u8 reg
[TEMP8_REG_NUM
] = {
1111 LM90_REG_W_LOCAL_LOW
,
1112 LM90_REG_W_LOCAL_HIGH
,
1113 LM90_REG_W_LOCAL_CRIT
,
1114 LM90_REG_W_REMOTE_CRIT
,
1115 MAX6659_REG_W_LOCAL_EMERG
,
1116 MAX6659_REG_W_REMOTE_EMERG
,
1117 LM90_REG_W_REMOTE_CRIT
,
1118 MAX6659_REG_W_REMOTE_EMERG
,
1120 struct i2c_client
*client
= data
->client
;
1123 /* +16 degrees offset for temp2 for the LM99 */
1124 if (data
->kind
== lm99
&& index
== 3) {
1125 /* prevent integer underflow */
1126 val
= max(val
, -128000l);
1130 if (data
->flags
& LM90_HAVE_EXTENDED_TEMP
)
1131 data
->temp8
[index
] = temp_to_u8_adt7461(data
, val
);
1132 else if (data
->kind
== max6646
)
1133 data
->temp8
[index
] = temp_to_u8(val
);
1135 data
->temp8
[index
] = temp_to_s8(val
);
1137 lm90_select_remote_channel(data
, index
>= 6);
1138 err
= i2c_smbus_write_byte_data(client
, reg
[index
], data
->temp8
[index
]);
1139 lm90_select_remote_channel(data
, 0);
1144 static int lm90_get_temphyst(struct lm90_data
*data
, int index
)
1148 if (data
->flags
& LM90_HAVE_EXTENDED_TEMP
)
1149 temp
= temp_from_u8_adt7461(data
, data
->temp8
[index
]);
1150 else if (data
->kind
== max6646
)
1151 temp
= temp_from_u8(data
->temp8
[index
]);
1153 temp
= temp_from_s8(data
->temp8
[index
]);
1155 /* +16 degrees offset for temp2 for the LM99 */
1156 if (data
->kind
== lm99
&& index
== 3)
1159 return temp
- temp_from_s8(data
->temp_hyst
);
1162 static int lm90_set_temphyst(struct lm90_data
*data
, long val
)
1164 struct i2c_client
*client
= data
->client
;
1168 if (data
->flags
& LM90_HAVE_EXTENDED_TEMP
)
1169 temp
= temp_from_u8_adt7461(data
, data
->temp8
[LOCAL_CRIT
]);
1170 else if (data
->kind
== max6646
)
1171 temp
= temp_from_u8(data
->temp8
[LOCAL_CRIT
]);
1173 temp
= temp_from_s8(data
->temp8
[LOCAL_CRIT
]);
1175 /* prevent integer overflow/underflow */
1176 val
= clamp_val(val
, -128000l, 255000l);
1178 data
->temp_hyst
= hyst_to_reg(temp
- val
);
1179 err
= i2c_smbus_write_byte_data(client
, LM90_REG_W_TCRIT_HYST
,
1184 static const u8 lm90_temp_index
[3] = {
1185 LOCAL_TEMP
, REMOTE_TEMP
, REMOTE2_TEMP
1188 static const u8 lm90_temp_min_index
[3] = {
1189 LOCAL_LOW
, REMOTE_LOW
, REMOTE2_LOW
1192 static const u8 lm90_temp_max_index
[3] = {
1193 LOCAL_HIGH
, REMOTE_HIGH
, REMOTE2_HIGH
1196 static const u8 lm90_temp_crit_index
[3] = {
1197 LOCAL_CRIT
, REMOTE_CRIT
, REMOTE2_CRIT
1200 static const u8 lm90_temp_emerg_index
[3] = {
1201 LOCAL_EMERG
, REMOTE_EMERG
, REMOTE2_EMERG
1204 static const u8 lm90_min_alarm_bits
[3] = { 5, 3, 11 };
1205 static const u8 lm90_max_alarm_bits
[3] = { 6, 4, 12 };
1206 static const u8 lm90_crit_alarm_bits
[3] = { 0, 1, 9 };
1207 static const u8 lm90_crit_alarm_bits_swapped
[3] = { 1, 0, 9 };
1208 static const u8 lm90_emergency_alarm_bits
[3] = { 15, 13, 14 };
1209 static const u8 lm90_fault_bits
[3] = { 0, 2, 10 };
1211 static int lm90_temp_read(struct device
*dev
, u32 attr
, int channel
, long *val
)
1213 struct lm90_data
*data
= dev_get_drvdata(dev
);
1216 mutex_lock(&data
->update_lock
);
1217 err
= lm90_update_device(dev
);
1218 mutex_unlock(&data
->update_lock
);
1223 case hwmon_temp_input
:
1224 *val
= lm90_get_temp11(data
, lm90_temp_index
[channel
]);
1226 case hwmon_temp_min_alarm
:
1227 *val
= (data
->alarms
>> lm90_min_alarm_bits
[channel
]) & 1;
1229 case hwmon_temp_max_alarm
:
1230 *val
= (data
->alarms
>> lm90_max_alarm_bits
[channel
]) & 1;
1232 case hwmon_temp_crit_alarm
:
1233 if (data
->flags
& LM90_HAVE_CRIT_ALRM_SWP
)
1234 *val
= (data
->alarms
>> lm90_crit_alarm_bits_swapped
[channel
]) & 1;
1236 *val
= (data
->alarms
>> lm90_crit_alarm_bits
[channel
]) & 1;
1238 case hwmon_temp_emergency_alarm
:
1239 *val
= (data
->alarms
>> lm90_emergency_alarm_bits
[channel
]) & 1;
1241 case hwmon_temp_fault
:
1242 *val
= (data
->alarms
>> lm90_fault_bits
[channel
]) & 1;
1244 case hwmon_temp_min
:
1246 *val
= lm90_get_temp8(data
,
1247 lm90_temp_min_index
[channel
]);
1249 *val
= lm90_get_temp11(data
,
1250 lm90_temp_min_index
[channel
]);
1252 case hwmon_temp_max
:
1254 *val
= lm90_get_temp8(data
,
1255 lm90_temp_max_index
[channel
]);
1257 *val
= lm90_get_temp11(data
,
1258 lm90_temp_max_index
[channel
]);
1260 case hwmon_temp_crit
:
1261 *val
= lm90_get_temp8(data
, lm90_temp_crit_index
[channel
]);
1263 case hwmon_temp_crit_hyst
:
1264 *val
= lm90_get_temphyst(data
, lm90_temp_crit_index
[channel
]);
1266 case hwmon_temp_emergency
:
1267 *val
= lm90_get_temp8(data
, lm90_temp_emerg_index
[channel
]);
1269 case hwmon_temp_emergency_hyst
:
1270 *val
= lm90_get_temphyst(data
, lm90_temp_emerg_index
[channel
]);
1272 case hwmon_temp_offset
:
1273 *val
= lm90_get_temp11(data
, REMOTE_OFFSET
);
1281 static int lm90_temp_write(struct device
*dev
, u32 attr
, int channel
, long val
)
1283 struct lm90_data
*data
= dev_get_drvdata(dev
);
1286 mutex_lock(&data
->update_lock
);
1288 err
= lm90_update_device(dev
);
1293 case hwmon_temp_min
:
1295 err
= lm90_set_temp8(data
,
1296 lm90_temp_min_index
[channel
],
1299 err
= lm90_set_temp11(data
,
1300 lm90_temp_min_index
[channel
],
1303 case hwmon_temp_max
:
1305 err
= lm90_set_temp8(data
,
1306 lm90_temp_max_index
[channel
],
1309 err
= lm90_set_temp11(data
,
1310 lm90_temp_max_index
[channel
],
1313 case hwmon_temp_crit
:
1314 err
= lm90_set_temp8(data
, lm90_temp_crit_index
[channel
], val
);
1316 case hwmon_temp_crit_hyst
:
1317 err
= lm90_set_temphyst(data
, val
);
1319 case hwmon_temp_emergency
:
1320 err
= lm90_set_temp8(data
, lm90_temp_emerg_index
[channel
], val
);
1322 case hwmon_temp_offset
:
1323 err
= lm90_set_temp11(data
, REMOTE_OFFSET
, val
);
1330 mutex_unlock(&data
->update_lock
);
1335 static umode_t
lm90_temp_is_visible(const void *data
, u32 attr
, int channel
)
1338 case hwmon_temp_input
:
1339 case hwmon_temp_min_alarm
:
1340 case hwmon_temp_max_alarm
:
1341 case hwmon_temp_crit_alarm
:
1342 case hwmon_temp_emergency_alarm
:
1343 case hwmon_temp_emergency_hyst
:
1344 case hwmon_temp_fault
:
1346 case hwmon_temp_min
:
1347 case hwmon_temp_max
:
1348 case hwmon_temp_crit
:
1349 case hwmon_temp_emergency
:
1350 case hwmon_temp_offset
:
1352 case hwmon_temp_crit_hyst
:
1361 static int lm90_chip_read(struct device
*dev
, u32 attr
, int channel
, long *val
)
1363 struct lm90_data
*data
= dev_get_drvdata(dev
);
1366 mutex_lock(&data
->update_lock
);
1367 err
= lm90_update_device(dev
);
1368 mutex_unlock(&data
->update_lock
);
1373 case hwmon_chip_update_interval
:
1374 *val
= data
->update_interval
;
1376 case hwmon_chip_alarms
:
1377 *val
= data
->alarms
;
1386 static int lm90_chip_write(struct device
*dev
, u32 attr
, int channel
, long val
)
1388 struct lm90_data
*data
= dev_get_drvdata(dev
);
1389 struct i2c_client
*client
= data
->client
;
1392 mutex_lock(&data
->update_lock
);
1394 err
= lm90_update_device(dev
);
1399 case hwmon_chip_update_interval
:
1400 err
= lm90_set_convrate(client
, data
,
1401 clamp_val(val
, 0, 100000));
1408 mutex_unlock(&data
->update_lock
);
1413 static umode_t
lm90_chip_is_visible(const void *data
, u32 attr
, int channel
)
1416 case hwmon_chip_update_interval
:
1418 case hwmon_chip_alarms
:
1425 static int lm90_read(struct device
*dev
, enum hwmon_sensor_types type
,
1426 u32 attr
, int channel
, long *val
)
1430 return lm90_chip_read(dev
, attr
, channel
, val
);
1432 return lm90_temp_read(dev
, attr
, channel
, val
);
1438 static int lm90_write(struct device
*dev
, enum hwmon_sensor_types type
,
1439 u32 attr
, int channel
, long val
)
1443 return lm90_chip_write(dev
, attr
, channel
, val
);
1445 return lm90_temp_write(dev
, attr
, channel
, val
);
1451 static umode_t
lm90_is_visible(const void *data
, enum hwmon_sensor_types type
,
1452 u32 attr
, int channel
)
1456 return lm90_chip_is_visible(data
, attr
, channel
);
1458 return lm90_temp_is_visible(data
, attr
, channel
);
1464 /* Return 0 if detection is successful, -ENODEV otherwise */
1465 static int lm90_detect(struct i2c_client
*client
,
1466 struct i2c_board_info
*info
)
1468 struct i2c_adapter
*adapter
= client
->adapter
;
1469 int address
= client
->addr
;
1470 const char *name
= NULL
;
1471 int man_id
, chip_id
, config1
, config2
, convrate
;
1473 if (!i2c_check_functionality(adapter
, I2C_FUNC_SMBUS_BYTE_DATA
))
1476 /* detection and identification */
1477 man_id
= i2c_smbus_read_byte_data(client
, LM90_REG_R_MAN_ID
);
1478 chip_id
= i2c_smbus_read_byte_data(client
, LM90_REG_R_CHIP_ID
);
1479 config1
= i2c_smbus_read_byte_data(client
, LM90_REG_R_CONFIG1
);
1480 convrate
= i2c_smbus_read_byte_data(client
, LM90_REG_R_CONVRATE
);
1481 if (man_id
< 0 || chip_id
< 0 || config1
< 0 || convrate
< 0)
1484 if (man_id
== 0x01 || man_id
== 0x5C || man_id
== 0xA1) {
1485 config2
= i2c_smbus_read_byte_data(client
, LM90_REG_R_CONFIG2
);
1490 if ((address
== 0x4C || address
== 0x4D)
1491 && man_id
== 0x01) { /* National Semiconductor */
1492 if ((config1
& 0x2A) == 0x00
1493 && (config2
& 0xF8) == 0x00
1494 && convrate
<= 0x09) {
1496 && (chip_id
& 0xF0) == 0x20) { /* LM90 */
1499 if ((chip_id
& 0xF0) == 0x30) { /* LM89/LM99 */
1501 dev_info(&adapter
->dev
,
1502 "Assuming LM99 chip at 0x%02x\n",
1504 dev_info(&adapter
->dev
,
1505 "If it is an LM89, instantiate it "
1506 "with the new_device sysfs "
1510 && (chip_id
& 0xF0) == 0x10) { /* LM86 */
1515 if ((address
== 0x4C || address
== 0x4D)
1516 && man_id
== 0x41) { /* Analog Devices */
1517 if ((chip_id
& 0xF0) == 0x40 /* ADM1032 */
1518 && (config1
& 0x3F) == 0x00
1519 && convrate
<= 0x0A) {
1522 * The ADM1032 supports PEC, but only if combined
1523 * transactions are not used.
1525 if (i2c_check_functionality(adapter
,
1526 I2C_FUNC_SMBUS_BYTE
))
1527 info
->flags
|= I2C_CLIENT_PEC
;
1529 if (chip_id
== 0x51 /* ADT7461 */
1530 && (config1
& 0x1B) == 0x00
1531 && convrate
<= 0x0A) {
1534 if (chip_id
== 0x57 /* ADT7461A, NCT1008 */
1535 && (config1
& 0x1B) == 0x00
1536 && convrate
<= 0x0A) {
1540 if (man_id
== 0x4D) { /* Maxim */
1541 int emerg
, emerg2
, status2
;
1544 * We read MAX6659_REG_R_REMOTE_EMERG twice, and re-read
1545 * LM90_REG_R_MAN_ID in between. If MAX6659_REG_R_REMOTE_EMERG
1546 * exists, both readings will reflect the same value. Otherwise,
1547 * the readings will be different.
1549 emerg
= i2c_smbus_read_byte_data(client
,
1550 MAX6659_REG_R_REMOTE_EMERG
);
1551 man_id
= i2c_smbus_read_byte_data(client
,
1553 emerg2
= i2c_smbus_read_byte_data(client
,
1554 MAX6659_REG_R_REMOTE_EMERG
);
1555 status2
= i2c_smbus_read_byte_data(client
,
1556 MAX6696_REG_R_STATUS2
);
1557 if (emerg
< 0 || man_id
< 0 || emerg2
< 0 || status2
< 0)
1561 * The MAX6657, MAX6658 and MAX6659 do NOT have a chip_id
1562 * register. Reading from that address will return the last
1563 * read value, which in our case is those of the man_id
1564 * register. Likewise, the config1 register seems to lack a
1565 * low nibble, so the value will be those of the previous
1566 * read, so in our case those of the man_id register.
1567 * MAX6659 has a third set of upper temperature limit registers.
1568 * Those registers also return values on MAX6657 and MAX6658,
1569 * thus the only way to detect MAX6659 is by its address.
1570 * For this reason it will be mis-detected as MAX6657 if its
1573 if (chip_id
== man_id
1574 && (address
== 0x4C || address
== 0x4D || address
== 0x4E)
1575 && (config1
& 0x1F) == (man_id
& 0x0F)
1576 && convrate
<= 0x09) {
1577 if (address
== 0x4C)
1583 * Even though MAX6695 and MAX6696 do not have a chip ID
1584 * register, reading it returns 0x01. Bit 4 of the config1
1585 * register is unused and should return zero when read. Bit 0 of
1586 * the status2 register is unused and should return zero when
1589 * MAX6695 and MAX6696 have an additional set of temperature
1590 * limit registers. We can detect those chips by checking if
1591 * one of those registers exists.
1594 && (config1
& 0x10) == 0x00
1595 && (status2
& 0x01) == 0x00
1597 && convrate
<= 0x07) {
1601 * The chip_id register of the MAX6680 and MAX6681 holds the
1602 * revision of the chip. The lowest bit of the config1 register
1603 * is unused and should return zero when read, so should the
1604 * second to last bit of config1 (software reset).
1607 && (config1
& 0x03) == 0x00
1608 && convrate
<= 0x07) {
1612 * The chip_id register of the MAX6646/6647/6649 holds the
1613 * revision of the chip. The lowest 6 bits of the config1
1614 * register are unused and should return zero when read.
1617 && (config1
& 0x3f) == 0x00
1618 && convrate
<= 0x07) {
1622 * The chip_id of the MAX6654 holds the revision of the chip.
1623 * The lowest 3 bits of the config1 register are unused and
1624 * should return zero when read.
1627 && (config1
& 0x07) == 0x00
1628 && convrate
<= 0x07) {
1633 && man_id
== 0x5C) { /* Winbond/Nuvoton */
1634 if ((config1
& 0x2A) == 0x00
1635 && (config2
& 0xF8) == 0x00) {
1636 if (chip_id
== 0x01 /* W83L771W/G */
1637 && convrate
<= 0x09) {
1640 if ((chip_id
& 0xFE) == 0x10 /* W83L771AWG/ASG */
1641 && convrate
<= 0x08) {
1646 if (address
>= 0x48 && address
<= 0x4F
1647 && man_id
== 0xA1) { /* NXP Semiconductor/Philips */
1649 && (config1
& 0x2A) == 0x00
1650 && (config2
& 0xFE) == 0x00
1651 && convrate
<= 0x09) {
1655 if ((address
== 0x4C || address
== 0x4D)
1656 && man_id
== 0x47) { /* GMT */
1657 if (chip_id
== 0x01 /* G781 */
1658 && (config1
& 0x3F) == 0x00
1659 && convrate
<= 0x08)
1662 if (man_id
== 0x55 && chip_id
== 0x00 &&
1663 (config1
& 0x1B) == 0x00 && convrate
<= 0x09) {
1664 int local_ext
, conalert
, chen
, dfc
;
1666 local_ext
= i2c_smbus_read_byte_data(client
,
1667 TMP451_REG_R_LOCAL_TEMPL
);
1668 conalert
= i2c_smbus_read_byte_data(client
,
1669 TMP451_REG_CONALERT
);
1670 chen
= i2c_smbus_read_byte_data(client
, TMP461_REG_CHEN
);
1671 dfc
= i2c_smbus_read_byte_data(client
, TMP461_REG_DFC
);
1673 if ((local_ext
& 0x0F) == 0x00 &&
1674 (conalert
& 0xf1) == 0x01 &&
1675 (chen
& 0xfc) == 0x00 &&
1676 (dfc
& 0xfc) == 0x00) {
1677 if (address
== 0x4c && !(chen
& 0x03))
1679 else if (address
>= 0x48 && address
<= 0x4f)
1684 if (!name
) { /* identification failed */
1685 dev_dbg(&adapter
->dev
,
1686 "Unsupported chip at 0x%02x (man_id=0x%02X, "
1687 "chip_id=0x%02X)\n", address
, man_id
, chip_id
);
1691 strlcpy(info
->type
, name
, I2C_NAME_SIZE
);
1696 static void lm90_restore_conf(void *_data
)
1698 struct lm90_data
*data
= _data
;
1699 struct i2c_client
*client
= data
->client
;
1701 /* Restore initial configuration */
1702 lm90_write_convrate(data
, data
->convrate_orig
);
1703 i2c_smbus_write_byte_data(client
, LM90_REG_W_CONFIG1
,
1707 static int lm90_init_client(struct i2c_client
*client
, struct lm90_data
*data
)
1709 int config
, convrate
;
1711 convrate
= lm90_read_reg(client
, LM90_REG_R_CONVRATE
);
1714 data
->convrate_orig
= convrate
;
1717 * Start the conversions.
1719 config
= lm90_read_reg(client
, LM90_REG_R_CONFIG1
);
1722 data
->config_orig
= config
;
1723 data
->config
= config
;
1725 lm90_set_convrate(client
, data
, 500); /* 500ms; 2Hz conversion rate */
1727 /* Check Temperature Range Select */
1728 if (data
->flags
& LM90_HAVE_EXTENDED_TEMP
) {
1730 data
->flags
|= LM90_FLAG_ADT7461_EXT
;
1734 * Put MAX6680/MAX8881 into extended resolution (bit 0x10,
1735 * 0.125 degree resolution) and range (0x08, extend range
1736 * to -64 degree) mode for the remote temperature sensor.
1738 if (data
->kind
== max6680
)
1742 * Put MAX6654 into extended range (0x20, extend minimum range from
1743 * 0 degrees to -64 degrees). Note that extended resolution is not
1744 * possible on the MAX6654 unless conversion rate is set to 1 Hz or
1745 * slower, which is intentionally not done by default.
1747 if (data
->kind
== max6654
)
1751 * Select external channel 0 for max6695/96
1753 if (data
->kind
== max6696
)
1757 * Interrupt is enabled by default on reset, but it may be disabled
1758 * by bootloader, unmask it.
1763 config
&= 0xBF; /* run */
1764 lm90_update_confreg(data
, config
);
1766 return devm_add_action_or_reset(&client
->dev
, lm90_restore_conf
, data
);
1769 static bool lm90_is_tripped(struct i2c_client
*client
, u16
*status
)
1771 struct lm90_data
*data
= i2c_get_clientdata(client
);
1774 st
= lm90_read_reg(client
, LM90_REG_R_STATUS
);
1778 if (data
->kind
== max6696
) {
1779 st2
= lm90_read_reg(client
, MAX6696_REG_R_STATUS2
);
1784 *status
= st
| (st2
<< 8);
1786 if ((st
& 0x7f) == 0 && (st2
& 0xfe) == 0)
1789 if ((st
& (LM90_STATUS_LLOW
| LM90_STATUS_LHIGH
| LM90_STATUS_LTHRM
)) ||
1790 (st2
& MAX6696_STATUS2_LOT2
))
1791 dev_dbg(&client
->dev
,
1792 "temp%d out of range, please check!\n", 1);
1793 if ((st
& (LM90_STATUS_RLOW
| LM90_STATUS_RHIGH
| LM90_STATUS_RTHRM
)) ||
1794 (st2
& MAX6696_STATUS2_ROT2
))
1795 dev_dbg(&client
->dev
,
1796 "temp%d out of range, please check!\n", 2);
1797 if (st
& LM90_STATUS_ROPEN
)
1798 dev_dbg(&client
->dev
,
1799 "temp%d diode open, please check!\n", 2);
1800 if (st2
& (MAX6696_STATUS2_R2LOW
| MAX6696_STATUS2_R2HIGH
|
1801 MAX6696_STATUS2_R2THRM
| MAX6696_STATUS2_R2OT2
))
1802 dev_dbg(&client
->dev
,
1803 "temp%d out of range, please check!\n", 3);
1804 if (st2
& MAX6696_STATUS2_R2OPEN
)
1805 dev_dbg(&client
->dev
,
1806 "temp%d diode open, please check!\n", 3);
1808 if (st
& LM90_STATUS_LLOW
)
1809 hwmon_notify_event(data
->hwmon_dev
, hwmon_temp
,
1811 if (st
& LM90_STATUS_RLOW
)
1812 hwmon_notify_event(data
->hwmon_dev
, hwmon_temp
,
1814 if (st2
& MAX6696_STATUS2_R2LOW
)
1815 hwmon_notify_event(data
->hwmon_dev
, hwmon_temp
,
1817 if (st
& LM90_STATUS_LHIGH
)
1818 hwmon_notify_event(data
->hwmon_dev
, hwmon_temp
,
1820 if (st
& LM90_STATUS_RHIGH
)
1821 hwmon_notify_event(data
->hwmon_dev
, hwmon_temp
,
1823 if (st2
& MAX6696_STATUS2_R2HIGH
)
1824 hwmon_notify_event(data
->hwmon_dev
, hwmon_temp
,
1830 static irqreturn_t
lm90_irq_thread(int irq
, void *dev_id
)
1832 struct i2c_client
*client
= dev_id
;
1835 if (lm90_is_tripped(client
, &status
))
1841 static void lm90_remove_pec(void *dev
)
1843 device_remove_file(dev
, &dev_attr_pec
);
1846 static void lm90_regulator_disable(void *regulator
)
1848 regulator_disable(regulator
);
1852 static const struct hwmon_ops lm90_ops
= {
1853 .is_visible
= lm90_is_visible
,
1855 .write
= lm90_write
,
1858 static int lm90_probe(struct i2c_client
*client
)
1860 struct device
*dev
= &client
->dev
;
1861 struct i2c_adapter
*adapter
= client
->adapter
;
1862 struct hwmon_channel_info
*info
;
1863 struct regulator
*regulator
;
1864 struct device
*hwmon_dev
;
1865 struct lm90_data
*data
;
1868 regulator
= devm_regulator_get(dev
, "vcc");
1869 if (IS_ERR(regulator
))
1870 return PTR_ERR(regulator
);
1872 err
= regulator_enable(regulator
);
1874 dev_err(dev
, "Failed to enable regulator: %d\n", err
);
1878 err
= devm_add_action_or_reset(dev
, lm90_regulator_disable
, regulator
);
1882 data
= devm_kzalloc(dev
, sizeof(struct lm90_data
), GFP_KERNEL
);
1886 data
->client
= client
;
1887 i2c_set_clientdata(client
, data
);
1888 mutex_init(&data
->update_lock
);
1890 /* Set the device type */
1891 if (client
->dev
.of_node
)
1892 data
->kind
= (enum chips
)of_device_get_match_data(&client
->dev
);
1894 data
->kind
= i2c_match_id(lm90_id
, client
)->driver_data
;
1895 if (data
->kind
== adm1032
) {
1896 if (!i2c_check_functionality(adapter
, I2C_FUNC_SMBUS_BYTE
))
1897 client
->flags
&= ~I2C_CLIENT_PEC
;
1901 * Different devices have different alarm bits triggering the
1904 data
->alert_alarms
= lm90_params
[data
->kind
].alert_alarms
;
1906 /* Set chip capabilities */
1907 data
->flags
= lm90_params
[data
->kind
].flags
;
1909 data
->chip
.ops
= &lm90_ops
;
1910 data
->chip
.info
= data
->info
;
1912 data
->info
[0] = HWMON_CHANNEL_INFO(chip
,
1913 HWMON_C_REGISTER_TZ
| HWMON_C_UPDATE_INTERVAL
| HWMON_C_ALARMS
);
1914 data
->info
[1] = &data
->temp_info
;
1916 info
= &data
->temp_info
;
1917 info
->type
= hwmon_temp
;
1918 info
->config
= data
->channel_config
;
1920 data
->channel_config
[0] = HWMON_T_INPUT
| HWMON_T_MIN
| HWMON_T_MAX
|
1921 HWMON_T_MIN_ALARM
| HWMON_T_MAX_ALARM
;
1922 data
->channel_config
[1] = HWMON_T_INPUT
| HWMON_T_MIN
| HWMON_T_MAX
|
1923 HWMON_T_MIN_ALARM
| HWMON_T_MAX_ALARM
| HWMON_T_FAULT
;
1925 if (data
->flags
& LM90_HAVE_CRIT
) {
1926 data
->channel_config
[0] |= HWMON_T_CRIT
| HWMON_T_CRIT_ALARM
| HWMON_T_CRIT_HYST
;
1927 data
->channel_config
[1] |= HWMON_T_CRIT
| HWMON_T_CRIT_ALARM
| HWMON_T_CRIT_HYST
;
1930 if (data
->flags
& LM90_HAVE_OFFSET
)
1931 data
->channel_config
[1] |= HWMON_T_OFFSET
;
1933 if (data
->flags
& LM90_HAVE_EMERGENCY
) {
1934 data
->channel_config
[0] |= HWMON_T_EMERGENCY
|
1935 HWMON_T_EMERGENCY_HYST
;
1936 data
->channel_config
[1] |= HWMON_T_EMERGENCY
|
1937 HWMON_T_EMERGENCY_HYST
;
1940 if (data
->flags
& LM90_HAVE_EMERGENCY_ALARM
) {
1941 data
->channel_config
[0] |= HWMON_T_EMERGENCY_ALARM
;
1942 data
->channel_config
[1] |= HWMON_T_EMERGENCY_ALARM
;
1945 if (data
->flags
& LM90_HAVE_TEMP3
) {
1946 data
->channel_config
[2] = HWMON_T_INPUT
|
1947 HWMON_T_MIN
| HWMON_T_MAX
|
1948 HWMON_T_CRIT
| HWMON_T_CRIT_HYST
|
1949 HWMON_T_EMERGENCY
| HWMON_T_EMERGENCY_HYST
|
1950 HWMON_T_MIN_ALARM
| HWMON_T_MAX_ALARM
|
1951 HWMON_T_CRIT_ALARM
| HWMON_T_EMERGENCY_ALARM
|
1955 data
->reg_local_ext
= lm90_params
[data
->kind
].reg_local_ext
;
1957 /* Set maximum conversion rate */
1958 data
->max_convrate
= lm90_params
[data
->kind
].max_convrate
;
1960 /* Initialize the LM90 chip */
1961 err
= lm90_init_client(client
, data
);
1963 dev_err(dev
, "Failed to initialize device\n");
1968 * The 'pec' attribute is attached to the i2c device and thus created
1971 if (client
->flags
& I2C_CLIENT_PEC
) {
1972 err
= device_create_file(dev
, &dev_attr_pec
);
1975 err
= devm_add_action_or_reset(dev
, lm90_remove_pec
, dev
);
1980 hwmon_dev
= devm_hwmon_device_register_with_info(dev
, client
->name
,
1983 if (IS_ERR(hwmon_dev
))
1984 return PTR_ERR(hwmon_dev
);
1986 data
->hwmon_dev
= hwmon_dev
;
1989 dev_dbg(dev
, "IRQ: %d\n", client
->irq
);
1990 err
= devm_request_threaded_irq(dev
, client
->irq
,
1991 NULL
, lm90_irq_thread
,
1992 IRQF_ONESHOT
, "lm90", client
);
1994 dev_err(dev
, "cannot request IRQ %d\n", client
->irq
);
2002 static void lm90_alert(struct i2c_client
*client
, enum i2c_alert_protocol type
,
2007 if (type
!= I2C_PROTOCOL_SMBUS_ALERT
)
2010 if (lm90_is_tripped(client
, &alarms
)) {
2012 * Disable ALERT# output, because these chips don't implement
2013 * SMBus alert correctly; they should only hold the alert line
2016 struct lm90_data
*data
= i2c_get_clientdata(client
);
2018 if ((data
->flags
& LM90_HAVE_BROKEN_ALERT
) &&
2019 (alarms
& data
->alert_alarms
)) {
2020 dev_dbg(&client
->dev
, "Disabling ALERT#\n");
2021 lm90_update_confreg(data
, data
->config
| 0x80);
2024 dev_dbg(&client
->dev
, "Everything OK\n");
2028 static int __maybe_unused
lm90_suspend(struct device
*dev
)
2030 struct lm90_data
*data
= dev_get_drvdata(dev
);
2031 struct i2c_client
*client
= data
->client
;
2034 disable_irq(client
->irq
);
2039 static int __maybe_unused
lm90_resume(struct device
*dev
)
2041 struct lm90_data
*data
= dev_get_drvdata(dev
);
2042 struct i2c_client
*client
= data
->client
;
2045 enable_irq(client
->irq
);
2050 static SIMPLE_DEV_PM_OPS(lm90_pm_ops
, lm90_suspend
, lm90_resume
);
2052 static struct i2c_driver lm90_driver
= {
2053 .class = I2C_CLASS_HWMON
,
2056 .of_match_table
= of_match_ptr(lm90_of_match
),
2059 .probe_new
= lm90_probe
,
2060 .alert
= lm90_alert
,
2061 .id_table
= lm90_id
,
2062 .detect
= lm90_detect
,
2063 .address_list
= normal_i2c
,
2066 module_i2c_driver(lm90_driver
);
2068 MODULE_AUTHOR("Jean Delvare <jdelvare@suse.de>");
2069 MODULE_DESCRIPTION("LM90/ADM1032 driver");
2070 MODULE_LICENSE("GPL");