xref: /openbmc/linux/drivers/hwmon/adm9240.c (revision 87c2ce3b)
1 /*
2  * adm9240.c	Part of lm_sensors, Linux kernel modules for hardware
3  * 		monitoring
4  *
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@gmail.com> with valuable
9  * 				guidance from Jean Delvare
10  *
11  * Driver supports	Analog Devices		ADM9240
12  *			Dallas Semiconductor	DS1780
13  *			National Semiconductor	LM81
14  *
15  * ADM9240 is the reference, DS1780 and LM81 are register compatibles
16  *
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
22  *
23  * Chassis Intrusion: clear CI latch with 'echo 1 > chassis_clear'
24  *
25  * Test hardware: Intel SE440BX-2 desktop motherboard --Grant
26  *
27  * LM81 extended temp reading not implemented
28  *
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.
33  *
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.
38  *
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.
42  */
43 
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 
53 /* Addresses to scan */
54 static unsigned short normal_i2c[] = { 0x2c, 0x2d, 0x2e, 0x2f,
55 					I2C_CLIENT_END };
56 
57 /* Insmod parameters */
58 I2C_CLIENT_INSMOD_3(adm9240, ds1780, lm81);
59 
60 /* ADM9240 registers */
61 #define ADM9240_REG_MAN_ID		0x3e
62 #define ADM9240_REG_DIE_REV		0x3f
63 #define ADM9240_REG_CONFIG		0x40
64 
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
80 
81 /* generalised scaling with integer rounding */
82 static inline int SCALE(long val, int mul, int div)
83 {
84 	if (val < 0)
85 		return (val * mul - div / 2) / div;
86 	else
87 		return (val * mul + div / 2) / div;
88 }
89 
90 /* adm9240 internally scales voltage measurements */
91 static const u16 nom_mv[] = { 2500, 2700, 3300, 5000, 12000, 2700 };
92 
93 static inline unsigned int IN_FROM_REG(u8 reg, int n)
94 {
95 	return SCALE(reg, nom_mv[n], 192);
96 }
97 
98 static inline u8 IN_TO_REG(unsigned long val, int n)
99 {
100 	return SENSORS_LIMIT(SCALE(val, 192, nom_mv[n]), 0, 255);
101 }
102 
103 /* temperature range: -40..125, 127 disables temperature alarm */
104 static inline s8 TEMP_TO_REG(long val)
105 {
106 	return SENSORS_LIMIT(SCALE(val, 1, 1000), -40, 127);
107 }
108 
109 /* two fans, each with low fan speed limit */
110 static inline unsigned int FAN_FROM_REG(u8 reg, u8 div)
111 {
112 	if (!reg) /* error */
113 		return -1;
114 
115 	if (reg == 255)
116 		return 0;
117 
118 	return SCALE(1350000, 1, reg * div);
119 }
120 
121 /* analog out 0..1250mV */
122 static inline u8 AOUT_TO_REG(unsigned long val)
123 {
124 	return SENSORS_LIMIT(SCALE(val, 255, 1250), 0, 255);
125 }
126 
127 static inline unsigned int AOUT_FROM_REG(u8 reg)
128 {
129 	return SCALE(reg, 1250, 255);
130 }
131 
132 static int adm9240_attach_adapter(struct i2c_adapter *adapter);
133 static int adm9240_detect(struct i2c_adapter *adapter, int address, int kind);
134 static void adm9240_init_client(struct i2c_client *client);
135 static int adm9240_detach_client(struct i2c_client *client);
136 static struct adm9240_data *adm9240_update_device(struct device *dev);
137 
138 /* driver data */
139 static struct i2c_driver adm9240_driver = {
140 	.driver = {
141 		.name	= "adm9240",
142 	},
143 	.id		= I2C_DRIVERID_ADM9240,
144 	.attach_adapter	= adm9240_attach_adapter,
145 	.detach_client	= adm9240_detach_client,
146 };
147 
148 /* per client data */
149 struct adm9240_data {
150 	enum chips type;
151 	struct i2c_client client;
152 	struct class_device *class_dev;
153 	struct semaphore update_lock;
154 	char valid;
155 	unsigned long last_updated_measure;
156 	unsigned long last_updated_config;
157 
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 */
170 };
171 
172 /*** sysfs accessors ***/
173 
174 /* temperature */
175 static ssize_t show_temp(struct device *dev, struct device_attribute *dummy,
176 		char *buf)
177 {
178 	struct adm9240_data *data = adm9240_update_device(dev);
179 	return sprintf(buf, "%d\n", data->temp * 500); /* 9-bit value */
180 }
181 
182 static ssize_t show_max(struct device *dev, struct device_attribute *devattr,
183 		char *buf)
184 {
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);
188 }
189 
190 static ssize_t set_max(struct device *dev, struct device_attribute *devattr,
191 		const char *buf, size_t count)
192 {
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);
197 
198 	down(&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 	up(&data->update_lock);
203 	return count;
204 }
205 
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);
211 
212 /* voltage */
213 static ssize_t show_in(struct device *dev, struct device_attribute *devattr,
214 		char *buf)
215 {
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));
220 }
221 
222 static ssize_t show_in_min(struct device *dev,
223 		struct device_attribute *devattr, char *buf)
224 {
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));
229 }
230 
231 static ssize_t show_in_max(struct device *dev,
232 		struct device_attribute *devattr, char *buf)
233 {
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));
238 }
239 
240 static ssize_t set_in_min(struct device *dev,
241 		struct device_attribute *devattr,
242 		const char *buf, size_t count)
243 {
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);
248 
249 	down(&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 	up(&data->update_lock);
254 	return count;
255 }
256 
257 static ssize_t set_in_max(struct device *dev,
258 		struct device_attribute *devattr,
259 		const char *buf, size_t count)
260 {
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);
265 
266 	down(&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 	up(&data->update_lock);
271 	return count;
272 }
273 
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);
281 
282 vin(0);
283 vin(1);
284 vin(2);
285 vin(3);
286 vin(4);
287 vin(5);
288 
289 /* fans */
290 static ssize_t show_fan(struct device *dev,
291 		struct device_attribute *devattr, char *buf)
292 {
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]));
297 }
298 
299 static ssize_t show_fan_min(struct device *dev,
300 		struct device_attribute *devattr, char *buf)
301 {
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]));
306 }
307 
308 static ssize_t show_fan_div(struct device *dev,
309 		struct device_attribute *devattr, char *buf)
310 {
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]);
314 }
315 
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)
319 {
320 	u8 reg, old, shift = (nr + 2) * 2;
321 
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);
329 }
330 
331 /*
332  * set fan speed low limit:
333  *
334  * - value is zero: disable fan speed low limit alarm
335  *
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
338  *
339  * - otherwise: select fan clock divider to suit fan speed low limit,
340  *   measurement code may adjust registers to ensure fan speed reading
341  */
342 static ssize_t set_fan_min(struct device *dev,
343 		struct device_attribute *devattr,
344 		const char *buf, size_t count)
345 {
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;
352 
353 	down(&data->update_lock);
354 
355 	if (!val) {
356 		data->fan_min[nr] = 255;
357 		new_div = data->fan_div[nr];
358 
359 		dev_dbg(&client->dev, "fan%u low limit set disabled\n",
360 				nr + 1);
361 
362 	} else if (val < 1350000 / (8 * 254)) {
363 		new_div = 3;
364 		data->fan_min[nr] = 254;
365 
366 		dev_dbg(&client->dev, "fan%u low limit set minimum %u\n",
367 				nr + 1, FAN_FROM_REG(254, 1 << new_div));
368 
369 	} else {
370 		unsigned int new_min = 1350000 / val;
371 
372 		new_div = 0;
373 		while (new_min > 192 && new_div < 3) {
374 			new_div++;
375 			new_min /= 2;
376 		}
377 		if (!new_min) /* keep > 0 */
378 			new_min++;
379 
380 		data->fan_min[nr] = new_min;
381 
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));
384 	}
385 
386 	if (new_div != data->fan_div[nr]) {
387 		data->fan_div[nr] = new_div;
388 		adm9240_write_fan_div(client, nr, new_div);
389 	}
390 	i2c_smbus_write_byte_data(client, ADM9240_REG_FAN_MIN(nr),
391 			data->fan_min[nr]);
392 
393 	up(&data->update_lock);
394 	return count;
395 }
396 
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);
404 
405 fan(1);
406 fan(2);
407 
408 /* alarms */
409 static ssize_t show_alarms(struct device *dev,
410 		struct device_attribute *attr, char *buf)
411 {
412 	struct adm9240_data *data = adm9240_update_device(dev);
413 	return sprintf(buf, "%u\n", data->alarms);
414 }
415 static DEVICE_ATTR(alarms, S_IRUGO, show_alarms, NULL);
416 
417 /* vid */
418 static ssize_t show_vid(struct device *dev,
419 		struct device_attribute *attr, char *buf)
420 {
421 	struct adm9240_data *data = adm9240_update_device(dev);
422 	return sprintf(buf, "%d\n", vid_from_reg(data->vid, data->vrm));
423 }
424 static DEVICE_ATTR(cpu0_vid, S_IRUGO, show_vid, NULL);
425 
426 /* analog output */
427 static ssize_t show_aout(struct device *dev,
428 		struct device_attribute *attr, char *buf)
429 {
430 	struct adm9240_data *data = adm9240_update_device(dev);
431 	return sprintf(buf, "%d\n", AOUT_FROM_REG(data->aout));
432 }
433 
434 static ssize_t set_aout(struct device *dev,
435 		struct device_attribute *attr,
436 		const char *buf, size_t count)
437 {
438 	struct i2c_client *client = to_i2c_client(dev);
439 	struct adm9240_data *data = i2c_get_clientdata(client);
440 	unsigned long val = simple_strtol(buf, NULL, 10);
441 
442 	down(&data->update_lock);
443 	data->aout = AOUT_TO_REG(val);
444 	i2c_smbus_write_byte_data(client, ADM9240_REG_ANALOG_OUT, data->aout);
445 	up(&data->update_lock);
446 	return count;
447 }
448 static DEVICE_ATTR(aout_output, S_IRUGO | S_IWUSR, show_aout, set_aout);
449 
450 /* chassis_clear */
451 static ssize_t chassis_clear(struct device *dev,
452 		struct device_attribute *attr,
453 		const char *buf, size_t count)
454 {
455 	struct i2c_client *client = to_i2c_client(dev);
456 	unsigned long val = simple_strtol(buf, NULL, 10);
457 
458 	if (val == 1) {
459 		i2c_smbus_write_byte_data(client,
460 				ADM9240_REG_CHASSIS_CLEAR, 0x80);
461 		dev_dbg(&client->dev, "chassis intrusion latch cleared\n");
462 	}
463 	return count;
464 }
465 static DEVICE_ATTR(chassis_clear, S_IWUSR, NULL, chassis_clear);
466 
467 
468 /*** sensor chip detect and driver install ***/
469 
470 static int adm9240_detect(struct i2c_adapter *adapter, int address, int kind)
471 {
472 	struct i2c_client *new_client;
473 	struct adm9240_data *data;
474 	int err = 0;
475 	const char *name = "";
476 	u8 man_id, die_rev;
477 
478 	if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
479 		goto exit;
480 
481 	if (!(data = kzalloc(sizeof(*data), GFP_KERNEL))) {
482 		err = -ENOMEM;
483 		goto exit;
484 	}
485 
486 	new_client = &data->client;
487 	i2c_set_clientdata(new_client, data);
488 	new_client->addr = address;
489 	new_client->adapter = adapter;
490 	new_client->driver = &adm9240_driver;
491 	new_client->flags = 0;
492 
493 	if (kind == 0) {
494 		kind = adm9240;
495 	}
496 
497 	if (kind < 0) {
498 
499 		/* verify chip: reg address should match i2c address */
500 		if (i2c_smbus_read_byte_data(new_client, ADM9240_REG_I2C_ADDR)
501 				!= address) {
502 			dev_err(&adapter->dev, "detect fail: address match, "
503 					"0x%02x\n", address);
504 			goto exit_free;
505 		}
506 
507 		/* check known chip manufacturer */
508 		man_id = i2c_smbus_read_byte_data(new_client,
509 				ADM9240_REG_MAN_ID);
510 		if (man_id == 0x23) {
511 			kind = adm9240;
512 		} else if (man_id == 0xda) {
513 			kind = ds1780;
514 		} else if (man_id == 0x01) {
515 			kind = lm81;
516 		} else {
517 			dev_err(&adapter->dev, "detect fail: unknown manuf, "
518 					"0x%02x\n", man_id);
519 			goto exit_free;
520 		}
521 
522 		/* successful detect, print chip info */
523 		die_rev = i2c_smbus_read_byte_data(new_client,
524 				ADM9240_REG_DIE_REV);
525 		dev_info(&adapter->dev, "found %s revision %u\n",
526 				man_id == 0x23 ? "ADM9240" :
527 				man_id == 0xda ? "DS1780" : "LM81", die_rev);
528 	}
529 
530 	/* either forced or detected chip kind */
531 	if (kind == adm9240) {
532 		name = "adm9240";
533 	} else if (kind == ds1780) {
534 		name = "ds1780";
535 	} else if (kind == lm81) {
536 		name = "lm81";
537 	}
538 
539 	/* fill in the remaining client fields and attach */
540 	strlcpy(new_client->name, name, I2C_NAME_SIZE);
541 	data->type = kind;
542 	init_MUTEX(&data->update_lock);
543 
544 	if ((err = i2c_attach_client(new_client)))
545 		goto exit_free;
546 
547 	adm9240_init_client(new_client);
548 
549 	/* populate sysfs filesystem */
550 	data->class_dev = hwmon_device_register(&new_client->dev);
551 	if (IS_ERR(data->class_dev)) {
552 		err = PTR_ERR(data->class_dev);
553 		goto exit_detach;
554 	}
555 
556 	device_create_file(&new_client->dev,
557 			&sensor_dev_attr_in0_input.dev_attr);
558 	device_create_file(&new_client->dev,
559 			&sensor_dev_attr_in0_min.dev_attr);
560 	device_create_file(&new_client->dev,
561 			&sensor_dev_attr_in0_max.dev_attr);
562 	device_create_file(&new_client->dev,
563 			&sensor_dev_attr_in1_input.dev_attr);
564 	device_create_file(&new_client->dev,
565 			&sensor_dev_attr_in1_min.dev_attr);
566 	device_create_file(&new_client->dev,
567 			&sensor_dev_attr_in1_max.dev_attr);
568 	device_create_file(&new_client->dev,
569 			&sensor_dev_attr_in2_input.dev_attr);
570 	device_create_file(&new_client->dev,
571 			&sensor_dev_attr_in2_min.dev_attr);
572 	device_create_file(&new_client->dev,
573 			&sensor_dev_attr_in2_max.dev_attr);
574 	device_create_file(&new_client->dev,
575 			&sensor_dev_attr_in3_input.dev_attr);
576 	device_create_file(&new_client->dev,
577 			&sensor_dev_attr_in3_min.dev_attr);
578 	device_create_file(&new_client->dev,
579 			&sensor_dev_attr_in3_max.dev_attr);
580 	device_create_file(&new_client->dev,
581 			&sensor_dev_attr_in4_input.dev_attr);
582 	device_create_file(&new_client->dev,
583 			&sensor_dev_attr_in4_min.dev_attr);
584 	device_create_file(&new_client->dev,
585 			&sensor_dev_attr_in4_max.dev_attr);
586 	device_create_file(&new_client->dev,
587 			&sensor_dev_attr_in5_input.dev_attr);
588 	device_create_file(&new_client->dev,
589 			&sensor_dev_attr_in5_min.dev_attr);
590 	device_create_file(&new_client->dev,
591 			&sensor_dev_attr_in5_max.dev_attr);
592 	device_create_file(&new_client->dev, &dev_attr_temp1_input);
593 	device_create_file(&new_client->dev,
594 			&sensor_dev_attr_temp1_max.dev_attr);
595 	device_create_file(&new_client->dev,
596 			&sensor_dev_attr_temp1_max_hyst.dev_attr);
597 	device_create_file(&new_client->dev,
598 			&sensor_dev_attr_fan1_input.dev_attr);
599 	device_create_file(&new_client->dev,
600 			&sensor_dev_attr_fan1_div.dev_attr);
601 	device_create_file(&new_client->dev,
602 			&sensor_dev_attr_fan1_min.dev_attr);
603 	device_create_file(&new_client->dev,
604 			&sensor_dev_attr_fan2_input.dev_attr);
605 	device_create_file(&new_client->dev,
606 			&sensor_dev_attr_fan2_div.dev_attr);
607 	device_create_file(&new_client->dev,
608 			&sensor_dev_attr_fan2_min.dev_attr);
609 	device_create_file(&new_client->dev, &dev_attr_alarms);
610 	device_create_file(&new_client->dev, &dev_attr_aout_output);
611 	device_create_file(&new_client->dev, &dev_attr_chassis_clear);
612 	device_create_file(&new_client->dev, &dev_attr_cpu0_vid);
613 
614 	return 0;
615 
616 exit_detach:
617 	i2c_detach_client(new_client);
618 exit_free:
619 	kfree(data);
620 exit:
621 	return err;
622 }
623 
624 static int adm9240_attach_adapter(struct i2c_adapter *adapter)
625 {
626 	if (!(adapter->class & I2C_CLASS_HWMON))
627 		return 0;
628 	return i2c_probe(adapter, &addr_data, adm9240_detect);
629 }
630 
631 static int adm9240_detach_client(struct i2c_client *client)
632 {
633 	struct adm9240_data *data = i2c_get_clientdata(client);
634 	int err;
635 
636 	hwmon_device_unregister(data->class_dev);
637 
638 	if ((err = i2c_detach_client(client)))
639 		return err;
640 
641 	kfree(data);
642 	return 0;
643 }
644 
645 static void adm9240_init_client(struct i2c_client *client)
646 {
647 	struct adm9240_data *data = i2c_get_clientdata(client);
648 	u8 conf = i2c_smbus_read_byte_data(client, ADM9240_REG_CONFIG);
649 	u8 mode = i2c_smbus_read_byte_data(client, ADM9240_REG_TEMP_CONF) & 3;
650 
651 	data->vrm = vid_which_vrm(); /* need this to report vid as mV */
652 
653 	dev_info(&client->dev, "Using VRM: %d.%d\n", data->vrm / 10,
654 			data->vrm % 10);
655 
656 	if (conf & 1) { /* measurement cycle running: report state */
657 
658 		dev_info(&client->dev, "status: config 0x%02x mode %u\n",
659 				conf, mode);
660 
661 	} else { /* cold start: open limits before starting chip */
662 		int i;
663 
664 		for (i = 0; i < 6; i++)
665 		{
666 			i2c_smbus_write_byte_data(client,
667 					ADM9240_REG_IN_MIN(i), 0);
668 			i2c_smbus_write_byte_data(client,
669 					ADM9240_REG_IN_MAX(i), 255);
670 		}
671 		i2c_smbus_write_byte_data(client,
672 				ADM9240_REG_FAN_MIN(0), 255);
673 		i2c_smbus_write_byte_data(client,
674 				ADM9240_REG_FAN_MIN(1), 255);
675 		i2c_smbus_write_byte_data(client,
676 				ADM9240_REG_TEMP_MAX(0), 127);
677 		i2c_smbus_write_byte_data(client,
678 				ADM9240_REG_TEMP_MAX(1), 127);
679 
680 		/* start measurement cycle */
681 		i2c_smbus_write_byte_data(client, ADM9240_REG_CONFIG, 1);
682 
683 		dev_info(&client->dev, "cold start: config was 0x%02x "
684 				"mode %u\n", conf, mode);
685 	}
686 }
687 
688 static struct adm9240_data *adm9240_update_device(struct device *dev)
689 {
690 	struct i2c_client *client = to_i2c_client(dev);
691 	struct adm9240_data *data = i2c_get_clientdata(client);
692 	int i;
693 
694 	down(&data->update_lock);
695 
696 	/* minimum measurement cycle: 1.75 seconds */
697 	if (time_after(jiffies, data->last_updated_measure + (HZ * 7 / 4))
698 			|| !data->valid) {
699 
700 		for (i = 0; i < 6; i++) /* read voltages */
701 		{
702 			data->in[i] = i2c_smbus_read_byte_data(client,
703 					ADM9240_REG_IN(i));
704 		}
705 		data->alarms = i2c_smbus_read_byte_data(client,
706 					ADM9240_REG_INT(0)) |
707 					i2c_smbus_read_byte_data(client,
708 					ADM9240_REG_INT(1)) << 8;
709 
710 		/* read temperature: assume temperature changes less than
711 		 * 0.5'C per two measurement cycles thus ignore possible
712 		 * but unlikely aliasing error on lsb reading. --Grant */
713 		data->temp = ((i2c_smbus_read_byte_data(client,
714 					ADM9240_REG_TEMP) << 8) |
715 					i2c_smbus_read_byte_data(client,
716 					ADM9240_REG_TEMP_CONF)) / 128;
717 
718 		for (i = 0; i < 2; i++) /* read fans */
719 		{
720 			data->fan[i] = i2c_smbus_read_byte_data(client,
721 					ADM9240_REG_FAN(i));
722 
723 			/* adjust fan clock divider on overflow */
724 			if (data->valid && data->fan[i] == 255 &&
725 					data->fan_div[i] < 3) {
726 
727 				adm9240_write_fan_div(client, i,
728 						++data->fan_div[i]);
729 
730 				/* adjust fan_min if active, but not to 0 */
731 				if (data->fan_min[i] < 255 &&
732 						data->fan_min[i] >= 2)
733 					data->fan_min[i] /= 2;
734 			}
735 		}
736 		data->last_updated_measure = jiffies;
737 	}
738 
739 	/* minimum config reading cycle: 300 seconds */
740 	if (time_after(jiffies, data->last_updated_config + (HZ * 300))
741 			|| !data->valid) {
742 
743 		for (i = 0; i < 6; i++)
744 		{
745 			data->in_min[i] = i2c_smbus_read_byte_data(client,
746 					ADM9240_REG_IN_MIN(i));
747 			data->in_max[i] = i2c_smbus_read_byte_data(client,
748 					ADM9240_REG_IN_MAX(i));
749 		}
750 		for (i = 0; i < 2; i++)
751 		{
752 			data->fan_min[i] = i2c_smbus_read_byte_data(client,
753 					ADM9240_REG_FAN_MIN(i));
754 		}
755 		data->temp_max[0] = i2c_smbus_read_byte_data(client,
756 				ADM9240_REG_TEMP_MAX(0));
757 		data->temp_max[1] = i2c_smbus_read_byte_data(client,
758 				ADM9240_REG_TEMP_MAX(1));
759 
760 		/* read fan divs and 5-bit VID */
761 		i = i2c_smbus_read_byte_data(client, ADM9240_REG_VID_FAN_DIV);
762 		data->fan_div[0] = (i >> 4) & 3;
763 		data->fan_div[1] = (i >> 6) & 3;
764 		data->vid = i & 0x0f;
765 		data->vid |= (i2c_smbus_read_byte_data(client,
766 					ADM9240_REG_VID4) & 1) << 4;
767 		/* read analog out */
768 		data->aout = i2c_smbus_read_byte_data(client,
769 				ADM9240_REG_ANALOG_OUT);
770 
771 		data->last_updated_config = jiffies;
772 		data->valid = 1;
773 	}
774 	up(&data->update_lock);
775 	return data;
776 }
777 
778 static int __init sensors_adm9240_init(void)
779 {
780 	return i2c_add_driver(&adm9240_driver);
781 }
782 
783 static void __exit sensors_adm9240_exit(void)
784 {
785 	i2c_del_driver(&adm9240_driver);
786 }
787 
788 MODULE_AUTHOR("Michiel Rook <michiel@grendelproject.nl>, "
789 		"Grant Coady <gcoady@gmail.com> and others");
790 MODULE_DESCRIPTION("ADM9240/DS1780/LM81 driver");
791 MODULE_LICENSE("GPL");
792 
793 module_init(sensors_adm9240_init);
794 module_exit(sensors_adm9240_exit);
795 
796