xref: /linux/drivers/hwmon/vt1211.c (revision c31f4aa8fed048fa70e742c4bb49bb48dc489ab3)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * vt1211.c - driver for the VIA VT1211 Super-I/O chip integrated hardware
4  *            monitoring features
5  * Copyright (C) 2006 Juerg Haefliger <juergh@gmail.com>
6  *
7  * This driver is based on the driver for kernel 2.4 by Mark D. Studebaker
8  * and its port to kernel 2.6 by Lars Ekman.
9  */
10 
11 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
12 
13 #include <linux/module.h>
14 #include <linux/init.h>
15 #include <linux/slab.h>
16 #include <linux/jiffies.h>
17 #include <linux/platform_device.h>
18 #include <linux/hwmon.h>
19 #include <linux/hwmon-sysfs.h>
20 #include <linux/hwmon-vid.h>
21 #include <linux/err.h>
22 #include <linux/mutex.h>
23 #include <linux/ioport.h>
24 #include <linux/acpi.h>
25 #include <linux/io.h>
26 
27 static int uch_config = -1;
28 module_param(uch_config, int, 0);
29 MODULE_PARM_DESC(uch_config, "Initialize the universal channel configuration");
30 
31 static int int_mode = -1;
32 module_param(int_mode, int, 0);
33 MODULE_PARM_DESC(int_mode, "Force the temperature interrupt mode");
34 
35 static unsigned short force_id;
36 module_param(force_id, ushort, 0);
37 MODULE_PARM_DESC(force_id, "Override the detected device ID");
38 
39 static struct platform_device *pdev;
40 
41 #define DRVNAME "vt1211"
42 
43 /* ---------------------------------------------------------------------
44  * Registers
45  *
46  * The sensors are defined as follows.
47  *
48  * Sensor          Voltage Mode   Temp Mode   Notes (from the datasheet)
49  * --------        ------------   ---------   --------------------------
50  * Reading 1                      temp1       Intel thermal diode
51  * Reading 3                      temp2       Internal thermal diode
52  * UCH1/Reading2   in0            temp3       NTC type thermistor
53  * UCH2            in1            temp4       +2.5V
54  * UCH3            in2            temp5       VccP
55  * UCH4            in3            temp6       +5V
56  * UCH5            in4            temp7       +12V
57  * 3.3V            in5                        Internal VDD (+3.3V)
58  *
59  * --------------------------------------------------------------------- */
60 
61 /* Voltages (in) numbered 0-5 (ix) */
62 #define VT1211_REG_IN(ix)		(0x21 + (ix))
63 #define VT1211_REG_IN_MIN(ix)		((ix) == 0 ? 0x3e : 0x2a + 2 * (ix))
64 #define VT1211_REG_IN_MAX(ix)		((ix) == 0 ? 0x3d : 0x29 + 2 * (ix))
65 
66 /* Temperatures (temp) numbered 0-6 (ix) */
67 static u8 regtemp[]	= {0x1f, 0x20, 0x21, 0x22, 0x23, 0x24, 0x25};
68 static u8 regtempmax[]	= {0x39, 0x1d, 0x3d, 0x2b, 0x2d, 0x2f, 0x31};
69 static u8 regtemphyst[]	= {0x3a, 0x1e, 0x3e, 0x2c, 0x2e, 0x30, 0x32};
70 
71 /* Fans numbered 0-1 (ix) */
72 #define VT1211_REG_FAN(ix)		(0x29 + (ix))
73 #define VT1211_REG_FAN_MIN(ix)		(0x3b + (ix))
74 #define VT1211_REG_FAN_DIV		 0x47
75 
76 /* PWMs numbered 0-1 (ix) */
77 /* Auto points numbered 0-3 (ap) */
78 #define VT1211_REG_PWM(ix)		(0x60 + (ix))
79 #define VT1211_REG_PWM_CLK		 0x50
80 #define VT1211_REG_PWM_CTL		 0x51
81 #define VT1211_REG_PWM_AUTO_TEMP(ap)	(0x55 - (ap))
82 #define VT1211_REG_PWM_AUTO_PWM(ix, ap)	(0x58 + 2 * (ix) - (ap))
83 
84 /* Miscellaneous registers */
85 #define VT1211_REG_CONFIG		0x40
86 #define VT1211_REG_ALARM1		0x41
87 #define VT1211_REG_ALARM2		0x42
88 #define VT1211_REG_VID			0x45
89 #define VT1211_REG_UCH_CONFIG		0x4a
90 #define VT1211_REG_TEMP1_CONFIG		0x4b
91 #define VT1211_REG_TEMP2_CONFIG		0x4c
92 
93 /* In, temp & fan alarm bits */
94 static const u8 bitalarmin[]	= {11, 0, 1, 3, 8, 2, 9};
95 static const u8 bitalarmtemp[]	= {4, 15, 11, 0, 1, 3, 8};
96 static const u8 bitalarmfan[]	= {6, 7};
97 
98 /* ---------------------------------------------------------------------
99  * Data structures and manipulation thereof
100  * --------------------------------------------------------------------- */
101 
102 struct vt1211_data {
103 	unsigned short addr;
104 	const char *name;
105 	struct device *hwmon_dev;
106 
107 	struct mutex update_lock;
108 	bool valid;			/* true if following fields are valid */
109 	unsigned long last_updated;	/* In jiffies */
110 
111 	/* Register values */
112 	u8  in[6];
113 	u8  in_max[6];
114 	u8  in_min[6];
115 	u8  temp[7];
116 	u8  temp_max[7];
117 	u8  temp_hyst[7];
118 	u8  fan[2];
119 	u8  fan_min[2];
120 	u8  fan_div[2];
121 	u8  fan_ctl;
122 	u8  pwm[2];
123 	u8  pwm_ctl[2];
124 	u8  pwm_clk;
125 	u8  pwm_auto_temp[4];
126 	u8  pwm_auto_pwm[2][4];
127 	u8  vid;		/* Read once at init time */
128 	u8  vrm;
129 	u8  uch_config;		/* Read once at init time */
130 	u16 alarms;
131 };
132 
133 /* ix = [0-5] */
134 #define ISVOLT(ix, uch_config)	((ix) > 4 ? 1 : \
135 				 !(((uch_config) >> ((ix) + 2)) & 1))
136 
137 /* ix = [0-6] */
138 #define ISTEMP(ix, uch_config)	((ix) < 2 ? 1 : \
139 				 ((uch_config) >> (ix)) & 1)
140 
141 /*
142  * in5 (ix = 5) is special. It's the internal 3.3V so it's scaled in the
143  * driver according to the VT1211 BIOS porting guide
144  */
145 static int in_from_reg(int ix, int reg)
146 {
147 	if (reg < 3)
148 		return 0;
149 	if (ix == 5)
150 		return ((reg - 3) * 15882 + 479) / 958;
151 	return ((reg - 3) * 10000 + 479) / 958;
152 }
153 
154 #define IN_TO_REG(ix, val)	(clamp_val((ix) == 5 ? \
155 				 ((val) * 958 + 7941) / 15882 + 3 : \
156 				 ((val) * 958 + 5000) / 10000 + 3, 0, 255))
157 
158 /*
159  * temp1 (ix = 0) is an intel thermal diode which is scaled in user space.
160  * temp2 (ix = 1) is the internal temp diode so it's scaled in the driver
161  * according to some measurements that I took on an EPIA M10000.
162  * temp3-7 are thermistor based so the driver returns the voltage measured at
163  * the pin (range 0V - 2.2V).
164  */
165 static int temp_from_reg(int ix, int reg)
166 {
167 	if (ix == 0)
168 		return reg * 1000;
169 	if (ix == 1)
170 		return reg < 51 ? 0 : (reg - 51) * 1000;
171 	return ((253 - reg) * 2200 + 105) / 210;
172 }
173 
174 #define TEMP_TO_REG(ix, val)	clamp_val( \
175 				 ((ix) == 0 ? ((val) + 500) / 1000 : \
176 				  (ix) == 1 ? ((val) + 500) / 1000 + 51 : \
177 				  253 - ((val) * 210 + 1100) / 2200), 0, 255)
178 
179 #define DIV_FROM_REG(reg)	(1 << (reg))
180 
181 static int rpm_from_reg(int reg, int div)
182 {
183 	if (reg == 0 || reg == 255)
184 		return 0;
185 
186 	return 1310720 / reg / DIV_FROM_REG(div);
187 }
188 
189 #define RPM_TO_REG(val, div)	((val) == 0 ? 255 : \
190 				 clamp_val((1310720 / (val) / \
191 				 DIV_FROM_REG(div)), 1, 254))
192 
193 /* ---------------------------------------------------------------------
194  * Super-I/O constants and functions
195  * --------------------------------------------------------------------- */
196 
197 /*
198  * Configuration index port registers
199  * The vt1211 can live at 2 different addresses so we need to probe both
200  */
201 #define SIO_REG_CIP1		0x2e
202 #define SIO_REG_CIP2		0x4e
203 
204 /* Configuration registers */
205 #define SIO_VT1211_LDN		0x07	/* logical device number */
206 #define SIO_VT1211_DEVID	0x20	/* device ID */
207 #define SIO_VT1211_DEVREV	0x21	/* device revision */
208 #define SIO_VT1211_ACTIVE	0x30	/* HW monitor active */
209 #define SIO_VT1211_BADDR	0x60	/* base I/O address */
210 #define SIO_VT1211_ID		0x3c	/* VT1211 device ID */
211 
212 /* VT1211 logical device numbers */
213 #define SIO_VT1211_LDN_HWMON	0x0b	/* HW monitor */
214 
215 static inline int superio_inb(int sio_cip, int reg)
216 {
217 	outb(reg, sio_cip);
218 	return inb(sio_cip + 1);
219 }
220 
221 static inline void superio_select(int sio_cip, int ldn)
222 {
223 	outb(SIO_VT1211_LDN, sio_cip);
224 	outb(ldn, sio_cip + 1);
225 }
226 
227 static inline int superio_enter(int sio_cip)
228 {
229 	if (!request_muxed_region(sio_cip, 2, DRVNAME))
230 		return -EBUSY;
231 
232 	outb(0x87, sio_cip);
233 	outb(0x87, sio_cip);
234 
235 	return 0;
236 }
237 
238 static inline void superio_exit(int sio_cip)
239 {
240 	outb(0xaa, sio_cip);
241 	release_region(sio_cip, 2);
242 }
243 
244 /* ---------------------------------------------------------------------
245  * Device I/O access
246  * --------------------------------------------------------------------- */
247 
248 static inline u8 vt1211_read8(struct vt1211_data *data, u8 reg)
249 {
250 	return inb(data->addr + reg);
251 }
252 
253 static inline void vt1211_write8(struct vt1211_data *data, u8 reg, u8 val)
254 {
255 	outb(val, data->addr + reg);
256 }
257 
258 static struct vt1211_data *vt1211_update_device(struct device *dev)
259 {
260 	struct vt1211_data *data = dev_get_drvdata(dev);
261 	int ix, val;
262 
263 	mutex_lock(&data->update_lock);
264 
265 	/* registers cache is refreshed after 1 second */
266 	if (time_after(jiffies, data->last_updated + HZ) || !data->valid) {
267 		/* read VID */
268 		data->vid = vt1211_read8(data, VT1211_REG_VID) & 0x1f;
269 
270 		/* voltage (in) registers */
271 		for (ix = 0; ix < ARRAY_SIZE(data->in); ix++) {
272 			if (ISVOLT(ix, data->uch_config)) {
273 				data->in[ix] = vt1211_read8(data,
274 						VT1211_REG_IN(ix));
275 				data->in_min[ix] = vt1211_read8(data,
276 						VT1211_REG_IN_MIN(ix));
277 				data->in_max[ix] = vt1211_read8(data,
278 						VT1211_REG_IN_MAX(ix));
279 			}
280 		}
281 
282 		/* temp registers */
283 		for (ix = 0; ix < ARRAY_SIZE(data->temp); ix++) {
284 			if (ISTEMP(ix, data->uch_config)) {
285 				data->temp[ix] = vt1211_read8(data,
286 						regtemp[ix]);
287 				data->temp_max[ix] = vt1211_read8(data,
288 						regtempmax[ix]);
289 				data->temp_hyst[ix] = vt1211_read8(data,
290 						regtemphyst[ix]);
291 			}
292 		}
293 
294 		/* fan & pwm registers */
295 		for (ix = 0; ix < ARRAY_SIZE(data->fan); ix++) {
296 			data->fan[ix] = vt1211_read8(data,
297 						VT1211_REG_FAN(ix));
298 			data->fan_min[ix] = vt1211_read8(data,
299 						VT1211_REG_FAN_MIN(ix));
300 			data->pwm[ix] = vt1211_read8(data,
301 						VT1211_REG_PWM(ix));
302 		}
303 		val = vt1211_read8(data, VT1211_REG_FAN_DIV);
304 		data->fan_div[0] = (val >> 4) & 3;
305 		data->fan_div[1] = (val >> 6) & 3;
306 		data->fan_ctl = val & 0xf;
307 
308 		val = vt1211_read8(data, VT1211_REG_PWM_CTL);
309 		data->pwm_ctl[0] = val & 0xf;
310 		data->pwm_ctl[1] = (val >> 4) & 0xf;
311 
312 		data->pwm_clk = vt1211_read8(data, VT1211_REG_PWM_CLK);
313 
314 		/* pwm & temp auto point registers */
315 		data->pwm_auto_pwm[0][1] = vt1211_read8(data,
316 						VT1211_REG_PWM_AUTO_PWM(0, 1));
317 		data->pwm_auto_pwm[0][2] = vt1211_read8(data,
318 						VT1211_REG_PWM_AUTO_PWM(0, 2));
319 		data->pwm_auto_pwm[1][1] = vt1211_read8(data,
320 						VT1211_REG_PWM_AUTO_PWM(1, 1));
321 		data->pwm_auto_pwm[1][2] = vt1211_read8(data,
322 						VT1211_REG_PWM_AUTO_PWM(1, 2));
323 		for (ix = 0; ix < ARRAY_SIZE(data->pwm_auto_temp); ix++) {
324 			data->pwm_auto_temp[ix] = vt1211_read8(data,
325 						VT1211_REG_PWM_AUTO_TEMP(ix));
326 		}
327 
328 		/* alarm registers */
329 		data->alarms = (vt1211_read8(data, VT1211_REG_ALARM2) << 8) |
330 				vt1211_read8(data, VT1211_REG_ALARM1);
331 
332 		data->last_updated = jiffies;
333 		data->valid = true;
334 	}
335 
336 	mutex_unlock(&data->update_lock);
337 
338 	return data;
339 }
340 
341 /* ---------------------------------------------------------------------
342  * Voltage sysfs interfaces
343  * ix = [0-5]
344  * --------------------------------------------------------------------- */
345 
346 #define SHOW_IN_INPUT	0
347 #define SHOW_SET_IN_MIN	1
348 #define SHOW_SET_IN_MAX	2
349 #define SHOW_IN_ALARM	3
350 
351 static ssize_t show_in(struct device *dev, struct device_attribute *attr,
352 		       char *buf)
353 {
354 	struct vt1211_data *data = vt1211_update_device(dev);
355 	struct sensor_device_attribute_2 *sensor_attr_2 =
356 						to_sensor_dev_attr_2(attr);
357 	int ix = sensor_attr_2->index;
358 	int fn = sensor_attr_2->nr;
359 	int res;
360 
361 	switch (fn) {
362 	case SHOW_IN_INPUT:
363 		res = in_from_reg(ix, data->in[ix]);
364 		break;
365 	case SHOW_SET_IN_MIN:
366 		res = in_from_reg(ix, data->in_min[ix]);
367 		break;
368 	case SHOW_SET_IN_MAX:
369 		res = in_from_reg(ix, data->in_max[ix]);
370 		break;
371 	case SHOW_IN_ALARM:
372 		res = (data->alarms >> bitalarmin[ix]) & 1;
373 		break;
374 	default:
375 		res = 0;
376 		dev_dbg(dev, "Unknown attr fetch (%d)\n", fn);
377 	}
378 
379 	return sprintf(buf, "%d\n", res);
380 }
381 
382 static ssize_t set_in(struct device *dev, struct device_attribute *attr,
383 		      const char *buf, size_t count)
384 {
385 	struct vt1211_data *data = dev_get_drvdata(dev);
386 	struct sensor_device_attribute_2 *sensor_attr_2 =
387 						to_sensor_dev_attr_2(attr);
388 	int ix = sensor_attr_2->index;
389 	int fn = sensor_attr_2->nr;
390 	long val;
391 	int err;
392 
393 	err = kstrtol(buf, 10, &val);
394 	if (err)
395 		return err;
396 
397 	mutex_lock(&data->update_lock);
398 	switch (fn) {
399 	case SHOW_SET_IN_MIN:
400 		data->in_min[ix] = IN_TO_REG(ix, val);
401 		vt1211_write8(data, VT1211_REG_IN_MIN(ix), data->in_min[ix]);
402 		break;
403 	case SHOW_SET_IN_MAX:
404 		data->in_max[ix] = IN_TO_REG(ix, val);
405 		vt1211_write8(data, VT1211_REG_IN_MAX(ix), data->in_max[ix]);
406 		break;
407 	default:
408 		dev_dbg(dev, "Unknown attr fetch (%d)\n", fn);
409 	}
410 	mutex_unlock(&data->update_lock);
411 
412 	return count;
413 }
414 
415 /* ---------------------------------------------------------------------
416  * Temperature sysfs interfaces
417  * ix = [0-6]
418  * --------------------------------------------------------------------- */
419 
420 #define SHOW_TEMP_INPUT		0
421 #define SHOW_SET_TEMP_MAX	1
422 #define SHOW_SET_TEMP_MAX_HYST	2
423 #define SHOW_TEMP_ALARM		3
424 
425 static ssize_t show_temp(struct device *dev, struct device_attribute *attr,
426 			 char *buf)
427 {
428 	struct vt1211_data *data = vt1211_update_device(dev);
429 	struct sensor_device_attribute_2 *sensor_attr_2 =
430 						to_sensor_dev_attr_2(attr);
431 	int ix = sensor_attr_2->index;
432 	int fn = sensor_attr_2->nr;
433 	int res;
434 
435 	switch (fn) {
436 	case SHOW_TEMP_INPUT:
437 		res = temp_from_reg(ix, data->temp[ix]);
438 		break;
439 	case SHOW_SET_TEMP_MAX:
440 		res = temp_from_reg(ix, data->temp_max[ix]);
441 		break;
442 	case SHOW_SET_TEMP_MAX_HYST:
443 		res = temp_from_reg(ix, data->temp_hyst[ix]);
444 		break;
445 	case SHOW_TEMP_ALARM:
446 		res = (data->alarms >> bitalarmtemp[ix]) & 1;
447 		break;
448 	default:
449 		res = 0;
450 		dev_dbg(dev, "Unknown attr fetch (%d)\n", fn);
451 	}
452 
453 	return sprintf(buf, "%d\n", res);
454 }
455 
456 static ssize_t set_temp(struct device *dev, struct device_attribute *attr,
457 			const char *buf, size_t count)
458 {
459 	struct vt1211_data *data = dev_get_drvdata(dev);
460 	struct sensor_device_attribute_2 *sensor_attr_2 =
461 						to_sensor_dev_attr_2(attr);
462 	int ix = sensor_attr_2->index;
463 	int fn = sensor_attr_2->nr;
464 	long val;
465 	int err;
466 
467 	err = kstrtol(buf, 10, &val);
468 	if (err)
469 		return err;
470 
471 	mutex_lock(&data->update_lock);
472 	switch (fn) {
473 	case SHOW_SET_TEMP_MAX:
474 		data->temp_max[ix] = TEMP_TO_REG(ix, val);
475 		vt1211_write8(data, regtempmax[ix],
476 			      data->temp_max[ix]);
477 		break;
478 	case SHOW_SET_TEMP_MAX_HYST:
479 		data->temp_hyst[ix] = TEMP_TO_REG(ix, val);
480 		vt1211_write8(data, regtemphyst[ix],
481 			      data->temp_hyst[ix]);
482 		break;
483 	default:
484 		dev_dbg(dev, "Unknown attr fetch (%d)\n", fn);
485 	}
486 	mutex_unlock(&data->update_lock);
487 
488 	return count;
489 }
490 
491 /* ---------------------------------------------------------------------
492  * Fan sysfs interfaces
493  * ix = [0-1]
494  * --------------------------------------------------------------------- */
495 
496 #define SHOW_FAN_INPUT		0
497 #define SHOW_SET_FAN_MIN	1
498 #define SHOW_SET_FAN_DIV	2
499 #define SHOW_FAN_ALARM		3
500 
501 static ssize_t show_fan(struct device *dev, struct device_attribute *attr,
502 			char *buf)
503 {
504 	struct vt1211_data *data = vt1211_update_device(dev);
505 	struct sensor_device_attribute_2 *sensor_attr_2 =
506 						to_sensor_dev_attr_2(attr);
507 	int ix = sensor_attr_2->index;
508 	int fn = sensor_attr_2->nr;
509 	int res;
510 
511 	switch (fn) {
512 	case SHOW_FAN_INPUT:
513 		res = rpm_from_reg(data->fan[ix], data->fan_div[ix]);
514 		break;
515 	case SHOW_SET_FAN_MIN:
516 		res = rpm_from_reg(data->fan_min[ix], data->fan_div[ix]);
517 		break;
518 	case SHOW_SET_FAN_DIV:
519 		res = DIV_FROM_REG(data->fan_div[ix]);
520 		break;
521 	case SHOW_FAN_ALARM:
522 		res = (data->alarms >> bitalarmfan[ix]) & 1;
523 		break;
524 	default:
525 		res = 0;
526 		dev_dbg(dev, "Unknown attr fetch (%d)\n", fn);
527 	}
528 
529 	return sprintf(buf, "%d\n", res);
530 }
531 
532 static ssize_t set_fan(struct device *dev, struct device_attribute *attr,
533 		       const char *buf, size_t count)
534 {
535 	struct vt1211_data *data = dev_get_drvdata(dev);
536 	struct sensor_device_attribute_2 *sensor_attr_2 =
537 						to_sensor_dev_attr_2(attr);
538 	int ix = sensor_attr_2->index;
539 	int fn = sensor_attr_2->nr;
540 	int reg;
541 	unsigned long val;
542 	int err;
543 
544 	err = kstrtoul(buf, 10, &val);
545 	if (err)
546 		return err;
547 
548 	mutex_lock(&data->update_lock);
549 
550 	/* sync the data cache */
551 	reg = vt1211_read8(data, VT1211_REG_FAN_DIV);
552 	data->fan_div[0] = (reg >> 4) & 3;
553 	data->fan_div[1] = (reg >> 6) & 3;
554 	data->fan_ctl = reg & 0xf;
555 
556 	switch (fn) {
557 	case SHOW_SET_FAN_MIN:
558 		data->fan_min[ix] = RPM_TO_REG(val, data->fan_div[ix]);
559 		vt1211_write8(data, VT1211_REG_FAN_MIN(ix),
560 			      data->fan_min[ix]);
561 		break;
562 	case SHOW_SET_FAN_DIV:
563 		switch (val) {
564 		case 1:
565 			data->fan_div[ix] = 0;
566 			break;
567 		case 2:
568 			data->fan_div[ix] = 1;
569 			break;
570 		case 4:
571 			data->fan_div[ix] = 2;
572 			break;
573 		case 8:
574 			data->fan_div[ix] = 3;
575 			break;
576 		default:
577 			count = -EINVAL;
578 			dev_warn(dev,
579 				 "fan div value %ld not supported. Choose one of 1, 2, 4, or 8.\n",
580 				 val);
581 			goto EXIT;
582 		}
583 		vt1211_write8(data, VT1211_REG_FAN_DIV,
584 			      ((data->fan_div[1] << 6) |
585 			       (data->fan_div[0] << 4) |
586 				data->fan_ctl));
587 		break;
588 	default:
589 		dev_dbg(dev, "Unknown attr fetch (%d)\n", fn);
590 	}
591 
592 EXIT:
593 	mutex_unlock(&data->update_lock);
594 	return count;
595 }
596 
597 /* ---------------------------------------------------------------------
598  * PWM sysfs interfaces
599  * ix = [0-1]
600  * --------------------------------------------------------------------- */
601 
602 #define SHOW_PWM			0
603 #define SHOW_SET_PWM_ENABLE		1
604 #define SHOW_SET_PWM_FREQ		2
605 #define SHOW_SET_PWM_AUTO_CHANNELS_TEMP	3
606 
607 static ssize_t show_pwm(struct device *dev, struct device_attribute *attr,
608 			char *buf)
609 {
610 	struct vt1211_data *data = vt1211_update_device(dev);
611 	struct sensor_device_attribute_2 *sensor_attr_2 =
612 						to_sensor_dev_attr_2(attr);
613 	int ix = sensor_attr_2->index;
614 	int fn = sensor_attr_2->nr;
615 	int res;
616 
617 	switch (fn) {
618 	case SHOW_PWM:
619 		res = data->pwm[ix];
620 		break;
621 	case SHOW_SET_PWM_ENABLE:
622 		res = ((data->pwm_ctl[ix] >> 3) & 1) ? 2 : 0;
623 		break;
624 	case SHOW_SET_PWM_FREQ:
625 		res = 90000 >> (data->pwm_clk & 7);
626 		break;
627 	case SHOW_SET_PWM_AUTO_CHANNELS_TEMP:
628 		res = (data->pwm_ctl[ix] & 7) + 1;
629 		break;
630 	default:
631 		res = 0;
632 		dev_dbg(dev, "Unknown attr fetch (%d)\n", fn);
633 	}
634 
635 	return sprintf(buf, "%d\n", res);
636 }
637 
638 static ssize_t set_pwm(struct device *dev, struct device_attribute *attr,
639 		       const char *buf, size_t count)
640 {
641 	struct vt1211_data *data = dev_get_drvdata(dev);
642 	struct sensor_device_attribute_2 *sensor_attr_2 =
643 						to_sensor_dev_attr_2(attr);
644 	int ix = sensor_attr_2->index;
645 	int fn = sensor_attr_2->nr;
646 	int tmp, reg;
647 	unsigned long val;
648 	int err;
649 
650 	err = kstrtoul(buf, 10, &val);
651 	if (err)
652 		return err;
653 
654 	mutex_lock(&data->update_lock);
655 
656 	switch (fn) {
657 	case SHOW_SET_PWM_ENABLE:
658 		/* sync the data cache */
659 		reg = vt1211_read8(data, VT1211_REG_FAN_DIV);
660 		data->fan_div[0] = (reg >> 4) & 3;
661 		data->fan_div[1] = (reg >> 6) & 3;
662 		data->fan_ctl = reg & 0xf;
663 		reg = vt1211_read8(data, VT1211_REG_PWM_CTL);
664 		data->pwm_ctl[0] = reg & 0xf;
665 		data->pwm_ctl[1] = (reg >> 4) & 0xf;
666 		switch (val) {
667 		case 0:
668 			data->pwm_ctl[ix] &= 7;
669 			/*
670 			 * disable SmartGuardian if both PWM outputs are
671 			 * disabled
672 			 */
673 			if ((data->pwm_ctl[ix ^ 1] & 1) == 0)
674 				data->fan_ctl &= 0xe;
675 			break;
676 		case 2:
677 			data->pwm_ctl[ix] |= 8;
678 			data->fan_ctl |= 1;
679 			break;
680 		default:
681 			count = -EINVAL;
682 			dev_warn(dev,
683 				 "pwm mode %ld not supported. Choose one of 0 or 2.\n",
684 				 val);
685 			goto EXIT;
686 		}
687 		vt1211_write8(data, VT1211_REG_PWM_CTL,
688 			      ((data->pwm_ctl[1] << 4) |
689 				data->pwm_ctl[0]));
690 		vt1211_write8(data, VT1211_REG_FAN_DIV,
691 			      ((data->fan_div[1] << 6) |
692 			       (data->fan_div[0] << 4) |
693 				data->fan_ctl));
694 		break;
695 	case SHOW_SET_PWM_FREQ:
696 		val = 135000 / clamp_val(val, 135000 >> 7, 135000);
697 		/* calculate tmp = log2(val) */
698 		tmp = 0;
699 		for (val >>= 1; val > 0; val >>= 1)
700 			tmp++;
701 		/* sync the data cache */
702 		reg = vt1211_read8(data, VT1211_REG_PWM_CLK);
703 		data->pwm_clk = (reg & 0xf8) | tmp;
704 		vt1211_write8(data, VT1211_REG_PWM_CLK, data->pwm_clk);
705 		break;
706 	case SHOW_SET_PWM_AUTO_CHANNELS_TEMP:
707 		if (val < 1 || val > 7) {
708 			count = -EINVAL;
709 			dev_warn(dev,
710 				 "temp channel %ld not supported. Choose a value between 1 and 7.\n",
711 				 val);
712 			goto EXIT;
713 		}
714 		if (!ISTEMP(val - 1, data->uch_config)) {
715 			count = -EINVAL;
716 			dev_warn(dev, "temp channel %ld is not available.\n",
717 				 val);
718 			goto EXIT;
719 		}
720 		/* sync the data cache */
721 		reg = vt1211_read8(data, VT1211_REG_PWM_CTL);
722 		data->pwm_ctl[0] = reg & 0xf;
723 		data->pwm_ctl[1] = (reg >> 4) & 0xf;
724 		data->pwm_ctl[ix] = (data->pwm_ctl[ix] & 8) | (val - 1);
725 		vt1211_write8(data, VT1211_REG_PWM_CTL,
726 			      ((data->pwm_ctl[1] << 4) | data->pwm_ctl[0]));
727 		break;
728 	default:
729 		dev_dbg(dev, "Unknown attr fetch (%d)\n", fn);
730 	}
731 
732 EXIT:
733 	mutex_unlock(&data->update_lock);
734 	return count;
735 }
736 
737 /* ---------------------------------------------------------------------
738  * PWM auto point definitions
739  * ix = [0-1]
740  * ap = [0-3]
741  * --------------------------------------------------------------------- */
742 
743 /*
744  * pwm[ix+1]_auto_point[ap+1]_temp mapping table:
745  * Note that there is only a single set of temp auto points that controls both
746  * PWM controllers. We still create 2 sets of sysfs files to make it look
747  * more consistent even though they map to the same registers.
748  *
749  * ix ap : description
750  * -------------------
751  * 0  0  : pwm1/2 off temperature        (pwm_auto_temp[0])
752  * 0  1  : pwm1/2 low speed temperature  (pwm_auto_temp[1])
753  * 0  2  : pwm1/2 high speed temperature (pwm_auto_temp[2])
754  * 0  3  : pwm1/2 full speed temperature (pwm_auto_temp[3])
755  * 1  0  : pwm1/2 off temperature        (pwm_auto_temp[0])
756  * 1  1  : pwm1/2 low speed temperature  (pwm_auto_temp[1])
757  * 1  2  : pwm1/2 high speed temperature (pwm_auto_temp[2])
758  * 1  3  : pwm1/2 full speed temperature (pwm_auto_temp[3])
759  */
760 
761 static ssize_t show_pwm_auto_point_temp(struct device *dev,
762 					struct device_attribute *attr,
763 					char *buf)
764 {
765 	struct vt1211_data *data = vt1211_update_device(dev);
766 	struct sensor_device_attribute_2 *sensor_attr_2 =
767 						to_sensor_dev_attr_2(attr);
768 	int ix = sensor_attr_2->index;
769 	int ap = sensor_attr_2->nr;
770 
771 	return sprintf(buf, "%d\n", temp_from_reg(data->pwm_ctl[ix] & 7,
772 		       data->pwm_auto_temp[ap]));
773 }
774 
775 static ssize_t set_pwm_auto_point_temp(struct device *dev,
776 				       struct device_attribute *attr,
777 				       const char *buf, size_t count)
778 {
779 	struct vt1211_data *data = dev_get_drvdata(dev);
780 	struct sensor_device_attribute_2 *sensor_attr_2 =
781 						to_sensor_dev_attr_2(attr);
782 	int ix = sensor_attr_2->index;
783 	int ap = sensor_attr_2->nr;
784 	int reg;
785 	long val;
786 	int err;
787 
788 	err = kstrtol(buf, 10, &val);
789 	if (err)
790 		return err;
791 
792 
793 	mutex_lock(&data->update_lock);
794 
795 	/* sync the data cache */
796 	reg = vt1211_read8(data, VT1211_REG_PWM_CTL);
797 	data->pwm_ctl[0] = reg & 0xf;
798 	data->pwm_ctl[1] = (reg >> 4) & 0xf;
799 
800 	data->pwm_auto_temp[ap] = TEMP_TO_REG(data->pwm_ctl[ix] & 7, val);
801 	vt1211_write8(data, VT1211_REG_PWM_AUTO_TEMP(ap),
802 		      data->pwm_auto_temp[ap]);
803 	mutex_unlock(&data->update_lock);
804 
805 	return count;
806 }
807 
808 /*
809  * pwm[ix+1]_auto_point[ap+1]_pwm mapping table:
810  * Note that the PWM auto points 0 & 3 are hard-wired in the VT1211 and can't
811  * be changed.
812  *
813  * ix ap : description
814  * -------------------
815  * 0  0  : pwm1 off                   (pwm_auto_pwm[0][0], hard-wired to 0)
816  * 0  1  : pwm1 low speed duty cycle  (pwm_auto_pwm[0][1])
817  * 0  2  : pwm1 high speed duty cycle (pwm_auto_pwm[0][2])
818  * 0  3  : pwm1 full speed            (pwm_auto_pwm[0][3], hard-wired to 255)
819  * 1  0  : pwm2 off                   (pwm_auto_pwm[1][0], hard-wired to 0)
820  * 1  1  : pwm2 low speed duty cycle  (pwm_auto_pwm[1][1])
821  * 1  2  : pwm2 high speed duty cycle (pwm_auto_pwm[1][2])
822  * 1  3  : pwm2 full speed            (pwm_auto_pwm[1][3], hard-wired to 255)
823  */
824 
825 static ssize_t show_pwm_auto_point_pwm(struct device *dev,
826 				       struct device_attribute *attr,
827 				       char *buf)
828 {
829 	struct vt1211_data *data = vt1211_update_device(dev);
830 	struct sensor_device_attribute_2 *sensor_attr_2 =
831 						to_sensor_dev_attr_2(attr);
832 	int ix = sensor_attr_2->index;
833 	int ap = sensor_attr_2->nr;
834 
835 	return sprintf(buf, "%d\n", data->pwm_auto_pwm[ix][ap]);
836 }
837 
838 static ssize_t set_pwm_auto_point_pwm(struct device *dev,
839 				      struct device_attribute *attr,
840 				      const char *buf, size_t count)
841 {
842 	struct vt1211_data *data = dev_get_drvdata(dev);
843 	struct sensor_device_attribute_2 *sensor_attr_2 =
844 						to_sensor_dev_attr_2(attr);
845 	int ix = sensor_attr_2->index;
846 	int ap = sensor_attr_2->nr;
847 	unsigned long val;
848 	int err;
849 
850 	err = kstrtoul(buf, 10, &val);
851 	if (err)
852 		return err;
853 
854 	mutex_lock(&data->update_lock);
855 	data->pwm_auto_pwm[ix][ap] = clamp_val(val, 0, 255);
856 	vt1211_write8(data, VT1211_REG_PWM_AUTO_PWM(ix, ap),
857 		      data->pwm_auto_pwm[ix][ap]);
858 	mutex_unlock(&data->update_lock);
859 
860 	return count;
861 }
862 
863 /* ---------------------------------------------------------------------
864  * Miscellaneous sysfs interfaces (VRM, VID, name, and (legacy) alarms)
865  * --------------------------------------------------------------------- */
866 
867 static ssize_t show_vrm(struct device *dev, struct device_attribute *attr,
868 			char *buf)
869 {
870 	struct vt1211_data *data = dev_get_drvdata(dev);
871 
872 	return sprintf(buf, "%d\n", data->vrm);
873 }
874 
875 static ssize_t set_vrm(struct device *dev, struct device_attribute *attr,
876 		       const char *buf, size_t count)
877 {
878 	struct vt1211_data *data = dev_get_drvdata(dev);
879 	unsigned long val;
880 	int err;
881 
882 	err = kstrtoul(buf, 10, &val);
883 	if (err)
884 		return err;
885 
886 	if (val > 255)
887 		return -EINVAL;
888 
889 	data->vrm = val;
890 
891 	return count;
892 }
893 
894 static ssize_t show_vid(struct device *dev, struct device_attribute *attr,
895 			char *buf)
896 {
897 	struct vt1211_data *data = dev_get_drvdata(dev);
898 
899 	return sprintf(buf, "%d\n", vid_from_reg(data->vid, data->vrm));
900 }
901 
902 static ssize_t show_name(struct device *dev,
903 			 struct device_attribute *attr, char *buf)
904 {
905 	struct vt1211_data *data = dev_get_drvdata(dev);
906 
907 	return sprintf(buf, "%s\n", data->name);
908 }
909 
910 static ssize_t show_alarms(struct device *dev,
911 			   struct device_attribute *attr, char *buf)
912 {
913 	struct vt1211_data *data = vt1211_update_device(dev);
914 
915 	return sprintf(buf, "%d\n", data->alarms);
916 }
917 
918 /* ---------------------------------------------------------------------
919  * Device attribute structs
920  * --------------------------------------------------------------------- */
921 
922 #define SENSOR_ATTR_IN(ix) \
923 {	SENSOR_ATTR_2(in##ix##_input, S_IRUGO, \
924 		show_in, NULL, SHOW_IN_INPUT, ix), \
925 	SENSOR_ATTR_2(in##ix##_min, S_IRUGO | S_IWUSR, \
926 		show_in, set_in, SHOW_SET_IN_MIN, ix), \
927 	SENSOR_ATTR_2(in##ix##_max, S_IRUGO | S_IWUSR, \
928 		show_in, set_in, SHOW_SET_IN_MAX, ix), \
929 	SENSOR_ATTR_2(in##ix##_alarm, S_IRUGO, \
930 		show_in, NULL, SHOW_IN_ALARM, ix) \
931 }
932 
933 static struct sensor_device_attribute_2 vt1211_sysfs_in[][4] = {
934 	SENSOR_ATTR_IN(0),
935 	SENSOR_ATTR_IN(1),
936 	SENSOR_ATTR_IN(2),
937 	SENSOR_ATTR_IN(3),
938 	SENSOR_ATTR_IN(4),
939 	SENSOR_ATTR_IN(5)
940 };
941 
942 #define IN_UNIT_ATTRS(X)			\
943 {	&vt1211_sysfs_in[X][0].dev_attr.attr,	\
944 	&vt1211_sysfs_in[X][1].dev_attr.attr,	\
945 	&vt1211_sysfs_in[X][2].dev_attr.attr,	\
946 	&vt1211_sysfs_in[X][3].dev_attr.attr,	\
947 	NULL					\
948 }
949 
950 static struct attribute *vt1211_in_attr[][5] = {
951 	IN_UNIT_ATTRS(0),
952 	IN_UNIT_ATTRS(1),
953 	IN_UNIT_ATTRS(2),
954 	IN_UNIT_ATTRS(3),
955 	IN_UNIT_ATTRS(4),
956 	IN_UNIT_ATTRS(5)
957 };
958 
959 static const struct attribute_group vt1211_in_attr_group[] = {
960 	{ .attrs = vt1211_in_attr[0] },
961 	{ .attrs = vt1211_in_attr[1] },
962 	{ .attrs = vt1211_in_attr[2] },
963 	{ .attrs = vt1211_in_attr[3] },
964 	{ .attrs = vt1211_in_attr[4] },
965 	{ .attrs = vt1211_in_attr[5] }
966 };
967 
968 #define SENSOR_ATTR_TEMP(ix) \
969 {	SENSOR_ATTR_2(temp##ix##_input, S_IRUGO, \
970 		show_temp, NULL, SHOW_TEMP_INPUT, ix-1), \
971 	SENSOR_ATTR_2(temp##ix##_max, S_IRUGO | S_IWUSR, \
972 		show_temp, set_temp, SHOW_SET_TEMP_MAX, ix-1), \
973 	SENSOR_ATTR_2(temp##ix##_max_hyst, S_IRUGO | S_IWUSR, \
974 		show_temp, set_temp, SHOW_SET_TEMP_MAX_HYST, ix-1), \
975 	SENSOR_ATTR_2(temp##ix##_alarm, S_IRUGO, \
976 		show_temp, NULL, SHOW_TEMP_ALARM, ix-1) \
977 }
978 
979 static struct sensor_device_attribute_2 vt1211_sysfs_temp[][4] = {
980 	SENSOR_ATTR_TEMP(1),
981 	SENSOR_ATTR_TEMP(2),
982 	SENSOR_ATTR_TEMP(3),
983 	SENSOR_ATTR_TEMP(4),
984 	SENSOR_ATTR_TEMP(5),
985 	SENSOR_ATTR_TEMP(6),
986 	SENSOR_ATTR_TEMP(7),
987 };
988 
989 #define TEMP_UNIT_ATTRS(X)			\
990 {	&vt1211_sysfs_temp[X][0].dev_attr.attr,	\
991 	&vt1211_sysfs_temp[X][1].dev_attr.attr,	\
992 	&vt1211_sysfs_temp[X][2].dev_attr.attr,	\
993 	&vt1211_sysfs_temp[X][3].dev_attr.attr,	\
994 	NULL					\
995 }
996 
997 static struct attribute *vt1211_temp_attr[][5] = {
998 	TEMP_UNIT_ATTRS(0),
999 	TEMP_UNIT_ATTRS(1),
1000 	TEMP_UNIT_ATTRS(2),
1001 	TEMP_UNIT_ATTRS(3),
1002 	TEMP_UNIT_ATTRS(4),
1003 	TEMP_UNIT_ATTRS(5),
1004 	TEMP_UNIT_ATTRS(6)
1005 };
1006 
1007 static const struct attribute_group vt1211_temp_attr_group[] = {
1008 	{ .attrs = vt1211_temp_attr[0] },
1009 	{ .attrs = vt1211_temp_attr[1] },
1010 	{ .attrs = vt1211_temp_attr[2] },
1011 	{ .attrs = vt1211_temp_attr[3] },
1012 	{ .attrs = vt1211_temp_attr[4] },
1013 	{ .attrs = vt1211_temp_attr[5] },
1014 	{ .attrs = vt1211_temp_attr[6] }
1015 };
1016 
1017 #define SENSOR_ATTR_FAN(ix) \
1018 	SENSOR_ATTR_2(fan##ix##_input, S_IRUGO, \
1019 		show_fan, NULL, SHOW_FAN_INPUT, ix-1), \
1020 	SENSOR_ATTR_2(fan##ix##_min, S_IRUGO | S_IWUSR, \
1021 		show_fan, set_fan, SHOW_SET_FAN_MIN, ix-1), \
1022 	SENSOR_ATTR_2(fan##ix##_div, S_IRUGO | S_IWUSR, \
1023 		show_fan, set_fan, SHOW_SET_FAN_DIV, ix-1), \
1024 	SENSOR_ATTR_2(fan##ix##_alarm, S_IRUGO, \
1025 		show_fan, NULL, SHOW_FAN_ALARM, ix-1)
1026 
1027 #define SENSOR_ATTR_PWM(ix) \
1028 	SENSOR_ATTR_2(pwm##ix, S_IRUGO, \
1029 		show_pwm, NULL, SHOW_PWM, ix-1), \
1030 	SENSOR_ATTR_2(pwm##ix##_enable, S_IRUGO | S_IWUSR, \
1031 		show_pwm, set_pwm, SHOW_SET_PWM_ENABLE, ix-1), \
1032 	SENSOR_ATTR_2(pwm##ix##_auto_channels_temp, S_IRUGO | S_IWUSR, \
1033 		show_pwm, set_pwm, SHOW_SET_PWM_AUTO_CHANNELS_TEMP, ix-1)
1034 
1035 #define SENSOR_ATTR_PWM_FREQ(ix) \
1036 	SENSOR_ATTR_2(pwm##ix##_freq, S_IRUGO | S_IWUSR, \
1037 		show_pwm, set_pwm, SHOW_SET_PWM_FREQ, ix-1)
1038 
1039 #define SENSOR_ATTR_PWM_FREQ_RO(ix) \
1040 	SENSOR_ATTR_2(pwm##ix##_freq, S_IRUGO, \
1041 		show_pwm, NULL, SHOW_SET_PWM_FREQ, ix-1)
1042 
1043 #define SENSOR_ATTR_PWM_AUTO_POINT_TEMP(ix, ap) \
1044 	SENSOR_ATTR_2(pwm##ix##_auto_point##ap##_temp, S_IRUGO | S_IWUSR, \
1045 		show_pwm_auto_point_temp, set_pwm_auto_point_temp, \
1046 		ap-1, ix-1)
1047 
1048 #define SENSOR_ATTR_PWM_AUTO_POINT_TEMP_RO(ix, ap) \
1049 	SENSOR_ATTR_2(pwm##ix##_auto_point##ap##_temp, S_IRUGO, \
1050 		show_pwm_auto_point_temp, NULL, \
1051 		ap-1, ix-1)
1052 
1053 #define SENSOR_ATTR_PWM_AUTO_POINT_PWM(ix, ap) \
1054 	SENSOR_ATTR_2(pwm##ix##_auto_point##ap##_pwm, S_IRUGO | S_IWUSR, \
1055 		show_pwm_auto_point_pwm, set_pwm_auto_point_pwm, \
1056 		ap-1, ix-1)
1057 
1058 #define SENSOR_ATTR_PWM_AUTO_POINT_PWM_RO(ix, ap) \
1059 	SENSOR_ATTR_2(pwm##ix##_auto_point##ap##_pwm, S_IRUGO, \
1060 		show_pwm_auto_point_pwm, NULL, \
1061 		ap-1, ix-1)
1062 
1063 static struct sensor_device_attribute_2 vt1211_sysfs_fan_pwm[] = {
1064 	SENSOR_ATTR_FAN(1),
1065 	SENSOR_ATTR_FAN(2),
1066 	SENSOR_ATTR_PWM(1),
1067 	SENSOR_ATTR_PWM(2),
1068 	SENSOR_ATTR_PWM_FREQ(1),
1069 	SENSOR_ATTR_PWM_FREQ_RO(2),
1070 	SENSOR_ATTR_PWM_AUTO_POINT_TEMP(1, 1),
1071 	SENSOR_ATTR_PWM_AUTO_POINT_TEMP(1, 2),
1072 	SENSOR_ATTR_PWM_AUTO_POINT_TEMP(1, 3),
1073 	SENSOR_ATTR_PWM_AUTO_POINT_TEMP(1, 4),
1074 	SENSOR_ATTR_PWM_AUTO_POINT_TEMP_RO(2, 1),
1075 	SENSOR_ATTR_PWM_AUTO_POINT_TEMP_RO(2, 2),
1076 	SENSOR_ATTR_PWM_AUTO_POINT_TEMP_RO(2, 3),
1077 	SENSOR_ATTR_PWM_AUTO_POINT_TEMP_RO(2, 4),
1078 	SENSOR_ATTR_PWM_AUTO_POINT_PWM_RO(1, 1),
1079 	SENSOR_ATTR_PWM_AUTO_POINT_PWM(1, 2),
1080 	SENSOR_ATTR_PWM_AUTO_POINT_PWM(1, 3),
1081 	SENSOR_ATTR_PWM_AUTO_POINT_PWM_RO(1, 4),
1082 	SENSOR_ATTR_PWM_AUTO_POINT_PWM_RO(2, 1),
1083 	SENSOR_ATTR_PWM_AUTO_POINT_PWM(2, 2),
1084 	SENSOR_ATTR_PWM_AUTO_POINT_PWM(2, 3),
1085 	SENSOR_ATTR_PWM_AUTO_POINT_PWM_RO(2, 4),
1086 };
1087 
1088 static struct device_attribute vt1211_sysfs_misc[] = {
1089 	__ATTR(vrm, S_IRUGO | S_IWUSR, show_vrm, set_vrm),
1090 	__ATTR(cpu0_vid, S_IRUGO, show_vid, NULL),
1091 	__ATTR(name, S_IRUGO, show_name, NULL),
1092 	__ATTR(alarms, S_IRUGO, show_alarms, NULL),
1093 };
1094 
1095 /* ---------------------------------------------------------------------
1096  * Device registration and initialization
1097  * --------------------------------------------------------------------- */
1098 
1099 static void vt1211_init_device(struct vt1211_data *data)
1100 {
1101 	/* set VRM */
1102 	data->vrm = vid_which_vrm();
1103 
1104 	/* Read (and initialize) UCH config */
1105 	data->uch_config = vt1211_read8(data, VT1211_REG_UCH_CONFIG);
1106 	if (uch_config > -1) {
1107 		data->uch_config = (data->uch_config & 0x83) |
1108 				   (uch_config << 2);
1109 		vt1211_write8(data, VT1211_REG_UCH_CONFIG, data->uch_config);
1110 	}
1111 
1112 	/*
1113 	 * Initialize the interrupt mode (if request at module load time).
1114 	 * The VT1211 implements 3 different modes for clearing interrupts:
1115 	 * 0: Clear INT when status register is read. Regenerate INT as long
1116 	 *    as temp stays above hysteresis limit.
1117 	 * 1: Clear INT when status register is read. DON'T regenerate INT
1118 	 *    until temp falls below hysteresis limit and exceeds hot limit
1119 	 *    again.
1120 	 * 2: Clear INT when temp falls below max limit.
1121 	 *
1122 	 * The driver only allows to force mode 0 since that's the only one
1123 	 * that makes sense for 'sensors'
1124 	 */
1125 	if (int_mode == 0) {
1126 		vt1211_write8(data, VT1211_REG_TEMP1_CONFIG, 0);
1127 		vt1211_write8(data, VT1211_REG_TEMP2_CONFIG, 0);
1128 	}
1129 
1130 	/* Fill in some hard wired values into our data struct */
1131 	data->pwm_auto_pwm[0][3] = 255;
1132 	data->pwm_auto_pwm[1][3] = 255;
1133 }
1134 
1135 static void vt1211_remove_sysfs(struct platform_device *pdev)
1136 {
1137 	struct device *dev = &pdev->dev;
1138 	int i;
1139 
1140 	for (i = 0; i < ARRAY_SIZE(vt1211_in_attr_group); i++)
1141 		sysfs_remove_group(&dev->kobj, &vt1211_in_attr_group[i]);
1142 
1143 	for (i = 0; i < ARRAY_SIZE(vt1211_temp_attr_group); i++)
1144 		sysfs_remove_group(&dev->kobj, &vt1211_temp_attr_group[i]);
1145 
1146 	for (i = 0; i < ARRAY_SIZE(vt1211_sysfs_fan_pwm); i++) {
1147 		device_remove_file(dev,
1148 			&vt1211_sysfs_fan_pwm[i].dev_attr);
1149 	}
1150 	for (i = 0; i < ARRAY_SIZE(vt1211_sysfs_misc); i++)
1151 		device_remove_file(dev, &vt1211_sysfs_misc[i]);
1152 }
1153 
1154 static int vt1211_probe(struct platform_device *pdev)
1155 {
1156 	struct device *dev = &pdev->dev;
1157 	struct vt1211_data *data;
1158 	struct resource *res;
1159 	int i, err;
1160 
1161 	data = devm_kzalloc(dev, sizeof(struct vt1211_data), GFP_KERNEL);
1162 	if (!data)
1163 		return -ENOMEM;
1164 
1165 	res = platform_get_resource(pdev, IORESOURCE_IO, 0);
1166 	if (!devm_request_region(dev, res->start, resource_size(res),
1167 				 DRVNAME)) {
1168 		dev_err(dev, "Failed to request region 0x%lx-0x%lx\n",
1169 			(unsigned long)res->start, (unsigned long)res->end);
1170 		return -EBUSY;
1171 	}
1172 	data->addr = res->start;
1173 	data->name = DRVNAME;
1174 	mutex_init(&data->update_lock);
1175 
1176 	platform_set_drvdata(pdev, data);
1177 
1178 	/* Initialize the VT1211 chip */
1179 	vt1211_init_device(data);
1180 
1181 	/* Create sysfs interface files */
1182 	for (i = 0; i < ARRAY_SIZE(vt1211_in_attr_group); i++) {
1183 		if (ISVOLT(i, data->uch_config)) {
1184 			err = sysfs_create_group(&dev->kobj,
1185 						 &vt1211_in_attr_group[i]);
1186 			if (err)
1187 				goto EXIT_DEV_REMOVE;
1188 		}
1189 	}
1190 	for (i = 0; i < ARRAY_SIZE(vt1211_temp_attr_group); i++) {
1191 		if (ISTEMP(i, data->uch_config)) {
1192 			err = sysfs_create_group(&dev->kobj,
1193 						 &vt1211_temp_attr_group[i]);
1194 			if (err)
1195 				goto EXIT_DEV_REMOVE;
1196 		}
1197 	}
1198 	for (i = 0; i < ARRAY_SIZE(vt1211_sysfs_fan_pwm); i++) {
1199 		err = device_create_file(dev,
1200 			&vt1211_sysfs_fan_pwm[i].dev_attr);
1201 		if (err)
1202 			goto EXIT_DEV_REMOVE;
1203 	}
1204 	for (i = 0; i < ARRAY_SIZE(vt1211_sysfs_misc); i++) {
1205 		err = device_create_file(dev,
1206 		       &vt1211_sysfs_misc[i]);
1207 		if (err)
1208 			goto EXIT_DEV_REMOVE;
1209 	}
1210 
1211 	/* Register device */
1212 	data->hwmon_dev = hwmon_device_register(dev);
1213 	if (IS_ERR(data->hwmon_dev)) {
1214 		err = PTR_ERR(data->hwmon_dev);
1215 		dev_err(dev, "Class registration failed (%d)\n", err);
1216 		goto EXIT_DEV_REMOVE_SILENT;
1217 	}
1218 
1219 	return 0;
1220 
1221 EXIT_DEV_REMOVE:
1222 	dev_err(dev, "Sysfs interface creation failed (%d)\n", err);
1223 EXIT_DEV_REMOVE_SILENT:
1224 	vt1211_remove_sysfs(pdev);
1225 	return err;
1226 }
1227 
1228 static void vt1211_remove(struct platform_device *pdev)
1229 {
1230 	struct vt1211_data *data = platform_get_drvdata(pdev);
1231 
1232 	hwmon_device_unregister(data->hwmon_dev);
1233 	vt1211_remove_sysfs(pdev);
1234 }
1235 
1236 static struct platform_driver vt1211_driver = {
1237 	.driver = {
1238 		.name  = DRVNAME,
1239 	},
1240 	.probe  = vt1211_probe,
1241 	.remove = vt1211_remove,
1242 };
1243 
1244 static int __init vt1211_device_add(unsigned short address)
1245 {
1246 	struct resource res = {
1247 		.start	= address,
1248 		.end	= address + 0x7f,
1249 		.flags	= IORESOURCE_IO,
1250 	};
1251 	int err;
1252 
1253 	pdev = platform_device_alloc(DRVNAME, address);
1254 	if (!pdev) {
1255 		err = -ENOMEM;
1256 		pr_err("Device allocation failed (%d)\n", err);
1257 		goto EXIT;
1258 	}
1259 
1260 	res.name = pdev->name;
1261 	err = acpi_check_resource_conflict(&res);
1262 	if (err)
1263 		goto EXIT_DEV_PUT;
1264 
1265 	err = platform_device_add_resources(pdev, &res, 1);
1266 	if (err) {
1267 		pr_err("Device resource addition failed (%d)\n", err);
1268 		goto EXIT_DEV_PUT;
1269 	}
1270 
1271 	err = platform_device_add(pdev);
1272 	if (err) {
1273 		pr_err("Device addition failed (%d)\n", err);
1274 		goto EXIT_DEV_PUT;
1275 	}
1276 
1277 	return 0;
1278 
1279 EXIT_DEV_PUT:
1280 	platform_device_put(pdev);
1281 EXIT:
1282 	return err;
1283 }
1284 
1285 static int __init vt1211_find(int sio_cip, unsigned short *address)
1286 {
1287 	int err;
1288 	int devid;
1289 
1290 	err = superio_enter(sio_cip);
1291 	if (err)
1292 		return err;
1293 
1294 	err = -ENODEV;
1295 	devid = force_id ? force_id : superio_inb(sio_cip, SIO_VT1211_DEVID);
1296 	if (devid != SIO_VT1211_ID)
1297 		goto EXIT;
1298 
1299 	superio_select(sio_cip, SIO_VT1211_LDN_HWMON);
1300 
1301 	if ((superio_inb(sio_cip, SIO_VT1211_ACTIVE) & 1) == 0) {
1302 		pr_warn("HW monitor is disabled, skipping\n");
1303 		goto EXIT;
1304 	}
1305 
1306 	*address = ((superio_inb(sio_cip, SIO_VT1211_BADDR) << 8) |
1307 		    (superio_inb(sio_cip, SIO_VT1211_BADDR + 1))) & 0xff00;
1308 	if (*address == 0) {
1309 		pr_warn("Base address is not set, skipping\n");
1310 		goto EXIT;
1311 	}
1312 
1313 	err = 0;
1314 	pr_info("Found VT1211 chip at 0x%04x, revision %u\n",
1315 		*address, superio_inb(sio_cip, SIO_VT1211_DEVREV));
1316 
1317 EXIT:
1318 	superio_exit(sio_cip);
1319 	return err;
1320 }
1321 
1322 static int __init vt1211_init(void)
1323 {
1324 	int err;
1325 	unsigned short address = 0;
1326 
1327 	err = vt1211_find(SIO_REG_CIP1, &address);
1328 	if (err) {
1329 		err = vt1211_find(SIO_REG_CIP2, &address);
1330 		if (err)
1331 			goto EXIT;
1332 	}
1333 
1334 	if ((uch_config < -1) || (uch_config > 31)) {
1335 		err = -EINVAL;
1336 		pr_warn("Invalid UCH configuration %d. Choose a value between 0 and 31.\n",
1337 			uch_config);
1338 		goto EXIT;
1339 	}
1340 
1341 	if ((int_mode < -1) || (int_mode > 0)) {
1342 		err = -EINVAL;
1343 		pr_warn("Invalid interrupt mode %d. Only mode 0 is supported.\n",
1344 			int_mode);
1345 		goto EXIT;
1346 	}
1347 
1348 	err = platform_driver_register(&vt1211_driver);
1349 	if (err)
1350 		goto EXIT;
1351 
1352 	/* Sets global pdev as a side effect */
1353 	err = vt1211_device_add(address);
1354 	if (err)
1355 		goto EXIT_DRV_UNREGISTER;
1356 
1357 	return 0;
1358 
1359 EXIT_DRV_UNREGISTER:
1360 	platform_driver_unregister(&vt1211_driver);
1361 EXIT:
1362 	return err;
1363 }
1364 
1365 static void __exit vt1211_exit(void)
1366 {
1367 	platform_device_unregister(pdev);
1368 	platform_driver_unregister(&vt1211_driver);
1369 }
1370 
1371 MODULE_AUTHOR("Juerg Haefliger <juergh@gmail.com>");
1372 MODULE_DESCRIPTION("VT1211 sensors");
1373 MODULE_LICENSE("GPL");
1374 
1375 module_init(vt1211_init);
1376 module_exit(vt1211_exit);
1377