ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[linux-2.6.git] / drivers / i2c / chips / lm85.c
1 /*
2     lm85.c - Part of lm_sensors, Linux kernel modules for hardware
3              monitoring
4     Copyright (c) 1998, 1999  Frodo Looijaard <frodol@dds.nl> 
5     Copyright (c) 2002, 2003  Philip Pokorny <ppokorny@penguincomputing.com>
6     Copyright (c) 2003        Margit Schubert-While <margitsw@t-online.de>
7
8     Chip details at           <http://www.national.com/ds/LM/LM85.pdf>
9
10     This program is free software; you can redistribute it and/or modify
11     it under the terms of the GNU General Public License as published by
12     the Free Software Foundation; either version 2 of the License, or
13     (at your option) any later version.
14
15     This program is distributed in the hope that it will be useful,
16     but WITHOUT ANY WARRANTY; without even the implied warranty of
17     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18     GNU General Public License for more details.
19
20     You should have received a copy of the GNU General Public License
21     along with this program; if not, write to the Free Software
22     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23 */
24
25 #include <linux/config.h>
26 #include <linux/module.h>
27 #include <linux/init.h>
28 #include <linux/slab.h>
29 #include <linux/i2c.h>
30 #include <linux/i2c-sensor.h>
31 #include <linux/i2c-vid.h>
32 /*
33 #include <asm/io.h>
34 */
35
36 #undef  LM85EXTENDEDFUNC        /* Extended functionality */
37
38 /* Addresses to scan */
39 static unsigned short normal_i2c[] = { 0x2c, 0x2d, 0x2e, I2C_CLIENT_END };
40 static unsigned short normal_i2c_range[] = { I2C_CLIENT_END };
41 static unsigned int normal_isa[] = { I2C_CLIENT_ISA_END };
42 static unsigned int normal_isa_range[] = { I2C_CLIENT_ISA_END };
43
44 /* Insmod parameters */
45 SENSORS_INSMOD_4(lm85b, lm85c, adm1027, adt7463);
46
47 /* The LM85 registers */
48
49 #define LM85_REG_IN(nr)                 (0x20 + (nr))
50 #define LM85_REG_IN_MIN(nr)             (0x44 + (nr) * 2)
51 #define LM85_REG_IN_MAX(nr)             (0x45 + (nr) * 2)
52
53 #define LM85_REG_TEMP(nr)               (0x25 + (nr))
54 #define LM85_REG_TEMP_MIN(nr)           (0x4e + (nr) * 2)
55 #define LM85_REG_TEMP_MAX(nr)           (0x4f + (nr) * 2)
56
57 /* Fan speeds are LSB, MSB (2 bytes) */
58 #define LM85_REG_FAN(nr)                (0x28 + (nr) *2)
59 #define LM85_REG_FAN_MIN(nr)            (0x54 + (nr) *2)
60
61 #define LM85_REG_PWM(nr)                (0x30 + (nr))
62
63 #define ADT7463_REG_OPPOINT(nr)         (0x33 + (nr))
64
65 #define ADT7463_REG_TMIN_CTL1           0x36
66 #define ADT7463_REG_TMIN_CTL2           0x37
67
68 #define LM85_REG_DEVICE                 0x3d
69 #define LM85_REG_COMPANY                0x3e
70 #define LM85_REG_VERSTEP                0x3f
71 /* These are the recognized values for the above regs */
72 #define LM85_DEVICE_ADX                 0x27
73 #define LM85_COMPANY_NATIONAL           0x01
74 #define LM85_COMPANY_ANALOG_DEV         0x41
75 #define LM85_VERSTEP_GENERIC            0x60
76 #define LM85_VERSTEP_LM85C              0x60
77 #define LM85_VERSTEP_LM85B              0x62
78 #define LM85_VERSTEP_ADM1027            0x60
79 #define LM85_VERSTEP_ADT7463            0x62
80
81 #define LM85_REG_CONFIG                 0x40
82
83 #define LM85_REG_ALARM1                 0x41
84 #define LM85_REG_ALARM2                 0x42
85
86 #define LM85_REG_VID                    0x43
87
88 /* Automated FAN control */
89 #define LM85_REG_AFAN_CONFIG(nr)        (0x5c + (nr))
90 #define LM85_REG_AFAN_RANGE(nr)         (0x5f + (nr))
91 #define LM85_REG_AFAN_SPIKE1            0x62
92 #define LM85_REG_AFAN_SPIKE2            0x63
93 #define LM85_REG_AFAN_MINPWM(nr)        (0x64 + (nr))
94 #define LM85_REG_AFAN_LIMIT(nr)         (0x67 + (nr))
95 #define LM85_REG_AFAN_CRITICAL(nr)      (0x6a + (nr))
96 #define LM85_REG_AFAN_HYST1             0x6d
97 #define LM85_REG_AFAN_HYST2             0x6e
98
99 #define LM85_REG_TACH_MODE              0x74
100 #define LM85_REG_SPINUP_CTL             0x75
101
102 #define ADM1027_REG_TEMP_OFFSET(nr)     (0x70 + (nr))
103 #define ADM1027_REG_CONFIG2             0x73
104 #define ADM1027_REG_INTMASK1            0x74
105 #define ADM1027_REG_INTMASK2            0x75
106 #define ADM1027_REG_EXTEND_ADC1         0x76
107 #define ADM1027_REG_EXTEND_ADC2         0x77
108 #define ADM1027_REG_CONFIG3             0x78
109 #define ADM1027_REG_FAN_PPR             0x7b
110
111 #define ADT7463_REG_THERM               0x79
112 #define ADT7463_REG_THERM_LIMIT         0x7A
113
114 #define LM85_ALARM_IN0                  0x0001
115 #define LM85_ALARM_IN1                  0x0002
116 #define LM85_ALARM_IN2                  0x0004
117 #define LM85_ALARM_IN3                  0x0008
118 #define LM85_ALARM_TEMP1                0x0010
119 #define LM85_ALARM_TEMP2                0x0020
120 #define LM85_ALARM_TEMP3                0x0040
121 #define LM85_ALARM_ALARM2               0x0080
122 #define LM85_ALARM_IN4                  0x0100
123 #define LM85_ALARM_RESERVED             0x0200
124 #define LM85_ALARM_FAN1                 0x0400
125 #define LM85_ALARM_FAN2                 0x0800
126 #define LM85_ALARM_FAN3                 0x1000
127 #define LM85_ALARM_FAN4                 0x2000
128 #define LM85_ALARM_TEMP1_FAULT          0x4000
129 #define LM85_ALARM_TEMP3_FAULT          0x8000
130
131
132 /* Conversions. Rounding and limit checking is only done on the TO_REG 
133    variants. Note that you should be a bit careful with which arguments
134    these macros are called: arguments may be evaluated more than once.
135  */
136
137 /* IN are scaled 1.000 == 0xc0, mag = 3 */
138 #define IN_TO_REG(val)          (SENSORS_LIMIT((((val)*0xc0+500)/1000),0,255))
139 #define INEXT_FROM_REG(val,ext) (((val)*1000 + (ext)*250 + 96)/0xc0)
140 #define IN_FROM_REG(val)        (INEXT_FROM_REG(val,0))
141
142 /* IN are scaled acording to built-in resistors */
143 static int lm85_scaling[] = {  /* .001 Volts */
144                 2500, 2250, 3300, 5000, 12000
145         };
146 #define SCALE(val,from,to)              (((val)*(to) + ((from)/2))/(from))
147 #define INS_TO_REG(n,val)               (SENSORS_LIMIT(SCALE(val,lm85_scaling[n],192),0,255))
148 #define INSEXT_FROM_REG(n,val,ext)      (SCALE((val)*4 + (ext),192*4,lm85_scaling[n]))
149 #define INS_FROM_REG(n,val)             (INSEXT_FROM_REG(n,val,0))
150
151 /* FAN speed is measured using 90kHz clock */
152 #define FAN_TO_REG(val)         (SENSORS_LIMIT( (val)<=0?0: 5400000/(val),0,65534))
153 #define FAN_FROM_REG(val)       ((val)==0?-1:(val)==0xffff?0:5400000/(val))
154
155 /* Temperature is reported in .001 degC increments */
156 #define TEMP_TO_REG(val)                (SENSORS_LIMIT(((val)+500)/1000,-127,127))
157 #define TEMPEXT_FROM_REG(val,ext)       ((val)*1000 + (ext)*250)
158 #define TEMP_FROM_REG(val)              (TEMPEXT_FROM_REG(val,0))
159 #define EXTTEMP_TO_REG(val)             (SENSORS_LIMIT((val)/250,-127,127))
160
161 #define PWM_TO_REG(val)                 (SENSORS_LIMIT(val,0,255))
162 #define PWM_FROM_REG(val)               (val)
163
164 #define EXT_FROM_REG(val,sensor)        (((val)>>(sensor * 2))&0x03)
165
166 #ifdef  LM85EXTENDEDFUNC        /* Extended functionality */
167
168 /* ZONEs have the following parameters:
169  *    Limit (low) temp,           1. degC
170  *    Hysteresis (below limit),   1. degC (0-15)
171  *    Range of speed control,     .1 degC (2-80)
172  *    Critical (high) temp,       1. degC
173  *
174  * FAN PWMs have the following parameters:
175  *    Reference Zone,                 1, 2, 3, etc.
176  *    Spinup time,                    .05 sec
177  *    PWM value at limit/low temp,    1 count
178  *    PWM Frequency,                  1. Hz
179  *    PWM is Min or OFF below limit,  flag
180  *    Invert PWM output,              flag
181  *
182  * Some chips filter the temp, others the fan.
183  *    Filter constant (or disabled)   .1 seconds
184  */
185
186 /* These are the zone temperature range encodings */
187 static int lm85_range_map[] = {   /* .1 degC */
188                  20,  25,  33,  40,  50,  66,
189                  80, 100, 133, 160, 200, 266,
190                 320, 400, 533, 800
191         };
192 static int RANGE_TO_REG( int range )
193 {
194         int i;
195
196         if( range >= lm85_range_map[15] ) { return 15 ; }
197         for( i = 0 ; i < 15 ; ++i )
198                 if( range <= lm85_range_map[i] )
199                         break ;
200         return( i & 0x0f );
201 }
202 #define RANGE_FROM_REG(val) (lm85_range_map[(val)&0x0f])
203
204 /* These are the Acoustic Enhancement, or Temperature smoothing encodings
205  * NOTE: The enable/disable bit is INCLUDED in these encodings as the
206  *       MSB (bit 3, value 8).  If the enable bit is 0, the encoded value
207  *       is ignored, or set to 0.
208  */
209 static int lm85_smooth_map[] = {  /* .1 sec */
210                 350, 176, 118,  70,  44,   30,   16,    8
211 /*    35.4 *    1/1, 1/2, 1/3, 1/5, 1/8, 1/12, 1/24, 1/48  */
212         };
213 static int SMOOTH_TO_REG( int smooth )
214 {
215         int i;
216
217         if( smooth <= 0 ) { return 0 ; }  /* Disabled */
218         for( i = 0 ; i < 7 ; ++i )
219                 if( smooth >= lm85_smooth_map[i] )
220                         break ;
221         return( (i & 0x07) | 0x08 );
222 }
223 #define SMOOTH_FROM_REG(val) ((val)&0x08?lm85_smooth_map[(val)&0x07]:0)
224
225 /* These are the fan spinup delay time encodings */
226 static int lm85_spinup_map[] = {  /* .1 sec */
227                 0, 1, 2, 4, 7, 10, 20, 40
228         };
229 static int SPINUP_TO_REG( int spinup )
230 {
231         int i;
232
233         if( spinup >= lm85_spinup_map[7] ) { return 7 ; }
234         for( i = 0 ; i < 7 ; ++i )
235                 if( spinup <= lm85_spinup_map[i] )
236                         break ;
237         return( i & 0x07 );
238 }
239 #define SPINUP_FROM_REG(val) (lm85_spinup_map[(val)&0x07])
240
241 /* These are the PWM frequency encodings */
242 static int lm85_freq_map[] = { /* .1 Hz */
243                 100, 150, 230, 300, 380, 470, 620, 980
244         };
245 static int FREQ_TO_REG( int freq )
246 {
247         int i;
248
249         if( freq >= lm85_freq_map[7] ) { return 7 ; }
250         for( i = 0 ; i < 7 ; ++i )
251                 if( freq <= lm85_freq_map[i] )
252                         break ;
253         return( i & 0x07 );
254 }
255 #define FREQ_FROM_REG(val) (lm85_freq_map[(val)&0x07])
256
257 /* Since we can't use strings, I'm abusing these numbers
258  *   to stand in for the following meanings:
259  *      1 -- PWM responds to Zone 1
260  *      2 -- PWM responds to Zone 2
261  *      3 -- PWM responds to Zone 3
262  *     23 -- PWM responds to the higher temp of Zone 2 or 3
263  *    123 -- PWM responds to highest of Zone 1, 2, or 3
264  *      0 -- PWM is always at 0% (ie, off)
265  *     -1 -- PWM is always at 100%
266  *     -2 -- PWM responds to manual control
267  */
268
269 #endif          /* Extended functionality */
270
271 static int lm85_zone_map[] = { 1, 2, 3, -1, 0, 23, 123, -2 };
272 #define ZONE_FROM_REG(val) (lm85_zone_map[((val)>>5)&0x07])
273
274 #ifdef  LM85EXTENDEDFUNC        /* Extended functionality */
275
276 static int ZONE_TO_REG( int zone )
277 {
278         int i;
279
280         for( i = 0 ; i <= 7 ; ++i )
281                 if( zone == lm85_zone_map[i] )
282                         break ;
283         if( i > 7 )   /* Not found. */
284                 i = 3;  /* Always 100% */
285         return( (i & 0x07)<<5 );
286 }
287
288 #endif          /* Extended functionality */
289
290 #define HYST_TO_REG(val) (SENSORS_LIMIT((-(val)+5)/10,0,15))
291 #define HYST_FROM_REG(val) (-(val)*10)
292
293 #define OFFSET_TO_REG(val) (SENSORS_LIMIT((val)/25,-127,127))
294 #define OFFSET_FROM_REG(val) ((val)*25)
295
296 #define PPR_MASK(fan) (0x03<<(fan *2))
297 #define PPR_TO_REG(val,fan) (SENSORS_LIMIT((val)-1,0,3)<<(fan *2))
298 #define PPR_FROM_REG(val,fan) ((((val)>>(fan * 2))&0x03)+1)
299
300 /* i2c-vid.h defines vid_from_reg() */
301 #define VID_FROM_REG(val,vrm) (vid_from_reg((val),(vrm)))
302
303 #define ALARMS_FROM_REG(val) (val)
304
305 /* Unlike some other drivers we DO NOT set initial limits.  Use
306  * the config file to set limits.  Some users have reported
307  * motherboards shutting down when we set limits in a previous
308  * version of the driver.
309  */
310
311 /* Typically used with Pentium 4 systems v9.1 VRM spec */
312 #define LM85_INIT_VRM  91
313
314 /* Chip sampling rates
315  *
316  * Some sensors are not updated more frequently than once per second
317  *    so it doesn't make sense to read them more often than that.
318  *    We cache the results and return the saved data if the driver
319  *    is called again before a second has elapsed.
320  *
321  * Also, there is significant configuration data for this chip
322  *    given the automatic PWM fan control that is possible.  There
323  *    are about 47 bytes of config data to only 22 bytes of actual
324  *    readings.  So, we keep the config data up to date in the cache
325  *    when it is written and only sample it once every 1 *minute*
326  */
327 #define LM85_DATA_INTERVAL  (HZ + HZ / 2)
328 #define LM85_CONFIG_INTERVAL  (1 * 60 * HZ)
329
330 /* For each registered LM85, we need to keep some data in memory. That
331    data is pointed to by lm85_list[NR]->data. The structure itself is
332    dynamically allocated, at the same time when a new lm85 client is
333    allocated. */
334
335 /* LM85 can automatically adjust fan speeds based on temperature
336  * This structure encapsulates an entire Zone config.  There are
337  * three zones (one for each temperature input) on the lm85
338  */
339 struct lm85_zone {
340         s8 limit;       /* Low temp limit */
341         u8 hyst;        /* Low limit hysteresis. (0-15) */
342         u8 range;       /* Temp range, encoded */
343         s8 critical;    /* "All fans ON" temp limit */
344 };
345
346 struct lm85_autofan {
347         u8 config;      /* Register value */
348         u8 freq;        /* PWM frequency, encoded */
349         u8 min_pwm;     /* Minimum PWM value, encoded */
350         u8 min_off;     /* Min PWM or OFF below "limit", flag */
351 };
352
353 struct lm85_data {
354         struct i2c_client client;
355         struct semaphore lock;
356         enum chips type;
357
358         struct semaphore update_lock;
359         int valid;              /* !=0 if following fields are valid */
360         unsigned long last_reading;     /* In jiffies */
361         unsigned long last_config;      /* In jiffies */
362
363         u8 in[5];               /* Register value */
364         u8 in_max[5];           /* Register value */
365         u8 in_min[5];           /* Register value */
366         s8 temp[3];             /* Register value */
367         s8 temp_min[3];         /* Register value */
368         s8 temp_max[3];         /* Register value */
369         s8 temp_offset[3];      /* Register value */
370         u16 fan[4];             /* Register value */
371         u16 fan_min[4];         /* Register value */
372         u8 pwm[3];              /* Register value */
373         u8 spinup_ctl;          /* Register encoding, combined */
374         u8 tach_mode;           /* Register encoding, combined */
375         u16 extend_adc;         /* Register value */
376         u8 fan_ppr;             /* Register value */
377         u8 smooth[3];           /* Register encoding */
378         u8 vid;                 /* Register value */
379         u8 vrm;                 /* VRM version */
380         u8 syncpwm3;            /* Saved PWM3 for TACH 2,3,4 config */
381         u8 oppoint[3];          /* Register value */
382         u16 tmin_ctl;           /* Register value */
383         unsigned long therm_total; /* Cummulative therm count */
384         u8 therm_limit;         /* Register value */
385         u16 alarms;             /* Register encoding, combined */
386         struct lm85_autofan autofan[3];
387         struct lm85_zone zone[3];
388 };
389
390 static int lm85_attach_adapter(struct i2c_adapter *adapter);
391 static int lm85_detect(struct i2c_adapter *adapter, int address,
392                         int kind);
393 static int lm85_detach_client(struct i2c_client *client);
394
395 static int lm85_read_value(struct i2c_client *client, u8 register);
396 static int lm85_write_value(struct i2c_client *client, u8 register, int value);
397 static struct lm85_data *lm85_update_device(struct device *dev);
398 static void lm85_init_client(struct i2c_client *client);
399
400
401 static struct i2c_driver lm85_driver = {
402         .owner          = THIS_MODULE,
403         .name           = "lm85",
404         .id             = I2C_DRIVERID_LM85,
405         .flags          = I2C_DF_NOTIFY,
406         .attach_adapter = lm85_attach_adapter,
407         .detach_client  = lm85_detach_client,
408 };
409
410 /* Unique ID assigned to each LM85 detected */
411 static int lm85_id = 0;
412
413
414 /* 4 Fans */
415 static ssize_t show_fan(struct device *dev, char *buf, int nr)
416 {
417         struct lm85_data *data = lm85_update_device(dev);
418         return sprintf(buf,"%d\n", FAN_FROM_REG(data->fan[nr]) );
419 }
420 static ssize_t show_fan_min(struct device *dev, char *buf, int nr)
421 {
422         struct lm85_data *data = lm85_update_device(dev);
423         return sprintf(buf,"%d\n", FAN_FROM_REG(data->fan_min[nr]) );
424 }
425 static ssize_t set_fan_min(struct device *dev, const char *buf, 
426                 size_t count, int nr)
427 {
428         struct i2c_client *client = to_i2c_client(dev);
429         struct lm85_data *data = i2c_get_clientdata(client);
430         int     val;
431
432         down(&data->update_lock);
433         val = simple_strtol(buf, NULL, 10);
434         data->fan_min[nr] = FAN_TO_REG(val);
435         lm85_write_value(client, LM85_REG_FAN_MIN(nr), data->fan_min[nr]);
436         up(&data->update_lock);
437         return count;
438 }
439
440 #define show_fan_offset(offset)                                         \
441 static ssize_t show_fan_##offset (struct device *dev, char *buf)        \
442 {                                                                       \
443         return show_fan(dev, buf, 0x##offset - 1);                      \
444 }                                                                       \
445 static ssize_t show_fan_##offset##_min (struct device *dev, char *buf)  \
446 {                                                                       \
447         return show_fan_min(dev, buf, 0x##offset - 1);                  \
448 }                                                                       \
449 static ssize_t set_fan_##offset##_min (struct device *dev,              \
450         const char *buf, size_t count)                                  \
451 {                                                                       \
452         return set_fan_min(dev, buf, count, 0x##offset - 1);            \
453 }                                                                       \
454 static DEVICE_ATTR(fan##offset##_input, S_IRUGO, show_fan_##offset, NULL) \
455 static DEVICE_ATTR(fan##offset##_min, S_IRUGO | S_IWUSR,                \
456                 show_fan_##offset##_min, set_fan_##offset##_min)
457
458 show_fan_offset(1);
459 show_fan_offset(2);
460 show_fan_offset(3);
461 show_fan_offset(4);
462
463 /* vid, vrm, alarms */
464
465 static ssize_t show_vid_reg(struct device *dev, char *buf)
466 {
467         struct lm85_data *data = lm85_update_device(dev);
468         return sprintf(buf, "%ld\n", (long) vid_from_reg(data->vid, data->vrm));
469 }
470
471 static DEVICE_ATTR(in0_ref, S_IRUGO, show_vid_reg, NULL)
472
473 static ssize_t show_vrm_reg(struct device *dev, char *buf)
474 {
475         struct lm85_data *data = lm85_update_device(dev);
476         return sprintf(buf, "%ld\n", (long) data->vrm);
477 }
478
479 static ssize_t store_vrm_reg(struct device *dev, const char *buf, size_t count)
480 {
481         struct i2c_client *client = to_i2c_client(dev);
482         struct lm85_data *data = i2c_get_clientdata(client);
483         u32 val;
484
485         val = simple_strtoul(buf, NULL, 10);
486         data->vrm = val;
487         return count;
488 }
489
490 static DEVICE_ATTR(vrm, S_IRUGO | S_IWUSR, show_vrm_reg, store_vrm_reg)
491
492 static ssize_t show_alarms_reg(struct device *dev, char *buf)
493 {
494         struct lm85_data *data = lm85_update_device(dev);
495         return sprintf(buf, "%ld\n", (long) ALARMS_FROM_REG(data->alarms));
496 }
497
498 static DEVICE_ATTR(alarms, S_IRUGO, show_alarms_reg, NULL)
499
500 /* pwm */
501
502 static ssize_t show_pwm(struct device *dev, char *buf, int nr)
503 {
504         struct lm85_data *data = lm85_update_device(dev);
505         return sprintf(buf,"%d\n", PWM_FROM_REG(data->pwm[nr]) );
506 }
507 static ssize_t set_pwm(struct device *dev, const char *buf, 
508                 size_t count, int nr)
509 {
510         struct i2c_client *client = to_i2c_client(dev);
511         struct lm85_data *data = i2c_get_clientdata(client);
512         int     val;
513
514         down(&data->update_lock);
515         val = simple_strtol(buf, NULL, 10);
516         data->pwm[nr] = PWM_TO_REG(val);
517         lm85_write_value(client, LM85_REG_PWM(nr), data->pwm[nr]);
518         up(&data->update_lock);
519         return count;
520 }
521 static ssize_t show_pwm_enable(struct device *dev, char *buf, int nr)
522 {
523         struct lm85_data *data = lm85_update_device(dev);
524         int     pwm_zone;
525
526         pwm_zone = ZONE_FROM_REG(data->autofan[nr].config);
527         return sprintf(buf,"%d\n", (pwm_zone != 0 && pwm_zone != -1) );
528 }
529
530 #define show_pwm_reg(offset)                                            \
531 static ssize_t show_pwm_##offset (struct device *dev, char *buf)        \
532 {                                                                       \
533         return show_pwm(dev, buf, 0x##offset - 1);                      \
534 }                                                                       \
535 static ssize_t set_pwm_##offset (struct device *dev,                    \
536                                  const char *buf, size_t count)         \
537 {                                                                       \
538         return set_pwm(dev, buf, count, 0x##offset - 1);                \
539 }                                                                       \
540 static ssize_t show_pwm_enable##offset (struct device *dev, char *buf)  \
541 {                                                                       \
542         return show_pwm_enable(dev, buf, 0x##offset - 1);                       \
543 }                                                                       \
544 static DEVICE_ATTR(fan##offset##_pwm, S_IRUGO | S_IWUSR,                        \
545                 show_pwm_##offset, set_pwm_##offset)                    \
546 static DEVICE_ATTR(fan##offset##_pwm_enable, S_IRUGO, show_pwm_enable##offset, NULL)
547
548 show_pwm_reg(1);
549 show_pwm_reg(2);
550 show_pwm_reg(3);
551
552 /* Voltages */
553
554 static ssize_t show_in(struct device *dev, char *buf, int nr)
555 {
556         struct lm85_data *data = lm85_update_device(dev);
557         return sprintf(buf,"%d\n", INS_FROM_REG(nr, data->in[nr]) );
558 }
559 static ssize_t show_in_min(struct device *dev, char *buf, int nr)
560 {
561         struct lm85_data *data = lm85_update_device(dev);
562         return sprintf(buf,"%d\n", INS_FROM_REG(nr, data->in_min[nr]) );
563 }
564 static ssize_t set_in_min(struct device *dev, const char *buf, 
565                 size_t count, int nr)
566 {
567         struct i2c_client *client = to_i2c_client(dev);
568         struct lm85_data *data = i2c_get_clientdata(client);
569         int     val;
570
571         down(&data->update_lock);
572         val = simple_strtol(buf, NULL, 10);
573         data->in_min[nr] = INS_TO_REG(nr, val);
574         lm85_write_value(client, LM85_REG_IN_MIN(nr), data->in_min[nr]);
575         up(&data->update_lock);
576         return count;
577 }
578 static ssize_t show_in_max(struct device *dev, char *buf, int nr)
579 {
580         struct lm85_data *data = lm85_update_device(dev);
581         return sprintf(buf,"%d\n", INS_FROM_REG(nr, data->in_max[nr]) );
582 }
583 static ssize_t set_in_max(struct device *dev, const char *buf, 
584                 size_t count, int nr)
585 {
586         struct i2c_client *client = to_i2c_client(dev);
587         struct lm85_data *data = i2c_get_clientdata(client);
588         int     val;
589
590         down(&data->update_lock);
591         val = simple_strtol(buf, NULL, 10);
592         data->in_max[nr] = INS_TO_REG(nr, val);
593         lm85_write_value(client, LM85_REG_IN_MAX(nr), data->in_max[nr]);
594         up(&data->update_lock);
595         return count;
596 }
597 #define show_in_reg(offset)                                             \
598 static ssize_t show_in_##offset (struct device *dev, char *buf)         \
599 {                                                                       \
600         return show_in(dev, buf, 0x##offset);                           \
601 }                                                                       \
602 static ssize_t show_in_##offset##_min (struct device *dev, char *buf)   \
603 {                                                                       \
604         return show_in_min(dev, buf, 0x##offset);                       \
605 }                                                                       \
606 static ssize_t show_in_##offset##_max (struct device *dev, char *buf)   \
607 {                                                                       \
608         return show_in_max(dev, buf, 0x##offset);                       \
609 }                                                                       \
610 static ssize_t set_in_##offset##_min (struct device *dev,               \
611         const char *buf, size_t count)                                  \
612 {                                                                       \
613         return set_in_min(dev, buf, count, 0x##offset);                 \
614 }                                                                       \
615 static ssize_t set_in_##offset##_max (struct device *dev,               \
616         const char *buf, size_t count)                                  \
617 {                                                                       \
618         return set_in_max(dev, buf, count, 0x##offset);                 \
619 }                                                                       \
620 static DEVICE_ATTR(in##offset##_input, S_IRUGO, show_in_##offset, NULL) \
621 static DEVICE_ATTR(in##offset##_min, S_IRUGO | S_IWUSR,                 \
622                 show_in_##offset##_min, set_in_##offset##_min)          \
623 static DEVICE_ATTR(in##offset##_max, S_IRUGO | S_IWUSR,                 \
624                 show_in_##offset##_max, set_in_##offset##_max)
625
626 show_in_reg(0);
627 show_in_reg(1);
628 show_in_reg(2);
629 show_in_reg(3);
630 show_in_reg(4);
631
632 /* Temps */
633
634 static ssize_t show_temp(struct device *dev, char *buf, int nr)
635 {
636         struct lm85_data *data = lm85_update_device(dev);
637         return sprintf(buf,"%d\n", TEMP_FROM_REG(data->temp[nr]) );
638 }
639 static ssize_t show_temp_min(struct device *dev, char *buf, int nr)
640 {
641         struct lm85_data *data = lm85_update_device(dev);
642         return sprintf(buf,"%d\n", TEMP_FROM_REG(data->temp_min[nr]) );
643 }
644 static ssize_t set_temp_min(struct device *dev, const char *buf, 
645                 size_t count, int nr)
646 {
647         struct i2c_client *client = to_i2c_client(dev);
648         struct lm85_data *data = i2c_get_clientdata(client);
649         int     val;
650
651         down(&data->update_lock);
652         val = simple_strtol(buf, NULL, 10);
653         data->temp_min[nr] = TEMP_TO_REG(val);
654         lm85_write_value(client, LM85_REG_TEMP_MIN(nr), data->temp_min[nr]);
655         up(&data->update_lock);
656         return count;
657 }
658 static ssize_t show_temp_max(struct device *dev, char *buf, int nr)
659 {
660         struct lm85_data *data = lm85_update_device(dev);
661         return sprintf(buf,"%d\n", TEMP_FROM_REG(data->temp_max[nr]) );
662 }
663 static ssize_t set_temp_max(struct device *dev, const char *buf, 
664                 size_t count, int nr)
665 {
666         struct i2c_client *client = to_i2c_client(dev);
667         struct lm85_data *data = i2c_get_clientdata(client);
668         int     val;
669
670         down(&data->update_lock);
671         val = simple_strtol(buf, NULL, 10);
672         data->temp_max[nr] = TEMP_TO_REG(val);
673         lm85_write_value(client, LM85_REG_TEMP_MAX(nr), data->temp_max[nr]);
674         up(&data->update_lock);
675         return count;
676 }
677 #define show_temp_reg(offset)                                           \
678 static ssize_t show_temp_##offset (struct device *dev, char *buf)       \
679 {                                                                       \
680         return show_temp(dev, buf, 0x##offset - 1);                     \
681 }                                                                       \
682 static ssize_t show_temp_##offset##_min (struct device *dev, char *buf) \
683 {                                                                       \
684         return show_temp_min(dev, buf, 0x##offset - 1);                 \
685 }                                                                       \
686 static ssize_t show_temp_##offset##_max (struct device *dev, char *buf) \
687 {                                                                       \
688         return show_temp_max(dev, buf, 0x##offset - 1);                 \
689 }                                                                       \
690 static ssize_t set_temp_##offset##_min (struct device *dev,             \
691         const char *buf, size_t count)                                  \
692 {                                                                       \
693         return set_temp_min(dev, buf, count, 0x##offset - 1);           \
694 }                                                                       \
695 static ssize_t set_temp_##offset##_max (struct device *dev,             \
696         const char *buf, size_t count)                                  \
697 {                                                                       \
698         return set_temp_max(dev, buf, count, 0x##offset - 1);           \
699 }                                                                       \
700 static DEVICE_ATTR(temp##offset##_input, S_IRUGO, show_temp_##offset, NULL)     \
701 static DEVICE_ATTR(temp##offset##_min, S_IRUGO | S_IWUSR,               \
702                 show_temp_##offset##_min, set_temp_##offset##_min)      \
703 static DEVICE_ATTR(temp##offset##_max, S_IRUGO | S_IWUSR,               \
704                 show_temp_##offset##_max, set_temp_##offset##_max)
705
706 show_temp_reg(1);
707 show_temp_reg(2);
708 show_temp_reg(3);
709
710
711 int lm85_attach_adapter(struct i2c_adapter *adapter)
712 {
713         return i2c_detect(adapter, &addr_data, lm85_detect);
714 }
715
716 int lm85_detect(struct i2c_adapter *adapter, int address,
717                 int kind)
718 {
719         int company, verstep ;
720         struct i2c_client *new_client = NULL;
721         struct lm85_data *data;
722         int err = 0;
723         const char *type_name = "";
724
725         if (i2c_is_isa_adapter(adapter)) {
726                 /* This chip has no ISA interface */
727                 goto ERROR0 ;
728         };
729
730         if (!i2c_check_functionality(adapter,
731                                         I2C_FUNC_SMBUS_BYTE_DATA)) {
732                 /* We need to be able to do byte I/O */
733                 goto ERROR0 ;
734         };
735
736         /* OK. For now, we presume we have a valid client. We now create the
737            client structure, even though we cannot fill it completely yet.
738            But it allows us to access lm85_{read,write}_value. */
739
740         if (!(data = kmalloc(sizeof(struct lm85_data), GFP_KERNEL))) {
741                 err = -ENOMEM;
742                 goto ERROR0;
743         }
744         memset(data, 0, sizeof(struct lm85_data));
745
746         new_client = &data->client;
747         i2c_set_clientdata(new_client, data);
748         new_client->addr = address;
749         new_client->adapter = adapter;
750         new_client->driver = &lm85_driver;
751         new_client->flags = 0;
752
753         /* Now, we do the remaining detection. */
754
755         company = lm85_read_value(new_client, LM85_REG_COMPANY);
756         verstep = lm85_read_value(new_client, LM85_REG_VERSTEP);
757
758         dev_dbg(&adapter->dev, "Detecting device at %d,0x%02x with"
759                 " COMPANY: 0x%02x and VERSTEP: 0x%02x\n",
760                 i2c_adapter_id(new_client->adapter), new_client->addr,
761                 company, verstep);
762
763         /* If auto-detecting, Determine the chip type. */
764         if (kind <= 0) {
765                 dev_dbg(&adapter->dev, "Autodetecting device at %d,0x%02x ...\n",
766                         i2c_adapter_id(adapter), address );
767                 if( company == LM85_COMPANY_NATIONAL
768                     && verstep == LM85_VERSTEP_LM85C ) {
769                         kind = lm85c ;
770                 } else if( company == LM85_COMPANY_NATIONAL
771                     && verstep == LM85_VERSTEP_LM85B ) {
772                         kind = lm85b ;
773                 } else if( company == LM85_COMPANY_NATIONAL
774                     && (verstep & 0xf0) == LM85_VERSTEP_GENERIC ) {
775                         dev_err(&adapter->dev, "Unrecognized version/stepping 0x%02x"
776                                 " Defaulting to LM85.\n", verstep);
777                         kind = any_chip ;
778                 } else if( company == LM85_COMPANY_ANALOG_DEV
779                     && verstep == LM85_VERSTEP_ADM1027 ) {
780                         kind = adm1027 ;
781                 } else if( company == LM85_COMPANY_ANALOG_DEV
782                     && verstep == LM85_VERSTEP_ADT7463 ) {
783                         kind = adt7463 ;
784                 } else if( company == LM85_COMPANY_ANALOG_DEV
785                     && (verstep & 0xf0) == LM85_VERSTEP_GENERIC ) {
786                         dev_err(&adapter->dev, "Unrecognized version/stepping 0x%02x"
787                                 " Defaulting to ADM1027.\n", verstep);
788                         kind = adm1027 ;
789                 } else if( kind == 0 && (verstep & 0xf0) == 0x60) {
790                         dev_err(&adapter->dev, "Generic LM85 Version 6 detected\n");
791                         /* Leave kind as "any_chip" */
792                 } else {
793                         dev_dbg(&adapter->dev, "Autodetection failed\n");
794                         /* Not an LM85 ... */
795                         if( kind == 0 ) {  /* User used force=x,y */
796                                 dev_err(&adapter->dev, "Generic LM85 Version 6 not"
797                                         " found at %d,0x%02x. Try force_lm85c.\n",
798                                         i2c_adapter_id(adapter), address );
799                         }
800                         err = 0 ;
801                         goto ERROR1;
802                 }
803         }
804
805         /* Fill in the chip specific driver values */
806         if ( kind == any_chip ) {
807                 type_name = "lm85";
808         } else if ( kind == lm85b ) {
809                 type_name = "lm85b";
810         } else if ( kind == lm85c ) {
811                 type_name = "lm85c";
812         } else if ( kind == adm1027 ) {
813                 type_name = "adm1027";
814         } else if ( kind == adt7463 ) {
815                 type_name = "adt7463";
816         }
817         strlcpy(new_client->name, type_name, I2C_NAME_SIZE);
818
819         /* Fill in the remaining client fields */
820         new_client->id = lm85_id++;
821         data->type = kind;
822         data->valid = 0;
823         init_MUTEX(&data->update_lock);
824
825         dev_dbg(&adapter->dev, "Assigning ID %d to %s at %d,0x%02x\n",
826                 new_client->id, new_client->name,
827                 i2c_adapter_id(new_client->adapter),
828                 new_client->addr);
829
830         /* Tell the I2C layer a new client has arrived */
831         if ((err = i2c_attach_client(new_client)))
832                 goto ERROR1;
833
834         /* Set the VRM version */
835         data->vrm = LM85_INIT_VRM ;
836
837         /* Initialize the LM85 chip */
838         lm85_init_client(new_client);
839
840         /* Register sysfs hooks */
841         device_create_file(&new_client->dev, &dev_attr_fan1_input);
842         device_create_file(&new_client->dev, &dev_attr_fan2_input);
843         device_create_file(&new_client->dev, &dev_attr_fan3_input);
844         device_create_file(&new_client->dev, &dev_attr_fan4_input);
845         device_create_file(&new_client->dev, &dev_attr_fan1_min);
846         device_create_file(&new_client->dev, &dev_attr_fan2_min);
847         device_create_file(&new_client->dev, &dev_attr_fan3_min);
848         device_create_file(&new_client->dev, &dev_attr_fan4_min);
849         device_create_file(&new_client->dev, &dev_attr_fan1_pwm);
850         device_create_file(&new_client->dev, &dev_attr_fan2_pwm);
851         device_create_file(&new_client->dev, &dev_attr_fan3_pwm);
852         device_create_file(&new_client->dev, &dev_attr_fan1_pwm_enable);
853         device_create_file(&new_client->dev, &dev_attr_fan2_pwm_enable);
854         device_create_file(&new_client->dev, &dev_attr_fan3_pwm_enable);
855         device_create_file(&new_client->dev, &dev_attr_in0_input);
856         device_create_file(&new_client->dev, &dev_attr_in1_input);
857         device_create_file(&new_client->dev, &dev_attr_in2_input);
858         device_create_file(&new_client->dev, &dev_attr_in3_input);
859         device_create_file(&new_client->dev, &dev_attr_in4_input);
860         device_create_file(&new_client->dev, &dev_attr_in0_min);
861         device_create_file(&new_client->dev, &dev_attr_in1_min);
862         device_create_file(&new_client->dev, &dev_attr_in2_min);
863         device_create_file(&new_client->dev, &dev_attr_in3_min);
864         device_create_file(&new_client->dev, &dev_attr_in4_min);
865         device_create_file(&new_client->dev, &dev_attr_in0_max);
866         device_create_file(&new_client->dev, &dev_attr_in1_max);
867         device_create_file(&new_client->dev, &dev_attr_in2_max);
868         device_create_file(&new_client->dev, &dev_attr_in3_max);
869         device_create_file(&new_client->dev, &dev_attr_in4_max);
870         device_create_file(&new_client->dev, &dev_attr_temp1_input);
871         device_create_file(&new_client->dev, &dev_attr_temp2_input);
872         device_create_file(&new_client->dev, &dev_attr_temp3_input);
873         device_create_file(&new_client->dev, &dev_attr_temp1_min);
874         device_create_file(&new_client->dev, &dev_attr_temp2_min);
875         device_create_file(&new_client->dev, &dev_attr_temp3_min);
876         device_create_file(&new_client->dev, &dev_attr_temp1_max);
877         device_create_file(&new_client->dev, &dev_attr_temp2_max);
878         device_create_file(&new_client->dev, &dev_attr_temp3_max);
879         device_create_file(&new_client->dev, &dev_attr_vrm);
880         device_create_file(&new_client->dev, &dev_attr_in0_ref);
881         device_create_file(&new_client->dev, &dev_attr_alarms);
882
883         return 0;
884
885         /* Error out and cleanup code */
886     ERROR1:
887         kfree(data);
888     ERROR0:
889         return err;
890 }
891
892 int lm85_detach_client(struct i2c_client *client)
893 {
894         i2c_detach_client(client);
895         kfree(i2c_get_clientdata(client));
896         return 0;
897 }
898
899
900 int lm85_read_value(struct i2c_client *client, u8 reg)
901 {
902         int res;
903
904         /* What size location is it? */
905         switch( reg ) {
906         case LM85_REG_FAN(0) :  /* Read WORD data */
907         case LM85_REG_FAN(1) :
908         case LM85_REG_FAN(2) :
909         case LM85_REG_FAN(3) :
910         case LM85_REG_FAN_MIN(0) :
911         case LM85_REG_FAN_MIN(1) :
912         case LM85_REG_FAN_MIN(2) :
913         case LM85_REG_FAN_MIN(3) :
914         case LM85_REG_ALARM1 :  /* Read both bytes at once */
915         case ADM1027_REG_EXTEND_ADC1 :  /* Read two bytes at once */
916                 res = i2c_smbus_read_byte_data(client, reg) & 0xff ;
917                 res |= i2c_smbus_read_byte_data(client, reg+1) << 8 ;
918                 break ;
919         case ADT7463_REG_TMIN_CTL1 :  /* Read WORD MSB, LSB */
920                 res = i2c_smbus_read_byte_data(client, reg) << 8 ;
921                 res |= i2c_smbus_read_byte_data(client, reg+1) & 0xff ;
922                 break ;
923         default:        /* Read BYTE data */
924                 res = i2c_smbus_read_byte_data(client, reg);
925                 break ;
926         }
927
928         return res ;
929 }
930
931 int lm85_write_value(struct i2c_client *client, u8 reg, int value)
932 {
933         int res ;
934
935         switch( reg ) {
936         case LM85_REG_FAN(0) :  /* Write WORD data */
937         case LM85_REG_FAN(1) :
938         case LM85_REG_FAN(2) :
939         case LM85_REG_FAN(3) :
940         case LM85_REG_FAN_MIN(0) :
941         case LM85_REG_FAN_MIN(1) :
942         case LM85_REG_FAN_MIN(2) :
943         case LM85_REG_FAN_MIN(3) :
944         /* NOTE: ALARM is read only, so not included here */
945                 res = i2c_smbus_write_byte_data(client, reg, value & 0xff) ;
946                 res |= i2c_smbus_write_byte_data(client, reg+1, (value>>8) & 0xff) ;
947                 break ;
948         case ADT7463_REG_TMIN_CTL1 :  /* Write WORD MSB, LSB */
949                 res = i2c_smbus_write_byte_data(client, reg, (value>>8) & 0xff);
950                 res |= i2c_smbus_write_byte_data(client, reg+1, value & 0xff) ;
951                 break ;
952         default:        /* Write BYTE data */
953                 res = i2c_smbus_write_byte_data(client, reg, value);
954                 break ;
955         }
956
957         return res ;
958 }
959
960 void lm85_init_client(struct i2c_client *client)
961 {
962         int value;
963         struct lm85_data *data = i2c_get_clientdata(client);
964
965         dev_dbg(&client->dev, "Initializing device\n");
966
967         /* Warn if part was not "READY" */
968         value = lm85_read_value(client, LM85_REG_CONFIG);
969         dev_dbg(&client->dev, "LM85_REG_CONFIG is: 0x%02x\n", value);
970         if( value & 0x02 ) {
971                 dev_err(&client->dev, "Client (%d,0x%02x) config is locked.\n",
972                             i2c_adapter_id(client->adapter), client->addr );
973         };
974         if( ! (value & 0x04) ) {
975                 dev_err(&client->dev, "Client (%d,0x%02x) is not ready.\n",
976                             i2c_adapter_id(client->adapter), client->addr );
977         };
978         if( value & 0x10
979             && ( data->type == adm1027
980                 || data->type == adt7463 ) ) {
981                 dev_err(&client->dev, "Client (%d,0x%02x) VxI mode is set.  "
982                         "Please report this to the lm85 maintainer.\n",
983                             i2c_adapter_id(client->adapter), client->addr );
984         };
985
986         /* WE INTENTIONALLY make no changes to the limits,
987          *   offsets, pwms, fans and zones.  If they were
988          *   configured, we don't want to mess with them.
989          *   If they weren't, the default is 100% PWM, no
990          *   control and will suffice until 'sensors -s'
991          *   can be run by the user.
992          */
993
994         /* Start monitoring */
995         value = lm85_read_value(client, LM85_REG_CONFIG);
996         /* Try to clear LOCK, Set START, save everything else */
997         value = (value & ~ 0x02) | 0x01 ;
998         dev_dbg(&client->dev, "Setting CONFIG to: 0x%02x\n", value);
999         lm85_write_value(client, LM85_REG_CONFIG, value);
1000 }
1001
1002 static struct lm85_data *lm85_update_device(struct device *dev)
1003 {
1004         struct i2c_client *client = to_i2c_client(dev);
1005         struct lm85_data *data = i2c_get_clientdata(client);
1006         int i;
1007
1008         down(&data->update_lock);
1009
1010         if ( !data->valid ||
1011              (jiffies - data->last_reading > LM85_DATA_INTERVAL ) ) {
1012                 /* Things that change quickly */
1013                 dev_dbg(&client->dev, "Reading sensor values\n");
1014                 
1015                 /* Have to read extended bits first to "freeze" the
1016                  * more significant bits that are read later.
1017                  */
1018                 if ( (data->type == adm1027) || (data->type == adt7463) ) {
1019                         data->extend_adc =
1020                             lm85_read_value(client, ADM1027_REG_EXTEND_ADC1);
1021                 }
1022
1023                 for (i = 0; i <= 4; ++i) {
1024                         data->in[i] =
1025                             lm85_read_value(client, LM85_REG_IN(i));
1026                 }
1027
1028                 for (i = 0; i <= 3; ++i) {
1029                         data->fan[i] =
1030                             lm85_read_value(client, LM85_REG_FAN(i));
1031                 }
1032
1033                 for (i = 0; i <= 2; ++i) {
1034                         data->temp[i] =
1035                             lm85_read_value(client, LM85_REG_TEMP(i));
1036                 }
1037
1038                 for (i = 0; i <= 2; ++i) {
1039                         data->pwm[i] =
1040                             lm85_read_value(client, LM85_REG_PWM(i));
1041                 }
1042
1043                 if ( data->type == adt7463 ) {
1044                         if( data->therm_total < ULONG_MAX - 256 ) {
1045                             data->therm_total +=
1046                                 lm85_read_value(client, ADT7463_REG_THERM );
1047                         }
1048                 }
1049
1050                 data->alarms = lm85_read_value(client, LM85_REG_ALARM1);
1051
1052                 data->last_reading = jiffies ;
1053         };  /* last_reading */
1054
1055         if ( !data->valid ||
1056              (jiffies - data->last_config > LM85_CONFIG_INTERVAL) ) {
1057                 /* Things that don't change often */
1058                 dev_dbg(&client->dev, "Reading config values\n");
1059
1060                 for (i = 0; i <= 4; ++i) {
1061                         data->in_min[i] =
1062                             lm85_read_value(client, LM85_REG_IN_MIN(i));
1063                         data->in_max[i] =
1064                             lm85_read_value(client, LM85_REG_IN_MAX(i));
1065                 }
1066
1067                 for (i = 0; i <= 3; ++i) {
1068                         data->fan_min[i] =
1069                             lm85_read_value(client, LM85_REG_FAN_MIN(i));
1070                 }
1071
1072                 for (i = 0; i <= 2; ++i) {
1073                         data->temp_min[i] =
1074                             lm85_read_value(client, LM85_REG_TEMP_MIN(i));
1075                         data->temp_max[i] =
1076                             lm85_read_value(client, LM85_REG_TEMP_MAX(i));
1077                 }
1078
1079                 data->vid = lm85_read_value(client, LM85_REG_VID);
1080
1081                 for (i = 0; i <= 2; ++i) {
1082                         int val ;
1083                         data->autofan[i].config =
1084                             lm85_read_value(client, LM85_REG_AFAN_CONFIG(i));
1085                         val = lm85_read_value(client, LM85_REG_AFAN_RANGE(i));
1086                         data->autofan[i].freq = val & 0x07 ;
1087                         data->zone[i].range = (val >> 4) & 0x0f ;
1088                         data->autofan[i].min_pwm =
1089                             lm85_read_value(client, LM85_REG_AFAN_MINPWM(i));
1090                         data->zone[i].limit =
1091                             lm85_read_value(client, LM85_REG_AFAN_LIMIT(i));
1092                         data->zone[i].critical =
1093                             lm85_read_value(client, LM85_REG_AFAN_CRITICAL(i));
1094                 }
1095
1096                 i = lm85_read_value(client, LM85_REG_AFAN_SPIKE1);
1097                 data->smooth[0] = i & 0x0f ;
1098                 data->syncpwm3 = i & 0x10 ;  /* Save PWM3 config */
1099                 data->autofan[0].min_off = (i & 0x20) != 0 ;
1100                 data->autofan[1].min_off = (i & 0x40) != 0 ;
1101                 data->autofan[2].min_off = (i & 0x80) != 0 ;
1102                 i = lm85_read_value(client, LM85_REG_AFAN_SPIKE2);
1103                 data->smooth[1] = (i>>4) & 0x0f ;
1104                 data->smooth[2] = i & 0x0f ;
1105
1106                 i = lm85_read_value(client, LM85_REG_AFAN_HYST1);
1107                 data->zone[0].hyst = (i>>4) & 0x0f ;
1108                 data->zone[1].hyst = i & 0x0f ;
1109
1110                 i = lm85_read_value(client, LM85_REG_AFAN_HYST2);
1111                 data->zone[2].hyst = (i>>4) & 0x0f ;
1112
1113                 if ( (data->type == lm85b) || (data->type == lm85c) ) {
1114                         data->tach_mode = lm85_read_value(client,
1115                                 LM85_REG_TACH_MODE );
1116                         data->spinup_ctl = lm85_read_value(client,
1117                                 LM85_REG_SPINUP_CTL );
1118                 } else if ( (data->type == adt7463) || (data->type == adm1027) ) {
1119                         if ( data->type == adt7463 ) {
1120                                 for (i = 0; i <= 2; ++i) {
1121                                     data->oppoint[i] = lm85_read_value(client,
1122                                         ADT7463_REG_OPPOINT(i) );
1123                                 }
1124                                 data->tmin_ctl = lm85_read_value(client,
1125                                         ADT7463_REG_TMIN_CTL1 );
1126                                 data->therm_limit = lm85_read_value(client,
1127                                         ADT7463_REG_THERM_LIMIT );
1128                         }
1129                         for (i = 0; i <= 2; ++i) {
1130                             data->temp_offset[i] = lm85_read_value(client,
1131                                 ADM1027_REG_TEMP_OFFSET(i) );
1132                         }
1133                         data->tach_mode = lm85_read_value(client,
1134                                 ADM1027_REG_CONFIG3 );
1135                         data->fan_ppr = lm85_read_value(client,
1136                                 ADM1027_REG_FAN_PPR );
1137                 }
1138         
1139                 data->last_config = jiffies;
1140         };  /* last_config */
1141
1142         data->valid = 1;
1143
1144         up(&data->update_lock);
1145
1146         return data;
1147 }
1148
1149
1150 static int __init sm_lm85_init(void)
1151 {
1152         return i2c_add_driver(&lm85_driver);
1153 }
1154
1155 static void  __exit sm_lm85_exit(void)
1156 {
1157         i2c_del_driver(&lm85_driver);
1158 }
1159
1160 /* Thanks to Richard Barrington for adding the LM85 to sensors-detect.
1161  * Thanks to Margit Schubert-While <margitsw@t-online.de> for help with
1162  *     post 2.7.0 CVS changes.
1163  */
1164 MODULE_LICENSE("GPL");
1165 MODULE_AUTHOR("Philip Pokorny <ppokorny@penguincomputing.com>, Margit Schubert-While <margitsw@t-online.de>");
1166 MODULE_DESCRIPTION("LM85-B, LM85-C driver");
1167
1168 module_init(sm_lm85_init);
1169 module_exit(sm_lm85_exit);