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/of_device.h>
96 #include <linux/sysfs.h>
97 #include <linux/interrupt.h>
98 #include <linux/regulator/consumer.h>
102 * Address is fully defined internally and cannot be changed except for
103 * MAX6659, MAX6680 and MAX6681.
104 * LM86, LM89, LM90, LM99, ADM1032, ADM1032-1, ADT7461, ADT7461A, MAX6649,
105 * MAX6657, MAX6658, NCT1008 and W83L771 have address 0x4c.
106 * ADM1032-2, ADT7461-2, ADT7461A-2, LM89-1, LM99-1, MAX6646, and NCT1008D
108 * MAX6647 has address 0x4e.
109 * MAX6659 can have address 0x4c, 0x4d or 0x4e.
110 * MAX6680 and MAX6681 can have address 0x18, 0x19, 0x1a, 0x29, 0x2a, 0x2b,
111 * 0x4c, 0x4d or 0x4e.
112 * SA56004 can have address 0x48 through 0x4F.
115 static const unsigned short normal_i2c[] = {
116 0x18, 0x19, 0x1a, 0x29, 0x2a, 0x2b, 0x48, 0x49, 0x4a, 0x4b, 0x4c,
117 0x4d, 0x4e, 0x4f, I2C_CLIENT_END };
119 enum chips { lm90, adm1032, lm99, lm86, max6657, max6659, adt7461, max6680,
120 max6646, w83l771, max6696, sa56004, g781, tmp451 };
126 #define LM90_REG_R_MAN_ID 0xFE
127 #define LM90_REG_R_CHIP_ID 0xFF
128 #define LM90_REG_R_CONFIG1 0x03
129 #define LM90_REG_W_CONFIG1 0x09
130 #define LM90_REG_R_CONFIG2 0xBF
131 #define LM90_REG_W_CONFIG2 0xBF
132 #define LM90_REG_R_CONVRATE 0x04
133 #define LM90_REG_W_CONVRATE 0x0A
134 #define LM90_REG_R_STATUS 0x02
135 #define LM90_REG_R_LOCAL_TEMP 0x00
136 #define LM90_REG_R_LOCAL_HIGH 0x05
137 #define LM90_REG_W_LOCAL_HIGH 0x0B
138 #define LM90_REG_R_LOCAL_LOW 0x06
139 #define LM90_REG_W_LOCAL_LOW 0x0C
140 #define LM90_REG_R_LOCAL_CRIT 0x20
141 #define LM90_REG_W_LOCAL_CRIT 0x20
142 #define LM90_REG_R_REMOTE_TEMPH 0x01
143 #define LM90_REG_R_REMOTE_TEMPL 0x10
144 #define LM90_REG_R_REMOTE_OFFSH 0x11
145 #define LM90_REG_W_REMOTE_OFFSH 0x11
146 #define LM90_REG_R_REMOTE_OFFSL 0x12
147 #define LM90_REG_W_REMOTE_OFFSL 0x12
148 #define LM90_REG_R_REMOTE_HIGHH 0x07
149 #define LM90_REG_W_REMOTE_HIGHH 0x0D
150 #define LM90_REG_R_REMOTE_HIGHL 0x13
151 #define LM90_REG_W_REMOTE_HIGHL 0x13
152 #define LM90_REG_R_REMOTE_LOWH 0x08
153 #define LM90_REG_W_REMOTE_LOWH 0x0E
154 #define LM90_REG_R_REMOTE_LOWL 0x14
155 #define LM90_REG_W_REMOTE_LOWL 0x14
156 #define LM90_REG_R_REMOTE_CRIT 0x19
157 #define LM90_REG_W_REMOTE_CRIT 0x19
158 #define LM90_REG_R_TCRIT_HYST 0x21
159 #define LM90_REG_W_TCRIT_HYST 0x21
161 /* MAX6646/6647/6649/6657/6658/6659/6695/6696 registers */
163 #define MAX6657_REG_R_LOCAL_TEMPL 0x11
164 #define MAX6696_REG_R_STATUS2 0x12
165 #define MAX6659_REG_R_REMOTE_EMERG 0x16
166 #define MAX6659_REG_W_REMOTE_EMERG 0x16
167 #define MAX6659_REG_R_LOCAL_EMERG 0x17
168 #define MAX6659_REG_W_LOCAL_EMERG 0x17
170 /* SA56004 registers */
172 #define SA56004_REG_R_LOCAL_TEMPL 0x22
174 #define LM90_MAX_CONVRATE_MS 16000 /* Maximum conversion rate in ms */
176 /* TMP451 registers */
177 #define TMP451_REG_R_LOCAL_TEMPL 0x15
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_PAUSE_FOR_CONFIG (1 << 8) /* Pause conversion for config */
193 #define LM90_STATUS_LTHRM (1 << 0) /* local THERM limit tripped */
194 #define LM90_STATUS_RTHRM (1 << 1) /* remote THERM limit tripped */
195 #define LM90_STATUS_ROPEN (1 << 2) /* remote is an open circuit */
196 #define LM90_STATUS_RLOW (1 << 3) /* remote low temp limit tripped */
197 #define LM90_STATUS_RHIGH (1 << 4) /* remote high temp limit tripped */
198 #define LM90_STATUS_LLOW (1 << 5) /* local low temp limit tripped */
199 #define LM90_STATUS_LHIGH (1 << 6) /* local high temp limit tripped */
200 #define LM90_STATUS_BUSY (1 << 7) /* conversion is ongoing */
202 #define MAX6696_STATUS2_R2THRM (1 << 1) /* remote2 THERM limit tripped */
203 #define MAX6696_STATUS2_R2OPEN (1 << 2) /* remote2 is an open circuit */
204 #define MAX6696_STATUS2_R2LOW (1 << 3) /* remote2 low temp limit tripped */
205 #define MAX6696_STATUS2_R2HIGH (1 << 4) /* remote2 high temp limit tripped */
206 #define MAX6696_STATUS2_ROT2 (1 << 5) /* remote emergency limit tripped */
207 #define MAX6696_STATUS2_R2OT2 (1 << 6) /* remote2 emergency limit tripped */
208 #define MAX6696_STATUS2_LOT2 (1 << 7) /* local emergency limit tripped */
211 * Driver data (common to all clients)
214 static const struct i2c_device_id lm90_id[] = {
215 { "adm1032", adm1032 },
216 { "adt7461", adt7461 },
217 { "adt7461a", adt7461 },
223 { "max6646", max6646 },
224 { "max6647", max6646 },
225 { "max6649", max6646 },
226 { "max6657", max6657 },
227 { "max6658", max6657 },
228 { "max6659", max6659 },
229 { "max6680", max6680 },
230 { "max6681", max6680 },
231 { "max6695", max6696 },
232 { "max6696", max6696 },
233 { "nct1008", adt7461 },
234 { "w83l771", w83l771 },
235 { "sa56004", sa56004 },
236 { "tmp451", tmp451 },
239 MODULE_DEVICE_TABLE(i2c, lm90_id);
241 static const struct of_device_id lm90_of_match[] = {
243 .compatible = "adi,adm1032",
244 .data = (void *)adm1032
247 .compatible = "adi,adt7461",
248 .data = (void *)adt7461
251 .compatible = "adi,adt7461a",
252 .data = (void *)adt7461
255 .compatible = "gmt,g781",
259 .compatible = "national,lm90",
263 .compatible = "national,lm86",
267 .compatible = "national,lm89",
271 .compatible = "national,lm99",
275 .compatible = "dallas,max6646",
276 .data = (void *)max6646
279 .compatible = "dallas,max6647",
280 .data = (void *)max6646
283 .compatible = "dallas,max6649",
284 .data = (void *)max6646
287 .compatible = "dallas,max6657",
288 .data = (void *)max6657
291 .compatible = "dallas,max6658",
292 .data = (void *)max6657
295 .compatible = "dallas,max6659",
296 .data = (void *)max6659
299 .compatible = "dallas,max6680",
300 .data = (void *)max6680
303 .compatible = "dallas,max6681",
304 .data = (void *)max6680
307 .compatible = "dallas,max6695",
308 .data = (void *)max6696
311 .compatible = "dallas,max6696",
312 .data = (void *)max6696
315 .compatible = "onnn,nct1008",
316 .data = (void *)adt7461
319 .compatible = "winbond,w83l771",
320 .data = (void *)w83l771
323 .compatible = "nxp,sa56004",
324 .data = (void *)sa56004
327 .compatible = "ti,tmp451",
328 .data = (void *)tmp451
332 MODULE_DEVICE_TABLE(of, lm90_of_match);
335 * chip type specific parameters
338 u32 flags; /* Capabilities */
339 u16 alert_alarms; /* Which alarm bits trigger ALERT# */
340 /* Upper 8 bits for max6695/96 */
341 u8 max_convrate; /* Maximum conversion rate register value */
342 u8 reg_local_ext; /* Extended local temp register (optional) */
345 static const struct lm90_params lm90_params[] = {
347 .flags = LM90_HAVE_OFFSET | LM90_HAVE_REM_LIMIT_EXT
348 | LM90_HAVE_BROKEN_ALERT,
349 .alert_alarms = 0x7c,
353 .flags = LM90_HAVE_OFFSET | LM90_HAVE_REM_LIMIT_EXT
354 | LM90_HAVE_BROKEN_ALERT,
355 .alert_alarms = 0x7c,
359 .flags = LM90_HAVE_OFFSET | LM90_HAVE_REM_LIMIT_EXT
360 | LM90_HAVE_BROKEN_ALERT,
361 .alert_alarms = 0x7c,
365 .flags = LM90_HAVE_OFFSET | LM90_HAVE_REM_LIMIT_EXT,
366 .alert_alarms = 0x7b,
370 .flags = LM90_HAVE_OFFSET | LM90_HAVE_REM_LIMIT_EXT,
371 .alert_alarms = 0x7b,
375 .flags = LM90_HAVE_OFFSET | LM90_HAVE_REM_LIMIT_EXT,
376 .alert_alarms = 0x7b,
380 .alert_alarms = 0x7c,
382 .reg_local_ext = MAX6657_REG_R_LOCAL_TEMPL,
385 .flags = LM90_PAUSE_FOR_CONFIG,
386 .alert_alarms = 0x7c,
388 .reg_local_ext = MAX6657_REG_R_LOCAL_TEMPL,
391 .flags = LM90_HAVE_EMERGENCY,
392 .alert_alarms = 0x7c,
394 .reg_local_ext = MAX6657_REG_R_LOCAL_TEMPL,
397 .flags = LM90_HAVE_OFFSET,
398 .alert_alarms = 0x7c,
402 .flags = LM90_HAVE_EMERGENCY
403 | LM90_HAVE_EMERGENCY_ALARM | LM90_HAVE_TEMP3,
404 .alert_alarms = 0x1c7c,
406 .reg_local_ext = MAX6657_REG_R_LOCAL_TEMPL,
409 .flags = LM90_HAVE_OFFSET | LM90_HAVE_REM_LIMIT_EXT,
410 .alert_alarms = 0x7c,
414 .flags = LM90_HAVE_OFFSET | LM90_HAVE_REM_LIMIT_EXT,
415 .alert_alarms = 0x7b,
417 .reg_local_ext = SA56004_REG_R_LOCAL_TEMPL,
420 .flags = LM90_HAVE_OFFSET | LM90_HAVE_REM_LIMIT_EXT
421 | LM90_HAVE_BROKEN_ALERT,
422 .alert_alarms = 0x7c,
424 .reg_local_ext = TMP451_REG_R_LOCAL_TEMPL,
429 * TEMP8 register index
431 enum lm90_temp8_reg_index {
436 LOCAL_EMERG, /* max6659 and max6695/96 */
437 REMOTE_EMERG, /* max6659 and max6695/96 */
438 REMOTE2_CRIT, /* max6695/96 only */
439 REMOTE2_EMERG, /* max6695/96 only */
444 * TEMP11 register index
446 enum lm90_temp11_reg_index {
450 REMOTE_OFFSET, /* except max6646, max6657/58/59, and max6695/96 */
452 REMOTE2_TEMP, /* max6695/96 only */
453 REMOTE2_LOW, /* max6695/96 only */
454 REMOTE2_HIGH, /* max6695/96 only */
459 * Client data (each client gets its own)
463 struct i2c_client *client;
464 u32 channel_config[4];
465 struct hwmon_channel_info temp_info;
466 const struct hwmon_channel_info *info[3];
467 struct hwmon_chip_info chip;
468 struct mutex update_lock;
469 bool valid; /* true if register values are valid */
470 unsigned long last_updated; /* in jiffies */
474 unsigned int update_interval; /* in milliseconds */
476 u8 config_orig; /* Original configuration register value */
477 u8 convrate_orig; /* Original conversion rate register value */
478 u16 alert_alarms; /* Which alarm bits trigger ALERT# */
479 /* Upper 8 bits for max6695/96 */
480 u8 max_convrate; /* Maximum conversion rate */
481 u8 reg_local_ext; /* local extension register offset */
483 /* registers values */
484 s8 temp8[TEMP8_REG_NUM];
485 s16 temp11[TEMP11_REG_NUM];
487 u16 alarms; /* bitvector (upper 8 bits for max6695/96) */
495 * The ADM1032 supports PEC but not on write byte transactions, so we need
496 * to explicitly ask for a transaction without PEC.
498 static inline s32 adm1032_write_byte(struct i2c_client *client, u8 value)
500 return i2c_smbus_xfer(client->adapter, client->addr,
501 client->flags & ~I2C_CLIENT_PEC,
502 I2C_SMBUS_WRITE, value, I2C_SMBUS_BYTE, NULL);
506 * It is assumed that client->update_lock is held (unless we are in
507 * detection or initialization steps). This matters when PEC is enabled,
508 * because we don't want the address pointer to change between the write
509 * byte and the read byte transactions.
511 static int lm90_read_reg(struct i2c_client *client, u8 reg)
515 if (client->flags & I2C_CLIENT_PEC) {
516 err = adm1032_write_byte(client, reg);
518 err = i2c_smbus_read_byte(client);
520 err = i2c_smbus_read_byte_data(client, reg);
525 static int lm90_read16(struct i2c_client *client, u8 regh, u8 regl)
530 * There is a trick here. We have to read two registers to have the
531 * sensor temperature, but we have to beware a conversion could occur
532 * between the readings. The datasheet says we should either use
533 * the one-shot conversion register, which we don't want to do
534 * (disables hardware monitoring) or monitor the busy bit, which is
535 * impossible (we can't read the values and monitor that bit at the
536 * exact same time). So the solution used here is to read the high
537 * byte once, then the low byte, then the high byte again. If the new
538 * high byte matches the old one, then we have a valid reading. Else
539 * we have to read the low byte again, and now we believe we have a
542 oldh = lm90_read_reg(client, regh);
545 l = lm90_read_reg(client, regl);
548 newh = lm90_read_reg(client, regh);
552 l = lm90_read_reg(client, regl);
556 return (newh << 8) | l;
560 * client->update_lock must be held when calling this function (unless we are
561 * in detection or initialization steps), and while a remote channel other
562 * than channel 0 is selected. Also, calling code must make sure to re-select
563 * external channel 0 before releasing the lock. This is necessary because
564 * various registers have different meanings as a result of selecting a
565 * non-default remote channel.
567 static inline int lm90_select_remote_channel(struct i2c_client *client,
568 struct lm90_data *data,
573 if (data->kind == max6696) {
574 config = lm90_read_reg(client, LM90_REG_R_CONFIG1);
580 i2c_smbus_write_byte_data(client, LM90_REG_W_CONFIG1,
586 static int lm90_write_convrate(struct i2c_client *client,
587 struct lm90_data *data, int val)
590 int config_orig, config_stop;
592 /* Save config and pause conversion */
593 if (data->flags & LM90_PAUSE_FOR_CONFIG) {
594 config_orig = lm90_read_reg(client, LM90_REG_R_CONFIG1);
597 config_stop = config_orig | 0x40;
598 if (config_orig != config_stop) {
599 err = i2c_smbus_write_byte_data(client,
608 err = i2c_smbus_write_byte_data(client, LM90_REG_W_CONVRATE, val);
610 /* Revert change to config */
611 if (data->flags & LM90_PAUSE_FOR_CONFIG && config_orig != config_stop)
612 i2c_smbus_write_byte_data(client, LM90_REG_W_CONFIG1,
619 * Set conversion rate.
620 * client->update_lock must be held when calling this function (unless we are
621 * in detection or initialization steps).
623 static int lm90_set_convrate(struct i2c_client *client, struct lm90_data *data,
624 unsigned int interval)
626 unsigned int update_interval;
629 /* Shift calculations to avoid rounding errors */
632 /* find the nearest update rate */
633 for (i = 0, update_interval = LM90_MAX_CONVRATE_MS << 6;
634 i < data->max_convrate; i++, update_interval >>= 1)
635 if (interval >= update_interval * 3 / 4)
638 err = lm90_write_convrate(client, data, i);
639 data->update_interval = DIV_ROUND_CLOSEST(update_interval, 64);
643 static int lm90_update_limits(struct device *dev)
645 struct lm90_data *data = dev_get_drvdata(dev);
646 struct i2c_client *client = data->client;
649 val = lm90_read_reg(client, LM90_REG_R_LOCAL_CRIT);
652 data->temp8[LOCAL_CRIT] = val;
654 val = lm90_read_reg(client, LM90_REG_R_REMOTE_CRIT);
657 data->temp8[REMOTE_CRIT] = val;
659 val = lm90_read_reg(client, LM90_REG_R_TCRIT_HYST);
662 data->temp_hyst = val;
664 val = lm90_read_reg(client, LM90_REG_R_REMOTE_LOWH);
667 data->temp11[REMOTE_LOW] = val << 8;
669 if (data->flags & LM90_HAVE_REM_LIMIT_EXT) {
670 val = lm90_read_reg(client, LM90_REG_R_REMOTE_LOWL);
673 data->temp11[REMOTE_LOW] |= val;
676 val = lm90_read_reg(client, LM90_REG_R_REMOTE_HIGHH);
679 data->temp11[REMOTE_HIGH] = val << 8;
681 if (data->flags & LM90_HAVE_REM_LIMIT_EXT) {
682 val = lm90_read_reg(client, LM90_REG_R_REMOTE_HIGHL);
685 data->temp11[REMOTE_HIGH] |= val;
688 if (data->flags & LM90_HAVE_OFFSET) {
689 val = lm90_read16(client, LM90_REG_R_REMOTE_OFFSH,
690 LM90_REG_R_REMOTE_OFFSL);
693 data->temp11[REMOTE_OFFSET] = val;
696 if (data->flags & LM90_HAVE_EMERGENCY) {
697 val = lm90_read_reg(client, MAX6659_REG_R_LOCAL_EMERG);
700 data->temp8[LOCAL_EMERG] = val;
702 val = lm90_read_reg(client, MAX6659_REG_R_REMOTE_EMERG);
705 data->temp8[REMOTE_EMERG] = val;
708 if (data->kind == max6696) {
709 val = lm90_select_remote_channel(client, data, 1);
713 val = lm90_read_reg(client, LM90_REG_R_REMOTE_CRIT);
716 data->temp8[REMOTE2_CRIT] = val;
718 val = lm90_read_reg(client, MAX6659_REG_R_REMOTE_EMERG);
721 data->temp8[REMOTE2_EMERG] = val;
723 val = lm90_read_reg(client, LM90_REG_R_REMOTE_LOWH);
726 data->temp11[REMOTE2_LOW] = val << 8;
728 val = lm90_read_reg(client, LM90_REG_R_REMOTE_HIGHH);
731 data->temp11[REMOTE2_HIGH] = val << 8;
733 lm90_select_remote_channel(client, data, 0);
739 static int lm90_update_device(struct device *dev)
741 struct lm90_data *data = dev_get_drvdata(dev);
742 struct i2c_client *client = data->client;
743 unsigned long next_update;
747 val = lm90_update_limits(dev);
752 next_update = data->last_updated +
753 msecs_to_jiffies(data->update_interval);
754 if (time_after(jiffies, next_update) || !data->valid) {
755 dev_dbg(&client->dev, "Updating lm90 data.\n");
759 val = lm90_read_reg(client, LM90_REG_R_LOCAL_LOW);
762 data->temp8[LOCAL_LOW] = val;
764 val = lm90_read_reg(client, LM90_REG_R_LOCAL_HIGH);
767 data->temp8[LOCAL_HIGH] = val;
769 if (data->reg_local_ext) {
770 val = lm90_read16(client, LM90_REG_R_LOCAL_TEMP,
771 data->reg_local_ext);
774 data->temp11[LOCAL_TEMP] = val;
776 val = lm90_read_reg(client, LM90_REG_R_LOCAL_TEMP);
779 data->temp11[LOCAL_TEMP] = val << 8;
781 val = lm90_read16(client, LM90_REG_R_REMOTE_TEMPH,
782 LM90_REG_R_REMOTE_TEMPL);
785 data->temp11[REMOTE_TEMP] = val;
787 val = lm90_read_reg(client, LM90_REG_R_STATUS);
790 data->alarms = val & ~LM90_STATUS_BUSY;
792 if (data->kind == max6696) {
793 val = lm90_select_remote_channel(client, data, 1);
797 val = lm90_read16(client, LM90_REG_R_REMOTE_TEMPH,
798 LM90_REG_R_REMOTE_TEMPL);
800 lm90_select_remote_channel(client, data, 0);
803 data->temp11[REMOTE2_TEMP] = val;
805 lm90_select_remote_channel(client, data, 0);
807 val = lm90_read_reg(client, MAX6696_REG_R_STATUS2);
810 data->alarms |= val << 8;
814 * Re-enable ALERT# output if it was originally enabled and
815 * relevant alarms are all clear
817 if (!(data->config_orig & 0x80) &&
818 !(data->alarms & data->alert_alarms)) {
819 val = lm90_read_reg(client, LM90_REG_R_CONFIG1);
824 dev_dbg(&client->dev, "Re-enabling ALERT#\n");
825 i2c_smbus_write_byte_data(client,
831 data->last_updated = jiffies;
840 * For local temperatures and limits, critical limits and the hysteresis
841 * value, the LM90 uses signed 8-bit values with LSB = 1 degree Celsius.
842 * For remote temperatures and limits, it uses signed 11-bit values with
843 * LSB = 0.125 degree Celsius, left-justified in 16-bit registers. Some
844 * Maxim chips use unsigned values.
847 static inline int temp_from_s8(s8 val)
852 static inline int temp_from_u8(u8 val)
857 static inline int temp_from_s16(s16 val)
859 return val / 32 * 125;
862 static inline int temp_from_u16(u16 val)
864 return val / 32 * 125;
867 static s8 temp_to_s8(long val)
874 return (val - 500) / 1000;
875 return (val + 500) / 1000;
878 static u8 temp_to_u8(long val)
884 return (val + 500) / 1000;
887 static s16 temp_to_s16(long val)
894 return (val - 62) / 125 * 32;
895 return (val + 62) / 125 * 32;
898 static u8 hyst_to_reg(long val)
904 return (val + 500) / 1000;
908 * ADT7461 in compatibility mode is almost identical to LM90 except that
909 * attempts to write values that are outside the range 0 < temp < 127 are
910 * treated as the boundary value.
912 * ADT7461 in "extended mode" operation uses unsigned integers offset by
913 * 64 (e.g., 0 -> -64 degC). The range is restricted to -64..191 degC.
915 static inline int temp_from_u8_adt7461(struct lm90_data *data, u8 val)
917 if (data->flags & LM90_FLAG_ADT7461_EXT)
918 return (val - 64) * 1000;
919 return temp_from_s8(val);
922 static inline int temp_from_u16_adt7461(struct lm90_data *data, u16 val)
924 if (data->flags & LM90_FLAG_ADT7461_EXT)
925 return (val - 0x4000) / 64 * 250;
926 return temp_from_s16(val);
929 static u8 temp_to_u8_adt7461(struct lm90_data *data, long val)
931 if (data->flags & LM90_FLAG_ADT7461_EXT) {
936 return (val + 500 + 64000) / 1000;
942 return (val + 500) / 1000;
945 static u16 temp_to_u16_adt7461(struct lm90_data *data, long val)
947 if (data->flags & LM90_FLAG_ADT7461_EXT) {
952 return (val + 64000 + 125) / 250 * 64;
958 return (val + 125) / 250 * 64;
961 /* pec used for ADM1032 only */
962 static ssize_t pec_show(struct device *dev, struct device_attribute *dummy,
965 struct i2c_client *client = to_i2c_client(dev);
967 return sprintf(buf, "%d\n", !!(client->flags & I2C_CLIENT_PEC));
970 static ssize_t pec_store(struct device *dev, struct device_attribute *dummy,
971 const char *buf, size_t count)
973 struct i2c_client *client = to_i2c_client(dev);
977 err = kstrtol(buf, 10, &val);
983 client->flags &= ~I2C_CLIENT_PEC;
986 client->flags |= I2C_CLIENT_PEC;
995 static DEVICE_ATTR_RW(pec);
997 static int lm90_get_temp11(struct lm90_data *data, int index)
999 s16 temp11 = data->temp11[index];
1002 if (data->kind == adt7461 || data->kind == tmp451)
1003 temp = temp_from_u16_adt7461(data, temp11);
1004 else if (data->kind == max6646)
1005 temp = temp_from_u16(temp11);
1007 temp = temp_from_s16(temp11);
1009 /* +16 degrees offset for temp2 for the LM99 */
1010 if (data->kind == lm99 && index <= 2)
1016 static int lm90_set_temp11(struct lm90_data *data, int index, long val)
1022 [REMOTE_LOW] = { LM90_REG_W_REMOTE_LOWH, LM90_REG_W_REMOTE_LOWL },
1023 [REMOTE_HIGH] = { LM90_REG_W_REMOTE_HIGHH, LM90_REG_W_REMOTE_HIGHL },
1024 [REMOTE_OFFSET] = { LM90_REG_W_REMOTE_OFFSH, LM90_REG_W_REMOTE_OFFSL },
1025 [REMOTE2_LOW] = { LM90_REG_W_REMOTE_LOWH, LM90_REG_W_REMOTE_LOWL },
1026 [REMOTE2_HIGH] = { LM90_REG_W_REMOTE_HIGHH, LM90_REG_W_REMOTE_HIGHL }
1028 struct i2c_client *client = data->client;
1029 struct reg *regp = ®[index];
1032 /* +16 degrees offset for temp2 for the LM99 */
1033 if (data->kind == lm99 && index <= 2)
1036 if (data->kind == adt7461 || data->kind == tmp451)
1037 data->temp11[index] = temp_to_u16_adt7461(data, val);
1038 else if (data->kind == max6646)
1039 data->temp11[index] = temp_to_u8(val) << 8;
1040 else if (data->flags & LM90_HAVE_REM_LIMIT_EXT)
1041 data->temp11[index] = temp_to_s16(val);
1043 data->temp11[index] = temp_to_s8(val) << 8;
1045 lm90_select_remote_channel(client, data, index >= 3);
1046 err = i2c_smbus_write_byte_data(client, regp->high,
1047 data->temp11[index] >> 8);
1050 if (data->flags & LM90_HAVE_REM_LIMIT_EXT)
1051 err = i2c_smbus_write_byte_data(client, regp->low,
1052 data->temp11[index] & 0xff);
1054 lm90_select_remote_channel(client, data, 0);
1058 static int lm90_get_temp8(struct lm90_data *data, int index)
1060 s8 temp8 = data->temp8[index];
1063 if (data->kind == adt7461 || data->kind == tmp451)
1064 temp = temp_from_u8_adt7461(data, temp8);
1065 else if (data->kind == max6646)
1066 temp = temp_from_u8(temp8);
1068 temp = temp_from_s8(temp8);
1070 /* +16 degrees offset for temp2 for the LM99 */
1071 if (data->kind == lm99 && index == 3)
1077 static int lm90_set_temp8(struct lm90_data *data, int index, long val)
1079 static const u8 reg[TEMP8_REG_NUM] = {
1080 LM90_REG_W_LOCAL_LOW,
1081 LM90_REG_W_LOCAL_HIGH,
1082 LM90_REG_W_LOCAL_CRIT,
1083 LM90_REG_W_REMOTE_CRIT,
1084 MAX6659_REG_W_LOCAL_EMERG,
1085 MAX6659_REG_W_REMOTE_EMERG,
1086 LM90_REG_W_REMOTE_CRIT,
1087 MAX6659_REG_W_REMOTE_EMERG,
1089 struct i2c_client *client = data->client;
1092 /* +16 degrees offset for temp2 for the LM99 */
1093 if (data->kind == lm99 && index == 3)
1096 if (data->kind == adt7461 || data->kind == tmp451)
1097 data->temp8[index] = temp_to_u8_adt7461(data, val);
1098 else if (data->kind == max6646)
1099 data->temp8[index] = temp_to_u8(val);
1101 data->temp8[index] = temp_to_s8(val);
1103 lm90_select_remote_channel(client, data, index >= 6);
1104 err = i2c_smbus_write_byte_data(client, reg[index], data->temp8[index]);
1105 lm90_select_remote_channel(client, data, 0);
1110 static int lm90_get_temphyst(struct lm90_data *data, int index)
1114 if (data->kind == adt7461 || data->kind == tmp451)
1115 temp = temp_from_u8_adt7461(data, data->temp8[index]);
1116 else if (data->kind == max6646)
1117 temp = temp_from_u8(data->temp8[index]);
1119 temp = temp_from_s8(data->temp8[index]);
1121 /* +16 degrees offset for temp2 for the LM99 */
1122 if (data->kind == lm99 && index == 3)
1125 return temp - temp_from_s8(data->temp_hyst);
1128 static int lm90_set_temphyst(struct lm90_data *data, long val)
1130 struct i2c_client *client = data->client;
1134 if (data->kind == adt7461 || data->kind == tmp451)
1135 temp = temp_from_u8_adt7461(data, data->temp8[LOCAL_CRIT]);
1136 else if (data->kind == max6646)
1137 temp = temp_from_u8(data->temp8[LOCAL_CRIT]);
1139 temp = temp_from_s8(data->temp8[LOCAL_CRIT]);
1141 data->temp_hyst = hyst_to_reg(temp - val);
1142 err = i2c_smbus_write_byte_data(client, LM90_REG_W_TCRIT_HYST,
1147 static const u8 lm90_temp_index[3] = {
1148 LOCAL_TEMP, REMOTE_TEMP, REMOTE2_TEMP
1151 static const u8 lm90_temp_min_index[3] = {
1152 LOCAL_LOW, REMOTE_LOW, REMOTE2_LOW
1155 static const u8 lm90_temp_max_index[3] = {
1156 LOCAL_HIGH, REMOTE_HIGH, REMOTE2_HIGH
1159 static const u8 lm90_temp_crit_index[3] = {
1160 LOCAL_CRIT, REMOTE_CRIT, REMOTE2_CRIT
1163 static const u8 lm90_temp_emerg_index[3] = {
1164 LOCAL_EMERG, REMOTE_EMERG, REMOTE2_EMERG
1167 static const u8 lm90_min_alarm_bits[3] = { 5, 3, 11 };
1168 static const u8 lm90_max_alarm_bits[3] = { 6, 4, 12 };
1169 static const u8 lm90_crit_alarm_bits[3] = { 0, 1, 9 };
1170 static const u8 lm90_emergency_alarm_bits[3] = { 15, 13, 14 };
1171 static const u8 lm90_fault_bits[3] = { 0, 2, 10 };
1173 static int lm90_temp_read(struct device *dev, u32 attr, int channel, long *val)
1175 struct lm90_data *data = dev_get_drvdata(dev);
1178 mutex_lock(&data->update_lock);
1179 err = lm90_update_device(dev);
1180 mutex_unlock(&data->update_lock);
1185 case hwmon_temp_input:
1186 *val = lm90_get_temp11(data, lm90_temp_index[channel]);
1188 case hwmon_temp_min_alarm:
1189 *val = (data->alarms >> lm90_min_alarm_bits[channel]) & 1;
1191 case hwmon_temp_max_alarm:
1192 *val = (data->alarms >> lm90_max_alarm_bits[channel]) & 1;
1194 case hwmon_temp_crit_alarm:
1195 *val = (data->alarms >> lm90_crit_alarm_bits[channel]) & 1;
1197 case hwmon_temp_emergency_alarm:
1198 *val = (data->alarms >> lm90_emergency_alarm_bits[channel]) & 1;
1200 case hwmon_temp_fault:
1201 *val = (data->alarms >> lm90_fault_bits[channel]) & 1;
1203 case hwmon_temp_min:
1205 *val = lm90_get_temp8(data,
1206 lm90_temp_min_index[channel]);
1208 *val = lm90_get_temp11(data,
1209 lm90_temp_min_index[channel]);
1211 case hwmon_temp_max:
1213 *val = lm90_get_temp8(data,
1214 lm90_temp_max_index[channel]);
1216 *val = lm90_get_temp11(data,
1217 lm90_temp_max_index[channel]);
1219 case hwmon_temp_crit:
1220 *val = lm90_get_temp8(data, lm90_temp_crit_index[channel]);
1222 case hwmon_temp_crit_hyst:
1223 *val = lm90_get_temphyst(data, lm90_temp_crit_index[channel]);
1225 case hwmon_temp_emergency:
1226 *val = lm90_get_temp8(data, lm90_temp_emerg_index[channel]);
1228 case hwmon_temp_emergency_hyst:
1229 *val = lm90_get_temphyst(data, lm90_temp_emerg_index[channel]);
1231 case hwmon_temp_offset:
1232 *val = lm90_get_temp11(data, REMOTE_OFFSET);
1240 static int lm90_temp_write(struct device *dev, u32 attr, int channel, long val)
1242 struct lm90_data *data = dev_get_drvdata(dev);
1245 mutex_lock(&data->update_lock);
1247 err = lm90_update_device(dev);
1252 case hwmon_temp_min:
1254 err = lm90_set_temp8(data,
1255 lm90_temp_min_index[channel],
1258 err = lm90_set_temp11(data,
1259 lm90_temp_min_index[channel],
1262 case hwmon_temp_max:
1264 err = lm90_set_temp8(data,
1265 lm90_temp_max_index[channel],
1268 err = lm90_set_temp11(data,
1269 lm90_temp_max_index[channel],
1272 case hwmon_temp_crit:
1273 err = lm90_set_temp8(data, lm90_temp_crit_index[channel], val);
1275 case hwmon_temp_crit_hyst:
1276 err = lm90_set_temphyst(data, val);
1278 case hwmon_temp_emergency:
1279 err = lm90_set_temp8(data, lm90_temp_emerg_index[channel], val);
1281 case hwmon_temp_offset:
1282 err = lm90_set_temp11(data, REMOTE_OFFSET, val);
1289 mutex_unlock(&data->update_lock);
1294 static umode_t lm90_temp_is_visible(const void *data, u32 attr, int channel)
1297 case hwmon_temp_input:
1298 case hwmon_temp_min_alarm:
1299 case hwmon_temp_max_alarm:
1300 case hwmon_temp_crit_alarm:
1301 case hwmon_temp_emergency_alarm:
1302 case hwmon_temp_emergency_hyst:
1303 case hwmon_temp_fault:
1305 case hwmon_temp_min:
1306 case hwmon_temp_max:
1307 case hwmon_temp_crit:
1308 case hwmon_temp_emergency:
1309 case hwmon_temp_offset:
1310 return S_IRUGO | S_IWUSR;
1311 case hwmon_temp_crit_hyst:
1313 return S_IRUGO | S_IWUSR;
1320 static int lm90_chip_read(struct device *dev, u32 attr, int channel, long *val)
1322 struct lm90_data *data = dev_get_drvdata(dev);
1325 mutex_lock(&data->update_lock);
1326 err = lm90_update_device(dev);
1327 mutex_unlock(&data->update_lock);
1332 case hwmon_chip_update_interval:
1333 *val = data->update_interval;
1335 case hwmon_chip_alarms:
1336 *val = data->alarms;
1345 static int lm90_chip_write(struct device *dev, u32 attr, int channel, long val)
1347 struct lm90_data *data = dev_get_drvdata(dev);
1348 struct i2c_client *client = data->client;
1351 mutex_lock(&data->update_lock);
1353 err = lm90_update_device(dev);
1358 case hwmon_chip_update_interval:
1359 err = lm90_set_convrate(client, data,
1360 clamp_val(val, 0, 100000));
1367 mutex_unlock(&data->update_lock);
1372 static umode_t lm90_chip_is_visible(const void *data, u32 attr, int channel)
1375 case hwmon_chip_update_interval:
1376 return S_IRUGO | S_IWUSR;
1377 case hwmon_chip_alarms:
1384 static int lm90_read(struct device *dev, enum hwmon_sensor_types type,
1385 u32 attr, int channel, long *val)
1389 return lm90_chip_read(dev, attr, channel, val);
1391 return lm90_temp_read(dev, attr, channel, val);
1397 static int lm90_write(struct device *dev, enum hwmon_sensor_types type,
1398 u32 attr, int channel, long val)
1402 return lm90_chip_write(dev, attr, channel, val);
1404 return lm90_temp_write(dev, attr, channel, val);
1410 static umode_t lm90_is_visible(const void *data, enum hwmon_sensor_types type,
1411 u32 attr, int channel)
1415 return lm90_chip_is_visible(data, attr, channel);
1417 return lm90_temp_is_visible(data, attr, channel);
1423 /* Return 0 if detection is successful, -ENODEV otherwise */
1424 static int lm90_detect(struct i2c_client *client,
1425 struct i2c_board_info *info)
1427 struct i2c_adapter *adapter = client->adapter;
1428 int address = client->addr;
1429 const char *name = NULL;
1430 int man_id, chip_id, config1, config2, convrate;
1432 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
1435 /* detection and identification */
1436 man_id = i2c_smbus_read_byte_data(client, LM90_REG_R_MAN_ID);
1437 chip_id = i2c_smbus_read_byte_data(client, LM90_REG_R_CHIP_ID);
1438 config1 = i2c_smbus_read_byte_data(client, LM90_REG_R_CONFIG1);
1439 convrate = i2c_smbus_read_byte_data(client, LM90_REG_R_CONVRATE);
1440 if (man_id < 0 || chip_id < 0 || config1 < 0 || convrate < 0)
1443 if (man_id == 0x01 || man_id == 0x5C || man_id == 0xA1) {
1444 config2 = i2c_smbus_read_byte_data(client, LM90_REG_R_CONFIG2);
1449 if ((address == 0x4C || address == 0x4D)
1450 && man_id == 0x01) { /* National Semiconductor */
1451 if ((config1 & 0x2A) == 0x00
1452 && (config2 & 0xF8) == 0x00
1453 && convrate <= 0x09) {
1455 && (chip_id & 0xF0) == 0x20) { /* LM90 */
1458 if ((chip_id & 0xF0) == 0x30) { /* LM89/LM99 */
1460 dev_info(&adapter->dev,
1461 "Assuming LM99 chip at 0x%02x\n",
1463 dev_info(&adapter->dev,
1464 "If it is an LM89, instantiate it "
1465 "with the new_device sysfs "
1469 && (chip_id & 0xF0) == 0x10) { /* LM86 */
1474 if ((address == 0x4C || address == 0x4D)
1475 && man_id == 0x41) { /* Analog Devices */
1476 if ((chip_id & 0xF0) == 0x40 /* ADM1032 */
1477 && (config1 & 0x3F) == 0x00
1478 && convrate <= 0x0A) {
1481 * The ADM1032 supports PEC, but only if combined
1482 * transactions are not used.
1484 if (i2c_check_functionality(adapter,
1485 I2C_FUNC_SMBUS_BYTE))
1486 info->flags |= I2C_CLIENT_PEC;
1488 if (chip_id == 0x51 /* ADT7461 */
1489 && (config1 & 0x1B) == 0x00
1490 && convrate <= 0x0A) {
1493 if (chip_id == 0x57 /* ADT7461A, NCT1008 */
1494 && (config1 & 0x1B) == 0x00
1495 && convrate <= 0x0A) {
1499 if (man_id == 0x4D) { /* Maxim */
1500 int emerg, emerg2, status2;
1503 * We read MAX6659_REG_R_REMOTE_EMERG twice, and re-read
1504 * LM90_REG_R_MAN_ID in between. If MAX6659_REG_R_REMOTE_EMERG
1505 * exists, both readings will reflect the same value. Otherwise,
1506 * the readings will be different.
1508 emerg = i2c_smbus_read_byte_data(client,
1509 MAX6659_REG_R_REMOTE_EMERG);
1510 man_id = i2c_smbus_read_byte_data(client,
1512 emerg2 = i2c_smbus_read_byte_data(client,
1513 MAX6659_REG_R_REMOTE_EMERG);
1514 status2 = i2c_smbus_read_byte_data(client,
1515 MAX6696_REG_R_STATUS2);
1516 if (emerg < 0 || man_id < 0 || emerg2 < 0 || status2 < 0)
1520 * The MAX6657, MAX6658 and MAX6659 do NOT have a chip_id
1521 * register. Reading from that address will return the last
1522 * read value, which in our case is those of the man_id
1523 * register. Likewise, the config1 register seems to lack a
1524 * low nibble, so the value will be those of the previous
1525 * read, so in our case those of the man_id register.
1526 * MAX6659 has a third set of upper temperature limit registers.
1527 * Those registers also return values on MAX6657 and MAX6658,
1528 * thus the only way to detect MAX6659 is by its address.
1529 * For this reason it will be mis-detected as MAX6657 if its
1532 if (chip_id == man_id
1533 && (address == 0x4C || address == 0x4D || address == 0x4E)
1534 && (config1 & 0x1F) == (man_id & 0x0F)
1535 && convrate <= 0x09) {
1536 if (address == 0x4C)
1542 * Even though MAX6695 and MAX6696 do not have a chip ID
1543 * register, reading it returns 0x01. Bit 4 of the config1
1544 * register is unused and should return zero when read. Bit 0 of
1545 * the status2 register is unused and should return zero when
1548 * MAX6695 and MAX6696 have an additional set of temperature
1549 * limit registers. We can detect those chips by checking if
1550 * one of those registers exists.
1553 && (config1 & 0x10) == 0x00
1554 && (status2 & 0x01) == 0x00
1556 && convrate <= 0x07) {
1560 * The chip_id register of the MAX6680 and MAX6681 holds the
1561 * revision of the chip. The lowest bit of the config1 register
1562 * is unused and should return zero when read, so should the
1563 * second to last bit of config1 (software reset).
1566 && (config1 & 0x03) == 0x00
1567 && convrate <= 0x07) {
1571 * The chip_id register of the MAX6646/6647/6649 holds the
1572 * revision of the chip. The lowest 6 bits of the config1
1573 * register are unused and should return zero when read.
1576 && (config1 & 0x3f) == 0x00
1577 && convrate <= 0x07) {
1582 && man_id == 0x5C) { /* Winbond/Nuvoton */
1583 if ((config1 & 0x2A) == 0x00
1584 && (config2 & 0xF8) == 0x00) {
1585 if (chip_id == 0x01 /* W83L771W/G */
1586 && convrate <= 0x09) {
1589 if ((chip_id & 0xFE) == 0x10 /* W83L771AWG/ASG */
1590 && convrate <= 0x08) {
1595 if (address >= 0x48 && address <= 0x4F
1596 && man_id == 0xA1) { /* NXP Semiconductor/Philips */
1598 && (config1 & 0x2A) == 0x00
1599 && (config2 & 0xFE) == 0x00
1600 && convrate <= 0x09) {
1604 if ((address == 0x4C || address == 0x4D)
1605 && man_id == 0x47) { /* GMT */
1606 if (chip_id == 0x01 /* G781 */
1607 && (config1 & 0x3F) == 0x00
1608 && convrate <= 0x08)
1612 && man_id == 0x55) { /* Texas Instruments */
1615 local_ext = i2c_smbus_read_byte_data(client,
1616 TMP451_REG_R_LOCAL_TEMPL);
1618 if (chip_id == 0x00 /* TMP451 */
1619 && (config1 & 0x1B) == 0x00
1621 && (local_ext & 0x0F) == 0x00)
1625 if (!name) { /* identification failed */
1626 dev_dbg(&adapter->dev,
1627 "Unsupported chip at 0x%02x (man_id=0x%02X, "
1628 "chip_id=0x%02X)\n", address, man_id, chip_id);
1632 strlcpy(info->type, name, I2C_NAME_SIZE);
1637 static void lm90_restore_conf(void *_data)
1639 struct lm90_data *data = _data;
1640 struct i2c_client *client = data->client;
1642 /* Restore initial configuration */
1643 lm90_write_convrate(client, data, data->convrate_orig);
1644 i2c_smbus_write_byte_data(client, LM90_REG_W_CONFIG1,
1648 static int lm90_init_client(struct i2c_client *client, struct lm90_data *data)
1650 int config, convrate;
1652 convrate = lm90_read_reg(client, LM90_REG_R_CONVRATE);
1655 data->convrate_orig = convrate;
1658 * Start the conversions.
1660 config = lm90_read_reg(client, LM90_REG_R_CONFIG1);
1663 data->config_orig = config;
1665 lm90_set_convrate(client, data, 500); /* 500ms; 2Hz conversion rate */
1667 /* Check Temperature Range Select */
1668 if (data->kind == adt7461 || data->kind == tmp451) {
1670 data->flags |= LM90_FLAG_ADT7461_EXT;
1674 * Put MAX6680/MAX8881 into extended resolution (bit 0x10,
1675 * 0.125 degree resolution) and range (0x08, extend range
1676 * to -64 degree) mode for the remote temperature sensor.
1678 if (data->kind == max6680)
1682 * Select external channel 0 for max6695/96
1684 if (data->kind == max6696)
1687 config &= 0xBF; /* run */
1688 if (config != data->config_orig) /* Only write if changed */
1689 i2c_smbus_write_byte_data(client, LM90_REG_W_CONFIG1, config);
1691 return devm_add_action_or_reset(&client->dev, lm90_restore_conf, data);
1694 static bool lm90_is_tripped(struct i2c_client *client, u16 *status)
1696 struct lm90_data *data = i2c_get_clientdata(client);
1699 st = lm90_read_reg(client, LM90_REG_R_STATUS);
1703 if (data->kind == max6696) {
1704 st2 = lm90_read_reg(client, MAX6696_REG_R_STATUS2);
1709 *status = st | (st2 << 8);
1711 if ((st & 0x7f) == 0 && (st2 & 0xfe) == 0)
1714 if ((st & (LM90_STATUS_LLOW | LM90_STATUS_LHIGH | LM90_STATUS_LTHRM)) ||
1715 (st2 & MAX6696_STATUS2_LOT2))
1716 dev_warn(&client->dev,
1717 "temp%d out of range, please check!\n", 1);
1718 if ((st & (LM90_STATUS_RLOW | LM90_STATUS_RHIGH | LM90_STATUS_RTHRM)) ||
1719 (st2 & MAX6696_STATUS2_ROT2))
1720 dev_warn(&client->dev,
1721 "temp%d out of range, please check!\n", 2);
1722 if (st & LM90_STATUS_ROPEN)
1723 dev_warn(&client->dev,
1724 "temp%d diode open, please check!\n", 2);
1725 if (st2 & (MAX6696_STATUS2_R2LOW | MAX6696_STATUS2_R2HIGH |
1726 MAX6696_STATUS2_R2THRM | MAX6696_STATUS2_R2OT2))
1727 dev_warn(&client->dev,
1728 "temp%d out of range, please check!\n", 3);
1729 if (st2 & MAX6696_STATUS2_R2OPEN)
1730 dev_warn(&client->dev,
1731 "temp%d diode open, please check!\n", 3);
1736 static irqreturn_t lm90_irq_thread(int irq, void *dev_id)
1738 struct i2c_client *client = dev_id;
1741 if (lm90_is_tripped(client, &status))
1747 static void lm90_remove_pec(void *dev)
1749 device_remove_file(dev, &dev_attr_pec);
1752 static void lm90_regulator_disable(void *regulator)
1754 regulator_disable(regulator);
1757 static const u32 lm90_chip_config[] = {
1758 HWMON_C_REGISTER_TZ | HWMON_C_UPDATE_INTERVAL | HWMON_C_ALARMS,
1762 static const struct hwmon_channel_info lm90_chip_info = {
1764 .config = lm90_chip_config,
1768 static const struct hwmon_ops lm90_ops = {
1769 .is_visible = lm90_is_visible,
1771 .write = lm90_write,
1774 static int lm90_probe(struct i2c_client *client,
1775 const struct i2c_device_id *id)
1777 struct device *dev = &client->dev;
1778 struct i2c_adapter *adapter = to_i2c_adapter(dev->parent);
1779 struct hwmon_channel_info *info;
1780 struct regulator *regulator;
1781 struct device *hwmon_dev;
1782 struct lm90_data *data;
1785 regulator = devm_regulator_get(dev, "vcc");
1786 if (IS_ERR(regulator))
1787 return PTR_ERR(regulator);
1789 err = regulator_enable(regulator);
1791 dev_err(dev, "Failed to enable regulator: %d\n", err);
1795 err = devm_add_action_or_reset(dev, lm90_regulator_disable, regulator);
1799 data = devm_kzalloc(dev, sizeof(struct lm90_data), GFP_KERNEL);
1803 data->client = client;
1804 i2c_set_clientdata(client, data);
1805 mutex_init(&data->update_lock);
1807 /* Set the device type */
1808 if (client->dev.of_node)
1809 data->kind = (enum chips)of_device_get_match_data(&client->dev);
1811 data->kind = id->driver_data;
1812 if (data->kind == adm1032) {
1813 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE))
1814 client->flags &= ~I2C_CLIENT_PEC;
1818 * Different devices have different alarm bits triggering the
1821 data->alert_alarms = lm90_params[data->kind].alert_alarms;
1823 /* Set chip capabilities */
1824 data->flags = lm90_params[data->kind].flags;
1826 data->chip.ops = &lm90_ops;
1827 data->chip.info = data->info;
1829 data->info[0] = &lm90_chip_info;
1830 data->info[1] = &data->temp_info;
1832 info = &data->temp_info;
1833 info->type = hwmon_temp;
1834 info->config = data->channel_config;
1836 data->channel_config[0] = HWMON_T_INPUT | HWMON_T_MIN | HWMON_T_MAX |
1837 HWMON_T_CRIT | HWMON_T_CRIT_HYST | HWMON_T_MIN_ALARM |
1838 HWMON_T_MAX_ALARM | HWMON_T_CRIT_ALARM;
1839 data->channel_config[1] = HWMON_T_INPUT | HWMON_T_MIN | HWMON_T_MAX |
1840 HWMON_T_CRIT | HWMON_T_CRIT_HYST | HWMON_T_MIN_ALARM |
1841 HWMON_T_MAX_ALARM | HWMON_T_CRIT_ALARM | HWMON_T_FAULT;
1843 if (data->flags & LM90_HAVE_OFFSET)
1844 data->channel_config[1] |= HWMON_T_OFFSET;
1846 if (data->flags & LM90_HAVE_EMERGENCY) {
1847 data->channel_config[0] |= HWMON_T_EMERGENCY |
1848 HWMON_T_EMERGENCY_HYST;
1849 data->channel_config[1] |= HWMON_T_EMERGENCY |
1850 HWMON_T_EMERGENCY_HYST;
1853 if (data->flags & LM90_HAVE_EMERGENCY_ALARM) {
1854 data->channel_config[0] |= HWMON_T_EMERGENCY_ALARM;
1855 data->channel_config[1] |= HWMON_T_EMERGENCY_ALARM;
1858 if (data->flags & LM90_HAVE_TEMP3) {
1859 data->channel_config[2] = HWMON_T_INPUT |
1860 HWMON_T_MIN | HWMON_T_MAX |
1861 HWMON_T_CRIT | HWMON_T_CRIT_HYST |
1862 HWMON_T_EMERGENCY | HWMON_T_EMERGENCY_HYST |
1863 HWMON_T_MIN_ALARM | HWMON_T_MAX_ALARM |
1864 HWMON_T_CRIT_ALARM | HWMON_T_EMERGENCY_ALARM |
1868 data->reg_local_ext = lm90_params[data->kind].reg_local_ext;
1870 /* Set maximum conversion rate */
1871 data->max_convrate = lm90_params[data->kind].max_convrate;
1873 /* Initialize the LM90 chip */
1874 err = lm90_init_client(client, data);
1876 dev_err(dev, "Failed to initialize device\n");
1881 * The 'pec' attribute is attached to the i2c device and thus created
1884 if (client->flags & I2C_CLIENT_PEC) {
1885 err = device_create_file(dev, &dev_attr_pec);
1888 err = devm_add_action_or_reset(dev, lm90_remove_pec, dev);
1893 hwmon_dev = devm_hwmon_device_register_with_info(dev, client->name,
1896 if (IS_ERR(hwmon_dev))
1897 return PTR_ERR(hwmon_dev);
1900 dev_dbg(dev, "IRQ: %d\n", client->irq);
1901 err = devm_request_threaded_irq(dev, client->irq,
1902 NULL, lm90_irq_thread,
1903 IRQF_TRIGGER_LOW | IRQF_ONESHOT,
1906 dev_err(dev, "cannot request IRQ %d\n", client->irq);
1914 static void lm90_alert(struct i2c_client *client, enum i2c_alert_protocol type,
1919 if (type != I2C_PROTOCOL_SMBUS_ALERT)
1922 if (lm90_is_tripped(client, &alarms)) {
1924 * Disable ALERT# output, because these chips don't implement
1925 * SMBus alert correctly; they should only hold the alert line
1928 struct lm90_data *data = i2c_get_clientdata(client);
1930 if ((data->flags & LM90_HAVE_BROKEN_ALERT) &&
1931 (alarms & data->alert_alarms)) {
1934 dev_dbg(&client->dev, "Disabling ALERT#\n");
1935 config = lm90_read_reg(client, LM90_REG_R_CONFIG1);
1937 i2c_smbus_write_byte_data(client,
1942 dev_info(&client->dev, "Everything OK\n");
1946 static struct i2c_driver lm90_driver = {
1947 .class = I2C_CLASS_HWMON,
1950 .of_match_table = of_match_ptr(lm90_of_match),
1952 .probe = lm90_probe,
1953 .alert = lm90_alert,
1954 .id_table = lm90_id,
1955 .detect = lm90_detect,
1956 .address_list = normal_i2c,
1959 module_i2c_driver(lm90_driver);
1961 MODULE_AUTHOR("Jean Delvare <jdelvare@suse.de>");
1962 MODULE_DESCRIPTION("LM90/ADM1032 driver");
1963 MODULE_LICENSE("GPL");