2 * lm90.c - Part of lm_sensors, Linux kernel modules for hardware
4 * Copyright (C) 2003-2010 Jean Delvare <khali@linux-fr.org>
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 ADT7461 chip from Analog Devices.
48 * It's supported in both compatibility and extended mode. It is mostly
49 * compatible with LM90 except for a data format difference for the
50 * temperature value registers.
52 * Since the LM90 was the first chipset supported by this driver, most
53 * comments will refer to this chipset, but are actually general and
54 * concern all supported chipsets, unless mentioned otherwise.
56 * This program is free software; you can redistribute it and/or modify
57 * it under the terms of the GNU General Public License as published by
58 * the Free Software Foundation; either version 2 of the License, or
59 * (at your option) any later version.
61 * This program is distributed in the hope that it will be useful,
62 * but WITHOUT ANY WARRANTY; without even the implied warranty of
63 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
64 * GNU General Public License for more details.
66 * You should have received a copy of the GNU General Public License
67 * along with this program; if not, write to the Free Software
68 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
71 #include <linux/module.h>
72 #include <linux/init.h>
73 #include <linux/slab.h>
74 #include <linux/jiffies.h>
75 #include <linux/i2c.h>
76 #include <linux/hwmon-sysfs.h>
77 #include <linux/hwmon.h>
78 #include <linux/err.h>
79 #include <linux/mutex.h>
80 #include <linux/sysfs.h>
84 * Address is fully defined internally and cannot be changed except for
85 * MAX6659, MAX6680 and MAX6681.
86 * LM86, LM89, LM90, LM99, ADM1032, ADM1032-1, ADT7461, MAX6649, MAX6657
87 * and MAX6658 have address 0x4c.
88 * ADM1032-2, ADT7461-2, LM89-1, LM99-1 and MAX6646 have address 0x4d.
89 * MAX6647 has address 0x4e.
90 * MAX6659 can have address 0x4c, 0x4d or 0x4e.
91 * MAX6680 and MAX6681 can have address 0x18, 0x19, 0x1a, 0x29, 0x2a, 0x2b,
95 static const unsigned short normal_i2c[] = {
96 0x18, 0x19, 0x1a, 0x29, 0x2a, 0x2b, 0x4c, 0x4d, 0x4e, I2C_CLIENT_END };
98 enum chips { lm90, adm1032, lm99, lm86, max6657, max6659, adt7461, max6680,
105 #define LM90_REG_R_MAN_ID 0xFE
106 #define LM90_REG_R_CHIP_ID 0xFF
107 #define LM90_REG_R_CONFIG1 0x03
108 #define LM90_REG_W_CONFIG1 0x09
109 #define LM90_REG_R_CONFIG2 0xBF
110 #define LM90_REG_W_CONFIG2 0xBF
111 #define LM90_REG_R_CONVRATE 0x04
112 #define LM90_REG_W_CONVRATE 0x0A
113 #define LM90_REG_R_STATUS 0x02
114 #define LM90_REG_R_LOCAL_TEMP 0x00
115 #define LM90_REG_R_LOCAL_HIGH 0x05
116 #define LM90_REG_W_LOCAL_HIGH 0x0B
117 #define LM90_REG_R_LOCAL_LOW 0x06
118 #define LM90_REG_W_LOCAL_LOW 0x0C
119 #define LM90_REG_R_LOCAL_CRIT 0x20
120 #define LM90_REG_W_LOCAL_CRIT 0x20
121 #define LM90_REG_R_REMOTE_TEMPH 0x01
122 #define LM90_REG_R_REMOTE_TEMPL 0x10
123 #define LM90_REG_R_REMOTE_OFFSH 0x11
124 #define LM90_REG_W_REMOTE_OFFSH 0x11
125 #define LM90_REG_R_REMOTE_OFFSL 0x12
126 #define LM90_REG_W_REMOTE_OFFSL 0x12
127 #define LM90_REG_R_REMOTE_HIGHH 0x07
128 #define LM90_REG_W_REMOTE_HIGHH 0x0D
129 #define LM90_REG_R_REMOTE_HIGHL 0x13
130 #define LM90_REG_W_REMOTE_HIGHL 0x13
131 #define LM90_REG_R_REMOTE_LOWH 0x08
132 #define LM90_REG_W_REMOTE_LOWH 0x0E
133 #define LM90_REG_R_REMOTE_LOWL 0x14
134 #define LM90_REG_W_REMOTE_LOWL 0x14
135 #define LM90_REG_R_REMOTE_CRIT 0x19
136 #define LM90_REG_W_REMOTE_CRIT 0x19
137 #define LM90_REG_R_TCRIT_HYST 0x21
138 #define LM90_REG_W_TCRIT_HYST 0x21
140 /* MAX6646/6647/6649/6657/6658/6659 registers */
142 #define MAX6657_REG_R_LOCAL_TEMPL 0x11
143 #define MAX6659_REG_R_REMOTE_EMERG 0x16
144 #define MAX6659_REG_W_REMOTE_EMERG 0x16
145 #define MAX6659_REG_R_LOCAL_EMERG 0x17
146 #define MAX6659_REG_W_LOCAL_EMERG 0x17
151 #define LM90_FLAG_ADT7461_EXT (1 << 0) /* ADT7461 extended mode */
152 /* Device features */
153 #define LM90_HAVE_OFFSET (1 << 1) /* temperature offset register */
154 #define LM90_HAVE_LOCAL_EXT (1 << 2) /* extended local temperature */
155 #define LM90_HAVE_REM_LIMIT_EXT (1 << 3) /* extended remote limit */
156 #define LM90_HAVE_EMERGENCY (1 << 4) /* 3rd upper (emergency) limit */
159 * Functions declaration
162 static int lm90_detect(struct i2c_client *client, struct i2c_board_info *info);
163 static int lm90_probe(struct i2c_client *client,
164 const struct i2c_device_id *id);
165 static void lm90_init_client(struct i2c_client *client);
166 static void lm90_alert(struct i2c_client *client, unsigned int flag);
167 static int lm90_remove(struct i2c_client *client);
168 static struct lm90_data *lm90_update_device(struct device *dev);
171 * Driver data (common to all clients)
174 static const struct i2c_device_id lm90_id[] = {
175 { "adm1032", adm1032 },
176 { "adt7461", adt7461 },
181 { "max6646", max6646 },
182 { "max6647", max6646 },
183 { "max6649", max6646 },
184 { "max6657", max6657 },
185 { "max6658", max6657 },
186 { "max6659", max6659 },
187 { "max6680", max6680 },
188 { "max6681", max6680 },
189 { "w83l771", w83l771 },
192 MODULE_DEVICE_TABLE(i2c, lm90_id);
194 static struct i2c_driver lm90_driver = {
195 .class = I2C_CLASS_HWMON,
200 .remove = lm90_remove,
203 .detect = lm90_detect,
204 .address_list = normal_i2c,
208 * Client data (each client gets its own)
212 struct device *hwmon_dev;
213 struct mutex update_lock;
214 char valid; /* zero until following fields are valid */
215 unsigned long last_updated; /* in jiffies */
219 u8 config_orig; /* Original configuration register value */
220 u8 alert_alarms; /* Which alarm bits trigger ALERT# */
222 /* registers values */
223 s8 temp8[6]; /* 0: local low limit
225 2: local critical limit
226 3: remote critical limit
227 4: local emergency limit (max6659 only)
228 5: remote emergency limit (max6659 only) */
229 s16 temp11[5]; /* 0: remote input
232 3: remote offset (except max6646 and max6657/58/59)
235 u8 alarms; /* bitvector */
240 * For local temperatures and limits, critical limits and the hysteresis
241 * value, the LM90 uses signed 8-bit values with LSB = 1 degree Celsius.
242 * For remote temperatures and limits, it uses signed 11-bit values with
243 * LSB = 0.125 degree Celsius, left-justified in 16-bit registers. Some
244 * Maxim chips use unsigned values.
247 static inline int temp_from_s8(s8 val)
252 static inline int temp_from_u8(u8 val)
257 static inline int temp_from_s16(s16 val)
259 return val / 32 * 125;
262 static inline int temp_from_u16(u16 val)
264 return val / 32 * 125;
267 static s8 temp_to_s8(long val)
274 return (val - 500) / 1000;
275 return (val + 500) / 1000;
278 static u8 temp_to_u8(long val)
284 return (val + 500) / 1000;
287 static s16 temp_to_s16(long val)
294 return (val - 62) / 125 * 32;
295 return (val + 62) / 125 * 32;
298 static u8 hyst_to_reg(long val)
304 return (val + 500) / 1000;
308 * ADT7461 in compatibility mode is almost identical to LM90 except that
309 * attempts to write values that are outside the range 0 < temp < 127 are
310 * treated as the boundary value.
312 * ADT7461 in "extended mode" operation uses unsigned integers offset by
313 * 64 (e.g., 0 -> -64 degC). The range is restricted to -64..191 degC.
315 static inline int temp_from_u8_adt7461(struct lm90_data *data, u8 val)
317 if (data->flags & LM90_FLAG_ADT7461_EXT)
318 return (val - 64) * 1000;
320 return temp_from_s8(val);
323 static inline int temp_from_u16_adt7461(struct lm90_data *data, u16 val)
325 if (data->flags & LM90_FLAG_ADT7461_EXT)
326 return (val - 0x4000) / 64 * 250;
328 return temp_from_s16(val);
331 static u8 temp_to_u8_adt7461(struct lm90_data *data, long val)
333 if (data->flags & LM90_FLAG_ADT7461_EXT) {
338 return (val + 500 + 64000) / 1000;
344 return (val + 500) / 1000;
348 static u16 temp_to_u16_adt7461(struct lm90_data *data, long val)
350 if (data->flags & LM90_FLAG_ADT7461_EXT) {
355 return (val + 64000 + 125) / 250 * 64;
361 return (val + 125) / 250 * 64;
369 static ssize_t show_temp8(struct device *dev, struct device_attribute *devattr,
372 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
373 struct lm90_data *data = lm90_update_device(dev);
376 if (data->kind == adt7461)
377 temp = temp_from_u8_adt7461(data, data->temp8[attr->index]);
378 else if (data->kind == max6646)
379 temp = temp_from_u8(data->temp8[attr->index]);
381 temp = temp_from_s8(data->temp8[attr->index]);
383 /* +16 degrees offset for temp2 for the LM99 */
384 if (data->kind == lm99 && attr->index == 3)
387 return sprintf(buf, "%d\n", temp);
390 static ssize_t set_temp8(struct device *dev, struct device_attribute *devattr,
391 const char *buf, size_t count)
393 static const u8 reg[6] = {
394 LM90_REG_W_LOCAL_LOW,
395 LM90_REG_W_LOCAL_HIGH,
396 LM90_REG_W_LOCAL_CRIT,
397 LM90_REG_W_REMOTE_CRIT,
398 MAX6659_REG_W_LOCAL_EMERG,
399 MAX6659_REG_W_REMOTE_EMERG,
402 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
403 struct i2c_client *client = to_i2c_client(dev);
404 struct lm90_data *data = i2c_get_clientdata(client);
405 int nr = attr->index;
409 err = strict_strtol(buf, 10, &val);
413 /* +16 degrees offset for temp2 for the LM99 */
414 if (data->kind == lm99 && attr->index == 3)
417 mutex_lock(&data->update_lock);
418 if (data->kind == adt7461)
419 data->temp8[nr] = temp_to_u8_adt7461(data, val);
420 else if (data->kind == max6646)
421 data->temp8[nr] = temp_to_u8(val);
423 data->temp8[nr] = temp_to_s8(val);
424 i2c_smbus_write_byte_data(client, reg[nr], data->temp8[nr]);
425 mutex_unlock(&data->update_lock);
429 static ssize_t show_temp11(struct device *dev, struct device_attribute *devattr,
432 struct sensor_device_attribute_2 *attr = to_sensor_dev_attr_2(devattr);
433 struct lm90_data *data = lm90_update_device(dev);
436 if (data->kind == adt7461)
437 temp = temp_from_u16_adt7461(data, data->temp11[attr->index]);
438 else if (data->kind == max6646)
439 temp = temp_from_u16(data->temp11[attr->index]);
441 temp = temp_from_s16(data->temp11[attr->index]);
443 /* +16 degrees offset for temp2 for the LM99 */
444 if (data->kind == lm99 && attr->index <= 2)
447 return sprintf(buf, "%d\n", temp);
450 static ssize_t set_temp11(struct device *dev, struct device_attribute *devattr,
451 const char *buf, size_t count)
457 { LM90_REG_W_REMOTE_LOWH, LM90_REG_W_REMOTE_LOWL },
458 { LM90_REG_W_REMOTE_HIGHH, LM90_REG_W_REMOTE_HIGHL },
459 { LM90_REG_W_REMOTE_OFFSH, LM90_REG_W_REMOTE_OFFSL }
462 struct sensor_device_attribute_2 *attr = to_sensor_dev_attr_2(devattr);
463 struct i2c_client *client = to_i2c_client(dev);
464 struct lm90_data *data = i2c_get_clientdata(client);
466 int index = attr->index;
470 err = strict_strtol(buf, 10, &val);
474 /* +16 degrees offset for temp2 for the LM99 */
475 if (data->kind == lm99 && index <= 2)
478 mutex_lock(&data->update_lock);
479 if (data->kind == adt7461)
480 data->temp11[index] = temp_to_u16_adt7461(data, val);
481 else if (data->kind == max6646)
482 data->temp11[index] = temp_to_u8(val) << 8;
483 else if (data->flags & LM90_HAVE_REM_LIMIT_EXT)
484 data->temp11[index] = temp_to_s16(val);
486 data->temp11[index] = temp_to_s8(val) << 8;
488 i2c_smbus_write_byte_data(client, reg[nr].high,
489 data->temp11[index] >> 8);
490 if (data->flags & LM90_HAVE_REM_LIMIT_EXT)
491 i2c_smbus_write_byte_data(client, reg[nr].low,
492 data->temp11[index] & 0xff);
493 mutex_unlock(&data->update_lock);
497 static ssize_t show_temphyst(struct device *dev,
498 struct device_attribute *devattr,
501 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
502 struct lm90_data *data = lm90_update_device(dev);
505 if (data->kind == adt7461)
506 temp = temp_from_u8_adt7461(data, data->temp8[attr->index]);
507 else if (data->kind == max6646)
508 temp = temp_from_u8(data->temp8[attr->index]);
510 temp = temp_from_s8(data->temp8[attr->index]);
512 /* +16 degrees offset for temp2 for the LM99 */
513 if (data->kind == lm99 && attr->index == 3)
516 return sprintf(buf, "%d\n", temp - temp_from_s8(data->temp_hyst));
519 static ssize_t set_temphyst(struct device *dev, struct device_attribute *dummy,
520 const char *buf, size_t count)
522 struct i2c_client *client = to_i2c_client(dev);
523 struct lm90_data *data = i2c_get_clientdata(client);
528 err = strict_strtol(buf, 10, &val);
532 mutex_lock(&data->update_lock);
533 if (data->kind == adt7461)
534 temp = temp_from_u8_adt7461(data, data->temp8[2]);
535 else if (data->kind == max6646)
536 temp = temp_from_u8(data->temp8[2]);
538 temp = temp_from_s8(data->temp8[2]);
540 data->temp_hyst = hyst_to_reg(temp - val);
541 i2c_smbus_write_byte_data(client, LM90_REG_W_TCRIT_HYST,
543 mutex_unlock(&data->update_lock);
547 static ssize_t show_alarms(struct device *dev, struct device_attribute *dummy,
550 struct lm90_data *data = lm90_update_device(dev);
551 return sprintf(buf, "%d\n", data->alarms);
554 static ssize_t show_alarm(struct device *dev, struct device_attribute
557 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
558 struct lm90_data *data = lm90_update_device(dev);
559 int bitnr = attr->index;
561 return sprintf(buf, "%d\n", (data->alarms >> bitnr) & 1);
564 static SENSOR_DEVICE_ATTR_2(temp1_input, S_IRUGO, show_temp11, NULL, 0, 4);
565 static SENSOR_DEVICE_ATTR_2(temp2_input, S_IRUGO, show_temp11, NULL, 0, 0);
566 static SENSOR_DEVICE_ATTR(temp1_min, S_IWUSR | S_IRUGO, show_temp8,
568 static SENSOR_DEVICE_ATTR_2(temp2_min, S_IWUSR | S_IRUGO, show_temp11,
570 static SENSOR_DEVICE_ATTR(temp1_max, S_IWUSR | S_IRUGO, show_temp8,
572 static SENSOR_DEVICE_ATTR_2(temp2_max, S_IWUSR | S_IRUGO, show_temp11,
574 static SENSOR_DEVICE_ATTR(temp1_crit, S_IWUSR | S_IRUGO, show_temp8,
576 static SENSOR_DEVICE_ATTR(temp2_crit, S_IWUSR | S_IRUGO, show_temp8,
578 static SENSOR_DEVICE_ATTR(temp1_crit_hyst, S_IWUSR | S_IRUGO, show_temphyst,
580 static SENSOR_DEVICE_ATTR(temp2_crit_hyst, S_IRUGO, show_temphyst, NULL, 3);
581 static SENSOR_DEVICE_ATTR_2(temp2_offset, S_IWUSR | S_IRUGO, show_temp11,
584 /* Individual alarm files */
585 static SENSOR_DEVICE_ATTR(temp1_crit_alarm, S_IRUGO, show_alarm, NULL, 0);
586 static SENSOR_DEVICE_ATTR(temp2_crit_alarm, S_IRUGO, show_alarm, NULL, 1);
587 static SENSOR_DEVICE_ATTR(temp2_fault, S_IRUGO, show_alarm, NULL, 2);
588 static SENSOR_DEVICE_ATTR(temp2_min_alarm, S_IRUGO, show_alarm, NULL, 3);
589 static SENSOR_DEVICE_ATTR(temp2_max_alarm, S_IRUGO, show_alarm, NULL, 4);
590 static SENSOR_DEVICE_ATTR(temp1_min_alarm, S_IRUGO, show_alarm, NULL, 5);
591 static SENSOR_DEVICE_ATTR(temp1_max_alarm, S_IRUGO, show_alarm, NULL, 6);
592 /* Raw alarm file for compatibility */
593 static DEVICE_ATTR(alarms, S_IRUGO, show_alarms, NULL);
595 static struct attribute *lm90_attributes[] = {
596 &sensor_dev_attr_temp1_input.dev_attr.attr,
597 &sensor_dev_attr_temp2_input.dev_attr.attr,
598 &sensor_dev_attr_temp1_min.dev_attr.attr,
599 &sensor_dev_attr_temp2_min.dev_attr.attr,
600 &sensor_dev_attr_temp1_max.dev_attr.attr,
601 &sensor_dev_attr_temp2_max.dev_attr.attr,
602 &sensor_dev_attr_temp1_crit.dev_attr.attr,
603 &sensor_dev_attr_temp2_crit.dev_attr.attr,
604 &sensor_dev_attr_temp1_crit_hyst.dev_attr.attr,
605 &sensor_dev_attr_temp2_crit_hyst.dev_attr.attr,
607 &sensor_dev_attr_temp1_crit_alarm.dev_attr.attr,
608 &sensor_dev_attr_temp2_crit_alarm.dev_attr.attr,
609 &sensor_dev_attr_temp2_fault.dev_attr.attr,
610 &sensor_dev_attr_temp2_min_alarm.dev_attr.attr,
611 &sensor_dev_attr_temp2_max_alarm.dev_attr.attr,
612 &sensor_dev_attr_temp1_min_alarm.dev_attr.attr,
613 &sensor_dev_attr_temp1_max_alarm.dev_attr.attr,
614 &dev_attr_alarms.attr,
618 static const struct attribute_group lm90_group = {
619 .attrs = lm90_attributes,
623 * Additional attributes for devices with emergency sensors
625 static SENSOR_DEVICE_ATTR(temp1_emergency, S_IWUSR | S_IRUGO, show_temp8,
627 static SENSOR_DEVICE_ATTR(temp2_emergency, S_IWUSR | S_IRUGO, show_temp8,
629 static SENSOR_DEVICE_ATTR(temp1_emergency_hyst, S_IRUGO, show_temphyst,
631 static SENSOR_DEVICE_ATTR(temp2_emergency_hyst, S_IRUGO, show_temphyst,
634 static struct attribute *lm90_emergency_attributes[] = {
635 &sensor_dev_attr_temp1_emergency.dev_attr.attr,
636 &sensor_dev_attr_temp2_emergency.dev_attr.attr,
637 &sensor_dev_attr_temp1_emergency_hyst.dev_attr.attr,
638 &sensor_dev_attr_temp2_emergency_hyst.dev_attr.attr,
642 static const struct attribute_group lm90_emergency_group = {
643 .attrs = lm90_emergency_attributes,
646 /* pec used for ADM1032 only */
647 static ssize_t show_pec(struct device *dev, struct device_attribute *dummy,
650 struct i2c_client *client = to_i2c_client(dev);
651 return sprintf(buf, "%d\n", !!(client->flags & I2C_CLIENT_PEC));
654 static ssize_t set_pec(struct device *dev, struct device_attribute *dummy,
655 const char *buf, size_t count)
657 struct i2c_client *client = to_i2c_client(dev);
661 err = strict_strtol(buf, 10, &val);
667 client->flags &= ~I2C_CLIENT_PEC;
670 client->flags |= I2C_CLIENT_PEC;
679 static DEVICE_ATTR(pec, S_IWUSR | S_IRUGO, show_pec, set_pec);
686 * The ADM1032 supports PEC but not on write byte transactions, so we need
687 * to explicitly ask for a transaction without PEC.
689 static inline s32 adm1032_write_byte(struct i2c_client *client, u8 value)
691 return i2c_smbus_xfer(client->adapter, client->addr,
692 client->flags & ~I2C_CLIENT_PEC,
693 I2C_SMBUS_WRITE, value, I2C_SMBUS_BYTE, NULL);
697 * It is assumed that client->update_lock is held (unless we are in
698 * detection or initialization steps). This matters when PEC is enabled,
699 * because we don't want the address pointer to change between the write
700 * byte and the read byte transactions.
702 static int lm90_read_reg(struct i2c_client *client, u8 reg, u8 *value)
706 if (client->flags & I2C_CLIENT_PEC) {
707 err = adm1032_write_byte(client, reg);
709 err = i2c_smbus_read_byte(client);
711 err = i2c_smbus_read_byte_data(client, reg);
714 dev_warn(&client->dev, "Register %#02x read failed (%d)\n",
723 /* Return 0 if detection is successful, -ENODEV otherwise */
724 static int lm90_detect(struct i2c_client *new_client,
725 struct i2c_board_info *info)
727 struct i2c_adapter *adapter = new_client->adapter;
728 int address = new_client->addr;
729 const char *name = NULL;
730 int man_id, chip_id, reg_config1, reg_convrate;
732 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
735 /* detection and identification */
736 if ((man_id = i2c_smbus_read_byte_data(new_client,
737 LM90_REG_R_MAN_ID)) < 0
738 || (chip_id = i2c_smbus_read_byte_data(new_client,
739 LM90_REG_R_CHIP_ID)) < 0
740 || (reg_config1 = i2c_smbus_read_byte_data(new_client,
741 LM90_REG_R_CONFIG1)) < 0
742 || (reg_convrate = i2c_smbus_read_byte_data(new_client,
743 LM90_REG_R_CONVRATE)) < 0)
746 if ((address == 0x4C || address == 0x4D)
747 && man_id == 0x01) { /* National Semiconductor */
750 reg_config2 = i2c_smbus_read_byte_data(new_client,
755 if ((reg_config1 & 0x2A) == 0x00
756 && (reg_config2 & 0xF8) == 0x00
757 && reg_convrate <= 0x09) {
759 && (chip_id & 0xF0) == 0x20) { /* LM90 */
762 if ((chip_id & 0xF0) == 0x30) { /* LM89/LM99 */
764 dev_info(&adapter->dev,
765 "Assuming LM99 chip at 0x%02x\n",
767 dev_info(&adapter->dev,
768 "If it is an LM89, instantiate it "
769 "with the new_device sysfs "
773 && (chip_id & 0xF0) == 0x10) { /* LM86 */
778 if ((address == 0x4C || address == 0x4D)
779 && man_id == 0x41) { /* Analog Devices */
780 if ((chip_id & 0xF0) == 0x40 /* ADM1032 */
781 && (reg_config1 & 0x3F) == 0x00
782 && reg_convrate <= 0x0A) {
784 /* The ADM1032 supports PEC, but only if combined
785 transactions are not used. */
786 if (i2c_check_functionality(adapter,
787 I2C_FUNC_SMBUS_BYTE))
788 info->flags |= I2C_CLIENT_PEC;
790 if (chip_id == 0x51 /* ADT7461 */
791 && (reg_config1 & 0x1B) == 0x00
792 && reg_convrate <= 0x0A) {
796 if (man_id == 0x4D) { /* Maxim */
798 * The MAX6657, MAX6658 and MAX6659 do NOT have a chip_id
799 * register. Reading from that address will return the last
800 * read value, which in our case is those of the man_id
801 * register. Likewise, the config1 register seems to lack a
802 * low nibble, so the value will be those of the previous
803 * read, so in our case those of the man_id register.
804 * MAX6659 has a third set of upper temperature limit registers.
805 * Those registers also return values on MAX6657 and MAX6658,
806 * thus the only way to detect MAX6659 is by its address.
807 * For this reason it will be mis-detected as MAX6657 if its
810 if (chip_id == man_id
811 && (address == 0x4C || address == 0x4D || address == 0x4E)
812 && (reg_config1 & 0x1F) == (man_id & 0x0F)
813 && reg_convrate <= 0x09) {
820 * The chip_id register of the MAX6680 and MAX6681 holds the
821 * revision of the chip. The lowest bit of the config1 register
822 * is unused and should return zero when read, so should the
823 * second to last bit of config1 (software reset).
826 && (reg_config1 & 0x03) == 0x00
827 && reg_convrate <= 0x07) {
831 * The chip_id register of the MAX6646/6647/6649 holds the
832 * revision of the chip. The lowest 6 bits of the config1
833 * register are unused and should return zero when read.
836 && (reg_config1 & 0x3f) == 0x00
837 && reg_convrate <= 0x07) {
842 && man_id == 0x5C) { /* Winbond/Nuvoton */
843 if ((chip_id & 0xFE) == 0x10 /* W83L771AWG/ASG */
844 && (reg_config1 & 0x2A) == 0x00
845 && reg_convrate <= 0x08) {
850 if (!name) { /* identification failed */
851 dev_dbg(&adapter->dev,
852 "Unsupported chip at 0x%02x (man_id=0x%02X, "
853 "chip_id=0x%02X)\n", address, man_id, chip_id);
857 strlcpy(info->type, name, I2C_NAME_SIZE);
862 static void lm90_remove_files(struct i2c_client *client, struct lm90_data *data)
864 if (data->flags & LM90_HAVE_EMERGENCY)
865 sysfs_remove_group(&client->dev.kobj,
866 &lm90_emergency_group);
867 if (data->flags & LM90_HAVE_OFFSET)
868 device_remove_file(&client->dev,
869 &sensor_dev_attr_temp2_offset.dev_attr);
870 device_remove_file(&client->dev, &dev_attr_pec);
871 sysfs_remove_group(&client->dev.kobj, &lm90_group);
874 static int lm90_probe(struct i2c_client *new_client,
875 const struct i2c_device_id *id)
877 struct i2c_adapter *adapter = to_i2c_adapter(new_client->dev.parent);
878 struct lm90_data *data;
881 data = kzalloc(sizeof(struct lm90_data), GFP_KERNEL);
886 i2c_set_clientdata(new_client, data);
887 mutex_init(&data->update_lock);
889 /* Set the device type */
890 data->kind = id->driver_data;
891 if (data->kind == adm1032) {
892 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE))
893 new_client->flags &= ~I2C_CLIENT_PEC;
896 /* Different devices have different alarm bits triggering the
898 switch (data->kind) {
902 data->alert_alarms = 0x7b;
905 data->alert_alarms = 0x7c;
909 /* Set chip capabilities */
910 if (data->kind != max6657 && data->kind != max6659
911 && data->kind != max6646)
912 data->flags |= LM90_HAVE_OFFSET;
914 if (data->kind == max6657 || data->kind == max6659
915 || data->kind == max6646)
916 data->flags |= LM90_HAVE_LOCAL_EXT;
918 if (data->kind != max6657 && data->kind != max6659
919 && data->kind != max6646 && data->kind != max6680)
920 data->flags |= LM90_HAVE_REM_LIMIT_EXT;
922 if (data->kind == max6659)
923 data->flags |= LM90_HAVE_EMERGENCY;
925 /* Initialize the LM90 chip */
926 lm90_init_client(new_client);
928 /* Register sysfs hooks */
929 err = sysfs_create_group(&new_client->dev.kobj, &lm90_group);
932 if (new_client->flags & I2C_CLIENT_PEC) {
933 err = device_create_file(&new_client->dev, &dev_attr_pec);
935 goto exit_remove_files;
937 if (data->flags & LM90_HAVE_OFFSET) {
938 err = device_create_file(&new_client->dev,
939 &sensor_dev_attr_temp2_offset.dev_attr);
941 goto exit_remove_files;
943 if (data->flags & LM90_HAVE_EMERGENCY) {
944 err = sysfs_create_group(&new_client->dev.kobj,
945 &lm90_emergency_group);
947 goto exit_remove_files;
950 data->hwmon_dev = hwmon_device_register(&new_client->dev);
951 if (IS_ERR(data->hwmon_dev)) {
952 err = PTR_ERR(data->hwmon_dev);
953 goto exit_remove_files;
959 lm90_remove_files(new_client, data);
966 static void lm90_init_client(struct i2c_client *client)
969 struct lm90_data *data = i2c_get_clientdata(client);
972 * Start the conversions.
974 i2c_smbus_write_byte_data(client, LM90_REG_W_CONVRATE,
976 if (lm90_read_reg(client, LM90_REG_R_CONFIG1, &config) < 0) {
977 dev_warn(&client->dev, "Initialization failed!\n");
980 data->config_orig = config;
982 /* Check Temperature Range Select */
983 if (data->kind == adt7461) {
985 data->flags |= LM90_FLAG_ADT7461_EXT;
989 * Put MAX6680/MAX8881 into extended resolution (bit 0x10,
990 * 0.125 degree resolution) and range (0x08, extend range
991 * to -64 degree) mode for the remote temperature sensor.
993 if (data->kind == max6680)
996 config &= 0xBF; /* run */
997 if (config != data->config_orig) /* Only write if changed */
998 i2c_smbus_write_byte_data(client, LM90_REG_W_CONFIG1, config);
1001 static int lm90_remove(struct i2c_client *client)
1003 struct lm90_data *data = i2c_get_clientdata(client);
1005 hwmon_device_unregister(data->hwmon_dev);
1006 lm90_remove_files(client, data);
1008 /* Restore initial configuration */
1009 i2c_smbus_write_byte_data(client, LM90_REG_W_CONFIG1,
1016 static void lm90_alert(struct i2c_client *client, unsigned int flag)
1018 struct lm90_data *data = i2c_get_clientdata(client);
1021 lm90_read_reg(client, LM90_REG_R_STATUS, &alarms);
1022 if ((alarms & 0x7f) == 0) {
1023 dev_info(&client->dev, "Everything OK\n");
1026 dev_warn(&client->dev,
1027 "temp%d out of range, please check!\n", 1);
1029 dev_warn(&client->dev,
1030 "temp%d out of range, please check!\n", 2);
1032 dev_warn(&client->dev,
1033 "temp%d diode open, please check!\n", 2);
1035 /* Disable ALERT# output, because these chips don't implement
1036 SMBus alert correctly; they should only hold the alert line
1038 if ((data->kind == adm1032 || data->kind == adt7461)
1039 && (alarms & data->alert_alarms)) {
1040 dev_dbg(&client->dev, "Disabling ALERT#\n");
1041 lm90_read_reg(client, LM90_REG_R_CONFIG1, &config);
1042 i2c_smbus_write_byte_data(client, LM90_REG_W_CONFIG1,
1048 static int lm90_read16(struct i2c_client *client, u8 regh, u8 regl, u16 *value)
1054 * There is a trick here. We have to read two registers to have the
1055 * sensor temperature, but we have to beware a conversion could occur
1056 * inbetween the readings. The datasheet says we should either use
1057 * the one-shot conversion register, which we don't want to do
1058 * (disables hardware monitoring) or monitor the busy bit, which is
1059 * impossible (we can't read the values and monitor that bit at the
1060 * exact same time). So the solution used here is to read the high
1061 * byte once, then the low byte, then the high byte again. If the new
1062 * high byte matches the old one, then we have a valid reading. Else
1063 * we have to read the low byte again, and now we believe we have a
1066 if ((err = lm90_read_reg(client, regh, &oldh))
1067 || (err = lm90_read_reg(client, regl, &l))
1068 || (err = lm90_read_reg(client, regh, &newh)))
1071 err = lm90_read_reg(client, regl, &l);
1075 *value = (newh << 8) | l;
1080 static struct lm90_data *lm90_update_device(struct device *dev)
1082 struct i2c_client *client = to_i2c_client(dev);
1083 struct lm90_data *data = i2c_get_clientdata(client);
1085 mutex_lock(&data->update_lock);
1087 if (time_after(jiffies, data->last_updated + HZ / 2 + HZ / 10)
1091 dev_dbg(&client->dev, "Updating lm90 data.\n");
1092 lm90_read_reg(client, LM90_REG_R_LOCAL_LOW, &data->temp8[0]);
1093 lm90_read_reg(client, LM90_REG_R_LOCAL_HIGH, &data->temp8[1]);
1094 lm90_read_reg(client, LM90_REG_R_LOCAL_CRIT, &data->temp8[2]);
1095 lm90_read_reg(client, LM90_REG_R_REMOTE_CRIT, &data->temp8[3]);
1096 lm90_read_reg(client, LM90_REG_R_TCRIT_HYST, &data->temp_hyst);
1098 if (data->flags & LM90_HAVE_LOCAL_EXT) {
1099 lm90_read16(client, LM90_REG_R_LOCAL_TEMP,
1100 MAX6657_REG_R_LOCAL_TEMPL,
1103 if (lm90_read_reg(client, LM90_REG_R_LOCAL_TEMP,
1105 data->temp11[4] = h << 8;
1107 lm90_read16(client, LM90_REG_R_REMOTE_TEMPH,
1108 LM90_REG_R_REMOTE_TEMPL, &data->temp11[0]);
1110 if (lm90_read_reg(client, LM90_REG_R_REMOTE_LOWH, &h) == 0) {
1111 data->temp11[1] = h << 8;
1112 if ((data->flags & LM90_HAVE_REM_LIMIT_EXT)
1113 && lm90_read_reg(client, LM90_REG_R_REMOTE_LOWL,
1115 data->temp11[1] |= l;
1117 if (lm90_read_reg(client, LM90_REG_R_REMOTE_HIGHH, &h) == 0) {
1118 data->temp11[2] = h << 8;
1119 if ((data->flags & LM90_HAVE_REM_LIMIT_EXT)
1120 && lm90_read_reg(client, LM90_REG_R_REMOTE_HIGHL,
1122 data->temp11[2] |= l;
1125 if (data->flags & LM90_HAVE_OFFSET) {
1126 if (lm90_read_reg(client, LM90_REG_R_REMOTE_OFFSH,
1128 && lm90_read_reg(client, LM90_REG_R_REMOTE_OFFSL,
1130 data->temp11[3] = (h << 8) | l;
1132 if (data->flags & LM90_HAVE_EMERGENCY) {
1133 lm90_read_reg(client, MAX6659_REG_R_LOCAL_EMERG,
1135 lm90_read_reg(client, MAX6659_REG_R_REMOTE_EMERG,
1138 lm90_read_reg(client, LM90_REG_R_STATUS, &data->alarms);
1140 /* Re-enable ALERT# output if it was originally enabled and
1141 * relevant alarms are all clear */
1142 if ((data->config_orig & 0x80) == 0
1143 && (data->alarms & data->alert_alarms) == 0) {
1146 lm90_read_reg(client, LM90_REG_R_CONFIG1, &config);
1147 if (config & 0x80) {
1148 dev_dbg(&client->dev, "Re-enabling ALERT#\n");
1149 i2c_smbus_write_byte_data(client,
1155 data->last_updated = jiffies;
1159 mutex_unlock(&data->update_lock);
1164 static int __init sensors_lm90_init(void)
1166 return i2c_add_driver(&lm90_driver);
1169 static void __exit sensors_lm90_exit(void)
1171 i2c_del_driver(&lm90_driver);
1174 MODULE_AUTHOR("Jean Delvare <khali@linux-fr.org>");
1175 MODULE_DESCRIPTION("LM90/ADM1032 driver");
1176 MODULE_LICENSE("GPL");
1178 module_init(sensors_lm90_init);
1179 module_exit(sensors_lm90_exit);