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 */
204 #define MAX6696_STATUS2_R2THRM (1 << 1) /* remote2 THERM limit tripped */
205 #define MAX6696_STATUS2_R2OPEN (1 << 2) /* remote2 is an open circuit */
206 #define MAX6696_STATUS2_R2LOW (1 << 3) /* remote2 low temp limit tripped */
207 #define MAX6696_STATUS2_R2HIGH (1 << 4) /* remote2 high temp limit tripped */
208 #define MAX6696_STATUS2_ROT2 (1 << 5) /* remote emergency limit tripped */
209 #define MAX6696_STATUS2_R2OT2 (1 << 6) /* remote2 emergency limit tripped */
210 #define MAX6696_STATUS2_LOT2 (1 << 7) /* local emergency limit tripped */
213 * Driver data (common to all clients)
216 static const struct i2c_device_id lm90_id[] = {
217 { "adm1032", adm1032 },
218 { "adt7461", adt7461 },
219 { "adt7461a", adt7461 },
225 { "max6646", max6646 },
226 { "max6647", max6646 },
227 { "max6649", max6646 },
228 { "max6654", max6654 },
229 { "max6657", max6657 },
230 { "max6658", max6657 },
231 { "max6659", max6659 },
232 { "max6680", max6680 },
233 { "max6681", max6680 },
234 { "max6695", max6696 },
235 { "max6696", max6696 },
236 { "nct1008", adt7461 },
237 { "w83l771", w83l771 },
238 { "sa56004", sa56004 },
239 { "tmp451", tmp451 },
240 { "tmp461", tmp461 },
243 MODULE_DEVICE_TABLE(i2c, lm90_id);
245 static const struct of_device_id __maybe_unused lm90_of_match[] = {
247 .compatible = "adi,adm1032",
248 .data = (void *)adm1032
251 .compatible = "adi,adt7461",
252 .data = (void *)adt7461
255 .compatible = "adi,adt7461a",
256 .data = (void *)adt7461
259 .compatible = "gmt,g781",
263 .compatible = "national,lm90",
267 .compatible = "national,lm86",
271 .compatible = "national,lm89",
275 .compatible = "national,lm99",
279 .compatible = "dallas,max6646",
280 .data = (void *)max6646
283 .compatible = "dallas,max6647",
284 .data = (void *)max6646
287 .compatible = "dallas,max6649",
288 .data = (void *)max6646
291 .compatible = "dallas,max6654",
292 .data = (void *)max6654
295 .compatible = "dallas,max6657",
296 .data = (void *)max6657
299 .compatible = "dallas,max6658",
300 .data = (void *)max6657
303 .compatible = "dallas,max6659",
304 .data = (void *)max6659
307 .compatible = "dallas,max6680",
308 .data = (void *)max6680
311 .compatible = "dallas,max6681",
312 .data = (void *)max6680
315 .compatible = "dallas,max6695",
316 .data = (void *)max6696
319 .compatible = "dallas,max6696",
320 .data = (void *)max6696
323 .compatible = "onnn,nct1008",
324 .data = (void *)adt7461
327 .compatible = "winbond,w83l771",
328 .data = (void *)w83l771
331 .compatible = "nxp,sa56004",
332 .data = (void *)sa56004
335 .compatible = "ti,tmp451",
336 .data = (void *)tmp451
339 .compatible = "ti,tmp461",
340 .data = (void *)tmp461
344 MODULE_DEVICE_TABLE(of, lm90_of_match);
347 * chip type specific parameters
350 u32 flags; /* Capabilities */
351 u16 alert_alarms; /* Which alarm bits trigger ALERT# */
352 /* Upper 8 bits for max6695/96 */
353 u8 max_convrate; /* Maximum conversion rate register value */
354 u8 reg_local_ext; /* Extended local temp register (optional) */
357 static const struct lm90_params lm90_params[] = {
359 .flags = LM90_HAVE_OFFSET | LM90_HAVE_REM_LIMIT_EXT
360 | LM90_HAVE_BROKEN_ALERT | LM90_HAVE_CRIT,
361 .alert_alarms = 0x7c,
365 .flags = LM90_HAVE_OFFSET | LM90_HAVE_REM_LIMIT_EXT
366 | LM90_HAVE_BROKEN_ALERT | LM90_HAVE_EXTENDED_TEMP
368 .alert_alarms = 0x7c,
372 .flags = LM90_HAVE_OFFSET | LM90_HAVE_REM_LIMIT_EXT
373 | LM90_HAVE_BROKEN_ALERT | LM90_HAVE_CRIT,
374 .alert_alarms = 0x7c,
378 .flags = LM90_HAVE_OFFSET | LM90_HAVE_REM_LIMIT_EXT
380 .alert_alarms = 0x7b,
384 .flags = LM90_HAVE_OFFSET | LM90_HAVE_REM_LIMIT_EXT
386 .alert_alarms = 0x7b,
390 .flags = LM90_HAVE_OFFSET | LM90_HAVE_REM_LIMIT_EXT
392 .alert_alarms = 0x7b,
396 .flags = LM90_HAVE_CRIT,
397 .alert_alarms = 0x7c,
399 .reg_local_ext = MAX6657_REG_R_LOCAL_TEMPL,
402 .alert_alarms = 0x7c,
404 .reg_local_ext = MAX6657_REG_R_LOCAL_TEMPL,
407 .flags = LM90_PAUSE_FOR_CONFIG | LM90_HAVE_CRIT,
408 .alert_alarms = 0x7c,
410 .reg_local_ext = MAX6657_REG_R_LOCAL_TEMPL,
413 .flags = LM90_HAVE_EMERGENCY | LM90_HAVE_CRIT,
414 .alert_alarms = 0x7c,
416 .reg_local_ext = MAX6657_REG_R_LOCAL_TEMPL,
419 .flags = LM90_HAVE_OFFSET | LM90_HAVE_CRIT
420 | LM90_HAVE_CRIT_ALRM_SWP,
421 .alert_alarms = 0x7c,
425 .flags = LM90_HAVE_EMERGENCY
426 | LM90_HAVE_EMERGENCY_ALARM | LM90_HAVE_TEMP3 | LM90_HAVE_CRIT,
427 .alert_alarms = 0x1c7c,
429 .reg_local_ext = MAX6657_REG_R_LOCAL_TEMPL,
432 .flags = LM90_HAVE_OFFSET | LM90_HAVE_REM_LIMIT_EXT | LM90_HAVE_CRIT,
433 .alert_alarms = 0x7c,
437 .flags = LM90_HAVE_OFFSET | LM90_HAVE_REM_LIMIT_EXT | LM90_HAVE_CRIT,
438 .alert_alarms = 0x7b,
440 .reg_local_ext = SA56004_REG_R_LOCAL_TEMPL,
443 .flags = LM90_HAVE_OFFSET | LM90_HAVE_REM_LIMIT_EXT
444 | LM90_HAVE_BROKEN_ALERT | LM90_HAVE_EXTENDED_TEMP | LM90_HAVE_CRIT,
445 .alert_alarms = 0x7c,
447 .reg_local_ext = TMP451_REG_R_LOCAL_TEMPL,
450 .flags = LM90_HAVE_OFFSET | LM90_HAVE_REM_LIMIT_EXT
451 | LM90_HAVE_BROKEN_ALERT | LM90_HAVE_EXTENDED_TEMP | LM90_HAVE_CRIT,
452 .alert_alarms = 0x7c,
454 .reg_local_ext = TMP451_REG_R_LOCAL_TEMPL,
459 * TEMP8 register index
461 enum lm90_temp8_reg_index {
466 LOCAL_EMERG, /* max6659 and max6695/96 */
467 REMOTE_EMERG, /* max6659 and max6695/96 */
468 REMOTE2_CRIT, /* max6695/96 only */
469 REMOTE2_EMERG, /* max6695/96 only */
474 * TEMP11 register index
476 enum lm90_temp11_reg_index {
480 REMOTE_OFFSET, /* except max6646, max6657/58/59, and max6695/96 */
482 REMOTE2_TEMP, /* max6695/96 only */
483 REMOTE2_LOW, /* max6695/96 only */
484 REMOTE2_HIGH, /* max6695/96 only */
489 * Client data (each client gets its own)
493 struct i2c_client *client;
494 struct device *hwmon_dev;
495 u32 channel_config[4];
496 struct hwmon_channel_info temp_info;
497 const struct hwmon_channel_info *info[3];
498 struct hwmon_chip_info chip;
499 struct mutex update_lock;
500 bool valid; /* true if register values are valid */
501 unsigned long last_updated; /* in jiffies */
505 unsigned int update_interval; /* in milliseconds */
507 u8 config; /* Current configuration register value */
508 u8 config_orig; /* Original configuration register value */
509 u8 convrate_orig; /* Original conversion rate register value */
510 u16 alert_alarms; /* Which alarm bits trigger ALERT# */
511 /* Upper 8 bits for max6695/96 */
512 u8 max_convrate; /* Maximum conversion rate */
513 u8 reg_local_ext; /* local extension register offset */
515 /* registers values */
516 s8 temp8[TEMP8_REG_NUM];
517 s16 temp11[TEMP11_REG_NUM];
519 u16 alarms; /* bitvector (upper 8 bits for max6695/96) */
527 * The ADM1032 supports PEC but not on write byte transactions, so we need
528 * to explicitly ask for a transaction without PEC.
530 static inline s32 adm1032_write_byte(struct i2c_client *client, u8 value)
532 return i2c_smbus_xfer(client->adapter, client->addr,
533 client->flags & ~I2C_CLIENT_PEC,
534 I2C_SMBUS_WRITE, value, I2C_SMBUS_BYTE, NULL);
538 * It is assumed that client->update_lock is held (unless we are in
539 * detection or initialization steps). This matters when PEC is enabled,
540 * because we don't want the address pointer to change between the write
541 * byte and the read byte transactions.
543 static int lm90_read_reg(struct i2c_client *client, u8 reg)
547 if (client->flags & I2C_CLIENT_PEC) {
548 err = adm1032_write_byte(client, reg);
550 err = i2c_smbus_read_byte(client);
552 err = i2c_smbus_read_byte_data(client, reg);
557 static int lm90_read16(struct i2c_client *client, u8 regh, u8 regl)
562 * There is a trick here. We have to read two registers to have the
563 * sensor temperature, but we have to beware a conversion could occur
564 * between the readings. The datasheet says we should either use
565 * the one-shot conversion register, which we don't want to do
566 * (disables hardware monitoring) or monitor the busy bit, which is
567 * impossible (we can't read the values and monitor that bit at the
568 * exact same time). So the solution used here is to read the high
569 * byte once, then the low byte, then the high byte again. If the new
570 * high byte matches the old one, then we have a valid reading. Else
571 * we have to read the low byte again, and now we believe we have a
574 oldh = lm90_read_reg(client, regh);
577 l = lm90_read_reg(client, regl);
580 newh = lm90_read_reg(client, regh);
584 l = lm90_read_reg(client, regl);
588 return (newh << 8) | l;
591 static int lm90_update_confreg(struct lm90_data *data, u8 config)
593 if (data->config != config) {
596 err = i2c_smbus_write_byte_data(data->client,
601 data->config = config;
607 * client->update_lock must be held when calling this function (unless we are
608 * in detection or initialization steps), and while a remote channel other
609 * than channel 0 is selected. Also, calling code must make sure to re-select
610 * external channel 0 before releasing the lock. This is necessary because
611 * various registers have different meanings as a result of selecting a
612 * non-default remote channel.
614 static int lm90_select_remote_channel(struct lm90_data *data, int channel)
618 if (data->kind == max6696) {
619 u8 config = data->config & ~0x08;
623 err = lm90_update_confreg(data, config);
628 static int lm90_write_convrate(struct lm90_data *data, int val)
630 u8 config = data->config;
633 /* Save config and pause conversion */
634 if (data->flags & LM90_PAUSE_FOR_CONFIG) {
635 err = lm90_update_confreg(data, config | 0x40);
641 err = i2c_smbus_write_byte_data(data->client, LM90_REG_W_CONVRATE, val);
643 /* Revert change to config */
644 lm90_update_confreg(data, config);
650 * Set conversion rate.
651 * client->update_lock must be held when calling this function (unless we are
652 * in detection or initialization steps).
654 static int lm90_set_convrate(struct i2c_client *client, struct lm90_data *data,
655 unsigned int interval)
657 unsigned int update_interval;
660 /* Shift calculations to avoid rounding errors */
663 /* find the nearest update rate */
664 for (i = 0, update_interval = LM90_MAX_CONVRATE_MS << 6;
665 i < data->max_convrate; i++, update_interval >>= 1)
666 if (interval >= update_interval * 3 / 4)
669 err = lm90_write_convrate(data, i);
670 data->update_interval = DIV_ROUND_CLOSEST(update_interval, 64);
674 static int lm90_update_limits(struct device *dev)
676 struct lm90_data *data = dev_get_drvdata(dev);
677 struct i2c_client *client = data->client;
680 if (data->flags & LM90_HAVE_CRIT) {
681 val = lm90_read_reg(client, LM90_REG_R_LOCAL_CRIT);
684 data->temp8[LOCAL_CRIT] = val;
686 val = lm90_read_reg(client, LM90_REG_R_REMOTE_CRIT);
689 data->temp8[REMOTE_CRIT] = val;
691 val = lm90_read_reg(client, LM90_REG_R_TCRIT_HYST);
694 data->temp_hyst = val;
697 val = lm90_read_reg(client, LM90_REG_R_REMOTE_LOWH);
700 data->temp11[REMOTE_LOW] = val << 8;
702 if (data->flags & LM90_HAVE_REM_LIMIT_EXT) {
703 val = lm90_read_reg(client, LM90_REG_R_REMOTE_LOWL);
706 data->temp11[REMOTE_LOW] |= val;
709 val = lm90_read_reg(client, LM90_REG_R_REMOTE_HIGHH);
712 data->temp11[REMOTE_HIGH] = val << 8;
714 if (data->flags & LM90_HAVE_REM_LIMIT_EXT) {
715 val = lm90_read_reg(client, LM90_REG_R_REMOTE_HIGHL);
718 data->temp11[REMOTE_HIGH] |= val;
721 if (data->flags & LM90_HAVE_OFFSET) {
722 val = lm90_read16(client, LM90_REG_R_REMOTE_OFFSH,
723 LM90_REG_R_REMOTE_OFFSL);
726 data->temp11[REMOTE_OFFSET] = val;
729 if (data->flags & LM90_HAVE_EMERGENCY) {
730 val = lm90_read_reg(client, MAX6659_REG_R_LOCAL_EMERG);
733 data->temp8[LOCAL_EMERG] = val;
735 val = lm90_read_reg(client, MAX6659_REG_R_REMOTE_EMERG);
738 data->temp8[REMOTE_EMERG] = val;
741 if (data->kind == max6696) {
742 val = lm90_select_remote_channel(data, 1);
746 val = lm90_read_reg(client, LM90_REG_R_REMOTE_CRIT);
749 data->temp8[REMOTE2_CRIT] = val;
751 val = lm90_read_reg(client, MAX6659_REG_R_REMOTE_EMERG);
754 data->temp8[REMOTE2_EMERG] = val;
756 val = lm90_read_reg(client, LM90_REG_R_REMOTE_LOWH);
759 data->temp11[REMOTE2_LOW] = val << 8;
761 val = lm90_read_reg(client, LM90_REG_R_REMOTE_HIGHH);
764 data->temp11[REMOTE2_HIGH] = val << 8;
766 lm90_select_remote_channel(data, 0);
772 static int lm90_update_device(struct device *dev)
774 struct lm90_data *data = dev_get_drvdata(dev);
775 struct i2c_client *client = data->client;
776 unsigned long next_update;
780 val = lm90_update_limits(dev);
785 next_update = data->last_updated +
786 msecs_to_jiffies(data->update_interval);
787 if (time_after(jiffies, next_update) || !data->valid) {
788 dev_dbg(&client->dev, "Updating lm90 data.\n");
792 val = lm90_read_reg(client, LM90_REG_R_LOCAL_LOW);
795 data->temp8[LOCAL_LOW] = val;
797 val = lm90_read_reg(client, LM90_REG_R_LOCAL_HIGH);
800 data->temp8[LOCAL_HIGH] = val;
802 if (data->reg_local_ext) {
803 val = lm90_read16(client, LM90_REG_R_LOCAL_TEMP,
804 data->reg_local_ext);
807 data->temp11[LOCAL_TEMP] = val;
809 val = lm90_read_reg(client, LM90_REG_R_LOCAL_TEMP);
812 data->temp11[LOCAL_TEMP] = val << 8;
814 val = lm90_read16(client, LM90_REG_R_REMOTE_TEMPH,
815 LM90_REG_R_REMOTE_TEMPL);
818 data->temp11[REMOTE_TEMP] = val;
820 val = lm90_read_reg(client, LM90_REG_R_STATUS);
823 data->alarms = val; /* lower 8 bit of alarms */
825 if (data->kind == max6696) {
826 val = lm90_select_remote_channel(data, 1);
830 val = lm90_read16(client, LM90_REG_R_REMOTE_TEMPH,
831 LM90_REG_R_REMOTE_TEMPL);
833 lm90_select_remote_channel(data, 0);
836 data->temp11[REMOTE2_TEMP] = val;
838 lm90_select_remote_channel(data, 0);
840 val = lm90_read_reg(client, MAX6696_REG_R_STATUS2);
843 data->alarms |= val << 8;
847 * Re-enable ALERT# output if it was originally enabled and
848 * relevant alarms are all clear
850 if (!(data->config_orig & 0x80) &&
851 !(data->alarms & data->alert_alarms)) {
852 if (data->config & 0x80) {
853 dev_dbg(&client->dev, "Re-enabling ALERT#\n");
854 lm90_update_confreg(data, data->config & ~0x80);
858 data->last_updated = jiffies;
867 * For local temperatures and limits, critical limits and the hysteresis
868 * value, the LM90 uses signed 8-bit values with LSB = 1 degree Celsius.
869 * For remote temperatures and limits, it uses signed 11-bit values with
870 * LSB = 0.125 degree Celsius, left-justified in 16-bit registers. Some
871 * Maxim chips use unsigned values.
874 static inline int temp_from_s8(s8 val)
879 static inline int temp_from_u8(u8 val)
884 static inline int temp_from_s16(s16 val)
886 return val / 32 * 125;
889 static inline int temp_from_u16(u16 val)
891 return val / 32 * 125;
894 static s8 temp_to_s8(long val)
901 return (val - 500) / 1000;
902 return (val + 500) / 1000;
905 static u8 temp_to_u8(long val)
911 return (val + 500) / 1000;
914 static s16 temp_to_s16(long val)
921 return (val - 62) / 125 * 32;
922 return (val + 62) / 125 * 32;
925 static u8 hyst_to_reg(long val)
931 return (val + 500) / 1000;
935 * ADT7461 in compatibility mode is almost identical to LM90 except that
936 * attempts to write values that are outside the range 0 < temp < 127 are
937 * treated as the boundary value.
939 * ADT7461 in "extended mode" operation uses unsigned integers offset by
940 * 64 (e.g., 0 -> -64 degC). The range is restricted to -64..191 degC.
942 static inline int temp_from_u8_adt7461(struct lm90_data *data, u8 val)
944 if (data->flags & LM90_FLAG_ADT7461_EXT)
945 return (val - 64) * 1000;
946 return temp_from_s8(val);
949 static inline int temp_from_u16_adt7461(struct lm90_data *data, u16 val)
951 if (data->flags & LM90_FLAG_ADT7461_EXT)
952 return (val - 0x4000) / 64 * 250;
953 return temp_from_s16(val);
956 static u8 temp_to_u8_adt7461(struct lm90_data *data, long val)
958 if (data->flags & LM90_FLAG_ADT7461_EXT) {
963 return (val + 500 + 64000) / 1000;
969 return (val + 500) / 1000;
972 static u16 temp_to_u16_adt7461(struct lm90_data *data, long val)
974 if (data->flags & LM90_FLAG_ADT7461_EXT) {
979 return (val + 64000 + 125) / 250 * 64;
985 return (val + 125) / 250 * 64;
988 /* pec used for ADM1032 only */
989 static ssize_t pec_show(struct device *dev, struct device_attribute *dummy,
992 struct i2c_client *client = to_i2c_client(dev);
994 return sprintf(buf, "%d\n", !!(client->flags & I2C_CLIENT_PEC));
997 static ssize_t pec_store(struct device *dev, struct device_attribute *dummy,
998 const char *buf, size_t count)
1000 struct i2c_client *client = to_i2c_client(dev);
1004 err = kstrtol(buf, 10, &val);
1010 client->flags &= ~I2C_CLIENT_PEC;
1013 client->flags |= I2C_CLIENT_PEC;
1022 static DEVICE_ATTR_RW(pec);
1024 static int lm90_get_temp11(struct lm90_data *data, int index)
1026 s16 temp11 = data->temp11[index];
1029 if (data->flags & LM90_HAVE_EXTENDED_TEMP)
1030 temp = temp_from_u16_adt7461(data, temp11);
1031 else if (data->kind == max6646)
1032 temp = temp_from_u16(temp11);
1034 temp = temp_from_s16(temp11);
1036 /* +16 degrees offset for temp2 for the LM99 */
1037 if (data->kind == lm99 && index <= 2)
1043 static int lm90_set_temp11(struct lm90_data *data, int index, long val)
1049 [REMOTE_LOW] = { LM90_REG_W_REMOTE_LOWH, LM90_REG_W_REMOTE_LOWL },
1050 [REMOTE_HIGH] = { LM90_REG_W_REMOTE_HIGHH, LM90_REG_W_REMOTE_HIGHL },
1051 [REMOTE_OFFSET] = { LM90_REG_W_REMOTE_OFFSH, LM90_REG_W_REMOTE_OFFSL },
1052 [REMOTE2_LOW] = { LM90_REG_W_REMOTE_LOWH, LM90_REG_W_REMOTE_LOWL },
1053 [REMOTE2_HIGH] = { LM90_REG_W_REMOTE_HIGHH, LM90_REG_W_REMOTE_HIGHL }
1055 struct i2c_client *client = data->client;
1056 struct reg *regp = ®[index];
1059 /* +16 degrees offset for temp2 for the LM99 */
1060 if (data->kind == lm99 && index <= 2) {
1061 /* prevent integer underflow */
1062 val = max(val, -128000l);
1066 if (data->flags & LM90_HAVE_EXTENDED_TEMP)
1067 data->temp11[index] = temp_to_u16_adt7461(data, val);
1068 else if (data->kind == max6646)
1069 data->temp11[index] = temp_to_u8(val) << 8;
1070 else if (data->flags & LM90_HAVE_REM_LIMIT_EXT)
1071 data->temp11[index] = temp_to_s16(val);
1073 data->temp11[index] = temp_to_s8(val) << 8;
1075 lm90_select_remote_channel(data, index >= 3);
1076 err = i2c_smbus_write_byte_data(client, regp->high,
1077 data->temp11[index] >> 8);
1080 if (data->flags & LM90_HAVE_REM_LIMIT_EXT)
1081 err = i2c_smbus_write_byte_data(client, regp->low,
1082 data->temp11[index] & 0xff);
1084 lm90_select_remote_channel(data, 0);
1088 static int lm90_get_temp8(struct lm90_data *data, int index)
1090 s8 temp8 = data->temp8[index];
1093 if (data->flags & LM90_HAVE_EXTENDED_TEMP)
1094 temp = temp_from_u8_adt7461(data, temp8);
1095 else if (data->kind == max6646)
1096 temp = temp_from_u8(temp8);
1098 temp = temp_from_s8(temp8);
1100 /* +16 degrees offset for temp2 for the LM99 */
1101 if (data->kind == lm99 && index == 3)
1107 static int lm90_set_temp8(struct lm90_data *data, int index, long val)
1109 static const u8 reg[TEMP8_REG_NUM] = {
1110 LM90_REG_W_LOCAL_LOW,
1111 LM90_REG_W_LOCAL_HIGH,
1112 LM90_REG_W_LOCAL_CRIT,
1113 LM90_REG_W_REMOTE_CRIT,
1114 MAX6659_REG_W_LOCAL_EMERG,
1115 MAX6659_REG_W_REMOTE_EMERG,
1116 LM90_REG_W_REMOTE_CRIT,
1117 MAX6659_REG_W_REMOTE_EMERG,
1119 struct i2c_client *client = data->client;
1122 /* +16 degrees offset for temp2 for the LM99 */
1123 if (data->kind == lm99 && index == 3) {
1124 /* prevent integer underflow */
1125 val = max(val, -128000l);
1129 if (data->flags & LM90_HAVE_EXTENDED_TEMP)
1130 data->temp8[index] = temp_to_u8_adt7461(data, val);
1131 else if (data->kind == max6646)
1132 data->temp8[index] = temp_to_u8(val);
1134 data->temp8[index] = temp_to_s8(val);
1136 lm90_select_remote_channel(data, index >= 6);
1137 err = i2c_smbus_write_byte_data(client, reg[index], data->temp8[index]);
1138 lm90_select_remote_channel(data, 0);
1143 static int lm90_get_temphyst(struct lm90_data *data, int index)
1147 if (data->flags & LM90_HAVE_EXTENDED_TEMP)
1148 temp = temp_from_u8_adt7461(data, data->temp8[index]);
1149 else if (data->kind == max6646)
1150 temp = temp_from_u8(data->temp8[index]);
1152 temp = temp_from_s8(data->temp8[index]);
1154 /* +16 degrees offset for temp2 for the LM99 */
1155 if (data->kind == lm99 && index == 3)
1158 return temp - temp_from_s8(data->temp_hyst);
1161 static int lm90_set_temphyst(struct lm90_data *data, long val)
1163 struct i2c_client *client = data->client;
1167 if (data->flags & LM90_HAVE_EXTENDED_TEMP)
1168 temp = temp_from_u8_adt7461(data, data->temp8[LOCAL_CRIT]);
1169 else if (data->kind == max6646)
1170 temp = temp_from_u8(data->temp8[LOCAL_CRIT]);
1172 temp = temp_from_s8(data->temp8[LOCAL_CRIT]);
1174 /* prevent integer overflow/underflow */
1175 val = clamp_val(val, -128000l, 255000l);
1177 data->temp_hyst = hyst_to_reg(temp - val);
1178 err = i2c_smbus_write_byte_data(client, LM90_REG_W_TCRIT_HYST,
1183 static const u8 lm90_temp_index[3] = {
1184 LOCAL_TEMP, REMOTE_TEMP, REMOTE2_TEMP
1187 static const u8 lm90_temp_min_index[3] = {
1188 LOCAL_LOW, REMOTE_LOW, REMOTE2_LOW
1191 static const u8 lm90_temp_max_index[3] = {
1192 LOCAL_HIGH, REMOTE_HIGH, REMOTE2_HIGH
1195 static const u8 lm90_temp_crit_index[3] = {
1196 LOCAL_CRIT, REMOTE_CRIT, REMOTE2_CRIT
1199 static const u8 lm90_temp_emerg_index[3] = {
1200 LOCAL_EMERG, REMOTE_EMERG, REMOTE2_EMERG
1203 static const u8 lm90_min_alarm_bits[3] = { 5, 3, 11 };
1204 static const u8 lm90_max_alarm_bits[3] = { 6, 4, 12 };
1205 static const u8 lm90_crit_alarm_bits[3] = { 0, 1, 9 };
1206 static const u8 lm90_crit_alarm_bits_swapped[3] = { 1, 0, 9 };
1207 static const u8 lm90_emergency_alarm_bits[3] = { 15, 13, 14 };
1208 static const u8 lm90_fault_bits[3] = { 0, 2, 10 };
1210 static int lm90_temp_read(struct device *dev, u32 attr, int channel, long *val)
1212 struct lm90_data *data = dev_get_drvdata(dev);
1215 mutex_lock(&data->update_lock);
1216 err = lm90_update_device(dev);
1217 mutex_unlock(&data->update_lock);
1222 case hwmon_temp_input:
1223 *val = lm90_get_temp11(data, lm90_temp_index[channel]);
1225 case hwmon_temp_min_alarm:
1226 *val = (data->alarms >> lm90_min_alarm_bits[channel]) & 1;
1228 case hwmon_temp_max_alarm:
1229 *val = (data->alarms >> lm90_max_alarm_bits[channel]) & 1;
1231 case hwmon_temp_crit_alarm:
1232 if (data->flags & LM90_HAVE_CRIT_ALRM_SWP)
1233 *val = (data->alarms >> lm90_crit_alarm_bits_swapped[channel]) & 1;
1235 *val = (data->alarms >> lm90_crit_alarm_bits[channel]) & 1;
1237 case hwmon_temp_emergency_alarm:
1238 *val = (data->alarms >> lm90_emergency_alarm_bits[channel]) & 1;
1240 case hwmon_temp_fault:
1241 *val = (data->alarms >> lm90_fault_bits[channel]) & 1;
1243 case hwmon_temp_min:
1245 *val = lm90_get_temp8(data,
1246 lm90_temp_min_index[channel]);
1248 *val = lm90_get_temp11(data,
1249 lm90_temp_min_index[channel]);
1251 case hwmon_temp_max:
1253 *val = lm90_get_temp8(data,
1254 lm90_temp_max_index[channel]);
1256 *val = lm90_get_temp11(data,
1257 lm90_temp_max_index[channel]);
1259 case hwmon_temp_crit:
1260 *val = lm90_get_temp8(data, lm90_temp_crit_index[channel]);
1262 case hwmon_temp_crit_hyst:
1263 *val = lm90_get_temphyst(data, lm90_temp_crit_index[channel]);
1265 case hwmon_temp_emergency:
1266 *val = lm90_get_temp8(data, lm90_temp_emerg_index[channel]);
1268 case hwmon_temp_emergency_hyst:
1269 *val = lm90_get_temphyst(data, lm90_temp_emerg_index[channel]);
1271 case hwmon_temp_offset:
1272 *val = lm90_get_temp11(data, REMOTE_OFFSET);
1280 static int lm90_temp_write(struct device *dev, u32 attr, int channel, long val)
1282 struct lm90_data *data = dev_get_drvdata(dev);
1285 mutex_lock(&data->update_lock);
1287 err = lm90_update_device(dev);
1292 case hwmon_temp_min:
1294 err = lm90_set_temp8(data,
1295 lm90_temp_min_index[channel],
1298 err = lm90_set_temp11(data,
1299 lm90_temp_min_index[channel],
1302 case hwmon_temp_max:
1304 err = lm90_set_temp8(data,
1305 lm90_temp_max_index[channel],
1308 err = lm90_set_temp11(data,
1309 lm90_temp_max_index[channel],
1312 case hwmon_temp_crit:
1313 err = lm90_set_temp8(data, lm90_temp_crit_index[channel], val);
1315 case hwmon_temp_crit_hyst:
1316 err = lm90_set_temphyst(data, val);
1318 case hwmon_temp_emergency:
1319 err = lm90_set_temp8(data, lm90_temp_emerg_index[channel], val);
1321 case hwmon_temp_offset:
1322 err = lm90_set_temp11(data, REMOTE_OFFSET, val);
1329 mutex_unlock(&data->update_lock);
1334 static umode_t lm90_temp_is_visible(const void *data, u32 attr, int channel)
1337 case hwmon_temp_input:
1338 case hwmon_temp_min_alarm:
1339 case hwmon_temp_max_alarm:
1340 case hwmon_temp_crit_alarm:
1341 case hwmon_temp_emergency_alarm:
1342 case hwmon_temp_emergency_hyst:
1343 case hwmon_temp_fault:
1345 case hwmon_temp_min:
1346 case hwmon_temp_max:
1347 case hwmon_temp_crit:
1348 case hwmon_temp_emergency:
1349 case hwmon_temp_offset:
1351 case hwmon_temp_crit_hyst:
1360 static int lm90_chip_read(struct device *dev, u32 attr, int channel, long *val)
1362 struct lm90_data *data = dev_get_drvdata(dev);
1365 mutex_lock(&data->update_lock);
1366 err = lm90_update_device(dev);
1367 mutex_unlock(&data->update_lock);
1372 case hwmon_chip_update_interval:
1373 *val = data->update_interval;
1375 case hwmon_chip_alarms:
1376 *val = data->alarms;
1385 static int lm90_chip_write(struct device *dev, u32 attr, int channel, long val)
1387 struct lm90_data *data = dev_get_drvdata(dev);
1388 struct i2c_client *client = data->client;
1391 mutex_lock(&data->update_lock);
1393 err = lm90_update_device(dev);
1398 case hwmon_chip_update_interval:
1399 err = lm90_set_convrate(client, data,
1400 clamp_val(val, 0, 100000));
1407 mutex_unlock(&data->update_lock);
1412 static umode_t lm90_chip_is_visible(const void *data, u32 attr, int channel)
1415 case hwmon_chip_update_interval:
1417 case hwmon_chip_alarms:
1424 static int lm90_read(struct device *dev, enum hwmon_sensor_types type,
1425 u32 attr, int channel, long *val)
1429 return lm90_chip_read(dev, attr, channel, val);
1431 return lm90_temp_read(dev, attr, channel, val);
1437 static int lm90_write(struct device *dev, enum hwmon_sensor_types type,
1438 u32 attr, int channel, long val)
1442 return lm90_chip_write(dev, attr, channel, val);
1444 return lm90_temp_write(dev, attr, channel, val);
1450 static umode_t lm90_is_visible(const void *data, enum hwmon_sensor_types type,
1451 u32 attr, int channel)
1455 return lm90_chip_is_visible(data, attr, channel);
1457 return lm90_temp_is_visible(data, attr, channel);
1463 /* Return 0 if detection is successful, -ENODEV otherwise */
1464 static int lm90_detect(struct i2c_client *client,
1465 struct i2c_board_info *info)
1467 struct i2c_adapter *adapter = client->adapter;
1468 int address = client->addr;
1469 const char *name = NULL;
1470 int man_id, chip_id, config1, config2, convrate;
1472 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
1475 /* detection and identification */
1476 man_id = i2c_smbus_read_byte_data(client, LM90_REG_R_MAN_ID);
1477 chip_id = i2c_smbus_read_byte_data(client, LM90_REG_R_CHIP_ID);
1478 config1 = i2c_smbus_read_byte_data(client, LM90_REG_R_CONFIG1);
1479 convrate = i2c_smbus_read_byte_data(client, LM90_REG_R_CONVRATE);
1480 if (man_id < 0 || chip_id < 0 || config1 < 0 || convrate < 0)
1483 if (man_id == 0x01 || man_id == 0x5C || man_id == 0xA1) {
1484 config2 = i2c_smbus_read_byte_data(client, LM90_REG_R_CONFIG2);
1489 if ((address == 0x4C || address == 0x4D)
1490 && man_id == 0x01) { /* National Semiconductor */
1491 if ((config1 & 0x2A) == 0x00
1492 && (config2 & 0xF8) == 0x00
1493 && convrate <= 0x09) {
1495 && (chip_id & 0xF0) == 0x20) { /* LM90 */
1498 if ((chip_id & 0xF0) == 0x30) { /* LM89/LM99 */
1500 dev_info(&adapter->dev,
1501 "Assuming LM99 chip at 0x%02x\n",
1503 dev_info(&adapter->dev,
1504 "If it is an LM89, instantiate it "
1505 "with the new_device sysfs "
1509 && (chip_id & 0xF0) == 0x10) { /* LM86 */
1514 if ((address == 0x4C || address == 0x4D)
1515 && man_id == 0x41) { /* Analog Devices */
1516 if ((chip_id & 0xF0) == 0x40 /* ADM1032 */
1517 && (config1 & 0x3F) == 0x00
1518 && convrate <= 0x0A) {
1521 * The ADM1032 supports PEC, but only if combined
1522 * transactions are not used.
1524 if (i2c_check_functionality(adapter,
1525 I2C_FUNC_SMBUS_BYTE))
1526 info->flags |= I2C_CLIENT_PEC;
1528 if (chip_id == 0x51 /* ADT7461 */
1529 && (config1 & 0x1B) == 0x00
1530 && convrate <= 0x0A) {
1533 if (chip_id == 0x57 /* ADT7461A, NCT1008 */
1534 && (config1 & 0x1B) == 0x00
1535 && convrate <= 0x0A) {
1539 if (man_id == 0x4D) { /* Maxim */
1540 int emerg, emerg2, status2;
1543 * We read MAX6659_REG_R_REMOTE_EMERG twice, and re-read
1544 * LM90_REG_R_MAN_ID in between. If MAX6659_REG_R_REMOTE_EMERG
1545 * exists, both readings will reflect the same value. Otherwise,
1546 * the readings will be different.
1548 emerg = i2c_smbus_read_byte_data(client,
1549 MAX6659_REG_R_REMOTE_EMERG);
1550 man_id = i2c_smbus_read_byte_data(client,
1552 emerg2 = i2c_smbus_read_byte_data(client,
1553 MAX6659_REG_R_REMOTE_EMERG);
1554 status2 = i2c_smbus_read_byte_data(client,
1555 MAX6696_REG_R_STATUS2);
1556 if (emerg < 0 || man_id < 0 || emerg2 < 0 || status2 < 0)
1560 * The MAX6657, MAX6658 and MAX6659 do NOT have a chip_id
1561 * register. Reading from that address will return the last
1562 * read value, which in our case is those of the man_id
1563 * register. Likewise, the config1 register seems to lack a
1564 * low nibble, so the value will be those of the previous
1565 * read, so in our case those of the man_id register.
1566 * MAX6659 has a third set of upper temperature limit registers.
1567 * Those registers also return values on MAX6657 and MAX6658,
1568 * thus the only way to detect MAX6659 is by its address.
1569 * For this reason it will be mis-detected as MAX6657 if its
1572 if (chip_id == man_id
1573 && (address == 0x4C || address == 0x4D || address == 0x4E)
1574 && (config1 & 0x1F) == (man_id & 0x0F)
1575 && convrate <= 0x09) {
1576 if (address == 0x4C)
1582 * Even though MAX6695 and MAX6696 do not have a chip ID
1583 * register, reading it returns 0x01. Bit 4 of the config1
1584 * register is unused and should return zero when read. Bit 0 of
1585 * the status2 register is unused and should return zero when
1588 * MAX6695 and MAX6696 have an additional set of temperature
1589 * limit registers. We can detect those chips by checking if
1590 * one of those registers exists.
1593 && (config1 & 0x10) == 0x00
1594 && (status2 & 0x01) == 0x00
1596 && convrate <= 0x07) {
1600 * The chip_id register of the MAX6680 and MAX6681 holds the
1601 * revision of the chip. The lowest bit of the config1 register
1602 * is unused and should return zero when read, so should the
1603 * second to last bit of config1 (software reset).
1606 && (config1 & 0x03) == 0x00
1607 && convrate <= 0x07) {
1611 * The chip_id register of the MAX6646/6647/6649 holds the
1612 * revision of the chip. The lowest 6 bits of the config1
1613 * register are unused and should return zero when read.
1616 && (config1 & 0x3f) == 0x00
1617 && convrate <= 0x07) {
1621 * The chip_id of the MAX6654 holds the revision of the chip.
1622 * The lowest 3 bits of the config1 register are unused and
1623 * should return zero when read.
1626 && (config1 & 0x07) == 0x00
1627 && convrate <= 0x07) {
1632 && man_id == 0x5C) { /* Winbond/Nuvoton */
1633 if ((config1 & 0x2A) == 0x00
1634 && (config2 & 0xF8) == 0x00) {
1635 if (chip_id == 0x01 /* W83L771W/G */
1636 && convrate <= 0x09) {
1639 if ((chip_id & 0xFE) == 0x10 /* W83L771AWG/ASG */
1640 && convrate <= 0x08) {
1645 if (address >= 0x48 && address <= 0x4F
1646 && man_id == 0xA1) { /* NXP Semiconductor/Philips */
1648 && (config1 & 0x2A) == 0x00
1649 && (config2 & 0xFE) == 0x00
1650 && convrate <= 0x09) {
1654 if ((address == 0x4C || address == 0x4D)
1655 && man_id == 0x47) { /* GMT */
1656 if (chip_id == 0x01 /* G781 */
1657 && (config1 & 0x3F) == 0x00
1658 && convrate <= 0x08)
1661 if (man_id == 0x55 && chip_id == 0x00 &&
1662 (config1 & 0x1B) == 0x00 && convrate <= 0x09) {
1663 int local_ext, conalert, chen, dfc;
1665 local_ext = i2c_smbus_read_byte_data(client,
1666 TMP451_REG_R_LOCAL_TEMPL);
1667 conalert = i2c_smbus_read_byte_data(client,
1668 TMP451_REG_CONALERT);
1669 chen = i2c_smbus_read_byte_data(client, TMP461_REG_CHEN);
1670 dfc = i2c_smbus_read_byte_data(client, TMP461_REG_DFC);
1672 if ((local_ext & 0x0F) == 0x00 &&
1673 (conalert & 0xf1) == 0x01 &&
1674 (chen & 0xfc) == 0x00 &&
1675 (dfc & 0xfc) == 0x00) {
1676 if (address == 0x4c && !(chen & 0x03))
1678 else if (address >= 0x48 && address <= 0x4f)
1683 if (!name) { /* identification failed */
1684 dev_dbg(&adapter->dev,
1685 "Unsupported chip at 0x%02x (man_id=0x%02X, "
1686 "chip_id=0x%02X)\n", address, man_id, chip_id);
1690 strlcpy(info->type, name, I2C_NAME_SIZE);
1695 static void lm90_restore_conf(void *_data)
1697 struct lm90_data *data = _data;
1698 struct i2c_client *client = data->client;
1700 /* Restore initial configuration */
1701 lm90_write_convrate(data, data->convrate_orig);
1702 i2c_smbus_write_byte_data(client, LM90_REG_W_CONFIG1,
1706 static int lm90_init_client(struct i2c_client *client, struct lm90_data *data)
1708 int config, convrate;
1710 convrate = lm90_read_reg(client, LM90_REG_R_CONVRATE);
1713 data->convrate_orig = convrate;
1716 * Start the conversions.
1718 config = lm90_read_reg(client, LM90_REG_R_CONFIG1);
1721 data->config_orig = config;
1722 data->config = config;
1724 lm90_set_convrate(client, data, 500); /* 500ms; 2Hz conversion rate */
1726 /* Check Temperature Range Select */
1727 if (data->flags & LM90_HAVE_EXTENDED_TEMP) {
1729 data->flags |= LM90_FLAG_ADT7461_EXT;
1733 * Put MAX6680/MAX8881 into extended resolution (bit 0x10,
1734 * 0.125 degree resolution) and range (0x08, extend range
1735 * to -64 degree) mode for the remote temperature sensor.
1737 if (data->kind == max6680)
1741 * Put MAX6654 into extended range (0x20, extend minimum range from
1742 * 0 degrees to -64 degrees). Note that extended resolution is not
1743 * possible on the MAX6654 unless conversion rate is set to 1 Hz or
1744 * slower, which is intentionally not done by default.
1746 if (data->kind == max6654)
1750 * Select external channel 0 for max6695/96
1752 if (data->kind == max6696)
1756 * Interrupt is enabled by default on reset, but it may be disabled
1757 * by bootloader, unmask it.
1762 config &= 0xBF; /* run */
1763 lm90_update_confreg(data, config);
1765 return devm_add_action_or_reset(&client->dev, lm90_restore_conf, data);
1768 static bool lm90_is_tripped(struct i2c_client *client, u16 *status)
1770 struct lm90_data *data = i2c_get_clientdata(client);
1773 st = lm90_read_reg(client, LM90_REG_R_STATUS);
1777 if (data->kind == max6696) {
1778 st2 = lm90_read_reg(client, MAX6696_REG_R_STATUS2);
1783 *status = st | (st2 << 8);
1785 if ((st & 0x7f) == 0 && (st2 & 0xfe) == 0)
1788 if ((st & (LM90_STATUS_LLOW | LM90_STATUS_LHIGH | LM90_STATUS_LTHRM)) ||
1789 (st2 & MAX6696_STATUS2_LOT2))
1790 dev_dbg(&client->dev,
1791 "temp%d out of range, please check!\n", 1);
1792 if ((st & (LM90_STATUS_RLOW | LM90_STATUS_RHIGH | LM90_STATUS_RTHRM)) ||
1793 (st2 & MAX6696_STATUS2_ROT2))
1794 dev_dbg(&client->dev,
1795 "temp%d out of range, please check!\n", 2);
1796 if (st & LM90_STATUS_ROPEN)
1797 dev_dbg(&client->dev,
1798 "temp%d diode open, please check!\n", 2);
1799 if (st2 & (MAX6696_STATUS2_R2LOW | MAX6696_STATUS2_R2HIGH |
1800 MAX6696_STATUS2_R2THRM | MAX6696_STATUS2_R2OT2))
1801 dev_dbg(&client->dev,
1802 "temp%d out of range, please check!\n", 3);
1803 if (st2 & MAX6696_STATUS2_R2OPEN)
1804 dev_dbg(&client->dev,
1805 "temp%d diode open, please check!\n", 3);
1807 if (st & LM90_STATUS_LLOW)
1808 hwmon_notify_event(data->hwmon_dev, hwmon_temp,
1810 if (st & LM90_STATUS_RLOW)
1811 hwmon_notify_event(data->hwmon_dev, hwmon_temp,
1813 if (st2 & MAX6696_STATUS2_R2LOW)
1814 hwmon_notify_event(data->hwmon_dev, hwmon_temp,
1816 if (st & LM90_STATUS_LHIGH)
1817 hwmon_notify_event(data->hwmon_dev, hwmon_temp,
1819 if (st & LM90_STATUS_RHIGH)
1820 hwmon_notify_event(data->hwmon_dev, hwmon_temp,
1822 if (st2 & MAX6696_STATUS2_R2HIGH)
1823 hwmon_notify_event(data->hwmon_dev, hwmon_temp,
1829 static irqreturn_t lm90_irq_thread(int irq, void *dev_id)
1831 struct i2c_client *client = dev_id;
1834 if (lm90_is_tripped(client, &status))
1840 static void lm90_remove_pec(void *dev)
1842 device_remove_file(dev, &dev_attr_pec);
1845 static void lm90_regulator_disable(void *regulator)
1847 regulator_disable(regulator);
1851 static const struct hwmon_ops lm90_ops = {
1852 .is_visible = lm90_is_visible,
1854 .write = lm90_write,
1857 static int lm90_probe(struct i2c_client *client)
1859 struct device *dev = &client->dev;
1860 struct i2c_adapter *adapter = client->adapter;
1861 struct hwmon_channel_info *info;
1862 struct regulator *regulator;
1863 struct device *hwmon_dev;
1864 struct lm90_data *data;
1867 regulator = devm_regulator_get(dev, "vcc");
1868 if (IS_ERR(regulator))
1869 return PTR_ERR(regulator);
1871 err = regulator_enable(regulator);
1873 dev_err(dev, "Failed to enable regulator: %d\n", err);
1877 err = devm_add_action_or_reset(dev, lm90_regulator_disable, regulator);
1881 data = devm_kzalloc(dev, sizeof(struct lm90_data), GFP_KERNEL);
1885 data->client = client;
1886 i2c_set_clientdata(client, data);
1887 mutex_init(&data->update_lock);
1889 /* Set the device type */
1890 if (client->dev.of_node)
1891 data->kind = (enum chips)of_device_get_match_data(&client->dev);
1893 data->kind = i2c_match_id(lm90_id, client)->driver_data;
1894 if (data->kind == adm1032) {
1895 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE))
1896 client->flags &= ~I2C_CLIENT_PEC;
1900 * Different devices have different alarm bits triggering the
1903 data->alert_alarms = lm90_params[data->kind].alert_alarms;
1905 /* Set chip capabilities */
1906 data->flags = lm90_params[data->kind].flags;
1908 data->chip.ops = &lm90_ops;
1909 data->chip.info = data->info;
1911 data->info[0] = HWMON_CHANNEL_INFO(chip,
1912 HWMON_C_REGISTER_TZ | HWMON_C_UPDATE_INTERVAL | HWMON_C_ALARMS);
1913 data->info[1] = &data->temp_info;
1915 info = &data->temp_info;
1916 info->type = hwmon_temp;
1917 info->config = data->channel_config;
1919 data->channel_config[0] = HWMON_T_INPUT | HWMON_T_MIN | HWMON_T_MAX |
1920 HWMON_T_MIN_ALARM | HWMON_T_MAX_ALARM;
1921 data->channel_config[1] = HWMON_T_INPUT | HWMON_T_MIN | HWMON_T_MAX |
1922 HWMON_T_MIN_ALARM | HWMON_T_MAX_ALARM | HWMON_T_FAULT;
1924 if (data->flags & LM90_HAVE_CRIT) {
1925 data->channel_config[0] |= HWMON_T_CRIT | HWMON_T_CRIT_ALARM | HWMON_T_CRIT_HYST;
1926 data->channel_config[1] |= HWMON_T_CRIT | HWMON_T_CRIT_ALARM | HWMON_T_CRIT_HYST;
1929 if (data->flags & LM90_HAVE_OFFSET)
1930 data->channel_config[1] |= HWMON_T_OFFSET;
1932 if (data->flags & LM90_HAVE_EMERGENCY) {
1933 data->channel_config[0] |= HWMON_T_EMERGENCY |
1934 HWMON_T_EMERGENCY_HYST;
1935 data->channel_config[1] |= HWMON_T_EMERGENCY |
1936 HWMON_T_EMERGENCY_HYST;
1939 if (data->flags & LM90_HAVE_EMERGENCY_ALARM) {
1940 data->channel_config[0] |= HWMON_T_EMERGENCY_ALARM;
1941 data->channel_config[1] |= HWMON_T_EMERGENCY_ALARM;
1944 if (data->flags & LM90_HAVE_TEMP3) {
1945 data->channel_config[2] = HWMON_T_INPUT |
1946 HWMON_T_MIN | HWMON_T_MAX |
1947 HWMON_T_CRIT | HWMON_T_CRIT_HYST |
1948 HWMON_T_EMERGENCY | HWMON_T_EMERGENCY_HYST |
1949 HWMON_T_MIN_ALARM | HWMON_T_MAX_ALARM |
1950 HWMON_T_CRIT_ALARM | HWMON_T_EMERGENCY_ALARM |
1954 data->reg_local_ext = lm90_params[data->kind].reg_local_ext;
1956 /* Set maximum conversion rate */
1957 data->max_convrate = lm90_params[data->kind].max_convrate;
1959 /* Initialize the LM90 chip */
1960 err = lm90_init_client(client, data);
1962 dev_err(dev, "Failed to initialize device\n");
1967 * The 'pec' attribute is attached to the i2c device and thus created
1970 if (client->flags & I2C_CLIENT_PEC) {
1971 err = device_create_file(dev, &dev_attr_pec);
1974 err = devm_add_action_or_reset(dev, lm90_remove_pec, dev);
1979 hwmon_dev = devm_hwmon_device_register_with_info(dev, client->name,
1982 if (IS_ERR(hwmon_dev))
1983 return PTR_ERR(hwmon_dev);
1985 data->hwmon_dev = hwmon_dev;
1988 dev_dbg(dev, "IRQ: %d\n", client->irq);
1989 err = devm_request_threaded_irq(dev, client->irq,
1990 NULL, lm90_irq_thread,
1991 IRQF_ONESHOT, "lm90", client);
1993 dev_err(dev, "cannot request IRQ %d\n", client->irq);
2001 static void lm90_alert(struct i2c_client *client, enum i2c_alert_protocol type,
2006 if (type != I2C_PROTOCOL_SMBUS_ALERT)
2009 if (lm90_is_tripped(client, &alarms)) {
2011 * Disable ALERT# output, because these chips don't implement
2012 * SMBus alert correctly; they should only hold the alert line
2015 struct lm90_data *data = i2c_get_clientdata(client);
2017 if ((data->flags & LM90_HAVE_BROKEN_ALERT) &&
2018 (alarms & data->alert_alarms)) {
2019 dev_dbg(&client->dev, "Disabling ALERT#\n");
2020 lm90_update_confreg(data, data->config | 0x80);
2023 dev_dbg(&client->dev, "Everything OK\n");
2027 static int __maybe_unused lm90_suspend(struct device *dev)
2029 struct lm90_data *data = dev_get_drvdata(dev);
2030 struct i2c_client *client = data->client;
2033 disable_irq(client->irq);
2038 static int __maybe_unused lm90_resume(struct device *dev)
2040 struct lm90_data *data = dev_get_drvdata(dev);
2041 struct i2c_client *client = data->client;
2044 enable_irq(client->irq);
2049 static SIMPLE_DEV_PM_OPS(lm90_pm_ops, lm90_suspend, lm90_resume);
2051 static struct i2c_driver lm90_driver = {
2052 .class = I2C_CLASS_HWMON,
2055 .of_match_table = of_match_ptr(lm90_of_match),
2058 .probe_new = lm90_probe,
2059 .alert = lm90_alert,
2060 .id_table = lm90_id,
2061 .detect = lm90_detect,
2062 .address_list = normal_i2c,
2065 module_i2c_driver(lm90_driver);
2067 MODULE_AUTHOR("Jean Delvare <jdelvare@suse.de>");
2068 MODULE_DESCRIPTION("LM90/ADM1032 driver");
2069 MODULE_LICENSE("GPL");