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