xref: /openbmc/linux/drivers/hwmon/adm9240.c (revision 3381df09)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * adm9240.c	Part of lm_sensors, Linux kernel modules for hardware
4  *		monitoring
5  *
6  * Copyright (C) 1999	Frodo Looijaard <frodol@dds.nl>
7  *			Philip Edelbrock <phil@netroedge.com>
8  * Copyright (C) 2003	Michiel Rook <michiel@grendelproject.nl>
9  * Copyright (C) 2005	Grant Coady <gcoady.lk@gmail.com> with valuable
10  *				guidance from Jean Delvare
11  *
12  * Driver supports	Analog Devices		ADM9240
13  *			Dallas Semiconductor	DS1780
14  *			National Semiconductor	LM81
15  *
16  * ADM9240 is the reference, DS1780 and LM81 are register compatibles
17  *
18  * Voltage	Six inputs are scaled by chip, VID also reported
19  * Temperature	Chip temperature to 0.5'C, maximum and max_hysteris
20  * Fans		2 fans, low speed alarm, automatic fan clock divider
21  * Alarms	16-bit map of active alarms
22  * Analog Out	0..1250 mV output
23  *
24  * Chassis Intrusion: clear CI latch with 'echo 0 > intrusion0_alarm'
25  *
26  * Test hardware: Intel SE440BX-2 desktop motherboard --Grant
27  *
28  * LM81 extended temp reading not implemented
29  */
30 
31 #include <linux/init.h>
32 #include <linux/module.h>
33 #include <linux/slab.h>
34 #include <linux/i2c.h>
35 #include <linux/hwmon-sysfs.h>
36 #include <linux/hwmon.h>
37 #include <linux/hwmon-vid.h>
38 #include <linux/err.h>
39 #include <linux/mutex.h>
40 #include <linux/jiffies.h>
41 
42 /* Addresses to scan */
43 static const unsigned short normal_i2c[] = { 0x2c, 0x2d, 0x2e, 0x2f,
44 					I2C_CLIENT_END };
45 
46 enum chips { adm9240, ds1780, lm81 };
47 
48 /* ADM9240 registers */
49 #define ADM9240_REG_MAN_ID		0x3e
50 #define ADM9240_REG_DIE_REV		0x3f
51 #define ADM9240_REG_CONFIG		0x40
52 
53 #define ADM9240_REG_IN(nr)		(0x20 + (nr))   /* 0..5 */
54 #define ADM9240_REG_IN_MAX(nr)		(0x2b + (nr) * 2)
55 #define ADM9240_REG_IN_MIN(nr)		(0x2c + (nr) * 2)
56 #define ADM9240_REG_FAN(nr)		(0x28 + (nr))   /* 0..1 */
57 #define ADM9240_REG_FAN_MIN(nr)		(0x3b + (nr))
58 #define ADM9240_REG_INT(nr)		(0x41 + (nr))
59 #define ADM9240_REG_INT_MASK(nr)	(0x43 + (nr))
60 #define ADM9240_REG_TEMP		0x27
61 #define ADM9240_REG_TEMP_MAX(nr)	(0x39 + (nr)) /* 0, 1 = high, hyst */
62 #define ADM9240_REG_ANALOG_OUT		0x19
63 #define ADM9240_REG_CHASSIS_CLEAR	0x46
64 #define ADM9240_REG_VID_FAN_DIV		0x47
65 #define ADM9240_REG_I2C_ADDR		0x48
66 #define ADM9240_REG_VID4		0x49
67 #define ADM9240_REG_TEMP_CONF		0x4b
68 
69 /* generalised scaling with integer rounding */
70 static inline int SCALE(long val, int mul, int div)
71 {
72 	if (val < 0)
73 		return (val * mul - div / 2) / div;
74 	else
75 		return (val * mul + div / 2) / div;
76 }
77 
78 /* adm9240 internally scales voltage measurements */
79 static const u16 nom_mv[] = { 2500, 2700, 3300, 5000, 12000, 2700 };
80 
81 static inline unsigned int IN_FROM_REG(u8 reg, int n)
82 {
83 	return SCALE(reg, nom_mv[n], 192);
84 }
85 
86 static inline u8 IN_TO_REG(unsigned long val, int n)
87 {
88 	val = clamp_val(val, 0, nom_mv[n] * 255 / 192);
89 	return SCALE(val, 192, nom_mv[n]);
90 }
91 
92 /* temperature range: -40..125, 127 disables temperature alarm */
93 static inline s8 TEMP_TO_REG(long val)
94 {
95 	val = clamp_val(val, -40000, 127000);
96 	return SCALE(val, 1, 1000);
97 }
98 
99 /* two fans, each with low fan speed limit */
100 static inline unsigned int FAN_FROM_REG(u8 reg, u8 div)
101 {
102 	if (!reg) /* error */
103 		return -1;
104 
105 	if (reg == 255)
106 		return 0;
107 
108 	return SCALE(1350000, 1, reg * div);
109 }
110 
111 /* analog out 0..1250mV */
112 static inline u8 AOUT_TO_REG(unsigned long val)
113 {
114 	val = clamp_val(val, 0, 1250);
115 	return SCALE(val, 255, 1250);
116 }
117 
118 static inline unsigned int AOUT_FROM_REG(u8 reg)
119 {
120 	return SCALE(reg, 1250, 255);
121 }
122 
123 /* per client data */
124 struct adm9240_data {
125 	struct i2c_client *client;
126 	struct mutex update_lock;
127 	char valid;
128 	unsigned long last_updated_measure;
129 	unsigned long last_updated_config;
130 
131 	u8 in[6];		/* ro	in0_input */
132 	u8 in_max[6];		/* rw	in0_max */
133 	u8 in_min[6];		/* rw	in0_min */
134 	u8 fan[2];		/* ro	fan1_input */
135 	u8 fan_min[2];		/* rw	fan1_min */
136 	u8 fan_div[2];		/* rw	fan1_div, read-only accessor */
137 	s16 temp;		/* ro	temp1_input, 9-bit sign-extended */
138 	s8 temp_max[2];		/* rw	0 -> temp_max, 1 -> temp_max_hyst */
139 	u16 alarms;		/* ro	alarms */
140 	u8 aout;		/* rw	aout_output */
141 	u8 vid;			/* ro	vid */
142 	u8 vrm;			/* --	vrm set on startup, no accessor */
143 };
144 
145 /* write new fan div, callers must hold data->update_lock */
146 static void adm9240_write_fan_div(struct i2c_client *client, int nr,
147 		u8 fan_div)
148 {
149 	u8 reg, old, shift = (nr + 2) * 2;
150 
151 	reg = i2c_smbus_read_byte_data(client, ADM9240_REG_VID_FAN_DIV);
152 	old = (reg >> shift) & 3;
153 	reg &= ~(3 << shift);
154 	reg |= (fan_div << shift);
155 	i2c_smbus_write_byte_data(client, ADM9240_REG_VID_FAN_DIV, reg);
156 	dev_dbg(&client->dev,
157 		"fan%d clock divider changed from %u to %u\n",
158 		nr + 1, 1 << old, 1 << fan_div);
159 }
160 
161 static struct adm9240_data *adm9240_update_device(struct device *dev)
162 {
163 	struct adm9240_data *data = dev_get_drvdata(dev);
164 	struct i2c_client *client = data->client;
165 	int i;
166 
167 	mutex_lock(&data->update_lock);
168 
169 	/* minimum measurement cycle: 1.75 seconds */
170 	if (time_after(jiffies, data->last_updated_measure + (HZ * 7 / 4))
171 			|| !data->valid) {
172 
173 		for (i = 0; i < 6; i++) { /* read voltages */
174 			data->in[i] = i2c_smbus_read_byte_data(client,
175 					ADM9240_REG_IN(i));
176 		}
177 		data->alarms = i2c_smbus_read_byte_data(client,
178 					ADM9240_REG_INT(0)) |
179 					i2c_smbus_read_byte_data(client,
180 					ADM9240_REG_INT(1)) << 8;
181 
182 		/*
183 		 * read temperature: assume temperature changes less than
184 		 * 0.5'C per two measurement cycles thus ignore possible
185 		 * but unlikely aliasing error on lsb reading. --Grant
186 		 */
187 		data->temp = (i2c_smbus_read_byte_data(client,
188 					ADM9240_REG_TEMP) << 8) |
189 					i2c_smbus_read_byte_data(client,
190 					ADM9240_REG_TEMP_CONF);
191 
192 		for (i = 0; i < 2; i++) { /* read fans */
193 			data->fan[i] = i2c_smbus_read_byte_data(client,
194 					ADM9240_REG_FAN(i));
195 
196 			/* adjust fan clock divider on overflow */
197 			if (data->valid && data->fan[i] == 255 &&
198 					data->fan_div[i] < 3) {
199 
200 				adm9240_write_fan_div(client, i,
201 						++data->fan_div[i]);
202 
203 				/* adjust fan_min if active, but not to 0 */
204 				if (data->fan_min[i] < 255 &&
205 						data->fan_min[i] >= 2)
206 					data->fan_min[i] /= 2;
207 			}
208 		}
209 		data->last_updated_measure = jiffies;
210 	}
211 
212 	/* minimum config reading cycle: 300 seconds */
213 	if (time_after(jiffies, data->last_updated_config + (HZ * 300))
214 			|| !data->valid) {
215 
216 		for (i = 0; i < 6; i++) {
217 			data->in_min[i] = i2c_smbus_read_byte_data(client,
218 					ADM9240_REG_IN_MIN(i));
219 			data->in_max[i] = i2c_smbus_read_byte_data(client,
220 					ADM9240_REG_IN_MAX(i));
221 		}
222 		for (i = 0; i < 2; i++) {
223 			data->fan_min[i] = i2c_smbus_read_byte_data(client,
224 					ADM9240_REG_FAN_MIN(i));
225 		}
226 		data->temp_max[0] = i2c_smbus_read_byte_data(client,
227 				ADM9240_REG_TEMP_MAX(0));
228 		data->temp_max[1] = i2c_smbus_read_byte_data(client,
229 				ADM9240_REG_TEMP_MAX(1));
230 
231 		/* read fan divs and 5-bit VID */
232 		i = i2c_smbus_read_byte_data(client, ADM9240_REG_VID_FAN_DIV);
233 		data->fan_div[0] = (i >> 4) & 3;
234 		data->fan_div[1] = (i >> 6) & 3;
235 		data->vid = i & 0x0f;
236 		data->vid |= (i2c_smbus_read_byte_data(client,
237 					ADM9240_REG_VID4) & 1) << 4;
238 		/* read analog out */
239 		data->aout = i2c_smbus_read_byte_data(client,
240 				ADM9240_REG_ANALOG_OUT);
241 
242 		data->last_updated_config = jiffies;
243 		data->valid = 1;
244 	}
245 	mutex_unlock(&data->update_lock);
246 	return data;
247 }
248 
249 /*** sysfs accessors ***/
250 
251 /* temperature */
252 static ssize_t temp1_input_show(struct device *dev,
253 				struct device_attribute *dummy, char *buf)
254 {
255 	struct adm9240_data *data = adm9240_update_device(dev);
256 	return sprintf(buf, "%d\n", data->temp / 128 * 500); /* 9-bit value */
257 }
258 
259 static ssize_t max_show(struct device *dev, struct device_attribute *devattr,
260 			char *buf)
261 {
262 	struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
263 	struct adm9240_data *data = adm9240_update_device(dev);
264 	return sprintf(buf, "%d\n", data->temp_max[attr->index] * 1000);
265 }
266 
267 static ssize_t max_store(struct device *dev, struct device_attribute *devattr,
268 			 const char *buf, size_t count)
269 {
270 	struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
271 	struct adm9240_data *data = dev_get_drvdata(dev);
272 	struct i2c_client *client = data->client;
273 	long val;
274 	int err;
275 
276 	err = kstrtol(buf, 10, &val);
277 	if (err)
278 		return err;
279 
280 	mutex_lock(&data->update_lock);
281 	data->temp_max[attr->index] = TEMP_TO_REG(val);
282 	i2c_smbus_write_byte_data(client, ADM9240_REG_TEMP_MAX(attr->index),
283 			data->temp_max[attr->index]);
284 	mutex_unlock(&data->update_lock);
285 	return count;
286 }
287 
288 static DEVICE_ATTR_RO(temp1_input);
289 static SENSOR_DEVICE_ATTR_RW(temp1_max, max, 0);
290 static SENSOR_DEVICE_ATTR_RW(temp1_max_hyst, max, 1);
291 
292 /* voltage */
293 static ssize_t in_show(struct device *dev, struct device_attribute *devattr,
294 		       char *buf)
295 {
296 	struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
297 	struct adm9240_data *data = adm9240_update_device(dev);
298 	return sprintf(buf, "%d\n", IN_FROM_REG(data->in[attr->index],
299 				attr->index));
300 }
301 
302 static ssize_t in_min_show(struct device *dev,
303 			   struct device_attribute *devattr, char *buf)
304 {
305 	struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
306 	struct adm9240_data *data = adm9240_update_device(dev);
307 	return sprintf(buf, "%d\n", IN_FROM_REG(data->in_min[attr->index],
308 				attr->index));
309 }
310 
311 static ssize_t in_max_show(struct device *dev,
312 			   struct device_attribute *devattr, char *buf)
313 {
314 	struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
315 	struct adm9240_data *data = adm9240_update_device(dev);
316 	return sprintf(buf, "%d\n", IN_FROM_REG(data->in_max[attr->index],
317 				attr->index));
318 }
319 
320 static ssize_t in_min_store(struct device *dev,
321 			    struct device_attribute *devattr, const char *buf,
322 			    size_t count)
323 {
324 	struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
325 	struct adm9240_data *data = dev_get_drvdata(dev);
326 	struct i2c_client *client = data->client;
327 	unsigned long val;
328 	int err;
329 
330 	err = kstrtoul(buf, 10, &val);
331 	if (err)
332 		return err;
333 
334 	mutex_lock(&data->update_lock);
335 	data->in_min[attr->index] = IN_TO_REG(val, attr->index);
336 	i2c_smbus_write_byte_data(client, ADM9240_REG_IN_MIN(attr->index),
337 			data->in_min[attr->index]);
338 	mutex_unlock(&data->update_lock);
339 	return count;
340 }
341 
342 static ssize_t in_max_store(struct device *dev,
343 			    struct device_attribute *devattr, const char *buf,
344 			    size_t count)
345 {
346 	struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
347 	struct adm9240_data *data = dev_get_drvdata(dev);
348 	struct i2c_client *client = data->client;
349 	unsigned long val;
350 	int err;
351 
352 	err = kstrtoul(buf, 10, &val);
353 	if (err)
354 		return err;
355 
356 	mutex_lock(&data->update_lock);
357 	data->in_max[attr->index] = IN_TO_REG(val, attr->index);
358 	i2c_smbus_write_byte_data(client, ADM9240_REG_IN_MAX(attr->index),
359 			data->in_max[attr->index]);
360 	mutex_unlock(&data->update_lock);
361 	return count;
362 }
363 
364 static SENSOR_DEVICE_ATTR_RO(in0_input, in, 0);
365 static SENSOR_DEVICE_ATTR_RW(in0_min, in_min, 0);
366 static SENSOR_DEVICE_ATTR_RW(in0_max, in_max, 0);
367 static SENSOR_DEVICE_ATTR_RO(in1_input, in, 1);
368 static SENSOR_DEVICE_ATTR_RW(in1_min, in_min, 1);
369 static SENSOR_DEVICE_ATTR_RW(in1_max, in_max, 1);
370 static SENSOR_DEVICE_ATTR_RO(in2_input, in, 2);
371 static SENSOR_DEVICE_ATTR_RW(in2_min, in_min, 2);
372 static SENSOR_DEVICE_ATTR_RW(in2_max, in_max, 2);
373 static SENSOR_DEVICE_ATTR_RO(in3_input, in, 3);
374 static SENSOR_DEVICE_ATTR_RW(in3_min, in_min, 3);
375 static SENSOR_DEVICE_ATTR_RW(in3_max, in_max, 3);
376 static SENSOR_DEVICE_ATTR_RO(in4_input, in, 4);
377 static SENSOR_DEVICE_ATTR_RW(in4_min, in_min, 4);
378 static SENSOR_DEVICE_ATTR_RW(in4_max, in_max, 4);
379 static SENSOR_DEVICE_ATTR_RO(in5_input, in, 5);
380 static SENSOR_DEVICE_ATTR_RW(in5_min, in_min, 5);
381 static SENSOR_DEVICE_ATTR_RW(in5_max, in_max, 5);
382 
383 /* fans */
384 static ssize_t fan_show(struct device *dev, struct device_attribute *devattr,
385 			char *buf)
386 {
387 	struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
388 	struct adm9240_data *data = adm9240_update_device(dev);
389 	return sprintf(buf, "%d\n", FAN_FROM_REG(data->fan[attr->index],
390 				1 << data->fan_div[attr->index]));
391 }
392 
393 static ssize_t fan_min_show(struct device *dev,
394 			    struct device_attribute *devattr, char *buf)
395 {
396 	struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
397 	struct adm9240_data *data = adm9240_update_device(dev);
398 	return sprintf(buf, "%d\n", FAN_FROM_REG(data->fan_min[attr->index],
399 				1 << data->fan_div[attr->index]));
400 }
401 
402 static ssize_t fan_div_show(struct device *dev,
403 			    struct device_attribute *devattr, char *buf)
404 {
405 	struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
406 	struct adm9240_data *data = adm9240_update_device(dev);
407 	return sprintf(buf, "%d\n", 1 << data->fan_div[attr->index]);
408 }
409 
410 /*
411  * set fan speed low limit:
412  *
413  * - value is zero: disable fan speed low limit alarm
414  *
415  * - value is below fan speed measurement range: enable fan speed low
416  *   limit alarm to be asserted while fan speed too slow to measure
417  *
418  * - otherwise: select fan clock divider to suit fan speed low limit,
419  *   measurement code may adjust registers to ensure fan speed reading
420  */
421 static ssize_t fan_min_store(struct device *dev,
422 			     struct device_attribute *devattr,
423 			     const char *buf, size_t count)
424 {
425 	struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
426 	struct adm9240_data *data = dev_get_drvdata(dev);
427 	struct i2c_client *client = data->client;
428 	int nr = attr->index;
429 	u8 new_div;
430 	unsigned long val;
431 	int err;
432 
433 	err = kstrtoul(buf, 10, &val);
434 	if (err)
435 		return err;
436 
437 	mutex_lock(&data->update_lock);
438 
439 	if (!val) {
440 		data->fan_min[nr] = 255;
441 		new_div = data->fan_div[nr];
442 
443 		dev_dbg(&client->dev, "fan%u low limit set disabled\n",
444 				nr + 1);
445 
446 	} else if (val < 1350000 / (8 * 254)) {
447 		new_div = 3;
448 		data->fan_min[nr] = 254;
449 
450 		dev_dbg(&client->dev, "fan%u low limit set minimum %u\n",
451 				nr + 1, FAN_FROM_REG(254, 1 << new_div));
452 
453 	} else {
454 		unsigned int new_min = 1350000 / val;
455 
456 		new_div = 0;
457 		while (new_min > 192 && new_div < 3) {
458 			new_div++;
459 			new_min /= 2;
460 		}
461 		if (!new_min) /* keep > 0 */
462 			new_min++;
463 
464 		data->fan_min[nr] = new_min;
465 
466 		dev_dbg(&client->dev, "fan%u low limit set fan speed %u\n",
467 				nr + 1, FAN_FROM_REG(new_min, 1 << new_div));
468 	}
469 
470 	if (new_div != data->fan_div[nr]) {
471 		data->fan_div[nr] = new_div;
472 		adm9240_write_fan_div(client, nr, new_div);
473 	}
474 	i2c_smbus_write_byte_data(client, ADM9240_REG_FAN_MIN(nr),
475 			data->fan_min[nr]);
476 
477 	mutex_unlock(&data->update_lock);
478 	return count;
479 }
480 
481 static SENSOR_DEVICE_ATTR_RO(fan1_input, fan, 0);
482 static SENSOR_DEVICE_ATTR_RW(fan1_min, fan_min, 0);
483 static SENSOR_DEVICE_ATTR_RO(fan1_div, fan_div, 0);
484 static SENSOR_DEVICE_ATTR_RO(fan2_input, fan, 1);
485 static SENSOR_DEVICE_ATTR_RW(fan2_min, fan_min, 1);
486 static SENSOR_DEVICE_ATTR_RO(fan2_div, fan_div, 1);
487 
488 /* alarms */
489 static ssize_t alarms_show(struct device *dev,
490 		struct device_attribute *attr, char *buf)
491 {
492 	struct adm9240_data *data = adm9240_update_device(dev);
493 	return sprintf(buf, "%u\n", data->alarms);
494 }
495 static DEVICE_ATTR_RO(alarms);
496 
497 static ssize_t alarm_show(struct device *dev, struct device_attribute *attr,
498 			  char *buf)
499 {
500 	int bitnr = to_sensor_dev_attr(attr)->index;
501 	struct adm9240_data *data = adm9240_update_device(dev);
502 	return sprintf(buf, "%u\n", (data->alarms >> bitnr) & 1);
503 }
504 static SENSOR_DEVICE_ATTR_RO(in0_alarm, alarm, 0);
505 static SENSOR_DEVICE_ATTR_RO(in1_alarm, alarm, 1);
506 static SENSOR_DEVICE_ATTR_RO(in2_alarm, alarm, 2);
507 static SENSOR_DEVICE_ATTR_RO(in3_alarm, alarm, 3);
508 static SENSOR_DEVICE_ATTR_RO(in4_alarm, alarm, 8);
509 static SENSOR_DEVICE_ATTR_RO(in5_alarm, alarm, 9);
510 static SENSOR_DEVICE_ATTR_RO(temp1_alarm, alarm, 4);
511 static SENSOR_DEVICE_ATTR_RO(fan1_alarm, alarm, 6);
512 static SENSOR_DEVICE_ATTR_RO(fan2_alarm, alarm, 7);
513 
514 /* vid */
515 static ssize_t cpu0_vid_show(struct device *dev,
516 			     struct device_attribute *attr, char *buf)
517 {
518 	struct adm9240_data *data = adm9240_update_device(dev);
519 	return sprintf(buf, "%d\n", vid_from_reg(data->vid, data->vrm));
520 }
521 static DEVICE_ATTR_RO(cpu0_vid);
522 
523 /* analog output */
524 static ssize_t aout_output_show(struct device *dev,
525 				struct device_attribute *attr, char *buf)
526 {
527 	struct adm9240_data *data = adm9240_update_device(dev);
528 	return sprintf(buf, "%d\n", AOUT_FROM_REG(data->aout));
529 }
530 
531 static ssize_t aout_output_store(struct device *dev,
532 				 struct device_attribute *attr,
533 				 const char *buf, size_t count)
534 {
535 	struct adm9240_data *data = dev_get_drvdata(dev);
536 	struct i2c_client *client = data->client;
537 	long val;
538 	int err;
539 
540 	err = kstrtol(buf, 10, &val);
541 	if (err)
542 		return err;
543 
544 	mutex_lock(&data->update_lock);
545 	data->aout = AOUT_TO_REG(val);
546 	i2c_smbus_write_byte_data(client, ADM9240_REG_ANALOG_OUT, data->aout);
547 	mutex_unlock(&data->update_lock);
548 	return count;
549 }
550 static DEVICE_ATTR_RW(aout_output);
551 
552 static ssize_t alarm_store(struct device *dev, struct device_attribute *attr,
553 			   const char *buf, size_t count)
554 {
555 	struct adm9240_data *data = dev_get_drvdata(dev);
556 	struct i2c_client *client = data->client;
557 	unsigned long val;
558 
559 	if (kstrtoul(buf, 10, &val) || val != 0)
560 		return -EINVAL;
561 
562 	mutex_lock(&data->update_lock);
563 	i2c_smbus_write_byte_data(client, ADM9240_REG_CHASSIS_CLEAR, 0x80);
564 	data->valid = 0;		/* Force cache refresh */
565 	mutex_unlock(&data->update_lock);
566 	dev_dbg(&client->dev, "chassis intrusion latch cleared\n");
567 
568 	return count;
569 }
570 static SENSOR_DEVICE_ATTR_RW(intrusion0_alarm, alarm, 12);
571 
572 static struct attribute *adm9240_attrs[] = {
573 	&sensor_dev_attr_in0_input.dev_attr.attr,
574 	&sensor_dev_attr_in0_min.dev_attr.attr,
575 	&sensor_dev_attr_in0_max.dev_attr.attr,
576 	&sensor_dev_attr_in0_alarm.dev_attr.attr,
577 	&sensor_dev_attr_in1_input.dev_attr.attr,
578 	&sensor_dev_attr_in1_min.dev_attr.attr,
579 	&sensor_dev_attr_in1_max.dev_attr.attr,
580 	&sensor_dev_attr_in1_alarm.dev_attr.attr,
581 	&sensor_dev_attr_in2_input.dev_attr.attr,
582 	&sensor_dev_attr_in2_min.dev_attr.attr,
583 	&sensor_dev_attr_in2_max.dev_attr.attr,
584 	&sensor_dev_attr_in2_alarm.dev_attr.attr,
585 	&sensor_dev_attr_in3_input.dev_attr.attr,
586 	&sensor_dev_attr_in3_min.dev_attr.attr,
587 	&sensor_dev_attr_in3_max.dev_attr.attr,
588 	&sensor_dev_attr_in3_alarm.dev_attr.attr,
589 	&sensor_dev_attr_in4_input.dev_attr.attr,
590 	&sensor_dev_attr_in4_min.dev_attr.attr,
591 	&sensor_dev_attr_in4_max.dev_attr.attr,
592 	&sensor_dev_attr_in4_alarm.dev_attr.attr,
593 	&sensor_dev_attr_in5_input.dev_attr.attr,
594 	&sensor_dev_attr_in5_min.dev_attr.attr,
595 	&sensor_dev_attr_in5_max.dev_attr.attr,
596 	&sensor_dev_attr_in5_alarm.dev_attr.attr,
597 	&dev_attr_temp1_input.attr,
598 	&sensor_dev_attr_temp1_max.dev_attr.attr,
599 	&sensor_dev_attr_temp1_max_hyst.dev_attr.attr,
600 	&sensor_dev_attr_temp1_alarm.dev_attr.attr,
601 	&sensor_dev_attr_fan1_input.dev_attr.attr,
602 	&sensor_dev_attr_fan1_div.dev_attr.attr,
603 	&sensor_dev_attr_fan1_min.dev_attr.attr,
604 	&sensor_dev_attr_fan1_alarm.dev_attr.attr,
605 	&sensor_dev_attr_fan2_input.dev_attr.attr,
606 	&sensor_dev_attr_fan2_div.dev_attr.attr,
607 	&sensor_dev_attr_fan2_min.dev_attr.attr,
608 	&sensor_dev_attr_fan2_alarm.dev_attr.attr,
609 	&dev_attr_alarms.attr,
610 	&dev_attr_aout_output.attr,
611 	&sensor_dev_attr_intrusion0_alarm.dev_attr.attr,
612 	&dev_attr_cpu0_vid.attr,
613 	NULL
614 };
615 
616 ATTRIBUTE_GROUPS(adm9240);
617 
618 /*** sensor chip detect and driver install ***/
619 
620 /* Return 0 if detection is successful, -ENODEV otherwise */
621 static int adm9240_detect(struct i2c_client *new_client,
622 			  struct i2c_board_info *info)
623 {
624 	struct i2c_adapter *adapter = new_client->adapter;
625 	const char *name = "";
626 	int address = new_client->addr;
627 	u8 man_id, die_rev;
628 
629 	if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
630 		return -ENODEV;
631 
632 	/* verify chip: reg address should match i2c address */
633 	if (i2c_smbus_read_byte_data(new_client, ADM9240_REG_I2C_ADDR)
634 			!= address) {
635 		dev_err(&adapter->dev, "detect fail: address match, 0x%02x\n",
636 			address);
637 		return -ENODEV;
638 	}
639 
640 	/* check known chip manufacturer */
641 	man_id = i2c_smbus_read_byte_data(new_client, ADM9240_REG_MAN_ID);
642 	if (man_id == 0x23) {
643 		name = "adm9240";
644 	} else if (man_id == 0xda) {
645 		name = "ds1780";
646 	} else if (man_id == 0x01) {
647 		name = "lm81";
648 	} else {
649 		dev_err(&adapter->dev, "detect fail: unknown manuf, 0x%02x\n",
650 			man_id);
651 		return -ENODEV;
652 	}
653 
654 	/* successful detect, print chip info */
655 	die_rev = i2c_smbus_read_byte_data(new_client, ADM9240_REG_DIE_REV);
656 	dev_info(&adapter->dev, "found %s revision %u\n",
657 		 man_id == 0x23 ? "ADM9240" :
658 		 man_id == 0xda ? "DS1780" : "LM81", die_rev);
659 
660 	strlcpy(info->type, name, I2C_NAME_SIZE);
661 
662 	return 0;
663 }
664 
665 static void adm9240_init_client(struct i2c_client *client)
666 {
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;
670 
671 	data->vrm = vid_which_vrm(); /* need this to report vid as mV */
672 
673 	dev_info(&client->dev, "Using VRM: %d.%d\n", data->vrm / 10,
674 			data->vrm % 10);
675 
676 	if (conf & 1) { /* measurement cycle running: report state */
677 
678 		dev_info(&client->dev, "status: config 0x%02x mode %u\n",
679 				conf, mode);
680 
681 	} else { /* cold start: open limits before starting chip */
682 		int i;
683 
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);
689 		}
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);
698 
699 		/* start measurement cycle */
700 		i2c_smbus_write_byte_data(client, ADM9240_REG_CONFIG, 1);
701 
702 		dev_info(&client->dev,
703 			 "cold start: config was 0x%02x mode %u\n", conf, mode);
704 	}
705 }
706 
707 static int adm9240_probe(struct i2c_client *new_client,
708 			 const struct i2c_device_id *id)
709 {
710 	struct device *dev = &new_client->dev;
711 	struct device *hwmon_dev;
712 	struct adm9240_data *data;
713 
714 	data = devm_kzalloc(dev, sizeof(*data), GFP_KERNEL);
715 	if (!data)
716 		return -ENOMEM;
717 
718 	i2c_set_clientdata(new_client, data);
719 	data->client = new_client;
720 	mutex_init(&data->update_lock);
721 
722 	adm9240_init_client(new_client);
723 
724 	hwmon_dev = devm_hwmon_device_register_with_groups(dev,
725 							   new_client->name,
726 							   data,
727 							   adm9240_groups);
728 	return PTR_ERR_OR_ZERO(hwmon_dev);
729 }
730 
731 static const struct i2c_device_id adm9240_id[] = {
732 	{ "adm9240", adm9240 },
733 	{ "ds1780", ds1780 },
734 	{ "lm81", lm81 },
735 	{ }
736 };
737 MODULE_DEVICE_TABLE(i2c, adm9240_id);
738 
739 static struct i2c_driver adm9240_driver = {
740 	.class		= I2C_CLASS_HWMON,
741 	.driver = {
742 		.name	= "adm9240",
743 	},
744 	.probe		= adm9240_probe,
745 	.id_table	= adm9240_id,
746 	.detect		= adm9240_detect,
747 	.address_list	= normal_i2c,
748 };
749 
750 module_i2c_driver(adm9240_driver);
751 
752 MODULE_AUTHOR("Michiel Rook <michiel@grendelproject.nl>, "
753 		"Grant Coady <gcoady.lk@gmail.com> and others");
754 MODULE_DESCRIPTION("ADM9240/DS1780/LM81 driver");
755 MODULE_LICENSE("GPL");
756