2 * nct6775 - Driver for the hardware monitoring functionality of
3 * Nuvoton NCT677x Super-I/O chips
5 * Copyright (C) 2012 Guenter Roeck <linux@roeck-us.net>
7 * Derived from w83627ehf driver
8 * Copyright (C) 2005-2012 Jean Delvare <khali@linux-fr.org>
9 * Copyright (C) 2006 Yuan Mu (Winbond),
10 * Rudolf Marek <r.marek@assembler.cz>
11 * David Hubbard <david.c.hubbard@gmail.com>
12 * Daniel J Blueman <daniel.blueman@gmail.com>
13 * Copyright (C) 2010 Sheng-Yuan Huang (Nuvoton) (PS00)
15 * Shamelessly ripped from the w83627hf driver
16 * Copyright (C) 2003 Mark Studebaker
18 * This program is free software; you can redistribute it and/or modify
19 * it under the terms of the GNU General Public License as published by
20 * the Free Software Foundation; either version 2 of the License, or
21 * (at your option) any later version.
23 * This program is distributed in the hope that it will be useful,
24 * but WITHOUT ANY WARRANTY; without even the implied warranty of
25 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
26 * GNU General Public License for more details.
28 * You should have received a copy of the GNU General Public License
29 * along with this program; if not, write to the Free Software
30 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
33 * Supports the following chips:
35 * Chip #vin #fan #pwm #temp chip IDs man ID
36 * nct6775f 9 4 3 6+3 0xb470 0xc1 0x5ca3
37 * nct6776f 9 5 3 6+3 0xc330 0xc1 0x5ca3
38 * nct6779d 15 5 5 2+6 0xc560 0xc1 0x5ca3
40 * #temp lists the number of monitored temperature sources (first value) plus
41 * the number of directly connectable temperature sensors (second value).
44 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
46 #include <linux/module.h>
47 #include <linux/init.h>
48 #include <linux/slab.h>
49 #include <linux/jiffies.h>
50 #include <linux/platform_device.h>
51 #include <linux/hwmon.h>
52 #include <linux/hwmon-sysfs.h>
53 #include <linux/hwmon-vid.h>
54 #include <linux/err.h>
55 #include <linux/mutex.h>
56 #include <linux/acpi.h>
62 enum kinds { nct6775, nct6776, nct6779 };
64 /* used to set data->name = nct6775_device_names[data->sio_kind] */
65 static const char * const nct6775_device_names[] = {
71 static unsigned short force_id;
72 module_param(force_id, ushort, 0);
73 MODULE_PARM_DESC(force_id, "Override the detected device ID");
75 #define DRVNAME "nct6775"
78 * Super-I/O constants and functions
81 #define NCT6775_LD_ACPI 0x0a
82 #define NCT6775_LD_HWM 0x0b
83 #define NCT6775_LD_VID 0x0d
85 #define SIO_REG_LDSEL 0x07 /* Logical device select */
86 #define SIO_REG_DEVID 0x20 /* Device ID (2 bytes) */
87 #define SIO_REG_ENABLE 0x30 /* Logical device enable */
88 #define SIO_REG_ADDR 0x60 /* Logical device address (2 bytes) */
90 #define SIO_NCT6775_ID 0xb470
91 #define SIO_NCT6776_ID 0xc330
92 #define SIO_NCT6779_ID 0xc560
93 #define SIO_ID_MASK 0xFFF0
96 superio_outb(int ioreg, int reg, int val)
103 superio_inb(int ioreg, int reg)
106 return inb(ioreg + 1);
110 superio_select(int ioreg, int ld)
112 outb(SIO_REG_LDSEL, ioreg);
117 superio_enter(int ioreg)
120 * Try to reserve <ioreg> and <ioreg + 1> for exclusive access.
122 if (!request_muxed_region(ioreg, 2, DRVNAME))
132 superio_exit(int ioreg)
136 outb(0x02, ioreg + 1);
137 release_region(ioreg, 2);
144 #define IOREGION_ALIGNMENT (~7)
145 #define IOREGION_OFFSET 5
146 #define IOREGION_LENGTH 2
147 #define ADDR_REG_OFFSET 0
148 #define DATA_REG_OFFSET 1
150 #define NCT6775_REG_BANK 0x4E
151 #define NCT6775_REG_CONFIG 0x40
154 * Not currently used:
155 * REG_MAN_ID has the value 0x5ca3 for all supported chips.
156 * REG_CHIP_ID == 0x88/0xa1/0xc1 depending on chip model.
157 * REG_MAN_ID is at port 0x4f
158 * REG_CHIP_ID is at port 0x58
161 #define NUM_TEMP 10 /* Max number of temp attribute sets w/ limits*/
162 #define NUM_TEMP_FIXED 6 /* Max number of fixed temp attribute sets */
164 #define NUM_REG_ALARM 4 /* Max number of alarm registers */
166 /* Common and NCT6775 specific data */
168 /* Voltage min/max registers for nr=7..14 are in bank 5 */
170 static const u16 NCT6775_REG_IN_MAX[] = {
171 0x2b, 0x2d, 0x2f, 0x31, 0x33, 0x35, 0x37, 0x554, 0x556, 0x558, 0x55a,
172 0x55c, 0x55e, 0x560, 0x562 };
173 static const u16 NCT6775_REG_IN_MIN[] = {
174 0x2c, 0x2e, 0x30, 0x32, 0x34, 0x36, 0x38, 0x555, 0x557, 0x559, 0x55b,
175 0x55d, 0x55f, 0x561, 0x563 };
176 static const u16 NCT6775_REG_IN[] = {
177 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x550, 0x551, 0x552
180 #define NCT6775_REG_VBAT 0x5D
181 #define NCT6775_REG_DIODE 0x5E
183 static const u16 NCT6775_REG_ALARM[NUM_REG_ALARM] = { 0x459, 0x45A, 0x45B };
185 /* 0..15 voltages, 16..23 fans, 24..31 temperatures */
187 static const s8 NCT6775_ALARM_BITS[] = {
188 0, 1, 2, 3, 8, 21, 20, 16, /* in0.. in7 */
189 17, -1, -1, -1, -1, -1, -1, /* in8..in14 */
191 6, 7, 11, 10, 23, /* fan1..fan5 */
192 -1, -1, -1, /* unused */
193 4, 5, 13, -1, -1, -1, /* temp1..temp6 */
194 12, -1 }; /* intrusion0, intrusion1 */
196 #define TEMP_ALARM_BASE 24
197 #define INTRUSION_ALARM_BASE 30
199 static const u8 NCT6775_REG_CR_CASEOPEN_CLR[] = { 0xe6, 0xee };
200 static const u8 NCT6775_CR_CASEOPEN_CLR_MASK[] = { 0x20, 0x01 };
202 static const u16 NCT6775_REG_TEMP[] = {
203 0x27, 0x150, 0x250, 0x62b, 0x62c, 0x62d };
205 static const u16 NCT6775_REG_TEMP_CONFIG[ARRAY_SIZE(NCT6775_REG_TEMP)] = {
206 0, 0x152, 0x252, 0x628, 0x629, 0x62A };
207 static const u16 NCT6775_REG_TEMP_HYST[ARRAY_SIZE(NCT6775_REG_TEMP)] = {
208 0x3a, 0x153, 0x253, 0x673, 0x678, 0x67D };
209 static const u16 NCT6775_REG_TEMP_OVER[ARRAY_SIZE(NCT6775_REG_TEMP)] = {
210 0x39, 0x155, 0x255, 0x672, 0x677, 0x67C };
212 static const u16 NCT6775_REG_TEMP_SOURCE[ARRAY_SIZE(NCT6775_REG_TEMP)] = {
213 0x621, 0x622, 0x623, 0x624, 0x625, 0x626 };
215 static const u16 NCT6775_REG_TEMP_OFFSET[] = { 0x454, 0x455, 0x456 };
217 static const char *const nct6775_temp_label[] = {
231 "PCH_CHIP_CPU_MAX_TEMP",
241 static const u16 NCT6775_REG_TEMP_ALTERNATE[ARRAY_SIZE(nct6775_temp_label) - 1]
242 = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0x661, 0x662, 0x664 };
244 static const u16 NCT6775_REG_TEMP_CRIT[ARRAY_SIZE(nct6775_temp_label) - 1]
245 = { 0, 0, 0, 0, 0xa00, 0xa01, 0xa02, 0xa03, 0xa04, 0xa05, 0xa06,
248 /* NCT6776 specific data */
250 static const s8 NCT6776_ALARM_BITS[] = {
251 0, 1, 2, 3, 8, 21, 20, 16, /* in0.. in7 */
252 17, -1, -1, -1, -1, -1, -1, /* in8..in14 */
254 6, 7, 11, 10, 23, /* fan1..fan5 */
255 -1, -1, -1, /* unused */
256 4, 5, 13, -1, -1, -1, /* temp1..temp6 */
257 12, 9 }; /* intrusion0, intrusion1 */
259 static const u16 NCT6776_REG_TEMP_CONFIG[ARRAY_SIZE(NCT6775_REG_TEMP)] = {
260 0x18, 0x152, 0x252, 0x628, 0x629, 0x62A };
262 static const char *const nct6776_temp_label[] = {
277 "PCH_CHIP_CPU_MAX_TEMP",
288 static const u16 NCT6776_REG_TEMP_ALTERNATE[ARRAY_SIZE(nct6776_temp_label) - 1]
289 = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0x401, 0x402, 0x404 };
291 static const u16 NCT6776_REG_TEMP_CRIT[ARRAY_SIZE(nct6776_temp_label) - 1]
292 = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0x709, 0x70a };
294 /* NCT6779 specific data */
296 static const u16 NCT6779_REG_IN[] = {
297 0x480, 0x481, 0x482, 0x483, 0x484, 0x485, 0x486, 0x487,
298 0x488, 0x489, 0x48a, 0x48b, 0x48c, 0x48d, 0x48e };
300 static const u16 NCT6779_REG_ALARM[NUM_REG_ALARM] = {
301 0x459, 0x45A, 0x45B, 0x568 };
303 static const s8 NCT6779_ALARM_BITS[] = {
304 0, 1, 2, 3, 8, 21, 20, 16, /* in0.. in7 */
305 17, 24, 25, 26, 27, 28, 29, /* in8..in14 */
307 6, 7, 11, 10, 23, /* fan1..fan5 */
308 -1, -1, -1, /* unused */
309 4, 5, 13, -1, -1, -1, /* temp1..temp6 */
310 12, 9 }; /* intrusion0, intrusion1 */
312 static const u16 NCT6779_REG_TEMP[] = { 0x27, 0x150 };
313 static const u16 NCT6779_REG_TEMP_CONFIG[ARRAY_SIZE(NCT6779_REG_TEMP)] = {
315 static const u16 NCT6779_REG_TEMP_HYST[ARRAY_SIZE(NCT6779_REG_TEMP)] = {
317 static const u16 NCT6779_REG_TEMP_OVER[ARRAY_SIZE(NCT6779_REG_TEMP)] = {
320 static const u16 NCT6779_REG_TEMP_OFFSET[] = {
321 0x454, 0x455, 0x456, 0x44a, 0x44b, 0x44c };
323 static const char *const nct6779_temp_label[] = {
342 "PCH_CHIP_CPU_MAX_TEMP",
353 static const u16 NCT6779_REG_TEMP_ALTERNATE[ARRAY_SIZE(nct6779_temp_label) - 1]
354 = { 0x490, 0x491, 0x492, 0x493, 0x494, 0x495, 0, 0,
355 0, 0, 0, 0, 0, 0, 0, 0,
356 0, 0x400, 0x401, 0x402, 0x404, 0x405, 0x406, 0x407,
359 static const u16 NCT6779_REG_TEMP_CRIT[ARRAY_SIZE(nct6779_temp_label) - 1]
360 = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0x709, 0x70a };
367 * Some of the voltage inputs have internal scaling, the tables below
368 * contain 8 (the ADC LSB in mV) * scaling factor * 100
370 static const u16 scale_in[15] = {
371 800, 800, 1600, 1600, 800, 800, 800, 1600, 1600, 800, 800, 800, 800,
375 static inline long in_from_reg(u8 reg, u8 nr)
377 return DIV_ROUND_CLOSEST(reg * scale_in[nr], 100);
380 static inline u8 in_to_reg(u32 val, u8 nr)
382 return clamp_val(DIV_ROUND_CLOSEST(val * 100, scale_in[nr]), 0, 255);
386 * Data structures and manipulation thereof
389 struct nct6775_data {
390 int addr; /* IO base of hw monitor block */
394 struct device *hwmon_dev;
397 u16 reg_temp[4][NUM_TEMP]; /* 0=temp, 1=temp_over, 2=temp_hyst,
400 u8 temp_src[NUM_TEMP];
401 u16 reg_temp_config[NUM_TEMP];
402 const char * const *temp_label;
409 const s8 *ALARM_BITS;
412 const u16 *REG_IN_MINMAX[2];
414 const u16 *REG_TEMP_SOURCE; /* temp register sources */
416 const u16 *REG_TEMP_OFFSET;
418 const u16 *REG_ALARM;
420 struct mutex update_lock;
421 bool valid; /* true if following fields are valid */
422 unsigned long last_updated; /* In jiffies */
424 /* Register values */
425 u8 bank; /* current register bank */
426 u8 in_num; /* number of in inputs we have */
427 u8 in[15][3]; /* [0]=in, [1]=in_max, [2]=in_min */
429 u8 temp_fixed_num; /* 3 or 6 */
430 u8 temp_type[NUM_TEMP_FIXED];
431 s8 temp_offset[NUM_TEMP_FIXED];
432 s16 temp[4][NUM_TEMP]; /* 0=temp, 1=temp_over, 2=temp_hyst,
444 struct nct6775_sio_data {
449 static bool is_word_sized(struct nct6775_data *data, u16 reg)
451 switch (data->kind) {
453 return (((reg & 0xff00) == 0x100 ||
454 (reg & 0xff00) == 0x200) &&
455 ((reg & 0x00ff) == 0x50 ||
456 (reg & 0x00ff) == 0x53 ||
457 (reg & 0x00ff) == 0x55)) ||
458 (reg & 0xfff0) == 0x630 ||
459 reg == 0x640 || reg == 0x642 ||
461 ((reg & 0xfff0) == 0x650 && (reg & 0x000f) >= 0x06) ||
462 reg == 0x73 || reg == 0x75 || reg == 0x77;
464 return (((reg & 0xff00) == 0x100 ||
465 (reg & 0xff00) == 0x200) &&
466 ((reg & 0x00ff) == 0x50 ||
467 (reg & 0x00ff) == 0x53 ||
468 (reg & 0x00ff) == 0x55)) ||
469 (reg & 0xfff0) == 0x630 ||
471 reg == 0x640 || reg == 0x642 ||
472 ((reg & 0xfff0) == 0x650 && (reg & 0x000f) >= 0x06) ||
473 reg == 0x73 || reg == 0x75 || reg == 0x77;
475 return reg == 0x150 || reg == 0x153 || reg == 0x155 ||
476 ((reg & 0xfff0) == 0x4b0 && (reg & 0x000f) < 0x09) ||
478 reg == 0x63a || reg == 0x63c || reg == 0x63e ||
479 reg == 0x640 || reg == 0x642 ||
480 reg == 0x73 || reg == 0x75 || reg == 0x77 || reg == 0x79 ||
487 * On older chips, only registers 0x50-0x5f are banked.
488 * On more recent chips, all registers are banked.
489 * Assume that is the case and set the bank number for each access.
490 * Cache the bank number so it only needs to be set if it changes.
492 static inline void nct6775_set_bank(struct nct6775_data *data, u16 reg)
495 if (data->bank != bank) {
496 outb_p(NCT6775_REG_BANK, data->addr + ADDR_REG_OFFSET);
497 outb_p(bank, data->addr + DATA_REG_OFFSET);
502 static u16 nct6775_read_value(struct nct6775_data *data, u16 reg)
504 int res, word_sized = is_word_sized(data, reg);
506 mutex_lock(&data->lock);
508 nct6775_set_bank(data, reg);
509 outb_p(reg & 0xff, data->addr + ADDR_REG_OFFSET);
510 res = inb_p(data->addr + DATA_REG_OFFSET);
512 outb_p((reg & 0xff) + 1,
513 data->addr + ADDR_REG_OFFSET);
514 res = (res << 8) + inb_p(data->addr + DATA_REG_OFFSET);
517 mutex_unlock(&data->lock);
521 static int nct6775_write_value(struct nct6775_data *data, u16 reg, u16 value)
523 int word_sized = is_word_sized(data, reg);
525 mutex_lock(&data->lock);
527 nct6775_set_bank(data, reg);
528 outb_p(reg & 0xff, data->addr + ADDR_REG_OFFSET);
530 outb_p(value >> 8, data->addr + DATA_REG_OFFSET);
531 outb_p((reg & 0xff) + 1,
532 data->addr + ADDR_REG_OFFSET);
534 outb_p(value & 0xff, data->addr + DATA_REG_OFFSET);
536 mutex_unlock(&data->lock);
540 /* We left-align 8-bit temperature values to make the code simpler */
541 static u16 nct6775_read_temp(struct nct6775_data *data, u16 reg)
545 res = nct6775_read_value(data, reg);
546 if (!is_word_sized(data, reg))
552 static int nct6775_write_temp(struct nct6775_data *data, u16 reg, u16 value)
554 if (!is_word_sized(data, reg))
556 return nct6775_write_value(data, reg, value);
559 static struct nct6775_data *nct6775_update_device(struct device *dev)
561 struct nct6775_data *data = dev_get_drvdata(dev);
564 mutex_lock(&data->update_lock);
566 if (time_after(jiffies, data->last_updated + HZ + HZ/2)
568 /* Measured voltages and limits */
569 for (i = 0; i < data->in_num; i++) {
570 if (!(data->have_in & (1 << i)))
573 data->in[i][0] = nct6775_read_value(data,
575 data->in[i][1] = nct6775_read_value(data,
576 data->REG_IN_MINMAX[0][i]);
577 data->in[i][2] = nct6775_read_value(data,
578 data->REG_IN_MINMAX[1][i]);
581 /* Measured temperatures and limits */
582 for (i = 0; i < NUM_TEMP; i++) {
583 if (!(data->have_temp & (1 << i)))
585 for (j = 0; j < 4; j++) {
586 if (data->reg_temp[j][i])
588 = nct6775_read_temp(data,
589 data->reg_temp[j][i]);
591 if (!(data->have_temp_fixed & (1 << i)))
594 = nct6775_read_value(data, data->REG_TEMP_OFFSET[i]);
598 for (i = 0; i < NUM_REG_ALARM; i++) {
600 if (!data->REG_ALARM[i])
602 alarm = nct6775_read_value(data, data->REG_ALARM[i]);
603 data->alarms |= ((u64)alarm) << (i << 3);
606 data->last_updated = jiffies;
610 mutex_unlock(&data->update_lock);
615 * Sysfs callback functions
618 show_in_reg(struct device *dev, struct device_attribute *attr, char *buf)
620 struct nct6775_data *data = nct6775_update_device(dev);
621 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
623 int index = sattr->index;
624 return sprintf(buf, "%ld\n", in_from_reg(data->in[nr][index], nr));
628 store_in_reg(struct device *dev, struct device_attribute *attr, const char *buf,
631 struct nct6775_data *data = dev_get_drvdata(dev);
632 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
634 int index = sattr->index;
636 int err = kstrtoul(buf, 10, &val);
639 mutex_lock(&data->update_lock);
640 data->in[nr][index] = in_to_reg(val, nr);
641 nct6775_write_value(data, data->REG_IN_MINMAX[index-1][nr],
642 data->in[nr][index]);
643 mutex_unlock(&data->update_lock);
648 show_alarm(struct device *dev, struct device_attribute *attr, char *buf)
650 struct nct6775_data *data = nct6775_update_device(dev);
651 struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr);
652 int nr = data->ALARM_BITS[sattr->index];
653 return sprintf(buf, "%u\n",
654 (unsigned int)((data->alarms >> nr) & 0x01));
657 static SENSOR_DEVICE_ATTR_2(in0_input, S_IRUGO, show_in_reg, NULL, 0, 0);
658 static SENSOR_DEVICE_ATTR_2(in1_input, S_IRUGO, show_in_reg, NULL, 1, 0);
659 static SENSOR_DEVICE_ATTR_2(in2_input, S_IRUGO, show_in_reg, NULL, 2, 0);
660 static SENSOR_DEVICE_ATTR_2(in3_input, S_IRUGO, show_in_reg, NULL, 3, 0);
661 static SENSOR_DEVICE_ATTR_2(in4_input, S_IRUGO, show_in_reg, NULL, 4, 0);
662 static SENSOR_DEVICE_ATTR_2(in5_input, S_IRUGO, show_in_reg, NULL, 5, 0);
663 static SENSOR_DEVICE_ATTR_2(in6_input, S_IRUGO, show_in_reg, NULL, 6, 0);
664 static SENSOR_DEVICE_ATTR_2(in7_input, S_IRUGO, show_in_reg, NULL, 7, 0);
665 static SENSOR_DEVICE_ATTR_2(in8_input, S_IRUGO, show_in_reg, NULL, 8, 0);
666 static SENSOR_DEVICE_ATTR_2(in9_input, S_IRUGO, show_in_reg, NULL, 9, 0);
667 static SENSOR_DEVICE_ATTR_2(in10_input, S_IRUGO, show_in_reg, NULL, 10, 0);
668 static SENSOR_DEVICE_ATTR_2(in11_input, S_IRUGO, show_in_reg, NULL, 11, 0);
669 static SENSOR_DEVICE_ATTR_2(in12_input, S_IRUGO, show_in_reg, NULL, 12, 0);
670 static SENSOR_DEVICE_ATTR_2(in13_input, S_IRUGO, show_in_reg, NULL, 13, 0);
671 static SENSOR_DEVICE_ATTR_2(in14_input, S_IRUGO, show_in_reg, NULL, 14, 0);
673 static SENSOR_DEVICE_ATTR(in0_alarm, S_IRUGO, show_alarm, NULL, 0);
674 static SENSOR_DEVICE_ATTR(in1_alarm, S_IRUGO, show_alarm, NULL, 1);
675 static SENSOR_DEVICE_ATTR(in2_alarm, S_IRUGO, show_alarm, NULL, 2);
676 static SENSOR_DEVICE_ATTR(in3_alarm, S_IRUGO, show_alarm, NULL, 3);
677 static SENSOR_DEVICE_ATTR(in4_alarm, S_IRUGO, show_alarm, NULL, 4);
678 static SENSOR_DEVICE_ATTR(in5_alarm, S_IRUGO, show_alarm, NULL, 5);
679 static SENSOR_DEVICE_ATTR(in6_alarm, S_IRUGO, show_alarm, NULL, 6);
680 static SENSOR_DEVICE_ATTR(in7_alarm, S_IRUGO, show_alarm, NULL, 7);
681 static SENSOR_DEVICE_ATTR(in8_alarm, S_IRUGO, show_alarm, NULL, 8);
682 static SENSOR_DEVICE_ATTR(in9_alarm, S_IRUGO, show_alarm, NULL, 9);
683 static SENSOR_DEVICE_ATTR(in10_alarm, S_IRUGO, show_alarm, NULL, 10);
684 static SENSOR_DEVICE_ATTR(in11_alarm, S_IRUGO, show_alarm, NULL, 11);
685 static SENSOR_DEVICE_ATTR(in12_alarm, S_IRUGO, show_alarm, NULL, 12);
686 static SENSOR_DEVICE_ATTR(in13_alarm, S_IRUGO, show_alarm, NULL, 13);
687 static SENSOR_DEVICE_ATTR(in14_alarm, S_IRUGO, show_alarm, NULL, 14);
689 static SENSOR_DEVICE_ATTR_2(in0_min, S_IWUSR | S_IRUGO, show_in_reg,
691 static SENSOR_DEVICE_ATTR_2(in1_min, S_IWUSR | S_IRUGO, show_in_reg,
693 static SENSOR_DEVICE_ATTR_2(in2_min, S_IWUSR | S_IRUGO, show_in_reg,
695 static SENSOR_DEVICE_ATTR_2(in3_min, S_IWUSR | S_IRUGO, show_in_reg,
697 static SENSOR_DEVICE_ATTR_2(in4_min, S_IWUSR | S_IRUGO, show_in_reg,
699 static SENSOR_DEVICE_ATTR_2(in5_min, S_IWUSR | S_IRUGO, show_in_reg,
701 static SENSOR_DEVICE_ATTR_2(in6_min, S_IWUSR | S_IRUGO, show_in_reg,
703 static SENSOR_DEVICE_ATTR_2(in7_min, S_IWUSR | S_IRUGO, show_in_reg,
705 static SENSOR_DEVICE_ATTR_2(in8_min, S_IWUSR | S_IRUGO, show_in_reg,
707 static SENSOR_DEVICE_ATTR_2(in9_min, S_IWUSR | S_IRUGO, show_in_reg,
709 static SENSOR_DEVICE_ATTR_2(in10_min, S_IWUSR | S_IRUGO, show_in_reg,
710 store_in_reg, 10, 1);
711 static SENSOR_DEVICE_ATTR_2(in11_min, S_IWUSR | S_IRUGO, show_in_reg,
712 store_in_reg, 11, 1);
713 static SENSOR_DEVICE_ATTR_2(in12_min, S_IWUSR | S_IRUGO, show_in_reg,
714 store_in_reg, 12, 1);
715 static SENSOR_DEVICE_ATTR_2(in13_min, S_IWUSR | S_IRUGO, show_in_reg,
716 store_in_reg, 13, 1);
717 static SENSOR_DEVICE_ATTR_2(in14_min, S_IWUSR | S_IRUGO, show_in_reg,
718 store_in_reg, 14, 1);
720 static SENSOR_DEVICE_ATTR_2(in0_max, S_IWUSR | S_IRUGO, show_in_reg,
722 static SENSOR_DEVICE_ATTR_2(in1_max, S_IWUSR | S_IRUGO, show_in_reg,
724 static SENSOR_DEVICE_ATTR_2(in2_max, S_IWUSR | S_IRUGO, show_in_reg,
726 static SENSOR_DEVICE_ATTR_2(in3_max, S_IWUSR | S_IRUGO, show_in_reg,
728 static SENSOR_DEVICE_ATTR_2(in4_max, S_IWUSR | S_IRUGO, show_in_reg,
730 static SENSOR_DEVICE_ATTR_2(in5_max, S_IWUSR | S_IRUGO, show_in_reg,
732 static SENSOR_DEVICE_ATTR_2(in6_max, S_IWUSR | S_IRUGO, show_in_reg,
734 static SENSOR_DEVICE_ATTR_2(in7_max, S_IWUSR | S_IRUGO, show_in_reg,
736 static SENSOR_DEVICE_ATTR_2(in8_max, S_IWUSR | S_IRUGO, show_in_reg,
738 static SENSOR_DEVICE_ATTR_2(in9_max, S_IWUSR | S_IRUGO, show_in_reg,
740 static SENSOR_DEVICE_ATTR_2(in10_max, S_IWUSR | S_IRUGO, show_in_reg,
741 store_in_reg, 10, 2);
742 static SENSOR_DEVICE_ATTR_2(in11_max, S_IWUSR | S_IRUGO, show_in_reg,
743 store_in_reg, 11, 2);
744 static SENSOR_DEVICE_ATTR_2(in12_max, S_IWUSR | S_IRUGO, show_in_reg,
745 store_in_reg, 12, 2);
746 static SENSOR_DEVICE_ATTR_2(in13_max, S_IWUSR | S_IRUGO, show_in_reg,
747 store_in_reg, 13, 2);
748 static SENSOR_DEVICE_ATTR_2(in14_max, S_IWUSR | S_IRUGO, show_in_reg,
749 store_in_reg, 14, 2);
751 static struct attribute *nct6775_attributes_in[15][5] = {
753 &sensor_dev_attr_in0_input.dev_attr.attr,
754 &sensor_dev_attr_in0_min.dev_attr.attr,
755 &sensor_dev_attr_in0_max.dev_attr.attr,
756 &sensor_dev_attr_in0_alarm.dev_attr.attr,
760 &sensor_dev_attr_in1_input.dev_attr.attr,
761 &sensor_dev_attr_in1_min.dev_attr.attr,
762 &sensor_dev_attr_in1_max.dev_attr.attr,
763 &sensor_dev_attr_in1_alarm.dev_attr.attr,
767 &sensor_dev_attr_in2_input.dev_attr.attr,
768 &sensor_dev_attr_in2_min.dev_attr.attr,
769 &sensor_dev_attr_in2_max.dev_attr.attr,
770 &sensor_dev_attr_in2_alarm.dev_attr.attr,
774 &sensor_dev_attr_in3_input.dev_attr.attr,
775 &sensor_dev_attr_in3_min.dev_attr.attr,
776 &sensor_dev_attr_in3_max.dev_attr.attr,
777 &sensor_dev_attr_in3_alarm.dev_attr.attr,
781 &sensor_dev_attr_in4_input.dev_attr.attr,
782 &sensor_dev_attr_in4_min.dev_attr.attr,
783 &sensor_dev_attr_in4_max.dev_attr.attr,
784 &sensor_dev_attr_in4_alarm.dev_attr.attr,
788 &sensor_dev_attr_in5_input.dev_attr.attr,
789 &sensor_dev_attr_in5_min.dev_attr.attr,
790 &sensor_dev_attr_in5_max.dev_attr.attr,
791 &sensor_dev_attr_in5_alarm.dev_attr.attr,
795 &sensor_dev_attr_in6_input.dev_attr.attr,
796 &sensor_dev_attr_in6_min.dev_attr.attr,
797 &sensor_dev_attr_in6_max.dev_attr.attr,
798 &sensor_dev_attr_in6_alarm.dev_attr.attr,
802 &sensor_dev_attr_in7_input.dev_attr.attr,
803 &sensor_dev_attr_in7_min.dev_attr.attr,
804 &sensor_dev_attr_in7_max.dev_attr.attr,
805 &sensor_dev_attr_in7_alarm.dev_attr.attr,
809 &sensor_dev_attr_in8_input.dev_attr.attr,
810 &sensor_dev_attr_in8_min.dev_attr.attr,
811 &sensor_dev_attr_in8_max.dev_attr.attr,
812 &sensor_dev_attr_in8_alarm.dev_attr.attr,
816 &sensor_dev_attr_in9_input.dev_attr.attr,
817 &sensor_dev_attr_in9_min.dev_attr.attr,
818 &sensor_dev_attr_in9_max.dev_attr.attr,
819 &sensor_dev_attr_in9_alarm.dev_attr.attr,
823 &sensor_dev_attr_in10_input.dev_attr.attr,
824 &sensor_dev_attr_in10_min.dev_attr.attr,
825 &sensor_dev_attr_in10_max.dev_attr.attr,
826 &sensor_dev_attr_in10_alarm.dev_attr.attr,
830 &sensor_dev_attr_in11_input.dev_attr.attr,
831 &sensor_dev_attr_in11_min.dev_attr.attr,
832 &sensor_dev_attr_in11_max.dev_attr.attr,
833 &sensor_dev_attr_in11_alarm.dev_attr.attr,
837 &sensor_dev_attr_in12_input.dev_attr.attr,
838 &sensor_dev_attr_in12_min.dev_attr.attr,
839 &sensor_dev_attr_in12_max.dev_attr.attr,
840 &sensor_dev_attr_in12_alarm.dev_attr.attr,
844 &sensor_dev_attr_in13_input.dev_attr.attr,
845 &sensor_dev_attr_in13_min.dev_attr.attr,
846 &sensor_dev_attr_in13_max.dev_attr.attr,
847 &sensor_dev_attr_in13_alarm.dev_attr.attr,
851 &sensor_dev_attr_in14_input.dev_attr.attr,
852 &sensor_dev_attr_in14_min.dev_attr.attr,
853 &sensor_dev_attr_in14_max.dev_attr.attr,
854 &sensor_dev_attr_in14_alarm.dev_attr.attr,
859 static const struct attribute_group nct6775_group_in[15] = {
860 { .attrs = nct6775_attributes_in[0] },
861 { .attrs = nct6775_attributes_in[1] },
862 { .attrs = nct6775_attributes_in[2] },
863 { .attrs = nct6775_attributes_in[3] },
864 { .attrs = nct6775_attributes_in[4] },
865 { .attrs = nct6775_attributes_in[5] },
866 { .attrs = nct6775_attributes_in[6] },
867 { .attrs = nct6775_attributes_in[7] },
868 { .attrs = nct6775_attributes_in[8] },
869 { .attrs = nct6775_attributes_in[9] },
870 { .attrs = nct6775_attributes_in[10] },
871 { .attrs = nct6775_attributes_in[11] },
872 { .attrs = nct6775_attributes_in[12] },
873 { .attrs = nct6775_attributes_in[13] },
874 { .attrs = nct6775_attributes_in[14] },
878 show_temp_label(struct device *dev, struct device_attribute *attr, char *buf)
880 struct nct6775_data *data = nct6775_update_device(dev);
881 struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr);
882 int nr = sattr->index;
883 return sprintf(buf, "%s\n", data->temp_label[data->temp_src[nr]]);
887 show_temp(struct device *dev, struct device_attribute *attr, char *buf)
889 struct nct6775_data *data = nct6775_update_device(dev);
890 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
892 int index = sattr->index;
894 return sprintf(buf, "%d\n", LM75_TEMP_FROM_REG(data->temp[index][nr]));
898 store_temp(struct device *dev, struct device_attribute *attr, const char *buf,
901 struct nct6775_data *data = dev_get_drvdata(dev);
902 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
904 int index = sattr->index;
908 err = kstrtol(buf, 10, &val);
912 mutex_lock(&data->update_lock);
913 data->temp[index][nr] = LM75_TEMP_TO_REG(val);
914 nct6775_write_temp(data, data->reg_temp[index][nr],
915 data->temp[index][nr]);
916 mutex_unlock(&data->update_lock);
921 show_temp_offset(struct device *dev, struct device_attribute *attr, char *buf)
923 struct nct6775_data *data = nct6775_update_device(dev);
924 struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr);
926 return sprintf(buf, "%d\n", data->temp_offset[sattr->index] * 1000);
930 store_temp_offset(struct device *dev, struct device_attribute *attr,
931 const char *buf, size_t count)
933 struct nct6775_data *data = dev_get_drvdata(dev);
934 struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr);
935 int nr = sattr->index;
939 err = kstrtol(buf, 10, &val);
943 val = clamp_val(DIV_ROUND_CLOSEST(val, 1000), -128, 127);
945 mutex_lock(&data->update_lock);
946 data->temp_offset[nr] = val;
947 nct6775_write_value(data, data->REG_TEMP_OFFSET[nr], val);
948 mutex_unlock(&data->update_lock);
954 show_temp_type(struct device *dev, struct device_attribute *attr, char *buf)
956 struct nct6775_data *data = nct6775_update_device(dev);
957 struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr);
958 int nr = sattr->index;
959 return sprintf(buf, "%d\n", (int)data->temp_type[nr]);
963 store_temp_type(struct device *dev, struct device_attribute *attr,
964 const char *buf, size_t count)
966 struct nct6775_data *data = nct6775_update_device(dev);
967 struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr);
968 int nr = sattr->index;
973 err = kstrtoul(buf, 10, &val);
977 if (val != 1 && val != 3 && val != 4)
980 mutex_lock(&data->update_lock);
982 data->temp_type[nr] = val;
983 vbat = nct6775_read_value(data, data->REG_VBAT) & ~(0x02 << nr);
984 diode = nct6775_read_value(data, data->REG_DIODE) & ~(0x02 << nr);
987 case 1: /* CPU diode (diode, current mode) */
991 case 3: /* diode, voltage mode */
994 case 4: /* thermistor */
997 nct6775_write_value(data, data->REG_VBAT, vbat);
998 nct6775_write_value(data, data->REG_DIODE, diode);
1000 mutex_unlock(&data->update_lock);
1004 static struct sensor_device_attribute_2 sda_temp_input[] = {
1005 SENSOR_ATTR_2(temp1_input, S_IRUGO, show_temp, NULL, 0, 0),
1006 SENSOR_ATTR_2(temp2_input, S_IRUGO, show_temp, NULL, 1, 0),
1007 SENSOR_ATTR_2(temp3_input, S_IRUGO, show_temp, NULL, 2, 0),
1008 SENSOR_ATTR_2(temp4_input, S_IRUGO, show_temp, NULL, 3, 0),
1009 SENSOR_ATTR_2(temp5_input, S_IRUGO, show_temp, NULL, 4, 0),
1010 SENSOR_ATTR_2(temp6_input, S_IRUGO, show_temp, NULL, 5, 0),
1011 SENSOR_ATTR_2(temp7_input, S_IRUGO, show_temp, NULL, 6, 0),
1012 SENSOR_ATTR_2(temp8_input, S_IRUGO, show_temp, NULL, 7, 0),
1013 SENSOR_ATTR_2(temp9_input, S_IRUGO, show_temp, NULL, 8, 0),
1014 SENSOR_ATTR_2(temp10_input, S_IRUGO, show_temp, NULL, 9, 0),
1017 static struct sensor_device_attribute sda_temp_label[] = {
1018 SENSOR_ATTR(temp1_label, S_IRUGO, show_temp_label, NULL, 0),
1019 SENSOR_ATTR(temp2_label, S_IRUGO, show_temp_label, NULL, 1),
1020 SENSOR_ATTR(temp3_label, S_IRUGO, show_temp_label, NULL, 2),
1021 SENSOR_ATTR(temp4_label, S_IRUGO, show_temp_label, NULL, 3),
1022 SENSOR_ATTR(temp5_label, S_IRUGO, show_temp_label, NULL, 4),
1023 SENSOR_ATTR(temp6_label, S_IRUGO, show_temp_label, NULL, 5),
1024 SENSOR_ATTR(temp7_label, S_IRUGO, show_temp_label, NULL, 6),
1025 SENSOR_ATTR(temp8_label, S_IRUGO, show_temp_label, NULL, 7),
1026 SENSOR_ATTR(temp9_label, S_IRUGO, show_temp_label, NULL, 8),
1027 SENSOR_ATTR(temp10_label, S_IRUGO, show_temp_label, NULL, 9),
1030 static struct sensor_device_attribute_2 sda_temp_max[] = {
1031 SENSOR_ATTR_2(temp1_max, S_IRUGO | S_IWUSR, show_temp, store_temp,
1033 SENSOR_ATTR_2(temp2_max, S_IRUGO | S_IWUSR, show_temp, store_temp,
1035 SENSOR_ATTR_2(temp3_max, S_IRUGO | S_IWUSR, show_temp, store_temp,
1037 SENSOR_ATTR_2(temp4_max, S_IRUGO | S_IWUSR, show_temp, store_temp,
1039 SENSOR_ATTR_2(temp5_max, S_IRUGO | S_IWUSR, show_temp, store_temp,
1041 SENSOR_ATTR_2(temp6_max, S_IRUGO | S_IWUSR, show_temp, store_temp,
1043 SENSOR_ATTR_2(temp7_max, S_IRUGO | S_IWUSR, show_temp, store_temp,
1045 SENSOR_ATTR_2(temp8_max, S_IRUGO | S_IWUSR, show_temp, store_temp,
1047 SENSOR_ATTR_2(temp9_max, S_IRUGO | S_IWUSR, show_temp, store_temp,
1049 SENSOR_ATTR_2(temp10_max, S_IRUGO | S_IWUSR, show_temp, store_temp,
1053 static struct sensor_device_attribute_2 sda_temp_max_hyst[] = {
1054 SENSOR_ATTR_2(temp1_max_hyst, S_IRUGO | S_IWUSR, show_temp, store_temp,
1056 SENSOR_ATTR_2(temp2_max_hyst, S_IRUGO | S_IWUSR, show_temp, store_temp,
1058 SENSOR_ATTR_2(temp3_max_hyst, S_IRUGO | S_IWUSR, show_temp, store_temp,
1060 SENSOR_ATTR_2(temp4_max_hyst, S_IRUGO | S_IWUSR, show_temp, store_temp,
1062 SENSOR_ATTR_2(temp5_max_hyst, S_IRUGO | S_IWUSR, show_temp, store_temp,
1064 SENSOR_ATTR_2(temp6_max_hyst, S_IRUGO | S_IWUSR, show_temp, store_temp,
1066 SENSOR_ATTR_2(temp7_max_hyst, S_IRUGO | S_IWUSR, show_temp, store_temp,
1068 SENSOR_ATTR_2(temp8_max_hyst, S_IRUGO | S_IWUSR, show_temp, store_temp,
1070 SENSOR_ATTR_2(temp9_max_hyst, S_IRUGO | S_IWUSR, show_temp, store_temp,
1072 SENSOR_ATTR_2(temp10_max_hyst, S_IRUGO | S_IWUSR, show_temp, store_temp,
1076 static struct sensor_device_attribute_2 sda_temp_crit[] = {
1077 SENSOR_ATTR_2(temp1_crit, S_IRUGO | S_IWUSR, show_temp, store_temp,
1079 SENSOR_ATTR_2(temp2_crit, S_IRUGO | S_IWUSR, show_temp, store_temp,
1081 SENSOR_ATTR_2(temp3_crit, S_IRUGO | S_IWUSR, show_temp, store_temp,
1083 SENSOR_ATTR_2(temp4_crit, S_IRUGO | S_IWUSR, show_temp, store_temp,
1085 SENSOR_ATTR_2(temp5_crit, S_IRUGO | S_IWUSR, show_temp, store_temp,
1087 SENSOR_ATTR_2(temp6_crit, S_IRUGO | S_IWUSR, show_temp, store_temp,
1089 SENSOR_ATTR_2(temp7_crit, S_IRUGO | S_IWUSR, show_temp, store_temp,
1091 SENSOR_ATTR_2(temp8_crit, S_IRUGO | S_IWUSR, show_temp, store_temp,
1093 SENSOR_ATTR_2(temp9_crit, S_IRUGO | S_IWUSR, show_temp, store_temp,
1095 SENSOR_ATTR_2(temp10_crit, S_IRUGO | S_IWUSR, show_temp, store_temp,
1099 static struct sensor_device_attribute sda_temp_offset[] = {
1100 SENSOR_ATTR(temp1_offset, S_IRUGO | S_IWUSR, show_temp_offset,
1101 store_temp_offset, 0),
1102 SENSOR_ATTR(temp2_offset, S_IRUGO | S_IWUSR, show_temp_offset,
1103 store_temp_offset, 1),
1104 SENSOR_ATTR(temp3_offset, S_IRUGO | S_IWUSR, show_temp_offset,
1105 store_temp_offset, 2),
1106 SENSOR_ATTR(temp4_offset, S_IRUGO | S_IWUSR, show_temp_offset,
1107 store_temp_offset, 3),
1108 SENSOR_ATTR(temp5_offset, S_IRUGO | S_IWUSR, show_temp_offset,
1109 store_temp_offset, 4),
1110 SENSOR_ATTR(temp6_offset, S_IRUGO | S_IWUSR, show_temp_offset,
1111 store_temp_offset, 5),
1114 static struct sensor_device_attribute sda_temp_type[] = {
1115 SENSOR_ATTR(temp1_type, S_IRUGO | S_IWUSR, show_temp_type,
1116 store_temp_type, 0),
1117 SENSOR_ATTR(temp2_type, S_IRUGO | S_IWUSR, show_temp_type,
1118 store_temp_type, 1),
1119 SENSOR_ATTR(temp3_type, S_IRUGO | S_IWUSR, show_temp_type,
1120 store_temp_type, 2),
1121 SENSOR_ATTR(temp4_type, S_IRUGO | S_IWUSR, show_temp_type,
1122 store_temp_type, 3),
1123 SENSOR_ATTR(temp5_type, S_IRUGO | S_IWUSR, show_temp_type,
1124 store_temp_type, 4),
1125 SENSOR_ATTR(temp6_type, S_IRUGO | S_IWUSR, show_temp_type,
1126 store_temp_type, 5),
1129 static struct sensor_device_attribute sda_temp_alarm[] = {
1130 SENSOR_ATTR(temp1_alarm, S_IRUGO, show_alarm, NULL,
1132 SENSOR_ATTR(temp2_alarm, S_IRUGO, show_alarm, NULL,
1133 TEMP_ALARM_BASE + 1),
1134 SENSOR_ATTR(temp3_alarm, S_IRUGO, show_alarm, NULL,
1135 TEMP_ALARM_BASE + 2),
1136 SENSOR_ATTR(temp4_alarm, S_IRUGO, show_alarm, NULL,
1137 TEMP_ALARM_BASE + 3),
1138 SENSOR_ATTR(temp5_alarm, S_IRUGO, show_alarm, NULL,
1139 TEMP_ALARM_BASE + 4),
1140 SENSOR_ATTR(temp6_alarm, S_IRUGO, show_alarm, NULL,
1141 TEMP_ALARM_BASE + 5),
1144 #define NUM_TEMP_ALARM ARRAY_SIZE(sda_temp_alarm)
1147 show_name(struct device *dev, struct device_attribute *attr, char *buf)
1149 struct nct6775_data *data = dev_get_drvdata(dev);
1151 return sprintf(buf, "%s\n", data->name);
1154 static DEVICE_ATTR(name, S_IRUGO, show_name, NULL);
1157 show_vid(struct device *dev, struct device_attribute *attr, char *buf)
1159 struct nct6775_data *data = dev_get_drvdata(dev);
1160 return sprintf(buf, "%d\n", vid_from_reg(data->vid, data->vrm));
1163 static DEVICE_ATTR(cpu0_vid, S_IRUGO, show_vid, NULL);
1165 /* Case open detection */
1168 clear_caseopen(struct device *dev, struct device_attribute *attr,
1169 const char *buf, size_t count)
1171 struct nct6775_data *data = dev_get_drvdata(dev);
1172 struct nct6775_sio_data *sio_data = dev->platform_data;
1173 int nr = to_sensor_dev_attr(attr)->index - INTRUSION_ALARM_BASE;
1178 if (kstrtoul(buf, 10, &val) || val != 0)
1181 mutex_lock(&data->update_lock);
1184 * Use CR registers to clear caseopen status.
1185 * The CR registers are the same for all chips, and not all chips
1186 * support clearing the caseopen status through "regular" registers.
1188 ret = superio_enter(sio_data->sioreg);
1194 superio_select(sio_data->sioreg, NCT6775_LD_ACPI);
1195 reg = superio_inb(sio_data->sioreg, NCT6775_REG_CR_CASEOPEN_CLR[nr]);
1196 reg |= NCT6775_CR_CASEOPEN_CLR_MASK[nr];
1197 superio_outb(sio_data->sioreg, NCT6775_REG_CR_CASEOPEN_CLR[nr], reg);
1198 reg &= ~NCT6775_CR_CASEOPEN_CLR_MASK[nr];
1199 superio_outb(sio_data->sioreg, NCT6775_REG_CR_CASEOPEN_CLR[nr], reg);
1200 superio_exit(sio_data->sioreg);
1202 data->valid = false; /* Force cache refresh */
1204 mutex_unlock(&data->update_lock);
1208 static struct sensor_device_attribute sda_caseopen[] = {
1209 SENSOR_ATTR(intrusion0_alarm, S_IWUSR | S_IRUGO, show_alarm,
1210 clear_caseopen, INTRUSION_ALARM_BASE),
1211 SENSOR_ATTR(intrusion1_alarm, S_IWUSR | S_IRUGO, show_alarm,
1212 clear_caseopen, INTRUSION_ALARM_BASE + 1),
1216 * Driver and device management
1219 static void nct6775_device_remove_files(struct device *dev)
1222 * some entries in the following arrays may not have been used in
1223 * device_create_file(), but device_remove_file() will ignore them
1226 struct nct6775_data *data = dev_get_drvdata(dev);
1228 for (i = 0; i < data->in_num; i++)
1229 sysfs_remove_group(&dev->kobj, &nct6775_group_in[i]);
1231 for (i = 0; i < NUM_TEMP; i++) {
1232 if (!(data->have_temp & (1 << i)))
1234 device_remove_file(dev, &sda_temp_input[i].dev_attr);
1235 device_remove_file(dev, &sda_temp_label[i].dev_attr);
1236 device_remove_file(dev, &sda_temp_max[i].dev_attr);
1237 device_remove_file(dev, &sda_temp_max_hyst[i].dev_attr);
1238 device_remove_file(dev, &sda_temp_crit[i].dev_attr);
1239 if (!(data->have_temp_fixed & (1 << i)))
1241 device_remove_file(dev, &sda_temp_type[i].dev_attr);
1242 device_remove_file(dev, &sda_temp_offset[i].dev_attr);
1243 if (i >= NUM_TEMP_ALARM)
1245 device_remove_file(dev, &sda_temp_alarm[i].dev_attr);
1248 device_remove_file(dev, &sda_caseopen[0].dev_attr);
1249 device_remove_file(dev, &sda_caseopen[1].dev_attr);
1251 device_remove_file(dev, &dev_attr_name);
1252 device_remove_file(dev, &dev_attr_cpu0_vid);
1255 /* Get the monitoring functions started */
1256 static inline void nct6775_init_device(struct nct6775_data *data)
1261 /* Start monitoring if needed */
1262 if (data->REG_CONFIG) {
1263 tmp = nct6775_read_value(data, data->REG_CONFIG);
1265 nct6775_write_value(data, data->REG_CONFIG, tmp | 0x01);
1268 /* Enable temperature sensors if needed */
1269 for (i = 0; i < NUM_TEMP; i++) {
1270 if (!(data->have_temp & (1 << i)))
1272 if (!data->reg_temp_config[i])
1274 tmp = nct6775_read_value(data, data->reg_temp_config[i]);
1276 nct6775_write_value(data, data->reg_temp_config[i],
1280 /* Enable VBAT monitoring if needed */
1281 tmp = nct6775_read_value(data, data->REG_VBAT);
1283 nct6775_write_value(data, data->REG_VBAT, tmp | 0x01);
1285 diode = nct6775_read_value(data, data->REG_DIODE);
1287 for (i = 0; i < data->temp_fixed_num; i++) {
1288 if (!(data->have_temp_fixed & (1 << i)))
1290 if ((tmp & (0x02 << i))) /* diode */
1291 data->temp_type[i] = 3 - ((diode >> i) & 0x02);
1292 else /* thermistor */
1293 data->temp_type[i] = 4;
1297 static int nct6775_probe(struct platform_device *pdev)
1299 struct device *dev = &pdev->dev;
1300 struct nct6775_sio_data *sio_data = dev->platform_data;
1301 struct nct6775_data *data;
1302 struct resource *res;
1304 int src, mask, available;
1305 const u16 *reg_temp, *reg_temp_over, *reg_temp_hyst, *reg_temp_config;
1306 const u16 *reg_temp_alternate, *reg_temp_crit;
1309 res = platform_get_resource(pdev, IORESOURCE_IO, 0);
1310 if (!devm_request_region(&pdev->dev, res->start, IOREGION_LENGTH,
1314 data = devm_kzalloc(&pdev->dev, sizeof(struct nct6775_data),
1319 data->kind = sio_data->kind;
1320 data->addr = res->start;
1321 mutex_init(&data->lock);
1322 mutex_init(&data->update_lock);
1323 data->name = nct6775_device_names[data->kind];
1324 data->bank = 0xff; /* Force initial bank selection */
1325 platform_set_drvdata(pdev, data);
1327 switch (data->kind) {
1330 data->temp_fixed_num = 3;
1332 data->ALARM_BITS = NCT6775_ALARM_BITS;
1334 data->temp_label = nct6775_temp_label;
1335 data->temp_label_num = ARRAY_SIZE(nct6775_temp_label);
1337 data->REG_CONFIG = NCT6775_REG_CONFIG;
1338 data->REG_VBAT = NCT6775_REG_VBAT;
1339 data->REG_DIODE = NCT6775_REG_DIODE;
1340 data->REG_VIN = NCT6775_REG_IN;
1341 data->REG_IN_MINMAX[0] = NCT6775_REG_IN_MIN;
1342 data->REG_IN_MINMAX[1] = NCT6775_REG_IN_MAX;
1343 data->REG_TEMP_OFFSET = NCT6775_REG_TEMP_OFFSET;
1344 data->REG_TEMP_SOURCE = NCT6775_REG_TEMP_SOURCE;
1345 data->REG_ALARM = NCT6775_REG_ALARM;
1347 reg_temp = NCT6775_REG_TEMP;
1348 num_reg_temp = ARRAY_SIZE(NCT6775_REG_TEMP);
1349 reg_temp_over = NCT6775_REG_TEMP_OVER;
1350 reg_temp_hyst = NCT6775_REG_TEMP_HYST;
1351 reg_temp_config = NCT6775_REG_TEMP_CONFIG;
1352 reg_temp_alternate = NCT6775_REG_TEMP_ALTERNATE;
1353 reg_temp_crit = NCT6775_REG_TEMP_CRIT;
1358 data->temp_fixed_num = 3;
1360 data->ALARM_BITS = NCT6776_ALARM_BITS;
1362 data->temp_label = nct6776_temp_label;
1363 data->temp_label_num = ARRAY_SIZE(nct6776_temp_label);
1365 data->REG_CONFIG = NCT6775_REG_CONFIG;
1366 data->REG_VBAT = NCT6775_REG_VBAT;
1367 data->REG_DIODE = NCT6775_REG_DIODE;
1368 data->REG_VIN = NCT6775_REG_IN;
1369 data->REG_IN_MINMAX[0] = NCT6775_REG_IN_MIN;
1370 data->REG_IN_MINMAX[1] = NCT6775_REG_IN_MAX;
1371 data->REG_TEMP_OFFSET = NCT6775_REG_TEMP_OFFSET;
1372 data->REG_TEMP_SOURCE = NCT6775_REG_TEMP_SOURCE;
1373 data->REG_ALARM = NCT6775_REG_ALARM;
1375 reg_temp = NCT6775_REG_TEMP;
1376 num_reg_temp = ARRAY_SIZE(NCT6775_REG_TEMP);
1377 reg_temp_over = NCT6775_REG_TEMP_OVER;
1378 reg_temp_hyst = NCT6775_REG_TEMP_HYST;
1379 reg_temp_config = NCT6776_REG_TEMP_CONFIG;
1380 reg_temp_alternate = NCT6776_REG_TEMP_ALTERNATE;
1381 reg_temp_crit = NCT6776_REG_TEMP_CRIT;
1386 data->temp_fixed_num = 6;
1388 data->ALARM_BITS = NCT6779_ALARM_BITS;
1390 data->temp_label = nct6779_temp_label;
1391 data->temp_label_num = ARRAY_SIZE(nct6779_temp_label);
1393 data->REG_CONFIG = NCT6775_REG_CONFIG;
1394 data->REG_VBAT = NCT6775_REG_VBAT;
1395 data->REG_DIODE = NCT6775_REG_DIODE;
1396 data->REG_VIN = NCT6779_REG_IN;
1397 data->REG_IN_MINMAX[0] = NCT6775_REG_IN_MIN;
1398 data->REG_IN_MINMAX[1] = NCT6775_REG_IN_MAX;
1399 data->REG_TEMP_OFFSET = NCT6779_REG_TEMP_OFFSET;
1400 data->REG_TEMP_SOURCE = NCT6775_REG_TEMP_SOURCE;
1401 data->REG_ALARM = NCT6779_REG_ALARM;
1403 reg_temp = NCT6779_REG_TEMP;
1404 num_reg_temp = ARRAY_SIZE(NCT6779_REG_TEMP);
1405 reg_temp_over = NCT6779_REG_TEMP_OVER;
1406 reg_temp_hyst = NCT6779_REG_TEMP_HYST;
1407 reg_temp_config = NCT6779_REG_TEMP_CONFIG;
1408 reg_temp_alternate = NCT6779_REG_TEMP_ALTERNATE;
1409 reg_temp_crit = NCT6779_REG_TEMP_CRIT;
1415 data->have_in = (1 << data->in_num) - 1;
1416 data->have_temp = 0;
1419 * On some boards, not all available temperature sources are monitored,
1420 * even though some of the monitoring registers are unused.
1421 * Get list of unused monitoring registers, then detect if any fan
1422 * controls are configured to use unmonitored temperature sources.
1423 * If so, assign the unmonitored temperature sources to available
1424 * monitoring registers.
1428 for (i = 0; i < num_reg_temp; i++) {
1429 if (reg_temp[i] == 0)
1432 src = nct6775_read_value(data, data->REG_TEMP_SOURCE[i]) & 0x1f;
1433 if (!src || (mask & (1 << src)))
1434 available |= 1 << i;
1440 s = NUM_TEMP_FIXED; /* First dynamic temperature attribute */
1441 for (i = 0; i < num_reg_temp; i++) {
1442 if (reg_temp[i] == 0)
1445 src = nct6775_read_value(data, data->REG_TEMP_SOURCE[i]) & 0x1f;
1446 if (!src || (mask & (1 << src)))
1449 if (src >= data->temp_label_num ||
1450 !strlen(data->temp_label[src])) {
1452 "Invalid temperature source %d at index %d, source register 0x%x, temp register 0x%x\n",
1453 src, i, data->REG_TEMP_SOURCE[i], reg_temp[i]);
1459 /* Use fixed index for SYSTIN(1), CPUTIN(2), AUXTIN(3) */
1460 if (src <= data->temp_fixed_num) {
1461 data->have_temp |= 1 << (src - 1);
1462 data->have_temp_fixed |= 1 << (src - 1);
1463 data->reg_temp[0][src - 1] = reg_temp[i];
1464 data->reg_temp[1][src - 1] = reg_temp_over[i];
1465 data->reg_temp[2][src - 1] = reg_temp_hyst[i];
1466 data->reg_temp_config[src - 1] = reg_temp_config[i];
1467 data->temp_src[src - 1] = src;
1474 /* Use dynamic index for other sources */
1475 data->have_temp |= 1 << s;
1476 data->reg_temp[0][s] = reg_temp[i];
1477 data->reg_temp[1][s] = reg_temp_over[i];
1478 data->reg_temp[2][s] = reg_temp_hyst[i];
1479 data->reg_temp_config[s] = reg_temp_config[i];
1480 if (reg_temp_crit[src - 1])
1481 data->reg_temp[3][s] = reg_temp_crit[src - 1];
1483 data->temp_src[s] = src;
1487 #ifdef USE_ALTERNATE
1489 * Go through the list of alternate temp registers and enable
1491 * The temperature is already monitored if the respective bit in <mask>
1494 for (i = 0; i < data->temp_label_num - 1; i++) {
1495 if (!reg_temp_alternate[i])
1497 if (mask & (1 << (i + 1)))
1499 if (i < data->temp_fixed_num) {
1500 if (data->have_temp & (1 << i))
1502 data->have_temp |= 1 << i;
1503 data->have_temp_fixed |= 1 << i;
1504 data->reg_temp[0][i] = reg_temp_alternate[i];
1505 data->reg_temp[1][i] = reg_temp_over[i];
1506 data->reg_temp[2][i] = reg_temp_hyst[i];
1507 data->temp_src[i] = i + 1;
1511 if (s >= NUM_TEMP) /* Abort if no more space */
1514 data->have_temp |= 1 << s;
1515 data->reg_temp[0][s] = reg_temp_alternate[i];
1516 data->temp_src[s] = i + 1;
1519 #endif /* USE_ALTERNATE */
1521 switch (data->kind) {
1526 * On NCT6776, AUXTIN and VIN3 pins are shared.
1527 * Only way to detect it is to check if AUXTIN is used
1528 * as a temperature source, and if that source is
1531 * If that is the case, disable in6, which reports VIN3.
1532 * Otherwise disable temp3.
1534 if (data->have_temp & (1 << 2)) {
1535 u8 reg = nct6775_read_value(data,
1536 data->reg_temp_config[2]);
1538 data->have_temp &= ~(1 << 2);
1540 data->have_in &= ~(1 << 6);
1551 * There does not seem to be a clean way to detect if VINx or
1552 * AUXTINx is active, so for keep both sensor types enabled
1558 /* Initialize the chip */
1559 nct6775_init_device(data);
1561 data->vrm = vid_which_vrm();
1562 err = superio_enter(sio_data->sioreg);
1568 * We can get the VID input values directly at logical device D 0xe3.
1570 superio_select(sio_data->sioreg, NCT6775_LD_VID);
1571 data->vid = superio_inb(sio_data->sioreg, 0xe3);
1572 superio_exit(sio_data->sioreg);
1574 err = device_create_file(dev, &dev_attr_cpu0_vid);
1578 for (i = 0; i < data->in_num; i++) {
1579 if (!(data->have_in & (1 << i)))
1581 err = sysfs_create_group(&dev->kobj, &nct6775_group_in[i]);
1586 for (i = 0; i < NUM_TEMP; i++) {
1587 if (!(data->have_temp & (1 << i)))
1589 err = device_create_file(dev, &sda_temp_input[i].dev_attr);
1592 if (data->temp_label) {
1593 err = device_create_file(dev,
1594 &sda_temp_label[i].dev_attr);
1598 if (data->reg_temp[1][i]) {
1599 err = device_create_file(dev,
1600 &sda_temp_max[i].dev_attr);
1604 if (data->reg_temp[2][i]) {
1605 err = device_create_file(dev,
1606 &sda_temp_max_hyst[i].dev_attr);
1610 if (data->reg_temp[3][i]) {
1611 err = device_create_file(dev,
1612 &sda_temp_crit[i].dev_attr);
1616 if (!(data->have_temp_fixed & (1 << i)))
1618 err = device_create_file(dev, &sda_temp_type[i].dev_attr);
1621 err = device_create_file(dev, &sda_temp_offset[i].dev_attr);
1624 if (i >= NUM_TEMP_ALARM ||
1625 data->ALARM_BITS[TEMP_ALARM_BASE + i] < 0)
1627 err = device_create_file(dev, &sda_temp_alarm[i].dev_attr);
1632 for (i = 0; i < ARRAY_SIZE(sda_caseopen); i++) {
1633 if (data->ALARM_BITS[INTRUSION_ALARM_BASE + i] < 0)
1635 err = device_create_file(dev, &sda_caseopen[i].dev_attr);
1640 err = device_create_file(dev, &dev_attr_name);
1644 data->hwmon_dev = hwmon_device_register(dev);
1645 if (IS_ERR(data->hwmon_dev)) {
1646 err = PTR_ERR(data->hwmon_dev);
1653 nct6775_device_remove_files(dev);
1657 static int nct6775_remove(struct platform_device *pdev)
1659 struct nct6775_data *data = platform_get_drvdata(pdev);
1661 hwmon_device_unregister(data->hwmon_dev);
1662 nct6775_device_remove_files(&pdev->dev);
1667 static struct platform_driver nct6775_driver = {
1669 .owner = THIS_MODULE,
1672 .probe = nct6775_probe,
1673 .remove = nct6775_remove,
1676 /* nct6775_find() looks for a '627 in the Super-I/O config space */
1677 static int __init nct6775_find(int sioaddr, unsigned short *addr,
1678 struct nct6775_sio_data *sio_data)
1680 static const char sio_name_NCT6775[] __initconst = "NCT6775F";
1681 static const char sio_name_NCT6776[] __initconst = "NCT6776F";
1682 static const char sio_name_NCT6779[] __initconst = "NCT6779D";
1685 const char *sio_name;
1688 err = superio_enter(sioaddr);
1695 val = (superio_inb(sioaddr, SIO_REG_DEVID) << 8)
1696 | superio_inb(sioaddr, SIO_REG_DEVID + 1);
1697 switch (val & SIO_ID_MASK) {
1698 case SIO_NCT6775_ID:
1699 sio_data->kind = nct6775;
1700 sio_name = sio_name_NCT6775;
1702 case SIO_NCT6776_ID:
1703 sio_data->kind = nct6776;
1704 sio_name = sio_name_NCT6776;
1706 case SIO_NCT6779_ID:
1707 sio_data->kind = nct6779;
1708 sio_name = sio_name_NCT6779;
1712 pr_debug("unsupported chip ID: 0x%04x\n", val);
1713 superio_exit(sioaddr);
1717 /* We have a known chip, find the HWM I/O address */
1718 superio_select(sioaddr, NCT6775_LD_HWM);
1719 val = (superio_inb(sioaddr, SIO_REG_ADDR) << 8)
1720 | superio_inb(sioaddr, SIO_REG_ADDR + 1);
1721 *addr = val & IOREGION_ALIGNMENT;
1723 pr_err("Refusing to enable a Super-I/O device with a base I/O port 0\n");
1724 superio_exit(sioaddr);
1728 /* Activate logical device if needed */
1729 val = superio_inb(sioaddr, SIO_REG_ENABLE);
1730 if (!(val & 0x01)) {
1731 pr_warn("Forcibly enabling Super-I/O. Sensor is probably unusable.\n");
1732 superio_outb(sioaddr, SIO_REG_ENABLE, val | 0x01);
1735 superio_exit(sioaddr);
1736 pr_info("Found %s chip at %#x\n", sio_name, *addr);
1737 sio_data->sioreg = sioaddr;
1743 * when Super-I/O functions move to a separate file, the Super-I/O
1744 * bus will manage the lifetime of the device and this module will only keep
1745 * track of the nct6775 driver. But since we platform_device_alloc(), we
1746 * must keep track of the device
1748 static struct platform_device *pdev;
1750 static int __init sensors_nct6775_init(void)
1753 unsigned short address;
1754 struct resource res;
1755 struct nct6775_sio_data sio_data;
1758 * initialize sio_data->kind and sio_data->sioreg.
1760 * when Super-I/O functions move to a separate file, the Super-I/O
1761 * driver will probe 0x2e and 0x4e and auto-detect the presence of a
1762 * nct6775 hardware monitor, and call probe()
1764 if (nct6775_find(0x2e, &address, &sio_data) &&
1765 nct6775_find(0x4e, &address, &sio_data))
1768 err = platform_driver_register(&nct6775_driver);
1772 pdev = platform_device_alloc(DRVNAME, address);
1775 pr_err("Device allocation failed\n");
1776 goto exit_unregister;
1779 err = platform_device_add_data(pdev, &sio_data,
1780 sizeof(struct nct6775_sio_data));
1782 pr_err("Platform data allocation failed\n");
1783 goto exit_device_put;
1786 memset(&res, 0, sizeof(res));
1788 res.start = address + IOREGION_OFFSET;
1789 res.end = address + IOREGION_OFFSET + IOREGION_LENGTH - 1;
1790 res.flags = IORESOURCE_IO;
1792 err = acpi_check_resource_conflict(&res);
1794 goto exit_device_put;
1796 err = platform_device_add_resources(pdev, &res, 1);
1798 pr_err("Device resource addition failed (%d)\n", err);
1799 goto exit_device_put;
1802 /* platform_device_add calls probe() */
1803 err = platform_device_add(pdev);
1805 pr_err("Device addition failed (%d)\n", err);
1806 goto exit_device_put;
1812 platform_device_put(pdev);
1814 platform_driver_unregister(&nct6775_driver);
1819 static void __exit sensors_nct6775_exit(void)
1821 platform_device_unregister(pdev);
1822 platform_driver_unregister(&nct6775_driver);
1825 MODULE_AUTHOR("Guenter Roeck <linux@roeck-us.net>");
1826 MODULE_DESCRIPTION("NCT6775F/NCT6776F/NCT6779D driver");
1827 MODULE_LICENSE("GPL");
1829 module_init(sensors_nct6775_init);
1830 module_exit(sensors_nct6775_exit);