1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * lm90.c - Part of lm_sensors, Linux kernel modules for hardware
5 * Copyright (C) 2003-2010 Jean Delvare <jdelvare@suse.de>
7 * Based on the lm83 driver. The LM90 is a sensor chip made by National
8 * Semiconductor. It reports up to two temperatures (its own plus up to
9 * one external one) with a 0.125 deg resolution (1 deg for local
10 * temperature) and a 3-4 deg accuracy.
12 * This driver also supports the LM89 and LM99, two other sensor chips
13 * made by National Semiconductor. Both have an increased remote
14 * temperature measurement accuracy (1 degree), and the LM99
15 * additionally shifts remote temperatures (measured and limits) by 16
16 * degrees, which allows for higher temperatures measurement.
17 * Note that there is no way to differentiate between both chips.
18 * When device is auto-detected, the driver will assume an LM99.
20 * This driver also supports the LM86, another sensor chip made by
21 * National Semiconductor. It is exactly similar to the LM90 except it
22 * has a higher accuracy.
24 * This driver also supports the ADM1032, a sensor chip made by Analog
25 * Devices. That chip is similar to the LM90, with a few differences
26 * that are not handled by this driver. Among others, it has a higher
27 * accuracy than the LM90, much like the LM86 does.
29 * This driver also supports the MAX6657, MAX6658 and MAX6659 sensor
30 * chips made by Maxim. These chips are similar to the LM86.
31 * Note that there is no easy way to differentiate between the three
32 * variants. We use the device address to detect MAX6659, which will result
33 * in a detection as max6657 if it is on address 0x4c. The extra address
34 * and features of the MAX6659 are only supported if the chip is configured
35 * explicitly as max6659, or if its address is not 0x4c.
36 * These chips lack the remote temperature offset feature.
38 * This driver also supports the MAX6646, MAX6647, MAX6648, MAX6649 and
39 * MAX6692 chips made by Maxim. These are again similar to the LM86,
40 * but they use unsigned temperature values and can report temperatures
41 * from 0 to 145 degrees.
43 * This driver also supports the MAX6680 and MAX6681, two other sensor
44 * chips made by Maxim. These are quite similar to the other Maxim
45 * chips. The MAX6680 and MAX6681 only differ in the pinout so they can
46 * be treated identically.
48 * This driver also supports the MAX6695 and MAX6696, two other sensor
49 * chips made by Maxim. These are also quite similar to other Maxim
50 * chips, but support three temperature sensors instead of two. MAX6695
51 * and MAX6696 only differ in the pinout so they can be treated identically.
53 * This driver also supports ADT7461 and ADT7461A from Analog Devices as well as
54 * NCT1008 from ON Semiconductor. The chips are supported in both compatibility
55 * and extended mode. They are mostly compatible with LM90 except for a data
56 * format difference for the temperature value registers.
58 * This driver also supports the SA56004 from Philips. This device is
59 * pin-compatible with the LM86, the ED/EDP parts are also address-compatible.
61 * This driver also supports the G781 from GMT. This device is compatible
64 * This driver also supports TMP451 from Texas Instruments. This device is
65 * supported in both compatibility and extended mode. It's mostly compatible
66 * with ADT7461 except for local temperature low byte register and max
69 * Since the LM90 was the first chipset supported by this driver, most
70 * comments will refer to this chipset, but are actually general and
71 * concern all supported chipsets, unless mentioned otherwise.
74 #include <linux/module.h>
75 #include <linux/init.h>
76 #include <linux/slab.h>
77 #include <linux/jiffies.h>
78 #include <linux/i2c.h>
79 #include <linux/hwmon.h>
80 #include <linux/err.h>
81 #include <linux/mutex.h>
82 #include <linux/of_device.h>
83 #include <linux/sysfs.h>
84 #include <linux/interrupt.h>
85 #include <linux/regulator/consumer.h>
89 * Address is fully defined internally and cannot be changed except for
90 * MAX6659, MAX6680 and MAX6681.
91 * LM86, LM89, LM90, LM99, ADM1032, ADM1032-1, ADT7461, ADT7461A, MAX6649,
92 * MAX6657, MAX6658, NCT1008 and W83L771 have address 0x4c.
93 * ADM1032-2, ADT7461-2, ADT7461A-2, LM89-1, LM99-1, MAX6646, and NCT1008D
95 * MAX6647 has address 0x4e.
96 * MAX6659 can have address 0x4c, 0x4d or 0x4e.
97 * MAX6680 and MAX6681 can have address 0x18, 0x19, 0x1a, 0x29, 0x2a, 0x2b,
99 * SA56004 can have address 0x48 through 0x4F.
102 static const unsigned short normal_i2c[] = {
103 0x18, 0x19, 0x1a, 0x29, 0x2a, 0x2b, 0x48, 0x49, 0x4a, 0x4b, 0x4c,
104 0x4d, 0x4e, 0x4f, I2C_CLIENT_END };
106 enum chips { lm90, adm1032, lm99, lm86, max6657, max6659, adt7461, max6680,
107 max6646, w83l771, max6696, sa56004, g781, tmp451 };
113 #define LM90_REG_R_MAN_ID 0xFE
114 #define LM90_REG_R_CHIP_ID 0xFF
115 #define LM90_REG_R_CONFIG1 0x03
116 #define LM90_REG_W_CONFIG1 0x09
117 #define LM90_REG_R_CONFIG2 0xBF
118 #define LM90_REG_W_CONFIG2 0xBF
119 #define LM90_REG_R_CONVRATE 0x04
120 #define LM90_REG_W_CONVRATE 0x0A
121 #define LM90_REG_R_STATUS 0x02
122 #define LM90_REG_R_LOCAL_TEMP 0x00
123 #define LM90_REG_R_LOCAL_HIGH 0x05
124 #define LM90_REG_W_LOCAL_HIGH 0x0B
125 #define LM90_REG_R_LOCAL_LOW 0x06
126 #define LM90_REG_W_LOCAL_LOW 0x0C
127 #define LM90_REG_R_LOCAL_CRIT 0x20
128 #define LM90_REG_W_LOCAL_CRIT 0x20
129 #define LM90_REG_R_REMOTE_TEMPH 0x01
130 #define LM90_REG_R_REMOTE_TEMPL 0x10
131 #define LM90_REG_R_REMOTE_OFFSH 0x11
132 #define LM90_REG_W_REMOTE_OFFSH 0x11
133 #define LM90_REG_R_REMOTE_OFFSL 0x12
134 #define LM90_REG_W_REMOTE_OFFSL 0x12
135 #define LM90_REG_R_REMOTE_HIGHH 0x07
136 #define LM90_REG_W_REMOTE_HIGHH 0x0D
137 #define LM90_REG_R_REMOTE_HIGHL 0x13
138 #define LM90_REG_W_REMOTE_HIGHL 0x13
139 #define LM90_REG_R_REMOTE_LOWH 0x08
140 #define LM90_REG_W_REMOTE_LOWH 0x0E
141 #define LM90_REG_R_REMOTE_LOWL 0x14
142 #define LM90_REG_W_REMOTE_LOWL 0x14
143 #define LM90_REG_R_REMOTE_CRIT 0x19
144 #define LM90_REG_W_REMOTE_CRIT 0x19
145 #define LM90_REG_R_TCRIT_HYST 0x21
146 #define LM90_REG_W_TCRIT_HYST 0x21
148 /* MAX6646/6647/6649/6657/6658/6659/6695/6696 registers */
150 #define MAX6657_REG_R_LOCAL_TEMPL 0x11
151 #define MAX6696_REG_R_STATUS2 0x12
152 #define MAX6659_REG_R_REMOTE_EMERG 0x16
153 #define MAX6659_REG_W_REMOTE_EMERG 0x16
154 #define MAX6659_REG_R_LOCAL_EMERG 0x17
155 #define MAX6659_REG_W_LOCAL_EMERG 0x17
157 /* SA56004 registers */
159 #define SA56004_REG_R_LOCAL_TEMPL 0x22
161 #define LM90_MAX_CONVRATE_MS 16000 /* Maximum conversion rate in ms */
163 /* TMP451 registers */
164 #define TMP451_REG_R_LOCAL_TEMPL 0x15
169 #define LM90_FLAG_ADT7461_EXT (1 << 0) /* ADT7461 extended mode */
170 /* Device features */
171 #define LM90_HAVE_OFFSET (1 << 1) /* temperature offset register */
172 #define LM90_HAVE_REM_LIMIT_EXT (1 << 3) /* extended remote limit */
173 #define LM90_HAVE_EMERGENCY (1 << 4) /* 3rd upper (emergency) limit */
174 #define LM90_HAVE_EMERGENCY_ALARM (1 << 5)/* emergency alarm */
175 #define LM90_HAVE_TEMP3 (1 << 6) /* 3rd temperature sensor */
176 #define LM90_HAVE_BROKEN_ALERT (1 << 7) /* Broken alert */
179 #define LM90_STATUS_LTHRM (1 << 0) /* local THERM limit tripped */
180 #define LM90_STATUS_RTHRM (1 << 1) /* remote THERM limit tripped */
181 #define LM90_STATUS_ROPEN (1 << 2) /* remote is an open circuit */
182 #define LM90_STATUS_RLOW (1 << 3) /* remote low temp limit tripped */
183 #define LM90_STATUS_RHIGH (1 << 4) /* remote high temp limit tripped */
184 #define LM90_STATUS_LLOW (1 << 5) /* local low temp limit tripped */
185 #define LM90_STATUS_LHIGH (1 << 6) /* local high temp limit tripped */
187 #define MAX6696_STATUS2_R2THRM (1 << 1) /* remote2 THERM limit tripped */
188 #define MAX6696_STATUS2_R2OPEN (1 << 2) /* remote2 is an open circuit */
189 #define MAX6696_STATUS2_R2LOW (1 << 3) /* remote2 low temp limit tripped */
190 #define MAX6696_STATUS2_R2HIGH (1 << 4) /* remote2 high temp limit tripped */
191 #define MAX6696_STATUS2_ROT2 (1 << 5) /* remote emergency limit tripped */
192 #define MAX6696_STATUS2_R2OT2 (1 << 6) /* remote2 emergency limit tripped */
193 #define MAX6696_STATUS2_LOT2 (1 << 7) /* local emergency limit tripped */
196 * Driver data (common to all clients)
199 static const struct i2c_device_id lm90_id[] = {
200 { "adm1032", adm1032 },
201 { "adt7461", adt7461 },
202 { "adt7461a", adt7461 },
208 { "max6646", max6646 },
209 { "max6647", max6646 },
210 { "max6649", max6646 },
211 { "max6657", max6657 },
212 { "max6658", max6657 },
213 { "max6659", max6659 },
214 { "max6680", max6680 },
215 { "max6681", max6680 },
216 { "max6695", max6696 },
217 { "max6696", max6696 },
218 { "nct1008", adt7461 },
219 { "w83l771", w83l771 },
220 { "sa56004", sa56004 },
221 { "tmp451", tmp451 },
224 MODULE_DEVICE_TABLE(i2c, lm90_id);
226 static const struct of_device_id __maybe_unused lm90_of_match[] = {
228 .compatible = "adi,adm1032",
229 .data = (void *)adm1032
232 .compatible = "adi,adt7461",
233 .data = (void *)adt7461
236 .compatible = "adi,adt7461a",
237 .data = (void *)adt7461
240 .compatible = "gmt,g781",
244 .compatible = "national,lm90",
248 .compatible = "national,lm86",
252 .compatible = "national,lm89",
256 .compatible = "national,lm99",
260 .compatible = "dallas,max6646",
261 .data = (void *)max6646
264 .compatible = "dallas,max6647",
265 .data = (void *)max6646
268 .compatible = "dallas,max6649",
269 .data = (void *)max6646
272 .compatible = "dallas,max6657",
273 .data = (void *)max6657
276 .compatible = "dallas,max6658",
277 .data = (void *)max6657
280 .compatible = "dallas,max6659",
281 .data = (void *)max6659
284 .compatible = "dallas,max6680",
285 .data = (void *)max6680
288 .compatible = "dallas,max6681",
289 .data = (void *)max6680
292 .compatible = "dallas,max6695",
293 .data = (void *)max6696
296 .compatible = "dallas,max6696",
297 .data = (void *)max6696
300 .compatible = "onnn,nct1008",
301 .data = (void *)adt7461
304 .compatible = "winbond,w83l771",
305 .data = (void *)w83l771
308 .compatible = "nxp,sa56004",
309 .data = (void *)sa56004
312 .compatible = "ti,tmp451",
313 .data = (void *)tmp451
317 MODULE_DEVICE_TABLE(of, lm90_of_match);
320 * chip type specific parameters
323 u32 flags; /* Capabilities */
324 u16 alert_alarms; /* Which alarm bits trigger ALERT# */
325 /* Upper 8 bits for max6695/96 */
326 u8 max_convrate; /* Maximum conversion rate register value */
327 u8 reg_local_ext; /* Extended local temp register (optional) */
330 static const struct lm90_params lm90_params[] = {
332 .flags = LM90_HAVE_OFFSET | LM90_HAVE_REM_LIMIT_EXT
333 | LM90_HAVE_BROKEN_ALERT,
334 .alert_alarms = 0x7c,
338 .flags = LM90_HAVE_OFFSET | LM90_HAVE_REM_LIMIT_EXT
339 | LM90_HAVE_BROKEN_ALERT,
340 .alert_alarms = 0x7c,
344 .flags = LM90_HAVE_OFFSET | LM90_HAVE_REM_LIMIT_EXT
345 | LM90_HAVE_BROKEN_ALERT,
346 .alert_alarms = 0x7c,
350 .flags = LM90_HAVE_OFFSET | LM90_HAVE_REM_LIMIT_EXT,
351 .alert_alarms = 0x7b,
355 .flags = LM90_HAVE_OFFSET | LM90_HAVE_REM_LIMIT_EXT,
356 .alert_alarms = 0x7b,
360 .flags = LM90_HAVE_OFFSET | LM90_HAVE_REM_LIMIT_EXT,
361 .alert_alarms = 0x7b,
365 .alert_alarms = 0x7c,
367 .reg_local_ext = MAX6657_REG_R_LOCAL_TEMPL,
370 .alert_alarms = 0x7c,
372 .reg_local_ext = MAX6657_REG_R_LOCAL_TEMPL,
375 .flags = LM90_HAVE_EMERGENCY,
376 .alert_alarms = 0x7c,
378 .reg_local_ext = MAX6657_REG_R_LOCAL_TEMPL,
381 .flags = LM90_HAVE_OFFSET,
382 .alert_alarms = 0x7c,
386 .flags = LM90_HAVE_EMERGENCY
387 | LM90_HAVE_EMERGENCY_ALARM | LM90_HAVE_TEMP3,
388 .alert_alarms = 0x1c7c,
390 .reg_local_ext = MAX6657_REG_R_LOCAL_TEMPL,
393 .flags = LM90_HAVE_OFFSET | LM90_HAVE_REM_LIMIT_EXT,
394 .alert_alarms = 0x7c,
398 .flags = LM90_HAVE_OFFSET | LM90_HAVE_REM_LIMIT_EXT,
399 .alert_alarms = 0x7b,
401 .reg_local_ext = SA56004_REG_R_LOCAL_TEMPL,
404 .flags = LM90_HAVE_OFFSET | LM90_HAVE_REM_LIMIT_EXT
405 | LM90_HAVE_BROKEN_ALERT,
406 .alert_alarms = 0x7c,
408 .reg_local_ext = TMP451_REG_R_LOCAL_TEMPL,
413 * TEMP8 register index
415 enum lm90_temp8_reg_index {
420 LOCAL_EMERG, /* max6659 and max6695/96 */
421 REMOTE_EMERG, /* max6659 and max6695/96 */
422 REMOTE2_CRIT, /* max6695/96 only */
423 REMOTE2_EMERG, /* max6695/96 only */
428 * TEMP11 register index
430 enum lm90_temp11_reg_index {
434 REMOTE_OFFSET, /* except max6646, max6657/58/59, and max6695/96 */
436 REMOTE2_TEMP, /* max6695/96 only */
437 REMOTE2_LOW, /* max6695/96 only */
438 REMOTE2_HIGH, /* max6695/96 only */
443 * Client data (each client gets its own)
447 struct i2c_client *client;
448 u32 channel_config[4];
449 struct hwmon_channel_info temp_info;
450 const struct hwmon_channel_info *info[3];
451 struct hwmon_chip_info chip;
452 struct mutex update_lock;
453 bool valid; /* true if register values are valid */
454 unsigned long last_updated; /* in jiffies */
458 unsigned int update_interval; /* in milliseconds */
460 u8 config_orig; /* Original configuration register value */
461 u8 convrate_orig; /* Original conversion rate register value */
462 u16 alert_alarms; /* Which alarm bits trigger ALERT# */
463 /* Upper 8 bits for max6695/96 */
464 u8 max_convrate; /* Maximum conversion rate */
465 u8 reg_local_ext; /* local extension register offset */
467 /* registers values */
468 s8 temp8[TEMP8_REG_NUM];
469 s16 temp11[TEMP11_REG_NUM];
471 u16 alarms; /* bitvector (upper 8 bits for max6695/96) */
479 * The ADM1032 supports PEC but not on write byte transactions, so we need
480 * to explicitly ask for a transaction without PEC.
482 static inline s32 adm1032_write_byte(struct i2c_client *client, u8 value)
484 return i2c_smbus_xfer(client->adapter, client->addr,
485 client->flags & ~I2C_CLIENT_PEC,
486 I2C_SMBUS_WRITE, value, I2C_SMBUS_BYTE, NULL);
490 * It is assumed that client->update_lock is held (unless we are in
491 * detection or initialization steps). This matters when PEC is enabled,
492 * because we don't want the address pointer to change between the write
493 * byte and the read byte transactions.
495 static int lm90_read_reg(struct i2c_client *client, u8 reg)
499 if (client->flags & I2C_CLIENT_PEC) {
500 err = adm1032_write_byte(client, reg);
502 err = i2c_smbus_read_byte(client);
504 err = i2c_smbus_read_byte_data(client, reg);
509 static int lm90_read16(struct i2c_client *client, u8 regh, u8 regl)
514 * There is a trick here. We have to read two registers to have the
515 * sensor temperature, but we have to beware a conversion could occur
516 * between the readings. The datasheet says we should either use
517 * the one-shot conversion register, which we don't want to do
518 * (disables hardware monitoring) or monitor the busy bit, which is
519 * impossible (we can't read the values and monitor that bit at the
520 * exact same time). So the solution used here is to read the high
521 * byte once, then the low byte, then the high byte again. If the new
522 * high byte matches the old one, then we have a valid reading. Else
523 * we have to read the low byte again, and now we believe we have a
526 oldh = lm90_read_reg(client, regh);
529 l = lm90_read_reg(client, regl);
532 newh = lm90_read_reg(client, regh);
536 l = lm90_read_reg(client, regl);
540 return (newh << 8) | l;
544 * client->update_lock must be held when calling this function (unless we are
545 * in detection or initialization steps), and while a remote channel other
546 * than channel 0 is selected. Also, calling code must make sure to re-select
547 * external channel 0 before releasing the lock. This is necessary because
548 * various registers have different meanings as a result of selecting a
549 * non-default remote channel.
551 static inline int lm90_select_remote_channel(struct i2c_client *client,
552 struct lm90_data *data,
557 if (data->kind == max6696) {
558 config = lm90_read_reg(client, LM90_REG_R_CONFIG1);
564 i2c_smbus_write_byte_data(client, LM90_REG_W_CONFIG1,
571 * Set conversion rate.
572 * client->update_lock must be held when calling this function (unless we are
573 * in detection or initialization steps).
575 static int lm90_set_convrate(struct i2c_client *client, struct lm90_data *data,
576 unsigned int interval)
578 unsigned int update_interval;
581 /* Shift calculations to avoid rounding errors */
584 /* find the nearest update rate */
585 for (i = 0, update_interval = LM90_MAX_CONVRATE_MS << 6;
586 i < data->max_convrate; i++, update_interval >>= 1)
587 if (interval >= update_interval * 3 / 4)
590 err = i2c_smbus_write_byte_data(client, LM90_REG_W_CONVRATE, i);
591 data->update_interval = DIV_ROUND_CLOSEST(update_interval, 64);
595 static int lm90_update_limits(struct device *dev)
597 struct lm90_data *data = dev_get_drvdata(dev);
598 struct i2c_client *client = data->client;
601 val = lm90_read_reg(client, LM90_REG_R_LOCAL_CRIT);
604 data->temp8[LOCAL_CRIT] = val;
606 val = lm90_read_reg(client, LM90_REG_R_REMOTE_CRIT);
609 data->temp8[REMOTE_CRIT] = val;
611 val = lm90_read_reg(client, LM90_REG_R_TCRIT_HYST);
614 data->temp_hyst = val;
616 val = lm90_read_reg(client, LM90_REG_R_REMOTE_LOWH);
619 data->temp11[REMOTE_LOW] = val << 8;
621 if (data->flags & LM90_HAVE_REM_LIMIT_EXT) {
622 val = lm90_read_reg(client, LM90_REG_R_REMOTE_LOWL);
625 data->temp11[REMOTE_LOW] |= val;
628 val = lm90_read_reg(client, LM90_REG_R_REMOTE_HIGHH);
631 data->temp11[REMOTE_HIGH] = val << 8;
633 if (data->flags & LM90_HAVE_REM_LIMIT_EXT) {
634 val = lm90_read_reg(client, LM90_REG_R_REMOTE_HIGHL);
637 data->temp11[REMOTE_HIGH] |= val;
640 if (data->flags & LM90_HAVE_OFFSET) {
641 val = lm90_read16(client, LM90_REG_R_REMOTE_OFFSH,
642 LM90_REG_R_REMOTE_OFFSL);
645 data->temp11[REMOTE_OFFSET] = val;
648 if (data->flags & LM90_HAVE_EMERGENCY) {
649 val = lm90_read_reg(client, MAX6659_REG_R_LOCAL_EMERG);
652 data->temp8[LOCAL_EMERG] = val;
654 val = lm90_read_reg(client, MAX6659_REG_R_REMOTE_EMERG);
657 data->temp8[REMOTE_EMERG] = val;
660 if (data->kind == max6696) {
661 val = lm90_select_remote_channel(client, data, 1);
665 val = lm90_read_reg(client, LM90_REG_R_REMOTE_CRIT);
668 data->temp8[REMOTE2_CRIT] = val;
670 val = lm90_read_reg(client, MAX6659_REG_R_REMOTE_EMERG);
673 data->temp8[REMOTE2_EMERG] = val;
675 val = lm90_read_reg(client, LM90_REG_R_REMOTE_LOWH);
678 data->temp11[REMOTE2_LOW] = val << 8;
680 val = lm90_read_reg(client, LM90_REG_R_REMOTE_HIGHH);
683 data->temp11[REMOTE2_HIGH] = val << 8;
685 lm90_select_remote_channel(client, data, 0);
691 static int lm90_update_device(struct device *dev)
693 struct lm90_data *data = dev_get_drvdata(dev);
694 struct i2c_client *client = data->client;
695 unsigned long next_update;
699 val = lm90_update_limits(dev);
704 next_update = data->last_updated +
705 msecs_to_jiffies(data->update_interval);
706 if (time_after(jiffies, next_update) || !data->valid) {
707 dev_dbg(&client->dev, "Updating lm90 data.\n");
711 val = lm90_read_reg(client, LM90_REG_R_LOCAL_LOW);
714 data->temp8[LOCAL_LOW] = val;
716 val = lm90_read_reg(client, LM90_REG_R_LOCAL_HIGH);
719 data->temp8[LOCAL_HIGH] = val;
721 if (data->reg_local_ext) {
722 val = lm90_read16(client, LM90_REG_R_LOCAL_TEMP,
723 data->reg_local_ext);
726 data->temp11[LOCAL_TEMP] = val;
728 val = lm90_read_reg(client, LM90_REG_R_LOCAL_TEMP);
731 data->temp11[LOCAL_TEMP] = val << 8;
733 val = lm90_read16(client, LM90_REG_R_REMOTE_TEMPH,
734 LM90_REG_R_REMOTE_TEMPL);
737 data->temp11[REMOTE_TEMP] = val;
739 val = lm90_read_reg(client, LM90_REG_R_STATUS);
742 data->alarms = val; /* lower 8 bit of alarms */
744 if (data->kind == max6696) {
745 val = lm90_select_remote_channel(client, data, 1);
749 val = lm90_read16(client, LM90_REG_R_REMOTE_TEMPH,
750 LM90_REG_R_REMOTE_TEMPL);
752 lm90_select_remote_channel(client, data, 0);
755 data->temp11[REMOTE2_TEMP] = val;
757 lm90_select_remote_channel(client, data, 0);
759 val = lm90_read_reg(client, MAX6696_REG_R_STATUS2);
762 data->alarms |= val << 8;
766 * Re-enable ALERT# output if it was originally enabled and
767 * relevant alarms are all clear
769 if (!(data->config_orig & 0x80) &&
770 !(data->alarms & data->alert_alarms)) {
771 val = lm90_read_reg(client, LM90_REG_R_CONFIG1);
776 dev_dbg(&client->dev, "Re-enabling ALERT#\n");
777 i2c_smbus_write_byte_data(client,
783 data->last_updated = jiffies;
792 * For local temperatures and limits, critical limits and the hysteresis
793 * value, the LM90 uses signed 8-bit values with LSB = 1 degree Celsius.
794 * For remote temperatures and limits, it uses signed 11-bit values with
795 * LSB = 0.125 degree Celsius, left-justified in 16-bit registers. Some
796 * Maxim chips use unsigned values.
799 static inline int temp_from_s8(s8 val)
804 static inline int temp_from_u8(u8 val)
809 static inline int temp_from_s16(s16 val)
811 return val / 32 * 125;
814 static inline int temp_from_u16(u16 val)
816 return val / 32 * 125;
819 static s8 temp_to_s8(long val)
826 return (val - 500) / 1000;
827 return (val + 500) / 1000;
830 static u8 temp_to_u8(long val)
836 return (val + 500) / 1000;
839 static s16 temp_to_s16(long val)
846 return (val - 62) / 125 * 32;
847 return (val + 62) / 125 * 32;
850 static u8 hyst_to_reg(long val)
856 return (val + 500) / 1000;
860 * ADT7461 in compatibility mode is almost identical to LM90 except that
861 * attempts to write values that are outside the range 0 < temp < 127 are
862 * treated as the boundary value.
864 * ADT7461 in "extended mode" operation uses unsigned integers offset by
865 * 64 (e.g., 0 -> -64 degC). The range is restricted to -64..191 degC.
867 static inline int temp_from_u8_adt7461(struct lm90_data *data, u8 val)
869 if (data->flags & LM90_FLAG_ADT7461_EXT)
870 return (val - 64) * 1000;
871 return temp_from_s8(val);
874 static inline int temp_from_u16_adt7461(struct lm90_data *data, u16 val)
876 if (data->flags & LM90_FLAG_ADT7461_EXT)
877 return (val - 0x4000) / 64 * 250;
878 return temp_from_s16(val);
881 static u8 temp_to_u8_adt7461(struct lm90_data *data, long val)
883 if (data->flags & LM90_FLAG_ADT7461_EXT) {
888 return (val + 500 + 64000) / 1000;
894 return (val + 500) / 1000;
897 static u16 temp_to_u16_adt7461(struct lm90_data *data, long val)
899 if (data->flags & LM90_FLAG_ADT7461_EXT) {
904 return (val + 64000 + 125) / 250 * 64;
910 return (val + 125) / 250 * 64;
913 /* pec used for ADM1032 only */
914 static ssize_t pec_show(struct device *dev, struct device_attribute *dummy,
917 struct i2c_client *client = to_i2c_client(dev);
919 return sprintf(buf, "%d\n", !!(client->flags & I2C_CLIENT_PEC));
922 static ssize_t pec_store(struct device *dev, struct device_attribute *dummy,
923 const char *buf, size_t count)
925 struct i2c_client *client = to_i2c_client(dev);
929 err = kstrtol(buf, 10, &val);
935 client->flags &= ~I2C_CLIENT_PEC;
938 client->flags |= I2C_CLIENT_PEC;
947 static DEVICE_ATTR_RW(pec);
949 static int lm90_get_temp11(struct lm90_data *data, int index)
951 s16 temp11 = data->temp11[index];
954 if (data->kind == adt7461 || data->kind == tmp451)
955 temp = temp_from_u16_adt7461(data, temp11);
956 else if (data->kind == max6646)
957 temp = temp_from_u16(temp11);
959 temp = temp_from_s16(temp11);
961 /* +16 degrees offset for temp2 for the LM99 */
962 if (data->kind == lm99 && index <= 2)
968 static int lm90_set_temp11(struct lm90_data *data, int index, long val)
974 [REMOTE_LOW] = { LM90_REG_W_REMOTE_LOWH, LM90_REG_W_REMOTE_LOWL },
975 [REMOTE_HIGH] = { LM90_REG_W_REMOTE_HIGHH, LM90_REG_W_REMOTE_HIGHL },
976 [REMOTE_OFFSET] = { LM90_REG_W_REMOTE_OFFSH, LM90_REG_W_REMOTE_OFFSL },
977 [REMOTE2_LOW] = { LM90_REG_W_REMOTE_LOWH, LM90_REG_W_REMOTE_LOWL },
978 [REMOTE2_HIGH] = { LM90_REG_W_REMOTE_HIGHH, LM90_REG_W_REMOTE_HIGHL }
980 struct i2c_client *client = data->client;
981 struct reg *regp = ®[index];
984 /* +16 degrees offset for temp2 for the LM99 */
985 if (data->kind == lm99 && index <= 2)
988 if (data->kind == adt7461 || data->kind == tmp451)
989 data->temp11[index] = temp_to_u16_adt7461(data, val);
990 else if (data->kind == max6646)
991 data->temp11[index] = temp_to_u8(val) << 8;
992 else if (data->flags & LM90_HAVE_REM_LIMIT_EXT)
993 data->temp11[index] = temp_to_s16(val);
995 data->temp11[index] = temp_to_s8(val) << 8;
997 lm90_select_remote_channel(client, data, index >= 3);
998 err = i2c_smbus_write_byte_data(client, regp->high,
999 data->temp11[index] >> 8);
1002 if (data->flags & LM90_HAVE_REM_LIMIT_EXT)
1003 err = i2c_smbus_write_byte_data(client, regp->low,
1004 data->temp11[index] & 0xff);
1006 lm90_select_remote_channel(client, data, 0);
1010 static int lm90_get_temp8(struct lm90_data *data, int index)
1012 s8 temp8 = data->temp8[index];
1015 if (data->kind == adt7461 || data->kind == tmp451)
1016 temp = temp_from_u8_adt7461(data, temp8);
1017 else if (data->kind == max6646)
1018 temp = temp_from_u8(temp8);
1020 temp = temp_from_s8(temp8);
1022 /* +16 degrees offset for temp2 for the LM99 */
1023 if (data->kind == lm99 && index == 3)
1029 static int lm90_set_temp8(struct lm90_data *data, int index, long val)
1031 static const u8 reg[TEMP8_REG_NUM] = {
1032 LM90_REG_W_LOCAL_LOW,
1033 LM90_REG_W_LOCAL_HIGH,
1034 LM90_REG_W_LOCAL_CRIT,
1035 LM90_REG_W_REMOTE_CRIT,
1036 MAX6659_REG_W_LOCAL_EMERG,
1037 MAX6659_REG_W_REMOTE_EMERG,
1038 LM90_REG_W_REMOTE_CRIT,
1039 MAX6659_REG_W_REMOTE_EMERG,
1041 struct i2c_client *client = data->client;
1044 /* +16 degrees offset for temp2 for the LM99 */
1045 if (data->kind == lm99 && index == 3)
1048 if (data->kind == adt7461 || data->kind == tmp451)
1049 data->temp8[index] = temp_to_u8_adt7461(data, val);
1050 else if (data->kind == max6646)
1051 data->temp8[index] = temp_to_u8(val);
1053 data->temp8[index] = temp_to_s8(val);
1055 lm90_select_remote_channel(client, data, index >= 6);
1056 err = i2c_smbus_write_byte_data(client, reg[index], data->temp8[index]);
1057 lm90_select_remote_channel(client, data, 0);
1062 static int lm90_get_temphyst(struct lm90_data *data, int index)
1066 if (data->kind == adt7461 || data->kind == tmp451)
1067 temp = temp_from_u8_adt7461(data, data->temp8[index]);
1068 else if (data->kind == max6646)
1069 temp = temp_from_u8(data->temp8[index]);
1071 temp = temp_from_s8(data->temp8[index]);
1073 /* +16 degrees offset for temp2 for the LM99 */
1074 if (data->kind == lm99 && index == 3)
1077 return temp - temp_from_s8(data->temp_hyst);
1080 static int lm90_set_temphyst(struct lm90_data *data, long val)
1082 struct i2c_client *client = data->client;
1086 if (data->kind == adt7461 || data->kind == tmp451)
1087 temp = temp_from_u8_adt7461(data, data->temp8[LOCAL_CRIT]);
1088 else if (data->kind == max6646)
1089 temp = temp_from_u8(data->temp8[LOCAL_CRIT]);
1091 temp = temp_from_s8(data->temp8[LOCAL_CRIT]);
1093 data->temp_hyst = hyst_to_reg(temp - val);
1094 err = i2c_smbus_write_byte_data(client, LM90_REG_W_TCRIT_HYST,
1099 static const u8 lm90_temp_index[3] = {
1100 LOCAL_TEMP, REMOTE_TEMP, REMOTE2_TEMP
1103 static const u8 lm90_temp_min_index[3] = {
1104 LOCAL_LOW, REMOTE_LOW, REMOTE2_LOW
1107 static const u8 lm90_temp_max_index[3] = {
1108 LOCAL_HIGH, REMOTE_HIGH, REMOTE2_HIGH
1111 static const u8 lm90_temp_crit_index[3] = {
1112 LOCAL_CRIT, REMOTE_CRIT, REMOTE2_CRIT
1115 static const u8 lm90_temp_emerg_index[3] = {
1116 LOCAL_EMERG, REMOTE_EMERG, REMOTE2_EMERG
1119 static const u8 lm90_min_alarm_bits[3] = { 5, 3, 11 };
1120 static const u8 lm90_max_alarm_bits[3] = { 6, 4, 12 };
1121 static const u8 lm90_crit_alarm_bits[3] = { 0, 1, 9 };
1122 static const u8 lm90_emergency_alarm_bits[3] = { 15, 13, 14 };
1123 static const u8 lm90_fault_bits[3] = { 0, 2, 10 };
1125 static int lm90_temp_read(struct device *dev, u32 attr, int channel, long *val)
1127 struct lm90_data *data = dev_get_drvdata(dev);
1130 mutex_lock(&data->update_lock);
1131 err = lm90_update_device(dev);
1132 mutex_unlock(&data->update_lock);
1137 case hwmon_temp_input:
1138 *val = lm90_get_temp11(data, lm90_temp_index[channel]);
1140 case hwmon_temp_min_alarm:
1141 *val = (data->alarms >> lm90_min_alarm_bits[channel]) & 1;
1143 case hwmon_temp_max_alarm:
1144 *val = (data->alarms >> lm90_max_alarm_bits[channel]) & 1;
1146 case hwmon_temp_crit_alarm:
1147 *val = (data->alarms >> lm90_crit_alarm_bits[channel]) & 1;
1149 case hwmon_temp_emergency_alarm:
1150 *val = (data->alarms >> lm90_emergency_alarm_bits[channel]) & 1;
1152 case hwmon_temp_fault:
1153 *val = (data->alarms >> lm90_fault_bits[channel]) & 1;
1155 case hwmon_temp_min:
1157 *val = lm90_get_temp8(data,
1158 lm90_temp_min_index[channel]);
1160 *val = lm90_get_temp11(data,
1161 lm90_temp_min_index[channel]);
1163 case hwmon_temp_max:
1165 *val = lm90_get_temp8(data,
1166 lm90_temp_max_index[channel]);
1168 *val = lm90_get_temp11(data,
1169 lm90_temp_max_index[channel]);
1171 case hwmon_temp_crit:
1172 *val = lm90_get_temp8(data, lm90_temp_crit_index[channel]);
1174 case hwmon_temp_crit_hyst:
1175 *val = lm90_get_temphyst(data, lm90_temp_crit_index[channel]);
1177 case hwmon_temp_emergency:
1178 *val = lm90_get_temp8(data, lm90_temp_emerg_index[channel]);
1180 case hwmon_temp_emergency_hyst:
1181 *val = lm90_get_temphyst(data, lm90_temp_emerg_index[channel]);
1183 case hwmon_temp_offset:
1184 *val = lm90_get_temp11(data, REMOTE_OFFSET);
1192 static int lm90_temp_write(struct device *dev, u32 attr, int channel, long val)
1194 struct lm90_data *data = dev_get_drvdata(dev);
1197 mutex_lock(&data->update_lock);
1199 err = lm90_update_device(dev);
1204 case hwmon_temp_min:
1206 err = lm90_set_temp8(data,
1207 lm90_temp_min_index[channel],
1210 err = lm90_set_temp11(data,
1211 lm90_temp_min_index[channel],
1214 case hwmon_temp_max:
1216 err = lm90_set_temp8(data,
1217 lm90_temp_max_index[channel],
1220 err = lm90_set_temp11(data,
1221 lm90_temp_max_index[channel],
1224 case hwmon_temp_crit:
1225 err = lm90_set_temp8(data, lm90_temp_crit_index[channel], val);
1227 case hwmon_temp_crit_hyst:
1228 err = lm90_set_temphyst(data, val);
1230 case hwmon_temp_emergency:
1231 err = lm90_set_temp8(data, lm90_temp_emerg_index[channel], val);
1233 case hwmon_temp_offset:
1234 err = lm90_set_temp11(data, REMOTE_OFFSET, val);
1241 mutex_unlock(&data->update_lock);
1246 static umode_t lm90_temp_is_visible(const void *data, u32 attr, int channel)
1249 case hwmon_temp_input:
1250 case hwmon_temp_min_alarm:
1251 case hwmon_temp_max_alarm:
1252 case hwmon_temp_crit_alarm:
1253 case hwmon_temp_emergency_alarm:
1254 case hwmon_temp_emergency_hyst:
1255 case hwmon_temp_fault:
1257 case hwmon_temp_min:
1258 case hwmon_temp_max:
1259 case hwmon_temp_crit:
1260 case hwmon_temp_emergency:
1261 case hwmon_temp_offset:
1263 case hwmon_temp_crit_hyst:
1272 static int lm90_chip_read(struct device *dev, u32 attr, int channel, long *val)
1274 struct lm90_data *data = dev_get_drvdata(dev);
1277 mutex_lock(&data->update_lock);
1278 err = lm90_update_device(dev);
1279 mutex_unlock(&data->update_lock);
1284 case hwmon_chip_update_interval:
1285 *val = data->update_interval;
1287 case hwmon_chip_alarms:
1288 *val = data->alarms;
1297 static int lm90_chip_write(struct device *dev, u32 attr, int channel, long val)
1299 struct lm90_data *data = dev_get_drvdata(dev);
1300 struct i2c_client *client = data->client;
1303 mutex_lock(&data->update_lock);
1305 err = lm90_update_device(dev);
1310 case hwmon_chip_update_interval:
1311 err = lm90_set_convrate(client, data,
1312 clamp_val(val, 0, 100000));
1319 mutex_unlock(&data->update_lock);
1324 static umode_t lm90_chip_is_visible(const void *data, u32 attr, int channel)
1327 case hwmon_chip_update_interval:
1329 case hwmon_chip_alarms:
1336 static int lm90_read(struct device *dev, enum hwmon_sensor_types type,
1337 u32 attr, int channel, long *val)
1341 return lm90_chip_read(dev, attr, channel, val);
1343 return lm90_temp_read(dev, attr, channel, val);
1349 static int lm90_write(struct device *dev, enum hwmon_sensor_types type,
1350 u32 attr, int channel, long val)
1354 return lm90_chip_write(dev, attr, channel, val);
1356 return lm90_temp_write(dev, attr, channel, val);
1362 static umode_t lm90_is_visible(const void *data, enum hwmon_sensor_types type,
1363 u32 attr, int channel)
1367 return lm90_chip_is_visible(data, attr, channel);
1369 return lm90_temp_is_visible(data, attr, channel);
1375 /* Return 0 if detection is successful, -ENODEV otherwise */
1376 static int lm90_detect(struct i2c_client *client,
1377 struct i2c_board_info *info)
1379 struct i2c_adapter *adapter = client->adapter;
1380 int address = client->addr;
1381 const char *name = NULL;
1382 int man_id, chip_id, config1, config2, convrate;
1384 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
1387 /* detection and identification */
1388 man_id = i2c_smbus_read_byte_data(client, LM90_REG_R_MAN_ID);
1389 chip_id = i2c_smbus_read_byte_data(client, LM90_REG_R_CHIP_ID);
1390 config1 = i2c_smbus_read_byte_data(client, LM90_REG_R_CONFIG1);
1391 convrate = i2c_smbus_read_byte_data(client, LM90_REG_R_CONVRATE);
1392 if (man_id < 0 || chip_id < 0 || config1 < 0 || convrate < 0)
1395 if (man_id == 0x01 || man_id == 0x5C || man_id == 0x41) {
1396 config2 = i2c_smbus_read_byte_data(client, LM90_REG_R_CONFIG2);
1400 config2 = 0; /* Make compiler happy */
1402 if ((address == 0x4C || address == 0x4D)
1403 && man_id == 0x01) { /* National Semiconductor */
1404 if ((config1 & 0x2A) == 0x00
1405 && (config2 & 0xF8) == 0x00
1406 && convrate <= 0x09) {
1408 && (chip_id & 0xF0) == 0x20) { /* LM90 */
1411 if ((chip_id & 0xF0) == 0x30) { /* LM89/LM99 */
1413 dev_info(&adapter->dev,
1414 "Assuming LM99 chip at 0x%02x\n",
1416 dev_info(&adapter->dev,
1417 "If it is an LM89, instantiate it "
1418 "with the new_device sysfs "
1422 && (chip_id & 0xF0) == 0x10) { /* LM86 */
1427 if ((address == 0x4C || address == 0x4D)
1428 && man_id == 0x41) { /* Analog Devices */
1429 if ((chip_id & 0xF0) == 0x40 /* ADM1032 */
1430 && (config1 & 0x3F) == 0x00
1431 && convrate <= 0x0A) {
1434 * The ADM1032 supports PEC, but only if combined
1435 * transactions are not used.
1437 if (i2c_check_functionality(adapter,
1438 I2C_FUNC_SMBUS_BYTE))
1439 info->flags |= I2C_CLIENT_PEC;
1441 if (chip_id == 0x51 /* ADT7461 */
1442 && (config1 & 0x1B) == 0x00
1443 && convrate <= 0x0A) {
1446 if (chip_id == 0x57 /* ADT7461A, NCT1008 */
1447 && (config1 & 0x1B) == 0x00
1448 && convrate <= 0x0A) {
1452 if (man_id == 0x4D) { /* Maxim */
1453 int emerg, emerg2, status2;
1456 * We read MAX6659_REG_R_REMOTE_EMERG twice, and re-read
1457 * LM90_REG_R_MAN_ID in between. If MAX6659_REG_R_REMOTE_EMERG
1458 * exists, both readings will reflect the same value. Otherwise,
1459 * the readings will be different.
1461 emerg = i2c_smbus_read_byte_data(client,
1462 MAX6659_REG_R_REMOTE_EMERG);
1463 man_id = i2c_smbus_read_byte_data(client,
1465 emerg2 = i2c_smbus_read_byte_data(client,
1466 MAX6659_REG_R_REMOTE_EMERG);
1467 status2 = i2c_smbus_read_byte_data(client,
1468 MAX6696_REG_R_STATUS2);
1469 if (emerg < 0 || man_id < 0 || emerg2 < 0 || status2 < 0)
1473 * The MAX6657, MAX6658 and MAX6659 do NOT have a chip_id
1474 * register. Reading from that address will return the last
1475 * read value, which in our case is those of the man_id
1476 * register. Likewise, the config1 register seems to lack a
1477 * low nibble, so the value will be those of the previous
1478 * read, so in our case those of the man_id register.
1479 * MAX6659 has a third set of upper temperature limit registers.
1480 * Those registers also return values on MAX6657 and MAX6658,
1481 * thus the only way to detect MAX6659 is by its address.
1482 * For this reason it will be mis-detected as MAX6657 if its
1485 if (chip_id == man_id
1486 && (address == 0x4C || address == 0x4D || address == 0x4E)
1487 && (config1 & 0x1F) == (man_id & 0x0F)
1488 && convrate <= 0x09) {
1489 if (address == 0x4C)
1495 * Even though MAX6695 and MAX6696 do not have a chip ID
1496 * register, reading it returns 0x01. Bit 4 of the config1
1497 * register is unused and should return zero when read. Bit 0 of
1498 * the status2 register is unused and should return zero when
1501 * MAX6695 and MAX6696 have an additional set of temperature
1502 * limit registers. We can detect those chips by checking if
1503 * one of those registers exists.
1506 && (config1 & 0x10) == 0x00
1507 && (status2 & 0x01) == 0x00
1509 && convrate <= 0x07) {
1513 * The chip_id register of the MAX6680 and MAX6681 holds the
1514 * revision of the chip. The lowest bit of the config1 register
1515 * is unused and should return zero when read, so should the
1516 * second to last bit of config1 (software reset).
1519 && (config1 & 0x03) == 0x00
1520 && convrate <= 0x07) {
1524 * The chip_id register of the MAX6646/6647/6649 holds the
1525 * revision of the chip. The lowest 6 bits of the config1
1526 * register are unused and should return zero when read.
1529 && (config1 & 0x3f) == 0x00
1530 && convrate <= 0x07) {
1535 && man_id == 0x5C) { /* Winbond/Nuvoton */
1536 if ((config1 & 0x2A) == 0x00
1537 && (config2 & 0xF8) == 0x00) {
1538 if (chip_id == 0x01 /* W83L771W/G */
1539 && convrate <= 0x09) {
1542 if ((chip_id & 0xFE) == 0x10 /* W83L771AWG/ASG */
1543 && convrate <= 0x08) {
1548 if (address >= 0x48 && address <= 0x4F
1549 && man_id == 0xA1) { /* NXP Semiconductor/Philips */
1551 && (config1 & 0x2A) == 0x00
1552 && (config2 & 0xFE) == 0x00
1553 && convrate <= 0x09) {
1557 if ((address == 0x4C || address == 0x4D)
1558 && man_id == 0x47) { /* GMT */
1559 if (chip_id == 0x01 /* G781 */
1560 && (config1 & 0x3F) == 0x00
1561 && convrate <= 0x08)
1565 && man_id == 0x55) { /* Texas Instruments */
1568 local_ext = i2c_smbus_read_byte_data(client,
1569 TMP451_REG_R_LOCAL_TEMPL);
1571 if (chip_id == 0x00 /* TMP451 */
1572 && (config1 & 0x1B) == 0x00
1574 && (local_ext & 0x0F) == 0x00)
1578 if (!name) { /* identification failed */
1579 dev_dbg(&adapter->dev,
1580 "Unsupported chip at 0x%02x (man_id=0x%02X, "
1581 "chip_id=0x%02X)\n", address, man_id, chip_id);
1585 strlcpy(info->type, name, I2C_NAME_SIZE);
1590 static void lm90_restore_conf(void *_data)
1592 struct lm90_data *data = _data;
1593 struct i2c_client *client = data->client;
1595 /* Restore initial configuration */
1596 i2c_smbus_write_byte_data(client, LM90_REG_W_CONVRATE,
1597 data->convrate_orig);
1598 i2c_smbus_write_byte_data(client, LM90_REG_W_CONFIG1,
1602 static int lm90_init_client(struct i2c_client *client, struct lm90_data *data)
1604 int config, convrate;
1606 convrate = lm90_read_reg(client, LM90_REG_R_CONVRATE);
1609 data->convrate_orig = convrate;
1612 * Start the conversions.
1614 lm90_set_convrate(client, data, 500); /* 500ms; 2Hz conversion rate */
1615 config = lm90_read_reg(client, LM90_REG_R_CONFIG1);
1618 data->config_orig = config;
1620 /* Check Temperature Range Select */
1621 if (data->kind == adt7461 || data->kind == tmp451) {
1623 data->flags |= LM90_FLAG_ADT7461_EXT;
1627 * Put MAX6680/MAX8881 into extended resolution (bit 0x10,
1628 * 0.125 degree resolution) and range (0x08, extend range
1629 * to -64 degree) mode for the remote temperature sensor.
1631 if (data->kind == max6680)
1635 * Select external channel 0 for max6695/96
1637 if (data->kind == max6696)
1640 config &= 0xBF; /* run */
1641 if (config != data->config_orig) /* Only write if changed */
1642 i2c_smbus_write_byte_data(client, LM90_REG_W_CONFIG1, config);
1644 return devm_add_action_or_reset(&client->dev, lm90_restore_conf, data);
1647 static bool lm90_is_tripped(struct i2c_client *client, u16 *status)
1649 struct lm90_data *data = i2c_get_clientdata(client);
1652 st = lm90_read_reg(client, LM90_REG_R_STATUS);
1656 if (data->kind == max6696) {
1657 st2 = lm90_read_reg(client, MAX6696_REG_R_STATUS2);
1662 *status = st | (st2 << 8);
1664 if ((st & 0x7f) == 0 && (st2 & 0xfe) == 0)
1667 if ((st & (LM90_STATUS_LLOW | LM90_STATUS_LHIGH | LM90_STATUS_LTHRM)) ||
1668 (st2 & MAX6696_STATUS2_LOT2))
1669 dev_warn(&client->dev,
1670 "temp%d out of range, please check!\n", 1);
1671 if ((st & (LM90_STATUS_RLOW | LM90_STATUS_RHIGH | LM90_STATUS_RTHRM)) ||
1672 (st2 & MAX6696_STATUS2_ROT2))
1673 dev_warn(&client->dev,
1674 "temp%d out of range, please check!\n", 2);
1675 if (st & LM90_STATUS_ROPEN)
1676 dev_warn(&client->dev,
1677 "temp%d diode open, please check!\n", 2);
1678 if (st2 & (MAX6696_STATUS2_R2LOW | MAX6696_STATUS2_R2HIGH |
1679 MAX6696_STATUS2_R2THRM | MAX6696_STATUS2_R2OT2))
1680 dev_warn(&client->dev,
1681 "temp%d out of range, please check!\n", 3);
1682 if (st2 & MAX6696_STATUS2_R2OPEN)
1683 dev_warn(&client->dev,
1684 "temp%d diode open, please check!\n", 3);
1689 static irqreturn_t lm90_irq_thread(int irq, void *dev_id)
1691 struct i2c_client *client = dev_id;
1694 if (lm90_is_tripped(client, &status))
1700 static void lm90_remove_pec(void *dev)
1702 device_remove_file(dev, &dev_attr_pec);
1705 static void lm90_regulator_disable(void *regulator)
1707 regulator_disable(regulator);
1711 static const struct hwmon_ops lm90_ops = {
1712 .is_visible = lm90_is_visible,
1714 .write = lm90_write,
1717 static int lm90_probe(struct i2c_client *client,
1718 const struct i2c_device_id *id)
1720 struct device *dev = &client->dev;
1721 struct i2c_adapter *adapter = to_i2c_adapter(dev->parent);
1722 struct hwmon_channel_info *info;
1723 struct regulator *regulator;
1724 struct device *hwmon_dev;
1725 struct lm90_data *data;
1728 regulator = devm_regulator_get(dev, "vcc");
1729 if (IS_ERR(regulator))
1730 return PTR_ERR(regulator);
1732 err = regulator_enable(regulator);
1734 dev_err(dev, "Failed to enable regulator: %d\n", err);
1738 err = devm_add_action_or_reset(dev, lm90_regulator_disable, regulator);
1742 data = devm_kzalloc(dev, sizeof(struct lm90_data), GFP_KERNEL);
1746 data->client = client;
1747 i2c_set_clientdata(client, data);
1748 mutex_init(&data->update_lock);
1750 /* Set the device type */
1751 if (client->dev.of_node)
1752 data->kind = (enum chips)of_device_get_match_data(&client->dev);
1754 data->kind = id->driver_data;
1755 if (data->kind == adm1032) {
1756 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE))
1757 client->flags &= ~I2C_CLIENT_PEC;
1761 * Different devices have different alarm bits triggering the
1764 data->alert_alarms = lm90_params[data->kind].alert_alarms;
1766 /* Set chip capabilities */
1767 data->flags = lm90_params[data->kind].flags;
1769 data->chip.ops = &lm90_ops;
1770 data->chip.info = data->info;
1772 data->info[0] = HWMON_CHANNEL_INFO(chip,
1773 HWMON_C_REGISTER_TZ | HWMON_C_UPDATE_INTERVAL | HWMON_C_ALARMS);
1774 data->info[1] = &data->temp_info;
1776 info = &data->temp_info;
1777 info->type = hwmon_temp;
1778 info->config = data->channel_config;
1780 data->channel_config[0] = HWMON_T_INPUT | HWMON_T_MIN | HWMON_T_MAX |
1781 HWMON_T_CRIT | HWMON_T_CRIT_HYST | HWMON_T_MIN_ALARM |
1782 HWMON_T_MAX_ALARM | HWMON_T_CRIT_ALARM;
1783 data->channel_config[1] = HWMON_T_INPUT | HWMON_T_MIN | HWMON_T_MAX |
1784 HWMON_T_CRIT | HWMON_T_CRIT_HYST | HWMON_T_MIN_ALARM |
1785 HWMON_T_MAX_ALARM | HWMON_T_CRIT_ALARM | HWMON_T_FAULT;
1787 if (data->flags & LM90_HAVE_OFFSET)
1788 data->channel_config[1] |= HWMON_T_OFFSET;
1790 if (data->flags & LM90_HAVE_EMERGENCY) {
1791 data->channel_config[0] |= HWMON_T_EMERGENCY |
1792 HWMON_T_EMERGENCY_HYST;
1793 data->channel_config[1] |= HWMON_T_EMERGENCY |
1794 HWMON_T_EMERGENCY_HYST;
1797 if (data->flags & LM90_HAVE_EMERGENCY_ALARM) {
1798 data->channel_config[0] |= HWMON_T_EMERGENCY_ALARM;
1799 data->channel_config[1] |= HWMON_T_EMERGENCY_ALARM;
1802 if (data->flags & LM90_HAVE_TEMP3) {
1803 data->channel_config[2] = HWMON_T_INPUT |
1804 HWMON_T_MIN | HWMON_T_MAX |
1805 HWMON_T_CRIT | HWMON_T_CRIT_HYST |
1806 HWMON_T_EMERGENCY | HWMON_T_EMERGENCY_HYST |
1807 HWMON_T_MIN_ALARM | HWMON_T_MAX_ALARM |
1808 HWMON_T_CRIT_ALARM | HWMON_T_EMERGENCY_ALARM |
1812 data->reg_local_ext = lm90_params[data->kind].reg_local_ext;
1814 /* Set maximum conversion rate */
1815 data->max_convrate = lm90_params[data->kind].max_convrate;
1817 /* Initialize the LM90 chip */
1818 err = lm90_init_client(client, data);
1820 dev_err(dev, "Failed to initialize device\n");
1825 * The 'pec' attribute is attached to the i2c device and thus created
1828 if (client->flags & I2C_CLIENT_PEC) {
1829 err = device_create_file(dev, &dev_attr_pec);
1832 err = devm_add_action_or_reset(dev, lm90_remove_pec, dev);
1837 hwmon_dev = devm_hwmon_device_register_with_info(dev, client->name,
1840 if (IS_ERR(hwmon_dev))
1841 return PTR_ERR(hwmon_dev);
1844 dev_dbg(dev, "IRQ: %d\n", client->irq);
1845 err = devm_request_threaded_irq(dev, client->irq,
1846 NULL, lm90_irq_thread,
1847 IRQF_TRIGGER_LOW | IRQF_ONESHOT,
1850 dev_err(dev, "cannot request IRQ %d\n", client->irq);
1858 static void lm90_alert(struct i2c_client *client, enum i2c_alert_protocol type,
1863 if (type != I2C_PROTOCOL_SMBUS_ALERT)
1866 if (lm90_is_tripped(client, &alarms)) {
1868 * Disable ALERT# output, because these chips don't implement
1869 * SMBus alert correctly; they should only hold the alert line
1872 struct lm90_data *data = i2c_get_clientdata(client);
1874 if ((data->flags & LM90_HAVE_BROKEN_ALERT) &&
1875 (alarms & data->alert_alarms)) {
1878 dev_dbg(&client->dev, "Disabling ALERT#\n");
1879 config = lm90_read_reg(client, LM90_REG_R_CONFIG1);
1881 i2c_smbus_write_byte_data(client,
1886 dev_info(&client->dev, "Everything OK\n");
1890 static struct i2c_driver lm90_driver = {
1891 .class = I2C_CLASS_HWMON,
1894 .of_match_table = of_match_ptr(lm90_of_match),
1896 .probe = lm90_probe,
1897 .alert = lm90_alert,
1898 .id_table = lm90_id,
1899 .detect = lm90_detect,
1900 .address_list = normal_i2c,
1903 module_i2c_driver(lm90_driver);
1905 MODULE_AUTHOR("Jean Delvare <jdelvare@suse.de>");
1906 MODULE_DESCRIPTION("LM90/ADM1032 driver");
1907 MODULE_LICENSE("GPL");