xref: /openbmc/linux/drivers/hwmon/w83627hf.c (revision 1edd0337)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * w83627hf.c - Part of lm_sensors, Linux kernel modules for hardware
4  *		monitoring
5  * Copyright (c) 1998 - 2003  Frodo Looijaard <frodol@dds.nl>,
6  *			      Philip Edelbrock <phil@netroedge.com>,
7  *			      and Mark Studebaker <mdsxyz123@yahoo.com>
8  * Ported to 2.6 by Bernhard C. Schrenk <clemy@clemy.org>
9  * Copyright (c) 2007 - 1012  Jean Delvare <jdelvare@suse.de>
10  */
11 
12 /*
13  * Supports following chips:
14  *
15  * Chip		#vin	#fanin	#pwm	#temp	wchipid	vendid	i2c	ISA
16  * w83627hf	9	3	2	3	0x20	0x5ca3	no	yes(LPC)
17  * w83627thf	7	3	3	3	0x90	0x5ca3	no	yes(LPC)
18  * w83637hf	7	3	3	3	0x80	0x5ca3	no	yes(LPC)
19  * w83687thf	7	3	3	3	0x90	0x5ca3	no	yes(LPC)
20  * w83697hf	8	2	2	2	0x60	0x5ca3	no	yes(LPC)
21  *
22  * For other winbond chips, and for i2c support in the above chips,
23  * use w83781d.c.
24  *
25  * Note: automatic ("cruise") fan control for 697, 637 & 627thf not
26  * supported yet.
27  */
28 
29 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
30 
31 #include <linux/module.h>
32 #include <linux/init.h>
33 #include <linux/slab.h>
34 #include <linux/jiffies.h>
35 #include <linux/platform_device.h>
36 #include <linux/hwmon.h>
37 #include <linux/hwmon-sysfs.h>
38 #include <linux/hwmon-vid.h>
39 #include <linux/err.h>
40 #include <linux/mutex.h>
41 #include <linux/ioport.h>
42 #include <linux/acpi.h>
43 #include <linux/io.h>
44 #include "lm75.h"
45 
46 static struct platform_device *pdev;
47 
48 #define DRVNAME "w83627hf"
49 enum chips { w83627hf, w83627thf, w83697hf, w83637hf, w83687thf };
50 
51 struct w83627hf_sio_data {
52 	enum chips type;
53 	int sioaddr;
54 };
55 
56 static u8 force_i2c = 0x1f;
57 module_param(force_i2c, byte, 0);
58 MODULE_PARM_DESC(force_i2c,
59 		 "Initialize the i2c address of the sensors");
60 
61 static bool init = 1;
62 module_param(init, bool, 0);
63 MODULE_PARM_DESC(init, "Set to zero to bypass chip initialization");
64 
65 static unsigned short force_id;
66 module_param(force_id, ushort, 0);
67 MODULE_PARM_DESC(force_id, "Override the detected device ID");
68 
69 /* modified from kernel/include/traps.c */
70 #define DEV			0x07 /* Register: Logical device select */
71 
72 /* logical device numbers for superio_select (below) */
73 #define W83627HF_LD_FDC		0x00
74 #define W83627HF_LD_PRT		0x01
75 #define W83627HF_LD_UART1	0x02
76 #define W83627HF_LD_UART2	0x03
77 #define W83627HF_LD_KBC		0x05
78 #define W83627HF_LD_CIR		0x06 /* w83627hf only */
79 #define W83627HF_LD_GAME	0x07
80 #define W83627HF_LD_MIDI	0x07
81 #define W83627HF_LD_GPIO1	0x07
82 #define W83627HF_LD_GPIO5	0x07 /* w83627thf only */
83 #define W83627HF_LD_GPIO2	0x08
84 #define W83627HF_LD_GPIO3	0x09
85 #define W83627HF_LD_GPIO4	0x09 /* w83627thf only */
86 #define W83627HF_LD_ACPI	0x0a
87 #define W83627HF_LD_HWM		0x0b
88 
89 #define DEVID			0x20 /* Register: Device ID */
90 
91 #define W83627THF_GPIO5_EN	0x30 /* w83627thf only */
92 #define W83627THF_GPIO5_IOSR	0xf3 /* w83627thf only */
93 #define W83627THF_GPIO5_DR	0xf4 /* w83627thf only */
94 
95 #define W83687THF_VID_EN	0x29 /* w83687thf only */
96 #define W83687THF_VID_CFG	0xF0 /* w83687thf only */
97 #define W83687THF_VID_DATA	0xF1 /* w83687thf only */
98 
99 static inline void
100 superio_outb(struct w83627hf_sio_data *sio, int reg, int val)
101 {
102 	outb(reg, sio->sioaddr);
103 	outb(val, sio->sioaddr + 1);
104 }
105 
106 static inline int
107 superio_inb(struct w83627hf_sio_data *sio, int reg)
108 {
109 	outb(reg, sio->sioaddr);
110 	return inb(sio->sioaddr + 1);
111 }
112 
113 static inline void
114 superio_select(struct w83627hf_sio_data *sio, int ld)
115 {
116 	outb(DEV, sio->sioaddr);
117 	outb(ld,  sio->sioaddr + 1);
118 }
119 
120 static inline int
121 superio_enter(struct w83627hf_sio_data *sio)
122 {
123 	if (!request_muxed_region(sio->sioaddr, 2, DRVNAME))
124 		return -EBUSY;
125 
126 	outb(0x87, sio->sioaddr);
127 	outb(0x87, sio->sioaddr);
128 
129 	return 0;
130 }
131 
132 static inline void
133 superio_exit(struct w83627hf_sio_data *sio)
134 {
135 	outb(0xAA, sio->sioaddr);
136 	release_region(sio->sioaddr, 2);
137 }
138 
139 #define W627_DEVID 0x52
140 #define W627THF_DEVID 0x82
141 #define W697_DEVID 0x60
142 #define W637_DEVID 0x70
143 #define W687THF_DEVID 0x85
144 #define WINB_ACT_REG 0x30
145 #define WINB_BASE_REG 0x60
146 /* Constants specified below */
147 
148 /* Alignment of the base address */
149 #define WINB_ALIGNMENT		~7
150 
151 /* Offset & size of I/O region we are interested in */
152 #define WINB_REGION_OFFSET	5
153 #define WINB_REGION_SIZE	2
154 
155 /* Where are the sensors address/data registers relative to the region offset */
156 #define W83781D_ADDR_REG_OFFSET 0
157 #define W83781D_DATA_REG_OFFSET 1
158 
159 /* The W83781D registers */
160 /* The W83782D registers for nr=7,8 are in bank 5 */
161 #define W83781D_REG_IN_MAX(nr) ((nr < 7) ? (0x2b + (nr) * 2) : \
162 					   (0x554 + (((nr) - 7) * 2)))
163 #define W83781D_REG_IN_MIN(nr) ((nr < 7) ? (0x2c + (nr) * 2) : \
164 					   (0x555 + (((nr) - 7) * 2)))
165 #define W83781D_REG_IN(nr)     ((nr < 7) ? (0x20 + (nr)) : \
166 					   (0x550 + (nr) - 7))
167 
168 /* nr:0-2 for fans:1-3 */
169 #define W83627HF_REG_FAN_MIN(nr)	(0x3b + (nr))
170 #define W83627HF_REG_FAN(nr)		(0x28 + (nr))
171 
172 #define W83627HF_REG_TEMP2_CONFIG 0x152
173 #define W83627HF_REG_TEMP3_CONFIG 0x252
174 /* these are zero-based, unlike config constants above */
175 static const u16 w83627hf_reg_temp[]		= { 0x27, 0x150, 0x250 };
176 static const u16 w83627hf_reg_temp_hyst[]	= { 0x3A, 0x153, 0x253 };
177 static const u16 w83627hf_reg_temp_over[]	= { 0x39, 0x155, 0x255 };
178 
179 #define W83781D_REG_BANK 0x4E
180 
181 #define W83781D_REG_CONFIG 0x40
182 #define W83781D_REG_ALARM1 0x459
183 #define W83781D_REG_ALARM2 0x45A
184 #define W83781D_REG_ALARM3 0x45B
185 
186 #define W83781D_REG_BEEP_CONFIG 0x4D
187 #define W83781D_REG_BEEP_INTS1 0x56
188 #define W83781D_REG_BEEP_INTS2 0x57
189 #define W83781D_REG_BEEP_INTS3 0x453
190 
191 #define W83781D_REG_VID_FANDIV 0x47
192 
193 #define W83781D_REG_CHIPID 0x49
194 #define W83781D_REG_WCHIPID 0x58
195 #define W83781D_REG_CHIPMAN 0x4F
196 #define W83781D_REG_PIN 0x4B
197 
198 #define W83781D_REG_VBAT 0x5D
199 
200 #define W83627HF_REG_PWM1 0x5A
201 #define W83627HF_REG_PWM2 0x5B
202 
203 static const u8 W83627THF_REG_PWM_ENABLE[] = {
204 	0x04,		/* FAN 1 mode */
205 	0x04,		/* FAN 2 mode */
206 	0x12,		/* FAN AUX mode */
207 };
208 static const u8 W83627THF_PWM_ENABLE_SHIFT[] = { 2, 4, 1 };
209 
210 #define W83627THF_REG_PWM1		0x01	/* 697HF/637HF/687THF too */
211 #define W83627THF_REG_PWM2		0x03	/* 697HF/637HF/687THF too */
212 #define W83627THF_REG_PWM3		0x11	/* 637HF/687THF too */
213 
214 #define W83627THF_REG_VRM_OVT_CFG 	0x18	/* 637HF/687THF too */
215 
216 static const u8 regpwm_627hf[] = { W83627HF_REG_PWM1, W83627HF_REG_PWM2 };
217 static const u8 regpwm[] = { W83627THF_REG_PWM1, W83627THF_REG_PWM2,
218                              W83627THF_REG_PWM3 };
219 #define W836X7HF_REG_PWM(type, nr) (((type) == w83627hf) ? \
220 				    regpwm_627hf[nr] : regpwm[nr])
221 
222 #define W83627HF_REG_PWM_FREQ		0x5C	/* Only for the 627HF */
223 
224 #define W83637HF_REG_PWM_FREQ1		0x00	/* 697HF/687THF too */
225 #define W83637HF_REG_PWM_FREQ2		0x02	/* 697HF/687THF too */
226 #define W83637HF_REG_PWM_FREQ3		0x10	/* 687THF too */
227 
228 static const u8 W83637HF_REG_PWM_FREQ[] = { W83637HF_REG_PWM_FREQ1,
229 					W83637HF_REG_PWM_FREQ2,
230 					W83637HF_REG_PWM_FREQ3 };
231 
232 #define W83627HF_BASE_PWM_FREQ	46870
233 
234 #define W83781D_REG_I2C_ADDR 0x48
235 #define W83781D_REG_I2C_SUBADDR 0x4A
236 
237 /* Sensor selection */
238 #define W83781D_REG_SCFG1 0x5D
239 static const u8 BIT_SCFG1[] = { 0x02, 0x04, 0x08 };
240 #define W83781D_REG_SCFG2 0x59
241 static const u8 BIT_SCFG2[] = { 0x10, 0x20, 0x40 };
242 #define W83781D_DEFAULT_BETA 3435
243 
244 /*
245  * Conversions. Limit checking is only done on the TO_REG
246  * variants. Note that you should be a bit careful with which arguments
247  * these macros are called: arguments may be evaluated more than once.
248  * Fixing this is just not worth it.
249  */
250 #define IN_TO_REG(val)  (clamp_val((((val) + 8) / 16), 0, 255))
251 #define IN_FROM_REG(val) ((val) * 16)
252 
253 static inline u8 FAN_TO_REG(long rpm, int div)
254 {
255 	if (rpm == 0)
256 		return 255;
257 	rpm = clamp_val(rpm, 1, 1000000);
258 	return clamp_val((1350000 + rpm * div / 2) / (rpm * div), 1, 254);
259 }
260 
261 #define TEMP_MIN (-128000)
262 #define TEMP_MAX ( 127000)
263 
264 /*
265  * TEMP: 0.001C/bit (-128C to +127C)
266  * REG: 1C/bit, two's complement
267  */
268 static u8 TEMP_TO_REG(long temp)
269 {
270 	int ntemp = clamp_val(temp, TEMP_MIN, TEMP_MAX);
271 	ntemp += (ntemp < 0 ? -500 : 500);
272 	return (u8)(ntemp / 1000);
273 }
274 
275 static int TEMP_FROM_REG(u8 reg)
276 {
277         return (s8)reg * 1000;
278 }
279 
280 #define FAN_FROM_REG(val,div) ((val)==0?-1:(val)==255?0:1350000/((val)*(div)))
281 
282 #define PWM_TO_REG(val) (clamp_val((val), 0, 255))
283 
284 static inline unsigned long pwm_freq_from_reg_627hf(u8 reg)
285 {
286 	unsigned long freq;
287 	freq = W83627HF_BASE_PWM_FREQ >> reg;
288 	return freq;
289 }
290 static inline u8 pwm_freq_to_reg_627hf(unsigned long val)
291 {
292 	u8 i;
293 	/*
294 	 * Only 5 dividers (1 2 4 8 16)
295 	 * Search for the nearest available frequency
296 	 */
297 	for (i = 0; i < 4; i++) {
298 		if (val > (((W83627HF_BASE_PWM_FREQ >> i) +
299 			    (W83627HF_BASE_PWM_FREQ >> (i+1))) / 2))
300 			break;
301 	}
302 	return i;
303 }
304 
305 static inline unsigned long pwm_freq_from_reg(u8 reg)
306 {
307 	/* Clock bit 8 -> 180 kHz or 24 MHz */
308 	unsigned long clock = (reg & 0x80) ? 180000UL : 24000000UL;
309 
310 	reg &= 0x7f;
311 	/* This should not happen but anyway... */
312 	if (reg == 0)
313 		reg++;
314 	return clock / (reg << 8);
315 }
316 static inline u8 pwm_freq_to_reg(unsigned long val)
317 {
318 	/* Minimum divider value is 0x01 and maximum is 0x7F */
319 	if (val >= 93750)	/* The highest we can do */
320 		return 0x01;
321 	if (val >= 720)	/* Use 24 MHz clock */
322 		return 24000000UL / (val << 8);
323 	if (val < 6)		/* The lowest we can do */
324 		return 0xFF;
325 	else			/* Use 180 kHz clock */
326 		return 0x80 | (180000UL / (val << 8));
327 }
328 
329 #define BEEP_MASK_FROM_REG(val)		((val) & 0xff7fff)
330 #define BEEP_MASK_TO_REG(val)		((val) & 0xff7fff)
331 
332 #define DIV_FROM_REG(val) (1 << (val))
333 
334 static inline u8 DIV_TO_REG(long val)
335 {
336 	int i;
337 	val = clamp_val(val, 1, 128) >> 1;
338 	for (i = 0; i < 7; i++) {
339 		if (val == 0)
340 			break;
341 		val >>= 1;
342 	}
343 	return (u8)i;
344 }
345 
346 /*
347  * For each registered chip, we need to keep some data in memory.
348  * The structure is dynamically allocated.
349  */
350 struct w83627hf_data {
351 	unsigned short addr;
352 	const char *name;
353 	struct device *hwmon_dev;
354 	struct mutex lock;
355 	enum chips type;
356 
357 	struct mutex update_lock;
358 	bool valid;		/* true if following fields are valid */
359 	unsigned long last_updated;	/* In jiffies */
360 
361 	u8 in[9];		/* Register value */
362 	u8 in_max[9];		/* Register value */
363 	u8 in_min[9];		/* Register value */
364 	u8 fan[3];		/* Register value */
365 	u8 fan_min[3];		/* Register value */
366 	u16 temp[3];		/* Register value */
367 	u16 temp_max[3];	/* Register value */
368 	u16 temp_max_hyst[3];	/* Register value */
369 	u8 fan_div[3];		/* Register encoding, shifted right */
370 	u8 vid;			/* Register encoding, combined */
371 	u32 alarms;		/* Register encoding, combined */
372 	u32 beep_mask;		/* Register encoding, combined */
373 	u8 pwm[3];		/* Register value */
374 	u8 pwm_enable[3];	/* 1 = manual
375 				 * 2 = thermal cruise (also called SmartFan I)
376 				 * 3 = fan speed cruise
377 				 */
378 	u8 pwm_freq[3];		/* Register value */
379 	u16 sens[3];		/* 1 = pentium diode; 2 = 3904 diode;
380 				 * 4 = thermistor
381 				 */
382 	u8 vrm;
383 	u8 vrm_ovt;		/* Register value, 627THF/637HF/687THF only */
384 
385 #ifdef CONFIG_PM
386 	/* Remember extra register values over suspend/resume */
387 	u8 scfg1;
388 	u8 scfg2;
389 #endif
390 };
391 
392 /* Registers 0x50-0x5f are banked */
393 static inline void w83627hf_set_bank(struct w83627hf_data *data, u16 reg)
394 {
395 	if ((reg & 0x00f0) == 0x50) {
396 		outb_p(W83781D_REG_BANK, data->addr + W83781D_ADDR_REG_OFFSET);
397 		outb_p(reg >> 8, data->addr + W83781D_DATA_REG_OFFSET);
398 	}
399 }
400 
401 /* Not strictly necessary, but play it safe for now */
402 static inline void w83627hf_reset_bank(struct w83627hf_data *data, u16 reg)
403 {
404 	if (reg & 0xff00) {
405 		outb_p(W83781D_REG_BANK, data->addr + W83781D_ADDR_REG_OFFSET);
406 		outb_p(0, data->addr + W83781D_DATA_REG_OFFSET);
407 	}
408 }
409 
410 static int w83627hf_read_value(struct w83627hf_data *data, u16 reg)
411 {
412 	int res, word_sized;
413 
414 	mutex_lock(&data->lock);
415 	word_sized = (((reg & 0xff00) == 0x100)
416 		   || ((reg & 0xff00) == 0x200))
417 		  && (((reg & 0x00ff) == 0x50)
418 		   || ((reg & 0x00ff) == 0x53)
419 		   || ((reg & 0x00ff) == 0x55));
420 	w83627hf_set_bank(data, reg);
421 	outb_p(reg & 0xff, data->addr + W83781D_ADDR_REG_OFFSET);
422 	res = inb_p(data->addr + W83781D_DATA_REG_OFFSET);
423 	if (word_sized) {
424 		outb_p((reg & 0xff) + 1,
425 		       data->addr + W83781D_ADDR_REG_OFFSET);
426 		res =
427 		    (res << 8) + inb_p(data->addr +
428 				       W83781D_DATA_REG_OFFSET);
429 	}
430 	w83627hf_reset_bank(data, reg);
431 	mutex_unlock(&data->lock);
432 	return res;
433 }
434 
435 static int w83627hf_write_value(struct w83627hf_data *data, u16 reg, u16 value)
436 {
437 	int word_sized;
438 
439 	mutex_lock(&data->lock);
440 	word_sized = (((reg & 0xff00) == 0x100)
441 		   || ((reg & 0xff00) == 0x200))
442 		  && (((reg & 0x00ff) == 0x53)
443 		   || ((reg & 0x00ff) == 0x55));
444 	w83627hf_set_bank(data, reg);
445 	outb_p(reg & 0xff, data->addr + W83781D_ADDR_REG_OFFSET);
446 	if (word_sized) {
447 		outb_p(value >> 8,
448 		       data->addr + W83781D_DATA_REG_OFFSET);
449 		outb_p((reg & 0xff) + 1,
450 		       data->addr + W83781D_ADDR_REG_OFFSET);
451 	}
452 	outb_p(value & 0xff,
453 	       data->addr + W83781D_DATA_REG_OFFSET);
454 	w83627hf_reset_bank(data, reg);
455 	mutex_unlock(&data->lock);
456 	return 0;
457 }
458 
459 static void w83627hf_update_fan_div(struct w83627hf_data *data)
460 {
461 	int reg;
462 
463 	reg = w83627hf_read_value(data, W83781D_REG_VID_FANDIV);
464 	data->fan_div[0] = (reg >> 4) & 0x03;
465 	data->fan_div[1] = (reg >> 6) & 0x03;
466 	if (data->type != w83697hf) {
467 		data->fan_div[2] = (w83627hf_read_value(data,
468 				       W83781D_REG_PIN) >> 6) & 0x03;
469 	}
470 	reg = w83627hf_read_value(data, W83781D_REG_VBAT);
471 	data->fan_div[0] |= (reg >> 3) & 0x04;
472 	data->fan_div[1] |= (reg >> 4) & 0x04;
473 	if (data->type != w83697hf)
474 		data->fan_div[2] |= (reg >> 5) & 0x04;
475 }
476 
477 static struct w83627hf_data *w83627hf_update_device(struct device *dev)
478 {
479 	struct w83627hf_data *data = dev_get_drvdata(dev);
480 	int i, num_temps = (data->type == w83697hf) ? 2 : 3;
481 	int num_pwms = (data->type == w83697hf) ? 2 : 3;
482 
483 	mutex_lock(&data->update_lock);
484 
485 	if (time_after(jiffies, data->last_updated + HZ + HZ / 2)
486 	    || !data->valid) {
487 		for (i = 0; i <= 8; i++) {
488 			/* skip missing sensors */
489 			if (((data->type == w83697hf) && (i == 1)) ||
490 			    ((data->type != w83627hf && data->type != w83697hf)
491 			    && (i == 5 || i == 6)))
492 				continue;
493 			data->in[i] =
494 			    w83627hf_read_value(data, W83781D_REG_IN(i));
495 			data->in_min[i] =
496 			    w83627hf_read_value(data,
497 					       W83781D_REG_IN_MIN(i));
498 			data->in_max[i] =
499 			    w83627hf_read_value(data,
500 					       W83781D_REG_IN_MAX(i));
501 		}
502 		for (i = 0; i <= 2; i++) {
503 			data->fan[i] =
504 			    w83627hf_read_value(data, W83627HF_REG_FAN(i));
505 			data->fan_min[i] =
506 			    w83627hf_read_value(data,
507 					       W83627HF_REG_FAN_MIN(i));
508 		}
509 		for (i = 0; i <= 2; i++) {
510 			u8 tmp = w83627hf_read_value(data,
511 				W836X7HF_REG_PWM(data->type, i));
512 			/* bits 0-3 are reserved  in 627THF */
513 			if (data->type == w83627thf)
514 				tmp &= 0xf0;
515 			data->pwm[i] = tmp;
516 			if (i == 1 &&
517 			    (data->type == w83627hf || data->type == w83697hf))
518 				break;
519 		}
520 		if (data->type == w83627hf) {
521 				u8 tmp = w83627hf_read_value(data,
522 						W83627HF_REG_PWM_FREQ);
523 				data->pwm_freq[0] = tmp & 0x07;
524 				data->pwm_freq[1] = (tmp >> 4) & 0x07;
525 		} else if (data->type != w83627thf) {
526 			for (i = 1; i <= 3; i++) {
527 				data->pwm_freq[i - 1] =
528 					w83627hf_read_value(data,
529 						W83637HF_REG_PWM_FREQ[i - 1]);
530 				if (i == 2 && (data->type == w83697hf))
531 					break;
532 			}
533 		}
534 		if (data->type != w83627hf) {
535 			for (i = 0; i < num_pwms; i++) {
536 				u8 tmp = w83627hf_read_value(data,
537 					W83627THF_REG_PWM_ENABLE[i]);
538 				data->pwm_enable[i] =
539 					((tmp >> W83627THF_PWM_ENABLE_SHIFT[i])
540 					& 0x03) + 1;
541 			}
542 		}
543 		for (i = 0; i < num_temps; i++) {
544 			data->temp[i] = w83627hf_read_value(
545 						data, w83627hf_reg_temp[i]);
546 			data->temp_max[i] = w83627hf_read_value(
547 						data, w83627hf_reg_temp_over[i]);
548 			data->temp_max_hyst[i] = w83627hf_read_value(
549 						data, w83627hf_reg_temp_hyst[i]);
550 		}
551 
552 		w83627hf_update_fan_div(data);
553 
554 		data->alarms =
555 		    w83627hf_read_value(data, W83781D_REG_ALARM1) |
556 		    (w83627hf_read_value(data, W83781D_REG_ALARM2) << 8) |
557 		    (w83627hf_read_value(data, W83781D_REG_ALARM3) << 16);
558 		i = w83627hf_read_value(data, W83781D_REG_BEEP_INTS2);
559 		data->beep_mask = (i << 8) |
560 		    w83627hf_read_value(data, W83781D_REG_BEEP_INTS1) |
561 		    w83627hf_read_value(data, W83781D_REG_BEEP_INTS3) << 16;
562 		data->last_updated = jiffies;
563 		data->valid = true;
564 	}
565 
566 	mutex_unlock(&data->update_lock);
567 
568 	return data;
569 }
570 
571 #ifdef CONFIG_PM
572 static int w83627hf_suspend(struct device *dev)
573 {
574 	struct w83627hf_data *data = w83627hf_update_device(dev);
575 
576 	mutex_lock(&data->update_lock);
577 	data->scfg1 = w83627hf_read_value(data, W83781D_REG_SCFG1);
578 	data->scfg2 = w83627hf_read_value(data, W83781D_REG_SCFG2);
579 	mutex_unlock(&data->update_lock);
580 
581 	return 0;
582 }
583 
584 static int w83627hf_resume(struct device *dev)
585 {
586 	struct w83627hf_data *data = dev_get_drvdata(dev);
587 	int i, num_temps = (data->type == w83697hf) ? 2 : 3;
588 
589 	/* Restore limits */
590 	mutex_lock(&data->update_lock);
591 	for (i = 0; i <= 8; i++) {
592 		/* skip missing sensors */
593 		if (((data->type == w83697hf) && (i == 1)) ||
594 		    ((data->type != w83627hf && data->type != w83697hf)
595 		    && (i == 5 || i == 6)))
596 			continue;
597 		w83627hf_write_value(data, W83781D_REG_IN_MAX(i),
598 				     data->in_max[i]);
599 		w83627hf_write_value(data, W83781D_REG_IN_MIN(i),
600 				     data->in_min[i]);
601 	}
602 	for (i = 0; i <= 2; i++)
603 		w83627hf_write_value(data, W83627HF_REG_FAN_MIN(i),
604 				     data->fan_min[i]);
605 	for (i = 0; i < num_temps; i++) {
606 		w83627hf_write_value(data, w83627hf_reg_temp_over[i],
607 				     data->temp_max[i]);
608 		w83627hf_write_value(data, w83627hf_reg_temp_hyst[i],
609 				     data->temp_max_hyst[i]);
610 	}
611 
612 	/* Fixup BIOS bugs */
613 	if (data->type == w83627thf || data->type == w83637hf ||
614 	    data->type == w83687thf)
615 		w83627hf_write_value(data, W83627THF_REG_VRM_OVT_CFG,
616 				     data->vrm_ovt);
617 	w83627hf_write_value(data, W83781D_REG_SCFG1, data->scfg1);
618 	w83627hf_write_value(data, W83781D_REG_SCFG2, data->scfg2);
619 
620 	/* Force re-reading all values */
621 	data->valid = false;
622 	mutex_unlock(&data->update_lock);
623 
624 	return 0;
625 }
626 
627 static const struct dev_pm_ops w83627hf_dev_pm_ops = {
628 	.suspend = w83627hf_suspend,
629 	.resume = w83627hf_resume,
630 };
631 
632 #define W83627HF_DEV_PM_OPS	(&w83627hf_dev_pm_ops)
633 #else
634 #define W83627HF_DEV_PM_OPS	NULL
635 #endif /* CONFIG_PM */
636 
637 static int w83627thf_read_gpio5(struct platform_device *pdev)
638 {
639 	struct w83627hf_sio_data *sio_data = dev_get_platdata(&pdev->dev);
640 	int res = 0xff, sel;
641 
642 	if (superio_enter(sio_data)) {
643 		/*
644 		 * Some other driver reserved the address space for itself.
645 		 * We don't want to fail driver instantiation because of that,
646 		 * so display a warning and keep going.
647 		 */
648 		dev_warn(&pdev->dev,
649 			 "Can not read VID data: Failed to enable SuperIO access\n");
650 		return res;
651 	}
652 
653 	superio_select(sio_data, W83627HF_LD_GPIO5);
654 
655 	res = 0xff;
656 
657 	/* Make sure these GPIO pins are enabled */
658 	if (!(superio_inb(sio_data, W83627THF_GPIO5_EN) & (1<<3))) {
659 		dev_dbg(&pdev->dev, "GPIO5 disabled, no VID function\n");
660 		goto exit;
661 	}
662 
663 	/*
664 	 * Make sure the pins are configured for input
665 	 * There must be at least five (VRM 9), and possibly 6 (VRM 10)
666 	 */
667 	sel = superio_inb(sio_data, W83627THF_GPIO5_IOSR) & 0x3f;
668 	if ((sel & 0x1f) != 0x1f) {
669 		dev_dbg(&pdev->dev, "GPIO5 not configured for VID "
670 			"function\n");
671 		goto exit;
672 	}
673 
674 	dev_info(&pdev->dev, "Reading VID from GPIO5\n");
675 	res = superio_inb(sio_data, W83627THF_GPIO5_DR) & sel;
676 
677 exit:
678 	superio_exit(sio_data);
679 	return res;
680 }
681 
682 static int w83687thf_read_vid(struct platform_device *pdev)
683 {
684 	struct w83627hf_sio_data *sio_data = dev_get_platdata(&pdev->dev);
685 	int res = 0xff;
686 
687 	if (superio_enter(sio_data)) {
688 		/*
689 		 * Some other driver reserved the address space for itself.
690 		 * We don't want to fail driver instantiation because of that,
691 		 * so display a warning and keep going.
692 		 */
693 		dev_warn(&pdev->dev,
694 			 "Can not read VID data: Failed to enable SuperIO access\n");
695 		return res;
696 	}
697 
698 	superio_select(sio_data, W83627HF_LD_HWM);
699 
700 	/* Make sure these GPIO pins are enabled */
701 	if (!(superio_inb(sio_data, W83687THF_VID_EN) & (1 << 2))) {
702 		dev_dbg(&pdev->dev, "VID disabled, no VID function\n");
703 		goto exit;
704 	}
705 
706 	/* Make sure the pins are configured for input */
707 	if (!(superio_inb(sio_data, W83687THF_VID_CFG) & (1 << 4))) {
708 		dev_dbg(&pdev->dev, "VID configured as output, "
709 			"no VID function\n");
710 		goto exit;
711 	}
712 
713 	res = superio_inb(sio_data, W83687THF_VID_DATA) & 0x3f;
714 
715 exit:
716 	superio_exit(sio_data);
717 	return res;
718 }
719 
720 static void w83627hf_init_device(struct platform_device *pdev)
721 {
722 	struct w83627hf_data *data = platform_get_drvdata(pdev);
723 	int i;
724 	enum chips type = data->type;
725 	u8 tmp;
726 
727 	/* Minimize conflicts with other winbond i2c-only clients...  */
728 	/* disable i2c subclients... how to disable main i2c client?? */
729 	/* force i2c address to relatively uncommon address */
730 	if (type == w83627hf) {
731 		w83627hf_write_value(data, W83781D_REG_I2C_SUBADDR, 0x89);
732 		w83627hf_write_value(data, W83781D_REG_I2C_ADDR, force_i2c);
733 	}
734 
735 	/* Read VID only once */
736 	if (type == w83627hf || type == w83637hf) {
737 		int lo = w83627hf_read_value(data, W83781D_REG_VID_FANDIV);
738 		int hi = w83627hf_read_value(data, W83781D_REG_CHIPID);
739 		data->vid = (lo & 0x0f) | ((hi & 0x01) << 4);
740 	} else if (type == w83627thf) {
741 		data->vid = w83627thf_read_gpio5(pdev);
742 	} else if (type == w83687thf) {
743 		data->vid = w83687thf_read_vid(pdev);
744 	}
745 
746 	/* Read VRM & OVT Config only once */
747 	if (type == w83627thf || type == w83637hf || type == w83687thf) {
748 		data->vrm_ovt =
749 			w83627hf_read_value(data, W83627THF_REG_VRM_OVT_CFG);
750 	}
751 
752 	tmp = w83627hf_read_value(data, W83781D_REG_SCFG1);
753 	for (i = 1; i <= 3; i++) {
754 		if (!(tmp & BIT_SCFG1[i - 1])) {
755 			data->sens[i - 1] = 4;
756 		} else {
757 			if (w83627hf_read_value
758 			    (data,
759 			     W83781D_REG_SCFG2) & BIT_SCFG2[i - 1])
760 				data->sens[i - 1] = 1;
761 			else
762 				data->sens[i - 1] = 2;
763 		}
764 		if ((type == w83697hf) && (i == 2))
765 			break;
766 	}
767 
768 	if(init) {
769 		/* Enable temp2 */
770 		tmp = w83627hf_read_value(data, W83627HF_REG_TEMP2_CONFIG);
771 		if (tmp & 0x01) {
772 			dev_warn(&pdev->dev, "Enabling temp2, readings "
773 				 "might not make sense\n");
774 			w83627hf_write_value(data, W83627HF_REG_TEMP2_CONFIG,
775 				tmp & 0xfe);
776 		}
777 
778 		/* Enable temp3 */
779 		if (type != w83697hf) {
780 			tmp = w83627hf_read_value(data,
781 				W83627HF_REG_TEMP3_CONFIG);
782 			if (tmp & 0x01) {
783 				dev_warn(&pdev->dev, "Enabling temp3, "
784 					 "readings might not make sense\n");
785 				w83627hf_write_value(data,
786 					W83627HF_REG_TEMP3_CONFIG, tmp & 0xfe);
787 			}
788 		}
789 	}
790 
791 	/* Start monitoring */
792 	w83627hf_write_value(data, W83781D_REG_CONFIG,
793 			    (w83627hf_read_value(data,
794 						W83781D_REG_CONFIG) & 0xf7)
795 			    | 0x01);
796 
797 	/* Enable VBAT monitoring if needed */
798 	tmp = w83627hf_read_value(data, W83781D_REG_VBAT);
799 	if (!(tmp & 0x01))
800 		w83627hf_write_value(data, W83781D_REG_VBAT, tmp | 0x01);
801 }
802 
803 /* use a different set of functions for in0 */
804 static ssize_t show_in_0(struct w83627hf_data *data, char *buf, u8 reg)
805 {
806 	long in0;
807 
808 	if ((data->vrm_ovt & 0x01) &&
809 		(w83627thf == data->type || w83637hf == data->type
810 		 || w83687thf == data->type))
811 
812 		/* use VRM9 calculation */
813 		in0 = (long)((reg * 488 + 70000 + 50) / 100);
814 	else
815 		/* use VRM8 (standard) calculation */
816 		in0 = (long)IN_FROM_REG(reg);
817 
818 	return sprintf(buf,"%ld\n", in0);
819 }
820 
821 static ssize_t in0_input_show(struct device *dev,
822 			      struct device_attribute *attr, char *buf)
823 {
824 	struct w83627hf_data *data = w83627hf_update_device(dev);
825 	return show_in_0(data, buf, data->in[0]);
826 }
827 static DEVICE_ATTR_RO(in0_input);
828 
829 static ssize_t in0_min_show(struct device *dev, struct device_attribute *attr,
830 			    char *buf)
831 {
832 	struct w83627hf_data *data = w83627hf_update_device(dev);
833 	return show_in_0(data, buf, data->in_min[0]);
834 }
835 
836 static ssize_t in0_min_store(struct device *dev,
837 			     struct device_attribute *attr, const char *buf,
838 			     size_t count)
839 {
840 	struct w83627hf_data *data = dev_get_drvdata(dev);
841 	unsigned long val;
842 	int err;
843 
844 	err = kstrtoul(buf, 10, &val);
845 	if (err)
846 		return err;
847 
848 	mutex_lock(&data->update_lock);
849 
850 	if ((data->vrm_ovt & 0x01) &&
851 		(w83627thf == data->type || w83637hf == data->type
852 		 || w83687thf == data->type))
853 
854 		/* use VRM9 calculation */
855 		data->in_min[0] =
856 			clamp_val(((val * 100) - 70000 + 244) / 488, 0, 255);
857 	else
858 		/* use VRM8 (standard) calculation */
859 		data->in_min[0] = IN_TO_REG(val);
860 
861 	w83627hf_write_value(data, W83781D_REG_IN_MIN(0), data->in_min[0]);
862 	mutex_unlock(&data->update_lock);
863 	return count;
864 }
865 
866 static DEVICE_ATTR_RW(in0_min);
867 
868 static ssize_t in0_max_show(struct device *dev, struct device_attribute *attr,
869 			    char *buf)
870 {
871 	struct w83627hf_data *data = w83627hf_update_device(dev);
872 	return show_in_0(data, buf, data->in_max[0]);
873 }
874 
875 static ssize_t in0_max_store(struct device *dev,
876 			     struct device_attribute *attr, const char *buf,
877 			     size_t count)
878 {
879 	struct w83627hf_data *data = dev_get_drvdata(dev);
880 	unsigned long val;
881 	int err;
882 
883 	err = kstrtoul(buf, 10, &val);
884 	if (err)
885 		return err;
886 
887 	mutex_lock(&data->update_lock);
888 
889 	if ((data->vrm_ovt & 0x01) &&
890 		(w83627thf == data->type || w83637hf == data->type
891 		 || w83687thf == data->type))
892 
893 		/* use VRM9 calculation */
894 		data->in_max[0] =
895 			clamp_val(((val * 100) - 70000 + 244) / 488, 0, 255);
896 	else
897 		/* use VRM8 (standard) calculation */
898 		data->in_max[0] = IN_TO_REG(val);
899 
900 	w83627hf_write_value(data, W83781D_REG_IN_MAX(0), data->in_max[0]);
901 	mutex_unlock(&data->update_lock);
902 	return count;
903 }
904 
905 static DEVICE_ATTR_RW(in0_max);
906 
907 static ssize_t
908 alarm_show(struct device *dev, struct device_attribute *attr, char *buf)
909 {
910 	struct w83627hf_data *data = w83627hf_update_device(dev);
911 	int bitnr = to_sensor_dev_attr(attr)->index;
912 	return sprintf(buf, "%u\n", (data->alarms >> bitnr) & 1);
913 }
914 
915 static SENSOR_DEVICE_ATTR_RO(in0_alarm, alarm, 0);
916 static SENSOR_DEVICE_ATTR_RO(in1_alarm, alarm, 1);
917 static SENSOR_DEVICE_ATTR_RO(in2_alarm, alarm, 2);
918 static SENSOR_DEVICE_ATTR_RO(in3_alarm, alarm, 3);
919 static SENSOR_DEVICE_ATTR_RO(in4_alarm, alarm, 8);
920 static SENSOR_DEVICE_ATTR_RO(in5_alarm, alarm, 9);
921 static SENSOR_DEVICE_ATTR_RO(in6_alarm, alarm, 10);
922 static SENSOR_DEVICE_ATTR_RO(in7_alarm, alarm, 16);
923 static SENSOR_DEVICE_ATTR_RO(in8_alarm, alarm, 17);
924 static SENSOR_DEVICE_ATTR_RO(fan1_alarm, alarm, 6);
925 static SENSOR_DEVICE_ATTR_RO(fan2_alarm, alarm, 7);
926 static SENSOR_DEVICE_ATTR_RO(fan3_alarm, alarm, 11);
927 static SENSOR_DEVICE_ATTR_RO(temp1_alarm, alarm, 4);
928 static SENSOR_DEVICE_ATTR_RO(temp2_alarm, alarm, 5);
929 static SENSOR_DEVICE_ATTR_RO(temp3_alarm, alarm, 13);
930 
931 static ssize_t
932 beep_show(struct device *dev, struct device_attribute *attr, char *buf)
933 {
934 	struct w83627hf_data *data = w83627hf_update_device(dev);
935 	int bitnr = to_sensor_dev_attr(attr)->index;
936 	return sprintf(buf, "%u\n", (data->beep_mask >> bitnr) & 1);
937 }
938 
939 static ssize_t
940 beep_store(struct device *dev, struct device_attribute *attr, const char *buf,
941 	   size_t count)
942 {
943 	struct w83627hf_data *data = dev_get_drvdata(dev);
944 	int bitnr = to_sensor_dev_attr(attr)->index;
945 	u8 reg;
946 	unsigned long bit;
947 	int err;
948 
949 	err = kstrtoul(buf, 10, &bit);
950 	if (err)
951 		return err;
952 
953 	if (bit & ~1)
954 		return -EINVAL;
955 
956 	mutex_lock(&data->update_lock);
957 	if (bit)
958 		data->beep_mask |= (1 << bitnr);
959 	else
960 		data->beep_mask &= ~(1 << bitnr);
961 
962 	if (bitnr < 8) {
963 		reg = w83627hf_read_value(data, W83781D_REG_BEEP_INTS1);
964 		if (bit)
965 			reg |= (1 << bitnr);
966 		else
967 			reg &= ~(1 << bitnr);
968 		w83627hf_write_value(data, W83781D_REG_BEEP_INTS1, reg);
969 	} else if (bitnr < 16) {
970 		reg = w83627hf_read_value(data, W83781D_REG_BEEP_INTS2);
971 		if (bit)
972 			reg |= (1 << (bitnr - 8));
973 		else
974 			reg &= ~(1 << (bitnr - 8));
975 		w83627hf_write_value(data, W83781D_REG_BEEP_INTS2, reg);
976 	} else {
977 		reg = w83627hf_read_value(data, W83781D_REG_BEEP_INTS3);
978 		if (bit)
979 			reg |= (1 << (bitnr - 16));
980 		else
981 			reg &= ~(1 << (bitnr - 16));
982 		w83627hf_write_value(data, W83781D_REG_BEEP_INTS3, reg);
983 	}
984 	mutex_unlock(&data->update_lock);
985 
986 	return count;
987 }
988 
989 static SENSOR_DEVICE_ATTR_RW(in0_beep, beep, 0);
990 static SENSOR_DEVICE_ATTR_RW(in1_beep, beep, 1);
991 static SENSOR_DEVICE_ATTR_RW(in2_beep, beep, 2);
992 static SENSOR_DEVICE_ATTR_RW(in3_beep, beep, 3);
993 static SENSOR_DEVICE_ATTR_RW(in4_beep, beep, 8);
994 static SENSOR_DEVICE_ATTR_RW(in5_beep, beep, 9);
995 static SENSOR_DEVICE_ATTR_RW(in6_beep, beep, 10);
996 static SENSOR_DEVICE_ATTR_RW(in7_beep, beep, 16);
997 static SENSOR_DEVICE_ATTR_RW(in8_beep, beep, 17);
998 static SENSOR_DEVICE_ATTR_RW(fan1_beep, beep, 6);
999 static SENSOR_DEVICE_ATTR_RW(fan2_beep, beep, 7);
1000 static SENSOR_DEVICE_ATTR_RW(fan3_beep, beep, 11);
1001 static SENSOR_DEVICE_ATTR_RW(temp1_beep, beep, 4);
1002 static SENSOR_DEVICE_ATTR_RW(temp2_beep, beep, 5);
1003 static SENSOR_DEVICE_ATTR_RW(temp3_beep, beep, 13);
1004 static SENSOR_DEVICE_ATTR_RW(beep_enable, beep, 15);
1005 
1006 static ssize_t
1007 in_input_show(struct device *dev, struct device_attribute *devattr, char *buf)
1008 {
1009 	int nr = to_sensor_dev_attr(devattr)->index;
1010 	struct w83627hf_data *data = w83627hf_update_device(dev);
1011 	return sprintf(buf, "%ld\n", (long)IN_FROM_REG(data->in[nr]));
1012 }
1013 
1014 static ssize_t
1015 in_min_show(struct device *dev, struct device_attribute *devattr, char *buf)
1016 {
1017 	int nr = to_sensor_dev_attr(devattr)->index;
1018 	struct w83627hf_data *data = w83627hf_update_device(dev);
1019 	return sprintf(buf, "%ld\n", (long)IN_FROM_REG(data->in_min[nr]));
1020 }
1021 
1022 static ssize_t
1023 in_min_store(struct device *dev, struct device_attribute *devattr,
1024 	     const char *buf, size_t count)
1025 {
1026 	int nr = to_sensor_dev_attr(devattr)->index;
1027 	struct w83627hf_data *data = dev_get_drvdata(dev);
1028 	long val;
1029 	int err;
1030 
1031 	err = kstrtol(buf, 10, &val);
1032 	if (err)
1033 		return err;
1034 
1035 	mutex_lock(&data->update_lock);
1036 	data->in_min[nr] = IN_TO_REG(val);
1037 	w83627hf_write_value(data, W83781D_REG_IN_MIN(nr), data->in_min[nr]);
1038 	mutex_unlock(&data->update_lock);
1039 	return count;
1040 }
1041 
1042 static ssize_t
1043 in_max_show(struct device *dev, struct device_attribute *devattr, char *buf)
1044 {
1045 	int nr = to_sensor_dev_attr(devattr)->index;
1046 	struct w83627hf_data *data = w83627hf_update_device(dev);
1047 	return sprintf(buf, "%ld\n", (long)IN_FROM_REG(data->in_max[nr]));
1048 }
1049 
1050 static ssize_t
1051 in_max_store(struct device *dev, struct device_attribute *devattr,
1052 	     const char *buf, size_t count)
1053 {
1054 	int nr = to_sensor_dev_attr(devattr)->index;
1055 	struct w83627hf_data *data = dev_get_drvdata(dev);
1056 	long val;
1057 	int err;
1058 
1059 	err = kstrtol(buf, 10, &val);
1060 	if (err)
1061 		return err;
1062 
1063 	mutex_lock(&data->update_lock);
1064 	data->in_max[nr] = IN_TO_REG(val);
1065 	w83627hf_write_value(data, W83781D_REG_IN_MAX(nr), data->in_max[nr]);
1066 	mutex_unlock(&data->update_lock);
1067 	return count;
1068 }
1069 
1070 static SENSOR_DEVICE_ATTR_RO(in1_input, in_input, 1);
1071 static SENSOR_DEVICE_ATTR_RW(in1_min, in_min, 1);
1072 static SENSOR_DEVICE_ATTR_RW(in1_max, in_max, 1);
1073 static SENSOR_DEVICE_ATTR_RO(in2_input, in_input, 2);
1074 static SENSOR_DEVICE_ATTR_RW(in2_min, in_min, 2);
1075 static SENSOR_DEVICE_ATTR_RW(in2_max, in_max, 2);
1076 static SENSOR_DEVICE_ATTR_RO(in3_input, in_input, 3);
1077 static SENSOR_DEVICE_ATTR_RW(in3_min, in_min, 3);
1078 static SENSOR_DEVICE_ATTR_RW(in3_max, in_max, 3);
1079 static SENSOR_DEVICE_ATTR_RO(in4_input, in_input, 4);
1080 static SENSOR_DEVICE_ATTR_RW(in4_min, in_min, 4);
1081 static SENSOR_DEVICE_ATTR_RW(in4_max, in_max, 4);
1082 static SENSOR_DEVICE_ATTR_RO(in5_input, in_input, 5);
1083 static SENSOR_DEVICE_ATTR_RW(in5_min, in_min, 5);
1084 static SENSOR_DEVICE_ATTR_RW(in5_max, in_max, 5);
1085 static SENSOR_DEVICE_ATTR_RO(in6_input, in_input, 6);
1086 static SENSOR_DEVICE_ATTR_RW(in6_min, in_min, 6);
1087 static SENSOR_DEVICE_ATTR_RW(in6_max, in_max, 6);
1088 static SENSOR_DEVICE_ATTR_RO(in7_input, in_input, 7);
1089 static SENSOR_DEVICE_ATTR_RW(in7_min, in_min, 7);
1090 static SENSOR_DEVICE_ATTR_RW(in7_max, in_max, 7);
1091 static SENSOR_DEVICE_ATTR_RO(in8_input, in_input, 8);
1092 static SENSOR_DEVICE_ATTR_RW(in8_min, in_min, 8);
1093 static SENSOR_DEVICE_ATTR_RW(in8_max, in_max, 8);
1094 
1095 static ssize_t
1096 fan_input_show(struct device *dev, struct device_attribute *devattr,
1097 	       char *buf)
1098 {
1099 	int nr = to_sensor_dev_attr(devattr)->index;
1100 	struct w83627hf_data *data = w83627hf_update_device(dev);
1101 	return sprintf(buf, "%ld\n", FAN_FROM_REG(data->fan[nr],
1102 				(long)DIV_FROM_REG(data->fan_div[nr])));
1103 }
1104 
1105 static ssize_t
1106 fan_min_show(struct device *dev, struct device_attribute *devattr, char *buf)
1107 {
1108 	int nr = to_sensor_dev_attr(devattr)->index;
1109 	struct w83627hf_data *data = w83627hf_update_device(dev);
1110 	return sprintf(buf, "%ld\n", FAN_FROM_REG(data->fan_min[nr],
1111 				(long)DIV_FROM_REG(data->fan_div[nr])));
1112 }
1113 
1114 static ssize_t
1115 fan_min_store(struct device *dev, struct device_attribute *devattr,
1116 	      const char *buf, size_t count)
1117 {
1118 	int nr = to_sensor_dev_attr(devattr)->index;
1119 	struct w83627hf_data *data = dev_get_drvdata(dev);
1120 	unsigned long val;
1121 	int err;
1122 
1123 	err = kstrtoul(buf, 10, &val);
1124 	if (err)
1125 		return err;
1126 
1127 	mutex_lock(&data->update_lock);
1128 	data->fan_min[nr] = FAN_TO_REG(val, DIV_FROM_REG(data->fan_div[nr]));
1129 	w83627hf_write_value(data, W83627HF_REG_FAN_MIN(nr),
1130 			     data->fan_min[nr]);
1131 
1132 	mutex_unlock(&data->update_lock);
1133 	return count;
1134 }
1135 
1136 static SENSOR_DEVICE_ATTR_RO(fan1_input, fan_input, 0);
1137 static SENSOR_DEVICE_ATTR_RW(fan1_min, fan_min, 0);
1138 static SENSOR_DEVICE_ATTR_RO(fan2_input, fan_input, 1);
1139 static SENSOR_DEVICE_ATTR_RW(fan2_min, fan_min, 1);
1140 static SENSOR_DEVICE_ATTR_RO(fan3_input, fan_input, 2);
1141 static SENSOR_DEVICE_ATTR_RW(fan3_min, fan_min, 2);
1142 
1143 static ssize_t
1144 fan_div_show(struct device *dev, struct device_attribute *devattr, char *buf)
1145 {
1146 	int nr = to_sensor_dev_attr(devattr)->index;
1147 	struct w83627hf_data *data = w83627hf_update_device(dev);
1148 	return sprintf(buf, "%ld\n",
1149 		       (long) DIV_FROM_REG(data->fan_div[nr]));
1150 }
1151 
1152 /*
1153  * Note: we save and restore the fan minimum here, because its value is
1154  * determined in part by the fan divisor.  This follows the principle of
1155  * least surprise; the user doesn't expect the fan minimum to change just
1156  * because the divisor changed.
1157  */
1158 static ssize_t
1159 fan_div_store(struct device *dev, struct device_attribute *devattr,
1160 	      const char *buf, size_t count)
1161 {
1162 	int nr = to_sensor_dev_attr(devattr)->index;
1163 	struct w83627hf_data *data = dev_get_drvdata(dev);
1164 	unsigned long min;
1165 	u8 reg;
1166 	unsigned long val;
1167 	int err;
1168 
1169 	err = kstrtoul(buf, 10, &val);
1170 	if (err)
1171 		return err;
1172 
1173 	mutex_lock(&data->update_lock);
1174 
1175 	/* Save fan_min */
1176 	min = FAN_FROM_REG(data->fan_min[nr],
1177 			   DIV_FROM_REG(data->fan_div[nr]));
1178 
1179 	data->fan_div[nr] = DIV_TO_REG(val);
1180 
1181 	reg = (w83627hf_read_value(data, nr==2 ? W83781D_REG_PIN : W83781D_REG_VID_FANDIV)
1182 	       & (nr==0 ? 0xcf : 0x3f))
1183 	    | ((data->fan_div[nr] & 0x03) << (nr==0 ? 4 : 6));
1184 	w83627hf_write_value(data, nr==2 ? W83781D_REG_PIN : W83781D_REG_VID_FANDIV, reg);
1185 
1186 	reg = (w83627hf_read_value(data, W83781D_REG_VBAT)
1187 	       & ~(1 << (5 + nr)))
1188 	    | ((data->fan_div[nr] & 0x04) << (3 + nr));
1189 	w83627hf_write_value(data, W83781D_REG_VBAT, reg);
1190 
1191 	/* Restore fan_min */
1192 	data->fan_min[nr] = FAN_TO_REG(min, DIV_FROM_REG(data->fan_div[nr]));
1193 	w83627hf_write_value(data, W83627HF_REG_FAN_MIN(nr), data->fan_min[nr]);
1194 
1195 	mutex_unlock(&data->update_lock);
1196 	return count;
1197 }
1198 
1199 static SENSOR_DEVICE_ATTR_RW(fan1_div, fan_div, 0);
1200 static SENSOR_DEVICE_ATTR_RW(fan2_div, fan_div, 1);
1201 static SENSOR_DEVICE_ATTR_RW(fan3_div, fan_div, 2);
1202 
1203 static ssize_t
1204 temp_show(struct device *dev, struct device_attribute *devattr, char *buf)
1205 {
1206 	int nr = to_sensor_dev_attr(devattr)->index;
1207 	struct w83627hf_data *data = w83627hf_update_device(dev);
1208 
1209 	u16 tmp = data->temp[nr];
1210 	return sprintf(buf, "%ld\n", (nr) ? (long) LM75_TEMP_FROM_REG(tmp)
1211 					  : (long) TEMP_FROM_REG(tmp));
1212 }
1213 
1214 static ssize_t
1215 temp_max_show(struct device *dev, struct device_attribute *devattr, char *buf)
1216 {
1217 	int nr = to_sensor_dev_attr(devattr)->index;
1218 	struct w83627hf_data *data = w83627hf_update_device(dev);
1219 
1220 	u16 tmp = data->temp_max[nr];
1221 	return sprintf(buf, "%ld\n", (nr) ? (long) LM75_TEMP_FROM_REG(tmp)
1222 					  : (long) TEMP_FROM_REG(tmp));
1223 }
1224 
1225 static ssize_t
1226 temp_max_store(struct device *dev, struct device_attribute *devattr,
1227 	       const char *buf, size_t count)
1228 {
1229 	int nr = to_sensor_dev_attr(devattr)->index;
1230 	struct w83627hf_data *data = dev_get_drvdata(dev);
1231 	u16 tmp;
1232 	long val;
1233 	int err;
1234 
1235 	err = kstrtol(buf, 10, &val);
1236 	if (err)
1237 		return err;
1238 
1239 	tmp = (nr) ? LM75_TEMP_TO_REG(val) : TEMP_TO_REG(val);
1240 	mutex_lock(&data->update_lock);
1241 	data->temp_max[nr] = tmp;
1242 	w83627hf_write_value(data, w83627hf_reg_temp_over[nr], tmp);
1243 	mutex_unlock(&data->update_lock);
1244 	return count;
1245 }
1246 
1247 static ssize_t
1248 temp_max_hyst_show(struct device *dev, struct device_attribute *devattr,
1249 		   char *buf)
1250 {
1251 	int nr = to_sensor_dev_attr(devattr)->index;
1252 	struct w83627hf_data *data = w83627hf_update_device(dev);
1253 
1254 	u16 tmp = data->temp_max_hyst[nr];
1255 	return sprintf(buf, "%ld\n", (nr) ? (long) LM75_TEMP_FROM_REG(tmp)
1256 					  : (long) TEMP_FROM_REG(tmp));
1257 }
1258 
1259 static ssize_t
1260 temp_max_hyst_store(struct device *dev, struct device_attribute *devattr,
1261 		    const char *buf, size_t count)
1262 {
1263 	int nr = to_sensor_dev_attr(devattr)->index;
1264 	struct w83627hf_data *data = dev_get_drvdata(dev);
1265 	u16 tmp;
1266 	long val;
1267 	int err;
1268 
1269 	err = kstrtol(buf, 10, &val);
1270 	if (err)
1271 		return err;
1272 
1273 	tmp = (nr) ? LM75_TEMP_TO_REG(val) : TEMP_TO_REG(val);
1274 	mutex_lock(&data->update_lock);
1275 	data->temp_max_hyst[nr] = tmp;
1276 	w83627hf_write_value(data, w83627hf_reg_temp_hyst[nr], tmp);
1277 	mutex_unlock(&data->update_lock);
1278 	return count;
1279 }
1280 
1281 static SENSOR_DEVICE_ATTR_RO(temp1_input, temp, 0);
1282 static SENSOR_DEVICE_ATTR_RW(temp1_max, temp_max, 0);
1283 static SENSOR_DEVICE_ATTR_RW(temp1_max_hyst, temp_max_hyst, 0);
1284 static SENSOR_DEVICE_ATTR_RO(temp2_input, temp, 1);
1285 static SENSOR_DEVICE_ATTR_RW(temp2_max, temp_max, 1);
1286 static SENSOR_DEVICE_ATTR_RW(temp2_max_hyst, temp_max_hyst, 1);
1287 static SENSOR_DEVICE_ATTR_RO(temp3_input, temp, 2);
1288 static SENSOR_DEVICE_ATTR_RW(temp3_max, temp_max, 2);
1289 static SENSOR_DEVICE_ATTR_RW(temp3_max_hyst, temp_max_hyst, 2);
1290 
1291 static ssize_t
1292 temp_type_show(struct device *dev, struct device_attribute *devattr,
1293 	       char *buf)
1294 {
1295 	int nr = to_sensor_dev_attr(devattr)->index;
1296 	struct w83627hf_data *data = w83627hf_update_device(dev);
1297 	return sprintf(buf, "%ld\n", (long) data->sens[nr]);
1298 }
1299 
1300 static ssize_t
1301 temp_type_store(struct device *dev, struct device_attribute *devattr,
1302 		const char *buf, size_t count)
1303 {
1304 	int nr = to_sensor_dev_attr(devattr)->index;
1305 	struct w83627hf_data *data = dev_get_drvdata(dev);
1306 	unsigned long val;
1307 	u32 tmp;
1308 	int err;
1309 
1310 	err = kstrtoul(buf, 10, &val);
1311 	if (err)
1312 		return err;
1313 
1314 	mutex_lock(&data->update_lock);
1315 
1316 	switch (val) {
1317 	case 1:		/* PII/Celeron diode */
1318 		tmp = w83627hf_read_value(data, W83781D_REG_SCFG1);
1319 		w83627hf_write_value(data, W83781D_REG_SCFG1,
1320 				    tmp | BIT_SCFG1[nr]);
1321 		tmp = w83627hf_read_value(data, W83781D_REG_SCFG2);
1322 		w83627hf_write_value(data, W83781D_REG_SCFG2,
1323 				    tmp | BIT_SCFG2[nr]);
1324 		data->sens[nr] = val;
1325 		break;
1326 	case 2:		/* 3904 */
1327 		tmp = w83627hf_read_value(data, W83781D_REG_SCFG1);
1328 		w83627hf_write_value(data, W83781D_REG_SCFG1,
1329 				    tmp | BIT_SCFG1[nr]);
1330 		tmp = w83627hf_read_value(data, W83781D_REG_SCFG2);
1331 		w83627hf_write_value(data, W83781D_REG_SCFG2,
1332 				    tmp & ~BIT_SCFG2[nr]);
1333 		data->sens[nr] = val;
1334 		break;
1335 	case W83781D_DEFAULT_BETA:
1336 		dev_warn(dev, "Sensor type %d is deprecated, please use 4 "
1337 			 "instead\n", W83781D_DEFAULT_BETA);
1338 		fallthrough;
1339 	case 4:		/* thermistor */
1340 		tmp = w83627hf_read_value(data, W83781D_REG_SCFG1);
1341 		w83627hf_write_value(data, W83781D_REG_SCFG1,
1342 				    tmp & ~BIT_SCFG1[nr]);
1343 		data->sens[nr] = val;
1344 		break;
1345 	default:
1346 		dev_err(dev,
1347 		       "Invalid sensor type %ld; must be 1, 2, or 4\n",
1348 		       (long) val);
1349 		break;
1350 	}
1351 
1352 	mutex_unlock(&data->update_lock);
1353 	return count;
1354 }
1355 
1356 static SENSOR_DEVICE_ATTR_RW(temp1_type, temp_type, 0);
1357 static SENSOR_DEVICE_ATTR_RW(temp2_type, temp_type, 1);
1358 static SENSOR_DEVICE_ATTR_RW(temp3_type, temp_type, 2);
1359 
1360 static ssize_t
1361 alarms_show(struct device *dev, struct device_attribute *attr, char *buf)
1362 {
1363 	struct w83627hf_data *data = w83627hf_update_device(dev);
1364 	return sprintf(buf, "%ld\n", (long) data->alarms);
1365 }
1366 static DEVICE_ATTR_RO(alarms);
1367 
1368 #define VIN_UNIT_ATTRS(_X_)	\
1369 	&sensor_dev_attr_in##_X_##_input.dev_attr.attr,		\
1370 	&sensor_dev_attr_in##_X_##_min.dev_attr.attr,		\
1371 	&sensor_dev_attr_in##_X_##_max.dev_attr.attr,		\
1372 	&sensor_dev_attr_in##_X_##_alarm.dev_attr.attr,		\
1373 	&sensor_dev_attr_in##_X_##_beep.dev_attr.attr
1374 
1375 #define FAN_UNIT_ATTRS(_X_)	\
1376 	&sensor_dev_attr_fan##_X_##_input.dev_attr.attr,	\
1377 	&sensor_dev_attr_fan##_X_##_min.dev_attr.attr,		\
1378 	&sensor_dev_attr_fan##_X_##_div.dev_attr.attr,		\
1379 	&sensor_dev_attr_fan##_X_##_alarm.dev_attr.attr,	\
1380 	&sensor_dev_attr_fan##_X_##_beep.dev_attr.attr
1381 
1382 #define TEMP_UNIT_ATTRS(_X_)	\
1383 	&sensor_dev_attr_temp##_X_##_input.dev_attr.attr,	\
1384 	&sensor_dev_attr_temp##_X_##_max.dev_attr.attr,		\
1385 	&sensor_dev_attr_temp##_X_##_max_hyst.dev_attr.attr,	\
1386 	&sensor_dev_attr_temp##_X_##_type.dev_attr.attr,	\
1387 	&sensor_dev_attr_temp##_X_##_alarm.dev_attr.attr,	\
1388 	&sensor_dev_attr_temp##_X_##_beep.dev_attr.attr
1389 
1390 static ssize_t
1391 beep_mask_show(struct device *dev, struct device_attribute *attr, char *buf)
1392 {
1393 	struct w83627hf_data *data = w83627hf_update_device(dev);
1394 	return sprintf(buf, "%ld\n",
1395 		      (long)BEEP_MASK_FROM_REG(data->beep_mask));
1396 }
1397 
1398 static ssize_t
1399 beep_mask_store(struct device *dev, struct device_attribute *attr,
1400 		const char *buf, size_t count)
1401 {
1402 	struct w83627hf_data *data = dev_get_drvdata(dev);
1403 	unsigned long val;
1404 	int err;
1405 
1406 	err = kstrtoul(buf, 10, &val);
1407 	if (err)
1408 		return err;
1409 
1410 	mutex_lock(&data->update_lock);
1411 
1412 	/* preserve beep enable */
1413 	data->beep_mask = (data->beep_mask & 0x8000)
1414 			| BEEP_MASK_TO_REG(val);
1415 	w83627hf_write_value(data, W83781D_REG_BEEP_INTS1,
1416 			    data->beep_mask & 0xff);
1417 	w83627hf_write_value(data, W83781D_REG_BEEP_INTS3,
1418 			    ((data->beep_mask) >> 16) & 0xff);
1419 	w83627hf_write_value(data, W83781D_REG_BEEP_INTS2,
1420 			    (data->beep_mask >> 8) & 0xff);
1421 
1422 	mutex_unlock(&data->update_lock);
1423 	return count;
1424 }
1425 
1426 static DEVICE_ATTR_RW(beep_mask);
1427 
1428 static ssize_t
1429 pwm_show(struct device *dev, struct device_attribute *devattr, char *buf)
1430 {
1431 	int nr = to_sensor_dev_attr(devattr)->index;
1432 	struct w83627hf_data *data = w83627hf_update_device(dev);
1433 	return sprintf(buf, "%ld\n", (long) data->pwm[nr]);
1434 }
1435 
1436 static ssize_t
1437 pwm_store(struct device *dev, struct device_attribute *devattr,
1438 	  const char *buf, size_t count)
1439 {
1440 	int nr = to_sensor_dev_attr(devattr)->index;
1441 	struct w83627hf_data *data = dev_get_drvdata(dev);
1442 	unsigned long val;
1443 	int err;
1444 
1445 	err = kstrtoul(buf, 10, &val);
1446 	if (err)
1447 		return err;
1448 
1449 	mutex_lock(&data->update_lock);
1450 
1451 	if (data->type == w83627thf) {
1452 		/* bits 0-3 are reserved  in 627THF */
1453 		data->pwm[nr] = PWM_TO_REG(val) & 0xf0;
1454 		w83627hf_write_value(data,
1455 				     W836X7HF_REG_PWM(data->type, nr),
1456 				     data->pwm[nr] |
1457 				     (w83627hf_read_value(data,
1458 				     W836X7HF_REG_PWM(data->type, nr)) & 0x0f));
1459 	} else {
1460 		data->pwm[nr] = PWM_TO_REG(val);
1461 		w83627hf_write_value(data,
1462 				     W836X7HF_REG_PWM(data->type, nr),
1463 				     data->pwm[nr]);
1464 	}
1465 
1466 	mutex_unlock(&data->update_lock);
1467 	return count;
1468 }
1469 
1470 static SENSOR_DEVICE_ATTR_RW(pwm1, pwm, 0);
1471 static SENSOR_DEVICE_ATTR_RW(pwm2, pwm, 1);
1472 static SENSOR_DEVICE_ATTR_RW(pwm3, pwm, 2);
1473 
1474 static ssize_t
1475 name_show(struct device *dev, struct device_attribute *devattr, char *buf)
1476 {
1477 	struct w83627hf_data *data = dev_get_drvdata(dev);
1478 
1479 	return sprintf(buf, "%s\n", data->name);
1480 }
1481 
1482 static DEVICE_ATTR_RO(name);
1483 
1484 static struct attribute *w83627hf_attributes[] = {
1485 	&dev_attr_in0_input.attr,
1486 	&dev_attr_in0_min.attr,
1487 	&dev_attr_in0_max.attr,
1488 	&sensor_dev_attr_in0_alarm.dev_attr.attr,
1489 	&sensor_dev_attr_in0_beep.dev_attr.attr,
1490 	VIN_UNIT_ATTRS(2),
1491 	VIN_UNIT_ATTRS(3),
1492 	VIN_UNIT_ATTRS(4),
1493 	VIN_UNIT_ATTRS(7),
1494 	VIN_UNIT_ATTRS(8),
1495 
1496 	FAN_UNIT_ATTRS(1),
1497 	FAN_UNIT_ATTRS(2),
1498 
1499 	TEMP_UNIT_ATTRS(1),
1500 	TEMP_UNIT_ATTRS(2),
1501 
1502 	&dev_attr_alarms.attr,
1503 	&sensor_dev_attr_beep_enable.dev_attr.attr,
1504 	&dev_attr_beep_mask.attr,
1505 
1506 	&sensor_dev_attr_pwm1.dev_attr.attr,
1507 	&sensor_dev_attr_pwm2.dev_attr.attr,
1508 	&dev_attr_name.attr,
1509 	NULL
1510 };
1511 
1512 static const struct attribute_group w83627hf_group = {
1513 	.attrs = w83627hf_attributes,
1514 };
1515 
1516 static ssize_t
1517 pwm_freq_show(struct device *dev, struct device_attribute *devattr, char *buf)
1518 {
1519 	int nr = to_sensor_dev_attr(devattr)->index;
1520 	struct w83627hf_data *data = w83627hf_update_device(dev);
1521 	if (data->type == w83627hf)
1522 		return sprintf(buf, "%ld\n",
1523 			pwm_freq_from_reg_627hf(data->pwm_freq[nr]));
1524 	else
1525 		return sprintf(buf, "%ld\n",
1526 			pwm_freq_from_reg(data->pwm_freq[nr]));
1527 }
1528 
1529 static ssize_t
1530 pwm_freq_store(struct device *dev, struct device_attribute *devattr,
1531 	       const char *buf, size_t count)
1532 {
1533 	int nr = to_sensor_dev_attr(devattr)->index;
1534 	struct w83627hf_data *data = dev_get_drvdata(dev);
1535 	static const u8 mask[]={0xF8, 0x8F};
1536 	unsigned long val;
1537 	int err;
1538 
1539 	err = kstrtoul(buf, 10, &val);
1540 	if (err)
1541 		return err;
1542 
1543 	mutex_lock(&data->update_lock);
1544 
1545 	if (data->type == w83627hf) {
1546 		data->pwm_freq[nr] = pwm_freq_to_reg_627hf(val);
1547 		w83627hf_write_value(data, W83627HF_REG_PWM_FREQ,
1548 				(data->pwm_freq[nr] << (nr*4)) |
1549 				(w83627hf_read_value(data,
1550 				W83627HF_REG_PWM_FREQ) & mask[nr]));
1551 	} else {
1552 		data->pwm_freq[nr] = pwm_freq_to_reg(val);
1553 		w83627hf_write_value(data, W83637HF_REG_PWM_FREQ[nr],
1554 				data->pwm_freq[nr]);
1555 	}
1556 
1557 	mutex_unlock(&data->update_lock);
1558 	return count;
1559 }
1560 
1561 static SENSOR_DEVICE_ATTR_RW(pwm1_freq, pwm_freq, 0);
1562 static SENSOR_DEVICE_ATTR_RW(pwm2_freq, pwm_freq, 1);
1563 static SENSOR_DEVICE_ATTR_RW(pwm3_freq, pwm_freq, 2);
1564 
1565 static ssize_t
1566 cpu0_vid_show(struct device *dev, struct device_attribute *attr, char *buf)
1567 {
1568 	struct w83627hf_data *data = w83627hf_update_device(dev);
1569 	return sprintf(buf, "%ld\n", (long) vid_from_reg(data->vid, data->vrm));
1570 }
1571 
1572 static DEVICE_ATTR_RO(cpu0_vid);
1573 
1574 static ssize_t
1575 vrm_show(struct device *dev, struct device_attribute *attr, char *buf)
1576 {
1577 	struct w83627hf_data *data = dev_get_drvdata(dev);
1578 	return sprintf(buf, "%ld\n", (long) data->vrm);
1579 }
1580 
1581 static ssize_t
1582 vrm_store(struct device *dev, struct device_attribute *attr, const char *buf,
1583 	  size_t count)
1584 {
1585 	struct w83627hf_data *data = dev_get_drvdata(dev);
1586 	unsigned long val;
1587 	int err;
1588 
1589 	err = kstrtoul(buf, 10, &val);
1590 	if (err)
1591 		return err;
1592 
1593 	if (val > 255)
1594 		return -EINVAL;
1595 	data->vrm = val;
1596 
1597 	return count;
1598 }
1599 
1600 static DEVICE_ATTR_RW(vrm);
1601 
1602 static ssize_t
1603 pwm_enable_show(struct device *dev, struct device_attribute *devattr,
1604 		char *buf)
1605 {
1606 	int nr = to_sensor_dev_attr(devattr)->index;
1607 	struct w83627hf_data *data = w83627hf_update_device(dev);
1608 	return sprintf(buf, "%d\n", data->pwm_enable[nr]);
1609 }
1610 
1611 static ssize_t
1612 pwm_enable_store(struct device *dev, struct device_attribute *devattr,
1613 		 const char *buf, size_t count)
1614 {
1615 	int nr = to_sensor_dev_attr(devattr)->index;
1616 	struct w83627hf_data *data = dev_get_drvdata(dev);
1617 	u8 reg;
1618 	unsigned long val;
1619 	int err;
1620 
1621 	err = kstrtoul(buf, 10, &val);
1622 	if (err)
1623 		return err;
1624 
1625 	if (!val || val > 3)	/* modes 1, 2 and 3 are supported */
1626 		return -EINVAL;
1627 	mutex_lock(&data->update_lock);
1628 	data->pwm_enable[nr] = val;
1629 	reg = w83627hf_read_value(data, W83627THF_REG_PWM_ENABLE[nr]);
1630 	reg &= ~(0x03 << W83627THF_PWM_ENABLE_SHIFT[nr]);
1631 	reg |= (val - 1) << W83627THF_PWM_ENABLE_SHIFT[nr];
1632 	w83627hf_write_value(data, W83627THF_REG_PWM_ENABLE[nr], reg);
1633 	mutex_unlock(&data->update_lock);
1634 	return count;
1635 }
1636 
1637 static SENSOR_DEVICE_ATTR_RW(pwm1_enable, pwm_enable, 0);
1638 static SENSOR_DEVICE_ATTR_RW(pwm2_enable, pwm_enable, 1);
1639 static SENSOR_DEVICE_ATTR_RW(pwm3_enable, pwm_enable, 2);
1640 
1641 static struct attribute *w83627hf_attributes_opt[] = {
1642 	VIN_UNIT_ATTRS(1),
1643 	VIN_UNIT_ATTRS(5),
1644 	VIN_UNIT_ATTRS(6),
1645 
1646 	FAN_UNIT_ATTRS(3),
1647 	TEMP_UNIT_ATTRS(3),
1648 	&sensor_dev_attr_pwm3.dev_attr.attr,
1649 
1650 	&sensor_dev_attr_pwm1_freq.dev_attr.attr,
1651 	&sensor_dev_attr_pwm2_freq.dev_attr.attr,
1652 	&sensor_dev_attr_pwm3_freq.dev_attr.attr,
1653 
1654 	&sensor_dev_attr_pwm1_enable.dev_attr.attr,
1655 	&sensor_dev_attr_pwm2_enable.dev_attr.attr,
1656 	&sensor_dev_attr_pwm3_enable.dev_attr.attr,
1657 
1658 	NULL
1659 };
1660 
1661 static const struct attribute_group w83627hf_group_opt = {
1662 	.attrs = w83627hf_attributes_opt,
1663 };
1664 
1665 static int w83627hf_probe(struct platform_device *pdev)
1666 {
1667 	struct device *dev = &pdev->dev;
1668 	struct w83627hf_sio_data *sio_data = dev_get_platdata(dev);
1669 	struct w83627hf_data *data;
1670 	struct resource *res;
1671 	int err, i;
1672 
1673 	static const char *names[] = {
1674 		"w83627hf",
1675 		"w83627thf",
1676 		"w83697hf",
1677 		"w83637hf",
1678 		"w83687thf",
1679 	};
1680 
1681 	res = platform_get_resource(pdev, IORESOURCE_IO, 0);
1682 	if (!devm_request_region(dev, res->start, WINB_REGION_SIZE, DRVNAME)) {
1683 		dev_err(dev, "Failed to request region 0x%lx-0x%lx\n",
1684 			(unsigned long)res->start,
1685 			(unsigned long)(res->start + WINB_REGION_SIZE - 1));
1686 		return -EBUSY;
1687 	}
1688 
1689 	data = devm_kzalloc(dev, sizeof(struct w83627hf_data), GFP_KERNEL);
1690 	if (!data)
1691 		return -ENOMEM;
1692 
1693 	data->addr = res->start;
1694 	data->type = sio_data->type;
1695 	data->name = names[sio_data->type];
1696 	mutex_init(&data->lock);
1697 	mutex_init(&data->update_lock);
1698 	platform_set_drvdata(pdev, data);
1699 
1700 	/* Initialize the chip */
1701 	w83627hf_init_device(pdev);
1702 
1703 	/* A few vars need to be filled upon startup */
1704 	for (i = 0; i <= 2; i++)
1705 		data->fan_min[i] = w83627hf_read_value(
1706 					data, W83627HF_REG_FAN_MIN(i));
1707 	w83627hf_update_fan_div(data);
1708 
1709 	/* Register common device attributes */
1710 	err = sysfs_create_group(&dev->kobj, &w83627hf_group);
1711 	if (err)
1712 		return err;
1713 
1714 	/* Register chip-specific device attributes */
1715 	if (data->type == w83627hf || data->type == w83697hf)
1716 		if ((err = device_create_file(dev,
1717 				&sensor_dev_attr_in5_input.dev_attr))
1718 		 || (err = device_create_file(dev,
1719 				&sensor_dev_attr_in5_min.dev_attr))
1720 		 || (err = device_create_file(dev,
1721 				&sensor_dev_attr_in5_max.dev_attr))
1722 		 || (err = device_create_file(dev,
1723 				&sensor_dev_attr_in5_alarm.dev_attr))
1724 		 || (err = device_create_file(dev,
1725 				&sensor_dev_attr_in5_beep.dev_attr))
1726 		 || (err = device_create_file(dev,
1727 				&sensor_dev_attr_in6_input.dev_attr))
1728 		 || (err = device_create_file(dev,
1729 				&sensor_dev_attr_in6_min.dev_attr))
1730 		 || (err = device_create_file(dev,
1731 				&sensor_dev_attr_in6_max.dev_attr))
1732 		 || (err = device_create_file(dev,
1733 				&sensor_dev_attr_in6_alarm.dev_attr))
1734 		 || (err = device_create_file(dev,
1735 				&sensor_dev_attr_in6_beep.dev_attr))
1736 		 || (err = device_create_file(dev,
1737 				&sensor_dev_attr_pwm1_freq.dev_attr))
1738 		 || (err = device_create_file(dev,
1739 				&sensor_dev_attr_pwm2_freq.dev_attr)))
1740 			goto error;
1741 
1742 	if (data->type != w83697hf)
1743 		if ((err = device_create_file(dev,
1744 				&sensor_dev_attr_in1_input.dev_attr))
1745 		 || (err = device_create_file(dev,
1746 				&sensor_dev_attr_in1_min.dev_attr))
1747 		 || (err = device_create_file(dev,
1748 				&sensor_dev_attr_in1_max.dev_attr))
1749 		 || (err = device_create_file(dev,
1750 				&sensor_dev_attr_in1_alarm.dev_attr))
1751 		 || (err = device_create_file(dev,
1752 				&sensor_dev_attr_in1_beep.dev_attr))
1753 		 || (err = device_create_file(dev,
1754 				&sensor_dev_attr_fan3_input.dev_attr))
1755 		 || (err = device_create_file(dev,
1756 				&sensor_dev_attr_fan3_min.dev_attr))
1757 		 || (err = device_create_file(dev,
1758 				&sensor_dev_attr_fan3_div.dev_attr))
1759 		 || (err = device_create_file(dev,
1760 				&sensor_dev_attr_fan3_alarm.dev_attr))
1761 		 || (err = device_create_file(dev,
1762 				&sensor_dev_attr_fan3_beep.dev_attr))
1763 		 || (err = device_create_file(dev,
1764 				&sensor_dev_attr_temp3_input.dev_attr))
1765 		 || (err = device_create_file(dev,
1766 				&sensor_dev_attr_temp3_max.dev_attr))
1767 		 || (err = device_create_file(dev,
1768 				&sensor_dev_attr_temp3_max_hyst.dev_attr))
1769 		 || (err = device_create_file(dev,
1770 				&sensor_dev_attr_temp3_alarm.dev_attr))
1771 		 || (err = device_create_file(dev,
1772 				&sensor_dev_attr_temp3_beep.dev_attr))
1773 		 || (err = device_create_file(dev,
1774 				&sensor_dev_attr_temp3_type.dev_attr)))
1775 			goto error;
1776 
1777 	if (data->type != w83697hf && data->vid != 0xff) {
1778 		/* Convert VID to voltage based on VRM */
1779 		data->vrm = vid_which_vrm();
1780 
1781 		if ((err = device_create_file(dev, &dev_attr_cpu0_vid))
1782 		 || (err = device_create_file(dev, &dev_attr_vrm)))
1783 			goto error;
1784 	}
1785 
1786 	if (data->type == w83627thf || data->type == w83637hf
1787 	    || data->type == w83687thf) {
1788 		err = device_create_file(dev, &sensor_dev_attr_pwm3.dev_attr);
1789 		if (err)
1790 			goto error;
1791 	}
1792 
1793 	if (data->type == w83637hf || data->type == w83687thf)
1794 		if ((err = device_create_file(dev,
1795 				&sensor_dev_attr_pwm1_freq.dev_attr))
1796 		 || (err = device_create_file(dev,
1797 				&sensor_dev_attr_pwm2_freq.dev_attr))
1798 		 || (err = device_create_file(dev,
1799 				&sensor_dev_attr_pwm3_freq.dev_attr)))
1800 			goto error;
1801 
1802 	if (data->type != w83627hf)
1803 		if ((err = device_create_file(dev,
1804 				&sensor_dev_attr_pwm1_enable.dev_attr))
1805 		 || (err = device_create_file(dev,
1806 				&sensor_dev_attr_pwm2_enable.dev_attr)))
1807 			goto error;
1808 
1809 	if (data->type == w83627thf || data->type == w83637hf
1810 	    || data->type == w83687thf) {
1811 		err = device_create_file(dev,
1812 					 &sensor_dev_attr_pwm3_enable.dev_attr);
1813 		if (err)
1814 			goto error;
1815 	}
1816 
1817 	data->hwmon_dev = hwmon_device_register(dev);
1818 	if (IS_ERR(data->hwmon_dev)) {
1819 		err = PTR_ERR(data->hwmon_dev);
1820 		goto error;
1821 	}
1822 
1823 	return 0;
1824 
1825  error:
1826 	sysfs_remove_group(&dev->kobj, &w83627hf_group);
1827 	sysfs_remove_group(&dev->kobj, &w83627hf_group_opt);
1828 	return err;
1829 }
1830 
1831 static int w83627hf_remove(struct platform_device *pdev)
1832 {
1833 	struct w83627hf_data *data = platform_get_drvdata(pdev);
1834 
1835 	hwmon_device_unregister(data->hwmon_dev);
1836 
1837 	sysfs_remove_group(&pdev->dev.kobj, &w83627hf_group);
1838 	sysfs_remove_group(&pdev->dev.kobj, &w83627hf_group_opt);
1839 
1840 	return 0;
1841 }
1842 
1843 static struct platform_driver w83627hf_driver = {
1844 	.driver = {
1845 		.name	= DRVNAME,
1846 		.pm	= W83627HF_DEV_PM_OPS,
1847 	},
1848 	.probe		= w83627hf_probe,
1849 	.remove		= w83627hf_remove,
1850 };
1851 
1852 static int __init w83627hf_find(int sioaddr, unsigned short *addr,
1853 				struct w83627hf_sio_data *sio_data)
1854 {
1855 	int err;
1856 	u16 val;
1857 
1858 	static __initconst char *const names[] = {
1859 		"W83627HF",
1860 		"W83627THF",
1861 		"W83697HF",
1862 		"W83637HF",
1863 		"W83687THF",
1864 	};
1865 
1866 	sio_data->sioaddr = sioaddr;
1867 	err = superio_enter(sio_data);
1868 	if (err)
1869 		return err;
1870 
1871 	err = -ENODEV;
1872 	val = force_id ? force_id : superio_inb(sio_data, DEVID);
1873 	switch (val) {
1874 	case W627_DEVID:
1875 		sio_data->type = w83627hf;
1876 		break;
1877 	case W627THF_DEVID:
1878 		sio_data->type = w83627thf;
1879 		break;
1880 	case W697_DEVID:
1881 		sio_data->type = w83697hf;
1882 		break;
1883 	case W637_DEVID:
1884 		sio_data->type = w83637hf;
1885 		break;
1886 	case W687THF_DEVID:
1887 		sio_data->type = w83687thf;
1888 		break;
1889 	case 0xff:	/* No device at all */
1890 		goto exit;
1891 	default:
1892 		pr_debug(DRVNAME ": Unsupported chip (DEVID=0x%02x)\n", val);
1893 		goto exit;
1894 	}
1895 
1896 	superio_select(sio_data, W83627HF_LD_HWM);
1897 	val = (superio_inb(sio_data, WINB_BASE_REG) << 8) |
1898 	       superio_inb(sio_data, WINB_BASE_REG + 1);
1899 	*addr = val & WINB_ALIGNMENT;
1900 	if (*addr == 0) {
1901 		pr_warn("Base address not set, skipping\n");
1902 		goto exit;
1903 	}
1904 
1905 	val = superio_inb(sio_data, WINB_ACT_REG);
1906 	if (!(val & 0x01)) {
1907 		pr_warn("Enabling HWM logical device\n");
1908 		superio_outb(sio_data, WINB_ACT_REG, val | 0x01);
1909 	}
1910 
1911 	err = 0;
1912 	pr_info(DRVNAME ": Found %s chip at %#x\n",
1913 		names[sio_data->type], *addr);
1914 
1915  exit:
1916 	superio_exit(sio_data);
1917 	return err;
1918 }
1919 
1920 static int __init w83627hf_device_add(unsigned short address,
1921 				      const struct w83627hf_sio_data *sio_data)
1922 {
1923 	struct resource res = {
1924 		.start	= address + WINB_REGION_OFFSET,
1925 		.end	= address + WINB_REGION_OFFSET + WINB_REGION_SIZE - 1,
1926 		.name	= DRVNAME,
1927 		.flags	= IORESOURCE_IO,
1928 	};
1929 	int err;
1930 
1931 	err = acpi_check_resource_conflict(&res);
1932 	if (err)
1933 		goto exit;
1934 
1935 	pdev = platform_device_alloc(DRVNAME, address);
1936 	if (!pdev) {
1937 		err = -ENOMEM;
1938 		pr_err("Device allocation failed\n");
1939 		goto exit;
1940 	}
1941 
1942 	err = platform_device_add_resources(pdev, &res, 1);
1943 	if (err) {
1944 		pr_err("Device resource addition failed (%d)\n", err);
1945 		goto exit_device_put;
1946 	}
1947 
1948 	err = platform_device_add_data(pdev, sio_data,
1949 				       sizeof(struct w83627hf_sio_data));
1950 	if (err) {
1951 		pr_err("Platform data allocation failed\n");
1952 		goto exit_device_put;
1953 	}
1954 
1955 	err = platform_device_add(pdev);
1956 	if (err) {
1957 		pr_err("Device addition failed (%d)\n", err);
1958 		goto exit_device_put;
1959 	}
1960 
1961 	return 0;
1962 
1963 exit_device_put:
1964 	platform_device_put(pdev);
1965 exit:
1966 	return err;
1967 }
1968 
1969 static int __init sensors_w83627hf_init(void)
1970 {
1971 	int err;
1972 	unsigned short address;
1973 	struct w83627hf_sio_data sio_data;
1974 
1975 	if (w83627hf_find(0x2e, &address, &sio_data)
1976 	 && w83627hf_find(0x4e, &address, &sio_data))
1977 		return -ENODEV;
1978 
1979 	err = platform_driver_register(&w83627hf_driver);
1980 	if (err)
1981 		goto exit;
1982 
1983 	/* Sets global pdev as a side effect */
1984 	err = w83627hf_device_add(address, &sio_data);
1985 	if (err)
1986 		goto exit_driver;
1987 
1988 	return 0;
1989 
1990 exit_driver:
1991 	platform_driver_unregister(&w83627hf_driver);
1992 exit:
1993 	return err;
1994 }
1995 
1996 static void __exit sensors_w83627hf_exit(void)
1997 {
1998 	platform_device_unregister(pdev);
1999 	platform_driver_unregister(&w83627hf_driver);
2000 }
2001 
2002 MODULE_AUTHOR("Frodo Looijaard <frodol@dds.nl>, "
2003 	      "Philip Edelbrock <phil@netroedge.com>, "
2004 	      "and Mark Studebaker <mdsxyz123@yahoo.com>");
2005 MODULE_DESCRIPTION("W83627HF driver");
2006 MODULE_LICENSE("GPL");
2007 
2008 module_init(sensors_w83627hf_init);
2009 module_exit(sensors_w83627hf_exit);
2010