2 * lm90.c - Part of lm_sensors, Linux kernel modules for hardware
4 * Copyright (C) 2003-2008 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. The
16 * driver doesn't handle it since it can be done easily in user-space.
17 * Note that there is no way to differentiate between both chips.
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. The extra address and features of the MAX6659 are not
32 * supported by this driver. These chips lack the remote temperature
35 * This driver also supports the MAX6680 and MAX6681, two other sensor
36 * chips made by Maxim. These are quite similar to the other Maxim
37 * chips. The MAX6680 and MAX6681 only differ in the pinout so they can
38 * be treated identically.
40 * This driver also supports the ADT7461 chip from Analog Devices.
41 * It's supported in both compatibility and extended mode. It is mostly
42 * compatible with LM90 except for a data format difference for the
43 * temperature value registers.
45 * Since the LM90 was the first chipset supported by this driver, most
46 * comments will refer to this chipset, but are actually general and
47 * concern all supported chipsets, unless mentioned otherwise.
49 * This program is free software; you can redistribute it and/or modify
50 * it under the terms of the GNU General Public License as published by
51 * the Free Software Foundation; either version 2 of the License, or
52 * (at your option) any later version.
54 * This program is distributed in the hope that it will be useful,
55 * but WITHOUT ANY WARRANTY; without even the implied warranty of
56 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
57 * GNU General Public License for more details.
59 * You should have received a copy of the GNU General Public License
60 * along with this program; if not, write to the Free Software
61 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
64 #include <linux/module.h>
65 #include <linux/init.h>
66 #include <linux/slab.h>
67 #include <linux/jiffies.h>
68 #include <linux/i2c.h>
69 #include <linux/hwmon-sysfs.h>
70 #include <linux/hwmon.h>
71 #include <linux/err.h>
72 #include <linux/mutex.h>
73 #include <linux/sysfs.h>
77 * Address is fully defined internally and cannot be changed except for
78 * MAX6659, MAX6680 and MAX6681.
79 * LM86, LM89, LM90, LM99, ADM1032, ADM1032-1, ADT7461, MAX6657 and MAX6658
81 * ADM1032-2, ADT7461-2, LM89-1, and LM99-1 have address 0x4d.
82 * MAX6659 can have address 0x4c, 0x4d or 0x4e (unsupported).
83 * MAX6680 and MAX6681 can have address 0x18, 0x19, 0x1a, 0x29, 0x2a, 0x2b,
87 static const unsigned short normal_i2c[] = {
88 0x18, 0x19, 0x1a, 0x29, 0x2a, 0x2b, 0x4c, 0x4d, 0x4e, I2C_CLIENT_END };
94 I2C_CLIENT_INSMOD_7(lm90, adm1032, lm99, lm86, max6657, adt7461, max6680);
100 #define LM90_REG_R_MAN_ID 0xFE
101 #define LM90_REG_R_CHIP_ID 0xFF
102 #define LM90_REG_R_CONFIG1 0x03
103 #define LM90_REG_W_CONFIG1 0x09
104 #define LM90_REG_R_CONFIG2 0xBF
105 #define LM90_REG_W_CONFIG2 0xBF
106 #define LM90_REG_R_CONVRATE 0x04
107 #define LM90_REG_W_CONVRATE 0x0A
108 #define LM90_REG_R_STATUS 0x02
109 #define LM90_REG_R_LOCAL_TEMP 0x00
110 #define LM90_REG_R_LOCAL_HIGH 0x05
111 #define LM90_REG_W_LOCAL_HIGH 0x0B
112 #define LM90_REG_R_LOCAL_LOW 0x06
113 #define LM90_REG_W_LOCAL_LOW 0x0C
114 #define LM90_REG_R_LOCAL_CRIT 0x20
115 #define LM90_REG_W_LOCAL_CRIT 0x20
116 #define LM90_REG_R_REMOTE_TEMPH 0x01
117 #define LM90_REG_R_REMOTE_TEMPL 0x10
118 #define LM90_REG_R_REMOTE_OFFSH 0x11
119 #define LM90_REG_W_REMOTE_OFFSH 0x11
120 #define LM90_REG_R_REMOTE_OFFSL 0x12
121 #define LM90_REG_W_REMOTE_OFFSL 0x12
122 #define LM90_REG_R_REMOTE_HIGHH 0x07
123 #define LM90_REG_W_REMOTE_HIGHH 0x0D
124 #define LM90_REG_R_REMOTE_HIGHL 0x13
125 #define LM90_REG_W_REMOTE_HIGHL 0x13
126 #define LM90_REG_R_REMOTE_LOWH 0x08
127 #define LM90_REG_W_REMOTE_LOWH 0x0E
128 #define LM90_REG_R_REMOTE_LOWL 0x14
129 #define LM90_REG_W_REMOTE_LOWL 0x14
130 #define LM90_REG_R_REMOTE_CRIT 0x19
131 #define LM90_REG_W_REMOTE_CRIT 0x19
132 #define LM90_REG_R_TCRIT_HYST 0x21
133 #define LM90_REG_W_TCRIT_HYST 0x21
135 /* MAX6657-specific registers */
137 #define MAX6657_REG_R_LOCAL_TEMPL 0x11
142 #define LM90_FLAG_ADT7461_EXT 0x01 /* ADT7461 extended mode */
145 * Functions declaration
148 static int lm90_detect(struct i2c_client *client, int kind,
149 struct i2c_board_info *info);
150 static int lm90_probe(struct i2c_client *client,
151 const struct i2c_device_id *id);
152 static void lm90_init_client(struct i2c_client *client);
153 static int lm90_remove(struct i2c_client *client);
154 static struct lm90_data *lm90_update_device(struct device *dev);
157 * Driver data (common to all clients)
160 static const struct i2c_device_id lm90_id[] = {
161 { "adm1032", adm1032 },
162 { "adt7461", adt7461 },
166 { "lm99", lm99 }, /* Missing temperature offset */
167 { "max6657", max6657 },
168 { "max6658", max6657 },
169 { "max6659", max6657 },
170 { "max6680", max6680 },
171 { "max6681", max6680 },
174 MODULE_DEVICE_TABLE(i2c, lm90_id);
176 static struct i2c_driver lm90_driver = {
177 .class = I2C_CLASS_HWMON,
182 .remove = lm90_remove,
184 .detect = lm90_detect,
185 .address_data = &addr_data,
189 * Client data (each client gets its own)
193 struct device *hwmon_dev;
194 struct mutex update_lock;
195 char valid; /* zero until following fields are valid */
196 unsigned long last_updated; /* in jiffies */
200 /* registers values */
201 s8 temp8[4]; /* 0: local low limit
203 2: local critical limit
204 3: remote critical limit */
205 s16 temp11[5]; /* 0: remote input
208 3: remote offset (except max6657)
211 u8 alarms; /* bitvector */
216 * For local temperatures and limits, critical limits and the hysteresis
217 * value, the LM90 uses signed 8-bit values with LSB = 1 degree Celsius.
218 * For remote temperatures and limits, it uses signed 11-bit values with
219 * LSB = 0.125 degree Celsius, left-justified in 16-bit registers.
222 static inline int temp1_from_reg(s8 val)
227 static inline int temp2_from_reg(s16 val)
229 return val / 32 * 125;
232 static s8 temp1_to_reg(long val)
239 return (val - 500) / 1000;
240 return (val + 500) / 1000;
243 static s16 temp2_to_reg(long val)
250 return (val - 62) / 125 * 32;
251 return (val + 62) / 125 * 32;
254 static u8 hyst_to_reg(long val)
260 return (val + 500) / 1000;
264 * ADT7461 in compatibility mode is almost identical to LM90 except that
265 * attempts to write values that are outside the range 0 < temp < 127 are
266 * treated as the boundary value.
268 * ADT7461 in "extended mode" operation uses unsigned integers offset by
269 * 64 (e.g., 0 -> -64 degC). The range is restricted to -64..191 degC.
271 static inline int temp1_from_reg_adt7461(struct lm90_data *data, u8 val)
273 if (data->flags & LM90_FLAG_ADT7461_EXT)
274 return (val - 64) * 1000;
276 return temp1_from_reg(val);
279 static inline int temp2_from_reg_adt7461(struct lm90_data *data, u16 val)
281 if (data->flags & LM90_FLAG_ADT7461_EXT)
282 return (val - 0x4000) / 64 * 250;
284 return temp2_from_reg(val);
287 static u8 temp1_to_reg_adt7461(struct lm90_data *data, long val)
289 if (data->flags & LM90_FLAG_ADT7461_EXT) {
294 return (val + 500 + 64000) / 1000;
300 return (val + 500) / 1000;
304 static u16 temp2_to_reg_adt7461(struct lm90_data *data, long val)
306 if (data->flags & LM90_FLAG_ADT7461_EXT) {
311 return (val + 64000 + 125) / 250 * 64;
317 return (val + 125) / 250 * 64;
325 static ssize_t show_temp8(struct device *dev, struct device_attribute *devattr,
328 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
329 struct lm90_data *data = lm90_update_device(dev);
332 if (data->kind == adt7461)
333 temp = temp1_from_reg_adt7461(data, data->temp8[attr->index]);
335 temp = temp1_from_reg(data->temp8[attr->index]);
337 return sprintf(buf, "%d\n", temp);
340 static ssize_t set_temp8(struct device *dev, struct device_attribute *devattr,
341 const char *buf, size_t count)
343 static const u8 reg[4] = {
344 LM90_REG_W_LOCAL_LOW,
345 LM90_REG_W_LOCAL_HIGH,
346 LM90_REG_W_LOCAL_CRIT,
347 LM90_REG_W_REMOTE_CRIT,
350 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
351 struct i2c_client *client = to_i2c_client(dev);
352 struct lm90_data *data = i2c_get_clientdata(client);
353 long val = simple_strtol(buf, NULL, 10);
354 int nr = attr->index;
356 mutex_lock(&data->update_lock);
357 if (data->kind == adt7461)
358 data->temp8[nr] = temp1_to_reg_adt7461(data, val);
360 data->temp8[nr] = temp1_to_reg(val);
361 i2c_smbus_write_byte_data(client, reg[nr], data->temp8[nr]);
362 mutex_unlock(&data->update_lock);
366 static ssize_t show_temp11(struct device *dev, struct device_attribute *devattr,
369 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
370 struct lm90_data *data = lm90_update_device(dev);
373 if (data->kind == adt7461)
374 temp = temp2_from_reg_adt7461(data, data->temp11[attr->index]);
376 temp = temp2_from_reg(data->temp11[attr->index]);
378 return sprintf(buf, "%d\n", temp);
381 static ssize_t set_temp11(struct device *dev, struct device_attribute *devattr,
382 const char *buf, size_t count)
384 static const u8 reg[6] = {
385 LM90_REG_W_REMOTE_LOWH,
386 LM90_REG_W_REMOTE_LOWL,
387 LM90_REG_W_REMOTE_HIGHH,
388 LM90_REG_W_REMOTE_HIGHL,
389 LM90_REG_W_REMOTE_OFFSH,
390 LM90_REG_W_REMOTE_OFFSL,
393 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
394 struct i2c_client *client = to_i2c_client(dev);
395 struct lm90_data *data = i2c_get_clientdata(client);
396 long val = simple_strtol(buf, NULL, 10);
397 int nr = attr->index;
399 mutex_lock(&data->update_lock);
400 if (data->kind == adt7461)
401 data->temp11[nr] = temp2_to_reg_adt7461(data, val);
402 else if (data->kind == max6657 || data->kind == max6680)
403 data->temp11[nr] = temp1_to_reg(val) << 8;
405 data->temp11[nr] = temp2_to_reg(val);
407 i2c_smbus_write_byte_data(client, reg[(nr - 1) * 2],
408 data->temp11[nr] >> 8);
409 if (data->kind != max6657 && data->kind != max6680)
410 i2c_smbus_write_byte_data(client, reg[(nr - 1) * 2 + 1],
411 data->temp11[nr] & 0xff);
412 mutex_unlock(&data->update_lock);
416 static ssize_t show_temphyst(struct device *dev, struct device_attribute *devattr,
419 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
420 struct lm90_data *data = lm90_update_device(dev);
423 if (data->kind == adt7461)
424 temp = temp1_from_reg_adt7461(data, data->temp8[attr->index]);
426 temp = temp1_from_reg(data->temp8[attr->index]);
428 return sprintf(buf, "%d\n", temp - temp1_from_reg(data->temp_hyst));
431 static ssize_t set_temphyst(struct device *dev, struct device_attribute *dummy,
432 const char *buf, size_t count)
434 struct i2c_client *client = to_i2c_client(dev);
435 struct lm90_data *data = i2c_get_clientdata(client);
436 long val = simple_strtol(buf, NULL, 10);
439 mutex_lock(&data->update_lock);
440 hyst = temp1_from_reg(data->temp8[2]) - val;
441 i2c_smbus_write_byte_data(client, LM90_REG_W_TCRIT_HYST,
443 mutex_unlock(&data->update_lock);
447 static ssize_t show_alarms(struct device *dev, struct device_attribute *dummy,
450 struct lm90_data *data = lm90_update_device(dev);
451 return sprintf(buf, "%d\n", data->alarms);
454 static ssize_t show_alarm(struct device *dev, struct device_attribute
457 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
458 struct lm90_data *data = lm90_update_device(dev);
459 int bitnr = attr->index;
461 return sprintf(buf, "%d\n", (data->alarms >> bitnr) & 1);
464 static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, show_temp11, NULL, 4);
465 static SENSOR_DEVICE_ATTR(temp2_input, S_IRUGO, show_temp11, NULL, 0);
466 static SENSOR_DEVICE_ATTR(temp1_min, S_IWUSR | S_IRUGO, show_temp8,
468 static SENSOR_DEVICE_ATTR(temp2_min, S_IWUSR | S_IRUGO, show_temp11,
470 static SENSOR_DEVICE_ATTR(temp1_max, S_IWUSR | S_IRUGO, show_temp8,
472 static SENSOR_DEVICE_ATTR(temp2_max, S_IWUSR | S_IRUGO, show_temp11,
474 static SENSOR_DEVICE_ATTR(temp1_crit, S_IWUSR | S_IRUGO, show_temp8,
476 static SENSOR_DEVICE_ATTR(temp2_crit, S_IWUSR | S_IRUGO, show_temp8,
478 static SENSOR_DEVICE_ATTR(temp1_crit_hyst, S_IWUSR | S_IRUGO, show_temphyst,
480 static SENSOR_DEVICE_ATTR(temp2_crit_hyst, S_IRUGO, show_temphyst, NULL, 3);
481 static SENSOR_DEVICE_ATTR(temp2_offset, S_IWUSR | S_IRUGO, show_temp11,
484 /* Individual alarm files */
485 static SENSOR_DEVICE_ATTR(temp1_crit_alarm, S_IRUGO, show_alarm, NULL, 0);
486 static SENSOR_DEVICE_ATTR(temp2_crit_alarm, S_IRUGO, show_alarm, NULL, 1);
487 static SENSOR_DEVICE_ATTR(temp2_fault, S_IRUGO, show_alarm, NULL, 2);
488 static SENSOR_DEVICE_ATTR(temp2_min_alarm, S_IRUGO, show_alarm, NULL, 3);
489 static SENSOR_DEVICE_ATTR(temp2_max_alarm, S_IRUGO, show_alarm, NULL, 4);
490 static SENSOR_DEVICE_ATTR(temp1_min_alarm, S_IRUGO, show_alarm, NULL, 5);
491 static SENSOR_DEVICE_ATTR(temp1_max_alarm, S_IRUGO, show_alarm, NULL, 6);
492 /* Raw alarm file for compatibility */
493 static DEVICE_ATTR(alarms, S_IRUGO, show_alarms, NULL);
495 static struct attribute *lm90_attributes[] = {
496 &sensor_dev_attr_temp1_input.dev_attr.attr,
497 &sensor_dev_attr_temp2_input.dev_attr.attr,
498 &sensor_dev_attr_temp1_min.dev_attr.attr,
499 &sensor_dev_attr_temp2_min.dev_attr.attr,
500 &sensor_dev_attr_temp1_max.dev_attr.attr,
501 &sensor_dev_attr_temp2_max.dev_attr.attr,
502 &sensor_dev_attr_temp1_crit.dev_attr.attr,
503 &sensor_dev_attr_temp2_crit.dev_attr.attr,
504 &sensor_dev_attr_temp1_crit_hyst.dev_attr.attr,
505 &sensor_dev_attr_temp2_crit_hyst.dev_attr.attr,
507 &sensor_dev_attr_temp1_crit_alarm.dev_attr.attr,
508 &sensor_dev_attr_temp2_crit_alarm.dev_attr.attr,
509 &sensor_dev_attr_temp2_fault.dev_attr.attr,
510 &sensor_dev_attr_temp2_min_alarm.dev_attr.attr,
511 &sensor_dev_attr_temp2_max_alarm.dev_attr.attr,
512 &sensor_dev_attr_temp1_min_alarm.dev_attr.attr,
513 &sensor_dev_attr_temp1_max_alarm.dev_attr.attr,
514 &dev_attr_alarms.attr,
518 static const struct attribute_group lm90_group = {
519 .attrs = lm90_attributes,
522 /* pec used for ADM1032 only */
523 static ssize_t show_pec(struct device *dev, struct device_attribute *dummy,
526 struct i2c_client *client = to_i2c_client(dev);
527 return sprintf(buf, "%d\n", !!(client->flags & I2C_CLIENT_PEC));
530 static ssize_t set_pec(struct device *dev, struct device_attribute *dummy,
531 const char *buf, size_t count)
533 struct i2c_client *client = to_i2c_client(dev);
534 long val = simple_strtol(buf, NULL, 10);
538 client->flags &= ~I2C_CLIENT_PEC;
541 client->flags |= I2C_CLIENT_PEC;
550 static DEVICE_ATTR(pec, S_IWUSR | S_IRUGO, show_pec, set_pec);
556 /* The ADM1032 supports PEC but not on write byte transactions, so we need
557 to explicitly ask for a transaction without PEC. */
558 static inline s32 adm1032_write_byte(struct i2c_client *client, u8 value)
560 return i2c_smbus_xfer(client->adapter, client->addr,
561 client->flags & ~I2C_CLIENT_PEC,
562 I2C_SMBUS_WRITE, value, I2C_SMBUS_BYTE, NULL);
565 /* It is assumed that client->update_lock is held (unless we are in
566 detection or initialization steps). This matters when PEC is enabled,
567 because we don't want the address pointer to change between the write
568 byte and the read byte transactions. */
569 static int lm90_read_reg(struct i2c_client* client, u8 reg, u8 *value)
573 if (client->flags & I2C_CLIENT_PEC) {
574 err = adm1032_write_byte(client, reg);
576 err = i2c_smbus_read_byte(client);
578 err = i2c_smbus_read_byte_data(client, reg);
581 dev_warn(&client->dev, "Register %#02x read failed (%d)\n",
590 /* Return 0 if detection is successful, -ENODEV otherwise */
591 static int lm90_detect(struct i2c_client *new_client, int kind,
592 struct i2c_board_info *info)
594 struct i2c_adapter *adapter = new_client->adapter;
595 int address = new_client->addr;
596 const char *name = "";
598 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
602 * Now we do the remaining detection. A negative kind means that
603 * the driver was loaded with no force parameter (default), so we
604 * must both detect and identify the chip. A zero kind means that
605 * the driver was loaded with the force parameter, the detection
606 * step shall be skipped. A positive kind means that the driver
607 * was loaded with the force parameter and a given kind of chip is
608 * requested, so both the detection and the identification steps
612 /* Default to an LM90 if forced */
616 if (kind < 0) { /* detection and identification */
617 int man_id, chip_id, reg_config1, reg_convrate;
619 if ((man_id = i2c_smbus_read_byte_data(new_client,
620 LM90_REG_R_MAN_ID)) < 0
621 || (chip_id = i2c_smbus_read_byte_data(new_client,
622 LM90_REG_R_CHIP_ID)) < 0
623 || (reg_config1 = i2c_smbus_read_byte_data(new_client,
624 LM90_REG_R_CONFIG1)) < 0
625 || (reg_convrate = i2c_smbus_read_byte_data(new_client,
626 LM90_REG_R_CONVRATE)) < 0)
629 if ((address == 0x4C || address == 0x4D)
630 && man_id == 0x01) { /* National Semiconductor */
633 if ((reg_config2 = i2c_smbus_read_byte_data(new_client,
634 LM90_REG_R_CONFIG2)) < 0)
637 if ((reg_config1 & 0x2A) == 0x00
638 && (reg_config2 & 0xF8) == 0x00
639 && reg_convrate <= 0x09) {
641 && (chip_id & 0xF0) == 0x20) { /* LM90 */
644 if ((chip_id & 0xF0) == 0x30) { /* LM89/LM99 */
648 && (chip_id & 0xF0) == 0x10) { /* LM86 */
653 if ((address == 0x4C || address == 0x4D)
654 && man_id == 0x41) { /* Analog Devices */
655 if ((chip_id & 0xF0) == 0x40 /* ADM1032 */
656 && (reg_config1 & 0x3F) == 0x00
657 && reg_convrate <= 0x0A) {
660 if (chip_id == 0x51 /* ADT7461 */
661 && (reg_config1 & 0x1B) == 0x00
662 && reg_convrate <= 0x0A) {
666 if (man_id == 0x4D) { /* Maxim */
668 * The MAX6657, MAX6658 and MAX6659 do NOT have a
669 * chip_id register. Reading from that address will
670 * return the last read value, which in our case is
671 * those of the man_id register. Likewise, the config1
672 * register seems to lack a low nibble, so the value
673 * will be those of the previous read, so in our case
674 * those of the man_id register.
676 if (chip_id == man_id
677 && (address == 0x4C || address == 0x4D)
678 && (reg_config1 & 0x1F) == (man_id & 0x0F)
679 && reg_convrate <= 0x09) {
682 /* The chip_id register of the MAX6680 and MAX6681
683 * holds the revision of the chip.
684 * the lowest bit of the config1 register is unused
685 * and should return zero when read, so should the
686 * second to last bit of config1 (software reset)
689 && (reg_config1 & 0x03) == 0x00
690 && reg_convrate <= 0x07) {
695 if (kind <= 0) { /* identification failed */
696 dev_info(&adapter->dev,
697 "Unsupported chip (man_id=0x%02X, "
698 "chip_id=0x%02X).\n", man_id, chip_id);
703 /* Fill the i2c board info */
706 } else if (kind == adm1032) {
708 /* The ADM1032 supports PEC, but only if combined
709 transactions are not used. */
710 if (i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE))
711 info->flags |= I2C_CLIENT_PEC;
712 } else if (kind == lm99) {
714 } else if (kind == lm86) {
716 } else if (kind == max6657) {
718 } else if (kind == max6680) {
720 } else if (kind == adt7461) {
723 strlcpy(info->type, name, I2C_NAME_SIZE);
728 static int lm90_probe(struct i2c_client *new_client,
729 const struct i2c_device_id *id)
731 struct i2c_adapter *adapter = to_i2c_adapter(new_client->dev.parent);
732 struct lm90_data *data;
735 data = kzalloc(sizeof(struct lm90_data), GFP_KERNEL);
740 i2c_set_clientdata(new_client, data);
741 mutex_init(&data->update_lock);
743 /* Set the device type */
744 data->kind = id->driver_data;
745 if (data->kind == adm1032) {
746 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE))
747 new_client->flags &= ~I2C_CLIENT_PEC;
750 /* Initialize the LM90 chip */
751 lm90_init_client(new_client);
753 /* Register sysfs hooks */
754 if ((err = sysfs_create_group(&new_client->dev.kobj, &lm90_group)))
756 if (new_client->flags & I2C_CLIENT_PEC) {
757 if ((err = device_create_file(&new_client->dev,
759 goto exit_remove_files;
761 if (data->kind != max6657) {
762 if ((err = device_create_file(&new_client->dev,
763 &sensor_dev_attr_temp2_offset.dev_attr)))
764 goto exit_remove_files;
767 data->hwmon_dev = hwmon_device_register(&new_client->dev);
768 if (IS_ERR(data->hwmon_dev)) {
769 err = PTR_ERR(data->hwmon_dev);
770 goto exit_remove_files;
776 sysfs_remove_group(&new_client->dev.kobj, &lm90_group);
777 device_remove_file(&new_client->dev, &dev_attr_pec);
784 static void lm90_init_client(struct i2c_client *client)
786 u8 config, config_orig;
787 struct lm90_data *data = i2c_get_clientdata(client);
790 * Start the conversions.
792 i2c_smbus_write_byte_data(client, LM90_REG_W_CONVRATE,
794 if (lm90_read_reg(client, LM90_REG_R_CONFIG1, &config) < 0) {
795 dev_warn(&client->dev, "Initialization failed!\n");
798 config_orig = config;
800 /* Check Temperature Range Select */
801 if (data->kind == adt7461) {
803 data->flags |= LM90_FLAG_ADT7461_EXT;
807 * Put MAX6680/MAX8881 into extended resolution (bit 0x10,
808 * 0.125 degree resolution) and range (0x08, extend range
809 * to -64 degree) mode for the remote temperature sensor.
811 if (data->kind == max6680) {
815 config &= 0xBF; /* run */
816 if (config != config_orig) /* Only write if changed */
817 i2c_smbus_write_byte_data(client, LM90_REG_W_CONFIG1, config);
820 static int lm90_remove(struct i2c_client *client)
822 struct lm90_data *data = i2c_get_clientdata(client);
824 hwmon_device_unregister(data->hwmon_dev);
825 sysfs_remove_group(&client->dev.kobj, &lm90_group);
826 device_remove_file(&client->dev, &dev_attr_pec);
827 if (data->kind != max6657)
828 device_remove_file(&client->dev,
829 &sensor_dev_attr_temp2_offset.dev_attr);
835 static int lm90_read16(struct i2c_client *client, u8 regh, u8 regl, u16 *value)
841 * There is a trick here. We have to read two registers to have the
842 * sensor temperature, but we have to beware a conversion could occur
843 * inbetween the readings. The datasheet says we should either use
844 * the one-shot conversion register, which we don't want to do
845 * (disables hardware monitoring) or monitor the busy bit, which is
846 * impossible (we can't read the values and monitor that bit at the
847 * exact same time). So the solution used here is to read the high
848 * byte once, then the low byte, then the high byte again. If the new
849 * high byte matches the old one, then we have a valid reading. Else
850 * we have to read the low byte again, and now we believe we have a
853 if ((err = lm90_read_reg(client, regh, &oldh))
854 || (err = lm90_read_reg(client, regl, &l))
855 || (err = lm90_read_reg(client, regh, &newh)))
858 err = lm90_read_reg(client, regl, &l);
862 *value = (newh << 8) | l;
867 static struct lm90_data *lm90_update_device(struct device *dev)
869 struct i2c_client *client = to_i2c_client(dev);
870 struct lm90_data *data = i2c_get_clientdata(client);
872 mutex_lock(&data->update_lock);
874 if (time_after(jiffies, data->last_updated + HZ * 2) || !data->valid) {
877 dev_dbg(&client->dev, "Updating lm90 data.\n");
878 lm90_read_reg(client, LM90_REG_R_LOCAL_LOW, &data->temp8[0]);
879 lm90_read_reg(client, LM90_REG_R_LOCAL_HIGH, &data->temp8[1]);
880 lm90_read_reg(client, LM90_REG_R_LOCAL_CRIT, &data->temp8[2]);
881 lm90_read_reg(client, LM90_REG_R_REMOTE_CRIT, &data->temp8[3]);
882 lm90_read_reg(client, LM90_REG_R_TCRIT_HYST, &data->temp_hyst);
884 if (data->kind == max6657) {
885 lm90_read16(client, LM90_REG_R_LOCAL_TEMP,
886 MAX6657_REG_R_LOCAL_TEMPL,
889 if (lm90_read_reg(client, LM90_REG_R_LOCAL_TEMP,
891 data->temp11[4] = h << 8;
893 lm90_read16(client, LM90_REG_R_REMOTE_TEMPH,
894 LM90_REG_R_REMOTE_TEMPL, &data->temp11[0]);
896 if (lm90_read_reg(client, LM90_REG_R_REMOTE_LOWH, &h) == 0) {
897 data->temp11[1] = h << 8;
898 if (data->kind != max6657 && data->kind != max6680
899 && lm90_read_reg(client, LM90_REG_R_REMOTE_LOWL,
901 data->temp11[1] |= l;
903 if (lm90_read_reg(client, LM90_REG_R_REMOTE_HIGHH, &h) == 0) {
904 data->temp11[2] = h << 8;
905 if (data->kind != max6657 && data->kind != max6680
906 && lm90_read_reg(client, LM90_REG_R_REMOTE_HIGHL,
908 data->temp11[2] |= l;
911 if (data->kind != max6657) {
912 if (lm90_read_reg(client, LM90_REG_R_REMOTE_OFFSH,
914 && lm90_read_reg(client, LM90_REG_R_REMOTE_OFFSL,
916 data->temp11[3] = (h << 8) | l;
918 lm90_read_reg(client, LM90_REG_R_STATUS, &data->alarms);
920 data->last_updated = jiffies;
924 mutex_unlock(&data->update_lock);
929 static int __init sensors_lm90_init(void)
931 return i2c_add_driver(&lm90_driver);
934 static void __exit sensors_lm90_exit(void)
936 i2c_del_driver(&lm90_driver);
939 MODULE_AUTHOR("Jean Delvare <khali@linux-fr.org>");
940 MODULE_DESCRIPTION("LM90/ADM1032 driver");
941 MODULE_LICENSE("GPL");
943 module_init(sensors_lm90_init);
944 module_exit(sensors_lm90_exit);