1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * nct6683 - Driver for the hardware monitoring functionality of
4 * Nuvoton NCT6683D/NCT6687D eSIO
6 * Copyright (C) 2013 Guenter Roeck <linux@roeck-us.net>
8 * Derived from nct6775 driver
9 * Copyright (C) 2012, 2013 Guenter Roeck <linux@roeck-us.net>
11 * Supports the following chips:
13 * Chip #vin #fan #pwm #temp chip ID
14 * nct6683d 21(1) 16 8 32(1) 0xc730
15 * nct6687d 21(1) 16 8 32(1) 0xd590
18 * (1) Total number of vin and temp inputs is 32.
21 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
23 #include <linux/acpi.h>
24 #include <linux/delay.h>
25 #include <linux/err.h>
26 #include <linux/init.h>
28 #include <linux/jiffies.h>
29 #include <linux/hwmon.h>
30 #include <linux/hwmon-sysfs.h>
31 #include <linux/module.h>
32 #include <linux/mutex.h>
33 #include <linux/platform_device.h>
34 #include <linux/slab.h>
36 enum kinds
{ nct6683
, nct6687
};
39 module_param(force
, bool, 0);
40 MODULE_PARM_DESC(force
, "Set to one to enable support for unknown vendors");
42 static const char * const nct6683_device_names
[] = {
47 static const char * const nct6683_chip_names
[] = {
52 #define DRVNAME "nct6683"
55 * Super-I/O constants and functions
58 #define NCT6683_LD_ACPI 0x0a
59 #define NCT6683_LD_HWM 0x0b
60 #define NCT6683_LD_VID 0x0d
62 #define SIO_REG_LDSEL 0x07 /* Logical device select */
63 #define SIO_REG_DEVID 0x20 /* Device ID (2 bytes) */
64 #define SIO_REG_ENABLE 0x30 /* Logical device enable */
65 #define SIO_REG_ADDR 0x60 /* Logical device address (2 bytes) */
67 #define SIO_NCT6681_ID 0xb270 /* for later */
68 #define SIO_NCT6683_ID 0xc730
69 #define SIO_NCT6687_ID 0xd590
70 #define SIO_ID_MASK 0xFFF0
73 superio_outb(int ioreg
, int reg
, int val
)
80 superio_inb(int ioreg
, int reg
)
83 return inb(ioreg
+ 1);
87 superio_select(int ioreg
, int ld
)
89 outb(SIO_REG_LDSEL
, ioreg
);
94 superio_enter(int ioreg
)
97 * Try to reserve <ioreg> and <ioreg + 1> for exclusive access.
99 if (!request_muxed_region(ioreg
, 2, DRVNAME
))
109 superio_exit(int ioreg
)
113 outb(0x02, ioreg
+ 1);
114 release_region(ioreg
, 2);
121 #define IOREGION_ALIGNMENT (~7)
122 #define IOREGION_OFFSET 4 /* Use EC port 1 */
123 #define IOREGION_LENGTH 4
125 #define EC_PAGE_REG 0
126 #define EC_INDEX_REG 1
127 #define EC_DATA_REG 2
128 #define EC_EVENT_REG 3
130 /* Common and NCT6683 specific data */
132 #define NCT6683_NUM_REG_MON 32
133 #define NCT6683_NUM_REG_FAN 16
134 #define NCT6683_NUM_REG_PWM 8
136 #define NCT6683_REG_MON(x) (0x100 + (x) * 2)
137 #define NCT6683_REG_FAN_RPM(x) (0x140 + (x) * 2)
138 #define NCT6683_REG_PWM(x) (0x160 + (x))
139 #define NCT6683_REG_PWM_WRITE(x) (0xa28 + (x))
141 #define NCT6683_REG_MON_STS(x) (0x174 + (x))
142 #define NCT6683_REG_IDLE(x) (0x178 + (x))
144 #define NCT6683_REG_FAN_STS(x) (0x17c + (x))
145 #define NCT6683_REG_FAN_ERRSTS 0x17e
146 #define NCT6683_REG_FAN_INITSTS 0x17f
148 #define NCT6683_HWM_CFG 0x180
150 #define NCT6683_REG_MON_CFG(x) (0x1a0 + (x))
151 #define NCT6683_REG_FANIN_CFG(x) (0x1c0 + (x))
152 #define NCT6683_REG_FANOUT_CFG(x) (0x1d0 + (x))
154 #define NCT6683_REG_INTEL_TEMP_MAX(x) (0x901 + (x) * 16)
155 #define NCT6683_REG_INTEL_TEMP_CRIT(x) (0x90d + (x) * 16)
157 #define NCT6683_REG_TEMP_HYST(x) (0x330 + (x)) /* 8 bit */
158 #define NCT6683_REG_TEMP_MAX(x) (0x350 + (x)) /* 8 bit */
159 #define NCT6683_REG_MON_HIGH(x) (0x370 + (x) * 2) /* 8 bit */
160 #define NCT6683_REG_MON_LOW(x) (0x371 + (x) * 2) /* 8 bit */
162 #define NCT6683_REG_FAN_MIN(x) (0x3b8 + (x) * 2) /* 16 bit */
164 #define NCT6683_REG_FAN_CFG_CTRL 0xa01
165 #define NCT6683_FAN_CFG_REQ 0x80
166 #define NCT6683_FAN_CFG_DONE 0x40
168 #define NCT6683_REG_CUSTOMER_ID 0x602
169 #define NCT6683_CUSTOMER_ID_INTEL 0x805
170 #define NCT6683_CUSTOMER_ID_MITAC 0xa0e
171 #define NCT6683_CUSTOMER_ID_MSI 0x201
173 #define NCT6683_REG_BUILD_YEAR 0x604
174 #define NCT6683_REG_BUILD_MONTH 0x605
175 #define NCT6683_REG_BUILD_DAY 0x606
176 #define NCT6683_REG_SERIAL 0x607
177 #define NCT6683_REG_VERSION_HI 0x608
178 #define NCT6683_REG_VERSION_LO 0x609
180 #define NCT6683_REG_CR_CASEOPEN 0xe8
181 #define NCT6683_CR_CASEOPEN_MASK (1 << 7)
183 #define NCT6683_REG_CR_BEEP 0xe0
184 #define NCT6683_CR_BEEP_MASK (1 << 6)
186 static const char *const nct6683_mon_label
[] = {
203 "Thermistor 5", /* 0x10 */
212 NULL
, NULL
, NULL
, NULL
, NULL
, NULL
, NULL
,
213 "PECI 0.0", /* 0x20 */
225 NULL
, NULL
, NULL
, NULL
,
226 "PCH CPU", /* 0x30 */
252 NULL
, NULL
, NULL
, NULL
, NULL
, NULL
,
253 "Virtual 0", /* 0x50 */
261 NULL
, NULL
, NULL
, NULL
, NULL
, NULL
, NULL
, NULL
,
262 "VCC", /* 0x60 voltage sensors */
287 #define NUM_MON_LABELS ARRAY_SIZE(nct6683_mon_label)
288 #define MON_VOLTAGE_START 0x60
290 /* ------------------------------------------------------- */
292 struct nct6683_data
{
293 int addr
; /* IO base of EC space */
294 int sioreg
; /* SIO register */
298 struct device
*hwmon_dev
;
299 const struct attribute_group
*groups
[6];
301 int temp_num
; /* number of temperature attributes */
302 u8 temp_index
[NCT6683_NUM_REG_MON
];
303 u8 temp_src
[NCT6683_NUM_REG_MON
];
305 u8 in_num
; /* number of voltage attributes */
306 u8 in_index
[NCT6683_NUM_REG_MON
];
307 u8 in_src
[NCT6683_NUM_REG_MON
];
309 struct mutex update_lock
; /* used to protect sensor updates */
310 bool valid
; /* true if following fields are valid */
311 unsigned long last_updated
; /* In jiffies */
313 /* Voltage attribute values */
314 u8 in
[3][NCT6683_NUM_REG_MON
]; /* [0]=in, [1]=in_max, [2]=in_min */
316 /* Temperature attribute values */
317 s16 temp_in
[NCT6683_NUM_REG_MON
];
318 s8 temp
[4][NCT6683_NUM_REG_MON
];/* [0]=min, [1]=max, [2]=hyst,
322 /* Fan attribute values */
323 unsigned int rpm
[NCT6683_NUM_REG_FAN
];
324 u16 fan_min
[NCT6683_NUM_REG_FAN
];
325 u8 fanin_cfg
[NCT6683_NUM_REG_FAN
];
326 u8 fanout_cfg
[NCT6683_NUM_REG_FAN
];
327 u16 have_fan
; /* some fan inputs can be disabled */
330 u8 pwm
[NCT6683_NUM_REG_PWM
];
333 /* Remember extra register values over suspend/resume */
338 struct nct6683_sio_data
{
343 struct sensor_device_template
{
344 struct device_attribute dev_attr
;
352 bool s2
; /* true if both index and nr are used */
355 struct sensor_device_attr_u
{
357 struct sensor_device_attribute a1
;
358 struct sensor_device_attribute_2 a2
;
363 #define __TEMPLATE_ATTR(_template, _mode, _show, _store) { \
364 .attr = {.name = _template, .mode = _mode }, \
369 #define SENSOR_DEVICE_TEMPLATE(_template, _mode, _show, _store, _index) \
370 { .dev_attr = __TEMPLATE_ATTR(_template, _mode, _show, _store), \
374 #define SENSOR_DEVICE_TEMPLATE_2(_template, _mode, _show, _store, \
376 { .dev_attr = __TEMPLATE_ATTR(_template, _mode, _show, _store), \
377 .u.s.index = _index, \
381 #define SENSOR_TEMPLATE(_name, _template, _mode, _show, _store, _index) \
382 static struct sensor_device_template sensor_dev_template_##_name \
383 = SENSOR_DEVICE_TEMPLATE(_template, _mode, _show, _store, \
386 #define SENSOR_TEMPLATE_2(_name, _template, _mode, _show, _store, \
388 static struct sensor_device_template sensor_dev_template_##_name \
389 = SENSOR_DEVICE_TEMPLATE_2(_template, _mode, _show, _store, \
392 struct sensor_template_group
{
393 struct sensor_device_template
**templates
;
394 umode_t (*is_visible
)(struct kobject
*, struct attribute
*, int);
398 static struct attribute_group
*
399 nct6683_create_attr_group(struct device
*dev
,
400 const struct sensor_template_group
*tg
,
403 struct sensor_device_attribute_2
*a2
;
404 struct sensor_device_attribute
*a
;
405 struct sensor_device_template
**t
;
406 struct sensor_device_attr_u
*su
;
407 struct attribute_group
*group
;
408 struct attribute
**attrs
;
412 return ERR_PTR(-EINVAL
);
415 for (count
= 0; *t
; t
++, count
++)
419 return ERR_PTR(-EINVAL
);
421 group
= devm_kzalloc(dev
, sizeof(*group
), GFP_KERNEL
);
423 return ERR_PTR(-ENOMEM
);
425 attrs
= devm_kcalloc(dev
, repeat
* count
+ 1, sizeof(*attrs
),
428 return ERR_PTR(-ENOMEM
);
430 su
= devm_kzalloc(dev
, array3_size(repeat
, count
, sizeof(*su
)),
433 return ERR_PTR(-ENOMEM
);
435 group
->attrs
= attrs
;
436 group
->is_visible
= tg
->is_visible
;
438 for (i
= 0; i
< repeat
; i
++) {
440 for (j
= 0; *t
!= NULL
; j
++) {
441 snprintf(su
->name
, sizeof(su
->name
),
442 (*t
)->dev_attr
.attr
.name
, tg
->base
+ i
);
445 sysfs_attr_init(&a2
->dev_attr
.attr
);
446 a2
->dev_attr
.attr
.name
= su
->name
;
447 a2
->nr
= (*t
)->u
.s
.nr
+ i
;
448 a2
->index
= (*t
)->u
.s
.index
;
449 a2
->dev_attr
.attr
.mode
=
450 (*t
)->dev_attr
.attr
.mode
;
451 a2
->dev_attr
.show
= (*t
)->dev_attr
.show
;
452 a2
->dev_attr
.store
= (*t
)->dev_attr
.store
;
453 *attrs
= &a2
->dev_attr
.attr
;
456 sysfs_attr_init(&a
->dev_attr
.attr
);
457 a
->dev_attr
.attr
.name
= su
->name
;
458 a
->index
= (*t
)->u
.index
+ i
;
459 a
->dev_attr
.attr
.mode
=
460 (*t
)->dev_attr
.attr
.mode
;
461 a
->dev_attr
.show
= (*t
)->dev_attr
.show
;
462 a
->dev_attr
.store
= (*t
)->dev_attr
.store
;
463 *attrs
= &a
->dev_attr
.attr
;
474 /* LSB is 16 mV, except for the following sources, where it is 32 mV */
475 #define MON_SRC_VCC 0x60
476 #define MON_SRC_VSB 0x61
477 #define MON_SRC_AVSB 0x62
478 #define MON_SRC_VBAT 0x64
480 static inline long in_from_reg(u16 reg
, u8 src
)
484 if (src
== MON_SRC_VCC
|| src
== MON_SRC_VSB
|| src
== MON_SRC_AVSB
||
490 static inline u16
in_to_reg(u32 val
, u8 src
)
494 if (src
== MON_SRC_VCC
|| src
== MON_SRC_VSB
|| src
== MON_SRC_AVSB
||
498 return clamp_val(DIV_ROUND_CLOSEST(val
, scale
), 0, 127);
501 static u16
nct6683_read(struct nct6683_data
*data
, u16 reg
)
505 outb_p(0xff, data
->addr
+ EC_PAGE_REG
); /* unlock */
506 outb_p(reg
>> 8, data
->addr
+ EC_PAGE_REG
);
507 outb_p(reg
& 0xff, data
->addr
+ EC_INDEX_REG
);
508 res
= inb_p(data
->addr
+ EC_DATA_REG
);
512 static u16
nct6683_read16(struct nct6683_data
*data
, u16 reg
)
514 return (nct6683_read(data
, reg
) << 8) | nct6683_read(data
, reg
+ 1);
517 static void nct6683_write(struct nct6683_data
*data
, u16 reg
, u16 value
)
519 outb_p(0xff, data
->addr
+ EC_PAGE_REG
); /* unlock */
520 outb_p(reg
>> 8, data
->addr
+ EC_PAGE_REG
);
521 outb_p(reg
& 0xff, data
->addr
+ EC_INDEX_REG
);
522 outb_p(value
& 0xff, data
->addr
+ EC_DATA_REG
);
525 static int get_in_reg(struct nct6683_data
*data
, int nr
, int index
)
527 int ch
= data
->in_index
[index
];
532 reg
= NCT6683_REG_MON(ch
);
535 if (data
->customer_id
!= NCT6683_CUSTOMER_ID_INTEL
)
536 reg
= NCT6683_REG_MON_LOW(ch
);
539 if (data
->customer_id
!= NCT6683_CUSTOMER_ID_INTEL
)
540 reg
= NCT6683_REG_MON_HIGH(ch
);
548 static int get_temp_reg(struct nct6683_data
*data
, int nr
, int index
)
550 int ch
= data
->temp_index
[index
];
553 switch (data
->customer_id
) {
554 case NCT6683_CUSTOMER_ID_INTEL
:
558 reg
= NCT6683_REG_INTEL_TEMP_MAX(ch
);
561 reg
= NCT6683_REG_INTEL_TEMP_CRIT(ch
);
565 case NCT6683_CUSTOMER_ID_MITAC
:
570 reg
= NCT6683_REG_MON_LOW(ch
);
573 reg
= NCT6683_REG_TEMP_MAX(ch
);
576 reg
= NCT6683_REG_TEMP_HYST(ch
);
579 reg
= NCT6683_REG_MON_HIGH(ch
);
587 static void nct6683_update_pwm(struct device
*dev
)
589 struct nct6683_data
*data
= dev_get_drvdata(dev
);
592 for (i
= 0; i
< NCT6683_NUM_REG_PWM
; i
++) {
593 if (!(data
->have_pwm
& (1 << i
)))
595 data
->pwm
[i
] = nct6683_read(data
, NCT6683_REG_PWM(i
));
599 static struct nct6683_data
*nct6683_update_device(struct device
*dev
)
601 struct nct6683_data
*data
= dev_get_drvdata(dev
);
604 mutex_lock(&data
->update_lock
);
606 if (time_after(jiffies
, data
->last_updated
+ HZ
) || !data
->valid
) {
607 /* Measured voltages and limits */
608 for (i
= 0; i
< data
->in_num
; i
++) {
609 for (j
= 0; j
< 3; j
++) {
610 int reg
= get_in_reg(data
, j
, i
);
614 nct6683_read(data
, reg
);
618 /* Measured temperatures and limits */
619 for (i
= 0; i
< data
->temp_num
; i
++) {
620 u8 ch
= data
->temp_index
[i
];
622 data
->temp_in
[i
] = nct6683_read16(data
,
623 NCT6683_REG_MON(ch
));
624 for (j
= 0; j
< 4; j
++) {
625 int reg
= get_temp_reg(data
, j
, i
);
629 nct6683_read(data
, reg
);
633 /* Measured fan speeds and limits */
634 for (i
= 0; i
< ARRAY_SIZE(data
->rpm
); i
++) {
635 if (!(data
->have_fan
& (1 << i
)))
638 data
->rpm
[i
] = nct6683_read16(data
,
639 NCT6683_REG_FAN_RPM(i
));
640 data
->fan_min
[i
] = nct6683_read16(data
,
641 NCT6683_REG_FAN_MIN(i
));
644 nct6683_update_pwm(dev
);
646 data
->last_updated
= jiffies
;
650 mutex_unlock(&data
->update_lock
);
655 * Sysfs callback functions
658 show_in_label(struct device
*dev
, struct device_attribute
*attr
, char *buf
)
660 struct sensor_device_attribute
*sattr
= to_sensor_dev_attr(attr
);
661 struct nct6683_data
*data
= nct6683_update_device(dev
);
662 int nr
= sattr
->index
;
664 return sprintf(buf
, "%s\n", nct6683_mon_label
[data
->in_src
[nr
]]);
668 show_in_reg(struct device
*dev
, struct device_attribute
*attr
, char *buf
)
670 struct sensor_device_attribute_2
*sattr
= to_sensor_dev_attr_2(attr
);
671 struct nct6683_data
*data
= nct6683_update_device(dev
);
672 int index
= sattr
->index
;
675 return sprintf(buf
, "%ld\n",
676 in_from_reg(data
->in
[index
][nr
], data
->in_index
[index
]));
679 static umode_t
nct6683_in_is_visible(struct kobject
*kobj
,
680 struct attribute
*attr
, int index
)
682 struct device
*dev
= kobj_to_dev(kobj
);
683 struct nct6683_data
*data
= dev_get_drvdata(dev
);
684 int nr
= index
% 4; /* attribute */
687 * Voltage limits exist for Intel boards,
688 * but register location and encoding is unknown
690 if ((nr
== 2 || nr
== 3) &&
691 data
->customer_id
== NCT6683_CUSTOMER_ID_INTEL
)
697 SENSOR_TEMPLATE(in_label
, "in%d_label", S_IRUGO
, show_in_label
, NULL
, 0);
698 SENSOR_TEMPLATE_2(in_input
, "in%d_input", S_IRUGO
, show_in_reg
, NULL
, 0, 0);
699 SENSOR_TEMPLATE_2(in_min
, "in%d_min", S_IRUGO
, show_in_reg
, NULL
, 0, 1);
700 SENSOR_TEMPLATE_2(in_max
, "in%d_max", S_IRUGO
, show_in_reg
, NULL
, 0, 2);
702 static struct sensor_device_template
*nct6683_attributes_in_template
[] = {
703 &sensor_dev_template_in_label
,
704 &sensor_dev_template_in_input
,
705 &sensor_dev_template_in_min
,
706 &sensor_dev_template_in_max
,
710 static const struct sensor_template_group nct6683_in_template_group
= {
711 .templates
= nct6683_attributes_in_template
,
712 .is_visible
= nct6683_in_is_visible
,
716 show_fan(struct device
*dev
, struct device_attribute
*attr
, char *buf
)
718 struct sensor_device_attribute
*sattr
= to_sensor_dev_attr(attr
);
719 struct nct6683_data
*data
= nct6683_update_device(dev
);
721 return sprintf(buf
, "%d\n", data
->rpm
[sattr
->index
]);
725 show_fan_min(struct device
*dev
, struct device_attribute
*attr
, char *buf
)
727 struct nct6683_data
*data
= nct6683_update_device(dev
);
728 struct sensor_device_attribute
*sattr
= to_sensor_dev_attr(attr
);
729 int nr
= sattr
->index
;
731 return sprintf(buf
, "%d\n", data
->fan_min
[nr
]);
735 show_fan_pulses(struct device
*dev
, struct device_attribute
*attr
, char *buf
)
737 struct sensor_device_attribute
*sattr
= to_sensor_dev_attr(attr
);
738 struct nct6683_data
*data
= nct6683_update_device(dev
);
740 return sprintf(buf
, "%d\n",
741 ((data
->fanin_cfg
[sattr
->index
] >> 5) & 0x03) + 1);
744 static umode_t
nct6683_fan_is_visible(struct kobject
*kobj
,
745 struct attribute
*attr
, int index
)
747 struct device
*dev
= kobj_to_dev(kobj
);
748 struct nct6683_data
*data
= dev_get_drvdata(dev
);
749 int fan
= index
/ 3; /* fan index */
750 int nr
= index
% 3; /* attribute index */
752 if (!(data
->have_fan
& (1 << fan
)))
756 * Intel may have minimum fan speed limits,
757 * but register location and encoding are unknown.
759 if (nr
== 2 && data
->customer_id
== NCT6683_CUSTOMER_ID_INTEL
)
765 SENSOR_TEMPLATE(fan_input
, "fan%d_input", S_IRUGO
, show_fan
, NULL
, 0);
766 SENSOR_TEMPLATE(fan_pulses
, "fan%d_pulses", S_IRUGO
, show_fan_pulses
, NULL
, 0);
767 SENSOR_TEMPLATE(fan_min
, "fan%d_min", S_IRUGO
, show_fan_min
, NULL
, 0);
770 * nct6683_fan_is_visible uses the index into the following array
771 * to determine if attributes should be created or not.
772 * Any change in order or content must be matched.
774 static struct sensor_device_template
*nct6683_attributes_fan_template
[] = {
775 &sensor_dev_template_fan_input
,
776 &sensor_dev_template_fan_pulses
,
777 &sensor_dev_template_fan_min
,
781 static const struct sensor_template_group nct6683_fan_template_group
= {
782 .templates
= nct6683_attributes_fan_template
,
783 .is_visible
= nct6683_fan_is_visible
,
788 show_temp_label(struct device
*dev
, struct device_attribute
*attr
, char *buf
)
790 struct sensor_device_attribute
*sattr
= to_sensor_dev_attr(attr
);
791 struct nct6683_data
*data
= nct6683_update_device(dev
);
792 int nr
= sattr
->index
;
794 return sprintf(buf
, "%s\n", nct6683_mon_label
[data
->temp_src
[nr
]]);
798 show_temp8(struct device
*dev
, struct device_attribute
*attr
, char *buf
)
800 struct sensor_device_attribute_2
*sattr
= to_sensor_dev_attr_2(attr
);
801 struct nct6683_data
*data
= nct6683_update_device(dev
);
802 int index
= sattr
->index
;
805 return sprintf(buf
, "%d\n", data
->temp
[index
][nr
] * 1000);
809 show_temp_hyst(struct device
*dev
, struct device_attribute
*attr
, char *buf
)
811 struct sensor_device_attribute
*sattr
= to_sensor_dev_attr(attr
);
812 struct nct6683_data
*data
= nct6683_update_device(dev
);
813 int nr
= sattr
->index
;
814 int temp
= data
->temp
[1][nr
] - data
->temp
[2][nr
];
816 return sprintf(buf
, "%d\n", temp
* 1000);
820 show_temp16(struct device
*dev
, struct device_attribute
*attr
, char *buf
)
822 struct sensor_device_attribute
*sattr
= to_sensor_dev_attr(attr
);
823 struct nct6683_data
*data
= nct6683_update_device(dev
);
824 int index
= sattr
->index
;
826 return sprintf(buf
, "%d\n", (data
->temp_in
[index
] / 128) * 500);
830 * Temperature sensor type is determined by temperature source
831 * and can not be modified.
832 * 0x02..0x07: Thermal diode
833 * 0x08..0x18: Thermistor
834 * 0x20..0x2b: Intel PECI
835 * 0x42..0x49: AMD TSI
836 * Others are unspecified (not visible)
839 static int get_temp_type(u8 src
)
841 if (src
>= 0x02 && src
<= 0x07)
842 return 3; /* thermal diode */
843 else if (src
>= 0x08 && src
<= 0x18)
844 return 4; /* thermistor */
845 else if (src
>= 0x20 && src
<= 0x2b)
847 else if (src
>= 0x42 && src
<= 0x49)
854 show_temp_type(struct device
*dev
, struct device_attribute
*attr
, char *buf
)
856 struct nct6683_data
*data
= nct6683_update_device(dev
);
857 struct sensor_device_attribute
*sattr
= to_sensor_dev_attr(attr
);
858 int nr
= sattr
->index
;
859 return sprintf(buf
, "%d\n", get_temp_type(data
->temp_src
[nr
]));
862 static umode_t
nct6683_temp_is_visible(struct kobject
*kobj
,
863 struct attribute
*attr
, int index
)
865 struct device
*dev
= kobj_to_dev(kobj
);
866 struct nct6683_data
*data
= dev_get_drvdata(dev
);
867 int temp
= index
/ 7; /* temp index */
868 int nr
= index
% 7; /* attribute index */
871 * Intel does not have low temperature limits or temperature hysteresis
872 * registers, or at least register location and encoding is unknown.
874 if ((nr
== 2 || nr
== 4) &&
875 data
->customer_id
== NCT6683_CUSTOMER_ID_INTEL
)
878 if (nr
== 6 && get_temp_type(data
->temp_src
[temp
]) == 0)
884 SENSOR_TEMPLATE(temp_input
, "temp%d_input", S_IRUGO
, show_temp16
, NULL
, 0);
885 SENSOR_TEMPLATE(temp_label
, "temp%d_label", S_IRUGO
, show_temp_label
, NULL
, 0);
886 SENSOR_TEMPLATE_2(temp_min
, "temp%d_min", S_IRUGO
, show_temp8
, NULL
, 0, 0);
887 SENSOR_TEMPLATE_2(temp_max
, "temp%d_max", S_IRUGO
, show_temp8
, NULL
, 0, 1);
888 SENSOR_TEMPLATE(temp_max_hyst
, "temp%d_max_hyst", S_IRUGO
, show_temp_hyst
, NULL
,
890 SENSOR_TEMPLATE_2(temp_crit
, "temp%d_crit", S_IRUGO
, show_temp8
, NULL
, 0, 3);
891 SENSOR_TEMPLATE(temp_type
, "temp%d_type", S_IRUGO
, show_temp_type
, NULL
, 0);
894 * nct6683_temp_is_visible uses the index into the following array
895 * to determine if attributes should be created or not.
896 * Any change in order or content must be matched.
898 static struct sensor_device_template
*nct6683_attributes_temp_template
[] = {
899 &sensor_dev_template_temp_input
,
900 &sensor_dev_template_temp_label
,
901 &sensor_dev_template_temp_min
, /* 2 */
902 &sensor_dev_template_temp_max
, /* 3 */
903 &sensor_dev_template_temp_max_hyst
, /* 4 */
904 &sensor_dev_template_temp_crit
, /* 5 */
905 &sensor_dev_template_temp_type
, /* 6 */
909 static const struct sensor_template_group nct6683_temp_template_group
= {
910 .templates
= nct6683_attributes_temp_template
,
911 .is_visible
= nct6683_temp_is_visible
,
916 show_pwm(struct device
*dev
, struct device_attribute
*attr
, char *buf
)
918 struct nct6683_data
*data
= nct6683_update_device(dev
);
919 struct sensor_device_attribute_2
*sattr
= to_sensor_dev_attr_2(attr
);
920 int index
= sattr
->index
;
922 return sprintf(buf
, "%d\n", data
->pwm
[index
]);
926 store_pwm(struct device
*dev
, struct device_attribute
*attr
, const char *buf
,
929 struct sensor_device_attribute_2
*sattr
= to_sensor_dev_attr_2(attr
);
930 struct nct6683_data
*data
= dev_get_drvdata(dev
);
931 int index
= sattr
->index
;
934 if (kstrtoul(buf
, 10, &val
) || val
> 255)
937 mutex_lock(&data
->update_lock
);
938 nct6683_write(data
, NCT6683_REG_FAN_CFG_CTRL
, NCT6683_FAN_CFG_REQ
);
939 usleep_range(1000, 2000);
940 nct6683_write(data
, NCT6683_REG_PWM_WRITE(index
), val
);
941 nct6683_write(data
, NCT6683_REG_FAN_CFG_CTRL
, NCT6683_FAN_CFG_DONE
);
942 mutex_unlock(&data
->update_lock
);
947 SENSOR_TEMPLATE(pwm
, "pwm%d", S_IRUGO
, show_pwm
, store_pwm
, 0);
949 static umode_t
nct6683_pwm_is_visible(struct kobject
*kobj
,
950 struct attribute
*attr
, int index
)
952 struct device
*dev
= kobj_to_dev(kobj
);
953 struct nct6683_data
*data
= dev_get_drvdata(dev
);
954 int pwm
= index
; /* pwm index */
956 if (!(data
->have_pwm
& (1 << pwm
)))
959 /* Only update pwm values for Mitac boards */
960 if (data
->customer_id
== NCT6683_CUSTOMER_ID_MITAC
)
961 return attr
->mode
| S_IWUSR
;
966 static struct sensor_device_template
*nct6683_attributes_pwm_template
[] = {
967 &sensor_dev_template_pwm
,
971 static const struct sensor_template_group nct6683_pwm_template_group
= {
972 .templates
= nct6683_attributes_pwm_template
,
973 .is_visible
= nct6683_pwm_is_visible
,
978 beep_enable_show(struct device
*dev
, struct device_attribute
*attr
, char *buf
)
980 struct nct6683_data
*data
= dev_get_drvdata(dev
);
984 mutex_lock(&data
->update_lock
);
986 ret
= superio_enter(data
->sioreg
);
989 superio_select(data
->sioreg
, NCT6683_LD_HWM
);
990 reg
= superio_inb(data
->sioreg
, NCT6683_REG_CR_BEEP
);
991 superio_exit(data
->sioreg
);
993 mutex_unlock(&data
->update_lock
);
995 return sprintf(buf
, "%u\n", !!(reg
& NCT6683_CR_BEEP_MASK
));
998 mutex_unlock(&data
->update_lock
);
1003 beep_enable_store(struct device
*dev
, struct device_attribute
*attr
,
1004 const char *buf
, size_t count
)
1006 struct nct6683_data
*data
= dev_get_drvdata(dev
);
1011 if (kstrtoul(buf
, 10, &val
) || (val
!= 0 && val
!= 1))
1014 mutex_lock(&data
->update_lock
);
1016 ret
= superio_enter(data
->sioreg
);
1022 superio_select(data
->sioreg
, NCT6683_LD_HWM
);
1023 reg
= superio_inb(data
->sioreg
, NCT6683_REG_CR_BEEP
);
1025 reg
|= NCT6683_CR_BEEP_MASK
;
1027 reg
&= ~NCT6683_CR_BEEP_MASK
;
1028 superio_outb(data
->sioreg
, NCT6683_REG_CR_BEEP
, reg
);
1029 superio_exit(data
->sioreg
);
1031 mutex_unlock(&data
->update_lock
);
1035 /* Case open detection */
1038 intrusion0_alarm_show(struct device
*dev
, struct device_attribute
*attr
,
1041 struct nct6683_data
*data
= dev_get_drvdata(dev
);
1045 mutex_lock(&data
->update_lock
);
1047 ret
= superio_enter(data
->sioreg
);
1050 superio_select(data
->sioreg
, NCT6683_LD_ACPI
);
1051 reg
= superio_inb(data
->sioreg
, NCT6683_REG_CR_CASEOPEN
);
1052 superio_exit(data
->sioreg
);
1054 mutex_unlock(&data
->update_lock
);
1056 return sprintf(buf
, "%u\n", !(reg
& NCT6683_CR_CASEOPEN_MASK
));
1059 mutex_unlock(&data
->update_lock
);
1064 intrusion0_alarm_store(struct device
*dev
, struct device_attribute
*attr
,
1065 const char *buf
, size_t count
)
1067 struct nct6683_data
*data
= dev_get_drvdata(dev
);
1072 if (kstrtoul(buf
, 10, &val
) || val
!= 0)
1075 mutex_lock(&data
->update_lock
);
1078 * Use CR registers to clear caseopen status.
1079 * Caseopen is activ low, clear by writing 1 into the register.
1082 ret
= superio_enter(data
->sioreg
);
1088 superio_select(data
->sioreg
, NCT6683_LD_ACPI
);
1089 reg
= superio_inb(data
->sioreg
, NCT6683_REG_CR_CASEOPEN
);
1090 reg
|= NCT6683_CR_CASEOPEN_MASK
;
1091 superio_outb(data
->sioreg
, NCT6683_REG_CR_CASEOPEN
, reg
);
1092 reg
&= ~NCT6683_CR_CASEOPEN_MASK
;
1093 superio_outb(data
->sioreg
, NCT6683_REG_CR_CASEOPEN
, reg
);
1094 superio_exit(data
->sioreg
);
1096 data
->valid
= false; /* Force cache refresh */
1098 mutex_unlock(&data
->update_lock
);
1102 static DEVICE_ATTR_RW(intrusion0_alarm
);
1103 static DEVICE_ATTR_RW(beep_enable
);
1105 static struct attribute
*nct6683_attributes_other
[] = {
1106 &dev_attr_intrusion0_alarm
.attr
,
1107 &dev_attr_beep_enable
.attr
,
1111 static const struct attribute_group nct6683_group_other
= {
1112 .attrs
= nct6683_attributes_other
,
1115 /* Get the monitoring functions started */
1116 static inline void nct6683_init_device(struct nct6683_data
*data
)
1120 /* Start hardware monitoring if needed */
1121 tmp
= nct6683_read(data
, NCT6683_HWM_CFG
);
1123 nct6683_write(data
, NCT6683_HWM_CFG
, tmp
| 0x80);
1127 * There are a total of 24 fan inputs. Each can be configured as input
1128 * or as output. A maximum of 16 inputs and 8 outputs is configurable.
1131 nct6683_setup_fans(struct nct6683_data
*data
)
1136 for (i
= 0; i
< NCT6683_NUM_REG_FAN
; i
++) {
1137 reg
= nct6683_read(data
, NCT6683_REG_FANIN_CFG(i
));
1139 data
->have_fan
|= 1 << i
;
1140 data
->fanin_cfg
[i
] = reg
;
1142 for (i
= 0; i
< NCT6683_NUM_REG_PWM
; i
++) {
1143 reg
= nct6683_read(data
, NCT6683_REG_FANOUT_CFG(i
));
1145 data
->have_pwm
|= 1 << i
;
1146 data
->fanout_cfg
[i
] = reg
;
1151 * Translation from monitoring register to temperature and voltage attributes
1152 * ==========================================================================
1154 * There are a total of 32 monitoring registers. Each can be assigned to either
1155 * a temperature or voltage monitoring source.
1156 * NCT6683_REG_MON_CFG(x) defines assignment for each monitoring source.
1158 * Temperature and voltage attribute mapping is determined by walking through
1159 * the NCT6683_REG_MON_CFG registers. If the assigned source is
1160 * a temperature, temp_index[n] is set to the monitor register index, and
1161 * temp_src[n] is set to the temperature source. If the assigned source is
1162 * a voltage, the respective values are stored in in_index[] and in_src[],
1166 static void nct6683_setup_sensors(struct nct6683_data
*data
)
1173 for (i
= 0; i
< NCT6683_NUM_REG_MON
; i
++) {
1174 reg
= nct6683_read(data
, NCT6683_REG_MON_CFG(i
)) & 0x7f;
1175 /* Ignore invalid assignments */
1176 if (reg
>= NUM_MON_LABELS
)
1178 /* Skip if disabled or reserved */
1179 if (nct6683_mon_label
[reg
] == NULL
)
1181 if (reg
< MON_VOLTAGE_START
) {
1182 data
->temp_index
[data
->temp_num
] = i
;
1183 data
->temp_src
[data
->temp_num
] = reg
;
1186 data
->in_index
[data
->in_num
] = i
;
1187 data
->in_src
[data
->in_num
] = reg
;
1193 static int nct6683_probe(struct platform_device
*pdev
)
1195 struct device
*dev
= &pdev
->dev
;
1196 struct nct6683_sio_data
*sio_data
= dev
->platform_data
;
1197 struct attribute_group
*group
;
1198 struct nct6683_data
*data
;
1199 struct device
*hwmon_dev
;
1200 struct resource
*res
;
1204 res
= platform_get_resource(pdev
, IORESOURCE_IO
, 0);
1205 if (!devm_request_region(dev
, res
->start
, IOREGION_LENGTH
, DRVNAME
))
1208 data
= devm_kzalloc(dev
, sizeof(struct nct6683_data
), GFP_KERNEL
);
1212 data
->kind
= sio_data
->kind
;
1213 data
->sioreg
= sio_data
->sioreg
;
1214 data
->addr
= res
->start
;
1215 mutex_init(&data
->update_lock
);
1216 platform_set_drvdata(pdev
, data
);
1218 data
->customer_id
= nct6683_read16(data
, NCT6683_REG_CUSTOMER_ID
);
1220 /* By default only instantiate driver if the customer ID is known */
1221 switch (data
->customer_id
) {
1222 case NCT6683_CUSTOMER_ID_INTEL
:
1224 case NCT6683_CUSTOMER_ID_MITAC
:
1226 case NCT6683_CUSTOMER_ID_MSI
:
1233 nct6683_init_device(data
);
1234 nct6683_setup_fans(data
);
1235 nct6683_setup_sensors(data
);
1237 /* Register sysfs hooks */
1239 if (data
->have_pwm
) {
1240 group
= nct6683_create_attr_group(dev
,
1241 &nct6683_pwm_template_group
,
1242 fls(data
->have_pwm
));
1244 return PTR_ERR(group
);
1245 data
->groups
[groups
++] = group
;
1249 group
= nct6683_create_attr_group(dev
,
1250 &nct6683_in_template_group
,
1253 return PTR_ERR(group
);
1254 data
->groups
[groups
++] = group
;
1257 if (data
->have_fan
) {
1258 group
= nct6683_create_attr_group(dev
,
1259 &nct6683_fan_template_group
,
1260 fls(data
->have_fan
));
1262 return PTR_ERR(group
);
1263 data
->groups
[groups
++] = group
;
1266 if (data
->temp_num
) {
1267 group
= nct6683_create_attr_group(dev
,
1268 &nct6683_temp_template_group
,
1271 return PTR_ERR(group
);
1272 data
->groups
[groups
++] = group
;
1274 data
->groups
[groups
++] = &nct6683_group_other
;
1276 if (data
->customer_id
== NCT6683_CUSTOMER_ID_INTEL
)
1277 scnprintf(build
, sizeof(build
), "%02x/%02x/%02x",
1278 nct6683_read(data
, NCT6683_REG_BUILD_MONTH
),
1279 nct6683_read(data
, NCT6683_REG_BUILD_DAY
),
1280 nct6683_read(data
, NCT6683_REG_BUILD_YEAR
));
1282 scnprintf(build
, sizeof(build
), "%02d/%02d/%02d",
1283 nct6683_read(data
, NCT6683_REG_BUILD_MONTH
),
1284 nct6683_read(data
, NCT6683_REG_BUILD_DAY
),
1285 nct6683_read(data
, NCT6683_REG_BUILD_YEAR
));
1287 dev_info(dev
, "%s EC firmware version %d.%d build %s\n",
1288 nct6683_chip_names
[data
->kind
],
1289 nct6683_read(data
, NCT6683_REG_VERSION_HI
),
1290 nct6683_read(data
, NCT6683_REG_VERSION_LO
),
1293 hwmon_dev
= devm_hwmon_device_register_with_groups(dev
,
1294 nct6683_device_names
[data
->kind
], data
, data
->groups
);
1295 return PTR_ERR_OR_ZERO(hwmon_dev
);
1299 static int nct6683_suspend(struct device
*dev
)
1301 struct nct6683_data
*data
= nct6683_update_device(dev
);
1303 mutex_lock(&data
->update_lock
);
1304 data
->hwm_cfg
= nct6683_read(data
, NCT6683_HWM_CFG
);
1305 mutex_unlock(&data
->update_lock
);
1310 static int nct6683_resume(struct device
*dev
)
1312 struct nct6683_data
*data
= dev_get_drvdata(dev
);
1314 mutex_lock(&data
->update_lock
);
1316 nct6683_write(data
, NCT6683_HWM_CFG
, data
->hwm_cfg
);
1318 /* Force re-reading all values */
1319 data
->valid
= false;
1320 mutex_unlock(&data
->update_lock
);
1325 static const struct dev_pm_ops nct6683_dev_pm_ops
= {
1326 .suspend
= nct6683_suspend
,
1327 .resume
= nct6683_resume
,
1328 .freeze
= nct6683_suspend
,
1329 .restore
= nct6683_resume
,
1332 #define NCT6683_DEV_PM_OPS (&nct6683_dev_pm_ops)
1334 #define NCT6683_DEV_PM_OPS NULL
1335 #endif /* CONFIG_PM */
1337 static struct platform_driver nct6683_driver
= {
1340 .pm
= NCT6683_DEV_PM_OPS
,
1342 .probe
= nct6683_probe
,
1345 static int __init
nct6683_find(int sioaddr
, struct nct6683_sio_data
*sio_data
)
1351 err
= superio_enter(sioaddr
);
1355 val
= (superio_inb(sioaddr
, SIO_REG_DEVID
) << 8)
1356 | superio_inb(sioaddr
, SIO_REG_DEVID
+ 1);
1358 switch (val
& SIO_ID_MASK
) {
1359 case SIO_NCT6683_ID
:
1360 sio_data
->kind
= nct6683
;
1362 case SIO_NCT6687_ID
:
1363 sio_data
->kind
= nct6687
;
1367 pr_debug("unsupported chip ID: 0x%04x\n", val
);
1371 /* We have a known chip, find the HWM I/O address */
1372 superio_select(sioaddr
, NCT6683_LD_HWM
);
1373 val
= (superio_inb(sioaddr
, SIO_REG_ADDR
) << 8)
1374 | superio_inb(sioaddr
, SIO_REG_ADDR
+ 1);
1375 addr
= val
& IOREGION_ALIGNMENT
;
1377 pr_err("EC base I/O port unconfigured\n");
1381 /* Activate logical device if needed */
1382 val
= superio_inb(sioaddr
, SIO_REG_ENABLE
);
1383 if (!(val
& 0x01)) {
1384 pr_warn("Forcibly enabling EC access. Data may be unusable.\n");
1385 superio_outb(sioaddr
, SIO_REG_ENABLE
, val
| 0x01);
1388 superio_exit(sioaddr
);
1389 pr_info("Found %s or compatible chip at %#x:%#x\n",
1390 nct6683_chip_names
[sio_data
->kind
], sioaddr
, addr
);
1391 sio_data
->sioreg
= sioaddr
;
1396 superio_exit(sioaddr
);
1401 * when Super-I/O functions move to a separate file, the Super-I/O
1402 * bus will manage the lifetime of the device and this module will only keep
1403 * track of the nct6683 driver. But since we use platform_device_alloc(), we
1404 * must keep track of the device
1406 static struct platform_device
*pdev
[2];
1408 static int __init
sensors_nct6683_init(void)
1410 struct nct6683_sio_data sio_data
;
1411 int sioaddr
[2] = { 0x2e, 0x4e };
1412 struct resource res
;
1417 err
= platform_driver_register(&nct6683_driver
);
1422 * initialize sio_data->kind and sio_data->sioreg.
1424 * when Super-I/O functions move to a separate file, the Super-I/O
1425 * driver will probe 0x2e and 0x4e and auto-detect the presence of a
1426 * nct6683 hardware monitor, and call probe()
1428 for (i
= 0; i
< ARRAY_SIZE(pdev
); i
++) {
1429 address
= nct6683_find(sioaddr
[i
], &sio_data
);
1435 pdev
[i
] = platform_device_alloc(DRVNAME
, address
);
1438 goto exit_device_unregister
;
1441 err
= platform_device_add_data(pdev
[i
], &sio_data
,
1442 sizeof(struct nct6683_sio_data
));
1444 goto exit_device_put
;
1446 memset(&res
, 0, sizeof(res
));
1448 res
.start
= address
+ IOREGION_OFFSET
;
1449 res
.end
= address
+ IOREGION_OFFSET
+ IOREGION_LENGTH
- 1;
1450 res
.flags
= IORESOURCE_IO
;
1452 err
= acpi_check_resource_conflict(&res
);
1454 platform_device_put(pdev
[i
]);
1459 err
= platform_device_add_resources(pdev
[i
], &res
, 1);
1461 goto exit_device_put
;
1463 /* platform_device_add calls probe() */
1464 err
= platform_device_add(pdev
[i
]);
1466 goto exit_device_put
;
1470 goto exit_unregister
;
1476 platform_device_put(pdev
[i
]);
1477 exit_device_unregister
:
1480 platform_device_unregister(pdev
[i
]);
1483 platform_driver_unregister(&nct6683_driver
);
1487 static void __exit
sensors_nct6683_exit(void)
1491 for (i
= 0; i
< ARRAY_SIZE(pdev
); i
++) {
1493 platform_device_unregister(pdev
[i
]);
1495 platform_driver_unregister(&nct6683_driver
);
1498 MODULE_AUTHOR("Guenter Roeck <linux@roeck-us.net>");
1499 MODULE_DESCRIPTION("NCT6683D driver");
1500 MODULE_LICENSE("GPL");
1502 module_init(sensors_nct6683_init
);
1503 module_exit(sensors_nct6683_exit
);