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>
59 enum kinds { nct6775, nct6776, nct6779 };
61 /* used to set data->name = nct6775_device_names[data->sio_kind] */
62 static const char * const nct6775_device_names[] = {
68 static unsigned short force_id;
69 module_param(force_id, ushort, 0);
70 MODULE_PARM_DESC(force_id, "Override the detected device ID");
72 static unsigned short fan_debounce;
73 module_param(fan_debounce, ushort, 0);
74 MODULE_PARM_DESC(fan_debounce, "Enable debouncing for fan RPM signal");
76 #define DRVNAME "nct6775"
79 * Super-I/O constants and functions
82 #define NCT6775_LD_ACPI 0x0a
83 #define NCT6775_LD_HWM 0x0b
84 #define NCT6775_LD_VID 0x0d
86 #define SIO_REG_LDSEL 0x07 /* Logical device select */
87 #define SIO_REG_DEVID 0x20 /* Device ID (2 bytes) */
88 #define SIO_REG_EN_VRM10 0x2C /* GPIO3, GPIO4 selection */
89 #define SIO_REG_ENABLE 0x30 /* Logical device enable */
90 #define SIO_REG_ADDR 0x60 /* Logical device address (2 bytes) */
91 #define SIO_REG_VID_CTRL 0xF0 /* VID control */
92 #define SIO_REG_VID_DATA 0xF1 /* VID data */
94 #define SIO_NCT6775_ID 0xb470
95 #define SIO_NCT6776_ID 0xc330
96 #define SIO_NCT6779_ID 0xc560
97 #define SIO_ID_MASK 0xFFF0
100 superio_outb(int ioreg, int reg, int val)
103 outb(val, ioreg + 1);
107 superio_inb(int ioreg, int reg)
110 return inb(ioreg + 1);
114 superio_select(int ioreg, int ld)
116 outb(SIO_REG_LDSEL, ioreg);
121 superio_enter(int ioreg)
124 * Try to reserve <ioreg> and <ioreg + 1> for exclusive access.
126 if (!request_muxed_region(ioreg, 2, DRVNAME))
136 superio_exit(int ioreg)
140 outb(0x02, ioreg + 1);
141 release_region(ioreg, 2);
148 #define IOREGION_ALIGNMENT (~7)
149 #define IOREGION_OFFSET 5
150 #define IOREGION_LENGTH 2
151 #define ADDR_REG_OFFSET 0
152 #define DATA_REG_OFFSET 1
154 #define NCT6775_REG_BANK 0x4E
155 #define NCT6775_REG_CONFIG 0x40
158 * Not currently used:
159 * REG_MAN_ID has the value 0x5ca3 for all supported chips.
160 * REG_CHIP_ID == 0x88/0xa1/0xc1 depending on chip model.
161 * REG_MAN_ID is at port 0x4f
162 * REG_CHIP_ID is at port 0x58
165 #define NUM_TEMP 10 /* Max number of temp attribute sets w/ limits*/
166 #define NUM_TEMP_FIXED 6 /* Max number of fixed temp attribute sets */
168 #define NUM_REG_ALARM 4 /* Max number of alarm registers */
170 /* Common and NCT6775 specific data */
172 /* Voltage min/max registers for nr=7..14 are in bank 5 */
174 static const u16 NCT6775_REG_IN_MAX[] = {
175 0x2b, 0x2d, 0x2f, 0x31, 0x33, 0x35, 0x37, 0x554, 0x556, 0x558, 0x55a,
176 0x55c, 0x55e, 0x560, 0x562 };
177 static const u16 NCT6775_REG_IN_MIN[] = {
178 0x2c, 0x2e, 0x30, 0x32, 0x34, 0x36, 0x38, 0x555, 0x557, 0x559, 0x55b,
179 0x55d, 0x55f, 0x561, 0x563 };
180 static const u16 NCT6775_REG_IN[] = {
181 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x550, 0x551, 0x552
184 #define NCT6775_REG_VBAT 0x5D
185 #define NCT6775_REG_DIODE 0x5E
187 #define NCT6775_REG_FANDIV1 0x506
188 #define NCT6775_REG_FANDIV2 0x507
190 #define NCT6775_REG_CR_FAN_DEBOUNCE 0xf0
192 static const u16 NCT6775_REG_ALARM[NUM_REG_ALARM] = { 0x459, 0x45A, 0x45B };
194 /* 0..15 voltages, 16..23 fans, 24..31 temperatures */
196 static const s8 NCT6775_ALARM_BITS[] = {
197 0, 1, 2, 3, 8, 21, 20, 16, /* in0.. in7 */
198 17, -1, -1, -1, -1, -1, -1, /* in8..in14 */
200 6, 7, 11, 10, 23, /* fan1..fan5 */
201 -1, -1, -1, /* unused */
202 4, 5, 13, -1, -1, -1, /* temp1..temp6 */
203 12, -1 }; /* intrusion0, intrusion1 */
205 #define FAN_ALARM_BASE 16
206 #define TEMP_ALARM_BASE 24
207 #define INTRUSION_ALARM_BASE 30
209 static const u8 NCT6775_REG_CR_CASEOPEN_CLR[] = { 0xe6, 0xee };
210 static const u8 NCT6775_CR_CASEOPEN_CLR_MASK[] = { 0x20, 0x01 };
212 /* DC or PWM output fan configuration */
213 static const u8 NCT6775_REG_PWM_MODE[] = { 0x04, 0x04, 0x12 };
214 static const u8 NCT6775_PWM_MODE_MASK[] = { 0x01, 0x02, 0x01 };
216 /* Advanced Fan control, some values are common for all fans */
218 static const u16 NCT6775_REG_FAN_PULSES[] = { 0x641, 0x642, 0x643, 0x644, 0 };
220 static const u16 NCT6775_REG_TARGET[] = { 0x101, 0x201, 0x301, 0x801, 0x901 };
221 static const u16 NCT6775_REG_FAN_MODE[] = { 0x102, 0x202, 0x302, 0x802, 0x902 };
222 static const u16 NCT6775_REG_FAN_STEP_DOWN_TIME[] = {
223 0x103, 0x203, 0x303, 0x803, 0x903 };
224 static const u16 NCT6775_REG_FAN_STEP_UP_TIME[] = {
225 0x104, 0x204, 0x304, 0x804, 0x904 };
226 static const u16 NCT6775_REG_FAN_STOP_OUTPUT[] = {
227 0x105, 0x205, 0x305, 0x805, 0x905 };
228 static const u16 NCT6775_REG_FAN_START_OUTPUT[]
229 = { 0x106, 0x206, 0x306, 0x806, 0x906 };
230 static const u16 NCT6775_REG_FAN_STEP_ENABLE[] = {
231 0x120, 0x220, 0x320, 0x820, 0x920 };
232 static const u16 NCT6775_REG_FAN_MAX_OUTPUT[] = { 0x10a, 0x20a, 0x30a };
233 static const u16 NCT6775_REG_FAN_STEP_OUTPUT[] = { 0x10b, 0x20b, 0x30b };
235 static const u16 NCT6775_REG_FAN_STOP_TIME[] = {
236 0x107, 0x207, 0x307, 0x807, 0x907 };
237 static const u16 NCT6775_REG_PWM[] = { 0x109, 0x209, 0x309, 0x809, 0x909 };
238 static const u16 NCT6775_REG_PWM_READ[] = { 0x01, 0x03, 0x11, 0x13, 0x15 };
240 static const u16 NCT6775_REG_FAN[] = { 0x630, 0x632, 0x634, 0x636, 0x638 };
241 static const u16 NCT6775_REG_FAN_MIN[] = { 0x3b, 0x3c, 0x3d };
243 static const u16 NCT6775_REG_TEMP[] = {
244 0x27, 0x150, 0x250, 0x62b, 0x62c, 0x62d };
246 static const u16 NCT6775_REG_TEMP_CONFIG[] = {
247 0, 0x152, 0x252, 0x628, 0x629, 0x62A };
248 static const u16 NCT6775_REG_TEMP_HYST[] = {
249 0x3a, 0x153, 0x253, 0x673, 0x678, 0x67D };
250 static const u16 NCT6775_REG_TEMP_OVER[] = {
251 0x39, 0x155, 0x255, 0x672, 0x677, 0x67C };
253 static const u16 NCT6775_REG_TEMP_SOURCE[] = {
254 0x621, 0x622, 0x623, 0x624, 0x625, 0x626 };
256 static const u16 NCT6775_REG_TEMP_SEL[] = {
257 0x100, 0x200, 0x300, 0x800, 0x900 };
259 static const u16 NCT6775_REG_WEIGHT_TEMP_SEL[] = {
260 0x139, 0x239, 0x339, 0x839, 0x939 };
261 static const u16 NCT6775_REG_WEIGHT_TEMP_STEP[] = {
262 0x13a, 0x23a, 0x33a, 0x83a, 0x93a };
263 static const u16 NCT6775_REG_WEIGHT_TEMP_STEP_TOL[] = {
264 0x13b, 0x23b, 0x33b, 0x83b, 0x93b };
265 static const u16 NCT6775_REG_WEIGHT_DUTY_STEP[] = {
266 0x13c, 0x23c, 0x33c, 0x83c, 0x93c };
267 static const u16 NCT6775_REG_WEIGHT_TEMP_BASE[] = {
268 0x13d, 0x23d, 0x33d, 0x83d, 0x93d };
270 static const u16 NCT6775_REG_TEMP_OFFSET[] = { 0x454, 0x455, 0x456 };
272 static const u16 NCT6775_REG_AUTO_TEMP[] = {
273 0x121, 0x221, 0x321, 0x821, 0x921 };
274 static const u16 NCT6775_REG_AUTO_PWM[] = {
275 0x127, 0x227, 0x327, 0x827, 0x927 };
277 #define NCT6775_AUTO_TEMP(data, nr, p) ((data)->REG_AUTO_TEMP[nr] + (p))
278 #define NCT6775_AUTO_PWM(data, nr, p) ((data)->REG_AUTO_PWM[nr] + (p))
280 static const u16 NCT6775_REG_CRITICAL_ENAB[] = { 0x134, 0x234, 0x334 };
282 static const u16 NCT6775_REG_CRITICAL_TEMP[] = {
283 0x135, 0x235, 0x335, 0x835, 0x935 };
284 static const u16 NCT6775_REG_CRITICAL_TEMP_TOLERANCE[] = {
285 0x138, 0x238, 0x338, 0x838, 0x938 };
287 static const char *const nct6775_temp_label[] = {
301 "PCH_CHIP_CPU_MAX_TEMP",
311 static const u16 NCT6775_REG_TEMP_ALTERNATE[ARRAY_SIZE(nct6775_temp_label) - 1]
312 = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0x661, 0x662, 0x664 };
314 static const u16 NCT6775_REG_TEMP_CRIT[ARRAY_SIZE(nct6775_temp_label) - 1]
315 = { 0, 0, 0, 0, 0xa00, 0xa01, 0xa02, 0xa03, 0xa04, 0xa05, 0xa06,
318 /* NCT6776 specific data */
320 static const s8 NCT6776_ALARM_BITS[] = {
321 0, 1, 2, 3, 8, 21, 20, 16, /* in0.. in7 */
322 17, -1, -1, -1, -1, -1, -1, /* in8..in14 */
324 6, 7, 11, 10, 23, /* fan1..fan5 */
325 -1, -1, -1, /* unused */
326 4, 5, 13, -1, -1, -1, /* temp1..temp6 */
327 12, 9 }; /* intrusion0, intrusion1 */
329 static const u8 NCT6776_REG_PWM_MODE[] = { 0x04, 0, 0 };
330 static const u8 NCT6776_PWM_MODE_MASK[] = { 0x01, 0, 0 };
332 static const u16 NCT6776_REG_FAN_PULSES[] = { 0x644, 0x645, 0x646, 0, 0 };
333 static const u16 NCT6776_REG_FAN_MIN[] = { 0x63a, 0x63c, 0x63e, 0x640, 0x642 };
335 static const u16 NCT6776_REG_WEIGHT_DUTY_BASE[] = {
336 0x13e, 0x23e, 0x33e, 0x83e, 0x93e };
338 static const u16 NCT6776_REG_TEMP_CONFIG[11] = {
339 0x18, 0x152, 0x252, 0x628, 0x629, 0x62A };
341 static const char *const nct6776_temp_label[] = {
356 "PCH_CHIP_CPU_MAX_TEMP",
367 static const u16 NCT6776_REG_TEMP_ALTERNATE[ARRAY_SIZE(nct6776_temp_label) - 1]
368 = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0x401, 0x402, 0x404 };
370 static const u16 NCT6776_REG_TEMP_CRIT[ARRAY_SIZE(nct6776_temp_label) - 1]
371 = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0x709, 0x70a };
373 /* NCT6779 specific data */
375 static const u16 NCT6779_REG_IN[] = {
376 0x480, 0x481, 0x482, 0x483, 0x484, 0x485, 0x486, 0x487,
377 0x488, 0x489, 0x48a, 0x48b, 0x48c, 0x48d, 0x48e };
379 static const u16 NCT6779_REG_ALARM[NUM_REG_ALARM] = {
380 0x459, 0x45A, 0x45B, 0x568 };
382 static const s8 NCT6779_ALARM_BITS[] = {
383 0, 1, 2, 3, 8, 21, 20, 16, /* in0.. in7 */
384 17, 24, 25, 26, 27, 28, 29, /* in8..in14 */
386 6, 7, 11, 10, 23, /* fan1..fan5 */
387 -1, -1, -1, /* unused */
388 4, 5, 13, -1, -1, -1, /* temp1..temp6 */
389 12, 9 }; /* intrusion0, intrusion1 */
391 static const u16 NCT6779_REG_FAN_PULSES[] = {
392 0x644, 0x645, 0x646, 0x647, 0x648 };
394 static const u16 NCT6779_REG_FAN[] = { 0x4b0, 0x4b2, 0x4b4, 0x4b6, 0x4b8 };
396 static const u16 NCT6779_REG_TOLERANCE_H[] = {
397 0x10c, 0x20c, 0x30c, 0x80c, 0x90c };
399 static const u16 NCT6779_REG_CRITICAL_PWM_ENABLE[] = {
400 0x136, 0x236, 0x336, 0x836, 0x936 };
401 static const u16 NCT6779_REG_CRITICAL_PWM[] = {
402 0x137, 0x237, 0x337, 0x837, 0x937 };
404 static const u16 NCT6779_REG_TEMP[] = { 0x27, 0x150 };
405 static const u16 NCT6779_REG_TEMP_HYST[] = { 0x3a, 0x153, 0, 0, 0, 0 };
406 static const u16 NCT6779_REG_TEMP_OVER[] = { 0x39, 0x155, 0, 0, 0, 0 };
407 static const u16 NCT6779_REG_TEMP_OFFSET[] = {
408 0x454, 0x455, 0x456, 0x44a, 0x44b, 0x44c };
410 static const u16 NCT6779_REG_TEMP_CONFIG[11] = { 0x18, 0x152 };
412 static const char *const nct6779_temp_label[] = {
431 "PCH_CHIP_CPU_MAX_TEMP",
442 static const u16 NCT6779_REG_TEMP_ALTERNATE[ARRAY_SIZE(nct6779_temp_label) - 1]
443 = { 0x490, 0x491, 0x492, 0x493, 0x494, 0x495, 0, 0,
444 0, 0, 0, 0, 0, 0, 0, 0,
445 0, 0x400, 0x401, 0x402, 0x404, 0x405, 0x406, 0x407,
448 static const u16 NCT6779_REG_TEMP_CRIT[ARRAY_SIZE(nct6779_temp_label) - 1]
449 = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0x709, 0x70a };
451 static inline int reg_to_pwm_enable(int pwm, int mode)
453 if (mode == 0 && pwm == 255)
458 static inline int pwm_enable_to_reg(int mode)
469 /* 1 is DC mode, output in ms */
470 static inline unsigned int step_time_from_reg(u8 reg, u8 mode)
472 return mode ? 400 * reg : 100 * reg;
475 static inline u8 step_time_to_reg(unsigned int msec, u8 mode)
477 return SENSORS_LIMIT((mode ? (msec + 200) / 400 :
478 (msec + 50) / 100), 1, 255);
481 static unsigned int fan_from_reg8(u16 reg, unsigned int divreg)
483 if (reg == 0 || reg == 255)
485 return 1350000U / (reg << divreg);
488 static unsigned int fan_from_reg13(u16 reg, unsigned int divreg)
490 if ((reg & 0xff1f) == 0xff1f)
493 reg = (reg & 0x1f) | ((reg & 0xff00) >> 3);
498 return 1350000U / reg;
501 static unsigned int fan_from_reg16(u16 reg, unsigned int divreg)
503 if (reg == 0 || reg == 0xffff)
507 * Even though the registers are 16 bit wide, the fan divisor
510 return 1350000U / (reg << divreg);
513 static inline unsigned int
520 * Some of the voltage inputs have internal scaling, the tables below
521 * contain 8 (the ADC LSB in mV) * scaling factor * 100
523 static const u16 scale_in[15] = {
524 800, 800, 1600, 1600, 800, 800, 800, 1600, 1600, 800, 800, 800, 800,
528 static inline long in_from_reg(u8 reg, u8 nr)
530 return DIV_ROUND_CLOSEST(reg * scale_in[nr], 100);
533 static inline u8 in_to_reg(u32 val, u8 nr)
535 return SENSORS_LIMIT(DIV_ROUND_CLOSEST(val * 100, scale_in[nr]), 0,
540 * Data structures and manipulation thereof
543 struct nct6775_data {
544 int addr; /* IO base of hw monitor block */
548 struct device *hwmon_dev;
551 u16 reg_temp[4][NUM_TEMP]; /* 0=temp, 1=temp_over, 2=temp_hyst,
554 u8 temp_src[NUM_TEMP];
555 u16 reg_temp_config[NUM_TEMP];
556 const char * const *temp_label;
563 const s8 *ALARM_BITS;
566 const u16 *REG_IN_MINMAX[2];
568 const u16 *REG_TARGET;
570 const u16 *REG_FAN_MODE;
571 const u16 *REG_FAN_MIN;
572 const u16 *REG_FAN_TIME[3];
574 const u8 *REG_PWM_MODE;
575 const u8 *PWM_MODE_MASK;
577 const u16 *REG_PWM[7]; /* [0]=pwm, [1]=start_output, [2]=stop_output,
578 * [3]=max_output, [4]=step_output,
579 * [5]=weight_duty_step, [6]=weight_duty_base
581 const u16 *REG_PWM_READ;
583 const u16 *REG_AUTO_TEMP;
584 const u16 *REG_AUTO_PWM;
586 const u16 *REG_CRITICAL_TEMP;
587 const u16 *REG_CRITICAL_TEMP_TOLERANCE;
589 const u16 *REG_TEMP_SOURCE; /* temp register sources */
590 const u16 *REG_TEMP_SEL[3]; /* pwm temp:
591 * 0=base, 1=weight, 2=step enable
594 const u16 *REG_WEIGHT_TEMP[3]; /* 0=base, 1=hyst, 2=step */
596 const u16 *REG_TEMP_OFFSET;
598 const u16 *REG_ALARM;
600 unsigned int (*fan_from_reg)(u16 reg, unsigned int divreg);
601 unsigned int (*fan_from_reg_min)(u16 reg, unsigned int divreg);
603 struct mutex update_lock;
604 char valid; /* !=0 if following fields are valid */
605 unsigned long last_updated; /* In jiffies */
607 /* Register values */
608 u8 bank; /* current register bank */
609 u8 in_num; /* number of in inputs we have */
610 u8 in[15][3]; /* [0]=in, [1]=in_max, [2]=in_min */
615 u8 has_fan; /* some fan inputs can be disabled */
616 u8 has_fan_min; /* some fans don't have min register */
619 u8 temp_fixed_num; /* 3 or 6 */
620 u8 temp_type[NUM_TEMP_FIXED];
621 s8 temp_offset[NUM_TEMP_FIXED];
622 s16 temp[4][NUM_TEMP]; /* 0=temp, 1=temp_over, 2=temp_hyst,
626 u8 pwm_num; /* number of pwm */
628 u8 pwm_mode[5]; /* 1->DC variable voltage, 0->PWM variable duty cycle */
629 u8 pwm_enable[5]; /* 0->off
631 * 2->thermal cruise mode (also called SmartFan I)
632 * 3->fan speed cruise mode
634 * 5->enhanced variable thermal cruise (SmartFan IV)
636 u8 pwm[7][5]; /* [0]=pwm, [1]=start_output, [2]=stop_output,
637 * [3]=max_output, [4]=step_output,
638 * [5]=weight_duty_step, [6]=weight_duty_base
643 u8 fan_time[3][5]; /* 0 = stop_time, 1 = step_up, 2 = step_down */
645 /* Automatic fan speed control registers */
650 u8 pwm_temp_sel[2][5];
652 bool pwm_sel_enable[3][5];/* 0->stop_val, 1->weight, 2->step;
653 * false->off, true->on
655 u8 weight_temp[3][5]; /* 0->temp_step, 1->temp_step_tol,
667 struct nct6775_sio_data {
672 static bool is_word_sized(struct nct6775_data *data, u16 reg)
674 switch (data->kind) {
676 return (((reg & 0xff00) == 0x100 ||
677 (reg & 0xff00) == 0x200) &&
678 ((reg & 0x00ff) == 0x50 ||
679 (reg & 0x00ff) == 0x53 ||
680 (reg & 0x00ff) == 0x55)) ||
681 (reg & 0xfff0) == 0x630 ||
682 reg == 0x640 || reg == 0x642 ||
684 ((reg & 0xfff0) == 0x650 && (reg & 0x000f) >= 0x06) ||
685 reg == 0x73 || reg == 0x75 || reg == 0x77;
687 return (((reg & 0xff00) == 0x100 ||
688 (reg & 0xff00) == 0x200) &&
689 ((reg & 0x00ff) == 0x50 ||
690 (reg & 0x00ff) == 0x53 ||
691 (reg & 0x00ff) == 0x55)) ||
692 (reg & 0xfff0) == 0x630 ||
694 reg == 0x640 || reg == 0x642 ||
695 ((reg & 0xfff0) == 0x650 && (reg & 0x000f) >= 0x06) ||
696 reg == 0x73 || reg == 0x75 || reg == 0x77;
698 return reg == 0x150 || reg == 0x153 || reg == 0x155 ||
699 ((reg & 0xfff0) == 0x4b0 && (reg & 0x000f) < 0x09) ||
701 reg == 0x63a || reg == 0x63c || reg == 0x63e ||
702 reg == 0x640 || reg == 0x642 ||
703 reg == 0x73 || reg == 0x75 || reg == 0x77 || reg == 0x79 ||
710 * On older chips, only registers 0x50-0x5f are banked.
711 * On more recent chips, all registers are banked.
712 * Assume that is the case and set the bank number for each access.
713 * Cache the bank number so it only needs to be set if it changes.
715 static inline void nct6775_set_bank(struct nct6775_data *data, u16 reg)
718 if (data->bank != bank) {
719 outb_p(NCT6775_REG_BANK, data->addr + ADDR_REG_OFFSET);
720 outb_p(bank, data->addr + DATA_REG_OFFSET);
725 static u16 nct6775_read_value(struct nct6775_data *data, u16 reg)
727 int res, word_sized = is_word_sized(data, reg);
729 mutex_lock(&data->lock);
731 nct6775_set_bank(data, reg);
732 outb_p(reg & 0xff, data->addr + ADDR_REG_OFFSET);
733 res = inb_p(data->addr + DATA_REG_OFFSET);
735 outb_p((reg & 0xff) + 1,
736 data->addr + ADDR_REG_OFFSET);
737 res = (res << 8) + inb_p(data->addr + DATA_REG_OFFSET);
740 mutex_unlock(&data->lock);
744 static int nct6775_write_value(struct nct6775_data *data, u16 reg, u16 value)
746 int word_sized = is_word_sized(data, reg);
748 mutex_lock(&data->lock);
750 nct6775_set_bank(data, reg);
751 outb_p(reg & 0xff, data->addr + ADDR_REG_OFFSET);
753 outb_p(value >> 8, data->addr + DATA_REG_OFFSET);
754 outb_p((reg & 0xff) + 1,
755 data->addr + ADDR_REG_OFFSET);
757 outb_p(value & 0xff, data->addr + DATA_REG_OFFSET);
759 mutex_unlock(&data->lock);
763 /* We left-align 8-bit temperature values to make the code simpler */
764 static u16 nct6775_read_temp(struct nct6775_data *data, u16 reg)
768 res = nct6775_read_value(data, reg);
769 if (!is_word_sized(data, reg))
775 static int nct6775_write_temp(struct nct6775_data *data, u16 reg, u16 value)
777 if (!is_word_sized(data, reg))
779 return nct6775_write_value(data, reg, value);
782 /* This function assumes that the caller holds data->update_lock */
783 static void nct6775_write_fan_div(struct nct6775_data *data, int nr)
789 reg = (nct6775_read_value(data, NCT6775_REG_FANDIV1) & 0x70)
790 | (data->fan_div[0] & 0x7);
791 nct6775_write_value(data, NCT6775_REG_FANDIV1, reg);
794 reg = (nct6775_read_value(data, NCT6775_REG_FANDIV1) & 0x7)
795 | ((data->fan_div[1] << 4) & 0x70);
796 nct6775_write_value(data, NCT6775_REG_FANDIV1, reg);
799 reg = (nct6775_read_value(data, NCT6775_REG_FANDIV2) & 0x70)
800 | (data->fan_div[2] & 0x7);
801 nct6775_write_value(data, NCT6775_REG_FANDIV2, reg);
804 reg = (nct6775_read_value(data, NCT6775_REG_FANDIV2) & 0x7)
805 | ((data->fan_div[3] << 4) & 0x70);
806 nct6775_write_value(data, NCT6775_REG_FANDIV2, reg);
811 static void nct6775_write_fan_div_common(struct device *dev,
812 struct nct6775_data *data, int nr)
814 if (data->kind == nct6775)
815 nct6775_write_fan_div(data, nr);
818 static void nct6775_update_fan_div(struct nct6775_data *data)
822 i = nct6775_read_value(data, NCT6775_REG_FANDIV1);
823 data->fan_div[0] = i & 0x7;
824 data->fan_div[1] = (i & 0x70) >> 4;
825 i = nct6775_read_value(data, NCT6775_REG_FANDIV2);
826 data->fan_div[2] = i & 0x7;
827 if (data->has_fan & (1<<3))
828 data->fan_div[3] = (i & 0x70) >> 4;
831 static void nct6775_update_fan_div_common(struct device *dev,
832 struct nct6775_data *data)
834 if (data->kind == nct6775)
835 nct6775_update_fan_div(data);
838 static void nct6775_update_pwm(struct device *dev)
840 struct nct6775_data *data = dev_get_drvdata(dev);
845 for (i = 0; i < data->pwm_num; i++) {
846 if (!(data->has_pwm & (1 << i)))
849 duty_is_dc = data->REG_PWM_MODE[i] &&
850 (nct6775_read_value(data, data->REG_PWM_MODE[i])
851 & data->PWM_MODE_MASK[i]);
852 data->pwm_mode[i] = duty_is_dc;
854 fanmodecfg = nct6775_read_value(data, data->REG_FAN_MODE[i]);
855 for (j = 0; j < ARRAY_SIZE(data->REG_PWM); j++) {
856 if (data->REG_PWM[j] && data->REG_PWM[j][i]) {
858 = nct6775_read_value(data,
859 data->REG_PWM[j][i]);
863 data->pwm_enable[i] = reg_to_pwm_enable(data->pwm[0][i],
864 (fanmodecfg >> 4) & 7);
866 data->tolerance[i][0] = fanmodecfg & 0x0f;
867 if (data->kind == nct6779) {
868 reg = nct6775_read_value(data,
869 NCT6779_REG_TOLERANCE_H[i]);
870 data->tolerance[i][0] |= (reg & 0x70) >> 1;
872 data->tolerance[i][1] =
873 nct6775_read_value(data,
874 data->REG_CRITICAL_TEMP_TOLERANCE[i]);
876 data->pwm_sel_enable[0][i] =
877 nct6775_read_value(data, data->REG_TEMP_SEL[0][i])
879 data->pwm_sel_enable[1][i] =
880 nct6775_read_value(data, data->REG_TEMP_SEL[1][i])
882 data->pwm_sel_enable[2][i] =
883 nct6775_read_value(data, data->REG_TEMP_SEL[2][i])
887 for (j = 0; j < 2; j++) {
888 reg = nct6775_read_value(data,
889 data->REG_TEMP_SEL[j][i]);
890 data->pwm_temp_sel[j][i] = reg & 0x1f;
892 /* Weight temp data */
893 for (j = 0; j < 3; j++) {
894 data->weight_temp[j][i]
895 = nct6775_read_value(data,
896 data->REG_WEIGHT_TEMP[j][i]);
901 static void nct6775_update_pwm_limits(struct device *dev)
903 struct nct6775_data *data = dev_get_drvdata(dev);
907 for (i = 0; i < data->pwm_num; i++) {
908 if (!(data->has_pwm & (1 << i)))
911 for (j = 0; j < 3; j++) {
912 data->fan_time[j][i] =
913 nct6775_read_value(data, data->REG_FAN_TIME[j][i]);
916 data->target_temp[i] =
917 nct6775_read_value(data, data->REG_TARGET[i]) &
918 (data->pwm_mode[i] ? 0xff : 0x7f);
920 for (j = 0; j < data->auto_pwm_num; j++) {
921 data->auto_pwm[i][j] =
922 nct6775_read_value(data,
923 NCT6775_AUTO_PWM(data, i, j));
924 data->auto_temp[i][j] =
925 nct6775_read_value(data,
926 NCT6775_AUTO_TEMP(data, i, j));
929 /* critical auto_pwm temperature data */
930 data->auto_temp[i][data->auto_pwm_num] =
931 nct6775_read_value(data, data->REG_CRITICAL_TEMP[i]);
933 switch (data->kind) {
935 reg = nct6775_read_value(data,
936 NCT6775_REG_CRITICAL_ENAB[i]);
937 data->auto_pwm[i][data->auto_pwm_num] =
938 (reg & 0x02) ? 0xff : 0x00;
941 data->auto_pwm[i][data->auto_pwm_num] = 0xff;
944 reg = nct6775_read_value(data,
945 NCT6779_REG_CRITICAL_PWM_ENABLE[i]);
947 data->auto_pwm[i][data->auto_pwm_num] =
948 nct6775_read_value(data,
949 NCT6779_REG_CRITICAL_PWM[i]);
951 data->auto_pwm[i][data->auto_pwm_num] = 0xff;
957 static struct nct6775_data *nct6775_update_device(struct device *dev)
959 struct nct6775_data *data = dev_get_drvdata(dev);
962 mutex_lock(&data->update_lock);
964 if (time_after(jiffies, data->last_updated + HZ + HZ/2)
966 /* Fan clock dividers */
967 nct6775_update_fan_div_common(dev, data);
969 /* Measured voltages and limits */
970 for (i = 0; i < data->in_num; i++) {
971 if (!(data->have_in & (1 << i)))
974 data->in[i][0] = nct6775_read_value(data,
976 data->in[i][1] = nct6775_read_value(data,
977 data->REG_IN_MINMAX[0][i]);
978 data->in[i][2] = nct6775_read_value(data,
979 data->REG_IN_MINMAX[1][i]);
982 /* Measured fan speeds and limits */
983 for (i = 0; i < 5; i++) {
986 if (!(data->has_fan & (1 << i)))
989 reg = nct6775_read_value(data, data->REG_FAN[i]);
990 data->rpm[i] = data->fan_from_reg(reg,
993 if (data->has_fan_min & (1 << i))
994 data->fan_min[i] = nct6775_read_value(data,
995 data->REG_FAN_MIN[i]);
998 * If we failed to measure the fan speed and clock
999 * divider can be increased, let's try that for next
1002 if (data->has_fan_div && (reg >= 0xff || reg == 0x00)
1003 && data->fan_div[i] < 0x07) {
1005 "Increasing fan%d clock divider from %u to %u\n",
1006 i + 1, div_from_reg(data->fan_div[i]),
1007 div_from_reg(data->fan_div[i] + 1));
1009 nct6775_write_fan_div_common(dev, data, i);
1010 /* Preserve min limit if possible */
1011 if ((data->has_fan_min & (1 << i))
1012 && data->fan_min[i] >= 2
1013 && data->fan_min[i] != 255)
1014 nct6775_write_value(data,
1015 data->REG_FAN_MIN[i],
1016 (data->fan_min[i] /= 2));
1020 nct6775_update_pwm(dev);
1021 nct6775_update_pwm_limits(dev);
1023 /* Measured temperatures and limits */
1024 for (i = 0; i < NUM_TEMP; i++) {
1025 if (!(data->have_temp & (1 << i)))
1027 for (j = 0; j < 4; j++) {
1028 if (data->reg_temp[j][i])
1030 = nct6775_read_temp(data,
1031 data->reg_temp[j][i]);
1033 if (!(data->have_temp_fixed & (1 << i)))
1035 data->temp_offset[i]
1036 = nct6775_read_value(data, data->REG_TEMP_OFFSET[i]);
1040 for (i = 0; i < NUM_REG_ALARM; i++) {
1042 if (!data->REG_ALARM[i])
1044 alarm = nct6775_read_value(data, data->REG_ALARM[i]);
1045 data->alarms |= ((u64)alarm) << (i << 3);
1048 data->last_updated = jiffies;
1052 mutex_unlock(&data->update_lock);
1057 * Sysfs callback functions
1060 show_in_reg(struct device *dev, struct device_attribute *attr, char *buf)
1062 struct nct6775_data *data = nct6775_update_device(dev);
1063 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
1065 int index = sattr->index;
1066 return sprintf(buf, "%ld\n", in_from_reg(data->in[nr][index], nr));
1070 store_in_reg(struct device *dev, struct device_attribute *attr, const char *buf,
1073 struct nct6775_data *data = dev_get_drvdata(dev);
1074 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
1076 int index = sattr->index;
1078 int err = kstrtoul(buf, 10, &val);
1081 mutex_lock(&data->update_lock);
1082 data->in[nr][index] = in_to_reg(val, nr);
1083 nct6775_write_value(data, data->REG_IN_MINMAX[index-1][nr],
1084 data->in[nr][index]);
1085 mutex_unlock(&data->update_lock);
1090 show_alarm(struct device *dev, struct device_attribute *attr, char *buf)
1092 struct nct6775_data *data = nct6775_update_device(dev);
1093 struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr);
1094 int nr = data->ALARM_BITS[sattr->index];
1095 return sprintf(buf, "%u\n",
1096 (unsigned int)((data->alarms >> nr) & 0x01));
1099 static SENSOR_DEVICE_ATTR_2(in0_input, S_IRUGO, show_in_reg, NULL, 0, 0);
1100 static SENSOR_DEVICE_ATTR_2(in1_input, S_IRUGO, show_in_reg, NULL, 1, 0);
1101 static SENSOR_DEVICE_ATTR_2(in2_input, S_IRUGO, show_in_reg, NULL, 2, 0);
1102 static SENSOR_DEVICE_ATTR_2(in3_input, S_IRUGO, show_in_reg, NULL, 3, 0);
1103 static SENSOR_DEVICE_ATTR_2(in4_input, S_IRUGO, show_in_reg, NULL, 4, 0);
1104 static SENSOR_DEVICE_ATTR_2(in5_input, S_IRUGO, show_in_reg, NULL, 5, 0);
1105 static SENSOR_DEVICE_ATTR_2(in6_input, S_IRUGO, show_in_reg, NULL, 6, 0);
1106 static SENSOR_DEVICE_ATTR_2(in7_input, S_IRUGO, show_in_reg, NULL, 7, 0);
1107 static SENSOR_DEVICE_ATTR_2(in8_input, S_IRUGO, show_in_reg, NULL, 8, 0);
1108 static SENSOR_DEVICE_ATTR_2(in9_input, S_IRUGO, show_in_reg, NULL, 9, 0);
1109 static SENSOR_DEVICE_ATTR_2(in10_input, S_IRUGO, show_in_reg, NULL, 10, 0);
1110 static SENSOR_DEVICE_ATTR_2(in11_input, S_IRUGO, show_in_reg, NULL, 11, 0);
1111 static SENSOR_DEVICE_ATTR_2(in12_input, S_IRUGO, show_in_reg, NULL, 12, 0);
1112 static SENSOR_DEVICE_ATTR_2(in13_input, S_IRUGO, show_in_reg, NULL, 13, 0);
1113 static SENSOR_DEVICE_ATTR_2(in14_input, S_IRUGO, show_in_reg, NULL, 14, 0);
1115 static SENSOR_DEVICE_ATTR(in0_alarm, S_IRUGO, show_alarm, NULL, 0);
1116 static SENSOR_DEVICE_ATTR(in1_alarm, S_IRUGO, show_alarm, NULL, 1);
1117 static SENSOR_DEVICE_ATTR(in2_alarm, S_IRUGO, show_alarm, NULL, 2);
1118 static SENSOR_DEVICE_ATTR(in3_alarm, S_IRUGO, show_alarm, NULL, 3);
1119 static SENSOR_DEVICE_ATTR(in4_alarm, S_IRUGO, show_alarm, NULL, 4);
1120 static SENSOR_DEVICE_ATTR(in5_alarm, S_IRUGO, show_alarm, NULL, 5);
1121 static SENSOR_DEVICE_ATTR(in6_alarm, S_IRUGO, show_alarm, NULL, 6);
1122 static SENSOR_DEVICE_ATTR(in7_alarm, S_IRUGO, show_alarm, NULL, 7);
1123 static SENSOR_DEVICE_ATTR(in8_alarm, S_IRUGO, show_alarm, NULL, 8);
1124 static SENSOR_DEVICE_ATTR(in9_alarm, S_IRUGO, show_alarm, NULL, 9);
1125 static SENSOR_DEVICE_ATTR(in10_alarm, S_IRUGO, show_alarm, NULL, 10);
1126 static SENSOR_DEVICE_ATTR(in11_alarm, S_IRUGO, show_alarm, NULL, 11);
1127 static SENSOR_DEVICE_ATTR(in12_alarm, S_IRUGO, show_alarm, NULL, 12);
1128 static SENSOR_DEVICE_ATTR(in13_alarm, S_IRUGO, show_alarm, NULL, 13);
1129 static SENSOR_DEVICE_ATTR(in14_alarm, S_IRUGO, show_alarm, NULL, 14);
1131 static SENSOR_DEVICE_ATTR_2(in0_min, S_IWUSR | S_IRUGO, show_in_reg,
1132 store_in_reg, 0, 1);
1133 static SENSOR_DEVICE_ATTR_2(in1_min, S_IWUSR | S_IRUGO, show_in_reg,
1134 store_in_reg, 1, 1);
1135 static SENSOR_DEVICE_ATTR_2(in2_min, S_IWUSR | S_IRUGO, show_in_reg,
1136 store_in_reg, 2, 1);
1137 static SENSOR_DEVICE_ATTR_2(in3_min, S_IWUSR | S_IRUGO, show_in_reg,
1138 store_in_reg, 3, 1);
1139 static SENSOR_DEVICE_ATTR_2(in4_min, S_IWUSR | S_IRUGO, show_in_reg,
1140 store_in_reg, 4, 1);
1141 static SENSOR_DEVICE_ATTR_2(in5_min, S_IWUSR | S_IRUGO, show_in_reg,
1142 store_in_reg, 5, 1);
1143 static SENSOR_DEVICE_ATTR_2(in6_min, S_IWUSR | S_IRUGO, show_in_reg,
1144 store_in_reg, 6, 1);
1145 static SENSOR_DEVICE_ATTR_2(in7_min, S_IWUSR | S_IRUGO, show_in_reg,
1146 store_in_reg, 7, 1);
1147 static SENSOR_DEVICE_ATTR_2(in8_min, S_IWUSR | S_IRUGO, show_in_reg,
1148 store_in_reg, 8, 1);
1149 static SENSOR_DEVICE_ATTR_2(in9_min, S_IWUSR | S_IRUGO, show_in_reg,
1150 store_in_reg, 9, 1);
1151 static SENSOR_DEVICE_ATTR_2(in10_min, S_IWUSR | S_IRUGO, show_in_reg,
1152 store_in_reg, 10, 1);
1153 static SENSOR_DEVICE_ATTR_2(in11_min, S_IWUSR | S_IRUGO, show_in_reg,
1154 store_in_reg, 11, 1);
1155 static SENSOR_DEVICE_ATTR_2(in12_min, S_IWUSR | S_IRUGO, show_in_reg,
1156 store_in_reg, 12, 1);
1157 static SENSOR_DEVICE_ATTR_2(in13_min, S_IWUSR | S_IRUGO, show_in_reg,
1158 store_in_reg, 13, 1);
1159 static SENSOR_DEVICE_ATTR_2(in14_min, S_IWUSR | S_IRUGO, show_in_reg,
1160 store_in_reg, 14, 1);
1162 static SENSOR_DEVICE_ATTR_2(in0_max, S_IWUSR | S_IRUGO, show_in_reg,
1163 store_in_reg, 0, 2);
1164 static SENSOR_DEVICE_ATTR_2(in1_max, S_IWUSR | S_IRUGO, show_in_reg,
1165 store_in_reg, 1, 2);
1166 static SENSOR_DEVICE_ATTR_2(in2_max, S_IWUSR | S_IRUGO, show_in_reg,
1167 store_in_reg, 2, 2);
1168 static SENSOR_DEVICE_ATTR_2(in3_max, S_IWUSR | S_IRUGO, show_in_reg,
1169 store_in_reg, 3, 2);
1170 static SENSOR_DEVICE_ATTR_2(in4_max, S_IWUSR | S_IRUGO, show_in_reg,
1171 store_in_reg, 4, 2);
1172 static SENSOR_DEVICE_ATTR_2(in5_max, S_IWUSR | S_IRUGO, show_in_reg,
1173 store_in_reg, 5, 2);
1174 static SENSOR_DEVICE_ATTR_2(in6_max, S_IWUSR | S_IRUGO, show_in_reg,
1175 store_in_reg, 6, 2);
1176 static SENSOR_DEVICE_ATTR_2(in7_max, S_IWUSR | S_IRUGO, show_in_reg,
1177 store_in_reg, 7, 2);
1178 static SENSOR_DEVICE_ATTR_2(in8_max, S_IWUSR | S_IRUGO, show_in_reg,
1179 store_in_reg, 8, 2);
1180 static SENSOR_DEVICE_ATTR_2(in9_max, S_IWUSR | S_IRUGO, show_in_reg,
1181 store_in_reg, 9, 2);
1182 static SENSOR_DEVICE_ATTR_2(in10_max, S_IWUSR | S_IRUGO, show_in_reg,
1183 store_in_reg, 10, 2);
1184 static SENSOR_DEVICE_ATTR_2(in11_max, S_IWUSR | S_IRUGO, show_in_reg,
1185 store_in_reg, 11, 2);
1186 static SENSOR_DEVICE_ATTR_2(in12_max, S_IWUSR | S_IRUGO, show_in_reg,
1187 store_in_reg, 12, 2);
1188 static SENSOR_DEVICE_ATTR_2(in13_max, S_IWUSR | S_IRUGO, show_in_reg,
1189 store_in_reg, 13, 2);
1190 static SENSOR_DEVICE_ATTR_2(in14_max, S_IWUSR | S_IRUGO, show_in_reg,
1191 store_in_reg, 14, 2);
1193 static struct attribute *nct6775_attributes_in[15][5] = {
1195 &sensor_dev_attr_in0_input.dev_attr.attr,
1196 &sensor_dev_attr_in0_min.dev_attr.attr,
1197 &sensor_dev_attr_in0_max.dev_attr.attr,
1198 &sensor_dev_attr_in0_alarm.dev_attr.attr,
1202 &sensor_dev_attr_in1_input.dev_attr.attr,
1203 &sensor_dev_attr_in1_min.dev_attr.attr,
1204 &sensor_dev_attr_in1_max.dev_attr.attr,
1205 &sensor_dev_attr_in1_alarm.dev_attr.attr,
1209 &sensor_dev_attr_in2_input.dev_attr.attr,
1210 &sensor_dev_attr_in2_min.dev_attr.attr,
1211 &sensor_dev_attr_in2_max.dev_attr.attr,
1212 &sensor_dev_attr_in2_alarm.dev_attr.attr,
1216 &sensor_dev_attr_in3_input.dev_attr.attr,
1217 &sensor_dev_attr_in3_min.dev_attr.attr,
1218 &sensor_dev_attr_in3_max.dev_attr.attr,
1219 &sensor_dev_attr_in3_alarm.dev_attr.attr,
1223 &sensor_dev_attr_in4_input.dev_attr.attr,
1224 &sensor_dev_attr_in4_min.dev_attr.attr,
1225 &sensor_dev_attr_in4_max.dev_attr.attr,
1226 &sensor_dev_attr_in4_alarm.dev_attr.attr,
1230 &sensor_dev_attr_in5_input.dev_attr.attr,
1231 &sensor_dev_attr_in5_min.dev_attr.attr,
1232 &sensor_dev_attr_in5_max.dev_attr.attr,
1233 &sensor_dev_attr_in5_alarm.dev_attr.attr,
1237 &sensor_dev_attr_in6_input.dev_attr.attr,
1238 &sensor_dev_attr_in6_min.dev_attr.attr,
1239 &sensor_dev_attr_in6_max.dev_attr.attr,
1240 &sensor_dev_attr_in6_alarm.dev_attr.attr,
1244 &sensor_dev_attr_in7_input.dev_attr.attr,
1245 &sensor_dev_attr_in7_min.dev_attr.attr,
1246 &sensor_dev_attr_in7_max.dev_attr.attr,
1247 &sensor_dev_attr_in7_alarm.dev_attr.attr,
1251 &sensor_dev_attr_in8_input.dev_attr.attr,
1252 &sensor_dev_attr_in8_min.dev_attr.attr,
1253 &sensor_dev_attr_in8_max.dev_attr.attr,
1254 &sensor_dev_attr_in8_alarm.dev_attr.attr,
1258 &sensor_dev_attr_in9_input.dev_attr.attr,
1259 &sensor_dev_attr_in9_min.dev_attr.attr,
1260 &sensor_dev_attr_in9_max.dev_attr.attr,
1261 &sensor_dev_attr_in9_alarm.dev_attr.attr,
1265 &sensor_dev_attr_in10_input.dev_attr.attr,
1266 &sensor_dev_attr_in10_min.dev_attr.attr,
1267 &sensor_dev_attr_in10_max.dev_attr.attr,
1268 &sensor_dev_attr_in10_alarm.dev_attr.attr,
1272 &sensor_dev_attr_in11_input.dev_attr.attr,
1273 &sensor_dev_attr_in11_min.dev_attr.attr,
1274 &sensor_dev_attr_in11_max.dev_attr.attr,
1275 &sensor_dev_attr_in11_alarm.dev_attr.attr,
1279 &sensor_dev_attr_in12_input.dev_attr.attr,
1280 &sensor_dev_attr_in12_min.dev_attr.attr,
1281 &sensor_dev_attr_in12_max.dev_attr.attr,
1282 &sensor_dev_attr_in12_alarm.dev_attr.attr,
1286 &sensor_dev_attr_in13_input.dev_attr.attr,
1287 &sensor_dev_attr_in13_min.dev_attr.attr,
1288 &sensor_dev_attr_in13_max.dev_attr.attr,
1289 &sensor_dev_attr_in13_alarm.dev_attr.attr,
1293 &sensor_dev_attr_in14_input.dev_attr.attr,
1294 &sensor_dev_attr_in14_min.dev_attr.attr,
1295 &sensor_dev_attr_in14_max.dev_attr.attr,
1296 &sensor_dev_attr_in14_alarm.dev_attr.attr,
1301 static const struct attribute_group nct6775_group_in[15] = {
1302 { .attrs = nct6775_attributes_in[0] },
1303 { .attrs = nct6775_attributes_in[1] },
1304 { .attrs = nct6775_attributes_in[2] },
1305 { .attrs = nct6775_attributes_in[3] },
1306 { .attrs = nct6775_attributes_in[4] },
1307 { .attrs = nct6775_attributes_in[5] },
1308 { .attrs = nct6775_attributes_in[6] },
1309 { .attrs = nct6775_attributes_in[7] },
1310 { .attrs = nct6775_attributes_in[8] },
1311 { .attrs = nct6775_attributes_in[9] },
1312 { .attrs = nct6775_attributes_in[10] },
1313 { .attrs = nct6775_attributes_in[11] },
1314 { .attrs = nct6775_attributes_in[12] },
1315 { .attrs = nct6775_attributes_in[13] },
1316 { .attrs = nct6775_attributes_in[14] },
1320 show_fan(struct device *dev, struct device_attribute *attr, char *buf)
1322 struct nct6775_data *data = nct6775_update_device(dev);
1323 struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr);
1324 int nr = sattr->index;
1325 return sprintf(buf, "%d\n", data->rpm[nr]);
1329 show_fan_min(struct device *dev, struct device_attribute *attr, char *buf)
1331 struct nct6775_data *data = nct6775_update_device(dev);
1332 struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr);
1333 int nr = sattr->index;
1334 return sprintf(buf, "%d\n",
1335 data->fan_from_reg_min(data->fan_min[nr],
1336 data->fan_div[nr]));
1340 show_fan_div(struct device *dev, struct device_attribute *attr, char *buf)
1342 struct nct6775_data *data = nct6775_update_device(dev);
1343 struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr);
1344 int nr = sattr->index;
1345 return sprintf(buf, "%u\n", div_from_reg(data->fan_div[nr]));
1349 store_fan_min(struct device *dev, struct device_attribute *attr,
1350 const char *buf, size_t count)
1352 struct nct6775_data *data = dev_get_drvdata(dev);
1353 struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr);
1354 int nr = sattr->index;
1360 err = kstrtoul(buf, 10, &val);
1364 mutex_lock(&data->update_lock);
1365 if (!data->has_fan_div) {
1366 /* NCT6776F or NCT6779D; we know this is a 13 bit register */
1372 val = 1350000U / val;
1373 val = (val & 0x1f) | ((val << 3) & 0xff00);
1375 data->fan_min[nr] = val;
1376 goto write_min; /* Leave fan divider alone */
1379 /* No min limit, alarm disabled */
1380 data->fan_min[nr] = 255;
1381 new_div = data->fan_div[nr]; /* No change */
1382 dev_info(dev, "fan%u low limit and alarm disabled\n", nr + 1);
1385 reg = 1350000U / val;
1386 if (reg >= 128 * 255) {
1388 * Speed below this value cannot possibly be represented,
1389 * even with the highest divider (128)
1391 data->fan_min[nr] = 254;
1392 new_div = 7; /* 128 == (1 << 7) */
1394 "fan%u low limit %lu below minimum %u, set to minimum\n",
1395 nr + 1, val, data->fan_from_reg_min(254, 7));
1398 * Speed above this value cannot possibly be represented,
1399 * even with the lowest divider (1)
1401 data->fan_min[nr] = 1;
1402 new_div = 0; /* 1 == (1 << 0) */
1404 "fan%u low limit %lu above maximum %u, set to maximum\n",
1405 nr + 1, val, data->fan_from_reg_min(1, 0));
1408 * Automatically pick the best divider, i.e. the one such
1409 * that the min limit will correspond to a register value
1410 * in the 96..192 range
1413 while (reg > 192 && new_div < 7) {
1417 data->fan_min[nr] = reg;
1422 * Write both the fan clock divider (if it changed) and the new
1423 * fan min (unconditionally)
1425 if (new_div != data->fan_div[nr]) {
1426 dev_dbg(dev, "fan%u clock divider changed from %u to %u\n",
1427 nr + 1, div_from_reg(data->fan_div[nr]),
1428 div_from_reg(new_div));
1429 data->fan_div[nr] = new_div;
1430 nct6775_write_fan_div_common(dev, data, nr);
1431 /* Give the chip time to sample a new speed value */
1432 data->last_updated = jiffies;
1436 nct6775_write_value(data, data->REG_FAN_MIN[nr], data->fan_min[nr]);
1437 mutex_unlock(&data->update_lock);
1442 static struct sensor_device_attribute sda_fan_input[] = {
1443 SENSOR_ATTR(fan1_input, S_IRUGO, show_fan, NULL, 0),
1444 SENSOR_ATTR(fan2_input, S_IRUGO, show_fan, NULL, 1),
1445 SENSOR_ATTR(fan3_input, S_IRUGO, show_fan, NULL, 2),
1446 SENSOR_ATTR(fan4_input, S_IRUGO, show_fan, NULL, 3),
1447 SENSOR_ATTR(fan5_input, S_IRUGO, show_fan, NULL, 4),
1450 static struct sensor_device_attribute sda_fan_alarm[] = {
1451 SENSOR_ATTR(fan1_alarm, S_IRUGO, show_alarm, NULL, FAN_ALARM_BASE),
1452 SENSOR_ATTR(fan2_alarm, S_IRUGO, show_alarm, NULL, FAN_ALARM_BASE + 1),
1453 SENSOR_ATTR(fan3_alarm, S_IRUGO, show_alarm, NULL, FAN_ALARM_BASE + 2),
1454 SENSOR_ATTR(fan4_alarm, S_IRUGO, show_alarm, NULL, FAN_ALARM_BASE + 3),
1455 SENSOR_ATTR(fan5_alarm, S_IRUGO, show_alarm, NULL, FAN_ALARM_BASE + 4),
1458 static struct sensor_device_attribute sda_fan_min[] = {
1459 SENSOR_ATTR(fan1_min, S_IWUSR | S_IRUGO, show_fan_min,
1461 SENSOR_ATTR(fan2_min, S_IWUSR | S_IRUGO, show_fan_min,
1463 SENSOR_ATTR(fan3_min, S_IWUSR | S_IRUGO, show_fan_min,
1465 SENSOR_ATTR(fan4_min, S_IWUSR | S_IRUGO, show_fan_min,
1467 SENSOR_ATTR(fan5_min, S_IWUSR | S_IRUGO, show_fan_min,
1471 static struct sensor_device_attribute sda_fan_div[] = {
1472 SENSOR_ATTR(fan1_div, S_IRUGO, show_fan_div, NULL, 0),
1473 SENSOR_ATTR(fan2_div, S_IRUGO, show_fan_div, NULL, 1),
1474 SENSOR_ATTR(fan3_div, S_IRUGO, show_fan_div, NULL, 2),
1475 SENSOR_ATTR(fan4_div, S_IRUGO, show_fan_div, NULL, 3),
1476 SENSOR_ATTR(fan5_div, S_IRUGO, show_fan_div, NULL, 4),
1480 show_temp_label(struct device *dev, struct device_attribute *attr, char *buf)
1482 struct nct6775_data *data = nct6775_update_device(dev);
1483 struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr);
1484 int nr = sattr->index;
1485 return sprintf(buf, "%s\n", data->temp_label[data->temp_src[nr]]);
1489 show_temp(struct device *dev, struct device_attribute *attr, char *buf)
1491 struct nct6775_data *data = nct6775_update_device(dev);
1492 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
1494 int index = sattr->index;
1496 return sprintf(buf, "%d\n", LM75_TEMP_FROM_REG(data->temp[index][nr]));
1500 store_temp(struct device *dev, struct device_attribute *attr, const char *buf,
1503 struct nct6775_data *data = dev_get_drvdata(dev);
1504 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
1506 int index = sattr->index;
1510 err = kstrtol(buf, 10, &val);
1514 mutex_lock(&data->update_lock);
1515 data->temp[index][nr] = LM75_TEMP_TO_REG(val);
1516 nct6775_write_temp(data, data->reg_temp[index][nr],
1517 data->temp[index][nr]);
1518 mutex_unlock(&data->update_lock);
1523 show_temp_offset(struct device *dev, struct device_attribute *attr, char *buf)
1525 struct nct6775_data *data = nct6775_update_device(dev);
1526 struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr);
1528 return sprintf(buf, "%d\n", data->temp_offset[sattr->index] * 1000);
1532 store_temp_offset(struct device *dev, struct device_attribute *attr,
1533 const char *buf, size_t count)
1535 struct nct6775_data *data = dev_get_drvdata(dev);
1536 struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr);
1537 int nr = sattr->index;
1541 err = kstrtol(buf, 10, &val);
1545 val = SENSORS_LIMIT(DIV_ROUND_CLOSEST(val, 1000), -128, 127);
1547 mutex_lock(&data->update_lock);
1548 data->temp_offset[nr] = val;
1549 nct6775_write_value(data, data->REG_TEMP_OFFSET[nr], val);
1550 mutex_unlock(&data->update_lock);
1556 show_temp_type(struct device *dev, struct device_attribute *attr, char *buf)
1558 struct nct6775_data *data = nct6775_update_device(dev);
1559 struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr);
1560 int nr = sattr->index;
1561 return sprintf(buf, "%d\n", (int)data->temp_type[nr]);
1565 store_temp_type(struct device *dev, struct device_attribute *attr,
1566 const char *buf, size_t count)
1568 struct nct6775_data *data = nct6775_update_device(dev);
1569 struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr);
1570 int nr = sattr->index;
1573 u8 vbat, diode, bit;
1575 err = kstrtoul(buf, 10, &val);
1579 if (val != 1 && val != 3 && val != 4)
1582 mutex_lock(&data->update_lock);
1584 data->temp_type[nr] = val;
1585 vbat = nct6775_read_value(data, data->REG_VBAT) & ~(0x02 << nr);
1586 diode = nct6775_read_value(data, data->REG_DIODE) & ~(0x02 << nr);
1589 case 1: /* CPU diode (diode, current mode) */
1593 case 3: /* diode, voltage mode */
1596 case 4: /* thermistor */
1599 nct6775_write_value(data, data->REG_VBAT, vbat);
1600 nct6775_write_value(data, data->REG_DIODE, diode);
1602 mutex_unlock(&data->update_lock);
1606 static struct sensor_device_attribute_2 sda_temp_input[] = {
1607 SENSOR_ATTR_2(temp1_input, S_IRUGO, show_temp, NULL, 0, 0),
1608 SENSOR_ATTR_2(temp2_input, S_IRUGO, show_temp, NULL, 1, 0),
1609 SENSOR_ATTR_2(temp3_input, S_IRUGO, show_temp, NULL, 2, 0),
1610 SENSOR_ATTR_2(temp4_input, S_IRUGO, show_temp, NULL, 3, 0),
1611 SENSOR_ATTR_2(temp5_input, S_IRUGO, show_temp, NULL, 4, 0),
1612 SENSOR_ATTR_2(temp6_input, S_IRUGO, show_temp, NULL, 5, 0),
1613 SENSOR_ATTR_2(temp7_input, S_IRUGO, show_temp, NULL, 6, 0),
1614 SENSOR_ATTR_2(temp8_input, S_IRUGO, show_temp, NULL, 7, 0),
1615 SENSOR_ATTR_2(temp9_input, S_IRUGO, show_temp, NULL, 8, 0),
1616 SENSOR_ATTR_2(temp10_input, S_IRUGO, show_temp, NULL, 9, 0),
1619 static struct sensor_device_attribute sda_temp_label[] = {
1620 SENSOR_ATTR(temp1_label, S_IRUGO, show_temp_label, NULL, 0),
1621 SENSOR_ATTR(temp2_label, S_IRUGO, show_temp_label, NULL, 1),
1622 SENSOR_ATTR(temp3_label, S_IRUGO, show_temp_label, NULL, 2),
1623 SENSOR_ATTR(temp4_label, S_IRUGO, show_temp_label, NULL, 3),
1624 SENSOR_ATTR(temp5_label, S_IRUGO, show_temp_label, NULL, 4),
1625 SENSOR_ATTR(temp6_label, S_IRUGO, show_temp_label, NULL, 5),
1626 SENSOR_ATTR(temp7_label, S_IRUGO, show_temp_label, NULL, 6),
1627 SENSOR_ATTR(temp8_label, S_IRUGO, show_temp_label, NULL, 7),
1628 SENSOR_ATTR(temp9_label, S_IRUGO, show_temp_label, NULL, 8),
1629 SENSOR_ATTR(temp10_label, S_IRUGO, show_temp_label, NULL, 9),
1632 static struct sensor_device_attribute_2 sda_temp_max[] = {
1633 SENSOR_ATTR_2(temp1_max, S_IRUGO | S_IWUSR, show_temp, store_temp,
1635 SENSOR_ATTR_2(temp2_max, S_IRUGO | S_IWUSR, show_temp, store_temp,
1637 SENSOR_ATTR_2(temp3_max, S_IRUGO | S_IWUSR, show_temp, store_temp,
1639 SENSOR_ATTR_2(temp4_max, S_IRUGO | S_IWUSR, show_temp, store_temp,
1641 SENSOR_ATTR_2(temp5_max, S_IRUGO | S_IWUSR, show_temp, store_temp,
1643 SENSOR_ATTR_2(temp6_max, S_IRUGO | S_IWUSR, show_temp, store_temp,
1645 SENSOR_ATTR_2(temp7_max, S_IRUGO | S_IWUSR, show_temp, store_temp,
1647 SENSOR_ATTR_2(temp8_max, S_IRUGO | S_IWUSR, show_temp, store_temp,
1649 SENSOR_ATTR_2(temp9_max, S_IRUGO | S_IWUSR, show_temp, store_temp,
1651 SENSOR_ATTR_2(temp10_max, S_IRUGO | S_IWUSR, show_temp, store_temp,
1655 static struct sensor_device_attribute_2 sda_temp_max_hyst[] = {
1656 SENSOR_ATTR_2(temp1_max_hyst, S_IRUGO | S_IWUSR, show_temp, store_temp,
1658 SENSOR_ATTR_2(temp2_max_hyst, S_IRUGO | S_IWUSR, show_temp, store_temp,
1660 SENSOR_ATTR_2(temp3_max_hyst, S_IRUGO | S_IWUSR, show_temp, store_temp,
1662 SENSOR_ATTR_2(temp4_max_hyst, S_IRUGO | S_IWUSR, show_temp, store_temp,
1664 SENSOR_ATTR_2(temp5_max_hyst, S_IRUGO | S_IWUSR, show_temp, store_temp,
1666 SENSOR_ATTR_2(temp6_max_hyst, S_IRUGO | S_IWUSR, show_temp, store_temp,
1668 SENSOR_ATTR_2(temp7_max_hyst, S_IRUGO | S_IWUSR, show_temp, store_temp,
1670 SENSOR_ATTR_2(temp8_max_hyst, S_IRUGO | S_IWUSR, show_temp, store_temp,
1672 SENSOR_ATTR_2(temp9_max_hyst, S_IRUGO | S_IWUSR, show_temp, store_temp,
1674 SENSOR_ATTR_2(temp10_max_hyst, S_IRUGO | S_IWUSR, show_temp, store_temp,
1678 static struct sensor_device_attribute_2 sda_temp_crit[] = {
1679 SENSOR_ATTR_2(temp1_crit, S_IRUGO | S_IWUSR, show_temp, store_temp,
1681 SENSOR_ATTR_2(temp2_crit, S_IRUGO | S_IWUSR, show_temp, store_temp,
1683 SENSOR_ATTR_2(temp3_crit, S_IRUGO | S_IWUSR, show_temp, store_temp,
1685 SENSOR_ATTR_2(temp4_crit, S_IRUGO | S_IWUSR, show_temp, store_temp,
1687 SENSOR_ATTR_2(temp5_crit, S_IRUGO | S_IWUSR, show_temp, store_temp,
1689 SENSOR_ATTR_2(temp6_crit, S_IRUGO | S_IWUSR, show_temp, store_temp,
1691 SENSOR_ATTR_2(temp7_crit, S_IRUGO | S_IWUSR, show_temp, store_temp,
1693 SENSOR_ATTR_2(temp8_crit, S_IRUGO | S_IWUSR, show_temp, store_temp,
1695 SENSOR_ATTR_2(temp9_crit, S_IRUGO | S_IWUSR, show_temp, store_temp,
1697 SENSOR_ATTR_2(temp10_crit, S_IRUGO | S_IWUSR, show_temp, store_temp,
1701 static struct sensor_device_attribute sda_temp_offset[] = {
1702 SENSOR_ATTR(temp1_offset, S_IRUGO | S_IWUSR, show_temp_offset,
1703 store_temp_offset, 0),
1704 SENSOR_ATTR(temp2_offset, S_IRUGO | S_IWUSR, show_temp_offset,
1705 store_temp_offset, 1),
1706 SENSOR_ATTR(temp3_offset, S_IRUGO | S_IWUSR, show_temp_offset,
1707 store_temp_offset, 2),
1708 SENSOR_ATTR(temp4_offset, S_IRUGO | S_IWUSR, show_temp_offset,
1709 store_temp_offset, 3),
1710 SENSOR_ATTR(temp5_offset, S_IRUGO | S_IWUSR, show_temp_offset,
1711 store_temp_offset, 4),
1712 SENSOR_ATTR(temp6_offset, S_IRUGO | S_IWUSR, show_temp_offset,
1713 store_temp_offset, 5),
1716 static struct sensor_device_attribute sda_temp_type[] = {
1717 SENSOR_ATTR(temp1_type, S_IRUGO | S_IWUSR, show_temp_type,
1718 store_temp_type, 0),
1719 SENSOR_ATTR(temp2_type, S_IRUGO | S_IWUSR, show_temp_type,
1720 store_temp_type, 1),
1721 SENSOR_ATTR(temp3_type, S_IRUGO | S_IWUSR, show_temp_type,
1722 store_temp_type, 2),
1723 SENSOR_ATTR(temp4_type, S_IRUGO | S_IWUSR, show_temp_type,
1724 store_temp_type, 3),
1725 SENSOR_ATTR(temp5_type, S_IRUGO | S_IWUSR, show_temp_type,
1726 store_temp_type, 4),
1727 SENSOR_ATTR(temp6_type, S_IRUGO | S_IWUSR, show_temp_type,
1728 store_temp_type, 5),
1731 static struct sensor_device_attribute sda_temp_alarm[] = {
1732 SENSOR_ATTR(temp1_alarm, S_IRUGO, show_alarm, NULL,
1734 SENSOR_ATTR(temp2_alarm, S_IRUGO, show_alarm, NULL,
1735 TEMP_ALARM_BASE + 1),
1736 SENSOR_ATTR(temp3_alarm, S_IRUGO, show_alarm, NULL,
1737 TEMP_ALARM_BASE + 2),
1738 SENSOR_ATTR(temp4_alarm, S_IRUGO, show_alarm, NULL,
1739 TEMP_ALARM_BASE + 3),
1740 SENSOR_ATTR(temp5_alarm, S_IRUGO, show_alarm, NULL,
1741 TEMP_ALARM_BASE + 4),
1742 SENSOR_ATTR(temp6_alarm, S_IRUGO, show_alarm, NULL,
1743 TEMP_ALARM_BASE + 5),
1746 #define NUM_TEMP_ALARM ARRAY_SIZE(sda_temp_alarm)
1749 show_pwm_mode(struct device *dev, struct device_attribute *attr, char *buf)
1751 struct nct6775_data *data = nct6775_update_device(dev);
1752 struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr);
1754 return sprintf(buf, "%d\n", !data->pwm_mode[sattr->index]);
1758 store_pwm_mode(struct device *dev, struct device_attribute *attr,
1759 const char *buf, size_t count)
1761 struct nct6775_data *data = dev_get_drvdata(dev);
1762 struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr);
1763 int nr = sattr->index;
1768 err = kstrtoul(buf, 10, &val);
1775 /* Setting DC mode is not supported for all chips/channels */
1776 if (data->REG_PWM_MODE[nr] == 0) {
1782 mutex_lock(&data->update_lock);
1783 data->pwm_mode[nr] = val;
1784 reg = nct6775_read_value(data, data->REG_PWM_MODE[nr]);
1785 reg &= ~data->PWM_MODE_MASK[nr];
1787 reg |= data->PWM_MODE_MASK[nr];
1788 nct6775_write_value(data, data->REG_PWM_MODE[nr], reg);
1789 mutex_unlock(&data->update_lock);
1794 show_pwm_enable(struct device *dev, struct device_attribute *attr, char *buf)
1796 struct nct6775_data *data = nct6775_update_device(dev);
1797 struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr);
1799 return sprintf(buf, "%d\n", data->pwm_enable[sattr->index]);
1803 show_pwm(struct device *dev, struct device_attribute *attr, char *buf)
1805 struct nct6775_data *data = nct6775_update_device(dev);
1806 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
1808 int index = sattr->index;
1812 * For automatic fan control modes, show current pwm readings.
1813 * Otherwise, show the configured value.
1815 if (index == 0 && data->pwm_enable[nr] > 1)
1816 pwm = nct6775_read_value(data, data->REG_PWM_READ[nr]);
1818 pwm = data->pwm[index][nr];
1820 return sprintf(buf, "%d\n", pwm);
1824 store_pwm(struct device *dev, struct device_attribute *attr, const char *buf,
1827 struct nct6775_data *data = dev_get_drvdata(dev);
1828 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
1830 int index = sattr->index;
1832 int minval[7] = { 0, 1, 1, data->pwm[2][nr], 0, 0, 0 };
1834 = { 255, 255, data->pwm[3][nr] ? : 255, 255, 255, 255, 255 };
1837 err = kstrtoul(buf, 10, &val);
1840 val = SENSORS_LIMIT(val, minval[index], maxval[index]);
1842 mutex_lock(&data->update_lock);
1843 data->pwm[index][nr] = val;
1844 nct6775_write_value(data, data->REG_PWM[index][nr], val);
1845 mutex_unlock(&data->update_lock);
1849 /* Returns 0 if OK, -EINVAL otherwise */
1850 static int check_trip_points(struct nct6775_data *data, int nr)
1854 for (i = 0; i < data->auto_pwm_num - 1; i++) {
1855 if (data->auto_temp[nr][i] > data->auto_temp[nr][i + 1])
1858 for (i = 0; i < data->auto_pwm_num - 1; i++) {
1859 if (data->auto_pwm[nr][i] > data->auto_pwm[nr][i + 1])
1862 /* validate critical temperature and pwm if enabled (pwm > 0) */
1863 if (data->auto_pwm[nr][data->auto_pwm_num]) {
1864 if (data->auto_temp[nr][data->auto_pwm_num - 1] >
1865 data->auto_temp[nr][data->auto_pwm_num] ||
1866 data->auto_pwm[nr][data->auto_pwm_num - 1] >
1867 data->auto_pwm[nr][data->auto_pwm_num])
1874 store_pwm_enable(struct device *dev, struct device_attribute *attr,
1875 const char *buf, size_t count)
1877 struct nct6775_data *data = dev_get_drvdata(dev);
1878 struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr);
1879 int nr = sattr->index;
1884 err = kstrtoul(buf, 10, &val);
1891 if (val == 4 && data->kind != nct6775)
1894 if (val == 5 && check_trip_points(data, nr)) {
1895 dev_err(dev, "Inconsistent trip points, not switching to SmartFan IV mode\n");
1896 dev_err(dev, "Adjust trip points and try again\n");
1900 mutex_lock(&data->update_lock);
1901 data->pwm_enable[nr] = val;
1904 * turn off pwm control: select manual mode, set pwm to maximum
1906 data->pwm[0][nr] = 255;
1907 nct6775_write_value(data, data->REG_PWM[0][nr], 255);
1909 reg = nct6775_read_value(data, data->REG_FAN_MODE[nr]);
1911 reg |= pwm_enable_to_reg(val) << 4;
1912 nct6775_write_value(data, data->REG_FAN_MODE[nr], reg);
1913 mutex_unlock(&data->update_lock);
1918 show_pwm_temp_sel(struct device *dev, struct device_attribute *attr, char *buf)
1920 struct nct6775_data *data = nct6775_update_device(dev);
1921 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
1922 int i, src, sel = 0;
1924 src = data->pwm_temp_sel[sattr->index][sattr->nr];
1926 for (i = 0; i < NUM_TEMP; i++) {
1927 if (!(data->have_temp & (1 << i)))
1929 if (src == data->temp_src[i]) {
1935 return sprintf(buf, "%d\n", sel);
1939 store_pwm_temp_sel(struct device *dev, struct device_attribute *attr,
1940 const char *buf, size_t count)
1942 struct nct6775_data *data = nct6775_update_device(dev);
1943 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
1945 int index = sattr->index;
1949 err = kstrtoul(buf, 10, &val);
1952 if (val == 0 || val > NUM_TEMP)
1954 if (!(data->have_temp & (1 << (val - 1))) || !data->temp_src[val - 1])
1957 mutex_lock(&data->update_lock);
1958 src = data->temp_src[val - 1];
1959 data->pwm_temp_sel[index][nr] = src;
1960 reg = nct6775_read_value(data, data->REG_TEMP_SEL[index][nr]);
1963 nct6775_write_value(data, data->REG_TEMP_SEL[index][nr], reg);
1964 mutex_unlock(&data->update_lock);
1970 show_target_temp(struct device *dev, struct device_attribute *attr, char *buf)
1972 struct nct6775_data *data = nct6775_update_device(dev);
1973 struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr);
1975 return sprintf(buf, "%d\n",
1976 data->target_temp[sattr->index] * 1000);
1980 store_target_temp(struct device *dev, struct device_attribute *attr,
1981 const char *buf, size_t count)
1983 struct nct6775_data *data = dev_get_drvdata(dev);
1984 struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr);
1985 int nr = sattr->index;
1989 err = kstrtol(buf, 10, &val);
1993 val = SENSORS_LIMIT(DIV_ROUND_CLOSEST(val, 1000), 0, 127);
1995 mutex_lock(&data->update_lock);
1996 data->target_temp[nr] = val;
1997 nct6775_write_value(data, data->REG_TARGET[nr], val);
1998 mutex_unlock(&data->update_lock);
2003 show_auto_temp_hyst(struct device *dev, struct device_attribute *attr,
2006 struct nct6775_data *data = nct6775_update_device(dev);
2007 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
2009 int point = sattr->index >= data->auto_pwm_num ? 1 : 0;
2010 int tolerance = data->tolerance[nr][point];
2011 int temp = data->auto_temp[nr][sattr->index];
2013 return sprintf(buf, "%d\n", (temp - tolerance) * 1000);
2017 store_auto_temp_hyst(struct device *dev, struct device_attribute *attr,
2018 const char *buf, size_t count)
2020 struct nct6775_data *data = dev_get_drvdata(dev);
2021 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
2023 int point = sattr->index >= data->auto_pwm_num ? 1 : 0;
2027 int maxlimit[2][3] = { { 15, 7, 63 }, { 15, 7, 7 } };
2028 int mask[] = { 0x0f, 0x07, 0x07 };
2031 err = kstrtol(buf, 10, &val);
2035 temp = data->auto_temp[nr][sattr->index];
2036 val = temp - DIV_ROUND_CLOSEST(val, 1000);
2038 /* Limit tolerance as needed */
2039 val = SENSORS_LIMIT(val, 0, maxlimit[point][data->kind]);
2041 mutex_lock(&data->update_lock);
2043 nct6775_write_value(data,
2044 data->REG_CRITICAL_TEMP_TOLERANCE[nr],
2047 reg = nct6775_read_value(data, data->REG_FAN_MODE[nr]);
2048 reg = (reg & ~mask[nr]) | (val & mask[nr]);
2049 nct6775_write_value(data, data->REG_FAN_MODE[nr], reg);
2050 if (data->kind == nct6779) {
2051 reg = nct6775_read_value(data,
2052 NCT6779_REG_TOLERANCE_H[nr]);
2053 reg = (reg & 0x70) | ((val & 0x38) << 1);
2054 nct6775_write_value(data,
2055 NCT6779_REG_TOLERANCE_H[nr], reg);
2059 data->tolerance[nr][point] = val;
2060 mutex_unlock(&data->update_lock);
2064 static SENSOR_DEVICE_ATTR_2(pwm1, S_IWUSR | S_IRUGO, show_pwm, store_pwm, 0, 0);
2065 static SENSOR_DEVICE_ATTR_2(pwm2, S_IWUSR | S_IRUGO, show_pwm, store_pwm, 1, 0);
2066 static SENSOR_DEVICE_ATTR_2(pwm3, S_IWUSR | S_IRUGO, show_pwm, store_pwm, 2, 0);
2067 static SENSOR_DEVICE_ATTR_2(pwm4, S_IWUSR | S_IRUGO, show_pwm, store_pwm, 3, 0);
2068 static SENSOR_DEVICE_ATTR_2(pwm5, S_IWUSR | S_IRUGO, show_pwm, store_pwm, 4, 0);
2070 static SENSOR_DEVICE_ATTR(pwm1_mode, S_IWUSR | S_IRUGO, show_pwm_mode,
2072 static SENSOR_DEVICE_ATTR(pwm2_mode, S_IWUSR | S_IRUGO, show_pwm_mode,
2074 static SENSOR_DEVICE_ATTR(pwm3_mode, S_IWUSR | S_IRUGO, show_pwm_mode,
2076 static SENSOR_DEVICE_ATTR(pwm4_mode, S_IWUSR | S_IRUGO, show_pwm_mode,
2078 static SENSOR_DEVICE_ATTR(pwm5_mode, S_IWUSR | S_IRUGO, show_pwm_mode,
2081 static SENSOR_DEVICE_ATTR(pwm1_enable, S_IWUSR | S_IRUGO, show_pwm_enable,
2082 store_pwm_enable, 0);
2083 static SENSOR_DEVICE_ATTR(pwm2_enable, S_IWUSR | S_IRUGO, show_pwm_enable,
2084 store_pwm_enable, 1);
2085 static SENSOR_DEVICE_ATTR(pwm3_enable, S_IWUSR | S_IRUGO, show_pwm_enable,
2086 store_pwm_enable, 2);
2087 static SENSOR_DEVICE_ATTR(pwm4_enable, S_IWUSR | S_IRUGO, show_pwm_enable,
2088 store_pwm_enable, 3);
2089 static SENSOR_DEVICE_ATTR(pwm5_enable, S_IWUSR | S_IRUGO, show_pwm_enable,
2090 store_pwm_enable, 4);
2092 static SENSOR_DEVICE_ATTR_2(pwm1_temp_sel, S_IWUSR | S_IRUGO,
2093 show_pwm_temp_sel, store_pwm_temp_sel, 0, 0);
2094 static SENSOR_DEVICE_ATTR_2(pwm2_temp_sel, S_IWUSR | S_IRUGO,
2095 show_pwm_temp_sel, store_pwm_temp_sel, 1, 0);
2096 static SENSOR_DEVICE_ATTR_2(pwm3_temp_sel, S_IWUSR | S_IRUGO,
2097 show_pwm_temp_sel, store_pwm_temp_sel, 2, 0);
2098 static SENSOR_DEVICE_ATTR_2(pwm4_temp_sel, S_IWUSR | S_IRUGO,
2099 show_pwm_temp_sel, store_pwm_temp_sel, 3, 0);
2100 static SENSOR_DEVICE_ATTR_2(pwm5_temp_sel, S_IWUSR | S_IRUGO,
2101 show_pwm_temp_sel, store_pwm_temp_sel, 4, 0);
2103 static SENSOR_DEVICE_ATTR(pwm1_target, S_IWUSR | S_IRUGO, show_target_temp,
2104 store_target_temp, 0);
2105 static SENSOR_DEVICE_ATTR(pwm2_target, S_IWUSR | S_IRUGO, show_target_temp,
2106 store_target_temp, 1);
2107 static SENSOR_DEVICE_ATTR(pwm3_target, S_IWUSR | S_IRUGO, show_target_temp,
2108 store_target_temp, 2);
2109 static SENSOR_DEVICE_ATTR(pwm4_target, S_IWUSR | S_IRUGO, show_target_temp,
2110 store_target_temp, 3);
2111 static SENSOR_DEVICE_ATTR(pwm5_target, S_IWUSR | S_IRUGO, show_target_temp,
2112 store_target_temp, 4);
2114 /* Smart Fan registers */
2117 show_pwm_sel_enable(struct device *dev, struct device_attribute *attr,
2120 struct nct6775_data *data = nct6775_update_device(dev);
2121 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
2123 return sprintf(buf, "%d\n",
2124 data->pwm_sel_enable[sattr->index][sattr->nr]);
2128 store_pwm_sel_enable(struct device *dev, struct device_attribute *attr,
2129 const char *buf, size_t count)
2131 struct nct6775_data *data = dev_get_drvdata(dev);
2132 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
2134 int index = sattr->index;
2138 static const u8 bit[] = { 0x80, 0x80, 0x01 };
2141 err = kstrtoul(buf, 10, &val);
2148 mutex_lock(&data->update_lock);
2149 data->pwm_sel_enable[index][nr] = val;
2150 reg = nct6775_read_value(data, data->REG_TEMP_SEL[index][nr]);
2154 nct6775_write_value(data, data->REG_TEMP_SEL[index][nr], reg);
2155 mutex_unlock(&data->update_lock);
2160 show_weight_temp(struct device *dev, struct device_attribute *attr, char *buf)
2162 struct nct6775_data *data = nct6775_update_device(dev);
2163 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
2165 int index = sattr->index;
2167 return sprintf(buf, "%d\n", data->weight_temp[index][nr] * 1000);
2171 store_weight_temp(struct device *dev, struct device_attribute *attr,
2172 const char *buf, size_t count)
2174 struct nct6775_data *data = dev_get_drvdata(dev);
2175 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
2177 int index = sattr->index;
2181 err = kstrtoul(buf, 10, &val);
2185 val = SENSORS_LIMIT(DIV_ROUND_CLOSEST(val, 1000), 0, 255);
2187 mutex_lock(&data->update_lock);
2188 data->weight_temp[index][nr] = val;
2189 nct6775_write_value(data, data->REG_WEIGHT_TEMP[index][nr], val);
2190 mutex_unlock(&data->update_lock);
2194 static SENSOR_DEVICE_ATTR_2(pwm1_stop_output_enable, S_IWUSR | S_IRUGO,
2195 show_pwm_sel_enable, store_pwm_sel_enable, 0, 0);
2196 static SENSOR_DEVICE_ATTR_2(pwm2_stop_output_enable, S_IWUSR | S_IRUGO,
2197 show_pwm_sel_enable, store_pwm_sel_enable, 1, 0);
2198 static SENSOR_DEVICE_ATTR_2(pwm3_stop_output_enable, S_IWUSR | S_IRUGO,
2199 show_pwm_sel_enable, store_pwm_sel_enable, 2, 0);
2200 static SENSOR_DEVICE_ATTR_2(pwm4_stop_output_enable, S_IWUSR | S_IRUGO,
2201 show_pwm_sel_enable, store_pwm_sel_enable, 3, 0);
2202 static SENSOR_DEVICE_ATTR_2(pwm5_stop_output_enable, S_IWUSR | S_IRUGO,
2203 show_pwm_sel_enable, store_pwm_sel_enable, 4, 0);
2205 static SENSOR_DEVICE_ATTR_2(pwm1_weight_enable, S_IWUSR | S_IRUGO,
2206 show_pwm_sel_enable, store_pwm_sel_enable, 0, 1);
2207 static SENSOR_DEVICE_ATTR_2(pwm2_weight_enable, S_IWUSR | S_IRUGO,
2208 show_pwm_sel_enable, store_pwm_sel_enable, 1, 1);
2209 static SENSOR_DEVICE_ATTR_2(pwm3_weight_enable, S_IWUSR | S_IRUGO,
2210 show_pwm_sel_enable, store_pwm_sel_enable, 2, 1);
2211 static SENSOR_DEVICE_ATTR_2(pwm4_weight_enable, S_IWUSR | S_IRUGO,
2212 show_pwm_sel_enable, store_pwm_sel_enable, 3, 1);
2213 static SENSOR_DEVICE_ATTR_2(pwm5_weight_enable, S_IWUSR | S_IRUGO,
2214 show_pwm_sel_enable, store_pwm_sel_enable, 4, 1);
2216 static SENSOR_DEVICE_ATTR_2(pwm1_step_enable, S_IWUSR | S_IRUGO,
2217 show_pwm_sel_enable, store_pwm_sel_enable, 0, 2);
2218 static SENSOR_DEVICE_ATTR_2(pwm2_step_enable, S_IWUSR | S_IRUGO,
2219 show_pwm_sel_enable, store_pwm_sel_enable, 1, 2);
2220 static SENSOR_DEVICE_ATTR_2(pwm3_step_enable, S_IWUSR | S_IRUGO,
2221 show_pwm_sel_enable, store_pwm_sel_enable, 2, 2);
2222 static SENSOR_DEVICE_ATTR_2(pwm4_step_enable, S_IWUSR | S_IRUGO,
2223 show_pwm_sel_enable, store_pwm_sel_enable, 3, 2);
2224 static SENSOR_DEVICE_ATTR_2(pwm5_step_enable, S_IWUSR | S_IRUGO,
2225 show_pwm_sel_enable, store_pwm_sel_enable, 4, 2);
2227 static SENSOR_DEVICE_ATTR_2(pwm1_weight_temp_sel, S_IWUSR | S_IRUGO,
2228 show_pwm_temp_sel, store_pwm_temp_sel, 0, 1);
2229 static SENSOR_DEVICE_ATTR_2(pwm2_weight_temp_sel, S_IWUSR | S_IRUGO,
2230 show_pwm_temp_sel, store_pwm_temp_sel, 1, 1);
2231 static SENSOR_DEVICE_ATTR_2(pwm3_weight_temp_sel, S_IWUSR | S_IRUGO,
2232 show_pwm_temp_sel, store_pwm_temp_sel, 2, 1);
2233 static SENSOR_DEVICE_ATTR_2(pwm4_weight_temp_sel, S_IWUSR | S_IRUGO,
2234 show_pwm_temp_sel, store_pwm_temp_sel, 3, 1);
2235 static SENSOR_DEVICE_ATTR_2(pwm5_weight_temp_sel, S_IWUSR | S_IRUGO,
2236 show_pwm_temp_sel, store_pwm_temp_sel, 4, 1);
2238 static SENSOR_DEVICE_ATTR_2(pwm1_weight_temp_step, S_IWUSR | S_IRUGO,
2239 show_weight_temp, store_weight_temp, 0, 0);
2240 static SENSOR_DEVICE_ATTR_2(pwm2_weight_temp_step, S_IWUSR | S_IRUGO,
2241 show_weight_temp, store_weight_temp, 1, 0);
2242 static SENSOR_DEVICE_ATTR_2(pwm3_weight_temp_step, S_IWUSR | S_IRUGO,
2243 show_weight_temp, store_weight_temp, 2, 0);
2244 static SENSOR_DEVICE_ATTR_2(pwm4_weight_temp_step, S_IWUSR | S_IRUGO,
2245 show_weight_temp, store_weight_temp, 3, 0);
2246 static SENSOR_DEVICE_ATTR_2(pwm5_weight_temp_step, S_IWUSR | S_IRUGO,
2247 show_weight_temp, store_weight_temp, 4, 0);
2249 static SENSOR_DEVICE_ATTR_2(pwm1_weight_temp_step_tol, S_IWUSR | S_IRUGO,
2250 show_weight_temp, store_weight_temp, 0, 1);
2251 static SENSOR_DEVICE_ATTR_2(pwm2_weight_temp_step_tol, S_IWUSR | S_IRUGO,
2252 show_weight_temp, store_weight_temp, 1, 1);
2253 static SENSOR_DEVICE_ATTR_2(pwm3_weight_temp_step_tol, S_IWUSR | S_IRUGO,
2254 show_weight_temp, store_weight_temp, 2, 1);
2255 static SENSOR_DEVICE_ATTR_2(pwm4_weight_temp_step_tol, S_IWUSR | S_IRUGO,
2256 show_weight_temp, store_weight_temp, 3, 1);
2257 static SENSOR_DEVICE_ATTR_2(pwm5_weight_temp_step_tol, S_IWUSR | S_IRUGO,
2258 show_weight_temp, store_weight_temp, 4, 1);
2260 static SENSOR_DEVICE_ATTR_2(pwm1_weight_temp_step_base, S_IWUSR | S_IRUGO,
2261 show_weight_temp, store_weight_temp, 0, 2);
2262 static SENSOR_DEVICE_ATTR_2(pwm2_weight_temp_step_base, S_IWUSR | S_IRUGO,
2263 show_weight_temp, store_weight_temp, 1, 2);
2264 static SENSOR_DEVICE_ATTR_2(pwm3_weight_temp_step_base, S_IWUSR | S_IRUGO,
2265 show_weight_temp, store_weight_temp, 2, 2);
2266 static SENSOR_DEVICE_ATTR_2(pwm4_weight_temp_step_base, S_IWUSR | S_IRUGO,
2267 show_weight_temp, store_weight_temp, 3, 2);
2268 static SENSOR_DEVICE_ATTR_2(pwm5_weight_temp_step_base, S_IWUSR | S_IRUGO,
2269 show_weight_temp, store_weight_temp, 4, 2);
2271 static SENSOR_DEVICE_ATTR_2(pwm1_weight_duty_step, S_IWUSR | S_IRUGO,
2272 show_pwm, store_pwm, 0, 5);
2273 static SENSOR_DEVICE_ATTR_2(pwm2_weight_duty_step, S_IWUSR | S_IRUGO,
2274 show_pwm, store_pwm, 1, 5);
2275 static SENSOR_DEVICE_ATTR_2(pwm3_weight_duty_step, S_IWUSR | S_IRUGO,
2276 show_pwm, store_pwm, 2, 5);
2277 static SENSOR_DEVICE_ATTR_2(pwm4_weight_duty_step, S_IWUSR | S_IRUGO,
2278 show_pwm, store_pwm, 3, 5);
2279 static SENSOR_DEVICE_ATTR_2(pwm5_weight_duty_step, S_IWUSR | S_IRUGO,
2280 show_pwm, store_pwm, 4, 5);
2282 /* duty_base is not supported on all chips */
2283 static struct sensor_device_attribute_2 sda_weight_duty_base[] = {
2284 SENSOR_ATTR_2(pwm1_weight_duty_base, S_IWUSR | S_IRUGO,
2285 show_pwm, store_pwm, 0, 6),
2286 SENSOR_ATTR_2(pwm2_weight_duty_base, S_IWUSR | S_IRUGO,
2287 show_pwm, store_pwm, 1, 6),
2288 SENSOR_ATTR_2(pwm3_weight_duty_base, S_IWUSR | S_IRUGO,
2289 show_pwm, store_pwm, 2, 6),
2290 SENSOR_ATTR_2(pwm4_weight_duty_base, S_IWUSR | S_IRUGO,
2291 show_pwm, store_pwm, 3, 6),
2292 SENSOR_ATTR_2(pwm5_weight_duty_base, S_IWUSR | S_IRUGO,
2293 show_pwm, store_pwm, 4, 6),
2297 show_fan_time(struct device *dev, struct device_attribute *attr, char *buf)
2299 struct nct6775_data *data = nct6775_update_device(dev);
2300 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
2302 int index = sattr->index;
2304 return sprintf(buf, "%d\n",
2305 step_time_from_reg(data->fan_time[index][nr],
2306 data->pwm_mode[nr]));
2310 store_fan_time(struct device *dev, struct device_attribute *attr,
2311 const char *buf, size_t count)
2313 struct nct6775_data *data = dev_get_drvdata(dev);
2314 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
2316 int index = sattr->index;
2320 err = kstrtoul(buf, 10, &val);
2324 val = step_time_to_reg(val, data->pwm_mode[nr]);
2325 mutex_lock(&data->update_lock);
2326 data->fan_time[index][nr] = val;
2327 nct6775_write_value(data, data->REG_FAN_TIME[index][nr], val);
2328 mutex_unlock(&data->update_lock);
2333 show_name(struct device *dev, struct device_attribute *attr, char *buf)
2335 struct nct6775_data *data = dev_get_drvdata(dev);
2337 return sprintf(buf, "%s\n", data->name);
2340 static DEVICE_ATTR(name, S_IRUGO, show_name, NULL);
2342 static SENSOR_DEVICE_ATTR_2(pwm1_stop_time, S_IWUSR | S_IRUGO, show_fan_time,
2343 store_fan_time, 0, 0);
2344 static SENSOR_DEVICE_ATTR_2(pwm2_stop_time, S_IWUSR | S_IRUGO, show_fan_time,
2345 store_fan_time, 1, 0);
2346 static SENSOR_DEVICE_ATTR_2(pwm3_stop_time, S_IWUSR | S_IRUGO, show_fan_time,
2347 store_fan_time, 2, 0);
2348 static SENSOR_DEVICE_ATTR_2(pwm4_stop_time, S_IWUSR | S_IRUGO, show_fan_time,
2349 store_fan_time, 3, 0);
2350 static SENSOR_DEVICE_ATTR_2(pwm5_stop_time, S_IWUSR | S_IRUGO, show_fan_time,
2351 store_fan_time, 4, 0);
2353 static SENSOR_DEVICE_ATTR_2(pwm1_step_up_time, S_IWUSR | S_IRUGO, show_fan_time,
2354 store_fan_time, 0, 1);
2355 static SENSOR_DEVICE_ATTR_2(pwm2_step_up_time, S_IWUSR | S_IRUGO, show_fan_time,
2356 store_fan_time, 1, 1);
2357 static SENSOR_DEVICE_ATTR_2(pwm3_step_up_time, S_IWUSR | S_IRUGO, show_fan_time,
2358 store_fan_time, 2, 1);
2359 static SENSOR_DEVICE_ATTR_2(pwm4_step_up_time, S_IWUSR | S_IRUGO, show_fan_time,
2360 store_fan_time, 3, 1);
2361 static SENSOR_DEVICE_ATTR_2(pwm5_step_up_time, S_IWUSR | S_IRUGO, show_fan_time,
2362 store_fan_time, 4, 1);
2364 static SENSOR_DEVICE_ATTR_2(pwm1_step_down_time, S_IWUSR | S_IRUGO,
2365 show_fan_time, store_fan_time, 0, 2);
2366 static SENSOR_DEVICE_ATTR_2(pwm2_step_down_time, S_IWUSR | S_IRUGO,
2367 show_fan_time, store_fan_time, 1, 2);
2368 static SENSOR_DEVICE_ATTR_2(pwm3_step_down_time, S_IWUSR | S_IRUGO,
2369 show_fan_time, store_fan_time, 2, 2);
2370 static SENSOR_DEVICE_ATTR_2(pwm4_step_down_time, S_IWUSR | S_IRUGO,
2371 show_fan_time, store_fan_time, 3, 2);
2372 static SENSOR_DEVICE_ATTR_2(pwm5_step_down_time, S_IWUSR | S_IRUGO,
2373 show_fan_time, store_fan_time, 4, 2);
2375 static SENSOR_DEVICE_ATTR_2(pwm1_start_output, S_IWUSR | S_IRUGO, show_pwm,
2377 static SENSOR_DEVICE_ATTR_2(pwm2_start_output, S_IWUSR | S_IRUGO, show_pwm,
2379 static SENSOR_DEVICE_ATTR_2(pwm3_start_output, S_IWUSR | S_IRUGO, show_pwm,
2381 static SENSOR_DEVICE_ATTR_2(pwm4_start_output, S_IWUSR | S_IRUGO, show_pwm,
2383 static SENSOR_DEVICE_ATTR_2(pwm5_start_output, S_IWUSR | S_IRUGO, show_pwm,
2386 static SENSOR_DEVICE_ATTR_2(pwm1_stop_output, S_IWUSR | S_IRUGO, show_pwm,
2388 static SENSOR_DEVICE_ATTR_2(pwm2_stop_output, S_IWUSR | S_IRUGO, show_pwm,
2390 static SENSOR_DEVICE_ATTR_2(pwm3_stop_output, S_IWUSR | S_IRUGO, show_pwm,
2392 static SENSOR_DEVICE_ATTR_2(pwm4_stop_output, S_IWUSR | S_IRUGO, show_pwm,
2394 static SENSOR_DEVICE_ATTR_2(pwm5_stop_output, S_IWUSR | S_IRUGO, show_pwm,
2397 /* max_output is not supported on all chips */
2398 static struct sensor_device_attribute_2 sda_max_output[] = {
2399 SENSOR_ATTR_2(pwm1_max_output, S_IWUSR | S_IRUGO, show_pwm, store_pwm,
2401 SENSOR_ATTR_2(pwm2_max_output, S_IWUSR | S_IRUGO, show_pwm, store_pwm,
2403 SENSOR_ATTR_2(pwm3_max_output, S_IWUSR | S_IRUGO, show_pwm, store_pwm,
2405 SENSOR_ATTR_2(pwm4_max_output, S_IWUSR | S_IRUGO, show_pwm, store_pwm,
2407 SENSOR_ATTR_2(pwm5_max_output, S_IWUSR | S_IRUGO, show_pwm, store_pwm,
2411 /* step_output is not supported on all chips */
2412 static struct sensor_device_attribute_2 sda_step_output[] = {
2413 SENSOR_ATTR_2(pwm1_step_output, S_IWUSR | S_IRUGO, show_pwm, store_pwm,
2415 SENSOR_ATTR_2(pwm2_step_output, S_IWUSR | S_IRUGO, show_pwm, store_pwm,
2417 SENSOR_ATTR_2(pwm3_step_output, S_IWUSR | S_IRUGO, show_pwm, store_pwm,
2419 SENSOR_ATTR_2(pwm4_step_output, S_IWUSR | S_IRUGO, show_pwm, store_pwm,
2421 SENSOR_ATTR_2(pwm5_step_output, S_IWUSR | S_IRUGO, show_pwm, store_pwm,
2425 static struct attribute *nct6775_attributes_pwm[5][20] = {
2427 &sensor_dev_attr_pwm1.dev_attr.attr,
2428 &sensor_dev_attr_pwm1_mode.dev_attr.attr,
2429 &sensor_dev_attr_pwm1_enable.dev_attr.attr,
2430 &sensor_dev_attr_pwm1_stop_output_enable.dev_attr.attr,
2431 &sensor_dev_attr_pwm1_weight_enable.dev_attr.attr,
2432 &sensor_dev_attr_pwm1_step_enable.dev_attr.attr,
2433 &sensor_dev_attr_pwm1_temp_sel.dev_attr.attr,
2434 &sensor_dev_attr_pwm1_target.dev_attr.attr,
2435 &sensor_dev_attr_pwm1_stop_time.dev_attr.attr,
2436 &sensor_dev_attr_pwm1_step_up_time.dev_attr.attr,
2437 &sensor_dev_attr_pwm1_step_down_time.dev_attr.attr,
2438 &sensor_dev_attr_pwm1_start_output.dev_attr.attr,
2439 &sensor_dev_attr_pwm1_stop_output.dev_attr.attr,
2440 &sensor_dev_attr_pwm1_weight_temp_sel.dev_attr.attr,
2441 &sensor_dev_attr_pwm1_weight_temp_step.dev_attr.attr,
2442 &sensor_dev_attr_pwm1_weight_temp_step_tol.dev_attr.attr,
2443 &sensor_dev_attr_pwm1_weight_temp_step_base.dev_attr.attr,
2444 &sensor_dev_attr_pwm1_weight_duty_step.dev_attr.attr,
2448 &sensor_dev_attr_pwm2.dev_attr.attr,
2449 &sensor_dev_attr_pwm2_mode.dev_attr.attr,
2450 &sensor_dev_attr_pwm2_enable.dev_attr.attr,
2451 &sensor_dev_attr_pwm2_stop_output_enable.dev_attr.attr,
2452 &sensor_dev_attr_pwm2_weight_enable.dev_attr.attr,
2453 &sensor_dev_attr_pwm2_step_enable.dev_attr.attr,
2454 &sensor_dev_attr_pwm2_temp_sel.dev_attr.attr,
2455 &sensor_dev_attr_pwm2_target.dev_attr.attr,
2456 &sensor_dev_attr_pwm2_stop_time.dev_attr.attr,
2457 &sensor_dev_attr_pwm2_step_up_time.dev_attr.attr,
2458 &sensor_dev_attr_pwm2_step_down_time.dev_attr.attr,
2459 &sensor_dev_attr_pwm2_start_output.dev_attr.attr,
2460 &sensor_dev_attr_pwm2_stop_output.dev_attr.attr,
2461 &sensor_dev_attr_pwm2_weight_temp_sel.dev_attr.attr,
2462 &sensor_dev_attr_pwm2_weight_temp_step.dev_attr.attr,
2463 &sensor_dev_attr_pwm2_weight_temp_step_tol.dev_attr.attr,
2464 &sensor_dev_attr_pwm2_weight_temp_step_base.dev_attr.attr,
2465 &sensor_dev_attr_pwm2_weight_duty_step.dev_attr.attr,
2469 &sensor_dev_attr_pwm3.dev_attr.attr,
2470 &sensor_dev_attr_pwm3_mode.dev_attr.attr,
2471 &sensor_dev_attr_pwm3_enable.dev_attr.attr,
2472 &sensor_dev_attr_pwm3_stop_output_enable.dev_attr.attr,
2473 &sensor_dev_attr_pwm3_weight_enable.dev_attr.attr,
2474 &sensor_dev_attr_pwm3_step_enable.dev_attr.attr,
2475 &sensor_dev_attr_pwm3_temp_sel.dev_attr.attr,
2476 &sensor_dev_attr_pwm3_target.dev_attr.attr,
2477 &sensor_dev_attr_pwm3_stop_time.dev_attr.attr,
2478 &sensor_dev_attr_pwm3_step_up_time.dev_attr.attr,
2479 &sensor_dev_attr_pwm3_step_down_time.dev_attr.attr,
2480 &sensor_dev_attr_pwm3_start_output.dev_attr.attr,
2481 &sensor_dev_attr_pwm3_stop_output.dev_attr.attr,
2482 &sensor_dev_attr_pwm3_weight_temp_sel.dev_attr.attr,
2483 &sensor_dev_attr_pwm3_weight_temp_step.dev_attr.attr,
2484 &sensor_dev_attr_pwm3_weight_temp_step_tol.dev_attr.attr,
2485 &sensor_dev_attr_pwm3_weight_temp_step_base.dev_attr.attr,
2486 &sensor_dev_attr_pwm3_weight_duty_step.dev_attr.attr,
2490 &sensor_dev_attr_pwm4.dev_attr.attr,
2491 &sensor_dev_attr_pwm4_mode.dev_attr.attr,
2492 &sensor_dev_attr_pwm4_enable.dev_attr.attr,
2493 &sensor_dev_attr_pwm4_stop_output_enable.dev_attr.attr,
2494 &sensor_dev_attr_pwm4_weight_enable.dev_attr.attr,
2495 &sensor_dev_attr_pwm4_step_enable.dev_attr.attr,
2496 &sensor_dev_attr_pwm4_temp_sel.dev_attr.attr,
2497 &sensor_dev_attr_pwm4_target.dev_attr.attr,
2498 &sensor_dev_attr_pwm4_stop_time.dev_attr.attr,
2499 &sensor_dev_attr_pwm4_step_up_time.dev_attr.attr,
2500 &sensor_dev_attr_pwm4_step_down_time.dev_attr.attr,
2501 &sensor_dev_attr_pwm4_start_output.dev_attr.attr,
2502 &sensor_dev_attr_pwm4_stop_output.dev_attr.attr,
2503 &sensor_dev_attr_pwm4_weight_temp_sel.dev_attr.attr,
2504 &sensor_dev_attr_pwm4_weight_temp_step.dev_attr.attr,
2505 &sensor_dev_attr_pwm4_weight_temp_step_tol.dev_attr.attr,
2506 &sensor_dev_attr_pwm4_weight_temp_step_base.dev_attr.attr,
2507 &sensor_dev_attr_pwm4_weight_duty_step.dev_attr.attr,
2511 &sensor_dev_attr_pwm5.dev_attr.attr,
2512 &sensor_dev_attr_pwm5_mode.dev_attr.attr,
2513 &sensor_dev_attr_pwm5_enable.dev_attr.attr,
2514 &sensor_dev_attr_pwm5_stop_output_enable.dev_attr.attr,
2515 &sensor_dev_attr_pwm5_weight_enable.dev_attr.attr,
2516 &sensor_dev_attr_pwm5_step_enable.dev_attr.attr,
2517 &sensor_dev_attr_pwm5_temp_sel.dev_attr.attr,
2518 &sensor_dev_attr_pwm5_target.dev_attr.attr,
2519 &sensor_dev_attr_pwm5_stop_time.dev_attr.attr,
2520 &sensor_dev_attr_pwm5_step_up_time.dev_attr.attr,
2521 &sensor_dev_attr_pwm5_step_down_time.dev_attr.attr,
2522 &sensor_dev_attr_pwm5_start_output.dev_attr.attr,
2523 &sensor_dev_attr_pwm5_stop_output.dev_attr.attr,
2524 &sensor_dev_attr_pwm5_weight_temp_sel.dev_attr.attr,
2525 &sensor_dev_attr_pwm5_weight_temp_step.dev_attr.attr,
2526 &sensor_dev_attr_pwm5_weight_temp_step_tol.dev_attr.attr,
2527 &sensor_dev_attr_pwm5_weight_temp_step_base.dev_attr.attr,
2528 &sensor_dev_attr_pwm5_weight_duty_step.dev_attr.attr,
2533 static const struct attribute_group nct6775_group_pwm[5] = {
2534 { .attrs = nct6775_attributes_pwm[0] },
2535 { .attrs = nct6775_attributes_pwm[1] },
2536 { .attrs = nct6775_attributes_pwm[2] },
2537 { .attrs = nct6775_attributes_pwm[3] },
2538 { .attrs = nct6775_attributes_pwm[4] },
2542 show_auto_pwm(struct device *dev, struct device_attribute *attr, char *buf)
2544 struct nct6775_data *data = nct6775_update_device(dev);
2545 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
2547 return sprintf(buf, "%d\n", data->auto_pwm[sattr->nr][sattr->index]);
2551 store_auto_pwm(struct device *dev, struct device_attribute *attr,
2552 const char *buf, size_t count)
2554 struct nct6775_data *data = dev_get_drvdata(dev);
2555 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
2557 int point = sattr->index;
2562 err = kstrtoul(buf, 10, &val);
2568 if (point == data->auto_pwm_num) {
2569 if (data->kind != nct6775 && !val)
2571 if (data->kind != nct6779 && val)
2575 mutex_lock(&data->update_lock);
2576 data->auto_pwm[nr][point] = val;
2577 if (point < data->auto_pwm_num) {
2578 nct6775_write_value(data,
2579 NCT6775_AUTO_PWM(data, nr, point),
2580 data->auto_pwm[nr][point]);
2582 switch (data->kind) {
2584 /* disable if needed (pwm == 0) */
2585 reg = nct6775_read_value(data,
2586 NCT6775_REG_CRITICAL_ENAB[nr]);
2591 nct6775_write_value(data, NCT6775_REG_CRITICAL_ENAB[nr],
2595 break; /* always enabled, nothing to do */
2597 nct6775_write_value(data, NCT6779_REG_CRITICAL_PWM[nr],
2599 reg = nct6775_read_value(data,
2600 NCT6779_REG_CRITICAL_PWM_ENABLE[nr]);
2605 nct6775_write_value(data,
2606 NCT6779_REG_CRITICAL_PWM_ENABLE[nr],
2611 mutex_unlock(&data->update_lock);
2616 show_auto_temp(struct device *dev, struct device_attribute *attr, char *buf)
2618 struct nct6775_data *data = nct6775_update_device(dev);
2619 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
2621 int point = sattr->index;
2624 * We don't know for sure if the temperature is signed or unsigned.
2625 * Assume it is unsigned.
2627 return sprintf(buf, "%d\n", data->auto_temp[nr][point] * 1000);
2631 store_auto_temp(struct device *dev, struct device_attribute *attr,
2632 const char *buf, size_t count)
2634 struct nct6775_data *data = dev_get_drvdata(dev);
2635 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
2637 int point = sattr->index;
2641 err = kstrtoul(buf, 10, &val);
2647 mutex_lock(&data->update_lock);
2648 data->auto_temp[nr][point] = DIV_ROUND_CLOSEST(val, 1000);
2649 if (point < data->auto_pwm_num) {
2650 nct6775_write_value(data,
2651 NCT6775_AUTO_TEMP(data, nr, point),
2652 data->auto_temp[nr][point]);
2654 nct6775_write_value(data, data->REG_CRITICAL_TEMP[nr],
2655 data->auto_temp[nr][point]);
2657 mutex_unlock(&data->update_lock);
2662 * The number of auto-point trip points is chip dependent.
2663 * Need to check support while generating/removing attribute files.
2665 static struct sensor_device_attribute_2 sda_auto_pwm_arrays[] = {
2666 SENSOR_ATTR_2(pwm1_auto_point1_pwm, S_IWUSR | S_IRUGO,
2667 show_auto_pwm, store_auto_pwm, 0, 0),
2668 SENSOR_ATTR_2(pwm1_auto_point1_temp, S_IWUSR | S_IRUGO,
2669 show_auto_temp, store_auto_temp, 0, 0),
2670 SENSOR_ATTR_2(pwm1_auto_point1_temp_hyst, S_IWUSR | S_IRUGO,
2671 show_auto_temp_hyst, store_auto_temp_hyst, 0, 0),
2672 SENSOR_ATTR_2(pwm1_auto_point2_pwm, S_IWUSR | S_IRUGO,
2673 show_auto_pwm, store_auto_pwm, 0, 1),
2674 SENSOR_ATTR_2(pwm1_auto_point2_temp, S_IWUSR | S_IRUGO,
2675 show_auto_temp, store_auto_temp, 0, 1),
2676 SENSOR_ATTR_2(pwm1_auto_point2_temp_hyst, S_IWUSR | S_IRUGO,
2677 show_auto_temp_hyst, store_auto_temp_hyst, 0, 1),
2678 SENSOR_ATTR_2(pwm1_auto_point3_pwm, S_IWUSR | S_IRUGO,
2679 show_auto_pwm, store_auto_pwm, 0, 2),
2680 SENSOR_ATTR_2(pwm1_auto_point3_temp, S_IWUSR | S_IRUGO,
2681 show_auto_temp, store_auto_temp, 0, 2),
2682 SENSOR_ATTR_2(pwm1_auto_point3_temp_hyst, S_IWUSR | S_IRUGO,
2683 show_auto_temp_hyst, store_auto_temp_hyst, 0, 2),
2684 SENSOR_ATTR_2(pwm1_auto_point4_pwm, S_IWUSR | S_IRUGO,
2685 show_auto_pwm, store_auto_pwm, 0, 3),
2686 SENSOR_ATTR_2(pwm1_auto_point4_temp, S_IWUSR | S_IRUGO,
2687 show_auto_temp, store_auto_temp, 0, 3),
2688 SENSOR_ATTR_2(pwm1_auto_point4_temp_hyst, S_IWUSR | S_IRUGO,
2689 show_auto_temp_hyst, store_auto_temp_hyst, 0, 3),
2690 SENSOR_ATTR_2(pwm1_auto_point5_pwm, S_IWUSR | S_IRUGO,
2691 show_auto_pwm, store_auto_pwm, 0, 4),
2692 SENSOR_ATTR_2(pwm1_auto_point5_temp, S_IWUSR | S_IRUGO,
2693 show_auto_temp, store_auto_temp, 0, 4),
2694 SENSOR_ATTR_2(pwm1_auto_point5_temp_hyst, S_IWUSR | S_IRUGO,
2695 show_auto_temp_hyst, store_auto_temp_hyst, 0, 4),
2696 SENSOR_ATTR_2(pwm1_auto_point6_pwm, S_IWUSR | S_IRUGO,
2697 show_auto_pwm, store_auto_pwm, 0, 5),
2698 SENSOR_ATTR_2(pwm1_auto_point6_temp, S_IWUSR | S_IRUGO,
2699 show_auto_temp, store_auto_temp, 0, 5),
2700 SENSOR_ATTR_2(pwm1_auto_point6_temp_hyst, S_IWUSR | S_IRUGO,
2701 show_auto_temp_hyst, store_auto_temp_hyst, 0, 5),
2702 SENSOR_ATTR_2(pwm1_auto_point7_pwm, S_IWUSR | S_IRUGO,
2703 show_auto_pwm, store_auto_pwm, 0, 6),
2704 SENSOR_ATTR_2(pwm1_auto_point7_temp, S_IWUSR | S_IRUGO,
2705 show_auto_temp, store_auto_temp, 0, 6),
2706 SENSOR_ATTR_2(pwm1_auto_point7_temp_hyst, S_IWUSR | S_IRUGO,
2707 show_auto_temp_hyst, store_auto_temp_hyst, 0, 6),
2709 SENSOR_ATTR_2(pwm2_auto_point1_pwm, S_IWUSR | S_IRUGO,
2710 show_auto_pwm, store_auto_pwm, 1, 0),
2711 SENSOR_ATTR_2(pwm2_auto_point1_temp, S_IWUSR | S_IRUGO,
2712 show_auto_temp, store_auto_temp, 1, 0),
2713 SENSOR_ATTR_2(pwm2_auto_point1_temp_hyst, S_IWUSR | S_IRUGO,
2714 show_auto_temp_hyst, store_auto_temp_hyst, 1, 0),
2715 SENSOR_ATTR_2(pwm2_auto_point2_pwm, S_IWUSR | S_IRUGO,
2716 show_auto_pwm, store_auto_pwm, 1, 1),
2717 SENSOR_ATTR_2(pwm2_auto_point2_temp, S_IWUSR | S_IRUGO,
2718 show_auto_temp, store_auto_temp, 1, 1),
2719 SENSOR_ATTR_2(pwm2_auto_point2_temp_hyst, S_IWUSR | S_IRUGO,
2720 show_auto_temp_hyst, store_auto_temp_hyst, 1, 1),
2721 SENSOR_ATTR_2(pwm2_auto_point3_pwm, S_IWUSR | S_IRUGO,
2722 show_auto_pwm, store_auto_pwm, 1, 2),
2723 SENSOR_ATTR_2(pwm2_auto_point3_temp, S_IWUSR | S_IRUGO,
2724 show_auto_temp, store_auto_temp, 1, 2),
2725 SENSOR_ATTR_2(pwm2_auto_point3_temp_hyst, S_IWUSR | S_IRUGO,
2726 show_auto_temp_hyst, store_auto_temp_hyst, 1, 2),
2727 SENSOR_ATTR_2(pwm2_auto_point4_pwm, S_IWUSR | S_IRUGO,
2728 show_auto_pwm, store_auto_pwm, 1, 3),
2729 SENSOR_ATTR_2(pwm2_auto_point4_temp, S_IWUSR | S_IRUGO,
2730 show_auto_temp, store_auto_temp, 1, 3),
2731 SENSOR_ATTR_2(pwm2_auto_point4_temp_hyst, S_IWUSR | S_IRUGO,
2732 show_auto_temp_hyst, store_auto_temp_hyst, 1, 3),
2733 SENSOR_ATTR_2(pwm2_auto_point5_pwm, S_IWUSR | S_IRUGO,
2734 show_auto_pwm, store_auto_pwm, 1, 4),
2735 SENSOR_ATTR_2(pwm2_auto_point5_temp, S_IWUSR | S_IRUGO,
2736 show_auto_temp, store_auto_temp, 1, 4),
2737 SENSOR_ATTR_2(pwm2_auto_point5_temp_hyst, S_IWUSR | S_IRUGO,
2738 show_auto_temp_hyst, store_auto_temp_hyst, 1, 4),
2739 SENSOR_ATTR_2(pwm2_auto_point6_pwm, S_IWUSR | S_IRUGO,
2740 show_auto_pwm, store_auto_pwm, 1, 5),
2741 SENSOR_ATTR_2(pwm2_auto_point6_temp, S_IWUSR | S_IRUGO,
2742 show_auto_temp, store_auto_temp, 1, 5),
2743 SENSOR_ATTR_2(pwm2_auto_point6_temp_hyst, S_IWUSR | S_IRUGO,
2744 show_auto_temp_hyst, store_auto_temp_hyst, 1, 5),
2745 SENSOR_ATTR_2(pwm2_auto_point7_pwm, S_IWUSR | S_IRUGO,
2746 show_auto_pwm, store_auto_pwm, 1, 6),
2747 SENSOR_ATTR_2(pwm2_auto_point7_temp, S_IWUSR | S_IRUGO,
2748 show_auto_temp, store_auto_temp, 1, 6),
2749 SENSOR_ATTR_2(pwm2_auto_point7_temp_hyst, S_IWUSR | S_IRUGO,
2750 show_auto_temp_hyst, store_auto_temp_hyst, 1, 6),
2752 SENSOR_ATTR_2(pwm3_auto_point1_pwm, S_IWUSR | S_IRUGO,
2753 show_auto_pwm, store_auto_pwm, 2, 0),
2754 SENSOR_ATTR_2(pwm3_auto_point1_temp, S_IWUSR | S_IRUGO,
2755 show_auto_temp, store_auto_temp, 2, 0),
2756 SENSOR_ATTR_2(pwm3_auto_point1_temp_hyst, S_IWUSR | S_IRUGO,
2757 show_auto_temp_hyst, store_auto_temp_hyst, 2, 0),
2758 SENSOR_ATTR_2(pwm3_auto_point2_pwm, S_IWUSR | S_IRUGO,
2759 show_auto_pwm, store_auto_pwm, 2, 1),
2760 SENSOR_ATTR_2(pwm3_auto_point2_temp, S_IWUSR | S_IRUGO,
2761 show_auto_temp, store_auto_temp, 2, 1),
2762 SENSOR_ATTR_2(pwm3_auto_point2_temp_hyst, S_IWUSR | S_IRUGO,
2763 show_auto_temp_hyst, store_auto_temp_hyst, 2, 1),
2764 SENSOR_ATTR_2(pwm3_auto_point3_pwm, S_IWUSR | S_IRUGO,
2765 show_auto_pwm, store_auto_pwm, 2, 2),
2766 SENSOR_ATTR_2(pwm3_auto_point3_temp, S_IWUSR | S_IRUGO,
2767 show_auto_temp, store_auto_temp, 2, 2),
2768 SENSOR_ATTR_2(pwm3_auto_point3_temp_hyst, S_IWUSR | S_IRUGO,
2769 show_auto_temp_hyst, store_auto_temp_hyst, 2, 2),
2770 SENSOR_ATTR_2(pwm3_auto_point4_pwm, S_IWUSR | S_IRUGO,
2771 show_auto_pwm, store_auto_pwm, 2, 3),
2772 SENSOR_ATTR_2(pwm3_auto_point4_temp, S_IWUSR | S_IRUGO,
2773 show_auto_temp, store_auto_temp, 2, 3),
2774 SENSOR_ATTR_2(pwm3_auto_point4_temp_hyst, S_IWUSR | S_IRUGO,
2775 show_auto_temp_hyst, store_auto_temp_hyst, 2, 3),
2776 SENSOR_ATTR_2(pwm3_auto_point5_pwm, S_IWUSR | S_IRUGO,
2777 show_auto_pwm, store_auto_pwm, 2, 4),
2778 SENSOR_ATTR_2(pwm3_auto_point5_temp, S_IWUSR | S_IRUGO,
2779 show_auto_temp, store_auto_temp, 2, 4),
2780 SENSOR_ATTR_2(pwm3_auto_point5_temp_hyst, S_IWUSR | S_IRUGO,
2781 show_auto_temp_hyst, store_auto_temp_hyst, 2, 4),
2782 SENSOR_ATTR_2(pwm3_auto_point6_pwm, S_IWUSR | S_IRUGO,
2783 show_auto_pwm, store_auto_pwm, 2, 5),
2784 SENSOR_ATTR_2(pwm3_auto_point6_temp, S_IWUSR | S_IRUGO,
2785 show_auto_temp, store_auto_temp, 2, 5),
2786 SENSOR_ATTR_2(pwm3_auto_point6_temp_hyst, S_IWUSR | S_IRUGO,
2787 show_auto_temp_hyst, store_auto_temp_hyst, 2, 5),
2788 SENSOR_ATTR_2(pwm3_auto_point7_pwm, S_IWUSR | S_IRUGO,
2789 show_auto_pwm, store_auto_pwm, 2, 6),
2790 SENSOR_ATTR_2(pwm3_auto_point7_temp, S_IWUSR | S_IRUGO,
2791 show_auto_temp, store_auto_temp, 2, 6),
2792 SENSOR_ATTR_2(pwm3_auto_point7_temp_hyst, S_IWUSR | S_IRUGO,
2793 show_auto_temp_hyst, store_auto_temp_hyst, 2, 6),
2795 SENSOR_ATTR_2(pwm4_auto_point1_pwm, S_IWUSR | S_IRUGO,
2796 show_auto_pwm, store_auto_pwm, 3, 0),
2797 SENSOR_ATTR_2(pwm4_auto_point1_temp, S_IWUSR | S_IRUGO,
2798 show_auto_temp, store_auto_temp, 3, 0),
2799 SENSOR_ATTR_2(pwm4_auto_point1_temp_hyst, S_IWUSR | S_IRUGO,
2800 show_auto_temp_hyst, store_auto_temp_hyst, 3, 0),
2801 SENSOR_ATTR_2(pwm4_auto_point2_pwm, S_IWUSR | S_IRUGO,
2802 show_auto_pwm, store_auto_pwm, 3, 1),
2803 SENSOR_ATTR_2(pwm4_auto_point2_temp, S_IWUSR | S_IRUGO,
2804 show_auto_temp, store_auto_temp, 3, 1),
2805 SENSOR_ATTR_2(pwm4_auto_point2_temp_hyst, S_IWUSR | S_IRUGO,
2806 show_auto_temp_hyst, store_auto_temp_hyst, 3, 1),
2807 SENSOR_ATTR_2(pwm4_auto_point3_pwm, S_IWUSR | S_IRUGO,
2808 show_auto_pwm, store_auto_pwm, 3, 2),
2809 SENSOR_ATTR_2(pwm4_auto_point3_temp, S_IWUSR | S_IRUGO,
2810 show_auto_temp, store_auto_temp, 3, 2),
2811 SENSOR_ATTR_2(pwm4_auto_point3_temp_hyst, S_IWUSR | S_IRUGO,
2812 show_auto_temp_hyst, store_auto_temp_hyst, 3, 2),
2813 SENSOR_ATTR_2(pwm4_auto_point4_pwm, S_IWUSR | S_IRUGO,
2814 show_auto_pwm, store_auto_pwm, 3, 3),
2815 SENSOR_ATTR_2(pwm4_auto_point4_temp, S_IWUSR | S_IRUGO,
2816 show_auto_temp, store_auto_temp, 3, 3),
2817 SENSOR_ATTR_2(pwm4_auto_point4_temp_hyst, S_IWUSR | S_IRUGO,
2818 show_auto_temp_hyst, store_auto_temp_hyst, 3, 3),
2819 SENSOR_ATTR_2(pwm4_auto_point5_pwm, S_IWUSR | S_IRUGO,
2820 show_auto_pwm, store_auto_pwm, 3, 4),
2821 SENSOR_ATTR_2(pwm4_auto_point5_temp, S_IWUSR | S_IRUGO,
2822 show_auto_temp, store_auto_temp, 3, 4),
2823 SENSOR_ATTR_2(pwm4_auto_point5_temp_hyst, S_IWUSR | S_IRUGO,
2824 show_auto_temp_hyst, store_auto_temp_hyst, 3, 4),
2825 SENSOR_ATTR_2(pwm4_auto_point6_pwm, S_IWUSR | S_IRUGO,
2826 show_auto_pwm, store_auto_pwm, 3, 5),
2827 SENSOR_ATTR_2(pwm4_auto_point6_temp, S_IWUSR | S_IRUGO,
2828 show_auto_temp, store_auto_temp, 3, 5),
2829 SENSOR_ATTR_2(pwm4_auto_point6_temp_hyst, S_IWUSR | S_IRUGO,
2830 show_auto_temp_hyst, store_auto_temp_hyst, 3, 5),
2831 SENSOR_ATTR_2(pwm4_auto_point7_pwm, S_IWUSR | S_IRUGO,
2832 show_auto_pwm, store_auto_pwm, 3, 6),
2833 SENSOR_ATTR_2(pwm4_auto_point7_temp, S_IWUSR | S_IRUGO,
2834 show_auto_temp, store_auto_temp, 3, 6),
2835 SENSOR_ATTR_2(pwm4_auto_point7_temp_hyst, S_IWUSR | S_IRUGO,
2836 show_auto_temp_hyst, store_auto_temp_hyst, 3, 6),
2838 SENSOR_ATTR_2(pwm5_auto_point1_pwm, S_IWUSR | S_IRUGO,
2839 show_auto_pwm, store_auto_pwm, 4, 0),
2840 SENSOR_ATTR_2(pwm5_auto_point1_temp, S_IWUSR | S_IRUGO,
2841 show_auto_temp, store_auto_temp, 4, 0),
2842 SENSOR_ATTR_2(pwm5_auto_point1_temp_hyst, S_IWUSR | S_IRUGO,
2843 show_auto_temp_hyst, store_auto_temp_hyst, 4, 0),
2844 SENSOR_ATTR_2(pwm5_auto_point2_pwm, S_IWUSR | S_IRUGO,
2845 show_auto_pwm, store_auto_pwm, 4, 1),
2846 SENSOR_ATTR_2(pwm5_auto_point2_temp, S_IWUSR | S_IRUGO,
2847 show_auto_temp, store_auto_temp, 4, 1),
2848 SENSOR_ATTR_2(pwm5_auto_point2_temp_hyst, S_IWUSR | S_IRUGO,
2849 show_auto_temp_hyst, store_auto_temp_hyst, 4, 1),
2850 SENSOR_ATTR_2(pwm5_auto_point3_pwm, S_IWUSR | S_IRUGO,
2851 show_auto_pwm, store_auto_pwm, 4, 2),
2852 SENSOR_ATTR_2(pwm5_auto_point3_temp, S_IWUSR | S_IRUGO,
2853 show_auto_temp, store_auto_temp, 4, 2),
2854 SENSOR_ATTR_2(pwm5_auto_point3_temp_hyst, S_IWUSR | S_IRUGO,
2855 show_auto_temp_hyst, store_auto_temp_hyst, 4, 2),
2856 SENSOR_ATTR_2(pwm5_auto_point4_pwm, S_IWUSR | S_IRUGO,
2857 show_auto_pwm, store_auto_pwm, 4, 3),
2858 SENSOR_ATTR_2(pwm5_auto_point4_temp, S_IWUSR | S_IRUGO,
2859 show_auto_temp, store_auto_temp, 4, 3),
2860 SENSOR_ATTR_2(pwm5_auto_point4_temp_hyst, S_IWUSR | S_IRUGO,
2861 show_auto_temp_hyst, store_auto_temp_hyst, 4, 3),
2862 SENSOR_ATTR_2(pwm5_auto_point5_pwm, S_IWUSR | S_IRUGO,
2863 show_auto_pwm, store_auto_pwm, 4, 4),
2864 SENSOR_ATTR_2(pwm5_auto_point5_temp, S_IWUSR | S_IRUGO,
2865 show_auto_temp, store_auto_temp, 4, 4),
2866 SENSOR_ATTR_2(pwm5_auto_point5_temp_hyst, S_IWUSR | S_IRUGO,
2867 show_auto_temp_hyst, store_auto_temp_hyst, 4, 4),
2868 SENSOR_ATTR_2(pwm5_auto_point6_pwm, S_IWUSR | S_IRUGO,
2869 show_auto_pwm, store_auto_pwm, 4, 5),
2870 SENSOR_ATTR_2(pwm5_auto_point6_temp, S_IWUSR | S_IRUGO,
2871 show_auto_temp, store_auto_temp, 4, 5),
2872 SENSOR_ATTR_2(pwm5_auto_point6_temp_hyst, S_IWUSR | S_IRUGO,
2873 show_auto_temp_hyst, store_auto_temp_hyst, 4, 5),
2874 SENSOR_ATTR_2(pwm5_auto_point7_pwm, S_IWUSR | S_IRUGO,
2875 show_auto_pwm, store_auto_pwm, 4, 6),
2876 SENSOR_ATTR_2(pwm5_auto_point7_temp, S_IWUSR | S_IRUGO,
2877 show_auto_temp, store_auto_temp, 4, 6),
2878 SENSOR_ATTR_2(pwm5_auto_point7_temp_hyst, S_IWUSR | S_IRUGO,
2879 show_auto_temp_hyst, store_auto_temp_hyst, 4, 6),
2883 show_vid(struct device *dev, struct device_attribute *attr, char *buf)
2885 struct nct6775_data *data = dev_get_drvdata(dev);
2886 return sprintf(buf, "%d\n", vid_from_reg(data->vid, data->vrm));
2889 static DEVICE_ATTR(cpu0_vid, S_IRUGO, show_vid, NULL);
2891 /* Case open detection */
2894 clear_caseopen(struct device *dev, struct device_attribute *attr,
2895 const char *buf, size_t count)
2897 struct nct6775_data *data = dev_get_drvdata(dev);
2898 struct nct6775_sio_data *sio_data = dev->platform_data;
2899 int nr = to_sensor_dev_attr(attr)->index - INTRUSION_ALARM_BASE;
2904 if (kstrtoul(buf, 10, &val) || val != 0)
2907 mutex_lock(&data->update_lock);
2910 * Use CR registers to clear caseopen status.
2911 * The CR registers are the same for all chips, and not all chips
2912 * support clearing the caseopen status through "regular" registers.
2914 ret = superio_enter(sio_data->sioreg);
2920 superio_select(sio_data->sioreg, NCT6775_LD_ACPI);
2921 reg = superio_inb(sio_data->sioreg, NCT6775_REG_CR_CASEOPEN_CLR[nr]);
2922 reg |= NCT6775_CR_CASEOPEN_CLR_MASK[nr];
2923 superio_outb(sio_data->sioreg, NCT6775_REG_CR_CASEOPEN_CLR[nr], reg);
2924 reg &= ~NCT6775_CR_CASEOPEN_CLR_MASK[nr];
2925 superio_outb(sio_data->sioreg, NCT6775_REG_CR_CASEOPEN_CLR[nr], reg);
2926 superio_exit(sio_data->sioreg);
2928 data->valid = 0; /* Force cache refresh */
2930 mutex_unlock(&data->update_lock);
2934 static struct sensor_device_attribute sda_caseopen[] = {
2935 SENSOR_ATTR(intrusion0_alarm, S_IWUSR | S_IRUGO, show_alarm,
2936 clear_caseopen, INTRUSION_ALARM_BASE),
2937 SENSOR_ATTR(intrusion1_alarm, S_IWUSR | S_IRUGO, show_alarm,
2938 clear_caseopen, INTRUSION_ALARM_BASE + 1),
2942 * Driver and device management
2945 static void nct6775_device_remove_files(struct device *dev)
2948 * some entries in the following arrays may not have been used in
2949 * device_create_file(), but device_remove_file() will ignore them
2952 struct nct6775_data *data = dev_get_drvdata(dev);
2954 for (i = 0; i < data->pwm_num; i++)
2955 sysfs_remove_group(&dev->kobj, &nct6775_group_pwm[i]);
2957 for (i = 0; i < ARRAY_SIZE(sda_max_output); i++)
2958 device_remove_file(dev, &sda_max_output[i].dev_attr);
2960 for (i = 0; i < ARRAY_SIZE(sda_step_output); i++)
2961 device_remove_file(dev, &sda_max_output[i].dev_attr);
2963 for (i = 0; i < ARRAY_SIZE(sda_weight_duty_base); i++)
2964 device_remove_file(dev, &sda_weight_duty_base[i].dev_attr);
2966 for (i = 0; i < ARRAY_SIZE(sda_auto_pwm_arrays); i++)
2967 device_remove_file(dev, &sda_auto_pwm_arrays[i].dev_attr);
2969 for (i = 0; i < data->in_num; i++)
2970 sysfs_remove_group(&dev->kobj, &nct6775_group_in[i]);
2972 for (i = 0; i < 5; i++) {
2973 device_remove_file(dev, &sda_fan_input[i].dev_attr);
2974 device_remove_file(dev, &sda_fan_alarm[i].dev_attr);
2975 device_remove_file(dev, &sda_fan_div[i].dev_attr);
2976 device_remove_file(dev, &sda_fan_min[i].dev_attr);
2978 for (i = 0; i < NUM_TEMP; i++) {
2979 if (!(data->have_temp & (1 << i)))
2981 device_remove_file(dev, &sda_temp_input[i].dev_attr);
2982 device_remove_file(dev, &sda_temp_label[i].dev_attr);
2983 device_remove_file(dev, &sda_temp_max[i].dev_attr);
2984 device_remove_file(dev, &sda_temp_max_hyst[i].dev_attr);
2985 device_remove_file(dev, &sda_temp_crit[i].dev_attr);
2986 if (!(data->have_temp_fixed & (1 << i)))
2988 device_remove_file(dev, &sda_temp_type[i].dev_attr);
2989 device_remove_file(dev, &sda_temp_offset[i].dev_attr);
2990 if (i >= NUM_TEMP_ALARM)
2992 device_remove_file(dev, &sda_temp_alarm[i].dev_attr);
2995 device_remove_file(dev, &sda_caseopen[0].dev_attr);
2996 device_remove_file(dev, &sda_caseopen[1].dev_attr);
2998 device_remove_file(dev, &dev_attr_name);
2999 device_remove_file(dev, &dev_attr_cpu0_vid);
3002 /* Get the monitoring functions started */
3003 static inline void __devinit nct6775_init_device(struct nct6775_data *data)
3008 /* Start monitoring if needed */
3009 if (data->REG_CONFIG) {
3010 tmp = nct6775_read_value(data, data->REG_CONFIG);
3012 nct6775_write_value(data, data->REG_CONFIG, tmp | 0x01);
3015 /* Enable temperature sensors if needed */
3016 for (i = 0; i < NUM_TEMP; i++) {
3017 if (!(data->have_temp & (1 << i)))
3019 if (!data->reg_temp_config[i])
3021 tmp = nct6775_read_value(data, data->reg_temp_config[i]);
3023 nct6775_write_value(data, data->reg_temp_config[i],
3027 /* Enable VBAT monitoring if needed */
3028 tmp = nct6775_read_value(data, data->REG_VBAT);
3030 nct6775_write_value(data, data->REG_VBAT, tmp | 0x01);
3032 diode = nct6775_read_value(data, data->REG_DIODE);
3034 for (i = 0; i < data->temp_fixed_num; i++) {
3035 if (!(data->have_temp_fixed & (1 << i)))
3037 if ((tmp & (0x02 << i))) /* diode */
3038 data->temp_type[i] = 3 - ((diode >> i) & 0x02);
3039 else /* thermistor */
3040 data->temp_type[i] = 4;
3044 static int __devinit
3045 nct6775_check_fan_inputs(const struct nct6775_sio_data *sio_data,
3046 struct nct6775_data *data)
3049 bool fan3pin, fan3min, fan4pin, fan4min, fan5pin;
3050 bool pwm3pin, pwm4pin, pwm5pin;
3053 ret = superio_enter(sio_data->sioreg);
3057 /* fan4 and fan5 share some pins with the GPIO and serial flash */
3058 if (data->kind == nct6775) {
3059 regval = superio_inb(sio_data->sioreg, 0x2c);
3061 fan3pin = regval & (1 << 6);
3063 pwm3pin = regval & (1 << 7);
3065 /* On NCT6775, fan4 shares pins with the fdc interface */
3066 fan4pin = !(superio_inb(sio_data->sioreg, 0x2A) & 0x80);
3071 } else if (data->kind == nct6776) {
3072 bool gpok = superio_inb(sio_data->sioreg, 0x27) & 0x80;
3074 superio_select(sio_data->sioreg, NCT6775_LD_HWM);
3075 regval = superio_inb(sio_data->sioreg, SIO_REG_ENABLE);
3080 fan3pin = !(superio_inb(sio_data->sioreg, 0x24) & 0x40);
3085 fan4pin = superio_inb(sio_data->sioreg, 0x1C) & 0x01;
3090 fan5pin = superio_inb(sio_data->sioreg, 0x1C) & 0x02;
3097 } else { /* NCT6779D */
3098 regval = superio_inb(sio_data->sioreg, 0x1c);
3100 fan3pin = !(regval & (1 << 5));
3101 fan4pin = !(regval & (1 << 6));
3102 fan5pin = !(regval & (1 << 7));
3104 pwm3pin = !(regval & (1 << 0));
3105 pwm4pin = !(regval & (1 << 1));
3106 pwm5pin = !(regval & (1 << 2));
3112 superio_exit(sio_data->sioreg);
3114 data->has_fan = data->has_fan_min = 0x03; /* fan1 and fan2 */
3115 data->has_fan |= fan3pin << 2;
3116 data->has_fan_min |= fan3min << 2;
3118 data->has_fan |= (fan4pin << 3) | (fan5pin << 4);
3119 data->has_fan_min |= (fan4min << 3) | (fan5pin << 4);
3121 data->has_pwm = 0x03 | (pwm3pin << 2) | (pwm4pin << 3) | (pwm5pin << 4);
3126 static int __devinit nct6775_probe(struct platform_device *pdev)
3128 struct device *dev = &pdev->dev;
3129 struct nct6775_sio_data *sio_data = dev->platform_data;
3130 struct nct6775_data *data;
3131 struct resource *res;
3133 int src, mask, available;
3134 const u16 *reg_temp, *reg_temp_over, *reg_temp_hyst, *reg_temp_config;
3135 const u16 *reg_temp_alternate, *reg_temp_crit;
3138 res = platform_get_resource(pdev, IORESOURCE_IO, 0);
3139 if (!devm_request_region(&pdev->dev, res->start, IOREGION_LENGTH,
3143 data = devm_kzalloc(&pdev->dev, sizeof(struct nct6775_data),
3148 data->kind = sio_data->kind;
3149 data->addr = res->start;
3150 mutex_init(&data->lock);
3151 mutex_init(&data->update_lock);
3152 data->name = nct6775_device_names[data->kind];
3153 platform_set_drvdata(pdev, data);
3155 switch (data->kind) {
3159 data->auto_pwm_num = 6;
3160 data->has_fan_div = true;
3161 data->temp_fixed_num = 3;
3163 data->ALARM_BITS = NCT6775_ALARM_BITS;
3165 data->fan_from_reg = fan_from_reg16;
3166 data->fan_from_reg_min = fan_from_reg8;
3168 data->temp_label = nct6775_temp_label;
3169 data->temp_label_num = ARRAY_SIZE(nct6775_temp_label);
3171 data->REG_CONFIG = NCT6775_REG_CONFIG;
3172 data->REG_VBAT = NCT6775_REG_VBAT;
3173 data->REG_DIODE = NCT6775_REG_DIODE;
3174 data->REG_VIN = NCT6775_REG_IN;
3175 data->REG_IN_MINMAX[0] = NCT6775_REG_IN_MIN;
3176 data->REG_IN_MINMAX[1] = NCT6775_REG_IN_MAX;
3177 data->REG_TARGET = NCT6775_REG_TARGET;
3178 data->REG_FAN = NCT6775_REG_FAN;
3179 data->REG_FAN_MODE = NCT6775_REG_FAN_MODE;
3180 data->REG_FAN_MIN = NCT6775_REG_FAN_MIN;
3181 data->REG_FAN_TIME[0] = NCT6775_REG_FAN_STOP_TIME;
3182 data->REG_FAN_TIME[1] = NCT6775_REG_FAN_STEP_UP_TIME;
3183 data->REG_FAN_TIME[2] = NCT6775_REG_FAN_STEP_DOWN_TIME;
3184 data->REG_PWM[0] = NCT6775_REG_PWM;
3185 data->REG_PWM[1] = NCT6775_REG_FAN_START_OUTPUT;
3186 data->REG_PWM[2] = NCT6775_REG_FAN_STOP_OUTPUT;
3187 data->REG_PWM[3] = NCT6775_REG_FAN_MAX_OUTPUT;
3188 data->REG_PWM[4] = NCT6775_REG_FAN_STEP_OUTPUT;
3189 data->REG_PWM[5] = NCT6775_REG_WEIGHT_DUTY_STEP;
3190 data->REG_PWM_READ = NCT6775_REG_PWM_READ;
3191 data->REG_PWM_MODE = NCT6775_REG_PWM_MODE;
3192 data->PWM_MODE_MASK = NCT6775_PWM_MODE_MASK;
3193 data->REG_AUTO_TEMP = NCT6775_REG_AUTO_TEMP;
3194 data->REG_AUTO_PWM = NCT6775_REG_AUTO_PWM;
3195 data->REG_CRITICAL_TEMP = NCT6775_REG_CRITICAL_TEMP;
3196 data->REG_CRITICAL_TEMP_TOLERANCE
3197 = NCT6775_REG_CRITICAL_TEMP_TOLERANCE;
3198 data->REG_TEMP_OFFSET = NCT6775_REG_TEMP_OFFSET;
3199 data->REG_TEMP_SOURCE = NCT6775_REG_TEMP_SOURCE;
3200 data->REG_TEMP_SEL[0] = NCT6775_REG_TEMP_SEL;
3201 data->REG_TEMP_SEL[1] = NCT6775_REG_WEIGHT_TEMP_SEL;
3202 data->REG_TEMP_SEL[2] = NCT6775_REG_FAN_STEP_ENABLE;
3203 data->REG_WEIGHT_TEMP[0] = NCT6775_REG_WEIGHT_TEMP_STEP;
3204 data->REG_WEIGHT_TEMP[1] = NCT6775_REG_WEIGHT_TEMP_STEP_TOL;
3205 data->REG_WEIGHT_TEMP[2] = NCT6775_REG_WEIGHT_TEMP_BASE;
3206 data->REG_ALARM = NCT6775_REG_ALARM;
3208 reg_temp = NCT6775_REG_TEMP;
3209 num_reg_temp = ARRAY_SIZE(NCT6775_REG_TEMP);
3210 reg_temp_over = NCT6775_REG_TEMP_OVER;
3211 reg_temp_hyst = NCT6775_REG_TEMP_HYST;
3212 reg_temp_config = NCT6775_REG_TEMP_CONFIG;
3213 reg_temp_alternate = NCT6775_REG_TEMP_ALTERNATE;
3214 reg_temp_crit = NCT6775_REG_TEMP_CRIT;
3220 data->auto_pwm_num = 4;
3221 data->has_fan_div = false;
3222 data->temp_fixed_num = 3;
3224 data->ALARM_BITS = NCT6776_ALARM_BITS;
3226 data->fan_from_reg = fan_from_reg13;
3227 data->fan_from_reg_min = fan_from_reg13;
3229 data->temp_label = nct6776_temp_label;
3230 data->temp_label_num = ARRAY_SIZE(nct6776_temp_label);
3232 data->REG_CONFIG = NCT6775_REG_CONFIG;
3233 data->REG_VBAT = NCT6775_REG_VBAT;
3234 data->REG_DIODE = NCT6775_REG_DIODE;
3235 data->REG_VIN = NCT6775_REG_IN;
3236 data->REG_IN_MINMAX[0] = NCT6775_REG_IN_MIN;
3237 data->REG_IN_MINMAX[1] = NCT6775_REG_IN_MAX;
3238 data->REG_TARGET = NCT6775_REG_TARGET;
3239 data->REG_FAN = NCT6775_REG_FAN;
3240 data->REG_FAN_MODE = NCT6775_REG_FAN_MODE;
3241 data->REG_FAN_MIN = NCT6776_REG_FAN_MIN;
3242 data->REG_FAN_TIME[0] = NCT6775_REG_FAN_STOP_TIME;
3243 data->REG_FAN_TIME[1] = NCT6775_REG_FAN_STEP_UP_TIME;
3244 data->REG_FAN_TIME[2] = NCT6775_REG_FAN_STEP_DOWN_TIME;
3245 data->REG_PWM[0] = NCT6775_REG_PWM;
3246 data->REG_PWM[1] = NCT6775_REG_FAN_START_OUTPUT;
3247 data->REG_PWM[2] = NCT6775_REG_FAN_STOP_OUTPUT;
3248 data->REG_PWM[5] = NCT6775_REG_WEIGHT_DUTY_STEP;
3249 data->REG_PWM[6] = NCT6776_REG_WEIGHT_DUTY_BASE;
3250 data->REG_PWM_READ = NCT6775_REG_PWM_READ;
3251 data->REG_PWM_MODE = NCT6776_REG_PWM_MODE;
3252 data->PWM_MODE_MASK = NCT6776_PWM_MODE_MASK;
3253 data->REG_AUTO_TEMP = NCT6775_REG_AUTO_TEMP;
3254 data->REG_AUTO_PWM = NCT6775_REG_AUTO_PWM;
3255 data->REG_CRITICAL_TEMP = NCT6775_REG_CRITICAL_TEMP;
3256 data->REG_CRITICAL_TEMP_TOLERANCE
3257 = NCT6775_REG_CRITICAL_TEMP_TOLERANCE;
3258 data->REG_TEMP_OFFSET = NCT6775_REG_TEMP_OFFSET;
3259 data->REG_TEMP_SOURCE = NCT6775_REG_TEMP_SOURCE;
3260 data->REG_TEMP_SEL[0] = NCT6775_REG_TEMP_SEL;
3261 data->REG_TEMP_SEL[1] = NCT6775_REG_WEIGHT_TEMP_SEL;
3262 data->REG_TEMP_SEL[2] = NCT6775_REG_FAN_STEP_ENABLE;
3263 data->REG_WEIGHT_TEMP[0] = NCT6775_REG_WEIGHT_TEMP_STEP;
3264 data->REG_WEIGHT_TEMP[1] = NCT6775_REG_WEIGHT_TEMP_STEP_TOL;
3265 data->REG_WEIGHT_TEMP[2] = NCT6775_REG_WEIGHT_TEMP_BASE;
3266 data->REG_ALARM = NCT6775_REG_ALARM;
3268 reg_temp = NCT6775_REG_TEMP;
3269 num_reg_temp = ARRAY_SIZE(NCT6775_REG_TEMP);
3270 reg_temp_over = NCT6775_REG_TEMP_OVER;
3271 reg_temp_hyst = NCT6775_REG_TEMP_HYST;
3272 reg_temp_config = NCT6776_REG_TEMP_CONFIG;
3273 reg_temp_alternate = NCT6776_REG_TEMP_ALTERNATE;
3274 reg_temp_crit = NCT6776_REG_TEMP_CRIT;
3280 data->auto_pwm_num = 4;
3281 data->has_fan_div = false;
3282 data->temp_fixed_num = 6;
3284 data->ALARM_BITS = NCT6779_ALARM_BITS;
3286 data->fan_from_reg = fan_from_reg13;
3287 data->fan_from_reg_min = fan_from_reg13;
3289 data->temp_label = nct6779_temp_label;
3290 data->temp_label_num = ARRAY_SIZE(nct6779_temp_label);
3292 data->REG_CONFIG = NCT6775_REG_CONFIG;
3293 data->REG_VBAT = NCT6775_REG_VBAT;
3294 data->REG_DIODE = NCT6775_REG_DIODE;
3295 data->REG_VIN = NCT6779_REG_IN;
3296 data->REG_IN_MINMAX[0] = NCT6775_REG_IN_MIN;
3297 data->REG_IN_MINMAX[1] = NCT6775_REG_IN_MAX;
3298 data->REG_TARGET = NCT6775_REG_TARGET;
3299 data->REG_FAN = NCT6779_REG_FAN;
3300 data->REG_FAN_MODE = NCT6775_REG_FAN_MODE;
3301 data->REG_FAN_MIN = NCT6776_REG_FAN_MIN;
3302 data->REG_FAN_TIME[0] = NCT6775_REG_FAN_STOP_TIME;
3303 data->REG_FAN_TIME[1] = NCT6775_REG_FAN_STEP_UP_TIME;
3304 data->REG_FAN_TIME[2] = NCT6775_REG_FAN_STEP_DOWN_TIME;
3305 data->REG_PWM[0] = NCT6775_REG_PWM;
3306 data->REG_PWM[1] = NCT6775_REG_FAN_START_OUTPUT;
3307 data->REG_PWM[2] = NCT6775_REG_FAN_STOP_OUTPUT;
3308 data->REG_PWM[5] = NCT6775_REG_WEIGHT_DUTY_STEP;
3309 data->REG_PWM[6] = NCT6776_REG_WEIGHT_DUTY_BASE;
3310 data->REG_PWM_READ = NCT6775_REG_PWM_READ;
3311 data->REG_PWM_MODE = NCT6776_REG_PWM_MODE;
3312 data->PWM_MODE_MASK = NCT6776_PWM_MODE_MASK;
3313 data->REG_AUTO_TEMP = NCT6775_REG_AUTO_TEMP;
3314 data->REG_AUTO_PWM = NCT6775_REG_AUTO_PWM;
3315 data->REG_CRITICAL_TEMP = NCT6775_REG_CRITICAL_TEMP;
3316 data->REG_CRITICAL_TEMP_TOLERANCE
3317 = NCT6775_REG_CRITICAL_TEMP_TOLERANCE;
3318 data->REG_TEMP_OFFSET = NCT6779_REG_TEMP_OFFSET;
3319 data->REG_TEMP_SOURCE = NCT6775_REG_TEMP_SOURCE;
3320 data->REG_TEMP_SEL[0] = NCT6775_REG_TEMP_SEL;
3321 data->REG_TEMP_SEL[1] = NCT6775_REG_WEIGHT_TEMP_SEL;
3322 data->REG_TEMP_SEL[2] = NCT6775_REG_FAN_STEP_ENABLE;
3323 data->REG_WEIGHT_TEMP[0] = NCT6775_REG_WEIGHT_TEMP_STEP;
3324 data->REG_WEIGHT_TEMP[1] = NCT6775_REG_WEIGHT_TEMP_STEP_TOL;
3325 data->REG_WEIGHT_TEMP[2] = NCT6775_REG_WEIGHT_TEMP_BASE;
3326 data->REG_ALARM = NCT6779_REG_ALARM;
3328 reg_temp = NCT6779_REG_TEMP;
3329 num_reg_temp = ARRAY_SIZE(NCT6779_REG_TEMP);
3330 reg_temp_over = NCT6779_REG_TEMP_OVER;
3331 reg_temp_hyst = NCT6779_REG_TEMP_HYST;
3332 reg_temp_config = NCT6779_REG_TEMP_CONFIG;
3333 reg_temp_alternate = NCT6779_REG_TEMP_ALTERNATE;
3334 reg_temp_crit = NCT6779_REG_TEMP_CRIT;
3340 data->have_in = (1 << data->in_num) - 1;
3341 data->have_temp = 0;
3344 * On some boards, not all available temperature sources are monitored,
3345 * even though some of the monitoring registers are unused.
3346 * Get list of unused monitoring registers, then detect if any fan
3347 * controls are configured to use unmonitored temperature sources.
3348 * If so, assign the unmonitored temperature sources to available
3349 * monitoring registers.
3353 for (i = 0; i < num_reg_temp; i++) {
3354 if (reg_temp[i] == 0)
3357 src = nct6775_read_value(data, data->REG_TEMP_SOURCE[i]) & 0x1f;
3358 if (!src || (mask & (1 << src)))
3359 available |= 1 << i;
3365 * Now find unmonitored temperature registers and enable monitoring
3366 * if additional monitoring registers are available.
3368 for (i = 0; i < ARRAY_SIZE(data->REG_TEMP_SEL) && available; i++) {
3371 if (!data->REG_TEMP_SEL[i])
3373 for (j = 0; j < data->pwm_num && available; j++) {
3376 if (!data->REG_TEMP_SEL[i][j])
3378 src = nct6775_read_value(data,
3379 data->REG_TEMP_SEL[i][j]);
3381 if (!src || (mask & (1 << src)))
3383 if (src >= data->temp_label_num ||
3384 !strlen(data->temp_label[src])) {
3386 "Select source %d:%d reg 0x%x invalid (%d)\n",
3387 i, j, data->REG_TEMP_SEL[i][j], src);
3391 index = __ffs(available);
3392 nct6775_write_value(data,
3393 data->REG_TEMP_SOURCE[index],
3395 available &= ~(1 << index);
3401 s = NUM_TEMP_FIXED; /* First dynamic temperature attribute */
3402 for (i = 0; i < num_reg_temp; i++) {
3403 if (reg_temp[i] == 0)
3406 src = nct6775_read_value(data, data->REG_TEMP_SOURCE[i]) & 0x1f;
3407 if (!src || (mask & (1 << src)))
3410 if (src >= data->temp_label_num ||
3411 !strlen(data->temp_label[src])) {
3413 "Invalid temperature source %d at index %d, source register 0x%x, temp register 0x%x\n",
3414 src, i, data->REG_TEMP_SOURCE[i], reg_temp[i]);
3420 /* Use fixed index for SYSTIN(1), CPUTIN(2), AUXTIN(3) */
3421 if (src <= data->temp_fixed_num) {
3422 data->have_temp |= 1 << (src - 1);
3423 data->have_temp_fixed |= 1 << (src - 1);
3424 data->reg_temp[0][src - 1] = reg_temp[i];
3425 data->reg_temp[1][src - 1] = reg_temp_over[i];
3426 data->reg_temp[2][src - 1] = reg_temp_hyst[i];
3427 data->reg_temp_config[src - 1] = reg_temp_config[i];
3428 data->temp_src[src - 1] = src;
3435 /* Use dynamic index for other sources */
3436 data->have_temp |= 1 << s;
3437 data->reg_temp[0][s] = reg_temp[i];
3438 data->reg_temp[1][s] = reg_temp_over[i];
3439 data->reg_temp[2][s] = reg_temp_hyst[i];
3440 data->reg_temp_config[s] = reg_temp_config[i];
3441 if (reg_temp_crit[src - 1])
3442 data->reg_temp[3][s] = reg_temp_crit[src - 1];
3444 data->temp_src[s] = src;
3450 * Go through the list of alternate temp registers and enable
3452 * The temperature is already monitored if the respective bit in <mask>
3455 for (i = 0; i < data->temp_label_num - 1; i++) {
3456 if (!reg_temp_alternate[i])
3458 if (mask & (1 << (i + 1)))
3460 if (i < data->temp_fixed_num) {
3461 if (data->have_temp & (1 << i))
3463 data->have_temp |= 1 << i;
3464 data->have_temp_fixed |= 1 << i;
3465 data->reg_temp[0][i] = reg_temp_alternate[i];
3466 data->reg_temp[1][i] = reg_temp_over[i];
3467 data->reg_temp[2][i] = reg_temp_hyst[i];
3468 data->temp_src[i] = i + 1;
3472 if (s >= NUM_TEMP) /* Abort if no more space */
3475 data->have_temp |= 1 << s;
3476 data->reg_temp[0][s] = reg_temp_alternate[i];
3477 data->temp_src[s] = i + 1;
3480 #endif /* TESTING */
3482 switch (data->kind) {
3487 * On NCT6776, AUXTIN and VIN3 pins are shared.
3488 * Only way to detect it is to check if AUXTIN is used
3489 * as a temperature source, and if that source is
3492 * If that is the case, disable in6, which reports VIN3.
3493 * Otherwise disable temp3.
3495 if (data->have_temp & (1 << 2)) {
3496 u8 reg = nct6775_read_value(data,
3497 data->reg_temp_config[2]);
3499 data->have_temp &= ~(1 << 2);
3501 data->have_in &= ~(1 << 6);
3512 * There does not seem to be a clean way to detect if VINx or
3513 * AUXTINx is active, so for keep both sensor types enabled
3519 /* Initialize the chip */
3520 nct6775_init_device(data);
3522 data->vrm = vid_which_vrm();
3523 err = superio_enter(sio_data->sioreg);
3529 * We can get the VID input values directly at logical device D 0xe3.
3531 superio_select(sio_data->sioreg, NCT6775_LD_VID);
3532 data->vid = superio_inb(sio_data->sioreg, 0xe3);
3537 superio_select(sio_data->sioreg, NCT6775_LD_HWM);
3538 tmp = superio_inb(sio_data->sioreg,
3539 NCT6775_REG_CR_FAN_DEBOUNCE);
3540 switch (data->kind) {
3549 superio_outb(sio_data->sioreg, NCT6775_REG_CR_FAN_DEBOUNCE,
3551 dev_info(&pdev->dev, "Enabled fan debounce for chip %s\n",
3555 superio_exit(sio_data->sioreg);
3557 err = device_create_file(dev, &dev_attr_cpu0_vid);
3561 err = nct6775_check_fan_inputs(sio_data, data);
3565 /* Read fan clock dividers immediately */
3566 nct6775_update_fan_div_common(dev, data);
3568 /* Register sysfs hooks */
3569 for (i = 0; i < data->pwm_num; i++) {
3570 if (!(data->has_pwm & (1 << i)))
3573 err = sysfs_create_group(&dev->kobj, &nct6775_group_pwm[i]);
3577 if (data->REG_PWM[3]) {
3578 err = device_create_file(dev,
3579 &sda_max_output[i].dev_attr);
3583 if (data->REG_PWM[4]) {
3584 err = device_create_file(dev,
3585 &sda_step_output[i].dev_attr);
3589 if (data->REG_PWM[6]) {
3590 err = device_create_file(dev,
3591 &sda_weight_duty_base[i].dev_attr);
3596 for (i = 0; i < ARRAY_SIZE(sda_auto_pwm_arrays); i++) {
3597 struct sensor_device_attribute_2 *attr =
3598 &sda_auto_pwm_arrays[i];
3600 if (!(data->has_pwm & (1 << attr->nr)))
3602 if (attr->index > data->auto_pwm_num)
3604 err = device_create_file(dev, &attr->dev_attr);
3609 for (i = 0; i < data->in_num; i++) {
3610 if (!(data->have_in & (1 << i)))
3612 err = sysfs_create_group(&dev->kobj, &nct6775_group_in[i]);
3617 for (i = 0; i < 5; i++) {
3618 if (data->has_fan & (1 << i)) {
3619 err = device_create_file(dev,
3620 &sda_fan_input[i].dev_attr);
3623 err = device_create_file(dev,
3624 &sda_fan_alarm[i].dev_attr);
3627 if (data->kind != nct6776 &&
3628 data->kind != nct6779) {
3629 err = device_create_file(dev,
3630 &sda_fan_div[i].dev_attr);
3634 if (data->has_fan_min & (1 << i)) {
3635 err = device_create_file(dev,
3636 &sda_fan_min[i].dev_attr);
3643 for (i = 0; i < NUM_TEMP; i++) {
3644 if (!(data->have_temp & (1 << i)))
3646 err = device_create_file(dev, &sda_temp_input[i].dev_attr);
3649 if (data->temp_label) {
3650 err = device_create_file(dev,
3651 &sda_temp_label[i].dev_attr);
3655 if (data->reg_temp[1][i]) {
3656 err = device_create_file(dev,
3657 &sda_temp_max[i].dev_attr);
3661 if (data->reg_temp[2][i]) {
3662 err = device_create_file(dev,
3663 &sda_temp_max_hyst[i].dev_attr);
3667 if (data->reg_temp[3][i]) {
3668 err = device_create_file(dev,
3669 &sda_temp_crit[i].dev_attr);
3673 if (!(data->have_temp_fixed & (1 << i)))
3675 err = device_create_file(dev, &sda_temp_type[i].dev_attr);
3678 err = device_create_file(dev, &sda_temp_offset[i].dev_attr);
3681 if (i >= NUM_TEMP_ALARM ||
3682 data->ALARM_BITS[TEMP_ALARM_BASE + i] < 0)
3684 err = device_create_file(dev, &sda_temp_alarm[i].dev_attr);
3689 for (i = 0; i < ARRAY_SIZE(sda_caseopen); i++) {
3690 if (data->ALARM_BITS[INTRUSION_ALARM_BASE + i] < 0)
3692 err = device_create_file(dev, &sda_caseopen[i].dev_attr);
3697 err = device_create_file(dev, &dev_attr_name);
3701 data->hwmon_dev = hwmon_device_register(dev);
3702 if (IS_ERR(data->hwmon_dev)) {
3703 err = PTR_ERR(data->hwmon_dev);
3710 nct6775_device_remove_files(dev);
3714 static int __devexit nct6775_remove(struct platform_device *pdev)
3716 struct nct6775_data *data = platform_get_drvdata(pdev);
3718 hwmon_device_unregister(data->hwmon_dev);
3719 nct6775_device_remove_files(&pdev->dev);
3724 static struct platform_driver nct6775_driver = {
3726 .owner = THIS_MODULE,
3729 .probe = nct6775_probe,
3730 .remove = __devexit_p(nct6775_remove),
3733 /* nct6775_find() looks for a '627 in the Super-I/O config space */
3734 static int __init nct6775_find(int sioaddr, unsigned short *addr,
3735 struct nct6775_sio_data *sio_data)
3737 static const char sio_name_NCT6775[] __initconst = "NCT6775F";
3738 static const char sio_name_NCT6776[] __initconst = "NCT6776F";
3739 static const char sio_name_NCT6779[] __initconst = "NCT6779D";
3742 const char *sio_name;
3745 err = superio_enter(sioaddr);
3752 val = (superio_inb(sioaddr, SIO_REG_DEVID) << 8)
3753 | superio_inb(sioaddr, SIO_REG_DEVID + 1);
3754 switch (val & SIO_ID_MASK) {
3755 case SIO_NCT6775_ID:
3756 sio_data->kind = nct6775;
3757 sio_name = sio_name_NCT6775;
3759 case SIO_NCT6776_ID:
3760 sio_data->kind = nct6776;
3761 sio_name = sio_name_NCT6776;
3763 case SIO_NCT6779_ID:
3764 sio_data->kind = nct6779;
3765 sio_name = sio_name_NCT6779;
3769 pr_debug("unsupported chip ID: 0x%04x\n", val);
3770 superio_exit(sioaddr);
3774 /* We have a known chip, find the HWM I/O address */
3775 superio_select(sioaddr, NCT6775_LD_HWM);
3776 val = (superio_inb(sioaddr, SIO_REG_ADDR) << 8)
3777 | superio_inb(sioaddr, SIO_REG_ADDR + 1);
3778 *addr = val & IOREGION_ALIGNMENT;
3780 pr_err("Refusing to enable a Super-I/O device with a base I/O port 0\n");
3781 superio_exit(sioaddr);
3785 /* Activate logical device if needed */
3786 val = superio_inb(sioaddr, SIO_REG_ENABLE);
3787 if (!(val & 0x01)) {
3788 pr_warn("Forcibly enabling Super-I/O. Sensor is probably unusable.\n");
3789 superio_outb(sioaddr, SIO_REG_ENABLE, val | 0x01);
3792 superio_exit(sioaddr);
3793 pr_info("Found %s chip at %#x\n", sio_name, *addr);
3794 sio_data->sioreg = sioaddr;
3800 * when Super-I/O functions move to a separate file, the Super-I/O
3801 * bus will manage the lifetime of the device and this module will only keep
3802 * track of the nct6775 driver. But since we platform_device_alloc(), we
3803 * must keep track of the device
3805 static struct platform_device *pdev;
3807 static int __init sensors_nct6775_init(void)
3810 unsigned short address;
3811 struct resource res;
3812 struct nct6775_sio_data sio_data;
3815 * initialize sio_data->kind and sio_data->sioreg.
3817 * when Super-I/O functions move to a separate file, the Super-I/O
3818 * driver will probe 0x2e and 0x4e and auto-detect the presence of a
3819 * nct6775 hardware monitor, and call probe()
3821 if (nct6775_find(0x2e, &address, &sio_data) &&
3822 nct6775_find(0x4e, &address, &sio_data))
3825 err = platform_driver_register(&nct6775_driver);
3829 pdev = platform_device_alloc(DRVNAME, address);
3832 pr_err("Device allocation failed\n");
3833 goto exit_unregister;
3836 err = platform_device_add_data(pdev, &sio_data,
3837 sizeof(struct nct6775_sio_data));
3839 pr_err("Platform data allocation failed\n");
3840 goto exit_device_put;
3843 memset(&res, 0, sizeof(res));
3845 res.start = address + IOREGION_OFFSET;
3846 res.end = address + IOREGION_OFFSET + IOREGION_LENGTH - 1;
3847 res.flags = IORESOURCE_IO;
3849 err = acpi_check_resource_conflict(&res);
3851 goto exit_device_put;
3853 err = platform_device_add_resources(pdev, &res, 1);
3855 pr_err("Device resource addition failed (%d)\n", err);
3856 goto exit_device_put;
3859 /* platform_device_add calls probe() */
3860 err = platform_device_add(pdev);
3862 pr_err("Device addition failed (%d)\n", err);
3863 goto exit_device_put;
3869 platform_device_put(pdev);
3871 platform_driver_unregister(&nct6775_driver);
3876 static void __exit sensors_nct6775_exit(void)
3878 platform_device_unregister(pdev);
3879 platform_driver_unregister(&nct6775_driver);
3882 MODULE_AUTHOR("Guenter Roeck <linux@roeck-us.net>");
3883 MODULE_DESCRIPTION("NCT6775F/NCT6776F/NCT6779D driver");
3884 MODULE_LICENSE("GPL");
3886 module_init(sensors_nct6775_init);
3887 module_exit(sensors_nct6775_exit);