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 /* Voltage min/max registers for nr=7..14 are in bank 5 */
167 static const u16 NCT6775_REG_IN_MAX[] = {
168 0x2b, 0x2d, 0x2f, 0x31, 0x33, 0x35, 0x37, 0x554, 0x556, 0x558, 0x55a,
169 0x55c, 0x55e, 0x560, 0x562 };
170 static const u16 NCT6775_REG_IN_MIN[] = {
171 0x2c, 0x2e, 0x30, 0x32, 0x34, 0x36, 0x38, 0x555, 0x557, 0x559, 0x55b,
172 0x55d, 0x55f, 0x561, 0x563 };
173 static const u16 NCT6775_REG_IN[] = {
174 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x550, 0x551, 0x552
177 static const u16 NCT6779_REG_IN[] = {
178 0x480, 0x481, 0x482, 0x483, 0x484, 0x485, 0x486, 0x487,
179 0x488, 0x489, 0x48a, 0x48b, 0x48c, 0x48d, 0x48e
182 #define NCT6775_REG_VBAT 0x5D
183 #define NCT6775_REG_DIODE 0x5E
185 #define NCT6775_REG_FANDIV1 0x506
186 #define NCT6775_REG_FANDIV2 0x507
188 #define NCT6775_REG_CR_FAN_DEBOUNCE 0xf0
190 static const u16 NCT6775_REG_ALARM[6] = { 0x459, 0x45A, 0x45B };
191 static const u16 NCT6779_REG_ALARM[6] = { 0x459, 0x45A, 0x45B, 0x568 };
193 /* 0..15 voltages, 16..23 fans, 24..31 temperatures */
195 static const s8 NCT6775_ALARM_BITS[]
196 = { 0, 1, 2, 3, 8, 21, 20, 16, /* in0.. in7 */
197 17, -1, -1, -1, -1, -1, -1, /* in8..in14 */
199 6, 7, 11, 10, 23, /* fan1..fan5 */
200 -1, -1, -1, /* unused */
201 4, 5, 13, -1, -1, -1, /* temp1..temp6 */
202 12, -1 }; /* intrusion0, intrusion1 */
204 static const s8 NCT6776_ALARM_BITS[]
205 = { 0, 1, 2, 3, 8, 21, 20, 16, /* in0.. in7 */
206 17, -1, -1, -1, -1, -1, -1, /* in8..in14 */
208 6, 7, 11, 10, 23, /* fan1..fan5 */
209 -1, -1, -1, /* unused */
210 4, 5, 13, -1, -1, -1, /* temp1..temp6 */
211 12, 9 }; /* intrusion0, intrusion1 */
213 static const s8 NCT6779_ALARM_BITS[]
214 = { 0, 1, 2, 3, 8, 21, 20, 16, /* in0.. in7 */
215 17, 24, 25, 26, 27, 28, 29, /* in8..in14 */
217 6, 7, 11, 10, 23, /* fan1..fan5 */
218 -1, -1, -1, /* unused */
219 4, 5, 13, -1, -1, -1, /* temp1..temp6 */
220 12, 9 }; /* intrusion0, intrusion1 */
222 #define FAN_ALARM_BASE 16
223 #define TEMP_ALARM_BASE 24
224 #define INTRUSION_ALARM_BASE 30
226 static const u8 NCT6775_REG_CR_CASEOPEN_CLR[] = { 0xe6, 0xee };
227 static const u8 NCT6775_CR_CASEOPEN_CLR_MASK[] = { 0x20, 0x01 };
229 /* DC or PWM output fan configuration */
230 static const u8 NCT6775_REG_PWM_MODE[] = { 0x04, 0x04, 0x12 };
231 static const u8 NCT6775_PWM_MODE_MASK[] = { 0x01, 0x02, 0x01 };
233 static const u8 NCT6776_REG_PWM_MODE[] = { 0x04, 0, 0 };
234 static const u8 NCT6776_PWM_MODE_MASK[] = { 0x01, 0, 0 };
236 /* Advanced Fan control, some values are common for all fans */
238 static const u16 NCT6775_REG_FAN_PULSES[] = { 0x641, 0x642, 0x643, 0x644, 0 };
239 static const u16 NCT6776_REG_FAN_PULSES[] = { 0x644, 0x645, 0x646, 0, 0 };
240 static const u16 NCT6779_REG_FAN_PULSES[]
241 = { 0x644, 0x645, 0x646, 0x647, 0x648 };
243 static const u16 NCT6775_REG_TARGET[] = { 0x101, 0x201, 0x301, 0x801, 0x901 };
244 static const u16 NCT6775_REG_FAN_MODE[] = { 0x102, 0x202, 0x302, 0x802, 0x902 };
245 static const u16 NCT6775_REG_FAN_STEP_DOWN_TIME[] = {
246 0x103, 0x203, 0x303, 0x803, 0x903 };
247 static const u16 NCT6775_REG_FAN_STEP_UP_TIME[] = {
248 0x104, 0x204, 0x304, 0x804, 0x904 };
249 static const u16 NCT6775_REG_FAN_STOP_OUTPUT[] = {
250 0x105, 0x205, 0x305, 0x805, 0x905 };
251 static const u16 NCT6775_REG_FAN_START_OUTPUT[]
252 = { 0x106, 0x206, 0x306, 0x806, 0x906 };
253 static const u16 NCT6775_REG_FAN_STEP_ENABLE[] = {
254 0x120, 0x220, 0x320, 0x820, 0x920 };
255 static const u16 NCT6775_REG_FAN_MAX_OUTPUT[] = { 0x10a, 0x20a, 0x30a };
256 static const u16 NCT6775_REG_FAN_STEP_OUTPUT[] = { 0x10b, 0x20b, 0x30b };
258 static const u16 NCT6775_REG_FAN_STOP_TIME[]
259 = { 0x107, 0x207, 0x307, 0x807, 0x907 };
260 static const u16 NCT6775_REG_PWM[] = { 0x109, 0x209, 0x309, 0x809, 0x909 };
261 static const u16 NCT6775_REG_PWM_READ[] = { 0x01, 0x03, 0x11, 0x13, 0x15 };
263 static const u16 NCT6775_REG_FAN[] = { 0x630, 0x632, 0x634, 0x636, 0x638 };
264 static const u16 NCT6775_REG_FAN_MIN[] = { 0x3b, 0x3c, 0x3d };
266 static const u16 NCT6776_REG_FAN_MIN[] = { 0x63a, 0x63c, 0x63e, 0x640, 0x642};
268 static const u16 NCT6779_REG_FAN[] = { 0x4b0, 0x4b2, 0x4b4, 0x4b6, 0x4b8 };
270 static const u16 NCT6779_REG_TOLERANCE_H[]
271 = { 0x10c, 0x20c, 0x30c, 0x80c, 0x90c };
273 static const u16 NCT6775_REG_TEMP[]
274 = { 0x27, 0x150, 0x250, 0x62b, 0x62c, 0x62d };
275 static const u16 NCT6779_REG_TEMP[] = { 0x27, 0x150 };
277 static const u16 NCT6775_REG_TEMP_CONFIG[]
278 = { 0, 0x152, 0x252, 0x628, 0x629, 0x62A };
279 static const u16 NCT6775_REG_TEMP_HYST[]
280 = { 0x3a, 0x153, 0x253, 0x673, 0x678, 0x67D };
281 static const u16 NCT6775_REG_TEMP_OVER[]
282 = { 0x39, 0x155, 0x255, 0x672, 0x677, 0x67C };
284 static const u16 NCT6779_REG_TEMP_HYST[]
285 = { 0x3a, 0x153, 0, 0, 0, 0 };
286 static const u16 NCT6779_REG_TEMP_OVER[]
287 = { 0x39, 0x155, 0, 0, 0, 0 };
289 static const u16 NCT6775_REG_TEMP_SOURCE[]
290 = { 0x621, 0x622, 0x623, 0x624, 0x625, 0x626 };
292 static const u16 NCT6775_REG_TEMP_SEL[]
293 = { 0x100, 0x200, 0x300, 0x800, 0x900 };
295 static const u16 NCT6775_REG_WEIGHT_TEMP_SEL[]
296 = { 0x139, 0x239, 0x339, 0x839, 0x939 };
297 static const u16 NCT6775_REG_WEIGHT_TEMP_STEP[]
298 = { 0x13a, 0x23a, 0x33a, 0x83a, 0x93a };
299 static const u16 NCT6775_REG_WEIGHT_TEMP_STEP_TOL[]
300 = { 0x13b, 0x23b, 0x33b, 0x83b, 0x93b };
301 static const u16 NCT6775_REG_WEIGHT_DUTY_STEP[]
302 = { 0x13c, 0x23c, 0x33c, 0x83c, 0x93c };
303 static const u16 NCT6775_REG_WEIGHT_TEMP_BASE[]
304 = { 0x13d, 0x23d, 0x33d, 0x83d, 0x93d };
306 static const u16 NCT6776_REG_WEIGHT_DUTY_BASE[]
307 = { 0x13e, 0x23e, 0x33e, 0x83e, 0x93e };
309 static const u16 NCT6775_REG_TEMP_OFFSET[] = { 0x454, 0x455, 0x456 };
311 static const u16 NCT6779_REG_TEMP_OFFSET[]
312 = { 0x454, 0x455, 0x456, 0x44a, 0x44b, 0x44c };
314 static const u16 NCT6776_REG_TEMP_CONFIG[11]
315 = { 0x18, 0x152, 0x252, 0x628, 0x629, 0x62A };
317 static const u16 NCT6779_REG_TEMP_CONFIG[11] = { 0x18, 0x152 };
319 static const u16 NCT6775_REG_TEMP_MON[] = { 0x73, 0x75, 0x77, 0x79, 0x7b };
321 static const u16 NCT6775_REG_AUTO_TEMP[]
322 = { 0x121, 0x221, 0x321, 0x821, 0x921 };
323 static const u16 NCT6775_REG_AUTO_PWM[]
324 = { 0x127, 0x227, 0x327, 0x827, 0x927 };
326 #define NCT6775_AUTO_TEMP(data, nr, p) ((data)->REG_AUTO_TEMP[nr] + (p))
327 #define NCT6775_AUTO_PWM(data, nr, p) ((data)->REG_AUTO_PWM[nr] + (p))
329 static const u16 NCT6775_REG_CRITICAL_ENAB[] = { 0x134, 0x234, 0x334 };
331 static const u16 NCT6775_REG_CRITICAL_TEMP[] = {
332 0x135, 0x235, 0x335, 0x835, 0x935 };
333 static const u16 NCT6775_REG_CRITICAL_TEMP_TOLERANCE[] = {
334 0x138, 0x238, 0x338, 0x838, 0x938 };
336 static const u16 NCT6779_REG_CRITICAL_PWM_ENABLE[] = {
337 0x136, 0x236, 0x336, 0x836, 0x936 };
338 static const u16 NCT6779_REG_CRITICAL_PWM[] = {
339 0x137, 0x237, 0x337, 0x837, 0x937 };
341 static const char *const nct6775_temp_label[] = {
355 "PCH_CHIP_CPU_MAX_TEMP",
365 static const char *const nct6776_temp_label[] = {
380 "PCH_CHIP_CPU_MAX_TEMP",
391 static const char *const nct6779_temp_label[] = {
410 "PCH_CHIP_CPU_MAX_TEMP",
421 static const u16 NCT6775_REG_TEMP_ALTERNATE[ARRAY_SIZE(nct6775_temp_label) - 1]
422 = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0x661, 0x662, 0x664 };
424 static const u16 NCT6776_REG_TEMP_ALTERNATE[ARRAY_SIZE(nct6776_temp_label) - 1]
425 = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0x401, 0x402, 0x404 };
427 static const u16 NCT6779_REG_TEMP_ALTERNATE[ARRAY_SIZE(nct6779_temp_label) - 1]
428 = { 0x490, 0x491, 0x492, 0x493, 0x494, 0x495, 0, 0,
429 0, 0, 0, 0, 0, 0, 0, 0,
430 0, 0x400, 0x401, 0x402, 0x404, 0x405, 0x406, 0x407,
433 static const u16 NCT6775_REG_TEMP_CRIT[ARRAY_SIZE(nct6775_temp_label) - 1]
434 = { 0, 0, 0, 0, 0xa00, 0xa01, 0xa02, 0xa03, 0xa04, 0xa05, 0xa06,
437 static const u16 NCT6776_REG_TEMP_CRIT[ARRAY_SIZE(nct6776_temp_label) - 1]
438 = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0x709, 0x70a };
440 static const u16 NCT6779_REG_TEMP_CRIT[ARRAY_SIZE(nct6779_temp_label) - 1]
441 = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0x709, 0x70a };
443 #define NUM_TEMP 10 /* Max number of temp attribute sets w/ limits*/
444 #define NUM_TEMP_FIXED 6 /* Max number of fixed temp attribute sets */
446 static inline int reg_to_pwm_enable(int pwm, int mode)
448 if (mode == 0 && pwm == 255)
453 static inline int pwm_enable_to_reg(int mode)
464 /* 1 is DC mode, output in ms */
465 static inline unsigned int step_time_from_reg(u8 reg, u8 mode)
467 return mode ? 400 * reg : 100 * reg;
470 static inline u8 step_time_to_reg(unsigned int msec, u8 mode)
472 return SENSORS_LIMIT((mode ? (msec + 200) / 400 :
473 (msec + 50) / 100), 1, 255);
476 static unsigned int fan_from_reg8(u16 reg, unsigned int divreg)
478 if (reg == 0 || reg == 255)
480 return 1350000U / (reg << divreg);
483 static unsigned int fan_from_reg13(u16 reg, unsigned int divreg)
485 if ((reg & 0xff1f) == 0xff1f)
488 reg = (reg & 0x1f) | ((reg & 0xff00) >> 3);
493 return 1350000U / reg;
496 static unsigned int fan_from_reg16(u16 reg, unsigned int divreg)
498 if (reg == 0 || reg == 0xffff)
502 * Even though the registers are 16 bit wide, the fan divisor
505 return 1350000U / (reg << divreg);
508 static inline unsigned int
515 * Some of the voltage inputs have internal scaling, the tables below
516 * contain 8 (the ADC LSB in mV) * scaling factor * 100
518 static const u16 scale_in[15] = {
519 800, 800, 1600, 1600, 800, 800, 800, 1600, 1600, 800, 800, 800, 800,
523 static inline long in_from_reg(u8 reg, u8 nr)
525 return DIV_ROUND_CLOSEST(reg * scale_in[nr], 100);
528 static inline u8 in_to_reg(u32 val, u8 nr)
530 return SENSORS_LIMIT(DIV_ROUND_CLOSEST(val * 100, scale_in[nr]), 0,
535 * Data structures and manipulation thereof
538 struct nct6775_data {
539 int addr; /* IO base of hw monitor block */
543 struct device *hwmon_dev;
546 u16 reg_temp[4][NUM_TEMP]; /* 0=temp, 1=temp_over, 2=temp_hyst,
549 u8 temp_src[NUM_TEMP];
550 u16 reg_temp_config[NUM_TEMP];
551 const char * const *temp_label;
558 const s8 *ALARM_BITS;
561 const u16 *REG_IN_MINMAX[2];
563 const u16 *REG_TARGET;
565 const u16 *REG_FAN_MODE;
566 const u16 *REG_FAN_MIN;
567 const u16 *REG_FAN_TIME[3];
569 const u8 *REG_PWM_MODE;
570 const u8 *PWM_MODE_MASK;
572 const u16 *REG_PWM[7]; /* [0]=pwm, [1]=start_output, [2]=stop_output,
573 * [3]=max_output, [4]=step_output,
574 * [5]=weight_duty_step, [6]=weight_duty_base
576 const u16 *REG_PWM_READ;
578 const u16 *REG_TEMP_MON;
579 const u16 *REG_AUTO_TEMP;
580 const u16 *REG_AUTO_PWM;
582 const u16 *REG_CRITICAL_TEMP;
583 const u16 *REG_CRITICAL_TEMP_TOLERANCE;
585 const u16 *REG_TEMP_SOURCE; /* temp register sources */
586 const u16 *REG_TEMP_SEL[3]; /* pwm temp:
587 * 0=base, 1=weight, 2=step enable
590 const u16 *REG_WEIGHT_TEMP[3]; /* 0=base, 1=hyst, 2=step */
592 const u16 *REG_TEMP_OFFSET;
594 const u16 *REG_ALARM;
596 unsigned int (*fan_from_reg)(u16 reg, unsigned int divreg);
597 unsigned int (*fan_from_reg_min)(u16 reg, unsigned int divreg);
599 struct mutex update_lock;
600 char valid; /* !=0 if following fields are valid */
601 unsigned long last_updated; /* In jiffies */
603 /* Register values */
604 u8 bank; /* current register bank */
605 u8 in_num; /* number of in inputs we have */
606 u8 in[15][3]; /* [0]=in, [1]=in_max, [2]=in_min */
611 u8 has_fan; /* some fan inputs can be disabled */
612 u8 has_fan_min; /* some fans don't have min register */
615 u8 temp_fixed_num; /* 3 or 6 */
616 u8 temp_type[NUM_TEMP_FIXED];
617 s8 temp_offset[NUM_TEMP_FIXED];
618 s16 temp[3][NUM_TEMP]; /* 0=temp, 1=temp_over, 2=temp_hyst */
621 u8 pwm_num; /* number of pwm */
623 u8 pwm_mode[5]; /* 1->DC variable voltage, 0->PWM variable duty cycle */
624 u8 pwm_enable[5]; /* 0->off
626 * 2->thermal cruise mode (also called SmartFan I)
627 * 3->fan speed cruise mode
629 * 5->enhanced variable thermal cruise (SmartFan IV)
631 u8 pwm[7][5]; /* [0]=pwm, [1]=start_output, [2]=stop_output,
632 * [3]=max_output, [4]=step_output,
633 * [5]=weight_duty_step, [6]=weight_duty_base
639 u8 fan_time[3][5]; /* 0 = stop_time, 1 = step_up, 2 = step_down */
641 /* Automatic fan speed control registers */
646 u8 pwm_temp_sel[2][5];
648 bool pwm_sel_enable[3][5];/* 0->stop_val, 1->weight, 2->step;
649 * false->off, true->on
651 u8 weight_temp[3][5]; /* 0->temp_step, 1->temp_step_tol,
663 struct nct6775_sio_data {
668 static bool is_word_sized(struct nct6775_data *data, u16 reg)
670 switch (data->kind) {
672 return (((reg & 0xff00) == 0x100 ||
673 (reg & 0xff00) == 0x200) &&
674 ((reg & 0x00ff) == 0x50 ||
675 (reg & 0x00ff) == 0x53 ||
676 (reg & 0x00ff) == 0x55)) ||
677 (reg & 0xfff0) == 0x630 ||
678 reg == 0x640 || reg == 0x642 ||
680 ((reg & 0xfff0) == 0x650 && (reg & 0x000f) >= 0x06) ||
681 reg == 0x73 || reg == 0x75 || reg == 0x77;
683 return (((reg & 0xff00) == 0x100 ||
684 (reg & 0xff00) == 0x200) &&
685 ((reg & 0x00ff) == 0x50 ||
686 (reg & 0x00ff) == 0x53 ||
687 (reg & 0x00ff) == 0x55)) ||
688 (reg & 0xfff0) == 0x630 ||
690 reg == 0x640 || reg == 0x642 ||
691 ((reg & 0xfff0) == 0x650 && (reg & 0x000f) >= 0x06) ||
692 reg == 0x73 || reg == 0x75 || reg == 0x77;
694 return reg == 0x150 || reg == 0x153 || reg == 0x155 ||
695 ((reg & 0xfff0) == 0x4b0 && (reg & 0x000f) < 0x09) ||
697 reg == 0x63a || reg == 0x63c || reg == 0x63e ||
698 reg == 0x640 || reg == 0x642 ||
699 reg == 0x73 || reg == 0x75 || reg == 0x77 || reg == 0x79 ||
706 * On older chips, only registers 0x50-0x5f are banked.
707 * On more recent chips, all registers are banked.
708 * Assume that is the case and set the bank number for each access.
709 * Cache the bank number so it only needs to be set if it changes.
711 static inline void nct6775_set_bank(struct nct6775_data *data, u16 reg)
714 if (data->bank != bank) {
715 outb_p(NCT6775_REG_BANK, data->addr + ADDR_REG_OFFSET);
716 outb_p(bank, data->addr + DATA_REG_OFFSET);
721 static u16 nct6775_read_value(struct nct6775_data *data, u16 reg)
723 int res, word_sized = is_word_sized(data, reg);
725 mutex_lock(&data->lock);
727 nct6775_set_bank(data, reg);
728 outb_p(reg & 0xff, data->addr + ADDR_REG_OFFSET);
729 res = inb_p(data->addr + DATA_REG_OFFSET);
731 outb_p((reg & 0xff) + 1,
732 data->addr + ADDR_REG_OFFSET);
733 res = (res << 8) + inb_p(data->addr + DATA_REG_OFFSET);
736 mutex_unlock(&data->lock);
740 static int nct6775_write_value(struct nct6775_data *data, u16 reg, u16 value)
742 int word_sized = is_word_sized(data, reg);
744 mutex_lock(&data->lock);
746 nct6775_set_bank(data, reg);
747 outb_p(reg & 0xff, data->addr + ADDR_REG_OFFSET);
749 outb_p(value >> 8, data->addr + DATA_REG_OFFSET);
750 outb_p((reg & 0xff) + 1,
751 data->addr + ADDR_REG_OFFSET);
753 outb_p(value & 0xff, data->addr + DATA_REG_OFFSET);
755 mutex_unlock(&data->lock);
759 /* We left-align 8-bit temperature values to make the code simpler */
760 static u16 nct6775_read_temp(struct nct6775_data *data, u16 reg)
764 res = nct6775_read_value(data, reg);
765 if (!is_word_sized(data, reg))
771 static int nct6775_write_temp(struct nct6775_data *data, u16 reg, u16 value)
773 if (!is_word_sized(data, reg))
775 return nct6775_write_value(data, reg, value);
778 /* This function assumes that the caller holds data->update_lock */
779 static void nct6775_write_fan_div(struct nct6775_data *data, int nr)
785 reg = (nct6775_read_value(data, NCT6775_REG_FANDIV1) & 0x70)
786 | (data->fan_div[0] & 0x7);
787 nct6775_write_value(data, NCT6775_REG_FANDIV1, reg);
790 reg = (nct6775_read_value(data, NCT6775_REG_FANDIV1) & 0x7)
791 | ((data->fan_div[1] << 4) & 0x70);
792 nct6775_write_value(data, NCT6775_REG_FANDIV1, reg);
795 reg = (nct6775_read_value(data, NCT6775_REG_FANDIV2) & 0x70)
796 | (data->fan_div[2] & 0x7);
797 nct6775_write_value(data, NCT6775_REG_FANDIV2, reg);
800 reg = (nct6775_read_value(data, NCT6775_REG_FANDIV2) & 0x7)
801 | ((data->fan_div[3] << 4) & 0x70);
802 nct6775_write_value(data, NCT6775_REG_FANDIV2, reg);
807 static void nct6775_write_fan_div_common(struct device *dev,
808 struct nct6775_data *data, int nr)
810 if (data->kind == nct6775)
811 nct6775_write_fan_div(data, nr);
814 static void nct6775_update_fan_div(struct nct6775_data *data)
818 i = nct6775_read_value(data, NCT6775_REG_FANDIV1);
819 data->fan_div[0] = i & 0x7;
820 data->fan_div[1] = (i & 0x70) >> 4;
821 i = nct6775_read_value(data, NCT6775_REG_FANDIV2);
822 data->fan_div[2] = i & 0x7;
823 if (data->has_fan & (1<<3))
824 data->fan_div[3] = (i & 0x70) >> 4;
827 static void nct6775_update_fan_div_common(struct device *dev,
828 struct nct6775_data *data)
830 if (data->kind == nct6775)
831 nct6775_update_fan_div(data);
834 static void nct6775_update_pwm(struct device *dev)
836 struct nct6775_data *data = dev_get_drvdata(dev);
841 for (i = 0; i < data->pwm_num; i++) {
842 if (!(data->has_pwm & (1 << i)))
845 duty_is_dc = data->REG_PWM_MODE[i] &&
846 (nct6775_read_value(data, data->REG_PWM_MODE[i])
847 & data->PWM_MODE_MASK[i]);
848 data->pwm_mode[i] = duty_is_dc;
850 fanmodecfg = nct6775_read_value(data, data->REG_FAN_MODE[i]);
851 for (j = 0; j < ARRAY_SIZE(data->REG_PWM); j++) {
852 if (data->REG_PWM[j] && data->REG_PWM[j][i]) {
854 = nct6775_read_value(data,
855 data->REG_PWM[j][i]);
859 data->pwm_enable[i] = reg_to_pwm_enable(data->pwm[0][i],
860 (fanmodecfg >> 4) & 7);
862 data->tolerance[i][0] = fanmodecfg & 0x0f;
863 if (data->kind == nct6779) {
864 reg = nct6775_read_value(data,
865 NCT6779_REG_TOLERANCE_H[i]);
866 data->tolerance[i][0] |= (reg & 0x70) >> 1;
868 data->tolerance[i][1] =
869 nct6775_read_value(data,
870 data->REG_CRITICAL_TEMP_TOLERANCE[i]);
872 data->pwm_sel_enable[0][i] =
873 nct6775_read_value(data, data->REG_TEMP_SEL[0][i])
875 data->pwm_sel_enable[1][i] =
876 nct6775_read_value(data, data->REG_TEMP_SEL[1][i])
878 data->pwm_sel_enable[2][i] =
879 nct6775_read_value(data, data->REG_TEMP_SEL[2][i])
883 for (j = 0; j < 2; j++) {
884 reg = nct6775_read_value(data,
885 data->REG_TEMP_SEL[j][i]);
886 data->pwm_temp_sel[j][i] = reg & 0x1f;
888 /* Weight temp data */
889 for (j = 0; j < 3; j++) {
890 data->weight_temp[j][i]
891 = nct6775_read_value(data,
892 data->REG_WEIGHT_TEMP[j][i]);
897 static void nct6775_update_pwm_limits(struct device *dev)
899 struct nct6775_data *data = dev_get_drvdata(dev);
903 for (i = 0; i < data->pwm_num; i++) {
904 if (!(data->has_pwm & (1 << i)))
907 for (j = 0; j < 3; j++) {
908 data->fan_time[j][i] =
909 nct6775_read_value(data, data->REG_FAN_TIME[j][i]);
912 data->target_temp[i] =
913 nct6775_read_value(data, data->REG_TARGET[i]) &
914 (data->pwm_mode[i] ? 0xff : 0x7f);
916 nct6775_read_value(data, data->REG_TEMP_MON[i]);
918 for (j = 0; j < data->auto_pwm_num; j++) {
919 data->auto_pwm[i][j] =
920 nct6775_read_value(data,
921 NCT6775_AUTO_PWM(data, i, j));
922 data->auto_temp[i][j] =
923 nct6775_read_value(data,
924 NCT6775_AUTO_TEMP(data, i, j));
927 /* critical auto_pwm temperature data */
928 data->auto_temp[i][data->auto_pwm_num] =
929 nct6775_read_value(data, data->REG_CRITICAL_TEMP[i]);
931 switch (data->kind) {
933 reg = nct6775_read_value(data,
934 NCT6775_REG_CRITICAL_ENAB[i]);
935 data->auto_pwm[i][data->auto_pwm_num] =
936 (reg & 0x02) ? 0xff : 0x00;
939 data->auto_pwm[i][data->auto_pwm_num] = 0xff;
942 reg = nct6775_read_value(data,
943 NCT6779_REG_CRITICAL_PWM_ENABLE[i]);
945 data->auto_pwm[i][data->auto_pwm_num] =
946 nct6775_read_value(data,
947 NCT6779_REG_CRITICAL_PWM[i]);
949 data->auto_pwm[i][data->auto_pwm_num] = 0xff;
955 static struct nct6775_data *nct6775_update_device(struct device *dev)
957 struct nct6775_data *data = dev_get_drvdata(dev);
960 mutex_lock(&data->update_lock);
962 if (time_after(jiffies, data->last_updated + HZ + HZ/2)
964 /* Fan clock dividers */
965 nct6775_update_fan_div_common(dev, data);
967 /* Measured voltages and limits */
968 for (i = 0; i < data->in_num; i++) {
969 if (!(data->have_in & (1 << i)))
972 data->in[i][0] = nct6775_read_value(data,
974 data->in[i][1] = nct6775_read_value(data,
975 data->REG_IN_MINMAX[0][i]);
976 data->in[i][2] = nct6775_read_value(data,
977 data->REG_IN_MINMAX[1][i]);
980 /* Measured fan speeds and limits */
981 for (i = 0; i < 5; i++) {
984 if (!(data->has_fan & (1 << i)))
987 reg = nct6775_read_value(data, data->REG_FAN[i]);
988 data->rpm[i] = data->fan_from_reg(reg,
991 if (data->has_fan_min & (1 << i))
992 data->fan_min[i] = nct6775_read_value(data,
993 data->REG_FAN_MIN[i]);
996 * If we failed to measure the fan speed and clock
997 * divider can be increased, let's try that for next
1000 if (data->has_fan_div && (reg >= 0xff || reg == 0x00)
1001 && data->fan_div[i] < 0x07) {
1003 "Increasing fan%d clock divider from %u to %u\n",
1004 i + 1, div_from_reg(data->fan_div[i]),
1005 div_from_reg(data->fan_div[i] + 1));
1007 nct6775_write_fan_div_common(dev, data, i);
1008 /* Preserve min limit if possible */
1009 if ((data->has_fan_min & (1 << i))
1010 && data->fan_min[i] >= 2
1011 && data->fan_min[i] != 255)
1012 nct6775_write_value(data,
1013 data->REG_FAN_MIN[i],
1014 (data->fan_min[i] /= 2));
1018 nct6775_update_pwm(dev);
1019 nct6775_update_pwm_limits(dev);
1021 /* Measured temperatures and limits */
1022 for (i = 0; i < NUM_TEMP; i++) {
1023 if (!(data->have_temp & (1 << i)))
1025 for (j = 0; j < 4; j++) {
1026 if (data->reg_temp[j][i])
1028 = nct6775_read_temp(data,
1029 data->reg_temp[j][i]);
1031 if (!(data->have_temp_fixed & (1 << i)))
1033 data->temp_offset[i]
1034 = nct6775_read_value(data, data->REG_TEMP_OFFSET[i]);
1038 for (i = 0; i < 6; i++) {
1040 if (!data->REG_ALARM[i])
1042 alarm = nct6775_read_value(data, data->REG_ALARM[i]);
1043 data->alarms |= ((u64)alarm) << (i << 3);
1046 data->last_updated = jiffies;
1050 mutex_unlock(&data->update_lock);
1055 * Sysfs callback functions
1058 show_in_reg(struct device *dev, struct device_attribute *attr, char *buf)
1060 struct nct6775_data *data = nct6775_update_device(dev);
1061 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
1063 int index = sattr->index;
1064 return sprintf(buf, "%ld\n", in_from_reg(data->in[nr][index], nr));
1068 store_in_reg(struct device *dev, struct device_attribute *attr, const char *buf,
1071 struct nct6775_data *data = dev_get_drvdata(dev);
1072 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
1074 int index = sattr->index;
1076 int err = kstrtoul(buf, 10, &val);
1079 mutex_lock(&data->update_lock);
1080 data->in[nr][index] = in_to_reg(val, nr);
1081 nct6775_write_value(data, data->REG_IN_MINMAX[index-1][nr],
1082 data->in[nr][index]);
1083 mutex_unlock(&data->update_lock);
1088 show_alarm(struct device *dev, struct device_attribute *attr, char *buf)
1090 struct nct6775_data *data = nct6775_update_device(dev);
1091 struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr);
1092 int nr = data->ALARM_BITS[sattr->index];
1093 return sprintf(buf, "%u\n",
1094 (unsigned int)((data->alarms >> nr) & 0x01));
1097 static SENSOR_DEVICE_ATTR_2(in0_input, S_IRUGO, show_in_reg, NULL, 0, 0);
1098 static SENSOR_DEVICE_ATTR_2(in1_input, S_IRUGO, show_in_reg, NULL, 1, 0);
1099 static SENSOR_DEVICE_ATTR_2(in2_input, S_IRUGO, show_in_reg, NULL, 2, 0);
1100 static SENSOR_DEVICE_ATTR_2(in3_input, S_IRUGO, show_in_reg, NULL, 3, 0);
1101 static SENSOR_DEVICE_ATTR_2(in4_input, S_IRUGO, show_in_reg, NULL, 4, 0);
1102 static SENSOR_DEVICE_ATTR_2(in5_input, S_IRUGO, show_in_reg, NULL, 5, 0);
1103 static SENSOR_DEVICE_ATTR_2(in6_input, S_IRUGO, show_in_reg, NULL, 6, 0);
1104 static SENSOR_DEVICE_ATTR_2(in7_input, S_IRUGO, show_in_reg, NULL, 7, 0);
1105 static SENSOR_DEVICE_ATTR_2(in8_input, S_IRUGO, show_in_reg, NULL, 8, 0);
1106 static SENSOR_DEVICE_ATTR_2(in9_input, S_IRUGO, show_in_reg, NULL, 9, 0);
1107 static SENSOR_DEVICE_ATTR_2(in10_input, S_IRUGO, show_in_reg, NULL, 10, 0);
1108 static SENSOR_DEVICE_ATTR_2(in11_input, S_IRUGO, show_in_reg, NULL, 11, 0);
1109 static SENSOR_DEVICE_ATTR_2(in12_input, S_IRUGO, show_in_reg, NULL, 12, 0);
1110 static SENSOR_DEVICE_ATTR_2(in13_input, S_IRUGO, show_in_reg, NULL, 13, 0);
1111 static SENSOR_DEVICE_ATTR_2(in14_input, S_IRUGO, show_in_reg, NULL, 14, 0);
1113 static SENSOR_DEVICE_ATTR(in0_alarm, S_IRUGO, show_alarm, NULL, 0);
1114 static SENSOR_DEVICE_ATTR(in1_alarm, S_IRUGO, show_alarm, NULL, 1);
1115 static SENSOR_DEVICE_ATTR(in2_alarm, S_IRUGO, show_alarm, NULL, 2);
1116 static SENSOR_DEVICE_ATTR(in3_alarm, S_IRUGO, show_alarm, NULL, 3);
1117 static SENSOR_DEVICE_ATTR(in4_alarm, S_IRUGO, show_alarm, NULL, 4);
1118 static SENSOR_DEVICE_ATTR(in5_alarm, S_IRUGO, show_alarm, NULL, 5);
1119 static SENSOR_DEVICE_ATTR(in6_alarm, S_IRUGO, show_alarm, NULL, 6);
1120 static SENSOR_DEVICE_ATTR(in7_alarm, S_IRUGO, show_alarm, NULL, 7);
1121 static SENSOR_DEVICE_ATTR(in8_alarm, S_IRUGO, show_alarm, NULL, 8);
1122 static SENSOR_DEVICE_ATTR(in9_alarm, S_IRUGO, show_alarm, NULL, 9);
1123 static SENSOR_DEVICE_ATTR(in10_alarm, S_IRUGO, show_alarm, NULL, 10);
1124 static SENSOR_DEVICE_ATTR(in11_alarm, S_IRUGO, show_alarm, NULL, 11);
1125 static SENSOR_DEVICE_ATTR(in12_alarm, S_IRUGO, show_alarm, NULL, 12);
1126 static SENSOR_DEVICE_ATTR(in13_alarm, S_IRUGO, show_alarm, NULL, 13);
1127 static SENSOR_DEVICE_ATTR(in14_alarm, S_IRUGO, show_alarm, NULL, 14);
1129 static SENSOR_DEVICE_ATTR_2(in0_min, S_IWUSR | S_IRUGO, show_in_reg,
1130 store_in_reg, 0, 1);
1131 static SENSOR_DEVICE_ATTR_2(in1_min, S_IWUSR | S_IRUGO, show_in_reg,
1132 store_in_reg, 1, 1);
1133 static SENSOR_DEVICE_ATTR_2(in2_min, S_IWUSR | S_IRUGO, show_in_reg,
1134 store_in_reg, 2, 1);
1135 static SENSOR_DEVICE_ATTR_2(in3_min, S_IWUSR | S_IRUGO, show_in_reg,
1136 store_in_reg, 3, 1);
1137 static SENSOR_DEVICE_ATTR_2(in4_min, S_IWUSR | S_IRUGO, show_in_reg,
1138 store_in_reg, 4, 1);
1139 static SENSOR_DEVICE_ATTR_2(in5_min, S_IWUSR | S_IRUGO, show_in_reg,
1140 store_in_reg, 5, 1);
1141 static SENSOR_DEVICE_ATTR_2(in6_min, S_IWUSR | S_IRUGO, show_in_reg,
1142 store_in_reg, 6, 1);
1143 static SENSOR_DEVICE_ATTR_2(in7_min, S_IWUSR | S_IRUGO, show_in_reg,
1144 store_in_reg, 7, 1);
1145 static SENSOR_DEVICE_ATTR_2(in8_min, S_IWUSR | S_IRUGO, show_in_reg,
1146 store_in_reg, 8, 1);
1147 static SENSOR_DEVICE_ATTR_2(in9_min, S_IWUSR | S_IRUGO, show_in_reg,
1148 store_in_reg, 9, 1);
1149 static SENSOR_DEVICE_ATTR_2(in10_min, S_IWUSR | S_IRUGO, show_in_reg,
1150 store_in_reg, 10, 1);
1151 static SENSOR_DEVICE_ATTR_2(in11_min, S_IWUSR | S_IRUGO, show_in_reg,
1152 store_in_reg, 11, 1);
1153 static SENSOR_DEVICE_ATTR_2(in12_min, S_IWUSR | S_IRUGO, show_in_reg,
1154 store_in_reg, 12, 1);
1155 static SENSOR_DEVICE_ATTR_2(in13_min, S_IWUSR | S_IRUGO, show_in_reg,
1156 store_in_reg, 13, 1);
1157 static SENSOR_DEVICE_ATTR_2(in14_min, S_IWUSR | S_IRUGO, show_in_reg,
1158 store_in_reg, 14, 1);
1160 static SENSOR_DEVICE_ATTR_2(in0_max, S_IWUSR | S_IRUGO, show_in_reg,
1161 store_in_reg, 0, 2);
1162 static SENSOR_DEVICE_ATTR_2(in1_max, S_IWUSR | S_IRUGO, show_in_reg,
1163 store_in_reg, 1, 2);
1164 static SENSOR_DEVICE_ATTR_2(in2_max, S_IWUSR | S_IRUGO, show_in_reg,
1165 store_in_reg, 2, 2);
1166 static SENSOR_DEVICE_ATTR_2(in3_max, S_IWUSR | S_IRUGO, show_in_reg,
1167 store_in_reg, 3, 2);
1168 static SENSOR_DEVICE_ATTR_2(in4_max, S_IWUSR | S_IRUGO, show_in_reg,
1169 store_in_reg, 4, 2);
1170 static SENSOR_DEVICE_ATTR_2(in5_max, S_IWUSR | S_IRUGO, show_in_reg,
1171 store_in_reg, 5, 2);
1172 static SENSOR_DEVICE_ATTR_2(in6_max, S_IWUSR | S_IRUGO, show_in_reg,
1173 store_in_reg, 6, 2);
1174 static SENSOR_DEVICE_ATTR_2(in7_max, S_IWUSR | S_IRUGO, show_in_reg,
1175 store_in_reg, 7, 2);
1176 static SENSOR_DEVICE_ATTR_2(in8_max, S_IWUSR | S_IRUGO, show_in_reg,
1177 store_in_reg, 8, 2);
1178 static SENSOR_DEVICE_ATTR_2(in9_max, S_IWUSR | S_IRUGO, show_in_reg,
1179 store_in_reg, 9, 2);
1180 static SENSOR_DEVICE_ATTR_2(in10_max, S_IWUSR | S_IRUGO, show_in_reg,
1181 store_in_reg, 10, 2);
1182 static SENSOR_DEVICE_ATTR_2(in11_max, S_IWUSR | S_IRUGO, show_in_reg,
1183 store_in_reg, 11, 2);
1184 static SENSOR_DEVICE_ATTR_2(in12_max, S_IWUSR | S_IRUGO, show_in_reg,
1185 store_in_reg, 12, 2);
1186 static SENSOR_DEVICE_ATTR_2(in13_max, S_IWUSR | S_IRUGO, show_in_reg,
1187 store_in_reg, 13, 2);
1188 static SENSOR_DEVICE_ATTR_2(in14_max, S_IWUSR | S_IRUGO, show_in_reg,
1189 store_in_reg, 14, 2);
1191 static struct attribute *nct6775_attributes_in[15][5] = {
1193 &sensor_dev_attr_in0_input.dev_attr.attr,
1194 &sensor_dev_attr_in0_min.dev_attr.attr,
1195 &sensor_dev_attr_in0_max.dev_attr.attr,
1196 &sensor_dev_attr_in0_alarm.dev_attr.attr,
1200 &sensor_dev_attr_in1_input.dev_attr.attr,
1201 &sensor_dev_attr_in1_min.dev_attr.attr,
1202 &sensor_dev_attr_in1_max.dev_attr.attr,
1203 &sensor_dev_attr_in1_alarm.dev_attr.attr,
1207 &sensor_dev_attr_in2_input.dev_attr.attr,
1208 &sensor_dev_attr_in2_min.dev_attr.attr,
1209 &sensor_dev_attr_in2_max.dev_attr.attr,
1210 &sensor_dev_attr_in2_alarm.dev_attr.attr,
1214 &sensor_dev_attr_in3_input.dev_attr.attr,
1215 &sensor_dev_attr_in3_min.dev_attr.attr,
1216 &sensor_dev_attr_in3_max.dev_attr.attr,
1217 &sensor_dev_attr_in3_alarm.dev_attr.attr,
1221 &sensor_dev_attr_in4_input.dev_attr.attr,
1222 &sensor_dev_attr_in4_min.dev_attr.attr,
1223 &sensor_dev_attr_in4_max.dev_attr.attr,
1224 &sensor_dev_attr_in4_alarm.dev_attr.attr,
1228 &sensor_dev_attr_in5_input.dev_attr.attr,
1229 &sensor_dev_attr_in5_min.dev_attr.attr,
1230 &sensor_dev_attr_in5_max.dev_attr.attr,
1231 &sensor_dev_attr_in5_alarm.dev_attr.attr,
1235 &sensor_dev_attr_in6_input.dev_attr.attr,
1236 &sensor_dev_attr_in6_min.dev_attr.attr,
1237 &sensor_dev_attr_in6_max.dev_attr.attr,
1238 &sensor_dev_attr_in6_alarm.dev_attr.attr,
1242 &sensor_dev_attr_in7_input.dev_attr.attr,
1243 &sensor_dev_attr_in7_min.dev_attr.attr,
1244 &sensor_dev_attr_in7_max.dev_attr.attr,
1245 &sensor_dev_attr_in7_alarm.dev_attr.attr,
1249 &sensor_dev_attr_in8_input.dev_attr.attr,
1250 &sensor_dev_attr_in8_min.dev_attr.attr,
1251 &sensor_dev_attr_in8_max.dev_attr.attr,
1252 &sensor_dev_attr_in8_alarm.dev_attr.attr,
1256 &sensor_dev_attr_in9_input.dev_attr.attr,
1257 &sensor_dev_attr_in9_min.dev_attr.attr,
1258 &sensor_dev_attr_in9_max.dev_attr.attr,
1259 &sensor_dev_attr_in9_alarm.dev_attr.attr,
1263 &sensor_dev_attr_in10_input.dev_attr.attr,
1264 &sensor_dev_attr_in10_min.dev_attr.attr,
1265 &sensor_dev_attr_in10_max.dev_attr.attr,
1266 &sensor_dev_attr_in10_alarm.dev_attr.attr,
1270 &sensor_dev_attr_in11_input.dev_attr.attr,
1271 &sensor_dev_attr_in11_min.dev_attr.attr,
1272 &sensor_dev_attr_in11_max.dev_attr.attr,
1273 &sensor_dev_attr_in11_alarm.dev_attr.attr,
1277 &sensor_dev_attr_in12_input.dev_attr.attr,
1278 &sensor_dev_attr_in12_min.dev_attr.attr,
1279 &sensor_dev_attr_in12_max.dev_attr.attr,
1280 &sensor_dev_attr_in12_alarm.dev_attr.attr,
1284 &sensor_dev_attr_in13_input.dev_attr.attr,
1285 &sensor_dev_attr_in13_min.dev_attr.attr,
1286 &sensor_dev_attr_in13_max.dev_attr.attr,
1287 &sensor_dev_attr_in13_alarm.dev_attr.attr,
1291 &sensor_dev_attr_in14_input.dev_attr.attr,
1292 &sensor_dev_attr_in14_min.dev_attr.attr,
1293 &sensor_dev_attr_in14_max.dev_attr.attr,
1294 &sensor_dev_attr_in14_alarm.dev_attr.attr,
1299 static const struct attribute_group nct6775_group_in[15] = {
1300 { .attrs = nct6775_attributes_in[0] },
1301 { .attrs = nct6775_attributes_in[1] },
1302 { .attrs = nct6775_attributes_in[2] },
1303 { .attrs = nct6775_attributes_in[3] },
1304 { .attrs = nct6775_attributes_in[4] },
1305 { .attrs = nct6775_attributes_in[5] },
1306 { .attrs = nct6775_attributes_in[6] },
1307 { .attrs = nct6775_attributes_in[7] },
1308 { .attrs = nct6775_attributes_in[8] },
1309 { .attrs = nct6775_attributes_in[9] },
1310 { .attrs = nct6775_attributes_in[10] },
1311 { .attrs = nct6775_attributes_in[11] },
1312 { .attrs = nct6775_attributes_in[12] },
1313 { .attrs = nct6775_attributes_in[13] },
1314 { .attrs = nct6775_attributes_in[14] },
1318 show_fan(struct device *dev, struct device_attribute *attr, char *buf)
1320 struct nct6775_data *data = nct6775_update_device(dev);
1321 struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr);
1322 int nr = sattr->index;
1323 return sprintf(buf, "%d\n", data->rpm[nr]);
1327 show_fan_min(struct device *dev, struct device_attribute *attr, char *buf)
1329 struct nct6775_data *data = nct6775_update_device(dev);
1330 struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr);
1331 int nr = sattr->index;
1332 return sprintf(buf, "%d\n",
1333 data->fan_from_reg_min(data->fan_min[nr],
1334 data->fan_div[nr]));
1338 show_fan_div(struct device *dev, struct device_attribute *attr, char *buf)
1340 struct nct6775_data *data = nct6775_update_device(dev);
1341 struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr);
1342 int nr = sattr->index;
1343 return sprintf(buf, "%u\n", div_from_reg(data->fan_div[nr]));
1347 store_fan_min(struct device *dev, struct device_attribute *attr,
1348 const char *buf, size_t count)
1350 struct nct6775_data *data = dev_get_drvdata(dev);
1351 struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr);
1352 int nr = sattr->index;
1358 err = kstrtoul(buf, 10, &val);
1362 mutex_lock(&data->update_lock);
1363 if (!data->has_fan_div) {
1364 /* NCT6776F or NCT6779D; we know this is a 13 bit register */
1370 val = 1350000U / val;
1371 val = (val & 0x1f) | ((val << 3) & 0xff00);
1373 data->fan_min[nr] = val;
1374 goto write_min; /* Leave fan divider alone */
1377 /* No min limit, alarm disabled */
1378 data->fan_min[nr] = 255;
1379 new_div = data->fan_div[nr]; /* No change */
1380 dev_info(dev, "fan%u low limit and alarm disabled\n", nr + 1);
1383 reg = 1350000U / val;
1384 if (reg >= 128 * 255) {
1386 * Speed below this value cannot possibly be represented,
1387 * even with the highest divider (128)
1389 data->fan_min[nr] = 254;
1390 new_div = 7; /* 128 == (1 << 7) */
1392 "fan%u low limit %lu below minimum %u, set to minimum\n",
1393 nr + 1, val, data->fan_from_reg_min(254, 7));
1396 * Speed above this value cannot possibly be represented,
1397 * even with the lowest divider (1)
1399 data->fan_min[nr] = 1;
1400 new_div = 0; /* 1 == (1 << 0) */
1402 "fan%u low limit %lu above maximum %u, set to maximum\n",
1403 nr + 1, val, data->fan_from_reg_min(1, 0));
1406 * Automatically pick the best divider, i.e. the one such
1407 * that the min limit will correspond to a register value
1408 * in the 96..192 range
1411 while (reg > 192 && new_div < 7) {
1415 data->fan_min[nr] = reg;
1420 * Write both the fan clock divider (if it changed) and the new
1421 * fan min (unconditionally)
1423 if (new_div != data->fan_div[nr]) {
1424 dev_dbg(dev, "fan%u clock divider changed from %u to %u\n",
1425 nr + 1, div_from_reg(data->fan_div[nr]),
1426 div_from_reg(new_div));
1427 data->fan_div[nr] = new_div;
1428 nct6775_write_fan_div_common(dev, data, nr);
1429 /* Give the chip time to sample a new speed value */
1430 data->last_updated = jiffies;
1434 nct6775_write_value(data, data->REG_FAN_MIN[nr], data->fan_min[nr]);
1435 mutex_unlock(&data->update_lock);
1440 static struct sensor_device_attribute sda_fan_input[] = {
1441 SENSOR_ATTR(fan1_input, S_IRUGO, show_fan, NULL, 0),
1442 SENSOR_ATTR(fan2_input, S_IRUGO, show_fan, NULL, 1),
1443 SENSOR_ATTR(fan3_input, S_IRUGO, show_fan, NULL, 2),
1444 SENSOR_ATTR(fan4_input, S_IRUGO, show_fan, NULL, 3),
1445 SENSOR_ATTR(fan5_input, S_IRUGO, show_fan, NULL, 4),
1448 static struct sensor_device_attribute sda_fan_alarm[] = {
1449 SENSOR_ATTR(fan1_alarm, S_IRUGO, show_alarm, NULL, FAN_ALARM_BASE),
1450 SENSOR_ATTR(fan2_alarm, S_IRUGO, show_alarm, NULL, FAN_ALARM_BASE + 1),
1451 SENSOR_ATTR(fan3_alarm, S_IRUGO, show_alarm, NULL, FAN_ALARM_BASE + 2),
1452 SENSOR_ATTR(fan4_alarm, S_IRUGO, show_alarm, NULL, FAN_ALARM_BASE + 3),
1453 SENSOR_ATTR(fan5_alarm, S_IRUGO, show_alarm, NULL, FAN_ALARM_BASE + 4),
1456 static struct sensor_device_attribute sda_fan_min[] = {
1457 SENSOR_ATTR(fan1_min, S_IWUSR | S_IRUGO, show_fan_min,
1459 SENSOR_ATTR(fan2_min, S_IWUSR | S_IRUGO, show_fan_min,
1461 SENSOR_ATTR(fan3_min, S_IWUSR | S_IRUGO, show_fan_min,
1463 SENSOR_ATTR(fan4_min, S_IWUSR | S_IRUGO, show_fan_min,
1465 SENSOR_ATTR(fan5_min, S_IWUSR | S_IRUGO, show_fan_min,
1469 static struct sensor_device_attribute sda_fan_div[] = {
1470 SENSOR_ATTR(fan1_div, S_IRUGO, show_fan_div, NULL, 0),
1471 SENSOR_ATTR(fan2_div, S_IRUGO, show_fan_div, NULL, 1),
1472 SENSOR_ATTR(fan3_div, S_IRUGO, show_fan_div, NULL, 2),
1473 SENSOR_ATTR(fan4_div, S_IRUGO, show_fan_div, NULL, 3),
1474 SENSOR_ATTR(fan5_div, S_IRUGO, show_fan_div, NULL, 4),
1478 show_temp_label(struct device *dev, struct device_attribute *attr, char *buf)
1480 struct nct6775_data *data = nct6775_update_device(dev);
1481 struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr);
1482 int nr = sattr->index;
1483 return sprintf(buf, "%s\n", data->temp_label[data->temp_src[nr]]);
1487 show_temp(struct device *dev, struct device_attribute *attr, char *buf)
1489 struct nct6775_data *data = nct6775_update_device(dev);
1490 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
1492 int index = sattr->index;
1494 return sprintf(buf, "%d\n", LM75_TEMP_FROM_REG(data->temp[index][nr]));
1498 store_temp(struct device *dev, struct device_attribute *attr, const char *buf,
1501 struct nct6775_data *data = dev_get_drvdata(dev);
1502 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
1504 int index = sattr->index;
1508 err = kstrtol(buf, 10, &val);
1512 mutex_lock(&data->update_lock);
1513 data->temp[index][nr] = LM75_TEMP_TO_REG(val);
1514 nct6775_write_temp(data, data->reg_temp[index][nr],
1515 data->temp[index][nr]);
1516 mutex_unlock(&data->update_lock);
1521 show_temp_offset(struct device *dev, struct device_attribute *attr, char *buf)
1523 struct nct6775_data *data = nct6775_update_device(dev);
1524 struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr);
1526 return sprintf(buf, "%d\n", data->temp_offset[sattr->index] * 1000);
1530 store_temp_offset(struct device *dev, struct device_attribute *attr,
1531 const char *buf, size_t count)
1533 struct nct6775_data *data = dev_get_drvdata(dev);
1534 struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr);
1535 int nr = sattr->index;
1539 err = kstrtol(buf, 10, &val);
1543 val = SENSORS_LIMIT(DIV_ROUND_CLOSEST(val, 1000), -128, 127);
1545 mutex_lock(&data->update_lock);
1546 data->temp_offset[nr] = val;
1547 nct6775_write_value(data, data->REG_TEMP_OFFSET[nr], val);
1548 mutex_unlock(&data->update_lock);
1554 show_temp_type(struct device *dev, struct device_attribute *attr, char *buf)
1556 struct nct6775_data *data = nct6775_update_device(dev);
1557 struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr);
1558 int nr = sattr->index;
1559 return sprintf(buf, "%d\n", (int)data->temp_type[nr]);
1563 store_temp_type(struct device *dev, struct device_attribute *attr,
1564 const char *buf, size_t count)
1566 struct nct6775_data *data = nct6775_update_device(dev);
1567 struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr);
1568 int nr = sattr->index;
1571 u8 vbat, diode, bit;
1573 err = kstrtoul(buf, 10, &val);
1577 if (val != 1 && val != 3 && val != 4)
1580 mutex_lock(&data->update_lock);
1582 data->temp_type[nr] = val;
1583 vbat = nct6775_read_value(data, data->REG_VBAT) & ~(0x02 << nr);
1584 diode = nct6775_read_value(data, data->REG_DIODE) & ~(0x02 << nr);
1587 case 1: /* CPU diode (diode, current mode) */
1591 case 3: /* diode, voltage mode */
1594 case 4: /* thermistor */
1597 nct6775_write_value(data, data->REG_VBAT, vbat);
1598 nct6775_write_value(data, data->REG_DIODE, diode);
1600 mutex_unlock(&data->update_lock);
1604 static struct sensor_device_attribute_2 sda_temp_input[] = {
1605 SENSOR_ATTR_2(temp1_input, S_IRUGO, show_temp, NULL, 0, 0),
1606 SENSOR_ATTR_2(temp2_input, S_IRUGO, show_temp, NULL, 1, 0),
1607 SENSOR_ATTR_2(temp3_input, S_IRUGO, show_temp, NULL, 2, 0),
1608 SENSOR_ATTR_2(temp4_input, S_IRUGO, show_temp, NULL, 3, 0),
1609 SENSOR_ATTR_2(temp5_input, S_IRUGO, show_temp, NULL, 4, 0),
1610 SENSOR_ATTR_2(temp6_input, S_IRUGO, show_temp, NULL, 5, 0),
1611 SENSOR_ATTR_2(temp7_input, S_IRUGO, show_temp, NULL, 6, 0),
1612 SENSOR_ATTR_2(temp8_input, S_IRUGO, show_temp, NULL, 7, 0),
1613 SENSOR_ATTR_2(temp9_input, S_IRUGO, show_temp, NULL, 8, 0),
1614 SENSOR_ATTR_2(temp10_input, S_IRUGO, show_temp, NULL, 9, 0),
1617 static struct sensor_device_attribute sda_temp_label[] = {
1618 SENSOR_ATTR(temp1_label, S_IRUGO, show_temp_label, NULL, 0),
1619 SENSOR_ATTR(temp2_label, S_IRUGO, show_temp_label, NULL, 1),
1620 SENSOR_ATTR(temp3_label, S_IRUGO, show_temp_label, NULL, 2),
1621 SENSOR_ATTR(temp4_label, S_IRUGO, show_temp_label, NULL, 3),
1622 SENSOR_ATTR(temp5_label, S_IRUGO, show_temp_label, NULL, 4),
1623 SENSOR_ATTR(temp6_label, S_IRUGO, show_temp_label, NULL, 5),
1624 SENSOR_ATTR(temp7_label, S_IRUGO, show_temp_label, NULL, 6),
1625 SENSOR_ATTR(temp8_label, S_IRUGO, show_temp_label, NULL, 7),
1626 SENSOR_ATTR(temp9_label, S_IRUGO, show_temp_label, NULL, 8),
1627 SENSOR_ATTR(temp10_label, S_IRUGO, show_temp_label, NULL, 9),
1630 static struct sensor_device_attribute_2 sda_temp_max[] = {
1631 SENSOR_ATTR_2(temp1_max, S_IRUGO | S_IWUSR, show_temp, store_temp,
1633 SENSOR_ATTR_2(temp2_max, S_IRUGO | S_IWUSR, show_temp, store_temp,
1635 SENSOR_ATTR_2(temp3_max, S_IRUGO | S_IWUSR, show_temp, store_temp,
1637 SENSOR_ATTR_2(temp4_max, S_IRUGO | S_IWUSR, show_temp, store_temp,
1639 SENSOR_ATTR_2(temp5_max, S_IRUGO | S_IWUSR, show_temp, store_temp,
1641 SENSOR_ATTR_2(temp6_max, S_IRUGO | S_IWUSR, show_temp, store_temp,
1643 SENSOR_ATTR_2(temp7_max, S_IRUGO | S_IWUSR, show_temp, store_temp,
1645 SENSOR_ATTR_2(temp8_max, S_IRUGO | S_IWUSR, show_temp, store_temp,
1647 SENSOR_ATTR_2(temp9_max, S_IRUGO | S_IWUSR, show_temp, store_temp,
1649 SENSOR_ATTR_2(temp10_max, S_IRUGO | S_IWUSR, show_temp, store_temp,
1653 static struct sensor_device_attribute_2 sda_temp_max_hyst[] = {
1654 SENSOR_ATTR_2(temp1_max_hyst, S_IRUGO | S_IWUSR, show_temp, store_temp,
1656 SENSOR_ATTR_2(temp2_max_hyst, S_IRUGO | S_IWUSR, show_temp, store_temp,
1658 SENSOR_ATTR_2(temp3_max_hyst, S_IRUGO | S_IWUSR, show_temp, store_temp,
1660 SENSOR_ATTR_2(temp4_max_hyst, S_IRUGO | S_IWUSR, show_temp, store_temp,
1662 SENSOR_ATTR_2(temp5_max_hyst, S_IRUGO | S_IWUSR, show_temp, store_temp,
1664 SENSOR_ATTR_2(temp6_max_hyst, S_IRUGO | S_IWUSR, show_temp, store_temp,
1666 SENSOR_ATTR_2(temp7_max_hyst, S_IRUGO | S_IWUSR, show_temp, store_temp,
1668 SENSOR_ATTR_2(temp8_max_hyst, S_IRUGO | S_IWUSR, show_temp, store_temp,
1670 SENSOR_ATTR_2(temp9_max_hyst, S_IRUGO | S_IWUSR, show_temp, store_temp,
1672 SENSOR_ATTR_2(temp10_max_hyst, S_IRUGO | S_IWUSR, show_temp, store_temp,
1676 static struct sensor_device_attribute_2 sda_temp_crit[] = {
1677 SENSOR_ATTR_2(temp1_crit, S_IRUGO | S_IWUSR, show_temp, store_temp,
1679 SENSOR_ATTR_2(temp2_crit, S_IRUGO | S_IWUSR, show_temp, store_temp,
1681 SENSOR_ATTR_2(temp3_crit, S_IRUGO | S_IWUSR, show_temp, store_temp,
1683 SENSOR_ATTR_2(temp4_crit, S_IRUGO | S_IWUSR, show_temp, store_temp,
1685 SENSOR_ATTR_2(temp5_crit, S_IRUGO | S_IWUSR, show_temp, store_temp,
1687 SENSOR_ATTR_2(temp6_crit, S_IRUGO | S_IWUSR, show_temp, store_temp,
1689 SENSOR_ATTR_2(temp7_crit, S_IRUGO | S_IWUSR, show_temp, store_temp,
1691 SENSOR_ATTR_2(temp8_crit, S_IRUGO | S_IWUSR, show_temp, store_temp,
1693 SENSOR_ATTR_2(temp9_crit, S_IRUGO | S_IWUSR, show_temp, store_temp,
1695 SENSOR_ATTR_2(temp10_crit, S_IRUGO | S_IWUSR, show_temp, store_temp,
1699 static struct sensor_device_attribute sda_temp_offset[] = {
1700 SENSOR_ATTR(temp1_offset, S_IRUGO | S_IWUSR, show_temp_offset,
1701 store_temp_offset, 0),
1702 SENSOR_ATTR(temp2_offset, S_IRUGO | S_IWUSR, show_temp_offset,
1703 store_temp_offset, 1),
1704 SENSOR_ATTR(temp3_offset, S_IRUGO | S_IWUSR, show_temp_offset,
1705 store_temp_offset, 2),
1706 SENSOR_ATTR(temp4_offset, S_IRUGO | S_IWUSR, show_temp_offset,
1707 store_temp_offset, 3),
1708 SENSOR_ATTR(temp5_offset, S_IRUGO | S_IWUSR, show_temp_offset,
1709 store_temp_offset, 4),
1710 SENSOR_ATTR(temp6_offset, S_IRUGO | S_IWUSR, show_temp_offset,
1711 store_temp_offset, 5),
1714 static struct sensor_device_attribute sda_temp_type[] = {
1715 SENSOR_ATTR(temp1_type, S_IRUGO | S_IWUSR, show_temp_type,
1716 store_temp_type, 0),
1717 SENSOR_ATTR(temp2_type, S_IRUGO | S_IWUSR, show_temp_type,
1718 store_temp_type, 1),
1719 SENSOR_ATTR(temp3_type, S_IRUGO | S_IWUSR, show_temp_type,
1720 store_temp_type, 2),
1721 SENSOR_ATTR(temp4_type, S_IRUGO | S_IWUSR, show_temp_type,
1722 store_temp_type, 3),
1723 SENSOR_ATTR(temp5_type, S_IRUGO | S_IWUSR, show_temp_type,
1724 store_temp_type, 4),
1725 SENSOR_ATTR(temp6_type, S_IRUGO | S_IWUSR, show_temp_type,
1726 store_temp_type, 5),
1729 static struct sensor_device_attribute sda_temp_alarm[] = {
1730 SENSOR_ATTR(temp1_alarm, S_IRUGO, show_alarm, NULL,
1732 SENSOR_ATTR(temp2_alarm, S_IRUGO, show_alarm, NULL,
1733 TEMP_ALARM_BASE + 1),
1734 SENSOR_ATTR(temp3_alarm, S_IRUGO, show_alarm, NULL,
1735 TEMP_ALARM_BASE + 2),
1736 SENSOR_ATTR(temp4_alarm, S_IRUGO, show_alarm, NULL,
1737 TEMP_ALARM_BASE + 3),
1738 SENSOR_ATTR(temp5_alarm, S_IRUGO, show_alarm, NULL,
1739 TEMP_ALARM_BASE + 4),
1740 SENSOR_ATTR(temp6_alarm, S_IRUGO, show_alarm, NULL,
1741 TEMP_ALARM_BASE + 5),
1744 #define NUM_TEMP_ALARM ARRAY_SIZE(sda_temp_alarm)
1747 show_pwm_mode(struct device *dev, struct device_attribute *attr, char *buf)
1749 struct nct6775_data *data = nct6775_update_device(dev);
1750 struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr);
1752 return sprintf(buf, "%d\n", !data->pwm_mode[sattr->index]);
1756 store_pwm_mode(struct device *dev, struct device_attribute *attr,
1757 const char *buf, size_t count)
1759 struct nct6775_data *data = dev_get_drvdata(dev);
1760 struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr);
1761 int nr = sattr->index;
1766 err = kstrtoul(buf, 10, &val);
1773 /* Setting DC mode is not supported for all chips/channels */
1774 if (data->REG_PWM_MODE[nr] == 0) {
1780 mutex_lock(&data->update_lock);
1781 data->pwm_mode[nr] = val;
1782 reg = nct6775_read_value(data, data->REG_PWM_MODE[nr]);
1783 reg &= ~data->PWM_MODE_MASK[nr];
1785 reg |= data->PWM_MODE_MASK[nr];
1786 nct6775_write_value(data, data->REG_PWM_MODE[nr], reg);
1787 mutex_unlock(&data->update_lock);
1792 show_pwm_enable(struct device *dev, struct device_attribute *attr, char *buf)
1794 struct nct6775_data *data = nct6775_update_device(dev);
1795 struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr);
1797 return sprintf(buf, "%d\n", data->pwm_enable[sattr->index]);
1801 show_pwm(struct device *dev, struct device_attribute *attr, char *buf)
1803 struct nct6775_data *data = nct6775_update_device(dev);
1804 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
1806 int index = sattr->index;
1810 * For automatic fan control modes, show current pwm readings.
1811 * Otherwise, show the configured value.
1813 if (index == 0 && data->pwm_enable[nr] > 1)
1814 pwm = nct6775_read_value(data, data->REG_PWM_READ[nr]);
1816 pwm = data->pwm[index][nr];
1818 return sprintf(buf, "%d\n", pwm);
1822 store_pwm(struct device *dev, struct device_attribute *attr, const char *buf,
1825 struct nct6775_data *data = dev_get_drvdata(dev);
1826 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
1828 int index = sattr->index;
1830 int minval[7] = { 0, 1, 1, data->pwm[2][nr], 0, 0, 0 };
1832 = { 255, 255, data->pwm[3][nr] ? : 255, 255, 255, 255, 255 };
1835 err = kstrtoul(buf, 10, &val);
1838 val = SENSORS_LIMIT(val, minval[index], maxval[index]);
1840 mutex_lock(&data->update_lock);
1841 data->pwm[index][nr] = val;
1842 nct6775_write_value(data, data->REG_PWM[index][nr], val);
1843 mutex_unlock(&data->update_lock);
1847 /* Returns 0 if OK, -EINVAL otherwise */
1848 static int check_trip_points(struct nct6775_data *data, int nr)
1852 for (i = 0; i < data->auto_pwm_num - 1; i++) {
1853 if (data->auto_temp[nr][i] > data->auto_temp[nr][i + 1])
1856 for (i = 0; i < data->auto_pwm_num - 1; i++) {
1857 if (data->auto_pwm[nr][i] > data->auto_pwm[nr][i + 1])
1860 /* validate critical temperature and pwm if enabled (pwm > 0) */
1861 if (data->auto_pwm[nr][data->auto_pwm_num]) {
1862 if (data->auto_temp[nr][data->auto_pwm_num - 1] >
1863 data->auto_temp[nr][data->auto_pwm_num] ||
1864 data->auto_pwm[nr][data->auto_pwm_num - 1] >
1865 data->auto_pwm[nr][data->auto_pwm_num])
1872 store_pwm_enable(struct device *dev, struct device_attribute *attr,
1873 const char *buf, size_t count)
1875 struct nct6775_data *data = dev_get_drvdata(dev);
1876 struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr);
1877 int nr = sattr->index;
1882 err = kstrtoul(buf, 10, &val);
1889 if (val == 4 && data->kind != nct6775)
1892 if (val == 5 && check_trip_points(data, nr)) {
1893 dev_err(dev, "Inconsistent trip points, not switching to SmartFan IV mode\n");
1894 dev_err(dev, "Adjust trip points and try again\n");
1898 mutex_lock(&data->update_lock);
1899 data->pwm_enable[nr] = val;
1902 * turn off pwm control: select manual mode, set pwm to maximum
1904 data->pwm[0][nr] = 255;
1905 nct6775_write_value(data, data->REG_PWM[0][nr], 255);
1907 reg = nct6775_read_value(data, data->REG_FAN_MODE[nr]);
1909 reg |= pwm_enable_to_reg(val) << 4;
1910 nct6775_write_value(data, data->REG_FAN_MODE[nr], reg);
1911 mutex_unlock(&data->update_lock);
1916 show_pwm_temp_sel(struct device *dev, struct device_attribute *attr, char *buf)
1918 struct nct6775_data *data = nct6775_update_device(dev);
1919 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
1920 int i, src, sel = 0;
1922 src = data->pwm_temp_sel[sattr->index][sattr->nr];
1924 for (i = 0; i < NUM_TEMP; i++) {
1925 if (!(data->have_temp & (1 << i)))
1927 if (src == data->temp_src[i]) {
1933 return sprintf(buf, "%d\n", sel);
1937 store_pwm_temp_sel(struct device *dev, struct device_attribute *attr,
1938 const char *buf, size_t count)
1940 struct nct6775_data *data = nct6775_update_device(dev);
1941 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
1943 int index = sattr->index;
1947 err = kstrtoul(buf, 10, &val);
1950 if (val == 0 || val > NUM_TEMP)
1952 if (!(data->have_temp & (1 << (val - 1))) || !data->temp_src[val - 1])
1955 mutex_lock(&data->update_lock);
1956 src = data->temp_src[val - 1];
1957 data->pwm_temp_sel[index][nr] = src;
1958 reg = nct6775_read_value(data, data->REG_TEMP_SEL[index][nr]);
1961 nct6775_write_value(data, data->REG_TEMP_SEL[index][nr], reg);
1962 mutex_unlock(&data->update_lock);
1968 show_target_temp(struct device *dev, struct device_attribute *attr, char *buf)
1970 struct nct6775_data *data = nct6775_update_device(dev);
1971 struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr);
1973 return sprintf(buf, "%d\n",
1974 data->target_temp[sattr->index] * 1000);
1978 store_target_temp(struct device *dev, struct device_attribute *attr,
1979 const char *buf, size_t count)
1981 struct nct6775_data *data = dev_get_drvdata(dev);
1982 struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr);
1983 int nr = sattr->index;
1987 err = kstrtol(buf, 10, &val);
1991 val = SENSORS_LIMIT(DIV_ROUND_CLOSEST(val, 1000), 0, 127);
1993 mutex_lock(&data->update_lock);
1994 data->target_temp[nr] = val;
1995 nct6775_write_value(data, data->REG_TARGET[nr], val);
1996 mutex_unlock(&data->update_lock);
2001 show_auto_temp_hyst(struct device *dev, struct device_attribute *attr,
2004 struct nct6775_data *data = nct6775_update_device(dev);
2005 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
2007 int point = sattr->index >= data->auto_pwm_num ? 1 : 0;
2008 int tolerance = data->tolerance[nr][point];
2009 int temp = data->auto_temp[nr][sattr->index];
2011 return sprintf(buf, "%d\n", (temp - tolerance) * 1000);
2015 store_auto_temp_hyst(struct device *dev, struct device_attribute *attr,
2016 const char *buf, size_t count)
2018 struct nct6775_data *data = dev_get_drvdata(dev);
2019 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
2021 int point = sattr->index >= data->auto_pwm_num ? 1 : 0;
2025 int maxlimit[2][3] = { { 15, 7, 63 }, { 15, 7, 7 } };
2026 int mask[] = { 0x0f, 0x07, 0x07 };
2029 err = kstrtol(buf, 10, &val);
2033 temp = data->auto_temp[nr][sattr->index];
2034 val = temp - DIV_ROUND_CLOSEST(val, 1000);
2036 /* Limit tolerance as needed */
2037 val = SENSORS_LIMIT(val, 0, maxlimit[point][data->kind]);
2039 mutex_lock(&data->update_lock);
2041 nct6775_write_value(data,
2042 data->REG_CRITICAL_TEMP_TOLERANCE[nr],
2045 reg = nct6775_read_value(data, data->REG_FAN_MODE[nr]);
2046 reg = (reg & ~mask[nr]) | (val & mask[nr]);
2047 nct6775_write_value(data, data->REG_FAN_MODE[nr], reg);
2048 if (data->kind == nct6779) {
2049 reg = nct6775_read_value(data,
2050 NCT6779_REG_TOLERANCE_H[nr]);
2051 reg = (reg & 0x70) | ((val & 0x38) << 1);
2052 nct6775_write_value(data,
2053 NCT6779_REG_TOLERANCE_H[nr], reg);
2057 data->tolerance[nr][point] = val;
2058 mutex_unlock(&data->update_lock);
2062 static SENSOR_DEVICE_ATTR_2(pwm1, S_IWUSR | S_IRUGO, show_pwm, store_pwm, 0, 0);
2063 static SENSOR_DEVICE_ATTR_2(pwm2, S_IWUSR | S_IRUGO, show_pwm, store_pwm, 1, 0);
2064 static SENSOR_DEVICE_ATTR_2(pwm3, S_IWUSR | S_IRUGO, show_pwm, store_pwm, 2, 0);
2065 static SENSOR_DEVICE_ATTR_2(pwm4, S_IWUSR | S_IRUGO, show_pwm, store_pwm, 3, 0);
2066 static SENSOR_DEVICE_ATTR_2(pwm5, S_IWUSR | S_IRUGO, show_pwm, store_pwm, 4, 0);
2068 static SENSOR_DEVICE_ATTR(pwm1_mode, S_IWUSR | S_IRUGO, show_pwm_mode,
2070 static SENSOR_DEVICE_ATTR(pwm2_mode, S_IWUSR | S_IRUGO, show_pwm_mode,
2072 static SENSOR_DEVICE_ATTR(pwm3_mode, S_IWUSR | S_IRUGO, show_pwm_mode,
2074 static SENSOR_DEVICE_ATTR(pwm4_mode, S_IWUSR | S_IRUGO, show_pwm_mode,
2076 static SENSOR_DEVICE_ATTR(pwm5_mode, S_IWUSR | S_IRUGO, show_pwm_mode,
2079 static SENSOR_DEVICE_ATTR(pwm1_enable, S_IWUSR | S_IRUGO, show_pwm_enable,
2080 store_pwm_enable, 0);
2081 static SENSOR_DEVICE_ATTR(pwm2_enable, S_IWUSR | S_IRUGO, show_pwm_enable,
2082 store_pwm_enable, 1);
2083 static SENSOR_DEVICE_ATTR(pwm3_enable, S_IWUSR | S_IRUGO, show_pwm_enable,
2084 store_pwm_enable, 2);
2085 static SENSOR_DEVICE_ATTR(pwm4_enable, S_IWUSR | S_IRUGO, show_pwm_enable,
2086 store_pwm_enable, 3);
2087 static SENSOR_DEVICE_ATTR(pwm5_enable, S_IWUSR | S_IRUGO, show_pwm_enable,
2088 store_pwm_enable, 4);
2090 static SENSOR_DEVICE_ATTR_2(pwm1_temp_sel, S_IWUSR | S_IRUGO,
2091 show_pwm_temp_sel, store_pwm_temp_sel, 0, 0);
2092 static SENSOR_DEVICE_ATTR_2(pwm2_temp_sel, S_IWUSR | S_IRUGO,
2093 show_pwm_temp_sel, store_pwm_temp_sel, 1, 0);
2094 static SENSOR_DEVICE_ATTR_2(pwm3_temp_sel, S_IWUSR | S_IRUGO,
2095 show_pwm_temp_sel, store_pwm_temp_sel, 2, 0);
2096 static SENSOR_DEVICE_ATTR_2(pwm4_temp_sel, S_IWUSR | S_IRUGO,
2097 show_pwm_temp_sel, store_pwm_temp_sel, 3, 0);
2098 static SENSOR_DEVICE_ATTR_2(pwm5_temp_sel, S_IWUSR | S_IRUGO,
2099 show_pwm_temp_sel, store_pwm_temp_sel, 4, 0);
2101 static SENSOR_DEVICE_ATTR(pwm1_target, S_IWUSR | S_IRUGO, show_target_temp,
2102 store_target_temp, 0);
2103 static SENSOR_DEVICE_ATTR(pwm2_target, S_IWUSR | S_IRUGO, show_target_temp,
2104 store_target_temp, 1);
2105 static SENSOR_DEVICE_ATTR(pwm3_target, S_IWUSR | S_IRUGO, show_target_temp,
2106 store_target_temp, 2);
2107 static SENSOR_DEVICE_ATTR(pwm4_target, S_IWUSR | S_IRUGO, show_target_temp,
2108 store_target_temp, 3);
2109 static SENSOR_DEVICE_ATTR(pwm5_target, S_IWUSR | S_IRUGO, show_target_temp,
2110 store_target_temp, 4);
2112 /* Smart Fan registers */
2115 show_pwm_sel_enable(struct device *dev, struct device_attribute *attr,
2118 struct nct6775_data *data = nct6775_update_device(dev);
2119 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
2121 return sprintf(buf, "%d\n",
2122 data->pwm_sel_enable[sattr->index][sattr->nr]);
2126 store_pwm_sel_enable(struct device *dev, struct device_attribute *attr,
2127 const char *buf, size_t count)
2129 struct nct6775_data *data = dev_get_drvdata(dev);
2130 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
2132 int index = sattr->index;
2136 static const u8 bit[] = { 0x80, 0x80, 0x01 };
2139 err = kstrtoul(buf, 10, &val);
2146 mutex_lock(&data->update_lock);
2147 data->pwm_sel_enable[index][nr] = val;
2148 reg = nct6775_read_value(data, data->REG_TEMP_SEL[index][nr]);
2152 nct6775_write_value(data, data->REG_TEMP_SEL[index][nr], reg);
2153 mutex_unlock(&data->update_lock);
2158 show_weight_temp(struct device *dev, struct device_attribute *attr, char *buf)
2160 struct nct6775_data *data = nct6775_update_device(dev);
2161 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
2163 int index = sattr->index;
2165 return sprintf(buf, "%d\n", data->weight_temp[index][nr] * 1000);
2169 store_weight_temp(struct device *dev, struct device_attribute *attr,
2170 const char *buf, size_t count)
2172 struct nct6775_data *data = dev_get_drvdata(dev);
2173 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
2175 int index = sattr->index;
2179 err = kstrtoul(buf, 10, &val);
2183 val = SENSORS_LIMIT(DIV_ROUND_CLOSEST(val, 1000), 0, 255);
2185 mutex_lock(&data->update_lock);
2186 data->weight_temp[index][nr] = val;
2187 nct6775_write_value(data, data->REG_WEIGHT_TEMP[index][nr], val);
2188 mutex_unlock(&data->update_lock);
2192 static SENSOR_DEVICE_ATTR_2(pwm1_stop_output_enable, S_IWUSR | S_IRUGO,
2193 show_pwm_sel_enable, store_pwm_sel_enable, 0, 0);
2194 static SENSOR_DEVICE_ATTR_2(pwm2_stop_output_enable, S_IWUSR | S_IRUGO,
2195 show_pwm_sel_enable, store_pwm_sel_enable, 1, 0);
2196 static SENSOR_DEVICE_ATTR_2(pwm3_stop_output_enable, S_IWUSR | S_IRUGO,
2197 show_pwm_sel_enable, store_pwm_sel_enable, 2, 0);
2198 static SENSOR_DEVICE_ATTR_2(pwm4_stop_output_enable, S_IWUSR | S_IRUGO,
2199 show_pwm_sel_enable, store_pwm_sel_enable, 3, 0);
2200 static SENSOR_DEVICE_ATTR_2(pwm5_stop_output_enable, S_IWUSR | S_IRUGO,
2201 show_pwm_sel_enable, store_pwm_sel_enable, 4, 0);
2203 static SENSOR_DEVICE_ATTR_2(pwm1_weight_enable, S_IWUSR | S_IRUGO,
2204 show_pwm_sel_enable, store_pwm_sel_enable, 0, 1);
2205 static SENSOR_DEVICE_ATTR_2(pwm2_weight_enable, S_IWUSR | S_IRUGO,
2206 show_pwm_sel_enable, store_pwm_sel_enable, 1, 1);
2207 static SENSOR_DEVICE_ATTR_2(pwm3_weight_enable, S_IWUSR | S_IRUGO,
2208 show_pwm_sel_enable, store_pwm_sel_enable, 2, 1);
2209 static SENSOR_DEVICE_ATTR_2(pwm4_weight_enable, S_IWUSR | S_IRUGO,
2210 show_pwm_sel_enable, store_pwm_sel_enable, 3, 1);
2211 static SENSOR_DEVICE_ATTR_2(pwm5_weight_enable, S_IWUSR | S_IRUGO,
2212 show_pwm_sel_enable, store_pwm_sel_enable, 4, 1);
2214 static SENSOR_DEVICE_ATTR_2(pwm1_step_enable, S_IWUSR | S_IRUGO,
2215 show_pwm_sel_enable, store_pwm_sel_enable, 0, 2);
2216 static SENSOR_DEVICE_ATTR_2(pwm2_step_enable, S_IWUSR | S_IRUGO,
2217 show_pwm_sel_enable, store_pwm_sel_enable, 1, 2);
2218 static SENSOR_DEVICE_ATTR_2(pwm3_step_enable, S_IWUSR | S_IRUGO,
2219 show_pwm_sel_enable, store_pwm_sel_enable, 2, 2);
2220 static SENSOR_DEVICE_ATTR_2(pwm4_step_enable, S_IWUSR | S_IRUGO,
2221 show_pwm_sel_enable, store_pwm_sel_enable, 3, 2);
2222 static SENSOR_DEVICE_ATTR_2(pwm5_step_enable, S_IWUSR | S_IRUGO,
2223 show_pwm_sel_enable, store_pwm_sel_enable, 4, 2);
2225 static SENSOR_DEVICE_ATTR_2(pwm1_weight_temp_sel, S_IWUSR | S_IRUGO,
2226 show_pwm_temp_sel, store_pwm_temp_sel, 0, 1);
2227 static SENSOR_DEVICE_ATTR_2(pwm2_weight_temp_sel, S_IWUSR | S_IRUGO,
2228 show_pwm_temp_sel, store_pwm_temp_sel, 1, 1);
2229 static SENSOR_DEVICE_ATTR_2(pwm3_weight_temp_sel, S_IWUSR | S_IRUGO,
2230 show_pwm_temp_sel, store_pwm_temp_sel, 2, 1);
2231 static SENSOR_DEVICE_ATTR_2(pwm4_weight_temp_sel, S_IWUSR | S_IRUGO,
2232 show_pwm_temp_sel, store_pwm_temp_sel, 3, 1);
2233 static SENSOR_DEVICE_ATTR_2(pwm5_weight_temp_sel, S_IWUSR | S_IRUGO,
2234 show_pwm_temp_sel, store_pwm_temp_sel, 4, 1);
2236 static SENSOR_DEVICE_ATTR_2(pwm1_weight_temp_step, S_IWUSR | S_IRUGO,
2237 show_weight_temp, store_weight_temp, 0, 0);
2238 static SENSOR_DEVICE_ATTR_2(pwm2_weight_temp_step, S_IWUSR | S_IRUGO,
2239 show_weight_temp, store_weight_temp, 1, 0);
2240 static SENSOR_DEVICE_ATTR_2(pwm3_weight_temp_step, S_IWUSR | S_IRUGO,
2241 show_weight_temp, store_weight_temp, 2, 0);
2242 static SENSOR_DEVICE_ATTR_2(pwm4_weight_temp_step, S_IWUSR | S_IRUGO,
2243 show_weight_temp, store_weight_temp, 3, 0);
2244 static SENSOR_DEVICE_ATTR_2(pwm5_weight_temp_step, S_IWUSR | S_IRUGO,
2245 show_weight_temp, store_weight_temp, 4, 0);
2247 static SENSOR_DEVICE_ATTR_2(pwm1_weight_temp_step_tol, S_IWUSR | S_IRUGO,
2248 show_weight_temp, store_weight_temp, 0, 1);
2249 static SENSOR_DEVICE_ATTR_2(pwm2_weight_temp_step_tol, S_IWUSR | S_IRUGO,
2250 show_weight_temp, store_weight_temp, 1, 1);
2251 static SENSOR_DEVICE_ATTR_2(pwm3_weight_temp_step_tol, S_IWUSR | S_IRUGO,
2252 show_weight_temp, store_weight_temp, 2, 1);
2253 static SENSOR_DEVICE_ATTR_2(pwm4_weight_temp_step_tol, S_IWUSR | S_IRUGO,
2254 show_weight_temp, store_weight_temp, 3, 1);
2255 static SENSOR_DEVICE_ATTR_2(pwm5_weight_temp_step_tol, S_IWUSR | S_IRUGO,
2256 show_weight_temp, store_weight_temp, 4, 1);
2258 static SENSOR_DEVICE_ATTR_2(pwm1_weight_temp_step_base, S_IWUSR | S_IRUGO,
2259 show_weight_temp, store_weight_temp, 0, 2);
2260 static SENSOR_DEVICE_ATTR_2(pwm2_weight_temp_step_base, S_IWUSR | S_IRUGO,
2261 show_weight_temp, store_weight_temp, 1, 2);
2262 static SENSOR_DEVICE_ATTR_2(pwm3_weight_temp_step_base, S_IWUSR | S_IRUGO,
2263 show_weight_temp, store_weight_temp, 2, 2);
2264 static SENSOR_DEVICE_ATTR_2(pwm4_weight_temp_step_base, S_IWUSR | S_IRUGO,
2265 show_weight_temp, store_weight_temp, 3, 2);
2266 static SENSOR_DEVICE_ATTR_2(pwm5_weight_temp_step_base, S_IWUSR | S_IRUGO,
2267 show_weight_temp, store_weight_temp, 4, 2);
2269 static SENSOR_DEVICE_ATTR_2(pwm1_weight_duty_step, S_IWUSR | S_IRUGO,
2270 show_pwm, store_pwm, 0, 5);
2271 static SENSOR_DEVICE_ATTR_2(pwm2_weight_duty_step, S_IWUSR | S_IRUGO,
2272 show_pwm, store_pwm, 1, 5);
2273 static SENSOR_DEVICE_ATTR_2(pwm3_weight_duty_step, S_IWUSR | S_IRUGO,
2274 show_pwm, store_pwm, 2, 5);
2275 static SENSOR_DEVICE_ATTR_2(pwm4_weight_duty_step, S_IWUSR | S_IRUGO,
2276 show_pwm, store_pwm, 3, 5);
2277 static SENSOR_DEVICE_ATTR_2(pwm5_weight_duty_step, S_IWUSR | S_IRUGO,
2278 show_pwm, store_pwm, 4, 5);
2280 /* duty_base is not supported on all chips */
2281 static struct sensor_device_attribute_2 sda_weight_duty_base[] = {
2282 SENSOR_ATTR_2(pwm1_weight_duty_base, S_IWUSR | S_IRUGO,
2283 show_pwm, store_pwm, 0, 6),
2284 SENSOR_ATTR_2(pwm2_weight_duty_base, S_IWUSR | S_IRUGO,
2285 show_pwm, store_pwm, 1, 6),
2286 SENSOR_ATTR_2(pwm3_weight_duty_base, S_IWUSR | S_IRUGO,
2287 show_pwm, store_pwm, 2, 6),
2288 SENSOR_ATTR_2(pwm4_weight_duty_base, S_IWUSR | S_IRUGO,
2289 show_pwm, store_pwm, 3, 6),
2290 SENSOR_ATTR_2(pwm5_weight_duty_base, S_IWUSR | S_IRUGO,
2291 show_pwm, store_pwm, 4, 6),
2295 show_fan_time(struct device *dev, struct device_attribute *attr, char *buf)
2297 struct nct6775_data *data = nct6775_update_device(dev);
2298 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
2300 int index = sattr->index;
2302 return sprintf(buf, "%d\n",
2303 step_time_from_reg(data->fan_time[index][nr],
2304 data->pwm_mode[nr]));
2308 store_fan_time(struct device *dev, struct device_attribute *attr,
2309 const char *buf, size_t count)
2311 struct nct6775_data *data = dev_get_drvdata(dev);
2312 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
2314 int index = sattr->index;
2318 err = kstrtoul(buf, 10, &val);
2322 val = step_time_to_reg(val, data->pwm_mode[nr]);
2323 mutex_lock(&data->update_lock);
2324 data->fan_time[index][nr] = val;
2325 nct6775_write_value(data, data->REG_FAN_TIME[index][nr], val);
2326 mutex_unlock(&data->update_lock);
2331 show_name(struct device *dev, struct device_attribute *attr, char *buf)
2333 struct nct6775_data *data = dev_get_drvdata(dev);
2335 return sprintf(buf, "%s\n", data->name);
2338 static DEVICE_ATTR(name, S_IRUGO, show_name, NULL);
2340 static SENSOR_DEVICE_ATTR_2(pwm1_stop_time, S_IWUSR | S_IRUGO, show_fan_time,
2341 store_fan_time, 0, 0);
2342 static SENSOR_DEVICE_ATTR_2(pwm2_stop_time, S_IWUSR | S_IRUGO, show_fan_time,
2343 store_fan_time, 1, 0);
2344 static SENSOR_DEVICE_ATTR_2(pwm3_stop_time, S_IWUSR | S_IRUGO, show_fan_time,
2345 store_fan_time, 2, 0);
2346 static SENSOR_DEVICE_ATTR_2(pwm4_stop_time, S_IWUSR | S_IRUGO, show_fan_time,
2347 store_fan_time, 3, 0);
2348 static SENSOR_DEVICE_ATTR_2(pwm5_stop_time, S_IWUSR | S_IRUGO, show_fan_time,
2349 store_fan_time, 4, 0);
2351 static SENSOR_DEVICE_ATTR_2(pwm1_step_up_time, S_IWUSR | S_IRUGO, show_fan_time,
2352 store_fan_time, 0, 1);
2353 static SENSOR_DEVICE_ATTR_2(pwm2_step_up_time, S_IWUSR | S_IRUGO, show_fan_time,
2354 store_fan_time, 1, 1);
2355 static SENSOR_DEVICE_ATTR_2(pwm3_step_up_time, S_IWUSR | S_IRUGO, show_fan_time,
2356 store_fan_time, 2, 1);
2357 static SENSOR_DEVICE_ATTR_2(pwm4_step_up_time, S_IWUSR | S_IRUGO, show_fan_time,
2358 store_fan_time, 3, 1);
2359 static SENSOR_DEVICE_ATTR_2(pwm5_step_up_time, S_IWUSR | S_IRUGO, show_fan_time,
2360 store_fan_time, 4, 1);
2362 static SENSOR_DEVICE_ATTR_2(pwm1_step_down_time, S_IWUSR | S_IRUGO,
2363 show_fan_time, store_fan_time, 0, 2);
2364 static SENSOR_DEVICE_ATTR_2(pwm2_step_down_time, S_IWUSR | S_IRUGO,
2365 show_fan_time, store_fan_time, 1, 2);
2366 static SENSOR_DEVICE_ATTR_2(pwm3_step_down_time, S_IWUSR | S_IRUGO,
2367 show_fan_time, store_fan_time, 2, 2);
2368 static SENSOR_DEVICE_ATTR_2(pwm4_step_down_time, S_IWUSR | S_IRUGO,
2369 show_fan_time, store_fan_time, 3, 2);
2370 static SENSOR_DEVICE_ATTR_2(pwm5_step_down_time, S_IWUSR | S_IRUGO,
2371 show_fan_time, store_fan_time, 4, 2);
2373 static SENSOR_DEVICE_ATTR_2(pwm1_start_output, S_IWUSR | S_IRUGO, show_pwm,
2375 static SENSOR_DEVICE_ATTR_2(pwm2_start_output, S_IWUSR | S_IRUGO, show_pwm,
2377 static SENSOR_DEVICE_ATTR_2(pwm3_start_output, S_IWUSR | S_IRUGO, show_pwm,
2379 static SENSOR_DEVICE_ATTR_2(pwm4_start_output, S_IWUSR | S_IRUGO, show_pwm,
2381 static SENSOR_DEVICE_ATTR_2(pwm5_start_output, S_IWUSR | S_IRUGO, show_pwm,
2384 static SENSOR_DEVICE_ATTR_2(pwm1_stop_output, S_IWUSR | S_IRUGO, show_pwm,
2386 static SENSOR_DEVICE_ATTR_2(pwm2_stop_output, S_IWUSR | S_IRUGO, show_pwm,
2388 static SENSOR_DEVICE_ATTR_2(pwm3_stop_output, S_IWUSR | S_IRUGO, show_pwm,
2390 static SENSOR_DEVICE_ATTR_2(pwm4_stop_output, S_IWUSR | S_IRUGO, show_pwm,
2392 static SENSOR_DEVICE_ATTR_2(pwm5_stop_output, S_IWUSR | S_IRUGO, show_pwm,
2395 /* max_output is not supported on all chips */
2396 static struct sensor_device_attribute_2 sda_max_output[] = {
2397 SENSOR_ATTR_2(pwm1_max_output, S_IWUSR | S_IRUGO, show_pwm, store_pwm,
2399 SENSOR_ATTR_2(pwm2_max_output, S_IWUSR | S_IRUGO, show_pwm, store_pwm,
2401 SENSOR_ATTR_2(pwm3_max_output, S_IWUSR | S_IRUGO, show_pwm, store_pwm,
2403 SENSOR_ATTR_2(pwm4_max_output, S_IWUSR | S_IRUGO, show_pwm, store_pwm,
2405 SENSOR_ATTR_2(pwm5_max_output, S_IWUSR | S_IRUGO, show_pwm, store_pwm,
2409 /* step_output is not supported on all chips */
2410 static struct sensor_device_attribute_2 sda_step_output[] = {
2411 SENSOR_ATTR_2(pwm1_step_output, S_IWUSR | S_IRUGO, show_pwm, store_pwm,
2413 SENSOR_ATTR_2(pwm2_step_output, S_IWUSR | S_IRUGO, show_pwm, store_pwm,
2415 SENSOR_ATTR_2(pwm3_step_output, S_IWUSR | S_IRUGO, show_pwm, store_pwm,
2417 SENSOR_ATTR_2(pwm4_step_output, S_IWUSR | S_IRUGO, show_pwm, store_pwm,
2419 SENSOR_ATTR_2(pwm5_step_output, S_IWUSR | S_IRUGO, show_pwm, store_pwm,
2423 static struct attribute *nct6775_attributes_pwm[5][20] = {
2425 &sensor_dev_attr_pwm1.dev_attr.attr,
2426 &sensor_dev_attr_pwm1_mode.dev_attr.attr,
2427 &sensor_dev_attr_pwm1_enable.dev_attr.attr,
2428 &sensor_dev_attr_pwm1_stop_output_enable.dev_attr.attr,
2429 &sensor_dev_attr_pwm1_weight_enable.dev_attr.attr,
2430 &sensor_dev_attr_pwm1_step_enable.dev_attr.attr,
2431 &sensor_dev_attr_pwm1_temp_sel.dev_attr.attr,
2432 &sensor_dev_attr_pwm1_target.dev_attr.attr,
2433 &sensor_dev_attr_pwm1_stop_time.dev_attr.attr,
2434 &sensor_dev_attr_pwm1_step_up_time.dev_attr.attr,
2435 &sensor_dev_attr_pwm1_step_down_time.dev_attr.attr,
2436 &sensor_dev_attr_pwm1_start_output.dev_attr.attr,
2437 &sensor_dev_attr_pwm1_stop_output.dev_attr.attr,
2438 &sensor_dev_attr_pwm1_weight_temp_sel.dev_attr.attr,
2439 &sensor_dev_attr_pwm1_weight_temp_step.dev_attr.attr,
2440 &sensor_dev_attr_pwm1_weight_temp_step_tol.dev_attr.attr,
2441 &sensor_dev_attr_pwm1_weight_temp_step_base.dev_attr.attr,
2442 &sensor_dev_attr_pwm1_weight_duty_step.dev_attr.attr,
2446 &sensor_dev_attr_pwm2.dev_attr.attr,
2447 &sensor_dev_attr_pwm2_mode.dev_attr.attr,
2448 &sensor_dev_attr_pwm2_enable.dev_attr.attr,
2449 &sensor_dev_attr_pwm2_stop_output_enable.dev_attr.attr,
2450 &sensor_dev_attr_pwm2_weight_enable.dev_attr.attr,
2451 &sensor_dev_attr_pwm2_step_enable.dev_attr.attr,
2452 &sensor_dev_attr_pwm2_temp_sel.dev_attr.attr,
2453 &sensor_dev_attr_pwm2_target.dev_attr.attr,
2454 &sensor_dev_attr_pwm2_stop_time.dev_attr.attr,
2455 &sensor_dev_attr_pwm2_step_up_time.dev_attr.attr,
2456 &sensor_dev_attr_pwm2_step_down_time.dev_attr.attr,
2457 &sensor_dev_attr_pwm2_start_output.dev_attr.attr,
2458 &sensor_dev_attr_pwm2_stop_output.dev_attr.attr,
2459 &sensor_dev_attr_pwm2_weight_temp_sel.dev_attr.attr,
2460 &sensor_dev_attr_pwm2_weight_temp_step.dev_attr.attr,
2461 &sensor_dev_attr_pwm2_weight_temp_step_tol.dev_attr.attr,
2462 &sensor_dev_attr_pwm2_weight_temp_step_base.dev_attr.attr,
2463 &sensor_dev_attr_pwm2_weight_duty_step.dev_attr.attr,
2467 &sensor_dev_attr_pwm3.dev_attr.attr,
2468 &sensor_dev_attr_pwm3_mode.dev_attr.attr,
2469 &sensor_dev_attr_pwm3_enable.dev_attr.attr,
2470 &sensor_dev_attr_pwm3_stop_output_enable.dev_attr.attr,
2471 &sensor_dev_attr_pwm3_weight_enable.dev_attr.attr,
2472 &sensor_dev_attr_pwm3_step_enable.dev_attr.attr,
2473 &sensor_dev_attr_pwm3_temp_sel.dev_attr.attr,
2474 &sensor_dev_attr_pwm3_target.dev_attr.attr,
2475 &sensor_dev_attr_pwm3_stop_time.dev_attr.attr,
2476 &sensor_dev_attr_pwm3_step_up_time.dev_attr.attr,
2477 &sensor_dev_attr_pwm3_step_down_time.dev_attr.attr,
2478 &sensor_dev_attr_pwm3_start_output.dev_attr.attr,
2479 &sensor_dev_attr_pwm3_stop_output.dev_attr.attr,
2480 &sensor_dev_attr_pwm3_weight_temp_sel.dev_attr.attr,
2481 &sensor_dev_attr_pwm3_weight_temp_step.dev_attr.attr,
2482 &sensor_dev_attr_pwm3_weight_temp_step_tol.dev_attr.attr,
2483 &sensor_dev_attr_pwm3_weight_temp_step_base.dev_attr.attr,
2484 &sensor_dev_attr_pwm3_weight_duty_step.dev_attr.attr,
2488 &sensor_dev_attr_pwm4.dev_attr.attr,
2489 &sensor_dev_attr_pwm4_mode.dev_attr.attr,
2490 &sensor_dev_attr_pwm4_enable.dev_attr.attr,
2491 &sensor_dev_attr_pwm4_stop_output_enable.dev_attr.attr,
2492 &sensor_dev_attr_pwm4_weight_enable.dev_attr.attr,
2493 &sensor_dev_attr_pwm4_step_enable.dev_attr.attr,
2494 &sensor_dev_attr_pwm4_temp_sel.dev_attr.attr,
2495 &sensor_dev_attr_pwm4_target.dev_attr.attr,
2496 &sensor_dev_attr_pwm4_stop_time.dev_attr.attr,
2497 &sensor_dev_attr_pwm4_step_up_time.dev_attr.attr,
2498 &sensor_dev_attr_pwm4_step_down_time.dev_attr.attr,
2499 &sensor_dev_attr_pwm4_start_output.dev_attr.attr,
2500 &sensor_dev_attr_pwm4_stop_output.dev_attr.attr,
2501 &sensor_dev_attr_pwm4_weight_temp_sel.dev_attr.attr,
2502 &sensor_dev_attr_pwm4_weight_temp_step.dev_attr.attr,
2503 &sensor_dev_attr_pwm4_weight_temp_step_tol.dev_attr.attr,
2504 &sensor_dev_attr_pwm4_weight_temp_step_base.dev_attr.attr,
2505 &sensor_dev_attr_pwm4_weight_duty_step.dev_attr.attr,
2509 &sensor_dev_attr_pwm5.dev_attr.attr,
2510 &sensor_dev_attr_pwm5_mode.dev_attr.attr,
2511 &sensor_dev_attr_pwm5_enable.dev_attr.attr,
2512 &sensor_dev_attr_pwm5_stop_output_enable.dev_attr.attr,
2513 &sensor_dev_attr_pwm5_weight_enable.dev_attr.attr,
2514 &sensor_dev_attr_pwm5_step_enable.dev_attr.attr,
2515 &sensor_dev_attr_pwm5_temp_sel.dev_attr.attr,
2516 &sensor_dev_attr_pwm5_target.dev_attr.attr,
2517 &sensor_dev_attr_pwm5_stop_time.dev_attr.attr,
2518 &sensor_dev_attr_pwm5_step_up_time.dev_attr.attr,
2519 &sensor_dev_attr_pwm5_step_down_time.dev_attr.attr,
2520 &sensor_dev_attr_pwm5_start_output.dev_attr.attr,
2521 &sensor_dev_attr_pwm5_stop_output.dev_attr.attr,
2522 &sensor_dev_attr_pwm5_weight_temp_sel.dev_attr.attr,
2523 &sensor_dev_attr_pwm5_weight_temp_step.dev_attr.attr,
2524 &sensor_dev_attr_pwm5_weight_temp_step_tol.dev_attr.attr,
2525 &sensor_dev_attr_pwm5_weight_temp_step_base.dev_attr.attr,
2526 &sensor_dev_attr_pwm5_weight_duty_step.dev_attr.attr,
2531 static const struct attribute_group nct6775_group_pwm[5] = {
2532 { .attrs = nct6775_attributes_pwm[0] },
2533 { .attrs = nct6775_attributes_pwm[1] },
2534 { .attrs = nct6775_attributes_pwm[2] },
2535 { .attrs = nct6775_attributes_pwm[3] },
2536 { .attrs = nct6775_attributes_pwm[4] },
2540 show_auto_pwm(struct device *dev, struct device_attribute *attr, char *buf)
2542 struct nct6775_data *data = nct6775_update_device(dev);
2543 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
2545 return sprintf(buf, "%d\n", data->auto_pwm[sattr->nr][sattr->index]);
2549 store_auto_pwm(struct device *dev, struct device_attribute *attr,
2550 const char *buf, size_t count)
2552 struct nct6775_data *data = dev_get_drvdata(dev);
2553 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
2555 int point = sattr->index;
2560 err = kstrtoul(buf, 10, &val);
2566 if (point == data->auto_pwm_num) {
2567 if (data->kind != nct6775 && !val)
2569 if (data->kind != nct6779 && val)
2573 mutex_lock(&data->update_lock);
2574 data->auto_pwm[nr][point] = val;
2575 if (point < data->auto_pwm_num) {
2576 nct6775_write_value(data,
2577 NCT6775_AUTO_PWM(data, nr, point),
2578 data->auto_pwm[nr][point]);
2580 switch (data->kind) {
2582 /* disable if needed (pwm == 0) */
2583 reg = nct6775_read_value(data,
2584 NCT6775_REG_CRITICAL_ENAB[nr]);
2589 nct6775_write_value(data, NCT6775_REG_CRITICAL_ENAB[nr],
2593 break; /* always enabled, nothing to do */
2595 nct6775_write_value(data, NCT6779_REG_CRITICAL_PWM[nr],
2597 reg = nct6775_read_value(data,
2598 NCT6779_REG_CRITICAL_PWM_ENABLE[nr]);
2603 nct6775_write_value(data,
2604 NCT6779_REG_CRITICAL_PWM_ENABLE[nr],
2609 mutex_unlock(&data->update_lock);
2614 show_auto_temp(struct device *dev, struct device_attribute *attr, char *buf)
2616 struct nct6775_data *data = nct6775_update_device(dev);
2617 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
2619 int point = sattr->index;
2622 * We don't know for sure if the temperature is signed or unsigned.
2623 * Assume it is unsigned.
2625 return sprintf(buf, "%d\n", data->auto_temp[nr][point] * 1000);
2629 store_auto_temp(struct device *dev, struct device_attribute *attr,
2630 const char *buf, size_t count)
2632 struct nct6775_data *data = dev_get_drvdata(dev);
2633 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
2635 int point = sattr->index;
2639 err = kstrtoul(buf, 10, &val);
2645 mutex_lock(&data->update_lock);
2646 data->auto_temp[nr][point] = DIV_ROUND_CLOSEST(val, 1000);
2647 if (point < data->auto_pwm_num) {
2648 nct6775_write_value(data,
2649 NCT6775_AUTO_TEMP(data, nr, point),
2650 data->auto_temp[nr][point]);
2652 nct6775_write_value(data, data->REG_CRITICAL_TEMP[nr],
2653 data->auto_temp[nr][point]);
2655 mutex_unlock(&data->update_lock);
2660 * The number of auto-point trip points is chip dependent.
2661 * Need to check support while generating/removing attribute files.
2663 static struct sensor_device_attribute_2 sda_auto_pwm_arrays[] = {
2664 SENSOR_ATTR_2(pwm1_auto_point1_pwm, S_IWUSR | S_IRUGO,
2665 show_auto_pwm, store_auto_pwm, 0, 0),
2666 SENSOR_ATTR_2(pwm1_auto_point1_temp, S_IWUSR | S_IRUGO,
2667 show_auto_temp, store_auto_temp, 0, 0),
2668 SENSOR_ATTR_2(pwm1_auto_point1_temp_hyst, S_IWUSR | S_IRUGO,
2669 show_auto_temp_hyst, store_auto_temp_hyst, 0, 0),
2670 SENSOR_ATTR_2(pwm1_auto_point2_pwm, S_IWUSR | S_IRUGO,
2671 show_auto_pwm, store_auto_pwm, 0, 1),
2672 SENSOR_ATTR_2(pwm1_auto_point2_temp, S_IWUSR | S_IRUGO,
2673 show_auto_temp, store_auto_temp, 0, 1),
2674 SENSOR_ATTR_2(pwm1_auto_point2_temp_hyst, S_IWUSR | S_IRUGO,
2675 show_auto_temp_hyst, store_auto_temp_hyst, 0, 1),
2676 SENSOR_ATTR_2(pwm1_auto_point3_pwm, S_IWUSR | S_IRUGO,
2677 show_auto_pwm, store_auto_pwm, 0, 2),
2678 SENSOR_ATTR_2(pwm1_auto_point3_temp, S_IWUSR | S_IRUGO,
2679 show_auto_temp, store_auto_temp, 0, 2),
2680 SENSOR_ATTR_2(pwm1_auto_point3_temp_hyst, S_IWUSR | S_IRUGO,
2681 show_auto_temp_hyst, store_auto_temp_hyst, 0, 2),
2682 SENSOR_ATTR_2(pwm1_auto_point4_pwm, S_IWUSR | S_IRUGO,
2683 show_auto_pwm, store_auto_pwm, 0, 3),
2684 SENSOR_ATTR_2(pwm1_auto_point4_temp, S_IWUSR | S_IRUGO,
2685 show_auto_temp, store_auto_temp, 0, 3),
2686 SENSOR_ATTR_2(pwm1_auto_point4_temp_hyst, S_IWUSR | S_IRUGO,
2687 show_auto_temp_hyst, store_auto_temp_hyst, 0, 3),
2688 SENSOR_ATTR_2(pwm1_auto_point5_pwm, S_IWUSR | S_IRUGO,
2689 show_auto_pwm, store_auto_pwm, 0, 4),
2690 SENSOR_ATTR_2(pwm1_auto_point5_temp, S_IWUSR | S_IRUGO,
2691 show_auto_temp, store_auto_temp, 0, 4),
2692 SENSOR_ATTR_2(pwm1_auto_point5_temp_hyst, S_IWUSR | S_IRUGO,
2693 show_auto_temp_hyst, store_auto_temp_hyst, 0, 4),
2694 SENSOR_ATTR_2(pwm1_auto_point6_pwm, S_IWUSR | S_IRUGO,
2695 show_auto_pwm, store_auto_pwm, 0, 5),
2696 SENSOR_ATTR_2(pwm1_auto_point6_temp, S_IWUSR | S_IRUGO,
2697 show_auto_temp, store_auto_temp, 0, 5),
2698 SENSOR_ATTR_2(pwm1_auto_point6_temp_hyst, S_IWUSR | S_IRUGO,
2699 show_auto_temp_hyst, store_auto_temp_hyst, 0, 5),
2700 SENSOR_ATTR_2(pwm1_auto_point7_pwm, S_IWUSR | S_IRUGO,
2701 show_auto_pwm, store_auto_pwm, 0, 6),
2702 SENSOR_ATTR_2(pwm1_auto_point7_temp, S_IWUSR | S_IRUGO,
2703 show_auto_temp, store_auto_temp, 0, 6),
2704 SENSOR_ATTR_2(pwm1_auto_point7_temp_hyst, S_IWUSR | S_IRUGO,
2705 show_auto_temp_hyst, store_auto_temp_hyst, 0, 6),
2707 SENSOR_ATTR_2(pwm2_auto_point1_pwm, S_IWUSR | S_IRUGO,
2708 show_auto_pwm, store_auto_pwm, 1, 0),
2709 SENSOR_ATTR_2(pwm2_auto_point1_temp, S_IWUSR | S_IRUGO,
2710 show_auto_temp, store_auto_temp, 1, 0),
2711 SENSOR_ATTR_2(pwm2_auto_point1_temp_hyst, S_IWUSR | S_IRUGO,
2712 show_auto_temp_hyst, store_auto_temp_hyst, 1, 0),
2713 SENSOR_ATTR_2(pwm2_auto_point2_pwm, S_IWUSR | S_IRUGO,
2714 show_auto_pwm, store_auto_pwm, 1, 1),
2715 SENSOR_ATTR_2(pwm2_auto_point2_temp, S_IWUSR | S_IRUGO,
2716 show_auto_temp, store_auto_temp, 1, 1),
2717 SENSOR_ATTR_2(pwm2_auto_point2_temp_hyst, S_IWUSR | S_IRUGO,
2718 show_auto_temp_hyst, store_auto_temp_hyst, 1, 1),
2719 SENSOR_ATTR_2(pwm2_auto_point3_pwm, S_IWUSR | S_IRUGO,
2720 show_auto_pwm, store_auto_pwm, 1, 2),
2721 SENSOR_ATTR_2(pwm2_auto_point3_temp, S_IWUSR | S_IRUGO,
2722 show_auto_temp, store_auto_temp, 1, 2),
2723 SENSOR_ATTR_2(pwm2_auto_point3_temp_hyst, S_IWUSR | S_IRUGO,
2724 show_auto_temp_hyst, store_auto_temp_hyst, 1, 2),
2725 SENSOR_ATTR_2(pwm2_auto_point4_pwm, S_IWUSR | S_IRUGO,
2726 show_auto_pwm, store_auto_pwm, 1, 3),
2727 SENSOR_ATTR_2(pwm2_auto_point4_temp, S_IWUSR | S_IRUGO,
2728 show_auto_temp, store_auto_temp, 1, 3),
2729 SENSOR_ATTR_2(pwm2_auto_point4_temp_hyst, S_IWUSR | S_IRUGO,
2730 show_auto_temp_hyst, store_auto_temp_hyst, 1, 3),
2731 SENSOR_ATTR_2(pwm2_auto_point5_pwm, S_IWUSR | S_IRUGO,
2732 show_auto_pwm, store_auto_pwm, 1, 4),
2733 SENSOR_ATTR_2(pwm2_auto_point5_temp, S_IWUSR | S_IRUGO,
2734 show_auto_temp, store_auto_temp, 1, 4),
2735 SENSOR_ATTR_2(pwm2_auto_point5_temp_hyst, S_IWUSR | S_IRUGO,
2736 show_auto_temp_hyst, store_auto_temp_hyst, 1, 4),
2737 SENSOR_ATTR_2(pwm2_auto_point6_pwm, S_IWUSR | S_IRUGO,
2738 show_auto_pwm, store_auto_pwm, 1, 5),
2739 SENSOR_ATTR_2(pwm2_auto_point6_temp, S_IWUSR | S_IRUGO,
2740 show_auto_temp, store_auto_temp, 1, 5),
2741 SENSOR_ATTR_2(pwm2_auto_point6_temp_hyst, S_IWUSR | S_IRUGO,
2742 show_auto_temp_hyst, store_auto_temp_hyst, 1, 5),
2743 SENSOR_ATTR_2(pwm2_auto_point7_pwm, S_IWUSR | S_IRUGO,
2744 show_auto_pwm, store_auto_pwm, 1, 6),
2745 SENSOR_ATTR_2(pwm2_auto_point7_temp, S_IWUSR | S_IRUGO,
2746 show_auto_temp, store_auto_temp, 1, 6),
2747 SENSOR_ATTR_2(pwm2_auto_point7_temp_hyst, S_IWUSR | S_IRUGO,
2748 show_auto_temp_hyst, store_auto_temp_hyst, 1, 6),
2750 SENSOR_ATTR_2(pwm3_auto_point1_pwm, S_IWUSR | S_IRUGO,
2751 show_auto_pwm, store_auto_pwm, 2, 0),
2752 SENSOR_ATTR_2(pwm3_auto_point1_temp, S_IWUSR | S_IRUGO,
2753 show_auto_temp, store_auto_temp, 2, 0),
2754 SENSOR_ATTR_2(pwm3_auto_point1_temp_hyst, S_IWUSR | S_IRUGO,
2755 show_auto_temp_hyst, store_auto_temp_hyst, 2, 0),
2756 SENSOR_ATTR_2(pwm3_auto_point2_pwm, S_IWUSR | S_IRUGO,
2757 show_auto_pwm, store_auto_pwm, 2, 1),
2758 SENSOR_ATTR_2(pwm3_auto_point2_temp, S_IWUSR | S_IRUGO,
2759 show_auto_temp, store_auto_temp, 2, 1),
2760 SENSOR_ATTR_2(pwm3_auto_point2_temp_hyst, S_IWUSR | S_IRUGO,
2761 show_auto_temp_hyst, store_auto_temp_hyst, 2, 1),
2762 SENSOR_ATTR_2(pwm3_auto_point3_pwm, S_IWUSR | S_IRUGO,
2763 show_auto_pwm, store_auto_pwm, 2, 2),
2764 SENSOR_ATTR_2(pwm3_auto_point3_temp, S_IWUSR | S_IRUGO,
2765 show_auto_temp, store_auto_temp, 2, 2),
2766 SENSOR_ATTR_2(pwm3_auto_point3_temp_hyst, S_IWUSR | S_IRUGO,
2767 show_auto_temp_hyst, store_auto_temp_hyst, 2, 2),
2768 SENSOR_ATTR_2(pwm3_auto_point4_pwm, S_IWUSR | S_IRUGO,
2769 show_auto_pwm, store_auto_pwm, 2, 3),
2770 SENSOR_ATTR_2(pwm3_auto_point4_temp, S_IWUSR | S_IRUGO,
2771 show_auto_temp, store_auto_temp, 2, 3),
2772 SENSOR_ATTR_2(pwm3_auto_point4_temp_hyst, S_IWUSR | S_IRUGO,
2773 show_auto_temp_hyst, store_auto_temp_hyst, 2, 3),
2774 SENSOR_ATTR_2(pwm3_auto_point5_pwm, S_IWUSR | S_IRUGO,
2775 show_auto_pwm, store_auto_pwm, 2, 4),
2776 SENSOR_ATTR_2(pwm3_auto_point5_temp, S_IWUSR | S_IRUGO,
2777 show_auto_temp, store_auto_temp, 2, 4),
2778 SENSOR_ATTR_2(pwm3_auto_point5_temp_hyst, S_IWUSR | S_IRUGO,
2779 show_auto_temp_hyst, store_auto_temp_hyst, 2, 4),
2780 SENSOR_ATTR_2(pwm3_auto_point6_pwm, S_IWUSR | S_IRUGO,
2781 show_auto_pwm, store_auto_pwm, 2, 5),
2782 SENSOR_ATTR_2(pwm3_auto_point6_temp, S_IWUSR | S_IRUGO,
2783 show_auto_temp, store_auto_temp, 2, 5),
2784 SENSOR_ATTR_2(pwm3_auto_point6_temp_hyst, S_IWUSR | S_IRUGO,
2785 show_auto_temp_hyst, store_auto_temp_hyst, 2, 5),
2786 SENSOR_ATTR_2(pwm3_auto_point7_pwm, S_IWUSR | S_IRUGO,
2787 show_auto_pwm, store_auto_pwm, 2, 6),
2788 SENSOR_ATTR_2(pwm3_auto_point7_temp, S_IWUSR | S_IRUGO,
2789 show_auto_temp, store_auto_temp, 2, 6),
2790 SENSOR_ATTR_2(pwm3_auto_point7_temp_hyst, S_IWUSR | S_IRUGO,
2791 show_auto_temp_hyst, store_auto_temp_hyst, 2, 6),
2793 SENSOR_ATTR_2(pwm4_auto_point1_pwm, S_IWUSR | S_IRUGO,
2794 show_auto_pwm, store_auto_pwm, 3, 0),
2795 SENSOR_ATTR_2(pwm4_auto_point1_temp, S_IWUSR | S_IRUGO,
2796 show_auto_temp, store_auto_temp, 3, 0),
2797 SENSOR_ATTR_2(pwm4_auto_point1_temp_hyst, S_IWUSR | S_IRUGO,
2798 show_auto_temp_hyst, store_auto_temp_hyst, 3, 0),
2799 SENSOR_ATTR_2(pwm4_auto_point2_pwm, S_IWUSR | S_IRUGO,
2800 show_auto_pwm, store_auto_pwm, 3, 1),
2801 SENSOR_ATTR_2(pwm4_auto_point2_temp, S_IWUSR | S_IRUGO,
2802 show_auto_temp, store_auto_temp, 3, 1),
2803 SENSOR_ATTR_2(pwm4_auto_point2_temp_hyst, S_IWUSR | S_IRUGO,
2804 show_auto_temp_hyst, store_auto_temp_hyst, 3, 1),
2805 SENSOR_ATTR_2(pwm4_auto_point3_pwm, S_IWUSR | S_IRUGO,
2806 show_auto_pwm, store_auto_pwm, 3, 2),
2807 SENSOR_ATTR_2(pwm4_auto_point3_temp, S_IWUSR | S_IRUGO,
2808 show_auto_temp, store_auto_temp, 3, 2),
2809 SENSOR_ATTR_2(pwm4_auto_point3_temp_hyst, S_IWUSR | S_IRUGO,
2810 show_auto_temp_hyst, store_auto_temp_hyst, 3, 2),
2811 SENSOR_ATTR_2(pwm4_auto_point4_pwm, S_IWUSR | S_IRUGO,
2812 show_auto_pwm, store_auto_pwm, 3, 3),
2813 SENSOR_ATTR_2(pwm4_auto_point4_temp, S_IWUSR | S_IRUGO,
2814 show_auto_temp, store_auto_temp, 3, 3),
2815 SENSOR_ATTR_2(pwm4_auto_point4_temp_hyst, S_IWUSR | S_IRUGO,
2816 show_auto_temp_hyst, store_auto_temp_hyst, 3, 3),
2817 SENSOR_ATTR_2(pwm4_auto_point5_pwm, S_IWUSR | S_IRUGO,
2818 show_auto_pwm, store_auto_pwm, 3, 4),
2819 SENSOR_ATTR_2(pwm4_auto_point5_temp, S_IWUSR | S_IRUGO,
2820 show_auto_temp, store_auto_temp, 3, 4),
2821 SENSOR_ATTR_2(pwm4_auto_point5_temp_hyst, S_IWUSR | S_IRUGO,
2822 show_auto_temp_hyst, store_auto_temp_hyst, 3, 4),
2823 SENSOR_ATTR_2(pwm4_auto_point6_pwm, S_IWUSR | S_IRUGO,
2824 show_auto_pwm, store_auto_pwm, 3, 5),
2825 SENSOR_ATTR_2(pwm4_auto_point6_temp, S_IWUSR | S_IRUGO,
2826 show_auto_temp, store_auto_temp, 3, 5),
2827 SENSOR_ATTR_2(pwm4_auto_point6_temp_hyst, S_IWUSR | S_IRUGO,
2828 show_auto_temp_hyst, store_auto_temp_hyst, 3, 5),
2829 SENSOR_ATTR_2(pwm4_auto_point7_pwm, S_IWUSR | S_IRUGO,
2830 show_auto_pwm, store_auto_pwm, 3, 6),
2831 SENSOR_ATTR_2(pwm4_auto_point7_temp, S_IWUSR | S_IRUGO,
2832 show_auto_temp, store_auto_temp, 3, 6),
2833 SENSOR_ATTR_2(pwm4_auto_point7_temp_hyst, S_IWUSR | S_IRUGO,
2834 show_auto_temp_hyst, store_auto_temp_hyst, 3, 6),
2836 SENSOR_ATTR_2(pwm5_auto_point1_pwm, S_IWUSR | S_IRUGO,
2837 show_auto_pwm, store_auto_pwm, 4, 0),
2838 SENSOR_ATTR_2(pwm5_auto_point1_temp, S_IWUSR | S_IRUGO,
2839 show_auto_temp, store_auto_temp, 4, 0),
2840 SENSOR_ATTR_2(pwm5_auto_point1_temp_hyst, S_IWUSR | S_IRUGO,
2841 show_auto_temp_hyst, store_auto_temp_hyst, 4, 0),
2842 SENSOR_ATTR_2(pwm5_auto_point2_pwm, S_IWUSR | S_IRUGO,
2843 show_auto_pwm, store_auto_pwm, 4, 1),
2844 SENSOR_ATTR_2(pwm5_auto_point2_temp, S_IWUSR | S_IRUGO,
2845 show_auto_temp, store_auto_temp, 4, 1),
2846 SENSOR_ATTR_2(pwm5_auto_point2_temp_hyst, S_IWUSR | S_IRUGO,
2847 show_auto_temp_hyst, store_auto_temp_hyst, 4, 1),
2848 SENSOR_ATTR_2(pwm5_auto_point3_pwm, S_IWUSR | S_IRUGO,
2849 show_auto_pwm, store_auto_pwm, 4, 2),
2850 SENSOR_ATTR_2(pwm5_auto_point3_temp, S_IWUSR | S_IRUGO,
2851 show_auto_temp, store_auto_temp, 4, 2),
2852 SENSOR_ATTR_2(pwm5_auto_point3_temp_hyst, S_IWUSR | S_IRUGO,
2853 show_auto_temp_hyst, store_auto_temp_hyst, 4, 2),
2854 SENSOR_ATTR_2(pwm5_auto_point4_pwm, S_IWUSR | S_IRUGO,
2855 show_auto_pwm, store_auto_pwm, 4, 3),
2856 SENSOR_ATTR_2(pwm5_auto_point4_temp, S_IWUSR | S_IRUGO,
2857 show_auto_temp, store_auto_temp, 4, 3),
2858 SENSOR_ATTR_2(pwm5_auto_point4_temp_hyst, S_IWUSR | S_IRUGO,
2859 show_auto_temp_hyst, store_auto_temp_hyst, 4, 3),
2860 SENSOR_ATTR_2(pwm5_auto_point5_pwm, S_IWUSR | S_IRUGO,
2861 show_auto_pwm, store_auto_pwm, 4, 4),
2862 SENSOR_ATTR_2(pwm5_auto_point5_temp, S_IWUSR | S_IRUGO,
2863 show_auto_temp, store_auto_temp, 4, 4),
2864 SENSOR_ATTR_2(pwm5_auto_point5_temp_hyst, S_IWUSR | S_IRUGO,
2865 show_auto_temp_hyst, store_auto_temp_hyst, 4, 4),
2866 SENSOR_ATTR_2(pwm5_auto_point6_pwm, S_IWUSR | S_IRUGO,
2867 show_auto_pwm, store_auto_pwm, 4, 5),
2868 SENSOR_ATTR_2(pwm5_auto_point6_temp, S_IWUSR | S_IRUGO,
2869 show_auto_temp, store_auto_temp, 4, 5),
2870 SENSOR_ATTR_2(pwm5_auto_point6_temp_hyst, S_IWUSR | S_IRUGO,
2871 show_auto_temp_hyst, store_auto_temp_hyst, 4, 5),
2872 SENSOR_ATTR_2(pwm5_auto_point7_pwm, S_IWUSR | S_IRUGO,
2873 show_auto_pwm, store_auto_pwm, 4, 6),
2874 SENSOR_ATTR_2(pwm5_auto_point7_temp, S_IWUSR | S_IRUGO,
2875 show_auto_temp, store_auto_temp, 4, 6),
2876 SENSOR_ATTR_2(pwm5_auto_point7_temp_hyst, S_IWUSR | S_IRUGO,
2877 show_auto_temp_hyst, store_auto_temp_hyst, 4, 6),
2881 show_vid(struct device *dev, struct device_attribute *attr, char *buf)
2883 struct nct6775_data *data = dev_get_drvdata(dev);
2884 return sprintf(buf, "%d\n", vid_from_reg(data->vid, data->vrm));
2887 static DEVICE_ATTR(cpu0_vid, S_IRUGO, show_vid, NULL);
2889 /* Case open detection */
2892 clear_caseopen(struct device *dev, struct device_attribute *attr,
2893 const char *buf, size_t count)
2895 struct nct6775_data *data = dev_get_drvdata(dev);
2896 struct nct6775_sio_data *sio_data = dev->platform_data;
2897 int nr = to_sensor_dev_attr(attr)->index - INTRUSION_ALARM_BASE;
2902 if (kstrtoul(buf, 10, &val) || val != 0)
2905 mutex_lock(&data->update_lock);
2908 * Use CR registers to clear caseopen status.
2909 * The CR registers are the same for all chips, and not all chips
2910 * support clearing the caseopen status through "regular" registers.
2912 ret = superio_enter(sio_data->sioreg);
2918 superio_select(sio_data->sioreg, NCT6775_LD_ACPI);
2919 reg = superio_inb(sio_data->sioreg, NCT6775_REG_CR_CASEOPEN_CLR[nr]);
2920 reg |= NCT6775_CR_CASEOPEN_CLR_MASK[nr];
2921 superio_outb(sio_data->sioreg, NCT6775_REG_CR_CASEOPEN_CLR[nr], reg);
2922 reg &= ~NCT6775_CR_CASEOPEN_CLR_MASK[nr];
2923 superio_outb(sio_data->sioreg, NCT6775_REG_CR_CASEOPEN_CLR[nr], reg);
2924 superio_exit(sio_data->sioreg);
2926 data->valid = 0; /* Force cache refresh */
2928 mutex_unlock(&data->update_lock);
2932 static struct sensor_device_attribute sda_caseopen[] = {
2933 SENSOR_ATTR(intrusion0_alarm, S_IWUSR | S_IRUGO, show_alarm,
2934 clear_caseopen, INTRUSION_ALARM_BASE),
2935 SENSOR_ATTR(intrusion1_alarm, S_IWUSR | S_IRUGO, show_alarm,
2936 clear_caseopen, INTRUSION_ALARM_BASE + 1),
2940 * Driver and device management
2943 static void nct6775_device_remove_files(struct device *dev)
2946 * some entries in the following arrays may not have been used in
2947 * device_create_file(), but device_remove_file() will ignore them
2950 struct nct6775_data *data = dev_get_drvdata(dev);
2952 for (i = 0; i < data->pwm_num; i++)
2953 sysfs_remove_group(&dev->kobj, &nct6775_group_pwm[i]);
2955 for (i = 0; i < ARRAY_SIZE(sda_max_output); i++)
2956 device_remove_file(dev, &sda_max_output[i].dev_attr);
2958 for (i = 0; i < ARRAY_SIZE(sda_step_output); i++)
2959 device_remove_file(dev, &sda_max_output[i].dev_attr);
2961 for (i = 0; i < ARRAY_SIZE(sda_weight_duty_base); i++)
2962 device_remove_file(dev, &sda_weight_duty_base[i].dev_attr);
2964 for (i = 0; i < ARRAY_SIZE(sda_auto_pwm_arrays); i++)
2965 device_remove_file(dev, &sda_auto_pwm_arrays[i].dev_attr);
2967 for (i = 0; i < data->in_num; i++)
2968 sysfs_remove_group(&dev->kobj, &nct6775_group_in[i]);
2970 for (i = 0; i < 5; i++) {
2971 device_remove_file(dev, &sda_fan_input[i].dev_attr);
2972 device_remove_file(dev, &sda_fan_alarm[i].dev_attr);
2973 device_remove_file(dev, &sda_fan_div[i].dev_attr);
2974 device_remove_file(dev, &sda_fan_min[i].dev_attr);
2976 for (i = 0; i < NUM_TEMP; i++) {
2977 if (!(data->have_temp & (1 << i)))
2979 device_remove_file(dev, &sda_temp_input[i].dev_attr);
2980 device_remove_file(dev, &sda_temp_label[i].dev_attr);
2981 device_remove_file(dev, &sda_temp_max[i].dev_attr);
2982 device_remove_file(dev, &sda_temp_max_hyst[i].dev_attr);
2983 device_remove_file(dev, &sda_temp_crit[i].dev_attr);
2984 if (!(data->have_temp_fixed & (1 << i)))
2986 device_remove_file(dev, &sda_temp_type[i].dev_attr);
2987 device_remove_file(dev, &sda_temp_offset[i].dev_attr);
2988 if (i >= NUM_TEMP_ALARM)
2990 device_remove_file(dev, &sda_temp_alarm[i].dev_attr);
2993 device_remove_file(dev, &sda_caseopen[0].dev_attr);
2994 device_remove_file(dev, &sda_caseopen[1].dev_attr);
2996 device_remove_file(dev, &dev_attr_name);
2997 device_remove_file(dev, &dev_attr_cpu0_vid);
3000 /* Get the monitoring functions started */
3001 static inline void __devinit nct6775_init_device(struct nct6775_data *data)
3006 /* Start monitoring if needed */
3007 if (data->REG_CONFIG) {
3008 tmp = nct6775_read_value(data, data->REG_CONFIG);
3010 nct6775_write_value(data, data->REG_CONFIG, tmp | 0x01);
3013 /* Enable temperature sensors if needed */
3014 for (i = 0; i < NUM_TEMP; i++) {
3015 if (!(data->have_temp & (1 << i)))
3017 if (!data->reg_temp_config[i])
3019 tmp = nct6775_read_value(data, data->reg_temp_config[i]);
3021 nct6775_write_value(data, data->reg_temp_config[i],
3025 /* Enable VBAT monitoring if needed */
3026 tmp = nct6775_read_value(data, data->REG_VBAT);
3028 nct6775_write_value(data, data->REG_VBAT, tmp | 0x01);
3030 diode = nct6775_read_value(data, data->REG_DIODE);
3032 for (i = 0; i < data->temp_fixed_num; i++) {
3033 if (!(data->have_temp_fixed & (1 << i)))
3035 if ((tmp & (0x02 << i))) /* diode */
3036 data->temp_type[i] = 3 - ((diode >> i) & 0x02);
3037 else /* thermistor */
3038 data->temp_type[i] = 4;
3042 static int __devinit
3043 nct6775_check_fan_inputs(const struct nct6775_sio_data *sio_data,
3044 struct nct6775_data *data)
3047 bool fan3pin, fan3min, fan4pin, fan4min, fan5pin;
3048 bool pwm3pin, pwm4pin, pwm5pin;
3051 ret = superio_enter(sio_data->sioreg);
3055 /* fan4 and fan5 share some pins with the GPIO and serial flash */
3056 if (data->kind == nct6775) {
3057 regval = superio_inb(sio_data->sioreg, 0x2c);
3059 fan3pin = regval & (1 << 6);
3061 pwm3pin = regval & (1 << 7);
3063 /* On NCT6775, fan4 shares pins with the fdc interface */
3064 fan4pin = !(superio_inb(sio_data->sioreg, 0x2A) & 0x80);
3069 } else if (data->kind == nct6776) {
3070 bool gpok = superio_inb(sio_data->sioreg, 0x27) & 0x80;
3072 superio_select(sio_data->sioreg, NCT6775_LD_HWM);
3073 regval = superio_inb(sio_data->sioreg, SIO_REG_ENABLE);
3078 fan3pin = !(superio_inb(sio_data->sioreg, 0x24) & 0x40);
3083 fan4pin = superio_inb(sio_data->sioreg, 0x1C) & 0x01;
3088 fan5pin = superio_inb(sio_data->sioreg, 0x1C) & 0x02;
3095 } else { /* NCT6779D */
3096 regval = superio_inb(sio_data->sioreg, 0x1c);
3098 fan3pin = !(regval & (1 << 5));
3099 fan4pin = !(regval & (1 << 6));
3100 fan5pin = !(regval & (1 << 7));
3102 pwm3pin = !(regval & (1 << 0));
3103 pwm4pin = !(regval & (1 << 1));
3104 pwm5pin = !(regval & (1 << 2));
3110 superio_exit(sio_data->sioreg);
3112 data->has_fan = data->has_fan_min = 0x03; /* fan1 and fan2 */
3113 data->has_fan |= fan3pin << 2;
3114 data->has_fan_min |= fan3min << 2;
3116 data->has_fan |= (fan4pin << 3) | (fan5pin << 4);
3117 data->has_fan_min |= (fan4min << 3) | (fan5pin << 4);
3119 data->has_pwm = 0x03 | (pwm3pin << 2) | (pwm4pin << 3) | (pwm5pin << 4);
3124 static int __devinit nct6775_probe(struct platform_device *pdev)
3126 struct device *dev = &pdev->dev;
3127 struct nct6775_sio_data *sio_data = dev->platform_data;
3128 struct nct6775_data *data;
3129 struct resource *res;
3131 int src, mask, available;
3132 const u16 *reg_temp, *reg_temp_over, *reg_temp_hyst, *reg_temp_config;
3133 const u16 *reg_temp_alternate, *reg_temp_crit;
3136 res = platform_get_resource(pdev, IORESOURCE_IO, 0);
3137 if (!devm_request_region(&pdev->dev, res->start, IOREGION_LENGTH,
3141 data = devm_kzalloc(&pdev->dev, sizeof(struct nct6775_data),
3146 data->kind = sio_data->kind;
3147 data->addr = res->start;
3148 mutex_init(&data->lock);
3149 mutex_init(&data->update_lock);
3150 data->name = nct6775_device_names[data->kind];
3151 platform_set_drvdata(pdev, data);
3153 switch (data->kind) {
3157 data->auto_pwm_num = 6;
3158 data->has_fan_div = true;
3159 data->temp_fixed_num = 3;
3161 data->ALARM_BITS = NCT6775_ALARM_BITS;
3163 data->fan_from_reg = fan_from_reg16;
3164 data->fan_from_reg_min = fan_from_reg8;
3166 data->temp_label = nct6775_temp_label;
3167 data->temp_label_num = ARRAY_SIZE(nct6775_temp_label);
3169 data->REG_CONFIG = NCT6775_REG_CONFIG;
3170 data->REG_VBAT = NCT6775_REG_VBAT;
3171 data->REG_DIODE = NCT6775_REG_DIODE;
3172 data->REG_VIN = NCT6775_REG_IN;
3173 data->REG_IN_MINMAX[0] = NCT6775_REG_IN_MIN;
3174 data->REG_IN_MINMAX[1] = NCT6775_REG_IN_MAX;
3175 data->REG_TARGET = NCT6775_REG_TARGET;
3176 data->REG_FAN = NCT6775_REG_FAN;
3177 data->REG_FAN_MODE = NCT6775_REG_FAN_MODE;
3178 data->REG_FAN_MIN = NCT6775_REG_FAN_MIN;
3179 data->REG_FAN_TIME[0] = NCT6775_REG_FAN_STOP_TIME;
3180 data->REG_FAN_TIME[1] = NCT6775_REG_FAN_STEP_UP_TIME;
3181 data->REG_FAN_TIME[2] = NCT6775_REG_FAN_STEP_DOWN_TIME;
3182 data->REG_PWM[0] = NCT6775_REG_PWM;
3183 data->REG_PWM[1] = NCT6775_REG_FAN_START_OUTPUT;
3184 data->REG_PWM[2] = NCT6775_REG_FAN_STOP_OUTPUT;
3185 data->REG_PWM[3] = NCT6775_REG_FAN_MAX_OUTPUT;
3186 data->REG_PWM[4] = NCT6775_REG_FAN_STEP_OUTPUT;
3187 data->REG_PWM[5] = NCT6775_REG_WEIGHT_DUTY_STEP;
3188 data->REG_PWM_READ = NCT6775_REG_PWM_READ;
3189 data->REG_PWM_MODE = NCT6775_REG_PWM_MODE;
3190 data->PWM_MODE_MASK = NCT6775_PWM_MODE_MASK;
3191 data->REG_TEMP_MON = NCT6775_REG_TEMP_MON;
3192 data->REG_AUTO_TEMP = NCT6775_REG_AUTO_TEMP;
3193 data->REG_AUTO_PWM = NCT6775_REG_AUTO_PWM;
3194 data->REG_CRITICAL_TEMP = NCT6775_REG_CRITICAL_TEMP;
3195 data->REG_CRITICAL_TEMP_TOLERANCE
3196 = NCT6775_REG_CRITICAL_TEMP_TOLERANCE;
3197 data->REG_TEMP_OFFSET = NCT6775_REG_TEMP_OFFSET;
3198 data->REG_TEMP_SOURCE = NCT6775_REG_TEMP_SOURCE;
3199 data->REG_TEMP_SEL[0] = NCT6775_REG_TEMP_SEL;
3200 data->REG_TEMP_SEL[1] = NCT6775_REG_WEIGHT_TEMP_SEL;
3201 data->REG_TEMP_SEL[2] = NCT6775_REG_FAN_STEP_ENABLE;
3202 data->REG_WEIGHT_TEMP[0] = NCT6775_REG_WEIGHT_TEMP_STEP;
3203 data->REG_WEIGHT_TEMP[1] = NCT6775_REG_WEIGHT_TEMP_STEP_TOL;
3204 data->REG_WEIGHT_TEMP[2] = NCT6775_REG_WEIGHT_TEMP_BASE;
3205 data->REG_ALARM = NCT6775_REG_ALARM;
3207 reg_temp = NCT6775_REG_TEMP;
3208 num_reg_temp = ARRAY_SIZE(NCT6775_REG_TEMP);
3209 reg_temp_over = NCT6775_REG_TEMP_OVER;
3210 reg_temp_hyst = NCT6775_REG_TEMP_HYST;
3211 reg_temp_config = NCT6775_REG_TEMP_CONFIG;
3212 reg_temp_alternate = NCT6775_REG_TEMP_ALTERNATE;
3213 reg_temp_crit = NCT6775_REG_TEMP_CRIT;
3219 data->auto_pwm_num = 4;
3220 data->has_fan_div = false;
3221 data->temp_fixed_num = 3;
3223 data->ALARM_BITS = NCT6776_ALARM_BITS;
3225 data->fan_from_reg = fan_from_reg13;
3226 data->fan_from_reg_min = fan_from_reg13;
3228 data->temp_label = nct6776_temp_label;
3229 data->temp_label_num = ARRAY_SIZE(nct6776_temp_label);
3231 data->REG_CONFIG = NCT6775_REG_CONFIG;
3232 data->REG_VBAT = NCT6775_REG_VBAT;
3233 data->REG_DIODE = NCT6775_REG_DIODE;
3234 data->REG_VIN = NCT6775_REG_IN;
3235 data->REG_IN_MINMAX[0] = NCT6775_REG_IN_MIN;
3236 data->REG_IN_MINMAX[1] = NCT6775_REG_IN_MAX;
3237 data->REG_TARGET = NCT6775_REG_TARGET;
3238 data->REG_FAN = NCT6775_REG_FAN;
3239 data->REG_FAN_MODE = NCT6775_REG_FAN_MODE;
3240 data->REG_FAN_MIN = NCT6776_REG_FAN_MIN;
3241 data->REG_FAN_TIME[0] = NCT6775_REG_FAN_STOP_TIME;
3242 data->REG_FAN_TIME[1] = NCT6775_REG_FAN_STEP_UP_TIME;
3243 data->REG_FAN_TIME[2] = NCT6775_REG_FAN_STEP_DOWN_TIME;
3244 data->REG_PWM[0] = NCT6775_REG_PWM;
3245 data->REG_PWM[1] = NCT6775_REG_FAN_START_OUTPUT;
3246 data->REG_PWM[2] = NCT6775_REG_FAN_STOP_OUTPUT;
3247 data->REG_PWM[5] = NCT6775_REG_WEIGHT_DUTY_STEP;
3248 data->REG_PWM[6] = NCT6776_REG_WEIGHT_DUTY_BASE;
3249 data->REG_PWM_READ = NCT6775_REG_PWM_READ;
3250 data->REG_PWM_MODE = NCT6776_REG_PWM_MODE;
3251 data->PWM_MODE_MASK = NCT6776_PWM_MODE_MASK;
3252 data->REG_TEMP_MON = NCT6775_REG_TEMP_MON;
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_TEMP_MON = NCT6775_REG_TEMP_MON;
3314 data->REG_AUTO_TEMP = NCT6775_REG_AUTO_TEMP;
3315 data->REG_AUTO_PWM = NCT6775_REG_AUTO_PWM;
3316 data->REG_CRITICAL_TEMP = NCT6775_REG_CRITICAL_TEMP;
3317 data->REG_CRITICAL_TEMP_TOLERANCE
3318 = NCT6775_REG_CRITICAL_TEMP_TOLERANCE;
3319 data->REG_TEMP_OFFSET = NCT6779_REG_TEMP_OFFSET;
3320 data->REG_TEMP_SOURCE = NCT6775_REG_TEMP_SOURCE;
3321 data->REG_TEMP_SEL[0] = NCT6775_REG_TEMP_SEL;
3322 data->REG_TEMP_SEL[1] = NCT6775_REG_WEIGHT_TEMP_SEL;
3323 data->REG_TEMP_SEL[2] = NCT6775_REG_FAN_STEP_ENABLE;
3324 data->REG_WEIGHT_TEMP[0] = NCT6775_REG_WEIGHT_TEMP_STEP;
3325 data->REG_WEIGHT_TEMP[1] = NCT6775_REG_WEIGHT_TEMP_STEP_TOL;
3326 data->REG_WEIGHT_TEMP[2] = NCT6775_REG_WEIGHT_TEMP_BASE;
3327 data->REG_ALARM = NCT6779_REG_ALARM;
3329 reg_temp = NCT6779_REG_TEMP;
3330 num_reg_temp = ARRAY_SIZE(NCT6779_REG_TEMP);
3331 reg_temp_over = NCT6779_REG_TEMP_OVER;
3332 reg_temp_hyst = NCT6779_REG_TEMP_HYST;
3333 reg_temp_config = NCT6779_REG_TEMP_CONFIG;
3334 reg_temp_alternate = NCT6779_REG_TEMP_ALTERNATE;
3335 reg_temp_crit = NCT6779_REG_TEMP_CRIT;
3341 data->have_in = (1 << data->in_num) - 1;
3342 data->have_temp = 0;
3345 * On some boards, not all available temperature sources are monitored,
3346 * even though some of the monitoring registers are unused.
3347 * Get list of unused monitoring registers, then detect if any fan
3348 * controls are configured to use unmonitored temperature sources.
3349 * If so, assign the unmonitored temperature sources to available
3350 * monitoring registers.
3354 for (i = 0; i < num_reg_temp; i++) {
3355 if (reg_temp[i] == 0)
3358 src = nct6775_read_value(data, data->REG_TEMP_SOURCE[i]) & 0x1f;
3359 if (!src || (mask & (1 << src)))
3360 available |= 1 << i;
3366 * Now find unmonitored temperature registers and enable monitoring
3367 * if additional monitoring registers are available.
3369 for (i = 0; i < ARRAY_SIZE(data->REG_TEMP_SEL) && available; i++) {
3372 if (!data->REG_TEMP_SEL[i])
3374 for (j = 0; j < data->pwm_num && available; j++) {
3377 if (!data->REG_TEMP_SEL[i][j])
3379 src = nct6775_read_value(data,
3380 data->REG_TEMP_SEL[i][j]);
3382 if (!src || (mask & (1 << src)))
3384 if (src >= data->temp_label_num ||
3385 !strlen(data->temp_label[src])) {
3387 "Select source %d:%d reg 0x%x invalid (%d)\n",
3388 i, j, data->REG_TEMP_SEL[i][j], src);
3392 index = __ffs(available);
3393 nct6775_write_value(data,
3394 data->REG_TEMP_SOURCE[index],
3396 available &= ~(1 << index);
3402 s = NUM_TEMP_FIXED; /* First dynamic temperature attribute */
3403 for (i = 0; i < num_reg_temp; i++) {
3404 if (reg_temp[i] == 0)
3407 src = nct6775_read_value(data, data->REG_TEMP_SOURCE[i]) & 0x1f;
3408 if (!src || (mask & (1 << src)))
3411 if (src >= data->temp_label_num ||
3412 !strlen(data->temp_label[src])) {
3414 "Invalid temperature source %d at index %d, source register 0x%x, temp register 0x%x\n",
3415 src, i, data->REG_TEMP_SOURCE[i], reg_temp[i]);
3421 /* Use fixed index for SYSTIN(1), CPUTIN(2), AUXTIN(3) */
3422 if (src <= data->temp_fixed_num) {
3423 data->have_temp |= 1 << (src - 1);
3424 data->have_temp_fixed |= 1 << (src - 1);
3425 data->reg_temp[0][src - 1] = reg_temp[i];
3426 data->reg_temp[1][src - 1] = reg_temp_over[i];
3427 data->reg_temp[2][src - 1] = reg_temp_hyst[i];
3428 data->reg_temp_config[src - 1] = reg_temp_config[i];
3429 data->temp_src[src - 1] = src;
3436 /* Use dynamic index for other sources */
3437 data->have_temp |= 1 << s;
3438 data->reg_temp[0][s] = reg_temp[i];
3439 data->reg_temp[1][s] = reg_temp_over[i];
3440 data->reg_temp[2][s] = reg_temp_hyst[i];
3441 data->reg_temp_config[s] = reg_temp_config[i];
3442 if (reg_temp_crit[src - 1])
3443 data->reg_temp[3][s] = reg_temp_crit[src - 1];
3445 data->temp_src[s] = src;
3451 * Go through the list of alternate temp registers and enable
3453 * The temperature is already monitored if the respective bit in <mask>
3456 for (i = 0; i < data->temp_label_num - 1; i++) {
3457 if (!reg_temp_alternate[i])
3459 if (mask & (1 << (i + 1)))
3461 if (i < data->temp_fixed_num) {
3462 if (data->have_temp & (1 << i))
3464 data->have_temp |= 1 << i;
3465 data->have_temp_fixed |= 1 << i;
3466 data->reg_temp[0][i] = reg_temp_alternate[i];
3467 data->reg_temp[1][i] = reg_temp_over[i];
3468 data->reg_temp[2][i] = reg_temp_hyst[i];
3469 data->temp_src[i] = i + 1;
3473 if (s >= NUM_TEMP) /* Abort if no more space */
3476 data->have_temp |= 1 << s;
3477 data->reg_temp[0][s] = reg_temp_alternate[i];
3478 data->temp_src[s] = i + 1;
3481 #endif /* TESTING */
3483 switch (data->kind) {
3488 * On NCT6776, AUXTIN and VIN3 pins are shared.
3489 * Only way to detect it is to check if AUXTIN is used
3490 * as a temperature source, and if that source is
3493 * If that is the case, disable in6, which reports VIN3.
3494 * Otherwise disable temp3.
3496 if (data->have_temp & (1 << 2)) {
3497 u8 reg = nct6775_read_value(data,
3498 data->reg_temp_config[2]);
3500 data->have_temp &= ~(1 << 2);
3502 data->have_in &= ~(1 << 6);
3513 * There does not seem to be a clean way to detect if VINx or
3514 * AUXTINx is active, so for keep both sensor types enabled
3520 /* Initialize the chip */
3521 nct6775_init_device(data);
3523 data->vrm = vid_which_vrm();
3524 err = superio_enter(sio_data->sioreg);
3530 * We can get the VID input values directly at logical device D 0xe3.
3532 superio_select(sio_data->sioreg, NCT6775_LD_VID);
3533 data->vid = superio_inb(sio_data->sioreg, 0xe3);
3538 superio_select(sio_data->sioreg, NCT6775_LD_HWM);
3539 tmp = superio_inb(sio_data->sioreg,
3540 NCT6775_REG_CR_FAN_DEBOUNCE);
3541 switch (data->kind) {
3550 superio_outb(sio_data->sioreg, NCT6775_REG_CR_FAN_DEBOUNCE,
3552 dev_info(&pdev->dev, "Enabled fan debounce for chip %s\n",
3556 superio_exit(sio_data->sioreg);
3558 err = device_create_file(dev, &dev_attr_cpu0_vid);
3562 err = nct6775_check_fan_inputs(sio_data, data);
3566 /* Read fan clock dividers immediately */
3567 nct6775_update_fan_div_common(dev, data);
3569 /* Register sysfs hooks */
3570 for (i = 0; i < data->pwm_num; i++) {
3571 if (!(data->has_pwm & (1 << i)))
3574 err = sysfs_create_group(&dev->kobj, &nct6775_group_pwm[i]);
3578 if (data->REG_PWM[3]) {
3579 err = device_create_file(dev,
3580 &sda_max_output[i].dev_attr);
3584 if (data->REG_PWM[4]) {
3585 err = device_create_file(dev,
3586 &sda_step_output[i].dev_attr);
3590 if (data->REG_PWM[6]) {
3591 err = device_create_file(dev,
3592 &sda_weight_duty_base[i].dev_attr);
3597 for (i = 0; i < ARRAY_SIZE(sda_auto_pwm_arrays); i++) {
3598 struct sensor_device_attribute_2 *attr =
3599 &sda_auto_pwm_arrays[i];
3601 if (!(data->has_pwm & (1 << attr->nr)))
3603 if (attr->index > data->auto_pwm_num)
3605 err = device_create_file(dev, &attr->dev_attr);
3610 for (i = 0; i < data->in_num; i++) {
3611 if (!(data->have_in & (1 << i)))
3613 err = sysfs_create_group(&dev->kobj, &nct6775_group_in[i]);
3618 for (i = 0; i < 5; i++) {
3619 if (data->has_fan & (1 << i)) {
3620 err = device_create_file(dev,
3621 &sda_fan_input[i].dev_attr);
3624 err = device_create_file(dev,
3625 &sda_fan_alarm[i].dev_attr);
3628 if (data->kind != nct6776 &&
3629 data->kind != nct6779) {
3630 err = device_create_file(dev,
3631 &sda_fan_div[i].dev_attr);
3635 if (data->has_fan_min & (1 << i)) {
3636 err = device_create_file(dev,
3637 &sda_fan_min[i].dev_attr);
3644 for (i = 0; i < NUM_TEMP; i++) {
3645 if (!(data->have_temp & (1 << i)))
3647 err = device_create_file(dev, &sda_temp_input[i].dev_attr);
3650 if (data->temp_label) {
3651 err = device_create_file(dev,
3652 &sda_temp_label[i].dev_attr);
3656 if (data->reg_temp[1][i]) {
3657 err = device_create_file(dev,
3658 &sda_temp_max[i].dev_attr);
3662 if (data->reg_temp[2][i]) {
3663 err = device_create_file(dev,
3664 &sda_temp_max_hyst[i].dev_attr);
3668 if (data->reg_temp[3][i]) {
3669 err = device_create_file(dev,
3670 &sda_temp_crit[i].dev_attr);
3674 if (!(data->have_temp_fixed & (1 << i)))
3676 err = device_create_file(dev, &sda_temp_type[i].dev_attr);
3679 err = device_create_file(dev, &sda_temp_offset[i].dev_attr);
3682 if (i >= NUM_TEMP_ALARM ||
3683 data->ALARM_BITS[TEMP_ALARM_BASE + i] < 0)
3685 err = device_create_file(dev, &sda_temp_alarm[i].dev_attr);
3690 for (i = 0; i < ARRAY_SIZE(sda_caseopen); i++) {
3691 if (data->ALARM_BITS[INTRUSION_ALARM_BASE + i] < 0)
3693 err = device_create_file(dev, &sda_caseopen[i].dev_attr);
3698 err = device_create_file(dev, &dev_attr_name);
3702 data->hwmon_dev = hwmon_device_register(dev);
3703 if (IS_ERR(data->hwmon_dev)) {
3704 err = PTR_ERR(data->hwmon_dev);
3711 nct6775_device_remove_files(dev);
3715 static int __devexit nct6775_remove(struct platform_device *pdev)
3717 struct nct6775_data *data = platform_get_drvdata(pdev);
3719 hwmon_device_unregister(data->hwmon_dev);
3720 nct6775_device_remove_files(&pdev->dev);
3725 static struct platform_driver nct6775_driver = {
3727 .owner = THIS_MODULE,
3730 .probe = nct6775_probe,
3731 .remove = __devexit_p(nct6775_remove),
3734 /* nct6775_find() looks for a '627 in the Super-I/O config space */
3735 static int __init nct6775_find(int sioaddr, unsigned short *addr,
3736 struct nct6775_sio_data *sio_data)
3738 static const char sio_name_NCT6775[] __initconst = "NCT6775F";
3739 static const char sio_name_NCT6776[] __initconst = "NCT6776F";
3740 static const char sio_name_NCT6779[] __initconst = "NCT6779D";
3743 const char *sio_name;
3746 err = superio_enter(sioaddr);
3753 val = (superio_inb(sioaddr, SIO_REG_DEVID) << 8)
3754 | superio_inb(sioaddr, SIO_REG_DEVID + 1);
3755 switch (val & SIO_ID_MASK) {
3756 case SIO_NCT6775_ID:
3757 sio_data->kind = nct6775;
3758 sio_name = sio_name_NCT6775;
3760 case SIO_NCT6776_ID:
3761 sio_data->kind = nct6776;
3762 sio_name = sio_name_NCT6776;
3764 case SIO_NCT6779_ID:
3765 sio_data->kind = nct6779;
3766 sio_name = sio_name_NCT6779;
3770 pr_debug("unsupported chip ID: 0x%04x\n", val);
3771 superio_exit(sioaddr);
3775 /* We have a known chip, find the HWM I/O address */
3776 superio_select(sioaddr, NCT6775_LD_HWM);
3777 val = (superio_inb(sioaddr, SIO_REG_ADDR) << 8)
3778 | superio_inb(sioaddr, SIO_REG_ADDR + 1);
3779 *addr = val & IOREGION_ALIGNMENT;
3781 pr_err("Refusing to enable a Super-I/O device with a base I/O port 0\n");
3782 superio_exit(sioaddr);
3786 /* Activate logical device if needed */
3787 val = superio_inb(sioaddr, SIO_REG_ENABLE);
3788 if (!(val & 0x01)) {
3789 pr_warn("Forcibly enabling Super-I/O. Sensor is probably unusable.\n");
3790 superio_outb(sioaddr, SIO_REG_ENABLE, val | 0x01);
3793 superio_exit(sioaddr);
3794 pr_info("Found %s chip at %#x\n", sio_name, *addr);
3795 sio_data->sioreg = sioaddr;
3801 * when Super-I/O functions move to a separate file, the Super-I/O
3802 * bus will manage the lifetime of the device and this module will only keep
3803 * track of the nct6775 driver. But since we platform_device_alloc(), we
3804 * must keep track of the device
3806 static struct platform_device *pdev;
3808 static int __init sensors_nct6775_init(void)
3811 unsigned short address;
3812 struct resource res;
3813 struct nct6775_sio_data sio_data;
3816 * initialize sio_data->kind and sio_data->sioreg.
3818 * when Super-I/O functions move to a separate file, the Super-I/O
3819 * driver will probe 0x2e and 0x4e and auto-detect the presence of a
3820 * nct6775 hardware monitor, and call probe()
3822 if (nct6775_find(0x2e, &address, &sio_data) &&
3823 nct6775_find(0x4e, &address, &sio_data))
3826 err = platform_driver_register(&nct6775_driver);
3830 pdev = platform_device_alloc(DRVNAME, address);
3833 pr_err("Device allocation failed\n");
3834 goto exit_unregister;
3837 err = platform_device_add_data(pdev, &sio_data,
3838 sizeof(struct nct6775_sio_data));
3840 pr_err("Platform data allocation failed\n");
3841 goto exit_device_put;
3844 memset(&res, 0, sizeof(res));
3846 res.start = address + IOREGION_OFFSET;
3847 res.end = address + IOREGION_OFFSET + IOREGION_LENGTH - 1;
3848 res.flags = IORESOURCE_IO;
3850 err = acpi_check_resource_conflict(&res);
3852 goto exit_device_put;
3854 err = platform_device_add_resources(pdev, &res, 1);
3856 pr_err("Device resource addition failed (%d)\n", err);
3857 goto exit_device_put;
3860 /* platform_device_add calls probe() */
3861 err = platform_device_add(pdev);
3863 pr_err("Device addition failed (%d)\n", err);
3864 goto exit_device_put;
3870 platform_device_put(pdev);
3872 platform_driver_unregister(&nct6775_driver);
3877 static void __exit sensors_nct6775_exit(void)
3879 platform_device_unregister(pdev);
3880 platform_driver_unregister(&nct6775_driver);
3883 MODULE_AUTHOR("Guenter Roeck <linux@roeck-us.net>");
3884 MODULE_DESCRIPTION("NCT6775F/NCT6776F/NCT6779D driver");
3885 MODULE_LICENSE("GPL");
3887 module_init(sensors_nct6775_init);
3888 module_exit(sensors_nct6775_exit);