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>
37 #include <linux/util_macros.h>
39 /* Addresses to scan */
40 static const unsigned short normal_i2c
[] = { 0x2c, 0x2d, 0x2e, I2C_CLIENT_END
};
44 adm1027
, adt7463
, adt7468
,
45 emc6d100
, emc6d102
, emc6d103
, emc6d103s
48 /* The LM85 registers */
50 #define LM85_REG_IN(nr) (0x20 + (nr))
51 #define LM85_REG_IN_MIN(nr) (0x44 + (nr) * 2)
52 #define LM85_REG_IN_MAX(nr) (0x45 + (nr) * 2)
54 #define LM85_REG_TEMP(nr) (0x25 + (nr))
55 #define LM85_REG_TEMP_MIN(nr) (0x4e + (nr) * 2)
56 #define LM85_REG_TEMP_MAX(nr) (0x4f + (nr) * 2)
58 /* Fan speeds are LSB, MSB (2 bytes) */
59 #define LM85_REG_FAN(nr) (0x28 + (nr) * 2)
60 #define LM85_REG_FAN_MIN(nr) (0x54 + (nr) * 2)
62 #define LM85_REG_PWM(nr) (0x30 + (nr))
64 #define LM85_REG_COMPANY 0x3e
65 #define LM85_REG_VERSTEP 0x3f
67 #define ADT7468_REG_CFG5 0x7c
68 #define ADT7468_OFF64 (1 << 0)
69 #define ADT7468_HFPWM (1 << 1)
70 #define IS_ADT7468_OFF64(data) \
71 ((data)->type == adt7468 && !((data)->cfg5 & ADT7468_OFF64))
72 #define IS_ADT7468_HFPWM(data) \
73 ((data)->type == adt7468 && !((data)->cfg5 & ADT7468_HFPWM))
75 /* These are the recognized values for the above regs */
76 #define LM85_COMPANY_NATIONAL 0x01
77 #define LM85_COMPANY_ANALOG_DEV 0x41
78 #define LM85_COMPANY_SMSC 0x5c
79 #define LM85_VERSTEP_LM85C 0x60
80 #define LM85_VERSTEP_LM85B 0x62
81 #define LM85_VERSTEP_LM96000_1 0x68
82 #define LM85_VERSTEP_LM96000_2 0x69
83 #define LM85_VERSTEP_ADM1027 0x60
84 #define LM85_VERSTEP_ADT7463 0x62
85 #define LM85_VERSTEP_ADT7463C 0x6A
86 #define LM85_VERSTEP_ADT7468_1 0x71
87 #define LM85_VERSTEP_ADT7468_2 0x72
88 #define LM85_VERSTEP_EMC6D100_A0 0x60
89 #define LM85_VERSTEP_EMC6D100_A1 0x61
90 #define LM85_VERSTEP_EMC6D102 0x65
91 #define LM85_VERSTEP_EMC6D103_A0 0x68
92 #define LM85_VERSTEP_EMC6D103_A1 0x69
93 #define LM85_VERSTEP_EMC6D103S 0x6A /* Also known as EMC6D103:A2 */
95 #define LM85_REG_CONFIG 0x40
97 #define LM85_REG_ALARM1 0x41
98 #define LM85_REG_ALARM2 0x42
100 #define LM85_REG_VID 0x43
102 /* Automated FAN control */
103 #define LM85_REG_AFAN_CONFIG(nr) (0x5c + (nr))
104 #define LM85_REG_AFAN_RANGE(nr) (0x5f + (nr))
105 #define LM85_REG_AFAN_SPIKE1 0x62
106 #define LM85_REG_AFAN_MINPWM(nr) (0x64 + (nr))
107 #define LM85_REG_AFAN_LIMIT(nr) (0x67 + (nr))
108 #define LM85_REG_AFAN_CRITICAL(nr) (0x6a + (nr))
109 #define LM85_REG_AFAN_HYST1 0x6d
110 #define LM85_REG_AFAN_HYST2 0x6e
112 #define ADM1027_REG_EXTEND_ADC1 0x76
113 #define ADM1027_REG_EXTEND_ADC2 0x77
115 #define EMC6D100_REG_ALARM3 0x7d
116 /* IN5, IN6 and IN7 */
117 #define EMC6D100_REG_IN(nr) (0x70 + ((nr) - 5))
118 #define EMC6D100_REG_IN_MIN(nr) (0x73 + ((nr) - 5) * 2)
119 #define EMC6D100_REG_IN_MAX(nr) (0x74 + ((nr) - 5) * 2)
120 #define EMC6D102_REG_EXTEND_ADC1 0x85
121 #define EMC6D102_REG_EXTEND_ADC2 0x86
122 #define EMC6D102_REG_EXTEND_ADC3 0x87
123 #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 SCALE(clamp_val(val, 0, 255 * lm85_scaling[n] / 192), \
140 lm85_scaling[n], 192)
142 #define INSEXT_FROM_REG(n, val, ext) \
143 SCALE(((val) << 4) + (ext), 192 << 4, lm85_scaling[n])
145 #define INS_FROM_REG(n, val) SCALE((val), 192, lm85_scaling[n])
147 /* FAN speed is measured using 90kHz clock */
148 static inline u16
FAN_TO_REG(unsigned long val
)
152 return clamp_val(5400000 / val
, 1, 0xfffe);
154 #define FAN_FROM_REG(val) ((val) == 0 ? -1 : (val) == 0xffff ? 0 : \
157 /* Temperature is reported in .001 degC increments */
158 #define TEMP_TO_REG(val) \
159 DIV_ROUND_CLOSEST(clamp_val((val), -127000, 127000), 1000)
160 #define TEMPEXT_FROM_REG(val, ext) \
161 SCALE(((val) << 4) + (ext), 16, 1000)
162 #define TEMP_FROM_REG(val) ((val) * 1000)
164 #define PWM_TO_REG(val) clamp_val(val, 0, 255)
165 #define PWM_FROM_REG(val) (val)
169 * ZONEs have the following parameters:
170 * Limit (low) temp, 1. degC
171 * Hysteresis (below limit), 1. degC (0-15)
172 * Range of speed control, .1 degC (2-80)
173 * Critical (high) temp, 1. degC
175 * FAN PWMs have the following parameters:
176 * Reference Zone, 1, 2, 3, etc.
177 * Spinup time, .05 sec
178 * PWM value at limit/low temp, 1 count
179 * PWM Frequency, 1. Hz
180 * PWM is Min or OFF below limit, flag
181 * Invert PWM output, flag
183 * Some chips filter the temp, others the fan.
184 * Filter constant (or disabled) .1 seconds
187 /* These are the zone temperature range encodings in .001 degree C */
188 static const int lm85_range_map
[] = {
189 2000, 2500, 3300, 4000, 5000, 6600, 8000, 10000,
190 13300, 16000, 20000, 26600, 32000, 40000, 53300, 80000
193 static int RANGE_TO_REG(long range
)
195 return find_closest(range
, lm85_range_map
, ARRAY_SIZE(lm85_range_map
));
197 #define RANGE_FROM_REG(val) lm85_range_map[(val) & 0x0f]
199 /* These are the PWM frequency encodings */
200 static const int lm85_freq_map
[8] = { /* 1 Hz */
201 10, 15, 23, 30, 38, 47, 61, 94
203 static const int adm1027_freq_map
[8] = { /* 1 Hz */
204 11, 15, 22, 29, 35, 44, 59, 88
206 #define FREQ_MAP_LEN 8
208 static int FREQ_TO_REG(const int *map
,
209 unsigned int map_size
, unsigned long freq
)
211 return find_closest(freq
, map
, map_size
);
214 static int FREQ_FROM_REG(const int *map
, u8 reg
)
216 return map
[reg
& 0x07];
220 * Since we can't use strings, I'm abusing these numbers
221 * to stand in for the following meanings:
222 * 1 -- PWM responds to Zone 1
223 * 2 -- PWM responds to Zone 2
224 * 3 -- PWM responds to Zone 3
225 * 23 -- PWM responds to the higher temp of Zone 2 or 3
226 * 123 -- PWM responds to highest of Zone 1, 2, or 3
227 * 0 -- PWM is always at 0% (ie, off)
228 * -1 -- PWM is always at 100%
229 * -2 -- PWM responds to manual control
232 static const int lm85_zone_map
[] = { 1, 2, 3, -1, 0, 23, 123, -2 };
233 #define ZONE_FROM_REG(val) lm85_zone_map[(val) >> 5]
235 static int ZONE_TO_REG(int zone
)
239 for (i
= 0; i
<= 7; ++i
)
240 if (zone
== lm85_zone_map
[i
])
242 if (i
> 7) /* Not found. */
243 i
= 3; /* Always 100% */
247 #define HYST_TO_REG(val) clamp_val(((val) + 500) / 1000, 0, 15)
248 #define HYST_FROM_REG(val) ((val) * 1000)
251 * Chip sampling rates
253 * Some sensors are not updated more frequently than once per second
254 * so it doesn't make sense to read them more often than that.
255 * We cache the results and return the saved data if the driver
256 * is called again before a second has elapsed.
258 * Also, there is significant configuration data for this chip
259 * given the automatic PWM fan control that is possible. There
260 * are about 47 bytes of config data to only 22 bytes of actual
261 * readings. So, we keep the config data up to date in the cache
262 * when it is written and only sample it once every 1 *minute*
264 #define LM85_DATA_INTERVAL (HZ + HZ / 2)
265 #define LM85_CONFIG_INTERVAL (1 * 60 * HZ)
268 * LM85 can automatically adjust fan speeds based on temperature
269 * This structure encapsulates an entire Zone config. There are
270 * three zones (one for each temperature input) on the lm85
273 s8 limit
; /* Low temp limit */
274 u8 hyst
; /* Low limit hysteresis. (0-15) */
275 u8 range
; /* Temp range, encoded */
276 s8 critical
; /* "All fans ON" temp limit */
278 * Actual "max" temperature specified. Preserved
279 * to prevent "drift" as other autofan control
284 struct lm85_autofan
{
285 u8 config
; /* Register value */
286 u8 min_pwm
; /* Minimum PWM value, encoded */
287 u8 min_off
; /* Min PWM or OFF below "limit", flag */
291 * For each registered chip, we need to keep some data in memory.
292 * The structure is dynamically allocated.
295 struct i2c_client
*client
;
296 const struct attribute_group
*groups
[6];
300 bool has_vid5
; /* true if VID5 is configured for ADT7463 or ADT7468 */
302 struct mutex update_lock
;
303 int valid
; /* !=0 if following fields are valid */
304 unsigned long last_reading
; /* In jiffies */
305 unsigned long last_config
; /* In jiffies */
307 u8 in
[8]; /* Register value */
308 u8 in_max
[8]; /* Register value */
309 u8 in_min
[8]; /* Register value */
310 s8 temp
[3]; /* Register value */
311 s8 temp_min
[3]; /* Register value */
312 s8 temp_max
[3]; /* Register value */
313 u16 fan
[4]; /* Register value */
314 u16 fan_min
[4]; /* Register value */
315 u8 pwm
[3]; /* Register value */
316 u8 pwm_freq
[3]; /* Register encoding */
317 u8 temp_ext
[3]; /* Decoded values */
318 u8 in_ext
[8]; /* Decoded values */
319 u8 vid
; /* Register value */
320 u8 vrm
; /* VRM version */
321 u32 alarms
; /* Register encoding, combined */
322 u8 cfg5
; /* Config Register 5 on ADT7468 */
323 struct lm85_autofan autofan
[3];
324 struct lm85_zone zone
[3];
327 static int lm85_read_value(struct i2c_client
*client
, u8 reg
)
331 /* What size location is it? */
333 case LM85_REG_FAN(0): /* Read WORD data */
334 case LM85_REG_FAN(1):
335 case LM85_REG_FAN(2):
336 case LM85_REG_FAN(3):
337 case LM85_REG_FAN_MIN(0):
338 case LM85_REG_FAN_MIN(1):
339 case LM85_REG_FAN_MIN(2):
340 case LM85_REG_FAN_MIN(3):
341 case LM85_REG_ALARM1
: /* Read both bytes at once */
342 res
= i2c_smbus_read_byte_data(client
, reg
) & 0xff;
343 res
|= i2c_smbus_read_byte_data(client
, reg
+ 1) << 8;
345 default: /* Read BYTE data */
346 res
= i2c_smbus_read_byte_data(client
, reg
);
353 static void lm85_write_value(struct i2c_client
*client
, u8 reg
, int value
)
356 case LM85_REG_FAN(0): /* Write WORD data */
357 case LM85_REG_FAN(1):
358 case LM85_REG_FAN(2):
359 case LM85_REG_FAN(3):
360 case LM85_REG_FAN_MIN(0):
361 case LM85_REG_FAN_MIN(1):
362 case LM85_REG_FAN_MIN(2):
363 case LM85_REG_FAN_MIN(3):
364 /* NOTE: ALARM is read only, so not included here */
365 i2c_smbus_write_byte_data(client
, reg
, value
& 0xff);
366 i2c_smbus_write_byte_data(client
, reg
+ 1, value
>> 8);
368 default: /* Write BYTE data */
369 i2c_smbus_write_byte_data(client
, reg
, value
);
374 static struct lm85_data
*lm85_update_device(struct device
*dev
)
376 struct lm85_data
*data
= dev_get_drvdata(dev
);
377 struct i2c_client
*client
= data
->client
;
380 mutex_lock(&data
->update_lock
);
383 time_after(jiffies
, data
->last_reading
+ LM85_DATA_INTERVAL
)) {
384 /* Things that change quickly */
385 dev_dbg(&client
->dev
, "Reading sensor values\n");
388 * Have to read extended bits first to "freeze" the
389 * more significant bits that are read later.
390 * There are 2 additional resolution bits per channel and we
391 * have room for 4, so we shift them to the left.
393 if (data
->type
== adm1027
|| data
->type
== adt7463
||
394 data
->type
== adt7468
) {
395 int ext1
= lm85_read_value(client
,
396 ADM1027_REG_EXTEND_ADC1
);
397 int ext2
= lm85_read_value(client
,
398 ADM1027_REG_EXTEND_ADC2
);
399 int val
= (ext1
<< 8) + ext2
;
401 for (i
= 0; i
<= 4; i
++)
403 ((val
>> (i
* 2)) & 0x03) << 2;
405 for (i
= 0; i
<= 2; i
++)
407 (val
>> ((i
+ 4) * 2)) & 0x0c;
410 data
->vid
= lm85_read_value(client
, LM85_REG_VID
);
412 for (i
= 0; i
<= 3; ++i
) {
414 lm85_read_value(client
, LM85_REG_IN(i
));
416 lm85_read_value(client
, LM85_REG_FAN(i
));
420 data
->in
[4] = lm85_read_value(client
, LM85_REG_IN(4));
422 if (data
->type
== adt7468
)
423 data
->cfg5
= lm85_read_value(client
, ADT7468_REG_CFG5
);
425 for (i
= 0; i
<= 2; ++i
) {
427 lm85_read_value(client
, LM85_REG_TEMP(i
));
429 lm85_read_value(client
, LM85_REG_PWM(i
));
431 if (IS_ADT7468_OFF64(data
))
435 data
->alarms
= lm85_read_value(client
, LM85_REG_ALARM1
);
437 if (data
->type
== emc6d100
) {
438 /* Three more voltage sensors */
439 for (i
= 5; i
<= 7; ++i
) {
440 data
->in
[i
] = lm85_read_value(client
,
443 /* More alarm bits */
444 data
->alarms
|= lm85_read_value(client
,
445 EMC6D100_REG_ALARM3
) << 16;
446 } else if (data
->type
== emc6d102
|| data
->type
== emc6d103
||
447 data
->type
== emc6d103s
) {
449 * Have to read LSB bits after the MSB ones because
450 * the reading of the MSB bits has frozen the
451 * LSBs (backward from the ADM1027).
453 int ext1
= lm85_read_value(client
,
454 EMC6D102_REG_EXTEND_ADC1
);
455 int ext2
= lm85_read_value(client
,
456 EMC6D102_REG_EXTEND_ADC2
);
457 int ext3
= lm85_read_value(client
,
458 EMC6D102_REG_EXTEND_ADC3
);
459 int ext4
= lm85_read_value(client
,
460 EMC6D102_REG_EXTEND_ADC4
);
461 data
->in_ext
[0] = ext3
& 0x0f;
462 data
->in_ext
[1] = ext4
& 0x0f;
463 data
->in_ext
[2] = ext4
>> 4;
464 data
->in_ext
[3] = ext3
>> 4;
465 data
->in_ext
[4] = ext2
>> 4;
467 data
->temp_ext
[0] = ext1
& 0x0f;
468 data
->temp_ext
[1] = ext2
& 0x0f;
469 data
->temp_ext
[2] = ext1
>> 4;
472 data
->last_reading
= jiffies
;
476 time_after(jiffies
, data
->last_config
+ LM85_CONFIG_INTERVAL
)) {
477 /* Things that don't change often */
478 dev_dbg(&client
->dev
, "Reading config values\n");
480 for (i
= 0; i
<= 3; ++i
) {
482 lm85_read_value(client
, LM85_REG_IN_MIN(i
));
484 lm85_read_value(client
, LM85_REG_IN_MAX(i
));
486 lm85_read_value(client
, LM85_REG_FAN_MIN(i
));
489 if (!data
->has_vid5
) {
490 data
->in_min
[4] = lm85_read_value(client
,
492 data
->in_max
[4] = lm85_read_value(client
,
496 if (data
->type
== emc6d100
) {
497 for (i
= 5; i
<= 7; ++i
) {
498 data
->in_min
[i
] = lm85_read_value(client
,
499 EMC6D100_REG_IN_MIN(i
));
500 data
->in_max
[i
] = lm85_read_value(client
,
501 EMC6D100_REG_IN_MAX(i
));
505 for (i
= 0; i
<= 2; ++i
) {
509 lm85_read_value(client
, LM85_REG_TEMP_MIN(i
));
511 lm85_read_value(client
, LM85_REG_TEMP_MAX(i
));
513 data
->autofan
[i
].config
=
514 lm85_read_value(client
, LM85_REG_AFAN_CONFIG(i
));
515 val
= lm85_read_value(client
, LM85_REG_AFAN_RANGE(i
));
516 data
->pwm_freq
[i
] = val
& 0x07;
517 data
->zone
[i
].range
= val
>> 4;
518 data
->autofan
[i
].min_pwm
=
519 lm85_read_value(client
, LM85_REG_AFAN_MINPWM(i
));
520 data
->zone
[i
].limit
=
521 lm85_read_value(client
, LM85_REG_AFAN_LIMIT(i
));
522 data
->zone
[i
].critical
=
523 lm85_read_value(client
, LM85_REG_AFAN_CRITICAL(i
));
525 if (IS_ADT7468_OFF64(data
)) {
526 data
->temp_min
[i
] -= 64;
527 data
->temp_max
[i
] -= 64;
528 data
->zone
[i
].limit
-= 64;
529 data
->zone
[i
].critical
-= 64;
533 if (data
->type
!= emc6d103s
) {
534 i
= lm85_read_value(client
, LM85_REG_AFAN_SPIKE1
);
535 data
->autofan
[0].min_off
= (i
& 0x20) != 0;
536 data
->autofan
[1].min_off
= (i
& 0x40) != 0;
537 data
->autofan
[2].min_off
= (i
& 0x80) != 0;
539 i
= lm85_read_value(client
, LM85_REG_AFAN_HYST1
);
540 data
->zone
[0].hyst
= i
>> 4;
541 data
->zone
[1].hyst
= i
& 0x0f;
543 i
= lm85_read_value(client
, LM85_REG_AFAN_HYST2
);
544 data
->zone
[2].hyst
= i
>> 4;
547 data
->last_config
= jiffies
;
552 mutex_unlock(&data
->update_lock
);
558 static ssize_t
show_fan(struct device
*dev
, struct device_attribute
*attr
,
561 int nr
= to_sensor_dev_attr(attr
)->index
;
562 struct lm85_data
*data
= lm85_update_device(dev
);
563 return sprintf(buf
, "%d\n", FAN_FROM_REG(data
->fan
[nr
]));
566 static ssize_t
show_fan_min(struct device
*dev
, struct device_attribute
*attr
,
569 int nr
= to_sensor_dev_attr(attr
)->index
;
570 struct lm85_data
*data
= lm85_update_device(dev
);
571 return sprintf(buf
, "%d\n", FAN_FROM_REG(data
->fan_min
[nr
]));
574 static ssize_t
set_fan_min(struct device
*dev
, struct device_attribute
*attr
,
575 const char *buf
, size_t count
)
577 int nr
= to_sensor_dev_attr(attr
)->index
;
578 struct lm85_data
*data
= dev_get_drvdata(dev
);
579 struct i2c_client
*client
= data
->client
;
583 err
= kstrtoul(buf
, 10, &val
);
587 mutex_lock(&data
->update_lock
);
588 data
->fan_min
[nr
] = FAN_TO_REG(val
);
589 lm85_write_value(client
, LM85_REG_FAN_MIN(nr
), data
->fan_min
[nr
]);
590 mutex_unlock(&data
->update_lock
);
594 #define show_fan_offset(offset) \
595 static SENSOR_DEVICE_ATTR(fan##offset##_input, S_IRUGO, \
596 show_fan, NULL, offset - 1); \
597 static SENSOR_DEVICE_ATTR(fan##offset##_min, S_IRUGO | S_IWUSR, \
598 show_fan_min, set_fan_min, offset - 1)
605 /* vid, vrm, alarms */
607 static ssize_t
show_vid_reg(struct device
*dev
, struct device_attribute
*attr
,
610 struct lm85_data
*data
= lm85_update_device(dev
);
613 if (data
->has_vid5
) {
614 /* 6-pin VID (VRM 10) */
615 vid
= vid_from_reg(data
->vid
& 0x3f, data
->vrm
);
617 /* 5-pin VID (VRM 9) */
618 vid
= vid_from_reg(data
->vid
& 0x1f, data
->vrm
);
621 return sprintf(buf
, "%d\n", vid
);
624 static DEVICE_ATTR(cpu0_vid
, S_IRUGO
, show_vid_reg
, NULL
);
626 static ssize_t
show_vrm_reg(struct device
*dev
, struct device_attribute
*attr
,
629 struct lm85_data
*data
= dev_get_drvdata(dev
);
630 return sprintf(buf
, "%ld\n", (long) data
->vrm
);
633 static ssize_t
store_vrm_reg(struct device
*dev
, struct device_attribute
*attr
,
634 const char *buf
, size_t count
)
636 struct lm85_data
*data
= dev_get_drvdata(dev
);
640 err
= kstrtoul(buf
, 10, &val
);
651 static DEVICE_ATTR(vrm
, S_IRUGO
| S_IWUSR
, show_vrm_reg
, store_vrm_reg
);
653 static ssize_t
show_alarms_reg(struct device
*dev
, struct device_attribute
656 struct lm85_data
*data
= lm85_update_device(dev
);
657 return sprintf(buf
, "%u\n", data
->alarms
);
660 static DEVICE_ATTR(alarms
, S_IRUGO
, show_alarms_reg
, NULL
);
662 static ssize_t
show_alarm(struct device
*dev
, struct device_attribute
*attr
,
665 int nr
= to_sensor_dev_attr(attr
)->index
;
666 struct lm85_data
*data
= lm85_update_device(dev
);
667 return sprintf(buf
, "%u\n", (data
->alarms
>> nr
) & 1);
670 static SENSOR_DEVICE_ATTR(in0_alarm
, S_IRUGO
, show_alarm
, NULL
, 0);
671 static SENSOR_DEVICE_ATTR(in1_alarm
, S_IRUGO
, show_alarm
, NULL
, 1);
672 static SENSOR_DEVICE_ATTR(in2_alarm
, S_IRUGO
, show_alarm
, NULL
, 2);
673 static SENSOR_DEVICE_ATTR(in3_alarm
, S_IRUGO
, show_alarm
, NULL
, 3);
674 static SENSOR_DEVICE_ATTR(in4_alarm
, S_IRUGO
, show_alarm
, NULL
, 8);
675 static SENSOR_DEVICE_ATTR(in5_alarm
, S_IRUGO
, show_alarm
, NULL
, 18);
676 static SENSOR_DEVICE_ATTR(in6_alarm
, S_IRUGO
, show_alarm
, NULL
, 16);
677 static SENSOR_DEVICE_ATTR(in7_alarm
, S_IRUGO
, show_alarm
, NULL
, 17);
678 static SENSOR_DEVICE_ATTR(temp1_alarm
, S_IRUGO
, show_alarm
, NULL
, 4);
679 static SENSOR_DEVICE_ATTR(temp1_fault
, S_IRUGO
, show_alarm
, NULL
, 14);
680 static SENSOR_DEVICE_ATTR(temp2_alarm
, S_IRUGO
, show_alarm
, NULL
, 5);
681 static SENSOR_DEVICE_ATTR(temp3_alarm
, S_IRUGO
, show_alarm
, NULL
, 6);
682 static SENSOR_DEVICE_ATTR(temp3_fault
, S_IRUGO
, show_alarm
, NULL
, 15);
683 static SENSOR_DEVICE_ATTR(fan1_alarm
, S_IRUGO
, show_alarm
, NULL
, 10);
684 static SENSOR_DEVICE_ATTR(fan2_alarm
, S_IRUGO
, show_alarm
, NULL
, 11);
685 static SENSOR_DEVICE_ATTR(fan3_alarm
, S_IRUGO
, show_alarm
, NULL
, 12);
686 static SENSOR_DEVICE_ATTR(fan4_alarm
, S_IRUGO
, show_alarm
, NULL
, 13);
690 static ssize_t
show_pwm(struct device
*dev
, struct device_attribute
*attr
,
693 int nr
= to_sensor_dev_attr(attr
)->index
;
694 struct lm85_data
*data
= lm85_update_device(dev
);
695 return sprintf(buf
, "%d\n", PWM_FROM_REG(data
->pwm
[nr
]));
698 static ssize_t
set_pwm(struct device
*dev
, struct device_attribute
*attr
,
699 const char *buf
, size_t count
)
701 int nr
= to_sensor_dev_attr(attr
)->index
;
702 struct lm85_data
*data
= dev_get_drvdata(dev
);
703 struct i2c_client
*client
= data
->client
;
707 err
= kstrtoul(buf
, 10, &val
);
711 mutex_lock(&data
->update_lock
);
712 data
->pwm
[nr
] = PWM_TO_REG(val
);
713 lm85_write_value(client
, LM85_REG_PWM(nr
), data
->pwm
[nr
]);
714 mutex_unlock(&data
->update_lock
);
718 static ssize_t
show_pwm_enable(struct device
*dev
, struct device_attribute
721 int nr
= to_sensor_dev_attr(attr
)->index
;
722 struct lm85_data
*data
= lm85_update_device(dev
);
723 int pwm_zone
, enable
;
725 pwm_zone
= ZONE_FROM_REG(data
->autofan
[nr
].config
);
727 case -1: /* PWM is always at 100% */
730 case 0: /* PWM is always at 0% */
731 case -2: /* PWM responds to manual control */
734 default: /* PWM in automatic mode */
737 return sprintf(buf
, "%d\n", enable
);
740 static ssize_t
set_pwm_enable(struct device
*dev
, struct device_attribute
741 *attr
, const char *buf
, size_t count
)
743 int nr
= to_sensor_dev_attr(attr
)->index
;
744 struct lm85_data
*data
= dev_get_drvdata(dev
);
745 struct i2c_client
*client
= data
->client
;
750 err
= kstrtoul(buf
, 10, &val
);
763 * Here we have to choose arbitrarily one of the 5 possible
764 * configurations; I go for the safest
772 mutex_lock(&data
->update_lock
);
773 data
->autofan
[nr
].config
= lm85_read_value(client
,
774 LM85_REG_AFAN_CONFIG(nr
));
775 data
->autofan
[nr
].config
= (data
->autofan
[nr
].config
& ~0xe0)
777 lm85_write_value(client
, LM85_REG_AFAN_CONFIG(nr
),
778 data
->autofan
[nr
].config
);
779 mutex_unlock(&data
->update_lock
);
783 static ssize_t
show_pwm_freq(struct device
*dev
,
784 struct device_attribute
*attr
, char *buf
)
786 int nr
= to_sensor_dev_attr(attr
)->index
;
787 struct lm85_data
*data
= lm85_update_device(dev
);
790 if (IS_ADT7468_HFPWM(data
))
793 freq
= FREQ_FROM_REG(data
->freq_map
, data
->pwm_freq
[nr
]);
795 return sprintf(buf
, "%d\n", freq
);
798 static ssize_t
set_pwm_freq(struct device
*dev
,
799 struct device_attribute
*attr
, const char *buf
, size_t count
)
801 int nr
= to_sensor_dev_attr(attr
)->index
;
802 struct lm85_data
*data
= dev_get_drvdata(dev
);
803 struct i2c_client
*client
= data
->client
;
807 err
= kstrtoul(buf
, 10, &val
);
811 mutex_lock(&data
->update_lock
);
813 * The ADT7468 has a special high-frequency PWM output mode,
814 * where all PWM outputs are driven by a 22.5 kHz clock.
815 * This might confuse the user, but there's not much we can do.
817 if (data
->type
== adt7468
&& val
>= 11300) { /* High freq. mode */
818 data
->cfg5
&= ~ADT7468_HFPWM
;
819 lm85_write_value(client
, ADT7468_REG_CFG5
, data
->cfg5
);
820 } else { /* Low freq. mode */
821 data
->pwm_freq
[nr
] = FREQ_TO_REG(data
->freq_map
,
823 lm85_write_value(client
, LM85_REG_AFAN_RANGE(nr
),
824 (data
->zone
[nr
].range
<< 4)
825 | data
->pwm_freq
[nr
]);
826 if (data
->type
== adt7468
) {
827 data
->cfg5
|= ADT7468_HFPWM
;
828 lm85_write_value(client
, ADT7468_REG_CFG5
, data
->cfg5
);
831 mutex_unlock(&data
->update_lock
);
835 #define show_pwm_reg(offset) \
836 static SENSOR_DEVICE_ATTR(pwm##offset, S_IRUGO | S_IWUSR, \
837 show_pwm, set_pwm, offset - 1); \
838 static SENSOR_DEVICE_ATTR(pwm##offset##_enable, S_IRUGO | S_IWUSR, \
839 show_pwm_enable, set_pwm_enable, offset - 1); \
840 static SENSOR_DEVICE_ATTR(pwm##offset##_freq, S_IRUGO | S_IWUSR, \
841 show_pwm_freq, set_pwm_freq, offset - 1)
849 static ssize_t
show_in(struct device
*dev
, struct device_attribute
*attr
,
852 int nr
= to_sensor_dev_attr(attr
)->index
;
853 struct lm85_data
*data
= lm85_update_device(dev
);
854 return sprintf(buf
, "%d\n", INSEXT_FROM_REG(nr
, data
->in
[nr
],
858 static ssize_t
show_in_min(struct device
*dev
, struct device_attribute
*attr
,
861 int nr
= to_sensor_dev_attr(attr
)->index
;
862 struct lm85_data
*data
= lm85_update_device(dev
);
863 return sprintf(buf
, "%d\n", INS_FROM_REG(nr
, data
->in_min
[nr
]));
866 static ssize_t
set_in_min(struct device
*dev
, struct device_attribute
*attr
,
867 const char *buf
, size_t count
)
869 int nr
= to_sensor_dev_attr(attr
)->index
;
870 struct lm85_data
*data
= dev_get_drvdata(dev
);
871 struct i2c_client
*client
= data
->client
;
875 err
= kstrtol(buf
, 10, &val
);
879 mutex_lock(&data
->update_lock
);
880 data
->in_min
[nr
] = INS_TO_REG(nr
, val
);
881 lm85_write_value(client
, LM85_REG_IN_MIN(nr
), data
->in_min
[nr
]);
882 mutex_unlock(&data
->update_lock
);
886 static ssize_t
show_in_max(struct device
*dev
, struct device_attribute
*attr
,
889 int nr
= to_sensor_dev_attr(attr
)->index
;
890 struct lm85_data
*data
= lm85_update_device(dev
);
891 return sprintf(buf
, "%d\n", INS_FROM_REG(nr
, data
->in_max
[nr
]));
894 static ssize_t
set_in_max(struct device
*dev
, struct device_attribute
*attr
,
895 const char *buf
, size_t count
)
897 int nr
= to_sensor_dev_attr(attr
)->index
;
898 struct lm85_data
*data
= dev_get_drvdata(dev
);
899 struct i2c_client
*client
= data
->client
;
903 err
= kstrtol(buf
, 10, &val
);
907 mutex_lock(&data
->update_lock
);
908 data
->in_max
[nr
] = INS_TO_REG(nr
, val
);
909 lm85_write_value(client
, LM85_REG_IN_MAX(nr
), data
->in_max
[nr
]);
910 mutex_unlock(&data
->update_lock
);
914 #define show_in_reg(offset) \
915 static SENSOR_DEVICE_ATTR(in##offset##_input, S_IRUGO, \
916 show_in, NULL, offset); \
917 static SENSOR_DEVICE_ATTR(in##offset##_min, S_IRUGO | S_IWUSR, \
918 show_in_min, set_in_min, offset); \
919 static SENSOR_DEVICE_ATTR(in##offset##_max, S_IRUGO | S_IWUSR, \
920 show_in_max, set_in_max, offset)
933 static ssize_t
show_temp(struct device
*dev
, struct device_attribute
*attr
,
936 int nr
= to_sensor_dev_attr(attr
)->index
;
937 struct lm85_data
*data
= lm85_update_device(dev
);
938 return sprintf(buf
, "%d\n", TEMPEXT_FROM_REG(data
->temp
[nr
],
939 data
->temp_ext
[nr
]));
942 static ssize_t
show_temp_min(struct device
*dev
, struct device_attribute
*attr
,
945 int nr
= to_sensor_dev_attr(attr
)->index
;
946 struct lm85_data
*data
= lm85_update_device(dev
);
947 return sprintf(buf
, "%d\n", TEMP_FROM_REG(data
->temp_min
[nr
]));
950 static ssize_t
set_temp_min(struct device
*dev
, struct device_attribute
*attr
,
951 const char *buf
, size_t count
)
953 int nr
= to_sensor_dev_attr(attr
)->index
;
954 struct lm85_data
*data
= dev_get_drvdata(dev
);
955 struct i2c_client
*client
= data
->client
;
959 err
= kstrtol(buf
, 10, &val
);
963 if (IS_ADT7468_OFF64(data
))
966 mutex_lock(&data
->update_lock
);
967 data
->temp_min
[nr
] = TEMP_TO_REG(val
);
968 lm85_write_value(client
, LM85_REG_TEMP_MIN(nr
), data
->temp_min
[nr
]);
969 mutex_unlock(&data
->update_lock
);
973 static ssize_t
show_temp_max(struct device
*dev
, struct device_attribute
*attr
,
976 int nr
= to_sensor_dev_attr(attr
)->index
;
977 struct lm85_data
*data
= lm85_update_device(dev
);
978 return sprintf(buf
, "%d\n", TEMP_FROM_REG(data
->temp_max
[nr
]));
981 static ssize_t
set_temp_max(struct device
*dev
, struct device_attribute
*attr
,
982 const char *buf
, size_t count
)
984 int nr
= to_sensor_dev_attr(attr
)->index
;
985 struct lm85_data
*data
= dev_get_drvdata(dev
);
986 struct i2c_client
*client
= data
->client
;
990 err
= kstrtol(buf
, 10, &val
);
994 if (IS_ADT7468_OFF64(data
))
997 mutex_lock(&data
->update_lock
);
998 data
->temp_max
[nr
] = TEMP_TO_REG(val
);
999 lm85_write_value(client
, LM85_REG_TEMP_MAX(nr
), data
->temp_max
[nr
]);
1000 mutex_unlock(&data
->update_lock
);
1004 #define show_temp_reg(offset) \
1005 static SENSOR_DEVICE_ATTR(temp##offset##_input, S_IRUGO, \
1006 show_temp, NULL, offset - 1); \
1007 static SENSOR_DEVICE_ATTR(temp##offset##_min, S_IRUGO | S_IWUSR, \
1008 show_temp_min, set_temp_min, offset - 1); \
1009 static SENSOR_DEVICE_ATTR(temp##offset##_max, S_IRUGO | S_IWUSR, \
1010 show_temp_max, set_temp_max, offset - 1);
1017 /* Automatic PWM control */
1019 static ssize_t
show_pwm_auto_channels(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", ZONE_FROM_REG(data
->autofan
[nr
].config
));
1027 static ssize_t
set_pwm_auto_channels(struct device
*dev
,
1028 struct device_attribute
*attr
, const char *buf
, size_t count
)
1030 int nr
= to_sensor_dev_attr(attr
)->index
;
1031 struct lm85_data
*data
= dev_get_drvdata(dev
);
1032 struct i2c_client
*client
= data
->client
;
1036 err
= kstrtol(buf
, 10, &val
);
1040 mutex_lock(&data
->update_lock
);
1041 data
->autofan
[nr
].config
= (data
->autofan
[nr
].config
& (~0xe0))
1043 lm85_write_value(client
, LM85_REG_AFAN_CONFIG(nr
),
1044 data
->autofan
[nr
].config
);
1045 mutex_unlock(&data
->update_lock
);
1049 static ssize_t
show_pwm_auto_pwm_min(struct device
*dev
,
1050 struct device_attribute
*attr
, char *buf
)
1052 int nr
= to_sensor_dev_attr(attr
)->index
;
1053 struct lm85_data
*data
= lm85_update_device(dev
);
1054 return sprintf(buf
, "%d\n", PWM_FROM_REG(data
->autofan
[nr
].min_pwm
));
1057 static ssize_t
set_pwm_auto_pwm_min(struct device
*dev
,
1058 struct device_attribute
*attr
, const char *buf
, size_t count
)
1060 int nr
= to_sensor_dev_attr(attr
)->index
;
1061 struct lm85_data
*data
= dev_get_drvdata(dev
);
1062 struct i2c_client
*client
= data
->client
;
1066 err
= kstrtoul(buf
, 10, &val
);
1070 mutex_lock(&data
->update_lock
);
1071 data
->autofan
[nr
].min_pwm
= PWM_TO_REG(val
);
1072 lm85_write_value(client
, LM85_REG_AFAN_MINPWM(nr
),
1073 data
->autofan
[nr
].min_pwm
);
1074 mutex_unlock(&data
->update_lock
);
1078 static ssize_t
show_pwm_auto_pwm_minctl(struct device
*dev
,
1079 struct device_attribute
*attr
, char *buf
)
1081 int nr
= to_sensor_dev_attr(attr
)->index
;
1082 struct lm85_data
*data
= lm85_update_device(dev
);
1083 return sprintf(buf
, "%d\n", data
->autofan
[nr
].min_off
);
1086 static ssize_t
set_pwm_auto_pwm_minctl(struct device
*dev
,
1087 struct device_attribute
*attr
, const char *buf
, size_t count
)
1089 int nr
= to_sensor_dev_attr(attr
)->index
;
1090 struct lm85_data
*data
= dev_get_drvdata(dev
);
1091 struct i2c_client
*client
= data
->client
;
1096 err
= kstrtol(buf
, 10, &val
);
1100 mutex_lock(&data
->update_lock
);
1101 data
->autofan
[nr
].min_off
= val
;
1102 tmp
= lm85_read_value(client
, LM85_REG_AFAN_SPIKE1
);
1103 tmp
&= ~(0x20 << nr
);
1104 if (data
->autofan
[nr
].min_off
)
1106 lm85_write_value(client
, LM85_REG_AFAN_SPIKE1
, tmp
);
1107 mutex_unlock(&data
->update_lock
);
1111 #define pwm_auto(offset) \
1112 static SENSOR_DEVICE_ATTR(pwm##offset##_auto_channels, \
1113 S_IRUGO | S_IWUSR, show_pwm_auto_channels, \
1114 set_pwm_auto_channels, offset - 1); \
1115 static SENSOR_DEVICE_ATTR(pwm##offset##_auto_pwm_min, \
1116 S_IRUGO | S_IWUSR, show_pwm_auto_pwm_min, \
1117 set_pwm_auto_pwm_min, offset - 1); \
1118 static SENSOR_DEVICE_ATTR(pwm##offset##_auto_pwm_minctl, \
1119 S_IRUGO | S_IWUSR, show_pwm_auto_pwm_minctl, \
1120 set_pwm_auto_pwm_minctl, offset - 1)
1126 /* Temperature settings for automatic PWM control */
1128 static ssize_t
show_temp_auto_temp_off(struct device
*dev
,
1129 struct device_attribute
*attr
, char *buf
)
1131 int nr
= to_sensor_dev_attr(attr
)->index
;
1132 struct lm85_data
*data
= lm85_update_device(dev
);
1133 return sprintf(buf
, "%d\n", TEMP_FROM_REG(data
->zone
[nr
].limit
) -
1134 HYST_FROM_REG(data
->zone
[nr
].hyst
));
1137 static ssize_t
set_temp_auto_temp_off(struct device
*dev
,
1138 struct device_attribute
*attr
, const char *buf
, size_t count
)
1140 int nr
= to_sensor_dev_attr(attr
)->index
;
1141 struct lm85_data
*data
= dev_get_drvdata(dev
);
1142 struct i2c_client
*client
= data
->client
;
1147 err
= kstrtol(buf
, 10, &val
);
1151 mutex_lock(&data
->update_lock
);
1152 min
= TEMP_FROM_REG(data
->zone
[nr
].limit
);
1153 data
->zone
[nr
].hyst
= HYST_TO_REG(min
- val
);
1154 if (nr
== 0 || nr
== 1) {
1155 lm85_write_value(client
, LM85_REG_AFAN_HYST1
,
1156 (data
->zone
[0].hyst
<< 4)
1157 | data
->zone
[1].hyst
);
1159 lm85_write_value(client
, LM85_REG_AFAN_HYST2
,
1160 (data
->zone
[2].hyst
<< 4));
1162 mutex_unlock(&data
->update_lock
);
1166 static ssize_t
show_temp_auto_temp_min(struct device
*dev
,
1167 struct device_attribute
*attr
, char *buf
)
1169 int nr
= to_sensor_dev_attr(attr
)->index
;
1170 struct lm85_data
*data
= lm85_update_device(dev
);
1171 return sprintf(buf
, "%d\n", TEMP_FROM_REG(data
->zone
[nr
].limit
));
1174 static ssize_t
set_temp_auto_temp_min(struct device
*dev
,
1175 struct device_attribute
*attr
, const char *buf
, size_t count
)
1177 int nr
= to_sensor_dev_attr(attr
)->index
;
1178 struct lm85_data
*data
= dev_get_drvdata(dev
);
1179 struct i2c_client
*client
= data
->client
;
1183 err
= kstrtol(buf
, 10, &val
);
1187 mutex_lock(&data
->update_lock
);
1188 data
->zone
[nr
].limit
= TEMP_TO_REG(val
);
1189 lm85_write_value(client
, LM85_REG_AFAN_LIMIT(nr
),
1190 data
->zone
[nr
].limit
);
1192 /* Update temp_auto_max and temp_auto_range */
1193 data
->zone
[nr
].range
= RANGE_TO_REG(
1194 TEMP_FROM_REG(data
->zone
[nr
].max_desired
) -
1195 TEMP_FROM_REG(data
->zone
[nr
].limit
));
1196 lm85_write_value(client
, LM85_REG_AFAN_RANGE(nr
),
1197 ((data
->zone
[nr
].range
& 0x0f) << 4)
1198 | (data
->pwm_freq
[nr
] & 0x07));
1200 mutex_unlock(&data
->update_lock
);
1204 static ssize_t
show_temp_auto_temp_max(struct device
*dev
,
1205 struct device_attribute
*attr
, char *buf
)
1207 int nr
= to_sensor_dev_attr(attr
)->index
;
1208 struct lm85_data
*data
= lm85_update_device(dev
);
1209 return sprintf(buf
, "%d\n", TEMP_FROM_REG(data
->zone
[nr
].limit
) +
1210 RANGE_FROM_REG(data
->zone
[nr
].range
));
1213 static ssize_t
set_temp_auto_temp_max(struct device
*dev
,
1214 struct device_attribute
*attr
, const char *buf
, size_t count
)
1216 int nr
= to_sensor_dev_attr(attr
)->index
;
1217 struct lm85_data
*data
= dev_get_drvdata(dev
);
1218 struct i2c_client
*client
= data
->client
;
1223 err
= kstrtol(buf
, 10, &val
);
1227 mutex_lock(&data
->update_lock
);
1228 min
= TEMP_FROM_REG(data
->zone
[nr
].limit
);
1229 data
->zone
[nr
].max_desired
= TEMP_TO_REG(val
);
1230 data
->zone
[nr
].range
= RANGE_TO_REG(
1232 lm85_write_value(client
, LM85_REG_AFAN_RANGE(nr
),
1233 ((data
->zone
[nr
].range
& 0x0f) << 4)
1234 | (data
->pwm_freq
[nr
] & 0x07));
1235 mutex_unlock(&data
->update_lock
);
1239 static ssize_t
show_temp_auto_temp_crit(struct device
*dev
,
1240 struct device_attribute
*attr
, char *buf
)
1242 int nr
= to_sensor_dev_attr(attr
)->index
;
1243 struct lm85_data
*data
= lm85_update_device(dev
);
1244 return sprintf(buf
, "%d\n", TEMP_FROM_REG(data
->zone
[nr
].critical
));
1247 static ssize_t
set_temp_auto_temp_crit(struct device
*dev
,
1248 struct device_attribute
*attr
, const char *buf
, size_t count
)
1250 int nr
= to_sensor_dev_attr(attr
)->index
;
1251 struct lm85_data
*data
= dev_get_drvdata(dev
);
1252 struct i2c_client
*client
= data
->client
;
1256 err
= kstrtol(buf
, 10, &val
);
1260 mutex_lock(&data
->update_lock
);
1261 data
->zone
[nr
].critical
= TEMP_TO_REG(val
);
1262 lm85_write_value(client
, LM85_REG_AFAN_CRITICAL(nr
),
1263 data
->zone
[nr
].critical
);
1264 mutex_unlock(&data
->update_lock
);
1268 #define temp_auto(offset) \
1269 static SENSOR_DEVICE_ATTR(temp##offset##_auto_temp_off, \
1270 S_IRUGO | S_IWUSR, show_temp_auto_temp_off, \
1271 set_temp_auto_temp_off, offset - 1); \
1272 static SENSOR_DEVICE_ATTR(temp##offset##_auto_temp_min, \
1273 S_IRUGO | S_IWUSR, show_temp_auto_temp_min, \
1274 set_temp_auto_temp_min, offset - 1); \
1275 static SENSOR_DEVICE_ATTR(temp##offset##_auto_temp_max, \
1276 S_IRUGO | S_IWUSR, show_temp_auto_temp_max, \
1277 set_temp_auto_temp_max, offset - 1); \
1278 static SENSOR_DEVICE_ATTR(temp##offset##_auto_temp_crit, \
1279 S_IRUGO | S_IWUSR, show_temp_auto_temp_crit, \
1280 set_temp_auto_temp_crit, offset - 1);
1286 static struct attribute
*lm85_attributes
[] = {
1287 &sensor_dev_attr_fan1_input
.dev_attr
.attr
,
1288 &sensor_dev_attr_fan2_input
.dev_attr
.attr
,
1289 &sensor_dev_attr_fan3_input
.dev_attr
.attr
,
1290 &sensor_dev_attr_fan4_input
.dev_attr
.attr
,
1291 &sensor_dev_attr_fan1_min
.dev_attr
.attr
,
1292 &sensor_dev_attr_fan2_min
.dev_attr
.attr
,
1293 &sensor_dev_attr_fan3_min
.dev_attr
.attr
,
1294 &sensor_dev_attr_fan4_min
.dev_attr
.attr
,
1295 &sensor_dev_attr_fan1_alarm
.dev_attr
.attr
,
1296 &sensor_dev_attr_fan2_alarm
.dev_attr
.attr
,
1297 &sensor_dev_attr_fan3_alarm
.dev_attr
.attr
,
1298 &sensor_dev_attr_fan4_alarm
.dev_attr
.attr
,
1300 &sensor_dev_attr_pwm1
.dev_attr
.attr
,
1301 &sensor_dev_attr_pwm2
.dev_attr
.attr
,
1302 &sensor_dev_attr_pwm3
.dev_attr
.attr
,
1303 &sensor_dev_attr_pwm1_enable
.dev_attr
.attr
,
1304 &sensor_dev_attr_pwm2_enable
.dev_attr
.attr
,
1305 &sensor_dev_attr_pwm3_enable
.dev_attr
.attr
,
1306 &sensor_dev_attr_pwm1_freq
.dev_attr
.attr
,
1307 &sensor_dev_attr_pwm2_freq
.dev_attr
.attr
,
1308 &sensor_dev_attr_pwm3_freq
.dev_attr
.attr
,
1310 &sensor_dev_attr_in0_input
.dev_attr
.attr
,
1311 &sensor_dev_attr_in1_input
.dev_attr
.attr
,
1312 &sensor_dev_attr_in2_input
.dev_attr
.attr
,
1313 &sensor_dev_attr_in3_input
.dev_attr
.attr
,
1314 &sensor_dev_attr_in0_min
.dev_attr
.attr
,
1315 &sensor_dev_attr_in1_min
.dev_attr
.attr
,
1316 &sensor_dev_attr_in2_min
.dev_attr
.attr
,
1317 &sensor_dev_attr_in3_min
.dev_attr
.attr
,
1318 &sensor_dev_attr_in0_max
.dev_attr
.attr
,
1319 &sensor_dev_attr_in1_max
.dev_attr
.attr
,
1320 &sensor_dev_attr_in2_max
.dev_attr
.attr
,
1321 &sensor_dev_attr_in3_max
.dev_attr
.attr
,
1322 &sensor_dev_attr_in0_alarm
.dev_attr
.attr
,
1323 &sensor_dev_attr_in1_alarm
.dev_attr
.attr
,
1324 &sensor_dev_attr_in2_alarm
.dev_attr
.attr
,
1325 &sensor_dev_attr_in3_alarm
.dev_attr
.attr
,
1327 &sensor_dev_attr_temp1_input
.dev_attr
.attr
,
1328 &sensor_dev_attr_temp2_input
.dev_attr
.attr
,
1329 &sensor_dev_attr_temp3_input
.dev_attr
.attr
,
1330 &sensor_dev_attr_temp1_min
.dev_attr
.attr
,
1331 &sensor_dev_attr_temp2_min
.dev_attr
.attr
,
1332 &sensor_dev_attr_temp3_min
.dev_attr
.attr
,
1333 &sensor_dev_attr_temp1_max
.dev_attr
.attr
,
1334 &sensor_dev_attr_temp2_max
.dev_attr
.attr
,
1335 &sensor_dev_attr_temp3_max
.dev_attr
.attr
,
1336 &sensor_dev_attr_temp1_alarm
.dev_attr
.attr
,
1337 &sensor_dev_attr_temp2_alarm
.dev_attr
.attr
,
1338 &sensor_dev_attr_temp3_alarm
.dev_attr
.attr
,
1339 &sensor_dev_attr_temp1_fault
.dev_attr
.attr
,
1340 &sensor_dev_attr_temp3_fault
.dev_attr
.attr
,
1342 &sensor_dev_attr_pwm1_auto_channels
.dev_attr
.attr
,
1343 &sensor_dev_attr_pwm2_auto_channels
.dev_attr
.attr
,
1344 &sensor_dev_attr_pwm3_auto_channels
.dev_attr
.attr
,
1345 &sensor_dev_attr_pwm1_auto_pwm_min
.dev_attr
.attr
,
1346 &sensor_dev_attr_pwm2_auto_pwm_min
.dev_attr
.attr
,
1347 &sensor_dev_attr_pwm3_auto_pwm_min
.dev_attr
.attr
,
1349 &sensor_dev_attr_temp1_auto_temp_min
.dev_attr
.attr
,
1350 &sensor_dev_attr_temp2_auto_temp_min
.dev_attr
.attr
,
1351 &sensor_dev_attr_temp3_auto_temp_min
.dev_attr
.attr
,
1352 &sensor_dev_attr_temp1_auto_temp_max
.dev_attr
.attr
,
1353 &sensor_dev_attr_temp2_auto_temp_max
.dev_attr
.attr
,
1354 &sensor_dev_attr_temp3_auto_temp_max
.dev_attr
.attr
,
1355 &sensor_dev_attr_temp1_auto_temp_crit
.dev_attr
.attr
,
1356 &sensor_dev_attr_temp2_auto_temp_crit
.dev_attr
.attr
,
1357 &sensor_dev_attr_temp3_auto_temp_crit
.dev_attr
.attr
,
1360 &dev_attr_cpu0_vid
.attr
,
1361 &dev_attr_alarms
.attr
,
1365 static const struct attribute_group lm85_group
= {
1366 .attrs
= lm85_attributes
,
1369 static struct attribute
*lm85_attributes_minctl
[] = {
1370 &sensor_dev_attr_pwm1_auto_pwm_minctl
.dev_attr
.attr
,
1371 &sensor_dev_attr_pwm2_auto_pwm_minctl
.dev_attr
.attr
,
1372 &sensor_dev_attr_pwm3_auto_pwm_minctl
.dev_attr
.attr
,
1376 static const struct attribute_group lm85_group_minctl
= {
1377 .attrs
= lm85_attributes_minctl
,
1380 static struct attribute
*lm85_attributes_temp_off
[] = {
1381 &sensor_dev_attr_temp1_auto_temp_off
.dev_attr
.attr
,
1382 &sensor_dev_attr_temp2_auto_temp_off
.dev_attr
.attr
,
1383 &sensor_dev_attr_temp3_auto_temp_off
.dev_attr
.attr
,
1387 static const struct attribute_group lm85_group_temp_off
= {
1388 .attrs
= lm85_attributes_temp_off
,
1391 static struct attribute
*lm85_attributes_in4
[] = {
1392 &sensor_dev_attr_in4_input
.dev_attr
.attr
,
1393 &sensor_dev_attr_in4_min
.dev_attr
.attr
,
1394 &sensor_dev_attr_in4_max
.dev_attr
.attr
,
1395 &sensor_dev_attr_in4_alarm
.dev_attr
.attr
,
1399 static const struct attribute_group lm85_group_in4
= {
1400 .attrs
= lm85_attributes_in4
,
1403 static struct attribute
*lm85_attributes_in567
[] = {
1404 &sensor_dev_attr_in5_input
.dev_attr
.attr
,
1405 &sensor_dev_attr_in6_input
.dev_attr
.attr
,
1406 &sensor_dev_attr_in7_input
.dev_attr
.attr
,
1407 &sensor_dev_attr_in5_min
.dev_attr
.attr
,
1408 &sensor_dev_attr_in6_min
.dev_attr
.attr
,
1409 &sensor_dev_attr_in7_min
.dev_attr
.attr
,
1410 &sensor_dev_attr_in5_max
.dev_attr
.attr
,
1411 &sensor_dev_attr_in6_max
.dev_attr
.attr
,
1412 &sensor_dev_attr_in7_max
.dev_attr
.attr
,
1413 &sensor_dev_attr_in5_alarm
.dev_attr
.attr
,
1414 &sensor_dev_attr_in6_alarm
.dev_attr
.attr
,
1415 &sensor_dev_attr_in7_alarm
.dev_attr
.attr
,
1419 static const struct attribute_group lm85_group_in567
= {
1420 .attrs
= lm85_attributes_in567
,
1423 static void lm85_init_client(struct i2c_client
*client
)
1427 /* Start monitoring if needed */
1428 value
= lm85_read_value(client
, LM85_REG_CONFIG
);
1429 if (!(value
& 0x01)) {
1430 dev_info(&client
->dev
, "Starting monitoring\n");
1431 lm85_write_value(client
, LM85_REG_CONFIG
, value
| 0x01);
1434 /* Warn about unusual configuration bits */
1436 dev_warn(&client
->dev
, "Device configuration is locked\n");
1437 if (!(value
& 0x04))
1438 dev_warn(&client
->dev
, "Device is not ready\n");
1441 static int lm85_is_fake(struct i2c_client
*client
)
1444 * Differenciate between real LM96000 and Winbond WPCD377I. The latter
1445 * emulate the former except that it has no hardware monitoring function
1446 * so the readings are always 0.
1451 for (i
= 0; i
< 8; i
++) {
1452 in_temp
= i2c_smbus_read_byte_data(client
, 0x20 + i
);
1453 fan
= i2c_smbus_read_byte_data(client
, 0x28 + i
);
1454 if (in_temp
!= 0x00 || fan
!= 0xff)
1461 /* Return 0 if detection is successful, -ENODEV otherwise */
1462 static int lm85_detect(struct i2c_client
*client
, struct i2c_board_info
*info
)
1464 struct i2c_adapter
*adapter
= client
->adapter
;
1465 int address
= client
->addr
;
1466 const char *type_name
= NULL
;
1467 int company
, verstep
;
1469 if (!i2c_check_functionality(adapter
, I2C_FUNC_SMBUS_BYTE_DATA
)) {
1470 /* We need to be able to do byte I/O */
1474 /* Determine the chip type */
1475 company
= lm85_read_value(client
, LM85_REG_COMPANY
);
1476 verstep
= lm85_read_value(client
, LM85_REG_VERSTEP
);
1478 dev_dbg(&adapter
->dev
,
1479 "Detecting device at 0x%02x with COMPANY: 0x%02x and VERSTEP: 0x%02x\n",
1480 address
, company
, verstep
);
1482 if (company
== LM85_COMPANY_NATIONAL
) {
1484 case LM85_VERSTEP_LM85C
:
1485 type_name
= "lm85c";
1487 case LM85_VERSTEP_LM85B
:
1488 type_name
= "lm85b";
1490 case LM85_VERSTEP_LM96000_1
:
1491 case LM85_VERSTEP_LM96000_2
:
1492 /* Check for Winbond WPCD377I */
1493 if (lm85_is_fake(client
)) {
1494 dev_dbg(&adapter
->dev
,
1495 "Found Winbond WPCD377I, ignoring\n");
1501 } else if (company
== LM85_COMPANY_ANALOG_DEV
) {
1503 case LM85_VERSTEP_ADM1027
:
1504 type_name
= "adm1027";
1506 case LM85_VERSTEP_ADT7463
:
1507 case LM85_VERSTEP_ADT7463C
:
1508 type_name
= "adt7463";
1510 case LM85_VERSTEP_ADT7468_1
:
1511 case LM85_VERSTEP_ADT7468_2
:
1512 type_name
= "adt7468";
1515 } else if (company
== LM85_COMPANY_SMSC
) {
1517 case LM85_VERSTEP_EMC6D100_A0
:
1518 case LM85_VERSTEP_EMC6D100_A1
:
1519 /* Note: we can't tell a '100 from a '101 */
1520 type_name
= "emc6d100";
1522 case LM85_VERSTEP_EMC6D102
:
1523 type_name
= "emc6d102";
1525 case LM85_VERSTEP_EMC6D103_A0
:
1526 case LM85_VERSTEP_EMC6D103_A1
:
1527 type_name
= "emc6d103";
1529 case LM85_VERSTEP_EMC6D103S
:
1530 type_name
= "emc6d103s";
1538 strlcpy(info
->type
, type_name
, I2C_NAME_SIZE
);
1543 static int lm85_probe(struct i2c_client
*client
, const struct i2c_device_id
*id
)
1545 struct device
*dev
= &client
->dev
;
1546 struct device
*hwmon_dev
;
1547 struct lm85_data
*data
;
1550 data
= devm_kzalloc(dev
, sizeof(struct lm85_data
), GFP_KERNEL
);
1554 data
->client
= client
;
1555 data
->type
= id
->driver_data
;
1556 mutex_init(&data
->update_lock
);
1558 /* Fill in the chip specific driver values */
1559 switch (data
->type
) {
1567 data
->freq_map
= adm1027_freq_map
;
1570 data
->freq_map
= lm85_freq_map
;
1573 /* Set the VRM version */
1574 data
->vrm
= vid_which_vrm();
1576 /* Initialize the LM85 chip */
1577 lm85_init_client(client
);
1580 data
->groups
[idx
++] = &lm85_group
;
1582 /* minctl and temp_off exist on all chips except emc6d103s */
1583 if (data
->type
!= emc6d103s
) {
1584 data
->groups
[idx
++] = &lm85_group_minctl
;
1585 data
->groups
[idx
++] = &lm85_group_temp_off
;
1589 * The ADT7463/68 have an optional VRM 10 mode where pin 21 is used
1590 * as a sixth digital VID input rather than an analog input.
1592 if (data
->type
== adt7463
|| data
->type
== adt7468
) {
1593 u8 vid
= lm85_read_value(client
, LM85_REG_VID
);
1595 data
->has_vid5
= true;
1598 if (!data
->has_vid5
)
1599 data
->groups
[idx
++] = &lm85_group_in4
;
1601 /* The EMC6D100 has 3 additional voltage inputs */
1602 if (data
->type
== emc6d100
)
1603 data
->groups
[idx
++] = &lm85_group_in567
;
1605 hwmon_dev
= devm_hwmon_device_register_with_groups(dev
, client
->name
,
1606 data
, data
->groups
);
1607 return PTR_ERR_OR_ZERO(hwmon_dev
);
1610 static const struct i2c_device_id lm85_id
[] = {
1611 { "adm1027", adm1027
},
1612 { "adt7463", adt7463
},
1613 { "adt7468", adt7468
},
1617 { "emc6d100", emc6d100
},
1618 { "emc6d101", emc6d100
},
1619 { "emc6d102", emc6d102
},
1620 { "emc6d103", emc6d103
},
1621 { "emc6d103s", emc6d103s
},
1624 MODULE_DEVICE_TABLE(i2c
, lm85_id
);
1626 static struct i2c_driver lm85_driver
= {
1627 .class = I2C_CLASS_HWMON
,
1631 .probe
= lm85_probe
,
1632 .id_table
= lm85_id
,
1633 .detect
= lm85_detect
,
1634 .address_list
= normal_i2c
,
1637 module_i2c_driver(lm85_driver
);
1639 MODULE_LICENSE("GPL");
1640 MODULE_AUTHOR("Philip Pokorny <ppokorny@penguincomputing.com>, "
1641 "Margit Schubert-While <margitsw@t-online.de>, "
1642 "Justin Thiessen <jthiessen@penguincomputing.com>");
1643 MODULE_DESCRIPTION("LM85-B, LM85-C driver");