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