1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * nct6683 - Driver for the hardware monitoring functionality of
4 * Nuvoton NCT6683D 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
17 * (1) Total number of vin and temp inputs is 32.
20 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
22 #include <linux/acpi.h>
23 #include <linux/delay.h>
24 #include <linux/err.h>
25 #include <linux/init.h>
27 #include <linux/jiffies.h>
28 #include <linux/hwmon.h>
29 #include <linux/hwmon-sysfs.h>
30 #include <linux/module.h>
31 #include <linux/mutex.h>
32 #include <linux/platform_device.h>
33 #include <linux/slab.h>
35 enum kinds
{ nct6683
};
38 module_param(force
, bool, 0);
39 MODULE_PARM_DESC(force
, "Set to one to enable support for unknown vendors");
41 static const char * const nct6683_device_names
[] = {
45 static const char * const nct6683_chip_names
[] = {
49 #define DRVNAME "nct6683"
52 * Super-I/O constants and functions
55 #define NCT6683_LD_ACPI 0x0a
56 #define NCT6683_LD_HWM 0x0b
57 #define NCT6683_LD_VID 0x0d
59 #define SIO_REG_LDSEL 0x07 /* Logical device select */
60 #define SIO_REG_DEVID 0x20 /* Device ID (2 bytes) */
61 #define SIO_REG_ENABLE 0x30 /* Logical device enable */
62 #define SIO_REG_ADDR 0x60 /* Logical device address (2 bytes) */
64 #define SIO_NCT6681_ID 0xb270 /* for later */
65 #define SIO_NCT6683_ID 0xc730
66 #define SIO_ID_MASK 0xFFF0
69 superio_outb(int ioreg
, int reg
, int val
)
76 superio_inb(int ioreg
, int reg
)
79 return inb(ioreg
+ 1);
83 superio_select(int ioreg
, int ld
)
85 outb(SIO_REG_LDSEL
, ioreg
);
90 superio_enter(int ioreg
)
93 * Try to reserve <ioreg> and <ioreg + 1> for exclusive access.
95 if (!request_muxed_region(ioreg
, 2, DRVNAME
))
105 superio_exit(int ioreg
)
109 outb(0x02, ioreg
+ 1);
110 release_region(ioreg
, 2);
117 #define IOREGION_ALIGNMENT (~7)
118 #define IOREGION_OFFSET 4 /* Use EC port 1 */
119 #define IOREGION_LENGTH 4
121 #define EC_PAGE_REG 0
122 #define EC_INDEX_REG 1
123 #define EC_DATA_REG 2
124 #define EC_EVENT_REG 3
126 /* Common and NCT6683 specific data */
128 #define NCT6683_NUM_REG_MON 32
129 #define NCT6683_NUM_REG_FAN 16
130 #define NCT6683_NUM_REG_PWM 8
132 #define NCT6683_REG_MON(x) (0x100 + (x) * 2)
133 #define NCT6683_REG_FAN_RPM(x) (0x140 + (x) * 2)
134 #define NCT6683_REG_PWM(x) (0x160 + (x))
135 #define NCT6683_REG_PWM_WRITE(x) (0xa28 + (x))
137 #define NCT6683_REG_MON_STS(x) (0x174 + (x))
138 #define NCT6683_REG_IDLE(x) (0x178 + (x))
140 #define NCT6683_REG_FAN_STS(x) (0x17c + (x))
141 #define NCT6683_REG_FAN_ERRSTS 0x17e
142 #define NCT6683_REG_FAN_INITSTS 0x17f
144 #define NCT6683_HWM_CFG 0x180
146 #define NCT6683_REG_MON_CFG(x) (0x1a0 + (x))
147 #define NCT6683_REG_FANIN_CFG(x) (0x1c0 + (x))
148 #define NCT6683_REG_FANOUT_CFG(x) (0x1d0 + (x))
150 #define NCT6683_REG_INTEL_TEMP_MAX(x) (0x901 + (x) * 16)
151 #define NCT6683_REG_INTEL_TEMP_CRIT(x) (0x90d + (x) * 16)
153 #define NCT6683_REG_TEMP_HYST(x) (0x330 + (x)) /* 8 bit */
154 #define NCT6683_REG_TEMP_MAX(x) (0x350 + (x)) /* 8 bit */
155 #define NCT6683_REG_MON_HIGH(x) (0x370 + (x) * 2) /* 8 bit */
156 #define NCT6683_REG_MON_LOW(x) (0x371 + (x) * 2) /* 8 bit */
158 #define NCT6683_REG_FAN_MIN(x) (0x3b8 + (x) * 2) /* 16 bit */
160 #define NCT6683_REG_FAN_CFG_CTRL 0xa01
161 #define NCT6683_FAN_CFG_REQ 0x80
162 #define NCT6683_FAN_CFG_DONE 0x40
164 #define NCT6683_REG_CUSTOMER_ID 0x602
165 #define NCT6683_CUSTOMER_ID_INTEL 0x805
166 #define NCT6683_CUSTOMER_ID_MITAC 0xa0e
168 #define NCT6683_REG_BUILD_YEAR 0x604
169 #define NCT6683_REG_BUILD_MONTH 0x605
170 #define NCT6683_REG_BUILD_DAY 0x606
171 #define NCT6683_REG_SERIAL 0x607
172 #define NCT6683_REG_VERSION_HI 0x608
173 #define NCT6683_REG_VERSION_LO 0x609
175 #define NCT6683_REG_CR_CASEOPEN 0xe8
176 #define NCT6683_CR_CASEOPEN_MASK (1 << 7)
178 #define NCT6683_REG_CR_BEEP 0xe0
179 #define NCT6683_CR_BEEP_MASK (1 << 6)
181 static const char *const nct6683_mon_label
[] = {
198 "Thermistor 5", /* 0x10 */
207 NULL
, NULL
, NULL
, NULL
, NULL
, NULL
, NULL
,
208 "PECI 0.0", /* 0x20 */
220 NULL
, NULL
, NULL
, NULL
,
221 "PCH CPU", /* 0x30 */
247 NULL
, NULL
, NULL
, NULL
, NULL
, NULL
,
248 "Virtual 0", /* 0x50 */
256 NULL
, NULL
, NULL
, NULL
, NULL
, NULL
, NULL
, NULL
,
257 "VCC", /* 0x60 voltage sensors */
282 #define NUM_MON_LABELS ARRAY_SIZE(nct6683_mon_label)
283 #define MON_VOLTAGE_START 0x60
285 /* ------------------------------------------------------- */
287 struct nct6683_data
{
288 int addr
; /* IO base of EC space */
289 int sioreg
; /* SIO register */
293 struct device
*hwmon_dev
;
294 const struct attribute_group
*groups
[6];
296 int temp_num
; /* number of temperature attributes */
297 u8 temp_index
[NCT6683_NUM_REG_MON
];
298 u8 temp_src
[NCT6683_NUM_REG_MON
];
300 u8 in_num
; /* number of voltage attributes */
301 u8 in_index
[NCT6683_NUM_REG_MON
];
302 u8 in_src
[NCT6683_NUM_REG_MON
];
304 struct mutex update_lock
; /* used to protect sensor updates */
305 bool valid
; /* true if following fields are valid */
306 unsigned long last_updated
; /* In jiffies */
308 /* Voltage attribute values */
309 u8 in
[3][NCT6683_NUM_REG_MON
]; /* [0]=in, [1]=in_max, [2]=in_min */
311 /* Temperature attribute values */
312 s16 temp_in
[NCT6683_NUM_REG_MON
];
313 s8 temp
[4][NCT6683_NUM_REG_MON
];/* [0]=min, [1]=max, [2]=hyst,
317 /* Fan attribute values */
318 unsigned int rpm
[NCT6683_NUM_REG_FAN
];
319 u16 fan_min
[NCT6683_NUM_REG_FAN
];
320 u8 fanin_cfg
[NCT6683_NUM_REG_FAN
];
321 u8 fanout_cfg
[NCT6683_NUM_REG_FAN
];
322 u16 have_fan
; /* some fan inputs can be disabled */
325 u8 pwm
[NCT6683_NUM_REG_PWM
];
328 /* Remember extra register values over suspend/resume */
333 struct nct6683_sio_data
{
338 struct sensor_device_template
{
339 struct device_attribute dev_attr
;
347 bool s2
; /* true if both index and nr are used */
350 struct sensor_device_attr_u
{
352 struct sensor_device_attribute a1
;
353 struct sensor_device_attribute_2 a2
;
358 #define __TEMPLATE_ATTR(_template, _mode, _show, _store) { \
359 .attr = {.name = _template, .mode = _mode }, \
364 #define SENSOR_DEVICE_TEMPLATE(_template, _mode, _show, _store, _index) \
365 { .dev_attr = __TEMPLATE_ATTR(_template, _mode, _show, _store), \
369 #define SENSOR_DEVICE_TEMPLATE_2(_template, _mode, _show, _store, \
371 { .dev_attr = __TEMPLATE_ATTR(_template, _mode, _show, _store), \
372 .u.s.index = _index, \
376 #define SENSOR_TEMPLATE(_name, _template, _mode, _show, _store, _index) \
377 static struct sensor_device_template sensor_dev_template_##_name \
378 = SENSOR_DEVICE_TEMPLATE(_template, _mode, _show, _store, \
381 #define SENSOR_TEMPLATE_2(_name, _template, _mode, _show, _store, \
383 static struct sensor_device_template sensor_dev_template_##_name \
384 = SENSOR_DEVICE_TEMPLATE_2(_template, _mode, _show, _store, \
387 struct sensor_template_group
{
388 struct sensor_device_template
**templates
;
389 umode_t (*is_visible
)(struct kobject
*, struct attribute
*, int);
393 static struct attribute_group
*
394 nct6683_create_attr_group(struct device
*dev
,
395 const struct sensor_template_group
*tg
,
398 struct sensor_device_attribute_2
*a2
;
399 struct sensor_device_attribute
*a
;
400 struct sensor_device_template
**t
;
401 struct sensor_device_attr_u
*su
;
402 struct attribute_group
*group
;
403 struct attribute
**attrs
;
407 return ERR_PTR(-EINVAL
);
410 for (count
= 0; *t
; t
++, count
++)
414 return ERR_PTR(-EINVAL
);
416 group
= devm_kzalloc(dev
, sizeof(*group
), GFP_KERNEL
);
418 return ERR_PTR(-ENOMEM
);
420 attrs
= devm_kcalloc(dev
, repeat
* count
+ 1, sizeof(*attrs
),
423 return ERR_PTR(-ENOMEM
);
425 su
= devm_kzalloc(dev
, array3_size(repeat
, count
, sizeof(*su
)),
428 return ERR_PTR(-ENOMEM
);
430 group
->attrs
= attrs
;
431 group
->is_visible
= tg
->is_visible
;
433 for (i
= 0; i
< repeat
; i
++) {
435 for (j
= 0; *t
!= NULL
; j
++) {
436 snprintf(su
->name
, sizeof(su
->name
),
437 (*t
)->dev_attr
.attr
.name
, tg
->base
+ i
);
440 sysfs_attr_init(&a2
->dev_attr
.attr
);
441 a2
->dev_attr
.attr
.name
= su
->name
;
442 a2
->nr
= (*t
)->u
.s
.nr
+ i
;
443 a2
->index
= (*t
)->u
.s
.index
;
444 a2
->dev_attr
.attr
.mode
=
445 (*t
)->dev_attr
.attr
.mode
;
446 a2
->dev_attr
.show
= (*t
)->dev_attr
.show
;
447 a2
->dev_attr
.store
= (*t
)->dev_attr
.store
;
448 *attrs
= &a2
->dev_attr
.attr
;
451 sysfs_attr_init(&a
->dev_attr
.attr
);
452 a
->dev_attr
.attr
.name
= su
->name
;
453 a
->index
= (*t
)->u
.index
+ i
;
454 a
->dev_attr
.attr
.mode
=
455 (*t
)->dev_attr
.attr
.mode
;
456 a
->dev_attr
.show
= (*t
)->dev_attr
.show
;
457 a
->dev_attr
.store
= (*t
)->dev_attr
.store
;
458 *attrs
= &a
->dev_attr
.attr
;
469 /* LSB is 16 mV, except for the following sources, where it is 32 mV */
470 #define MON_SRC_VCC 0x60
471 #define MON_SRC_VSB 0x61
472 #define MON_SRC_AVSB 0x62
473 #define MON_SRC_VBAT 0x64
475 static inline long in_from_reg(u16 reg
, u8 src
)
479 if (src
== MON_SRC_VCC
|| src
== MON_SRC_VSB
|| src
== MON_SRC_AVSB
||
485 static inline u16
in_to_reg(u32 val
, u8 src
)
489 if (src
== MON_SRC_VCC
|| src
== MON_SRC_VSB
|| src
== MON_SRC_AVSB
||
493 return clamp_val(DIV_ROUND_CLOSEST(val
, scale
), 0, 127);
496 static u16
nct6683_read(struct nct6683_data
*data
, u16 reg
)
500 outb_p(0xff, data
->addr
+ EC_PAGE_REG
); /* unlock */
501 outb_p(reg
>> 8, data
->addr
+ EC_PAGE_REG
);
502 outb_p(reg
& 0xff, data
->addr
+ EC_INDEX_REG
);
503 res
= inb_p(data
->addr
+ EC_DATA_REG
);
507 static u16
nct6683_read16(struct nct6683_data
*data
, u16 reg
)
509 return (nct6683_read(data
, reg
) << 8) | nct6683_read(data
, reg
+ 1);
512 static void nct6683_write(struct nct6683_data
*data
, u16 reg
, u16 value
)
514 outb_p(0xff, data
->addr
+ EC_PAGE_REG
); /* unlock */
515 outb_p(reg
>> 8, data
->addr
+ EC_PAGE_REG
);
516 outb_p(reg
& 0xff, data
->addr
+ EC_INDEX_REG
);
517 outb_p(value
& 0xff, data
->addr
+ EC_DATA_REG
);
520 static int get_in_reg(struct nct6683_data
*data
, int nr
, int index
)
522 int ch
= data
->in_index
[index
];
527 reg
= NCT6683_REG_MON(ch
);
530 if (data
->customer_id
!= NCT6683_CUSTOMER_ID_INTEL
)
531 reg
= NCT6683_REG_MON_LOW(ch
);
534 if (data
->customer_id
!= NCT6683_CUSTOMER_ID_INTEL
)
535 reg
= NCT6683_REG_MON_HIGH(ch
);
543 static int get_temp_reg(struct nct6683_data
*data
, int nr
, int index
)
545 int ch
= data
->temp_index
[index
];
548 switch (data
->customer_id
) {
549 case NCT6683_CUSTOMER_ID_INTEL
:
553 reg
= NCT6683_REG_INTEL_TEMP_MAX(ch
);
556 reg
= NCT6683_REG_INTEL_TEMP_CRIT(ch
);
560 case NCT6683_CUSTOMER_ID_MITAC
:
565 reg
= NCT6683_REG_MON_LOW(ch
);
568 reg
= NCT6683_REG_TEMP_MAX(ch
);
571 reg
= NCT6683_REG_TEMP_HYST(ch
);
574 reg
= NCT6683_REG_MON_HIGH(ch
);
582 static void nct6683_update_pwm(struct device
*dev
)
584 struct nct6683_data
*data
= dev_get_drvdata(dev
);
587 for (i
= 0; i
< NCT6683_NUM_REG_PWM
; i
++) {
588 if (!(data
->have_pwm
& (1 << i
)))
590 data
->pwm
[i
] = nct6683_read(data
, NCT6683_REG_PWM(i
));
594 static struct nct6683_data
*nct6683_update_device(struct device
*dev
)
596 struct nct6683_data
*data
= dev_get_drvdata(dev
);
599 mutex_lock(&data
->update_lock
);
601 if (time_after(jiffies
, data
->last_updated
+ HZ
) || !data
->valid
) {
602 /* Measured voltages and limits */
603 for (i
= 0; i
< data
->in_num
; i
++) {
604 for (j
= 0; j
< 3; j
++) {
605 int reg
= get_in_reg(data
, j
, i
);
609 nct6683_read(data
, reg
);
613 /* Measured temperatures and limits */
614 for (i
= 0; i
< data
->temp_num
; i
++) {
615 u8 ch
= data
->temp_index
[i
];
617 data
->temp_in
[i
] = nct6683_read16(data
,
618 NCT6683_REG_MON(ch
));
619 for (j
= 0; j
< 4; j
++) {
620 int reg
= get_temp_reg(data
, j
, i
);
624 nct6683_read(data
, reg
);
628 /* Measured fan speeds and limits */
629 for (i
= 0; i
< ARRAY_SIZE(data
->rpm
); i
++) {
630 if (!(data
->have_fan
& (1 << i
)))
633 data
->rpm
[i
] = nct6683_read16(data
,
634 NCT6683_REG_FAN_RPM(i
));
635 data
->fan_min
[i
] = nct6683_read16(data
,
636 NCT6683_REG_FAN_MIN(i
));
639 nct6683_update_pwm(dev
);
641 data
->last_updated
= jiffies
;
645 mutex_unlock(&data
->update_lock
);
650 * Sysfs callback functions
653 show_in_label(struct device
*dev
, struct device_attribute
*attr
, char *buf
)
655 struct sensor_device_attribute
*sattr
= to_sensor_dev_attr(attr
);
656 struct nct6683_data
*data
= nct6683_update_device(dev
);
657 int nr
= sattr
->index
;
659 return sprintf(buf
, "%s\n", nct6683_mon_label
[data
->in_src
[nr
]]);
663 show_in_reg(struct device
*dev
, struct device_attribute
*attr
, char *buf
)
665 struct sensor_device_attribute_2
*sattr
= to_sensor_dev_attr_2(attr
);
666 struct nct6683_data
*data
= nct6683_update_device(dev
);
667 int index
= sattr
->index
;
670 return sprintf(buf
, "%ld\n",
671 in_from_reg(data
->in
[index
][nr
], data
->in_index
[index
]));
674 static umode_t
nct6683_in_is_visible(struct kobject
*kobj
,
675 struct attribute
*attr
, int index
)
677 struct device
*dev
= container_of(kobj
, struct device
, kobj
);
678 struct nct6683_data
*data
= dev_get_drvdata(dev
);
679 int nr
= index
% 4; /* attribute */
682 * Voltage limits exist for Intel boards,
683 * but register location and encoding is unknown
685 if ((nr
== 2 || nr
== 3) &&
686 data
->customer_id
== NCT6683_CUSTOMER_ID_INTEL
)
692 SENSOR_TEMPLATE(in_label
, "in%d_label", S_IRUGO
, show_in_label
, NULL
, 0);
693 SENSOR_TEMPLATE_2(in_input
, "in%d_input", S_IRUGO
, show_in_reg
, NULL
, 0, 0);
694 SENSOR_TEMPLATE_2(in_min
, "in%d_min", S_IRUGO
, show_in_reg
, NULL
, 0, 1);
695 SENSOR_TEMPLATE_2(in_max
, "in%d_max", S_IRUGO
, show_in_reg
, NULL
, 0, 2);
697 static struct sensor_device_template
*nct6683_attributes_in_template
[] = {
698 &sensor_dev_template_in_label
,
699 &sensor_dev_template_in_input
,
700 &sensor_dev_template_in_min
,
701 &sensor_dev_template_in_max
,
705 static const struct sensor_template_group nct6683_in_template_group
= {
706 .templates
= nct6683_attributes_in_template
,
707 .is_visible
= nct6683_in_is_visible
,
711 show_fan(struct device
*dev
, struct device_attribute
*attr
, char *buf
)
713 struct sensor_device_attribute
*sattr
= to_sensor_dev_attr(attr
);
714 struct nct6683_data
*data
= nct6683_update_device(dev
);
716 return sprintf(buf
, "%d\n", data
->rpm
[sattr
->index
]);
720 show_fan_min(struct device
*dev
, struct device_attribute
*attr
, char *buf
)
722 struct nct6683_data
*data
= nct6683_update_device(dev
);
723 struct sensor_device_attribute
*sattr
= to_sensor_dev_attr(attr
);
724 int nr
= sattr
->index
;
726 return sprintf(buf
, "%d\n", data
->fan_min
[nr
]);
730 show_fan_pulses(struct device
*dev
, struct device_attribute
*attr
, char *buf
)
732 struct sensor_device_attribute
*sattr
= to_sensor_dev_attr(attr
);
733 struct nct6683_data
*data
= nct6683_update_device(dev
);
735 return sprintf(buf
, "%d\n",
736 ((data
->fanin_cfg
[sattr
->index
] >> 5) & 0x03) + 1);
739 static umode_t
nct6683_fan_is_visible(struct kobject
*kobj
,
740 struct attribute
*attr
, int index
)
742 struct device
*dev
= container_of(kobj
, struct device
, kobj
);
743 struct nct6683_data
*data
= dev_get_drvdata(dev
);
744 int fan
= index
/ 3; /* fan index */
745 int nr
= index
% 3; /* attribute index */
747 if (!(data
->have_fan
& (1 << fan
)))
751 * Intel may have minimum fan speed limits,
752 * but register location and encoding are unknown.
754 if (nr
== 2 && data
->customer_id
== NCT6683_CUSTOMER_ID_INTEL
)
760 SENSOR_TEMPLATE(fan_input
, "fan%d_input", S_IRUGO
, show_fan
, NULL
, 0);
761 SENSOR_TEMPLATE(fan_pulses
, "fan%d_pulses", S_IRUGO
, show_fan_pulses
, NULL
, 0);
762 SENSOR_TEMPLATE(fan_min
, "fan%d_min", S_IRUGO
, show_fan_min
, NULL
, 0);
765 * nct6683_fan_is_visible uses the index into the following array
766 * to determine if attributes should be created or not.
767 * Any change in order or content must be matched.
769 static struct sensor_device_template
*nct6683_attributes_fan_template
[] = {
770 &sensor_dev_template_fan_input
,
771 &sensor_dev_template_fan_pulses
,
772 &sensor_dev_template_fan_min
,
776 static const struct sensor_template_group nct6683_fan_template_group
= {
777 .templates
= nct6683_attributes_fan_template
,
778 .is_visible
= nct6683_fan_is_visible
,
783 show_temp_label(struct device
*dev
, struct device_attribute
*attr
, char *buf
)
785 struct sensor_device_attribute
*sattr
= to_sensor_dev_attr(attr
);
786 struct nct6683_data
*data
= nct6683_update_device(dev
);
787 int nr
= sattr
->index
;
789 return sprintf(buf
, "%s\n", nct6683_mon_label
[data
->temp_src
[nr
]]);
793 show_temp8(struct device
*dev
, struct device_attribute
*attr
, char *buf
)
795 struct sensor_device_attribute_2
*sattr
= to_sensor_dev_attr_2(attr
);
796 struct nct6683_data
*data
= nct6683_update_device(dev
);
797 int index
= sattr
->index
;
800 return sprintf(buf
, "%d\n", data
->temp
[index
][nr
] * 1000);
804 show_temp_hyst(struct device
*dev
, struct device_attribute
*attr
, char *buf
)
806 struct sensor_device_attribute
*sattr
= to_sensor_dev_attr(attr
);
807 struct nct6683_data
*data
= nct6683_update_device(dev
);
808 int nr
= sattr
->index
;
809 int temp
= data
->temp
[1][nr
] - data
->temp
[2][nr
];
811 return sprintf(buf
, "%d\n", temp
* 1000);
815 show_temp16(struct device
*dev
, struct device_attribute
*attr
, char *buf
)
817 struct sensor_device_attribute
*sattr
= to_sensor_dev_attr(attr
);
818 struct nct6683_data
*data
= nct6683_update_device(dev
);
819 int index
= sattr
->index
;
821 return sprintf(buf
, "%d\n", (data
->temp_in
[index
] / 128) * 500);
825 * Temperature sensor type is determined by temperature source
826 * and can not be modified.
827 * 0x02..0x07: Thermal diode
828 * 0x08..0x18: Thermistor
829 * 0x20..0x2b: Intel PECI
830 * 0x42..0x49: AMD TSI
831 * Others are unspecified (not visible)
834 static int get_temp_type(u8 src
)
836 if (src
>= 0x02 && src
<= 0x07)
837 return 3; /* thermal diode */
838 else if (src
>= 0x08 && src
<= 0x18)
839 return 4; /* thermistor */
840 else if (src
>= 0x20 && src
<= 0x2b)
842 else if (src
>= 0x42 && src
<= 0x49)
849 show_temp_type(struct device
*dev
, struct device_attribute
*attr
, char *buf
)
851 struct nct6683_data
*data
= nct6683_update_device(dev
);
852 struct sensor_device_attribute
*sattr
= to_sensor_dev_attr(attr
);
853 int nr
= sattr
->index
;
854 return sprintf(buf
, "%d\n", get_temp_type(data
->temp_src
[nr
]));
857 static umode_t
nct6683_temp_is_visible(struct kobject
*kobj
,
858 struct attribute
*attr
, int index
)
860 struct device
*dev
= container_of(kobj
, struct device
, kobj
);
861 struct nct6683_data
*data
= dev_get_drvdata(dev
);
862 int temp
= index
/ 7; /* temp index */
863 int nr
= index
% 7; /* attribute index */
866 * Intel does not have low temperature limits or temperature hysteresis
867 * registers, or at least register location and encoding is unknown.
869 if ((nr
== 2 || nr
== 4) &&
870 data
->customer_id
== NCT6683_CUSTOMER_ID_INTEL
)
873 if (nr
== 6 && get_temp_type(data
->temp_src
[temp
]) == 0)
879 SENSOR_TEMPLATE(temp_input
, "temp%d_input", S_IRUGO
, show_temp16
, NULL
, 0);
880 SENSOR_TEMPLATE(temp_label
, "temp%d_label", S_IRUGO
, show_temp_label
, NULL
, 0);
881 SENSOR_TEMPLATE_2(temp_min
, "temp%d_min", S_IRUGO
, show_temp8
, NULL
, 0, 0);
882 SENSOR_TEMPLATE_2(temp_max
, "temp%d_max", S_IRUGO
, show_temp8
, NULL
, 0, 1);
883 SENSOR_TEMPLATE(temp_max_hyst
, "temp%d_max_hyst", S_IRUGO
, show_temp_hyst
, NULL
,
885 SENSOR_TEMPLATE_2(temp_crit
, "temp%d_crit", S_IRUGO
, show_temp8
, NULL
, 0, 3);
886 SENSOR_TEMPLATE(temp_type
, "temp%d_type", S_IRUGO
, show_temp_type
, NULL
, 0);
889 * nct6683_temp_is_visible uses the index into the following array
890 * to determine if attributes should be created or not.
891 * Any change in order or content must be matched.
893 static struct sensor_device_template
*nct6683_attributes_temp_template
[] = {
894 &sensor_dev_template_temp_input
,
895 &sensor_dev_template_temp_label
,
896 &sensor_dev_template_temp_min
, /* 2 */
897 &sensor_dev_template_temp_max
, /* 3 */
898 &sensor_dev_template_temp_max_hyst
, /* 4 */
899 &sensor_dev_template_temp_crit
, /* 5 */
900 &sensor_dev_template_temp_type
, /* 6 */
904 static const struct sensor_template_group nct6683_temp_template_group
= {
905 .templates
= nct6683_attributes_temp_template
,
906 .is_visible
= nct6683_temp_is_visible
,
911 show_pwm(struct device
*dev
, struct device_attribute
*attr
, char *buf
)
913 struct nct6683_data
*data
= nct6683_update_device(dev
);
914 struct sensor_device_attribute_2
*sattr
= to_sensor_dev_attr_2(attr
);
915 int index
= sattr
->index
;
917 return sprintf(buf
, "%d\n", data
->pwm
[index
]);
921 store_pwm(struct device
*dev
, struct device_attribute
*attr
, const char *buf
,
924 struct sensor_device_attribute_2
*sattr
= to_sensor_dev_attr_2(attr
);
925 struct nct6683_data
*data
= dev_get_drvdata(dev
);
926 int index
= sattr
->index
;
929 if (kstrtoul(buf
, 10, &val
) || val
> 255)
932 mutex_lock(&data
->update_lock
);
933 nct6683_write(data
, NCT6683_REG_FAN_CFG_CTRL
, NCT6683_FAN_CFG_REQ
);
934 usleep_range(1000, 2000);
935 nct6683_write(data
, NCT6683_REG_PWM_WRITE(index
), val
);
936 nct6683_write(data
, NCT6683_REG_FAN_CFG_CTRL
, NCT6683_FAN_CFG_DONE
);
937 mutex_unlock(&data
->update_lock
);
942 SENSOR_TEMPLATE(pwm
, "pwm%d", S_IRUGO
, show_pwm
, store_pwm
, 0);
944 static umode_t
nct6683_pwm_is_visible(struct kobject
*kobj
,
945 struct attribute
*attr
, int index
)
947 struct device
*dev
= container_of(kobj
, struct device
, kobj
);
948 struct nct6683_data
*data
= dev_get_drvdata(dev
);
949 int pwm
= index
; /* pwm index */
951 if (!(data
->have_pwm
& (1 << pwm
)))
954 /* Only update pwm values for Mitac boards */
955 if (data
->customer_id
== NCT6683_CUSTOMER_ID_MITAC
)
956 return attr
->mode
| S_IWUSR
;
961 static struct sensor_device_template
*nct6683_attributes_pwm_template
[] = {
962 &sensor_dev_template_pwm
,
966 static const struct sensor_template_group nct6683_pwm_template_group
= {
967 .templates
= nct6683_attributes_pwm_template
,
968 .is_visible
= nct6683_pwm_is_visible
,
973 beep_enable_show(struct device
*dev
, struct device_attribute
*attr
, char *buf
)
975 struct nct6683_data
*data
= dev_get_drvdata(dev
);
979 mutex_lock(&data
->update_lock
);
981 ret
= superio_enter(data
->sioreg
);
984 superio_select(data
->sioreg
, NCT6683_LD_HWM
);
985 reg
= superio_inb(data
->sioreg
, NCT6683_REG_CR_BEEP
);
986 superio_exit(data
->sioreg
);
988 mutex_unlock(&data
->update_lock
);
990 return sprintf(buf
, "%u\n", !!(reg
& NCT6683_CR_BEEP_MASK
));
993 mutex_unlock(&data
->update_lock
);
998 beep_enable_store(struct device
*dev
, struct device_attribute
*attr
,
999 const char *buf
, size_t count
)
1001 struct nct6683_data
*data
= dev_get_drvdata(dev
);
1006 if (kstrtoul(buf
, 10, &val
) || (val
!= 0 && val
!= 1))
1009 mutex_lock(&data
->update_lock
);
1011 ret
= superio_enter(data
->sioreg
);
1017 superio_select(data
->sioreg
, NCT6683_LD_HWM
);
1018 reg
= superio_inb(data
->sioreg
, NCT6683_REG_CR_BEEP
);
1020 reg
|= NCT6683_CR_BEEP_MASK
;
1022 reg
&= ~NCT6683_CR_BEEP_MASK
;
1023 superio_outb(data
->sioreg
, NCT6683_REG_CR_BEEP
, reg
);
1024 superio_exit(data
->sioreg
);
1026 mutex_unlock(&data
->update_lock
);
1030 /* Case open detection */
1033 intrusion0_alarm_show(struct device
*dev
, struct device_attribute
*attr
,
1036 struct nct6683_data
*data
= dev_get_drvdata(dev
);
1040 mutex_lock(&data
->update_lock
);
1042 ret
= superio_enter(data
->sioreg
);
1045 superio_select(data
->sioreg
, NCT6683_LD_ACPI
);
1046 reg
= superio_inb(data
->sioreg
, NCT6683_REG_CR_CASEOPEN
);
1047 superio_exit(data
->sioreg
);
1049 mutex_unlock(&data
->update_lock
);
1051 return sprintf(buf
, "%u\n", !(reg
& NCT6683_CR_CASEOPEN_MASK
));
1054 mutex_unlock(&data
->update_lock
);
1059 intrusion0_alarm_store(struct device
*dev
, struct device_attribute
*attr
,
1060 const char *buf
, size_t count
)
1062 struct nct6683_data
*data
= dev_get_drvdata(dev
);
1067 if (kstrtoul(buf
, 10, &val
) || val
!= 0)
1070 mutex_lock(&data
->update_lock
);
1073 * Use CR registers to clear caseopen status.
1074 * Caseopen is activ low, clear by writing 1 into the register.
1077 ret
= superio_enter(data
->sioreg
);
1083 superio_select(data
->sioreg
, NCT6683_LD_ACPI
);
1084 reg
= superio_inb(data
->sioreg
, NCT6683_REG_CR_CASEOPEN
);
1085 reg
|= NCT6683_CR_CASEOPEN_MASK
;
1086 superio_outb(data
->sioreg
, NCT6683_REG_CR_CASEOPEN
, reg
);
1087 reg
&= ~NCT6683_CR_CASEOPEN_MASK
;
1088 superio_outb(data
->sioreg
, NCT6683_REG_CR_CASEOPEN
, reg
);
1089 superio_exit(data
->sioreg
);
1091 data
->valid
= false; /* Force cache refresh */
1093 mutex_unlock(&data
->update_lock
);
1097 static DEVICE_ATTR_RW(intrusion0_alarm
);
1098 static DEVICE_ATTR_RW(beep_enable
);
1100 static struct attribute
*nct6683_attributes_other
[] = {
1101 &dev_attr_intrusion0_alarm
.attr
,
1102 &dev_attr_beep_enable
.attr
,
1106 static const struct attribute_group nct6683_group_other
= {
1107 .attrs
= nct6683_attributes_other
,
1110 /* Get the monitoring functions started */
1111 static inline void nct6683_init_device(struct nct6683_data
*data
)
1115 /* Start hardware monitoring if needed */
1116 tmp
= nct6683_read(data
, NCT6683_HWM_CFG
);
1118 nct6683_write(data
, NCT6683_HWM_CFG
, tmp
| 0x80);
1122 * There are a total of 24 fan inputs. Each can be configured as input
1123 * or as output. A maximum of 16 inputs and 8 outputs is configurable.
1126 nct6683_setup_fans(struct nct6683_data
*data
)
1131 for (i
= 0; i
< NCT6683_NUM_REG_FAN
; i
++) {
1132 reg
= nct6683_read(data
, NCT6683_REG_FANIN_CFG(i
));
1134 data
->have_fan
|= 1 << i
;
1135 data
->fanin_cfg
[i
] = reg
;
1137 for (i
= 0; i
< NCT6683_NUM_REG_PWM
; i
++) {
1138 reg
= nct6683_read(data
, NCT6683_REG_FANOUT_CFG(i
));
1140 data
->have_pwm
|= 1 << i
;
1141 data
->fanout_cfg
[i
] = reg
;
1146 * Translation from monitoring register to temperature and voltage attributes
1147 * ==========================================================================
1149 * There are a total of 32 monitoring registers. Each can be assigned to either
1150 * a temperature or voltage monitoring source.
1151 * NCT6683_REG_MON_CFG(x) defines assignment for each monitoring source.
1153 * Temperature and voltage attribute mapping is determined by walking through
1154 * the NCT6683_REG_MON_CFG registers. If the assigned source is
1155 * a temperature, temp_index[n] is set to the monitor register index, and
1156 * temp_src[n] is set to the temperature source. If the assigned source is
1157 * a voltage, the respective values are stored in in_index[] and in_src[],
1161 static void nct6683_setup_sensors(struct nct6683_data
*data
)
1168 for (i
= 0; i
< NCT6683_NUM_REG_MON
; i
++) {
1169 reg
= nct6683_read(data
, NCT6683_REG_MON_CFG(i
)) & 0x7f;
1170 /* Ignore invalid assignments */
1171 if (reg
>= NUM_MON_LABELS
)
1173 /* Skip if disabled or reserved */
1174 if (nct6683_mon_label
[reg
] == NULL
)
1176 if (reg
< MON_VOLTAGE_START
) {
1177 data
->temp_index
[data
->temp_num
] = i
;
1178 data
->temp_src
[data
->temp_num
] = reg
;
1181 data
->in_index
[data
->in_num
] = i
;
1182 data
->in_src
[data
->in_num
] = reg
;
1188 static int nct6683_probe(struct platform_device
*pdev
)
1190 struct device
*dev
= &pdev
->dev
;
1191 struct nct6683_sio_data
*sio_data
= dev
->platform_data
;
1192 struct attribute_group
*group
;
1193 struct nct6683_data
*data
;
1194 struct device
*hwmon_dev
;
1195 struct resource
*res
;
1199 res
= platform_get_resource(pdev
, IORESOURCE_IO
, 0);
1200 if (!devm_request_region(dev
, res
->start
, IOREGION_LENGTH
, DRVNAME
))
1203 data
= devm_kzalloc(dev
, sizeof(struct nct6683_data
), GFP_KERNEL
);
1207 data
->kind
= sio_data
->kind
;
1208 data
->sioreg
= sio_data
->sioreg
;
1209 data
->addr
= res
->start
;
1210 mutex_init(&data
->update_lock
);
1211 platform_set_drvdata(pdev
, data
);
1213 data
->customer_id
= nct6683_read16(data
, NCT6683_REG_CUSTOMER_ID
);
1215 /* By default only instantiate driver if the customer ID is known */
1216 switch (data
->customer_id
) {
1217 case NCT6683_CUSTOMER_ID_INTEL
:
1219 case NCT6683_CUSTOMER_ID_MITAC
:
1226 nct6683_init_device(data
);
1227 nct6683_setup_fans(data
);
1228 nct6683_setup_sensors(data
);
1230 /* Register sysfs hooks */
1232 if (data
->have_pwm
) {
1233 group
= nct6683_create_attr_group(dev
,
1234 &nct6683_pwm_template_group
,
1235 fls(data
->have_pwm
));
1237 return PTR_ERR(group
);
1238 data
->groups
[groups
++] = group
;
1242 group
= nct6683_create_attr_group(dev
,
1243 &nct6683_in_template_group
,
1246 return PTR_ERR(group
);
1247 data
->groups
[groups
++] = group
;
1250 if (data
->have_fan
) {
1251 group
= nct6683_create_attr_group(dev
,
1252 &nct6683_fan_template_group
,
1253 fls(data
->have_fan
));
1255 return PTR_ERR(group
);
1256 data
->groups
[groups
++] = group
;
1259 if (data
->temp_num
) {
1260 group
= nct6683_create_attr_group(dev
,
1261 &nct6683_temp_template_group
,
1264 return PTR_ERR(group
);
1265 data
->groups
[groups
++] = group
;
1267 data
->groups
[groups
++] = &nct6683_group_other
;
1269 if (data
->customer_id
== NCT6683_CUSTOMER_ID_INTEL
)
1270 scnprintf(build
, sizeof(build
), "%02x/%02x/%02x",
1271 nct6683_read(data
, NCT6683_REG_BUILD_MONTH
),
1272 nct6683_read(data
, NCT6683_REG_BUILD_DAY
),
1273 nct6683_read(data
, NCT6683_REG_BUILD_YEAR
));
1275 scnprintf(build
, sizeof(build
), "%02d/%02d/%02d",
1276 nct6683_read(data
, NCT6683_REG_BUILD_MONTH
),
1277 nct6683_read(data
, NCT6683_REG_BUILD_DAY
),
1278 nct6683_read(data
, NCT6683_REG_BUILD_YEAR
));
1280 dev_info(dev
, "%s EC firmware version %d.%d build %s\n",
1281 nct6683_chip_names
[data
->kind
],
1282 nct6683_read(data
, NCT6683_REG_VERSION_HI
),
1283 nct6683_read(data
, NCT6683_REG_VERSION_LO
),
1286 hwmon_dev
= devm_hwmon_device_register_with_groups(dev
,
1287 nct6683_device_names
[data
->kind
], data
, data
->groups
);
1288 return PTR_ERR_OR_ZERO(hwmon_dev
);
1292 static int nct6683_suspend(struct device
*dev
)
1294 struct nct6683_data
*data
= nct6683_update_device(dev
);
1296 mutex_lock(&data
->update_lock
);
1297 data
->hwm_cfg
= nct6683_read(data
, NCT6683_HWM_CFG
);
1298 mutex_unlock(&data
->update_lock
);
1303 static int nct6683_resume(struct device
*dev
)
1305 struct nct6683_data
*data
= dev_get_drvdata(dev
);
1307 mutex_lock(&data
->update_lock
);
1309 nct6683_write(data
, NCT6683_HWM_CFG
, data
->hwm_cfg
);
1311 /* Force re-reading all values */
1312 data
->valid
= false;
1313 mutex_unlock(&data
->update_lock
);
1318 static const struct dev_pm_ops nct6683_dev_pm_ops
= {
1319 .suspend
= nct6683_suspend
,
1320 .resume
= nct6683_resume
,
1321 .freeze
= nct6683_suspend
,
1322 .restore
= nct6683_resume
,
1325 #define NCT6683_DEV_PM_OPS (&nct6683_dev_pm_ops)
1327 #define NCT6683_DEV_PM_OPS NULL
1328 #endif /* CONFIG_PM */
1330 static struct platform_driver nct6683_driver
= {
1333 .pm
= NCT6683_DEV_PM_OPS
,
1335 .probe
= nct6683_probe
,
1338 static int __init
nct6683_find(int sioaddr
, struct nct6683_sio_data
*sio_data
)
1344 err
= superio_enter(sioaddr
);
1348 val
= (superio_inb(sioaddr
, SIO_REG_DEVID
) << 8)
1349 | superio_inb(sioaddr
, SIO_REG_DEVID
+ 1);
1351 switch (val
& SIO_ID_MASK
) {
1352 case SIO_NCT6683_ID
:
1353 sio_data
->kind
= nct6683
;
1357 pr_debug("unsupported chip ID: 0x%04x\n", val
);
1361 /* We have a known chip, find the HWM I/O address */
1362 superio_select(sioaddr
, NCT6683_LD_HWM
);
1363 val
= (superio_inb(sioaddr
, SIO_REG_ADDR
) << 8)
1364 | superio_inb(sioaddr
, SIO_REG_ADDR
+ 1);
1365 addr
= val
& IOREGION_ALIGNMENT
;
1367 pr_err("EC base I/O port unconfigured\n");
1371 /* Activate logical device if needed */
1372 val
= superio_inb(sioaddr
, SIO_REG_ENABLE
);
1373 if (!(val
& 0x01)) {
1374 pr_warn("Forcibly enabling EC access. Data may be unusable.\n");
1375 superio_outb(sioaddr
, SIO_REG_ENABLE
, val
| 0x01);
1378 superio_exit(sioaddr
);
1379 pr_info("Found %s or compatible chip at %#x:%#x\n",
1380 nct6683_chip_names
[sio_data
->kind
], sioaddr
, addr
);
1381 sio_data
->sioreg
= sioaddr
;
1386 superio_exit(sioaddr
);
1391 * when Super-I/O functions move to a separate file, the Super-I/O
1392 * bus will manage the lifetime of the device and this module will only keep
1393 * track of the nct6683 driver. But since we use platform_device_alloc(), we
1394 * must keep track of the device
1396 static struct platform_device
*pdev
[2];
1398 static int __init
sensors_nct6683_init(void)
1400 struct nct6683_sio_data sio_data
;
1401 int sioaddr
[2] = { 0x2e, 0x4e };
1402 struct resource res
;
1407 err
= platform_driver_register(&nct6683_driver
);
1412 * initialize sio_data->kind and sio_data->sioreg.
1414 * when Super-I/O functions move to a separate file, the Super-I/O
1415 * driver will probe 0x2e and 0x4e and auto-detect the presence of a
1416 * nct6683 hardware monitor, and call probe()
1418 for (i
= 0; i
< ARRAY_SIZE(pdev
); i
++) {
1419 address
= nct6683_find(sioaddr
[i
], &sio_data
);
1425 pdev
[i
] = platform_device_alloc(DRVNAME
, address
);
1428 goto exit_device_unregister
;
1431 err
= platform_device_add_data(pdev
[i
], &sio_data
,
1432 sizeof(struct nct6683_sio_data
));
1434 goto exit_device_put
;
1436 memset(&res
, 0, sizeof(res
));
1438 res
.start
= address
+ IOREGION_OFFSET
;
1439 res
.end
= address
+ IOREGION_OFFSET
+ IOREGION_LENGTH
- 1;
1440 res
.flags
= IORESOURCE_IO
;
1442 err
= acpi_check_resource_conflict(&res
);
1444 platform_device_put(pdev
[i
]);
1449 err
= platform_device_add_resources(pdev
[i
], &res
, 1);
1451 goto exit_device_put
;
1453 /* platform_device_add calls probe() */
1454 err
= platform_device_add(pdev
[i
]);
1456 goto exit_device_put
;
1460 goto exit_unregister
;
1466 platform_device_put(pdev
[i
]);
1467 exit_device_unregister
:
1470 platform_device_unregister(pdev
[i
]);
1473 platform_driver_unregister(&nct6683_driver
);
1477 static void __exit
sensors_nct6683_exit(void)
1481 for (i
= 0; i
< ARRAY_SIZE(pdev
); i
++) {
1483 platform_device_unregister(pdev
[i
]);
1485 platform_driver_unregister(&nct6683_driver
);
1488 MODULE_AUTHOR("Guenter Roeck <linux@roeck-us.net>");
1489 MODULE_DESCRIPTION("NCT6683D driver");
1490 MODULE_LICENSE("GPL");
1492 module_init(sensors_nct6683_init
);
1493 module_exit(sensors_nct6683_exit
);