Linux v2.6.13
[linux-2.6/next.git] / drivers / hwmon / w83627ehf.c
blob8a40b6976e1ad25838a37c70c49e4235a1ea3281
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 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.
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.
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.
27 Supports the following chips:
29 Chip #vin #fan #pwm #temp chip_id man_id
30 w83627ehf - 5 - 3 0x88 0x5ca3
32 This is a preliminary version of the driver, only supporting the
33 fan and temperature inputs. The chip does much more than that.
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"
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 };
49 /* Insmod parameters */
50 SENSORS_INSMOD_1(w83627ehf);
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_LENGTH 8
109 #define ADDR_REG_OFFSET 5
110 #define DATA_REG_OFFSET 6
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
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 };
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 };
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
136 * Conversions
139 static inline unsigned int
140 fan_from_reg(u8 reg, unsigned int div)
142 if (reg == 0 || reg == 255)
143 return 0;
144 return 1350000U / (reg * div);
147 static inline unsigned int
148 div_from_reg(u8 reg)
150 return 1 << reg;
153 static inline int
154 temp1_from_reg(s8 reg)
156 return reg * 1000;
159 static inline s8
160 temp1_to_reg(int temp)
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;
172 * Data structures and manipulation thereof
175 struct w83627ehf_data {
176 struct i2c_client client;
177 struct semaphore lock;
179 struct semaphore update_lock;
180 char valid; /* !=0 if following fields are valid */
181 unsigned long last_updated; /* In jiffies */
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];
196 static inline int is_word_sized(u16 reg)
198 return (((reg & 0xff00) == 0x100
199 || (reg & 0xff00) == 0x200)
200 && ((reg & 0x00ff) == 0x50
201 || (reg & 0x00ff) == 0x53
202 || (reg & 0x00ff) == 0x55));
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)
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);
217 static inline void w83627ehf_reset_bank(struct i2c_client *client, u16 reg)
219 if (reg & 0xff00) {
220 outb_p(W83627EHF_REG_BANK, client->addr + ADDR_REG_OFFSET);
221 outb_p(0, client->addr + DATA_REG_OFFSET);
225 static u16 w83627ehf_read_value(struct i2c_client *client, u16 reg)
227 struct w83627ehf_data *data = i2c_get_clientdata(client);
228 int res, word_sized = is_word_sized(reg);
230 down(&data->lock);
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);
240 w83627ehf_reset_bank(client, reg);
242 up(&data->lock);
244 return res;
247 static int w83627ehf_write_value(struct i2c_client *client, u16 reg, u16 value)
249 struct w83627ehf_data *data = i2c_get_clientdata(client);
250 int word_sized = is_word_sized(reg);
252 down(&data->lock);
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);
261 outb_p(value & 0xff, client->addr + DATA_REG_OFFSET);
262 w83627ehf_reset_bank(client, reg);
264 up(&data->lock);
265 return 0;
268 /* This function assumes that the caller holds data->update_lock */
269 static void w83627ehf_write_fan_div(struct i2c_client *client, int nr)
271 struct w83627ehf_data *data = i2c_get_clientdata(client);
272 u8 reg;
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;
316 static struct w83627ehf_data *w83627ehf_update_device(struct device *dev)
318 struct i2c_client *client = to_i2c_client(dev);
319 struct w83627ehf_data *data = i2c_get_clientdata(client);
320 int i;
322 down(&data->update_lock);
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);
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;
347 /* Measured fan speeds and limits */
348 for (i = 0; i < 5; i++) {
349 if (!(data->has_fan & (1 << i)))
350 continue;
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]);
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));
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]);
393 data->last_updated = jiffies;
394 data->valid = 1;
397 up(&data->update_lock);
398 return data;
402 * Sysfs callback functions
405 #define show_fan_reg(reg) \
406 static ssize_t \
407 show_##reg(struct device *dev, char *buf, int nr) \
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]))); \
414 show_fan_reg(fan);
415 show_fan_reg(fan_min);
417 static ssize_t
418 show_fan_div(struct device *dev, char *buf, int nr)
420 struct w83627ehf_data *data = w83627ehf_update_device(dev);
421 return sprintf(buf, "%u\n",
422 div_from_reg(data->fan_div[nr]));
425 static ssize_t
426 store_fan_min(struct device *dev, const char *buf, size_t count, int nr)
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;
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++;
463 data->fan_min[nr] = reg;
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]);
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);
480 w83627ehf_write_value(client, W83627EHF_REG_FAN_MIN[nr],
481 data->fan_min[nr]);
482 up(&data->update_lock);
484 return count;
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) \
492 return show_fan(dev, buf, offset-1); \
494 static DEVICE_ATTR(fan##offset##_input, S_IRUGO, \
495 show_reg_fan_##offset, NULL);
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) \
502 return show_fan_min(dev, buf, offset-1); \
504 static ssize_t \
505 store_reg_fan##offset##_min(struct device *dev, struct device_attribute *attr, \
506 const char *buf, size_t count) \
508 return store_fan_min(dev, buf, count, offset-1); \
510 static DEVICE_ATTR(fan##offset##_min, S_IRUGO | S_IWUSR, \
511 show_reg_fan##offset##_min, \
512 store_reg_fan##offset##_min);
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) \
519 return show_fan_div(dev, buf, offset - 1); \
521 static DEVICE_ATTR(fan##offset##_div, S_IRUGO, \
522 show_reg_fan##offset##_div, NULL);
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);
540 #define show_temp1_reg(reg) \
541 static ssize_t \
542 show_##reg(struct device *dev, struct device_attribute *attr, \
543 char *buf) \
545 struct w83627ehf_data *data = w83627ehf_update_device(dev); \
546 return sprintf(buf, "%d\n", temp1_from_reg(data->reg)); \
548 show_temp1_reg(temp1);
549 show_temp1_reg(temp1_max);
550 show_temp1_reg(temp1_max_hyst);
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) \
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); \
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; \
568 store_temp1_reg(OVER, max);
569 store_temp1_reg(HYST, max_hyst);
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);
577 #define show_temp_reg(reg) \
578 static ssize_t \
579 show_##reg (struct device *dev, char *buf, int nr) \
581 struct w83627ehf_data *data = w83627ehf_update_device(dev); \
582 return sprintf(buf, "%d\n", \
583 LM75_TEMP_FROM_REG(data->reg[nr])); \
585 show_temp_reg(temp);
586 show_temp_reg(temp_max);
587 show_temp_reg(temp_max_hyst);
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) \
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); \
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; \
604 store_temp_reg(OVER, temp_max);
605 store_temp_reg(HYST, temp_max_hyst);
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) \
612 return show_temp(dev, buf, offset - 2); \
614 static DEVICE_ATTR(temp##offset##_input, S_IRUGO, \
615 show_reg_temp##offset, NULL);
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) \
622 return show_temp_##reg(dev, buf, offset - 2); \
624 static ssize_t \
625 store_reg_temp##offset##_##reg(struct device *dev, struct device_attribute *attr, \
626 const char *buf, size_t count) \
628 return store_temp_##reg(dev, buf, count, offset - 2); \
630 static DEVICE_ATTR(temp##offset##_##reg, S_IRUGO| S_IWUSR, \
631 show_reg_temp##offset##_##reg, \
632 store_reg_temp##offset##_##reg);
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);
642 * Driver and client management
645 static struct i2c_driver w83627ehf_driver;
647 static void w83627ehf_init_client(struct i2c_client *client)
649 int i;
650 u8 tmp;
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);
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);
669 static int w83627ehf_detect(struct i2c_adapter *adapter, int address, int kind)
671 struct i2c_client *client;
672 struct w83627ehf_data *data;
673 int i, err = 0;
675 if (!i2c_is_isa_adapter(adapter))
676 return 0;
678 if (!request_region(address, REGION_LENGTH, w83627ehf_driver.name)) {
679 err = -EBUSY;
680 goto exit;
683 if (!(data = kmalloc(sizeof(struct w83627ehf_data), GFP_KERNEL))) {
684 err = -ENOMEM;
685 goto exit_release;
687 memset(data, 0, sizeof(struct w83627ehf_data));
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 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);
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);
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);
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);
754 return 0;
756 exit_free:
757 kfree(data);
758 exit_release:
759 release_region(address, REGION_LENGTH);
760 exit:
761 return err;
764 static int w83627ehf_attach_adapter(struct i2c_adapter *adapter)
766 if (!(adapter->class & I2C_CLASS_HWMON))
767 return 0;
768 return i2c_detect(adapter, &addr_data, w83627ehf_detect);
771 static int w83627ehf_detach_client(struct i2c_client *client)
773 int err;
775 if ((err = i2c_detach_client(client))) {
776 dev_err(&client->dev, "Client deregistration failed, "
777 "client not detached.\n");
778 return err;
780 release_region(client->addr, REGION_LENGTH);
781 kfree(i2c_get_clientdata(client));
783 return 0;
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,
794 static int __init w83627ehf_find(int sioaddr, int *address)
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 *address = val & ~(REGION_LENGTH - 1);
813 if (*address == 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, &normal_isa[0])
830 && w83627ehf_find(0x4e, &normal_isa[0]))
831 return -ENODEV;
833 return i2c_add_driver(&w83627ehf_driver);
836 static void __exit sensors_w83627ehf_exit(void)
838 i2c_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);