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