patch-2_6_7-vs1_9_1_12
[linux-2.6.git] / drivers / i2c / chips / w83781d.c
1 /*
2     w83781d.c - Part of lm_sensors, Linux kernel modules for hardware
3                 monitoring
4     Copyright (c) 1998 - 2001  Frodo Looijaard <frodol@dds.nl>,
5     Philip Edelbrock <phil@netroedge.com>,
6     and Mark Studebaker <mdsxyz123@yahoo.com>
7
8     This program is free software; you can redistribute it and/or modify
9     it under the terms of the GNU General Public License as published by
10     the Free Software Foundation; either version 2 of the License, or
11     (at your option) any later version.
12
13     This program is distributed in the hope that it will be useful,
14     but WITHOUT ANY WARRANTY; without even the implied warranty of
15     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16     GNU General Public License for more details.
17
18     You should have received a copy of the GNU General Public License
19     along with this program; if not, write to the Free Software
20     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 */
22
23 /*
24     Supports following chips:
25
26     Chip        #vin    #fanin  #pwm    #temp   wchipid vendid  i2c     ISA
27     as99127f    7       3       0       3       0x31    0x12c3  yes     no
28     as99127f rev.2 (type_name = as99127f)       0x31    0x5ca3  yes     no
29     w83781d     7       3       0       3       0x10-1  0x5ca3  yes     yes
30     w83627hf    9       3       2       3       0x21    0x5ca3  yes     yes(LPC)
31     w83627thf   9       3       2       3       0x90    0x5ca3  no      yes(LPC)
32     w83782d     9       3       2-4     3       0x30    0x5ca3  yes     yes
33     w83783s     5-6     3       2       1-2     0x40    0x5ca3  yes     no
34     w83697hf    8       2       2       2       0x60    0x5ca3  no      yes(LPC)
35
36 */
37
38 #include <linux/config.h>
39 #include <linux/module.h>
40 #include <linux/init.h>
41 #include <linux/slab.h>
42 #include <linux/i2c.h>
43 #include <linux/i2c-sensor.h>
44 #include <linux/i2c-vid.h>
45 #include <asm/io.h>
46 #include "lm75.h"
47
48 /* RT Table support #defined so we can take it out if it gets bothersome */
49 #define W83781D_RT                      1
50
51 /* Addresses to scan */
52 static unsigned short normal_i2c[] = { I2C_CLIENT_END };
53 static unsigned short normal_i2c_range[] = { 0x20, 0x2f, I2C_CLIENT_END };
54 static unsigned int normal_isa[] = { 0x0290, I2C_CLIENT_ISA_END };
55 static unsigned int normal_isa_range[] = { I2C_CLIENT_ISA_END };
56
57 /* Insmod parameters */
58 SENSORS_INSMOD_6(w83781d, w83782d, w83783s, w83627hf, as99127f, w83697hf);
59 I2C_CLIENT_MODULE_PARM(force_subclients, "List of subclient addresses: "
60                     "{bus, clientaddr, subclientaddr1, subclientaddr2}");
61
62 static int init = 1;
63 MODULE_PARM(init, "i");
64 MODULE_PARM_DESC(init, "Set to zero to bypass chip initialization");
65
66 /* Constants specified below */
67
68 /* Length of ISA address segment */
69 #define W83781D_EXTENT                  8
70
71 /* Where are the ISA address/data registers relative to the base address */
72 #define W83781D_ADDR_REG_OFFSET         5
73 #define W83781D_DATA_REG_OFFSET         6
74
75 /* The W83781D registers */
76 /* The W83782D registers for nr=7,8 are in bank 5 */
77 #define W83781D_REG_IN_MAX(nr)          ((nr < 7) ? (0x2b + (nr) * 2) : \
78                                                     (0x554 + (((nr) - 7) * 2)))
79 #define W83781D_REG_IN_MIN(nr)          ((nr < 7) ? (0x2c + (nr) * 2) : \
80                                                     (0x555 + (((nr) - 7) * 2)))
81 #define W83781D_REG_IN(nr)              ((nr < 7) ? (0x20 + (nr)) : \
82                                                     (0x550 + (nr) - 7))
83
84 #define W83781D_REG_FAN_MIN(nr)         (0x3a + (nr))
85 #define W83781D_REG_FAN(nr)             (0x27 + (nr))
86
87 #define W83781D_REG_BANK                0x4E
88 #define W83781D_REG_TEMP2_CONFIG        0x152
89 #define W83781D_REG_TEMP3_CONFIG        0x252
90 #define W83781D_REG_TEMP(nr)            ((nr == 3) ? (0x0250) : \
91                                         ((nr == 2) ? (0x0150) : \
92                                                      (0x27)))
93 #define W83781D_REG_TEMP_HYST(nr)       ((nr == 3) ? (0x253) : \
94                                         ((nr == 2) ? (0x153) : \
95                                                      (0x3A)))
96 #define W83781D_REG_TEMP_OVER(nr)       ((nr == 3) ? (0x255) : \
97                                         ((nr == 2) ? (0x155) : \
98                                                      (0x39)))
99
100 #define W83781D_REG_CONFIG              0x40
101 #define W83781D_REG_ALARM1              0x41
102 #define W83781D_REG_ALARM2              0x42
103 #define W83781D_REG_ALARM3              0x450   /* not on W83781D */
104
105 #define W83781D_REG_IRQ                 0x4C
106 #define W83781D_REG_BEEP_CONFIG         0x4D
107 #define W83781D_REG_BEEP_INTS1          0x56
108 #define W83781D_REG_BEEP_INTS2          0x57
109 #define W83781D_REG_BEEP_INTS3          0x453   /* not on W83781D */
110
111 #define W83781D_REG_VID_FANDIV          0x47
112
113 #define W83781D_REG_CHIPID              0x49
114 #define W83781D_REG_WCHIPID             0x58
115 #define W83781D_REG_CHIPMAN             0x4F
116 #define W83781D_REG_PIN                 0x4B
117
118 /* 782D/783S only */
119 #define W83781D_REG_VBAT                0x5D
120
121 /* PWM 782D (1-4) and 783S (1-2) only */
122 #define W83781D_REG_PWM1                0x5B    /* 782d and 783s/627hf datasheets disagree */
123                                                 /* on which is which; */
124 #define W83781D_REG_PWM2                0x5A    /* We follow the 782d convention here, */
125                                                 /* However 782d is probably wrong. */
126 #define W83781D_REG_PWM3                0x5E
127 #define W83781D_REG_PWM4                0x5F
128 #define W83781D_REG_PWMCLK12            0x5C
129 #define W83781D_REG_PWMCLK34            0x45C
130 static const u8 regpwm[] = { W83781D_REG_PWM1, W83781D_REG_PWM2,
131         W83781D_REG_PWM3, W83781D_REG_PWM4
132 };
133
134 #define W83781D_REG_PWM(nr)             (regpwm[(nr) - 1])
135
136 #define W83781D_REG_I2C_ADDR            0x48
137 #define W83781D_REG_I2C_SUBADDR         0x4A
138
139 /* The following are undocumented in the data sheets however we
140    received the information in an email from Winbond tech support */
141 /* Sensor selection - not on 781d */
142 #define W83781D_REG_SCFG1               0x5D
143 static const u8 BIT_SCFG1[] = { 0x02, 0x04, 0x08 };
144
145 #define W83781D_REG_SCFG2               0x59
146 static const u8 BIT_SCFG2[] = { 0x10, 0x20, 0x40 };
147
148 #define W83781D_DEFAULT_BETA            3435
149
150 /* RT Table registers */
151 #define W83781D_REG_RT_IDX              0x50
152 #define W83781D_REG_RT_VAL              0x51
153
154 /* Conversions. Rounding and limit checking is only done on the TO_REG
155    variants. Note that you should be a bit careful with which arguments
156    these macros are called: arguments may be evaluated more than once.
157    Fixing this is just not worth it. */
158 #define IN_TO_REG(val)                  (SENSORS_LIMIT((((val) * 10 + 8)/16),0,255))
159 #define IN_FROM_REG(val)                (((val) * 16) / 10)
160
161 static inline u8
162 FAN_TO_REG(long rpm, int div)
163 {
164         if (rpm == 0)
165                 return 255;
166         rpm = SENSORS_LIMIT(rpm, 1, 1000000);
167         return SENSORS_LIMIT((1350000 + rpm * div / 2) / (rpm * div), 1, 254);
168 }
169
170 #define FAN_FROM_REG(val,div)           ((val) == 0   ? -1 : \
171                                         ((val) == 255 ? 0 : \
172                                                         1350000 / ((val) * (div))))
173
174 #define TEMP_TO_REG(val)                (SENSORS_LIMIT(((val) < 0 ? (val)+0x100*1000 \
175                                                 : (val)) / 1000, 0, 0xff))
176 #define TEMP_FROM_REG(val)              (((val) & 0x80 ? (val)-0x100 : (val)) * 1000)
177
178 #define AS99127_TEMP_ADD_TO_REG(val)    (SENSORS_LIMIT((((val) < 0 ? (val)+0x10000*250 \
179                                                 : (val)) / 250) << 7, 0, 0xffff))
180 #define AS99127_TEMP_ADD_FROM_REG(val)  ((((val) & 0x8000 ? (val)-0x10000 : (val)) \
181                                                 >> 7) * 250)
182
183 #define ALARMS_FROM_REG(val)            (val)
184 #define PWM_FROM_REG(val)               (val)
185 #define PWM_TO_REG(val)                 (SENSORS_LIMIT((val),0,255))
186 #define BEEP_MASK_FROM_REG(val,type)    ((type) == as99127f ? \
187                                          (val) ^ 0x7fff : (val))
188 #define BEEP_MASK_TO_REG(val,type)      ((type) == as99127f ? \
189                                          (~(val)) & 0x7fff : (val) & 0xffffff)
190
191 #define BEEP_ENABLE_TO_REG(val)         ((val) ? 1 : 0)
192 #define BEEP_ENABLE_FROM_REG(val)       ((val) ? 1 : 0)
193
194 #define DIV_FROM_REG(val)               (1 << (val))
195
196 static inline u8
197 DIV_TO_REG(long val, enum chips type)
198 {
199         int i;
200         val = SENSORS_LIMIT(val, 1,
201                             ((type == w83781d
202                               || type == as99127f) ? 8 : 128)) >> 1;
203         for (i = 0; i < 6; i++) {
204                 if (val == 0)
205                         break;
206                 val >>= 1;
207         }
208         return ((u8) i);
209 }
210
211 /* There are some complications in a module like this. First off, W83781D chips
212    may be both present on the SMBus and the ISA bus, and we have to handle
213    those cases separately at some places. Second, there might be several
214    W83781D chips available (well, actually, that is probably never done; but
215    it is a clean illustration of how to handle a case like that). Finally,
216    a specific chip may be attached to *both* ISA and SMBus, and we would
217    not like to detect it double. Fortunately, in the case of the W83781D at
218    least, a register tells us what SMBus address we are on, so that helps
219    a bit - except if there could be more than one SMBus. Groan. No solution
220    for this yet. */
221
222 /* This module may seem overly long and complicated. In fact, it is not so
223    bad. Quite a lot of bookkeeping is done. A real driver can often cut
224    some corners. */
225
226 /* For each registered W83781D, we need to keep some data in memory. That
227    data is pointed to by w83781d_list[NR]->data. The structure itself is
228    dynamically allocated, at the same time when a new w83781d client is
229    allocated. */
230 struct w83781d_data {
231         struct i2c_client client;
232         struct semaphore lock;
233         enum chips type;
234
235         struct semaphore update_lock;
236         char valid;             /* !=0 if following fields are valid */
237         unsigned long last_updated;     /* In jiffies */
238
239         struct i2c_client *lm75[2];     /* for secondary I2C addresses */
240         /* array of 2 pointers to subclients */
241
242         u8 in[9];               /* Register value - 8 & 9 for 782D only */
243         u8 in_max[9];           /* Register value - 8 & 9 for 782D only */
244         u8 in_min[9];           /* Register value - 8 & 9 for 782D only */
245         u8 fan[3];              /* Register value */
246         u8 fan_min[3];          /* Register value */
247         u8 temp;
248         u8 temp_max;            /* Register value */
249         u8 temp_max_hyst;       /* Register value */
250         u16 temp_add[2];        /* Register value */
251         u16 temp_max_add[2];    /* Register value */
252         u16 temp_max_hyst_add[2];       /* Register value */
253         u8 fan_div[3];          /* Register encoding, shifted right */
254         u8 vid;                 /* Register encoding, combined */
255         u32 alarms;             /* Register encoding, combined */
256         u32 beep_mask;          /* Register encoding, combined */
257         u8 beep_enable;         /* Boolean */
258         u8 pwm[4];              /* Register value */
259         u8 pwmenable[4];        /* Boolean */
260         u16 sens[3];            /* 782D/783S only.
261                                    1 = pentium diode; 2 = 3904 diode;
262                                    3000-5000 = thermistor beta.
263                                    Default = 3435. 
264                                    Other Betas unimplemented */
265 #ifdef W83781D_RT
266         u8 rt[3][32];           /* Register value */
267 #endif
268         u8 vrm;
269 };
270
271 static int w83781d_attach_adapter(struct i2c_adapter *adapter);
272 static int w83781d_detect(struct i2c_adapter *adapter, int address, int kind);
273 static int w83781d_detach_client(struct i2c_client *client);
274
275 static int w83781d_read_value(struct i2c_client *client, u16 register);
276 static int w83781d_write_value(struct i2c_client *client, u16 register,
277                                u16 value);
278 static struct w83781d_data *w83781d_update_device(struct device *dev);
279 static void w83781d_init_client(struct i2c_client *client);
280
281 static struct i2c_driver w83781d_driver = {
282         .owner = THIS_MODULE,
283         .name = "w83781d",
284         .id = I2C_DRIVERID_W83781D,
285         .flags = I2C_DF_NOTIFY,
286         .attach_adapter = w83781d_attach_adapter,
287         .detach_client = w83781d_detach_client,
288 };
289
290 /* following are the sysfs callback functions */
291 #define show_in_reg(reg) \
292 static ssize_t show_##reg (struct device *dev, char *buf, int nr) \
293 { \
294         struct w83781d_data *data = w83781d_update_device(dev); \
295         return sprintf(buf,"%ld\n", (long)IN_FROM_REG(data->reg[nr] * 10)); \
296 }
297 show_in_reg(in);
298 show_in_reg(in_min);
299 show_in_reg(in_max);
300
301 #define store_in_reg(REG, reg) \
302 static ssize_t store_in_##reg (struct device *dev, const char *buf, size_t count, int nr) \
303 { \
304         struct i2c_client *client = to_i2c_client(dev); \
305         struct w83781d_data *data = i2c_get_clientdata(client); \
306         u32 val; \
307          \
308         val = simple_strtoul(buf, NULL, 10) / 10; \
309         data->in_##reg[nr] = IN_TO_REG(val); \
310         w83781d_write_value(client, W83781D_REG_IN_##REG(nr), data->in_##reg[nr]); \
311          \
312         return count; \
313 }
314 store_in_reg(MIN, min);
315 store_in_reg(MAX, max);
316
317 #define sysfs_in_offset(offset) \
318 static ssize_t \
319 show_regs_in_##offset (struct device *dev, char *buf) \
320 { \
321         return show_in(dev, buf, 0x##offset); \
322 } \
323 static DEVICE_ATTR(in##offset##_input, S_IRUGO, show_regs_in_##offset, NULL)
324
325 #define sysfs_in_reg_offset(reg, offset) \
326 static ssize_t show_regs_in_##reg##offset (struct device *dev, char *buf) \
327 { \
328         return show_in_##reg (dev, buf, 0x##offset); \
329 } \
330 static ssize_t store_regs_in_##reg##offset (struct device *dev, const char *buf, size_t count) \
331 { \
332         return store_in_##reg (dev, buf, count, 0x##offset); \
333 } \
334 static DEVICE_ATTR(in##offset##_##reg, S_IRUGO| S_IWUSR, show_regs_in_##reg##offset, store_regs_in_##reg##offset)
335
336 #define sysfs_in_offsets(offset) \
337 sysfs_in_offset(offset); \
338 sysfs_in_reg_offset(min, offset); \
339 sysfs_in_reg_offset(max, offset);
340
341 sysfs_in_offsets(0);
342 sysfs_in_offsets(1);
343 sysfs_in_offsets(2);
344 sysfs_in_offsets(3);
345 sysfs_in_offsets(4);
346 sysfs_in_offsets(5);
347 sysfs_in_offsets(6);
348 sysfs_in_offsets(7);
349 sysfs_in_offsets(8);
350
351 #define device_create_file_in(client, offset) \
352 do { \
353 device_create_file(&client->dev, &dev_attr_in##offset##_input); \
354 device_create_file(&client->dev, &dev_attr_in##offset##_min); \
355 device_create_file(&client->dev, &dev_attr_in##offset##_max); \
356 } while (0)
357
358 #define show_fan_reg(reg) \
359 static ssize_t show_##reg (struct device *dev, char *buf, int nr) \
360 { \
361         struct w83781d_data *data = w83781d_update_device(dev); \
362         return sprintf(buf,"%ld\n", \
363                 FAN_FROM_REG(data->reg[nr-1], (long)DIV_FROM_REG(data->fan_div[nr-1]))); \
364 }
365 show_fan_reg(fan);
366 show_fan_reg(fan_min);
367
368 static ssize_t
369 store_fan_min(struct device *dev, const char *buf, size_t count, int nr)
370 {
371         struct i2c_client *client = to_i2c_client(dev);
372         struct w83781d_data *data = i2c_get_clientdata(client);
373         u32 val;
374
375         val = simple_strtoul(buf, NULL, 10);
376         data->fan_min[nr - 1] =
377             FAN_TO_REG(val, DIV_FROM_REG(data->fan_div[nr - 1]));
378         w83781d_write_value(client, W83781D_REG_FAN_MIN(nr),
379                             data->fan_min[nr - 1]);
380
381         return count;
382 }
383
384 #define sysfs_fan_offset(offset) \
385 static ssize_t show_regs_fan_##offset (struct device *dev, char *buf) \
386 { \
387         return show_fan(dev, buf, 0x##offset); \
388 } \
389 static DEVICE_ATTR(fan##offset##_input, S_IRUGO, show_regs_fan_##offset, NULL)
390
391 #define sysfs_fan_min_offset(offset) \
392 static ssize_t show_regs_fan_min##offset (struct device *dev, char *buf) \
393 { \
394         return show_fan_min(dev, buf, 0x##offset); \
395 } \
396 static ssize_t store_regs_fan_min##offset (struct device *dev, const char *buf, size_t count) \
397 { \
398         return store_fan_min(dev, buf, count, 0x##offset); \
399 } \
400 static DEVICE_ATTR(fan##offset##_min, S_IRUGO | S_IWUSR, show_regs_fan_min##offset, store_regs_fan_min##offset)
401
402 sysfs_fan_offset(1);
403 sysfs_fan_min_offset(1);
404 sysfs_fan_offset(2);
405 sysfs_fan_min_offset(2);
406 sysfs_fan_offset(3);
407 sysfs_fan_min_offset(3);
408
409 #define device_create_file_fan(client, offset) \
410 do { \
411 device_create_file(&client->dev, &dev_attr_fan##offset##_input); \
412 device_create_file(&client->dev, &dev_attr_fan##offset##_min); \
413 } while (0)
414
415 #define show_temp_reg(reg) \
416 static ssize_t show_##reg (struct device *dev, char *buf, int nr) \
417 { \
418         struct w83781d_data *data = w83781d_update_device(dev); \
419         if (nr >= 2) {  /* TEMP2 and TEMP3 */ \
420                 if (data->type == as99127f) { \
421                         return sprintf(buf,"%ld\n", \
422                                 (long)AS99127_TEMP_ADD_FROM_REG(data->reg##_add[nr-2])); \
423                 } else { \
424                         return sprintf(buf,"%d\n", \
425                                 LM75_TEMP_FROM_REG(data->reg##_add[nr-2])); \
426                 } \
427         } else {        /* TEMP1 */ \
428                 return sprintf(buf,"%ld\n", (long)TEMP_FROM_REG(data->reg)); \
429         } \
430 }
431 show_temp_reg(temp);
432 show_temp_reg(temp_max);
433 show_temp_reg(temp_max_hyst);
434
435 #define store_temp_reg(REG, reg) \
436 static ssize_t store_temp_##reg (struct device *dev, const char *buf, size_t count, int nr) \
437 { \
438         struct i2c_client *client = to_i2c_client(dev); \
439         struct w83781d_data *data = i2c_get_clientdata(client); \
440         s32 val; \
441          \
442         val = simple_strtol(buf, NULL, 10); \
443          \
444         if (nr >= 2) {  /* TEMP2 and TEMP3 */ \
445                 if (data->type == as99127f) \
446                         data->temp_##reg##_add[nr-2] = AS99127_TEMP_ADD_TO_REG(val); \
447                 else \
448                         data->temp_##reg##_add[nr-2] = LM75_TEMP_TO_REG(val); \
449                  \
450                 w83781d_write_value(client, W83781D_REG_TEMP_##REG(nr), \
451                                 data->temp_##reg##_add[nr-2]); \
452         } else {        /* TEMP1 */ \
453                 data->temp_##reg = TEMP_TO_REG(val); \
454                 w83781d_write_value(client, W83781D_REG_TEMP_##REG(nr), \
455                         data->temp_##reg); \
456         } \
457          \
458         return count; \
459 }
460 store_temp_reg(OVER, max);
461 store_temp_reg(HYST, max_hyst);
462
463 #define sysfs_temp_offset(offset) \
464 static ssize_t \
465 show_regs_temp_##offset (struct device *dev, char *buf) \
466 { \
467         return show_temp(dev, buf, 0x##offset); \
468 } \
469 static DEVICE_ATTR(temp##offset##_input, S_IRUGO, show_regs_temp_##offset, NULL)
470
471 #define sysfs_temp_reg_offset(reg, offset) \
472 static ssize_t show_regs_temp_##reg##offset (struct device *dev, char *buf) \
473 { \
474         return show_temp_##reg (dev, buf, 0x##offset); \
475 } \
476 static ssize_t store_regs_temp_##reg##offset (struct device *dev, const char *buf, size_t count) \
477 { \
478         return store_temp_##reg (dev, buf, count, 0x##offset); \
479 } \
480 static DEVICE_ATTR(temp##offset##_##reg, S_IRUGO| S_IWUSR, show_regs_temp_##reg##offset, store_regs_temp_##reg##offset)
481
482 #define sysfs_temp_offsets(offset) \
483 sysfs_temp_offset(offset); \
484 sysfs_temp_reg_offset(max, offset); \
485 sysfs_temp_reg_offset(max_hyst, offset);
486
487 sysfs_temp_offsets(1);
488 sysfs_temp_offsets(2);
489 sysfs_temp_offsets(3);
490
491 #define device_create_file_temp(client, offset) \
492 do { \
493 device_create_file(&client->dev, &dev_attr_temp##offset##_input); \
494 device_create_file(&client->dev, &dev_attr_temp##offset##_max); \
495 device_create_file(&client->dev, &dev_attr_temp##offset##_max_hyst); \
496 } while (0)
497
498 static ssize_t
499 show_vid_reg(struct device *dev, char *buf)
500 {
501         struct w83781d_data *data = w83781d_update_device(dev);
502         return sprintf(buf, "%ld\n", (long) vid_from_reg(data->vid, data->vrm));
503 }
504
505 static
506 DEVICE_ATTR(in0_ref, S_IRUGO, show_vid_reg, NULL)
507 #define device_create_file_vid(client) \
508 device_create_file(&client->dev, &dev_attr_in0_ref);
509 static ssize_t
510 show_vrm_reg(struct device *dev, char *buf)
511 {
512         struct w83781d_data *data = w83781d_update_device(dev);
513         return sprintf(buf, "%ld\n", (long) data->vrm);
514 }
515
516 static ssize_t
517 store_vrm_reg(struct device *dev, const char *buf, size_t count)
518 {
519         struct i2c_client *client = to_i2c_client(dev);
520         struct w83781d_data *data = i2c_get_clientdata(client);
521         u32 val;
522
523         val = simple_strtoul(buf, NULL, 10);
524         data->vrm = val;
525
526         return count;
527 }
528
529 static
530 DEVICE_ATTR(vrm, S_IRUGO | S_IWUSR, show_vrm_reg, store_vrm_reg)
531 #define device_create_file_vrm(client) \
532 device_create_file(&client->dev, &dev_attr_vrm);
533 static ssize_t
534 show_alarms_reg(struct device *dev, char *buf)
535 {
536         struct w83781d_data *data = w83781d_update_device(dev);
537         return sprintf(buf, "%ld\n", (long) ALARMS_FROM_REG(data->alarms));
538 }
539
540 static
541 DEVICE_ATTR(alarms, S_IRUGO, show_alarms_reg, NULL)
542 #define device_create_file_alarms(client) \
543 device_create_file(&client->dev, &dev_attr_alarms);
544 static ssize_t show_beep_mask (struct device *dev, char *buf)
545 {
546         struct w83781d_data *data = w83781d_update_device(dev);
547         return sprintf(buf, "%ld\n",
548                        (long)BEEP_MASK_FROM_REG(data->beep_mask, data->type));
549 }
550 static ssize_t show_beep_enable (struct device *dev, char *buf)
551 {
552         struct w83781d_data *data = w83781d_update_device(dev);
553         return sprintf(buf, "%ld\n",
554                        (long)BEEP_ENABLE_FROM_REG(data->beep_enable));
555 }
556
557 #define BEEP_ENABLE                     0       /* Store beep_enable */
558 #define BEEP_MASK                       1       /* Store beep_mask */
559
560 static ssize_t
561 store_beep_reg(struct device *dev, const char *buf, size_t count,
562                int update_mask)
563 {
564         struct i2c_client *client = to_i2c_client(dev);
565         struct w83781d_data *data = i2c_get_clientdata(client);
566         u32 val, val2;
567
568         val = simple_strtoul(buf, NULL, 10);
569
570         if (update_mask == BEEP_MASK) { /* We are storing beep_mask */
571                 data->beep_mask = BEEP_MASK_TO_REG(val, data->type);
572                 w83781d_write_value(client, W83781D_REG_BEEP_INTS1,
573                                     data->beep_mask & 0xff);
574
575                 if ((data->type != w83781d) && (data->type != as99127f)) {
576                         w83781d_write_value(client, W83781D_REG_BEEP_INTS3,
577                                             ((data->beep_mask) >> 16) & 0xff);
578                 }
579
580                 val2 = (data->beep_mask >> 8) & 0x7f;
581         } else {                /* We are storing beep_enable */
582                 val2 = w83781d_read_value(client, W83781D_REG_BEEP_INTS2) & 0x7f;
583                 data->beep_enable = BEEP_ENABLE_TO_REG(val);
584         }
585
586         w83781d_write_value(client, W83781D_REG_BEEP_INTS2,
587                             val2 | data->beep_enable << 7);
588
589         return count;
590 }
591
592 #define sysfs_beep(REG, reg) \
593 static ssize_t show_regs_beep_##reg (struct device *dev, char *buf) \
594 { \
595         return show_beep_##reg(dev, buf); \
596 } \
597 static ssize_t store_regs_beep_##reg (struct device *dev, const char *buf, size_t count) \
598 { \
599         return store_beep_reg(dev, buf, count, BEEP_##REG); \
600 } \
601 static DEVICE_ATTR(beep_##reg, S_IRUGO | S_IWUSR, show_regs_beep_##reg, store_regs_beep_##reg)
602
603 sysfs_beep(ENABLE, enable);
604 sysfs_beep(MASK, mask);
605
606 #define device_create_file_beep(client) \
607 do { \
608 device_create_file(&client->dev, &dev_attr_beep_enable); \
609 device_create_file(&client->dev, &dev_attr_beep_mask); \
610 } while (0)
611
612 static ssize_t
613 show_fan_div_reg(struct device *dev, char *buf, int nr)
614 {
615         struct w83781d_data *data = w83781d_update_device(dev);
616         return sprintf(buf, "%ld\n",
617                        (long) DIV_FROM_REG(data->fan_div[nr - 1]));
618 }
619
620 /* Note: we save and restore the fan minimum here, because its value is
621    determined in part by the fan divisor.  This follows the principle of
622    least suprise; the user doesn't expect the fan minimum to change just
623    because the divisor changed. */
624 static ssize_t
625 store_fan_div_reg(struct device *dev, const char *buf, size_t count, int nr)
626 {
627         struct i2c_client *client = to_i2c_client(dev);
628         struct w83781d_data *data = i2c_get_clientdata(client);
629         unsigned long min;
630         u8 reg;
631
632         /* Save fan_min */
633         min = FAN_FROM_REG(data->fan_min[nr],
634                            DIV_FROM_REG(data->fan_div[nr]));
635
636         data->fan_div[nr] = DIV_TO_REG(simple_strtoul(buf, NULL, 10),
637                                       data->type);
638
639         reg = (w83781d_read_value(client, nr==2 ? W83781D_REG_PIN : W83781D_REG_VID_FANDIV)
640                & (nr==0 ? 0xcf : 0x3f))
641             | ((data->fan_div[nr] & 0x03) << (nr==0 ? 4 : 6));
642         w83781d_write_value(client, nr==2 ? W83781D_REG_PIN : W83781D_REG_VID_FANDIV, reg);
643
644         /* w83781d and as99127f don't have extended divisor bits */
645         if (data->type != w83781d && data->type != as99127f) {
646                 reg = (w83781d_read_value(client, W83781D_REG_VBAT)
647                        & ~(1 << (5 + nr)))
648                     | ((data->fan_div[nr] & 0x04) << (3 + nr));
649                 w83781d_write_value(client, W83781D_REG_VBAT, reg);
650         }
651
652         /* Restore fan_min */
653         data->fan_min[nr] = FAN_TO_REG(min, DIV_FROM_REG(data->fan_div[nr]));
654         w83781d_write_value(client, W83781D_REG_FAN_MIN(nr+1), data->fan_min[nr]);
655
656         return count;
657 }
658
659 #define sysfs_fan_div(offset) \
660 static ssize_t show_regs_fan_div_##offset (struct device *dev, char *buf) \
661 { \
662         return show_fan_div_reg(dev, buf, offset); \
663 } \
664 static ssize_t store_regs_fan_div_##offset (struct device *dev, const char *buf, size_t count) \
665 { \
666         return store_fan_div_reg(dev, buf, count, offset - 1); \
667 } \
668 static DEVICE_ATTR(fan##offset##_div, S_IRUGO | S_IWUSR, show_regs_fan_div_##offset, store_regs_fan_div_##offset)
669
670 sysfs_fan_div(1);
671 sysfs_fan_div(2);
672 sysfs_fan_div(3);
673
674 #define device_create_file_fan_div(client, offset) \
675 do { \
676 device_create_file(&client->dev, &dev_attr_fan##offset##_div); \
677 } while (0)
678
679 static ssize_t
680 show_pwm_reg(struct device *dev, char *buf, int nr)
681 {
682         struct w83781d_data *data = w83781d_update_device(dev);
683         return sprintf(buf, "%ld\n", (long) PWM_FROM_REG(data->pwm[nr - 1]));
684 }
685
686 static ssize_t
687 show_pwmenable_reg(struct device *dev, char *buf, int nr)
688 {
689         struct w83781d_data *data = w83781d_update_device(dev);
690         return sprintf(buf, "%ld\n", (long) data->pwmenable[nr - 1]);
691 }
692
693 static ssize_t
694 store_pwm_reg(struct device *dev, const char *buf, size_t count, int nr)
695 {
696         struct i2c_client *client = to_i2c_client(dev);
697         struct w83781d_data *data = i2c_get_clientdata(client);
698         u32 val;
699
700         val = simple_strtoul(buf, NULL, 10);
701
702         data->pwm[nr - 1] = PWM_TO_REG(val);
703         w83781d_write_value(client, W83781D_REG_PWM(nr), data->pwm[nr - 1]);
704
705         return count;
706 }
707
708 static ssize_t
709 store_pwmenable_reg(struct device *dev, const char *buf, size_t count, int nr)
710 {
711         struct i2c_client *client = to_i2c_client(dev);
712         struct w83781d_data *data = i2c_get_clientdata(client);
713         u32 val, reg;
714
715         val = simple_strtoul(buf, NULL, 10);
716
717         switch (val) {
718         case 0:
719         case 1:
720                 reg = w83781d_read_value(client, W83781D_REG_PWMCLK12);
721                 w83781d_write_value(client, W83781D_REG_PWMCLK12,
722                                     (reg & 0xf7) | (val << 3));
723
724                 reg = w83781d_read_value(client, W83781D_REG_BEEP_CONFIG);
725                 w83781d_write_value(client, W83781D_REG_BEEP_CONFIG,
726                                     (reg & 0xef) | (!val << 4));
727
728                 data->pwmenable[nr - 1] = val;
729                 break;
730
731         default:
732                 return -EINVAL;
733         }
734
735         return count;
736 }
737
738 #define sysfs_pwm(offset) \
739 static ssize_t show_regs_pwm_##offset (struct device *dev, char *buf) \
740 { \
741         return show_pwm_reg(dev, buf, offset); \
742 } \
743 static ssize_t store_regs_pwm_##offset (struct device *dev, const char *buf, size_t count) \
744 { \
745         return store_pwm_reg(dev, buf, count, offset); \
746 } \
747 static DEVICE_ATTR(fan##offset##_pwm, S_IRUGO | S_IWUSR, show_regs_pwm_##offset, store_regs_pwm_##offset)
748
749 #define sysfs_pwmenable(offset) \
750 static ssize_t show_regs_pwmenable_##offset (struct device *dev, char *buf) \
751 { \
752         return show_pwmenable_reg(dev, buf, offset); \
753 } \
754 static ssize_t store_regs_pwmenable_##offset (struct device *dev, const char *buf, size_t count) \
755 { \
756         return store_pwmenable_reg(dev, buf, count, offset); \
757 } \
758 static DEVICE_ATTR(fan##offset##_pwm_enable, S_IRUGO | S_IWUSR, show_regs_pwmenable_##offset, store_regs_pwmenable_##offset)
759
760 sysfs_pwm(1);
761 sysfs_pwm(2);
762 sysfs_pwmenable(2);             /* only PWM2 can be enabled/disabled */
763 sysfs_pwm(3);
764 sysfs_pwm(4);
765
766 #define device_create_file_pwm(client, offset) \
767 do { \
768 device_create_file(&client->dev, &dev_attr_fan##offset##_pwm); \
769 } while (0)
770
771 #define device_create_file_pwmenable(client, offset) \
772 do { \
773 device_create_file(&client->dev, &dev_attr_fan##offset##_pwm_enable); \
774 } while (0)
775
776 static ssize_t
777 show_sensor_reg(struct device *dev, char *buf, int nr)
778 {
779         struct w83781d_data *data = w83781d_update_device(dev);
780         return sprintf(buf, "%ld\n", (long) data->sens[nr - 1]);
781 }
782
783 static ssize_t
784 store_sensor_reg(struct device *dev, const char *buf, size_t count, int nr)
785 {
786         struct i2c_client *client = to_i2c_client(dev);
787         struct w83781d_data *data = i2c_get_clientdata(client);
788         u32 val, tmp;
789
790         val = simple_strtoul(buf, NULL, 10);
791
792         switch (val) {
793         case 1:         /* PII/Celeron diode */
794                 tmp = w83781d_read_value(client, W83781D_REG_SCFG1);
795                 w83781d_write_value(client, W83781D_REG_SCFG1,
796                                     tmp | BIT_SCFG1[nr - 1]);
797                 tmp = w83781d_read_value(client, W83781D_REG_SCFG2);
798                 w83781d_write_value(client, W83781D_REG_SCFG2,
799                                     tmp | BIT_SCFG2[nr - 1]);
800                 data->sens[nr - 1] = val;
801                 break;
802         case 2:         /* 3904 */
803                 tmp = w83781d_read_value(client, W83781D_REG_SCFG1);
804                 w83781d_write_value(client, W83781D_REG_SCFG1,
805                                     tmp | BIT_SCFG1[nr - 1]);
806                 tmp = w83781d_read_value(client, W83781D_REG_SCFG2);
807                 w83781d_write_value(client, W83781D_REG_SCFG2,
808                                     tmp & ~BIT_SCFG2[nr - 1]);
809                 data->sens[nr - 1] = val;
810                 break;
811         case W83781D_DEFAULT_BETA:      /* thermistor */
812                 tmp = w83781d_read_value(client, W83781D_REG_SCFG1);
813                 w83781d_write_value(client, W83781D_REG_SCFG1,
814                                     tmp & ~BIT_SCFG1[nr - 1]);
815                 data->sens[nr - 1] = val;
816                 break;
817         default:
818                 dev_err(&client->dev,
819                        "Invalid sensor type %ld; must be 1, 2, or %d\n",
820                        (long) val, W83781D_DEFAULT_BETA);
821                 break;
822         }
823
824         return count;
825 }
826
827 #define sysfs_sensor(offset) \
828 static ssize_t show_regs_sensor_##offset (struct device *dev, char *buf) \
829 { \
830     return show_sensor_reg(dev, buf, offset); \
831 } \
832 static ssize_t store_regs_sensor_##offset (struct device *dev, const char *buf, size_t count) \
833 { \
834     return store_sensor_reg(dev, buf, count, offset); \
835 } \
836 static DEVICE_ATTR(temp##offset##_type, S_IRUGO | S_IWUSR, show_regs_sensor_##offset, store_regs_sensor_##offset)
837
838 sysfs_sensor(1);
839 sysfs_sensor(2);
840 sysfs_sensor(3);
841
842 #define device_create_file_sensor(client, offset) \
843 do { \
844 device_create_file(&client->dev, &dev_attr_temp##offset##_type); \
845 } while (0)
846
847 #ifdef W83781D_RT
848 static ssize_t
849 show_rt_reg(struct device *dev, char *buf, int nr)
850 {
851         struct w83781d_data *data = w83781d_update_device(dev);
852         int i, j = 0;
853
854         for (i = 0; i < 32; i++) {
855                 if (i > 0)
856                         j += sprintf(buf, " %ld", (long) data->rt[nr - 1][i]);
857                 else
858                         j += sprintf(buf, "%ld", (long) data->rt[nr - 1][i]);
859         }
860         j += sprintf(buf, "\n");
861
862         return j;
863 }
864
865 static ssize_t
866 store_rt_reg(struct device *dev, const char *buf, size_t count, int nr)
867 {
868         struct i2c_client *client = to_i2c_client(dev);
869         struct w83781d_data *data = i2c_get_clientdata(client);
870         u32 val, i;
871
872         for (i = 0; i < count; i++) {
873                 val = simple_strtoul(buf + count, NULL, 10);
874
875                 /* fixme: no bounds checking 0-255 */
876                 data->rt[nr - 1][i] = val & 0xff;
877                 w83781d_write_value(client, W83781D_REG_RT_IDX, i);
878                 w83781d_write_value(client, W83781D_REG_RT_VAL,
879                                     data->rt[nr - 1][i]);
880         }
881
882         return count;
883 }
884
885 #define sysfs_rt(offset) \
886 static ssize_t show_regs_rt_##offset (struct device *dev, char *buf) \
887 { \
888         return show_rt_reg(dev, buf, offset); \
889 } \
890 static ssize_t store_regs_rt_##offset (struct device *dev, const char *buf, size_t count) \
891 { \
892     return store_rt_reg(dev, buf, count, offset); \
893 } \
894 static DEVICE_ATTR(rt##offset, S_IRUGO | S_IWUSR, show_regs_rt_##offset, store_regs_rt_##offset)
895
896 sysfs_rt(1);
897 sysfs_rt(2);
898 sysfs_rt(3);
899
900 #define device_create_file_rt(client, offset) \
901 do { \
902 device_create_file(&client->dev, &dev_attr_rt##offset); \
903 } while (0)
904
905 #endif                          /* ifdef W83781D_RT */
906
907 /* This function is called when:
908      * w83781d_driver is inserted (when this module is loaded), for each
909        available adapter
910      * when a new adapter is inserted (and w83781d_driver is still present) */
911 static int
912 w83781d_attach_adapter(struct i2c_adapter *adapter)
913 {
914         if (!(adapter->class & I2C_CLASS_HWMON))
915                 return 0;
916         return i2c_detect(adapter, &addr_data, w83781d_detect);
917 }
918
919 /* Assumes that adapter is of I2C, not ISA variety.
920  * OTHERWISE DON'T CALL THIS
921  */
922 static int
923 w83781d_detect_subclients(struct i2c_adapter *adapter, int address, int kind,
924                 struct i2c_client *new_client)
925 {
926         int i, val1 = 0, id;
927         int err;
928         const char *client_name = "";
929         struct w83781d_data *data = i2c_get_clientdata(new_client);
930
931         data->lm75[0] = kmalloc(sizeof(struct i2c_client), GFP_KERNEL);
932         if (!(data->lm75[0])) {
933                 err = -ENOMEM;
934                 goto ERROR_SC_0;
935         }
936         memset(data->lm75[0], 0x00, sizeof (struct i2c_client));
937
938         id = i2c_adapter_id(adapter);
939
940         if (force_subclients[0] == id && force_subclients[1] == address) {
941                 for (i = 2; i <= 3; i++) {
942                         if (force_subclients[i] < 0x48 ||
943                             force_subclients[i] > 0x4f) {
944                                 dev_err(&new_client->dev, "Invalid subclient "
945                                         "address %d; must be 0x48-0x4f\n",
946                                         force_subclients[i]);
947                                 err = -EINVAL;
948                                 goto ERROR_SC_1;
949                         }
950                 }
951                 w83781d_write_value(new_client, W83781D_REG_I2C_SUBADDR,
952                                 (force_subclients[2] & 0x07) |
953                                 ((force_subclients[3] & 0x07) << 4));
954                 data->lm75[0]->addr = force_subclients[2];
955         } else {
956                 val1 = w83781d_read_value(new_client, W83781D_REG_I2C_SUBADDR);
957                 data->lm75[0]->addr = 0x48 + (val1 & 0x07);
958         }
959
960         if (kind != w83783s) {
961
962                 data->lm75[1] = kmalloc(sizeof(struct i2c_client), GFP_KERNEL);
963                 if (!(data->lm75[1])) {
964                         err = -ENOMEM;
965                         goto ERROR_SC_1;
966                 }
967                 memset(data->lm75[1], 0x0, sizeof(struct i2c_client));
968
969                 if (force_subclients[0] == id &&
970                     force_subclients[1] == address) {
971                         data->lm75[1]->addr = force_subclients[3];
972                 } else {
973                         data->lm75[1]->addr = 0x48 + ((val1 >> 4) & 0x07);
974                 }
975                 if (data->lm75[0]->addr == data->lm75[1]->addr) {
976                         dev_err(&new_client->dev,
977                                "Duplicate addresses 0x%x for subclients.\n",
978                                data->lm75[0]->addr);
979                         err = -EBUSY;
980                         goto ERROR_SC_2;
981                 }
982         }
983
984         if (kind == w83781d)
985                 client_name = "w83781d subclient";
986         else if (kind == w83782d)
987                 client_name = "w83782d subclient";
988         else if (kind == w83783s)
989                 client_name = "w83783s subclient";
990         else if (kind == w83627hf)
991                 client_name = "w83627hf subclient";
992         else if (kind == as99127f)
993                 client_name = "as99127f subclient";
994
995         for (i = 0; i <= 1; i++) {
996                 /* store all data in w83781d */
997                 i2c_set_clientdata(data->lm75[i], NULL);
998                 data->lm75[i]->adapter = adapter;
999                 data->lm75[i]->driver = &w83781d_driver;
1000                 data->lm75[i]->flags = 0;
1001                 strlcpy(data->lm75[i]->name, client_name,
1002                         I2C_NAME_SIZE);
1003                 if ((err = i2c_attach_client(data->lm75[i]))) {
1004                         dev_err(&new_client->dev, "Subclient %d "
1005                                 "registration at address 0x%x "
1006                                 "failed.\n", i, data->lm75[i]->addr);
1007                         if (i == 1)
1008                                 goto ERROR_SC_3;
1009                         goto ERROR_SC_2;
1010                 }
1011                 if (kind == w83783s)
1012                         break;
1013         }
1014
1015         return 0;
1016
1017 /* Undo inits in case of errors */
1018 ERROR_SC_3:
1019         i2c_detach_client(data->lm75[0]);
1020 ERROR_SC_2:
1021         if (NULL != data->lm75[1])
1022                 kfree(data->lm75[1]);
1023 ERROR_SC_1:
1024         if (NULL != data->lm75[0])
1025                 kfree(data->lm75[0]);
1026 ERROR_SC_0:
1027         return err;
1028 }
1029
1030 static int
1031 w83781d_detect(struct i2c_adapter *adapter, int address, int kind)
1032 {
1033         int i = 0, val1 = 0, val2;
1034         struct i2c_client *new_client;
1035         struct w83781d_data *data;
1036         int err;
1037         const char *client_name = "";
1038         int is_isa = i2c_is_isa_adapter(adapter);
1039         enum vendor { winbond, asus } vendid;
1040
1041         if (!is_isa
1042             && !i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA)) {
1043                 err = -EINVAL;
1044                 goto ERROR0;
1045         }
1046
1047         /* Prevent users from forcing a kind for a bus it isn't supposed
1048            to possibly be on */
1049         if (is_isa && (kind == as99127f || kind == w83783s)) {
1050                 dev_err(&adapter->dev,
1051                         "Cannot force I2C-only chip for ISA address 0x%02x.\n",
1052                         address);
1053                 err = -EINVAL;
1054                 goto ERROR0;
1055         }
1056         if (!is_isa && kind == w83697hf) {
1057                 dev_err(&adapter->dev,
1058                         "Cannot force ISA-only chip for I2C address 0x%02x.\n",
1059                         address);
1060                 err = -EINVAL;
1061                 goto ERROR0;
1062         }
1063         
1064         if (is_isa)
1065                 if (!request_region(address, W83781D_EXTENT, "w83781d")) {
1066                         err = -EBUSY;
1067                         goto ERROR0;
1068                 }
1069
1070         /* Probe whether there is anything available on this address. Already
1071            done for SMBus clients */
1072         if (kind < 0) {
1073                 if (is_isa) {
1074
1075 #define REALLY_SLOW_IO
1076                         /* We need the timeouts for at least some LM78-like
1077                            chips. But only if we read 'undefined' registers. */
1078                         i = inb_p(address + 1);
1079                         if (inb_p(address + 2) != i) {
1080                                 err = -ENODEV;
1081                                 goto ERROR1;
1082                         }
1083                         if (inb_p(address + 3) != i) {
1084                                 err = -ENODEV;
1085                                 goto ERROR1;
1086                         }
1087                         if (inb_p(address + 7) != i) {
1088                                 err = -ENODEV;
1089                                 goto ERROR1;
1090                         }
1091 #undef REALLY_SLOW_IO
1092
1093                         /* Let's just hope nothing breaks here */
1094                         i = inb_p(address + 5) & 0x7f;
1095                         outb_p(~i & 0x7f, address + 5);
1096                         if ((inb_p(address + 5) & 0x7f) != (~i & 0x7f)) {
1097                                 outb_p(i, address + 5);
1098                                 err = -ENODEV;
1099                                 goto ERROR1;
1100                         }
1101                 }
1102         }
1103
1104         /* OK. For now, we presume we have a valid client. We now create the
1105            client structure, even though we cannot fill it completely yet.
1106            But it allows us to access w83781d_{read,write}_value. */
1107
1108         if (!(data = kmalloc(sizeof(struct w83781d_data), GFP_KERNEL))) {
1109                 err = -ENOMEM;
1110                 goto ERROR1;
1111         }
1112         memset(data, 0, sizeof(struct w83781d_data));
1113
1114         new_client = &data->client;
1115         i2c_set_clientdata(new_client, data);
1116         new_client->addr = address;
1117         init_MUTEX(&data->lock);
1118         new_client->adapter = adapter;
1119         new_client->driver = &w83781d_driver;
1120         new_client->flags = 0;
1121
1122         /* Now, we do the remaining detection. */
1123
1124         /* The w8378?d may be stuck in some other bank than bank 0. This may
1125            make reading other information impossible. Specify a force=... or
1126            force_*=... parameter, and the Winbond will be reset to the right
1127            bank. */
1128         if (kind < 0) {
1129                 if (w83781d_read_value(new_client, W83781D_REG_CONFIG) & 0x80){
1130                         err = -ENODEV;
1131                         goto ERROR2;
1132                 }
1133                 val1 = w83781d_read_value(new_client, W83781D_REG_BANK);
1134                 val2 = w83781d_read_value(new_client, W83781D_REG_CHIPMAN);
1135                 /* Check for Winbond or Asus ID if in bank 0 */
1136                 if ((!(val1 & 0x07)) &&
1137                     (((!(val1 & 0x80)) && (val2 != 0xa3) && (val2 != 0xc3))
1138                      || ((val1 & 0x80) && (val2 != 0x5c) && (val2 != 0x12)))) {
1139                         err = -ENODEV;
1140                         goto ERROR2;
1141                 }
1142                 /* If Winbond SMBus, check address at 0x48.
1143                    Asus doesn't support, except for as99127f rev.2 */
1144                 if ((!is_isa) && (((!(val1 & 0x80)) && (val2 == 0xa3)) ||
1145                                   ((val1 & 0x80) && (val2 == 0x5c)))) {
1146                         if (w83781d_read_value
1147                             (new_client, W83781D_REG_I2C_ADDR) != address) {
1148                                 err = -ENODEV;
1149                                 goto ERROR2;
1150                         }
1151                 }
1152         }
1153
1154         /* We have either had a force parameter, or we have already detected the
1155            Winbond. Put it now into bank 0 and Vendor ID High Byte */
1156         w83781d_write_value(new_client, W83781D_REG_BANK,
1157                             (w83781d_read_value(new_client,
1158                                                 W83781D_REG_BANK) & 0x78) |
1159                             0x80);
1160
1161         /* Determine the chip type. */
1162         if (kind <= 0) {
1163                 /* get vendor ID */
1164                 val2 = w83781d_read_value(new_client, W83781D_REG_CHIPMAN);
1165                 if (val2 == 0x5c)
1166                         vendid = winbond;
1167                 else if (val2 == 0x12)
1168                         vendid = asus;
1169                 else {
1170                         err = -ENODEV;
1171                         goto ERROR2;
1172                 }
1173
1174                 val1 = w83781d_read_value(new_client, W83781D_REG_WCHIPID);
1175                 if ((val1 == 0x10 || val1 == 0x11) && vendid == winbond)
1176                         kind = w83781d;
1177                 else if (val1 == 0x30 && vendid == winbond)
1178                         kind = w83782d;
1179                 else if (val1 == 0x40 && vendid == winbond && !is_isa
1180                                 && address == 0x2d)
1181                         kind = w83783s;
1182                 else if ((val1 == 0x21 || val1 == 0x90) && vendid == winbond)
1183                         kind = w83627hf;
1184                 else if (val1 == 0x31 && !is_isa && address >= 0x28)
1185                         kind = as99127f;
1186                 else if (val1 == 0x60 && vendid == winbond && is_isa)
1187                         kind = w83697hf;
1188                 else {
1189                         if (kind == 0)
1190                                 dev_warn(&new_client->dev,
1191                                        "Ignoring 'force' parameter for unknown chip at"
1192                                        "adapter %d, address 0x%02x\n",
1193                                        i2c_adapter_id(adapter), address);
1194                         err = -EINVAL;
1195                         goto ERROR2;
1196                 }
1197         }
1198
1199         if (kind == w83781d) {
1200                 client_name = "w83781d";
1201         } else if (kind == w83782d) {
1202                 client_name = "w83782d";
1203         } else if (kind == w83783s) {
1204                 client_name = "w83783s";
1205         } else if (kind == w83627hf) {
1206                 if (val1 == 0x90)
1207                         client_name = "w83627thf";
1208                 else
1209                         client_name = "w83627hf";
1210         } else if (kind == as99127f) {
1211                 client_name = "as99127f";
1212         } else if (kind == w83697hf) {
1213                 client_name = "w83697hf";
1214         }
1215
1216         /* Fill in the remaining client fields and put into the global list */
1217         strlcpy(new_client->name, client_name, I2C_NAME_SIZE);
1218         data->type = kind;
1219
1220         data->valid = 0;
1221         init_MUTEX(&data->update_lock);
1222
1223         /* Tell the I2C layer a new client has arrived */
1224         if ((err = i2c_attach_client(new_client)))
1225                 goto ERROR2;
1226
1227         /* attach secondary i2c lm75-like clients */
1228         if (!is_isa) {
1229                 if ((err = w83781d_detect_subclients(adapter, address,
1230                                 kind, new_client)))
1231                         goto ERROR3;
1232         } else {
1233                 data->lm75[0] = NULL;
1234                 data->lm75[1] = NULL;
1235         }
1236
1237         /* Initialize the chip */
1238         w83781d_init_client(new_client);
1239
1240         /* A few vars need to be filled upon startup */
1241         for (i = 1; i <= 3; i++) {
1242                 data->fan_min[i - 1] = w83781d_read_value(new_client,
1243                                         W83781D_REG_FAN_MIN(i));
1244         }
1245         if (kind != w83781d && kind != as99127f)
1246                 for (i = 0; i < 4; i++)
1247                         data->pwmenable[i] = 1;
1248
1249         /* Register sysfs hooks */
1250         device_create_file_in(new_client, 0);
1251         if (kind != w83783s && kind != w83697hf)
1252                 device_create_file_in(new_client, 1);
1253         device_create_file_in(new_client, 2);
1254         device_create_file_in(new_client, 3);
1255         device_create_file_in(new_client, 4);
1256         device_create_file_in(new_client, 5);
1257         device_create_file_in(new_client, 6);
1258         if (kind != as99127f && kind != w83781d && kind != w83783s) {
1259                 device_create_file_in(new_client, 7);
1260                 device_create_file_in(new_client, 8);
1261         }
1262
1263         device_create_file_fan(new_client, 1);
1264         device_create_file_fan(new_client, 2);
1265         if (kind != w83697hf)
1266                 device_create_file_fan(new_client, 3);
1267
1268         device_create_file_temp(new_client, 1);
1269         device_create_file_temp(new_client, 2);
1270         if (kind != w83783s && kind != w83697hf)
1271                 device_create_file_temp(new_client, 3);
1272
1273         if (kind != w83697hf)
1274                 device_create_file_vid(new_client);
1275
1276         if (kind != w83697hf)
1277                 device_create_file_vrm(new_client);
1278
1279         device_create_file_fan_div(new_client, 1);
1280         device_create_file_fan_div(new_client, 2);
1281         if (kind != w83697hf)
1282                 device_create_file_fan_div(new_client, 3);
1283
1284         device_create_file_alarms(new_client);
1285
1286         device_create_file_beep(new_client);
1287
1288         if (kind != w83781d && kind != as99127f) {
1289                 device_create_file_pwm(new_client, 1);
1290                 device_create_file_pwm(new_client, 2);
1291                 device_create_file_pwmenable(new_client, 2);
1292         }
1293         if (kind == w83782d && !is_isa) {
1294                 device_create_file_pwm(new_client, 3);
1295                 device_create_file_pwm(new_client, 4);
1296         }
1297
1298         if (kind != as99127f && kind != w83781d) {
1299                 device_create_file_sensor(new_client, 1);
1300                 device_create_file_sensor(new_client, 2);
1301                 if (kind != w83783s && kind != w83697hf)
1302                         device_create_file_sensor(new_client, 3);
1303         }
1304 #ifdef W83781D_RT
1305         if (kind == w83781d) {
1306                 device_create_file_rt(new_client, 1);
1307                 device_create_file_rt(new_client, 2);
1308                 device_create_file_rt(new_client, 3);
1309         }
1310 #endif
1311
1312         return 0;
1313
1314 ERROR3:
1315         i2c_detach_client(new_client);
1316 ERROR2:
1317         kfree(data);
1318 ERROR1:
1319         if (is_isa)
1320                 release_region(address, W83781D_EXTENT);
1321 ERROR0:
1322         return err;
1323 }
1324
1325 static int
1326 w83781d_detach_client(struct i2c_client *client)
1327 {
1328         int err;
1329
1330         if (i2c_is_isa_client(client))
1331                 release_region(client->addr, W83781D_EXTENT);
1332
1333         if ((err = i2c_detach_client(client))) {
1334                 dev_err(&client->dev,
1335                        "Client deregistration failed, client not detached.\n");
1336                 return err;
1337         }
1338
1339         if (i2c_get_clientdata(client)==NULL) {
1340                 /* subclients */
1341                 kfree(client);
1342         } else {
1343                 /* main client */
1344                 kfree(i2c_get_clientdata(client));
1345         }
1346
1347         return 0;
1348 }
1349
1350 /* The SMBus locks itself, usually, but nothing may access the Winbond between
1351    bank switches. ISA access must always be locked explicitly! 
1352    We ignore the W83781D BUSY flag at this moment - it could lead to deadlocks,
1353    would slow down the W83781D access and should not be necessary. 
1354    There are some ugly typecasts here, but the good news is - they should
1355    nowhere else be necessary! */
1356 static int
1357 w83781d_read_value(struct i2c_client *client, u16 reg)
1358 {
1359         struct w83781d_data *data = i2c_get_clientdata(client);
1360         int res, word_sized, bank;
1361         struct i2c_client *cl;
1362
1363         down(&data->lock);
1364         if (i2c_is_isa_client(client)) {
1365                 word_sized = (((reg & 0xff00) == 0x100)
1366                               || ((reg & 0xff00) == 0x200))
1367                     && (((reg & 0x00ff) == 0x50)
1368                         || ((reg & 0x00ff) == 0x53)
1369                         || ((reg & 0x00ff) == 0x55));
1370                 if (reg & 0xff00) {
1371                         outb_p(W83781D_REG_BANK,
1372                                client->addr + W83781D_ADDR_REG_OFFSET);
1373                         outb_p(reg >> 8,
1374                                client->addr + W83781D_DATA_REG_OFFSET);
1375                 }
1376                 outb_p(reg & 0xff, client->addr + W83781D_ADDR_REG_OFFSET);
1377                 res = inb_p(client->addr + W83781D_DATA_REG_OFFSET);
1378                 if (word_sized) {
1379                         outb_p((reg & 0xff) + 1,
1380                                client->addr + W83781D_ADDR_REG_OFFSET);
1381                         res =
1382                             (res << 8) + inb_p(client->addr +
1383                                                W83781D_DATA_REG_OFFSET);
1384                 }
1385                 if (reg & 0xff00) {
1386                         outb_p(W83781D_REG_BANK,
1387                                client->addr + W83781D_ADDR_REG_OFFSET);
1388                         outb_p(0, client->addr + W83781D_DATA_REG_OFFSET);
1389                 }
1390         } else {
1391                 bank = (reg >> 8) & 0x0f;
1392                 if (bank > 2)
1393                         /* switch banks */
1394                         i2c_smbus_write_byte_data(client, W83781D_REG_BANK,
1395                                                   bank);
1396                 if (bank == 0 || bank > 2) {
1397                         res = i2c_smbus_read_byte_data(client, reg & 0xff);
1398                 } else {
1399                         /* switch to subclient */
1400                         cl = data->lm75[bank - 1];
1401                         /* convert from ISA to LM75 I2C addresses */
1402                         switch (reg & 0xff) {
1403                         case 0x50:      /* TEMP */
1404                                 res = swab16(i2c_smbus_read_word_data(cl, 0));
1405                                 break;
1406                         case 0x52:      /* CONFIG */
1407                                 res = i2c_smbus_read_byte_data(cl, 1);
1408                                 break;
1409                         case 0x53:      /* HYST */
1410                                 res = swab16(i2c_smbus_read_word_data(cl, 2));
1411                                 break;
1412                         case 0x55:      /* OVER */
1413                         default:
1414                                 res = swab16(i2c_smbus_read_word_data(cl, 3));
1415                                 break;
1416                         }
1417                 }
1418                 if (bank > 2)
1419                         i2c_smbus_write_byte_data(client, W83781D_REG_BANK, 0);
1420         }
1421         up(&data->lock);
1422         return res;
1423 }
1424
1425 static int
1426 w83781d_write_value(struct i2c_client *client, u16 reg, u16 value)
1427 {
1428         struct w83781d_data *data = i2c_get_clientdata(client);
1429         int word_sized, bank;
1430         struct i2c_client *cl;
1431
1432         down(&data->lock);
1433         if (i2c_is_isa_client(client)) {
1434                 word_sized = (((reg & 0xff00) == 0x100)
1435                               || ((reg & 0xff00) == 0x200))
1436                     && (((reg & 0x00ff) == 0x53)
1437                         || ((reg & 0x00ff) == 0x55));
1438                 if (reg & 0xff00) {
1439                         outb_p(W83781D_REG_BANK,
1440                                client->addr + W83781D_ADDR_REG_OFFSET);
1441                         outb_p(reg >> 8,
1442                                client->addr + W83781D_DATA_REG_OFFSET);
1443                 }
1444                 outb_p(reg & 0xff, client->addr + W83781D_ADDR_REG_OFFSET);
1445                 if (word_sized) {
1446                         outb_p(value >> 8,
1447                                client->addr + W83781D_DATA_REG_OFFSET);
1448                         outb_p((reg & 0xff) + 1,
1449                                client->addr + W83781D_ADDR_REG_OFFSET);
1450                 }
1451                 outb_p(value & 0xff, client->addr + W83781D_DATA_REG_OFFSET);
1452                 if (reg & 0xff00) {
1453                         outb_p(W83781D_REG_BANK,
1454                                client->addr + W83781D_ADDR_REG_OFFSET);
1455                         outb_p(0, client->addr + W83781D_DATA_REG_OFFSET);
1456                 }
1457         } else {
1458                 bank = (reg >> 8) & 0x0f;
1459                 if (bank > 2)
1460                         /* switch banks */
1461                         i2c_smbus_write_byte_data(client, W83781D_REG_BANK,
1462                                                   bank);
1463                 if (bank == 0 || bank > 2) {
1464                         i2c_smbus_write_byte_data(client, reg & 0xff,
1465                                                   value & 0xff);
1466                 } else {
1467                         /* switch to subclient */
1468                         cl = data->lm75[bank - 1];
1469                         /* convert from ISA to LM75 I2C addresses */
1470                         switch (reg & 0xff) {
1471                         case 0x52:      /* CONFIG */
1472                                 i2c_smbus_write_byte_data(cl, 1, value & 0xff);
1473                                 break;
1474                         case 0x53:      /* HYST */
1475                                 i2c_smbus_write_word_data(cl, 2, swab16(value));
1476                                 break;
1477                         case 0x55:      /* OVER */
1478                                 i2c_smbus_write_word_data(cl, 3, swab16(value));
1479                                 break;
1480                         }
1481                 }
1482                 if (bank > 2)
1483                         i2c_smbus_write_byte_data(client, W83781D_REG_BANK, 0);
1484         }
1485         up(&data->lock);
1486         return 0;
1487 }
1488
1489 /* Called when we have found a new W83781D. It should set limits, etc. */
1490 static void
1491 w83781d_init_client(struct i2c_client *client)
1492 {
1493         struct w83781d_data *data = i2c_get_clientdata(client);
1494         int vid = 0, i, p;
1495         int type = data->type;
1496         u8 tmp;
1497
1498         if (init && type != as99127f) { /* this resets registers we don't have
1499                                            documentation for on the as99127f */
1500                 /* save these registers */
1501                 i = w83781d_read_value(client, W83781D_REG_BEEP_CONFIG);
1502                 p = w83781d_read_value(client, W83781D_REG_PWMCLK12);
1503                 /* Reset all except Watchdog values and last conversion values
1504                    This sets fan-divs to 2, among others */
1505                 w83781d_write_value(client, W83781D_REG_CONFIG, 0x80);
1506                 /* Restore the registers and disable power-on abnormal beep.
1507                    This saves FAN 1/2/3 input/output values set by BIOS. */
1508                 w83781d_write_value(client, W83781D_REG_BEEP_CONFIG, i | 0x80);
1509                 w83781d_write_value(client, W83781D_REG_PWMCLK12, p);
1510                 /* Disable master beep-enable (reset turns it on).
1511                    Individual beep_mask should be reset to off but for some reason
1512                    disabling this bit helps some people not get beeped */
1513                 w83781d_write_value(client, W83781D_REG_BEEP_INTS2, 0);
1514         }
1515
1516         if (type != w83697hf) {
1517                 vid = w83781d_read_value(client, W83781D_REG_VID_FANDIV) & 0x0f;
1518                 vid |=
1519                     (w83781d_read_value(client, W83781D_REG_CHIPID) & 0x01) <<
1520                     4;
1521                 data->vrm = DEFAULT_VRM;
1522                 vid = vid_from_reg(vid, data->vrm);
1523         }
1524
1525         if ((type != w83781d) && (type != as99127f)) {
1526                 tmp = w83781d_read_value(client, W83781D_REG_SCFG1);
1527                 for (i = 1; i <= 3; i++) {
1528                         if (!(tmp & BIT_SCFG1[i - 1])) {
1529                                 data->sens[i - 1] = W83781D_DEFAULT_BETA;
1530                         } else {
1531                                 if (w83781d_read_value
1532                                     (client,
1533                                      W83781D_REG_SCFG2) & BIT_SCFG2[i - 1])
1534                                         data->sens[i - 1] = 1;
1535                                 else
1536                                         data->sens[i - 1] = 2;
1537                         }
1538                         if ((type == w83783s || type == w83697hf) && (i == 2))
1539                                 break;
1540                 }
1541         }
1542 #ifdef W83781D_RT
1543 /*
1544    Fill up the RT Tables.
1545    We assume that they are 32 bytes long, in order for temp 1-3.
1546    Data sheet documentation is sparse.
1547    We also assume that it is only for the 781D although I suspect
1548    that the others support it as well....
1549 */
1550
1551         if (init && type == w83781d) {
1552                 u16 k = 0;
1553 /*
1554     Auto-indexing doesn't seem to work...
1555     w83781d_write_value(client,W83781D_REG_RT_IDX,0);
1556 */
1557                 for (i = 0; i < 3; i++) {
1558                         int j;
1559                         for (j = 0; j < 32; j++) {
1560                                 w83781d_write_value(client,
1561                                                     W83781D_REG_RT_IDX, k++);
1562                                 data->rt[i][j] =
1563                                     w83781d_read_value(client,
1564                                                        W83781D_REG_RT_VAL);
1565                         }
1566                 }
1567         }
1568 #endif                          /* W83781D_RT */
1569
1570         if (init) {
1571                 if (type != w83783s && type != w83697hf) {
1572                         w83781d_write_value(client, W83781D_REG_TEMP3_CONFIG,
1573                                             0x00);
1574                 }
1575                 if (type != w83781d) {
1576                         /* enable comparator mode for temp2 and temp3 so
1577                            alarm indication will work correctly */
1578                         i = w83781d_read_value(client, W83781D_REG_IRQ);
1579                         if (!(i & 0x40))
1580                                 w83781d_write_value(client, W83781D_REG_IRQ,
1581                                                     i | 0x40);
1582                 }
1583         }
1584
1585         /* Start monitoring */
1586         w83781d_write_value(client, W83781D_REG_CONFIG,
1587                             (w83781d_read_value(client,
1588                                                 W83781D_REG_CONFIG) & 0xf7)
1589                             | 0x01);
1590 }
1591
1592 static struct w83781d_data *w83781d_update_device(struct device *dev)
1593 {
1594         struct i2c_client *client = to_i2c_client(dev);
1595         struct w83781d_data *data = i2c_get_clientdata(client);
1596         int i;
1597
1598         down(&data->update_lock);
1599
1600         if (time_after
1601             (jiffies - data->last_updated, (unsigned long) (HZ + HZ / 2))
1602             || time_before(jiffies, data->last_updated) || !data->valid) {
1603                 pr_debug("Starting device update\n");
1604
1605                 for (i = 0; i <= 8; i++) {
1606                         if ((data->type == w83783s || data->type == w83697hf)
1607                             && (i == 1))
1608                                 continue;       /* 783S has no in1 */
1609                         data->in[i] =
1610                             w83781d_read_value(client, W83781D_REG_IN(i));
1611                         data->in_min[i] =
1612                             w83781d_read_value(client, W83781D_REG_IN_MIN(i));
1613                         data->in_max[i] =
1614                             w83781d_read_value(client, W83781D_REG_IN_MAX(i));
1615                         if ((data->type != w83782d) && (data->type != w83697hf)
1616                             && (data->type != w83627hf) && (i == 6))
1617                                 break;
1618                 }
1619                 for (i = 1; i <= 3; i++) {
1620                         data->fan[i - 1] =
1621                             w83781d_read_value(client, W83781D_REG_FAN(i));
1622                         data->fan_min[i - 1] =
1623                             w83781d_read_value(client, W83781D_REG_FAN_MIN(i));
1624                 }
1625                 if (data->type != w83781d && data->type != as99127f) {
1626                         for (i = 1; i <= 4; i++) {
1627                                 data->pwm[i - 1] =
1628                                     w83781d_read_value(client,
1629                                                        W83781D_REG_PWM(i));
1630                                 if ((data->type != w83782d
1631                                      || i2c_is_isa_client(client))
1632                                     && i == 2)
1633                                         break;
1634                         }
1635                         /* Only PWM2 can be disabled */
1636                         data->pwmenable[1] = (w83781d_read_value(client,
1637                                               W83781D_REG_PWMCLK12) & 0x08) >> 3;
1638                 }
1639
1640                 data->temp = w83781d_read_value(client, W83781D_REG_TEMP(1));
1641                 data->temp_max =
1642                     w83781d_read_value(client, W83781D_REG_TEMP_OVER(1));
1643                 data->temp_max_hyst =
1644                     w83781d_read_value(client, W83781D_REG_TEMP_HYST(1));
1645                 data->temp_add[0] =
1646                     w83781d_read_value(client, W83781D_REG_TEMP(2));
1647                 data->temp_max_add[0] =
1648                     w83781d_read_value(client, W83781D_REG_TEMP_OVER(2));
1649                 data->temp_max_hyst_add[0] =
1650                     w83781d_read_value(client, W83781D_REG_TEMP_HYST(2));
1651                 if (data->type != w83783s && data->type != w83697hf) {
1652                         data->temp_add[1] =
1653                             w83781d_read_value(client, W83781D_REG_TEMP(3));
1654                         data->temp_max_add[1] =
1655                             w83781d_read_value(client,
1656                                                W83781D_REG_TEMP_OVER(3));
1657                         data->temp_max_hyst_add[1] =
1658                             w83781d_read_value(client,
1659                                                W83781D_REG_TEMP_HYST(3));
1660                 }
1661                 i = w83781d_read_value(client, W83781D_REG_VID_FANDIV);
1662                 if (data->type != w83697hf) {
1663                         data->vid = i & 0x0f;
1664                         data->vid |=
1665                             (w83781d_read_value(client, W83781D_REG_CHIPID) &
1666                              0x01)
1667                             << 4;
1668                 }
1669                 data->fan_div[0] = (i >> 4) & 0x03;
1670                 data->fan_div[1] = (i >> 6) & 0x03;
1671                 if (data->type != w83697hf) {
1672                         data->fan_div[2] = (w83781d_read_value(client,
1673                                                                W83781D_REG_PIN)
1674                                             >> 6) & 0x03;
1675                 }
1676                 if ((data->type != w83781d) && (data->type != as99127f)) {
1677                         i = w83781d_read_value(client, W83781D_REG_VBAT);
1678                         data->fan_div[0] |= (i >> 3) & 0x04;
1679                         data->fan_div[1] |= (i >> 4) & 0x04;
1680                         if (data->type != w83697hf)
1681                                 data->fan_div[2] |= (i >> 5) & 0x04;
1682                 }
1683                 data->alarms =
1684                     w83781d_read_value(client,
1685                                        W83781D_REG_ALARM1) +
1686                     (w83781d_read_value(client, W83781D_REG_ALARM2) << 8);
1687                 if ((data->type == w83782d) || (data->type == w83627hf)) {
1688                         data->alarms |=
1689                             w83781d_read_value(client,
1690                                                W83781D_REG_ALARM3) << 16;
1691                 }
1692                 i = w83781d_read_value(client, W83781D_REG_BEEP_INTS2);
1693                 data->beep_enable = i >> 7;
1694                 data->beep_mask = ((i & 0x7f) << 8) +
1695                     w83781d_read_value(client, W83781D_REG_BEEP_INTS1);
1696                 if ((data->type != w83781d) && (data->type != as99127f)) {
1697                         data->beep_mask |=
1698                             w83781d_read_value(client,
1699                                                W83781D_REG_BEEP_INTS3) << 16;
1700                 }
1701                 data->last_updated = jiffies;
1702                 data->valid = 1;
1703         }
1704
1705         up(&data->update_lock);
1706
1707         return data;
1708 }
1709
1710 static int __init
1711 sensors_w83781d_init(void)
1712 {
1713         return i2c_add_driver(&w83781d_driver);
1714 }
1715
1716 static void __exit
1717 sensors_w83781d_exit(void)
1718 {
1719         i2c_del_driver(&w83781d_driver);
1720 }
1721
1722 MODULE_AUTHOR("Frodo Looijaard <frodol@dds.nl>, "
1723               "Philip Edelbrock <phil@netroedge.com>, "
1724               "and Mark Studebaker <mdsxyz123@yahoo.com>");
1725 MODULE_DESCRIPTION("W83781D driver");
1726 MODULE_LICENSE("GPL");
1727
1728 module_init(sensors_w83781d_init);
1729 module_exit(sensors_w83781d_exit);