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
,
44 emc6d100
, emc6d102
, emc6d103
, emc6d103s
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
93 #define LM85_VERSTEP_EMC6D103_A0 0x68
94 #define LM85_VERSTEP_EMC6D103_A1 0x69
95 #define LM85_VERSTEP_EMC6D103S 0x6A /* Also known as EMC6D103:A2 */
97 #define LM85_REG_CONFIG 0x40
99 #define LM85_REG_ALARM1 0x41
100 #define LM85_REG_ALARM2 0x42
102 #define LM85_REG_VID 0x43
104 /* Automated FAN control */
105 #define LM85_REG_AFAN_CONFIG(nr) (0x5c + (nr))
106 #define LM85_REG_AFAN_RANGE(nr) (0x5f + (nr))
107 #define LM85_REG_AFAN_SPIKE1 0x62
108 #define LM85_REG_AFAN_MINPWM(nr) (0x64 + (nr))
109 #define LM85_REG_AFAN_LIMIT(nr) (0x67 + (nr))
110 #define LM85_REG_AFAN_CRITICAL(nr) (0x6a + (nr))
111 #define LM85_REG_AFAN_HYST1 0x6d
112 #define LM85_REG_AFAN_HYST2 0x6e
114 #define ADM1027_REG_EXTEND_ADC1 0x76
115 #define ADM1027_REG_EXTEND_ADC2 0x77
117 #define EMC6D100_REG_ALARM3 0x7d
118 /* IN5, IN6 and IN7 */
119 #define EMC6D100_REG_IN(nr) (0x70 + ((nr) - 5))
120 #define EMC6D100_REG_IN_MIN(nr) (0x73 + ((nr) - 5) * 2)
121 #define EMC6D100_REG_IN_MAX(nr) (0x74 + ((nr) - 5) * 2)
122 #define EMC6D102_REG_EXTEND_ADC1 0x85
123 #define EMC6D102_REG_EXTEND_ADC2 0x86
124 #define EMC6D102_REG_EXTEND_ADC3 0x87
125 #define EMC6D102_REG_EXTEND_ADC4 0x88
129 * Conversions. Rounding and limit checking is only done on the TO_REG
130 * variants. Note that you should be a bit careful with which arguments
131 * these macros are called: arguments may be evaluated more than once.
134 /* IN are scaled according to built-in resistors */
135 static const int lm85_scaling
[] = { /* .001 Volts */
136 2500, 2250, 3300, 5000, 12000,
137 3300, 1500, 1800 /*EMC6D100*/
139 #define SCALE(val, from, to) (((val) * (to) + ((from) / 2)) / (from))
141 #define INS_TO_REG(n, val) \
142 clamp_val(SCALE(val, lm85_scaling[n], 192), 0, 255)
144 #define INSEXT_FROM_REG(n, val, ext) \
145 SCALE(((val) << 4) + (ext), 192 << 4, lm85_scaling[n])
147 #define INS_FROM_REG(n, val) SCALE((val), 192, lm85_scaling[n])
149 /* FAN speed is measured using 90kHz clock */
150 static inline u16
FAN_TO_REG(unsigned long val
)
154 return clamp_val(5400000 / val
, 1, 0xfffe);
156 #define FAN_FROM_REG(val) ((val) == 0 ? -1 : (val) == 0xffff ? 0 : \
159 /* Temperature is reported in .001 degC increments */
160 #define TEMP_TO_REG(val) \
161 clamp_val(SCALE(val, 1000, 1), -127, 127)
162 #define TEMPEXT_FROM_REG(val, ext) \
163 SCALE(((val) << 4) + (ext), 16, 1000)
164 #define TEMP_FROM_REG(val) ((val) * 1000)
166 #define PWM_TO_REG(val) clamp_val(val, 0, 255)
167 #define PWM_FROM_REG(val) (val)
171 * ZONEs have the following parameters:
172 * Limit (low) temp, 1. degC
173 * Hysteresis (below limit), 1. degC (0-15)
174 * Range of speed control, .1 degC (2-80)
175 * Critical (high) temp, 1. degC
177 * FAN PWMs have the following parameters:
178 * Reference Zone, 1, 2, 3, etc.
179 * Spinup time, .05 sec
180 * PWM value at limit/low temp, 1 count
181 * PWM Frequency, 1. Hz
182 * PWM is Min or OFF below limit, flag
183 * Invert PWM output, flag
185 * Some chips filter the temp, others the fan.
186 * Filter constant (or disabled) .1 seconds
189 /* These are the zone temperature range encodings in .001 degree C */
190 static const int lm85_range_map
[] = {
191 2000, 2500, 3300, 4000, 5000, 6600, 8000, 10000,
192 13300, 16000, 20000, 26600, 32000, 40000, 53300, 80000
195 static int RANGE_TO_REG(int range
)
199 /* Find the closest match */
200 for (i
= 0; i
< 15; ++i
) {
201 if (range
<= (lm85_range_map
[i
] + lm85_range_map
[i
+ 1]) / 2)
207 #define RANGE_FROM_REG(val) lm85_range_map[(val) & 0x0f]
209 /* These are the PWM frequency encodings */
210 static const int lm85_freq_map
[8] = { /* 1 Hz */
211 10, 15, 23, 30, 38, 47, 61, 94
213 static const int adm1027_freq_map
[8] = { /* 1 Hz */
214 11, 15, 22, 29, 35, 44, 59, 88
217 static int FREQ_TO_REG(const int *map
, int freq
)
221 /* Find the closest match */
222 for (i
= 0; i
< 7; ++i
)
223 if (freq
<= (map
[i
] + map
[i
+ 1]) / 2)
228 static int FREQ_FROM_REG(const int *map
, u8 reg
)
230 return map
[reg
& 0x07];
234 * Since we can't use strings, I'm abusing these numbers
235 * to stand in for the following meanings:
236 * 1 -- PWM responds to Zone 1
237 * 2 -- PWM responds to Zone 2
238 * 3 -- PWM responds to Zone 3
239 * 23 -- PWM responds to the higher temp of Zone 2 or 3
240 * 123 -- PWM responds to highest of Zone 1, 2, or 3
241 * 0 -- PWM is always at 0% (ie, off)
242 * -1 -- PWM is always at 100%
243 * -2 -- PWM responds to manual control
246 static const int lm85_zone_map
[] = { 1, 2, 3, -1, 0, 23, 123, -2 };
247 #define ZONE_FROM_REG(val) lm85_zone_map[(val) >> 5]
249 static int ZONE_TO_REG(int zone
)
253 for (i
= 0; i
<= 7; ++i
)
254 if (zone
== lm85_zone_map
[i
])
256 if (i
> 7) /* Not found. */
257 i
= 3; /* Always 100% */
261 #define HYST_TO_REG(val) clamp_val(((val) + 500) / 1000, 0, 15)
262 #define HYST_FROM_REG(val) ((val) * 1000)
265 * Chip sampling rates
267 * Some sensors are not updated more frequently than once per second
268 * so it doesn't make sense to read them more often than that.
269 * We cache the results and return the saved data if the driver
270 * is called again before a second has elapsed.
272 * Also, there is significant configuration data for this chip
273 * given the automatic PWM fan control that is possible. There
274 * are about 47 bytes of config data to only 22 bytes of actual
275 * readings. So, we keep the config data up to date in the cache
276 * when it is written and only sample it once every 1 *minute*
278 #define LM85_DATA_INTERVAL (HZ + HZ / 2)
279 #define LM85_CONFIG_INTERVAL (1 * 60 * HZ)
282 * LM85 can automatically adjust fan speeds based on temperature
283 * This structure encapsulates an entire Zone config. There are
284 * three zones (one for each temperature input) on the lm85
287 s8 limit
; /* Low temp limit */
288 u8 hyst
; /* Low limit hysteresis. (0-15) */
289 u8 range
; /* Temp range, encoded */
290 s8 critical
; /* "All fans ON" temp limit */
292 * Actual "max" temperature specified. Preserved
293 * to prevent "drift" as other autofan control
298 struct lm85_autofan
{
299 u8 config
; /* Register value */
300 u8 min_pwm
; /* Minimum PWM value, encoded */
301 u8 min_off
; /* Min PWM or OFF below "limit", flag */
305 * For each registered chip, we need to keep some data in memory.
306 * The structure is dynamically allocated.
309 struct device
*hwmon_dev
;
313 bool has_vid5
; /* true if VID5 is configured for ADT7463 or ADT7468 */
315 struct mutex update_lock
;
316 int valid
; /* !=0 if following fields are valid */
317 unsigned long last_reading
; /* In jiffies */
318 unsigned long last_config
; /* In jiffies */
320 u8 in
[8]; /* Register value */
321 u8 in_max
[8]; /* Register value */
322 u8 in_min
[8]; /* Register value */
323 s8 temp
[3]; /* Register value */
324 s8 temp_min
[3]; /* Register value */
325 s8 temp_max
[3]; /* Register value */
326 u16 fan
[4]; /* Register value */
327 u16 fan_min
[4]; /* Register value */
328 u8 pwm
[3]; /* Register value */
329 u8 pwm_freq
[3]; /* Register encoding */
330 u8 temp_ext
[3]; /* Decoded values */
331 u8 in_ext
[8]; /* Decoded values */
332 u8 vid
; /* Register value */
333 u8 vrm
; /* VRM version */
334 u32 alarms
; /* Register encoding, combined */
335 u8 cfg5
; /* Config Register 5 on ADT7468 */
336 struct lm85_autofan autofan
[3];
337 struct lm85_zone zone
[3];
340 static int lm85_detect(struct i2c_client
*client
, struct i2c_board_info
*info
);
341 static int lm85_probe(struct i2c_client
*client
,
342 const struct i2c_device_id
*id
);
343 static int lm85_remove(struct i2c_client
*client
);
345 static int lm85_read_value(struct i2c_client
*client
, u8 reg
);
346 static void lm85_write_value(struct i2c_client
*client
, u8 reg
, int value
);
347 static struct lm85_data
*lm85_update_device(struct device
*dev
);
350 static const struct i2c_device_id lm85_id
[] = {
351 { "adm1027", adm1027
},
352 { "adt7463", adt7463
},
353 { "adt7468", adt7468
},
354 { "lm85", any_chip
},
357 { "emc6d100", emc6d100
},
358 { "emc6d101", emc6d100
},
359 { "emc6d102", emc6d102
},
360 { "emc6d103", emc6d103
},
361 { "emc6d103s", emc6d103s
},
364 MODULE_DEVICE_TABLE(i2c
, lm85_id
);
366 static struct i2c_driver lm85_driver
= {
367 .class = I2C_CLASS_HWMON
,
372 .remove
= lm85_remove
,
374 .detect
= lm85_detect
,
375 .address_list
= normal_i2c
,
380 static ssize_t
show_fan(struct device
*dev
, struct device_attribute
*attr
,
383 int nr
= to_sensor_dev_attr(attr
)->index
;
384 struct lm85_data
*data
= lm85_update_device(dev
);
385 return sprintf(buf
, "%d\n", FAN_FROM_REG(data
->fan
[nr
]));
388 static ssize_t
show_fan_min(struct device
*dev
, struct device_attribute
*attr
,
391 int nr
= to_sensor_dev_attr(attr
)->index
;
392 struct lm85_data
*data
= lm85_update_device(dev
);
393 return sprintf(buf
, "%d\n", FAN_FROM_REG(data
->fan_min
[nr
]));
396 static ssize_t
set_fan_min(struct device
*dev
, struct device_attribute
*attr
,
397 const char *buf
, size_t count
)
399 int nr
= to_sensor_dev_attr(attr
)->index
;
400 struct i2c_client
*client
= to_i2c_client(dev
);
401 struct lm85_data
*data
= i2c_get_clientdata(client
);
405 err
= kstrtoul(buf
, 10, &val
);
409 mutex_lock(&data
->update_lock
);
410 data
->fan_min
[nr
] = FAN_TO_REG(val
);
411 lm85_write_value(client
, LM85_REG_FAN_MIN(nr
), data
->fan_min
[nr
]);
412 mutex_unlock(&data
->update_lock
);
416 #define show_fan_offset(offset) \
417 static SENSOR_DEVICE_ATTR(fan##offset##_input, S_IRUGO, \
418 show_fan, NULL, offset - 1); \
419 static SENSOR_DEVICE_ATTR(fan##offset##_min, S_IRUGO | S_IWUSR, \
420 show_fan_min, set_fan_min, offset - 1)
427 /* vid, vrm, alarms */
429 static ssize_t
show_vid_reg(struct device
*dev
, struct device_attribute
*attr
,
432 struct lm85_data
*data
= lm85_update_device(dev
);
435 if (data
->has_vid5
) {
436 /* 6-pin VID (VRM 10) */
437 vid
= vid_from_reg(data
->vid
& 0x3f, data
->vrm
);
439 /* 5-pin VID (VRM 9) */
440 vid
= vid_from_reg(data
->vid
& 0x1f, data
->vrm
);
443 return sprintf(buf
, "%d\n", vid
);
446 static DEVICE_ATTR(cpu0_vid
, S_IRUGO
, show_vid_reg
, NULL
);
448 static ssize_t
show_vrm_reg(struct device
*dev
, struct device_attribute
*attr
,
451 struct lm85_data
*data
= dev_get_drvdata(dev
);
452 return sprintf(buf
, "%ld\n", (long) data
->vrm
);
455 static ssize_t
store_vrm_reg(struct device
*dev
, struct device_attribute
*attr
,
456 const char *buf
, size_t count
)
458 struct lm85_data
*data
= dev_get_drvdata(dev
);
462 err
= kstrtoul(buf
, 10, &val
);
470 static DEVICE_ATTR(vrm
, S_IRUGO
| S_IWUSR
, show_vrm_reg
, store_vrm_reg
);
472 static ssize_t
show_alarms_reg(struct device
*dev
, struct device_attribute
475 struct lm85_data
*data
= lm85_update_device(dev
);
476 return sprintf(buf
, "%u\n", data
->alarms
);
479 static DEVICE_ATTR(alarms
, S_IRUGO
, show_alarms_reg
, NULL
);
481 static ssize_t
show_alarm(struct device
*dev
, struct device_attribute
*attr
,
484 int nr
= to_sensor_dev_attr(attr
)->index
;
485 struct lm85_data
*data
= lm85_update_device(dev
);
486 return sprintf(buf
, "%u\n", (data
->alarms
>> nr
) & 1);
489 static SENSOR_DEVICE_ATTR(in0_alarm
, S_IRUGO
, show_alarm
, NULL
, 0);
490 static SENSOR_DEVICE_ATTR(in1_alarm
, S_IRUGO
, show_alarm
, NULL
, 1);
491 static SENSOR_DEVICE_ATTR(in2_alarm
, S_IRUGO
, show_alarm
, NULL
, 2);
492 static SENSOR_DEVICE_ATTR(in3_alarm
, S_IRUGO
, show_alarm
, NULL
, 3);
493 static SENSOR_DEVICE_ATTR(in4_alarm
, S_IRUGO
, show_alarm
, NULL
, 8);
494 static SENSOR_DEVICE_ATTR(in5_alarm
, S_IRUGO
, show_alarm
, NULL
, 18);
495 static SENSOR_DEVICE_ATTR(in6_alarm
, S_IRUGO
, show_alarm
, NULL
, 16);
496 static SENSOR_DEVICE_ATTR(in7_alarm
, S_IRUGO
, show_alarm
, NULL
, 17);
497 static SENSOR_DEVICE_ATTR(temp1_alarm
, S_IRUGO
, show_alarm
, NULL
, 4);
498 static SENSOR_DEVICE_ATTR(temp1_fault
, S_IRUGO
, show_alarm
, NULL
, 14);
499 static SENSOR_DEVICE_ATTR(temp2_alarm
, S_IRUGO
, show_alarm
, NULL
, 5);
500 static SENSOR_DEVICE_ATTR(temp3_alarm
, S_IRUGO
, show_alarm
, NULL
, 6);
501 static SENSOR_DEVICE_ATTR(temp3_fault
, S_IRUGO
, show_alarm
, NULL
, 15);
502 static SENSOR_DEVICE_ATTR(fan1_alarm
, S_IRUGO
, show_alarm
, NULL
, 10);
503 static SENSOR_DEVICE_ATTR(fan2_alarm
, S_IRUGO
, show_alarm
, NULL
, 11);
504 static SENSOR_DEVICE_ATTR(fan3_alarm
, S_IRUGO
, show_alarm
, NULL
, 12);
505 static SENSOR_DEVICE_ATTR(fan4_alarm
, S_IRUGO
, show_alarm
, NULL
, 13);
509 static ssize_t
show_pwm(struct device
*dev
, struct device_attribute
*attr
,
512 int nr
= to_sensor_dev_attr(attr
)->index
;
513 struct lm85_data
*data
= lm85_update_device(dev
);
514 return sprintf(buf
, "%d\n", PWM_FROM_REG(data
->pwm
[nr
]));
517 static ssize_t
set_pwm(struct device
*dev
, struct device_attribute
*attr
,
518 const char *buf
, size_t count
)
520 int nr
= to_sensor_dev_attr(attr
)->index
;
521 struct i2c_client
*client
= to_i2c_client(dev
);
522 struct lm85_data
*data
= i2c_get_clientdata(client
);
526 err
= kstrtoul(buf
, 10, &val
);
530 mutex_lock(&data
->update_lock
);
531 data
->pwm
[nr
] = PWM_TO_REG(val
);
532 lm85_write_value(client
, LM85_REG_PWM(nr
), data
->pwm
[nr
]);
533 mutex_unlock(&data
->update_lock
);
537 static ssize_t
show_pwm_enable(struct device
*dev
, struct device_attribute
540 int nr
= to_sensor_dev_attr(attr
)->index
;
541 struct lm85_data
*data
= lm85_update_device(dev
);
542 int pwm_zone
, enable
;
544 pwm_zone
= ZONE_FROM_REG(data
->autofan
[nr
].config
);
546 case -1: /* PWM is always at 100% */
549 case 0: /* PWM is always at 0% */
550 case -2: /* PWM responds to manual control */
553 default: /* PWM in automatic mode */
556 return sprintf(buf
, "%d\n", enable
);
559 static ssize_t
set_pwm_enable(struct device
*dev
, struct device_attribute
560 *attr
, const char *buf
, size_t count
)
562 int nr
= to_sensor_dev_attr(attr
)->index
;
563 struct i2c_client
*client
= to_i2c_client(dev
);
564 struct lm85_data
*data
= i2c_get_clientdata(client
);
569 err
= kstrtoul(buf
, 10, &val
);
582 * Here we have to choose arbitrarily one of the 5 possible
583 * configurations; I go for the safest
591 mutex_lock(&data
->update_lock
);
592 data
->autofan
[nr
].config
= lm85_read_value(client
,
593 LM85_REG_AFAN_CONFIG(nr
));
594 data
->autofan
[nr
].config
= (data
->autofan
[nr
].config
& ~0xe0)
596 lm85_write_value(client
, LM85_REG_AFAN_CONFIG(nr
),
597 data
->autofan
[nr
].config
);
598 mutex_unlock(&data
->update_lock
);
602 static ssize_t
show_pwm_freq(struct device
*dev
,
603 struct device_attribute
*attr
, char *buf
)
605 int nr
= to_sensor_dev_attr(attr
)->index
;
606 struct lm85_data
*data
= lm85_update_device(dev
);
609 if (IS_ADT7468_HFPWM(data
))
612 freq
= FREQ_FROM_REG(data
->freq_map
, data
->pwm_freq
[nr
]);
614 return sprintf(buf
, "%d\n", freq
);
617 static ssize_t
set_pwm_freq(struct device
*dev
,
618 struct device_attribute
*attr
, const char *buf
, size_t count
)
620 int nr
= to_sensor_dev_attr(attr
)->index
;
621 struct i2c_client
*client
= to_i2c_client(dev
);
622 struct lm85_data
*data
= i2c_get_clientdata(client
);
626 err
= kstrtoul(buf
, 10, &val
);
630 mutex_lock(&data
->update_lock
);
632 * The ADT7468 has a special high-frequency PWM output mode,
633 * where all PWM outputs are driven by a 22.5 kHz clock.
634 * This might confuse the user, but there's not much we can do.
636 if (data
->type
== adt7468
&& val
>= 11300) { /* High freq. mode */
637 data
->cfg5
&= ~ADT7468_HFPWM
;
638 lm85_write_value(client
, ADT7468_REG_CFG5
, data
->cfg5
);
639 } else { /* Low freq. mode */
640 data
->pwm_freq
[nr
] = FREQ_TO_REG(data
->freq_map
, val
);
641 lm85_write_value(client
, LM85_REG_AFAN_RANGE(nr
),
642 (data
->zone
[nr
].range
<< 4)
643 | data
->pwm_freq
[nr
]);
644 if (data
->type
== adt7468
) {
645 data
->cfg5
|= ADT7468_HFPWM
;
646 lm85_write_value(client
, ADT7468_REG_CFG5
, data
->cfg5
);
649 mutex_unlock(&data
->update_lock
);
653 #define show_pwm_reg(offset) \
654 static SENSOR_DEVICE_ATTR(pwm##offset, S_IRUGO | S_IWUSR, \
655 show_pwm, set_pwm, offset - 1); \
656 static SENSOR_DEVICE_ATTR(pwm##offset##_enable, S_IRUGO | S_IWUSR, \
657 show_pwm_enable, set_pwm_enable, offset - 1); \
658 static SENSOR_DEVICE_ATTR(pwm##offset##_freq, S_IRUGO | S_IWUSR, \
659 show_pwm_freq, set_pwm_freq, offset - 1)
667 static ssize_t
show_in(struct device
*dev
, struct device_attribute
*attr
,
670 int nr
= to_sensor_dev_attr(attr
)->index
;
671 struct lm85_data
*data
= lm85_update_device(dev
);
672 return sprintf(buf
, "%d\n", INSEXT_FROM_REG(nr
, data
->in
[nr
],
676 static ssize_t
show_in_min(struct device
*dev
, struct device_attribute
*attr
,
679 int nr
= to_sensor_dev_attr(attr
)->index
;
680 struct lm85_data
*data
= lm85_update_device(dev
);
681 return sprintf(buf
, "%d\n", INS_FROM_REG(nr
, data
->in_min
[nr
]));
684 static ssize_t
set_in_min(struct device
*dev
, struct device_attribute
*attr
,
685 const char *buf
, size_t count
)
687 int nr
= to_sensor_dev_attr(attr
)->index
;
688 struct i2c_client
*client
= to_i2c_client(dev
);
689 struct lm85_data
*data
= i2c_get_clientdata(client
);
693 err
= kstrtol(buf
, 10, &val
);
697 mutex_lock(&data
->update_lock
);
698 data
->in_min
[nr
] = INS_TO_REG(nr
, val
);
699 lm85_write_value(client
, LM85_REG_IN_MIN(nr
), data
->in_min
[nr
]);
700 mutex_unlock(&data
->update_lock
);
704 static ssize_t
show_in_max(struct device
*dev
, struct device_attribute
*attr
,
707 int nr
= to_sensor_dev_attr(attr
)->index
;
708 struct lm85_data
*data
= lm85_update_device(dev
);
709 return sprintf(buf
, "%d\n", INS_FROM_REG(nr
, data
->in_max
[nr
]));
712 static ssize_t
set_in_max(struct device
*dev
, struct device_attribute
*attr
,
713 const char *buf
, size_t count
)
715 int nr
= to_sensor_dev_attr(attr
)->index
;
716 struct i2c_client
*client
= to_i2c_client(dev
);
717 struct lm85_data
*data
= i2c_get_clientdata(client
);
721 err
= kstrtol(buf
, 10, &val
);
725 mutex_lock(&data
->update_lock
);
726 data
->in_max
[nr
] = INS_TO_REG(nr
, val
);
727 lm85_write_value(client
, LM85_REG_IN_MAX(nr
), data
->in_max
[nr
]);
728 mutex_unlock(&data
->update_lock
);
732 #define show_in_reg(offset) \
733 static SENSOR_DEVICE_ATTR(in##offset##_input, S_IRUGO, \
734 show_in, NULL, offset); \
735 static SENSOR_DEVICE_ATTR(in##offset##_min, S_IRUGO | S_IWUSR, \
736 show_in_min, set_in_min, offset); \
737 static SENSOR_DEVICE_ATTR(in##offset##_max, S_IRUGO | S_IWUSR, \
738 show_in_max, set_in_max, offset)
751 static ssize_t
show_temp(struct device
*dev
, struct device_attribute
*attr
,
754 int nr
= to_sensor_dev_attr(attr
)->index
;
755 struct lm85_data
*data
= lm85_update_device(dev
);
756 return sprintf(buf
, "%d\n", TEMPEXT_FROM_REG(data
->temp
[nr
],
757 data
->temp_ext
[nr
]));
760 static ssize_t
show_temp_min(struct device
*dev
, struct device_attribute
*attr
,
763 int nr
= to_sensor_dev_attr(attr
)->index
;
764 struct lm85_data
*data
= lm85_update_device(dev
);
765 return sprintf(buf
, "%d\n", TEMP_FROM_REG(data
->temp_min
[nr
]));
768 static ssize_t
set_temp_min(struct device
*dev
, struct device_attribute
*attr
,
769 const char *buf
, size_t count
)
771 int nr
= to_sensor_dev_attr(attr
)->index
;
772 struct i2c_client
*client
= to_i2c_client(dev
);
773 struct lm85_data
*data
= i2c_get_clientdata(client
);
777 err
= kstrtol(buf
, 10, &val
);
781 if (IS_ADT7468_OFF64(data
))
784 mutex_lock(&data
->update_lock
);
785 data
->temp_min
[nr
] = TEMP_TO_REG(val
);
786 lm85_write_value(client
, LM85_REG_TEMP_MIN(nr
), data
->temp_min
[nr
]);
787 mutex_unlock(&data
->update_lock
);
791 static ssize_t
show_temp_max(struct device
*dev
, struct device_attribute
*attr
,
794 int nr
= to_sensor_dev_attr(attr
)->index
;
795 struct lm85_data
*data
= lm85_update_device(dev
);
796 return sprintf(buf
, "%d\n", TEMP_FROM_REG(data
->temp_max
[nr
]));
799 static ssize_t
set_temp_max(struct device
*dev
, struct device_attribute
*attr
,
800 const char *buf
, size_t count
)
802 int nr
= to_sensor_dev_attr(attr
)->index
;
803 struct i2c_client
*client
= to_i2c_client(dev
);
804 struct lm85_data
*data
= i2c_get_clientdata(client
);
808 err
= kstrtol(buf
, 10, &val
);
812 if (IS_ADT7468_OFF64(data
))
815 mutex_lock(&data
->update_lock
);
816 data
->temp_max
[nr
] = TEMP_TO_REG(val
);
817 lm85_write_value(client
, LM85_REG_TEMP_MAX(nr
), data
->temp_max
[nr
]);
818 mutex_unlock(&data
->update_lock
);
822 #define show_temp_reg(offset) \
823 static SENSOR_DEVICE_ATTR(temp##offset##_input, S_IRUGO, \
824 show_temp, NULL, offset - 1); \
825 static SENSOR_DEVICE_ATTR(temp##offset##_min, S_IRUGO | S_IWUSR, \
826 show_temp_min, set_temp_min, offset - 1); \
827 static SENSOR_DEVICE_ATTR(temp##offset##_max, S_IRUGO | S_IWUSR, \
828 show_temp_max, set_temp_max, offset - 1);
835 /* Automatic PWM control */
837 static ssize_t
show_pwm_auto_channels(struct device
*dev
,
838 struct device_attribute
*attr
, char *buf
)
840 int nr
= to_sensor_dev_attr(attr
)->index
;
841 struct lm85_data
*data
= lm85_update_device(dev
);
842 return sprintf(buf
, "%d\n", ZONE_FROM_REG(data
->autofan
[nr
].config
));
845 static ssize_t
set_pwm_auto_channels(struct device
*dev
,
846 struct device_attribute
*attr
, const char *buf
, size_t count
)
848 int nr
= to_sensor_dev_attr(attr
)->index
;
849 struct i2c_client
*client
= to_i2c_client(dev
);
850 struct lm85_data
*data
= i2c_get_clientdata(client
);
854 err
= kstrtol(buf
, 10, &val
);
858 mutex_lock(&data
->update_lock
);
859 data
->autofan
[nr
].config
= (data
->autofan
[nr
].config
& (~0xe0))
861 lm85_write_value(client
, LM85_REG_AFAN_CONFIG(nr
),
862 data
->autofan
[nr
].config
);
863 mutex_unlock(&data
->update_lock
);
867 static ssize_t
show_pwm_auto_pwm_min(struct device
*dev
,
868 struct device_attribute
*attr
, char *buf
)
870 int nr
= to_sensor_dev_attr(attr
)->index
;
871 struct lm85_data
*data
= lm85_update_device(dev
);
872 return sprintf(buf
, "%d\n", PWM_FROM_REG(data
->autofan
[nr
].min_pwm
));
875 static ssize_t
set_pwm_auto_pwm_min(struct device
*dev
,
876 struct device_attribute
*attr
, const char *buf
, size_t count
)
878 int nr
= to_sensor_dev_attr(attr
)->index
;
879 struct i2c_client
*client
= to_i2c_client(dev
);
880 struct lm85_data
*data
= i2c_get_clientdata(client
);
884 err
= kstrtoul(buf
, 10, &val
);
888 mutex_lock(&data
->update_lock
);
889 data
->autofan
[nr
].min_pwm
= PWM_TO_REG(val
);
890 lm85_write_value(client
, LM85_REG_AFAN_MINPWM(nr
),
891 data
->autofan
[nr
].min_pwm
);
892 mutex_unlock(&data
->update_lock
);
896 static ssize_t
show_pwm_auto_pwm_minctl(struct device
*dev
,
897 struct device_attribute
*attr
, char *buf
)
899 int nr
= to_sensor_dev_attr(attr
)->index
;
900 struct lm85_data
*data
= lm85_update_device(dev
);
901 return sprintf(buf
, "%d\n", data
->autofan
[nr
].min_off
);
904 static ssize_t
set_pwm_auto_pwm_minctl(struct device
*dev
,
905 struct device_attribute
*attr
, const char *buf
, size_t count
)
907 int nr
= to_sensor_dev_attr(attr
)->index
;
908 struct i2c_client
*client
= to_i2c_client(dev
);
909 struct lm85_data
*data
= i2c_get_clientdata(client
);
914 err
= kstrtol(buf
, 10, &val
);
918 mutex_lock(&data
->update_lock
);
919 data
->autofan
[nr
].min_off
= val
;
920 tmp
= lm85_read_value(client
, LM85_REG_AFAN_SPIKE1
);
921 tmp
&= ~(0x20 << nr
);
922 if (data
->autofan
[nr
].min_off
)
924 lm85_write_value(client
, LM85_REG_AFAN_SPIKE1
, tmp
);
925 mutex_unlock(&data
->update_lock
);
929 #define pwm_auto(offset) \
930 static SENSOR_DEVICE_ATTR(pwm##offset##_auto_channels, \
931 S_IRUGO | S_IWUSR, show_pwm_auto_channels, \
932 set_pwm_auto_channels, offset - 1); \
933 static SENSOR_DEVICE_ATTR(pwm##offset##_auto_pwm_min, \
934 S_IRUGO | S_IWUSR, show_pwm_auto_pwm_min, \
935 set_pwm_auto_pwm_min, offset - 1); \
936 static SENSOR_DEVICE_ATTR(pwm##offset##_auto_pwm_minctl, \
937 S_IRUGO | S_IWUSR, show_pwm_auto_pwm_minctl, \
938 set_pwm_auto_pwm_minctl, offset - 1)
944 /* Temperature settings for automatic PWM control */
946 static ssize_t
show_temp_auto_temp_off(struct device
*dev
,
947 struct device_attribute
*attr
, char *buf
)
949 int nr
= to_sensor_dev_attr(attr
)->index
;
950 struct lm85_data
*data
= lm85_update_device(dev
);
951 return sprintf(buf
, "%d\n", TEMP_FROM_REG(data
->zone
[nr
].limit
) -
952 HYST_FROM_REG(data
->zone
[nr
].hyst
));
955 static ssize_t
set_temp_auto_temp_off(struct device
*dev
,
956 struct device_attribute
*attr
, const char *buf
, size_t count
)
958 int nr
= to_sensor_dev_attr(attr
)->index
;
959 struct i2c_client
*client
= to_i2c_client(dev
);
960 struct lm85_data
*data
= i2c_get_clientdata(client
);
965 err
= kstrtol(buf
, 10, &val
);
969 mutex_lock(&data
->update_lock
);
970 min
= TEMP_FROM_REG(data
->zone
[nr
].limit
);
971 data
->zone
[nr
].hyst
= HYST_TO_REG(min
- val
);
972 if (nr
== 0 || nr
== 1) {
973 lm85_write_value(client
, LM85_REG_AFAN_HYST1
,
974 (data
->zone
[0].hyst
<< 4)
975 | data
->zone
[1].hyst
);
977 lm85_write_value(client
, LM85_REG_AFAN_HYST2
,
978 (data
->zone
[2].hyst
<< 4));
980 mutex_unlock(&data
->update_lock
);
984 static ssize_t
show_temp_auto_temp_min(struct device
*dev
,
985 struct device_attribute
*attr
, char *buf
)
987 int nr
= to_sensor_dev_attr(attr
)->index
;
988 struct lm85_data
*data
= lm85_update_device(dev
);
989 return sprintf(buf
, "%d\n", TEMP_FROM_REG(data
->zone
[nr
].limit
));
992 static ssize_t
set_temp_auto_temp_min(struct device
*dev
,
993 struct device_attribute
*attr
, const char *buf
, size_t count
)
995 int nr
= to_sensor_dev_attr(attr
)->index
;
996 struct i2c_client
*client
= to_i2c_client(dev
);
997 struct lm85_data
*data
= i2c_get_clientdata(client
);
1001 err
= kstrtol(buf
, 10, &val
);
1005 mutex_lock(&data
->update_lock
);
1006 data
->zone
[nr
].limit
= TEMP_TO_REG(val
);
1007 lm85_write_value(client
, LM85_REG_AFAN_LIMIT(nr
),
1008 data
->zone
[nr
].limit
);
1010 /* Update temp_auto_max and temp_auto_range */
1011 data
->zone
[nr
].range
= RANGE_TO_REG(
1012 TEMP_FROM_REG(data
->zone
[nr
].max_desired
) -
1013 TEMP_FROM_REG(data
->zone
[nr
].limit
));
1014 lm85_write_value(client
, LM85_REG_AFAN_RANGE(nr
),
1015 ((data
->zone
[nr
].range
& 0x0f) << 4)
1016 | (data
->pwm_freq
[nr
] & 0x07));
1018 mutex_unlock(&data
->update_lock
);
1022 static ssize_t
show_temp_auto_temp_max(struct device
*dev
,
1023 struct device_attribute
*attr
, char *buf
)
1025 int nr
= to_sensor_dev_attr(attr
)->index
;
1026 struct lm85_data
*data
= lm85_update_device(dev
);
1027 return sprintf(buf
, "%d\n", TEMP_FROM_REG(data
->zone
[nr
].limit
) +
1028 RANGE_FROM_REG(data
->zone
[nr
].range
));
1031 static ssize_t
set_temp_auto_temp_max(struct device
*dev
,
1032 struct device_attribute
*attr
, const char *buf
, size_t count
)
1034 int nr
= to_sensor_dev_attr(attr
)->index
;
1035 struct i2c_client
*client
= to_i2c_client(dev
);
1036 struct lm85_data
*data
= i2c_get_clientdata(client
);
1041 err
= kstrtol(buf
, 10, &val
);
1045 mutex_lock(&data
->update_lock
);
1046 min
= TEMP_FROM_REG(data
->zone
[nr
].limit
);
1047 data
->zone
[nr
].max_desired
= TEMP_TO_REG(val
);
1048 data
->zone
[nr
].range
= RANGE_TO_REG(
1050 lm85_write_value(client
, LM85_REG_AFAN_RANGE(nr
),
1051 ((data
->zone
[nr
].range
& 0x0f) << 4)
1052 | (data
->pwm_freq
[nr
] & 0x07));
1053 mutex_unlock(&data
->update_lock
);
1057 static ssize_t
show_temp_auto_temp_crit(struct device
*dev
,
1058 struct device_attribute
*attr
, char *buf
)
1060 int nr
= to_sensor_dev_attr(attr
)->index
;
1061 struct lm85_data
*data
= lm85_update_device(dev
);
1062 return sprintf(buf
, "%d\n", TEMP_FROM_REG(data
->zone
[nr
].critical
));
1065 static ssize_t
set_temp_auto_temp_crit(struct device
*dev
,
1066 struct device_attribute
*attr
, const char *buf
, size_t count
)
1068 int nr
= to_sensor_dev_attr(attr
)->index
;
1069 struct i2c_client
*client
= to_i2c_client(dev
);
1070 struct lm85_data
*data
= i2c_get_clientdata(client
);
1074 err
= kstrtol(buf
, 10, &val
);
1078 mutex_lock(&data
->update_lock
);
1079 data
->zone
[nr
].critical
= TEMP_TO_REG(val
);
1080 lm85_write_value(client
, LM85_REG_AFAN_CRITICAL(nr
),
1081 data
->zone
[nr
].critical
);
1082 mutex_unlock(&data
->update_lock
);
1086 #define temp_auto(offset) \
1087 static SENSOR_DEVICE_ATTR(temp##offset##_auto_temp_off, \
1088 S_IRUGO | S_IWUSR, show_temp_auto_temp_off, \
1089 set_temp_auto_temp_off, offset - 1); \
1090 static SENSOR_DEVICE_ATTR(temp##offset##_auto_temp_min, \
1091 S_IRUGO | S_IWUSR, show_temp_auto_temp_min, \
1092 set_temp_auto_temp_min, offset - 1); \
1093 static SENSOR_DEVICE_ATTR(temp##offset##_auto_temp_max, \
1094 S_IRUGO | S_IWUSR, show_temp_auto_temp_max, \
1095 set_temp_auto_temp_max, offset - 1); \
1096 static SENSOR_DEVICE_ATTR(temp##offset##_auto_temp_crit, \
1097 S_IRUGO | S_IWUSR, show_temp_auto_temp_crit, \
1098 set_temp_auto_temp_crit, offset - 1);
1104 static struct attribute
*lm85_attributes
[] = {
1105 &sensor_dev_attr_fan1_input
.dev_attr
.attr
,
1106 &sensor_dev_attr_fan2_input
.dev_attr
.attr
,
1107 &sensor_dev_attr_fan3_input
.dev_attr
.attr
,
1108 &sensor_dev_attr_fan4_input
.dev_attr
.attr
,
1109 &sensor_dev_attr_fan1_min
.dev_attr
.attr
,
1110 &sensor_dev_attr_fan2_min
.dev_attr
.attr
,
1111 &sensor_dev_attr_fan3_min
.dev_attr
.attr
,
1112 &sensor_dev_attr_fan4_min
.dev_attr
.attr
,
1113 &sensor_dev_attr_fan1_alarm
.dev_attr
.attr
,
1114 &sensor_dev_attr_fan2_alarm
.dev_attr
.attr
,
1115 &sensor_dev_attr_fan3_alarm
.dev_attr
.attr
,
1116 &sensor_dev_attr_fan4_alarm
.dev_attr
.attr
,
1118 &sensor_dev_attr_pwm1
.dev_attr
.attr
,
1119 &sensor_dev_attr_pwm2
.dev_attr
.attr
,
1120 &sensor_dev_attr_pwm3
.dev_attr
.attr
,
1121 &sensor_dev_attr_pwm1_enable
.dev_attr
.attr
,
1122 &sensor_dev_attr_pwm2_enable
.dev_attr
.attr
,
1123 &sensor_dev_attr_pwm3_enable
.dev_attr
.attr
,
1124 &sensor_dev_attr_pwm1_freq
.dev_attr
.attr
,
1125 &sensor_dev_attr_pwm2_freq
.dev_attr
.attr
,
1126 &sensor_dev_attr_pwm3_freq
.dev_attr
.attr
,
1128 &sensor_dev_attr_in0_input
.dev_attr
.attr
,
1129 &sensor_dev_attr_in1_input
.dev_attr
.attr
,
1130 &sensor_dev_attr_in2_input
.dev_attr
.attr
,
1131 &sensor_dev_attr_in3_input
.dev_attr
.attr
,
1132 &sensor_dev_attr_in0_min
.dev_attr
.attr
,
1133 &sensor_dev_attr_in1_min
.dev_attr
.attr
,
1134 &sensor_dev_attr_in2_min
.dev_attr
.attr
,
1135 &sensor_dev_attr_in3_min
.dev_attr
.attr
,
1136 &sensor_dev_attr_in0_max
.dev_attr
.attr
,
1137 &sensor_dev_attr_in1_max
.dev_attr
.attr
,
1138 &sensor_dev_attr_in2_max
.dev_attr
.attr
,
1139 &sensor_dev_attr_in3_max
.dev_attr
.attr
,
1140 &sensor_dev_attr_in0_alarm
.dev_attr
.attr
,
1141 &sensor_dev_attr_in1_alarm
.dev_attr
.attr
,
1142 &sensor_dev_attr_in2_alarm
.dev_attr
.attr
,
1143 &sensor_dev_attr_in3_alarm
.dev_attr
.attr
,
1145 &sensor_dev_attr_temp1_input
.dev_attr
.attr
,
1146 &sensor_dev_attr_temp2_input
.dev_attr
.attr
,
1147 &sensor_dev_attr_temp3_input
.dev_attr
.attr
,
1148 &sensor_dev_attr_temp1_min
.dev_attr
.attr
,
1149 &sensor_dev_attr_temp2_min
.dev_attr
.attr
,
1150 &sensor_dev_attr_temp3_min
.dev_attr
.attr
,
1151 &sensor_dev_attr_temp1_max
.dev_attr
.attr
,
1152 &sensor_dev_attr_temp2_max
.dev_attr
.attr
,
1153 &sensor_dev_attr_temp3_max
.dev_attr
.attr
,
1154 &sensor_dev_attr_temp1_alarm
.dev_attr
.attr
,
1155 &sensor_dev_attr_temp2_alarm
.dev_attr
.attr
,
1156 &sensor_dev_attr_temp3_alarm
.dev_attr
.attr
,
1157 &sensor_dev_attr_temp1_fault
.dev_attr
.attr
,
1158 &sensor_dev_attr_temp3_fault
.dev_attr
.attr
,
1160 &sensor_dev_attr_pwm1_auto_channels
.dev_attr
.attr
,
1161 &sensor_dev_attr_pwm2_auto_channels
.dev_attr
.attr
,
1162 &sensor_dev_attr_pwm3_auto_channels
.dev_attr
.attr
,
1163 &sensor_dev_attr_pwm1_auto_pwm_min
.dev_attr
.attr
,
1164 &sensor_dev_attr_pwm2_auto_pwm_min
.dev_attr
.attr
,
1165 &sensor_dev_attr_pwm3_auto_pwm_min
.dev_attr
.attr
,
1167 &sensor_dev_attr_temp1_auto_temp_min
.dev_attr
.attr
,
1168 &sensor_dev_attr_temp2_auto_temp_min
.dev_attr
.attr
,
1169 &sensor_dev_attr_temp3_auto_temp_min
.dev_attr
.attr
,
1170 &sensor_dev_attr_temp1_auto_temp_max
.dev_attr
.attr
,
1171 &sensor_dev_attr_temp2_auto_temp_max
.dev_attr
.attr
,
1172 &sensor_dev_attr_temp3_auto_temp_max
.dev_attr
.attr
,
1173 &sensor_dev_attr_temp1_auto_temp_crit
.dev_attr
.attr
,
1174 &sensor_dev_attr_temp2_auto_temp_crit
.dev_attr
.attr
,
1175 &sensor_dev_attr_temp3_auto_temp_crit
.dev_attr
.attr
,
1178 &dev_attr_cpu0_vid
.attr
,
1179 &dev_attr_alarms
.attr
,
1183 static const struct attribute_group lm85_group
= {
1184 .attrs
= lm85_attributes
,
1187 static struct attribute
*lm85_attributes_minctl
[] = {
1188 &sensor_dev_attr_pwm1_auto_pwm_minctl
.dev_attr
.attr
,
1189 &sensor_dev_attr_pwm2_auto_pwm_minctl
.dev_attr
.attr
,
1190 &sensor_dev_attr_pwm3_auto_pwm_minctl
.dev_attr
.attr
,
1194 static const struct attribute_group lm85_group_minctl
= {
1195 .attrs
= lm85_attributes_minctl
,
1198 static struct attribute
*lm85_attributes_temp_off
[] = {
1199 &sensor_dev_attr_temp1_auto_temp_off
.dev_attr
.attr
,
1200 &sensor_dev_attr_temp2_auto_temp_off
.dev_attr
.attr
,
1201 &sensor_dev_attr_temp3_auto_temp_off
.dev_attr
.attr
,
1205 static const struct attribute_group lm85_group_temp_off
= {
1206 .attrs
= lm85_attributes_temp_off
,
1209 static struct attribute
*lm85_attributes_in4
[] = {
1210 &sensor_dev_attr_in4_input
.dev_attr
.attr
,
1211 &sensor_dev_attr_in4_min
.dev_attr
.attr
,
1212 &sensor_dev_attr_in4_max
.dev_attr
.attr
,
1213 &sensor_dev_attr_in4_alarm
.dev_attr
.attr
,
1217 static const struct attribute_group lm85_group_in4
= {
1218 .attrs
= lm85_attributes_in4
,
1221 static struct attribute
*lm85_attributes_in567
[] = {
1222 &sensor_dev_attr_in5_input
.dev_attr
.attr
,
1223 &sensor_dev_attr_in6_input
.dev_attr
.attr
,
1224 &sensor_dev_attr_in7_input
.dev_attr
.attr
,
1225 &sensor_dev_attr_in5_min
.dev_attr
.attr
,
1226 &sensor_dev_attr_in6_min
.dev_attr
.attr
,
1227 &sensor_dev_attr_in7_min
.dev_attr
.attr
,
1228 &sensor_dev_attr_in5_max
.dev_attr
.attr
,
1229 &sensor_dev_attr_in6_max
.dev_attr
.attr
,
1230 &sensor_dev_attr_in7_max
.dev_attr
.attr
,
1231 &sensor_dev_attr_in5_alarm
.dev_attr
.attr
,
1232 &sensor_dev_attr_in6_alarm
.dev_attr
.attr
,
1233 &sensor_dev_attr_in7_alarm
.dev_attr
.attr
,
1237 static const struct attribute_group lm85_group_in567
= {
1238 .attrs
= lm85_attributes_in567
,
1241 static void lm85_init_client(struct i2c_client
*client
)
1245 /* Start monitoring if needed */
1246 value
= lm85_read_value(client
, LM85_REG_CONFIG
);
1247 if (!(value
& 0x01)) {
1248 dev_info(&client
->dev
, "Starting monitoring\n");
1249 lm85_write_value(client
, LM85_REG_CONFIG
, value
| 0x01);
1252 /* Warn about unusual configuration bits */
1254 dev_warn(&client
->dev
, "Device configuration is locked\n");
1255 if (!(value
& 0x04))
1256 dev_warn(&client
->dev
, "Device is not ready\n");
1259 static int lm85_is_fake(struct i2c_client
*client
)
1262 * Differenciate between real LM96000 and Winbond WPCD377I. The latter
1263 * emulate the former except that it has no hardware monitoring function
1264 * so the readings are always 0.
1269 for (i
= 0; i
< 8; i
++) {
1270 in_temp
= i2c_smbus_read_byte_data(client
, 0x20 + i
);
1271 fan
= i2c_smbus_read_byte_data(client
, 0x28 + i
);
1272 if (in_temp
!= 0x00 || fan
!= 0xff)
1279 /* Return 0 if detection is successful, -ENODEV otherwise */
1280 static int lm85_detect(struct i2c_client
*client
, struct i2c_board_info
*info
)
1282 struct i2c_adapter
*adapter
= client
->adapter
;
1283 int address
= client
->addr
;
1284 const char *type_name
;
1285 int company
, verstep
;
1287 if (!i2c_check_functionality(adapter
, I2C_FUNC_SMBUS_BYTE_DATA
)) {
1288 /* We need to be able to do byte I/O */
1292 /* Determine the chip type */
1293 company
= lm85_read_value(client
, LM85_REG_COMPANY
);
1294 verstep
= lm85_read_value(client
, LM85_REG_VERSTEP
);
1296 dev_dbg(&adapter
->dev
,
1297 "Detecting device at 0x%02x with COMPANY: 0x%02x and VERSTEP: 0x%02x\n",
1298 address
, company
, verstep
);
1300 /* All supported chips have the version in common */
1301 if ((verstep
& LM85_VERSTEP_VMASK
) != LM85_VERSTEP_GENERIC
&&
1302 (verstep
& LM85_VERSTEP_VMASK
) != LM85_VERSTEP_GENERIC2
) {
1303 dev_dbg(&adapter
->dev
,
1304 "Autodetection failed: unsupported version\n");
1309 /* Now, refine the detection */
1310 if (company
== LM85_COMPANY_NATIONAL
) {
1312 case LM85_VERSTEP_LM85C
:
1313 type_name
= "lm85c";
1315 case LM85_VERSTEP_LM85B
:
1316 type_name
= "lm85b";
1318 case LM85_VERSTEP_LM96000_1
:
1319 case LM85_VERSTEP_LM96000_2
:
1320 /* Check for Winbond WPCD377I */
1321 if (lm85_is_fake(client
)) {
1322 dev_dbg(&adapter
->dev
,
1323 "Found Winbond WPCD377I, ignoring\n");
1328 } else if (company
== LM85_COMPANY_ANALOG_DEV
) {
1330 case LM85_VERSTEP_ADM1027
:
1331 type_name
= "adm1027";
1333 case LM85_VERSTEP_ADT7463
:
1334 case LM85_VERSTEP_ADT7463C
:
1335 type_name
= "adt7463";
1337 case LM85_VERSTEP_ADT7468_1
:
1338 case LM85_VERSTEP_ADT7468_2
:
1339 type_name
= "adt7468";
1342 } else if (company
== LM85_COMPANY_SMSC
) {
1344 case LM85_VERSTEP_EMC6D100_A0
:
1345 case LM85_VERSTEP_EMC6D100_A1
:
1346 /* Note: we can't tell a '100 from a '101 */
1347 type_name
= "emc6d100";
1349 case LM85_VERSTEP_EMC6D102
:
1350 type_name
= "emc6d102";
1352 case LM85_VERSTEP_EMC6D103_A0
:
1353 case LM85_VERSTEP_EMC6D103_A1
:
1354 type_name
= "emc6d103";
1356 case LM85_VERSTEP_EMC6D103S
:
1357 type_name
= "emc6d103s";
1361 dev_dbg(&adapter
->dev
,
1362 "Autodetection failed: unknown vendor\n");
1366 strlcpy(info
->type
, type_name
, I2C_NAME_SIZE
);
1371 static void lm85_remove_files(struct i2c_client
*client
, struct lm85_data
*data
)
1373 sysfs_remove_group(&client
->dev
.kobj
, &lm85_group
);
1374 if (data
->type
!= emc6d103s
) {
1375 sysfs_remove_group(&client
->dev
.kobj
, &lm85_group_minctl
);
1376 sysfs_remove_group(&client
->dev
.kobj
, &lm85_group_temp_off
);
1378 if (!data
->has_vid5
)
1379 sysfs_remove_group(&client
->dev
.kobj
, &lm85_group_in4
);
1380 if (data
->type
== emc6d100
)
1381 sysfs_remove_group(&client
->dev
.kobj
, &lm85_group_in567
);
1384 static int lm85_probe(struct i2c_client
*client
,
1385 const struct i2c_device_id
*id
)
1387 struct lm85_data
*data
;
1390 data
= devm_kzalloc(&client
->dev
, sizeof(struct lm85_data
), GFP_KERNEL
);
1394 i2c_set_clientdata(client
, data
);
1395 data
->type
= id
->driver_data
;
1396 mutex_init(&data
->update_lock
);
1398 /* Fill in the chip specific driver values */
1399 switch (data
->type
) {
1407 data
->freq_map
= adm1027_freq_map
;
1410 data
->freq_map
= lm85_freq_map
;
1413 /* Set the VRM version */
1414 data
->vrm
= vid_which_vrm();
1416 /* Initialize the LM85 chip */
1417 lm85_init_client(client
);
1419 /* Register sysfs hooks */
1420 err
= sysfs_create_group(&client
->dev
.kobj
, &lm85_group
);
1424 /* minctl and temp_off exist on all chips except emc6d103s */
1425 if (data
->type
!= emc6d103s
) {
1426 err
= sysfs_create_group(&client
->dev
.kobj
, &lm85_group_minctl
);
1428 goto err_remove_files
;
1429 err
= sysfs_create_group(&client
->dev
.kobj
,
1430 &lm85_group_temp_off
);
1432 goto err_remove_files
;
1436 * The ADT7463/68 have an optional VRM 10 mode where pin 21 is used
1437 * as a sixth digital VID input rather than an analog input.
1439 if (data
->type
== adt7463
|| data
->type
== adt7468
) {
1440 u8 vid
= lm85_read_value(client
, LM85_REG_VID
);
1442 data
->has_vid5
= true;
1445 if (!data
->has_vid5
) {
1446 err
= sysfs_create_group(&client
->dev
.kobj
, &lm85_group_in4
);
1448 goto err_remove_files
;
1451 /* The EMC6D100 has 3 additional voltage inputs */
1452 if (data
->type
== emc6d100
) {
1453 err
= sysfs_create_group(&client
->dev
.kobj
, &lm85_group_in567
);
1455 goto err_remove_files
;
1458 data
->hwmon_dev
= hwmon_device_register(&client
->dev
);
1459 if (IS_ERR(data
->hwmon_dev
)) {
1460 err
= PTR_ERR(data
->hwmon_dev
);
1461 goto err_remove_files
;
1466 /* Error out and cleanup code */
1468 lm85_remove_files(client
, data
);
1472 static int lm85_remove(struct i2c_client
*client
)
1474 struct lm85_data
*data
= i2c_get_clientdata(client
);
1475 hwmon_device_unregister(data
->hwmon_dev
);
1476 lm85_remove_files(client
, data
);
1481 static int lm85_read_value(struct i2c_client
*client
, u8 reg
)
1485 /* What size location is it? */
1487 case LM85_REG_FAN(0): /* Read WORD data */
1488 case LM85_REG_FAN(1):
1489 case LM85_REG_FAN(2):
1490 case LM85_REG_FAN(3):
1491 case LM85_REG_FAN_MIN(0):
1492 case LM85_REG_FAN_MIN(1):
1493 case LM85_REG_FAN_MIN(2):
1494 case LM85_REG_FAN_MIN(3):
1495 case LM85_REG_ALARM1
: /* Read both bytes at once */
1496 res
= i2c_smbus_read_byte_data(client
, reg
) & 0xff;
1497 res
|= i2c_smbus_read_byte_data(client
, reg
+ 1) << 8;
1499 default: /* Read BYTE data */
1500 res
= i2c_smbus_read_byte_data(client
, reg
);
1507 static void lm85_write_value(struct i2c_client
*client
, u8 reg
, int value
)
1510 case LM85_REG_FAN(0): /* Write WORD data */
1511 case LM85_REG_FAN(1):
1512 case LM85_REG_FAN(2):
1513 case LM85_REG_FAN(3):
1514 case LM85_REG_FAN_MIN(0):
1515 case LM85_REG_FAN_MIN(1):
1516 case LM85_REG_FAN_MIN(2):
1517 case LM85_REG_FAN_MIN(3):
1518 /* NOTE: ALARM is read only, so not included here */
1519 i2c_smbus_write_byte_data(client
, reg
, value
& 0xff);
1520 i2c_smbus_write_byte_data(client
, reg
+ 1, value
>> 8);
1522 default: /* Write BYTE data */
1523 i2c_smbus_write_byte_data(client
, reg
, value
);
1528 static struct lm85_data
*lm85_update_device(struct device
*dev
)
1530 struct i2c_client
*client
= to_i2c_client(dev
);
1531 struct lm85_data
*data
= i2c_get_clientdata(client
);
1534 mutex_lock(&data
->update_lock
);
1537 time_after(jiffies
, data
->last_reading
+ LM85_DATA_INTERVAL
)) {
1538 /* Things that change quickly */
1539 dev_dbg(&client
->dev
, "Reading sensor values\n");
1542 * Have to read extended bits first to "freeze" the
1543 * more significant bits that are read later.
1544 * There are 2 additional resolution bits per channel and we
1545 * have room for 4, so we shift them to the left.
1547 if (data
->type
== adm1027
|| data
->type
== adt7463
||
1548 data
->type
== adt7468
) {
1549 int ext1
= lm85_read_value(client
,
1550 ADM1027_REG_EXTEND_ADC1
);
1551 int ext2
= lm85_read_value(client
,
1552 ADM1027_REG_EXTEND_ADC2
);
1553 int val
= (ext1
<< 8) + ext2
;
1555 for (i
= 0; i
<= 4; i
++)
1557 ((val
>> (i
* 2)) & 0x03) << 2;
1559 for (i
= 0; i
<= 2; i
++)
1561 (val
>> ((i
+ 4) * 2)) & 0x0c;
1564 data
->vid
= lm85_read_value(client
, LM85_REG_VID
);
1566 for (i
= 0; i
<= 3; ++i
) {
1568 lm85_read_value(client
, LM85_REG_IN(i
));
1570 lm85_read_value(client
, LM85_REG_FAN(i
));
1573 if (!data
->has_vid5
)
1574 data
->in
[4] = lm85_read_value(client
, LM85_REG_IN(4));
1576 if (data
->type
== adt7468
)
1577 data
->cfg5
= lm85_read_value(client
, ADT7468_REG_CFG5
);
1579 for (i
= 0; i
<= 2; ++i
) {
1581 lm85_read_value(client
, LM85_REG_TEMP(i
));
1583 lm85_read_value(client
, LM85_REG_PWM(i
));
1585 if (IS_ADT7468_OFF64(data
))
1586 data
->temp
[i
] -= 64;
1589 data
->alarms
= lm85_read_value(client
, LM85_REG_ALARM1
);
1591 if (data
->type
== emc6d100
) {
1592 /* Three more voltage sensors */
1593 for (i
= 5; i
<= 7; ++i
) {
1594 data
->in
[i
] = lm85_read_value(client
,
1595 EMC6D100_REG_IN(i
));
1597 /* More alarm bits */
1598 data
->alarms
|= lm85_read_value(client
,
1599 EMC6D100_REG_ALARM3
) << 16;
1600 } else if (data
->type
== emc6d102
|| data
->type
== emc6d103
||
1601 data
->type
== emc6d103s
) {
1603 * Have to read LSB bits after the MSB ones because
1604 * the reading of the MSB bits has frozen the
1605 * LSBs (backward from the ADM1027).
1607 int ext1
= lm85_read_value(client
,
1608 EMC6D102_REG_EXTEND_ADC1
);
1609 int ext2
= lm85_read_value(client
,
1610 EMC6D102_REG_EXTEND_ADC2
);
1611 int ext3
= lm85_read_value(client
,
1612 EMC6D102_REG_EXTEND_ADC3
);
1613 int ext4
= lm85_read_value(client
,
1614 EMC6D102_REG_EXTEND_ADC4
);
1615 data
->in_ext
[0] = ext3
& 0x0f;
1616 data
->in_ext
[1] = ext4
& 0x0f;
1617 data
->in_ext
[2] = ext4
>> 4;
1618 data
->in_ext
[3] = ext3
>> 4;
1619 data
->in_ext
[4] = ext2
>> 4;
1621 data
->temp_ext
[0] = ext1
& 0x0f;
1622 data
->temp_ext
[1] = ext2
& 0x0f;
1623 data
->temp_ext
[2] = ext1
>> 4;
1626 data
->last_reading
= jiffies
;
1627 } /* last_reading */
1630 time_after(jiffies
, data
->last_config
+ LM85_CONFIG_INTERVAL
)) {
1631 /* Things that don't change often */
1632 dev_dbg(&client
->dev
, "Reading config values\n");
1634 for (i
= 0; i
<= 3; ++i
) {
1636 lm85_read_value(client
, LM85_REG_IN_MIN(i
));
1638 lm85_read_value(client
, LM85_REG_IN_MAX(i
));
1640 lm85_read_value(client
, LM85_REG_FAN_MIN(i
));
1643 if (!data
->has_vid5
) {
1644 data
->in_min
[4] = lm85_read_value(client
,
1645 LM85_REG_IN_MIN(4));
1646 data
->in_max
[4] = lm85_read_value(client
,
1647 LM85_REG_IN_MAX(4));
1650 if (data
->type
== emc6d100
) {
1651 for (i
= 5; i
<= 7; ++i
) {
1652 data
->in_min
[i
] = lm85_read_value(client
,
1653 EMC6D100_REG_IN_MIN(i
));
1654 data
->in_max
[i
] = lm85_read_value(client
,
1655 EMC6D100_REG_IN_MAX(i
));
1659 for (i
= 0; i
<= 2; ++i
) {
1663 lm85_read_value(client
, LM85_REG_TEMP_MIN(i
));
1665 lm85_read_value(client
, LM85_REG_TEMP_MAX(i
));
1667 data
->autofan
[i
].config
=
1668 lm85_read_value(client
, LM85_REG_AFAN_CONFIG(i
));
1669 val
= lm85_read_value(client
, LM85_REG_AFAN_RANGE(i
));
1670 data
->pwm_freq
[i
] = val
& 0x07;
1671 data
->zone
[i
].range
= val
>> 4;
1672 data
->autofan
[i
].min_pwm
=
1673 lm85_read_value(client
, LM85_REG_AFAN_MINPWM(i
));
1674 data
->zone
[i
].limit
=
1675 lm85_read_value(client
, LM85_REG_AFAN_LIMIT(i
));
1676 data
->zone
[i
].critical
=
1677 lm85_read_value(client
, LM85_REG_AFAN_CRITICAL(i
));
1679 if (IS_ADT7468_OFF64(data
)) {
1680 data
->temp_min
[i
] -= 64;
1681 data
->temp_max
[i
] -= 64;
1682 data
->zone
[i
].limit
-= 64;
1683 data
->zone
[i
].critical
-= 64;
1687 if (data
->type
!= emc6d103s
) {
1688 i
= lm85_read_value(client
, LM85_REG_AFAN_SPIKE1
);
1689 data
->autofan
[0].min_off
= (i
& 0x20) != 0;
1690 data
->autofan
[1].min_off
= (i
& 0x40) != 0;
1691 data
->autofan
[2].min_off
= (i
& 0x80) != 0;
1693 i
= lm85_read_value(client
, LM85_REG_AFAN_HYST1
);
1694 data
->zone
[0].hyst
= i
>> 4;
1695 data
->zone
[1].hyst
= i
& 0x0f;
1697 i
= lm85_read_value(client
, LM85_REG_AFAN_HYST2
);
1698 data
->zone
[2].hyst
= i
>> 4;
1701 data
->last_config
= jiffies
;
1706 mutex_unlock(&data
->update_lock
);
1711 module_i2c_driver(lm85_driver
);
1713 MODULE_LICENSE("GPL");
1714 MODULE_AUTHOR("Philip Pokorny <ppokorny@penguincomputing.com>, "
1715 "Margit Schubert-While <margitsw@t-online.de>, "
1716 "Justin Thiessen <jthiessen@penguincomputing.com>");
1717 MODULE_DESCRIPTION("LM85-B, LM85-C driver");