ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[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, unused yet */
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 + 5)
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 };
316
317
318 static int w83627hf_attach_adapter(struct i2c_adapter *adapter);
319 static int w83627hf_detect(struct i2c_adapter *adapter, int address,
320                           int kind);
321 static int w83627hf_detach_client(struct i2c_client *client);
322
323 static int w83627hf_read_value(struct i2c_client *client, u16 register);
324 static int w83627hf_write_value(struct i2c_client *client, u16 register,
325                                u16 value);
326 static struct w83627hf_data *w83627hf_update_device(struct device *dev);
327 static void w83627hf_init_client(struct i2c_client *client);
328
329 static struct i2c_driver w83627hf_driver = {
330         .owner          = THIS_MODULE,
331         .name           = "w83627hf",
332         .id             = I2C_DRIVERID_W83627HF,
333         .flags          = I2C_DF_NOTIFY,
334         .attach_adapter = w83627hf_attach_adapter,
335         .detach_client  = w83627hf_detach_client,
336 };
337
338 /* following are the sysfs callback functions */
339 #define show_in_reg(reg) \
340 static ssize_t show_##reg (struct device *dev, char *buf, int nr) \
341 { \
342         struct w83627hf_data *data = w83627hf_update_device(dev); \
343         return sprintf(buf,"%ld\n", (long)IN_FROM_REG(data->reg[nr])); \
344 }
345 show_in_reg(in)
346 show_in_reg(in_min)
347 show_in_reg(in_max)
348
349 #define store_in_reg(REG, reg) \
350 static ssize_t \
351 store_in_##reg (struct device *dev, const char *buf, size_t count, int nr) \
352 { \
353         struct i2c_client *client = to_i2c_client(dev); \
354         struct w83627hf_data *data = i2c_get_clientdata(client); \
355         u32 val; \
356          \
357         val = simple_strtoul(buf, NULL, 10); \
358         data->in_##reg[nr] = IN_TO_REG(val); \
359         w83627hf_write_value(client, W83781D_REG_IN_##REG(nr), \
360                             data->in_##reg[nr]); \
361          \
362         return count; \
363 }
364 store_in_reg(MIN, min)
365 store_in_reg(MAX, max)
366
367 #define sysfs_in_offset(offset) \
368 static ssize_t \
369 show_regs_in_##offset (struct device *dev, char *buf) \
370 { \
371         return show_in(dev, buf, 0x##offset); \
372 } \
373 static DEVICE_ATTR(in##offset##_input, S_IRUGO, show_regs_in_##offset, NULL)
374
375 #define sysfs_in_reg_offset(reg, offset) \
376 static ssize_t show_regs_in_##reg##offset (struct device *dev, char *buf) \
377 { \
378         return show_in_##reg (dev, buf, 0x##offset); \
379 } \
380 static ssize_t \
381 store_regs_in_##reg##offset (struct device *dev, \
382                             const char *buf, size_t count) \
383 { \
384         return store_in_##reg (dev, buf, count, 0x##offset); \
385 } \
386 static DEVICE_ATTR(in##offset##_##reg, S_IRUGO| S_IWUSR, \
387                   show_regs_in_##reg##offset, store_regs_in_##reg##offset)
388
389 #define sysfs_in_offsets(offset) \
390 sysfs_in_offset(offset) \
391 sysfs_in_reg_offset(min, offset) \
392 sysfs_in_reg_offset(max, offset)
393
394 sysfs_in_offsets(0)
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 #define device_create_file_in(client, offset) \
405 do { \
406 device_create_file(&client->dev, &dev_attr_in##offset##_input); \
407 device_create_file(&client->dev, &dev_attr_in##offset##_min); \
408 device_create_file(&client->dev, &dev_attr_in##offset##_max); \
409 } while (0)
410
411 #define show_fan_reg(reg) \
412 static ssize_t show_##reg (struct device *dev, char *buf, int nr) \
413 { \
414         struct w83627hf_data *data = w83627hf_update_device(dev); \
415         return sprintf(buf,"%ld\n", \
416                 FAN_FROM_REG(data->reg[nr-1], \
417                             (long)DIV_FROM_REG(data->fan_div[nr-1]))); \
418 }
419 show_fan_reg(fan)
420 show_fan_reg(fan_min)
421
422 static ssize_t
423 store_fan_min(struct device *dev, const char *buf, size_t count, int nr)
424 {
425         struct i2c_client *client = to_i2c_client(dev);
426         struct w83627hf_data *data = i2c_get_clientdata(client);
427         u32 val;
428
429         val = simple_strtoul(buf, NULL, 10);
430         data->fan_min[nr - 1] =
431             FAN_TO_REG(val, DIV_FROM_REG(data->fan_div[nr - 1]));
432         w83627hf_write_value(client, W83781D_REG_FAN_MIN(nr),
433                             data->fan_min[nr - 1]);
434
435         return count;
436 }
437
438 #define sysfs_fan_offset(offset) \
439 static ssize_t show_regs_fan_##offset (struct device *dev, char *buf) \
440 { \
441         return show_fan(dev, buf, 0x##offset); \
442 } \
443 static DEVICE_ATTR(fan##offset##_input, S_IRUGO, show_regs_fan_##offset, NULL)
444
445 #define sysfs_fan_min_offset(offset) \
446 static ssize_t show_regs_fan_min##offset (struct device *dev, char *buf) \
447 { \
448         return show_fan_min(dev, buf, 0x##offset); \
449 } \
450 static ssize_t \
451 store_regs_fan_min##offset (struct device *dev, const char *buf, size_t count) \
452 { \
453         return store_fan_min(dev, buf, count, 0x##offset); \
454 } \
455 static DEVICE_ATTR(fan##offset##_min, S_IRUGO | S_IWUSR, \
456                   show_regs_fan_min##offset, store_regs_fan_min##offset)
457
458 sysfs_fan_offset(1)
459 sysfs_fan_min_offset(1)
460 sysfs_fan_offset(2)
461 sysfs_fan_min_offset(2)
462 sysfs_fan_offset(3)
463 sysfs_fan_min_offset(3)
464
465 #define device_create_file_fan(client, offset) \
466 do { \
467 device_create_file(&client->dev, &dev_attr_fan##offset##_input); \
468 device_create_file(&client->dev, &dev_attr_fan##offset##_min); \
469 } while (0)
470
471 #define show_temp_reg(reg) \
472 static ssize_t show_##reg (struct device *dev, char *buf, int nr) \
473 { \
474         struct w83627hf_data *data = w83627hf_update_device(dev); \
475         if (nr >= 2) {  /* TEMP2 and TEMP3 */ \
476                 return sprintf(buf,"%ld\n", \
477                         (long)LM75_TEMP_FROM_REG(data->reg##_add[nr-2])); \
478         } else {        /* TEMP1 */ \
479                 return sprintf(buf,"%ld\n", (long)TEMP_FROM_REG(data->reg)); \
480         } \
481 }
482 show_temp_reg(temp)
483 show_temp_reg(temp_max)
484 show_temp_reg(temp_max_hyst)
485
486 #define store_temp_reg(REG, reg) \
487 static ssize_t \
488 store_temp_##reg (struct device *dev, const char *buf, size_t count, int nr) \
489 { \
490         struct i2c_client *client = to_i2c_client(dev); \
491         struct w83627hf_data *data = i2c_get_clientdata(client); \
492         u32 val; \
493          \
494         val = simple_strtoul(buf, NULL, 10); \
495          \
496         if (nr >= 2) {  /* TEMP2 and TEMP3 */ \
497                 data->temp_##reg##_add[nr-2] = LM75_TEMP_TO_REG(val); \
498                 w83627hf_write_value(client, W83781D_REG_TEMP_##REG(nr), \
499                                 data->temp_##reg##_add[nr-2]); \
500         } else {        /* TEMP1 */ \
501                 data->temp_##reg = TEMP_TO_REG(val); \
502                 w83627hf_write_value(client, W83781D_REG_TEMP_##REG(nr), \
503                         data->temp_##reg); \
504         } \
505          \
506         return count; \
507 }
508 store_temp_reg(OVER, max)
509 store_temp_reg(HYST, max_hyst)
510
511 #define sysfs_temp_offset(offset) \
512 static ssize_t \
513 show_regs_temp_##offset (struct device *dev, char *buf) \
514 { \
515         return show_temp(dev, buf, 0x##offset); \
516 } \
517 static DEVICE_ATTR(temp##offset##_input, S_IRUGO, show_regs_temp_##offset, NULL)
518
519 #define sysfs_temp_reg_offset(reg, offset) \
520 static ssize_t show_regs_temp_##reg##offset (struct device *dev, char *buf) \
521 { \
522         return show_temp_##reg (dev, buf, 0x##offset); \
523 } \
524 static ssize_t \
525 store_regs_temp_##reg##offset (struct device *dev, \
526                               const char *buf, size_t count) \
527 { \
528         return store_temp_##reg (dev, buf, count, 0x##offset); \
529 } \
530 static DEVICE_ATTR(temp##offset##_##reg, S_IRUGO| S_IWUSR, \
531                   show_regs_temp_##reg##offset, store_regs_temp_##reg##offset)
532
533 #define sysfs_temp_offsets(offset) \
534 sysfs_temp_offset(offset) \
535 sysfs_temp_reg_offset(max, offset) \
536 sysfs_temp_reg_offset(max_hyst, offset)
537
538 sysfs_temp_offsets(1)
539 sysfs_temp_offsets(2)
540 sysfs_temp_offsets(3)
541
542 #define device_create_file_temp(client, offset) \
543 do { \
544 device_create_file(&client->dev, &dev_attr_temp##offset##_input); \
545 device_create_file(&client->dev, &dev_attr_temp##offset##_max); \
546 device_create_file(&client->dev, &dev_attr_temp##offset##_max_hyst); \
547 } while (0)
548
549 static ssize_t
550 show_vid_reg(struct device *dev, char *buf)
551 {
552         struct w83627hf_data *data = w83627hf_update_device(dev);
553         return sprintf(buf, "%ld\n", (long) vid_from_reg(data->vid, data->vrm));
554 }
555 static DEVICE_ATTR(in0_ref, S_IRUGO, show_vid_reg, NULL)
556 #define device_create_file_vid(client) \
557 device_create_file(&client->dev, &dev_attr_in0_ref)
558
559 static ssize_t
560 show_vrm_reg(struct device *dev, char *buf)
561 {
562         struct w83627hf_data *data = w83627hf_update_device(dev);
563         return sprintf(buf, "%ld\n", (long) data->vrm);
564 }
565 static ssize_t
566 store_vrm_reg(struct device *dev, const char *buf, size_t count)
567 {
568         struct i2c_client *client = to_i2c_client(dev);
569         struct w83627hf_data *data = i2c_get_clientdata(client);
570         u32 val;
571
572         val = simple_strtoul(buf, NULL, 10);
573         data->vrm = val;
574
575         return count;
576 }
577 static DEVICE_ATTR(vrm, S_IRUGO | S_IWUSR, show_vrm_reg, store_vrm_reg)
578 #define device_create_file_vrm(client) \
579 device_create_file(&client->dev, &dev_attr_vrm)
580
581 static ssize_t
582 show_alarms_reg(struct device *dev, char *buf)
583 {
584         struct w83627hf_data *data = w83627hf_update_device(dev);
585         return sprintf(buf, "%ld\n", (long) data->alarms);
586 }
587 static DEVICE_ATTR(alarms, S_IRUGO, show_alarms_reg, NULL)
588 #define device_create_file_alarms(client) \
589 device_create_file(&client->dev, &dev_attr_alarms)
590
591 #define show_beep_reg(REG, reg) \
592 static ssize_t show_beep_##reg (struct device *dev, char *buf) \
593 { \
594         struct w83627hf_data *data = w83627hf_update_device(dev); \
595         return sprintf(buf,"%ld\n", \
596                       (long)BEEP_##REG##_FROM_REG(data->beep_##reg)); \
597 }
598 show_beep_reg(ENABLE, enable)
599 show_beep_reg(MASK, mask)
600
601 #define BEEP_ENABLE                     0       /* Store beep_enable */
602 #define BEEP_MASK                       1       /* Store beep_mask */
603
604 static ssize_t
605 store_beep_reg(struct device *dev, const char *buf, size_t count,
606                int update_mask)
607 {
608         struct i2c_client *client = to_i2c_client(dev);
609         struct w83627hf_data *data = i2c_get_clientdata(client);
610         u32 val, val2;
611
612         val = simple_strtoul(buf, NULL, 10);
613
614         if (update_mask == BEEP_MASK) { /* We are storing beep_mask */
615                 data->beep_mask = BEEP_MASK_TO_REG(val);
616                 w83627hf_write_value(client, W83781D_REG_BEEP_INTS1,
617                                     data->beep_mask & 0xff);
618                 w83627hf_write_value(client, W83781D_REG_BEEP_INTS3,
619                                     ((data->beep_mask) >> 16) & 0xff);
620                 val2 = (data->beep_mask >> 8) & 0x7f;
621         } else {                /* We are storing beep_enable */
622                 val2 =
623                     w83627hf_read_value(client, W83781D_REG_BEEP_INTS2) & 0x7f;
624                 data->beep_enable = BEEP_ENABLE_TO_REG(val);
625         }
626
627         w83627hf_write_value(client, W83781D_REG_BEEP_INTS2,
628                             val2 | data->beep_enable << 7);
629
630         return count;
631 }
632
633 #define sysfs_beep(REG, reg) \
634 static ssize_t show_regs_beep_##reg (struct device *dev, char *buf) \
635 { \
636         return show_beep_##reg(dev, buf); \
637 } \
638 static ssize_t \
639 store_regs_beep_##reg (struct device *dev, const char *buf, size_t count) \
640 { \
641         return store_beep_reg(dev, buf, count, BEEP_##REG); \
642 } \
643 static DEVICE_ATTR(beep_##reg, S_IRUGO | S_IWUSR, \
644                   show_regs_beep_##reg, store_regs_beep_##reg)
645
646 sysfs_beep(ENABLE, enable)
647 sysfs_beep(MASK, mask)
648
649 #define device_create_file_beep(client) \
650 do { \
651 device_create_file(&client->dev, &dev_attr_beep_enable); \
652 device_create_file(&client->dev, &dev_attr_beep_mask); \
653 } while (0)
654
655 static ssize_t
656 show_fan_div_reg(struct device *dev, char *buf, int nr)
657 {
658         struct w83627hf_data *data = w83627hf_update_device(dev);
659         return sprintf(buf, "%ld\n",
660                        (long) DIV_FROM_REG(data->fan_div[nr - 1]));
661 }
662
663 /* Note: we save and restore the fan minimum here, because its value is
664    determined in part by the fan divisor.  This follows the principle of
665    least suprise; the user doesn't expect the fan minimum to change just
666    because the divisor changed. */
667 static ssize_t
668 store_fan_div_reg(struct device *dev, const char *buf, size_t count, int nr)
669 {
670         struct i2c_client *client = to_i2c_client(dev);
671         struct w83627hf_data *data = i2c_get_clientdata(client);
672         unsigned long min;
673         u8 reg;
674
675         /* Save fan_min */
676         min = FAN_FROM_REG(data->fan_min[nr],
677                            DIV_FROM_REG(data->fan_div[nr]));
678
679         data->fan_div[nr] = DIV_TO_REG(simple_strtoul(buf, NULL, 10));
680
681         reg = (w83627hf_read_value(client, nr==2 ? W83781D_REG_PIN : W83781D_REG_VID_FANDIV)
682                & (nr==0 ? 0xcf : 0x3f))
683             | ((data->fan_div[nr] & 0x03) << (nr==0 ? 4 : 6));
684         w83627hf_write_value(client, nr==2 ? W83781D_REG_PIN : W83781D_REG_VID_FANDIV, reg);
685
686         reg = (w83627hf_read_value(client, W83781D_REG_VBAT)
687                & ~(1 << (5 + nr)))
688             | ((data->fan_div[nr] & 0x04) << (3 + nr));
689         w83627hf_write_value(client, W83781D_REG_VBAT, reg);
690
691         /* Restore fan_min */
692         data->fan_min[nr] = FAN_TO_REG(min, DIV_FROM_REG(data->fan_div[nr]));
693         w83627hf_write_value(client, W83781D_REG_FAN_MIN(nr+1), data->fan_min[nr]);
694
695         return count;
696 }
697
698 #define sysfs_fan_div(offset) \
699 static ssize_t show_regs_fan_div_##offset (struct device *dev, char *buf) \
700 { \
701         return show_fan_div_reg(dev, buf, offset); \
702 } \
703 static ssize_t \
704 store_regs_fan_div_##offset (struct device *dev, \
705                             const char *buf, size_t count) \
706 { \
707         return store_fan_div_reg(dev, buf, count, offset - 1); \
708 } \
709 static DEVICE_ATTR(fan##offset##_div, S_IRUGO | S_IWUSR, \
710                   show_regs_fan_div_##offset, store_regs_fan_div_##offset)
711
712 sysfs_fan_div(1)
713 sysfs_fan_div(2)
714 sysfs_fan_div(3)
715
716 #define device_create_file_fan_div(client, offset) \
717 do { \
718 device_create_file(&client->dev, &dev_attr_fan##offset##_div); \
719 } while (0)
720
721 static ssize_t
722 show_pwm_reg(struct device *dev, char *buf, int nr)
723 {
724         struct w83627hf_data *data = w83627hf_update_device(dev);
725         return sprintf(buf, "%ld\n", (long) data->pwm[nr - 1]);
726 }
727
728 static ssize_t
729 store_pwm_reg(struct device *dev, const char *buf, size_t count, int nr)
730 {
731         struct i2c_client *client = to_i2c_client(dev);
732         struct w83627hf_data *data = i2c_get_clientdata(client);
733         u32 val;
734
735         val = simple_strtoul(buf, NULL, 10);
736
737         if (data->type == w83627thf) {
738                 /* bits 0-3 are reserved  in 627THF */
739                 data->pwm[nr - 1] = PWM_TO_REG(val) & 0xf0;
740                 w83627hf_write_value(client,
741                                      W836X7HF_REG_PWM(data->type, nr),
742                                      data->pwm[nr - 1] |
743                                      (w83627hf_read_value(client,
744                                      W836X7HF_REG_PWM(data->type, nr)) & 0x0f));
745         } else {
746                 data->pwm[nr - 1] = PWM_TO_REG(val);
747                 w83627hf_write_value(client,
748                                      W836X7HF_REG_PWM(data->type, nr),
749                                      data->pwm[nr - 1]);
750         }
751
752         return count;
753 }
754
755 #define sysfs_pwm(offset) \
756 static ssize_t show_regs_pwm_##offset (struct device *dev, char *buf) \
757 { \
758         return show_pwm_reg(dev, buf, offset); \
759 } \
760 static ssize_t \
761 store_regs_pwm_##offset (struct device *dev, const char *buf, size_t count) \
762 { \
763         return store_pwm_reg(dev, buf, count, offset); \
764 } \
765 static DEVICE_ATTR(fan##offset##_pwm, S_IRUGO | S_IWUSR, \
766                   show_regs_pwm_##offset, store_regs_pwm_##offset)
767
768 sysfs_pwm(1)
769 sysfs_pwm(2)
770 sysfs_pwm(3)
771
772 #define device_create_file_pwm(client, offset) \
773 do { \
774 device_create_file(&client->dev, &dev_attr_fan##offset##_pwm); \
775 } while (0)
776
777 static ssize_t
778 show_sensor_reg(struct device *dev, char *buf, int nr)
779 {
780         struct w83627hf_data *data = w83627hf_update_device(dev);
781         return sprintf(buf, "%ld\n", (long) data->sens[nr - 1]);
782 }
783
784 static ssize_t
785 store_sensor_reg(struct device *dev, const char *buf, size_t count, int nr)
786 {
787         struct i2c_client *client = to_i2c_client(dev);
788         struct w83627hf_data *data = i2c_get_clientdata(client);
789         u32 val, tmp;
790
791         val = simple_strtoul(buf, NULL, 10);
792
793         switch (val) {
794         case 1:         /* PII/Celeron diode */
795                 tmp = w83627hf_read_value(client, W83781D_REG_SCFG1);
796                 w83627hf_write_value(client, W83781D_REG_SCFG1,
797                                     tmp | BIT_SCFG1[nr - 1]);
798                 tmp = w83627hf_read_value(client, W83781D_REG_SCFG2);
799                 w83627hf_write_value(client, W83781D_REG_SCFG2,
800                                     tmp | BIT_SCFG2[nr - 1]);
801                 data->sens[nr - 1] = val;
802                 break;
803         case 2:         /* 3904 */
804                 tmp = w83627hf_read_value(client, W83781D_REG_SCFG1);
805                 w83627hf_write_value(client, W83781D_REG_SCFG1,
806                                     tmp | BIT_SCFG1[nr - 1]);
807                 tmp = w83627hf_read_value(client, W83781D_REG_SCFG2);
808                 w83627hf_write_value(client, W83781D_REG_SCFG2,
809                                     tmp & ~BIT_SCFG2[nr - 1]);
810                 data->sens[nr - 1] = val;
811                 break;
812         case W83781D_DEFAULT_BETA:      /* thermistor */
813                 tmp = w83627hf_read_value(client, W83781D_REG_SCFG1);
814                 w83627hf_write_value(client, W83781D_REG_SCFG1,
815                                     tmp & ~BIT_SCFG1[nr - 1]);
816                 data->sens[nr - 1] = val;
817                 break;
818         default:
819                 dev_err(&client->dev,
820                        "Invalid sensor type %ld; must be 1, 2, or %d\n",
821                        (long) val, W83781D_DEFAULT_BETA);
822                 break;
823         }
824
825         return count;
826 }
827
828 #define sysfs_sensor(offset) \
829 static ssize_t show_regs_sensor_##offset (struct device *dev, char *buf) \
830 { \
831     return show_sensor_reg(dev, buf, offset); \
832 } \
833 static ssize_t \
834 store_regs_sensor_##offset (struct device *dev, const char *buf, size_t count) \
835 { \
836     return store_sensor_reg(dev, buf, count, offset); \
837 } \
838 static DEVICE_ATTR(temp##offset##_type, S_IRUGO | S_IWUSR, \
839                   show_regs_sensor_##offset, store_regs_sensor_##offset)
840
841 sysfs_sensor(1)
842 sysfs_sensor(2)
843 sysfs_sensor(3)
844
845 #define device_create_file_sensor(client, offset) \
846 do { \
847 device_create_file(&client->dev, &dev_attr_temp##offset##_type); \
848 } while (0)
849
850
851 /* This function is called when:
852      * w83627hf_driver is inserted (when this module is loaded), for each
853        available adapter
854      * when a new adapter is inserted (and w83627hf_driver is still present) */
855 static int w83627hf_attach_adapter(struct i2c_adapter *adapter)
856 {
857         return i2c_detect(adapter, &addr_data, w83627hf_detect);
858 }
859
860 static int w83627hf_find(int *address)
861 {
862         u16 val;
863
864         superio_enter();
865         val= superio_inb(DEVID);
866         if(val != W627_DEVID &&
867            val != W627THF_DEVID &&
868            val != W697_DEVID &&
869            val != W637_DEVID) {
870                 superio_exit();
871                 return -ENODEV;
872         }
873
874         superio_select(W83627HF_LD_HWM);
875         val = (superio_inb(WINB_BASE_REG) << 8) |
876                superio_inb(WINB_BASE_REG + 1);
877         *address = val & ~(WINB_EXTENT - 1);
878         if (*address == 0 && force_addr == 0) {
879                 superio_exit();
880                 return -ENODEV;
881         }
882         if (force_addr)
883                 *address = force_addr;  /* so detect will get called */
884
885         superio_exit();
886         return 0;
887 }
888
889 int w83627hf_detect(struct i2c_adapter *adapter, int address,
890                    int kind)
891 {
892         int val;
893         struct i2c_client *new_client;
894         struct w83627hf_data *data;
895         int err = 0;
896         const char *client_name = "";
897
898         if (!i2c_is_isa_adapter(adapter)) {
899                 err = -ENODEV;
900                 goto ERROR0;
901         }
902
903         if(force_addr)
904                 address = force_addr & ~(WINB_EXTENT - 1);
905
906         if (!request_region(address, WINB_EXTENT, "w83627hf")) {
907                 err = -EBUSY;
908                 goto ERROR0;
909         }
910
911         if(force_addr) {
912                 printk("w83627hf.o: forcing ISA address 0x%04X\n", address);
913                 superio_enter();
914                 superio_select(W83627HF_LD_HWM);
915                 superio_outb(WINB_BASE_REG, address >> 8);
916                 superio_outb(WINB_BASE_REG+1, address & 0xff);
917                 superio_exit();
918         }
919
920         superio_enter();
921         val= superio_inb(DEVID);
922         if(val == W627_DEVID)
923                 kind = w83627hf;
924         else if(val == W697_DEVID)
925                 kind = w83697hf;
926         else if(val == W627THF_DEVID)
927                 kind = w83627thf;
928         else if(val == W637_DEVID)
929                 kind = w83637hf;
930         else {
931                 dev_info(&adapter->dev,
932                          "Unsupported chip (dev_id=0x%02X).\n", val);
933                 goto ERROR1;
934         }
935
936         superio_select(W83627HF_LD_HWM);
937         if((val = 0x01 & superio_inb(WINB_ACT_REG)) == 0)
938                 superio_outb(WINB_ACT_REG, 1);
939         superio_exit();
940
941         /* OK. For now, we presume we have a valid client. We now create the
942            client structure, even though we cannot fill it completely yet.
943            But it allows us to access w83627hf_{read,write}_value. */
944
945         if (!(data = kmalloc(sizeof(struct w83627hf_data), GFP_KERNEL))) {
946                 err = -ENOMEM;
947                 goto ERROR1;
948         }
949         memset(data, 0, sizeof(struct w83627hf_data));
950
951         new_client = &data->client;
952         i2c_set_clientdata(new_client, data);
953         new_client->addr = address;
954         init_MUTEX(&data->lock);
955         new_client->adapter = adapter;
956         new_client->driver = &w83627hf_driver;
957         new_client->flags = 0;
958
959
960         if (kind == w83627hf) {
961                 client_name = "w83627hf";
962         } else if (kind == w83627thf) {
963                 client_name = "w83627thf";
964         } else if (kind == w83697hf) {
965                 client_name = "w83697hf";
966         } else if (kind == w83637hf) {
967                 client_name = "w83637hf";
968         }
969
970         /* Fill in the remaining client fields and put into the global list */
971         strlcpy(new_client->name, client_name, I2C_NAME_SIZE);
972         data->type = kind;
973         data->valid = 0;
974         init_MUTEX(&data->update_lock);
975
976         /* Tell the I2C layer a new client has arrived */
977         if ((err = i2c_attach_client(new_client)))
978                 goto ERROR2;
979
980         data->lm75 = NULL;
981
982         /* Initialize the chip */
983         w83627hf_init_client(new_client);
984
985         /* A few vars need to be filled upon startup */
986         data->fan_min[0] = w83627hf_read_value(new_client, W83781D_REG_FAN_MIN(1));
987         data->fan_min[1] = w83627hf_read_value(new_client, W83781D_REG_FAN_MIN(2));
988         data->fan_min[2] = w83627hf_read_value(new_client, W83781D_REG_FAN_MIN(3));
989
990         /* Register sysfs hooks */
991         device_create_file_in(new_client, 0);
992         if (kind != w83697hf)
993                 device_create_file_in(new_client, 1);
994         device_create_file_in(new_client, 2);
995         device_create_file_in(new_client, 3);
996         device_create_file_in(new_client, 4);
997         if (kind != w83627thf && kind != w83637hf) {
998                 device_create_file_in(new_client, 5);
999                 device_create_file_in(new_client, 6);
1000         }
1001         device_create_file_in(new_client, 7);
1002         device_create_file_in(new_client, 8);
1003
1004         device_create_file_fan(new_client, 1);
1005         device_create_file_fan(new_client, 2);
1006         if (kind != w83697hf)
1007                 device_create_file_fan(new_client, 3);
1008
1009         device_create_file_temp(new_client, 1);
1010         device_create_file_temp(new_client, 2);
1011         if (kind != w83697hf)
1012                 device_create_file_temp(new_client, 3);
1013
1014         if (kind != w83697hf)
1015                 device_create_file_vid(new_client);
1016
1017         if (kind != w83697hf)
1018                 device_create_file_vrm(new_client);
1019
1020         device_create_file_fan_div(new_client, 1);
1021         device_create_file_fan_div(new_client, 2);
1022         if (kind != w83697hf)
1023                 device_create_file_fan_div(new_client, 3);
1024
1025         device_create_file_alarms(new_client);
1026
1027         device_create_file_beep(new_client);
1028
1029         device_create_file_pwm(new_client, 1);
1030         device_create_file_pwm(new_client, 2);
1031         if (kind == w83627thf || kind == w83637hf)
1032                 device_create_file_pwm(new_client, 3);
1033
1034         device_create_file_sensor(new_client, 1);
1035         device_create_file_sensor(new_client, 2);
1036         if (kind != w83697hf)
1037                 device_create_file_sensor(new_client, 3);
1038
1039         return 0;
1040
1041       ERROR2:
1042         kfree(data);
1043       ERROR1:
1044         release_region(address, WINB_EXTENT);
1045       ERROR0:
1046         return err;
1047 }
1048
1049 static int w83627hf_detach_client(struct i2c_client *client)
1050 {
1051         int err;
1052
1053         if ((err = i2c_detach_client(client))) {
1054                 dev_err(&client->dev,
1055                        "Client deregistration failed, client not detached.\n");
1056                 return err;
1057         }
1058
1059         release_region(client->addr, WINB_EXTENT);
1060         kfree(i2c_get_clientdata(client));
1061
1062         return 0;
1063 }
1064
1065
1066 /*
1067    ISA access must always be locked explicitly!
1068    We ignore the W83781D BUSY flag at this moment - it could lead to deadlocks,
1069    would slow down the W83781D access and should not be necessary.
1070    There are some ugly typecasts here, but the good news is - they should
1071    nowhere else be necessary! */
1072 static int w83627hf_read_value(struct i2c_client *client, u16 reg)
1073 {
1074         struct w83627hf_data *data = i2c_get_clientdata(client);
1075         int res, word_sized;
1076
1077         down(&data->lock);
1078         word_sized = (((reg & 0xff00) == 0x100)
1079                    || ((reg & 0xff00) == 0x200))
1080                   && (((reg & 0x00ff) == 0x50)
1081                    || ((reg & 0x00ff) == 0x53)
1082                    || ((reg & 0x00ff) == 0x55));
1083         if (reg & 0xff00) {
1084                 outb_p(W83781D_REG_BANK,
1085                        client->addr + W83781D_ADDR_REG_OFFSET);
1086                 outb_p(reg >> 8,
1087                        client->addr + W83781D_DATA_REG_OFFSET);
1088         }
1089         outb_p(reg & 0xff, client->addr + W83781D_ADDR_REG_OFFSET);
1090         res = inb_p(client->addr + W83781D_DATA_REG_OFFSET);
1091         if (word_sized) {
1092                 outb_p((reg & 0xff) + 1,
1093                        client->addr + W83781D_ADDR_REG_OFFSET);
1094                 res =
1095                     (res << 8) + inb_p(client->addr +
1096                                        W83781D_DATA_REG_OFFSET);
1097         }
1098         if (reg & 0xff00) {
1099                 outb_p(W83781D_REG_BANK,
1100                        client->addr + W83781D_ADDR_REG_OFFSET);
1101                 outb_p(0, client->addr + W83781D_DATA_REG_OFFSET);
1102         }
1103         up(&data->lock);
1104         return res;
1105 }
1106
1107 static int w83627thf_read_gpio5(struct i2c_client *client)
1108 {
1109         struct w83627hf_data *data = i2c_get_clientdata(client);
1110         int res, inv;
1111
1112         down(&data->lock);
1113         superio_enter();
1114         superio_select(W83627HF_LD_GPIO5);
1115         res = superio_inb(W83627THF_GPIO5_DR);
1116         inv = superio_inb(W83627THF_GPIO5_INVR);
1117         superio_exit();
1118         up(&data->lock);
1119         return res;
1120 }
1121
1122 static int w83627hf_write_value(struct i2c_client *client, u16 reg, u16 value)
1123 {
1124         struct w83627hf_data *data = i2c_get_clientdata(client);
1125         int word_sized;
1126
1127         down(&data->lock);
1128         word_sized = (((reg & 0xff00) == 0x100)
1129                    || ((reg & 0xff00) == 0x200))
1130                   && (((reg & 0x00ff) == 0x53)
1131                    || ((reg & 0x00ff) == 0x55));
1132         if (reg & 0xff00) {
1133                 outb_p(W83781D_REG_BANK,
1134                        client->addr + W83781D_ADDR_REG_OFFSET);
1135                 outb_p(reg >> 8,
1136                        client->addr + W83781D_DATA_REG_OFFSET);
1137         }
1138         outb_p(reg & 0xff, client->addr + W83781D_ADDR_REG_OFFSET);
1139         if (word_sized) {
1140                 outb_p(value >> 8,
1141                        client->addr + W83781D_DATA_REG_OFFSET);
1142                 outb_p((reg & 0xff) + 1,
1143                        client->addr + W83781D_ADDR_REG_OFFSET);
1144         }
1145         outb_p(value & 0xff,
1146                client->addr + W83781D_DATA_REG_OFFSET);
1147         if (reg & 0xff00) {
1148                 outb_p(W83781D_REG_BANK,
1149                        client->addr + W83781D_ADDR_REG_OFFSET);
1150                 outb_p(0, client->addr + W83781D_DATA_REG_OFFSET);
1151         }
1152         up(&data->lock);
1153         return 0;
1154 }
1155
1156 /* Called when we have found a new W83781D. It should set limits, etc. */
1157 static void w83627hf_init_client(struct i2c_client *client)
1158 {
1159         struct w83627hf_data *data = i2c_get_clientdata(client);
1160         int vid = 0, i;
1161         int type = data->type;
1162         u8 tmp;
1163
1164         if(init) {
1165                 /* save this register */
1166                 i = w83627hf_read_value(client, W83781D_REG_BEEP_CONFIG);
1167                 /* Reset all except Watchdog values and last conversion values
1168                    This sets fan-divs to 2, among others */
1169                 w83627hf_write_value(client, W83781D_REG_CONFIG, 0x80);
1170                 /* Restore the register and disable power-on abnormal beep.
1171                    This saves FAN 1/2/3 input/output values set by BIOS. */
1172                 w83627hf_write_value(client, W83781D_REG_BEEP_CONFIG, i | 0x80);
1173                 /* Disable master beep-enable (reset turns it on).
1174                    Individual beeps should be reset to off but for some reason
1175                    disabling this bit helps some people not get beeped */
1176                 w83627hf_write_value(client, W83781D_REG_BEEP_INTS2, 0);
1177         }
1178
1179         /* Minimize conflicts with other winbond i2c-only clients...  */
1180         /* disable i2c subclients... how to disable main i2c client?? */
1181         /* force i2c address to relatively uncommon address */
1182         w83627hf_write_value(client, W83781D_REG_I2C_SUBADDR, 0x89);
1183         w83627hf_write_value(client, W83781D_REG_I2C_ADDR, force_i2c);
1184
1185         /* Read VID only once */
1186         if (w83627hf == data->type || w83637hf == data->type) {
1187                 int lo = w83627hf_read_value(client, W83781D_REG_VID_FANDIV);
1188                 int hi = w83627hf_read_value(client, W83781D_REG_CHIPID);
1189                 data->vid = (lo & 0x0f) | ((hi & 0x01) << 4);
1190         } else if (w83627thf == data->type) {
1191                 data->vid = w83627thf_read_gpio5(client) & 0x1f;
1192         }
1193
1194         /* Convert VID to voltage based on default VRM */
1195         data->vrm = DEFAULT_VRM;
1196         if (type != w83697hf)
1197                 vid = vid_from_reg(vid, data->vrm);
1198
1199         tmp = w83627hf_read_value(client, W83781D_REG_SCFG1);
1200         for (i = 1; i <= 3; i++) {
1201                 if (!(tmp & BIT_SCFG1[i - 1])) {
1202                         data->sens[i - 1] = W83781D_DEFAULT_BETA;
1203                 } else {
1204                         if (w83627hf_read_value
1205                             (client,
1206                              W83781D_REG_SCFG2) & BIT_SCFG2[i - 1])
1207                                 data->sens[i - 1] = 1;
1208                         else
1209                                 data->sens[i - 1] = 2;
1210                 }
1211                 if ((type == w83697hf) && (i == 2))
1212                         break;
1213         }
1214
1215         data->pwmenable[0] = 1;
1216         data->pwmenable[1] = 1;
1217         data->pwmenable[2] = 1;
1218
1219         if(init) {
1220                 if (type == w83627hf) {
1221                         /* enable PWM2 control (can't hurt since PWM reg
1222                            should have been reset to 0xff) */
1223                         w83627hf_write_value(client, W83627HF_REG_PWMCLK12,
1224                                             0x19);
1225                 }
1226                 /* enable comparator mode for temp2 and temp3 so
1227                    alarm indication will work correctly */
1228                 i = w83627hf_read_value(client, W83781D_REG_IRQ);
1229                 if (!(i & 0x40))
1230                         w83627hf_write_value(client, W83781D_REG_IRQ,
1231                                             i | 0x40);
1232         }
1233
1234         /* Start monitoring */
1235         w83627hf_write_value(client, W83781D_REG_CONFIG,
1236                             (w83627hf_read_value(client,
1237                                                 W83781D_REG_CONFIG) & 0xf7)
1238                             | 0x01);
1239 }
1240
1241 static struct w83627hf_data *w83627hf_update_device(struct device *dev)
1242 {
1243         struct i2c_client *client = to_i2c_client(dev);
1244         struct w83627hf_data *data = i2c_get_clientdata(client);
1245         int i;
1246
1247         down(&data->update_lock);
1248
1249         if ((jiffies - data->last_updated > HZ + HZ / 2) ||
1250             (jiffies < data->last_updated) || !data->valid) {
1251                 for (i = 0; i <= 8; i++) {
1252                         /* skip missing sensors */
1253                         if (((data->type == w83697hf) && (i == 1)) ||
1254                             ((data->type == w83627thf || data->type == w83637hf)
1255                             && (i == 4 || i == 5)))
1256                                 continue;
1257                         data->in[i] =
1258                             w83627hf_read_value(client, W83781D_REG_IN(i));
1259                         data->in_min[i] =
1260                             w83627hf_read_value(client,
1261                                                W83781D_REG_IN_MIN(i));
1262                         data->in_max[i] =
1263                             w83627hf_read_value(client,
1264                                                W83781D_REG_IN_MAX(i));
1265                 }
1266                 for (i = 1; i <= 3; i++) {
1267                         data->fan[i - 1] =
1268                             w83627hf_read_value(client, W83781D_REG_FAN(i));
1269                         data->fan_min[i - 1] =
1270                             w83627hf_read_value(client,
1271                                                W83781D_REG_FAN_MIN(i));
1272                 }
1273                 for (i = 1; i <= 3; i++) {
1274                         u8 tmp = w83627hf_read_value(client,
1275                                 W836X7HF_REG_PWM(data->type, i));
1276                         /* bits 0-3 are reserved  in 627THF */
1277                         if (data->type == w83627thf)
1278                                 tmp &= 0xf0;
1279                         data->pwm[i - 1] = tmp;
1280                         if(i == 2 &&
1281                            (data->type == w83627hf || data->type == w83697hf))
1282                                 break;
1283                 }
1284
1285                 data->temp = w83627hf_read_value(client, W83781D_REG_TEMP(1));
1286                 data->temp_max =
1287                     w83627hf_read_value(client, W83781D_REG_TEMP_OVER(1));
1288                 data->temp_max_hyst =
1289                     w83627hf_read_value(client, W83781D_REG_TEMP_HYST(1));
1290                 data->temp_add[0] =
1291                     w83627hf_read_value(client, W83781D_REG_TEMP(2));
1292                 data->temp_max_add[0] =
1293                     w83627hf_read_value(client, W83781D_REG_TEMP_OVER(2));
1294                 data->temp_max_hyst_add[0] =
1295                     w83627hf_read_value(client, W83781D_REG_TEMP_HYST(2));
1296                 if (data->type != w83697hf) {
1297                         data->temp_add[1] =
1298                           w83627hf_read_value(client, W83781D_REG_TEMP(3));
1299                         data->temp_max_add[1] =
1300                           w83627hf_read_value(client, W83781D_REG_TEMP_OVER(3));
1301                         data->temp_max_hyst_add[1] =
1302                           w83627hf_read_value(client, W83781D_REG_TEMP_HYST(3));
1303                 }
1304
1305                 i = w83627hf_read_value(client, W83781D_REG_VID_FANDIV);
1306                 data->fan_div[0] = (i >> 4) & 0x03;
1307                 data->fan_div[1] = (i >> 6) & 0x03;
1308                 if (data->type != w83697hf) {
1309                         data->fan_div[2] = (w83627hf_read_value(client,
1310                                                W83781D_REG_PIN) >> 6) & 0x03;
1311                 }
1312                 i = w83627hf_read_value(client, W83781D_REG_VBAT);
1313                 data->fan_div[0] |= (i >> 3) & 0x04;
1314                 data->fan_div[1] |= (i >> 4) & 0x04;
1315                 if (data->type != w83697hf)
1316                         data->fan_div[2] |= (i >> 5) & 0x04;
1317                 data->alarms =
1318                     w83627hf_read_value(client, W83781D_REG_ALARM1) |
1319                     (w83627hf_read_value(client, W83781D_REG_ALARM2) << 8) |
1320                     (w83627hf_read_value(client, W83781D_REG_ALARM3) << 16);
1321                 i = w83627hf_read_value(client, W83781D_REG_BEEP_INTS2);
1322                 data->beep_enable = i >> 7;
1323                 data->beep_mask = ((i & 0x7f) << 8) |
1324                     w83627hf_read_value(client, W83781D_REG_BEEP_INTS1) |
1325                     w83627hf_read_value(client, W83781D_REG_BEEP_INTS3) << 16;
1326                 data->last_updated = jiffies;
1327                 data->valid = 1;
1328         }
1329
1330         up(&data->update_lock);
1331
1332         return data;
1333 }
1334
1335 static int __init sensors_w83627hf_init(void)
1336 {
1337         int addr;
1338
1339         if (w83627hf_find(&addr)) {
1340                 return -ENODEV;
1341         }
1342         normal_isa[0] = addr;
1343
1344         return i2c_add_driver(&w83627hf_driver);
1345 }
1346
1347 static void __exit sensors_w83627hf_exit(void)
1348 {
1349         i2c_del_driver(&w83627hf_driver);
1350 }
1351
1352 MODULE_AUTHOR("Frodo Looijaard <frodol@dds.nl>, "
1353               "Philip Edelbrock <phil@netroedge.com>, "
1354               "and Mark Studebaker <mdsxyz123@yahoo.com>");
1355 MODULE_DESCRIPTION("W83627HF driver");
1356 MODULE_LICENSE("GPL");
1357
1358 module_init(sensors_w83627hf_init);
1359 module_exit(sensors_w83627hf_exit);