2 * nct7802 - Driver for Nuvoton NCT7802Y
4 * Copyright (C) 2014 Guenter Roeck <linux@roeck-us.net>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
17 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
19 #include <linux/err.h>
20 #include <linux/i2c.h>
21 #include <linux/init.h>
22 #include <linux/hwmon.h>
23 #include <linux/hwmon-sysfs.h>
24 #include <linux/jiffies.h>
25 #include <linux/module.h>
26 #include <linux/mutex.h>
27 #include <linux/regmap.h>
28 #include <linux/slab.h>
30 #define DRVNAME "nct7802"
32 static const u8 REG_VOLTAGE[5] = { 0x09, 0x0a, 0x0c, 0x0d, 0x0e };
34 static const u8 REG_VOLTAGE_LIMIT_LSB[2][5] = {
35 { 0x40, 0x00, 0x42, 0x44, 0x46 },
36 { 0x3f, 0x00, 0x41, 0x43, 0x45 },
39 static const u8 REG_VOLTAGE_LIMIT_MSB[5] = { 0x48, 0x00, 0x47, 0x47, 0x48 };
41 static const u8 REG_VOLTAGE_LIMIT_MSB_SHIFT[2][5] = {
47 #define REG_TEMP_LSB 0x05
48 #define REG_TEMP_PECI_LSB 0x08
49 #define REG_VOLTAGE_LOW 0x0f
50 #define REG_FANCOUNT_LOW 0x13
51 #define REG_START 0x21
52 #define REG_MODE 0x22 /* 7.2.32 Mode Selection Register */
53 #define REG_PECI_ENABLE 0x23
54 #define REG_FAN_ENABLE 0x24
55 #define REG_VMON_ENABLE 0x25
56 #define REG_PWM(x) (0x60 + (x))
57 #define REG_SMARTFAN_EN(x) (0x64 + (x) / 2)
58 #define SMARTFAN_EN_SHIFT(x) ((x) % 2 * 4)
59 #define REG_VENDOR_ID 0xfd
60 #define REG_CHIP_ID 0xfe
61 #define REG_VERSION_ID 0xff
64 * Data structures and manipulation thereof
68 struct regmap *regmap;
69 struct mutex access_lock; /* for multi-byte read and write operations */
72 static ssize_t show_temp_type(struct device *dev, struct device_attribute *attr,
75 struct nct7802_data *data = dev_get_drvdata(dev);
76 struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr);
80 ret = regmap_read(data->regmap, REG_MODE, &mode);
84 return sprintf(buf, "%u\n", (mode >> (2 * sattr->index) & 3) + 2);
87 static ssize_t store_temp_type(struct device *dev,
88 struct device_attribute *attr,
89 const char *buf, size_t count)
91 struct nct7802_data *data = dev_get_drvdata(dev);
92 struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr);
96 err = kstrtouint(buf, 0, &type);
99 if (sattr->index == 2 && type != 4) /* RD3 */
101 if (type < 3 || type > 4)
103 err = regmap_update_bits(data->regmap, REG_MODE,
104 3 << 2 * sattr->index, (type - 2) << 2 * sattr->index);
105 return err ? : count;
108 static ssize_t show_pwm_mode(struct device *dev, struct device_attribute *attr,
111 struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr);
112 struct nct7802_data *data = dev_get_drvdata(dev);
116 if (sattr->index > 1)
117 return sprintf(buf, "1\n");
119 ret = regmap_read(data->regmap, 0x5E, ®val);
123 return sprintf(buf, "%u\n", !(regval & (1 << sattr->index)));
126 static ssize_t show_pwm(struct device *dev, struct device_attribute *devattr,
129 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
130 struct nct7802_data *data = dev_get_drvdata(dev);
135 return sprintf(buf, "255\n");
137 ret = regmap_read(data->regmap, attr->index, &val);
141 return sprintf(buf, "%d\n", val);
144 static ssize_t store_pwm(struct device *dev, struct device_attribute *devattr,
145 const char *buf, size_t count)
147 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
148 struct nct7802_data *data = dev_get_drvdata(dev);
152 err = kstrtou8(buf, 0, &val);
156 err = regmap_write(data->regmap, attr->index, val);
157 return err ? : count;
160 static ssize_t show_pwm_enable(struct device *dev,
161 struct device_attribute *attr, char *buf)
163 struct nct7802_data *data = dev_get_drvdata(dev);
164 struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr);
165 unsigned int reg, enabled;
168 ret = regmap_read(data->regmap, REG_SMARTFAN_EN(sattr->index), ®);
171 enabled = reg >> SMARTFAN_EN_SHIFT(sattr->index) & 1;
172 return sprintf(buf, "%u\n", enabled + 1);
175 static ssize_t store_pwm_enable(struct device *dev,
176 struct device_attribute *attr,
177 const char *buf, size_t count)
179 struct nct7802_data *data = dev_get_drvdata(dev);
180 struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr);
184 ret = kstrtou8(buf, 0, &val);
187 if (val < 1 || val > 2)
189 ret = regmap_update_bits(data->regmap, REG_SMARTFAN_EN(sattr->index),
190 1 << SMARTFAN_EN_SHIFT(sattr->index),
191 (val - 1) << SMARTFAN_EN_SHIFT(sattr->index));
192 return ret ? : count;
195 static int nct7802_read_temp(struct nct7802_data *data,
196 u8 reg_temp, u8 reg_temp_low, int *temp)
198 unsigned int t1, t2 = 0;
203 mutex_lock(&data->access_lock);
204 err = regmap_read(data->regmap, reg_temp, &t1);
208 if (reg_temp_low) { /* 11 bit data */
209 err = regmap_read(data->regmap, reg_temp_low, &t2);
214 *temp = (s16)t1 / 32 * 125;
216 mutex_unlock(&data->access_lock);
220 static int nct7802_read_fan(struct nct7802_data *data, u8 reg_fan)
225 mutex_lock(&data->access_lock);
226 ret = regmap_read(data->regmap, reg_fan, &f1);
229 ret = regmap_read(data->regmap, REG_FANCOUNT_LOW, &f2);
232 ret = (f1 << 5) | (f2 >> 3);
233 /* convert fan count to rpm */
234 if (ret == 0x1fff) /* maximum value, assume fan is stopped */
237 ret = DIV_ROUND_CLOSEST(1350000U, ret);
239 mutex_unlock(&data->access_lock);
243 static int nct7802_read_fan_min(struct nct7802_data *data, u8 reg_fan_low,
249 mutex_lock(&data->access_lock);
250 ret = regmap_read(data->regmap, reg_fan_low, &f1);
253 ret = regmap_read(data->regmap, reg_fan_high, &f2);
256 ret = f1 | ((f2 & 0xf8) << 5);
257 /* convert fan count to rpm */
258 if (ret == 0x1fff) /* maximum value, assume no limit */
261 ret = DIV_ROUND_CLOSEST(1350000U, ret);
265 mutex_unlock(&data->access_lock);
269 static int nct7802_write_fan_min(struct nct7802_data *data, u8 reg_fan_low,
270 u8 reg_fan_high, unsigned long limit)
275 limit = DIV_ROUND_CLOSEST(1350000U, limit);
278 limit = clamp_val(limit, 0, 0x1fff);
280 mutex_lock(&data->access_lock);
281 err = regmap_write(data->regmap, reg_fan_low, limit & 0xff);
285 err = regmap_write(data->regmap, reg_fan_high, (limit & 0x1f00) >> 5);
287 mutex_unlock(&data->access_lock);
291 static u8 nct7802_vmul[] = { 4, 2, 2, 2, 2 };
293 static int nct7802_read_voltage(struct nct7802_data *data, int nr, int index)
298 mutex_lock(&data->access_lock);
299 if (index == 0) { /* voltage */
300 ret = regmap_read(data->regmap, REG_VOLTAGE[nr], &v1);
303 ret = regmap_read(data->regmap, REG_VOLTAGE_LOW, &v2);
306 ret = ((v1 << 2) | (v2 >> 6)) * nct7802_vmul[nr];
308 int shift = 8 - REG_VOLTAGE_LIMIT_MSB_SHIFT[index - 1][nr];
310 ret = regmap_read(data->regmap,
311 REG_VOLTAGE_LIMIT_LSB[index - 1][nr], &v1);
314 ret = regmap_read(data->regmap, REG_VOLTAGE_LIMIT_MSB[nr],
318 ret = (v1 | ((v2 << shift) & 0x300)) * nct7802_vmul[nr];
321 mutex_unlock(&data->access_lock);
325 static int nct7802_write_voltage(struct nct7802_data *data, int nr, int index,
326 unsigned long voltage)
328 int shift = 8 - REG_VOLTAGE_LIMIT_MSB_SHIFT[index - 1][nr];
331 voltage = clamp_val(voltage, 0, 0x3ff * nct7802_vmul[nr]);
332 voltage = DIV_ROUND_CLOSEST(voltage, nct7802_vmul[nr]);
334 mutex_lock(&data->access_lock);
335 err = regmap_write(data->regmap,
336 REG_VOLTAGE_LIMIT_LSB[index - 1][nr],
341 err = regmap_update_bits(data->regmap, REG_VOLTAGE_LIMIT_MSB[nr],
342 0x0300 >> shift, (voltage & 0x0300) >> shift);
344 mutex_unlock(&data->access_lock);
348 static ssize_t show_in(struct device *dev, struct device_attribute *attr,
351 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
352 struct nct7802_data *data = dev_get_drvdata(dev);
355 voltage = nct7802_read_voltage(data, sattr->nr, sattr->index);
359 return sprintf(buf, "%d\n", voltage);
362 static ssize_t store_in(struct device *dev, struct device_attribute *attr,
363 const char *buf, size_t count)
365 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
366 struct nct7802_data *data = dev_get_drvdata(dev);
367 int index = sattr->index;
372 err = kstrtoul(buf, 10, &val);
376 err = nct7802_write_voltage(data, nr, index, val);
377 return err ? : count;
380 static ssize_t show_temp(struct device *dev, struct device_attribute *attr,
383 struct nct7802_data *data = dev_get_drvdata(dev);
384 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
387 err = nct7802_read_temp(data, sattr->nr, sattr->index, &temp);
391 return sprintf(buf, "%d\n", temp);
394 static ssize_t store_temp(struct device *dev, struct device_attribute *attr,
395 const char *buf, size_t count)
397 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
398 struct nct7802_data *data = dev_get_drvdata(dev);
403 err = kstrtol(buf, 10, &val);
407 val = DIV_ROUND_CLOSEST(clamp_val(val, -128000, 127000), 1000);
409 err = regmap_write(data->regmap, nr, val & 0xff);
410 return err ? : count;
413 static ssize_t show_fan(struct device *dev, struct device_attribute *attr,
416 struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr);
417 struct nct7802_data *data = dev_get_drvdata(dev);
420 speed = nct7802_read_fan(data, sattr->index);
424 return sprintf(buf, "%d\n", speed);
427 static ssize_t show_fan_min(struct device *dev, struct device_attribute *attr,
430 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
431 struct nct7802_data *data = dev_get_drvdata(dev);
434 speed = nct7802_read_fan_min(data, sattr->nr, sattr->index);
438 return sprintf(buf, "%d\n", speed);
441 static ssize_t store_fan_min(struct device *dev, struct device_attribute *attr,
442 const char *buf, size_t count)
444 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
445 struct nct7802_data *data = dev_get_drvdata(dev);
449 err = kstrtoul(buf, 10, &val);
453 err = nct7802_write_fan_min(data, sattr->nr, sattr->index, val);
454 return err ? : count;
457 static ssize_t show_alarm(struct device *dev, struct device_attribute *attr,
460 struct nct7802_data *data = dev_get_drvdata(dev);
461 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
462 int bit = sattr->index;
466 ret = regmap_read(data->regmap, sattr->nr, &val);
470 return sprintf(buf, "%u\n", !!(val & (1 << bit)));
474 show_beep(struct device *dev, struct device_attribute *attr, char *buf)
476 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
477 struct nct7802_data *data = dev_get_drvdata(dev);
481 err = regmap_read(data->regmap, sattr->nr, ®val);
485 return sprintf(buf, "%u\n", !!(regval & (1 << sattr->index)));
489 store_beep(struct device *dev, struct device_attribute *attr, const char *buf,
492 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
493 struct nct7802_data *data = dev_get_drvdata(dev);
497 err = kstrtoul(buf, 10, &val);
503 err = regmap_update_bits(data->regmap, sattr->nr, 1 << sattr->index,
504 val ? 1 << sattr->index : 0);
505 return err ? : count;
508 static SENSOR_DEVICE_ATTR(temp1_type, S_IRUGO | S_IWUSR,
509 show_temp_type, store_temp_type, 0);
510 static SENSOR_DEVICE_ATTR_2(temp1_input, S_IRUGO, show_temp, NULL, 0x01,
512 static SENSOR_DEVICE_ATTR_2(temp1_min, S_IRUGO | S_IWUSR, show_temp,
513 store_temp, 0x31, 0);
514 static SENSOR_DEVICE_ATTR_2(temp1_max, S_IRUGO | S_IWUSR, show_temp,
515 store_temp, 0x30, 0);
516 static SENSOR_DEVICE_ATTR_2(temp1_crit, S_IRUGO | S_IWUSR, show_temp,
517 store_temp, 0x3a, 0);
519 static SENSOR_DEVICE_ATTR(temp2_type, S_IRUGO | S_IWUSR,
520 show_temp_type, store_temp_type, 1);
521 static SENSOR_DEVICE_ATTR_2(temp2_input, S_IRUGO, show_temp, NULL, 0x02,
523 static SENSOR_DEVICE_ATTR_2(temp2_min, S_IRUGO | S_IWUSR, show_temp,
524 store_temp, 0x33, 0);
525 static SENSOR_DEVICE_ATTR_2(temp2_max, S_IRUGO | S_IWUSR, show_temp,
526 store_temp, 0x32, 0);
527 static SENSOR_DEVICE_ATTR_2(temp2_crit, S_IRUGO | S_IWUSR, show_temp,
528 store_temp, 0x3b, 0);
530 static SENSOR_DEVICE_ATTR(temp3_type, S_IRUGO | S_IWUSR,
531 show_temp_type, store_temp_type, 2);
532 static SENSOR_DEVICE_ATTR_2(temp3_input, S_IRUGO, show_temp, NULL, 0x03,
534 static SENSOR_DEVICE_ATTR_2(temp3_min, S_IRUGO | S_IWUSR, show_temp,
535 store_temp, 0x35, 0);
536 static SENSOR_DEVICE_ATTR_2(temp3_max, S_IRUGO | S_IWUSR, show_temp,
537 store_temp, 0x34, 0);
538 static SENSOR_DEVICE_ATTR_2(temp3_crit, S_IRUGO | S_IWUSR, show_temp,
539 store_temp, 0x3c, 0);
541 static SENSOR_DEVICE_ATTR_2(temp4_input, S_IRUGO, show_temp, NULL, 0x04, 0);
542 static SENSOR_DEVICE_ATTR_2(temp4_min, S_IRUGO | S_IWUSR, show_temp,
543 store_temp, 0x37, 0);
544 static SENSOR_DEVICE_ATTR_2(temp4_max, S_IRUGO | S_IWUSR, show_temp,
545 store_temp, 0x36, 0);
546 static SENSOR_DEVICE_ATTR_2(temp4_crit, S_IRUGO | S_IWUSR, show_temp,
547 store_temp, 0x3d, 0);
549 static SENSOR_DEVICE_ATTR_2(temp5_input, S_IRUGO, show_temp, NULL, 0x06,
551 static SENSOR_DEVICE_ATTR_2(temp5_min, S_IRUGO | S_IWUSR, show_temp,
552 store_temp, 0x39, 0);
553 static SENSOR_DEVICE_ATTR_2(temp5_max, S_IRUGO | S_IWUSR, show_temp,
554 store_temp, 0x38, 0);
555 static SENSOR_DEVICE_ATTR_2(temp5_crit, S_IRUGO | S_IWUSR, show_temp,
556 store_temp, 0x3e, 0);
558 static SENSOR_DEVICE_ATTR_2(temp6_input, S_IRUGO, show_temp, NULL, 0x07,
561 static SENSOR_DEVICE_ATTR_2(temp1_min_alarm, S_IRUGO, show_alarm, NULL,
563 static SENSOR_DEVICE_ATTR_2(temp2_min_alarm, S_IRUGO, show_alarm, NULL,
565 static SENSOR_DEVICE_ATTR_2(temp3_min_alarm, S_IRUGO, show_alarm, NULL,
567 static SENSOR_DEVICE_ATTR_2(temp4_min_alarm, S_IRUGO, show_alarm, NULL,
569 static SENSOR_DEVICE_ATTR_2(temp5_min_alarm, S_IRUGO, show_alarm, NULL,
572 static SENSOR_DEVICE_ATTR_2(temp1_max_alarm, S_IRUGO, show_alarm, NULL,
574 static SENSOR_DEVICE_ATTR_2(temp2_max_alarm, S_IRUGO, show_alarm, NULL,
576 static SENSOR_DEVICE_ATTR_2(temp3_max_alarm, S_IRUGO, show_alarm, NULL,
578 static SENSOR_DEVICE_ATTR_2(temp4_max_alarm, S_IRUGO, show_alarm, NULL,
580 static SENSOR_DEVICE_ATTR_2(temp5_max_alarm, S_IRUGO, show_alarm, NULL,
583 static SENSOR_DEVICE_ATTR_2(temp1_crit_alarm, S_IRUGO, show_alarm, NULL,
585 static SENSOR_DEVICE_ATTR_2(temp2_crit_alarm, S_IRUGO, show_alarm, NULL,
587 static SENSOR_DEVICE_ATTR_2(temp3_crit_alarm, S_IRUGO, show_alarm, NULL,
589 static SENSOR_DEVICE_ATTR_2(temp4_crit_alarm, S_IRUGO, show_alarm, NULL,
591 static SENSOR_DEVICE_ATTR_2(temp5_crit_alarm, S_IRUGO, show_alarm, NULL,
594 static SENSOR_DEVICE_ATTR_2(temp1_fault, S_IRUGO, show_alarm, NULL, 0x17, 0);
595 static SENSOR_DEVICE_ATTR_2(temp2_fault, S_IRUGO, show_alarm, NULL, 0x17, 1);
596 static SENSOR_DEVICE_ATTR_2(temp3_fault, S_IRUGO, show_alarm, NULL, 0x17, 2);
598 static SENSOR_DEVICE_ATTR_2(temp1_beep, S_IRUGO | S_IWUSR, show_beep,
599 store_beep, 0x5c, 0);
600 static SENSOR_DEVICE_ATTR_2(temp2_beep, S_IRUGO | S_IWUSR, show_beep,
601 store_beep, 0x5c, 1);
602 static SENSOR_DEVICE_ATTR_2(temp3_beep, S_IRUGO | S_IWUSR, show_beep,
603 store_beep, 0x5c, 2);
604 static SENSOR_DEVICE_ATTR_2(temp4_beep, S_IRUGO | S_IWUSR, show_beep,
605 store_beep, 0x5c, 3);
606 static SENSOR_DEVICE_ATTR_2(temp5_beep, S_IRUGO | S_IWUSR, show_beep,
607 store_beep, 0x5c, 4);
608 static SENSOR_DEVICE_ATTR_2(temp6_beep, S_IRUGO | S_IWUSR, show_beep,
609 store_beep, 0x5c, 5);
611 static struct attribute *nct7802_temp_attrs[] = {
612 &sensor_dev_attr_temp1_type.dev_attr.attr,
613 &sensor_dev_attr_temp1_input.dev_attr.attr,
614 &sensor_dev_attr_temp1_min.dev_attr.attr,
615 &sensor_dev_attr_temp1_max.dev_attr.attr,
616 &sensor_dev_attr_temp1_crit.dev_attr.attr,
617 &sensor_dev_attr_temp1_min_alarm.dev_attr.attr,
618 &sensor_dev_attr_temp1_max_alarm.dev_attr.attr,
619 &sensor_dev_attr_temp1_crit_alarm.dev_attr.attr,
620 &sensor_dev_attr_temp1_fault.dev_attr.attr,
621 &sensor_dev_attr_temp1_beep.dev_attr.attr,
623 &sensor_dev_attr_temp2_type.dev_attr.attr, /* 10 */
624 &sensor_dev_attr_temp2_input.dev_attr.attr,
625 &sensor_dev_attr_temp2_min.dev_attr.attr,
626 &sensor_dev_attr_temp2_max.dev_attr.attr,
627 &sensor_dev_attr_temp2_crit.dev_attr.attr,
628 &sensor_dev_attr_temp2_min_alarm.dev_attr.attr,
629 &sensor_dev_attr_temp2_max_alarm.dev_attr.attr,
630 &sensor_dev_attr_temp2_crit_alarm.dev_attr.attr,
631 &sensor_dev_attr_temp2_fault.dev_attr.attr,
632 &sensor_dev_attr_temp2_beep.dev_attr.attr,
634 &sensor_dev_attr_temp3_type.dev_attr.attr, /* 20 */
635 &sensor_dev_attr_temp3_input.dev_attr.attr,
636 &sensor_dev_attr_temp3_min.dev_attr.attr,
637 &sensor_dev_attr_temp3_max.dev_attr.attr,
638 &sensor_dev_attr_temp3_crit.dev_attr.attr,
639 &sensor_dev_attr_temp3_min_alarm.dev_attr.attr,
640 &sensor_dev_attr_temp3_max_alarm.dev_attr.attr,
641 &sensor_dev_attr_temp3_crit_alarm.dev_attr.attr,
642 &sensor_dev_attr_temp3_fault.dev_attr.attr,
643 &sensor_dev_attr_temp3_beep.dev_attr.attr,
645 &sensor_dev_attr_temp4_input.dev_attr.attr, /* 30 */
646 &sensor_dev_attr_temp4_min.dev_attr.attr,
647 &sensor_dev_attr_temp4_max.dev_attr.attr,
648 &sensor_dev_attr_temp4_crit.dev_attr.attr,
649 &sensor_dev_attr_temp4_min_alarm.dev_attr.attr,
650 &sensor_dev_attr_temp4_max_alarm.dev_attr.attr,
651 &sensor_dev_attr_temp4_crit_alarm.dev_attr.attr,
652 &sensor_dev_attr_temp4_beep.dev_attr.attr,
654 &sensor_dev_attr_temp5_input.dev_attr.attr, /* 38 */
655 &sensor_dev_attr_temp5_min.dev_attr.attr,
656 &sensor_dev_attr_temp5_max.dev_attr.attr,
657 &sensor_dev_attr_temp5_crit.dev_attr.attr,
658 &sensor_dev_attr_temp5_min_alarm.dev_attr.attr,
659 &sensor_dev_attr_temp5_max_alarm.dev_attr.attr,
660 &sensor_dev_attr_temp5_crit_alarm.dev_attr.attr,
661 &sensor_dev_attr_temp5_beep.dev_attr.attr,
663 &sensor_dev_attr_temp6_input.dev_attr.attr, /* 46 */
664 &sensor_dev_attr_temp6_beep.dev_attr.attr,
669 static umode_t nct7802_temp_is_visible(struct kobject *kobj,
670 struct attribute *attr, int index)
672 struct device *dev = container_of(kobj, struct device, kobj);
673 struct nct7802_data *data = dev_get_drvdata(dev);
677 err = regmap_read(data->regmap, REG_MODE, ®);
682 (reg & 03) != 0x01 && (reg & 0x03) != 0x02) /* RD1 */
685 if (index >= 10 && index < 20 &&
686 (reg & 0x0c) != 0x04 && (reg & 0x0c) != 0x08) /* RD2 */
688 if (index >= 20 && index < 30 && (reg & 0x30) != 0x20) /* RD3 */
691 if (index >= 30 && index < 38) /* local */
694 err = regmap_read(data->regmap, REG_PECI_ENABLE, ®);
698 if (index >= 38 && index < 46 && !(reg & 0x01)) /* PECI 0 */
701 if (index >= 0x46 && (!(reg & 0x02))) /* PECI 1 */
707 static const struct attribute_group nct7802_temp_group = {
708 .attrs = nct7802_temp_attrs,
709 .is_visible = nct7802_temp_is_visible,
712 static SENSOR_DEVICE_ATTR_2(in0_input, S_IRUGO, show_in, NULL, 0, 0);
713 static SENSOR_DEVICE_ATTR_2(in0_min, S_IRUGO | S_IWUSR, show_in, store_in,
715 static SENSOR_DEVICE_ATTR_2(in0_max, S_IRUGO | S_IWUSR, show_in, store_in,
717 static SENSOR_DEVICE_ATTR_2(in0_alarm, S_IRUGO, show_alarm, NULL, 0x1e, 3);
718 static SENSOR_DEVICE_ATTR_2(in0_beep, S_IRUGO | S_IWUSR, show_beep, store_beep,
721 static SENSOR_DEVICE_ATTR_2(in1_input, S_IRUGO, show_in, NULL, 1, 0);
723 static SENSOR_DEVICE_ATTR_2(in2_input, S_IRUGO, show_in, NULL, 2, 0);
724 static SENSOR_DEVICE_ATTR_2(in2_min, S_IRUGO | S_IWUSR, show_in, store_in,
726 static SENSOR_DEVICE_ATTR_2(in2_max, S_IRUGO | S_IWUSR, show_in, store_in,
728 static SENSOR_DEVICE_ATTR_2(in2_alarm, S_IRUGO, show_alarm, NULL, 0x1e, 0);
729 static SENSOR_DEVICE_ATTR_2(in2_beep, S_IRUGO | S_IWUSR, show_beep, store_beep,
732 static SENSOR_DEVICE_ATTR_2(in3_input, S_IRUGO, show_in, NULL, 3, 0);
733 static SENSOR_DEVICE_ATTR_2(in3_min, S_IRUGO | S_IWUSR, show_in, store_in,
735 static SENSOR_DEVICE_ATTR_2(in3_max, S_IRUGO | S_IWUSR, show_in, store_in,
737 static SENSOR_DEVICE_ATTR_2(in3_alarm, S_IRUGO, show_alarm, NULL, 0x1e, 1);
738 static SENSOR_DEVICE_ATTR_2(in3_beep, S_IRUGO | S_IWUSR, show_beep, store_beep,
741 static SENSOR_DEVICE_ATTR_2(in4_input, S_IRUGO, show_in, NULL, 4, 0);
742 static SENSOR_DEVICE_ATTR_2(in4_min, S_IRUGO | S_IWUSR, show_in, store_in,
744 static SENSOR_DEVICE_ATTR_2(in4_max, S_IRUGO | S_IWUSR, show_in, store_in,
746 static SENSOR_DEVICE_ATTR_2(in4_alarm, S_IRUGO, show_alarm, NULL, 0x1e, 2);
747 static SENSOR_DEVICE_ATTR_2(in4_beep, S_IRUGO | S_IWUSR, show_beep, store_beep,
750 static struct attribute *nct7802_in_attrs[] = {
751 &sensor_dev_attr_in0_input.dev_attr.attr,
752 &sensor_dev_attr_in0_min.dev_attr.attr,
753 &sensor_dev_attr_in0_max.dev_attr.attr,
754 &sensor_dev_attr_in0_alarm.dev_attr.attr,
755 &sensor_dev_attr_in0_beep.dev_attr.attr,
757 &sensor_dev_attr_in1_input.dev_attr.attr, /* 5 */
759 &sensor_dev_attr_in2_input.dev_attr.attr, /* 6 */
760 &sensor_dev_attr_in2_min.dev_attr.attr,
761 &sensor_dev_attr_in2_max.dev_attr.attr,
762 &sensor_dev_attr_in2_alarm.dev_attr.attr,
763 &sensor_dev_attr_in2_beep.dev_attr.attr,
765 &sensor_dev_attr_in3_input.dev_attr.attr, /* 11 */
766 &sensor_dev_attr_in3_min.dev_attr.attr,
767 &sensor_dev_attr_in3_max.dev_attr.attr,
768 &sensor_dev_attr_in3_alarm.dev_attr.attr,
769 &sensor_dev_attr_in3_beep.dev_attr.attr,
771 &sensor_dev_attr_in4_input.dev_attr.attr, /* 17 */
772 &sensor_dev_attr_in4_min.dev_attr.attr,
773 &sensor_dev_attr_in4_max.dev_attr.attr,
774 &sensor_dev_attr_in4_alarm.dev_attr.attr,
775 &sensor_dev_attr_in4_beep.dev_attr.attr,
780 static umode_t nct7802_in_is_visible(struct kobject *kobj,
781 struct attribute *attr, int index)
783 struct device *dev = container_of(kobj, struct device, kobj);
784 struct nct7802_data *data = dev_get_drvdata(dev);
788 if (index < 6) /* VCC, VCORE */
791 err = regmap_read(data->regmap, REG_MODE, ®);
795 if (index >= 6 && index < 11 && (reg & 0x03) != 0x03) /* VSEN1 */
797 if (index >= 11 && index < 17 && (reg & 0x0c) != 0x0c) /* VSEN2 */
799 if (index >= 17 && (reg & 0x30) != 0x30) /* VSEN3 */
805 static const struct attribute_group nct7802_in_group = {
806 .attrs = nct7802_in_attrs,
807 .is_visible = nct7802_in_is_visible,
810 static SENSOR_DEVICE_ATTR(fan1_input, S_IRUGO, show_fan, NULL, 0x10);
811 static SENSOR_DEVICE_ATTR_2(fan1_min, S_IRUGO | S_IWUSR, show_fan_min,
812 store_fan_min, 0x49, 0x4c);
813 static SENSOR_DEVICE_ATTR_2(fan1_alarm, S_IRUGO, show_alarm, NULL, 0x1a, 0);
814 static SENSOR_DEVICE_ATTR_2(fan1_beep, S_IRUGO | S_IWUSR, show_beep, store_beep,
816 static SENSOR_DEVICE_ATTR(fan2_input, S_IRUGO, show_fan, NULL, 0x11);
817 static SENSOR_DEVICE_ATTR_2(fan2_min, S_IRUGO | S_IWUSR, show_fan_min,
818 store_fan_min, 0x4a, 0x4d);
819 static SENSOR_DEVICE_ATTR_2(fan2_alarm, S_IRUGO, show_alarm, NULL, 0x1a, 1);
820 static SENSOR_DEVICE_ATTR_2(fan2_beep, S_IRUGO | S_IWUSR, show_beep, store_beep,
822 static SENSOR_DEVICE_ATTR(fan3_input, S_IRUGO, show_fan, NULL, 0x12);
823 static SENSOR_DEVICE_ATTR_2(fan3_min, S_IRUGO | S_IWUSR, show_fan_min,
824 store_fan_min, 0x4b, 0x4e);
825 static SENSOR_DEVICE_ATTR_2(fan3_alarm, S_IRUGO, show_alarm, NULL, 0x1a, 2);
826 static SENSOR_DEVICE_ATTR_2(fan3_beep, S_IRUGO | S_IWUSR, show_beep, store_beep,
829 /* 7.2.89 Fan Control Output Type */
830 static SENSOR_DEVICE_ATTR(pwm1_mode, S_IRUGO, show_pwm_mode, NULL, 0);
831 static SENSOR_DEVICE_ATTR(pwm2_mode, S_IRUGO, show_pwm_mode, NULL, 1);
832 static SENSOR_DEVICE_ATTR(pwm3_mode, S_IRUGO, show_pwm_mode, NULL, 2);
834 /* 7.2.91... Fan Control Output Value */
835 static SENSOR_DEVICE_ATTR(pwm1, S_IRUGO | S_IWUSR, show_pwm, store_pwm,
837 static SENSOR_DEVICE_ATTR(pwm2, S_IRUGO | S_IWUSR, show_pwm, store_pwm,
839 static SENSOR_DEVICE_ATTR(pwm3, S_IRUGO | S_IWUSR, show_pwm, store_pwm,
842 /* 7.2.95... Temperature to Fan mapping Relationships Register */
843 static SENSOR_DEVICE_ATTR(pwm1_enable, S_IRUGO | S_IWUSR, show_pwm_enable,
844 store_pwm_enable, 0);
845 static SENSOR_DEVICE_ATTR(pwm2_enable, S_IRUGO | S_IWUSR, show_pwm_enable,
846 store_pwm_enable, 1);
847 static SENSOR_DEVICE_ATTR(pwm3_enable, S_IRUGO | S_IWUSR, show_pwm_enable,
848 store_pwm_enable, 2);
850 static struct attribute *nct7802_fan_attrs[] = {
851 &sensor_dev_attr_fan1_input.dev_attr.attr,
852 &sensor_dev_attr_fan1_min.dev_attr.attr,
853 &sensor_dev_attr_fan1_alarm.dev_attr.attr,
854 &sensor_dev_attr_fan1_beep.dev_attr.attr,
855 &sensor_dev_attr_fan2_input.dev_attr.attr,
856 &sensor_dev_attr_fan2_min.dev_attr.attr,
857 &sensor_dev_attr_fan2_alarm.dev_attr.attr,
858 &sensor_dev_attr_fan2_beep.dev_attr.attr,
859 &sensor_dev_attr_fan3_input.dev_attr.attr,
860 &sensor_dev_attr_fan3_min.dev_attr.attr,
861 &sensor_dev_attr_fan3_alarm.dev_attr.attr,
862 &sensor_dev_attr_fan3_beep.dev_attr.attr,
867 static umode_t nct7802_fan_is_visible(struct kobject *kobj,
868 struct attribute *attr, int index)
870 struct device *dev = container_of(kobj, struct device, kobj);
871 struct nct7802_data *data = dev_get_drvdata(dev);
872 int fan = index / 4; /* 4 attributes per fan */
876 err = regmap_read(data->regmap, REG_FAN_ENABLE, ®);
877 if (err < 0 || !(reg & (1 << fan)))
883 static const struct attribute_group nct7802_fan_group = {
884 .attrs = nct7802_fan_attrs,
885 .is_visible = nct7802_fan_is_visible,
888 static struct attribute *nct7802_pwm_attrs[] = {
889 &sensor_dev_attr_pwm1_enable.dev_attr.attr,
890 &sensor_dev_attr_pwm1_mode.dev_attr.attr,
891 &sensor_dev_attr_pwm1.dev_attr.attr,
892 &sensor_dev_attr_pwm2_enable.dev_attr.attr,
893 &sensor_dev_attr_pwm2_mode.dev_attr.attr,
894 &sensor_dev_attr_pwm2.dev_attr.attr,
895 &sensor_dev_attr_pwm3_enable.dev_attr.attr,
896 &sensor_dev_attr_pwm3_mode.dev_attr.attr,
897 &sensor_dev_attr_pwm3.dev_attr.attr,
901 static const struct attribute_group nct7802_pwm_group = {
902 .attrs = nct7802_pwm_attrs,
905 /* 7.2.115... 0x80-0x83, 0x84 Temperature (X-axis) transition */
906 static SENSOR_DEVICE_ATTR_2(pwm1_auto_point1_temp, S_IRUGO | S_IWUSR,
907 show_temp, store_temp, 0x80, 0);
908 static SENSOR_DEVICE_ATTR_2(pwm1_auto_point2_temp, S_IRUGO | S_IWUSR,
909 show_temp, store_temp, 0x81, 0);
910 static SENSOR_DEVICE_ATTR_2(pwm1_auto_point3_temp, S_IRUGO | S_IWUSR,
911 show_temp, store_temp, 0x82, 0);
912 static SENSOR_DEVICE_ATTR_2(pwm1_auto_point4_temp, S_IRUGO | S_IWUSR,
913 show_temp, store_temp, 0x83, 0);
914 static SENSOR_DEVICE_ATTR_2(pwm1_auto_point5_temp, S_IRUGO | S_IWUSR,
915 show_temp, store_temp, 0x84, 0);
917 /* 7.2.120... 0x85-0x88 PWM (Y-axis) transition */
918 static SENSOR_DEVICE_ATTR(pwm1_auto_point1_pwm, S_IRUGO | S_IWUSR,
919 show_pwm, store_pwm, 0x85);
920 static SENSOR_DEVICE_ATTR(pwm1_auto_point2_pwm, S_IRUGO | S_IWUSR,
921 show_pwm, store_pwm, 0x86);
922 static SENSOR_DEVICE_ATTR(pwm1_auto_point3_pwm, S_IRUGO | S_IWUSR,
923 show_pwm, store_pwm, 0x87);
924 static SENSOR_DEVICE_ATTR(pwm1_auto_point4_pwm, S_IRUGO | S_IWUSR,
925 show_pwm, store_pwm, 0x88);
926 static SENSOR_DEVICE_ATTR(pwm1_auto_point5_pwm, S_IRUGO, show_pwm, NULL, 0);
928 /* 7.2.124 Table 2 X-axis Transition Point 1 Register */
929 static SENSOR_DEVICE_ATTR_2(pwm2_auto_point1_temp, S_IRUGO | S_IWUSR,
930 show_temp, store_temp, 0x90, 0);
931 static SENSOR_DEVICE_ATTR_2(pwm2_auto_point2_temp, S_IRUGO | S_IWUSR,
932 show_temp, store_temp, 0x91, 0);
933 static SENSOR_DEVICE_ATTR_2(pwm2_auto_point3_temp, S_IRUGO | S_IWUSR,
934 show_temp, store_temp, 0x92, 0);
935 static SENSOR_DEVICE_ATTR_2(pwm2_auto_point4_temp, S_IRUGO | S_IWUSR,
936 show_temp, store_temp, 0x93, 0);
937 static SENSOR_DEVICE_ATTR_2(pwm2_auto_point5_temp, S_IRUGO | S_IWUSR,
938 show_temp, store_temp, 0x94, 0);
940 /* 7.2.129 Table 2 Y-axis Transition Point 1 Register */
941 static SENSOR_DEVICE_ATTR(pwm2_auto_point1_pwm, S_IRUGO | S_IWUSR,
942 show_pwm, store_pwm, 0x95);
943 static SENSOR_DEVICE_ATTR(pwm2_auto_point2_pwm, S_IRUGO | S_IWUSR,
944 show_pwm, store_pwm, 0x96);
945 static SENSOR_DEVICE_ATTR(pwm2_auto_point3_pwm, S_IRUGO | S_IWUSR,
946 show_pwm, store_pwm, 0x97);
947 static SENSOR_DEVICE_ATTR(pwm2_auto_point4_pwm, S_IRUGO | S_IWUSR,
948 show_pwm, store_pwm, 0x98);
949 static SENSOR_DEVICE_ATTR(pwm2_auto_point5_pwm, S_IRUGO, show_pwm, NULL, 0);
951 /* 7.2.133 Table 3 X-axis Transition Point 1 Register */
952 static SENSOR_DEVICE_ATTR_2(pwm3_auto_point1_temp, S_IRUGO | S_IWUSR,
953 show_temp, store_temp, 0xA0, 0);
954 static SENSOR_DEVICE_ATTR_2(pwm3_auto_point2_temp, S_IRUGO | S_IWUSR,
955 show_temp, store_temp, 0xA1, 0);
956 static SENSOR_DEVICE_ATTR_2(pwm3_auto_point3_temp, S_IRUGO | S_IWUSR,
957 show_temp, store_temp, 0xA2, 0);
958 static SENSOR_DEVICE_ATTR_2(pwm3_auto_point4_temp, S_IRUGO | S_IWUSR,
959 show_temp, store_temp, 0xA3, 0);
960 static SENSOR_DEVICE_ATTR_2(pwm3_auto_point5_temp, S_IRUGO | S_IWUSR,
961 show_temp, store_temp, 0xA4, 0);
963 /* 7.2.138 Table 3 Y-axis Transition Point 1 Register */
964 static SENSOR_DEVICE_ATTR(pwm3_auto_point1_pwm, S_IRUGO | S_IWUSR,
965 show_pwm, store_pwm, 0xA5);
966 static SENSOR_DEVICE_ATTR(pwm3_auto_point2_pwm, S_IRUGO | S_IWUSR,
967 show_pwm, store_pwm, 0xA6);
968 static SENSOR_DEVICE_ATTR(pwm3_auto_point3_pwm, S_IRUGO | S_IWUSR,
969 show_pwm, store_pwm, 0xA7);
970 static SENSOR_DEVICE_ATTR(pwm3_auto_point4_pwm, S_IRUGO | S_IWUSR,
971 show_pwm, store_pwm, 0xA8);
972 static SENSOR_DEVICE_ATTR(pwm3_auto_point5_pwm, S_IRUGO, show_pwm, NULL, 0);
974 static struct attribute *nct7802_auto_point_attrs[] = {
975 &sensor_dev_attr_pwm1_auto_point1_temp.dev_attr.attr,
976 &sensor_dev_attr_pwm1_auto_point2_temp.dev_attr.attr,
977 &sensor_dev_attr_pwm1_auto_point3_temp.dev_attr.attr,
978 &sensor_dev_attr_pwm1_auto_point4_temp.dev_attr.attr,
979 &sensor_dev_attr_pwm1_auto_point5_temp.dev_attr.attr,
981 &sensor_dev_attr_pwm1_auto_point1_pwm.dev_attr.attr,
982 &sensor_dev_attr_pwm1_auto_point2_pwm.dev_attr.attr,
983 &sensor_dev_attr_pwm1_auto_point3_pwm.dev_attr.attr,
984 &sensor_dev_attr_pwm1_auto_point4_pwm.dev_attr.attr,
985 &sensor_dev_attr_pwm1_auto_point5_pwm.dev_attr.attr,
987 &sensor_dev_attr_pwm2_auto_point1_temp.dev_attr.attr,
988 &sensor_dev_attr_pwm2_auto_point2_temp.dev_attr.attr,
989 &sensor_dev_attr_pwm2_auto_point3_temp.dev_attr.attr,
990 &sensor_dev_attr_pwm2_auto_point4_temp.dev_attr.attr,
991 &sensor_dev_attr_pwm2_auto_point5_temp.dev_attr.attr,
993 &sensor_dev_attr_pwm2_auto_point1_pwm.dev_attr.attr,
994 &sensor_dev_attr_pwm2_auto_point2_pwm.dev_attr.attr,
995 &sensor_dev_attr_pwm2_auto_point3_pwm.dev_attr.attr,
996 &sensor_dev_attr_pwm2_auto_point4_pwm.dev_attr.attr,
997 &sensor_dev_attr_pwm2_auto_point5_pwm.dev_attr.attr,
999 &sensor_dev_attr_pwm3_auto_point1_temp.dev_attr.attr,
1000 &sensor_dev_attr_pwm3_auto_point2_temp.dev_attr.attr,
1001 &sensor_dev_attr_pwm3_auto_point3_temp.dev_attr.attr,
1002 &sensor_dev_attr_pwm3_auto_point4_temp.dev_attr.attr,
1003 &sensor_dev_attr_pwm3_auto_point5_temp.dev_attr.attr,
1005 &sensor_dev_attr_pwm3_auto_point1_pwm.dev_attr.attr,
1006 &sensor_dev_attr_pwm3_auto_point2_pwm.dev_attr.attr,
1007 &sensor_dev_attr_pwm3_auto_point3_pwm.dev_attr.attr,
1008 &sensor_dev_attr_pwm3_auto_point4_pwm.dev_attr.attr,
1009 &sensor_dev_attr_pwm3_auto_point5_pwm.dev_attr.attr,
1014 static const struct attribute_group nct7802_auto_point_group = {
1015 .attrs = nct7802_auto_point_attrs,
1018 static const struct attribute_group *nct7802_groups[] = {
1019 &nct7802_temp_group,
1023 &nct7802_auto_point_group,
1027 static int nct7802_detect(struct i2c_client *client,
1028 struct i2c_board_info *info)
1033 * Chip identification registers are only available in bank 0,
1034 * so only attempt chip detection if bank 0 is selected
1036 reg = i2c_smbus_read_byte_data(client, REG_BANK);
1040 reg = i2c_smbus_read_byte_data(client, REG_VENDOR_ID);
1044 reg = i2c_smbus_read_byte_data(client, REG_CHIP_ID);
1048 reg = i2c_smbus_read_byte_data(client, REG_VERSION_ID);
1049 if (reg < 0 || (reg & 0xf0) != 0x20)
1052 /* Also validate lower bits of voltage and temperature registers */
1053 reg = i2c_smbus_read_byte_data(client, REG_TEMP_LSB);
1054 if (reg < 0 || (reg & 0x1f))
1057 reg = i2c_smbus_read_byte_data(client, REG_TEMP_PECI_LSB);
1058 if (reg < 0 || (reg & 0x3f))
1061 reg = i2c_smbus_read_byte_data(client, REG_VOLTAGE_LOW);
1062 if (reg < 0 || (reg & 0x3f))
1065 strlcpy(info->type, "nct7802", I2C_NAME_SIZE);
1069 static bool nct7802_regmap_is_volatile(struct device *dev, unsigned int reg)
1071 return (reg != REG_BANK && reg <= 0x20) ||
1072 (reg >= REG_PWM(0) && reg <= REG_PWM(2));
1075 static const struct regmap_config nct7802_regmap_config = {
1078 .cache_type = REGCACHE_RBTREE,
1079 .volatile_reg = nct7802_regmap_is_volatile,
1082 static int nct7802_init_chip(struct nct7802_data *data)
1087 err = regmap_update_bits(data->regmap, REG_START, 0x01, 0x01);
1091 /* Enable local temperature sensor */
1092 err = regmap_update_bits(data->regmap, REG_MODE, 0x40, 0x40);
1096 /* Enable Vcore and VCC voltage monitoring */
1097 return regmap_update_bits(data->regmap, REG_VMON_ENABLE, 0x03, 0x03);
1100 static int nct7802_probe(struct i2c_client *client,
1101 const struct i2c_device_id *id)
1103 struct device *dev = &client->dev;
1104 struct nct7802_data *data;
1105 struct device *hwmon_dev;
1108 data = devm_kzalloc(dev, sizeof(*data), GFP_KERNEL);
1112 data->regmap = devm_regmap_init_i2c(client, &nct7802_regmap_config);
1113 if (IS_ERR(data->regmap))
1114 return PTR_ERR(data->regmap);
1116 mutex_init(&data->access_lock);
1118 ret = nct7802_init_chip(data);
1122 hwmon_dev = devm_hwmon_device_register_with_groups(dev, client->name,
1125 return PTR_ERR_OR_ZERO(hwmon_dev);
1128 static const unsigned short nct7802_address_list[] = {
1129 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f, I2C_CLIENT_END
1132 static const struct i2c_device_id nct7802_idtable[] = {
1136 MODULE_DEVICE_TABLE(i2c, nct7802_idtable);
1138 static struct i2c_driver nct7802_driver = {
1139 .class = I2C_CLASS_HWMON,
1143 .detect = nct7802_detect,
1144 .probe = nct7802_probe,
1145 .id_table = nct7802_idtable,
1146 .address_list = nct7802_address_list,
1149 module_i2c_driver(nct7802_driver);
1151 MODULE_AUTHOR("Guenter Roeck <linux@roeck-us.net>");
1152 MODULE_DESCRIPTION("NCT7802Y Hardware Monitoring Driver");
1153 MODULE_LICENSE("GPL v2");