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