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