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