e1000e: cleanup PARENTHESIS_ALIGNMENT checkpatch checks
[linux/fpc-iii.git] / drivers / hwmon / adm9240.c
blob2416628e0ab1d78a264a4c8bc6a606a0057ed3fb
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 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>
53 #include <linux/jiffies.h>
55 /* Addresses to scan */
56 static const unsigned short normal_i2c[] = { 0x2c, 0x2d, 0x2e, 0x2f,
57 I2C_CLIENT_END };
59 enum chips { 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 clamp_val(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 clamp_val(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 clamp_val(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_probe(struct i2c_client *client,
134 const struct i2c_device_id *id);
135 static int adm9240_detect(struct i2c_client *client,
136 struct i2c_board_info *info);
137 static void adm9240_init_client(struct i2c_client *client);
138 static int adm9240_remove(struct i2c_client *client);
139 static struct adm9240_data *adm9240_update_device(struct device *dev);
141 /* driver data */
142 static const struct i2c_device_id adm9240_id[] = {
143 { "adm9240", adm9240 },
144 { "ds1780", ds1780 },
145 { "lm81", lm81 },
148 MODULE_DEVICE_TABLE(i2c, adm9240_id);
150 static struct i2c_driver adm9240_driver = {
151 .class = I2C_CLASS_HWMON,
152 .driver = {
153 .name = "adm9240",
155 .probe = adm9240_probe,
156 .remove = adm9240_remove,
157 .id_table = adm9240_id,
158 .detect = adm9240_detect,
159 .address_list = normal_i2c,
162 /* per client data */
163 struct adm9240_data {
164 struct device *hwmon_dev;
165 struct mutex update_lock;
166 char valid;
167 unsigned long last_updated_measure;
168 unsigned long last_updated_config;
170 u8 in[6]; /* ro in0_input */
171 u8 in_max[6]; /* rw in0_max */
172 u8 in_min[6]; /* rw in0_min */
173 u8 fan[2]; /* ro fan1_input */
174 u8 fan_min[2]; /* rw fan1_min */
175 u8 fan_div[2]; /* rw fan1_div, read-only accessor */
176 s16 temp; /* ro temp1_input, 9-bit sign-extended */
177 s8 temp_max[2]; /* rw 0 -> temp_max, 1 -> temp_max_hyst */
178 u16 alarms; /* ro alarms */
179 u8 aout; /* rw aout_output */
180 u8 vid; /* ro vid */
181 u8 vrm; /* -- vrm set on startup, no accessor */
184 /*** sysfs accessors ***/
186 /* temperature */
187 static ssize_t show_temp(struct device *dev, struct device_attribute *dummy,
188 char *buf)
190 struct adm9240_data *data = adm9240_update_device(dev);
191 return sprintf(buf, "%d\n", data->temp * 500); /* 9-bit value */
194 static ssize_t show_max(struct device *dev, struct device_attribute *devattr,
195 char *buf)
197 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
198 struct adm9240_data *data = adm9240_update_device(dev);
199 return sprintf(buf, "%d\n", data->temp_max[attr->index] * 1000);
202 static ssize_t set_max(struct device *dev, struct device_attribute *devattr,
203 const char *buf, size_t count)
205 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
206 struct i2c_client *client = to_i2c_client(dev);
207 struct adm9240_data *data = i2c_get_clientdata(client);
208 long val;
209 int err;
211 err = kstrtol(buf, 10, &val);
212 if (err)
213 return err;
215 mutex_lock(&data->update_lock);
216 data->temp_max[attr->index] = TEMP_TO_REG(val);
217 i2c_smbus_write_byte_data(client, ADM9240_REG_TEMP_MAX(attr->index),
218 data->temp_max[attr->index]);
219 mutex_unlock(&data->update_lock);
220 return count;
223 static DEVICE_ATTR(temp1_input, S_IRUGO, show_temp, NULL);
224 static SENSOR_DEVICE_ATTR(temp1_max, S_IWUSR | S_IRUGO,
225 show_max, set_max, 0);
226 static SENSOR_DEVICE_ATTR(temp1_max_hyst, S_IWUSR | S_IRUGO,
227 show_max, set_max, 1);
229 /* voltage */
230 static ssize_t show_in(struct device *dev, struct device_attribute *devattr,
231 char *buf)
233 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
234 struct adm9240_data *data = adm9240_update_device(dev);
235 return sprintf(buf, "%d\n", IN_FROM_REG(data->in[attr->index],
236 attr->index));
239 static ssize_t show_in_min(struct device *dev,
240 struct device_attribute *devattr, char *buf)
242 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
243 struct adm9240_data *data = adm9240_update_device(dev);
244 return sprintf(buf, "%d\n", IN_FROM_REG(data->in_min[attr->index],
245 attr->index));
248 static ssize_t show_in_max(struct device *dev,
249 struct device_attribute *devattr, char *buf)
251 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
252 struct adm9240_data *data = adm9240_update_device(dev);
253 return sprintf(buf, "%d\n", IN_FROM_REG(data->in_max[attr->index],
254 attr->index));
257 static ssize_t set_in_min(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;
265 int err;
267 err = kstrtoul(buf, 10, &val);
268 if (err)
269 return err;
271 mutex_lock(&data->update_lock);
272 data->in_min[attr->index] = IN_TO_REG(val, attr->index);
273 i2c_smbus_write_byte_data(client, ADM9240_REG_IN_MIN(attr->index),
274 data->in_min[attr->index]);
275 mutex_unlock(&data->update_lock);
276 return count;
279 static ssize_t set_in_max(struct device *dev,
280 struct device_attribute *devattr,
281 const char *buf, size_t count)
283 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
284 struct i2c_client *client = to_i2c_client(dev);
285 struct adm9240_data *data = i2c_get_clientdata(client);
286 unsigned long val;
287 int err;
289 err = kstrtoul(buf, 10, &val);
290 if (err)
291 return err;
293 mutex_lock(&data->update_lock);
294 data->in_max[attr->index] = IN_TO_REG(val, attr->index);
295 i2c_smbus_write_byte_data(client, ADM9240_REG_IN_MAX(attr->index),
296 data->in_max[attr->index]);
297 mutex_unlock(&data->update_lock);
298 return count;
301 #define vin(nr) \
302 static SENSOR_DEVICE_ATTR(in##nr##_input, S_IRUGO, \
303 show_in, NULL, nr); \
304 static SENSOR_DEVICE_ATTR(in##nr##_min, S_IRUGO | S_IWUSR, \
305 show_in_min, set_in_min, nr); \
306 static SENSOR_DEVICE_ATTR(in##nr##_max, S_IRUGO | S_IWUSR, \
307 show_in_max, set_in_max, nr);
309 vin(0);
310 vin(1);
311 vin(2);
312 vin(3);
313 vin(4);
314 vin(5);
316 /* fans */
317 static ssize_t show_fan(struct device *dev,
318 struct device_attribute *devattr, char *buf)
320 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
321 struct adm9240_data *data = adm9240_update_device(dev);
322 return sprintf(buf, "%d\n", FAN_FROM_REG(data->fan[attr->index],
323 1 << data->fan_div[attr->index]));
326 static ssize_t show_fan_min(struct device *dev,
327 struct device_attribute *devattr, char *buf)
329 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
330 struct adm9240_data *data = adm9240_update_device(dev);
331 return sprintf(buf, "%d\n", FAN_FROM_REG(data->fan_min[attr->index],
332 1 << data->fan_div[attr->index]));
335 static ssize_t show_fan_div(struct device *dev,
336 struct device_attribute *devattr, char *buf)
338 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
339 struct adm9240_data *data = adm9240_update_device(dev);
340 return sprintf(buf, "%d\n", 1 << data->fan_div[attr->index]);
343 /* write new fan div, callers must hold data->update_lock */
344 static void adm9240_write_fan_div(struct i2c_client *client, int nr,
345 u8 fan_div)
347 u8 reg, old, shift = (nr + 2) * 2;
349 reg = i2c_smbus_read_byte_data(client, ADM9240_REG_VID_FAN_DIV);
350 old = (reg >> shift) & 3;
351 reg &= ~(3 << shift);
352 reg |= (fan_div << shift);
353 i2c_smbus_write_byte_data(client, ADM9240_REG_VID_FAN_DIV, reg);
354 dev_dbg(&client->dev, "fan%d clock divider changed from %u "
355 "to %u\n", nr + 1, 1 << old, 1 << fan_div);
359 * set fan speed low limit:
361 * - value is zero: disable fan speed low limit alarm
363 * - value is below fan speed measurement range: enable fan speed low
364 * limit alarm to be asserted while fan speed too slow to measure
366 * - otherwise: select fan clock divider to suit fan speed low limit,
367 * measurement code may adjust registers to ensure fan speed reading
369 static ssize_t set_fan_min(struct device *dev,
370 struct device_attribute *devattr,
371 const char *buf, size_t count)
373 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
374 struct i2c_client *client = to_i2c_client(dev);
375 struct adm9240_data *data = i2c_get_clientdata(client);
376 int nr = attr->index;
377 u8 new_div;
378 unsigned long val;
379 int err;
381 err = kstrtoul(buf, 10, &val);
382 if (err)
383 return err;
385 mutex_lock(&data->update_lock);
387 if (!val) {
388 data->fan_min[nr] = 255;
389 new_div = data->fan_div[nr];
391 dev_dbg(&client->dev, "fan%u low limit set disabled\n",
392 nr + 1);
394 } else if (val < 1350000 / (8 * 254)) {
395 new_div = 3;
396 data->fan_min[nr] = 254;
398 dev_dbg(&client->dev, "fan%u low limit set minimum %u\n",
399 nr + 1, FAN_FROM_REG(254, 1 << new_div));
401 } else {
402 unsigned int new_min = 1350000 / val;
404 new_div = 0;
405 while (new_min > 192 && new_div < 3) {
406 new_div++;
407 new_min /= 2;
409 if (!new_min) /* keep > 0 */
410 new_min++;
412 data->fan_min[nr] = new_min;
414 dev_dbg(&client->dev, "fan%u low limit set fan speed %u\n",
415 nr + 1, FAN_FROM_REG(new_min, 1 << new_div));
418 if (new_div != data->fan_div[nr]) {
419 data->fan_div[nr] = new_div;
420 adm9240_write_fan_div(client, nr, new_div);
422 i2c_smbus_write_byte_data(client, ADM9240_REG_FAN_MIN(nr),
423 data->fan_min[nr]);
425 mutex_unlock(&data->update_lock);
426 return count;
429 #define fan(nr) \
430 static SENSOR_DEVICE_ATTR(fan##nr##_input, S_IRUGO, \
431 show_fan, NULL, nr - 1); \
432 static SENSOR_DEVICE_ATTR(fan##nr##_div, S_IRUGO, \
433 show_fan_div, NULL, nr - 1); \
434 static SENSOR_DEVICE_ATTR(fan##nr##_min, S_IRUGO | S_IWUSR, \
435 show_fan_min, set_fan_min, nr - 1);
437 fan(1);
438 fan(2);
440 /* alarms */
441 static ssize_t show_alarms(struct device *dev,
442 struct device_attribute *attr, char *buf)
444 struct adm9240_data *data = adm9240_update_device(dev);
445 return sprintf(buf, "%u\n", data->alarms);
447 static DEVICE_ATTR(alarms, S_IRUGO, show_alarms, NULL);
449 static ssize_t show_alarm(struct device *dev,
450 struct device_attribute *attr, char *buf)
452 int bitnr = to_sensor_dev_attr(attr)->index;
453 struct adm9240_data *data = adm9240_update_device(dev);
454 return sprintf(buf, "%u\n", (data->alarms >> bitnr) & 1);
456 static SENSOR_DEVICE_ATTR(in0_alarm, S_IRUGO, show_alarm, NULL, 0);
457 static SENSOR_DEVICE_ATTR(in1_alarm, S_IRUGO, show_alarm, NULL, 1);
458 static SENSOR_DEVICE_ATTR(in2_alarm, S_IRUGO, show_alarm, NULL, 2);
459 static SENSOR_DEVICE_ATTR(in3_alarm, S_IRUGO, show_alarm, NULL, 3);
460 static SENSOR_DEVICE_ATTR(in4_alarm, S_IRUGO, show_alarm, NULL, 8);
461 static SENSOR_DEVICE_ATTR(in5_alarm, S_IRUGO, show_alarm, NULL, 9);
462 static SENSOR_DEVICE_ATTR(temp1_alarm, S_IRUGO, show_alarm, NULL, 4);
463 static SENSOR_DEVICE_ATTR(fan1_alarm, S_IRUGO, show_alarm, NULL, 6);
464 static SENSOR_DEVICE_ATTR(fan2_alarm, S_IRUGO, show_alarm, NULL, 7);
466 /* vid */
467 static ssize_t show_vid(struct device *dev,
468 struct device_attribute *attr, char *buf)
470 struct adm9240_data *data = adm9240_update_device(dev);
471 return sprintf(buf, "%d\n", vid_from_reg(data->vid, data->vrm));
473 static DEVICE_ATTR(cpu0_vid, S_IRUGO, show_vid, NULL);
475 /* analog output */
476 static ssize_t show_aout(struct device *dev,
477 struct device_attribute *attr, char *buf)
479 struct adm9240_data *data = adm9240_update_device(dev);
480 return sprintf(buf, "%d\n", AOUT_FROM_REG(data->aout));
483 static ssize_t set_aout(struct device *dev,
484 struct device_attribute *attr,
485 const char *buf, size_t count)
487 struct i2c_client *client = to_i2c_client(dev);
488 struct adm9240_data *data = i2c_get_clientdata(client);
489 long val;
490 int err;
492 err = kstrtol(buf, 10, &val);
493 if (err)
494 return err;
496 mutex_lock(&data->update_lock);
497 data->aout = AOUT_TO_REG(val);
498 i2c_smbus_write_byte_data(client, ADM9240_REG_ANALOG_OUT, data->aout);
499 mutex_unlock(&data->update_lock);
500 return count;
502 static DEVICE_ATTR(aout_output, S_IRUGO | S_IWUSR, show_aout, set_aout);
504 static ssize_t chassis_clear(struct device *dev,
505 struct device_attribute *attr,
506 const char *buf, size_t count)
508 struct i2c_client *client = to_i2c_client(dev);
509 struct adm9240_data *data = i2c_get_clientdata(client);
510 unsigned long val;
512 if (kstrtoul(buf, 10, &val) || val != 0)
513 return -EINVAL;
515 mutex_lock(&data->update_lock);
516 i2c_smbus_write_byte_data(client, ADM9240_REG_CHASSIS_CLEAR, 0x80);
517 data->valid = 0; /* Force cache refresh */
518 mutex_unlock(&data->update_lock);
519 dev_dbg(&client->dev, "chassis intrusion latch cleared\n");
521 return count;
523 static SENSOR_DEVICE_ATTR(intrusion0_alarm, S_IRUGO | S_IWUSR, show_alarm,
524 chassis_clear, 12);
526 static struct attribute *adm9240_attributes[] = {
527 &sensor_dev_attr_in0_input.dev_attr.attr,
528 &sensor_dev_attr_in0_min.dev_attr.attr,
529 &sensor_dev_attr_in0_max.dev_attr.attr,
530 &sensor_dev_attr_in0_alarm.dev_attr.attr,
531 &sensor_dev_attr_in1_input.dev_attr.attr,
532 &sensor_dev_attr_in1_min.dev_attr.attr,
533 &sensor_dev_attr_in1_max.dev_attr.attr,
534 &sensor_dev_attr_in1_alarm.dev_attr.attr,
535 &sensor_dev_attr_in2_input.dev_attr.attr,
536 &sensor_dev_attr_in2_min.dev_attr.attr,
537 &sensor_dev_attr_in2_max.dev_attr.attr,
538 &sensor_dev_attr_in2_alarm.dev_attr.attr,
539 &sensor_dev_attr_in3_input.dev_attr.attr,
540 &sensor_dev_attr_in3_min.dev_attr.attr,
541 &sensor_dev_attr_in3_max.dev_attr.attr,
542 &sensor_dev_attr_in3_alarm.dev_attr.attr,
543 &sensor_dev_attr_in4_input.dev_attr.attr,
544 &sensor_dev_attr_in4_min.dev_attr.attr,
545 &sensor_dev_attr_in4_max.dev_attr.attr,
546 &sensor_dev_attr_in4_alarm.dev_attr.attr,
547 &sensor_dev_attr_in5_input.dev_attr.attr,
548 &sensor_dev_attr_in5_min.dev_attr.attr,
549 &sensor_dev_attr_in5_max.dev_attr.attr,
550 &sensor_dev_attr_in5_alarm.dev_attr.attr,
551 &dev_attr_temp1_input.attr,
552 &sensor_dev_attr_temp1_max.dev_attr.attr,
553 &sensor_dev_attr_temp1_max_hyst.dev_attr.attr,
554 &sensor_dev_attr_temp1_alarm.dev_attr.attr,
555 &sensor_dev_attr_fan1_input.dev_attr.attr,
556 &sensor_dev_attr_fan1_div.dev_attr.attr,
557 &sensor_dev_attr_fan1_min.dev_attr.attr,
558 &sensor_dev_attr_fan1_alarm.dev_attr.attr,
559 &sensor_dev_attr_fan2_input.dev_attr.attr,
560 &sensor_dev_attr_fan2_div.dev_attr.attr,
561 &sensor_dev_attr_fan2_min.dev_attr.attr,
562 &sensor_dev_attr_fan2_alarm.dev_attr.attr,
563 &dev_attr_alarms.attr,
564 &dev_attr_aout_output.attr,
565 &sensor_dev_attr_intrusion0_alarm.dev_attr.attr,
566 &dev_attr_cpu0_vid.attr,
567 NULL
570 static const struct attribute_group adm9240_group = {
571 .attrs = adm9240_attributes,
575 /*** sensor chip detect and driver install ***/
577 /* Return 0 if detection is successful, -ENODEV otherwise */
578 static int adm9240_detect(struct i2c_client *new_client,
579 struct i2c_board_info *info)
581 struct i2c_adapter *adapter = new_client->adapter;
582 const char *name = "";
583 int address = new_client->addr;
584 u8 man_id, die_rev;
586 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
587 return -ENODEV;
589 /* verify chip: reg address should match i2c address */
590 if (i2c_smbus_read_byte_data(new_client, ADM9240_REG_I2C_ADDR)
591 != address) {
592 dev_err(&adapter->dev, "detect fail: address match, 0x%02x\n",
593 address);
594 return -ENODEV;
597 /* check known chip manufacturer */
598 man_id = i2c_smbus_read_byte_data(new_client, ADM9240_REG_MAN_ID);
599 if (man_id == 0x23) {
600 name = "adm9240";
601 } else if (man_id == 0xda) {
602 name = "ds1780";
603 } else if (man_id == 0x01) {
604 name = "lm81";
605 } else {
606 dev_err(&adapter->dev, "detect fail: unknown manuf, 0x%02x\n",
607 man_id);
608 return -ENODEV;
611 /* successful detect, print chip info */
612 die_rev = i2c_smbus_read_byte_data(new_client, ADM9240_REG_DIE_REV);
613 dev_info(&adapter->dev, "found %s revision %u\n",
614 man_id == 0x23 ? "ADM9240" :
615 man_id == 0xda ? "DS1780" : "LM81", die_rev);
617 strlcpy(info->type, name, I2C_NAME_SIZE);
619 return 0;
622 static int adm9240_probe(struct i2c_client *new_client,
623 const struct i2c_device_id *id)
625 struct adm9240_data *data;
626 int err;
628 data = devm_kzalloc(&new_client->dev, sizeof(*data), GFP_KERNEL);
629 if (!data)
630 return -ENOMEM;
632 i2c_set_clientdata(new_client, data);
633 mutex_init(&data->update_lock);
635 adm9240_init_client(new_client);
637 /* populate sysfs filesystem */
638 err = sysfs_create_group(&new_client->dev.kobj, &adm9240_group);
639 if (err)
640 return err;
642 data->hwmon_dev = hwmon_device_register(&new_client->dev);
643 if (IS_ERR(data->hwmon_dev)) {
644 err = PTR_ERR(data->hwmon_dev);
645 goto exit_remove;
648 return 0;
650 exit_remove:
651 sysfs_remove_group(&new_client->dev.kobj, &adm9240_group);
652 return err;
655 static int adm9240_remove(struct i2c_client *client)
657 struct adm9240_data *data = i2c_get_clientdata(client);
659 hwmon_device_unregister(data->hwmon_dev);
660 sysfs_remove_group(&client->dev.kobj, &adm9240_group);
662 return 0;
665 static void adm9240_init_client(struct i2c_client *client)
667 struct adm9240_data *data = i2c_get_clientdata(client);
668 u8 conf = i2c_smbus_read_byte_data(client, ADM9240_REG_CONFIG);
669 u8 mode = i2c_smbus_read_byte_data(client, ADM9240_REG_TEMP_CONF) & 3;
671 data->vrm = vid_which_vrm(); /* need this to report vid as mV */
673 dev_info(&client->dev, "Using VRM: %d.%d\n", data->vrm / 10,
674 data->vrm % 10);
676 if (conf & 1) { /* measurement cycle running: report state */
678 dev_info(&client->dev, "status: config 0x%02x mode %u\n",
679 conf, mode);
681 } else { /* cold start: open limits before starting chip */
682 int i;
684 for (i = 0; i < 6; i++) {
685 i2c_smbus_write_byte_data(client,
686 ADM9240_REG_IN_MIN(i), 0);
687 i2c_smbus_write_byte_data(client,
688 ADM9240_REG_IN_MAX(i), 255);
690 i2c_smbus_write_byte_data(client,
691 ADM9240_REG_FAN_MIN(0), 255);
692 i2c_smbus_write_byte_data(client,
693 ADM9240_REG_FAN_MIN(1), 255);
694 i2c_smbus_write_byte_data(client,
695 ADM9240_REG_TEMP_MAX(0), 127);
696 i2c_smbus_write_byte_data(client,
697 ADM9240_REG_TEMP_MAX(1), 127);
699 /* start measurement cycle */
700 i2c_smbus_write_byte_data(client, ADM9240_REG_CONFIG, 1);
702 dev_info(&client->dev, "cold start: config was 0x%02x "
703 "mode %u\n", conf, mode);
707 static struct adm9240_data *adm9240_update_device(struct device *dev)
709 struct i2c_client *client = to_i2c_client(dev);
710 struct adm9240_data *data = i2c_get_clientdata(client);
711 int i;
713 mutex_lock(&data->update_lock);
715 /* minimum measurement cycle: 1.75 seconds */
716 if (time_after(jiffies, data->last_updated_measure + (HZ * 7 / 4))
717 || !data->valid) {
719 for (i = 0; i < 6; i++) { /* read voltages */
720 data->in[i] = i2c_smbus_read_byte_data(client,
721 ADM9240_REG_IN(i));
723 data->alarms = i2c_smbus_read_byte_data(client,
724 ADM9240_REG_INT(0)) |
725 i2c_smbus_read_byte_data(client,
726 ADM9240_REG_INT(1)) << 8;
729 * read temperature: assume temperature changes less than
730 * 0.5'C per two measurement cycles thus ignore possible
731 * but unlikely aliasing error on lsb reading. --Grant
733 data->temp = ((i2c_smbus_read_byte_data(client,
734 ADM9240_REG_TEMP) << 8) |
735 i2c_smbus_read_byte_data(client,
736 ADM9240_REG_TEMP_CONF)) / 128;
738 for (i = 0; i < 2; i++) { /* read fans */
739 data->fan[i] = i2c_smbus_read_byte_data(client,
740 ADM9240_REG_FAN(i));
742 /* adjust fan clock divider on overflow */
743 if (data->valid && data->fan[i] == 255 &&
744 data->fan_div[i] < 3) {
746 adm9240_write_fan_div(client, i,
747 ++data->fan_div[i]);
749 /* adjust fan_min if active, but not to 0 */
750 if (data->fan_min[i] < 255 &&
751 data->fan_min[i] >= 2)
752 data->fan_min[i] /= 2;
755 data->last_updated_measure = jiffies;
758 /* minimum config reading cycle: 300 seconds */
759 if (time_after(jiffies, data->last_updated_config + (HZ * 300))
760 || !data->valid) {
762 for (i = 0; i < 6; i++) {
763 data->in_min[i] = i2c_smbus_read_byte_data(client,
764 ADM9240_REG_IN_MIN(i));
765 data->in_max[i] = i2c_smbus_read_byte_data(client,
766 ADM9240_REG_IN_MAX(i));
768 for (i = 0; i < 2; i++) {
769 data->fan_min[i] = i2c_smbus_read_byte_data(client,
770 ADM9240_REG_FAN_MIN(i));
772 data->temp_max[0] = i2c_smbus_read_byte_data(client,
773 ADM9240_REG_TEMP_MAX(0));
774 data->temp_max[1] = i2c_smbus_read_byte_data(client,
775 ADM9240_REG_TEMP_MAX(1));
777 /* read fan divs and 5-bit VID */
778 i = i2c_smbus_read_byte_data(client, ADM9240_REG_VID_FAN_DIV);
779 data->fan_div[0] = (i >> 4) & 3;
780 data->fan_div[1] = (i >> 6) & 3;
781 data->vid = i & 0x0f;
782 data->vid |= (i2c_smbus_read_byte_data(client,
783 ADM9240_REG_VID4) & 1) << 4;
784 /* read analog out */
785 data->aout = i2c_smbus_read_byte_data(client,
786 ADM9240_REG_ANALOG_OUT);
788 data->last_updated_config = jiffies;
789 data->valid = 1;
791 mutex_unlock(&data->update_lock);
792 return data;
795 module_i2c_driver(adm9240_driver);
797 MODULE_AUTHOR("Michiel Rook <michiel@grendelproject.nl>, "
798 "Grant Coady <gcoady.lk@gmail.com> and others");
799 MODULE_DESCRIPTION("ADM9240/DS1780/LM81 driver");
800 MODULE_LICENSE("GPL");