ACPI: GPE enabling should happen after EC installation
[linux-2.6/mini2440.git] / drivers / hwmon / adm9240.c
blob149ef25252e7658442e4191b9b645a5c088685c7
1 /*
2 * adm9240.c Part of lm_sensors, Linux kernel modules for hardware
3 * monitoring
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 1 > chassis_clear'
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,
56 I2C_CLIENT_END };
58 /* Insmod parameters */
59 I2C_CLIENT_INSMOD_3(adm9240, ds1780, lm81);
61 /* ADM9240 registers */
62 #define ADM9240_REG_MAN_ID 0x3e
63 #define ADM9240_REG_DIE_REV 0x3f
64 #define ADM9240_REG_CONFIG 0x40
66 #define ADM9240_REG_IN(nr) (0x20 + (nr)) /* 0..5 */
67 #define ADM9240_REG_IN_MAX(nr) (0x2b + (nr) * 2)
68 #define ADM9240_REG_IN_MIN(nr) (0x2c + (nr) * 2)
69 #define ADM9240_REG_FAN(nr) (0x28 + (nr)) /* 0..1 */
70 #define ADM9240_REG_FAN_MIN(nr) (0x3b + (nr))
71 #define ADM9240_REG_INT(nr) (0x41 + (nr))
72 #define ADM9240_REG_INT_MASK(nr) (0x43 + (nr))
73 #define ADM9240_REG_TEMP 0x27
74 #define ADM9240_REG_TEMP_MAX(nr) (0x39 + (nr)) /* 0, 1 = high, hyst */
75 #define ADM9240_REG_ANALOG_OUT 0x19
76 #define ADM9240_REG_CHASSIS_CLEAR 0x46
77 #define ADM9240_REG_VID_FAN_DIV 0x47
78 #define ADM9240_REG_I2C_ADDR 0x48
79 #define ADM9240_REG_VID4 0x49
80 #define ADM9240_REG_TEMP_CONF 0x4b
82 /* generalised scaling with integer rounding */
83 static inline int SCALE(long val, int mul, int div)
85 if (val < 0)
86 return (val * mul - div / 2) / div;
87 else
88 return (val * mul + div / 2) / div;
91 /* adm9240 internally scales voltage measurements */
92 static const u16 nom_mv[] = { 2500, 2700, 3300, 5000, 12000, 2700 };
94 static inline unsigned int IN_FROM_REG(u8 reg, int n)
96 return SCALE(reg, nom_mv[n], 192);
99 static inline u8 IN_TO_REG(unsigned long val, int n)
101 return SENSORS_LIMIT(SCALE(val, 192, nom_mv[n]), 0, 255);
104 /* temperature range: -40..125, 127 disables temperature alarm */
105 static inline s8 TEMP_TO_REG(long val)
107 return SENSORS_LIMIT(SCALE(val, 1, 1000), -40, 127);
110 /* two fans, each with low fan speed limit */
111 static inline unsigned int FAN_FROM_REG(u8 reg, u8 div)
113 if (!reg) /* error */
114 return -1;
116 if (reg == 255)
117 return 0;
119 return SCALE(1350000, 1, reg * div);
122 /* analog out 0..1250mV */
123 static inline u8 AOUT_TO_REG(unsigned long val)
125 return SENSORS_LIMIT(SCALE(val, 255, 1250), 0, 255);
128 static inline unsigned int AOUT_FROM_REG(u8 reg)
130 return SCALE(reg, 1250, 255);
133 static int adm9240_attach_adapter(struct i2c_adapter *adapter);
134 static int adm9240_detect(struct i2c_adapter *adapter, int address, int kind);
135 static void adm9240_init_client(struct i2c_client *client);
136 static int adm9240_detach_client(struct i2c_client *client);
137 static struct adm9240_data *adm9240_update_device(struct device *dev);
139 /* driver data */
140 static struct i2c_driver adm9240_driver = {
141 .driver = {
142 .name = "adm9240",
144 .attach_adapter = adm9240_attach_adapter,
145 .detach_client = adm9240_detach_client,
148 /* per client data */
149 struct adm9240_data {
150 enum chips type;
151 struct i2c_client client;
152 struct device *hwmon_dev;
153 struct mutex update_lock;
154 char valid;
155 unsigned long last_updated_measure;
156 unsigned long last_updated_config;
158 u8 in[6]; /* ro in0_input */
159 u8 in_max[6]; /* rw in0_max */
160 u8 in_min[6]; /* rw in0_min */
161 u8 fan[2]; /* ro fan1_input */
162 u8 fan_min[2]; /* rw fan1_min */
163 u8 fan_div[2]; /* rw fan1_div, read-only accessor */
164 s16 temp; /* ro temp1_input, 9-bit sign-extended */
165 s8 temp_max[2]; /* rw 0 -> temp_max, 1 -> temp_max_hyst */
166 u16 alarms; /* ro alarms */
167 u8 aout; /* rw aout_output */
168 u8 vid; /* ro vid */
169 u8 vrm; /* -- vrm set on startup, no accessor */
172 /*** sysfs accessors ***/
174 /* temperature */
175 static ssize_t show_temp(struct device *dev, struct device_attribute *dummy,
176 char *buf)
178 struct adm9240_data *data = adm9240_update_device(dev);
179 return sprintf(buf, "%d\n", data->temp * 500); /* 9-bit value */
182 static ssize_t show_max(struct device *dev, struct device_attribute *devattr,
183 char *buf)
185 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
186 struct adm9240_data *data = adm9240_update_device(dev);
187 return sprintf(buf, "%d\n", data->temp_max[attr->index] * 1000);
190 static ssize_t set_max(struct device *dev, struct device_attribute *devattr,
191 const char *buf, size_t count)
193 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
194 struct i2c_client *client = to_i2c_client(dev);
195 struct adm9240_data *data = i2c_get_clientdata(client);
196 long val = simple_strtol(buf, NULL, 10);
198 mutex_lock(&data->update_lock);
199 data->temp_max[attr->index] = TEMP_TO_REG(val);
200 i2c_smbus_write_byte_data(client, ADM9240_REG_TEMP_MAX(attr->index),
201 data->temp_max[attr->index]);
202 mutex_unlock(&data->update_lock);
203 return count;
206 static DEVICE_ATTR(temp1_input, S_IRUGO, show_temp, NULL);
207 static SENSOR_DEVICE_ATTR(temp1_max, S_IWUSR | S_IRUGO,
208 show_max, set_max, 0);
209 static SENSOR_DEVICE_ATTR(temp1_max_hyst, S_IWUSR | S_IRUGO,
210 show_max, set_max, 1);
212 /* voltage */
213 static ssize_t show_in(struct device *dev, struct device_attribute *devattr,
214 char *buf)
216 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
217 struct adm9240_data *data = adm9240_update_device(dev);
218 return sprintf(buf, "%d\n", IN_FROM_REG(data->in[attr->index],
219 attr->index));
222 static ssize_t show_in_min(struct device *dev,
223 struct device_attribute *devattr, char *buf)
225 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
226 struct adm9240_data *data = adm9240_update_device(dev);
227 return sprintf(buf, "%d\n", IN_FROM_REG(data->in_min[attr->index],
228 attr->index));
231 static ssize_t show_in_max(struct device *dev,
232 struct device_attribute *devattr, char *buf)
234 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
235 struct adm9240_data *data = adm9240_update_device(dev);
236 return sprintf(buf, "%d\n", IN_FROM_REG(data->in_max[attr->index],
237 attr->index));
240 static ssize_t set_in_min(struct device *dev,
241 struct device_attribute *devattr,
242 const char *buf, size_t count)
244 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
245 struct i2c_client *client = to_i2c_client(dev);
246 struct adm9240_data *data = i2c_get_clientdata(client);
247 unsigned long val = simple_strtoul(buf, NULL, 10);
249 mutex_lock(&data->update_lock);
250 data->in_min[attr->index] = IN_TO_REG(val, attr->index);
251 i2c_smbus_write_byte_data(client, ADM9240_REG_IN_MIN(attr->index),
252 data->in_min[attr->index]);
253 mutex_unlock(&data->update_lock);
254 return count;
257 static ssize_t set_in_max(struct device *dev,
258 struct device_attribute *devattr,
259 const char *buf, size_t count)
261 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
262 struct i2c_client *client = to_i2c_client(dev);
263 struct adm9240_data *data = i2c_get_clientdata(client);
264 unsigned long val = simple_strtoul(buf, NULL, 10);
266 mutex_lock(&data->update_lock);
267 data->in_max[attr->index] = IN_TO_REG(val, attr->index);
268 i2c_smbus_write_byte_data(client, ADM9240_REG_IN_MAX(attr->index),
269 data->in_max[attr->index]);
270 mutex_unlock(&data->update_lock);
271 return count;
274 #define vin(nr) \
275 static SENSOR_DEVICE_ATTR(in##nr##_input, S_IRUGO, \
276 show_in, NULL, nr); \
277 static SENSOR_DEVICE_ATTR(in##nr##_min, S_IRUGO | S_IWUSR, \
278 show_in_min, set_in_min, nr); \
279 static SENSOR_DEVICE_ATTR(in##nr##_max, S_IRUGO | S_IWUSR, \
280 show_in_max, set_in_max, nr);
282 vin(0);
283 vin(1);
284 vin(2);
285 vin(3);
286 vin(4);
287 vin(5);
289 /* fans */
290 static ssize_t show_fan(struct device *dev,
291 struct device_attribute *devattr, char *buf)
293 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
294 struct adm9240_data *data = adm9240_update_device(dev);
295 return sprintf(buf, "%d\n", FAN_FROM_REG(data->fan[attr->index],
296 1 << data->fan_div[attr->index]));
299 static ssize_t show_fan_min(struct device *dev,
300 struct device_attribute *devattr, char *buf)
302 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
303 struct adm9240_data *data = adm9240_update_device(dev);
304 return sprintf(buf, "%d\n", FAN_FROM_REG(data->fan_min[attr->index],
305 1 << data->fan_div[attr->index]));
308 static ssize_t show_fan_div(struct device *dev,
309 struct device_attribute *devattr, char *buf)
311 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
312 struct adm9240_data *data = adm9240_update_device(dev);
313 return sprintf(buf, "%d\n", 1 << data->fan_div[attr->index]);
316 /* write new fan div, callers must hold data->update_lock */
317 static void adm9240_write_fan_div(struct i2c_client *client, int nr,
318 u8 fan_div)
320 u8 reg, old, shift = (nr + 2) * 2;
322 reg = i2c_smbus_read_byte_data(client, ADM9240_REG_VID_FAN_DIV);
323 old = (reg >> shift) & 3;
324 reg &= ~(3 << shift);
325 reg |= (fan_div << shift);
326 i2c_smbus_write_byte_data(client, ADM9240_REG_VID_FAN_DIV, reg);
327 dev_dbg(&client->dev, "fan%d clock divider changed from %u "
328 "to %u\n", nr + 1, 1 << old, 1 << fan_div);
332 * set fan speed low limit:
334 * - value is zero: disable fan speed low limit alarm
336 * - value is below fan speed measurement range: enable fan speed low
337 * limit alarm to be asserted while fan speed too slow to measure
339 * - otherwise: select fan clock divider to suit fan speed low limit,
340 * measurement code may adjust registers to ensure fan speed reading
342 static ssize_t set_fan_min(struct device *dev,
343 struct device_attribute *devattr,
344 const char *buf, size_t count)
346 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
347 struct i2c_client *client = to_i2c_client(dev);
348 struct adm9240_data *data = i2c_get_clientdata(client);
349 unsigned long val = simple_strtoul(buf, NULL, 10);
350 int nr = attr->index;
351 u8 new_div;
353 mutex_lock(&data->update_lock);
355 if (!val) {
356 data->fan_min[nr] = 255;
357 new_div = data->fan_div[nr];
359 dev_dbg(&client->dev, "fan%u low limit set disabled\n",
360 nr + 1);
362 } else if (val < 1350000 / (8 * 254)) {
363 new_div = 3;
364 data->fan_min[nr] = 254;
366 dev_dbg(&client->dev, "fan%u low limit set minimum %u\n",
367 nr + 1, FAN_FROM_REG(254, 1 << new_div));
369 } else {
370 unsigned int new_min = 1350000 / val;
372 new_div = 0;
373 while (new_min > 192 && new_div < 3) {
374 new_div++;
375 new_min /= 2;
377 if (!new_min) /* keep > 0 */
378 new_min++;
380 data->fan_min[nr] = new_min;
382 dev_dbg(&client->dev, "fan%u low limit set fan speed %u\n",
383 nr + 1, FAN_FROM_REG(new_min, 1 << new_div));
386 if (new_div != data->fan_div[nr]) {
387 data->fan_div[nr] = new_div;
388 adm9240_write_fan_div(client, nr, new_div);
390 i2c_smbus_write_byte_data(client, ADM9240_REG_FAN_MIN(nr),
391 data->fan_min[nr]);
393 mutex_unlock(&data->update_lock);
394 return count;
397 #define fan(nr) \
398 static SENSOR_DEVICE_ATTR(fan##nr##_input, S_IRUGO, \
399 show_fan, NULL, nr - 1); \
400 static SENSOR_DEVICE_ATTR(fan##nr##_div, S_IRUGO, \
401 show_fan_div, NULL, nr - 1); \
402 static SENSOR_DEVICE_ATTR(fan##nr##_min, S_IRUGO | S_IWUSR, \
403 show_fan_min, set_fan_min, nr - 1);
405 fan(1);
406 fan(2);
408 /* alarms */
409 static ssize_t show_alarms(struct device *dev,
410 struct device_attribute *attr, char *buf)
412 struct adm9240_data *data = adm9240_update_device(dev);
413 return sprintf(buf, "%u\n", data->alarms);
415 static DEVICE_ATTR(alarms, S_IRUGO, show_alarms, NULL);
417 static ssize_t show_alarm(struct device *dev,
418 struct device_attribute *attr, char *buf)
420 int bitnr = to_sensor_dev_attr(attr)->index;
421 struct adm9240_data *data = adm9240_update_device(dev);
422 return sprintf(buf, "%u\n", (data->alarms >> bitnr) & 1);
424 static SENSOR_DEVICE_ATTR(in0_alarm, S_IRUGO, show_alarm, NULL, 0);
425 static SENSOR_DEVICE_ATTR(in1_alarm, S_IRUGO, show_alarm, NULL, 1);
426 static SENSOR_DEVICE_ATTR(in2_alarm, S_IRUGO, show_alarm, NULL, 2);
427 static SENSOR_DEVICE_ATTR(in3_alarm, S_IRUGO, show_alarm, NULL, 3);
428 static SENSOR_DEVICE_ATTR(in4_alarm, S_IRUGO, show_alarm, NULL, 8);
429 static SENSOR_DEVICE_ATTR(in5_alarm, S_IRUGO, show_alarm, NULL, 9);
430 static SENSOR_DEVICE_ATTR(temp1_alarm, S_IRUGO, show_alarm, NULL, 4);
431 static SENSOR_DEVICE_ATTR(fan1_alarm, S_IRUGO, show_alarm, NULL, 6);
432 static SENSOR_DEVICE_ATTR(fan2_alarm, S_IRUGO, show_alarm, NULL, 7);
434 /* vid */
435 static ssize_t show_vid(struct device *dev,
436 struct device_attribute *attr, char *buf)
438 struct adm9240_data *data = adm9240_update_device(dev);
439 return sprintf(buf, "%d\n", vid_from_reg(data->vid, data->vrm));
441 static DEVICE_ATTR(cpu0_vid, S_IRUGO, show_vid, NULL);
443 /* analog output */
444 static ssize_t show_aout(struct device *dev,
445 struct device_attribute *attr, char *buf)
447 struct adm9240_data *data = adm9240_update_device(dev);
448 return sprintf(buf, "%d\n", AOUT_FROM_REG(data->aout));
451 static ssize_t set_aout(struct device *dev,
452 struct device_attribute *attr,
453 const char *buf, size_t count)
455 struct i2c_client *client = to_i2c_client(dev);
456 struct adm9240_data *data = i2c_get_clientdata(client);
457 unsigned long val = simple_strtol(buf, NULL, 10);
459 mutex_lock(&data->update_lock);
460 data->aout = AOUT_TO_REG(val);
461 i2c_smbus_write_byte_data(client, ADM9240_REG_ANALOG_OUT, data->aout);
462 mutex_unlock(&data->update_lock);
463 return count;
465 static DEVICE_ATTR(aout_output, S_IRUGO | S_IWUSR, show_aout, set_aout);
467 /* chassis_clear */
468 static ssize_t chassis_clear(struct device *dev,
469 struct device_attribute *attr,
470 const char *buf, size_t count)
472 struct i2c_client *client = to_i2c_client(dev);
473 unsigned long val = simple_strtol(buf, NULL, 10);
475 if (val == 1) {
476 i2c_smbus_write_byte_data(client,
477 ADM9240_REG_CHASSIS_CLEAR, 0x80);
478 dev_dbg(&client->dev, "chassis intrusion latch cleared\n");
480 return count;
482 static DEVICE_ATTR(chassis_clear, S_IWUSR, NULL, chassis_clear);
484 static struct attribute *adm9240_attributes[] = {
485 &sensor_dev_attr_in0_input.dev_attr.attr,
486 &sensor_dev_attr_in0_min.dev_attr.attr,
487 &sensor_dev_attr_in0_max.dev_attr.attr,
488 &sensor_dev_attr_in0_alarm.dev_attr.attr,
489 &sensor_dev_attr_in1_input.dev_attr.attr,
490 &sensor_dev_attr_in1_min.dev_attr.attr,
491 &sensor_dev_attr_in1_max.dev_attr.attr,
492 &sensor_dev_attr_in1_alarm.dev_attr.attr,
493 &sensor_dev_attr_in2_input.dev_attr.attr,
494 &sensor_dev_attr_in2_min.dev_attr.attr,
495 &sensor_dev_attr_in2_max.dev_attr.attr,
496 &sensor_dev_attr_in2_alarm.dev_attr.attr,
497 &sensor_dev_attr_in3_input.dev_attr.attr,
498 &sensor_dev_attr_in3_min.dev_attr.attr,
499 &sensor_dev_attr_in3_max.dev_attr.attr,
500 &sensor_dev_attr_in3_alarm.dev_attr.attr,
501 &sensor_dev_attr_in4_input.dev_attr.attr,
502 &sensor_dev_attr_in4_min.dev_attr.attr,
503 &sensor_dev_attr_in4_max.dev_attr.attr,
504 &sensor_dev_attr_in4_alarm.dev_attr.attr,
505 &sensor_dev_attr_in5_input.dev_attr.attr,
506 &sensor_dev_attr_in5_min.dev_attr.attr,
507 &sensor_dev_attr_in5_max.dev_attr.attr,
508 &sensor_dev_attr_in5_alarm.dev_attr.attr,
509 &dev_attr_temp1_input.attr,
510 &sensor_dev_attr_temp1_max.dev_attr.attr,
511 &sensor_dev_attr_temp1_max_hyst.dev_attr.attr,
512 &sensor_dev_attr_temp1_alarm.dev_attr.attr,
513 &sensor_dev_attr_fan1_input.dev_attr.attr,
514 &sensor_dev_attr_fan1_div.dev_attr.attr,
515 &sensor_dev_attr_fan1_min.dev_attr.attr,
516 &sensor_dev_attr_fan1_alarm.dev_attr.attr,
517 &sensor_dev_attr_fan2_input.dev_attr.attr,
518 &sensor_dev_attr_fan2_div.dev_attr.attr,
519 &sensor_dev_attr_fan2_min.dev_attr.attr,
520 &sensor_dev_attr_fan2_alarm.dev_attr.attr,
521 &dev_attr_alarms.attr,
522 &dev_attr_aout_output.attr,
523 &dev_attr_chassis_clear.attr,
524 &dev_attr_cpu0_vid.attr,
525 NULL
528 static const struct attribute_group adm9240_group = {
529 .attrs = adm9240_attributes,
533 /*** sensor chip detect and driver install ***/
535 static int adm9240_detect(struct i2c_adapter *adapter, int address, int kind)
537 struct i2c_client *new_client;
538 struct adm9240_data *data;
539 int err = 0;
540 const char *name = "";
541 u8 man_id, die_rev;
543 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
544 goto exit;
546 if (!(data = kzalloc(sizeof(*data), GFP_KERNEL))) {
547 err = -ENOMEM;
548 goto exit;
551 new_client = &data->client;
552 i2c_set_clientdata(new_client, data);
553 new_client->addr = address;
554 new_client->adapter = adapter;
555 new_client->driver = &adm9240_driver;
556 new_client->flags = 0;
558 if (kind == 0) {
559 kind = adm9240;
562 if (kind < 0) {
564 /* verify chip: reg address should match i2c address */
565 if (i2c_smbus_read_byte_data(new_client, ADM9240_REG_I2C_ADDR)
566 != address) {
567 dev_err(&adapter->dev, "detect fail: address match, "
568 "0x%02x\n", address);
569 goto exit_free;
572 /* check known chip manufacturer */
573 man_id = i2c_smbus_read_byte_data(new_client,
574 ADM9240_REG_MAN_ID);
575 if (man_id == 0x23) {
576 kind = adm9240;
577 } else if (man_id == 0xda) {
578 kind = ds1780;
579 } else if (man_id == 0x01) {
580 kind = lm81;
581 } else {
582 dev_err(&adapter->dev, "detect fail: unknown manuf, "
583 "0x%02x\n", man_id);
584 goto exit_free;
587 /* successful detect, print chip info */
588 die_rev = i2c_smbus_read_byte_data(new_client,
589 ADM9240_REG_DIE_REV);
590 dev_info(&adapter->dev, "found %s revision %u\n",
591 man_id == 0x23 ? "ADM9240" :
592 man_id == 0xda ? "DS1780" : "LM81", die_rev);
595 /* either forced or detected chip kind */
596 if (kind == adm9240) {
597 name = "adm9240";
598 } else if (kind == ds1780) {
599 name = "ds1780";
600 } else if (kind == lm81) {
601 name = "lm81";
604 /* fill in the remaining client fields and attach */
605 strlcpy(new_client->name, name, I2C_NAME_SIZE);
606 data->type = kind;
607 mutex_init(&data->update_lock);
609 if ((err = i2c_attach_client(new_client)))
610 goto exit_free;
612 adm9240_init_client(new_client);
614 /* populate sysfs filesystem */
615 if ((err = sysfs_create_group(&new_client->dev.kobj, &adm9240_group)))
616 goto exit_detach;
618 data->hwmon_dev = hwmon_device_register(&new_client->dev);
619 if (IS_ERR(data->hwmon_dev)) {
620 err = PTR_ERR(data->hwmon_dev);
621 goto exit_remove;
624 return 0;
626 exit_remove:
627 sysfs_remove_group(&new_client->dev.kobj, &adm9240_group);
628 exit_detach:
629 i2c_detach_client(new_client);
630 exit_free:
631 kfree(data);
632 exit:
633 return err;
636 static int adm9240_attach_adapter(struct i2c_adapter *adapter)
638 if (!(adapter->class & I2C_CLASS_HWMON))
639 return 0;
640 return i2c_probe(adapter, &addr_data, adm9240_detect);
643 static int adm9240_detach_client(struct i2c_client *client)
645 struct adm9240_data *data = i2c_get_clientdata(client);
646 int err;
648 hwmon_device_unregister(data->hwmon_dev);
649 sysfs_remove_group(&client->dev.kobj, &adm9240_group);
651 if ((err = i2c_detach_client(client)))
652 return err;
654 kfree(data);
655 return 0;
658 static void adm9240_init_client(struct i2c_client *client)
660 struct adm9240_data *data = i2c_get_clientdata(client);
661 u8 conf = i2c_smbus_read_byte_data(client, ADM9240_REG_CONFIG);
662 u8 mode = i2c_smbus_read_byte_data(client, ADM9240_REG_TEMP_CONF) & 3;
664 data->vrm = vid_which_vrm(); /* need this to report vid as mV */
666 dev_info(&client->dev, "Using VRM: %d.%d\n", data->vrm / 10,
667 data->vrm % 10);
669 if (conf & 1) { /* measurement cycle running: report state */
671 dev_info(&client->dev, "status: config 0x%02x mode %u\n",
672 conf, mode);
674 } else { /* cold start: open limits before starting chip */
675 int i;
677 for (i = 0; i < 6; i++)
679 i2c_smbus_write_byte_data(client,
680 ADM9240_REG_IN_MIN(i), 0);
681 i2c_smbus_write_byte_data(client,
682 ADM9240_REG_IN_MAX(i), 255);
684 i2c_smbus_write_byte_data(client,
685 ADM9240_REG_FAN_MIN(0), 255);
686 i2c_smbus_write_byte_data(client,
687 ADM9240_REG_FAN_MIN(1), 255);
688 i2c_smbus_write_byte_data(client,
689 ADM9240_REG_TEMP_MAX(0), 127);
690 i2c_smbus_write_byte_data(client,
691 ADM9240_REG_TEMP_MAX(1), 127);
693 /* start measurement cycle */
694 i2c_smbus_write_byte_data(client, ADM9240_REG_CONFIG, 1);
696 dev_info(&client->dev, "cold start: config was 0x%02x "
697 "mode %u\n", conf, mode);
701 static struct adm9240_data *adm9240_update_device(struct device *dev)
703 struct i2c_client *client = to_i2c_client(dev);
704 struct adm9240_data *data = i2c_get_clientdata(client);
705 int i;
707 mutex_lock(&data->update_lock);
709 /* minimum measurement cycle: 1.75 seconds */
710 if (time_after(jiffies, data->last_updated_measure + (HZ * 7 / 4))
711 || !data->valid) {
713 for (i = 0; i < 6; i++) /* read voltages */
715 data->in[i] = i2c_smbus_read_byte_data(client,
716 ADM9240_REG_IN(i));
718 data->alarms = i2c_smbus_read_byte_data(client,
719 ADM9240_REG_INT(0)) |
720 i2c_smbus_read_byte_data(client,
721 ADM9240_REG_INT(1)) << 8;
723 /* read temperature: assume temperature changes less than
724 * 0.5'C per two measurement cycles thus ignore possible
725 * but unlikely aliasing error on lsb reading. --Grant */
726 data->temp = ((i2c_smbus_read_byte_data(client,
727 ADM9240_REG_TEMP) << 8) |
728 i2c_smbus_read_byte_data(client,
729 ADM9240_REG_TEMP_CONF)) / 128;
731 for (i = 0; i < 2; i++) /* read fans */
733 data->fan[i] = i2c_smbus_read_byte_data(client,
734 ADM9240_REG_FAN(i));
736 /* adjust fan clock divider on overflow */
737 if (data->valid && data->fan[i] == 255 &&
738 data->fan_div[i] < 3) {
740 adm9240_write_fan_div(client, i,
741 ++data->fan_div[i]);
743 /* adjust fan_min if active, but not to 0 */
744 if (data->fan_min[i] < 255 &&
745 data->fan_min[i] >= 2)
746 data->fan_min[i] /= 2;
749 data->last_updated_measure = jiffies;
752 /* minimum config reading cycle: 300 seconds */
753 if (time_after(jiffies, data->last_updated_config + (HZ * 300))
754 || !data->valid) {
756 for (i = 0; i < 6; i++)
758 data->in_min[i] = i2c_smbus_read_byte_data(client,
759 ADM9240_REG_IN_MIN(i));
760 data->in_max[i] = i2c_smbus_read_byte_data(client,
761 ADM9240_REG_IN_MAX(i));
763 for (i = 0; i < 2; i++)
765 data->fan_min[i] = i2c_smbus_read_byte_data(client,
766 ADM9240_REG_FAN_MIN(i));
768 data->temp_max[0] = i2c_smbus_read_byte_data(client,
769 ADM9240_REG_TEMP_MAX(0));
770 data->temp_max[1] = i2c_smbus_read_byte_data(client,
771 ADM9240_REG_TEMP_MAX(1));
773 /* read fan divs and 5-bit VID */
774 i = i2c_smbus_read_byte_data(client, ADM9240_REG_VID_FAN_DIV);
775 data->fan_div[0] = (i >> 4) & 3;
776 data->fan_div[1] = (i >> 6) & 3;
777 data->vid = i & 0x0f;
778 data->vid |= (i2c_smbus_read_byte_data(client,
779 ADM9240_REG_VID4) & 1) << 4;
780 /* read analog out */
781 data->aout = i2c_smbus_read_byte_data(client,
782 ADM9240_REG_ANALOG_OUT);
784 data->last_updated_config = jiffies;
785 data->valid = 1;
787 mutex_unlock(&data->update_lock);
788 return data;
791 static int __init sensors_adm9240_init(void)
793 return i2c_add_driver(&adm9240_driver);
796 static void __exit sensors_adm9240_exit(void)
798 i2c_del_driver(&adm9240_driver);
801 MODULE_AUTHOR("Michiel Rook <michiel@grendelproject.nl>, "
802 "Grant Coady <gcoady.lk@gmail.com> and others");
803 MODULE_DESCRIPTION("ADM9240/DS1780/LM81 driver");
804 MODULE_LICENSE("GPL");
806 module_init(sensors_adm9240_init);
807 module_exit(sensors_adm9240_exit);