2 * lm90.c - Part of lm_sensors, Linux kernel modules for hardware
4 * Copyright (C) 2003-2010 Jean Delvare <jdelvare@suse.de>
6 * Based on the lm83 driver. The LM90 is a sensor chip made by National
7 * Semiconductor. It reports up to two temperatures (its own plus up to
8 * one external one) with a 0.125 deg resolution (1 deg for local
9 * temperature) and a 3-4 deg accuracy.
11 * This driver also supports the LM89 and LM99, two other sensor chips
12 * made by National Semiconductor. Both have an increased remote
13 * temperature measurement accuracy (1 degree), and the LM99
14 * additionally shifts remote temperatures (measured and limits) by 16
15 * degrees, which allows for higher temperatures measurement.
16 * Note that there is no way to differentiate between both chips.
17 * When device is auto-detected, the driver will assume an LM99.
19 * This driver also supports the LM86, another sensor chip made by
20 * National Semiconductor. It is exactly similar to the LM90 except it
21 * has a higher accuracy.
23 * This driver also supports the ADM1032, a sensor chip made by Analog
24 * Devices. That chip is similar to the LM90, with a few differences
25 * that are not handled by this driver. Among others, it has a higher
26 * accuracy than the LM90, much like the LM86 does.
28 * This driver also supports the MAX6657, MAX6658 and MAX6659 sensor
29 * chips made by Maxim. These chips are similar to the LM86.
30 * Note that there is no easy way to differentiate between the three
31 * variants. We use the device address to detect MAX6659, which will result
32 * in a detection as max6657 if it is on address 0x4c. The extra address
33 * and features of the MAX6659 are only supported if the chip is configured
34 * explicitly as max6659, or if its address is not 0x4c.
35 * These chips lack the remote temperature offset feature.
37 * This driver also supports the MAX6646, MAX6647, MAX6648, MAX6649 and
38 * MAX6692 chips made by Maxim. These are again similar to the LM86,
39 * but they use unsigned temperature values and can report temperatures
40 * from 0 to 145 degrees.
42 * This driver also supports the MAX6680 and MAX6681, two other sensor
43 * chips made by Maxim. These are quite similar to the other Maxim
44 * chips. The MAX6680 and MAX6681 only differ in the pinout so they can
45 * be treated identically.
47 * This driver also supports the MAX6695 and MAX6696, two other sensor
48 * chips made by Maxim. These are also quite similar to other Maxim
49 * chips, but support three temperature sensors instead of two. MAX6695
50 * and MAX6696 only differ in the pinout so they can be treated identically.
52 * This driver also supports ADT7461 and ADT7461A from Analog Devices as well as
53 * NCT1008 from ON Semiconductor. The chips are supported in both compatibility
54 * and extended mode. They are mostly compatible with LM90 except for a data
55 * format difference for the temperature value registers.
57 * This driver also supports the SA56004 from Philips. This device is
58 * pin-compatible with the LM86, the ED/EDP parts are also address-compatible.
60 * This driver also supports the G781 from GMT. This device is compatible
63 * This driver also supports TMP451 from Texas Instruments. This device is
64 * supported in both compatibility and extended mode. It's mostly compatible
65 * with ADT7461 except for local temperature low byte register and max
68 * Since the LM90 was the first chipset supported by this driver, most
69 * comments will refer to this chipset, but are actually general and
70 * concern all supported chipsets, unless mentioned otherwise.
72 * This program is free software; you can redistribute it and/or modify
73 * it under the terms of the GNU General Public License as published by
74 * the Free Software Foundation; either version 2 of the License, or
75 * (at your option) any later version.
77 * This program is distributed in the hope that it will be useful,
78 * but WITHOUT ANY WARRANTY; without even the implied warranty of
79 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
80 * GNU General Public License for more details.
82 * You should have received a copy of the GNU General Public License
83 * along with this program; if not, write to the Free Software
84 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
87 #include <linux/module.h>
88 #include <linux/init.h>
89 #include <linux/slab.h>
90 #include <linux/jiffies.h>
91 #include <linux/i2c.h>
92 #include <linux/hwmon.h>
93 #include <linux/err.h>
94 #include <linux/mutex.h>
95 #include <linux/sysfs.h>
96 #include <linux/interrupt.h>
97 #include <linux/regulator/consumer.h>
101 * Address is fully defined internally and cannot be changed except for
102 * MAX6659, MAX6680 and MAX6681.
103 * LM86, LM89, LM90, LM99, ADM1032, ADM1032-1, ADT7461, ADT7461A, MAX6649,
104 * MAX6657, MAX6658, NCT1008 and W83L771 have address 0x4c.
105 * ADM1032-2, ADT7461-2, ADT7461A-2, LM89-1, LM99-1, MAX6646, and NCT1008D
107 * MAX6647 has address 0x4e.
108 * MAX6659 can have address 0x4c, 0x4d or 0x4e.
109 * MAX6680 and MAX6681 can have address 0x18, 0x19, 0x1a, 0x29, 0x2a, 0x2b,
110 * 0x4c, 0x4d or 0x4e.
111 * SA56004 can have address 0x48 through 0x4F.
114 static const unsigned short normal_i2c[] = {
115 0x18, 0x19, 0x1a, 0x29, 0x2a, 0x2b, 0x48, 0x49, 0x4a, 0x4b, 0x4c,
116 0x4d, 0x4e, 0x4f, I2C_CLIENT_END };
118 enum chips { lm90, adm1032, lm99, lm86, max6657, max6659, adt7461, max6680,
119 max6646, w83l771, max6696, sa56004, g781, tmp451 };
125 #define LM90_REG_R_MAN_ID 0xFE
126 #define LM90_REG_R_CHIP_ID 0xFF
127 #define LM90_REG_R_CONFIG1 0x03
128 #define LM90_REG_W_CONFIG1 0x09
129 #define LM90_REG_R_CONFIG2 0xBF
130 #define LM90_REG_W_CONFIG2 0xBF
131 #define LM90_REG_R_CONVRATE 0x04
132 #define LM90_REG_W_CONVRATE 0x0A
133 #define LM90_REG_R_STATUS 0x02
134 #define LM90_REG_R_LOCAL_TEMP 0x00
135 #define LM90_REG_R_LOCAL_HIGH 0x05
136 #define LM90_REG_W_LOCAL_HIGH 0x0B
137 #define LM90_REG_R_LOCAL_LOW 0x06
138 #define LM90_REG_W_LOCAL_LOW 0x0C
139 #define LM90_REG_R_LOCAL_CRIT 0x20
140 #define LM90_REG_W_LOCAL_CRIT 0x20
141 #define LM90_REG_R_REMOTE_TEMPH 0x01
142 #define LM90_REG_R_REMOTE_TEMPL 0x10
143 #define LM90_REG_R_REMOTE_OFFSH 0x11
144 #define LM90_REG_W_REMOTE_OFFSH 0x11
145 #define LM90_REG_R_REMOTE_OFFSL 0x12
146 #define LM90_REG_W_REMOTE_OFFSL 0x12
147 #define LM90_REG_R_REMOTE_HIGHH 0x07
148 #define LM90_REG_W_REMOTE_HIGHH 0x0D
149 #define LM90_REG_R_REMOTE_HIGHL 0x13
150 #define LM90_REG_W_REMOTE_HIGHL 0x13
151 #define LM90_REG_R_REMOTE_LOWH 0x08
152 #define LM90_REG_W_REMOTE_LOWH 0x0E
153 #define LM90_REG_R_REMOTE_LOWL 0x14
154 #define LM90_REG_W_REMOTE_LOWL 0x14
155 #define LM90_REG_R_REMOTE_CRIT 0x19
156 #define LM90_REG_W_REMOTE_CRIT 0x19
157 #define LM90_REG_R_TCRIT_HYST 0x21
158 #define LM90_REG_W_TCRIT_HYST 0x21
160 /* MAX6646/6647/6649/6657/6658/6659/6695/6696 registers */
162 #define MAX6657_REG_R_LOCAL_TEMPL 0x11
163 #define MAX6696_REG_R_STATUS2 0x12
164 #define MAX6659_REG_R_REMOTE_EMERG 0x16
165 #define MAX6659_REG_W_REMOTE_EMERG 0x16
166 #define MAX6659_REG_R_LOCAL_EMERG 0x17
167 #define MAX6659_REG_W_LOCAL_EMERG 0x17
169 /* SA56004 registers */
171 #define SA56004_REG_R_LOCAL_TEMPL 0x22
173 #define LM90_MAX_CONVRATE_MS 16000 /* Maximum conversion rate in ms */
175 /* TMP451 registers */
176 #define TMP451_REG_R_LOCAL_TEMPL 0x15
181 #define LM90_FLAG_ADT7461_EXT (1 << 0) /* ADT7461 extended mode */
182 /* Device features */
183 #define LM90_HAVE_OFFSET (1 << 1) /* temperature offset register */
184 #define LM90_HAVE_REM_LIMIT_EXT (1 << 3) /* extended remote limit */
185 #define LM90_HAVE_EMERGENCY (1 << 4) /* 3rd upper (emergency) limit */
186 #define LM90_HAVE_EMERGENCY_ALARM (1 << 5)/* emergency alarm */
187 #define LM90_HAVE_TEMP3 (1 << 6) /* 3rd temperature sensor */
188 #define LM90_HAVE_BROKEN_ALERT (1 << 7) /* Broken alert */
189 #define LM90_PAUSE_FOR_CONFIG (1 << 8) /* Pause conversion for config */
192 #define LM90_STATUS_LTHRM (1 << 0) /* local THERM limit tripped */
193 #define LM90_STATUS_RTHRM (1 << 1) /* remote THERM limit tripped */
194 #define LM90_STATUS_ROPEN (1 << 2) /* remote is an open circuit */
195 #define LM90_STATUS_RLOW (1 << 3) /* remote low temp limit tripped */
196 #define LM90_STATUS_RHIGH (1 << 4) /* remote high temp limit tripped */
197 #define LM90_STATUS_LLOW (1 << 5) /* local low temp limit tripped */
198 #define LM90_STATUS_LHIGH (1 << 6) /* local high temp limit tripped */
199 #define LM90_STATUS_BUSY (1 << 7) /* conversion is ongoing */
201 #define MAX6696_STATUS2_R2THRM (1 << 1) /* remote2 THERM limit tripped */
202 #define MAX6696_STATUS2_R2OPEN (1 << 2) /* remote2 is an open circuit */
203 #define MAX6696_STATUS2_R2LOW (1 << 3) /* remote2 low temp limit tripped */
204 #define MAX6696_STATUS2_R2HIGH (1 << 4) /* remote2 high temp limit tripped */
205 #define MAX6696_STATUS2_ROT2 (1 << 5) /* remote emergency limit tripped */
206 #define MAX6696_STATUS2_R2OT2 (1 << 6) /* remote2 emergency limit tripped */
207 #define MAX6696_STATUS2_LOT2 (1 << 7) /* local emergency limit tripped */
210 * Driver data (common to all clients)
213 static const struct i2c_device_id lm90_id[] = {
214 { "adm1032", adm1032 },
215 { "adt7461", adt7461 },
216 { "adt7461a", adt7461 },
222 { "max6646", max6646 },
223 { "max6647", max6646 },
224 { "max6649", max6646 },
225 { "max6657", max6657 },
226 { "max6658", max6657 },
227 { "max6659", max6659 },
228 { "max6680", max6680 },
229 { "max6681", max6680 },
230 { "max6695", max6696 },
231 { "max6696", max6696 },
232 { "nct1008", adt7461 },
233 { "w83l771", w83l771 },
234 { "sa56004", sa56004 },
235 { "tmp451", tmp451 },
238 MODULE_DEVICE_TABLE(i2c, lm90_id);
241 * chip type specific parameters
244 u32 flags; /* Capabilities */
245 u16 alert_alarms; /* Which alarm bits trigger ALERT# */
246 /* Upper 8 bits for max6695/96 */
247 u8 max_convrate; /* Maximum conversion rate register value */
248 u8 reg_local_ext; /* Extended local temp register (optional) */
251 static const struct lm90_params lm90_params[] = {
253 .flags = LM90_HAVE_OFFSET | LM90_HAVE_REM_LIMIT_EXT
254 | LM90_HAVE_BROKEN_ALERT,
255 .alert_alarms = 0x7c,
259 .flags = LM90_HAVE_OFFSET | LM90_HAVE_REM_LIMIT_EXT
260 | LM90_HAVE_BROKEN_ALERT,
261 .alert_alarms = 0x7c,
265 .flags = LM90_HAVE_OFFSET | LM90_HAVE_REM_LIMIT_EXT
266 | LM90_HAVE_BROKEN_ALERT,
267 .alert_alarms = 0x7c,
271 .flags = LM90_HAVE_OFFSET | LM90_HAVE_REM_LIMIT_EXT,
272 .alert_alarms = 0x7b,
276 .flags = LM90_HAVE_OFFSET | LM90_HAVE_REM_LIMIT_EXT,
277 .alert_alarms = 0x7b,
281 .flags = LM90_HAVE_OFFSET | LM90_HAVE_REM_LIMIT_EXT,
282 .alert_alarms = 0x7b,
286 .alert_alarms = 0x7c,
288 .reg_local_ext = MAX6657_REG_R_LOCAL_TEMPL,
291 .flags = LM90_PAUSE_FOR_CONFIG,
292 .alert_alarms = 0x7c,
294 .reg_local_ext = MAX6657_REG_R_LOCAL_TEMPL,
297 .flags = LM90_HAVE_EMERGENCY,
298 .alert_alarms = 0x7c,
300 .reg_local_ext = MAX6657_REG_R_LOCAL_TEMPL,
303 .flags = LM90_HAVE_OFFSET,
304 .alert_alarms = 0x7c,
308 .flags = LM90_HAVE_EMERGENCY
309 | LM90_HAVE_EMERGENCY_ALARM | LM90_HAVE_TEMP3,
310 .alert_alarms = 0x1c7c,
312 .reg_local_ext = MAX6657_REG_R_LOCAL_TEMPL,
315 .flags = LM90_HAVE_OFFSET | LM90_HAVE_REM_LIMIT_EXT,
316 .alert_alarms = 0x7c,
320 .flags = LM90_HAVE_OFFSET | LM90_HAVE_REM_LIMIT_EXT,
321 .alert_alarms = 0x7b,
323 .reg_local_ext = SA56004_REG_R_LOCAL_TEMPL,
326 .flags = LM90_HAVE_OFFSET | LM90_HAVE_REM_LIMIT_EXT
327 | LM90_HAVE_BROKEN_ALERT,
328 .alert_alarms = 0x7c,
330 .reg_local_ext = TMP451_REG_R_LOCAL_TEMPL,
335 * TEMP8 register index
337 enum lm90_temp8_reg_index {
342 LOCAL_EMERG, /* max6659 and max6695/96 */
343 REMOTE_EMERG, /* max6659 and max6695/96 */
344 REMOTE2_CRIT, /* max6695/96 only */
345 REMOTE2_EMERG, /* max6695/96 only */
350 * TEMP11 register index
352 enum lm90_temp11_reg_index {
356 REMOTE_OFFSET, /* except max6646, max6657/58/59, and max6695/96 */
358 REMOTE2_TEMP, /* max6695/96 only */
359 REMOTE2_LOW, /* max6695/96 only */
360 REMOTE2_HIGH, /* max6695/96 only */
365 * Client data (each client gets its own)
369 struct i2c_client *client;
370 u32 channel_config[4];
371 struct hwmon_channel_info temp_info;
372 const struct hwmon_channel_info *info[3];
373 struct hwmon_chip_info chip;
374 struct mutex update_lock;
375 bool valid; /* true if register values are valid */
376 unsigned long last_updated; /* in jiffies */
380 unsigned int update_interval; /* in milliseconds */
382 u8 config_orig; /* Original configuration register value */
383 u8 convrate_orig; /* Original conversion rate register value */
384 u16 alert_alarms; /* Which alarm bits trigger ALERT# */
385 /* Upper 8 bits for max6695/96 */
386 u8 max_convrate; /* Maximum conversion rate */
387 u8 reg_local_ext; /* local extension register offset */
389 /* registers values */
390 s8 temp8[TEMP8_REG_NUM];
391 s16 temp11[TEMP11_REG_NUM];
393 u16 alarms; /* bitvector (upper 8 bits for max6695/96) */
401 * The ADM1032 supports PEC but not on write byte transactions, so we need
402 * to explicitly ask for a transaction without PEC.
404 static inline s32 adm1032_write_byte(struct i2c_client *client, u8 value)
406 return i2c_smbus_xfer(client->adapter, client->addr,
407 client->flags & ~I2C_CLIENT_PEC,
408 I2C_SMBUS_WRITE, value, I2C_SMBUS_BYTE, NULL);
412 * It is assumed that client->update_lock is held (unless we are in
413 * detection or initialization steps). This matters when PEC is enabled,
414 * because we don't want the address pointer to change between the write
415 * byte and the read byte transactions.
417 static int lm90_read_reg(struct i2c_client *client, u8 reg)
421 if (client->flags & I2C_CLIENT_PEC) {
422 err = adm1032_write_byte(client, reg);
424 err = i2c_smbus_read_byte(client);
426 err = i2c_smbus_read_byte_data(client, reg);
431 static int lm90_read16(struct i2c_client *client, u8 regh, u8 regl)
436 * There is a trick here. We have to read two registers to have the
437 * sensor temperature, but we have to beware a conversion could occur
438 * between the readings. The datasheet says we should either use
439 * the one-shot conversion register, which we don't want to do
440 * (disables hardware monitoring) or monitor the busy bit, which is
441 * impossible (we can't read the values and monitor that bit at the
442 * exact same time). So the solution used here is to read the high
443 * byte once, then the low byte, then the high byte again. If the new
444 * high byte matches the old one, then we have a valid reading. Else
445 * we have to read the low byte again, and now we believe we have a
448 oldh = lm90_read_reg(client, regh);
451 l = lm90_read_reg(client, regl);
454 newh = lm90_read_reg(client, regh);
458 l = lm90_read_reg(client, regl);
462 return (newh << 8) | l;
466 * client->update_lock must be held when calling this function (unless we are
467 * in detection or initialization steps), and while a remote channel other
468 * than channel 0 is selected. Also, calling code must make sure to re-select
469 * external channel 0 before releasing the lock. This is necessary because
470 * various registers have different meanings as a result of selecting a
471 * non-default remote channel.
473 static inline int lm90_select_remote_channel(struct i2c_client *client,
474 struct lm90_data *data,
479 if (data->kind == max6696) {
480 config = lm90_read_reg(client, LM90_REG_R_CONFIG1);
486 i2c_smbus_write_byte_data(client, LM90_REG_W_CONFIG1,
492 static int lm90_write_convrate(struct i2c_client *client,
493 struct lm90_data *data, int val)
496 int config_orig, config_stop;
498 /* Save config and pause conversion */
499 if (data->flags & LM90_PAUSE_FOR_CONFIG) {
500 config_orig = lm90_read_reg(client, LM90_REG_R_CONFIG1);
503 config_stop = config_orig | 0x40;
504 if (config_orig != config_stop) {
505 err = i2c_smbus_write_byte_data(client,
514 err = i2c_smbus_write_byte_data(client, LM90_REG_W_CONVRATE, val);
516 /* Revert change to config */
517 if (data->flags & LM90_PAUSE_FOR_CONFIG && config_orig != config_stop)
518 i2c_smbus_write_byte_data(client, LM90_REG_W_CONFIG1,
525 * Set conversion rate.
526 * client->update_lock must be held when calling this function (unless we are
527 * in detection or initialization steps).
529 static int lm90_set_convrate(struct i2c_client *client, struct lm90_data *data,
530 unsigned int interval)
532 unsigned int update_interval;
535 /* Shift calculations to avoid rounding errors */
538 /* find the nearest update rate */
539 for (i = 0, update_interval = LM90_MAX_CONVRATE_MS << 6;
540 i < data->max_convrate; i++, update_interval >>= 1)
541 if (interval >= update_interval * 3 / 4)
544 err = lm90_write_convrate(client, data, i);
545 data->update_interval = DIV_ROUND_CLOSEST(update_interval, 64);
549 static int lm90_update_limits(struct device *dev)
551 struct lm90_data *data = dev_get_drvdata(dev);
552 struct i2c_client *client = data->client;
555 val = lm90_read_reg(client, LM90_REG_R_LOCAL_CRIT);
558 data->temp8[LOCAL_CRIT] = val;
560 val = lm90_read_reg(client, LM90_REG_R_REMOTE_CRIT);
563 data->temp8[REMOTE_CRIT] = val;
565 val = lm90_read_reg(client, LM90_REG_R_TCRIT_HYST);
568 data->temp_hyst = val;
570 val = lm90_read_reg(client, LM90_REG_R_REMOTE_LOWH);
573 data->temp11[REMOTE_LOW] = val << 8;
575 if (data->flags & LM90_HAVE_REM_LIMIT_EXT) {
576 val = lm90_read_reg(client, LM90_REG_R_REMOTE_LOWL);
579 data->temp11[REMOTE_LOW] |= val;
582 val = lm90_read_reg(client, LM90_REG_R_REMOTE_HIGHH);
585 data->temp11[REMOTE_HIGH] = val << 8;
587 if (data->flags & LM90_HAVE_REM_LIMIT_EXT) {
588 val = lm90_read_reg(client, LM90_REG_R_REMOTE_HIGHL);
591 data->temp11[REMOTE_HIGH] |= val;
594 if (data->flags & LM90_HAVE_OFFSET) {
595 val = lm90_read16(client, LM90_REG_R_REMOTE_OFFSH,
596 LM90_REG_R_REMOTE_OFFSL);
599 data->temp11[REMOTE_OFFSET] = val;
602 if (data->flags & LM90_HAVE_EMERGENCY) {
603 val = lm90_read_reg(client, MAX6659_REG_R_LOCAL_EMERG);
606 data->temp8[LOCAL_EMERG] = val;
608 val = lm90_read_reg(client, MAX6659_REG_R_REMOTE_EMERG);
611 data->temp8[REMOTE_EMERG] = val;
614 if (data->kind == max6696) {
615 val = lm90_select_remote_channel(client, data, 1);
619 val = lm90_read_reg(client, LM90_REG_R_REMOTE_CRIT);
622 data->temp8[REMOTE2_CRIT] = val;
624 val = lm90_read_reg(client, MAX6659_REG_R_REMOTE_EMERG);
627 data->temp8[REMOTE2_EMERG] = val;
629 val = lm90_read_reg(client, LM90_REG_R_REMOTE_LOWH);
632 data->temp11[REMOTE2_LOW] = val << 8;
634 val = lm90_read_reg(client, LM90_REG_R_REMOTE_HIGHH);
637 data->temp11[REMOTE2_HIGH] = val << 8;
639 lm90_select_remote_channel(client, data, 0);
645 static int lm90_update_device(struct device *dev)
647 struct lm90_data *data = dev_get_drvdata(dev);
648 struct i2c_client *client = data->client;
649 unsigned long next_update;
653 val = lm90_update_limits(dev);
658 next_update = data->last_updated +
659 msecs_to_jiffies(data->update_interval);
660 if (time_after(jiffies, next_update) || !data->valid) {
661 dev_dbg(&client->dev, "Updating lm90 data.\n");
665 val = lm90_read_reg(client, LM90_REG_R_LOCAL_LOW);
668 data->temp8[LOCAL_LOW] = val;
670 val = lm90_read_reg(client, LM90_REG_R_LOCAL_HIGH);
673 data->temp8[LOCAL_HIGH] = val;
675 if (data->reg_local_ext) {
676 val = lm90_read16(client, LM90_REG_R_LOCAL_TEMP,
677 data->reg_local_ext);
680 data->temp11[LOCAL_TEMP] = val;
682 val = lm90_read_reg(client, LM90_REG_R_LOCAL_TEMP);
685 data->temp11[LOCAL_TEMP] = val << 8;
687 val = lm90_read16(client, LM90_REG_R_REMOTE_TEMPH,
688 LM90_REG_R_REMOTE_TEMPL);
691 data->temp11[REMOTE_TEMP] = val;
693 val = lm90_read_reg(client, LM90_REG_R_STATUS);
696 data->alarms = val & ~LM90_STATUS_BUSY;
698 if (data->kind == max6696) {
699 val = lm90_select_remote_channel(client, data, 1);
703 val = lm90_read16(client, LM90_REG_R_REMOTE_TEMPH,
704 LM90_REG_R_REMOTE_TEMPL);
706 lm90_select_remote_channel(client, data, 0);
709 data->temp11[REMOTE2_TEMP] = val;
711 lm90_select_remote_channel(client, data, 0);
713 val = lm90_read_reg(client, MAX6696_REG_R_STATUS2);
716 data->alarms |= val << 8;
720 * Re-enable ALERT# output if it was originally enabled and
721 * relevant alarms are all clear
723 if (!(data->config_orig & 0x80) &&
724 !(data->alarms & data->alert_alarms)) {
725 val = lm90_read_reg(client, LM90_REG_R_CONFIG1);
730 dev_dbg(&client->dev, "Re-enabling ALERT#\n");
731 i2c_smbus_write_byte_data(client,
737 data->last_updated = jiffies;
746 * For local temperatures and limits, critical limits and the hysteresis
747 * value, the LM90 uses signed 8-bit values with LSB = 1 degree Celsius.
748 * For remote temperatures and limits, it uses signed 11-bit values with
749 * LSB = 0.125 degree Celsius, left-justified in 16-bit registers. Some
750 * Maxim chips use unsigned values.
753 static inline int temp_from_s8(s8 val)
758 static inline int temp_from_u8(u8 val)
763 static inline int temp_from_s16(s16 val)
765 return val / 32 * 125;
768 static inline int temp_from_u16(u16 val)
770 return val / 32 * 125;
773 static s8 temp_to_s8(long val)
780 return (val - 500) / 1000;
781 return (val + 500) / 1000;
784 static u8 temp_to_u8(long val)
790 return (val + 500) / 1000;
793 static s16 temp_to_s16(long val)
800 return (val - 62) / 125 * 32;
801 return (val + 62) / 125 * 32;
804 static u8 hyst_to_reg(long val)
810 return (val + 500) / 1000;
814 * ADT7461 in compatibility mode is almost identical to LM90 except that
815 * attempts to write values that are outside the range 0 < temp < 127 are
816 * treated as the boundary value.
818 * ADT7461 in "extended mode" operation uses unsigned integers offset by
819 * 64 (e.g., 0 -> -64 degC). The range is restricted to -64..191 degC.
821 static inline int temp_from_u8_adt7461(struct lm90_data *data, u8 val)
823 if (data->flags & LM90_FLAG_ADT7461_EXT)
824 return (val - 64) * 1000;
825 return temp_from_s8(val);
828 static inline int temp_from_u16_adt7461(struct lm90_data *data, u16 val)
830 if (data->flags & LM90_FLAG_ADT7461_EXT)
831 return (val - 0x4000) / 64 * 250;
832 return temp_from_s16(val);
835 static u8 temp_to_u8_adt7461(struct lm90_data *data, long val)
837 if (data->flags & LM90_FLAG_ADT7461_EXT) {
842 return (val + 500 + 64000) / 1000;
848 return (val + 500) / 1000;
851 static u16 temp_to_u16_adt7461(struct lm90_data *data, long val)
853 if (data->flags & LM90_FLAG_ADT7461_EXT) {
858 return (val + 64000 + 125) / 250 * 64;
864 return (val + 125) / 250 * 64;
867 /* pec used for ADM1032 only */
868 static ssize_t show_pec(struct device *dev, struct device_attribute *dummy,
871 struct i2c_client *client = to_i2c_client(dev);
873 return sprintf(buf, "%d\n", !!(client->flags & I2C_CLIENT_PEC));
876 static ssize_t set_pec(struct device *dev, struct device_attribute *dummy,
877 const char *buf, size_t count)
879 struct i2c_client *client = to_i2c_client(dev);
883 err = kstrtol(buf, 10, &val);
889 client->flags &= ~I2C_CLIENT_PEC;
892 client->flags |= I2C_CLIENT_PEC;
901 static DEVICE_ATTR(pec, S_IWUSR | S_IRUGO, show_pec, set_pec);
903 static int lm90_get_temp11(struct lm90_data *data, int index)
905 s16 temp11 = data->temp11[index];
908 if (data->kind == adt7461 || data->kind == tmp451)
909 temp = temp_from_u16_adt7461(data, temp11);
910 else if (data->kind == max6646)
911 temp = temp_from_u16(temp11);
913 temp = temp_from_s16(temp11);
915 /* +16 degrees offset for temp2 for the LM99 */
916 if (data->kind == lm99 && index <= 2)
922 static int lm90_set_temp11(struct lm90_data *data, int index, long val)
928 [REMOTE_LOW] = { LM90_REG_W_REMOTE_LOWH, LM90_REG_W_REMOTE_LOWL },
929 [REMOTE_HIGH] = { LM90_REG_W_REMOTE_HIGHH, LM90_REG_W_REMOTE_HIGHL },
930 [REMOTE_OFFSET] = { LM90_REG_W_REMOTE_OFFSH, LM90_REG_W_REMOTE_OFFSL },
931 [REMOTE2_LOW] = { LM90_REG_W_REMOTE_LOWH, LM90_REG_W_REMOTE_LOWL },
932 [REMOTE2_HIGH] = { LM90_REG_W_REMOTE_HIGHH, LM90_REG_W_REMOTE_HIGHL }
934 struct i2c_client *client = data->client;
935 struct reg *regp = ®[index];
938 /* +16 degrees offset for temp2 for the LM99 */
939 if (data->kind == lm99 && index <= 2)
942 if (data->kind == adt7461 || data->kind == tmp451)
943 data->temp11[index] = temp_to_u16_adt7461(data, val);
944 else if (data->kind == max6646)
945 data->temp11[index] = temp_to_u8(val) << 8;
946 else if (data->flags & LM90_HAVE_REM_LIMIT_EXT)
947 data->temp11[index] = temp_to_s16(val);
949 data->temp11[index] = temp_to_s8(val) << 8;
951 lm90_select_remote_channel(client, data, index >= 3);
952 err = i2c_smbus_write_byte_data(client, regp->high,
953 data->temp11[index] >> 8);
956 if (data->flags & LM90_HAVE_REM_LIMIT_EXT)
957 err = i2c_smbus_write_byte_data(client, regp->low,
958 data->temp11[index] & 0xff);
960 lm90_select_remote_channel(client, data, 0);
964 static int lm90_get_temp8(struct lm90_data *data, int index)
966 s8 temp8 = data->temp8[index];
969 if (data->kind == adt7461 || data->kind == tmp451)
970 temp = temp_from_u8_adt7461(data, temp8);
971 else if (data->kind == max6646)
972 temp = temp_from_u8(temp8);
974 temp = temp_from_s8(temp8);
976 /* +16 degrees offset for temp2 for the LM99 */
977 if (data->kind == lm99 && index == 3)
983 static int lm90_set_temp8(struct lm90_data *data, int index, long val)
985 static const u8 reg[TEMP8_REG_NUM] = {
986 LM90_REG_W_LOCAL_LOW,
987 LM90_REG_W_LOCAL_HIGH,
988 LM90_REG_W_LOCAL_CRIT,
989 LM90_REG_W_REMOTE_CRIT,
990 MAX6659_REG_W_LOCAL_EMERG,
991 MAX6659_REG_W_REMOTE_EMERG,
992 LM90_REG_W_REMOTE_CRIT,
993 MAX6659_REG_W_REMOTE_EMERG,
995 struct i2c_client *client = data->client;
998 /* +16 degrees offset for temp2 for the LM99 */
999 if (data->kind == lm99 && index == 3)
1002 if (data->kind == adt7461 || data->kind == tmp451)
1003 data->temp8[index] = temp_to_u8_adt7461(data, val);
1004 else if (data->kind == max6646)
1005 data->temp8[index] = temp_to_u8(val);
1007 data->temp8[index] = temp_to_s8(val);
1009 lm90_select_remote_channel(client, data, index >= 6);
1010 err = i2c_smbus_write_byte_data(client, reg[index], data->temp8[index]);
1011 lm90_select_remote_channel(client, data, 0);
1016 static int lm90_get_temphyst(struct lm90_data *data, int index)
1020 if (data->kind == adt7461 || data->kind == tmp451)
1021 temp = temp_from_u8_adt7461(data, data->temp8[index]);
1022 else if (data->kind == max6646)
1023 temp = temp_from_u8(data->temp8[index]);
1025 temp = temp_from_s8(data->temp8[index]);
1027 /* +16 degrees offset for temp2 for the LM99 */
1028 if (data->kind == lm99 && index == 3)
1031 return temp - temp_from_s8(data->temp_hyst);
1034 static int lm90_set_temphyst(struct lm90_data *data, long val)
1036 struct i2c_client *client = data->client;
1040 if (data->kind == adt7461 || data->kind == tmp451)
1041 temp = temp_from_u8_adt7461(data, data->temp8[LOCAL_CRIT]);
1042 else if (data->kind == max6646)
1043 temp = temp_from_u8(data->temp8[LOCAL_CRIT]);
1045 temp = temp_from_s8(data->temp8[LOCAL_CRIT]);
1047 data->temp_hyst = hyst_to_reg(temp - val);
1048 err = i2c_smbus_write_byte_data(client, LM90_REG_W_TCRIT_HYST,
1053 static const u8 lm90_temp_index[3] = {
1054 LOCAL_TEMP, REMOTE_TEMP, REMOTE2_TEMP
1057 static const u8 lm90_temp_min_index[3] = {
1058 LOCAL_LOW, REMOTE_LOW, REMOTE2_LOW
1061 static const u8 lm90_temp_max_index[3] = {
1062 LOCAL_HIGH, REMOTE_HIGH, REMOTE2_HIGH
1065 static const u8 lm90_temp_crit_index[3] = {
1066 LOCAL_CRIT, REMOTE_CRIT, REMOTE2_CRIT
1069 static const u8 lm90_temp_emerg_index[3] = {
1070 LOCAL_EMERG, REMOTE_EMERG, REMOTE2_EMERG
1073 static const u8 lm90_min_alarm_bits[3] = { 5, 3, 11 };
1074 static const u8 lm90_max_alarm_bits[3] = { 6, 4, 12 };
1075 static const u8 lm90_crit_alarm_bits[3] = { 0, 1, 9 };
1076 static const u8 lm90_emergency_alarm_bits[3] = { 15, 13, 14 };
1077 static const u8 lm90_fault_bits[3] = { 0, 2, 10 };
1079 static int lm90_temp_read(struct device *dev, u32 attr, int channel, long *val)
1081 struct lm90_data *data = dev_get_drvdata(dev);
1084 mutex_lock(&data->update_lock);
1085 err = lm90_update_device(dev);
1086 mutex_unlock(&data->update_lock);
1091 case hwmon_temp_input:
1092 *val = lm90_get_temp11(data, lm90_temp_index[channel]);
1094 case hwmon_temp_min_alarm:
1095 *val = (data->alarms >> lm90_min_alarm_bits[channel]) & 1;
1097 case hwmon_temp_max_alarm:
1098 *val = (data->alarms >> lm90_max_alarm_bits[channel]) & 1;
1100 case hwmon_temp_crit_alarm:
1101 *val = (data->alarms >> lm90_crit_alarm_bits[channel]) & 1;
1103 case hwmon_temp_emergency_alarm:
1104 *val = (data->alarms >> lm90_emergency_alarm_bits[channel]) & 1;
1106 case hwmon_temp_fault:
1107 *val = (data->alarms >> lm90_fault_bits[channel]) & 1;
1109 case hwmon_temp_min:
1111 *val = lm90_get_temp8(data,
1112 lm90_temp_min_index[channel]);
1114 *val = lm90_get_temp11(data,
1115 lm90_temp_min_index[channel]);
1117 case hwmon_temp_max:
1119 *val = lm90_get_temp8(data,
1120 lm90_temp_max_index[channel]);
1122 *val = lm90_get_temp11(data,
1123 lm90_temp_max_index[channel]);
1125 case hwmon_temp_crit:
1126 *val = lm90_get_temp8(data, lm90_temp_crit_index[channel]);
1128 case hwmon_temp_crit_hyst:
1129 *val = lm90_get_temphyst(data, lm90_temp_crit_index[channel]);
1131 case hwmon_temp_emergency:
1132 *val = lm90_get_temp8(data, lm90_temp_emerg_index[channel]);
1134 case hwmon_temp_emergency_hyst:
1135 *val = lm90_get_temphyst(data, lm90_temp_emerg_index[channel]);
1137 case hwmon_temp_offset:
1138 *val = lm90_get_temp11(data, REMOTE_OFFSET);
1146 static int lm90_temp_write(struct device *dev, u32 attr, int channel, long val)
1148 struct lm90_data *data = dev_get_drvdata(dev);
1151 mutex_lock(&data->update_lock);
1153 err = lm90_update_device(dev);
1158 case hwmon_temp_min:
1160 err = lm90_set_temp8(data,
1161 lm90_temp_min_index[channel],
1164 err = lm90_set_temp11(data,
1165 lm90_temp_min_index[channel],
1168 case hwmon_temp_max:
1170 err = lm90_set_temp8(data,
1171 lm90_temp_max_index[channel],
1174 err = lm90_set_temp11(data,
1175 lm90_temp_max_index[channel],
1178 case hwmon_temp_crit:
1179 err = lm90_set_temp8(data, lm90_temp_crit_index[channel], val);
1181 case hwmon_temp_crit_hyst:
1182 err = lm90_set_temphyst(data, val);
1184 case hwmon_temp_emergency:
1185 err = lm90_set_temp8(data, lm90_temp_emerg_index[channel], val);
1187 case hwmon_temp_offset:
1188 err = lm90_set_temp11(data, REMOTE_OFFSET, val);
1195 mutex_unlock(&data->update_lock);
1200 static umode_t lm90_temp_is_visible(const void *data, u32 attr, int channel)
1203 case hwmon_temp_input:
1204 case hwmon_temp_min_alarm:
1205 case hwmon_temp_max_alarm:
1206 case hwmon_temp_crit_alarm:
1207 case hwmon_temp_emergency_alarm:
1208 case hwmon_temp_emergency_hyst:
1209 case hwmon_temp_fault:
1211 case hwmon_temp_min:
1212 case hwmon_temp_max:
1213 case hwmon_temp_crit:
1214 case hwmon_temp_emergency:
1215 case hwmon_temp_offset:
1216 return S_IRUGO | S_IWUSR;
1217 case hwmon_temp_crit_hyst:
1219 return S_IRUGO | S_IWUSR;
1226 static int lm90_chip_read(struct device *dev, u32 attr, int channel, long *val)
1228 struct lm90_data *data = dev_get_drvdata(dev);
1231 mutex_lock(&data->update_lock);
1232 err = lm90_update_device(dev);
1233 mutex_unlock(&data->update_lock);
1238 case hwmon_chip_update_interval:
1239 *val = data->update_interval;
1241 case hwmon_chip_alarms:
1242 *val = data->alarms;
1251 static int lm90_chip_write(struct device *dev, u32 attr, int channel, long val)
1253 struct lm90_data *data = dev_get_drvdata(dev);
1254 struct i2c_client *client = data->client;
1257 mutex_lock(&data->update_lock);
1259 err = lm90_update_device(dev);
1264 case hwmon_chip_update_interval:
1265 err = lm90_set_convrate(client, data,
1266 clamp_val(val, 0, 100000));
1273 mutex_unlock(&data->update_lock);
1278 static umode_t lm90_chip_is_visible(const void *data, u32 attr, int channel)
1281 case hwmon_chip_update_interval:
1282 return S_IRUGO | S_IWUSR;
1283 case hwmon_chip_alarms:
1290 static int lm90_read(struct device *dev, enum hwmon_sensor_types type,
1291 u32 attr, int channel, long *val)
1295 return lm90_chip_read(dev, attr, channel, val);
1297 return lm90_temp_read(dev, attr, channel, val);
1303 static int lm90_write(struct device *dev, enum hwmon_sensor_types type,
1304 u32 attr, int channel, long val)
1308 return lm90_chip_write(dev, attr, channel, val);
1310 return lm90_temp_write(dev, attr, channel, val);
1316 static umode_t lm90_is_visible(const void *data, enum hwmon_sensor_types type,
1317 u32 attr, int channel)
1321 return lm90_chip_is_visible(data, attr, channel);
1323 return lm90_temp_is_visible(data, attr, channel);
1329 /* Return 0 if detection is successful, -ENODEV otherwise */
1330 static int lm90_detect(struct i2c_client *client,
1331 struct i2c_board_info *info)
1333 struct i2c_adapter *adapter = client->adapter;
1334 int address = client->addr;
1335 const char *name = NULL;
1336 int man_id, chip_id, config1, config2, convrate;
1338 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
1341 /* detection and identification */
1342 man_id = i2c_smbus_read_byte_data(client, LM90_REG_R_MAN_ID);
1343 chip_id = i2c_smbus_read_byte_data(client, LM90_REG_R_CHIP_ID);
1344 config1 = i2c_smbus_read_byte_data(client, LM90_REG_R_CONFIG1);
1345 convrate = i2c_smbus_read_byte_data(client, LM90_REG_R_CONVRATE);
1346 if (man_id < 0 || chip_id < 0 || config1 < 0 || convrate < 0)
1349 if (man_id == 0x01 || man_id == 0x5C || man_id == 0xA1) {
1350 config2 = i2c_smbus_read_byte_data(client, LM90_REG_R_CONFIG2);
1355 if ((address == 0x4C || address == 0x4D)
1356 && man_id == 0x01) { /* National Semiconductor */
1357 if ((config1 & 0x2A) == 0x00
1358 && (config2 & 0xF8) == 0x00
1359 && convrate <= 0x09) {
1361 && (chip_id & 0xF0) == 0x20) { /* LM90 */
1364 if ((chip_id & 0xF0) == 0x30) { /* LM89/LM99 */
1366 dev_info(&adapter->dev,
1367 "Assuming LM99 chip at 0x%02x\n",
1369 dev_info(&adapter->dev,
1370 "If it is an LM89, instantiate it "
1371 "with the new_device sysfs "
1375 && (chip_id & 0xF0) == 0x10) { /* LM86 */
1380 if ((address == 0x4C || address == 0x4D)
1381 && man_id == 0x41) { /* Analog Devices */
1382 if ((chip_id & 0xF0) == 0x40 /* ADM1032 */
1383 && (config1 & 0x3F) == 0x00
1384 && convrate <= 0x0A) {
1387 * The ADM1032 supports PEC, but only if combined
1388 * transactions are not used.
1390 if (i2c_check_functionality(adapter,
1391 I2C_FUNC_SMBUS_BYTE))
1392 info->flags |= I2C_CLIENT_PEC;
1394 if (chip_id == 0x51 /* ADT7461 */
1395 && (config1 & 0x1B) == 0x00
1396 && convrate <= 0x0A) {
1399 if (chip_id == 0x57 /* ADT7461A, NCT1008 */
1400 && (config1 & 0x1B) == 0x00
1401 && convrate <= 0x0A) {
1405 if (man_id == 0x4D) { /* Maxim */
1406 int emerg, emerg2, status2;
1409 * We read MAX6659_REG_R_REMOTE_EMERG twice, and re-read
1410 * LM90_REG_R_MAN_ID in between. If MAX6659_REG_R_REMOTE_EMERG
1411 * exists, both readings will reflect the same value. Otherwise,
1412 * the readings will be different.
1414 emerg = i2c_smbus_read_byte_data(client,
1415 MAX6659_REG_R_REMOTE_EMERG);
1416 man_id = i2c_smbus_read_byte_data(client,
1418 emerg2 = i2c_smbus_read_byte_data(client,
1419 MAX6659_REG_R_REMOTE_EMERG);
1420 status2 = i2c_smbus_read_byte_data(client,
1421 MAX6696_REG_R_STATUS2);
1422 if (emerg < 0 || man_id < 0 || emerg2 < 0 || status2 < 0)
1426 * The MAX6657, MAX6658 and MAX6659 do NOT have a chip_id
1427 * register. Reading from that address will return the last
1428 * read value, which in our case is those of the man_id
1429 * register. Likewise, the config1 register seems to lack a
1430 * low nibble, so the value will be those of the previous
1431 * read, so in our case those of the man_id register.
1432 * MAX6659 has a third set of upper temperature limit registers.
1433 * Those registers also return values on MAX6657 and MAX6658,
1434 * thus the only way to detect MAX6659 is by its address.
1435 * For this reason it will be mis-detected as MAX6657 if its
1438 if (chip_id == man_id
1439 && (address == 0x4C || address == 0x4D || address == 0x4E)
1440 && (config1 & 0x1F) == (man_id & 0x0F)
1441 && convrate <= 0x09) {
1442 if (address == 0x4C)
1448 * Even though MAX6695 and MAX6696 do not have a chip ID
1449 * register, reading it returns 0x01. Bit 4 of the config1
1450 * register is unused and should return zero when read. Bit 0 of
1451 * the status2 register is unused and should return zero when
1454 * MAX6695 and MAX6696 have an additional set of temperature
1455 * limit registers. We can detect those chips by checking if
1456 * one of those registers exists.
1459 && (config1 & 0x10) == 0x00
1460 && (status2 & 0x01) == 0x00
1462 && convrate <= 0x07) {
1466 * The chip_id register of the MAX6680 and MAX6681 holds the
1467 * revision of the chip. The lowest bit of the config1 register
1468 * is unused and should return zero when read, so should the
1469 * second to last bit of config1 (software reset).
1472 && (config1 & 0x03) == 0x00
1473 && convrate <= 0x07) {
1477 * The chip_id register of the MAX6646/6647/6649 holds the
1478 * revision of the chip. The lowest 6 bits of the config1
1479 * register are unused and should return zero when read.
1482 && (config1 & 0x3f) == 0x00
1483 && convrate <= 0x07) {
1488 && man_id == 0x5C) { /* Winbond/Nuvoton */
1489 if ((config1 & 0x2A) == 0x00
1490 && (config2 & 0xF8) == 0x00) {
1491 if (chip_id == 0x01 /* W83L771W/G */
1492 && convrate <= 0x09) {
1495 if ((chip_id & 0xFE) == 0x10 /* W83L771AWG/ASG */
1496 && convrate <= 0x08) {
1501 if (address >= 0x48 && address <= 0x4F
1502 && man_id == 0xA1) { /* NXP Semiconductor/Philips */
1504 && (config1 & 0x2A) == 0x00
1505 && (config2 & 0xFE) == 0x00
1506 && convrate <= 0x09) {
1510 if ((address == 0x4C || address == 0x4D)
1511 && man_id == 0x47) { /* GMT */
1512 if (chip_id == 0x01 /* G781 */
1513 && (config1 & 0x3F) == 0x00
1514 && convrate <= 0x08)
1518 && man_id == 0x55) { /* Texas Instruments */
1521 local_ext = i2c_smbus_read_byte_data(client,
1522 TMP451_REG_R_LOCAL_TEMPL);
1524 if (chip_id == 0x00 /* TMP451 */
1525 && (config1 & 0x1B) == 0x00
1527 && (local_ext & 0x0F) == 0x00)
1531 if (!name) { /* identification failed */
1532 dev_dbg(&adapter->dev,
1533 "Unsupported chip at 0x%02x (man_id=0x%02X, "
1534 "chip_id=0x%02X)\n", address, man_id, chip_id);
1538 strlcpy(info->type, name, I2C_NAME_SIZE);
1543 static void lm90_restore_conf(void *_data)
1545 struct lm90_data *data = _data;
1546 struct i2c_client *client = data->client;
1548 /* Restore initial configuration */
1549 lm90_write_convrate(client, data, data->convrate_orig);
1550 i2c_smbus_write_byte_data(client, LM90_REG_W_CONFIG1,
1554 static int lm90_init_client(struct i2c_client *client, struct lm90_data *data)
1556 int config, convrate;
1558 convrate = lm90_read_reg(client, LM90_REG_R_CONVRATE);
1561 data->convrate_orig = convrate;
1564 * Start the conversions.
1566 config = lm90_read_reg(client, LM90_REG_R_CONFIG1);
1569 data->config_orig = config;
1571 lm90_set_convrate(client, data, 500); /* 500ms; 2Hz conversion rate */
1573 /* Check Temperature Range Select */
1574 if (data->kind == adt7461 || data->kind == tmp451) {
1576 data->flags |= LM90_FLAG_ADT7461_EXT;
1580 * Put MAX6680/MAX8881 into extended resolution (bit 0x10,
1581 * 0.125 degree resolution) and range (0x08, extend range
1582 * to -64 degree) mode for the remote temperature sensor.
1584 if (data->kind == max6680)
1588 * Select external channel 0 for max6695/96
1590 if (data->kind == max6696)
1593 config &= 0xBF; /* run */
1594 if (config != data->config_orig) /* Only write if changed */
1595 i2c_smbus_write_byte_data(client, LM90_REG_W_CONFIG1, config);
1597 return devm_add_action_or_reset(&client->dev, lm90_restore_conf, data);
1600 static bool lm90_is_tripped(struct i2c_client *client, u16 *status)
1602 struct lm90_data *data = i2c_get_clientdata(client);
1605 st = lm90_read_reg(client, LM90_REG_R_STATUS);
1609 if (data->kind == max6696) {
1610 st2 = lm90_read_reg(client, MAX6696_REG_R_STATUS2);
1615 *status = st | (st2 << 8);
1617 if ((st & 0x7f) == 0 && (st2 & 0xfe) == 0)
1620 if ((st & (LM90_STATUS_LLOW | LM90_STATUS_LHIGH | LM90_STATUS_LTHRM)) ||
1621 (st2 & MAX6696_STATUS2_LOT2))
1622 dev_warn(&client->dev,
1623 "temp%d out of range, please check!\n", 1);
1624 if ((st & (LM90_STATUS_RLOW | LM90_STATUS_RHIGH | LM90_STATUS_RTHRM)) ||
1625 (st2 & MAX6696_STATUS2_ROT2))
1626 dev_warn(&client->dev,
1627 "temp%d out of range, please check!\n", 2);
1628 if (st & LM90_STATUS_ROPEN)
1629 dev_warn(&client->dev,
1630 "temp%d diode open, please check!\n", 2);
1631 if (st2 & (MAX6696_STATUS2_R2LOW | MAX6696_STATUS2_R2HIGH |
1632 MAX6696_STATUS2_R2THRM | MAX6696_STATUS2_R2OT2))
1633 dev_warn(&client->dev,
1634 "temp%d out of range, please check!\n", 3);
1635 if (st2 & MAX6696_STATUS2_R2OPEN)
1636 dev_warn(&client->dev,
1637 "temp%d diode open, please check!\n", 3);
1642 static irqreturn_t lm90_irq_thread(int irq, void *dev_id)
1644 struct i2c_client *client = dev_id;
1647 if (lm90_is_tripped(client, &status))
1653 static void lm90_remove_pec(void *dev)
1655 device_remove_file(dev, &dev_attr_pec);
1658 static void lm90_regulator_disable(void *regulator)
1660 regulator_disable(regulator);
1663 static const u32 lm90_chip_config[] = {
1664 HWMON_C_REGISTER_TZ | HWMON_C_UPDATE_INTERVAL | HWMON_C_ALARMS,
1668 static const struct hwmon_channel_info lm90_chip_info = {
1670 .config = lm90_chip_config,
1674 static const struct hwmon_ops lm90_ops = {
1675 .is_visible = lm90_is_visible,
1677 .write = lm90_write,
1680 static int lm90_probe(struct i2c_client *client,
1681 const struct i2c_device_id *id)
1683 struct device *dev = &client->dev;
1684 struct i2c_adapter *adapter = to_i2c_adapter(dev->parent);
1685 struct hwmon_channel_info *info;
1686 struct regulator *regulator;
1687 struct device *hwmon_dev;
1688 struct lm90_data *data;
1691 regulator = devm_regulator_get(dev, "vcc");
1692 if (IS_ERR(regulator))
1693 return PTR_ERR(regulator);
1695 err = regulator_enable(regulator);
1697 dev_err(dev, "Failed to enable regulator: %d\n", err);
1701 err = devm_add_action_or_reset(dev, lm90_regulator_disable, regulator);
1705 data = devm_kzalloc(dev, sizeof(struct lm90_data), GFP_KERNEL);
1709 data->client = client;
1710 i2c_set_clientdata(client, data);
1711 mutex_init(&data->update_lock);
1713 /* Set the device type */
1714 data->kind = id->driver_data;
1715 if (data->kind == adm1032) {
1716 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE))
1717 client->flags &= ~I2C_CLIENT_PEC;
1721 * Different devices have different alarm bits triggering the
1724 data->alert_alarms = lm90_params[data->kind].alert_alarms;
1726 /* Set chip capabilities */
1727 data->flags = lm90_params[data->kind].flags;
1729 data->chip.ops = &lm90_ops;
1730 data->chip.info = data->info;
1732 data->info[0] = &lm90_chip_info;
1733 data->info[1] = &data->temp_info;
1735 info = &data->temp_info;
1736 info->type = hwmon_temp;
1737 info->config = data->channel_config;
1739 data->channel_config[0] = HWMON_T_INPUT | HWMON_T_MIN | HWMON_T_MAX |
1740 HWMON_T_CRIT | HWMON_T_CRIT_HYST | HWMON_T_MIN_ALARM |
1741 HWMON_T_MAX_ALARM | HWMON_T_CRIT_ALARM;
1742 data->channel_config[1] = HWMON_T_INPUT | HWMON_T_MIN | HWMON_T_MAX |
1743 HWMON_T_CRIT | HWMON_T_CRIT_HYST | HWMON_T_MIN_ALARM |
1744 HWMON_T_MAX_ALARM | HWMON_T_CRIT_ALARM | HWMON_T_FAULT;
1746 if (data->flags & LM90_HAVE_OFFSET)
1747 data->channel_config[1] |= HWMON_T_OFFSET;
1749 if (data->flags & LM90_HAVE_EMERGENCY) {
1750 data->channel_config[0] |= HWMON_T_EMERGENCY |
1751 HWMON_T_EMERGENCY_HYST;
1752 data->channel_config[1] |= HWMON_T_EMERGENCY |
1753 HWMON_T_EMERGENCY_HYST;
1756 if (data->flags & LM90_HAVE_EMERGENCY_ALARM) {
1757 data->channel_config[0] |= HWMON_T_EMERGENCY_ALARM;
1758 data->channel_config[1] |= HWMON_T_EMERGENCY_ALARM;
1761 if (data->flags & LM90_HAVE_TEMP3) {
1762 data->channel_config[2] = HWMON_T_INPUT |
1763 HWMON_T_MIN | HWMON_T_MAX |
1764 HWMON_T_CRIT | HWMON_T_CRIT_HYST |
1765 HWMON_T_EMERGENCY | HWMON_T_EMERGENCY_HYST |
1766 HWMON_T_MIN_ALARM | HWMON_T_MAX_ALARM |
1767 HWMON_T_CRIT_ALARM | HWMON_T_EMERGENCY_ALARM |
1771 data->reg_local_ext = lm90_params[data->kind].reg_local_ext;
1773 /* Set maximum conversion rate */
1774 data->max_convrate = lm90_params[data->kind].max_convrate;
1776 /* Initialize the LM90 chip */
1777 err = lm90_init_client(client, data);
1779 dev_err(dev, "Failed to initialize device\n");
1784 * The 'pec' attribute is attached to the i2c device and thus created
1787 if (client->flags & I2C_CLIENT_PEC) {
1788 err = device_create_file(dev, &dev_attr_pec);
1791 err = devm_add_action_or_reset(dev, lm90_remove_pec, dev);
1796 hwmon_dev = devm_hwmon_device_register_with_info(dev, client->name,
1799 if (IS_ERR(hwmon_dev))
1800 return PTR_ERR(hwmon_dev);
1803 dev_dbg(dev, "IRQ: %d\n", client->irq);
1804 err = devm_request_threaded_irq(dev, client->irq,
1805 NULL, lm90_irq_thread,
1806 IRQF_TRIGGER_LOW | IRQF_ONESHOT,
1809 dev_err(dev, "cannot request IRQ %d\n", client->irq);
1817 static void lm90_alert(struct i2c_client *client, enum i2c_alert_protocol type,
1822 if (type != I2C_PROTOCOL_SMBUS_ALERT)
1825 if (lm90_is_tripped(client, &alarms)) {
1827 * Disable ALERT# output, because these chips don't implement
1828 * SMBus alert correctly; they should only hold the alert line
1831 struct lm90_data *data = i2c_get_clientdata(client);
1833 if ((data->flags & LM90_HAVE_BROKEN_ALERT) &&
1834 (alarms & data->alert_alarms)) {
1837 dev_dbg(&client->dev, "Disabling ALERT#\n");
1838 config = lm90_read_reg(client, LM90_REG_R_CONFIG1);
1840 i2c_smbus_write_byte_data(client,
1845 dev_info(&client->dev, "Everything OK\n");
1849 static struct i2c_driver lm90_driver = {
1850 .class = I2C_CLASS_HWMON,
1854 .probe = lm90_probe,
1855 .alert = lm90_alert,
1856 .id_table = lm90_id,
1857 .detect = lm90_detect,
1858 .address_list = normal_i2c,
1861 module_i2c_driver(lm90_driver);
1863 MODULE_AUTHOR("Jean Delvare <jdelvare@suse.de>");
1864 MODULE_DESCRIPTION("LM90/ADM1032 driver");
1865 MODULE_LICENSE("GPL");