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>
7 Copyright (c) 2004 Justin Thiessen <jthiessen@penguincomputing.com>
9 Chip details at <http://www.national.com/ds/LM/LM85.pdf>
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.
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.
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.
26 #include <linux/config.h>
27 #include <linux/module.h>
28 #include <linux/init.h>
29 #include <linux/slab.h>
30 #include <linux/i2c.h>
31 #include <linux/i2c-sensor.h>
32 #include <linux/i2c-vid.h>
34 /* Addresses to scan */
35 static unsigned short normal_i2c[] = { 0x2c, 0x2d, 0x2e, I2C_CLIENT_END };
36 static unsigned int normal_isa[] = { I2C_CLIENT_ISA_END };
38 /* Insmod parameters */
39 SENSORS_INSMOD_4(lm85b, lm85c, adm1027, adt7463);
41 /* The LM85 registers */
43 #define LM85_REG_IN(nr) (0x20 + (nr))
44 #define LM85_REG_IN_MIN(nr) (0x44 + (nr) * 2)
45 #define LM85_REG_IN_MAX(nr) (0x45 + (nr) * 2)
47 #define LM85_REG_TEMP(nr) (0x25 + (nr))
48 #define LM85_REG_TEMP_MIN(nr) (0x4e + (nr) * 2)
49 #define LM85_REG_TEMP_MAX(nr) (0x4f + (nr) * 2)
51 /* Fan speeds are LSB, MSB (2 bytes) */
52 #define LM85_REG_FAN(nr) (0x28 + (nr) *2)
53 #define LM85_REG_FAN_MIN(nr) (0x54 + (nr) *2)
55 #define LM85_REG_PWM(nr) (0x30 + (nr))
57 #define ADT7463_REG_OPPOINT(nr) (0x33 + (nr))
59 #define ADT7463_REG_TMIN_CTL1 0x36
60 #define ADT7463_REG_TMIN_CTL2 0x37
62 #define LM85_REG_DEVICE 0x3d
63 #define LM85_REG_COMPANY 0x3e
64 #define LM85_REG_VERSTEP 0x3f
65 /* These are the recognized values for the above regs */
66 #define LM85_DEVICE_ADX 0x27
67 #define LM85_COMPANY_NATIONAL 0x01
68 #define LM85_COMPANY_ANALOG_DEV 0x41
69 #define LM85_VERSTEP_GENERIC 0x60
70 #define LM85_VERSTEP_LM85C 0x60
71 #define LM85_VERSTEP_LM85B 0x62
72 #define LM85_VERSTEP_ADM1027 0x60
73 #define LM85_VERSTEP_ADT7463 0x62
75 #define LM85_REG_CONFIG 0x40
77 #define LM85_REG_ALARM1 0x41
78 #define LM85_REG_ALARM2 0x42
80 #define LM85_REG_VID 0x43
82 /* Automated FAN control */
83 #define LM85_REG_AFAN_CONFIG(nr) (0x5c + (nr))
84 #define LM85_REG_AFAN_RANGE(nr) (0x5f + (nr))
85 #define LM85_REG_AFAN_SPIKE1 0x62
86 #define LM85_REG_AFAN_SPIKE2 0x63
87 #define LM85_REG_AFAN_MINPWM(nr) (0x64 + (nr))
88 #define LM85_REG_AFAN_LIMIT(nr) (0x67 + (nr))
89 #define LM85_REG_AFAN_CRITICAL(nr) (0x6a + (nr))
90 #define LM85_REG_AFAN_HYST1 0x6d
91 #define LM85_REG_AFAN_HYST2 0x6e
93 #define LM85_REG_TACH_MODE 0x74
94 #define LM85_REG_SPINUP_CTL 0x75
96 #define ADM1027_REG_TEMP_OFFSET(nr) (0x70 + (nr))
97 #define ADM1027_REG_CONFIG2 0x73
98 #define ADM1027_REG_INTMASK1 0x74
99 #define ADM1027_REG_INTMASK2 0x75
100 #define ADM1027_REG_EXTEND_ADC1 0x76
101 #define ADM1027_REG_EXTEND_ADC2 0x77
102 #define ADM1027_REG_CONFIG3 0x78
103 #define ADM1027_REG_FAN_PPR 0x7b
105 #define ADT7463_REG_THERM 0x79
106 #define ADT7463_REG_THERM_LIMIT 0x7A
108 #define LM85_ALARM_IN0 0x0001
109 #define LM85_ALARM_IN1 0x0002
110 #define LM85_ALARM_IN2 0x0004
111 #define LM85_ALARM_IN3 0x0008
112 #define LM85_ALARM_TEMP1 0x0010
113 #define LM85_ALARM_TEMP2 0x0020
114 #define LM85_ALARM_TEMP3 0x0040
115 #define LM85_ALARM_ALARM2 0x0080
116 #define LM85_ALARM_IN4 0x0100
117 #define LM85_ALARM_RESERVED 0x0200
118 #define LM85_ALARM_FAN1 0x0400
119 #define LM85_ALARM_FAN2 0x0800
120 #define LM85_ALARM_FAN3 0x1000
121 #define LM85_ALARM_FAN4 0x2000
122 #define LM85_ALARM_TEMP1_FAULT 0x4000
123 #define LM85_ALARM_TEMP3_FAULT 0x8000
126 /* Conversions. Rounding and limit checking is only done on the TO_REG
127 variants. Note that you should be a bit careful with which arguments
128 these macros are called: arguments may be evaluated more than once.
131 /* IN are scaled 1.000 == 0xc0, mag = 3 */
132 #define IN_TO_REG(val) (SENSORS_LIMIT((((val)*0xc0+500)/1000),0,255))
133 #define INEXT_FROM_REG(val,ext) (((val)*1000 + (ext)*250 + 96)/0xc0)
134 #define IN_FROM_REG(val) (INEXT_FROM_REG(val,0))
136 /* IN are scaled acording to built-in resistors */
137 static int lm85_scaling[] = { /* .001 Volts */
138 2500, 2250, 3300, 5000, 12000
140 #define SCALE(val,from,to) (((val)*(to) + ((from)/2))/(from))
141 #define INS_TO_REG(n,val) (SENSORS_LIMIT(SCALE(val,lm85_scaling[n],192),0,255))
142 #define INSEXT_FROM_REG(n,val,ext) (SCALE((val)*4 + (ext),192*4,lm85_scaling[n]))
143 #define INS_FROM_REG(n,val) (INSEXT_FROM_REG(n,val,0))
145 /* FAN speed is measured using 90kHz clock */
146 #define FAN_TO_REG(val) (SENSORS_LIMIT( (val)<=0?0: 5400000/(val),0,65534))
147 #define FAN_FROM_REG(val) ((val)==0?-1:(val)==0xffff?0:5400000/(val))
149 /* Temperature is reported in .001 degC increments */
150 #define TEMP_TO_REG(val) (SENSORS_LIMIT(((val)+500)/1000,-127,127))
151 #define TEMPEXT_FROM_REG(val,ext) ((val)*1000 + (ext)*250)
152 #define TEMP_FROM_REG(val) (TEMPEXT_FROM_REG(val,0))
153 #define EXTTEMP_TO_REG(val) (SENSORS_LIMIT((val)/250,-127,127))
155 #define PWM_TO_REG(val) (SENSORS_LIMIT(val,0,255))
156 #define PWM_FROM_REG(val) (val)
158 #define EXT_FROM_REG(val,sensor) (((val)>>(sensor * 2))&0x03)
160 /* ZONEs have the following parameters:
161 * Limit (low) temp, 1. degC
162 * Hysteresis (below limit), 1. degC (0-15)
163 * Range of speed control, .1 degC (2-80)
164 * Critical (high) temp, 1. degC
166 * FAN PWMs have the following parameters:
167 * Reference Zone, 1, 2, 3, etc.
168 * Spinup time, .05 sec
169 * PWM value at limit/low temp, 1 count
170 * PWM Frequency, 1. Hz
171 * PWM is Min or OFF below limit, flag
172 * Invert PWM output, flag
174 * Some chips filter the temp, others the fan.
175 * Filter constant (or disabled) .1 seconds
178 /* These are the zone temperature range encodings in .001 degree C */
179 static int lm85_range_map[] = {
180 2000, 2500, 3300, 4000, 5000, 6600,
181 8000, 10000, 13300, 16000, 20000, 26600,
182 32000, 40000, 53300, 80000
184 static int RANGE_TO_REG( int range )
188 if ( range < lm85_range_map[0] ) {
190 } else if ( range > lm85_range_map[15] ) {
192 } else { /* find closest match */
193 for ( i = 14 ; i >= 0 ; --i ) {
194 if ( range > lm85_range_map[i] ) { /* range bracketed */
195 if ((lm85_range_map[i+1] - range) <
196 (range - lm85_range_map[i])) {
206 #define RANGE_FROM_REG(val) (lm85_range_map[(val)&0x0f])
208 /* These are the Acoustic Enhancement, or Temperature smoothing encodings
209 * NOTE: The enable/disable bit is INCLUDED in these encodings as the
210 * MSB (bit 3, value 8). If the enable bit is 0, the encoded value
211 * is ignored, or set to 0.
213 /* These are the PWM frequency encodings */
214 static int lm85_freq_map[] = { /* .1 Hz */
215 100, 150, 230, 300, 380, 470, 620, 940
217 static int FREQ_TO_REG( int freq )
221 if( freq >= lm85_freq_map[7] ) { return 7 ; }
222 for( i = 0 ; i < 7 ; ++i )
223 if( freq <= lm85_freq_map[i] )
227 #define FREQ_FROM_REG(val) (lm85_freq_map[(val)&0x07])
229 /* Since we can't use strings, I'm abusing these numbers
230 * to stand in for the following meanings:
231 * 1 -- PWM responds to Zone 1
232 * 2 -- PWM responds to Zone 2
233 * 3 -- PWM responds to Zone 3
234 * 23 -- PWM responds to the higher temp of Zone 2 or 3
235 * 123 -- PWM responds to highest of Zone 1, 2, or 3
236 * 0 -- PWM is always at 0% (ie, off)
237 * -1 -- PWM is always at 100%
238 * -2 -- PWM responds to manual control
241 static int lm85_zone_map[] = { 1, 2, 3, -1, 0, 23, 123, -2 };
242 #define ZONE_FROM_REG(val) (lm85_zone_map[((val)>>5)&0x07])
244 static int ZONE_TO_REG( int zone )
248 for( i = 0 ; i <= 7 ; ++i )
249 if( zone == lm85_zone_map[i] )
251 if( i > 7 ) /* Not found. */
252 i = 3; /* Always 100% */
253 return( (i & 0x07)<<5 );
256 #define HYST_TO_REG(val) (SENSORS_LIMIT(((val)+500)/1000,0,15))
257 #define HYST_FROM_REG(val) ((val)*1000)
259 #define OFFSET_TO_REG(val) (SENSORS_LIMIT((val)/25,-127,127))
260 #define OFFSET_FROM_REG(val) ((val)*25)
262 #define PPR_MASK(fan) (0x03<<(fan *2))
263 #define PPR_TO_REG(val,fan) (SENSORS_LIMIT((val)-1,0,3)<<(fan *2))
264 #define PPR_FROM_REG(val,fan) ((((val)>>(fan * 2))&0x03)+1)
266 /* i2c-vid.h defines vid_from_reg() */
267 #define VID_FROM_REG(val,vrm) (vid_from_reg((val),(vrm)))
269 #define ALARMS_FROM_REG(val) (val)
271 /* Unlike some other drivers we DO NOT set initial limits. Use
272 * the config file to set limits. Some users have reported
273 * motherboards shutting down when we set limits in a previous
274 * version of the driver.
277 /* Chip sampling rates
279 * Some sensors are not updated more frequently than once per second
280 * so it doesn't make sense to read them more often than that.
281 * We cache the results and return the saved data if the driver
282 * is called again before a second has elapsed.
284 * Also, there is significant configuration data for this chip
285 * given the automatic PWM fan control that is possible. There
286 * are about 47 bytes of config data to only 22 bytes of actual
287 * readings. So, we keep the config data up to date in the cache
288 * when it is written and only sample it once every 1 *minute*
290 #define LM85_DATA_INTERVAL (HZ + HZ / 2)
291 #define LM85_CONFIG_INTERVAL (1 * 60 * HZ)
293 /* For each registered LM85, we need to keep some data in memory. That
294 data is pointed to by lm85_list[NR]->data. The structure itself is
295 dynamically allocated, at the same time when a new lm85 client is
298 /* LM85 can automatically adjust fan speeds based on temperature
299 * This structure encapsulates an entire Zone config. There are
300 * three zones (one for each temperature input) on the lm85
303 s8 limit; /* Low temp limit */
304 u8 hyst; /* Low limit hysteresis. (0-15) */
305 u8 range; /* Temp range, encoded */
306 s8 critical; /* "All fans ON" temp limit */
307 u8 off_desired; /* Actual "off" temperature specified. Preserved
308 * to prevent "drift" as other autofan control
311 u8 max_desired; /* Actual "max" temperature specified. Preserved
312 * to prevent "drift" as other autofan control
317 struct lm85_autofan {
318 u8 config; /* Register value */
319 u8 freq; /* PWM frequency, encoded */
320 u8 min_pwm; /* Minimum PWM value, encoded */
321 u8 min_off; /* Min PWM or OFF below "limit", flag */
325 struct i2c_client client;
326 struct semaphore lock;
329 struct semaphore update_lock;
330 int valid; /* !=0 if following fields are valid */
331 unsigned long last_reading; /* In jiffies */
332 unsigned long last_config; /* In jiffies */
334 u8 in[5]; /* Register value */
335 u8 in_max[5]; /* Register value */
336 u8 in_min[5]; /* Register value */
337 s8 temp[3]; /* Register value */
338 s8 temp_min[3]; /* Register value */
339 s8 temp_max[3]; /* Register value */
340 s8 temp_offset[3]; /* Register value */
341 u16 fan[4]; /* Register value */
342 u16 fan_min[4]; /* Register value */
343 u8 pwm[3]; /* Register value */
344 u8 spinup_ctl; /* Register encoding, combined */
345 u8 tach_mode; /* Register encoding, combined */
346 u16 extend_adc; /* Register value */
347 u8 fan_ppr; /* Register value */
348 u8 smooth[3]; /* Register encoding */
349 u8 vid; /* Register value */
350 u8 vrm; /* VRM version */
351 u8 syncpwm3; /* Saved PWM3 for TACH 2,3,4 config */
352 u8 oppoint[3]; /* Register value */
353 u16 tmin_ctl; /* Register value */
354 unsigned long therm_total; /* Cummulative therm count */
355 u8 therm_limit; /* Register value */
356 u16 alarms; /* Register encoding, combined */
357 struct lm85_autofan autofan[3];
358 struct lm85_zone zone[3];
361 static int lm85_attach_adapter(struct i2c_adapter *adapter);
362 static int lm85_detect(struct i2c_adapter *adapter, int address,
364 static int lm85_detach_client(struct i2c_client *client);
366 static int lm85_read_value(struct i2c_client *client, u8 register);
367 static int lm85_write_value(struct i2c_client *client, u8 register, int value);
368 static struct lm85_data *lm85_update_device(struct device *dev);
369 static void lm85_init_client(struct i2c_client *client);
372 static struct i2c_driver lm85_driver = {
373 .owner = THIS_MODULE,
375 .id = I2C_DRIVERID_LM85,
376 .flags = I2C_DF_NOTIFY,
377 .attach_adapter = lm85_attach_adapter,
378 .detach_client = lm85_detach_client,
381 /* Unique ID assigned to each LM85 detected */
386 static ssize_t show_fan(struct device *dev, char *buf, int nr)
388 struct lm85_data *data = lm85_update_device(dev);
389 return sprintf(buf,"%d\n", FAN_FROM_REG(data->fan[nr]) );
391 static ssize_t show_fan_min(struct device *dev, char *buf, int nr)
393 struct lm85_data *data = lm85_update_device(dev);
394 return sprintf(buf,"%d\n", FAN_FROM_REG(data->fan_min[nr]) );
396 static ssize_t set_fan_min(struct device *dev, const char *buf,
397 size_t count, int nr)
399 struct i2c_client *client = to_i2c_client(dev);
400 struct lm85_data *data = i2c_get_clientdata(client);
403 down(&data->update_lock);
404 val = simple_strtol(buf, NULL, 10);
405 data->fan_min[nr] = FAN_TO_REG(val);
406 lm85_write_value(client, LM85_REG_FAN_MIN(nr), data->fan_min[nr]);
407 up(&data->update_lock);
411 #define show_fan_offset(offset) \
412 static ssize_t show_fan_##offset (struct device *dev, char *buf) \
414 return show_fan(dev, buf, offset - 1); \
416 static ssize_t show_fan_##offset##_min (struct device *dev, char *buf) \
418 return show_fan_min(dev, buf, offset - 1); \
420 static ssize_t set_fan_##offset##_min (struct device *dev, \
421 const char *buf, size_t count) \
423 return set_fan_min(dev, buf, count, offset - 1); \
425 static DEVICE_ATTR(fan##offset##_input, S_IRUGO, show_fan_##offset, \
427 static DEVICE_ATTR(fan##offset##_min, S_IRUGO | S_IWUSR, \
428 show_fan_##offset##_min, set_fan_##offset##_min);
435 /* vid, vrm, alarms */
437 static ssize_t show_vid_reg(struct device *dev, char *buf)
439 struct lm85_data *data = lm85_update_device(dev);
440 return sprintf(buf, "%ld\n", (long) vid_from_reg(data->vid, data->vrm));
443 static DEVICE_ATTR(cpu0_vid, S_IRUGO, show_vid_reg, NULL);
445 static ssize_t show_vrm_reg(struct device *dev, char *buf)
447 struct lm85_data *data = lm85_update_device(dev);
448 return sprintf(buf, "%ld\n", (long) data->vrm);
451 static ssize_t store_vrm_reg(struct device *dev, const char *buf, size_t count)
453 struct i2c_client *client = to_i2c_client(dev);
454 struct lm85_data *data = i2c_get_clientdata(client);
457 val = simple_strtoul(buf, NULL, 10);
462 static DEVICE_ATTR(vrm, S_IRUGO | S_IWUSR, show_vrm_reg, store_vrm_reg);
464 static ssize_t show_alarms_reg(struct device *dev, char *buf)
466 struct lm85_data *data = lm85_update_device(dev);
467 return sprintf(buf, "%ld\n", (long) ALARMS_FROM_REG(data->alarms));
470 static DEVICE_ATTR(alarms, S_IRUGO, show_alarms_reg, NULL);
474 static ssize_t show_pwm(struct device *dev, char *buf, int nr)
476 struct lm85_data *data = lm85_update_device(dev);
477 return sprintf(buf,"%d\n", PWM_FROM_REG(data->pwm[nr]) );
479 static ssize_t set_pwm(struct device *dev, const char *buf,
480 size_t count, int nr)
482 struct i2c_client *client = to_i2c_client(dev);
483 struct lm85_data *data = i2c_get_clientdata(client);
486 down(&data->update_lock);
487 val = simple_strtol(buf, NULL, 10);
488 data->pwm[nr] = PWM_TO_REG(val);
489 lm85_write_value(client, LM85_REG_PWM(nr), data->pwm[nr]);
490 up(&data->update_lock);
493 static ssize_t show_pwm_enable(struct device *dev, char *buf, int nr)
495 struct lm85_data *data = lm85_update_device(dev);
498 pwm_zone = ZONE_FROM_REG(data->autofan[nr].config);
499 return sprintf(buf,"%d\n", (pwm_zone != 0 && pwm_zone != -1) );
502 #define show_pwm_reg(offset) \
503 static ssize_t show_pwm_##offset (struct device *dev, char *buf) \
505 return show_pwm(dev, buf, offset - 1); \
507 static ssize_t set_pwm_##offset (struct device *dev, \
508 const char *buf, size_t count) \
510 return set_pwm(dev, buf, count, offset - 1); \
512 static ssize_t show_pwm_enable##offset (struct device *dev, char *buf) \
514 return show_pwm_enable(dev, buf, offset - 1); \
516 static DEVICE_ATTR(pwm##offset, S_IRUGO | S_IWUSR, \
517 show_pwm_##offset, set_pwm_##offset); \
518 static DEVICE_ATTR(pwm##offset##_enable, S_IRUGO, \
519 show_pwm_enable##offset, NULL);
527 static ssize_t show_in(struct device *dev, char *buf, int nr)
529 struct lm85_data *data = lm85_update_device(dev);
530 return sprintf(buf,"%d\n", INS_FROM_REG(nr, data->in[nr]) );
532 static ssize_t show_in_min(struct device *dev, char *buf, int nr)
534 struct lm85_data *data = lm85_update_device(dev);
535 return sprintf(buf,"%d\n", INS_FROM_REG(nr, data->in_min[nr]) );
537 static ssize_t set_in_min(struct device *dev, const char *buf,
538 size_t count, int nr)
540 struct i2c_client *client = to_i2c_client(dev);
541 struct lm85_data *data = i2c_get_clientdata(client);
544 down(&data->update_lock);
545 val = simple_strtol(buf, NULL, 10);
546 data->in_min[nr] = INS_TO_REG(nr, val);
547 lm85_write_value(client, LM85_REG_IN_MIN(nr), data->in_min[nr]);
548 up(&data->update_lock);
551 static ssize_t show_in_max(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_max[nr]) );
556 static ssize_t set_in_max(struct device *dev, const char *buf,
557 size_t count, int nr)
559 struct i2c_client *client = to_i2c_client(dev);
560 struct lm85_data *data = i2c_get_clientdata(client);
563 down(&data->update_lock);
564 val = simple_strtol(buf, NULL, 10);
565 data->in_max[nr] = INS_TO_REG(nr, val);
566 lm85_write_value(client, LM85_REG_IN_MAX(nr), data->in_max[nr]);
567 up(&data->update_lock);
570 #define show_in_reg(offset) \
571 static ssize_t show_in_##offset (struct device *dev, char *buf) \
573 return show_in(dev, buf, offset); \
575 static ssize_t show_in_##offset##_min (struct device *dev, char *buf) \
577 return show_in_min(dev, buf, offset); \
579 static ssize_t show_in_##offset##_max (struct device *dev, char *buf) \
581 return show_in_max(dev, buf, offset); \
583 static ssize_t set_in_##offset##_min (struct device *dev, \
584 const char *buf, size_t count) \
586 return set_in_min(dev, buf, count, offset); \
588 static ssize_t set_in_##offset##_max (struct device *dev, \
589 const char *buf, size_t count) \
591 return set_in_max(dev, buf, count, offset); \
593 static DEVICE_ATTR(in##offset##_input, S_IRUGO, show_in_##offset, \
595 static DEVICE_ATTR(in##offset##_min, S_IRUGO | S_IWUSR, \
596 show_in_##offset##_min, set_in_##offset##_min); \
597 static DEVICE_ATTR(in##offset##_max, S_IRUGO | S_IWUSR, \
598 show_in_##offset##_max, set_in_##offset##_max);
608 static ssize_t show_temp(struct device *dev, char *buf, int nr)
610 struct lm85_data *data = lm85_update_device(dev);
611 return sprintf(buf,"%d\n", TEMP_FROM_REG(data->temp[nr]) );
613 static ssize_t show_temp_min(struct device *dev, char *buf, int nr)
615 struct lm85_data *data = lm85_update_device(dev);
616 return sprintf(buf,"%d\n", TEMP_FROM_REG(data->temp_min[nr]) );
618 static ssize_t set_temp_min(struct device *dev, const char *buf,
619 size_t count, int nr)
621 struct i2c_client *client = to_i2c_client(dev);
622 struct lm85_data *data = i2c_get_clientdata(client);
625 down(&data->update_lock);
626 val = simple_strtol(buf, NULL, 10);
627 data->temp_min[nr] = TEMP_TO_REG(val);
628 lm85_write_value(client, LM85_REG_TEMP_MIN(nr), data->temp_min[nr]);
629 up(&data->update_lock);
632 static ssize_t show_temp_max(struct device *dev, char *buf, int nr)
634 struct lm85_data *data = lm85_update_device(dev);
635 return sprintf(buf,"%d\n", TEMP_FROM_REG(data->temp_max[nr]) );
637 static ssize_t set_temp_max(struct device *dev, const char *buf,
638 size_t count, int nr)
640 struct i2c_client *client = to_i2c_client(dev);
641 struct lm85_data *data = i2c_get_clientdata(client);
644 down(&data->update_lock);
645 val = simple_strtol(buf, NULL, 10);
646 data->temp_max[nr] = TEMP_TO_REG(val);
647 lm85_write_value(client, LM85_REG_TEMP_MAX(nr), data->temp_max[nr]);
648 up(&data->update_lock);
651 #define show_temp_reg(offset) \
652 static ssize_t show_temp_##offset (struct device *dev, char *buf) \
654 return show_temp(dev, buf, offset - 1); \
656 static ssize_t show_temp_##offset##_min (struct device *dev, char *buf) \
658 return show_temp_min(dev, buf, offset - 1); \
660 static ssize_t show_temp_##offset##_max (struct device *dev, char *buf) \
662 return show_temp_max(dev, buf, offset - 1); \
664 static ssize_t set_temp_##offset##_min (struct device *dev, \
665 const char *buf, size_t count) \
667 return set_temp_min(dev, buf, count, offset - 1); \
669 static ssize_t set_temp_##offset##_max (struct device *dev, \
670 const char *buf, size_t count) \
672 return set_temp_max(dev, buf, count, offset - 1); \
674 static DEVICE_ATTR(temp##offset##_input, S_IRUGO, show_temp_##offset, \
676 static DEVICE_ATTR(temp##offset##_min, S_IRUGO | S_IWUSR, \
677 show_temp_##offset##_min, set_temp_##offset##_min); \
678 static DEVICE_ATTR(temp##offset##_max, S_IRUGO | S_IWUSR, \
679 show_temp_##offset##_max, set_temp_##offset##_max);
686 /* Automatic PWM control */
688 static ssize_t show_pwm_auto_channels(struct device *dev, char *buf, int nr)
690 struct lm85_data *data = lm85_update_device(dev);
691 return sprintf(buf,"%d\n", ZONE_FROM_REG(data->autofan[nr].config));
693 static ssize_t set_pwm_auto_channels(struct device *dev, const char *buf,
694 size_t count, int nr)
696 struct i2c_client *client = to_i2c_client(dev);
697 struct lm85_data *data = i2c_get_clientdata(client);
700 down(&data->update_lock);
701 val = simple_strtol(buf, NULL, 10);
702 data->autofan[nr].config = (data->autofan[nr].config & (~0xe0))
704 lm85_write_value(client, LM85_REG_AFAN_CONFIG(nr),
705 data->autofan[nr].config);
706 up(&data->update_lock);
709 static ssize_t show_pwm_auto_pwm_min(struct device *dev, char *buf, int nr)
711 struct lm85_data *data = lm85_update_device(dev);
712 return sprintf(buf,"%d\n", PWM_FROM_REG(data->autofan[nr].min_pwm));
714 static ssize_t set_pwm_auto_pwm_min(struct device *dev, const char *buf,
715 size_t count, int nr)
717 struct i2c_client *client = to_i2c_client(dev);
718 struct lm85_data *data = i2c_get_clientdata(client);
721 down(&data->update_lock);
722 val = simple_strtol(buf, NULL, 10);
723 data->autofan[nr].min_pwm = PWM_TO_REG(val);
724 lm85_write_value(client, LM85_REG_AFAN_MINPWM(nr),
725 data->autofan[nr].min_pwm);
726 up(&data->update_lock);
729 static ssize_t show_pwm_auto_pwm_minctl(struct device *dev, char *buf, int nr)
731 struct lm85_data *data = lm85_update_device(dev);
732 return sprintf(buf,"%d\n", data->autofan[nr].min_off);
734 static ssize_t set_pwm_auto_pwm_minctl(struct device *dev, const char *buf,
735 size_t count, int nr)
737 struct i2c_client *client = to_i2c_client(dev);
738 struct lm85_data *data = i2c_get_clientdata(client);
741 down(&data->update_lock);
742 val = simple_strtol(buf, NULL, 10);
743 data->autofan[nr].min_off = val;
744 lm85_write_value(client, LM85_REG_AFAN_SPIKE1, data->smooth[0]
746 | (data->autofan[0].min_off ? 0x20 : 0)
747 | (data->autofan[1].min_off ? 0x40 : 0)
748 | (data->autofan[2].min_off ? 0x80 : 0)
750 up(&data->update_lock);
753 static ssize_t show_pwm_auto_pwm_freq(struct device *dev, char *buf, int nr)
755 struct lm85_data *data = lm85_update_device(dev);
756 return sprintf(buf,"%d\n", FREQ_FROM_REG(data->autofan[nr].freq));
758 static ssize_t set_pwm_auto_pwm_freq(struct device *dev, const char *buf,
759 size_t count, int nr)
761 struct i2c_client *client = to_i2c_client(dev);
762 struct lm85_data *data = i2c_get_clientdata(client);
765 down(&data->update_lock);
766 val = simple_strtol(buf, NULL, 10);
767 data->autofan[nr].freq = FREQ_TO_REG(val);
768 lm85_write_value(client, LM85_REG_AFAN_RANGE(nr),
769 (data->zone[nr].range << 4)
770 | data->autofan[nr].freq
772 up(&data->update_lock);
775 #define pwm_auto(offset) \
776 static ssize_t show_pwm##offset##_auto_channels (struct device *dev, \
779 return show_pwm_auto_channels(dev, buf, offset - 1); \
781 static ssize_t set_pwm##offset##_auto_channels (struct device *dev, \
782 const char *buf, size_t count) \
784 return set_pwm_auto_channels(dev, buf, count, offset - 1); \
786 static ssize_t show_pwm##offset##_auto_pwm_min (struct device *dev, \
789 return show_pwm_auto_pwm_min(dev, buf, offset - 1); \
791 static ssize_t set_pwm##offset##_auto_pwm_min (struct device *dev, \
792 const char *buf, size_t count) \
794 return set_pwm_auto_pwm_min(dev, buf, count, offset - 1); \
796 static ssize_t show_pwm##offset##_auto_pwm_minctl (struct device *dev, \
799 return show_pwm_auto_pwm_minctl(dev, buf, offset - 1); \
801 static ssize_t set_pwm##offset##_auto_pwm_minctl (struct device *dev, \
802 const char *buf, size_t count) \
804 return set_pwm_auto_pwm_minctl(dev, buf, count, offset - 1); \
806 static ssize_t show_pwm##offset##_auto_pwm_freq (struct device *dev, \
809 return show_pwm_auto_pwm_freq(dev, buf, offset - 1); \
811 static ssize_t set_pwm##offset##_auto_pwm_freq(struct device *dev, \
812 const char *buf, size_t count) \
814 return set_pwm_auto_pwm_freq(dev, buf, count, offset - 1); \
816 static DEVICE_ATTR(pwm##offset##_auto_channels, S_IRUGO | S_IWUSR, \
817 show_pwm##offset##_auto_channels, \
818 set_pwm##offset##_auto_channels); \
819 static DEVICE_ATTR(pwm##offset##_auto_pwm_min, S_IRUGO | S_IWUSR, \
820 show_pwm##offset##_auto_pwm_min, \
821 set_pwm##offset##_auto_pwm_min); \
822 static DEVICE_ATTR(pwm##offset##_auto_pwm_minctl, S_IRUGO | S_IWUSR, \
823 show_pwm##offset##_auto_pwm_minctl, \
824 set_pwm##offset##_auto_pwm_minctl); \
825 static DEVICE_ATTR(pwm##offset##_auto_pwm_freq, S_IRUGO | S_IWUSR, \
826 show_pwm##offset##_auto_pwm_freq, \
827 set_pwm##offset##_auto_pwm_freq);
832 /* Temperature settings for automatic PWM control */
834 static ssize_t show_temp_auto_temp_off(struct device *dev, char *buf, int nr)
836 struct lm85_data *data = lm85_update_device(dev);
837 return sprintf(buf,"%d\n", TEMP_FROM_REG(data->zone[nr].limit) -
838 HYST_FROM_REG(data->zone[nr].hyst));
840 static ssize_t set_temp_auto_temp_off(struct device *dev, const char *buf,
841 size_t count, int nr)
843 struct i2c_client *client = to_i2c_client(dev);
844 struct lm85_data *data = i2c_get_clientdata(client);
847 down(&data->update_lock);
848 val = simple_strtol(buf, NULL, 10);
849 min = TEMP_FROM_REG(data->zone[nr].limit);
850 data->zone[nr].off_desired = TEMP_TO_REG(val);
851 data->zone[nr].hyst = HYST_TO_REG(min - val);
852 if ( nr == 0 || nr == 1 ) {
853 lm85_write_value(client, LM85_REG_AFAN_HYST1,
854 (data->zone[0].hyst << 4)
858 lm85_write_value(client, LM85_REG_AFAN_HYST2,
859 (data->zone[2].hyst << 4)
862 up(&data->update_lock);
865 static ssize_t show_temp_auto_temp_min(struct device *dev, char *buf, int nr)
867 struct lm85_data *data = lm85_update_device(dev);
868 return sprintf(buf,"%d\n", TEMP_FROM_REG(data->zone[nr].limit) );
870 static ssize_t set_temp_auto_temp_min(struct device *dev, const char *buf,
871 size_t count, int nr)
873 struct i2c_client *client = to_i2c_client(dev);
874 struct lm85_data *data = i2c_get_clientdata(client);
877 down(&data->update_lock);
878 val = simple_strtol(buf, NULL, 10);
879 data->zone[nr].limit = TEMP_TO_REG(val);
880 lm85_write_value(client, LM85_REG_AFAN_LIMIT(nr),
881 data->zone[nr].limit);
883 /* Update temp_auto_max and temp_auto_range */
884 data->zone[nr].range = RANGE_TO_REG(
885 TEMP_FROM_REG(data->zone[nr].max_desired) -
886 TEMP_FROM_REG(data->zone[nr].limit));
887 lm85_write_value(client, LM85_REG_AFAN_RANGE(nr),
888 ((data->zone[nr].range & 0x0f) << 4)
889 | (data->autofan[nr].freq & 0x07));
891 /* Update temp_auto_hyst and temp_auto_off */
892 data->zone[nr].hyst = HYST_TO_REG(TEMP_FROM_REG(
893 data->zone[nr].limit) - TEMP_FROM_REG(
894 data->zone[nr].off_desired));
895 if ( nr == 0 || nr == 1 ) {
896 lm85_write_value(client, LM85_REG_AFAN_HYST1,
897 (data->zone[0].hyst << 4)
901 lm85_write_value(client, LM85_REG_AFAN_HYST2,
902 (data->zone[2].hyst << 4)
905 up(&data->update_lock);
908 static ssize_t show_temp_auto_temp_max(struct device *dev, char *buf, int nr)
910 struct lm85_data *data = lm85_update_device(dev);
911 return sprintf(buf,"%d\n", TEMP_FROM_REG(data->zone[nr].limit) +
912 RANGE_FROM_REG(data->zone[nr].range));
914 static ssize_t set_temp_auto_temp_max(struct device *dev, const char *buf,
915 size_t count, int nr)
917 struct i2c_client *client = to_i2c_client(dev);
918 struct lm85_data *data = i2c_get_clientdata(client);
921 down(&data->update_lock);
922 min = TEMP_FROM_REG(data->zone[nr].limit);
923 val = simple_strtol(buf, NULL, 10);
924 data->zone[nr].max_desired = TEMP_TO_REG(val);
925 data->zone[nr].range = RANGE_TO_REG(
927 lm85_write_value(client, LM85_REG_AFAN_RANGE(nr),
928 ((data->zone[nr].range & 0x0f) << 4)
929 | (data->autofan[nr].freq & 0x07));
930 up(&data->update_lock);
933 static ssize_t show_temp_auto_temp_crit(struct device *dev, char *buf, int nr)
935 struct lm85_data *data = lm85_update_device(dev);
936 return sprintf(buf,"%d\n", TEMP_FROM_REG(data->zone[nr].critical));
938 static ssize_t set_temp_auto_temp_crit(struct device *dev, const char *buf,
939 size_t count, int nr)
941 struct i2c_client *client = to_i2c_client(dev);
942 struct lm85_data *data = i2c_get_clientdata(client);
945 down(&data->update_lock);
946 val = simple_strtol(buf, NULL, 10);
947 data->zone[nr].critical = TEMP_TO_REG(val);
948 lm85_write_value(client, LM85_REG_AFAN_CRITICAL(nr),
949 data->zone[nr].critical);
950 up(&data->update_lock);
953 #define temp_auto(offset) \
954 static ssize_t show_temp##offset##_auto_temp_off (struct device *dev, \
957 return show_temp_auto_temp_off(dev, buf, offset - 1); \
959 static ssize_t set_temp##offset##_auto_temp_off (struct device *dev, \
960 const char *buf, size_t count) \
962 return set_temp_auto_temp_off(dev, buf, count, offset - 1); \
964 static ssize_t show_temp##offset##_auto_temp_min (struct device *dev, \
967 return show_temp_auto_temp_min(dev, buf, offset - 1); \
969 static ssize_t set_temp##offset##_auto_temp_min (struct device *dev, \
970 const char *buf, size_t count) \
972 return set_temp_auto_temp_min(dev, buf, count, offset - 1); \
974 static ssize_t show_temp##offset##_auto_temp_max (struct device *dev, \
977 return show_temp_auto_temp_max(dev, buf, offset - 1); \
979 static ssize_t set_temp##offset##_auto_temp_max (struct device *dev, \
980 const char *buf, size_t count) \
982 return set_temp_auto_temp_max(dev, buf, count, offset - 1); \
984 static ssize_t show_temp##offset##_auto_temp_crit (struct device *dev, \
987 return show_temp_auto_temp_crit(dev, buf, offset - 1); \
989 static ssize_t set_temp##offset##_auto_temp_crit (struct device *dev, \
990 const char *buf, size_t count) \
992 return set_temp_auto_temp_crit(dev, buf, count, offset - 1); \
994 static DEVICE_ATTR(temp##offset##_auto_temp_off, S_IRUGO | S_IWUSR, \
995 show_temp##offset##_auto_temp_off, \
996 set_temp##offset##_auto_temp_off); \
997 static DEVICE_ATTR(temp##offset##_auto_temp_min, S_IRUGO | S_IWUSR, \
998 show_temp##offset##_auto_temp_min, \
999 set_temp##offset##_auto_temp_min); \
1000 static DEVICE_ATTR(temp##offset##_auto_temp_max, S_IRUGO | S_IWUSR, \
1001 show_temp##offset##_auto_temp_max, \
1002 set_temp##offset##_auto_temp_max); \
1003 static DEVICE_ATTR(temp##offset##_auto_temp_crit, S_IRUGO | S_IWUSR, \
1004 show_temp##offset##_auto_temp_crit, \
1005 set_temp##offset##_auto_temp_crit);
1010 int lm85_attach_adapter(struct i2c_adapter *adapter)
1012 if (!(adapter->class & I2C_CLASS_HWMON))
1014 return i2c_detect(adapter, &addr_data, lm85_detect);
1017 int lm85_detect(struct i2c_adapter *adapter, int address,
1020 int company, verstep ;
1021 struct i2c_client *new_client = NULL;
1022 struct lm85_data *data;
1024 const char *type_name = "";
1026 if (i2c_is_isa_adapter(adapter)) {
1027 /* This chip has no ISA interface */
1031 if (!i2c_check_functionality(adapter,
1032 I2C_FUNC_SMBUS_BYTE_DATA)) {
1033 /* We need to be able to do byte I/O */
1037 /* OK. For now, we presume we have a valid client. We now create the
1038 client structure, even though we cannot fill it completely yet.
1039 But it allows us to access lm85_{read,write}_value. */
1041 if (!(data = kmalloc(sizeof(struct lm85_data), GFP_KERNEL))) {
1045 memset(data, 0, sizeof(struct lm85_data));
1047 new_client = &data->client;
1048 i2c_set_clientdata(new_client, data);
1049 new_client->addr = address;
1050 new_client->adapter = adapter;
1051 new_client->driver = &lm85_driver;
1052 new_client->flags = 0;
1054 /* Now, we do the remaining detection. */
1056 company = lm85_read_value(new_client, LM85_REG_COMPANY);
1057 verstep = lm85_read_value(new_client, LM85_REG_VERSTEP);
1059 dev_dbg(&adapter->dev, "Detecting device at %d,0x%02x with"
1060 " COMPANY: 0x%02x and VERSTEP: 0x%02x\n",
1061 i2c_adapter_id(new_client->adapter), new_client->addr,
1064 /* If auto-detecting, Determine the chip type. */
1066 dev_dbg(&adapter->dev, "Autodetecting device at %d,0x%02x ...\n",
1067 i2c_adapter_id(adapter), address );
1068 if( company == LM85_COMPANY_NATIONAL
1069 && verstep == LM85_VERSTEP_LM85C ) {
1071 } else if( company == LM85_COMPANY_NATIONAL
1072 && verstep == LM85_VERSTEP_LM85B ) {
1074 } else if( company == LM85_COMPANY_NATIONAL
1075 && (verstep & 0xf0) == LM85_VERSTEP_GENERIC ) {
1076 dev_err(&adapter->dev, "Unrecognized version/stepping 0x%02x"
1077 " Defaulting to LM85.\n", verstep);
1079 } else if( company == LM85_COMPANY_ANALOG_DEV
1080 && verstep == LM85_VERSTEP_ADM1027 ) {
1082 } else if( company == LM85_COMPANY_ANALOG_DEV
1083 && verstep == LM85_VERSTEP_ADT7463 ) {
1085 } else if( company == LM85_COMPANY_ANALOG_DEV
1086 && (verstep & 0xf0) == LM85_VERSTEP_GENERIC ) {
1087 dev_err(&adapter->dev, "Unrecognized version/stepping 0x%02x"
1088 " Defaulting to ADM1027.\n", verstep);
1090 } else if( kind == 0 && (verstep & 0xf0) == 0x60) {
1091 dev_err(&adapter->dev, "Generic LM85 Version 6 detected\n");
1092 /* Leave kind as "any_chip" */
1094 dev_dbg(&adapter->dev, "Autodetection failed\n");
1095 /* Not an LM85 ... */
1096 if( kind == 0 ) { /* User used force=x,y */
1097 dev_err(&adapter->dev, "Generic LM85 Version 6 not"
1098 " found at %d,0x%02x. Try force_lm85c.\n",
1099 i2c_adapter_id(adapter), address );
1106 /* Fill in the chip specific driver values */
1107 if ( kind == any_chip ) {
1109 } else if ( kind == lm85b ) {
1110 type_name = "lm85b";
1111 } else if ( kind == lm85c ) {
1112 type_name = "lm85c";
1113 } else if ( kind == adm1027 ) {
1114 type_name = "adm1027";
1115 } else if ( kind == adt7463 ) {
1116 type_name = "adt7463";
1118 strlcpy(new_client->name, type_name, I2C_NAME_SIZE);
1120 /* Fill in the remaining client fields */
1121 new_client->id = lm85_id++;
1124 init_MUTEX(&data->update_lock);
1126 dev_dbg(&adapter->dev, "Assigning ID %d to %s at %d,0x%02x\n",
1127 new_client->id, new_client->name,
1128 i2c_adapter_id(new_client->adapter),
1131 /* Tell the I2C layer a new client has arrived */
1132 if ((err = i2c_attach_client(new_client)))
1135 /* Set the VRM version */
1136 data->vrm = i2c_which_vrm();
1138 /* Initialize the LM85 chip */
1139 lm85_init_client(new_client);
1141 /* Register sysfs hooks */
1142 device_create_file(&new_client->dev, &dev_attr_fan1_input);
1143 device_create_file(&new_client->dev, &dev_attr_fan2_input);
1144 device_create_file(&new_client->dev, &dev_attr_fan3_input);
1145 device_create_file(&new_client->dev, &dev_attr_fan4_input);
1146 device_create_file(&new_client->dev, &dev_attr_fan1_min);
1147 device_create_file(&new_client->dev, &dev_attr_fan2_min);
1148 device_create_file(&new_client->dev, &dev_attr_fan3_min);
1149 device_create_file(&new_client->dev, &dev_attr_fan4_min);
1150 device_create_file(&new_client->dev, &dev_attr_pwm1);
1151 device_create_file(&new_client->dev, &dev_attr_pwm2);
1152 device_create_file(&new_client->dev, &dev_attr_pwm3);
1153 device_create_file(&new_client->dev, &dev_attr_pwm1_enable);
1154 device_create_file(&new_client->dev, &dev_attr_pwm2_enable);
1155 device_create_file(&new_client->dev, &dev_attr_pwm3_enable);
1156 device_create_file(&new_client->dev, &dev_attr_in0_input);
1157 device_create_file(&new_client->dev, &dev_attr_in1_input);
1158 device_create_file(&new_client->dev, &dev_attr_in2_input);
1159 device_create_file(&new_client->dev, &dev_attr_in3_input);
1160 device_create_file(&new_client->dev, &dev_attr_in4_input);
1161 device_create_file(&new_client->dev, &dev_attr_in0_min);
1162 device_create_file(&new_client->dev, &dev_attr_in1_min);
1163 device_create_file(&new_client->dev, &dev_attr_in2_min);
1164 device_create_file(&new_client->dev, &dev_attr_in3_min);
1165 device_create_file(&new_client->dev, &dev_attr_in4_min);
1166 device_create_file(&new_client->dev, &dev_attr_in0_max);
1167 device_create_file(&new_client->dev, &dev_attr_in1_max);
1168 device_create_file(&new_client->dev, &dev_attr_in2_max);
1169 device_create_file(&new_client->dev, &dev_attr_in3_max);
1170 device_create_file(&new_client->dev, &dev_attr_in4_max);
1171 device_create_file(&new_client->dev, &dev_attr_temp1_input);
1172 device_create_file(&new_client->dev, &dev_attr_temp2_input);
1173 device_create_file(&new_client->dev, &dev_attr_temp3_input);
1174 device_create_file(&new_client->dev, &dev_attr_temp1_min);
1175 device_create_file(&new_client->dev, &dev_attr_temp2_min);
1176 device_create_file(&new_client->dev, &dev_attr_temp3_min);
1177 device_create_file(&new_client->dev, &dev_attr_temp1_max);
1178 device_create_file(&new_client->dev, &dev_attr_temp2_max);
1179 device_create_file(&new_client->dev, &dev_attr_temp3_max);
1180 device_create_file(&new_client->dev, &dev_attr_vrm);
1181 device_create_file(&new_client->dev, &dev_attr_cpu0_vid);
1182 device_create_file(&new_client->dev, &dev_attr_alarms);
1183 device_create_file(&new_client->dev, &dev_attr_pwm1_auto_channels);
1184 device_create_file(&new_client->dev, &dev_attr_pwm2_auto_channels);
1185 device_create_file(&new_client->dev, &dev_attr_pwm3_auto_channels);
1186 device_create_file(&new_client->dev, &dev_attr_pwm1_auto_pwm_min);
1187 device_create_file(&new_client->dev, &dev_attr_pwm2_auto_pwm_min);
1188 device_create_file(&new_client->dev, &dev_attr_pwm3_auto_pwm_min);
1189 device_create_file(&new_client->dev, &dev_attr_pwm1_auto_pwm_minctl);
1190 device_create_file(&new_client->dev, &dev_attr_pwm2_auto_pwm_minctl);
1191 device_create_file(&new_client->dev, &dev_attr_pwm3_auto_pwm_minctl);
1192 device_create_file(&new_client->dev, &dev_attr_pwm1_auto_pwm_freq);
1193 device_create_file(&new_client->dev, &dev_attr_pwm2_auto_pwm_freq);
1194 device_create_file(&new_client->dev, &dev_attr_pwm3_auto_pwm_freq);
1195 device_create_file(&new_client->dev, &dev_attr_temp1_auto_temp_off);
1196 device_create_file(&new_client->dev, &dev_attr_temp2_auto_temp_off);
1197 device_create_file(&new_client->dev, &dev_attr_temp3_auto_temp_off);
1198 device_create_file(&new_client->dev, &dev_attr_temp1_auto_temp_min);
1199 device_create_file(&new_client->dev, &dev_attr_temp2_auto_temp_min);
1200 device_create_file(&new_client->dev, &dev_attr_temp3_auto_temp_min);
1201 device_create_file(&new_client->dev, &dev_attr_temp1_auto_temp_max);
1202 device_create_file(&new_client->dev, &dev_attr_temp2_auto_temp_max);
1203 device_create_file(&new_client->dev, &dev_attr_temp3_auto_temp_max);
1204 device_create_file(&new_client->dev, &dev_attr_temp1_auto_temp_crit);
1205 device_create_file(&new_client->dev, &dev_attr_temp2_auto_temp_crit);
1206 device_create_file(&new_client->dev, &dev_attr_temp3_auto_temp_crit);
1210 /* Error out and cleanup code */
1217 int lm85_detach_client(struct i2c_client *client)
1219 i2c_detach_client(client);
1220 kfree(i2c_get_clientdata(client));
1225 int lm85_read_value(struct i2c_client *client, u8 reg)
1229 /* What size location is it? */
1231 case LM85_REG_FAN(0) : /* Read WORD data */
1232 case LM85_REG_FAN(1) :
1233 case LM85_REG_FAN(2) :
1234 case LM85_REG_FAN(3) :
1235 case LM85_REG_FAN_MIN(0) :
1236 case LM85_REG_FAN_MIN(1) :
1237 case LM85_REG_FAN_MIN(2) :
1238 case LM85_REG_FAN_MIN(3) :
1239 case LM85_REG_ALARM1 : /* Read both bytes at once */
1240 case ADM1027_REG_EXTEND_ADC1 : /* Read two bytes at once */
1241 res = i2c_smbus_read_byte_data(client, reg) & 0xff ;
1242 res |= i2c_smbus_read_byte_data(client, reg+1) << 8 ;
1244 case ADT7463_REG_TMIN_CTL1 : /* Read WORD MSB, LSB */
1245 res = i2c_smbus_read_byte_data(client, reg) << 8 ;
1246 res |= i2c_smbus_read_byte_data(client, reg+1) & 0xff ;
1248 default: /* Read BYTE data */
1249 res = i2c_smbus_read_byte_data(client, reg);
1256 int lm85_write_value(struct i2c_client *client, u8 reg, int value)
1261 case LM85_REG_FAN(0) : /* Write WORD data */
1262 case LM85_REG_FAN(1) :
1263 case LM85_REG_FAN(2) :
1264 case LM85_REG_FAN(3) :
1265 case LM85_REG_FAN_MIN(0) :
1266 case LM85_REG_FAN_MIN(1) :
1267 case LM85_REG_FAN_MIN(2) :
1268 case LM85_REG_FAN_MIN(3) :
1269 /* NOTE: ALARM is read only, so not included here */
1270 res = i2c_smbus_write_byte_data(client, reg, value & 0xff) ;
1271 res |= i2c_smbus_write_byte_data(client, reg+1, (value>>8) & 0xff) ;
1273 case ADT7463_REG_TMIN_CTL1 : /* Write WORD MSB, LSB */
1274 res = i2c_smbus_write_byte_data(client, reg, (value>>8) & 0xff);
1275 res |= i2c_smbus_write_byte_data(client, reg+1, value & 0xff) ;
1277 default: /* Write BYTE data */
1278 res = i2c_smbus_write_byte_data(client, reg, value);
1285 void lm85_init_client(struct i2c_client *client)
1288 struct lm85_data *data = i2c_get_clientdata(client);
1290 dev_dbg(&client->dev, "Initializing device\n");
1292 /* Warn if part was not "READY" */
1293 value = lm85_read_value(client, LM85_REG_CONFIG);
1294 dev_dbg(&client->dev, "LM85_REG_CONFIG is: 0x%02x\n", value);
1295 if( value & 0x02 ) {
1296 dev_err(&client->dev, "Client (%d,0x%02x) config is locked.\n",
1297 i2c_adapter_id(client->adapter), client->addr );
1299 if( ! (value & 0x04) ) {
1300 dev_err(&client->dev, "Client (%d,0x%02x) is not ready.\n",
1301 i2c_adapter_id(client->adapter), client->addr );
1304 && ( data->type == adm1027
1305 || data->type == adt7463 ) ) {
1306 dev_err(&client->dev, "Client (%d,0x%02x) VxI mode is set. "
1307 "Please report this to the lm85 maintainer.\n",
1308 i2c_adapter_id(client->adapter), client->addr );
1311 /* WE INTENTIONALLY make no changes to the limits,
1312 * offsets, pwms, fans and zones. If they were
1313 * configured, we don't want to mess with them.
1314 * If they weren't, the default is 100% PWM, no
1315 * control and will suffice until 'sensors -s'
1316 * can be run by the user.
1319 /* Start monitoring */
1320 value = lm85_read_value(client, LM85_REG_CONFIG);
1321 /* Try to clear LOCK, Set START, save everything else */
1322 value = (value & ~ 0x02) | 0x01 ;
1323 dev_dbg(&client->dev, "Setting CONFIG to: 0x%02x\n", value);
1324 lm85_write_value(client, LM85_REG_CONFIG, value);
1327 static struct lm85_data *lm85_update_device(struct device *dev)
1329 struct i2c_client *client = to_i2c_client(dev);
1330 struct lm85_data *data = i2c_get_clientdata(client);
1333 down(&data->update_lock);
1335 if ( !data->valid ||
1336 (jiffies - data->last_reading > LM85_DATA_INTERVAL ) ) {
1337 /* Things that change quickly */
1338 dev_dbg(&client->dev, "Reading sensor values\n");
1340 /* Have to read extended bits first to "freeze" the
1341 * more significant bits that are read later.
1343 if ( (data->type == adm1027) || (data->type == adt7463) ) {
1345 lm85_read_value(client, ADM1027_REG_EXTEND_ADC1);
1348 for (i = 0; i <= 4; ++i) {
1350 lm85_read_value(client, LM85_REG_IN(i));
1353 for (i = 0; i <= 3; ++i) {
1355 lm85_read_value(client, LM85_REG_FAN(i));
1358 for (i = 0; i <= 2; ++i) {
1360 lm85_read_value(client, LM85_REG_TEMP(i));
1363 for (i = 0; i <= 2; ++i) {
1365 lm85_read_value(client, LM85_REG_PWM(i));
1368 if ( data->type == adt7463 ) {
1369 if( data->therm_total < ULONG_MAX - 256 ) {
1370 data->therm_total +=
1371 lm85_read_value(client, ADT7463_REG_THERM );
1375 data->alarms = lm85_read_value(client, LM85_REG_ALARM1);
1377 data->last_reading = jiffies ;
1378 }; /* last_reading */
1380 if ( !data->valid ||
1381 (jiffies - data->last_config > LM85_CONFIG_INTERVAL) ) {
1382 /* Things that don't change often */
1383 dev_dbg(&client->dev, "Reading config values\n");
1385 for (i = 0; i <= 4; ++i) {
1387 lm85_read_value(client, LM85_REG_IN_MIN(i));
1389 lm85_read_value(client, LM85_REG_IN_MAX(i));
1392 for (i = 0; i <= 3; ++i) {
1394 lm85_read_value(client, LM85_REG_FAN_MIN(i));
1397 for (i = 0; i <= 2; ++i) {
1399 lm85_read_value(client, LM85_REG_TEMP_MIN(i));
1401 lm85_read_value(client, LM85_REG_TEMP_MAX(i));
1404 data->vid = lm85_read_value(client, LM85_REG_VID);
1406 for (i = 0; i <= 2; ++i) {
1408 data->autofan[i].config =
1409 lm85_read_value(client, LM85_REG_AFAN_CONFIG(i));
1410 val = lm85_read_value(client, LM85_REG_AFAN_RANGE(i));
1411 data->autofan[i].freq = val & 0x07 ;
1412 data->zone[i].range = (val >> 4) & 0x0f ;
1413 data->autofan[i].min_pwm =
1414 lm85_read_value(client, LM85_REG_AFAN_MINPWM(i));
1415 data->zone[i].limit =
1416 lm85_read_value(client, LM85_REG_AFAN_LIMIT(i));
1417 data->zone[i].critical =
1418 lm85_read_value(client, LM85_REG_AFAN_CRITICAL(i));
1421 i = lm85_read_value(client, LM85_REG_AFAN_SPIKE1);
1422 data->smooth[0] = i & 0x0f ;
1423 data->syncpwm3 = i & 0x10 ; /* Save PWM3 config */
1424 data->autofan[0].min_off = (i & 0x20) != 0 ;
1425 data->autofan[1].min_off = (i & 0x40) != 0 ;
1426 data->autofan[2].min_off = (i & 0x80) != 0 ;
1427 i = lm85_read_value(client, LM85_REG_AFAN_SPIKE2);
1428 data->smooth[1] = (i>>4) & 0x0f ;
1429 data->smooth[2] = i & 0x0f ;
1431 i = lm85_read_value(client, LM85_REG_AFAN_HYST1);
1432 data->zone[0].hyst = (i>>4) & 0x0f ;
1433 data->zone[1].hyst = i & 0x0f ;
1435 i = lm85_read_value(client, LM85_REG_AFAN_HYST2);
1436 data->zone[2].hyst = (i>>4) & 0x0f ;
1438 if ( (data->type == lm85b) || (data->type == lm85c) ) {
1439 data->tach_mode = lm85_read_value(client,
1440 LM85_REG_TACH_MODE );
1441 data->spinup_ctl = lm85_read_value(client,
1442 LM85_REG_SPINUP_CTL );
1443 } else if ( (data->type == adt7463) || (data->type == adm1027) ) {
1444 if ( data->type == adt7463 ) {
1445 for (i = 0; i <= 2; ++i) {
1446 data->oppoint[i] = lm85_read_value(client,
1447 ADT7463_REG_OPPOINT(i) );
1449 data->tmin_ctl = lm85_read_value(client,
1450 ADT7463_REG_TMIN_CTL1 );
1451 data->therm_limit = lm85_read_value(client,
1452 ADT7463_REG_THERM_LIMIT );
1454 for (i = 0; i <= 2; ++i) {
1455 data->temp_offset[i] = lm85_read_value(client,
1456 ADM1027_REG_TEMP_OFFSET(i) );
1458 data->tach_mode = lm85_read_value(client,
1459 ADM1027_REG_CONFIG3 );
1460 data->fan_ppr = lm85_read_value(client,
1461 ADM1027_REG_FAN_PPR );
1464 data->last_config = jiffies;
1465 }; /* last_config */
1469 up(&data->update_lock);
1475 static int __init sm_lm85_init(void)
1477 return i2c_add_driver(&lm85_driver);
1480 static void __exit sm_lm85_exit(void)
1482 i2c_del_driver(&lm85_driver);
1485 /* Thanks to Richard Barrington for adding the LM85 to sensors-detect.
1486 * Thanks to Margit Schubert-While <margitsw@t-online.de> for help with
1487 * post 2.7.0 CVS changes.
1489 MODULE_LICENSE("GPL");
1490 MODULE_AUTHOR("Philip Pokorny <ppokorny@penguincomputing.com>, Margit Schubert-While <margitsw@t-online.de>, Justin Thiessen <jthiessen@penguincomputing.com");
1491 MODULE_DESCRIPTION("LM85-B, LM85-C driver");
1493 module_init(sm_lm85_init);
1494 module_exit(sm_lm85_exit);