[PATCH] I2C: Add support for the LPC47M15x and LPC47M192 chips to smsc47m1
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / i2c / chips / lm85.c
blobd618d10313013c572aa6f4aaf02bfb904d136e6f
1 /*
2 lm85.c - Part of lm_sensors, Linux kernel modules for hardware
3 monitoring
4 Copyright (c) 1998, 1999 Frodo Looijaard <frodol@dds.nl>
5 Copyright (c) 2002, 2003 Philip Pokorny <ppokorny@penguincomputing.com>
6 Copyright (c) 2003 Margit Schubert-While <margitsw@t-online.de>
7 Copyright (c) 2004 Justin Thiessen <jthiessen@penguincomputing.com>
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/module.h>
27 #include <linux/init.h>
28 #include <linux/slab.h>
29 #include <linux/jiffies.h>
30 #include <linux/i2c.h>
31 #include <linux/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_6(lm85b, lm85c, adm1027, adt7463, emc6d100, emc6d102);
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_COMPANY_SMSC 0x5c
70 #define LM85_VERSTEP_VMASK 0xf0
71 #define LM85_VERSTEP_GENERIC 0x60
72 #define LM85_VERSTEP_LM85C 0x60
73 #define LM85_VERSTEP_LM85B 0x62
74 #define LM85_VERSTEP_ADM1027 0x60
75 #define LM85_VERSTEP_ADT7463 0x62
76 #define LM85_VERSTEP_ADT7463C 0x6A
77 #define LM85_VERSTEP_EMC6D100_A0 0x60
78 #define LM85_VERSTEP_EMC6D100_A1 0x61
79 #define LM85_VERSTEP_EMC6D102 0x65
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 EMC6D100_REG_ALARM3 0x7d
115 /* IN5, IN6 and IN7 */
116 #define EMC6D100_REG_IN(nr) (0x70 + ((nr)-5))
117 #define EMC6D100_REG_IN_MIN(nr) (0x73 + ((nr)-5) * 2)
118 #define EMC6D100_REG_IN_MAX(nr) (0x74 + ((nr)-5) * 2)
119 #define EMC6D102_REG_EXTEND_ADC1 0x85
120 #define EMC6D102_REG_EXTEND_ADC2 0x86
121 #define EMC6D102_REG_EXTEND_ADC3 0x87
122 #define EMC6D102_REG_EXTEND_ADC4 0x88
124 #define LM85_ALARM_IN0 0x0001
125 #define LM85_ALARM_IN1 0x0002
126 #define LM85_ALARM_IN2 0x0004
127 #define LM85_ALARM_IN3 0x0008
128 #define LM85_ALARM_TEMP1 0x0010
129 #define LM85_ALARM_TEMP2 0x0020
130 #define LM85_ALARM_TEMP3 0x0040
131 #define LM85_ALARM_ALARM2 0x0080
132 #define LM85_ALARM_IN4 0x0100
133 #define LM85_ALARM_RESERVED 0x0200
134 #define LM85_ALARM_FAN1 0x0400
135 #define LM85_ALARM_FAN2 0x0800
136 #define LM85_ALARM_FAN3 0x1000
137 #define LM85_ALARM_FAN4 0x2000
138 #define LM85_ALARM_TEMP1_FAULT 0x4000
139 #define LM85_ALARM_TEMP3_FAULT 0x8000
142 /* Conversions. Rounding and limit checking is only done on the TO_REG
143 variants. Note that you should be a bit careful with which arguments
144 these macros are called: arguments may be evaluated more than once.
147 /* IN are scaled acording to built-in resistors */
148 static int lm85_scaling[] = { /* .001 Volts */
149 2500, 2250, 3300, 5000, 12000,
150 3300, 1500, 1800 /*EMC6D100*/
152 #define SCALE(val,from,to) (((val)*(to) + ((from)/2))/(from))
154 #define INS_TO_REG(n,val) \
155 SENSORS_LIMIT(SCALE(val,lm85_scaling[n],192),0,255)
157 #define INSEXT_FROM_REG(n,val,ext,scale) \
158 SCALE((val)*(scale) + (ext),192*(scale),lm85_scaling[n])
160 #define INS_FROM_REG(n,val) INSEXT_FROM_REG(n,val,0,1)
162 /* FAN speed is measured using 90kHz clock */
163 #define FAN_TO_REG(val) (SENSORS_LIMIT( (val)<=0?0: 5400000/(val),0,65534))
164 #define FAN_FROM_REG(val) ((val)==0?-1:(val)==0xffff?0:5400000/(val))
166 /* Temperature is reported in .001 degC increments */
167 #define TEMP_TO_REG(val) \
168 SENSORS_LIMIT(SCALE(val,1000,1),-127,127)
169 #define TEMPEXT_FROM_REG(val,ext,scale) \
170 SCALE((val)*scale + (ext),scale,1000)
171 #define TEMP_FROM_REG(val) \
172 TEMPEXT_FROM_REG(val,0,1)
174 #define PWM_TO_REG(val) (SENSORS_LIMIT(val,0,255))
175 #define PWM_FROM_REG(val) (val)
178 /* ZONEs have the following parameters:
179 * Limit (low) temp, 1. degC
180 * Hysteresis (below limit), 1. degC (0-15)
181 * Range of speed control, .1 degC (2-80)
182 * Critical (high) temp, 1. degC
184 * FAN PWMs have the following parameters:
185 * Reference Zone, 1, 2, 3, etc.
186 * Spinup time, .05 sec
187 * PWM value at limit/low temp, 1 count
188 * PWM Frequency, 1. Hz
189 * PWM is Min or OFF below limit, flag
190 * Invert PWM output, flag
192 * Some chips filter the temp, others the fan.
193 * Filter constant (or disabled) .1 seconds
196 /* These are the zone temperature range encodings in .001 degree C */
197 static int lm85_range_map[] = {
198 2000, 2500, 3300, 4000, 5000, 6600,
199 8000, 10000, 13300, 16000, 20000, 26600,
200 32000, 40000, 53300, 80000
202 static int RANGE_TO_REG( int range )
204 int i;
206 if ( range < lm85_range_map[0] ) {
207 return 0 ;
208 } else if ( range > lm85_range_map[15] ) {
209 return 15 ;
210 } else { /* find closest match */
211 for ( i = 14 ; i >= 0 ; --i ) {
212 if ( range > lm85_range_map[i] ) { /* range bracketed */
213 if ((lm85_range_map[i+1] - range) <
214 (range - lm85_range_map[i])) {
215 i++;
216 break;
218 break;
222 return( i & 0x0f );
224 #define RANGE_FROM_REG(val) (lm85_range_map[(val)&0x0f])
226 /* These are the Acoustic Enhancement, or Temperature smoothing encodings
227 * NOTE: The enable/disable bit is INCLUDED in these encodings as the
228 * MSB (bit 3, value 8). If the enable bit is 0, the encoded value
229 * is ignored, or set to 0.
231 /* These are the PWM frequency encodings */
232 static int lm85_freq_map[] = { /* .1 Hz */
233 100, 150, 230, 300, 380, 470, 620, 940
235 static int FREQ_TO_REG( int freq )
237 int i;
239 if( freq >= lm85_freq_map[7] ) { return 7 ; }
240 for( i = 0 ; i < 7 ; ++i )
241 if( freq <= lm85_freq_map[i] )
242 break ;
243 return( i & 0x07 );
245 #define FREQ_FROM_REG(val) (lm85_freq_map[(val)&0x07])
247 /* Since we can't use strings, I'm abusing these numbers
248 * to stand in for the following meanings:
249 * 1 -- PWM responds to Zone 1
250 * 2 -- PWM responds to Zone 2
251 * 3 -- PWM responds to Zone 3
252 * 23 -- PWM responds to the higher temp of Zone 2 or 3
253 * 123 -- PWM responds to highest of Zone 1, 2, or 3
254 * 0 -- PWM is always at 0% (ie, off)
255 * -1 -- PWM is always at 100%
256 * -2 -- PWM responds to manual control
259 static int lm85_zone_map[] = { 1, 2, 3, -1, 0, 23, 123, -2 };
260 #define ZONE_FROM_REG(val) (lm85_zone_map[((val)>>5)&0x07])
262 static int ZONE_TO_REG( int zone )
264 int i;
266 for( i = 0 ; i <= 7 ; ++i )
267 if( zone == lm85_zone_map[i] )
268 break ;
269 if( i > 7 ) /* Not found. */
270 i = 3; /* Always 100% */
271 return( (i & 0x07)<<5 );
274 #define HYST_TO_REG(val) (SENSORS_LIMIT(((val)+500)/1000,0,15))
275 #define HYST_FROM_REG(val) ((val)*1000)
277 #define OFFSET_TO_REG(val) (SENSORS_LIMIT((val)/25,-127,127))
278 #define OFFSET_FROM_REG(val) ((val)*25)
280 #define PPR_MASK(fan) (0x03<<(fan *2))
281 #define PPR_TO_REG(val,fan) (SENSORS_LIMIT((val)-1,0,3)<<(fan *2))
282 #define PPR_FROM_REG(val,fan) ((((val)>>(fan * 2))&0x03)+1)
284 /* i2c-vid.h defines vid_from_reg() */
285 #define VID_FROM_REG(val,vrm) (vid_from_reg((val),(vrm)))
287 #define ALARMS_FROM_REG(val) (val)
289 /* Unlike some other drivers we DO NOT set initial limits. Use
290 * the config file to set limits. Some users have reported
291 * motherboards shutting down when we set limits in a previous
292 * version of the driver.
295 /* Chip sampling rates
297 * Some sensors are not updated more frequently than once per second
298 * so it doesn't make sense to read them more often than that.
299 * We cache the results and return the saved data if the driver
300 * is called again before a second has elapsed.
302 * Also, there is significant configuration data for this chip
303 * given the automatic PWM fan control that is possible. There
304 * are about 47 bytes of config data to only 22 bytes of actual
305 * readings. So, we keep the config data up to date in the cache
306 * when it is written and only sample it once every 1 *minute*
308 #define LM85_DATA_INTERVAL (HZ + HZ / 2)
309 #define LM85_CONFIG_INTERVAL (1 * 60 * HZ)
311 /* For each registered LM85, we need to keep some data in memory. That
312 data is pointed to by lm85_list[NR]->data. The structure itself is
313 dynamically allocated, at the same time when a new lm85 client is
314 allocated. */
316 /* LM85 can automatically adjust fan speeds based on temperature
317 * This structure encapsulates an entire Zone config. There are
318 * three zones (one for each temperature input) on the lm85
320 struct lm85_zone {
321 s8 limit; /* Low temp limit */
322 u8 hyst; /* Low limit hysteresis. (0-15) */
323 u8 range; /* Temp range, encoded */
324 s8 critical; /* "All fans ON" temp limit */
325 u8 off_desired; /* Actual "off" temperature specified. Preserved
326 * to prevent "drift" as other autofan control
327 * values change.
329 u8 max_desired; /* Actual "max" temperature specified. Preserved
330 * to prevent "drift" as other autofan control
331 * values change.
335 struct lm85_autofan {
336 u8 config; /* Register value */
337 u8 freq; /* PWM frequency, encoded */
338 u8 min_pwm; /* Minimum PWM value, encoded */
339 u8 min_off; /* Min PWM or OFF below "limit", flag */
342 struct lm85_data {
343 struct i2c_client client;
344 struct semaphore lock;
345 enum chips type;
347 struct semaphore update_lock;
348 int valid; /* !=0 if following fields are valid */
349 unsigned long last_reading; /* In jiffies */
350 unsigned long last_config; /* In jiffies */
352 u8 in[8]; /* Register value */
353 u8 in_max[8]; /* Register value */
354 u8 in_min[8]; /* Register value */
355 s8 temp[3]; /* Register value */
356 s8 temp_min[3]; /* Register value */
357 s8 temp_max[3]; /* Register value */
358 s8 temp_offset[3]; /* Register value */
359 u16 fan[4]; /* Register value */
360 u16 fan_min[4]; /* Register value */
361 u8 pwm[3]; /* Register value */
362 u8 spinup_ctl; /* Register encoding, combined */
363 u8 tach_mode; /* Register encoding, combined */
364 u8 temp_ext[3]; /* Decoded values */
365 u8 in_ext[8]; /* Decoded values */
366 u8 adc_scale; /* ADC Extended bits scaling factor */
367 u8 fan_ppr; /* Register value */
368 u8 smooth[3]; /* Register encoding */
369 u8 vid; /* Register value */
370 u8 vrm; /* VRM version */
371 u8 syncpwm3; /* Saved PWM3 for TACH 2,3,4 config */
372 u8 oppoint[3]; /* Register value */
373 u16 tmin_ctl; /* Register value */
374 unsigned long therm_total; /* Cummulative therm count */
375 u8 therm_limit; /* Register value */
376 u32 alarms; /* Register encoding, combined */
377 struct lm85_autofan autofan[3];
378 struct lm85_zone zone[3];
381 static int lm85_attach_adapter(struct i2c_adapter *adapter);
382 static int lm85_detect(struct i2c_adapter *adapter, int address,
383 int kind);
384 static int lm85_detach_client(struct i2c_client *client);
386 static int lm85_read_value(struct i2c_client *client, u8 register);
387 static int lm85_write_value(struct i2c_client *client, u8 register, int value);
388 static struct lm85_data *lm85_update_device(struct device *dev);
389 static void lm85_init_client(struct i2c_client *client);
392 static struct i2c_driver lm85_driver = {
393 .owner = THIS_MODULE,
394 .name = "lm85",
395 .id = I2C_DRIVERID_LM85,
396 .flags = I2C_DF_NOTIFY,
397 .attach_adapter = lm85_attach_adapter,
398 .detach_client = lm85_detach_client,
402 /* 4 Fans */
403 static ssize_t show_fan(struct device *dev, char *buf, int nr)
405 struct lm85_data *data = lm85_update_device(dev);
406 return sprintf(buf,"%d\n", FAN_FROM_REG(data->fan[nr]) );
408 static ssize_t show_fan_min(struct device *dev, char *buf, int nr)
410 struct lm85_data *data = lm85_update_device(dev);
411 return sprintf(buf,"%d\n", FAN_FROM_REG(data->fan_min[nr]) );
413 static ssize_t set_fan_min(struct device *dev, const char *buf,
414 size_t count, int nr)
416 struct i2c_client *client = to_i2c_client(dev);
417 struct lm85_data *data = i2c_get_clientdata(client);
418 long val = simple_strtol(buf, NULL, 10);
420 down(&data->update_lock);
421 data->fan_min[nr] = FAN_TO_REG(val);
422 lm85_write_value(client, LM85_REG_FAN_MIN(nr), data->fan_min[nr]);
423 up(&data->update_lock);
424 return count;
427 #define show_fan_offset(offset) \
428 static ssize_t show_fan_##offset (struct device *dev, struct device_attribute *attr, char *buf) \
430 return show_fan(dev, buf, offset - 1); \
432 static ssize_t show_fan_##offset##_min (struct device *dev, struct device_attribute *attr, char *buf) \
434 return show_fan_min(dev, buf, offset - 1); \
436 static ssize_t set_fan_##offset##_min (struct device *dev, struct device_attribute *attr, \
437 const char *buf, size_t count) \
439 return set_fan_min(dev, buf, count, offset - 1); \
441 static DEVICE_ATTR(fan##offset##_input, S_IRUGO, show_fan_##offset, \
442 NULL); \
443 static DEVICE_ATTR(fan##offset##_min, S_IRUGO | S_IWUSR, \
444 show_fan_##offset##_min, set_fan_##offset##_min);
446 show_fan_offset(1);
447 show_fan_offset(2);
448 show_fan_offset(3);
449 show_fan_offset(4);
451 /* vid, vrm, alarms */
453 static ssize_t show_vid_reg(struct device *dev, struct device_attribute *attr, char *buf)
455 struct lm85_data *data = lm85_update_device(dev);
456 return sprintf(buf, "%ld\n", (long) vid_from_reg(data->vid, data->vrm));
459 static DEVICE_ATTR(cpu0_vid, S_IRUGO, show_vid_reg, NULL);
461 static ssize_t show_vrm_reg(struct device *dev, struct device_attribute *attr, char *buf)
463 struct lm85_data *data = lm85_update_device(dev);
464 return sprintf(buf, "%ld\n", (long) data->vrm);
467 static ssize_t store_vrm_reg(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
469 struct i2c_client *client = to_i2c_client(dev);
470 struct lm85_data *data = i2c_get_clientdata(client);
471 u32 val;
473 val = simple_strtoul(buf, NULL, 10);
474 data->vrm = val;
475 return count;
478 static DEVICE_ATTR(vrm, S_IRUGO | S_IWUSR, show_vrm_reg, store_vrm_reg);
480 static ssize_t show_alarms_reg(struct device *dev, struct device_attribute *attr, char *buf)
482 struct lm85_data *data = lm85_update_device(dev);
483 return sprintf(buf, "%ld\n", (long) ALARMS_FROM_REG(data->alarms));
486 static DEVICE_ATTR(alarms, S_IRUGO, show_alarms_reg, NULL);
488 /* pwm */
490 static ssize_t show_pwm(struct device *dev, char *buf, int nr)
492 struct lm85_data *data = lm85_update_device(dev);
493 return sprintf(buf,"%d\n", PWM_FROM_REG(data->pwm[nr]) );
495 static ssize_t set_pwm(struct device *dev, const char *buf,
496 size_t count, int nr)
498 struct i2c_client *client = to_i2c_client(dev);
499 struct lm85_data *data = i2c_get_clientdata(client);
500 long val = simple_strtol(buf, NULL, 10);
502 down(&data->update_lock);
503 data->pwm[nr] = PWM_TO_REG(val);
504 lm85_write_value(client, LM85_REG_PWM(nr), data->pwm[nr]);
505 up(&data->update_lock);
506 return count;
508 static ssize_t show_pwm_enable(struct device *dev, char *buf, int nr)
510 struct lm85_data *data = lm85_update_device(dev);
511 int pwm_zone;
513 pwm_zone = ZONE_FROM_REG(data->autofan[nr].config);
514 return sprintf(buf,"%d\n", (pwm_zone != 0 && pwm_zone != -1) );
517 #define show_pwm_reg(offset) \
518 static ssize_t show_pwm_##offset (struct device *dev, struct device_attribute *attr, char *buf) \
520 return show_pwm(dev, buf, offset - 1); \
522 static ssize_t set_pwm_##offset (struct device *dev, struct device_attribute *attr, \
523 const char *buf, size_t count) \
525 return set_pwm(dev, buf, count, offset - 1); \
527 static ssize_t show_pwm_enable##offset (struct device *dev, struct device_attribute *attr, char *buf) \
529 return show_pwm_enable(dev, buf, offset - 1); \
531 static DEVICE_ATTR(pwm##offset, S_IRUGO | S_IWUSR, \
532 show_pwm_##offset, set_pwm_##offset); \
533 static DEVICE_ATTR(pwm##offset##_enable, S_IRUGO, \
534 show_pwm_enable##offset, NULL);
536 show_pwm_reg(1);
537 show_pwm_reg(2);
538 show_pwm_reg(3);
540 /* Voltages */
542 static ssize_t show_in(struct device *dev, char *buf, int nr)
544 struct lm85_data *data = lm85_update_device(dev);
545 return sprintf( buf, "%d\n", INSEXT_FROM_REG(nr,
546 data->in[nr],
547 data->in_ext[nr],
548 data->adc_scale) );
550 static ssize_t show_in_min(struct device *dev, char *buf, int nr)
552 struct lm85_data *data = lm85_update_device(dev);
553 return sprintf(buf,"%d\n", INS_FROM_REG(nr, data->in_min[nr]) );
555 static ssize_t set_in_min(struct device *dev, const char *buf,
556 size_t count, int nr)
558 struct i2c_client *client = to_i2c_client(dev);
559 struct lm85_data *data = i2c_get_clientdata(client);
560 long val = simple_strtol(buf, NULL, 10);
562 down(&data->update_lock);
563 data->in_min[nr] = INS_TO_REG(nr, val);
564 lm85_write_value(client, LM85_REG_IN_MIN(nr), data->in_min[nr]);
565 up(&data->update_lock);
566 return count;
568 static ssize_t show_in_max(struct device *dev, char *buf, int nr)
570 struct lm85_data *data = lm85_update_device(dev);
571 return sprintf(buf,"%d\n", INS_FROM_REG(nr, data->in_max[nr]) );
573 static ssize_t set_in_max(struct device *dev, const char *buf,
574 size_t count, int nr)
576 struct i2c_client *client = to_i2c_client(dev);
577 struct lm85_data *data = i2c_get_clientdata(client);
578 long val = simple_strtol(buf, NULL, 10);
580 down(&data->update_lock);
581 data->in_max[nr] = INS_TO_REG(nr, val);
582 lm85_write_value(client, LM85_REG_IN_MAX(nr), data->in_max[nr]);
583 up(&data->update_lock);
584 return count;
586 #define show_in_reg(offset) \
587 static ssize_t show_in_##offset (struct device *dev, struct device_attribute *attr, char *buf) \
589 return show_in(dev, buf, offset); \
591 static ssize_t show_in_##offset##_min (struct device *dev, struct device_attribute *attr, char *buf) \
593 return show_in_min(dev, buf, offset); \
595 static ssize_t show_in_##offset##_max (struct device *dev, struct device_attribute *attr, char *buf) \
597 return show_in_max(dev, buf, offset); \
599 static ssize_t set_in_##offset##_min (struct device *dev, struct device_attribute *attr, \
600 const char *buf, size_t count) \
602 return set_in_min(dev, buf, count, offset); \
604 static ssize_t set_in_##offset##_max (struct device *dev, struct device_attribute *attr, \
605 const char *buf, size_t count) \
607 return set_in_max(dev, buf, count, offset); \
609 static DEVICE_ATTR(in##offset##_input, S_IRUGO, show_in_##offset, \
610 NULL); \
611 static DEVICE_ATTR(in##offset##_min, S_IRUGO | S_IWUSR, \
612 show_in_##offset##_min, set_in_##offset##_min); \
613 static DEVICE_ATTR(in##offset##_max, S_IRUGO | S_IWUSR, \
614 show_in_##offset##_max, set_in_##offset##_max);
616 show_in_reg(0);
617 show_in_reg(1);
618 show_in_reg(2);
619 show_in_reg(3);
620 show_in_reg(4);
622 /* Temps */
624 static ssize_t show_temp(struct device *dev, char *buf, int nr)
626 struct lm85_data *data = lm85_update_device(dev);
627 return sprintf(buf,"%d\n", TEMPEXT_FROM_REG(data->temp[nr],
628 data->temp_ext[nr],
629 data->adc_scale) );
631 static ssize_t show_temp_min(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_min[nr]) );
636 static ssize_t set_temp_min(struct device *dev, const char *buf,
637 size_t count, int nr)
639 struct i2c_client *client = to_i2c_client(dev);
640 struct lm85_data *data = i2c_get_clientdata(client);
641 long val = simple_strtol(buf, NULL, 10);
643 down(&data->update_lock);
644 data->temp_min[nr] = TEMP_TO_REG(val);
645 lm85_write_value(client, LM85_REG_TEMP_MIN(nr), data->temp_min[nr]);
646 up(&data->update_lock);
647 return count;
649 static ssize_t show_temp_max(struct device *dev, char *buf, int nr)
651 struct lm85_data *data = lm85_update_device(dev);
652 return sprintf(buf,"%d\n", TEMP_FROM_REG(data->temp_max[nr]) );
654 static ssize_t set_temp_max(struct device *dev, const char *buf,
655 size_t count, int nr)
657 struct i2c_client *client = to_i2c_client(dev);
658 struct lm85_data *data = i2c_get_clientdata(client);
659 long val = simple_strtol(buf, NULL, 10);
661 down(&data->update_lock);
662 data->temp_max[nr] = TEMP_TO_REG(val);
663 lm85_write_value(client, LM85_REG_TEMP_MAX(nr), data->temp_max[nr]);
664 up(&data->update_lock);
665 return count;
667 #define show_temp_reg(offset) \
668 static ssize_t show_temp_##offset (struct device *dev, struct device_attribute *attr, char *buf) \
670 return show_temp(dev, buf, offset - 1); \
672 static ssize_t show_temp_##offset##_min (struct device *dev, struct device_attribute *attr, char *buf) \
674 return show_temp_min(dev, buf, offset - 1); \
676 static ssize_t show_temp_##offset##_max (struct device *dev, struct device_attribute *attr, char *buf) \
678 return show_temp_max(dev, buf, offset - 1); \
680 static ssize_t set_temp_##offset##_min (struct device *dev, struct device_attribute *attr, \
681 const char *buf, size_t count) \
683 return set_temp_min(dev, buf, count, offset - 1); \
685 static ssize_t set_temp_##offset##_max (struct device *dev, struct device_attribute *attr, \
686 const char *buf, size_t count) \
688 return set_temp_max(dev, buf, count, offset - 1); \
690 static DEVICE_ATTR(temp##offset##_input, S_IRUGO, show_temp_##offset, \
691 NULL); \
692 static DEVICE_ATTR(temp##offset##_min, S_IRUGO | S_IWUSR, \
693 show_temp_##offset##_min, set_temp_##offset##_min); \
694 static DEVICE_ATTR(temp##offset##_max, S_IRUGO | S_IWUSR, \
695 show_temp_##offset##_max, set_temp_##offset##_max);
697 show_temp_reg(1);
698 show_temp_reg(2);
699 show_temp_reg(3);
702 /* Automatic PWM control */
704 static ssize_t show_pwm_auto_channels(struct device *dev, char *buf, int nr)
706 struct lm85_data *data = lm85_update_device(dev);
707 return sprintf(buf,"%d\n", ZONE_FROM_REG(data->autofan[nr].config));
709 static ssize_t set_pwm_auto_channels(struct device *dev, const char *buf,
710 size_t count, int nr)
712 struct i2c_client *client = to_i2c_client(dev);
713 struct lm85_data *data = i2c_get_clientdata(client);
714 long val = simple_strtol(buf, NULL, 10);
716 down(&data->update_lock);
717 data->autofan[nr].config = (data->autofan[nr].config & (~0xe0))
718 | ZONE_TO_REG(val) ;
719 lm85_write_value(client, LM85_REG_AFAN_CONFIG(nr),
720 data->autofan[nr].config);
721 up(&data->update_lock);
722 return count;
724 static ssize_t show_pwm_auto_pwm_min(struct device *dev, char *buf, int nr)
726 struct lm85_data *data = lm85_update_device(dev);
727 return sprintf(buf,"%d\n", PWM_FROM_REG(data->autofan[nr].min_pwm));
729 static ssize_t set_pwm_auto_pwm_min(struct device *dev, const char *buf,
730 size_t count, int nr)
732 struct i2c_client *client = to_i2c_client(dev);
733 struct lm85_data *data = i2c_get_clientdata(client);
734 long val = simple_strtol(buf, NULL, 10);
736 down(&data->update_lock);
737 data->autofan[nr].min_pwm = PWM_TO_REG(val);
738 lm85_write_value(client, LM85_REG_AFAN_MINPWM(nr),
739 data->autofan[nr].min_pwm);
740 up(&data->update_lock);
741 return count;
743 static ssize_t show_pwm_auto_pwm_minctl(struct device *dev, char *buf, int nr)
745 struct lm85_data *data = lm85_update_device(dev);
746 return sprintf(buf,"%d\n", data->autofan[nr].min_off);
748 static ssize_t set_pwm_auto_pwm_minctl(struct device *dev, const char *buf,
749 size_t count, int nr)
751 struct i2c_client *client = to_i2c_client(dev);
752 struct lm85_data *data = i2c_get_clientdata(client);
753 long val = simple_strtol(buf, NULL, 10);
755 down(&data->update_lock);
756 data->autofan[nr].min_off = val;
757 lm85_write_value(client, LM85_REG_AFAN_SPIKE1, data->smooth[0]
758 | data->syncpwm3
759 | (data->autofan[0].min_off ? 0x20 : 0)
760 | (data->autofan[1].min_off ? 0x40 : 0)
761 | (data->autofan[2].min_off ? 0x80 : 0)
763 up(&data->update_lock);
764 return count;
766 static ssize_t show_pwm_auto_pwm_freq(struct device *dev, char *buf, int nr)
768 struct lm85_data *data = lm85_update_device(dev);
769 return sprintf(buf,"%d\n", FREQ_FROM_REG(data->autofan[nr].freq));
771 static ssize_t set_pwm_auto_pwm_freq(struct device *dev, const char *buf,
772 size_t count, int nr)
774 struct i2c_client *client = to_i2c_client(dev);
775 struct lm85_data *data = i2c_get_clientdata(client);
776 long val = simple_strtol(buf, NULL, 10);
778 down(&data->update_lock);
779 data->autofan[nr].freq = FREQ_TO_REG(val);
780 lm85_write_value(client, LM85_REG_AFAN_RANGE(nr),
781 (data->zone[nr].range << 4)
782 | data->autofan[nr].freq
784 up(&data->update_lock);
785 return count;
787 #define pwm_auto(offset) \
788 static ssize_t show_pwm##offset##_auto_channels (struct device *dev, struct device_attribute *attr, \
789 char *buf) \
791 return show_pwm_auto_channels(dev, buf, offset - 1); \
793 static ssize_t set_pwm##offset##_auto_channels (struct device *dev, struct device_attribute *attr, \
794 const char *buf, size_t count) \
796 return set_pwm_auto_channels(dev, buf, count, offset - 1); \
798 static ssize_t show_pwm##offset##_auto_pwm_min (struct device *dev, struct device_attribute *attr, \
799 char *buf) \
801 return show_pwm_auto_pwm_min(dev, buf, offset - 1); \
803 static ssize_t set_pwm##offset##_auto_pwm_min (struct device *dev, struct device_attribute *attr, \
804 const char *buf, size_t count) \
806 return set_pwm_auto_pwm_min(dev, buf, count, offset - 1); \
808 static ssize_t show_pwm##offset##_auto_pwm_minctl (struct device *dev, struct device_attribute *attr, \
809 char *buf) \
811 return show_pwm_auto_pwm_minctl(dev, buf, offset - 1); \
813 static ssize_t set_pwm##offset##_auto_pwm_minctl (struct device *dev, struct device_attribute *attr, \
814 const char *buf, size_t count) \
816 return set_pwm_auto_pwm_minctl(dev, buf, count, offset - 1); \
818 static ssize_t show_pwm##offset##_auto_pwm_freq (struct device *dev, struct device_attribute *attr, \
819 char *buf) \
821 return show_pwm_auto_pwm_freq(dev, buf, offset - 1); \
823 static ssize_t set_pwm##offset##_auto_pwm_freq(struct device *dev, struct device_attribute *attr, \
824 const char *buf, size_t count) \
826 return set_pwm_auto_pwm_freq(dev, buf, count, offset - 1); \
828 static DEVICE_ATTR(pwm##offset##_auto_channels, S_IRUGO | S_IWUSR, \
829 show_pwm##offset##_auto_channels, \
830 set_pwm##offset##_auto_channels); \
831 static DEVICE_ATTR(pwm##offset##_auto_pwm_min, S_IRUGO | S_IWUSR, \
832 show_pwm##offset##_auto_pwm_min, \
833 set_pwm##offset##_auto_pwm_min); \
834 static DEVICE_ATTR(pwm##offset##_auto_pwm_minctl, S_IRUGO | S_IWUSR, \
835 show_pwm##offset##_auto_pwm_minctl, \
836 set_pwm##offset##_auto_pwm_minctl); \
837 static DEVICE_ATTR(pwm##offset##_auto_pwm_freq, S_IRUGO | S_IWUSR, \
838 show_pwm##offset##_auto_pwm_freq, \
839 set_pwm##offset##_auto_pwm_freq);
840 pwm_auto(1);
841 pwm_auto(2);
842 pwm_auto(3);
844 /* Temperature settings for automatic PWM control */
846 static ssize_t show_temp_auto_temp_off(struct device *dev, char *buf, int nr)
848 struct lm85_data *data = lm85_update_device(dev);
849 return sprintf(buf,"%d\n", TEMP_FROM_REG(data->zone[nr].limit) -
850 HYST_FROM_REG(data->zone[nr].hyst));
852 static ssize_t set_temp_auto_temp_off(struct device *dev, const char *buf,
853 size_t count, int nr)
855 struct i2c_client *client = to_i2c_client(dev);
856 struct lm85_data *data = i2c_get_clientdata(client);
857 int min;
858 long val = simple_strtol(buf, NULL, 10);
860 down(&data->update_lock);
861 min = TEMP_FROM_REG(data->zone[nr].limit);
862 data->zone[nr].off_desired = TEMP_TO_REG(val);
863 data->zone[nr].hyst = HYST_TO_REG(min - val);
864 if ( nr == 0 || nr == 1 ) {
865 lm85_write_value(client, LM85_REG_AFAN_HYST1,
866 (data->zone[0].hyst << 4)
867 | data->zone[1].hyst
869 } else {
870 lm85_write_value(client, LM85_REG_AFAN_HYST2,
871 (data->zone[2].hyst << 4)
874 up(&data->update_lock);
875 return count;
877 static ssize_t show_temp_auto_temp_min(struct device *dev, char *buf, int nr)
879 struct lm85_data *data = lm85_update_device(dev);
880 return sprintf(buf,"%d\n", TEMP_FROM_REG(data->zone[nr].limit) );
882 static ssize_t set_temp_auto_temp_min(struct device *dev, const char *buf,
883 size_t count, int nr)
885 struct i2c_client *client = to_i2c_client(dev);
886 struct lm85_data *data = i2c_get_clientdata(client);
887 long val = simple_strtol(buf, NULL, 10);
889 down(&data->update_lock);
890 data->zone[nr].limit = TEMP_TO_REG(val);
891 lm85_write_value(client, LM85_REG_AFAN_LIMIT(nr),
892 data->zone[nr].limit);
894 /* Update temp_auto_max and temp_auto_range */
895 data->zone[nr].range = RANGE_TO_REG(
896 TEMP_FROM_REG(data->zone[nr].max_desired) -
897 TEMP_FROM_REG(data->zone[nr].limit));
898 lm85_write_value(client, LM85_REG_AFAN_RANGE(nr),
899 ((data->zone[nr].range & 0x0f) << 4)
900 | (data->autofan[nr].freq & 0x07));
902 /* Update temp_auto_hyst and temp_auto_off */
903 data->zone[nr].hyst = HYST_TO_REG(TEMP_FROM_REG(
904 data->zone[nr].limit) - TEMP_FROM_REG(
905 data->zone[nr].off_desired));
906 if ( nr == 0 || nr == 1 ) {
907 lm85_write_value(client, LM85_REG_AFAN_HYST1,
908 (data->zone[0].hyst << 4)
909 | data->zone[1].hyst
911 } else {
912 lm85_write_value(client, LM85_REG_AFAN_HYST2,
913 (data->zone[2].hyst << 4)
916 up(&data->update_lock);
917 return count;
919 static ssize_t show_temp_auto_temp_max(struct device *dev, char *buf, int nr)
921 struct lm85_data *data = lm85_update_device(dev);
922 return sprintf(buf,"%d\n", TEMP_FROM_REG(data->zone[nr].limit) +
923 RANGE_FROM_REG(data->zone[nr].range));
925 static ssize_t set_temp_auto_temp_max(struct device *dev, const char *buf,
926 size_t count, int nr)
928 struct i2c_client *client = to_i2c_client(dev);
929 struct lm85_data *data = i2c_get_clientdata(client);
930 int min;
931 long val = simple_strtol(buf, NULL, 10);
933 down(&data->update_lock);
934 min = TEMP_FROM_REG(data->zone[nr].limit);
935 data->zone[nr].max_desired = TEMP_TO_REG(val);
936 data->zone[nr].range = RANGE_TO_REG(
937 val - min);
938 lm85_write_value(client, LM85_REG_AFAN_RANGE(nr),
939 ((data->zone[nr].range & 0x0f) << 4)
940 | (data->autofan[nr].freq & 0x07));
941 up(&data->update_lock);
942 return count;
944 static ssize_t show_temp_auto_temp_crit(struct device *dev, char *buf, int nr)
946 struct lm85_data *data = lm85_update_device(dev);
947 return sprintf(buf,"%d\n", TEMP_FROM_REG(data->zone[nr].critical));
949 static ssize_t set_temp_auto_temp_crit(struct device *dev, const char *buf,
950 size_t count, int nr)
952 struct i2c_client *client = to_i2c_client(dev);
953 struct lm85_data *data = i2c_get_clientdata(client);
954 long val = simple_strtol(buf, NULL, 10);
956 down(&data->update_lock);
957 data->zone[nr].critical = TEMP_TO_REG(val);
958 lm85_write_value(client, LM85_REG_AFAN_CRITICAL(nr),
959 data->zone[nr].critical);
960 up(&data->update_lock);
961 return count;
963 #define temp_auto(offset) \
964 static ssize_t show_temp##offset##_auto_temp_off (struct device *dev, struct device_attribute *attr, \
965 char *buf) \
967 return show_temp_auto_temp_off(dev, buf, offset - 1); \
969 static ssize_t set_temp##offset##_auto_temp_off (struct device *dev, struct device_attribute *attr, \
970 const char *buf, size_t count) \
972 return set_temp_auto_temp_off(dev, buf, count, offset - 1); \
974 static ssize_t show_temp##offset##_auto_temp_min (struct device *dev, struct device_attribute *attr, \
975 char *buf) \
977 return show_temp_auto_temp_min(dev, buf, offset - 1); \
979 static ssize_t set_temp##offset##_auto_temp_min (struct device *dev, struct device_attribute *attr, \
980 const char *buf, size_t count) \
982 return set_temp_auto_temp_min(dev, buf, count, offset - 1); \
984 static ssize_t show_temp##offset##_auto_temp_max (struct device *dev, struct device_attribute *attr, \
985 char *buf) \
987 return show_temp_auto_temp_max(dev, buf, offset - 1); \
989 static ssize_t set_temp##offset##_auto_temp_max (struct device *dev, struct device_attribute *attr, \
990 const char *buf, size_t count) \
992 return set_temp_auto_temp_max(dev, buf, count, offset - 1); \
994 static ssize_t show_temp##offset##_auto_temp_crit (struct device *dev, struct device_attribute *attr, \
995 char *buf) \
997 return show_temp_auto_temp_crit(dev, buf, offset - 1); \
999 static ssize_t set_temp##offset##_auto_temp_crit (struct device *dev, struct device_attribute *attr, \
1000 const char *buf, size_t count) \
1002 return set_temp_auto_temp_crit(dev, buf, count, offset - 1); \
1004 static DEVICE_ATTR(temp##offset##_auto_temp_off, S_IRUGO | S_IWUSR, \
1005 show_temp##offset##_auto_temp_off, \
1006 set_temp##offset##_auto_temp_off); \
1007 static DEVICE_ATTR(temp##offset##_auto_temp_min, S_IRUGO | S_IWUSR, \
1008 show_temp##offset##_auto_temp_min, \
1009 set_temp##offset##_auto_temp_min); \
1010 static DEVICE_ATTR(temp##offset##_auto_temp_max, S_IRUGO | S_IWUSR, \
1011 show_temp##offset##_auto_temp_max, \
1012 set_temp##offset##_auto_temp_max); \
1013 static DEVICE_ATTR(temp##offset##_auto_temp_crit, S_IRUGO | S_IWUSR, \
1014 show_temp##offset##_auto_temp_crit, \
1015 set_temp##offset##_auto_temp_crit);
1016 temp_auto(1);
1017 temp_auto(2);
1018 temp_auto(3);
1020 int lm85_attach_adapter(struct i2c_adapter *adapter)
1022 if (!(adapter->class & I2C_CLASS_HWMON))
1023 return 0;
1024 return i2c_detect(adapter, &addr_data, lm85_detect);
1027 int lm85_detect(struct i2c_adapter *adapter, int address,
1028 int kind)
1030 int company, verstep ;
1031 struct i2c_client *new_client = NULL;
1032 struct lm85_data *data;
1033 int err = 0;
1034 const char *type_name = "";
1036 if (i2c_is_isa_adapter(adapter)) {
1037 /* This chip has no ISA interface */
1038 goto ERROR0 ;
1041 if (!i2c_check_functionality(adapter,
1042 I2C_FUNC_SMBUS_BYTE_DATA)) {
1043 /* We need to be able to do byte I/O */
1044 goto ERROR0 ;
1047 /* OK. For now, we presume we have a valid client. We now create the
1048 client structure, even though we cannot fill it completely yet.
1049 But it allows us to access lm85_{read,write}_value. */
1051 if (!(data = kmalloc(sizeof(struct lm85_data), GFP_KERNEL))) {
1052 err = -ENOMEM;
1053 goto ERROR0;
1055 memset(data, 0, sizeof(struct lm85_data));
1057 new_client = &data->client;
1058 i2c_set_clientdata(new_client, data);
1059 new_client->addr = address;
1060 new_client->adapter = adapter;
1061 new_client->driver = &lm85_driver;
1062 new_client->flags = 0;
1064 /* Now, we do the remaining detection. */
1066 company = lm85_read_value(new_client, LM85_REG_COMPANY);
1067 verstep = lm85_read_value(new_client, LM85_REG_VERSTEP);
1069 dev_dbg(&adapter->dev, "Detecting device at %d,0x%02x with"
1070 " COMPANY: 0x%02x and VERSTEP: 0x%02x\n",
1071 i2c_adapter_id(new_client->adapter), new_client->addr,
1072 company, verstep);
1074 /* If auto-detecting, Determine the chip type. */
1075 if (kind <= 0) {
1076 dev_dbg(&adapter->dev, "Autodetecting device at %d,0x%02x ...\n",
1077 i2c_adapter_id(adapter), address );
1078 if( company == LM85_COMPANY_NATIONAL
1079 && verstep == LM85_VERSTEP_LM85C ) {
1080 kind = lm85c ;
1081 } else if( company == LM85_COMPANY_NATIONAL
1082 && verstep == LM85_VERSTEP_LM85B ) {
1083 kind = lm85b ;
1084 } else if( company == LM85_COMPANY_NATIONAL
1085 && (verstep & LM85_VERSTEP_VMASK) == LM85_VERSTEP_GENERIC ) {
1086 dev_err(&adapter->dev, "Unrecognized version/stepping 0x%02x"
1087 " Defaulting to LM85.\n", verstep);
1088 kind = any_chip ;
1089 } else if( company == LM85_COMPANY_ANALOG_DEV
1090 && verstep == LM85_VERSTEP_ADM1027 ) {
1091 kind = adm1027 ;
1092 } else if( company == LM85_COMPANY_ANALOG_DEV
1093 && (verstep == LM85_VERSTEP_ADT7463
1094 || verstep == LM85_VERSTEP_ADT7463C) ) {
1095 kind = adt7463 ;
1096 } else if( company == LM85_COMPANY_ANALOG_DEV
1097 && (verstep & LM85_VERSTEP_VMASK) == LM85_VERSTEP_GENERIC ) {
1098 dev_err(&adapter->dev, "Unrecognized version/stepping 0x%02x"
1099 " Defaulting to Generic LM85.\n", verstep );
1100 kind = any_chip ;
1101 } else if( company == LM85_COMPANY_SMSC
1102 && (verstep == LM85_VERSTEP_EMC6D100_A0
1103 || verstep == LM85_VERSTEP_EMC6D100_A1) ) {
1104 /* Unfortunately, we can't tell a '100 from a '101
1105 * from the registers. Since a '101 is a '100
1106 * in a package with fewer pins and therefore no
1107 * 3.3V, 1.5V or 1.8V inputs, perhaps if those
1108 * inputs read 0, then it's a '101.
1110 kind = emc6d100 ;
1111 } else if( company == LM85_COMPANY_SMSC
1112 && verstep == LM85_VERSTEP_EMC6D102) {
1113 kind = emc6d102 ;
1114 } else if( company == LM85_COMPANY_SMSC
1115 && (verstep & LM85_VERSTEP_VMASK) == LM85_VERSTEP_GENERIC) {
1116 dev_err(&adapter->dev, "lm85: Detected SMSC chip\n");
1117 dev_err(&adapter->dev, "lm85: Unrecognized version/stepping 0x%02x"
1118 " Defaulting to Generic LM85.\n", verstep );
1119 kind = any_chip ;
1120 } else if( kind == any_chip
1121 && (verstep & LM85_VERSTEP_VMASK) == LM85_VERSTEP_GENERIC) {
1122 dev_err(&adapter->dev, "Generic LM85 Version 6 detected\n");
1123 /* Leave kind as "any_chip" */
1124 } else {
1125 dev_dbg(&adapter->dev, "Autodetection failed\n");
1126 /* Not an LM85 ... */
1127 if( kind == any_chip ) { /* User used force=x,y */
1128 dev_err(&adapter->dev, "Generic LM85 Version 6 not"
1129 " found at %d,0x%02x. Try force_lm85c.\n",
1130 i2c_adapter_id(adapter), address );
1132 err = 0 ;
1133 goto ERROR1;
1137 /* Fill in the chip specific driver values */
1138 if ( kind == any_chip ) {
1139 type_name = "lm85";
1140 } else if ( kind == lm85b ) {
1141 type_name = "lm85b";
1142 } else if ( kind == lm85c ) {
1143 type_name = "lm85c";
1144 } else if ( kind == adm1027 ) {
1145 type_name = "adm1027";
1146 } else if ( kind == adt7463 ) {
1147 type_name = "adt7463";
1148 } else if ( kind == emc6d100){
1149 type_name = "emc6d100";
1150 } else if ( kind == emc6d102 ) {
1151 type_name = "emc6d102";
1153 strlcpy(new_client->name, type_name, I2C_NAME_SIZE);
1155 /* Fill in the remaining client fields */
1156 data->type = kind;
1157 data->valid = 0;
1158 init_MUTEX(&data->update_lock);
1160 /* Tell the I2C layer a new client has arrived */
1161 if ((err = i2c_attach_client(new_client)))
1162 goto ERROR1;
1164 /* Set the VRM version */
1165 data->vrm = i2c_which_vrm();
1167 /* Initialize the LM85 chip */
1168 lm85_init_client(new_client);
1170 /* Register sysfs hooks */
1171 device_create_file(&new_client->dev, &dev_attr_fan1_input);
1172 device_create_file(&new_client->dev, &dev_attr_fan2_input);
1173 device_create_file(&new_client->dev, &dev_attr_fan3_input);
1174 device_create_file(&new_client->dev, &dev_attr_fan4_input);
1175 device_create_file(&new_client->dev, &dev_attr_fan1_min);
1176 device_create_file(&new_client->dev, &dev_attr_fan2_min);
1177 device_create_file(&new_client->dev, &dev_attr_fan3_min);
1178 device_create_file(&new_client->dev, &dev_attr_fan4_min);
1179 device_create_file(&new_client->dev, &dev_attr_pwm1);
1180 device_create_file(&new_client->dev, &dev_attr_pwm2);
1181 device_create_file(&new_client->dev, &dev_attr_pwm3);
1182 device_create_file(&new_client->dev, &dev_attr_pwm1_enable);
1183 device_create_file(&new_client->dev, &dev_attr_pwm2_enable);
1184 device_create_file(&new_client->dev, &dev_attr_pwm3_enable);
1185 device_create_file(&new_client->dev, &dev_attr_in0_input);
1186 device_create_file(&new_client->dev, &dev_attr_in1_input);
1187 device_create_file(&new_client->dev, &dev_attr_in2_input);
1188 device_create_file(&new_client->dev, &dev_attr_in3_input);
1189 device_create_file(&new_client->dev, &dev_attr_in4_input);
1190 device_create_file(&new_client->dev, &dev_attr_in0_min);
1191 device_create_file(&new_client->dev, &dev_attr_in1_min);
1192 device_create_file(&new_client->dev, &dev_attr_in2_min);
1193 device_create_file(&new_client->dev, &dev_attr_in3_min);
1194 device_create_file(&new_client->dev, &dev_attr_in4_min);
1195 device_create_file(&new_client->dev, &dev_attr_in0_max);
1196 device_create_file(&new_client->dev, &dev_attr_in1_max);
1197 device_create_file(&new_client->dev, &dev_attr_in2_max);
1198 device_create_file(&new_client->dev, &dev_attr_in3_max);
1199 device_create_file(&new_client->dev, &dev_attr_in4_max);
1200 device_create_file(&new_client->dev, &dev_attr_temp1_input);
1201 device_create_file(&new_client->dev, &dev_attr_temp2_input);
1202 device_create_file(&new_client->dev, &dev_attr_temp3_input);
1203 device_create_file(&new_client->dev, &dev_attr_temp1_min);
1204 device_create_file(&new_client->dev, &dev_attr_temp2_min);
1205 device_create_file(&new_client->dev, &dev_attr_temp3_min);
1206 device_create_file(&new_client->dev, &dev_attr_temp1_max);
1207 device_create_file(&new_client->dev, &dev_attr_temp2_max);
1208 device_create_file(&new_client->dev, &dev_attr_temp3_max);
1209 device_create_file(&new_client->dev, &dev_attr_vrm);
1210 device_create_file(&new_client->dev, &dev_attr_cpu0_vid);
1211 device_create_file(&new_client->dev, &dev_attr_alarms);
1212 device_create_file(&new_client->dev, &dev_attr_pwm1_auto_channels);
1213 device_create_file(&new_client->dev, &dev_attr_pwm2_auto_channels);
1214 device_create_file(&new_client->dev, &dev_attr_pwm3_auto_channels);
1215 device_create_file(&new_client->dev, &dev_attr_pwm1_auto_pwm_min);
1216 device_create_file(&new_client->dev, &dev_attr_pwm2_auto_pwm_min);
1217 device_create_file(&new_client->dev, &dev_attr_pwm3_auto_pwm_min);
1218 device_create_file(&new_client->dev, &dev_attr_pwm1_auto_pwm_minctl);
1219 device_create_file(&new_client->dev, &dev_attr_pwm2_auto_pwm_minctl);
1220 device_create_file(&new_client->dev, &dev_attr_pwm3_auto_pwm_minctl);
1221 device_create_file(&new_client->dev, &dev_attr_pwm1_auto_pwm_freq);
1222 device_create_file(&new_client->dev, &dev_attr_pwm2_auto_pwm_freq);
1223 device_create_file(&new_client->dev, &dev_attr_pwm3_auto_pwm_freq);
1224 device_create_file(&new_client->dev, &dev_attr_temp1_auto_temp_off);
1225 device_create_file(&new_client->dev, &dev_attr_temp2_auto_temp_off);
1226 device_create_file(&new_client->dev, &dev_attr_temp3_auto_temp_off);
1227 device_create_file(&new_client->dev, &dev_attr_temp1_auto_temp_min);
1228 device_create_file(&new_client->dev, &dev_attr_temp2_auto_temp_min);
1229 device_create_file(&new_client->dev, &dev_attr_temp3_auto_temp_min);
1230 device_create_file(&new_client->dev, &dev_attr_temp1_auto_temp_max);
1231 device_create_file(&new_client->dev, &dev_attr_temp2_auto_temp_max);
1232 device_create_file(&new_client->dev, &dev_attr_temp3_auto_temp_max);
1233 device_create_file(&new_client->dev, &dev_attr_temp1_auto_temp_crit);
1234 device_create_file(&new_client->dev, &dev_attr_temp2_auto_temp_crit);
1235 device_create_file(&new_client->dev, &dev_attr_temp3_auto_temp_crit);
1237 return 0;
1239 /* Error out and cleanup code */
1240 ERROR1:
1241 kfree(data);
1242 ERROR0:
1243 return err;
1246 int lm85_detach_client(struct i2c_client *client)
1248 i2c_detach_client(client);
1249 kfree(i2c_get_clientdata(client));
1250 return 0;
1254 int lm85_read_value(struct i2c_client *client, u8 reg)
1256 int res;
1258 /* What size location is it? */
1259 switch( reg ) {
1260 case LM85_REG_FAN(0) : /* Read WORD data */
1261 case LM85_REG_FAN(1) :
1262 case LM85_REG_FAN(2) :
1263 case LM85_REG_FAN(3) :
1264 case LM85_REG_FAN_MIN(0) :
1265 case LM85_REG_FAN_MIN(1) :
1266 case LM85_REG_FAN_MIN(2) :
1267 case LM85_REG_FAN_MIN(3) :
1268 case LM85_REG_ALARM1 : /* Read both bytes at once */
1269 res = i2c_smbus_read_byte_data(client, reg) & 0xff ;
1270 res |= i2c_smbus_read_byte_data(client, reg+1) << 8 ;
1271 break ;
1272 case ADT7463_REG_TMIN_CTL1 : /* Read WORD MSB, LSB */
1273 res = i2c_smbus_read_byte_data(client, reg) << 8 ;
1274 res |= i2c_smbus_read_byte_data(client, reg+1) & 0xff ;
1275 break ;
1276 default: /* Read BYTE data */
1277 res = i2c_smbus_read_byte_data(client, reg);
1278 break ;
1281 return res ;
1284 int lm85_write_value(struct i2c_client *client, u8 reg, int value)
1286 int res ;
1288 switch( reg ) {
1289 case LM85_REG_FAN(0) : /* Write WORD data */
1290 case LM85_REG_FAN(1) :
1291 case LM85_REG_FAN(2) :
1292 case LM85_REG_FAN(3) :
1293 case LM85_REG_FAN_MIN(0) :
1294 case LM85_REG_FAN_MIN(1) :
1295 case LM85_REG_FAN_MIN(2) :
1296 case LM85_REG_FAN_MIN(3) :
1297 /* NOTE: ALARM is read only, so not included here */
1298 res = i2c_smbus_write_byte_data(client, reg, value & 0xff) ;
1299 res |= i2c_smbus_write_byte_data(client, reg+1, (value>>8) & 0xff) ;
1300 break ;
1301 case ADT7463_REG_TMIN_CTL1 : /* Write WORD MSB, LSB */
1302 res = i2c_smbus_write_byte_data(client, reg, (value>>8) & 0xff);
1303 res |= i2c_smbus_write_byte_data(client, reg+1, value & 0xff) ;
1304 break ;
1305 default: /* Write BYTE data */
1306 res = i2c_smbus_write_byte_data(client, reg, value);
1307 break ;
1310 return res ;
1313 void lm85_init_client(struct i2c_client *client)
1315 int value;
1316 struct lm85_data *data = i2c_get_clientdata(client);
1318 dev_dbg(&client->dev, "Initializing device\n");
1320 /* Warn if part was not "READY" */
1321 value = lm85_read_value(client, LM85_REG_CONFIG);
1322 dev_dbg(&client->dev, "LM85_REG_CONFIG is: 0x%02x\n", value);
1323 if( value & 0x02 ) {
1324 dev_err(&client->dev, "Client (%d,0x%02x) config is locked.\n",
1325 i2c_adapter_id(client->adapter), client->addr );
1327 if( ! (value & 0x04) ) {
1328 dev_err(&client->dev, "Client (%d,0x%02x) is not ready.\n",
1329 i2c_adapter_id(client->adapter), client->addr );
1331 if( value & 0x10
1332 && ( data->type == adm1027
1333 || data->type == adt7463 ) ) {
1334 dev_err(&client->dev, "Client (%d,0x%02x) VxI mode is set. "
1335 "Please report this to the lm85 maintainer.\n",
1336 i2c_adapter_id(client->adapter), client->addr );
1339 /* WE INTENTIONALLY make no changes to the limits,
1340 * offsets, pwms, fans and zones. If they were
1341 * configured, we don't want to mess with them.
1342 * If they weren't, the default is 100% PWM, no
1343 * control and will suffice until 'sensors -s'
1344 * can be run by the user.
1347 /* Start monitoring */
1348 value = lm85_read_value(client, LM85_REG_CONFIG);
1349 /* Try to clear LOCK, Set START, save everything else */
1350 value = (value & ~ 0x02) | 0x01 ;
1351 dev_dbg(&client->dev, "Setting CONFIG to: 0x%02x\n", value);
1352 lm85_write_value(client, LM85_REG_CONFIG, value);
1355 static struct lm85_data *lm85_update_device(struct device *dev)
1357 struct i2c_client *client = to_i2c_client(dev);
1358 struct lm85_data *data = i2c_get_clientdata(client);
1359 int i;
1361 down(&data->update_lock);
1363 if ( !data->valid ||
1364 time_after(jiffies, data->last_reading + LM85_DATA_INTERVAL) ) {
1365 /* Things that change quickly */
1366 dev_dbg(&client->dev, "Reading sensor values\n");
1368 /* Have to read extended bits first to "freeze" the
1369 * more significant bits that are read later.
1371 if ( (data->type == adm1027) || (data->type == adt7463) ) {
1372 int ext1 = lm85_read_value(client,
1373 ADM1027_REG_EXTEND_ADC1);
1374 int ext2 = lm85_read_value(client,
1375 ADM1027_REG_EXTEND_ADC2);
1376 int val = (ext1 << 8) + ext2;
1378 for(i = 0; i <= 4; i++)
1379 data->in_ext[i] = (val>>(i * 2))&0x03;
1381 for(i = 0; i <= 2; i++)
1382 data->temp_ext[i] = (val>>((i + 5) * 2))&0x03;
1385 /* adc_scale is 2^(number of LSBs). There are 4 extra bits in
1386 the emc6d102 and 2 in the adt7463 and adm1027. In all
1387 other chips ext is always 0 and the value of scale is
1388 irrelevant. So it is left in 4*/
1389 data->adc_scale = (data->type == emc6d102 ) ? 16 : 4;
1391 for (i = 0; i <= 4; ++i) {
1392 data->in[i] =
1393 lm85_read_value(client, LM85_REG_IN(i));
1396 for (i = 0; i <= 3; ++i) {
1397 data->fan[i] =
1398 lm85_read_value(client, LM85_REG_FAN(i));
1401 for (i = 0; i <= 2; ++i) {
1402 data->temp[i] =
1403 lm85_read_value(client, LM85_REG_TEMP(i));
1406 for (i = 0; i <= 2; ++i) {
1407 data->pwm[i] =
1408 lm85_read_value(client, LM85_REG_PWM(i));
1411 data->alarms = lm85_read_value(client, LM85_REG_ALARM1);
1413 if ( data->type == adt7463 ) {
1414 if( data->therm_total < ULONG_MAX - 256 ) {
1415 data->therm_total +=
1416 lm85_read_value(client, ADT7463_REG_THERM );
1418 } else if ( data->type == emc6d100 ) {
1419 /* Three more voltage sensors */
1420 for (i = 5; i <= 7; ++i) {
1421 data->in[i] =
1422 lm85_read_value(client, EMC6D100_REG_IN(i));
1424 /* More alarm bits */
1425 data->alarms |=
1426 lm85_read_value(client, EMC6D100_REG_ALARM3) << 16;
1427 } else if (data->type == emc6d102 ) {
1428 /* Have to read LSB bits after the MSB ones because
1429 the reading of the MSB bits has frozen the
1430 LSBs (backward from the ADM1027).
1432 int ext1 = lm85_read_value(client,
1433 EMC6D102_REG_EXTEND_ADC1);
1434 int ext2 = lm85_read_value(client,
1435 EMC6D102_REG_EXTEND_ADC2);
1436 int ext3 = lm85_read_value(client,
1437 EMC6D102_REG_EXTEND_ADC3);
1438 int ext4 = lm85_read_value(client,
1439 EMC6D102_REG_EXTEND_ADC4);
1440 data->in_ext[0] = ext3 & 0x0f;
1441 data->in_ext[1] = ext4 & 0x0f;
1442 data->in_ext[2] = (ext4 >> 4) & 0x0f;
1443 data->in_ext[3] = (ext3 >> 4) & 0x0f;
1444 data->in_ext[4] = (ext2 >> 4) & 0x0f;
1446 data->temp_ext[0] = ext1 & 0x0f;
1447 data->temp_ext[1] = ext2 & 0x0f;
1448 data->temp_ext[2] = (ext1 >> 4) & 0x0f;
1451 data->last_reading = jiffies ;
1452 }; /* last_reading */
1454 if ( !data->valid ||
1455 time_after(jiffies, data->last_config + LM85_CONFIG_INTERVAL) ) {
1456 /* Things that don't change often */
1457 dev_dbg(&client->dev, "Reading config values\n");
1459 for (i = 0; i <= 4; ++i) {
1460 data->in_min[i] =
1461 lm85_read_value(client, LM85_REG_IN_MIN(i));
1462 data->in_max[i] =
1463 lm85_read_value(client, LM85_REG_IN_MAX(i));
1466 if ( data->type == emc6d100 ) {
1467 for (i = 5; i <= 7; ++i) {
1468 data->in_min[i] =
1469 lm85_read_value(client, EMC6D100_REG_IN_MIN(i));
1470 data->in_max[i] =
1471 lm85_read_value(client, EMC6D100_REG_IN_MAX(i));
1475 for (i = 0; i <= 3; ++i) {
1476 data->fan_min[i] =
1477 lm85_read_value(client, LM85_REG_FAN_MIN(i));
1480 for (i = 0; i <= 2; ++i) {
1481 data->temp_min[i] =
1482 lm85_read_value(client, LM85_REG_TEMP_MIN(i));
1483 data->temp_max[i] =
1484 lm85_read_value(client, LM85_REG_TEMP_MAX(i));
1487 data->vid = lm85_read_value(client, LM85_REG_VID);
1489 for (i = 0; i <= 2; ++i) {
1490 int val ;
1491 data->autofan[i].config =
1492 lm85_read_value(client, LM85_REG_AFAN_CONFIG(i));
1493 val = lm85_read_value(client, LM85_REG_AFAN_RANGE(i));
1494 data->autofan[i].freq = val & 0x07 ;
1495 data->zone[i].range = (val >> 4) & 0x0f ;
1496 data->autofan[i].min_pwm =
1497 lm85_read_value(client, LM85_REG_AFAN_MINPWM(i));
1498 data->zone[i].limit =
1499 lm85_read_value(client, LM85_REG_AFAN_LIMIT(i));
1500 data->zone[i].critical =
1501 lm85_read_value(client, LM85_REG_AFAN_CRITICAL(i));
1504 i = lm85_read_value(client, LM85_REG_AFAN_SPIKE1);
1505 data->smooth[0] = i & 0x0f ;
1506 data->syncpwm3 = i & 0x10 ; /* Save PWM3 config */
1507 data->autofan[0].min_off = (i & 0x20) != 0 ;
1508 data->autofan[1].min_off = (i & 0x40) != 0 ;
1509 data->autofan[2].min_off = (i & 0x80) != 0 ;
1510 i = lm85_read_value(client, LM85_REG_AFAN_SPIKE2);
1511 data->smooth[1] = (i>>4) & 0x0f ;
1512 data->smooth[2] = i & 0x0f ;
1514 i = lm85_read_value(client, LM85_REG_AFAN_HYST1);
1515 data->zone[0].hyst = (i>>4) & 0x0f ;
1516 data->zone[1].hyst = i & 0x0f ;
1518 i = lm85_read_value(client, LM85_REG_AFAN_HYST2);
1519 data->zone[2].hyst = (i>>4) & 0x0f ;
1521 if ( (data->type == lm85b) || (data->type == lm85c) ) {
1522 data->tach_mode = lm85_read_value(client,
1523 LM85_REG_TACH_MODE );
1524 data->spinup_ctl = lm85_read_value(client,
1525 LM85_REG_SPINUP_CTL );
1526 } else if ( (data->type == adt7463) || (data->type == adm1027) ) {
1527 if ( data->type == adt7463 ) {
1528 for (i = 0; i <= 2; ++i) {
1529 data->oppoint[i] = lm85_read_value(client,
1530 ADT7463_REG_OPPOINT(i) );
1532 data->tmin_ctl = lm85_read_value(client,
1533 ADT7463_REG_TMIN_CTL1 );
1534 data->therm_limit = lm85_read_value(client,
1535 ADT7463_REG_THERM_LIMIT );
1537 for (i = 0; i <= 2; ++i) {
1538 data->temp_offset[i] = lm85_read_value(client,
1539 ADM1027_REG_TEMP_OFFSET(i) );
1541 data->tach_mode = lm85_read_value(client,
1542 ADM1027_REG_CONFIG3 );
1543 data->fan_ppr = lm85_read_value(client,
1544 ADM1027_REG_FAN_PPR );
1547 data->last_config = jiffies;
1548 }; /* last_config */
1550 data->valid = 1;
1552 up(&data->update_lock);
1554 return data;
1558 static int __init sm_lm85_init(void)
1560 return i2c_add_driver(&lm85_driver);
1563 static void __exit sm_lm85_exit(void)
1565 i2c_del_driver(&lm85_driver);
1568 /* Thanks to Richard Barrington for adding the LM85 to sensors-detect.
1569 * Thanks to Margit Schubert-While <margitsw@t-online.de> for help with
1570 * post 2.7.0 CVS changes.
1572 MODULE_LICENSE("GPL");
1573 MODULE_AUTHOR("Philip Pokorny <ppokorny@penguincomputing.com>, Margit Schubert-While <margitsw@t-online.de>, Justin Thiessen <jthiessen@penguincomputing.com");
1574 MODULE_DESCRIPTION("LM85-B, LM85-C driver");
1576 module_init(sm_lm85_init);
1577 module_exit(sm_lm85_exit);