1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * nct7802 - Driver for Nuvoton NCT7802Y
5 * Copyright (C) 2014 Guenter Roeck <linux@roeck-us.net>
8 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
10 #include <linux/err.h>
11 #include <linux/i2c.h>
12 #include <linux/init.h>
13 #include <linux/hwmon.h>
14 #include <linux/hwmon-sysfs.h>
15 #include <linux/jiffies.h>
16 #include <linux/module.h>
17 #include <linux/mutex.h>
18 #include <linux/regmap.h>
19 #include <linux/slab.h>
21 #define DRVNAME "nct7802"
23 static const u8 REG_VOLTAGE
[5] = { 0x09, 0x0a, 0x0c, 0x0d, 0x0e };
25 static const u8 REG_VOLTAGE_LIMIT_LSB
[2][5] = {
26 { 0x46, 0x00, 0x40, 0x42, 0x44 },
27 { 0x45, 0x00, 0x3f, 0x41, 0x43 },
30 static const u8 REG_VOLTAGE_LIMIT_MSB
[5] = { 0x48, 0x00, 0x47, 0x47, 0x48 };
32 static const u8 REG_VOLTAGE_LIMIT_MSB_SHIFT
[2][5] = {
38 #define REG_TEMP_LSB 0x05
39 #define REG_TEMP_PECI_LSB 0x08
40 #define REG_VOLTAGE_LOW 0x0f
41 #define REG_FANCOUNT_LOW 0x13
42 #define REG_START 0x21
43 #define REG_MODE 0x22 /* 7.2.32 Mode Selection Register */
44 #define REG_PECI_ENABLE 0x23
45 #define REG_FAN_ENABLE 0x24
46 #define REG_VMON_ENABLE 0x25
47 #define REG_PWM(x) (0x60 + (x))
48 #define REG_SMARTFAN_EN(x) (0x64 + (x) / 2)
49 #define SMARTFAN_EN_SHIFT(x) ((x) % 2 * 4)
50 #define REG_VENDOR_ID 0xfd
51 #define REG_CHIP_ID 0xfe
52 #define REG_VERSION_ID 0xff
55 * Data structures and manipulation thereof
59 struct regmap
*regmap
;
60 struct mutex access_lock
; /* for multi-byte read and write operations */
62 struct mutex in_alarm_lock
;
65 static ssize_t
temp_type_show(struct device
*dev
,
66 struct device_attribute
*attr
, char *buf
)
68 struct nct7802_data
*data
= dev_get_drvdata(dev
);
69 struct sensor_device_attribute
*sattr
= to_sensor_dev_attr(attr
);
73 ret
= regmap_read(data
->regmap
, REG_MODE
, &mode
);
77 return sprintf(buf
, "%u\n", (mode
>> (2 * sattr
->index
) & 3) + 2);
80 static ssize_t
temp_type_store(struct device
*dev
,
81 struct device_attribute
*attr
, const char *buf
,
84 struct nct7802_data
*data
= dev_get_drvdata(dev
);
85 struct sensor_device_attribute
*sattr
= to_sensor_dev_attr(attr
);
89 err
= kstrtouint(buf
, 0, &type
);
92 if (sattr
->index
== 2 && type
!= 4) /* RD3 */
94 if (type
< 3 || type
> 4)
96 err
= regmap_update_bits(data
->regmap
, REG_MODE
,
97 3 << 2 * sattr
->index
, (type
- 2) << 2 * sattr
->index
);
101 static ssize_t
pwm_mode_show(struct device
*dev
,
102 struct device_attribute
*attr
, char *buf
)
104 struct sensor_device_attribute
*sattr
= to_sensor_dev_attr(attr
);
105 struct nct7802_data
*data
= dev_get_drvdata(dev
);
109 if (sattr
->index
> 1)
110 return sprintf(buf
, "1\n");
112 ret
= regmap_read(data
->regmap
, 0x5E, ®val
);
116 return sprintf(buf
, "%u\n", !(regval
& (1 << sattr
->index
)));
119 static ssize_t
pwm_show(struct device
*dev
, struct device_attribute
*devattr
,
122 struct sensor_device_attribute
*attr
= to_sensor_dev_attr(devattr
);
123 struct nct7802_data
*data
= dev_get_drvdata(dev
);
128 return sprintf(buf
, "255\n");
130 ret
= regmap_read(data
->regmap
, attr
->index
, &val
);
134 return sprintf(buf
, "%d\n", val
);
137 static ssize_t
pwm_store(struct device
*dev
, struct device_attribute
*devattr
,
138 const char *buf
, size_t count
)
140 struct sensor_device_attribute
*attr
= to_sensor_dev_attr(devattr
);
141 struct nct7802_data
*data
= dev_get_drvdata(dev
);
145 err
= kstrtou8(buf
, 0, &val
);
149 err
= regmap_write(data
->regmap
, attr
->index
, val
);
150 return err
? : count
;
153 static ssize_t
pwm_enable_show(struct device
*dev
,
154 struct device_attribute
*attr
, char *buf
)
156 struct nct7802_data
*data
= dev_get_drvdata(dev
);
157 struct sensor_device_attribute
*sattr
= to_sensor_dev_attr(attr
);
158 unsigned int reg
, enabled
;
161 ret
= regmap_read(data
->regmap
, REG_SMARTFAN_EN(sattr
->index
), ®
);
164 enabled
= reg
>> SMARTFAN_EN_SHIFT(sattr
->index
) & 1;
165 return sprintf(buf
, "%u\n", enabled
+ 1);
168 static ssize_t
pwm_enable_store(struct device
*dev
,
169 struct device_attribute
*attr
,
170 const char *buf
, size_t count
)
172 struct nct7802_data
*data
= dev_get_drvdata(dev
);
173 struct sensor_device_attribute
*sattr
= to_sensor_dev_attr(attr
);
177 ret
= kstrtou8(buf
, 0, &val
);
180 if (val
< 1 || val
> 2)
182 ret
= regmap_update_bits(data
->regmap
, REG_SMARTFAN_EN(sattr
->index
),
183 1 << SMARTFAN_EN_SHIFT(sattr
->index
),
184 (val
- 1) << SMARTFAN_EN_SHIFT(sattr
->index
));
185 return ret
? : count
;
188 static int nct7802_read_temp(struct nct7802_data
*data
,
189 u8 reg_temp
, u8 reg_temp_low
, int *temp
)
191 unsigned int t1
, t2
= 0;
196 mutex_lock(&data
->access_lock
);
197 err
= regmap_read(data
->regmap
, reg_temp
, &t1
);
201 if (reg_temp_low
) { /* 11 bit data */
202 err
= regmap_read(data
->regmap
, reg_temp_low
, &t2
);
207 *temp
= (s16
)t1
/ 32 * 125;
209 mutex_unlock(&data
->access_lock
);
213 static int nct7802_read_fan(struct nct7802_data
*data
, u8 reg_fan
)
218 mutex_lock(&data
->access_lock
);
219 ret
= regmap_read(data
->regmap
, reg_fan
, &f1
);
222 ret
= regmap_read(data
->regmap
, REG_FANCOUNT_LOW
, &f2
);
225 ret
= (f1
<< 5) | (f2
>> 3);
226 /* convert fan count to rpm */
227 if (ret
== 0x1fff) /* maximum value, assume fan is stopped */
230 ret
= DIV_ROUND_CLOSEST(1350000U, ret
);
232 mutex_unlock(&data
->access_lock
);
236 static int nct7802_read_fan_min(struct nct7802_data
*data
, u8 reg_fan_low
,
242 mutex_lock(&data
->access_lock
);
243 ret
= regmap_read(data
->regmap
, reg_fan_low
, &f1
);
246 ret
= regmap_read(data
->regmap
, reg_fan_high
, &f2
);
249 ret
= f1
| ((f2
& 0xf8) << 5);
250 /* convert fan count to rpm */
251 if (ret
== 0x1fff) /* maximum value, assume no limit */
254 ret
= DIV_ROUND_CLOSEST(1350000U, ret
);
258 mutex_unlock(&data
->access_lock
);
262 static int nct7802_write_fan_min(struct nct7802_data
*data
, u8 reg_fan_low
,
263 u8 reg_fan_high
, unsigned long limit
)
268 limit
= DIV_ROUND_CLOSEST(1350000U, limit
);
271 limit
= clamp_val(limit
, 0, 0x1fff);
273 mutex_lock(&data
->access_lock
);
274 err
= regmap_write(data
->regmap
, reg_fan_low
, limit
& 0xff);
278 err
= regmap_write(data
->regmap
, reg_fan_high
, (limit
& 0x1f00) >> 5);
280 mutex_unlock(&data
->access_lock
);
284 static u8 nct7802_vmul
[] = { 4, 2, 2, 2, 2 };
286 static int nct7802_read_voltage(struct nct7802_data
*data
, int nr
, int index
)
291 mutex_lock(&data
->access_lock
);
292 if (index
== 0) { /* voltage */
293 ret
= regmap_read(data
->regmap
, REG_VOLTAGE
[nr
], &v1
);
296 ret
= regmap_read(data
->regmap
, REG_VOLTAGE_LOW
, &v2
);
299 ret
= ((v1
<< 2) | (v2
>> 6)) * nct7802_vmul
[nr
];
301 int shift
= 8 - REG_VOLTAGE_LIMIT_MSB_SHIFT
[index
- 1][nr
];
303 ret
= regmap_read(data
->regmap
,
304 REG_VOLTAGE_LIMIT_LSB
[index
- 1][nr
], &v1
);
307 ret
= regmap_read(data
->regmap
, REG_VOLTAGE_LIMIT_MSB
[nr
],
311 ret
= (v1
| ((v2
<< shift
) & 0x300)) * nct7802_vmul
[nr
];
314 mutex_unlock(&data
->access_lock
);
318 static int nct7802_write_voltage(struct nct7802_data
*data
, int nr
, int index
,
319 unsigned long voltage
)
321 int shift
= 8 - REG_VOLTAGE_LIMIT_MSB_SHIFT
[index
- 1][nr
];
324 voltage
= clamp_val(voltage
, 0, 0x3ff * nct7802_vmul
[nr
]);
325 voltage
= DIV_ROUND_CLOSEST(voltage
, nct7802_vmul
[nr
]);
327 mutex_lock(&data
->access_lock
);
328 err
= regmap_write(data
->regmap
,
329 REG_VOLTAGE_LIMIT_LSB
[index
- 1][nr
],
334 err
= regmap_update_bits(data
->regmap
, REG_VOLTAGE_LIMIT_MSB
[nr
],
335 0x0300 >> shift
, (voltage
& 0x0300) >> shift
);
337 mutex_unlock(&data
->access_lock
);
341 static ssize_t
in_show(struct device
*dev
, struct device_attribute
*attr
,
344 struct sensor_device_attribute_2
*sattr
= to_sensor_dev_attr_2(attr
);
345 struct nct7802_data
*data
= dev_get_drvdata(dev
);
348 voltage
= nct7802_read_voltage(data
, sattr
->nr
, sattr
->index
);
352 return sprintf(buf
, "%d\n", voltage
);
355 static ssize_t
in_store(struct device
*dev
, struct device_attribute
*attr
,
356 const char *buf
, size_t count
)
358 struct sensor_device_attribute_2
*sattr
= to_sensor_dev_attr_2(attr
);
359 struct nct7802_data
*data
= dev_get_drvdata(dev
);
360 int index
= sattr
->index
;
365 err
= kstrtoul(buf
, 10, &val
);
369 err
= nct7802_write_voltage(data
, nr
, index
, val
);
370 return err
? : count
;
373 static ssize_t
in_alarm_show(struct device
*dev
, struct device_attribute
*attr
,
376 struct sensor_device_attribute_2
*sattr
= to_sensor_dev_attr_2(attr
);
377 struct nct7802_data
*data
= dev_get_drvdata(dev
);
378 int volt
, min
, max
, ret
;
381 mutex_lock(&data
->in_alarm_lock
);
384 * The SMI Voltage status register is the only register giving a status
385 * for voltages. A bit is set for each input crossing a threshold, in
386 * both direction, but the "inside" or "outside" limits info is not
387 * available. Also this register is cleared on read.
388 * Note: this is not explicitly spelled out in the datasheet, but
390 * To deal with this we use a status cache with one validity bit and
391 * one status bit for each input. Validity is cleared at startup and
392 * each time the register reports a change, and the status is processed
393 * by software based on current input value and limits.
395 ret
= regmap_read(data
->regmap
, 0x1e, &val
); /* SMI Voltage status */
399 /* invalidate cached status for all inputs crossing a threshold */
400 data
->in_status
&= ~((val
& 0x0f) << 4);
402 /* if cached status for requested input is invalid, update it */
403 if (!(data
->in_status
& (0x10 << sattr
->index
))) {
404 ret
= nct7802_read_voltage(data
, sattr
->nr
, 0);
409 ret
= nct7802_read_voltage(data
, sattr
->nr
, 1);
414 ret
= nct7802_read_voltage(data
, sattr
->nr
, 2);
419 if (volt
< min
|| volt
> max
)
420 data
->in_status
|= (1 << sattr
->index
);
422 data
->in_status
&= ~(1 << sattr
->index
);
424 data
->in_status
|= 0x10 << sattr
->index
;
427 ret
= sprintf(buf
, "%u\n", !!(data
->in_status
& (1 << sattr
->index
)));
429 mutex_unlock(&data
->in_alarm_lock
);
433 static ssize_t
temp_show(struct device
*dev
, struct device_attribute
*attr
,
436 struct nct7802_data
*data
= dev_get_drvdata(dev
);
437 struct sensor_device_attribute_2
*sattr
= to_sensor_dev_attr_2(attr
);
440 err
= nct7802_read_temp(data
, sattr
->nr
, sattr
->index
, &temp
);
444 return sprintf(buf
, "%d\n", temp
);
447 static ssize_t
temp_store(struct device
*dev
, struct device_attribute
*attr
,
448 const char *buf
, size_t count
)
450 struct sensor_device_attribute_2
*sattr
= to_sensor_dev_attr_2(attr
);
451 struct nct7802_data
*data
= dev_get_drvdata(dev
);
456 err
= kstrtol(buf
, 10, &val
);
460 val
= DIV_ROUND_CLOSEST(clamp_val(val
, -128000, 127000), 1000);
462 err
= regmap_write(data
->regmap
, nr
, val
& 0xff);
463 return err
? : count
;
466 static ssize_t
fan_show(struct device
*dev
, struct device_attribute
*attr
,
469 struct sensor_device_attribute
*sattr
= to_sensor_dev_attr(attr
);
470 struct nct7802_data
*data
= dev_get_drvdata(dev
);
473 speed
= nct7802_read_fan(data
, sattr
->index
);
477 return sprintf(buf
, "%d\n", speed
);
480 static ssize_t
fan_min_show(struct device
*dev
, struct device_attribute
*attr
,
483 struct sensor_device_attribute_2
*sattr
= to_sensor_dev_attr_2(attr
);
484 struct nct7802_data
*data
= dev_get_drvdata(dev
);
487 speed
= nct7802_read_fan_min(data
, sattr
->nr
, sattr
->index
);
491 return sprintf(buf
, "%d\n", speed
);
494 static ssize_t
fan_min_store(struct device
*dev
,
495 struct device_attribute
*attr
, const char *buf
,
498 struct sensor_device_attribute_2
*sattr
= to_sensor_dev_attr_2(attr
);
499 struct nct7802_data
*data
= dev_get_drvdata(dev
);
503 err
= kstrtoul(buf
, 10, &val
);
507 err
= nct7802_write_fan_min(data
, sattr
->nr
, sattr
->index
, val
);
508 return err
? : count
;
511 static ssize_t
alarm_show(struct device
*dev
, struct device_attribute
*attr
,
514 struct nct7802_data
*data
= dev_get_drvdata(dev
);
515 struct sensor_device_attribute_2
*sattr
= to_sensor_dev_attr_2(attr
);
516 int bit
= sattr
->index
;
520 ret
= regmap_read(data
->regmap
, sattr
->nr
, &val
);
524 return sprintf(buf
, "%u\n", !!(val
& (1 << bit
)));
528 beep_show(struct device
*dev
, struct device_attribute
*attr
, char *buf
)
530 struct sensor_device_attribute_2
*sattr
= to_sensor_dev_attr_2(attr
);
531 struct nct7802_data
*data
= dev_get_drvdata(dev
);
535 err
= regmap_read(data
->regmap
, sattr
->nr
, ®val
);
539 return sprintf(buf
, "%u\n", !!(regval
& (1 << sattr
->index
)));
543 beep_store(struct device
*dev
, struct device_attribute
*attr
, const char *buf
,
546 struct sensor_device_attribute_2
*sattr
= to_sensor_dev_attr_2(attr
);
547 struct nct7802_data
*data
= dev_get_drvdata(dev
);
551 err
= kstrtoul(buf
, 10, &val
);
557 err
= regmap_update_bits(data
->regmap
, sattr
->nr
, 1 << sattr
->index
,
558 val
? 1 << sattr
->index
: 0);
559 return err
? : count
;
562 static SENSOR_DEVICE_ATTR_RW(temp1_type
, temp_type
, 0);
563 static SENSOR_DEVICE_ATTR_2_RO(temp1_input
, temp
, 0x01, REG_TEMP_LSB
);
564 static SENSOR_DEVICE_ATTR_2_RW(temp1_min
, temp
, 0x31, 0);
565 static SENSOR_DEVICE_ATTR_2_RW(temp1_max
, temp
, 0x30, 0);
566 static SENSOR_DEVICE_ATTR_2_RW(temp1_crit
, temp
, 0x3a, 0);
568 static SENSOR_DEVICE_ATTR_RW(temp2_type
, temp_type
, 1);
569 static SENSOR_DEVICE_ATTR_2_RO(temp2_input
, temp
, 0x02, REG_TEMP_LSB
);
570 static SENSOR_DEVICE_ATTR_2_RW(temp2_min
, temp
, 0x33, 0);
571 static SENSOR_DEVICE_ATTR_2_RW(temp2_max
, temp
, 0x32, 0);
572 static SENSOR_DEVICE_ATTR_2_RW(temp2_crit
, temp
, 0x3b, 0);
574 static SENSOR_DEVICE_ATTR_RW(temp3_type
, temp_type
, 2);
575 static SENSOR_DEVICE_ATTR_2_RO(temp3_input
, temp
, 0x03, REG_TEMP_LSB
);
576 static SENSOR_DEVICE_ATTR_2_RW(temp3_min
, temp
, 0x35, 0);
577 static SENSOR_DEVICE_ATTR_2_RW(temp3_max
, temp
, 0x34, 0);
578 static SENSOR_DEVICE_ATTR_2_RW(temp3_crit
, temp
, 0x3c, 0);
580 static SENSOR_DEVICE_ATTR_2_RO(temp4_input
, temp
, 0x04, 0);
581 static SENSOR_DEVICE_ATTR_2_RW(temp4_min
, temp
, 0x37, 0);
582 static SENSOR_DEVICE_ATTR_2_RW(temp4_max
, temp
, 0x36, 0);
583 static SENSOR_DEVICE_ATTR_2_RW(temp4_crit
, temp
, 0x3d, 0);
585 static SENSOR_DEVICE_ATTR_2_RO(temp5_input
, temp
, 0x06, REG_TEMP_PECI_LSB
);
586 static SENSOR_DEVICE_ATTR_2_RW(temp5_min
, temp
, 0x39, 0);
587 static SENSOR_DEVICE_ATTR_2_RW(temp5_max
, temp
, 0x38, 0);
588 static SENSOR_DEVICE_ATTR_2_RW(temp5_crit
, temp
, 0x3e, 0);
590 static SENSOR_DEVICE_ATTR_2_RO(temp6_input
, temp
, 0x07, REG_TEMP_PECI_LSB
);
592 static SENSOR_DEVICE_ATTR_2_RO(temp1_min_alarm
, alarm
, 0x18, 0);
593 static SENSOR_DEVICE_ATTR_2_RO(temp2_min_alarm
, alarm
, 0x18, 1);
594 static SENSOR_DEVICE_ATTR_2_RO(temp3_min_alarm
, alarm
, 0x18, 2);
595 static SENSOR_DEVICE_ATTR_2_RO(temp4_min_alarm
, alarm
, 0x18, 3);
596 static SENSOR_DEVICE_ATTR_2_RO(temp5_min_alarm
, alarm
, 0x18, 4);
598 static SENSOR_DEVICE_ATTR_2_RO(temp1_max_alarm
, alarm
, 0x19, 0);
599 static SENSOR_DEVICE_ATTR_2_RO(temp2_max_alarm
, alarm
, 0x19, 1);
600 static SENSOR_DEVICE_ATTR_2_RO(temp3_max_alarm
, alarm
, 0x19, 2);
601 static SENSOR_DEVICE_ATTR_2_RO(temp4_max_alarm
, alarm
, 0x19, 3);
602 static SENSOR_DEVICE_ATTR_2_RO(temp5_max_alarm
, alarm
, 0x19, 4);
604 static SENSOR_DEVICE_ATTR_2_RO(temp1_crit_alarm
, alarm
, 0x1b, 0);
605 static SENSOR_DEVICE_ATTR_2_RO(temp2_crit_alarm
, alarm
, 0x1b, 1);
606 static SENSOR_DEVICE_ATTR_2_RO(temp3_crit_alarm
, alarm
, 0x1b, 2);
607 static SENSOR_DEVICE_ATTR_2_RO(temp4_crit_alarm
, alarm
, 0x1b, 3);
608 static SENSOR_DEVICE_ATTR_2_RO(temp5_crit_alarm
, alarm
, 0x1b, 4);
610 static SENSOR_DEVICE_ATTR_2_RO(temp1_fault
, alarm
, 0x17, 0);
611 static SENSOR_DEVICE_ATTR_2_RO(temp2_fault
, alarm
, 0x17, 1);
612 static SENSOR_DEVICE_ATTR_2_RO(temp3_fault
, alarm
, 0x17, 2);
614 static SENSOR_DEVICE_ATTR_2_RW(temp1_beep
, beep
, 0x5c, 0);
615 static SENSOR_DEVICE_ATTR_2_RW(temp2_beep
, beep
, 0x5c, 1);
616 static SENSOR_DEVICE_ATTR_2_RW(temp3_beep
, beep
, 0x5c, 2);
617 static SENSOR_DEVICE_ATTR_2_RW(temp4_beep
, beep
, 0x5c, 3);
618 static SENSOR_DEVICE_ATTR_2_RW(temp5_beep
, beep
, 0x5c, 4);
619 static SENSOR_DEVICE_ATTR_2_RW(temp6_beep
, beep
, 0x5c, 5);
621 static struct attribute
*nct7802_temp_attrs
[] = {
622 &sensor_dev_attr_temp1_type
.dev_attr
.attr
,
623 &sensor_dev_attr_temp1_input
.dev_attr
.attr
,
624 &sensor_dev_attr_temp1_min
.dev_attr
.attr
,
625 &sensor_dev_attr_temp1_max
.dev_attr
.attr
,
626 &sensor_dev_attr_temp1_crit
.dev_attr
.attr
,
627 &sensor_dev_attr_temp1_min_alarm
.dev_attr
.attr
,
628 &sensor_dev_attr_temp1_max_alarm
.dev_attr
.attr
,
629 &sensor_dev_attr_temp1_crit_alarm
.dev_attr
.attr
,
630 &sensor_dev_attr_temp1_fault
.dev_attr
.attr
,
631 &sensor_dev_attr_temp1_beep
.dev_attr
.attr
,
633 &sensor_dev_attr_temp2_type
.dev_attr
.attr
, /* 10 */
634 &sensor_dev_attr_temp2_input
.dev_attr
.attr
,
635 &sensor_dev_attr_temp2_min
.dev_attr
.attr
,
636 &sensor_dev_attr_temp2_max
.dev_attr
.attr
,
637 &sensor_dev_attr_temp2_crit
.dev_attr
.attr
,
638 &sensor_dev_attr_temp2_min_alarm
.dev_attr
.attr
,
639 &sensor_dev_attr_temp2_max_alarm
.dev_attr
.attr
,
640 &sensor_dev_attr_temp2_crit_alarm
.dev_attr
.attr
,
641 &sensor_dev_attr_temp2_fault
.dev_attr
.attr
,
642 &sensor_dev_attr_temp2_beep
.dev_attr
.attr
,
644 &sensor_dev_attr_temp3_type
.dev_attr
.attr
, /* 20 */
645 &sensor_dev_attr_temp3_input
.dev_attr
.attr
,
646 &sensor_dev_attr_temp3_min
.dev_attr
.attr
,
647 &sensor_dev_attr_temp3_max
.dev_attr
.attr
,
648 &sensor_dev_attr_temp3_crit
.dev_attr
.attr
,
649 &sensor_dev_attr_temp3_min_alarm
.dev_attr
.attr
,
650 &sensor_dev_attr_temp3_max_alarm
.dev_attr
.attr
,
651 &sensor_dev_attr_temp3_crit_alarm
.dev_attr
.attr
,
652 &sensor_dev_attr_temp3_fault
.dev_attr
.attr
,
653 &sensor_dev_attr_temp3_beep
.dev_attr
.attr
,
655 &sensor_dev_attr_temp4_input
.dev_attr
.attr
, /* 30 */
656 &sensor_dev_attr_temp4_min
.dev_attr
.attr
,
657 &sensor_dev_attr_temp4_max
.dev_attr
.attr
,
658 &sensor_dev_attr_temp4_crit
.dev_attr
.attr
,
659 &sensor_dev_attr_temp4_min_alarm
.dev_attr
.attr
,
660 &sensor_dev_attr_temp4_max_alarm
.dev_attr
.attr
,
661 &sensor_dev_attr_temp4_crit_alarm
.dev_attr
.attr
,
662 &sensor_dev_attr_temp4_beep
.dev_attr
.attr
,
664 &sensor_dev_attr_temp5_input
.dev_attr
.attr
, /* 38 */
665 &sensor_dev_attr_temp5_min
.dev_attr
.attr
,
666 &sensor_dev_attr_temp5_max
.dev_attr
.attr
,
667 &sensor_dev_attr_temp5_crit
.dev_attr
.attr
,
668 &sensor_dev_attr_temp5_min_alarm
.dev_attr
.attr
,
669 &sensor_dev_attr_temp5_max_alarm
.dev_attr
.attr
,
670 &sensor_dev_attr_temp5_crit_alarm
.dev_attr
.attr
,
671 &sensor_dev_attr_temp5_beep
.dev_attr
.attr
,
673 &sensor_dev_attr_temp6_input
.dev_attr
.attr
, /* 46 */
674 &sensor_dev_attr_temp6_beep
.dev_attr
.attr
,
679 static umode_t
nct7802_temp_is_visible(struct kobject
*kobj
,
680 struct attribute
*attr
, int index
)
682 struct device
*dev
= kobj_to_dev(kobj
);
683 struct nct7802_data
*data
= dev_get_drvdata(dev
);
687 err
= regmap_read(data
->regmap
, REG_MODE
, ®
);
692 (reg
& 03) != 0x01 && (reg
& 0x03) != 0x02) /* RD1 */
695 if (index
>= 10 && index
< 20 &&
696 (reg
& 0x0c) != 0x04 && (reg
& 0x0c) != 0x08) /* RD2 */
698 if (index
>= 20 && index
< 30 && (reg
& 0x30) != 0x20) /* RD3 */
701 if (index
>= 30 && index
< 38) /* local */
704 err
= regmap_read(data
->regmap
, REG_PECI_ENABLE
, ®
);
708 if (index
>= 38 && index
< 46 && !(reg
& 0x01)) /* PECI 0 */
711 if (index
>= 0x46 && (!(reg
& 0x02))) /* PECI 1 */
717 static const struct attribute_group nct7802_temp_group
= {
718 .attrs
= nct7802_temp_attrs
,
719 .is_visible
= nct7802_temp_is_visible
,
722 static SENSOR_DEVICE_ATTR_2_RO(in0_input
, in
, 0, 0);
723 static SENSOR_DEVICE_ATTR_2_RW(in0_min
, in
, 0, 1);
724 static SENSOR_DEVICE_ATTR_2_RW(in0_max
, in
, 0, 2);
725 static SENSOR_DEVICE_ATTR_2_RO(in0_alarm
, in_alarm
, 0, 3);
726 static SENSOR_DEVICE_ATTR_2_RW(in0_beep
, beep
, 0x5a, 3);
728 static SENSOR_DEVICE_ATTR_2_RO(in1_input
, in
, 1, 0);
730 static SENSOR_DEVICE_ATTR_2_RO(in2_input
, in
, 2, 0);
731 static SENSOR_DEVICE_ATTR_2_RW(in2_min
, in
, 2, 1);
732 static SENSOR_DEVICE_ATTR_2_RW(in2_max
, in
, 2, 2);
733 static SENSOR_DEVICE_ATTR_2_RO(in2_alarm
, in_alarm
, 2, 0);
734 static SENSOR_DEVICE_ATTR_2_RW(in2_beep
, beep
, 0x5a, 0);
736 static SENSOR_DEVICE_ATTR_2_RO(in3_input
, in
, 3, 0);
737 static SENSOR_DEVICE_ATTR_2_RW(in3_min
, in
, 3, 1);
738 static SENSOR_DEVICE_ATTR_2_RW(in3_max
, in
, 3, 2);
739 static SENSOR_DEVICE_ATTR_2_RO(in3_alarm
, in_alarm
, 3, 1);
740 static SENSOR_DEVICE_ATTR_2_RW(in3_beep
, beep
, 0x5a, 1);
742 static SENSOR_DEVICE_ATTR_2_RO(in4_input
, in
, 4, 0);
743 static SENSOR_DEVICE_ATTR_2_RW(in4_min
, in
, 4, 1);
744 static SENSOR_DEVICE_ATTR_2_RW(in4_max
, in
, 4, 2);
745 static SENSOR_DEVICE_ATTR_2_RO(in4_alarm
, in_alarm
, 4, 2);
746 static SENSOR_DEVICE_ATTR_2_RW(in4_beep
, beep
, 0x5a, 2);
748 static struct attribute
*nct7802_in_attrs
[] = {
749 &sensor_dev_attr_in0_input
.dev_attr
.attr
,
750 &sensor_dev_attr_in0_min
.dev_attr
.attr
,
751 &sensor_dev_attr_in0_max
.dev_attr
.attr
,
752 &sensor_dev_attr_in0_alarm
.dev_attr
.attr
,
753 &sensor_dev_attr_in0_beep
.dev_attr
.attr
,
755 &sensor_dev_attr_in1_input
.dev_attr
.attr
, /* 5 */
757 &sensor_dev_attr_in2_input
.dev_attr
.attr
, /* 6 */
758 &sensor_dev_attr_in2_min
.dev_attr
.attr
,
759 &sensor_dev_attr_in2_max
.dev_attr
.attr
,
760 &sensor_dev_attr_in2_alarm
.dev_attr
.attr
,
761 &sensor_dev_attr_in2_beep
.dev_attr
.attr
,
763 &sensor_dev_attr_in3_input
.dev_attr
.attr
, /* 11 */
764 &sensor_dev_attr_in3_min
.dev_attr
.attr
,
765 &sensor_dev_attr_in3_max
.dev_attr
.attr
,
766 &sensor_dev_attr_in3_alarm
.dev_attr
.attr
,
767 &sensor_dev_attr_in3_beep
.dev_attr
.attr
,
769 &sensor_dev_attr_in4_input
.dev_attr
.attr
, /* 16 */
770 &sensor_dev_attr_in4_min
.dev_attr
.attr
,
771 &sensor_dev_attr_in4_max
.dev_attr
.attr
,
772 &sensor_dev_attr_in4_alarm
.dev_attr
.attr
,
773 &sensor_dev_attr_in4_beep
.dev_attr
.attr
,
778 static umode_t
nct7802_in_is_visible(struct kobject
*kobj
,
779 struct attribute
*attr
, int index
)
781 struct device
*dev
= kobj_to_dev(kobj
);
782 struct nct7802_data
*data
= dev_get_drvdata(dev
);
786 if (index
< 6) /* VCC, VCORE */
789 err
= regmap_read(data
->regmap
, REG_MODE
, ®
);
793 if (index
>= 6 && index
< 11 && (reg
& 0x03) != 0x03) /* VSEN1 */
795 if (index
>= 11 && index
< 16 && (reg
& 0x0c) != 0x0c) /* VSEN2 */
797 if (index
>= 16 && (reg
& 0x30) != 0x30) /* VSEN3 */
803 static const struct attribute_group nct7802_in_group
= {
804 .attrs
= nct7802_in_attrs
,
805 .is_visible
= nct7802_in_is_visible
,
808 static SENSOR_DEVICE_ATTR_RO(fan1_input
, fan
, 0x10);
809 static SENSOR_DEVICE_ATTR_2_RW(fan1_min
, fan_min
, 0x49, 0x4c);
810 static SENSOR_DEVICE_ATTR_2_RO(fan1_alarm
, alarm
, 0x1a, 0);
811 static SENSOR_DEVICE_ATTR_2_RW(fan1_beep
, beep
, 0x5b, 0);
812 static SENSOR_DEVICE_ATTR_RO(fan2_input
, fan
, 0x11);
813 static SENSOR_DEVICE_ATTR_2_RW(fan2_min
, fan_min
, 0x4a, 0x4d);
814 static SENSOR_DEVICE_ATTR_2_RO(fan2_alarm
, alarm
, 0x1a, 1);
815 static SENSOR_DEVICE_ATTR_2_RW(fan2_beep
, beep
, 0x5b, 1);
816 static SENSOR_DEVICE_ATTR_RO(fan3_input
, fan
, 0x12);
817 static SENSOR_DEVICE_ATTR_2_RW(fan3_min
, fan_min
, 0x4b, 0x4e);
818 static SENSOR_DEVICE_ATTR_2_RO(fan3_alarm
, alarm
, 0x1a, 2);
819 static SENSOR_DEVICE_ATTR_2_RW(fan3_beep
, beep
, 0x5b, 2);
821 /* 7.2.89 Fan Control Output Type */
822 static SENSOR_DEVICE_ATTR_RO(pwm1_mode
, pwm_mode
, 0);
823 static SENSOR_DEVICE_ATTR_RO(pwm2_mode
, pwm_mode
, 1);
824 static SENSOR_DEVICE_ATTR_RO(pwm3_mode
, pwm_mode
, 2);
826 /* 7.2.91... Fan Control Output Value */
827 static SENSOR_DEVICE_ATTR_RW(pwm1
, pwm
, REG_PWM(0));
828 static SENSOR_DEVICE_ATTR_RW(pwm2
, pwm
, REG_PWM(1));
829 static SENSOR_DEVICE_ATTR_RW(pwm3
, pwm
, REG_PWM(2));
831 /* 7.2.95... Temperature to Fan mapping Relationships Register */
832 static SENSOR_DEVICE_ATTR_RW(pwm1_enable
, pwm_enable
, 0);
833 static SENSOR_DEVICE_ATTR_RW(pwm2_enable
, pwm_enable
, 1);
834 static SENSOR_DEVICE_ATTR_RW(pwm3_enable
, pwm_enable
, 2);
836 static struct attribute
*nct7802_fan_attrs
[] = {
837 &sensor_dev_attr_fan1_input
.dev_attr
.attr
,
838 &sensor_dev_attr_fan1_min
.dev_attr
.attr
,
839 &sensor_dev_attr_fan1_alarm
.dev_attr
.attr
,
840 &sensor_dev_attr_fan1_beep
.dev_attr
.attr
,
841 &sensor_dev_attr_fan2_input
.dev_attr
.attr
,
842 &sensor_dev_attr_fan2_min
.dev_attr
.attr
,
843 &sensor_dev_attr_fan2_alarm
.dev_attr
.attr
,
844 &sensor_dev_attr_fan2_beep
.dev_attr
.attr
,
845 &sensor_dev_attr_fan3_input
.dev_attr
.attr
,
846 &sensor_dev_attr_fan3_min
.dev_attr
.attr
,
847 &sensor_dev_attr_fan3_alarm
.dev_attr
.attr
,
848 &sensor_dev_attr_fan3_beep
.dev_attr
.attr
,
853 static umode_t
nct7802_fan_is_visible(struct kobject
*kobj
,
854 struct attribute
*attr
, int index
)
856 struct device
*dev
= kobj_to_dev(kobj
);
857 struct nct7802_data
*data
= dev_get_drvdata(dev
);
858 int fan
= index
/ 4; /* 4 attributes per fan */
862 err
= regmap_read(data
->regmap
, REG_FAN_ENABLE
, ®
);
863 if (err
< 0 || !(reg
& (1 << fan
)))
869 static const struct attribute_group nct7802_fan_group
= {
870 .attrs
= nct7802_fan_attrs
,
871 .is_visible
= nct7802_fan_is_visible
,
874 static struct attribute
*nct7802_pwm_attrs
[] = {
875 &sensor_dev_attr_pwm1_enable
.dev_attr
.attr
,
876 &sensor_dev_attr_pwm1_mode
.dev_attr
.attr
,
877 &sensor_dev_attr_pwm1
.dev_attr
.attr
,
878 &sensor_dev_attr_pwm2_enable
.dev_attr
.attr
,
879 &sensor_dev_attr_pwm2_mode
.dev_attr
.attr
,
880 &sensor_dev_attr_pwm2
.dev_attr
.attr
,
881 &sensor_dev_attr_pwm3_enable
.dev_attr
.attr
,
882 &sensor_dev_attr_pwm3_mode
.dev_attr
.attr
,
883 &sensor_dev_attr_pwm3
.dev_attr
.attr
,
887 static const struct attribute_group nct7802_pwm_group
= {
888 .attrs
= nct7802_pwm_attrs
,
891 /* 7.2.115... 0x80-0x83, 0x84 Temperature (X-axis) transition */
892 static SENSOR_DEVICE_ATTR_2_RW(pwm1_auto_point1_temp
, temp
, 0x80, 0);
893 static SENSOR_DEVICE_ATTR_2_RW(pwm1_auto_point2_temp
, temp
, 0x81, 0);
894 static SENSOR_DEVICE_ATTR_2_RW(pwm1_auto_point3_temp
, temp
, 0x82, 0);
895 static SENSOR_DEVICE_ATTR_2_RW(pwm1_auto_point4_temp
, temp
, 0x83, 0);
896 static SENSOR_DEVICE_ATTR_2_RW(pwm1_auto_point5_temp
, temp
, 0x84, 0);
898 /* 7.2.120... 0x85-0x88 PWM (Y-axis) transition */
899 static SENSOR_DEVICE_ATTR_RW(pwm1_auto_point1_pwm
, pwm
, 0x85);
900 static SENSOR_DEVICE_ATTR_RW(pwm1_auto_point2_pwm
, pwm
, 0x86);
901 static SENSOR_DEVICE_ATTR_RW(pwm1_auto_point3_pwm
, pwm
, 0x87);
902 static SENSOR_DEVICE_ATTR_RW(pwm1_auto_point4_pwm
, pwm
, 0x88);
903 static SENSOR_DEVICE_ATTR_RO(pwm1_auto_point5_pwm
, pwm
, 0);
905 /* 7.2.124 Table 2 X-axis Transition Point 1 Register */
906 static SENSOR_DEVICE_ATTR_2_RW(pwm2_auto_point1_temp
, temp
, 0x90, 0);
907 static SENSOR_DEVICE_ATTR_2_RW(pwm2_auto_point2_temp
, temp
, 0x91, 0);
908 static SENSOR_DEVICE_ATTR_2_RW(pwm2_auto_point3_temp
, temp
, 0x92, 0);
909 static SENSOR_DEVICE_ATTR_2_RW(pwm2_auto_point4_temp
, temp
, 0x93, 0);
910 static SENSOR_DEVICE_ATTR_2_RW(pwm2_auto_point5_temp
, temp
, 0x94, 0);
912 /* 7.2.129 Table 2 Y-axis Transition Point 1 Register */
913 static SENSOR_DEVICE_ATTR_RW(pwm2_auto_point1_pwm
, pwm
, 0x95);
914 static SENSOR_DEVICE_ATTR_RW(pwm2_auto_point2_pwm
, pwm
, 0x96);
915 static SENSOR_DEVICE_ATTR_RW(pwm2_auto_point3_pwm
, pwm
, 0x97);
916 static SENSOR_DEVICE_ATTR_RW(pwm2_auto_point4_pwm
, pwm
, 0x98);
917 static SENSOR_DEVICE_ATTR_RO(pwm2_auto_point5_pwm
, pwm
, 0);
919 /* 7.2.133 Table 3 X-axis Transition Point 1 Register */
920 static SENSOR_DEVICE_ATTR_2_RW(pwm3_auto_point1_temp
, temp
, 0xA0, 0);
921 static SENSOR_DEVICE_ATTR_2_RW(pwm3_auto_point2_temp
, temp
, 0xA1, 0);
922 static SENSOR_DEVICE_ATTR_2_RW(pwm3_auto_point3_temp
, temp
, 0xA2, 0);
923 static SENSOR_DEVICE_ATTR_2_RW(pwm3_auto_point4_temp
, temp
, 0xA3, 0);
924 static SENSOR_DEVICE_ATTR_2_RW(pwm3_auto_point5_temp
, temp
, 0xA4, 0);
926 /* 7.2.138 Table 3 Y-axis Transition Point 1 Register */
927 static SENSOR_DEVICE_ATTR_RW(pwm3_auto_point1_pwm
, pwm
, 0xA5);
928 static SENSOR_DEVICE_ATTR_RW(pwm3_auto_point2_pwm
, pwm
, 0xA6);
929 static SENSOR_DEVICE_ATTR_RW(pwm3_auto_point3_pwm
, pwm
, 0xA7);
930 static SENSOR_DEVICE_ATTR_RW(pwm3_auto_point4_pwm
, pwm
, 0xA8);
931 static SENSOR_DEVICE_ATTR_RO(pwm3_auto_point5_pwm
, pwm
, 0);
933 static struct attribute
*nct7802_auto_point_attrs
[] = {
934 &sensor_dev_attr_pwm1_auto_point1_temp
.dev_attr
.attr
,
935 &sensor_dev_attr_pwm1_auto_point2_temp
.dev_attr
.attr
,
936 &sensor_dev_attr_pwm1_auto_point3_temp
.dev_attr
.attr
,
937 &sensor_dev_attr_pwm1_auto_point4_temp
.dev_attr
.attr
,
938 &sensor_dev_attr_pwm1_auto_point5_temp
.dev_attr
.attr
,
940 &sensor_dev_attr_pwm1_auto_point1_pwm
.dev_attr
.attr
,
941 &sensor_dev_attr_pwm1_auto_point2_pwm
.dev_attr
.attr
,
942 &sensor_dev_attr_pwm1_auto_point3_pwm
.dev_attr
.attr
,
943 &sensor_dev_attr_pwm1_auto_point4_pwm
.dev_attr
.attr
,
944 &sensor_dev_attr_pwm1_auto_point5_pwm
.dev_attr
.attr
,
946 &sensor_dev_attr_pwm2_auto_point1_temp
.dev_attr
.attr
,
947 &sensor_dev_attr_pwm2_auto_point2_temp
.dev_attr
.attr
,
948 &sensor_dev_attr_pwm2_auto_point3_temp
.dev_attr
.attr
,
949 &sensor_dev_attr_pwm2_auto_point4_temp
.dev_attr
.attr
,
950 &sensor_dev_attr_pwm2_auto_point5_temp
.dev_attr
.attr
,
952 &sensor_dev_attr_pwm2_auto_point1_pwm
.dev_attr
.attr
,
953 &sensor_dev_attr_pwm2_auto_point2_pwm
.dev_attr
.attr
,
954 &sensor_dev_attr_pwm2_auto_point3_pwm
.dev_attr
.attr
,
955 &sensor_dev_attr_pwm2_auto_point4_pwm
.dev_attr
.attr
,
956 &sensor_dev_attr_pwm2_auto_point5_pwm
.dev_attr
.attr
,
958 &sensor_dev_attr_pwm3_auto_point1_temp
.dev_attr
.attr
,
959 &sensor_dev_attr_pwm3_auto_point2_temp
.dev_attr
.attr
,
960 &sensor_dev_attr_pwm3_auto_point3_temp
.dev_attr
.attr
,
961 &sensor_dev_attr_pwm3_auto_point4_temp
.dev_attr
.attr
,
962 &sensor_dev_attr_pwm3_auto_point5_temp
.dev_attr
.attr
,
964 &sensor_dev_attr_pwm3_auto_point1_pwm
.dev_attr
.attr
,
965 &sensor_dev_attr_pwm3_auto_point2_pwm
.dev_attr
.attr
,
966 &sensor_dev_attr_pwm3_auto_point3_pwm
.dev_attr
.attr
,
967 &sensor_dev_attr_pwm3_auto_point4_pwm
.dev_attr
.attr
,
968 &sensor_dev_attr_pwm3_auto_point5_pwm
.dev_attr
.attr
,
973 static const struct attribute_group nct7802_auto_point_group
= {
974 .attrs
= nct7802_auto_point_attrs
,
977 static const struct attribute_group
*nct7802_groups
[] = {
982 &nct7802_auto_point_group
,
986 static int nct7802_detect(struct i2c_client
*client
,
987 struct i2c_board_info
*info
)
992 * Chip identification registers are only available in bank 0,
993 * so only attempt chip detection if bank 0 is selected
995 reg
= i2c_smbus_read_byte_data(client
, REG_BANK
);
999 reg
= i2c_smbus_read_byte_data(client
, REG_VENDOR_ID
);
1003 reg
= i2c_smbus_read_byte_data(client
, REG_CHIP_ID
);
1007 reg
= i2c_smbus_read_byte_data(client
, REG_VERSION_ID
);
1008 if (reg
< 0 || (reg
& 0xf0) != 0x20)
1011 /* Also validate lower bits of voltage and temperature registers */
1012 reg
= i2c_smbus_read_byte_data(client
, REG_TEMP_LSB
);
1013 if (reg
< 0 || (reg
& 0x1f))
1016 reg
= i2c_smbus_read_byte_data(client
, REG_TEMP_PECI_LSB
);
1017 if (reg
< 0 || (reg
& 0x3f))
1020 reg
= i2c_smbus_read_byte_data(client
, REG_VOLTAGE_LOW
);
1021 if (reg
< 0 || (reg
& 0x3f))
1024 strlcpy(info
->type
, "nct7802", I2C_NAME_SIZE
);
1028 static bool nct7802_regmap_is_volatile(struct device
*dev
, unsigned int reg
)
1030 return (reg
!= REG_BANK
&& reg
<= 0x20) ||
1031 (reg
>= REG_PWM(0) && reg
<= REG_PWM(2));
1034 static const struct regmap_config nct7802_regmap_config
= {
1037 .cache_type
= REGCACHE_RBTREE
,
1038 .volatile_reg
= nct7802_regmap_is_volatile
,
1041 static int nct7802_init_chip(struct nct7802_data
*data
)
1046 err
= regmap_update_bits(data
->regmap
, REG_START
, 0x01, 0x01);
1050 /* Enable local temperature sensor */
1051 err
= regmap_update_bits(data
->regmap
, REG_MODE
, 0x40, 0x40);
1055 /* Enable Vcore and VCC voltage monitoring */
1056 return regmap_update_bits(data
->regmap
, REG_VMON_ENABLE
, 0x03, 0x03);
1059 static int nct7802_probe(struct i2c_client
*client
)
1061 struct device
*dev
= &client
->dev
;
1062 struct nct7802_data
*data
;
1063 struct device
*hwmon_dev
;
1066 data
= devm_kzalloc(dev
, sizeof(*data
), GFP_KERNEL
);
1070 data
->regmap
= devm_regmap_init_i2c(client
, &nct7802_regmap_config
);
1071 if (IS_ERR(data
->regmap
))
1072 return PTR_ERR(data
->regmap
);
1074 mutex_init(&data
->access_lock
);
1075 mutex_init(&data
->in_alarm_lock
);
1077 ret
= nct7802_init_chip(data
);
1081 hwmon_dev
= devm_hwmon_device_register_with_groups(dev
, client
->name
,
1084 return PTR_ERR_OR_ZERO(hwmon_dev
);
1087 static const unsigned short nct7802_address_list
[] = {
1088 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f, I2C_CLIENT_END
1091 static const struct i2c_device_id nct7802_idtable
[] = {
1095 MODULE_DEVICE_TABLE(i2c
, nct7802_idtable
);
1097 static struct i2c_driver nct7802_driver
= {
1098 .class = I2C_CLASS_HWMON
,
1102 .detect
= nct7802_detect
,
1103 .probe_new
= nct7802_probe
,
1104 .id_table
= nct7802_idtable
,
1105 .address_list
= nct7802_address_list
,
1108 module_i2c_driver(nct7802_driver
);
1110 MODULE_AUTHOR("Guenter Roeck <linux@roeck-us.net>");
1111 MODULE_DESCRIPTION("NCT7802Y Hardware Monitoring Driver");
1112 MODULE_LICENSE("GPL v2");