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