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>
8 Copyright (C) 2007--2009 Jean Delvare <khali@linux-fr.org>
10 Chip details at <http://www.national.com/ds/LM/LM85.pdf>
12 This program is free software; you can redistribute it and/or modify
13 it under the terms of the GNU General Public License as published by
14 the Free Software Foundation; either version 2 of the License, or
15 (at your option) any later version.
17 This program is distributed in the hope that it will be useful,
18 but WITHOUT ANY WARRANTY; without even the implied warranty of
19 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 GNU General Public License for more details.
22 You should have received a copy of the GNU General Public License
23 along with this program; if not, write to the Free Software
24 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
27 #include <linux/module.h>
28 #include <linux/init.h>
29 #include <linux/slab.h>
30 #include <linux/jiffies.h>
31 #include <linux/i2c.h>
32 #include <linux/hwmon.h>
33 #include <linux/hwmon-vid.h>
34 #include <linux/hwmon-sysfs.h>
35 #include <linux/err.h>
36 #include <linux/mutex.h>
38 /* Addresses to scan */
39 static const unsigned short normal_i2c
[] = { 0x2c, 0x2d, 0x2e, I2C_CLIENT_END
};
42 any_chip
, lm85b
, lm85c
,
43 adm1027
, adt7463
, adt7468
,
47 /* The LM85 registers */
49 #define LM85_REG_IN(nr) (0x20 + (nr))
50 #define LM85_REG_IN_MIN(nr) (0x44 + (nr) * 2)
51 #define LM85_REG_IN_MAX(nr) (0x45 + (nr) * 2)
53 #define LM85_REG_TEMP(nr) (0x25 + (nr))
54 #define LM85_REG_TEMP_MIN(nr) (0x4e + (nr) * 2)
55 #define LM85_REG_TEMP_MAX(nr) (0x4f + (nr) * 2)
57 /* Fan speeds are LSB, MSB (2 bytes) */
58 #define LM85_REG_FAN(nr) (0x28 + (nr) * 2)
59 #define LM85_REG_FAN_MIN(nr) (0x54 + (nr) * 2)
61 #define LM85_REG_PWM(nr) (0x30 + (nr))
63 #define LM85_REG_COMPANY 0x3e
64 #define LM85_REG_VERSTEP 0x3f
66 #define ADT7468_REG_CFG5 0x7c
67 #define ADT7468_OFF64 (1 << 0)
68 #define ADT7468_HFPWM (1 << 1)
69 #define IS_ADT7468_OFF64(data) \
70 ((data)->type == adt7468 && !((data)->cfg5 & ADT7468_OFF64))
71 #define IS_ADT7468_HFPWM(data) \
72 ((data)->type == adt7468 && !((data)->cfg5 & ADT7468_HFPWM))
74 /* These are the recognized values for the above regs */
75 #define LM85_COMPANY_NATIONAL 0x01
76 #define LM85_COMPANY_ANALOG_DEV 0x41
77 #define LM85_COMPANY_SMSC 0x5c
78 #define LM85_VERSTEP_VMASK 0xf0
79 #define LM85_VERSTEP_GENERIC 0x60
80 #define LM85_VERSTEP_GENERIC2 0x70
81 #define LM85_VERSTEP_LM85C 0x60
82 #define LM85_VERSTEP_LM85B 0x62
83 #define LM85_VERSTEP_LM96000_1 0x68
84 #define LM85_VERSTEP_LM96000_2 0x69
85 #define LM85_VERSTEP_ADM1027 0x60
86 #define LM85_VERSTEP_ADT7463 0x62
87 #define LM85_VERSTEP_ADT7463C 0x6A
88 #define LM85_VERSTEP_ADT7468_1 0x71
89 #define LM85_VERSTEP_ADT7468_2 0x72
90 #define LM85_VERSTEP_EMC6D100_A0 0x60
91 #define LM85_VERSTEP_EMC6D100_A1 0x61
92 #define LM85_VERSTEP_EMC6D102 0x65
94 #define LM85_REG_CONFIG 0x40
96 #define LM85_REG_ALARM1 0x41
97 #define LM85_REG_ALARM2 0x42
99 #define LM85_REG_VID 0x43
101 /* Automated FAN control */
102 #define LM85_REG_AFAN_CONFIG(nr) (0x5c + (nr))
103 #define LM85_REG_AFAN_RANGE(nr) (0x5f + (nr))
104 #define LM85_REG_AFAN_SPIKE1 0x62
105 #define LM85_REG_AFAN_MINPWM(nr) (0x64 + (nr))
106 #define LM85_REG_AFAN_LIMIT(nr) (0x67 + (nr))
107 #define LM85_REG_AFAN_CRITICAL(nr) (0x6a + (nr))
108 #define LM85_REG_AFAN_HYST1 0x6d
109 #define LM85_REG_AFAN_HYST2 0x6e
111 #define ADM1027_REG_EXTEND_ADC1 0x76
112 #define ADM1027_REG_EXTEND_ADC2 0x77
114 #define EMC6D100_REG_ALARM3 0x7d
115 /* IN5, IN6 and IN7 */
116 #define EMC6D100_REG_IN(nr) (0x70 + ((nr) - 5))
117 #define EMC6D100_REG_IN_MIN(nr) (0x73 + ((nr) - 5) * 2)
118 #define EMC6D100_REG_IN_MAX(nr) (0x74 + ((nr) - 5) * 2)
119 #define EMC6D102_REG_EXTEND_ADC1 0x85
120 #define EMC6D102_REG_EXTEND_ADC2 0x86
121 #define EMC6D102_REG_EXTEND_ADC3 0x87
122 #define EMC6D102_REG_EXTEND_ADC4 0x88
125 /* Conversions. Rounding and limit checking is only done on the TO_REG
126 variants. Note that you should be a bit careful with which arguments
127 these macros are called: arguments may be evaluated more than once.
130 /* IN are scaled acording to built-in resistors */
131 static const int lm85_scaling
[] = { /* .001 Volts */
132 2500, 2250, 3300, 5000, 12000,
133 3300, 1500, 1800 /*EMC6D100*/
135 #define SCALE(val, from, to) (((val) * (to) + ((from) / 2)) / (from))
137 #define INS_TO_REG(n, val) \
138 SENSORS_LIMIT(SCALE(val, lm85_scaling[n], 192), 0, 255)
140 #define INSEXT_FROM_REG(n, val, ext) \
141 SCALE(((val) << 4) + (ext), 192 << 4, lm85_scaling[n])
143 #define INS_FROM_REG(n, val) SCALE((val), 192, lm85_scaling[n])
145 /* FAN speed is measured using 90kHz clock */
146 static inline u16
FAN_TO_REG(unsigned long val
)
150 return SENSORS_LIMIT(5400000 / val
, 1, 0xfffe);
152 #define FAN_FROM_REG(val) ((val) == 0 ? -1 : (val) == 0xffff ? 0 : \
155 /* Temperature is reported in .001 degC increments */
156 #define TEMP_TO_REG(val) \
157 SENSORS_LIMIT(SCALE(val, 1000, 1), -127, 127)
158 #define TEMPEXT_FROM_REG(val, ext) \
159 SCALE(((val) << 4) + (ext), 16, 1000)
160 #define TEMP_FROM_REG(val) ((val) * 1000)
162 #define PWM_TO_REG(val) SENSORS_LIMIT(val, 0, 255)
163 #define PWM_FROM_REG(val) (val)
166 /* ZONEs have the following parameters:
167 * Limit (low) temp, 1. degC
168 * Hysteresis (below limit), 1. degC (0-15)
169 * Range of speed control, .1 degC (2-80)
170 * Critical (high) temp, 1. degC
172 * FAN PWMs have the following parameters:
173 * Reference Zone, 1, 2, 3, etc.
174 * Spinup time, .05 sec
175 * PWM value at limit/low temp, 1 count
176 * PWM Frequency, 1. Hz
177 * PWM is Min or OFF below limit, flag
178 * Invert PWM output, flag
180 * Some chips filter the temp, others the fan.
181 * Filter constant (or disabled) .1 seconds
184 /* These are the zone temperature range encodings in .001 degree C */
185 static const int lm85_range_map
[] = {
186 2000, 2500, 3300, 4000, 5000, 6600, 8000, 10000,
187 13300, 16000, 20000, 26600, 32000, 40000, 53300, 80000
190 static int RANGE_TO_REG(int range
)
194 /* Find the closest match */
195 for (i
= 0; i
< 15; ++i
) {
196 if (range
<= (lm85_range_map
[i
] + lm85_range_map
[i
+ 1]) / 2)
202 #define RANGE_FROM_REG(val) lm85_range_map[(val) & 0x0f]
204 /* These are the PWM frequency encodings */
205 static const int lm85_freq_map
[8] = { /* 1 Hz */
206 10, 15, 23, 30, 38, 47, 61, 94
208 static const int adm1027_freq_map
[8] = { /* 1 Hz */
209 11, 15, 22, 29, 35, 44, 59, 88
212 static int FREQ_TO_REG(const int *map
, int freq
)
216 /* Find the closest match */
217 for (i
= 0; i
< 7; ++i
)
218 if (freq
<= (map
[i
] + map
[i
+ 1]) / 2)
223 static int FREQ_FROM_REG(const int *map
, u8 reg
)
225 return map
[reg
& 0x07];
228 /* Since we can't use strings, I'm abusing these numbers
229 * to stand in for the following meanings:
230 * 1 -- PWM responds to Zone 1
231 * 2 -- PWM responds to Zone 2
232 * 3 -- PWM responds to Zone 3
233 * 23 -- PWM responds to the higher temp of Zone 2 or 3
234 * 123 -- PWM responds to highest of Zone 1, 2, or 3
235 * 0 -- PWM is always at 0% (ie, off)
236 * -1 -- PWM is always at 100%
237 * -2 -- PWM responds to manual control
240 static const int lm85_zone_map
[] = { 1, 2, 3, -1, 0, 23, 123, -2 };
241 #define ZONE_FROM_REG(val) lm85_zone_map[(val) >> 5]
243 static int ZONE_TO_REG(int zone
)
247 for (i
= 0; i
<= 7; ++i
)
248 if (zone
== lm85_zone_map
[i
])
250 if (i
> 7) /* Not found. */
251 i
= 3; /* Always 100% */
255 #define HYST_TO_REG(val) SENSORS_LIMIT(((val) + 500) / 1000, 0, 15)
256 #define HYST_FROM_REG(val) ((val) * 1000)
258 /* Chip sampling rates
260 * Some sensors are not updated more frequently than once per second
261 * so it doesn't make sense to read them more often than that.
262 * We cache the results and return the saved data if the driver
263 * is called again before a second has elapsed.
265 * Also, there is significant configuration data for this chip
266 * given the automatic PWM fan control that is possible. There
267 * are about 47 bytes of config data to only 22 bytes of actual
268 * readings. So, we keep the config data up to date in the cache
269 * when it is written and only sample it once every 1 *minute*
271 #define LM85_DATA_INTERVAL (HZ + HZ / 2)
272 #define LM85_CONFIG_INTERVAL (1 * 60 * HZ)
274 /* LM85 can automatically adjust fan speeds based on temperature
275 * This structure encapsulates an entire Zone config. There are
276 * three zones (one for each temperature input) on the lm85
279 s8 limit
; /* Low temp limit */
280 u8 hyst
; /* Low limit hysteresis. (0-15) */
281 u8 range
; /* Temp range, encoded */
282 s8 critical
; /* "All fans ON" temp limit */
283 u8 off_desired
; /* Actual "off" temperature specified. Preserved
284 * to prevent "drift" as other autofan control
287 u8 max_desired
; /* Actual "max" temperature specified. Preserved
288 * to prevent "drift" as other autofan control
293 struct lm85_autofan
{
294 u8 config
; /* Register value */
295 u8 min_pwm
; /* Minimum PWM value, encoded */
296 u8 min_off
; /* Min PWM or OFF below "limit", flag */
299 /* For each registered chip, we need to keep some data in memory.
300 The structure is dynamically allocated. */
302 struct device
*hwmon_dev
;
306 struct mutex update_lock
;
307 int valid
; /* !=0 if following fields are valid */
308 unsigned long last_reading
; /* In jiffies */
309 unsigned long last_config
; /* In jiffies */
311 u8 in
[8]; /* Register value */
312 u8 in_max
[8]; /* Register value */
313 u8 in_min
[8]; /* Register value */
314 s8 temp
[3]; /* Register value */
315 s8 temp_min
[3]; /* Register value */
316 s8 temp_max
[3]; /* Register value */
317 u16 fan
[4]; /* Register value */
318 u16 fan_min
[4]; /* Register value */
319 u8 pwm
[3]; /* Register value */
320 u8 pwm_freq
[3]; /* Register encoding */
321 u8 temp_ext
[3]; /* Decoded values */
322 u8 in_ext
[8]; /* Decoded values */
323 u8 vid
; /* Register value */
324 u8 vrm
; /* VRM version */
325 u32 alarms
; /* Register encoding, combined */
326 u8 cfg5
; /* Config Register 5 on ADT7468 */
327 struct lm85_autofan autofan
[3];
328 struct lm85_zone zone
[3];
331 static int lm85_detect(struct i2c_client
*client
, struct i2c_board_info
*info
);
332 static int lm85_probe(struct i2c_client
*client
,
333 const struct i2c_device_id
*id
);
334 static int lm85_remove(struct i2c_client
*client
);
336 static int lm85_read_value(struct i2c_client
*client
, u8 reg
);
337 static void lm85_write_value(struct i2c_client
*client
, u8 reg
, int value
);
338 static struct lm85_data
*lm85_update_device(struct device
*dev
);
341 static const struct i2c_device_id lm85_id
[] = {
342 { "adm1027", adm1027
},
343 { "adt7463", adt7463
},
344 { "adt7468", adt7468
},
345 { "lm85", any_chip
},
348 { "emc6d100", emc6d100
},
349 { "emc6d101", emc6d100
},
350 { "emc6d102", emc6d102
},
353 MODULE_DEVICE_TABLE(i2c
, lm85_id
);
355 static struct i2c_driver lm85_driver
= {
356 .class = I2C_CLASS_HWMON
,
361 .remove
= lm85_remove
,
363 .detect
= lm85_detect
,
364 .address_list
= normal_i2c
,
369 static ssize_t
show_fan(struct device
*dev
, struct device_attribute
*attr
,
372 int nr
= to_sensor_dev_attr(attr
)->index
;
373 struct lm85_data
*data
= lm85_update_device(dev
);
374 return sprintf(buf
, "%d\n", FAN_FROM_REG(data
->fan
[nr
]));
377 static ssize_t
show_fan_min(struct device
*dev
, struct device_attribute
*attr
,
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_min
[nr
]));
385 static ssize_t
set_fan_min(struct device
*dev
, struct device_attribute
*attr
,
386 const char *buf
, size_t count
)
388 int nr
= to_sensor_dev_attr(attr
)->index
;
389 struct i2c_client
*client
= to_i2c_client(dev
);
390 struct lm85_data
*data
= i2c_get_clientdata(client
);
391 unsigned long val
= simple_strtoul(buf
, NULL
, 10);
393 mutex_lock(&data
->update_lock
);
394 data
->fan_min
[nr
] = FAN_TO_REG(val
);
395 lm85_write_value(client
, LM85_REG_FAN_MIN(nr
), data
->fan_min
[nr
]);
396 mutex_unlock(&data
->update_lock
);
400 #define show_fan_offset(offset) \
401 static SENSOR_DEVICE_ATTR(fan##offset##_input, S_IRUGO, \
402 show_fan, NULL, offset - 1); \
403 static SENSOR_DEVICE_ATTR(fan##offset##_min, S_IRUGO | S_IWUSR, \
404 show_fan_min, set_fan_min, offset - 1)
411 /* vid, vrm, alarms */
413 static ssize_t
show_vid_reg(struct device
*dev
, struct device_attribute
*attr
,
416 struct lm85_data
*data
= lm85_update_device(dev
);
419 if ((data
->type
== adt7463
|| data
->type
== adt7468
) &&
420 (data
->vid
& 0x80)) {
421 /* 6-pin VID (VRM 10) */
422 vid
= vid_from_reg(data
->vid
& 0x3f, data
->vrm
);
424 /* 5-pin VID (VRM 9) */
425 vid
= vid_from_reg(data
->vid
& 0x1f, data
->vrm
);
428 return sprintf(buf
, "%d\n", vid
);
431 static DEVICE_ATTR(cpu0_vid
, S_IRUGO
, show_vid_reg
, NULL
);
433 static ssize_t
show_vrm_reg(struct device
*dev
, struct device_attribute
*attr
,
436 struct lm85_data
*data
= dev_get_drvdata(dev
);
437 return sprintf(buf
, "%ld\n", (long) data
->vrm
);
440 static ssize_t
store_vrm_reg(struct device
*dev
, struct device_attribute
*attr
,
441 const char *buf
, size_t count
)
443 struct lm85_data
*data
= dev_get_drvdata(dev
);
444 data
->vrm
= simple_strtoul(buf
, NULL
, 10);
448 static DEVICE_ATTR(vrm
, S_IRUGO
| S_IWUSR
, show_vrm_reg
, store_vrm_reg
);
450 static ssize_t
show_alarms_reg(struct device
*dev
, struct device_attribute
453 struct lm85_data
*data
= lm85_update_device(dev
);
454 return sprintf(buf
, "%u\n", data
->alarms
);
457 static DEVICE_ATTR(alarms
, S_IRUGO
, show_alarms_reg
, NULL
);
459 static ssize_t
show_alarm(struct device
*dev
, struct device_attribute
*attr
,
462 int nr
= to_sensor_dev_attr(attr
)->index
;
463 struct lm85_data
*data
= lm85_update_device(dev
);
464 return sprintf(buf
, "%u\n", (data
->alarms
>> nr
) & 1);
467 static SENSOR_DEVICE_ATTR(in0_alarm
, S_IRUGO
, show_alarm
, NULL
, 0);
468 static SENSOR_DEVICE_ATTR(in1_alarm
, S_IRUGO
, show_alarm
, NULL
, 1);
469 static SENSOR_DEVICE_ATTR(in2_alarm
, S_IRUGO
, show_alarm
, NULL
, 2);
470 static SENSOR_DEVICE_ATTR(in3_alarm
, S_IRUGO
, show_alarm
, NULL
, 3);
471 static SENSOR_DEVICE_ATTR(in4_alarm
, S_IRUGO
, show_alarm
, NULL
, 8);
472 static SENSOR_DEVICE_ATTR(in5_alarm
, S_IRUGO
, show_alarm
, NULL
, 18);
473 static SENSOR_DEVICE_ATTR(in6_alarm
, S_IRUGO
, show_alarm
, NULL
, 16);
474 static SENSOR_DEVICE_ATTR(in7_alarm
, S_IRUGO
, show_alarm
, NULL
, 17);
475 static SENSOR_DEVICE_ATTR(temp1_alarm
, S_IRUGO
, show_alarm
, NULL
, 4);
476 static SENSOR_DEVICE_ATTR(temp1_fault
, S_IRUGO
, show_alarm
, NULL
, 14);
477 static SENSOR_DEVICE_ATTR(temp2_alarm
, S_IRUGO
, show_alarm
, NULL
, 5);
478 static SENSOR_DEVICE_ATTR(temp3_alarm
, S_IRUGO
, show_alarm
, NULL
, 6);
479 static SENSOR_DEVICE_ATTR(temp3_fault
, S_IRUGO
, show_alarm
, NULL
, 15);
480 static SENSOR_DEVICE_ATTR(fan1_alarm
, S_IRUGO
, show_alarm
, NULL
, 10);
481 static SENSOR_DEVICE_ATTR(fan2_alarm
, S_IRUGO
, show_alarm
, NULL
, 11);
482 static SENSOR_DEVICE_ATTR(fan3_alarm
, S_IRUGO
, show_alarm
, NULL
, 12);
483 static SENSOR_DEVICE_ATTR(fan4_alarm
, S_IRUGO
, show_alarm
, NULL
, 13);
487 static ssize_t
show_pwm(struct device
*dev
, struct device_attribute
*attr
,
490 int nr
= to_sensor_dev_attr(attr
)->index
;
491 struct lm85_data
*data
= lm85_update_device(dev
);
492 return sprintf(buf
, "%d\n", PWM_FROM_REG(data
->pwm
[nr
]));
495 static ssize_t
set_pwm(struct device
*dev
, struct device_attribute
*attr
,
496 const char *buf
, size_t count
)
498 int nr
= to_sensor_dev_attr(attr
)->index
;
499 struct i2c_client
*client
= to_i2c_client(dev
);
500 struct lm85_data
*data
= i2c_get_clientdata(client
);
501 long val
= simple_strtol(buf
, NULL
, 10);
503 mutex_lock(&data
->update_lock
);
504 data
->pwm
[nr
] = PWM_TO_REG(val
);
505 lm85_write_value(client
, LM85_REG_PWM(nr
), data
->pwm
[nr
]);
506 mutex_unlock(&data
->update_lock
);
510 static ssize_t
show_pwm_enable(struct device
*dev
, struct device_attribute
513 int nr
= to_sensor_dev_attr(attr
)->index
;
514 struct lm85_data
*data
= lm85_update_device(dev
);
515 int pwm_zone
, enable
;
517 pwm_zone
= ZONE_FROM_REG(data
->autofan
[nr
].config
);
519 case -1: /* PWM is always at 100% */
522 case 0: /* PWM is always at 0% */
523 case -2: /* PWM responds to manual control */
526 default: /* PWM in automatic mode */
529 return sprintf(buf
, "%d\n", enable
);
532 static ssize_t
set_pwm_enable(struct device
*dev
, struct device_attribute
533 *attr
, const char *buf
, size_t count
)
535 int nr
= to_sensor_dev_attr(attr
)->index
;
536 struct i2c_client
*client
= to_i2c_client(dev
);
537 struct lm85_data
*data
= i2c_get_clientdata(client
);
538 long val
= simple_strtol(buf
, NULL
, 10);
549 /* Here we have to choose arbitrarily one of the 5 possible
550 configurations; I go for the safest */
557 mutex_lock(&data
->update_lock
);
558 data
->autofan
[nr
].config
= lm85_read_value(client
,
559 LM85_REG_AFAN_CONFIG(nr
));
560 data
->autofan
[nr
].config
= (data
->autofan
[nr
].config
& ~0xe0)
562 lm85_write_value(client
, LM85_REG_AFAN_CONFIG(nr
),
563 data
->autofan
[nr
].config
);
564 mutex_unlock(&data
->update_lock
);
568 static ssize_t
show_pwm_freq(struct device
*dev
,
569 struct device_attribute
*attr
, char *buf
)
571 int nr
= to_sensor_dev_attr(attr
)->index
;
572 struct lm85_data
*data
= lm85_update_device(dev
);
575 if (IS_ADT7468_HFPWM(data
))
578 freq
= FREQ_FROM_REG(data
->freq_map
, data
->pwm_freq
[nr
]);
580 return sprintf(buf
, "%d\n", freq
);
583 static ssize_t
set_pwm_freq(struct device
*dev
,
584 struct device_attribute
*attr
, const char *buf
, size_t count
)
586 int nr
= to_sensor_dev_attr(attr
)->index
;
587 struct i2c_client
*client
= to_i2c_client(dev
);
588 struct lm85_data
*data
= i2c_get_clientdata(client
);
589 long val
= simple_strtol(buf
, NULL
, 10);
591 mutex_lock(&data
->update_lock
);
592 /* The ADT7468 has a special high-frequency PWM output mode,
593 * where all PWM outputs are driven by a 22.5 kHz clock.
594 * This might confuse the user, but there's not much we can do. */
595 if (data
->type
== adt7468
&& val
>= 11300) { /* High freq. mode */
596 data
->cfg5
&= ~ADT7468_HFPWM
;
597 lm85_write_value(client
, ADT7468_REG_CFG5
, data
->cfg5
);
598 } else { /* Low freq. mode */
599 data
->pwm_freq
[nr
] = FREQ_TO_REG(data
->freq_map
, val
);
600 lm85_write_value(client
, LM85_REG_AFAN_RANGE(nr
),
601 (data
->zone
[nr
].range
<< 4)
602 | data
->pwm_freq
[nr
]);
603 if (data
->type
== adt7468
) {
604 data
->cfg5
|= ADT7468_HFPWM
;
605 lm85_write_value(client
, ADT7468_REG_CFG5
, data
->cfg5
);
608 mutex_unlock(&data
->update_lock
);
612 #define show_pwm_reg(offset) \
613 static SENSOR_DEVICE_ATTR(pwm##offset, S_IRUGO | S_IWUSR, \
614 show_pwm, set_pwm, offset - 1); \
615 static SENSOR_DEVICE_ATTR(pwm##offset##_enable, S_IRUGO | S_IWUSR, \
616 show_pwm_enable, set_pwm_enable, offset - 1); \
617 static SENSOR_DEVICE_ATTR(pwm##offset##_freq, S_IRUGO | S_IWUSR, \
618 show_pwm_freq, set_pwm_freq, offset - 1)
626 static ssize_t
show_in(struct device
*dev
, struct device_attribute
*attr
,
629 int nr
= to_sensor_dev_attr(attr
)->index
;
630 struct lm85_data
*data
= lm85_update_device(dev
);
631 return sprintf(buf
, "%d\n", INSEXT_FROM_REG(nr
, data
->in
[nr
],
635 static ssize_t
show_in_min(struct device
*dev
, struct device_attribute
*attr
,
638 int nr
= to_sensor_dev_attr(attr
)->index
;
639 struct lm85_data
*data
= lm85_update_device(dev
);
640 return sprintf(buf
, "%d\n", INS_FROM_REG(nr
, data
->in_min
[nr
]));
643 static ssize_t
set_in_min(struct device
*dev
, struct device_attribute
*attr
,
644 const char *buf
, size_t count
)
646 int nr
= to_sensor_dev_attr(attr
)->index
;
647 struct i2c_client
*client
= to_i2c_client(dev
);
648 struct lm85_data
*data
= i2c_get_clientdata(client
);
649 long val
= simple_strtol(buf
, NULL
, 10);
651 mutex_lock(&data
->update_lock
);
652 data
->in_min
[nr
] = INS_TO_REG(nr
, val
);
653 lm85_write_value(client
, LM85_REG_IN_MIN(nr
), data
->in_min
[nr
]);
654 mutex_unlock(&data
->update_lock
);
658 static ssize_t
show_in_max(struct device
*dev
, struct device_attribute
*attr
,
661 int nr
= to_sensor_dev_attr(attr
)->index
;
662 struct lm85_data
*data
= lm85_update_device(dev
);
663 return sprintf(buf
, "%d\n", INS_FROM_REG(nr
, data
->in_max
[nr
]));
666 static ssize_t
set_in_max(struct device
*dev
, struct device_attribute
*attr
,
667 const char *buf
, size_t count
)
669 int nr
= to_sensor_dev_attr(attr
)->index
;
670 struct i2c_client
*client
= to_i2c_client(dev
);
671 struct lm85_data
*data
= i2c_get_clientdata(client
);
672 long val
= simple_strtol(buf
, NULL
, 10);
674 mutex_lock(&data
->update_lock
);
675 data
->in_max
[nr
] = INS_TO_REG(nr
, val
);
676 lm85_write_value(client
, LM85_REG_IN_MAX(nr
), data
->in_max
[nr
]);
677 mutex_unlock(&data
->update_lock
);
681 #define show_in_reg(offset) \
682 static SENSOR_DEVICE_ATTR(in##offset##_input, S_IRUGO, \
683 show_in, NULL, offset); \
684 static SENSOR_DEVICE_ATTR(in##offset##_min, S_IRUGO | S_IWUSR, \
685 show_in_min, set_in_min, offset); \
686 static SENSOR_DEVICE_ATTR(in##offset##_max, S_IRUGO | S_IWUSR, \
687 show_in_max, set_in_max, offset)
700 static ssize_t
show_temp(struct device
*dev
, struct device_attribute
*attr
,
703 int nr
= to_sensor_dev_attr(attr
)->index
;
704 struct lm85_data
*data
= lm85_update_device(dev
);
705 return sprintf(buf
, "%d\n", TEMPEXT_FROM_REG(data
->temp
[nr
],
706 data
->temp_ext
[nr
]));
709 static ssize_t
show_temp_min(struct device
*dev
, struct device_attribute
*attr
,
712 int nr
= to_sensor_dev_attr(attr
)->index
;
713 struct lm85_data
*data
= lm85_update_device(dev
);
714 return sprintf(buf
, "%d\n", TEMP_FROM_REG(data
->temp_min
[nr
]));
717 static ssize_t
set_temp_min(struct device
*dev
, struct device_attribute
*attr
,
718 const char *buf
, size_t count
)
720 int nr
= to_sensor_dev_attr(attr
)->index
;
721 struct i2c_client
*client
= to_i2c_client(dev
);
722 struct lm85_data
*data
= i2c_get_clientdata(client
);
723 long val
= simple_strtol(buf
, NULL
, 10);
725 if (IS_ADT7468_OFF64(data
))
728 mutex_lock(&data
->update_lock
);
729 data
->temp_min
[nr
] = TEMP_TO_REG(val
);
730 lm85_write_value(client
, LM85_REG_TEMP_MIN(nr
), data
->temp_min
[nr
]);
731 mutex_unlock(&data
->update_lock
);
735 static ssize_t
show_temp_max(struct device
*dev
, struct device_attribute
*attr
,
738 int nr
= to_sensor_dev_attr(attr
)->index
;
739 struct lm85_data
*data
= lm85_update_device(dev
);
740 return sprintf(buf
, "%d\n", TEMP_FROM_REG(data
->temp_max
[nr
]));
743 static ssize_t
set_temp_max(struct device
*dev
, struct device_attribute
*attr
,
744 const char *buf
, size_t count
)
746 int nr
= to_sensor_dev_attr(attr
)->index
;
747 struct i2c_client
*client
= to_i2c_client(dev
);
748 struct lm85_data
*data
= i2c_get_clientdata(client
);
749 long val
= simple_strtol(buf
, NULL
, 10);
751 if (IS_ADT7468_OFF64(data
))
754 mutex_lock(&data
->update_lock
);
755 data
->temp_max
[nr
] = TEMP_TO_REG(val
);
756 lm85_write_value(client
, LM85_REG_TEMP_MAX(nr
), data
->temp_max
[nr
]);
757 mutex_unlock(&data
->update_lock
);
761 #define show_temp_reg(offset) \
762 static SENSOR_DEVICE_ATTR(temp##offset##_input, S_IRUGO, \
763 show_temp, NULL, offset - 1); \
764 static SENSOR_DEVICE_ATTR(temp##offset##_min, S_IRUGO | S_IWUSR, \
765 show_temp_min, set_temp_min, offset - 1); \
766 static SENSOR_DEVICE_ATTR(temp##offset##_max, S_IRUGO | S_IWUSR, \
767 show_temp_max, set_temp_max, offset - 1);
774 /* Automatic PWM control */
776 static ssize_t
show_pwm_auto_channels(struct device
*dev
,
777 struct device_attribute
*attr
, char *buf
)
779 int nr
= to_sensor_dev_attr(attr
)->index
;
780 struct lm85_data
*data
= lm85_update_device(dev
);
781 return sprintf(buf
, "%d\n", ZONE_FROM_REG(data
->autofan
[nr
].config
));
784 static ssize_t
set_pwm_auto_channels(struct device
*dev
,
785 struct device_attribute
*attr
, const char *buf
, size_t count
)
787 int nr
= to_sensor_dev_attr(attr
)->index
;
788 struct i2c_client
*client
= to_i2c_client(dev
);
789 struct lm85_data
*data
= i2c_get_clientdata(client
);
790 long val
= simple_strtol(buf
, NULL
, 10);
792 mutex_lock(&data
->update_lock
);
793 data
->autofan
[nr
].config
= (data
->autofan
[nr
].config
& (~0xe0))
795 lm85_write_value(client
, LM85_REG_AFAN_CONFIG(nr
),
796 data
->autofan
[nr
].config
);
797 mutex_unlock(&data
->update_lock
);
801 static ssize_t
show_pwm_auto_pwm_min(struct device
*dev
,
802 struct device_attribute
*attr
, char *buf
)
804 int nr
= to_sensor_dev_attr(attr
)->index
;
805 struct lm85_data
*data
= lm85_update_device(dev
);
806 return sprintf(buf
, "%d\n", PWM_FROM_REG(data
->autofan
[nr
].min_pwm
));
809 static ssize_t
set_pwm_auto_pwm_min(struct device
*dev
,
810 struct device_attribute
*attr
, const char *buf
, size_t count
)
812 int nr
= to_sensor_dev_attr(attr
)->index
;
813 struct i2c_client
*client
= to_i2c_client(dev
);
814 struct lm85_data
*data
= i2c_get_clientdata(client
);
815 long val
= simple_strtol(buf
, NULL
, 10);
817 mutex_lock(&data
->update_lock
);
818 data
->autofan
[nr
].min_pwm
= PWM_TO_REG(val
);
819 lm85_write_value(client
, LM85_REG_AFAN_MINPWM(nr
),
820 data
->autofan
[nr
].min_pwm
);
821 mutex_unlock(&data
->update_lock
);
825 static ssize_t
show_pwm_auto_pwm_minctl(struct device
*dev
,
826 struct device_attribute
*attr
, char *buf
)
828 int nr
= to_sensor_dev_attr(attr
)->index
;
829 struct lm85_data
*data
= lm85_update_device(dev
);
830 return sprintf(buf
, "%d\n", data
->autofan
[nr
].min_off
);
833 static ssize_t
set_pwm_auto_pwm_minctl(struct device
*dev
,
834 struct device_attribute
*attr
, const char *buf
, size_t count
)
836 int nr
= to_sensor_dev_attr(attr
)->index
;
837 struct i2c_client
*client
= to_i2c_client(dev
);
838 struct lm85_data
*data
= i2c_get_clientdata(client
);
839 long val
= simple_strtol(buf
, NULL
, 10);
842 mutex_lock(&data
->update_lock
);
843 data
->autofan
[nr
].min_off
= val
;
844 tmp
= lm85_read_value(client
, LM85_REG_AFAN_SPIKE1
);
845 tmp
&= ~(0x20 << nr
);
846 if (data
->autofan
[nr
].min_off
)
848 lm85_write_value(client
, LM85_REG_AFAN_SPIKE1
, tmp
);
849 mutex_unlock(&data
->update_lock
);
853 #define pwm_auto(offset) \
854 static SENSOR_DEVICE_ATTR(pwm##offset##_auto_channels, \
855 S_IRUGO | S_IWUSR, show_pwm_auto_channels, \
856 set_pwm_auto_channels, offset - 1); \
857 static SENSOR_DEVICE_ATTR(pwm##offset##_auto_pwm_min, \
858 S_IRUGO | S_IWUSR, show_pwm_auto_pwm_min, \
859 set_pwm_auto_pwm_min, offset - 1); \
860 static SENSOR_DEVICE_ATTR(pwm##offset##_auto_pwm_minctl, \
861 S_IRUGO | S_IWUSR, show_pwm_auto_pwm_minctl, \
862 set_pwm_auto_pwm_minctl, offset - 1)
868 /* Temperature settings for automatic PWM control */
870 static ssize_t
show_temp_auto_temp_off(struct device
*dev
,
871 struct device_attribute
*attr
, char *buf
)
873 int nr
= to_sensor_dev_attr(attr
)->index
;
874 struct lm85_data
*data
= lm85_update_device(dev
);
875 return sprintf(buf
, "%d\n", TEMP_FROM_REG(data
->zone
[nr
].limit
) -
876 HYST_FROM_REG(data
->zone
[nr
].hyst
));
879 static ssize_t
set_temp_auto_temp_off(struct device
*dev
,
880 struct device_attribute
*attr
, const char *buf
, size_t count
)
882 int nr
= to_sensor_dev_attr(attr
)->index
;
883 struct i2c_client
*client
= to_i2c_client(dev
);
884 struct lm85_data
*data
= i2c_get_clientdata(client
);
886 long val
= simple_strtol(buf
, NULL
, 10);
888 mutex_lock(&data
->update_lock
);
889 min
= TEMP_FROM_REG(data
->zone
[nr
].limit
);
890 data
->zone
[nr
].off_desired
= TEMP_TO_REG(val
);
891 data
->zone
[nr
].hyst
= HYST_TO_REG(min
- val
);
892 if (nr
== 0 || nr
== 1) {
893 lm85_write_value(client
, LM85_REG_AFAN_HYST1
,
894 (data
->zone
[0].hyst
<< 4)
895 | data
->zone
[1].hyst
);
897 lm85_write_value(client
, LM85_REG_AFAN_HYST2
,
898 (data
->zone
[2].hyst
<< 4));
900 mutex_unlock(&data
->update_lock
);
904 static ssize_t
show_temp_auto_temp_min(struct device
*dev
,
905 struct device_attribute
*attr
, char *buf
)
907 int nr
= to_sensor_dev_attr(attr
)->index
;
908 struct lm85_data
*data
= lm85_update_device(dev
);
909 return sprintf(buf
, "%d\n", TEMP_FROM_REG(data
->zone
[nr
].limit
));
912 static ssize_t
set_temp_auto_temp_min(struct device
*dev
,
913 struct device_attribute
*attr
, const char *buf
, size_t count
)
915 int nr
= to_sensor_dev_attr(attr
)->index
;
916 struct i2c_client
*client
= to_i2c_client(dev
);
917 struct lm85_data
*data
= i2c_get_clientdata(client
);
918 long val
= simple_strtol(buf
, NULL
, 10);
920 mutex_lock(&data
->update_lock
);
921 data
->zone
[nr
].limit
= TEMP_TO_REG(val
);
922 lm85_write_value(client
, LM85_REG_AFAN_LIMIT(nr
),
923 data
->zone
[nr
].limit
);
925 /* Update temp_auto_max and temp_auto_range */
926 data
->zone
[nr
].range
= RANGE_TO_REG(
927 TEMP_FROM_REG(data
->zone
[nr
].max_desired
) -
928 TEMP_FROM_REG(data
->zone
[nr
].limit
));
929 lm85_write_value(client
, LM85_REG_AFAN_RANGE(nr
),
930 ((data
->zone
[nr
].range
& 0x0f) << 4)
931 | (data
->pwm_freq
[nr
] & 0x07));
933 /* Update temp_auto_hyst and temp_auto_off */
934 data
->zone
[nr
].hyst
= HYST_TO_REG(TEMP_FROM_REG(
935 data
->zone
[nr
].limit
) - TEMP_FROM_REG(
936 data
->zone
[nr
].off_desired
));
937 if (nr
== 0 || nr
== 1) {
938 lm85_write_value(client
, LM85_REG_AFAN_HYST1
,
939 (data
->zone
[0].hyst
<< 4)
940 | data
->zone
[1].hyst
);
942 lm85_write_value(client
, LM85_REG_AFAN_HYST2
,
943 (data
->zone
[2].hyst
<< 4));
945 mutex_unlock(&data
->update_lock
);
949 static ssize_t
show_temp_auto_temp_max(struct device
*dev
,
950 struct device_attribute
*attr
, char *buf
)
952 int nr
= to_sensor_dev_attr(attr
)->index
;
953 struct lm85_data
*data
= lm85_update_device(dev
);
954 return sprintf(buf
, "%d\n", TEMP_FROM_REG(data
->zone
[nr
].limit
) +
955 RANGE_FROM_REG(data
->zone
[nr
].range
));
958 static ssize_t
set_temp_auto_temp_max(struct device
*dev
,
959 struct device_attribute
*attr
, const char *buf
, size_t count
)
961 int nr
= to_sensor_dev_attr(attr
)->index
;
962 struct i2c_client
*client
= to_i2c_client(dev
);
963 struct lm85_data
*data
= i2c_get_clientdata(client
);
965 long val
= simple_strtol(buf
, NULL
, 10);
967 mutex_lock(&data
->update_lock
);
968 min
= TEMP_FROM_REG(data
->zone
[nr
].limit
);
969 data
->zone
[nr
].max_desired
= TEMP_TO_REG(val
);
970 data
->zone
[nr
].range
= RANGE_TO_REG(
972 lm85_write_value(client
, LM85_REG_AFAN_RANGE(nr
),
973 ((data
->zone
[nr
].range
& 0x0f) << 4)
974 | (data
->pwm_freq
[nr
] & 0x07));
975 mutex_unlock(&data
->update_lock
);
979 static ssize_t
show_temp_auto_temp_crit(struct device
*dev
,
980 struct device_attribute
*attr
, char *buf
)
982 int nr
= to_sensor_dev_attr(attr
)->index
;
983 struct lm85_data
*data
= lm85_update_device(dev
);
984 return sprintf(buf
, "%d\n", TEMP_FROM_REG(data
->zone
[nr
].critical
));
987 static ssize_t
set_temp_auto_temp_crit(struct device
*dev
,
988 struct device_attribute
*attr
, const char *buf
, size_t count
)
990 int nr
= to_sensor_dev_attr(attr
)->index
;
991 struct i2c_client
*client
= to_i2c_client(dev
);
992 struct lm85_data
*data
= i2c_get_clientdata(client
);
993 long val
= simple_strtol(buf
, NULL
, 10);
995 mutex_lock(&data
->update_lock
);
996 data
->zone
[nr
].critical
= TEMP_TO_REG(val
);
997 lm85_write_value(client
, LM85_REG_AFAN_CRITICAL(nr
),
998 data
->zone
[nr
].critical
);
999 mutex_unlock(&data
->update_lock
);
1003 #define temp_auto(offset) \
1004 static SENSOR_DEVICE_ATTR(temp##offset##_auto_temp_off, \
1005 S_IRUGO | S_IWUSR, show_temp_auto_temp_off, \
1006 set_temp_auto_temp_off, offset - 1); \
1007 static SENSOR_DEVICE_ATTR(temp##offset##_auto_temp_min, \
1008 S_IRUGO | S_IWUSR, show_temp_auto_temp_min, \
1009 set_temp_auto_temp_min, offset - 1); \
1010 static SENSOR_DEVICE_ATTR(temp##offset##_auto_temp_max, \
1011 S_IRUGO | S_IWUSR, show_temp_auto_temp_max, \
1012 set_temp_auto_temp_max, offset - 1); \
1013 static SENSOR_DEVICE_ATTR(temp##offset##_auto_temp_crit, \
1014 S_IRUGO | S_IWUSR, show_temp_auto_temp_crit, \
1015 set_temp_auto_temp_crit, offset - 1);
1021 static struct attribute
*lm85_attributes
[] = {
1022 &sensor_dev_attr_fan1_input
.dev_attr
.attr
,
1023 &sensor_dev_attr_fan2_input
.dev_attr
.attr
,
1024 &sensor_dev_attr_fan3_input
.dev_attr
.attr
,
1025 &sensor_dev_attr_fan4_input
.dev_attr
.attr
,
1026 &sensor_dev_attr_fan1_min
.dev_attr
.attr
,
1027 &sensor_dev_attr_fan2_min
.dev_attr
.attr
,
1028 &sensor_dev_attr_fan3_min
.dev_attr
.attr
,
1029 &sensor_dev_attr_fan4_min
.dev_attr
.attr
,
1030 &sensor_dev_attr_fan1_alarm
.dev_attr
.attr
,
1031 &sensor_dev_attr_fan2_alarm
.dev_attr
.attr
,
1032 &sensor_dev_attr_fan3_alarm
.dev_attr
.attr
,
1033 &sensor_dev_attr_fan4_alarm
.dev_attr
.attr
,
1035 &sensor_dev_attr_pwm1
.dev_attr
.attr
,
1036 &sensor_dev_attr_pwm2
.dev_attr
.attr
,
1037 &sensor_dev_attr_pwm3
.dev_attr
.attr
,
1038 &sensor_dev_attr_pwm1_enable
.dev_attr
.attr
,
1039 &sensor_dev_attr_pwm2_enable
.dev_attr
.attr
,
1040 &sensor_dev_attr_pwm3_enable
.dev_attr
.attr
,
1041 &sensor_dev_attr_pwm1_freq
.dev_attr
.attr
,
1042 &sensor_dev_attr_pwm2_freq
.dev_attr
.attr
,
1043 &sensor_dev_attr_pwm3_freq
.dev_attr
.attr
,
1045 &sensor_dev_attr_in0_input
.dev_attr
.attr
,
1046 &sensor_dev_attr_in1_input
.dev_attr
.attr
,
1047 &sensor_dev_attr_in2_input
.dev_attr
.attr
,
1048 &sensor_dev_attr_in3_input
.dev_attr
.attr
,
1049 &sensor_dev_attr_in0_min
.dev_attr
.attr
,
1050 &sensor_dev_attr_in1_min
.dev_attr
.attr
,
1051 &sensor_dev_attr_in2_min
.dev_attr
.attr
,
1052 &sensor_dev_attr_in3_min
.dev_attr
.attr
,
1053 &sensor_dev_attr_in0_max
.dev_attr
.attr
,
1054 &sensor_dev_attr_in1_max
.dev_attr
.attr
,
1055 &sensor_dev_attr_in2_max
.dev_attr
.attr
,
1056 &sensor_dev_attr_in3_max
.dev_attr
.attr
,
1057 &sensor_dev_attr_in0_alarm
.dev_attr
.attr
,
1058 &sensor_dev_attr_in1_alarm
.dev_attr
.attr
,
1059 &sensor_dev_attr_in2_alarm
.dev_attr
.attr
,
1060 &sensor_dev_attr_in3_alarm
.dev_attr
.attr
,
1062 &sensor_dev_attr_temp1_input
.dev_attr
.attr
,
1063 &sensor_dev_attr_temp2_input
.dev_attr
.attr
,
1064 &sensor_dev_attr_temp3_input
.dev_attr
.attr
,
1065 &sensor_dev_attr_temp1_min
.dev_attr
.attr
,
1066 &sensor_dev_attr_temp2_min
.dev_attr
.attr
,
1067 &sensor_dev_attr_temp3_min
.dev_attr
.attr
,
1068 &sensor_dev_attr_temp1_max
.dev_attr
.attr
,
1069 &sensor_dev_attr_temp2_max
.dev_attr
.attr
,
1070 &sensor_dev_attr_temp3_max
.dev_attr
.attr
,
1071 &sensor_dev_attr_temp1_alarm
.dev_attr
.attr
,
1072 &sensor_dev_attr_temp2_alarm
.dev_attr
.attr
,
1073 &sensor_dev_attr_temp3_alarm
.dev_attr
.attr
,
1074 &sensor_dev_attr_temp1_fault
.dev_attr
.attr
,
1075 &sensor_dev_attr_temp3_fault
.dev_attr
.attr
,
1077 &sensor_dev_attr_pwm1_auto_channels
.dev_attr
.attr
,
1078 &sensor_dev_attr_pwm2_auto_channels
.dev_attr
.attr
,
1079 &sensor_dev_attr_pwm3_auto_channels
.dev_attr
.attr
,
1080 &sensor_dev_attr_pwm1_auto_pwm_min
.dev_attr
.attr
,
1081 &sensor_dev_attr_pwm2_auto_pwm_min
.dev_attr
.attr
,
1082 &sensor_dev_attr_pwm3_auto_pwm_min
.dev_attr
.attr
,
1083 &sensor_dev_attr_pwm1_auto_pwm_minctl
.dev_attr
.attr
,
1084 &sensor_dev_attr_pwm2_auto_pwm_minctl
.dev_attr
.attr
,
1085 &sensor_dev_attr_pwm3_auto_pwm_minctl
.dev_attr
.attr
,
1087 &sensor_dev_attr_temp1_auto_temp_off
.dev_attr
.attr
,
1088 &sensor_dev_attr_temp2_auto_temp_off
.dev_attr
.attr
,
1089 &sensor_dev_attr_temp3_auto_temp_off
.dev_attr
.attr
,
1090 &sensor_dev_attr_temp1_auto_temp_min
.dev_attr
.attr
,
1091 &sensor_dev_attr_temp2_auto_temp_min
.dev_attr
.attr
,
1092 &sensor_dev_attr_temp3_auto_temp_min
.dev_attr
.attr
,
1093 &sensor_dev_attr_temp1_auto_temp_max
.dev_attr
.attr
,
1094 &sensor_dev_attr_temp2_auto_temp_max
.dev_attr
.attr
,
1095 &sensor_dev_attr_temp3_auto_temp_max
.dev_attr
.attr
,
1096 &sensor_dev_attr_temp1_auto_temp_crit
.dev_attr
.attr
,
1097 &sensor_dev_attr_temp2_auto_temp_crit
.dev_attr
.attr
,
1098 &sensor_dev_attr_temp3_auto_temp_crit
.dev_attr
.attr
,
1101 &dev_attr_cpu0_vid
.attr
,
1102 &dev_attr_alarms
.attr
,
1106 static const struct attribute_group lm85_group
= {
1107 .attrs
= lm85_attributes
,
1110 static struct attribute
*lm85_attributes_in4
[] = {
1111 &sensor_dev_attr_in4_input
.dev_attr
.attr
,
1112 &sensor_dev_attr_in4_min
.dev_attr
.attr
,
1113 &sensor_dev_attr_in4_max
.dev_attr
.attr
,
1114 &sensor_dev_attr_in4_alarm
.dev_attr
.attr
,
1118 static const struct attribute_group lm85_group_in4
= {
1119 .attrs
= lm85_attributes_in4
,
1122 static struct attribute
*lm85_attributes_in567
[] = {
1123 &sensor_dev_attr_in5_input
.dev_attr
.attr
,
1124 &sensor_dev_attr_in6_input
.dev_attr
.attr
,
1125 &sensor_dev_attr_in7_input
.dev_attr
.attr
,
1126 &sensor_dev_attr_in5_min
.dev_attr
.attr
,
1127 &sensor_dev_attr_in6_min
.dev_attr
.attr
,
1128 &sensor_dev_attr_in7_min
.dev_attr
.attr
,
1129 &sensor_dev_attr_in5_max
.dev_attr
.attr
,
1130 &sensor_dev_attr_in6_max
.dev_attr
.attr
,
1131 &sensor_dev_attr_in7_max
.dev_attr
.attr
,
1132 &sensor_dev_attr_in5_alarm
.dev_attr
.attr
,
1133 &sensor_dev_attr_in6_alarm
.dev_attr
.attr
,
1134 &sensor_dev_attr_in7_alarm
.dev_attr
.attr
,
1138 static const struct attribute_group lm85_group_in567
= {
1139 .attrs
= lm85_attributes_in567
,
1142 static void lm85_init_client(struct i2c_client
*client
)
1146 /* Start monitoring if needed */
1147 value
= lm85_read_value(client
, LM85_REG_CONFIG
);
1148 if (!(value
& 0x01)) {
1149 dev_info(&client
->dev
, "Starting monitoring\n");
1150 lm85_write_value(client
, LM85_REG_CONFIG
, value
| 0x01);
1153 /* Warn about unusual configuration bits */
1155 dev_warn(&client
->dev
, "Device configuration is locked\n");
1156 if (!(value
& 0x04))
1157 dev_warn(&client
->dev
, "Device is not ready\n");
1160 static int lm85_is_fake(struct i2c_client
*client
)
1163 * Differenciate between real LM96000 and Winbond WPCD377I. The latter
1164 * emulate the former except that it has no hardware monitoring function
1165 * so the readings are always 0.
1170 for (i
= 0; i
< 8; i
++) {
1171 in_temp
= i2c_smbus_read_byte_data(client
, 0x20 + i
);
1172 fan
= i2c_smbus_read_byte_data(client
, 0x28 + i
);
1173 if (in_temp
!= 0x00 || fan
!= 0xff)
1180 /* Return 0 if detection is successful, -ENODEV otherwise */
1181 static int lm85_detect(struct i2c_client
*client
, struct i2c_board_info
*info
)
1183 struct i2c_adapter
*adapter
= client
->adapter
;
1184 int address
= client
->addr
;
1185 const char *type_name
;
1186 int company
, verstep
;
1188 if (!i2c_check_functionality(adapter
, I2C_FUNC_SMBUS_BYTE_DATA
)) {
1189 /* We need to be able to do byte I/O */
1193 /* Determine the chip type */
1194 company
= lm85_read_value(client
, LM85_REG_COMPANY
);
1195 verstep
= lm85_read_value(client
, LM85_REG_VERSTEP
);
1197 dev_dbg(&adapter
->dev
, "Detecting device at 0x%02x with "
1198 "COMPANY: 0x%02x and VERSTEP: 0x%02x\n",
1199 address
, company
, verstep
);
1201 /* All supported chips have the version in common */
1202 if ((verstep
& LM85_VERSTEP_VMASK
) != LM85_VERSTEP_GENERIC
&&
1203 (verstep
& LM85_VERSTEP_VMASK
) != LM85_VERSTEP_GENERIC2
) {
1204 dev_dbg(&adapter
->dev
,
1205 "Autodetection failed: unsupported version\n");
1210 /* Now, refine the detection */
1211 if (company
== LM85_COMPANY_NATIONAL
) {
1213 case LM85_VERSTEP_LM85C
:
1214 type_name
= "lm85c";
1216 case LM85_VERSTEP_LM85B
:
1217 type_name
= "lm85b";
1219 case LM85_VERSTEP_LM96000_1
:
1220 case LM85_VERSTEP_LM96000_2
:
1221 /* Check for Winbond WPCD377I */
1222 if (lm85_is_fake(client
)) {
1223 dev_dbg(&adapter
->dev
,
1224 "Found Winbond WPCD377I, ignoring\n");
1229 } else if (company
== LM85_COMPANY_ANALOG_DEV
) {
1231 case LM85_VERSTEP_ADM1027
:
1232 type_name
= "adm1027";
1234 case LM85_VERSTEP_ADT7463
:
1235 case LM85_VERSTEP_ADT7463C
:
1236 type_name
= "adt7463";
1238 case LM85_VERSTEP_ADT7468_1
:
1239 case LM85_VERSTEP_ADT7468_2
:
1240 type_name
= "adt7468";
1243 } else if (company
== LM85_COMPANY_SMSC
) {
1245 case LM85_VERSTEP_EMC6D100_A0
:
1246 case LM85_VERSTEP_EMC6D100_A1
:
1247 /* Note: we can't tell a '100 from a '101 */
1248 type_name
= "emc6d100";
1250 case LM85_VERSTEP_EMC6D102
:
1251 type_name
= "emc6d102";
1255 dev_dbg(&adapter
->dev
,
1256 "Autodetection failed: unknown vendor\n");
1260 strlcpy(info
->type
, type_name
, I2C_NAME_SIZE
);
1265 static int lm85_probe(struct i2c_client
*client
,
1266 const struct i2c_device_id
*id
)
1268 struct lm85_data
*data
;
1271 data
= kzalloc(sizeof(struct lm85_data
), GFP_KERNEL
);
1275 i2c_set_clientdata(client
, data
);
1276 data
->type
= id
->driver_data
;
1277 mutex_init(&data
->update_lock
);
1279 /* Fill in the chip specific driver values */
1280 switch (data
->type
) {
1286 data
->freq_map
= adm1027_freq_map
;
1289 data
->freq_map
= lm85_freq_map
;
1292 /* Set the VRM version */
1293 data
->vrm
= vid_which_vrm();
1295 /* Initialize the LM85 chip */
1296 lm85_init_client(client
);
1298 /* Register sysfs hooks */
1299 err
= sysfs_create_group(&client
->dev
.kobj
, &lm85_group
);
1303 /* The ADT7463/68 have an optional VRM 10 mode where pin 21 is used
1304 as a sixth digital VID input rather than an analog input. */
1305 data
->vid
= lm85_read_value(client
, LM85_REG_VID
);
1306 if (!((data
->type
== adt7463
|| data
->type
== adt7468
) &&
1307 (data
->vid
& 0x80)))
1308 if ((err
= sysfs_create_group(&client
->dev
.kobj
,
1310 goto err_remove_files
;
1312 /* The EMC6D100 has 3 additional voltage inputs */
1313 if (data
->type
== emc6d100
)
1314 if ((err
= sysfs_create_group(&client
->dev
.kobj
,
1315 &lm85_group_in567
)))
1316 goto err_remove_files
;
1318 data
->hwmon_dev
= hwmon_device_register(&client
->dev
);
1319 if (IS_ERR(data
->hwmon_dev
)) {
1320 err
= PTR_ERR(data
->hwmon_dev
);
1321 goto err_remove_files
;
1326 /* Error out and cleanup code */
1328 sysfs_remove_group(&client
->dev
.kobj
, &lm85_group
);
1329 sysfs_remove_group(&client
->dev
.kobj
, &lm85_group_in4
);
1330 if (data
->type
== emc6d100
)
1331 sysfs_remove_group(&client
->dev
.kobj
, &lm85_group_in567
);
1337 static int lm85_remove(struct i2c_client
*client
)
1339 struct lm85_data
*data
= i2c_get_clientdata(client
);
1340 hwmon_device_unregister(data
->hwmon_dev
);
1341 sysfs_remove_group(&client
->dev
.kobj
, &lm85_group
);
1342 sysfs_remove_group(&client
->dev
.kobj
, &lm85_group_in4
);
1343 if (data
->type
== emc6d100
)
1344 sysfs_remove_group(&client
->dev
.kobj
, &lm85_group_in567
);
1350 static int lm85_read_value(struct i2c_client
*client
, u8 reg
)
1354 /* What size location is it? */
1356 case LM85_REG_FAN(0): /* Read WORD data */
1357 case LM85_REG_FAN(1):
1358 case LM85_REG_FAN(2):
1359 case LM85_REG_FAN(3):
1360 case LM85_REG_FAN_MIN(0):
1361 case LM85_REG_FAN_MIN(1):
1362 case LM85_REG_FAN_MIN(2):
1363 case LM85_REG_FAN_MIN(3):
1364 case LM85_REG_ALARM1
: /* Read both bytes at once */
1365 res
= i2c_smbus_read_byte_data(client
, reg
) & 0xff;
1366 res
|= i2c_smbus_read_byte_data(client
, reg
+ 1) << 8;
1368 default: /* Read BYTE data */
1369 res
= i2c_smbus_read_byte_data(client
, reg
);
1376 static void lm85_write_value(struct i2c_client
*client
, u8 reg
, int value
)
1379 case LM85_REG_FAN(0): /* Write WORD data */
1380 case LM85_REG_FAN(1):
1381 case LM85_REG_FAN(2):
1382 case LM85_REG_FAN(3):
1383 case LM85_REG_FAN_MIN(0):
1384 case LM85_REG_FAN_MIN(1):
1385 case LM85_REG_FAN_MIN(2):
1386 case LM85_REG_FAN_MIN(3):
1387 /* NOTE: ALARM is read only, so not included here */
1388 i2c_smbus_write_byte_data(client
, reg
, value
& 0xff);
1389 i2c_smbus_write_byte_data(client
, reg
+ 1, value
>> 8);
1391 default: /* Write BYTE data */
1392 i2c_smbus_write_byte_data(client
, reg
, value
);
1397 static struct lm85_data
*lm85_update_device(struct device
*dev
)
1399 struct i2c_client
*client
= to_i2c_client(dev
);
1400 struct lm85_data
*data
= i2c_get_clientdata(client
);
1403 mutex_lock(&data
->update_lock
);
1406 time_after(jiffies
, data
->last_reading
+ LM85_DATA_INTERVAL
)) {
1407 /* Things that change quickly */
1408 dev_dbg(&client
->dev
, "Reading sensor values\n");
1410 /* Have to read extended bits first to "freeze" the
1411 * more significant bits that are read later.
1412 * There are 2 additional resolution bits per channel and we
1413 * have room for 4, so we shift them to the left.
1415 if (data
->type
== adm1027
|| data
->type
== adt7463
||
1416 data
->type
== adt7468
) {
1417 int ext1
= lm85_read_value(client
,
1418 ADM1027_REG_EXTEND_ADC1
);
1419 int ext2
= lm85_read_value(client
,
1420 ADM1027_REG_EXTEND_ADC2
);
1421 int val
= (ext1
<< 8) + ext2
;
1423 for (i
= 0; i
<= 4; i
++)
1425 ((val
>> (i
* 2)) & 0x03) << 2;
1427 for (i
= 0; i
<= 2; i
++)
1429 (val
>> ((i
+ 4) * 2)) & 0x0c;
1432 data
->vid
= lm85_read_value(client
, LM85_REG_VID
);
1434 for (i
= 0; i
<= 3; ++i
) {
1436 lm85_read_value(client
, LM85_REG_IN(i
));
1438 lm85_read_value(client
, LM85_REG_FAN(i
));
1441 if (!((data
->type
== adt7463
|| data
->type
== adt7468
) &&
1442 (data
->vid
& 0x80))) {
1443 data
->in
[4] = lm85_read_value(client
,
1447 if (data
->type
== adt7468
)
1448 data
->cfg5
= lm85_read_value(client
, ADT7468_REG_CFG5
);
1450 for (i
= 0; i
<= 2; ++i
) {
1452 lm85_read_value(client
, LM85_REG_TEMP(i
));
1454 lm85_read_value(client
, LM85_REG_PWM(i
));
1456 if (IS_ADT7468_OFF64(data
))
1457 data
->temp
[i
] -= 64;
1460 data
->alarms
= lm85_read_value(client
, LM85_REG_ALARM1
);
1462 if (data
->type
== emc6d100
) {
1463 /* Three more voltage sensors */
1464 for (i
= 5; i
<= 7; ++i
) {
1465 data
->in
[i
] = lm85_read_value(client
,
1466 EMC6D100_REG_IN(i
));
1468 /* More alarm bits */
1469 data
->alarms
|= lm85_read_value(client
,
1470 EMC6D100_REG_ALARM3
) << 16;
1471 } else if (data
->type
== emc6d102
) {
1472 /* Have to read LSB bits after the MSB ones because
1473 the reading of the MSB bits has frozen the
1474 LSBs (backward from the ADM1027).
1476 int ext1
= lm85_read_value(client
,
1477 EMC6D102_REG_EXTEND_ADC1
);
1478 int ext2
= lm85_read_value(client
,
1479 EMC6D102_REG_EXTEND_ADC2
);
1480 int ext3
= lm85_read_value(client
,
1481 EMC6D102_REG_EXTEND_ADC3
);
1482 int ext4
= lm85_read_value(client
,
1483 EMC6D102_REG_EXTEND_ADC4
);
1484 data
->in_ext
[0] = ext3
& 0x0f;
1485 data
->in_ext
[1] = ext4
& 0x0f;
1486 data
->in_ext
[2] = ext4
>> 4;
1487 data
->in_ext
[3] = ext3
>> 4;
1488 data
->in_ext
[4] = ext2
>> 4;
1490 data
->temp_ext
[0] = ext1
& 0x0f;
1491 data
->temp_ext
[1] = ext2
& 0x0f;
1492 data
->temp_ext
[2] = ext1
>> 4;
1495 data
->last_reading
= jiffies
;
1496 } /* last_reading */
1499 time_after(jiffies
, data
->last_config
+ LM85_CONFIG_INTERVAL
)) {
1500 /* Things that don't change often */
1501 dev_dbg(&client
->dev
, "Reading config values\n");
1503 for (i
= 0; i
<= 3; ++i
) {
1505 lm85_read_value(client
, LM85_REG_IN_MIN(i
));
1507 lm85_read_value(client
, LM85_REG_IN_MAX(i
));
1509 lm85_read_value(client
, LM85_REG_FAN_MIN(i
));
1512 if (!((data
->type
== adt7463
|| data
->type
== adt7468
) &&
1513 (data
->vid
& 0x80))) {
1514 data
->in_min
[4] = lm85_read_value(client
,
1515 LM85_REG_IN_MIN(4));
1516 data
->in_max
[4] = lm85_read_value(client
,
1517 LM85_REG_IN_MAX(4));
1520 if (data
->type
== emc6d100
) {
1521 for (i
= 5; i
<= 7; ++i
) {
1522 data
->in_min
[i
] = lm85_read_value(client
,
1523 EMC6D100_REG_IN_MIN(i
));
1524 data
->in_max
[i
] = lm85_read_value(client
,
1525 EMC6D100_REG_IN_MAX(i
));
1529 for (i
= 0; i
<= 2; ++i
) {
1533 lm85_read_value(client
, LM85_REG_TEMP_MIN(i
));
1535 lm85_read_value(client
, LM85_REG_TEMP_MAX(i
));
1537 data
->autofan
[i
].config
=
1538 lm85_read_value(client
, LM85_REG_AFAN_CONFIG(i
));
1539 val
= lm85_read_value(client
, LM85_REG_AFAN_RANGE(i
));
1540 data
->pwm_freq
[i
] = val
& 0x07;
1541 data
->zone
[i
].range
= val
>> 4;
1542 data
->autofan
[i
].min_pwm
=
1543 lm85_read_value(client
, LM85_REG_AFAN_MINPWM(i
));
1544 data
->zone
[i
].limit
=
1545 lm85_read_value(client
, LM85_REG_AFAN_LIMIT(i
));
1546 data
->zone
[i
].critical
=
1547 lm85_read_value(client
, LM85_REG_AFAN_CRITICAL(i
));
1549 if (IS_ADT7468_OFF64(data
)) {
1550 data
->temp_min
[i
] -= 64;
1551 data
->temp_max
[i
] -= 64;
1552 data
->zone
[i
].limit
-= 64;
1553 data
->zone
[i
].critical
-= 64;
1557 i
= lm85_read_value(client
, LM85_REG_AFAN_SPIKE1
);
1558 data
->autofan
[0].min_off
= (i
& 0x20) != 0;
1559 data
->autofan
[1].min_off
= (i
& 0x40) != 0;
1560 data
->autofan
[2].min_off
= (i
& 0x80) != 0;
1562 i
= lm85_read_value(client
, LM85_REG_AFAN_HYST1
);
1563 data
->zone
[0].hyst
= i
>> 4;
1564 data
->zone
[1].hyst
= i
& 0x0f;
1566 i
= lm85_read_value(client
, LM85_REG_AFAN_HYST2
);
1567 data
->zone
[2].hyst
= i
>> 4;
1569 data
->last_config
= jiffies
;
1574 mutex_unlock(&data
->update_lock
);
1580 static int __init
sm_lm85_init(void)
1582 return i2c_add_driver(&lm85_driver
);
1585 static void __exit
sm_lm85_exit(void)
1587 i2c_del_driver(&lm85_driver
);
1590 MODULE_LICENSE("GPL");
1591 MODULE_AUTHOR("Philip Pokorny <ppokorny@penguincomputing.com>, "
1592 "Margit Schubert-While <margitsw@t-online.de>, "
1593 "Justin Thiessen <jthiessen@penguincomputing.com>");
1594 MODULE_DESCRIPTION("LM85-B, LM85-C driver");
1596 module_init(sm_lm85_init
);
1597 module_exit(sm_lm85_exit
);