2 * adm9240.c Part of lm_sensors, Linux kernel modules for hardware
5 * Copyright (C) 1999 Frodo Looijaard <frodol@dds.nl>
6 * Philip Edelbrock <phil@netroedge.com>
7 * Copyright (C) 2003 Michiel Rook <michiel@grendelproject.nl>
8 * Copyright (C) 2005 Grant Coady <gcoady.lk@gmail.com> with valuable
9 * guidance from Jean Delvare
11 * Driver supports Analog Devices ADM9240
12 * Dallas Semiconductor DS1780
13 * National Semiconductor LM81
15 * ADM9240 is the reference, DS1780 and LM81 are register compatibles
17 * Voltage Six inputs are scaled by chip, VID also reported
18 * Temperature Chip temperature to 0.5'C, maximum and max_hysteris
19 * Fans 2 fans, low speed alarm, automatic fan clock divider
20 * Alarms 16-bit map of active alarms
21 * Analog Out 0..1250 mV output
23 * Chassis Intrusion: clear CI latch with 'echo 0 > intrusion0_alarm'
25 * Test hardware: Intel SE440BX-2 desktop motherboard --Grant
27 * LM81 extended temp reading not implemented
29 * This program is free software; you can redistribute it and/or modify
30 * it under the terms of the GNU General Public License as published by
31 * the Free Software Foundation; either version 2 of the License, or
32 * (at your option) any later version.
34 * This program is distributed in the hope that it will be useful,
35 * but WITHOUT ANY WARRANTY; without even the implied warranty of
36 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
37 * GNU General Public License for more details.
39 * You should have received a copy of the GNU General Public License
40 * along with this program; if not, write to the Free Software
41 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
44 #include <linux/init.h>
45 #include <linux/module.h>
46 #include <linux/slab.h>
47 #include <linux/i2c.h>
48 #include <linux/hwmon-sysfs.h>
49 #include <linux/hwmon.h>
50 #include <linux/hwmon-vid.h>
51 #include <linux/err.h>
52 #include <linux/mutex.h>
54 /* Addresses to scan */
55 static const unsigned short normal_i2c[] = { 0x2c, 0x2d, 0x2e, 0x2f,
58 enum chips { adm9240, ds1780, lm81 };
60 /* ADM9240 registers */
61 #define ADM9240_REG_MAN_ID 0x3e
62 #define ADM9240_REG_DIE_REV 0x3f
63 #define ADM9240_REG_CONFIG 0x40
65 #define ADM9240_REG_IN(nr) (0x20 + (nr)) /* 0..5 */
66 #define ADM9240_REG_IN_MAX(nr) (0x2b + (nr) * 2)
67 #define ADM9240_REG_IN_MIN(nr) (0x2c + (nr) * 2)
68 #define ADM9240_REG_FAN(nr) (0x28 + (nr)) /* 0..1 */
69 #define ADM9240_REG_FAN_MIN(nr) (0x3b + (nr))
70 #define ADM9240_REG_INT(nr) (0x41 + (nr))
71 #define ADM9240_REG_INT_MASK(nr) (0x43 + (nr))
72 #define ADM9240_REG_TEMP 0x27
73 #define ADM9240_REG_TEMP_MAX(nr) (0x39 + (nr)) /* 0, 1 = high, hyst */
74 #define ADM9240_REG_ANALOG_OUT 0x19
75 #define ADM9240_REG_CHASSIS_CLEAR 0x46
76 #define ADM9240_REG_VID_FAN_DIV 0x47
77 #define ADM9240_REG_I2C_ADDR 0x48
78 #define ADM9240_REG_VID4 0x49
79 #define ADM9240_REG_TEMP_CONF 0x4b
81 /* generalised scaling with integer rounding */
82 static inline int SCALE(long val, int mul, int div)
85 return (val * mul - div / 2) / div;
87 return (val * mul + div / 2) / div;
90 /* adm9240 internally scales voltage measurements */
91 static const u16 nom_mv[] = { 2500, 2700, 3300, 5000, 12000, 2700 };
93 static inline unsigned int IN_FROM_REG(u8 reg, int n)
95 return SCALE(reg, nom_mv[n], 192);
98 static inline u8 IN_TO_REG(unsigned long val, int n)
100 return SENSORS_LIMIT(SCALE(val, 192, nom_mv[n]), 0, 255);
103 /* temperature range: -40..125, 127 disables temperature alarm */
104 static inline s8 TEMP_TO_REG(long val)
106 return SENSORS_LIMIT(SCALE(val, 1, 1000), -40, 127);
109 /* two fans, each with low fan speed limit */
110 static inline unsigned int FAN_FROM_REG(u8 reg, u8 div)
112 if (!reg) /* error */
118 return SCALE(1350000, 1, reg * div);
121 /* analog out 0..1250mV */
122 static inline u8 AOUT_TO_REG(unsigned long val)
124 return SENSORS_LIMIT(SCALE(val, 255, 1250), 0, 255);
127 static inline unsigned int AOUT_FROM_REG(u8 reg)
129 return SCALE(reg, 1250, 255);
132 static int adm9240_probe(struct i2c_client *client,
133 const struct i2c_device_id *id);
134 static int adm9240_detect(struct i2c_client *client,
135 struct i2c_board_info *info);
136 static void adm9240_init_client(struct i2c_client *client);
137 static int adm9240_remove(struct i2c_client *client);
138 static struct adm9240_data *adm9240_update_device(struct device *dev);
141 static const struct i2c_device_id adm9240_id[] = {
142 { "adm9240", adm9240 },
143 { "ds1780", ds1780 },
147 MODULE_DEVICE_TABLE(i2c, adm9240_id);
149 static struct i2c_driver adm9240_driver = {
150 .class = I2C_CLASS_HWMON,
154 .probe = adm9240_probe,
155 .remove = adm9240_remove,
156 .id_table = adm9240_id,
157 .detect = adm9240_detect,
158 .address_list = normal_i2c,
161 /* per client data */
162 struct adm9240_data {
163 struct device *hwmon_dev;
164 struct mutex update_lock;
166 unsigned long last_updated_measure;
167 unsigned long last_updated_config;
169 u8 in[6]; /* ro in0_input */
170 u8 in_max[6]; /* rw in0_max */
171 u8 in_min[6]; /* rw in0_min */
172 u8 fan[2]; /* ro fan1_input */
173 u8 fan_min[2]; /* rw fan1_min */
174 u8 fan_div[2]; /* rw fan1_div, read-only accessor */
175 s16 temp; /* ro temp1_input, 9-bit sign-extended */
176 s8 temp_max[2]; /* rw 0 -> temp_max, 1 -> temp_max_hyst */
177 u16 alarms; /* ro alarms */
178 u8 aout; /* rw aout_output */
180 u8 vrm; /* -- vrm set on startup, no accessor */
183 /*** sysfs accessors ***/
186 static ssize_t show_temp(struct device *dev, struct device_attribute *dummy,
189 struct adm9240_data *data = adm9240_update_device(dev);
190 return sprintf(buf, "%d\n", data->temp * 500); /* 9-bit value */
193 static ssize_t show_max(struct device *dev, struct device_attribute *devattr,
196 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
197 struct adm9240_data *data = adm9240_update_device(dev);
198 return sprintf(buf, "%d\n", data->temp_max[attr->index] * 1000);
201 static ssize_t set_max(struct device *dev, struct device_attribute *devattr,
202 const char *buf, size_t count)
204 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
205 struct i2c_client *client = to_i2c_client(dev);
206 struct adm9240_data *data = i2c_get_clientdata(client);
210 err = kstrtol(buf, 10, &val);
214 mutex_lock(&data->update_lock);
215 data->temp_max[attr->index] = TEMP_TO_REG(val);
216 i2c_smbus_write_byte_data(client, ADM9240_REG_TEMP_MAX(attr->index),
217 data->temp_max[attr->index]);
218 mutex_unlock(&data->update_lock);
222 static DEVICE_ATTR(temp1_input, S_IRUGO, show_temp, NULL);
223 static SENSOR_DEVICE_ATTR(temp1_max, S_IWUSR | S_IRUGO,
224 show_max, set_max, 0);
225 static SENSOR_DEVICE_ATTR(temp1_max_hyst, S_IWUSR | S_IRUGO,
226 show_max, set_max, 1);
229 static ssize_t show_in(struct device *dev, struct device_attribute *devattr,
232 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
233 struct adm9240_data *data = adm9240_update_device(dev);
234 return sprintf(buf, "%d\n", IN_FROM_REG(data->in[attr->index],
238 static ssize_t show_in_min(struct device *dev,
239 struct device_attribute *devattr, char *buf)
241 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
242 struct adm9240_data *data = adm9240_update_device(dev);
243 return sprintf(buf, "%d\n", IN_FROM_REG(data->in_min[attr->index],
247 static ssize_t show_in_max(struct device *dev,
248 struct device_attribute *devattr, char *buf)
250 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
251 struct adm9240_data *data = adm9240_update_device(dev);
252 return sprintf(buf, "%d\n", IN_FROM_REG(data->in_max[attr->index],
256 static ssize_t set_in_min(struct device *dev,
257 struct device_attribute *devattr,
258 const char *buf, size_t count)
260 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
261 struct i2c_client *client = to_i2c_client(dev);
262 struct adm9240_data *data = i2c_get_clientdata(client);
266 err = kstrtoul(buf, 10, &val);
270 mutex_lock(&data->update_lock);
271 data->in_min[attr->index] = IN_TO_REG(val, attr->index);
272 i2c_smbus_write_byte_data(client, ADM9240_REG_IN_MIN(attr->index),
273 data->in_min[attr->index]);
274 mutex_unlock(&data->update_lock);
278 static ssize_t set_in_max(struct device *dev,
279 struct device_attribute *devattr,
280 const char *buf, size_t count)
282 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
283 struct i2c_client *client = to_i2c_client(dev);
284 struct adm9240_data *data = i2c_get_clientdata(client);
288 err = kstrtoul(buf, 10, &val);
292 mutex_lock(&data->update_lock);
293 data->in_max[attr->index] = IN_TO_REG(val, attr->index);
294 i2c_smbus_write_byte_data(client, ADM9240_REG_IN_MAX(attr->index),
295 data->in_max[attr->index]);
296 mutex_unlock(&data->update_lock);
301 static SENSOR_DEVICE_ATTR(in##nr##_input, S_IRUGO, \
302 show_in, NULL, nr); \
303 static SENSOR_DEVICE_ATTR(in##nr##_min, S_IRUGO | S_IWUSR, \
304 show_in_min, set_in_min, nr); \
305 static SENSOR_DEVICE_ATTR(in##nr##_max, S_IRUGO | S_IWUSR, \
306 show_in_max, set_in_max, nr);
316 static ssize_t show_fan(struct device *dev,
317 struct device_attribute *devattr, char *buf)
319 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
320 struct adm9240_data *data = adm9240_update_device(dev);
321 return sprintf(buf, "%d\n", FAN_FROM_REG(data->fan[attr->index],
322 1 << data->fan_div[attr->index]));
325 static ssize_t show_fan_min(struct device *dev,
326 struct device_attribute *devattr, char *buf)
328 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
329 struct adm9240_data *data = adm9240_update_device(dev);
330 return sprintf(buf, "%d\n", FAN_FROM_REG(data->fan_min[attr->index],
331 1 << data->fan_div[attr->index]));
334 static ssize_t show_fan_div(struct device *dev,
335 struct device_attribute *devattr, char *buf)
337 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
338 struct adm9240_data *data = adm9240_update_device(dev);
339 return sprintf(buf, "%d\n", 1 << data->fan_div[attr->index]);
342 /* write new fan div, callers must hold data->update_lock */
343 static void adm9240_write_fan_div(struct i2c_client *client, int nr,
346 u8 reg, old, shift = (nr + 2) * 2;
348 reg = i2c_smbus_read_byte_data(client, ADM9240_REG_VID_FAN_DIV);
349 old = (reg >> shift) & 3;
350 reg &= ~(3 << shift);
351 reg |= (fan_div << shift);
352 i2c_smbus_write_byte_data(client, ADM9240_REG_VID_FAN_DIV, reg);
353 dev_dbg(&client->dev, "fan%d clock divider changed from %u "
354 "to %u\n", nr + 1, 1 << old, 1 << fan_div);
358 * set fan speed low limit:
360 * - value is zero: disable fan speed low limit alarm
362 * - value is below fan speed measurement range: enable fan speed low
363 * limit alarm to be asserted while fan speed too slow to measure
365 * - otherwise: select fan clock divider to suit fan speed low limit,
366 * measurement code may adjust registers to ensure fan speed reading
368 static ssize_t set_fan_min(struct device *dev,
369 struct device_attribute *devattr,
370 const char *buf, size_t count)
372 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
373 struct i2c_client *client = to_i2c_client(dev);
374 struct adm9240_data *data = i2c_get_clientdata(client);
375 int nr = attr->index;
380 err = kstrtoul(buf, 10, &val);
384 mutex_lock(&data->update_lock);
387 data->fan_min[nr] = 255;
388 new_div = data->fan_div[nr];
390 dev_dbg(&client->dev, "fan%u low limit set disabled\n",
393 } else if (val < 1350000 / (8 * 254)) {
395 data->fan_min[nr] = 254;
397 dev_dbg(&client->dev, "fan%u low limit set minimum %u\n",
398 nr + 1, FAN_FROM_REG(254, 1 << new_div));
401 unsigned int new_min = 1350000 / val;
404 while (new_min > 192 && new_div < 3) {
408 if (!new_min) /* keep > 0 */
411 data->fan_min[nr] = new_min;
413 dev_dbg(&client->dev, "fan%u low limit set fan speed %u\n",
414 nr + 1, FAN_FROM_REG(new_min, 1 << new_div));
417 if (new_div != data->fan_div[nr]) {
418 data->fan_div[nr] = new_div;
419 adm9240_write_fan_div(client, nr, new_div);
421 i2c_smbus_write_byte_data(client, ADM9240_REG_FAN_MIN(nr),
424 mutex_unlock(&data->update_lock);
429 static SENSOR_DEVICE_ATTR(fan##nr##_input, S_IRUGO, \
430 show_fan, NULL, nr - 1); \
431 static SENSOR_DEVICE_ATTR(fan##nr##_div, S_IRUGO, \
432 show_fan_div, NULL, nr - 1); \
433 static SENSOR_DEVICE_ATTR(fan##nr##_min, S_IRUGO | S_IWUSR, \
434 show_fan_min, set_fan_min, nr - 1);
440 static ssize_t show_alarms(struct device *dev,
441 struct device_attribute *attr, char *buf)
443 struct adm9240_data *data = adm9240_update_device(dev);
444 return sprintf(buf, "%u\n", data->alarms);
446 static DEVICE_ATTR(alarms, S_IRUGO, show_alarms, NULL);
448 static ssize_t show_alarm(struct device *dev,
449 struct device_attribute *attr, char *buf)
451 int bitnr = to_sensor_dev_attr(attr)->index;
452 struct adm9240_data *data = adm9240_update_device(dev);
453 return sprintf(buf, "%u\n", (data->alarms >> bitnr) & 1);
455 static SENSOR_DEVICE_ATTR(in0_alarm, S_IRUGO, show_alarm, NULL, 0);
456 static SENSOR_DEVICE_ATTR(in1_alarm, S_IRUGO, show_alarm, NULL, 1);
457 static SENSOR_DEVICE_ATTR(in2_alarm, S_IRUGO, show_alarm, NULL, 2);
458 static SENSOR_DEVICE_ATTR(in3_alarm, S_IRUGO, show_alarm, NULL, 3);
459 static SENSOR_DEVICE_ATTR(in4_alarm, S_IRUGO, show_alarm, NULL, 8);
460 static SENSOR_DEVICE_ATTR(in5_alarm, S_IRUGO, show_alarm, NULL, 9);
461 static SENSOR_DEVICE_ATTR(temp1_alarm, S_IRUGO, show_alarm, NULL, 4);
462 static SENSOR_DEVICE_ATTR(fan1_alarm, S_IRUGO, show_alarm, NULL, 6);
463 static SENSOR_DEVICE_ATTR(fan2_alarm, S_IRUGO, show_alarm, NULL, 7);
466 static ssize_t show_vid(struct device *dev,
467 struct device_attribute *attr, char *buf)
469 struct adm9240_data *data = adm9240_update_device(dev);
470 return sprintf(buf, "%d\n", vid_from_reg(data->vid, data->vrm));
472 static DEVICE_ATTR(cpu0_vid, S_IRUGO, show_vid, NULL);
475 static ssize_t show_aout(struct device *dev,
476 struct device_attribute *attr, char *buf)
478 struct adm9240_data *data = adm9240_update_device(dev);
479 return sprintf(buf, "%d\n", AOUT_FROM_REG(data->aout));
482 static ssize_t set_aout(struct device *dev,
483 struct device_attribute *attr,
484 const char *buf, size_t count)
486 struct i2c_client *client = to_i2c_client(dev);
487 struct adm9240_data *data = i2c_get_clientdata(client);
491 err = kstrtol(buf, 10, &val);
495 mutex_lock(&data->update_lock);
496 data->aout = AOUT_TO_REG(val);
497 i2c_smbus_write_byte_data(client, ADM9240_REG_ANALOG_OUT, data->aout);
498 mutex_unlock(&data->update_lock);
501 static DEVICE_ATTR(aout_output, S_IRUGO | S_IWUSR, show_aout, set_aout);
503 static ssize_t chassis_clear(struct device *dev,
504 struct device_attribute *attr,
505 const char *buf, size_t count)
507 struct i2c_client *client = to_i2c_client(dev);
508 struct adm9240_data *data = i2c_get_clientdata(client);
511 if (kstrtoul(buf, 10, &val) || val != 0)
514 mutex_lock(&data->update_lock);
515 i2c_smbus_write_byte_data(client, ADM9240_REG_CHASSIS_CLEAR, 0x80);
516 data->valid = 0; /* Force cache refresh */
517 mutex_unlock(&data->update_lock);
518 dev_dbg(&client->dev, "chassis intrusion latch cleared\n");
522 static SENSOR_DEVICE_ATTR(intrusion0_alarm, S_IRUGO | S_IWUSR, show_alarm,
525 static struct attribute *adm9240_attributes[] = {
526 &sensor_dev_attr_in0_input.dev_attr.attr,
527 &sensor_dev_attr_in0_min.dev_attr.attr,
528 &sensor_dev_attr_in0_max.dev_attr.attr,
529 &sensor_dev_attr_in0_alarm.dev_attr.attr,
530 &sensor_dev_attr_in1_input.dev_attr.attr,
531 &sensor_dev_attr_in1_min.dev_attr.attr,
532 &sensor_dev_attr_in1_max.dev_attr.attr,
533 &sensor_dev_attr_in1_alarm.dev_attr.attr,
534 &sensor_dev_attr_in2_input.dev_attr.attr,
535 &sensor_dev_attr_in2_min.dev_attr.attr,
536 &sensor_dev_attr_in2_max.dev_attr.attr,
537 &sensor_dev_attr_in2_alarm.dev_attr.attr,
538 &sensor_dev_attr_in3_input.dev_attr.attr,
539 &sensor_dev_attr_in3_min.dev_attr.attr,
540 &sensor_dev_attr_in3_max.dev_attr.attr,
541 &sensor_dev_attr_in3_alarm.dev_attr.attr,
542 &sensor_dev_attr_in4_input.dev_attr.attr,
543 &sensor_dev_attr_in4_min.dev_attr.attr,
544 &sensor_dev_attr_in4_max.dev_attr.attr,
545 &sensor_dev_attr_in4_alarm.dev_attr.attr,
546 &sensor_dev_attr_in5_input.dev_attr.attr,
547 &sensor_dev_attr_in5_min.dev_attr.attr,
548 &sensor_dev_attr_in5_max.dev_attr.attr,
549 &sensor_dev_attr_in5_alarm.dev_attr.attr,
550 &dev_attr_temp1_input.attr,
551 &sensor_dev_attr_temp1_max.dev_attr.attr,
552 &sensor_dev_attr_temp1_max_hyst.dev_attr.attr,
553 &sensor_dev_attr_temp1_alarm.dev_attr.attr,
554 &sensor_dev_attr_fan1_input.dev_attr.attr,
555 &sensor_dev_attr_fan1_div.dev_attr.attr,
556 &sensor_dev_attr_fan1_min.dev_attr.attr,
557 &sensor_dev_attr_fan1_alarm.dev_attr.attr,
558 &sensor_dev_attr_fan2_input.dev_attr.attr,
559 &sensor_dev_attr_fan2_div.dev_attr.attr,
560 &sensor_dev_attr_fan2_min.dev_attr.attr,
561 &sensor_dev_attr_fan2_alarm.dev_attr.attr,
562 &dev_attr_alarms.attr,
563 &dev_attr_aout_output.attr,
564 &sensor_dev_attr_intrusion0_alarm.dev_attr.attr,
565 &dev_attr_cpu0_vid.attr,
569 static const struct attribute_group adm9240_group = {
570 .attrs = adm9240_attributes,
574 /*** sensor chip detect and driver install ***/
576 /* Return 0 if detection is successful, -ENODEV otherwise */
577 static int adm9240_detect(struct i2c_client *new_client,
578 struct i2c_board_info *info)
580 struct i2c_adapter *adapter = new_client->adapter;
581 const char *name = "";
582 int address = new_client->addr;
585 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
588 /* verify chip: reg address should match i2c address */
589 if (i2c_smbus_read_byte_data(new_client, ADM9240_REG_I2C_ADDR)
591 dev_err(&adapter->dev, "detect fail: address match, 0x%02x\n",
596 /* check known chip manufacturer */
597 man_id = i2c_smbus_read_byte_data(new_client, ADM9240_REG_MAN_ID);
598 if (man_id == 0x23) {
600 } else if (man_id == 0xda) {
602 } else if (man_id == 0x01) {
605 dev_err(&adapter->dev, "detect fail: unknown manuf, 0x%02x\n",
610 /* successful detect, print chip info */
611 die_rev = i2c_smbus_read_byte_data(new_client, ADM9240_REG_DIE_REV);
612 dev_info(&adapter->dev, "found %s revision %u\n",
613 man_id == 0x23 ? "ADM9240" :
614 man_id == 0xda ? "DS1780" : "LM81", die_rev);
616 strlcpy(info->type, name, I2C_NAME_SIZE);
621 static int adm9240_probe(struct i2c_client *new_client,
622 const struct i2c_device_id *id)
624 struct adm9240_data *data;
627 data = devm_kzalloc(&new_client->dev, sizeof(*data), GFP_KERNEL);
631 i2c_set_clientdata(new_client, data);
632 mutex_init(&data->update_lock);
634 adm9240_init_client(new_client);
636 /* populate sysfs filesystem */
637 err = sysfs_create_group(&new_client->dev.kobj, &adm9240_group);
641 data->hwmon_dev = hwmon_device_register(&new_client->dev);
642 if (IS_ERR(data->hwmon_dev)) {
643 err = PTR_ERR(data->hwmon_dev);
650 sysfs_remove_group(&new_client->dev.kobj, &adm9240_group);
654 static int adm9240_remove(struct i2c_client *client)
656 struct adm9240_data *data = i2c_get_clientdata(client);
658 hwmon_device_unregister(data->hwmon_dev);
659 sysfs_remove_group(&client->dev.kobj, &adm9240_group);
664 static void adm9240_init_client(struct i2c_client *client)
666 struct adm9240_data *data = i2c_get_clientdata(client);
667 u8 conf = i2c_smbus_read_byte_data(client, ADM9240_REG_CONFIG);
668 u8 mode = i2c_smbus_read_byte_data(client, ADM9240_REG_TEMP_CONF) & 3;
670 data->vrm = vid_which_vrm(); /* need this to report vid as mV */
672 dev_info(&client->dev, "Using VRM: %d.%d\n", data->vrm / 10,
675 if (conf & 1) { /* measurement cycle running: report state */
677 dev_info(&client->dev, "status: config 0x%02x mode %u\n",
680 } else { /* cold start: open limits before starting chip */
683 for (i = 0; i < 6; i++) {
684 i2c_smbus_write_byte_data(client,
685 ADM9240_REG_IN_MIN(i), 0);
686 i2c_smbus_write_byte_data(client,
687 ADM9240_REG_IN_MAX(i), 255);
689 i2c_smbus_write_byte_data(client,
690 ADM9240_REG_FAN_MIN(0), 255);
691 i2c_smbus_write_byte_data(client,
692 ADM9240_REG_FAN_MIN(1), 255);
693 i2c_smbus_write_byte_data(client,
694 ADM9240_REG_TEMP_MAX(0), 127);
695 i2c_smbus_write_byte_data(client,
696 ADM9240_REG_TEMP_MAX(1), 127);
698 /* start measurement cycle */
699 i2c_smbus_write_byte_data(client, ADM9240_REG_CONFIG, 1);
701 dev_info(&client->dev, "cold start: config was 0x%02x "
702 "mode %u\n", conf, mode);
706 static struct adm9240_data *adm9240_update_device(struct device *dev)
708 struct i2c_client *client = to_i2c_client(dev);
709 struct adm9240_data *data = i2c_get_clientdata(client);
712 mutex_lock(&data->update_lock);
714 /* minimum measurement cycle: 1.75 seconds */
715 if (time_after(jiffies, data->last_updated_measure + (HZ * 7 / 4))
718 for (i = 0; i < 6; i++) { /* read voltages */
719 data->in[i] = i2c_smbus_read_byte_data(client,
722 data->alarms = i2c_smbus_read_byte_data(client,
723 ADM9240_REG_INT(0)) |
724 i2c_smbus_read_byte_data(client,
725 ADM9240_REG_INT(1)) << 8;
728 * read temperature: assume temperature changes less than
729 * 0.5'C per two measurement cycles thus ignore possible
730 * but unlikely aliasing error on lsb reading. --Grant
732 data->temp = ((i2c_smbus_read_byte_data(client,
733 ADM9240_REG_TEMP) << 8) |
734 i2c_smbus_read_byte_data(client,
735 ADM9240_REG_TEMP_CONF)) / 128;
737 for (i = 0; i < 2; i++) { /* read fans */
738 data->fan[i] = i2c_smbus_read_byte_data(client,
741 /* adjust fan clock divider on overflow */
742 if (data->valid && data->fan[i] == 255 &&
743 data->fan_div[i] < 3) {
745 adm9240_write_fan_div(client, i,
748 /* adjust fan_min if active, but not to 0 */
749 if (data->fan_min[i] < 255 &&
750 data->fan_min[i] >= 2)
751 data->fan_min[i] /= 2;
754 data->last_updated_measure = jiffies;
757 /* minimum config reading cycle: 300 seconds */
758 if (time_after(jiffies, data->last_updated_config + (HZ * 300))
761 for (i = 0; i < 6; i++) {
762 data->in_min[i] = i2c_smbus_read_byte_data(client,
763 ADM9240_REG_IN_MIN(i));
764 data->in_max[i] = i2c_smbus_read_byte_data(client,
765 ADM9240_REG_IN_MAX(i));
767 for (i = 0; i < 2; i++) {
768 data->fan_min[i] = i2c_smbus_read_byte_data(client,
769 ADM9240_REG_FAN_MIN(i));
771 data->temp_max[0] = i2c_smbus_read_byte_data(client,
772 ADM9240_REG_TEMP_MAX(0));
773 data->temp_max[1] = i2c_smbus_read_byte_data(client,
774 ADM9240_REG_TEMP_MAX(1));
776 /* read fan divs and 5-bit VID */
777 i = i2c_smbus_read_byte_data(client, ADM9240_REG_VID_FAN_DIV);
778 data->fan_div[0] = (i >> 4) & 3;
779 data->fan_div[1] = (i >> 6) & 3;
780 data->vid = i & 0x0f;
781 data->vid |= (i2c_smbus_read_byte_data(client,
782 ADM9240_REG_VID4) & 1) << 4;
783 /* read analog out */
784 data->aout = i2c_smbus_read_byte_data(client,
785 ADM9240_REG_ANALOG_OUT);
787 data->last_updated_config = jiffies;
790 mutex_unlock(&data->update_lock);
794 module_i2c_driver(adm9240_driver);
796 MODULE_AUTHOR("Michiel Rook <michiel@grendelproject.nl>, "
797 "Grant Coady <gcoady.lk@gmail.com> and others");
798 MODULE_DESCRIPTION("ADM9240/DS1780/LM81 driver");
799 MODULE_LICENSE("GPL");