xref: /linux/drivers/hwmon/w83627ehf.c (revision 20d0021394c1b070bf04b22c5bc8fdb437edd4c5)
1 /*
2     w83627ehf - Driver for the hardware monitoring functionality of
3                 the Winbond W83627EHF Super-I/O chip
4     Copyright (C) 2005  Jean Delvare <khali@linux-fr.org>
5 
6     Shamelessly ripped from the w83627hf driver
7     Copyright (C) 2003  Mark Studebaker
8 
9     Thanks to Leon Moonen, Steve Cliffe and Grant Coady for their help
10     in testing and debugging this driver.
11 
12     This program is free software; you can redistribute it and/or modify
13     it under the terms of the GNU General Public License as published by
14     the Free Software Foundation; either version 2 of the License, or
15     (at your option) any later version.
16 
17     This program is distributed in the hope that it will be useful,
18     but WITHOUT ANY WARRANTY; without even the implied warranty of
19     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20     GNU General Public License for more details.
21 
22     You should have received a copy of the GNU General Public License
23     along with this program; if not, write to the Free Software
24     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
25 
26 
27     Supports the following chips:
28 
29     Chip        #vin    #fan    #pwm    #temp   chip_id man_id
30     w83627ehf   -       5       -       3       0x88    0x5ca3
31 
32     This is a preliminary version of the driver, only supporting the
33     fan and temperature inputs. The chip does much more than that.
34 */
35 
36 #include <linux/module.h>
37 #include <linux/init.h>
38 #include <linux/slab.h>
39 #include <linux/i2c.h>
40 #include <linux/i2c-sensor.h>
41 #include <asm/io.h>
42 #include "lm75.h"
43 
44 /* Addresses to scan
45    The actual ISA address is read from Super-I/O configuration space */
46 static unsigned short normal_i2c[] = { I2C_CLIENT_END };
47 static unsigned int normal_isa[] = { 0, I2C_CLIENT_ISA_END };
48 
49 /* Insmod parameters */
50 SENSORS_INSMOD_1(w83627ehf);
51 
52 /*
53  * Super-I/O constants and functions
54  */
55 
56 static int REG;		/* The register to read/write */
57 static int VAL;		/* The value to read/write */
58 
59 #define W83627EHF_LD_HWM	0x0b
60 
61 #define SIO_REG_LDSEL		0x07	/* Logical device select */
62 #define SIO_REG_DEVID		0x20	/* Device ID (2 bytes) */
63 #define SIO_REG_ENABLE		0x30	/* Logical device enable */
64 #define SIO_REG_ADDR		0x60	/* Logical device address (2 bytes) */
65 
66 #define SIO_W83627EHF_ID	0x8840
67 #define SIO_ID_MASK		0xFFC0
68 
69 static inline void
70 superio_outb(int reg, int val)
71 {
72 	outb(reg, REG);
73 	outb(val, VAL);
74 }
75 
76 static inline int
77 superio_inb(int reg)
78 {
79 	outb(reg, REG);
80 	return inb(VAL);
81 }
82 
83 static inline void
84 superio_select(int ld)
85 {
86 	outb(SIO_REG_LDSEL, REG);
87 	outb(ld, VAL);
88 }
89 
90 static inline void
91 superio_enter(void)
92 {
93 	outb(0x87, REG);
94 	outb(0x87, REG);
95 }
96 
97 static inline void
98 superio_exit(void)
99 {
100 	outb(0x02, REG);
101 	outb(0x02, VAL);
102 }
103 
104 /*
105  * ISA constants
106  */
107 
108 #define REGION_LENGTH		8
109 #define ADDR_REG_OFFSET		5
110 #define DATA_REG_OFFSET		6
111 
112 #define W83627EHF_REG_BANK		0x4E
113 #define W83627EHF_REG_CONFIG		0x40
114 #define W83627EHF_REG_CHIP_ID		0x49
115 #define W83627EHF_REG_MAN_ID		0x4F
116 
117 static const u16 W83627EHF_REG_FAN[] = { 0x28, 0x29, 0x2a, 0x3f, 0x553 };
118 static const u16 W83627EHF_REG_FAN_MIN[] = { 0x3b, 0x3c, 0x3d, 0x3e, 0x55c };
119 
120 #define W83627EHF_REG_TEMP1		0x27
121 #define W83627EHF_REG_TEMP1_HYST	0x3a
122 #define W83627EHF_REG_TEMP1_OVER	0x39
123 static const u16 W83627EHF_REG_TEMP[] = { 0x150, 0x250 };
124 static const u16 W83627EHF_REG_TEMP_HYST[] = { 0x153, 0x253 };
125 static const u16 W83627EHF_REG_TEMP_OVER[] = { 0x155, 0x255 };
126 static const u16 W83627EHF_REG_TEMP_CONFIG[] = { 0x152, 0x252 };
127 
128 /* Fan clock dividers are spread over the following five registers */
129 #define W83627EHF_REG_FANDIV1		0x47
130 #define W83627EHF_REG_FANDIV2		0x4B
131 #define W83627EHF_REG_VBAT		0x5D
132 #define W83627EHF_REG_DIODE		0x59
133 #define W83627EHF_REG_SMI_OVT		0x4C
134 
135 /*
136  * Conversions
137  */
138 
139 static inline unsigned int
140 fan_from_reg(u8 reg, unsigned int div)
141 {
142 	if (reg == 0 || reg == 255)
143 		return 0;
144 	return 1350000U / (reg * div);
145 }
146 
147 static inline unsigned int
148 div_from_reg(u8 reg)
149 {
150 	return 1 << reg;
151 }
152 
153 static inline int
154 temp1_from_reg(s8 reg)
155 {
156 	return reg * 1000;
157 }
158 
159 static inline s8
160 temp1_to_reg(int temp)
161 {
162 	if (temp <= -128000)
163 		return -128;
164 	if (temp >= 127000)
165 		return 127;
166 	if (temp < 0)
167 		return (temp - 500) / 1000;
168 	return (temp + 500) / 1000;
169 }
170 
171 /*
172  * Data structures and manipulation thereof
173  */
174 
175 struct w83627ehf_data {
176 	struct i2c_client client;
177 	struct semaphore lock;
178 
179 	struct semaphore update_lock;
180 	char valid;		/* !=0 if following fields are valid */
181 	unsigned long last_updated;	/* In jiffies */
182 
183 	/* Register values */
184 	u8 fan[5];
185 	u8 fan_min[5];
186 	u8 fan_div[5];
187 	u8 has_fan;		/* some fan inputs can be disabled */
188 	s8 temp1;
189 	s8 temp1_max;
190 	s8 temp1_max_hyst;
191 	s16 temp[2];
192 	s16 temp_max[2];
193 	s16 temp_max_hyst[2];
194 };
195 
196 static inline int is_word_sized(u16 reg)
197 {
198 	return (((reg & 0xff00) == 0x100
199 	      || (reg & 0xff00) == 0x200)
200 	     && ((reg & 0x00ff) == 0x50
201 	      || (reg & 0x00ff) == 0x53
202 	      || (reg & 0x00ff) == 0x55));
203 }
204 
205 /* We assume that the default bank is 0, thus the following two functions do
206    nothing for registers which live in bank 0. For others, they respectively
207    set the bank register to the correct value (before the register is
208    accessed), and back to 0 (afterwards). */
209 static inline void w83627ehf_set_bank(struct i2c_client *client, u16 reg)
210 {
211 	if (reg & 0xff00) {
212 		outb_p(W83627EHF_REG_BANK, client->addr + ADDR_REG_OFFSET);
213 		outb_p(reg >> 8, client->addr + DATA_REG_OFFSET);
214 	}
215 }
216 
217 static inline void w83627ehf_reset_bank(struct i2c_client *client, u16 reg)
218 {
219 	if (reg & 0xff00) {
220 		outb_p(W83627EHF_REG_BANK, client->addr + ADDR_REG_OFFSET);
221 		outb_p(0, client->addr + DATA_REG_OFFSET);
222 	}
223 }
224 
225 static u16 w83627ehf_read_value(struct i2c_client *client, u16 reg)
226 {
227 	struct w83627ehf_data *data = i2c_get_clientdata(client);
228 	int res, word_sized = is_word_sized(reg);
229 
230 	down(&data->lock);
231 
232 	w83627ehf_set_bank(client, reg);
233 	outb_p(reg & 0xff, client->addr + ADDR_REG_OFFSET);
234 	res = inb_p(client->addr + DATA_REG_OFFSET);
235 	if (word_sized) {
236 		outb_p((reg & 0xff) + 1,
237 		       client->addr + ADDR_REG_OFFSET);
238 		res = (res << 8) + inb_p(client->addr + DATA_REG_OFFSET);
239 	}
240 	w83627ehf_reset_bank(client, reg);
241 
242 	up(&data->lock);
243 
244 	return res;
245 }
246 
247 static int w83627ehf_write_value(struct i2c_client *client, u16 reg, u16 value)
248 {
249 	struct w83627ehf_data *data = i2c_get_clientdata(client);
250 	int word_sized = is_word_sized(reg);
251 
252 	down(&data->lock);
253 
254 	w83627ehf_set_bank(client, reg);
255 	outb_p(reg & 0xff, client->addr + ADDR_REG_OFFSET);
256 	if (word_sized) {
257 		outb_p(value >> 8, client->addr + DATA_REG_OFFSET);
258 		outb_p((reg & 0xff) + 1,
259 		       client->addr + ADDR_REG_OFFSET);
260 	}
261 	outb_p(value & 0xff, client->addr + DATA_REG_OFFSET);
262 	w83627ehf_reset_bank(client, reg);
263 
264 	up(&data->lock);
265 	return 0;
266 }
267 
268 /* This function assumes that the caller holds data->update_lock */
269 static void w83627ehf_write_fan_div(struct i2c_client *client, int nr)
270 {
271 	struct w83627ehf_data *data = i2c_get_clientdata(client);
272 	u8 reg;
273 
274 	switch (nr) {
275 	case 0:
276 		reg = (w83627ehf_read_value(client, W83627EHF_REG_FANDIV1) & 0xcf)
277 		    | ((data->fan_div[0] & 0x03) << 4);
278 		w83627ehf_write_value(client, W83627EHF_REG_FANDIV1, reg);
279 		reg = (w83627ehf_read_value(client, W83627EHF_REG_VBAT) & 0xdf)
280 		    | ((data->fan_div[0] & 0x04) << 3);
281 		w83627ehf_write_value(client, W83627EHF_REG_VBAT, reg);
282 		break;
283 	case 1:
284 		reg = (w83627ehf_read_value(client, W83627EHF_REG_FANDIV1) & 0x3f)
285 		    | ((data->fan_div[1] & 0x03) << 6);
286 		w83627ehf_write_value(client, W83627EHF_REG_FANDIV1, reg);
287 		reg = (w83627ehf_read_value(client, W83627EHF_REG_VBAT) & 0xbf)
288 		    | ((data->fan_div[1] & 0x04) << 4);
289 		w83627ehf_write_value(client, W83627EHF_REG_VBAT, reg);
290 		break;
291 	case 2:
292 		reg = (w83627ehf_read_value(client, W83627EHF_REG_FANDIV2) & 0x3f)
293 		    | ((data->fan_div[2] & 0x03) << 6);
294 		w83627ehf_write_value(client, W83627EHF_REG_FANDIV2, reg);
295 		reg = (w83627ehf_read_value(client, W83627EHF_REG_VBAT) & 0x7f)
296 		    | ((data->fan_div[2] & 0x04) << 5);
297 		w83627ehf_write_value(client, W83627EHF_REG_VBAT, reg);
298 		break;
299 	case 3:
300 		reg = (w83627ehf_read_value(client, W83627EHF_REG_DIODE) & 0xfc)
301 		    | (data->fan_div[3] & 0x03);
302 		w83627ehf_write_value(client, W83627EHF_REG_DIODE, reg);
303 		reg = (w83627ehf_read_value(client, W83627EHF_REG_SMI_OVT) & 0x7f)
304 		    | ((data->fan_div[3] & 0x04) << 5);
305 		w83627ehf_write_value(client, W83627EHF_REG_SMI_OVT, reg);
306 		break;
307 	case 4:
308 		reg = (w83627ehf_read_value(client, W83627EHF_REG_DIODE) & 0x73)
309 		    | ((data->fan_div[4] & 0x03) << 3)
310 		    | ((data->fan_div[4] & 0x04) << 5);
311 		w83627ehf_write_value(client, W83627EHF_REG_DIODE, reg);
312 		break;
313 	}
314 }
315 
316 static struct w83627ehf_data *w83627ehf_update_device(struct device *dev)
317 {
318 	struct i2c_client *client = to_i2c_client(dev);
319 	struct w83627ehf_data *data = i2c_get_clientdata(client);
320 	int i;
321 
322 	down(&data->update_lock);
323 
324 	if (time_after(jiffies, data->last_updated + HZ)
325 	 || !data->valid) {
326 		/* Fan clock dividers */
327 		i = w83627ehf_read_value(client, W83627EHF_REG_FANDIV1);
328 		data->fan_div[0] = (i >> 4) & 0x03;
329 		data->fan_div[1] = (i >> 6) & 0x03;
330 		i = w83627ehf_read_value(client, W83627EHF_REG_FANDIV2);
331 		data->fan_div[2] = (i >> 6) & 0x03;
332 		i = w83627ehf_read_value(client, W83627EHF_REG_VBAT);
333 		data->fan_div[0] |= (i >> 3) & 0x04;
334 		data->fan_div[1] |= (i >> 4) & 0x04;
335 		data->fan_div[2] |= (i >> 5) & 0x04;
336 		if (data->has_fan & ((1 << 3) | (1 << 4))) {
337 			i = w83627ehf_read_value(client, W83627EHF_REG_DIODE);
338 			data->fan_div[3] = i & 0x03;
339 			data->fan_div[4] = ((i >> 2) & 0x03)
340 					 | ((i >> 5) & 0x04);
341 		}
342 		if (data->has_fan & (1 << 3)) {
343 			i = w83627ehf_read_value(client, W83627EHF_REG_SMI_OVT);
344 			data->fan_div[3] |= (i >> 5) & 0x04;
345 		}
346 
347 		/* Measured fan speeds and limits */
348 		for (i = 0; i < 5; i++) {
349 			if (!(data->has_fan & (1 << i)))
350 				continue;
351 
352 			data->fan[i] = w83627ehf_read_value(client,
353 				       W83627EHF_REG_FAN[i]);
354 			data->fan_min[i] = w83627ehf_read_value(client,
355 					   W83627EHF_REG_FAN_MIN[i]);
356 
357 			/* If we failed to measure the fan speed and clock
358 			   divider can be increased, let's try that for next
359 			   time */
360 			if (data->fan[i] == 0xff
361 			 && data->fan_div[i] < 0x07) {
362 			 	dev_dbg(&client->dev, "Increasing fan %d "
363 					"clock divider from %u to %u\n",
364 					i, div_from_reg(data->fan_div[i]),
365 					div_from_reg(data->fan_div[i] + 1));
366 				data->fan_div[i]++;
367 				w83627ehf_write_fan_div(client, i);
368 				/* Preserve min limit if possible */
369 				if (data->fan_min[i] >= 2
370 				 && data->fan_min[i] != 255)
371 					w83627ehf_write_value(client,
372 						W83627EHF_REG_FAN_MIN[i],
373 						(data->fan_min[i] /= 2));
374 			}
375 		}
376 
377 		/* Measured temperatures and limits */
378 		data->temp1 = w83627ehf_read_value(client,
379 			      W83627EHF_REG_TEMP1);
380 		data->temp1_max = w83627ehf_read_value(client,
381 				  W83627EHF_REG_TEMP1_OVER);
382 		data->temp1_max_hyst = w83627ehf_read_value(client,
383 				       W83627EHF_REG_TEMP1_HYST);
384 		for (i = 0; i < 2; i++) {
385 			data->temp[i] = w83627ehf_read_value(client,
386 					W83627EHF_REG_TEMP[i]);
387 			data->temp_max[i] = w83627ehf_read_value(client,
388 					    W83627EHF_REG_TEMP_OVER[i]);
389 			data->temp_max_hyst[i] = w83627ehf_read_value(client,
390 						 W83627EHF_REG_TEMP_HYST[i]);
391 		}
392 
393 		data->last_updated = jiffies;
394 		data->valid = 1;
395 	}
396 
397 	up(&data->update_lock);
398 	return data;
399 }
400 
401 /*
402  * Sysfs callback functions
403  */
404 
405 #define show_fan_reg(reg) \
406 static ssize_t \
407 show_##reg(struct device *dev, char *buf, int nr) \
408 { \
409 	struct w83627ehf_data *data = w83627ehf_update_device(dev); \
410 	return sprintf(buf, "%d\n", \
411 		       fan_from_reg(data->reg[nr], \
412 				    div_from_reg(data->fan_div[nr]))); \
413 }
414 show_fan_reg(fan);
415 show_fan_reg(fan_min);
416 
417 static ssize_t
418 show_fan_div(struct device *dev, char *buf, int nr)
419 {
420 	struct w83627ehf_data *data = w83627ehf_update_device(dev);
421 	return sprintf(buf, "%u\n",
422 		       div_from_reg(data->fan_div[nr]));
423 }
424 
425 static ssize_t
426 store_fan_min(struct device *dev, const char *buf, size_t count, int nr)
427 {
428 	struct i2c_client *client = to_i2c_client(dev);
429 	struct w83627ehf_data *data = i2c_get_clientdata(client);
430 	unsigned int val = simple_strtoul(buf, NULL, 10);
431 	unsigned int reg;
432 	u8 new_div;
433 
434 	down(&data->update_lock);
435 	if (!val) {
436 		/* No min limit, alarm disabled */
437 		data->fan_min[nr] = 255;
438 		new_div = data->fan_div[nr]; /* No change */
439 		dev_info(dev, "fan%u low limit and alarm disabled\n", nr + 1);
440 	} else if ((reg = 1350000U / val) >= 128 * 255) {
441 		/* Speed below this value cannot possibly be represented,
442 		   even with the highest divider (128) */
443 		data->fan_min[nr] = 254;
444 		new_div = 7; /* 128 == (1 << 7) */
445 		dev_warn(dev, "fan%u low limit %u below minimum %u, set to "
446 			 "minimum\n", nr + 1, val, fan_from_reg(254, 128));
447 	} else if (!reg) {
448 		/* Speed above this value cannot possibly be represented,
449 		   even with the lowest divider (1) */
450 		data->fan_min[nr] = 1;
451 		new_div = 0; /* 1 == (1 << 0) */
452 		dev_warn(dev, "fan%u low limit %u above maximum %u, set to "
453 			 "maximum\n", nr + 1, val, fan_from_reg(1, 1));
454 	} else {
455 		/* Automatically pick the best divider, i.e. the one such
456 		   that the min limit will correspond to a register value
457 		   in the 96..192 range */
458 		new_div = 0;
459 		while (reg > 192 && new_div < 7) {
460 			reg >>= 1;
461 			new_div++;
462 		}
463 		data->fan_min[nr] = reg;
464 	}
465 
466 	/* Write both the fan clock divider (if it changed) and the new
467 	   fan min (unconditionally) */
468 	if (new_div != data->fan_div[nr]) {
469 		if (new_div > data->fan_div[nr])
470 			data->fan[nr] >>= (data->fan_div[nr] - new_div);
471 		else
472 			data->fan[nr] <<= (new_div - data->fan_div[nr]);
473 
474 		dev_dbg(dev, "fan%u clock divider changed from %u to %u\n",
475 			nr + 1, div_from_reg(data->fan_div[nr]),
476 			div_from_reg(new_div));
477 		data->fan_div[nr] = new_div;
478 		w83627ehf_write_fan_div(client, nr);
479 	}
480 	w83627ehf_write_value(client, W83627EHF_REG_FAN_MIN[nr],
481 			      data->fan_min[nr]);
482 	up(&data->update_lock);
483 
484 	return count;
485 }
486 
487 #define sysfs_fan_offset(offset) \
488 static ssize_t \
489 show_reg_fan_##offset(struct device *dev, struct device_attribute *attr, \
490 		      char *buf) \
491 { \
492 	return show_fan(dev, buf, offset-1); \
493 } \
494 static DEVICE_ATTR(fan##offset##_input, S_IRUGO, \
495 		   show_reg_fan_##offset, NULL);
496 
497 #define sysfs_fan_min_offset(offset) \
498 static ssize_t \
499 show_reg_fan##offset##_min(struct device *dev, struct device_attribute *attr, \
500 			   char *buf) \
501 { \
502 	return show_fan_min(dev, buf, offset-1); \
503 } \
504 static ssize_t \
505 store_reg_fan##offset##_min(struct device *dev, struct device_attribute *attr, \
506 			    const char *buf, size_t count) \
507 { \
508 	return store_fan_min(dev, buf, count, offset-1); \
509 } \
510 static DEVICE_ATTR(fan##offset##_min, S_IRUGO | S_IWUSR, \
511 		   show_reg_fan##offset##_min, \
512 		   store_reg_fan##offset##_min);
513 
514 #define sysfs_fan_div_offset(offset) \
515 static ssize_t \
516 show_reg_fan##offset##_div(struct device *dev, struct device_attribute *attr, \
517 			   char *buf) \
518 { \
519 	return show_fan_div(dev, buf, offset - 1); \
520 } \
521 static DEVICE_ATTR(fan##offset##_div, S_IRUGO, \
522 		   show_reg_fan##offset##_div, NULL);
523 
524 sysfs_fan_offset(1);
525 sysfs_fan_min_offset(1);
526 sysfs_fan_div_offset(1);
527 sysfs_fan_offset(2);
528 sysfs_fan_min_offset(2);
529 sysfs_fan_div_offset(2);
530 sysfs_fan_offset(3);
531 sysfs_fan_min_offset(3);
532 sysfs_fan_div_offset(3);
533 sysfs_fan_offset(4);
534 sysfs_fan_min_offset(4);
535 sysfs_fan_div_offset(4);
536 sysfs_fan_offset(5);
537 sysfs_fan_min_offset(5);
538 sysfs_fan_div_offset(5);
539 
540 #define show_temp1_reg(reg) \
541 static ssize_t \
542 show_##reg(struct device *dev, struct device_attribute *attr, \
543 	   char *buf) \
544 { \
545 	struct w83627ehf_data *data = w83627ehf_update_device(dev); \
546 	return sprintf(buf, "%d\n", temp1_from_reg(data->reg)); \
547 }
548 show_temp1_reg(temp1);
549 show_temp1_reg(temp1_max);
550 show_temp1_reg(temp1_max_hyst);
551 
552 #define store_temp1_reg(REG, reg) \
553 static ssize_t \
554 store_temp1_##reg(struct device *dev, struct device_attribute *attr, \
555 		  const char *buf, size_t count) \
556 { \
557 	struct i2c_client *client = to_i2c_client(dev); \
558 	struct w83627ehf_data *data = i2c_get_clientdata(client); \
559 	u32 val = simple_strtoul(buf, NULL, 10); \
560  \
561 	down(&data->update_lock); \
562 	data->temp1_##reg = temp1_to_reg(val); \
563 	w83627ehf_write_value(client, W83627EHF_REG_TEMP1_##REG, \
564 			      data->temp1_##reg); \
565 	up(&data->update_lock); \
566 	return count; \
567 }
568 store_temp1_reg(OVER, max);
569 store_temp1_reg(HYST, max_hyst);
570 
571 static DEVICE_ATTR(temp1_input, S_IRUGO, show_temp1, NULL);
572 static DEVICE_ATTR(temp1_max, S_IRUGO| S_IWUSR,
573 		   show_temp1_max, store_temp1_max);
574 static DEVICE_ATTR(temp1_max_hyst, S_IRUGO| S_IWUSR,
575 		   show_temp1_max_hyst, store_temp1_max_hyst);
576 
577 #define show_temp_reg(reg) \
578 static ssize_t \
579 show_##reg (struct device *dev, char *buf, int nr) \
580 { \
581 	struct w83627ehf_data *data = w83627ehf_update_device(dev); \
582 	return sprintf(buf, "%d\n", \
583 		       LM75_TEMP_FROM_REG(data->reg[nr])); \
584 }
585 show_temp_reg(temp);
586 show_temp_reg(temp_max);
587 show_temp_reg(temp_max_hyst);
588 
589 #define store_temp_reg(REG, reg) \
590 static ssize_t \
591 store_##reg (struct device *dev, const char *buf, size_t count, int nr) \
592 { \
593 	struct i2c_client *client = to_i2c_client(dev); \
594 	struct w83627ehf_data *data = i2c_get_clientdata(client); \
595 	u32 val = simple_strtoul(buf, NULL, 10); \
596  \
597 	down(&data->update_lock); \
598 	data->reg[nr] = LM75_TEMP_TO_REG(val); \
599 	w83627ehf_write_value(client, W83627EHF_REG_TEMP_##REG[nr], \
600 			      data->reg[nr]); \
601 	up(&data->update_lock); \
602 	return count; \
603 }
604 store_temp_reg(OVER, temp_max);
605 store_temp_reg(HYST, temp_max_hyst);
606 
607 #define sysfs_temp_offset(offset) \
608 static ssize_t \
609 show_reg_temp##offset (struct device *dev, struct device_attribute *attr, \
610 		       char *buf) \
611 { \
612 	return show_temp(dev, buf, offset - 2); \
613 } \
614 static DEVICE_ATTR(temp##offset##_input, S_IRUGO, \
615 		   show_reg_temp##offset, NULL);
616 
617 #define sysfs_temp_reg_offset(reg, offset) \
618 static ssize_t \
619 show_reg_temp##offset##_##reg(struct device *dev, struct device_attribute *attr, \
620 			      char *buf) \
621 { \
622 	return show_temp_##reg(dev, buf, offset - 2); \
623 } \
624 static ssize_t \
625 store_reg_temp##offset##_##reg(struct device *dev, struct device_attribute *attr, \
626 			       const char *buf, size_t count) \
627 { \
628 	return store_temp_##reg(dev, buf, count, offset - 2); \
629 } \
630 static DEVICE_ATTR(temp##offset##_##reg, S_IRUGO| S_IWUSR, \
631 		   show_reg_temp##offset##_##reg, \
632 		   store_reg_temp##offset##_##reg);
633 
634 sysfs_temp_offset(2);
635 sysfs_temp_reg_offset(max, 2);
636 sysfs_temp_reg_offset(max_hyst, 2);
637 sysfs_temp_offset(3);
638 sysfs_temp_reg_offset(max, 3);
639 sysfs_temp_reg_offset(max_hyst, 3);
640 
641 /*
642  * Driver and client management
643  */
644 
645 static struct i2c_driver w83627ehf_driver;
646 
647 static void w83627ehf_init_client(struct i2c_client *client)
648 {
649 	int i;
650 	u8 tmp;
651 
652 	/* Start monitoring is needed */
653 	tmp = w83627ehf_read_value(client, W83627EHF_REG_CONFIG);
654 	if (!(tmp & 0x01))
655 		w83627ehf_write_value(client, W83627EHF_REG_CONFIG,
656 				      tmp | 0x01);
657 
658 	/* Enable temp2 and temp3 if needed */
659 	for (i = 0; i < 2; i++) {
660 		tmp = w83627ehf_read_value(client,
661 					   W83627EHF_REG_TEMP_CONFIG[i]);
662 		if (tmp & 0x01)
663 			w83627ehf_write_value(client,
664 					      W83627EHF_REG_TEMP_CONFIG[i],
665 					      tmp & 0xfe);
666 	}
667 }
668 
669 static int w83627ehf_detect(struct i2c_adapter *adapter, int address, int kind)
670 {
671 	struct i2c_client *client;
672 	struct w83627ehf_data *data;
673 	int i, err = 0;
674 
675 	if (!i2c_is_isa_adapter(adapter))
676 		return 0;
677 
678 	if (!request_region(address, REGION_LENGTH, w83627ehf_driver.name)) {
679 		err = -EBUSY;
680 		goto exit;
681 	}
682 
683 	if (!(data = kmalloc(sizeof(struct w83627ehf_data), GFP_KERNEL))) {
684 		err = -ENOMEM;
685 		goto exit_release;
686 	}
687 	memset(data, 0, sizeof(struct w83627ehf_data));
688 
689 	client = &data->client;
690 	i2c_set_clientdata(client, data);
691 	client->addr = address;
692 	init_MUTEX(&data->lock);
693 	client->adapter = adapter;
694 	client->driver = &w83627ehf_driver;
695 	client->flags = 0;
696 
697 	strlcpy(client->name, "w83627ehf", I2C_NAME_SIZE);
698 	data->valid = 0;
699 	init_MUTEX(&data->update_lock);
700 
701 	/* Tell the i2c layer a new client has arrived */
702 	if ((err = i2c_attach_client(client)))
703 		goto exit_free;
704 
705 	/* Initialize the chip */
706 	w83627ehf_init_client(client);
707 
708 	/* A few vars need to be filled upon startup */
709 	for (i = 0; i < 5; i++)
710 		data->fan_min[i] = w83627ehf_read_value(client,
711 				   W83627EHF_REG_FAN_MIN[i]);
712 
713 	/* It looks like fan4 and fan5 pins can be alternatively used
714 	   as fan on/off switches */
715 	data->has_fan = 0x07; /* fan1, fan2 and fan3 */
716 	i = w83627ehf_read_value(client, W83627EHF_REG_FANDIV1);
717 	if (i & (1 << 2))
718 		data->has_fan |= (1 << 3);
719 	if (i & (1 << 0))
720 		data->has_fan |= (1 << 4);
721 
722 	/* Register sysfs hooks */
723 	device_create_file(&client->dev, &dev_attr_fan1_input);
724 	device_create_file(&client->dev, &dev_attr_fan1_min);
725 	device_create_file(&client->dev, &dev_attr_fan1_div);
726 	device_create_file(&client->dev, &dev_attr_fan2_input);
727 	device_create_file(&client->dev, &dev_attr_fan2_min);
728 	device_create_file(&client->dev, &dev_attr_fan2_div);
729 	device_create_file(&client->dev, &dev_attr_fan3_input);
730 	device_create_file(&client->dev, &dev_attr_fan3_min);
731 	device_create_file(&client->dev, &dev_attr_fan3_div);
732 
733 	if (data->has_fan & (1 << 3)) {
734 		device_create_file(&client->dev, &dev_attr_fan4_input);
735 		device_create_file(&client->dev, &dev_attr_fan4_min);
736 		device_create_file(&client->dev, &dev_attr_fan4_div);
737 	}
738 	if (data->has_fan & (1 << 4)) {
739 		device_create_file(&client->dev, &dev_attr_fan5_input);
740 		device_create_file(&client->dev, &dev_attr_fan5_min);
741 		device_create_file(&client->dev, &dev_attr_fan5_div);
742 	}
743 
744 	device_create_file(&client->dev, &dev_attr_temp1_input);
745 	device_create_file(&client->dev, &dev_attr_temp1_max);
746 	device_create_file(&client->dev, &dev_attr_temp1_max_hyst);
747 	device_create_file(&client->dev, &dev_attr_temp2_input);
748 	device_create_file(&client->dev, &dev_attr_temp2_max);
749 	device_create_file(&client->dev, &dev_attr_temp2_max_hyst);
750 	device_create_file(&client->dev, &dev_attr_temp3_input);
751 	device_create_file(&client->dev, &dev_attr_temp3_max);
752 	device_create_file(&client->dev, &dev_attr_temp3_max_hyst);
753 
754 	return 0;
755 
756 exit_free:
757 	kfree(data);
758 exit_release:
759 	release_region(address, REGION_LENGTH);
760 exit:
761 	return err;
762 }
763 
764 static int w83627ehf_attach_adapter(struct i2c_adapter *adapter)
765 {
766 	if (!(adapter->class & I2C_CLASS_HWMON))
767 		return 0;
768 	return i2c_detect(adapter, &addr_data, w83627ehf_detect);
769 }
770 
771 static int w83627ehf_detach_client(struct i2c_client *client)
772 {
773 	int err;
774 
775 	if ((err = i2c_detach_client(client))) {
776 		dev_err(&client->dev, "Client deregistration failed, "
777 			"client not detached.\n");
778 		return err;
779 	}
780 	release_region(client->addr, REGION_LENGTH);
781 	kfree(i2c_get_clientdata(client));
782 
783 	return 0;
784 }
785 
786 static struct i2c_driver w83627ehf_driver = {
787 	.owner		= THIS_MODULE,
788 	.name		= "w83627ehf",
789 	.flags		= I2C_DF_NOTIFY,
790 	.attach_adapter	= w83627ehf_attach_adapter,
791 	.detach_client	= w83627ehf_detach_client,
792 };
793 
794 static int __init w83627ehf_find(int sioaddr, int *address)
795 {
796 	u16 val;
797 
798 	REG = sioaddr;
799 	VAL = sioaddr + 1;
800 	superio_enter();
801 
802 	val = (superio_inb(SIO_REG_DEVID) << 8)
803 	    | superio_inb(SIO_REG_DEVID + 1);
804 	if ((val & SIO_ID_MASK) != SIO_W83627EHF_ID) {
805 		superio_exit();
806 		return -ENODEV;
807 	}
808 
809 	superio_select(W83627EHF_LD_HWM);
810 	val = (superio_inb(SIO_REG_ADDR) << 8)
811 	    | superio_inb(SIO_REG_ADDR + 1);
812 	*address = val & ~(REGION_LENGTH - 1);
813 	if (*address == 0) {
814 		superio_exit();
815 		return -ENODEV;
816 	}
817 
818 	/* Activate logical device if needed */
819 	val = superio_inb(SIO_REG_ENABLE);
820 	if (!(val & 0x01))
821 		superio_outb(SIO_REG_ENABLE, val | 0x01);
822 
823 	superio_exit();
824 	return 0;
825 }
826 
827 static int __init sensors_w83627ehf_init(void)
828 {
829 	if (w83627ehf_find(0x2e, &normal_isa[0])
830 	 && w83627ehf_find(0x4e, &normal_isa[0]))
831 		return -ENODEV;
832 
833 	return i2c_add_driver(&w83627ehf_driver);
834 }
835 
836 static void __exit sensors_w83627ehf_exit(void)
837 {
838 	i2c_del_driver(&w83627ehf_driver);
839 }
840 
841 MODULE_AUTHOR("Jean Delvare <khali@linux-fr.org>");
842 MODULE_DESCRIPTION("W83627EHF driver");
843 MODULE_LICENSE("GPL");
844 
845 module_init(sensors_w83627ehf_init);
846 module_exit(sensors_w83627ehf_exit);
847