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