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>
60 enum kinds { nct6775, nct6776, nct6779 };
62 /* used to set data->name = nct6775_device_names[data->sio_kind] */
63 static const char * const nct6775_device_names[] = {
69 static unsigned short force_id;
70 module_param(force_id, ushort, 0);
71 MODULE_PARM_DESC(force_id, "Override the detected device ID");
73 static unsigned short fan_debounce;
74 module_param(fan_debounce, ushort, 0);
75 MODULE_PARM_DESC(fan_debounce, "Enable debouncing for fan RPM signal");
77 #define DRVNAME "nct6775"
80 * Super-I/O constants and functions
83 #define NCT6775_LD_ACPI 0x0a
84 #define NCT6775_LD_HWM 0x0b
85 #define NCT6775_LD_VID 0x0d
87 #define SIO_REG_LDSEL 0x07 /* Logical device select */
88 #define SIO_REG_DEVID 0x20 /* Device ID (2 bytes) */
89 #define SIO_REG_EN_VRM10 0x2C /* GPIO3, GPIO4 selection */
90 #define SIO_REG_ENABLE 0x30 /* Logical device enable */
91 #define SIO_REG_ADDR 0x60 /* Logical device address (2 bytes) */
92 #define SIO_REG_VID_CTRL 0xF0 /* VID control */
93 #define SIO_REG_VID_DATA 0xF1 /* VID data */
95 #define SIO_NCT6775_ID 0xb470
96 #define SIO_NCT6776_ID 0xc330
97 #define SIO_NCT6779_ID 0xc560
98 #define SIO_ID_MASK 0xFFF0
101 superio_outb(int ioreg, int reg, int val)
104 outb(val, ioreg + 1);
108 superio_inb(int ioreg, int reg)
111 return inb(ioreg + 1);
115 superio_select(int ioreg, int ld)
117 outb(SIO_REG_LDSEL, ioreg);
122 superio_enter(int ioreg)
125 * Try to reserve <ioreg> and <ioreg + 1> for exclusive access.
127 if (!request_muxed_region(ioreg, 2, DRVNAME))
137 superio_exit(int ioreg)
141 outb(0x02, ioreg + 1);
142 release_region(ioreg, 2);
149 #define IOREGION_ALIGNMENT (~7)
150 #define IOREGION_OFFSET 5
151 #define IOREGION_LENGTH 2
152 #define ADDR_REG_OFFSET 0
153 #define DATA_REG_OFFSET 1
155 #define NCT6775_REG_BANK 0x4E
156 #define NCT6775_REG_CONFIG 0x40
159 * Not currently used:
160 * REG_MAN_ID has the value 0x5ca3 for all supported chips.
161 * REG_CHIP_ID == 0x88/0xa1/0xc1 depending on chip model.
162 * REG_MAN_ID is at port 0x4f
163 * REG_CHIP_ID is at port 0x58
166 #define NUM_TEMP 10 /* Max number of temp attribute sets w/ limits*/
167 #define NUM_TEMP_FIXED 6 /* Max number of fixed temp attribute sets */
169 #define NUM_REG_ALARM 4 /* Max number of alarm registers */
171 /* Common and NCT6775 specific data */
173 /* Voltage min/max registers for nr=7..14 are in bank 5 */
175 static const u16 NCT6775_REG_IN_MAX[] = {
176 0x2b, 0x2d, 0x2f, 0x31, 0x33, 0x35, 0x37, 0x554, 0x556, 0x558, 0x55a,
177 0x55c, 0x55e, 0x560, 0x562 };
178 static const u16 NCT6775_REG_IN_MIN[] = {
179 0x2c, 0x2e, 0x30, 0x32, 0x34, 0x36, 0x38, 0x555, 0x557, 0x559, 0x55b,
180 0x55d, 0x55f, 0x561, 0x563 };
181 static const u16 NCT6775_REG_IN[] = {
182 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x550, 0x551, 0x552
185 #define NCT6775_REG_VBAT 0x5D
186 #define NCT6775_REG_DIODE 0x5E
188 #define NCT6775_REG_FANDIV1 0x506
189 #define NCT6775_REG_FANDIV2 0x507
191 #define NCT6775_REG_CR_FAN_DEBOUNCE 0xf0
193 static const u16 NCT6775_REG_ALARM[NUM_REG_ALARM] = { 0x459, 0x45A, 0x45B };
195 /* 0..15 voltages, 16..23 fans, 24..31 temperatures */
197 static const s8 NCT6775_ALARM_BITS[] = {
198 0, 1, 2, 3, 8, 21, 20, 16, /* in0.. in7 */
199 17, -1, -1, -1, -1, -1, -1, /* in8..in14 */
201 6, 7, 11, 10, 23, /* fan1..fan5 */
202 -1, -1, -1, /* unused */
203 4, 5, 13, -1, -1, -1, /* temp1..temp6 */
204 12, -1 }; /* intrusion0, intrusion1 */
206 #define FAN_ALARM_BASE 16
207 #define TEMP_ALARM_BASE 24
208 #define INTRUSION_ALARM_BASE 30
210 static const u8 NCT6775_REG_CR_CASEOPEN_CLR[] = { 0xe6, 0xee };
211 static const u8 NCT6775_CR_CASEOPEN_CLR_MASK[] = { 0x20, 0x01 };
213 /* DC or PWM output fan configuration */
214 static const u8 NCT6775_REG_PWM_MODE[] = { 0x04, 0x04, 0x12 };
215 static const u8 NCT6775_PWM_MODE_MASK[] = { 0x01, 0x02, 0x01 };
217 /* Advanced Fan control, some values are common for all fans */
219 static const u16 NCT6775_REG_FAN_PULSES[] = { 0x641, 0x642, 0x643, 0x644, 0 };
221 static const u16 NCT6775_REG_TARGET[] = { 0x101, 0x201, 0x301, 0x801, 0x901 };
222 static const u16 NCT6775_REG_FAN_MODE[] = { 0x102, 0x202, 0x302, 0x802, 0x902 };
223 static const u16 NCT6775_REG_FAN_STEP_DOWN_TIME[] = {
224 0x103, 0x203, 0x303, 0x803, 0x903 };
225 static const u16 NCT6775_REG_FAN_STEP_UP_TIME[] = {
226 0x104, 0x204, 0x304, 0x804, 0x904 };
227 static const u16 NCT6775_REG_FAN_STOP_OUTPUT[] = {
228 0x105, 0x205, 0x305, 0x805, 0x905 };
229 static const u16 NCT6775_REG_FAN_START_OUTPUT[]
230 = { 0x106, 0x206, 0x306, 0x806, 0x906 };
231 static const u16 NCT6775_REG_FAN_STEP_ENABLE[] = {
232 0x120, 0x220, 0x320, 0x820, 0x920 };
233 static const u16 NCT6775_REG_FAN_MAX_OUTPUT[] = { 0x10a, 0x20a, 0x30a };
234 static const u16 NCT6775_REG_FAN_STEP_OUTPUT[] = { 0x10b, 0x20b, 0x30b };
236 static const u16 NCT6775_REG_FAN_STOP_TIME[] = {
237 0x107, 0x207, 0x307, 0x807, 0x907 };
238 static const u16 NCT6775_REG_PWM[] = { 0x109, 0x209, 0x309, 0x809, 0x909 };
239 static const u16 NCT6775_REG_PWM_READ[] = { 0x01, 0x03, 0x11, 0x13, 0x15 };
241 static const u16 NCT6775_REG_FAN[] = { 0x630, 0x632, 0x634, 0x636, 0x638 };
242 static const u16 NCT6775_REG_FAN_MIN[] = { 0x3b, 0x3c, 0x3d };
244 static const u16 NCT6775_REG_TEMP[] = {
245 0x27, 0x150, 0x250, 0x62b, 0x62c, 0x62d };
247 static const u16 NCT6775_REG_TEMP_CONFIG[] = {
248 0, 0x152, 0x252, 0x628, 0x629, 0x62A };
249 static const u16 NCT6775_REG_TEMP_HYST[] = {
250 0x3a, 0x153, 0x253, 0x673, 0x678, 0x67D };
251 static const u16 NCT6775_REG_TEMP_OVER[] = {
252 0x39, 0x155, 0x255, 0x672, 0x677, 0x67C };
254 static const u16 NCT6775_REG_TEMP_SOURCE[] = {
255 0x621, 0x622, 0x623, 0x624, 0x625, 0x626 };
257 static const u16 NCT6775_REG_TEMP_SEL[] = {
258 0x100, 0x200, 0x300, 0x800, 0x900 };
260 static const u16 NCT6775_REG_WEIGHT_TEMP_SEL[] = {
261 0x139, 0x239, 0x339, 0x839, 0x939 };
262 static const u16 NCT6775_REG_WEIGHT_TEMP_STEP[] = {
263 0x13a, 0x23a, 0x33a, 0x83a, 0x93a };
264 static const u16 NCT6775_REG_WEIGHT_TEMP_STEP_TOL[] = {
265 0x13b, 0x23b, 0x33b, 0x83b, 0x93b };
266 static const u16 NCT6775_REG_WEIGHT_DUTY_STEP[] = {
267 0x13c, 0x23c, 0x33c, 0x83c, 0x93c };
268 static const u16 NCT6775_REG_WEIGHT_TEMP_BASE[] = {
269 0x13d, 0x23d, 0x33d, 0x83d, 0x93d };
271 static const u16 NCT6775_REG_TEMP_OFFSET[] = { 0x454, 0x455, 0x456 };
273 static const u16 NCT6775_REG_AUTO_TEMP[] = {
274 0x121, 0x221, 0x321, 0x821, 0x921 };
275 static const u16 NCT6775_REG_AUTO_PWM[] = {
276 0x127, 0x227, 0x327, 0x827, 0x927 };
278 #define NCT6775_AUTO_TEMP(data, nr, p) ((data)->REG_AUTO_TEMP[nr] + (p))
279 #define NCT6775_AUTO_PWM(data, nr, p) ((data)->REG_AUTO_PWM[nr] + (p))
281 static const u16 NCT6775_REG_CRITICAL_ENAB[] = { 0x134, 0x234, 0x334 };
283 static const u16 NCT6775_REG_CRITICAL_TEMP[] = {
284 0x135, 0x235, 0x335, 0x835, 0x935 };
285 static const u16 NCT6775_REG_CRITICAL_TEMP_TOLERANCE[] = {
286 0x138, 0x238, 0x338, 0x838, 0x938 };
288 static const char *const nct6775_temp_label[] = {
302 "PCH_CHIP_CPU_MAX_TEMP",
312 static const u16 NCT6775_REG_TEMP_ALTERNATE[ARRAY_SIZE(nct6775_temp_label) - 1]
313 = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0x661, 0x662, 0x664 };
315 static const u16 NCT6775_REG_TEMP_CRIT[ARRAY_SIZE(nct6775_temp_label) - 1]
316 = { 0, 0, 0, 0, 0xa00, 0xa01, 0xa02, 0xa03, 0xa04, 0xa05, 0xa06,
319 /* NCT6776 specific data */
321 static const s8 NCT6776_ALARM_BITS[] = {
322 0, 1, 2, 3, 8, 21, 20, 16, /* in0.. in7 */
323 17, -1, -1, -1, -1, -1, -1, /* in8..in14 */
325 6, 7, 11, 10, 23, /* fan1..fan5 */
326 -1, -1, -1, /* unused */
327 4, 5, 13, -1, -1, -1, /* temp1..temp6 */
328 12, 9 }; /* intrusion0, intrusion1 */
330 static const u8 NCT6776_REG_PWM_MODE[] = { 0x04, 0, 0 };
331 static const u8 NCT6776_PWM_MODE_MASK[] = { 0x01, 0, 0 };
333 static const u16 NCT6776_REG_FAN_PULSES[] = { 0x644, 0x645, 0x646, 0, 0 };
334 static const u16 NCT6776_REG_FAN_MIN[] = { 0x63a, 0x63c, 0x63e, 0x640, 0x642 };
336 static const u16 NCT6776_REG_WEIGHT_DUTY_BASE[] = {
337 0x13e, 0x23e, 0x33e, 0x83e, 0x93e };
339 static const u16 NCT6776_REG_TEMP_CONFIG[11] = {
340 0x18, 0x152, 0x252, 0x628, 0x629, 0x62A };
342 static const char *const nct6776_temp_label[] = {
357 "PCH_CHIP_CPU_MAX_TEMP",
368 static const u16 NCT6776_REG_TEMP_ALTERNATE[ARRAY_SIZE(nct6776_temp_label) - 1]
369 = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0x401, 0x402, 0x404 };
371 static const u16 NCT6776_REG_TEMP_CRIT[ARRAY_SIZE(nct6776_temp_label) - 1]
372 = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0x709, 0x70a };
374 /* NCT6779 specific data */
376 static const u16 NCT6779_REG_IN[] = {
377 0x480, 0x481, 0x482, 0x483, 0x484, 0x485, 0x486, 0x487,
378 0x488, 0x489, 0x48a, 0x48b, 0x48c, 0x48d, 0x48e };
380 static const u16 NCT6779_REG_ALARM[NUM_REG_ALARM] = {
381 0x459, 0x45A, 0x45B, 0x568 };
383 static const s8 NCT6779_ALARM_BITS[] = {
384 0, 1, 2, 3, 8, 21, 20, 16, /* in0.. in7 */
385 17, 24, 25, 26, 27, 28, 29, /* in8..in14 */
387 6, 7, 11, 10, 23, /* fan1..fan5 */
388 -1, -1, -1, /* unused */
389 4, 5, 13, -1, -1, -1, /* temp1..temp6 */
390 12, 9 }; /* intrusion0, intrusion1 */
392 static const u16 NCT6779_REG_FAN_PULSES[] = {
393 0x644, 0x645, 0x646, 0x647, 0x648 };
395 static const u16 NCT6779_REG_FAN[] = { 0x4b0, 0x4b2, 0x4b4, 0x4b6, 0x4b8 };
397 static const u16 NCT6779_REG_TOLERANCE_H[] = {
398 0x10c, 0x20c, 0x30c, 0x80c, 0x90c };
400 static const u16 NCT6779_REG_CRITICAL_PWM_ENABLE[] = {
401 0x136, 0x236, 0x336, 0x836, 0x936 };
402 static const u16 NCT6779_REG_CRITICAL_PWM[] = {
403 0x137, 0x237, 0x337, 0x837, 0x937 };
405 static const u16 NCT6779_REG_TEMP[] = { 0x27, 0x150 };
406 static const u16 NCT6779_REG_TEMP_HYST[] = { 0x3a, 0x153, 0, 0, 0, 0 };
407 static const u16 NCT6779_REG_TEMP_OVER[] = { 0x39, 0x155, 0, 0, 0, 0 };
408 static const u16 NCT6779_REG_TEMP_OFFSET[] = {
409 0x454, 0x455, 0x456, 0x44a, 0x44b, 0x44c };
411 static const u16 NCT6779_REG_TEMP_CONFIG[11] = { 0x18, 0x152 };
413 static const char *const nct6779_temp_label[] = {
432 "PCH_CHIP_CPU_MAX_TEMP",
443 static const u16 NCT6779_REG_TEMP_ALTERNATE[ARRAY_SIZE(nct6779_temp_label) - 1]
444 = { 0x490, 0x491, 0x492, 0x493, 0x494, 0x495, 0, 0,
445 0, 0, 0, 0, 0, 0, 0, 0,
446 0, 0x400, 0x401, 0x402, 0x404, 0x405, 0x406, 0x407,
449 static const u16 NCT6779_REG_TEMP_CRIT[ARRAY_SIZE(nct6779_temp_label) - 1]
450 = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0x709, 0x70a };
452 static inline int reg_to_pwm_enable(int pwm, int mode)
454 if (mode == 0 && pwm == 255)
459 static inline int pwm_enable_to_reg(int mode)
470 /* 1 is DC mode, output in ms */
471 static inline unsigned int step_time_from_reg(u8 reg, u8 mode)
473 return mode ? 400 * reg : 100 * reg;
476 static inline u8 step_time_to_reg(unsigned int msec, u8 mode)
478 return SENSORS_LIMIT((mode ? (msec + 200) / 400 :
479 (msec + 50) / 100), 1, 255);
482 static unsigned int fan_from_reg8(u16 reg, unsigned int divreg)
484 if (reg == 0 || reg == 255)
486 return 1350000U / (reg << divreg);
489 static unsigned int fan_from_reg13(u16 reg, unsigned int divreg)
491 if ((reg & 0xff1f) == 0xff1f)
494 reg = (reg & 0x1f) | ((reg & 0xff00) >> 3);
499 return 1350000U / reg;
502 static unsigned int fan_from_reg16(u16 reg, unsigned int divreg)
504 if (reg == 0 || reg == 0xffff)
508 * Even though the registers are 16 bit wide, the fan divisor
511 return 1350000U / (reg << divreg);
514 static inline unsigned int
521 * Some of the voltage inputs have internal scaling, the tables below
522 * contain 8 (the ADC LSB in mV) * scaling factor * 100
524 static const u16 scale_in[15] = {
525 800, 800, 1600, 1600, 800, 800, 800, 1600, 1600, 800, 800, 800, 800,
529 static inline long in_from_reg(u8 reg, u8 nr)
531 return DIV_ROUND_CLOSEST(reg * scale_in[nr], 100);
534 static inline u8 in_to_reg(u32 val, u8 nr)
536 return SENSORS_LIMIT(DIV_ROUND_CLOSEST(val * 100, scale_in[nr]), 0,
541 * Data structures and manipulation thereof
544 struct nct6775_data {
545 int addr; /* IO base of hw monitor block */
549 struct device *hwmon_dev;
552 u16 reg_temp[4][NUM_TEMP]; /* 0=temp, 1=temp_over, 2=temp_hyst,
555 u8 temp_src[NUM_TEMP];
556 u16 reg_temp_config[NUM_TEMP];
557 const char * const *temp_label;
564 const s8 *ALARM_BITS;
567 const u16 *REG_IN_MINMAX[2];
569 const u16 *REG_TARGET;
571 const u16 *REG_FAN_MODE;
572 const u16 *REG_FAN_MIN;
573 const u16 *REG_FAN_TIME[3];
575 const u8 *REG_PWM_MODE;
576 const u8 *PWM_MODE_MASK;
578 const u16 *REG_PWM[7]; /* [0]=pwm, [1]=start_output, [2]=stop_output,
579 * [3]=max_output, [4]=step_output,
580 * [5]=weight_duty_step, [6]=weight_duty_base
582 const u16 *REG_PWM_READ;
584 const u16 *REG_AUTO_TEMP;
585 const u16 *REG_AUTO_PWM;
587 const u16 *REG_CRITICAL_TEMP;
588 const u16 *REG_CRITICAL_TEMP_TOLERANCE;
590 const u16 *REG_TEMP_SOURCE; /* temp register sources */
591 const u16 *REG_TEMP_SEL[3]; /* pwm temp:
592 * 0=base, 1=weight, 2=step enable
595 const u16 *REG_WEIGHT_TEMP[3]; /* 0=base, 1=hyst, 2=step */
597 const u16 *REG_TEMP_OFFSET;
599 const u16 *REG_ALARM;
601 unsigned int (*fan_from_reg)(u16 reg, unsigned int divreg);
602 unsigned int (*fan_from_reg_min)(u16 reg, unsigned int divreg);
604 struct mutex update_lock;
605 char valid; /* !=0 if following fields are valid */
606 unsigned long last_updated; /* In jiffies */
608 /* Register values */
609 u8 bank; /* current register bank */
610 u8 in_num; /* number of in inputs we have */
611 u8 in[15][3]; /* [0]=in, [1]=in_max, [2]=in_min */
616 u8 has_fan; /* some fan inputs can be disabled */
617 u8 has_fan_min; /* some fans don't have min register */
620 u8 temp_fixed_num; /* 3 or 6 */
621 u8 temp_type[NUM_TEMP_FIXED];
622 s8 temp_offset[NUM_TEMP_FIXED];
623 s16 temp[4][NUM_TEMP]; /* 0=temp, 1=temp_over, 2=temp_hyst,
627 u8 pwm_num; /* number of pwm */
629 u8 pwm_mode[5]; /* 1->DC variable voltage, 0->PWM variable duty cycle */
630 u8 pwm_enable[5]; /* 0->off
632 * 2->thermal cruise mode (also called SmartFan I)
633 * 3->fan speed cruise mode
635 * 5->enhanced variable thermal cruise (SmartFan IV)
637 u8 pwm[7][5]; /* [0]=pwm, [1]=start_output, [2]=stop_output,
638 * [3]=max_output, [4]=step_output,
639 * [5]=weight_duty_step, [6]=weight_duty_base
644 u8 fan_time[3][5]; /* 0 = stop_time, 1 = step_up, 2 = step_down */
646 /* Automatic fan speed control registers */
651 u8 pwm_temp_sel[2][5];
653 bool pwm_sel_enable[3][5];/* 0->stop_val, 1->weight, 2->step;
654 * false->off, true->on
656 u8 weight_temp[3][5]; /* 0->temp_step, 1->temp_step_tol,
668 struct nct6775_sio_data {
673 static bool is_word_sized(struct nct6775_data *data, u16 reg)
675 switch (data->kind) {
677 return (((reg & 0xff00) == 0x100 ||
678 (reg & 0xff00) == 0x200) &&
679 ((reg & 0x00ff) == 0x50 ||
680 (reg & 0x00ff) == 0x53 ||
681 (reg & 0x00ff) == 0x55)) ||
682 (reg & 0xfff0) == 0x630 ||
683 reg == 0x640 || reg == 0x642 ||
685 ((reg & 0xfff0) == 0x650 && (reg & 0x000f) >= 0x06) ||
686 reg == 0x73 || reg == 0x75 || reg == 0x77;
688 return (((reg & 0xff00) == 0x100 ||
689 (reg & 0xff00) == 0x200) &&
690 ((reg & 0x00ff) == 0x50 ||
691 (reg & 0x00ff) == 0x53 ||
692 (reg & 0x00ff) == 0x55)) ||
693 (reg & 0xfff0) == 0x630 ||
695 reg == 0x640 || reg == 0x642 ||
696 ((reg & 0xfff0) == 0x650 && (reg & 0x000f) >= 0x06) ||
697 reg == 0x73 || reg == 0x75 || reg == 0x77;
699 return reg == 0x150 || reg == 0x153 || reg == 0x155 ||
700 ((reg & 0xfff0) == 0x4b0 && (reg & 0x000f) < 0x09) ||
702 reg == 0x63a || reg == 0x63c || reg == 0x63e ||
703 reg == 0x640 || reg == 0x642 ||
704 reg == 0x73 || reg == 0x75 || reg == 0x77 || reg == 0x79 ||
711 * On older chips, only registers 0x50-0x5f are banked.
712 * On more recent chips, all registers are banked.
713 * Assume that is the case and set the bank number for each access.
714 * Cache the bank number so it only needs to be set if it changes.
716 static inline void nct6775_set_bank(struct nct6775_data *data, u16 reg)
719 if (data->bank != bank) {
720 outb_p(NCT6775_REG_BANK, data->addr + ADDR_REG_OFFSET);
721 outb_p(bank, data->addr + DATA_REG_OFFSET);
726 static u16 nct6775_read_value(struct nct6775_data *data, u16 reg)
728 int res, word_sized = is_word_sized(data, reg);
730 mutex_lock(&data->lock);
732 nct6775_set_bank(data, reg);
733 outb_p(reg & 0xff, data->addr + ADDR_REG_OFFSET);
734 res = inb_p(data->addr + DATA_REG_OFFSET);
736 outb_p((reg & 0xff) + 1,
737 data->addr + ADDR_REG_OFFSET);
738 res = (res << 8) + inb_p(data->addr + DATA_REG_OFFSET);
741 mutex_unlock(&data->lock);
745 static int nct6775_write_value(struct nct6775_data *data, u16 reg, u16 value)
747 int word_sized = is_word_sized(data, reg);
749 mutex_lock(&data->lock);
751 nct6775_set_bank(data, reg);
752 outb_p(reg & 0xff, data->addr + ADDR_REG_OFFSET);
754 outb_p(value >> 8, data->addr + DATA_REG_OFFSET);
755 outb_p((reg & 0xff) + 1,
756 data->addr + ADDR_REG_OFFSET);
758 outb_p(value & 0xff, data->addr + DATA_REG_OFFSET);
760 mutex_unlock(&data->lock);
764 /* We left-align 8-bit temperature values to make the code simpler */
765 static u16 nct6775_read_temp(struct nct6775_data *data, u16 reg)
769 res = nct6775_read_value(data, reg);
770 if (!is_word_sized(data, reg))
776 static int nct6775_write_temp(struct nct6775_data *data, u16 reg, u16 value)
778 if (!is_word_sized(data, reg))
780 return nct6775_write_value(data, reg, value);
783 /* This function assumes that the caller holds data->update_lock */
784 static void nct6775_write_fan_div(struct nct6775_data *data, int nr)
790 reg = (nct6775_read_value(data, NCT6775_REG_FANDIV1) & 0x70)
791 | (data->fan_div[0] & 0x7);
792 nct6775_write_value(data, NCT6775_REG_FANDIV1, reg);
795 reg = (nct6775_read_value(data, NCT6775_REG_FANDIV1) & 0x7)
796 | ((data->fan_div[1] << 4) & 0x70);
797 nct6775_write_value(data, NCT6775_REG_FANDIV1, reg);
800 reg = (nct6775_read_value(data, NCT6775_REG_FANDIV2) & 0x70)
801 | (data->fan_div[2] & 0x7);
802 nct6775_write_value(data, NCT6775_REG_FANDIV2, reg);
805 reg = (nct6775_read_value(data, NCT6775_REG_FANDIV2) & 0x7)
806 | ((data->fan_div[3] << 4) & 0x70);
807 nct6775_write_value(data, NCT6775_REG_FANDIV2, reg);
812 static void nct6775_write_fan_div_common(struct device *dev,
813 struct nct6775_data *data, int nr)
815 if (data->kind == nct6775)
816 nct6775_write_fan_div(data, nr);
819 static void nct6775_update_fan_div(struct nct6775_data *data)
823 i = nct6775_read_value(data, NCT6775_REG_FANDIV1);
824 data->fan_div[0] = i & 0x7;
825 data->fan_div[1] = (i & 0x70) >> 4;
826 i = nct6775_read_value(data, NCT6775_REG_FANDIV2);
827 data->fan_div[2] = i & 0x7;
828 if (data->has_fan & (1<<3))
829 data->fan_div[3] = (i & 0x70) >> 4;
832 static void nct6775_update_fan_div_common(struct device *dev,
833 struct nct6775_data *data)
835 if (data->kind == nct6775)
836 nct6775_update_fan_div(data);
839 static void nct6775_update_pwm(struct device *dev)
841 struct nct6775_data *data = dev_get_drvdata(dev);
846 for (i = 0; i < data->pwm_num; i++) {
847 if (!(data->has_pwm & (1 << i)))
850 duty_is_dc = data->REG_PWM_MODE[i] &&
851 (nct6775_read_value(data, data->REG_PWM_MODE[i])
852 & data->PWM_MODE_MASK[i]);
853 data->pwm_mode[i] = duty_is_dc;
855 fanmodecfg = nct6775_read_value(data, data->REG_FAN_MODE[i]);
856 for (j = 0; j < ARRAY_SIZE(data->REG_PWM); j++) {
857 if (data->REG_PWM[j] && data->REG_PWM[j][i]) {
859 = nct6775_read_value(data,
860 data->REG_PWM[j][i]);
864 data->pwm_enable[i] = reg_to_pwm_enable(data->pwm[0][i],
865 (fanmodecfg >> 4) & 7);
867 data->tolerance[i][0] = fanmodecfg & 0x0f;
868 if (data->kind == nct6779) {
869 reg = nct6775_read_value(data,
870 NCT6779_REG_TOLERANCE_H[i]);
871 data->tolerance[i][0] |= (reg & 0x70) >> 1;
873 data->tolerance[i][1] =
874 nct6775_read_value(data,
875 data->REG_CRITICAL_TEMP_TOLERANCE[i]);
877 data->pwm_sel_enable[0][i] =
878 nct6775_read_value(data, data->REG_TEMP_SEL[0][i])
880 data->pwm_sel_enable[1][i] =
881 nct6775_read_value(data, data->REG_TEMP_SEL[1][i])
883 data->pwm_sel_enable[2][i] =
884 nct6775_read_value(data, data->REG_TEMP_SEL[2][i])
888 for (j = 0; j < 2; j++) {
889 reg = nct6775_read_value(data,
890 data->REG_TEMP_SEL[j][i]);
891 data->pwm_temp_sel[j][i] = reg & 0x1f;
893 /* Weight temp data */
894 for (j = 0; j < 3; j++) {
895 data->weight_temp[j][i]
896 = nct6775_read_value(data,
897 data->REG_WEIGHT_TEMP[j][i]);
902 static void nct6775_update_pwm_limits(struct device *dev)
904 struct nct6775_data *data = dev_get_drvdata(dev);
908 for (i = 0; i < data->pwm_num; i++) {
909 if (!(data->has_pwm & (1 << i)))
912 for (j = 0; j < 3; j++) {
913 data->fan_time[j][i] =
914 nct6775_read_value(data, data->REG_FAN_TIME[j][i]);
917 data->target_temp[i] =
918 nct6775_read_value(data, data->REG_TARGET[i]) &
919 (data->pwm_mode[i] ? 0xff : 0x7f);
921 for (j = 0; j < data->auto_pwm_num; j++) {
922 data->auto_pwm[i][j] =
923 nct6775_read_value(data,
924 NCT6775_AUTO_PWM(data, i, j));
925 data->auto_temp[i][j] =
926 nct6775_read_value(data,
927 NCT6775_AUTO_TEMP(data, i, j));
930 /* critical auto_pwm temperature data */
931 data->auto_temp[i][data->auto_pwm_num] =
932 nct6775_read_value(data, data->REG_CRITICAL_TEMP[i]);
934 switch (data->kind) {
936 reg = nct6775_read_value(data,
937 NCT6775_REG_CRITICAL_ENAB[i]);
938 data->auto_pwm[i][data->auto_pwm_num] =
939 (reg & 0x02) ? 0xff : 0x00;
942 data->auto_pwm[i][data->auto_pwm_num] = 0xff;
945 reg = nct6775_read_value(data,
946 NCT6779_REG_CRITICAL_PWM_ENABLE[i]);
948 data->auto_pwm[i][data->auto_pwm_num] =
949 nct6775_read_value(data,
950 NCT6779_REG_CRITICAL_PWM[i]);
952 data->auto_pwm[i][data->auto_pwm_num] = 0xff;
958 static struct nct6775_data *nct6775_update_device(struct device *dev)
960 struct nct6775_data *data = dev_get_drvdata(dev);
963 mutex_lock(&data->update_lock);
965 if (time_after(jiffies, data->last_updated + HZ + HZ/2)
967 /* Fan clock dividers */
968 nct6775_update_fan_div_common(dev, data);
970 /* Measured voltages and limits */
971 for (i = 0; i < data->in_num; i++) {
972 if (!(data->have_in & (1 << i)))
975 data->in[i][0] = nct6775_read_value(data,
977 data->in[i][1] = nct6775_read_value(data,
978 data->REG_IN_MINMAX[0][i]);
979 data->in[i][2] = nct6775_read_value(data,
980 data->REG_IN_MINMAX[1][i]);
983 /* Measured fan speeds and limits */
984 for (i = 0; i < 5; i++) {
987 if (!(data->has_fan & (1 << i)))
990 reg = nct6775_read_value(data, data->REG_FAN[i]);
991 data->rpm[i] = data->fan_from_reg(reg,
994 if (data->has_fan_min & (1 << i))
995 data->fan_min[i] = nct6775_read_value(data,
996 data->REG_FAN_MIN[i]);
999 * If we failed to measure the fan speed and clock
1000 * divider can be increased, let's try that for next
1003 if (data->has_fan_div && (reg >= 0xff || reg == 0x00)
1004 && data->fan_div[i] < 0x07) {
1006 "Increasing fan%d clock divider from %u to %u\n",
1007 i + 1, div_from_reg(data->fan_div[i]),
1008 div_from_reg(data->fan_div[i] + 1));
1010 nct6775_write_fan_div_common(dev, data, i);
1011 /* Preserve min limit if possible */
1012 if ((data->has_fan_min & (1 << i))
1013 && data->fan_min[i] >= 2
1014 && data->fan_min[i] != 255)
1015 nct6775_write_value(data,
1016 data->REG_FAN_MIN[i],
1017 (data->fan_min[i] /= 2));
1021 nct6775_update_pwm(dev);
1022 nct6775_update_pwm_limits(dev);
1024 /* Measured temperatures and limits */
1025 for (i = 0; i < NUM_TEMP; i++) {
1026 if (!(data->have_temp & (1 << i)))
1028 for (j = 0; j < 4; j++) {
1029 if (data->reg_temp[j][i])
1031 = nct6775_read_temp(data,
1032 data->reg_temp[j][i]);
1034 if (!(data->have_temp_fixed & (1 << i)))
1036 data->temp_offset[i]
1037 = nct6775_read_value(data, data->REG_TEMP_OFFSET[i]);
1041 for (i = 0; i < NUM_REG_ALARM; i++) {
1043 if (!data->REG_ALARM[i])
1045 alarm = nct6775_read_value(data, data->REG_ALARM[i]);
1046 data->alarms |= ((u64)alarm) << (i << 3);
1049 data->last_updated = jiffies;
1053 mutex_unlock(&data->update_lock);
1058 * Sysfs callback functions
1061 show_in_reg(struct device *dev, struct device_attribute *attr, char *buf)
1063 struct nct6775_data *data = nct6775_update_device(dev);
1064 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
1066 int index = sattr->index;
1067 return sprintf(buf, "%ld\n", in_from_reg(data->in[nr][index], nr));
1071 store_in_reg(struct device *dev, struct device_attribute *attr, const char *buf,
1074 struct nct6775_data *data = dev_get_drvdata(dev);
1075 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
1077 int index = sattr->index;
1079 int err = kstrtoul(buf, 10, &val);
1082 mutex_lock(&data->update_lock);
1083 data->in[nr][index] = in_to_reg(val, nr);
1084 nct6775_write_value(data, data->REG_IN_MINMAX[index-1][nr],
1085 data->in[nr][index]);
1086 mutex_unlock(&data->update_lock);
1091 show_alarm(struct device *dev, struct device_attribute *attr, char *buf)
1093 struct nct6775_data *data = nct6775_update_device(dev);
1094 struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr);
1095 int nr = data->ALARM_BITS[sattr->index];
1096 return sprintf(buf, "%u\n",
1097 (unsigned int)((data->alarms >> nr) & 0x01));
1100 static SENSOR_DEVICE_ATTR_2(in0_input, S_IRUGO, show_in_reg, NULL, 0, 0);
1101 static SENSOR_DEVICE_ATTR_2(in1_input, S_IRUGO, show_in_reg, NULL, 1, 0);
1102 static SENSOR_DEVICE_ATTR_2(in2_input, S_IRUGO, show_in_reg, NULL, 2, 0);
1103 static SENSOR_DEVICE_ATTR_2(in3_input, S_IRUGO, show_in_reg, NULL, 3, 0);
1104 static SENSOR_DEVICE_ATTR_2(in4_input, S_IRUGO, show_in_reg, NULL, 4, 0);
1105 static SENSOR_DEVICE_ATTR_2(in5_input, S_IRUGO, show_in_reg, NULL, 5, 0);
1106 static SENSOR_DEVICE_ATTR_2(in6_input, S_IRUGO, show_in_reg, NULL, 6, 0);
1107 static SENSOR_DEVICE_ATTR_2(in7_input, S_IRUGO, show_in_reg, NULL, 7, 0);
1108 static SENSOR_DEVICE_ATTR_2(in8_input, S_IRUGO, show_in_reg, NULL, 8, 0);
1109 static SENSOR_DEVICE_ATTR_2(in9_input, S_IRUGO, show_in_reg, NULL, 9, 0);
1110 static SENSOR_DEVICE_ATTR_2(in10_input, S_IRUGO, show_in_reg, NULL, 10, 0);
1111 static SENSOR_DEVICE_ATTR_2(in11_input, S_IRUGO, show_in_reg, NULL, 11, 0);
1112 static SENSOR_DEVICE_ATTR_2(in12_input, S_IRUGO, show_in_reg, NULL, 12, 0);
1113 static SENSOR_DEVICE_ATTR_2(in13_input, S_IRUGO, show_in_reg, NULL, 13, 0);
1114 static SENSOR_DEVICE_ATTR_2(in14_input, S_IRUGO, show_in_reg, NULL, 14, 0);
1116 static SENSOR_DEVICE_ATTR(in0_alarm, S_IRUGO, show_alarm, NULL, 0);
1117 static SENSOR_DEVICE_ATTR(in1_alarm, S_IRUGO, show_alarm, NULL, 1);
1118 static SENSOR_DEVICE_ATTR(in2_alarm, S_IRUGO, show_alarm, NULL, 2);
1119 static SENSOR_DEVICE_ATTR(in3_alarm, S_IRUGO, show_alarm, NULL, 3);
1120 static SENSOR_DEVICE_ATTR(in4_alarm, S_IRUGO, show_alarm, NULL, 4);
1121 static SENSOR_DEVICE_ATTR(in5_alarm, S_IRUGO, show_alarm, NULL, 5);
1122 static SENSOR_DEVICE_ATTR(in6_alarm, S_IRUGO, show_alarm, NULL, 6);
1123 static SENSOR_DEVICE_ATTR(in7_alarm, S_IRUGO, show_alarm, NULL, 7);
1124 static SENSOR_DEVICE_ATTR(in8_alarm, S_IRUGO, show_alarm, NULL, 8);
1125 static SENSOR_DEVICE_ATTR(in9_alarm, S_IRUGO, show_alarm, NULL, 9);
1126 static SENSOR_DEVICE_ATTR(in10_alarm, S_IRUGO, show_alarm, NULL, 10);
1127 static SENSOR_DEVICE_ATTR(in11_alarm, S_IRUGO, show_alarm, NULL, 11);
1128 static SENSOR_DEVICE_ATTR(in12_alarm, S_IRUGO, show_alarm, NULL, 12);
1129 static SENSOR_DEVICE_ATTR(in13_alarm, S_IRUGO, show_alarm, NULL, 13);
1130 static SENSOR_DEVICE_ATTR(in14_alarm, S_IRUGO, show_alarm, NULL, 14);
1132 static SENSOR_DEVICE_ATTR_2(in0_min, S_IWUSR | S_IRUGO, show_in_reg,
1133 store_in_reg, 0, 1);
1134 static SENSOR_DEVICE_ATTR_2(in1_min, S_IWUSR | S_IRUGO, show_in_reg,
1135 store_in_reg, 1, 1);
1136 static SENSOR_DEVICE_ATTR_2(in2_min, S_IWUSR | S_IRUGO, show_in_reg,
1137 store_in_reg, 2, 1);
1138 static SENSOR_DEVICE_ATTR_2(in3_min, S_IWUSR | S_IRUGO, show_in_reg,
1139 store_in_reg, 3, 1);
1140 static SENSOR_DEVICE_ATTR_2(in4_min, S_IWUSR | S_IRUGO, show_in_reg,
1141 store_in_reg, 4, 1);
1142 static SENSOR_DEVICE_ATTR_2(in5_min, S_IWUSR | S_IRUGO, show_in_reg,
1143 store_in_reg, 5, 1);
1144 static SENSOR_DEVICE_ATTR_2(in6_min, S_IWUSR | S_IRUGO, show_in_reg,
1145 store_in_reg, 6, 1);
1146 static SENSOR_DEVICE_ATTR_2(in7_min, S_IWUSR | S_IRUGO, show_in_reg,
1147 store_in_reg, 7, 1);
1148 static SENSOR_DEVICE_ATTR_2(in8_min, S_IWUSR | S_IRUGO, show_in_reg,
1149 store_in_reg, 8, 1);
1150 static SENSOR_DEVICE_ATTR_2(in9_min, S_IWUSR | S_IRUGO, show_in_reg,
1151 store_in_reg, 9, 1);
1152 static SENSOR_DEVICE_ATTR_2(in10_min, S_IWUSR | S_IRUGO, show_in_reg,
1153 store_in_reg, 10, 1);
1154 static SENSOR_DEVICE_ATTR_2(in11_min, S_IWUSR | S_IRUGO, show_in_reg,
1155 store_in_reg, 11, 1);
1156 static SENSOR_DEVICE_ATTR_2(in12_min, S_IWUSR | S_IRUGO, show_in_reg,
1157 store_in_reg, 12, 1);
1158 static SENSOR_DEVICE_ATTR_2(in13_min, S_IWUSR | S_IRUGO, show_in_reg,
1159 store_in_reg, 13, 1);
1160 static SENSOR_DEVICE_ATTR_2(in14_min, S_IWUSR | S_IRUGO, show_in_reg,
1161 store_in_reg, 14, 1);
1163 static SENSOR_DEVICE_ATTR_2(in0_max, S_IWUSR | S_IRUGO, show_in_reg,
1164 store_in_reg, 0, 2);
1165 static SENSOR_DEVICE_ATTR_2(in1_max, S_IWUSR | S_IRUGO, show_in_reg,
1166 store_in_reg, 1, 2);
1167 static SENSOR_DEVICE_ATTR_2(in2_max, S_IWUSR | S_IRUGO, show_in_reg,
1168 store_in_reg, 2, 2);
1169 static SENSOR_DEVICE_ATTR_2(in3_max, S_IWUSR | S_IRUGO, show_in_reg,
1170 store_in_reg, 3, 2);
1171 static SENSOR_DEVICE_ATTR_2(in4_max, S_IWUSR | S_IRUGO, show_in_reg,
1172 store_in_reg, 4, 2);
1173 static SENSOR_DEVICE_ATTR_2(in5_max, S_IWUSR | S_IRUGO, show_in_reg,
1174 store_in_reg, 5, 2);
1175 static SENSOR_DEVICE_ATTR_2(in6_max, S_IWUSR | S_IRUGO, show_in_reg,
1176 store_in_reg, 6, 2);
1177 static SENSOR_DEVICE_ATTR_2(in7_max, S_IWUSR | S_IRUGO, show_in_reg,
1178 store_in_reg, 7, 2);
1179 static SENSOR_DEVICE_ATTR_2(in8_max, S_IWUSR | S_IRUGO, show_in_reg,
1180 store_in_reg, 8, 2);
1181 static SENSOR_DEVICE_ATTR_2(in9_max, S_IWUSR | S_IRUGO, show_in_reg,
1182 store_in_reg, 9, 2);
1183 static SENSOR_DEVICE_ATTR_2(in10_max, S_IWUSR | S_IRUGO, show_in_reg,
1184 store_in_reg, 10, 2);
1185 static SENSOR_DEVICE_ATTR_2(in11_max, S_IWUSR | S_IRUGO, show_in_reg,
1186 store_in_reg, 11, 2);
1187 static SENSOR_DEVICE_ATTR_2(in12_max, S_IWUSR | S_IRUGO, show_in_reg,
1188 store_in_reg, 12, 2);
1189 static SENSOR_DEVICE_ATTR_2(in13_max, S_IWUSR | S_IRUGO, show_in_reg,
1190 store_in_reg, 13, 2);
1191 static SENSOR_DEVICE_ATTR_2(in14_max, S_IWUSR | S_IRUGO, show_in_reg,
1192 store_in_reg, 14, 2);
1194 static struct attribute *nct6775_attributes_in[15][5] = {
1196 &sensor_dev_attr_in0_input.dev_attr.attr,
1197 &sensor_dev_attr_in0_min.dev_attr.attr,
1198 &sensor_dev_attr_in0_max.dev_attr.attr,
1199 &sensor_dev_attr_in0_alarm.dev_attr.attr,
1203 &sensor_dev_attr_in1_input.dev_attr.attr,
1204 &sensor_dev_attr_in1_min.dev_attr.attr,
1205 &sensor_dev_attr_in1_max.dev_attr.attr,
1206 &sensor_dev_attr_in1_alarm.dev_attr.attr,
1210 &sensor_dev_attr_in2_input.dev_attr.attr,
1211 &sensor_dev_attr_in2_min.dev_attr.attr,
1212 &sensor_dev_attr_in2_max.dev_attr.attr,
1213 &sensor_dev_attr_in2_alarm.dev_attr.attr,
1217 &sensor_dev_attr_in3_input.dev_attr.attr,
1218 &sensor_dev_attr_in3_min.dev_attr.attr,
1219 &sensor_dev_attr_in3_max.dev_attr.attr,
1220 &sensor_dev_attr_in3_alarm.dev_attr.attr,
1224 &sensor_dev_attr_in4_input.dev_attr.attr,
1225 &sensor_dev_attr_in4_min.dev_attr.attr,
1226 &sensor_dev_attr_in4_max.dev_attr.attr,
1227 &sensor_dev_attr_in4_alarm.dev_attr.attr,
1231 &sensor_dev_attr_in5_input.dev_attr.attr,
1232 &sensor_dev_attr_in5_min.dev_attr.attr,
1233 &sensor_dev_attr_in5_max.dev_attr.attr,
1234 &sensor_dev_attr_in5_alarm.dev_attr.attr,
1238 &sensor_dev_attr_in6_input.dev_attr.attr,
1239 &sensor_dev_attr_in6_min.dev_attr.attr,
1240 &sensor_dev_attr_in6_max.dev_attr.attr,
1241 &sensor_dev_attr_in6_alarm.dev_attr.attr,
1245 &sensor_dev_attr_in7_input.dev_attr.attr,
1246 &sensor_dev_attr_in7_min.dev_attr.attr,
1247 &sensor_dev_attr_in7_max.dev_attr.attr,
1248 &sensor_dev_attr_in7_alarm.dev_attr.attr,
1252 &sensor_dev_attr_in8_input.dev_attr.attr,
1253 &sensor_dev_attr_in8_min.dev_attr.attr,
1254 &sensor_dev_attr_in8_max.dev_attr.attr,
1255 &sensor_dev_attr_in8_alarm.dev_attr.attr,
1259 &sensor_dev_attr_in9_input.dev_attr.attr,
1260 &sensor_dev_attr_in9_min.dev_attr.attr,
1261 &sensor_dev_attr_in9_max.dev_attr.attr,
1262 &sensor_dev_attr_in9_alarm.dev_attr.attr,
1266 &sensor_dev_attr_in10_input.dev_attr.attr,
1267 &sensor_dev_attr_in10_min.dev_attr.attr,
1268 &sensor_dev_attr_in10_max.dev_attr.attr,
1269 &sensor_dev_attr_in10_alarm.dev_attr.attr,
1273 &sensor_dev_attr_in11_input.dev_attr.attr,
1274 &sensor_dev_attr_in11_min.dev_attr.attr,
1275 &sensor_dev_attr_in11_max.dev_attr.attr,
1276 &sensor_dev_attr_in11_alarm.dev_attr.attr,
1280 &sensor_dev_attr_in12_input.dev_attr.attr,
1281 &sensor_dev_attr_in12_min.dev_attr.attr,
1282 &sensor_dev_attr_in12_max.dev_attr.attr,
1283 &sensor_dev_attr_in12_alarm.dev_attr.attr,
1287 &sensor_dev_attr_in13_input.dev_attr.attr,
1288 &sensor_dev_attr_in13_min.dev_attr.attr,
1289 &sensor_dev_attr_in13_max.dev_attr.attr,
1290 &sensor_dev_attr_in13_alarm.dev_attr.attr,
1294 &sensor_dev_attr_in14_input.dev_attr.attr,
1295 &sensor_dev_attr_in14_min.dev_attr.attr,
1296 &sensor_dev_attr_in14_max.dev_attr.attr,
1297 &sensor_dev_attr_in14_alarm.dev_attr.attr,
1302 static const struct attribute_group nct6775_group_in[15] = {
1303 { .attrs = nct6775_attributes_in[0] },
1304 { .attrs = nct6775_attributes_in[1] },
1305 { .attrs = nct6775_attributes_in[2] },
1306 { .attrs = nct6775_attributes_in[3] },
1307 { .attrs = nct6775_attributes_in[4] },
1308 { .attrs = nct6775_attributes_in[5] },
1309 { .attrs = nct6775_attributes_in[6] },
1310 { .attrs = nct6775_attributes_in[7] },
1311 { .attrs = nct6775_attributes_in[8] },
1312 { .attrs = nct6775_attributes_in[9] },
1313 { .attrs = nct6775_attributes_in[10] },
1314 { .attrs = nct6775_attributes_in[11] },
1315 { .attrs = nct6775_attributes_in[12] },
1316 { .attrs = nct6775_attributes_in[13] },
1317 { .attrs = nct6775_attributes_in[14] },
1321 show_fan(struct device *dev, struct device_attribute *attr, char *buf)
1323 struct nct6775_data *data = nct6775_update_device(dev);
1324 struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr);
1325 int nr = sattr->index;
1326 return sprintf(buf, "%d\n", data->rpm[nr]);
1330 show_fan_min(struct device *dev, struct device_attribute *attr, char *buf)
1332 struct nct6775_data *data = nct6775_update_device(dev);
1333 struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr);
1334 int nr = sattr->index;
1335 return sprintf(buf, "%d\n",
1336 data->fan_from_reg_min(data->fan_min[nr],
1337 data->fan_div[nr]));
1341 show_fan_div(struct device *dev, struct device_attribute *attr, char *buf)
1343 struct nct6775_data *data = nct6775_update_device(dev);
1344 struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr);
1345 int nr = sattr->index;
1346 return sprintf(buf, "%u\n", div_from_reg(data->fan_div[nr]));
1350 store_fan_min(struct device *dev, struct device_attribute *attr,
1351 const char *buf, size_t count)
1353 struct nct6775_data *data = dev_get_drvdata(dev);
1354 struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr);
1355 int nr = sattr->index;
1361 err = kstrtoul(buf, 10, &val);
1365 mutex_lock(&data->update_lock);
1366 if (!data->has_fan_div) {
1367 /* NCT6776F or NCT6779D; we know this is a 13 bit register */
1373 val = 1350000U / val;
1374 val = (val & 0x1f) | ((val << 3) & 0xff00);
1376 data->fan_min[nr] = val;
1377 goto write_min; /* Leave fan divider alone */
1380 /* No min limit, alarm disabled */
1381 data->fan_min[nr] = 255;
1382 new_div = data->fan_div[nr]; /* No change */
1383 dev_info(dev, "fan%u low limit and alarm disabled\n", nr + 1);
1386 reg = 1350000U / val;
1387 if (reg >= 128 * 255) {
1389 * Speed below this value cannot possibly be represented,
1390 * even with the highest divider (128)
1392 data->fan_min[nr] = 254;
1393 new_div = 7; /* 128 == (1 << 7) */
1395 "fan%u low limit %lu below minimum %u, set to minimum\n",
1396 nr + 1, val, data->fan_from_reg_min(254, 7));
1399 * Speed above this value cannot possibly be represented,
1400 * even with the lowest divider (1)
1402 data->fan_min[nr] = 1;
1403 new_div = 0; /* 1 == (1 << 0) */
1405 "fan%u low limit %lu above maximum %u, set to maximum\n",
1406 nr + 1, val, data->fan_from_reg_min(1, 0));
1409 * Automatically pick the best divider, i.e. the one such
1410 * that the min limit will correspond to a register value
1411 * in the 96..192 range
1414 while (reg > 192 && new_div < 7) {
1418 data->fan_min[nr] = reg;
1423 * Write both the fan clock divider (if it changed) and the new
1424 * fan min (unconditionally)
1426 if (new_div != data->fan_div[nr]) {
1427 dev_dbg(dev, "fan%u clock divider changed from %u to %u\n",
1428 nr + 1, div_from_reg(data->fan_div[nr]),
1429 div_from_reg(new_div));
1430 data->fan_div[nr] = new_div;
1431 nct6775_write_fan_div_common(dev, data, nr);
1432 /* Give the chip time to sample a new speed value */
1433 data->last_updated = jiffies;
1437 nct6775_write_value(data, data->REG_FAN_MIN[nr], data->fan_min[nr]);
1438 mutex_unlock(&data->update_lock);
1443 static struct sensor_device_attribute sda_fan_input[] = {
1444 SENSOR_ATTR(fan1_input, S_IRUGO, show_fan, NULL, 0),
1445 SENSOR_ATTR(fan2_input, S_IRUGO, show_fan, NULL, 1),
1446 SENSOR_ATTR(fan3_input, S_IRUGO, show_fan, NULL, 2),
1447 SENSOR_ATTR(fan4_input, S_IRUGO, show_fan, NULL, 3),
1448 SENSOR_ATTR(fan5_input, S_IRUGO, show_fan, NULL, 4),
1451 static struct sensor_device_attribute sda_fan_alarm[] = {
1452 SENSOR_ATTR(fan1_alarm, S_IRUGO, show_alarm, NULL, FAN_ALARM_BASE),
1453 SENSOR_ATTR(fan2_alarm, S_IRUGO, show_alarm, NULL, FAN_ALARM_BASE + 1),
1454 SENSOR_ATTR(fan3_alarm, S_IRUGO, show_alarm, NULL, FAN_ALARM_BASE + 2),
1455 SENSOR_ATTR(fan4_alarm, S_IRUGO, show_alarm, NULL, FAN_ALARM_BASE + 3),
1456 SENSOR_ATTR(fan5_alarm, S_IRUGO, show_alarm, NULL, FAN_ALARM_BASE + 4),
1459 static struct sensor_device_attribute sda_fan_min[] = {
1460 SENSOR_ATTR(fan1_min, S_IWUSR | S_IRUGO, show_fan_min,
1462 SENSOR_ATTR(fan2_min, S_IWUSR | S_IRUGO, show_fan_min,
1464 SENSOR_ATTR(fan3_min, S_IWUSR | S_IRUGO, show_fan_min,
1466 SENSOR_ATTR(fan4_min, S_IWUSR | S_IRUGO, show_fan_min,
1468 SENSOR_ATTR(fan5_min, S_IWUSR | S_IRUGO, show_fan_min,
1472 static struct sensor_device_attribute sda_fan_div[] = {
1473 SENSOR_ATTR(fan1_div, S_IRUGO, show_fan_div, NULL, 0),
1474 SENSOR_ATTR(fan2_div, S_IRUGO, show_fan_div, NULL, 1),
1475 SENSOR_ATTR(fan3_div, S_IRUGO, show_fan_div, NULL, 2),
1476 SENSOR_ATTR(fan4_div, S_IRUGO, show_fan_div, NULL, 3),
1477 SENSOR_ATTR(fan5_div, S_IRUGO, show_fan_div, NULL, 4),
1481 show_temp_label(struct device *dev, struct device_attribute *attr, char *buf)
1483 struct nct6775_data *data = nct6775_update_device(dev);
1484 struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr);
1485 int nr = sattr->index;
1486 return sprintf(buf, "%s\n", data->temp_label[data->temp_src[nr]]);
1490 show_temp(struct device *dev, struct device_attribute *attr, char *buf)
1492 struct nct6775_data *data = nct6775_update_device(dev);
1493 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
1495 int index = sattr->index;
1497 return sprintf(buf, "%d\n", LM75_TEMP_FROM_REG(data->temp[index][nr]));
1501 store_temp(struct device *dev, struct device_attribute *attr, const char *buf,
1504 struct nct6775_data *data = dev_get_drvdata(dev);
1505 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
1507 int index = sattr->index;
1511 err = kstrtol(buf, 10, &val);
1515 mutex_lock(&data->update_lock);
1516 data->temp[index][nr] = LM75_TEMP_TO_REG(val);
1517 nct6775_write_temp(data, data->reg_temp[index][nr],
1518 data->temp[index][nr]);
1519 mutex_unlock(&data->update_lock);
1524 show_temp_offset(struct device *dev, struct device_attribute *attr, char *buf)
1526 struct nct6775_data *data = nct6775_update_device(dev);
1527 struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr);
1529 return sprintf(buf, "%d\n", data->temp_offset[sattr->index] * 1000);
1533 store_temp_offset(struct device *dev, struct device_attribute *attr,
1534 const char *buf, size_t count)
1536 struct nct6775_data *data = dev_get_drvdata(dev);
1537 struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr);
1538 int nr = sattr->index;
1542 err = kstrtol(buf, 10, &val);
1546 val = SENSORS_LIMIT(DIV_ROUND_CLOSEST(val, 1000), -128, 127);
1548 mutex_lock(&data->update_lock);
1549 data->temp_offset[nr] = val;
1550 nct6775_write_value(data, data->REG_TEMP_OFFSET[nr], val);
1551 mutex_unlock(&data->update_lock);
1557 show_temp_type(struct device *dev, struct device_attribute *attr, char *buf)
1559 struct nct6775_data *data = nct6775_update_device(dev);
1560 struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr);
1561 int nr = sattr->index;
1562 return sprintf(buf, "%d\n", (int)data->temp_type[nr]);
1566 store_temp_type(struct device *dev, struct device_attribute *attr,
1567 const char *buf, size_t count)
1569 struct nct6775_data *data = nct6775_update_device(dev);
1570 struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr);
1571 int nr = sattr->index;
1574 u8 vbat, diode, bit;
1576 err = kstrtoul(buf, 10, &val);
1580 if (val != 1 && val != 3 && val != 4)
1583 mutex_lock(&data->update_lock);
1585 data->temp_type[nr] = val;
1586 vbat = nct6775_read_value(data, data->REG_VBAT) & ~(0x02 << nr);
1587 diode = nct6775_read_value(data, data->REG_DIODE) & ~(0x02 << nr);
1590 case 1: /* CPU diode (diode, current mode) */
1594 case 3: /* diode, voltage mode */
1597 case 4: /* thermistor */
1600 nct6775_write_value(data, data->REG_VBAT, vbat);
1601 nct6775_write_value(data, data->REG_DIODE, diode);
1603 mutex_unlock(&data->update_lock);
1607 static struct sensor_device_attribute_2 sda_temp_input[] = {
1608 SENSOR_ATTR_2(temp1_input, S_IRUGO, show_temp, NULL, 0, 0),
1609 SENSOR_ATTR_2(temp2_input, S_IRUGO, show_temp, NULL, 1, 0),
1610 SENSOR_ATTR_2(temp3_input, S_IRUGO, show_temp, NULL, 2, 0),
1611 SENSOR_ATTR_2(temp4_input, S_IRUGO, show_temp, NULL, 3, 0),
1612 SENSOR_ATTR_2(temp5_input, S_IRUGO, show_temp, NULL, 4, 0),
1613 SENSOR_ATTR_2(temp6_input, S_IRUGO, show_temp, NULL, 5, 0),
1614 SENSOR_ATTR_2(temp7_input, S_IRUGO, show_temp, NULL, 6, 0),
1615 SENSOR_ATTR_2(temp8_input, S_IRUGO, show_temp, NULL, 7, 0),
1616 SENSOR_ATTR_2(temp9_input, S_IRUGO, show_temp, NULL, 8, 0),
1617 SENSOR_ATTR_2(temp10_input, S_IRUGO, show_temp, NULL, 9, 0),
1620 static struct sensor_device_attribute sda_temp_label[] = {
1621 SENSOR_ATTR(temp1_label, S_IRUGO, show_temp_label, NULL, 0),
1622 SENSOR_ATTR(temp2_label, S_IRUGO, show_temp_label, NULL, 1),
1623 SENSOR_ATTR(temp3_label, S_IRUGO, show_temp_label, NULL, 2),
1624 SENSOR_ATTR(temp4_label, S_IRUGO, show_temp_label, NULL, 3),
1625 SENSOR_ATTR(temp5_label, S_IRUGO, show_temp_label, NULL, 4),
1626 SENSOR_ATTR(temp6_label, S_IRUGO, show_temp_label, NULL, 5),
1627 SENSOR_ATTR(temp7_label, S_IRUGO, show_temp_label, NULL, 6),
1628 SENSOR_ATTR(temp8_label, S_IRUGO, show_temp_label, NULL, 7),
1629 SENSOR_ATTR(temp9_label, S_IRUGO, show_temp_label, NULL, 8),
1630 SENSOR_ATTR(temp10_label, S_IRUGO, show_temp_label, NULL, 9),
1633 static struct sensor_device_attribute_2 sda_temp_max[] = {
1634 SENSOR_ATTR_2(temp1_max, S_IRUGO | S_IWUSR, show_temp, store_temp,
1636 SENSOR_ATTR_2(temp2_max, S_IRUGO | S_IWUSR, show_temp, store_temp,
1638 SENSOR_ATTR_2(temp3_max, S_IRUGO | S_IWUSR, show_temp, store_temp,
1640 SENSOR_ATTR_2(temp4_max, S_IRUGO | S_IWUSR, show_temp, store_temp,
1642 SENSOR_ATTR_2(temp5_max, S_IRUGO | S_IWUSR, show_temp, store_temp,
1644 SENSOR_ATTR_2(temp6_max, S_IRUGO | S_IWUSR, show_temp, store_temp,
1646 SENSOR_ATTR_2(temp7_max, S_IRUGO | S_IWUSR, show_temp, store_temp,
1648 SENSOR_ATTR_2(temp8_max, S_IRUGO | S_IWUSR, show_temp, store_temp,
1650 SENSOR_ATTR_2(temp9_max, S_IRUGO | S_IWUSR, show_temp, store_temp,
1652 SENSOR_ATTR_2(temp10_max, S_IRUGO | S_IWUSR, show_temp, store_temp,
1656 static struct sensor_device_attribute_2 sda_temp_max_hyst[] = {
1657 SENSOR_ATTR_2(temp1_max_hyst, S_IRUGO | S_IWUSR, show_temp, store_temp,
1659 SENSOR_ATTR_2(temp2_max_hyst, S_IRUGO | S_IWUSR, show_temp, store_temp,
1661 SENSOR_ATTR_2(temp3_max_hyst, S_IRUGO | S_IWUSR, show_temp, store_temp,
1663 SENSOR_ATTR_2(temp4_max_hyst, S_IRUGO | S_IWUSR, show_temp, store_temp,
1665 SENSOR_ATTR_2(temp5_max_hyst, S_IRUGO | S_IWUSR, show_temp, store_temp,
1667 SENSOR_ATTR_2(temp6_max_hyst, S_IRUGO | S_IWUSR, show_temp, store_temp,
1669 SENSOR_ATTR_2(temp7_max_hyst, S_IRUGO | S_IWUSR, show_temp, store_temp,
1671 SENSOR_ATTR_2(temp8_max_hyst, S_IRUGO | S_IWUSR, show_temp, store_temp,
1673 SENSOR_ATTR_2(temp9_max_hyst, S_IRUGO | S_IWUSR, show_temp, store_temp,
1675 SENSOR_ATTR_2(temp10_max_hyst, S_IRUGO | S_IWUSR, show_temp, store_temp,
1679 static struct sensor_device_attribute_2 sda_temp_crit[] = {
1680 SENSOR_ATTR_2(temp1_crit, S_IRUGO | S_IWUSR, show_temp, store_temp,
1682 SENSOR_ATTR_2(temp2_crit, S_IRUGO | S_IWUSR, show_temp, store_temp,
1684 SENSOR_ATTR_2(temp3_crit, S_IRUGO | S_IWUSR, show_temp, store_temp,
1686 SENSOR_ATTR_2(temp4_crit, S_IRUGO | S_IWUSR, show_temp, store_temp,
1688 SENSOR_ATTR_2(temp5_crit, S_IRUGO | S_IWUSR, show_temp, store_temp,
1690 SENSOR_ATTR_2(temp6_crit, S_IRUGO | S_IWUSR, show_temp, store_temp,
1692 SENSOR_ATTR_2(temp7_crit, S_IRUGO | S_IWUSR, show_temp, store_temp,
1694 SENSOR_ATTR_2(temp8_crit, S_IRUGO | S_IWUSR, show_temp, store_temp,
1696 SENSOR_ATTR_2(temp9_crit, S_IRUGO | S_IWUSR, show_temp, store_temp,
1698 SENSOR_ATTR_2(temp10_crit, S_IRUGO | S_IWUSR, show_temp, store_temp,
1702 static struct sensor_device_attribute sda_temp_offset[] = {
1703 SENSOR_ATTR(temp1_offset, S_IRUGO | S_IWUSR, show_temp_offset,
1704 store_temp_offset, 0),
1705 SENSOR_ATTR(temp2_offset, S_IRUGO | S_IWUSR, show_temp_offset,
1706 store_temp_offset, 1),
1707 SENSOR_ATTR(temp3_offset, S_IRUGO | S_IWUSR, show_temp_offset,
1708 store_temp_offset, 2),
1709 SENSOR_ATTR(temp4_offset, S_IRUGO | S_IWUSR, show_temp_offset,
1710 store_temp_offset, 3),
1711 SENSOR_ATTR(temp5_offset, S_IRUGO | S_IWUSR, show_temp_offset,
1712 store_temp_offset, 4),
1713 SENSOR_ATTR(temp6_offset, S_IRUGO | S_IWUSR, show_temp_offset,
1714 store_temp_offset, 5),
1717 static struct sensor_device_attribute sda_temp_type[] = {
1718 SENSOR_ATTR(temp1_type, S_IRUGO | S_IWUSR, show_temp_type,
1719 store_temp_type, 0),
1720 SENSOR_ATTR(temp2_type, S_IRUGO | S_IWUSR, show_temp_type,
1721 store_temp_type, 1),
1722 SENSOR_ATTR(temp3_type, S_IRUGO | S_IWUSR, show_temp_type,
1723 store_temp_type, 2),
1724 SENSOR_ATTR(temp4_type, S_IRUGO | S_IWUSR, show_temp_type,
1725 store_temp_type, 3),
1726 SENSOR_ATTR(temp5_type, S_IRUGO | S_IWUSR, show_temp_type,
1727 store_temp_type, 4),
1728 SENSOR_ATTR(temp6_type, S_IRUGO | S_IWUSR, show_temp_type,
1729 store_temp_type, 5),
1732 static struct sensor_device_attribute sda_temp_alarm[] = {
1733 SENSOR_ATTR(temp1_alarm, S_IRUGO, show_alarm, NULL,
1735 SENSOR_ATTR(temp2_alarm, S_IRUGO, show_alarm, NULL,
1736 TEMP_ALARM_BASE + 1),
1737 SENSOR_ATTR(temp3_alarm, S_IRUGO, show_alarm, NULL,
1738 TEMP_ALARM_BASE + 2),
1739 SENSOR_ATTR(temp4_alarm, S_IRUGO, show_alarm, NULL,
1740 TEMP_ALARM_BASE + 3),
1741 SENSOR_ATTR(temp5_alarm, S_IRUGO, show_alarm, NULL,
1742 TEMP_ALARM_BASE + 4),
1743 SENSOR_ATTR(temp6_alarm, S_IRUGO, show_alarm, NULL,
1744 TEMP_ALARM_BASE + 5),
1747 #define NUM_TEMP_ALARM ARRAY_SIZE(sda_temp_alarm)
1750 show_pwm_mode(struct device *dev, struct device_attribute *attr, char *buf)
1752 struct nct6775_data *data = nct6775_update_device(dev);
1753 struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr);
1755 return sprintf(buf, "%d\n", !data->pwm_mode[sattr->index]);
1759 store_pwm_mode(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 /* Setting DC mode is not supported for all chips/channels */
1777 if (data->REG_PWM_MODE[nr] == 0) {
1783 mutex_lock(&data->update_lock);
1784 data->pwm_mode[nr] = val;
1785 reg = nct6775_read_value(data, data->REG_PWM_MODE[nr]);
1786 reg &= ~data->PWM_MODE_MASK[nr];
1788 reg |= data->PWM_MODE_MASK[nr];
1789 nct6775_write_value(data, data->REG_PWM_MODE[nr], reg);
1790 mutex_unlock(&data->update_lock);
1795 show_pwm_enable(struct device *dev, struct device_attribute *attr, char *buf)
1797 struct nct6775_data *data = nct6775_update_device(dev);
1798 struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr);
1800 return sprintf(buf, "%d\n", data->pwm_enable[sattr->index]);
1804 show_pwm(struct device *dev, struct device_attribute *attr, char *buf)
1806 struct nct6775_data *data = nct6775_update_device(dev);
1807 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
1809 int index = sattr->index;
1813 * For automatic fan control modes, show current pwm readings.
1814 * Otherwise, show the configured value.
1816 if (index == 0 && data->pwm_enable[nr] > 1)
1817 pwm = nct6775_read_value(data, data->REG_PWM_READ[nr]);
1819 pwm = data->pwm[index][nr];
1821 return sprintf(buf, "%d\n", pwm);
1825 store_pwm(struct device *dev, struct device_attribute *attr, const char *buf,
1828 struct nct6775_data *data = dev_get_drvdata(dev);
1829 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
1831 int index = sattr->index;
1833 int minval[7] = { 0, 1, 1, data->pwm[2][nr], 0, 0, 0 };
1835 = { 255, 255, data->pwm[3][nr] ? : 255, 255, 255, 255, 255 };
1838 err = kstrtoul(buf, 10, &val);
1841 val = SENSORS_LIMIT(val, minval[index], maxval[index]);
1843 mutex_lock(&data->update_lock);
1844 data->pwm[index][nr] = val;
1845 nct6775_write_value(data, data->REG_PWM[index][nr], val);
1846 mutex_unlock(&data->update_lock);
1850 /* Returns 0 if OK, -EINVAL otherwise */
1851 static int check_trip_points(struct nct6775_data *data, int nr)
1855 for (i = 0; i < data->auto_pwm_num - 1; i++) {
1856 if (data->auto_temp[nr][i] > data->auto_temp[nr][i + 1])
1859 for (i = 0; i < data->auto_pwm_num - 1; i++) {
1860 if (data->auto_pwm[nr][i] > data->auto_pwm[nr][i + 1])
1863 /* validate critical temperature and pwm if enabled (pwm > 0) */
1864 if (data->auto_pwm[nr][data->auto_pwm_num]) {
1865 if (data->auto_temp[nr][data->auto_pwm_num - 1] >
1866 data->auto_temp[nr][data->auto_pwm_num] ||
1867 data->auto_pwm[nr][data->auto_pwm_num - 1] >
1868 data->auto_pwm[nr][data->auto_pwm_num])
1875 store_pwm_enable(struct device *dev, struct device_attribute *attr,
1876 const char *buf, size_t count)
1878 struct nct6775_data *data = dev_get_drvdata(dev);
1879 struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr);
1880 int nr = sattr->index;
1885 err = kstrtoul(buf, 10, &val);
1892 if (val == 4 && data->kind != nct6775)
1895 if (val == 5 && check_trip_points(data, nr)) {
1896 dev_err(dev, "Inconsistent trip points, not switching to SmartFan IV mode\n");
1897 dev_err(dev, "Adjust trip points and try again\n");
1901 mutex_lock(&data->update_lock);
1902 data->pwm_enable[nr] = val;
1905 * turn off pwm control: select manual mode, set pwm to maximum
1907 data->pwm[0][nr] = 255;
1908 nct6775_write_value(data, data->REG_PWM[0][nr], 255);
1910 reg = nct6775_read_value(data, data->REG_FAN_MODE[nr]);
1912 reg |= pwm_enable_to_reg(val) << 4;
1913 nct6775_write_value(data, data->REG_FAN_MODE[nr], reg);
1914 mutex_unlock(&data->update_lock);
1919 show_pwm_temp_sel(struct device *dev, struct device_attribute *attr, char *buf)
1921 struct nct6775_data *data = nct6775_update_device(dev);
1922 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
1923 int i, src, sel = 0;
1925 src = data->pwm_temp_sel[sattr->index][sattr->nr];
1927 for (i = 0; i < NUM_TEMP; i++) {
1928 if (!(data->have_temp & (1 << i)))
1930 if (src == data->temp_src[i]) {
1936 return sprintf(buf, "%d\n", sel);
1940 store_pwm_temp_sel(struct device *dev, struct device_attribute *attr,
1941 const char *buf, size_t count)
1943 struct nct6775_data *data = nct6775_update_device(dev);
1944 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
1946 int index = sattr->index;
1950 err = kstrtoul(buf, 10, &val);
1953 if (val == 0 || val > NUM_TEMP)
1955 if (!(data->have_temp & (1 << (val - 1))) || !data->temp_src[val - 1])
1958 mutex_lock(&data->update_lock);
1959 src = data->temp_src[val - 1];
1960 data->pwm_temp_sel[index][nr] = src;
1961 reg = nct6775_read_value(data, data->REG_TEMP_SEL[index][nr]);
1964 nct6775_write_value(data, data->REG_TEMP_SEL[index][nr], reg);
1965 mutex_unlock(&data->update_lock);
1971 show_target_temp(struct device *dev, struct device_attribute *attr, char *buf)
1973 struct nct6775_data *data = nct6775_update_device(dev);
1974 struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr);
1976 return sprintf(buf, "%d\n",
1977 data->target_temp[sattr->index] * 1000);
1981 store_target_temp(struct device *dev, struct device_attribute *attr,
1982 const char *buf, size_t count)
1984 struct nct6775_data *data = dev_get_drvdata(dev);
1985 struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr);
1986 int nr = sattr->index;
1990 err = kstrtol(buf, 10, &val);
1994 val = SENSORS_LIMIT(DIV_ROUND_CLOSEST(val, 1000), 0, 127);
1996 mutex_lock(&data->update_lock);
1997 data->target_temp[nr] = val;
1998 nct6775_write_value(data, data->REG_TARGET[nr], val);
1999 mutex_unlock(&data->update_lock);
2004 show_auto_temp_hyst(struct device *dev, struct device_attribute *attr,
2007 struct nct6775_data *data = nct6775_update_device(dev);
2008 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
2010 int point = sattr->index >= data->auto_pwm_num ? 1 : 0;
2011 int tolerance = data->tolerance[nr][point];
2012 int temp = data->auto_temp[nr][sattr->index];
2014 return sprintf(buf, "%d\n", (temp - tolerance) * 1000);
2018 store_auto_temp_hyst(struct device *dev, struct device_attribute *attr,
2019 const char *buf, size_t count)
2021 struct nct6775_data *data = dev_get_drvdata(dev);
2022 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
2024 int point = sattr->index >= data->auto_pwm_num ? 1 : 0;
2028 int maxlimit[2][3] = { { 15, 7, 63 }, { 15, 7, 7 } };
2029 int mask[] = { 0x0f, 0x07, 0x07 };
2032 err = kstrtol(buf, 10, &val);
2036 temp = data->auto_temp[nr][sattr->index];
2037 val = temp - DIV_ROUND_CLOSEST(val, 1000);
2039 /* Limit tolerance as needed */
2040 val = SENSORS_LIMIT(val, 0, maxlimit[point][data->kind]);
2042 mutex_lock(&data->update_lock);
2044 nct6775_write_value(data,
2045 data->REG_CRITICAL_TEMP_TOLERANCE[nr],
2048 reg = nct6775_read_value(data, data->REG_FAN_MODE[nr]);
2049 reg = (reg & ~mask[nr]) | (val & mask[nr]);
2050 nct6775_write_value(data, data->REG_FAN_MODE[nr], reg);
2051 if (data->kind == nct6779) {
2052 reg = nct6775_read_value(data,
2053 NCT6779_REG_TOLERANCE_H[nr]);
2054 reg = (reg & 0x70) | ((val & 0x38) << 1);
2055 nct6775_write_value(data,
2056 NCT6779_REG_TOLERANCE_H[nr], reg);
2060 data->tolerance[nr][point] = val;
2061 mutex_unlock(&data->update_lock);
2065 static SENSOR_DEVICE_ATTR_2(pwm1, S_IWUSR | S_IRUGO, show_pwm, store_pwm, 0, 0);
2066 static SENSOR_DEVICE_ATTR_2(pwm2, S_IWUSR | S_IRUGO, show_pwm, store_pwm, 1, 0);
2067 static SENSOR_DEVICE_ATTR_2(pwm3, S_IWUSR | S_IRUGO, show_pwm, store_pwm, 2, 0);
2068 static SENSOR_DEVICE_ATTR_2(pwm4, S_IWUSR | S_IRUGO, show_pwm, store_pwm, 3, 0);
2069 static SENSOR_DEVICE_ATTR_2(pwm5, S_IWUSR | S_IRUGO, show_pwm, store_pwm, 4, 0);
2071 static SENSOR_DEVICE_ATTR(pwm1_mode, S_IWUSR | S_IRUGO, show_pwm_mode,
2073 static SENSOR_DEVICE_ATTR(pwm2_mode, S_IWUSR | S_IRUGO, show_pwm_mode,
2075 static SENSOR_DEVICE_ATTR(pwm3_mode, S_IWUSR | S_IRUGO, show_pwm_mode,
2077 static SENSOR_DEVICE_ATTR(pwm4_mode, S_IWUSR | S_IRUGO, show_pwm_mode,
2079 static SENSOR_DEVICE_ATTR(pwm5_mode, S_IWUSR | S_IRUGO, show_pwm_mode,
2082 static SENSOR_DEVICE_ATTR(pwm1_enable, S_IWUSR | S_IRUGO, show_pwm_enable,
2083 store_pwm_enable, 0);
2084 static SENSOR_DEVICE_ATTR(pwm2_enable, S_IWUSR | S_IRUGO, show_pwm_enable,
2085 store_pwm_enable, 1);
2086 static SENSOR_DEVICE_ATTR(pwm3_enable, S_IWUSR | S_IRUGO, show_pwm_enable,
2087 store_pwm_enable, 2);
2088 static SENSOR_DEVICE_ATTR(pwm4_enable, S_IWUSR | S_IRUGO, show_pwm_enable,
2089 store_pwm_enable, 3);
2090 static SENSOR_DEVICE_ATTR(pwm5_enable, S_IWUSR | S_IRUGO, show_pwm_enable,
2091 store_pwm_enable, 4);
2093 static SENSOR_DEVICE_ATTR_2(pwm1_temp_sel, S_IWUSR | S_IRUGO,
2094 show_pwm_temp_sel, store_pwm_temp_sel, 0, 0);
2095 static SENSOR_DEVICE_ATTR_2(pwm2_temp_sel, S_IWUSR | S_IRUGO,
2096 show_pwm_temp_sel, store_pwm_temp_sel, 1, 0);
2097 static SENSOR_DEVICE_ATTR_2(pwm3_temp_sel, S_IWUSR | S_IRUGO,
2098 show_pwm_temp_sel, store_pwm_temp_sel, 2, 0);
2099 static SENSOR_DEVICE_ATTR_2(pwm4_temp_sel, S_IWUSR | S_IRUGO,
2100 show_pwm_temp_sel, store_pwm_temp_sel, 3, 0);
2101 static SENSOR_DEVICE_ATTR_2(pwm5_temp_sel, S_IWUSR | S_IRUGO,
2102 show_pwm_temp_sel, store_pwm_temp_sel, 4, 0);
2104 static SENSOR_DEVICE_ATTR(pwm1_target, S_IWUSR | S_IRUGO, show_target_temp,
2105 store_target_temp, 0);
2106 static SENSOR_DEVICE_ATTR(pwm2_target, S_IWUSR | S_IRUGO, show_target_temp,
2107 store_target_temp, 1);
2108 static SENSOR_DEVICE_ATTR(pwm3_target, S_IWUSR | S_IRUGO, show_target_temp,
2109 store_target_temp, 2);
2110 static SENSOR_DEVICE_ATTR(pwm4_target, S_IWUSR | S_IRUGO, show_target_temp,
2111 store_target_temp, 3);
2112 static SENSOR_DEVICE_ATTR(pwm5_target, S_IWUSR | S_IRUGO, show_target_temp,
2113 store_target_temp, 4);
2115 /* Smart Fan registers */
2118 show_pwm_sel_enable(struct device *dev, struct device_attribute *attr,
2121 struct nct6775_data *data = nct6775_update_device(dev);
2122 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
2124 return sprintf(buf, "%d\n",
2125 data->pwm_sel_enable[sattr->index][sattr->nr]);
2129 store_pwm_sel_enable(struct device *dev, struct device_attribute *attr,
2130 const char *buf, size_t count)
2132 struct nct6775_data *data = dev_get_drvdata(dev);
2133 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
2135 int index = sattr->index;
2139 static const u8 bit[] = { 0x80, 0x80, 0x01 };
2142 err = kstrtoul(buf, 10, &val);
2149 mutex_lock(&data->update_lock);
2150 data->pwm_sel_enable[index][nr] = val;
2151 reg = nct6775_read_value(data, data->REG_TEMP_SEL[index][nr]);
2155 nct6775_write_value(data, data->REG_TEMP_SEL[index][nr], reg);
2156 mutex_unlock(&data->update_lock);
2161 show_weight_temp(struct device *dev, struct device_attribute *attr, char *buf)
2163 struct nct6775_data *data = nct6775_update_device(dev);
2164 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
2166 int index = sattr->index;
2168 return sprintf(buf, "%d\n", data->weight_temp[index][nr] * 1000);
2172 store_weight_temp(struct device *dev, struct device_attribute *attr,
2173 const char *buf, size_t count)
2175 struct nct6775_data *data = dev_get_drvdata(dev);
2176 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
2178 int index = sattr->index;
2182 err = kstrtoul(buf, 10, &val);
2186 val = SENSORS_LIMIT(DIV_ROUND_CLOSEST(val, 1000), 0, 255);
2188 mutex_lock(&data->update_lock);
2189 data->weight_temp[index][nr] = val;
2190 nct6775_write_value(data, data->REG_WEIGHT_TEMP[index][nr], val);
2191 mutex_unlock(&data->update_lock);
2195 static SENSOR_DEVICE_ATTR_2(pwm1_stop_output_enable, S_IWUSR | S_IRUGO,
2196 show_pwm_sel_enable, store_pwm_sel_enable, 0, 0);
2197 static SENSOR_DEVICE_ATTR_2(pwm2_stop_output_enable, S_IWUSR | S_IRUGO,
2198 show_pwm_sel_enable, store_pwm_sel_enable, 1, 0);
2199 static SENSOR_DEVICE_ATTR_2(pwm3_stop_output_enable, S_IWUSR | S_IRUGO,
2200 show_pwm_sel_enable, store_pwm_sel_enable, 2, 0);
2201 static SENSOR_DEVICE_ATTR_2(pwm4_stop_output_enable, S_IWUSR | S_IRUGO,
2202 show_pwm_sel_enable, store_pwm_sel_enable, 3, 0);
2203 static SENSOR_DEVICE_ATTR_2(pwm5_stop_output_enable, S_IWUSR | S_IRUGO,
2204 show_pwm_sel_enable, store_pwm_sel_enable, 4, 0);
2206 static SENSOR_DEVICE_ATTR_2(pwm1_weight_enable, S_IWUSR | S_IRUGO,
2207 show_pwm_sel_enable, store_pwm_sel_enable, 0, 1);
2208 static SENSOR_DEVICE_ATTR_2(pwm2_weight_enable, S_IWUSR | S_IRUGO,
2209 show_pwm_sel_enable, store_pwm_sel_enable, 1, 1);
2210 static SENSOR_DEVICE_ATTR_2(pwm3_weight_enable, S_IWUSR | S_IRUGO,
2211 show_pwm_sel_enable, store_pwm_sel_enable, 2, 1);
2212 static SENSOR_DEVICE_ATTR_2(pwm4_weight_enable, S_IWUSR | S_IRUGO,
2213 show_pwm_sel_enable, store_pwm_sel_enable, 3, 1);
2214 static SENSOR_DEVICE_ATTR_2(pwm5_weight_enable, S_IWUSR | S_IRUGO,
2215 show_pwm_sel_enable, store_pwm_sel_enable, 4, 1);
2217 static SENSOR_DEVICE_ATTR_2(pwm1_step_enable, S_IWUSR | S_IRUGO,
2218 show_pwm_sel_enable, store_pwm_sel_enable, 0, 2);
2219 static SENSOR_DEVICE_ATTR_2(pwm2_step_enable, S_IWUSR | S_IRUGO,
2220 show_pwm_sel_enable, store_pwm_sel_enable, 1, 2);
2221 static SENSOR_DEVICE_ATTR_2(pwm3_step_enable, S_IWUSR | S_IRUGO,
2222 show_pwm_sel_enable, store_pwm_sel_enable, 2, 2);
2223 static SENSOR_DEVICE_ATTR_2(pwm4_step_enable, S_IWUSR | S_IRUGO,
2224 show_pwm_sel_enable, store_pwm_sel_enable, 3, 2);
2225 static SENSOR_DEVICE_ATTR_2(pwm5_step_enable, S_IWUSR | S_IRUGO,
2226 show_pwm_sel_enable, store_pwm_sel_enable, 4, 2);
2228 static SENSOR_DEVICE_ATTR_2(pwm1_weight_temp_sel, S_IWUSR | S_IRUGO,
2229 show_pwm_temp_sel, store_pwm_temp_sel, 0, 1);
2230 static SENSOR_DEVICE_ATTR_2(pwm2_weight_temp_sel, S_IWUSR | S_IRUGO,
2231 show_pwm_temp_sel, store_pwm_temp_sel, 1, 1);
2232 static SENSOR_DEVICE_ATTR_2(pwm3_weight_temp_sel, S_IWUSR | S_IRUGO,
2233 show_pwm_temp_sel, store_pwm_temp_sel, 2, 1);
2234 static SENSOR_DEVICE_ATTR_2(pwm4_weight_temp_sel, S_IWUSR | S_IRUGO,
2235 show_pwm_temp_sel, store_pwm_temp_sel, 3, 1);
2236 static SENSOR_DEVICE_ATTR_2(pwm5_weight_temp_sel, S_IWUSR | S_IRUGO,
2237 show_pwm_temp_sel, store_pwm_temp_sel, 4, 1);
2239 static SENSOR_DEVICE_ATTR_2(pwm1_weight_temp_step, S_IWUSR | S_IRUGO,
2240 show_weight_temp, store_weight_temp, 0, 0);
2241 static SENSOR_DEVICE_ATTR_2(pwm2_weight_temp_step, S_IWUSR | S_IRUGO,
2242 show_weight_temp, store_weight_temp, 1, 0);
2243 static SENSOR_DEVICE_ATTR_2(pwm3_weight_temp_step, S_IWUSR | S_IRUGO,
2244 show_weight_temp, store_weight_temp, 2, 0);
2245 static SENSOR_DEVICE_ATTR_2(pwm4_weight_temp_step, S_IWUSR | S_IRUGO,
2246 show_weight_temp, store_weight_temp, 3, 0);
2247 static SENSOR_DEVICE_ATTR_2(pwm5_weight_temp_step, S_IWUSR | S_IRUGO,
2248 show_weight_temp, store_weight_temp, 4, 0);
2250 static SENSOR_DEVICE_ATTR_2(pwm1_weight_temp_step_tol, S_IWUSR | S_IRUGO,
2251 show_weight_temp, store_weight_temp, 0, 1);
2252 static SENSOR_DEVICE_ATTR_2(pwm2_weight_temp_step_tol, S_IWUSR | S_IRUGO,
2253 show_weight_temp, store_weight_temp, 1, 1);
2254 static SENSOR_DEVICE_ATTR_2(pwm3_weight_temp_step_tol, S_IWUSR | S_IRUGO,
2255 show_weight_temp, store_weight_temp, 2, 1);
2256 static SENSOR_DEVICE_ATTR_2(pwm4_weight_temp_step_tol, S_IWUSR | S_IRUGO,
2257 show_weight_temp, store_weight_temp, 3, 1);
2258 static SENSOR_DEVICE_ATTR_2(pwm5_weight_temp_step_tol, S_IWUSR | S_IRUGO,
2259 show_weight_temp, store_weight_temp, 4, 1);
2261 static SENSOR_DEVICE_ATTR_2(pwm1_weight_temp_step_base, S_IWUSR | S_IRUGO,
2262 show_weight_temp, store_weight_temp, 0, 2);
2263 static SENSOR_DEVICE_ATTR_2(pwm2_weight_temp_step_base, S_IWUSR | S_IRUGO,
2264 show_weight_temp, store_weight_temp, 1, 2);
2265 static SENSOR_DEVICE_ATTR_2(pwm3_weight_temp_step_base, S_IWUSR | S_IRUGO,
2266 show_weight_temp, store_weight_temp, 2, 2);
2267 static SENSOR_DEVICE_ATTR_2(pwm4_weight_temp_step_base, S_IWUSR | S_IRUGO,
2268 show_weight_temp, store_weight_temp, 3, 2);
2269 static SENSOR_DEVICE_ATTR_2(pwm5_weight_temp_step_base, S_IWUSR | S_IRUGO,
2270 show_weight_temp, store_weight_temp, 4, 2);
2272 static SENSOR_DEVICE_ATTR_2(pwm1_weight_duty_step, S_IWUSR | S_IRUGO,
2273 show_pwm, store_pwm, 0, 5);
2274 static SENSOR_DEVICE_ATTR_2(pwm2_weight_duty_step, S_IWUSR | S_IRUGO,
2275 show_pwm, store_pwm, 1, 5);
2276 static SENSOR_DEVICE_ATTR_2(pwm3_weight_duty_step, S_IWUSR | S_IRUGO,
2277 show_pwm, store_pwm, 2, 5);
2278 static SENSOR_DEVICE_ATTR_2(pwm4_weight_duty_step, S_IWUSR | S_IRUGO,
2279 show_pwm, store_pwm, 3, 5);
2280 static SENSOR_DEVICE_ATTR_2(pwm5_weight_duty_step, S_IWUSR | S_IRUGO,
2281 show_pwm, store_pwm, 4, 5);
2283 /* duty_base is not supported on all chips */
2284 static struct sensor_device_attribute_2 sda_weight_duty_base[] = {
2285 SENSOR_ATTR_2(pwm1_weight_duty_base, S_IWUSR | S_IRUGO,
2286 show_pwm, store_pwm, 0, 6),
2287 SENSOR_ATTR_2(pwm2_weight_duty_base, S_IWUSR | S_IRUGO,
2288 show_pwm, store_pwm, 1, 6),
2289 SENSOR_ATTR_2(pwm3_weight_duty_base, S_IWUSR | S_IRUGO,
2290 show_pwm, store_pwm, 2, 6),
2291 SENSOR_ATTR_2(pwm4_weight_duty_base, S_IWUSR | S_IRUGO,
2292 show_pwm, store_pwm, 3, 6),
2293 SENSOR_ATTR_2(pwm5_weight_duty_base, S_IWUSR | S_IRUGO,
2294 show_pwm, store_pwm, 4, 6),
2298 show_fan_time(struct device *dev, struct device_attribute *attr, char *buf)
2300 struct nct6775_data *data = nct6775_update_device(dev);
2301 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
2303 int index = sattr->index;
2305 return sprintf(buf, "%d\n",
2306 step_time_from_reg(data->fan_time[index][nr],
2307 data->pwm_mode[nr]));
2311 store_fan_time(struct device *dev, struct device_attribute *attr,
2312 const char *buf, size_t count)
2314 struct nct6775_data *data = dev_get_drvdata(dev);
2315 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
2317 int index = sattr->index;
2321 err = kstrtoul(buf, 10, &val);
2325 val = step_time_to_reg(val, data->pwm_mode[nr]);
2326 mutex_lock(&data->update_lock);
2327 data->fan_time[index][nr] = val;
2328 nct6775_write_value(data, data->REG_FAN_TIME[index][nr], val);
2329 mutex_unlock(&data->update_lock);
2334 show_name(struct device *dev, struct device_attribute *attr, char *buf)
2336 struct nct6775_data *data = dev_get_drvdata(dev);
2338 return sprintf(buf, "%s\n", data->name);
2341 static DEVICE_ATTR(name, S_IRUGO, show_name, NULL);
2343 static SENSOR_DEVICE_ATTR_2(pwm1_stop_time, S_IWUSR | S_IRUGO, show_fan_time,
2344 store_fan_time, 0, 0);
2345 static SENSOR_DEVICE_ATTR_2(pwm2_stop_time, S_IWUSR | S_IRUGO, show_fan_time,
2346 store_fan_time, 1, 0);
2347 static SENSOR_DEVICE_ATTR_2(pwm3_stop_time, S_IWUSR | S_IRUGO, show_fan_time,
2348 store_fan_time, 2, 0);
2349 static SENSOR_DEVICE_ATTR_2(pwm4_stop_time, S_IWUSR | S_IRUGO, show_fan_time,
2350 store_fan_time, 3, 0);
2351 static SENSOR_DEVICE_ATTR_2(pwm5_stop_time, S_IWUSR | S_IRUGO, show_fan_time,
2352 store_fan_time, 4, 0);
2354 static SENSOR_DEVICE_ATTR_2(pwm1_step_up_time, S_IWUSR | S_IRUGO, show_fan_time,
2355 store_fan_time, 0, 1);
2356 static SENSOR_DEVICE_ATTR_2(pwm2_step_up_time, S_IWUSR | S_IRUGO, show_fan_time,
2357 store_fan_time, 1, 1);
2358 static SENSOR_DEVICE_ATTR_2(pwm3_step_up_time, S_IWUSR | S_IRUGO, show_fan_time,
2359 store_fan_time, 2, 1);
2360 static SENSOR_DEVICE_ATTR_2(pwm4_step_up_time, S_IWUSR | S_IRUGO, show_fan_time,
2361 store_fan_time, 3, 1);
2362 static SENSOR_DEVICE_ATTR_2(pwm5_step_up_time, S_IWUSR | S_IRUGO, show_fan_time,
2363 store_fan_time, 4, 1);
2365 static SENSOR_DEVICE_ATTR_2(pwm1_step_down_time, S_IWUSR | S_IRUGO,
2366 show_fan_time, store_fan_time, 0, 2);
2367 static SENSOR_DEVICE_ATTR_2(pwm2_step_down_time, S_IWUSR | S_IRUGO,
2368 show_fan_time, store_fan_time, 1, 2);
2369 static SENSOR_DEVICE_ATTR_2(pwm3_step_down_time, S_IWUSR | S_IRUGO,
2370 show_fan_time, store_fan_time, 2, 2);
2371 static SENSOR_DEVICE_ATTR_2(pwm4_step_down_time, S_IWUSR | S_IRUGO,
2372 show_fan_time, store_fan_time, 3, 2);
2373 static SENSOR_DEVICE_ATTR_2(pwm5_step_down_time, S_IWUSR | S_IRUGO,
2374 show_fan_time, store_fan_time, 4, 2);
2376 static SENSOR_DEVICE_ATTR_2(pwm1_start_output, S_IWUSR | S_IRUGO, show_pwm,
2378 static SENSOR_DEVICE_ATTR_2(pwm2_start_output, S_IWUSR | S_IRUGO, show_pwm,
2380 static SENSOR_DEVICE_ATTR_2(pwm3_start_output, S_IWUSR | S_IRUGO, show_pwm,
2382 static SENSOR_DEVICE_ATTR_2(pwm4_start_output, S_IWUSR | S_IRUGO, show_pwm,
2384 static SENSOR_DEVICE_ATTR_2(pwm5_start_output, S_IWUSR | S_IRUGO, show_pwm,
2387 static SENSOR_DEVICE_ATTR_2(pwm1_stop_output, S_IWUSR | S_IRUGO, show_pwm,
2389 static SENSOR_DEVICE_ATTR_2(pwm2_stop_output, S_IWUSR | S_IRUGO, show_pwm,
2391 static SENSOR_DEVICE_ATTR_2(pwm3_stop_output, S_IWUSR | S_IRUGO, show_pwm,
2393 static SENSOR_DEVICE_ATTR_2(pwm4_stop_output, S_IWUSR | S_IRUGO, show_pwm,
2395 static SENSOR_DEVICE_ATTR_2(pwm5_stop_output, S_IWUSR | S_IRUGO, show_pwm,
2398 /* max_output is not supported on all chips */
2399 static struct sensor_device_attribute_2 sda_max_output[] = {
2400 SENSOR_ATTR_2(pwm1_max_output, S_IWUSR | S_IRUGO, show_pwm, store_pwm,
2402 SENSOR_ATTR_2(pwm2_max_output, S_IWUSR | S_IRUGO, show_pwm, store_pwm,
2404 SENSOR_ATTR_2(pwm3_max_output, S_IWUSR | S_IRUGO, show_pwm, store_pwm,
2406 SENSOR_ATTR_2(pwm4_max_output, S_IWUSR | S_IRUGO, show_pwm, store_pwm,
2408 SENSOR_ATTR_2(pwm5_max_output, S_IWUSR | S_IRUGO, show_pwm, store_pwm,
2412 /* step_output is not supported on all chips */
2413 static struct sensor_device_attribute_2 sda_step_output[] = {
2414 SENSOR_ATTR_2(pwm1_step_output, S_IWUSR | S_IRUGO, show_pwm, store_pwm,
2416 SENSOR_ATTR_2(pwm2_step_output, S_IWUSR | S_IRUGO, show_pwm, store_pwm,
2418 SENSOR_ATTR_2(pwm3_step_output, S_IWUSR | S_IRUGO, show_pwm, store_pwm,
2420 SENSOR_ATTR_2(pwm4_step_output, S_IWUSR | S_IRUGO, show_pwm, store_pwm,
2422 SENSOR_ATTR_2(pwm5_step_output, S_IWUSR | S_IRUGO, show_pwm, store_pwm,
2426 static struct attribute *nct6775_attributes_pwm[5][20] = {
2428 &sensor_dev_attr_pwm1.dev_attr.attr,
2429 &sensor_dev_attr_pwm1_mode.dev_attr.attr,
2430 &sensor_dev_attr_pwm1_enable.dev_attr.attr,
2431 &sensor_dev_attr_pwm1_stop_output_enable.dev_attr.attr,
2432 &sensor_dev_attr_pwm1_weight_enable.dev_attr.attr,
2433 &sensor_dev_attr_pwm1_step_enable.dev_attr.attr,
2434 &sensor_dev_attr_pwm1_temp_sel.dev_attr.attr,
2435 &sensor_dev_attr_pwm1_target.dev_attr.attr,
2436 &sensor_dev_attr_pwm1_stop_time.dev_attr.attr,
2437 &sensor_dev_attr_pwm1_step_up_time.dev_attr.attr,
2438 &sensor_dev_attr_pwm1_step_down_time.dev_attr.attr,
2439 &sensor_dev_attr_pwm1_start_output.dev_attr.attr,
2440 &sensor_dev_attr_pwm1_stop_output.dev_attr.attr,
2441 &sensor_dev_attr_pwm1_weight_temp_sel.dev_attr.attr,
2442 &sensor_dev_attr_pwm1_weight_temp_step.dev_attr.attr,
2443 &sensor_dev_attr_pwm1_weight_temp_step_tol.dev_attr.attr,
2444 &sensor_dev_attr_pwm1_weight_temp_step_base.dev_attr.attr,
2445 &sensor_dev_attr_pwm1_weight_duty_step.dev_attr.attr,
2449 &sensor_dev_attr_pwm2.dev_attr.attr,
2450 &sensor_dev_attr_pwm2_mode.dev_attr.attr,
2451 &sensor_dev_attr_pwm2_enable.dev_attr.attr,
2452 &sensor_dev_attr_pwm2_stop_output_enable.dev_attr.attr,
2453 &sensor_dev_attr_pwm2_weight_enable.dev_attr.attr,
2454 &sensor_dev_attr_pwm2_step_enable.dev_attr.attr,
2455 &sensor_dev_attr_pwm2_temp_sel.dev_attr.attr,
2456 &sensor_dev_attr_pwm2_target.dev_attr.attr,
2457 &sensor_dev_attr_pwm2_stop_time.dev_attr.attr,
2458 &sensor_dev_attr_pwm2_step_up_time.dev_attr.attr,
2459 &sensor_dev_attr_pwm2_step_down_time.dev_attr.attr,
2460 &sensor_dev_attr_pwm2_start_output.dev_attr.attr,
2461 &sensor_dev_attr_pwm2_stop_output.dev_attr.attr,
2462 &sensor_dev_attr_pwm2_weight_temp_sel.dev_attr.attr,
2463 &sensor_dev_attr_pwm2_weight_temp_step.dev_attr.attr,
2464 &sensor_dev_attr_pwm2_weight_temp_step_tol.dev_attr.attr,
2465 &sensor_dev_attr_pwm2_weight_temp_step_base.dev_attr.attr,
2466 &sensor_dev_attr_pwm2_weight_duty_step.dev_attr.attr,
2470 &sensor_dev_attr_pwm3.dev_attr.attr,
2471 &sensor_dev_attr_pwm3_mode.dev_attr.attr,
2472 &sensor_dev_attr_pwm3_enable.dev_attr.attr,
2473 &sensor_dev_attr_pwm3_stop_output_enable.dev_attr.attr,
2474 &sensor_dev_attr_pwm3_weight_enable.dev_attr.attr,
2475 &sensor_dev_attr_pwm3_step_enable.dev_attr.attr,
2476 &sensor_dev_attr_pwm3_temp_sel.dev_attr.attr,
2477 &sensor_dev_attr_pwm3_target.dev_attr.attr,
2478 &sensor_dev_attr_pwm3_stop_time.dev_attr.attr,
2479 &sensor_dev_attr_pwm3_step_up_time.dev_attr.attr,
2480 &sensor_dev_attr_pwm3_step_down_time.dev_attr.attr,
2481 &sensor_dev_attr_pwm3_start_output.dev_attr.attr,
2482 &sensor_dev_attr_pwm3_stop_output.dev_attr.attr,
2483 &sensor_dev_attr_pwm3_weight_temp_sel.dev_attr.attr,
2484 &sensor_dev_attr_pwm3_weight_temp_step.dev_attr.attr,
2485 &sensor_dev_attr_pwm3_weight_temp_step_tol.dev_attr.attr,
2486 &sensor_dev_attr_pwm3_weight_temp_step_base.dev_attr.attr,
2487 &sensor_dev_attr_pwm3_weight_duty_step.dev_attr.attr,
2491 &sensor_dev_attr_pwm4.dev_attr.attr,
2492 &sensor_dev_attr_pwm4_mode.dev_attr.attr,
2493 &sensor_dev_attr_pwm4_enable.dev_attr.attr,
2494 &sensor_dev_attr_pwm4_stop_output_enable.dev_attr.attr,
2495 &sensor_dev_attr_pwm4_weight_enable.dev_attr.attr,
2496 &sensor_dev_attr_pwm4_step_enable.dev_attr.attr,
2497 &sensor_dev_attr_pwm4_temp_sel.dev_attr.attr,
2498 &sensor_dev_attr_pwm4_target.dev_attr.attr,
2499 &sensor_dev_attr_pwm4_stop_time.dev_attr.attr,
2500 &sensor_dev_attr_pwm4_step_up_time.dev_attr.attr,
2501 &sensor_dev_attr_pwm4_step_down_time.dev_attr.attr,
2502 &sensor_dev_attr_pwm4_start_output.dev_attr.attr,
2503 &sensor_dev_attr_pwm4_stop_output.dev_attr.attr,
2504 &sensor_dev_attr_pwm4_weight_temp_sel.dev_attr.attr,
2505 &sensor_dev_attr_pwm4_weight_temp_step.dev_attr.attr,
2506 &sensor_dev_attr_pwm4_weight_temp_step_tol.dev_attr.attr,
2507 &sensor_dev_attr_pwm4_weight_temp_step_base.dev_attr.attr,
2508 &sensor_dev_attr_pwm4_weight_duty_step.dev_attr.attr,
2512 &sensor_dev_attr_pwm5.dev_attr.attr,
2513 &sensor_dev_attr_pwm5_mode.dev_attr.attr,
2514 &sensor_dev_attr_pwm5_enable.dev_attr.attr,
2515 &sensor_dev_attr_pwm5_stop_output_enable.dev_attr.attr,
2516 &sensor_dev_attr_pwm5_weight_enable.dev_attr.attr,
2517 &sensor_dev_attr_pwm5_step_enable.dev_attr.attr,
2518 &sensor_dev_attr_pwm5_temp_sel.dev_attr.attr,
2519 &sensor_dev_attr_pwm5_target.dev_attr.attr,
2520 &sensor_dev_attr_pwm5_stop_time.dev_attr.attr,
2521 &sensor_dev_attr_pwm5_step_up_time.dev_attr.attr,
2522 &sensor_dev_attr_pwm5_step_down_time.dev_attr.attr,
2523 &sensor_dev_attr_pwm5_start_output.dev_attr.attr,
2524 &sensor_dev_attr_pwm5_stop_output.dev_attr.attr,
2525 &sensor_dev_attr_pwm5_weight_temp_sel.dev_attr.attr,
2526 &sensor_dev_attr_pwm5_weight_temp_step.dev_attr.attr,
2527 &sensor_dev_attr_pwm5_weight_temp_step_tol.dev_attr.attr,
2528 &sensor_dev_attr_pwm5_weight_temp_step_base.dev_attr.attr,
2529 &sensor_dev_attr_pwm5_weight_duty_step.dev_attr.attr,
2534 static const struct attribute_group nct6775_group_pwm[5] = {
2535 { .attrs = nct6775_attributes_pwm[0] },
2536 { .attrs = nct6775_attributes_pwm[1] },
2537 { .attrs = nct6775_attributes_pwm[2] },
2538 { .attrs = nct6775_attributes_pwm[3] },
2539 { .attrs = nct6775_attributes_pwm[4] },
2543 show_auto_pwm(struct device *dev, struct device_attribute *attr, char *buf)
2545 struct nct6775_data *data = nct6775_update_device(dev);
2546 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
2548 return sprintf(buf, "%d\n", data->auto_pwm[sattr->nr][sattr->index]);
2552 store_auto_pwm(struct device *dev, struct device_attribute *attr,
2553 const char *buf, size_t count)
2555 struct nct6775_data *data = dev_get_drvdata(dev);
2556 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
2558 int point = sattr->index;
2563 err = kstrtoul(buf, 10, &val);
2569 if (point == data->auto_pwm_num) {
2570 if (data->kind != nct6775 && !val)
2572 if (data->kind != nct6779 && val)
2576 mutex_lock(&data->update_lock);
2577 data->auto_pwm[nr][point] = val;
2578 if (point < data->auto_pwm_num) {
2579 nct6775_write_value(data,
2580 NCT6775_AUTO_PWM(data, nr, point),
2581 data->auto_pwm[nr][point]);
2583 switch (data->kind) {
2585 /* disable if needed (pwm == 0) */
2586 reg = nct6775_read_value(data,
2587 NCT6775_REG_CRITICAL_ENAB[nr]);
2592 nct6775_write_value(data, NCT6775_REG_CRITICAL_ENAB[nr],
2596 break; /* always enabled, nothing to do */
2598 nct6775_write_value(data, NCT6779_REG_CRITICAL_PWM[nr],
2600 reg = nct6775_read_value(data,
2601 NCT6779_REG_CRITICAL_PWM_ENABLE[nr]);
2606 nct6775_write_value(data,
2607 NCT6779_REG_CRITICAL_PWM_ENABLE[nr],
2612 mutex_unlock(&data->update_lock);
2617 show_auto_temp(struct device *dev, struct device_attribute *attr, char *buf)
2619 struct nct6775_data *data = nct6775_update_device(dev);
2620 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
2622 int point = sattr->index;
2625 * We don't know for sure if the temperature is signed or unsigned.
2626 * Assume it is unsigned.
2628 return sprintf(buf, "%d\n", data->auto_temp[nr][point] * 1000);
2632 store_auto_temp(struct device *dev, struct device_attribute *attr,
2633 const char *buf, size_t count)
2635 struct nct6775_data *data = dev_get_drvdata(dev);
2636 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
2638 int point = sattr->index;
2642 err = kstrtoul(buf, 10, &val);
2648 mutex_lock(&data->update_lock);
2649 data->auto_temp[nr][point] = DIV_ROUND_CLOSEST(val, 1000);
2650 if (point < data->auto_pwm_num) {
2651 nct6775_write_value(data,
2652 NCT6775_AUTO_TEMP(data, nr, point),
2653 data->auto_temp[nr][point]);
2655 nct6775_write_value(data, data->REG_CRITICAL_TEMP[nr],
2656 data->auto_temp[nr][point]);
2658 mutex_unlock(&data->update_lock);
2663 * The number of auto-point trip points is chip dependent.
2664 * Need to check support while generating/removing attribute files.
2666 static struct sensor_device_attribute_2 sda_auto_pwm_arrays[] = {
2667 SENSOR_ATTR_2(pwm1_auto_point1_pwm, S_IWUSR | S_IRUGO,
2668 show_auto_pwm, store_auto_pwm, 0, 0),
2669 SENSOR_ATTR_2(pwm1_auto_point1_temp, S_IWUSR | S_IRUGO,
2670 show_auto_temp, store_auto_temp, 0, 0),
2671 SENSOR_ATTR_2(pwm1_auto_point1_temp_hyst, S_IWUSR | S_IRUGO,
2672 show_auto_temp_hyst, store_auto_temp_hyst, 0, 0),
2673 SENSOR_ATTR_2(pwm1_auto_point2_pwm, S_IWUSR | S_IRUGO,
2674 show_auto_pwm, store_auto_pwm, 0, 1),
2675 SENSOR_ATTR_2(pwm1_auto_point2_temp, S_IWUSR | S_IRUGO,
2676 show_auto_temp, store_auto_temp, 0, 1),
2677 SENSOR_ATTR_2(pwm1_auto_point2_temp_hyst, S_IWUSR | S_IRUGO,
2678 show_auto_temp_hyst, store_auto_temp_hyst, 0, 1),
2679 SENSOR_ATTR_2(pwm1_auto_point3_pwm, S_IWUSR | S_IRUGO,
2680 show_auto_pwm, store_auto_pwm, 0, 2),
2681 SENSOR_ATTR_2(pwm1_auto_point3_temp, S_IWUSR | S_IRUGO,
2682 show_auto_temp, store_auto_temp, 0, 2),
2683 SENSOR_ATTR_2(pwm1_auto_point3_temp_hyst, S_IWUSR | S_IRUGO,
2684 show_auto_temp_hyst, store_auto_temp_hyst, 0, 2),
2685 SENSOR_ATTR_2(pwm1_auto_point4_pwm, S_IWUSR | S_IRUGO,
2686 show_auto_pwm, store_auto_pwm, 0, 3),
2687 SENSOR_ATTR_2(pwm1_auto_point4_temp, S_IWUSR | S_IRUGO,
2688 show_auto_temp, store_auto_temp, 0, 3),
2689 SENSOR_ATTR_2(pwm1_auto_point4_temp_hyst, S_IWUSR | S_IRUGO,
2690 show_auto_temp_hyst, store_auto_temp_hyst, 0, 3),
2691 SENSOR_ATTR_2(pwm1_auto_point5_pwm, S_IWUSR | S_IRUGO,
2692 show_auto_pwm, store_auto_pwm, 0, 4),
2693 SENSOR_ATTR_2(pwm1_auto_point5_temp, S_IWUSR | S_IRUGO,
2694 show_auto_temp, store_auto_temp, 0, 4),
2695 SENSOR_ATTR_2(pwm1_auto_point5_temp_hyst, S_IWUSR | S_IRUGO,
2696 show_auto_temp_hyst, store_auto_temp_hyst, 0, 4),
2697 SENSOR_ATTR_2(pwm1_auto_point6_pwm, S_IWUSR | S_IRUGO,
2698 show_auto_pwm, store_auto_pwm, 0, 5),
2699 SENSOR_ATTR_2(pwm1_auto_point6_temp, S_IWUSR | S_IRUGO,
2700 show_auto_temp, store_auto_temp, 0, 5),
2701 SENSOR_ATTR_2(pwm1_auto_point6_temp_hyst, S_IWUSR | S_IRUGO,
2702 show_auto_temp_hyst, store_auto_temp_hyst, 0, 5),
2703 SENSOR_ATTR_2(pwm1_auto_point7_pwm, S_IWUSR | S_IRUGO,
2704 show_auto_pwm, store_auto_pwm, 0, 6),
2705 SENSOR_ATTR_2(pwm1_auto_point7_temp, S_IWUSR | S_IRUGO,
2706 show_auto_temp, store_auto_temp, 0, 6),
2707 SENSOR_ATTR_2(pwm1_auto_point7_temp_hyst, S_IWUSR | S_IRUGO,
2708 show_auto_temp_hyst, store_auto_temp_hyst, 0, 6),
2710 SENSOR_ATTR_2(pwm2_auto_point1_pwm, S_IWUSR | S_IRUGO,
2711 show_auto_pwm, store_auto_pwm, 1, 0),
2712 SENSOR_ATTR_2(pwm2_auto_point1_temp, S_IWUSR | S_IRUGO,
2713 show_auto_temp, store_auto_temp, 1, 0),
2714 SENSOR_ATTR_2(pwm2_auto_point1_temp_hyst, S_IWUSR | S_IRUGO,
2715 show_auto_temp_hyst, store_auto_temp_hyst, 1, 0),
2716 SENSOR_ATTR_2(pwm2_auto_point2_pwm, S_IWUSR | S_IRUGO,
2717 show_auto_pwm, store_auto_pwm, 1, 1),
2718 SENSOR_ATTR_2(pwm2_auto_point2_temp, S_IWUSR | S_IRUGO,
2719 show_auto_temp, store_auto_temp, 1, 1),
2720 SENSOR_ATTR_2(pwm2_auto_point2_temp_hyst, S_IWUSR | S_IRUGO,
2721 show_auto_temp_hyst, store_auto_temp_hyst, 1, 1),
2722 SENSOR_ATTR_2(pwm2_auto_point3_pwm, S_IWUSR | S_IRUGO,
2723 show_auto_pwm, store_auto_pwm, 1, 2),
2724 SENSOR_ATTR_2(pwm2_auto_point3_temp, S_IWUSR | S_IRUGO,
2725 show_auto_temp, store_auto_temp, 1, 2),
2726 SENSOR_ATTR_2(pwm2_auto_point3_temp_hyst, S_IWUSR | S_IRUGO,
2727 show_auto_temp_hyst, store_auto_temp_hyst, 1, 2),
2728 SENSOR_ATTR_2(pwm2_auto_point4_pwm, S_IWUSR | S_IRUGO,
2729 show_auto_pwm, store_auto_pwm, 1, 3),
2730 SENSOR_ATTR_2(pwm2_auto_point4_temp, S_IWUSR | S_IRUGO,
2731 show_auto_temp, store_auto_temp, 1, 3),
2732 SENSOR_ATTR_2(pwm2_auto_point4_temp_hyst, S_IWUSR | S_IRUGO,
2733 show_auto_temp_hyst, store_auto_temp_hyst, 1, 3),
2734 SENSOR_ATTR_2(pwm2_auto_point5_pwm, S_IWUSR | S_IRUGO,
2735 show_auto_pwm, store_auto_pwm, 1, 4),
2736 SENSOR_ATTR_2(pwm2_auto_point5_temp, S_IWUSR | S_IRUGO,
2737 show_auto_temp, store_auto_temp, 1, 4),
2738 SENSOR_ATTR_2(pwm2_auto_point5_temp_hyst, S_IWUSR | S_IRUGO,
2739 show_auto_temp_hyst, store_auto_temp_hyst, 1, 4),
2740 SENSOR_ATTR_2(pwm2_auto_point6_pwm, S_IWUSR | S_IRUGO,
2741 show_auto_pwm, store_auto_pwm, 1, 5),
2742 SENSOR_ATTR_2(pwm2_auto_point6_temp, S_IWUSR | S_IRUGO,
2743 show_auto_temp, store_auto_temp, 1, 5),
2744 SENSOR_ATTR_2(pwm2_auto_point6_temp_hyst, S_IWUSR | S_IRUGO,
2745 show_auto_temp_hyst, store_auto_temp_hyst, 1, 5),
2746 SENSOR_ATTR_2(pwm2_auto_point7_pwm, S_IWUSR | S_IRUGO,
2747 show_auto_pwm, store_auto_pwm, 1, 6),
2748 SENSOR_ATTR_2(pwm2_auto_point7_temp, S_IWUSR | S_IRUGO,
2749 show_auto_temp, store_auto_temp, 1, 6),
2750 SENSOR_ATTR_2(pwm2_auto_point7_temp_hyst, S_IWUSR | S_IRUGO,
2751 show_auto_temp_hyst, store_auto_temp_hyst, 1, 6),
2753 SENSOR_ATTR_2(pwm3_auto_point1_pwm, S_IWUSR | S_IRUGO,
2754 show_auto_pwm, store_auto_pwm, 2, 0),
2755 SENSOR_ATTR_2(pwm3_auto_point1_temp, S_IWUSR | S_IRUGO,
2756 show_auto_temp, store_auto_temp, 2, 0),
2757 SENSOR_ATTR_2(pwm3_auto_point1_temp_hyst, S_IWUSR | S_IRUGO,
2758 show_auto_temp_hyst, store_auto_temp_hyst, 2, 0),
2759 SENSOR_ATTR_2(pwm3_auto_point2_pwm, S_IWUSR | S_IRUGO,
2760 show_auto_pwm, store_auto_pwm, 2, 1),
2761 SENSOR_ATTR_2(pwm3_auto_point2_temp, S_IWUSR | S_IRUGO,
2762 show_auto_temp, store_auto_temp, 2, 1),
2763 SENSOR_ATTR_2(pwm3_auto_point2_temp_hyst, S_IWUSR | S_IRUGO,
2764 show_auto_temp_hyst, store_auto_temp_hyst, 2, 1),
2765 SENSOR_ATTR_2(pwm3_auto_point3_pwm, S_IWUSR | S_IRUGO,
2766 show_auto_pwm, store_auto_pwm, 2, 2),
2767 SENSOR_ATTR_2(pwm3_auto_point3_temp, S_IWUSR | S_IRUGO,
2768 show_auto_temp, store_auto_temp, 2, 2),
2769 SENSOR_ATTR_2(pwm3_auto_point3_temp_hyst, S_IWUSR | S_IRUGO,
2770 show_auto_temp_hyst, store_auto_temp_hyst, 2, 2),
2771 SENSOR_ATTR_2(pwm3_auto_point4_pwm, S_IWUSR | S_IRUGO,
2772 show_auto_pwm, store_auto_pwm, 2, 3),
2773 SENSOR_ATTR_2(pwm3_auto_point4_temp, S_IWUSR | S_IRUGO,
2774 show_auto_temp, store_auto_temp, 2, 3),
2775 SENSOR_ATTR_2(pwm3_auto_point4_temp_hyst, S_IWUSR | S_IRUGO,
2776 show_auto_temp_hyst, store_auto_temp_hyst, 2, 3),
2777 SENSOR_ATTR_2(pwm3_auto_point5_pwm, S_IWUSR | S_IRUGO,
2778 show_auto_pwm, store_auto_pwm, 2, 4),
2779 SENSOR_ATTR_2(pwm3_auto_point5_temp, S_IWUSR | S_IRUGO,
2780 show_auto_temp, store_auto_temp, 2, 4),
2781 SENSOR_ATTR_2(pwm3_auto_point5_temp_hyst, S_IWUSR | S_IRUGO,
2782 show_auto_temp_hyst, store_auto_temp_hyst, 2, 4),
2783 SENSOR_ATTR_2(pwm3_auto_point6_pwm, S_IWUSR | S_IRUGO,
2784 show_auto_pwm, store_auto_pwm, 2, 5),
2785 SENSOR_ATTR_2(pwm3_auto_point6_temp, S_IWUSR | S_IRUGO,
2786 show_auto_temp, store_auto_temp, 2, 5),
2787 SENSOR_ATTR_2(pwm3_auto_point6_temp_hyst, S_IWUSR | S_IRUGO,
2788 show_auto_temp_hyst, store_auto_temp_hyst, 2, 5),
2789 SENSOR_ATTR_2(pwm3_auto_point7_pwm, S_IWUSR | S_IRUGO,
2790 show_auto_pwm, store_auto_pwm, 2, 6),
2791 SENSOR_ATTR_2(pwm3_auto_point7_temp, S_IWUSR | S_IRUGO,
2792 show_auto_temp, store_auto_temp, 2, 6),
2793 SENSOR_ATTR_2(pwm3_auto_point7_temp_hyst, S_IWUSR | S_IRUGO,
2794 show_auto_temp_hyst, store_auto_temp_hyst, 2, 6),
2796 SENSOR_ATTR_2(pwm4_auto_point1_pwm, S_IWUSR | S_IRUGO,
2797 show_auto_pwm, store_auto_pwm, 3, 0),
2798 SENSOR_ATTR_2(pwm4_auto_point1_temp, S_IWUSR | S_IRUGO,
2799 show_auto_temp, store_auto_temp, 3, 0),
2800 SENSOR_ATTR_2(pwm4_auto_point1_temp_hyst, S_IWUSR | S_IRUGO,
2801 show_auto_temp_hyst, store_auto_temp_hyst, 3, 0),
2802 SENSOR_ATTR_2(pwm4_auto_point2_pwm, S_IWUSR | S_IRUGO,
2803 show_auto_pwm, store_auto_pwm, 3, 1),
2804 SENSOR_ATTR_2(pwm4_auto_point2_temp, S_IWUSR | S_IRUGO,
2805 show_auto_temp, store_auto_temp, 3, 1),
2806 SENSOR_ATTR_2(pwm4_auto_point2_temp_hyst, S_IWUSR | S_IRUGO,
2807 show_auto_temp_hyst, store_auto_temp_hyst, 3, 1),
2808 SENSOR_ATTR_2(pwm4_auto_point3_pwm, S_IWUSR | S_IRUGO,
2809 show_auto_pwm, store_auto_pwm, 3, 2),
2810 SENSOR_ATTR_2(pwm4_auto_point3_temp, S_IWUSR | S_IRUGO,
2811 show_auto_temp, store_auto_temp, 3, 2),
2812 SENSOR_ATTR_2(pwm4_auto_point3_temp_hyst, S_IWUSR | S_IRUGO,
2813 show_auto_temp_hyst, store_auto_temp_hyst, 3, 2),
2814 SENSOR_ATTR_2(pwm4_auto_point4_pwm, S_IWUSR | S_IRUGO,
2815 show_auto_pwm, store_auto_pwm, 3, 3),
2816 SENSOR_ATTR_2(pwm4_auto_point4_temp, S_IWUSR | S_IRUGO,
2817 show_auto_temp, store_auto_temp, 3, 3),
2818 SENSOR_ATTR_2(pwm4_auto_point4_temp_hyst, S_IWUSR | S_IRUGO,
2819 show_auto_temp_hyst, store_auto_temp_hyst, 3, 3),
2820 SENSOR_ATTR_2(pwm4_auto_point5_pwm, S_IWUSR | S_IRUGO,
2821 show_auto_pwm, store_auto_pwm, 3, 4),
2822 SENSOR_ATTR_2(pwm4_auto_point5_temp, S_IWUSR | S_IRUGO,
2823 show_auto_temp, store_auto_temp, 3, 4),
2824 SENSOR_ATTR_2(pwm4_auto_point5_temp_hyst, S_IWUSR | S_IRUGO,
2825 show_auto_temp_hyst, store_auto_temp_hyst, 3, 4),
2826 SENSOR_ATTR_2(pwm4_auto_point6_pwm, S_IWUSR | S_IRUGO,
2827 show_auto_pwm, store_auto_pwm, 3, 5),
2828 SENSOR_ATTR_2(pwm4_auto_point6_temp, S_IWUSR | S_IRUGO,
2829 show_auto_temp, store_auto_temp, 3, 5),
2830 SENSOR_ATTR_2(pwm4_auto_point6_temp_hyst, S_IWUSR | S_IRUGO,
2831 show_auto_temp_hyst, store_auto_temp_hyst, 3, 5),
2832 SENSOR_ATTR_2(pwm4_auto_point7_pwm, S_IWUSR | S_IRUGO,
2833 show_auto_pwm, store_auto_pwm, 3, 6),
2834 SENSOR_ATTR_2(pwm4_auto_point7_temp, S_IWUSR | S_IRUGO,
2835 show_auto_temp, store_auto_temp, 3, 6),
2836 SENSOR_ATTR_2(pwm4_auto_point7_temp_hyst, S_IWUSR | S_IRUGO,
2837 show_auto_temp_hyst, store_auto_temp_hyst, 3, 6),
2839 SENSOR_ATTR_2(pwm5_auto_point1_pwm, S_IWUSR | S_IRUGO,
2840 show_auto_pwm, store_auto_pwm, 4, 0),
2841 SENSOR_ATTR_2(pwm5_auto_point1_temp, S_IWUSR | S_IRUGO,
2842 show_auto_temp, store_auto_temp, 4, 0),
2843 SENSOR_ATTR_2(pwm5_auto_point1_temp_hyst, S_IWUSR | S_IRUGO,
2844 show_auto_temp_hyst, store_auto_temp_hyst, 4, 0),
2845 SENSOR_ATTR_2(pwm5_auto_point2_pwm, S_IWUSR | S_IRUGO,
2846 show_auto_pwm, store_auto_pwm, 4, 1),
2847 SENSOR_ATTR_2(pwm5_auto_point2_temp, S_IWUSR | S_IRUGO,
2848 show_auto_temp, store_auto_temp, 4, 1),
2849 SENSOR_ATTR_2(pwm5_auto_point2_temp_hyst, S_IWUSR | S_IRUGO,
2850 show_auto_temp_hyst, store_auto_temp_hyst, 4, 1),
2851 SENSOR_ATTR_2(pwm5_auto_point3_pwm, S_IWUSR | S_IRUGO,
2852 show_auto_pwm, store_auto_pwm, 4, 2),
2853 SENSOR_ATTR_2(pwm5_auto_point3_temp, S_IWUSR | S_IRUGO,
2854 show_auto_temp, store_auto_temp, 4, 2),
2855 SENSOR_ATTR_2(pwm5_auto_point3_temp_hyst, S_IWUSR | S_IRUGO,
2856 show_auto_temp_hyst, store_auto_temp_hyst, 4, 2),
2857 SENSOR_ATTR_2(pwm5_auto_point4_pwm, S_IWUSR | S_IRUGO,
2858 show_auto_pwm, store_auto_pwm, 4, 3),
2859 SENSOR_ATTR_2(pwm5_auto_point4_temp, S_IWUSR | S_IRUGO,
2860 show_auto_temp, store_auto_temp, 4, 3),
2861 SENSOR_ATTR_2(pwm5_auto_point4_temp_hyst, S_IWUSR | S_IRUGO,
2862 show_auto_temp_hyst, store_auto_temp_hyst, 4, 3),
2863 SENSOR_ATTR_2(pwm5_auto_point5_pwm, S_IWUSR | S_IRUGO,
2864 show_auto_pwm, store_auto_pwm, 4, 4),
2865 SENSOR_ATTR_2(pwm5_auto_point5_temp, S_IWUSR | S_IRUGO,
2866 show_auto_temp, store_auto_temp, 4, 4),
2867 SENSOR_ATTR_2(pwm5_auto_point5_temp_hyst, S_IWUSR | S_IRUGO,
2868 show_auto_temp_hyst, store_auto_temp_hyst, 4, 4),
2869 SENSOR_ATTR_2(pwm5_auto_point6_pwm, S_IWUSR | S_IRUGO,
2870 show_auto_pwm, store_auto_pwm, 4, 5),
2871 SENSOR_ATTR_2(pwm5_auto_point6_temp, S_IWUSR | S_IRUGO,
2872 show_auto_temp, store_auto_temp, 4, 5),
2873 SENSOR_ATTR_2(pwm5_auto_point6_temp_hyst, S_IWUSR | S_IRUGO,
2874 show_auto_temp_hyst, store_auto_temp_hyst, 4, 5),
2875 SENSOR_ATTR_2(pwm5_auto_point7_pwm, S_IWUSR | S_IRUGO,
2876 show_auto_pwm, store_auto_pwm, 4, 6),
2877 SENSOR_ATTR_2(pwm5_auto_point7_temp, S_IWUSR | S_IRUGO,
2878 show_auto_temp, store_auto_temp, 4, 6),
2879 SENSOR_ATTR_2(pwm5_auto_point7_temp_hyst, S_IWUSR | S_IRUGO,
2880 show_auto_temp_hyst, store_auto_temp_hyst, 4, 6),
2884 show_vid(struct device *dev, struct device_attribute *attr, char *buf)
2886 struct nct6775_data *data = dev_get_drvdata(dev);
2887 return sprintf(buf, "%d\n", vid_from_reg(data->vid, data->vrm));
2890 static DEVICE_ATTR(cpu0_vid, S_IRUGO, show_vid, NULL);
2892 /* Case open detection */
2895 clear_caseopen(struct device *dev, struct device_attribute *attr,
2896 const char *buf, size_t count)
2898 struct nct6775_data *data = dev_get_drvdata(dev);
2899 struct nct6775_sio_data *sio_data = dev->platform_data;
2900 int nr = to_sensor_dev_attr(attr)->index - INTRUSION_ALARM_BASE;
2905 if (kstrtoul(buf, 10, &val) || val != 0)
2908 mutex_lock(&data->update_lock);
2911 * Use CR registers to clear caseopen status.
2912 * The CR registers are the same for all chips, and not all chips
2913 * support clearing the caseopen status through "regular" registers.
2915 ret = superio_enter(sio_data->sioreg);
2921 superio_select(sio_data->sioreg, NCT6775_LD_ACPI);
2922 reg = superio_inb(sio_data->sioreg, NCT6775_REG_CR_CASEOPEN_CLR[nr]);
2923 reg |= NCT6775_CR_CASEOPEN_CLR_MASK[nr];
2924 superio_outb(sio_data->sioreg, NCT6775_REG_CR_CASEOPEN_CLR[nr], reg);
2925 reg &= ~NCT6775_CR_CASEOPEN_CLR_MASK[nr];
2926 superio_outb(sio_data->sioreg, NCT6775_REG_CR_CASEOPEN_CLR[nr], reg);
2927 superio_exit(sio_data->sioreg);
2929 data->valid = 0; /* Force cache refresh */
2931 mutex_unlock(&data->update_lock);
2935 static struct sensor_device_attribute sda_caseopen[] = {
2936 SENSOR_ATTR(intrusion0_alarm, S_IWUSR | S_IRUGO, show_alarm,
2937 clear_caseopen, INTRUSION_ALARM_BASE),
2938 SENSOR_ATTR(intrusion1_alarm, S_IWUSR | S_IRUGO, show_alarm,
2939 clear_caseopen, INTRUSION_ALARM_BASE + 1),
2943 * Driver and device management
2946 static void nct6775_device_remove_files(struct device *dev)
2949 * some entries in the following arrays may not have been used in
2950 * device_create_file(), but device_remove_file() will ignore them
2953 struct nct6775_data *data = dev_get_drvdata(dev);
2955 for (i = 0; i < data->pwm_num; i++)
2956 sysfs_remove_group(&dev->kobj, &nct6775_group_pwm[i]);
2958 for (i = 0; i < ARRAY_SIZE(sda_max_output); i++)
2959 device_remove_file(dev, &sda_max_output[i].dev_attr);
2961 for (i = 0; i < ARRAY_SIZE(sda_step_output); i++)
2962 device_remove_file(dev, &sda_max_output[i].dev_attr);
2964 for (i = 0; i < ARRAY_SIZE(sda_weight_duty_base); i++)
2965 device_remove_file(dev, &sda_weight_duty_base[i].dev_attr);
2967 for (i = 0; i < ARRAY_SIZE(sda_auto_pwm_arrays); i++)
2968 device_remove_file(dev, &sda_auto_pwm_arrays[i].dev_attr);
2970 for (i = 0; i < data->in_num; i++)
2971 sysfs_remove_group(&dev->kobj, &nct6775_group_in[i]);
2973 for (i = 0; i < 5; i++) {
2974 device_remove_file(dev, &sda_fan_input[i].dev_attr);
2975 device_remove_file(dev, &sda_fan_alarm[i].dev_attr);
2976 device_remove_file(dev, &sda_fan_div[i].dev_attr);
2977 device_remove_file(dev, &sda_fan_min[i].dev_attr);
2979 for (i = 0; i < NUM_TEMP; i++) {
2980 if (!(data->have_temp & (1 << i)))
2982 device_remove_file(dev, &sda_temp_input[i].dev_attr);
2983 device_remove_file(dev, &sda_temp_label[i].dev_attr);
2984 device_remove_file(dev, &sda_temp_max[i].dev_attr);
2985 device_remove_file(dev, &sda_temp_max_hyst[i].dev_attr);
2986 device_remove_file(dev, &sda_temp_crit[i].dev_attr);
2987 if (!(data->have_temp_fixed & (1 << i)))
2989 device_remove_file(dev, &sda_temp_type[i].dev_attr);
2990 device_remove_file(dev, &sda_temp_offset[i].dev_attr);
2991 if (i >= NUM_TEMP_ALARM)
2993 device_remove_file(dev, &sda_temp_alarm[i].dev_attr);
2996 device_remove_file(dev, &sda_caseopen[0].dev_attr);
2997 device_remove_file(dev, &sda_caseopen[1].dev_attr);
2999 device_remove_file(dev, &dev_attr_name);
3000 device_remove_file(dev, &dev_attr_cpu0_vid);
3003 /* Get the monitoring functions started */
3004 static inline void __devinit nct6775_init_device(struct nct6775_data *data)
3009 /* Start monitoring if needed */
3010 if (data->REG_CONFIG) {
3011 tmp = nct6775_read_value(data, data->REG_CONFIG);
3013 nct6775_write_value(data, data->REG_CONFIG, tmp | 0x01);
3016 /* Enable temperature sensors if needed */
3017 for (i = 0; i < NUM_TEMP; i++) {
3018 if (!(data->have_temp & (1 << i)))
3020 if (!data->reg_temp_config[i])
3022 tmp = nct6775_read_value(data, data->reg_temp_config[i]);
3024 nct6775_write_value(data, data->reg_temp_config[i],
3028 /* Enable VBAT monitoring if needed */
3029 tmp = nct6775_read_value(data, data->REG_VBAT);
3031 nct6775_write_value(data, data->REG_VBAT, tmp | 0x01);
3033 diode = nct6775_read_value(data, data->REG_DIODE);
3035 for (i = 0; i < data->temp_fixed_num; i++) {
3036 if (!(data->have_temp_fixed & (1 << i)))
3038 if ((tmp & (0x02 << i))) /* diode */
3039 data->temp_type[i] = 3 - ((diode >> i) & 0x02);
3040 else /* thermistor */
3041 data->temp_type[i] = 4;
3045 static int __devinit
3046 nct6775_check_fan_inputs(const struct nct6775_sio_data *sio_data,
3047 struct nct6775_data *data)
3050 bool fan3pin, fan3min, fan4pin, fan4min, fan5pin;
3051 bool pwm3pin, pwm4pin, pwm5pin;
3054 ret = superio_enter(sio_data->sioreg);
3058 /* fan4 and fan5 share some pins with the GPIO and serial flash */
3059 if (data->kind == nct6775) {
3060 regval = superio_inb(sio_data->sioreg, 0x2c);
3062 fan3pin = regval & (1 << 6);
3064 pwm3pin = regval & (1 << 7);
3066 /* On NCT6775, fan4 shares pins with the fdc interface */
3067 fan4pin = !(superio_inb(sio_data->sioreg, 0x2A) & 0x80);
3072 } else if (data->kind == nct6776) {
3073 bool gpok = superio_inb(sio_data->sioreg, 0x27) & 0x80;
3075 superio_select(sio_data->sioreg, NCT6775_LD_HWM);
3076 regval = superio_inb(sio_data->sioreg, SIO_REG_ENABLE);
3081 fan3pin = !(superio_inb(sio_data->sioreg, 0x24) & 0x40);
3086 fan4pin = superio_inb(sio_data->sioreg, 0x1C) & 0x01;
3091 fan5pin = superio_inb(sio_data->sioreg, 0x1C) & 0x02;
3098 } else { /* NCT6779D */
3099 regval = superio_inb(sio_data->sioreg, 0x1c);
3101 fan3pin = !(regval & (1 << 5));
3102 fan4pin = !(regval & (1 << 6));
3103 fan5pin = !(regval & (1 << 7));
3105 pwm3pin = !(regval & (1 << 0));
3106 pwm4pin = !(regval & (1 << 1));
3107 pwm5pin = !(regval & (1 << 2));
3113 superio_exit(sio_data->sioreg);
3115 data->has_fan = data->has_fan_min = 0x03; /* fan1 and fan2 */
3116 data->has_fan |= fan3pin << 2;
3117 data->has_fan_min |= fan3min << 2;
3119 data->has_fan |= (fan4pin << 3) | (fan5pin << 4);
3120 data->has_fan_min |= (fan4min << 3) | (fan5pin << 4);
3122 data->has_pwm = 0x03 | (pwm3pin << 2) | (pwm4pin << 3) | (pwm5pin << 4);
3127 static int __devinit nct6775_probe(struct platform_device *pdev)
3129 struct device *dev = &pdev->dev;
3130 struct nct6775_sio_data *sio_data = dev->platform_data;
3131 struct nct6775_data *data;
3132 struct resource *res;
3134 int src, mask, available;
3135 const u16 *reg_temp, *reg_temp_over, *reg_temp_hyst, *reg_temp_config;
3136 const u16 *reg_temp_alternate, *reg_temp_crit;
3139 res = platform_get_resource(pdev, IORESOURCE_IO, 0);
3140 if (!devm_request_region(&pdev->dev, res->start, IOREGION_LENGTH,
3144 data = devm_kzalloc(&pdev->dev, sizeof(struct nct6775_data),
3149 data->kind = sio_data->kind;
3150 data->addr = res->start;
3151 mutex_init(&data->lock);
3152 mutex_init(&data->update_lock);
3153 data->name = nct6775_device_names[data->kind];
3154 platform_set_drvdata(pdev, data);
3156 switch (data->kind) {
3160 data->auto_pwm_num = 6;
3161 data->has_fan_div = true;
3162 data->temp_fixed_num = 3;
3164 data->ALARM_BITS = NCT6775_ALARM_BITS;
3166 data->fan_from_reg = fan_from_reg16;
3167 data->fan_from_reg_min = fan_from_reg8;
3169 data->temp_label = nct6775_temp_label;
3170 data->temp_label_num = ARRAY_SIZE(nct6775_temp_label);
3172 data->REG_CONFIG = NCT6775_REG_CONFIG;
3173 data->REG_VBAT = NCT6775_REG_VBAT;
3174 data->REG_DIODE = NCT6775_REG_DIODE;
3175 data->REG_VIN = NCT6775_REG_IN;
3176 data->REG_IN_MINMAX[0] = NCT6775_REG_IN_MIN;
3177 data->REG_IN_MINMAX[1] = NCT6775_REG_IN_MAX;
3178 data->REG_TARGET = NCT6775_REG_TARGET;
3179 data->REG_FAN = NCT6775_REG_FAN;
3180 data->REG_FAN_MODE = NCT6775_REG_FAN_MODE;
3181 data->REG_FAN_MIN = NCT6775_REG_FAN_MIN;
3182 data->REG_FAN_TIME[0] = NCT6775_REG_FAN_STOP_TIME;
3183 data->REG_FAN_TIME[1] = NCT6775_REG_FAN_STEP_UP_TIME;
3184 data->REG_FAN_TIME[2] = NCT6775_REG_FAN_STEP_DOWN_TIME;
3185 data->REG_PWM[0] = NCT6775_REG_PWM;
3186 data->REG_PWM[1] = NCT6775_REG_FAN_START_OUTPUT;
3187 data->REG_PWM[2] = NCT6775_REG_FAN_STOP_OUTPUT;
3188 data->REG_PWM[3] = NCT6775_REG_FAN_MAX_OUTPUT;
3189 data->REG_PWM[4] = NCT6775_REG_FAN_STEP_OUTPUT;
3190 data->REG_PWM[5] = NCT6775_REG_WEIGHT_DUTY_STEP;
3191 data->REG_PWM_READ = NCT6775_REG_PWM_READ;
3192 data->REG_PWM_MODE = NCT6775_REG_PWM_MODE;
3193 data->PWM_MODE_MASK = NCT6775_PWM_MODE_MASK;
3194 data->REG_AUTO_TEMP = NCT6775_REG_AUTO_TEMP;
3195 data->REG_AUTO_PWM = NCT6775_REG_AUTO_PWM;
3196 data->REG_CRITICAL_TEMP = NCT6775_REG_CRITICAL_TEMP;
3197 data->REG_CRITICAL_TEMP_TOLERANCE
3198 = NCT6775_REG_CRITICAL_TEMP_TOLERANCE;
3199 data->REG_TEMP_OFFSET = NCT6775_REG_TEMP_OFFSET;
3200 data->REG_TEMP_SOURCE = NCT6775_REG_TEMP_SOURCE;
3201 data->REG_TEMP_SEL[0] = NCT6775_REG_TEMP_SEL;
3202 data->REG_TEMP_SEL[1] = NCT6775_REG_WEIGHT_TEMP_SEL;
3203 data->REG_TEMP_SEL[2] = NCT6775_REG_FAN_STEP_ENABLE;
3204 data->REG_WEIGHT_TEMP[0] = NCT6775_REG_WEIGHT_TEMP_STEP;
3205 data->REG_WEIGHT_TEMP[1] = NCT6775_REG_WEIGHT_TEMP_STEP_TOL;
3206 data->REG_WEIGHT_TEMP[2] = NCT6775_REG_WEIGHT_TEMP_BASE;
3207 data->REG_ALARM = NCT6775_REG_ALARM;
3209 reg_temp = NCT6775_REG_TEMP;
3210 num_reg_temp = ARRAY_SIZE(NCT6775_REG_TEMP);
3211 reg_temp_over = NCT6775_REG_TEMP_OVER;
3212 reg_temp_hyst = NCT6775_REG_TEMP_HYST;
3213 reg_temp_config = NCT6775_REG_TEMP_CONFIG;
3214 reg_temp_alternate = NCT6775_REG_TEMP_ALTERNATE;
3215 reg_temp_crit = NCT6775_REG_TEMP_CRIT;
3221 data->auto_pwm_num = 4;
3222 data->has_fan_div = false;
3223 data->temp_fixed_num = 3;
3225 data->ALARM_BITS = NCT6776_ALARM_BITS;
3227 data->fan_from_reg = fan_from_reg13;
3228 data->fan_from_reg_min = fan_from_reg13;
3230 data->temp_label = nct6776_temp_label;
3231 data->temp_label_num = ARRAY_SIZE(nct6776_temp_label);
3233 data->REG_CONFIG = NCT6775_REG_CONFIG;
3234 data->REG_VBAT = NCT6775_REG_VBAT;
3235 data->REG_DIODE = NCT6775_REG_DIODE;
3236 data->REG_VIN = NCT6775_REG_IN;
3237 data->REG_IN_MINMAX[0] = NCT6775_REG_IN_MIN;
3238 data->REG_IN_MINMAX[1] = NCT6775_REG_IN_MAX;
3239 data->REG_TARGET = NCT6775_REG_TARGET;
3240 data->REG_FAN = NCT6775_REG_FAN;
3241 data->REG_FAN_MODE = NCT6775_REG_FAN_MODE;
3242 data->REG_FAN_MIN = NCT6776_REG_FAN_MIN;
3243 data->REG_FAN_TIME[0] = NCT6775_REG_FAN_STOP_TIME;
3244 data->REG_FAN_TIME[1] = NCT6775_REG_FAN_STEP_UP_TIME;
3245 data->REG_FAN_TIME[2] = NCT6775_REG_FAN_STEP_DOWN_TIME;
3246 data->REG_PWM[0] = NCT6775_REG_PWM;
3247 data->REG_PWM[1] = NCT6775_REG_FAN_START_OUTPUT;
3248 data->REG_PWM[2] = NCT6775_REG_FAN_STOP_OUTPUT;
3249 data->REG_PWM[5] = NCT6775_REG_WEIGHT_DUTY_STEP;
3250 data->REG_PWM[6] = NCT6776_REG_WEIGHT_DUTY_BASE;
3251 data->REG_PWM_READ = NCT6775_REG_PWM_READ;
3252 data->REG_PWM_MODE = NCT6776_REG_PWM_MODE;
3253 data->PWM_MODE_MASK = NCT6776_PWM_MODE_MASK;
3254 data->REG_AUTO_TEMP = NCT6775_REG_AUTO_TEMP;
3255 data->REG_AUTO_PWM = NCT6775_REG_AUTO_PWM;
3256 data->REG_CRITICAL_TEMP = NCT6775_REG_CRITICAL_TEMP;
3257 data->REG_CRITICAL_TEMP_TOLERANCE
3258 = NCT6775_REG_CRITICAL_TEMP_TOLERANCE;
3259 data->REG_TEMP_OFFSET = NCT6775_REG_TEMP_OFFSET;
3260 data->REG_TEMP_SOURCE = NCT6775_REG_TEMP_SOURCE;
3261 data->REG_TEMP_SEL[0] = NCT6775_REG_TEMP_SEL;
3262 data->REG_TEMP_SEL[1] = NCT6775_REG_WEIGHT_TEMP_SEL;
3263 data->REG_TEMP_SEL[2] = NCT6775_REG_FAN_STEP_ENABLE;
3264 data->REG_WEIGHT_TEMP[0] = NCT6775_REG_WEIGHT_TEMP_STEP;
3265 data->REG_WEIGHT_TEMP[1] = NCT6775_REG_WEIGHT_TEMP_STEP_TOL;
3266 data->REG_WEIGHT_TEMP[2] = NCT6775_REG_WEIGHT_TEMP_BASE;
3267 data->REG_ALARM = NCT6775_REG_ALARM;
3269 reg_temp = NCT6775_REG_TEMP;
3270 num_reg_temp = ARRAY_SIZE(NCT6775_REG_TEMP);
3271 reg_temp_over = NCT6775_REG_TEMP_OVER;
3272 reg_temp_hyst = NCT6775_REG_TEMP_HYST;
3273 reg_temp_config = NCT6776_REG_TEMP_CONFIG;
3274 reg_temp_alternate = NCT6776_REG_TEMP_ALTERNATE;
3275 reg_temp_crit = NCT6776_REG_TEMP_CRIT;
3281 data->auto_pwm_num = 4;
3282 data->has_fan_div = false;
3283 data->temp_fixed_num = 6;
3285 data->ALARM_BITS = NCT6779_ALARM_BITS;
3287 data->fan_from_reg = fan_from_reg13;
3288 data->fan_from_reg_min = fan_from_reg13;
3290 data->temp_label = nct6779_temp_label;
3291 data->temp_label_num = ARRAY_SIZE(nct6779_temp_label);
3293 data->REG_CONFIG = NCT6775_REG_CONFIG;
3294 data->REG_VBAT = NCT6775_REG_VBAT;
3295 data->REG_DIODE = NCT6775_REG_DIODE;
3296 data->REG_VIN = NCT6779_REG_IN;
3297 data->REG_IN_MINMAX[0] = NCT6775_REG_IN_MIN;
3298 data->REG_IN_MINMAX[1] = NCT6775_REG_IN_MAX;
3299 data->REG_TARGET = NCT6775_REG_TARGET;
3300 data->REG_FAN = NCT6779_REG_FAN;
3301 data->REG_FAN_MODE = NCT6775_REG_FAN_MODE;
3302 data->REG_FAN_MIN = NCT6776_REG_FAN_MIN;
3303 data->REG_FAN_TIME[0] = NCT6775_REG_FAN_STOP_TIME;
3304 data->REG_FAN_TIME[1] = NCT6775_REG_FAN_STEP_UP_TIME;
3305 data->REG_FAN_TIME[2] = NCT6775_REG_FAN_STEP_DOWN_TIME;
3306 data->REG_PWM[0] = NCT6775_REG_PWM;
3307 data->REG_PWM[1] = NCT6775_REG_FAN_START_OUTPUT;
3308 data->REG_PWM[2] = NCT6775_REG_FAN_STOP_OUTPUT;
3309 data->REG_PWM[5] = NCT6775_REG_WEIGHT_DUTY_STEP;
3310 data->REG_PWM[6] = NCT6776_REG_WEIGHT_DUTY_BASE;
3311 data->REG_PWM_READ = NCT6775_REG_PWM_READ;
3312 data->REG_PWM_MODE = NCT6776_REG_PWM_MODE;
3313 data->PWM_MODE_MASK = NCT6776_PWM_MODE_MASK;
3314 data->REG_AUTO_TEMP = NCT6775_REG_AUTO_TEMP;
3315 data->REG_AUTO_PWM = NCT6775_REG_AUTO_PWM;
3316 data->REG_CRITICAL_TEMP = NCT6775_REG_CRITICAL_TEMP;
3317 data->REG_CRITICAL_TEMP_TOLERANCE
3318 = NCT6775_REG_CRITICAL_TEMP_TOLERANCE;
3319 data->REG_TEMP_OFFSET = NCT6779_REG_TEMP_OFFSET;
3320 data->REG_TEMP_SOURCE = NCT6775_REG_TEMP_SOURCE;
3321 data->REG_TEMP_SEL[0] = NCT6775_REG_TEMP_SEL;
3322 data->REG_TEMP_SEL[1] = NCT6775_REG_WEIGHT_TEMP_SEL;
3323 data->REG_TEMP_SEL[2] = NCT6775_REG_FAN_STEP_ENABLE;
3324 data->REG_WEIGHT_TEMP[0] = NCT6775_REG_WEIGHT_TEMP_STEP;
3325 data->REG_WEIGHT_TEMP[1] = NCT6775_REG_WEIGHT_TEMP_STEP_TOL;
3326 data->REG_WEIGHT_TEMP[2] = NCT6775_REG_WEIGHT_TEMP_BASE;
3327 data->REG_ALARM = NCT6779_REG_ALARM;
3329 reg_temp = NCT6779_REG_TEMP;
3330 num_reg_temp = ARRAY_SIZE(NCT6779_REG_TEMP);
3331 reg_temp_over = NCT6779_REG_TEMP_OVER;
3332 reg_temp_hyst = NCT6779_REG_TEMP_HYST;
3333 reg_temp_config = NCT6779_REG_TEMP_CONFIG;
3334 reg_temp_alternate = NCT6779_REG_TEMP_ALTERNATE;
3335 reg_temp_crit = NCT6779_REG_TEMP_CRIT;
3341 data->have_in = (1 << data->in_num) - 1;
3342 data->have_temp = 0;
3345 * On some boards, not all available temperature sources are monitored,
3346 * even though some of the monitoring registers are unused.
3347 * Get list of unused monitoring registers, then detect if any fan
3348 * controls are configured to use unmonitored temperature sources.
3349 * If so, assign the unmonitored temperature sources to available
3350 * monitoring registers.
3354 for (i = 0; i < num_reg_temp; i++) {
3355 if (reg_temp[i] == 0)
3358 src = nct6775_read_value(data, data->REG_TEMP_SOURCE[i]) & 0x1f;
3359 if (!src || (mask & (1 << src)))
3360 available |= 1 << i;
3366 * Now find unmonitored temperature registers and enable monitoring
3367 * if additional monitoring registers are available.
3369 for (i = 0; i < ARRAY_SIZE(data->REG_TEMP_SEL) && available; i++) {
3372 if (!data->REG_TEMP_SEL[i])
3374 for (j = 0; j < data->pwm_num && available; j++) {
3377 if (!data->REG_TEMP_SEL[i][j])
3379 src = nct6775_read_value(data,
3380 data->REG_TEMP_SEL[i][j]);
3382 if (!src || (mask & (1 << src)))
3384 if (src >= data->temp_label_num ||
3385 !strlen(data->temp_label[src])) {
3387 "Select source %d:%d reg 0x%x invalid (%d)\n",
3388 i, j, data->REG_TEMP_SEL[i][j], src);
3392 index = __ffs(available);
3393 nct6775_write_value(data,
3394 data->REG_TEMP_SOURCE[index],
3396 available &= ~(1 << index);
3402 s = NUM_TEMP_FIXED; /* First dynamic temperature attribute */
3403 for (i = 0; i < num_reg_temp; i++) {
3404 if (reg_temp[i] == 0)
3407 src = nct6775_read_value(data, data->REG_TEMP_SOURCE[i]) & 0x1f;
3408 if (!src || (mask & (1 << src)))
3411 if (src >= data->temp_label_num ||
3412 !strlen(data->temp_label[src])) {
3414 "Invalid temperature source %d at index %d, source register 0x%x, temp register 0x%x\n",
3415 src, i, data->REG_TEMP_SOURCE[i], reg_temp[i]);
3421 /* Use fixed index for SYSTIN(1), CPUTIN(2), AUXTIN(3) */
3422 if (src <= data->temp_fixed_num) {
3423 data->have_temp |= 1 << (src - 1);
3424 data->have_temp_fixed |= 1 << (src - 1);
3425 data->reg_temp[0][src - 1] = reg_temp[i];
3426 data->reg_temp[1][src - 1] = reg_temp_over[i];
3427 data->reg_temp[2][src - 1] = reg_temp_hyst[i];
3428 data->reg_temp_config[src - 1] = reg_temp_config[i];
3429 data->temp_src[src - 1] = src;
3436 /* Use dynamic index for other sources */
3437 data->have_temp |= 1 << s;
3438 data->reg_temp[0][s] = reg_temp[i];
3439 data->reg_temp[1][s] = reg_temp_over[i];
3440 data->reg_temp[2][s] = reg_temp_hyst[i];
3441 data->reg_temp_config[s] = reg_temp_config[i];
3442 if (reg_temp_crit[src - 1])
3443 data->reg_temp[3][s] = reg_temp_crit[src - 1];
3445 data->temp_src[s] = src;
3451 * Go through the list of alternate temp registers and enable
3453 * The temperature is already monitored if the respective bit in <mask>
3456 for (i = 0; i < data->temp_label_num - 1; i++) {
3457 if (!reg_temp_alternate[i])
3459 if (mask & (1 << (i + 1)))
3461 if (i < data->temp_fixed_num) {
3462 if (data->have_temp & (1 << i))
3464 data->have_temp |= 1 << i;
3465 data->have_temp_fixed |= 1 << i;
3466 data->reg_temp[0][i] = reg_temp_alternate[i];
3467 data->reg_temp[1][i] = reg_temp_over[i];
3468 data->reg_temp[2][i] = reg_temp_hyst[i];
3469 data->temp_src[i] = i + 1;
3473 if (s >= NUM_TEMP) /* Abort if no more space */
3476 data->have_temp |= 1 << s;
3477 data->reg_temp[0][s] = reg_temp_alternate[i];
3478 data->temp_src[s] = i + 1;
3481 #endif /* TESTING */
3483 switch (data->kind) {
3488 * On NCT6776, AUXTIN and VIN3 pins are shared.
3489 * Only way to detect it is to check if AUXTIN is used
3490 * as a temperature source, and if that source is
3493 * If that is the case, disable in6, which reports VIN3.
3494 * Otherwise disable temp3.
3496 if (data->have_temp & (1 << 2)) {
3497 u8 reg = nct6775_read_value(data,
3498 data->reg_temp_config[2]);
3500 data->have_temp &= ~(1 << 2);
3502 data->have_in &= ~(1 << 6);
3513 * There does not seem to be a clean way to detect if VINx or
3514 * AUXTINx is active, so for keep both sensor types enabled
3520 /* Initialize the chip */
3521 nct6775_init_device(data);
3523 data->vrm = vid_which_vrm();
3524 err = superio_enter(sio_data->sioreg);
3530 * We can get the VID input values directly at logical device D 0xe3.
3532 superio_select(sio_data->sioreg, NCT6775_LD_VID);
3533 data->vid = superio_inb(sio_data->sioreg, 0xe3);
3538 superio_select(sio_data->sioreg, NCT6775_LD_HWM);
3539 tmp = superio_inb(sio_data->sioreg,
3540 NCT6775_REG_CR_FAN_DEBOUNCE);
3541 switch (data->kind) {
3550 superio_outb(sio_data->sioreg, NCT6775_REG_CR_FAN_DEBOUNCE,
3552 dev_info(&pdev->dev, "Enabled fan debounce for chip %s\n",
3556 superio_exit(sio_data->sioreg);
3558 err = device_create_file(dev, &dev_attr_cpu0_vid);
3562 err = nct6775_check_fan_inputs(sio_data, data);
3566 /* Read fan clock dividers immediately */
3567 nct6775_update_fan_div_common(dev, data);
3569 /* Register sysfs hooks */
3570 for (i = 0; i < data->pwm_num; i++) {
3571 if (!(data->has_pwm & (1 << i)))
3574 err = sysfs_create_group(&dev->kobj, &nct6775_group_pwm[i]);
3578 if (data->REG_PWM[3]) {
3579 err = device_create_file(dev,
3580 &sda_max_output[i].dev_attr);
3584 if (data->REG_PWM[4]) {
3585 err = device_create_file(dev,
3586 &sda_step_output[i].dev_attr);
3590 if (data->REG_PWM[6]) {
3591 err = device_create_file(dev,
3592 &sda_weight_duty_base[i].dev_attr);
3597 for (i = 0; i < ARRAY_SIZE(sda_auto_pwm_arrays); i++) {
3598 struct sensor_device_attribute_2 *attr =
3599 &sda_auto_pwm_arrays[i];
3601 if (!(data->has_pwm & (1 << attr->nr)))
3603 if (attr->index > data->auto_pwm_num)
3605 err = device_create_file(dev, &attr->dev_attr);
3610 for (i = 0; i < data->in_num; i++) {
3611 if (!(data->have_in & (1 << i)))
3613 err = sysfs_create_group(&dev->kobj, &nct6775_group_in[i]);
3618 for (i = 0; i < 5; i++) {
3619 if (data->has_fan & (1 << i)) {
3620 err = device_create_file(dev,
3621 &sda_fan_input[i].dev_attr);
3624 err = device_create_file(dev,
3625 &sda_fan_alarm[i].dev_attr);
3628 if (data->kind != nct6776 &&
3629 data->kind != nct6779) {
3630 err = device_create_file(dev,
3631 &sda_fan_div[i].dev_attr);
3635 if (data->has_fan_min & (1 << i)) {
3636 err = device_create_file(dev,
3637 &sda_fan_min[i].dev_attr);
3644 for (i = 0; i < NUM_TEMP; i++) {
3645 if (!(data->have_temp & (1 << i)))
3647 err = device_create_file(dev, &sda_temp_input[i].dev_attr);
3650 if (data->temp_label) {
3651 err = device_create_file(dev,
3652 &sda_temp_label[i].dev_attr);
3656 if (data->reg_temp[1][i]) {
3657 err = device_create_file(dev,
3658 &sda_temp_max[i].dev_attr);
3662 if (data->reg_temp[2][i]) {
3663 err = device_create_file(dev,
3664 &sda_temp_max_hyst[i].dev_attr);
3668 if (data->reg_temp[3][i]) {
3669 err = device_create_file(dev,
3670 &sda_temp_crit[i].dev_attr);
3674 if (!(data->have_temp_fixed & (1 << i)))
3676 err = device_create_file(dev, &sda_temp_type[i].dev_attr);
3679 err = device_create_file(dev, &sda_temp_offset[i].dev_attr);
3682 if (i >= NUM_TEMP_ALARM ||
3683 data->ALARM_BITS[TEMP_ALARM_BASE + i] < 0)
3685 err = device_create_file(dev, &sda_temp_alarm[i].dev_attr);
3690 for (i = 0; i < ARRAY_SIZE(sda_caseopen); i++) {
3691 if (data->ALARM_BITS[INTRUSION_ALARM_BASE + i] < 0)
3693 err = device_create_file(dev, &sda_caseopen[i].dev_attr);
3698 err = device_create_file(dev, &dev_attr_name);
3702 data->hwmon_dev = hwmon_device_register(dev);
3703 if (IS_ERR(data->hwmon_dev)) {
3704 err = PTR_ERR(data->hwmon_dev);
3711 nct6775_device_remove_files(dev);
3715 static int __devexit nct6775_remove(struct platform_device *pdev)
3717 struct nct6775_data *data = platform_get_drvdata(pdev);
3719 hwmon_device_unregister(data->hwmon_dev);
3720 nct6775_device_remove_files(&pdev->dev);
3725 static struct platform_driver nct6775_driver = {
3727 .owner = THIS_MODULE,
3730 .probe = nct6775_probe,
3731 .remove = __devexit_p(nct6775_remove),
3734 /* nct6775_find() looks for a '627 in the Super-I/O config space */
3735 static int __init nct6775_find(int sioaddr, unsigned short *addr,
3736 struct nct6775_sio_data *sio_data)
3738 static const char sio_name_NCT6775[] __initconst = "NCT6775F";
3739 static const char sio_name_NCT6776[] __initconst = "NCT6776F";
3740 static const char sio_name_NCT6779[] __initconst = "NCT6779D";
3743 const char *sio_name;
3746 err = superio_enter(sioaddr);
3753 val = (superio_inb(sioaddr, SIO_REG_DEVID) << 8)
3754 | superio_inb(sioaddr, SIO_REG_DEVID + 1);
3755 switch (val & SIO_ID_MASK) {
3756 case SIO_NCT6775_ID:
3757 sio_data->kind = nct6775;
3758 sio_name = sio_name_NCT6775;
3760 case SIO_NCT6776_ID:
3761 sio_data->kind = nct6776;
3762 sio_name = sio_name_NCT6776;
3764 case SIO_NCT6779_ID:
3765 sio_data->kind = nct6779;
3766 sio_name = sio_name_NCT6779;
3770 pr_debug("unsupported chip ID: 0x%04x\n", val);
3771 superio_exit(sioaddr);
3775 /* We have a known chip, find the HWM I/O address */
3776 superio_select(sioaddr, NCT6775_LD_HWM);
3777 val = (superio_inb(sioaddr, SIO_REG_ADDR) << 8)
3778 | superio_inb(sioaddr, SIO_REG_ADDR + 1);
3779 *addr = val & IOREGION_ALIGNMENT;
3781 pr_err("Refusing to enable a Super-I/O device with a base I/O port 0\n");
3782 superio_exit(sioaddr);
3786 /* Activate logical device if needed */
3787 val = superio_inb(sioaddr, SIO_REG_ENABLE);
3788 if (!(val & 0x01)) {
3789 pr_warn("Forcibly enabling Super-I/O. Sensor is probably unusable.\n");
3790 superio_outb(sioaddr, SIO_REG_ENABLE, val | 0x01);
3793 superio_exit(sioaddr);
3794 pr_info("Found %s chip at %#x\n", sio_name, *addr);
3795 sio_data->sioreg = sioaddr;
3801 * when Super-I/O functions move to a separate file, the Super-I/O
3802 * bus will manage the lifetime of the device and this module will only keep
3803 * track of the nct6775 driver. But since we platform_device_alloc(), we
3804 * must keep track of the device
3806 static struct platform_device *pdev;
3808 static int __init sensors_nct6775_init(void)
3811 unsigned short address;
3812 struct resource res;
3813 struct nct6775_sio_data sio_data;
3816 * initialize sio_data->kind and sio_data->sioreg.
3818 * when Super-I/O functions move to a separate file, the Super-I/O
3819 * driver will probe 0x2e and 0x4e and auto-detect the presence of a
3820 * nct6775 hardware monitor, and call probe()
3822 if (nct6775_find(0x2e, &address, &sio_data) &&
3823 nct6775_find(0x4e, &address, &sio_data))
3826 err = platform_driver_register(&nct6775_driver);
3830 pdev = platform_device_alloc(DRVNAME, address);
3833 pr_err("Device allocation failed\n");
3834 goto exit_unregister;
3837 err = platform_device_add_data(pdev, &sio_data,
3838 sizeof(struct nct6775_sio_data));
3840 pr_err("Platform data allocation failed\n");
3841 goto exit_device_put;
3844 memset(&res, 0, sizeof(res));
3846 res.start = address + IOREGION_OFFSET;
3847 res.end = address + IOREGION_OFFSET + IOREGION_LENGTH - 1;
3848 res.flags = IORESOURCE_IO;
3850 err = acpi_check_resource_conflict(&res);
3852 goto exit_device_put;
3854 err = platform_device_add_resources(pdev, &res, 1);
3856 pr_err("Device resource addition failed (%d)\n", err);
3857 goto exit_device_put;
3860 /* platform_device_add calls probe() */
3861 err = platform_device_add(pdev);
3863 pr_err("Device addition failed (%d)\n", err);
3864 goto exit_device_put;
3870 platform_device_put(pdev);
3872 platform_driver_unregister(&nct6775_driver);
3877 static void __exit sensors_nct6775_exit(void)
3879 platform_device_unregister(pdev);
3880 platform_driver_unregister(&nct6775_driver);
3883 MODULE_AUTHOR("Guenter Roeck <linux@roeck-us.net>");
3884 MODULE_DESCRIPTION("NCT6775F/NCT6776F/NCT6779D driver");
3885 MODULE_LICENSE("GPL");
3887 module_init(sensors_nct6775_init);
3888 module_exit(sensors_nct6775_exit);