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