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
45 * to 2 Hz by design. The driver also supports MAX6690, which is practically
46 * identical to MAX6654.
48 * This driver also supports the MAX6646, MAX6647, MAX6648, MAX6649 and
49 * MAX6692 chips made by Maxim. These are again similar to the LM86,
50 * but they use unsigned temperature values and can report temperatures
51 * from 0 to 145 degrees.
53 * This driver also supports the MAX6680 and MAX6681, two other sensor
54 * chips made by Maxim. These are quite similar to the other Maxim
55 * chips. The MAX6680 and MAX6681 only differ in the pinout so they can
56 * be treated identically.
58 * This driver also supports the MAX6695 and MAX6696, two other sensor
59 * chips made by Maxim. These are also quite similar to other Maxim
60 * chips, but support three temperature sensors instead of two. MAX6695
61 * and MAX6696 only differ in the pinout so they can be treated identically.
63 * This driver also supports ADT7461 and ADT7461A from Analog Devices as well as
64 * NCT1008 from ON Semiconductor. The chips are supported in both compatibility
65 * and extended mode. They are mostly compatible with LM90 except for a data
66 * format difference for the temperature value registers.
68 * This driver also supports ADT7481, ADT7482, and ADT7483 from Analog Devices
69 * / ON Semiconductor. The chips are similar to ADT7461 but support two external
70 * temperature sensors.
72 * This driver also supports NCT72, NCT214, and NCT218 from ON Semiconductor.
73 * The chips are similar to ADT7461/ADT7461A but have full PEC support
76 * This driver also supports the SA56004 from Philips. This device is
77 * pin-compatible with the LM86, the ED/EDP parts are also address-compatible.
79 * This driver also supports the G781 from GMT. This device is compatible
82 * This driver also supports TMP451 and TMP461 from Texas Instruments.
83 * Those devices are supported in both compatibility and extended mode.
84 * They are mostly compatible with ADT7461 except for local temperature
85 * low byte register and max conversion rate.
87 * This driver also supports MAX1617 and various clones such as G767
88 * and NE1617. Such clones will be detected as MAX1617.
90 * This driver also supports NE1618 from Philips. It is similar to NE1617
91 * but supports 11 bit external temperature values.
93 * Since the LM90 was the first chipset supported by this driver, most
94 * comments will refer to this chipset, but are actually general and
95 * concern all supported chipsets, unless mentioned otherwise.
98 #include <linux/bits.h>
99 #include <linux/device.h>
100 #include <linux/err.h>
101 #include <linux/i2c.h>
102 #include <linux/init.h>
103 #include <linux/interrupt.h>
104 #include <linux/jiffies.h>
105 #include <linux/hwmon.h>
106 #include <linux/module.h>
107 #include <linux/mutex.h>
108 #include <linux/of_device.h>
109 #include <linux/regulator/consumer.h>
110 #include <linux/slab.h>
111 #include <linux/workqueue.h>
115 * Address is fully defined internally and cannot be changed except for
116 * MAX6659, MAX6680 and MAX6681.
117 * LM86, LM89, LM90, LM99, ADM1032, ADM1032-1, ADT7461, ADT7461A, MAX6649,
118 * MAX6657, MAX6658, NCT1008 and W83L771 have address 0x4c.
119 * ADM1032-2, ADT7461-2, ADT7461A-2, LM89-1, LM99-1, MAX6646, and NCT1008D
121 * MAX6647 has address 0x4e.
122 * MAX6659 can have address 0x4c, 0x4d or 0x4e.
123 * MAX6654, MAX6680, and MAX6681 can have address 0x18, 0x19, 0x1a, 0x29,
124 * 0x2a, 0x2b, 0x4c, 0x4d or 0x4e.
125 * SA56004 can have address 0x48 through 0x4F.
128 static const unsigned short normal_i2c[] = {
129 0x18, 0x19, 0x1a, 0x29, 0x2a, 0x2b, 0x48, 0x49, 0x4a, 0x4b, 0x4c,
130 0x4d, 0x4e, 0x4f, I2C_CLIENT_END };
132 enum chips { adm1023, adm1032, adt7461, adt7461a, adt7481,
133 g781, lm84, lm90, lm99,
134 max1617, max6642, max6646, max6648, max6654, max6657, max6659, max6680, max6696,
135 nct210, nct72, ne1618, sa56004, tmp451, tmp461, w83l771,
142 #define LM90_REG_MAN_ID 0xFE
143 #define LM90_REG_CHIP_ID 0xFF
144 #define LM90_REG_CONFIG1 0x03
145 #define LM90_REG_CONFIG2 0xBF
146 #define LM90_REG_CONVRATE 0x04
147 #define LM90_REG_STATUS 0x02
148 #define LM90_REG_LOCAL_TEMP 0x00
149 #define LM90_REG_LOCAL_HIGH 0x05
150 #define LM90_REG_LOCAL_LOW 0x06
151 #define LM90_REG_LOCAL_CRIT 0x20
152 #define LM90_REG_REMOTE_TEMPH 0x01
153 #define LM90_REG_REMOTE_TEMPL 0x10
154 #define LM90_REG_REMOTE_OFFSH 0x11
155 #define LM90_REG_REMOTE_OFFSL 0x12
156 #define LM90_REG_REMOTE_HIGHH 0x07
157 #define LM90_REG_REMOTE_HIGHL 0x13
158 #define LM90_REG_REMOTE_LOWH 0x08
159 #define LM90_REG_REMOTE_LOWL 0x14
160 #define LM90_REG_REMOTE_CRIT 0x19
161 #define LM90_REG_TCRIT_HYST 0x21
163 /* MAX6646/6647/6649/6654/6657/6658/6659/6695/6696 registers */
165 #define MAX6657_REG_LOCAL_TEMPL 0x11
166 #define MAX6696_REG_STATUS2 0x12
167 #define MAX6659_REG_REMOTE_EMERG 0x16
168 #define MAX6659_REG_LOCAL_EMERG 0x17
170 /* SA56004 registers */
172 #define SA56004_REG_LOCAL_TEMPL 0x22
174 #define LM90_MAX_CONVRATE_MS 16000 /* Maximum conversion rate in ms */
176 /* TMP451/TMP461 registers */
177 #define TMP451_REG_LOCAL_TEMPL 0x15
178 #define TMP451_REG_CONALERT 0x22
180 #define TMP461_REG_CHEN 0x16
181 #define TMP461_REG_DFC 0x24
183 /* ADT7481 registers */
184 #define ADT7481_REG_STATUS2 0x23
185 #define ADT7481_REG_CONFIG2 0x24
187 #define ADT7481_REG_MAN_ID 0x3e
188 #define ADT7481_REG_CHIP_ID 0x3d
190 /* Device features */
191 #define LM90_HAVE_EXTENDED_TEMP BIT(0) /* extended temperature support */
192 #define LM90_HAVE_OFFSET BIT(1) /* temperature offset register */
193 #define LM90_HAVE_UNSIGNED_TEMP BIT(2) /* temperatures are unsigned */
194 #define LM90_HAVE_REM_LIMIT_EXT BIT(3) /* extended remote limit */
195 #define LM90_HAVE_EMERGENCY BIT(4) /* 3rd upper (emergency) limit */
196 #define LM90_HAVE_EMERGENCY_ALARM BIT(5)/* emergency alarm */
197 #define LM90_HAVE_TEMP3 BIT(6) /* 3rd temperature sensor */
198 #define LM90_HAVE_BROKEN_ALERT BIT(7) /* Broken alert */
199 #define LM90_PAUSE_FOR_CONFIG BIT(8) /* Pause conversion for config */
200 #define LM90_HAVE_CRIT BIT(9) /* Chip supports CRIT/OVERT register */
201 #define LM90_HAVE_CRIT_ALRM_SWP BIT(10) /* critical alarm bits swapped */
202 #define LM90_HAVE_PEC BIT(11) /* Chip supports PEC */
203 #define LM90_HAVE_PARTIAL_PEC BIT(12) /* Partial PEC support (adm1032)*/
204 #define LM90_HAVE_ALARMS BIT(13) /* Create 'alarms' attribute */
205 #define LM90_HAVE_EXT_UNSIGNED BIT(14) /* extended unsigned temperature*/
206 #define LM90_HAVE_LOW BIT(15) /* low limits */
207 #define LM90_HAVE_CONVRATE BIT(16) /* conversion rate */
208 #define LM90_HAVE_REMOTE_EXT BIT(17) /* extended remote temperature */
211 #define LM90_STATUS_LTHRM BIT(0) /* local THERM limit tripped */
212 #define LM90_STATUS_RTHRM BIT(1) /* remote THERM limit tripped */
213 #define LM90_STATUS_ROPEN BIT(2) /* remote is an open circuit */
214 #define LM90_STATUS_RLOW BIT(3) /* remote low temp limit tripped */
215 #define LM90_STATUS_RHIGH BIT(4) /* remote high temp limit tripped */
216 #define LM90_STATUS_LLOW BIT(5) /* local low temp limit tripped */
217 #define LM90_STATUS_LHIGH BIT(6) /* local high temp limit tripped */
218 #define LM90_STATUS_BUSY BIT(7) /* conversion is ongoing */
220 /* MAX6695/6696 and ADT7481 2nd status register */
221 #define MAX6696_STATUS2_R2THRM BIT(1) /* remote2 THERM limit tripped */
222 #define MAX6696_STATUS2_R2OPEN BIT(2) /* remote2 is an open circuit */
223 #define MAX6696_STATUS2_R2LOW BIT(3) /* remote2 low temp limit tripped */
224 #define MAX6696_STATUS2_R2HIGH BIT(4) /* remote2 high temp limit tripped */
225 #define MAX6696_STATUS2_ROT2 BIT(5) /* remote emergency limit tripped */
226 #define MAX6696_STATUS2_R2OT2 BIT(6) /* remote2 emergency limit tripped */
227 #define MAX6696_STATUS2_LOT2 BIT(7) /* local emergency limit tripped */
230 * Driver data (common to all clients)
233 static const struct i2c_device_id lm90_id[] = {
234 { "adm1020", max1617 },
235 { "adm1021", max1617 },
236 { "adm1023", adm1023 },
237 { "adm1032", adm1032 },
238 { "adt7421", adt7461a },
239 { "adt7461", adt7461 },
240 { "adt7461a", adt7461a },
241 { "adt7481", adt7481 },
242 { "adt7482", adt7481 },
243 { "adt7483a", adt7481 },
245 { "gl523sm", max1617 },
251 { "max1617", max1617 },
252 { "max6642", max6642 },
253 { "max6646", max6646 },
254 { "max6647", max6646 },
255 { "max6648", max6648 },
256 { "max6649", max6646 },
257 { "max6654", max6654 },
258 { "max6657", max6657 },
259 { "max6658", max6657 },
260 { "max6659", max6659 },
261 { "max6680", max6680 },
262 { "max6681", max6680 },
263 { "max6690", max6654 },
264 { "max6692", max6648 },
265 { "max6695", max6696 },
266 { "max6696", max6696 },
267 { "mc1066", max1617 },
268 { "nct1008", adt7461a },
269 { "nct210", nct210 },
273 { "ne1618", ne1618 },
274 { "w83l771", w83l771 },
275 { "sa56004", sa56004 },
276 { "thmc10", max1617 },
277 { "tmp451", tmp451 },
278 { "tmp461", tmp461 },
281 MODULE_DEVICE_TABLE(i2c, lm90_id);
283 static const struct of_device_id __maybe_unused lm90_of_match[] = {
285 .compatible = "adi,adm1032",
286 .data = (void *)adm1032
289 .compatible = "adi,adt7461",
290 .data = (void *)adt7461
293 .compatible = "adi,adt7461a",
294 .data = (void *)adt7461a
297 .compatible = "gmt,g781",
301 .compatible = "national,lm90",
305 .compatible = "national,lm86",
309 .compatible = "national,lm89",
313 .compatible = "national,lm99",
317 .compatible = "dallas,max6646",
318 .data = (void *)max6646
321 .compatible = "dallas,max6647",
322 .data = (void *)max6646
325 .compatible = "dallas,max6649",
326 .data = (void *)max6646
329 .compatible = "dallas,max6654",
330 .data = (void *)max6654
333 .compatible = "dallas,max6657",
334 .data = (void *)max6657
337 .compatible = "dallas,max6658",
338 .data = (void *)max6657
341 .compatible = "dallas,max6659",
342 .data = (void *)max6659
345 .compatible = "dallas,max6680",
346 .data = (void *)max6680
349 .compatible = "dallas,max6681",
350 .data = (void *)max6680
353 .compatible = "dallas,max6695",
354 .data = (void *)max6696
357 .compatible = "dallas,max6696",
358 .data = (void *)max6696
361 .compatible = "onnn,nct1008",
362 .data = (void *)adt7461a
365 .compatible = "onnn,nct214",
366 .data = (void *)nct72
369 .compatible = "onnn,nct218",
370 .data = (void *)nct72
373 .compatible = "onnn,nct72",
374 .data = (void *)nct72
377 .compatible = "winbond,w83l771",
378 .data = (void *)w83l771
381 .compatible = "nxp,sa56004",
382 .data = (void *)sa56004
385 .compatible = "ti,tmp451",
386 .data = (void *)tmp451
389 .compatible = "ti,tmp461",
390 .data = (void *)tmp461
394 MODULE_DEVICE_TABLE(of, lm90_of_match);
397 * chip type specific parameters
400 u32 flags; /* Capabilities */
401 u16 alert_alarms; /* Which alarm bits trigger ALERT# */
402 /* Upper 8 bits for max6695/96 */
403 u8 max_convrate; /* Maximum conversion rate register value */
404 u8 resolution; /* 16-bit resolution (default 11 bit) */
405 u8 reg_status2; /* 2nd status register (optional) */
406 u8 reg_local_ext; /* Extended local temp register (optional) */
409 static const struct lm90_params lm90_params[] = {
411 .flags = LM90_HAVE_ALARMS | LM90_HAVE_OFFSET | LM90_HAVE_BROKEN_ALERT
412 | LM90_HAVE_REM_LIMIT_EXT | LM90_HAVE_LOW | LM90_HAVE_CONVRATE
413 | LM90_HAVE_REMOTE_EXT,
414 .alert_alarms = 0x7c,
419 .flags = LM90_HAVE_OFFSET | LM90_HAVE_REM_LIMIT_EXT
420 | LM90_HAVE_BROKEN_ALERT | LM90_HAVE_CRIT
421 | LM90_HAVE_PARTIAL_PEC | LM90_HAVE_ALARMS
422 | LM90_HAVE_LOW | LM90_HAVE_CONVRATE | LM90_HAVE_REMOTE_EXT,
423 .alert_alarms = 0x7c,
428 * Standard temperature range is supposed to be unsigned,
429 * but that does not match reality. Negative temperatures
430 * are always reported.
432 .flags = LM90_HAVE_OFFSET | LM90_HAVE_REM_LIMIT_EXT
433 | LM90_HAVE_BROKEN_ALERT | LM90_HAVE_EXTENDED_TEMP
434 | LM90_HAVE_CRIT | LM90_HAVE_PARTIAL_PEC
435 | LM90_HAVE_ALARMS | LM90_HAVE_LOW | LM90_HAVE_CONVRATE
436 | LM90_HAVE_REMOTE_EXT,
437 .alert_alarms = 0x7c,
442 .flags = LM90_HAVE_OFFSET | LM90_HAVE_REM_LIMIT_EXT
443 | LM90_HAVE_BROKEN_ALERT | LM90_HAVE_EXTENDED_TEMP
444 | LM90_HAVE_CRIT | LM90_HAVE_PEC | LM90_HAVE_ALARMS
445 | LM90_HAVE_LOW | LM90_HAVE_CONVRATE | LM90_HAVE_REMOTE_EXT,
446 .alert_alarms = 0x7c,
450 .flags = LM90_HAVE_OFFSET | LM90_HAVE_REM_LIMIT_EXT
451 | LM90_HAVE_BROKEN_ALERT | LM90_HAVE_EXTENDED_TEMP
452 | LM90_HAVE_UNSIGNED_TEMP | LM90_HAVE_PEC
453 | LM90_HAVE_TEMP3 | LM90_HAVE_CRIT | LM90_HAVE_LOW
454 | LM90_HAVE_CONVRATE | LM90_HAVE_REMOTE_EXT,
455 .alert_alarms = 0x1c7c,
458 .reg_status2 = ADT7481_REG_STATUS2,
461 .flags = LM90_HAVE_OFFSET | LM90_HAVE_REM_LIMIT_EXT
462 | LM90_HAVE_BROKEN_ALERT | LM90_HAVE_CRIT
463 | LM90_HAVE_ALARMS | LM90_HAVE_LOW | LM90_HAVE_CONVRATE
464 | LM90_HAVE_REMOTE_EXT,
465 .alert_alarms = 0x7c,
469 .flags = LM90_HAVE_ALARMS,
473 .flags = LM90_HAVE_OFFSET | LM90_HAVE_REM_LIMIT_EXT
474 | LM90_HAVE_CRIT | LM90_HAVE_ALARMS | LM90_HAVE_LOW
475 | LM90_HAVE_CONVRATE | LM90_HAVE_REMOTE_EXT,
476 .alert_alarms = 0x7b,
480 .flags = LM90_HAVE_OFFSET | LM90_HAVE_REM_LIMIT_EXT
481 | LM90_HAVE_CRIT | LM90_HAVE_ALARMS | LM90_HAVE_LOW
482 | LM90_HAVE_CONVRATE | LM90_HAVE_REMOTE_EXT,
483 .alert_alarms = 0x7b,
487 .flags = LM90_HAVE_CONVRATE | LM90_HAVE_BROKEN_ALERT |
488 LM90_HAVE_LOW | LM90_HAVE_ALARMS,
489 .alert_alarms = 0x78,
494 .flags = LM90_HAVE_BROKEN_ALERT | LM90_HAVE_EXT_UNSIGNED
495 | LM90_HAVE_REMOTE_EXT,
496 .alert_alarms = 0x50,
498 .reg_local_ext = MAX6657_REG_LOCAL_TEMPL,
501 .flags = LM90_HAVE_CRIT | LM90_HAVE_BROKEN_ALERT
502 | LM90_HAVE_EXT_UNSIGNED | LM90_HAVE_ALARMS | LM90_HAVE_LOW
503 | LM90_HAVE_CONVRATE | LM90_HAVE_REMOTE_EXT,
504 .alert_alarms = 0x7c,
506 .reg_local_ext = MAX6657_REG_LOCAL_TEMPL,
509 .flags = LM90_HAVE_UNSIGNED_TEMP | LM90_HAVE_CRIT
510 | LM90_HAVE_BROKEN_ALERT | LM90_HAVE_LOW
511 | LM90_HAVE_CONVRATE | LM90_HAVE_REMOTE_EXT,
512 .alert_alarms = 0x7c,
514 .reg_local_ext = MAX6657_REG_LOCAL_TEMPL,
517 .flags = LM90_HAVE_BROKEN_ALERT | LM90_HAVE_ALARMS | LM90_HAVE_LOW
518 | LM90_HAVE_CONVRATE | LM90_HAVE_REMOTE_EXT,
519 .alert_alarms = 0x7c,
521 .reg_local_ext = MAX6657_REG_LOCAL_TEMPL,
524 .flags = LM90_PAUSE_FOR_CONFIG | LM90_HAVE_CRIT
525 | LM90_HAVE_ALARMS | LM90_HAVE_LOW | LM90_HAVE_CONVRATE
526 | LM90_HAVE_REMOTE_EXT,
527 .alert_alarms = 0x7c,
529 .reg_local_ext = MAX6657_REG_LOCAL_TEMPL,
532 .flags = LM90_HAVE_EMERGENCY | LM90_HAVE_CRIT
533 | LM90_HAVE_ALARMS | LM90_HAVE_LOW | LM90_HAVE_CONVRATE
534 | LM90_HAVE_REMOTE_EXT,
535 .alert_alarms = 0x7c,
537 .reg_local_ext = MAX6657_REG_LOCAL_TEMPL,
541 * Apparent temperatures of 128 degrees C or higher are reported
542 * and treated as negative temperatures (meaning min_alarm will
545 .flags = LM90_HAVE_OFFSET | LM90_HAVE_CRIT
546 | LM90_HAVE_CRIT_ALRM_SWP | LM90_HAVE_BROKEN_ALERT
547 | LM90_HAVE_ALARMS | LM90_HAVE_LOW | LM90_HAVE_CONVRATE
548 | LM90_HAVE_REMOTE_EXT,
549 .alert_alarms = 0x7c,
553 .flags = LM90_HAVE_EMERGENCY
554 | LM90_HAVE_EMERGENCY_ALARM | LM90_HAVE_TEMP3 | LM90_HAVE_CRIT
555 | LM90_HAVE_ALARMS | LM90_HAVE_LOW | LM90_HAVE_CONVRATE
556 | LM90_HAVE_REMOTE_EXT,
557 .alert_alarms = 0x1c7c,
559 .reg_status2 = MAX6696_REG_STATUS2,
560 .reg_local_ext = MAX6657_REG_LOCAL_TEMPL,
563 .flags = LM90_HAVE_OFFSET | LM90_HAVE_REM_LIMIT_EXT
564 | LM90_HAVE_BROKEN_ALERT | LM90_HAVE_EXTENDED_TEMP
565 | LM90_HAVE_CRIT | LM90_HAVE_PEC | LM90_HAVE_UNSIGNED_TEMP
566 | LM90_HAVE_LOW | LM90_HAVE_CONVRATE | LM90_HAVE_REMOTE_EXT,
567 .alert_alarms = 0x7c,
572 .flags = LM90_HAVE_ALARMS | LM90_HAVE_BROKEN_ALERT
573 | LM90_HAVE_REM_LIMIT_EXT | LM90_HAVE_LOW | LM90_HAVE_CONVRATE
574 | LM90_HAVE_REMOTE_EXT,
575 .alert_alarms = 0x7c,
580 .flags = LM90_PAUSE_FOR_CONFIG | LM90_HAVE_BROKEN_ALERT
581 | LM90_HAVE_LOW | LM90_HAVE_CONVRATE | LM90_HAVE_REMOTE_EXT,
582 .alert_alarms = 0x7c,
587 .flags = LM90_HAVE_OFFSET | LM90_HAVE_REM_LIMIT_EXT | LM90_HAVE_CRIT
588 | LM90_HAVE_ALARMS | LM90_HAVE_LOW | LM90_HAVE_CONVRATE
589 | LM90_HAVE_REMOTE_EXT,
590 .alert_alarms = 0x7c,
595 * Apparent temperatures of 128 degrees C or higher are reported
596 * and treated as negative temperatures (meaning min_alarm will
599 .flags = LM90_HAVE_OFFSET | LM90_HAVE_REM_LIMIT_EXT | LM90_HAVE_CRIT
600 | LM90_HAVE_ALARMS | LM90_HAVE_LOW | LM90_HAVE_CONVRATE
601 | LM90_HAVE_REMOTE_EXT,
602 .alert_alarms = 0x7b,
604 .reg_local_ext = SA56004_REG_LOCAL_TEMPL,
607 .flags = LM90_HAVE_OFFSET | LM90_HAVE_REM_LIMIT_EXT
608 | LM90_HAVE_BROKEN_ALERT | LM90_HAVE_EXTENDED_TEMP | LM90_HAVE_CRIT
609 | LM90_HAVE_UNSIGNED_TEMP | LM90_HAVE_ALARMS | LM90_HAVE_LOW
610 | LM90_HAVE_CONVRATE | LM90_HAVE_REMOTE_EXT,
611 .alert_alarms = 0x7c,
614 .reg_local_ext = TMP451_REG_LOCAL_TEMPL,
617 .flags = LM90_HAVE_OFFSET | LM90_HAVE_REM_LIMIT_EXT
618 | LM90_HAVE_BROKEN_ALERT | LM90_HAVE_EXTENDED_TEMP | LM90_HAVE_CRIT
619 | LM90_HAVE_ALARMS | LM90_HAVE_LOW | LM90_HAVE_CONVRATE
620 | LM90_HAVE_REMOTE_EXT,
621 .alert_alarms = 0x7c,
624 .reg_local_ext = TMP451_REG_LOCAL_TEMPL,
629 * temperature register index
631 enum lm90_temp_reg_index {
636 LOCAL_EMERG, /* max6659 and max6695/96 */
637 REMOTE_EMERG, /* max6659 and max6695/96 */
638 REMOTE2_CRIT, /* max6695/96 only */
639 REMOTE2_EMERG, /* max6695/96 only */
644 REMOTE_OFFSET, /* except max6646, max6657/58/59, and max6695/96 */
646 REMOTE2_TEMP, /* max6695/96 only */
647 REMOTE2_LOW, /* max6695/96 only */
648 REMOTE2_HIGH, /* max6695/96 only */
654 * Client data (each client gets its own)
658 struct i2c_client *client;
659 struct device *hwmon_dev;
661 u32 channel_config[4];
662 struct hwmon_channel_info chip_info;
663 struct hwmon_channel_info temp_info;
664 const struct hwmon_channel_info *info[3];
665 struct hwmon_chip_info chip;
666 struct mutex update_lock;
667 struct delayed_work alert_work;
668 bool valid; /* true if register values are valid */
669 bool alarms_valid; /* true if status register values are valid */
670 unsigned long last_updated; /* in jiffies */
671 unsigned long alarms_updated; /* in jiffies */
675 unsigned int update_interval; /* in milliseconds */
677 u8 config; /* Current configuration register value */
678 u8 config_orig; /* Original configuration register value */
679 u8 convrate_orig; /* Original conversion rate register value */
680 u8 resolution; /* temperature resolution in bit */
681 u16 alert_alarms; /* Which alarm bits trigger ALERT# */
682 /* Upper 8 bits for max6695/96 */
683 u8 max_convrate; /* Maximum conversion rate */
684 u8 reg_status2; /* 2nd status register (optional) */
685 u8 reg_local_ext; /* local extension register offset */
686 u8 reg_remote_ext; /* remote temperature low byte */
688 /* registers values */
689 u16 temp[TEMP_REG_NUM];
691 u16 reported_alarms; /* alarms reported as sysfs/udev events */
692 u16 current_alarms; /* current alarms, reported by chip */
693 u16 alarms; /* alarms not yet reported to user */
701 * If the chip supports PEC but not on write byte transactions, we need
702 * to explicitly ask for a transaction without PEC.
704 static inline s32 lm90_write_no_pec(struct i2c_client *client, u8 value)
706 return i2c_smbus_xfer(client->adapter, client->addr,
707 client->flags & ~I2C_CLIENT_PEC,
708 I2C_SMBUS_WRITE, value, I2C_SMBUS_BYTE, NULL);
712 * It is assumed that client->update_lock is held (unless we are in
713 * detection or initialization steps). This matters when PEC is enabled
714 * for chips with partial PEC support, because we don't want the address
715 * pointer to change between the write byte and the read byte transactions.
717 static int lm90_read_reg(struct i2c_client *client, u8 reg)
719 struct lm90_data *data = i2c_get_clientdata(client);
720 bool partial_pec = (client->flags & I2C_CLIENT_PEC) &&
721 (data->flags & LM90_HAVE_PARTIAL_PEC);
725 err = lm90_write_no_pec(client, reg);
728 return i2c_smbus_read_byte(client);
730 return i2c_smbus_read_byte_data(client, reg);
734 * Return register write address
736 * The write address for registers 0x03 .. 0x08 is the read address plus 6.
737 * For other registers the write address matches the read address.
739 static u8 lm90_write_reg_addr(u8 reg)
741 if (reg >= LM90_REG_CONFIG1 && reg <= LM90_REG_REMOTE_LOWH)
747 * Write into LM90 register.
748 * Convert register address to write address if needed, then execute the
751 static int lm90_write_reg(struct i2c_client *client, u8 reg, u8 val)
753 return i2c_smbus_write_byte_data(client, lm90_write_reg_addr(reg), val);
757 * Write into 16-bit LM90 register.
758 * Convert register addresses to write address if needed, then execute the
761 static int lm90_write16(struct i2c_client *client, u8 regh, u8 regl, u16 val)
765 ret = lm90_write_reg(client, regh, val >> 8);
766 if (ret < 0 || !regl)
768 return lm90_write_reg(client, regl, val & 0xff);
771 static int lm90_read16(struct i2c_client *client, u8 regh, u8 regl,
776 oldh = lm90_read_reg(client, regh);
783 l = lm90_read_reg(client, regl);
788 return (oldh << 8) | l;
791 * For volatile registers we have to use a trick.
792 * We have to read two registers to have the sensor temperature,
793 * but we have to beware a conversion could occur between the
794 * readings. The datasheet says we should either use
795 * the one-shot conversion register, which we don't want to do
796 * (disables hardware monitoring) or monitor the busy bit, which is
797 * impossible (we can't read the values and monitor that bit at the
798 * exact same time). So the solution used here is to read the high
799 * the high byte again. If the new high byte matches the old one,
800 * then we have a valid reading. Otherwise we have to read the low
801 * byte again, and now we believe we have a correct reading.
803 newh = lm90_read_reg(client, regh);
807 l = lm90_read_reg(client, regl);
811 return (newh << 8) | l;
814 static int lm90_update_confreg(struct lm90_data *data, u8 config)
816 if (data->config != config) {
819 err = lm90_write_reg(data->client, LM90_REG_CONFIG1, config);
822 data->config = config;
828 * client->update_lock must be held when calling this function (unless we are
829 * in detection or initialization steps), and while a remote channel other
830 * than channel 0 is selected. Also, calling code must make sure to re-select
831 * external channel 0 before releasing the lock. This is necessary because
832 * various registers have different meanings as a result of selecting a
833 * non-default remote channel.
835 static int lm90_select_remote_channel(struct lm90_data *data, bool second)
837 u8 config = data->config & ~0x08;
842 return lm90_update_confreg(data, config);
845 static int lm90_write_convrate(struct lm90_data *data, int val)
847 u8 config = data->config;
850 /* Save config and pause conversion */
851 if (data->flags & LM90_PAUSE_FOR_CONFIG) {
852 err = lm90_update_confreg(data, config | 0x40);
858 err = lm90_write_reg(data->client, LM90_REG_CONVRATE, val);
860 /* Revert change to config */
861 lm90_update_confreg(data, config);
867 * Set conversion rate.
868 * client->update_lock must be held when calling this function (unless we are
869 * in detection or initialization steps).
871 static int lm90_set_convrate(struct i2c_client *client, struct lm90_data *data,
872 unsigned int interval)
874 unsigned int update_interval;
877 /* Shift calculations to avoid rounding errors */
880 /* find the nearest update rate */
881 for (i = 0, update_interval = LM90_MAX_CONVRATE_MS << 6;
882 i < data->max_convrate; i++, update_interval >>= 1)
883 if (interval >= update_interval * 3 / 4)
886 err = lm90_write_convrate(data, i);
887 data->update_interval = DIV_ROUND_CLOSEST(update_interval, 64);
891 static int lm90_update_limits(struct device *dev)
893 struct lm90_data *data = dev_get_drvdata(dev);
894 struct i2c_client *client = data->client;
897 if (data->flags & LM90_HAVE_CRIT) {
898 val = lm90_read_reg(client, LM90_REG_LOCAL_CRIT);
901 data->temp[LOCAL_CRIT] = val << 8;
903 val = lm90_read_reg(client, LM90_REG_REMOTE_CRIT);
906 data->temp[REMOTE_CRIT] = val << 8;
908 val = lm90_read_reg(client, LM90_REG_TCRIT_HYST);
911 data->temp_hyst = val;
914 val = lm90_read16(client, LM90_REG_REMOTE_LOWH,
915 (data->flags & LM90_HAVE_REM_LIMIT_EXT) ? LM90_REG_REMOTE_LOWL : 0,
919 data->temp[REMOTE_LOW] = val;
921 val = lm90_read16(client, LM90_REG_REMOTE_HIGHH,
922 (data->flags & LM90_HAVE_REM_LIMIT_EXT) ? LM90_REG_REMOTE_HIGHL : 0,
926 data->temp[REMOTE_HIGH] = val;
928 if (data->flags & LM90_HAVE_OFFSET) {
929 val = lm90_read16(client, LM90_REG_REMOTE_OFFSH,
930 LM90_REG_REMOTE_OFFSL, false);
933 data->temp[REMOTE_OFFSET] = val;
936 if (data->flags & LM90_HAVE_EMERGENCY) {
937 val = lm90_read_reg(client, MAX6659_REG_LOCAL_EMERG);
940 data->temp[LOCAL_EMERG] = val << 8;
942 val = lm90_read_reg(client, MAX6659_REG_REMOTE_EMERG);
945 data->temp[REMOTE_EMERG] = val << 8;
948 if (data->flags & LM90_HAVE_TEMP3) {
949 val = lm90_select_remote_channel(data, true);
953 val = lm90_read_reg(client, LM90_REG_REMOTE_CRIT);
956 data->temp[REMOTE2_CRIT] = val << 8;
958 if (data->flags & LM90_HAVE_EMERGENCY) {
959 val = lm90_read_reg(client, MAX6659_REG_REMOTE_EMERG);
962 data->temp[REMOTE2_EMERG] = val << 8;
965 val = lm90_read_reg(client, LM90_REG_REMOTE_LOWH);
968 data->temp[REMOTE2_LOW] = val << 8;
970 val = lm90_read_reg(client, LM90_REG_REMOTE_HIGHH);
973 data->temp[REMOTE2_HIGH] = val << 8;
975 lm90_select_remote_channel(data, false);
981 static void lm90_report_alarms(struct device *dev, struct lm90_data *data)
983 u16 cleared_alarms = data->reported_alarms & ~data->current_alarms;
984 u16 new_alarms = data->current_alarms & ~data->reported_alarms;
985 struct device *hwmon_dev = data->hwmon_dev;
988 if (!cleared_alarms && !new_alarms)
991 st = new_alarms & 0xff;
992 st2 = new_alarms >> 8;
994 if ((st & (LM90_STATUS_LLOW | LM90_STATUS_LHIGH | LM90_STATUS_LTHRM)) ||
995 (st2 & MAX6696_STATUS2_LOT2))
996 dev_dbg(dev, "temp%d out of range, please check!\n", 1);
997 if ((st & (LM90_STATUS_RLOW | LM90_STATUS_RHIGH | LM90_STATUS_RTHRM)) ||
998 (st2 & MAX6696_STATUS2_ROT2))
999 dev_dbg(dev, "temp%d out of range, please check!\n", 2);
1000 if (st & LM90_STATUS_ROPEN)
1001 dev_dbg(dev, "temp%d diode open, please check!\n", 2);
1002 if (st2 & (MAX6696_STATUS2_R2LOW | MAX6696_STATUS2_R2HIGH |
1003 MAX6696_STATUS2_R2THRM | MAX6696_STATUS2_R2OT2))
1004 dev_dbg(dev, "temp%d out of range, please check!\n", 3);
1005 if (st2 & MAX6696_STATUS2_R2OPEN)
1006 dev_dbg(dev, "temp%d diode open, please check!\n", 3);
1008 st |= cleared_alarms & 0xff;
1009 st2 |= cleared_alarms >> 8;
1011 if (st & LM90_STATUS_LLOW)
1012 hwmon_notify_event(hwmon_dev, hwmon_temp, hwmon_temp_min_alarm, 0);
1013 if (st & LM90_STATUS_RLOW)
1014 hwmon_notify_event(hwmon_dev, hwmon_temp, hwmon_temp_min_alarm, 1);
1015 if (st2 & MAX6696_STATUS2_R2LOW)
1016 hwmon_notify_event(hwmon_dev, hwmon_temp, hwmon_temp_min_alarm, 2);
1018 if (st & LM90_STATUS_LHIGH)
1019 hwmon_notify_event(hwmon_dev, hwmon_temp, hwmon_temp_max_alarm, 0);
1020 if (st & LM90_STATUS_RHIGH)
1021 hwmon_notify_event(hwmon_dev, hwmon_temp, hwmon_temp_max_alarm, 1);
1022 if (st2 & MAX6696_STATUS2_R2HIGH)
1023 hwmon_notify_event(hwmon_dev, hwmon_temp, hwmon_temp_max_alarm, 2);
1025 if (st & LM90_STATUS_LTHRM)
1026 hwmon_notify_event(hwmon_dev, hwmon_temp, hwmon_temp_crit_alarm, 0);
1027 if (st & LM90_STATUS_RTHRM)
1028 hwmon_notify_event(hwmon_dev, hwmon_temp, hwmon_temp_crit_alarm, 1);
1029 if (st2 & MAX6696_STATUS2_R2THRM)
1030 hwmon_notify_event(hwmon_dev, hwmon_temp, hwmon_temp_crit_alarm, 2);
1032 if (st2 & MAX6696_STATUS2_LOT2)
1033 hwmon_notify_event(hwmon_dev, hwmon_temp, hwmon_temp_emergency_alarm, 0);
1034 if (st2 & MAX6696_STATUS2_ROT2)
1035 hwmon_notify_event(hwmon_dev, hwmon_temp, hwmon_temp_emergency_alarm, 1);
1036 if (st2 & MAX6696_STATUS2_R2OT2)
1037 hwmon_notify_event(hwmon_dev, hwmon_temp, hwmon_temp_emergency_alarm, 2);
1039 data->reported_alarms = data->current_alarms;
1042 static int lm90_update_alarms_locked(struct lm90_data *data, bool force)
1044 if (force || !data->alarms_valid ||
1045 time_after(jiffies, data->alarms_updated + msecs_to_jiffies(data->update_interval))) {
1046 struct i2c_client *client = data->client;
1051 data->alarms_valid = false;
1053 val = lm90_read_reg(client, LM90_REG_STATUS);
1056 alarms = val & ~LM90_STATUS_BUSY;
1058 if (data->reg_status2) {
1059 val = lm90_read_reg(client, data->reg_status2);
1065 * If the update is forced (called from interrupt or alert
1066 * handler) and alarm data is valid, the alarms may have been
1067 * updated after the last update interval, and the status
1068 * register may still be cleared. Only add additional alarms
1069 * in this case. Alarms will be cleared later if appropriate.
1071 if (force && data->alarms_valid)
1072 data->current_alarms |= alarms;
1074 data->current_alarms = alarms;
1075 data->alarms |= alarms;
1077 check_enable = (client->irq || !(data->config_orig & 0x80)) &&
1078 (data->config & 0x80);
1080 if (force || check_enable)
1081 lm90_report_alarms(&client->dev, data);
1084 * Re-enable ALERT# output if it was originally enabled, relevant
1085 * alarms are all clear, and alerts are currently disabled.
1086 * Otherwise (re)schedule worker if needed.
1089 if (!(data->current_alarms & data->alert_alarms)) {
1090 dev_dbg(&client->dev, "Re-enabling ALERT#\n");
1091 lm90_update_confreg(data, data->config & ~0x80);
1093 * We may have been called from the update handler.
1094 * If so, the worker, if scheduled, is no longer
1095 * needed. Cancel it. Don't synchronize because
1096 * it may already be running.
1098 cancel_delayed_work(&data->alert_work);
1100 schedule_delayed_work(&data->alert_work,
1101 max_t(int, HZ, msecs_to_jiffies(data->update_interval)));
1104 data->alarms_updated = jiffies;
1105 data->alarms_valid = true;
1110 static int lm90_update_alarms(struct lm90_data *data, bool force)
1114 mutex_lock(&data->update_lock);
1115 err = lm90_update_alarms_locked(data, force);
1116 mutex_unlock(&data->update_lock);
1121 static void lm90_alert_work(struct work_struct *__work)
1123 struct delayed_work *delayed_work = container_of(__work, struct delayed_work, work);
1124 struct lm90_data *data = container_of(delayed_work, struct lm90_data, alert_work);
1126 /* Nothing to do if alerts are enabled */
1127 if (!(data->config & 0x80))
1130 lm90_update_alarms(data, true);
1133 static int lm90_update_device(struct device *dev)
1135 struct lm90_data *data = dev_get_drvdata(dev);
1136 struct i2c_client *client = data->client;
1137 unsigned long next_update;
1141 val = lm90_update_limits(dev);
1146 next_update = data->last_updated +
1147 msecs_to_jiffies(data->update_interval);
1148 if (time_after(jiffies, next_update) || !data->valid) {
1149 dev_dbg(&client->dev, "Updating lm90 data.\n");
1151 data->valid = false;
1153 val = lm90_read_reg(client, LM90_REG_LOCAL_LOW);
1156 data->temp[LOCAL_LOW] = val << 8;
1158 val = lm90_read_reg(client, LM90_REG_LOCAL_HIGH);
1161 data->temp[LOCAL_HIGH] = val << 8;
1163 val = lm90_read16(client, LM90_REG_LOCAL_TEMP,
1164 data->reg_local_ext, true);
1167 data->temp[LOCAL_TEMP] = val;
1168 val = lm90_read16(client, LM90_REG_REMOTE_TEMPH,
1169 data->reg_remote_ext, true);
1172 data->temp[REMOTE_TEMP] = val;
1174 if (data->flags & LM90_HAVE_TEMP3) {
1175 val = lm90_select_remote_channel(data, true);
1179 val = lm90_read16(client, LM90_REG_REMOTE_TEMPH,
1180 data->reg_remote_ext, true);
1182 lm90_select_remote_channel(data, false);
1185 data->temp[REMOTE2_TEMP] = val;
1187 lm90_select_remote_channel(data, false);
1190 val = lm90_update_alarms_locked(data, false);
1194 data->last_updated = jiffies;
1201 /* pec used for devices with PEC support */
1202 static ssize_t pec_show(struct device *dev, struct device_attribute *dummy,
1205 struct i2c_client *client = to_i2c_client(dev);
1207 return sprintf(buf, "%d\n", !!(client->flags & I2C_CLIENT_PEC));
1210 static ssize_t pec_store(struct device *dev, struct device_attribute *dummy,
1211 const char *buf, size_t count)
1213 struct i2c_client *client = to_i2c_client(dev);
1217 err = kstrtol(buf, 10, &val);
1223 client->flags &= ~I2C_CLIENT_PEC;
1226 client->flags |= I2C_CLIENT_PEC;
1235 static DEVICE_ATTR_RW(pec);
1237 static int lm90_temp_get_resolution(struct lm90_data *data, int index)
1241 if (data->reg_remote_ext)
1242 return data->resolution;
1246 return data->resolution;
1248 if (data->reg_local_ext)
1249 return data->resolution;
1255 if (data->flags & LM90_HAVE_REM_LIMIT_EXT)
1256 return data->resolution;
1263 static int lm90_temp_from_reg(u32 flags, u16 regval, u8 resolution)
1267 if (flags & LM90_HAVE_EXTENDED_TEMP)
1268 val = regval - 0x4000;
1269 else if (flags & (LM90_HAVE_UNSIGNED_TEMP | LM90_HAVE_EXT_UNSIGNED))
1274 return ((val >> (16 - resolution)) * 1000) >> (resolution - 8);
1277 static int lm90_get_temp(struct lm90_data *data, int index, int channel)
1279 int temp = lm90_temp_from_reg(data->flags, data->temp[index],
1280 lm90_temp_get_resolution(data, index));
1282 /* +16 degrees offset for remote temperature on LM99 */
1283 if (data->kind == lm99 && channel)
1289 static u16 lm90_temp_to_reg(u32 flags, long val, u8 resolution)
1291 int fraction = resolution > 8 ?
1292 1000 - DIV_ROUND_CLOSEST(1000, BIT(resolution - 8)) : 0;
1294 if (flags & LM90_HAVE_EXTENDED_TEMP) {
1295 val = clamp_val(val, -64000, 191000 + fraction);
1297 } else if (flags & LM90_HAVE_EXT_UNSIGNED) {
1298 val = clamp_val(val, 0, 255000 + fraction);
1299 } else if (flags & LM90_HAVE_UNSIGNED_TEMP) {
1300 val = clamp_val(val, 0, 127000 + fraction);
1302 val = clamp_val(val, -128000, 127000 + fraction);
1305 return DIV_ROUND_CLOSEST(val << (resolution - 8), 1000) << (16 - resolution);
1308 static int lm90_set_temp(struct lm90_data *data, int index, int channel, long val)
1310 static const u8 regs[] = {
1311 [LOCAL_LOW] = LM90_REG_LOCAL_LOW,
1312 [LOCAL_HIGH] = LM90_REG_LOCAL_HIGH,
1313 [LOCAL_CRIT] = LM90_REG_LOCAL_CRIT,
1314 [REMOTE_CRIT] = LM90_REG_REMOTE_CRIT,
1315 [LOCAL_EMERG] = MAX6659_REG_LOCAL_EMERG,
1316 [REMOTE_EMERG] = MAX6659_REG_REMOTE_EMERG,
1317 [REMOTE2_CRIT] = LM90_REG_REMOTE_CRIT,
1318 [REMOTE2_EMERG] = MAX6659_REG_REMOTE_EMERG,
1319 [REMOTE_LOW] = LM90_REG_REMOTE_LOWH,
1320 [REMOTE_HIGH] = LM90_REG_REMOTE_HIGHH,
1321 [REMOTE2_LOW] = LM90_REG_REMOTE_LOWH,
1322 [REMOTE2_HIGH] = LM90_REG_REMOTE_HIGHH,
1324 struct i2c_client *client = data->client;
1325 u8 regh = regs[index];
1329 if (channel && (data->flags & LM90_HAVE_REM_LIMIT_EXT)) {
1330 if (index == REMOTE_LOW || index == REMOTE2_LOW)
1331 regl = LM90_REG_REMOTE_LOWL;
1332 else if (index == REMOTE_HIGH || index == REMOTE2_HIGH)
1333 regl = LM90_REG_REMOTE_HIGHL;
1336 /* +16 degrees offset for remote temperature on LM99 */
1337 if (data->kind == lm99 && channel) {
1338 /* prevent integer underflow */
1339 val = max(val, -128000l);
1343 data->temp[index] = lm90_temp_to_reg(data->flags, val,
1344 lm90_temp_get_resolution(data, index));
1347 lm90_select_remote_channel(data, true);
1349 err = lm90_write16(client, regh, regl, data->temp[index]);
1352 lm90_select_remote_channel(data, false);
1357 static int lm90_get_temphyst(struct lm90_data *data, int index, int channel)
1359 int temp = lm90_get_temp(data, index, channel);
1361 return temp - data->temp_hyst * 1000;
1364 static int lm90_set_temphyst(struct lm90_data *data, long val)
1366 int temp = lm90_get_temp(data, LOCAL_CRIT, 0);
1368 /* prevent integer overflow/underflow */
1369 val = clamp_val(val, -128000l, 255000l);
1370 data->temp_hyst = clamp_val(DIV_ROUND_CLOSEST(temp - val, 1000), 0, 31);
1372 return lm90_write_reg(data->client, LM90_REG_TCRIT_HYST, data->temp_hyst);
1375 static const u8 lm90_temp_index[3] = {
1376 LOCAL_TEMP, REMOTE_TEMP, REMOTE2_TEMP
1379 static const u8 lm90_temp_min_index[3] = {
1380 LOCAL_LOW, REMOTE_LOW, REMOTE2_LOW
1383 static const u8 lm90_temp_max_index[3] = {
1384 LOCAL_HIGH, REMOTE_HIGH, REMOTE2_HIGH
1387 static const u8 lm90_temp_crit_index[3] = {
1388 LOCAL_CRIT, REMOTE_CRIT, REMOTE2_CRIT
1391 static const u8 lm90_temp_emerg_index[3] = {
1392 LOCAL_EMERG, REMOTE_EMERG, REMOTE2_EMERG
1395 static const u16 lm90_min_alarm_bits[3] = { BIT(5), BIT(3), BIT(11) };
1396 static const u16 lm90_max_alarm_bits[3] = { BIT(6), BIT(4), BIT(12) };
1397 static const u16 lm90_crit_alarm_bits[3] = { BIT(0), BIT(1), BIT(9) };
1398 static const u16 lm90_crit_alarm_bits_swapped[3] = { BIT(1), BIT(0), BIT(9) };
1399 static const u16 lm90_emergency_alarm_bits[3] = { BIT(15), BIT(13), BIT(14) };
1400 static const u16 lm90_fault_bits[3] = { BIT(0), BIT(2), BIT(10) };
1402 static int lm90_temp_read(struct device *dev, u32 attr, int channel, long *val)
1404 struct lm90_data *data = dev_get_drvdata(dev);
1408 mutex_lock(&data->update_lock);
1409 err = lm90_update_device(dev);
1410 mutex_unlock(&data->update_lock);
1415 case hwmon_temp_input:
1416 *val = lm90_get_temp(data, lm90_temp_index[channel], channel);
1418 case hwmon_temp_min_alarm:
1419 case hwmon_temp_max_alarm:
1420 case hwmon_temp_crit_alarm:
1421 case hwmon_temp_emergency_alarm:
1422 case hwmon_temp_fault:
1424 case hwmon_temp_min_alarm:
1425 bit = lm90_min_alarm_bits[channel];
1427 case hwmon_temp_max_alarm:
1428 bit = lm90_max_alarm_bits[channel];
1430 case hwmon_temp_crit_alarm:
1431 if (data->flags & LM90_HAVE_CRIT_ALRM_SWP)
1432 bit = lm90_crit_alarm_bits_swapped[channel];
1434 bit = lm90_crit_alarm_bits[channel];
1436 case hwmon_temp_emergency_alarm:
1437 bit = lm90_emergency_alarm_bits[channel];
1439 case hwmon_temp_fault:
1440 bit = lm90_fault_bits[channel];
1443 *val = !!(data->alarms & bit);
1444 data->alarms &= ~bit;
1445 data->alarms |= data->current_alarms;
1447 case hwmon_temp_min:
1448 *val = lm90_get_temp(data, lm90_temp_min_index[channel], channel);
1450 case hwmon_temp_max:
1451 *val = lm90_get_temp(data, lm90_temp_max_index[channel], channel);
1453 case hwmon_temp_crit:
1454 *val = lm90_get_temp(data, lm90_temp_crit_index[channel], channel);
1456 case hwmon_temp_crit_hyst:
1457 *val = lm90_get_temphyst(data, lm90_temp_crit_index[channel], channel);
1459 case hwmon_temp_emergency:
1460 *val = lm90_get_temp(data, lm90_temp_emerg_index[channel], channel);
1462 case hwmon_temp_emergency_hyst:
1463 *val = lm90_get_temphyst(data, lm90_temp_emerg_index[channel], channel);
1465 case hwmon_temp_offset:
1466 *val = lm90_temp_from_reg(0, data->temp[REMOTE_OFFSET],
1467 lm90_temp_get_resolution(data, REMOTE_OFFSET));
1475 static int lm90_temp_write(struct device *dev, u32 attr, int channel, long val)
1477 struct lm90_data *data = dev_get_drvdata(dev);
1480 mutex_lock(&data->update_lock);
1482 err = lm90_update_device(dev);
1487 case hwmon_temp_min:
1488 err = lm90_set_temp(data, lm90_temp_min_index[channel],
1491 case hwmon_temp_max:
1492 err = lm90_set_temp(data, lm90_temp_max_index[channel],
1495 case hwmon_temp_crit:
1496 err = lm90_set_temp(data, lm90_temp_crit_index[channel],
1499 case hwmon_temp_crit_hyst:
1500 err = lm90_set_temphyst(data, val);
1502 case hwmon_temp_emergency:
1503 err = lm90_set_temp(data, lm90_temp_emerg_index[channel],
1506 case hwmon_temp_offset:
1507 val = lm90_temp_to_reg(0, val,
1508 lm90_temp_get_resolution(data, REMOTE_OFFSET));
1509 err = lm90_write16(data->client, LM90_REG_REMOTE_OFFSH,
1510 LM90_REG_REMOTE_OFFSL, val);
1513 data->temp[REMOTE_OFFSET] = val;
1520 mutex_unlock(&data->update_lock);
1525 static umode_t lm90_temp_is_visible(const void *data, u32 attr, int channel)
1528 case hwmon_temp_input:
1529 case hwmon_temp_min_alarm:
1530 case hwmon_temp_max_alarm:
1531 case hwmon_temp_crit_alarm:
1532 case hwmon_temp_emergency_alarm:
1533 case hwmon_temp_emergency_hyst:
1534 case hwmon_temp_fault:
1536 case hwmon_temp_min:
1537 case hwmon_temp_max:
1538 case hwmon_temp_crit:
1539 case hwmon_temp_emergency:
1540 case hwmon_temp_offset:
1542 case hwmon_temp_crit_hyst:
1551 static int lm90_chip_read(struct device *dev, u32 attr, int channel, long *val)
1553 struct lm90_data *data = dev_get_drvdata(dev);
1556 mutex_lock(&data->update_lock);
1557 err = lm90_update_device(dev);
1558 mutex_unlock(&data->update_lock);
1563 case hwmon_chip_update_interval:
1564 *val = data->update_interval;
1566 case hwmon_chip_alarms:
1567 *val = data->alarms;
1576 static int lm90_chip_write(struct device *dev, u32 attr, int channel, long val)
1578 struct lm90_data *data = dev_get_drvdata(dev);
1579 struct i2c_client *client = data->client;
1582 mutex_lock(&data->update_lock);
1584 err = lm90_update_device(dev);
1589 case hwmon_chip_update_interval:
1590 err = lm90_set_convrate(client, data,
1591 clamp_val(val, 0, 100000));
1598 mutex_unlock(&data->update_lock);
1603 static umode_t lm90_chip_is_visible(const void *data, u32 attr, int channel)
1606 case hwmon_chip_update_interval:
1608 case hwmon_chip_alarms:
1615 static int lm90_read(struct device *dev, enum hwmon_sensor_types type,
1616 u32 attr, int channel, long *val)
1620 return lm90_chip_read(dev, attr, channel, val);
1622 return lm90_temp_read(dev, attr, channel, val);
1628 static int lm90_write(struct device *dev, enum hwmon_sensor_types type,
1629 u32 attr, int channel, long val)
1633 return lm90_chip_write(dev, attr, channel, val);
1635 return lm90_temp_write(dev, attr, channel, val);
1641 static umode_t lm90_is_visible(const void *data, enum hwmon_sensor_types type,
1642 u32 attr, int channel)
1646 return lm90_chip_is_visible(data, attr, channel);
1648 return lm90_temp_is_visible(data, attr, channel);
1654 static const char *lm90_detect_lm84(struct i2c_client *client)
1656 static const u8 regs[] = {
1657 LM90_REG_STATUS, LM90_REG_LOCAL_TEMP, LM90_REG_LOCAL_HIGH,
1658 LM90_REG_REMOTE_TEMPH, LM90_REG_REMOTE_HIGHH
1660 int status = i2c_smbus_read_byte_data(client, LM90_REG_STATUS);
1661 int reg1, reg2, reg3, reg4;
1662 bool nonzero = false;
1666 if (status < 0 || (status & 0xab))
1670 * For LM84, undefined registers return the most recent value.
1671 * Repeat several times, each time checking against a different
1672 * (presumably) existing register.
1674 for (i = 0; i < ARRAY_SIZE(regs); i++) {
1675 reg1 = i2c_smbus_read_byte_data(client, regs[i]);
1676 reg2 = i2c_smbus_read_byte_data(client, LM90_REG_REMOTE_TEMPL);
1677 reg3 = i2c_smbus_read_byte_data(client, LM90_REG_LOCAL_LOW);
1678 reg4 = i2c_smbus_read_byte_data(client, LM90_REG_REMOTE_LOWH);
1683 /* If any register has a different value, this is not an LM84 */
1684 if (reg2 != reg1 || reg3 != reg1 || reg4 != reg1)
1687 nonzero |= reg1 || reg2 || reg3 || reg4;
1691 * If all registers always returned 0 or 0xff, all bets are off,
1692 * and we can not make any predictions about the chip type.
1694 return nonzero && ff != 0xff ? "lm84" : NULL;
1697 static const char *lm90_detect_max1617(struct i2c_client *client, int config1)
1699 int status = i2c_smbus_read_byte_data(client, LM90_REG_STATUS);
1700 int llo, rlo, lhi, rhi;
1702 if (status < 0 || (status & 0x03))
1709 * Fail if unsupported registers return anything but 0xff.
1710 * The calling code already checked man_id and chip_id.
1711 * A byte read operation repeats the most recent read operation
1712 * and should also return 0xff.
1714 if (i2c_smbus_read_byte_data(client, LM90_REG_REMOTE_TEMPL) != 0xff ||
1715 i2c_smbus_read_byte_data(client, MAX6657_REG_LOCAL_TEMPL) != 0xff ||
1716 i2c_smbus_read_byte_data(client, LM90_REG_REMOTE_LOWL) != 0xff ||
1717 i2c_smbus_read_byte(client) != 0xff)
1720 llo = i2c_smbus_read_byte_data(client, LM90_REG_LOCAL_LOW);
1721 rlo = i2c_smbus_read_byte_data(client, LM90_REG_REMOTE_LOWH);
1723 lhi = i2c_smbus_read_byte_data(client, LM90_REG_LOCAL_HIGH);
1724 rhi = i2c_smbus_read_byte_data(client, LM90_REG_REMOTE_HIGHH);
1726 if (llo < 0 || rlo < 0)
1730 * A byte read operation repeats the most recent read and should
1731 * return the same value.
1733 if (i2c_smbus_read_byte(client) != rhi)
1737 * The following two checks are marginal since the checked values
1738 * are strictly speaking valid.
1741 /* fail for negative high limits; this also catches read errors */
1742 if ((s8)lhi < 0 || (s8)rhi < 0)
1745 /* fail if low limits are larger than or equal to high limits */
1746 if ((s8)llo >= lhi || (s8)rlo >= rhi)
1749 if (i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_WORD_DATA)) {
1751 * Word read operations return 0xff in second byte
1753 if (i2c_smbus_read_word_data(client, LM90_REG_REMOTE_TEMPL) !=
1756 if (i2c_smbus_read_word_data(client, LM90_REG_CONFIG1) !=
1759 if (i2c_smbus_read_word_data(client, LM90_REG_LOCAL_HIGH) !=
1767 static const char *lm90_detect_national(struct i2c_client *client, int chip_id,
1768 int config1, int convrate)
1770 int config2 = i2c_smbus_read_byte_data(client, LM90_REG_CONFIG2);
1771 int address = client->addr;
1772 const char *name = NULL;
1777 if ((config1 & 0x2a) || (config2 & 0xf8) || convrate > 0x09)
1780 if (address != 0x4c && address != 0x4d)
1783 switch (chip_id & 0xf0) {
1784 case 0x10: /* LM86 */
1785 if (address == 0x4c)
1788 case 0x20: /* LM90 */
1789 if (address == 0x4c)
1792 case 0x30: /* LM89/LM99 */
1793 name = "lm99"; /* detect LM89 as LM99 */
1802 static const char *lm90_detect_on(struct i2c_client *client, int chip_id, int config1,
1805 int address = client->addr;
1806 const char *name = NULL;
1809 case 0xca: /* NCT218 */
1810 if ((address == 0x4c || address == 0x4d) && !(config1 & 0x1b) &&
1820 static const char *lm90_detect_analog(struct i2c_client *client, bool common_address,
1821 int chip_id, int config1, int convrate)
1823 int status = i2c_smbus_read_byte_data(client, LM90_REG_STATUS);
1824 int config2 = i2c_smbus_read_byte_data(client, ADT7481_REG_CONFIG2);
1825 int man_id2 = i2c_smbus_read_byte_data(client, ADT7481_REG_MAN_ID);
1826 int chip_id2 = i2c_smbus_read_byte_data(client, ADT7481_REG_CHIP_ID);
1827 int address = client->addr;
1828 const char *name = NULL;
1830 if (status < 0 || config2 < 0 || man_id2 < 0 || chip_id2 < 0)
1834 case 0x00 ... 0x03: /* ADM1021 */
1836 if (man_id2 == 0x00 && chip_id2 == 0x00 && common_address &&
1837 !(status & 0x03) && !(config1 & 0x3f) && !(convrate & 0xf8))
1840 case 0x04: /* ADT7421 (undocumented) */
1841 if (man_id2 == 0x41 && chip_id2 == 0x21 &&
1842 (address == 0x4c || address == 0x4d) &&
1843 (config1 & 0x0b) == 0x08 && convrate <= 0x0a)
1846 case 0x30 ... 0x38: /* ADM1021A, ADM1023 */
1849 * ADM1021A and compatible chips will be mis-detected as
1850 * ADM1023. Chips labeled 'ADM1021A' and 'ADM1023' were both
1851 * found to have a Chip ID of 0x3c.
1852 * ADM1021A does not officially support low byte registers
1853 * (0x12 .. 0x14), but a chip labeled ADM1021A does support it.
1854 * Official support for the temperature offset high byte
1855 * register (0x11) was added to revision F of the ADM1021A
1857 * It is currently unknown if there is a means to distinguish
1858 * ADM1021A from ADM1023, and/or if revisions of ADM1021A exist
1859 * which differ in functionality from ADM1023.
1861 if (man_id2 == 0x00 && chip_id2 == 0x00 && common_address &&
1862 !(status & 0x03) && !(config1 & 0x3f) && !(convrate & 0xf8))
1865 case 0x39: /* ADM1020 (undocumented) */
1866 if (man_id2 == 0x00 && chip_id2 == 0x00 &&
1867 (address == 0x4c || address == 0x4d || address == 0x4e) &&
1868 !(status & 0x03) && !(config1 & 0x3f) && !(convrate & 0xf8))
1871 case 0x3f: /* NCT210 */
1872 if (man_id2 == 0x00 && chip_id2 == 0x00 && common_address &&
1873 !(status & 0x03) && !(config1 & 0x3f) && !(convrate & 0xf8))
1876 case 0x40 ... 0x4f: /* ADM1032 */
1877 if (man_id2 == 0x00 && chip_id2 == 0x00 &&
1878 (address == 0x4c || address == 0x4d) && !(config1 & 0x3f) &&
1882 case 0x51: /* ADT7461 */
1883 if (man_id2 == 0x00 && chip_id2 == 0x00 &&
1884 (address == 0x4c || address == 0x4d) && !(config1 & 0x1b) &&
1888 case 0x54: /* NCT1008 */
1889 if (man_id2 == 0x41 && chip_id2 == 0x61 &&
1890 (address == 0x4c || address == 0x4d) && !(config1 & 0x1b) &&
1894 case 0x55: /* NCT72 */
1895 if (man_id2 == 0x41 && chip_id2 == 0x61 &&
1896 (address == 0x4c || address == 0x4d) && !(config1 & 0x1b) &&
1900 case 0x57: /* ADT7461A, NCT1008 (datasheet rev. 3) */
1901 if (man_id2 == 0x41 && chip_id2 == 0x61 &&
1902 (address == 0x4c || address == 0x4d) && !(config1 & 0x1b) &&
1906 case 0x5a: /* NCT214 */
1907 if (man_id2 == 0x41 && chip_id2 == 0x61 &&
1908 common_address && !(config1 & 0x1b) && convrate <= 0x0a)
1911 case 0x62: /* ADT7481, undocumented */
1912 if (man_id2 == 0x41 && chip_id2 == 0x81 &&
1913 (address == 0x4b || address == 0x4c) && !(config1 & 0x10) &&
1914 !(config2 & 0x7f) && (convrate & 0x0f) <= 0x0b) {
1918 case 0x65: /* ADT7482, datasheet */
1919 case 0x75: /* ADT7482, real chip */
1920 if (man_id2 == 0x41 && chip_id2 == 0x82 &&
1921 address == 0x4c && !(config1 & 0x10) && !(config2 & 0x7f) &&
1925 case 0x94: /* ADT7483 */
1926 if (man_id2 == 0x41 && chip_id2 == 0x83 &&
1928 ((address >= 0x18 && address <= 0x1a) ||
1929 (address >= 0x29 && address <= 0x2b) ||
1930 (address >= 0x4c && address <= 0x4e)) &&
1931 !(config1 & 0x10) && !(config2 & 0x7f) && convrate <= 0x0a)
1941 static const char *lm90_detect_maxim(struct i2c_client *client, bool common_address,
1942 int chip_id, int config1, int convrate)
1944 int man_id, emerg, emerg2, status2;
1945 int address = client->addr;
1946 const char *name = NULL;
1950 if (!common_address)
1954 * We read MAX6659_REG_REMOTE_EMERG twice, and re-read
1955 * LM90_REG_MAN_ID in between. If MAX6659_REG_REMOTE_EMERG
1956 * exists, both readings will reflect the same value. Otherwise,
1957 * the readings will be different.
1959 emerg = i2c_smbus_read_byte_data(client,
1960 MAX6659_REG_REMOTE_EMERG);
1961 man_id = i2c_smbus_read_byte_data(client,
1963 emerg2 = i2c_smbus_read_byte_data(client,
1964 MAX6659_REG_REMOTE_EMERG);
1965 status2 = i2c_smbus_read_byte_data(client,
1966 MAX6696_REG_STATUS2);
1967 if (emerg < 0 || man_id < 0 || emerg2 < 0 || status2 < 0)
1971 * Even though MAX6695 and MAX6696 do not have a chip ID
1972 * register, reading it returns 0x01. Bit 4 of the config1
1973 * register is unused and should return zero when read. Bit 0 of
1974 * the status2 register is unused and should return zero when
1977 * MAX6695 and MAX6696 have an additional set of temperature
1978 * limit registers. We can detect those chips by checking if
1979 * one of those registers exists.
1981 if (!(config1 & 0x10) && !(status2 & 0x01) && emerg == emerg2 &&
1985 * The chip_id register of the MAX6680 and MAX6681 holds the
1986 * revision of the chip. The lowest bit of the config1 register
1987 * is unused and should return zero when read, so should the
1988 * second to last bit of config1 (software reset). Register
1989 * address 0x12 (LM90_REG_REMOTE_OFFSL) exists for this chip and
1990 * should differ from emerg2, and emerg2 should match man_id
1991 * since it does not exist.
1993 else if (!(config1 & 0x03) && convrate <= 0x07 &&
1994 emerg2 == man_id && emerg2 != status2)
1997 * MAX1617A does not have any extended registers (register
1998 * address 0x10 or higher) except for manufacturer and
1999 * device ID registers. Unlike other chips of this series,
2000 * unsupported registers were observed to return a fixed value
2002 * Note: Multiple chips with different markings labeled as
2003 * "MAX1617" (no "A") were observed to report manufacturer ID
2004 * 0x4d and device ID 0x01. It is unknown if other variants of
2005 * MAX1617/MAX617A with different behavior exist. The detection
2006 * code below works for those chips.
2008 else if (!(config1 & 0x03f) && convrate <= 0x07 &&
2009 emerg == 0x01 && emerg2 == 0x01 && status2 == 0x01)
2014 * The chip_id of the MAX6654 holds the revision of the chip.
2015 * The lowest 3 bits of the config1 register are unused and
2016 * should return zero when read.
2018 if (common_address && !(config1 & 0x07) && convrate <= 0x07)
2023 * The chip_id of the MAX6690 holds the revision of the chip.
2024 * The lowest 3 bits of the config1 register are unused and
2025 * should return zero when read.
2026 * Note that MAX6654 and MAX6690 are practically the same chips.
2027 * The only diference is the rated accuracy. Rev. 1 of the
2028 * MAX6690 datasheet lists a chip ID of 0x08, and a chip labeled
2029 * MAX6654 was observed to have a chip ID of 0x09.
2031 if (common_address && !(config1 & 0x07) && convrate <= 0x07)
2036 * MAX6642, MAX6657, MAX6658 and MAX6659 do NOT have a chip_id
2037 * register. Reading from that address will return the last
2038 * read value, which in our case is those of the man_id
2039 * register, or 0x4d.
2040 * MAX6642 does not have a conversion rate register, nor low
2041 * limit registers. Reading from those registers returns the
2044 * For MAX6657, MAX6658 and MAX6659, the config1 register lacks
2045 * a low nibble, so the value will be those of the previous
2046 * read, so in our case again those of the man_id register.
2047 * MAX6659 has a third set of upper temperature limit registers.
2048 * Those registers also return values on MAX6657 and MAX6658,
2049 * thus the only way to detect MAX6659 is by its address.
2050 * For this reason it will be mis-detected as MAX6657 if its
2053 if (address >= 0x48 && address <= 0x4f && config1 == convrate &&
2054 !(config1 & 0x0f)) {
2058 * We know that this is not a MAX6657/58/59 because its
2059 * configuration register has the wrong value and it does
2060 * not appear to have a conversion rate register.
2063 /* re-read manufacturer ID to have a good baseline */
2064 if (i2c_smbus_read_byte_data(client, LM90_REG_MAN_ID) != 0x4d)
2067 /* check various non-existing registers */
2068 if (i2c_smbus_read_byte_data(client, LM90_REG_CONVRATE) != 0x4d ||
2069 i2c_smbus_read_byte_data(client, LM90_REG_LOCAL_LOW) != 0x4d ||
2070 i2c_smbus_read_byte_data(client, LM90_REG_REMOTE_LOWH) != 0x4d)
2073 /* check for unused status register bits */
2074 regval = i2c_smbus_read_byte_data(client, LM90_REG_STATUS);
2075 if (regval < 0 || (regval & 0x2b))
2078 /* re-check unsupported registers */
2079 if (i2c_smbus_read_byte_data(client, LM90_REG_CONVRATE) != regval ||
2080 i2c_smbus_read_byte_data(client, LM90_REG_LOCAL_LOW) != regval ||
2081 i2c_smbus_read_byte_data(client, LM90_REG_REMOTE_LOWH) != regval)
2085 } else if ((address == 0x4c || address == 0x4d || address == 0x4e) &&
2086 (config1 & 0x1f) == 0x0d && convrate <= 0x09) {
2087 if (address == 0x4c)
2095 * The chip_id register of the MAX6646/6647/6649 holds the
2096 * revision of the chip. The lowest 6 bits of the config1
2097 * register are unused and should return zero when read.
2098 * The I2C address of MAX6648/6692 is fixed at 0x4c.
2099 * MAX6646 is at address 0x4d, MAX6647 is at address 0x4e,
2100 * and MAX6649 is at address 0x4c. A slight difference between
2101 * the two sets of chips is that the remote temperature register
2102 * reports different values if the DXP pin is open or shorted.
2103 * We can use that information to help distinguish between the
2104 * chips. MAX6648 will be mis-detected as MAX6649 if the remote
2105 * diode is connected, but there isn't really anything we can
2108 if (!(config1 & 0x3f) && convrate <= 0x07) {
2114 * MAX6649 reports an external temperature
2115 * value of 0xff if DXP is open or shorted.
2116 * MAX6648 reports 0x80 in that case.
2118 temp = i2c_smbus_read_byte_data(client,
2119 LM90_REG_REMOTE_TEMPH);
2143 static const char *lm90_detect_nuvoton(struct i2c_client *client, int chip_id,
2144 int config1, int convrate)
2146 int config2 = i2c_smbus_read_byte_data(client, LM90_REG_CONFIG2);
2147 int address = client->addr;
2148 const char *name = NULL;
2151 return ERR_PTR(-ENODEV);
2153 if (address == 0x4c && !(config1 & 0x2a) && !(config2 & 0xf8)) {
2154 if (chip_id == 0x01 && convrate <= 0x09) {
2157 } else if ((chip_id & 0xfe) == 0x10 && convrate <= 0x08) {
2158 /* W83L771AWG/ASG */
2165 static const char *lm90_detect_nxp(struct i2c_client *client, bool common_address,
2166 int chip_id, int config1, int convrate)
2168 int address = client->addr;
2169 const char *name = NULL;
2174 config2 = i2c_smbus_read_byte_data(client, LM90_REG_CONFIG2);
2177 if (address >= 0x48 && address <= 0x4f &&
2178 !(config1 & 0x2a) && !(config2 & 0xfe) && convrate <= 0x09)
2182 if (common_address && !(config1 & 0x3f) && convrate <= 0x07)
2191 static const char *lm90_detect_gmt(struct i2c_client *client, int chip_id,
2192 int config1, int convrate)
2194 int address = client->addr;
2197 * According to the datasheet, G781 is supposed to be at I2C Address
2198 * 0x4c and have a chip ID of 0x01. G781-1 is supposed to be at I2C
2199 * address 0x4d and have a chip ID of 0x03. However, when support
2200 * for G781 was added, chips at 0x4c and 0x4d were found to have a
2201 * chip ID of 0x01. A G781-1 at I2C address 0x4d was now found with
2203 * To avoid detection failures, accept chip ID 0x01 and 0x03 at both
2205 * G784 reports manufacturer ID 0x47 and chip ID 0x01. A public
2206 * datasheet is not available. Extensive testing suggests that
2207 * the chip appears to be fully compatible with G781.
2208 * Available register dumps show that G751 also reports manufacturer
2209 * ID 0x47 and chip ID 0x01 even though that chip does not officially
2210 * support those registers. This makes chip detection somewhat
2211 * vulnerable. To improve detection quality, read the offset low byte
2212 * and alert fault queue registers and verify that only expected bits
2215 if ((chip_id == 0x01 || chip_id == 0x03) &&
2216 (address == 0x4c || address == 0x4d) &&
2217 !(config1 & 0x3f) && convrate <= 0x08) {
2220 reg = i2c_smbus_read_byte_data(client, LM90_REG_REMOTE_OFFSL);
2221 if (reg < 0 || reg & 0x1f)
2223 reg = i2c_smbus_read_byte_data(client, TMP451_REG_CONALERT);
2224 if (reg < 0 || reg & 0xf1)
2233 static const char *lm90_detect_ti49(struct i2c_client *client, bool common_address,
2234 int chip_id, int config1, int convrate)
2236 if (common_address && chip_id == 0x00 && !(config1 & 0x3f) && !(convrate & 0xf8)) {
2237 /* THMC10: Unsupported registers return 0xff */
2238 if (i2c_smbus_read_byte_data(client, LM90_REG_REMOTE_TEMPL) == 0xff &&
2239 i2c_smbus_read_byte_data(client, LM90_REG_REMOTE_CRIT) == 0xff)
2245 static const char *lm90_detect_ti(struct i2c_client *client, int chip_id,
2246 int config1, int convrate)
2248 int address = client->addr;
2249 const char *name = NULL;
2251 if (chip_id == 0x00 && !(config1 & 0x1b) && convrate <= 0x09) {
2252 int local_ext, conalert, chen, dfc;
2254 local_ext = i2c_smbus_read_byte_data(client,
2255 TMP451_REG_LOCAL_TEMPL);
2256 conalert = i2c_smbus_read_byte_data(client,
2257 TMP451_REG_CONALERT);
2258 chen = i2c_smbus_read_byte_data(client, TMP461_REG_CHEN);
2259 dfc = i2c_smbus_read_byte_data(client, TMP461_REG_DFC);
2261 if (!(local_ext & 0x0f) && (conalert & 0xf1) == 0x01 &&
2262 (chen & 0xfc) == 0x00 && (dfc & 0xfc) == 0x00) {
2263 if (address == 0x4c && !(chen & 0x03))
2265 else if (address >= 0x48 && address <= 0x4f)
2273 /* Return 0 if detection is successful, -ENODEV otherwise */
2274 static int lm90_detect(struct i2c_client *client, struct i2c_board_info *info)
2276 struct i2c_adapter *adapter = client->adapter;
2277 int man_id, chip_id, config1, convrate, lhigh;
2278 const char *name = NULL;
2279 int address = client->addr;
2280 bool common_address =
2281 (address >= 0x18 && address <= 0x1a) ||
2282 (address >= 0x29 && address <= 0x2b) ||
2283 (address >= 0x4c && address <= 0x4e);
2285 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
2289 * Get well defined register value for chips with neither man_id nor
2290 * chip_id registers.
2292 lhigh = i2c_smbus_read_byte_data(client, LM90_REG_LOCAL_HIGH);
2294 /* detection and identification */
2295 man_id = i2c_smbus_read_byte_data(client, LM90_REG_MAN_ID);
2296 chip_id = i2c_smbus_read_byte_data(client, LM90_REG_CHIP_ID);
2297 config1 = i2c_smbus_read_byte_data(client, LM90_REG_CONFIG1);
2298 convrate = i2c_smbus_read_byte_data(client, LM90_REG_CONVRATE);
2299 if (man_id < 0 || chip_id < 0 || config1 < 0 || convrate < 0 || lhigh < 0)
2302 /* Bail out immediately if all register report the same value */
2303 if (lhigh == man_id && lhigh == chip_id && lhigh == config1 && lhigh == convrate)
2307 * If reading man_id and chip_id both return the same value as lhigh,
2308 * the chip may not support those registers and return the most recent read
2309 * value. Check again with a different register and handle accordingly.
2311 if (man_id == lhigh && chip_id == lhigh) {
2312 convrate = i2c_smbus_read_byte_data(client, LM90_REG_CONVRATE);
2313 man_id = i2c_smbus_read_byte_data(client, LM90_REG_MAN_ID);
2314 chip_id = i2c_smbus_read_byte_data(client, LM90_REG_CHIP_ID);
2315 if (convrate < 0 || man_id < 0 || chip_id < 0)
2317 if (man_id == convrate && chip_id == convrate)
2321 case -1: /* Chip does not support man_id / chip_id */
2322 if (common_address && !convrate && !(config1 & 0x7f))
2323 name = lm90_detect_lm84(client);
2325 case 0x01: /* National Semiconductor */
2326 name = lm90_detect_national(client, chip_id, config1, convrate);
2329 name = lm90_detect_on(client, chip_id, config1, convrate);
2331 case 0x23: /* Genesys Logic */
2332 if (common_address && !(config1 & 0x3f) && !(convrate & 0xf8))
2335 case 0x41: /* Analog Devices */
2336 name = lm90_detect_analog(client, common_address, chip_id, config1,
2339 case 0x47: /* GMT */
2340 name = lm90_detect_gmt(client, chip_id, config1, convrate);
2343 name = lm90_detect_ti49(client, common_address, chip_id, config1, convrate);
2345 case 0x4d: /* Maxim Integrated */
2346 name = lm90_detect_maxim(client, common_address, chip_id,
2349 case 0x54: /* ON MC1066, Microchip TC1068, TCM1617 (originally TelCom) */
2350 if (common_address && !(config1 & 0x3f) && !(convrate & 0xf8))
2354 name = lm90_detect_ti(client, chip_id, config1, convrate);
2356 case 0x5c: /* Winbond/Nuvoton */
2357 name = lm90_detect_nuvoton(client, chip_id, config1, convrate);
2359 case 0xa1: /* NXP Semiconductor/Philips */
2360 name = lm90_detect_nxp(client, common_address, chip_id, config1, convrate);
2362 case 0xff: /* MAX1617, G767, NE1617 */
2363 if (common_address && chip_id == 0xff && convrate < 8)
2364 name = lm90_detect_max1617(client, config1);
2370 if (!name) { /* identification failed */
2371 dev_dbg(&adapter->dev,
2372 "Unsupported chip at 0x%02x (man_id=0x%02X, chip_id=0x%02X)\n",
2373 client->addr, man_id, chip_id);
2377 strlcpy(info->type, name, I2C_NAME_SIZE);
2382 static void lm90_restore_conf(void *_data)
2384 struct lm90_data *data = _data;
2385 struct i2c_client *client = data->client;
2387 cancel_delayed_work_sync(&data->alert_work);
2389 /* Restore initial configuration */
2390 if (data->flags & LM90_HAVE_CONVRATE)
2391 lm90_write_convrate(data, data->convrate_orig);
2392 lm90_write_reg(client, LM90_REG_CONFIG1, data->config_orig);
2395 static int lm90_init_client(struct i2c_client *client, struct lm90_data *data)
2397 struct device_node *np = client->dev.of_node;
2398 int config, convrate;
2400 if (data->flags & LM90_HAVE_CONVRATE) {
2401 convrate = lm90_read_reg(client, LM90_REG_CONVRATE);
2404 data->convrate_orig = convrate;
2405 lm90_set_convrate(client, data, 500); /* 500ms; 2Hz conversion rate */
2407 data->update_interval = 500;
2411 * Start the conversions.
2413 config = lm90_read_reg(client, LM90_REG_CONFIG1);
2416 data->config_orig = config;
2417 data->config = config;
2419 /* Check Temperature Range Select */
2420 if (data->flags & LM90_HAVE_EXTENDED_TEMP) {
2421 if (of_property_read_bool(np, "ti,extended-range-enable"))
2423 if (!(config & 0x04))
2424 data->flags &= ~LM90_HAVE_EXTENDED_TEMP;
2428 * Put MAX6680/MAX8881 into extended resolution (bit 0x10,
2429 * 0.125 degree resolution) and range (0x08, extend range
2430 * to -64 degree) mode for the remote temperature sensor.
2431 * Note that expeciments with an actual chip do not show a difference
2432 * if bit 3 is set or not.
2434 if (data->kind == max6680)
2438 * Put MAX6654 into extended range (0x20, extend minimum range from
2439 * 0 degrees to -64 degrees). Note that extended resolution is not
2440 * possible on the MAX6654 unless conversion rate is set to 1 Hz or
2441 * slower, which is intentionally not done by default.
2443 if (data->kind == max6654)
2447 * Select external channel 0 for devices with three sensors
2449 if (data->flags & LM90_HAVE_TEMP3)
2453 * Interrupt is enabled by default on reset, but it may be disabled
2454 * by bootloader, unmask it.
2459 config &= 0xBF; /* run */
2460 lm90_update_confreg(data, config);
2462 return devm_add_action_or_reset(&client->dev, lm90_restore_conf, data);
2465 static bool lm90_is_tripped(struct i2c_client *client)
2467 struct lm90_data *data = i2c_get_clientdata(client);
2470 ret = lm90_update_alarms(data, true);
2474 return !!data->current_alarms;
2477 static irqreturn_t lm90_irq_thread(int irq, void *dev_id)
2479 struct i2c_client *client = dev_id;
2481 if (lm90_is_tripped(client))
2487 static void lm90_remove_pec(void *dev)
2489 device_remove_file(dev, &dev_attr_pec);
2492 static void lm90_regulator_disable(void *regulator)
2494 regulator_disable(regulator);
2498 static const struct hwmon_ops lm90_ops = {
2499 .is_visible = lm90_is_visible,
2501 .write = lm90_write,
2504 static int lm90_probe(struct i2c_client *client)
2506 struct device *dev = &client->dev;
2507 struct i2c_adapter *adapter = client->adapter;
2508 struct hwmon_channel_info *info;
2509 struct regulator *regulator;
2510 struct device *hwmon_dev;
2511 struct lm90_data *data;
2514 regulator = devm_regulator_get(dev, "vcc");
2515 if (IS_ERR(regulator))
2516 return PTR_ERR(regulator);
2518 err = regulator_enable(regulator);
2520 dev_err(dev, "Failed to enable regulator: %d\n", err);
2524 err = devm_add_action_or_reset(dev, lm90_regulator_disable, regulator);
2528 data = devm_kzalloc(dev, sizeof(struct lm90_data), GFP_KERNEL);
2532 data->client = client;
2533 i2c_set_clientdata(client, data);
2534 mutex_init(&data->update_lock);
2535 INIT_DELAYED_WORK(&data->alert_work, lm90_alert_work);
2537 /* Set the device type */
2538 if (client->dev.of_node)
2539 data->kind = (enum chips)of_device_get_match_data(&client->dev);
2541 data->kind = i2c_match_id(lm90_id, client)->driver_data;
2544 * Different devices have different alarm bits triggering the
2547 data->alert_alarms = lm90_params[data->kind].alert_alarms;
2548 data->resolution = lm90_params[data->kind].resolution ? : 11;
2550 /* Set chip capabilities */
2551 data->flags = lm90_params[data->kind].flags;
2553 if ((data->flags & (LM90_HAVE_PEC | LM90_HAVE_PARTIAL_PEC)) &&
2554 !i2c_check_functionality(adapter, I2C_FUNC_SMBUS_PEC))
2555 data->flags &= ~(LM90_HAVE_PEC | LM90_HAVE_PARTIAL_PEC);
2557 if ((data->flags & LM90_HAVE_PARTIAL_PEC) &&
2558 !i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE))
2559 data->flags &= ~LM90_HAVE_PARTIAL_PEC;
2561 data->chip.ops = &lm90_ops;
2562 data->chip.info = data->info;
2564 data->info[0] = &data->chip_info;
2565 info = &data->chip_info;
2566 info->type = hwmon_chip;
2567 info->config = data->chip_config;
2569 data->chip_config[0] = HWMON_C_REGISTER_TZ;
2570 if (data->flags & LM90_HAVE_ALARMS)
2571 data->chip_config[0] |= HWMON_C_ALARMS;
2572 if (data->flags & LM90_HAVE_CONVRATE)
2573 data->chip_config[0] |= HWMON_C_UPDATE_INTERVAL;
2575 data->info[1] = &data->temp_info;
2577 info = &data->temp_info;
2578 info->type = hwmon_temp;
2579 info->config = data->channel_config;
2581 data->channel_config[0] = HWMON_T_INPUT | HWMON_T_MAX |
2583 data->channel_config[1] = HWMON_T_INPUT | HWMON_T_MAX |
2584 HWMON_T_MAX_ALARM | HWMON_T_FAULT;
2586 if (data->flags & LM90_HAVE_LOW) {
2587 data->channel_config[0] |= HWMON_T_MIN | HWMON_T_MIN_ALARM;
2588 data->channel_config[1] |= HWMON_T_MIN | HWMON_T_MIN_ALARM;
2591 if (data->flags & LM90_HAVE_CRIT) {
2592 data->channel_config[0] |= HWMON_T_CRIT | HWMON_T_CRIT_ALARM | HWMON_T_CRIT_HYST;
2593 data->channel_config[1] |= HWMON_T_CRIT | HWMON_T_CRIT_ALARM | HWMON_T_CRIT_HYST;
2596 if (data->flags & LM90_HAVE_OFFSET)
2597 data->channel_config[1] |= HWMON_T_OFFSET;
2599 if (data->flags & LM90_HAVE_EMERGENCY) {
2600 data->channel_config[0] |= HWMON_T_EMERGENCY |
2601 HWMON_T_EMERGENCY_HYST;
2602 data->channel_config[1] |= HWMON_T_EMERGENCY |
2603 HWMON_T_EMERGENCY_HYST;
2606 if (data->flags & LM90_HAVE_EMERGENCY_ALARM) {
2607 data->channel_config[0] |= HWMON_T_EMERGENCY_ALARM;
2608 data->channel_config[1] |= HWMON_T_EMERGENCY_ALARM;
2611 if (data->flags & LM90_HAVE_TEMP3) {
2612 data->channel_config[2] = HWMON_T_INPUT |
2613 HWMON_T_MIN | HWMON_T_MAX |
2614 HWMON_T_CRIT | HWMON_T_CRIT_HYST |
2615 HWMON_T_MIN_ALARM | HWMON_T_MAX_ALARM |
2616 HWMON_T_CRIT_ALARM | HWMON_T_FAULT;
2617 if (data->flags & LM90_HAVE_EMERGENCY) {
2618 data->channel_config[2] |= HWMON_T_EMERGENCY |
2619 HWMON_T_EMERGENCY_HYST;
2621 if (data->flags & LM90_HAVE_EMERGENCY_ALARM)
2622 data->channel_config[2] |= HWMON_T_EMERGENCY_ALARM;
2625 data->reg_local_ext = lm90_params[data->kind].reg_local_ext;
2626 if (data->flags & LM90_HAVE_REMOTE_EXT)
2627 data->reg_remote_ext = LM90_REG_REMOTE_TEMPL;
2628 data->reg_status2 = lm90_params[data->kind].reg_status2;
2630 /* Set maximum conversion rate */
2631 data->max_convrate = lm90_params[data->kind].max_convrate;
2633 /* Initialize the LM90 chip */
2634 err = lm90_init_client(client, data);
2636 dev_err(dev, "Failed to initialize device\n");
2641 * The 'pec' attribute is attached to the i2c device and thus created
2644 if (data->flags & (LM90_HAVE_PEC | LM90_HAVE_PARTIAL_PEC)) {
2645 err = device_create_file(dev, &dev_attr_pec);
2648 err = devm_add_action_or_reset(dev, lm90_remove_pec, dev);
2653 hwmon_dev = devm_hwmon_device_register_with_info(dev, client->name,
2656 if (IS_ERR(hwmon_dev))
2657 return PTR_ERR(hwmon_dev);
2659 data->hwmon_dev = hwmon_dev;
2662 dev_dbg(dev, "IRQ: %d\n", client->irq);
2663 err = devm_request_threaded_irq(dev, client->irq,
2664 NULL, lm90_irq_thread,
2665 IRQF_ONESHOT, "lm90", client);
2667 dev_err(dev, "cannot request IRQ %d\n", client->irq);
2675 static void lm90_alert(struct i2c_client *client, enum i2c_alert_protocol type,
2678 if (type != I2C_PROTOCOL_SMBUS_ALERT)
2681 if (lm90_is_tripped(client)) {
2683 * Disable ALERT# output, because these chips don't implement
2684 * SMBus alert correctly; they should only hold the alert line
2687 struct lm90_data *data = i2c_get_clientdata(client);
2689 if ((data->flags & LM90_HAVE_BROKEN_ALERT) &&
2690 (data->current_alarms & data->alert_alarms)) {
2691 if (!(data->config & 0x80)) {
2692 dev_dbg(&client->dev, "Disabling ALERT#\n");
2693 lm90_update_confreg(data, data->config | 0x80);
2695 schedule_delayed_work(&data->alert_work,
2696 max_t(int, HZ, msecs_to_jiffies(data->update_interval)));
2699 dev_dbg(&client->dev, "Everything OK\n");
2703 static int __maybe_unused lm90_suspend(struct device *dev)
2705 struct lm90_data *data = dev_get_drvdata(dev);
2706 struct i2c_client *client = data->client;
2709 disable_irq(client->irq);
2714 static int __maybe_unused lm90_resume(struct device *dev)
2716 struct lm90_data *data = dev_get_drvdata(dev);
2717 struct i2c_client *client = data->client;
2720 enable_irq(client->irq);
2725 static SIMPLE_DEV_PM_OPS(lm90_pm_ops, lm90_suspend, lm90_resume);
2727 static struct i2c_driver lm90_driver = {
2728 .class = I2C_CLASS_HWMON,
2731 .of_match_table = of_match_ptr(lm90_of_match),
2734 .probe_new = lm90_probe,
2735 .alert = lm90_alert,
2736 .id_table = lm90_id,
2737 .detect = lm90_detect,
2738 .address_list = normal_i2c,
2741 module_i2c_driver(lm90_driver);
2743 MODULE_AUTHOR("Jean Delvare <jdelvare@suse.de>");
2744 MODULE_DESCRIPTION("LM90/ADM1032 driver");
2745 MODULE_LICENSE("GPL");