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 * Resistance temperature detector (RTD) modes according to 7.2.32 Mode
58 #define RTD_MODE_CURRENT 0x1
59 #define RTD_MODE_THERMISTOR 0x2
60 #define RTD_MODE_VOLTAGE 0x3
62 #define MODE_RTD_MASK 0x3
63 #define MODE_LTD_EN 0x40
66 * Bit offset for sensors modes in REG_MODE.
67 * Valid for index 0..2, indicating RTD1..3.
69 #define MODE_BIT_OFFSET_RTD(index) ((index) * 2)
72 * Data structures and manipulation thereof
76 struct regmap *regmap;
77 struct mutex access_lock; /* for multi-byte read and write operations */
79 struct mutex in_alarm_lock;
82 static ssize_t temp_type_show(struct device *dev,
83 struct device_attribute *attr, char *buf)
85 struct nct7802_data *data = dev_get_drvdata(dev);
86 struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr);
90 ret = regmap_read(data->regmap, REG_MODE, &mode);
94 return sprintf(buf, "%u\n", (mode >> (2 * sattr->index) & 3) + 2);
97 static ssize_t temp_type_store(struct device *dev,
98 struct device_attribute *attr, const char *buf,
101 struct nct7802_data *data = dev_get_drvdata(dev);
102 struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr);
106 err = kstrtouint(buf, 0, &type);
109 if (sattr->index == 2 && type != 4) /* RD3 */
111 if (type < 3 || type > 4)
113 err = regmap_update_bits(data->regmap, REG_MODE,
114 3 << 2 * sattr->index, (type - 2) << 2 * sattr->index);
115 return err ? : count;
118 static ssize_t pwm_mode_show(struct device *dev,
119 struct device_attribute *attr, char *buf)
121 struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr);
122 struct nct7802_data *data = dev_get_drvdata(dev);
126 if (sattr->index > 1)
127 return sprintf(buf, "1\n");
129 ret = regmap_read(data->regmap, 0x5E, ®val);
133 return sprintf(buf, "%u\n", !(regval & (1 << sattr->index)));
136 static ssize_t pwm_show(struct device *dev, struct device_attribute *devattr,
139 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
140 struct nct7802_data *data = dev_get_drvdata(dev);
145 return sprintf(buf, "255\n");
147 ret = regmap_read(data->regmap, attr->index, &val);
151 return sprintf(buf, "%d\n", val);
154 static ssize_t pwm_store(struct device *dev, struct device_attribute *devattr,
155 const char *buf, size_t count)
157 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
158 struct nct7802_data *data = dev_get_drvdata(dev);
162 err = kstrtou8(buf, 0, &val);
166 err = regmap_write(data->regmap, attr->index, val);
167 return err ? : count;
170 static ssize_t pwm_enable_show(struct device *dev,
171 struct device_attribute *attr, char *buf)
173 struct nct7802_data *data = dev_get_drvdata(dev);
174 struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr);
175 unsigned int reg, enabled;
178 ret = regmap_read(data->regmap, REG_SMARTFAN_EN(sattr->index), ®);
181 enabled = reg >> SMARTFAN_EN_SHIFT(sattr->index) & 1;
182 return sprintf(buf, "%u\n", enabled + 1);
185 static ssize_t pwm_enable_store(struct device *dev,
186 struct device_attribute *attr,
187 const char *buf, size_t count)
189 struct nct7802_data *data = dev_get_drvdata(dev);
190 struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr);
194 ret = kstrtou8(buf, 0, &val);
197 if (val < 1 || val > 2)
199 ret = regmap_update_bits(data->regmap, REG_SMARTFAN_EN(sattr->index),
200 1 << SMARTFAN_EN_SHIFT(sattr->index),
201 (val - 1) << SMARTFAN_EN_SHIFT(sattr->index));
202 return ret ? : count;
205 static int nct7802_read_temp(struct nct7802_data *data,
206 u8 reg_temp, u8 reg_temp_low, int *temp)
208 unsigned int t1, t2 = 0;
213 mutex_lock(&data->access_lock);
214 err = regmap_read(data->regmap, reg_temp, &t1);
218 if (reg_temp_low) { /* 11 bit data */
219 err = regmap_read(data->regmap, reg_temp_low, &t2);
224 *temp = (s16)t1 / 32 * 125;
226 mutex_unlock(&data->access_lock);
230 static int nct7802_read_fan(struct nct7802_data *data, u8 reg_fan)
235 mutex_lock(&data->access_lock);
236 ret = regmap_read(data->regmap, reg_fan, &f1);
239 ret = regmap_read(data->regmap, REG_FANCOUNT_LOW, &f2);
242 ret = (f1 << 5) | (f2 >> 3);
243 /* convert fan count to rpm */
244 if (ret == 0x1fff) /* maximum value, assume fan is stopped */
247 ret = DIV_ROUND_CLOSEST(1350000U, ret);
249 mutex_unlock(&data->access_lock);
253 static int nct7802_read_fan_min(struct nct7802_data *data, u8 reg_fan_low,
259 mutex_lock(&data->access_lock);
260 ret = regmap_read(data->regmap, reg_fan_low, &f1);
263 ret = regmap_read(data->regmap, reg_fan_high, &f2);
266 ret = f1 | ((f2 & 0xf8) << 5);
267 /* convert fan count to rpm */
268 if (ret == 0x1fff) /* maximum value, assume no limit */
271 ret = DIV_ROUND_CLOSEST(1350000U, ret);
275 mutex_unlock(&data->access_lock);
279 static int nct7802_write_fan_min(struct nct7802_data *data, u8 reg_fan_low,
280 u8 reg_fan_high, unsigned long limit)
285 limit = DIV_ROUND_CLOSEST(1350000U, limit);
288 limit = clamp_val(limit, 0, 0x1fff);
290 mutex_lock(&data->access_lock);
291 err = regmap_write(data->regmap, reg_fan_low, limit & 0xff);
295 err = regmap_write(data->regmap, reg_fan_high, (limit & 0x1f00) >> 5);
297 mutex_unlock(&data->access_lock);
301 static u8 nct7802_vmul[] = { 4, 2, 2, 2, 2 };
303 static int nct7802_read_voltage(struct nct7802_data *data, int nr, int index)
308 mutex_lock(&data->access_lock);
309 if (index == 0) { /* voltage */
310 ret = regmap_read(data->regmap, REG_VOLTAGE[nr], &v1);
313 ret = regmap_read(data->regmap, REG_VOLTAGE_LOW, &v2);
316 ret = ((v1 << 2) | (v2 >> 6)) * nct7802_vmul[nr];
318 int shift = 8 - REG_VOLTAGE_LIMIT_MSB_SHIFT[index - 1][nr];
320 ret = regmap_read(data->regmap,
321 REG_VOLTAGE_LIMIT_LSB[index - 1][nr], &v1);
324 ret = regmap_read(data->regmap, REG_VOLTAGE_LIMIT_MSB[nr],
328 ret = (v1 | ((v2 << shift) & 0x300)) * nct7802_vmul[nr];
331 mutex_unlock(&data->access_lock);
335 static int nct7802_write_voltage(struct nct7802_data *data, int nr, int index,
336 unsigned long voltage)
338 int shift = 8 - REG_VOLTAGE_LIMIT_MSB_SHIFT[index - 1][nr];
341 voltage = clamp_val(voltage, 0, 0x3ff * nct7802_vmul[nr]);
342 voltage = DIV_ROUND_CLOSEST(voltage, nct7802_vmul[nr]);
344 mutex_lock(&data->access_lock);
345 err = regmap_write(data->regmap,
346 REG_VOLTAGE_LIMIT_LSB[index - 1][nr],
351 err = regmap_update_bits(data->regmap, REG_VOLTAGE_LIMIT_MSB[nr],
352 0x0300 >> shift, (voltage & 0x0300) >> shift);
354 mutex_unlock(&data->access_lock);
358 static ssize_t in_show(struct device *dev, struct device_attribute *attr,
361 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
362 struct nct7802_data *data = dev_get_drvdata(dev);
365 voltage = nct7802_read_voltage(data, sattr->nr, sattr->index);
369 return sprintf(buf, "%d\n", voltage);
372 static ssize_t in_store(struct device *dev, struct device_attribute *attr,
373 const char *buf, size_t count)
375 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
376 struct nct7802_data *data = dev_get_drvdata(dev);
377 int index = sattr->index;
382 err = kstrtoul(buf, 10, &val);
386 err = nct7802_write_voltage(data, nr, index, val);
387 return err ? : count;
390 static ssize_t in_alarm_show(struct device *dev, struct device_attribute *attr,
393 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
394 struct nct7802_data *data = dev_get_drvdata(dev);
395 int volt, min, max, ret;
398 mutex_lock(&data->in_alarm_lock);
401 * The SMI Voltage status register is the only register giving a status
402 * for voltages. A bit is set for each input crossing a threshold, in
403 * both direction, but the "inside" or "outside" limits info is not
404 * available. Also this register is cleared on read.
405 * Note: this is not explicitly spelled out in the datasheet, but
407 * To deal with this we use a status cache with one validity bit and
408 * one status bit for each input. Validity is cleared at startup and
409 * each time the register reports a change, and the status is processed
410 * by software based on current input value and limits.
412 ret = regmap_read(data->regmap, 0x1e, &val); /* SMI Voltage status */
416 /* invalidate cached status for all inputs crossing a threshold */
417 data->in_status &= ~((val & 0x0f) << 4);
419 /* if cached status for requested input is invalid, update it */
420 if (!(data->in_status & (0x10 << sattr->index))) {
421 ret = nct7802_read_voltage(data, sattr->nr, 0);
426 ret = nct7802_read_voltage(data, sattr->nr, 1);
431 ret = nct7802_read_voltage(data, sattr->nr, 2);
436 if (volt < min || volt > max)
437 data->in_status |= (1 << sattr->index);
439 data->in_status &= ~(1 << sattr->index);
441 data->in_status |= 0x10 << sattr->index;
444 ret = sprintf(buf, "%u\n", !!(data->in_status & (1 << sattr->index)));
446 mutex_unlock(&data->in_alarm_lock);
450 static ssize_t temp_show(struct device *dev, struct device_attribute *attr,
453 struct nct7802_data *data = dev_get_drvdata(dev);
454 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
457 err = nct7802_read_temp(data, sattr->nr, sattr->index, &temp);
461 return sprintf(buf, "%d\n", temp);
464 static ssize_t temp_store(struct device *dev, struct device_attribute *attr,
465 const char *buf, size_t count)
467 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
468 struct nct7802_data *data = dev_get_drvdata(dev);
473 err = kstrtol(buf, 10, &val);
477 val = DIV_ROUND_CLOSEST(clamp_val(val, -128000, 127000), 1000);
479 err = regmap_write(data->regmap, nr, val & 0xff);
480 return err ? : count;
483 static ssize_t fan_show(struct device *dev, struct device_attribute *attr,
486 struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr);
487 struct nct7802_data *data = dev_get_drvdata(dev);
490 speed = nct7802_read_fan(data, sattr->index);
494 return sprintf(buf, "%d\n", speed);
497 static ssize_t fan_min_show(struct device *dev, struct device_attribute *attr,
500 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
501 struct nct7802_data *data = dev_get_drvdata(dev);
504 speed = nct7802_read_fan_min(data, sattr->nr, sattr->index);
508 return sprintf(buf, "%d\n", speed);
511 static ssize_t fan_min_store(struct device *dev,
512 struct device_attribute *attr, const char *buf,
515 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
516 struct nct7802_data *data = dev_get_drvdata(dev);
520 err = kstrtoul(buf, 10, &val);
524 err = nct7802_write_fan_min(data, sattr->nr, sattr->index, val);
525 return err ? : count;
528 static ssize_t alarm_show(struct device *dev, struct device_attribute *attr,
531 struct nct7802_data *data = dev_get_drvdata(dev);
532 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
533 int bit = sattr->index;
537 ret = regmap_read(data->regmap, sattr->nr, &val);
541 return sprintf(buf, "%u\n", !!(val & (1 << bit)));
545 beep_show(struct device *dev, struct device_attribute *attr, char *buf)
547 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
548 struct nct7802_data *data = dev_get_drvdata(dev);
552 err = regmap_read(data->regmap, sattr->nr, ®val);
556 return sprintf(buf, "%u\n", !!(regval & (1 << sattr->index)));
560 beep_store(struct device *dev, struct device_attribute *attr, const char *buf,
563 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
564 struct nct7802_data *data = dev_get_drvdata(dev);
568 err = kstrtoul(buf, 10, &val);
574 err = regmap_update_bits(data->regmap, sattr->nr, 1 << sattr->index,
575 val ? 1 << sattr->index : 0);
576 return err ? : count;
579 static SENSOR_DEVICE_ATTR_RW(temp1_type, temp_type, 0);
580 static SENSOR_DEVICE_ATTR_2_RO(temp1_input, temp, 0x01, REG_TEMP_LSB);
581 static SENSOR_DEVICE_ATTR_2_RW(temp1_min, temp, 0x31, 0);
582 static SENSOR_DEVICE_ATTR_2_RW(temp1_max, temp, 0x30, 0);
583 static SENSOR_DEVICE_ATTR_2_RW(temp1_crit, temp, 0x3a, 0);
585 static SENSOR_DEVICE_ATTR_RW(temp2_type, temp_type, 1);
586 static SENSOR_DEVICE_ATTR_2_RO(temp2_input, temp, 0x02, REG_TEMP_LSB);
587 static SENSOR_DEVICE_ATTR_2_RW(temp2_min, temp, 0x33, 0);
588 static SENSOR_DEVICE_ATTR_2_RW(temp2_max, temp, 0x32, 0);
589 static SENSOR_DEVICE_ATTR_2_RW(temp2_crit, temp, 0x3b, 0);
591 static SENSOR_DEVICE_ATTR_RW(temp3_type, temp_type, 2);
592 static SENSOR_DEVICE_ATTR_2_RO(temp3_input, temp, 0x03, REG_TEMP_LSB);
593 static SENSOR_DEVICE_ATTR_2_RW(temp3_min, temp, 0x35, 0);
594 static SENSOR_DEVICE_ATTR_2_RW(temp3_max, temp, 0x34, 0);
595 static SENSOR_DEVICE_ATTR_2_RW(temp3_crit, temp, 0x3c, 0);
597 static SENSOR_DEVICE_ATTR_2_RO(temp4_input, temp, 0x04, 0);
598 static SENSOR_DEVICE_ATTR_2_RW(temp4_min, temp, 0x37, 0);
599 static SENSOR_DEVICE_ATTR_2_RW(temp4_max, temp, 0x36, 0);
600 static SENSOR_DEVICE_ATTR_2_RW(temp4_crit, temp, 0x3d, 0);
602 static SENSOR_DEVICE_ATTR_2_RO(temp5_input, temp, 0x06, REG_TEMP_PECI_LSB);
603 static SENSOR_DEVICE_ATTR_2_RW(temp5_min, temp, 0x39, 0);
604 static SENSOR_DEVICE_ATTR_2_RW(temp5_max, temp, 0x38, 0);
605 static SENSOR_DEVICE_ATTR_2_RW(temp5_crit, temp, 0x3e, 0);
607 static SENSOR_DEVICE_ATTR_2_RO(temp6_input, temp, 0x07, REG_TEMP_PECI_LSB);
609 static SENSOR_DEVICE_ATTR_2_RO(temp1_min_alarm, alarm, 0x18, 0);
610 static SENSOR_DEVICE_ATTR_2_RO(temp2_min_alarm, alarm, 0x18, 1);
611 static SENSOR_DEVICE_ATTR_2_RO(temp3_min_alarm, alarm, 0x18, 2);
612 static SENSOR_DEVICE_ATTR_2_RO(temp4_min_alarm, alarm, 0x18, 3);
613 static SENSOR_DEVICE_ATTR_2_RO(temp5_min_alarm, alarm, 0x18, 4);
615 static SENSOR_DEVICE_ATTR_2_RO(temp1_max_alarm, alarm, 0x19, 0);
616 static SENSOR_DEVICE_ATTR_2_RO(temp2_max_alarm, alarm, 0x19, 1);
617 static SENSOR_DEVICE_ATTR_2_RO(temp3_max_alarm, alarm, 0x19, 2);
618 static SENSOR_DEVICE_ATTR_2_RO(temp4_max_alarm, alarm, 0x19, 3);
619 static SENSOR_DEVICE_ATTR_2_RO(temp5_max_alarm, alarm, 0x19, 4);
621 static SENSOR_DEVICE_ATTR_2_RO(temp1_crit_alarm, alarm, 0x1b, 0);
622 static SENSOR_DEVICE_ATTR_2_RO(temp2_crit_alarm, alarm, 0x1b, 1);
623 static SENSOR_DEVICE_ATTR_2_RO(temp3_crit_alarm, alarm, 0x1b, 2);
624 static SENSOR_DEVICE_ATTR_2_RO(temp4_crit_alarm, alarm, 0x1b, 3);
625 static SENSOR_DEVICE_ATTR_2_RO(temp5_crit_alarm, alarm, 0x1b, 4);
627 static SENSOR_DEVICE_ATTR_2_RO(temp1_fault, alarm, 0x17, 0);
628 static SENSOR_DEVICE_ATTR_2_RO(temp2_fault, alarm, 0x17, 1);
629 static SENSOR_DEVICE_ATTR_2_RO(temp3_fault, alarm, 0x17, 2);
631 static SENSOR_DEVICE_ATTR_2_RW(temp1_beep, beep, 0x5c, 0);
632 static SENSOR_DEVICE_ATTR_2_RW(temp2_beep, beep, 0x5c, 1);
633 static SENSOR_DEVICE_ATTR_2_RW(temp3_beep, beep, 0x5c, 2);
634 static SENSOR_DEVICE_ATTR_2_RW(temp4_beep, beep, 0x5c, 3);
635 static SENSOR_DEVICE_ATTR_2_RW(temp5_beep, beep, 0x5c, 4);
636 static SENSOR_DEVICE_ATTR_2_RW(temp6_beep, beep, 0x5c, 5);
638 static struct attribute *nct7802_temp_attrs[] = {
639 &sensor_dev_attr_temp1_type.dev_attr.attr,
640 &sensor_dev_attr_temp1_input.dev_attr.attr,
641 &sensor_dev_attr_temp1_min.dev_attr.attr,
642 &sensor_dev_attr_temp1_max.dev_attr.attr,
643 &sensor_dev_attr_temp1_crit.dev_attr.attr,
644 &sensor_dev_attr_temp1_min_alarm.dev_attr.attr,
645 &sensor_dev_attr_temp1_max_alarm.dev_attr.attr,
646 &sensor_dev_attr_temp1_crit_alarm.dev_attr.attr,
647 &sensor_dev_attr_temp1_fault.dev_attr.attr,
648 &sensor_dev_attr_temp1_beep.dev_attr.attr,
650 &sensor_dev_attr_temp2_type.dev_attr.attr, /* 10 */
651 &sensor_dev_attr_temp2_input.dev_attr.attr,
652 &sensor_dev_attr_temp2_min.dev_attr.attr,
653 &sensor_dev_attr_temp2_max.dev_attr.attr,
654 &sensor_dev_attr_temp2_crit.dev_attr.attr,
655 &sensor_dev_attr_temp2_min_alarm.dev_attr.attr,
656 &sensor_dev_attr_temp2_max_alarm.dev_attr.attr,
657 &sensor_dev_attr_temp2_crit_alarm.dev_attr.attr,
658 &sensor_dev_attr_temp2_fault.dev_attr.attr,
659 &sensor_dev_attr_temp2_beep.dev_attr.attr,
661 &sensor_dev_attr_temp3_type.dev_attr.attr, /* 20 */
662 &sensor_dev_attr_temp3_input.dev_attr.attr,
663 &sensor_dev_attr_temp3_min.dev_attr.attr,
664 &sensor_dev_attr_temp3_max.dev_attr.attr,
665 &sensor_dev_attr_temp3_crit.dev_attr.attr,
666 &sensor_dev_attr_temp3_min_alarm.dev_attr.attr,
667 &sensor_dev_attr_temp3_max_alarm.dev_attr.attr,
668 &sensor_dev_attr_temp3_crit_alarm.dev_attr.attr,
669 &sensor_dev_attr_temp3_fault.dev_attr.attr,
670 &sensor_dev_attr_temp3_beep.dev_attr.attr,
672 &sensor_dev_attr_temp4_input.dev_attr.attr, /* 30 */
673 &sensor_dev_attr_temp4_min.dev_attr.attr,
674 &sensor_dev_attr_temp4_max.dev_attr.attr,
675 &sensor_dev_attr_temp4_crit.dev_attr.attr,
676 &sensor_dev_attr_temp4_min_alarm.dev_attr.attr,
677 &sensor_dev_attr_temp4_max_alarm.dev_attr.attr,
678 &sensor_dev_attr_temp4_crit_alarm.dev_attr.attr,
679 &sensor_dev_attr_temp4_beep.dev_attr.attr,
681 &sensor_dev_attr_temp5_input.dev_attr.attr, /* 38 */
682 &sensor_dev_attr_temp5_min.dev_attr.attr,
683 &sensor_dev_attr_temp5_max.dev_attr.attr,
684 &sensor_dev_attr_temp5_crit.dev_attr.attr,
685 &sensor_dev_attr_temp5_min_alarm.dev_attr.attr,
686 &sensor_dev_attr_temp5_max_alarm.dev_attr.attr,
687 &sensor_dev_attr_temp5_crit_alarm.dev_attr.attr,
688 &sensor_dev_attr_temp5_beep.dev_attr.attr,
690 &sensor_dev_attr_temp6_input.dev_attr.attr, /* 46 */
691 &sensor_dev_attr_temp6_beep.dev_attr.attr,
696 static umode_t nct7802_temp_is_visible(struct kobject *kobj,
697 struct attribute *attr, int index)
699 struct device *dev = kobj_to_dev(kobj);
700 struct nct7802_data *data = dev_get_drvdata(dev);
704 err = regmap_read(data->regmap, REG_MODE, ®);
709 (reg & 03) != 0x01 && (reg & 0x03) != 0x02) /* RD1 */
712 if (index >= 10 && index < 20 &&
713 (reg & 0x0c) != 0x04 && (reg & 0x0c) != 0x08) /* RD2 */
715 if (index >= 20 && index < 30 && (reg & 0x30) != 0x20) /* RD3 */
718 if (index >= 30 && index < 38) /* local */
721 err = regmap_read(data->regmap, REG_PECI_ENABLE, ®);
725 if (index >= 38 && index < 46 && !(reg & 0x01)) /* PECI 0 */
728 if (index >= 0x46 && (!(reg & 0x02))) /* PECI 1 */
734 static const struct attribute_group nct7802_temp_group = {
735 .attrs = nct7802_temp_attrs,
736 .is_visible = nct7802_temp_is_visible,
739 static SENSOR_DEVICE_ATTR_2_RO(in0_input, in, 0, 0);
740 static SENSOR_DEVICE_ATTR_2_RW(in0_min, in, 0, 1);
741 static SENSOR_DEVICE_ATTR_2_RW(in0_max, in, 0, 2);
742 static SENSOR_DEVICE_ATTR_2_RO(in0_alarm, in_alarm, 0, 3);
743 static SENSOR_DEVICE_ATTR_2_RW(in0_beep, beep, 0x5a, 3);
745 static SENSOR_DEVICE_ATTR_2_RO(in1_input, in, 1, 0);
747 static SENSOR_DEVICE_ATTR_2_RO(in2_input, in, 2, 0);
748 static SENSOR_DEVICE_ATTR_2_RW(in2_min, in, 2, 1);
749 static SENSOR_DEVICE_ATTR_2_RW(in2_max, in, 2, 2);
750 static SENSOR_DEVICE_ATTR_2_RO(in2_alarm, in_alarm, 2, 0);
751 static SENSOR_DEVICE_ATTR_2_RW(in2_beep, beep, 0x5a, 0);
753 static SENSOR_DEVICE_ATTR_2_RO(in3_input, in, 3, 0);
754 static SENSOR_DEVICE_ATTR_2_RW(in3_min, in, 3, 1);
755 static SENSOR_DEVICE_ATTR_2_RW(in3_max, in, 3, 2);
756 static SENSOR_DEVICE_ATTR_2_RO(in3_alarm, in_alarm, 3, 1);
757 static SENSOR_DEVICE_ATTR_2_RW(in3_beep, beep, 0x5a, 1);
759 static SENSOR_DEVICE_ATTR_2_RO(in4_input, in, 4, 0);
760 static SENSOR_DEVICE_ATTR_2_RW(in4_min, in, 4, 1);
761 static SENSOR_DEVICE_ATTR_2_RW(in4_max, in, 4, 2);
762 static SENSOR_DEVICE_ATTR_2_RO(in4_alarm, in_alarm, 4, 2);
763 static SENSOR_DEVICE_ATTR_2_RW(in4_beep, beep, 0x5a, 2);
765 static struct attribute *nct7802_in_attrs[] = {
766 &sensor_dev_attr_in0_input.dev_attr.attr,
767 &sensor_dev_attr_in0_min.dev_attr.attr,
768 &sensor_dev_attr_in0_max.dev_attr.attr,
769 &sensor_dev_attr_in0_alarm.dev_attr.attr,
770 &sensor_dev_attr_in0_beep.dev_attr.attr,
772 &sensor_dev_attr_in1_input.dev_attr.attr, /* 5 */
774 &sensor_dev_attr_in2_input.dev_attr.attr, /* 6 */
775 &sensor_dev_attr_in2_min.dev_attr.attr,
776 &sensor_dev_attr_in2_max.dev_attr.attr,
777 &sensor_dev_attr_in2_alarm.dev_attr.attr,
778 &sensor_dev_attr_in2_beep.dev_attr.attr,
780 &sensor_dev_attr_in3_input.dev_attr.attr, /* 11 */
781 &sensor_dev_attr_in3_min.dev_attr.attr,
782 &sensor_dev_attr_in3_max.dev_attr.attr,
783 &sensor_dev_attr_in3_alarm.dev_attr.attr,
784 &sensor_dev_attr_in3_beep.dev_attr.attr,
786 &sensor_dev_attr_in4_input.dev_attr.attr, /* 16 */
787 &sensor_dev_attr_in4_min.dev_attr.attr,
788 &sensor_dev_attr_in4_max.dev_attr.attr,
789 &sensor_dev_attr_in4_alarm.dev_attr.attr,
790 &sensor_dev_attr_in4_beep.dev_attr.attr,
795 static umode_t nct7802_in_is_visible(struct kobject *kobj,
796 struct attribute *attr, int index)
798 struct device *dev = kobj_to_dev(kobj);
799 struct nct7802_data *data = dev_get_drvdata(dev);
803 if (index < 6) /* VCC, VCORE */
806 err = regmap_read(data->regmap, REG_MODE, ®);
810 if (index >= 6 && index < 11 && (reg & 0x03) != 0x03) /* VSEN1 */
812 if (index >= 11 && index < 16 && (reg & 0x0c) != 0x0c) /* VSEN2 */
814 if (index >= 16 && (reg & 0x30) != 0x30) /* VSEN3 */
820 static const struct attribute_group nct7802_in_group = {
821 .attrs = nct7802_in_attrs,
822 .is_visible = nct7802_in_is_visible,
825 static SENSOR_DEVICE_ATTR_RO(fan1_input, fan, 0x10);
826 static SENSOR_DEVICE_ATTR_2_RW(fan1_min, fan_min, 0x49, 0x4c);
827 static SENSOR_DEVICE_ATTR_2_RO(fan1_alarm, alarm, 0x1a, 0);
828 static SENSOR_DEVICE_ATTR_2_RW(fan1_beep, beep, 0x5b, 0);
829 static SENSOR_DEVICE_ATTR_RO(fan2_input, fan, 0x11);
830 static SENSOR_DEVICE_ATTR_2_RW(fan2_min, fan_min, 0x4a, 0x4d);
831 static SENSOR_DEVICE_ATTR_2_RO(fan2_alarm, alarm, 0x1a, 1);
832 static SENSOR_DEVICE_ATTR_2_RW(fan2_beep, beep, 0x5b, 1);
833 static SENSOR_DEVICE_ATTR_RO(fan3_input, fan, 0x12);
834 static SENSOR_DEVICE_ATTR_2_RW(fan3_min, fan_min, 0x4b, 0x4e);
835 static SENSOR_DEVICE_ATTR_2_RO(fan3_alarm, alarm, 0x1a, 2);
836 static SENSOR_DEVICE_ATTR_2_RW(fan3_beep, beep, 0x5b, 2);
838 /* 7.2.89 Fan Control Output Type */
839 static SENSOR_DEVICE_ATTR_RO(pwm1_mode, pwm_mode, 0);
840 static SENSOR_DEVICE_ATTR_RO(pwm2_mode, pwm_mode, 1);
841 static SENSOR_DEVICE_ATTR_RO(pwm3_mode, pwm_mode, 2);
843 /* 7.2.91... Fan Control Output Value */
844 static SENSOR_DEVICE_ATTR_RW(pwm1, pwm, REG_PWM(0));
845 static SENSOR_DEVICE_ATTR_RW(pwm2, pwm, REG_PWM(1));
846 static SENSOR_DEVICE_ATTR_RW(pwm3, pwm, REG_PWM(2));
848 /* 7.2.95... Temperature to Fan mapping Relationships Register */
849 static SENSOR_DEVICE_ATTR_RW(pwm1_enable, pwm_enable, 0);
850 static SENSOR_DEVICE_ATTR_RW(pwm2_enable, pwm_enable, 1);
851 static SENSOR_DEVICE_ATTR_RW(pwm3_enable, pwm_enable, 2);
853 static struct attribute *nct7802_fan_attrs[] = {
854 &sensor_dev_attr_fan1_input.dev_attr.attr,
855 &sensor_dev_attr_fan1_min.dev_attr.attr,
856 &sensor_dev_attr_fan1_alarm.dev_attr.attr,
857 &sensor_dev_attr_fan1_beep.dev_attr.attr,
858 &sensor_dev_attr_fan2_input.dev_attr.attr,
859 &sensor_dev_attr_fan2_min.dev_attr.attr,
860 &sensor_dev_attr_fan2_alarm.dev_attr.attr,
861 &sensor_dev_attr_fan2_beep.dev_attr.attr,
862 &sensor_dev_attr_fan3_input.dev_attr.attr,
863 &sensor_dev_attr_fan3_min.dev_attr.attr,
864 &sensor_dev_attr_fan3_alarm.dev_attr.attr,
865 &sensor_dev_attr_fan3_beep.dev_attr.attr,
870 static umode_t nct7802_fan_is_visible(struct kobject *kobj,
871 struct attribute *attr, int index)
873 struct device *dev = kobj_to_dev(kobj);
874 struct nct7802_data *data = dev_get_drvdata(dev);
875 int fan = index / 4; /* 4 attributes per fan */
879 err = regmap_read(data->regmap, REG_FAN_ENABLE, ®);
880 if (err < 0 || !(reg & (1 << fan)))
886 static const struct attribute_group nct7802_fan_group = {
887 .attrs = nct7802_fan_attrs,
888 .is_visible = nct7802_fan_is_visible,
891 static struct attribute *nct7802_pwm_attrs[] = {
892 &sensor_dev_attr_pwm1_enable.dev_attr.attr,
893 &sensor_dev_attr_pwm1_mode.dev_attr.attr,
894 &sensor_dev_attr_pwm1.dev_attr.attr,
895 &sensor_dev_attr_pwm2_enable.dev_attr.attr,
896 &sensor_dev_attr_pwm2_mode.dev_attr.attr,
897 &sensor_dev_attr_pwm2.dev_attr.attr,
898 &sensor_dev_attr_pwm3_enable.dev_attr.attr,
899 &sensor_dev_attr_pwm3_mode.dev_attr.attr,
900 &sensor_dev_attr_pwm3.dev_attr.attr,
904 static const struct attribute_group nct7802_pwm_group = {
905 .attrs = nct7802_pwm_attrs,
908 /* 7.2.115... 0x80-0x83, 0x84 Temperature (X-axis) transition */
909 static SENSOR_DEVICE_ATTR_2_RW(pwm1_auto_point1_temp, temp, 0x80, 0);
910 static SENSOR_DEVICE_ATTR_2_RW(pwm1_auto_point2_temp, temp, 0x81, 0);
911 static SENSOR_DEVICE_ATTR_2_RW(pwm1_auto_point3_temp, temp, 0x82, 0);
912 static SENSOR_DEVICE_ATTR_2_RW(pwm1_auto_point4_temp, temp, 0x83, 0);
913 static SENSOR_DEVICE_ATTR_2_RW(pwm1_auto_point5_temp, temp, 0x84, 0);
915 /* 7.2.120... 0x85-0x88 PWM (Y-axis) transition */
916 static SENSOR_DEVICE_ATTR_RW(pwm1_auto_point1_pwm, pwm, 0x85);
917 static SENSOR_DEVICE_ATTR_RW(pwm1_auto_point2_pwm, pwm, 0x86);
918 static SENSOR_DEVICE_ATTR_RW(pwm1_auto_point3_pwm, pwm, 0x87);
919 static SENSOR_DEVICE_ATTR_RW(pwm1_auto_point4_pwm, pwm, 0x88);
920 static SENSOR_DEVICE_ATTR_RO(pwm1_auto_point5_pwm, pwm, 0);
922 /* 7.2.124 Table 2 X-axis Transition Point 1 Register */
923 static SENSOR_DEVICE_ATTR_2_RW(pwm2_auto_point1_temp, temp, 0x90, 0);
924 static SENSOR_DEVICE_ATTR_2_RW(pwm2_auto_point2_temp, temp, 0x91, 0);
925 static SENSOR_DEVICE_ATTR_2_RW(pwm2_auto_point3_temp, temp, 0x92, 0);
926 static SENSOR_DEVICE_ATTR_2_RW(pwm2_auto_point4_temp, temp, 0x93, 0);
927 static SENSOR_DEVICE_ATTR_2_RW(pwm2_auto_point5_temp, temp, 0x94, 0);
929 /* 7.2.129 Table 2 Y-axis Transition Point 1 Register */
930 static SENSOR_DEVICE_ATTR_RW(pwm2_auto_point1_pwm, pwm, 0x95);
931 static SENSOR_DEVICE_ATTR_RW(pwm2_auto_point2_pwm, pwm, 0x96);
932 static SENSOR_DEVICE_ATTR_RW(pwm2_auto_point3_pwm, pwm, 0x97);
933 static SENSOR_DEVICE_ATTR_RW(pwm2_auto_point4_pwm, pwm, 0x98);
934 static SENSOR_DEVICE_ATTR_RO(pwm2_auto_point5_pwm, pwm, 0);
936 /* 7.2.133 Table 3 X-axis Transition Point 1 Register */
937 static SENSOR_DEVICE_ATTR_2_RW(pwm3_auto_point1_temp, temp, 0xA0, 0);
938 static SENSOR_DEVICE_ATTR_2_RW(pwm3_auto_point2_temp, temp, 0xA1, 0);
939 static SENSOR_DEVICE_ATTR_2_RW(pwm3_auto_point3_temp, temp, 0xA2, 0);
940 static SENSOR_DEVICE_ATTR_2_RW(pwm3_auto_point4_temp, temp, 0xA3, 0);
941 static SENSOR_DEVICE_ATTR_2_RW(pwm3_auto_point5_temp, temp, 0xA4, 0);
943 /* 7.2.138 Table 3 Y-axis Transition Point 1 Register */
944 static SENSOR_DEVICE_ATTR_RW(pwm3_auto_point1_pwm, pwm, 0xA5);
945 static SENSOR_DEVICE_ATTR_RW(pwm3_auto_point2_pwm, pwm, 0xA6);
946 static SENSOR_DEVICE_ATTR_RW(pwm3_auto_point3_pwm, pwm, 0xA7);
947 static SENSOR_DEVICE_ATTR_RW(pwm3_auto_point4_pwm, pwm, 0xA8);
948 static SENSOR_DEVICE_ATTR_RO(pwm3_auto_point5_pwm, pwm, 0);
950 static struct attribute *nct7802_auto_point_attrs[] = {
951 &sensor_dev_attr_pwm1_auto_point1_temp.dev_attr.attr,
952 &sensor_dev_attr_pwm1_auto_point2_temp.dev_attr.attr,
953 &sensor_dev_attr_pwm1_auto_point3_temp.dev_attr.attr,
954 &sensor_dev_attr_pwm1_auto_point4_temp.dev_attr.attr,
955 &sensor_dev_attr_pwm1_auto_point5_temp.dev_attr.attr,
957 &sensor_dev_attr_pwm1_auto_point1_pwm.dev_attr.attr,
958 &sensor_dev_attr_pwm1_auto_point2_pwm.dev_attr.attr,
959 &sensor_dev_attr_pwm1_auto_point3_pwm.dev_attr.attr,
960 &sensor_dev_attr_pwm1_auto_point4_pwm.dev_attr.attr,
961 &sensor_dev_attr_pwm1_auto_point5_pwm.dev_attr.attr,
963 &sensor_dev_attr_pwm2_auto_point1_temp.dev_attr.attr,
964 &sensor_dev_attr_pwm2_auto_point2_temp.dev_attr.attr,
965 &sensor_dev_attr_pwm2_auto_point3_temp.dev_attr.attr,
966 &sensor_dev_attr_pwm2_auto_point4_temp.dev_attr.attr,
967 &sensor_dev_attr_pwm2_auto_point5_temp.dev_attr.attr,
969 &sensor_dev_attr_pwm2_auto_point1_pwm.dev_attr.attr,
970 &sensor_dev_attr_pwm2_auto_point2_pwm.dev_attr.attr,
971 &sensor_dev_attr_pwm2_auto_point3_pwm.dev_attr.attr,
972 &sensor_dev_attr_pwm2_auto_point4_pwm.dev_attr.attr,
973 &sensor_dev_attr_pwm2_auto_point5_pwm.dev_attr.attr,
975 &sensor_dev_attr_pwm3_auto_point1_temp.dev_attr.attr,
976 &sensor_dev_attr_pwm3_auto_point2_temp.dev_attr.attr,
977 &sensor_dev_attr_pwm3_auto_point3_temp.dev_attr.attr,
978 &sensor_dev_attr_pwm3_auto_point4_temp.dev_attr.attr,
979 &sensor_dev_attr_pwm3_auto_point5_temp.dev_attr.attr,
981 &sensor_dev_attr_pwm3_auto_point1_pwm.dev_attr.attr,
982 &sensor_dev_attr_pwm3_auto_point2_pwm.dev_attr.attr,
983 &sensor_dev_attr_pwm3_auto_point3_pwm.dev_attr.attr,
984 &sensor_dev_attr_pwm3_auto_point4_pwm.dev_attr.attr,
985 &sensor_dev_attr_pwm3_auto_point5_pwm.dev_attr.attr,
990 static const struct attribute_group nct7802_auto_point_group = {
991 .attrs = nct7802_auto_point_attrs,
994 static const struct attribute_group *nct7802_groups[] = {
999 &nct7802_auto_point_group,
1003 static int nct7802_detect(struct i2c_client *client,
1004 struct i2c_board_info *info)
1009 * Chip identification registers are only available in bank 0,
1010 * so only attempt chip detection if bank 0 is selected
1012 reg = i2c_smbus_read_byte_data(client, REG_BANK);
1016 reg = i2c_smbus_read_byte_data(client, REG_VENDOR_ID);
1020 reg = i2c_smbus_read_byte_data(client, REG_CHIP_ID);
1024 reg = i2c_smbus_read_byte_data(client, REG_VERSION_ID);
1025 if (reg < 0 || (reg & 0xf0) != 0x20)
1028 /* Also validate lower bits of voltage and temperature registers */
1029 reg = i2c_smbus_read_byte_data(client, REG_TEMP_LSB);
1030 if (reg < 0 || (reg & 0x1f))
1033 reg = i2c_smbus_read_byte_data(client, REG_TEMP_PECI_LSB);
1034 if (reg < 0 || (reg & 0x3f))
1037 reg = i2c_smbus_read_byte_data(client, REG_VOLTAGE_LOW);
1038 if (reg < 0 || (reg & 0x3f))
1041 strscpy(info->type, "nct7802", I2C_NAME_SIZE);
1045 static bool nct7802_regmap_is_volatile(struct device *dev, unsigned int reg)
1047 return (reg != REG_BANK && reg <= 0x20) ||
1048 (reg >= REG_PWM(0) && reg <= REG_PWM(2));
1051 static const struct regmap_config nct7802_regmap_config = {
1054 .cache_type = REGCACHE_RBTREE,
1055 .volatile_reg = nct7802_regmap_is_volatile,
1058 static int nct7802_get_channel_config(struct device *dev,
1059 struct device_node *node, u8 *mode_mask,
1063 const char *type_str, *md_str;
1066 if (!node->name || of_node_cmp(node->name, "channel"))
1069 if (of_property_read_u32(node, "reg", ®)) {
1070 dev_err(dev, "Could not read reg value for '%s'\n",
1076 dev_err(dev, "Invalid reg (%u) in '%s'\n", reg,
1082 if (!of_device_is_available(node))
1083 *mode_val &= ~MODE_LTD_EN;
1085 *mode_val |= MODE_LTD_EN;
1086 *mode_mask |= MODE_LTD_EN;
1090 /* At this point we have reg >= 1 && reg <= 3 */
1092 if (!of_device_is_available(node)) {
1093 *mode_val &= ~(MODE_RTD_MASK << MODE_BIT_OFFSET_RTD(reg - 1));
1094 *mode_mask |= MODE_RTD_MASK << MODE_BIT_OFFSET_RTD(reg - 1);
1098 if (of_property_read_string(node, "sensor-type", &type_str)) {
1099 dev_err(dev, "No type for '%s'\n", node->full_name);
1103 if (!strcmp(type_str, "voltage")) {
1104 *mode_val |= (RTD_MODE_VOLTAGE & MODE_RTD_MASK)
1105 << MODE_BIT_OFFSET_RTD(reg - 1);
1106 *mode_mask |= MODE_RTD_MASK << MODE_BIT_OFFSET_RTD(reg - 1);
1110 if (strcmp(type_str, "temperature")) {
1111 dev_err(dev, "Invalid type '%s' for '%s'\n", type_str,
1117 /* RTD3 only supports thermistor mode */
1118 md = RTD_MODE_THERMISTOR;
1120 if (of_property_read_string(node, "temperature-mode",
1122 dev_err(dev, "No mode for '%s'\n", node->full_name);
1126 if (!strcmp(md_str, "thermal-diode"))
1127 md = RTD_MODE_CURRENT;
1128 else if (!strcmp(md_str, "thermistor"))
1129 md = RTD_MODE_THERMISTOR;
1131 dev_err(dev, "Invalid mode '%s' for '%s'\n", md_str,
1137 *mode_val |= (md & MODE_RTD_MASK) << MODE_BIT_OFFSET_RTD(reg - 1);
1138 *mode_mask |= MODE_RTD_MASK << MODE_BIT_OFFSET_RTD(reg - 1);
1143 static int nct7802_configure_channels(struct device *dev,
1144 struct nct7802_data *data)
1146 /* Enable local temperature sensor by default */
1147 u8 mode_mask = MODE_LTD_EN, mode_val = MODE_LTD_EN;
1148 struct device_node *node;
1152 for_each_child_of_node(dev->of_node, node) {
1153 err = nct7802_get_channel_config(dev, node, &mode_mask,
1162 return regmap_update_bits(data->regmap, REG_MODE, mode_mask, mode_val);
1165 static int nct7802_init_chip(struct device *dev, struct nct7802_data *data)
1170 err = regmap_update_bits(data->regmap, REG_START, 0x01, 0x01);
1174 err = nct7802_configure_channels(dev, data);
1178 /* Enable Vcore and VCC voltage monitoring */
1179 return regmap_update_bits(data->regmap, REG_VMON_ENABLE, 0x03, 0x03);
1182 static int nct7802_probe(struct i2c_client *client)
1184 struct device *dev = &client->dev;
1185 struct nct7802_data *data;
1186 struct device *hwmon_dev;
1189 data = devm_kzalloc(dev, sizeof(*data), GFP_KERNEL);
1193 data->regmap = devm_regmap_init_i2c(client, &nct7802_regmap_config);
1194 if (IS_ERR(data->regmap))
1195 return PTR_ERR(data->regmap);
1197 mutex_init(&data->access_lock);
1198 mutex_init(&data->in_alarm_lock);
1200 ret = nct7802_init_chip(dev, data);
1204 hwmon_dev = devm_hwmon_device_register_with_groups(dev, client->name,
1207 return PTR_ERR_OR_ZERO(hwmon_dev);
1210 static const unsigned short nct7802_address_list[] = {
1211 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f, I2C_CLIENT_END
1214 static const struct i2c_device_id nct7802_idtable[] = {
1218 MODULE_DEVICE_TABLE(i2c, nct7802_idtable);
1220 static struct i2c_driver nct7802_driver = {
1221 .class = I2C_CLASS_HWMON,
1225 .detect = nct7802_detect,
1226 .probe_new = nct7802_probe,
1227 .id_table = nct7802_idtable,
1228 .address_list = nct7802_address_list,
1231 module_i2c_driver(nct7802_driver);
1233 MODULE_AUTHOR("Guenter Roeck <linux@roeck-us.net>");
1234 MODULE_DESCRIPTION("NCT7802Y Hardware Monitoring Driver");
1235 MODULE_LICENSE("GPL v2");