Merge master.kernel.org:/home/rmk/linux-2.6-arm
[linux-2.6/x86.git] / drivers / hwmon / w83627ehf.c
blobeee22a57e929dbf8d09a07693fc28ce8fb96fc8f
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>
6 Shamelessly ripped from the w83627hf driver
7 Copyright (C) 2003 Mark Studebaker
9 Thanks to Leon Moonen, Steve Cliffe and Grant Coady for their help
10 in testing and debugging this driver.
12 This driver also supports the W83627EHG, which is the lead-free
13 version of the W83627EHF.
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.
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.
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.
30 Supports the following chips:
32 Chip #vin #fan #pwm #temp chip_id man_id
33 w83627ehf - 5 - 3 0x88 0x5ca3
35 This is a preliminary version of the driver, only supporting the
36 fan and temperature inputs. The chip does much more than that.
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/err.h>
46 #include <asm/io.h>
47 #include "lm75.h"
49 /* The actual ISA address is read from Super-I/O configuration space */
50 static unsigned short address;
53 * Super-I/O constants and functions
56 static int REG; /* The register to read/write */
57 static int VAL; /* The value to read/write */
59 #define W83627EHF_LD_HWM 0x0b
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) */
66 #define SIO_W83627EHF_ID 0x8840
67 #define SIO_ID_MASK 0xFFC0
69 static inline void
70 superio_outb(int reg, int val)
72 outb(reg, REG);
73 outb(val, VAL);
76 static inline int
77 superio_inb(int reg)
79 outb(reg, REG);
80 return inb(VAL);
83 static inline void
84 superio_select(int ld)
86 outb(SIO_REG_LDSEL, REG);
87 outb(ld, VAL);
90 static inline void
91 superio_enter(void)
93 outb(0x87, REG);
94 outb(0x87, REG);
97 static inline void
98 superio_exit(void)
100 outb(0x02, REG);
101 outb(0x02, VAL);
105 * ISA constants
108 #define REGION_ALIGNMENT ~7
109 #define REGION_OFFSET 5
110 #define REGION_LENGTH 2
111 #define ADDR_REG_OFFSET 5
112 #define DATA_REG_OFFSET 6
114 #define W83627EHF_REG_BANK 0x4E
115 #define W83627EHF_REG_CONFIG 0x40
116 #define W83627EHF_REG_CHIP_ID 0x49
117 #define W83627EHF_REG_MAN_ID 0x4F
119 static const u16 W83627EHF_REG_FAN[] = { 0x28, 0x29, 0x2a, 0x3f, 0x553 };
120 static const u16 W83627EHF_REG_FAN_MIN[] = { 0x3b, 0x3c, 0x3d, 0x3e, 0x55c };
122 #define W83627EHF_REG_TEMP1 0x27
123 #define W83627EHF_REG_TEMP1_HYST 0x3a
124 #define W83627EHF_REG_TEMP1_OVER 0x39
125 static const u16 W83627EHF_REG_TEMP[] = { 0x150, 0x250 };
126 static const u16 W83627EHF_REG_TEMP_HYST[] = { 0x153, 0x253 };
127 static const u16 W83627EHF_REG_TEMP_OVER[] = { 0x155, 0x255 };
128 static const u16 W83627EHF_REG_TEMP_CONFIG[] = { 0x152, 0x252 };
130 /* Fan clock dividers are spread over the following five registers */
131 #define W83627EHF_REG_FANDIV1 0x47
132 #define W83627EHF_REG_FANDIV2 0x4B
133 #define W83627EHF_REG_VBAT 0x5D
134 #define W83627EHF_REG_DIODE 0x59
135 #define W83627EHF_REG_SMI_OVT 0x4C
138 * Conversions
141 static inline unsigned int
142 fan_from_reg(u8 reg, unsigned int div)
144 if (reg == 0 || reg == 255)
145 return 0;
146 return 1350000U / (reg * div);
149 static inline unsigned int
150 div_from_reg(u8 reg)
152 return 1 << reg;
155 static inline int
156 temp1_from_reg(s8 reg)
158 return reg * 1000;
161 static inline s8
162 temp1_to_reg(int temp)
164 if (temp <= -128000)
165 return -128;
166 if (temp >= 127000)
167 return 127;
168 if (temp < 0)
169 return (temp - 500) / 1000;
170 return (temp + 500) / 1000;
174 * Data structures and manipulation thereof
177 struct w83627ehf_data {
178 struct i2c_client client;
179 struct class_device *class_dev;
180 struct semaphore lock;
182 struct semaphore update_lock;
183 char valid; /* !=0 if following fields are valid */
184 unsigned long last_updated; /* In jiffies */
186 /* Register values */
187 u8 fan[5];
188 u8 fan_min[5];
189 u8 fan_div[5];
190 u8 has_fan; /* some fan inputs can be disabled */
191 s8 temp1;
192 s8 temp1_max;
193 s8 temp1_max_hyst;
194 s16 temp[2];
195 s16 temp_max[2];
196 s16 temp_max_hyst[2];
199 static inline int is_word_sized(u16 reg)
201 return (((reg & 0xff00) == 0x100
202 || (reg & 0xff00) == 0x200)
203 && ((reg & 0x00ff) == 0x50
204 || (reg & 0x00ff) == 0x53
205 || (reg & 0x00ff) == 0x55));
208 /* We assume that the default bank is 0, thus the following two functions do
209 nothing for registers which live in bank 0. For others, they respectively
210 set the bank register to the correct value (before the register is
211 accessed), and back to 0 (afterwards). */
212 static inline void w83627ehf_set_bank(struct i2c_client *client, u16 reg)
214 if (reg & 0xff00) {
215 outb_p(W83627EHF_REG_BANK, client->addr + ADDR_REG_OFFSET);
216 outb_p(reg >> 8, client->addr + DATA_REG_OFFSET);
220 static inline void w83627ehf_reset_bank(struct i2c_client *client, u16 reg)
222 if (reg & 0xff00) {
223 outb_p(W83627EHF_REG_BANK, client->addr + ADDR_REG_OFFSET);
224 outb_p(0, client->addr + DATA_REG_OFFSET);
228 static u16 w83627ehf_read_value(struct i2c_client *client, u16 reg)
230 struct w83627ehf_data *data = i2c_get_clientdata(client);
231 int res, word_sized = is_word_sized(reg);
233 down(&data->lock);
235 w83627ehf_set_bank(client, reg);
236 outb_p(reg & 0xff, client->addr + ADDR_REG_OFFSET);
237 res = inb_p(client->addr + DATA_REG_OFFSET);
238 if (word_sized) {
239 outb_p((reg & 0xff) + 1,
240 client->addr + ADDR_REG_OFFSET);
241 res = (res << 8) + inb_p(client->addr + DATA_REG_OFFSET);
243 w83627ehf_reset_bank(client, reg);
245 up(&data->lock);
247 return res;
250 static int w83627ehf_write_value(struct i2c_client *client, u16 reg, u16 value)
252 struct w83627ehf_data *data = i2c_get_clientdata(client);
253 int word_sized = is_word_sized(reg);
255 down(&data->lock);
257 w83627ehf_set_bank(client, reg);
258 outb_p(reg & 0xff, client->addr + ADDR_REG_OFFSET);
259 if (word_sized) {
260 outb_p(value >> 8, client->addr + DATA_REG_OFFSET);
261 outb_p((reg & 0xff) + 1,
262 client->addr + ADDR_REG_OFFSET);
264 outb_p(value & 0xff, client->addr + DATA_REG_OFFSET);
265 w83627ehf_reset_bank(client, reg);
267 up(&data->lock);
268 return 0;
271 /* This function assumes that the caller holds data->update_lock */
272 static void w83627ehf_write_fan_div(struct i2c_client *client, int nr)
274 struct w83627ehf_data *data = i2c_get_clientdata(client);
275 u8 reg;
277 switch (nr) {
278 case 0:
279 reg = (w83627ehf_read_value(client, W83627EHF_REG_FANDIV1) & 0xcf)
280 | ((data->fan_div[0] & 0x03) << 4);
281 w83627ehf_write_value(client, W83627EHF_REG_FANDIV1, reg);
282 reg = (w83627ehf_read_value(client, W83627EHF_REG_VBAT) & 0xdf)
283 | ((data->fan_div[0] & 0x04) << 3);
284 w83627ehf_write_value(client, W83627EHF_REG_VBAT, reg);
285 break;
286 case 1:
287 reg = (w83627ehf_read_value(client, W83627EHF_REG_FANDIV1) & 0x3f)
288 | ((data->fan_div[1] & 0x03) << 6);
289 w83627ehf_write_value(client, W83627EHF_REG_FANDIV1, reg);
290 reg = (w83627ehf_read_value(client, W83627EHF_REG_VBAT) & 0xbf)
291 | ((data->fan_div[1] & 0x04) << 4);
292 w83627ehf_write_value(client, W83627EHF_REG_VBAT, reg);
293 break;
294 case 2:
295 reg = (w83627ehf_read_value(client, W83627EHF_REG_FANDIV2) & 0x3f)
296 | ((data->fan_div[2] & 0x03) << 6);
297 w83627ehf_write_value(client, W83627EHF_REG_FANDIV2, reg);
298 reg = (w83627ehf_read_value(client, W83627EHF_REG_VBAT) & 0x7f)
299 | ((data->fan_div[2] & 0x04) << 5);
300 w83627ehf_write_value(client, W83627EHF_REG_VBAT, reg);
301 break;
302 case 3:
303 reg = (w83627ehf_read_value(client, W83627EHF_REG_DIODE) & 0xfc)
304 | (data->fan_div[3] & 0x03);
305 w83627ehf_write_value(client, W83627EHF_REG_DIODE, reg);
306 reg = (w83627ehf_read_value(client, W83627EHF_REG_SMI_OVT) & 0x7f)
307 | ((data->fan_div[3] & 0x04) << 5);
308 w83627ehf_write_value(client, W83627EHF_REG_SMI_OVT, reg);
309 break;
310 case 4:
311 reg = (w83627ehf_read_value(client, W83627EHF_REG_DIODE) & 0x73)
312 | ((data->fan_div[4] & 0x03) << 3)
313 | ((data->fan_div[4] & 0x04) << 5);
314 w83627ehf_write_value(client, W83627EHF_REG_DIODE, reg);
315 break;
319 static struct w83627ehf_data *w83627ehf_update_device(struct device *dev)
321 struct i2c_client *client = to_i2c_client(dev);
322 struct w83627ehf_data *data = i2c_get_clientdata(client);
323 int i;
325 down(&data->update_lock);
327 if (time_after(jiffies, data->last_updated + HZ)
328 || !data->valid) {
329 /* Fan clock dividers */
330 i = w83627ehf_read_value(client, W83627EHF_REG_FANDIV1);
331 data->fan_div[0] = (i >> 4) & 0x03;
332 data->fan_div[1] = (i >> 6) & 0x03;
333 i = w83627ehf_read_value(client, W83627EHF_REG_FANDIV2);
334 data->fan_div[2] = (i >> 6) & 0x03;
335 i = w83627ehf_read_value(client, W83627EHF_REG_VBAT);
336 data->fan_div[0] |= (i >> 3) & 0x04;
337 data->fan_div[1] |= (i >> 4) & 0x04;
338 data->fan_div[2] |= (i >> 5) & 0x04;
339 if (data->has_fan & ((1 << 3) | (1 << 4))) {
340 i = w83627ehf_read_value(client, W83627EHF_REG_DIODE);
341 data->fan_div[3] = i & 0x03;
342 data->fan_div[4] = ((i >> 2) & 0x03)
343 | ((i >> 5) & 0x04);
345 if (data->has_fan & (1 << 3)) {
346 i = w83627ehf_read_value(client, W83627EHF_REG_SMI_OVT);
347 data->fan_div[3] |= (i >> 5) & 0x04;
350 /* Measured fan speeds and limits */
351 for (i = 0; i < 5; i++) {
352 if (!(data->has_fan & (1 << i)))
353 continue;
355 data->fan[i] = w83627ehf_read_value(client,
356 W83627EHF_REG_FAN[i]);
357 data->fan_min[i] = w83627ehf_read_value(client,
358 W83627EHF_REG_FAN_MIN[i]);
360 /* If we failed to measure the fan speed and clock
361 divider can be increased, let's try that for next
362 time */
363 if (data->fan[i] == 0xff
364 && data->fan_div[i] < 0x07) {
365 dev_dbg(&client->dev, "Increasing fan %d "
366 "clock divider from %u to %u\n",
367 i, div_from_reg(data->fan_div[i]),
368 div_from_reg(data->fan_div[i] + 1));
369 data->fan_div[i]++;
370 w83627ehf_write_fan_div(client, i);
371 /* Preserve min limit if possible */
372 if (data->fan_min[i] >= 2
373 && data->fan_min[i] != 255)
374 w83627ehf_write_value(client,
375 W83627EHF_REG_FAN_MIN[i],
376 (data->fan_min[i] /= 2));
380 /* Measured temperatures and limits */
381 data->temp1 = w83627ehf_read_value(client,
382 W83627EHF_REG_TEMP1);
383 data->temp1_max = w83627ehf_read_value(client,
384 W83627EHF_REG_TEMP1_OVER);
385 data->temp1_max_hyst = w83627ehf_read_value(client,
386 W83627EHF_REG_TEMP1_HYST);
387 for (i = 0; i < 2; i++) {
388 data->temp[i] = w83627ehf_read_value(client,
389 W83627EHF_REG_TEMP[i]);
390 data->temp_max[i] = w83627ehf_read_value(client,
391 W83627EHF_REG_TEMP_OVER[i]);
392 data->temp_max_hyst[i] = w83627ehf_read_value(client,
393 W83627EHF_REG_TEMP_HYST[i]);
396 data->last_updated = jiffies;
397 data->valid = 1;
400 up(&data->update_lock);
401 return data;
405 * Sysfs callback functions
408 #define show_fan_reg(reg) \
409 static ssize_t \
410 show_##reg(struct device *dev, char *buf, int nr) \
412 struct w83627ehf_data *data = w83627ehf_update_device(dev); \
413 return sprintf(buf, "%d\n", \
414 fan_from_reg(data->reg[nr], \
415 div_from_reg(data->fan_div[nr]))); \
417 show_fan_reg(fan);
418 show_fan_reg(fan_min);
420 static ssize_t
421 show_fan_div(struct device *dev, char *buf, int nr)
423 struct w83627ehf_data *data = w83627ehf_update_device(dev);
424 return sprintf(buf, "%u\n",
425 div_from_reg(data->fan_div[nr]));
428 static ssize_t
429 store_fan_min(struct device *dev, const char *buf, size_t count, int nr)
431 struct i2c_client *client = to_i2c_client(dev);
432 struct w83627ehf_data *data = i2c_get_clientdata(client);
433 unsigned int val = simple_strtoul(buf, NULL, 10);
434 unsigned int reg;
435 u8 new_div;
437 down(&data->update_lock);
438 if (!val) {
439 /* No min limit, alarm disabled */
440 data->fan_min[nr] = 255;
441 new_div = data->fan_div[nr]; /* No change */
442 dev_info(dev, "fan%u low limit and alarm disabled\n", nr + 1);
443 } else if ((reg = 1350000U / val) >= 128 * 255) {
444 /* Speed below this value cannot possibly be represented,
445 even with the highest divider (128) */
446 data->fan_min[nr] = 254;
447 new_div = 7; /* 128 == (1 << 7) */
448 dev_warn(dev, "fan%u low limit %u below minimum %u, set to "
449 "minimum\n", nr + 1, val, fan_from_reg(254, 128));
450 } else if (!reg) {
451 /* Speed above this value cannot possibly be represented,
452 even with the lowest divider (1) */
453 data->fan_min[nr] = 1;
454 new_div = 0; /* 1 == (1 << 0) */
455 dev_warn(dev, "fan%u low limit %u above maximum %u, set to "
456 "maximum\n", nr + 1, val, fan_from_reg(1, 1));
457 } else {
458 /* Automatically pick the best divider, i.e. the one such
459 that the min limit will correspond to a register value
460 in the 96..192 range */
461 new_div = 0;
462 while (reg > 192 && new_div < 7) {
463 reg >>= 1;
464 new_div++;
466 data->fan_min[nr] = reg;
469 /* Write both the fan clock divider (if it changed) and the new
470 fan min (unconditionally) */
471 if (new_div != data->fan_div[nr]) {
472 if (new_div > data->fan_div[nr])
473 data->fan[nr] >>= (data->fan_div[nr] - new_div);
474 else
475 data->fan[nr] <<= (new_div - data->fan_div[nr]);
477 dev_dbg(dev, "fan%u clock divider changed from %u to %u\n",
478 nr + 1, div_from_reg(data->fan_div[nr]),
479 div_from_reg(new_div));
480 data->fan_div[nr] = new_div;
481 w83627ehf_write_fan_div(client, nr);
483 w83627ehf_write_value(client, W83627EHF_REG_FAN_MIN[nr],
484 data->fan_min[nr]);
485 up(&data->update_lock);
487 return count;
490 #define sysfs_fan_offset(offset) \
491 static ssize_t \
492 show_reg_fan_##offset(struct device *dev, struct device_attribute *attr, \
493 char *buf) \
495 return show_fan(dev, buf, offset-1); \
497 static DEVICE_ATTR(fan##offset##_input, S_IRUGO, \
498 show_reg_fan_##offset, NULL);
500 #define sysfs_fan_min_offset(offset) \
501 static ssize_t \
502 show_reg_fan##offset##_min(struct device *dev, struct device_attribute *attr, \
503 char *buf) \
505 return show_fan_min(dev, buf, offset-1); \
507 static ssize_t \
508 store_reg_fan##offset##_min(struct device *dev, struct device_attribute *attr, \
509 const char *buf, size_t count) \
511 return store_fan_min(dev, buf, count, offset-1); \
513 static DEVICE_ATTR(fan##offset##_min, S_IRUGO | S_IWUSR, \
514 show_reg_fan##offset##_min, \
515 store_reg_fan##offset##_min);
517 #define sysfs_fan_div_offset(offset) \
518 static ssize_t \
519 show_reg_fan##offset##_div(struct device *dev, struct device_attribute *attr, \
520 char *buf) \
522 return show_fan_div(dev, buf, offset - 1); \
524 static DEVICE_ATTR(fan##offset##_div, S_IRUGO, \
525 show_reg_fan##offset##_div, NULL);
527 sysfs_fan_offset(1);
528 sysfs_fan_min_offset(1);
529 sysfs_fan_div_offset(1);
530 sysfs_fan_offset(2);
531 sysfs_fan_min_offset(2);
532 sysfs_fan_div_offset(2);
533 sysfs_fan_offset(3);
534 sysfs_fan_min_offset(3);
535 sysfs_fan_div_offset(3);
536 sysfs_fan_offset(4);
537 sysfs_fan_min_offset(4);
538 sysfs_fan_div_offset(4);
539 sysfs_fan_offset(5);
540 sysfs_fan_min_offset(5);
541 sysfs_fan_div_offset(5);
543 #define show_temp1_reg(reg) \
544 static ssize_t \
545 show_##reg(struct device *dev, struct device_attribute *attr, \
546 char *buf) \
548 struct w83627ehf_data *data = w83627ehf_update_device(dev); \
549 return sprintf(buf, "%d\n", temp1_from_reg(data->reg)); \
551 show_temp1_reg(temp1);
552 show_temp1_reg(temp1_max);
553 show_temp1_reg(temp1_max_hyst);
555 #define store_temp1_reg(REG, reg) \
556 static ssize_t \
557 store_temp1_##reg(struct device *dev, struct device_attribute *attr, \
558 const char *buf, size_t count) \
560 struct i2c_client *client = to_i2c_client(dev); \
561 struct w83627ehf_data *data = i2c_get_clientdata(client); \
562 u32 val = simple_strtoul(buf, NULL, 10); \
564 down(&data->update_lock); \
565 data->temp1_##reg = temp1_to_reg(val); \
566 w83627ehf_write_value(client, W83627EHF_REG_TEMP1_##REG, \
567 data->temp1_##reg); \
568 up(&data->update_lock); \
569 return count; \
571 store_temp1_reg(OVER, max);
572 store_temp1_reg(HYST, max_hyst);
574 static DEVICE_ATTR(temp1_input, S_IRUGO, show_temp1, NULL);
575 static DEVICE_ATTR(temp1_max, S_IRUGO| S_IWUSR,
576 show_temp1_max, store_temp1_max);
577 static DEVICE_ATTR(temp1_max_hyst, S_IRUGO| S_IWUSR,
578 show_temp1_max_hyst, store_temp1_max_hyst);
580 #define show_temp_reg(reg) \
581 static ssize_t \
582 show_##reg (struct device *dev, char *buf, int nr) \
584 struct w83627ehf_data *data = w83627ehf_update_device(dev); \
585 return sprintf(buf, "%d\n", \
586 LM75_TEMP_FROM_REG(data->reg[nr])); \
588 show_temp_reg(temp);
589 show_temp_reg(temp_max);
590 show_temp_reg(temp_max_hyst);
592 #define store_temp_reg(REG, reg) \
593 static ssize_t \
594 store_##reg (struct device *dev, const char *buf, size_t count, int nr) \
596 struct i2c_client *client = to_i2c_client(dev); \
597 struct w83627ehf_data *data = i2c_get_clientdata(client); \
598 u32 val = simple_strtoul(buf, NULL, 10); \
600 down(&data->update_lock); \
601 data->reg[nr] = LM75_TEMP_TO_REG(val); \
602 w83627ehf_write_value(client, W83627EHF_REG_TEMP_##REG[nr], \
603 data->reg[nr]); \
604 up(&data->update_lock); \
605 return count; \
607 store_temp_reg(OVER, temp_max);
608 store_temp_reg(HYST, temp_max_hyst);
610 #define sysfs_temp_offset(offset) \
611 static ssize_t \
612 show_reg_temp##offset (struct device *dev, struct device_attribute *attr, \
613 char *buf) \
615 return show_temp(dev, buf, offset - 2); \
617 static DEVICE_ATTR(temp##offset##_input, S_IRUGO, \
618 show_reg_temp##offset, NULL);
620 #define sysfs_temp_reg_offset(reg, offset) \
621 static ssize_t \
622 show_reg_temp##offset##_##reg(struct device *dev, struct device_attribute *attr, \
623 char *buf) \
625 return show_temp_##reg(dev, buf, offset - 2); \
627 static ssize_t \
628 store_reg_temp##offset##_##reg(struct device *dev, struct device_attribute *attr, \
629 const char *buf, size_t count) \
631 return store_temp_##reg(dev, buf, count, offset - 2); \
633 static DEVICE_ATTR(temp##offset##_##reg, S_IRUGO| S_IWUSR, \
634 show_reg_temp##offset##_##reg, \
635 store_reg_temp##offset##_##reg);
637 sysfs_temp_offset(2);
638 sysfs_temp_reg_offset(max, 2);
639 sysfs_temp_reg_offset(max_hyst, 2);
640 sysfs_temp_offset(3);
641 sysfs_temp_reg_offset(max, 3);
642 sysfs_temp_reg_offset(max_hyst, 3);
645 * Driver and client management
648 static struct i2c_driver w83627ehf_driver;
650 static void w83627ehf_init_client(struct i2c_client *client)
652 int i;
653 u8 tmp;
655 /* Start monitoring is needed */
656 tmp = w83627ehf_read_value(client, W83627EHF_REG_CONFIG);
657 if (!(tmp & 0x01))
658 w83627ehf_write_value(client, W83627EHF_REG_CONFIG,
659 tmp | 0x01);
661 /* Enable temp2 and temp3 if needed */
662 for (i = 0; i < 2; i++) {
663 tmp = w83627ehf_read_value(client,
664 W83627EHF_REG_TEMP_CONFIG[i]);
665 if (tmp & 0x01)
666 w83627ehf_write_value(client,
667 W83627EHF_REG_TEMP_CONFIG[i],
668 tmp & 0xfe);
672 static int w83627ehf_detect(struct i2c_adapter *adapter)
674 struct i2c_client *client;
675 struct w83627ehf_data *data;
676 int i, err = 0;
678 if (!request_region(address + REGION_OFFSET, REGION_LENGTH,
679 w83627ehf_driver.name)) {
680 err = -EBUSY;
681 goto exit;
684 if (!(data = kzalloc(sizeof(struct w83627ehf_data), GFP_KERNEL))) {
685 err = -ENOMEM;
686 goto exit_release;
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;
697 strlcpy(client->name, "w83627ehf", I2C_NAME_SIZE);
698 data->valid = 0;
699 init_MUTEX(&data->update_lock);
701 /* Tell the i2c layer a new client has arrived */
702 if ((err = i2c_attach_client(client)))
703 goto exit_free;
705 /* Initialize the chip */
706 w83627ehf_init_client(client);
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]);
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);
722 /* Register sysfs hooks */
723 data->class_dev = hwmon_device_register(&client->dev);
724 if (IS_ERR(data->class_dev)) {
725 err = PTR_ERR(data->class_dev);
726 goto exit_detach;
729 device_create_file(&client->dev, &dev_attr_fan1_input);
730 device_create_file(&client->dev, &dev_attr_fan1_min);
731 device_create_file(&client->dev, &dev_attr_fan1_div);
732 device_create_file(&client->dev, &dev_attr_fan2_input);
733 device_create_file(&client->dev, &dev_attr_fan2_min);
734 device_create_file(&client->dev, &dev_attr_fan2_div);
735 device_create_file(&client->dev, &dev_attr_fan3_input);
736 device_create_file(&client->dev, &dev_attr_fan3_min);
737 device_create_file(&client->dev, &dev_attr_fan3_div);
739 if (data->has_fan & (1 << 3)) {
740 device_create_file(&client->dev, &dev_attr_fan4_input);
741 device_create_file(&client->dev, &dev_attr_fan4_min);
742 device_create_file(&client->dev, &dev_attr_fan4_div);
744 if (data->has_fan & (1 << 4)) {
745 device_create_file(&client->dev, &dev_attr_fan5_input);
746 device_create_file(&client->dev, &dev_attr_fan5_min);
747 device_create_file(&client->dev, &dev_attr_fan5_div);
750 device_create_file(&client->dev, &dev_attr_temp1_input);
751 device_create_file(&client->dev, &dev_attr_temp1_max);
752 device_create_file(&client->dev, &dev_attr_temp1_max_hyst);
753 device_create_file(&client->dev, &dev_attr_temp2_input);
754 device_create_file(&client->dev, &dev_attr_temp2_max);
755 device_create_file(&client->dev, &dev_attr_temp2_max_hyst);
756 device_create_file(&client->dev, &dev_attr_temp3_input);
757 device_create_file(&client->dev, &dev_attr_temp3_max);
758 device_create_file(&client->dev, &dev_attr_temp3_max_hyst);
760 return 0;
762 exit_detach:
763 i2c_detach_client(client);
764 exit_free:
765 kfree(data);
766 exit_release:
767 release_region(address + REGION_OFFSET, REGION_LENGTH);
768 exit:
769 return err;
772 static int w83627ehf_detach_client(struct i2c_client *client)
774 struct w83627ehf_data *data = i2c_get_clientdata(client);
775 int err;
777 hwmon_device_unregister(data->class_dev);
779 if ((err = i2c_detach_client(client)))
780 return err;
781 release_region(client->addr + REGION_OFFSET, REGION_LENGTH);
782 kfree(data);
784 return 0;
787 static struct i2c_driver w83627ehf_driver = {
788 .owner = THIS_MODULE,
789 .name = "w83627ehf",
790 .attach_adapter = w83627ehf_detect,
791 .detach_client = w83627ehf_detach_client,
794 static int __init w83627ehf_find(int sioaddr, unsigned short *addr)
796 u16 val;
798 REG = sioaddr;
799 VAL = sioaddr + 1;
800 superio_enter();
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;
809 superio_select(W83627EHF_LD_HWM);
810 val = (superio_inb(SIO_REG_ADDR) << 8)
811 | superio_inb(SIO_REG_ADDR + 1);
812 *addr = val & REGION_ALIGNMENT;
813 if (*addr == 0) {
814 superio_exit();
815 return -ENODEV;
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);
823 superio_exit();
824 return 0;
827 static int __init sensors_w83627ehf_init(void)
829 if (w83627ehf_find(0x2e, &address)
830 && w83627ehf_find(0x4e, &address))
831 return -ENODEV;
833 return i2c_isa_add_driver(&w83627ehf_driver);
836 static void __exit sensors_w83627ehf_exit(void)
838 i2c_isa_del_driver(&w83627ehf_driver);
841 MODULE_AUTHOR("Jean Delvare <khali@linux-fr.org>");
842 MODULE_DESCRIPTION("W83627EHF driver");
843 MODULE_LICENSE("GPL");
845 module_init(sensors_w83627ehf_init);
846 module_exit(sensors_w83627ehf_exit);