Avoid beyond bounds copy while caching ACL
[zen-stable.git] / drivers / hwmon / adm9240.c
blob3f63f5f9741d3b10165f76901f5c138a45d96f1d
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>
54 /* Addresses to scan */
55 static const unsigned short normal_i2c[] = { 0x2c, 0x2d, 0x2e, 0x2f,
56 I2C_CLIENT_END };
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)
84 if (val < 0)
85 return (val * mul - div / 2) / div;
86 else
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 */
113 return -1;
115 if (reg == 255)
116 return 0;
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);
140 /* driver data */
141 static const struct i2c_device_id adm9240_id[] = {
142 { "adm9240", adm9240 },
143 { "ds1780", ds1780 },
144 { "lm81", lm81 },
147 MODULE_DEVICE_TABLE(i2c, adm9240_id);
149 static struct i2c_driver adm9240_driver = {
150 .class = I2C_CLASS_HWMON,
151 .driver = {
152 .name = "adm9240",
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;
165 char valid;
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 */
179 u8 vid; /* ro vid */
180 u8 vrm; /* -- vrm set on startup, no accessor */
183 /*** sysfs accessors ***/
185 /* temperature */
186 static ssize_t show_temp(struct device *dev, struct device_attribute *dummy,
187 char *buf)
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,
194 char *buf)
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);
207 long val = simple_strtol(buf, NULL, 10);
209 mutex_lock(&data->update_lock);
210 data->temp_max[attr->index] = TEMP_TO_REG(val);
211 i2c_smbus_write_byte_data(client, ADM9240_REG_TEMP_MAX(attr->index),
212 data->temp_max[attr->index]);
213 mutex_unlock(&data->update_lock);
214 return count;
217 static DEVICE_ATTR(temp1_input, S_IRUGO, show_temp, NULL);
218 static SENSOR_DEVICE_ATTR(temp1_max, S_IWUSR | S_IRUGO,
219 show_max, set_max, 0);
220 static SENSOR_DEVICE_ATTR(temp1_max_hyst, S_IWUSR | S_IRUGO,
221 show_max, set_max, 1);
223 /* voltage */
224 static ssize_t show_in(struct device *dev, struct device_attribute *devattr,
225 char *buf)
227 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
228 struct adm9240_data *data = adm9240_update_device(dev);
229 return sprintf(buf, "%d\n", IN_FROM_REG(data->in[attr->index],
230 attr->index));
233 static ssize_t show_in_min(struct device *dev,
234 struct device_attribute *devattr, char *buf)
236 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
237 struct adm9240_data *data = adm9240_update_device(dev);
238 return sprintf(buf, "%d\n", IN_FROM_REG(data->in_min[attr->index],
239 attr->index));
242 static ssize_t show_in_max(struct device *dev,
243 struct device_attribute *devattr, char *buf)
245 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
246 struct adm9240_data *data = adm9240_update_device(dev);
247 return sprintf(buf, "%d\n", IN_FROM_REG(data->in_max[attr->index],
248 attr->index));
251 static ssize_t set_in_min(struct device *dev,
252 struct device_attribute *devattr,
253 const char *buf, size_t count)
255 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
256 struct i2c_client *client = to_i2c_client(dev);
257 struct adm9240_data *data = i2c_get_clientdata(client);
258 unsigned long val = simple_strtoul(buf, NULL, 10);
260 mutex_lock(&data->update_lock);
261 data->in_min[attr->index] = IN_TO_REG(val, attr->index);
262 i2c_smbus_write_byte_data(client, ADM9240_REG_IN_MIN(attr->index),
263 data->in_min[attr->index]);
264 mutex_unlock(&data->update_lock);
265 return count;
268 static ssize_t set_in_max(struct device *dev,
269 struct device_attribute *devattr,
270 const char *buf, size_t count)
272 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
273 struct i2c_client *client = to_i2c_client(dev);
274 struct adm9240_data *data = i2c_get_clientdata(client);
275 unsigned long val = simple_strtoul(buf, NULL, 10);
277 mutex_lock(&data->update_lock);
278 data->in_max[attr->index] = IN_TO_REG(val, attr->index);
279 i2c_smbus_write_byte_data(client, ADM9240_REG_IN_MAX(attr->index),
280 data->in_max[attr->index]);
281 mutex_unlock(&data->update_lock);
282 return count;
285 #define vin(nr) \
286 static SENSOR_DEVICE_ATTR(in##nr##_input, S_IRUGO, \
287 show_in, NULL, nr); \
288 static SENSOR_DEVICE_ATTR(in##nr##_min, S_IRUGO | S_IWUSR, \
289 show_in_min, set_in_min, nr); \
290 static SENSOR_DEVICE_ATTR(in##nr##_max, S_IRUGO | S_IWUSR, \
291 show_in_max, set_in_max, nr);
293 vin(0);
294 vin(1);
295 vin(2);
296 vin(3);
297 vin(4);
298 vin(5);
300 /* fans */
301 static ssize_t show_fan(struct device *dev,
302 struct device_attribute *devattr, char *buf)
304 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
305 struct adm9240_data *data = adm9240_update_device(dev);
306 return sprintf(buf, "%d\n", FAN_FROM_REG(data->fan[attr->index],
307 1 << data->fan_div[attr->index]));
310 static ssize_t show_fan_min(struct device *dev,
311 struct device_attribute *devattr, char *buf)
313 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
314 struct adm9240_data *data = adm9240_update_device(dev);
315 return sprintf(buf, "%d\n", FAN_FROM_REG(data->fan_min[attr->index],
316 1 << data->fan_div[attr->index]));
319 static ssize_t show_fan_div(struct device *dev,
320 struct device_attribute *devattr, char *buf)
322 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
323 struct adm9240_data *data = adm9240_update_device(dev);
324 return sprintf(buf, "%d\n", 1 << data->fan_div[attr->index]);
327 /* write new fan div, callers must hold data->update_lock */
328 static void adm9240_write_fan_div(struct i2c_client *client, int nr,
329 u8 fan_div)
331 u8 reg, old, shift = (nr + 2) * 2;
333 reg = i2c_smbus_read_byte_data(client, ADM9240_REG_VID_FAN_DIV);
334 old = (reg >> shift) & 3;
335 reg &= ~(3 << shift);
336 reg |= (fan_div << shift);
337 i2c_smbus_write_byte_data(client, ADM9240_REG_VID_FAN_DIV, reg);
338 dev_dbg(&client->dev, "fan%d clock divider changed from %u "
339 "to %u\n", nr + 1, 1 << old, 1 << fan_div);
343 * set fan speed low limit:
345 * - value is zero: disable fan speed low limit alarm
347 * - value is below fan speed measurement range: enable fan speed low
348 * limit alarm to be asserted while fan speed too slow to measure
350 * - otherwise: select fan clock divider to suit fan speed low limit,
351 * measurement code may adjust registers to ensure fan speed reading
353 static ssize_t set_fan_min(struct device *dev,
354 struct device_attribute *devattr,
355 const char *buf, size_t count)
357 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
358 struct i2c_client *client = to_i2c_client(dev);
359 struct adm9240_data *data = i2c_get_clientdata(client);
360 unsigned long val = simple_strtoul(buf, NULL, 10);
361 int nr = attr->index;
362 u8 new_div;
364 mutex_lock(&data->update_lock);
366 if (!val) {
367 data->fan_min[nr] = 255;
368 new_div = data->fan_div[nr];
370 dev_dbg(&client->dev, "fan%u low limit set disabled\n",
371 nr + 1);
373 } else if (val < 1350000 / (8 * 254)) {
374 new_div = 3;
375 data->fan_min[nr] = 254;
377 dev_dbg(&client->dev, "fan%u low limit set minimum %u\n",
378 nr + 1, FAN_FROM_REG(254, 1 << new_div));
380 } else {
381 unsigned int new_min = 1350000 / val;
383 new_div = 0;
384 while (new_min > 192 && new_div < 3) {
385 new_div++;
386 new_min /= 2;
388 if (!new_min) /* keep > 0 */
389 new_min++;
391 data->fan_min[nr] = new_min;
393 dev_dbg(&client->dev, "fan%u low limit set fan speed %u\n",
394 nr + 1, FAN_FROM_REG(new_min, 1 << new_div));
397 if (new_div != data->fan_div[nr]) {
398 data->fan_div[nr] = new_div;
399 adm9240_write_fan_div(client, nr, new_div);
401 i2c_smbus_write_byte_data(client, ADM9240_REG_FAN_MIN(nr),
402 data->fan_min[nr]);
404 mutex_unlock(&data->update_lock);
405 return count;
408 #define fan(nr) \
409 static SENSOR_DEVICE_ATTR(fan##nr##_input, S_IRUGO, \
410 show_fan, NULL, nr - 1); \
411 static SENSOR_DEVICE_ATTR(fan##nr##_div, S_IRUGO, \
412 show_fan_div, NULL, nr - 1); \
413 static SENSOR_DEVICE_ATTR(fan##nr##_min, S_IRUGO | S_IWUSR, \
414 show_fan_min, set_fan_min, nr - 1);
416 fan(1);
417 fan(2);
419 /* alarms */
420 static ssize_t show_alarms(struct device *dev,
421 struct device_attribute *attr, char *buf)
423 struct adm9240_data *data = adm9240_update_device(dev);
424 return sprintf(buf, "%u\n", data->alarms);
426 static DEVICE_ATTR(alarms, S_IRUGO, show_alarms, NULL);
428 static ssize_t show_alarm(struct device *dev,
429 struct device_attribute *attr, char *buf)
431 int bitnr = to_sensor_dev_attr(attr)->index;
432 struct adm9240_data *data = adm9240_update_device(dev);
433 return sprintf(buf, "%u\n", (data->alarms >> bitnr) & 1);
435 static SENSOR_DEVICE_ATTR(in0_alarm, S_IRUGO, show_alarm, NULL, 0);
436 static SENSOR_DEVICE_ATTR(in1_alarm, S_IRUGO, show_alarm, NULL, 1);
437 static SENSOR_DEVICE_ATTR(in2_alarm, S_IRUGO, show_alarm, NULL, 2);
438 static SENSOR_DEVICE_ATTR(in3_alarm, S_IRUGO, show_alarm, NULL, 3);
439 static SENSOR_DEVICE_ATTR(in4_alarm, S_IRUGO, show_alarm, NULL, 8);
440 static SENSOR_DEVICE_ATTR(in5_alarm, S_IRUGO, show_alarm, NULL, 9);
441 static SENSOR_DEVICE_ATTR(temp1_alarm, S_IRUGO, show_alarm, NULL, 4);
442 static SENSOR_DEVICE_ATTR(fan1_alarm, S_IRUGO, show_alarm, NULL, 6);
443 static SENSOR_DEVICE_ATTR(fan2_alarm, S_IRUGO, show_alarm, NULL, 7);
445 /* vid */
446 static ssize_t show_vid(struct device *dev,
447 struct device_attribute *attr, char *buf)
449 struct adm9240_data *data = adm9240_update_device(dev);
450 return sprintf(buf, "%d\n", vid_from_reg(data->vid, data->vrm));
452 static DEVICE_ATTR(cpu0_vid, S_IRUGO, show_vid, NULL);
454 /* analog output */
455 static ssize_t show_aout(struct device *dev,
456 struct device_attribute *attr, char *buf)
458 struct adm9240_data *data = adm9240_update_device(dev);
459 return sprintf(buf, "%d\n", AOUT_FROM_REG(data->aout));
462 static ssize_t set_aout(struct device *dev,
463 struct device_attribute *attr,
464 const char *buf, size_t count)
466 struct i2c_client *client = to_i2c_client(dev);
467 struct adm9240_data *data = i2c_get_clientdata(client);
468 unsigned long val = simple_strtol(buf, NULL, 10);
470 mutex_lock(&data->update_lock);
471 data->aout = AOUT_TO_REG(val);
472 i2c_smbus_write_byte_data(client, ADM9240_REG_ANALOG_OUT, data->aout);
473 mutex_unlock(&data->update_lock);
474 return count;
476 static DEVICE_ATTR(aout_output, S_IRUGO | S_IWUSR, show_aout, set_aout);
478 /* chassis_clear */
479 static ssize_t chassis_clear_legacy(struct device *dev,
480 struct device_attribute *attr,
481 const char *buf, size_t count)
483 struct i2c_client *client = to_i2c_client(dev);
484 unsigned long val = simple_strtol(buf, NULL, 10);
486 dev_warn(dev, "Attribute chassis_clear is deprecated, "
487 "use intrusion0_alarm instead\n");
489 if (val == 1) {
490 i2c_smbus_write_byte_data(client,
491 ADM9240_REG_CHASSIS_CLEAR, 0x80);
492 dev_dbg(&client->dev, "chassis intrusion latch cleared\n");
494 return count;
496 static DEVICE_ATTR(chassis_clear, S_IWUSR, NULL, chassis_clear_legacy);
498 static ssize_t chassis_clear(struct device *dev,
499 struct device_attribute *attr,
500 const char *buf, size_t count)
502 struct i2c_client *client = to_i2c_client(dev);
503 struct adm9240_data *data = i2c_get_clientdata(client);
504 unsigned long val;
506 if (kstrtoul(buf, 10, &val) || val != 0)
507 return -EINVAL;
509 mutex_lock(&data->update_lock);
510 i2c_smbus_write_byte_data(client, ADM9240_REG_CHASSIS_CLEAR, 0x80);
511 data->valid = 0; /* Force cache refresh */
512 mutex_unlock(&data->update_lock);
513 dev_dbg(&client->dev, "chassis intrusion latch cleared\n");
515 return count;
517 static SENSOR_DEVICE_ATTR(intrusion0_alarm, S_IRUGO | S_IWUSR, show_alarm,
518 chassis_clear, 12);
520 static struct attribute *adm9240_attributes[] = {
521 &sensor_dev_attr_in0_input.dev_attr.attr,
522 &sensor_dev_attr_in0_min.dev_attr.attr,
523 &sensor_dev_attr_in0_max.dev_attr.attr,
524 &sensor_dev_attr_in0_alarm.dev_attr.attr,
525 &sensor_dev_attr_in1_input.dev_attr.attr,
526 &sensor_dev_attr_in1_min.dev_attr.attr,
527 &sensor_dev_attr_in1_max.dev_attr.attr,
528 &sensor_dev_attr_in1_alarm.dev_attr.attr,
529 &sensor_dev_attr_in2_input.dev_attr.attr,
530 &sensor_dev_attr_in2_min.dev_attr.attr,
531 &sensor_dev_attr_in2_max.dev_attr.attr,
532 &sensor_dev_attr_in2_alarm.dev_attr.attr,
533 &sensor_dev_attr_in3_input.dev_attr.attr,
534 &sensor_dev_attr_in3_min.dev_attr.attr,
535 &sensor_dev_attr_in3_max.dev_attr.attr,
536 &sensor_dev_attr_in3_alarm.dev_attr.attr,
537 &sensor_dev_attr_in4_input.dev_attr.attr,
538 &sensor_dev_attr_in4_min.dev_attr.attr,
539 &sensor_dev_attr_in4_max.dev_attr.attr,
540 &sensor_dev_attr_in4_alarm.dev_attr.attr,
541 &sensor_dev_attr_in5_input.dev_attr.attr,
542 &sensor_dev_attr_in5_min.dev_attr.attr,
543 &sensor_dev_attr_in5_max.dev_attr.attr,
544 &sensor_dev_attr_in5_alarm.dev_attr.attr,
545 &dev_attr_temp1_input.attr,
546 &sensor_dev_attr_temp1_max.dev_attr.attr,
547 &sensor_dev_attr_temp1_max_hyst.dev_attr.attr,
548 &sensor_dev_attr_temp1_alarm.dev_attr.attr,
549 &sensor_dev_attr_fan1_input.dev_attr.attr,
550 &sensor_dev_attr_fan1_div.dev_attr.attr,
551 &sensor_dev_attr_fan1_min.dev_attr.attr,
552 &sensor_dev_attr_fan1_alarm.dev_attr.attr,
553 &sensor_dev_attr_fan2_input.dev_attr.attr,
554 &sensor_dev_attr_fan2_div.dev_attr.attr,
555 &sensor_dev_attr_fan2_min.dev_attr.attr,
556 &sensor_dev_attr_fan2_alarm.dev_attr.attr,
557 &dev_attr_alarms.attr,
558 &dev_attr_aout_output.attr,
559 &dev_attr_chassis_clear.attr,
560 &sensor_dev_attr_intrusion0_alarm.dev_attr.attr,
561 &dev_attr_cpu0_vid.attr,
562 NULL
565 static const struct attribute_group adm9240_group = {
566 .attrs = adm9240_attributes,
570 /*** sensor chip detect and driver install ***/
572 /* Return 0 if detection is successful, -ENODEV otherwise */
573 static int adm9240_detect(struct i2c_client *new_client,
574 struct i2c_board_info *info)
576 struct i2c_adapter *adapter = new_client->adapter;
577 const char *name = "";
578 int address = new_client->addr;
579 u8 man_id, die_rev;
581 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
582 return -ENODEV;
584 /* verify chip: reg address should match i2c address */
585 if (i2c_smbus_read_byte_data(new_client, ADM9240_REG_I2C_ADDR)
586 != address) {
587 dev_err(&adapter->dev, "detect fail: address match, 0x%02x\n",
588 address);
589 return -ENODEV;
592 /* check known chip manufacturer */
593 man_id = i2c_smbus_read_byte_data(new_client, ADM9240_REG_MAN_ID);
594 if (man_id == 0x23) {
595 name = "adm9240";
596 } else if (man_id == 0xda) {
597 name = "ds1780";
598 } else if (man_id == 0x01) {
599 name = "lm81";
600 } else {
601 dev_err(&adapter->dev, "detect fail: unknown manuf, 0x%02x\n",
602 man_id);
603 return -ENODEV;
606 /* successful detect, print chip info */
607 die_rev = i2c_smbus_read_byte_data(new_client, ADM9240_REG_DIE_REV);
608 dev_info(&adapter->dev, "found %s revision %u\n",
609 man_id == 0x23 ? "ADM9240" :
610 man_id == 0xda ? "DS1780" : "LM81", die_rev);
612 strlcpy(info->type, name, I2C_NAME_SIZE);
614 return 0;
617 static int adm9240_probe(struct i2c_client *new_client,
618 const struct i2c_device_id *id)
620 struct adm9240_data *data;
621 int err;
623 data = kzalloc(sizeof(*data), GFP_KERNEL);
624 if (!data) {
625 err = -ENOMEM;
626 goto exit;
629 i2c_set_clientdata(new_client, data);
630 mutex_init(&data->update_lock);
632 adm9240_init_client(new_client);
634 /* populate sysfs filesystem */
635 if ((err = sysfs_create_group(&new_client->dev.kobj, &adm9240_group)))
636 goto exit_free;
638 data->hwmon_dev = hwmon_device_register(&new_client->dev);
639 if (IS_ERR(data->hwmon_dev)) {
640 err = PTR_ERR(data->hwmon_dev);
641 goto exit_remove;
644 return 0;
646 exit_remove:
647 sysfs_remove_group(&new_client->dev.kobj, &adm9240_group);
648 exit_free:
649 kfree(data);
650 exit:
651 return err;
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);
661 kfree(data);
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++)
686 i2c_smbus_write_byte_data(client,
687 ADM9240_REG_IN_MIN(i), 0);
688 i2c_smbus_write_byte_data(client,
689 ADM9240_REG_IN_MAX(i), 255);
691 i2c_smbus_write_byte_data(client,
692 ADM9240_REG_FAN_MIN(0), 255);
693 i2c_smbus_write_byte_data(client,
694 ADM9240_REG_FAN_MIN(1), 255);
695 i2c_smbus_write_byte_data(client,
696 ADM9240_REG_TEMP_MAX(0), 127);
697 i2c_smbus_write_byte_data(client,
698 ADM9240_REG_TEMP_MAX(1), 127);
700 /* start measurement cycle */
701 i2c_smbus_write_byte_data(client, ADM9240_REG_CONFIG, 1);
703 dev_info(&client->dev, "cold start: config was 0x%02x "
704 "mode %u\n", conf, mode);
708 static struct adm9240_data *adm9240_update_device(struct device *dev)
710 struct i2c_client *client = to_i2c_client(dev);
711 struct adm9240_data *data = i2c_get_clientdata(client);
712 int i;
714 mutex_lock(&data->update_lock);
716 /* minimum measurement cycle: 1.75 seconds */
717 if (time_after(jiffies, data->last_updated_measure + (HZ * 7 / 4))
718 || !data->valid) {
720 for (i = 0; i < 6; i++) /* read voltages */
722 data->in[i] = i2c_smbus_read_byte_data(client,
723 ADM9240_REG_IN(i));
725 data->alarms = i2c_smbus_read_byte_data(client,
726 ADM9240_REG_INT(0)) |
727 i2c_smbus_read_byte_data(client,
728 ADM9240_REG_INT(1)) << 8;
730 /* read temperature: assume temperature changes less than
731 * 0.5'C per two measurement cycles thus ignore possible
732 * 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 */
740 data->fan[i] = i2c_smbus_read_byte_data(client,
741 ADM9240_REG_FAN(i));
743 /* adjust fan clock divider on overflow */
744 if (data->valid && data->fan[i] == 255 &&
745 data->fan_div[i] < 3) {
747 adm9240_write_fan_div(client, i,
748 ++data->fan_div[i]);
750 /* adjust fan_min if active, but not to 0 */
751 if (data->fan_min[i] < 255 &&
752 data->fan_min[i] >= 2)
753 data->fan_min[i] /= 2;
756 data->last_updated_measure = jiffies;
759 /* minimum config reading cycle: 300 seconds */
760 if (time_after(jiffies, data->last_updated_config + (HZ * 300))
761 || !data->valid) {
763 for (i = 0; i < 6; i++)
765 data->in_min[i] = i2c_smbus_read_byte_data(client,
766 ADM9240_REG_IN_MIN(i));
767 data->in_max[i] = i2c_smbus_read_byte_data(client,
768 ADM9240_REG_IN_MAX(i));
770 for (i = 0; i < 2; i++)
772 data->fan_min[i] = i2c_smbus_read_byte_data(client,
773 ADM9240_REG_FAN_MIN(i));
775 data->temp_max[0] = i2c_smbus_read_byte_data(client,
776 ADM9240_REG_TEMP_MAX(0));
777 data->temp_max[1] = i2c_smbus_read_byte_data(client,
778 ADM9240_REG_TEMP_MAX(1));
780 /* read fan divs and 5-bit VID */
781 i = i2c_smbus_read_byte_data(client, ADM9240_REG_VID_FAN_DIV);
782 data->fan_div[0] = (i >> 4) & 3;
783 data->fan_div[1] = (i >> 6) & 3;
784 data->vid = i & 0x0f;
785 data->vid |= (i2c_smbus_read_byte_data(client,
786 ADM9240_REG_VID4) & 1) << 4;
787 /* read analog out */
788 data->aout = i2c_smbus_read_byte_data(client,
789 ADM9240_REG_ANALOG_OUT);
791 data->last_updated_config = jiffies;
792 data->valid = 1;
794 mutex_unlock(&data->update_lock);
795 return data;
798 static int __init sensors_adm9240_init(void)
800 return i2c_add_driver(&adm9240_driver);
803 static void __exit sensors_adm9240_exit(void)
805 i2c_del_driver(&adm9240_driver);
808 MODULE_AUTHOR("Michiel Rook <michiel@grendelproject.nl>, "
809 "Grant Coady <gcoady.lk@gmail.com> and others");
810 MODULE_DESCRIPTION("ADM9240/DS1780/LM81 driver");
811 MODULE_LICENSE("GPL");
813 module_init(sensors_adm9240_init);
814 module_exit(sensors_adm9240_exit);