root/lm-sensors/trunk/kernel/chips/w83627hf.c @ 2828

Revision 2828, 45.7 KB (checked in by khali, 8 years ago)

Better VID support for the W83627THF (backport from Linux 2.6).

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
Line 
1/*
2    w83627hf.c - Part of lm_sensors, Linux kernel modules for hardware
3                monitoring
4    Copyright (c) 1998 - 2003  Frodo Looijaard <frodol@dds.nl>,
5    Philip Edelbrock <phil@netroedge.com>,
6    and Mark Studebaker <mdsxyz123@yahoo.com>
7
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    w83627hf    9       3       2       3       0x20    0x5ca3  no      yes(LPC)
28    w83627thf   7       3       3       3       0x90    0x5ca3  no      yes(LPC)
29    w83637hf    7       3       3       3       0x80    0x5ca3  no      yes(LPC)
30    w83697hf    8       2       2       2       0x60    0x5ca3  no      yes(LPC)
31
32    For other winbond chips, and for i2c support in the above chips,
33    use w83781d.c.
34
35    Note: automatic ("cruise") fan control for 697, 637 & 627thf not
36    supported yet.
37*/
38
39#include <linux/module.h>
40#include <linux/slab.h>
41#include <linux/ioport.h>
42#include <linux/i2c.h>
43#include <linux/i2c-proc.h>
44#include <linux/init.h>
45#include <asm/io.h>
46#include "version.h"
47#include "sensors_vid.h"
48#include "lm75.h"
49
50static int force_addr;
51MODULE_PARM(force_addr, "i");
52MODULE_PARM_DESC(force_addr,
53                 "Initialize the base address of the sensors");
54static int force_i2c = 0x1f;
55MODULE_PARM(force_i2c, "i");
56MODULE_PARM_DESC(force_i2c,
57                 "Initialize the i2c address of the sensors");
58
59/* Addresses to scan */
60static unsigned short normal_i2c[] = { SENSORS_I2C_END };
61static unsigned short normal_i2c_range[] = { SENSORS_I2C_END };
62static unsigned int normal_isa[] = { 0, SENSORS_ISA_END };
63static unsigned int normal_isa_range[] = { SENSORS_ISA_END };
64
65/* Insmod parameters */
66SENSORS_INSMOD_4(w83627hf, w83627thf, w83697hf, w83637hf);
67
68static int init = 1;
69MODULE_PARM(init, "i");
70MODULE_PARM_DESC(init, "Set to zero to bypass chip initialization");
71
72/* modified from kernel/include/traps.c */
73#define REG     0x2e    /* The register to read/write */
74#define DEV     0x07    /* Register: Logical device select */
75#define VAL     0x2f    /* The value to read/write */
76
77/* logical device numbers for superio_select (below) */
78#define W83627HF_LD_FDC         0x00
79#define W83627HF_LD_PRT         0x01
80#define W83627HF_LD_UART1       0x02
81#define W83627HF_LD_UART2       0x03
82#define W83627HF_LD_KBC         0x05
83#define W83627HF_LD_CIR         0x06 /* w83627hf only */
84#define W83627HF_LD_GAME        0x07
85#define W83627HF_LD_MIDI        0x07
86#define W83627HF_LD_GPIO1       0x07
87#define W83627HF_LD_GPIO5       0x07 /* w83627thf only */
88#define W83627HF_LD_GPIO2       0x08
89#define W83627HF_LD_GPIO3       0x09
90#define W83627HF_LD_GPIO4       0x09 /* w83627thf only */
91#define W83627HF_LD_ACPI        0x0a
92#define W83627HF_LD_HWM         0x0b
93
94#define DEVID   0x20    /* Register: Device ID */
95
96#define W83627THF_GPIO5_EN      0x30 /* w83627thf only */
97#define W83627THF_GPIO5_IOSR    0xf3 /* w83627thf only */
98#define W83627THF_GPIO5_DR      0xf4 /* w83627thf only */
99
100static inline void
101superio_outb(int reg, int val)
102{
103        outb(reg, REG);
104        outb(val, VAL);
105}
106
107static inline int
108superio_inb(int reg)
109{
110        outb(reg, REG);
111        return inb(VAL);
112}
113
114static inline void
115superio_select(int ld)
116{
117        outb(DEV, REG);
118        outb(ld, VAL);
119}
120
121static inline void
122superio_enter(void)
123{
124        outb(0x87, REG);
125        outb(0x87, REG);
126}
127
128static inline void
129superio_exit(void)
130{
131        outb(0xAA, REG);
132}
133
134#define W627_DEVID 0x52
135#define W627THF_DEVID 0x82
136#define W697_DEVID 0x60
137#define W637_DEVID 0x70
138#define WINB_ACT_REG 0x30
139#define WINB_BASE_REG 0x60
140/* Constants specified below */
141
142/* Length of ISA address segment */
143#define WINB_EXTENT 8
144
145/* Where are the ISA address/data registers relative to the base address */
146#define W83781D_ADDR_REG_OFFSET 5
147#define W83781D_DATA_REG_OFFSET 6
148
149/* The W83781D registers */
150/* The W83782D registers for nr=7,8 are in bank 5 */
151#define W83781D_REG_IN_MAX(nr) ((nr < 7) ? (0x2b + (nr) * 2) : \
152                                           (0x554 + (((nr) - 7) * 2)))
153#define W83781D_REG_IN_MIN(nr) ((nr < 7) ? (0x2c + (nr) * 2) : \
154                                           (0x555 + (((nr) - 7) * 2)))
155#define W83781D_REG_IN(nr)     ((nr < 7) ? (0x20 + (nr)) : \
156                                           (0x550 + (nr) - 7))
157
158#define W83781D_REG_FAN_MIN(nr) (0x3a + (nr))
159#define W83781D_REG_FAN(nr) (0x27 + (nr))
160
161#define W83781D_REG_TEMP2 0x0150
162#define W83781D_REG_TEMP3 0x0250
163#define W83781D_REG_TEMP2_HYST 0x153
164#define W83781D_REG_TEMP3_HYST 0x253
165#define W83781D_REG_TEMP2_CONFIG 0x152
166#define W83781D_REG_TEMP3_CONFIG 0x252
167#define W83781D_REG_TEMP2_OVER 0x155
168#define W83781D_REG_TEMP3_OVER 0x255
169
170#define W83781D_REG_TEMP 0x27
171#define W83781D_REG_TEMP_OVER 0x39
172#define W83781D_REG_TEMP_HYST 0x3A
173#define W83781D_REG_BANK 0x4E
174
175#define W83781D_REG_CONFIG 0x40
176#define W83781D_REG_ALARM1 0x41
177#define W83781D_REG_ALARM2 0x42
178#define W83781D_REG_ALARM3 0x450
179
180#define W83781D_REG_IRQ 0x4C
181#define W83781D_REG_BEEP_CONFIG 0x4D
182#define W83781D_REG_BEEP_INTS1 0x56
183#define W83781D_REG_BEEP_INTS2 0x57
184#define W83781D_REG_BEEP_INTS3 0x453
185
186#define W83781D_REG_VID_FANDIV 0x47
187
188#define W83781D_REG_CHIPID 0x49
189#define W83781D_REG_WCHIPID 0x58
190#define W83781D_REG_CHIPMAN 0x4F
191#define W83781D_REG_PIN 0x4B
192
193#define W83781D_REG_VBAT 0x5D
194
195#define W83627HF_REG_PWM1 0x5A
196#define W83627HF_REG_PWM2 0x5B
197#define W83627HF_REG_PWMCLK12 0x5C
198
199#define W83627THF_REG_PWM1              0x01    /* 697HF and 637HF too */
200#define W83627THF_REG_PWM2              0x03    /* 697HF and 637HF too */
201#define W83627THF_REG_PWM3              0x11    /* 637HF too */
202
203#define W83627THF_REG_VRM_OVT_CFG       0x18    /* 637HF too */
204
205static const u8 regpwm_627hf[] = { W83627HF_REG_PWM1, W83627HF_REG_PWM2 };
206static const u8 regpwm[] = { W83627THF_REG_PWM1, W83627THF_REG_PWM2,
207                             W83627THF_REG_PWM3 };
208#define W836X7HF_REG_PWM(type, nr) (((type) == w83627hf) ? \
209                                     regpwm_627hf[(nr) - 1] : regpwm[(nr) - 1])
210
211#define W83781D_REG_I2C_ADDR 0x48
212#define W83781D_REG_I2C_SUBADDR 0x4A
213
214/* Sensor selection */
215#define W83781D_REG_SCFG1 0x5D
216static const u8 BIT_SCFG1[] = { 0x02, 0x04, 0x08 };
217#define W83781D_REG_SCFG2 0x59
218static const u8 BIT_SCFG2[] = { 0x10, 0x20, 0x40 };
219#define W83781D_DEFAULT_BETA 3435
220
221/* Conversions. Limit checking is only done on the TO_REG
222   variants. Note that you should be a bit careful with which arguments
223   these macros are called: arguments may be evaluated more than once.
224   Fixing this is just not worth it. */
225#define IN_TO_REG(val)  (SENSORS_LIMIT((((val) * 10 + 8)/16),0,255))
226#define IN_FROM_REG(val) (((val) * 16 + 5) / 10)
227
228#define IN_TO_REG_VRM9(val) \
229        (SENSORS_LIMIT((((val) * 1000 - 70000 + 244) / 488), 0, 255))
230#define IN_FROM_REG_VRM9(reg)   (((reg) * 488 + 70000 + 500) / 1000)
231
232static inline u8 FAN_TO_REG(long rpm, int div)
233{
234        if (rpm == 0)
235                return 255;
236        rpm = SENSORS_LIMIT(rpm, 1, 1000000);
237        return SENSORS_LIMIT((1350000 + rpm * div / 2) / (rpm * div), 1,
238                             254);
239}
240
241#define TEMP_MIN (-1280)
242#define TEMP_MAX ( 1270)
243
244/* TEMP: 1/10 degrees C (-128C to +127C)
245   REG: 1C/bit, two's complement */
246static u8 TEMP_TO_REG(int temp)
247{
248        int ntemp = SENSORS_LIMIT(temp, TEMP_MIN, TEMP_MAX);
249        ntemp += (ntemp<0 ? -5 : 5);
250        return (u8)(ntemp / 10);
251}
252                                                                               
253static int TEMP_FROM_REG(u8 reg)
254{
255        return (s8)reg * 10;
256}
257
258#define FAN_FROM_REG(val,div) ((val)==0?-1:(val)==255?0:1350000/((val)*(div)))
259
260#define PWM_TO_REG(val) (SENSORS_LIMIT((val),0,255))
261#define BEEPS_TO_REG(val) ((val) & 0xffffff)
262
263#define BEEP_ENABLE_TO_REG(val)   ((val)?1:0)
264#define BEEP_ENABLE_FROM_REG(val) ((val)?1:0)
265
266#define DIV_FROM_REG(val) (1 << (val))
267
268static inline u8 DIV_TO_REG(long val)
269{
270        int i;
271        val = SENSORS_LIMIT(val, 1, 128) >> 1;
272        for (i = 0; i < 6; i++) {
273                if (val == 0)
274                        break;
275                val >>= 1;
276        }
277        return ((u8) i);
278}
279
280/* For each registered chip, we need to keep some data in memory. That
281   data is pointed to by w83627hf_list[NR]->data. The structure itself is
282   dynamically allocated, at the same time when a new client is allocated. */
283struct w83627hf_data {
284        struct i2c_client client;
285        struct semaphore lock;
286        int sysctl_id;
287        enum chips type;
288
289        struct semaphore update_lock;
290        char valid;             /* !=0 if following fields are valid */
291        unsigned long last_updated;     /* In jiffies */
292
293        struct i2c_client *lm75;        /* for secondary I2C addresses */
294        /* pointer to array of 2 subclients */
295
296        u8 in[9];               /* Register value */
297        u8 in_max[9];           /* Register value */
298        u8 in_min[9];           /* Register value */
299        u8 fan[3];              /* Register value */
300        u8 fan_min[3];          /* Register value */
301        u8 temp;
302        u8 temp_over;           /* Register value */
303        u8 temp_hyst;           /* Register value */
304        u16 temp_add[2];        /* Register value */
305        u16 temp_add_over[2];   /* Register value */
306        u16 temp_add_hyst[2];   /* Register value */
307        u8 fan_div[3];          /* Register encoding, shifted right */
308        u8 vid;                 /* Register encoding, combined */
309        u32 alarms;             /* Register encoding, combined */
310        u32 beeps;              /* Register encoding, combined */
311        u8 beep_enable;         /* Boolean */
312        u8 pwm[3];              /* Register value */
313        u8 pwmenable[3];        /* bool */
314        u16 sens[3];            /* 782D/783S only.
315                                   1 = pentium diode; 2 = 3904 diode;
316                                   3000-5000 = thermistor beta.
317                                   Default = 3435.
318                                   Other Betas unimplemented */
319        u8 vrm;
320        u8 vrm_ovt;             /* Register value, 627thf & 637hf only */
321};
322
323
324static int w83627hf_attach_adapter(struct i2c_adapter *adapter);
325static int w83627hf_detect(struct i2c_adapter *adapter, int address,
326                          unsigned short flags, int kind);
327static int w83627hf_detach_client(struct i2c_client *client);
328
329static int w83627hf_read_value(struct i2c_client *client, u16 register);
330static int w83627hf_write_value(struct i2c_client *client, u16 register,
331                               u16 value);
332static void w83627hf_update_client(struct i2c_client *client);
333static void w83627hf_init_client(struct i2c_client *client);
334
335
336static void w83627hf_in(struct i2c_client *client, int operation,
337                       int ctl_name, int *nrels_mag, long *results);
338static void w83627hf_fan(struct i2c_client *client, int operation,
339                        int ctl_name, int *nrels_mag, long *results);
340static void w83627hf_temp(struct i2c_client *client, int operation,
341                         int ctl_name, int *nrels_mag, long *results);
342static void w83627hf_temp_add(struct i2c_client *client, int operation,
343                             int ctl_name, int *nrels_mag, long *results);
344static void w83627hf_vid(struct i2c_client *client, int operation,
345                        int ctl_name, int *nrels_mag, long *results);
346static void w83627hf_vrm(struct i2c_client *client, int operation,
347                        int ctl_name, int *nrels_mag, long *results);
348static void w83627hf_alarms(struct i2c_client *client, int operation,
349                           int ctl_name, int *nrels_mag, long *results);
350static void w83627hf_beep(struct i2c_client *client, int operation,
351                         int ctl_name, int *nrels_mag, long *results);
352static void w83627hf_fan_div(struct i2c_client *client, int operation,
353                            int ctl_name, int *nrels_mag, long *results);
354static void w83627hf_pwm(struct i2c_client *client, int operation,
355                        int ctl_name, int *nrels_mag, long *results);
356static void w83627hf_sens(struct i2c_client *client, int operation,
357                         int ctl_name, int *nrels_mag, long *results);
358
359static int w83627hf_id = 0;
360
361static struct i2c_driver w83627hf_driver = {
362        .name           = "W83627HF sensor driver",
363        .id             = I2C_DRIVERID_W83627HF,
364        .flags          = I2C_DF_NOTIFY,
365        .attach_adapter = w83627hf_attach_adapter,
366        .detach_client  = w83627hf_detach_client,
367};
368
369/* The /proc/sys entries */
370/* WARNING these are copied from w83781d.c and have not been renamed.
371   Note that the 627hf and 697hf are supported by both drivers.
372   Do not make incompatible changes here or we will have errors
373   in the generated file ../include/sensors.h !!!
374*/
375/* -- SENSORS SYSCTL START -- */
376
377#define W83781D_SYSCTL_IN0 1000 /* Volts * 100 */
378#define W83781D_SYSCTL_IN1 1001
379#define W83781D_SYSCTL_IN2 1002
380#define W83781D_SYSCTL_IN3 1003
381#define W83781D_SYSCTL_IN4 1004
382#define W83781D_SYSCTL_IN5 1005
383#define W83781D_SYSCTL_IN6 1006
384#define W83781D_SYSCTL_IN7 1007
385#define W83781D_SYSCTL_IN8 1008
386#define W83781D_SYSCTL_FAN1 1101        /* Rotations/min */
387#define W83781D_SYSCTL_FAN2 1102
388#define W83781D_SYSCTL_FAN3 1103
389#define W83781D_SYSCTL_TEMP1 1200       /* Degrees Celcius * 10 */
390#define W83781D_SYSCTL_TEMP2 1201       /* Degrees Celcius * 10 */
391#define W83781D_SYSCTL_TEMP3 1202       /* Degrees Celcius * 10 */
392#define W83781D_SYSCTL_VID 1300         /* Volts * 1000 */
393#define W83781D_SYSCTL_VRM 1301
394#define W83781D_SYSCTL_PWM1 1401
395#define W83781D_SYSCTL_PWM2 1402
396#define W83781D_SYSCTL_PWM3 1403
397#define W83781D_SYSCTL_SENS1 1501       /* 1, 2, or Beta (3000-5000) */
398#define W83781D_SYSCTL_SENS2 1502
399#define W83781D_SYSCTL_SENS3 1503
400#define W83781D_SYSCTL_FAN_DIV 2000     /* 1, 2, 4 or 8 */
401#define W83781D_SYSCTL_ALARMS 2001      /* bitvector */
402#define W83781D_SYSCTL_BEEP 2002        /* bitvector */
403
404#define W83781D_ALARM_IN0 0x0001
405#define W83781D_ALARM_IN1 0x0002
406#define W83781D_ALARM_IN2 0x0004
407#define W83781D_ALARM_IN3 0x0008
408#define W83781D_ALARM_IN4 0x0100
409#define W83781D_ALARM_IN5 0x0200
410#define W83781D_ALARM_IN6 0x0400
411#define W83782D_ALARM_IN7 0x10000
412#define W83782D_ALARM_IN8 0x20000
413#define W83781D_ALARM_FAN1 0x0040
414#define W83781D_ALARM_FAN2 0x0080
415#define W83781D_ALARM_FAN3 0x0800
416#define W83781D_ALARM_TEMP1 0x0010
417#define W83781D_ALARM_TEMP23 0x0020     /* 781D only */
418#define W83781D_ALARM_TEMP2 0x0020      /* 782D/783S */
419#define W83781D_ALARM_TEMP3 0x2000      /* 782D only */
420#define W83781D_ALARM_CHAS 0x1000
421
422/* -- SENSORS SYSCTL END -- */
423
424/* These files are created for each detected chip. This is just a template;
425   though at first sight, you might think we could use a statically
426   allocated list, we need some way to get back to the parent - which
427   is done through one of the 'extra' fields which are initialized
428   when a new copy is allocated. */
429
430/* without pwm3-4 */
431static ctl_table w83627hf_dir_table_template[] = {
432        {W83781D_SYSCTL_IN0, "in0", NULL, 0, 0644, NULL, &i2c_proc_real,
433         &i2c_sysctl_real, NULL, &w83627hf_in},
434        {W83781D_SYSCTL_IN1, "in1", NULL, 0, 0644, NULL, &i2c_proc_real,
435         &i2c_sysctl_real, NULL, &w83627hf_in},
436        {W83781D_SYSCTL_IN2, "in2", NULL, 0, 0644, NULL, &i2c_proc_real,
437         &i2c_sysctl_real, NULL, &w83627hf_in},
438        {W83781D_SYSCTL_IN3, "in3", NULL, 0, 0644, NULL, &i2c_proc_real,
439         &i2c_sysctl_real, NULL, &w83627hf_in},
440        {W83781D_SYSCTL_IN4, "in4", NULL, 0, 0644, NULL, &i2c_proc_real,
441         &i2c_sysctl_real, NULL, &w83627hf_in},
442        {W83781D_SYSCTL_IN5, "in5", NULL, 0, 0644, NULL, &i2c_proc_real,
443         &i2c_sysctl_real, NULL, &w83627hf_in},
444        {W83781D_SYSCTL_IN6, "in6", NULL, 0, 0644, NULL, &i2c_proc_real,
445         &i2c_sysctl_real, NULL, &w83627hf_in},
446        {W83781D_SYSCTL_IN7, "in7", NULL, 0, 0644, NULL, &i2c_proc_real,
447         &i2c_sysctl_real, NULL, &w83627hf_in},
448        {W83781D_SYSCTL_IN8, "in8", NULL, 0, 0644, NULL, &i2c_proc_real,
449         &i2c_sysctl_real, NULL, &w83627hf_in},
450        {W83781D_SYSCTL_FAN1, "fan1", NULL, 0, 0644, NULL, &i2c_proc_real,
451         &i2c_sysctl_real, NULL, &w83627hf_fan},
452        {W83781D_SYSCTL_FAN2, "fan2", NULL, 0, 0644, NULL, &i2c_proc_real,
453         &i2c_sysctl_real, NULL, &w83627hf_fan},
454        {W83781D_SYSCTL_FAN3, "fan3", NULL, 0, 0644, NULL, &i2c_proc_real,
455         &i2c_sysctl_real, NULL, &w83627hf_fan},
456        {W83781D_SYSCTL_TEMP1, "temp1", NULL, 0, 0644, NULL, &i2c_proc_real,
457         &i2c_sysctl_real, NULL, &w83627hf_temp},
458        {W83781D_SYSCTL_TEMP2, "temp2", NULL, 0, 0644, NULL, &i2c_proc_real,
459         &i2c_sysctl_real, NULL, &w83627hf_temp_add},
460        {W83781D_SYSCTL_TEMP3, "temp3", NULL, 0, 0644, NULL, &i2c_proc_real,
461         &i2c_sysctl_real, NULL, &w83627hf_temp_add},
462        {W83781D_SYSCTL_VID, "vid", NULL, 0, 0444, NULL, &i2c_proc_real,
463         &i2c_sysctl_real, NULL, &w83627hf_vid},
464        {W83781D_SYSCTL_VRM, "vrm", NULL, 0, 0644, NULL, &i2c_proc_real,
465         &i2c_sysctl_real, NULL, &w83627hf_vrm},
466        {W83781D_SYSCTL_FAN_DIV, "fan_div", NULL, 0, 0644, NULL, &i2c_proc_real,
467         &i2c_sysctl_real, NULL, &w83627hf_fan_div},
468        {W83781D_SYSCTL_ALARMS, "alarms", NULL, 0, 0444, NULL, &i2c_proc_real,
469         &i2c_sysctl_real, NULL, &w83627hf_alarms},
470        {W83781D_SYSCTL_BEEP, "beep", NULL, 0, 0644, NULL, &i2c_proc_real,
471         &i2c_sysctl_real, NULL, &w83627hf_beep},
472        {W83781D_SYSCTL_PWM1, "pwm1", NULL, 0, 0644, NULL, &i2c_proc_real,
473         &i2c_sysctl_real, NULL, &w83627hf_pwm},
474        {W83781D_SYSCTL_PWM2, "pwm2", NULL, 0, 0644, NULL, &i2c_proc_real,
475         &i2c_sysctl_real, NULL, &w83627hf_pwm},
476        {W83781D_SYSCTL_SENS1, "sensor1", NULL, 0, 0644, NULL, &i2c_proc_real,
477         &i2c_sysctl_real, NULL, &w83627hf_sens},
478        {W83781D_SYSCTL_SENS2, "sensor2", NULL, 0, 0644, NULL, &i2c_proc_real,
479         &i2c_sysctl_real, NULL, &w83627hf_sens},
480        {W83781D_SYSCTL_SENS3, "sensor3", NULL, 0, 0644, NULL, &i2c_proc_real,
481         &i2c_sysctl_real, NULL, &w83627hf_sens},
482        {0}
483};
484
485/* similar to w83782d but no fan3, no vid */
486static ctl_table w83697hf_dir_table_template[] = {
487        {W83781D_SYSCTL_IN0, "in0", NULL, 0, 0644, NULL, &i2c_proc_real,
488         &i2c_sysctl_real, NULL, &w83627hf_in},
489        /* no in1 to maintain compatibility with 781d and 782d. */
490        {W83781D_SYSCTL_IN2, "in2", NULL, 0, 0644, NULL, &i2c_proc_real,
491         &i2c_sysctl_real, NULL, &w83627hf_in},
492        {W83781D_SYSCTL_IN3, "in3", NULL, 0, 0644, NULL, &i2c_proc_real,
493         &i2c_sysctl_real, NULL, &w83627hf_in},
494        {W83781D_SYSCTL_IN4, "in4", NULL, 0, 0644, NULL, &i2c_proc_real,
495         &i2c_sysctl_real, NULL, &w83627hf_in},
496        {W83781D_SYSCTL_IN5, "in5", NULL, 0, 0644, NULL, &i2c_proc_real,
497         &i2c_sysctl_real, NULL, &w83627hf_in},
498        {W83781D_SYSCTL_IN6, "in6", NULL, 0, 0644, NULL, &i2c_proc_real,
499         &i2c_sysctl_real, NULL, &w83627hf_in},
500        {W83781D_SYSCTL_IN7, "in7", NULL, 0, 0644, NULL, &i2c_proc_real,
501         &i2c_sysctl_real, NULL, &w83627hf_in},
502        {W83781D_SYSCTL_IN8, "in8", NULL, 0, 0644, NULL, &i2c_proc_real,
503         &i2c_sysctl_real, NULL, &w83627hf_in},
504        {W83781D_SYSCTL_FAN1, "fan1", NULL, 0, 0644, NULL, &i2c_proc_real,
505         &i2c_sysctl_real, NULL, &w83627hf_fan},
506        {W83781D_SYSCTL_FAN2, "fan2", NULL, 0, 0644, NULL, &i2c_proc_real,
507         &i2c_sysctl_real, NULL, &w83627hf_fan},
508        {W83781D_SYSCTL_TEMP1, "temp1", NULL, 0, 0644, NULL, &i2c_proc_real,
509         &i2c_sysctl_real, NULL, &w83627hf_temp},
510        {W83781D_SYSCTL_TEMP2, "temp2", NULL, 0, 0644, NULL, &i2c_proc_real,
511         &i2c_sysctl_real, NULL, &w83627hf_temp_add},
512        {W83781D_SYSCTL_FAN_DIV, "fan_div", NULL, 0, 0644, NULL, &i2c_proc_real,
513         &i2c_sysctl_real, NULL, &w83627hf_fan_div},
514        {W83781D_SYSCTL_ALARMS, "alarms", NULL, 0, 0444, NULL, &i2c_proc_real,
515         &i2c_sysctl_real, NULL, &w83627hf_alarms},
516        {W83781D_SYSCTL_BEEP, "beep", NULL, 0, 0644, NULL, &i2c_proc_real,
517         &i2c_sysctl_real, NULL, &w83627hf_beep},
518        {W83781D_SYSCTL_PWM1, "pwm1", NULL, 0, 0644, NULL, &i2c_proc_real,
519         &i2c_sysctl_real, NULL, &w83627hf_pwm},
520        {W83781D_SYSCTL_PWM2, "pwm2", NULL, 0, 0644, NULL, &i2c_proc_real,
521         &i2c_sysctl_real, NULL, &w83627hf_pwm},
522        {W83781D_SYSCTL_SENS1, "sensor1", NULL, 0, 0644, NULL, &i2c_proc_real,
523         &i2c_sysctl_real, NULL, &w83627hf_sens},
524        {W83781D_SYSCTL_SENS2, "sensor2", NULL, 0, 0644, NULL, &i2c_proc_real,
525         &i2c_sysctl_real, NULL, &w83627hf_sens},
526        {0}
527};
528
529/* no in5 and in6 */
530/* We use this one for W83637HF too */
531static ctl_table w83627thf_dir_table_template[] = {
532        {W83781D_SYSCTL_IN0, "in0", NULL, 0, 0644, NULL, &i2c_proc_real,
533         &i2c_sysctl_real, NULL, &w83627hf_in},
534        {W83781D_SYSCTL_IN1, "in1", NULL, 0, 0644, NULL, &i2c_proc_real,
535         &i2c_sysctl_real, NULL, &w83627hf_in},
536        {W83781D_SYSCTL_IN2, "in2", NULL, 0, 0644, NULL, &i2c_proc_real,
537         &i2c_sysctl_real, NULL, &w83627hf_in},
538        {W83781D_SYSCTL_IN3, "in3", NULL, 0, 0644, NULL, &i2c_proc_real,
539         &i2c_sysctl_real, NULL, &w83627hf_in},
540        {W83781D_SYSCTL_IN4, "in4", NULL, 0, 0644, NULL, &i2c_proc_real,
541         &i2c_sysctl_real, NULL, &w83627hf_in},
542        {W83781D_SYSCTL_IN7, "in7", NULL, 0, 0644, NULL, &i2c_proc_real,
543         &i2c_sysctl_real, NULL, &w83627hf_in},
544        {W83781D_SYSCTL_IN8, "in8", NULL, 0, 0644, NULL, &i2c_proc_real,
545         &i2c_sysctl_real, NULL, &w83627hf_in},
546        {W83781D_SYSCTL_FAN1, "fan1", NULL, 0, 0644, NULL, &i2c_proc_real,
547         &i2c_sysctl_real, NULL, &w83627hf_fan},
548        {W83781D_SYSCTL_FAN2, "fan2", NULL, 0, 0644, NULL, &i2c_proc_real,
549         &i2c_sysctl_real, NULL, &w83627hf_fan},
550        {W83781D_SYSCTL_FAN3, "fan3", NULL, 0, 0644, NULL, &i2c_proc_real,
551         &i2c_sysctl_real, NULL, &w83627hf_fan},
552        {W83781D_SYSCTL_TEMP1, "temp1", NULL, 0, 0644, NULL, &i2c_proc_real,
553         &i2c_sysctl_real, NULL, &w83627hf_temp},
554        {W83781D_SYSCTL_TEMP2, "temp2", NULL, 0, 0644, NULL, &i2c_proc_real,
555         &i2c_sysctl_real, NULL, &w83627hf_temp_add},
556        {W83781D_SYSCTL_TEMP3, "temp3", NULL, 0, 0644, NULL, &i2c_proc_real,
557         &i2c_sysctl_real, NULL, &w83627hf_temp_add},
558        {W83781D_SYSCTL_VID, "vid", NULL, 0, 0444, NULL, &i2c_proc_real,
559         &i2c_sysctl_real, NULL, &w83627hf_vid},
560        {W83781D_SYSCTL_VRM, "vrm", NULL, 0, 0644, NULL, &i2c_proc_real,
561         &i2c_sysctl_real, NULL, &w83627hf_vrm},
562        {W83781D_SYSCTL_FAN_DIV, "fan_div", NULL, 0, 0644, NULL, &i2c_proc_real,
563         &i2c_sysctl_real, NULL, &w83627hf_fan_div},
564        {W83781D_SYSCTL_ALARMS, "alarms", NULL, 0, 0444, NULL, &i2c_proc_real,
565         &i2c_sysctl_real, NULL, &w83627hf_alarms},
566        {W83781D_SYSCTL_BEEP, "beep", NULL, 0, 0644, NULL, &i2c_proc_real,
567         &i2c_sysctl_real, NULL, &w83627hf_beep},
568        {W83781D_SYSCTL_PWM1, "pwm1", NULL, 0, 0644, NULL, &i2c_proc_real,
569         &i2c_sysctl_real, NULL, &w83627hf_pwm},
570        {W83781D_SYSCTL_PWM2, "pwm2", NULL, 0, 0644, NULL, &i2c_proc_real,
571         &i2c_sysctl_real, NULL, &w83627hf_pwm},
572        {W83781D_SYSCTL_PWM3, "pwm3", NULL, 0, 0644, NULL, &i2c_proc_real,
573         &i2c_sysctl_real, NULL, &w83627hf_pwm},
574        {W83781D_SYSCTL_SENS1, "sensor1", NULL, 0, 0644, NULL, &i2c_proc_real,
575         &i2c_sysctl_real, NULL, &w83627hf_sens},
576        {W83781D_SYSCTL_SENS2, "sensor2", NULL, 0, 0644, NULL, &i2c_proc_real,
577         &i2c_sysctl_real, NULL, &w83627hf_sens},
578        {W83781D_SYSCTL_SENS3, "sensor3", NULL, 0, 0644, NULL, &i2c_proc_real,
579         &i2c_sysctl_real, NULL, &w83627hf_sens},
580        {0}
581};
582
583
584/* This function is called when:
585     * w83627hf_driver is inserted (when this module is loaded), for each
586       available adapter
587     * when a new adapter is inserted (and w83627hf_driver is still present) */
588static int w83627hf_attach_adapter(struct i2c_adapter *adapter)
589{
590        return i2c_detect(adapter, &addr_data, w83627hf_detect);
591}
592
593static int w83627hf_find(int *address)
594{
595        u16 val;
596
597        superio_enter();
598        val= superio_inb(DEVID);
599        if(val != W627_DEVID && val !=W627THF_DEVID && val != W697_DEVID && val != W637_DEVID) {
600                superio_exit();
601                return -ENODEV;
602        }
603
604        superio_select(W83627HF_LD_HWM);
605        val = (superio_inb(WINB_BASE_REG) << 8) |
606               superio_inb(WINB_BASE_REG + 1);
607        *address = val & ~(WINB_EXTENT - 1);
608        if (*address == 0 && force_addr == 0) {
609                printk("w83627hf.o: base address not set - use force_addr=0xaddr\n");
610                superio_exit();
611                return -ENODEV;
612        }
613        if (force_addr)
614                *address = force_addr;  /* so detect will get called */
615
616        superio_exit();
617        return 0;
618}
619
620int w83627hf_detect(struct i2c_adapter *adapter, int address,
621                   unsigned short flags, int kind)
622{
623        int i, val;
624        struct i2c_client *new_client;
625        struct w83627hf_data *data;
626        int err = 0;
627        const char *type_name = "";
628        const char *client_name = "";
629
630        if (!i2c_is_isa_adapter(adapter))
631                return 0;
632
633        if(force_addr)
634                address = force_addr & ~(WINB_EXTENT - 1);
635        if (check_region(address, WINB_EXTENT)) {
636                printk("w83627hf.o: region 0x%x already in use!\n", address);
637                return -ENODEV;
638        }
639        if(force_addr) {
640                printk("w83627hf.o: forcing ISA address 0x%04X\n", address);
641                superio_enter();
642                superio_select(W83627HF_LD_HWM);
643                superio_outb(WINB_BASE_REG, address >> 8);
644                superio_outb(WINB_BASE_REG+1, address & 0xff);
645                superio_exit();
646        }
647
648        superio_enter();
649        val= superio_inb(DEVID);
650        if(val == W627_DEVID)
651                kind = w83627hf;
652        else if(val == W697_DEVID)
653                kind = w83697hf;
654        else if(val == W627THF_DEVID)
655                kind = w83627thf;
656        else if(val == W637_DEVID)
657                kind = w83637hf;
658               
659        superio_select(W83627HF_LD_HWM);
660        if((val = 0x01 & superio_inb(WINB_ACT_REG)) == 0)
661                superio_outb(WINB_ACT_REG, 1);
662        superio_exit();
663
664        /* OK. For now, we presume we have a valid client. We now create the
665           client structure, even though we cannot fill it completely yet.
666           But it allows us to access w83627hf_{read,write}_value. */
667
668        if (!(data = kmalloc(sizeof(struct w83627hf_data), GFP_KERNEL))) {
669                err = -ENOMEM;
670                goto ERROR0;
671        }
672
673        new_client = &data->client;
674        new_client->addr = address;
675        init_MUTEX(&data->lock);
676        new_client->data = data;
677        new_client->adapter = adapter;
678        new_client->driver = &w83627hf_driver;
679        new_client->flags = 0;
680
681
682        if (kind == w83627hf) {
683                type_name = "w83627hf";
684                client_name = "W83627HF chip";
685        } else if (kind == w83627thf) {
686                type_name = "w83627thf";
687                client_name = "W83627THF chip";
688        } else if (kind == w83697hf) {
689                type_name = "w83697hf";
690                client_name = "W83697HF chip";
691        } else if (kind == w83637hf) {
692                type_name = "w83637hf";
693                client_name = "W83637HF chip";
694        } else {
695                goto ERROR1;
696        }
697
698        request_region(address, WINB_EXTENT, type_name);
699
700        /* Fill in the remaining client fields and put it into the global list */
701        strcpy(new_client->name, client_name);
702        data->type = kind;
703        new_client->id = w83627hf_id++;
704        data->valid = 0;
705        init_MUTEX(&data->update_lock);
706
707        /* Tell the I2C layer a new client has arrived */
708        if ((err = i2c_attach_client(new_client)))
709                goto ERROR3;
710
711        data->lm75 = NULL;
712
713        /* Register a new directory entry with module sensors */
714        if ((i = i2c_register_entry(new_client,
715                                type_name,
716                                (kind == w83697hf) ?
717                                   w83697hf_dir_table_template :
718                                (kind == w83627hf) ?
719                                   w83627hf_dir_table_template :
720                                   /* w83627thf table also used for 637 */
721                                   w83627thf_dir_table_template,
722                                THIS_MODULE)) < 0) {
723                err = i;
724                goto ERROR7;
725        }
726        data->sysctl_id = i;
727
728        /* Initialize the chip */
729        w83627hf_init_client(new_client);
730        return 0;
731
732/* OK, this is not exactly good programming practice, usually. But it is
733   very code-efficient in this case. */
734
735      ERROR7:
736        i2c_detach_client(new_client);
737      ERROR3:
738        release_region(address, WINB_EXTENT);
739      ERROR1:
740        kfree(data);
741      ERROR0:
742        return err;
743}
744
745static int w83627hf_detach_client(struct i2c_client *client)
746{
747        int err;
748
749        i2c_deregister_entry(((struct w83627hf_data *) (client->data))->
750                                 sysctl_id);
751
752        if ((err = i2c_detach_client(client))) {
753                printk
754                    (KERN_ERR "w83627hf.o: Client deregistration failed, client not detached.\n");
755                return err;
756        }
757
758        release_region(client->addr, WINB_EXTENT);
759        kfree(client->data);
760
761        return 0;
762}
763
764
765/*
766   ISA access must always be locked explicitly!
767   We ignore the W83781D BUSY flag at this moment - it could lead to deadlocks,
768   would slow down the W83781D access and should not be necessary.
769   There are some ugly typecasts here, but the good news is - they should
770   nowhere else be necessary! */
771static int w83627hf_read_value(struct i2c_client *client, u16 reg)
772{
773        int res, word_sized;
774
775        down(&(((struct w83627hf_data *) (client->data))->lock));
776        word_sized = (((reg & 0xff00) == 0x100)
777                      || ((reg & 0xff00) == 0x200))
778            && (((reg & 0x00ff) == 0x50)
779                || ((reg & 0x00ff) == 0x53)
780                || ((reg & 0x00ff) == 0x55));
781        if (reg & 0xff00) {
782                outb_p(W83781D_REG_BANK,
783                       client->addr + W83781D_ADDR_REG_OFFSET);
784                outb_p(reg >> 8,
785                       client->addr + W83781D_DATA_REG_OFFSET);
786        }
787        outb_p(reg & 0xff, client->addr + W83781D_ADDR_REG_OFFSET);
788        res = inb_p(client->addr + W83781D_DATA_REG_OFFSET);
789        if (word_sized) {
790                outb_p((reg & 0xff) + 1,
791                       client->addr + W83781D_ADDR_REG_OFFSET);
792                res =
793                    (res << 8) + inb_p(client->addr +
794                                       W83781D_DATA_REG_OFFSET);
795        }
796        if (reg & 0xff00) {
797                outb_p(W83781D_REG_BANK,
798                       client->addr + W83781D_ADDR_REG_OFFSET);
799                outb_p(0, client->addr + W83781D_DATA_REG_OFFSET);
800        }
801        up(&(((struct w83627hf_data *) (client->data))->lock));
802        return res;
803}
804
805static int w83627thf_read_gpio5(struct i2c_client *client)
806{
807        int res = 0xff, sel;
808
809        superio_enter();
810        superio_select(W83627HF_LD_GPIO5);
811
812        /* Make sure these GPIO pins are enabled */
813        if (!(superio_inb(W83627THF_GPIO5_EN) & (1<<3))) {
814#ifdef DEBUG
815                printk(KERN_DEBUG "w83627hf: GPIO5 disabled, no VID "
816                       "function\n");
817#endif
818                goto exit;
819        }
820
821        /* Make sure the pins are configured for input
822           There must be at least five (VRM 9), and possibly 6 (VRM 10) */
823        sel = superio_inb(W83627THF_GPIO5_IOSR);
824        if ((sel & 0x1f) != 0x1f) {
825#ifdef DEBUG
826                printk(KERN_DEBUG "w83627hf: GPIO5 not configured for "
827                       "VID function\n");
828#endif
829                goto exit;
830        }
831
832        printk(KERN_INFO "w83627hf: Reading VID from GPIO5\n");
833        res = superio_inb(W83627THF_GPIO5_DR) & sel;
834
835exit:
836        superio_exit();
837        return res;
838}
839
840static int w83627hf_write_value(struct i2c_client *client, u16 reg, u16 value)
841{
842        int word_sized;
843
844        down(&(((struct w83627hf_data *) (client->data))->lock));
845        word_sized = (((reg & 0xff00) == 0x100)
846                      || ((reg & 0xff00) == 0x200))
847            && (((reg & 0x00ff) == 0x53)
848                || ((reg & 0x00ff) == 0x55));
849        if (reg & 0xff00) {
850                outb_p(W83781D_REG_BANK,
851                       client->addr + W83781D_ADDR_REG_OFFSET);
852                outb_p(reg >> 8,
853                       client->addr + W83781D_DATA_REG_OFFSET);
854        }
855        outb_p(reg & 0xff, client->addr + W83781D_ADDR_REG_OFFSET);
856        if (word_sized) {
857                outb_p(value >> 8,
858                       client->addr + W83781D_DATA_REG_OFFSET);
859                outb_p((reg & 0xff) + 1,
860                       client->addr + W83781D_ADDR_REG_OFFSET);
861        }
862        outb_p(value & 0xff,
863               client->addr + W83781D_DATA_REG_OFFSET);
864        if (reg & 0xff00) {
865                outb_p(W83781D_REG_BANK,
866                       client->addr + W83781D_ADDR_REG_OFFSET);
867                outb_p(0, client->addr + W83781D_DATA_REG_OFFSET);
868        }
869        up(&(((struct w83627hf_data *) (client->data))->lock));
870        return 0;
871}
872
873/* Called when we have found a new W83781D. It should set limits, etc. */
874static void w83627hf_init_client(struct i2c_client *client)
875{
876        struct w83627hf_data *data = client->data;
877        int i;
878        int type = data->type;
879        u8 tmp;
880
881        /* Minimize conflicts with other winbond i2c-only clients...  */
882        /* disable i2c subclients... how to disable main i2c client?? */
883        /* force i2c address to relatively uncommon address */
884        w83627hf_write_value(client, W83781D_REG_I2C_SUBADDR, 0x89);
885        w83627hf_write_value(client, W83781D_REG_I2C_ADDR, force_i2c);
886
887        /* Read VID only once */
888        if (w83627hf == data->type || w83637hf == data->type) {
889                int lo = w83627hf_read_value(client, W83781D_REG_VID_FANDIV);
890                int hi = w83627hf_read_value(client, W83781D_REG_CHIPID);
891                data->vid = (lo & 0x0f) | ((hi & 0x01) << 4);
892        } else if (w83627thf == data->type) {
893                data->vid = w83627thf_read_gpio5(client) & 0x3f;
894        }
895
896        /* Read VRM & OVT Config only once */
897        if (w83627thf == data->type || w83637hf == data->type)
898                data->vrm_ovt =
899                        w83627hf_read_value(client, W83627THF_REG_VRM_OVT_CFG);
900        else
901                data->vrm_ovt = 0;
902
903        /* Choose VRM based on "VRM & OVT" register */
904        data->vrm = (data->vrm_ovt & 0x01) ? 90 : 82;
905
906        tmp = w83627hf_read_value(client, W83781D_REG_SCFG1);
907        for (i = 1; i <= 3; i++) {
908                if (!(tmp & BIT_SCFG1[i - 1])) {
909                        data->sens[i - 1] = W83781D_DEFAULT_BETA;
910                } else {
911                        if (w83627hf_read_value
912                            (client,
913                             W83781D_REG_SCFG2) & BIT_SCFG2[i - 1])
914                                data->sens[i - 1] = 1;
915                        else
916                                data->sens[i - 1] = 2;
917                }
918                if ((type == w83697hf) && (i == 2))
919                        break;
920        }
921
922        data->pwmenable[0] = 1;
923        data->pwmenable[1] = 1;
924        data->pwmenable[2] = 1;
925
926        if(init) {
927                if (type == w83627hf) {
928                        /* enable PWM2 control (can't hurt since PWM reg
929                           should have been reset to 0xff) */
930                        w83627hf_write_value(client, W83627HF_REG_PWMCLK12, 0x19);
931                }
932                /* enable comparator mode for temp2 and temp3 so
933                   alarm indication will work correctly */
934                i = w83627hf_read_value(client, W83781D_REG_IRQ);
935                if (!(i & 0x40))
936                        w83627hf_write_value(client, W83781D_REG_IRQ,
937                                            i | 0x40);
938        }
939
940        /* Start monitoring */
941        w83627hf_write_value(client, W83781D_REG_CONFIG,
942                            (w83627hf_read_value(client,
943                                                W83781D_REG_CONFIG) & 0xf7)
944                            | 0x01);
945}
946
947static void w83627hf_update_client(struct i2c_client *client)
948{
949        struct w83627hf_data *data = client->data;
950        int i;
951
952        down(&data->update_lock);
953
954        if ((jiffies - data->last_updated > HZ + HZ / 2) ||
955            (jiffies < data->last_updated) || !data->valid) {
956                for (i = 0; i <= 8; i++) {
957                        /* skip missing sensors */
958                        if (((data->type == w83697hf) && (i == 1)) ||
959                            ((data->type == w83627thf || data->type == w83637hf) &&
960                             (i == 4 || i == 5)))
961                                continue;
962                        data->in[i] =
963                            w83627hf_read_value(client, W83781D_REG_IN(i));
964                        data->in_min[i] =
965                            w83627hf_read_value(client,
966                                               W83781D_REG_IN_MIN(i));
967                        data->in_max[i] =
968                            w83627hf_read_value(client,
969                                               W83781D_REG_IN_MAX(i));
970                }
971                for (i = 1; i <= 3; i++) {
972                        data->fan[i - 1] =
973                            w83627hf_read_value(client, W83781D_REG_FAN(i));
974                        data->fan_min[i - 1] =
975                            w83627hf_read_value(client,
976                                               W83781D_REG_FAN_MIN(i));
977                }
978                for (i = 1; i <= 3; i++) {
979                        u8 tmp = w83627hf_read_value(client,
980                                W836X7HF_REG_PWM(data->type, i));
981                        if (data->type == w83627thf)
982                                tmp &= 0xf0; /* bits 0-3 are reserved  in 627THF */
983                        data->pwm[i - 1] = tmp;
984                        if(i == 2 && (data->type == w83627hf || data->type == w83697hf))
985                                break;
986                }
987
988                data->temp = w83627hf_read_value(client, W83781D_REG_TEMP);
989                data->temp_over =
990                    w83627hf_read_value(client, W83781D_REG_TEMP_OVER);
991                data->temp_hyst =
992                    w83627hf_read_value(client, W83781D_REG_TEMP_HYST);
993                data->temp_add[0] =
994                    w83627hf_read_value(client, W83781D_REG_TEMP2);
995                data->temp_add_over[0] =
996                    w83627hf_read_value(client, W83781D_REG_TEMP2_OVER);
997                data->temp_add_hyst[0] =
998                    w83627hf_read_value(client, W83781D_REG_TEMP2_HYST);
999                if (data->type != w83697hf) {
1000                        data->temp_add[1] =
1001                            w83627hf_read_value(client, W83781D_REG_TEMP3);
1002                        data->temp_add_over[1] =
1003                            w83627hf_read_value(client, W83781D_REG_TEMP3_OVER);
1004                        data->temp_add_hyst[1] =
1005                            w83627hf_read_value(client, W83781D_REG_TEMP3_HYST);
1006                }
1007
1008                i = w83627hf_read_value(client, W83781D_REG_VID_FANDIV);
1009                data->fan_div[0] = (i >> 4) & 0x03;
1010                data->fan_div[1] = (i >> 6) & 0x03;
1011                if (data->type != w83697hf) {
1012                        data->fan_div[2] = (w83627hf_read_value(client,
1013                                               W83781D_REG_PIN) >> 6) & 0x03;
1014                }
1015                i = w83627hf_read_value(client, W83781D_REG_VBAT);
1016                data->fan_div[0] |= (i >> 3) & 0x04;
1017                data->fan_div[1] |= (i >> 4) & 0x04;
1018                if (data->type != w83697hf)
1019                        data->fan_div[2] |= (i >> 5) & 0x04;
1020                data->alarms =
1021                    w83627hf_read_value(client, W83781D_REG_ALARM1) |
1022                    (w83627hf_read_value(client, W83781D_REG_ALARM2) << 8) |
1023                    (w83627hf_read_value(client, W83781D_REG_ALARM3) << 16);
1024                i = w83627hf_read_value(client, W83781D_REG_BEEP_INTS2);
1025                data->beep_enable = i >> 7;
1026                data->beeps = ((i & 0x7f) << 8) |
1027                    w83627hf_read_value(client, W83781D_REG_BEEP_INTS1) |
1028                    w83627hf_read_value(client, W83781D_REG_BEEP_INTS3) << 16;
1029                data->last_updated = jiffies;
1030                data->valid = 1;
1031        }
1032
1033        up(&data->update_lock);
1034}
1035
1036void w83627hf_in(struct i2c_client *client, int operation, int ctl_name,
1037                int *nrels_mag, long *results)
1038{
1039        struct w83627hf_data *data = client->data;
1040        int nr = ctl_name - W83781D_SYSCTL_IN0;
1041
1042        if (operation == SENSORS_PROC_REAL_INFO)
1043                *nrels_mag = 2;
1044        else if (operation == SENSORS_PROC_REAL_READ) {
1045                w83627hf_update_client(client);
1046
1047                if (nr == 0 && (data->vrm_ovt & 0x01)) {
1048                        /* use VRM9 calculation */
1049                        results[0] = IN_FROM_REG_VRM9(data->in_min[0]);
1050                        results[1] = IN_FROM_REG_VRM9(data->in_max[0]);
1051                        results[2] = IN_FROM_REG_VRM9(data->in[0]);
1052
1053                } else {
1054                        /* use VRM8 (standard) calculation */
1055                        results[0] = IN_FROM_REG(data->in_min[nr]);
1056                        results[1] = IN_FROM_REG(data->in_max[nr]);
1057                        results[2] = IN_FROM_REG(data->in[nr]);
1058                }
1059
1060                *nrels_mag = 3;
1061
1062        } else if (operation == SENSORS_PROC_REAL_WRITE) {
1063                if (*nrels_mag >= 1) {
1064                        if (nr == 0 && (data->vrm_ovt & 0x01))
1065                                /* use VRM9 calculation */
1066                                data->in_min[0] = IN_TO_REG_VRM9(results[0]);
1067                        else
1068                                /* use VRM8 (standard) calculation */
1069                                data->in_min[nr] = IN_TO_REG(results[0]);
1070
1071                        w83627hf_write_value(client, W83781D_REG_IN_MIN(nr),
1072                                            data->in_min[nr]);
1073                }
1074                if (*nrels_mag >= 2) {
1075                        if (nr == 0 && (data->vrm_ovt & 0x01))
1076                                /* use VRM9 calculation */
1077                                data->in_min[0] = IN_TO_REG_VRM9(results[1]);
1078                        else
1079                                /* use VRM8 (standard) calculation */
1080                                data->in_max[nr] = IN_TO_REG(results[1]);
1081
1082                        w83627hf_write_value(client, W83781D_REG_IN_MAX(nr),
1083                                            data->in_max[nr]);
1084                }
1085        }
1086}
1087
1088void w83627hf_fan(struct i2c_client *client, int operation, int ctl_name,
1089                 int *nrels_mag, long *results)
1090{
1091        struct w83627hf_data *data = client->data;
1092        int nr = ctl_name - W83781D_SYSCTL_FAN1 + 1;
1093
1094        if (operation == SENSORS_PROC_REAL_INFO)
1095                *nrels_mag = 0;
1096        else if (operation == SENSORS_PROC_REAL_READ) {
1097                w83627hf_update_client(client);
1098                results[0] = FAN_FROM_REG(data->fan_min[nr - 1],
1099                                  DIV_FROM_REG(data->fan_div[nr - 1]));
1100                results[1] = FAN_FROM_REG(data->fan[nr - 1],
1101                                  DIV_FROM_REG(data->fan_div[nr - 1]));
1102                *nrels_mag = 2;
1103        } else if (operation == SENSORS_PROC_REAL_WRITE) {
1104                if (*nrels_mag >= 1) {
1105                        data->fan_min[nr - 1] =
1106                             FAN_TO_REG(results[0],
1107                                    DIV_FROM_REG(data->fan_div[nr-1]));
1108                        w83627hf_write_value(client,
1109                                            W83781D_REG_FAN_MIN(nr),
1110                                            data->fan_min[nr - 1]);
1111                }
1112        }
1113}
1114
1115void w83627hf_temp(struct i2c_client *client, int operation, int ctl_name,
1116                  int *nrels_mag, long *results)
1117{
1118        struct w83627hf_data *data = client->data;
1119        if (operation == SENSORS_PROC_REAL_INFO)
1120                *nrels_mag = 1;
1121        else if (operation == SENSORS_PROC_REAL_READ) {
1122                w83627hf_update_client(client);
1123                results[0] = TEMP_FROM_REG(data->temp_over);
1124                results[1] = TEMP_FROM_REG(data->temp_hyst);
1125                results[2] = TEMP_FROM_REG(data->temp);
1126                *nrels_mag = 3;
1127        } else if (operation == SENSORS_PROC_REAL_WRITE) {
1128                if (*nrels_mag >= 1) {
1129                        data->temp_over = TEMP_TO_REG(results[0]);
1130                        w83627hf_write_value(client, W83781D_REG_TEMP_OVER,
1131                                            data->temp_over);
1132                }
1133                if (*nrels_mag >= 2) {
1134                        data->temp_hyst = TEMP_TO_REG(results[1]);
1135                        w83627hf_write_value(client, W83781D_REG_TEMP_HYST,
1136                                            data->temp_hyst);
1137                }
1138        }
1139}
1140
1141void w83627hf_temp_add(struct i2c_client *client, int operation,
1142                      int ctl_name, int *nrels_mag, long *results)
1143{
1144        struct w83627hf_data *data = client->data;
1145        int nr = ctl_name - W83781D_SYSCTL_TEMP2;
1146
1147        if (operation == SENSORS_PROC_REAL_INFO)
1148                *nrels_mag = 1;
1149        else if (operation == SENSORS_PROC_REAL_READ) {
1150                w83627hf_update_client(client);
1151                        results[0] =
1152                            LM75_TEMP_FROM_REG(data->temp_add_over[nr]);
1153                        results[1] =
1154                            LM75_TEMP_FROM_REG(data->temp_add_hyst[nr]);
1155                        results[2] = LM75_TEMP_FROM_REG(data->temp_add[nr]);
1156                *nrels_mag = 3;
1157        } else if (operation == SENSORS_PROC_REAL_WRITE) {
1158                if (*nrels_mag >= 1) {
1159                                data->temp_add_over[nr] =
1160                                    LM75_TEMP_TO_REG(results[0]);
1161                        w83627hf_write_value(client,
1162                                            nr ? W83781D_REG_TEMP3_OVER :
1163                                            W83781D_REG_TEMP2_OVER,
1164                                            data->temp_add_over[nr]);
1165                }
1166                if (*nrels_mag >= 2) {
1167                                data->temp_add_hyst[nr] =
1168                                    LM75_TEMP_TO_REG(results[1]);
1169                        w83627hf_write_value(client,
1170                                            nr ? W83781D_REG_TEMP3_HYST :
1171                                            W83781D_REG_TEMP2_HYST,
1172                                            data->temp_add_hyst[nr]);
1173                }
1174        }
1175}
1176
1177void w83627hf_vid(struct i2c_client *client, int operation, int ctl_name,
1178                 int *nrels_mag, long *results)
1179{
1180        struct w83627hf_data *data = client->data;
1181        if (operation == SENSORS_PROC_REAL_INFO)
1182                *nrels_mag = 3;
1183        else if (operation == SENSORS_PROC_REAL_READ) {
1184                w83627hf_update_client(client);
1185                results[0] = vid_from_reg(data->vid, data->vrm);
1186                *nrels_mag = 1;
1187        }
1188}
1189
1190void w83627hf_vrm(struct i2c_client *client, int operation, int ctl_name,
1191                 int *nrels_mag, long *results)
1192{
1193        struct w83627hf_data *data = client->data;
1194        if (operation == SENSORS_PROC_REAL_INFO)
1195                *nrels_mag = 1;
1196        else if (operation == SENSORS_PROC_REAL_READ) {
1197                results[0] = data->vrm;
1198                *nrels_mag = 1;
1199        } else if (operation == SENSORS_PROC_REAL_WRITE) {
1200                if (*nrels_mag >= 1)
1201                        data->vrm = results[0];
1202        }
1203}
1204
1205void w83627hf_alarms(struct i2c_client *client, int operation, int ctl_name,
1206                    int *nrels_mag, long *results)
1207{
1208        struct w83627hf_data *data = client->data;
1209        if (operation == SENSORS_PROC_REAL_INFO)
1210                *nrels_mag = 0;
1211        else if (operation == SENSORS_PROC_REAL_READ) {
1212                w83627hf_update_client(client);
1213                results[0] = data->alarms;
1214                *nrels_mag = 1;
1215        }
1216}
1217
1218void w83627hf_beep(struct i2c_client *client, int operation, int ctl_name,
1219                  int *nrels_mag, long *results)
1220{
1221        struct w83627hf_data *data = client->data;
1222        int val;
1223
1224        if (operation == SENSORS_PROC_REAL_INFO)
1225                *nrels_mag = 0;
1226        else if (operation == SENSORS_PROC_REAL_READ) {
1227                w83627hf_update_client(client);
1228                results[0] = BEEP_ENABLE_FROM_REG(data->beep_enable);
1229                results[1] = data->beeps;
1230                *nrels_mag = 2;
1231        } else if (operation == SENSORS_PROC_REAL_WRITE) {
1232                if (*nrels_mag >= 2) {
1233                        data->beeps = BEEPS_TO_REG(results[1]);
1234                        w83627hf_write_value(client, W83781D_REG_BEEP_INTS1,
1235                                            data->beeps & 0xff);
1236                                w83627hf_write_value(client,
1237                                                    W83781D_REG_BEEP_INTS3,
1238                                                    ((data-> beeps) >> 16) &
1239                                                      0xff);
1240                        val = (data->beeps >> 8) & 0x7f;
1241                } else if (*nrels_mag >= 1)
1242                        val =
1243                            w83627hf_read_value(client,
1244                                               W83781D_REG_BEEP_INTS2) &
1245                            0x7f;
1246                if (*nrels_mag >= 1) {
1247                        data->beep_enable = BEEP_ENABLE_TO_REG(results[0]);
1248                        w83627hf_write_value(client, W83781D_REG_BEEP_INTS2,
1249                                            val | data->beep_enable << 7);
1250                }
1251        }
1252}
1253
1254/* w83697hf only has two fans */
1255void w83627hf_fan_div(struct i2c_client *client, int operation,
1256                     int ctl_name, int *nrels_mag, long *results)
1257{
1258        struct w83627hf_data *data = client->data;
1259        int old, old2, old3 = 0;
1260
1261        if (operation == SENSORS_PROC_REAL_INFO)
1262                *nrels_mag = 0;
1263        else if (operation == SENSORS_PROC_REAL_READ) {
1264                w83627hf_update_client(client);
1265                results[0] = DIV_FROM_REG(data->fan_div[0]);
1266                results[1] = DIV_FROM_REG(data->fan_div[1]);
1267                if (data->type == w83697hf) {
1268                        *nrels_mag = 2;
1269                } else {
1270                        results[2] = DIV_FROM_REG(data->fan_div[2]);
1271                        *nrels_mag = 3;
1272                }
1273        } else if (operation == SENSORS_PROC_REAL_WRITE) {
1274                old = w83627hf_read_value(client, W83781D_REG_VID_FANDIV);
1275                /* w83627hf doesn't have extended divisor bits */
1276                        old3 =
1277                            w83627hf_read_value(client, W83781D_REG_VBAT);
1278                if (*nrels_mag >= 3 && data->type != w83697hf) {
1279                        data->fan_div[2] =
1280                            DIV_TO_REG(results[2]);
1281                        old2 = w83627hf_read_value(client, W83781D_REG_PIN);
1282                        old2 =
1283                            (old2 & 0x3f) | ((data->fan_div[2] & 0x03) << 6);
1284                        w83627hf_write_value(client, W83781D_REG_PIN, old2);
1285                                old3 =
1286                                    (old3 & 0x7f) |
1287                                    ((data->fan_div[2] & 0x04) << 5);
1288                }
1289                if (*nrels_mag >= 2) {
1290                        data->fan_div[1] =
1291                            DIV_TO_REG(results[1]);
1292                        old =
1293                            (old & 0x3f) | ((data->fan_div[1] & 0x03) << 6);
1294                                old3 =
1295                                    (old3 & 0xbf) |
1296                                    ((data->fan_div[1] & 0x04) << 4);
1297                }
1298                if (*nrels_mag >= 1) {
1299                        data->fan_div[0] =
1300                            DIV_TO_REG(results[0]);
1301                        old =
1302                            (old & 0xcf) | ((data->fan_div[0] & 0x03) << 4);
1303                        w83627hf_write_value(client, W83781D_REG_VID_FANDIV,
1304                                            old);
1305                                old3 =
1306                                    (old3 & 0xdf) |
1307                                    ((data->fan_div[0] & 0x04) << 3);
1308                                w83627hf_write_value(client,
1309                                                    W83781D_REG_VBAT,
1310                                                    old3);
1311                }
1312        }
1313}
1314
1315/* we do not currently support disabling PWM with 2nd argument;
1316  set first argument to 255 to disable */
1317void w83627hf_pwm(struct i2c_client *client, int operation, int ctl_name,
1318                 int *nrels_mag, long *results)
1319{
1320        struct w83627hf_data *data = client->data;
1321        int nr = 1 + ctl_name - W83781D_SYSCTL_PWM1;
1322
1323        if (operation == SENSORS_PROC_REAL_INFO)
1324                *nrels_mag = 0;
1325        else if (operation == SENSORS_PROC_REAL_READ) {
1326                w83627hf_update_client(client);
1327                results[0] = data->pwm[nr - 1];
1328                results[1] = data->pwmenable[nr - 1];
1329                *nrels_mag = 2;
1330        } else if (operation == SENSORS_PROC_REAL_WRITE) {
1331                if (*nrels_mag >= 1) {
1332                        if (data->type == w83627thf) {
1333                                /* bits 0-3 are reserved  in 627THF */
1334                                data->pwm[nr - 1] = PWM_TO_REG(results[0]) & 0xf0;
1335                                w83627hf_write_value(client,
1336                                                     W836X7HF_REG_PWM(data->type, nr),
1337                                                     data->pwm[nr - 1] |
1338                                                     (w83627hf_read_value(client,
1339                                                      W836X7HF_REG_PWM(data->type, nr)) & 0x0f));
1340                        } else {
1341                                data->pwm[nr - 1] = PWM_TO_REG(results[0]);
1342                                w83627hf_write_value(client,
1343                                                     W836X7HF_REG_PWM(data->type, nr),
1344                                                     data->pwm[nr - 1]);
1345                        }
1346                }
1347        }
1348}
1349
1350void w83627hf_sens(struct i2c_client *client, int operation, int ctl_name,
1351                  int *nrels_mag, long *results)
1352{
1353        struct w83627hf_data *data = client->data;
1354        int nr = 1 + ctl_name - W83781D_SYSCTL_SENS1;
1355        u8 tmp;
1356
1357        if (operation == SENSORS_PROC_REAL_INFO)
1358                *nrels_mag = 0;
1359        else if (operation == SENSORS_PROC_REAL_READ) {
1360                results[0] = data->sens[nr - 1];
1361                *nrels_mag = 1;
1362        } else if (operation == SENSORS_PROC_REAL_WRITE) {
1363                if (*nrels_mag >= 1) {
1364                        switch (results[0]) {
1365                        case 1: /* PII/Celeron diode */
1366                                tmp = w83627hf_read_value(client,
1367                                                       W83781D_REG_SCFG1);
1368                                w83627hf_write_value(client,
1369                                                    W83781D_REG_SCFG1,
1370                                                    tmp | BIT_SCFG1[nr -
1371                                                                    1]);
1372                                tmp = w83627hf_read_value(client,
1373                                                       W83781D_REG_SCFG2);
1374                                w83627hf_write_value(client,
1375                                                    W83781D_REG_SCFG2,
1376                                                    tmp | BIT_SCFG2[nr -
1377                                                                    1]);
1378                                data->sens[nr - 1] = results[0];
1379                                break;
1380                        case 2: /* 3904 */
1381                                tmp = w83627hf_read_value(client,
1382                                                       W83781D_REG_SCFG1);
1383                                w83627hf_write_value(client,
1384                                                    W83781D_REG_SCFG1,
1385                                                    tmp | BIT_SCFG1[nr -
1386                                                                    1]);
1387                                tmp = w83627hf_read_value(client,
1388                                                       W83781D_REG_SCFG2);
1389                                w83627hf_write_value(client,
1390                                                    W83781D_REG_SCFG2,
1391                                                    tmp & ~BIT_SCFG2[nr -
1392                                                                     1]);
1393                                data->sens[nr - 1] = results[0];
1394                                break;
1395                        case W83781D_DEFAULT_BETA:      /* thermistor */
1396                                tmp = w83627hf_read_value(client,
1397                                                       W83781D_REG_SCFG1);
1398                                w83627hf_write_value(client,
1399                                                    W83781D_REG_SCFG1,
1400                                                    tmp & ~BIT_SCFG1[nr -
1401                                                                     1]);
1402                                data->sens[nr - 1] = results[0];
1403                                break;
1404                        default:
1405                                printk
1406                                    (KERN_ERR "w83627hf.o: Invalid sensor type %ld; must be 1, 2, or %d\n",
1407                                     results[0], W83781D_DEFAULT_BETA);
1408                                break;
1409                        }
1410                }
1411        }
1412}
1413
1414static int __init sm_w83627hf_init(void)
1415{
1416        int addr;
1417
1418        printk(KERN_INFO "w83627hf.o version %s (%s)\n", LM_VERSION, LM_DATE);
1419        if (w83627hf_find(&addr)) {
1420                printk("w83627hf.o: W83627/697 not detected, module not inserted.\n");
1421                return -ENODEV;
1422        }
1423        normal_isa[0] = addr;
1424
1425        return i2c_add_driver(&w83627hf_driver);
1426}
1427
1428static void __exit sm_w83627hf_exit(void)
1429{
1430        i2c_del_driver(&w83627hf_driver);
1431}
1432
1433
1434
1435MODULE_AUTHOR("Frodo Looijaard <frodol@dds.nl>, "
1436              "Philip Edelbrock <phil@netroedge.com>, "
1437              "and Mark Studebaker <mdsxyz123@yahoo.com>");
1438MODULE_DESCRIPTION("W83627HF driver");
1439MODULE_LICENSE("GPL");
1440
1441module_init(sm_w83627hf_init);
1442module_exit(sm_w83627hf_exit);
Note: See TracBrowser for help on using the browser.