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-2011 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 9 0xb470 0xc1 0x5ca3
37 * nct6776f 9 5 3 9 0xc330 0xc1 0x5ca3
38 * nct6779d 15 5 5 7 0xc560 0xc1 0x5ca3
41 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
43 #include <linux/module.h>
44 #include <linux/init.h>
45 #include <linux/slab.h>
46 #include <linux/jiffies.h>
47 #include <linux/platform_device.h>
48 #include <linux/hwmon.h>
49 #include <linux/hwmon-sysfs.h>
50 #include <linux/hwmon-vid.h>
51 #include <linux/err.h>
52 #include <linux/mutex.h>
53 #include <linux/acpi.h>
57 enum kinds { nct6775, nct6776, nct6779, };
59 /* used to set data->name = nct6775_device_names[data->sio_kind] */
60 static const char * const nct6775_device_names[] = {
66 static unsigned short force_id;
67 module_param(force_id, ushort, 0);
68 MODULE_PARM_DESC(force_id, "Override the detected device ID");
70 static unsigned short fan_debounce;
71 module_param(fan_debounce, ushort, 0);
72 MODULE_PARM_DESC(fan_debounce, "Enable debouncing for fan RPM signal");
74 #define DRVNAME "nct6775"
77 * Super-I/O constants and functions
80 #define NCT6775_LD_HWM 0x0b
81 #define NCT6775_LD_VID 0x0d
83 #define SIO_REG_LDSEL 0x07 /* Logical device select */
84 #define SIO_REG_DEVID 0x20 /* Device ID (2 bytes) */
85 #define SIO_REG_EN_VRM10 0x2C /* GPIO3, GPIO4 selection */
86 #define SIO_REG_ENABLE 0x30 /* Logical device enable */
87 #define SIO_REG_ADDR 0x60 /* Logical device address (2 bytes) */
88 #define SIO_REG_VID_CTRL 0xF0 /* VID control */
89 #define SIO_REG_VID_DATA 0xF1 /* VID data */
91 #define SIO_NCT6775_ID 0xb470
92 #define SIO_NCT6776_ID 0xc330
93 #define SIO_NCT6779_ID 0xc560
94 #define SIO_ID_MASK 0xFFF0
97 superio_outb(int ioreg, int reg, int val)
100 outb(val, ioreg + 1);
104 superio_inb(int ioreg, int reg)
107 return inb(ioreg + 1);
111 superio_select(int ioreg, int ld)
113 outb(SIO_REG_LDSEL, ioreg);
118 superio_enter(int ioreg)
125 superio_exit(int ioreg)
129 outb(0x02, ioreg + 1);
136 #define IOREGION_ALIGNMENT (~7)
137 #define IOREGION_OFFSET 5
138 #define IOREGION_LENGTH 2
139 #define ADDR_REG_OFFSET 0
140 #define DATA_REG_OFFSET 1
142 #define NCT6775_REG_BANK 0x4E
143 #define NCT6775_REG_CONFIG 0x40
146 * Not currently used:
147 * REG_MAN_ID has the value 0x5ca3 for all supported chips.
148 * REG_CHIP_ID == 0x88/0xa1/0xc1 depending on chip model.
149 * REG_MAN_ID is at port 0x4f
150 * REG_CHIP_ID is at port 0x58
153 static const u16 NCT6775_REG_FAN_MIN[] = { 0x3b, 0x3c, 0x3d };
155 /* Voltage min/max registers for nr=7..14 are in bank 5 */
156 #define NCT6775_REG_IN_MAX(nr) ((nr < 7) ? (0x2b + (nr) * 2) : \
157 (0x554 + (((nr) - 7) * 2)))
158 #define NCT6775_REG_IN_MIN(nr) ((nr < 7) ? (0x2c + (nr) * 2) : \
159 (0x555 + (((nr) - 7) * 2)))
161 static const u16 NCT6775_REG_IN[] = {
162 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x550, 0x551, 0x552
165 static const u16 NCT6779_REG_IN[] = {
166 0x480, 0x481, 0x482, 0x483, 0x484, 0x485, 0x486, 0x487,
167 0x488, 0x489, 0x48a, 0x48b, 0x48c, 0x48d, 0x48e
170 #define NCT6775_REG_VBAT 0x5D
171 #define NCT6775_REG_DIODE 0x59
173 #define NCT6775_REG_FANDIV1 0x506
174 #define NCT6775_REG_FANDIV2 0x507
176 #define NCT6775_REG_FAN_DEBOUNCE 0xf0
178 #define NCT6775_REG_ALARM1 0x459
179 #define NCT6775_REG_ALARM2 0x45A
180 #define NCT6775_REG_ALARM3 0x45B
181 #define NCT6779_REG_ALARM4 0x568
183 #define NCT6775_REG_CASEOPEN_DET 0x42 /* SMI STATUS #2 */
184 #define NCT6775_REG_CASEOPEN_CLR 0x46 /* SMI MASK #3 */
186 /* DC or PWM output fan configuration */
187 static const u8 NCT6775_REG_PWM_ENABLE[] = {
188 0x04, /* SYSFAN output mode */
189 0x04, /* CPUFAN output mode */
190 0x12, /* AUXFAN output mode */
193 static const u8 NCT6775_PWM_MODE_SHIFT[] = { 0, 1, 0 };
195 /* Advanced Fan control, some values are common for all fans */
197 static const u16 NCT6775_REG_TARGET[] = { 0x101, 0x201, 0x301, 0x801, 0x901 };
198 static const u16 NCT6775_REG_FAN_MODE[] = { 0x102, 0x202, 0x302, 0x802, 0x902 };
199 static const u16 NCT6775_REG_FAN_STEP_DOWN_TIME[] = {
200 0x103, 0x203, 0x303, 0x803, 0x903 };
201 static const u16 NCT6775_REG_FAN_STEP_UP_TIME[] = {
202 0x104, 0x204, 0x304, 0x804, 0x904 };
203 static const u16 NCT6775_REG_FAN_STOP_OUTPUT[] = {
204 0x105, 0x205, 0x305, 0x805, 0x905 };
205 static const u16 NCT6775_REG_FAN_START_OUTPUT[]
206 = { 0x106, 0x206, 0x306, 0x806, 0x906 };
207 static const u16 NCT6775_REG_FAN_STOP_TIME[]
208 = { 0x107, 0x207, 0x307, 0x807, 0x907 };
209 static const u16 NCT6775_REG_PWM[] = { 0x109, 0x209, 0x309, 0x809, 0x909 };
210 static const u16 NCT6775_REG_PWM_READ[] = { 0x01, 0x03, 0x11, 0x13, 0x15 };
212 static const u16 NCT6775_REG_FAN[] = { 0x630, 0x632, 0x634, 0x636, 0x638 };
214 static const u16 NCT6776_REG_FAN_MIN[] = { 0x63a, 0x63c, 0x63e, 0x640, 0x642};
216 static const u16 NCT6779_REG_TOLERANCE_H[]
217 = { 0x10c, 0x20c, 0x30c, 0x40c, 0x50c };
219 static const u16 NCT6779_REG_FAN[] = { 0x4c0, 0x4c2, 0x4c4, 0x4c6, 0x4c8 };
221 static const u16 NCT6775_REG_TEMP[11]
222 = { 0x27, 0x150, 0x250, 0x62b, 0x62c, 0x62d, 0x73, 0x75, 0x77 };
223 static const u16 NCT6775_REG_TEMP_CONFIG[11]
224 = { 0, 0x152, 0x252, 0x628, 0x629, 0x62A };
225 static const u16 NCT6775_REG_TEMP_HYST[11]
226 = { 0x3a, 0x153, 0x253, 0x673, 0x678, 0x67D };
227 static const u16 NCT6775_REG_TEMP_OVER[11]
228 = { 0x39, 0x155, 0x255, 0x672, 0x677, 0x67C };
229 static const u16 NCT6775_REG_TEMP_SOURCE[11]
230 = { 0x621, 0x622, 0x623, 0x624, 0x625, 0x626, 0x100, 0x200, 0x300,
233 static const u16 NCT6776_REG_TEMP_CONFIG[11]
234 = { 0x18, 0x152, 0x252, 0x628, 0x629, 0x62A };
236 static const u16 NCT6779_REG_TEMP[11]
237 = { 0x27, 0x150, 0, 0, 0, 0, 0x73, 0x75, 0x77, 0x79, 0x7b };
239 static const u16 NCT6775_REG_AUTO_BASE[]
240 = { 0x100, 0x200, 0x300, 0x800, 0x900 };
242 #define NCT6775_REG_AUTO_TEMP(nr, p) (NCT6775_REG_AUTO_BASE[nr] + 0x21 + (p))
243 #define NCT6775_REG_AUTO_PWM(nr, p) (NCT6775_REG_AUTO_BASE[nr] + 0x27 + (p))
245 static const u16 NCT6775_REG_CRITICAL_ENAB[] = { 0x134, 0x234, 0x334 };
246 static const u16 NCT6775_REG_CRITICAL_TEMP[] = {
247 0x135, 0x235, 0x335, 0x835, 0x935 };
248 static const u16 NCT6775_REG_CRITICAL_TEMP_TOLERANCE[] = {
249 0x138, 0x238, 0x338, 0x838, 0x938 };
251 static const u16 NCT6779_REG_CRITICAL_PWM_ENABLE[] = {
252 0x136, 0x236, 0x336, 0x836, 0x936 };
253 static const u16 NCT6779_REG_CRITICAL_PWM[] = {
254 0x137, 0x237, 0x337, 0x837, 0x937 };
256 static const char *const nct6775_temp_label[] = {
270 "PCH_CHIP_CPU_MAX_TEMP",
280 static const char *const nct6776_temp_label[] = {
295 "PCH_CHIP_CPU_MAX_TEMP",
306 static const char *const nct6779_temp_label[] = {
325 "PCH_CHIP_CPU_MAX_TEMP",
336 #define NUM_REG_TEMP ARRAY_SIZE(NCT6775_REG_TEMP)
338 static inline int reg_to_pwm_enable(int pwm, int mode)
340 if (mode == 0 && pwm == 255)
342 if (mode == 3) /* SmartFan III */
343 return 2; /* convert to thermal cruise */
346 return 4; /* SmartFan IV */
349 static inline int pwm_enable_to_reg(int mode)
358 static int is_word_sized(u16 reg)
360 return ((((reg & 0xff00) == 0x100
361 || (reg & 0xff00) == 0x200)
362 && ((reg & 0x00ff) == 0x50
363 || (reg & 0x00ff) == 0x53
364 || (reg & 0x00ff) == 0x55))
365 || (reg & 0xfff0) == 0x630
366 || (reg & 0xfff0) == 0x4c0
367 || reg == 0x640 || reg == 0x642
368 || ((reg & 0xfff0) == 0x650
369 && (reg & 0x000f) >= 0x06)
370 || reg == 0x73 || reg == 0x75 || reg == 0x77 || reg == 0x77
379 /* 1 is PWM mode, output in ms */
380 static inline unsigned int step_time_from_reg(u8 reg, u8 mode)
382 return mode ? 100 * reg : 400 * reg;
385 static inline u8 step_time_to_reg(unsigned int msec, u8 mode)
387 return SENSORS_LIMIT((mode ? (msec + 50) / 100 :
388 (msec + 200) / 400), 1, 255);
391 static unsigned int fan_from_reg8(u16 reg, unsigned int divreg)
393 if (reg == 0 || reg == 255)
395 return 1350000U / (reg << divreg);
398 static unsigned int fan_from_reg13(u16 reg, unsigned int divreg)
400 if ((reg & 0xff1f) == 0xff1f)
403 reg = (reg & 0x1f) | ((reg & 0xff00) >> 3);
408 return 1350000U / reg;
411 static unsigned int fan_from_reg16(u16 reg, unsigned int divreg)
413 if (reg == 0 || reg == 0xffff)
417 * Even though the registers are 16 bit wide, the fan divisor
420 return 1350000U / (reg << divreg);
423 static inline unsigned int
430 * Some of the voltage inputs have internal scaling, the tables below
431 * contain 8 (the ADC LSB in mV) * scaling factor * 100
433 static const u16 scale_in[15] = {
434 800, 800, 1600, 1600, 800, 800, 800, 1600, 1600, 800, 800, 800, 800,
438 static inline long in_from_reg(u8 reg, u8 nr)
440 return DIV_ROUND_CLOSEST(reg * scale_in[nr], 100);
443 static inline u8 in_to_reg(u32 val, u8 nr)
445 return SENSORS_LIMIT(DIV_ROUND_CLOSEST(val * 100, scale_in[nr]), 0,
450 * Data structures and manipulation thereof
453 struct nct6775_data {
454 int addr; /* IO base of hw monitor block */
457 struct device *hwmon_dev;
460 u16 reg_temp[NUM_REG_TEMP];
461 u16 reg_temp_over[NUM_REG_TEMP];
462 u16 reg_temp_hyst[NUM_REG_TEMP];
463 u16 reg_temp_config[NUM_REG_TEMP];
464 u8 temp_src[NUM_REG_TEMP];
465 const char * const *temp_label;
469 const u16 *REG_TARGET;
471 const u16 *REG_FAN_MIN;
472 const u16 *REG_FAN_START_OUTPUT;
473 const u16 *REG_FAN_STOP_OUTPUT;
475 unsigned int (*fan_from_reg)(u16 reg, unsigned int divreg);
476 unsigned int (*fan_from_reg_min)(u16 reg, unsigned int divreg);
478 struct mutex update_lock;
479 char valid; /* !=0 if following fields are valid */
480 unsigned long last_updated; /* In jiffies */
482 /* Register values */
483 u8 bank; /* current register bank */
484 u8 in_num; /* number of in inputs we have */
485 u8 in[15]; /* Register value */
486 u8 in_max[15]; /* Register value */
487 u8 in_min[15]; /* Register value */
492 u8 has_fan; /* some fan inputs can be disabled */
493 u8 has_fan_min; /* some fans don't have min register */
498 s16 temp_max_hyst[6];
502 u8 pwm_mode[5]; /* 0->DC variable voltage, 1->PWM variable duty cycle */
503 u8 pwm_enable[5]; /* 0->off
505 * 2->thermal cruise mode (also called SmartFan I)
506 * 3->fan speed cruise mode
507 * 4->enhanced variable thermal cruise (also called
510 u8 pwm_num; /* number of pwm */
515 u8 fan_start_output[5]; /* minimum fan speed when spinning up */
516 u8 fan_stop_output[5]; /* minimum fan speed when spinning down */
517 u8 fan_stop_time[5]; /* time at minimum before disabling fan */
518 u8 fan_step_up_time[5];
519 u8 fan_step_down_time[5];
521 /* Automatic fan speed control registers */
533 struct nct6775_sio_data {
539 * On older chips, only registers 0x50-0x5f are banked.
540 * On more recent chips, all registers are banked.
541 * Assume that is the case and set the bank number for each access.
542 * Cache the bank number so it only needs to be set if it changes.
544 static inline void nct6775_set_bank(struct nct6775_data *data, u16 reg)
547 if (data->bank != bank) {
548 outb_p(NCT6775_REG_BANK, data->addr + ADDR_REG_OFFSET);
549 outb_p(bank, data->addr + DATA_REG_OFFSET);
554 static u16 nct6775_read_value(struct nct6775_data *data, u16 reg)
556 int res, word_sized = is_word_sized(reg);
558 mutex_lock(&data->lock);
560 nct6775_set_bank(data, reg);
561 outb_p(reg & 0xff, data->addr + ADDR_REG_OFFSET);
562 res = inb_p(data->addr + DATA_REG_OFFSET);
564 outb_p((reg & 0xff) + 1,
565 data->addr + ADDR_REG_OFFSET);
566 res = (res << 8) + inb_p(data->addr + DATA_REG_OFFSET);
569 mutex_unlock(&data->lock);
573 static int nct6775_write_value(struct nct6775_data *data, u16 reg,
576 int word_sized = is_word_sized(reg);
578 mutex_lock(&data->lock);
580 nct6775_set_bank(data, reg);
581 outb_p(reg & 0xff, data->addr + ADDR_REG_OFFSET);
583 outb_p(value >> 8, data->addr + DATA_REG_OFFSET);
584 outb_p((reg & 0xff) + 1,
585 data->addr + ADDR_REG_OFFSET);
587 outb_p(value & 0xff, data->addr + DATA_REG_OFFSET);
589 mutex_unlock(&data->lock);
593 /* We left-align 8-bit temperature values to make the code simpler */
594 static u16 nct6775_read_temp(struct nct6775_data *data, u16 reg)
598 res = nct6775_read_value(data, reg);
599 if (!is_word_sized(reg))
605 static int nct6775_write_temp(struct nct6775_data *data, u16 reg,
608 if (!is_word_sized(reg))
610 return nct6775_write_value(data, reg, value);
613 /* This function assumes that the caller holds data->update_lock */
614 static void nct6775_write_fan_div(struct nct6775_data *data, int nr)
620 reg = (nct6775_read_value(data, NCT6775_REG_FANDIV1) & 0x70)
621 | (data->fan_div[0] & 0x7);
622 nct6775_write_value(data, NCT6775_REG_FANDIV1, reg);
625 reg = (nct6775_read_value(data, NCT6775_REG_FANDIV1) & 0x7)
626 | ((data->fan_div[1] << 4) & 0x70);
627 nct6775_write_value(data, NCT6775_REG_FANDIV1, reg);
629 reg = (nct6775_read_value(data, NCT6775_REG_FANDIV2) & 0x70)
630 | (data->fan_div[2] & 0x7);
631 nct6775_write_value(data, NCT6775_REG_FANDIV2, reg);
634 reg = (nct6775_read_value(data, NCT6775_REG_FANDIV2) & 0x7)
635 | ((data->fan_div[3] << 4) & 0x70);
636 nct6775_write_value(data, NCT6775_REG_FANDIV2, reg);
641 static void nct6775_write_fan_div_common(struct device *dev,
642 struct nct6775_data *data, int nr)
644 struct nct6775_sio_data *sio_data = dev->platform_data;
646 if (sio_data->kind == nct6775)
647 nct6775_write_fan_div(data, nr);
650 static void nct6775_update_fan_div(struct nct6775_data *data)
654 i = nct6775_read_value(data, NCT6775_REG_FANDIV1);
655 data->fan_div[0] = i & 0x7;
656 data->fan_div[1] = (i & 0x70) >> 4;
657 i = nct6775_read_value(data, NCT6775_REG_FANDIV2);
658 data->fan_div[2] = i & 0x7;
659 if (data->has_fan & (1<<3))
660 data->fan_div[3] = (i & 0x70) >> 4;
663 static void nct6775_update_fan_div_common(struct device *dev,
664 struct nct6775_data *data)
666 struct nct6775_sio_data *sio_data = dev->platform_data;
668 if (sio_data->kind == nct6775)
669 nct6775_update_fan_div(data);
672 static void nct6775_update_pwm(struct device *dev)
674 struct nct6775_data *data = dev_get_drvdata(dev);
675 struct nct6775_sio_data *sio_data = dev->platform_data;
677 int pwmcfg, fanmodecfg, tol;
679 for (i = 0; i < data->pwm_num; i++) {
682 !(nct6775_read_value(data,
683 NCT6775_REG_PWM_ENABLE[0]) & 1);
687 data->pwm_mode[i] = pwmcfg;
689 fanmodecfg = nct6775_read_value(data,
690 NCT6775_REG_FAN_MODE[i]);
691 data->pwm[i] = nct6775_read_value(data, NCT6775_REG_PWM[i]);
692 data->pwm_enable[i] = reg_to_pwm_enable(data->pwm[i],
693 (fanmodecfg >> 4) & 7);
695 data->tolerance[i][0] = fanmodecfg & 0x0f;
696 if (sio_data->kind == nct6779) {
697 tol = nct6775_read_value(data,
698 NCT6779_REG_TOLERANCE_H[i]);
699 data->tolerance[i][0] |= (tol & 0x70) >> 1;
701 data->tolerance[i][1] =
702 nct6775_read_value(data,
703 NCT6775_REG_CRITICAL_TEMP_TOLERANCE[i]);
707 static void nct6775_update_pwm_limits(struct device *dev)
709 struct nct6775_data *data = dev_get_drvdata(dev);
710 struct nct6775_sio_data *sio_data = dev->platform_data;
714 for (i = 0; i < data->pwm_num; i++) {
715 if (!(data->has_pwm & (1 << i)))
718 data->fan_start_output[i] =
719 nct6775_read_value(data, data->REG_FAN_START_OUTPUT[i]);
720 data->fan_stop_output[i] =
721 nct6775_read_value(data, data->REG_FAN_STOP_OUTPUT[i]);
722 data->fan_stop_time[i] =
723 nct6775_read_value(data, NCT6775_REG_FAN_STOP_TIME[i]);
724 data->fan_step_up_time[i] =
725 nct6775_read_value(data, NCT6775_REG_FAN_STEP_UP_TIME[i]);
726 data->fan_step_down_time[i] =
727 nct6775_read_value(data, NCT6775_REG_FAN_STEP_DOWN_TIME[i]);
729 data->target_temp[i] =
730 nct6775_read_value(data,
731 data->REG_TARGET[i]) &
732 (data->pwm_mode[i] == 1 ? 0x7f : 0xff);
733 for (j = 0; j < data->auto_pwm_num; j++) {
734 data->auto_pwm[i][j] =
735 nct6775_read_value(data, NCT6775_REG_AUTO_PWM(i, j));
736 data->auto_temp[i][j] =
737 nct6775_read_value(data, NCT6775_REG_AUTO_TEMP(i, j));
739 /* handle critical auto_pwm temperature data */
740 data->auto_temp[i][data->auto_pwm_num] =
741 nct6775_read_value(data, NCT6775_REG_CRITICAL_TEMP[i]);
743 switch (sio_data->kind) {
745 reg = nct6775_read_value(data,
746 NCT6775_REG_CRITICAL_ENAB[i]);
747 data->auto_pwm[i][data->auto_pwm_num] =
748 (reg & 0x02) ? 0xff : 0x00;
751 data->auto_pwm[i][data->auto_pwm_num] = 0xff;
754 reg = nct6775_read_value(data,
755 NCT6779_REG_CRITICAL_PWM_ENABLE[i]);
757 data->auto_pwm[i][data->auto_pwm_num] =
758 nct6775_read_value(data,
759 NCT6779_REG_CRITICAL_PWM[i]);
761 data->auto_pwm[i][data->auto_pwm_num] = 0xff;
767 static struct nct6775_data *nct6775_update_device(struct device *dev)
769 struct nct6775_data *data = dev_get_drvdata(dev);
770 struct nct6775_sio_data *sio_data = dev->platform_data;
774 mutex_lock(&data->update_lock);
776 if (time_after(jiffies, data->last_updated + HZ + HZ/2)
778 /* Fan clock dividers */
779 nct6775_update_fan_div_common(dev, data);
781 /* Measured voltages and limits */
782 for (i = 0; i < data->in_num; i++) {
783 if (!(data->have_in & (1 << i)))
786 data->in[i] = nct6775_read_value(data,
788 data->in_min[i] = nct6775_read_value(data,
789 NCT6775_REG_IN_MIN(i));
790 data->in_max[i] = nct6775_read_value(data,
791 NCT6775_REG_IN_MAX(i));
794 /* Measured fan speeds and limits */
795 for (i = 0; i < 5; i++) {
798 if (!(data->has_fan & (1 << i)))
801 reg = nct6775_read_value(data, data->REG_FAN[i]);
802 data->rpm[i] = data->fan_from_reg(reg,
805 if (data->has_fan_min & (1 << i))
806 data->fan_min[i] = nct6775_read_value(data,
807 data->REG_FAN_MIN[i]);
810 * If we failed to measure the fan speed and clock
811 * divider can be increased, let's try that for next
814 if (data->has_fan_div
815 && (reg >= 0xff || (sio_data->kind == nct6775
817 && data->fan_div[i] < 0x07) {
818 dev_dbg(dev, "Increasing fan%d "
819 "clock divider from %u to %u\n",
820 i + 1, div_from_reg(data->fan_div[i]),
821 div_from_reg(data->fan_div[i] + 1));
823 nct6775_write_fan_div_common(dev, data, i);
824 /* Preserve min limit if possible */
825 if ((data->has_fan_min & (1 << i))
826 && data->fan_min[i] >= 2
827 && data->fan_min[i] != 255)
828 nct6775_write_value(data,
829 data->REG_FAN_MIN[i],
830 (data->fan_min[i] /= 2));
834 nct6775_update_pwm(dev);
835 nct6775_update_pwm_limits(dev);
837 /* Measured temperatures and limits */
838 for (i = 0; i < NUM_REG_TEMP; i++) {
839 if (!(data->have_temp & (1 << i)))
841 data->temp[i] = nct6775_read_temp(data,
843 if (data->reg_temp_over[i])
845 = nct6775_read_temp(data,
846 data->reg_temp_over[i]);
847 if (data->reg_temp_hyst[i])
848 data->temp_max_hyst[i]
849 = nct6775_read_temp(data,
850 data->reg_temp_hyst[i]);
853 data->alarms = nct6775_read_value(data,
854 NCT6775_REG_ALARM1) |
855 (nct6775_read_value(data,
856 NCT6775_REG_ALARM2) << 8) |
857 (nct6775_read_value(data,
858 NCT6775_REG_ALARM3) << 16);
859 if (sio_data->kind == nct6779) {
860 data->alarms |= nct6775_read_value(data,
861 NCT6779_REG_ALARM4) << 24;
864 data->caseopen = nct6775_read_value(data,
865 NCT6775_REG_CASEOPEN_DET);
867 data->last_updated = jiffies;
871 mutex_unlock(&data->update_lock);
876 * Sysfs callback functions
878 #define show_in_reg(reg) \
880 show_##reg(struct device *dev, struct device_attribute *attr, \
883 struct nct6775_data *data = nct6775_update_device(dev); \
884 struct sensor_device_attribute *sensor_attr = \
885 to_sensor_dev_attr(attr); \
886 int nr = sensor_attr->index; \
887 return sprintf(buf, "%ld\n", in_from_reg(data->reg[nr], nr)); \
893 #define store_in_reg(REG, reg) \
895 store_in_##reg(struct device *dev, struct device_attribute *attr, \
896 const char *buf, size_t count) \
898 struct nct6775_data *data = dev_get_drvdata(dev); \
899 struct sensor_device_attribute *sensor_attr = \
900 to_sensor_dev_attr(attr); \
901 int nr = sensor_attr->index; \
903 int err = kstrtoul(buf, 10, &val); \
906 mutex_lock(&data->update_lock); \
907 data->in_##reg[nr] = in_to_reg(val, nr); \
908 nct6775_write_value(data, NCT6775_REG_IN_##REG(nr), \
909 data->in_##reg[nr]); \
910 mutex_unlock(&data->update_lock); \
914 store_in_reg(MIN, min)
915 store_in_reg(MAX, max)
917 static ssize_t show_alarm(struct device *dev, struct device_attribute *attr,
920 struct nct6775_data *data = nct6775_update_device(dev);
921 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
922 int nr = sensor_attr->index;
923 return sprintf(buf, "%u\n", (data->alarms >> nr) & 0x01);
926 static SENSOR_DEVICE_ATTR(in0_input, S_IRUGO, show_in, NULL, 0);
927 static SENSOR_DEVICE_ATTR(in1_input, S_IRUGO, show_in, NULL, 1);
928 static SENSOR_DEVICE_ATTR(in2_input, S_IRUGO, show_in, NULL, 2);
929 static SENSOR_DEVICE_ATTR(in3_input, S_IRUGO, show_in, NULL, 3);
930 static SENSOR_DEVICE_ATTR(in4_input, S_IRUGO, show_in, NULL, 4);
931 static SENSOR_DEVICE_ATTR(in5_input, S_IRUGO, show_in, NULL, 5);
932 static SENSOR_DEVICE_ATTR(in6_input, S_IRUGO, show_in, NULL, 6);
933 static SENSOR_DEVICE_ATTR(in7_input, S_IRUGO, show_in, NULL, 7);
934 static SENSOR_DEVICE_ATTR(in8_input, S_IRUGO, show_in, NULL, 8);
935 static SENSOR_DEVICE_ATTR(in9_input, S_IRUGO, show_in, NULL, 9);
936 static SENSOR_DEVICE_ATTR(in10_input, S_IRUGO, show_in, NULL, 10);
937 static SENSOR_DEVICE_ATTR(in11_input, S_IRUGO, show_in, NULL, 11);
938 static SENSOR_DEVICE_ATTR(in12_input, S_IRUGO, show_in, NULL, 12);
939 static SENSOR_DEVICE_ATTR(in13_input, S_IRUGO, show_in, NULL, 13);
940 static SENSOR_DEVICE_ATTR(in14_input, S_IRUGO, show_in, NULL, 14);
942 static SENSOR_DEVICE_ATTR(in0_alarm, S_IRUGO, show_alarm, NULL, 0);
943 static SENSOR_DEVICE_ATTR(in1_alarm, S_IRUGO, show_alarm, NULL, 1);
944 static SENSOR_DEVICE_ATTR(in2_alarm, S_IRUGO, show_alarm, NULL, 2);
945 static SENSOR_DEVICE_ATTR(in3_alarm, S_IRUGO, show_alarm, NULL, 3);
946 static SENSOR_DEVICE_ATTR(in4_alarm, S_IRUGO, show_alarm, NULL, 8);
947 static SENSOR_DEVICE_ATTR(in5_alarm, S_IRUGO, show_alarm, NULL, 21);
948 static SENSOR_DEVICE_ATTR(in6_alarm, S_IRUGO, show_alarm, NULL, 20);
949 static SENSOR_DEVICE_ATTR(in7_alarm, S_IRUGO, show_alarm, NULL, 16);
950 static SENSOR_DEVICE_ATTR(in8_alarm, S_IRUGO, show_alarm, NULL, 17);
951 static SENSOR_DEVICE_ATTR(in9_alarm, S_IRUGO, show_alarm, NULL, 24);
952 static SENSOR_DEVICE_ATTR(in10_alarm, S_IRUGO, show_alarm, NULL, 25);
953 static SENSOR_DEVICE_ATTR(in11_alarm, S_IRUGO, show_alarm, NULL, 26);
954 static SENSOR_DEVICE_ATTR(in12_alarm, S_IRUGO, show_alarm, NULL, 27);
955 static SENSOR_DEVICE_ATTR(in13_alarm, S_IRUGO, show_alarm, NULL, 28);
956 static SENSOR_DEVICE_ATTR(in14_alarm, S_IRUGO, show_alarm, NULL, 29);
958 static SENSOR_DEVICE_ATTR(in0_min, S_IWUSR | S_IRUGO, show_in_min,
960 static SENSOR_DEVICE_ATTR(in1_min, S_IWUSR | S_IRUGO, show_in_min,
962 static SENSOR_DEVICE_ATTR(in2_min, S_IWUSR | S_IRUGO, show_in_min,
964 static SENSOR_DEVICE_ATTR(in3_min, S_IWUSR | S_IRUGO, show_in_min,
966 static SENSOR_DEVICE_ATTR(in4_min, S_IWUSR | S_IRUGO, show_in_min,
968 static SENSOR_DEVICE_ATTR(in5_min, S_IWUSR | S_IRUGO, show_in_min,
970 static SENSOR_DEVICE_ATTR(in6_min, S_IWUSR | S_IRUGO, show_in_min,
972 static SENSOR_DEVICE_ATTR(in7_min, S_IWUSR | S_IRUGO, show_in_min,
974 static SENSOR_DEVICE_ATTR(in8_min, S_IWUSR | S_IRUGO, show_in_min,
976 static SENSOR_DEVICE_ATTR(in9_min, S_IWUSR | S_IRUGO, show_in_min,
978 static SENSOR_DEVICE_ATTR(in10_min, S_IWUSR | S_IRUGO, show_in_min,
980 static SENSOR_DEVICE_ATTR(in11_min, S_IWUSR | S_IRUGO, show_in_min,
982 static SENSOR_DEVICE_ATTR(in12_min, S_IWUSR | S_IRUGO, show_in_min,
984 static SENSOR_DEVICE_ATTR(in13_min, S_IWUSR | S_IRUGO, show_in_min,
986 static SENSOR_DEVICE_ATTR(in14_min, S_IWUSR | S_IRUGO, show_in_min,
989 static SENSOR_DEVICE_ATTR(in0_max, S_IWUSR | S_IRUGO, show_in_max,
991 static SENSOR_DEVICE_ATTR(in1_max, S_IWUSR | S_IRUGO, show_in_max,
993 static SENSOR_DEVICE_ATTR(in2_max, S_IWUSR | S_IRUGO, show_in_max,
995 static SENSOR_DEVICE_ATTR(in3_max, S_IWUSR | S_IRUGO, show_in_max,
997 static SENSOR_DEVICE_ATTR(in4_max, S_IWUSR | S_IRUGO, show_in_max,
999 static SENSOR_DEVICE_ATTR(in5_max, S_IWUSR | S_IRUGO, show_in_max,
1001 static SENSOR_DEVICE_ATTR(in6_max, S_IWUSR | S_IRUGO, show_in_max,
1003 static SENSOR_DEVICE_ATTR(in7_max, S_IWUSR | S_IRUGO, show_in_max,
1005 static SENSOR_DEVICE_ATTR(in8_max, S_IWUSR | S_IRUGO, show_in_max,
1007 static SENSOR_DEVICE_ATTR(in9_max, S_IWUSR | S_IRUGO, show_in_max,
1009 static SENSOR_DEVICE_ATTR(in10_max, S_IWUSR | S_IRUGO, show_in_max,
1011 static SENSOR_DEVICE_ATTR(in11_max, S_IWUSR | S_IRUGO, show_in_max,
1013 static SENSOR_DEVICE_ATTR(in12_max, S_IWUSR | S_IRUGO, show_in_max,
1015 static SENSOR_DEVICE_ATTR(in13_max, S_IWUSR | S_IRUGO, show_in_max,
1017 static SENSOR_DEVICE_ATTR(in14_max, S_IWUSR | S_IRUGO, show_in_max,
1020 static struct attribute *nct6775_attributes_in[15][5] = {
1022 &sensor_dev_attr_in0_input.dev_attr.attr,
1023 &sensor_dev_attr_in0_min.dev_attr.attr,
1024 &sensor_dev_attr_in0_max.dev_attr.attr,
1025 &sensor_dev_attr_in0_alarm.dev_attr.attr,
1029 &sensor_dev_attr_in1_input.dev_attr.attr,
1030 &sensor_dev_attr_in1_min.dev_attr.attr,
1031 &sensor_dev_attr_in1_max.dev_attr.attr,
1032 &sensor_dev_attr_in1_alarm.dev_attr.attr,
1036 &sensor_dev_attr_in2_input.dev_attr.attr,
1037 &sensor_dev_attr_in2_min.dev_attr.attr,
1038 &sensor_dev_attr_in2_max.dev_attr.attr,
1039 &sensor_dev_attr_in2_alarm.dev_attr.attr,
1043 &sensor_dev_attr_in3_input.dev_attr.attr,
1044 &sensor_dev_attr_in3_min.dev_attr.attr,
1045 &sensor_dev_attr_in3_max.dev_attr.attr,
1046 &sensor_dev_attr_in3_alarm.dev_attr.attr,
1050 &sensor_dev_attr_in4_input.dev_attr.attr,
1051 &sensor_dev_attr_in4_min.dev_attr.attr,
1052 &sensor_dev_attr_in4_max.dev_attr.attr,
1053 &sensor_dev_attr_in4_alarm.dev_attr.attr,
1057 &sensor_dev_attr_in5_input.dev_attr.attr,
1058 &sensor_dev_attr_in5_min.dev_attr.attr,
1059 &sensor_dev_attr_in5_max.dev_attr.attr,
1060 &sensor_dev_attr_in5_alarm.dev_attr.attr,
1064 &sensor_dev_attr_in6_input.dev_attr.attr,
1065 &sensor_dev_attr_in6_min.dev_attr.attr,
1066 &sensor_dev_attr_in6_max.dev_attr.attr,
1067 &sensor_dev_attr_in6_alarm.dev_attr.attr,
1071 &sensor_dev_attr_in7_input.dev_attr.attr,
1072 &sensor_dev_attr_in7_min.dev_attr.attr,
1073 &sensor_dev_attr_in7_max.dev_attr.attr,
1074 &sensor_dev_attr_in7_alarm.dev_attr.attr,
1078 &sensor_dev_attr_in8_input.dev_attr.attr,
1079 &sensor_dev_attr_in8_min.dev_attr.attr,
1080 &sensor_dev_attr_in8_max.dev_attr.attr,
1081 &sensor_dev_attr_in8_alarm.dev_attr.attr,
1085 &sensor_dev_attr_in9_input.dev_attr.attr,
1086 &sensor_dev_attr_in9_min.dev_attr.attr,
1087 &sensor_dev_attr_in9_max.dev_attr.attr,
1088 &sensor_dev_attr_in9_alarm.dev_attr.attr,
1092 &sensor_dev_attr_in10_input.dev_attr.attr,
1093 &sensor_dev_attr_in10_min.dev_attr.attr,
1094 &sensor_dev_attr_in10_max.dev_attr.attr,
1095 &sensor_dev_attr_in10_alarm.dev_attr.attr,
1099 &sensor_dev_attr_in11_input.dev_attr.attr,
1100 &sensor_dev_attr_in11_min.dev_attr.attr,
1101 &sensor_dev_attr_in11_max.dev_attr.attr,
1102 &sensor_dev_attr_in11_alarm.dev_attr.attr,
1106 &sensor_dev_attr_in12_input.dev_attr.attr,
1107 &sensor_dev_attr_in12_min.dev_attr.attr,
1108 &sensor_dev_attr_in12_max.dev_attr.attr,
1109 &sensor_dev_attr_in12_alarm.dev_attr.attr,
1113 &sensor_dev_attr_in13_input.dev_attr.attr,
1114 &sensor_dev_attr_in13_min.dev_attr.attr,
1115 &sensor_dev_attr_in13_max.dev_attr.attr,
1116 &sensor_dev_attr_in13_alarm.dev_attr.attr,
1120 &sensor_dev_attr_in14_input.dev_attr.attr,
1121 &sensor_dev_attr_in14_min.dev_attr.attr,
1122 &sensor_dev_attr_in14_max.dev_attr.attr,
1123 &sensor_dev_attr_in14_alarm.dev_attr.attr,
1128 static const struct attribute_group nct6775_group_in[15] = {
1129 { .attrs = nct6775_attributes_in[0] },
1130 { .attrs = nct6775_attributes_in[1] },
1131 { .attrs = nct6775_attributes_in[2] },
1132 { .attrs = nct6775_attributes_in[3] },
1133 { .attrs = nct6775_attributes_in[4] },
1134 { .attrs = nct6775_attributes_in[5] },
1135 { .attrs = nct6775_attributes_in[6] },
1136 { .attrs = nct6775_attributes_in[7] },
1137 { .attrs = nct6775_attributes_in[8] },
1138 { .attrs = nct6775_attributes_in[9] },
1139 { .attrs = nct6775_attributes_in[10] },
1140 { .attrs = nct6775_attributes_in[11] },
1141 { .attrs = nct6775_attributes_in[12] },
1142 { .attrs = nct6775_attributes_in[13] },
1143 { .attrs = nct6775_attributes_in[14] },
1147 show_fan(struct device *dev, struct device_attribute *attr, char *buf)
1149 struct nct6775_data *data = nct6775_update_device(dev);
1150 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
1151 int nr = sensor_attr->index;
1152 return sprintf(buf, "%d\n", data->rpm[nr]);
1156 show_fan_min(struct device *dev, struct device_attribute *attr, char *buf)
1158 struct nct6775_data *data = nct6775_update_device(dev);
1159 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
1160 int nr = sensor_attr->index;
1161 return sprintf(buf, "%d\n",
1162 data->fan_from_reg_min(data->fan_min[nr],
1163 data->fan_div[nr]));
1167 show_fan_div(struct device *dev, struct device_attribute *attr,
1170 struct nct6775_data *data = nct6775_update_device(dev);
1171 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
1172 int nr = sensor_attr->index;
1173 return sprintf(buf, "%u\n", div_from_reg(data->fan_div[nr]));
1177 store_fan_min(struct device *dev, struct device_attribute *attr,
1178 const char *buf, size_t count)
1180 struct nct6775_data *data = dev_get_drvdata(dev);
1181 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
1182 int nr = sensor_attr->index;
1188 err = kstrtoul(buf, 10, &val);
1192 mutex_lock(&data->update_lock);
1193 if (!data->has_fan_div) {
1195 * Only NCT6776F for now, so we know that this is a 13 bit
1203 val = 1350000U / val;
1204 val = (val & 0x1f) | ((val << 3) & 0xff00);
1206 data->fan_min[nr] = val;
1207 goto write_min; /* Leave fan divider alone */
1210 /* No min limit, alarm disabled */
1211 data->fan_min[nr] = 255;
1212 new_div = data->fan_div[nr]; /* No change */
1213 dev_info(dev, "fan%u low limit and alarm disabled\n", nr + 1);
1216 reg = 1350000U / val;
1217 if (reg >= 128 * 255) {
1219 * Speed below this value cannot possibly be represented,
1220 * even with the highest divider (128)
1222 data->fan_min[nr] = 254;
1223 new_div = 7; /* 128 == (1 << 7) */
1224 dev_warn(dev, "fan%u low limit %lu below minimum %u, set to "
1225 "minimum\n", nr + 1, val,
1226 data->fan_from_reg_min(254, 7));
1229 * Speed above this value cannot possibly be represented,
1230 * even with the lowest divider (1)
1232 data->fan_min[nr] = 1;
1233 new_div = 0; /* 1 == (1 << 0) */
1234 dev_warn(dev, "fan%u low limit %lu above maximum %u, set to "
1235 "maximum\n", nr + 1, val,
1236 data->fan_from_reg_min(1, 0));
1239 * Automatically pick the best divider, i.e. the one such
1240 * that the min limit will correspond to a register value
1241 * in the 96..192 range
1244 while (reg > 192 && new_div < 7) {
1248 data->fan_min[nr] = reg;
1253 * Write both the fan clock divider (if it changed) and the new
1254 * fan min (unconditionally)
1256 if (new_div != data->fan_div[nr]) {
1257 dev_dbg(dev, "fan%u clock divider changed from %u to %u\n",
1258 nr + 1, div_from_reg(data->fan_div[nr]),
1259 div_from_reg(new_div));
1260 data->fan_div[nr] = new_div;
1261 nct6775_write_fan_div_common(dev, data, nr);
1262 /* Give the chip time to sample a new speed value */
1263 data->last_updated = jiffies;
1266 nct6775_write_value(data, data->REG_FAN_MIN[nr],
1268 mutex_unlock(&data->update_lock);
1273 static struct sensor_device_attribute sda_fan_input[] = {
1274 SENSOR_ATTR(fan1_input, S_IRUGO, show_fan, NULL, 0),
1275 SENSOR_ATTR(fan2_input, S_IRUGO, show_fan, NULL, 1),
1276 SENSOR_ATTR(fan3_input, S_IRUGO, show_fan, NULL, 2),
1277 SENSOR_ATTR(fan4_input, S_IRUGO, show_fan, NULL, 3),
1278 SENSOR_ATTR(fan5_input, S_IRUGO, show_fan, NULL, 4),
1281 static struct sensor_device_attribute sda_fan_alarm[] = {
1282 SENSOR_ATTR(fan1_alarm, S_IRUGO, show_alarm, NULL, 6),
1283 SENSOR_ATTR(fan2_alarm, S_IRUGO, show_alarm, NULL, 7),
1284 SENSOR_ATTR(fan3_alarm, S_IRUGO, show_alarm, NULL, 11),
1285 SENSOR_ATTR(fan4_alarm, S_IRUGO, show_alarm, NULL, 10),
1286 SENSOR_ATTR(fan5_alarm, S_IRUGO, show_alarm, NULL, 23),
1289 static struct sensor_device_attribute sda_fan_min[] = {
1290 SENSOR_ATTR(fan1_min, S_IWUSR | S_IRUGO, show_fan_min,
1292 SENSOR_ATTR(fan2_min, S_IWUSR | S_IRUGO, show_fan_min,
1294 SENSOR_ATTR(fan3_min, S_IWUSR | S_IRUGO, show_fan_min,
1296 SENSOR_ATTR(fan4_min, S_IWUSR | S_IRUGO, show_fan_min,
1298 SENSOR_ATTR(fan5_min, S_IWUSR | S_IRUGO, show_fan_min,
1302 static struct sensor_device_attribute sda_fan_div[] = {
1303 SENSOR_ATTR(fan1_div, S_IRUGO, show_fan_div, NULL, 0),
1304 SENSOR_ATTR(fan2_div, S_IRUGO, show_fan_div, NULL, 1),
1305 SENSOR_ATTR(fan3_div, S_IRUGO, show_fan_div, NULL, 2),
1306 SENSOR_ATTR(fan4_div, S_IRUGO, show_fan_div, NULL, 3),
1307 SENSOR_ATTR(fan5_div, S_IRUGO, show_fan_div, NULL, 4),
1311 show_temp_label(struct device *dev, struct device_attribute *attr, char *buf)
1313 struct nct6775_data *data = nct6775_update_device(dev);
1314 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
1315 int nr = sensor_attr->index;
1316 return sprintf(buf, "%s\n", data->temp_label[data->temp_src[nr]]);
1319 #define show_temp_reg(addr, reg) \
1321 show_##reg(struct device *dev, struct device_attribute *attr, \
1324 struct nct6775_data *data = nct6775_update_device(dev); \
1325 struct sensor_device_attribute *sensor_attr = \
1326 to_sensor_dev_attr(attr); \
1327 int nr = sensor_attr->index; \
1328 return sprintf(buf, "%d\n", LM75_TEMP_FROM_REG(data->reg[nr])); \
1330 show_temp_reg(reg_temp, temp);
1331 show_temp_reg(reg_temp_over, temp_max);
1332 show_temp_reg(reg_temp_hyst, temp_max_hyst);
1334 #define store_temp_reg(addr, reg) \
1336 store_##reg(struct device *dev, struct device_attribute *attr, \
1337 const char *buf, size_t count) \
1339 struct nct6775_data *data = dev_get_drvdata(dev); \
1340 struct sensor_device_attribute *sensor_attr = \
1341 to_sensor_dev_attr(attr); \
1342 int nr = sensor_attr->index; \
1345 err = kstrtol(buf, 10, &val); \
1348 mutex_lock(&data->update_lock); \
1349 data->reg[nr] = LM75_TEMP_TO_REG(val); \
1350 nct6775_write_temp(data, data->addr[nr], data->reg[nr]); \
1351 mutex_unlock(&data->update_lock); \
1354 store_temp_reg(reg_temp_over, temp_max);
1355 store_temp_reg(reg_temp_hyst, temp_max_hyst);
1358 show_temp_type(struct device *dev, struct device_attribute *attr, char *buf)
1360 struct nct6775_data *data = nct6775_update_device(dev);
1361 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
1362 int nr = sensor_attr->index;
1363 return sprintf(buf, "%d\n", (int)data->temp_type[nr]);
1366 static struct sensor_device_attribute sda_temp_input[] = {
1367 SENSOR_ATTR(temp1_input, S_IRUGO, show_temp, NULL, 0),
1368 SENSOR_ATTR(temp2_input, S_IRUGO, show_temp, NULL, 1),
1369 SENSOR_ATTR(temp3_input, S_IRUGO, show_temp, NULL, 2),
1370 SENSOR_ATTR(temp4_input, S_IRUGO, show_temp, NULL, 3),
1371 SENSOR_ATTR(temp5_input, S_IRUGO, show_temp, NULL, 4),
1372 SENSOR_ATTR(temp6_input, S_IRUGO, show_temp, NULL, 5),
1373 SENSOR_ATTR(temp7_input, S_IRUGO, show_temp, NULL, 6),
1374 SENSOR_ATTR(temp8_input, S_IRUGO, show_temp, NULL, 7),
1375 SENSOR_ATTR(temp9_input, S_IRUGO, show_temp, NULL, 8),
1376 SENSOR_ATTR(temp10_input, S_IRUGO, show_temp, NULL, 9),
1377 SENSOR_ATTR(temp11_input, S_IRUGO, show_temp, NULL, 10),
1380 static struct sensor_device_attribute sda_temp_label[] = {
1381 SENSOR_ATTR(temp1_label, S_IRUGO, show_temp_label, NULL, 0),
1382 SENSOR_ATTR(temp2_label, S_IRUGO, show_temp_label, NULL, 1),
1383 SENSOR_ATTR(temp3_label, S_IRUGO, show_temp_label, NULL, 2),
1384 SENSOR_ATTR(temp4_label, S_IRUGO, show_temp_label, NULL, 3),
1385 SENSOR_ATTR(temp5_label, S_IRUGO, show_temp_label, NULL, 4),
1386 SENSOR_ATTR(temp6_label, S_IRUGO, show_temp_label, NULL, 5),
1389 static struct sensor_device_attribute sda_temp_max[] = {
1390 SENSOR_ATTR(temp1_max, S_IRUGO | S_IWUSR, show_temp_max,
1392 SENSOR_ATTR(temp2_max, S_IRUGO | S_IWUSR, show_temp_max,
1394 SENSOR_ATTR(temp3_max, S_IRUGO | S_IWUSR, show_temp_max,
1396 SENSOR_ATTR(temp4_max, S_IRUGO | S_IWUSR, show_temp_max,
1398 SENSOR_ATTR(temp5_max, S_IRUGO | S_IWUSR, show_temp_max,
1400 SENSOR_ATTR(temp6_max, S_IRUGO | S_IWUSR, show_temp_max,
1404 static struct sensor_device_attribute sda_temp_max_hyst[] = {
1405 SENSOR_ATTR(temp1_max_hyst, S_IRUGO | S_IWUSR, show_temp_max_hyst,
1406 store_temp_max_hyst, 0),
1407 SENSOR_ATTR(temp2_max_hyst, S_IRUGO | S_IWUSR, show_temp_max_hyst,
1408 store_temp_max_hyst, 1),
1409 SENSOR_ATTR(temp3_max_hyst, S_IRUGO | S_IWUSR, show_temp_max_hyst,
1410 store_temp_max_hyst, 2),
1411 SENSOR_ATTR(temp4_max_hyst, S_IRUGO | S_IWUSR, show_temp_max_hyst,
1412 store_temp_max_hyst, 3),
1413 SENSOR_ATTR(temp5_max_hyst, S_IRUGO | S_IWUSR, show_temp_max_hyst,
1414 store_temp_max_hyst, 4),
1415 SENSOR_ATTR(temp6_max_hyst, S_IRUGO | S_IWUSR, show_temp_max_hyst,
1416 store_temp_max_hyst, 5),
1419 static struct sensor_device_attribute sda_temp_alarm[] = {
1420 SENSOR_ATTR(temp1_alarm, S_IRUGO, show_alarm, NULL, 4),
1421 SENSOR_ATTR(temp2_alarm, S_IRUGO, show_alarm, NULL, 5),
1422 SENSOR_ATTR(temp3_alarm, S_IRUGO, show_alarm, NULL, 13),
1425 static struct sensor_device_attribute sda_temp_type[] = {
1426 SENSOR_ATTR(temp1_type, S_IRUGO, show_temp_type, NULL, 0),
1427 SENSOR_ATTR(temp2_type, S_IRUGO, show_temp_type, NULL, 1),
1428 SENSOR_ATTR(temp3_type, S_IRUGO, show_temp_type, NULL, 2),
1431 #define show_pwm_reg(reg) \
1432 static ssize_t show_##reg(struct device *dev, struct device_attribute *attr, \
1435 struct nct6775_data *data = nct6775_update_device(dev); \
1436 struct sensor_device_attribute *sensor_attr = \
1437 to_sensor_dev_attr(attr); \
1438 int nr = sensor_attr->index; \
1439 return sprintf(buf, "%d\n", data->reg[nr]); \
1442 show_pwm_reg(pwm_mode)
1443 show_pwm_reg(pwm_enable)
1445 static ssize_t show_pwm(struct device *dev, struct device_attribute *attr,
1448 struct nct6775_data *data = nct6775_update_device(dev);
1449 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
1450 int nr = sensor_attr->index;
1453 * For automatic fan control modes, show current pwm readings.
1454 * Otherwise, show the configured value.
1456 return sprintf(buf, "%d\n",
1457 data->pwm_mode[nr] > 1 ?
1459 nct6775_read_value(data,
1460 NCT6775_REG_PWM_READ[nr]));
1464 store_pwm_mode(struct device *dev, struct device_attribute *attr,
1465 const char *buf, size_t count)
1467 struct nct6775_data *data = dev_get_drvdata(dev);
1468 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
1469 struct nct6775_sio_data *sio_data = dev->platform_data;
1470 int nr = sensor_attr->index;
1475 err = kstrtoul(buf, 10, &val);
1482 /* On NCT6776F and NCT6779D, DC mode is only supported for pwm1 */
1483 if ((sio_data->kind == nct6776 || sio_data->kind == nct6779) && nr) {
1489 mutex_lock(&data->update_lock);
1490 reg = nct6775_read_value(data, NCT6775_REG_PWM_ENABLE[nr]);
1491 data->pwm_mode[nr] = val;
1492 reg &= ~(1 << NCT6775_PWM_MODE_SHIFT[nr]);
1494 reg |= 1 << NCT6775_PWM_MODE_SHIFT[nr];
1495 nct6775_write_value(data, NCT6775_REG_PWM_ENABLE[nr], reg);
1496 mutex_unlock(&data->update_lock);
1501 store_pwm(struct device *dev, struct device_attribute *attr,
1502 const char *buf, size_t count)
1504 struct nct6775_data *data = dev_get_drvdata(dev);
1505 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
1506 int nr = sensor_attr->index;
1510 err = kstrtoul(buf, 10, &val);
1514 val = SENSORS_LIMIT(val, 0, 255);
1516 mutex_lock(&data->update_lock);
1517 data->pwm[nr] = val;
1518 nct6775_write_value(data, NCT6775_REG_PWM[nr], val);
1519 mutex_unlock(&data->update_lock);
1523 /* Returns 0 if OK, -EINVAL otherwise */
1524 static int check_trip_points(struct nct6775_data *data, int nr)
1528 for (i = 0; i < data->auto_pwm_num - 1; i++) {
1529 if (data->auto_temp[nr][i] > data->auto_temp[nr][i + 1])
1532 for (i = 0; i < data->auto_pwm_num - 1; i++) {
1533 if (data->auto_pwm[nr][i] > data->auto_pwm[nr][i + 1])
1536 /* validate critical temperature and pwm if enabled (pwm > 0) */
1537 if (data->auto_pwm[nr][data->auto_pwm_num]) {
1538 if (data->auto_temp[nr][data->auto_pwm_num - 1] >
1539 data->auto_temp[nr][data->auto_pwm_num] ||
1540 data->auto_pwm[nr][data->auto_pwm_num - 1] >
1541 data->auto_pwm[nr][data->auto_pwm_num])
1548 store_pwm_enable(struct device *dev, struct device_attribute *attr,
1549 const char *buf, size_t count)
1551 struct nct6775_data *data = dev_get_drvdata(dev);
1552 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
1553 int nr = sensor_attr->index;
1558 err = kstrtoul(buf, 10, &val);
1565 if (val == 4 && check_trip_points(data, nr)) {
1566 dev_err(dev, "Inconsistent trip points, not switching to SmartFan IV mode\n");
1567 dev_err(dev, "Adjust trip points and try again\n");
1571 mutex_lock(&data->update_lock);
1572 data->pwm_enable[nr] = val;
1575 * turn off pwm control: select manual mode, set pwm to maximum
1577 data->pwm[nr] = 255;
1578 nct6775_write_value(data, NCT6775_REG_PWM[nr], 255);
1580 reg = nct6775_read_value(data, NCT6775_REG_FAN_MODE[nr]);
1582 reg |= (pwm_enable_to_reg(val) << 4);
1583 nct6775_write_value(data, NCT6775_REG_FAN_MODE[nr], reg);
1584 mutex_unlock(&data->update_lock);
1589 show_pwm_temp_src(struct device *dev, struct device_attribute *attr, char *buf)
1591 struct nct6775_data *data = nct6775_update_device(dev);
1592 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
1593 int nr = sensor_attr->index;
1594 return sprintf(buf, "%d\n", data->temp_src[nr]);
1598 store_pwm_temp_src(struct device *dev, struct device_attribute *attr,
1599 const char *buf, size_t count)
1601 struct nct6775_data *data = nct6775_update_device(dev);
1602 struct nct6775_sio_data *sio_data = dev->platform_data;
1603 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
1604 int nr = sensor_attr->index;
1608 static const int max_src[] = {
1609 ARRAY_SIZE(nct6775_temp_label),
1610 ARRAY_SIZE(nct6776_temp_label),
1611 ARRAY_SIZE(nct6779_temp_label)
1614 err = kstrtoul(buf, 10, &val);
1617 if (val == 0 || val >= max_src[sio_data->kind])
1620 if (!strlen(data->temp_label[val]))
1623 mutex_lock(&data->update_lock);
1624 data->temp_src[nr] = val;
1625 reg = nct6775_read_value(data, NCT6775_REG_TEMP_SOURCE[nr]);
1628 nct6775_write_value(data, NCT6775_REG_TEMP_SOURCE[nr], reg);
1629 mutex_unlock(&data->update_lock);
1635 show_target_temp(struct device *dev, struct device_attribute *attr, char *buf)
1637 struct nct6775_data *data = nct6775_update_device(dev);
1638 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
1640 return sprintf(buf, "%d\n",
1641 data->target_temp[sensor_attr->index] * 1000);
1645 store_target_temp(struct device *dev, struct device_attribute *attr,
1646 const char *buf, size_t count)
1648 struct nct6775_data *data = dev_get_drvdata(dev);
1649 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
1650 int nr = sensor_attr->index;
1654 err = kstrtol(buf, 10, &val);
1658 val = SENSORS_LIMIT(DIV_ROUND_CLOSEST(val, 1000), 0, 127);
1660 mutex_lock(&data->update_lock);
1661 data->target_temp[nr] = val;
1662 nct6775_write_value(data, data->REG_TARGET[nr], val);
1663 mutex_unlock(&data->update_lock);
1668 show_auto_temp_hyst(struct device *dev, struct device_attribute *attr,
1671 struct nct6775_data *data = nct6775_update_device(dev);
1672 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
1674 int point = sattr->index >= data->auto_pwm_num ? 1 : 0;
1675 int tolerance = data->tolerance[nr][point];
1676 int temp = data->auto_temp[nr][sattr->index];
1678 return sprintf(buf, "%d\n", (temp - tolerance) * 1000);
1682 store_auto_temp_hyst(struct device *dev, struct device_attribute *attr,
1683 const char *buf, size_t count)
1685 struct nct6775_data *data = dev_get_drvdata(dev);
1686 struct nct6775_sio_data *sio_data = dev->platform_data;
1687 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
1689 int point = sattr->index >= data->auto_pwm_num ? 1 : 0;
1693 int maxlimit[2][3] = { { 15, 7, 63 }, { 15, 7, 7 } };
1694 int mask[] = { 0x0f, 0x07, 0x07 };
1697 err = kstrtol(buf, 10, &val);
1701 temp = data->auto_temp[nr][sattr->index];
1702 val = temp - DIV_ROUND_CLOSEST(val, 1000);
1704 /* Limit tolerance as needed */
1705 val = SENSORS_LIMIT(val, 0, maxlimit[point][sio_data->kind]);
1707 mutex_lock(&data->update_lock);
1709 nct6775_write_value(data,
1710 NCT6775_REG_CRITICAL_TEMP_TOLERANCE[nr],
1713 reg = nct6775_read_value(data, NCT6775_REG_FAN_MODE[nr]);
1714 reg = (reg & ~mask[nr]) | (val & mask[nr]);
1715 nct6775_write_value(data, NCT6775_REG_FAN_MODE[nr], reg);
1716 if (sio_data->kind == nct6779) {
1717 reg = nct6775_read_value(data,
1718 NCT6779_REG_TOLERANCE_H[nr]);
1719 reg = (reg & 0x70) | ((val & 0x38) << 1);
1720 nct6775_write_value(data,
1721 NCT6779_REG_TOLERANCE_H[nr], reg);
1725 data->tolerance[nr][point] = val;
1726 mutex_unlock(&data->update_lock);
1730 static SENSOR_DEVICE_ATTR(pwm1, S_IWUSR | S_IRUGO, show_pwm, store_pwm, 0);
1731 static SENSOR_DEVICE_ATTR(pwm2, S_IWUSR | S_IRUGO, show_pwm, store_pwm, 1);
1732 static SENSOR_DEVICE_ATTR(pwm3, S_IWUSR | S_IRUGO, show_pwm, store_pwm, 2);
1733 static SENSOR_DEVICE_ATTR(pwm4, S_IWUSR | S_IRUGO, show_pwm, store_pwm, 3);
1734 static SENSOR_DEVICE_ATTR(pwm5, S_IWUSR | S_IRUGO, show_pwm, store_pwm, 4);
1736 static SENSOR_DEVICE_ATTR(pwm1_mode, S_IWUSR | S_IRUGO, show_pwm_mode,
1738 static SENSOR_DEVICE_ATTR(pwm2_mode, S_IWUSR | S_IRUGO, show_pwm_mode,
1740 static SENSOR_DEVICE_ATTR(pwm3_mode, S_IWUSR | S_IRUGO, show_pwm_mode,
1742 static SENSOR_DEVICE_ATTR(pwm4_mode, S_IWUSR | S_IRUGO, show_pwm_mode,
1744 static SENSOR_DEVICE_ATTR(pwm5_mode, S_IWUSR | S_IRUGO, show_pwm_mode,
1747 static SENSOR_DEVICE_ATTR(pwm1_enable, S_IWUSR | S_IRUGO, show_pwm_enable,
1748 store_pwm_enable, 0);
1749 static SENSOR_DEVICE_ATTR(pwm2_enable, S_IWUSR | S_IRUGO, show_pwm_enable,
1750 store_pwm_enable, 1);
1751 static SENSOR_DEVICE_ATTR(pwm3_enable, S_IWUSR | S_IRUGO, show_pwm_enable,
1752 store_pwm_enable, 2);
1753 static SENSOR_DEVICE_ATTR(pwm4_enable, S_IWUSR | S_IRUGO, show_pwm_enable,
1754 store_pwm_enable, 3);
1755 static SENSOR_DEVICE_ATTR(pwm5_enable, S_IWUSR | S_IRUGO, show_pwm_enable,
1756 store_pwm_enable, 4);
1758 static SENSOR_DEVICE_ATTR(pwm1_temp_src, S_IWUSR | S_IRUGO, show_pwm_temp_src,
1759 store_pwm_temp_src, 6);
1760 static SENSOR_DEVICE_ATTR(pwm2_temp_src, S_IWUSR | S_IRUGO, show_pwm_temp_src,
1761 store_pwm_temp_src, 7);
1762 static SENSOR_DEVICE_ATTR(pwm3_temp_src, S_IWUSR | S_IRUGO, show_pwm_temp_src,
1763 store_pwm_temp_src, 8);
1764 static SENSOR_DEVICE_ATTR(pwm4_temp_src, S_IWUSR | S_IRUGO, show_pwm_temp_src,
1765 store_pwm_temp_src, 9);
1766 static SENSOR_DEVICE_ATTR(pwm5_temp_src, S_IWUSR | S_IRUGO, show_pwm_temp_src,
1767 store_pwm_temp_src, 10);
1769 static SENSOR_DEVICE_ATTR(pwm1_target, S_IWUSR | S_IRUGO, show_target_temp,
1770 store_target_temp, 0);
1771 static SENSOR_DEVICE_ATTR(pwm2_target, S_IWUSR | S_IRUGO, show_target_temp,
1772 store_target_temp, 1);
1773 static SENSOR_DEVICE_ATTR(pwm3_target, S_IWUSR | S_IRUGO, show_target_temp,
1774 store_target_temp, 2);
1775 static SENSOR_DEVICE_ATTR(pwm4_target, S_IWUSR | S_IRUGO, show_target_temp,
1776 store_target_temp, 3);
1777 static SENSOR_DEVICE_ATTR(pwm5_target, S_IWUSR | S_IRUGO, show_target_temp,
1778 store_target_temp, 4);
1780 /* Smart Fan registers */
1782 #define fan_functions(reg, REG) \
1783 static ssize_t show_##reg(struct device *dev, struct device_attribute *attr, \
1786 struct nct6775_data *data = nct6775_update_device(dev); \
1787 struct sensor_device_attribute *sensor_attr = \
1788 to_sensor_dev_attr(attr); \
1789 int nr = sensor_attr->index; \
1790 return sprintf(buf, "%d\n", data->reg[nr]); \
1793 store_##reg(struct device *dev, struct device_attribute *attr, \
1794 const char *buf, size_t count) \
1796 struct nct6775_data *data = dev_get_drvdata(dev); \
1797 struct sensor_device_attribute *sensor_attr = \
1798 to_sensor_dev_attr(attr); \
1799 int nr = sensor_attr->index; \
1800 unsigned long val; \
1802 err = kstrtoul(buf, 10, &val); \
1805 val = SENSORS_LIMIT(val, 1, 255); \
1806 mutex_lock(&data->update_lock); \
1807 data->reg[nr] = val; \
1808 nct6775_write_value(data, data->REG_##REG[nr], val); \
1809 mutex_unlock(&data->update_lock); \
1813 fan_functions(fan_start_output, FAN_START_OUTPUT)
1814 fan_functions(fan_stop_output, FAN_STOP_OUTPUT)
1816 #define fan_step_functions(reg, REG) \
1817 static ssize_t show_##reg(struct device *dev, \
1818 struct device_attribute *attr, char *buf) \
1820 struct nct6775_data *data = nct6775_update_device(dev); \
1821 struct sensor_device_attribute *sensor_attr = \
1822 to_sensor_dev_attr(attr); \
1823 int nr = sensor_attr->index; \
1824 return sprintf(buf, "%d\n", \
1825 step_time_from_reg(data->reg[nr], \
1826 data->pwm_mode[nr])); \
1828 static ssize_t store_##reg(struct device *dev, \
1829 struct device_attribute *attr, \
1830 const char *buf, size_t count) \
1832 struct nct6775_data *data = dev_get_drvdata(dev); \
1833 struct sensor_device_attribute *sensor_attr = \
1834 to_sensor_dev_attr(attr); \
1835 int nr = sensor_attr->index; \
1836 unsigned long val; \
1838 err = kstrtoul(buf, 10, &val); \
1841 val = step_time_to_reg(val, data->pwm_mode[nr]); \
1842 mutex_lock(&data->update_lock); \
1843 data->reg[nr] = val; \
1844 nct6775_write_value(data, NCT6775_REG_##REG[nr], val); \
1845 mutex_unlock(&data->update_lock); \
1849 fan_step_functions(fan_stop_time, FAN_STOP_TIME)
1850 fan_step_functions(fan_step_up_time, FAN_STEP_UP_TIME)
1851 fan_step_functions(fan_step_down_time, FAN_STEP_DOWN_TIME)
1853 static ssize_t show_name(struct device *dev, struct device_attribute *attr,
1856 struct nct6775_data *data = dev_get_drvdata(dev);
1858 return sprintf(buf, "%s\n", data->name);
1860 static DEVICE_ATTR(name, S_IRUGO, show_name, NULL);
1862 static SENSOR_DEVICE_ATTR(pwm1_stop_time, S_IWUSR | S_IRUGO, show_fan_stop_time,
1863 store_fan_stop_time, 0);
1864 static SENSOR_DEVICE_ATTR(pwm2_stop_time, S_IWUSR | S_IRUGO, show_fan_stop_time,
1865 store_fan_stop_time, 1);
1866 static SENSOR_DEVICE_ATTR(pwm3_stop_time, S_IWUSR | S_IRUGO, show_fan_stop_time,
1867 store_fan_stop_time, 2);
1868 static SENSOR_DEVICE_ATTR(pwm4_stop_time, S_IWUSR | S_IRUGO, show_fan_stop_time,
1869 store_fan_stop_time, 3);
1870 static SENSOR_DEVICE_ATTR(pwm5_stop_time, S_IWUSR | S_IRUGO, show_fan_stop_time,
1871 store_fan_stop_time, 4);
1873 static SENSOR_DEVICE_ATTR(pwm1_step_up_time, S_IWUSR | S_IRUGO,
1874 show_fan_step_up_time, store_fan_step_up_time, 0);
1875 static SENSOR_DEVICE_ATTR(pwm2_step_up_time, S_IWUSR | S_IRUGO,
1876 show_fan_step_up_time, store_fan_step_up_time, 1);
1877 static SENSOR_DEVICE_ATTR(pwm3_step_up_time, S_IWUSR | S_IRUGO,
1878 show_fan_step_up_time, store_fan_step_up_time, 2);
1879 static SENSOR_DEVICE_ATTR(pwm4_step_up_time, S_IWUSR | S_IRUGO,
1880 show_fan_step_up_time, store_fan_step_up_time, 3);
1881 static SENSOR_DEVICE_ATTR(pwm5_step_up_time, S_IWUSR | S_IRUGO,
1882 show_fan_step_up_time, store_fan_step_up_time, 4);
1884 static SENSOR_DEVICE_ATTR(pwm1_step_down_time, S_IWUSR | S_IRUGO,
1885 show_fan_step_down_time, store_fan_step_down_time, 0);
1886 static SENSOR_DEVICE_ATTR(pwm2_step_down_time, S_IWUSR | S_IRUGO,
1887 show_fan_step_down_time, store_fan_step_down_time, 1);
1888 static SENSOR_DEVICE_ATTR(pwm3_step_down_time, S_IWUSR | S_IRUGO,
1889 show_fan_step_down_time, store_fan_step_down_time, 2);
1890 static SENSOR_DEVICE_ATTR(pwm4_step_down_time, S_IWUSR | S_IRUGO,
1891 show_fan_step_down_time, store_fan_step_down_time, 3);
1892 static SENSOR_DEVICE_ATTR(pwm5_step_down_time, S_IWUSR | S_IRUGO,
1893 show_fan_step_down_time, store_fan_step_down_time, 4);
1895 static SENSOR_DEVICE_ATTR(pwm1_start_output, S_IWUSR | S_IRUGO,
1896 show_fan_start_output, store_fan_start_output, 0);
1897 static SENSOR_DEVICE_ATTR(pwm2_start_output, S_IWUSR | S_IRUGO,
1898 show_fan_start_output, store_fan_start_output, 1);
1899 static SENSOR_DEVICE_ATTR(pwm3_start_output, S_IWUSR | S_IRUGO,
1900 show_fan_start_output, store_fan_start_output, 2);
1901 static SENSOR_DEVICE_ATTR(pwm4_start_output, S_IWUSR | S_IRUGO,
1902 show_fan_start_output, store_fan_start_output, 3);
1903 static SENSOR_DEVICE_ATTR(pwm5_start_output, S_IWUSR | S_IRUGO,
1904 show_fan_start_output, store_fan_start_output, 4);
1906 static SENSOR_DEVICE_ATTR(pwm1_stop_output, S_IWUSR | S_IRUGO,
1907 show_fan_stop_output, store_fan_stop_output, 0);
1908 static SENSOR_DEVICE_ATTR(pwm2_stop_output, S_IWUSR | S_IRUGO,
1909 show_fan_stop_output, store_fan_stop_output, 1);
1910 static SENSOR_DEVICE_ATTR(pwm3_stop_output, S_IWUSR | S_IRUGO,
1911 show_fan_stop_output, store_fan_stop_output, 2);
1912 static SENSOR_DEVICE_ATTR(pwm4_stop_output, S_IWUSR | S_IRUGO,
1913 show_fan_stop_output, store_fan_stop_output, 3);
1914 static SENSOR_DEVICE_ATTR(pwm5_stop_output, S_IWUSR | S_IRUGO,
1915 show_fan_stop_output, store_fan_stop_output, 4);
1917 static struct attribute *nct6775_attributes_pwm[5][11] = {
1919 &sensor_dev_attr_pwm1.dev_attr.attr,
1920 &sensor_dev_attr_pwm1_mode.dev_attr.attr,
1921 &sensor_dev_attr_pwm1_enable.dev_attr.attr,
1922 &sensor_dev_attr_pwm1_temp_src.dev_attr.attr,
1923 &sensor_dev_attr_pwm1_target.dev_attr.attr,
1924 &sensor_dev_attr_pwm1_stop_time.dev_attr.attr,
1925 &sensor_dev_attr_pwm1_step_up_time.dev_attr.attr,
1926 &sensor_dev_attr_pwm1_step_down_time.dev_attr.attr,
1927 &sensor_dev_attr_pwm1_start_output.dev_attr.attr,
1928 &sensor_dev_attr_pwm1_stop_output.dev_attr.attr,
1932 &sensor_dev_attr_pwm2.dev_attr.attr,
1933 &sensor_dev_attr_pwm2_mode.dev_attr.attr,
1934 &sensor_dev_attr_pwm2_enable.dev_attr.attr,
1935 &sensor_dev_attr_pwm2_temp_src.dev_attr.attr,
1936 &sensor_dev_attr_pwm2_target.dev_attr.attr,
1937 &sensor_dev_attr_pwm2_stop_time.dev_attr.attr,
1938 &sensor_dev_attr_pwm2_step_up_time.dev_attr.attr,
1939 &sensor_dev_attr_pwm2_step_down_time.dev_attr.attr,
1940 &sensor_dev_attr_pwm2_start_output.dev_attr.attr,
1941 &sensor_dev_attr_pwm2_stop_output.dev_attr.attr,
1945 &sensor_dev_attr_pwm3.dev_attr.attr,
1946 &sensor_dev_attr_pwm3_mode.dev_attr.attr,
1947 &sensor_dev_attr_pwm3_enable.dev_attr.attr,
1948 &sensor_dev_attr_pwm3_temp_src.dev_attr.attr,
1949 &sensor_dev_attr_pwm3_target.dev_attr.attr,
1950 &sensor_dev_attr_pwm3_stop_time.dev_attr.attr,
1951 &sensor_dev_attr_pwm3_step_up_time.dev_attr.attr,
1952 &sensor_dev_attr_pwm3_step_down_time.dev_attr.attr,
1953 &sensor_dev_attr_pwm3_start_output.dev_attr.attr,
1954 &sensor_dev_attr_pwm3_stop_output.dev_attr.attr,
1958 &sensor_dev_attr_pwm4.dev_attr.attr,
1959 &sensor_dev_attr_pwm4_mode.dev_attr.attr,
1960 &sensor_dev_attr_pwm4_enable.dev_attr.attr,
1961 &sensor_dev_attr_pwm4_temp_src.dev_attr.attr,
1962 &sensor_dev_attr_pwm4_target.dev_attr.attr,
1963 &sensor_dev_attr_pwm4_stop_time.dev_attr.attr,
1964 &sensor_dev_attr_pwm4_step_up_time.dev_attr.attr,
1965 &sensor_dev_attr_pwm4_step_down_time.dev_attr.attr,
1966 &sensor_dev_attr_pwm4_start_output.dev_attr.attr,
1967 &sensor_dev_attr_pwm4_stop_output.dev_attr.attr,
1971 &sensor_dev_attr_pwm5.dev_attr.attr,
1972 &sensor_dev_attr_pwm5_mode.dev_attr.attr,
1973 &sensor_dev_attr_pwm5_enable.dev_attr.attr,
1974 &sensor_dev_attr_pwm5_temp_src.dev_attr.attr,
1975 &sensor_dev_attr_pwm5_target.dev_attr.attr,
1976 &sensor_dev_attr_pwm5_stop_time.dev_attr.attr,
1977 &sensor_dev_attr_pwm5_step_up_time.dev_attr.attr,
1978 &sensor_dev_attr_pwm5_step_down_time.dev_attr.attr,
1979 &sensor_dev_attr_pwm5_start_output.dev_attr.attr,
1980 &sensor_dev_attr_pwm5_stop_output.dev_attr.attr,
1985 static const struct attribute_group nct6775_group_pwm[5] = {
1986 { .attrs = nct6775_attributes_pwm[0] },
1987 { .attrs = nct6775_attributes_pwm[1] },
1988 { .attrs = nct6775_attributes_pwm[2] },
1989 { .attrs = nct6775_attributes_pwm[3] },
1990 { .attrs = nct6775_attributes_pwm[4] },
1993 static ssize_t show_auto_pwm(struct device *dev, struct device_attribute *attr,
1996 struct nct6775_data *data = nct6775_update_device(dev);
1997 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
1999 return sprintf(buf, "%d\n", data->auto_pwm[sattr->nr][sattr->index]);
2002 static ssize_t store_auto_pwm(struct device *dev, struct device_attribute *attr,
2003 const char *buf, size_t count)
2005 struct nct6775_sio_data *sio_data = dev->platform_data;
2006 struct nct6775_data *data = dev_get_drvdata(dev);
2007 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
2009 int point = sattr->index;
2014 err = kstrtoul(buf, 10, &val);
2020 if (point == data->auto_pwm_num) {
2021 if (sio_data->kind != nct6775 && !val)
2023 if (sio_data->kind != nct6779 && val)
2027 mutex_lock(&data->update_lock);
2028 data->auto_pwm[nr][point] = val;
2029 if (point < data->auto_pwm_num) {
2030 nct6775_write_value(data, NCT6775_REG_AUTO_PWM(nr, point),
2031 data->auto_pwm[nr][point]);
2033 switch(sio_data->kind) {
2035 /* disable if needed (pwm == 0) */
2036 reg = nct6775_read_value(data,
2037 NCT6775_REG_CRITICAL_ENAB[nr]);
2042 nct6775_write_value(data, NCT6775_REG_CRITICAL_ENAB[nr],
2046 break; /* always enabled, nothing to do */
2048 nct6775_write_value(data, NCT6779_REG_CRITICAL_PWM[nr],
2050 reg = nct6775_read_value(data,
2051 NCT6779_REG_CRITICAL_PWM_ENABLE[nr]);
2056 nct6775_write_value(data,
2057 NCT6779_REG_CRITICAL_PWM_ENABLE[nr],
2062 mutex_unlock(&data->update_lock);
2066 static ssize_t show_auto_temp(struct device *dev, struct device_attribute *attr,
2069 struct nct6775_data *data = nct6775_update_device(dev);
2070 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
2072 int point = sattr->index;
2075 * We don't know for sure if the temperature is signed or unsigned.
2076 * Assume it is unsigned.
2078 return sprintf(buf, "%d\n", data->auto_temp[nr][point] * 1000);
2081 static ssize_t store_auto_temp(struct device *dev,
2082 struct device_attribute *attr,
2083 const char *buf, size_t count)
2085 struct nct6775_data *data = dev_get_drvdata(dev);
2086 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
2088 int point = sattr->index;
2092 err = kstrtoul(buf, 10, &val);
2098 mutex_lock(&data->update_lock);
2099 data->auto_temp[nr][point] = DIV_ROUND_CLOSEST(val, 1000);
2100 if (point < data->auto_pwm_num) {
2101 nct6775_write_value(data, NCT6775_REG_AUTO_TEMP(nr, point),
2102 data->auto_temp[nr][point]);
2104 nct6775_write_value(data, NCT6775_REG_CRITICAL_TEMP[nr],
2105 data->auto_temp[nr][point]);
2107 mutex_unlock(&data->update_lock);
2112 * The number of auto-point trip points is chip dependent.
2113 * Need to check support while generating/removing attribute files.
2115 static struct sensor_device_attribute_2 sda_auto_pwm_arrays[] = {
2116 SENSOR_ATTR_2(pwm1_auto_point1_pwm, S_IWUSR | S_IRUGO,
2117 show_auto_pwm, store_auto_pwm, 0, 0),
2118 SENSOR_ATTR_2(pwm1_auto_point1_temp, S_IWUSR | S_IRUGO,
2119 show_auto_temp, store_auto_temp, 0, 0),
2120 SENSOR_ATTR_2(pwm1_auto_point1_temp_hyst, S_IWUSR | S_IRUGO,
2121 show_auto_temp_hyst, store_auto_temp_hyst, 0, 0),
2122 SENSOR_ATTR_2(pwm1_auto_point2_pwm, S_IWUSR | S_IRUGO,
2123 show_auto_pwm, store_auto_pwm, 0, 1),
2124 SENSOR_ATTR_2(pwm1_auto_point2_temp, S_IWUSR | S_IRUGO,
2125 show_auto_temp, store_auto_temp, 0, 1),
2126 SENSOR_ATTR_2(pwm1_auto_point2_temp_hyst, S_IWUSR | S_IRUGO,
2127 show_auto_temp_hyst, store_auto_temp_hyst, 0, 1),
2128 SENSOR_ATTR_2(pwm1_auto_point3_pwm, S_IWUSR | S_IRUGO,
2129 show_auto_pwm, store_auto_pwm, 0, 2),
2130 SENSOR_ATTR_2(pwm1_auto_point3_temp, S_IWUSR | S_IRUGO,
2131 show_auto_temp, store_auto_temp, 0, 2),
2132 SENSOR_ATTR_2(pwm1_auto_point3_temp_hyst, S_IWUSR | S_IRUGO,
2133 show_auto_temp_hyst, store_auto_temp_hyst, 0, 2),
2134 SENSOR_ATTR_2(pwm1_auto_point4_pwm, S_IWUSR | S_IRUGO,
2135 show_auto_pwm, store_auto_pwm, 0, 3),
2136 SENSOR_ATTR_2(pwm1_auto_point4_temp, S_IWUSR | S_IRUGO,
2137 show_auto_temp, store_auto_temp, 0, 3),
2138 SENSOR_ATTR_2(pwm1_auto_point4_temp_hyst, S_IWUSR | S_IRUGO,
2139 show_auto_temp_hyst, store_auto_temp_hyst, 0, 3),
2140 SENSOR_ATTR_2(pwm1_auto_point5_pwm, S_IWUSR | S_IRUGO,
2141 show_auto_pwm, store_auto_pwm, 0, 4),
2142 SENSOR_ATTR_2(pwm1_auto_point5_temp, S_IWUSR | S_IRUGO,
2143 show_auto_temp, store_auto_temp, 0, 4),
2144 SENSOR_ATTR_2(pwm1_auto_point5_temp_hyst, S_IWUSR | S_IRUGO,
2145 show_auto_temp_hyst, store_auto_temp_hyst, 0, 4),
2146 SENSOR_ATTR_2(pwm1_auto_point6_pwm, S_IWUSR | S_IRUGO,
2147 show_auto_pwm, store_auto_pwm, 0, 5),
2148 SENSOR_ATTR_2(pwm1_auto_point6_temp, S_IWUSR | S_IRUGO,
2149 show_auto_temp, store_auto_temp, 0, 5),
2150 SENSOR_ATTR_2(pwm1_auto_point6_temp_hyst, S_IWUSR | S_IRUGO,
2151 show_auto_temp_hyst, store_auto_temp_hyst, 0, 5),
2152 SENSOR_ATTR_2(pwm1_auto_point7_pwm, S_IWUSR | S_IRUGO,
2153 show_auto_pwm, store_auto_pwm, 0, 6),
2154 SENSOR_ATTR_2(pwm1_auto_point7_temp, S_IWUSR | S_IRUGO,
2155 show_auto_temp, store_auto_temp, 0, 6),
2156 SENSOR_ATTR_2(pwm1_auto_point7_temp_hyst, S_IWUSR | S_IRUGO,
2157 show_auto_temp_hyst, store_auto_temp_hyst, 0, 6),
2159 SENSOR_ATTR_2(pwm2_auto_point1_pwm, S_IWUSR | S_IRUGO,
2160 show_auto_pwm, store_auto_pwm, 1, 0),
2161 SENSOR_ATTR_2(pwm2_auto_point1_temp, S_IWUSR | S_IRUGO,
2162 show_auto_temp, store_auto_temp, 1, 0),
2163 SENSOR_ATTR_2(pwm2_auto_point1_temp_hyst, S_IWUSR | S_IRUGO,
2164 show_auto_temp_hyst, store_auto_temp_hyst, 1, 0),
2165 SENSOR_ATTR_2(pwm2_auto_point2_pwm, S_IWUSR | S_IRUGO,
2166 show_auto_pwm, store_auto_pwm, 1, 1),
2167 SENSOR_ATTR_2(pwm2_auto_point2_temp, S_IWUSR | S_IRUGO,
2168 show_auto_temp, store_auto_temp, 1, 1),
2169 SENSOR_ATTR_2(pwm2_auto_point2_temp_hyst, S_IWUSR | S_IRUGO,
2170 show_auto_temp_hyst, store_auto_temp_hyst, 1, 1),
2171 SENSOR_ATTR_2(pwm2_auto_point3_pwm, S_IWUSR | S_IRUGO,
2172 show_auto_pwm, store_auto_pwm, 1, 2),
2173 SENSOR_ATTR_2(pwm2_auto_point3_temp, S_IWUSR | S_IRUGO,
2174 show_auto_temp, store_auto_temp, 1, 2),
2175 SENSOR_ATTR_2(pwm2_auto_point3_temp_hyst, S_IWUSR | S_IRUGO,
2176 show_auto_temp_hyst, store_auto_temp_hyst, 1, 2),
2177 SENSOR_ATTR_2(pwm2_auto_point4_pwm, S_IWUSR | S_IRUGO,
2178 show_auto_pwm, store_auto_pwm, 1, 3),
2179 SENSOR_ATTR_2(pwm2_auto_point4_temp, S_IWUSR | S_IRUGO,
2180 show_auto_temp, store_auto_temp, 1, 3),
2181 SENSOR_ATTR_2(pwm2_auto_point4_temp_hyst, S_IWUSR | S_IRUGO,
2182 show_auto_temp_hyst, store_auto_temp_hyst, 1, 3),
2183 SENSOR_ATTR_2(pwm2_auto_point5_pwm, S_IWUSR | S_IRUGO,
2184 show_auto_pwm, store_auto_pwm, 1, 4),
2185 SENSOR_ATTR_2(pwm2_auto_point5_temp, S_IWUSR | S_IRUGO,
2186 show_auto_temp, store_auto_temp, 1, 4),
2187 SENSOR_ATTR_2(pwm2_auto_point5_temp_hyst, S_IWUSR | S_IRUGO,
2188 show_auto_temp_hyst, store_auto_temp_hyst, 1, 4),
2189 SENSOR_ATTR_2(pwm2_auto_point6_pwm, S_IWUSR | S_IRUGO,
2190 show_auto_pwm, store_auto_pwm, 1, 5),
2191 SENSOR_ATTR_2(pwm2_auto_point6_temp, S_IWUSR | S_IRUGO,
2192 show_auto_temp, store_auto_temp, 1, 5),
2193 SENSOR_ATTR_2(pwm2_auto_point6_temp_hyst, S_IWUSR | S_IRUGO,
2194 show_auto_temp_hyst, store_auto_temp_hyst, 1, 5),
2195 SENSOR_ATTR_2(pwm2_auto_point7_pwm, S_IWUSR | S_IRUGO,
2196 show_auto_pwm, store_auto_pwm, 1, 6),
2197 SENSOR_ATTR_2(pwm2_auto_point7_temp, S_IWUSR | S_IRUGO,
2198 show_auto_temp, store_auto_temp, 1, 6),
2199 SENSOR_ATTR_2(pwm2_auto_point7_temp_hyst, S_IWUSR | S_IRUGO,
2200 show_auto_temp_hyst, store_auto_temp_hyst, 1, 6),
2202 SENSOR_ATTR_2(pwm3_auto_point1_pwm, S_IWUSR | S_IRUGO,
2203 show_auto_pwm, store_auto_pwm, 2, 0),
2204 SENSOR_ATTR_2(pwm3_auto_point1_temp, S_IWUSR | S_IRUGO,
2205 show_auto_temp, store_auto_temp, 2, 0),
2206 SENSOR_ATTR_2(pwm3_auto_point1_temp_hyst, S_IWUSR | S_IRUGO,
2207 show_auto_temp_hyst, store_auto_temp_hyst, 2, 0),
2208 SENSOR_ATTR_2(pwm3_auto_point2_pwm, S_IWUSR | S_IRUGO,
2209 show_auto_pwm, store_auto_pwm, 2, 1),
2210 SENSOR_ATTR_2(pwm3_auto_point2_temp, S_IWUSR | S_IRUGO,
2211 show_auto_temp, store_auto_temp, 2, 1),
2212 SENSOR_ATTR_2(pwm3_auto_point2_temp_hyst, S_IWUSR | S_IRUGO,
2213 show_auto_temp_hyst, store_auto_temp_hyst, 2, 1),
2214 SENSOR_ATTR_2(pwm3_auto_point3_pwm, S_IWUSR | S_IRUGO,
2215 show_auto_pwm, store_auto_pwm, 2, 2),
2216 SENSOR_ATTR_2(pwm3_auto_point3_temp, S_IWUSR | S_IRUGO,
2217 show_auto_temp, store_auto_temp, 2, 2),
2218 SENSOR_ATTR_2(pwm3_auto_point3_temp_hyst, S_IWUSR | S_IRUGO,
2219 show_auto_temp_hyst, store_auto_temp_hyst, 2, 2),
2220 SENSOR_ATTR_2(pwm3_auto_point4_pwm, S_IWUSR | S_IRUGO,
2221 show_auto_pwm, store_auto_pwm, 2, 3),
2222 SENSOR_ATTR_2(pwm3_auto_point4_temp, S_IWUSR | S_IRUGO,
2223 show_auto_temp, store_auto_temp, 2, 3),
2224 SENSOR_ATTR_2(pwm3_auto_point4_temp_hyst, S_IWUSR | S_IRUGO,
2225 show_auto_temp_hyst, store_auto_temp_hyst, 2, 3),
2226 SENSOR_ATTR_2(pwm3_auto_point5_pwm, S_IWUSR | S_IRUGO,
2227 show_auto_pwm, store_auto_pwm, 2, 4),
2228 SENSOR_ATTR_2(pwm3_auto_point5_temp, S_IWUSR | S_IRUGO,
2229 show_auto_temp, store_auto_temp, 2, 4),
2230 SENSOR_ATTR_2(pwm3_auto_point5_temp_hyst, S_IWUSR | S_IRUGO,
2231 show_auto_temp_hyst, store_auto_temp_hyst, 2, 4),
2232 SENSOR_ATTR_2(pwm3_auto_point6_pwm, S_IWUSR | S_IRUGO,
2233 show_auto_pwm, store_auto_pwm, 2, 5),
2234 SENSOR_ATTR_2(pwm3_auto_point6_temp, S_IWUSR | S_IRUGO,
2235 show_auto_temp, store_auto_temp, 2, 5),
2236 SENSOR_ATTR_2(pwm3_auto_point6_temp_hyst, S_IWUSR | S_IRUGO,
2237 show_auto_temp_hyst, store_auto_temp_hyst, 2, 5),
2238 SENSOR_ATTR_2(pwm3_auto_point7_pwm, S_IWUSR | S_IRUGO,
2239 show_auto_pwm, store_auto_pwm, 2, 6),
2240 SENSOR_ATTR_2(pwm3_auto_point7_temp, S_IWUSR | S_IRUGO,
2241 show_auto_temp, store_auto_temp, 2, 6),
2242 SENSOR_ATTR_2(pwm3_auto_point7_temp_hyst, S_IWUSR | S_IRUGO,
2243 show_auto_temp_hyst, store_auto_temp_hyst, 2, 6),
2245 SENSOR_ATTR_2(pwm4_auto_point1_pwm, S_IWUSR | S_IRUGO,
2246 show_auto_pwm, store_auto_pwm, 3, 0),
2247 SENSOR_ATTR_2(pwm4_auto_point1_temp, S_IWUSR | S_IRUGO,
2248 show_auto_temp, store_auto_temp, 3, 0),
2249 SENSOR_ATTR_2(pwm4_auto_point1_temp_hyst, S_IWUSR | S_IRUGO,
2250 show_auto_temp_hyst, store_auto_temp_hyst, 3, 0),
2251 SENSOR_ATTR_2(pwm4_auto_point2_pwm, S_IWUSR | S_IRUGO,
2252 show_auto_pwm, store_auto_pwm, 3, 1),
2253 SENSOR_ATTR_2(pwm4_auto_point2_temp, S_IWUSR | S_IRUGO,
2254 show_auto_temp, store_auto_temp, 3, 1),
2255 SENSOR_ATTR_2(pwm4_auto_point2_temp_hyst, S_IWUSR | S_IRUGO,
2256 show_auto_temp_hyst, store_auto_temp_hyst, 3, 1),
2257 SENSOR_ATTR_2(pwm4_auto_point3_pwm, S_IWUSR | S_IRUGO,
2258 show_auto_pwm, store_auto_pwm, 3, 2),
2259 SENSOR_ATTR_2(pwm4_auto_point3_temp, S_IWUSR | S_IRUGO,
2260 show_auto_temp, store_auto_temp, 3, 2),
2261 SENSOR_ATTR_2(pwm4_auto_point3_temp_hyst, S_IWUSR | S_IRUGO,
2262 show_auto_temp_hyst, store_auto_temp_hyst, 3, 2),
2263 SENSOR_ATTR_2(pwm4_auto_point4_pwm, S_IWUSR | S_IRUGO,
2264 show_auto_pwm, store_auto_pwm, 3, 3),
2265 SENSOR_ATTR_2(pwm4_auto_point4_temp, S_IWUSR | S_IRUGO,
2266 show_auto_temp, store_auto_temp, 3, 3),
2267 SENSOR_ATTR_2(pwm4_auto_point4_temp_hyst, S_IWUSR | S_IRUGO,
2268 show_auto_temp_hyst, store_auto_temp_hyst, 3, 3),
2269 SENSOR_ATTR_2(pwm4_auto_point5_pwm, S_IWUSR | S_IRUGO,
2270 show_auto_pwm, store_auto_pwm, 3, 4),
2271 SENSOR_ATTR_2(pwm4_auto_point5_temp, S_IWUSR | S_IRUGO,
2272 show_auto_temp, store_auto_temp, 3, 4),
2273 SENSOR_ATTR_2(pwm4_auto_point5_temp_hyst, S_IWUSR | S_IRUGO,
2274 show_auto_temp_hyst, store_auto_temp_hyst, 3, 4),
2275 SENSOR_ATTR_2(pwm4_auto_point6_pwm, S_IWUSR | S_IRUGO,
2276 show_auto_pwm, store_auto_pwm, 3, 5),
2277 SENSOR_ATTR_2(pwm4_auto_point6_temp, S_IWUSR | S_IRUGO,
2278 show_auto_temp, store_auto_temp, 3, 5),
2279 SENSOR_ATTR_2(pwm4_auto_point6_temp_hyst, S_IWUSR | S_IRUGO,
2280 show_auto_temp_hyst, store_auto_temp_hyst, 3, 5),
2281 SENSOR_ATTR_2(pwm4_auto_point7_pwm, S_IWUSR | S_IRUGO,
2282 show_auto_pwm, store_auto_pwm, 3, 6),
2283 SENSOR_ATTR_2(pwm4_auto_point7_temp, S_IWUSR | S_IRUGO,
2284 show_auto_temp, store_auto_temp, 3, 6),
2285 SENSOR_ATTR_2(pwm4_auto_point7_temp_hyst, S_IWUSR | S_IRUGO,
2286 show_auto_temp_hyst, store_auto_temp_hyst, 3, 6),
2288 SENSOR_ATTR_2(pwm5_auto_point1_pwm, S_IWUSR | S_IRUGO,
2289 show_auto_pwm, store_auto_pwm, 4, 0),
2290 SENSOR_ATTR_2(pwm5_auto_point1_temp, S_IWUSR | S_IRUGO,
2291 show_auto_temp, store_auto_temp, 4, 0),
2292 SENSOR_ATTR_2(pwm5_auto_point1_temp_hyst, S_IWUSR | S_IRUGO,
2293 show_auto_temp_hyst, store_auto_temp_hyst, 4, 0),
2294 SENSOR_ATTR_2(pwm5_auto_point2_pwm, S_IWUSR | S_IRUGO,
2295 show_auto_pwm, store_auto_pwm, 4, 1),
2296 SENSOR_ATTR_2(pwm5_auto_point2_temp, S_IWUSR | S_IRUGO,
2297 show_auto_temp, store_auto_temp, 4, 1),
2298 SENSOR_ATTR_2(pwm5_auto_point2_temp_hyst, S_IWUSR | S_IRUGO,
2299 show_auto_temp_hyst, store_auto_temp_hyst, 4, 1),
2300 SENSOR_ATTR_2(pwm5_auto_point3_pwm, S_IWUSR | S_IRUGO,
2301 show_auto_pwm, store_auto_pwm, 4, 2),
2302 SENSOR_ATTR_2(pwm5_auto_point3_temp, S_IWUSR | S_IRUGO,
2303 show_auto_temp, store_auto_temp, 4, 2),
2304 SENSOR_ATTR_2(pwm5_auto_point3_temp_hyst, S_IWUSR | S_IRUGO,
2305 show_auto_temp_hyst, store_auto_temp_hyst, 4, 2),
2306 SENSOR_ATTR_2(pwm5_auto_point4_pwm, S_IWUSR | S_IRUGO,
2307 show_auto_pwm, store_auto_pwm, 4, 3),
2308 SENSOR_ATTR_2(pwm5_auto_point4_temp, S_IWUSR | S_IRUGO,
2309 show_auto_temp, store_auto_temp, 4, 3),
2310 SENSOR_ATTR_2(pwm5_auto_point4_temp_hyst, S_IWUSR | S_IRUGO,
2311 show_auto_temp_hyst, store_auto_temp_hyst, 4, 3),
2312 SENSOR_ATTR_2(pwm5_auto_point5_pwm, S_IWUSR | S_IRUGO,
2313 show_auto_pwm, store_auto_pwm, 4, 4),
2314 SENSOR_ATTR_2(pwm5_auto_point5_temp, S_IWUSR | S_IRUGO,
2315 show_auto_temp, store_auto_temp, 4, 4),
2316 SENSOR_ATTR_2(pwm5_auto_point5_temp_hyst, S_IWUSR | S_IRUGO,
2317 show_auto_temp_hyst, store_auto_temp_hyst, 4, 4),
2318 SENSOR_ATTR_2(pwm5_auto_point6_pwm, S_IWUSR | S_IRUGO,
2319 show_auto_pwm, store_auto_pwm, 4, 5),
2320 SENSOR_ATTR_2(pwm5_auto_point6_temp, S_IWUSR | S_IRUGO,
2321 show_auto_temp, store_auto_temp, 4, 5),
2322 SENSOR_ATTR_2(pwm5_auto_point6_temp_hyst, S_IWUSR | S_IRUGO,
2323 show_auto_temp_hyst, store_auto_temp_hyst, 4, 5),
2324 SENSOR_ATTR_2(pwm5_auto_point7_pwm, S_IWUSR | S_IRUGO,
2325 show_auto_pwm, store_auto_pwm, 4, 6),
2326 SENSOR_ATTR_2(pwm5_auto_point7_temp, S_IWUSR | S_IRUGO,
2327 show_auto_temp, store_auto_temp, 4, 6),
2328 SENSOR_ATTR_2(pwm5_auto_point7_temp_hyst, S_IWUSR | S_IRUGO,
2329 show_auto_temp_hyst, store_auto_temp_hyst, 4, 6),
2333 show_vid(struct device *dev, struct device_attribute *attr, char *buf)
2335 struct nct6775_data *data = dev_get_drvdata(dev);
2336 return sprintf(buf, "%d\n", vid_from_reg(data->vid, data->vrm));
2338 static DEVICE_ATTR(cpu0_vid, S_IRUGO, show_vid, NULL);
2341 /* Case open detection */
2344 show_caseopen(struct device *dev, struct device_attribute *attr, char *buf)
2346 struct nct6775_data *data = nct6775_update_device(dev);
2348 return sprintf(buf, "%d\n",
2349 !!(data->caseopen & to_sensor_dev_attr_2(attr)->index));
2353 clear_caseopen(struct device *dev, struct device_attribute *attr,
2354 const char *buf, size_t count)
2356 struct nct6775_data *data = dev_get_drvdata(dev);
2360 if (kstrtoul(buf, 10, &val) || val != 0)
2363 mask = to_sensor_dev_attr_2(attr)->nr;
2365 mutex_lock(&data->update_lock);
2366 reg = nct6775_read_value(data, NCT6775_REG_CASEOPEN_CLR);
2367 nct6775_write_value(data, NCT6775_REG_CASEOPEN_CLR, reg | mask);
2368 nct6775_write_value(data, NCT6775_REG_CASEOPEN_CLR, reg & ~mask);
2369 data->valid = 0; /* Force cache refresh */
2370 mutex_unlock(&data->update_lock);
2375 static struct sensor_device_attribute_2 sda_caseopen[] = {
2376 SENSOR_ATTR_2(intrusion0_alarm, S_IWUSR | S_IRUGO, show_caseopen,
2377 clear_caseopen, 0x80, 0x10),
2378 SENSOR_ATTR_2(intrusion1_alarm, S_IWUSR | S_IRUGO, show_caseopen,
2379 clear_caseopen, 0x40, 0x40),
2383 * Driver and device management
2386 static void nct6775_device_remove_files(struct device *dev)
2389 * some entries in the following arrays may not have been used in
2390 * device_create_file(), but device_remove_file() will ignore them
2393 struct nct6775_data *data = dev_get_drvdata(dev);
2395 for (i = 0; i < data->pwm_num; i++) {
2396 sysfs_remove_group(&dev->kobj, &nct6775_group_pwm[i]);
2397 for (j = 0; j < ARRAY_SIZE(sda_auto_pwm_arrays); j++)
2398 device_remove_file(dev,
2399 &sda_auto_pwm_arrays[j].dev_attr);
2402 for (i = 0; i < data->in_num; i++)
2403 sysfs_remove_group(&dev->kobj, &nct6775_group_in[i]);
2405 for (i = 0; i < 5; i++) {
2406 device_remove_file(dev, &sda_fan_input[i].dev_attr);
2407 device_remove_file(dev, &sda_fan_alarm[i].dev_attr);
2408 device_remove_file(dev, &sda_fan_div[i].dev_attr);
2409 device_remove_file(dev, &sda_fan_min[i].dev_attr);
2411 for (i = 0; i < NUM_REG_TEMP; i++) {
2412 if (!(data->have_temp & (1 << i)))
2414 device_remove_file(dev, &sda_temp_input[i].dev_attr);
2417 device_remove_file(dev, &sda_temp_label[i].dev_attr);
2418 device_remove_file(dev, &sda_temp_max[i].dev_attr);
2419 device_remove_file(dev, &sda_temp_max_hyst[i].dev_attr);
2422 device_remove_file(dev, &sda_temp_alarm[i].dev_attr);
2423 device_remove_file(dev, &sda_temp_type[i].dev_attr);
2426 device_remove_file(dev, &sda_caseopen[0].dev_attr);
2427 device_remove_file(dev, &sda_caseopen[1].dev_attr);
2429 device_remove_file(dev, &dev_attr_name);
2430 device_remove_file(dev, &dev_attr_cpu0_vid);
2433 /* Get the monitoring functions started */
2434 static inline void __devinit nct6775_init_device(struct nct6775_data *data,
2440 /* Start monitoring if needed */
2441 tmp = nct6775_read_value(data, NCT6775_REG_CONFIG);
2443 nct6775_write_value(data, NCT6775_REG_CONFIG,
2446 /* Enable temperature sensors if needed */
2447 for (i = 0; i < NUM_REG_TEMP; i++) {
2448 if (!(data->have_temp & (1 << i)))
2450 if (!data->reg_temp_config[i])
2452 tmp = nct6775_read_value(data,
2453 data->reg_temp_config[i]);
2455 nct6775_write_value(data,
2456 data->reg_temp_config[i],
2460 /* Enable VBAT monitoring if needed */
2461 tmp = nct6775_read_value(data, NCT6775_REG_VBAT);
2463 nct6775_write_value(data, NCT6775_REG_VBAT, tmp | 0x01);
2465 for (i = 0; i < 3; i++) {
2466 const char *label = NULL;
2468 if (data->temp_label)
2469 label = data->temp_label[data->temp_src[i]];
2471 /* Digital source overrides analog type */
2472 if (label && strncmp(label, "PECI", 4) == 0)
2473 data->temp_type[i] = 6;
2474 else if (label && strncmp(label, "AMD", 3) == 0)
2475 data->temp_type[i] = 5;
2476 else if ((tmp & (0x02 << i)))
2477 data->temp_type[i] = 1; /* diode */
2479 data->temp_type[i] = 4; /* thermistor */
2483 static void w82627ehf_swap_tempreg(struct nct6775_data *data,
2488 tmp = data->temp_src[r1];
2489 data->temp_src[r1] = data->temp_src[r2];
2490 data->temp_src[r2] = tmp;
2492 tmp = data->reg_temp[r1];
2493 data->reg_temp[r1] = data->reg_temp[r2];
2494 data->reg_temp[r2] = tmp;
2496 tmp = data->reg_temp_over[r1];
2497 data->reg_temp_over[r1] = data->reg_temp_over[r2];
2498 data->reg_temp_over[r2] = tmp;
2500 tmp = data->reg_temp_hyst[r1];
2501 data->reg_temp_hyst[r1] = data->reg_temp_hyst[r2];
2502 data->reg_temp_hyst[r2] = tmp;
2504 tmp = data->reg_temp_config[r1];
2505 data->reg_temp_config[r1] = data->reg_temp_config[r2];
2506 data->reg_temp_config[r2] = tmp;
2509 static void __devinit
2510 nct6775_check_fan_inputs(const struct nct6775_sio_data *sio_data,
2511 struct nct6775_data *data)
2514 bool fan3pin, fan3min, fan4pin, fan4min, fan5pin;
2515 bool pwm3pin, pwm4pin, pwm5pin;
2517 superio_enter(sio_data->sioreg);
2519 /* fan4 and fan5 share some pins with the GPIO and serial flash */
2520 if (sio_data->kind == nct6775) {
2521 regval = superio_inb(sio_data->sioreg, 0x2c);
2523 fan3pin = regval & (1 << 6);
2525 pwm3pin = regval & (1 << 7);
2527 /* On NCT6775, fan4 shares pins with the fdc interface */
2528 fan4pin = !(superio_inb(sio_data->sioreg, 0x2A) & 0x80);
2533 } else if (sio_data->kind == nct6776) {
2534 bool gpok = superio_inb(sio_data->sioreg, 0x27) & 0x80;
2536 superio_select(sio_data->sioreg, NCT6775_LD_HWM);
2537 regval = superio_inb(sio_data->sioreg, SIO_REG_ENABLE);
2542 fan3pin = !(superio_inb(sio_data->sioreg, 0x24) & 0x40);
2547 fan4pin = superio_inb(sio_data->sioreg, 0x1C) & 0x01;
2552 fan5pin = superio_inb(sio_data->sioreg, 0x1C) & 0x02;
2559 } else { /* NCT6779D */
2560 regval = superio_inb(sio_data->sioreg, 0x1c);
2562 fan3pin = !(regval & (1 << 5));
2563 fan4pin = !(regval & (1 << 6));
2564 fan5pin = !(regval & (1 << 7));
2566 pwm3pin = !(regval & (1 << 0));
2567 pwm4pin = !(regval & (1 << 1));
2568 pwm5pin = !(regval & (1 << 2));
2574 superio_exit(sio_data->sioreg);
2576 data->has_fan = data->has_fan_min = 0x03; /* fan1 and fan2 */
2577 data->has_fan |= (fan3pin << 2);
2578 data->has_fan_min |= (fan3min << 2);
2580 data->has_fan |= (fan4pin << 3) | (fan5pin << 4);
2581 data->has_fan_min |= (fan4min << 3) | (fan5pin << 4);
2583 data->has_pwm = 0x03 | (pwm3pin << 2) | (pwm4pin << 3) | (pwm5pin << 4);
2586 static int __devinit nct6775_probe(struct platform_device *pdev)
2588 struct device *dev = &pdev->dev;
2589 struct nct6775_sio_data *sio_data = dev->platform_data;
2590 struct nct6775_data *data;
2591 struct resource *res;
2594 const u16 *reg_temp, *reg_temp_config;
2596 res = platform_get_resource(pdev, IORESOURCE_IO, 0);
2597 if (!request_region(res->start, IOREGION_LENGTH, DRVNAME)) {
2599 dev_err(dev, "Failed to request region 0x%lx-0x%lx\n",
2600 (unsigned long)res->start,
2601 (unsigned long)res->start + IOREGION_LENGTH - 1);
2605 data = devm_kzalloc(&pdev->dev, sizeof(struct nct6775_data),
2612 data->addr = res->start;
2613 mutex_init(&data->lock);
2614 mutex_init(&data->update_lock);
2615 data->name = nct6775_device_names[sio_data->kind];
2616 platform_set_drvdata(pdev, data);
2618 switch (sio_data->kind) {
2621 data->have_in = 0x1ff;
2625 data->have_in = 0x7fff;
2629 switch (sio_data->kind) {
2632 data->auto_pwm_num = 6;
2636 data->auto_pwm_num = 4;
2640 data->auto_pwm_num = 4;
2644 /* Default to no temperature inputs, code below will adjust as needed */
2645 data->have_temp = 0;
2647 /* Deal with temperature register setup first. */
2648 switch(sio_data->kind == nct6775) {
2650 reg_temp = NCT6775_REG_TEMP;
2651 reg_temp_config = NCT6775_REG_TEMP_CONFIG;
2653 reg_temp = NCT6775_REG_TEMP;
2654 reg_temp_config = NCT6776_REG_TEMP_CONFIG;
2657 reg_temp = NCT6779_REG_TEMP;
2658 reg_temp_config = NCT6776_REG_TEMP_CONFIG;
2662 for (i = 0; i < NUM_REG_TEMP; i++) {
2665 if (reg_temp[i] == 0)
2668 data->reg_temp[i] = reg_temp[i];
2669 data->reg_temp_over[i] = NCT6775_REG_TEMP_OVER[i];
2670 data->reg_temp_hyst[i] = NCT6775_REG_TEMP_HYST[i];
2671 data->reg_temp_config[i] = reg_temp_config[i];
2673 src = nct6775_read_value(data, NCT6775_REG_TEMP_SOURCE[i]);
2676 /* Always display temp6..10 (fan control sources) if enabled */
2677 if (src && (i > 5 || !(mask & (1 << src)))) {
2678 data->have_temp |= 1 << i;
2682 data->temp_src[i] = src;
2686 * Do some register swapping if index 0..2 don't
2687 * point to SYSTIN(1), CPUIN(2), and AUXIN(3).
2688 * Idea is to have the first three attributes
2689 * report SYSTIN, CPUIN, and AUXIN if possible
2690 * without overriding the basic system configuration.
2691 * Do this only for the first six temperature sources;
2692 * the remaining temperatures are fan control sources,
2693 * and we don't want to touch those.
2695 if (i > 0 && data->temp_src[0] != 1
2696 && data->temp_src[i] == 1)
2697 w82627ehf_swap_tempreg(data, 0, i);
2698 if (i > 1 && data->temp_src[1] != 2
2699 && data->temp_src[i] == 2)
2700 w82627ehf_swap_tempreg(data, 1, i);
2701 if (i > 2 && data->temp_src[2] != 3
2702 && data->temp_src[i] == 3)
2703 w82627ehf_swap_tempreg(data, 2, i);
2707 switch (sio_data->kind) {
2709 data->temp_label = nct6775_temp_label;
2713 * On NCT6776, AUXTIN and VIN3 pins are shared.
2714 * Only way to detect it is to check if AUXTIN is used
2715 * as a temperature source, and if that source is
2718 * If that is the case, disable in6, which reports VIN3.
2719 * Otherwise disable temp3.
2721 if (data->temp_src[2] == 3) {
2724 if (data->reg_temp_config[2])
2725 reg = nct6775_read_value(data,
2726 data->reg_temp_config[2]);
2728 reg = 0; /* Assume AUXTIN is used */
2731 data->have_temp &= ~(1 << 2);
2733 data->have_in &= ~(1 << 6);
2735 data->temp_label = nct6776_temp_label;
2743 * Assume voltage is disabled if the respective temperature is
2744 * used as temperature source.
2746 for (i = 0; i < ARRAY_SIZE(NCT6779_REG_TEMP); i++) {
2747 if (!(data->have_temp & (1 << i)))
2749 if (data->temp_src[i] == 3) /* AUXTIN0 */
2750 data->have_in &= ~(1 << 6); /* no VIN4 */
2751 if (data->temp_src[i] == 4) /* AUXTIN1 */
2752 data->have_in &= ~(1 << 10); /* no VIN5 */
2753 if (data->temp_src[i] == 5) /* AUXTIN2 */
2754 data->have_in &= ~(1 << 11); /* no VIN6 */
2755 if (data->temp_src[i] == 6) /* AUXTIN0 */
2756 data->have_in &= ~(1 << 14); /* no VIN7 */
2758 data->temp_label = nct6779_temp_label;
2762 if (sio_data->kind == nct6775) {
2763 data->has_fan_div = true;
2764 data->fan_from_reg = fan_from_reg16;
2765 data->fan_from_reg_min = fan_from_reg8;
2766 data->REG_VIN = NCT6775_REG_IN;
2767 data->REG_TARGET = NCT6775_REG_TARGET;
2768 data->REG_FAN = NCT6775_REG_FAN;
2769 data->REG_FAN_MIN = NCT6775_REG_FAN_MIN;
2770 data->REG_FAN_START_OUTPUT = NCT6775_REG_FAN_START_OUTPUT;
2771 data->REG_FAN_STOP_OUTPUT = NCT6775_REG_FAN_STOP_OUTPUT;
2772 } else if (sio_data->kind == nct6776) {
2773 data->has_fan_div = false;
2774 data->fan_from_reg = fan_from_reg13;
2775 data->fan_from_reg_min = fan_from_reg13;
2776 data->REG_VIN = NCT6775_REG_IN;
2777 data->REG_TARGET = NCT6775_REG_TARGET;
2778 data->REG_FAN = NCT6775_REG_FAN;
2779 data->REG_FAN_MIN = NCT6776_REG_FAN_MIN;
2780 data->REG_FAN_START_OUTPUT = NCT6775_REG_FAN_START_OUTPUT;
2781 data->REG_FAN_STOP_OUTPUT = NCT6775_REG_FAN_STOP_OUTPUT;
2782 } else if (sio_data->kind == nct6779) {
2783 data->has_fan_div = false;
2784 data->fan_from_reg = fan_from_reg13;
2785 data->fan_from_reg_min = fan_from_reg13;
2786 data->REG_VIN = NCT6779_REG_IN;
2787 data->REG_TARGET = NCT6775_REG_TARGET;
2788 data->REG_FAN = NCT6779_REG_FAN;
2789 data->REG_FAN_MIN = NCT6776_REG_FAN_MIN;
2790 data->REG_FAN_START_OUTPUT = NCT6775_REG_FAN_START_OUTPUT;
2791 data->REG_FAN_STOP_OUTPUT = NCT6775_REG_FAN_STOP_OUTPUT;
2794 /* Initialize the chip */
2795 nct6775_init_device(data, sio_data->kind);
2797 data->vrm = vid_which_vrm();
2798 superio_enter(sio_data->sioreg);
2801 * We can get the VID input values directly at logical device D 0xe3.
2803 superio_select(sio_data->sioreg, NCT6775_LD_VID);
2804 data->vid = superio_inb(sio_data->sioreg, 0xe3);
2805 err = device_create_file(dev, &dev_attr_cpu0_vid);
2812 superio_select(sio_data->sioreg, NCT6775_LD_HWM);
2813 tmp = superio_inb(sio_data->sioreg, NCT6775_REG_FAN_DEBOUNCE);
2814 if (sio_data->kind == nct6776 || sio_data->kind == nct6779)
2815 superio_outb(sio_data->sioreg, NCT6775_REG_FAN_DEBOUNCE,
2818 superio_outb(sio_data->sioreg, NCT6775_REG_FAN_DEBOUNCE,
2820 pr_info("Enabled fan debounce for chip %s\n", data->name);
2823 superio_exit(sio_data->sioreg);
2825 nct6775_check_fan_inputs(sio_data, data);
2827 /* Read fan clock dividers immediately */
2828 nct6775_update_fan_div_common(dev, data);
2830 /* Register sysfs hooks */
2831 for (i = 0; i < data->pwm_num; i++) {
2832 if (!(data->has_pwm & (1 << i)))
2834 err = sysfs_create_group(&dev->kobj, &nct6775_group_pwm[i]);
2838 for (i = 0; i < ARRAY_SIZE(sda_auto_pwm_arrays); i++) {
2839 struct sensor_device_attribute_2 *attr =
2840 &sda_auto_pwm_arrays[i];
2842 if (!(data->has_pwm & (1 << attr->nr)))
2844 if (attr->index > data->auto_pwm_num)
2846 err = device_create_file(dev, &attr->dev_attr);
2851 for (i = 0; i < data->in_num; i++) {
2852 if (!(data->have_in & (1 << i)))
2854 err = sysfs_create_group(&dev->kobj, &nct6775_group_in[i]);
2859 for (i = 0; i < 5; i++) {
2860 if (data->has_fan & (1 << i)) {
2861 err = device_create_file(dev,
2862 &sda_fan_input[i].dev_attr);
2865 err = device_create_file(dev,
2866 &sda_fan_alarm[i].dev_attr);
2869 if (sio_data->kind != nct6776 &&
2870 sio_data->kind != nct6779) {
2871 err = device_create_file(dev,
2872 &sda_fan_div[i].dev_attr);
2876 if (data->has_fan_min & (1 << i)) {
2877 err = device_create_file(dev,
2878 &sda_fan_min[i].dev_attr);
2885 for (i = 0; i < NUM_REG_TEMP; i++) {
2886 if (!(data->have_temp & (1 << i)))
2888 err = device_create_file(dev, &sda_temp_input[i].dev_attr);
2893 if (data->temp_label) {
2894 err = device_create_file(dev,
2895 &sda_temp_label[i].dev_attr);
2899 if (data->reg_temp_over[i]) {
2900 err = device_create_file(dev,
2901 &sda_temp_max[i].dev_attr);
2905 if (data->reg_temp_hyst[i]) {
2906 err = device_create_file(dev,
2907 &sda_temp_max_hyst[i].dev_attr);
2913 err = device_create_file(dev, &sda_temp_alarm[i].dev_attr);
2916 err = device_create_file(dev, &sda_temp_type[i].dev_attr);
2921 err = device_create_file(dev, &sda_caseopen[0].dev_attr);
2925 if (sio_data->kind == nct6776 || sio_data->kind == nct6779) {
2926 err = device_create_file(dev, &sda_caseopen[1].dev_attr);
2931 err = device_create_file(dev, &dev_attr_name);
2935 data->hwmon_dev = hwmon_device_register(dev);
2936 if (IS_ERR(data->hwmon_dev)) {
2937 err = PTR_ERR(data->hwmon_dev);
2944 nct6775_device_remove_files(dev);
2946 platform_set_drvdata(pdev, NULL);
2947 release_region(res->start, IOREGION_LENGTH);
2952 static int __devexit nct6775_remove(struct platform_device *pdev)
2954 struct nct6775_data *data = platform_get_drvdata(pdev);
2956 hwmon_device_unregister(data->hwmon_dev);
2957 nct6775_device_remove_files(&pdev->dev);
2958 release_region(data->addr, IOREGION_LENGTH);
2959 platform_set_drvdata(pdev, NULL);
2964 static struct platform_driver nct6775_driver = {
2966 .owner = THIS_MODULE,
2969 .probe = nct6775_probe,
2970 .remove = __devexit_p(nct6775_remove),
2973 /* nct6775_find() looks for a '627 in the Super-I/O config space */
2974 static int __init nct6775_find(int sioaddr, unsigned short *addr,
2975 struct nct6775_sio_data *sio_data)
2977 static const char __initdata sio_name_NCT6775[] = "NCT6775F";
2978 static const char __initdata sio_name_NCT6776[] = "NCT6776F";
2979 static const char __initdata sio_name_NCT6779[] = "NCT6779F";
2982 const char *sio_name;
2984 superio_enter(sioaddr);
2989 val = (superio_inb(sioaddr, SIO_REG_DEVID) << 8)
2990 | superio_inb(sioaddr, SIO_REG_DEVID + 1);
2991 switch (val & SIO_ID_MASK) {
2992 case SIO_NCT6775_ID:
2993 sio_data->kind = nct6775;
2994 sio_name = sio_name_NCT6775;
2996 case SIO_NCT6776_ID:
2997 sio_data->kind = nct6776;
2998 sio_name = sio_name_NCT6776;
3000 case SIO_NCT6779_ID:
3001 sio_data->kind = nct6779;
3002 sio_name = sio_name_NCT6779;
3006 pr_debug("unsupported chip ID: 0x%04x\n", val);
3007 superio_exit(sioaddr);
3011 /* We have a known chip, find the HWM I/O address */
3012 superio_select(sioaddr, NCT6775_LD_HWM);
3013 val = (superio_inb(sioaddr, SIO_REG_ADDR) << 8)
3014 | superio_inb(sioaddr, SIO_REG_ADDR + 1);
3015 *addr = val & IOREGION_ALIGNMENT;
3017 pr_err("Refusing to enable a Super-I/O device with a base I/O port 0\n");
3018 superio_exit(sioaddr);
3022 /* Activate logical device if needed */
3023 val = superio_inb(sioaddr, SIO_REG_ENABLE);
3024 if (!(val & 0x01)) {
3025 pr_warn("Forcibly enabling Super-I/O. "
3026 "Sensor is probably unusable.\n");
3027 superio_outb(sioaddr, SIO_REG_ENABLE, val | 0x01);
3030 superio_exit(sioaddr);
3031 pr_info("Found %s chip at %#x\n", sio_name, *addr);
3032 sio_data->sioreg = sioaddr;
3038 * when Super-I/O functions move to a separate file, the Super-I/O
3039 * bus will manage the lifetime of the device and this module will only keep
3040 * track of the nct6775 driver. But since we platform_device_alloc(), we
3041 * must keep track of the device
3043 static struct platform_device *pdev;
3045 static int __init sensors_nct6775_init(void)
3048 unsigned short address;
3049 struct resource res;
3050 struct nct6775_sio_data sio_data;
3053 * initialize sio_data->kind and sio_data->sioreg.
3055 * when Super-I/O functions move to a separate file, the Super-I/O
3056 * driver will probe 0x2e and 0x4e and auto-detect the presence of a
3057 * nct6775 hardware monitor, and call probe()
3059 if (nct6775_find(0x2e, &address, &sio_data) &&
3060 nct6775_find(0x4e, &address, &sio_data))
3063 err = platform_driver_register(&nct6775_driver);
3067 pdev = platform_device_alloc(DRVNAME, address);
3070 pr_err("Device allocation failed\n");
3071 goto exit_unregister;
3074 err = platform_device_add_data(pdev, &sio_data,
3075 sizeof(struct nct6775_sio_data));
3077 pr_err("Platform data allocation failed\n");
3078 goto exit_device_put;
3081 memset(&res, 0, sizeof(res));
3083 res.start = address + IOREGION_OFFSET;
3084 res.end = address + IOREGION_OFFSET + IOREGION_LENGTH - 1;
3085 res.flags = IORESOURCE_IO;
3087 err = acpi_check_resource_conflict(&res);
3089 goto exit_device_put;
3091 err = platform_device_add_resources(pdev, &res, 1);
3093 pr_err("Device resource addition failed (%d)\n", err);
3094 goto exit_device_put;
3097 /* platform_device_add calls probe() */
3098 err = platform_device_add(pdev);
3100 pr_err("Device addition failed (%d)\n", err);
3101 goto exit_device_put;
3107 platform_device_put(pdev);
3109 platform_driver_unregister(&nct6775_driver);
3114 static void __exit sensors_nct6775_exit(void)
3116 platform_device_unregister(pdev);
3117 platform_driver_unregister(&nct6775_driver);
3120 MODULE_AUTHOR("Guenter Roeck <linux@roeck-us.net>");
3121 MODULE_DESCRIPTION("NCT677x driver");
3122 MODULE_LICENSE("GPL");
3124 module_init(sensors_nct6775_init);
3125 module_exit(sensors_nct6775_exit);