2 lm85.c - Part of lm_sensors, Linux kernel modules for hardware
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>
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,
192 static int RANGE_TO_REG( int range )
196 if( range >= lm85_range_map[15] ) { return 15 ; }
197 for( i = 0 ; i < 15 ; ++i )
198 if( range <= lm85_range_map[i] )
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 )
217 if( smooth <= 0 ) { return 0 ; } /* Disabled */
218 for( i = 0 ; i < 7 ; ++i )
219 if( smooth >= lm85_smooth_map[i] )
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 )
233 if( spinup >= lm85_spinup_map[7] ) { return 7 ; }
234 for( i = 0 ; i < 7 ; ++i )
235 if( spinup <= lm85_spinup_map[i] )
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 )
249 if( freq >= lm85_freq_map[7] ) { return 7 ; }
250 for( i = 0 ; i < 7 ; ++i )
251 if( freq <= lm85_freq_map[i] )
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 )
280 for( i = 0 ; i <= 7 ; ++i )
281 if( zone == lm85_zone_map[i] )
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 /* Typically used with Pentium 4 systems v9.1 VRM spec */
312 #define LM85_INIT_VRM 91
314 /* Chip sampling rates
316 * Some sensors are not updated more frequently than once per second
317 * so it doesn't make sense to read them more often than that.
318 * We cache the results and return the saved data if the driver
319 * is called again before a second has elapsed.
321 * Also, there is significant configuration data for this chip
322 * given the automatic PWM fan control that is possible. There
323 * are about 47 bytes of config data to only 22 bytes of actual
324 * readings. So, we keep the config data up to date in the cache
325 * when it is written and only sample it once every 1 *minute*
327 #define LM85_DATA_INTERVAL (HZ + HZ / 2)
328 #define LM85_CONFIG_INTERVAL (1 * 60 * HZ)
330 /* For each registered LM85, we need to keep some data in memory. That
331 data is pointed to by lm85_list[NR]->data. The structure itself is
332 dynamically allocated, at the same time when a new lm85 client is
335 /* LM85 can automatically adjust fan speeds based on temperature
336 * This structure encapsulates an entire Zone config. There are
337 * three zones (one for each temperature input) on the lm85
340 s8 limit; /* Low temp limit */
341 u8 hyst; /* Low limit hysteresis. (0-15) */
342 u8 range; /* Temp range, encoded */
343 s8 critical; /* "All fans ON" temp limit */
346 struct lm85_autofan {
347 u8 config; /* Register value */
348 u8 freq; /* PWM frequency, encoded */
349 u8 min_pwm; /* Minimum PWM value, encoded */
350 u8 min_off; /* Min PWM or OFF below "limit", flag */
354 struct i2c_client client;
355 struct semaphore lock;
358 struct semaphore update_lock;
359 int valid; /* !=0 if following fields are valid */
360 unsigned long last_reading; /* In jiffies */
361 unsigned long last_config; /* In jiffies */
363 u8 in[5]; /* Register value */
364 u8 in_max[5]; /* Register value */
365 u8 in_min[5]; /* Register value */
366 s8 temp[3]; /* Register value */
367 s8 temp_min[3]; /* Register value */
368 s8 temp_max[3]; /* Register value */
369 s8 temp_offset[3]; /* Register value */
370 u16 fan[4]; /* Register value */
371 u16 fan_min[4]; /* Register value */
372 u8 pwm[3]; /* Register value */
373 u8 spinup_ctl; /* Register encoding, combined */
374 u8 tach_mode; /* Register encoding, combined */
375 u16 extend_adc; /* Register value */
376 u8 fan_ppr; /* Register value */
377 u8 smooth[3]; /* Register encoding */
378 u8 vid; /* Register value */
379 u8 vrm; /* VRM version */
380 u8 syncpwm3; /* Saved PWM3 for TACH 2,3,4 config */
381 u8 oppoint[3]; /* Register value */
382 u16 tmin_ctl; /* Register value */
383 unsigned long therm_total; /* Cummulative therm count */
384 u8 therm_limit; /* Register value */
385 u16 alarms; /* Register encoding, combined */
386 struct lm85_autofan autofan[3];
387 struct lm85_zone zone[3];
390 static int lm85_attach_adapter(struct i2c_adapter *adapter);
391 static int lm85_detect(struct i2c_adapter *adapter, int address,
393 static int lm85_detach_client(struct i2c_client *client);
395 static int lm85_read_value(struct i2c_client *client, u8 register);
396 static int lm85_write_value(struct i2c_client *client, u8 register, int value);
397 static struct lm85_data *lm85_update_device(struct device *dev);
398 static void lm85_init_client(struct i2c_client *client);
401 static struct i2c_driver lm85_driver = {
402 .owner = THIS_MODULE,
404 .id = I2C_DRIVERID_LM85,
405 .flags = I2C_DF_NOTIFY,
406 .attach_adapter = lm85_attach_adapter,
407 .detach_client = lm85_detach_client,
410 /* Unique ID assigned to each LM85 detected */
411 static int lm85_id = 0;
415 static ssize_t show_fan(struct device *dev, char *buf, int nr)
417 struct lm85_data *data = lm85_update_device(dev);
418 return sprintf(buf,"%d\n", FAN_FROM_REG(data->fan[nr]) );
420 static ssize_t show_fan_min(struct device *dev, char *buf, int nr)
422 struct lm85_data *data = lm85_update_device(dev);
423 return sprintf(buf,"%d\n", FAN_FROM_REG(data->fan_min[nr]) );
425 static ssize_t set_fan_min(struct device *dev, const char *buf,
426 size_t count, int nr)
428 struct i2c_client *client = to_i2c_client(dev);
429 struct lm85_data *data = i2c_get_clientdata(client);
432 down(&data->update_lock);
433 val = simple_strtol(buf, NULL, 10);
434 data->fan_min[nr] = FAN_TO_REG(val);
435 lm85_write_value(client, LM85_REG_FAN_MIN(nr), data->fan_min[nr]);
436 up(&data->update_lock);
440 #define show_fan_offset(offset) \
441 static ssize_t show_fan_##offset (struct device *dev, char *buf) \
443 return show_fan(dev, buf, 0x##offset - 1); \
445 static ssize_t show_fan_##offset##_min (struct device *dev, char *buf) \
447 return show_fan_min(dev, buf, 0x##offset - 1); \
449 static ssize_t set_fan_##offset##_min (struct device *dev, \
450 const char *buf, size_t count) \
452 return set_fan_min(dev, buf, count, 0x##offset - 1); \
454 static DEVICE_ATTR(fan##offset##_input, S_IRUGO, show_fan_##offset, NULL);\
455 static DEVICE_ATTR(fan##offset##_min, S_IRUGO | S_IWUSR, \
456 show_fan_##offset##_min, set_fan_##offset##_min);
463 /* vid, vrm, alarms */
465 static ssize_t show_vid_reg(struct device *dev, char *buf)
467 struct lm85_data *data = lm85_update_device(dev);
468 return sprintf(buf, "%ld\n", (long) vid_from_reg(data->vid, data->vrm));
471 static DEVICE_ATTR(in0_ref, S_IRUGO, show_vid_reg, NULL);
473 static ssize_t show_vrm_reg(struct device *dev, char *buf)
475 struct lm85_data *data = lm85_update_device(dev);
476 return sprintf(buf, "%ld\n", (long) data->vrm);
479 static ssize_t store_vrm_reg(struct device *dev, const char *buf, size_t count)
481 struct i2c_client *client = to_i2c_client(dev);
482 struct lm85_data *data = i2c_get_clientdata(client);
485 val = simple_strtoul(buf, NULL, 10);
490 static DEVICE_ATTR(vrm, S_IRUGO | S_IWUSR, show_vrm_reg, store_vrm_reg);
492 static ssize_t show_alarms_reg(struct device *dev, char *buf)
494 struct lm85_data *data = lm85_update_device(dev);
495 return sprintf(buf, "%ld\n", (long) ALARMS_FROM_REG(data->alarms));
498 static DEVICE_ATTR(alarms, S_IRUGO, show_alarms_reg, NULL);
502 static ssize_t show_pwm(struct device *dev, char *buf, int nr)
504 struct lm85_data *data = lm85_update_device(dev);
505 return sprintf(buf,"%d\n", PWM_FROM_REG(data->pwm[nr]) );
507 static ssize_t set_pwm(struct device *dev, const char *buf,
508 size_t count, int nr)
510 struct i2c_client *client = to_i2c_client(dev);
511 struct lm85_data *data = i2c_get_clientdata(client);
514 down(&data->update_lock);
515 val = simple_strtol(buf, NULL, 10);
516 data->pwm[nr] = PWM_TO_REG(val);
517 lm85_write_value(client, LM85_REG_PWM(nr), data->pwm[nr]);
518 up(&data->update_lock);
521 static ssize_t show_pwm_enable(struct device *dev, char *buf, int nr)
523 struct lm85_data *data = lm85_update_device(dev);
526 pwm_zone = ZONE_FROM_REG(data->autofan[nr].config);
527 return sprintf(buf,"%d\n", (pwm_zone != 0 && pwm_zone != -1) );
530 #define show_pwm_reg(offset) \
531 static ssize_t show_pwm_##offset (struct device *dev, char *buf) \
533 return show_pwm(dev, buf, 0x##offset - 1); \
535 static ssize_t set_pwm_##offset (struct device *dev, \
536 const char *buf, size_t count) \
538 return set_pwm(dev, buf, count, 0x##offset - 1); \
540 static ssize_t show_pwm_enable##offset (struct device *dev, char *buf) \
542 return show_pwm_enable(dev, buf, 0x##offset - 1); \
544 static DEVICE_ATTR(fan##offset##_pwm, S_IRUGO | S_IWUSR, \
545 show_pwm_##offset, set_pwm_##offset); \
546 static DEVICE_ATTR(fan##offset##_pwm_enable, S_IRUGO, show_pwm_enable##offset, NULL);
554 static ssize_t show_in(struct device *dev, char *buf, int nr)
556 struct lm85_data *data = lm85_update_device(dev);
557 return sprintf(buf,"%d\n", INS_FROM_REG(nr, data->in[nr]) );
559 static ssize_t show_in_min(struct device *dev, char *buf, int nr)
561 struct lm85_data *data = lm85_update_device(dev);
562 return sprintf(buf,"%d\n", INS_FROM_REG(nr, data->in_min[nr]) );
564 static ssize_t set_in_min(struct device *dev, const char *buf,
565 size_t count, int nr)
567 struct i2c_client *client = to_i2c_client(dev);
568 struct lm85_data *data = i2c_get_clientdata(client);
571 down(&data->update_lock);
572 val = simple_strtol(buf, NULL, 10);
573 data->in_min[nr] = INS_TO_REG(nr, val);
574 lm85_write_value(client, LM85_REG_IN_MIN(nr), data->in_min[nr]);
575 up(&data->update_lock);
578 static ssize_t show_in_max(struct device *dev, char *buf, int nr)
580 struct lm85_data *data = lm85_update_device(dev);
581 return sprintf(buf,"%d\n", INS_FROM_REG(nr, data->in_max[nr]) );
583 static ssize_t set_in_max(struct device *dev, const char *buf,
584 size_t count, int nr)
586 struct i2c_client *client = to_i2c_client(dev);
587 struct lm85_data *data = i2c_get_clientdata(client);
590 down(&data->update_lock);
591 val = simple_strtol(buf, NULL, 10);
592 data->in_max[nr] = INS_TO_REG(nr, val);
593 lm85_write_value(client, LM85_REG_IN_MAX(nr), data->in_max[nr]);
594 up(&data->update_lock);
597 #define show_in_reg(offset) \
598 static ssize_t show_in_##offset (struct device *dev, char *buf) \
600 return show_in(dev, buf, 0x##offset); \
602 static ssize_t show_in_##offset##_min (struct device *dev, char *buf) \
604 return show_in_min(dev, buf, 0x##offset); \
606 static ssize_t show_in_##offset##_max (struct device *dev, char *buf) \
608 return show_in_max(dev, buf, 0x##offset); \
610 static ssize_t set_in_##offset##_min (struct device *dev, \
611 const char *buf, size_t count) \
613 return set_in_min(dev, buf, count, 0x##offset); \
615 static ssize_t set_in_##offset##_max (struct device *dev, \
616 const char *buf, size_t count) \
618 return set_in_max(dev, buf, count, 0x##offset); \
620 static DEVICE_ATTR(in##offset##_input, S_IRUGO, show_in_##offset, NULL); \
621 static DEVICE_ATTR(in##offset##_min, S_IRUGO | S_IWUSR, \
622 show_in_##offset##_min, set_in_##offset##_min); \
623 static DEVICE_ATTR(in##offset##_max, S_IRUGO | S_IWUSR, \
624 show_in_##offset##_max, set_in_##offset##_max);
634 static ssize_t show_temp(struct device *dev, char *buf, int nr)
636 struct lm85_data *data = lm85_update_device(dev);
637 return sprintf(buf,"%d\n", TEMP_FROM_REG(data->temp[nr]) );
639 static ssize_t show_temp_min(struct device *dev, char *buf, int nr)
641 struct lm85_data *data = lm85_update_device(dev);
642 return sprintf(buf,"%d\n", TEMP_FROM_REG(data->temp_min[nr]) );
644 static ssize_t set_temp_min(struct device *dev, const char *buf,
645 size_t count, int nr)
647 struct i2c_client *client = to_i2c_client(dev);
648 struct lm85_data *data = i2c_get_clientdata(client);
651 down(&data->update_lock);
652 val = simple_strtol(buf, NULL, 10);
653 data->temp_min[nr] = TEMP_TO_REG(val);
654 lm85_write_value(client, LM85_REG_TEMP_MIN(nr), data->temp_min[nr]);
655 up(&data->update_lock);
658 static ssize_t show_temp_max(struct device *dev, char *buf, int nr)
660 struct lm85_data *data = lm85_update_device(dev);
661 return sprintf(buf,"%d\n", TEMP_FROM_REG(data->temp_max[nr]) );
663 static ssize_t set_temp_max(struct device *dev, const char *buf,
664 size_t count, int nr)
666 struct i2c_client *client = to_i2c_client(dev);
667 struct lm85_data *data = i2c_get_clientdata(client);
670 down(&data->update_lock);
671 val = simple_strtol(buf, NULL, 10);
672 data->temp_max[nr] = TEMP_TO_REG(val);
673 lm85_write_value(client, LM85_REG_TEMP_MAX(nr), data->temp_max[nr]);
674 up(&data->update_lock);
677 #define show_temp_reg(offset) \
678 static ssize_t show_temp_##offset (struct device *dev, char *buf) \
680 return show_temp(dev, buf, 0x##offset - 1); \
682 static ssize_t show_temp_##offset##_min (struct device *dev, char *buf) \
684 return show_temp_min(dev, buf, 0x##offset - 1); \
686 static ssize_t show_temp_##offset##_max (struct device *dev, char *buf) \
688 return show_temp_max(dev, buf, 0x##offset - 1); \
690 static ssize_t set_temp_##offset##_min (struct device *dev, \
691 const char *buf, size_t count) \
693 return set_temp_min(dev, buf, count, 0x##offset - 1); \
695 static ssize_t set_temp_##offset##_max (struct device *dev, \
696 const char *buf, size_t count) \
698 return set_temp_max(dev, buf, count, 0x##offset - 1); \
700 static DEVICE_ATTR(temp##offset##_input, S_IRUGO, show_temp_##offset, NULL); \
701 static DEVICE_ATTR(temp##offset##_min, S_IRUGO | S_IWUSR, \
702 show_temp_##offset##_min, set_temp_##offset##_min); \
703 static DEVICE_ATTR(temp##offset##_max, S_IRUGO | S_IWUSR, \
704 show_temp_##offset##_max, set_temp_##offset##_max);
711 int lm85_attach_adapter(struct i2c_adapter *adapter)
713 return i2c_detect(adapter, &addr_data, lm85_detect);
716 int lm85_detect(struct i2c_adapter *adapter, int address,
719 int company, verstep ;
720 struct i2c_client *new_client = NULL;
721 struct lm85_data *data;
723 const char *type_name = "";
725 if (i2c_is_isa_adapter(adapter)) {
726 /* This chip has no ISA interface */
730 if (!i2c_check_functionality(adapter,
731 I2C_FUNC_SMBUS_BYTE_DATA)) {
732 /* We need to be able to do byte I/O */
736 /* OK. For now, we presume we have a valid client. We now create the
737 client structure, even though we cannot fill it completely yet.
738 But it allows us to access lm85_{read,write}_value. */
740 if (!(data = kmalloc(sizeof(struct lm85_data), GFP_KERNEL))) {
744 memset(data, 0, sizeof(struct lm85_data));
746 new_client = &data->client;
747 i2c_set_clientdata(new_client, data);
748 new_client->addr = address;
749 new_client->adapter = adapter;
750 new_client->driver = &lm85_driver;
751 new_client->flags = 0;
753 /* Now, we do the remaining detection. */
755 company = lm85_read_value(new_client, LM85_REG_COMPANY);
756 verstep = lm85_read_value(new_client, LM85_REG_VERSTEP);
758 dev_dbg(&adapter->dev, "Detecting device at %d,0x%02x with"
759 " COMPANY: 0x%02x and VERSTEP: 0x%02x\n",
760 i2c_adapter_id(new_client->adapter), new_client->addr,
763 /* If auto-detecting, Determine the chip type. */
765 dev_dbg(&adapter->dev, "Autodetecting device at %d,0x%02x ...\n",
766 i2c_adapter_id(adapter), address );
767 if( company == LM85_COMPANY_NATIONAL
768 && verstep == LM85_VERSTEP_LM85C ) {
770 } else if( company == LM85_COMPANY_NATIONAL
771 && verstep == LM85_VERSTEP_LM85B ) {
773 } else if( company == LM85_COMPANY_NATIONAL
774 && (verstep & 0xf0) == LM85_VERSTEP_GENERIC ) {
775 dev_err(&adapter->dev, "Unrecognized version/stepping 0x%02x"
776 " Defaulting to LM85.\n", verstep);
778 } else if( company == LM85_COMPANY_ANALOG_DEV
779 && verstep == LM85_VERSTEP_ADM1027 ) {
781 } else if( company == LM85_COMPANY_ANALOG_DEV
782 && verstep == LM85_VERSTEP_ADT7463 ) {
784 } else if( company == LM85_COMPANY_ANALOG_DEV
785 && (verstep & 0xf0) == LM85_VERSTEP_GENERIC ) {
786 dev_err(&adapter->dev, "Unrecognized version/stepping 0x%02x"
787 " Defaulting to ADM1027.\n", verstep);
789 } else if( kind == 0 && (verstep & 0xf0) == 0x60) {
790 dev_err(&adapter->dev, "Generic LM85 Version 6 detected\n");
791 /* Leave kind as "any_chip" */
793 dev_dbg(&adapter->dev, "Autodetection failed\n");
794 /* Not an LM85 ... */
795 if( kind == 0 ) { /* User used force=x,y */
796 dev_err(&adapter->dev, "Generic LM85 Version 6 not"
797 " found at %d,0x%02x. Try force_lm85c.\n",
798 i2c_adapter_id(adapter), address );
805 /* Fill in the chip specific driver values */
806 if ( kind == any_chip ) {
808 } else if ( kind == lm85b ) {
810 } else if ( kind == lm85c ) {
812 } else if ( kind == adm1027 ) {
813 type_name = "adm1027";
814 } else if ( kind == adt7463 ) {
815 type_name = "adt7463";
817 strlcpy(new_client->name, type_name, I2C_NAME_SIZE);
819 /* Fill in the remaining client fields */
820 new_client->id = lm85_id++;
823 init_MUTEX(&data->update_lock);
825 dev_dbg(&adapter->dev, "Assigning ID %d to %s at %d,0x%02x\n",
826 new_client->id, new_client->name,
827 i2c_adapter_id(new_client->adapter),
830 /* Tell the I2C layer a new client has arrived */
831 if ((err = i2c_attach_client(new_client)))
834 /* Set the VRM version */
835 data->vrm = LM85_INIT_VRM ;
837 /* Initialize the LM85 chip */
838 lm85_init_client(new_client);
840 /* Register sysfs hooks */
841 device_create_file(&new_client->dev, &dev_attr_fan1_input);
842 device_create_file(&new_client->dev, &dev_attr_fan2_input);
843 device_create_file(&new_client->dev, &dev_attr_fan3_input);
844 device_create_file(&new_client->dev, &dev_attr_fan4_input);
845 device_create_file(&new_client->dev, &dev_attr_fan1_min);
846 device_create_file(&new_client->dev, &dev_attr_fan2_min);
847 device_create_file(&new_client->dev, &dev_attr_fan3_min);
848 device_create_file(&new_client->dev, &dev_attr_fan4_min);
849 device_create_file(&new_client->dev, &dev_attr_fan1_pwm);
850 device_create_file(&new_client->dev, &dev_attr_fan2_pwm);
851 device_create_file(&new_client->dev, &dev_attr_fan3_pwm);
852 device_create_file(&new_client->dev, &dev_attr_fan1_pwm_enable);
853 device_create_file(&new_client->dev, &dev_attr_fan2_pwm_enable);
854 device_create_file(&new_client->dev, &dev_attr_fan3_pwm_enable);
855 device_create_file(&new_client->dev, &dev_attr_in0_input);
856 device_create_file(&new_client->dev, &dev_attr_in1_input);
857 device_create_file(&new_client->dev, &dev_attr_in2_input);
858 device_create_file(&new_client->dev, &dev_attr_in3_input);
859 device_create_file(&new_client->dev, &dev_attr_in4_input);
860 device_create_file(&new_client->dev, &dev_attr_in0_min);
861 device_create_file(&new_client->dev, &dev_attr_in1_min);
862 device_create_file(&new_client->dev, &dev_attr_in2_min);
863 device_create_file(&new_client->dev, &dev_attr_in3_min);
864 device_create_file(&new_client->dev, &dev_attr_in4_min);
865 device_create_file(&new_client->dev, &dev_attr_in0_max);
866 device_create_file(&new_client->dev, &dev_attr_in1_max);
867 device_create_file(&new_client->dev, &dev_attr_in2_max);
868 device_create_file(&new_client->dev, &dev_attr_in3_max);
869 device_create_file(&new_client->dev, &dev_attr_in4_max);
870 device_create_file(&new_client->dev, &dev_attr_temp1_input);
871 device_create_file(&new_client->dev, &dev_attr_temp2_input);
872 device_create_file(&new_client->dev, &dev_attr_temp3_input);
873 device_create_file(&new_client->dev, &dev_attr_temp1_min);
874 device_create_file(&new_client->dev, &dev_attr_temp2_min);
875 device_create_file(&new_client->dev, &dev_attr_temp3_min);
876 device_create_file(&new_client->dev, &dev_attr_temp1_max);
877 device_create_file(&new_client->dev, &dev_attr_temp2_max);
878 device_create_file(&new_client->dev, &dev_attr_temp3_max);
879 device_create_file(&new_client->dev, &dev_attr_vrm);
880 device_create_file(&new_client->dev, &dev_attr_in0_ref);
881 device_create_file(&new_client->dev, &dev_attr_alarms);
885 /* Error out and cleanup code */
892 int lm85_detach_client(struct i2c_client *client)
894 i2c_detach_client(client);
895 kfree(i2c_get_clientdata(client));
900 int lm85_read_value(struct i2c_client *client, u8 reg)
904 /* What size location is it? */
906 case LM85_REG_FAN(0) : /* Read WORD data */
907 case LM85_REG_FAN(1) :
908 case LM85_REG_FAN(2) :
909 case LM85_REG_FAN(3) :
910 case LM85_REG_FAN_MIN(0) :
911 case LM85_REG_FAN_MIN(1) :
912 case LM85_REG_FAN_MIN(2) :
913 case LM85_REG_FAN_MIN(3) :
914 case LM85_REG_ALARM1 : /* Read both bytes at once */
915 case ADM1027_REG_EXTEND_ADC1 : /* Read two bytes at once */
916 res = i2c_smbus_read_byte_data(client, reg) & 0xff ;
917 res |= i2c_smbus_read_byte_data(client, reg+1) << 8 ;
919 case ADT7463_REG_TMIN_CTL1 : /* Read WORD MSB, LSB */
920 res = i2c_smbus_read_byte_data(client, reg) << 8 ;
921 res |= i2c_smbus_read_byte_data(client, reg+1) & 0xff ;
923 default: /* Read BYTE data */
924 res = i2c_smbus_read_byte_data(client, reg);
931 int lm85_write_value(struct i2c_client *client, u8 reg, int value)
936 case LM85_REG_FAN(0) : /* Write WORD data */
937 case LM85_REG_FAN(1) :
938 case LM85_REG_FAN(2) :
939 case LM85_REG_FAN(3) :
940 case LM85_REG_FAN_MIN(0) :
941 case LM85_REG_FAN_MIN(1) :
942 case LM85_REG_FAN_MIN(2) :
943 case LM85_REG_FAN_MIN(3) :
944 /* NOTE: ALARM is read only, so not included here */
945 res = i2c_smbus_write_byte_data(client, reg, value & 0xff) ;
946 res |= i2c_smbus_write_byte_data(client, reg+1, (value>>8) & 0xff) ;
948 case ADT7463_REG_TMIN_CTL1 : /* Write WORD MSB, LSB */
949 res = i2c_smbus_write_byte_data(client, reg, (value>>8) & 0xff);
950 res |= i2c_smbus_write_byte_data(client, reg+1, value & 0xff) ;
952 default: /* Write BYTE data */
953 res = i2c_smbus_write_byte_data(client, reg, value);
960 void lm85_init_client(struct i2c_client *client)
963 struct lm85_data *data = i2c_get_clientdata(client);
965 dev_dbg(&client->dev, "Initializing device\n");
967 /* Warn if part was not "READY" */
968 value = lm85_read_value(client, LM85_REG_CONFIG);
969 dev_dbg(&client->dev, "LM85_REG_CONFIG is: 0x%02x\n", value);
971 dev_err(&client->dev, "Client (%d,0x%02x) config is locked.\n",
972 i2c_adapter_id(client->adapter), client->addr );
974 if( ! (value & 0x04) ) {
975 dev_err(&client->dev, "Client (%d,0x%02x) is not ready.\n",
976 i2c_adapter_id(client->adapter), client->addr );
979 && ( data->type == adm1027
980 || data->type == adt7463 ) ) {
981 dev_err(&client->dev, "Client (%d,0x%02x) VxI mode is set. "
982 "Please report this to the lm85 maintainer.\n",
983 i2c_adapter_id(client->adapter), client->addr );
986 /* WE INTENTIONALLY make no changes to the limits,
987 * offsets, pwms, fans and zones. If they were
988 * configured, we don't want to mess with them.
989 * If they weren't, the default is 100% PWM, no
990 * control and will suffice until 'sensors -s'
991 * can be run by the user.
994 /* Start monitoring */
995 value = lm85_read_value(client, LM85_REG_CONFIG);
996 /* Try to clear LOCK, Set START, save everything else */
997 value = (value & ~ 0x02) | 0x01 ;
998 dev_dbg(&client->dev, "Setting CONFIG to: 0x%02x\n", value);
999 lm85_write_value(client, LM85_REG_CONFIG, value);
1002 static struct lm85_data *lm85_update_device(struct device *dev)
1004 struct i2c_client *client = to_i2c_client(dev);
1005 struct lm85_data *data = i2c_get_clientdata(client);
1008 down(&data->update_lock);
1010 if ( !data->valid ||
1011 (jiffies - data->last_reading > LM85_DATA_INTERVAL ) ) {
1012 /* Things that change quickly */
1013 dev_dbg(&client->dev, "Reading sensor values\n");
1015 /* Have to read extended bits first to "freeze" the
1016 * more significant bits that are read later.
1018 if ( (data->type == adm1027) || (data->type == adt7463) ) {
1020 lm85_read_value(client, ADM1027_REG_EXTEND_ADC1);
1023 for (i = 0; i <= 4; ++i) {
1025 lm85_read_value(client, LM85_REG_IN(i));
1028 for (i = 0; i <= 3; ++i) {
1030 lm85_read_value(client, LM85_REG_FAN(i));
1033 for (i = 0; i <= 2; ++i) {
1035 lm85_read_value(client, LM85_REG_TEMP(i));
1038 for (i = 0; i <= 2; ++i) {
1040 lm85_read_value(client, LM85_REG_PWM(i));
1043 if ( data->type == adt7463 ) {
1044 if( data->therm_total < ULONG_MAX - 256 ) {
1045 data->therm_total +=
1046 lm85_read_value(client, ADT7463_REG_THERM );
1050 data->alarms = lm85_read_value(client, LM85_REG_ALARM1);
1052 data->last_reading = jiffies ;
1053 }; /* last_reading */
1055 if ( !data->valid ||
1056 (jiffies - data->last_config > LM85_CONFIG_INTERVAL) ) {
1057 /* Things that don't change often */
1058 dev_dbg(&client->dev, "Reading config values\n");
1060 for (i = 0; i <= 4; ++i) {
1062 lm85_read_value(client, LM85_REG_IN_MIN(i));
1064 lm85_read_value(client, LM85_REG_IN_MAX(i));
1067 for (i = 0; i <= 3; ++i) {
1069 lm85_read_value(client, LM85_REG_FAN_MIN(i));
1072 for (i = 0; i <= 2; ++i) {
1074 lm85_read_value(client, LM85_REG_TEMP_MIN(i));
1076 lm85_read_value(client, LM85_REG_TEMP_MAX(i));
1079 data->vid = lm85_read_value(client, LM85_REG_VID);
1081 for (i = 0; i <= 2; ++i) {
1083 data->autofan[i].config =
1084 lm85_read_value(client, LM85_REG_AFAN_CONFIG(i));
1085 val = lm85_read_value(client, LM85_REG_AFAN_RANGE(i));
1086 data->autofan[i].freq = val & 0x07 ;
1087 data->zone[i].range = (val >> 4) & 0x0f ;
1088 data->autofan[i].min_pwm =
1089 lm85_read_value(client, LM85_REG_AFAN_MINPWM(i));
1090 data->zone[i].limit =
1091 lm85_read_value(client, LM85_REG_AFAN_LIMIT(i));
1092 data->zone[i].critical =
1093 lm85_read_value(client, LM85_REG_AFAN_CRITICAL(i));
1096 i = lm85_read_value(client, LM85_REG_AFAN_SPIKE1);
1097 data->smooth[0] = i & 0x0f ;
1098 data->syncpwm3 = i & 0x10 ; /* Save PWM3 config */
1099 data->autofan[0].min_off = (i & 0x20) != 0 ;
1100 data->autofan[1].min_off = (i & 0x40) != 0 ;
1101 data->autofan[2].min_off = (i & 0x80) != 0 ;
1102 i = lm85_read_value(client, LM85_REG_AFAN_SPIKE2);
1103 data->smooth[1] = (i>>4) & 0x0f ;
1104 data->smooth[2] = i & 0x0f ;
1106 i = lm85_read_value(client, LM85_REG_AFAN_HYST1);
1107 data->zone[0].hyst = (i>>4) & 0x0f ;
1108 data->zone[1].hyst = i & 0x0f ;
1110 i = lm85_read_value(client, LM85_REG_AFAN_HYST2);
1111 data->zone[2].hyst = (i>>4) & 0x0f ;
1113 if ( (data->type == lm85b) || (data->type == lm85c) ) {
1114 data->tach_mode = lm85_read_value(client,
1115 LM85_REG_TACH_MODE );
1116 data->spinup_ctl = lm85_read_value(client,
1117 LM85_REG_SPINUP_CTL );
1118 } else if ( (data->type == adt7463) || (data->type == adm1027) ) {
1119 if ( data->type == adt7463 ) {
1120 for (i = 0; i <= 2; ++i) {
1121 data->oppoint[i] = lm85_read_value(client,
1122 ADT7463_REG_OPPOINT(i) );
1124 data->tmin_ctl = lm85_read_value(client,
1125 ADT7463_REG_TMIN_CTL1 );
1126 data->therm_limit = lm85_read_value(client,
1127 ADT7463_REG_THERM_LIMIT );
1129 for (i = 0; i <= 2; ++i) {
1130 data->temp_offset[i] = lm85_read_value(client,
1131 ADM1027_REG_TEMP_OFFSET(i) );
1133 data->tach_mode = lm85_read_value(client,
1134 ADM1027_REG_CONFIG3 );
1135 data->fan_ppr = lm85_read_value(client,
1136 ADM1027_REG_FAN_PPR );
1139 data->last_config = jiffies;
1140 }; /* last_config */
1144 up(&data->update_lock);
1150 static int __init sm_lm85_init(void)
1152 return i2c_add_driver(&lm85_driver);
1155 static void __exit sm_lm85_exit(void)
1157 i2c_del_driver(&lm85_driver);
1160 /* Thanks to Richard Barrington for adding the LM85 to sensors-detect.
1161 * Thanks to Margit Schubert-While <margitsw@t-online.de> for help with
1162 * post 2.7.0 CVS changes.
1164 MODULE_LICENSE("GPL");
1165 MODULE_AUTHOR("Philip Pokorny <ppokorny@penguincomputing.com>, Margit Schubert-While <margitsw@t-online.de>");
1166 MODULE_DESCRIPTION("LM85-B, LM85-C driver");
1168 module_init(sm_lm85_init);
1169 module_exit(sm_lm85_exit);