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/module.h>
27 #include <linux/init.h>
28 #include <linux/slab.h>
29 #include <linux/jiffies.h>
30 #include <linux/i2c.h>
31 #include <linux/hwmon.h>
32 #include <linux/hwmon-vid.h>
33 #include <linux/err.h>
35 /* Addresses to scan */
36 static unsigned short normal_i2c[] = { 0x2c, 0x2d, 0x2e, I2C_CLIENT_END };
38 /* Insmod parameters */
39 I2C_CLIENT_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 )
206 if ( range < lm85_range_map[0] ) {
208 } else if ( range > lm85_range_map[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])) {
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 )
239 if( freq >= lm85_freq_map[7] ) { return 7 ; }
240 for( i = 0 ; i < 7 ; ++i )
241 if( freq <= lm85_freq_map[i] )
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 )
266 for( i = 0 ; i <= 7 ; ++i )
267 if( zone == lm85_zone_map[i] )
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 /* Chip sampling rates
286 * Some sensors are not updated more frequently than once per second
287 * so it doesn't make sense to read them more often than that.
288 * We cache the results and return the saved data if the driver
289 * is called again before a second has elapsed.
291 * Also, there is significant configuration data for this chip
292 * given the automatic PWM fan control that is possible. There
293 * are about 47 bytes of config data to only 22 bytes of actual
294 * readings. So, we keep the config data up to date in the cache
295 * when it is written and only sample it once every 1 *minute*
297 #define LM85_DATA_INTERVAL (HZ + HZ / 2)
298 #define LM85_CONFIG_INTERVAL (1 * 60 * HZ)
300 /* For each registered LM85, we need to keep some data in memory. That
301 data is pointed to by lm85_list[NR]->data. The structure itself is
302 dynamically allocated, at the same time when a new lm85 client is
305 /* LM85 can automatically adjust fan speeds based on temperature
306 * This structure encapsulates an entire Zone config. There are
307 * three zones (one for each temperature input) on the lm85
310 s8 limit; /* Low temp limit */
311 u8 hyst; /* Low limit hysteresis. (0-15) */
312 u8 range; /* Temp range, encoded */
313 s8 critical; /* "All fans ON" temp limit */
314 u8 off_desired; /* Actual "off" temperature specified. Preserved
315 * to prevent "drift" as other autofan control
318 u8 max_desired; /* Actual "max" temperature specified. Preserved
319 * to prevent "drift" as other autofan control
324 struct lm85_autofan {
325 u8 config; /* Register value */
326 u8 freq; /* PWM frequency, encoded */
327 u8 min_pwm; /* Minimum PWM value, encoded */
328 u8 min_off; /* Min PWM or OFF below "limit", flag */
332 struct i2c_client client;
333 struct class_device *class_dev;
334 struct semaphore lock;
337 struct semaphore update_lock;
338 int valid; /* !=0 if following fields are valid */
339 unsigned long last_reading; /* In jiffies */
340 unsigned long last_config; /* In jiffies */
342 u8 in[8]; /* Register value */
343 u8 in_max[8]; /* Register value */
344 u8 in_min[8]; /* Register value */
345 s8 temp[3]; /* Register value */
346 s8 temp_min[3]; /* Register value */
347 s8 temp_max[3]; /* Register value */
348 s8 temp_offset[3]; /* Register value */
349 u16 fan[4]; /* Register value */
350 u16 fan_min[4]; /* Register value */
351 u8 pwm[3]; /* Register value */
352 u8 spinup_ctl; /* Register encoding, combined */
353 u8 tach_mode; /* Register encoding, combined */
354 u8 temp_ext[3]; /* Decoded values */
355 u8 in_ext[8]; /* Decoded values */
356 u8 adc_scale; /* ADC Extended bits scaling factor */
357 u8 fan_ppr; /* Register value */
358 u8 smooth[3]; /* Register encoding */
359 u8 vid; /* Register value */
360 u8 vrm; /* VRM version */
361 u8 syncpwm3; /* Saved PWM3 for TACH 2,3,4 config */
362 u8 oppoint[3]; /* Register value */
363 u16 tmin_ctl; /* Register value */
364 unsigned long therm_total; /* Cummulative therm count */
365 u8 therm_limit; /* Register value */
366 u32 alarms; /* Register encoding, combined */
367 struct lm85_autofan autofan[3];
368 struct lm85_zone zone[3];
371 static int lm85_attach_adapter(struct i2c_adapter *adapter);
372 static int lm85_detect(struct i2c_adapter *adapter, int address,
374 static int lm85_detach_client(struct i2c_client *client);
376 static int lm85_read_value(struct i2c_client *client, u8 register);
377 static int lm85_write_value(struct i2c_client *client, u8 register, int value);
378 static struct lm85_data *lm85_update_device(struct device *dev);
379 static void lm85_init_client(struct i2c_client *client);
382 static struct i2c_driver lm85_driver = {
383 .owner = THIS_MODULE,
385 .id = I2C_DRIVERID_LM85,
386 .attach_adapter = lm85_attach_adapter,
387 .detach_client = lm85_detach_client,
392 static ssize_t show_fan(struct device *dev, char *buf, int nr)
394 struct lm85_data *data = lm85_update_device(dev);
395 return sprintf(buf,"%d\n", FAN_FROM_REG(data->fan[nr]) );
397 static ssize_t show_fan_min(struct device *dev, char *buf, int nr)
399 struct lm85_data *data = lm85_update_device(dev);
400 return sprintf(buf,"%d\n", FAN_FROM_REG(data->fan_min[nr]) );
402 static ssize_t set_fan_min(struct device *dev, const char *buf,
403 size_t count, int nr)
405 struct i2c_client *client = to_i2c_client(dev);
406 struct lm85_data *data = i2c_get_clientdata(client);
407 long val = simple_strtol(buf, NULL, 10);
409 down(&data->update_lock);
410 data->fan_min[nr] = FAN_TO_REG(val);
411 lm85_write_value(client, LM85_REG_FAN_MIN(nr), data->fan_min[nr]);
412 up(&data->update_lock);
416 #define show_fan_offset(offset) \
417 static ssize_t show_fan_##offset (struct device *dev, struct device_attribute *attr, char *buf) \
419 return show_fan(dev, buf, offset - 1); \
421 static ssize_t show_fan_##offset##_min (struct device *dev, struct device_attribute *attr, char *buf) \
423 return show_fan_min(dev, buf, offset - 1); \
425 static ssize_t set_fan_##offset##_min (struct device *dev, struct device_attribute *attr, \
426 const char *buf, size_t count) \
428 return set_fan_min(dev, buf, count, offset - 1); \
430 static DEVICE_ATTR(fan##offset##_input, S_IRUGO, show_fan_##offset, \
432 static DEVICE_ATTR(fan##offset##_min, S_IRUGO | S_IWUSR, \
433 show_fan_##offset##_min, set_fan_##offset##_min);
440 /* vid, vrm, alarms */
442 static ssize_t show_vid_reg(struct device *dev, struct device_attribute *attr, char *buf)
444 struct lm85_data *data = lm85_update_device(dev);
447 if (data->type == adt7463 && (data->vid & 0x80)) {
448 /* 6-pin VID (VRM 10) */
449 vid = vid_from_reg(data->vid & 0x3f, data->vrm);
451 /* 5-pin VID (VRM 9) */
452 vid = vid_from_reg(data->vid & 0x1f, data->vrm);
455 return sprintf(buf, "%d\n", vid);
458 static DEVICE_ATTR(cpu0_vid, S_IRUGO, show_vid_reg, NULL);
460 static ssize_t show_vrm_reg(struct device *dev, struct device_attribute *attr, char *buf)
462 struct lm85_data *data = lm85_update_device(dev);
463 return sprintf(buf, "%ld\n", (long) data->vrm);
466 static ssize_t store_vrm_reg(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
468 struct i2c_client *client = to_i2c_client(dev);
469 struct lm85_data *data = i2c_get_clientdata(client);
472 val = simple_strtoul(buf, NULL, 10);
477 static DEVICE_ATTR(vrm, S_IRUGO | S_IWUSR, show_vrm_reg, store_vrm_reg);
479 static ssize_t show_alarms_reg(struct device *dev, struct device_attribute *attr, char *buf)
481 struct lm85_data *data = lm85_update_device(dev);
482 return sprintf(buf, "%u\n", data->alarms);
485 static DEVICE_ATTR(alarms, S_IRUGO, show_alarms_reg, NULL);
489 static ssize_t show_pwm(struct device *dev, char *buf, int nr)
491 struct lm85_data *data = lm85_update_device(dev);
492 return sprintf(buf,"%d\n", PWM_FROM_REG(data->pwm[nr]) );
494 static ssize_t set_pwm(struct device *dev, const char *buf,
495 size_t count, int nr)
497 struct i2c_client *client = to_i2c_client(dev);
498 struct lm85_data *data = i2c_get_clientdata(client);
499 long val = simple_strtol(buf, NULL, 10);
501 down(&data->update_lock);
502 data->pwm[nr] = PWM_TO_REG(val);
503 lm85_write_value(client, LM85_REG_PWM(nr), data->pwm[nr]);
504 up(&data->update_lock);
507 static ssize_t show_pwm_enable(struct device *dev, char *buf, int nr)
509 struct lm85_data *data = lm85_update_device(dev);
512 pwm_zone = ZONE_FROM_REG(data->autofan[nr].config);
513 return sprintf(buf,"%d\n", (pwm_zone != 0 && pwm_zone != -1) );
516 #define show_pwm_reg(offset) \
517 static ssize_t show_pwm_##offset (struct device *dev, struct device_attribute *attr, char *buf) \
519 return show_pwm(dev, buf, offset - 1); \
521 static ssize_t set_pwm_##offset (struct device *dev, struct device_attribute *attr, \
522 const char *buf, size_t count) \
524 return set_pwm(dev, buf, count, offset - 1); \
526 static ssize_t show_pwm_enable##offset (struct device *dev, struct device_attribute *attr, char *buf) \
528 return show_pwm_enable(dev, buf, offset - 1); \
530 static DEVICE_ATTR(pwm##offset, S_IRUGO | S_IWUSR, \
531 show_pwm_##offset, set_pwm_##offset); \
532 static DEVICE_ATTR(pwm##offset##_enable, S_IRUGO, \
533 show_pwm_enable##offset, NULL);
541 static ssize_t show_in(struct device *dev, char *buf, int nr)
543 struct lm85_data *data = lm85_update_device(dev);
544 return sprintf( buf, "%d\n", INSEXT_FROM_REG(nr,
549 static ssize_t show_in_min(struct device *dev, char *buf, int nr)
551 struct lm85_data *data = lm85_update_device(dev);
552 return sprintf(buf,"%d\n", INS_FROM_REG(nr, data->in_min[nr]) );
554 static ssize_t set_in_min(struct device *dev, const char *buf,
555 size_t count, int nr)
557 struct i2c_client *client = to_i2c_client(dev);
558 struct lm85_data *data = i2c_get_clientdata(client);
559 long val = simple_strtol(buf, NULL, 10);
561 down(&data->update_lock);
562 data->in_min[nr] = INS_TO_REG(nr, val);
563 lm85_write_value(client, LM85_REG_IN_MIN(nr), data->in_min[nr]);
564 up(&data->update_lock);
567 static ssize_t show_in_max(struct device *dev, char *buf, int nr)
569 struct lm85_data *data = lm85_update_device(dev);
570 return sprintf(buf,"%d\n", INS_FROM_REG(nr, data->in_max[nr]) );
572 static ssize_t set_in_max(struct device *dev, const char *buf,
573 size_t count, int nr)
575 struct i2c_client *client = to_i2c_client(dev);
576 struct lm85_data *data = i2c_get_clientdata(client);
577 long val = simple_strtol(buf, NULL, 10);
579 down(&data->update_lock);
580 data->in_max[nr] = INS_TO_REG(nr, val);
581 lm85_write_value(client, LM85_REG_IN_MAX(nr), data->in_max[nr]);
582 up(&data->update_lock);
585 #define show_in_reg(offset) \
586 static ssize_t show_in_##offset (struct device *dev, struct device_attribute *attr, char *buf) \
588 return show_in(dev, buf, offset); \
590 static ssize_t show_in_##offset##_min (struct device *dev, struct device_attribute *attr, char *buf) \
592 return show_in_min(dev, buf, offset); \
594 static ssize_t show_in_##offset##_max (struct device *dev, struct device_attribute *attr, char *buf) \
596 return show_in_max(dev, buf, offset); \
598 static ssize_t set_in_##offset##_min (struct device *dev, struct device_attribute *attr, \
599 const char *buf, size_t count) \
601 return set_in_min(dev, buf, count, offset); \
603 static ssize_t set_in_##offset##_max (struct device *dev, struct device_attribute *attr, \
604 const char *buf, size_t count) \
606 return set_in_max(dev, buf, count, offset); \
608 static DEVICE_ATTR(in##offset##_input, S_IRUGO, show_in_##offset, \
610 static DEVICE_ATTR(in##offset##_min, S_IRUGO | S_IWUSR, \
611 show_in_##offset##_min, set_in_##offset##_min); \
612 static DEVICE_ATTR(in##offset##_max, S_IRUGO | S_IWUSR, \
613 show_in_##offset##_max, set_in_##offset##_max);
623 static ssize_t show_temp(struct device *dev, char *buf, int nr)
625 struct lm85_data *data = lm85_update_device(dev);
626 return sprintf(buf,"%d\n", TEMPEXT_FROM_REG(data->temp[nr],
630 static ssize_t show_temp_min(struct device *dev, char *buf, int nr)
632 struct lm85_data *data = lm85_update_device(dev);
633 return sprintf(buf,"%d\n", TEMP_FROM_REG(data->temp_min[nr]) );
635 static ssize_t set_temp_min(struct device *dev, const char *buf,
636 size_t count, int nr)
638 struct i2c_client *client = to_i2c_client(dev);
639 struct lm85_data *data = i2c_get_clientdata(client);
640 long val = simple_strtol(buf, NULL, 10);
642 down(&data->update_lock);
643 data->temp_min[nr] = TEMP_TO_REG(val);
644 lm85_write_value(client, LM85_REG_TEMP_MIN(nr), data->temp_min[nr]);
645 up(&data->update_lock);
648 static ssize_t show_temp_max(struct device *dev, char *buf, int nr)
650 struct lm85_data *data = lm85_update_device(dev);
651 return sprintf(buf,"%d\n", TEMP_FROM_REG(data->temp_max[nr]) );
653 static ssize_t set_temp_max(struct device *dev, const char *buf,
654 size_t count, int nr)
656 struct i2c_client *client = to_i2c_client(dev);
657 struct lm85_data *data = i2c_get_clientdata(client);
658 long val = simple_strtol(buf, NULL, 10);
660 down(&data->update_lock);
661 data->temp_max[nr] = TEMP_TO_REG(val);
662 lm85_write_value(client, LM85_REG_TEMP_MAX(nr), data->temp_max[nr]);
663 up(&data->update_lock);
666 #define show_temp_reg(offset) \
667 static ssize_t show_temp_##offset (struct device *dev, struct device_attribute *attr, char *buf) \
669 return show_temp(dev, buf, offset - 1); \
671 static ssize_t show_temp_##offset##_min (struct device *dev, struct device_attribute *attr, char *buf) \
673 return show_temp_min(dev, buf, offset - 1); \
675 static ssize_t show_temp_##offset##_max (struct device *dev, struct device_attribute *attr, char *buf) \
677 return show_temp_max(dev, buf, offset - 1); \
679 static ssize_t set_temp_##offset##_min (struct device *dev, struct device_attribute *attr, \
680 const char *buf, size_t count) \
682 return set_temp_min(dev, buf, count, offset - 1); \
684 static ssize_t set_temp_##offset##_max (struct device *dev, struct device_attribute *attr, \
685 const char *buf, size_t count) \
687 return set_temp_max(dev, buf, count, offset - 1); \
689 static DEVICE_ATTR(temp##offset##_input, S_IRUGO, show_temp_##offset, \
691 static DEVICE_ATTR(temp##offset##_min, S_IRUGO | S_IWUSR, \
692 show_temp_##offset##_min, set_temp_##offset##_min); \
693 static DEVICE_ATTR(temp##offset##_max, S_IRUGO | S_IWUSR, \
694 show_temp_##offset##_max, set_temp_##offset##_max);
701 /* Automatic PWM control */
703 static ssize_t show_pwm_auto_channels(struct device *dev, char *buf, int nr)
705 struct lm85_data *data = lm85_update_device(dev);
706 return sprintf(buf,"%d\n", ZONE_FROM_REG(data->autofan[nr].config));
708 static ssize_t set_pwm_auto_channels(struct device *dev, const char *buf,
709 size_t count, int nr)
711 struct i2c_client *client = to_i2c_client(dev);
712 struct lm85_data *data = i2c_get_clientdata(client);
713 long val = simple_strtol(buf, NULL, 10);
715 down(&data->update_lock);
716 data->autofan[nr].config = (data->autofan[nr].config & (~0xe0))
718 lm85_write_value(client, LM85_REG_AFAN_CONFIG(nr),
719 data->autofan[nr].config);
720 up(&data->update_lock);
723 static ssize_t show_pwm_auto_pwm_min(struct device *dev, char *buf, int nr)
725 struct lm85_data *data = lm85_update_device(dev);
726 return sprintf(buf,"%d\n", PWM_FROM_REG(data->autofan[nr].min_pwm));
728 static ssize_t set_pwm_auto_pwm_min(struct device *dev, const char *buf,
729 size_t count, int nr)
731 struct i2c_client *client = to_i2c_client(dev);
732 struct lm85_data *data = i2c_get_clientdata(client);
733 long val = simple_strtol(buf, NULL, 10);
735 down(&data->update_lock);
736 data->autofan[nr].min_pwm = PWM_TO_REG(val);
737 lm85_write_value(client, LM85_REG_AFAN_MINPWM(nr),
738 data->autofan[nr].min_pwm);
739 up(&data->update_lock);
742 static ssize_t show_pwm_auto_pwm_minctl(struct device *dev, char *buf, int nr)
744 struct lm85_data *data = lm85_update_device(dev);
745 return sprintf(buf,"%d\n", data->autofan[nr].min_off);
747 static ssize_t set_pwm_auto_pwm_minctl(struct device *dev, const char *buf,
748 size_t count, int nr)
750 struct i2c_client *client = to_i2c_client(dev);
751 struct lm85_data *data = i2c_get_clientdata(client);
752 long val = simple_strtol(buf, NULL, 10);
754 down(&data->update_lock);
755 data->autofan[nr].min_off = val;
756 lm85_write_value(client, LM85_REG_AFAN_SPIKE1, data->smooth[0]
758 | (data->autofan[0].min_off ? 0x20 : 0)
759 | (data->autofan[1].min_off ? 0x40 : 0)
760 | (data->autofan[2].min_off ? 0x80 : 0)
762 up(&data->update_lock);
765 static ssize_t show_pwm_auto_pwm_freq(struct device *dev, char *buf, int nr)
767 struct lm85_data *data = lm85_update_device(dev);
768 return sprintf(buf,"%d\n", FREQ_FROM_REG(data->autofan[nr].freq));
770 static ssize_t set_pwm_auto_pwm_freq(struct device *dev, const char *buf,
771 size_t count, int nr)
773 struct i2c_client *client = to_i2c_client(dev);
774 struct lm85_data *data = i2c_get_clientdata(client);
775 long val = simple_strtol(buf, NULL, 10);
777 down(&data->update_lock);
778 data->autofan[nr].freq = FREQ_TO_REG(val);
779 lm85_write_value(client, LM85_REG_AFAN_RANGE(nr),
780 (data->zone[nr].range << 4)
781 | data->autofan[nr].freq
783 up(&data->update_lock);
786 #define pwm_auto(offset) \
787 static ssize_t show_pwm##offset##_auto_channels (struct device *dev, struct device_attribute *attr, \
790 return show_pwm_auto_channels(dev, buf, offset - 1); \
792 static ssize_t set_pwm##offset##_auto_channels (struct device *dev, struct device_attribute *attr, \
793 const char *buf, size_t count) \
795 return set_pwm_auto_channels(dev, buf, count, offset - 1); \
797 static ssize_t show_pwm##offset##_auto_pwm_min (struct device *dev, struct device_attribute *attr, \
800 return show_pwm_auto_pwm_min(dev, buf, offset - 1); \
802 static ssize_t set_pwm##offset##_auto_pwm_min (struct device *dev, struct device_attribute *attr, \
803 const char *buf, size_t count) \
805 return set_pwm_auto_pwm_min(dev, buf, count, offset - 1); \
807 static ssize_t show_pwm##offset##_auto_pwm_minctl (struct device *dev, struct device_attribute *attr, \
810 return show_pwm_auto_pwm_minctl(dev, buf, offset - 1); \
812 static ssize_t set_pwm##offset##_auto_pwm_minctl (struct device *dev, struct device_attribute *attr, \
813 const char *buf, size_t count) \
815 return set_pwm_auto_pwm_minctl(dev, buf, count, offset - 1); \
817 static ssize_t show_pwm##offset##_auto_pwm_freq (struct device *dev, struct device_attribute *attr, \
820 return show_pwm_auto_pwm_freq(dev, buf, offset - 1); \
822 static ssize_t set_pwm##offset##_auto_pwm_freq(struct device *dev, struct device_attribute *attr, \
823 const char *buf, size_t count) \
825 return set_pwm_auto_pwm_freq(dev, buf, count, offset - 1); \
827 static DEVICE_ATTR(pwm##offset##_auto_channels, S_IRUGO | S_IWUSR, \
828 show_pwm##offset##_auto_channels, \
829 set_pwm##offset##_auto_channels); \
830 static DEVICE_ATTR(pwm##offset##_auto_pwm_min, S_IRUGO | S_IWUSR, \
831 show_pwm##offset##_auto_pwm_min, \
832 set_pwm##offset##_auto_pwm_min); \
833 static DEVICE_ATTR(pwm##offset##_auto_pwm_minctl, S_IRUGO | S_IWUSR, \
834 show_pwm##offset##_auto_pwm_minctl, \
835 set_pwm##offset##_auto_pwm_minctl); \
836 static DEVICE_ATTR(pwm##offset##_auto_pwm_freq, S_IRUGO | S_IWUSR, \
837 show_pwm##offset##_auto_pwm_freq, \
838 set_pwm##offset##_auto_pwm_freq);
843 /* Temperature settings for automatic PWM control */
845 static ssize_t show_temp_auto_temp_off(struct device *dev, char *buf, int nr)
847 struct lm85_data *data = lm85_update_device(dev);
848 return sprintf(buf,"%d\n", TEMP_FROM_REG(data->zone[nr].limit) -
849 HYST_FROM_REG(data->zone[nr].hyst));
851 static ssize_t set_temp_auto_temp_off(struct device *dev, const char *buf,
852 size_t count, int nr)
854 struct i2c_client *client = to_i2c_client(dev);
855 struct lm85_data *data = i2c_get_clientdata(client);
857 long val = simple_strtol(buf, NULL, 10);
859 down(&data->update_lock);
860 min = TEMP_FROM_REG(data->zone[nr].limit);
861 data->zone[nr].off_desired = TEMP_TO_REG(val);
862 data->zone[nr].hyst = HYST_TO_REG(min - val);
863 if ( nr == 0 || nr == 1 ) {
864 lm85_write_value(client, LM85_REG_AFAN_HYST1,
865 (data->zone[0].hyst << 4)
869 lm85_write_value(client, LM85_REG_AFAN_HYST2,
870 (data->zone[2].hyst << 4)
873 up(&data->update_lock);
876 static ssize_t show_temp_auto_temp_min(struct device *dev, char *buf, int nr)
878 struct lm85_data *data = lm85_update_device(dev);
879 return sprintf(buf,"%d\n", TEMP_FROM_REG(data->zone[nr].limit) );
881 static ssize_t set_temp_auto_temp_min(struct device *dev, const char *buf,
882 size_t count, int nr)
884 struct i2c_client *client = to_i2c_client(dev);
885 struct lm85_data *data = i2c_get_clientdata(client);
886 long val = simple_strtol(buf, NULL, 10);
888 down(&data->update_lock);
889 data->zone[nr].limit = TEMP_TO_REG(val);
890 lm85_write_value(client, LM85_REG_AFAN_LIMIT(nr),
891 data->zone[nr].limit);
893 /* Update temp_auto_max and temp_auto_range */
894 data->zone[nr].range = RANGE_TO_REG(
895 TEMP_FROM_REG(data->zone[nr].max_desired) -
896 TEMP_FROM_REG(data->zone[nr].limit));
897 lm85_write_value(client, LM85_REG_AFAN_RANGE(nr),
898 ((data->zone[nr].range & 0x0f) << 4)
899 | (data->autofan[nr].freq & 0x07));
901 /* Update temp_auto_hyst and temp_auto_off */
902 data->zone[nr].hyst = HYST_TO_REG(TEMP_FROM_REG(
903 data->zone[nr].limit) - TEMP_FROM_REG(
904 data->zone[nr].off_desired));
905 if ( nr == 0 || nr == 1 ) {
906 lm85_write_value(client, LM85_REG_AFAN_HYST1,
907 (data->zone[0].hyst << 4)
911 lm85_write_value(client, LM85_REG_AFAN_HYST2,
912 (data->zone[2].hyst << 4)
915 up(&data->update_lock);
918 static ssize_t show_temp_auto_temp_max(struct device *dev, char *buf, int nr)
920 struct lm85_data *data = lm85_update_device(dev);
921 return sprintf(buf,"%d\n", TEMP_FROM_REG(data->zone[nr].limit) +
922 RANGE_FROM_REG(data->zone[nr].range));
924 static ssize_t set_temp_auto_temp_max(struct device *dev, const char *buf,
925 size_t count, int nr)
927 struct i2c_client *client = to_i2c_client(dev);
928 struct lm85_data *data = i2c_get_clientdata(client);
930 long val = simple_strtol(buf, NULL, 10);
932 down(&data->update_lock);
933 min = TEMP_FROM_REG(data->zone[nr].limit);
934 data->zone[nr].max_desired = TEMP_TO_REG(val);
935 data->zone[nr].range = RANGE_TO_REG(
937 lm85_write_value(client, LM85_REG_AFAN_RANGE(nr),
938 ((data->zone[nr].range & 0x0f) << 4)
939 | (data->autofan[nr].freq & 0x07));
940 up(&data->update_lock);
943 static ssize_t show_temp_auto_temp_crit(struct device *dev, char *buf, int nr)
945 struct lm85_data *data = lm85_update_device(dev);
946 return sprintf(buf,"%d\n", TEMP_FROM_REG(data->zone[nr].critical));
948 static ssize_t set_temp_auto_temp_crit(struct device *dev, const char *buf,
949 size_t count, int nr)
951 struct i2c_client *client = to_i2c_client(dev);
952 struct lm85_data *data = i2c_get_clientdata(client);
953 long val = simple_strtol(buf, NULL, 10);
955 down(&data->update_lock);
956 data->zone[nr].critical = TEMP_TO_REG(val);
957 lm85_write_value(client, LM85_REG_AFAN_CRITICAL(nr),
958 data->zone[nr].critical);
959 up(&data->update_lock);
962 #define temp_auto(offset) \
963 static ssize_t show_temp##offset##_auto_temp_off (struct device *dev, struct device_attribute *attr, \
966 return show_temp_auto_temp_off(dev, buf, offset - 1); \
968 static ssize_t set_temp##offset##_auto_temp_off (struct device *dev, struct device_attribute *attr, \
969 const char *buf, size_t count) \
971 return set_temp_auto_temp_off(dev, buf, count, offset - 1); \
973 static ssize_t show_temp##offset##_auto_temp_min (struct device *dev, struct device_attribute *attr, \
976 return show_temp_auto_temp_min(dev, buf, offset - 1); \
978 static ssize_t set_temp##offset##_auto_temp_min (struct device *dev, struct device_attribute *attr, \
979 const char *buf, size_t count) \
981 return set_temp_auto_temp_min(dev, buf, count, offset - 1); \
983 static ssize_t show_temp##offset##_auto_temp_max (struct device *dev, struct device_attribute *attr, \
986 return show_temp_auto_temp_max(dev, buf, offset - 1); \
988 static ssize_t set_temp##offset##_auto_temp_max (struct device *dev, struct device_attribute *attr, \
989 const char *buf, size_t count) \
991 return set_temp_auto_temp_max(dev, buf, count, offset - 1); \
993 static ssize_t show_temp##offset##_auto_temp_crit (struct device *dev, struct device_attribute *attr, \
996 return show_temp_auto_temp_crit(dev, buf, offset - 1); \
998 static ssize_t set_temp##offset##_auto_temp_crit (struct device *dev, struct device_attribute *attr, \
999 const char *buf, size_t count) \
1001 return set_temp_auto_temp_crit(dev, buf, count, offset - 1); \
1003 static DEVICE_ATTR(temp##offset##_auto_temp_off, S_IRUGO | S_IWUSR, \
1004 show_temp##offset##_auto_temp_off, \
1005 set_temp##offset##_auto_temp_off); \
1006 static DEVICE_ATTR(temp##offset##_auto_temp_min, S_IRUGO | S_IWUSR, \
1007 show_temp##offset##_auto_temp_min, \
1008 set_temp##offset##_auto_temp_min); \
1009 static DEVICE_ATTR(temp##offset##_auto_temp_max, S_IRUGO | S_IWUSR, \
1010 show_temp##offset##_auto_temp_max, \
1011 set_temp##offset##_auto_temp_max); \
1012 static DEVICE_ATTR(temp##offset##_auto_temp_crit, S_IRUGO | S_IWUSR, \
1013 show_temp##offset##_auto_temp_crit, \
1014 set_temp##offset##_auto_temp_crit);
1019 static int lm85_attach_adapter(struct i2c_adapter *adapter)
1021 if (!(adapter->class & I2C_CLASS_HWMON))
1023 return i2c_probe(adapter, &addr_data, lm85_detect);
1026 static int lm85_detect(struct i2c_adapter *adapter, int address,
1029 int company, verstep ;
1030 struct i2c_client *new_client = NULL;
1031 struct lm85_data *data;
1033 const char *type_name = "";
1035 if (!i2c_check_functionality(adapter,
1036 I2C_FUNC_SMBUS_BYTE_DATA)) {
1037 /* We need to be able to do byte I/O */
1041 /* OK. For now, we presume we have a valid client. We now create the
1042 client structure, even though we cannot fill it completely yet.
1043 But it allows us to access lm85_{read,write}_value. */
1045 if (!(data = kzalloc(sizeof(struct lm85_data), GFP_KERNEL))) {
1050 new_client = &data->client;
1051 i2c_set_clientdata(new_client, data);
1052 new_client->addr = address;
1053 new_client->adapter = adapter;
1054 new_client->driver = &lm85_driver;
1055 new_client->flags = 0;
1057 /* Now, we do the remaining detection. */
1059 company = lm85_read_value(new_client, LM85_REG_COMPANY);
1060 verstep = lm85_read_value(new_client, LM85_REG_VERSTEP);
1062 dev_dbg(&adapter->dev, "Detecting device at %d,0x%02x with"
1063 " COMPANY: 0x%02x and VERSTEP: 0x%02x\n",
1064 i2c_adapter_id(new_client->adapter), new_client->addr,
1067 /* If auto-detecting, Determine the chip type. */
1069 dev_dbg(&adapter->dev, "Autodetecting device at %d,0x%02x ...\n",
1070 i2c_adapter_id(adapter), address );
1071 if( company == LM85_COMPANY_NATIONAL
1072 && verstep == LM85_VERSTEP_LM85C ) {
1074 } else if( company == LM85_COMPANY_NATIONAL
1075 && verstep == LM85_VERSTEP_LM85B ) {
1077 } else if( company == LM85_COMPANY_NATIONAL
1078 && (verstep & LM85_VERSTEP_VMASK) == LM85_VERSTEP_GENERIC ) {
1079 dev_err(&adapter->dev, "Unrecognized version/stepping 0x%02x"
1080 " Defaulting to LM85.\n", verstep);
1082 } else if( company == LM85_COMPANY_ANALOG_DEV
1083 && verstep == LM85_VERSTEP_ADM1027 ) {
1085 } else if( company == LM85_COMPANY_ANALOG_DEV
1086 && (verstep == LM85_VERSTEP_ADT7463
1087 || verstep == LM85_VERSTEP_ADT7463C) ) {
1089 } else if( company == LM85_COMPANY_ANALOG_DEV
1090 && (verstep & LM85_VERSTEP_VMASK) == LM85_VERSTEP_GENERIC ) {
1091 dev_err(&adapter->dev, "Unrecognized version/stepping 0x%02x"
1092 " Defaulting to Generic LM85.\n", verstep );
1094 } else if( company == LM85_COMPANY_SMSC
1095 && (verstep == LM85_VERSTEP_EMC6D100_A0
1096 || verstep == LM85_VERSTEP_EMC6D100_A1) ) {
1097 /* Unfortunately, we can't tell a '100 from a '101
1098 * from the registers. Since a '101 is a '100
1099 * in a package with fewer pins and therefore no
1100 * 3.3V, 1.5V or 1.8V inputs, perhaps if those
1101 * inputs read 0, then it's a '101.
1104 } else if( company == LM85_COMPANY_SMSC
1105 && verstep == LM85_VERSTEP_EMC6D102) {
1107 } else if( company == LM85_COMPANY_SMSC
1108 && (verstep & LM85_VERSTEP_VMASK) == LM85_VERSTEP_GENERIC) {
1109 dev_err(&adapter->dev, "lm85: Detected SMSC chip\n");
1110 dev_err(&adapter->dev, "lm85: Unrecognized version/stepping 0x%02x"
1111 " Defaulting to Generic LM85.\n", verstep );
1113 } else if( kind == any_chip
1114 && (verstep & LM85_VERSTEP_VMASK) == LM85_VERSTEP_GENERIC) {
1115 dev_err(&adapter->dev, "Generic LM85 Version 6 detected\n");
1116 /* Leave kind as "any_chip" */
1118 dev_dbg(&adapter->dev, "Autodetection failed\n");
1119 /* Not an LM85 ... */
1120 if( kind == any_chip ) { /* User used force=x,y */
1121 dev_err(&adapter->dev, "Generic LM85 Version 6 not"
1122 " found at %d,0x%02x. Try force_lm85c.\n",
1123 i2c_adapter_id(adapter), address );
1130 /* Fill in the chip specific driver values */
1131 if ( kind == any_chip ) {
1133 } else if ( kind == lm85b ) {
1134 type_name = "lm85b";
1135 } else if ( kind == lm85c ) {
1136 type_name = "lm85c";
1137 } else if ( kind == adm1027 ) {
1138 type_name = "adm1027";
1139 } else if ( kind == adt7463 ) {
1140 type_name = "adt7463";
1141 } else if ( kind == emc6d100){
1142 type_name = "emc6d100";
1143 } else if ( kind == emc6d102 ) {
1144 type_name = "emc6d102";
1146 strlcpy(new_client->name, type_name, I2C_NAME_SIZE);
1148 /* Fill in the remaining client fields */
1151 init_MUTEX(&data->update_lock);
1153 /* Tell the I2C layer a new client has arrived */
1154 if ((err = i2c_attach_client(new_client)))
1157 /* Set the VRM version */
1158 data->vrm = vid_which_vrm();
1160 /* Initialize the LM85 chip */
1161 lm85_init_client(new_client);
1163 /* Register sysfs hooks */
1164 data->class_dev = hwmon_device_register(&new_client->dev);
1165 if (IS_ERR(data->class_dev)) {
1166 err = PTR_ERR(data->class_dev);
1170 device_create_file(&new_client->dev, &dev_attr_fan1_input);
1171 device_create_file(&new_client->dev, &dev_attr_fan2_input);
1172 device_create_file(&new_client->dev, &dev_attr_fan3_input);
1173 device_create_file(&new_client->dev, &dev_attr_fan4_input);
1174 device_create_file(&new_client->dev, &dev_attr_fan1_min);
1175 device_create_file(&new_client->dev, &dev_attr_fan2_min);
1176 device_create_file(&new_client->dev, &dev_attr_fan3_min);
1177 device_create_file(&new_client->dev, &dev_attr_fan4_min);
1178 device_create_file(&new_client->dev, &dev_attr_pwm1);
1179 device_create_file(&new_client->dev, &dev_attr_pwm2);
1180 device_create_file(&new_client->dev, &dev_attr_pwm3);
1181 device_create_file(&new_client->dev, &dev_attr_pwm1_enable);
1182 device_create_file(&new_client->dev, &dev_attr_pwm2_enable);
1183 device_create_file(&new_client->dev, &dev_attr_pwm3_enable);
1184 device_create_file(&new_client->dev, &dev_attr_in0_input);
1185 device_create_file(&new_client->dev, &dev_attr_in1_input);
1186 device_create_file(&new_client->dev, &dev_attr_in2_input);
1187 device_create_file(&new_client->dev, &dev_attr_in3_input);
1188 device_create_file(&new_client->dev, &dev_attr_in0_min);
1189 device_create_file(&new_client->dev, &dev_attr_in1_min);
1190 device_create_file(&new_client->dev, &dev_attr_in2_min);
1191 device_create_file(&new_client->dev, &dev_attr_in3_min);
1192 device_create_file(&new_client->dev, &dev_attr_in0_max);
1193 device_create_file(&new_client->dev, &dev_attr_in1_max);
1194 device_create_file(&new_client->dev, &dev_attr_in2_max);
1195 device_create_file(&new_client->dev, &dev_attr_in3_max);
1196 device_create_file(&new_client->dev, &dev_attr_temp1_input);
1197 device_create_file(&new_client->dev, &dev_attr_temp2_input);
1198 device_create_file(&new_client->dev, &dev_attr_temp3_input);
1199 device_create_file(&new_client->dev, &dev_attr_temp1_min);
1200 device_create_file(&new_client->dev, &dev_attr_temp2_min);
1201 device_create_file(&new_client->dev, &dev_attr_temp3_min);
1202 device_create_file(&new_client->dev, &dev_attr_temp1_max);
1203 device_create_file(&new_client->dev, &dev_attr_temp2_max);
1204 device_create_file(&new_client->dev, &dev_attr_temp3_max);
1205 device_create_file(&new_client->dev, &dev_attr_vrm);
1206 device_create_file(&new_client->dev, &dev_attr_cpu0_vid);
1207 device_create_file(&new_client->dev, &dev_attr_alarms);
1208 device_create_file(&new_client->dev, &dev_attr_pwm1_auto_channels);
1209 device_create_file(&new_client->dev, &dev_attr_pwm2_auto_channels);
1210 device_create_file(&new_client->dev, &dev_attr_pwm3_auto_channels);
1211 device_create_file(&new_client->dev, &dev_attr_pwm1_auto_pwm_min);
1212 device_create_file(&new_client->dev, &dev_attr_pwm2_auto_pwm_min);
1213 device_create_file(&new_client->dev, &dev_attr_pwm3_auto_pwm_min);
1214 device_create_file(&new_client->dev, &dev_attr_pwm1_auto_pwm_minctl);
1215 device_create_file(&new_client->dev, &dev_attr_pwm2_auto_pwm_minctl);
1216 device_create_file(&new_client->dev, &dev_attr_pwm3_auto_pwm_minctl);
1217 device_create_file(&new_client->dev, &dev_attr_pwm1_auto_pwm_freq);
1218 device_create_file(&new_client->dev, &dev_attr_pwm2_auto_pwm_freq);
1219 device_create_file(&new_client->dev, &dev_attr_pwm3_auto_pwm_freq);
1220 device_create_file(&new_client->dev, &dev_attr_temp1_auto_temp_off);
1221 device_create_file(&new_client->dev, &dev_attr_temp2_auto_temp_off);
1222 device_create_file(&new_client->dev, &dev_attr_temp3_auto_temp_off);
1223 device_create_file(&new_client->dev, &dev_attr_temp1_auto_temp_min);
1224 device_create_file(&new_client->dev, &dev_attr_temp2_auto_temp_min);
1225 device_create_file(&new_client->dev, &dev_attr_temp3_auto_temp_min);
1226 device_create_file(&new_client->dev, &dev_attr_temp1_auto_temp_max);
1227 device_create_file(&new_client->dev, &dev_attr_temp2_auto_temp_max);
1228 device_create_file(&new_client->dev, &dev_attr_temp3_auto_temp_max);
1229 device_create_file(&new_client->dev, &dev_attr_temp1_auto_temp_crit);
1230 device_create_file(&new_client->dev, &dev_attr_temp2_auto_temp_crit);
1231 device_create_file(&new_client->dev, &dev_attr_temp3_auto_temp_crit);
1233 /* The ADT7463 has an optional VRM 10 mode where pin 21 is used
1234 as a sixth digital VID input rather than an analog input. */
1235 data->vid = lm85_read_value(new_client, LM85_REG_VID);
1236 if (!(kind == adt7463 && (data->vid & 0x80))) {
1237 device_create_file(&new_client->dev, &dev_attr_in4_input);
1238 device_create_file(&new_client->dev, &dev_attr_in4_min);
1239 device_create_file(&new_client->dev, &dev_attr_in4_max);
1244 /* Error out and cleanup code */
1246 i2c_detach_client(new_client);
1253 static int lm85_detach_client(struct i2c_client *client)
1255 struct lm85_data *data = i2c_get_clientdata(client);
1256 hwmon_device_unregister(data->class_dev);
1257 i2c_detach_client(client);
1263 static int lm85_read_value(struct i2c_client *client, u8 reg)
1267 /* What size location is it? */
1269 case LM85_REG_FAN(0) : /* Read WORD data */
1270 case LM85_REG_FAN(1) :
1271 case LM85_REG_FAN(2) :
1272 case LM85_REG_FAN(3) :
1273 case LM85_REG_FAN_MIN(0) :
1274 case LM85_REG_FAN_MIN(1) :
1275 case LM85_REG_FAN_MIN(2) :
1276 case LM85_REG_FAN_MIN(3) :
1277 case LM85_REG_ALARM1 : /* Read both bytes at once */
1278 res = i2c_smbus_read_byte_data(client, reg) & 0xff ;
1279 res |= i2c_smbus_read_byte_data(client, reg+1) << 8 ;
1281 case ADT7463_REG_TMIN_CTL1 : /* Read WORD MSB, LSB */
1282 res = i2c_smbus_read_byte_data(client, reg) << 8 ;
1283 res |= i2c_smbus_read_byte_data(client, reg+1) & 0xff ;
1285 default: /* Read BYTE data */
1286 res = i2c_smbus_read_byte_data(client, reg);
1293 static int lm85_write_value(struct i2c_client *client, u8 reg, int value)
1298 case LM85_REG_FAN(0) : /* Write WORD data */
1299 case LM85_REG_FAN(1) :
1300 case LM85_REG_FAN(2) :
1301 case LM85_REG_FAN(3) :
1302 case LM85_REG_FAN_MIN(0) :
1303 case LM85_REG_FAN_MIN(1) :
1304 case LM85_REG_FAN_MIN(2) :
1305 case LM85_REG_FAN_MIN(3) :
1306 /* NOTE: ALARM is read only, so not included here */
1307 res = i2c_smbus_write_byte_data(client, reg, value & 0xff) ;
1308 res |= i2c_smbus_write_byte_data(client, reg+1, (value>>8) & 0xff) ;
1310 case ADT7463_REG_TMIN_CTL1 : /* Write WORD MSB, LSB */
1311 res = i2c_smbus_write_byte_data(client, reg, (value>>8) & 0xff);
1312 res |= i2c_smbus_write_byte_data(client, reg+1, value & 0xff) ;
1314 default: /* Write BYTE data */
1315 res = i2c_smbus_write_byte_data(client, reg, value);
1322 static void lm85_init_client(struct i2c_client *client)
1325 struct lm85_data *data = i2c_get_clientdata(client);
1327 dev_dbg(&client->dev, "Initializing device\n");
1329 /* Warn if part was not "READY" */
1330 value = lm85_read_value(client, LM85_REG_CONFIG);
1331 dev_dbg(&client->dev, "LM85_REG_CONFIG is: 0x%02x\n", value);
1332 if( value & 0x02 ) {
1333 dev_err(&client->dev, "Client (%d,0x%02x) config is locked.\n",
1334 i2c_adapter_id(client->adapter), client->addr );
1336 if( ! (value & 0x04) ) {
1337 dev_err(&client->dev, "Client (%d,0x%02x) is not ready.\n",
1338 i2c_adapter_id(client->adapter), client->addr );
1341 && ( data->type == adm1027
1342 || data->type == adt7463 ) ) {
1343 dev_err(&client->dev, "Client (%d,0x%02x) VxI mode is set. "
1344 "Please report this to the lm85 maintainer.\n",
1345 i2c_adapter_id(client->adapter), client->addr );
1348 /* WE INTENTIONALLY make no changes to the limits,
1349 * offsets, pwms, fans and zones. If they were
1350 * configured, we don't want to mess with them.
1351 * If they weren't, the default is 100% PWM, no
1352 * control and will suffice until 'sensors -s'
1353 * can be run by the user.
1356 /* Start monitoring */
1357 value = lm85_read_value(client, LM85_REG_CONFIG);
1358 /* Try to clear LOCK, Set START, save everything else */
1359 value = (value & ~ 0x02) | 0x01 ;
1360 dev_dbg(&client->dev, "Setting CONFIG to: 0x%02x\n", value);
1361 lm85_write_value(client, LM85_REG_CONFIG, value);
1364 static struct lm85_data *lm85_update_device(struct device *dev)
1366 struct i2c_client *client = to_i2c_client(dev);
1367 struct lm85_data *data = i2c_get_clientdata(client);
1370 down(&data->update_lock);
1372 if ( !data->valid ||
1373 time_after(jiffies, data->last_reading + LM85_DATA_INTERVAL) ) {
1374 /* Things that change quickly */
1375 dev_dbg(&client->dev, "Reading sensor values\n");
1377 /* Have to read extended bits first to "freeze" the
1378 * more significant bits that are read later.
1380 if ( (data->type == adm1027) || (data->type == adt7463) ) {
1381 int ext1 = lm85_read_value(client,
1382 ADM1027_REG_EXTEND_ADC1);
1383 int ext2 = lm85_read_value(client,
1384 ADM1027_REG_EXTEND_ADC2);
1385 int val = (ext1 << 8) + ext2;
1387 for(i = 0; i <= 4; i++)
1388 data->in_ext[i] = (val>>(i * 2))&0x03;
1390 for(i = 0; i <= 2; i++)
1391 data->temp_ext[i] = (val>>((i + 5) * 2))&0x03;
1394 /* adc_scale is 2^(number of LSBs). There are 4 extra bits in
1395 the emc6d102 and 2 in the adt7463 and adm1027. In all
1396 other chips ext is always 0 and the value of scale is
1397 irrelevant. So it is left in 4*/
1398 data->adc_scale = (data->type == emc6d102 ) ? 16 : 4;
1400 data->vid = lm85_read_value(client, LM85_REG_VID);
1402 for (i = 0; i <= 3; ++i) {
1404 lm85_read_value(client, LM85_REG_IN(i));
1407 if (!(data->type == adt7463 && (data->vid & 0x80))) {
1408 data->in[4] = lm85_read_value(client,
1412 for (i = 0; i <= 3; ++i) {
1414 lm85_read_value(client, LM85_REG_FAN(i));
1417 for (i = 0; i <= 2; ++i) {
1419 lm85_read_value(client, LM85_REG_TEMP(i));
1422 for (i = 0; i <= 2; ++i) {
1424 lm85_read_value(client, LM85_REG_PWM(i));
1427 data->alarms = lm85_read_value(client, LM85_REG_ALARM1);
1429 if ( data->type == adt7463 ) {
1430 if( data->therm_total < ULONG_MAX - 256 ) {
1431 data->therm_total +=
1432 lm85_read_value(client, ADT7463_REG_THERM );
1434 } else if ( data->type == emc6d100 ) {
1435 /* Three more voltage sensors */
1436 for (i = 5; i <= 7; ++i) {
1438 lm85_read_value(client, EMC6D100_REG_IN(i));
1440 /* More alarm bits */
1442 lm85_read_value(client, EMC6D100_REG_ALARM3) << 16;
1443 } else if (data->type == emc6d102 ) {
1444 /* Have to read LSB bits after the MSB ones because
1445 the reading of the MSB bits has frozen the
1446 LSBs (backward from the ADM1027).
1448 int ext1 = lm85_read_value(client,
1449 EMC6D102_REG_EXTEND_ADC1);
1450 int ext2 = lm85_read_value(client,
1451 EMC6D102_REG_EXTEND_ADC2);
1452 int ext3 = lm85_read_value(client,
1453 EMC6D102_REG_EXTEND_ADC3);
1454 int ext4 = lm85_read_value(client,
1455 EMC6D102_REG_EXTEND_ADC4);
1456 data->in_ext[0] = ext3 & 0x0f;
1457 data->in_ext[1] = ext4 & 0x0f;
1458 data->in_ext[2] = (ext4 >> 4) & 0x0f;
1459 data->in_ext[3] = (ext3 >> 4) & 0x0f;
1460 data->in_ext[4] = (ext2 >> 4) & 0x0f;
1462 data->temp_ext[0] = ext1 & 0x0f;
1463 data->temp_ext[1] = ext2 & 0x0f;
1464 data->temp_ext[2] = (ext1 >> 4) & 0x0f;
1467 data->last_reading = jiffies ;
1468 }; /* last_reading */
1470 if ( !data->valid ||
1471 time_after(jiffies, data->last_config + LM85_CONFIG_INTERVAL) ) {
1472 /* Things that don't change often */
1473 dev_dbg(&client->dev, "Reading config values\n");
1475 for (i = 0; i <= 3; ++i) {
1477 lm85_read_value(client, LM85_REG_IN_MIN(i));
1479 lm85_read_value(client, LM85_REG_IN_MAX(i));
1482 if (!(data->type == adt7463 && (data->vid & 0x80))) {
1483 data->in_min[4] = lm85_read_value(client,
1484 LM85_REG_IN_MIN(4));
1485 data->in_max[4] = lm85_read_value(client,
1486 LM85_REG_IN_MAX(4));
1489 if ( data->type == emc6d100 ) {
1490 for (i = 5; i <= 7; ++i) {
1492 lm85_read_value(client, EMC6D100_REG_IN_MIN(i));
1494 lm85_read_value(client, EMC6D100_REG_IN_MAX(i));
1498 for (i = 0; i <= 3; ++i) {
1500 lm85_read_value(client, LM85_REG_FAN_MIN(i));
1503 for (i = 0; i <= 2; ++i) {
1505 lm85_read_value(client, LM85_REG_TEMP_MIN(i));
1507 lm85_read_value(client, LM85_REG_TEMP_MAX(i));
1510 for (i = 0; i <= 2; ++i) {
1512 data->autofan[i].config =
1513 lm85_read_value(client, LM85_REG_AFAN_CONFIG(i));
1514 val = lm85_read_value(client, LM85_REG_AFAN_RANGE(i));
1515 data->autofan[i].freq = val & 0x07 ;
1516 data->zone[i].range = (val >> 4) & 0x0f ;
1517 data->autofan[i].min_pwm =
1518 lm85_read_value(client, LM85_REG_AFAN_MINPWM(i));
1519 data->zone[i].limit =
1520 lm85_read_value(client, LM85_REG_AFAN_LIMIT(i));
1521 data->zone[i].critical =
1522 lm85_read_value(client, LM85_REG_AFAN_CRITICAL(i));
1525 i = lm85_read_value(client, LM85_REG_AFAN_SPIKE1);
1526 data->smooth[0] = i & 0x0f ;
1527 data->syncpwm3 = i & 0x10 ; /* Save PWM3 config */
1528 data->autofan[0].min_off = (i & 0x20) != 0 ;
1529 data->autofan[1].min_off = (i & 0x40) != 0 ;
1530 data->autofan[2].min_off = (i & 0x80) != 0 ;
1531 i = lm85_read_value(client, LM85_REG_AFAN_SPIKE2);
1532 data->smooth[1] = (i>>4) & 0x0f ;
1533 data->smooth[2] = i & 0x0f ;
1535 i = lm85_read_value(client, LM85_REG_AFAN_HYST1);
1536 data->zone[0].hyst = (i>>4) & 0x0f ;
1537 data->zone[1].hyst = i & 0x0f ;
1539 i = lm85_read_value(client, LM85_REG_AFAN_HYST2);
1540 data->zone[2].hyst = (i>>4) & 0x0f ;
1542 if ( (data->type == lm85b) || (data->type == lm85c) ) {
1543 data->tach_mode = lm85_read_value(client,
1544 LM85_REG_TACH_MODE );
1545 data->spinup_ctl = lm85_read_value(client,
1546 LM85_REG_SPINUP_CTL );
1547 } else if ( (data->type == adt7463) || (data->type == adm1027) ) {
1548 if ( data->type == adt7463 ) {
1549 for (i = 0; i <= 2; ++i) {
1550 data->oppoint[i] = lm85_read_value(client,
1551 ADT7463_REG_OPPOINT(i) );
1553 data->tmin_ctl = lm85_read_value(client,
1554 ADT7463_REG_TMIN_CTL1 );
1555 data->therm_limit = lm85_read_value(client,
1556 ADT7463_REG_THERM_LIMIT );
1558 for (i = 0; i <= 2; ++i) {
1559 data->temp_offset[i] = lm85_read_value(client,
1560 ADM1027_REG_TEMP_OFFSET(i) );
1562 data->tach_mode = lm85_read_value(client,
1563 ADM1027_REG_CONFIG3 );
1564 data->fan_ppr = lm85_read_value(client,
1565 ADM1027_REG_FAN_PPR );
1568 data->last_config = jiffies;
1569 }; /* last_config */
1573 up(&data->update_lock);
1579 static int __init sm_lm85_init(void)
1581 return i2c_add_driver(&lm85_driver);
1584 static void __exit sm_lm85_exit(void)
1586 i2c_del_driver(&lm85_driver);
1589 /* Thanks to Richard Barrington for adding the LM85 to sensors-detect.
1590 * Thanks to Margit Schubert-While <margitsw@t-online.de> for help with
1591 * post 2.7.0 CVS changes.
1593 MODULE_LICENSE("GPL");
1594 MODULE_AUTHOR("Philip Pokorny <ppokorny@penguincomputing.com>, Margit Schubert-While <margitsw@t-online.de>, Justin Thiessen <jthiessen@penguincomputing.com");
1595 MODULE_DESCRIPTION("LM85-B, LM85-C driver");
1597 module_init(sm_lm85_init);
1598 module_exit(sm_lm85_exit);