1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * adm1031.c - Part of lm_sensors, Linux kernel modules for hardware
5 * Based on lm75.c and lm85.c
6 * Supports adm1030 / adm1031
7 * Copyright (C) 2004 Alexandre d'Alton <alex@alexdalton.org>
8 * Reworked by Jean Delvare <jdelvare@suse.de>
11 #include <linux/module.h>
12 #include <linux/init.h>
13 #include <linux/slab.h>
14 #include <linux/jiffies.h>
15 #include <linux/i2c.h>
16 #include <linux/hwmon.h>
17 #include <linux/hwmon-sysfs.h>
18 #include <linux/err.h>
19 #include <linux/mutex.h>
21 /* Following macros takes channel parameter starting from 0 to 2 */
22 #define ADM1031_REG_FAN_SPEED(nr) (0x08 + (nr))
23 #define ADM1031_REG_FAN_DIV(nr) (0x20 + (nr))
24 #define ADM1031_REG_PWM (0x22)
25 #define ADM1031_REG_FAN_MIN(nr) (0x10 + (nr))
26 #define ADM1031_REG_FAN_FILTER (0x23)
28 #define ADM1031_REG_TEMP_OFFSET(nr) (0x0d + (nr))
29 #define ADM1031_REG_TEMP_MAX(nr) (0x14 + 4 * (nr))
30 #define ADM1031_REG_TEMP_MIN(nr) (0x15 + 4 * (nr))
31 #define ADM1031_REG_TEMP_CRIT(nr) (0x16 + 4 * (nr))
33 #define ADM1031_REG_TEMP(nr) (0x0a + (nr))
34 #define ADM1031_REG_AUTO_TEMP(nr) (0x24 + (nr))
36 #define ADM1031_REG_STATUS(nr) (0x2 + (nr))
38 #define ADM1031_REG_CONF1 0x00
39 #define ADM1031_REG_CONF2 0x01
40 #define ADM1031_REG_EXT_TEMP 0x06
42 #define ADM1031_CONF1_MONITOR_ENABLE 0x01 /* Monitoring enable */
43 #define ADM1031_CONF1_PWM_INVERT 0x08 /* PWM Invert */
44 #define ADM1031_CONF1_AUTO_MODE 0x80 /* Auto FAN */
46 #define ADM1031_CONF2_PWM1_ENABLE 0x01
47 #define ADM1031_CONF2_PWM2_ENABLE 0x02
48 #define ADM1031_CONF2_TACH1_ENABLE 0x04
49 #define ADM1031_CONF2_TACH2_ENABLE 0x08
50 #define ADM1031_CONF2_TEMP_ENABLE(chan) (0x10 << (chan))
52 #define ADM1031_UPDATE_RATE_MASK 0x1c
53 #define ADM1031_UPDATE_RATE_SHIFT 2
55 /* Addresses to scan */
56 static const unsigned short normal_i2c
[] = { 0x2c, 0x2d, 0x2e, I2C_CLIENT_END
};
58 enum chips
{ adm1030
, adm1031
};
60 typedef u8 auto_chan_table_t
[8][2];
62 /* Each client has this additional data */
64 struct i2c_client
*client
;
65 const struct attribute_group
*groups
[3];
66 struct mutex update_lock
;
68 char valid
; /* !=0 if following fields are valid */
69 unsigned long last_updated
; /* In jiffies */
70 unsigned int update_interval
; /* In milliseconds */
72 * The chan_select_table contains the possible configurations for
75 const auto_chan_table_t
*chan_select_table
;
96 static inline u8
adm1031_read_value(struct i2c_client
*client
, u8 reg
)
98 return i2c_smbus_read_byte_data(client
, reg
);
102 adm1031_write_value(struct i2c_client
*client
, u8 reg
, unsigned int value
)
104 return i2c_smbus_write_byte_data(client
, reg
, value
);
107 static struct adm1031_data
*adm1031_update_device(struct device
*dev
)
109 struct adm1031_data
*data
= dev_get_drvdata(dev
);
110 struct i2c_client
*client
= data
->client
;
111 unsigned long next_update
;
114 mutex_lock(&data
->update_lock
);
116 next_update
= data
->last_updated
117 + msecs_to_jiffies(data
->update_interval
);
118 if (time_after(jiffies
, next_update
) || !data
->valid
) {
120 dev_dbg(&client
->dev
, "Starting adm1031 update\n");
122 chan
< ((data
->chip_type
== adm1031
) ? 3 : 2); chan
++) {
126 adm1031_read_value(client
, ADM1031_REG_TEMP(chan
));
127 data
->ext_temp
[chan
] =
128 adm1031_read_value(client
, ADM1031_REG_EXT_TEMP
);
130 adm1031_read_value(client
, ADM1031_REG_TEMP(chan
));
132 data
->ext_temp
[chan
] =
133 adm1031_read_value(client
,
134 ADM1031_REG_EXT_TEMP
);
137 adm1031_read_value(client
,
138 ADM1031_REG_TEMP(chan
));
140 /* oldh is actually newer */
142 dev_warn(&client
->dev
,
143 "Remote temperature may be wrong.\n");
146 data
->temp
[chan
] = newh
;
148 data
->temp_offset
[chan
] =
149 adm1031_read_value(client
,
150 ADM1031_REG_TEMP_OFFSET(chan
));
151 data
->temp_min
[chan
] =
152 adm1031_read_value(client
,
153 ADM1031_REG_TEMP_MIN(chan
));
154 data
->temp_max
[chan
] =
155 adm1031_read_value(client
,
156 ADM1031_REG_TEMP_MAX(chan
));
157 data
->temp_crit
[chan
] =
158 adm1031_read_value(client
,
159 ADM1031_REG_TEMP_CRIT(chan
));
160 data
->auto_temp
[chan
] =
161 adm1031_read_value(client
,
162 ADM1031_REG_AUTO_TEMP(chan
));
166 data
->conf1
= adm1031_read_value(client
, ADM1031_REG_CONF1
);
167 data
->conf2
= adm1031_read_value(client
, ADM1031_REG_CONF2
);
169 data
->alarm
= adm1031_read_value(client
, ADM1031_REG_STATUS(0))
170 | (adm1031_read_value(client
, ADM1031_REG_STATUS(1)) << 8);
171 if (data
->chip_type
== adm1030
)
172 data
->alarm
&= 0xc0ff;
174 for (chan
= 0; chan
< (data
->chip_type
== adm1030
? 1 : 2);
176 data
->fan_div
[chan
] =
177 adm1031_read_value(client
,
178 ADM1031_REG_FAN_DIV(chan
));
179 data
->fan_min
[chan
] =
180 adm1031_read_value(client
,
181 ADM1031_REG_FAN_MIN(chan
));
183 adm1031_read_value(client
,
184 ADM1031_REG_FAN_SPEED(chan
));
186 (adm1031_read_value(client
,
187 ADM1031_REG_PWM
) >> (4 * chan
)) & 0x0f;
189 data
->last_updated
= jiffies
;
193 mutex_unlock(&data
->update_lock
);
198 #define TEMP_TO_REG(val) (((val) < 0 ? ((val - 500) / 1000) : \
199 ((val + 500) / 1000)))
201 #define TEMP_FROM_REG(val) ((val) * 1000)
203 #define TEMP_FROM_REG_EXT(val, ext) (TEMP_FROM_REG(val) + (ext) * 125)
205 #define TEMP_OFFSET_TO_REG(val) (TEMP_TO_REG(val) & 0x8f)
206 #define TEMP_OFFSET_FROM_REG(val) TEMP_FROM_REG((val) < 0 ? \
207 (val) | 0x70 : (val))
209 #define FAN_FROM_REG(reg, div) ((reg) ? \
210 (11250 * 60) / ((reg) * (div)) : 0)
212 static int FAN_TO_REG(int reg
, int div
)
215 tmp
= FAN_FROM_REG(clamp_val(reg
, 0, 65535), div
);
216 return tmp
> 255 ? 255 : tmp
;
219 #define FAN_DIV_FROM_REG(reg) (1<<(((reg)&0xc0)>>6))
221 #define PWM_TO_REG(val) (clamp_val((val), 0, 255) >> 4)
222 #define PWM_FROM_REG(val) ((val) << 4)
224 #define FAN_CHAN_FROM_REG(reg) (((reg) >> 5) & 7)
225 #define FAN_CHAN_TO_REG(val, reg) \
226 (((reg) & 0x1F) | (((val) << 5) & 0xe0))
228 #define AUTO_TEMP_MIN_TO_REG(val, reg) \
229 ((((val) / 500) & 0xf8) | ((reg) & 0x7))
230 #define AUTO_TEMP_RANGE_FROM_REG(reg) (5000 * (1 << ((reg) & 0x7)))
231 #define AUTO_TEMP_MIN_FROM_REG(reg) (1000 * ((((reg) >> 3) & 0x1f) << 2))
233 #define AUTO_TEMP_MIN_FROM_REG_DEG(reg) ((((reg) >> 3) & 0x1f) << 2)
235 #define AUTO_TEMP_OFF_FROM_REG(reg) \
236 (AUTO_TEMP_MIN_FROM_REG(reg) - 5000)
238 #define AUTO_TEMP_MAX_FROM_REG(reg) \
239 (AUTO_TEMP_RANGE_FROM_REG(reg) + \
240 AUTO_TEMP_MIN_FROM_REG(reg))
242 static int AUTO_TEMP_MAX_TO_REG(int val
, int reg
, int pwm
)
245 int range
= val
- AUTO_TEMP_MIN_FROM_REG(reg
);
247 range
= ((val
- AUTO_TEMP_MIN_FROM_REG(reg
))*10)/(16 - pwm
);
248 ret
= ((reg
& 0xf8) |
251 range
< 40000 ? 2 : range
< 80000 ? 3 : 4));
255 /* FAN auto control */
256 #define GET_FAN_AUTO_BITFIELD(data, idx) \
257 (*(data)->chan_select_table)[FAN_CHAN_FROM_REG((data)->conf1)][idx % 2]
260 * The tables below contains the possible values for the auto fan
261 * control bitfields. the index in the table is the register value.
262 * MSb is the auto fan control enable bit, so the four first entries
263 * in the table disables auto fan control when both bitfields are zero.
265 static const auto_chan_table_t auto_channel_select_table_adm1031
= {
266 { 0, 0 }, { 0, 0 }, { 0, 0 }, { 0, 0 },
267 { 2 /* 0b010 */ , 4 /* 0b100 */ },
268 { 2 /* 0b010 */ , 2 /* 0b010 */ },
269 { 4 /* 0b100 */ , 4 /* 0b100 */ },
270 { 7 /* 0b111 */ , 7 /* 0b111 */ },
273 static const auto_chan_table_t auto_channel_select_table_adm1030
= {
274 { 0, 0 }, { 0, 0 }, { 0, 0 }, { 0, 0 },
275 { 2 /* 0b10 */ , 0 },
276 { 0xff /* invalid */ , 0 },
277 { 0xff /* invalid */ , 0 },
278 { 3 /* 0b11 */ , 0 },
282 * That function checks if a bitfield is valid and returns the other bitfield
283 * nearest match if no exact match where found.
286 get_fan_auto_nearest(struct adm1031_data
*data
, int chan
, u8 val
, u8 reg
)
289 int first_match
= -1, exact_match
= -1;
291 (*data
->chan_select_table
)[FAN_CHAN_FROM_REG(reg
)][chan
? 0 : 1];
296 for (i
= 0; i
< 8; i
++) {
297 if ((val
== (*data
->chan_select_table
)[i
][chan
]) &&
298 ((*data
->chan_select_table
)[i
][chan
? 0 : 1] ==
300 /* We found an exact match */
303 } else if (val
== (*data
->chan_select_table
)[i
][chan
] &&
306 * Save the first match in case of an exact match has
313 if (exact_match
>= 0)
315 else if (first_match
>= 0)
321 static ssize_t
fan_auto_channel_show(struct device
*dev
,
322 struct device_attribute
*attr
, char *buf
)
324 int nr
= to_sensor_dev_attr(attr
)->index
;
325 struct adm1031_data
*data
= adm1031_update_device(dev
);
326 return sprintf(buf
, "%d\n", GET_FAN_AUTO_BITFIELD(data
, nr
));
330 fan_auto_channel_store(struct device
*dev
, struct device_attribute
*attr
,
331 const char *buf
, size_t count
)
333 struct adm1031_data
*data
= dev_get_drvdata(dev
);
334 struct i2c_client
*client
= data
->client
;
335 int nr
= to_sensor_dev_attr(attr
)->index
;
341 ret
= kstrtol(buf
, 10, &val
);
345 old_fan_mode
= data
->conf1
;
347 mutex_lock(&data
->update_lock
);
349 ret
= get_fan_auto_nearest(data
, nr
, val
, data
->conf1
);
351 mutex_unlock(&data
->update_lock
);
355 data
->conf1
= FAN_CHAN_TO_REG(reg
, data
->conf1
);
356 if ((data
->conf1
& ADM1031_CONF1_AUTO_MODE
) ^
357 (old_fan_mode
& ADM1031_CONF1_AUTO_MODE
)) {
358 if (data
->conf1
& ADM1031_CONF1_AUTO_MODE
) {
360 * Switch to Auto Fan Mode
362 * Set PWM registers to 33% Both
364 data
->old_pwm
[0] = data
->pwm
[0];
365 data
->old_pwm
[1] = data
->pwm
[1];
366 adm1031_write_value(client
, ADM1031_REG_PWM
, 0x55);
368 /* Switch to Manual Mode */
369 data
->pwm
[0] = data
->old_pwm
[0];
370 data
->pwm
[1] = data
->old_pwm
[1];
371 /* Restore PWM registers */
372 adm1031_write_value(client
, ADM1031_REG_PWM
,
373 data
->pwm
[0] | (data
->pwm
[1] << 4));
376 data
->conf1
= FAN_CHAN_TO_REG(reg
, data
->conf1
);
377 adm1031_write_value(client
, ADM1031_REG_CONF1
, data
->conf1
);
378 mutex_unlock(&data
->update_lock
);
382 static SENSOR_DEVICE_ATTR_RW(auto_fan1_channel
, fan_auto_channel
, 0);
383 static SENSOR_DEVICE_ATTR_RW(auto_fan2_channel
, fan_auto_channel
, 1);
386 static ssize_t
auto_temp_off_show(struct device
*dev
,
387 struct device_attribute
*attr
, char *buf
)
389 int nr
= to_sensor_dev_attr(attr
)->index
;
390 struct adm1031_data
*data
= adm1031_update_device(dev
);
391 return sprintf(buf
, "%d\n",
392 AUTO_TEMP_OFF_FROM_REG(data
->auto_temp
[nr
]));
394 static ssize_t
auto_temp_min_show(struct device
*dev
,
395 struct device_attribute
*attr
, char *buf
)
397 int nr
= to_sensor_dev_attr(attr
)->index
;
398 struct adm1031_data
*data
= adm1031_update_device(dev
);
399 return sprintf(buf
, "%d\n",
400 AUTO_TEMP_MIN_FROM_REG(data
->auto_temp
[nr
]));
403 auto_temp_min_store(struct device
*dev
, struct device_attribute
*attr
,
404 const char *buf
, size_t count
)
406 struct adm1031_data
*data
= dev_get_drvdata(dev
);
407 struct i2c_client
*client
= data
->client
;
408 int nr
= to_sensor_dev_attr(attr
)->index
;
412 ret
= kstrtol(buf
, 10, &val
);
416 val
= clamp_val(val
, 0, 127000);
417 mutex_lock(&data
->update_lock
);
418 data
->auto_temp
[nr
] = AUTO_TEMP_MIN_TO_REG(val
, data
->auto_temp
[nr
]);
419 adm1031_write_value(client
, ADM1031_REG_AUTO_TEMP(nr
),
420 data
->auto_temp
[nr
]);
421 mutex_unlock(&data
->update_lock
);
424 static ssize_t
auto_temp_max_show(struct device
*dev
,
425 struct device_attribute
*attr
, char *buf
)
427 int nr
= to_sensor_dev_attr(attr
)->index
;
428 struct adm1031_data
*data
= adm1031_update_device(dev
);
429 return sprintf(buf
, "%d\n",
430 AUTO_TEMP_MAX_FROM_REG(data
->auto_temp
[nr
]));
433 auto_temp_max_store(struct device
*dev
, struct device_attribute
*attr
,
434 const char *buf
, size_t count
)
436 struct adm1031_data
*data
= dev_get_drvdata(dev
);
437 struct i2c_client
*client
= data
->client
;
438 int nr
= to_sensor_dev_attr(attr
)->index
;
442 ret
= kstrtol(buf
, 10, &val
);
446 val
= clamp_val(val
, 0, 127000);
447 mutex_lock(&data
->update_lock
);
448 data
->temp_max
[nr
] = AUTO_TEMP_MAX_TO_REG(val
, data
->auto_temp
[nr
],
450 adm1031_write_value(client
, ADM1031_REG_AUTO_TEMP(nr
),
452 mutex_unlock(&data
->update_lock
);
456 static SENSOR_DEVICE_ATTR_RO(auto_temp1_off
, auto_temp_off
, 0);
457 static SENSOR_DEVICE_ATTR_RW(auto_temp1_min
, auto_temp_min
, 0);
458 static SENSOR_DEVICE_ATTR_RW(auto_temp1_max
, auto_temp_max
, 0);
459 static SENSOR_DEVICE_ATTR_RO(auto_temp2_off
, auto_temp_off
, 1);
460 static SENSOR_DEVICE_ATTR_RW(auto_temp2_min
, auto_temp_min
, 1);
461 static SENSOR_DEVICE_ATTR_RW(auto_temp2_max
, auto_temp_max
, 1);
462 static SENSOR_DEVICE_ATTR_RO(auto_temp3_off
, auto_temp_off
, 2);
463 static SENSOR_DEVICE_ATTR_RW(auto_temp3_min
, auto_temp_min
, 2);
464 static SENSOR_DEVICE_ATTR_RW(auto_temp3_max
, auto_temp_max
, 2);
467 static ssize_t
pwm_show(struct device
*dev
, struct device_attribute
*attr
,
470 int nr
= to_sensor_dev_attr(attr
)->index
;
471 struct adm1031_data
*data
= adm1031_update_device(dev
);
472 return sprintf(buf
, "%d\n", PWM_FROM_REG(data
->pwm
[nr
]));
474 static ssize_t
pwm_store(struct device
*dev
, struct device_attribute
*attr
,
475 const char *buf
, size_t count
)
477 struct adm1031_data
*data
= dev_get_drvdata(dev
);
478 struct i2c_client
*client
= data
->client
;
479 int nr
= to_sensor_dev_attr(attr
)->index
;
483 ret
= kstrtol(buf
, 10, &val
);
487 mutex_lock(&data
->update_lock
);
488 if ((data
->conf1
& ADM1031_CONF1_AUTO_MODE
) &&
489 (((val
>>4) & 0xf) != 5)) {
490 /* In automatic mode, the only PWM accepted is 33% */
491 mutex_unlock(&data
->update_lock
);
494 data
->pwm
[nr
] = PWM_TO_REG(val
);
495 reg
= adm1031_read_value(client
, ADM1031_REG_PWM
);
496 adm1031_write_value(client
, ADM1031_REG_PWM
,
497 nr
? ((data
->pwm
[nr
] << 4) & 0xf0) | (reg
& 0xf)
498 : (data
->pwm
[nr
] & 0xf) | (reg
& 0xf0));
499 mutex_unlock(&data
->update_lock
);
503 static SENSOR_DEVICE_ATTR_RW(pwm1
, pwm
, 0);
504 static SENSOR_DEVICE_ATTR_RW(pwm2
, pwm
, 1);
505 static SENSOR_DEVICE_ATTR_RW(auto_fan1_min_pwm
, pwm
, 0);
506 static SENSOR_DEVICE_ATTR_RW(auto_fan2_min_pwm
, pwm
, 1);
511 * That function checks the cases where the fan reading is not
512 * relevant. It is used to provide 0 as fan reading when the fan is
513 * not supposed to run
515 static int trust_fan_readings(struct adm1031_data
*data
, int chan
)
519 if (data
->conf1
& ADM1031_CONF1_AUTO_MODE
) {
520 switch (data
->conf1
& 0x60) {
523 * remote temp1 controls fan1,
524 * remote temp2 controls fan2
526 res
= data
->temp
[chan
+1] >=
527 AUTO_TEMP_MIN_FROM_REG_DEG(data
->auto_temp
[chan
+1]);
529 case 0x20: /* remote temp1 controls both fans */
532 AUTO_TEMP_MIN_FROM_REG_DEG(data
->auto_temp
[1]);
534 case 0x40: /* remote temp2 controls both fans */
537 AUTO_TEMP_MIN_FROM_REG_DEG(data
->auto_temp
[2]);
539 case 0x60: /* max controls both fans */
542 AUTO_TEMP_MIN_FROM_REG_DEG(data
->auto_temp
[0])
544 AUTO_TEMP_MIN_FROM_REG_DEG(data
->auto_temp
[1])
545 || (data
->chip_type
== adm1031
547 AUTO_TEMP_MIN_FROM_REG_DEG(data
->auto_temp
[2]));
551 res
= data
->pwm
[chan
] > 0;
556 static ssize_t
fan_show(struct device
*dev
, struct device_attribute
*attr
,
559 int nr
= to_sensor_dev_attr(attr
)->index
;
560 struct adm1031_data
*data
= adm1031_update_device(dev
);
563 value
= trust_fan_readings(data
, nr
) ? FAN_FROM_REG(data
->fan
[nr
],
564 FAN_DIV_FROM_REG(data
->fan_div
[nr
])) : 0;
565 return sprintf(buf
, "%d\n", value
);
568 static ssize_t
fan_div_show(struct device
*dev
, struct device_attribute
*attr
,
571 int nr
= to_sensor_dev_attr(attr
)->index
;
572 struct adm1031_data
*data
= adm1031_update_device(dev
);
573 return sprintf(buf
, "%d\n", FAN_DIV_FROM_REG(data
->fan_div
[nr
]));
575 static ssize_t
fan_min_show(struct device
*dev
, struct device_attribute
*attr
,
578 int nr
= to_sensor_dev_attr(attr
)->index
;
579 struct adm1031_data
*data
= adm1031_update_device(dev
);
580 return sprintf(buf
, "%d\n",
581 FAN_FROM_REG(data
->fan_min
[nr
],
582 FAN_DIV_FROM_REG(data
->fan_div
[nr
])));
584 static ssize_t
fan_min_store(struct device
*dev
,
585 struct device_attribute
*attr
, const char *buf
,
588 struct adm1031_data
*data
= dev_get_drvdata(dev
);
589 struct i2c_client
*client
= data
->client
;
590 int nr
= to_sensor_dev_attr(attr
)->index
;
594 ret
= kstrtol(buf
, 10, &val
);
598 mutex_lock(&data
->update_lock
);
601 FAN_TO_REG(val
, FAN_DIV_FROM_REG(data
->fan_div
[nr
]));
603 data
->fan_min
[nr
] = 0xff;
605 adm1031_write_value(client
, ADM1031_REG_FAN_MIN(nr
), data
->fan_min
[nr
]);
606 mutex_unlock(&data
->update_lock
);
609 static ssize_t
fan_div_store(struct device
*dev
,
610 struct device_attribute
*attr
, const char *buf
,
613 struct adm1031_data
*data
= dev_get_drvdata(dev
);
614 struct i2c_client
*client
= data
->client
;
615 int nr
= to_sensor_dev_attr(attr
)->index
;
622 ret
= kstrtol(buf
, 10, &val
);
626 tmp
= val
== 8 ? 0xc0 :
634 mutex_lock(&data
->update_lock
);
635 /* Get fresh readings */
636 data
->fan_div
[nr
] = adm1031_read_value(client
,
637 ADM1031_REG_FAN_DIV(nr
));
638 data
->fan_min
[nr
] = adm1031_read_value(client
,
639 ADM1031_REG_FAN_MIN(nr
));
641 /* Write the new clock divider and fan min */
642 old_div
= FAN_DIV_FROM_REG(data
->fan_div
[nr
]);
643 data
->fan_div
[nr
] = tmp
| (0x3f & data
->fan_div
[nr
]);
644 new_min
= data
->fan_min
[nr
] * old_div
/ val
;
645 data
->fan_min
[nr
] = new_min
> 0xff ? 0xff : new_min
;
647 adm1031_write_value(client
, ADM1031_REG_FAN_DIV(nr
),
649 adm1031_write_value(client
, ADM1031_REG_FAN_MIN(nr
),
652 /* Invalidate the cache: fan speed is no longer valid */
654 mutex_unlock(&data
->update_lock
);
658 static SENSOR_DEVICE_ATTR_RO(fan1_input
, fan
, 0);
659 static SENSOR_DEVICE_ATTR_RW(fan1_min
, fan_min
, 0);
660 static SENSOR_DEVICE_ATTR_RW(fan1_div
, fan_div
, 0);
661 static SENSOR_DEVICE_ATTR_RO(fan2_input
, fan
, 1);
662 static SENSOR_DEVICE_ATTR_RW(fan2_min
, fan_min
, 1);
663 static SENSOR_DEVICE_ATTR_RW(fan2_div
, fan_div
, 1);
666 static ssize_t
temp_show(struct device
*dev
, struct device_attribute
*attr
,
669 int nr
= to_sensor_dev_attr(attr
)->index
;
670 struct adm1031_data
*data
= adm1031_update_device(dev
);
673 ((data
->ext_temp
[nr
] >> 6) & 0x3) * 2 :
674 (((data
->ext_temp
[nr
] >> ((nr
- 1) * 3)) & 7));
675 return sprintf(buf
, "%d\n", TEMP_FROM_REG_EXT(data
->temp
[nr
], ext
));
677 static ssize_t
temp_offset_show(struct device
*dev
,
678 struct device_attribute
*attr
, char *buf
)
680 int nr
= to_sensor_dev_attr(attr
)->index
;
681 struct adm1031_data
*data
= adm1031_update_device(dev
);
682 return sprintf(buf
, "%d\n",
683 TEMP_OFFSET_FROM_REG(data
->temp_offset
[nr
]));
685 static ssize_t
temp_min_show(struct device
*dev
,
686 struct device_attribute
*attr
, char *buf
)
688 int nr
= to_sensor_dev_attr(attr
)->index
;
689 struct adm1031_data
*data
= adm1031_update_device(dev
);
690 return sprintf(buf
, "%d\n", TEMP_FROM_REG(data
->temp_min
[nr
]));
692 static ssize_t
temp_max_show(struct device
*dev
,
693 struct device_attribute
*attr
, char *buf
)
695 int nr
= to_sensor_dev_attr(attr
)->index
;
696 struct adm1031_data
*data
= adm1031_update_device(dev
);
697 return sprintf(buf
, "%d\n", TEMP_FROM_REG(data
->temp_max
[nr
]));
699 static ssize_t
temp_crit_show(struct device
*dev
,
700 struct device_attribute
*attr
, char *buf
)
702 int nr
= to_sensor_dev_attr(attr
)->index
;
703 struct adm1031_data
*data
= adm1031_update_device(dev
);
704 return sprintf(buf
, "%d\n", TEMP_FROM_REG(data
->temp_crit
[nr
]));
706 static ssize_t
temp_offset_store(struct device
*dev
,
707 struct device_attribute
*attr
,
708 const char *buf
, size_t count
)
710 struct adm1031_data
*data
= dev_get_drvdata(dev
);
711 struct i2c_client
*client
= data
->client
;
712 int nr
= to_sensor_dev_attr(attr
)->index
;
716 ret
= kstrtol(buf
, 10, &val
);
720 val
= clamp_val(val
, -15000, 15000);
721 mutex_lock(&data
->update_lock
);
722 data
->temp_offset
[nr
] = TEMP_OFFSET_TO_REG(val
);
723 adm1031_write_value(client
, ADM1031_REG_TEMP_OFFSET(nr
),
724 data
->temp_offset
[nr
]);
725 mutex_unlock(&data
->update_lock
);
728 static ssize_t
temp_min_store(struct device
*dev
,
729 struct device_attribute
*attr
, const char *buf
,
732 struct adm1031_data
*data
= dev_get_drvdata(dev
);
733 struct i2c_client
*client
= data
->client
;
734 int nr
= to_sensor_dev_attr(attr
)->index
;
738 ret
= kstrtol(buf
, 10, &val
);
742 val
= clamp_val(val
, -55000, 127000);
743 mutex_lock(&data
->update_lock
);
744 data
->temp_min
[nr
] = TEMP_TO_REG(val
);
745 adm1031_write_value(client
, ADM1031_REG_TEMP_MIN(nr
),
747 mutex_unlock(&data
->update_lock
);
750 static ssize_t
temp_max_store(struct device
*dev
,
751 struct device_attribute
*attr
, const char *buf
,
754 struct adm1031_data
*data
= dev_get_drvdata(dev
);
755 struct i2c_client
*client
= data
->client
;
756 int nr
= to_sensor_dev_attr(attr
)->index
;
760 ret
= kstrtol(buf
, 10, &val
);
764 val
= clamp_val(val
, -55000, 127000);
765 mutex_lock(&data
->update_lock
);
766 data
->temp_max
[nr
] = TEMP_TO_REG(val
);
767 adm1031_write_value(client
, ADM1031_REG_TEMP_MAX(nr
),
769 mutex_unlock(&data
->update_lock
);
772 static ssize_t
temp_crit_store(struct device
*dev
,
773 struct device_attribute
*attr
, const char *buf
,
776 struct adm1031_data
*data
= dev_get_drvdata(dev
);
777 struct i2c_client
*client
= data
->client
;
778 int nr
= to_sensor_dev_attr(attr
)->index
;
782 ret
= kstrtol(buf
, 10, &val
);
786 val
= clamp_val(val
, -55000, 127000);
787 mutex_lock(&data
->update_lock
);
788 data
->temp_crit
[nr
] = TEMP_TO_REG(val
);
789 adm1031_write_value(client
, ADM1031_REG_TEMP_CRIT(nr
),
790 data
->temp_crit
[nr
]);
791 mutex_unlock(&data
->update_lock
);
795 static SENSOR_DEVICE_ATTR_RO(temp1_input
, temp
, 0);
796 static SENSOR_DEVICE_ATTR_RW(temp1_offset
, temp_offset
, 0);
797 static SENSOR_DEVICE_ATTR_RW(temp1_min
, temp_min
, 0);
798 static SENSOR_DEVICE_ATTR_RW(temp1_max
, temp_max
, 0);
799 static SENSOR_DEVICE_ATTR_RW(temp1_crit
, temp_crit
, 0);
800 static SENSOR_DEVICE_ATTR_RO(temp2_input
, temp
, 1);
801 static SENSOR_DEVICE_ATTR_RW(temp2_offset
, temp_offset
, 1);
802 static SENSOR_DEVICE_ATTR_RW(temp2_min
, temp_min
, 1);
803 static SENSOR_DEVICE_ATTR_RW(temp2_max
, temp_max
, 1);
804 static SENSOR_DEVICE_ATTR_RW(temp2_crit
, temp_crit
, 1);
805 static SENSOR_DEVICE_ATTR_RO(temp3_input
, temp
, 2);
806 static SENSOR_DEVICE_ATTR_RW(temp3_offset
, temp_offset
, 2);
807 static SENSOR_DEVICE_ATTR_RW(temp3_min
, temp_min
, 2);
808 static SENSOR_DEVICE_ATTR_RW(temp3_max
, temp_max
, 2);
809 static SENSOR_DEVICE_ATTR_RW(temp3_crit
, temp_crit
, 2);
812 static ssize_t
alarms_show(struct device
*dev
, struct device_attribute
*attr
,
815 struct adm1031_data
*data
= adm1031_update_device(dev
);
816 return sprintf(buf
, "%d\n", data
->alarm
);
819 static DEVICE_ATTR_RO(alarms
);
821 static ssize_t
alarm_show(struct device
*dev
, struct device_attribute
*attr
,
824 int bitnr
= to_sensor_dev_attr(attr
)->index
;
825 struct adm1031_data
*data
= adm1031_update_device(dev
);
826 return sprintf(buf
, "%d\n", (data
->alarm
>> bitnr
) & 1);
829 static SENSOR_DEVICE_ATTR_RO(fan1_alarm
, alarm
, 0);
830 static SENSOR_DEVICE_ATTR_RO(fan1_fault
, alarm
, 1);
831 static SENSOR_DEVICE_ATTR_RO(temp2_max_alarm
, alarm
, 2);
832 static SENSOR_DEVICE_ATTR_RO(temp2_min_alarm
, alarm
, 3);
833 static SENSOR_DEVICE_ATTR_RO(temp2_crit_alarm
, alarm
, 4);
834 static SENSOR_DEVICE_ATTR_RO(temp2_fault
, alarm
, 5);
835 static SENSOR_DEVICE_ATTR_RO(temp1_max_alarm
, alarm
, 6);
836 static SENSOR_DEVICE_ATTR_RO(temp1_min_alarm
, alarm
, 7);
837 static SENSOR_DEVICE_ATTR_RO(fan2_alarm
, alarm
, 8);
838 static SENSOR_DEVICE_ATTR_RO(fan2_fault
, alarm
, 9);
839 static SENSOR_DEVICE_ATTR_RO(temp3_max_alarm
, alarm
, 10);
840 static SENSOR_DEVICE_ATTR_RO(temp3_min_alarm
, alarm
, 11);
841 static SENSOR_DEVICE_ATTR_RO(temp3_crit_alarm
, alarm
, 12);
842 static SENSOR_DEVICE_ATTR_RO(temp3_fault
, alarm
, 13);
843 static SENSOR_DEVICE_ATTR_RO(temp1_crit_alarm
, alarm
, 14);
845 /* Update Interval */
846 static const unsigned int update_intervals
[] = {
847 16000, 8000, 4000, 2000, 1000, 500, 250, 125,
850 static ssize_t
update_interval_show(struct device
*dev
,
851 struct device_attribute
*attr
, char *buf
)
853 struct adm1031_data
*data
= dev_get_drvdata(dev
);
855 return sprintf(buf
, "%u\n", data
->update_interval
);
858 static ssize_t
update_interval_store(struct device
*dev
,
859 struct device_attribute
*attr
,
860 const char *buf
, size_t count
)
862 struct adm1031_data
*data
= dev_get_drvdata(dev
);
863 struct i2c_client
*client
= data
->client
;
868 err
= kstrtoul(buf
, 10, &val
);
873 * Find the nearest update interval from the table.
874 * Use it to determine the matching update rate.
876 for (i
= 0; i
< ARRAY_SIZE(update_intervals
) - 1; i
++) {
877 if (val
>= update_intervals
[i
])
880 /* if not found, we point to the last entry (lowest update interval) */
882 /* set the new update rate while preserving other settings */
883 reg
= adm1031_read_value(client
, ADM1031_REG_FAN_FILTER
);
884 reg
&= ~ADM1031_UPDATE_RATE_MASK
;
885 reg
|= i
<< ADM1031_UPDATE_RATE_SHIFT
;
886 adm1031_write_value(client
, ADM1031_REG_FAN_FILTER
, reg
);
888 mutex_lock(&data
->update_lock
);
889 data
->update_interval
= update_intervals
[i
];
890 mutex_unlock(&data
->update_lock
);
895 static DEVICE_ATTR_RW(update_interval
);
897 static struct attribute
*adm1031_attributes
[] = {
898 &sensor_dev_attr_fan1_input
.dev_attr
.attr
,
899 &sensor_dev_attr_fan1_div
.dev_attr
.attr
,
900 &sensor_dev_attr_fan1_min
.dev_attr
.attr
,
901 &sensor_dev_attr_fan1_alarm
.dev_attr
.attr
,
902 &sensor_dev_attr_fan1_fault
.dev_attr
.attr
,
903 &sensor_dev_attr_pwm1
.dev_attr
.attr
,
904 &sensor_dev_attr_auto_fan1_channel
.dev_attr
.attr
,
905 &sensor_dev_attr_temp1_input
.dev_attr
.attr
,
906 &sensor_dev_attr_temp1_offset
.dev_attr
.attr
,
907 &sensor_dev_attr_temp1_min
.dev_attr
.attr
,
908 &sensor_dev_attr_temp1_min_alarm
.dev_attr
.attr
,
909 &sensor_dev_attr_temp1_max
.dev_attr
.attr
,
910 &sensor_dev_attr_temp1_max_alarm
.dev_attr
.attr
,
911 &sensor_dev_attr_temp1_crit
.dev_attr
.attr
,
912 &sensor_dev_attr_temp1_crit_alarm
.dev_attr
.attr
,
913 &sensor_dev_attr_temp2_input
.dev_attr
.attr
,
914 &sensor_dev_attr_temp2_offset
.dev_attr
.attr
,
915 &sensor_dev_attr_temp2_min
.dev_attr
.attr
,
916 &sensor_dev_attr_temp2_min_alarm
.dev_attr
.attr
,
917 &sensor_dev_attr_temp2_max
.dev_attr
.attr
,
918 &sensor_dev_attr_temp2_max_alarm
.dev_attr
.attr
,
919 &sensor_dev_attr_temp2_crit
.dev_attr
.attr
,
920 &sensor_dev_attr_temp2_crit_alarm
.dev_attr
.attr
,
921 &sensor_dev_attr_temp2_fault
.dev_attr
.attr
,
923 &sensor_dev_attr_auto_temp1_off
.dev_attr
.attr
,
924 &sensor_dev_attr_auto_temp1_min
.dev_attr
.attr
,
925 &sensor_dev_attr_auto_temp1_max
.dev_attr
.attr
,
927 &sensor_dev_attr_auto_temp2_off
.dev_attr
.attr
,
928 &sensor_dev_attr_auto_temp2_min
.dev_attr
.attr
,
929 &sensor_dev_attr_auto_temp2_max
.dev_attr
.attr
,
931 &sensor_dev_attr_auto_fan1_min_pwm
.dev_attr
.attr
,
933 &dev_attr_update_interval
.attr
,
934 &dev_attr_alarms
.attr
,
939 static const struct attribute_group adm1031_group
= {
940 .attrs
= adm1031_attributes
,
943 static struct attribute
*adm1031_attributes_opt
[] = {
944 &sensor_dev_attr_fan2_input
.dev_attr
.attr
,
945 &sensor_dev_attr_fan2_div
.dev_attr
.attr
,
946 &sensor_dev_attr_fan2_min
.dev_attr
.attr
,
947 &sensor_dev_attr_fan2_alarm
.dev_attr
.attr
,
948 &sensor_dev_attr_fan2_fault
.dev_attr
.attr
,
949 &sensor_dev_attr_pwm2
.dev_attr
.attr
,
950 &sensor_dev_attr_auto_fan2_channel
.dev_attr
.attr
,
951 &sensor_dev_attr_temp3_input
.dev_attr
.attr
,
952 &sensor_dev_attr_temp3_offset
.dev_attr
.attr
,
953 &sensor_dev_attr_temp3_min
.dev_attr
.attr
,
954 &sensor_dev_attr_temp3_min_alarm
.dev_attr
.attr
,
955 &sensor_dev_attr_temp3_max
.dev_attr
.attr
,
956 &sensor_dev_attr_temp3_max_alarm
.dev_attr
.attr
,
957 &sensor_dev_attr_temp3_crit
.dev_attr
.attr
,
958 &sensor_dev_attr_temp3_crit_alarm
.dev_attr
.attr
,
959 &sensor_dev_attr_temp3_fault
.dev_attr
.attr
,
960 &sensor_dev_attr_auto_temp3_off
.dev_attr
.attr
,
961 &sensor_dev_attr_auto_temp3_min
.dev_attr
.attr
,
962 &sensor_dev_attr_auto_temp3_max
.dev_attr
.attr
,
963 &sensor_dev_attr_auto_fan2_min_pwm
.dev_attr
.attr
,
967 static const struct attribute_group adm1031_group_opt
= {
968 .attrs
= adm1031_attributes_opt
,
971 /* Return 0 if detection is successful, -ENODEV otherwise */
972 static int adm1031_detect(struct i2c_client
*client
,
973 struct i2c_board_info
*info
)
975 struct i2c_adapter
*adapter
= client
->adapter
;
979 if (!i2c_check_functionality(adapter
, I2C_FUNC_SMBUS_BYTE_DATA
))
982 id
= i2c_smbus_read_byte_data(client
, 0x3d);
983 co
= i2c_smbus_read_byte_data(client
, 0x3e);
985 if (!((id
== 0x31 || id
== 0x30) && co
== 0x41))
987 name
= (id
== 0x30) ? "adm1030" : "adm1031";
989 strlcpy(info
->type
, name
, I2C_NAME_SIZE
);
994 static void adm1031_init_client(struct i2c_client
*client
)
996 unsigned int read_val
;
999 struct adm1031_data
*data
= i2c_get_clientdata(client
);
1001 mask
= (ADM1031_CONF2_PWM1_ENABLE
| ADM1031_CONF2_TACH1_ENABLE
);
1002 if (data
->chip_type
== adm1031
) {
1003 mask
|= (ADM1031_CONF2_PWM2_ENABLE
|
1004 ADM1031_CONF2_TACH2_ENABLE
);
1006 /* Initialize the ADM1031 chip (enables fan speed reading ) */
1007 read_val
= adm1031_read_value(client
, ADM1031_REG_CONF2
);
1008 if ((read_val
| mask
) != read_val
)
1009 adm1031_write_value(client
, ADM1031_REG_CONF2
, read_val
| mask
);
1011 read_val
= adm1031_read_value(client
, ADM1031_REG_CONF1
);
1012 if ((read_val
| ADM1031_CONF1_MONITOR_ENABLE
) != read_val
) {
1013 adm1031_write_value(client
, ADM1031_REG_CONF1
,
1014 read_val
| ADM1031_CONF1_MONITOR_ENABLE
);
1017 /* Read the chip's update rate */
1018 mask
= ADM1031_UPDATE_RATE_MASK
;
1019 read_val
= adm1031_read_value(client
, ADM1031_REG_FAN_FILTER
);
1020 i
= (read_val
& mask
) >> ADM1031_UPDATE_RATE_SHIFT
;
1021 /* Save it as update interval */
1022 data
->update_interval
= update_intervals
[i
];
1025 static int adm1031_probe(struct i2c_client
*client
,
1026 const struct i2c_device_id
*id
)
1028 struct device
*dev
= &client
->dev
;
1029 struct device
*hwmon_dev
;
1030 struct adm1031_data
*data
;
1032 data
= devm_kzalloc(dev
, sizeof(struct adm1031_data
), GFP_KERNEL
);
1036 i2c_set_clientdata(client
, data
);
1037 data
->client
= client
;
1038 data
->chip_type
= id
->driver_data
;
1039 mutex_init(&data
->update_lock
);
1041 if (data
->chip_type
== adm1030
)
1042 data
->chan_select_table
= &auto_channel_select_table_adm1030
;
1044 data
->chan_select_table
= &auto_channel_select_table_adm1031
;
1046 /* Initialize the ADM1031 chip */
1047 adm1031_init_client(client
);
1050 data
->groups
[0] = &adm1031_group
;
1051 if (data
->chip_type
== adm1031
)
1052 data
->groups
[1] = &adm1031_group_opt
;
1054 hwmon_dev
= devm_hwmon_device_register_with_groups(dev
, client
->name
,
1055 data
, data
->groups
);
1056 return PTR_ERR_OR_ZERO(hwmon_dev
);
1059 static const struct i2c_device_id adm1031_id
[] = {
1060 { "adm1030", adm1030
},
1061 { "adm1031", adm1031
},
1064 MODULE_DEVICE_TABLE(i2c
, adm1031_id
);
1066 static struct i2c_driver adm1031_driver
= {
1067 .class = I2C_CLASS_HWMON
,
1071 .probe
= adm1031_probe
,
1072 .id_table
= adm1031_id
,
1073 .detect
= adm1031_detect
,
1074 .address_list
= normal_i2c
,
1077 module_i2c_driver(adm1031_driver
);
1079 MODULE_AUTHOR("Alexandre d'Alton <alex@alexdalton.org>");
1080 MODULE_DESCRIPTION("ADM1031/ADM1030 driver");
1081 MODULE_LICENSE("GPL");