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 6+3 0xb470 0xc1 0x5ca3
37 * nct6776f 9 5 3 6+3 0xc330 0xc1 0x5ca3
38 * nct6779d 15 5 5 2+5 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_ACPI 0x0a
81 #define NCT6775_LD_HWM 0x0b
82 #define NCT6775_LD_VID 0x0d
84 #define SIO_REG_LDSEL 0x07 /* Logical device select */
85 #define SIO_REG_DEVID 0x20 /* Device ID (2 bytes) */
86 #define SIO_REG_EN_VRM10 0x2C /* GPIO3, GPIO4 selection */
87 #define SIO_REG_ENABLE 0x30 /* Logical device enable */
88 #define SIO_REG_ADDR 0x60 /* Logical device address (2 bytes) */
89 #define SIO_REG_VID_CTRL 0xF0 /* VID control */
90 #define SIO_REG_VID_DATA 0xF1 /* VID data */
92 #define SIO_NCT6775_ID 0xb470
93 #define SIO_NCT6776_ID 0xc330
94 #define SIO_NCT6779_ID 0xc560
95 #define SIO_ID_MASK 0xFFF0
98 superio_outb(int ioreg, int reg, int val)
101 outb(val, ioreg + 1);
105 superio_inb(int ioreg, int reg)
108 return inb(ioreg + 1);
112 superio_select(int ioreg, int ld)
114 outb(SIO_REG_LDSEL, ioreg);
119 superio_enter(int ioreg)
126 superio_exit(int ioreg)
130 outb(0x02, ioreg + 1);
137 #define IOREGION_ALIGNMENT (~7)
138 #define IOREGION_OFFSET 5
139 #define IOREGION_LENGTH 2
140 #define ADDR_REG_OFFSET 0
141 #define DATA_REG_OFFSET 1
143 #define NCT6775_REG_BANK 0x4E
144 #define NCT6775_REG_CONFIG 0x40
147 * Not currently used:
148 * REG_MAN_ID has the value 0x5ca3 for all supported chips.
149 * REG_CHIP_ID == 0x88/0xa1/0xc1 depending on chip model.
150 * REG_MAN_ID is at port 0x4f
151 * REG_CHIP_ID is at port 0x58
154 static const u16 NCT6775_REG_FAN_MIN[] = { 0x3b, 0x3c, 0x3d };
156 /* Voltage min/max registers for nr=7..14 are in bank 5 */
158 static const u16 NCT6775_REG_IN_MAX[] = {
159 0x2b, 0x2d, 0x2f, 0x31, 0x33, 0x35, 0x37, 0x554, 0x556 };
160 static const u16 NCT6775_REG_IN_MIN[] = {
161 0x2c, 0x2e, 0x30, 0x32, 0x34, 0x36, 0x38, 0x555, 0x557 };
162 static const u16 NCT6775_REG_IN[] = {
163 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x550, 0x551, 0x552
166 static const u16 NCT6779_REG_IN[] = {
167 0x480, 0x481, 0x482, 0x483, 0x484, 0x485, 0x486, 0x487,
168 0x488, 0x489, 0x48a, 0x48b, 0x48c, 0x48d, 0x48e
171 #define NCT6775_REG_VBAT 0x5D
172 #define NCT6775_REG_DIODE 0x5E
174 #define NCT6775_REG_FANDIV1 0x506
175 #define NCT6775_REG_FANDIV2 0x507
177 #define NCT6775_REG_CR_FAN_DEBOUNCE 0xf0
179 static const u16 NCT6775_REG_ALARM[6] = { 0x459, 0x45A, 0x45B };
180 static const u16 NCT6779_REG_ALARM[6] = { 0x459, 0x45A, 0x45B, 0x568 };
182 #define NCT6775_REG_CASEOPEN 0x42
184 static const u8 NCT6775_CASEOPEN_MASK[] = { 0x10, 0x00 };
185 static const u8 NCT6776_CASEOPEN_MASK[] = { 0x10, 0x40 };
187 static const u8 NCT6775_REG_CR_CASEOPEN_CLR[] = { 0xe6, 0xee };
188 static const u8 NCT6775_CR_CASEOPEN_CLR_MASK[] = { 0x20, 0x01 };
190 /* DC or PWM output fan configuration */
191 static const u8 NCT6775_REG_PWM_MODE[] = { 0x04, 0x04, 0x12 };
192 static const u8 NCT6775_PWM_MODE_MASK[] = { 0x01, 0x02, 0x01 };
194 static const u8 NCT6776_REG_PWM_MODE[] = { 0x04, 0, 0 };
195 static const u8 NCT6776_PWM_MODE_MASK[] = { 0x01, 0, 0 };
197 /* Advanced Fan control, some values are common for all fans */
199 static const u16 NCT6775_REG_TARGET[] = { 0x101, 0x201, 0x301, 0x801, 0x901 };
200 static const u16 NCT6775_REG_FAN_MODE[] = { 0x102, 0x202, 0x302, 0x802, 0x902 };
201 static const u16 NCT6775_REG_FAN_STEP_DOWN_TIME[] = {
202 0x103, 0x203, 0x303, 0x803, 0x903 };
203 static const u16 NCT6775_REG_FAN_STEP_UP_TIME[] = {
204 0x104, 0x204, 0x304, 0x804, 0x904 };
205 static const u16 NCT6775_REG_FAN_STOP_OUTPUT[] = {
206 0x105, 0x205, 0x305, 0x805, 0x905 };
207 static const u16 NCT6775_REG_FAN_START_OUTPUT[]
208 = { 0x106, 0x206, 0x306, 0x806, 0x906 };
209 static const u16 NCT6775_REG_FAN_STOP_TIME[]
210 = { 0x107, 0x207, 0x307, 0x807, 0x907 };
211 static const u16 NCT6775_REG_PWM[] = { 0x109, 0x209, 0x309, 0x809, 0x909 };
212 static const u16 NCT6775_REG_PWM_READ[] = { 0x01, 0x03, 0x11, 0x13, 0x15 };
214 static const u16 NCT6775_REG_FAN[] = { 0x630, 0x632, 0x634, 0x636, 0x638 };
216 static const u16 NCT6776_REG_FAN_MIN[] = { 0x63a, 0x63c, 0x63e, 0x640, 0x642};
218 static const u16 NCT6779_REG_TOLERANCE_H[]
219 = { 0x10c, 0x20c, 0x30c, 0x40c, 0x50c };
221 static const u16 NCT6779_REG_FAN[] = { 0x4c0, 0x4c2, 0x4c4, 0x4c6, 0x4c8 };
223 static const u16 NCT6775_REG_TEMP[]
224 = { 0x27, 0x150, 0x250, 0x62b, 0x62c, 0x62d };
225 static const u16 NCT6779_REG_TEMP[] = { 0x27, 0x150 };
226 static const u16 NCT6779_REG_TEMP_FIXED[]
227 = { 0x490, 0x491, 0x492, 0x493, 0x494, 0x495};
229 static const u16 NCT6775_REG_TEMP_CONFIG[]
230 = { 0, 0x152, 0x252, 0x628, 0x629, 0x62A };
231 static const u16 NCT6775_REG_TEMP_HYST[]
232 = { 0x3a, 0x153, 0x253, 0x673, 0x678, 0x67D };
233 static const u16 NCT6775_REG_TEMP_OVER[]
234 = { 0x39, 0x155, 0x255, 0x672, 0x677, 0x67C };
236 static const u16 NCT6775_REG_TEMP_SOURCE[]
237 = { 0x621, 0x622, 0x623, 0x624, 0x625, 0x626 };
239 static const u16 NCT6775_REG_TEMP_SEL[]
240 = { 0x100, 0x200, 0x300, 0x800, 0x900 };
242 static const u16 NCT6775_REG_WEIGHT_TEMP_SEL[]
243 = { 0x139, 0x239, 0x339, 0x839, 0x939 };
244 static const u16 NCT6775_REG_WEIGHT_TEMP_STEP[]
245 = { 0x13a, 0x23a, 0x33a, 0x83a, 0x93a };
246 static const u16 NCT6775_REG_WEIGHT_TEMP_STEP_TOL[]
247 = { 0x13b, 0x23b, 0x33b, 0x83b, 0x93b };
248 static const u16 NCT6775_REG_WEIGHT_DUTY_STEP[]
249 = { 0x13c, 0x23c, 0x33c, 0x83c, 0x93c };
250 static const u16 NCT6775_REG_WEIGHT_TEMP_BASE[]
251 = { 0x13d, 0x23d, 0x33d, 0x83d, 0x93d };
253 static const u16 NCT6776_REG_WEIGHT_DUTY_BASE[]
254 = { 0x13e, 0x23e, 0x33e, 0x83e, 0x93e };
256 static const u16 NCT6775_REG_TEMP_OFFSET[] = { 0x454, 0x455, 0x456 };
258 static const u16 NCT6779_REG_TEMP_OFFSET[]
259 = { 0x454, 0x455, 0x456, 0x44a, 0x44b, 0x44c };
261 static const u16 NCT6776_REG_TEMP_CONFIG[11]
262 = { 0x18, 0x152, 0x252, 0x628, 0x629, 0x62A };
264 static const u16 NCT6775_REG_TEMP_MON[] = { 0x73, 0x75, 0x77, 0x79, 0x7b };
266 static const u16 NCT6775_REG_AUTO_TEMP[]
267 = { 0x121, 0x221, 0x321, 0x821, 0x921 };
268 static const u16 NCT6775_REG_AUTO_PWM[]
269 = { 0x127, 0x227, 0x327, 0x827, 0x927 };
271 #define NCT6775_AUTO_TEMP(data, nr, p) ((data)->REG_AUTO_TEMP[nr] + (p))
272 #define NCT6775_AUTO_PWM(data, nr, p) ((data)->REG_AUTO_PWM[nr] + (p))
274 static const u16 NCT6775_REG_CRITICAL_ENAB[] = { 0x134, 0x234, 0x334 };
276 static const u16 NCT6775_REG_CRITICAL_TEMP[] = {
277 0x135, 0x235, 0x335, 0x835, 0x935 };
278 static const u16 NCT6775_REG_CRITICAL_TEMP_TOLERANCE[] = {
279 0x138, 0x238, 0x338, 0x838, 0x938 };
281 static const u16 NCT6779_REG_CRITICAL_PWM_ENABLE[] = {
282 0x136, 0x236, 0x336, 0x836, 0x936 };
283 static const u16 NCT6779_REG_CRITICAL_PWM[] = {
284 0x137, 0x237, 0x337, 0x837, 0x937 };
286 static const char *const nct6775_temp_label[] = {
300 "PCH_CHIP_CPU_MAX_TEMP",
310 static const char *const nct6776_temp_label[] = {
325 "PCH_CHIP_CPU_MAX_TEMP",
336 static const char *const nct6779_temp_label[] = {
355 "PCH_CHIP_CPU_MAX_TEMP",
366 #define NUM_TEMP 10 /* Max number of temp attribute sets w/ limits*/
367 #define NUM_TEMP_FIXED 6 /* Max number of fixed temp attribute sets */
369 #define NUM_REG_TEMP ARRAY_SIZE(NCT6775_REG_TEMP)
371 static inline int reg_to_pwm_enable(int pwm, int mode)
373 if (mode == 0 && pwm == 255)
375 if (mode == 3) /* SmartFan III */
376 return 2; /* convert to thermal cruise */
379 return 4; /* SmartFan IV */
382 static inline int pwm_enable_to_reg(int mode)
395 /* 1 is DC mode, output in ms */
396 static inline unsigned int step_time_from_reg(u8 reg, u8 mode)
398 return mode ? 400 * reg : 100 * reg;
401 static inline u8 step_time_to_reg(unsigned int msec, u8 mode)
403 return SENSORS_LIMIT((mode ? (msec + 200) / 400 :
404 (msec + 50) / 100), 1, 255);
407 static unsigned int fan_from_reg8(u16 reg, unsigned int divreg)
409 if (reg == 0 || reg == 255)
411 return 1350000U / (reg << divreg);
414 static unsigned int fan_from_reg13(u16 reg, unsigned int divreg)
416 if ((reg & 0xff1f) == 0xff1f)
419 reg = (reg & 0x1f) | ((reg & 0xff00) >> 3);
424 return 1350000U / reg;
427 static unsigned int fan_from_reg16(u16 reg, unsigned int divreg)
429 if (reg == 0 || reg == 0xffff)
433 * Even though the registers are 16 bit wide, the fan divisor
436 return 1350000U / (reg << divreg);
439 static inline unsigned int
446 * Some of the voltage inputs have internal scaling, the tables below
447 * contain 8 (the ADC LSB in mV) * scaling factor * 100
449 static const u16 scale_in[15] = {
450 800, 800, 1600, 1600, 800, 800, 800, 1600, 1600, 800, 800, 800, 800,
454 static inline long in_from_reg(u8 reg, u8 nr)
456 return DIV_ROUND_CLOSEST(reg * scale_in[nr], 100);
459 static inline u8 in_to_reg(u32 val, u8 nr)
461 return SENSORS_LIMIT(DIV_ROUND_CLOSEST(val * 100, scale_in[nr]), 0,
466 * Data structures and manipulation thereof
469 struct nct6775_data {
470 int addr; /* IO base of hw monitor block */
474 struct device *hwmon_dev;
477 u16 reg_temp[3][NUM_TEMP]; /* 0=temp, 1=temp_over, 2=temp_hyst */
478 u8 temp_src[NUM_TEMP];
479 u16 reg_temp_config[NUM_TEMP];
480 const char * const *temp_label;
487 const u16 *REG_IN_MINMAX[2];
489 const u16 *REG_TARGET;
491 const u16 *REG_FAN_MODE;
492 const u16 *REG_FAN_MIN;
493 const u16 *REG_FAN_TIME[3];
495 const u8 *REG_PWM_MODE;
496 const u8 *PWM_MODE_MASK;
498 const u16 *REG_PWM[3];
499 const u16 *REG_PWM_READ;
501 const u16 *REG_TEMP_FIXED;
502 const u16 *REG_TEMP_MON;
503 const u16 *REG_AUTO_TEMP;
504 const u16 *REG_AUTO_PWM;
506 const u16 *REG_CRITICAL_TEMP;
507 const u16 *REG_CRITICAL_TEMP_TOLERANCE;
509 const u16 *REG_TEMP_SOURCE; /* temp register sources */
510 const u16 *REG_TEMP_SEL[2]; /* pwm temp, 0=base, 1=weight */
512 const u16 *REG_WEIGHT_TEMP[3]; /* 0=base, 1=hyst, 2=step */
513 const u16 *REG_WEIGHT_DUTY[2]; /* 0=step, 1=base */
515 const u16 *REG_TEMP_OFFSET;
517 const u16 *REG_ALARM;
520 const u8 *CASEOPEN_MASK;
522 unsigned int (*fan_from_reg)(u16 reg, unsigned int divreg);
523 unsigned int (*fan_from_reg_min)(u16 reg, unsigned int divreg);
525 struct mutex update_lock;
526 char valid; /* !=0 if following fields are valid */
527 unsigned long last_updated; /* In jiffies */
529 /* Register values */
530 u8 bank; /* current register bank */
531 u8 in_num; /* number of in inputs we have */
532 u8 in[15][3]; /* [0]=in, [1]=in_max, [2]=in_min */
537 u8 has_fan; /* some fan inputs can be disabled */
538 u8 has_fan_min; /* some fans don't have min register */
541 u8 temp_fixed_num; /* 3 or 6 */
542 u8 temp_type[NUM_TEMP_FIXED];
543 s8 temp_offset[NUM_TEMP_FIXED];
544 s16 temp[3][NUM_TEMP]; /* 0=temp, 1=temp_over, 2=temp_hyst */
548 u8 pwm_num; /* number of pwm */
550 u8 pwm_mode[5]; /* 1->DC variable voltage, 0->PWM variable duty cycle */
551 u8 pwm_enable[5]; /* 0->off
553 * 2->thermal cruise mode (also called SmartFan I)
554 * 3->fan speed cruise mode
555 * 4->enhanced variable thermal cruise (also called
558 u8 pwm[3][5]; /* [0]=pwm, [1]=fan_start_output, [2]=fan_stop_output */
563 u8 fan_time[3][5]; /* 0 = stop_time, 1 = step_up, 2 = step_down */
565 /* Automatic fan speed control registers */
570 u8 pwm_temp_sel[2][5];
572 bool pwm_sel_enable[2][5];/* 0->stop_val, 1->weight;
573 * false->off, true->on
575 u8 weight_temp[3][5]; /* 0->temp_step, 1->temp_step_tol,
578 u8 weight_duty[2][5]; /* 0->duty_step, 1->duty_base */
588 struct nct6775_sio_data {
593 static bool is_word_sized(struct nct6775_data *data, u16 reg)
595 switch (data->kind) {
598 return (((reg & 0xff00) == 0x100 ||
599 (reg & 0xff00) == 0x200) &&
600 ((reg & 0x00ff) == 0x50 ||
601 (reg & 0x00ff) == 0x53 ||
602 (reg & 0x00ff) == 0x55)) ||
603 (reg & 0xfff0) == 0x630 ||
604 reg == 0x640 || reg == 0x642 ||
605 ((reg & 0xfff0) == 0x650 && (reg & 0x000f) >= 0x06) ||
606 reg == 0x73 || reg == 0x75 || reg == 0x77;
608 return reg == 0x150 || reg == 0x153 || reg == 0x155 ||
609 ((reg & 0xfff0) == 0x4c0 && (reg & 0x000f) < 0x09) ||
610 reg == 0x63a || reg == 0x63c || reg == 0x63e ||
611 reg == 0x640 || reg == 0x642 ||
612 reg == 0x73 || reg == 0x75 || reg == 0x77 || reg == 0x79 ||
619 * On older chips, only registers 0x50-0x5f are banked.
620 * On more recent chips, all registers are banked.
621 * Assume that is the case and set the bank number for each access.
622 * Cache the bank number so it only needs to be set if it changes.
624 static inline void nct6775_set_bank(struct nct6775_data *data, u16 reg)
627 if (data->bank != bank) {
628 outb_p(NCT6775_REG_BANK, data->addr + ADDR_REG_OFFSET);
629 outb_p(bank, data->addr + DATA_REG_OFFSET);
634 static u16 nct6775_read_value(struct nct6775_data *data, u16 reg)
636 int res, word_sized = is_word_sized(data, reg);
638 mutex_lock(&data->lock);
640 nct6775_set_bank(data, reg);
641 outb_p(reg & 0xff, data->addr + ADDR_REG_OFFSET);
642 res = inb_p(data->addr + DATA_REG_OFFSET);
644 outb_p((reg & 0xff) + 1,
645 data->addr + ADDR_REG_OFFSET);
646 res = (res << 8) + inb_p(data->addr + DATA_REG_OFFSET);
649 mutex_unlock(&data->lock);
653 static int nct6775_write_value(struct nct6775_data *data, u16 reg, u16 value)
655 int word_sized = is_word_sized(data, reg);
657 mutex_lock(&data->lock);
659 nct6775_set_bank(data, reg);
660 outb_p(reg & 0xff, data->addr + ADDR_REG_OFFSET);
662 outb_p(value >> 8, data->addr + DATA_REG_OFFSET);
663 outb_p((reg & 0xff) + 1,
664 data->addr + ADDR_REG_OFFSET);
666 outb_p(value & 0xff, data->addr + DATA_REG_OFFSET);
668 mutex_unlock(&data->lock);
672 /* We left-align 8-bit temperature values to make the code simpler */
673 static u16 nct6775_read_temp(struct nct6775_data *data, u16 reg)
677 res = nct6775_read_value(data, reg);
678 if (!is_word_sized(data, reg))
684 static int nct6775_write_temp(struct nct6775_data *data, u16 reg, u16 value)
686 if (!is_word_sized(data, reg))
688 return nct6775_write_value(data, reg, value);
691 /* This function assumes that the caller holds data->update_lock */
692 static void nct6775_write_fan_div(struct nct6775_data *data, int nr)
698 reg = (nct6775_read_value(data, NCT6775_REG_FANDIV1) & 0x70)
699 | (data->fan_div[0] & 0x7);
700 nct6775_write_value(data, NCT6775_REG_FANDIV1, reg);
703 reg = (nct6775_read_value(data, NCT6775_REG_FANDIV1) & 0x7)
704 | ((data->fan_div[1] << 4) & 0x70);
705 nct6775_write_value(data, NCT6775_REG_FANDIV1, reg);
707 reg = (nct6775_read_value(data, NCT6775_REG_FANDIV2) & 0x70)
708 | (data->fan_div[2] & 0x7);
709 nct6775_write_value(data, NCT6775_REG_FANDIV2, reg);
712 reg = (nct6775_read_value(data, NCT6775_REG_FANDIV2) & 0x7)
713 | ((data->fan_div[3] << 4) & 0x70);
714 nct6775_write_value(data, NCT6775_REG_FANDIV2, reg);
719 static void nct6775_write_fan_div_common(struct device *dev,
720 struct nct6775_data *data, int nr)
722 if (data->kind == nct6775)
723 nct6775_write_fan_div(data, nr);
726 static void nct6775_update_fan_div(struct nct6775_data *data)
730 i = nct6775_read_value(data, NCT6775_REG_FANDIV1);
731 data->fan_div[0] = i & 0x7;
732 data->fan_div[1] = (i & 0x70) >> 4;
733 i = nct6775_read_value(data, NCT6775_REG_FANDIV2);
734 data->fan_div[2] = i & 0x7;
735 if (data->has_fan & (1<<3))
736 data->fan_div[3] = (i & 0x70) >> 4;
739 static void nct6775_update_fan_div_common(struct device *dev,
740 struct nct6775_data *data)
742 if (data->kind == nct6775)
743 nct6775_update_fan_div(data);
746 static void nct6775_update_pwm(struct device *dev)
748 struct nct6775_data *data = dev_get_drvdata(dev);
753 for (i = 0; i < data->pwm_num; i++) {
754 if (!(data->has_pwm & (1 << i)))
757 duty_is_dc = data->REG_PWM_MODE[i] &&
758 (nct6775_read_value(data, data->REG_PWM_MODE[i])
759 & data->PWM_MODE_MASK[i]);
760 data->pwm_mode[i] = duty_is_dc;
762 fanmodecfg = nct6775_read_value(data, data->REG_FAN_MODE[i]);
763 for (j = 0; j < 3; j++)
765 = nct6775_read_value(data, data->REG_PWM[j][i]);
767 data->pwm_enable[i] = reg_to_pwm_enable(data->pwm[0][i],
768 (fanmodecfg >> 4) & 7);
770 data->tolerance[i][0] = fanmodecfg & 0x0f;
771 if (data->kind == nct6779) {
772 reg = nct6775_read_value(data,
773 NCT6779_REG_TOLERANCE_H[i]);
774 data->tolerance[i][0] |= (reg & 0x70) >> 1;
776 data->tolerance[i][1] =
777 nct6775_read_value(data,
778 data->REG_CRITICAL_TEMP_TOLERANCE[i]);
780 data->pwm_sel_enable[0][i] =
781 nct6775_read_value(data, data->REG_TEMP_SEL[0][i])
783 data->pwm_sel_enable[1][i] =
784 nct6775_read_value(data, data->REG_TEMP_SEL[1][i])
788 for (j = 0; j < 2; j++) {
789 reg = nct6775_read_value(data,
790 data->REG_TEMP_SEL[j][i]);
791 data->pwm_temp_sel[j][i] = reg & 0x1f;
793 /* Weight temp data */
794 for (j = 0; j < 3; j++) {
795 data->weight_temp[j][i]
796 = nct6775_read_value(data,
797 data->REG_WEIGHT_TEMP[j][i]);
799 /* Weight duty (pwm) data */
800 for (j = 0; j < 2; j++) {
801 if (!data->REG_WEIGHT_DUTY[j])
803 data->weight_duty[j][i]
804 = nct6775_read_value(data,
805 data->REG_WEIGHT_DUTY[j][i]);
810 static void nct6775_update_pwm_limits(struct device *dev)
812 struct nct6775_data *data = dev_get_drvdata(dev);
816 for (i = 0; i < data->pwm_num; i++) {
817 if (!(data->has_pwm & (1 << i)))
820 for (j = 0; j < 3; j++) {
821 data->fan_time[j][i] =
822 nct6775_read_value(data, data->REG_FAN_TIME[j][i]);
825 data->target_temp[i] =
826 nct6775_read_value(data, data->REG_TARGET[i]) &
827 (data->pwm_mode[i] ? 0xff : 0x7f);
829 nct6775_read_value(data, data->REG_TEMP_MON[i]);
831 for (j = 0; j < data->auto_pwm_num; j++) {
832 data->auto_pwm[i][j] =
833 nct6775_read_value(data,
834 NCT6775_AUTO_PWM(data, i, j));
835 data->auto_temp[i][j] =
836 nct6775_read_value(data,
837 NCT6775_AUTO_TEMP(data, i, j));
840 /* critical auto_pwm temperature data */
841 data->auto_temp[i][data->auto_pwm_num] =
842 nct6775_read_value(data, data->REG_CRITICAL_TEMP[i]);
844 switch (data->kind) {
846 reg = nct6775_read_value(data,
847 NCT6775_REG_CRITICAL_ENAB[i]);
848 data->auto_pwm[i][data->auto_pwm_num] =
849 (reg & 0x02) ? 0xff : 0x00;
852 data->auto_pwm[i][data->auto_pwm_num] = 0xff;
855 reg = nct6775_read_value(data,
856 NCT6779_REG_CRITICAL_PWM_ENABLE[i]);
858 data->auto_pwm[i][data->auto_pwm_num] =
859 nct6775_read_value(data,
860 NCT6779_REG_CRITICAL_PWM[i]);
862 data->auto_pwm[i][data->auto_pwm_num] = 0xff;
868 static struct nct6775_data *nct6775_update_device(struct device *dev)
870 struct nct6775_data *data = dev_get_drvdata(dev);
873 mutex_lock(&data->update_lock);
875 if (time_after(jiffies, data->last_updated + HZ + HZ/2)
877 /* Fan clock dividers */
878 nct6775_update_fan_div_common(dev, data);
880 /* Measured voltages and limits */
881 for (i = 0; i < data->in_num; i++) {
882 if (!(data->have_in & (1 << i)))
885 data->in[i][0] = nct6775_read_value(data,
887 data->in[i][1] = nct6775_read_value(data,
888 data->REG_IN_MINMAX[0][i]);
889 data->in[i][2] = nct6775_read_value(data,
890 data->REG_IN_MINMAX[1][i]);
893 /* Measured fan speeds and limits */
894 for (i = 0; i < 5; i++) {
897 if (!(data->has_fan & (1 << i)))
900 reg = nct6775_read_value(data, data->REG_FAN[i]);
901 data->rpm[i] = data->fan_from_reg(reg,
904 if (data->has_fan_min & (1 << i))
905 data->fan_min[i] = nct6775_read_value(data,
906 data->REG_FAN_MIN[i]);
909 * If we failed to measure the fan speed and clock
910 * divider can be increased, let's try that for next
913 if (data->has_fan_div
914 && (reg >= 0xff || (data->kind == nct6775
916 && data->fan_div[i] < 0x07) {
917 dev_dbg(dev, "Increasing fan%d "
918 "clock divider from %u to %u\n",
919 i + 1, div_from_reg(data->fan_div[i]),
920 div_from_reg(data->fan_div[i] + 1));
922 nct6775_write_fan_div_common(dev, data, i);
923 /* Preserve min limit if possible */
924 if ((data->has_fan_min & (1 << i))
925 && data->fan_min[i] >= 2
926 && data->fan_min[i] != 255)
927 nct6775_write_value(data,
928 data->REG_FAN_MIN[i],
929 (data->fan_min[i] /= 2));
933 nct6775_update_pwm(dev);
934 nct6775_update_pwm_limits(dev);
936 /* Measured temperatures and limits */
937 for (i = 0; i < NUM_TEMP; i++) {
938 if (!(data->have_temp & (1 << i)))
940 for (j = 0; j < 3; j++) {
941 if (data->reg_temp[j][i])
943 = nct6775_read_temp(data,
944 data->reg_temp[j][i]);
946 if (!(data->have_temp_fixed & (1 << i)))
949 = nct6775_read_value(data, data->REG_TEMP_OFFSET[i]);
953 for (i = 0; i < 6; i++) {
955 if (!data->REG_ALARM[i])
957 alarm = nct6775_read_value(data, data->REG_ALARM[i]);
958 data->alarms |= (((u64)alarm) << (i << 3));
961 data->caseopen = nct6775_read_value(data, data->REG_CASEOPEN);
963 data->last_updated = jiffies;
967 mutex_unlock(&data->update_lock);
972 * Sysfs callback functions
975 show_in_reg(struct device *dev, struct device_attribute *attr, char *buf)
977 struct nct6775_data *data = nct6775_update_device(dev);
978 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
980 int index = sattr->index;
981 return sprintf(buf, "%ld\n", in_from_reg(data->in[nr][index], nr));
985 store_in_reg(struct device *dev, struct device_attribute *attr, const char *buf,
988 struct nct6775_data *data = dev_get_drvdata(dev);
989 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
991 int index = sattr->index;
993 int err = kstrtoul(buf, 10, &val);
996 mutex_lock(&data->update_lock);
997 data->in[nr][index] = in_to_reg(val, nr);
998 nct6775_write_value(data, data->REG_IN_MINMAX[index-1][nr],
999 data->in[nr][index]);
1000 mutex_unlock(&data->update_lock);
1005 show_alarm(struct device *dev, struct device_attribute *attr, char *buf)
1007 struct nct6775_data *data = nct6775_update_device(dev);
1008 struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr);
1009 int nr = sattr->index;
1010 return sprintf(buf, "%u\n",
1011 (unsigned int)((data->alarms >> nr) & 0x01));
1014 static SENSOR_DEVICE_ATTR_2(in0_input, S_IRUGO, show_in_reg, NULL, 0, 0);
1015 static SENSOR_DEVICE_ATTR_2(in1_input, S_IRUGO, show_in_reg, NULL, 1, 0);
1016 static SENSOR_DEVICE_ATTR_2(in2_input, S_IRUGO, show_in_reg, NULL, 2, 0);
1017 static SENSOR_DEVICE_ATTR_2(in3_input, S_IRUGO, show_in_reg, NULL, 3, 0);
1018 static SENSOR_DEVICE_ATTR_2(in4_input, S_IRUGO, show_in_reg, NULL, 4, 0);
1019 static SENSOR_DEVICE_ATTR_2(in5_input, S_IRUGO, show_in_reg, NULL, 5, 0);
1020 static SENSOR_DEVICE_ATTR_2(in6_input, S_IRUGO, show_in_reg, NULL, 6, 0);
1021 static SENSOR_DEVICE_ATTR_2(in7_input, S_IRUGO, show_in_reg, NULL, 7, 0);
1022 static SENSOR_DEVICE_ATTR_2(in8_input, S_IRUGO, show_in_reg, NULL, 8, 0);
1023 static SENSOR_DEVICE_ATTR_2(in9_input, S_IRUGO, show_in_reg, NULL, 9, 0);
1024 static SENSOR_DEVICE_ATTR_2(in10_input, S_IRUGO, show_in_reg, NULL, 10, 0);
1025 static SENSOR_DEVICE_ATTR_2(in11_input, S_IRUGO, show_in_reg, NULL, 11, 0);
1026 static SENSOR_DEVICE_ATTR_2(in12_input, S_IRUGO, show_in_reg, NULL, 12, 0);
1027 static SENSOR_DEVICE_ATTR_2(in13_input, S_IRUGO, show_in_reg, NULL, 13, 0);
1028 static SENSOR_DEVICE_ATTR_2(in14_input, S_IRUGO, show_in_reg, NULL, 14, 0);
1030 static SENSOR_DEVICE_ATTR(in0_alarm, S_IRUGO, show_alarm, NULL, 0);
1031 static SENSOR_DEVICE_ATTR(in1_alarm, S_IRUGO, show_alarm, NULL, 1);
1032 static SENSOR_DEVICE_ATTR(in2_alarm, S_IRUGO, show_alarm, NULL, 2);
1033 static SENSOR_DEVICE_ATTR(in3_alarm, S_IRUGO, show_alarm, NULL, 3);
1034 static SENSOR_DEVICE_ATTR(in4_alarm, S_IRUGO, show_alarm, NULL, 8);
1035 static SENSOR_DEVICE_ATTR(in5_alarm, S_IRUGO, show_alarm, NULL, 21);
1036 static SENSOR_DEVICE_ATTR(in6_alarm, S_IRUGO, show_alarm, NULL, 20);
1037 static SENSOR_DEVICE_ATTR(in7_alarm, S_IRUGO, show_alarm, NULL, 16);
1038 static SENSOR_DEVICE_ATTR(in8_alarm, S_IRUGO, show_alarm, NULL, 17);
1039 static SENSOR_DEVICE_ATTR(in9_alarm, S_IRUGO, show_alarm, NULL, 24);
1040 static SENSOR_DEVICE_ATTR(in10_alarm, S_IRUGO, show_alarm, NULL, 25);
1041 static SENSOR_DEVICE_ATTR(in11_alarm, S_IRUGO, show_alarm, NULL, 26);
1042 static SENSOR_DEVICE_ATTR(in12_alarm, S_IRUGO, show_alarm, NULL, 27);
1043 static SENSOR_DEVICE_ATTR(in13_alarm, S_IRUGO, show_alarm, NULL, 28);
1044 static SENSOR_DEVICE_ATTR(in14_alarm, S_IRUGO, show_alarm, NULL, 29);
1046 static SENSOR_DEVICE_ATTR_2(in0_min, S_IWUSR | S_IRUGO, show_in_reg,
1047 store_in_reg, 0, 1);
1048 static SENSOR_DEVICE_ATTR_2(in1_min, S_IWUSR | S_IRUGO, show_in_reg,
1049 store_in_reg, 1, 1);
1050 static SENSOR_DEVICE_ATTR_2(in2_min, S_IWUSR | S_IRUGO, show_in_reg,
1051 store_in_reg, 2, 1);
1052 static SENSOR_DEVICE_ATTR_2(in3_min, S_IWUSR | S_IRUGO, show_in_reg,
1053 store_in_reg, 3, 1);
1054 static SENSOR_DEVICE_ATTR_2(in4_min, S_IWUSR | S_IRUGO, show_in_reg,
1055 store_in_reg, 4, 1);
1056 static SENSOR_DEVICE_ATTR_2(in5_min, S_IWUSR | S_IRUGO, show_in_reg,
1057 store_in_reg, 5, 1);
1058 static SENSOR_DEVICE_ATTR_2(in6_min, S_IWUSR | S_IRUGO, show_in_reg,
1059 store_in_reg, 6, 1);
1060 static SENSOR_DEVICE_ATTR_2(in7_min, S_IWUSR | S_IRUGO, show_in_reg,
1061 store_in_reg, 7, 1);
1062 static SENSOR_DEVICE_ATTR_2(in8_min, S_IWUSR | S_IRUGO, show_in_reg,
1063 store_in_reg, 8, 1);
1064 static SENSOR_DEVICE_ATTR_2(in9_min, S_IWUSR | S_IRUGO, show_in_reg,
1065 store_in_reg, 9, 1);
1066 static SENSOR_DEVICE_ATTR_2(in10_min, S_IWUSR | S_IRUGO, show_in_reg,
1067 store_in_reg, 10, 1);
1068 static SENSOR_DEVICE_ATTR_2(in11_min, S_IWUSR | S_IRUGO, show_in_reg,
1069 store_in_reg, 11, 1);
1070 static SENSOR_DEVICE_ATTR_2(in12_min, S_IWUSR | S_IRUGO, show_in_reg,
1071 store_in_reg, 12, 1);
1072 static SENSOR_DEVICE_ATTR_2(in13_min, S_IWUSR | S_IRUGO, show_in_reg,
1073 store_in_reg, 13, 1);
1074 static SENSOR_DEVICE_ATTR_2(in14_min, S_IWUSR | S_IRUGO, show_in_reg,
1075 store_in_reg, 14, 1);
1077 static SENSOR_DEVICE_ATTR_2(in0_max, S_IWUSR | S_IRUGO, show_in_reg,
1078 store_in_reg, 0, 2);
1079 static SENSOR_DEVICE_ATTR_2(in1_max, S_IWUSR | S_IRUGO, show_in_reg,
1080 store_in_reg, 1, 2);
1081 static SENSOR_DEVICE_ATTR_2(in2_max, S_IWUSR | S_IRUGO, show_in_reg,
1082 store_in_reg, 2, 2);
1083 static SENSOR_DEVICE_ATTR_2(in3_max, S_IWUSR | S_IRUGO, show_in_reg,
1084 store_in_reg, 3, 2);
1085 static SENSOR_DEVICE_ATTR_2(in4_max, S_IWUSR | S_IRUGO, show_in_reg,
1086 store_in_reg, 4, 2);
1087 static SENSOR_DEVICE_ATTR_2(in5_max, S_IWUSR | S_IRUGO, show_in_reg,
1088 store_in_reg, 5, 2);
1089 static SENSOR_DEVICE_ATTR_2(in6_max, S_IWUSR | S_IRUGO, show_in_reg,
1090 store_in_reg, 6, 2);
1091 static SENSOR_DEVICE_ATTR_2(in7_max, S_IWUSR | S_IRUGO, show_in_reg,
1092 store_in_reg, 7, 2);
1093 static SENSOR_DEVICE_ATTR_2(in8_max, S_IWUSR | S_IRUGO, show_in_reg,
1094 store_in_reg, 8, 2);
1095 static SENSOR_DEVICE_ATTR_2(in9_max, S_IWUSR | S_IRUGO, show_in_reg,
1096 store_in_reg, 9, 2);
1097 static SENSOR_DEVICE_ATTR_2(in10_max, S_IWUSR | S_IRUGO, show_in_reg,
1098 store_in_reg, 10, 2);
1099 static SENSOR_DEVICE_ATTR_2(in11_max, S_IWUSR | S_IRUGO, show_in_reg,
1100 store_in_reg, 11, 2);
1101 static SENSOR_DEVICE_ATTR_2(in12_max, S_IWUSR | S_IRUGO, show_in_reg,
1102 store_in_reg, 12, 2);
1103 static SENSOR_DEVICE_ATTR_2(in13_max, S_IWUSR | S_IRUGO, show_in_reg,
1104 store_in_reg, 13, 2);
1105 static SENSOR_DEVICE_ATTR_2(in14_max, S_IWUSR | S_IRUGO, show_in_reg,
1106 store_in_reg, 14, 2);
1108 static struct attribute *nct6775_attributes_in[15][5] = {
1110 &sensor_dev_attr_in0_input.dev_attr.attr,
1111 &sensor_dev_attr_in0_min.dev_attr.attr,
1112 &sensor_dev_attr_in0_max.dev_attr.attr,
1113 &sensor_dev_attr_in0_alarm.dev_attr.attr,
1117 &sensor_dev_attr_in1_input.dev_attr.attr,
1118 &sensor_dev_attr_in1_min.dev_attr.attr,
1119 &sensor_dev_attr_in1_max.dev_attr.attr,
1120 &sensor_dev_attr_in1_alarm.dev_attr.attr,
1124 &sensor_dev_attr_in2_input.dev_attr.attr,
1125 &sensor_dev_attr_in2_min.dev_attr.attr,
1126 &sensor_dev_attr_in2_max.dev_attr.attr,
1127 &sensor_dev_attr_in2_alarm.dev_attr.attr,
1131 &sensor_dev_attr_in3_input.dev_attr.attr,
1132 &sensor_dev_attr_in3_min.dev_attr.attr,
1133 &sensor_dev_attr_in3_max.dev_attr.attr,
1134 &sensor_dev_attr_in3_alarm.dev_attr.attr,
1138 &sensor_dev_attr_in4_input.dev_attr.attr,
1139 &sensor_dev_attr_in4_min.dev_attr.attr,
1140 &sensor_dev_attr_in4_max.dev_attr.attr,
1141 &sensor_dev_attr_in4_alarm.dev_attr.attr,
1145 &sensor_dev_attr_in5_input.dev_attr.attr,
1146 &sensor_dev_attr_in5_min.dev_attr.attr,
1147 &sensor_dev_attr_in5_max.dev_attr.attr,
1148 &sensor_dev_attr_in5_alarm.dev_attr.attr,
1152 &sensor_dev_attr_in6_input.dev_attr.attr,
1153 &sensor_dev_attr_in6_min.dev_attr.attr,
1154 &sensor_dev_attr_in6_max.dev_attr.attr,
1155 &sensor_dev_attr_in6_alarm.dev_attr.attr,
1159 &sensor_dev_attr_in7_input.dev_attr.attr,
1160 &sensor_dev_attr_in7_min.dev_attr.attr,
1161 &sensor_dev_attr_in7_max.dev_attr.attr,
1162 &sensor_dev_attr_in7_alarm.dev_attr.attr,
1166 &sensor_dev_attr_in8_input.dev_attr.attr,
1167 &sensor_dev_attr_in8_min.dev_attr.attr,
1168 &sensor_dev_attr_in8_max.dev_attr.attr,
1169 &sensor_dev_attr_in8_alarm.dev_attr.attr,
1173 &sensor_dev_attr_in9_input.dev_attr.attr,
1174 &sensor_dev_attr_in9_min.dev_attr.attr,
1175 &sensor_dev_attr_in9_max.dev_attr.attr,
1176 &sensor_dev_attr_in9_alarm.dev_attr.attr,
1180 &sensor_dev_attr_in10_input.dev_attr.attr,
1181 &sensor_dev_attr_in10_min.dev_attr.attr,
1182 &sensor_dev_attr_in10_max.dev_attr.attr,
1183 &sensor_dev_attr_in10_alarm.dev_attr.attr,
1187 &sensor_dev_attr_in11_input.dev_attr.attr,
1188 &sensor_dev_attr_in11_min.dev_attr.attr,
1189 &sensor_dev_attr_in11_max.dev_attr.attr,
1190 &sensor_dev_attr_in11_alarm.dev_attr.attr,
1194 &sensor_dev_attr_in12_input.dev_attr.attr,
1195 &sensor_dev_attr_in12_min.dev_attr.attr,
1196 &sensor_dev_attr_in12_max.dev_attr.attr,
1197 &sensor_dev_attr_in12_alarm.dev_attr.attr,
1201 &sensor_dev_attr_in13_input.dev_attr.attr,
1202 &sensor_dev_attr_in13_min.dev_attr.attr,
1203 &sensor_dev_attr_in13_max.dev_attr.attr,
1204 &sensor_dev_attr_in13_alarm.dev_attr.attr,
1208 &sensor_dev_attr_in14_input.dev_attr.attr,
1209 &sensor_dev_attr_in14_min.dev_attr.attr,
1210 &sensor_dev_attr_in14_max.dev_attr.attr,
1211 &sensor_dev_attr_in14_alarm.dev_attr.attr,
1216 static const struct attribute_group nct6775_group_in[15] = {
1217 { .attrs = nct6775_attributes_in[0] },
1218 { .attrs = nct6775_attributes_in[1] },
1219 { .attrs = nct6775_attributes_in[2] },
1220 { .attrs = nct6775_attributes_in[3] },
1221 { .attrs = nct6775_attributes_in[4] },
1222 { .attrs = nct6775_attributes_in[5] },
1223 { .attrs = nct6775_attributes_in[6] },
1224 { .attrs = nct6775_attributes_in[7] },
1225 { .attrs = nct6775_attributes_in[8] },
1226 { .attrs = nct6775_attributes_in[9] },
1227 { .attrs = nct6775_attributes_in[10] },
1228 { .attrs = nct6775_attributes_in[11] },
1229 { .attrs = nct6775_attributes_in[12] },
1230 { .attrs = nct6775_attributes_in[13] },
1231 { .attrs = nct6775_attributes_in[14] },
1235 show_fan(struct device *dev, struct device_attribute *attr, char *buf)
1237 struct nct6775_data *data = nct6775_update_device(dev);
1238 struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr);
1239 int nr = sattr->index;
1240 return sprintf(buf, "%d\n", data->rpm[nr]);
1244 show_fan_min(struct device *dev, struct device_attribute *attr, char *buf)
1246 struct nct6775_data *data = nct6775_update_device(dev);
1247 struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr);
1248 int nr = sattr->index;
1249 return sprintf(buf, "%d\n",
1250 data->fan_from_reg_min(data->fan_min[nr],
1251 data->fan_div[nr]));
1255 show_fan_div(struct device *dev, struct device_attribute *attr, char *buf)
1257 struct nct6775_data *data = nct6775_update_device(dev);
1258 struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr);
1259 int nr = sattr->index;
1260 return sprintf(buf, "%u\n", div_from_reg(data->fan_div[nr]));
1264 store_fan_min(struct device *dev, struct device_attribute *attr,
1265 const char *buf, size_t count)
1267 struct nct6775_data *data = dev_get_drvdata(dev);
1268 struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr);
1269 int nr = sattr->index;
1275 err = kstrtoul(buf, 10, &val);
1279 mutex_lock(&data->update_lock);
1280 if (!data->has_fan_div) {
1282 * Only NCT6776F for now, so we know that this is a 13 bit
1290 val = 1350000U / val;
1291 val = (val & 0x1f) | ((val << 3) & 0xff00);
1293 data->fan_min[nr] = val;
1294 goto write_min; /* Leave fan divider alone */
1297 /* No min limit, alarm disabled */
1298 data->fan_min[nr] = 255;
1299 new_div = data->fan_div[nr]; /* No change */
1300 dev_info(dev, "fan%u low limit and alarm disabled\n", nr + 1);
1303 reg = 1350000U / val;
1304 if (reg >= 128 * 255) {
1306 * Speed below this value cannot possibly be represented,
1307 * even with the highest divider (128)
1309 data->fan_min[nr] = 254;
1310 new_div = 7; /* 128 == (1 << 7) */
1311 dev_warn(dev, "fan%u low limit %lu below minimum %u, set to "
1312 "minimum\n", nr + 1, val,
1313 data->fan_from_reg_min(254, 7));
1316 * Speed above this value cannot possibly be represented,
1317 * even with the lowest divider (1)
1319 data->fan_min[nr] = 1;
1320 new_div = 0; /* 1 == (1 << 0) */
1321 dev_warn(dev, "fan%u low limit %lu above maximum %u, set to "
1322 "maximum\n", nr + 1, val,
1323 data->fan_from_reg_min(1, 0));
1326 * Automatically pick the best divider, i.e. the one such
1327 * that the min limit will correspond to a register value
1328 * in the 96..192 range
1331 while (reg > 192 && new_div < 7) {
1335 data->fan_min[nr] = reg;
1340 * Write both the fan clock divider (if it changed) and the new
1341 * fan min (unconditionally)
1343 if (new_div != data->fan_div[nr]) {
1344 dev_dbg(dev, "fan%u clock divider changed from %u to %u\n",
1345 nr + 1, div_from_reg(data->fan_div[nr]),
1346 div_from_reg(new_div));
1347 data->fan_div[nr] = new_div;
1348 nct6775_write_fan_div_common(dev, data, nr);
1349 /* Give the chip time to sample a new speed value */
1350 data->last_updated = jiffies;
1354 nct6775_write_value(data, data->REG_FAN_MIN[nr],
1356 mutex_unlock(&data->update_lock);
1361 static struct sensor_device_attribute sda_fan_input[] = {
1362 SENSOR_ATTR(fan1_input, S_IRUGO, show_fan, NULL, 0),
1363 SENSOR_ATTR(fan2_input, S_IRUGO, show_fan, NULL, 1),
1364 SENSOR_ATTR(fan3_input, S_IRUGO, show_fan, NULL, 2),
1365 SENSOR_ATTR(fan4_input, S_IRUGO, show_fan, NULL, 3),
1366 SENSOR_ATTR(fan5_input, S_IRUGO, show_fan, NULL, 4),
1369 static struct sensor_device_attribute sda_fan_alarm[] = {
1370 SENSOR_ATTR(fan1_alarm, S_IRUGO, show_alarm, NULL, 6),
1371 SENSOR_ATTR(fan2_alarm, S_IRUGO, show_alarm, NULL, 7),
1372 SENSOR_ATTR(fan3_alarm, S_IRUGO, show_alarm, NULL, 11),
1373 SENSOR_ATTR(fan4_alarm, S_IRUGO, show_alarm, NULL, 10),
1374 SENSOR_ATTR(fan5_alarm, S_IRUGO, show_alarm, NULL, 23),
1377 static struct sensor_device_attribute sda_fan_min[] = {
1378 SENSOR_ATTR(fan1_min, S_IWUSR | S_IRUGO, show_fan_min,
1380 SENSOR_ATTR(fan2_min, S_IWUSR | S_IRUGO, show_fan_min,
1382 SENSOR_ATTR(fan3_min, S_IWUSR | S_IRUGO, show_fan_min,
1384 SENSOR_ATTR(fan4_min, S_IWUSR | S_IRUGO, show_fan_min,
1386 SENSOR_ATTR(fan5_min, S_IWUSR | S_IRUGO, show_fan_min,
1390 static struct sensor_device_attribute sda_fan_div[] = {
1391 SENSOR_ATTR(fan1_div, S_IRUGO, show_fan_div, NULL, 0),
1392 SENSOR_ATTR(fan2_div, S_IRUGO, show_fan_div, NULL, 1),
1393 SENSOR_ATTR(fan3_div, S_IRUGO, show_fan_div, NULL, 2),
1394 SENSOR_ATTR(fan4_div, S_IRUGO, show_fan_div, NULL, 3),
1395 SENSOR_ATTR(fan5_div, S_IRUGO, show_fan_div, NULL, 4),
1399 show_temp_label(struct device *dev, struct device_attribute *attr, char *buf)
1401 struct nct6775_data *data = nct6775_update_device(dev);
1402 struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr);
1403 int nr = sattr->index;
1404 return sprintf(buf, "%s\n", data->temp_label[data->temp_src[nr]]);
1408 show_temp(struct device *dev, struct device_attribute *attr, char *buf)
1410 struct nct6775_data *data = nct6775_update_device(dev);
1411 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
1413 int index = sattr->index;
1415 return sprintf(buf, "%d\n", LM75_TEMP_FROM_REG(data->temp[index][nr]));
1419 store_temp(struct device *dev, struct device_attribute *attr, const char *buf,
1422 struct nct6775_data *data = dev_get_drvdata(dev);
1423 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
1425 int index = sattr->index;
1429 err = kstrtol(buf, 10, &val);
1433 mutex_lock(&data->update_lock);
1434 data->temp[index][nr] = LM75_TEMP_TO_REG(val);
1435 nct6775_write_temp(data, data->reg_temp[index][nr],
1436 data->temp[index][nr]);
1437 mutex_unlock(&data->update_lock);
1442 show_temp_offset(struct device *dev, struct device_attribute *attr, char *buf)
1444 struct nct6775_data *data = nct6775_update_device(dev);
1445 struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr);
1447 return sprintf(buf, "%d\n", data->temp_offset[sattr->index] * 1000);
1451 store_temp_offset(struct device *dev, struct device_attribute *attr,
1452 const char *buf, size_t count)
1454 struct nct6775_data *data = dev_get_drvdata(dev);
1455 struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr);
1456 int nr = sattr->index;
1460 err = kstrtol(buf, 10, &val);
1464 val = SENSORS_LIMIT(DIV_ROUND_CLOSEST(val, 1000), -128, 127);
1466 mutex_lock(&data->update_lock);
1467 data->temp_offset[nr] = val;
1468 nct6775_write_value(data, data->REG_TEMP_OFFSET[nr], val);
1469 mutex_unlock(&data->update_lock);
1475 show_temp_type(struct device *dev, struct device_attribute *attr, char *buf)
1477 struct nct6775_data *data = nct6775_update_device(dev);
1478 struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr);
1479 int nr = sattr->index;
1480 return sprintf(buf, "%d\n", (int)data->temp_type[nr]);
1484 store_temp_type(struct device *dev, struct device_attribute *attr,
1485 const char *buf, size_t count)
1487 struct nct6775_data *data = nct6775_update_device(dev);
1488 struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr);
1489 int nr = sattr->index;
1492 u8 vbat, diode, bit;
1494 err = kstrtoul(buf, 10, &val);
1498 if (val != 1 && val != 3 && val != 4)
1501 mutex_lock(&data->update_lock);
1503 data->temp_type[nr] = val;
1504 vbat = nct6775_read_value(data, data->REG_VBAT) & ~(0x02 << nr);
1505 diode = nct6775_read_value(data, data->REG_DIODE) & ~(0x02 << nr);
1508 case 1: /* CPU diode (diode, current mode) */
1512 case 3: /* diode, voltage mode */
1515 case 4: /* thermistor */
1518 nct6775_write_value(data, data->REG_VBAT, vbat);
1519 nct6775_write_value(data, data->REG_DIODE, diode);
1521 mutex_unlock(&data->update_lock);
1525 static struct sensor_device_attribute_2 sda_temp_input[] = {
1526 SENSOR_ATTR_2(temp1_input, S_IRUGO, show_temp, NULL, 0, 0),
1527 SENSOR_ATTR_2(temp2_input, S_IRUGO, show_temp, NULL, 1, 0),
1528 SENSOR_ATTR_2(temp3_input, S_IRUGO, show_temp, NULL, 2, 0),
1529 SENSOR_ATTR_2(temp4_input, S_IRUGO, show_temp, NULL, 3, 0),
1530 SENSOR_ATTR_2(temp5_input, S_IRUGO, show_temp, NULL, 4, 0),
1531 SENSOR_ATTR_2(temp6_input, S_IRUGO, show_temp, NULL, 5, 0),
1532 SENSOR_ATTR_2(temp7_input, S_IRUGO, show_temp, NULL, 6, 0),
1533 SENSOR_ATTR_2(temp8_input, S_IRUGO, show_temp, NULL, 7, 0),
1534 SENSOR_ATTR_2(temp9_input, S_IRUGO, show_temp, NULL, 8, 0),
1535 SENSOR_ATTR_2(temp10_input, S_IRUGO, show_temp, NULL, 9, 0),
1538 static struct sensor_device_attribute sda_temp_label[] = {
1539 SENSOR_ATTR(temp1_label, S_IRUGO, show_temp_label, NULL, 0),
1540 SENSOR_ATTR(temp2_label, S_IRUGO, show_temp_label, NULL, 1),
1541 SENSOR_ATTR(temp3_label, S_IRUGO, show_temp_label, NULL, 2),
1542 SENSOR_ATTR(temp4_label, S_IRUGO, show_temp_label, NULL, 3),
1543 SENSOR_ATTR(temp5_label, S_IRUGO, show_temp_label, NULL, 4),
1544 SENSOR_ATTR(temp6_label, S_IRUGO, show_temp_label, NULL, 5),
1545 SENSOR_ATTR(temp7_label, S_IRUGO, show_temp_label, NULL, 6),
1546 SENSOR_ATTR(temp8_label, S_IRUGO, show_temp_label, NULL, 7),
1547 SENSOR_ATTR(temp9_label, S_IRUGO, show_temp_label, NULL, 8),
1548 SENSOR_ATTR(temp10_label, S_IRUGO, show_temp_label, NULL, 9),
1551 static struct sensor_device_attribute_2 sda_temp_max[] = {
1552 SENSOR_ATTR_2(temp1_max, S_IRUGO | S_IWUSR, show_temp, store_temp,
1554 SENSOR_ATTR_2(temp2_max, S_IRUGO | S_IWUSR, show_temp, store_temp,
1556 SENSOR_ATTR_2(temp3_max, S_IRUGO | S_IWUSR, show_temp, store_temp,
1558 SENSOR_ATTR_2(temp4_max, S_IRUGO | S_IWUSR, show_temp, store_temp,
1560 SENSOR_ATTR_2(temp5_max, S_IRUGO | S_IWUSR, show_temp, store_temp,
1562 SENSOR_ATTR_2(temp6_max, S_IRUGO | S_IWUSR, show_temp, store_temp,
1564 SENSOR_ATTR_2(temp7_max, S_IRUGO | S_IWUSR, show_temp, store_temp,
1566 SENSOR_ATTR_2(temp8_max, S_IRUGO | S_IWUSR, show_temp, store_temp,
1568 SENSOR_ATTR_2(temp9_max, S_IRUGO | S_IWUSR, show_temp, store_temp,
1570 SENSOR_ATTR_2(temp10_max, S_IRUGO | S_IWUSR, show_temp, store_temp,
1574 static struct sensor_device_attribute_2 sda_temp_max_hyst[] = {
1575 SENSOR_ATTR_2(temp1_max_hyst, S_IRUGO | S_IWUSR, show_temp, store_temp,
1577 SENSOR_ATTR_2(temp2_max_hyst, S_IRUGO | S_IWUSR, show_temp, store_temp,
1579 SENSOR_ATTR_2(temp3_max_hyst, S_IRUGO | S_IWUSR, show_temp, store_temp,
1581 SENSOR_ATTR_2(temp4_max_hyst, S_IRUGO | S_IWUSR, show_temp, store_temp,
1583 SENSOR_ATTR_2(temp5_max_hyst, S_IRUGO | S_IWUSR, show_temp, store_temp,
1585 SENSOR_ATTR_2(temp6_max_hyst, S_IRUGO | S_IWUSR, show_temp, store_temp,
1587 SENSOR_ATTR_2(temp7_max_hyst, S_IRUGO | S_IWUSR, show_temp, store_temp,
1589 SENSOR_ATTR_2(temp8_max_hyst, S_IRUGO | S_IWUSR, show_temp, store_temp,
1591 SENSOR_ATTR_2(temp9_max_hyst, S_IRUGO | S_IWUSR, show_temp, store_temp,
1593 SENSOR_ATTR_2(temp9_max_hyst, S_IRUGO | S_IWUSR, show_temp, store_temp,
1597 static struct sensor_device_attribute sda_temp_offset[] = {
1598 SENSOR_ATTR(temp1_offset, S_IRUGO | S_IWUSR, show_temp_offset,
1599 store_temp_offset, 0),
1600 SENSOR_ATTR(temp2_offset, S_IRUGO | S_IWUSR, show_temp_offset,
1601 store_temp_offset, 1),
1602 SENSOR_ATTR(temp3_offset, S_IRUGO | S_IWUSR, show_temp_offset,
1603 store_temp_offset, 2),
1604 SENSOR_ATTR(temp4_offset, S_IRUGO | S_IWUSR, show_temp_offset,
1605 store_temp_offset, 3),
1606 SENSOR_ATTR(temp5_offset, S_IRUGO | S_IWUSR, show_temp_offset,
1607 store_temp_offset, 4),
1608 SENSOR_ATTR(temp6_offset, S_IRUGO | S_IWUSR, show_temp_offset,
1609 store_temp_offset, 5),
1612 static struct sensor_device_attribute sda_temp_type[] = {
1613 SENSOR_ATTR(temp1_type, S_IRUGO | S_IWUSR, show_temp_type,
1614 store_temp_type, 0),
1615 SENSOR_ATTR(temp2_type, S_IRUGO | S_IWUSR, show_temp_type,
1616 store_temp_type, 1),
1617 SENSOR_ATTR(temp3_type, S_IRUGO | S_IWUSR, show_temp_type,
1618 store_temp_type, 2),
1619 SENSOR_ATTR(temp4_type, S_IRUGO | S_IWUSR, show_temp_type,
1620 store_temp_type, 3),
1621 SENSOR_ATTR(temp5_type, S_IRUGO | S_IWUSR, show_temp_type,
1622 store_temp_type, 4),
1623 SENSOR_ATTR(temp6_type, S_IRUGO | S_IWUSR, show_temp_type,
1624 store_temp_type, 5),
1627 static struct sensor_device_attribute sda_temp_alarm[] = {
1628 SENSOR_ATTR(temp1_alarm, S_IRUGO, show_alarm, NULL, 4),
1629 SENSOR_ATTR(temp2_alarm, S_IRUGO, show_alarm, NULL, 5),
1630 SENSOR_ATTR(temp3_alarm, S_IRUGO, show_alarm, NULL, 13),
1633 #define NUM_TEMP_ALARM ARRAY_SIZE(sda_temp_alarm)
1636 show_pwm_mode(struct device *dev, struct device_attribute *attr, char *buf)
1638 struct nct6775_data *data = nct6775_update_device(dev);
1639 struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr);
1641 return sprintf(buf, "%d\n", !data->pwm_mode[sattr->index]);
1645 store_pwm_mode(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 *sattr = to_sensor_dev_attr(attr);
1650 int nr = sattr->index;
1655 err = kstrtoul(buf, 10, &val);
1662 /* Setting DC mode is not supported for all chips/channels */
1663 if (data->REG_PWM_MODE[nr] == 0) {
1669 mutex_lock(&data->update_lock);
1670 data->pwm_mode[nr] = val;
1671 reg = nct6775_read_value(data, data->REG_PWM_MODE[nr]);
1672 reg &= ~data->PWM_MODE_MASK[nr];
1674 reg |= data->PWM_MODE_MASK[nr];
1675 nct6775_write_value(data, data->REG_PWM_MODE[nr], reg);
1676 mutex_unlock(&data->update_lock);
1681 show_pwm_enable(struct device *dev, struct device_attribute *attr, char *buf)
1683 struct nct6775_data *data = nct6775_update_device(dev);
1684 struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr);
1686 return sprintf(buf, "%d\n", data->pwm_enable[sattr->index]);
1690 show_pwm(struct device *dev, struct device_attribute *attr, char *buf)
1692 struct nct6775_data *data = nct6775_update_device(dev);
1693 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
1695 int index = sattr->index;
1699 * For automatic fan control modes, show current pwm readings.
1700 * Otherwise, show the configured value.
1702 if (index == 0 && data->pwm_enable[nr] > 1)
1703 pwm = nct6775_read_value(data, data->REG_PWM_READ[nr]);
1705 pwm = data->pwm[index][nr];
1707 return sprintf(buf, "%d\n", pwm);
1711 store_pwm(struct device *dev, struct device_attribute *attr, const char *buf,
1714 struct nct6775_data *data = dev_get_drvdata(dev);
1715 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
1717 int index = sattr->index;
1721 err = kstrtoul(buf, 10, &val);
1724 if (val > 255 || (index && val == 0))
1727 mutex_lock(&data->update_lock);
1728 data->pwm[index][nr] = val;
1729 nct6775_write_value(data, data->REG_PWM[index][nr], val);
1730 mutex_unlock(&data->update_lock);
1734 /* Returns 0 if OK, -EINVAL otherwise */
1735 static int check_trip_points(struct nct6775_data *data, int nr)
1739 for (i = 0; i < data->auto_pwm_num - 1; i++) {
1740 if (data->auto_temp[nr][i] > data->auto_temp[nr][i + 1])
1743 for (i = 0; i < data->auto_pwm_num - 1; i++) {
1744 if (data->auto_pwm[nr][i] > data->auto_pwm[nr][i + 1])
1747 /* validate critical temperature and pwm if enabled (pwm > 0) */
1748 if (data->auto_pwm[nr][data->auto_pwm_num]) {
1749 if (data->auto_temp[nr][data->auto_pwm_num - 1] >
1750 data->auto_temp[nr][data->auto_pwm_num] ||
1751 data->auto_pwm[nr][data->auto_pwm_num - 1] >
1752 data->auto_pwm[nr][data->auto_pwm_num])
1759 store_pwm_enable(struct device *dev, struct device_attribute *attr,
1760 const char *buf, size_t count)
1762 struct nct6775_data *data = dev_get_drvdata(dev);
1763 struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr);
1764 int nr = sattr->index;
1769 err = kstrtoul(buf, 10, &val);
1776 if (val == 4 && check_trip_points(data, nr)) {
1777 dev_err(dev, "Inconsistent trip points, not switching to SmartFan IV mode\n");
1778 dev_err(dev, "Adjust trip points and try again\n");
1782 mutex_lock(&data->update_lock);
1783 data->pwm_enable[nr] = val;
1786 * turn off pwm control: select manual mode, set pwm to maximum
1788 data->pwm[0][nr] = 255;
1789 nct6775_write_value(data, data->REG_PWM[0][nr], 255);
1791 reg = nct6775_read_value(data, data->REG_FAN_MODE[nr]);
1793 reg |= (pwm_enable_to_reg(val) << 4);
1794 nct6775_write_value(data, data->REG_FAN_MODE[nr], reg);
1795 mutex_unlock(&data->update_lock);
1800 show_pwm_temp(struct device *dev, struct device_attribute *attr, char *buf)
1802 struct nct6775_data *data = nct6775_update_device(dev);
1803 struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr);
1804 int nr = sattr->index;
1806 return sprintf(buf, "%d\n", LM75_TEMP_FROM_REG(data->pwm_temp[nr]));
1810 show_pwm_temp_sel(struct device *dev, struct device_attribute *attr, char *buf)
1812 struct nct6775_data *data = nct6775_update_device(dev);
1813 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
1815 return sprintf(buf, "%d\n",
1816 data->pwm_temp_sel[sattr->index][sattr->nr]);
1820 store_pwm_temp_sel(struct device *dev, struct device_attribute *attr,
1821 const char *buf, size_t count)
1823 struct nct6775_data *data = nct6775_update_device(dev);
1824 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
1826 int index = sattr->index;
1830 static const int max_src[] = {
1831 ARRAY_SIZE(nct6775_temp_label),
1832 ARRAY_SIZE(nct6776_temp_label),
1833 ARRAY_SIZE(nct6779_temp_label)
1836 err = kstrtoul(buf, 10, &val);
1839 if (val == 0 || val > 0x1f)
1842 val = SENSORS_LIMIT(val, 1, max_src[data->kind]);
1844 if (!strlen(data->temp_label[val]))
1847 mutex_lock(&data->update_lock);
1848 data->pwm_temp_sel[index][nr] = val;
1849 reg = nct6775_read_value(data, data->REG_TEMP_SEL[index][nr]);
1852 nct6775_write_value(data, data->REG_TEMP_SEL[index][nr], reg);
1853 mutex_unlock(&data->update_lock);
1859 show_target_temp(struct device *dev, struct device_attribute *attr, char *buf)
1861 struct nct6775_data *data = nct6775_update_device(dev);
1862 struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr);
1864 return sprintf(buf, "%d\n",
1865 data->target_temp[sattr->index] * 1000);
1869 store_target_temp(struct device *dev, struct device_attribute *attr,
1870 const char *buf, size_t count)
1872 struct nct6775_data *data = dev_get_drvdata(dev);
1873 struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr);
1874 int nr = sattr->index;
1878 err = kstrtol(buf, 10, &val);
1882 val = SENSORS_LIMIT(DIV_ROUND_CLOSEST(val, 1000), 0, 127);
1884 mutex_lock(&data->update_lock);
1885 data->target_temp[nr] = val;
1886 nct6775_write_value(data, data->REG_TARGET[nr], val);
1887 mutex_unlock(&data->update_lock);
1892 show_auto_temp_hyst(struct device *dev, struct device_attribute *attr,
1895 struct nct6775_data *data = nct6775_update_device(dev);
1896 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
1898 int point = sattr->index >= data->auto_pwm_num ? 1 : 0;
1899 int tolerance = data->tolerance[nr][point];
1900 int temp = data->auto_temp[nr][sattr->index];
1902 return sprintf(buf, "%d\n", (temp - tolerance) * 1000);
1906 store_auto_temp_hyst(struct device *dev, struct device_attribute *attr,
1907 const char *buf, size_t count)
1909 struct nct6775_data *data = dev_get_drvdata(dev);
1910 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
1912 int point = sattr->index >= data->auto_pwm_num ? 1 : 0;
1916 int maxlimit[2][3] = { { 15, 7, 63 }, { 15, 7, 7 } };
1917 int mask[] = { 0x0f, 0x07, 0x07 };
1920 err = kstrtol(buf, 10, &val);
1924 temp = data->auto_temp[nr][sattr->index];
1925 val = temp - DIV_ROUND_CLOSEST(val, 1000);
1927 /* Limit tolerance as needed */
1928 val = SENSORS_LIMIT(val, 0, maxlimit[point][data->kind]);
1930 mutex_lock(&data->update_lock);
1932 nct6775_write_value(data,
1933 data->REG_CRITICAL_TEMP_TOLERANCE[nr],
1936 reg = nct6775_read_value(data, data->REG_FAN_MODE[nr]);
1937 reg = (reg & ~mask[nr]) | (val & mask[nr]);
1938 nct6775_write_value(data, data->REG_FAN_MODE[nr], reg);
1939 if (data->kind == nct6779) {
1940 reg = nct6775_read_value(data,
1941 NCT6779_REG_TOLERANCE_H[nr]);
1942 reg = (reg & 0x70) | ((val & 0x38) << 1);
1943 nct6775_write_value(data,
1944 NCT6779_REG_TOLERANCE_H[nr], reg);
1948 data->tolerance[nr][point] = val;
1949 mutex_unlock(&data->update_lock);
1953 static SENSOR_DEVICE_ATTR_2(pwm1, S_IWUSR | S_IRUGO, show_pwm, store_pwm, 0, 0);
1954 static SENSOR_DEVICE_ATTR_2(pwm2, S_IWUSR | S_IRUGO, show_pwm, store_pwm, 1, 0);
1955 static SENSOR_DEVICE_ATTR_2(pwm3, S_IWUSR | S_IRUGO, show_pwm, store_pwm, 2, 0);
1956 static SENSOR_DEVICE_ATTR_2(pwm4, S_IWUSR | S_IRUGO, show_pwm, store_pwm, 3, 0);
1957 static SENSOR_DEVICE_ATTR_2(pwm5, S_IWUSR | S_IRUGO, show_pwm, store_pwm, 4, 0);
1959 static SENSOR_DEVICE_ATTR(pwm1_mode, S_IWUSR | S_IRUGO, show_pwm_mode,
1961 static SENSOR_DEVICE_ATTR(pwm2_mode, S_IWUSR | S_IRUGO, show_pwm_mode,
1963 static SENSOR_DEVICE_ATTR(pwm3_mode, S_IWUSR | S_IRUGO, show_pwm_mode,
1965 static SENSOR_DEVICE_ATTR(pwm4_mode, S_IWUSR | S_IRUGO, show_pwm_mode,
1967 static SENSOR_DEVICE_ATTR(pwm5_mode, S_IWUSR | S_IRUGO, show_pwm_mode,
1970 static SENSOR_DEVICE_ATTR(pwm1_enable, S_IWUSR | S_IRUGO, show_pwm_enable,
1971 store_pwm_enable, 0);
1972 static SENSOR_DEVICE_ATTR(pwm2_enable, S_IWUSR | S_IRUGO, show_pwm_enable,
1973 store_pwm_enable, 1);
1974 static SENSOR_DEVICE_ATTR(pwm3_enable, S_IWUSR | S_IRUGO, show_pwm_enable,
1975 store_pwm_enable, 2);
1976 static SENSOR_DEVICE_ATTR(pwm4_enable, S_IWUSR | S_IRUGO, show_pwm_enable,
1977 store_pwm_enable, 3);
1978 static SENSOR_DEVICE_ATTR(pwm5_enable, S_IWUSR | S_IRUGO, show_pwm_enable,
1979 store_pwm_enable, 4);
1981 static SENSOR_DEVICE_ATTR_2(pwm1_temp_sel, S_IWUSR | S_IRUGO,
1982 show_pwm_temp_sel, store_pwm_temp_sel, 0, 0);
1983 static SENSOR_DEVICE_ATTR_2(pwm2_temp_sel, S_IWUSR | S_IRUGO,
1984 show_pwm_temp_sel, store_pwm_temp_sel, 1, 0);
1985 static SENSOR_DEVICE_ATTR_2(pwm3_temp_sel, S_IWUSR | S_IRUGO,
1986 show_pwm_temp_sel, store_pwm_temp_sel, 2, 0);
1987 static SENSOR_DEVICE_ATTR_2(pwm4_temp_sel, S_IWUSR | S_IRUGO,
1988 show_pwm_temp_sel, store_pwm_temp_sel, 3, 0);
1989 static SENSOR_DEVICE_ATTR_2(pwm5_temp_sel, S_IWUSR | S_IRUGO,
1990 show_pwm_temp_sel, store_pwm_temp_sel, 4, 0);
1992 static SENSOR_DEVICE_ATTR(pwm1_target, S_IWUSR | S_IRUGO, show_target_temp,
1993 store_target_temp, 0);
1994 static SENSOR_DEVICE_ATTR(pwm2_target, S_IWUSR | S_IRUGO, show_target_temp,
1995 store_target_temp, 1);
1996 static SENSOR_DEVICE_ATTR(pwm3_target, S_IWUSR | S_IRUGO, show_target_temp,
1997 store_target_temp, 2);
1998 static SENSOR_DEVICE_ATTR(pwm4_target, S_IWUSR | S_IRUGO, show_target_temp,
1999 store_target_temp, 3);
2000 static SENSOR_DEVICE_ATTR(pwm5_target, S_IWUSR | S_IRUGO, show_target_temp,
2001 store_target_temp, 4);
2003 /* Monitored pwm temperatures */
2004 static SENSOR_DEVICE_ATTR(temp11_input, S_IRUGO, show_pwm_temp, NULL, 0);
2005 static SENSOR_DEVICE_ATTR(temp12_input, S_IRUGO, show_pwm_temp, NULL, 1);
2006 static SENSOR_DEVICE_ATTR(temp13_input, S_IRUGO, show_pwm_temp, NULL, 2);
2007 static SENSOR_DEVICE_ATTR(temp14_input, S_IRUGO, show_pwm_temp, NULL, 3);
2008 static SENSOR_DEVICE_ATTR(temp15_input, S_IRUGO, show_pwm_temp, NULL, 4);
2010 /* Smart Fan registers */
2013 show_pwm_sel_enable(struct device *dev, struct device_attribute *attr,
2016 struct nct6775_data *data = nct6775_update_device(dev);
2017 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
2019 return sprintf(buf, "%d\n",
2020 data->pwm_sel_enable[sattr->index][sattr->nr]);
2024 store_pwm_sel_enable(struct device *dev, struct device_attribute *attr,
2025 const char *buf, size_t count)
2027 struct nct6775_data *data = dev_get_drvdata(dev);
2028 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
2030 int index = sattr->index;
2035 err = kstrtoul(buf, 10, &val);
2042 mutex_lock(&data->update_lock);
2043 data->pwm_sel_enable[index][nr] = val;
2044 reg = nct6775_read_value(data, data->REG_TEMP_SEL[index][nr]);
2048 nct6775_write_value(data, data->REG_TEMP_SEL[index][nr], reg);
2049 mutex_unlock(&data->update_lock);
2054 show_weight_temp(struct device *dev, struct device_attribute *attr, char *buf)
2056 struct nct6775_data *data = nct6775_update_device(dev);
2057 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
2059 int index = sattr->index;
2061 return sprintf(buf, "%d\n", data->weight_temp[index][nr] * 1000);
2065 store_weight_temp(struct device *dev, struct device_attribute *attr,
2066 const char *buf, size_t count)
2068 struct nct6775_data *data = dev_get_drvdata(dev);
2069 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
2071 int index = sattr->index;
2075 err = kstrtoul(buf, 10, &val);
2079 val = SENSORS_LIMIT(DIV_ROUND_CLOSEST(val, 1000), 0, 255);
2081 mutex_lock(&data->update_lock);
2082 data->weight_temp[index][nr] = val;
2083 nct6775_write_value(data, data->REG_WEIGHT_TEMP[index][nr], val);
2084 mutex_unlock(&data->update_lock);
2089 show_weight_duty(struct device *dev, struct device_attribute *attr, char *buf)
2091 struct nct6775_data *data = nct6775_update_device(dev);
2092 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
2094 int index = sattr->index;
2096 return sprintf(buf, "%d\n", data->weight_duty[index][nr]);
2100 store_weight_duty(struct device *dev, struct device_attribute *attr,
2101 const char *buf, size_t count)
2103 struct nct6775_data *data = dev_get_drvdata(dev);
2104 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
2106 int index = sattr->index;
2110 err = kstrtoul(buf, 10, &val);
2114 val = SENSORS_LIMIT(val, 0, 255);
2116 mutex_lock(&data->update_lock);
2117 data->weight_duty[index][nr] = val;
2118 nct6775_write_value(data, data->REG_WEIGHT_DUTY[index][nr], val);
2119 mutex_unlock(&data->update_lock);
2123 static SENSOR_DEVICE_ATTR_2(pwm1_stop_output_enable, S_IWUSR | S_IRUGO,
2124 show_pwm_sel_enable, store_pwm_sel_enable, 0, 0);
2125 static SENSOR_DEVICE_ATTR_2(pwm2_stop_output_enable, S_IWUSR | S_IRUGO,
2126 show_pwm_sel_enable, store_pwm_sel_enable, 1, 0);
2127 static SENSOR_DEVICE_ATTR_2(pwm3_stop_output_enable, S_IWUSR | S_IRUGO,
2128 show_pwm_sel_enable, store_pwm_sel_enable, 2, 0);
2129 static SENSOR_DEVICE_ATTR_2(pwm4_stop_output_enable, S_IWUSR | S_IRUGO,
2130 show_pwm_sel_enable, store_pwm_sel_enable, 3, 0);
2131 static SENSOR_DEVICE_ATTR_2(pwm5_stop_output_enable, S_IWUSR | S_IRUGO,
2132 show_pwm_sel_enable, store_pwm_sel_enable, 4, 0);
2134 static SENSOR_DEVICE_ATTR_2(pwm1_weight_enable, S_IWUSR | S_IRUGO,
2135 show_pwm_sel_enable, store_pwm_sel_enable, 0, 1);
2136 static SENSOR_DEVICE_ATTR_2(pwm2_weight_enable, S_IWUSR | S_IRUGO,
2137 show_pwm_sel_enable, store_pwm_sel_enable, 1, 1);
2138 static SENSOR_DEVICE_ATTR_2(pwm3_weight_enable, S_IWUSR | S_IRUGO,
2139 show_pwm_sel_enable, store_pwm_sel_enable, 2, 1);
2140 static SENSOR_DEVICE_ATTR_2(pwm4_weight_enable, S_IWUSR | S_IRUGO,
2141 show_pwm_sel_enable, store_pwm_sel_enable, 3, 1);
2142 static SENSOR_DEVICE_ATTR_2(pwm5_weight_enable, S_IWUSR | S_IRUGO,
2143 show_pwm_sel_enable, store_pwm_sel_enable, 4, 1);
2145 static SENSOR_DEVICE_ATTR_2(pwm1_weight_temp_sel, S_IWUSR | S_IRUGO,
2146 show_pwm_temp_sel, store_pwm_temp_sel, 0, 1);
2147 static SENSOR_DEVICE_ATTR_2(pwm2_weight_temp_sel, S_IWUSR | S_IRUGO,
2148 show_pwm_temp_sel, store_pwm_temp_sel, 1, 1);
2149 static SENSOR_DEVICE_ATTR_2(pwm3_weight_temp_sel, S_IWUSR | S_IRUGO,
2150 show_pwm_temp_sel, store_pwm_temp_sel, 2, 1);
2151 static SENSOR_DEVICE_ATTR_2(pwm4_weight_temp_sel, S_IWUSR | S_IRUGO,
2152 show_pwm_temp_sel, store_pwm_temp_sel, 3, 1);
2153 static SENSOR_DEVICE_ATTR_2(pwm5_weight_temp_sel, S_IWUSR | S_IRUGO,
2154 show_pwm_temp_sel, store_pwm_temp_sel, 4, 1);
2156 static SENSOR_DEVICE_ATTR_2(pwm1_weight_temp_step, S_IWUSR | S_IRUGO,
2157 show_weight_temp, store_weight_temp, 0, 0);
2158 static SENSOR_DEVICE_ATTR_2(pwm2_weight_temp_step, S_IWUSR | S_IRUGO,
2159 show_weight_temp, store_weight_temp, 1, 0);
2160 static SENSOR_DEVICE_ATTR_2(pwm3_weight_temp_step, S_IWUSR | S_IRUGO,
2161 show_weight_temp, store_weight_temp, 2, 0);
2162 static SENSOR_DEVICE_ATTR_2(pwm4_weight_temp_step, S_IWUSR | S_IRUGO,
2163 show_weight_temp, store_weight_temp, 3, 0);
2164 static SENSOR_DEVICE_ATTR_2(pwm5_weight_temp_step, S_IWUSR | S_IRUGO,
2165 show_weight_temp, store_weight_temp, 4, 0);
2167 static SENSOR_DEVICE_ATTR_2(pwm1_weight_temp_step_tol, S_IWUSR | S_IRUGO,
2168 show_weight_temp, store_weight_temp, 0, 1);
2169 static SENSOR_DEVICE_ATTR_2(pwm2_weight_temp_step_tol, S_IWUSR | S_IRUGO,
2170 show_weight_temp, store_weight_temp, 1, 1);
2171 static SENSOR_DEVICE_ATTR_2(pwm3_weight_temp_step_tol, S_IWUSR | S_IRUGO,
2172 show_weight_temp, store_weight_temp, 2, 1);
2173 static SENSOR_DEVICE_ATTR_2(pwm4_weight_temp_step_tol, S_IWUSR | S_IRUGO,
2174 show_weight_temp, store_weight_temp, 3, 1);
2175 static SENSOR_DEVICE_ATTR_2(pwm5_weight_temp_step_tol, S_IWUSR | S_IRUGO,
2176 show_weight_temp, store_weight_temp, 4, 1);
2178 static SENSOR_DEVICE_ATTR_2(pwm1_weight_temp_step_base, S_IWUSR | S_IRUGO,
2179 show_weight_temp, store_weight_temp, 0, 2);
2180 static SENSOR_DEVICE_ATTR_2(pwm2_weight_temp_step_base, S_IWUSR | S_IRUGO,
2181 show_weight_temp, store_weight_temp, 1, 2);
2182 static SENSOR_DEVICE_ATTR_2(pwm3_weight_temp_step_base, S_IWUSR | S_IRUGO,
2183 show_weight_temp, store_weight_temp, 2, 2);
2184 static SENSOR_DEVICE_ATTR_2(pwm4_weight_temp_step_base, S_IWUSR | S_IRUGO,
2185 show_weight_temp, store_weight_temp, 3, 2);
2186 static SENSOR_DEVICE_ATTR_2(pwm5_weight_temp_step_base, S_IWUSR | S_IRUGO,
2187 show_weight_temp, store_weight_temp, 4, 2);
2189 static SENSOR_DEVICE_ATTR_2(pwm1_weight_duty_step, S_IWUSR | S_IRUGO,
2190 show_weight_duty, store_weight_duty, 0, 0);
2191 static SENSOR_DEVICE_ATTR_2(pwm2_weight_duty_step, S_IWUSR | S_IRUGO,
2192 show_weight_duty, store_weight_duty, 1, 0);
2193 static SENSOR_DEVICE_ATTR_2(pwm3_weight_duty_step, S_IWUSR | S_IRUGO,
2194 show_weight_duty, store_weight_duty, 2, 0);
2195 static SENSOR_DEVICE_ATTR_2(pwm4_weight_duty_step, S_IWUSR | S_IRUGO,
2196 show_weight_duty, store_weight_duty, 3, 0);
2197 static SENSOR_DEVICE_ATTR_2(pwm5_weight_duty_step, S_IWUSR | S_IRUGO,
2198 show_weight_duty, store_weight_duty, 4, 0);
2200 /* duty_base is not supported on all chips */
2201 static struct sensor_device_attribute_2 sda_weight_duty_base[] = {
2202 SENSOR_ATTR_2(pwm1_weight_duty_base, S_IWUSR | S_IRUGO,
2203 show_weight_duty, store_weight_duty, 0, 1),
2204 SENSOR_ATTR_2(pwm2_weight_duty_base, S_IWUSR | S_IRUGO,
2205 show_weight_duty, store_weight_duty, 1, 1),
2206 SENSOR_ATTR_2(pwm3_weight_duty_base, S_IWUSR | S_IRUGO,
2207 show_weight_duty, store_weight_duty, 2, 1),
2208 SENSOR_ATTR_2(pwm4_weight_duty_base, S_IWUSR | S_IRUGO,
2209 show_weight_duty, store_weight_duty, 3, 1),
2210 SENSOR_ATTR_2(pwm5_weight_duty_base, S_IWUSR | S_IRUGO,
2211 show_weight_duty, store_weight_duty, 4, 1),
2215 show_fan_time(struct device *dev, struct device_attribute *attr, char *buf)
2217 struct nct6775_data *data = nct6775_update_device(dev);
2218 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
2220 int index = sattr->index;
2222 return sprintf(buf, "%d\n",
2223 step_time_from_reg(data->fan_time[index][nr],
2224 data->pwm_mode[nr]));
2228 store_fan_time(struct device *dev, struct device_attribute *attr,
2229 const char *buf, size_t count)
2231 struct nct6775_data *data = dev_get_drvdata(dev);
2232 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
2234 int index = sattr->index;
2238 err = kstrtoul(buf, 10, &val);
2242 val = step_time_to_reg(val, data->pwm_mode[nr]);
2243 mutex_lock(&data->update_lock);
2244 data->fan_time[index][nr] = val;
2245 nct6775_write_value(data, data->REG_FAN_TIME[index][nr], val);
2246 mutex_unlock(&data->update_lock);
2251 show_name(struct device *dev, struct device_attribute *attr, char *buf)
2253 struct nct6775_data *data = dev_get_drvdata(dev);
2255 return sprintf(buf, "%s\n", data->name);
2258 static DEVICE_ATTR(name, S_IRUGO, show_name, NULL);
2260 static SENSOR_DEVICE_ATTR_2(pwm1_stop_time, S_IWUSR | S_IRUGO, show_fan_time,
2261 store_fan_time, 0, 0);
2262 static SENSOR_DEVICE_ATTR_2(pwm2_stop_time, S_IWUSR | S_IRUGO, show_fan_time,
2263 store_fan_time, 1, 0);
2264 static SENSOR_DEVICE_ATTR_2(pwm3_stop_time, S_IWUSR | S_IRUGO, show_fan_time,
2265 store_fan_time, 2, 0);
2266 static SENSOR_DEVICE_ATTR_2(pwm4_stop_time, S_IWUSR | S_IRUGO, show_fan_time,
2267 store_fan_time, 3, 0);
2268 static SENSOR_DEVICE_ATTR_2(pwm5_stop_time, S_IWUSR | S_IRUGO, show_fan_time,
2269 store_fan_time, 4, 0);
2271 static SENSOR_DEVICE_ATTR_2(pwm1_step_up_time, S_IWUSR | S_IRUGO, show_fan_time,
2272 store_fan_time, 0, 1);
2273 static SENSOR_DEVICE_ATTR_2(pwm2_step_up_time, S_IWUSR | S_IRUGO, show_fan_time,
2274 store_fan_time, 1, 1);
2275 static SENSOR_DEVICE_ATTR_2(pwm3_step_up_time, S_IWUSR | S_IRUGO, show_fan_time,
2276 store_fan_time, 2, 1);
2277 static SENSOR_DEVICE_ATTR_2(pwm4_step_up_time, S_IWUSR | S_IRUGO, show_fan_time,
2278 store_fan_time, 3, 1);
2279 static SENSOR_DEVICE_ATTR_2(pwm5_step_up_time, S_IWUSR | S_IRUGO, show_fan_time,
2280 store_fan_time, 4, 1);
2282 static SENSOR_DEVICE_ATTR_2(pwm1_step_down_time, S_IWUSR | S_IRUGO,
2283 show_fan_time, store_fan_time, 0, 2);
2284 static SENSOR_DEVICE_ATTR_2(pwm2_step_down_time, S_IWUSR | S_IRUGO,
2285 show_fan_time, store_fan_time, 1, 2);
2286 static SENSOR_DEVICE_ATTR_2(pwm3_step_down_time, S_IWUSR | S_IRUGO,
2287 show_fan_time, store_fan_time, 2, 2);
2288 static SENSOR_DEVICE_ATTR_2(pwm4_step_down_time, S_IWUSR | S_IRUGO,
2289 show_fan_time, store_fan_time, 3, 2);
2290 static SENSOR_DEVICE_ATTR_2(pwm5_step_down_time, S_IWUSR | S_IRUGO,
2291 show_fan_time, store_fan_time, 4, 2);
2293 static SENSOR_DEVICE_ATTR_2(pwm1_start_output, S_IWUSR | S_IRUGO, show_pwm,
2295 static SENSOR_DEVICE_ATTR_2(pwm2_start_output, S_IWUSR | S_IRUGO, show_pwm,
2297 static SENSOR_DEVICE_ATTR_2(pwm3_start_output, S_IWUSR | S_IRUGO, show_pwm,
2299 static SENSOR_DEVICE_ATTR_2(pwm4_start_output, S_IWUSR | S_IRUGO, show_pwm,
2301 static SENSOR_DEVICE_ATTR_2(pwm5_start_output, S_IWUSR | S_IRUGO, show_pwm,
2304 static SENSOR_DEVICE_ATTR_2(pwm1_stop_output, S_IWUSR | S_IRUGO, show_pwm,
2306 static SENSOR_DEVICE_ATTR_2(pwm2_stop_output, S_IWUSR | S_IRUGO, show_pwm,
2308 static SENSOR_DEVICE_ATTR_2(pwm3_stop_output, S_IWUSR | S_IRUGO, show_pwm,
2310 static SENSOR_DEVICE_ATTR_2(pwm4_stop_output, S_IWUSR | S_IRUGO, show_pwm,
2312 static SENSOR_DEVICE_ATTR_2(pwm5_stop_output, S_IWUSR | S_IRUGO, show_pwm,
2315 static struct attribute *nct6775_attributes_pwm[5][20] = {
2317 &sensor_dev_attr_temp11_input.dev_attr.attr,
2318 &sensor_dev_attr_pwm1.dev_attr.attr,
2319 &sensor_dev_attr_pwm1_mode.dev_attr.attr,
2320 &sensor_dev_attr_pwm1_enable.dev_attr.attr,
2321 &sensor_dev_attr_pwm1_stop_output_enable.dev_attr.attr,
2322 &sensor_dev_attr_pwm1_weight_enable.dev_attr.attr,
2323 &sensor_dev_attr_pwm1_temp_sel.dev_attr.attr,
2324 &sensor_dev_attr_pwm1_target.dev_attr.attr,
2325 &sensor_dev_attr_pwm1_stop_time.dev_attr.attr,
2326 &sensor_dev_attr_pwm1_step_up_time.dev_attr.attr,
2327 &sensor_dev_attr_pwm1_step_down_time.dev_attr.attr,
2328 &sensor_dev_attr_pwm1_start_output.dev_attr.attr,
2329 &sensor_dev_attr_pwm1_stop_output.dev_attr.attr,
2330 &sensor_dev_attr_pwm1_weight_temp_sel.dev_attr.attr,
2331 &sensor_dev_attr_pwm1_weight_temp_step.dev_attr.attr,
2332 &sensor_dev_attr_pwm1_weight_temp_step_tol.dev_attr.attr,
2333 &sensor_dev_attr_pwm1_weight_temp_step_base.dev_attr.attr,
2334 &sensor_dev_attr_pwm1_weight_duty_step.dev_attr.attr,
2338 &sensor_dev_attr_temp12_input.dev_attr.attr,
2339 &sensor_dev_attr_pwm2.dev_attr.attr,
2340 &sensor_dev_attr_pwm2_mode.dev_attr.attr,
2341 &sensor_dev_attr_pwm2_enable.dev_attr.attr,
2342 &sensor_dev_attr_pwm2_stop_output_enable.dev_attr.attr,
2343 &sensor_dev_attr_pwm2_weight_enable.dev_attr.attr,
2344 &sensor_dev_attr_pwm2_temp_sel.dev_attr.attr,
2345 &sensor_dev_attr_pwm2_target.dev_attr.attr,
2346 &sensor_dev_attr_pwm2_stop_time.dev_attr.attr,
2347 &sensor_dev_attr_pwm2_step_up_time.dev_attr.attr,
2348 &sensor_dev_attr_pwm2_step_down_time.dev_attr.attr,
2349 &sensor_dev_attr_pwm2_start_output.dev_attr.attr,
2350 &sensor_dev_attr_pwm2_stop_output.dev_attr.attr,
2351 &sensor_dev_attr_pwm2_weight_temp_sel.dev_attr.attr,
2352 &sensor_dev_attr_pwm2_weight_temp_step.dev_attr.attr,
2353 &sensor_dev_attr_pwm2_weight_temp_step_tol.dev_attr.attr,
2354 &sensor_dev_attr_pwm2_weight_temp_step_base.dev_attr.attr,
2355 &sensor_dev_attr_pwm2_weight_duty_step.dev_attr.attr,
2359 &sensor_dev_attr_temp13_input.dev_attr.attr,
2360 &sensor_dev_attr_pwm3.dev_attr.attr,
2361 &sensor_dev_attr_pwm3_mode.dev_attr.attr,
2362 &sensor_dev_attr_pwm3_enable.dev_attr.attr,
2363 &sensor_dev_attr_pwm3_stop_output_enable.dev_attr.attr,
2364 &sensor_dev_attr_pwm3_weight_enable.dev_attr.attr,
2365 &sensor_dev_attr_pwm3_temp_sel.dev_attr.attr,
2366 &sensor_dev_attr_pwm3_target.dev_attr.attr,
2367 &sensor_dev_attr_pwm3_stop_time.dev_attr.attr,
2368 &sensor_dev_attr_pwm3_step_up_time.dev_attr.attr,
2369 &sensor_dev_attr_pwm3_step_down_time.dev_attr.attr,
2370 &sensor_dev_attr_pwm3_start_output.dev_attr.attr,
2371 &sensor_dev_attr_pwm3_stop_output.dev_attr.attr,
2372 &sensor_dev_attr_pwm3_weight_temp_sel.dev_attr.attr,
2373 &sensor_dev_attr_pwm3_weight_temp_step.dev_attr.attr,
2374 &sensor_dev_attr_pwm3_weight_temp_step_tol.dev_attr.attr,
2375 &sensor_dev_attr_pwm3_weight_temp_step_base.dev_attr.attr,
2376 &sensor_dev_attr_pwm3_weight_duty_step.dev_attr.attr,
2380 &sensor_dev_attr_temp14_input.dev_attr.attr,
2381 &sensor_dev_attr_pwm4.dev_attr.attr,
2382 &sensor_dev_attr_pwm4_mode.dev_attr.attr,
2383 &sensor_dev_attr_pwm4_enable.dev_attr.attr,
2384 &sensor_dev_attr_pwm4_stop_output_enable.dev_attr.attr,
2385 &sensor_dev_attr_pwm4_weight_enable.dev_attr.attr,
2386 &sensor_dev_attr_pwm4_temp_sel.dev_attr.attr,
2387 &sensor_dev_attr_pwm4_target.dev_attr.attr,
2388 &sensor_dev_attr_pwm4_stop_time.dev_attr.attr,
2389 &sensor_dev_attr_pwm4_step_up_time.dev_attr.attr,
2390 &sensor_dev_attr_pwm4_step_down_time.dev_attr.attr,
2391 &sensor_dev_attr_pwm4_start_output.dev_attr.attr,
2392 &sensor_dev_attr_pwm4_stop_output.dev_attr.attr,
2393 &sensor_dev_attr_pwm4_weight_temp_sel.dev_attr.attr,
2394 &sensor_dev_attr_pwm4_weight_temp_step.dev_attr.attr,
2395 &sensor_dev_attr_pwm4_weight_temp_step_tol.dev_attr.attr,
2396 &sensor_dev_attr_pwm4_weight_temp_step_base.dev_attr.attr,
2397 &sensor_dev_attr_pwm4_weight_duty_step.dev_attr.attr,
2401 &sensor_dev_attr_temp15_input.dev_attr.attr,
2402 &sensor_dev_attr_pwm5.dev_attr.attr,
2403 &sensor_dev_attr_pwm5_mode.dev_attr.attr,
2404 &sensor_dev_attr_pwm5_enable.dev_attr.attr,
2405 &sensor_dev_attr_pwm5_stop_output_enable.dev_attr.attr,
2406 &sensor_dev_attr_pwm5_weight_enable.dev_attr.attr,
2407 &sensor_dev_attr_pwm5_temp_sel.dev_attr.attr,
2408 &sensor_dev_attr_pwm5_target.dev_attr.attr,
2409 &sensor_dev_attr_pwm5_stop_time.dev_attr.attr,
2410 &sensor_dev_attr_pwm5_step_up_time.dev_attr.attr,
2411 &sensor_dev_attr_pwm5_step_down_time.dev_attr.attr,
2412 &sensor_dev_attr_pwm5_start_output.dev_attr.attr,
2413 &sensor_dev_attr_pwm5_stop_output.dev_attr.attr,
2414 &sensor_dev_attr_pwm5_weight_temp_sel.dev_attr.attr,
2415 &sensor_dev_attr_pwm5_weight_temp_step.dev_attr.attr,
2416 &sensor_dev_attr_pwm5_weight_temp_step_tol.dev_attr.attr,
2417 &sensor_dev_attr_pwm5_weight_temp_step_base.dev_attr.attr,
2418 &sensor_dev_attr_pwm5_weight_duty_step.dev_attr.attr,
2423 static const struct attribute_group nct6775_group_pwm[5] = {
2424 { .attrs = nct6775_attributes_pwm[0] },
2425 { .attrs = nct6775_attributes_pwm[1] },
2426 { .attrs = nct6775_attributes_pwm[2] },
2427 { .attrs = nct6775_attributes_pwm[3] },
2428 { .attrs = nct6775_attributes_pwm[4] },
2432 show_auto_pwm(struct device *dev, struct device_attribute *attr, char *buf)
2434 struct nct6775_data *data = nct6775_update_device(dev);
2435 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
2437 return sprintf(buf, "%d\n", data->auto_pwm[sattr->nr][sattr->index]);
2441 store_auto_pwm(struct device *dev, struct device_attribute *attr,
2442 const char *buf, size_t count)
2444 struct nct6775_data *data = dev_get_drvdata(dev);
2445 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
2447 int point = sattr->index;
2452 err = kstrtoul(buf, 10, &val);
2458 if (point == data->auto_pwm_num) {
2459 if (data->kind != nct6775 && !val)
2461 if (data->kind != nct6779 && val)
2465 mutex_lock(&data->update_lock);
2466 data->auto_pwm[nr][point] = val;
2467 if (point < data->auto_pwm_num) {
2468 nct6775_write_value(data,
2469 NCT6775_AUTO_PWM(data, nr, point),
2470 data->auto_pwm[nr][point]);
2472 switch (data->kind) {
2474 /* disable if needed (pwm == 0) */
2475 reg = nct6775_read_value(data,
2476 NCT6775_REG_CRITICAL_ENAB[nr]);
2481 nct6775_write_value(data, NCT6775_REG_CRITICAL_ENAB[nr],
2485 break; /* always enabled, nothing to do */
2487 nct6775_write_value(data, NCT6779_REG_CRITICAL_PWM[nr],
2489 reg = nct6775_read_value(data,
2490 NCT6779_REG_CRITICAL_PWM_ENABLE[nr]);
2495 nct6775_write_value(data,
2496 NCT6779_REG_CRITICAL_PWM_ENABLE[nr],
2501 mutex_unlock(&data->update_lock);
2506 show_auto_temp(struct device *dev, struct device_attribute *attr, char *buf)
2508 struct nct6775_data *data = nct6775_update_device(dev);
2509 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
2511 int point = sattr->index;
2514 * We don't know for sure if the temperature is signed or unsigned.
2515 * Assume it is unsigned.
2517 return sprintf(buf, "%d\n", data->auto_temp[nr][point] * 1000);
2521 store_auto_temp(struct device *dev, struct device_attribute *attr,
2522 const char *buf, size_t count)
2524 struct nct6775_data *data = dev_get_drvdata(dev);
2525 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
2527 int point = sattr->index;
2531 err = kstrtoul(buf, 10, &val);
2537 mutex_lock(&data->update_lock);
2538 data->auto_temp[nr][point] = DIV_ROUND_CLOSEST(val, 1000);
2539 if (point < data->auto_pwm_num) {
2540 nct6775_write_value(data,
2541 NCT6775_AUTO_TEMP(data, nr, point),
2542 data->auto_temp[nr][point]);
2544 nct6775_write_value(data, data->REG_CRITICAL_TEMP[nr],
2545 data->auto_temp[nr][point]);
2547 mutex_unlock(&data->update_lock);
2552 * The number of auto-point trip points is chip dependent.
2553 * Need to check support while generating/removing attribute files.
2555 static struct sensor_device_attribute_2 sda_auto_pwm_arrays[] = {
2556 SENSOR_ATTR_2(pwm1_auto_point1_pwm, S_IWUSR | S_IRUGO,
2557 show_auto_pwm, store_auto_pwm, 0, 0),
2558 SENSOR_ATTR_2(pwm1_auto_point1_temp, S_IWUSR | S_IRUGO,
2559 show_auto_temp, store_auto_temp, 0, 0),
2560 SENSOR_ATTR_2(pwm1_auto_point1_temp_hyst, S_IWUSR | S_IRUGO,
2561 show_auto_temp_hyst, store_auto_temp_hyst, 0, 0),
2562 SENSOR_ATTR_2(pwm1_auto_point2_pwm, S_IWUSR | S_IRUGO,
2563 show_auto_pwm, store_auto_pwm, 0, 1),
2564 SENSOR_ATTR_2(pwm1_auto_point2_temp, S_IWUSR | S_IRUGO,
2565 show_auto_temp, store_auto_temp, 0, 1),
2566 SENSOR_ATTR_2(pwm1_auto_point2_temp_hyst, S_IWUSR | S_IRUGO,
2567 show_auto_temp_hyst, store_auto_temp_hyst, 0, 1),
2568 SENSOR_ATTR_2(pwm1_auto_point3_pwm, S_IWUSR | S_IRUGO,
2569 show_auto_pwm, store_auto_pwm, 0, 2),
2570 SENSOR_ATTR_2(pwm1_auto_point3_temp, S_IWUSR | S_IRUGO,
2571 show_auto_temp, store_auto_temp, 0, 2),
2572 SENSOR_ATTR_2(pwm1_auto_point3_temp_hyst, S_IWUSR | S_IRUGO,
2573 show_auto_temp_hyst, store_auto_temp_hyst, 0, 2),
2574 SENSOR_ATTR_2(pwm1_auto_point4_pwm, S_IWUSR | S_IRUGO,
2575 show_auto_pwm, store_auto_pwm, 0, 3),
2576 SENSOR_ATTR_2(pwm1_auto_point4_temp, S_IWUSR | S_IRUGO,
2577 show_auto_temp, store_auto_temp, 0, 3),
2578 SENSOR_ATTR_2(pwm1_auto_point4_temp_hyst, S_IWUSR | S_IRUGO,
2579 show_auto_temp_hyst, store_auto_temp_hyst, 0, 3),
2580 SENSOR_ATTR_2(pwm1_auto_point5_pwm, S_IWUSR | S_IRUGO,
2581 show_auto_pwm, store_auto_pwm, 0, 4),
2582 SENSOR_ATTR_2(pwm1_auto_point5_temp, S_IWUSR | S_IRUGO,
2583 show_auto_temp, store_auto_temp, 0, 4),
2584 SENSOR_ATTR_2(pwm1_auto_point5_temp_hyst, S_IWUSR | S_IRUGO,
2585 show_auto_temp_hyst, store_auto_temp_hyst, 0, 4),
2586 SENSOR_ATTR_2(pwm1_auto_point6_pwm, S_IWUSR | S_IRUGO,
2587 show_auto_pwm, store_auto_pwm, 0, 5),
2588 SENSOR_ATTR_2(pwm1_auto_point6_temp, S_IWUSR | S_IRUGO,
2589 show_auto_temp, store_auto_temp, 0, 5),
2590 SENSOR_ATTR_2(pwm1_auto_point6_temp_hyst, S_IWUSR | S_IRUGO,
2591 show_auto_temp_hyst, store_auto_temp_hyst, 0, 5),
2592 SENSOR_ATTR_2(pwm1_auto_point7_pwm, S_IWUSR | S_IRUGO,
2593 show_auto_pwm, store_auto_pwm, 0, 6),
2594 SENSOR_ATTR_2(pwm1_auto_point7_temp, S_IWUSR | S_IRUGO,
2595 show_auto_temp, store_auto_temp, 0, 6),
2596 SENSOR_ATTR_2(pwm1_auto_point7_temp_hyst, S_IWUSR | S_IRUGO,
2597 show_auto_temp_hyst, store_auto_temp_hyst, 0, 6),
2599 SENSOR_ATTR_2(pwm2_auto_point1_pwm, S_IWUSR | S_IRUGO,
2600 show_auto_pwm, store_auto_pwm, 1, 0),
2601 SENSOR_ATTR_2(pwm2_auto_point1_temp, S_IWUSR | S_IRUGO,
2602 show_auto_temp, store_auto_temp, 1, 0),
2603 SENSOR_ATTR_2(pwm2_auto_point1_temp_hyst, S_IWUSR | S_IRUGO,
2604 show_auto_temp_hyst, store_auto_temp_hyst, 1, 0),
2605 SENSOR_ATTR_2(pwm2_auto_point2_pwm, S_IWUSR | S_IRUGO,
2606 show_auto_pwm, store_auto_pwm, 1, 1),
2607 SENSOR_ATTR_2(pwm2_auto_point2_temp, S_IWUSR | S_IRUGO,
2608 show_auto_temp, store_auto_temp, 1, 1),
2609 SENSOR_ATTR_2(pwm2_auto_point2_temp_hyst, S_IWUSR | S_IRUGO,
2610 show_auto_temp_hyst, store_auto_temp_hyst, 1, 1),
2611 SENSOR_ATTR_2(pwm2_auto_point3_pwm, S_IWUSR | S_IRUGO,
2612 show_auto_pwm, store_auto_pwm, 1, 2),
2613 SENSOR_ATTR_2(pwm2_auto_point3_temp, S_IWUSR | S_IRUGO,
2614 show_auto_temp, store_auto_temp, 1, 2),
2615 SENSOR_ATTR_2(pwm2_auto_point3_temp_hyst, S_IWUSR | S_IRUGO,
2616 show_auto_temp_hyst, store_auto_temp_hyst, 1, 2),
2617 SENSOR_ATTR_2(pwm2_auto_point4_pwm, S_IWUSR | S_IRUGO,
2618 show_auto_pwm, store_auto_pwm, 1, 3),
2619 SENSOR_ATTR_2(pwm2_auto_point4_temp, S_IWUSR | S_IRUGO,
2620 show_auto_temp, store_auto_temp, 1, 3),
2621 SENSOR_ATTR_2(pwm2_auto_point4_temp_hyst, S_IWUSR | S_IRUGO,
2622 show_auto_temp_hyst, store_auto_temp_hyst, 1, 3),
2623 SENSOR_ATTR_2(pwm2_auto_point5_pwm, S_IWUSR | S_IRUGO,
2624 show_auto_pwm, store_auto_pwm, 1, 4),
2625 SENSOR_ATTR_2(pwm2_auto_point5_temp, S_IWUSR | S_IRUGO,
2626 show_auto_temp, store_auto_temp, 1, 4),
2627 SENSOR_ATTR_2(pwm2_auto_point5_temp_hyst, S_IWUSR | S_IRUGO,
2628 show_auto_temp_hyst, store_auto_temp_hyst, 1, 4),
2629 SENSOR_ATTR_2(pwm2_auto_point6_pwm, S_IWUSR | S_IRUGO,
2630 show_auto_pwm, store_auto_pwm, 1, 5),
2631 SENSOR_ATTR_2(pwm2_auto_point6_temp, S_IWUSR | S_IRUGO,
2632 show_auto_temp, store_auto_temp, 1, 5),
2633 SENSOR_ATTR_2(pwm2_auto_point6_temp_hyst, S_IWUSR | S_IRUGO,
2634 show_auto_temp_hyst, store_auto_temp_hyst, 1, 5),
2635 SENSOR_ATTR_2(pwm2_auto_point7_pwm, S_IWUSR | S_IRUGO,
2636 show_auto_pwm, store_auto_pwm, 1, 6),
2637 SENSOR_ATTR_2(pwm2_auto_point7_temp, S_IWUSR | S_IRUGO,
2638 show_auto_temp, store_auto_temp, 1, 6),
2639 SENSOR_ATTR_2(pwm2_auto_point7_temp_hyst, S_IWUSR | S_IRUGO,
2640 show_auto_temp_hyst, store_auto_temp_hyst, 1, 6),
2642 SENSOR_ATTR_2(pwm3_auto_point1_pwm, S_IWUSR | S_IRUGO,
2643 show_auto_pwm, store_auto_pwm, 2, 0),
2644 SENSOR_ATTR_2(pwm3_auto_point1_temp, S_IWUSR | S_IRUGO,
2645 show_auto_temp, store_auto_temp, 2, 0),
2646 SENSOR_ATTR_2(pwm3_auto_point1_temp_hyst, S_IWUSR | S_IRUGO,
2647 show_auto_temp_hyst, store_auto_temp_hyst, 2, 0),
2648 SENSOR_ATTR_2(pwm3_auto_point2_pwm, S_IWUSR | S_IRUGO,
2649 show_auto_pwm, store_auto_pwm, 2, 1),
2650 SENSOR_ATTR_2(pwm3_auto_point2_temp, S_IWUSR | S_IRUGO,
2651 show_auto_temp, store_auto_temp, 2, 1),
2652 SENSOR_ATTR_2(pwm3_auto_point2_temp_hyst, S_IWUSR | S_IRUGO,
2653 show_auto_temp_hyst, store_auto_temp_hyst, 2, 1),
2654 SENSOR_ATTR_2(pwm3_auto_point3_pwm, S_IWUSR | S_IRUGO,
2655 show_auto_pwm, store_auto_pwm, 2, 2),
2656 SENSOR_ATTR_2(pwm3_auto_point3_temp, S_IWUSR | S_IRUGO,
2657 show_auto_temp, store_auto_temp, 2, 2),
2658 SENSOR_ATTR_2(pwm3_auto_point3_temp_hyst, S_IWUSR | S_IRUGO,
2659 show_auto_temp_hyst, store_auto_temp_hyst, 2, 2),
2660 SENSOR_ATTR_2(pwm3_auto_point4_pwm, S_IWUSR | S_IRUGO,
2661 show_auto_pwm, store_auto_pwm, 2, 3),
2662 SENSOR_ATTR_2(pwm3_auto_point4_temp, S_IWUSR | S_IRUGO,
2663 show_auto_temp, store_auto_temp, 2, 3),
2664 SENSOR_ATTR_2(pwm3_auto_point4_temp_hyst, S_IWUSR | S_IRUGO,
2665 show_auto_temp_hyst, store_auto_temp_hyst, 2, 3),
2666 SENSOR_ATTR_2(pwm3_auto_point5_pwm, S_IWUSR | S_IRUGO,
2667 show_auto_pwm, store_auto_pwm, 2, 4),
2668 SENSOR_ATTR_2(pwm3_auto_point5_temp, S_IWUSR | S_IRUGO,
2669 show_auto_temp, store_auto_temp, 2, 4),
2670 SENSOR_ATTR_2(pwm3_auto_point5_temp_hyst, S_IWUSR | S_IRUGO,
2671 show_auto_temp_hyst, store_auto_temp_hyst, 2, 4),
2672 SENSOR_ATTR_2(pwm3_auto_point6_pwm, S_IWUSR | S_IRUGO,
2673 show_auto_pwm, store_auto_pwm, 2, 5),
2674 SENSOR_ATTR_2(pwm3_auto_point6_temp, S_IWUSR | S_IRUGO,
2675 show_auto_temp, store_auto_temp, 2, 5),
2676 SENSOR_ATTR_2(pwm3_auto_point6_temp_hyst, S_IWUSR | S_IRUGO,
2677 show_auto_temp_hyst, store_auto_temp_hyst, 2, 5),
2678 SENSOR_ATTR_2(pwm3_auto_point7_pwm, S_IWUSR | S_IRUGO,
2679 show_auto_pwm, store_auto_pwm, 2, 6),
2680 SENSOR_ATTR_2(pwm3_auto_point7_temp, S_IWUSR | S_IRUGO,
2681 show_auto_temp, store_auto_temp, 2, 6),
2682 SENSOR_ATTR_2(pwm3_auto_point7_temp_hyst, S_IWUSR | S_IRUGO,
2683 show_auto_temp_hyst, store_auto_temp_hyst, 2, 6),
2685 SENSOR_ATTR_2(pwm4_auto_point1_pwm, S_IWUSR | S_IRUGO,
2686 show_auto_pwm, store_auto_pwm, 3, 0),
2687 SENSOR_ATTR_2(pwm4_auto_point1_temp, S_IWUSR | S_IRUGO,
2688 show_auto_temp, store_auto_temp, 3, 0),
2689 SENSOR_ATTR_2(pwm4_auto_point1_temp_hyst, S_IWUSR | S_IRUGO,
2690 show_auto_temp_hyst, store_auto_temp_hyst, 3, 0),
2691 SENSOR_ATTR_2(pwm4_auto_point2_pwm, S_IWUSR | S_IRUGO,
2692 show_auto_pwm, store_auto_pwm, 3, 1),
2693 SENSOR_ATTR_2(pwm4_auto_point2_temp, S_IWUSR | S_IRUGO,
2694 show_auto_temp, store_auto_temp, 3, 1),
2695 SENSOR_ATTR_2(pwm4_auto_point2_temp_hyst, S_IWUSR | S_IRUGO,
2696 show_auto_temp_hyst, store_auto_temp_hyst, 3, 1),
2697 SENSOR_ATTR_2(pwm4_auto_point3_pwm, S_IWUSR | S_IRUGO,
2698 show_auto_pwm, store_auto_pwm, 3, 2),
2699 SENSOR_ATTR_2(pwm4_auto_point3_temp, S_IWUSR | S_IRUGO,
2700 show_auto_temp, store_auto_temp, 3, 2),
2701 SENSOR_ATTR_2(pwm4_auto_point3_temp_hyst, S_IWUSR | S_IRUGO,
2702 show_auto_temp_hyst, store_auto_temp_hyst, 3, 2),
2703 SENSOR_ATTR_2(pwm4_auto_point4_pwm, S_IWUSR | S_IRUGO,
2704 show_auto_pwm, store_auto_pwm, 3, 3),
2705 SENSOR_ATTR_2(pwm4_auto_point4_temp, S_IWUSR | S_IRUGO,
2706 show_auto_temp, store_auto_temp, 3, 3),
2707 SENSOR_ATTR_2(pwm4_auto_point4_temp_hyst, S_IWUSR | S_IRUGO,
2708 show_auto_temp_hyst, store_auto_temp_hyst, 3, 3),
2709 SENSOR_ATTR_2(pwm4_auto_point5_pwm, S_IWUSR | S_IRUGO,
2710 show_auto_pwm, store_auto_pwm, 3, 4),
2711 SENSOR_ATTR_2(pwm4_auto_point5_temp, S_IWUSR | S_IRUGO,
2712 show_auto_temp, store_auto_temp, 3, 4),
2713 SENSOR_ATTR_2(pwm4_auto_point5_temp_hyst, S_IWUSR | S_IRUGO,
2714 show_auto_temp_hyst, store_auto_temp_hyst, 3, 4),
2715 SENSOR_ATTR_2(pwm4_auto_point6_pwm, S_IWUSR | S_IRUGO,
2716 show_auto_pwm, store_auto_pwm, 3, 5),
2717 SENSOR_ATTR_2(pwm4_auto_point6_temp, S_IWUSR | S_IRUGO,
2718 show_auto_temp, store_auto_temp, 3, 5),
2719 SENSOR_ATTR_2(pwm4_auto_point6_temp_hyst, S_IWUSR | S_IRUGO,
2720 show_auto_temp_hyst, store_auto_temp_hyst, 3, 5),
2721 SENSOR_ATTR_2(pwm4_auto_point7_pwm, S_IWUSR | S_IRUGO,
2722 show_auto_pwm, store_auto_pwm, 3, 6),
2723 SENSOR_ATTR_2(pwm4_auto_point7_temp, S_IWUSR | S_IRUGO,
2724 show_auto_temp, store_auto_temp, 3, 6),
2725 SENSOR_ATTR_2(pwm4_auto_point7_temp_hyst, S_IWUSR | S_IRUGO,
2726 show_auto_temp_hyst, store_auto_temp_hyst, 3, 6),
2728 SENSOR_ATTR_2(pwm5_auto_point1_pwm, S_IWUSR | S_IRUGO,
2729 show_auto_pwm, store_auto_pwm, 4, 0),
2730 SENSOR_ATTR_2(pwm5_auto_point1_temp, S_IWUSR | S_IRUGO,
2731 show_auto_temp, store_auto_temp, 4, 0),
2732 SENSOR_ATTR_2(pwm5_auto_point1_temp_hyst, S_IWUSR | S_IRUGO,
2733 show_auto_temp_hyst, store_auto_temp_hyst, 4, 0),
2734 SENSOR_ATTR_2(pwm5_auto_point2_pwm, S_IWUSR | S_IRUGO,
2735 show_auto_pwm, store_auto_pwm, 4, 1),
2736 SENSOR_ATTR_2(pwm5_auto_point2_temp, S_IWUSR | S_IRUGO,
2737 show_auto_temp, store_auto_temp, 4, 1),
2738 SENSOR_ATTR_2(pwm5_auto_point2_temp_hyst, S_IWUSR | S_IRUGO,
2739 show_auto_temp_hyst, store_auto_temp_hyst, 4, 1),
2740 SENSOR_ATTR_2(pwm5_auto_point3_pwm, S_IWUSR | S_IRUGO,
2741 show_auto_pwm, store_auto_pwm, 4, 2),
2742 SENSOR_ATTR_2(pwm5_auto_point3_temp, S_IWUSR | S_IRUGO,
2743 show_auto_temp, store_auto_temp, 4, 2),
2744 SENSOR_ATTR_2(pwm5_auto_point3_temp_hyst, S_IWUSR | S_IRUGO,
2745 show_auto_temp_hyst, store_auto_temp_hyst, 4, 2),
2746 SENSOR_ATTR_2(pwm5_auto_point4_pwm, S_IWUSR | S_IRUGO,
2747 show_auto_pwm, store_auto_pwm, 4, 3),
2748 SENSOR_ATTR_2(pwm5_auto_point4_temp, S_IWUSR | S_IRUGO,
2749 show_auto_temp, store_auto_temp, 4, 3),
2750 SENSOR_ATTR_2(pwm5_auto_point4_temp_hyst, S_IWUSR | S_IRUGO,
2751 show_auto_temp_hyst, store_auto_temp_hyst, 4, 3),
2752 SENSOR_ATTR_2(pwm5_auto_point5_pwm, S_IWUSR | S_IRUGO,
2753 show_auto_pwm, store_auto_pwm, 4, 4),
2754 SENSOR_ATTR_2(pwm5_auto_point5_temp, S_IWUSR | S_IRUGO,
2755 show_auto_temp, store_auto_temp, 4, 4),
2756 SENSOR_ATTR_2(pwm5_auto_point5_temp_hyst, S_IWUSR | S_IRUGO,
2757 show_auto_temp_hyst, store_auto_temp_hyst, 4, 4),
2758 SENSOR_ATTR_2(pwm5_auto_point6_pwm, S_IWUSR | S_IRUGO,
2759 show_auto_pwm, store_auto_pwm, 4, 5),
2760 SENSOR_ATTR_2(pwm5_auto_point6_temp, S_IWUSR | S_IRUGO,
2761 show_auto_temp, store_auto_temp, 4, 5),
2762 SENSOR_ATTR_2(pwm5_auto_point6_temp_hyst, S_IWUSR | S_IRUGO,
2763 show_auto_temp_hyst, store_auto_temp_hyst, 4, 5),
2764 SENSOR_ATTR_2(pwm5_auto_point7_pwm, S_IWUSR | S_IRUGO,
2765 show_auto_pwm, store_auto_pwm, 4, 6),
2766 SENSOR_ATTR_2(pwm5_auto_point7_temp, S_IWUSR | S_IRUGO,
2767 show_auto_temp, store_auto_temp, 4, 6),
2768 SENSOR_ATTR_2(pwm5_auto_point7_temp_hyst, S_IWUSR | S_IRUGO,
2769 show_auto_temp_hyst, store_auto_temp_hyst, 4, 6),
2773 show_vid(struct device *dev, struct device_attribute *attr, char *buf)
2775 struct nct6775_data *data = dev_get_drvdata(dev);
2776 return sprintf(buf, "%d\n", vid_from_reg(data->vid, data->vrm));
2779 static DEVICE_ATTR(cpu0_vid, S_IRUGO, show_vid, NULL);
2781 /* Case open detection */
2784 show_caseopen(struct device *dev, struct device_attribute *attr, char *buf)
2786 struct nct6775_data *data = nct6775_update_device(dev);
2788 return sprintf(buf, "%d\n",
2789 !!(data->caseopen & to_sensor_dev_attr_2(attr)->index));
2793 clear_caseopen(struct device *dev, struct device_attribute *attr,
2794 const char *buf, size_t count)
2796 struct nct6775_data *data = dev_get_drvdata(dev);
2797 struct nct6775_sio_data *sio_data = dev->platform_data;
2798 int nr = to_sensor_dev_attr(attr)->index;
2802 if (kstrtoul(buf, 10, &val) || val != 0)
2805 mutex_lock(&data->update_lock);
2808 * Use CR registers to clear caseopen status.
2809 * The CR registers are the same for all chips, and not all chips
2810 * support clearing the caseopen status through "regular" registers.
2812 superio_enter(sio_data->sioreg);
2813 superio_select(sio_data->sioreg, NCT6775_LD_ACPI);
2814 reg = superio_inb(sio_data->sioreg, NCT6775_REG_CR_CASEOPEN_CLR[nr]);
2815 reg |= NCT6775_CR_CASEOPEN_CLR_MASK[nr];
2816 superio_outb(sio_data->sioreg, NCT6775_REG_CR_CASEOPEN_CLR[nr], reg);
2817 superio_exit(sio_data->sioreg);
2819 data->valid = 0; /* Force cache refresh */
2821 mutex_unlock(&data->update_lock);
2826 static struct sensor_device_attribute sda_caseopen[] = {
2827 SENSOR_ATTR(intrusion0_alarm, S_IWUSR | S_IRUGO, show_caseopen,
2829 SENSOR_ATTR(intrusion1_alarm, S_IWUSR | S_IRUGO, show_caseopen,
2834 * Driver and device management
2837 static void nct6775_device_remove_files(struct device *dev)
2840 * some entries in the following arrays may not have been used in
2841 * device_create_file(), but device_remove_file() will ignore them
2844 struct nct6775_data *data = dev_get_drvdata(dev);
2846 for (i = 0; i < data->pwm_num; i++)
2847 sysfs_remove_group(&dev->kobj, &nct6775_group_pwm[i]);
2849 for (i = 0; i < ARRAY_SIZE(sda_weight_duty_base); i++)
2850 device_remove_file(dev, &sda_weight_duty_base[i].dev_attr);
2852 for (i = 0; i < ARRAY_SIZE(sda_auto_pwm_arrays); i++)
2853 device_remove_file(dev, &sda_auto_pwm_arrays[i].dev_attr);
2855 for (i = 0; i < data->in_num; i++)
2856 sysfs_remove_group(&dev->kobj, &nct6775_group_in[i]);
2858 for (i = 0; i < 5; i++) {
2859 device_remove_file(dev, &sda_fan_input[i].dev_attr);
2860 device_remove_file(dev, &sda_fan_alarm[i].dev_attr);
2861 device_remove_file(dev, &sda_fan_div[i].dev_attr);
2862 device_remove_file(dev, &sda_fan_min[i].dev_attr);
2864 for (i = 0; i < NUM_TEMP; i++) {
2865 if (!(data->have_temp & (1 << i)))
2867 device_remove_file(dev, &sda_temp_input[i].dev_attr);
2868 device_remove_file(dev, &sda_temp_label[i].dev_attr);
2869 device_remove_file(dev, &sda_temp_max[i].dev_attr);
2870 device_remove_file(dev, &sda_temp_max_hyst[i].dev_attr);
2871 if (!(data->have_temp_fixed & (1 << i)))
2873 device_remove_file(dev, &sda_temp_type[i].dev_attr);
2874 device_remove_file(dev, &sda_temp_offset[i].dev_attr);
2875 if (i >= NUM_TEMP_ALARM)
2877 device_remove_file(dev, &sda_temp_alarm[i].dev_attr);
2880 device_remove_file(dev, &sda_caseopen[0].dev_attr);
2881 device_remove_file(dev, &sda_caseopen[1].dev_attr);
2883 device_remove_file(dev, &dev_attr_name);
2884 device_remove_file(dev, &dev_attr_cpu0_vid);
2887 /* Get the monitoring functions started */
2888 static inline void __devinit nct6775_init_device(struct nct6775_data *data)
2893 /* Start monitoring if needed */
2894 if (data->REG_CONFIG) {
2895 tmp = nct6775_read_value(data, data->REG_CONFIG);
2897 nct6775_write_value(data, data->REG_CONFIG, tmp | 0x01);
2900 /* Enable temperature sensors if needed */
2901 for (i = 0; i < NUM_TEMP; i++) {
2902 if (!(data->have_temp & (1 << i)))
2904 if (!data->reg_temp_config[i])
2906 tmp = nct6775_read_value(data, data->reg_temp_config[i]);
2908 nct6775_write_value(data, data->reg_temp_config[i],
2912 /* Enable VBAT monitoring if needed */
2913 tmp = nct6775_read_value(data, data->REG_VBAT);
2915 nct6775_write_value(data, data->REG_VBAT, tmp | 0x01);
2917 diode = nct6775_read_value(data, data->REG_DIODE);
2919 for (i = 0; i < data->temp_fixed_num; i++) {
2920 if (!(data->have_temp_fixed & (1 << i)))
2922 if ((tmp & (0x02 << i))) /* diode */
2923 data->temp_type[i] = 3 - ((diode >> i) & 0x02);
2924 else /* thermistor */
2925 data->temp_type[i] = 4;
2929 static void __devinit
2930 nct6775_check_fan_inputs(const struct nct6775_sio_data *sio_data,
2931 struct nct6775_data *data)
2934 bool fan3pin, fan3min, fan4pin, fan4min, fan5pin;
2935 bool pwm3pin, pwm4pin, pwm5pin;
2937 superio_enter(sio_data->sioreg);
2939 /* fan4 and fan5 share some pins with the GPIO and serial flash */
2940 if (data->kind == nct6775) {
2941 regval = superio_inb(sio_data->sioreg, 0x2c);
2943 fan3pin = regval & (1 << 6);
2945 pwm3pin = regval & (1 << 7);
2947 /* On NCT6775, fan4 shares pins with the fdc interface */
2948 fan4pin = !(superio_inb(sio_data->sioreg, 0x2A) & 0x80);
2953 } else if (data->kind == nct6776) {
2954 bool gpok = superio_inb(sio_data->sioreg, 0x27) & 0x80;
2956 superio_select(sio_data->sioreg, NCT6775_LD_HWM);
2957 regval = superio_inb(sio_data->sioreg, SIO_REG_ENABLE);
2962 fan3pin = !(superio_inb(sio_data->sioreg, 0x24) & 0x40);
2967 fan4pin = superio_inb(sio_data->sioreg, 0x1C) & 0x01;
2972 fan5pin = superio_inb(sio_data->sioreg, 0x1C) & 0x02;
2979 } else { /* NCT6779D */
2980 regval = superio_inb(sio_data->sioreg, 0x1c);
2982 fan3pin = !(regval & (1 << 5));
2983 fan4pin = !(regval & (1 << 6));
2984 fan5pin = !(regval & (1 << 7));
2986 pwm3pin = !(regval & (1 << 0));
2987 pwm4pin = !(regval & (1 << 1));
2988 pwm5pin = !(regval & (1 << 2));
2994 superio_exit(sio_data->sioreg);
2996 data->has_fan = data->has_fan_min = 0x03; /* fan1 and fan2 */
2997 data->has_fan |= (fan3pin << 2);
2998 data->has_fan_min |= (fan3min << 2);
3000 data->has_fan |= (fan4pin << 3) | (fan5pin << 4);
3001 data->has_fan_min |= (fan4min << 3) | (fan5pin << 4);
3003 data->has_pwm = 0x03 | (pwm3pin << 2) | (pwm4pin << 3) | (pwm5pin << 4);
3006 static int __devinit nct6775_probe(struct platform_device *pdev)
3008 struct device *dev = &pdev->dev;
3009 struct nct6775_sio_data *sio_data = dev->platform_data;
3010 struct nct6775_data *data;
3011 struct resource *res;
3014 const u16 *reg_temp, *reg_temp_over, *reg_temp_hyst, *reg_temp_config;
3016 res = platform_get_resource(pdev, IORESOURCE_IO, 0);
3017 if (!request_region(res->start, IOREGION_LENGTH, DRVNAME)) {
3019 dev_err(dev, "Failed to request region 0x%lx-0x%lx\n",
3020 (unsigned long)res->start,
3021 (unsigned long)res->start + IOREGION_LENGTH - 1);
3025 data = devm_kzalloc(&pdev->dev, sizeof(struct nct6775_data),
3032 data->kind = sio_data->kind;
3033 data->addr = res->start;
3034 mutex_init(&data->lock);
3035 mutex_init(&data->update_lock);
3036 data->name = nct6775_device_names[data->kind];
3037 platform_set_drvdata(pdev, data);
3039 switch (data->kind) {
3042 data->have_in = 0x1ff;
3044 data->auto_pwm_num = 6;
3045 data->has_fan_div = true;
3046 data->temp_fixed_num = 3;
3048 data->fan_from_reg = fan_from_reg16;
3049 data->fan_from_reg_min = fan_from_reg8;
3051 data->REG_CONFIG = NCT6775_REG_CONFIG;
3052 data->REG_VBAT = NCT6775_REG_VBAT;
3053 data->REG_DIODE = NCT6775_REG_DIODE;
3054 data->REG_VIN = NCT6775_REG_IN;
3055 data->REG_IN_MINMAX[0] = NCT6775_REG_IN_MIN;
3056 data->REG_IN_MINMAX[1] = NCT6775_REG_IN_MAX;
3057 data->REG_TARGET = NCT6775_REG_TARGET;
3058 data->REG_FAN = NCT6775_REG_FAN;
3059 data->REG_FAN_MODE = NCT6775_REG_FAN_MODE;
3060 data->REG_FAN_MIN = NCT6775_REG_FAN_MIN;
3061 data->REG_FAN_TIME[0] = NCT6775_REG_FAN_STOP_TIME;
3062 data->REG_FAN_TIME[1] = NCT6775_REG_FAN_STEP_UP_TIME;
3063 data->REG_FAN_TIME[2] = NCT6775_REG_FAN_STEP_DOWN_TIME;
3064 data->REG_PWM[0] = NCT6775_REG_PWM;
3065 data->REG_PWM[1] = NCT6775_REG_FAN_START_OUTPUT;
3066 data->REG_PWM[2] = NCT6775_REG_FAN_STOP_OUTPUT;
3067 data->REG_PWM_READ = NCT6775_REG_PWM_READ;
3068 data->REG_PWM_MODE = NCT6775_REG_PWM_MODE;
3069 data->PWM_MODE_MASK = NCT6775_PWM_MODE_MASK;
3070 data->REG_TEMP_MON = NCT6775_REG_TEMP_MON;
3071 data->REG_AUTO_TEMP = NCT6775_REG_AUTO_TEMP;
3072 data->REG_AUTO_PWM = NCT6775_REG_AUTO_PWM;
3073 data->REG_CRITICAL_TEMP = NCT6775_REG_CRITICAL_TEMP;
3074 data->REG_CRITICAL_TEMP_TOLERANCE
3075 = NCT6775_REG_CRITICAL_TEMP_TOLERANCE;
3076 data->REG_TEMP_OFFSET = NCT6775_REG_TEMP_OFFSET;
3077 data->REG_TEMP_SOURCE = NCT6775_REG_TEMP_SOURCE;
3078 data->REG_TEMP_SEL[0] = NCT6775_REG_TEMP_SEL;
3079 data->REG_TEMP_SEL[1] = NCT6775_REG_WEIGHT_TEMP_SEL;
3080 data->REG_WEIGHT_TEMP[0] = NCT6775_REG_WEIGHT_TEMP_STEP;
3081 data->REG_WEIGHT_TEMP[1] = NCT6775_REG_WEIGHT_TEMP_STEP_TOL;
3082 data->REG_WEIGHT_TEMP[2] = NCT6775_REG_WEIGHT_TEMP_BASE;
3083 data->REG_WEIGHT_DUTY[0] = NCT6775_REG_WEIGHT_DUTY_STEP;
3084 data->REG_ALARM = NCT6775_REG_ALARM;
3085 data->REG_CASEOPEN = NCT6775_REG_CASEOPEN;
3086 data->CASEOPEN_MASK = NCT6775_CASEOPEN_MASK;
3088 reg_temp = NCT6775_REG_TEMP;
3089 reg_temp_over = NCT6775_REG_TEMP_OVER;
3090 reg_temp_hyst = NCT6775_REG_TEMP_HYST;
3091 reg_temp_config = NCT6775_REG_TEMP_CONFIG;
3096 data->have_in = 0x1ff;
3098 data->auto_pwm_num = 4;
3099 data->has_fan_div = false;
3100 data->temp_fixed_num = 3;
3102 data->fan_from_reg = fan_from_reg13;
3103 data->fan_from_reg_min = fan_from_reg13;
3105 data->REG_CONFIG = NCT6775_REG_CONFIG;
3106 data->REG_VBAT = NCT6775_REG_VBAT;
3107 data->REG_DIODE = NCT6775_REG_DIODE;
3108 data->REG_VIN = NCT6775_REG_IN;
3109 data->REG_IN_MINMAX[0] = NCT6775_REG_IN_MIN;
3110 data->REG_IN_MINMAX[1] = NCT6775_REG_IN_MAX;
3111 data->REG_TARGET = NCT6775_REG_TARGET;
3112 data->REG_FAN = NCT6775_REG_FAN;
3113 data->REG_FAN_MODE = NCT6775_REG_FAN_MODE;
3114 data->REG_FAN_MIN = NCT6776_REG_FAN_MIN;
3115 data->REG_FAN_TIME[0] = NCT6775_REG_FAN_STOP_TIME;
3116 data->REG_FAN_TIME[1] = NCT6775_REG_FAN_STEP_UP_TIME;
3117 data->REG_FAN_TIME[2] = NCT6775_REG_FAN_STEP_DOWN_TIME;
3118 data->REG_PWM[0] = NCT6775_REG_PWM;
3119 data->REG_PWM[1] = NCT6775_REG_FAN_START_OUTPUT;
3120 data->REG_PWM[2] = NCT6775_REG_FAN_STOP_OUTPUT;
3121 data->REG_PWM_READ = NCT6775_REG_PWM_READ;
3122 data->REG_PWM_MODE = NCT6776_REG_PWM_MODE;
3123 data->PWM_MODE_MASK = NCT6776_PWM_MODE_MASK;
3124 data->REG_TEMP_MON = NCT6775_REG_TEMP_MON;
3125 data->REG_AUTO_TEMP = NCT6775_REG_AUTO_TEMP;
3126 data->REG_AUTO_PWM = NCT6775_REG_AUTO_PWM;
3127 data->REG_CRITICAL_TEMP = NCT6775_REG_CRITICAL_TEMP;
3128 data->REG_CRITICAL_TEMP_TOLERANCE
3129 = NCT6775_REG_CRITICAL_TEMP_TOLERANCE;
3130 data->REG_TEMP_OFFSET = NCT6775_REG_TEMP_OFFSET;
3131 data->REG_TEMP_SOURCE = NCT6775_REG_TEMP_SOURCE;
3132 data->REG_TEMP_SEL[0] = NCT6775_REG_TEMP_SEL;
3133 data->REG_TEMP_SEL[1] = NCT6775_REG_WEIGHT_TEMP_SEL;
3134 data->REG_WEIGHT_TEMP[0] = NCT6775_REG_WEIGHT_TEMP_STEP;
3135 data->REG_WEIGHT_TEMP[1] = NCT6775_REG_WEIGHT_TEMP_STEP_TOL;
3136 data->REG_WEIGHT_TEMP[2] = NCT6775_REG_WEIGHT_TEMP_BASE;
3137 data->REG_WEIGHT_DUTY[0] = NCT6775_REG_WEIGHT_DUTY_STEP;
3138 data->REG_WEIGHT_DUTY[1] = NCT6776_REG_WEIGHT_DUTY_BASE;
3139 data->REG_ALARM = NCT6775_REG_ALARM;
3140 data->REG_CASEOPEN = NCT6775_REG_CASEOPEN;
3141 data->CASEOPEN_MASK = NCT6776_CASEOPEN_MASK;
3143 reg_temp = NCT6775_REG_TEMP;
3144 reg_temp_over = NCT6775_REG_TEMP_OVER;
3145 reg_temp_hyst = NCT6775_REG_TEMP_HYST;
3146 reg_temp_config = NCT6776_REG_TEMP_CONFIG;
3151 data->have_in = 0x7fff;
3153 data->auto_pwm_num = 4;
3154 data->has_fan_div = false;
3155 data->temp_fixed_num = 6;
3157 data->fan_from_reg = fan_from_reg13;
3158 data->fan_from_reg_min = fan_from_reg13;
3160 data->REG_CONFIG = NCT6775_REG_CONFIG;
3161 data->REG_VBAT = NCT6775_REG_VBAT;
3162 data->REG_DIODE = NCT6775_REG_DIODE;
3163 data->REG_VIN = NCT6779_REG_IN;
3164 data->REG_IN_MINMAX[0] = NCT6775_REG_IN_MIN;
3165 data->REG_IN_MINMAX[1] = NCT6775_REG_IN_MAX;
3166 data->REG_TARGET = NCT6775_REG_TARGET;
3167 data->REG_FAN = NCT6779_REG_FAN;
3168 data->REG_FAN_MODE = NCT6775_REG_FAN_MODE;
3169 data->REG_FAN_MIN = NCT6776_REG_FAN_MIN;
3170 data->REG_FAN_TIME[0] = NCT6775_REG_FAN_STOP_TIME;
3171 data->REG_FAN_TIME[1] = NCT6775_REG_FAN_STEP_UP_TIME;
3172 data->REG_FAN_TIME[2] = NCT6775_REG_FAN_STEP_DOWN_TIME;
3173 data->REG_PWM[0] = NCT6775_REG_PWM;
3174 data->REG_PWM[1] = NCT6775_REG_FAN_START_OUTPUT;
3175 data->REG_PWM[2] = NCT6775_REG_FAN_STOP_OUTPUT;
3176 data->REG_PWM_READ = NCT6775_REG_PWM_READ;
3177 data->REG_PWM_MODE = NCT6776_REG_PWM_MODE;
3178 data->PWM_MODE_MASK = NCT6776_PWM_MODE_MASK;
3179 data->REG_TEMP_FIXED = NCT6779_REG_TEMP_FIXED;
3180 data->REG_TEMP_MON = NCT6775_REG_TEMP_MON;
3181 data->REG_AUTO_TEMP = NCT6775_REG_AUTO_TEMP;
3182 data->REG_AUTO_PWM = NCT6775_REG_AUTO_PWM;
3183 data->REG_CRITICAL_TEMP = NCT6775_REG_CRITICAL_TEMP;
3184 data->REG_CRITICAL_TEMP_TOLERANCE
3185 = NCT6775_REG_CRITICAL_TEMP_TOLERANCE;
3186 data->REG_TEMP_OFFSET = NCT6779_REG_TEMP_OFFSET;
3187 data->REG_TEMP_SOURCE = NCT6775_REG_TEMP_SOURCE;
3188 data->REG_TEMP_SEL[0] = NCT6775_REG_TEMP_SEL;
3189 data->REG_TEMP_SEL[1] = NCT6775_REG_WEIGHT_TEMP_SEL;
3190 data->REG_WEIGHT_TEMP[0] = NCT6775_REG_WEIGHT_TEMP_STEP;
3191 data->REG_WEIGHT_TEMP[1] = NCT6775_REG_WEIGHT_TEMP_STEP_TOL;
3192 data->REG_WEIGHT_TEMP[2] = NCT6775_REG_WEIGHT_TEMP_BASE;
3193 data->REG_WEIGHT_DUTY[0] = NCT6775_REG_WEIGHT_DUTY_STEP;
3194 data->REG_WEIGHT_DUTY[1] = NCT6776_REG_WEIGHT_DUTY_BASE;
3195 data->REG_ALARM = NCT6779_REG_ALARM;
3196 data->REG_CASEOPEN = NCT6775_REG_CASEOPEN;
3197 data->CASEOPEN_MASK = NCT6776_CASEOPEN_MASK;
3199 reg_temp = NCT6779_REG_TEMP;
3200 reg_temp_over = NCT6775_REG_TEMP_OVER;
3201 reg_temp_hyst = NCT6775_REG_TEMP_HYST;
3202 reg_temp_config = NCT6776_REG_TEMP_CONFIG;
3210 /* Default to no temperature inputs, code below will adjust as needed */
3211 data->have_temp = 0;
3213 s = NUM_TEMP_FIXED; /* Index for first non-standard temperature */
3214 for (i = 0; i < NUM_REG_TEMP; i++) {
3215 if (reg_temp[i] == 0)
3218 src = nct6775_read_value(data, data->REG_TEMP_SOURCE[i]);
3221 if (!src || (mask & (1 << src)))
3224 /* Use fixed index for SYSTIN(1), CPUTIN(2), AUXTIN(3) */
3225 if (src <= data->temp_fixed_num) {
3226 data->have_temp |= (1 << (src - 1));
3227 data->have_temp_fixed |= (1 << (src - 1));
3229 data->reg_temp[0][src - 1] = reg_temp[i];
3230 data->reg_temp[1][src - 1] = reg_temp_over[i];
3231 data->reg_temp[2][src - 1] = reg_temp_hyst[i];
3232 data->reg_temp_config[src - 1] = reg_temp_config[i];
3233 data->temp_src[src - 1] = src;
3240 /* Use dynamic index for other sources */
3241 data->have_temp |= 1 << s;
3244 data->reg_temp[0][s] = reg_temp[i];
3245 data->reg_temp[1][s] = reg_temp_over[i];
3246 data->reg_temp[2][s] = reg_temp_hyst[i];
3247 data->reg_temp_config[s] = reg_temp_config[i];
3250 data->temp_src[s] = src;
3254 if (data->REG_TEMP_FIXED) {
3255 for (i = 0; i < NUM_TEMP_FIXED; i++) {
3256 if (data->have_temp & (1 << i))
3258 if (!data->REG_TEMP_FIXED[i])
3260 data->have_temp |= (1 << i);
3261 data->reg_temp[0][i] = data->REG_TEMP_FIXED[i];
3262 data->temp_src[i] = i + 1;
3266 switch (data->kind) {
3268 data->temp_label = nct6775_temp_label;
3272 * On NCT6776, AUXTIN and VIN3 pins are shared.
3273 * Only way to detect it is to check if AUXTIN is used
3274 * as a temperature source, and if that source is
3277 * If that is the case, disable in6, which reports VIN3.
3278 * Otherwise disable temp3.
3280 if (data->have_temp & (1 << 2)) {
3281 u8 reg = nct6775_read_value(data,
3282 data->reg_temp_config[2]);
3284 data->have_temp &= ~(1 << 2);
3286 data->have_in &= ~(1 << 6);
3288 data->temp_label = nct6776_temp_label;
3297 * Assume voltage is disabled if the respective temperature is
3298 * used as temperature source.
3300 for (i = 0; i < ARRAY_SIZE(NCT6779_REG_TEMP); i++) {
3301 if (!(data->have_temp & (1 << i)))
3303 if (i == 2) /* AUXTIN0 */
3304 data->have_in &= ~(1 << 6); /* no VIN4 */
3305 if (i == 3) /* AUXTIN1 */
3306 data->have_in &= ~(1 << 10); /* no VIN5 */
3307 if (i == 4) /* AUXTIN2 */
3308 data->have_in &= ~(1 << 11); /* no VIN6 */
3309 if (i == 5) /* AUXTIN3 */
3310 data->have_in &= ~(1 << 14); /* no VIN7 */
3312 data->temp_label = nct6779_temp_label;
3316 /* Initialize the chip */
3317 nct6775_init_device(data);
3319 data->vrm = vid_which_vrm();
3320 superio_enter(sio_data->sioreg);
3323 * We can get the VID input values directly at logical device D 0xe3.
3325 superio_select(sio_data->sioreg, NCT6775_LD_VID);
3326 data->vid = superio_inb(sio_data->sioreg, 0xe3);
3327 err = device_create_file(dev, &dev_attr_cpu0_vid);
3334 superio_select(sio_data->sioreg, NCT6775_LD_HWM);
3335 tmp = superio_inb(sio_data->sioreg,
3336 NCT6775_REG_CR_FAN_DEBOUNCE);
3337 switch (data->kind) {
3346 superio_outb(sio_data->sioreg, NCT6775_REG_CR_FAN_DEBOUNCE,
3348 pr_info("Enabled fan debounce for chip %s\n", data->name);
3351 superio_exit(sio_data->sioreg);
3353 nct6775_check_fan_inputs(sio_data, data);
3355 /* Read fan clock dividers immediately */
3356 nct6775_update_fan_div_common(dev, data);
3358 /* Register sysfs hooks */
3359 for (i = 0; i < data->pwm_num; i++) {
3360 if (!(data->has_pwm & (1 << i)))
3363 err = sysfs_create_group(&dev->kobj, &nct6775_group_pwm[i]);
3367 if (data->REG_WEIGHT_DUTY[1]) {
3368 err = device_create_file(dev,
3369 &sda_weight_duty_base[i].dev_attr);
3374 for (i = 0; i < ARRAY_SIZE(sda_auto_pwm_arrays); i++) {
3375 struct sensor_device_attribute_2 *attr =
3376 &sda_auto_pwm_arrays[i];
3378 if (!(data->has_pwm & (1 << attr->nr)))
3380 if (attr->index > data->auto_pwm_num)
3382 err = device_create_file(dev, &attr->dev_attr);
3387 for (i = 0; i < data->in_num; i++) {
3388 if (!(data->have_in & (1 << i)))
3390 err = sysfs_create_group(&dev->kobj, &nct6775_group_in[i]);
3395 for (i = 0; i < 5; i++) {
3396 if (data->has_fan & (1 << i)) {
3397 err = device_create_file(dev,
3398 &sda_fan_input[i].dev_attr);
3401 err = device_create_file(dev,
3402 &sda_fan_alarm[i].dev_attr);
3405 if (data->kind != nct6776 &&
3406 data->kind != nct6779) {
3407 err = device_create_file(dev,
3408 &sda_fan_div[i].dev_attr);
3412 if (data->has_fan_min & (1 << i)) {
3413 err = device_create_file(dev,
3414 &sda_fan_min[i].dev_attr);
3421 for (i = 0; i < NUM_TEMP; i++) {
3422 if (!(data->have_temp & (1 << i)))
3424 err = device_create_file(dev, &sda_temp_input[i].dev_attr);
3427 if (data->temp_label) {
3428 err = device_create_file(dev,
3429 &sda_temp_label[i].dev_attr);
3433 if (data->reg_temp[1][i]) {
3434 err = device_create_file(dev,
3435 &sda_temp_max[i].dev_attr);
3439 if (data->reg_temp[2][i]) {
3440 err = device_create_file(dev,
3441 &sda_temp_max_hyst[i].dev_attr);
3445 if (!(data->have_temp_fixed & (1 << i)))
3447 err = device_create_file(dev, &sda_temp_type[i].dev_attr);
3450 err = device_create_file(dev,
3451 &sda_temp_offset[i].dev_attr);
3454 if (i >= NUM_TEMP_ALARM)
3456 err = device_create_file(dev, &sda_temp_alarm[i].dev_attr);
3461 for (i = 0; i < ARRAY_SIZE(sda_caseopen); i++) {
3462 if (!data->CASEOPEN_MASK[i])
3464 err = device_create_file(dev, &sda_caseopen[i].dev_attr);
3469 err = device_create_file(dev, &dev_attr_name);
3473 data->hwmon_dev = hwmon_device_register(dev);
3474 if (IS_ERR(data->hwmon_dev)) {
3475 err = PTR_ERR(data->hwmon_dev);
3482 nct6775_device_remove_files(dev);
3484 release_region(res->start, IOREGION_LENGTH);
3489 static int __devexit nct6775_remove(struct platform_device *pdev)
3491 struct nct6775_data *data = platform_get_drvdata(pdev);
3493 hwmon_device_unregister(data->hwmon_dev);
3494 nct6775_device_remove_files(&pdev->dev);
3495 release_region(data->addr, IOREGION_LENGTH);
3500 static struct platform_driver nct6775_driver = {
3502 .owner = THIS_MODULE,
3505 .probe = nct6775_probe,
3506 .remove = __devexit_p(nct6775_remove),
3509 /* nct6775_find() looks for a '627 in the Super-I/O config space */
3510 static int __init nct6775_find(int sioaddr, unsigned short *addr,
3511 struct nct6775_sio_data *sio_data)
3513 static const char __initdata sio_name_NCT6775[] = "NCT6775F";
3514 static const char __initdata sio_name_NCT6776[] = "NCT6776F";
3515 static const char __initdata sio_name_NCT6779[] = "NCT6779D";
3518 const char *sio_name;
3520 superio_enter(sioaddr);
3525 val = (superio_inb(sioaddr, SIO_REG_DEVID) << 8)
3526 | superio_inb(sioaddr, SIO_REG_DEVID + 1);
3527 switch (val & SIO_ID_MASK) {
3528 case SIO_NCT6775_ID:
3529 sio_data->kind = nct6775;
3530 sio_name = sio_name_NCT6775;
3532 case SIO_NCT6776_ID:
3533 sio_data->kind = nct6776;
3534 sio_name = sio_name_NCT6776;
3536 case SIO_NCT6779_ID:
3537 sio_data->kind = nct6779;
3538 sio_name = sio_name_NCT6779;
3542 pr_debug("unsupported chip ID: 0x%04x\n", val);
3543 superio_exit(sioaddr);
3547 /* We have a known chip, find the HWM I/O address */
3548 superio_select(sioaddr, NCT6775_LD_HWM);
3549 val = (superio_inb(sioaddr, SIO_REG_ADDR) << 8)
3550 | superio_inb(sioaddr, SIO_REG_ADDR + 1);
3551 *addr = val & IOREGION_ALIGNMENT;
3553 pr_err("Refusing to enable a Super-I/O device with a base I/O port 0\n");
3554 superio_exit(sioaddr);
3558 /* Activate logical device if needed */
3559 val = superio_inb(sioaddr, SIO_REG_ENABLE);
3560 if (!(val & 0x01)) {
3561 pr_warn("Forcibly enabling Super-I/O. "
3562 "Sensor is probably unusable.\n");
3563 superio_outb(sioaddr, SIO_REG_ENABLE, val | 0x01);
3566 superio_exit(sioaddr);
3567 pr_info("Found %s chip at %#x\n", sio_name, *addr);
3568 sio_data->sioreg = sioaddr;
3574 * when Super-I/O functions move to a separate file, the Super-I/O
3575 * bus will manage the lifetime of the device and this module will only keep
3576 * track of the nct6775 driver. But since we platform_device_alloc(), we
3577 * must keep track of the device
3579 static struct platform_device *pdev;
3581 static int __init sensors_nct6775_init(void)
3584 unsigned short address;
3585 struct resource res;
3586 struct nct6775_sio_data sio_data;
3589 * initialize sio_data->kind and sio_data->sioreg.
3591 * when Super-I/O functions move to a separate file, the Super-I/O
3592 * driver will probe 0x2e and 0x4e and auto-detect the presence of a
3593 * nct6775 hardware monitor, and call probe()
3595 if (nct6775_find(0x2e, &address, &sio_data) &&
3596 nct6775_find(0x4e, &address, &sio_data))
3599 err = platform_driver_register(&nct6775_driver);
3603 pdev = platform_device_alloc(DRVNAME, address);
3606 pr_err("Device allocation failed\n");
3607 goto exit_unregister;
3610 err = platform_device_add_data(pdev, &sio_data,
3611 sizeof(struct nct6775_sio_data));
3613 pr_err("Platform data allocation failed\n");
3614 goto exit_device_put;
3617 memset(&res, 0, sizeof(res));
3619 res.start = address + IOREGION_OFFSET;
3620 res.end = address + IOREGION_OFFSET + IOREGION_LENGTH - 1;
3621 res.flags = IORESOURCE_IO;
3623 err = acpi_check_resource_conflict(&res);
3625 goto exit_device_put;
3627 err = platform_device_add_resources(pdev, &res, 1);
3629 pr_err("Device resource addition failed (%d)\n", err);
3630 goto exit_device_put;
3633 /* platform_device_add calls probe() */
3634 err = platform_device_add(pdev);
3636 pr_err("Device addition failed (%d)\n", err);
3637 goto exit_device_put;
3643 platform_device_put(pdev);
3645 platform_driver_unregister(&nct6775_driver);
3650 static void __exit sensors_nct6775_exit(void)
3652 platform_device_unregister(pdev);
3653 platform_driver_unregister(&nct6775_driver);
3656 MODULE_AUTHOR("Guenter Roeck <linux@roeck-us.net>");
3657 MODULE_DESCRIPTION("NCT6775F/NCT6776F/NCT6779D driver");
3658 MODULE_LICENSE("GPL");
3660 module_init(sensors_nct6775_init);
3661 module_exit(sensors_nct6775_exit);