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_AUTO_TEMP[]
320 = { 0x121, 0x221, 0x321, 0x821, 0x921 };
321 static const u16 NCT6775_REG_AUTO_PWM[]
322 = { 0x127, 0x227, 0x327, 0x827, 0x927 };
324 #define NCT6775_AUTO_TEMP(data, nr, p) ((data)->REG_AUTO_TEMP[nr] + (p))
325 #define NCT6775_AUTO_PWM(data, nr, p) ((data)->REG_AUTO_PWM[nr] + (p))
327 static const u16 NCT6775_REG_CRITICAL_ENAB[] = { 0x134, 0x234, 0x334 };
329 static const u16 NCT6775_REG_CRITICAL_TEMP[] = {
330 0x135, 0x235, 0x335, 0x835, 0x935 };
331 static const u16 NCT6775_REG_CRITICAL_TEMP_TOLERANCE[] = {
332 0x138, 0x238, 0x338, 0x838, 0x938 };
334 static const u16 NCT6779_REG_CRITICAL_PWM_ENABLE[] = {
335 0x136, 0x236, 0x336, 0x836, 0x936 };
336 static const u16 NCT6779_REG_CRITICAL_PWM[] = {
337 0x137, 0x237, 0x337, 0x837, 0x937 };
339 static const char *const nct6775_temp_label[] = {
353 "PCH_CHIP_CPU_MAX_TEMP",
363 static const char *const nct6776_temp_label[] = {
378 "PCH_CHIP_CPU_MAX_TEMP",
389 static const char *const nct6779_temp_label[] = {
408 "PCH_CHIP_CPU_MAX_TEMP",
419 static const u16 NCT6775_REG_TEMP_ALTERNATE[ARRAY_SIZE(nct6775_temp_label) - 1]
420 = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0x661, 0x662, 0x664 };
422 static const u16 NCT6776_REG_TEMP_ALTERNATE[ARRAY_SIZE(nct6776_temp_label) - 1]
423 = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0x401, 0x402, 0x404 };
425 static const u16 NCT6779_REG_TEMP_ALTERNATE[ARRAY_SIZE(nct6779_temp_label) - 1]
426 = { 0x490, 0x491, 0x492, 0x493, 0x494, 0x495, 0, 0,
427 0, 0, 0, 0, 0, 0, 0, 0,
428 0, 0x400, 0x401, 0x402, 0x404, 0x405, 0x406, 0x407,
431 static const u16 NCT6775_REG_TEMP_CRIT[ARRAY_SIZE(nct6775_temp_label) - 1]
432 = { 0, 0, 0, 0, 0xa00, 0xa01, 0xa02, 0xa03, 0xa04, 0xa05, 0xa06,
435 static const u16 NCT6776_REG_TEMP_CRIT[ARRAY_SIZE(nct6776_temp_label) - 1]
436 = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0x709, 0x70a };
438 static const u16 NCT6779_REG_TEMP_CRIT[ARRAY_SIZE(nct6779_temp_label) - 1]
439 = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0x709, 0x70a };
441 #define NUM_TEMP 10 /* Max number of temp attribute sets w/ limits*/
442 #define NUM_TEMP_FIXED 6 /* Max number of fixed temp attribute sets */
444 static inline int reg_to_pwm_enable(int pwm, int mode)
446 if (mode == 0 && pwm == 255)
451 static inline int pwm_enable_to_reg(int mode)
462 /* 1 is DC mode, output in ms */
463 static inline unsigned int step_time_from_reg(u8 reg, u8 mode)
465 return mode ? 400 * reg : 100 * reg;
468 static inline u8 step_time_to_reg(unsigned int msec, u8 mode)
470 return SENSORS_LIMIT((mode ? (msec + 200) / 400 :
471 (msec + 50) / 100), 1, 255);
474 static unsigned int fan_from_reg8(u16 reg, unsigned int divreg)
476 if (reg == 0 || reg == 255)
478 return 1350000U / (reg << divreg);
481 static unsigned int fan_from_reg13(u16 reg, unsigned int divreg)
483 if ((reg & 0xff1f) == 0xff1f)
486 reg = (reg & 0x1f) | ((reg & 0xff00) >> 3);
491 return 1350000U / reg;
494 static unsigned int fan_from_reg16(u16 reg, unsigned int divreg)
496 if (reg == 0 || reg == 0xffff)
500 * Even though the registers are 16 bit wide, the fan divisor
503 return 1350000U / (reg << divreg);
506 static inline unsigned int
513 * Some of the voltage inputs have internal scaling, the tables below
514 * contain 8 (the ADC LSB in mV) * scaling factor * 100
516 static const u16 scale_in[15] = {
517 800, 800, 1600, 1600, 800, 800, 800, 1600, 1600, 800, 800, 800, 800,
521 static inline long in_from_reg(u8 reg, u8 nr)
523 return DIV_ROUND_CLOSEST(reg * scale_in[nr], 100);
526 static inline u8 in_to_reg(u32 val, u8 nr)
528 return SENSORS_LIMIT(DIV_ROUND_CLOSEST(val * 100, scale_in[nr]), 0,
533 * Data structures and manipulation thereof
536 struct nct6775_data {
537 int addr; /* IO base of hw monitor block */
541 struct device *hwmon_dev;
544 u16 reg_temp[4][NUM_TEMP]; /* 0=temp, 1=temp_over, 2=temp_hyst,
547 u8 temp_src[NUM_TEMP];
548 u16 reg_temp_config[NUM_TEMP];
549 const char * const *temp_label;
556 const s8 *ALARM_BITS;
559 const u16 *REG_IN_MINMAX[2];
561 const u16 *REG_TARGET;
563 const u16 *REG_FAN_MODE;
564 const u16 *REG_FAN_MIN;
565 const u16 *REG_FAN_TIME[3];
567 const u8 *REG_PWM_MODE;
568 const u8 *PWM_MODE_MASK;
570 const u16 *REG_PWM[7]; /* [0]=pwm, [1]=start_output, [2]=stop_output,
571 * [3]=max_output, [4]=step_output,
572 * [5]=weight_duty_step, [6]=weight_duty_base
574 const u16 *REG_PWM_READ;
576 const u16 *REG_AUTO_TEMP;
577 const u16 *REG_AUTO_PWM;
579 const u16 *REG_CRITICAL_TEMP;
580 const u16 *REG_CRITICAL_TEMP_TOLERANCE;
582 const u16 *REG_TEMP_SOURCE; /* temp register sources */
583 const u16 *REG_TEMP_SEL[3]; /* pwm temp:
584 * 0=base, 1=weight, 2=step enable
587 const u16 *REG_WEIGHT_TEMP[3]; /* 0=base, 1=hyst, 2=step */
589 const u16 *REG_TEMP_OFFSET;
591 const u16 *REG_ALARM;
593 unsigned int (*fan_from_reg)(u16 reg, unsigned int divreg);
594 unsigned int (*fan_from_reg_min)(u16 reg, unsigned int divreg);
596 struct mutex update_lock;
597 char valid; /* !=0 if following fields are valid */
598 unsigned long last_updated; /* In jiffies */
600 /* Register values */
601 u8 bank; /* current register bank */
602 u8 in_num; /* number of in inputs we have */
603 u8 in[15][3]; /* [0]=in, [1]=in_max, [2]=in_min */
608 u8 has_fan; /* some fan inputs can be disabled */
609 u8 has_fan_min; /* some fans don't have min register */
612 u8 temp_fixed_num; /* 3 or 6 */
613 u8 temp_type[NUM_TEMP_FIXED];
614 s8 temp_offset[NUM_TEMP_FIXED];
615 s16 temp[4][NUM_TEMP]; /* 0=temp, 1=temp_over, 2=temp_hyst,
619 u8 pwm_num; /* number of pwm */
621 u8 pwm_mode[5]; /* 1->DC variable voltage, 0->PWM variable duty cycle */
622 u8 pwm_enable[5]; /* 0->off
624 * 2->thermal cruise mode (also called SmartFan I)
625 * 3->fan speed cruise mode
627 * 5->enhanced variable thermal cruise (SmartFan IV)
629 u8 pwm[7][5]; /* [0]=pwm, [1]=start_output, [2]=stop_output,
630 * [3]=max_output, [4]=step_output,
631 * [5]=weight_duty_step, [6]=weight_duty_base
636 u8 fan_time[3][5]; /* 0 = stop_time, 1 = step_up, 2 = step_down */
638 /* Automatic fan speed control registers */
643 u8 pwm_temp_sel[2][5];
645 bool pwm_sel_enable[3][5];/* 0->stop_val, 1->weight, 2->step;
646 * false->off, true->on
648 u8 weight_temp[3][5]; /* 0->temp_step, 1->temp_step_tol,
660 struct nct6775_sio_data {
665 static bool is_word_sized(struct nct6775_data *data, u16 reg)
667 switch (data->kind) {
669 return (((reg & 0xff00) == 0x100 ||
670 (reg & 0xff00) == 0x200) &&
671 ((reg & 0x00ff) == 0x50 ||
672 (reg & 0x00ff) == 0x53 ||
673 (reg & 0x00ff) == 0x55)) ||
674 (reg & 0xfff0) == 0x630 ||
675 reg == 0x640 || reg == 0x642 ||
677 ((reg & 0xfff0) == 0x650 && (reg & 0x000f) >= 0x06) ||
678 reg == 0x73 || reg == 0x75 || reg == 0x77;
680 return (((reg & 0xff00) == 0x100 ||
681 (reg & 0xff00) == 0x200) &&
682 ((reg & 0x00ff) == 0x50 ||
683 (reg & 0x00ff) == 0x53 ||
684 (reg & 0x00ff) == 0x55)) ||
685 (reg & 0xfff0) == 0x630 ||
687 reg == 0x640 || reg == 0x642 ||
688 ((reg & 0xfff0) == 0x650 && (reg & 0x000f) >= 0x06) ||
689 reg == 0x73 || reg == 0x75 || reg == 0x77;
691 return reg == 0x150 || reg == 0x153 || reg == 0x155 ||
692 ((reg & 0xfff0) == 0x4b0 && (reg & 0x000f) < 0x09) ||
694 reg == 0x63a || reg == 0x63c || reg == 0x63e ||
695 reg == 0x640 || reg == 0x642 ||
696 reg == 0x73 || reg == 0x75 || reg == 0x77 || reg == 0x79 ||
703 * On older chips, only registers 0x50-0x5f are banked.
704 * On more recent chips, all registers are banked.
705 * Assume that is the case and set the bank number for each access.
706 * Cache the bank number so it only needs to be set if it changes.
708 static inline void nct6775_set_bank(struct nct6775_data *data, u16 reg)
711 if (data->bank != bank) {
712 outb_p(NCT6775_REG_BANK, data->addr + ADDR_REG_OFFSET);
713 outb_p(bank, data->addr + DATA_REG_OFFSET);
718 static u16 nct6775_read_value(struct nct6775_data *data, u16 reg)
720 int res, word_sized = is_word_sized(data, reg);
722 mutex_lock(&data->lock);
724 nct6775_set_bank(data, reg);
725 outb_p(reg & 0xff, data->addr + ADDR_REG_OFFSET);
726 res = inb_p(data->addr + DATA_REG_OFFSET);
728 outb_p((reg & 0xff) + 1,
729 data->addr + ADDR_REG_OFFSET);
730 res = (res << 8) + inb_p(data->addr + DATA_REG_OFFSET);
733 mutex_unlock(&data->lock);
737 static int nct6775_write_value(struct nct6775_data *data, u16 reg, u16 value)
739 int word_sized = is_word_sized(data, reg);
741 mutex_lock(&data->lock);
743 nct6775_set_bank(data, reg);
744 outb_p(reg & 0xff, data->addr + ADDR_REG_OFFSET);
746 outb_p(value >> 8, data->addr + DATA_REG_OFFSET);
747 outb_p((reg & 0xff) + 1,
748 data->addr + ADDR_REG_OFFSET);
750 outb_p(value & 0xff, data->addr + DATA_REG_OFFSET);
752 mutex_unlock(&data->lock);
756 /* We left-align 8-bit temperature values to make the code simpler */
757 static u16 nct6775_read_temp(struct nct6775_data *data, u16 reg)
761 res = nct6775_read_value(data, reg);
762 if (!is_word_sized(data, reg))
768 static int nct6775_write_temp(struct nct6775_data *data, u16 reg, u16 value)
770 if (!is_word_sized(data, reg))
772 return nct6775_write_value(data, reg, value);
775 /* This function assumes that the caller holds data->update_lock */
776 static void nct6775_write_fan_div(struct nct6775_data *data, int nr)
782 reg = (nct6775_read_value(data, NCT6775_REG_FANDIV1) & 0x70)
783 | (data->fan_div[0] & 0x7);
784 nct6775_write_value(data, NCT6775_REG_FANDIV1, reg);
787 reg = (nct6775_read_value(data, NCT6775_REG_FANDIV1) & 0x7)
788 | ((data->fan_div[1] << 4) & 0x70);
789 nct6775_write_value(data, NCT6775_REG_FANDIV1, reg);
792 reg = (nct6775_read_value(data, NCT6775_REG_FANDIV2) & 0x70)
793 | (data->fan_div[2] & 0x7);
794 nct6775_write_value(data, NCT6775_REG_FANDIV2, reg);
797 reg = (nct6775_read_value(data, NCT6775_REG_FANDIV2) & 0x7)
798 | ((data->fan_div[3] << 4) & 0x70);
799 nct6775_write_value(data, NCT6775_REG_FANDIV2, reg);
804 static void nct6775_write_fan_div_common(struct device *dev,
805 struct nct6775_data *data, int nr)
807 if (data->kind == nct6775)
808 nct6775_write_fan_div(data, nr);
811 static void nct6775_update_fan_div(struct nct6775_data *data)
815 i = nct6775_read_value(data, NCT6775_REG_FANDIV1);
816 data->fan_div[0] = i & 0x7;
817 data->fan_div[1] = (i & 0x70) >> 4;
818 i = nct6775_read_value(data, NCT6775_REG_FANDIV2);
819 data->fan_div[2] = i & 0x7;
820 if (data->has_fan & (1<<3))
821 data->fan_div[3] = (i & 0x70) >> 4;
824 static void nct6775_update_fan_div_common(struct device *dev,
825 struct nct6775_data *data)
827 if (data->kind == nct6775)
828 nct6775_update_fan_div(data);
831 static void nct6775_update_pwm(struct device *dev)
833 struct nct6775_data *data = dev_get_drvdata(dev);
838 for (i = 0; i < data->pwm_num; i++) {
839 if (!(data->has_pwm & (1 << i)))
842 duty_is_dc = data->REG_PWM_MODE[i] &&
843 (nct6775_read_value(data, data->REG_PWM_MODE[i])
844 & data->PWM_MODE_MASK[i]);
845 data->pwm_mode[i] = duty_is_dc;
847 fanmodecfg = nct6775_read_value(data, data->REG_FAN_MODE[i]);
848 for (j = 0; j < ARRAY_SIZE(data->REG_PWM); j++) {
849 if (data->REG_PWM[j] && data->REG_PWM[j][i]) {
851 = nct6775_read_value(data,
852 data->REG_PWM[j][i]);
856 data->pwm_enable[i] = reg_to_pwm_enable(data->pwm[0][i],
857 (fanmodecfg >> 4) & 7);
859 data->tolerance[i][0] = fanmodecfg & 0x0f;
860 if (data->kind == nct6779) {
861 reg = nct6775_read_value(data,
862 NCT6779_REG_TOLERANCE_H[i]);
863 data->tolerance[i][0] |= (reg & 0x70) >> 1;
865 data->tolerance[i][1] =
866 nct6775_read_value(data,
867 data->REG_CRITICAL_TEMP_TOLERANCE[i]);
869 data->pwm_sel_enable[0][i] =
870 nct6775_read_value(data, data->REG_TEMP_SEL[0][i])
872 data->pwm_sel_enable[1][i] =
873 nct6775_read_value(data, data->REG_TEMP_SEL[1][i])
875 data->pwm_sel_enable[2][i] =
876 nct6775_read_value(data, data->REG_TEMP_SEL[2][i])
880 for (j = 0; j < 2; j++) {
881 reg = nct6775_read_value(data,
882 data->REG_TEMP_SEL[j][i]);
883 data->pwm_temp_sel[j][i] = reg & 0x1f;
885 /* Weight temp data */
886 for (j = 0; j < 3; j++) {
887 data->weight_temp[j][i]
888 = nct6775_read_value(data,
889 data->REG_WEIGHT_TEMP[j][i]);
894 static void nct6775_update_pwm_limits(struct device *dev)
896 struct nct6775_data *data = dev_get_drvdata(dev);
900 for (i = 0; i < data->pwm_num; i++) {
901 if (!(data->has_pwm & (1 << i)))
904 for (j = 0; j < 3; j++) {
905 data->fan_time[j][i] =
906 nct6775_read_value(data, data->REG_FAN_TIME[j][i]);
909 data->target_temp[i] =
910 nct6775_read_value(data, data->REG_TARGET[i]) &
911 (data->pwm_mode[i] ? 0xff : 0x7f);
913 for (j = 0; j < data->auto_pwm_num; j++) {
914 data->auto_pwm[i][j] =
915 nct6775_read_value(data,
916 NCT6775_AUTO_PWM(data, i, j));
917 data->auto_temp[i][j] =
918 nct6775_read_value(data,
919 NCT6775_AUTO_TEMP(data, i, j));
922 /* critical auto_pwm temperature data */
923 data->auto_temp[i][data->auto_pwm_num] =
924 nct6775_read_value(data, data->REG_CRITICAL_TEMP[i]);
926 switch (data->kind) {
928 reg = nct6775_read_value(data,
929 NCT6775_REG_CRITICAL_ENAB[i]);
930 data->auto_pwm[i][data->auto_pwm_num] =
931 (reg & 0x02) ? 0xff : 0x00;
934 data->auto_pwm[i][data->auto_pwm_num] = 0xff;
937 reg = nct6775_read_value(data,
938 NCT6779_REG_CRITICAL_PWM_ENABLE[i]);
940 data->auto_pwm[i][data->auto_pwm_num] =
941 nct6775_read_value(data,
942 NCT6779_REG_CRITICAL_PWM[i]);
944 data->auto_pwm[i][data->auto_pwm_num] = 0xff;
950 static struct nct6775_data *nct6775_update_device(struct device *dev)
952 struct nct6775_data *data = dev_get_drvdata(dev);
955 mutex_lock(&data->update_lock);
957 if (time_after(jiffies, data->last_updated + HZ + HZ/2)
959 /* Fan clock dividers */
960 nct6775_update_fan_div_common(dev, data);
962 /* Measured voltages and limits */
963 for (i = 0; i < data->in_num; i++) {
964 if (!(data->have_in & (1 << i)))
967 data->in[i][0] = nct6775_read_value(data,
969 data->in[i][1] = nct6775_read_value(data,
970 data->REG_IN_MINMAX[0][i]);
971 data->in[i][2] = nct6775_read_value(data,
972 data->REG_IN_MINMAX[1][i]);
975 /* Measured fan speeds and limits */
976 for (i = 0; i < 5; i++) {
979 if (!(data->has_fan & (1 << i)))
982 reg = nct6775_read_value(data, data->REG_FAN[i]);
983 data->rpm[i] = data->fan_from_reg(reg,
986 if (data->has_fan_min & (1 << i))
987 data->fan_min[i] = nct6775_read_value(data,
988 data->REG_FAN_MIN[i]);
991 * If we failed to measure the fan speed and clock
992 * divider can be increased, let's try that for next
995 if (data->has_fan_div && (reg >= 0xff || reg == 0x00)
996 && data->fan_div[i] < 0x07) {
998 "Increasing fan%d clock divider from %u to %u\n",
999 i + 1, div_from_reg(data->fan_div[i]),
1000 div_from_reg(data->fan_div[i] + 1));
1002 nct6775_write_fan_div_common(dev, data, i);
1003 /* Preserve min limit if possible */
1004 if ((data->has_fan_min & (1 << i))
1005 && data->fan_min[i] >= 2
1006 && data->fan_min[i] != 255)
1007 nct6775_write_value(data,
1008 data->REG_FAN_MIN[i],
1009 (data->fan_min[i] /= 2));
1013 nct6775_update_pwm(dev);
1014 nct6775_update_pwm_limits(dev);
1016 /* Measured temperatures and limits */
1017 for (i = 0; i < NUM_TEMP; i++) {
1018 if (!(data->have_temp & (1 << i)))
1020 for (j = 0; j < 4; j++) {
1021 if (data->reg_temp[j][i])
1023 = nct6775_read_temp(data,
1024 data->reg_temp[j][i]);
1026 if (!(data->have_temp_fixed & (1 << i)))
1028 data->temp_offset[i]
1029 = nct6775_read_value(data, data->REG_TEMP_OFFSET[i]);
1033 for (i = 0; i < 6; i++) {
1035 if (!data->REG_ALARM[i])
1037 alarm = nct6775_read_value(data, data->REG_ALARM[i]);
1038 data->alarms |= ((u64)alarm) << (i << 3);
1041 data->last_updated = jiffies;
1045 mutex_unlock(&data->update_lock);
1050 * Sysfs callback functions
1053 show_in_reg(struct device *dev, struct device_attribute *attr, char *buf)
1055 struct nct6775_data *data = nct6775_update_device(dev);
1056 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
1058 int index = sattr->index;
1059 return sprintf(buf, "%ld\n", in_from_reg(data->in[nr][index], nr));
1063 store_in_reg(struct device *dev, struct device_attribute *attr, const char *buf,
1066 struct nct6775_data *data = dev_get_drvdata(dev);
1067 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
1069 int index = sattr->index;
1071 int err = kstrtoul(buf, 10, &val);
1074 mutex_lock(&data->update_lock);
1075 data->in[nr][index] = in_to_reg(val, nr);
1076 nct6775_write_value(data, data->REG_IN_MINMAX[index-1][nr],
1077 data->in[nr][index]);
1078 mutex_unlock(&data->update_lock);
1083 show_alarm(struct device *dev, struct device_attribute *attr, char *buf)
1085 struct nct6775_data *data = nct6775_update_device(dev);
1086 struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr);
1087 int nr = data->ALARM_BITS[sattr->index];
1088 return sprintf(buf, "%u\n",
1089 (unsigned int)((data->alarms >> nr) & 0x01));
1092 static SENSOR_DEVICE_ATTR_2(in0_input, S_IRUGO, show_in_reg, NULL, 0, 0);
1093 static SENSOR_DEVICE_ATTR_2(in1_input, S_IRUGO, show_in_reg, NULL, 1, 0);
1094 static SENSOR_DEVICE_ATTR_2(in2_input, S_IRUGO, show_in_reg, NULL, 2, 0);
1095 static SENSOR_DEVICE_ATTR_2(in3_input, S_IRUGO, show_in_reg, NULL, 3, 0);
1096 static SENSOR_DEVICE_ATTR_2(in4_input, S_IRUGO, show_in_reg, NULL, 4, 0);
1097 static SENSOR_DEVICE_ATTR_2(in5_input, S_IRUGO, show_in_reg, NULL, 5, 0);
1098 static SENSOR_DEVICE_ATTR_2(in6_input, S_IRUGO, show_in_reg, NULL, 6, 0);
1099 static SENSOR_DEVICE_ATTR_2(in7_input, S_IRUGO, show_in_reg, NULL, 7, 0);
1100 static SENSOR_DEVICE_ATTR_2(in8_input, S_IRUGO, show_in_reg, NULL, 8, 0);
1101 static SENSOR_DEVICE_ATTR_2(in9_input, S_IRUGO, show_in_reg, NULL, 9, 0);
1102 static SENSOR_DEVICE_ATTR_2(in10_input, S_IRUGO, show_in_reg, NULL, 10, 0);
1103 static SENSOR_DEVICE_ATTR_2(in11_input, S_IRUGO, show_in_reg, NULL, 11, 0);
1104 static SENSOR_DEVICE_ATTR_2(in12_input, S_IRUGO, show_in_reg, NULL, 12, 0);
1105 static SENSOR_DEVICE_ATTR_2(in13_input, S_IRUGO, show_in_reg, NULL, 13, 0);
1106 static SENSOR_DEVICE_ATTR_2(in14_input, S_IRUGO, show_in_reg, NULL, 14, 0);
1108 static SENSOR_DEVICE_ATTR(in0_alarm, S_IRUGO, show_alarm, NULL, 0);
1109 static SENSOR_DEVICE_ATTR(in1_alarm, S_IRUGO, show_alarm, NULL, 1);
1110 static SENSOR_DEVICE_ATTR(in2_alarm, S_IRUGO, show_alarm, NULL, 2);
1111 static SENSOR_DEVICE_ATTR(in3_alarm, S_IRUGO, show_alarm, NULL, 3);
1112 static SENSOR_DEVICE_ATTR(in4_alarm, S_IRUGO, show_alarm, NULL, 4);
1113 static SENSOR_DEVICE_ATTR(in5_alarm, S_IRUGO, show_alarm, NULL, 5);
1114 static SENSOR_DEVICE_ATTR(in6_alarm, S_IRUGO, show_alarm, NULL, 6);
1115 static SENSOR_DEVICE_ATTR(in7_alarm, S_IRUGO, show_alarm, NULL, 7);
1116 static SENSOR_DEVICE_ATTR(in8_alarm, S_IRUGO, show_alarm, NULL, 8);
1117 static SENSOR_DEVICE_ATTR(in9_alarm, S_IRUGO, show_alarm, NULL, 9);
1118 static SENSOR_DEVICE_ATTR(in10_alarm, S_IRUGO, show_alarm, NULL, 10);
1119 static SENSOR_DEVICE_ATTR(in11_alarm, S_IRUGO, show_alarm, NULL, 11);
1120 static SENSOR_DEVICE_ATTR(in12_alarm, S_IRUGO, show_alarm, NULL, 12);
1121 static SENSOR_DEVICE_ATTR(in13_alarm, S_IRUGO, show_alarm, NULL, 13);
1122 static SENSOR_DEVICE_ATTR(in14_alarm, S_IRUGO, show_alarm, NULL, 14);
1124 static SENSOR_DEVICE_ATTR_2(in0_min, S_IWUSR | S_IRUGO, show_in_reg,
1125 store_in_reg, 0, 1);
1126 static SENSOR_DEVICE_ATTR_2(in1_min, S_IWUSR | S_IRUGO, show_in_reg,
1127 store_in_reg, 1, 1);
1128 static SENSOR_DEVICE_ATTR_2(in2_min, S_IWUSR | S_IRUGO, show_in_reg,
1129 store_in_reg, 2, 1);
1130 static SENSOR_DEVICE_ATTR_2(in3_min, S_IWUSR | S_IRUGO, show_in_reg,
1131 store_in_reg, 3, 1);
1132 static SENSOR_DEVICE_ATTR_2(in4_min, S_IWUSR | S_IRUGO, show_in_reg,
1133 store_in_reg, 4, 1);
1134 static SENSOR_DEVICE_ATTR_2(in5_min, S_IWUSR | S_IRUGO, show_in_reg,
1135 store_in_reg, 5, 1);
1136 static SENSOR_DEVICE_ATTR_2(in6_min, S_IWUSR | S_IRUGO, show_in_reg,
1137 store_in_reg, 6, 1);
1138 static SENSOR_DEVICE_ATTR_2(in7_min, S_IWUSR | S_IRUGO, show_in_reg,
1139 store_in_reg, 7, 1);
1140 static SENSOR_DEVICE_ATTR_2(in8_min, S_IWUSR | S_IRUGO, show_in_reg,
1141 store_in_reg, 8, 1);
1142 static SENSOR_DEVICE_ATTR_2(in9_min, S_IWUSR | S_IRUGO, show_in_reg,
1143 store_in_reg, 9, 1);
1144 static SENSOR_DEVICE_ATTR_2(in10_min, S_IWUSR | S_IRUGO, show_in_reg,
1145 store_in_reg, 10, 1);
1146 static SENSOR_DEVICE_ATTR_2(in11_min, S_IWUSR | S_IRUGO, show_in_reg,
1147 store_in_reg, 11, 1);
1148 static SENSOR_DEVICE_ATTR_2(in12_min, S_IWUSR | S_IRUGO, show_in_reg,
1149 store_in_reg, 12, 1);
1150 static SENSOR_DEVICE_ATTR_2(in13_min, S_IWUSR | S_IRUGO, show_in_reg,
1151 store_in_reg, 13, 1);
1152 static SENSOR_DEVICE_ATTR_2(in14_min, S_IWUSR | S_IRUGO, show_in_reg,
1153 store_in_reg, 14, 1);
1155 static SENSOR_DEVICE_ATTR_2(in0_max, S_IWUSR | S_IRUGO, show_in_reg,
1156 store_in_reg, 0, 2);
1157 static SENSOR_DEVICE_ATTR_2(in1_max, S_IWUSR | S_IRUGO, show_in_reg,
1158 store_in_reg, 1, 2);
1159 static SENSOR_DEVICE_ATTR_2(in2_max, S_IWUSR | S_IRUGO, show_in_reg,
1160 store_in_reg, 2, 2);
1161 static SENSOR_DEVICE_ATTR_2(in3_max, S_IWUSR | S_IRUGO, show_in_reg,
1162 store_in_reg, 3, 2);
1163 static SENSOR_DEVICE_ATTR_2(in4_max, S_IWUSR | S_IRUGO, show_in_reg,
1164 store_in_reg, 4, 2);
1165 static SENSOR_DEVICE_ATTR_2(in5_max, S_IWUSR | S_IRUGO, show_in_reg,
1166 store_in_reg, 5, 2);
1167 static SENSOR_DEVICE_ATTR_2(in6_max, S_IWUSR | S_IRUGO, show_in_reg,
1168 store_in_reg, 6, 2);
1169 static SENSOR_DEVICE_ATTR_2(in7_max, S_IWUSR | S_IRUGO, show_in_reg,
1170 store_in_reg, 7, 2);
1171 static SENSOR_DEVICE_ATTR_2(in8_max, S_IWUSR | S_IRUGO, show_in_reg,
1172 store_in_reg, 8, 2);
1173 static SENSOR_DEVICE_ATTR_2(in9_max, S_IWUSR | S_IRUGO, show_in_reg,
1174 store_in_reg, 9, 2);
1175 static SENSOR_DEVICE_ATTR_2(in10_max, S_IWUSR | S_IRUGO, show_in_reg,
1176 store_in_reg, 10, 2);
1177 static SENSOR_DEVICE_ATTR_2(in11_max, S_IWUSR | S_IRUGO, show_in_reg,
1178 store_in_reg, 11, 2);
1179 static SENSOR_DEVICE_ATTR_2(in12_max, S_IWUSR | S_IRUGO, show_in_reg,
1180 store_in_reg, 12, 2);
1181 static SENSOR_DEVICE_ATTR_2(in13_max, S_IWUSR | S_IRUGO, show_in_reg,
1182 store_in_reg, 13, 2);
1183 static SENSOR_DEVICE_ATTR_2(in14_max, S_IWUSR | S_IRUGO, show_in_reg,
1184 store_in_reg, 14, 2);
1186 static struct attribute *nct6775_attributes_in[15][5] = {
1188 &sensor_dev_attr_in0_input.dev_attr.attr,
1189 &sensor_dev_attr_in0_min.dev_attr.attr,
1190 &sensor_dev_attr_in0_max.dev_attr.attr,
1191 &sensor_dev_attr_in0_alarm.dev_attr.attr,
1195 &sensor_dev_attr_in1_input.dev_attr.attr,
1196 &sensor_dev_attr_in1_min.dev_attr.attr,
1197 &sensor_dev_attr_in1_max.dev_attr.attr,
1198 &sensor_dev_attr_in1_alarm.dev_attr.attr,
1202 &sensor_dev_attr_in2_input.dev_attr.attr,
1203 &sensor_dev_attr_in2_min.dev_attr.attr,
1204 &sensor_dev_attr_in2_max.dev_attr.attr,
1205 &sensor_dev_attr_in2_alarm.dev_attr.attr,
1209 &sensor_dev_attr_in3_input.dev_attr.attr,
1210 &sensor_dev_attr_in3_min.dev_attr.attr,
1211 &sensor_dev_attr_in3_max.dev_attr.attr,
1212 &sensor_dev_attr_in3_alarm.dev_attr.attr,
1216 &sensor_dev_attr_in4_input.dev_attr.attr,
1217 &sensor_dev_attr_in4_min.dev_attr.attr,
1218 &sensor_dev_attr_in4_max.dev_attr.attr,
1219 &sensor_dev_attr_in4_alarm.dev_attr.attr,
1223 &sensor_dev_attr_in5_input.dev_attr.attr,
1224 &sensor_dev_attr_in5_min.dev_attr.attr,
1225 &sensor_dev_attr_in5_max.dev_attr.attr,
1226 &sensor_dev_attr_in5_alarm.dev_attr.attr,
1230 &sensor_dev_attr_in6_input.dev_attr.attr,
1231 &sensor_dev_attr_in6_min.dev_attr.attr,
1232 &sensor_dev_attr_in6_max.dev_attr.attr,
1233 &sensor_dev_attr_in6_alarm.dev_attr.attr,
1237 &sensor_dev_attr_in7_input.dev_attr.attr,
1238 &sensor_dev_attr_in7_min.dev_attr.attr,
1239 &sensor_dev_attr_in7_max.dev_attr.attr,
1240 &sensor_dev_attr_in7_alarm.dev_attr.attr,
1244 &sensor_dev_attr_in8_input.dev_attr.attr,
1245 &sensor_dev_attr_in8_min.dev_attr.attr,
1246 &sensor_dev_attr_in8_max.dev_attr.attr,
1247 &sensor_dev_attr_in8_alarm.dev_attr.attr,
1251 &sensor_dev_attr_in9_input.dev_attr.attr,
1252 &sensor_dev_attr_in9_min.dev_attr.attr,
1253 &sensor_dev_attr_in9_max.dev_attr.attr,
1254 &sensor_dev_attr_in9_alarm.dev_attr.attr,
1258 &sensor_dev_attr_in10_input.dev_attr.attr,
1259 &sensor_dev_attr_in10_min.dev_attr.attr,
1260 &sensor_dev_attr_in10_max.dev_attr.attr,
1261 &sensor_dev_attr_in10_alarm.dev_attr.attr,
1265 &sensor_dev_attr_in11_input.dev_attr.attr,
1266 &sensor_dev_attr_in11_min.dev_attr.attr,
1267 &sensor_dev_attr_in11_max.dev_attr.attr,
1268 &sensor_dev_attr_in11_alarm.dev_attr.attr,
1272 &sensor_dev_attr_in12_input.dev_attr.attr,
1273 &sensor_dev_attr_in12_min.dev_attr.attr,
1274 &sensor_dev_attr_in12_max.dev_attr.attr,
1275 &sensor_dev_attr_in12_alarm.dev_attr.attr,
1279 &sensor_dev_attr_in13_input.dev_attr.attr,
1280 &sensor_dev_attr_in13_min.dev_attr.attr,
1281 &sensor_dev_attr_in13_max.dev_attr.attr,
1282 &sensor_dev_attr_in13_alarm.dev_attr.attr,
1286 &sensor_dev_attr_in14_input.dev_attr.attr,
1287 &sensor_dev_attr_in14_min.dev_attr.attr,
1288 &sensor_dev_attr_in14_max.dev_attr.attr,
1289 &sensor_dev_attr_in14_alarm.dev_attr.attr,
1294 static const struct attribute_group nct6775_group_in[15] = {
1295 { .attrs = nct6775_attributes_in[0] },
1296 { .attrs = nct6775_attributes_in[1] },
1297 { .attrs = nct6775_attributes_in[2] },
1298 { .attrs = nct6775_attributes_in[3] },
1299 { .attrs = nct6775_attributes_in[4] },
1300 { .attrs = nct6775_attributes_in[5] },
1301 { .attrs = nct6775_attributes_in[6] },
1302 { .attrs = nct6775_attributes_in[7] },
1303 { .attrs = nct6775_attributes_in[8] },
1304 { .attrs = nct6775_attributes_in[9] },
1305 { .attrs = nct6775_attributes_in[10] },
1306 { .attrs = nct6775_attributes_in[11] },
1307 { .attrs = nct6775_attributes_in[12] },
1308 { .attrs = nct6775_attributes_in[13] },
1309 { .attrs = nct6775_attributes_in[14] },
1313 show_fan(struct device *dev, struct device_attribute *attr, char *buf)
1315 struct nct6775_data *data = nct6775_update_device(dev);
1316 struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr);
1317 int nr = sattr->index;
1318 return sprintf(buf, "%d\n", data->rpm[nr]);
1322 show_fan_min(struct device *dev, struct device_attribute *attr, char *buf)
1324 struct nct6775_data *data = nct6775_update_device(dev);
1325 struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr);
1326 int nr = sattr->index;
1327 return sprintf(buf, "%d\n",
1328 data->fan_from_reg_min(data->fan_min[nr],
1329 data->fan_div[nr]));
1333 show_fan_div(struct device *dev, struct device_attribute *attr, char *buf)
1335 struct nct6775_data *data = nct6775_update_device(dev);
1336 struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr);
1337 int nr = sattr->index;
1338 return sprintf(buf, "%u\n", div_from_reg(data->fan_div[nr]));
1342 store_fan_min(struct device *dev, struct device_attribute *attr,
1343 const char *buf, size_t count)
1345 struct nct6775_data *data = dev_get_drvdata(dev);
1346 struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr);
1347 int nr = sattr->index;
1353 err = kstrtoul(buf, 10, &val);
1357 mutex_lock(&data->update_lock);
1358 if (!data->has_fan_div) {
1359 /* NCT6776F or NCT6779D; we know this is a 13 bit register */
1365 val = 1350000U / val;
1366 val = (val & 0x1f) | ((val << 3) & 0xff00);
1368 data->fan_min[nr] = val;
1369 goto write_min; /* Leave fan divider alone */
1372 /* No min limit, alarm disabled */
1373 data->fan_min[nr] = 255;
1374 new_div = data->fan_div[nr]; /* No change */
1375 dev_info(dev, "fan%u low limit and alarm disabled\n", nr + 1);
1378 reg = 1350000U / val;
1379 if (reg >= 128 * 255) {
1381 * Speed below this value cannot possibly be represented,
1382 * even with the highest divider (128)
1384 data->fan_min[nr] = 254;
1385 new_div = 7; /* 128 == (1 << 7) */
1387 "fan%u low limit %lu below minimum %u, set to minimum\n",
1388 nr + 1, val, data->fan_from_reg_min(254, 7));
1391 * Speed above this value cannot possibly be represented,
1392 * even with the lowest divider (1)
1394 data->fan_min[nr] = 1;
1395 new_div = 0; /* 1 == (1 << 0) */
1397 "fan%u low limit %lu above maximum %u, set to maximum\n",
1398 nr + 1, val, data->fan_from_reg_min(1, 0));
1401 * Automatically pick the best divider, i.e. the one such
1402 * that the min limit will correspond to a register value
1403 * in the 96..192 range
1406 while (reg > 192 && new_div < 7) {
1410 data->fan_min[nr] = reg;
1415 * Write both the fan clock divider (if it changed) and the new
1416 * fan min (unconditionally)
1418 if (new_div != data->fan_div[nr]) {
1419 dev_dbg(dev, "fan%u clock divider changed from %u to %u\n",
1420 nr + 1, div_from_reg(data->fan_div[nr]),
1421 div_from_reg(new_div));
1422 data->fan_div[nr] = new_div;
1423 nct6775_write_fan_div_common(dev, data, nr);
1424 /* Give the chip time to sample a new speed value */
1425 data->last_updated = jiffies;
1429 nct6775_write_value(data, data->REG_FAN_MIN[nr], data->fan_min[nr]);
1430 mutex_unlock(&data->update_lock);
1435 static struct sensor_device_attribute sda_fan_input[] = {
1436 SENSOR_ATTR(fan1_input, S_IRUGO, show_fan, NULL, 0),
1437 SENSOR_ATTR(fan2_input, S_IRUGO, show_fan, NULL, 1),
1438 SENSOR_ATTR(fan3_input, S_IRUGO, show_fan, NULL, 2),
1439 SENSOR_ATTR(fan4_input, S_IRUGO, show_fan, NULL, 3),
1440 SENSOR_ATTR(fan5_input, S_IRUGO, show_fan, NULL, 4),
1443 static struct sensor_device_attribute sda_fan_alarm[] = {
1444 SENSOR_ATTR(fan1_alarm, S_IRUGO, show_alarm, NULL, FAN_ALARM_BASE),
1445 SENSOR_ATTR(fan2_alarm, S_IRUGO, show_alarm, NULL, FAN_ALARM_BASE + 1),
1446 SENSOR_ATTR(fan3_alarm, S_IRUGO, show_alarm, NULL, FAN_ALARM_BASE + 2),
1447 SENSOR_ATTR(fan4_alarm, S_IRUGO, show_alarm, NULL, FAN_ALARM_BASE + 3),
1448 SENSOR_ATTR(fan5_alarm, S_IRUGO, show_alarm, NULL, FAN_ALARM_BASE + 4),
1451 static struct sensor_device_attribute sda_fan_min[] = {
1452 SENSOR_ATTR(fan1_min, S_IWUSR | S_IRUGO, show_fan_min,
1454 SENSOR_ATTR(fan2_min, S_IWUSR | S_IRUGO, show_fan_min,
1456 SENSOR_ATTR(fan3_min, S_IWUSR | S_IRUGO, show_fan_min,
1458 SENSOR_ATTR(fan4_min, S_IWUSR | S_IRUGO, show_fan_min,
1460 SENSOR_ATTR(fan5_min, S_IWUSR | S_IRUGO, show_fan_min,
1464 static struct sensor_device_attribute sda_fan_div[] = {
1465 SENSOR_ATTR(fan1_div, S_IRUGO, show_fan_div, NULL, 0),
1466 SENSOR_ATTR(fan2_div, S_IRUGO, show_fan_div, NULL, 1),
1467 SENSOR_ATTR(fan3_div, S_IRUGO, show_fan_div, NULL, 2),
1468 SENSOR_ATTR(fan4_div, S_IRUGO, show_fan_div, NULL, 3),
1469 SENSOR_ATTR(fan5_div, S_IRUGO, show_fan_div, NULL, 4),
1473 show_temp_label(struct device *dev, struct device_attribute *attr, char *buf)
1475 struct nct6775_data *data = nct6775_update_device(dev);
1476 struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr);
1477 int nr = sattr->index;
1478 return sprintf(buf, "%s\n", data->temp_label[data->temp_src[nr]]);
1482 show_temp(struct device *dev, struct device_attribute *attr, char *buf)
1484 struct nct6775_data *data = nct6775_update_device(dev);
1485 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
1487 int index = sattr->index;
1489 return sprintf(buf, "%d\n", LM75_TEMP_FROM_REG(data->temp[index][nr]));
1493 store_temp(struct device *dev, struct device_attribute *attr, const char *buf,
1496 struct nct6775_data *data = dev_get_drvdata(dev);
1497 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
1499 int index = sattr->index;
1503 err = kstrtol(buf, 10, &val);
1507 mutex_lock(&data->update_lock);
1508 data->temp[index][nr] = LM75_TEMP_TO_REG(val);
1509 nct6775_write_temp(data, data->reg_temp[index][nr],
1510 data->temp[index][nr]);
1511 mutex_unlock(&data->update_lock);
1516 show_temp_offset(struct device *dev, struct device_attribute *attr, char *buf)
1518 struct nct6775_data *data = nct6775_update_device(dev);
1519 struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr);
1521 return sprintf(buf, "%d\n", data->temp_offset[sattr->index] * 1000);
1525 store_temp_offset(struct device *dev, struct device_attribute *attr,
1526 const char *buf, size_t count)
1528 struct nct6775_data *data = dev_get_drvdata(dev);
1529 struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr);
1530 int nr = sattr->index;
1534 err = kstrtol(buf, 10, &val);
1538 val = SENSORS_LIMIT(DIV_ROUND_CLOSEST(val, 1000), -128, 127);
1540 mutex_lock(&data->update_lock);
1541 data->temp_offset[nr] = val;
1542 nct6775_write_value(data, data->REG_TEMP_OFFSET[nr], val);
1543 mutex_unlock(&data->update_lock);
1549 show_temp_type(struct device *dev, struct device_attribute *attr, char *buf)
1551 struct nct6775_data *data = nct6775_update_device(dev);
1552 struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr);
1553 int nr = sattr->index;
1554 return sprintf(buf, "%d\n", (int)data->temp_type[nr]);
1558 store_temp_type(struct device *dev, struct device_attribute *attr,
1559 const char *buf, size_t count)
1561 struct nct6775_data *data = nct6775_update_device(dev);
1562 struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr);
1563 int nr = sattr->index;
1566 u8 vbat, diode, bit;
1568 err = kstrtoul(buf, 10, &val);
1572 if (val != 1 && val != 3 && val != 4)
1575 mutex_lock(&data->update_lock);
1577 data->temp_type[nr] = val;
1578 vbat = nct6775_read_value(data, data->REG_VBAT) & ~(0x02 << nr);
1579 diode = nct6775_read_value(data, data->REG_DIODE) & ~(0x02 << nr);
1582 case 1: /* CPU diode (diode, current mode) */
1586 case 3: /* diode, voltage mode */
1589 case 4: /* thermistor */
1592 nct6775_write_value(data, data->REG_VBAT, vbat);
1593 nct6775_write_value(data, data->REG_DIODE, diode);
1595 mutex_unlock(&data->update_lock);
1599 static struct sensor_device_attribute_2 sda_temp_input[] = {
1600 SENSOR_ATTR_2(temp1_input, S_IRUGO, show_temp, NULL, 0, 0),
1601 SENSOR_ATTR_2(temp2_input, S_IRUGO, show_temp, NULL, 1, 0),
1602 SENSOR_ATTR_2(temp3_input, S_IRUGO, show_temp, NULL, 2, 0),
1603 SENSOR_ATTR_2(temp4_input, S_IRUGO, show_temp, NULL, 3, 0),
1604 SENSOR_ATTR_2(temp5_input, S_IRUGO, show_temp, NULL, 4, 0),
1605 SENSOR_ATTR_2(temp6_input, S_IRUGO, show_temp, NULL, 5, 0),
1606 SENSOR_ATTR_2(temp7_input, S_IRUGO, show_temp, NULL, 6, 0),
1607 SENSOR_ATTR_2(temp8_input, S_IRUGO, show_temp, NULL, 7, 0),
1608 SENSOR_ATTR_2(temp9_input, S_IRUGO, show_temp, NULL, 8, 0),
1609 SENSOR_ATTR_2(temp10_input, S_IRUGO, show_temp, NULL, 9, 0),
1612 static struct sensor_device_attribute sda_temp_label[] = {
1613 SENSOR_ATTR(temp1_label, S_IRUGO, show_temp_label, NULL, 0),
1614 SENSOR_ATTR(temp2_label, S_IRUGO, show_temp_label, NULL, 1),
1615 SENSOR_ATTR(temp3_label, S_IRUGO, show_temp_label, NULL, 2),
1616 SENSOR_ATTR(temp4_label, S_IRUGO, show_temp_label, NULL, 3),
1617 SENSOR_ATTR(temp5_label, S_IRUGO, show_temp_label, NULL, 4),
1618 SENSOR_ATTR(temp6_label, S_IRUGO, show_temp_label, NULL, 5),
1619 SENSOR_ATTR(temp7_label, S_IRUGO, show_temp_label, NULL, 6),
1620 SENSOR_ATTR(temp8_label, S_IRUGO, show_temp_label, NULL, 7),
1621 SENSOR_ATTR(temp9_label, S_IRUGO, show_temp_label, NULL, 8),
1622 SENSOR_ATTR(temp10_label, S_IRUGO, show_temp_label, NULL, 9),
1625 static struct sensor_device_attribute_2 sda_temp_max[] = {
1626 SENSOR_ATTR_2(temp1_max, S_IRUGO | S_IWUSR, show_temp, store_temp,
1628 SENSOR_ATTR_2(temp2_max, S_IRUGO | S_IWUSR, show_temp, store_temp,
1630 SENSOR_ATTR_2(temp3_max, S_IRUGO | S_IWUSR, show_temp, store_temp,
1632 SENSOR_ATTR_2(temp4_max, S_IRUGO | S_IWUSR, show_temp, store_temp,
1634 SENSOR_ATTR_2(temp5_max, S_IRUGO | S_IWUSR, show_temp, store_temp,
1636 SENSOR_ATTR_2(temp6_max, S_IRUGO | S_IWUSR, show_temp, store_temp,
1638 SENSOR_ATTR_2(temp7_max, S_IRUGO | S_IWUSR, show_temp, store_temp,
1640 SENSOR_ATTR_2(temp8_max, S_IRUGO | S_IWUSR, show_temp, store_temp,
1642 SENSOR_ATTR_2(temp9_max, S_IRUGO | S_IWUSR, show_temp, store_temp,
1644 SENSOR_ATTR_2(temp10_max, S_IRUGO | S_IWUSR, show_temp, store_temp,
1648 static struct sensor_device_attribute_2 sda_temp_max_hyst[] = {
1649 SENSOR_ATTR_2(temp1_max_hyst, S_IRUGO | S_IWUSR, show_temp, store_temp,
1651 SENSOR_ATTR_2(temp2_max_hyst, S_IRUGO | S_IWUSR, show_temp, store_temp,
1653 SENSOR_ATTR_2(temp3_max_hyst, S_IRUGO | S_IWUSR, show_temp, store_temp,
1655 SENSOR_ATTR_2(temp4_max_hyst, S_IRUGO | S_IWUSR, show_temp, store_temp,
1657 SENSOR_ATTR_2(temp5_max_hyst, S_IRUGO | S_IWUSR, show_temp, store_temp,
1659 SENSOR_ATTR_2(temp6_max_hyst, S_IRUGO | S_IWUSR, show_temp, store_temp,
1661 SENSOR_ATTR_2(temp7_max_hyst, S_IRUGO | S_IWUSR, show_temp, store_temp,
1663 SENSOR_ATTR_2(temp8_max_hyst, S_IRUGO | S_IWUSR, show_temp, store_temp,
1665 SENSOR_ATTR_2(temp9_max_hyst, S_IRUGO | S_IWUSR, show_temp, store_temp,
1667 SENSOR_ATTR_2(temp10_max_hyst, S_IRUGO | S_IWUSR, show_temp, store_temp,
1671 static struct sensor_device_attribute_2 sda_temp_crit[] = {
1672 SENSOR_ATTR_2(temp1_crit, S_IRUGO | S_IWUSR, show_temp, store_temp,
1674 SENSOR_ATTR_2(temp2_crit, S_IRUGO | S_IWUSR, show_temp, store_temp,
1676 SENSOR_ATTR_2(temp3_crit, S_IRUGO | S_IWUSR, show_temp, store_temp,
1678 SENSOR_ATTR_2(temp4_crit, S_IRUGO | S_IWUSR, show_temp, store_temp,
1680 SENSOR_ATTR_2(temp5_crit, S_IRUGO | S_IWUSR, show_temp, store_temp,
1682 SENSOR_ATTR_2(temp6_crit, S_IRUGO | S_IWUSR, show_temp, store_temp,
1684 SENSOR_ATTR_2(temp7_crit, S_IRUGO | S_IWUSR, show_temp, store_temp,
1686 SENSOR_ATTR_2(temp8_crit, S_IRUGO | S_IWUSR, show_temp, store_temp,
1688 SENSOR_ATTR_2(temp9_crit, S_IRUGO | S_IWUSR, show_temp, store_temp,
1690 SENSOR_ATTR_2(temp10_crit, S_IRUGO | S_IWUSR, show_temp, store_temp,
1694 static struct sensor_device_attribute sda_temp_offset[] = {
1695 SENSOR_ATTR(temp1_offset, S_IRUGO | S_IWUSR, show_temp_offset,
1696 store_temp_offset, 0),
1697 SENSOR_ATTR(temp2_offset, S_IRUGO | S_IWUSR, show_temp_offset,
1698 store_temp_offset, 1),
1699 SENSOR_ATTR(temp3_offset, S_IRUGO | S_IWUSR, show_temp_offset,
1700 store_temp_offset, 2),
1701 SENSOR_ATTR(temp4_offset, S_IRUGO | S_IWUSR, show_temp_offset,
1702 store_temp_offset, 3),
1703 SENSOR_ATTR(temp5_offset, S_IRUGO | S_IWUSR, show_temp_offset,
1704 store_temp_offset, 4),
1705 SENSOR_ATTR(temp6_offset, S_IRUGO | S_IWUSR, show_temp_offset,
1706 store_temp_offset, 5),
1709 static struct sensor_device_attribute sda_temp_type[] = {
1710 SENSOR_ATTR(temp1_type, S_IRUGO | S_IWUSR, show_temp_type,
1711 store_temp_type, 0),
1712 SENSOR_ATTR(temp2_type, S_IRUGO | S_IWUSR, show_temp_type,
1713 store_temp_type, 1),
1714 SENSOR_ATTR(temp3_type, S_IRUGO | S_IWUSR, show_temp_type,
1715 store_temp_type, 2),
1716 SENSOR_ATTR(temp4_type, S_IRUGO | S_IWUSR, show_temp_type,
1717 store_temp_type, 3),
1718 SENSOR_ATTR(temp5_type, S_IRUGO | S_IWUSR, show_temp_type,
1719 store_temp_type, 4),
1720 SENSOR_ATTR(temp6_type, S_IRUGO | S_IWUSR, show_temp_type,
1721 store_temp_type, 5),
1724 static struct sensor_device_attribute sda_temp_alarm[] = {
1725 SENSOR_ATTR(temp1_alarm, S_IRUGO, show_alarm, NULL,
1727 SENSOR_ATTR(temp2_alarm, S_IRUGO, show_alarm, NULL,
1728 TEMP_ALARM_BASE + 1),
1729 SENSOR_ATTR(temp3_alarm, S_IRUGO, show_alarm, NULL,
1730 TEMP_ALARM_BASE + 2),
1731 SENSOR_ATTR(temp4_alarm, S_IRUGO, show_alarm, NULL,
1732 TEMP_ALARM_BASE + 3),
1733 SENSOR_ATTR(temp5_alarm, S_IRUGO, show_alarm, NULL,
1734 TEMP_ALARM_BASE + 4),
1735 SENSOR_ATTR(temp6_alarm, S_IRUGO, show_alarm, NULL,
1736 TEMP_ALARM_BASE + 5),
1739 #define NUM_TEMP_ALARM ARRAY_SIZE(sda_temp_alarm)
1742 show_pwm_mode(struct device *dev, struct device_attribute *attr, char *buf)
1744 struct nct6775_data *data = nct6775_update_device(dev);
1745 struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr);
1747 return sprintf(buf, "%d\n", !data->pwm_mode[sattr->index]);
1751 store_pwm_mode(struct device *dev, struct device_attribute *attr,
1752 const char *buf, size_t count)
1754 struct nct6775_data *data = dev_get_drvdata(dev);
1755 struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr);
1756 int nr = sattr->index;
1761 err = kstrtoul(buf, 10, &val);
1768 /* Setting DC mode is not supported for all chips/channels */
1769 if (data->REG_PWM_MODE[nr] == 0) {
1775 mutex_lock(&data->update_lock);
1776 data->pwm_mode[nr] = val;
1777 reg = nct6775_read_value(data, data->REG_PWM_MODE[nr]);
1778 reg &= ~data->PWM_MODE_MASK[nr];
1780 reg |= data->PWM_MODE_MASK[nr];
1781 nct6775_write_value(data, data->REG_PWM_MODE[nr], reg);
1782 mutex_unlock(&data->update_lock);
1787 show_pwm_enable(struct device *dev, struct device_attribute *attr, char *buf)
1789 struct nct6775_data *data = nct6775_update_device(dev);
1790 struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr);
1792 return sprintf(buf, "%d\n", data->pwm_enable[sattr->index]);
1796 show_pwm(struct device *dev, struct device_attribute *attr, char *buf)
1798 struct nct6775_data *data = nct6775_update_device(dev);
1799 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
1801 int index = sattr->index;
1805 * For automatic fan control modes, show current pwm readings.
1806 * Otherwise, show the configured value.
1808 if (index == 0 && data->pwm_enable[nr] > 1)
1809 pwm = nct6775_read_value(data, data->REG_PWM_READ[nr]);
1811 pwm = data->pwm[index][nr];
1813 return sprintf(buf, "%d\n", pwm);
1817 store_pwm(struct device *dev, struct device_attribute *attr, const char *buf,
1820 struct nct6775_data *data = dev_get_drvdata(dev);
1821 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
1823 int index = sattr->index;
1825 int minval[7] = { 0, 1, 1, data->pwm[2][nr], 0, 0, 0 };
1827 = { 255, 255, data->pwm[3][nr] ? : 255, 255, 255, 255, 255 };
1830 err = kstrtoul(buf, 10, &val);
1833 val = SENSORS_LIMIT(val, minval[index], maxval[index]);
1835 mutex_lock(&data->update_lock);
1836 data->pwm[index][nr] = val;
1837 nct6775_write_value(data, data->REG_PWM[index][nr], val);
1838 mutex_unlock(&data->update_lock);
1842 /* Returns 0 if OK, -EINVAL otherwise */
1843 static int check_trip_points(struct nct6775_data *data, int nr)
1847 for (i = 0; i < data->auto_pwm_num - 1; i++) {
1848 if (data->auto_temp[nr][i] > data->auto_temp[nr][i + 1])
1851 for (i = 0; i < data->auto_pwm_num - 1; i++) {
1852 if (data->auto_pwm[nr][i] > data->auto_pwm[nr][i + 1])
1855 /* validate critical temperature and pwm if enabled (pwm > 0) */
1856 if (data->auto_pwm[nr][data->auto_pwm_num]) {
1857 if (data->auto_temp[nr][data->auto_pwm_num - 1] >
1858 data->auto_temp[nr][data->auto_pwm_num] ||
1859 data->auto_pwm[nr][data->auto_pwm_num - 1] >
1860 data->auto_pwm[nr][data->auto_pwm_num])
1867 store_pwm_enable(struct device *dev, struct device_attribute *attr,
1868 const char *buf, size_t count)
1870 struct nct6775_data *data = dev_get_drvdata(dev);
1871 struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr);
1872 int nr = sattr->index;
1877 err = kstrtoul(buf, 10, &val);
1884 if (val == 4 && data->kind != nct6775)
1887 if (val == 5 && check_trip_points(data, nr)) {
1888 dev_err(dev, "Inconsistent trip points, not switching to SmartFan IV mode\n");
1889 dev_err(dev, "Adjust trip points and try again\n");
1893 mutex_lock(&data->update_lock);
1894 data->pwm_enable[nr] = val;
1897 * turn off pwm control: select manual mode, set pwm to maximum
1899 data->pwm[0][nr] = 255;
1900 nct6775_write_value(data, data->REG_PWM[0][nr], 255);
1902 reg = nct6775_read_value(data, data->REG_FAN_MODE[nr]);
1904 reg |= pwm_enable_to_reg(val) << 4;
1905 nct6775_write_value(data, data->REG_FAN_MODE[nr], reg);
1906 mutex_unlock(&data->update_lock);
1911 show_pwm_temp_sel(struct device *dev, struct device_attribute *attr, char *buf)
1913 struct nct6775_data *data = nct6775_update_device(dev);
1914 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
1915 int i, src, sel = 0;
1917 src = data->pwm_temp_sel[sattr->index][sattr->nr];
1919 for (i = 0; i < NUM_TEMP; i++) {
1920 if (!(data->have_temp & (1 << i)))
1922 if (src == data->temp_src[i]) {
1928 return sprintf(buf, "%d\n", sel);
1932 store_pwm_temp_sel(struct device *dev, struct device_attribute *attr,
1933 const char *buf, size_t count)
1935 struct nct6775_data *data = nct6775_update_device(dev);
1936 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
1938 int index = sattr->index;
1942 err = kstrtoul(buf, 10, &val);
1945 if (val == 0 || val > NUM_TEMP)
1947 if (!(data->have_temp & (1 << (val - 1))) || !data->temp_src[val - 1])
1950 mutex_lock(&data->update_lock);
1951 src = data->temp_src[val - 1];
1952 data->pwm_temp_sel[index][nr] = src;
1953 reg = nct6775_read_value(data, data->REG_TEMP_SEL[index][nr]);
1956 nct6775_write_value(data, data->REG_TEMP_SEL[index][nr], reg);
1957 mutex_unlock(&data->update_lock);
1963 show_target_temp(struct device *dev, struct device_attribute *attr, char *buf)
1965 struct nct6775_data *data = nct6775_update_device(dev);
1966 struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr);
1968 return sprintf(buf, "%d\n",
1969 data->target_temp[sattr->index] * 1000);
1973 store_target_temp(struct device *dev, struct device_attribute *attr,
1974 const char *buf, size_t count)
1976 struct nct6775_data *data = dev_get_drvdata(dev);
1977 struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr);
1978 int nr = sattr->index;
1982 err = kstrtol(buf, 10, &val);
1986 val = SENSORS_LIMIT(DIV_ROUND_CLOSEST(val, 1000), 0, 127);
1988 mutex_lock(&data->update_lock);
1989 data->target_temp[nr] = val;
1990 nct6775_write_value(data, data->REG_TARGET[nr], val);
1991 mutex_unlock(&data->update_lock);
1996 show_auto_temp_hyst(struct device *dev, struct device_attribute *attr,
1999 struct nct6775_data *data = nct6775_update_device(dev);
2000 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
2002 int point = sattr->index >= data->auto_pwm_num ? 1 : 0;
2003 int tolerance = data->tolerance[nr][point];
2004 int temp = data->auto_temp[nr][sattr->index];
2006 return sprintf(buf, "%d\n", (temp - tolerance) * 1000);
2010 store_auto_temp_hyst(struct device *dev, struct device_attribute *attr,
2011 const char *buf, size_t count)
2013 struct nct6775_data *data = dev_get_drvdata(dev);
2014 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
2016 int point = sattr->index >= data->auto_pwm_num ? 1 : 0;
2020 int maxlimit[2][3] = { { 15, 7, 63 }, { 15, 7, 7 } };
2021 int mask[] = { 0x0f, 0x07, 0x07 };
2024 err = kstrtol(buf, 10, &val);
2028 temp = data->auto_temp[nr][sattr->index];
2029 val = temp - DIV_ROUND_CLOSEST(val, 1000);
2031 /* Limit tolerance as needed */
2032 val = SENSORS_LIMIT(val, 0, maxlimit[point][data->kind]);
2034 mutex_lock(&data->update_lock);
2036 nct6775_write_value(data,
2037 data->REG_CRITICAL_TEMP_TOLERANCE[nr],
2040 reg = nct6775_read_value(data, data->REG_FAN_MODE[nr]);
2041 reg = (reg & ~mask[nr]) | (val & mask[nr]);
2042 nct6775_write_value(data, data->REG_FAN_MODE[nr], reg);
2043 if (data->kind == nct6779) {
2044 reg = nct6775_read_value(data,
2045 NCT6779_REG_TOLERANCE_H[nr]);
2046 reg = (reg & 0x70) | ((val & 0x38) << 1);
2047 nct6775_write_value(data,
2048 NCT6779_REG_TOLERANCE_H[nr], reg);
2052 data->tolerance[nr][point] = val;
2053 mutex_unlock(&data->update_lock);
2057 static SENSOR_DEVICE_ATTR_2(pwm1, S_IWUSR | S_IRUGO, show_pwm, store_pwm, 0, 0);
2058 static SENSOR_DEVICE_ATTR_2(pwm2, S_IWUSR | S_IRUGO, show_pwm, store_pwm, 1, 0);
2059 static SENSOR_DEVICE_ATTR_2(pwm3, S_IWUSR | S_IRUGO, show_pwm, store_pwm, 2, 0);
2060 static SENSOR_DEVICE_ATTR_2(pwm4, S_IWUSR | S_IRUGO, show_pwm, store_pwm, 3, 0);
2061 static SENSOR_DEVICE_ATTR_2(pwm5, S_IWUSR | S_IRUGO, show_pwm, store_pwm, 4, 0);
2063 static SENSOR_DEVICE_ATTR(pwm1_mode, S_IWUSR | S_IRUGO, show_pwm_mode,
2065 static SENSOR_DEVICE_ATTR(pwm2_mode, S_IWUSR | S_IRUGO, show_pwm_mode,
2067 static SENSOR_DEVICE_ATTR(pwm3_mode, S_IWUSR | S_IRUGO, show_pwm_mode,
2069 static SENSOR_DEVICE_ATTR(pwm4_mode, S_IWUSR | S_IRUGO, show_pwm_mode,
2071 static SENSOR_DEVICE_ATTR(pwm5_mode, S_IWUSR | S_IRUGO, show_pwm_mode,
2074 static SENSOR_DEVICE_ATTR(pwm1_enable, S_IWUSR | S_IRUGO, show_pwm_enable,
2075 store_pwm_enable, 0);
2076 static SENSOR_DEVICE_ATTR(pwm2_enable, S_IWUSR | S_IRUGO, show_pwm_enable,
2077 store_pwm_enable, 1);
2078 static SENSOR_DEVICE_ATTR(pwm3_enable, S_IWUSR | S_IRUGO, show_pwm_enable,
2079 store_pwm_enable, 2);
2080 static SENSOR_DEVICE_ATTR(pwm4_enable, S_IWUSR | S_IRUGO, show_pwm_enable,
2081 store_pwm_enable, 3);
2082 static SENSOR_DEVICE_ATTR(pwm5_enable, S_IWUSR | S_IRUGO, show_pwm_enable,
2083 store_pwm_enable, 4);
2085 static SENSOR_DEVICE_ATTR_2(pwm1_temp_sel, S_IWUSR | S_IRUGO,
2086 show_pwm_temp_sel, store_pwm_temp_sel, 0, 0);
2087 static SENSOR_DEVICE_ATTR_2(pwm2_temp_sel, S_IWUSR | S_IRUGO,
2088 show_pwm_temp_sel, store_pwm_temp_sel, 1, 0);
2089 static SENSOR_DEVICE_ATTR_2(pwm3_temp_sel, S_IWUSR | S_IRUGO,
2090 show_pwm_temp_sel, store_pwm_temp_sel, 2, 0);
2091 static SENSOR_DEVICE_ATTR_2(pwm4_temp_sel, S_IWUSR | S_IRUGO,
2092 show_pwm_temp_sel, store_pwm_temp_sel, 3, 0);
2093 static SENSOR_DEVICE_ATTR_2(pwm5_temp_sel, S_IWUSR | S_IRUGO,
2094 show_pwm_temp_sel, store_pwm_temp_sel, 4, 0);
2096 static SENSOR_DEVICE_ATTR(pwm1_target, S_IWUSR | S_IRUGO, show_target_temp,
2097 store_target_temp, 0);
2098 static SENSOR_DEVICE_ATTR(pwm2_target, S_IWUSR | S_IRUGO, show_target_temp,
2099 store_target_temp, 1);
2100 static SENSOR_DEVICE_ATTR(pwm3_target, S_IWUSR | S_IRUGO, show_target_temp,
2101 store_target_temp, 2);
2102 static SENSOR_DEVICE_ATTR(pwm4_target, S_IWUSR | S_IRUGO, show_target_temp,
2103 store_target_temp, 3);
2104 static SENSOR_DEVICE_ATTR(pwm5_target, S_IWUSR | S_IRUGO, show_target_temp,
2105 store_target_temp, 4);
2107 /* Smart Fan registers */
2110 show_pwm_sel_enable(struct device *dev, struct device_attribute *attr,
2113 struct nct6775_data *data = nct6775_update_device(dev);
2114 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
2116 return sprintf(buf, "%d\n",
2117 data->pwm_sel_enable[sattr->index][sattr->nr]);
2121 store_pwm_sel_enable(struct device *dev, struct device_attribute *attr,
2122 const char *buf, size_t count)
2124 struct nct6775_data *data = dev_get_drvdata(dev);
2125 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
2127 int index = sattr->index;
2131 static const u8 bit[] = { 0x80, 0x80, 0x01 };
2134 err = kstrtoul(buf, 10, &val);
2141 mutex_lock(&data->update_lock);
2142 data->pwm_sel_enable[index][nr] = val;
2143 reg = nct6775_read_value(data, data->REG_TEMP_SEL[index][nr]);
2147 nct6775_write_value(data, data->REG_TEMP_SEL[index][nr], reg);
2148 mutex_unlock(&data->update_lock);
2153 show_weight_temp(struct device *dev, struct device_attribute *attr, char *buf)
2155 struct nct6775_data *data = nct6775_update_device(dev);
2156 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
2158 int index = sattr->index;
2160 return sprintf(buf, "%d\n", data->weight_temp[index][nr] * 1000);
2164 store_weight_temp(struct device *dev, struct device_attribute *attr,
2165 const char *buf, size_t count)
2167 struct nct6775_data *data = dev_get_drvdata(dev);
2168 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
2170 int index = sattr->index;
2174 err = kstrtoul(buf, 10, &val);
2178 val = SENSORS_LIMIT(DIV_ROUND_CLOSEST(val, 1000), 0, 255);
2180 mutex_lock(&data->update_lock);
2181 data->weight_temp[index][nr] = val;
2182 nct6775_write_value(data, data->REG_WEIGHT_TEMP[index][nr], val);
2183 mutex_unlock(&data->update_lock);
2187 static SENSOR_DEVICE_ATTR_2(pwm1_stop_output_enable, S_IWUSR | S_IRUGO,
2188 show_pwm_sel_enable, store_pwm_sel_enable, 0, 0);
2189 static SENSOR_DEVICE_ATTR_2(pwm2_stop_output_enable, S_IWUSR | S_IRUGO,
2190 show_pwm_sel_enable, store_pwm_sel_enable, 1, 0);
2191 static SENSOR_DEVICE_ATTR_2(pwm3_stop_output_enable, S_IWUSR | S_IRUGO,
2192 show_pwm_sel_enable, store_pwm_sel_enable, 2, 0);
2193 static SENSOR_DEVICE_ATTR_2(pwm4_stop_output_enable, S_IWUSR | S_IRUGO,
2194 show_pwm_sel_enable, store_pwm_sel_enable, 3, 0);
2195 static SENSOR_DEVICE_ATTR_2(pwm5_stop_output_enable, S_IWUSR | S_IRUGO,
2196 show_pwm_sel_enable, store_pwm_sel_enable, 4, 0);
2198 static SENSOR_DEVICE_ATTR_2(pwm1_weight_enable, S_IWUSR | S_IRUGO,
2199 show_pwm_sel_enable, store_pwm_sel_enable, 0, 1);
2200 static SENSOR_DEVICE_ATTR_2(pwm2_weight_enable, S_IWUSR | S_IRUGO,
2201 show_pwm_sel_enable, store_pwm_sel_enable, 1, 1);
2202 static SENSOR_DEVICE_ATTR_2(pwm3_weight_enable, S_IWUSR | S_IRUGO,
2203 show_pwm_sel_enable, store_pwm_sel_enable, 2, 1);
2204 static SENSOR_DEVICE_ATTR_2(pwm4_weight_enable, S_IWUSR | S_IRUGO,
2205 show_pwm_sel_enable, store_pwm_sel_enable, 3, 1);
2206 static SENSOR_DEVICE_ATTR_2(pwm5_weight_enable, S_IWUSR | S_IRUGO,
2207 show_pwm_sel_enable, store_pwm_sel_enable, 4, 1);
2209 static SENSOR_DEVICE_ATTR_2(pwm1_step_enable, S_IWUSR | S_IRUGO,
2210 show_pwm_sel_enable, store_pwm_sel_enable, 0, 2);
2211 static SENSOR_DEVICE_ATTR_2(pwm2_step_enable, S_IWUSR | S_IRUGO,
2212 show_pwm_sel_enable, store_pwm_sel_enable, 1, 2);
2213 static SENSOR_DEVICE_ATTR_2(pwm3_step_enable, S_IWUSR | S_IRUGO,
2214 show_pwm_sel_enable, store_pwm_sel_enable, 2, 2);
2215 static SENSOR_DEVICE_ATTR_2(pwm4_step_enable, S_IWUSR | S_IRUGO,
2216 show_pwm_sel_enable, store_pwm_sel_enable, 3, 2);
2217 static SENSOR_DEVICE_ATTR_2(pwm5_step_enable, S_IWUSR | S_IRUGO,
2218 show_pwm_sel_enable, store_pwm_sel_enable, 4, 2);
2220 static SENSOR_DEVICE_ATTR_2(pwm1_weight_temp_sel, S_IWUSR | S_IRUGO,
2221 show_pwm_temp_sel, store_pwm_temp_sel, 0, 1);
2222 static SENSOR_DEVICE_ATTR_2(pwm2_weight_temp_sel, S_IWUSR | S_IRUGO,
2223 show_pwm_temp_sel, store_pwm_temp_sel, 1, 1);
2224 static SENSOR_DEVICE_ATTR_2(pwm3_weight_temp_sel, S_IWUSR | S_IRUGO,
2225 show_pwm_temp_sel, store_pwm_temp_sel, 2, 1);
2226 static SENSOR_DEVICE_ATTR_2(pwm4_weight_temp_sel, S_IWUSR | S_IRUGO,
2227 show_pwm_temp_sel, store_pwm_temp_sel, 3, 1);
2228 static SENSOR_DEVICE_ATTR_2(pwm5_weight_temp_sel, S_IWUSR | S_IRUGO,
2229 show_pwm_temp_sel, store_pwm_temp_sel, 4, 1);
2231 static SENSOR_DEVICE_ATTR_2(pwm1_weight_temp_step, S_IWUSR | S_IRUGO,
2232 show_weight_temp, store_weight_temp, 0, 0);
2233 static SENSOR_DEVICE_ATTR_2(pwm2_weight_temp_step, S_IWUSR | S_IRUGO,
2234 show_weight_temp, store_weight_temp, 1, 0);
2235 static SENSOR_DEVICE_ATTR_2(pwm3_weight_temp_step, S_IWUSR | S_IRUGO,
2236 show_weight_temp, store_weight_temp, 2, 0);
2237 static SENSOR_DEVICE_ATTR_2(pwm4_weight_temp_step, S_IWUSR | S_IRUGO,
2238 show_weight_temp, store_weight_temp, 3, 0);
2239 static SENSOR_DEVICE_ATTR_2(pwm5_weight_temp_step, S_IWUSR | S_IRUGO,
2240 show_weight_temp, store_weight_temp, 4, 0);
2242 static SENSOR_DEVICE_ATTR_2(pwm1_weight_temp_step_tol, S_IWUSR | S_IRUGO,
2243 show_weight_temp, store_weight_temp, 0, 1);
2244 static SENSOR_DEVICE_ATTR_2(pwm2_weight_temp_step_tol, S_IWUSR | S_IRUGO,
2245 show_weight_temp, store_weight_temp, 1, 1);
2246 static SENSOR_DEVICE_ATTR_2(pwm3_weight_temp_step_tol, S_IWUSR | S_IRUGO,
2247 show_weight_temp, store_weight_temp, 2, 1);
2248 static SENSOR_DEVICE_ATTR_2(pwm4_weight_temp_step_tol, S_IWUSR | S_IRUGO,
2249 show_weight_temp, store_weight_temp, 3, 1);
2250 static SENSOR_DEVICE_ATTR_2(pwm5_weight_temp_step_tol, S_IWUSR | S_IRUGO,
2251 show_weight_temp, store_weight_temp, 4, 1);
2253 static SENSOR_DEVICE_ATTR_2(pwm1_weight_temp_step_base, S_IWUSR | S_IRUGO,
2254 show_weight_temp, store_weight_temp, 0, 2);
2255 static SENSOR_DEVICE_ATTR_2(pwm2_weight_temp_step_base, S_IWUSR | S_IRUGO,
2256 show_weight_temp, store_weight_temp, 1, 2);
2257 static SENSOR_DEVICE_ATTR_2(pwm3_weight_temp_step_base, S_IWUSR | S_IRUGO,
2258 show_weight_temp, store_weight_temp, 2, 2);
2259 static SENSOR_DEVICE_ATTR_2(pwm4_weight_temp_step_base, S_IWUSR | S_IRUGO,
2260 show_weight_temp, store_weight_temp, 3, 2);
2261 static SENSOR_DEVICE_ATTR_2(pwm5_weight_temp_step_base, S_IWUSR | S_IRUGO,
2262 show_weight_temp, store_weight_temp, 4, 2);
2264 static SENSOR_DEVICE_ATTR_2(pwm1_weight_duty_step, S_IWUSR | S_IRUGO,
2265 show_pwm, store_pwm, 0, 5);
2266 static SENSOR_DEVICE_ATTR_2(pwm2_weight_duty_step, S_IWUSR | S_IRUGO,
2267 show_pwm, store_pwm, 1, 5);
2268 static SENSOR_DEVICE_ATTR_2(pwm3_weight_duty_step, S_IWUSR | S_IRUGO,
2269 show_pwm, store_pwm, 2, 5);
2270 static SENSOR_DEVICE_ATTR_2(pwm4_weight_duty_step, S_IWUSR | S_IRUGO,
2271 show_pwm, store_pwm, 3, 5);
2272 static SENSOR_DEVICE_ATTR_2(pwm5_weight_duty_step, S_IWUSR | S_IRUGO,
2273 show_pwm, store_pwm, 4, 5);
2275 /* duty_base is not supported on all chips */
2276 static struct sensor_device_attribute_2 sda_weight_duty_base[] = {
2277 SENSOR_ATTR_2(pwm1_weight_duty_base, S_IWUSR | S_IRUGO,
2278 show_pwm, store_pwm, 0, 6),
2279 SENSOR_ATTR_2(pwm2_weight_duty_base, S_IWUSR | S_IRUGO,
2280 show_pwm, store_pwm, 1, 6),
2281 SENSOR_ATTR_2(pwm3_weight_duty_base, S_IWUSR | S_IRUGO,
2282 show_pwm, store_pwm, 2, 6),
2283 SENSOR_ATTR_2(pwm4_weight_duty_base, S_IWUSR | S_IRUGO,
2284 show_pwm, store_pwm, 3, 6),
2285 SENSOR_ATTR_2(pwm5_weight_duty_base, S_IWUSR | S_IRUGO,
2286 show_pwm, store_pwm, 4, 6),
2290 show_fan_time(struct device *dev, struct device_attribute *attr, char *buf)
2292 struct nct6775_data *data = nct6775_update_device(dev);
2293 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
2295 int index = sattr->index;
2297 return sprintf(buf, "%d\n",
2298 step_time_from_reg(data->fan_time[index][nr],
2299 data->pwm_mode[nr]));
2303 store_fan_time(struct device *dev, struct device_attribute *attr,
2304 const char *buf, size_t count)
2306 struct nct6775_data *data = dev_get_drvdata(dev);
2307 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
2309 int index = sattr->index;
2313 err = kstrtoul(buf, 10, &val);
2317 val = step_time_to_reg(val, data->pwm_mode[nr]);
2318 mutex_lock(&data->update_lock);
2319 data->fan_time[index][nr] = val;
2320 nct6775_write_value(data, data->REG_FAN_TIME[index][nr], val);
2321 mutex_unlock(&data->update_lock);
2326 show_name(struct device *dev, struct device_attribute *attr, char *buf)
2328 struct nct6775_data *data = dev_get_drvdata(dev);
2330 return sprintf(buf, "%s\n", data->name);
2333 static DEVICE_ATTR(name, S_IRUGO, show_name, NULL);
2335 static SENSOR_DEVICE_ATTR_2(pwm1_stop_time, S_IWUSR | S_IRUGO, show_fan_time,
2336 store_fan_time, 0, 0);
2337 static SENSOR_DEVICE_ATTR_2(pwm2_stop_time, S_IWUSR | S_IRUGO, show_fan_time,
2338 store_fan_time, 1, 0);
2339 static SENSOR_DEVICE_ATTR_2(pwm3_stop_time, S_IWUSR | S_IRUGO, show_fan_time,
2340 store_fan_time, 2, 0);
2341 static SENSOR_DEVICE_ATTR_2(pwm4_stop_time, S_IWUSR | S_IRUGO, show_fan_time,
2342 store_fan_time, 3, 0);
2343 static SENSOR_DEVICE_ATTR_2(pwm5_stop_time, S_IWUSR | S_IRUGO, show_fan_time,
2344 store_fan_time, 4, 0);
2346 static SENSOR_DEVICE_ATTR_2(pwm1_step_up_time, S_IWUSR | S_IRUGO, show_fan_time,
2347 store_fan_time, 0, 1);
2348 static SENSOR_DEVICE_ATTR_2(pwm2_step_up_time, S_IWUSR | S_IRUGO, show_fan_time,
2349 store_fan_time, 1, 1);
2350 static SENSOR_DEVICE_ATTR_2(pwm3_step_up_time, S_IWUSR | S_IRUGO, show_fan_time,
2351 store_fan_time, 2, 1);
2352 static SENSOR_DEVICE_ATTR_2(pwm4_step_up_time, S_IWUSR | S_IRUGO, show_fan_time,
2353 store_fan_time, 3, 1);
2354 static SENSOR_DEVICE_ATTR_2(pwm5_step_up_time, S_IWUSR | S_IRUGO, show_fan_time,
2355 store_fan_time, 4, 1);
2357 static SENSOR_DEVICE_ATTR_2(pwm1_step_down_time, S_IWUSR | S_IRUGO,
2358 show_fan_time, store_fan_time, 0, 2);
2359 static SENSOR_DEVICE_ATTR_2(pwm2_step_down_time, S_IWUSR | S_IRUGO,
2360 show_fan_time, store_fan_time, 1, 2);
2361 static SENSOR_DEVICE_ATTR_2(pwm3_step_down_time, S_IWUSR | S_IRUGO,
2362 show_fan_time, store_fan_time, 2, 2);
2363 static SENSOR_DEVICE_ATTR_2(pwm4_step_down_time, S_IWUSR | S_IRUGO,
2364 show_fan_time, store_fan_time, 3, 2);
2365 static SENSOR_DEVICE_ATTR_2(pwm5_step_down_time, S_IWUSR | S_IRUGO,
2366 show_fan_time, store_fan_time, 4, 2);
2368 static SENSOR_DEVICE_ATTR_2(pwm1_start_output, S_IWUSR | S_IRUGO, show_pwm,
2370 static SENSOR_DEVICE_ATTR_2(pwm2_start_output, S_IWUSR | S_IRUGO, show_pwm,
2372 static SENSOR_DEVICE_ATTR_2(pwm3_start_output, S_IWUSR | S_IRUGO, show_pwm,
2374 static SENSOR_DEVICE_ATTR_2(pwm4_start_output, S_IWUSR | S_IRUGO, show_pwm,
2376 static SENSOR_DEVICE_ATTR_2(pwm5_start_output, S_IWUSR | S_IRUGO, show_pwm,
2379 static SENSOR_DEVICE_ATTR_2(pwm1_stop_output, S_IWUSR | S_IRUGO, show_pwm,
2381 static SENSOR_DEVICE_ATTR_2(pwm2_stop_output, S_IWUSR | S_IRUGO, show_pwm,
2383 static SENSOR_DEVICE_ATTR_2(pwm3_stop_output, S_IWUSR | S_IRUGO, show_pwm,
2385 static SENSOR_DEVICE_ATTR_2(pwm4_stop_output, S_IWUSR | S_IRUGO, show_pwm,
2387 static SENSOR_DEVICE_ATTR_2(pwm5_stop_output, S_IWUSR | S_IRUGO, show_pwm,
2390 /* max_output is not supported on all chips */
2391 static struct sensor_device_attribute_2 sda_max_output[] = {
2392 SENSOR_ATTR_2(pwm1_max_output, S_IWUSR | S_IRUGO, show_pwm, store_pwm,
2394 SENSOR_ATTR_2(pwm2_max_output, S_IWUSR | S_IRUGO, show_pwm, store_pwm,
2396 SENSOR_ATTR_2(pwm3_max_output, S_IWUSR | S_IRUGO, show_pwm, store_pwm,
2398 SENSOR_ATTR_2(pwm4_max_output, S_IWUSR | S_IRUGO, show_pwm, store_pwm,
2400 SENSOR_ATTR_2(pwm5_max_output, S_IWUSR | S_IRUGO, show_pwm, store_pwm,
2404 /* step_output is not supported on all chips */
2405 static struct sensor_device_attribute_2 sda_step_output[] = {
2406 SENSOR_ATTR_2(pwm1_step_output, S_IWUSR | S_IRUGO, show_pwm, store_pwm,
2408 SENSOR_ATTR_2(pwm2_step_output, S_IWUSR | S_IRUGO, show_pwm, store_pwm,
2410 SENSOR_ATTR_2(pwm3_step_output, S_IWUSR | S_IRUGO, show_pwm, store_pwm,
2412 SENSOR_ATTR_2(pwm4_step_output, S_IWUSR | S_IRUGO, show_pwm, store_pwm,
2414 SENSOR_ATTR_2(pwm5_step_output, S_IWUSR | S_IRUGO, show_pwm, store_pwm,
2418 static struct attribute *nct6775_attributes_pwm[5][20] = {
2420 &sensor_dev_attr_pwm1.dev_attr.attr,
2421 &sensor_dev_attr_pwm1_mode.dev_attr.attr,
2422 &sensor_dev_attr_pwm1_enable.dev_attr.attr,
2423 &sensor_dev_attr_pwm1_stop_output_enable.dev_attr.attr,
2424 &sensor_dev_attr_pwm1_weight_enable.dev_attr.attr,
2425 &sensor_dev_attr_pwm1_step_enable.dev_attr.attr,
2426 &sensor_dev_attr_pwm1_temp_sel.dev_attr.attr,
2427 &sensor_dev_attr_pwm1_target.dev_attr.attr,
2428 &sensor_dev_attr_pwm1_stop_time.dev_attr.attr,
2429 &sensor_dev_attr_pwm1_step_up_time.dev_attr.attr,
2430 &sensor_dev_attr_pwm1_step_down_time.dev_attr.attr,
2431 &sensor_dev_attr_pwm1_start_output.dev_attr.attr,
2432 &sensor_dev_attr_pwm1_stop_output.dev_attr.attr,
2433 &sensor_dev_attr_pwm1_weight_temp_sel.dev_attr.attr,
2434 &sensor_dev_attr_pwm1_weight_temp_step.dev_attr.attr,
2435 &sensor_dev_attr_pwm1_weight_temp_step_tol.dev_attr.attr,
2436 &sensor_dev_attr_pwm1_weight_temp_step_base.dev_attr.attr,
2437 &sensor_dev_attr_pwm1_weight_duty_step.dev_attr.attr,
2441 &sensor_dev_attr_pwm2.dev_attr.attr,
2442 &sensor_dev_attr_pwm2_mode.dev_attr.attr,
2443 &sensor_dev_attr_pwm2_enable.dev_attr.attr,
2444 &sensor_dev_attr_pwm2_stop_output_enable.dev_attr.attr,
2445 &sensor_dev_attr_pwm2_weight_enable.dev_attr.attr,
2446 &sensor_dev_attr_pwm2_step_enable.dev_attr.attr,
2447 &sensor_dev_attr_pwm2_temp_sel.dev_attr.attr,
2448 &sensor_dev_attr_pwm2_target.dev_attr.attr,
2449 &sensor_dev_attr_pwm2_stop_time.dev_attr.attr,
2450 &sensor_dev_attr_pwm2_step_up_time.dev_attr.attr,
2451 &sensor_dev_attr_pwm2_step_down_time.dev_attr.attr,
2452 &sensor_dev_attr_pwm2_start_output.dev_attr.attr,
2453 &sensor_dev_attr_pwm2_stop_output.dev_attr.attr,
2454 &sensor_dev_attr_pwm2_weight_temp_sel.dev_attr.attr,
2455 &sensor_dev_attr_pwm2_weight_temp_step.dev_attr.attr,
2456 &sensor_dev_attr_pwm2_weight_temp_step_tol.dev_attr.attr,
2457 &sensor_dev_attr_pwm2_weight_temp_step_base.dev_attr.attr,
2458 &sensor_dev_attr_pwm2_weight_duty_step.dev_attr.attr,
2462 &sensor_dev_attr_pwm3.dev_attr.attr,
2463 &sensor_dev_attr_pwm3_mode.dev_attr.attr,
2464 &sensor_dev_attr_pwm3_enable.dev_attr.attr,
2465 &sensor_dev_attr_pwm3_stop_output_enable.dev_attr.attr,
2466 &sensor_dev_attr_pwm3_weight_enable.dev_attr.attr,
2467 &sensor_dev_attr_pwm3_step_enable.dev_attr.attr,
2468 &sensor_dev_attr_pwm3_temp_sel.dev_attr.attr,
2469 &sensor_dev_attr_pwm3_target.dev_attr.attr,
2470 &sensor_dev_attr_pwm3_stop_time.dev_attr.attr,
2471 &sensor_dev_attr_pwm3_step_up_time.dev_attr.attr,
2472 &sensor_dev_attr_pwm3_step_down_time.dev_attr.attr,
2473 &sensor_dev_attr_pwm3_start_output.dev_attr.attr,
2474 &sensor_dev_attr_pwm3_stop_output.dev_attr.attr,
2475 &sensor_dev_attr_pwm3_weight_temp_sel.dev_attr.attr,
2476 &sensor_dev_attr_pwm3_weight_temp_step.dev_attr.attr,
2477 &sensor_dev_attr_pwm3_weight_temp_step_tol.dev_attr.attr,
2478 &sensor_dev_attr_pwm3_weight_temp_step_base.dev_attr.attr,
2479 &sensor_dev_attr_pwm3_weight_duty_step.dev_attr.attr,
2483 &sensor_dev_attr_pwm4.dev_attr.attr,
2484 &sensor_dev_attr_pwm4_mode.dev_attr.attr,
2485 &sensor_dev_attr_pwm4_enable.dev_attr.attr,
2486 &sensor_dev_attr_pwm4_stop_output_enable.dev_attr.attr,
2487 &sensor_dev_attr_pwm4_weight_enable.dev_attr.attr,
2488 &sensor_dev_attr_pwm4_step_enable.dev_attr.attr,
2489 &sensor_dev_attr_pwm4_temp_sel.dev_attr.attr,
2490 &sensor_dev_attr_pwm4_target.dev_attr.attr,
2491 &sensor_dev_attr_pwm4_stop_time.dev_attr.attr,
2492 &sensor_dev_attr_pwm4_step_up_time.dev_attr.attr,
2493 &sensor_dev_attr_pwm4_step_down_time.dev_attr.attr,
2494 &sensor_dev_attr_pwm4_start_output.dev_attr.attr,
2495 &sensor_dev_attr_pwm4_stop_output.dev_attr.attr,
2496 &sensor_dev_attr_pwm4_weight_temp_sel.dev_attr.attr,
2497 &sensor_dev_attr_pwm4_weight_temp_step.dev_attr.attr,
2498 &sensor_dev_attr_pwm4_weight_temp_step_tol.dev_attr.attr,
2499 &sensor_dev_attr_pwm4_weight_temp_step_base.dev_attr.attr,
2500 &sensor_dev_attr_pwm4_weight_duty_step.dev_attr.attr,
2504 &sensor_dev_attr_pwm5.dev_attr.attr,
2505 &sensor_dev_attr_pwm5_mode.dev_attr.attr,
2506 &sensor_dev_attr_pwm5_enable.dev_attr.attr,
2507 &sensor_dev_attr_pwm5_stop_output_enable.dev_attr.attr,
2508 &sensor_dev_attr_pwm5_weight_enable.dev_attr.attr,
2509 &sensor_dev_attr_pwm5_step_enable.dev_attr.attr,
2510 &sensor_dev_attr_pwm5_temp_sel.dev_attr.attr,
2511 &sensor_dev_attr_pwm5_target.dev_attr.attr,
2512 &sensor_dev_attr_pwm5_stop_time.dev_attr.attr,
2513 &sensor_dev_attr_pwm5_step_up_time.dev_attr.attr,
2514 &sensor_dev_attr_pwm5_step_down_time.dev_attr.attr,
2515 &sensor_dev_attr_pwm5_start_output.dev_attr.attr,
2516 &sensor_dev_attr_pwm5_stop_output.dev_attr.attr,
2517 &sensor_dev_attr_pwm5_weight_temp_sel.dev_attr.attr,
2518 &sensor_dev_attr_pwm5_weight_temp_step.dev_attr.attr,
2519 &sensor_dev_attr_pwm5_weight_temp_step_tol.dev_attr.attr,
2520 &sensor_dev_attr_pwm5_weight_temp_step_base.dev_attr.attr,
2521 &sensor_dev_attr_pwm5_weight_duty_step.dev_attr.attr,
2526 static const struct attribute_group nct6775_group_pwm[5] = {
2527 { .attrs = nct6775_attributes_pwm[0] },
2528 { .attrs = nct6775_attributes_pwm[1] },
2529 { .attrs = nct6775_attributes_pwm[2] },
2530 { .attrs = nct6775_attributes_pwm[3] },
2531 { .attrs = nct6775_attributes_pwm[4] },
2535 show_auto_pwm(struct device *dev, struct device_attribute *attr, char *buf)
2537 struct nct6775_data *data = nct6775_update_device(dev);
2538 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
2540 return sprintf(buf, "%d\n", data->auto_pwm[sattr->nr][sattr->index]);
2544 store_auto_pwm(struct device *dev, struct device_attribute *attr,
2545 const char *buf, size_t count)
2547 struct nct6775_data *data = dev_get_drvdata(dev);
2548 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
2550 int point = sattr->index;
2555 err = kstrtoul(buf, 10, &val);
2561 if (point == data->auto_pwm_num) {
2562 if (data->kind != nct6775 && !val)
2564 if (data->kind != nct6779 && val)
2568 mutex_lock(&data->update_lock);
2569 data->auto_pwm[nr][point] = val;
2570 if (point < data->auto_pwm_num) {
2571 nct6775_write_value(data,
2572 NCT6775_AUTO_PWM(data, nr, point),
2573 data->auto_pwm[nr][point]);
2575 switch (data->kind) {
2577 /* disable if needed (pwm == 0) */
2578 reg = nct6775_read_value(data,
2579 NCT6775_REG_CRITICAL_ENAB[nr]);
2584 nct6775_write_value(data, NCT6775_REG_CRITICAL_ENAB[nr],
2588 break; /* always enabled, nothing to do */
2590 nct6775_write_value(data, NCT6779_REG_CRITICAL_PWM[nr],
2592 reg = nct6775_read_value(data,
2593 NCT6779_REG_CRITICAL_PWM_ENABLE[nr]);
2598 nct6775_write_value(data,
2599 NCT6779_REG_CRITICAL_PWM_ENABLE[nr],
2604 mutex_unlock(&data->update_lock);
2609 show_auto_temp(struct device *dev, struct device_attribute *attr, char *buf)
2611 struct nct6775_data *data = nct6775_update_device(dev);
2612 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
2614 int point = sattr->index;
2617 * We don't know for sure if the temperature is signed or unsigned.
2618 * Assume it is unsigned.
2620 return sprintf(buf, "%d\n", data->auto_temp[nr][point] * 1000);
2624 store_auto_temp(struct device *dev, struct device_attribute *attr,
2625 const char *buf, size_t count)
2627 struct nct6775_data *data = dev_get_drvdata(dev);
2628 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
2630 int point = sattr->index;
2634 err = kstrtoul(buf, 10, &val);
2640 mutex_lock(&data->update_lock);
2641 data->auto_temp[nr][point] = DIV_ROUND_CLOSEST(val, 1000);
2642 if (point < data->auto_pwm_num) {
2643 nct6775_write_value(data,
2644 NCT6775_AUTO_TEMP(data, nr, point),
2645 data->auto_temp[nr][point]);
2647 nct6775_write_value(data, data->REG_CRITICAL_TEMP[nr],
2648 data->auto_temp[nr][point]);
2650 mutex_unlock(&data->update_lock);
2655 * The number of auto-point trip points is chip dependent.
2656 * Need to check support while generating/removing attribute files.
2658 static struct sensor_device_attribute_2 sda_auto_pwm_arrays[] = {
2659 SENSOR_ATTR_2(pwm1_auto_point1_pwm, S_IWUSR | S_IRUGO,
2660 show_auto_pwm, store_auto_pwm, 0, 0),
2661 SENSOR_ATTR_2(pwm1_auto_point1_temp, S_IWUSR | S_IRUGO,
2662 show_auto_temp, store_auto_temp, 0, 0),
2663 SENSOR_ATTR_2(pwm1_auto_point1_temp_hyst, S_IWUSR | S_IRUGO,
2664 show_auto_temp_hyst, store_auto_temp_hyst, 0, 0),
2665 SENSOR_ATTR_2(pwm1_auto_point2_pwm, S_IWUSR | S_IRUGO,
2666 show_auto_pwm, store_auto_pwm, 0, 1),
2667 SENSOR_ATTR_2(pwm1_auto_point2_temp, S_IWUSR | S_IRUGO,
2668 show_auto_temp, store_auto_temp, 0, 1),
2669 SENSOR_ATTR_2(pwm1_auto_point2_temp_hyst, S_IWUSR | S_IRUGO,
2670 show_auto_temp_hyst, store_auto_temp_hyst, 0, 1),
2671 SENSOR_ATTR_2(pwm1_auto_point3_pwm, S_IWUSR | S_IRUGO,
2672 show_auto_pwm, store_auto_pwm, 0, 2),
2673 SENSOR_ATTR_2(pwm1_auto_point3_temp, S_IWUSR | S_IRUGO,
2674 show_auto_temp, store_auto_temp, 0, 2),
2675 SENSOR_ATTR_2(pwm1_auto_point3_temp_hyst, S_IWUSR | S_IRUGO,
2676 show_auto_temp_hyst, store_auto_temp_hyst, 0, 2),
2677 SENSOR_ATTR_2(pwm1_auto_point4_pwm, S_IWUSR | S_IRUGO,
2678 show_auto_pwm, store_auto_pwm, 0, 3),
2679 SENSOR_ATTR_2(pwm1_auto_point4_temp, S_IWUSR | S_IRUGO,
2680 show_auto_temp, store_auto_temp, 0, 3),
2681 SENSOR_ATTR_2(pwm1_auto_point4_temp_hyst, S_IWUSR | S_IRUGO,
2682 show_auto_temp_hyst, store_auto_temp_hyst, 0, 3),
2683 SENSOR_ATTR_2(pwm1_auto_point5_pwm, S_IWUSR | S_IRUGO,
2684 show_auto_pwm, store_auto_pwm, 0, 4),
2685 SENSOR_ATTR_2(pwm1_auto_point5_temp, S_IWUSR | S_IRUGO,
2686 show_auto_temp, store_auto_temp, 0, 4),
2687 SENSOR_ATTR_2(pwm1_auto_point5_temp_hyst, S_IWUSR | S_IRUGO,
2688 show_auto_temp_hyst, store_auto_temp_hyst, 0, 4),
2689 SENSOR_ATTR_2(pwm1_auto_point6_pwm, S_IWUSR | S_IRUGO,
2690 show_auto_pwm, store_auto_pwm, 0, 5),
2691 SENSOR_ATTR_2(pwm1_auto_point6_temp, S_IWUSR | S_IRUGO,
2692 show_auto_temp, store_auto_temp, 0, 5),
2693 SENSOR_ATTR_2(pwm1_auto_point6_temp_hyst, S_IWUSR | S_IRUGO,
2694 show_auto_temp_hyst, store_auto_temp_hyst, 0, 5),
2695 SENSOR_ATTR_2(pwm1_auto_point7_pwm, S_IWUSR | S_IRUGO,
2696 show_auto_pwm, store_auto_pwm, 0, 6),
2697 SENSOR_ATTR_2(pwm1_auto_point7_temp, S_IWUSR | S_IRUGO,
2698 show_auto_temp, store_auto_temp, 0, 6),
2699 SENSOR_ATTR_2(pwm1_auto_point7_temp_hyst, S_IWUSR | S_IRUGO,
2700 show_auto_temp_hyst, store_auto_temp_hyst, 0, 6),
2702 SENSOR_ATTR_2(pwm2_auto_point1_pwm, S_IWUSR | S_IRUGO,
2703 show_auto_pwm, store_auto_pwm, 1, 0),
2704 SENSOR_ATTR_2(pwm2_auto_point1_temp, S_IWUSR | S_IRUGO,
2705 show_auto_temp, store_auto_temp, 1, 0),
2706 SENSOR_ATTR_2(pwm2_auto_point1_temp_hyst, S_IWUSR | S_IRUGO,
2707 show_auto_temp_hyst, store_auto_temp_hyst, 1, 0),
2708 SENSOR_ATTR_2(pwm2_auto_point2_pwm, S_IWUSR | S_IRUGO,
2709 show_auto_pwm, store_auto_pwm, 1, 1),
2710 SENSOR_ATTR_2(pwm2_auto_point2_temp, S_IWUSR | S_IRUGO,
2711 show_auto_temp, store_auto_temp, 1, 1),
2712 SENSOR_ATTR_2(pwm2_auto_point2_temp_hyst, S_IWUSR | S_IRUGO,
2713 show_auto_temp_hyst, store_auto_temp_hyst, 1, 1),
2714 SENSOR_ATTR_2(pwm2_auto_point3_pwm, S_IWUSR | S_IRUGO,
2715 show_auto_pwm, store_auto_pwm, 1, 2),
2716 SENSOR_ATTR_2(pwm2_auto_point3_temp, S_IWUSR | S_IRUGO,
2717 show_auto_temp, store_auto_temp, 1, 2),
2718 SENSOR_ATTR_2(pwm2_auto_point3_temp_hyst, S_IWUSR | S_IRUGO,
2719 show_auto_temp_hyst, store_auto_temp_hyst, 1, 2),
2720 SENSOR_ATTR_2(pwm2_auto_point4_pwm, S_IWUSR | S_IRUGO,
2721 show_auto_pwm, store_auto_pwm, 1, 3),
2722 SENSOR_ATTR_2(pwm2_auto_point4_temp, S_IWUSR | S_IRUGO,
2723 show_auto_temp, store_auto_temp, 1, 3),
2724 SENSOR_ATTR_2(pwm2_auto_point4_temp_hyst, S_IWUSR | S_IRUGO,
2725 show_auto_temp_hyst, store_auto_temp_hyst, 1, 3),
2726 SENSOR_ATTR_2(pwm2_auto_point5_pwm, S_IWUSR | S_IRUGO,
2727 show_auto_pwm, store_auto_pwm, 1, 4),
2728 SENSOR_ATTR_2(pwm2_auto_point5_temp, S_IWUSR | S_IRUGO,
2729 show_auto_temp, store_auto_temp, 1, 4),
2730 SENSOR_ATTR_2(pwm2_auto_point5_temp_hyst, S_IWUSR | S_IRUGO,
2731 show_auto_temp_hyst, store_auto_temp_hyst, 1, 4),
2732 SENSOR_ATTR_2(pwm2_auto_point6_pwm, S_IWUSR | S_IRUGO,
2733 show_auto_pwm, store_auto_pwm, 1, 5),
2734 SENSOR_ATTR_2(pwm2_auto_point6_temp, S_IWUSR | S_IRUGO,
2735 show_auto_temp, store_auto_temp, 1, 5),
2736 SENSOR_ATTR_2(pwm2_auto_point6_temp_hyst, S_IWUSR | S_IRUGO,
2737 show_auto_temp_hyst, store_auto_temp_hyst, 1, 5),
2738 SENSOR_ATTR_2(pwm2_auto_point7_pwm, S_IWUSR | S_IRUGO,
2739 show_auto_pwm, store_auto_pwm, 1, 6),
2740 SENSOR_ATTR_2(pwm2_auto_point7_temp, S_IWUSR | S_IRUGO,
2741 show_auto_temp, store_auto_temp, 1, 6),
2742 SENSOR_ATTR_2(pwm2_auto_point7_temp_hyst, S_IWUSR | S_IRUGO,
2743 show_auto_temp_hyst, store_auto_temp_hyst, 1, 6),
2745 SENSOR_ATTR_2(pwm3_auto_point1_pwm, S_IWUSR | S_IRUGO,
2746 show_auto_pwm, store_auto_pwm, 2, 0),
2747 SENSOR_ATTR_2(pwm3_auto_point1_temp, S_IWUSR | S_IRUGO,
2748 show_auto_temp, store_auto_temp, 2, 0),
2749 SENSOR_ATTR_2(pwm3_auto_point1_temp_hyst, S_IWUSR | S_IRUGO,
2750 show_auto_temp_hyst, store_auto_temp_hyst, 2, 0),
2751 SENSOR_ATTR_2(pwm3_auto_point2_pwm, S_IWUSR | S_IRUGO,
2752 show_auto_pwm, store_auto_pwm, 2, 1),
2753 SENSOR_ATTR_2(pwm3_auto_point2_temp, S_IWUSR | S_IRUGO,
2754 show_auto_temp, store_auto_temp, 2, 1),
2755 SENSOR_ATTR_2(pwm3_auto_point2_temp_hyst, S_IWUSR | S_IRUGO,
2756 show_auto_temp_hyst, store_auto_temp_hyst, 2, 1),
2757 SENSOR_ATTR_2(pwm3_auto_point3_pwm, S_IWUSR | S_IRUGO,
2758 show_auto_pwm, store_auto_pwm, 2, 2),
2759 SENSOR_ATTR_2(pwm3_auto_point3_temp, S_IWUSR | S_IRUGO,
2760 show_auto_temp, store_auto_temp, 2, 2),
2761 SENSOR_ATTR_2(pwm3_auto_point3_temp_hyst, S_IWUSR | S_IRUGO,
2762 show_auto_temp_hyst, store_auto_temp_hyst, 2, 2),
2763 SENSOR_ATTR_2(pwm3_auto_point4_pwm, S_IWUSR | S_IRUGO,
2764 show_auto_pwm, store_auto_pwm, 2, 3),
2765 SENSOR_ATTR_2(pwm3_auto_point4_temp, S_IWUSR | S_IRUGO,
2766 show_auto_temp, store_auto_temp, 2, 3),
2767 SENSOR_ATTR_2(pwm3_auto_point4_temp_hyst, S_IWUSR | S_IRUGO,
2768 show_auto_temp_hyst, store_auto_temp_hyst, 2, 3),
2769 SENSOR_ATTR_2(pwm3_auto_point5_pwm, S_IWUSR | S_IRUGO,
2770 show_auto_pwm, store_auto_pwm, 2, 4),
2771 SENSOR_ATTR_2(pwm3_auto_point5_temp, S_IWUSR | S_IRUGO,
2772 show_auto_temp, store_auto_temp, 2, 4),
2773 SENSOR_ATTR_2(pwm3_auto_point5_temp_hyst, S_IWUSR | S_IRUGO,
2774 show_auto_temp_hyst, store_auto_temp_hyst, 2, 4),
2775 SENSOR_ATTR_2(pwm3_auto_point6_pwm, S_IWUSR | S_IRUGO,
2776 show_auto_pwm, store_auto_pwm, 2, 5),
2777 SENSOR_ATTR_2(pwm3_auto_point6_temp, S_IWUSR | S_IRUGO,
2778 show_auto_temp, store_auto_temp, 2, 5),
2779 SENSOR_ATTR_2(pwm3_auto_point6_temp_hyst, S_IWUSR | S_IRUGO,
2780 show_auto_temp_hyst, store_auto_temp_hyst, 2, 5),
2781 SENSOR_ATTR_2(pwm3_auto_point7_pwm, S_IWUSR | S_IRUGO,
2782 show_auto_pwm, store_auto_pwm, 2, 6),
2783 SENSOR_ATTR_2(pwm3_auto_point7_temp, S_IWUSR | S_IRUGO,
2784 show_auto_temp, store_auto_temp, 2, 6),
2785 SENSOR_ATTR_2(pwm3_auto_point7_temp_hyst, S_IWUSR | S_IRUGO,
2786 show_auto_temp_hyst, store_auto_temp_hyst, 2, 6),
2788 SENSOR_ATTR_2(pwm4_auto_point1_pwm, S_IWUSR | S_IRUGO,
2789 show_auto_pwm, store_auto_pwm, 3, 0),
2790 SENSOR_ATTR_2(pwm4_auto_point1_temp, S_IWUSR | S_IRUGO,
2791 show_auto_temp, store_auto_temp, 3, 0),
2792 SENSOR_ATTR_2(pwm4_auto_point1_temp_hyst, S_IWUSR | S_IRUGO,
2793 show_auto_temp_hyst, store_auto_temp_hyst, 3, 0),
2794 SENSOR_ATTR_2(pwm4_auto_point2_pwm, S_IWUSR | S_IRUGO,
2795 show_auto_pwm, store_auto_pwm, 3, 1),
2796 SENSOR_ATTR_2(pwm4_auto_point2_temp, S_IWUSR | S_IRUGO,
2797 show_auto_temp, store_auto_temp, 3, 1),
2798 SENSOR_ATTR_2(pwm4_auto_point2_temp_hyst, S_IWUSR | S_IRUGO,
2799 show_auto_temp_hyst, store_auto_temp_hyst, 3, 1),
2800 SENSOR_ATTR_2(pwm4_auto_point3_pwm, S_IWUSR | S_IRUGO,
2801 show_auto_pwm, store_auto_pwm, 3, 2),
2802 SENSOR_ATTR_2(pwm4_auto_point3_temp, S_IWUSR | S_IRUGO,
2803 show_auto_temp, store_auto_temp, 3, 2),
2804 SENSOR_ATTR_2(pwm4_auto_point3_temp_hyst, S_IWUSR | S_IRUGO,
2805 show_auto_temp_hyst, store_auto_temp_hyst, 3, 2),
2806 SENSOR_ATTR_2(pwm4_auto_point4_pwm, S_IWUSR | S_IRUGO,
2807 show_auto_pwm, store_auto_pwm, 3, 3),
2808 SENSOR_ATTR_2(pwm4_auto_point4_temp, S_IWUSR | S_IRUGO,
2809 show_auto_temp, store_auto_temp, 3, 3),
2810 SENSOR_ATTR_2(pwm4_auto_point4_temp_hyst, S_IWUSR | S_IRUGO,
2811 show_auto_temp_hyst, store_auto_temp_hyst, 3, 3),
2812 SENSOR_ATTR_2(pwm4_auto_point5_pwm, S_IWUSR | S_IRUGO,
2813 show_auto_pwm, store_auto_pwm, 3, 4),
2814 SENSOR_ATTR_2(pwm4_auto_point5_temp, S_IWUSR | S_IRUGO,
2815 show_auto_temp, store_auto_temp, 3, 4),
2816 SENSOR_ATTR_2(pwm4_auto_point5_temp_hyst, S_IWUSR | S_IRUGO,
2817 show_auto_temp_hyst, store_auto_temp_hyst, 3, 4),
2818 SENSOR_ATTR_2(pwm4_auto_point6_pwm, S_IWUSR | S_IRUGO,
2819 show_auto_pwm, store_auto_pwm, 3, 5),
2820 SENSOR_ATTR_2(pwm4_auto_point6_temp, S_IWUSR | S_IRUGO,
2821 show_auto_temp, store_auto_temp, 3, 5),
2822 SENSOR_ATTR_2(pwm4_auto_point6_temp_hyst, S_IWUSR | S_IRUGO,
2823 show_auto_temp_hyst, store_auto_temp_hyst, 3, 5),
2824 SENSOR_ATTR_2(pwm4_auto_point7_pwm, S_IWUSR | S_IRUGO,
2825 show_auto_pwm, store_auto_pwm, 3, 6),
2826 SENSOR_ATTR_2(pwm4_auto_point7_temp, S_IWUSR | S_IRUGO,
2827 show_auto_temp, store_auto_temp, 3, 6),
2828 SENSOR_ATTR_2(pwm4_auto_point7_temp_hyst, S_IWUSR | S_IRUGO,
2829 show_auto_temp_hyst, store_auto_temp_hyst, 3, 6),
2831 SENSOR_ATTR_2(pwm5_auto_point1_pwm, S_IWUSR | S_IRUGO,
2832 show_auto_pwm, store_auto_pwm, 4, 0),
2833 SENSOR_ATTR_2(pwm5_auto_point1_temp, S_IWUSR | S_IRUGO,
2834 show_auto_temp, store_auto_temp, 4, 0),
2835 SENSOR_ATTR_2(pwm5_auto_point1_temp_hyst, S_IWUSR | S_IRUGO,
2836 show_auto_temp_hyst, store_auto_temp_hyst, 4, 0),
2837 SENSOR_ATTR_2(pwm5_auto_point2_pwm, S_IWUSR | S_IRUGO,
2838 show_auto_pwm, store_auto_pwm, 4, 1),
2839 SENSOR_ATTR_2(pwm5_auto_point2_temp, S_IWUSR | S_IRUGO,
2840 show_auto_temp, store_auto_temp, 4, 1),
2841 SENSOR_ATTR_2(pwm5_auto_point2_temp_hyst, S_IWUSR | S_IRUGO,
2842 show_auto_temp_hyst, store_auto_temp_hyst, 4, 1),
2843 SENSOR_ATTR_2(pwm5_auto_point3_pwm, S_IWUSR | S_IRUGO,
2844 show_auto_pwm, store_auto_pwm, 4, 2),
2845 SENSOR_ATTR_2(pwm5_auto_point3_temp, S_IWUSR | S_IRUGO,
2846 show_auto_temp, store_auto_temp, 4, 2),
2847 SENSOR_ATTR_2(pwm5_auto_point3_temp_hyst, S_IWUSR | S_IRUGO,
2848 show_auto_temp_hyst, store_auto_temp_hyst, 4, 2),
2849 SENSOR_ATTR_2(pwm5_auto_point4_pwm, S_IWUSR | S_IRUGO,
2850 show_auto_pwm, store_auto_pwm, 4, 3),
2851 SENSOR_ATTR_2(pwm5_auto_point4_temp, S_IWUSR | S_IRUGO,
2852 show_auto_temp, store_auto_temp, 4, 3),
2853 SENSOR_ATTR_2(pwm5_auto_point4_temp_hyst, S_IWUSR | S_IRUGO,
2854 show_auto_temp_hyst, store_auto_temp_hyst, 4, 3),
2855 SENSOR_ATTR_2(pwm5_auto_point5_pwm, S_IWUSR | S_IRUGO,
2856 show_auto_pwm, store_auto_pwm, 4, 4),
2857 SENSOR_ATTR_2(pwm5_auto_point5_temp, S_IWUSR | S_IRUGO,
2858 show_auto_temp, store_auto_temp, 4, 4),
2859 SENSOR_ATTR_2(pwm5_auto_point5_temp_hyst, S_IWUSR | S_IRUGO,
2860 show_auto_temp_hyst, store_auto_temp_hyst, 4, 4),
2861 SENSOR_ATTR_2(pwm5_auto_point6_pwm, S_IWUSR | S_IRUGO,
2862 show_auto_pwm, store_auto_pwm, 4, 5),
2863 SENSOR_ATTR_2(pwm5_auto_point6_temp, S_IWUSR | S_IRUGO,
2864 show_auto_temp, store_auto_temp, 4, 5),
2865 SENSOR_ATTR_2(pwm5_auto_point6_temp_hyst, S_IWUSR | S_IRUGO,
2866 show_auto_temp_hyst, store_auto_temp_hyst, 4, 5),
2867 SENSOR_ATTR_2(pwm5_auto_point7_pwm, S_IWUSR | S_IRUGO,
2868 show_auto_pwm, store_auto_pwm, 4, 6),
2869 SENSOR_ATTR_2(pwm5_auto_point7_temp, S_IWUSR | S_IRUGO,
2870 show_auto_temp, store_auto_temp, 4, 6),
2871 SENSOR_ATTR_2(pwm5_auto_point7_temp_hyst, S_IWUSR | S_IRUGO,
2872 show_auto_temp_hyst, store_auto_temp_hyst, 4, 6),
2876 show_vid(struct device *dev, struct device_attribute *attr, char *buf)
2878 struct nct6775_data *data = dev_get_drvdata(dev);
2879 return sprintf(buf, "%d\n", vid_from_reg(data->vid, data->vrm));
2882 static DEVICE_ATTR(cpu0_vid, S_IRUGO, show_vid, NULL);
2884 /* Case open detection */
2887 clear_caseopen(struct device *dev, struct device_attribute *attr,
2888 const char *buf, size_t count)
2890 struct nct6775_data *data = dev_get_drvdata(dev);
2891 struct nct6775_sio_data *sio_data = dev->platform_data;
2892 int nr = to_sensor_dev_attr(attr)->index - INTRUSION_ALARM_BASE;
2897 if (kstrtoul(buf, 10, &val) || val != 0)
2900 mutex_lock(&data->update_lock);
2903 * Use CR registers to clear caseopen status.
2904 * The CR registers are the same for all chips, and not all chips
2905 * support clearing the caseopen status through "regular" registers.
2907 ret = superio_enter(sio_data->sioreg);
2913 superio_select(sio_data->sioreg, NCT6775_LD_ACPI);
2914 reg = superio_inb(sio_data->sioreg, NCT6775_REG_CR_CASEOPEN_CLR[nr]);
2915 reg |= NCT6775_CR_CASEOPEN_CLR_MASK[nr];
2916 superio_outb(sio_data->sioreg, NCT6775_REG_CR_CASEOPEN_CLR[nr], reg);
2917 reg &= ~NCT6775_CR_CASEOPEN_CLR_MASK[nr];
2918 superio_outb(sio_data->sioreg, NCT6775_REG_CR_CASEOPEN_CLR[nr], reg);
2919 superio_exit(sio_data->sioreg);
2921 data->valid = 0; /* Force cache refresh */
2923 mutex_unlock(&data->update_lock);
2927 static struct sensor_device_attribute sda_caseopen[] = {
2928 SENSOR_ATTR(intrusion0_alarm, S_IWUSR | S_IRUGO, show_alarm,
2929 clear_caseopen, INTRUSION_ALARM_BASE),
2930 SENSOR_ATTR(intrusion1_alarm, S_IWUSR | S_IRUGO, show_alarm,
2931 clear_caseopen, INTRUSION_ALARM_BASE + 1),
2935 * Driver and device management
2938 static void nct6775_device_remove_files(struct device *dev)
2941 * some entries in the following arrays may not have been used in
2942 * device_create_file(), but device_remove_file() will ignore them
2945 struct nct6775_data *data = dev_get_drvdata(dev);
2947 for (i = 0; i < data->pwm_num; i++)
2948 sysfs_remove_group(&dev->kobj, &nct6775_group_pwm[i]);
2950 for (i = 0; i < ARRAY_SIZE(sda_max_output); i++)
2951 device_remove_file(dev, &sda_max_output[i].dev_attr);
2953 for (i = 0; i < ARRAY_SIZE(sda_step_output); i++)
2954 device_remove_file(dev, &sda_max_output[i].dev_attr);
2956 for (i = 0; i < ARRAY_SIZE(sda_weight_duty_base); i++)
2957 device_remove_file(dev, &sda_weight_duty_base[i].dev_attr);
2959 for (i = 0; i < ARRAY_SIZE(sda_auto_pwm_arrays); i++)
2960 device_remove_file(dev, &sda_auto_pwm_arrays[i].dev_attr);
2962 for (i = 0; i < data->in_num; i++)
2963 sysfs_remove_group(&dev->kobj, &nct6775_group_in[i]);
2965 for (i = 0; i < 5; i++) {
2966 device_remove_file(dev, &sda_fan_input[i].dev_attr);
2967 device_remove_file(dev, &sda_fan_alarm[i].dev_attr);
2968 device_remove_file(dev, &sda_fan_div[i].dev_attr);
2969 device_remove_file(dev, &sda_fan_min[i].dev_attr);
2971 for (i = 0; i < NUM_TEMP; i++) {
2972 if (!(data->have_temp & (1 << i)))
2974 device_remove_file(dev, &sda_temp_input[i].dev_attr);
2975 device_remove_file(dev, &sda_temp_label[i].dev_attr);
2976 device_remove_file(dev, &sda_temp_max[i].dev_attr);
2977 device_remove_file(dev, &sda_temp_max_hyst[i].dev_attr);
2978 device_remove_file(dev, &sda_temp_crit[i].dev_attr);
2979 if (!(data->have_temp_fixed & (1 << i)))
2981 device_remove_file(dev, &sda_temp_type[i].dev_attr);
2982 device_remove_file(dev, &sda_temp_offset[i].dev_attr);
2983 if (i >= NUM_TEMP_ALARM)
2985 device_remove_file(dev, &sda_temp_alarm[i].dev_attr);
2988 device_remove_file(dev, &sda_caseopen[0].dev_attr);
2989 device_remove_file(dev, &sda_caseopen[1].dev_attr);
2991 device_remove_file(dev, &dev_attr_name);
2992 device_remove_file(dev, &dev_attr_cpu0_vid);
2995 /* Get the monitoring functions started */
2996 static inline void __devinit nct6775_init_device(struct nct6775_data *data)
3001 /* Start monitoring if needed */
3002 if (data->REG_CONFIG) {
3003 tmp = nct6775_read_value(data, data->REG_CONFIG);
3005 nct6775_write_value(data, data->REG_CONFIG, tmp | 0x01);
3008 /* Enable temperature sensors if needed */
3009 for (i = 0; i < NUM_TEMP; i++) {
3010 if (!(data->have_temp & (1 << i)))
3012 if (!data->reg_temp_config[i])
3014 tmp = nct6775_read_value(data, data->reg_temp_config[i]);
3016 nct6775_write_value(data, data->reg_temp_config[i],
3020 /* Enable VBAT monitoring if needed */
3021 tmp = nct6775_read_value(data, data->REG_VBAT);
3023 nct6775_write_value(data, data->REG_VBAT, tmp | 0x01);
3025 diode = nct6775_read_value(data, data->REG_DIODE);
3027 for (i = 0; i < data->temp_fixed_num; i++) {
3028 if (!(data->have_temp_fixed & (1 << i)))
3030 if ((tmp & (0x02 << i))) /* diode */
3031 data->temp_type[i] = 3 - ((diode >> i) & 0x02);
3032 else /* thermistor */
3033 data->temp_type[i] = 4;
3037 static int __devinit
3038 nct6775_check_fan_inputs(const struct nct6775_sio_data *sio_data,
3039 struct nct6775_data *data)
3042 bool fan3pin, fan3min, fan4pin, fan4min, fan5pin;
3043 bool pwm3pin, pwm4pin, pwm5pin;
3046 ret = superio_enter(sio_data->sioreg);
3050 /* fan4 and fan5 share some pins with the GPIO and serial flash */
3051 if (data->kind == nct6775) {
3052 regval = superio_inb(sio_data->sioreg, 0x2c);
3054 fan3pin = regval & (1 << 6);
3056 pwm3pin = regval & (1 << 7);
3058 /* On NCT6775, fan4 shares pins with the fdc interface */
3059 fan4pin = !(superio_inb(sio_data->sioreg, 0x2A) & 0x80);
3064 } else if (data->kind == nct6776) {
3065 bool gpok = superio_inb(sio_data->sioreg, 0x27) & 0x80;
3067 superio_select(sio_data->sioreg, NCT6775_LD_HWM);
3068 regval = superio_inb(sio_data->sioreg, SIO_REG_ENABLE);
3073 fan3pin = !(superio_inb(sio_data->sioreg, 0x24) & 0x40);
3078 fan4pin = superio_inb(sio_data->sioreg, 0x1C) & 0x01;
3083 fan5pin = superio_inb(sio_data->sioreg, 0x1C) & 0x02;
3090 } else { /* NCT6779D */
3091 regval = superio_inb(sio_data->sioreg, 0x1c);
3093 fan3pin = !(regval & (1 << 5));
3094 fan4pin = !(regval & (1 << 6));
3095 fan5pin = !(regval & (1 << 7));
3097 pwm3pin = !(regval & (1 << 0));
3098 pwm4pin = !(regval & (1 << 1));
3099 pwm5pin = !(regval & (1 << 2));
3105 superio_exit(sio_data->sioreg);
3107 data->has_fan = data->has_fan_min = 0x03; /* fan1 and fan2 */
3108 data->has_fan |= fan3pin << 2;
3109 data->has_fan_min |= fan3min << 2;
3111 data->has_fan |= (fan4pin << 3) | (fan5pin << 4);
3112 data->has_fan_min |= (fan4min << 3) | (fan5pin << 4);
3114 data->has_pwm = 0x03 | (pwm3pin << 2) | (pwm4pin << 3) | (pwm5pin << 4);
3119 static int __devinit nct6775_probe(struct platform_device *pdev)
3121 struct device *dev = &pdev->dev;
3122 struct nct6775_sio_data *sio_data = dev->platform_data;
3123 struct nct6775_data *data;
3124 struct resource *res;
3126 int src, mask, available;
3127 const u16 *reg_temp, *reg_temp_over, *reg_temp_hyst, *reg_temp_config;
3128 const u16 *reg_temp_alternate, *reg_temp_crit;
3131 res = platform_get_resource(pdev, IORESOURCE_IO, 0);
3132 if (!devm_request_region(&pdev->dev, res->start, IOREGION_LENGTH,
3136 data = devm_kzalloc(&pdev->dev, sizeof(struct nct6775_data),
3141 data->kind = sio_data->kind;
3142 data->addr = res->start;
3143 mutex_init(&data->lock);
3144 mutex_init(&data->update_lock);
3145 data->name = nct6775_device_names[data->kind];
3146 platform_set_drvdata(pdev, data);
3148 switch (data->kind) {
3152 data->auto_pwm_num = 6;
3153 data->has_fan_div = true;
3154 data->temp_fixed_num = 3;
3156 data->ALARM_BITS = NCT6775_ALARM_BITS;
3158 data->fan_from_reg = fan_from_reg16;
3159 data->fan_from_reg_min = fan_from_reg8;
3161 data->temp_label = nct6775_temp_label;
3162 data->temp_label_num = ARRAY_SIZE(nct6775_temp_label);
3164 data->REG_CONFIG = NCT6775_REG_CONFIG;
3165 data->REG_VBAT = NCT6775_REG_VBAT;
3166 data->REG_DIODE = NCT6775_REG_DIODE;
3167 data->REG_VIN = NCT6775_REG_IN;
3168 data->REG_IN_MINMAX[0] = NCT6775_REG_IN_MIN;
3169 data->REG_IN_MINMAX[1] = NCT6775_REG_IN_MAX;
3170 data->REG_TARGET = NCT6775_REG_TARGET;
3171 data->REG_FAN = NCT6775_REG_FAN;
3172 data->REG_FAN_MODE = NCT6775_REG_FAN_MODE;
3173 data->REG_FAN_MIN = NCT6775_REG_FAN_MIN;
3174 data->REG_FAN_TIME[0] = NCT6775_REG_FAN_STOP_TIME;
3175 data->REG_FAN_TIME[1] = NCT6775_REG_FAN_STEP_UP_TIME;
3176 data->REG_FAN_TIME[2] = NCT6775_REG_FAN_STEP_DOWN_TIME;
3177 data->REG_PWM[0] = NCT6775_REG_PWM;
3178 data->REG_PWM[1] = NCT6775_REG_FAN_START_OUTPUT;
3179 data->REG_PWM[2] = NCT6775_REG_FAN_STOP_OUTPUT;
3180 data->REG_PWM[3] = NCT6775_REG_FAN_MAX_OUTPUT;
3181 data->REG_PWM[4] = NCT6775_REG_FAN_STEP_OUTPUT;
3182 data->REG_PWM[5] = NCT6775_REG_WEIGHT_DUTY_STEP;
3183 data->REG_PWM_READ = NCT6775_REG_PWM_READ;
3184 data->REG_PWM_MODE = NCT6775_REG_PWM_MODE;
3185 data->PWM_MODE_MASK = NCT6775_PWM_MODE_MASK;
3186 data->REG_AUTO_TEMP = NCT6775_REG_AUTO_TEMP;
3187 data->REG_AUTO_PWM = NCT6775_REG_AUTO_PWM;
3188 data->REG_CRITICAL_TEMP = NCT6775_REG_CRITICAL_TEMP;
3189 data->REG_CRITICAL_TEMP_TOLERANCE
3190 = NCT6775_REG_CRITICAL_TEMP_TOLERANCE;
3191 data->REG_TEMP_OFFSET = NCT6775_REG_TEMP_OFFSET;
3192 data->REG_TEMP_SOURCE = NCT6775_REG_TEMP_SOURCE;
3193 data->REG_TEMP_SEL[0] = NCT6775_REG_TEMP_SEL;
3194 data->REG_TEMP_SEL[1] = NCT6775_REG_WEIGHT_TEMP_SEL;
3195 data->REG_TEMP_SEL[2] = NCT6775_REG_FAN_STEP_ENABLE;
3196 data->REG_WEIGHT_TEMP[0] = NCT6775_REG_WEIGHT_TEMP_STEP;
3197 data->REG_WEIGHT_TEMP[1] = NCT6775_REG_WEIGHT_TEMP_STEP_TOL;
3198 data->REG_WEIGHT_TEMP[2] = NCT6775_REG_WEIGHT_TEMP_BASE;
3199 data->REG_ALARM = NCT6775_REG_ALARM;
3201 reg_temp = NCT6775_REG_TEMP;
3202 num_reg_temp = ARRAY_SIZE(NCT6775_REG_TEMP);
3203 reg_temp_over = NCT6775_REG_TEMP_OVER;
3204 reg_temp_hyst = NCT6775_REG_TEMP_HYST;
3205 reg_temp_config = NCT6775_REG_TEMP_CONFIG;
3206 reg_temp_alternate = NCT6775_REG_TEMP_ALTERNATE;
3207 reg_temp_crit = NCT6775_REG_TEMP_CRIT;
3213 data->auto_pwm_num = 4;
3214 data->has_fan_div = false;
3215 data->temp_fixed_num = 3;
3217 data->ALARM_BITS = NCT6776_ALARM_BITS;
3219 data->fan_from_reg = fan_from_reg13;
3220 data->fan_from_reg_min = fan_from_reg13;
3222 data->temp_label = nct6776_temp_label;
3223 data->temp_label_num = ARRAY_SIZE(nct6776_temp_label);
3225 data->REG_CONFIG = NCT6775_REG_CONFIG;
3226 data->REG_VBAT = NCT6775_REG_VBAT;
3227 data->REG_DIODE = NCT6775_REG_DIODE;
3228 data->REG_VIN = NCT6775_REG_IN;
3229 data->REG_IN_MINMAX[0] = NCT6775_REG_IN_MIN;
3230 data->REG_IN_MINMAX[1] = NCT6775_REG_IN_MAX;
3231 data->REG_TARGET = NCT6775_REG_TARGET;
3232 data->REG_FAN = NCT6775_REG_FAN;
3233 data->REG_FAN_MODE = NCT6775_REG_FAN_MODE;
3234 data->REG_FAN_MIN = NCT6776_REG_FAN_MIN;
3235 data->REG_FAN_TIME[0] = NCT6775_REG_FAN_STOP_TIME;
3236 data->REG_FAN_TIME[1] = NCT6775_REG_FAN_STEP_UP_TIME;
3237 data->REG_FAN_TIME[2] = NCT6775_REG_FAN_STEP_DOWN_TIME;
3238 data->REG_PWM[0] = NCT6775_REG_PWM;
3239 data->REG_PWM[1] = NCT6775_REG_FAN_START_OUTPUT;
3240 data->REG_PWM[2] = NCT6775_REG_FAN_STOP_OUTPUT;
3241 data->REG_PWM[5] = NCT6775_REG_WEIGHT_DUTY_STEP;
3242 data->REG_PWM[6] = NCT6776_REG_WEIGHT_DUTY_BASE;
3243 data->REG_PWM_READ = NCT6775_REG_PWM_READ;
3244 data->REG_PWM_MODE = NCT6776_REG_PWM_MODE;
3245 data->PWM_MODE_MASK = NCT6776_PWM_MODE_MASK;
3246 data->REG_AUTO_TEMP = NCT6775_REG_AUTO_TEMP;
3247 data->REG_AUTO_PWM = NCT6775_REG_AUTO_PWM;
3248 data->REG_CRITICAL_TEMP = NCT6775_REG_CRITICAL_TEMP;
3249 data->REG_CRITICAL_TEMP_TOLERANCE
3250 = NCT6775_REG_CRITICAL_TEMP_TOLERANCE;
3251 data->REG_TEMP_OFFSET = NCT6775_REG_TEMP_OFFSET;
3252 data->REG_TEMP_SOURCE = NCT6775_REG_TEMP_SOURCE;
3253 data->REG_TEMP_SEL[0] = NCT6775_REG_TEMP_SEL;
3254 data->REG_TEMP_SEL[1] = NCT6775_REG_WEIGHT_TEMP_SEL;
3255 data->REG_TEMP_SEL[2] = NCT6775_REG_FAN_STEP_ENABLE;
3256 data->REG_WEIGHT_TEMP[0] = NCT6775_REG_WEIGHT_TEMP_STEP;
3257 data->REG_WEIGHT_TEMP[1] = NCT6775_REG_WEIGHT_TEMP_STEP_TOL;
3258 data->REG_WEIGHT_TEMP[2] = NCT6775_REG_WEIGHT_TEMP_BASE;
3259 data->REG_ALARM = NCT6775_REG_ALARM;
3261 reg_temp = NCT6775_REG_TEMP;
3262 num_reg_temp = ARRAY_SIZE(NCT6775_REG_TEMP);
3263 reg_temp_over = NCT6775_REG_TEMP_OVER;
3264 reg_temp_hyst = NCT6775_REG_TEMP_HYST;
3265 reg_temp_config = NCT6776_REG_TEMP_CONFIG;
3266 reg_temp_alternate = NCT6776_REG_TEMP_ALTERNATE;
3267 reg_temp_crit = NCT6776_REG_TEMP_CRIT;
3273 data->auto_pwm_num = 4;
3274 data->has_fan_div = false;
3275 data->temp_fixed_num = 6;
3277 data->ALARM_BITS = NCT6779_ALARM_BITS;
3279 data->fan_from_reg = fan_from_reg13;
3280 data->fan_from_reg_min = fan_from_reg13;
3282 data->temp_label = nct6779_temp_label;
3283 data->temp_label_num = ARRAY_SIZE(nct6779_temp_label);
3285 data->REG_CONFIG = NCT6775_REG_CONFIG;
3286 data->REG_VBAT = NCT6775_REG_VBAT;
3287 data->REG_DIODE = NCT6775_REG_DIODE;
3288 data->REG_VIN = NCT6779_REG_IN;
3289 data->REG_IN_MINMAX[0] = NCT6775_REG_IN_MIN;
3290 data->REG_IN_MINMAX[1] = NCT6775_REG_IN_MAX;
3291 data->REG_TARGET = NCT6775_REG_TARGET;
3292 data->REG_FAN = NCT6779_REG_FAN;
3293 data->REG_FAN_MODE = NCT6775_REG_FAN_MODE;
3294 data->REG_FAN_MIN = NCT6776_REG_FAN_MIN;
3295 data->REG_FAN_TIME[0] = NCT6775_REG_FAN_STOP_TIME;
3296 data->REG_FAN_TIME[1] = NCT6775_REG_FAN_STEP_UP_TIME;
3297 data->REG_FAN_TIME[2] = NCT6775_REG_FAN_STEP_DOWN_TIME;
3298 data->REG_PWM[0] = NCT6775_REG_PWM;
3299 data->REG_PWM[1] = NCT6775_REG_FAN_START_OUTPUT;
3300 data->REG_PWM[2] = NCT6775_REG_FAN_STOP_OUTPUT;
3301 data->REG_PWM[5] = NCT6775_REG_WEIGHT_DUTY_STEP;
3302 data->REG_PWM[6] = NCT6776_REG_WEIGHT_DUTY_BASE;
3303 data->REG_PWM_READ = NCT6775_REG_PWM_READ;
3304 data->REG_PWM_MODE = NCT6776_REG_PWM_MODE;
3305 data->PWM_MODE_MASK = NCT6776_PWM_MODE_MASK;
3306 data->REG_AUTO_TEMP = NCT6775_REG_AUTO_TEMP;
3307 data->REG_AUTO_PWM = NCT6775_REG_AUTO_PWM;
3308 data->REG_CRITICAL_TEMP = NCT6775_REG_CRITICAL_TEMP;
3309 data->REG_CRITICAL_TEMP_TOLERANCE
3310 = NCT6775_REG_CRITICAL_TEMP_TOLERANCE;
3311 data->REG_TEMP_OFFSET = NCT6779_REG_TEMP_OFFSET;
3312 data->REG_TEMP_SOURCE = NCT6775_REG_TEMP_SOURCE;
3313 data->REG_TEMP_SEL[0] = NCT6775_REG_TEMP_SEL;
3314 data->REG_TEMP_SEL[1] = NCT6775_REG_WEIGHT_TEMP_SEL;
3315 data->REG_TEMP_SEL[2] = NCT6775_REG_FAN_STEP_ENABLE;
3316 data->REG_WEIGHT_TEMP[0] = NCT6775_REG_WEIGHT_TEMP_STEP;
3317 data->REG_WEIGHT_TEMP[1] = NCT6775_REG_WEIGHT_TEMP_STEP_TOL;
3318 data->REG_WEIGHT_TEMP[2] = NCT6775_REG_WEIGHT_TEMP_BASE;
3319 data->REG_ALARM = NCT6779_REG_ALARM;
3321 reg_temp = NCT6779_REG_TEMP;
3322 num_reg_temp = ARRAY_SIZE(NCT6779_REG_TEMP);
3323 reg_temp_over = NCT6779_REG_TEMP_OVER;
3324 reg_temp_hyst = NCT6779_REG_TEMP_HYST;
3325 reg_temp_config = NCT6779_REG_TEMP_CONFIG;
3326 reg_temp_alternate = NCT6779_REG_TEMP_ALTERNATE;
3327 reg_temp_crit = NCT6779_REG_TEMP_CRIT;
3333 data->have_in = (1 << data->in_num) - 1;
3334 data->have_temp = 0;
3337 * On some boards, not all available temperature sources are monitored,
3338 * even though some of the monitoring registers are unused.
3339 * Get list of unused monitoring registers, then detect if any fan
3340 * controls are configured to use unmonitored temperature sources.
3341 * If so, assign the unmonitored temperature sources to available
3342 * monitoring registers.
3346 for (i = 0; i < num_reg_temp; i++) {
3347 if (reg_temp[i] == 0)
3350 src = nct6775_read_value(data, data->REG_TEMP_SOURCE[i]) & 0x1f;
3351 if (!src || (mask & (1 << src)))
3352 available |= 1 << i;
3358 * Now find unmonitored temperature registers and enable monitoring
3359 * if additional monitoring registers are available.
3361 for (i = 0; i < ARRAY_SIZE(data->REG_TEMP_SEL) && available; i++) {
3364 if (!data->REG_TEMP_SEL[i])
3366 for (j = 0; j < data->pwm_num && available; j++) {
3369 if (!data->REG_TEMP_SEL[i][j])
3371 src = nct6775_read_value(data,
3372 data->REG_TEMP_SEL[i][j]);
3374 if (!src || (mask & (1 << src)))
3376 if (src >= data->temp_label_num ||
3377 !strlen(data->temp_label[src])) {
3379 "Select source %d:%d reg 0x%x invalid (%d)\n",
3380 i, j, data->REG_TEMP_SEL[i][j], src);
3384 index = __ffs(available);
3385 nct6775_write_value(data,
3386 data->REG_TEMP_SOURCE[index],
3388 available &= ~(1 << index);
3394 s = NUM_TEMP_FIXED; /* First dynamic temperature attribute */
3395 for (i = 0; i < num_reg_temp; i++) {
3396 if (reg_temp[i] == 0)
3399 src = nct6775_read_value(data, data->REG_TEMP_SOURCE[i]) & 0x1f;
3400 if (!src || (mask & (1 << src)))
3403 if (src >= data->temp_label_num ||
3404 !strlen(data->temp_label[src])) {
3406 "Invalid temperature source %d at index %d, source register 0x%x, temp register 0x%x\n",
3407 src, i, data->REG_TEMP_SOURCE[i], reg_temp[i]);
3413 /* Use fixed index for SYSTIN(1), CPUTIN(2), AUXTIN(3) */
3414 if (src <= data->temp_fixed_num) {
3415 data->have_temp |= 1 << (src - 1);
3416 data->have_temp_fixed |= 1 << (src - 1);
3417 data->reg_temp[0][src - 1] = reg_temp[i];
3418 data->reg_temp[1][src - 1] = reg_temp_over[i];
3419 data->reg_temp[2][src - 1] = reg_temp_hyst[i];
3420 data->reg_temp_config[src - 1] = reg_temp_config[i];
3421 data->temp_src[src - 1] = src;
3428 /* Use dynamic index for other sources */
3429 data->have_temp |= 1 << s;
3430 data->reg_temp[0][s] = reg_temp[i];
3431 data->reg_temp[1][s] = reg_temp_over[i];
3432 data->reg_temp[2][s] = reg_temp_hyst[i];
3433 data->reg_temp_config[s] = reg_temp_config[i];
3434 if (reg_temp_crit[src - 1])
3435 data->reg_temp[3][s] = reg_temp_crit[src - 1];
3437 data->temp_src[s] = src;
3443 * Go through the list of alternate temp registers and enable
3445 * The temperature is already monitored if the respective bit in <mask>
3448 for (i = 0; i < data->temp_label_num - 1; i++) {
3449 if (!reg_temp_alternate[i])
3451 if (mask & (1 << (i + 1)))
3453 if (i < data->temp_fixed_num) {
3454 if (data->have_temp & (1 << i))
3456 data->have_temp |= 1 << i;
3457 data->have_temp_fixed |= 1 << i;
3458 data->reg_temp[0][i] = reg_temp_alternate[i];
3459 data->reg_temp[1][i] = reg_temp_over[i];
3460 data->reg_temp[2][i] = reg_temp_hyst[i];
3461 data->temp_src[i] = i + 1;
3465 if (s >= NUM_TEMP) /* Abort if no more space */
3468 data->have_temp |= 1 << s;
3469 data->reg_temp[0][s] = reg_temp_alternate[i];
3470 data->temp_src[s] = i + 1;
3473 #endif /* TESTING */
3475 switch (data->kind) {
3480 * On NCT6776, AUXTIN and VIN3 pins are shared.
3481 * Only way to detect it is to check if AUXTIN is used
3482 * as a temperature source, and if that source is
3485 * If that is the case, disable in6, which reports VIN3.
3486 * Otherwise disable temp3.
3488 if (data->have_temp & (1 << 2)) {
3489 u8 reg = nct6775_read_value(data,
3490 data->reg_temp_config[2]);
3492 data->have_temp &= ~(1 << 2);
3494 data->have_in &= ~(1 << 6);
3505 * There does not seem to be a clean way to detect if VINx or
3506 * AUXTINx is active, so for keep both sensor types enabled
3512 /* Initialize the chip */
3513 nct6775_init_device(data);
3515 data->vrm = vid_which_vrm();
3516 err = superio_enter(sio_data->sioreg);
3522 * We can get the VID input values directly at logical device D 0xe3.
3524 superio_select(sio_data->sioreg, NCT6775_LD_VID);
3525 data->vid = superio_inb(sio_data->sioreg, 0xe3);
3530 superio_select(sio_data->sioreg, NCT6775_LD_HWM);
3531 tmp = superio_inb(sio_data->sioreg,
3532 NCT6775_REG_CR_FAN_DEBOUNCE);
3533 switch (data->kind) {
3542 superio_outb(sio_data->sioreg, NCT6775_REG_CR_FAN_DEBOUNCE,
3544 dev_info(&pdev->dev, "Enabled fan debounce for chip %s\n",
3548 superio_exit(sio_data->sioreg);
3550 err = device_create_file(dev, &dev_attr_cpu0_vid);
3554 err = nct6775_check_fan_inputs(sio_data, data);
3558 /* Read fan clock dividers immediately */
3559 nct6775_update_fan_div_common(dev, data);
3561 /* Register sysfs hooks */
3562 for (i = 0; i < data->pwm_num; i++) {
3563 if (!(data->has_pwm & (1 << i)))
3566 err = sysfs_create_group(&dev->kobj, &nct6775_group_pwm[i]);
3570 if (data->REG_PWM[3]) {
3571 err = device_create_file(dev,
3572 &sda_max_output[i].dev_attr);
3576 if (data->REG_PWM[4]) {
3577 err = device_create_file(dev,
3578 &sda_step_output[i].dev_attr);
3582 if (data->REG_PWM[6]) {
3583 err = device_create_file(dev,
3584 &sda_weight_duty_base[i].dev_attr);
3589 for (i = 0; i < ARRAY_SIZE(sda_auto_pwm_arrays); i++) {
3590 struct sensor_device_attribute_2 *attr =
3591 &sda_auto_pwm_arrays[i];
3593 if (!(data->has_pwm & (1 << attr->nr)))
3595 if (attr->index > data->auto_pwm_num)
3597 err = device_create_file(dev, &attr->dev_attr);
3602 for (i = 0; i < data->in_num; i++) {
3603 if (!(data->have_in & (1 << i)))
3605 err = sysfs_create_group(&dev->kobj, &nct6775_group_in[i]);
3610 for (i = 0; i < 5; i++) {
3611 if (data->has_fan & (1 << i)) {
3612 err = device_create_file(dev,
3613 &sda_fan_input[i].dev_attr);
3616 err = device_create_file(dev,
3617 &sda_fan_alarm[i].dev_attr);
3620 if (data->kind != nct6776 &&
3621 data->kind != nct6779) {
3622 err = device_create_file(dev,
3623 &sda_fan_div[i].dev_attr);
3627 if (data->has_fan_min & (1 << i)) {
3628 err = device_create_file(dev,
3629 &sda_fan_min[i].dev_attr);
3636 for (i = 0; i < NUM_TEMP; i++) {
3637 if (!(data->have_temp & (1 << i)))
3639 err = device_create_file(dev, &sda_temp_input[i].dev_attr);
3642 if (data->temp_label) {
3643 err = device_create_file(dev,
3644 &sda_temp_label[i].dev_attr);
3648 if (data->reg_temp[1][i]) {
3649 err = device_create_file(dev,
3650 &sda_temp_max[i].dev_attr);
3654 if (data->reg_temp[2][i]) {
3655 err = device_create_file(dev,
3656 &sda_temp_max_hyst[i].dev_attr);
3660 if (data->reg_temp[3][i]) {
3661 err = device_create_file(dev,
3662 &sda_temp_crit[i].dev_attr);
3666 if (!(data->have_temp_fixed & (1 << i)))
3668 err = device_create_file(dev, &sda_temp_type[i].dev_attr);
3671 err = device_create_file(dev, &sda_temp_offset[i].dev_attr);
3674 if (i >= NUM_TEMP_ALARM ||
3675 data->ALARM_BITS[TEMP_ALARM_BASE + i] < 0)
3677 err = device_create_file(dev, &sda_temp_alarm[i].dev_attr);
3682 for (i = 0; i < ARRAY_SIZE(sda_caseopen); i++) {
3683 if (data->ALARM_BITS[INTRUSION_ALARM_BASE + i] < 0)
3685 err = device_create_file(dev, &sda_caseopen[i].dev_attr);
3690 err = device_create_file(dev, &dev_attr_name);
3694 data->hwmon_dev = hwmon_device_register(dev);
3695 if (IS_ERR(data->hwmon_dev)) {
3696 err = PTR_ERR(data->hwmon_dev);
3703 nct6775_device_remove_files(dev);
3707 static int __devexit nct6775_remove(struct platform_device *pdev)
3709 struct nct6775_data *data = platform_get_drvdata(pdev);
3711 hwmon_device_unregister(data->hwmon_dev);
3712 nct6775_device_remove_files(&pdev->dev);
3717 static struct platform_driver nct6775_driver = {
3719 .owner = THIS_MODULE,
3722 .probe = nct6775_probe,
3723 .remove = __devexit_p(nct6775_remove),
3726 /* nct6775_find() looks for a '627 in the Super-I/O config space */
3727 static int __init nct6775_find(int sioaddr, unsigned short *addr,
3728 struct nct6775_sio_data *sio_data)
3730 static const char sio_name_NCT6775[] __initconst = "NCT6775F";
3731 static const char sio_name_NCT6776[] __initconst = "NCT6776F";
3732 static const char sio_name_NCT6779[] __initconst = "NCT6779D";
3735 const char *sio_name;
3738 err = superio_enter(sioaddr);
3745 val = (superio_inb(sioaddr, SIO_REG_DEVID) << 8)
3746 | superio_inb(sioaddr, SIO_REG_DEVID + 1);
3747 switch (val & SIO_ID_MASK) {
3748 case SIO_NCT6775_ID:
3749 sio_data->kind = nct6775;
3750 sio_name = sio_name_NCT6775;
3752 case SIO_NCT6776_ID:
3753 sio_data->kind = nct6776;
3754 sio_name = sio_name_NCT6776;
3756 case SIO_NCT6779_ID:
3757 sio_data->kind = nct6779;
3758 sio_name = sio_name_NCT6779;
3762 pr_debug("unsupported chip ID: 0x%04x\n", val);
3763 superio_exit(sioaddr);
3767 /* We have a known chip, find the HWM I/O address */
3768 superio_select(sioaddr, NCT6775_LD_HWM);
3769 val = (superio_inb(sioaddr, SIO_REG_ADDR) << 8)
3770 | superio_inb(sioaddr, SIO_REG_ADDR + 1);
3771 *addr = val & IOREGION_ALIGNMENT;
3773 pr_err("Refusing to enable a Super-I/O device with a base I/O port 0\n");
3774 superio_exit(sioaddr);
3778 /* Activate logical device if needed */
3779 val = superio_inb(sioaddr, SIO_REG_ENABLE);
3780 if (!(val & 0x01)) {
3781 pr_warn("Forcibly enabling Super-I/O. Sensor is probably unusable.\n");
3782 superio_outb(sioaddr, SIO_REG_ENABLE, val | 0x01);
3785 superio_exit(sioaddr);
3786 pr_info("Found %s chip at %#x\n", sio_name, *addr);
3787 sio_data->sioreg = sioaddr;
3793 * when Super-I/O functions move to a separate file, the Super-I/O
3794 * bus will manage the lifetime of the device and this module will only keep
3795 * track of the nct6775 driver. But since we platform_device_alloc(), we
3796 * must keep track of the device
3798 static struct platform_device *pdev;
3800 static int __init sensors_nct6775_init(void)
3803 unsigned short address;
3804 struct resource res;
3805 struct nct6775_sio_data sio_data;
3808 * initialize sio_data->kind and sio_data->sioreg.
3810 * when Super-I/O functions move to a separate file, the Super-I/O
3811 * driver will probe 0x2e and 0x4e and auto-detect the presence of a
3812 * nct6775 hardware monitor, and call probe()
3814 if (nct6775_find(0x2e, &address, &sio_data) &&
3815 nct6775_find(0x4e, &address, &sio_data))
3818 err = platform_driver_register(&nct6775_driver);
3822 pdev = platform_device_alloc(DRVNAME, address);
3825 pr_err("Device allocation failed\n");
3826 goto exit_unregister;
3829 err = platform_device_add_data(pdev, &sio_data,
3830 sizeof(struct nct6775_sio_data));
3832 pr_err("Platform data allocation failed\n");
3833 goto exit_device_put;
3836 memset(&res, 0, sizeof(res));
3838 res.start = address + IOREGION_OFFSET;
3839 res.end = address + IOREGION_OFFSET + IOREGION_LENGTH - 1;
3840 res.flags = IORESOURCE_IO;
3842 err = acpi_check_resource_conflict(&res);
3844 goto exit_device_put;
3846 err = platform_device_add_resources(pdev, &res, 1);
3848 pr_err("Device resource addition failed (%d)\n", err);
3849 goto exit_device_put;
3852 /* platform_device_add calls probe() */
3853 err = platform_device_add(pdev);
3855 pr_err("Device addition failed (%d)\n", err);
3856 goto exit_device_put;
3862 platform_device_put(pdev);
3864 platform_driver_unregister(&nct6775_driver);
3869 static void __exit sensors_nct6775_exit(void)
3871 platform_device_unregister(pdev);
3872 platform_driver_unregister(&nct6775_driver);
3875 MODULE_AUTHOR("Guenter Roeck <linux@roeck-us.net>");
3876 MODULE_DESCRIPTION("NCT6775F/NCT6776F/NCT6779D driver");
3877 MODULE_LICENSE("GPL");
3879 module_init(sensors_nct6775_init);
3880 module_exit(sensors_nct6775_exit);