1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * lm90.c - Part of lm_sensors, Linux kernel modules for hardware
5 * Copyright (C) 2003-2010 Jean Delvare <jdelvare@suse.de>
7 * Based on the lm83 driver. The LM90 is a sensor chip made by National
8 * Semiconductor. It reports up to two temperatures (its own plus up to
9 * one external one) with a 0.125 deg resolution (1 deg for local
10 * temperature) and a 3-4 deg accuracy.
12 * This driver also supports the LM89 and LM99, two other sensor chips
13 * made by National Semiconductor. Both have an increased remote
14 * temperature measurement accuracy (1 degree), and the LM99
15 * additionally shifts remote temperatures (measured and limits) by 16
16 * degrees, which allows for higher temperatures measurement.
17 * Note that there is no way to differentiate between both chips.
18 * When device is auto-detected, the driver will assume an LM99.
20 * This driver also supports the LM86, another sensor chip made by
21 * National Semiconductor. It is exactly similar to the LM90 except it
22 * has a higher accuracy.
24 * This driver also supports the ADM1032, a sensor chip made by Analog
25 * Devices. That chip is similar to the LM90, with a few differences
26 * that are not handled by this driver. Among others, it has a higher
27 * accuracy than the LM90, much like the LM86 does.
29 * This driver also supports the MAX6657, MAX6658 and MAX6659 sensor
30 * chips made by Maxim. These chips are similar to the LM86.
31 * Note that there is no easy way to differentiate between the three
32 * variants. We use the device address to detect MAX6659, which will result
33 * in a detection as max6657 if it is on address 0x4c. The extra address
34 * and features of the MAX6659 are only supported if the chip is configured
35 * explicitly as max6659, or if its address is not 0x4c.
36 * These chips lack the remote temperature offset feature.
38 * This driver also supports the MAX6654 chip made by Maxim. This chip can be
39 * at 9 different addresses, similar to MAX6680/MAX6681. The MAX6654 is similar
40 * to MAX6657/MAX6658/MAX6659, but does not support critical temperature
41 * limits. Extended range is available by setting the configuration register
42 * accordingly, and is done during initialization. Extended precision is only
43 * available at conversion rates of 1 Hz and slower. Note that extended
44 * precision is not enabled by default, as this driver initializes all chips
47 * This driver also supports the MAX6646, MAX6647, MAX6648, MAX6649 and
48 * MAX6692 chips made by Maxim. These are again similar to the LM86,
49 * but they use unsigned temperature values and can report temperatures
50 * from 0 to 145 degrees.
52 * This driver also supports the MAX6680 and MAX6681, two other sensor
53 * chips made by Maxim. These are quite similar to the other Maxim
54 * chips. The MAX6680 and MAX6681 only differ in the pinout so they can
55 * be treated identically.
57 * This driver also supports the MAX6695 and MAX6696, two other sensor
58 * chips made by Maxim. These are also quite similar to other Maxim
59 * chips, but support three temperature sensors instead of two. MAX6695
60 * and MAX6696 only differ in the pinout so they can be treated identically.
62 * This driver also supports ADT7461 and ADT7461A from Analog Devices as well as
63 * NCT1008 from ON Semiconductor. The chips are supported in both compatibility
64 * and extended mode. They are mostly compatible with LM90 except for a data
65 * format difference for the temperature value registers.
67 * This driver also supports the SA56004 from Philips. This device is
68 * pin-compatible with the LM86, the ED/EDP parts are also address-compatible.
70 * This driver also supports the G781 from GMT. This device is compatible
73 * This driver also supports TMP451 and TMP461 from Texas Instruments.
74 * Those devices are supported in both compatibility and extended mode.
75 * They are mostly compatible with ADT7461 except for local temperature
76 * low byte register and max conversion rate.
78 * Since the LM90 was the first chipset supported by this driver, most
79 * comments will refer to this chipset, but are actually general and
80 * concern all supported chipsets, unless mentioned otherwise.
83 #include <linux/module.h>
84 #include <linux/init.h>
85 #include <linux/slab.h>
86 #include <linux/jiffies.h>
87 #include <linux/i2c.h>
88 #include <linux/hwmon.h>
89 #include <linux/err.h>
90 #include <linux/mutex.h>
91 #include <linux/of_device.h>
92 #include <linux/sysfs.h>
93 #include <linux/interrupt.h>
94 #include <linux/regulator/consumer.h>
98 * Address is fully defined internally and cannot be changed except for
99 * MAX6659, MAX6680 and MAX6681.
100 * LM86, LM89, LM90, LM99, ADM1032, ADM1032-1, ADT7461, ADT7461A, MAX6649,
101 * MAX6657, MAX6658, NCT1008 and W83L771 have address 0x4c.
102 * ADM1032-2, ADT7461-2, ADT7461A-2, LM89-1, LM99-1, MAX6646, and NCT1008D
104 * MAX6647 has address 0x4e.
105 * MAX6659 can have address 0x4c, 0x4d or 0x4e.
106 * MAX6654, MAX6680, and MAX6681 can have address 0x18, 0x19, 0x1a, 0x29,
107 * 0x2a, 0x2b, 0x4c, 0x4d or 0x4e.
108 * SA56004 can have address 0x48 through 0x4F.
111 static const unsigned short normal_i2c[] = {
112 0x18, 0x19, 0x1a, 0x29, 0x2a, 0x2b, 0x48, 0x49, 0x4a, 0x4b, 0x4c,
113 0x4d, 0x4e, 0x4f, I2C_CLIENT_END };
115 enum chips { lm90, adm1032, lm99, lm86, max6657, max6659, adt7461, max6680,
116 max6646, w83l771, max6696, sa56004, g781, tmp451, tmp461, max6654 };
122 #define LM90_REG_R_MAN_ID 0xFE
123 #define LM90_REG_R_CHIP_ID 0xFF
124 #define LM90_REG_R_CONFIG1 0x03
125 #define LM90_REG_W_CONFIG1 0x09
126 #define LM90_REG_R_CONFIG2 0xBF
127 #define LM90_REG_W_CONFIG2 0xBF
128 #define LM90_REG_R_CONVRATE 0x04
129 #define LM90_REG_W_CONVRATE 0x0A
130 #define LM90_REG_R_STATUS 0x02
131 #define LM90_REG_R_LOCAL_TEMP 0x00
132 #define LM90_REG_R_LOCAL_HIGH 0x05
133 #define LM90_REG_W_LOCAL_HIGH 0x0B
134 #define LM90_REG_R_LOCAL_LOW 0x06
135 #define LM90_REG_W_LOCAL_LOW 0x0C
136 #define LM90_REG_R_LOCAL_CRIT 0x20
137 #define LM90_REG_W_LOCAL_CRIT 0x20
138 #define LM90_REG_R_REMOTE_TEMPH 0x01
139 #define LM90_REG_R_REMOTE_TEMPL 0x10
140 #define LM90_REG_R_REMOTE_OFFSH 0x11
141 #define LM90_REG_W_REMOTE_OFFSH 0x11
142 #define LM90_REG_R_REMOTE_OFFSL 0x12
143 #define LM90_REG_W_REMOTE_OFFSL 0x12
144 #define LM90_REG_R_REMOTE_HIGHH 0x07
145 #define LM90_REG_W_REMOTE_HIGHH 0x0D
146 #define LM90_REG_R_REMOTE_HIGHL 0x13
147 #define LM90_REG_W_REMOTE_HIGHL 0x13
148 #define LM90_REG_R_REMOTE_LOWH 0x08
149 #define LM90_REG_W_REMOTE_LOWH 0x0E
150 #define LM90_REG_R_REMOTE_LOWL 0x14
151 #define LM90_REG_W_REMOTE_LOWL 0x14
152 #define LM90_REG_R_REMOTE_CRIT 0x19
153 #define LM90_REG_W_REMOTE_CRIT 0x19
154 #define LM90_REG_R_TCRIT_HYST 0x21
155 #define LM90_REG_W_TCRIT_HYST 0x21
157 /* MAX6646/6647/6649/6654/6657/6658/6659/6695/6696 registers */
159 #define MAX6657_REG_R_LOCAL_TEMPL 0x11
160 #define MAX6696_REG_R_STATUS2 0x12
161 #define MAX6659_REG_R_REMOTE_EMERG 0x16
162 #define MAX6659_REG_W_REMOTE_EMERG 0x16
163 #define MAX6659_REG_R_LOCAL_EMERG 0x17
164 #define MAX6659_REG_W_LOCAL_EMERG 0x17
166 /* SA56004 registers */
168 #define SA56004_REG_R_LOCAL_TEMPL 0x22
170 #define LM90_MAX_CONVRATE_MS 16000 /* Maximum conversion rate in ms */
172 /* TMP451/TMP461 registers */
173 #define TMP451_REG_R_LOCAL_TEMPL 0x15
174 #define TMP451_REG_CONALERT 0x22
176 #define TMP461_REG_CHEN 0x16
177 #define TMP461_REG_DFC 0x24
182 #define LM90_FLAG_ADT7461_EXT (1 << 0) /* ADT7461 extended mode */
183 /* Device features */
184 #define LM90_HAVE_OFFSET (1 << 1) /* temperature offset register */
185 #define LM90_HAVE_REM_LIMIT_EXT (1 << 3) /* extended remote limit */
186 #define LM90_HAVE_EMERGENCY (1 << 4) /* 3rd upper (emergency) limit */
187 #define LM90_HAVE_EMERGENCY_ALARM (1 << 5)/* emergency alarm */
188 #define LM90_HAVE_TEMP3 (1 << 6) /* 3rd temperature sensor */
189 #define LM90_HAVE_BROKEN_ALERT (1 << 7) /* Broken alert */
190 #define LM90_HAVE_EXTENDED_TEMP (1 << 8) /* extended temperature support*/
191 #define LM90_PAUSE_FOR_CONFIG (1 << 9) /* Pause conversion for config */
192 #define LM90_HAVE_CRIT (1 << 10)/* Chip supports CRIT/OVERT register */
193 #define LM90_HAVE_CRIT_ALRM_SWP (1 << 11)/* critical alarm bits swapped */
196 #define LM90_STATUS_LTHRM (1 << 0) /* local THERM limit tripped */
197 #define LM90_STATUS_RTHRM (1 << 1) /* remote THERM limit tripped */
198 #define LM90_STATUS_ROPEN (1 << 2) /* remote is an open circuit */
199 #define LM90_STATUS_RLOW (1 << 3) /* remote low temp limit tripped */
200 #define LM90_STATUS_RHIGH (1 << 4) /* remote high temp limit tripped */
201 #define LM90_STATUS_LLOW (1 << 5) /* local low temp limit tripped */
202 #define LM90_STATUS_LHIGH (1 << 6) /* local high temp limit tripped */
203 #define LM90_STATUS_BUSY (1 << 7) /* conversion is ongoing */
205 #define MAX6696_STATUS2_R2THRM (1 << 1) /* remote2 THERM limit tripped */
206 #define MAX6696_STATUS2_R2OPEN (1 << 2) /* remote2 is an open circuit */
207 #define MAX6696_STATUS2_R2LOW (1 << 3) /* remote2 low temp limit tripped */
208 #define MAX6696_STATUS2_R2HIGH (1 << 4) /* remote2 high temp limit tripped */
209 #define MAX6696_STATUS2_ROT2 (1 << 5) /* remote emergency limit tripped */
210 #define MAX6696_STATUS2_R2OT2 (1 << 6) /* remote2 emergency limit tripped */
211 #define MAX6696_STATUS2_LOT2 (1 << 7) /* local emergency limit tripped */
214 * Driver data (common to all clients)
217 static const struct i2c_device_id lm90_id[] = {
218 { "adm1032", adm1032 },
219 { "adt7461", adt7461 },
220 { "adt7461a", adt7461 },
226 { "max6646", max6646 },
227 { "max6647", max6646 },
228 { "max6649", max6646 },
229 { "max6654", max6654 },
230 { "max6657", max6657 },
231 { "max6658", max6657 },
232 { "max6659", max6659 },
233 { "max6680", max6680 },
234 { "max6681", max6680 },
235 { "max6695", max6696 },
236 { "max6696", max6696 },
237 { "nct1008", adt7461 },
238 { "w83l771", w83l771 },
239 { "sa56004", sa56004 },
240 { "tmp451", tmp451 },
241 { "tmp461", tmp461 },
244 MODULE_DEVICE_TABLE(i2c, lm90_id);
246 static const struct of_device_id __maybe_unused lm90_of_match[] = {
248 .compatible = "adi,adm1032",
249 .data = (void *)adm1032
252 .compatible = "adi,adt7461",
253 .data = (void *)adt7461
256 .compatible = "adi,adt7461a",
257 .data = (void *)adt7461
260 .compatible = "gmt,g781",
264 .compatible = "national,lm90",
268 .compatible = "national,lm86",
272 .compatible = "national,lm89",
276 .compatible = "national,lm99",
280 .compatible = "dallas,max6646",
281 .data = (void *)max6646
284 .compatible = "dallas,max6647",
285 .data = (void *)max6646
288 .compatible = "dallas,max6649",
289 .data = (void *)max6646
292 .compatible = "dallas,max6654",
293 .data = (void *)max6654
296 .compatible = "dallas,max6657",
297 .data = (void *)max6657
300 .compatible = "dallas,max6658",
301 .data = (void *)max6657
304 .compatible = "dallas,max6659",
305 .data = (void *)max6659
308 .compatible = "dallas,max6680",
309 .data = (void *)max6680
312 .compatible = "dallas,max6681",
313 .data = (void *)max6680
316 .compatible = "dallas,max6695",
317 .data = (void *)max6696
320 .compatible = "dallas,max6696",
321 .data = (void *)max6696
324 .compatible = "onnn,nct1008",
325 .data = (void *)adt7461
328 .compatible = "winbond,w83l771",
329 .data = (void *)w83l771
332 .compatible = "nxp,sa56004",
333 .data = (void *)sa56004
336 .compatible = "ti,tmp451",
337 .data = (void *)tmp451
340 .compatible = "ti,tmp461",
341 .data = (void *)tmp461
345 MODULE_DEVICE_TABLE(of, lm90_of_match);
348 * chip type specific parameters
351 u32 flags; /* Capabilities */
352 u16 alert_alarms; /* Which alarm bits trigger ALERT# */
353 /* Upper 8 bits for max6695/96 */
354 u8 max_convrate; /* Maximum conversion rate register value */
355 u8 reg_local_ext; /* Extended local temp register (optional) */
358 static const struct lm90_params lm90_params[] = {
360 .flags = LM90_HAVE_OFFSET | LM90_HAVE_REM_LIMIT_EXT
361 | LM90_HAVE_BROKEN_ALERT | LM90_HAVE_CRIT,
362 .alert_alarms = 0x7c,
366 .flags = LM90_HAVE_OFFSET | LM90_HAVE_REM_LIMIT_EXT
367 | LM90_HAVE_BROKEN_ALERT | LM90_HAVE_EXTENDED_TEMP
369 .alert_alarms = 0x7c,
373 .flags = LM90_HAVE_OFFSET | LM90_HAVE_REM_LIMIT_EXT
374 | LM90_HAVE_BROKEN_ALERT | LM90_HAVE_CRIT,
375 .alert_alarms = 0x7c,
379 .flags = LM90_HAVE_OFFSET | LM90_HAVE_REM_LIMIT_EXT
381 .alert_alarms = 0x7b,
385 .flags = LM90_HAVE_OFFSET | LM90_HAVE_REM_LIMIT_EXT
387 .alert_alarms = 0x7b,
391 .flags = LM90_HAVE_OFFSET | LM90_HAVE_REM_LIMIT_EXT
393 .alert_alarms = 0x7b,
397 .flags = LM90_HAVE_CRIT | LM90_HAVE_BROKEN_ALERT,
398 .alert_alarms = 0x7c,
400 .reg_local_ext = MAX6657_REG_R_LOCAL_TEMPL,
403 .flags = LM90_HAVE_BROKEN_ALERT,
404 .alert_alarms = 0x7c,
406 .reg_local_ext = MAX6657_REG_R_LOCAL_TEMPL,
409 .flags = LM90_PAUSE_FOR_CONFIG | LM90_HAVE_CRIT,
410 .alert_alarms = 0x7c,
412 .reg_local_ext = MAX6657_REG_R_LOCAL_TEMPL,
415 .flags = LM90_HAVE_EMERGENCY | LM90_HAVE_CRIT,
416 .alert_alarms = 0x7c,
418 .reg_local_ext = MAX6657_REG_R_LOCAL_TEMPL,
421 .flags = LM90_HAVE_OFFSET | LM90_HAVE_CRIT
422 | LM90_HAVE_CRIT_ALRM_SWP | LM90_HAVE_BROKEN_ALERT,
423 .alert_alarms = 0x7c,
427 .flags = LM90_HAVE_EMERGENCY
428 | LM90_HAVE_EMERGENCY_ALARM | LM90_HAVE_TEMP3 | LM90_HAVE_CRIT,
429 .alert_alarms = 0x1c7c,
431 .reg_local_ext = MAX6657_REG_R_LOCAL_TEMPL,
434 .flags = LM90_HAVE_OFFSET | LM90_HAVE_REM_LIMIT_EXT | LM90_HAVE_CRIT,
435 .alert_alarms = 0x7c,
439 .flags = LM90_HAVE_OFFSET | LM90_HAVE_REM_LIMIT_EXT | LM90_HAVE_CRIT,
440 .alert_alarms = 0x7b,
442 .reg_local_ext = SA56004_REG_R_LOCAL_TEMPL,
445 .flags = LM90_HAVE_OFFSET | LM90_HAVE_REM_LIMIT_EXT
446 | LM90_HAVE_BROKEN_ALERT | LM90_HAVE_EXTENDED_TEMP | LM90_HAVE_CRIT,
447 .alert_alarms = 0x7c,
449 .reg_local_ext = TMP451_REG_R_LOCAL_TEMPL,
452 .flags = LM90_HAVE_OFFSET | LM90_HAVE_REM_LIMIT_EXT
453 | LM90_HAVE_BROKEN_ALERT | LM90_HAVE_EXTENDED_TEMP | LM90_HAVE_CRIT,
454 .alert_alarms = 0x7c,
456 .reg_local_ext = TMP451_REG_R_LOCAL_TEMPL,
461 * TEMP8 register index
463 enum lm90_temp8_reg_index {
468 LOCAL_EMERG, /* max6659 and max6695/96 */
469 REMOTE_EMERG, /* max6659 and max6695/96 */
470 REMOTE2_CRIT, /* max6695/96 only */
471 REMOTE2_EMERG, /* max6695/96 only */
476 * TEMP11 register index
478 enum lm90_temp11_reg_index {
482 REMOTE_OFFSET, /* except max6646, max6657/58/59, and max6695/96 */
484 REMOTE2_TEMP, /* max6695/96 only */
485 REMOTE2_LOW, /* max6695/96 only */
486 REMOTE2_HIGH, /* max6695/96 only */
491 * Client data (each client gets its own)
495 struct i2c_client *client;
496 struct device *hwmon_dev;
497 u32 channel_config[4];
498 struct hwmon_channel_info temp_info;
499 const struct hwmon_channel_info *info[3];
500 struct hwmon_chip_info chip;
501 struct mutex update_lock;
502 bool valid; /* true if register values are valid */
503 unsigned long last_updated; /* in jiffies */
507 unsigned int update_interval; /* in milliseconds */
509 u8 config; /* Current configuration register value */
510 u8 config_orig; /* Original configuration register value */
511 u8 convrate_orig; /* Original conversion rate register value */
512 u16 alert_alarms; /* Which alarm bits trigger ALERT# */
513 /* Upper 8 bits for max6695/96 */
514 u8 max_convrate; /* Maximum conversion rate */
515 u8 reg_local_ext; /* local extension register offset */
517 /* registers values */
518 s8 temp8[TEMP8_REG_NUM];
519 s16 temp11[TEMP11_REG_NUM];
521 u16 alarms; /* bitvector (upper 8 bits for max6695/96) */
529 * The ADM1032 supports PEC but not on write byte transactions, so we need
530 * to explicitly ask for a transaction without PEC.
532 static inline s32 adm1032_write_byte(struct i2c_client *client, u8 value)
534 return i2c_smbus_xfer(client->adapter, client->addr,
535 client->flags & ~I2C_CLIENT_PEC,
536 I2C_SMBUS_WRITE, value, I2C_SMBUS_BYTE, NULL);
540 * It is assumed that client->update_lock is held (unless we are in
541 * detection or initialization steps). This matters when PEC is enabled,
542 * because we don't want the address pointer to change between the write
543 * byte and the read byte transactions.
545 static int lm90_read_reg(struct i2c_client *client, u8 reg)
549 if (client->flags & I2C_CLIENT_PEC) {
550 err = adm1032_write_byte(client, reg);
552 err = i2c_smbus_read_byte(client);
554 err = i2c_smbus_read_byte_data(client, reg);
559 static int lm90_read16(struct i2c_client *client, u8 regh, u8 regl)
564 * There is a trick here. We have to read two registers to have the
565 * sensor temperature, but we have to beware a conversion could occur
566 * between the readings. The datasheet says we should either use
567 * the one-shot conversion register, which we don't want to do
568 * (disables hardware monitoring) or monitor the busy bit, which is
569 * impossible (we can't read the values and monitor that bit at the
570 * exact same time). So the solution used here is to read the high
571 * byte once, then the low byte, then the high byte again. If the new
572 * high byte matches the old one, then we have a valid reading. Else
573 * we have to read the low byte again, and now we believe we have a
576 oldh = lm90_read_reg(client, regh);
579 l = lm90_read_reg(client, regl);
582 newh = lm90_read_reg(client, regh);
586 l = lm90_read_reg(client, regl);
590 return (newh << 8) | l;
593 static int lm90_update_confreg(struct lm90_data *data, u8 config)
595 if (data->config != config) {
598 err = i2c_smbus_write_byte_data(data->client,
603 data->config = config;
609 * client->update_lock must be held when calling this function (unless we are
610 * in detection or initialization steps), and while a remote channel other
611 * than channel 0 is selected. Also, calling code must make sure to re-select
612 * external channel 0 before releasing the lock. This is necessary because
613 * various registers have different meanings as a result of selecting a
614 * non-default remote channel.
616 static int lm90_select_remote_channel(struct lm90_data *data, int channel)
620 if (data->kind == max6696) {
621 u8 config = data->config & ~0x08;
625 err = lm90_update_confreg(data, config);
630 static int lm90_write_convrate(struct lm90_data *data, int val)
632 u8 config = data->config;
635 /* Save config and pause conversion */
636 if (data->flags & LM90_PAUSE_FOR_CONFIG) {
637 err = lm90_update_confreg(data, config | 0x40);
643 err = i2c_smbus_write_byte_data(data->client, LM90_REG_W_CONVRATE, val);
645 /* Revert change to config */
646 lm90_update_confreg(data, config);
652 * Set conversion rate.
653 * client->update_lock must be held when calling this function (unless we are
654 * in detection or initialization steps).
656 static int lm90_set_convrate(struct i2c_client *client, struct lm90_data *data,
657 unsigned int interval)
659 unsigned int update_interval;
662 /* Shift calculations to avoid rounding errors */
665 /* find the nearest update rate */
666 for (i = 0, update_interval = LM90_MAX_CONVRATE_MS << 6;
667 i < data->max_convrate; i++, update_interval >>= 1)
668 if (interval >= update_interval * 3 / 4)
671 err = lm90_write_convrate(data, i);
672 data->update_interval = DIV_ROUND_CLOSEST(update_interval, 64);
676 static int lm90_update_limits(struct device *dev)
678 struct lm90_data *data = dev_get_drvdata(dev);
679 struct i2c_client *client = data->client;
682 if (data->flags & LM90_HAVE_CRIT) {
683 val = lm90_read_reg(client, LM90_REG_R_LOCAL_CRIT);
686 data->temp8[LOCAL_CRIT] = val;
688 val = lm90_read_reg(client, LM90_REG_R_REMOTE_CRIT);
691 data->temp8[REMOTE_CRIT] = val;
693 val = lm90_read_reg(client, LM90_REG_R_TCRIT_HYST);
696 data->temp_hyst = val;
699 val = lm90_read_reg(client, LM90_REG_R_REMOTE_LOWH);
702 data->temp11[REMOTE_LOW] = val << 8;
704 if (data->flags & LM90_HAVE_REM_LIMIT_EXT) {
705 val = lm90_read_reg(client, LM90_REG_R_REMOTE_LOWL);
708 data->temp11[REMOTE_LOW] |= val;
711 val = lm90_read_reg(client, LM90_REG_R_REMOTE_HIGHH);
714 data->temp11[REMOTE_HIGH] = val << 8;
716 if (data->flags & LM90_HAVE_REM_LIMIT_EXT) {
717 val = lm90_read_reg(client, LM90_REG_R_REMOTE_HIGHL);
720 data->temp11[REMOTE_HIGH] |= val;
723 if (data->flags & LM90_HAVE_OFFSET) {
724 val = lm90_read16(client, LM90_REG_R_REMOTE_OFFSH,
725 LM90_REG_R_REMOTE_OFFSL);
728 data->temp11[REMOTE_OFFSET] = val;
731 if (data->flags & LM90_HAVE_EMERGENCY) {
732 val = lm90_read_reg(client, MAX6659_REG_R_LOCAL_EMERG);
735 data->temp8[LOCAL_EMERG] = val;
737 val = lm90_read_reg(client, MAX6659_REG_R_REMOTE_EMERG);
740 data->temp8[REMOTE_EMERG] = val;
743 if (data->kind == max6696) {
744 val = lm90_select_remote_channel(data, 1);
748 val = lm90_read_reg(client, LM90_REG_R_REMOTE_CRIT);
751 data->temp8[REMOTE2_CRIT] = val;
753 val = lm90_read_reg(client, MAX6659_REG_R_REMOTE_EMERG);
756 data->temp8[REMOTE2_EMERG] = val;
758 val = lm90_read_reg(client, LM90_REG_R_REMOTE_LOWH);
761 data->temp11[REMOTE2_LOW] = val << 8;
763 val = lm90_read_reg(client, LM90_REG_R_REMOTE_HIGHH);
766 data->temp11[REMOTE2_HIGH] = val << 8;
768 lm90_select_remote_channel(data, 0);
774 static int lm90_update_device(struct device *dev)
776 struct lm90_data *data = dev_get_drvdata(dev);
777 struct i2c_client *client = data->client;
778 unsigned long next_update;
782 val = lm90_update_limits(dev);
787 next_update = data->last_updated +
788 msecs_to_jiffies(data->update_interval);
789 if (time_after(jiffies, next_update) || !data->valid) {
790 dev_dbg(&client->dev, "Updating lm90 data.\n");
794 val = lm90_read_reg(client, LM90_REG_R_LOCAL_LOW);
797 data->temp8[LOCAL_LOW] = val;
799 val = lm90_read_reg(client, LM90_REG_R_LOCAL_HIGH);
802 data->temp8[LOCAL_HIGH] = val;
804 if (data->reg_local_ext) {
805 val = lm90_read16(client, LM90_REG_R_LOCAL_TEMP,
806 data->reg_local_ext);
809 data->temp11[LOCAL_TEMP] = val;
811 val = lm90_read_reg(client, LM90_REG_R_LOCAL_TEMP);
814 data->temp11[LOCAL_TEMP] = val << 8;
816 val = lm90_read16(client, LM90_REG_R_REMOTE_TEMPH,
817 LM90_REG_R_REMOTE_TEMPL);
820 data->temp11[REMOTE_TEMP] = val;
822 val = lm90_read_reg(client, LM90_REG_R_STATUS);
825 data->alarms = val & ~LM90_STATUS_BUSY;
827 if (data->kind == max6696) {
828 val = lm90_select_remote_channel(data, 1);
832 val = lm90_read16(client, LM90_REG_R_REMOTE_TEMPH,
833 LM90_REG_R_REMOTE_TEMPL);
835 lm90_select_remote_channel(data, 0);
838 data->temp11[REMOTE2_TEMP] = val;
840 lm90_select_remote_channel(data, 0);
842 val = lm90_read_reg(client, MAX6696_REG_R_STATUS2);
845 data->alarms |= val << 8;
849 * Re-enable ALERT# output if it was originally enabled and
850 * relevant alarms are all clear
852 if ((client->irq || !(data->config_orig & 0x80)) &&
853 !(data->alarms & data->alert_alarms)) {
854 if (data->config & 0x80) {
855 dev_dbg(&client->dev, "Re-enabling ALERT#\n");
856 lm90_update_confreg(data, data->config & ~0x80);
860 data->last_updated = jiffies;
869 * For local temperatures and limits, critical limits and the hysteresis
870 * value, the LM90 uses signed 8-bit values with LSB = 1 degree Celsius.
871 * For remote temperatures and limits, it uses signed 11-bit values with
872 * LSB = 0.125 degree Celsius, left-justified in 16-bit registers. Some
873 * Maxim chips use unsigned values.
876 static inline int temp_from_s8(s8 val)
881 static inline int temp_from_u8(u8 val)
886 static inline int temp_from_s16(s16 val)
888 return val / 32 * 125;
891 static inline int temp_from_u16(u16 val)
893 return val / 32 * 125;
896 static s8 temp_to_s8(long val)
903 return (val - 500) / 1000;
904 return (val + 500) / 1000;
907 static u8 temp_to_u8(long val)
913 return (val + 500) / 1000;
916 static s16 temp_to_s16(long val)
923 return (val - 62) / 125 * 32;
924 return (val + 62) / 125 * 32;
927 static u8 hyst_to_reg(long val)
933 return (val + 500) / 1000;
937 * ADT7461 in compatibility mode is almost identical to LM90 except that
938 * attempts to write values that are outside the range 0 < temp < 127 are
939 * treated as the boundary value.
941 * ADT7461 in "extended mode" operation uses unsigned integers offset by
942 * 64 (e.g., 0 -> -64 degC). The range is restricted to -64..191 degC.
944 static inline int temp_from_u8_adt7461(struct lm90_data *data, u8 val)
946 if (data->flags & LM90_FLAG_ADT7461_EXT)
947 return (val - 64) * 1000;
948 return temp_from_s8(val);
951 static inline int temp_from_u16_adt7461(struct lm90_data *data, u16 val)
953 if (data->flags & LM90_FLAG_ADT7461_EXT)
954 return (val - 0x4000) / 64 * 250;
955 return temp_from_s16(val);
958 static u8 temp_to_u8_adt7461(struct lm90_data *data, long val)
960 if (data->flags & LM90_FLAG_ADT7461_EXT) {
965 return (val + 500 + 64000) / 1000;
971 return (val + 500) / 1000;
974 static u16 temp_to_u16_adt7461(struct lm90_data *data, long val)
976 if (data->flags & LM90_FLAG_ADT7461_EXT) {
981 return (val + 64000 + 125) / 250 * 64;
987 return (val + 125) / 250 * 64;
990 /* pec used for ADM1032 only */
991 static ssize_t pec_show(struct device *dev, struct device_attribute *dummy,
994 struct i2c_client *client = to_i2c_client(dev);
996 return sprintf(buf, "%d\n", !!(client->flags & I2C_CLIENT_PEC));
999 static ssize_t pec_store(struct device *dev, struct device_attribute *dummy,
1000 const char *buf, size_t count)
1002 struct i2c_client *client = to_i2c_client(dev);
1006 err = kstrtol(buf, 10, &val);
1012 client->flags &= ~I2C_CLIENT_PEC;
1015 client->flags |= I2C_CLIENT_PEC;
1024 static DEVICE_ATTR_RW(pec);
1026 static int lm90_get_temp11(struct lm90_data *data, int index)
1028 s16 temp11 = data->temp11[index];
1031 if (data->flags & LM90_HAVE_EXTENDED_TEMP)
1032 temp = temp_from_u16_adt7461(data, temp11);
1033 else if (data->kind == max6646)
1034 temp = temp_from_u16(temp11);
1036 temp = temp_from_s16(temp11);
1038 /* +16 degrees offset for temp2 for the LM99 */
1039 if (data->kind == lm99 && index <= 2)
1045 static int lm90_set_temp11(struct lm90_data *data, int index, long val)
1051 [REMOTE_LOW] = { LM90_REG_W_REMOTE_LOWH, LM90_REG_W_REMOTE_LOWL },
1052 [REMOTE_HIGH] = { LM90_REG_W_REMOTE_HIGHH, LM90_REG_W_REMOTE_HIGHL },
1053 [REMOTE_OFFSET] = { LM90_REG_W_REMOTE_OFFSH, LM90_REG_W_REMOTE_OFFSL },
1054 [REMOTE2_LOW] = { LM90_REG_W_REMOTE_LOWH, LM90_REG_W_REMOTE_LOWL },
1055 [REMOTE2_HIGH] = { LM90_REG_W_REMOTE_HIGHH, LM90_REG_W_REMOTE_HIGHL }
1057 struct i2c_client *client = data->client;
1058 struct reg *regp = ®[index];
1061 /* +16 degrees offset for temp2 for the LM99 */
1062 if (data->kind == lm99 && index <= 2) {
1063 /* prevent integer underflow */
1064 val = max(val, -128000l);
1068 if (data->flags & LM90_HAVE_EXTENDED_TEMP)
1069 data->temp11[index] = temp_to_u16_adt7461(data, val);
1070 else if (data->kind == max6646)
1071 data->temp11[index] = temp_to_u8(val) << 8;
1072 else if (data->flags & LM90_HAVE_REM_LIMIT_EXT)
1073 data->temp11[index] = temp_to_s16(val);
1075 data->temp11[index] = temp_to_s8(val) << 8;
1077 lm90_select_remote_channel(data, index >= 3);
1078 err = i2c_smbus_write_byte_data(client, regp->high,
1079 data->temp11[index] >> 8);
1082 if (data->flags & LM90_HAVE_REM_LIMIT_EXT)
1083 err = i2c_smbus_write_byte_data(client, regp->low,
1084 data->temp11[index] & 0xff);
1086 lm90_select_remote_channel(data, 0);
1090 static int lm90_get_temp8(struct lm90_data *data, int index)
1092 s8 temp8 = data->temp8[index];
1095 if (data->flags & LM90_HAVE_EXTENDED_TEMP)
1096 temp = temp_from_u8_adt7461(data, temp8);
1097 else if (data->kind == max6646)
1098 temp = temp_from_u8(temp8);
1100 temp = temp_from_s8(temp8);
1102 /* +16 degrees offset for temp2 for the LM99 */
1103 if (data->kind == lm99 && index == 3)
1109 static int lm90_set_temp8(struct lm90_data *data, int index, long val)
1111 static const u8 reg[TEMP8_REG_NUM] = {
1112 LM90_REG_W_LOCAL_LOW,
1113 LM90_REG_W_LOCAL_HIGH,
1114 LM90_REG_W_LOCAL_CRIT,
1115 LM90_REG_W_REMOTE_CRIT,
1116 MAX6659_REG_W_LOCAL_EMERG,
1117 MAX6659_REG_W_REMOTE_EMERG,
1118 LM90_REG_W_REMOTE_CRIT,
1119 MAX6659_REG_W_REMOTE_EMERG,
1121 struct i2c_client *client = data->client;
1124 /* +16 degrees offset for temp2 for the LM99 */
1125 if (data->kind == lm99 && index == 3) {
1126 /* prevent integer underflow */
1127 val = max(val, -128000l);
1131 if (data->flags & LM90_HAVE_EXTENDED_TEMP)
1132 data->temp8[index] = temp_to_u8_adt7461(data, val);
1133 else if (data->kind == max6646)
1134 data->temp8[index] = temp_to_u8(val);
1136 data->temp8[index] = temp_to_s8(val);
1138 lm90_select_remote_channel(data, index >= 6);
1139 err = i2c_smbus_write_byte_data(client, reg[index], data->temp8[index]);
1140 lm90_select_remote_channel(data, 0);
1145 static int lm90_get_temphyst(struct lm90_data *data, int index)
1149 if (data->flags & LM90_HAVE_EXTENDED_TEMP)
1150 temp = temp_from_u8_adt7461(data, data->temp8[index]);
1151 else if (data->kind == max6646)
1152 temp = temp_from_u8(data->temp8[index]);
1154 temp = temp_from_s8(data->temp8[index]);
1156 /* +16 degrees offset for temp2 for the LM99 */
1157 if (data->kind == lm99 && index == 3)
1160 return temp - temp_from_s8(data->temp_hyst);
1163 static int lm90_set_temphyst(struct lm90_data *data, long val)
1165 struct i2c_client *client = data->client;
1169 if (data->flags & LM90_HAVE_EXTENDED_TEMP)
1170 temp = temp_from_u8_adt7461(data, data->temp8[LOCAL_CRIT]);
1171 else if (data->kind == max6646)
1172 temp = temp_from_u8(data->temp8[LOCAL_CRIT]);
1174 temp = temp_from_s8(data->temp8[LOCAL_CRIT]);
1176 /* prevent integer overflow/underflow */
1177 val = clamp_val(val, -128000l, 255000l);
1179 data->temp_hyst = hyst_to_reg(temp - val);
1180 err = i2c_smbus_write_byte_data(client, LM90_REG_W_TCRIT_HYST,
1185 static const u8 lm90_temp_index[3] = {
1186 LOCAL_TEMP, REMOTE_TEMP, REMOTE2_TEMP
1189 static const u8 lm90_temp_min_index[3] = {
1190 LOCAL_LOW, REMOTE_LOW, REMOTE2_LOW
1193 static const u8 lm90_temp_max_index[3] = {
1194 LOCAL_HIGH, REMOTE_HIGH, REMOTE2_HIGH
1197 static const u8 lm90_temp_crit_index[3] = {
1198 LOCAL_CRIT, REMOTE_CRIT, REMOTE2_CRIT
1201 static const u8 lm90_temp_emerg_index[3] = {
1202 LOCAL_EMERG, REMOTE_EMERG, REMOTE2_EMERG
1205 static const u8 lm90_min_alarm_bits[3] = { 5, 3, 11 };
1206 static const u8 lm90_max_alarm_bits[3] = { 6, 4, 12 };
1207 static const u8 lm90_crit_alarm_bits[3] = { 0, 1, 9 };
1208 static const u8 lm90_crit_alarm_bits_swapped[3] = { 1, 0, 9 };
1209 static const u8 lm90_emergency_alarm_bits[3] = { 15, 13, 14 };
1210 static const u8 lm90_fault_bits[3] = { 0, 2, 10 };
1212 static int lm90_temp_read(struct device *dev, u32 attr, int channel, long *val)
1214 struct lm90_data *data = dev_get_drvdata(dev);
1217 mutex_lock(&data->update_lock);
1218 err = lm90_update_device(dev);
1219 mutex_unlock(&data->update_lock);
1224 case hwmon_temp_input:
1225 *val = lm90_get_temp11(data, lm90_temp_index[channel]);
1227 case hwmon_temp_min_alarm:
1228 *val = (data->alarms >> lm90_min_alarm_bits[channel]) & 1;
1230 case hwmon_temp_max_alarm:
1231 *val = (data->alarms >> lm90_max_alarm_bits[channel]) & 1;
1233 case hwmon_temp_crit_alarm:
1234 if (data->flags & LM90_HAVE_CRIT_ALRM_SWP)
1235 *val = (data->alarms >> lm90_crit_alarm_bits_swapped[channel]) & 1;
1237 *val = (data->alarms >> lm90_crit_alarm_bits[channel]) & 1;
1239 case hwmon_temp_emergency_alarm:
1240 *val = (data->alarms >> lm90_emergency_alarm_bits[channel]) & 1;
1242 case hwmon_temp_fault:
1243 *val = (data->alarms >> lm90_fault_bits[channel]) & 1;
1245 case hwmon_temp_min:
1247 *val = lm90_get_temp8(data,
1248 lm90_temp_min_index[channel]);
1250 *val = lm90_get_temp11(data,
1251 lm90_temp_min_index[channel]);
1253 case hwmon_temp_max:
1255 *val = lm90_get_temp8(data,
1256 lm90_temp_max_index[channel]);
1258 *val = lm90_get_temp11(data,
1259 lm90_temp_max_index[channel]);
1261 case hwmon_temp_crit:
1262 *val = lm90_get_temp8(data, lm90_temp_crit_index[channel]);
1264 case hwmon_temp_crit_hyst:
1265 *val = lm90_get_temphyst(data, lm90_temp_crit_index[channel]);
1267 case hwmon_temp_emergency:
1268 *val = lm90_get_temp8(data, lm90_temp_emerg_index[channel]);
1270 case hwmon_temp_emergency_hyst:
1271 *val = lm90_get_temphyst(data, lm90_temp_emerg_index[channel]);
1273 case hwmon_temp_offset:
1274 *val = lm90_get_temp11(data, REMOTE_OFFSET);
1282 static int lm90_temp_write(struct device *dev, u32 attr, int channel, long val)
1284 struct lm90_data *data = dev_get_drvdata(dev);
1287 mutex_lock(&data->update_lock);
1289 err = lm90_update_device(dev);
1294 case hwmon_temp_min:
1296 err = lm90_set_temp8(data,
1297 lm90_temp_min_index[channel],
1300 err = lm90_set_temp11(data,
1301 lm90_temp_min_index[channel],
1304 case hwmon_temp_max:
1306 err = lm90_set_temp8(data,
1307 lm90_temp_max_index[channel],
1310 err = lm90_set_temp11(data,
1311 lm90_temp_max_index[channel],
1314 case hwmon_temp_crit:
1315 err = lm90_set_temp8(data, lm90_temp_crit_index[channel], val);
1317 case hwmon_temp_crit_hyst:
1318 err = lm90_set_temphyst(data, val);
1320 case hwmon_temp_emergency:
1321 err = lm90_set_temp8(data, lm90_temp_emerg_index[channel], val);
1323 case hwmon_temp_offset:
1324 err = lm90_set_temp11(data, REMOTE_OFFSET, val);
1331 mutex_unlock(&data->update_lock);
1336 static umode_t lm90_temp_is_visible(const void *data, u32 attr, int channel)
1339 case hwmon_temp_input:
1340 case hwmon_temp_min_alarm:
1341 case hwmon_temp_max_alarm:
1342 case hwmon_temp_crit_alarm:
1343 case hwmon_temp_emergency_alarm:
1344 case hwmon_temp_emergency_hyst:
1345 case hwmon_temp_fault:
1347 case hwmon_temp_min:
1348 case hwmon_temp_max:
1349 case hwmon_temp_crit:
1350 case hwmon_temp_emergency:
1351 case hwmon_temp_offset:
1353 case hwmon_temp_crit_hyst:
1362 static int lm90_chip_read(struct device *dev, u32 attr, int channel, long *val)
1364 struct lm90_data *data = dev_get_drvdata(dev);
1367 mutex_lock(&data->update_lock);
1368 err = lm90_update_device(dev);
1369 mutex_unlock(&data->update_lock);
1374 case hwmon_chip_update_interval:
1375 *val = data->update_interval;
1377 case hwmon_chip_alarms:
1378 *val = data->alarms;
1387 static int lm90_chip_write(struct device *dev, u32 attr, int channel, long val)
1389 struct lm90_data *data = dev_get_drvdata(dev);
1390 struct i2c_client *client = data->client;
1393 mutex_lock(&data->update_lock);
1395 err = lm90_update_device(dev);
1400 case hwmon_chip_update_interval:
1401 err = lm90_set_convrate(client, data,
1402 clamp_val(val, 0, 100000));
1409 mutex_unlock(&data->update_lock);
1414 static umode_t lm90_chip_is_visible(const void *data, u32 attr, int channel)
1417 case hwmon_chip_update_interval:
1419 case hwmon_chip_alarms:
1426 static int lm90_read(struct device *dev, enum hwmon_sensor_types type,
1427 u32 attr, int channel, long *val)
1431 return lm90_chip_read(dev, attr, channel, val);
1433 return lm90_temp_read(dev, attr, channel, val);
1439 static int lm90_write(struct device *dev, enum hwmon_sensor_types type,
1440 u32 attr, int channel, long val)
1444 return lm90_chip_write(dev, attr, channel, val);
1446 return lm90_temp_write(dev, attr, channel, val);
1452 static umode_t lm90_is_visible(const void *data, enum hwmon_sensor_types type,
1453 u32 attr, int channel)
1457 return lm90_chip_is_visible(data, attr, channel);
1459 return lm90_temp_is_visible(data, attr, channel);
1465 /* Return 0 if detection is successful, -ENODEV otherwise */
1466 static int lm90_detect(struct i2c_client *client,
1467 struct i2c_board_info *info)
1469 struct i2c_adapter *adapter = client->adapter;
1470 int address = client->addr;
1471 const char *name = NULL;
1472 int man_id, chip_id, config1, config2, convrate;
1474 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
1477 /* detection and identification */
1478 man_id = i2c_smbus_read_byte_data(client, LM90_REG_R_MAN_ID);
1479 chip_id = i2c_smbus_read_byte_data(client, LM90_REG_R_CHIP_ID);
1480 config1 = i2c_smbus_read_byte_data(client, LM90_REG_R_CONFIG1);
1481 convrate = i2c_smbus_read_byte_data(client, LM90_REG_R_CONVRATE);
1482 if (man_id < 0 || chip_id < 0 || config1 < 0 || convrate < 0)
1485 if (man_id == 0x01 || man_id == 0x5C || man_id == 0xA1) {
1486 config2 = i2c_smbus_read_byte_data(client, LM90_REG_R_CONFIG2);
1491 if ((address == 0x4C || address == 0x4D)
1492 && man_id == 0x01) { /* National Semiconductor */
1493 if ((config1 & 0x2A) == 0x00
1494 && (config2 & 0xF8) == 0x00
1495 && convrate <= 0x09) {
1497 && (chip_id & 0xF0) == 0x20) { /* LM90 */
1500 if ((chip_id & 0xF0) == 0x30) { /* LM89/LM99 */
1502 dev_info(&adapter->dev,
1503 "Assuming LM99 chip at 0x%02x\n",
1505 dev_info(&adapter->dev,
1506 "If it is an LM89, instantiate it "
1507 "with the new_device sysfs "
1511 && (chip_id & 0xF0) == 0x10) { /* LM86 */
1516 if ((address == 0x4C || address == 0x4D)
1517 && man_id == 0x41) { /* Analog Devices */
1518 if ((chip_id & 0xF0) == 0x40 /* ADM1032 */
1519 && (config1 & 0x3F) == 0x00
1520 && convrate <= 0x0A) {
1523 * The ADM1032 supports PEC, but only if combined
1524 * transactions are not used.
1526 if (i2c_check_functionality(adapter,
1527 I2C_FUNC_SMBUS_BYTE))
1528 info->flags |= I2C_CLIENT_PEC;
1530 if (chip_id == 0x51 /* ADT7461 */
1531 && (config1 & 0x1B) == 0x00
1532 && convrate <= 0x0A) {
1535 if (chip_id == 0x57 /* ADT7461A, NCT1008 */
1536 && (config1 & 0x1B) == 0x00
1537 && convrate <= 0x0A) {
1541 if (man_id == 0x4D) { /* Maxim */
1542 int emerg, emerg2, status2;
1545 * We read MAX6659_REG_R_REMOTE_EMERG twice, and re-read
1546 * LM90_REG_R_MAN_ID in between. If MAX6659_REG_R_REMOTE_EMERG
1547 * exists, both readings will reflect the same value. Otherwise,
1548 * the readings will be different.
1550 emerg = i2c_smbus_read_byte_data(client,
1551 MAX6659_REG_R_REMOTE_EMERG);
1552 man_id = i2c_smbus_read_byte_data(client,
1554 emerg2 = i2c_smbus_read_byte_data(client,
1555 MAX6659_REG_R_REMOTE_EMERG);
1556 status2 = i2c_smbus_read_byte_data(client,
1557 MAX6696_REG_R_STATUS2);
1558 if (emerg < 0 || man_id < 0 || emerg2 < 0 || status2 < 0)
1562 * The MAX6657, MAX6658 and MAX6659 do NOT have a chip_id
1563 * register. Reading from that address will return the last
1564 * read value, which in our case is those of the man_id
1565 * register. Likewise, the config1 register seems to lack a
1566 * low nibble, so the value will be those of the previous
1567 * read, so in our case those of the man_id register.
1568 * MAX6659 has a third set of upper temperature limit registers.
1569 * Those registers also return values on MAX6657 and MAX6658,
1570 * thus the only way to detect MAX6659 is by its address.
1571 * For this reason it will be mis-detected as MAX6657 if its
1574 if (chip_id == man_id
1575 && (address == 0x4C || address == 0x4D || address == 0x4E)
1576 && (config1 & 0x1F) == (man_id & 0x0F)
1577 && convrate <= 0x09) {
1578 if (address == 0x4C)
1584 * Even though MAX6695 and MAX6696 do not have a chip ID
1585 * register, reading it returns 0x01. Bit 4 of the config1
1586 * register is unused and should return zero when read. Bit 0 of
1587 * the status2 register is unused and should return zero when
1590 * MAX6695 and MAX6696 have an additional set of temperature
1591 * limit registers. We can detect those chips by checking if
1592 * one of those registers exists.
1595 && (config1 & 0x10) == 0x00
1596 && (status2 & 0x01) == 0x00
1598 && convrate <= 0x07) {
1602 * The chip_id register of the MAX6680 and MAX6681 holds the
1603 * revision of the chip. The lowest bit of the config1 register
1604 * is unused and should return zero when read, so should the
1605 * second to last bit of config1 (software reset).
1608 && (config1 & 0x03) == 0x00
1609 && convrate <= 0x07) {
1613 * The chip_id register of the MAX6646/6647/6649 holds the
1614 * revision of the chip. The lowest 6 bits of the config1
1615 * register are unused and should return zero when read.
1618 && (config1 & 0x3f) == 0x00
1619 && convrate <= 0x07) {
1623 * The chip_id of the MAX6654 holds the revision of the chip.
1624 * The lowest 3 bits of the config1 register are unused and
1625 * should return zero when read.
1628 && (config1 & 0x07) == 0x00
1629 && convrate <= 0x07) {
1634 && man_id == 0x5C) { /* Winbond/Nuvoton */
1635 if ((config1 & 0x2A) == 0x00
1636 && (config2 & 0xF8) == 0x00) {
1637 if (chip_id == 0x01 /* W83L771W/G */
1638 && convrate <= 0x09) {
1641 if ((chip_id & 0xFE) == 0x10 /* W83L771AWG/ASG */
1642 && convrate <= 0x08) {
1647 if (address >= 0x48 && address <= 0x4F
1648 && man_id == 0xA1) { /* NXP Semiconductor/Philips */
1650 && (config1 & 0x2A) == 0x00
1651 && (config2 & 0xFE) == 0x00
1652 && convrate <= 0x09) {
1656 if ((address == 0x4C || address == 0x4D)
1657 && man_id == 0x47) { /* GMT */
1658 if (chip_id == 0x01 /* G781 */
1659 && (config1 & 0x3F) == 0x00
1660 && convrate <= 0x08)
1663 if (man_id == 0x55 && chip_id == 0x00 &&
1664 (config1 & 0x1B) == 0x00 && convrate <= 0x09) {
1665 int local_ext, conalert, chen, dfc;
1667 local_ext = i2c_smbus_read_byte_data(client,
1668 TMP451_REG_R_LOCAL_TEMPL);
1669 conalert = i2c_smbus_read_byte_data(client,
1670 TMP451_REG_CONALERT);
1671 chen = i2c_smbus_read_byte_data(client, TMP461_REG_CHEN);
1672 dfc = i2c_smbus_read_byte_data(client, TMP461_REG_DFC);
1674 if ((local_ext & 0x0F) == 0x00 &&
1675 (conalert & 0xf1) == 0x01 &&
1676 (chen & 0xfc) == 0x00 &&
1677 (dfc & 0xfc) == 0x00) {
1678 if (address == 0x4c && !(chen & 0x03))
1680 else if (address >= 0x48 && address <= 0x4f)
1685 if (!name) { /* identification failed */
1686 dev_dbg(&adapter->dev,
1687 "Unsupported chip at 0x%02x (man_id=0x%02X, "
1688 "chip_id=0x%02X)\n", address, man_id, chip_id);
1692 strlcpy(info->type, name, I2C_NAME_SIZE);
1697 static void lm90_restore_conf(void *_data)
1699 struct lm90_data *data = _data;
1700 struct i2c_client *client = data->client;
1702 /* Restore initial configuration */
1703 lm90_write_convrate(data, data->convrate_orig);
1704 i2c_smbus_write_byte_data(client, LM90_REG_W_CONFIG1,
1708 static int lm90_init_client(struct i2c_client *client, struct lm90_data *data)
1710 struct device_node *np = client->dev.of_node;
1711 int config, convrate;
1713 convrate = lm90_read_reg(client, LM90_REG_R_CONVRATE);
1716 data->convrate_orig = convrate;
1719 * Start the conversions.
1721 config = lm90_read_reg(client, LM90_REG_R_CONFIG1);
1724 data->config_orig = config;
1725 data->config = config;
1727 lm90_set_convrate(client, data, 500); /* 500ms; 2Hz conversion rate */
1729 /* Check Temperature Range Select */
1730 if (data->flags & LM90_HAVE_EXTENDED_TEMP) {
1731 if (of_property_read_bool(np, "ti,extended-range-enable"))
1735 data->flags |= LM90_FLAG_ADT7461_EXT;
1739 * Put MAX6680/MAX8881 into extended resolution (bit 0x10,
1740 * 0.125 degree resolution) and range (0x08, extend range
1741 * to -64 degree) mode for the remote temperature sensor.
1743 if (data->kind == max6680)
1747 * Put MAX6654 into extended range (0x20, extend minimum range from
1748 * 0 degrees to -64 degrees). Note that extended resolution is not
1749 * possible on the MAX6654 unless conversion rate is set to 1 Hz or
1750 * slower, which is intentionally not done by default.
1752 if (data->kind == max6654)
1756 * Select external channel 0 for max6695/96
1758 if (data->kind == max6696)
1762 * Interrupt is enabled by default on reset, but it may be disabled
1763 * by bootloader, unmask it.
1768 config &= 0xBF; /* run */
1769 lm90_update_confreg(data, config);
1771 return devm_add_action_or_reset(&client->dev, lm90_restore_conf, data);
1774 static bool lm90_is_tripped(struct i2c_client *client, u16 *status)
1776 struct lm90_data *data = i2c_get_clientdata(client);
1779 st = lm90_read_reg(client, LM90_REG_R_STATUS);
1783 if (data->kind == max6696) {
1784 st2 = lm90_read_reg(client, MAX6696_REG_R_STATUS2);
1789 *status = st | (st2 << 8);
1791 if ((st & 0x7f) == 0 && (st2 & 0xfe) == 0)
1794 if ((st & (LM90_STATUS_LLOW | LM90_STATUS_LHIGH | LM90_STATUS_LTHRM)) ||
1795 (st2 & MAX6696_STATUS2_LOT2))
1796 dev_dbg(&client->dev,
1797 "temp%d out of range, please check!\n", 1);
1798 if ((st & (LM90_STATUS_RLOW | LM90_STATUS_RHIGH | LM90_STATUS_RTHRM)) ||
1799 (st2 & MAX6696_STATUS2_ROT2))
1800 dev_dbg(&client->dev,
1801 "temp%d out of range, please check!\n", 2);
1802 if (st & LM90_STATUS_ROPEN)
1803 dev_dbg(&client->dev,
1804 "temp%d diode open, please check!\n", 2);
1805 if (st2 & (MAX6696_STATUS2_R2LOW | MAX6696_STATUS2_R2HIGH |
1806 MAX6696_STATUS2_R2THRM | MAX6696_STATUS2_R2OT2))
1807 dev_dbg(&client->dev,
1808 "temp%d out of range, please check!\n", 3);
1809 if (st2 & MAX6696_STATUS2_R2OPEN)
1810 dev_dbg(&client->dev,
1811 "temp%d diode open, please check!\n", 3);
1813 if (st & LM90_STATUS_LLOW)
1814 hwmon_notify_event(data->hwmon_dev, hwmon_temp,
1815 hwmon_temp_min_alarm, 0);
1816 if (st & LM90_STATUS_RLOW)
1817 hwmon_notify_event(data->hwmon_dev, hwmon_temp,
1818 hwmon_temp_min_alarm, 1);
1819 if (st2 & MAX6696_STATUS2_R2LOW)
1820 hwmon_notify_event(data->hwmon_dev, hwmon_temp,
1821 hwmon_temp_min_alarm, 2);
1822 if (st & LM90_STATUS_LHIGH)
1823 hwmon_notify_event(data->hwmon_dev, hwmon_temp,
1824 hwmon_temp_max_alarm, 0);
1825 if (st & LM90_STATUS_RHIGH)
1826 hwmon_notify_event(data->hwmon_dev, hwmon_temp,
1827 hwmon_temp_max_alarm, 1);
1828 if (st2 & MAX6696_STATUS2_R2HIGH)
1829 hwmon_notify_event(data->hwmon_dev, hwmon_temp,
1830 hwmon_temp_max_alarm, 2);
1835 static irqreturn_t lm90_irq_thread(int irq, void *dev_id)
1837 struct i2c_client *client = dev_id;
1840 if (lm90_is_tripped(client, &status))
1846 static void lm90_remove_pec(void *dev)
1848 device_remove_file(dev, &dev_attr_pec);
1851 static void lm90_regulator_disable(void *regulator)
1853 regulator_disable(regulator);
1857 static const struct hwmon_ops lm90_ops = {
1858 .is_visible = lm90_is_visible,
1860 .write = lm90_write,
1863 static int lm90_probe(struct i2c_client *client)
1865 struct device *dev = &client->dev;
1866 struct i2c_adapter *adapter = client->adapter;
1867 struct hwmon_channel_info *info;
1868 struct regulator *regulator;
1869 struct device *hwmon_dev;
1870 struct lm90_data *data;
1873 regulator = devm_regulator_get(dev, "vcc");
1874 if (IS_ERR(regulator))
1875 return PTR_ERR(regulator);
1877 err = regulator_enable(regulator);
1879 dev_err(dev, "Failed to enable regulator: %d\n", err);
1883 err = devm_add_action_or_reset(dev, lm90_regulator_disable, regulator);
1887 data = devm_kzalloc(dev, sizeof(struct lm90_data), GFP_KERNEL);
1891 data->client = client;
1892 i2c_set_clientdata(client, data);
1893 mutex_init(&data->update_lock);
1895 /* Set the device type */
1896 if (client->dev.of_node)
1897 data->kind = (enum chips)of_device_get_match_data(&client->dev);
1899 data->kind = i2c_match_id(lm90_id, client)->driver_data;
1900 if (data->kind == adm1032) {
1901 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE))
1902 client->flags &= ~I2C_CLIENT_PEC;
1906 * Different devices have different alarm bits triggering the
1909 data->alert_alarms = lm90_params[data->kind].alert_alarms;
1911 /* Set chip capabilities */
1912 data->flags = lm90_params[data->kind].flags;
1914 data->chip.ops = &lm90_ops;
1915 data->chip.info = data->info;
1917 data->info[0] = HWMON_CHANNEL_INFO(chip,
1918 HWMON_C_REGISTER_TZ | HWMON_C_UPDATE_INTERVAL | HWMON_C_ALARMS);
1919 data->info[1] = &data->temp_info;
1921 info = &data->temp_info;
1922 info->type = hwmon_temp;
1923 info->config = data->channel_config;
1925 data->channel_config[0] = HWMON_T_INPUT | HWMON_T_MIN | HWMON_T_MAX |
1926 HWMON_T_MIN_ALARM | HWMON_T_MAX_ALARM;
1927 data->channel_config[1] = HWMON_T_INPUT | HWMON_T_MIN | HWMON_T_MAX |
1928 HWMON_T_MIN_ALARM | HWMON_T_MAX_ALARM | HWMON_T_FAULT;
1930 if (data->flags & LM90_HAVE_CRIT) {
1931 data->channel_config[0] |= HWMON_T_CRIT | HWMON_T_CRIT_ALARM | HWMON_T_CRIT_HYST;
1932 data->channel_config[1] |= HWMON_T_CRIT | HWMON_T_CRIT_ALARM | HWMON_T_CRIT_HYST;
1935 if (data->flags & LM90_HAVE_OFFSET)
1936 data->channel_config[1] |= HWMON_T_OFFSET;
1938 if (data->flags & LM90_HAVE_EMERGENCY) {
1939 data->channel_config[0] |= HWMON_T_EMERGENCY |
1940 HWMON_T_EMERGENCY_HYST;
1941 data->channel_config[1] |= HWMON_T_EMERGENCY |
1942 HWMON_T_EMERGENCY_HYST;
1945 if (data->flags & LM90_HAVE_EMERGENCY_ALARM) {
1946 data->channel_config[0] |= HWMON_T_EMERGENCY_ALARM;
1947 data->channel_config[1] |= HWMON_T_EMERGENCY_ALARM;
1950 if (data->flags & LM90_HAVE_TEMP3) {
1951 data->channel_config[2] = HWMON_T_INPUT |
1952 HWMON_T_MIN | HWMON_T_MAX |
1953 HWMON_T_CRIT | HWMON_T_CRIT_HYST |
1954 HWMON_T_EMERGENCY | HWMON_T_EMERGENCY_HYST |
1955 HWMON_T_MIN_ALARM | HWMON_T_MAX_ALARM |
1956 HWMON_T_CRIT_ALARM | HWMON_T_EMERGENCY_ALARM |
1960 data->reg_local_ext = lm90_params[data->kind].reg_local_ext;
1962 /* Set maximum conversion rate */
1963 data->max_convrate = lm90_params[data->kind].max_convrate;
1965 /* Initialize the LM90 chip */
1966 err = lm90_init_client(client, data);
1968 dev_err(dev, "Failed to initialize device\n");
1973 * The 'pec' attribute is attached to the i2c device and thus created
1976 if (client->flags & I2C_CLIENT_PEC) {
1977 err = device_create_file(dev, &dev_attr_pec);
1980 err = devm_add_action_or_reset(dev, lm90_remove_pec, dev);
1985 hwmon_dev = devm_hwmon_device_register_with_info(dev, client->name,
1988 if (IS_ERR(hwmon_dev))
1989 return PTR_ERR(hwmon_dev);
1991 data->hwmon_dev = hwmon_dev;
1994 dev_dbg(dev, "IRQ: %d\n", client->irq);
1995 err = devm_request_threaded_irq(dev, client->irq,
1996 NULL, lm90_irq_thread,
1997 IRQF_ONESHOT, "lm90", client);
1999 dev_err(dev, "cannot request IRQ %d\n", client->irq);
2007 static void lm90_alert(struct i2c_client *client, enum i2c_alert_protocol type,
2012 if (type != I2C_PROTOCOL_SMBUS_ALERT)
2015 if (lm90_is_tripped(client, &alarms)) {
2017 * Disable ALERT# output, because these chips don't implement
2018 * SMBus alert correctly; they should only hold the alert line
2021 struct lm90_data *data = i2c_get_clientdata(client);
2023 if ((data->flags & LM90_HAVE_BROKEN_ALERT) &&
2024 (alarms & data->alert_alarms)) {
2025 dev_dbg(&client->dev, "Disabling ALERT#\n");
2026 lm90_update_confreg(data, data->config | 0x80);
2029 dev_dbg(&client->dev, "Everything OK\n");
2033 static int __maybe_unused lm90_suspend(struct device *dev)
2035 struct lm90_data *data = dev_get_drvdata(dev);
2036 struct i2c_client *client = data->client;
2039 disable_irq(client->irq);
2044 static int __maybe_unused lm90_resume(struct device *dev)
2046 struct lm90_data *data = dev_get_drvdata(dev);
2047 struct i2c_client *client = data->client;
2050 enable_irq(client->irq);
2055 static SIMPLE_DEV_PM_OPS(lm90_pm_ops, lm90_suspend, lm90_resume);
2057 static struct i2c_driver lm90_driver = {
2058 .class = I2C_CLASS_HWMON,
2061 .of_match_table = of_match_ptr(lm90_of_match),
2064 .probe_new = lm90_probe,
2065 .alert = lm90_alert,
2066 .id_table = lm90_id,
2067 .detect = lm90_detect,
2068 .address_list = normal_i2c,
2071 module_i2c_driver(lm90_driver);
2073 MODULE_AUTHOR("Jean Delvare <jdelvare@suse.de>");
2074 MODULE_DESCRIPTION("LM90/ADM1032 driver");
2075 MODULE_LICENSE("GPL");