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