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