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