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 */
200 #define MAX6696_STATUS2_R2THRM (1 << 1) /* remote2 THERM limit tripped */
201 #define MAX6696_STATUS2_R2OPEN (1 << 2) /* remote2 is an open circuit */
202 #define MAX6696_STATUS2_R2LOW (1 << 3) /* remote2 low temp limit tripped */
203 #define MAX6696_STATUS2_R2HIGH (1 << 4) /* remote2 high temp limit tripped */
204 #define MAX6696_STATUS2_ROT2 (1 << 5) /* remote emergency limit tripped */
205 #define MAX6696_STATUS2_R2OT2 (1 << 6) /* remote2 emergency limit tripped */
206 #define MAX6696_STATUS2_LOT2 (1 << 7) /* local emergency limit tripped */
209 * Driver data (common to all clients)
212 static const struct i2c_device_id lm90_id[] = {
213 { "adm1032", adm1032 },
214 { "adt7461", adt7461 },
215 { "adt7461a", adt7461 },
221 { "max6646", max6646 },
222 { "max6647", max6646 },
223 { "max6649", max6646 },
224 { "max6657", max6657 },
225 { "max6658", max6657 },
226 { "max6659", max6659 },
227 { "max6680", max6680 },
228 { "max6681", max6680 },
229 { "max6695", max6696 },
230 { "max6696", max6696 },
231 { "nct1008", adt7461 },
232 { "w83l771", w83l771 },
233 { "sa56004", sa56004 },
234 { "tmp451", tmp451 },
237 MODULE_DEVICE_TABLE(i2c, lm90_id);
240 * chip type specific parameters
243 u32 flags; /* Capabilities */
244 u16 alert_alarms; /* Which alarm bits trigger ALERT# */
245 /* Upper 8 bits for max6695/96 */
246 u8 max_convrate; /* Maximum conversion rate register value */
247 u8 reg_local_ext; /* Extended local temp register (optional) */
250 static const struct lm90_params lm90_params[] = {
252 .flags = LM90_HAVE_OFFSET | LM90_HAVE_REM_LIMIT_EXT
253 | LM90_HAVE_BROKEN_ALERT,
254 .alert_alarms = 0x7c,
258 .flags = LM90_HAVE_OFFSET | LM90_HAVE_REM_LIMIT_EXT
259 | LM90_HAVE_BROKEN_ALERT,
260 .alert_alarms = 0x7c,
264 .flags = LM90_HAVE_OFFSET | LM90_HAVE_REM_LIMIT_EXT
265 | LM90_HAVE_BROKEN_ALERT,
266 .alert_alarms = 0x7c,
270 .flags = LM90_HAVE_OFFSET | LM90_HAVE_REM_LIMIT_EXT,
271 .alert_alarms = 0x7b,
275 .flags = LM90_HAVE_OFFSET | LM90_HAVE_REM_LIMIT_EXT,
276 .alert_alarms = 0x7b,
280 .flags = LM90_HAVE_OFFSET | LM90_HAVE_REM_LIMIT_EXT,
281 .alert_alarms = 0x7b,
285 .alert_alarms = 0x7c,
287 .reg_local_ext = MAX6657_REG_R_LOCAL_TEMPL,
290 .flags = LM90_PAUSE_FOR_CONFIG,
291 .alert_alarms = 0x7c,
293 .reg_local_ext = MAX6657_REG_R_LOCAL_TEMPL,
296 .flags = LM90_HAVE_EMERGENCY,
297 .alert_alarms = 0x7c,
299 .reg_local_ext = MAX6657_REG_R_LOCAL_TEMPL,
302 .flags = LM90_HAVE_OFFSET,
303 .alert_alarms = 0x7c,
307 .flags = LM90_HAVE_EMERGENCY
308 | LM90_HAVE_EMERGENCY_ALARM | LM90_HAVE_TEMP3,
309 .alert_alarms = 0x1c7c,
311 .reg_local_ext = MAX6657_REG_R_LOCAL_TEMPL,
314 .flags = LM90_HAVE_OFFSET | LM90_HAVE_REM_LIMIT_EXT,
315 .alert_alarms = 0x7c,
319 .flags = LM90_HAVE_OFFSET | LM90_HAVE_REM_LIMIT_EXT,
320 .alert_alarms = 0x7b,
322 .reg_local_ext = SA56004_REG_R_LOCAL_TEMPL,
325 .flags = LM90_HAVE_OFFSET | LM90_HAVE_REM_LIMIT_EXT
326 | LM90_HAVE_BROKEN_ALERT,
327 .alert_alarms = 0x7c,
329 .reg_local_ext = TMP451_REG_R_LOCAL_TEMPL,
334 * TEMP8 register index
336 enum lm90_temp8_reg_index {
341 LOCAL_EMERG, /* max6659 and max6695/96 */
342 REMOTE_EMERG, /* max6659 and max6695/96 */
343 REMOTE2_CRIT, /* max6695/96 only */
344 REMOTE2_EMERG, /* max6695/96 only */
349 * TEMP11 register index
351 enum lm90_temp11_reg_index {
355 REMOTE_OFFSET, /* except max6646, max6657/58/59, and max6695/96 */
357 REMOTE2_TEMP, /* max6695/96 only */
358 REMOTE2_LOW, /* max6695/96 only */
359 REMOTE2_HIGH, /* max6695/96 only */
364 * Client data (each client gets its own)
368 struct i2c_client *client;
369 u32 channel_config[4];
370 struct hwmon_channel_info temp_info;
371 const struct hwmon_channel_info *info[3];
372 struct hwmon_chip_info chip;
373 struct mutex update_lock;
374 bool valid; /* true if register values are valid */
375 unsigned long last_updated; /* in jiffies */
379 unsigned int update_interval; /* in milliseconds */
381 u8 config_orig; /* Original configuration register value */
382 u8 convrate_orig; /* Original conversion rate register value */
383 u16 alert_alarms; /* Which alarm bits trigger ALERT# */
384 /* Upper 8 bits for max6695/96 */
385 u8 max_convrate; /* Maximum conversion rate */
386 u8 reg_local_ext; /* local extension register offset */
388 /* registers values */
389 s8 temp8[TEMP8_REG_NUM];
390 s16 temp11[TEMP11_REG_NUM];
392 u16 alarms; /* bitvector (upper 8 bits for max6695/96) */
400 * The ADM1032 supports PEC but not on write byte transactions, so we need
401 * to explicitly ask for a transaction without PEC.
403 static inline s32 adm1032_write_byte(struct i2c_client *client, u8 value)
405 return i2c_smbus_xfer(client->adapter, client->addr,
406 client->flags & ~I2C_CLIENT_PEC,
407 I2C_SMBUS_WRITE, value, I2C_SMBUS_BYTE, NULL);
411 * It is assumed that client->update_lock is held (unless we are in
412 * detection or initialization steps). This matters when PEC is enabled,
413 * because we don't want the address pointer to change between the write
414 * byte and the read byte transactions.
416 static int lm90_read_reg(struct i2c_client *client, u8 reg)
420 if (client->flags & I2C_CLIENT_PEC) {
421 err = adm1032_write_byte(client, reg);
423 err = i2c_smbus_read_byte(client);
425 err = i2c_smbus_read_byte_data(client, reg);
430 static int lm90_read16(struct i2c_client *client, u8 regh, u8 regl)
435 * There is a trick here. We have to read two registers to have the
436 * sensor temperature, but we have to beware a conversion could occur
437 * between the readings. The datasheet says we should either use
438 * the one-shot conversion register, which we don't want to do
439 * (disables hardware monitoring) or monitor the busy bit, which is
440 * impossible (we can't read the values and monitor that bit at the
441 * exact same time). So the solution used here is to read the high
442 * byte once, then the low byte, then the high byte again. If the new
443 * high byte matches the old one, then we have a valid reading. Else
444 * we have to read the low byte again, and now we believe we have a
447 oldh = lm90_read_reg(client, regh);
450 l = lm90_read_reg(client, regl);
453 newh = lm90_read_reg(client, regh);
457 l = lm90_read_reg(client, regl);
461 return (newh << 8) | l;
465 * client->update_lock must be held when calling this function (unless we are
466 * in detection or initialization steps), and while a remote channel other
467 * than channel 0 is selected. Also, calling code must make sure to re-select
468 * external channel 0 before releasing the lock. This is necessary because
469 * various registers have different meanings as a result of selecting a
470 * non-default remote channel.
472 static inline int lm90_select_remote_channel(struct i2c_client *client,
473 struct lm90_data *data,
478 if (data->kind == max6696) {
479 config = lm90_read_reg(client, LM90_REG_R_CONFIG1);
485 i2c_smbus_write_byte_data(client, LM90_REG_W_CONFIG1,
491 static int lm90_write_convrate(struct i2c_client *client,
492 struct lm90_data *data, int val)
495 int config_orig, config_stop;
497 /* Save config and pause conversion */
498 if (data->flags & LM90_PAUSE_FOR_CONFIG) {
499 config_orig = lm90_read_reg(client, LM90_REG_R_CONFIG1);
502 config_stop = config_orig | 0x40;
503 if (config_orig != config_stop) {
504 err = i2c_smbus_write_byte_data(client,
513 err = i2c_smbus_write_byte_data(client, LM90_REG_W_CONVRATE, val);
515 /* Revert change to config */
516 if (data->flags & LM90_PAUSE_FOR_CONFIG && config_orig != config_stop)
517 i2c_smbus_write_byte_data(client, LM90_REG_W_CONFIG1,
524 * Set conversion rate.
525 * client->update_lock must be held when calling this function (unless we are
526 * in detection or initialization steps).
528 static int lm90_set_convrate(struct i2c_client *client, struct lm90_data *data,
529 unsigned int interval)
531 unsigned int update_interval;
534 /* Shift calculations to avoid rounding errors */
537 /* find the nearest update rate */
538 for (i = 0, update_interval = LM90_MAX_CONVRATE_MS << 6;
539 i < data->max_convrate; i++, update_interval >>= 1)
540 if (interval >= update_interval * 3 / 4)
543 err = lm90_write_convrate(client, data, i);
544 data->update_interval = DIV_ROUND_CLOSEST(update_interval, 64);
548 static int lm90_update_limits(struct device *dev)
550 struct lm90_data *data = dev_get_drvdata(dev);
551 struct i2c_client *client = data->client;
554 val = lm90_read_reg(client, LM90_REG_R_LOCAL_CRIT);
557 data->temp8[LOCAL_CRIT] = val;
559 val = lm90_read_reg(client, LM90_REG_R_REMOTE_CRIT);
562 data->temp8[REMOTE_CRIT] = val;
564 val = lm90_read_reg(client, LM90_REG_R_TCRIT_HYST);
567 data->temp_hyst = val;
569 val = lm90_read_reg(client, LM90_REG_R_REMOTE_LOWH);
572 data->temp11[REMOTE_LOW] = val << 8;
574 if (data->flags & LM90_HAVE_REM_LIMIT_EXT) {
575 val = lm90_read_reg(client, LM90_REG_R_REMOTE_LOWL);
578 data->temp11[REMOTE_LOW] |= val;
581 val = lm90_read_reg(client, LM90_REG_R_REMOTE_HIGHH);
584 data->temp11[REMOTE_HIGH] = val << 8;
586 if (data->flags & LM90_HAVE_REM_LIMIT_EXT) {
587 val = lm90_read_reg(client, LM90_REG_R_REMOTE_HIGHL);
590 data->temp11[REMOTE_HIGH] |= val;
593 if (data->flags & LM90_HAVE_OFFSET) {
594 val = lm90_read16(client, LM90_REG_R_REMOTE_OFFSH,
595 LM90_REG_R_REMOTE_OFFSL);
598 data->temp11[REMOTE_OFFSET] = val;
601 if (data->flags & LM90_HAVE_EMERGENCY) {
602 val = lm90_read_reg(client, MAX6659_REG_R_LOCAL_EMERG);
605 data->temp8[LOCAL_EMERG] = val;
607 val = lm90_read_reg(client, MAX6659_REG_R_REMOTE_EMERG);
610 data->temp8[REMOTE_EMERG] = val;
613 if (data->kind == max6696) {
614 val = lm90_select_remote_channel(client, data, 1);
618 val = lm90_read_reg(client, LM90_REG_R_REMOTE_CRIT);
621 data->temp8[REMOTE2_CRIT] = val;
623 val = lm90_read_reg(client, MAX6659_REG_R_REMOTE_EMERG);
626 data->temp8[REMOTE2_EMERG] = val;
628 val = lm90_read_reg(client, LM90_REG_R_REMOTE_LOWH);
631 data->temp11[REMOTE2_LOW] = val << 8;
633 val = lm90_read_reg(client, LM90_REG_R_REMOTE_HIGHH);
636 data->temp11[REMOTE2_HIGH] = val << 8;
638 lm90_select_remote_channel(client, data, 0);
644 static int lm90_update_device(struct device *dev)
646 struct lm90_data *data = dev_get_drvdata(dev);
647 struct i2c_client *client = data->client;
648 unsigned long next_update;
652 val = lm90_update_limits(dev);
657 next_update = data->last_updated +
658 msecs_to_jiffies(data->update_interval);
659 if (time_after(jiffies, next_update) || !data->valid) {
660 dev_dbg(&client->dev, "Updating lm90 data.\n");
664 val = lm90_read_reg(client, LM90_REG_R_LOCAL_LOW);
667 data->temp8[LOCAL_LOW] = val;
669 val = lm90_read_reg(client, LM90_REG_R_LOCAL_HIGH);
672 data->temp8[LOCAL_HIGH] = val;
674 if (data->reg_local_ext) {
675 val = lm90_read16(client, LM90_REG_R_LOCAL_TEMP,
676 data->reg_local_ext);
679 data->temp11[LOCAL_TEMP] = val;
681 val = lm90_read_reg(client, LM90_REG_R_LOCAL_TEMP);
684 data->temp11[LOCAL_TEMP] = val << 8;
686 val = lm90_read16(client, LM90_REG_R_REMOTE_TEMPH,
687 LM90_REG_R_REMOTE_TEMPL);
690 data->temp11[REMOTE_TEMP] = val;
692 val = lm90_read_reg(client, LM90_REG_R_STATUS);
695 data->alarms = val; /* lower 8 bit of alarms */
697 if (data->kind == max6696) {
698 val = lm90_select_remote_channel(client, data, 1);
702 val = lm90_read16(client, LM90_REG_R_REMOTE_TEMPH,
703 LM90_REG_R_REMOTE_TEMPL);
705 lm90_select_remote_channel(client, data, 0);
708 data->temp11[REMOTE2_TEMP] = val;
710 lm90_select_remote_channel(client, data, 0);
712 val = lm90_read_reg(client, MAX6696_REG_R_STATUS2);
715 data->alarms |= val << 8;
719 * Re-enable ALERT# output if it was originally enabled and
720 * relevant alarms are all clear
722 if (!(data->config_orig & 0x80) &&
723 !(data->alarms & data->alert_alarms)) {
724 val = lm90_read_reg(client, LM90_REG_R_CONFIG1);
729 dev_dbg(&client->dev, "Re-enabling ALERT#\n");
730 i2c_smbus_write_byte_data(client,
736 data->last_updated = jiffies;
745 * For local temperatures and limits, critical limits and the hysteresis
746 * value, the LM90 uses signed 8-bit values with LSB = 1 degree Celsius.
747 * For remote temperatures and limits, it uses signed 11-bit values with
748 * LSB = 0.125 degree Celsius, left-justified in 16-bit registers. Some
749 * Maxim chips use unsigned values.
752 static inline int temp_from_s8(s8 val)
757 static inline int temp_from_u8(u8 val)
762 static inline int temp_from_s16(s16 val)
764 return val / 32 * 125;
767 static inline int temp_from_u16(u16 val)
769 return val / 32 * 125;
772 static s8 temp_to_s8(long val)
779 return (val - 500) / 1000;
780 return (val + 500) / 1000;
783 static u8 temp_to_u8(long val)
789 return (val + 500) / 1000;
792 static s16 temp_to_s16(long val)
799 return (val - 62) / 125 * 32;
800 return (val + 62) / 125 * 32;
803 static u8 hyst_to_reg(long val)
809 return (val + 500) / 1000;
813 * ADT7461 in compatibility mode is almost identical to LM90 except that
814 * attempts to write values that are outside the range 0 < temp < 127 are
815 * treated as the boundary value.
817 * ADT7461 in "extended mode" operation uses unsigned integers offset by
818 * 64 (e.g., 0 -> -64 degC). The range is restricted to -64..191 degC.
820 static inline int temp_from_u8_adt7461(struct lm90_data *data, u8 val)
822 if (data->flags & LM90_FLAG_ADT7461_EXT)
823 return (val - 64) * 1000;
824 return temp_from_s8(val);
827 static inline int temp_from_u16_adt7461(struct lm90_data *data, u16 val)
829 if (data->flags & LM90_FLAG_ADT7461_EXT)
830 return (val - 0x4000) / 64 * 250;
831 return temp_from_s16(val);
834 static u8 temp_to_u8_adt7461(struct lm90_data *data, long val)
836 if (data->flags & LM90_FLAG_ADT7461_EXT) {
841 return (val + 500 + 64000) / 1000;
847 return (val + 500) / 1000;
850 static u16 temp_to_u16_adt7461(struct lm90_data *data, long val)
852 if (data->flags & LM90_FLAG_ADT7461_EXT) {
857 return (val + 64000 + 125) / 250 * 64;
863 return (val + 125) / 250 * 64;
866 /* pec used for ADM1032 only */
867 static ssize_t show_pec(struct device *dev, struct device_attribute *dummy,
870 struct i2c_client *client = to_i2c_client(dev);
872 return sprintf(buf, "%d\n", !!(client->flags & I2C_CLIENT_PEC));
875 static ssize_t set_pec(struct device *dev, struct device_attribute *dummy,
876 const char *buf, size_t count)
878 struct i2c_client *client = to_i2c_client(dev);
882 err = kstrtol(buf, 10, &val);
888 client->flags &= ~I2C_CLIENT_PEC;
891 client->flags |= I2C_CLIENT_PEC;
900 static DEVICE_ATTR(pec, S_IWUSR | S_IRUGO, show_pec, set_pec);
902 static int lm90_get_temp11(struct lm90_data *data, int index)
904 s16 temp11 = data->temp11[index];
907 if (data->kind == adt7461 || data->kind == tmp451)
908 temp = temp_from_u16_adt7461(data, temp11);
909 else if (data->kind == max6646)
910 temp = temp_from_u16(temp11);
912 temp = temp_from_s16(temp11);
914 /* +16 degrees offset for temp2 for the LM99 */
915 if (data->kind == lm99 && index <= 2)
921 static int lm90_set_temp11(struct lm90_data *data, int index, long val)
927 [REMOTE_LOW] = { LM90_REG_W_REMOTE_LOWH, LM90_REG_W_REMOTE_LOWL },
928 [REMOTE_HIGH] = { LM90_REG_W_REMOTE_HIGHH, LM90_REG_W_REMOTE_HIGHL },
929 [REMOTE_OFFSET] = { LM90_REG_W_REMOTE_OFFSH, LM90_REG_W_REMOTE_OFFSL },
930 [REMOTE2_LOW] = { LM90_REG_W_REMOTE_LOWH, LM90_REG_W_REMOTE_LOWL },
931 [REMOTE2_HIGH] = { LM90_REG_W_REMOTE_HIGHH, LM90_REG_W_REMOTE_HIGHL }
933 struct i2c_client *client = data->client;
934 struct reg *regp = ®[index];
937 /* +16 degrees offset for temp2 for the LM99 */
938 if (data->kind == lm99 && index <= 2)
941 if (data->kind == adt7461 || data->kind == tmp451)
942 data->temp11[index] = temp_to_u16_adt7461(data, val);
943 else if (data->kind == max6646)
944 data->temp11[index] = temp_to_u8(val) << 8;
945 else if (data->flags & LM90_HAVE_REM_LIMIT_EXT)
946 data->temp11[index] = temp_to_s16(val);
948 data->temp11[index] = temp_to_s8(val) << 8;
950 lm90_select_remote_channel(client, data, index >= 3);
951 err = i2c_smbus_write_byte_data(client, regp->high,
952 data->temp11[index] >> 8);
955 if (data->flags & LM90_HAVE_REM_LIMIT_EXT)
956 err = i2c_smbus_write_byte_data(client, regp->low,
957 data->temp11[index] & 0xff);
959 lm90_select_remote_channel(client, data, 0);
963 static int lm90_get_temp8(struct lm90_data *data, int index)
965 s8 temp8 = data->temp8[index];
968 if (data->kind == adt7461 || data->kind == tmp451)
969 temp = temp_from_u8_adt7461(data, temp8);
970 else if (data->kind == max6646)
971 temp = temp_from_u8(temp8);
973 temp = temp_from_s8(temp8);
975 /* +16 degrees offset for temp2 for the LM99 */
976 if (data->kind == lm99 && index == 3)
982 static int lm90_set_temp8(struct lm90_data *data, int index, long val)
984 static const u8 reg[TEMP8_REG_NUM] = {
985 LM90_REG_W_LOCAL_LOW,
986 LM90_REG_W_LOCAL_HIGH,
987 LM90_REG_W_LOCAL_CRIT,
988 LM90_REG_W_REMOTE_CRIT,
989 MAX6659_REG_W_LOCAL_EMERG,
990 MAX6659_REG_W_REMOTE_EMERG,
991 LM90_REG_W_REMOTE_CRIT,
992 MAX6659_REG_W_REMOTE_EMERG,
994 struct i2c_client *client = data->client;
997 /* +16 degrees offset for temp2 for the LM99 */
998 if (data->kind == lm99 && index == 3)
1001 if (data->kind == adt7461 || data->kind == tmp451)
1002 data->temp8[index] = temp_to_u8_adt7461(data, val);
1003 else if (data->kind == max6646)
1004 data->temp8[index] = temp_to_u8(val);
1006 data->temp8[index] = temp_to_s8(val);
1008 lm90_select_remote_channel(client, data, index >= 6);
1009 err = i2c_smbus_write_byte_data(client, reg[index], data->temp8[index]);
1010 lm90_select_remote_channel(client, data, 0);
1015 static int lm90_get_temphyst(struct lm90_data *data, int index)
1019 if (data->kind == adt7461 || data->kind == tmp451)
1020 temp = temp_from_u8_adt7461(data, data->temp8[index]);
1021 else if (data->kind == max6646)
1022 temp = temp_from_u8(data->temp8[index]);
1024 temp = temp_from_s8(data->temp8[index]);
1026 /* +16 degrees offset for temp2 for the LM99 */
1027 if (data->kind == lm99 && index == 3)
1030 return temp - temp_from_s8(data->temp_hyst);
1033 static int lm90_set_temphyst(struct lm90_data *data, long val)
1035 struct i2c_client *client = data->client;
1039 if (data->kind == adt7461 || data->kind == tmp451)
1040 temp = temp_from_u8_adt7461(data, data->temp8[LOCAL_CRIT]);
1041 else if (data->kind == max6646)
1042 temp = temp_from_u8(data->temp8[LOCAL_CRIT]);
1044 temp = temp_from_s8(data->temp8[LOCAL_CRIT]);
1046 data->temp_hyst = hyst_to_reg(temp - val);
1047 err = i2c_smbus_write_byte_data(client, LM90_REG_W_TCRIT_HYST,
1052 static const u8 lm90_temp_index[3] = {
1053 LOCAL_TEMP, REMOTE_TEMP, REMOTE2_TEMP
1056 static const u8 lm90_temp_min_index[3] = {
1057 LOCAL_LOW, REMOTE_LOW, REMOTE2_LOW
1060 static const u8 lm90_temp_max_index[3] = {
1061 LOCAL_HIGH, REMOTE_HIGH, REMOTE2_HIGH
1064 static const u8 lm90_temp_crit_index[3] = {
1065 LOCAL_CRIT, REMOTE_CRIT, REMOTE2_CRIT
1068 static const u8 lm90_temp_emerg_index[3] = {
1069 LOCAL_EMERG, REMOTE_EMERG, REMOTE2_EMERG
1072 static const u8 lm90_min_alarm_bits[3] = { 5, 3, 11 };
1073 static const u8 lm90_max_alarm_bits[3] = { 6, 4, 12 };
1074 static const u8 lm90_crit_alarm_bits[3] = { 0, 1, 9 };
1075 static const u8 lm90_emergency_alarm_bits[3] = { 15, 13, 14 };
1076 static const u8 lm90_fault_bits[3] = { 0, 2, 10 };
1078 static int lm90_temp_read(struct device *dev, u32 attr, int channel, long *val)
1080 struct lm90_data *data = dev_get_drvdata(dev);
1083 mutex_lock(&data->update_lock);
1084 err = lm90_update_device(dev);
1085 mutex_unlock(&data->update_lock);
1090 case hwmon_temp_input:
1091 *val = lm90_get_temp11(data, lm90_temp_index[channel]);
1093 case hwmon_temp_min_alarm:
1094 *val = (data->alarms >> lm90_min_alarm_bits[channel]) & 1;
1096 case hwmon_temp_max_alarm:
1097 *val = (data->alarms >> lm90_max_alarm_bits[channel]) & 1;
1099 case hwmon_temp_crit_alarm:
1100 *val = (data->alarms >> lm90_crit_alarm_bits[channel]) & 1;
1102 case hwmon_temp_emergency_alarm:
1103 *val = (data->alarms >> lm90_emergency_alarm_bits[channel]) & 1;
1105 case hwmon_temp_fault:
1106 *val = (data->alarms >> lm90_fault_bits[channel]) & 1;
1108 case hwmon_temp_min:
1110 *val = lm90_get_temp8(data,
1111 lm90_temp_min_index[channel]);
1113 *val = lm90_get_temp11(data,
1114 lm90_temp_min_index[channel]);
1116 case hwmon_temp_max:
1118 *val = lm90_get_temp8(data,
1119 lm90_temp_max_index[channel]);
1121 *val = lm90_get_temp11(data,
1122 lm90_temp_max_index[channel]);
1124 case hwmon_temp_crit:
1125 *val = lm90_get_temp8(data, lm90_temp_crit_index[channel]);
1127 case hwmon_temp_crit_hyst:
1128 *val = lm90_get_temphyst(data, lm90_temp_crit_index[channel]);
1130 case hwmon_temp_emergency:
1131 *val = lm90_get_temp8(data, lm90_temp_emerg_index[channel]);
1133 case hwmon_temp_emergency_hyst:
1134 *val = lm90_get_temphyst(data, lm90_temp_emerg_index[channel]);
1136 case hwmon_temp_offset:
1137 *val = lm90_get_temp11(data, REMOTE_OFFSET);
1145 static int lm90_temp_write(struct device *dev, u32 attr, int channel, long val)
1147 struct lm90_data *data = dev_get_drvdata(dev);
1150 mutex_lock(&data->update_lock);
1152 err = lm90_update_device(dev);
1157 case hwmon_temp_min:
1159 err = lm90_set_temp8(data,
1160 lm90_temp_min_index[channel],
1163 err = lm90_set_temp11(data,
1164 lm90_temp_min_index[channel],
1167 case hwmon_temp_max:
1169 err = lm90_set_temp8(data,
1170 lm90_temp_max_index[channel],
1173 err = lm90_set_temp11(data,
1174 lm90_temp_max_index[channel],
1177 case hwmon_temp_crit:
1178 err = lm90_set_temp8(data, lm90_temp_crit_index[channel], val);
1180 case hwmon_temp_crit_hyst:
1181 err = lm90_set_temphyst(data, val);
1183 case hwmon_temp_emergency:
1184 err = lm90_set_temp8(data, lm90_temp_emerg_index[channel], val);
1186 case hwmon_temp_offset:
1187 err = lm90_set_temp11(data, REMOTE_OFFSET, val);
1194 mutex_unlock(&data->update_lock);
1199 static umode_t lm90_temp_is_visible(const void *data, u32 attr, int channel)
1202 case hwmon_temp_input:
1203 case hwmon_temp_min_alarm:
1204 case hwmon_temp_max_alarm:
1205 case hwmon_temp_crit_alarm:
1206 case hwmon_temp_emergency_alarm:
1207 case hwmon_temp_emergency_hyst:
1208 case hwmon_temp_fault:
1210 case hwmon_temp_min:
1211 case hwmon_temp_max:
1212 case hwmon_temp_crit:
1213 case hwmon_temp_emergency:
1214 case hwmon_temp_offset:
1215 return S_IRUGO | S_IWUSR;
1216 case hwmon_temp_crit_hyst:
1218 return S_IRUGO | S_IWUSR;
1225 static int lm90_chip_read(struct device *dev, u32 attr, int channel, long *val)
1227 struct lm90_data *data = dev_get_drvdata(dev);
1230 mutex_lock(&data->update_lock);
1231 err = lm90_update_device(dev);
1232 mutex_unlock(&data->update_lock);
1237 case hwmon_chip_update_interval:
1238 *val = data->update_interval;
1240 case hwmon_chip_alarms:
1241 *val = data->alarms;
1250 static int lm90_chip_write(struct device *dev, u32 attr, int channel, long val)
1252 struct lm90_data *data = dev_get_drvdata(dev);
1253 struct i2c_client *client = data->client;
1256 mutex_lock(&data->update_lock);
1258 err = lm90_update_device(dev);
1263 case hwmon_chip_update_interval:
1264 err = lm90_set_convrate(client, data,
1265 clamp_val(val, 0, 100000));
1272 mutex_unlock(&data->update_lock);
1277 static umode_t lm90_chip_is_visible(const void *data, u32 attr, int channel)
1280 case hwmon_chip_update_interval:
1281 return S_IRUGO | S_IWUSR;
1282 case hwmon_chip_alarms:
1289 static int lm90_read(struct device *dev, enum hwmon_sensor_types type,
1290 u32 attr, int channel, long *val)
1294 return lm90_chip_read(dev, attr, channel, val);
1296 return lm90_temp_read(dev, attr, channel, val);
1302 static int lm90_write(struct device *dev, enum hwmon_sensor_types type,
1303 u32 attr, int channel, long val)
1307 return lm90_chip_write(dev, attr, channel, val);
1309 return lm90_temp_write(dev, attr, channel, val);
1315 static umode_t lm90_is_visible(const void *data, enum hwmon_sensor_types type,
1316 u32 attr, int channel)
1320 return lm90_chip_is_visible(data, attr, channel);
1322 return lm90_temp_is_visible(data, attr, channel);
1328 /* Return 0 if detection is successful, -ENODEV otherwise */
1329 static int lm90_detect(struct i2c_client *client,
1330 struct i2c_board_info *info)
1332 struct i2c_adapter *adapter = client->adapter;
1333 int address = client->addr;
1334 const char *name = NULL;
1335 int man_id, chip_id, config1, config2, convrate;
1337 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
1340 /* detection and identification */
1341 man_id = i2c_smbus_read_byte_data(client, LM90_REG_R_MAN_ID);
1342 chip_id = i2c_smbus_read_byte_data(client, LM90_REG_R_CHIP_ID);
1343 config1 = i2c_smbus_read_byte_data(client, LM90_REG_R_CONFIG1);
1344 convrate = i2c_smbus_read_byte_data(client, LM90_REG_R_CONVRATE);
1345 if (man_id < 0 || chip_id < 0 || config1 < 0 || convrate < 0)
1348 if (man_id == 0x01 || man_id == 0x5C || man_id == 0x41) {
1349 config2 = i2c_smbus_read_byte_data(client, LM90_REG_R_CONFIG2);
1353 config2 = 0; /* Make compiler happy */
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");