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/hwmon-sysfs.h>
34 #include <linux/err.h>
35 #include <linux/mutex.h>
37 /* Addresses to scan */
38 static unsigned short normal_i2c[] = { 0x2c, 0x2d, 0x2e, I2C_CLIENT_END };
40 /* Insmod parameters */
41 I2C_CLIENT_INSMOD_6(lm85b, lm85c, adm1027, adt7463, emc6d100, emc6d102);
43 /* The LM85 registers */
45 #define LM85_REG_IN(nr) (0x20 + (nr))
46 #define LM85_REG_IN_MIN(nr) (0x44 + (nr) * 2)
47 #define LM85_REG_IN_MAX(nr) (0x45 + (nr) * 2)
49 #define LM85_REG_TEMP(nr) (0x25 + (nr))
50 #define LM85_REG_TEMP_MIN(nr) (0x4e + (nr) * 2)
51 #define LM85_REG_TEMP_MAX(nr) (0x4f + (nr) * 2)
53 /* Fan speeds are LSB, MSB (2 bytes) */
54 #define LM85_REG_FAN(nr) (0x28 + (nr) *2)
55 #define LM85_REG_FAN_MIN(nr) (0x54 + (nr) *2)
57 #define LM85_REG_PWM(nr) (0x30 + (nr))
59 #define ADT7463_REG_OPPOINT(nr) (0x33 + (nr))
61 #define ADT7463_REG_TMIN_CTL1 0x36
62 #define ADT7463_REG_TMIN_CTL2 0x37
64 #define LM85_REG_DEVICE 0x3d
65 #define LM85_REG_COMPANY 0x3e
66 #define LM85_REG_VERSTEP 0x3f
67 /* These are the recognized values for the above regs */
68 #define LM85_DEVICE_ADX 0x27
69 #define LM85_COMPANY_NATIONAL 0x01
70 #define LM85_COMPANY_ANALOG_DEV 0x41
71 #define LM85_COMPANY_SMSC 0x5c
72 #define LM85_VERSTEP_VMASK 0xf0
73 #define LM85_VERSTEP_GENERIC 0x60
74 #define LM85_VERSTEP_LM85C 0x60
75 #define LM85_VERSTEP_LM85B 0x62
76 #define LM85_VERSTEP_ADM1027 0x60
77 #define LM85_VERSTEP_ADT7463 0x62
78 #define LM85_VERSTEP_ADT7463C 0x6A
79 #define LM85_VERSTEP_EMC6D100_A0 0x60
80 #define LM85_VERSTEP_EMC6D100_A1 0x61
81 #define LM85_VERSTEP_EMC6D102 0x65
83 #define LM85_REG_CONFIG 0x40
85 #define LM85_REG_ALARM1 0x41
86 #define LM85_REG_ALARM2 0x42
88 #define LM85_REG_VID 0x43
90 /* Automated FAN control */
91 #define LM85_REG_AFAN_CONFIG(nr) (0x5c + (nr))
92 #define LM85_REG_AFAN_RANGE(nr) (0x5f + (nr))
93 #define LM85_REG_AFAN_SPIKE1 0x62
94 #define LM85_REG_AFAN_SPIKE2 0x63
95 #define LM85_REG_AFAN_MINPWM(nr) (0x64 + (nr))
96 #define LM85_REG_AFAN_LIMIT(nr) (0x67 + (nr))
97 #define LM85_REG_AFAN_CRITICAL(nr) (0x6a + (nr))
98 #define LM85_REG_AFAN_HYST1 0x6d
99 #define LM85_REG_AFAN_HYST2 0x6e
101 #define LM85_REG_TACH_MODE 0x74
102 #define LM85_REG_SPINUP_CTL 0x75
104 #define ADM1027_REG_TEMP_OFFSET(nr) (0x70 + (nr))
105 #define ADM1027_REG_CONFIG2 0x73
106 #define ADM1027_REG_INTMASK1 0x74
107 #define ADM1027_REG_INTMASK2 0x75
108 #define ADM1027_REG_EXTEND_ADC1 0x76
109 #define ADM1027_REG_EXTEND_ADC2 0x77
110 #define ADM1027_REG_CONFIG3 0x78
111 #define ADM1027_REG_FAN_PPR 0x7b
113 #define ADT7463_REG_THERM 0x79
114 #define ADT7463_REG_THERM_LIMIT 0x7A
116 #define EMC6D100_REG_ALARM3 0x7d
117 /* IN5, IN6 and IN7 */
118 #define EMC6D100_REG_IN(nr) (0x70 + ((nr)-5))
119 #define EMC6D100_REG_IN_MIN(nr) (0x73 + ((nr)-5) * 2)
120 #define EMC6D100_REG_IN_MAX(nr) (0x74 + ((nr)-5) * 2)
121 #define EMC6D102_REG_EXTEND_ADC1 0x85
122 #define EMC6D102_REG_EXTEND_ADC2 0x86
123 #define EMC6D102_REG_EXTEND_ADC3 0x87
124 #define EMC6D102_REG_EXTEND_ADC4 0x88
126 #define LM85_ALARM_IN0 0x0001
127 #define LM85_ALARM_IN1 0x0002
128 #define LM85_ALARM_IN2 0x0004
129 #define LM85_ALARM_IN3 0x0008
130 #define LM85_ALARM_TEMP1 0x0010
131 #define LM85_ALARM_TEMP2 0x0020
132 #define LM85_ALARM_TEMP3 0x0040
133 #define LM85_ALARM_ALARM2 0x0080
134 #define LM85_ALARM_IN4 0x0100
135 #define LM85_ALARM_RESERVED 0x0200
136 #define LM85_ALARM_FAN1 0x0400
137 #define LM85_ALARM_FAN2 0x0800
138 #define LM85_ALARM_FAN3 0x1000
139 #define LM85_ALARM_FAN4 0x2000
140 #define LM85_ALARM_TEMP1_FAULT 0x4000
141 #define LM85_ALARM_TEMP3_FAULT 0x8000
144 /* Conversions. Rounding and limit checking is only done on the TO_REG
145 variants. Note that you should be a bit careful with which arguments
146 these macros are called: arguments may be evaluated more than once.
149 /* IN are scaled acording to built-in resistors */
150 static int lm85_scaling[] = { /* .001 Volts */
151 2500, 2250, 3300, 5000, 12000,
152 3300, 1500, 1800 /*EMC6D100*/
154 #define SCALE(val,from,to) (((val)*(to) + ((from)/2))/(from))
156 #define INS_TO_REG(n,val) \
157 SENSORS_LIMIT(SCALE(val,lm85_scaling[n],192),0,255)
159 #define INSEXT_FROM_REG(n,val,ext,scale) \
160 SCALE((val)*(scale) + (ext),192*(scale),lm85_scaling[n])
162 #define INS_FROM_REG(n,val) INSEXT_FROM_REG(n,val,0,1)
164 /* FAN speed is measured using 90kHz clock */
165 #define FAN_TO_REG(val) (SENSORS_LIMIT( (val)<=0?0: 5400000/(val),0,65534))
166 #define FAN_FROM_REG(val) ((val)==0?-1:(val)==0xffff?0:5400000/(val))
168 /* Temperature is reported in .001 degC increments */
169 #define TEMP_TO_REG(val) \
170 SENSORS_LIMIT(SCALE(val,1000,1),-127,127)
171 #define TEMPEXT_FROM_REG(val,ext,scale) \
172 SCALE((val)*scale + (ext),scale,1000)
173 #define TEMP_FROM_REG(val) \
174 TEMPEXT_FROM_REG(val,0,1)
176 #define PWM_TO_REG(val) (SENSORS_LIMIT(val,0,255))
177 #define PWM_FROM_REG(val) (val)
180 /* ZONEs have the following parameters:
181 * Limit (low) temp, 1. degC
182 * Hysteresis (below limit), 1. degC (0-15)
183 * Range of speed control, .1 degC (2-80)
184 * Critical (high) temp, 1. degC
186 * FAN PWMs have the following parameters:
187 * Reference Zone, 1, 2, 3, etc.
188 * Spinup time, .05 sec
189 * PWM value at limit/low temp, 1 count
190 * PWM Frequency, 1. Hz
191 * PWM is Min or OFF below limit, flag
192 * Invert PWM output, flag
194 * Some chips filter the temp, others the fan.
195 * Filter constant (or disabled) .1 seconds
198 /* These are the zone temperature range encodings in .001 degree C */
199 static int lm85_range_map[] = {
200 2000, 2500, 3300, 4000, 5000, 6600,
201 8000, 10000, 13300, 16000, 20000, 26600,
202 32000, 40000, 53300, 80000
204 static int RANGE_TO_REG( int range )
208 if ( range < lm85_range_map[0] ) {
210 } else if ( range > lm85_range_map[15] ) {
212 } else { /* find closest match */
213 for ( i = 14 ; i >= 0 ; --i ) {
214 if ( range > lm85_range_map[i] ) { /* range bracketed */
215 if ((lm85_range_map[i+1] - range) <
216 (range - lm85_range_map[i])) {
226 #define RANGE_FROM_REG(val) (lm85_range_map[(val)&0x0f])
228 /* These are the Acoustic Enhancement, or Temperature smoothing encodings
229 * NOTE: The enable/disable bit is INCLUDED in these encodings as the
230 * MSB (bit 3, value 8). If the enable bit is 0, the encoded value
231 * is ignored, or set to 0.
233 /* These are the PWM frequency encodings */
234 static int lm85_freq_map[] = { /* .1 Hz */
235 100, 150, 230, 300, 380, 470, 620, 940
237 static int FREQ_TO_REG( int freq )
241 if( freq >= lm85_freq_map[7] ) { return 7 ; }
242 for( i = 0 ; i < 7 ; ++i )
243 if( freq <= lm85_freq_map[i] )
247 #define FREQ_FROM_REG(val) (lm85_freq_map[(val)&0x07])
249 /* Since we can't use strings, I'm abusing these numbers
250 * to stand in for the following meanings:
251 * 1 -- PWM responds to Zone 1
252 * 2 -- PWM responds to Zone 2
253 * 3 -- PWM responds to Zone 3
254 * 23 -- PWM responds to the higher temp of Zone 2 or 3
255 * 123 -- PWM responds to highest of Zone 1, 2, or 3
256 * 0 -- PWM is always at 0% (ie, off)
257 * -1 -- PWM is always at 100%
258 * -2 -- PWM responds to manual control
261 static int lm85_zone_map[] = { 1, 2, 3, -1, 0, 23, 123, -2 };
262 #define ZONE_FROM_REG(val) (lm85_zone_map[((val)>>5)&0x07])
264 static int ZONE_TO_REG( int zone )
268 for( i = 0 ; i <= 7 ; ++i )
269 if( zone == lm85_zone_map[i] )
271 if( i > 7 ) /* Not found. */
272 i = 3; /* Always 100% */
273 return( (i & 0x07)<<5 );
276 #define HYST_TO_REG(val) (SENSORS_LIMIT(((val)+500)/1000,0,15))
277 #define HYST_FROM_REG(val) ((val)*1000)
279 #define OFFSET_TO_REG(val) (SENSORS_LIMIT((val)/25,-127,127))
280 #define OFFSET_FROM_REG(val) ((val)*25)
282 #define PPR_MASK(fan) (0x03<<(fan *2))
283 #define PPR_TO_REG(val,fan) (SENSORS_LIMIT((val)-1,0,3)<<(fan *2))
284 #define PPR_FROM_REG(val,fan) ((((val)>>(fan * 2))&0x03)+1)
286 /* Chip sampling rates
288 * Some sensors are not updated more frequently than once per second
289 * so it doesn't make sense to read them more often than that.
290 * We cache the results and return the saved data if the driver
291 * is called again before a second has elapsed.
293 * Also, there is significant configuration data for this chip
294 * given the automatic PWM fan control that is possible. There
295 * are about 47 bytes of config data to only 22 bytes of actual
296 * readings. So, we keep the config data up to date in the cache
297 * when it is written and only sample it once every 1 *minute*
299 #define LM85_DATA_INTERVAL (HZ + HZ / 2)
300 #define LM85_CONFIG_INTERVAL (1 * 60 * HZ)
302 /* LM85 can automatically adjust fan speeds based on temperature
303 * This structure encapsulates an entire Zone config. There are
304 * three zones (one for each temperature input) on the lm85
307 s8 limit; /* Low temp limit */
308 u8 hyst; /* Low limit hysteresis. (0-15) */
309 u8 range; /* Temp range, encoded */
310 s8 critical; /* "All fans ON" temp limit */
311 u8 off_desired; /* Actual "off" temperature specified. Preserved
312 * to prevent "drift" as other autofan control
315 u8 max_desired; /* Actual "max" temperature specified. Preserved
316 * to prevent "drift" as other autofan control
321 struct lm85_autofan {
322 u8 config; /* Register value */
323 u8 freq; /* PWM frequency, encoded */
324 u8 min_pwm; /* Minimum PWM value, encoded */
325 u8 min_off; /* Min PWM or OFF below "limit", flag */
328 /* For each registered chip, we need to keep some data in memory.
329 The structure is dynamically allocated. */
331 struct i2c_client client;
332 struct device *hwmon_dev;
335 struct mutex update_lock;
336 int valid; /* !=0 if following fields are valid */
337 unsigned long last_reading; /* In jiffies */
338 unsigned long last_config; /* In jiffies */
340 u8 in[8]; /* Register value */
341 u8 in_max[8]; /* Register value */
342 u8 in_min[8]; /* Register value */
343 s8 temp[3]; /* Register value */
344 s8 temp_min[3]; /* Register value */
345 s8 temp_max[3]; /* Register value */
346 s8 temp_offset[3]; /* Register value */
347 u16 fan[4]; /* Register value */
348 u16 fan_min[4]; /* Register value */
349 u8 pwm[3]; /* Register value */
350 u8 spinup_ctl; /* Register encoding, combined */
351 u8 tach_mode; /* Register encoding, combined */
352 u8 temp_ext[3]; /* Decoded values */
353 u8 in_ext[8]; /* Decoded values */
354 u8 adc_scale; /* ADC Extended bits scaling factor */
355 u8 fan_ppr; /* Register value */
356 u8 smooth[3]; /* Register encoding */
357 u8 vid; /* Register value */
358 u8 vrm; /* VRM version */
359 u8 syncpwm3; /* Saved PWM3 for TACH 2,3,4 config */
360 u8 oppoint[3]; /* Register value */
361 u16 tmin_ctl; /* Register value */
362 unsigned long therm_total; /* Cummulative therm count */
363 u8 therm_limit; /* Register value */
364 u32 alarms; /* Register encoding, combined */
365 struct lm85_autofan autofan[3];
366 struct lm85_zone zone[3];
369 static int lm85_attach_adapter(struct i2c_adapter *adapter);
370 static int lm85_detect(struct i2c_adapter *adapter, int address,
372 static int lm85_detach_client(struct i2c_client *client);
374 static int lm85_read_value(struct i2c_client *client, u8 reg);
375 static int lm85_write_value(struct i2c_client *client, u8 reg, int value);
376 static struct lm85_data *lm85_update_device(struct device *dev);
377 static void lm85_init_client(struct i2c_client *client);
380 static struct i2c_driver lm85_driver = {
384 .id = I2C_DRIVERID_LM85,
385 .attach_adapter = lm85_attach_adapter,
386 .detach_client = lm85_detach_client,
391 static ssize_t show_fan(struct device *dev, struct device_attribute *attr,
394 int nr = to_sensor_dev_attr(attr)->index;
395 struct lm85_data *data = lm85_update_device(dev);
396 return sprintf(buf,"%d\n", FAN_FROM_REG(data->fan[nr]) );
399 static ssize_t show_fan_min(struct device *dev, struct device_attribute *attr,
402 int nr = to_sensor_dev_attr(attr)->index;
403 struct lm85_data *data = lm85_update_device(dev);
404 return sprintf(buf,"%d\n", FAN_FROM_REG(data->fan_min[nr]) );
407 static ssize_t set_fan_min(struct device *dev, struct device_attribute *attr,
408 const char *buf, size_t count)
410 int nr = to_sensor_dev_attr(attr)->index;
411 struct i2c_client *client = to_i2c_client(dev);
412 struct lm85_data *data = i2c_get_clientdata(client);
413 long val = simple_strtol(buf, NULL, 10);
415 mutex_lock(&data->update_lock);
416 data->fan_min[nr] = FAN_TO_REG(val);
417 lm85_write_value(client, LM85_REG_FAN_MIN(nr), data->fan_min[nr]);
418 mutex_unlock(&data->update_lock);
422 #define show_fan_offset(offset) \
423 static SENSOR_DEVICE_ATTR(fan##offset##_input, S_IRUGO, \
424 show_fan, NULL, offset - 1); \
425 static SENSOR_DEVICE_ATTR(fan##offset##_min, S_IRUGO | S_IWUSR, \
426 show_fan_min, set_fan_min, offset - 1)
433 /* vid, vrm, alarms */
435 static ssize_t show_vid_reg(struct device *dev, struct device_attribute *attr, char *buf)
437 struct lm85_data *data = lm85_update_device(dev);
440 if (data->type == adt7463 && (data->vid & 0x80)) {
441 /* 6-pin VID (VRM 10) */
442 vid = vid_from_reg(data->vid & 0x3f, data->vrm);
444 /* 5-pin VID (VRM 9) */
445 vid = vid_from_reg(data->vid & 0x1f, data->vrm);
448 return sprintf(buf, "%d\n", vid);
451 static DEVICE_ATTR(cpu0_vid, S_IRUGO, show_vid_reg, NULL);
453 static ssize_t show_vrm_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) data->vrm);
459 static ssize_t store_vrm_reg(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
461 struct i2c_client *client = to_i2c_client(dev);
462 struct lm85_data *data = i2c_get_clientdata(client);
465 val = simple_strtoul(buf, NULL, 10);
470 static DEVICE_ATTR(vrm, S_IRUGO | S_IWUSR, show_vrm_reg, store_vrm_reg);
472 static ssize_t show_alarms_reg(struct device *dev, struct device_attribute *attr, char *buf)
474 struct lm85_data *data = lm85_update_device(dev);
475 return sprintf(buf, "%u\n", data->alarms);
478 static DEVICE_ATTR(alarms, S_IRUGO, show_alarms_reg, NULL);
482 static ssize_t show_pwm(struct device *dev, struct device_attribute *attr,
485 int nr = to_sensor_dev_attr(attr)->index;
486 struct lm85_data *data = lm85_update_device(dev);
487 return sprintf(buf,"%d\n", PWM_FROM_REG(data->pwm[nr]) );
490 static ssize_t set_pwm(struct device *dev, struct device_attribute *attr,
491 const char *buf, size_t count)
493 int nr = to_sensor_dev_attr(attr)->index;
494 struct i2c_client *client = to_i2c_client(dev);
495 struct lm85_data *data = i2c_get_clientdata(client);
496 long val = simple_strtol(buf, NULL, 10);
498 mutex_lock(&data->update_lock);
499 data->pwm[nr] = PWM_TO_REG(val);
500 lm85_write_value(client, LM85_REG_PWM(nr), data->pwm[nr]);
501 mutex_unlock(&data->update_lock);
505 static ssize_t show_pwm_enable(struct device *dev, struct device_attribute
508 int nr = to_sensor_dev_attr(attr)->index;
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 SENSOR_DEVICE_ATTR(pwm##offset, S_IRUGO | S_IWUSR, \
518 show_pwm, set_pwm, offset - 1); \
519 static SENSOR_DEVICE_ATTR(pwm##offset##_enable, S_IRUGO, \
520 show_pwm_enable, NULL, offset - 1)
528 static ssize_t show_in(struct device *dev, struct device_attribute *attr,
531 int nr = to_sensor_dev_attr(attr)->index;
532 struct lm85_data *data = lm85_update_device(dev);
533 return sprintf( buf, "%d\n", INSEXT_FROM_REG(nr,
539 static ssize_t show_in_min(struct device *dev, struct device_attribute *attr,
542 int nr = to_sensor_dev_attr(attr)->index;
543 struct lm85_data *data = lm85_update_device(dev);
544 return sprintf(buf,"%d\n", INS_FROM_REG(nr, data->in_min[nr]) );
547 static ssize_t set_in_min(struct device *dev, struct device_attribute *attr,
548 const char *buf, size_t count)
550 int nr = to_sensor_dev_attr(attr)->index;
551 struct i2c_client *client = to_i2c_client(dev);
552 struct lm85_data *data = i2c_get_clientdata(client);
553 long val = simple_strtol(buf, NULL, 10);
555 mutex_lock(&data->update_lock);
556 data->in_min[nr] = INS_TO_REG(nr, val);
557 lm85_write_value(client, LM85_REG_IN_MIN(nr), data->in_min[nr]);
558 mutex_unlock(&data->update_lock);
562 static ssize_t show_in_max(struct device *dev, struct device_attribute *attr,
565 int nr = to_sensor_dev_attr(attr)->index;
566 struct lm85_data *data = lm85_update_device(dev);
567 return sprintf(buf,"%d\n", INS_FROM_REG(nr, data->in_max[nr]) );
570 static ssize_t set_in_max(struct device *dev, struct device_attribute *attr,
571 const char *buf, size_t count)
573 int nr = to_sensor_dev_attr(attr)->index;
574 struct i2c_client *client = to_i2c_client(dev);
575 struct lm85_data *data = i2c_get_clientdata(client);
576 long val = simple_strtol(buf, NULL, 10);
578 mutex_lock(&data->update_lock);
579 data->in_max[nr] = INS_TO_REG(nr, val);
580 lm85_write_value(client, LM85_REG_IN_MAX(nr), data->in_max[nr]);
581 mutex_unlock(&data->update_lock);
585 #define show_in_reg(offset) \
586 static SENSOR_DEVICE_ATTR(in##offset##_input, S_IRUGO, \
587 show_in, NULL, offset); \
588 static SENSOR_DEVICE_ATTR(in##offset##_min, S_IRUGO | S_IWUSR, \
589 show_in_min, set_in_min, offset); \
590 static SENSOR_DEVICE_ATTR(in##offset##_max, S_IRUGO | S_IWUSR, \
591 show_in_max, set_in_max, offset)
601 static ssize_t show_temp(struct device *dev, struct device_attribute *attr,
604 int nr = to_sensor_dev_attr(attr)->index;
605 struct lm85_data *data = lm85_update_device(dev);
606 return sprintf(buf,"%d\n", TEMPEXT_FROM_REG(data->temp[nr],
611 static ssize_t show_temp_min(struct device *dev, struct device_attribute *attr,
614 int nr = to_sensor_dev_attr(attr)->index;
615 struct lm85_data *data = lm85_update_device(dev);
616 return sprintf(buf,"%d\n", TEMP_FROM_REG(data->temp_min[nr]) );
619 static ssize_t set_temp_min(struct device *dev, struct device_attribute *attr,
620 const char *buf, size_t count)
622 int nr = to_sensor_dev_attr(attr)->index;
623 struct i2c_client *client = to_i2c_client(dev);
624 struct lm85_data *data = i2c_get_clientdata(client);
625 long val = simple_strtol(buf, NULL, 10);
627 mutex_lock(&data->update_lock);
628 data->temp_min[nr] = TEMP_TO_REG(val);
629 lm85_write_value(client, LM85_REG_TEMP_MIN(nr), data->temp_min[nr]);
630 mutex_unlock(&data->update_lock);
634 static ssize_t show_temp_max(struct device *dev, struct device_attribute *attr,
637 int nr = to_sensor_dev_attr(attr)->index;
638 struct lm85_data *data = lm85_update_device(dev);
639 return sprintf(buf,"%d\n", TEMP_FROM_REG(data->temp_max[nr]) );
642 static ssize_t set_temp_max(struct device *dev, struct device_attribute *attr,
643 const char *buf, size_t count)
645 int nr = to_sensor_dev_attr(attr)->index;
646 struct i2c_client *client = to_i2c_client(dev);
647 struct lm85_data *data = i2c_get_clientdata(client);
648 long val = simple_strtol(buf, NULL, 10);
650 mutex_lock(&data->update_lock);
651 data->temp_max[nr] = TEMP_TO_REG(val);
652 lm85_write_value(client, LM85_REG_TEMP_MAX(nr), data->temp_max[nr]);
653 mutex_unlock(&data->update_lock);
657 #define show_temp_reg(offset) \
658 static SENSOR_DEVICE_ATTR(temp##offset##_input, S_IRUGO, \
659 show_temp, NULL, offset - 1); \
660 static SENSOR_DEVICE_ATTR(temp##offset##_min, S_IRUGO | S_IWUSR, \
661 show_temp_min, set_temp_min, offset - 1); \
662 static SENSOR_DEVICE_ATTR(temp##offset##_max, S_IRUGO | S_IWUSR, \
663 show_temp_max, set_temp_max, offset - 1);
670 /* Automatic PWM control */
672 static ssize_t show_pwm_auto_channels(struct device *dev,
673 struct device_attribute *attr, char *buf)
675 int nr = to_sensor_dev_attr(attr)->index;
676 struct lm85_data *data = lm85_update_device(dev);
677 return sprintf(buf,"%d\n", ZONE_FROM_REG(data->autofan[nr].config));
680 static ssize_t set_pwm_auto_channels(struct device *dev,
681 struct device_attribute *attr, const char *buf, size_t count)
683 int nr = to_sensor_dev_attr(attr)->index;
684 struct i2c_client *client = to_i2c_client(dev);
685 struct lm85_data *data = i2c_get_clientdata(client);
686 long val = simple_strtol(buf, NULL, 10);
688 mutex_lock(&data->update_lock);
689 data->autofan[nr].config = (data->autofan[nr].config & (~0xe0))
691 lm85_write_value(client, LM85_REG_AFAN_CONFIG(nr),
692 data->autofan[nr].config);
693 mutex_unlock(&data->update_lock);
697 static ssize_t show_pwm_auto_pwm_min(struct device *dev,
698 struct device_attribute *attr, char *buf)
700 int nr = to_sensor_dev_attr(attr)->index;
701 struct lm85_data *data = lm85_update_device(dev);
702 return sprintf(buf,"%d\n", PWM_FROM_REG(data->autofan[nr].min_pwm));
705 static ssize_t set_pwm_auto_pwm_min(struct device *dev,
706 struct device_attribute *attr, const char *buf, size_t count)
708 int nr = to_sensor_dev_attr(attr)->index;
709 struct i2c_client *client = to_i2c_client(dev);
710 struct lm85_data *data = i2c_get_clientdata(client);
711 long val = simple_strtol(buf, NULL, 10);
713 mutex_lock(&data->update_lock);
714 data->autofan[nr].min_pwm = PWM_TO_REG(val);
715 lm85_write_value(client, LM85_REG_AFAN_MINPWM(nr),
716 data->autofan[nr].min_pwm);
717 mutex_unlock(&data->update_lock);
721 static ssize_t show_pwm_auto_pwm_minctl(struct device *dev,
722 struct device_attribute *attr, char *buf)
724 int nr = to_sensor_dev_attr(attr)->index;
725 struct lm85_data *data = lm85_update_device(dev);
726 return sprintf(buf,"%d\n", data->autofan[nr].min_off);
729 static ssize_t set_pwm_auto_pwm_minctl(struct device *dev,
730 struct device_attribute *attr, const char *buf, size_t count)
732 int nr = to_sensor_dev_attr(attr)->index;
733 struct i2c_client *client = to_i2c_client(dev);
734 struct lm85_data *data = i2c_get_clientdata(client);
735 long val = simple_strtol(buf, NULL, 10);
737 mutex_lock(&data->update_lock);
738 data->autofan[nr].min_off = val;
739 lm85_write_value(client, LM85_REG_AFAN_SPIKE1, data->smooth[0]
741 | (data->autofan[0].min_off ? 0x20 : 0)
742 | (data->autofan[1].min_off ? 0x40 : 0)
743 | (data->autofan[2].min_off ? 0x80 : 0)
745 mutex_unlock(&data->update_lock);
749 static ssize_t show_pwm_auto_pwm_freq(struct device *dev,
750 struct device_attribute *attr, char *buf)
752 int nr = to_sensor_dev_attr(attr)->index;
753 struct lm85_data *data = lm85_update_device(dev);
754 return sprintf(buf,"%d\n", FREQ_FROM_REG(data->autofan[nr].freq));
757 static ssize_t set_pwm_auto_pwm_freq(struct device *dev,
758 struct device_attribute *attr, const char *buf, size_t count)
760 int nr = to_sensor_dev_attr(attr)->index;
761 struct i2c_client *client = to_i2c_client(dev);
762 struct lm85_data *data = i2c_get_clientdata(client);
763 long val = simple_strtol(buf, NULL, 10);
765 mutex_lock(&data->update_lock);
766 data->autofan[nr].freq = FREQ_TO_REG(val);
767 lm85_write_value(client, LM85_REG_AFAN_RANGE(nr),
768 (data->zone[nr].range << 4)
769 | data->autofan[nr].freq
771 mutex_unlock(&data->update_lock);
775 #define pwm_auto(offset) \
776 static SENSOR_DEVICE_ATTR(pwm##offset##_auto_channels, \
777 S_IRUGO | S_IWUSR, show_pwm_auto_channels, \
778 set_pwm_auto_channels, offset - 1); \
779 static SENSOR_DEVICE_ATTR(pwm##offset##_auto_pwm_min, \
780 S_IRUGO | S_IWUSR, show_pwm_auto_pwm_min, \
781 set_pwm_auto_pwm_min, offset - 1); \
782 static SENSOR_DEVICE_ATTR(pwm##offset##_auto_pwm_minctl, \
783 S_IRUGO | S_IWUSR, show_pwm_auto_pwm_minctl, \
784 set_pwm_auto_pwm_minctl, offset - 1); \
785 static SENSOR_DEVICE_ATTR(pwm##offset##_auto_pwm_freq, \
786 S_IRUGO | S_IWUSR, show_pwm_auto_pwm_freq, \
787 set_pwm_auto_pwm_freq, offset - 1);
793 /* Temperature settings for automatic PWM control */
795 static ssize_t show_temp_auto_temp_off(struct device *dev,
796 struct device_attribute *attr, char *buf)
798 int nr = to_sensor_dev_attr(attr)->index;
799 struct lm85_data *data = lm85_update_device(dev);
800 return sprintf(buf,"%d\n", TEMP_FROM_REG(data->zone[nr].limit) -
801 HYST_FROM_REG(data->zone[nr].hyst));
804 static ssize_t set_temp_auto_temp_off(struct device *dev,
805 struct device_attribute *attr, const char *buf, size_t count)
807 int nr = to_sensor_dev_attr(attr)->index;
808 struct i2c_client *client = to_i2c_client(dev);
809 struct lm85_data *data = i2c_get_clientdata(client);
811 long val = simple_strtol(buf, NULL, 10);
813 mutex_lock(&data->update_lock);
814 min = TEMP_FROM_REG(data->zone[nr].limit);
815 data->zone[nr].off_desired = TEMP_TO_REG(val);
816 data->zone[nr].hyst = HYST_TO_REG(min - val);
817 if ( nr == 0 || nr == 1 ) {
818 lm85_write_value(client, LM85_REG_AFAN_HYST1,
819 (data->zone[0].hyst << 4)
823 lm85_write_value(client, LM85_REG_AFAN_HYST2,
824 (data->zone[2].hyst << 4)
827 mutex_unlock(&data->update_lock);
831 static ssize_t show_temp_auto_temp_min(struct device *dev,
832 struct device_attribute *attr, char *buf)
834 int nr = to_sensor_dev_attr(attr)->index;
835 struct lm85_data *data = lm85_update_device(dev);
836 return sprintf(buf,"%d\n", TEMP_FROM_REG(data->zone[nr].limit) );
839 static ssize_t set_temp_auto_temp_min(struct device *dev,
840 struct device_attribute *attr, const char *buf, size_t count)
842 int nr = to_sensor_dev_attr(attr)->index;
843 struct i2c_client *client = to_i2c_client(dev);
844 struct lm85_data *data = i2c_get_clientdata(client);
845 long val = simple_strtol(buf, NULL, 10);
847 mutex_lock(&data->update_lock);
848 data->zone[nr].limit = TEMP_TO_REG(val);
849 lm85_write_value(client, LM85_REG_AFAN_LIMIT(nr),
850 data->zone[nr].limit);
852 /* Update temp_auto_max and temp_auto_range */
853 data->zone[nr].range = RANGE_TO_REG(
854 TEMP_FROM_REG(data->zone[nr].max_desired) -
855 TEMP_FROM_REG(data->zone[nr].limit));
856 lm85_write_value(client, LM85_REG_AFAN_RANGE(nr),
857 ((data->zone[nr].range & 0x0f) << 4)
858 | (data->autofan[nr].freq & 0x07));
860 /* Update temp_auto_hyst and temp_auto_off */
861 data->zone[nr].hyst = HYST_TO_REG(TEMP_FROM_REG(
862 data->zone[nr].limit) - TEMP_FROM_REG(
863 data->zone[nr].off_desired));
864 if ( nr == 0 || nr == 1 ) {
865 lm85_write_value(client, LM85_REG_AFAN_HYST1,
866 (data->zone[0].hyst << 4)
870 lm85_write_value(client, LM85_REG_AFAN_HYST2,
871 (data->zone[2].hyst << 4)
874 mutex_unlock(&data->update_lock);
878 static ssize_t show_temp_auto_temp_max(struct device *dev,
879 struct device_attribute *attr, char *buf)
881 int nr = to_sensor_dev_attr(attr)->index;
882 struct lm85_data *data = lm85_update_device(dev);
883 return sprintf(buf,"%d\n", TEMP_FROM_REG(data->zone[nr].limit) +
884 RANGE_FROM_REG(data->zone[nr].range));
887 static ssize_t set_temp_auto_temp_max(struct device *dev,
888 struct device_attribute *attr, const char *buf, size_t count)
890 int nr = to_sensor_dev_attr(attr)->index;
891 struct i2c_client *client = to_i2c_client(dev);
892 struct lm85_data *data = i2c_get_clientdata(client);
894 long val = simple_strtol(buf, NULL, 10);
896 mutex_lock(&data->update_lock);
897 min = TEMP_FROM_REG(data->zone[nr].limit);
898 data->zone[nr].max_desired = TEMP_TO_REG(val);
899 data->zone[nr].range = RANGE_TO_REG(
901 lm85_write_value(client, LM85_REG_AFAN_RANGE(nr),
902 ((data->zone[nr].range & 0x0f) << 4)
903 | (data->autofan[nr].freq & 0x07));
904 mutex_unlock(&data->update_lock);
908 static ssize_t show_temp_auto_temp_crit(struct device *dev,
909 struct device_attribute *attr, char *buf)
911 int nr = to_sensor_dev_attr(attr)->index;
912 struct lm85_data *data = lm85_update_device(dev);
913 return sprintf(buf,"%d\n", TEMP_FROM_REG(data->zone[nr].critical));
916 static ssize_t set_temp_auto_temp_crit(struct device *dev,
917 struct device_attribute *attr,const char *buf, size_t count)
919 int nr = to_sensor_dev_attr(attr)->index;
920 struct i2c_client *client = to_i2c_client(dev);
921 struct lm85_data *data = i2c_get_clientdata(client);
922 long val = simple_strtol(buf, NULL, 10);
924 mutex_lock(&data->update_lock);
925 data->zone[nr].critical = TEMP_TO_REG(val);
926 lm85_write_value(client, LM85_REG_AFAN_CRITICAL(nr),
927 data->zone[nr].critical);
928 mutex_unlock(&data->update_lock);
932 #define temp_auto(offset) \
933 static SENSOR_DEVICE_ATTR(temp##offset##_auto_temp_off, \
934 S_IRUGO | S_IWUSR, show_temp_auto_temp_off, \
935 set_temp_auto_temp_off, offset - 1); \
936 static SENSOR_DEVICE_ATTR(temp##offset##_auto_temp_min, \
937 S_IRUGO | S_IWUSR, show_temp_auto_temp_min, \
938 set_temp_auto_temp_min, offset - 1); \
939 static SENSOR_DEVICE_ATTR(temp##offset##_auto_temp_max, \
940 S_IRUGO | S_IWUSR, show_temp_auto_temp_max, \
941 set_temp_auto_temp_max, offset - 1); \
942 static SENSOR_DEVICE_ATTR(temp##offset##_auto_temp_crit, \
943 S_IRUGO | S_IWUSR, show_temp_auto_temp_crit, \
944 set_temp_auto_temp_crit, offset - 1);
950 static int lm85_attach_adapter(struct i2c_adapter *adapter)
952 if (!(adapter->class & I2C_CLASS_HWMON))
954 return i2c_probe(adapter, &addr_data, lm85_detect);
957 static struct attribute *lm85_attributes[] = {
958 &sensor_dev_attr_fan1_input.dev_attr.attr,
959 &sensor_dev_attr_fan2_input.dev_attr.attr,
960 &sensor_dev_attr_fan3_input.dev_attr.attr,
961 &sensor_dev_attr_fan4_input.dev_attr.attr,
962 &sensor_dev_attr_fan1_min.dev_attr.attr,
963 &sensor_dev_attr_fan2_min.dev_attr.attr,
964 &sensor_dev_attr_fan3_min.dev_attr.attr,
965 &sensor_dev_attr_fan4_min.dev_attr.attr,
967 &sensor_dev_attr_pwm1.dev_attr.attr,
968 &sensor_dev_attr_pwm2.dev_attr.attr,
969 &sensor_dev_attr_pwm3.dev_attr.attr,
970 &sensor_dev_attr_pwm1_enable.dev_attr.attr,
971 &sensor_dev_attr_pwm2_enable.dev_attr.attr,
972 &sensor_dev_attr_pwm3_enable.dev_attr.attr,
974 &sensor_dev_attr_in0_input.dev_attr.attr,
975 &sensor_dev_attr_in1_input.dev_attr.attr,
976 &sensor_dev_attr_in2_input.dev_attr.attr,
977 &sensor_dev_attr_in3_input.dev_attr.attr,
978 &sensor_dev_attr_in0_min.dev_attr.attr,
979 &sensor_dev_attr_in1_min.dev_attr.attr,
980 &sensor_dev_attr_in2_min.dev_attr.attr,
981 &sensor_dev_attr_in3_min.dev_attr.attr,
982 &sensor_dev_attr_in0_max.dev_attr.attr,
983 &sensor_dev_attr_in1_max.dev_attr.attr,
984 &sensor_dev_attr_in2_max.dev_attr.attr,
985 &sensor_dev_attr_in3_max.dev_attr.attr,
987 &sensor_dev_attr_temp1_input.dev_attr.attr,
988 &sensor_dev_attr_temp2_input.dev_attr.attr,
989 &sensor_dev_attr_temp3_input.dev_attr.attr,
990 &sensor_dev_attr_temp1_min.dev_attr.attr,
991 &sensor_dev_attr_temp2_min.dev_attr.attr,
992 &sensor_dev_attr_temp3_min.dev_attr.attr,
993 &sensor_dev_attr_temp1_max.dev_attr.attr,
994 &sensor_dev_attr_temp2_max.dev_attr.attr,
995 &sensor_dev_attr_temp3_max.dev_attr.attr,
997 &sensor_dev_attr_pwm1_auto_channels.dev_attr.attr,
998 &sensor_dev_attr_pwm2_auto_channels.dev_attr.attr,
999 &sensor_dev_attr_pwm3_auto_channels.dev_attr.attr,
1000 &sensor_dev_attr_pwm1_auto_pwm_min.dev_attr.attr,
1001 &sensor_dev_attr_pwm2_auto_pwm_min.dev_attr.attr,
1002 &sensor_dev_attr_pwm3_auto_pwm_min.dev_attr.attr,
1003 &sensor_dev_attr_pwm1_auto_pwm_minctl.dev_attr.attr,
1004 &sensor_dev_attr_pwm2_auto_pwm_minctl.dev_attr.attr,
1005 &sensor_dev_attr_pwm3_auto_pwm_minctl.dev_attr.attr,
1006 &sensor_dev_attr_pwm1_auto_pwm_freq.dev_attr.attr,
1007 &sensor_dev_attr_pwm2_auto_pwm_freq.dev_attr.attr,
1008 &sensor_dev_attr_pwm3_auto_pwm_freq.dev_attr.attr,
1010 &sensor_dev_attr_temp1_auto_temp_off.dev_attr.attr,
1011 &sensor_dev_attr_temp2_auto_temp_off.dev_attr.attr,
1012 &sensor_dev_attr_temp3_auto_temp_off.dev_attr.attr,
1013 &sensor_dev_attr_temp1_auto_temp_min.dev_attr.attr,
1014 &sensor_dev_attr_temp2_auto_temp_min.dev_attr.attr,
1015 &sensor_dev_attr_temp3_auto_temp_min.dev_attr.attr,
1016 &sensor_dev_attr_temp1_auto_temp_max.dev_attr.attr,
1017 &sensor_dev_attr_temp2_auto_temp_max.dev_attr.attr,
1018 &sensor_dev_attr_temp3_auto_temp_max.dev_attr.attr,
1019 &sensor_dev_attr_temp1_auto_temp_crit.dev_attr.attr,
1020 &sensor_dev_attr_temp2_auto_temp_crit.dev_attr.attr,
1021 &sensor_dev_attr_temp3_auto_temp_crit.dev_attr.attr,
1024 &dev_attr_cpu0_vid.attr,
1025 &dev_attr_alarms.attr,
1029 static const struct attribute_group lm85_group = {
1030 .attrs = lm85_attributes,
1033 static struct attribute *lm85_attributes_opt[] = {
1034 &sensor_dev_attr_in4_input.dev_attr.attr,
1035 &sensor_dev_attr_in4_min.dev_attr.attr,
1036 &sensor_dev_attr_in4_max.dev_attr.attr,
1040 static const struct attribute_group lm85_group_opt = {
1041 .attrs = lm85_attributes_opt,
1044 static int lm85_detect(struct i2c_adapter *adapter, int address,
1047 int company, verstep ;
1048 struct i2c_client *new_client = NULL;
1049 struct lm85_data *data;
1051 const char *type_name = "";
1053 if (!i2c_check_functionality(adapter,
1054 I2C_FUNC_SMBUS_BYTE_DATA)) {
1055 /* We need to be able to do byte I/O */
1059 /* OK. For now, we presume we have a valid client. We now create the
1060 client structure, even though we cannot fill it completely yet.
1061 But it allows us to access lm85_{read,write}_value. */
1063 if (!(data = kzalloc(sizeof(struct lm85_data), GFP_KERNEL))) {
1068 new_client = &data->client;
1069 i2c_set_clientdata(new_client, data);
1070 new_client->addr = address;
1071 new_client->adapter = adapter;
1072 new_client->driver = &lm85_driver;
1073 new_client->flags = 0;
1075 /* Now, we do the remaining detection. */
1077 company = lm85_read_value(new_client, LM85_REG_COMPANY);
1078 verstep = lm85_read_value(new_client, LM85_REG_VERSTEP);
1080 dev_dbg(&adapter->dev, "Detecting device at %d,0x%02x with"
1081 " COMPANY: 0x%02x and VERSTEP: 0x%02x\n",
1082 i2c_adapter_id(new_client->adapter), new_client->addr,
1085 /* If auto-detecting, Determine the chip type. */
1087 dev_dbg(&adapter->dev, "Autodetecting device at %d,0x%02x ...\n",
1088 i2c_adapter_id(adapter), address );
1089 if( company == LM85_COMPANY_NATIONAL
1090 && verstep == LM85_VERSTEP_LM85C ) {
1092 } else if( company == LM85_COMPANY_NATIONAL
1093 && verstep == LM85_VERSTEP_LM85B ) {
1095 } else if( company == LM85_COMPANY_NATIONAL
1096 && (verstep & LM85_VERSTEP_VMASK) == LM85_VERSTEP_GENERIC ) {
1097 dev_err(&adapter->dev, "Unrecognized version/stepping 0x%02x"
1098 " Defaulting to LM85.\n", verstep);
1100 } else if( company == LM85_COMPANY_ANALOG_DEV
1101 && verstep == LM85_VERSTEP_ADM1027 ) {
1103 } else if( company == LM85_COMPANY_ANALOG_DEV
1104 && (verstep == LM85_VERSTEP_ADT7463
1105 || verstep == LM85_VERSTEP_ADT7463C) ) {
1107 } else if( company == LM85_COMPANY_ANALOG_DEV
1108 && (verstep & LM85_VERSTEP_VMASK) == LM85_VERSTEP_GENERIC ) {
1109 dev_err(&adapter->dev, "Unrecognized version/stepping 0x%02x"
1110 " Defaulting to Generic LM85.\n", verstep );
1112 } else if( company == LM85_COMPANY_SMSC
1113 && (verstep == LM85_VERSTEP_EMC6D100_A0
1114 || verstep == LM85_VERSTEP_EMC6D100_A1) ) {
1115 /* Unfortunately, we can't tell a '100 from a '101
1116 * from the registers. Since a '101 is a '100
1117 * in a package with fewer pins and therefore no
1118 * 3.3V, 1.5V or 1.8V inputs, perhaps if those
1119 * inputs read 0, then it's a '101.
1122 } else if( company == LM85_COMPANY_SMSC
1123 && verstep == LM85_VERSTEP_EMC6D102) {
1125 } else if( company == LM85_COMPANY_SMSC
1126 && (verstep & LM85_VERSTEP_VMASK) == LM85_VERSTEP_GENERIC) {
1127 dev_err(&adapter->dev, "lm85: Detected SMSC chip\n");
1128 dev_err(&adapter->dev, "lm85: Unrecognized version/stepping 0x%02x"
1129 " Defaulting to Generic LM85.\n", verstep );
1131 } else if( kind == any_chip
1132 && (verstep & LM85_VERSTEP_VMASK) == LM85_VERSTEP_GENERIC) {
1133 dev_err(&adapter->dev, "Generic LM85 Version 6 detected\n");
1134 /* Leave kind as "any_chip" */
1136 dev_dbg(&adapter->dev, "Autodetection failed\n");
1137 /* Not an LM85 ... */
1138 if( kind == any_chip ) { /* User used force=x,y */
1139 dev_err(&adapter->dev, "Generic LM85 Version 6 not"
1140 " found at %d,0x%02x. Try force_lm85c.\n",
1141 i2c_adapter_id(adapter), address );
1148 /* Fill in the chip specific driver values */
1149 if ( kind == any_chip ) {
1151 } else if ( kind == lm85b ) {
1152 type_name = "lm85b";
1153 } else if ( kind == lm85c ) {
1154 type_name = "lm85c";
1155 } else if ( kind == adm1027 ) {
1156 type_name = "adm1027";
1157 } else if ( kind == adt7463 ) {
1158 type_name = "adt7463";
1159 } else if ( kind == emc6d100){
1160 type_name = "emc6d100";
1161 } else if ( kind == emc6d102 ) {
1162 type_name = "emc6d102";
1164 strlcpy(new_client->name, type_name, I2C_NAME_SIZE);
1166 /* Fill in the remaining client fields */
1169 mutex_init(&data->update_lock);
1171 /* Tell the I2C layer a new client has arrived */
1172 if ((err = i2c_attach_client(new_client)))
1175 /* Set the VRM version */
1176 data->vrm = vid_which_vrm();
1178 /* Initialize the LM85 chip */
1179 lm85_init_client(new_client);
1181 /* Register sysfs hooks */
1182 if ((err = sysfs_create_group(&new_client->dev.kobj, &lm85_group)))
1185 /* The ADT7463 has an optional VRM 10 mode where pin 21 is used
1186 as a sixth digital VID input rather than an analog input. */
1187 data->vid = lm85_read_value(new_client, LM85_REG_VID);
1188 if (!(kind == adt7463 && (data->vid & 0x80)))
1189 if ((err = device_create_file(&new_client->dev,
1190 &sensor_dev_attr_in4_input.dev_attr))
1191 || (err = device_create_file(&new_client->dev,
1192 &sensor_dev_attr_in4_min.dev_attr))
1193 || (err = device_create_file(&new_client->dev,
1194 &sensor_dev_attr_in4_max.dev_attr)))
1197 data->hwmon_dev = hwmon_device_register(&new_client->dev);
1198 if (IS_ERR(data->hwmon_dev)) {
1199 err = PTR_ERR(data->hwmon_dev);
1205 /* Error out and cleanup code */
1207 sysfs_remove_group(&new_client->dev.kobj, &lm85_group);
1208 sysfs_remove_group(&new_client->dev.kobj, &lm85_group_opt);
1210 i2c_detach_client(new_client);
1217 static int lm85_detach_client(struct i2c_client *client)
1219 struct lm85_data *data = i2c_get_clientdata(client);
1220 hwmon_device_unregister(data->hwmon_dev);
1221 sysfs_remove_group(&client->dev.kobj, &lm85_group);
1222 sysfs_remove_group(&client->dev.kobj, &lm85_group_opt);
1223 i2c_detach_client(client);
1229 static int lm85_read_value(struct i2c_client *client, u8 reg)
1233 /* What size location is it? */
1235 case LM85_REG_FAN(0) : /* Read WORD data */
1236 case LM85_REG_FAN(1) :
1237 case LM85_REG_FAN(2) :
1238 case LM85_REG_FAN(3) :
1239 case LM85_REG_FAN_MIN(0) :
1240 case LM85_REG_FAN_MIN(1) :
1241 case LM85_REG_FAN_MIN(2) :
1242 case LM85_REG_FAN_MIN(3) :
1243 case LM85_REG_ALARM1 : /* Read both bytes at once */
1244 res = i2c_smbus_read_byte_data(client, reg) & 0xff ;
1245 res |= i2c_smbus_read_byte_data(client, reg+1) << 8 ;
1247 case ADT7463_REG_TMIN_CTL1 : /* Read WORD MSB, LSB */
1248 res = i2c_smbus_read_byte_data(client, reg) << 8 ;
1249 res |= i2c_smbus_read_byte_data(client, reg+1) & 0xff ;
1251 default: /* Read BYTE data */
1252 res = i2c_smbus_read_byte_data(client, reg);
1259 static int lm85_write_value(struct i2c_client *client, u8 reg, int value)
1264 case LM85_REG_FAN(0) : /* Write WORD data */
1265 case LM85_REG_FAN(1) :
1266 case LM85_REG_FAN(2) :
1267 case LM85_REG_FAN(3) :
1268 case LM85_REG_FAN_MIN(0) :
1269 case LM85_REG_FAN_MIN(1) :
1270 case LM85_REG_FAN_MIN(2) :
1271 case LM85_REG_FAN_MIN(3) :
1272 /* NOTE: ALARM is read only, so not included here */
1273 res = i2c_smbus_write_byte_data(client, reg, value & 0xff) ;
1274 res |= i2c_smbus_write_byte_data(client, reg+1, (value>>8) & 0xff) ;
1276 case ADT7463_REG_TMIN_CTL1 : /* Write WORD MSB, LSB */
1277 res = i2c_smbus_write_byte_data(client, reg, (value>>8) & 0xff);
1278 res |= i2c_smbus_write_byte_data(client, reg+1, value & 0xff) ;
1280 default: /* Write BYTE data */
1281 res = i2c_smbus_write_byte_data(client, reg, value);
1288 static void lm85_init_client(struct i2c_client *client)
1291 struct lm85_data *data = i2c_get_clientdata(client);
1293 dev_dbg(&client->dev, "Initializing device\n");
1295 /* Warn if part was not "READY" */
1296 value = lm85_read_value(client, LM85_REG_CONFIG);
1297 dev_dbg(&client->dev, "LM85_REG_CONFIG is: 0x%02x\n", value);
1298 if( value & 0x02 ) {
1299 dev_err(&client->dev, "Client (%d,0x%02x) config is locked.\n",
1300 i2c_adapter_id(client->adapter), client->addr );
1302 if( ! (value & 0x04) ) {
1303 dev_err(&client->dev, "Client (%d,0x%02x) is not ready.\n",
1304 i2c_adapter_id(client->adapter), client->addr );
1307 && ( data->type == adm1027
1308 || data->type == adt7463 ) ) {
1309 dev_err(&client->dev, "Client (%d,0x%02x) VxI mode is set. "
1310 "Please report this to the lm85 maintainer.\n",
1311 i2c_adapter_id(client->adapter), client->addr );
1314 /* WE INTENTIONALLY make no changes to the limits,
1315 * offsets, pwms, fans and zones. If they were
1316 * configured, we don't want to mess with them.
1317 * If they weren't, the default is 100% PWM, no
1318 * control and will suffice until 'sensors -s'
1319 * can be run by the user.
1322 /* Start monitoring */
1323 value = lm85_read_value(client, LM85_REG_CONFIG);
1324 /* Try to clear LOCK, Set START, save everything else */
1325 value = (value & ~ 0x02) | 0x01 ;
1326 dev_dbg(&client->dev, "Setting CONFIG to: 0x%02x\n", value);
1327 lm85_write_value(client, LM85_REG_CONFIG, value);
1330 static struct lm85_data *lm85_update_device(struct device *dev)
1332 struct i2c_client *client = to_i2c_client(dev);
1333 struct lm85_data *data = i2c_get_clientdata(client);
1336 mutex_lock(&data->update_lock);
1338 if ( !data->valid ||
1339 time_after(jiffies, data->last_reading + LM85_DATA_INTERVAL) ) {
1340 /* Things that change quickly */
1341 dev_dbg(&client->dev, "Reading sensor values\n");
1343 /* Have to read extended bits first to "freeze" the
1344 * more significant bits that are read later.
1346 if ( (data->type == adm1027) || (data->type == adt7463) ) {
1347 int ext1 = lm85_read_value(client,
1348 ADM1027_REG_EXTEND_ADC1);
1349 int ext2 = lm85_read_value(client,
1350 ADM1027_REG_EXTEND_ADC2);
1351 int val = (ext1 << 8) + ext2;
1353 for(i = 0; i <= 4; i++)
1354 data->in_ext[i] = (val>>(i * 2))&0x03;
1356 for(i = 0; i <= 2; i++)
1357 data->temp_ext[i] = (val>>((i + 5) * 2))&0x03;
1360 /* adc_scale is 2^(number of LSBs). There are 4 extra bits in
1361 the emc6d102 and 2 in the adt7463 and adm1027. In all
1362 other chips ext is always 0 and the value of scale is
1363 irrelevant. So it is left in 4*/
1364 data->adc_scale = (data->type == emc6d102 ) ? 16 : 4;
1366 data->vid = lm85_read_value(client, LM85_REG_VID);
1368 for (i = 0; i <= 3; ++i) {
1370 lm85_read_value(client, LM85_REG_IN(i));
1373 if (!(data->type == adt7463 && (data->vid & 0x80))) {
1374 data->in[4] = lm85_read_value(client,
1378 for (i = 0; i <= 3; ++i) {
1380 lm85_read_value(client, LM85_REG_FAN(i));
1383 for (i = 0; i <= 2; ++i) {
1385 lm85_read_value(client, LM85_REG_TEMP(i));
1388 for (i = 0; i <= 2; ++i) {
1390 lm85_read_value(client, LM85_REG_PWM(i));
1393 data->alarms = lm85_read_value(client, LM85_REG_ALARM1);
1395 if ( data->type == adt7463 ) {
1396 if( data->therm_total < ULONG_MAX - 256 ) {
1397 data->therm_total +=
1398 lm85_read_value(client, ADT7463_REG_THERM );
1400 } else if ( data->type == emc6d100 ) {
1401 /* Three more voltage sensors */
1402 for (i = 5; i <= 7; ++i) {
1404 lm85_read_value(client, EMC6D100_REG_IN(i));
1406 /* More alarm bits */
1408 lm85_read_value(client, EMC6D100_REG_ALARM3) << 16;
1409 } else if (data->type == emc6d102 ) {
1410 /* Have to read LSB bits after the MSB ones because
1411 the reading of the MSB bits has frozen the
1412 LSBs (backward from the ADM1027).
1414 int ext1 = lm85_read_value(client,
1415 EMC6D102_REG_EXTEND_ADC1);
1416 int ext2 = lm85_read_value(client,
1417 EMC6D102_REG_EXTEND_ADC2);
1418 int ext3 = lm85_read_value(client,
1419 EMC6D102_REG_EXTEND_ADC3);
1420 int ext4 = lm85_read_value(client,
1421 EMC6D102_REG_EXTEND_ADC4);
1422 data->in_ext[0] = ext3 & 0x0f;
1423 data->in_ext[1] = ext4 & 0x0f;
1424 data->in_ext[2] = (ext4 >> 4) & 0x0f;
1425 data->in_ext[3] = (ext3 >> 4) & 0x0f;
1426 data->in_ext[4] = (ext2 >> 4) & 0x0f;
1428 data->temp_ext[0] = ext1 & 0x0f;
1429 data->temp_ext[1] = ext2 & 0x0f;
1430 data->temp_ext[2] = (ext1 >> 4) & 0x0f;
1433 data->last_reading = jiffies ;
1434 }; /* last_reading */
1436 if ( !data->valid ||
1437 time_after(jiffies, data->last_config + LM85_CONFIG_INTERVAL) ) {
1438 /* Things that don't change often */
1439 dev_dbg(&client->dev, "Reading config values\n");
1441 for (i = 0; i <= 3; ++i) {
1443 lm85_read_value(client, LM85_REG_IN_MIN(i));
1445 lm85_read_value(client, LM85_REG_IN_MAX(i));
1448 if (!(data->type == adt7463 && (data->vid & 0x80))) {
1449 data->in_min[4] = lm85_read_value(client,
1450 LM85_REG_IN_MIN(4));
1451 data->in_max[4] = lm85_read_value(client,
1452 LM85_REG_IN_MAX(4));
1455 if ( data->type == emc6d100 ) {
1456 for (i = 5; i <= 7; ++i) {
1458 lm85_read_value(client, EMC6D100_REG_IN_MIN(i));
1460 lm85_read_value(client, EMC6D100_REG_IN_MAX(i));
1464 for (i = 0; i <= 3; ++i) {
1466 lm85_read_value(client, LM85_REG_FAN_MIN(i));
1469 for (i = 0; i <= 2; ++i) {
1471 lm85_read_value(client, LM85_REG_TEMP_MIN(i));
1473 lm85_read_value(client, LM85_REG_TEMP_MAX(i));
1476 for (i = 0; i <= 2; ++i) {
1478 data->autofan[i].config =
1479 lm85_read_value(client, LM85_REG_AFAN_CONFIG(i));
1480 val = lm85_read_value(client, LM85_REG_AFAN_RANGE(i));
1481 data->autofan[i].freq = val & 0x07 ;
1482 data->zone[i].range = (val >> 4) & 0x0f ;
1483 data->autofan[i].min_pwm =
1484 lm85_read_value(client, LM85_REG_AFAN_MINPWM(i));
1485 data->zone[i].limit =
1486 lm85_read_value(client, LM85_REG_AFAN_LIMIT(i));
1487 data->zone[i].critical =
1488 lm85_read_value(client, LM85_REG_AFAN_CRITICAL(i));
1491 i = lm85_read_value(client, LM85_REG_AFAN_SPIKE1);
1492 data->smooth[0] = i & 0x0f ;
1493 data->syncpwm3 = i & 0x10 ; /* Save PWM3 config */
1494 data->autofan[0].min_off = (i & 0x20) != 0 ;
1495 data->autofan[1].min_off = (i & 0x40) != 0 ;
1496 data->autofan[2].min_off = (i & 0x80) != 0 ;
1497 i = lm85_read_value(client, LM85_REG_AFAN_SPIKE2);
1498 data->smooth[1] = (i>>4) & 0x0f ;
1499 data->smooth[2] = i & 0x0f ;
1501 i = lm85_read_value(client, LM85_REG_AFAN_HYST1);
1502 data->zone[0].hyst = (i>>4) & 0x0f ;
1503 data->zone[1].hyst = i & 0x0f ;
1505 i = lm85_read_value(client, LM85_REG_AFAN_HYST2);
1506 data->zone[2].hyst = (i>>4) & 0x0f ;
1508 if ( (data->type == lm85b) || (data->type == lm85c) ) {
1509 data->tach_mode = lm85_read_value(client,
1510 LM85_REG_TACH_MODE );
1511 data->spinup_ctl = lm85_read_value(client,
1512 LM85_REG_SPINUP_CTL );
1513 } else if ( (data->type == adt7463) || (data->type == adm1027) ) {
1514 if ( data->type == adt7463 ) {
1515 for (i = 0; i <= 2; ++i) {
1516 data->oppoint[i] = lm85_read_value(client,
1517 ADT7463_REG_OPPOINT(i) );
1519 data->tmin_ctl = lm85_read_value(client,
1520 ADT7463_REG_TMIN_CTL1 );
1521 data->therm_limit = lm85_read_value(client,
1522 ADT7463_REG_THERM_LIMIT );
1524 for (i = 0; i <= 2; ++i) {
1525 data->temp_offset[i] = lm85_read_value(client,
1526 ADM1027_REG_TEMP_OFFSET(i) );
1528 data->tach_mode = lm85_read_value(client,
1529 ADM1027_REG_CONFIG3 );
1530 data->fan_ppr = lm85_read_value(client,
1531 ADM1027_REG_FAN_PPR );
1534 data->last_config = jiffies;
1535 }; /* last_config */
1539 mutex_unlock(&data->update_lock);
1545 static int __init sm_lm85_init(void)
1547 return i2c_add_driver(&lm85_driver);
1550 static void __exit sm_lm85_exit(void)
1552 i2c_del_driver(&lm85_driver);
1555 /* Thanks to Richard Barrington for adding the LM85 to sensors-detect.
1556 * Thanks to Margit Schubert-While <margitsw@t-online.de> for help with
1557 * post 2.7.0 CVS changes.
1559 MODULE_LICENSE("GPL");
1560 MODULE_AUTHOR("Philip Pokorny <ppokorny@penguincomputing.com>, Margit Schubert-While <margitsw@t-online.de>, Justin Thiessen <jthiessen@penguincomputing.com");
1561 MODULE_DESCRIPTION("LM85-B, LM85-C driver");
1563 module_init(sm_lm85_init);
1564 module_exit(sm_lm85_exit);