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--2014 Jean Delvare <jdelvare@suse.de>
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
};
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_LM85C 0x60
79 #define LM85_VERSTEP_LM85B 0x62
80 #define LM85_VERSTEP_LM96000_1 0x68
81 #define LM85_VERSTEP_LM96000_2 0x69
82 #define LM85_VERSTEP_ADM1027 0x60
83 #define LM85_VERSTEP_ADT7463 0x62
84 #define LM85_VERSTEP_ADT7463C 0x6A
85 #define LM85_VERSTEP_ADT7468_1 0x71
86 #define LM85_VERSTEP_ADT7468_2 0x72
87 #define LM85_VERSTEP_EMC6D100_A0 0x60
88 #define LM85_VERSTEP_EMC6D100_A1 0x61
89 #define LM85_VERSTEP_EMC6D102 0x65
90 #define LM85_VERSTEP_EMC6D103_A0 0x68
91 #define LM85_VERSTEP_EMC6D103_A1 0x69
92 #define LM85_VERSTEP_EMC6D103S 0x6A /* Also known as EMC6D103:A2 */
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
126 * Conversions. Rounding and limit checking is only done on the TO_REG
127 * variants. Note that you should be a bit careful with which arguments
128 * these macros are called: arguments may be evaluated more than once.
131 /* IN are scaled according to built-in resistors */
132 static const int lm85_scaling
[] = { /* .001 Volts */
133 2500, 2250, 3300, 5000, 12000,
134 3300, 1500, 1800 /*EMC6D100*/
136 #define SCALE(val, from, to) (((val) * (to) + ((from) / 2)) / (from))
138 #define INS_TO_REG(n, val) \
139 clamp_val(SCALE(val, lm85_scaling[n], 192), 0, 255)
141 #define INSEXT_FROM_REG(n, val, ext) \
142 SCALE(((val) << 4) + (ext), 192 << 4, lm85_scaling[n])
144 #define INS_FROM_REG(n, val) SCALE((val), 192, lm85_scaling[n])
146 /* FAN speed is measured using 90kHz clock */
147 static inline u16
FAN_TO_REG(unsigned long val
)
151 return clamp_val(5400000 / val
, 1, 0xfffe);
153 #define FAN_FROM_REG(val) ((val) == 0 ? -1 : (val) == 0xffff ? 0 : \
156 /* Temperature is reported in .001 degC increments */
157 #define TEMP_TO_REG(val) \
158 clamp_val(SCALE(val, 1000, 1), -127, 127)
159 #define TEMPEXT_FROM_REG(val, ext) \
160 SCALE(((val) << 4) + (ext), 16, 1000)
161 #define TEMP_FROM_REG(val) ((val) * 1000)
163 #define PWM_TO_REG(val) clamp_val(val, 0, 255)
164 #define PWM_FROM_REG(val) (val)
168 * ZONEs have the following parameters:
169 * Limit (low) temp, 1. degC
170 * Hysteresis (below limit), 1. degC (0-15)
171 * Range of speed control, .1 degC (2-80)
172 * Critical (high) temp, 1. degC
174 * FAN PWMs have the following parameters:
175 * Reference Zone, 1, 2, 3, etc.
176 * Spinup time, .05 sec
177 * PWM value at limit/low temp, 1 count
178 * PWM Frequency, 1. Hz
179 * PWM is Min or OFF below limit, flag
180 * Invert PWM output, flag
182 * Some chips filter the temp, others the fan.
183 * Filter constant (or disabled) .1 seconds
186 /* These are the zone temperature range encodings in .001 degree C */
187 static const int lm85_range_map
[] = {
188 2000, 2500, 3300, 4000, 5000, 6600, 8000, 10000,
189 13300, 16000, 20000, 26600, 32000, 40000, 53300, 80000
192 static int RANGE_TO_REG(int range
)
196 /* Find the closest match */
197 for (i
= 0; i
< 15; ++i
) {
198 if (range
<= (lm85_range_map
[i
] + lm85_range_map
[i
+ 1]) / 2)
204 #define RANGE_FROM_REG(val) lm85_range_map[(val) & 0x0f]
206 /* These are the PWM frequency encodings */
207 static const int lm85_freq_map
[8] = { /* 1 Hz */
208 10, 15, 23, 30, 38, 47, 61, 94
210 static const int adm1027_freq_map
[8] = { /* 1 Hz */
211 11, 15, 22, 29, 35, 44, 59, 88
214 static int FREQ_TO_REG(const int *map
, int freq
)
218 /* Find the closest match */
219 for (i
= 0; i
< 7; ++i
)
220 if (freq
<= (map
[i
] + map
[i
+ 1]) / 2)
225 static int FREQ_FROM_REG(const int *map
, u8 reg
)
227 return map
[reg
& 0x07];
231 * Since we can't use strings, I'm abusing these numbers
232 * to stand in for the following meanings:
233 * 1 -- PWM responds to Zone 1
234 * 2 -- PWM responds to Zone 2
235 * 3 -- PWM responds to Zone 3
236 * 23 -- PWM responds to the higher temp of Zone 2 or 3
237 * 123 -- PWM responds to highest of Zone 1, 2, or 3
238 * 0 -- PWM is always at 0% (ie, off)
239 * -1 -- PWM is always at 100%
240 * -2 -- PWM responds to manual control
243 static const int lm85_zone_map
[] = { 1, 2, 3, -1, 0, 23, 123, -2 };
244 #define ZONE_FROM_REG(val) lm85_zone_map[(val) >> 5]
246 static int ZONE_TO_REG(int zone
)
250 for (i
= 0; i
<= 7; ++i
)
251 if (zone
== lm85_zone_map
[i
])
253 if (i
> 7) /* Not found. */
254 i
= 3; /* Always 100% */
258 #define HYST_TO_REG(val) clamp_val(((val) + 500) / 1000, 0, 15)
259 #define HYST_FROM_REG(val) ((val) * 1000)
262 * Chip sampling rates
264 * Some sensors are not updated more frequently than once per second
265 * so it doesn't make sense to read them more often than that.
266 * We cache the results and return the saved data if the driver
267 * is called again before a second has elapsed.
269 * Also, there is significant configuration data for this chip
270 * given the automatic PWM fan control that is possible. There
271 * are about 47 bytes of config data to only 22 bytes of actual
272 * readings. So, we keep the config data up to date in the cache
273 * when it is written and only sample it once every 1 *minute*
275 #define LM85_DATA_INTERVAL (HZ + HZ / 2)
276 #define LM85_CONFIG_INTERVAL (1 * 60 * HZ)
279 * LM85 can automatically adjust fan speeds based on temperature
280 * This structure encapsulates an entire Zone config. There are
281 * three zones (one for each temperature input) on the lm85
284 s8 limit
; /* Low temp limit */
285 u8 hyst
; /* Low limit hysteresis. (0-15) */
286 u8 range
; /* Temp range, encoded */
287 s8 critical
; /* "All fans ON" temp limit */
289 * Actual "max" temperature specified. Preserved
290 * to prevent "drift" as other autofan control
295 struct lm85_autofan
{
296 u8 config
; /* Register value */
297 u8 min_pwm
; /* Minimum PWM value, encoded */
298 u8 min_off
; /* Min PWM or OFF below "limit", flag */
302 * For each registered chip, we need to keep some data in memory.
303 * The structure is dynamically allocated.
306 struct device
*hwmon_dev
;
310 bool has_vid5
; /* true if VID5 is configured for ADT7463 or ADT7468 */
312 struct mutex update_lock
;
313 int valid
; /* !=0 if following fields are valid */
314 unsigned long last_reading
; /* In jiffies */
315 unsigned long last_config
; /* In jiffies */
317 u8 in
[8]; /* Register value */
318 u8 in_max
[8]; /* Register value */
319 u8 in_min
[8]; /* Register value */
320 s8 temp
[3]; /* Register value */
321 s8 temp_min
[3]; /* Register value */
322 s8 temp_max
[3]; /* Register value */
323 u16 fan
[4]; /* Register value */
324 u16 fan_min
[4]; /* Register value */
325 u8 pwm
[3]; /* Register value */
326 u8 pwm_freq
[3]; /* Register encoding */
327 u8 temp_ext
[3]; /* Decoded values */
328 u8 in_ext
[8]; /* Decoded values */
329 u8 vid
; /* Register value */
330 u8 vrm
; /* VRM version */
331 u32 alarms
; /* Register encoding, combined */
332 u8 cfg5
; /* Config Register 5 on ADT7468 */
333 struct lm85_autofan autofan
[3];
334 struct lm85_zone zone
[3];
337 static int lm85_detect(struct i2c_client
*client
, struct i2c_board_info
*info
);
338 static int lm85_probe(struct i2c_client
*client
,
339 const struct i2c_device_id
*id
);
340 static int lm85_remove(struct i2c_client
*client
);
342 static int lm85_read_value(struct i2c_client
*client
, u8 reg
);
343 static void lm85_write_value(struct i2c_client
*client
, u8 reg
, int value
);
344 static struct lm85_data
*lm85_update_device(struct device
*dev
);
347 static const struct i2c_device_id lm85_id
[] = {
348 { "adm1027", adm1027
},
349 { "adt7463", adt7463
},
350 { "adt7468", adt7468
},
354 { "emc6d100", emc6d100
},
355 { "emc6d101", emc6d100
},
356 { "emc6d102", emc6d102
},
357 { "emc6d103", emc6d103
},
358 { "emc6d103s", emc6d103s
},
361 MODULE_DEVICE_TABLE(i2c
, lm85_id
);
363 static struct i2c_driver lm85_driver
= {
364 .class = I2C_CLASS_HWMON
,
369 .remove
= lm85_remove
,
371 .detect
= lm85_detect
,
372 .address_list
= normal_i2c
,
377 static ssize_t
show_fan(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
[nr
]));
385 static ssize_t
show_fan_min(struct device
*dev
, struct device_attribute
*attr
,
388 int nr
= to_sensor_dev_attr(attr
)->index
;
389 struct lm85_data
*data
= lm85_update_device(dev
);
390 return sprintf(buf
, "%d\n", FAN_FROM_REG(data
->fan_min
[nr
]));
393 static ssize_t
set_fan_min(struct device
*dev
, struct device_attribute
*attr
,
394 const char *buf
, size_t count
)
396 int nr
= to_sensor_dev_attr(attr
)->index
;
397 struct i2c_client
*client
= to_i2c_client(dev
);
398 struct lm85_data
*data
= i2c_get_clientdata(client
);
402 err
= kstrtoul(buf
, 10, &val
);
406 mutex_lock(&data
->update_lock
);
407 data
->fan_min
[nr
] = FAN_TO_REG(val
);
408 lm85_write_value(client
, LM85_REG_FAN_MIN(nr
), data
->fan_min
[nr
]);
409 mutex_unlock(&data
->update_lock
);
413 #define show_fan_offset(offset) \
414 static SENSOR_DEVICE_ATTR(fan##offset##_input, S_IRUGO, \
415 show_fan, NULL, offset - 1); \
416 static SENSOR_DEVICE_ATTR(fan##offset##_min, S_IRUGO | S_IWUSR, \
417 show_fan_min, set_fan_min, offset - 1)
424 /* vid, vrm, alarms */
426 static ssize_t
show_vid_reg(struct device
*dev
, struct device_attribute
*attr
,
429 struct lm85_data
*data
= lm85_update_device(dev
);
432 if (data
->has_vid5
) {
433 /* 6-pin VID (VRM 10) */
434 vid
= vid_from_reg(data
->vid
& 0x3f, data
->vrm
);
436 /* 5-pin VID (VRM 9) */
437 vid
= vid_from_reg(data
->vid
& 0x1f, data
->vrm
);
440 return sprintf(buf
, "%d\n", vid
);
443 static DEVICE_ATTR(cpu0_vid
, S_IRUGO
, show_vid_reg
, NULL
);
445 static ssize_t
show_vrm_reg(struct device
*dev
, struct device_attribute
*attr
,
448 struct lm85_data
*data
= dev_get_drvdata(dev
);
449 return sprintf(buf
, "%ld\n", (long) data
->vrm
);
452 static ssize_t
store_vrm_reg(struct device
*dev
, struct device_attribute
*attr
,
453 const char *buf
, size_t count
)
455 struct lm85_data
*data
= dev_get_drvdata(dev
);
459 err
= kstrtoul(buf
, 10, &val
);
467 static DEVICE_ATTR(vrm
, S_IRUGO
| S_IWUSR
, show_vrm_reg
, store_vrm_reg
);
469 static ssize_t
show_alarms_reg(struct device
*dev
, struct device_attribute
472 struct lm85_data
*data
= lm85_update_device(dev
);
473 return sprintf(buf
, "%u\n", data
->alarms
);
476 static DEVICE_ATTR(alarms
, S_IRUGO
, show_alarms_reg
, NULL
);
478 static ssize_t
show_alarm(struct device
*dev
, struct device_attribute
*attr
,
481 int nr
= to_sensor_dev_attr(attr
)->index
;
482 struct lm85_data
*data
= lm85_update_device(dev
);
483 return sprintf(buf
, "%u\n", (data
->alarms
>> nr
) & 1);
486 static SENSOR_DEVICE_ATTR(in0_alarm
, S_IRUGO
, show_alarm
, NULL
, 0);
487 static SENSOR_DEVICE_ATTR(in1_alarm
, S_IRUGO
, show_alarm
, NULL
, 1);
488 static SENSOR_DEVICE_ATTR(in2_alarm
, S_IRUGO
, show_alarm
, NULL
, 2);
489 static SENSOR_DEVICE_ATTR(in3_alarm
, S_IRUGO
, show_alarm
, NULL
, 3);
490 static SENSOR_DEVICE_ATTR(in4_alarm
, S_IRUGO
, show_alarm
, NULL
, 8);
491 static SENSOR_DEVICE_ATTR(in5_alarm
, S_IRUGO
, show_alarm
, NULL
, 18);
492 static SENSOR_DEVICE_ATTR(in6_alarm
, S_IRUGO
, show_alarm
, NULL
, 16);
493 static SENSOR_DEVICE_ATTR(in7_alarm
, S_IRUGO
, show_alarm
, NULL
, 17);
494 static SENSOR_DEVICE_ATTR(temp1_alarm
, S_IRUGO
, show_alarm
, NULL
, 4);
495 static SENSOR_DEVICE_ATTR(temp1_fault
, S_IRUGO
, show_alarm
, NULL
, 14);
496 static SENSOR_DEVICE_ATTR(temp2_alarm
, S_IRUGO
, show_alarm
, NULL
, 5);
497 static SENSOR_DEVICE_ATTR(temp3_alarm
, S_IRUGO
, show_alarm
, NULL
, 6);
498 static SENSOR_DEVICE_ATTR(temp3_fault
, S_IRUGO
, show_alarm
, NULL
, 15);
499 static SENSOR_DEVICE_ATTR(fan1_alarm
, S_IRUGO
, show_alarm
, NULL
, 10);
500 static SENSOR_DEVICE_ATTR(fan2_alarm
, S_IRUGO
, show_alarm
, NULL
, 11);
501 static SENSOR_DEVICE_ATTR(fan3_alarm
, S_IRUGO
, show_alarm
, NULL
, 12);
502 static SENSOR_DEVICE_ATTR(fan4_alarm
, S_IRUGO
, show_alarm
, NULL
, 13);
506 static ssize_t
show_pwm(struct device
*dev
, struct device_attribute
*attr
,
509 int nr
= to_sensor_dev_attr(attr
)->index
;
510 struct lm85_data
*data
= lm85_update_device(dev
);
511 return sprintf(buf
, "%d\n", PWM_FROM_REG(data
->pwm
[nr
]));
514 static ssize_t
set_pwm(struct device
*dev
, struct device_attribute
*attr
,
515 const char *buf
, size_t count
)
517 int nr
= to_sensor_dev_attr(attr
)->index
;
518 struct i2c_client
*client
= to_i2c_client(dev
);
519 struct lm85_data
*data
= i2c_get_clientdata(client
);
523 err
= kstrtoul(buf
, 10, &val
);
527 mutex_lock(&data
->update_lock
);
528 data
->pwm
[nr
] = PWM_TO_REG(val
);
529 lm85_write_value(client
, LM85_REG_PWM(nr
), data
->pwm
[nr
]);
530 mutex_unlock(&data
->update_lock
);
534 static ssize_t
show_pwm_enable(struct device
*dev
, struct device_attribute
537 int nr
= to_sensor_dev_attr(attr
)->index
;
538 struct lm85_data
*data
= lm85_update_device(dev
);
539 int pwm_zone
, enable
;
541 pwm_zone
= ZONE_FROM_REG(data
->autofan
[nr
].config
);
543 case -1: /* PWM is always at 100% */
546 case 0: /* PWM is always at 0% */
547 case -2: /* PWM responds to manual control */
550 default: /* PWM in automatic mode */
553 return sprintf(buf
, "%d\n", enable
);
556 static ssize_t
set_pwm_enable(struct device
*dev
, struct device_attribute
557 *attr
, const char *buf
, size_t count
)
559 int nr
= to_sensor_dev_attr(attr
)->index
;
560 struct i2c_client
*client
= to_i2c_client(dev
);
561 struct lm85_data
*data
= i2c_get_clientdata(client
);
566 err
= kstrtoul(buf
, 10, &val
);
579 * Here we have to choose arbitrarily one of the 5 possible
580 * configurations; I go for the safest
588 mutex_lock(&data
->update_lock
);
589 data
->autofan
[nr
].config
= lm85_read_value(client
,
590 LM85_REG_AFAN_CONFIG(nr
));
591 data
->autofan
[nr
].config
= (data
->autofan
[nr
].config
& ~0xe0)
593 lm85_write_value(client
, LM85_REG_AFAN_CONFIG(nr
),
594 data
->autofan
[nr
].config
);
595 mutex_unlock(&data
->update_lock
);
599 static ssize_t
show_pwm_freq(struct device
*dev
,
600 struct device_attribute
*attr
, char *buf
)
602 int nr
= to_sensor_dev_attr(attr
)->index
;
603 struct lm85_data
*data
= lm85_update_device(dev
);
606 if (IS_ADT7468_HFPWM(data
))
609 freq
= FREQ_FROM_REG(data
->freq_map
, data
->pwm_freq
[nr
]);
611 return sprintf(buf
, "%d\n", freq
);
614 static ssize_t
set_pwm_freq(struct device
*dev
,
615 struct device_attribute
*attr
, const char *buf
, size_t count
)
617 int nr
= to_sensor_dev_attr(attr
)->index
;
618 struct i2c_client
*client
= to_i2c_client(dev
);
619 struct lm85_data
*data
= i2c_get_clientdata(client
);
623 err
= kstrtoul(buf
, 10, &val
);
627 mutex_lock(&data
->update_lock
);
629 * The ADT7468 has a special high-frequency PWM output mode,
630 * where all PWM outputs are driven by a 22.5 kHz clock.
631 * This might confuse the user, but there's not much we can do.
633 if (data
->type
== adt7468
&& val
>= 11300) { /* High freq. mode */
634 data
->cfg5
&= ~ADT7468_HFPWM
;
635 lm85_write_value(client
, ADT7468_REG_CFG5
, data
->cfg5
);
636 } else { /* Low freq. mode */
637 data
->pwm_freq
[nr
] = FREQ_TO_REG(data
->freq_map
, val
);
638 lm85_write_value(client
, LM85_REG_AFAN_RANGE(nr
),
639 (data
->zone
[nr
].range
<< 4)
640 | data
->pwm_freq
[nr
]);
641 if (data
->type
== adt7468
) {
642 data
->cfg5
|= ADT7468_HFPWM
;
643 lm85_write_value(client
, ADT7468_REG_CFG5
, data
->cfg5
);
646 mutex_unlock(&data
->update_lock
);
650 #define show_pwm_reg(offset) \
651 static SENSOR_DEVICE_ATTR(pwm##offset, S_IRUGO | S_IWUSR, \
652 show_pwm, set_pwm, offset - 1); \
653 static SENSOR_DEVICE_ATTR(pwm##offset##_enable, S_IRUGO | S_IWUSR, \
654 show_pwm_enable, set_pwm_enable, offset - 1); \
655 static SENSOR_DEVICE_ATTR(pwm##offset##_freq, S_IRUGO | S_IWUSR, \
656 show_pwm_freq, set_pwm_freq, offset - 1)
664 static ssize_t
show_in(struct device
*dev
, struct device_attribute
*attr
,
667 int nr
= to_sensor_dev_attr(attr
)->index
;
668 struct lm85_data
*data
= lm85_update_device(dev
);
669 return sprintf(buf
, "%d\n", INSEXT_FROM_REG(nr
, data
->in
[nr
],
673 static ssize_t
show_in_min(struct device
*dev
, struct device_attribute
*attr
,
676 int nr
= to_sensor_dev_attr(attr
)->index
;
677 struct lm85_data
*data
= lm85_update_device(dev
);
678 return sprintf(buf
, "%d\n", INS_FROM_REG(nr
, data
->in_min
[nr
]));
681 static ssize_t
set_in_min(struct device
*dev
, struct device_attribute
*attr
,
682 const char *buf
, size_t count
)
684 int nr
= to_sensor_dev_attr(attr
)->index
;
685 struct i2c_client
*client
= to_i2c_client(dev
);
686 struct lm85_data
*data
= i2c_get_clientdata(client
);
690 err
= kstrtol(buf
, 10, &val
);
694 mutex_lock(&data
->update_lock
);
695 data
->in_min
[nr
] = INS_TO_REG(nr
, val
);
696 lm85_write_value(client
, LM85_REG_IN_MIN(nr
), data
->in_min
[nr
]);
697 mutex_unlock(&data
->update_lock
);
701 static ssize_t
show_in_max(struct device
*dev
, struct device_attribute
*attr
,
704 int nr
= to_sensor_dev_attr(attr
)->index
;
705 struct lm85_data
*data
= lm85_update_device(dev
);
706 return sprintf(buf
, "%d\n", INS_FROM_REG(nr
, data
->in_max
[nr
]));
709 static ssize_t
set_in_max(struct device
*dev
, struct device_attribute
*attr
,
710 const char *buf
, size_t count
)
712 int nr
= to_sensor_dev_attr(attr
)->index
;
713 struct i2c_client
*client
= to_i2c_client(dev
);
714 struct lm85_data
*data
= i2c_get_clientdata(client
);
718 err
= kstrtol(buf
, 10, &val
);
722 mutex_lock(&data
->update_lock
);
723 data
->in_max
[nr
] = INS_TO_REG(nr
, val
);
724 lm85_write_value(client
, LM85_REG_IN_MAX(nr
), data
->in_max
[nr
]);
725 mutex_unlock(&data
->update_lock
);
729 #define show_in_reg(offset) \
730 static SENSOR_DEVICE_ATTR(in##offset##_input, S_IRUGO, \
731 show_in, NULL, offset); \
732 static SENSOR_DEVICE_ATTR(in##offset##_min, S_IRUGO | S_IWUSR, \
733 show_in_min, set_in_min, offset); \
734 static SENSOR_DEVICE_ATTR(in##offset##_max, S_IRUGO | S_IWUSR, \
735 show_in_max, set_in_max, offset)
748 static ssize_t
show_temp(struct device
*dev
, struct device_attribute
*attr
,
751 int nr
= to_sensor_dev_attr(attr
)->index
;
752 struct lm85_data
*data
= lm85_update_device(dev
);
753 return sprintf(buf
, "%d\n", TEMPEXT_FROM_REG(data
->temp
[nr
],
754 data
->temp_ext
[nr
]));
757 static ssize_t
show_temp_min(struct device
*dev
, struct device_attribute
*attr
,
760 int nr
= to_sensor_dev_attr(attr
)->index
;
761 struct lm85_data
*data
= lm85_update_device(dev
);
762 return sprintf(buf
, "%d\n", TEMP_FROM_REG(data
->temp_min
[nr
]));
765 static ssize_t
set_temp_min(struct device
*dev
, struct device_attribute
*attr
,
766 const char *buf
, size_t count
)
768 int nr
= to_sensor_dev_attr(attr
)->index
;
769 struct i2c_client
*client
= to_i2c_client(dev
);
770 struct lm85_data
*data
= i2c_get_clientdata(client
);
774 err
= kstrtol(buf
, 10, &val
);
778 if (IS_ADT7468_OFF64(data
))
781 mutex_lock(&data
->update_lock
);
782 data
->temp_min
[nr
] = TEMP_TO_REG(val
);
783 lm85_write_value(client
, LM85_REG_TEMP_MIN(nr
), data
->temp_min
[nr
]);
784 mutex_unlock(&data
->update_lock
);
788 static ssize_t
show_temp_max(struct device
*dev
, struct device_attribute
*attr
,
791 int nr
= to_sensor_dev_attr(attr
)->index
;
792 struct lm85_data
*data
= lm85_update_device(dev
);
793 return sprintf(buf
, "%d\n", TEMP_FROM_REG(data
->temp_max
[nr
]));
796 static ssize_t
set_temp_max(struct device
*dev
, struct device_attribute
*attr
,
797 const char *buf
, size_t count
)
799 int nr
= to_sensor_dev_attr(attr
)->index
;
800 struct i2c_client
*client
= to_i2c_client(dev
);
801 struct lm85_data
*data
= i2c_get_clientdata(client
);
805 err
= kstrtol(buf
, 10, &val
);
809 if (IS_ADT7468_OFF64(data
))
812 mutex_lock(&data
->update_lock
);
813 data
->temp_max
[nr
] = TEMP_TO_REG(val
);
814 lm85_write_value(client
, LM85_REG_TEMP_MAX(nr
), data
->temp_max
[nr
]);
815 mutex_unlock(&data
->update_lock
);
819 #define show_temp_reg(offset) \
820 static SENSOR_DEVICE_ATTR(temp##offset##_input, S_IRUGO, \
821 show_temp, NULL, offset - 1); \
822 static SENSOR_DEVICE_ATTR(temp##offset##_min, S_IRUGO | S_IWUSR, \
823 show_temp_min, set_temp_min, offset - 1); \
824 static SENSOR_DEVICE_ATTR(temp##offset##_max, S_IRUGO | S_IWUSR, \
825 show_temp_max, set_temp_max, offset - 1);
832 /* Automatic PWM control */
834 static ssize_t
show_pwm_auto_channels(struct device
*dev
,
835 struct device_attribute
*attr
, char *buf
)
837 int nr
= to_sensor_dev_attr(attr
)->index
;
838 struct lm85_data
*data
= lm85_update_device(dev
);
839 return sprintf(buf
, "%d\n", ZONE_FROM_REG(data
->autofan
[nr
].config
));
842 static ssize_t
set_pwm_auto_channels(struct device
*dev
,
843 struct device_attribute
*attr
, const char *buf
, size_t count
)
845 int nr
= to_sensor_dev_attr(attr
)->index
;
846 struct i2c_client
*client
= to_i2c_client(dev
);
847 struct lm85_data
*data
= i2c_get_clientdata(client
);
851 err
= kstrtol(buf
, 10, &val
);
855 mutex_lock(&data
->update_lock
);
856 data
->autofan
[nr
].config
= (data
->autofan
[nr
].config
& (~0xe0))
858 lm85_write_value(client
, LM85_REG_AFAN_CONFIG(nr
),
859 data
->autofan
[nr
].config
);
860 mutex_unlock(&data
->update_lock
);
864 static ssize_t
show_pwm_auto_pwm_min(struct device
*dev
,
865 struct device_attribute
*attr
, char *buf
)
867 int nr
= to_sensor_dev_attr(attr
)->index
;
868 struct lm85_data
*data
= lm85_update_device(dev
);
869 return sprintf(buf
, "%d\n", PWM_FROM_REG(data
->autofan
[nr
].min_pwm
));
872 static ssize_t
set_pwm_auto_pwm_min(struct device
*dev
,
873 struct device_attribute
*attr
, const char *buf
, size_t count
)
875 int nr
= to_sensor_dev_attr(attr
)->index
;
876 struct i2c_client
*client
= to_i2c_client(dev
);
877 struct lm85_data
*data
= i2c_get_clientdata(client
);
881 err
= kstrtoul(buf
, 10, &val
);
885 mutex_lock(&data
->update_lock
);
886 data
->autofan
[nr
].min_pwm
= PWM_TO_REG(val
);
887 lm85_write_value(client
, LM85_REG_AFAN_MINPWM(nr
),
888 data
->autofan
[nr
].min_pwm
);
889 mutex_unlock(&data
->update_lock
);
893 static ssize_t
show_pwm_auto_pwm_minctl(struct device
*dev
,
894 struct device_attribute
*attr
, char *buf
)
896 int nr
= to_sensor_dev_attr(attr
)->index
;
897 struct lm85_data
*data
= lm85_update_device(dev
);
898 return sprintf(buf
, "%d\n", data
->autofan
[nr
].min_off
);
901 static ssize_t
set_pwm_auto_pwm_minctl(struct device
*dev
,
902 struct device_attribute
*attr
, const char *buf
, size_t count
)
904 int nr
= to_sensor_dev_attr(attr
)->index
;
905 struct i2c_client
*client
= to_i2c_client(dev
);
906 struct lm85_data
*data
= i2c_get_clientdata(client
);
911 err
= kstrtol(buf
, 10, &val
);
915 mutex_lock(&data
->update_lock
);
916 data
->autofan
[nr
].min_off
= val
;
917 tmp
= lm85_read_value(client
, LM85_REG_AFAN_SPIKE1
);
918 tmp
&= ~(0x20 << nr
);
919 if (data
->autofan
[nr
].min_off
)
921 lm85_write_value(client
, LM85_REG_AFAN_SPIKE1
, tmp
);
922 mutex_unlock(&data
->update_lock
);
926 #define pwm_auto(offset) \
927 static SENSOR_DEVICE_ATTR(pwm##offset##_auto_channels, \
928 S_IRUGO | S_IWUSR, show_pwm_auto_channels, \
929 set_pwm_auto_channels, offset - 1); \
930 static SENSOR_DEVICE_ATTR(pwm##offset##_auto_pwm_min, \
931 S_IRUGO | S_IWUSR, show_pwm_auto_pwm_min, \
932 set_pwm_auto_pwm_min, offset - 1); \
933 static SENSOR_DEVICE_ATTR(pwm##offset##_auto_pwm_minctl, \
934 S_IRUGO | S_IWUSR, show_pwm_auto_pwm_minctl, \
935 set_pwm_auto_pwm_minctl, offset - 1)
941 /* Temperature settings for automatic PWM control */
943 static ssize_t
show_temp_auto_temp_off(struct device
*dev
,
944 struct device_attribute
*attr
, char *buf
)
946 int nr
= to_sensor_dev_attr(attr
)->index
;
947 struct lm85_data
*data
= lm85_update_device(dev
);
948 return sprintf(buf
, "%d\n", TEMP_FROM_REG(data
->zone
[nr
].limit
) -
949 HYST_FROM_REG(data
->zone
[nr
].hyst
));
952 static ssize_t
set_temp_auto_temp_off(struct device
*dev
,
953 struct device_attribute
*attr
, const char *buf
, size_t count
)
955 int nr
= to_sensor_dev_attr(attr
)->index
;
956 struct i2c_client
*client
= to_i2c_client(dev
);
957 struct lm85_data
*data
= i2c_get_clientdata(client
);
962 err
= kstrtol(buf
, 10, &val
);
966 mutex_lock(&data
->update_lock
);
967 min
= TEMP_FROM_REG(data
->zone
[nr
].limit
);
968 data
->zone
[nr
].hyst
= HYST_TO_REG(min
- val
);
969 if (nr
== 0 || nr
== 1) {
970 lm85_write_value(client
, LM85_REG_AFAN_HYST1
,
971 (data
->zone
[0].hyst
<< 4)
972 | data
->zone
[1].hyst
);
974 lm85_write_value(client
, LM85_REG_AFAN_HYST2
,
975 (data
->zone
[2].hyst
<< 4));
977 mutex_unlock(&data
->update_lock
);
981 static ssize_t
show_temp_auto_temp_min(struct device
*dev
,
982 struct device_attribute
*attr
, char *buf
)
984 int nr
= to_sensor_dev_attr(attr
)->index
;
985 struct lm85_data
*data
= lm85_update_device(dev
);
986 return sprintf(buf
, "%d\n", TEMP_FROM_REG(data
->zone
[nr
].limit
));
989 static ssize_t
set_temp_auto_temp_min(struct device
*dev
,
990 struct device_attribute
*attr
, const char *buf
, size_t count
)
992 int nr
= to_sensor_dev_attr(attr
)->index
;
993 struct i2c_client
*client
= to_i2c_client(dev
);
994 struct lm85_data
*data
= i2c_get_clientdata(client
);
998 err
= kstrtol(buf
, 10, &val
);
1002 mutex_lock(&data
->update_lock
);
1003 data
->zone
[nr
].limit
= TEMP_TO_REG(val
);
1004 lm85_write_value(client
, LM85_REG_AFAN_LIMIT(nr
),
1005 data
->zone
[nr
].limit
);
1007 /* Update temp_auto_max and temp_auto_range */
1008 data
->zone
[nr
].range
= RANGE_TO_REG(
1009 TEMP_FROM_REG(data
->zone
[nr
].max_desired
) -
1010 TEMP_FROM_REG(data
->zone
[nr
].limit
));
1011 lm85_write_value(client
, LM85_REG_AFAN_RANGE(nr
),
1012 ((data
->zone
[nr
].range
& 0x0f) << 4)
1013 | (data
->pwm_freq
[nr
] & 0x07));
1015 mutex_unlock(&data
->update_lock
);
1019 static ssize_t
show_temp_auto_temp_max(struct device
*dev
,
1020 struct device_attribute
*attr
, char *buf
)
1022 int nr
= to_sensor_dev_attr(attr
)->index
;
1023 struct lm85_data
*data
= lm85_update_device(dev
);
1024 return sprintf(buf
, "%d\n", TEMP_FROM_REG(data
->zone
[nr
].limit
) +
1025 RANGE_FROM_REG(data
->zone
[nr
].range
));
1028 static ssize_t
set_temp_auto_temp_max(struct device
*dev
,
1029 struct device_attribute
*attr
, const char *buf
, size_t count
)
1031 int nr
= to_sensor_dev_attr(attr
)->index
;
1032 struct i2c_client
*client
= to_i2c_client(dev
);
1033 struct lm85_data
*data
= i2c_get_clientdata(client
);
1038 err
= kstrtol(buf
, 10, &val
);
1042 mutex_lock(&data
->update_lock
);
1043 min
= TEMP_FROM_REG(data
->zone
[nr
].limit
);
1044 data
->zone
[nr
].max_desired
= TEMP_TO_REG(val
);
1045 data
->zone
[nr
].range
= RANGE_TO_REG(
1047 lm85_write_value(client
, LM85_REG_AFAN_RANGE(nr
),
1048 ((data
->zone
[nr
].range
& 0x0f) << 4)
1049 | (data
->pwm_freq
[nr
] & 0x07));
1050 mutex_unlock(&data
->update_lock
);
1054 static ssize_t
show_temp_auto_temp_crit(struct device
*dev
,
1055 struct device_attribute
*attr
, char *buf
)
1057 int nr
= to_sensor_dev_attr(attr
)->index
;
1058 struct lm85_data
*data
= lm85_update_device(dev
);
1059 return sprintf(buf
, "%d\n", TEMP_FROM_REG(data
->zone
[nr
].critical
));
1062 static ssize_t
set_temp_auto_temp_crit(struct device
*dev
,
1063 struct device_attribute
*attr
, const char *buf
, size_t count
)
1065 int nr
= to_sensor_dev_attr(attr
)->index
;
1066 struct i2c_client
*client
= to_i2c_client(dev
);
1067 struct lm85_data
*data
= i2c_get_clientdata(client
);
1071 err
= kstrtol(buf
, 10, &val
);
1075 mutex_lock(&data
->update_lock
);
1076 data
->zone
[nr
].critical
= TEMP_TO_REG(val
);
1077 lm85_write_value(client
, LM85_REG_AFAN_CRITICAL(nr
),
1078 data
->zone
[nr
].critical
);
1079 mutex_unlock(&data
->update_lock
);
1083 #define temp_auto(offset) \
1084 static SENSOR_DEVICE_ATTR(temp##offset##_auto_temp_off, \
1085 S_IRUGO | S_IWUSR, show_temp_auto_temp_off, \
1086 set_temp_auto_temp_off, offset - 1); \
1087 static SENSOR_DEVICE_ATTR(temp##offset##_auto_temp_min, \
1088 S_IRUGO | S_IWUSR, show_temp_auto_temp_min, \
1089 set_temp_auto_temp_min, offset - 1); \
1090 static SENSOR_DEVICE_ATTR(temp##offset##_auto_temp_max, \
1091 S_IRUGO | S_IWUSR, show_temp_auto_temp_max, \
1092 set_temp_auto_temp_max, offset - 1); \
1093 static SENSOR_DEVICE_ATTR(temp##offset##_auto_temp_crit, \
1094 S_IRUGO | S_IWUSR, show_temp_auto_temp_crit, \
1095 set_temp_auto_temp_crit, offset - 1);
1101 static struct attribute
*lm85_attributes
[] = {
1102 &sensor_dev_attr_fan1_input
.dev_attr
.attr
,
1103 &sensor_dev_attr_fan2_input
.dev_attr
.attr
,
1104 &sensor_dev_attr_fan3_input
.dev_attr
.attr
,
1105 &sensor_dev_attr_fan4_input
.dev_attr
.attr
,
1106 &sensor_dev_attr_fan1_min
.dev_attr
.attr
,
1107 &sensor_dev_attr_fan2_min
.dev_attr
.attr
,
1108 &sensor_dev_attr_fan3_min
.dev_attr
.attr
,
1109 &sensor_dev_attr_fan4_min
.dev_attr
.attr
,
1110 &sensor_dev_attr_fan1_alarm
.dev_attr
.attr
,
1111 &sensor_dev_attr_fan2_alarm
.dev_attr
.attr
,
1112 &sensor_dev_attr_fan3_alarm
.dev_attr
.attr
,
1113 &sensor_dev_attr_fan4_alarm
.dev_attr
.attr
,
1115 &sensor_dev_attr_pwm1
.dev_attr
.attr
,
1116 &sensor_dev_attr_pwm2
.dev_attr
.attr
,
1117 &sensor_dev_attr_pwm3
.dev_attr
.attr
,
1118 &sensor_dev_attr_pwm1_enable
.dev_attr
.attr
,
1119 &sensor_dev_attr_pwm2_enable
.dev_attr
.attr
,
1120 &sensor_dev_attr_pwm3_enable
.dev_attr
.attr
,
1121 &sensor_dev_attr_pwm1_freq
.dev_attr
.attr
,
1122 &sensor_dev_attr_pwm2_freq
.dev_attr
.attr
,
1123 &sensor_dev_attr_pwm3_freq
.dev_attr
.attr
,
1125 &sensor_dev_attr_in0_input
.dev_attr
.attr
,
1126 &sensor_dev_attr_in1_input
.dev_attr
.attr
,
1127 &sensor_dev_attr_in2_input
.dev_attr
.attr
,
1128 &sensor_dev_attr_in3_input
.dev_attr
.attr
,
1129 &sensor_dev_attr_in0_min
.dev_attr
.attr
,
1130 &sensor_dev_attr_in1_min
.dev_attr
.attr
,
1131 &sensor_dev_attr_in2_min
.dev_attr
.attr
,
1132 &sensor_dev_attr_in3_min
.dev_attr
.attr
,
1133 &sensor_dev_attr_in0_max
.dev_attr
.attr
,
1134 &sensor_dev_attr_in1_max
.dev_attr
.attr
,
1135 &sensor_dev_attr_in2_max
.dev_attr
.attr
,
1136 &sensor_dev_attr_in3_max
.dev_attr
.attr
,
1137 &sensor_dev_attr_in0_alarm
.dev_attr
.attr
,
1138 &sensor_dev_attr_in1_alarm
.dev_attr
.attr
,
1139 &sensor_dev_attr_in2_alarm
.dev_attr
.attr
,
1140 &sensor_dev_attr_in3_alarm
.dev_attr
.attr
,
1142 &sensor_dev_attr_temp1_input
.dev_attr
.attr
,
1143 &sensor_dev_attr_temp2_input
.dev_attr
.attr
,
1144 &sensor_dev_attr_temp3_input
.dev_attr
.attr
,
1145 &sensor_dev_attr_temp1_min
.dev_attr
.attr
,
1146 &sensor_dev_attr_temp2_min
.dev_attr
.attr
,
1147 &sensor_dev_attr_temp3_min
.dev_attr
.attr
,
1148 &sensor_dev_attr_temp1_max
.dev_attr
.attr
,
1149 &sensor_dev_attr_temp2_max
.dev_attr
.attr
,
1150 &sensor_dev_attr_temp3_max
.dev_attr
.attr
,
1151 &sensor_dev_attr_temp1_alarm
.dev_attr
.attr
,
1152 &sensor_dev_attr_temp2_alarm
.dev_attr
.attr
,
1153 &sensor_dev_attr_temp3_alarm
.dev_attr
.attr
,
1154 &sensor_dev_attr_temp1_fault
.dev_attr
.attr
,
1155 &sensor_dev_attr_temp3_fault
.dev_attr
.attr
,
1157 &sensor_dev_attr_pwm1_auto_channels
.dev_attr
.attr
,
1158 &sensor_dev_attr_pwm2_auto_channels
.dev_attr
.attr
,
1159 &sensor_dev_attr_pwm3_auto_channels
.dev_attr
.attr
,
1160 &sensor_dev_attr_pwm1_auto_pwm_min
.dev_attr
.attr
,
1161 &sensor_dev_attr_pwm2_auto_pwm_min
.dev_attr
.attr
,
1162 &sensor_dev_attr_pwm3_auto_pwm_min
.dev_attr
.attr
,
1164 &sensor_dev_attr_temp1_auto_temp_min
.dev_attr
.attr
,
1165 &sensor_dev_attr_temp2_auto_temp_min
.dev_attr
.attr
,
1166 &sensor_dev_attr_temp3_auto_temp_min
.dev_attr
.attr
,
1167 &sensor_dev_attr_temp1_auto_temp_max
.dev_attr
.attr
,
1168 &sensor_dev_attr_temp2_auto_temp_max
.dev_attr
.attr
,
1169 &sensor_dev_attr_temp3_auto_temp_max
.dev_attr
.attr
,
1170 &sensor_dev_attr_temp1_auto_temp_crit
.dev_attr
.attr
,
1171 &sensor_dev_attr_temp2_auto_temp_crit
.dev_attr
.attr
,
1172 &sensor_dev_attr_temp3_auto_temp_crit
.dev_attr
.attr
,
1175 &dev_attr_cpu0_vid
.attr
,
1176 &dev_attr_alarms
.attr
,
1180 static const struct attribute_group lm85_group
= {
1181 .attrs
= lm85_attributes
,
1184 static struct attribute
*lm85_attributes_minctl
[] = {
1185 &sensor_dev_attr_pwm1_auto_pwm_minctl
.dev_attr
.attr
,
1186 &sensor_dev_attr_pwm2_auto_pwm_minctl
.dev_attr
.attr
,
1187 &sensor_dev_attr_pwm3_auto_pwm_minctl
.dev_attr
.attr
,
1191 static const struct attribute_group lm85_group_minctl
= {
1192 .attrs
= lm85_attributes_minctl
,
1195 static struct attribute
*lm85_attributes_temp_off
[] = {
1196 &sensor_dev_attr_temp1_auto_temp_off
.dev_attr
.attr
,
1197 &sensor_dev_attr_temp2_auto_temp_off
.dev_attr
.attr
,
1198 &sensor_dev_attr_temp3_auto_temp_off
.dev_attr
.attr
,
1202 static const struct attribute_group lm85_group_temp_off
= {
1203 .attrs
= lm85_attributes_temp_off
,
1206 static struct attribute
*lm85_attributes_in4
[] = {
1207 &sensor_dev_attr_in4_input
.dev_attr
.attr
,
1208 &sensor_dev_attr_in4_min
.dev_attr
.attr
,
1209 &sensor_dev_attr_in4_max
.dev_attr
.attr
,
1210 &sensor_dev_attr_in4_alarm
.dev_attr
.attr
,
1214 static const struct attribute_group lm85_group_in4
= {
1215 .attrs
= lm85_attributes_in4
,
1218 static struct attribute
*lm85_attributes_in567
[] = {
1219 &sensor_dev_attr_in5_input
.dev_attr
.attr
,
1220 &sensor_dev_attr_in6_input
.dev_attr
.attr
,
1221 &sensor_dev_attr_in7_input
.dev_attr
.attr
,
1222 &sensor_dev_attr_in5_min
.dev_attr
.attr
,
1223 &sensor_dev_attr_in6_min
.dev_attr
.attr
,
1224 &sensor_dev_attr_in7_min
.dev_attr
.attr
,
1225 &sensor_dev_attr_in5_max
.dev_attr
.attr
,
1226 &sensor_dev_attr_in6_max
.dev_attr
.attr
,
1227 &sensor_dev_attr_in7_max
.dev_attr
.attr
,
1228 &sensor_dev_attr_in5_alarm
.dev_attr
.attr
,
1229 &sensor_dev_attr_in6_alarm
.dev_attr
.attr
,
1230 &sensor_dev_attr_in7_alarm
.dev_attr
.attr
,
1234 static const struct attribute_group lm85_group_in567
= {
1235 .attrs
= lm85_attributes_in567
,
1238 static void lm85_init_client(struct i2c_client
*client
)
1242 /* Start monitoring if needed */
1243 value
= lm85_read_value(client
, LM85_REG_CONFIG
);
1244 if (!(value
& 0x01)) {
1245 dev_info(&client
->dev
, "Starting monitoring\n");
1246 lm85_write_value(client
, LM85_REG_CONFIG
, value
| 0x01);
1249 /* Warn about unusual configuration bits */
1251 dev_warn(&client
->dev
, "Device configuration is locked\n");
1252 if (!(value
& 0x04))
1253 dev_warn(&client
->dev
, "Device is not ready\n");
1256 static int lm85_is_fake(struct i2c_client
*client
)
1259 * Differenciate between real LM96000 and Winbond WPCD377I. The latter
1260 * emulate the former except that it has no hardware monitoring function
1261 * so the readings are always 0.
1266 for (i
= 0; i
< 8; i
++) {
1267 in_temp
= i2c_smbus_read_byte_data(client
, 0x20 + i
);
1268 fan
= i2c_smbus_read_byte_data(client
, 0x28 + i
);
1269 if (in_temp
!= 0x00 || fan
!= 0xff)
1276 /* Return 0 if detection is successful, -ENODEV otherwise */
1277 static int lm85_detect(struct i2c_client
*client
, struct i2c_board_info
*info
)
1279 struct i2c_adapter
*adapter
= client
->adapter
;
1280 int address
= client
->addr
;
1281 const char *type_name
= NULL
;
1282 int company
, verstep
;
1284 if (!i2c_check_functionality(adapter
, I2C_FUNC_SMBUS_BYTE_DATA
)) {
1285 /* We need to be able to do byte I/O */
1289 /* Determine the chip type */
1290 company
= lm85_read_value(client
, LM85_REG_COMPANY
);
1291 verstep
= lm85_read_value(client
, LM85_REG_VERSTEP
);
1293 dev_dbg(&adapter
->dev
,
1294 "Detecting device at 0x%02x with COMPANY: 0x%02x and VERSTEP: 0x%02x\n",
1295 address
, company
, verstep
);
1297 if (company
== LM85_COMPANY_NATIONAL
) {
1299 case LM85_VERSTEP_LM85C
:
1300 type_name
= "lm85c";
1302 case LM85_VERSTEP_LM85B
:
1303 type_name
= "lm85b";
1305 case LM85_VERSTEP_LM96000_1
:
1306 case LM85_VERSTEP_LM96000_2
:
1307 /* Check for Winbond WPCD377I */
1308 if (lm85_is_fake(client
)) {
1309 dev_dbg(&adapter
->dev
,
1310 "Found Winbond WPCD377I, ignoring\n");
1316 } else if (company
== LM85_COMPANY_ANALOG_DEV
) {
1318 case LM85_VERSTEP_ADM1027
:
1319 type_name
= "adm1027";
1321 case LM85_VERSTEP_ADT7463
:
1322 case LM85_VERSTEP_ADT7463C
:
1323 type_name
= "adt7463";
1325 case LM85_VERSTEP_ADT7468_1
:
1326 case LM85_VERSTEP_ADT7468_2
:
1327 type_name
= "adt7468";
1330 } else if (company
== LM85_COMPANY_SMSC
) {
1332 case LM85_VERSTEP_EMC6D100_A0
:
1333 case LM85_VERSTEP_EMC6D100_A1
:
1334 /* Note: we can't tell a '100 from a '101 */
1335 type_name
= "emc6d100";
1337 case LM85_VERSTEP_EMC6D102
:
1338 type_name
= "emc6d102";
1340 case LM85_VERSTEP_EMC6D103_A0
:
1341 case LM85_VERSTEP_EMC6D103_A1
:
1342 type_name
= "emc6d103";
1344 case LM85_VERSTEP_EMC6D103S
:
1345 type_name
= "emc6d103s";
1353 strlcpy(info
->type
, type_name
, I2C_NAME_SIZE
);
1358 static void lm85_remove_files(struct i2c_client
*client
, struct lm85_data
*data
)
1360 sysfs_remove_group(&client
->dev
.kobj
, &lm85_group
);
1361 if (data
->type
!= emc6d103s
) {
1362 sysfs_remove_group(&client
->dev
.kobj
, &lm85_group_minctl
);
1363 sysfs_remove_group(&client
->dev
.kobj
, &lm85_group_temp_off
);
1365 if (!data
->has_vid5
)
1366 sysfs_remove_group(&client
->dev
.kobj
, &lm85_group_in4
);
1367 if (data
->type
== emc6d100
)
1368 sysfs_remove_group(&client
->dev
.kobj
, &lm85_group_in567
);
1371 static int lm85_probe(struct i2c_client
*client
,
1372 const struct i2c_device_id
*id
)
1374 struct lm85_data
*data
;
1377 data
= devm_kzalloc(&client
->dev
, sizeof(struct lm85_data
), GFP_KERNEL
);
1381 i2c_set_clientdata(client
, data
);
1382 data
->type
= id
->driver_data
;
1383 mutex_init(&data
->update_lock
);
1385 /* Fill in the chip specific driver values */
1386 switch (data
->type
) {
1394 data
->freq_map
= adm1027_freq_map
;
1397 data
->freq_map
= lm85_freq_map
;
1400 /* Set the VRM version */
1401 data
->vrm
= vid_which_vrm();
1403 /* Initialize the LM85 chip */
1404 lm85_init_client(client
);
1406 /* Register sysfs hooks */
1407 err
= sysfs_create_group(&client
->dev
.kobj
, &lm85_group
);
1411 /* minctl and temp_off exist on all chips except emc6d103s */
1412 if (data
->type
!= emc6d103s
) {
1413 err
= sysfs_create_group(&client
->dev
.kobj
, &lm85_group_minctl
);
1415 goto err_remove_files
;
1416 err
= sysfs_create_group(&client
->dev
.kobj
,
1417 &lm85_group_temp_off
);
1419 goto err_remove_files
;
1423 * The ADT7463/68 have an optional VRM 10 mode where pin 21 is used
1424 * as a sixth digital VID input rather than an analog input.
1426 if (data
->type
== adt7463
|| data
->type
== adt7468
) {
1427 u8 vid
= lm85_read_value(client
, LM85_REG_VID
);
1429 data
->has_vid5
= true;
1432 if (!data
->has_vid5
) {
1433 err
= sysfs_create_group(&client
->dev
.kobj
, &lm85_group_in4
);
1435 goto err_remove_files
;
1438 /* The EMC6D100 has 3 additional voltage inputs */
1439 if (data
->type
== emc6d100
) {
1440 err
= sysfs_create_group(&client
->dev
.kobj
, &lm85_group_in567
);
1442 goto err_remove_files
;
1445 data
->hwmon_dev
= hwmon_device_register(&client
->dev
);
1446 if (IS_ERR(data
->hwmon_dev
)) {
1447 err
= PTR_ERR(data
->hwmon_dev
);
1448 goto err_remove_files
;
1453 /* Error out and cleanup code */
1455 lm85_remove_files(client
, data
);
1459 static int lm85_remove(struct i2c_client
*client
)
1461 struct lm85_data
*data
= i2c_get_clientdata(client
);
1462 hwmon_device_unregister(data
->hwmon_dev
);
1463 lm85_remove_files(client
, data
);
1468 static int lm85_read_value(struct i2c_client
*client
, u8 reg
)
1472 /* What size location is it? */
1474 case LM85_REG_FAN(0): /* Read WORD data */
1475 case LM85_REG_FAN(1):
1476 case LM85_REG_FAN(2):
1477 case LM85_REG_FAN(3):
1478 case LM85_REG_FAN_MIN(0):
1479 case LM85_REG_FAN_MIN(1):
1480 case LM85_REG_FAN_MIN(2):
1481 case LM85_REG_FAN_MIN(3):
1482 case LM85_REG_ALARM1
: /* Read both bytes at once */
1483 res
= i2c_smbus_read_byte_data(client
, reg
) & 0xff;
1484 res
|= i2c_smbus_read_byte_data(client
, reg
+ 1) << 8;
1486 default: /* Read BYTE data */
1487 res
= i2c_smbus_read_byte_data(client
, reg
);
1494 static void lm85_write_value(struct i2c_client
*client
, u8 reg
, int value
)
1497 case LM85_REG_FAN(0): /* Write WORD data */
1498 case LM85_REG_FAN(1):
1499 case LM85_REG_FAN(2):
1500 case LM85_REG_FAN(3):
1501 case LM85_REG_FAN_MIN(0):
1502 case LM85_REG_FAN_MIN(1):
1503 case LM85_REG_FAN_MIN(2):
1504 case LM85_REG_FAN_MIN(3):
1505 /* NOTE: ALARM is read only, so not included here */
1506 i2c_smbus_write_byte_data(client
, reg
, value
& 0xff);
1507 i2c_smbus_write_byte_data(client
, reg
+ 1, value
>> 8);
1509 default: /* Write BYTE data */
1510 i2c_smbus_write_byte_data(client
, reg
, value
);
1515 static struct lm85_data
*lm85_update_device(struct device
*dev
)
1517 struct i2c_client
*client
= to_i2c_client(dev
);
1518 struct lm85_data
*data
= i2c_get_clientdata(client
);
1521 mutex_lock(&data
->update_lock
);
1524 time_after(jiffies
, data
->last_reading
+ LM85_DATA_INTERVAL
)) {
1525 /* Things that change quickly */
1526 dev_dbg(&client
->dev
, "Reading sensor values\n");
1529 * Have to read extended bits first to "freeze" the
1530 * more significant bits that are read later.
1531 * There are 2 additional resolution bits per channel and we
1532 * have room for 4, so we shift them to the left.
1534 if (data
->type
== adm1027
|| data
->type
== adt7463
||
1535 data
->type
== adt7468
) {
1536 int ext1
= lm85_read_value(client
,
1537 ADM1027_REG_EXTEND_ADC1
);
1538 int ext2
= lm85_read_value(client
,
1539 ADM1027_REG_EXTEND_ADC2
);
1540 int val
= (ext1
<< 8) + ext2
;
1542 for (i
= 0; i
<= 4; i
++)
1544 ((val
>> (i
* 2)) & 0x03) << 2;
1546 for (i
= 0; i
<= 2; i
++)
1548 (val
>> ((i
+ 4) * 2)) & 0x0c;
1551 data
->vid
= lm85_read_value(client
, LM85_REG_VID
);
1553 for (i
= 0; i
<= 3; ++i
) {
1555 lm85_read_value(client
, LM85_REG_IN(i
));
1557 lm85_read_value(client
, LM85_REG_FAN(i
));
1560 if (!data
->has_vid5
)
1561 data
->in
[4] = lm85_read_value(client
, LM85_REG_IN(4));
1563 if (data
->type
== adt7468
)
1564 data
->cfg5
= lm85_read_value(client
, ADT7468_REG_CFG5
);
1566 for (i
= 0; i
<= 2; ++i
) {
1568 lm85_read_value(client
, LM85_REG_TEMP(i
));
1570 lm85_read_value(client
, LM85_REG_PWM(i
));
1572 if (IS_ADT7468_OFF64(data
))
1573 data
->temp
[i
] -= 64;
1576 data
->alarms
= lm85_read_value(client
, LM85_REG_ALARM1
);
1578 if (data
->type
== emc6d100
) {
1579 /* Three more voltage sensors */
1580 for (i
= 5; i
<= 7; ++i
) {
1581 data
->in
[i
] = lm85_read_value(client
,
1582 EMC6D100_REG_IN(i
));
1584 /* More alarm bits */
1585 data
->alarms
|= lm85_read_value(client
,
1586 EMC6D100_REG_ALARM3
) << 16;
1587 } else if (data
->type
== emc6d102
|| data
->type
== emc6d103
||
1588 data
->type
== emc6d103s
) {
1590 * Have to read LSB bits after the MSB ones because
1591 * the reading of the MSB bits has frozen the
1592 * LSBs (backward from the ADM1027).
1594 int ext1
= lm85_read_value(client
,
1595 EMC6D102_REG_EXTEND_ADC1
);
1596 int ext2
= lm85_read_value(client
,
1597 EMC6D102_REG_EXTEND_ADC2
);
1598 int ext3
= lm85_read_value(client
,
1599 EMC6D102_REG_EXTEND_ADC3
);
1600 int ext4
= lm85_read_value(client
,
1601 EMC6D102_REG_EXTEND_ADC4
);
1602 data
->in_ext
[0] = ext3
& 0x0f;
1603 data
->in_ext
[1] = ext4
& 0x0f;
1604 data
->in_ext
[2] = ext4
>> 4;
1605 data
->in_ext
[3] = ext3
>> 4;
1606 data
->in_ext
[4] = ext2
>> 4;
1608 data
->temp_ext
[0] = ext1
& 0x0f;
1609 data
->temp_ext
[1] = ext2
& 0x0f;
1610 data
->temp_ext
[2] = ext1
>> 4;
1613 data
->last_reading
= jiffies
;
1614 } /* last_reading */
1617 time_after(jiffies
, data
->last_config
+ LM85_CONFIG_INTERVAL
)) {
1618 /* Things that don't change often */
1619 dev_dbg(&client
->dev
, "Reading config values\n");
1621 for (i
= 0; i
<= 3; ++i
) {
1623 lm85_read_value(client
, LM85_REG_IN_MIN(i
));
1625 lm85_read_value(client
, LM85_REG_IN_MAX(i
));
1627 lm85_read_value(client
, LM85_REG_FAN_MIN(i
));
1630 if (!data
->has_vid5
) {
1631 data
->in_min
[4] = lm85_read_value(client
,
1632 LM85_REG_IN_MIN(4));
1633 data
->in_max
[4] = lm85_read_value(client
,
1634 LM85_REG_IN_MAX(4));
1637 if (data
->type
== emc6d100
) {
1638 for (i
= 5; i
<= 7; ++i
) {
1639 data
->in_min
[i
] = lm85_read_value(client
,
1640 EMC6D100_REG_IN_MIN(i
));
1641 data
->in_max
[i
] = lm85_read_value(client
,
1642 EMC6D100_REG_IN_MAX(i
));
1646 for (i
= 0; i
<= 2; ++i
) {
1650 lm85_read_value(client
, LM85_REG_TEMP_MIN(i
));
1652 lm85_read_value(client
, LM85_REG_TEMP_MAX(i
));
1654 data
->autofan
[i
].config
=
1655 lm85_read_value(client
, LM85_REG_AFAN_CONFIG(i
));
1656 val
= lm85_read_value(client
, LM85_REG_AFAN_RANGE(i
));
1657 data
->pwm_freq
[i
] = val
& 0x07;
1658 data
->zone
[i
].range
= val
>> 4;
1659 data
->autofan
[i
].min_pwm
=
1660 lm85_read_value(client
, LM85_REG_AFAN_MINPWM(i
));
1661 data
->zone
[i
].limit
=
1662 lm85_read_value(client
, LM85_REG_AFAN_LIMIT(i
));
1663 data
->zone
[i
].critical
=
1664 lm85_read_value(client
, LM85_REG_AFAN_CRITICAL(i
));
1666 if (IS_ADT7468_OFF64(data
)) {
1667 data
->temp_min
[i
] -= 64;
1668 data
->temp_max
[i
] -= 64;
1669 data
->zone
[i
].limit
-= 64;
1670 data
->zone
[i
].critical
-= 64;
1674 if (data
->type
!= emc6d103s
) {
1675 i
= lm85_read_value(client
, LM85_REG_AFAN_SPIKE1
);
1676 data
->autofan
[0].min_off
= (i
& 0x20) != 0;
1677 data
->autofan
[1].min_off
= (i
& 0x40) != 0;
1678 data
->autofan
[2].min_off
= (i
& 0x80) != 0;
1680 i
= lm85_read_value(client
, LM85_REG_AFAN_HYST1
);
1681 data
->zone
[0].hyst
= i
>> 4;
1682 data
->zone
[1].hyst
= i
& 0x0f;
1684 i
= lm85_read_value(client
, LM85_REG_AFAN_HYST2
);
1685 data
->zone
[2].hyst
= i
>> 4;
1688 data
->last_config
= jiffies
;
1693 mutex_unlock(&data
->update_lock
);
1698 module_i2c_driver(lm85_driver
);
1700 MODULE_LICENSE("GPL");
1701 MODULE_AUTHOR("Philip Pokorny <ppokorny@penguincomputing.com>, "
1702 "Margit Schubert-While <margitsw@t-online.de>, "
1703 "Justin Thiessen <jthiessen@penguincomputing.com>");
1704 MODULE_DESCRIPTION("LM85-B, LM85-C driver");