VServer 1.9.2 (patch-2.6.8.1-vs1.9.2.diff)
[linux-2.6.git] / drivers / i2c / chips / w83627hf.c
1 /*
2     w83627hf.c - Part of lm_sensors, Linux kernel modules for hardware
3                 monitoring
4     Copyright (c) 1998 - 2003  Frodo Looijaard <frodol@dds.nl>,
5     Philip Edelbrock <phil@netroedge.com>,
6     and Mark Studebaker <mdsxyz123@yahoo.com>
7     Ported to 2.6 by Bernhard C. Schrenk <clemy@clemy.org>
8
9     This program is free software; you can redistribute it and/or modify
10     it under the terms of the GNU General Public License as published by
11     the Free Software Foundation; either version 2 of the License, or
12     (at your option) any later version.
13
14     This program is distributed in the hope that it will be useful,
15     but WITHOUT ANY WARRANTY; without even the implied warranty of
16     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17     GNU General Public License for more details.
18
19     You should have received a copy of the GNU General Public License
20     along with this program; if not, write to the Free Software
21     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22 */
23
24 /*
25     Supports following chips:
26
27     Chip        #vin    #fanin  #pwm    #temp   wchipid vendid  i2c     ISA
28     w83627hf    9       3       2       3       0x20    0x5ca3  no      yes(LPC)
29     w83627thf   7       3       3       3       0x90    0x5ca3  no      yes(LPC)
30     w83637hf    7       3       3       3       0x80    0x5ca3  no      yes(LPC)
31     w83697hf    8       2       2       2       0x60    0x5ca3  no      yes(LPC)
32
33     For other winbond chips, and for i2c support in the above chips,
34     use w83781d.c.
35
36     Note: automatic ("cruise") fan control for 697, 637 & 627thf not
37     supported yet.
38 */
39
40 #include <linux/module.h>
41 #include <linux/init.h>
42 #include <linux/slab.h>
43 #include <linux/i2c.h>
44 #include <linux/i2c-sensor.h>
45 #include <linux/i2c-vid.h>
46 #include <asm/io.h>
47 #include "lm75.h"
48
49 static int force_addr;
50 MODULE_PARM(force_addr, "i");
51 MODULE_PARM_DESC(force_addr,
52                  "Initialize the base address of the sensors");
53 static int force_i2c = 0x1f;
54 MODULE_PARM(force_i2c, "i");
55 MODULE_PARM_DESC(force_i2c,
56                  "Initialize the i2c address of the sensors");
57
58 /* Addresses to scan */
59 static unsigned short normal_i2c[] = { I2C_CLIENT_END };
60 static unsigned short normal_i2c_range[] = { I2C_CLIENT_END };
61 static unsigned int normal_isa[] = { 0, I2C_CLIENT_ISA_END };
62 static unsigned int normal_isa_range[] = { I2C_CLIENT_ISA_END };
63
64 /* Insmod parameters */
65 SENSORS_INSMOD_4(w83627hf, w83627thf, w83697hf, w83637hf);
66
67 static int init = 1;
68 MODULE_PARM(init, "i");
69 MODULE_PARM_DESC(init, "Set to zero to bypass chip initialization");
70
71 /* modified from kernel/include/traps.c */
72 #define REG     0x2e    /* The register to read/write */
73 #define DEV     0x07    /* Register: Logical device select */
74 #define VAL     0x2f    /* The value to read/write */
75
76 /* logical device numbers for superio_select (below) */
77 #define W83627HF_LD_FDC         0x00
78 #define W83627HF_LD_PRT         0x01
79 #define W83627HF_LD_UART1       0x02
80 #define W83627HF_LD_UART2       0x03
81 #define W83627HF_LD_KBC         0x05
82 #define W83627HF_LD_CIR         0x06 /* w83627hf only */
83 #define W83627HF_LD_GAME        0x07
84 #define W83627HF_LD_MIDI        0x07
85 #define W83627HF_LD_GPIO1       0x07
86 #define W83627HF_LD_GPIO5       0x07 /* w83627thf only */
87 #define W83627HF_LD_GPIO2       0x08
88 #define W83627HF_LD_GPIO3       0x09
89 #define W83627HF_LD_GPIO4       0x09 /* w83627thf only */
90 #define W83627HF_LD_ACPI        0x0a
91 #define W83627HF_LD_HWM         0x0b
92
93 #define DEVID   0x20    /* Register: Device ID */
94
95 #define W83627THF_GPIO5_IOSR    0xf3 /* w83627thf only */
96 #define W83627THF_GPIO5_DR      0xf4 /* w83627thf only */
97 #define W83627THF_GPIO5_INVR    0xf5 /* w83627thf only */
98
99 static inline void
100 superio_outb(int reg, int val)
101 {
102         outb(reg, REG);
103         outb(val, VAL);
104 }
105
106 static inline int
107 superio_inb(int reg)
108 {
109         outb(reg, REG);
110         return inb(VAL);
111 }
112
113 static inline void
114 superio_select(int ld)
115 {
116         outb(DEV, REG);
117         outb(ld, VAL);
118 }
119
120 static inline void
121 superio_enter(void)
122 {
123         outb(0x87, REG);
124         outb(0x87, REG);
125 }
126
127 static inline void
128 superio_exit(void)
129 {
130         outb(0xAA, REG);
131 }
132
133 #define W627_DEVID 0x52
134 #define W627THF_DEVID 0x82
135 #define W697_DEVID 0x60
136 #define W637_DEVID 0x70
137 #define WINB_ACT_REG 0x30
138 #define WINB_BASE_REG 0x60
139 /* Constants specified below */
140
141 /* Length of ISA address segment */
142 #define WINB_EXTENT 8
143
144 /* Where are the ISA address/data registers relative to the base address */
145 #define W83781D_ADDR_REG_OFFSET 5
146 #define W83781D_DATA_REG_OFFSET 6
147
148 /* The W83781D registers */
149 /* The W83782D registers for nr=7,8 are in bank 5 */
150 #define W83781D_REG_IN_MAX(nr) ((nr < 7) ? (0x2b + (nr) * 2) : \
151                                            (0x554 + (((nr) - 7) * 2)))
152 #define W83781D_REG_IN_MIN(nr) ((nr < 7) ? (0x2c + (nr) * 2) : \
153                                            (0x555 + (((nr) - 7) * 2)))
154 #define W83781D_REG_IN(nr)     ((nr < 7) ? (0x20 + (nr)) : \
155                                            (0x550 + (nr) - 7))
156
157 #define W83781D_REG_FAN_MIN(nr) (0x3a + (nr))
158 #define W83781D_REG_FAN(nr) (0x27 + (nr))
159
160 #define W83781D_REG_TEMP2_CONFIG 0x152
161 #define W83781D_REG_TEMP3_CONFIG 0x252
162 #define W83781D_REG_TEMP(nr)            ((nr == 3) ? (0x0250) : \
163                                         ((nr == 2) ? (0x0150) : \
164                                                      (0x27)))
165 #define W83781D_REG_TEMP_HYST(nr)       ((nr == 3) ? (0x253) : \
166                                         ((nr == 2) ? (0x153) : \
167                                                      (0x3A)))
168 #define W83781D_REG_TEMP_OVER(nr)       ((nr == 3) ? (0x255) : \
169                                         ((nr == 2) ? (0x155) : \
170                                                      (0x39)))
171
172 #define W83781D_REG_BANK 0x4E
173
174 #define W83781D_REG_CONFIG 0x40
175 #define W83781D_REG_ALARM1 0x41
176 #define W83781D_REG_ALARM2 0x42
177 #define W83781D_REG_ALARM3 0x450
178
179 #define W83781D_REG_IRQ 0x4C
180 #define W83781D_REG_BEEP_CONFIG 0x4D
181 #define W83781D_REG_BEEP_INTS1 0x56
182 #define W83781D_REG_BEEP_INTS2 0x57
183 #define W83781D_REG_BEEP_INTS3 0x453
184
185 #define W83781D_REG_VID_FANDIV 0x47
186
187 #define W83781D_REG_CHIPID 0x49
188 #define W83781D_REG_WCHIPID 0x58
189 #define W83781D_REG_CHIPMAN 0x4F
190 #define W83781D_REG_PIN 0x4B
191
192 #define W83781D_REG_VBAT 0x5D
193
194 #define W83627HF_REG_PWM1 0x5A
195 #define W83627HF_REG_PWM2 0x5B
196 #define W83627HF_REG_PWMCLK12 0x5C
197
198 #define W83627THF_REG_PWM1              0x01    /* 697HF and 637HF too */
199 #define W83627THF_REG_PWM2              0x03    /* 697HF and 637HF too */
200 #define W83627THF_REG_PWM3              0x11    /* 637HF too */
201
202 #define W83627THF_REG_VRM_OVT_CFG       0x18    /* 637HF too */
203
204 static const u8 regpwm_627hf[] = { W83627HF_REG_PWM1, W83627HF_REG_PWM2 };
205 static const u8 regpwm[] = { W83627THF_REG_PWM1, W83627THF_REG_PWM2,
206                              W83627THF_REG_PWM3 };
207 #define W836X7HF_REG_PWM(type, nr) (((type) == w83627hf) ? \
208                                      regpwm_627hf[(nr) - 1] : regpwm[(nr) - 1])
209
210 #define W83781D_REG_I2C_ADDR 0x48
211 #define W83781D_REG_I2C_SUBADDR 0x4A
212
213 /* Sensor selection */
214 #define W83781D_REG_SCFG1 0x5D
215 static const u8 BIT_SCFG1[] = { 0x02, 0x04, 0x08 };
216 #define W83781D_REG_SCFG2 0x59
217 static const u8 BIT_SCFG2[] = { 0x10, 0x20, 0x40 };
218 #define W83781D_DEFAULT_BETA 3435
219
220 /* Conversions. Limit checking is only done on the TO_REG
221    variants. Note that you should be a bit careful with which arguments
222    these macros are called: arguments may be evaluated more than once.
223    Fixing this is just not worth it. */
224 #define IN_TO_REG(val)  (SENSORS_LIMIT((((val) + 8)/16),0,255))
225 #define IN_FROM_REG(val) ((val) * 16)
226
227 static inline u8 FAN_TO_REG(long rpm, int div)
228 {
229         if (rpm == 0)
230                 return 255;
231         rpm = SENSORS_LIMIT(rpm, 1, 1000000);
232         return SENSORS_LIMIT((1350000 + rpm * div / 2) / (rpm * div), 1,
233                              254);
234 }
235
236 #define TEMP_MIN (-128000)
237 #define TEMP_MAX ( 127000)
238
239 /* TEMP: 0.001C/bit (-128C to +127C)
240    REG: 1C/bit, two's complement */
241 static u8 TEMP_TO_REG(int temp)
242 {
243         int ntemp = SENSORS_LIMIT(temp, TEMP_MIN, TEMP_MAX);
244         ntemp += (ntemp<0 ? -500 : 500);
245         return (u8)(ntemp / 1000);
246 }
247
248 static int TEMP_FROM_REG(u8 reg)
249 {
250         return (s8)reg * 1000;
251 }
252
253 #define FAN_FROM_REG(val,div) ((val)==0?-1:(val)==255?0:1350000/((val)*(div)))
254
255 #define PWM_TO_REG(val) (SENSORS_LIMIT((val),0,255))
256
257 #define BEEP_MASK_FROM_REG(val)          (val)
258 #define BEEP_MASK_TO_REG(val)           ((val) & 0xffffff)
259 #define BEEP_ENABLE_TO_REG(val)         ((val)?1:0)
260 #define BEEP_ENABLE_FROM_REG(val)       ((val)?1:0)
261
262 #define DIV_FROM_REG(val) (1 << (val))
263
264 static inline u8 DIV_TO_REG(long val)
265 {
266         int i;
267         val = SENSORS_LIMIT(val, 1, 128) >> 1;
268         for (i = 0; i < 6; i++) {
269                 if (val == 0)
270                         break;
271                 val >>= 1;
272         }
273         return ((u8) i);
274 }
275
276 /* For each registered chip, we need to keep some data in memory. That
277    data is pointed to by w83627hf_list[NR]->data. The structure itself is
278    dynamically allocated, at the same time when a new client is allocated. */
279 struct w83627hf_data {
280         struct i2c_client client;
281         struct semaphore lock;
282         enum chips type;
283
284         struct semaphore update_lock;
285         char valid;             /* !=0 if following fields are valid */
286         unsigned long last_updated;     /* In jiffies */
287
288         struct i2c_client *lm75;        /* for secondary I2C addresses */
289         /* pointer to array of 2 subclients */
290
291         u8 in[9];               /* Register value */
292         u8 in_max[9];           /* Register value */
293         u8 in_min[9];           /* Register value */
294         u8 fan[3];              /* Register value */
295         u8 fan_min[3];          /* Register value */
296         u8 temp;
297         u8 temp_max;            /* Register value */
298         u8 temp_max_hyst;       /* Register value */
299         u16 temp_add[2];        /* Register value */
300         u16 temp_max_add[2];    /* Register value */
301         u16 temp_max_hyst_add[2]; /* Register value */
302         u8 fan_div[3];          /* Register encoding, shifted right */
303         u8 vid;                 /* Register encoding, combined */
304         u32 alarms;             /* Register encoding, combined */
305         u32 beep_mask;          /* Register encoding, combined */
306         u8 beep_enable;         /* Boolean */
307         u8 pwm[3];              /* Register value */
308         u8 pwmenable[3];        /* bool */
309         u16 sens[3];            /* 782D/783S only.
310                                    1 = pentium diode; 2 = 3904 diode;
311                                    3000-5000 = thermistor beta.
312                                    Default = 3435.
313                                    Other Betas unimplemented */
314         u8 vrm;
315         u8 vrm_ovt;             /* Register value, 627thf & 637hf only */
316 };
317
318
319 static int w83627hf_attach_adapter(struct i2c_adapter *adapter);
320 static int w83627hf_detect(struct i2c_adapter *adapter, int address,
321                           int kind);
322 static int w83627hf_detach_client(struct i2c_client *client);
323
324 static int w83627hf_read_value(struct i2c_client *client, u16 register);
325 static int w83627hf_write_value(struct i2c_client *client, u16 register,
326                                u16 value);
327 static struct w83627hf_data *w83627hf_update_device(struct device *dev);
328 static void w83627hf_init_client(struct i2c_client *client);
329
330 static struct i2c_driver w83627hf_driver = {
331         .owner          = THIS_MODULE,
332         .name           = "w83627hf",
333         .id             = I2C_DRIVERID_W83627HF,
334         .flags          = I2C_DF_NOTIFY,
335         .attach_adapter = w83627hf_attach_adapter,
336         .detach_client  = w83627hf_detach_client,
337 };
338
339 /* following are the sysfs callback functions */
340 #define show_in_reg(reg) \
341 static ssize_t show_##reg (struct device *dev, char *buf, int nr) \
342 { \
343         struct w83627hf_data *data = w83627hf_update_device(dev); \
344         return sprintf(buf,"%ld\n", (long)IN_FROM_REG(data->reg[nr])); \
345 }
346 show_in_reg(in)
347 show_in_reg(in_min)
348 show_in_reg(in_max)
349
350 #define store_in_reg(REG, reg) \
351 static ssize_t \
352 store_in_##reg (struct device *dev, const char *buf, size_t count, int nr) \
353 { \
354         struct i2c_client *client = to_i2c_client(dev); \
355         struct w83627hf_data *data = i2c_get_clientdata(client); \
356         u32 val; \
357          \
358         val = simple_strtoul(buf, NULL, 10); \
359         data->in_##reg[nr] = IN_TO_REG(val); \
360         w83627hf_write_value(client, W83781D_REG_IN_##REG(nr), \
361                             data->in_##reg[nr]); \
362          \
363         return count; \
364 }
365 store_in_reg(MIN, min)
366 store_in_reg(MAX, max)
367
368 #define sysfs_in_offset(offset) \
369 static ssize_t \
370 show_regs_in_##offset (struct device *dev, char *buf) \
371 { \
372         return show_in(dev, buf, 0x##offset); \
373 } \
374 static DEVICE_ATTR(in##offset##_input, S_IRUGO, show_regs_in_##offset, NULL);
375
376 #define sysfs_in_reg_offset(reg, offset) \
377 static ssize_t show_regs_in_##reg##offset (struct device *dev, char *buf) \
378 { \
379         return show_in_##reg (dev, buf, 0x##offset); \
380 } \
381 static ssize_t \
382 store_regs_in_##reg##offset (struct device *dev, \
383                             const char *buf, size_t count) \
384 { \
385         return store_in_##reg (dev, buf, count, 0x##offset); \
386 } \
387 static DEVICE_ATTR(in##offset##_##reg, S_IRUGO| S_IWUSR, \
388                   show_regs_in_##reg##offset, store_regs_in_##reg##offset);
389
390 #define sysfs_in_offsets(offset) \
391 sysfs_in_offset(offset) \
392 sysfs_in_reg_offset(min, offset) \
393 sysfs_in_reg_offset(max, offset)
394
395 sysfs_in_offsets(1);
396 sysfs_in_offsets(2);
397 sysfs_in_offsets(3);
398 sysfs_in_offsets(4);
399 sysfs_in_offsets(5);
400 sysfs_in_offsets(6);
401 sysfs_in_offsets(7);
402 sysfs_in_offsets(8);
403
404 /* use a different set of functions for in0 */
405 static ssize_t show_in_0(struct w83627hf_data *data, char *buf, u8 reg)
406 {
407         long in0;
408
409         if ((data->vrm_ovt & 0x01) &&
410                 (w83627thf == data->type || w83637hf == data->type))
411
412                 /* use VRM9 calculation */
413                 in0 = (long)((reg * 488 + 70000 + 50) / 100);
414         else
415                 /* use VRM8 (standard) calculation */
416                 in0 = (long)IN_FROM_REG(reg);
417
418         return sprintf(buf,"%ld\n", in0);
419 }
420
421 static ssize_t show_regs_in_0(struct device *dev, char *buf)
422 {
423         struct w83627hf_data *data = w83627hf_update_device(dev);
424         return show_in_0(data, buf, data->in[0]);
425 }
426
427 static ssize_t show_regs_in_min0(struct device *dev, char *buf)
428 {
429         struct w83627hf_data *data = w83627hf_update_device(dev);
430         return show_in_0(data, buf, data->in_min[0]);
431 }
432
433 static ssize_t show_regs_in_max0(struct device *dev, char *buf)
434 {
435         struct w83627hf_data *data = w83627hf_update_device(dev);
436         return show_in_0(data, buf, data->in_max[0]);
437 }
438
439 static ssize_t store_regs_in_min0(struct device *dev,
440         const char *buf, size_t count)
441 {
442         struct i2c_client *client = to_i2c_client(dev);
443         struct w83627hf_data *data = i2c_get_clientdata(client);
444         u32 val;
445
446         val = simple_strtoul(buf, NULL, 10);
447         if ((data->vrm_ovt & 0x01) &&
448                 (w83627thf == data->type || w83637hf == data->type))
449
450                 /* use VRM9 calculation */
451                 data->in_min[0] = (u8)(((val * 100) - 70000 + 244) / 488);
452         else
453                 /* use VRM8 (standard) calculation */
454                 data->in_min[0] = IN_TO_REG(val);
455
456         w83627hf_write_value(client, W83781D_REG_IN_MIN(0), data->in_min[0]);
457         return count;
458 }
459
460 static ssize_t store_regs_in_max0(struct device *dev,
461         const char *buf, size_t count)
462 {
463         struct i2c_client *client = to_i2c_client(dev);
464         struct w83627hf_data *data = i2c_get_clientdata(client);
465         u32 val;
466
467         val = simple_strtoul(buf, NULL, 10);
468         if ((data->vrm_ovt & 0x01) &&
469                 (w83627thf == data->type || w83637hf == data->type))
470                 
471                 /* use VRM9 calculation */
472                 data->in_max[0] = (u8)(((val * 100) - 70000 + 244) / 488);
473         else
474                 /* use VRM8 (standard) calculation */
475                 data->in_max[0] = IN_TO_REG(val);
476
477         w83627hf_write_value(client, W83781D_REG_IN_MAX(0), data->in_max[0]);
478         return count;
479 }
480
481 static DEVICE_ATTR(in0_input, S_IRUGO, show_regs_in_0, NULL);
482 static DEVICE_ATTR(in0_min, S_IRUGO | S_IWUSR,
483         show_regs_in_min0, store_regs_in_min0);
484 static DEVICE_ATTR(in0_max, S_IRUGO | S_IWUSR,
485         show_regs_in_max0, store_regs_in_max0);
486
487 #define device_create_file_in(client, offset) \
488 do { \
489 device_create_file(&client->dev, &dev_attr_in##offset##_input); \
490 device_create_file(&client->dev, &dev_attr_in##offset##_min); \
491 device_create_file(&client->dev, &dev_attr_in##offset##_max); \
492 } while (0)
493
494 #define show_fan_reg(reg) \
495 static ssize_t show_##reg (struct device *dev, char *buf, int nr) \
496 { \
497         struct w83627hf_data *data = w83627hf_update_device(dev); \
498         return sprintf(buf,"%ld\n", \
499                 FAN_FROM_REG(data->reg[nr-1], \
500                             (long)DIV_FROM_REG(data->fan_div[nr-1]))); \
501 }
502 show_fan_reg(fan);
503 show_fan_reg(fan_min);
504
505 static ssize_t
506 store_fan_min(struct device *dev, const char *buf, size_t count, int nr)
507 {
508         struct i2c_client *client = to_i2c_client(dev);
509         struct w83627hf_data *data = i2c_get_clientdata(client);
510         u32 val;
511
512         val = simple_strtoul(buf, NULL, 10);
513         data->fan_min[nr - 1] =
514             FAN_TO_REG(val, DIV_FROM_REG(data->fan_div[nr - 1]));
515         w83627hf_write_value(client, W83781D_REG_FAN_MIN(nr),
516                             data->fan_min[nr - 1]);
517
518         return count;
519 }
520
521 #define sysfs_fan_offset(offset) \
522 static ssize_t show_regs_fan_##offset (struct device *dev, char *buf) \
523 { \
524         return show_fan(dev, buf, 0x##offset); \
525 } \
526 static DEVICE_ATTR(fan##offset##_input, S_IRUGO, show_regs_fan_##offset, NULL);
527
528 #define sysfs_fan_min_offset(offset) \
529 static ssize_t show_regs_fan_min##offset (struct device *dev, char *buf) \
530 { \
531         return show_fan_min(dev, buf, 0x##offset); \
532 } \
533 static ssize_t \
534 store_regs_fan_min##offset (struct device *dev, const char *buf, size_t count) \
535 { \
536         return store_fan_min(dev, buf, count, 0x##offset); \
537 } \
538 static DEVICE_ATTR(fan##offset##_min, S_IRUGO | S_IWUSR, \
539                   show_regs_fan_min##offset, store_regs_fan_min##offset);
540
541 sysfs_fan_offset(1);
542 sysfs_fan_min_offset(1);
543 sysfs_fan_offset(2);
544 sysfs_fan_min_offset(2);
545 sysfs_fan_offset(3);
546 sysfs_fan_min_offset(3);
547
548 #define device_create_file_fan(client, offset) \
549 do { \
550 device_create_file(&client->dev, &dev_attr_fan##offset##_input); \
551 device_create_file(&client->dev, &dev_attr_fan##offset##_min); \
552 } while (0)
553
554 #define show_temp_reg(reg) \
555 static ssize_t show_##reg (struct device *dev, char *buf, int nr) \
556 { \
557         struct w83627hf_data *data = w83627hf_update_device(dev); \
558         if (nr >= 2) {  /* TEMP2 and TEMP3 */ \
559                 return sprintf(buf,"%ld\n", \
560                         (long)LM75_TEMP_FROM_REG(data->reg##_add[nr-2])); \
561         } else {        /* TEMP1 */ \
562                 return sprintf(buf,"%ld\n", (long)TEMP_FROM_REG(data->reg)); \
563         } \
564 }
565 show_temp_reg(temp);
566 show_temp_reg(temp_max);
567 show_temp_reg(temp_max_hyst);
568
569 #define store_temp_reg(REG, reg) \
570 static ssize_t \
571 store_temp_##reg (struct device *dev, const char *buf, size_t count, int nr) \
572 { \
573         struct i2c_client *client = to_i2c_client(dev); \
574         struct w83627hf_data *data = i2c_get_clientdata(client); \
575         u32 val; \
576          \
577         val = simple_strtoul(buf, NULL, 10); \
578          \
579         if (nr >= 2) {  /* TEMP2 and TEMP3 */ \
580                 data->temp_##reg##_add[nr-2] = LM75_TEMP_TO_REG(val); \
581                 w83627hf_write_value(client, W83781D_REG_TEMP_##REG(nr), \
582                                 data->temp_##reg##_add[nr-2]); \
583         } else {        /* TEMP1 */ \
584                 data->temp_##reg = TEMP_TO_REG(val); \
585                 w83627hf_write_value(client, W83781D_REG_TEMP_##REG(nr), \
586                         data->temp_##reg); \
587         } \
588          \
589         return count; \
590 }
591 store_temp_reg(OVER, max);
592 store_temp_reg(HYST, max_hyst);
593
594 #define sysfs_temp_offset(offset) \
595 static ssize_t \
596 show_regs_temp_##offset (struct device *dev, char *buf) \
597 { \
598         return show_temp(dev, buf, 0x##offset); \
599 } \
600 static DEVICE_ATTR(temp##offset##_input, S_IRUGO, show_regs_temp_##offset, NULL);
601
602 #define sysfs_temp_reg_offset(reg, offset) \
603 static ssize_t show_regs_temp_##reg##offset (struct device *dev, char *buf) \
604 { \
605         return show_temp_##reg (dev, buf, 0x##offset); \
606 } \
607 static ssize_t \
608 store_regs_temp_##reg##offset (struct device *dev, \
609                               const char *buf, size_t count) \
610 { \
611         return store_temp_##reg (dev, buf, count, 0x##offset); \
612 } \
613 static DEVICE_ATTR(temp##offset##_##reg, S_IRUGO| S_IWUSR, \
614                   show_regs_temp_##reg##offset, store_regs_temp_##reg##offset);
615
616 #define sysfs_temp_offsets(offset) \
617 sysfs_temp_offset(offset) \
618 sysfs_temp_reg_offset(max, offset) \
619 sysfs_temp_reg_offset(max_hyst, offset)
620
621 sysfs_temp_offsets(1);
622 sysfs_temp_offsets(2);
623 sysfs_temp_offsets(3);
624
625 #define device_create_file_temp(client, offset) \
626 do { \
627 device_create_file(&client->dev, &dev_attr_temp##offset##_input); \
628 device_create_file(&client->dev, &dev_attr_temp##offset##_max); \
629 device_create_file(&client->dev, &dev_attr_temp##offset##_max_hyst); \
630 } while (0)
631
632 static ssize_t
633 show_vid_reg(struct device *dev, char *buf)
634 {
635         struct w83627hf_data *data = w83627hf_update_device(dev);
636         return sprintf(buf, "%ld\n", (long) vid_from_reg(data->vid, data->vrm));
637 }
638 static DEVICE_ATTR(in0_ref, S_IRUGO, show_vid_reg, NULL);
639 #define device_create_file_vid(client) \
640 device_create_file(&client->dev, &dev_attr_in0_ref)
641
642 static ssize_t
643 show_vrm_reg(struct device *dev, char *buf)
644 {
645         struct w83627hf_data *data = w83627hf_update_device(dev);
646         return sprintf(buf, "%ld\n", (long) data->vrm);
647 }
648 static ssize_t
649 store_vrm_reg(struct device *dev, const char *buf, size_t count)
650 {
651         struct i2c_client *client = to_i2c_client(dev);
652         struct w83627hf_data *data = i2c_get_clientdata(client);
653         u32 val;
654
655         val = simple_strtoul(buf, NULL, 10);
656         data->vrm = val;
657
658         return count;
659 }
660 static DEVICE_ATTR(vrm, S_IRUGO | S_IWUSR, show_vrm_reg, store_vrm_reg);
661 #define device_create_file_vrm(client) \
662 device_create_file(&client->dev, &dev_attr_vrm)
663
664 static ssize_t
665 show_alarms_reg(struct device *dev, char *buf)
666 {
667         struct w83627hf_data *data = w83627hf_update_device(dev);
668         return sprintf(buf, "%ld\n", (long) data->alarms);
669 }
670 static DEVICE_ATTR(alarms, S_IRUGO, show_alarms_reg, NULL);
671 #define device_create_file_alarms(client) \
672 device_create_file(&client->dev, &dev_attr_alarms)
673
674 #define show_beep_reg(REG, reg) \
675 static ssize_t show_beep_##reg (struct device *dev, char *buf) \
676 { \
677         struct w83627hf_data *data = w83627hf_update_device(dev); \
678         return sprintf(buf,"%ld\n", \
679                       (long)BEEP_##REG##_FROM_REG(data->beep_##reg)); \
680 }
681 show_beep_reg(ENABLE, enable)
682 show_beep_reg(MASK, mask)
683
684 #define BEEP_ENABLE                     0       /* Store beep_enable */
685 #define BEEP_MASK                       1       /* Store beep_mask */
686
687 static ssize_t
688 store_beep_reg(struct device *dev, const char *buf, size_t count,
689                int update_mask)
690 {
691         struct i2c_client *client = to_i2c_client(dev);
692         struct w83627hf_data *data = i2c_get_clientdata(client);
693         u32 val, val2;
694
695         val = simple_strtoul(buf, NULL, 10);
696
697         if (update_mask == BEEP_MASK) { /* We are storing beep_mask */
698                 data->beep_mask = BEEP_MASK_TO_REG(val);
699                 w83627hf_write_value(client, W83781D_REG_BEEP_INTS1,
700                                     data->beep_mask & 0xff);
701                 w83627hf_write_value(client, W83781D_REG_BEEP_INTS3,
702                                     ((data->beep_mask) >> 16) & 0xff);
703                 val2 = (data->beep_mask >> 8) & 0x7f;
704         } else {                /* We are storing beep_enable */
705                 val2 =
706                     w83627hf_read_value(client, W83781D_REG_BEEP_INTS2) & 0x7f;
707                 data->beep_enable = BEEP_ENABLE_TO_REG(val);
708         }
709
710         w83627hf_write_value(client, W83781D_REG_BEEP_INTS2,
711                             val2 | data->beep_enable << 7);
712
713         return count;
714 }
715
716 #define sysfs_beep(REG, reg) \
717 static ssize_t show_regs_beep_##reg (struct device *dev, char *buf) \
718 { \
719         return show_beep_##reg(dev, buf); \
720 } \
721 static ssize_t \
722 store_regs_beep_##reg (struct device *dev, const char *buf, size_t count) \
723 { \
724         return store_beep_reg(dev, buf, count, BEEP_##REG); \
725 } \
726 static DEVICE_ATTR(beep_##reg, S_IRUGO | S_IWUSR, \
727                   show_regs_beep_##reg, store_regs_beep_##reg);
728
729 sysfs_beep(ENABLE, enable);
730 sysfs_beep(MASK, mask);
731
732 #define device_create_file_beep(client) \
733 do { \
734 device_create_file(&client->dev, &dev_attr_beep_enable); \
735 device_create_file(&client->dev, &dev_attr_beep_mask); \
736 } while (0)
737
738 static ssize_t
739 show_fan_div_reg(struct device *dev, char *buf, int nr)
740 {
741         struct w83627hf_data *data = w83627hf_update_device(dev);
742         return sprintf(buf, "%ld\n",
743                        (long) DIV_FROM_REG(data->fan_div[nr - 1]));
744 }
745
746 /* Note: we save and restore the fan minimum here, because its value is
747    determined in part by the fan divisor.  This follows the principle of
748    least suprise; the user doesn't expect the fan minimum to change just
749    because the divisor changed. */
750 static ssize_t
751 store_fan_div_reg(struct device *dev, const char *buf, size_t count, int nr)
752 {
753         struct i2c_client *client = to_i2c_client(dev);
754         struct w83627hf_data *data = i2c_get_clientdata(client);
755         unsigned long min;
756         u8 reg;
757
758         /* Save fan_min */
759         min = FAN_FROM_REG(data->fan_min[nr],
760                            DIV_FROM_REG(data->fan_div[nr]));
761
762         data->fan_div[nr] = DIV_TO_REG(simple_strtoul(buf, NULL, 10));
763
764         reg = (w83627hf_read_value(client, nr==2 ? W83781D_REG_PIN : W83781D_REG_VID_FANDIV)
765                & (nr==0 ? 0xcf : 0x3f))
766             | ((data->fan_div[nr] & 0x03) << (nr==0 ? 4 : 6));
767         w83627hf_write_value(client, nr==2 ? W83781D_REG_PIN : W83781D_REG_VID_FANDIV, reg);
768
769         reg = (w83627hf_read_value(client, W83781D_REG_VBAT)
770                & ~(1 << (5 + nr)))
771             | ((data->fan_div[nr] & 0x04) << (3 + nr));
772         w83627hf_write_value(client, W83781D_REG_VBAT, reg);
773
774         /* Restore fan_min */
775         data->fan_min[nr] = FAN_TO_REG(min, DIV_FROM_REG(data->fan_div[nr]));
776         w83627hf_write_value(client, W83781D_REG_FAN_MIN(nr+1), data->fan_min[nr]);
777
778         return count;
779 }
780
781 #define sysfs_fan_div(offset) \
782 static ssize_t show_regs_fan_div_##offset (struct device *dev, char *buf) \
783 { \
784         return show_fan_div_reg(dev, buf, offset); \
785 } \
786 static ssize_t \
787 store_regs_fan_div_##offset (struct device *dev, \
788                             const char *buf, size_t count) \
789 { \
790         return store_fan_div_reg(dev, buf, count, offset - 1); \
791 } \
792 static DEVICE_ATTR(fan##offset##_div, S_IRUGO | S_IWUSR, \
793                   show_regs_fan_div_##offset, store_regs_fan_div_##offset);
794
795 sysfs_fan_div(1);
796 sysfs_fan_div(2);
797 sysfs_fan_div(3);
798
799 #define device_create_file_fan_div(client, offset) \
800 do { \
801 device_create_file(&client->dev, &dev_attr_fan##offset##_div); \
802 } while (0)
803
804 static ssize_t
805 show_pwm_reg(struct device *dev, char *buf, int nr)
806 {
807         struct w83627hf_data *data = w83627hf_update_device(dev);
808         return sprintf(buf, "%ld\n", (long) data->pwm[nr - 1]);
809 }
810
811 static ssize_t
812 store_pwm_reg(struct device *dev, const char *buf, size_t count, int nr)
813 {
814         struct i2c_client *client = to_i2c_client(dev);
815         struct w83627hf_data *data = i2c_get_clientdata(client);
816         u32 val;
817
818         val = simple_strtoul(buf, NULL, 10);
819
820         if (data->type == w83627thf) {
821                 /* bits 0-3 are reserved  in 627THF */
822                 data->pwm[nr - 1] = PWM_TO_REG(val) & 0xf0;
823                 w83627hf_write_value(client,
824                                      W836X7HF_REG_PWM(data->type, nr),
825                                      data->pwm[nr - 1] |
826                                      (w83627hf_read_value(client,
827                                      W836X7HF_REG_PWM(data->type, nr)) & 0x0f));
828         } else {
829                 data->pwm[nr - 1] = PWM_TO_REG(val);
830                 w83627hf_write_value(client,
831                                      W836X7HF_REG_PWM(data->type, nr),
832                                      data->pwm[nr - 1]);
833         }
834
835         return count;
836 }
837
838 #define sysfs_pwm(offset) \
839 static ssize_t show_regs_pwm_##offset (struct device *dev, char *buf) \
840 { \
841         return show_pwm_reg(dev, buf, offset); \
842 } \
843 static ssize_t \
844 store_regs_pwm_##offset (struct device *dev, const char *buf, size_t count) \
845 { \
846         return store_pwm_reg(dev, buf, count, offset); \
847 } \
848 static DEVICE_ATTR(fan##offset##_pwm, S_IRUGO | S_IWUSR, \
849                   show_regs_pwm_##offset, store_regs_pwm_##offset);
850
851 sysfs_pwm(1);
852 sysfs_pwm(2);
853 sysfs_pwm(3);
854
855 #define device_create_file_pwm(client, offset) \
856 do { \
857 device_create_file(&client->dev, &dev_attr_fan##offset##_pwm); \
858 } while (0)
859
860 static ssize_t
861 show_sensor_reg(struct device *dev, char *buf, int nr)
862 {
863         struct w83627hf_data *data = w83627hf_update_device(dev);
864         return sprintf(buf, "%ld\n", (long) data->sens[nr - 1]);
865 }
866
867 static ssize_t
868 store_sensor_reg(struct device *dev, const char *buf, size_t count, int nr)
869 {
870         struct i2c_client *client = to_i2c_client(dev);
871         struct w83627hf_data *data = i2c_get_clientdata(client);
872         u32 val, tmp;
873
874         val = simple_strtoul(buf, NULL, 10);
875
876         switch (val) {
877         case 1:         /* PII/Celeron diode */
878                 tmp = w83627hf_read_value(client, W83781D_REG_SCFG1);
879                 w83627hf_write_value(client, W83781D_REG_SCFG1,
880                                     tmp | BIT_SCFG1[nr - 1]);
881                 tmp = w83627hf_read_value(client, W83781D_REG_SCFG2);
882                 w83627hf_write_value(client, W83781D_REG_SCFG2,
883                                     tmp | BIT_SCFG2[nr - 1]);
884                 data->sens[nr - 1] = val;
885                 break;
886         case 2:         /* 3904 */
887                 tmp = w83627hf_read_value(client, W83781D_REG_SCFG1);
888                 w83627hf_write_value(client, W83781D_REG_SCFG1,
889                                     tmp | BIT_SCFG1[nr - 1]);
890                 tmp = w83627hf_read_value(client, W83781D_REG_SCFG2);
891                 w83627hf_write_value(client, W83781D_REG_SCFG2,
892                                     tmp & ~BIT_SCFG2[nr - 1]);
893                 data->sens[nr - 1] = val;
894                 break;
895         case W83781D_DEFAULT_BETA:      /* thermistor */
896                 tmp = w83627hf_read_value(client, W83781D_REG_SCFG1);
897                 w83627hf_write_value(client, W83781D_REG_SCFG1,
898                                     tmp & ~BIT_SCFG1[nr - 1]);
899                 data->sens[nr - 1] = val;
900                 break;
901         default:
902                 dev_err(&client->dev,
903                        "Invalid sensor type %ld; must be 1, 2, or %d\n",
904                        (long) val, W83781D_DEFAULT_BETA);
905                 break;
906         }
907
908         return count;
909 }
910
911 #define sysfs_sensor(offset) \
912 static ssize_t show_regs_sensor_##offset (struct device *dev, char *buf) \
913 { \
914     return show_sensor_reg(dev, buf, offset); \
915 } \
916 static ssize_t \
917 store_regs_sensor_##offset (struct device *dev, const char *buf, size_t count) \
918 { \
919     return store_sensor_reg(dev, buf, count, offset); \
920 } \
921 static DEVICE_ATTR(temp##offset##_type, S_IRUGO | S_IWUSR, \
922                   show_regs_sensor_##offset, store_regs_sensor_##offset);
923
924 sysfs_sensor(1);
925 sysfs_sensor(2);
926 sysfs_sensor(3);
927
928 #define device_create_file_sensor(client, offset) \
929 do { \
930 device_create_file(&client->dev, &dev_attr_temp##offset##_type); \
931 } while (0)
932
933
934 /* This function is called when:
935      * w83627hf_driver is inserted (when this module is loaded), for each
936        available adapter
937      * when a new adapter is inserted (and w83627hf_driver is still present) */
938 static int w83627hf_attach_adapter(struct i2c_adapter *adapter)
939 {
940         return i2c_detect(adapter, &addr_data, w83627hf_detect);
941 }
942
943 static int w83627hf_find(int *address)
944 {
945         u16 val;
946
947         superio_enter();
948         val= superio_inb(DEVID);
949         if(val != W627_DEVID &&
950            val != W627THF_DEVID &&
951            val != W697_DEVID &&
952            val != W637_DEVID) {
953                 superio_exit();
954                 return -ENODEV;
955         }
956
957         superio_select(W83627HF_LD_HWM);
958         val = (superio_inb(WINB_BASE_REG) << 8) |
959                superio_inb(WINB_BASE_REG + 1);
960         *address = val & ~(WINB_EXTENT - 1);
961         if (*address == 0 && force_addr == 0) {
962                 superio_exit();
963                 return -ENODEV;
964         }
965         if (force_addr)
966                 *address = force_addr;  /* so detect will get called */
967
968         superio_exit();
969         return 0;
970 }
971
972 int w83627hf_detect(struct i2c_adapter *adapter, int address,
973                    int kind)
974 {
975         int val;
976         struct i2c_client *new_client;
977         struct w83627hf_data *data;
978         int err = 0;
979         const char *client_name = "";
980
981         if (!i2c_is_isa_adapter(adapter)) {
982                 err = -ENODEV;
983                 goto ERROR0;
984         }
985
986         if(force_addr)
987                 address = force_addr & ~(WINB_EXTENT - 1);
988
989         if (!request_region(address, WINB_EXTENT, "w83627hf")) {
990                 err = -EBUSY;
991                 goto ERROR0;
992         }
993
994         if(force_addr) {
995                 printk("w83627hf.o: forcing ISA address 0x%04X\n", address);
996                 superio_enter();
997                 superio_select(W83627HF_LD_HWM);
998                 superio_outb(WINB_BASE_REG, address >> 8);
999                 superio_outb(WINB_BASE_REG+1, address & 0xff);
1000                 superio_exit();
1001         }
1002
1003         superio_enter();
1004         val= superio_inb(DEVID);
1005         if(val == W627_DEVID)
1006                 kind = w83627hf;
1007         else if(val == W697_DEVID)
1008                 kind = w83697hf;
1009         else if(val == W627THF_DEVID)
1010                 kind = w83627thf;
1011         else if(val == W637_DEVID)
1012                 kind = w83637hf;
1013         else {
1014                 dev_info(&adapter->dev,
1015                          "Unsupported chip (dev_id=0x%02X).\n", val);
1016                 goto ERROR1;
1017         }
1018
1019         superio_select(W83627HF_LD_HWM);
1020         if((val = 0x01 & superio_inb(WINB_ACT_REG)) == 0)
1021                 superio_outb(WINB_ACT_REG, 1);
1022         superio_exit();
1023
1024         /* OK. For now, we presume we have a valid client. We now create the
1025            client structure, even though we cannot fill it completely yet.
1026            But it allows us to access w83627hf_{read,write}_value. */
1027
1028         if (!(data = kmalloc(sizeof(struct w83627hf_data), GFP_KERNEL))) {
1029                 err = -ENOMEM;
1030                 goto ERROR1;
1031         }
1032         memset(data, 0, sizeof(struct w83627hf_data));
1033
1034         new_client = &data->client;
1035         i2c_set_clientdata(new_client, data);
1036         new_client->addr = address;
1037         init_MUTEX(&data->lock);
1038         new_client->adapter = adapter;
1039         new_client->driver = &w83627hf_driver;
1040         new_client->flags = 0;
1041
1042
1043         if (kind == w83627hf) {
1044                 client_name = "w83627hf";
1045         } else if (kind == w83627thf) {
1046                 client_name = "w83627thf";
1047         } else if (kind == w83697hf) {
1048                 client_name = "w83697hf";
1049         } else if (kind == w83637hf) {
1050                 client_name = "w83637hf";
1051         }
1052
1053         /* Fill in the remaining client fields and put into the global list */
1054         strlcpy(new_client->name, client_name, I2C_NAME_SIZE);
1055         data->type = kind;
1056         data->valid = 0;
1057         init_MUTEX(&data->update_lock);
1058
1059         /* Tell the I2C layer a new client has arrived */
1060         if ((err = i2c_attach_client(new_client)))
1061                 goto ERROR2;
1062
1063         data->lm75 = NULL;
1064
1065         /* Initialize the chip */
1066         w83627hf_init_client(new_client);
1067
1068         /* A few vars need to be filled upon startup */
1069         data->fan_min[0] = w83627hf_read_value(new_client, W83781D_REG_FAN_MIN(1));
1070         data->fan_min[1] = w83627hf_read_value(new_client, W83781D_REG_FAN_MIN(2));
1071         data->fan_min[2] = w83627hf_read_value(new_client, W83781D_REG_FAN_MIN(3));
1072
1073         /* Register sysfs hooks */
1074         device_create_file_in(new_client, 0);
1075         if (kind != w83697hf)
1076                 device_create_file_in(new_client, 1);
1077         device_create_file_in(new_client, 2);
1078         device_create_file_in(new_client, 3);
1079         device_create_file_in(new_client, 4);
1080         if (kind != w83627thf && kind != w83637hf) {
1081                 device_create_file_in(new_client, 5);
1082                 device_create_file_in(new_client, 6);
1083         }
1084         device_create_file_in(new_client, 7);
1085         device_create_file_in(new_client, 8);
1086
1087         device_create_file_fan(new_client, 1);
1088         device_create_file_fan(new_client, 2);
1089         if (kind != w83697hf)
1090                 device_create_file_fan(new_client, 3);
1091
1092         device_create_file_temp(new_client, 1);
1093         device_create_file_temp(new_client, 2);
1094         if (kind != w83697hf)
1095                 device_create_file_temp(new_client, 3);
1096
1097         if (kind != w83697hf)
1098                 device_create_file_vid(new_client);
1099
1100         if (kind != w83697hf)
1101                 device_create_file_vrm(new_client);
1102
1103         device_create_file_fan_div(new_client, 1);
1104         device_create_file_fan_div(new_client, 2);
1105         if (kind != w83697hf)
1106                 device_create_file_fan_div(new_client, 3);
1107
1108         device_create_file_alarms(new_client);
1109
1110         device_create_file_beep(new_client);
1111
1112         device_create_file_pwm(new_client, 1);
1113         device_create_file_pwm(new_client, 2);
1114         if (kind == w83627thf || kind == w83637hf)
1115                 device_create_file_pwm(new_client, 3);
1116
1117         device_create_file_sensor(new_client, 1);
1118         device_create_file_sensor(new_client, 2);
1119         if (kind != w83697hf)
1120                 device_create_file_sensor(new_client, 3);
1121
1122         return 0;
1123
1124       ERROR2:
1125         kfree(data);
1126       ERROR1:
1127         release_region(address, WINB_EXTENT);
1128       ERROR0:
1129         return err;
1130 }
1131
1132 static int w83627hf_detach_client(struct i2c_client *client)
1133 {
1134         int err;
1135
1136         if ((err = i2c_detach_client(client))) {
1137                 dev_err(&client->dev,
1138                        "Client deregistration failed, client not detached.\n");
1139                 return err;
1140         }
1141
1142         release_region(client->addr, WINB_EXTENT);
1143         kfree(i2c_get_clientdata(client));
1144
1145         return 0;
1146 }
1147
1148
1149 /*
1150    ISA access must always be locked explicitly!
1151    We ignore the W83781D BUSY flag at this moment - it could lead to deadlocks,
1152    would slow down the W83781D access and should not be necessary.
1153    There are some ugly typecasts here, but the good news is - they should
1154    nowhere else be necessary! */
1155 static int w83627hf_read_value(struct i2c_client *client, u16 reg)
1156 {
1157         struct w83627hf_data *data = i2c_get_clientdata(client);
1158         int res, word_sized;
1159
1160         down(&data->lock);
1161         word_sized = (((reg & 0xff00) == 0x100)
1162                    || ((reg & 0xff00) == 0x200))
1163                   && (((reg & 0x00ff) == 0x50)
1164                    || ((reg & 0x00ff) == 0x53)
1165                    || ((reg & 0x00ff) == 0x55));
1166         if (reg & 0xff00) {
1167                 outb_p(W83781D_REG_BANK,
1168                        client->addr + W83781D_ADDR_REG_OFFSET);
1169                 outb_p(reg >> 8,
1170                        client->addr + W83781D_DATA_REG_OFFSET);
1171         }
1172         outb_p(reg & 0xff, client->addr + W83781D_ADDR_REG_OFFSET);
1173         res = inb_p(client->addr + W83781D_DATA_REG_OFFSET);
1174         if (word_sized) {
1175                 outb_p((reg & 0xff) + 1,
1176                        client->addr + W83781D_ADDR_REG_OFFSET);
1177                 res =
1178                     (res << 8) + inb_p(client->addr +
1179                                        W83781D_DATA_REG_OFFSET);
1180         }
1181         if (reg & 0xff00) {
1182                 outb_p(W83781D_REG_BANK,
1183                        client->addr + W83781D_ADDR_REG_OFFSET);
1184                 outb_p(0, client->addr + W83781D_DATA_REG_OFFSET);
1185         }
1186         up(&data->lock);
1187         return res;
1188 }
1189
1190 static int w83627thf_read_gpio5(struct i2c_client *client)
1191 {
1192         struct w83627hf_data *data = i2c_get_clientdata(client);
1193         int res, inv;
1194
1195         down(&data->lock);
1196         superio_enter();
1197         superio_select(W83627HF_LD_GPIO5);
1198         res = superio_inb(W83627THF_GPIO5_DR);
1199         inv = superio_inb(W83627THF_GPIO5_INVR);
1200         superio_exit();
1201         up(&data->lock);
1202         return res;
1203 }
1204
1205 static int w83627hf_write_value(struct i2c_client *client, u16 reg, u16 value)
1206 {
1207         struct w83627hf_data *data = i2c_get_clientdata(client);
1208         int word_sized;
1209
1210         down(&data->lock);
1211         word_sized = (((reg & 0xff00) == 0x100)
1212                    || ((reg & 0xff00) == 0x200))
1213                   && (((reg & 0x00ff) == 0x53)
1214                    || ((reg & 0x00ff) == 0x55));
1215         if (reg & 0xff00) {
1216                 outb_p(W83781D_REG_BANK,
1217                        client->addr + W83781D_ADDR_REG_OFFSET);
1218                 outb_p(reg >> 8,
1219                        client->addr + W83781D_DATA_REG_OFFSET);
1220         }
1221         outb_p(reg & 0xff, client->addr + W83781D_ADDR_REG_OFFSET);
1222         if (word_sized) {
1223                 outb_p(value >> 8,
1224                        client->addr + W83781D_DATA_REG_OFFSET);
1225                 outb_p((reg & 0xff) + 1,
1226                        client->addr + W83781D_ADDR_REG_OFFSET);
1227         }
1228         outb_p(value & 0xff,
1229                client->addr + W83781D_DATA_REG_OFFSET);
1230         if (reg & 0xff00) {
1231                 outb_p(W83781D_REG_BANK,
1232                        client->addr + W83781D_ADDR_REG_OFFSET);
1233                 outb_p(0, client->addr + W83781D_DATA_REG_OFFSET);
1234         }
1235         up(&data->lock);
1236         return 0;
1237 }
1238
1239 /* Called when we have found a new W83781D. It should set limits, etc. */
1240 static void w83627hf_init_client(struct i2c_client *client)
1241 {
1242         struct w83627hf_data *data = i2c_get_clientdata(client);
1243         int i;
1244         int type = data->type;
1245         u8 tmp;
1246
1247         if(init) {
1248                 /* save this register */
1249                 i = w83627hf_read_value(client, W83781D_REG_BEEP_CONFIG);
1250                 /* Reset all except Watchdog values and last conversion values
1251                    This sets fan-divs to 2, among others */
1252                 w83627hf_write_value(client, W83781D_REG_CONFIG, 0x80);
1253                 /* Restore the register and disable power-on abnormal beep.
1254                    This saves FAN 1/2/3 input/output values set by BIOS. */
1255                 w83627hf_write_value(client, W83781D_REG_BEEP_CONFIG, i | 0x80);
1256                 /* Disable master beep-enable (reset turns it on).
1257                    Individual beeps should be reset to off but for some reason
1258                    disabling this bit helps some people not get beeped */
1259                 w83627hf_write_value(client, W83781D_REG_BEEP_INTS2, 0);
1260         }
1261
1262         /* Minimize conflicts with other winbond i2c-only clients...  */
1263         /* disable i2c subclients... how to disable main i2c client?? */
1264         /* force i2c address to relatively uncommon address */
1265         w83627hf_write_value(client, W83781D_REG_I2C_SUBADDR, 0x89);
1266         w83627hf_write_value(client, W83781D_REG_I2C_ADDR, force_i2c);
1267
1268         /* Read VID only once */
1269         if (w83627hf == data->type || w83637hf == data->type) {
1270                 int lo = w83627hf_read_value(client, W83781D_REG_VID_FANDIV);
1271                 int hi = w83627hf_read_value(client, W83781D_REG_CHIPID);
1272                 data->vid = (lo & 0x0f) | ((hi & 0x01) << 4);
1273         } else if (w83627thf == data->type) {
1274                 data->vid = w83627thf_read_gpio5(client) & 0x1f;
1275         }
1276
1277         /* Read VRM & OVT Config only once */
1278         if (w83627thf == data->type || w83637hf == data->type) {
1279                 data->vrm_ovt = 
1280                         w83627hf_read_value(client, W83627THF_REG_VRM_OVT_CFG);
1281                 data->vrm = (data->vrm_ovt & 0x01) ? 90 : 82;
1282         } else {
1283                 /* Convert VID to voltage based on default VRM */
1284                 data->vrm = DEFAULT_VRM;
1285         }
1286
1287         tmp = w83627hf_read_value(client, W83781D_REG_SCFG1);
1288         for (i = 1; i <= 3; i++) {
1289                 if (!(tmp & BIT_SCFG1[i - 1])) {
1290                         data->sens[i - 1] = W83781D_DEFAULT_BETA;
1291                 } else {
1292                         if (w83627hf_read_value
1293                             (client,
1294                              W83781D_REG_SCFG2) & BIT_SCFG2[i - 1])
1295                                 data->sens[i - 1] = 1;
1296                         else
1297                                 data->sens[i - 1] = 2;
1298                 }
1299                 if ((type == w83697hf) && (i == 2))
1300                         break;
1301         }
1302
1303         data->pwmenable[0] = 1;
1304         data->pwmenable[1] = 1;
1305         data->pwmenable[2] = 1;
1306
1307         if(init) {
1308                 if (type == w83627hf) {
1309                         /* enable PWM2 control (can't hurt since PWM reg
1310                            should have been reset to 0xff) */
1311                         w83627hf_write_value(client, W83627HF_REG_PWMCLK12,
1312                                             0x19);
1313                 }
1314                 /* enable comparator mode for temp2 and temp3 so
1315                    alarm indication will work correctly */
1316                 i = w83627hf_read_value(client, W83781D_REG_IRQ);
1317                 if (!(i & 0x40))
1318                         w83627hf_write_value(client, W83781D_REG_IRQ,
1319                                             i | 0x40);
1320         }
1321
1322         /* Start monitoring */
1323         w83627hf_write_value(client, W83781D_REG_CONFIG,
1324                             (w83627hf_read_value(client,
1325                                                 W83781D_REG_CONFIG) & 0xf7)
1326                             | 0x01);
1327 }
1328
1329 static struct w83627hf_data *w83627hf_update_device(struct device *dev)
1330 {
1331         struct i2c_client *client = to_i2c_client(dev);
1332         struct w83627hf_data *data = i2c_get_clientdata(client);
1333         int i;
1334
1335         down(&data->update_lock);
1336
1337         if ((jiffies - data->last_updated > HZ + HZ / 2) ||
1338             (jiffies < data->last_updated) || !data->valid) {
1339                 for (i = 0; i <= 8; i++) {
1340                         /* skip missing sensors */
1341                         if (((data->type == w83697hf) && (i == 1)) ||
1342                             ((data->type == w83627thf || data->type == w83637hf)
1343                             && (i == 4 || i == 5)))
1344                                 continue;
1345                         data->in[i] =
1346                             w83627hf_read_value(client, W83781D_REG_IN(i));
1347                         data->in_min[i] =
1348                             w83627hf_read_value(client,
1349                                                W83781D_REG_IN_MIN(i));
1350                         data->in_max[i] =
1351                             w83627hf_read_value(client,
1352                                                W83781D_REG_IN_MAX(i));
1353                 }
1354                 for (i = 1; i <= 3; i++) {
1355                         data->fan[i - 1] =
1356                             w83627hf_read_value(client, W83781D_REG_FAN(i));
1357                         data->fan_min[i - 1] =
1358                             w83627hf_read_value(client,
1359                                                W83781D_REG_FAN_MIN(i));
1360                 }
1361                 for (i = 1; i <= 3; i++) {
1362                         u8 tmp = w83627hf_read_value(client,
1363                                 W836X7HF_REG_PWM(data->type, i));
1364                         /* bits 0-3 are reserved  in 627THF */
1365                         if (data->type == w83627thf)
1366                                 tmp &= 0xf0;
1367                         data->pwm[i - 1] = tmp;
1368                         if(i == 2 &&
1369                            (data->type == w83627hf || data->type == w83697hf))
1370                                 break;
1371                 }
1372
1373                 data->temp = w83627hf_read_value(client, W83781D_REG_TEMP(1));
1374                 data->temp_max =
1375                     w83627hf_read_value(client, W83781D_REG_TEMP_OVER(1));
1376                 data->temp_max_hyst =
1377                     w83627hf_read_value(client, W83781D_REG_TEMP_HYST(1));
1378                 data->temp_add[0] =
1379                     w83627hf_read_value(client, W83781D_REG_TEMP(2));
1380                 data->temp_max_add[0] =
1381                     w83627hf_read_value(client, W83781D_REG_TEMP_OVER(2));
1382                 data->temp_max_hyst_add[0] =
1383                     w83627hf_read_value(client, W83781D_REG_TEMP_HYST(2));
1384                 if (data->type != w83697hf) {
1385                         data->temp_add[1] =
1386                           w83627hf_read_value(client, W83781D_REG_TEMP(3));
1387                         data->temp_max_add[1] =
1388                           w83627hf_read_value(client, W83781D_REG_TEMP_OVER(3));
1389                         data->temp_max_hyst_add[1] =
1390                           w83627hf_read_value(client, W83781D_REG_TEMP_HYST(3));
1391                 }
1392
1393                 i = w83627hf_read_value(client, W83781D_REG_VID_FANDIV);
1394                 data->fan_div[0] = (i >> 4) & 0x03;
1395                 data->fan_div[1] = (i >> 6) & 0x03;
1396                 if (data->type != w83697hf) {
1397                         data->fan_div[2] = (w83627hf_read_value(client,
1398                                                W83781D_REG_PIN) >> 6) & 0x03;
1399                 }
1400                 i = w83627hf_read_value(client, W83781D_REG_VBAT);
1401                 data->fan_div[0] |= (i >> 3) & 0x04;
1402                 data->fan_div[1] |= (i >> 4) & 0x04;
1403                 if (data->type != w83697hf)
1404                         data->fan_div[2] |= (i >> 5) & 0x04;
1405                 data->alarms =
1406                     w83627hf_read_value(client, W83781D_REG_ALARM1) |
1407                     (w83627hf_read_value(client, W83781D_REG_ALARM2) << 8) |
1408                     (w83627hf_read_value(client, W83781D_REG_ALARM3) << 16);
1409                 i = w83627hf_read_value(client, W83781D_REG_BEEP_INTS2);
1410                 data->beep_enable = i >> 7;
1411                 data->beep_mask = ((i & 0x7f) << 8) |
1412                     w83627hf_read_value(client, W83781D_REG_BEEP_INTS1) |
1413                     w83627hf_read_value(client, W83781D_REG_BEEP_INTS3) << 16;
1414                 data->last_updated = jiffies;
1415                 data->valid = 1;
1416         }
1417
1418         up(&data->update_lock);
1419
1420         return data;
1421 }
1422
1423 static int __init sensors_w83627hf_init(void)
1424 {
1425         int addr;
1426
1427         if (w83627hf_find(&addr)) {
1428                 return -ENODEV;
1429         }
1430         normal_isa[0] = addr;
1431
1432         return i2c_add_driver(&w83627hf_driver);
1433 }
1434
1435 static void __exit sensors_w83627hf_exit(void)
1436 {
1437         i2c_del_driver(&w83627hf_driver);
1438 }
1439
1440 MODULE_AUTHOR("Frodo Looijaard <frodol@dds.nl>, "
1441               "Philip Edelbrock <phil@netroedge.com>, "
1442               "and Mark Studebaker <mdsxyz123@yahoo.com>");
1443 MODULE_DESCRIPTION("W83627HF driver");
1444 MODULE_LICENSE("GPL");
1445
1446 module_init(sensors_w83627hf_init);
1447 module_exit(sensors_w83627hf_exit);