x86: fix bootup crash in native_read_tsc()
[wrt350n-kernel.git] / drivers / hwmon / lm85.c
bloba02480be65f24db5e2c4fdca873d3b5682f92b10
1 /*
2 lm85.c - Part of lm_sensors, Linux kernel modules for hardware
3 monitoring
4 Copyright (c) 1998, 1999 Frodo Looijaard <frodol@dds.nl>
5 Copyright (c) 2002, 2003 Philip Pokorny <ppokorny@penguincomputing.com>
6 Copyright (c) 2003 Margit Schubert-While <margitsw@t-online.de>
7 Copyright (c) 2004 Justin Thiessen <jthiessen@penguincomputing.com>
9 Chip details at <http://www.national.com/ds/LM/LM85.pdf>
11 This program is free software; you can redistribute it and/or modify
12 it under the terms of the GNU General Public License as published by
13 the Free Software Foundation; either version 2 of the License, or
14 (at your option) any later version.
16 This program is distributed in the hope that it will be useful,
17 but WITHOUT ANY WARRANTY; without even the implied warranty of
18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 GNU General Public License for more details.
21 You should have received a copy of the GNU General Public License
22 along with this program; if not, write to the Free Software
23 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
26 #include <linux/module.h>
27 #include <linux/init.h>
28 #include <linux/slab.h>
29 #include <linux/jiffies.h>
30 #include <linux/i2c.h>
31 #include <linux/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
127 /* Conversions. Rounding and limit checking is only done on the TO_REG
128 variants. Note that you should be a bit careful with which arguments
129 these macros are called: arguments may be evaluated more than once.
132 /* IN are scaled acording to built-in resistors */
133 static int lm85_scaling[] = { /* .001 Volts */
134 2500, 2250, 3300, 5000, 12000,
135 3300, 1500, 1800 /*EMC6D100*/
137 #define SCALE(val,from,to) (((val)*(to) + ((from)/2))/(from))
139 #define INS_TO_REG(n,val) \
140 SENSORS_LIMIT(SCALE(val,lm85_scaling[n],192),0,255)
142 #define INSEXT_FROM_REG(n,val,ext) \
143 SCALE(((val) << 4) + (ext), 192 << 4, lm85_scaling[n])
145 #define INS_FROM_REG(n,val) SCALE((val), 192, lm85_scaling[n])
147 /* FAN speed is measured using 90kHz clock */
148 static inline u16 FAN_TO_REG(unsigned long val)
150 if (!val)
151 return 0xffff;
152 return SENSORS_LIMIT(5400000 / val, 1, 0xfffe);
154 #define FAN_FROM_REG(val) ((val)==0?-1:(val)==0xffff?0:5400000/(val))
156 /* Temperature is reported in .001 degC increments */
157 #define TEMP_TO_REG(val) \
158 SENSORS_LIMIT(SCALE(val,1000,1),-127,127)
159 #define TEMPEXT_FROM_REG(val,ext) \
160 SCALE(((val) << 4) + (ext), 16, 1000)
161 #define TEMP_FROM_REG(val) ((val) * 1000)
163 #define PWM_TO_REG(val) (SENSORS_LIMIT(val,0,255))
164 #define PWM_FROM_REG(val) (val)
167 /* ZONEs have the following parameters:
168 * Limit (low) temp, 1. degC
169 * Hysteresis (below limit), 1. degC (0-15)
170 * Range of speed control, .1 degC (2-80)
171 * Critical (high) temp, 1. degC
173 * FAN PWMs have the following parameters:
174 * Reference Zone, 1, 2, 3, etc.
175 * Spinup time, .05 sec
176 * PWM value at limit/low temp, 1 count
177 * PWM Frequency, 1. Hz
178 * PWM is Min or OFF below limit, flag
179 * Invert PWM output, flag
181 * Some chips filter the temp, others the fan.
182 * Filter constant (or disabled) .1 seconds
185 /* These are the zone temperature range encodings in .001 degree C */
186 static int lm85_range_map[] = {
187 2000, 2500, 3300, 4000, 5000, 6600,
188 8000, 10000, 13300, 16000, 20000, 26600,
189 32000, 40000, 53300, 80000
191 static int RANGE_TO_REG( int range )
193 int i;
195 if ( range < lm85_range_map[0] ) {
196 return 0 ;
197 } else if ( range > lm85_range_map[15] ) {
198 return 15 ;
199 } else { /* find closest match */
200 for ( i = 14 ; i >= 0 ; --i ) {
201 if ( range > lm85_range_map[i] ) { /* range bracketed */
202 if ((lm85_range_map[i+1] - range) <
203 (range - lm85_range_map[i])) {
204 i++;
205 break;
207 break;
211 return( i & 0x0f );
213 #define RANGE_FROM_REG(val) (lm85_range_map[(val)&0x0f])
215 /* These are the Acoustic Enhancement, or Temperature smoothing encodings
216 * NOTE: The enable/disable bit is INCLUDED in these encodings as the
217 * MSB (bit 3, value 8). If the enable bit is 0, the encoded value
218 * is ignored, or set to 0.
220 /* These are the PWM frequency encodings */
221 static int lm85_freq_map[] = { /* .1 Hz */
222 100, 150, 230, 300, 380, 470, 620, 940
224 static int FREQ_TO_REG( int freq )
226 int i;
228 if( freq >= lm85_freq_map[7] ) { return 7 ; }
229 for( i = 0 ; i < 7 ; ++i )
230 if( freq <= lm85_freq_map[i] )
231 break ;
232 return( i & 0x07 );
234 #define FREQ_FROM_REG(val) (lm85_freq_map[(val)&0x07])
236 /* Since we can't use strings, I'm abusing these numbers
237 * to stand in for the following meanings:
238 * 1 -- PWM responds to Zone 1
239 * 2 -- PWM responds to Zone 2
240 * 3 -- PWM responds to Zone 3
241 * 23 -- PWM responds to the higher temp of Zone 2 or 3
242 * 123 -- PWM responds to highest of Zone 1, 2, or 3
243 * 0 -- PWM is always at 0% (ie, off)
244 * -1 -- PWM is always at 100%
245 * -2 -- PWM responds to manual control
248 static int lm85_zone_map[] = { 1, 2, 3, -1, 0, 23, 123, -2 };
249 #define ZONE_FROM_REG(val) (lm85_zone_map[((val)>>5)&0x07])
251 static int ZONE_TO_REG( int zone )
253 int i;
255 for( i = 0 ; i <= 7 ; ++i )
256 if( zone == lm85_zone_map[i] )
257 break ;
258 if( i > 7 ) /* Not found. */
259 i = 3; /* Always 100% */
260 return( (i & 0x07)<<5 );
263 #define HYST_TO_REG(val) (SENSORS_LIMIT(((val)+500)/1000,0,15))
264 #define HYST_FROM_REG(val) ((val)*1000)
266 #define OFFSET_TO_REG(val) (SENSORS_LIMIT((val)/25,-127,127))
267 #define OFFSET_FROM_REG(val) ((val)*25)
269 #define PPR_MASK(fan) (0x03<<(fan *2))
270 #define PPR_TO_REG(val,fan) (SENSORS_LIMIT((val)-1,0,3)<<(fan *2))
271 #define PPR_FROM_REG(val,fan) ((((val)>>(fan * 2))&0x03)+1)
273 /* Chip sampling rates
275 * Some sensors are not updated more frequently than once per second
276 * so it doesn't make sense to read them more often than that.
277 * We cache the results and return the saved data if the driver
278 * is called again before a second has elapsed.
280 * Also, there is significant configuration data for this chip
281 * given the automatic PWM fan control that is possible. There
282 * are about 47 bytes of config data to only 22 bytes of actual
283 * readings. So, we keep the config data up to date in the cache
284 * when it is written and only sample it once every 1 *minute*
286 #define LM85_DATA_INTERVAL (HZ + HZ / 2)
287 #define LM85_CONFIG_INTERVAL (1 * 60 * HZ)
289 /* LM85 can automatically adjust fan speeds based on temperature
290 * This structure encapsulates an entire Zone config. There are
291 * three zones (one for each temperature input) on the lm85
293 struct lm85_zone {
294 s8 limit; /* Low temp limit */
295 u8 hyst; /* Low limit hysteresis. (0-15) */
296 u8 range; /* Temp range, encoded */
297 s8 critical; /* "All fans ON" temp limit */
298 u8 off_desired; /* Actual "off" temperature specified. Preserved
299 * to prevent "drift" as other autofan control
300 * values change.
302 u8 max_desired; /* Actual "max" temperature specified. Preserved
303 * to prevent "drift" as other autofan control
304 * values change.
308 struct lm85_autofan {
309 u8 config; /* Register value */
310 u8 freq; /* PWM frequency, encoded */
311 u8 min_pwm; /* Minimum PWM value, encoded */
312 u8 min_off; /* Min PWM or OFF below "limit", flag */
315 /* For each registered chip, we need to keep some data in memory.
316 The structure is dynamically allocated. */
317 struct lm85_data {
318 struct i2c_client client;
319 struct device *hwmon_dev;
320 enum chips type;
322 struct mutex update_lock;
323 int valid; /* !=0 if following fields are valid */
324 unsigned long last_reading; /* In jiffies */
325 unsigned long last_config; /* In jiffies */
327 u8 in[8]; /* Register value */
328 u8 in_max[8]; /* Register value */
329 u8 in_min[8]; /* Register value */
330 s8 temp[3]; /* Register value */
331 s8 temp_min[3]; /* Register value */
332 s8 temp_max[3]; /* Register value */
333 s8 temp_offset[3]; /* Register value */
334 u16 fan[4]; /* Register value */
335 u16 fan_min[4]; /* Register value */
336 u8 pwm[3]; /* Register value */
337 u8 spinup_ctl; /* Register encoding, combined */
338 u8 tach_mode; /* Register encoding, combined */
339 u8 temp_ext[3]; /* Decoded values */
340 u8 in_ext[8]; /* Decoded values */
341 u8 fan_ppr; /* Register value */
342 u8 smooth[3]; /* Register encoding */
343 u8 vid; /* Register value */
344 u8 vrm; /* VRM version */
345 u8 syncpwm3; /* Saved PWM3 for TACH 2,3,4 config */
346 u8 oppoint[3]; /* Register value */
347 u16 tmin_ctl; /* Register value */
348 unsigned long therm_total; /* Cummulative therm count */
349 u8 therm_limit; /* Register value */
350 u32 alarms; /* Register encoding, combined */
351 struct lm85_autofan autofan[3];
352 struct lm85_zone zone[3];
355 static int lm85_attach_adapter(struct i2c_adapter *adapter);
356 static int lm85_detect(struct i2c_adapter *adapter, int address,
357 int kind);
358 static int lm85_detach_client(struct i2c_client *client);
360 static int lm85_read_value(struct i2c_client *client, u8 reg);
361 static int lm85_write_value(struct i2c_client *client, u8 reg, int value);
362 static struct lm85_data *lm85_update_device(struct device *dev);
363 static void lm85_init_client(struct i2c_client *client);
366 static struct i2c_driver lm85_driver = {
367 .driver = {
368 .name = "lm85",
370 .id = I2C_DRIVERID_LM85,
371 .attach_adapter = lm85_attach_adapter,
372 .detach_client = lm85_detach_client,
376 /* 4 Fans */
377 static ssize_t show_fan(struct device *dev, struct device_attribute *attr,
378 char *buf)
380 int nr = to_sensor_dev_attr(attr)->index;
381 struct lm85_data *data = lm85_update_device(dev);
382 return sprintf(buf,"%d\n", FAN_FROM_REG(data->fan[nr]) );
385 static ssize_t show_fan_min(struct device *dev, struct device_attribute *attr,
386 char *buf)
388 int nr = to_sensor_dev_attr(attr)->index;
389 struct lm85_data *data = lm85_update_device(dev);
390 return sprintf(buf,"%d\n", FAN_FROM_REG(data->fan_min[nr]) );
393 static ssize_t set_fan_min(struct device *dev, struct device_attribute *attr,
394 const char *buf, size_t count)
396 int nr = to_sensor_dev_attr(attr)->index;
397 struct i2c_client *client = to_i2c_client(dev);
398 struct lm85_data *data = i2c_get_clientdata(client);
399 unsigned long val = simple_strtoul(buf, NULL, 10);
401 mutex_lock(&data->update_lock);
402 data->fan_min[nr] = FAN_TO_REG(val);
403 lm85_write_value(client, LM85_REG_FAN_MIN(nr), data->fan_min[nr]);
404 mutex_unlock(&data->update_lock);
405 return count;
408 #define show_fan_offset(offset) \
409 static SENSOR_DEVICE_ATTR(fan##offset##_input, S_IRUGO, \
410 show_fan, NULL, offset - 1); \
411 static SENSOR_DEVICE_ATTR(fan##offset##_min, S_IRUGO | S_IWUSR, \
412 show_fan_min, set_fan_min, offset - 1)
414 show_fan_offset(1);
415 show_fan_offset(2);
416 show_fan_offset(3);
417 show_fan_offset(4);
419 /* vid, vrm, alarms */
421 static ssize_t show_vid_reg(struct device *dev, struct device_attribute *attr, char *buf)
423 struct lm85_data *data = lm85_update_device(dev);
424 int vid;
426 if (data->type == adt7463 && (data->vid & 0x80)) {
427 /* 6-pin VID (VRM 10) */
428 vid = vid_from_reg(data->vid & 0x3f, data->vrm);
429 } else {
430 /* 5-pin VID (VRM 9) */
431 vid = vid_from_reg(data->vid & 0x1f, data->vrm);
434 return sprintf(buf, "%d\n", vid);
437 static DEVICE_ATTR(cpu0_vid, S_IRUGO, show_vid_reg, NULL);
439 static ssize_t show_vrm_reg(struct device *dev, struct device_attribute *attr, char *buf)
441 struct lm85_data *data = dev_get_drvdata(dev);
442 return sprintf(buf, "%ld\n", (long) data->vrm);
445 static ssize_t store_vrm_reg(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
447 struct i2c_client *client = to_i2c_client(dev);
448 struct lm85_data *data = i2c_get_clientdata(client);
449 u32 val;
451 val = simple_strtoul(buf, NULL, 10);
452 data->vrm = val;
453 return count;
456 static DEVICE_ATTR(vrm, S_IRUGO | S_IWUSR, show_vrm_reg, store_vrm_reg);
458 static ssize_t show_alarms_reg(struct device *dev, struct device_attribute *attr, char *buf)
460 struct lm85_data *data = lm85_update_device(dev);
461 return sprintf(buf, "%u\n", data->alarms);
464 static DEVICE_ATTR(alarms, S_IRUGO, show_alarms_reg, NULL);
466 static ssize_t show_alarm(struct device *dev, struct device_attribute *attr,
467 char *buf)
469 int nr = to_sensor_dev_attr(attr)->index;
470 struct lm85_data *data = lm85_update_device(dev);
471 return sprintf(buf, "%u\n", (data->alarms >> nr) & 1);
474 static SENSOR_DEVICE_ATTR(in0_alarm, S_IRUGO, show_alarm, NULL, 0);
475 static SENSOR_DEVICE_ATTR(in1_alarm, S_IRUGO, show_alarm, NULL, 1);
476 static SENSOR_DEVICE_ATTR(in2_alarm, S_IRUGO, show_alarm, NULL, 2);
477 static SENSOR_DEVICE_ATTR(in3_alarm, S_IRUGO, show_alarm, NULL, 3);
478 static SENSOR_DEVICE_ATTR(in4_alarm, S_IRUGO, show_alarm, NULL, 8);
479 static SENSOR_DEVICE_ATTR(in5_alarm, S_IRUGO, show_alarm, NULL, 18);
480 static SENSOR_DEVICE_ATTR(in6_alarm, S_IRUGO, show_alarm, NULL, 16);
481 static SENSOR_DEVICE_ATTR(in7_alarm, S_IRUGO, show_alarm, NULL, 17);
482 static SENSOR_DEVICE_ATTR(temp1_alarm, S_IRUGO, show_alarm, NULL, 4);
483 static SENSOR_DEVICE_ATTR(temp1_fault, S_IRUGO, show_alarm, NULL, 14);
484 static SENSOR_DEVICE_ATTR(temp2_alarm, S_IRUGO, show_alarm, NULL, 5);
485 static SENSOR_DEVICE_ATTR(temp3_alarm, S_IRUGO, show_alarm, NULL, 6);
486 static SENSOR_DEVICE_ATTR(temp3_fault, S_IRUGO, show_alarm, NULL, 15);
487 static SENSOR_DEVICE_ATTR(fan1_alarm, S_IRUGO, show_alarm, NULL, 10);
488 static SENSOR_DEVICE_ATTR(fan2_alarm, S_IRUGO, show_alarm, NULL, 11);
489 static SENSOR_DEVICE_ATTR(fan3_alarm, S_IRUGO, show_alarm, NULL, 12);
490 static SENSOR_DEVICE_ATTR(fan4_alarm, S_IRUGO, show_alarm, NULL, 13);
492 /* pwm */
494 static ssize_t show_pwm(struct device *dev, struct device_attribute *attr,
495 char *buf)
497 int nr = to_sensor_dev_attr(attr)->index;
498 struct lm85_data *data = lm85_update_device(dev);
499 return sprintf(buf,"%d\n", PWM_FROM_REG(data->pwm[nr]) );
502 static ssize_t set_pwm(struct device *dev, struct device_attribute *attr,
503 const char *buf, size_t count)
505 int nr = to_sensor_dev_attr(attr)->index;
506 struct i2c_client *client = to_i2c_client(dev);
507 struct lm85_data *data = i2c_get_clientdata(client);
508 long val = simple_strtol(buf, NULL, 10);
510 mutex_lock(&data->update_lock);
511 data->pwm[nr] = PWM_TO_REG(val);
512 lm85_write_value(client, LM85_REG_PWM(nr), data->pwm[nr]);
513 mutex_unlock(&data->update_lock);
514 return count;
517 static ssize_t show_pwm_enable(struct device *dev, struct device_attribute
518 *attr, char *buf)
520 int nr = to_sensor_dev_attr(attr)->index;
521 struct lm85_data *data = lm85_update_device(dev);
522 int pwm_zone;
524 pwm_zone = ZONE_FROM_REG(data->autofan[nr].config);
525 return sprintf(buf,"%d\n", (pwm_zone != 0 && pwm_zone != -1) );
528 #define show_pwm_reg(offset) \
529 static SENSOR_DEVICE_ATTR(pwm##offset, S_IRUGO | S_IWUSR, \
530 show_pwm, set_pwm, offset - 1); \
531 static SENSOR_DEVICE_ATTR(pwm##offset##_enable, S_IRUGO, \
532 show_pwm_enable, NULL, offset - 1)
534 show_pwm_reg(1);
535 show_pwm_reg(2);
536 show_pwm_reg(3);
538 /* Voltages */
540 static ssize_t show_in(struct device *dev, struct device_attribute *attr,
541 char *buf)
543 int nr = to_sensor_dev_attr(attr)->index;
544 struct lm85_data *data = lm85_update_device(dev);
545 return sprintf( buf, "%d\n", INSEXT_FROM_REG(nr,
546 data->in[nr],
547 data->in_ext[nr]));
550 static ssize_t show_in_min(struct device *dev, struct device_attribute *attr,
551 char *buf)
553 int nr = to_sensor_dev_attr(attr)->index;
554 struct lm85_data *data = lm85_update_device(dev);
555 return sprintf(buf,"%d\n", INS_FROM_REG(nr, data->in_min[nr]) );
558 static ssize_t set_in_min(struct device *dev, struct device_attribute *attr,
559 const char *buf, size_t count)
561 int nr = to_sensor_dev_attr(attr)->index;
562 struct i2c_client *client = to_i2c_client(dev);
563 struct lm85_data *data = i2c_get_clientdata(client);
564 long val = simple_strtol(buf, NULL, 10);
566 mutex_lock(&data->update_lock);
567 data->in_min[nr] = INS_TO_REG(nr, val);
568 lm85_write_value(client, LM85_REG_IN_MIN(nr), data->in_min[nr]);
569 mutex_unlock(&data->update_lock);
570 return count;
573 static ssize_t show_in_max(struct device *dev, struct device_attribute *attr,
574 char *buf)
576 int nr = to_sensor_dev_attr(attr)->index;
577 struct lm85_data *data = lm85_update_device(dev);
578 return sprintf(buf,"%d\n", INS_FROM_REG(nr, data->in_max[nr]) );
581 static ssize_t set_in_max(struct device *dev, struct device_attribute *attr,
582 const char *buf, size_t count)
584 int nr = to_sensor_dev_attr(attr)->index;
585 struct i2c_client *client = to_i2c_client(dev);
586 struct lm85_data *data = i2c_get_clientdata(client);
587 long val = simple_strtol(buf, NULL, 10);
589 mutex_lock(&data->update_lock);
590 data->in_max[nr] = INS_TO_REG(nr, val);
591 lm85_write_value(client, LM85_REG_IN_MAX(nr), data->in_max[nr]);
592 mutex_unlock(&data->update_lock);
593 return count;
596 #define show_in_reg(offset) \
597 static SENSOR_DEVICE_ATTR(in##offset##_input, S_IRUGO, \
598 show_in, NULL, offset); \
599 static SENSOR_DEVICE_ATTR(in##offset##_min, S_IRUGO | S_IWUSR, \
600 show_in_min, set_in_min, offset); \
601 static SENSOR_DEVICE_ATTR(in##offset##_max, S_IRUGO | S_IWUSR, \
602 show_in_max, set_in_max, offset)
604 show_in_reg(0);
605 show_in_reg(1);
606 show_in_reg(2);
607 show_in_reg(3);
608 show_in_reg(4);
609 show_in_reg(5);
610 show_in_reg(6);
611 show_in_reg(7);
613 /* Temps */
615 static ssize_t show_temp(struct device *dev, struct device_attribute *attr,
616 char *buf)
618 int nr = to_sensor_dev_attr(attr)->index;
619 struct lm85_data *data = lm85_update_device(dev);
620 return sprintf(buf,"%d\n", TEMPEXT_FROM_REG(data->temp[nr],
621 data->temp_ext[nr]));
624 static ssize_t show_temp_min(struct device *dev, struct device_attribute *attr,
625 char *buf)
627 int nr = to_sensor_dev_attr(attr)->index;
628 struct lm85_data *data = lm85_update_device(dev);
629 return sprintf(buf,"%d\n", TEMP_FROM_REG(data->temp_min[nr]) );
632 static ssize_t set_temp_min(struct device *dev, struct device_attribute *attr,
633 const char *buf, size_t count)
635 int nr = to_sensor_dev_attr(attr)->index;
636 struct i2c_client *client = to_i2c_client(dev);
637 struct lm85_data *data = i2c_get_clientdata(client);
638 long val = simple_strtol(buf, NULL, 10);
640 mutex_lock(&data->update_lock);
641 data->temp_min[nr] = TEMP_TO_REG(val);
642 lm85_write_value(client, LM85_REG_TEMP_MIN(nr), data->temp_min[nr]);
643 mutex_unlock(&data->update_lock);
644 return count;
647 static ssize_t show_temp_max(struct device *dev, struct device_attribute *attr,
648 char *buf)
650 int nr = to_sensor_dev_attr(attr)->index;
651 struct lm85_data *data = lm85_update_device(dev);
652 return sprintf(buf,"%d\n", TEMP_FROM_REG(data->temp_max[nr]) );
655 static ssize_t set_temp_max(struct device *dev, struct device_attribute *attr,
656 const char *buf, size_t count)
658 int nr = to_sensor_dev_attr(attr)->index;
659 struct i2c_client *client = to_i2c_client(dev);
660 struct lm85_data *data = i2c_get_clientdata(client);
661 long val = simple_strtol(buf, NULL, 10);
663 mutex_lock(&data->update_lock);
664 data->temp_max[nr] = TEMP_TO_REG(val);
665 lm85_write_value(client, LM85_REG_TEMP_MAX(nr), data->temp_max[nr]);
666 mutex_unlock(&data->update_lock);
667 return count;
670 #define show_temp_reg(offset) \
671 static SENSOR_DEVICE_ATTR(temp##offset##_input, S_IRUGO, \
672 show_temp, NULL, offset - 1); \
673 static SENSOR_DEVICE_ATTR(temp##offset##_min, S_IRUGO | S_IWUSR, \
674 show_temp_min, set_temp_min, offset - 1); \
675 static SENSOR_DEVICE_ATTR(temp##offset##_max, S_IRUGO | S_IWUSR, \
676 show_temp_max, set_temp_max, offset - 1);
678 show_temp_reg(1);
679 show_temp_reg(2);
680 show_temp_reg(3);
683 /* Automatic PWM control */
685 static ssize_t show_pwm_auto_channels(struct device *dev,
686 struct device_attribute *attr, char *buf)
688 int nr = to_sensor_dev_attr(attr)->index;
689 struct lm85_data *data = lm85_update_device(dev);
690 return sprintf(buf,"%d\n", ZONE_FROM_REG(data->autofan[nr].config));
693 static ssize_t set_pwm_auto_channels(struct device *dev,
694 struct device_attribute *attr, const char *buf, size_t count)
696 int nr = to_sensor_dev_attr(attr)->index;
697 struct i2c_client *client = to_i2c_client(dev);
698 struct lm85_data *data = i2c_get_clientdata(client);
699 long val = simple_strtol(buf, NULL, 10);
701 mutex_lock(&data->update_lock);
702 data->autofan[nr].config = (data->autofan[nr].config & (~0xe0))
703 | ZONE_TO_REG(val) ;
704 lm85_write_value(client, LM85_REG_AFAN_CONFIG(nr),
705 data->autofan[nr].config);
706 mutex_unlock(&data->update_lock);
707 return count;
710 static ssize_t show_pwm_auto_pwm_min(struct device *dev,
711 struct device_attribute *attr, char *buf)
713 int nr = to_sensor_dev_attr(attr)->index;
714 struct lm85_data *data = lm85_update_device(dev);
715 return sprintf(buf,"%d\n", PWM_FROM_REG(data->autofan[nr].min_pwm));
718 static ssize_t set_pwm_auto_pwm_min(struct device *dev,
719 struct device_attribute *attr, const char *buf, size_t count)
721 int nr = to_sensor_dev_attr(attr)->index;
722 struct i2c_client *client = to_i2c_client(dev);
723 struct lm85_data *data = i2c_get_clientdata(client);
724 long val = simple_strtol(buf, NULL, 10);
726 mutex_lock(&data->update_lock);
727 data->autofan[nr].min_pwm = PWM_TO_REG(val);
728 lm85_write_value(client, LM85_REG_AFAN_MINPWM(nr),
729 data->autofan[nr].min_pwm);
730 mutex_unlock(&data->update_lock);
731 return count;
734 static ssize_t show_pwm_auto_pwm_minctl(struct device *dev,
735 struct device_attribute *attr, char *buf)
737 int nr = to_sensor_dev_attr(attr)->index;
738 struct lm85_data *data = lm85_update_device(dev);
739 return sprintf(buf,"%d\n", data->autofan[nr].min_off);
742 static ssize_t set_pwm_auto_pwm_minctl(struct device *dev,
743 struct device_attribute *attr, const char *buf, size_t count)
745 int nr = to_sensor_dev_attr(attr)->index;
746 struct i2c_client *client = to_i2c_client(dev);
747 struct lm85_data *data = i2c_get_clientdata(client);
748 long val = simple_strtol(buf, NULL, 10);
750 mutex_lock(&data->update_lock);
751 data->autofan[nr].min_off = val;
752 lm85_write_value(client, LM85_REG_AFAN_SPIKE1, data->smooth[0]
753 | data->syncpwm3
754 | (data->autofan[0].min_off ? 0x20 : 0)
755 | (data->autofan[1].min_off ? 0x40 : 0)
756 | (data->autofan[2].min_off ? 0x80 : 0)
758 mutex_unlock(&data->update_lock);
759 return count;
762 static ssize_t show_pwm_auto_pwm_freq(struct device *dev,
763 struct device_attribute *attr, char *buf)
765 int nr = to_sensor_dev_attr(attr)->index;
766 struct lm85_data *data = lm85_update_device(dev);
767 return sprintf(buf,"%d\n", FREQ_FROM_REG(data->autofan[nr].freq));
770 static ssize_t set_pwm_auto_pwm_freq(struct device *dev,
771 struct device_attribute *attr, const char *buf, size_t count)
773 int nr = to_sensor_dev_attr(attr)->index;
774 struct i2c_client *client = to_i2c_client(dev);
775 struct lm85_data *data = i2c_get_clientdata(client);
776 long val = simple_strtol(buf, NULL, 10);
778 mutex_lock(&data->update_lock);
779 data->autofan[nr].freq = FREQ_TO_REG(val);
780 lm85_write_value(client, LM85_REG_AFAN_RANGE(nr),
781 (data->zone[nr].range << 4)
782 | data->autofan[nr].freq
784 mutex_unlock(&data->update_lock);
785 return count;
788 #define pwm_auto(offset) \
789 static SENSOR_DEVICE_ATTR(pwm##offset##_auto_channels, \
790 S_IRUGO | S_IWUSR, show_pwm_auto_channels, \
791 set_pwm_auto_channels, offset - 1); \
792 static SENSOR_DEVICE_ATTR(pwm##offset##_auto_pwm_min, \
793 S_IRUGO | S_IWUSR, show_pwm_auto_pwm_min, \
794 set_pwm_auto_pwm_min, offset - 1); \
795 static SENSOR_DEVICE_ATTR(pwm##offset##_auto_pwm_minctl, \
796 S_IRUGO | S_IWUSR, show_pwm_auto_pwm_minctl, \
797 set_pwm_auto_pwm_minctl, offset - 1); \
798 static SENSOR_DEVICE_ATTR(pwm##offset##_auto_pwm_freq, \
799 S_IRUGO | S_IWUSR, show_pwm_auto_pwm_freq, \
800 set_pwm_auto_pwm_freq, offset - 1);
802 pwm_auto(1);
803 pwm_auto(2);
804 pwm_auto(3);
806 /* Temperature settings for automatic PWM control */
808 static ssize_t show_temp_auto_temp_off(struct device *dev,
809 struct device_attribute *attr, char *buf)
811 int nr = to_sensor_dev_attr(attr)->index;
812 struct lm85_data *data = lm85_update_device(dev);
813 return sprintf(buf,"%d\n", TEMP_FROM_REG(data->zone[nr].limit) -
814 HYST_FROM_REG(data->zone[nr].hyst));
817 static ssize_t set_temp_auto_temp_off(struct device *dev,
818 struct device_attribute *attr, const char *buf, size_t count)
820 int nr = to_sensor_dev_attr(attr)->index;
821 struct i2c_client *client = to_i2c_client(dev);
822 struct lm85_data *data = i2c_get_clientdata(client);
823 int min;
824 long val = simple_strtol(buf, NULL, 10);
826 mutex_lock(&data->update_lock);
827 min = TEMP_FROM_REG(data->zone[nr].limit);
828 data->zone[nr].off_desired = TEMP_TO_REG(val);
829 data->zone[nr].hyst = HYST_TO_REG(min - val);
830 if ( nr == 0 || nr == 1 ) {
831 lm85_write_value(client, LM85_REG_AFAN_HYST1,
832 (data->zone[0].hyst << 4)
833 | data->zone[1].hyst
835 } else {
836 lm85_write_value(client, LM85_REG_AFAN_HYST2,
837 (data->zone[2].hyst << 4)
840 mutex_unlock(&data->update_lock);
841 return count;
844 static ssize_t show_temp_auto_temp_min(struct device *dev,
845 struct device_attribute *attr, char *buf)
847 int nr = to_sensor_dev_attr(attr)->index;
848 struct lm85_data *data = lm85_update_device(dev);
849 return sprintf(buf,"%d\n", TEMP_FROM_REG(data->zone[nr].limit) );
852 static ssize_t set_temp_auto_temp_min(struct device *dev,
853 struct device_attribute *attr, const char *buf, size_t count)
855 int nr = to_sensor_dev_attr(attr)->index;
856 struct i2c_client *client = to_i2c_client(dev);
857 struct lm85_data *data = i2c_get_clientdata(client);
858 long val = simple_strtol(buf, NULL, 10);
860 mutex_lock(&data->update_lock);
861 data->zone[nr].limit = TEMP_TO_REG(val);
862 lm85_write_value(client, LM85_REG_AFAN_LIMIT(nr),
863 data->zone[nr].limit);
865 /* Update temp_auto_max and temp_auto_range */
866 data->zone[nr].range = RANGE_TO_REG(
867 TEMP_FROM_REG(data->zone[nr].max_desired) -
868 TEMP_FROM_REG(data->zone[nr].limit));
869 lm85_write_value(client, LM85_REG_AFAN_RANGE(nr),
870 ((data->zone[nr].range & 0x0f) << 4)
871 | (data->autofan[nr].freq & 0x07));
873 /* Update temp_auto_hyst and temp_auto_off */
874 data->zone[nr].hyst = HYST_TO_REG(TEMP_FROM_REG(
875 data->zone[nr].limit) - TEMP_FROM_REG(
876 data->zone[nr].off_desired));
877 if ( nr == 0 || nr == 1 ) {
878 lm85_write_value(client, LM85_REG_AFAN_HYST1,
879 (data->zone[0].hyst << 4)
880 | data->zone[1].hyst
882 } else {
883 lm85_write_value(client, LM85_REG_AFAN_HYST2,
884 (data->zone[2].hyst << 4)
887 mutex_unlock(&data->update_lock);
888 return count;
891 static ssize_t show_temp_auto_temp_max(struct device *dev,
892 struct device_attribute *attr, char *buf)
894 int nr = to_sensor_dev_attr(attr)->index;
895 struct lm85_data *data = lm85_update_device(dev);
896 return sprintf(buf,"%d\n", TEMP_FROM_REG(data->zone[nr].limit) +
897 RANGE_FROM_REG(data->zone[nr].range));
900 static ssize_t set_temp_auto_temp_max(struct device *dev,
901 struct device_attribute *attr, const char *buf, size_t count)
903 int nr = to_sensor_dev_attr(attr)->index;
904 struct i2c_client *client = to_i2c_client(dev);
905 struct lm85_data *data = i2c_get_clientdata(client);
906 int min;
907 long val = simple_strtol(buf, NULL, 10);
909 mutex_lock(&data->update_lock);
910 min = TEMP_FROM_REG(data->zone[nr].limit);
911 data->zone[nr].max_desired = TEMP_TO_REG(val);
912 data->zone[nr].range = RANGE_TO_REG(
913 val - min);
914 lm85_write_value(client, LM85_REG_AFAN_RANGE(nr),
915 ((data->zone[nr].range & 0x0f) << 4)
916 | (data->autofan[nr].freq & 0x07));
917 mutex_unlock(&data->update_lock);
918 return count;
921 static ssize_t show_temp_auto_temp_crit(struct device *dev,
922 struct device_attribute *attr, char *buf)
924 int nr = to_sensor_dev_attr(attr)->index;
925 struct lm85_data *data = lm85_update_device(dev);
926 return sprintf(buf,"%d\n", TEMP_FROM_REG(data->zone[nr].critical));
929 static ssize_t set_temp_auto_temp_crit(struct device *dev,
930 struct device_attribute *attr,const char *buf, size_t count)
932 int nr = to_sensor_dev_attr(attr)->index;
933 struct i2c_client *client = to_i2c_client(dev);
934 struct lm85_data *data = i2c_get_clientdata(client);
935 long val = simple_strtol(buf, NULL, 10);
937 mutex_lock(&data->update_lock);
938 data->zone[nr].critical = TEMP_TO_REG(val);
939 lm85_write_value(client, LM85_REG_AFAN_CRITICAL(nr),
940 data->zone[nr].critical);
941 mutex_unlock(&data->update_lock);
942 return count;
945 #define temp_auto(offset) \
946 static SENSOR_DEVICE_ATTR(temp##offset##_auto_temp_off, \
947 S_IRUGO | S_IWUSR, show_temp_auto_temp_off, \
948 set_temp_auto_temp_off, offset - 1); \
949 static SENSOR_DEVICE_ATTR(temp##offset##_auto_temp_min, \
950 S_IRUGO | S_IWUSR, show_temp_auto_temp_min, \
951 set_temp_auto_temp_min, offset - 1); \
952 static SENSOR_DEVICE_ATTR(temp##offset##_auto_temp_max, \
953 S_IRUGO | S_IWUSR, show_temp_auto_temp_max, \
954 set_temp_auto_temp_max, offset - 1); \
955 static SENSOR_DEVICE_ATTR(temp##offset##_auto_temp_crit, \
956 S_IRUGO | S_IWUSR, show_temp_auto_temp_crit, \
957 set_temp_auto_temp_crit, offset - 1);
959 temp_auto(1);
960 temp_auto(2);
961 temp_auto(3);
963 static int lm85_attach_adapter(struct i2c_adapter *adapter)
965 if (!(adapter->class & I2C_CLASS_HWMON))
966 return 0;
967 return i2c_probe(adapter, &addr_data, lm85_detect);
970 static struct attribute *lm85_attributes[] = {
971 &sensor_dev_attr_fan1_input.dev_attr.attr,
972 &sensor_dev_attr_fan2_input.dev_attr.attr,
973 &sensor_dev_attr_fan3_input.dev_attr.attr,
974 &sensor_dev_attr_fan4_input.dev_attr.attr,
975 &sensor_dev_attr_fan1_min.dev_attr.attr,
976 &sensor_dev_attr_fan2_min.dev_attr.attr,
977 &sensor_dev_attr_fan3_min.dev_attr.attr,
978 &sensor_dev_attr_fan4_min.dev_attr.attr,
979 &sensor_dev_attr_fan1_alarm.dev_attr.attr,
980 &sensor_dev_attr_fan2_alarm.dev_attr.attr,
981 &sensor_dev_attr_fan3_alarm.dev_attr.attr,
982 &sensor_dev_attr_fan4_alarm.dev_attr.attr,
984 &sensor_dev_attr_pwm1.dev_attr.attr,
985 &sensor_dev_attr_pwm2.dev_attr.attr,
986 &sensor_dev_attr_pwm3.dev_attr.attr,
987 &sensor_dev_attr_pwm1_enable.dev_attr.attr,
988 &sensor_dev_attr_pwm2_enable.dev_attr.attr,
989 &sensor_dev_attr_pwm3_enable.dev_attr.attr,
991 &sensor_dev_attr_in0_input.dev_attr.attr,
992 &sensor_dev_attr_in1_input.dev_attr.attr,
993 &sensor_dev_attr_in2_input.dev_attr.attr,
994 &sensor_dev_attr_in3_input.dev_attr.attr,
995 &sensor_dev_attr_in0_min.dev_attr.attr,
996 &sensor_dev_attr_in1_min.dev_attr.attr,
997 &sensor_dev_attr_in2_min.dev_attr.attr,
998 &sensor_dev_attr_in3_min.dev_attr.attr,
999 &sensor_dev_attr_in0_max.dev_attr.attr,
1000 &sensor_dev_attr_in1_max.dev_attr.attr,
1001 &sensor_dev_attr_in2_max.dev_attr.attr,
1002 &sensor_dev_attr_in3_max.dev_attr.attr,
1003 &sensor_dev_attr_in0_alarm.dev_attr.attr,
1004 &sensor_dev_attr_in1_alarm.dev_attr.attr,
1005 &sensor_dev_attr_in2_alarm.dev_attr.attr,
1006 &sensor_dev_attr_in3_alarm.dev_attr.attr,
1008 &sensor_dev_attr_temp1_input.dev_attr.attr,
1009 &sensor_dev_attr_temp2_input.dev_attr.attr,
1010 &sensor_dev_attr_temp3_input.dev_attr.attr,
1011 &sensor_dev_attr_temp1_min.dev_attr.attr,
1012 &sensor_dev_attr_temp2_min.dev_attr.attr,
1013 &sensor_dev_attr_temp3_min.dev_attr.attr,
1014 &sensor_dev_attr_temp1_max.dev_attr.attr,
1015 &sensor_dev_attr_temp2_max.dev_attr.attr,
1016 &sensor_dev_attr_temp3_max.dev_attr.attr,
1017 &sensor_dev_attr_temp1_alarm.dev_attr.attr,
1018 &sensor_dev_attr_temp2_alarm.dev_attr.attr,
1019 &sensor_dev_attr_temp3_alarm.dev_attr.attr,
1020 &sensor_dev_attr_temp1_fault.dev_attr.attr,
1021 &sensor_dev_attr_temp3_fault.dev_attr.attr,
1023 &sensor_dev_attr_pwm1_auto_channels.dev_attr.attr,
1024 &sensor_dev_attr_pwm2_auto_channels.dev_attr.attr,
1025 &sensor_dev_attr_pwm3_auto_channels.dev_attr.attr,
1026 &sensor_dev_attr_pwm1_auto_pwm_min.dev_attr.attr,
1027 &sensor_dev_attr_pwm2_auto_pwm_min.dev_attr.attr,
1028 &sensor_dev_attr_pwm3_auto_pwm_min.dev_attr.attr,
1029 &sensor_dev_attr_pwm1_auto_pwm_minctl.dev_attr.attr,
1030 &sensor_dev_attr_pwm2_auto_pwm_minctl.dev_attr.attr,
1031 &sensor_dev_attr_pwm3_auto_pwm_minctl.dev_attr.attr,
1032 &sensor_dev_attr_pwm1_auto_pwm_freq.dev_attr.attr,
1033 &sensor_dev_attr_pwm2_auto_pwm_freq.dev_attr.attr,
1034 &sensor_dev_attr_pwm3_auto_pwm_freq.dev_attr.attr,
1036 &sensor_dev_attr_temp1_auto_temp_off.dev_attr.attr,
1037 &sensor_dev_attr_temp2_auto_temp_off.dev_attr.attr,
1038 &sensor_dev_attr_temp3_auto_temp_off.dev_attr.attr,
1039 &sensor_dev_attr_temp1_auto_temp_min.dev_attr.attr,
1040 &sensor_dev_attr_temp2_auto_temp_min.dev_attr.attr,
1041 &sensor_dev_attr_temp3_auto_temp_min.dev_attr.attr,
1042 &sensor_dev_attr_temp1_auto_temp_max.dev_attr.attr,
1043 &sensor_dev_attr_temp2_auto_temp_max.dev_attr.attr,
1044 &sensor_dev_attr_temp3_auto_temp_max.dev_attr.attr,
1045 &sensor_dev_attr_temp1_auto_temp_crit.dev_attr.attr,
1046 &sensor_dev_attr_temp2_auto_temp_crit.dev_attr.attr,
1047 &sensor_dev_attr_temp3_auto_temp_crit.dev_attr.attr,
1049 &dev_attr_vrm.attr,
1050 &dev_attr_cpu0_vid.attr,
1051 &dev_attr_alarms.attr,
1052 NULL
1055 static const struct attribute_group lm85_group = {
1056 .attrs = lm85_attributes,
1059 static struct attribute *lm85_attributes_in4[] = {
1060 &sensor_dev_attr_in4_input.dev_attr.attr,
1061 &sensor_dev_attr_in4_min.dev_attr.attr,
1062 &sensor_dev_attr_in4_max.dev_attr.attr,
1063 &sensor_dev_attr_in4_alarm.dev_attr.attr,
1064 NULL
1067 static const struct attribute_group lm85_group_in4 = {
1068 .attrs = lm85_attributes_in4,
1071 static struct attribute *lm85_attributes_in567[] = {
1072 &sensor_dev_attr_in5_input.dev_attr.attr,
1073 &sensor_dev_attr_in6_input.dev_attr.attr,
1074 &sensor_dev_attr_in7_input.dev_attr.attr,
1075 &sensor_dev_attr_in5_min.dev_attr.attr,
1076 &sensor_dev_attr_in6_min.dev_attr.attr,
1077 &sensor_dev_attr_in7_min.dev_attr.attr,
1078 &sensor_dev_attr_in5_max.dev_attr.attr,
1079 &sensor_dev_attr_in6_max.dev_attr.attr,
1080 &sensor_dev_attr_in7_max.dev_attr.attr,
1081 &sensor_dev_attr_in5_alarm.dev_attr.attr,
1082 &sensor_dev_attr_in6_alarm.dev_attr.attr,
1083 &sensor_dev_attr_in7_alarm.dev_attr.attr,
1084 NULL
1087 static const struct attribute_group lm85_group_in567 = {
1088 .attrs = lm85_attributes_in567,
1091 static int lm85_detect(struct i2c_adapter *adapter, int address,
1092 int kind)
1094 int company, verstep ;
1095 struct i2c_client *new_client = NULL;
1096 struct lm85_data *data;
1097 int err = 0;
1098 const char *type_name = "";
1100 if (!i2c_check_functionality(adapter,
1101 I2C_FUNC_SMBUS_BYTE_DATA)) {
1102 /* We need to be able to do byte I/O */
1103 goto ERROR0 ;
1106 /* OK. For now, we presume we have a valid client. We now create the
1107 client structure, even though we cannot fill it completely yet.
1108 But it allows us to access lm85_{read,write}_value. */
1110 if (!(data = kzalloc(sizeof(struct lm85_data), GFP_KERNEL))) {
1111 err = -ENOMEM;
1112 goto ERROR0;
1115 new_client = &data->client;
1116 i2c_set_clientdata(new_client, data);
1117 new_client->addr = address;
1118 new_client->adapter = adapter;
1119 new_client->driver = &lm85_driver;
1120 new_client->flags = 0;
1122 /* Now, we do the remaining detection. */
1124 company = lm85_read_value(new_client, LM85_REG_COMPANY);
1125 verstep = lm85_read_value(new_client, LM85_REG_VERSTEP);
1127 dev_dbg(&adapter->dev, "Detecting device at %d,0x%02x with"
1128 " COMPANY: 0x%02x and VERSTEP: 0x%02x\n",
1129 i2c_adapter_id(new_client->adapter), new_client->addr,
1130 company, verstep);
1132 /* If auto-detecting, Determine the chip type. */
1133 if (kind <= 0) {
1134 dev_dbg(&adapter->dev, "Autodetecting device at %d,0x%02x ...\n",
1135 i2c_adapter_id(adapter), address );
1136 if( company == LM85_COMPANY_NATIONAL
1137 && verstep == LM85_VERSTEP_LM85C ) {
1138 kind = lm85c ;
1139 } else if( company == LM85_COMPANY_NATIONAL
1140 && verstep == LM85_VERSTEP_LM85B ) {
1141 kind = lm85b ;
1142 } else if( company == LM85_COMPANY_NATIONAL
1143 && (verstep & LM85_VERSTEP_VMASK) == LM85_VERSTEP_GENERIC ) {
1144 dev_err(&adapter->dev, "Unrecognized version/stepping 0x%02x"
1145 " Defaulting to LM85.\n", verstep);
1146 kind = any_chip ;
1147 } else if( company == LM85_COMPANY_ANALOG_DEV
1148 && verstep == LM85_VERSTEP_ADM1027 ) {
1149 kind = adm1027 ;
1150 } else if( company == LM85_COMPANY_ANALOG_DEV
1151 && (verstep == LM85_VERSTEP_ADT7463
1152 || verstep == LM85_VERSTEP_ADT7463C) ) {
1153 kind = adt7463 ;
1154 } else if( company == LM85_COMPANY_ANALOG_DEV
1155 && (verstep & LM85_VERSTEP_VMASK) == LM85_VERSTEP_GENERIC ) {
1156 dev_err(&adapter->dev, "Unrecognized version/stepping 0x%02x"
1157 " Defaulting to Generic LM85.\n", verstep );
1158 kind = any_chip ;
1159 } else if( company == LM85_COMPANY_SMSC
1160 && (verstep == LM85_VERSTEP_EMC6D100_A0
1161 || verstep == LM85_VERSTEP_EMC6D100_A1) ) {
1162 /* Unfortunately, we can't tell a '100 from a '101
1163 * from the registers. Since a '101 is a '100
1164 * in a package with fewer pins and therefore no
1165 * 3.3V, 1.5V or 1.8V inputs, perhaps if those
1166 * inputs read 0, then it's a '101.
1168 kind = emc6d100 ;
1169 } else if( company == LM85_COMPANY_SMSC
1170 && verstep == LM85_VERSTEP_EMC6D102) {
1171 kind = emc6d102 ;
1172 } else if( company == LM85_COMPANY_SMSC
1173 && (verstep & LM85_VERSTEP_VMASK) == LM85_VERSTEP_GENERIC) {
1174 dev_err(&adapter->dev, "lm85: Detected SMSC chip\n");
1175 dev_err(&adapter->dev, "lm85: Unrecognized version/stepping 0x%02x"
1176 " Defaulting to Generic LM85.\n", verstep );
1177 kind = any_chip ;
1178 } else if( kind == any_chip
1179 && (verstep & LM85_VERSTEP_VMASK) == LM85_VERSTEP_GENERIC) {
1180 dev_err(&adapter->dev, "Generic LM85 Version 6 detected\n");
1181 /* Leave kind as "any_chip" */
1182 } else {
1183 dev_dbg(&adapter->dev, "Autodetection failed\n");
1184 /* Not an LM85 ... */
1185 if( kind == any_chip ) { /* User used force=x,y */
1186 dev_err(&adapter->dev, "Generic LM85 Version 6 not"
1187 " found at %d,0x%02x. Try force_lm85c.\n",
1188 i2c_adapter_id(adapter), address );
1190 err = 0 ;
1191 goto ERROR1;
1195 /* Fill in the chip specific driver values */
1196 if ( kind == any_chip ) {
1197 type_name = "lm85";
1198 } else if ( kind == lm85b ) {
1199 type_name = "lm85b";
1200 } else if ( kind == lm85c ) {
1201 type_name = "lm85c";
1202 } else if ( kind == adm1027 ) {
1203 type_name = "adm1027";
1204 } else if ( kind == adt7463 ) {
1205 type_name = "adt7463";
1206 } else if ( kind == emc6d100){
1207 type_name = "emc6d100";
1208 } else if ( kind == emc6d102 ) {
1209 type_name = "emc6d102";
1211 strlcpy(new_client->name, type_name, I2C_NAME_SIZE);
1213 /* Fill in the remaining client fields */
1214 data->type = kind;
1215 data->valid = 0;
1216 mutex_init(&data->update_lock);
1218 /* Tell the I2C layer a new client has arrived */
1219 if ((err = i2c_attach_client(new_client)))
1220 goto ERROR1;
1222 /* Set the VRM version */
1223 data->vrm = vid_which_vrm();
1225 /* Initialize the LM85 chip */
1226 lm85_init_client(new_client);
1228 /* Register sysfs hooks */
1229 if ((err = sysfs_create_group(&new_client->dev.kobj, &lm85_group)))
1230 goto ERROR2;
1232 /* The ADT7463 has an optional VRM 10 mode where pin 21 is used
1233 as a sixth digital VID input rather than an analog input. */
1234 data->vid = lm85_read_value(new_client, LM85_REG_VID);
1235 if (!(kind == adt7463 && (data->vid & 0x80)))
1236 if ((err = sysfs_create_group(&new_client->dev.kobj,
1237 &lm85_group_in4)))
1238 goto ERROR3;
1240 /* The EMC6D100 has 3 additional voltage inputs */
1241 if (kind == emc6d100)
1242 if ((err = sysfs_create_group(&new_client->dev.kobj,
1243 &lm85_group_in567)))
1244 goto ERROR3;
1246 data->hwmon_dev = hwmon_device_register(&new_client->dev);
1247 if (IS_ERR(data->hwmon_dev)) {
1248 err = PTR_ERR(data->hwmon_dev);
1249 goto ERROR3;
1252 return 0;
1254 /* Error out and cleanup code */
1255 ERROR3:
1256 sysfs_remove_group(&new_client->dev.kobj, &lm85_group);
1257 sysfs_remove_group(&new_client->dev.kobj, &lm85_group_in4);
1258 if (kind == emc6d100)
1259 sysfs_remove_group(&new_client->dev.kobj, &lm85_group_in567);
1260 ERROR2:
1261 i2c_detach_client(new_client);
1262 ERROR1:
1263 kfree(data);
1264 ERROR0:
1265 return err;
1268 static int lm85_detach_client(struct i2c_client *client)
1270 struct lm85_data *data = i2c_get_clientdata(client);
1271 hwmon_device_unregister(data->hwmon_dev);
1272 sysfs_remove_group(&client->dev.kobj, &lm85_group);
1273 sysfs_remove_group(&client->dev.kobj, &lm85_group_in4);
1274 if (data->type == emc6d100)
1275 sysfs_remove_group(&client->dev.kobj, &lm85_group_in567);
1276 i2c_detach_client(client);
1277 kfree(data);
1278 return 0;
1282 static int lm85_read_value(struct i2c_client *client, u8 reg)
1284 int res;
1286 /* What size location is it? */
1287 switch( reg ) {
1288 case LM85_REG_FAN(0) : /* Read WORD data */
1289 case LM85_REG_FAN(1) :
1290 case LM85_REG_FAN(2) :
1291 case LM85_REG_FAN(3) :
1292 case LM85_REG_FAN_MIN(0) :
1293 case LM85_REG_FAN_MIN(1) :
1294 case LM85_REG_FAN_MIN(2) :
1295 case LM85_REG_FAN_MIN(3) :
1296 case LM85_REG_ALARM1 : /* Read both bytes at once */
1297 res = i2c_smbus_read_byte_data(client, reg) & 0xff ;
1298 res |= i2c_smbus_read_byte_data(client, reg+1) << 8 ;
1299 break ;
1300 case ADT7463_REG_TMIN_CTL1 : /* Read WORD MSB, LSB */
1301 res = i2c_smbus_read_byte_data(client, reg) << 8 ;
1302 res |= i2c_smbus_read_byte_data(client, reg+1) & 0xff ;
1303 break ;
1304 default: /* Read BYTE data */
1305 res = i2c_smbus_read_byte_data(client, reg);
1306 break ;
1309 return res ;
1312 static int lm85_write_value(struct i2c_client *client, u8 reg, int value)
1314 int res ;
1316 switch( reg ) {
1317 case LM85_REG_FAN(0) : /* Write WORD data */
1318 case LM85_REG_FAN(1) :
1319 case LM85_REG_FAN(2) :
1320 case LM85_REG_FAN(3) :
1321 case LM85_REG_FAN_MIN(0) :
1322 case LM85_REG_FAN_MIN(1) :
1323 case LM85_REG_FAN_MIN(2) :
1324 case LM85_REG_FAN_MIN(3) :
1325 /* NOTE: ALARM is read only, so not included here */
1326 res = i2c_smbus_write_byte_data(client, reg, value & 0xff) ;
1327 res |= i2c_smbus_write_byte_data(client, reg+1, (value>>8) & 0xff) ;
1328 break ;
1329 case ADT7463_REG_TMIN_CTL1 : /* Write WORD MSB, LSB */
1330 res = i2c_smbus_write_byte_data(client, reg, (value>>8) & 0xff);
1331 res |= i2c_smbus_write_byte_data(client, reg+1, value & 0xff) ;
1332 break ;
1333 default: /* Write BYTE data */
1334 res = i2c_smbus_write_byte_data(client, reg, value);
1335 break ;
1338 return res ;
1341 static void lm85_init_client(struct i2c_client *client)
1343 int value;
1344 struct lm85_data *data = i2c_get_clientdata(client);
1346 dev_dbg(&client->dev, "Initializing device\n");
1348 /* Warn if part was not "READY" */
1349 value = lm85_read_value(client, LM85_REG_CONFIG);
1350 dev_dbg(&client->dev, "LM85_REG_CONFIG is: 0x%02x\n", value);
1351 if( value & 0x02 ) {
1352 dev_err(&client->dev, "Client (%d,0x%02x) config is locked.\n",
1353 i2c_adapter_id(client->adapter), client->addr );
1355 if( ! (value & 0x04) ) {
1356 dev_err(&client->dev, "Client (%d,0x%02x) is not ready.\n",
1357 i2c_adapter_id(client->adapter), client->addr );
1359 if( value & 0x10
1360 && ( data->type == adm1027
1361 || data->type == adt7463 ) ) {
1362 dev_err(&client->dev, "Client (%d,0x%02x) VxI mode is set. "
1363 "Please report this to the lm85 maintainer.\n",
1364 i2c_adapter_id(client->adapter), client->addr );
1367 /* WE INTENTIONALLY make no changes to the limits,
1368 * offsets, pwms, fans and zones. If they were
1369 * configured, we don't want to mess with them.
1370 * If they weren't, the default is 100% PWM, no
1371 * control and will suffice until 'sensors -s'
1372 * can be run by the user.
1375 /* Start monitoring */
1376 value = lm85_read_value(client, LM85_REG_CONFIG);
1377 /* Try to clear LOCK, Set START, save everything else */
1378 value = (value & ~ 0x02) | 0x01 ;
1379 dev_dbg(&client->dev, "Setting CONFIG to: 0x%02x\n", value);
1380 lm85_write_value(client, LM85_REG_CONFIG, value);
1383 static struct lm85_data *lm85_update_device(struct device *dev)
1385 struct i2c_client *client = to_i2c_client(dev);
1386 struct lm85_data *data = i2c_get_clientdata(client);
1387 int i;
1389 mutex_lock(&data->update_lock);
1391 if ( !data->valid ||
1392 time_after(jiffies, data->last_reading + LM85_DATA_INTERVAL) ) {
1393 /* Things that change quickly */
1394 dev_dbg(&client->dev, "Reading sensor values\n");
1396 /* Have to read extended bits first to "freeze" the
1397 * more significant bits that are read later.
1398 * There are 2 additional resolution bits per channel and we
1399 * have room for 4, so we shift them to the left.
1401 if ( (data->type == adm1027) || (data->type == adt7463) ) {
1402 int ext1 = lm85_read_value(client,
1403 ADM1027_REG_EXTEND_ADC1);
1404 int ext2 = lm85_read_value(client,
1405 ADM1027_REG_EXTEND_ADC2);
1406 int val = (ext1 << 8) + ext2;
1408 for(i = 0; i <= 4; i++)
1409 data->in_ext[i] = ((val>>(i * 2))&0x03) << 2;
1411 for(i = 0; i <= 2; i++)
1412 data->temp_ext[i] = (val>>((i + 4) * 2))&0x0c;
1415 data->vid = lm85_read_value(client, LM85_REG_VID);
1417 for (i = 0; i <= 3; ++i) {
1418 data->in[i] =
1419 lm85_read_value(client, LM85_REG_IN(i));
1422 if (!(data->type == adt7463 && (data->vid & 0x80))) {
1423 data->in[4] = lm85_read_value(client,
1424 LM85_REG_IN(4));
1427 for (i = 0; i <= 3; ++i) {
1428 data->fan[i] =
1429 lm85_read_value(client, LM85_REG_FAN(i));
1432 for (i = 0; i <= 2; ++i) {
1433 data->temp[i] =
1434 lm85_read_value(client, LM85_REG_TEMP(i));
1437 for (i = 0; i <= 2; ++i) {
1438 data->pwm[i] =
1439 lm85_read_value(client, LM85_REG_PWM(i));
1442 data->alarms = lm85_read_value(client, LM85_REG_ALARM1);
1444 if ( data->type == adt7463 ) {
1445 if( data->therm_total < ULONG_MAX - 256 ) {
1446 data->therm_total +=
1447 lm85_read_value(client, ADT7463_REG_THERM );
1449 } else if ( data->type == emc6d100 ) {
1450 /* Three more voltage sensors */
1451 for (i = 5; i <= 7; ++i) {
1452 data->in[i] =
1453 lm85_read_value(client, EMC6D100_REG_IN(i));
1455 /* More alarm bits */
1456 data->alarms |=
1457 lm85_read_value(client, EMC6D100_REG_ALARM3) << 16;
1458 } else if (data->type == emc6d102 ) {
1459 /* Have to read LSB bits after the MSB ones because
1460 the reading of the MSB bits has frozen the
1461 LSBs (backward from the ADM1027).
1463 int ext1 = lm85_read_value(client,
1464 EMC6D102_REG_EXTEND_ADC1);
1465 int ext2 = lm85_read_value(client,
1466 EMC6D102_REG_EXTEND_ADC2);
1467 int ext3 = lm85_read_value(client,
1468 EMC6D102_REG_EXTEND_ADC3);
1469 int ext4 = lm85_read_value(client,
1470 EMC6D102_REG_EXTEND_ADC4);
1471 data->in_ext[0] = ext3 & 0x0f;
1472 data->in_ext[1] = ext4 & 0x0f;
1473 data->in_ext[2] = (ext4 >> 4) & 0x0f;
1474 data->in_ext[3] = (ext3 >> 4) & 0x0f;
1475 data->in_ext[4] = (ext2 >> 4) & 0x0f;
1477 data->temp_ext[0] = ext1 & 0x0f;
1478 data->temp_ext[1] = ext2 & 0x0f;
1479 data->temp_ext[2] = (ext1 >> 4) & 0x0f;
1482 data->last_reading = jiffies ;
1483 }; /* last_reading */
1485 if ( !data->valid ||
1486 time_after(jiffies, data->last_config + LM85_CONFIG_INTERVAL) ) {
1487 /* Things that don't change often */
1488 dev_dbg(&client->dev, "Reading config values\n");
1490 for (i = 0; i <= 3; ++i) {
1491 data->in_min[i] =
1492 lm85_read_value(client, LM85_REG_IN_MIN(i));
1493 data->in_max[i] =
1494 lm85_read_value(client, LM85_REG_IN_MAX(i));
1497 if (!(data->type == adt7463 && (data->vid & 0x80))) {
1498 data->in_min[4] = lm85_read_value(client,
1499 LM85_REG_IN_MIN(4));
1500 data->in_max[4] = lm85_read_value(client,
1501 LM85_REG_IN_MAX(4));
1504 if ( data->type == emc6d100 ) {
1505 for (i = 5; i <= 7; ++i) {
1506 data->in_min[i] =
1507 lm85_read_value(client, EMC6D100_REG_IN_MIN(i));
1508 data->in_max[i] =
1509 lm85_read_value(client, EMC6D100_REG_IN_MAX(i));
1513 for (i = 0; i <= 3; ++i) {
1514 data->fan_min[i] =
1515 lm85_read_value(client, LM85_REG_FAN_MIN(i));
1518 for (i = 0; i <= 2; ++i) {
1519 data->temp_min[i] =
1520 lm85_read_value(client, LM85_REG_TEMP_MIN(i));
1521 data->temp_max[i] =
1522 lm85_read_value(client, LM85_REG_TEMP_MAX(i));
1525 for (i = 0; i <= 2; ++i) {
1526 int val ;
1527 data->autofan[i].config =
1528 lm85_read_value(client, LM85_REG_AFAN_CONFIG(i));
1529 val = lm85_read_value(client, LM85_REG_AFAN_RANGE(i));
1530 data->autofan[i].freq = val & 0x07 ;
1531 data->zone[i].range = (val >> 4) & 0x0f ;
1532 data->autofan[i].min_pwm =
1533 lm85_read_value(client, LM85_REG_AFAN_MINPWM(i));
1534 data->zone[i].limit =
1535 lm85_read_value(client, LM85_REG_AFAN_LIMIT(i));
1536 data->zone[i].critical =
1537 lm85_read_value(client, LM85_REG_AFAN_CRITICAL(i));
1540 i = lm85_read_value(client, LM85_REG_AFAN_SPIKE1);
1541 data->smooth[0] = i & 0x0f ;
1542 data->syncpwm3 = i & 0x10 ; /* Save PWM3 config */
1543 data->autofan[0].min_off = (i & 0x20) != 0 ;
1544 data->autofan[1].min_off = (i & 0x40) != 0 ;
1545 data->autofan[2].min_off = (i & 0x80) != 0 ;
1546 i = lm85_read_value(client, LM85_REG_AFAN_SPIKE2);
1547 data->smooth[1] = (i>>4) & 0x0f ;
1548 data->smooth[2] = i & 0x0f ;
1550 i = lm85_read_value(client, LM85_REG_AFAN_HYST1);
1551 data->zone[0].hyst = (i>>4) & 0x0f ;
1552 data->zone[1].hyst = i & 0x0f ;
1554 i = lm85_read_value(client, LM85_REG_AFAN_HYST2);
1555 data->zone[2].hyst = (i>>4) & 0x0f ;
1557 if ( (data->type == lm85b) || (data->type == lm85c) ) {
1558 data->tach_mode = lm85_read_value(client,
1559 LM85_REG_TACH_MODE );
1560 data->spinup_ctl = lm85_read_value(client,
1561 LM85_REG_SPINUP_CTL );
1562 } else if ( (data->type == adt7463) || (data->type == adm1027) ) {
1563 if ( data->type == adt7463 ) {
1564 for (i = 0; i <= 2; ++i) {
1565 data->oppoint[i] = lm85_read_value(client,
1566 ADT7463_REG_OPPOINT(i) );
1568 data->tmin_ctl = lm85_read_value(client,
1569 ADT7463_REG_TMIN_CTL1 );
1570 data->therm_limit = lm85_read_value(client,
1571 ADT7463_REG_THERM_LIMIT );
1573 for (i = 0; i <= 2; ++i) {
1574 data->temp_offset[i] = lm85_read_value(client,
1575 ADM1027_REG_TEMP_OFFSET(i) );
1577 data->tach_mode = lm85_read_value(client,
1578 ADM1027_REG_CONFIG3 );
1579 data->fan_ppr = lm85_read_value(client,
1580 ADM1027_REG_FAN_PPR );
1583 data->last_config = jiffies;
1584 }; /* last_config */
1586 data->valid = 1;
1588 mutex_unlock(&data->update_lock);
1590 return data;
1594 static int __init sm_lm85_init(void)
1596 return i2c_add_driver(&lm85_driver);
1599 static void __exit sm_lm85_exit(void)
1601 i2c_del_driver(&lm85_driver);
1604 /* Thanks to Richard Barrington for adding the LM85 to sensors-detect.
1605 * Thanks to Margit Schubert-While <margitsw@t-online.de> for help with
1606 * post 2.7.0 CVS changes.
1608 MODULE_LICENSE("GPL");
1609 MODULE_AUTHOR("Philip Pokorny <ppokorny@penguincomputing.com>, Margit Schubert-While <margitsw@t-online.de>, Justin Thiessen <jthiessen@penguincomputing.com");
1610 MODULE_DESCRIPTION("LM85-B, LM85-C driver");
1612 module_init(sm_lm85_init);
1613 module_exit(sm_lm85_exit);