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[3][NUM_TEMP]; /* 0=temp, 1=temp_over, 2=temp_hyst */
618 u8 pwm_num; /* number of pwm */
620 u8 pwm_mode[5]; /* 1->DC variable voltage, 0->PWM variable duty cycle */
621 u8 pwm_enable[5]; /* 0->off
623 * 2->thermal cruise mode (also called SmartFan I)
624 * 3->fan speed cruise mode
626 * 5->enhanced variable thermal cruise (SmartFan IV)
628 u8 pwm[7][5]; /* [0]=pwm, [1]=start_output, [2]=stop_output,
629 * [3]=max_output, [4]=step_output,
630 * [5]=weight_duty_step, [6]=weight_duty_base
635 u8 fan_time[3][5]; /* 0 = stop_time, 1 = step_up, 2 = step_down */
637 /* Automatic fan speed control registers */
642 u8 pwm_temp_sel[2][5];
644 bool pwm_sel_enable[3][5];/* 0->stop_val, 1->weight, 2->step;
645 * false->off, true->on
647 u8 weight_temp[3][5]; /* 0->temp_step, 1->temp_step_tol,
659 struct nct6775_sio_data {
664 static bool is_word_sized(struct nct6775_data *data, u16 reg)
666 switch (data->kind) {
668 return (((reg & 0xff00) == 0x100 ||
669 (reg & 0xff00) == 0x200) &&
670 ((reg & 0x00ff) == 0x50 ||
671 (reg & 0x00ff) == 0x53 ||
672 (reg & 0x00ff) == 0x55)) ||
673 (reg & 0xfff0) == 0x630 ||
674 reg == 0x640 || reg == 0x642 ||
676 ((reg & 0xfff0) == 0x650 && (reg & 0x000f) >= 0x06) ||
677 reg == 0x73 || reg == 0x75 || reg == 0x77;
679 return (((reg & 0xff00) == 0x100 ||
680 (reg & 0xff00) == 0x200) &&
681 ((reg & 0x00ff) == 0x50 ||
682 (reg & 0x00ff) == 0x53 ||
683 (reg & 0x00ff) == 0x55)) ||
684 (reg & 0xfff0) == 0x630 ||
686 reg == 0x640 || reg == 0x642 ||
687 ((reg & 0xfff0) == 0x650 && (reg & 0x000f) >= 0x06) ||
688 reg == 0x73 || reg == 0x75 || reg == 0x77;
690 return reg == 0x150 || reg == 0x153 || reg == 0x155 ||
691 ((reg & 0xfff0) == 0x4b0 && (reg & 0x000f) < 0x09) ||
693 reg == 0x63a || reg == 0x63c || reg == 0x63e ||
694 reg == 0x640 || reg == 0x642 ||
695 reg == 0x73 || reg == 0x75 || reg == 0x77 || reg == 0x79 ||
702 * On older chips, only registers 0x50-0x5f are banked.
703 * On more recent chips, all registers are banked.
704 * Assume that is the case and set the bank number for each access.
705 * Cache the bank number so it only needs to be set if it changes.
707 static inline void nct6775_set_bank(struct nct6775_data *data, u16 reg)
710 if (data->bank != bank) {
711 outb_p(NCT6775_REG_BANK, data->addr + ADDR_REG_OFFSET);
712 outb_p(bank, data->addr + DATA_REG_OFFSET);
717 static u16 nct6775_read_value(struct nct6775_data *data, u16 reg)
719 int res, word_sized = is_word_sized(data, reg);
721 mutex_lock(&data->lock);
723 nct6775_set_bank(data, reg);
724 outb_p(reg & 0xff, data->addr + ADDR_REG_OFFSET);
725 res = inb_p(data->addr + DATA_REG_OFFSET);
727 outb_p((reg & 0xff) + 1,
728 data->addr + ADDR_REG_OFFSET);
729 res = (res << 8) + inb_p(data->addr + DATA_REG_OFFSET);
732 mutex_unlock(&data->lock);
736 static int nct6775_write_value(struct nct6775_data *data, u16 reg, u16 value)
738 int word_sized = is_word_sized(data, reg);
740 mutex_lock(&data->lock);
742 nct6775_set_bank(data, reg);
743 outb_p(reg & 0xff, data->addr + ADDR_REG_OFFSET);
745 outb_p(value >> 8, data->addr + DATA_REG_OFFSET);
746 outb_p((reg & 0xff) + 1,
747 data->addr + ADDR_REG_OFFSET);
749 outb_p(value & 0xff, data->addr + DATA_REG_OFFSET);
751 mutex_unlock(&data->lock);
755 /* We left-align 8-bit temperature values to make the code simpler */
756 static u16 nct6775_read_temp(struct nct6775_data *data, u16 reg)
760 res = nct6775_read_value(data, reg);
761 if (!is_word_sized(data, reg))
767 static int nct6775_write_temp(struct nct6775_data *data, u16 reg, u16 value)
769 if (!is_word_sized(data, reg))
771 return nct6775_write_value(data, reg, value);
774 /* This function assumes that the caller holds data->update_lock */
775 static void nct6775_write_fan_div(struct nct6775_data *data, int nr)
781 reg = (nct6775_read_value(data, NCT6775_REG_FANDIV1) & 0x70)
782 | (data->fan_div[0] & 0x7);
783 nct6775_write_value(data, NCT6775_REG_FANDIV1, reg);
786 reg = (nct6775_read_value(data, NCT6775_REG_FANDIV1) & 0x7)
787 | ((data->fan_div[1] << 4) & 0x70);
788 nct6775_write_value(data, NCT6775_REG_FANDIV1, reg);
791 reg = (nct6775_read_value(data, NCT6775_REG_FANDIV2) & 0x70)
792 | (data->fan_div[2] & 0x7);
793 nct6775_write_value(data, NCT6775_REG_FANDIV2, reg);
796 reg = (nct6775_read_value(data, NCT6775_REG_FANDIV2) & 0x7)
797 | ((data->fan_div[3] << 4) & 0x70);
798 nct6775_write_value(data, NCT6775_REG_FANDIV2, reg);
803 static void nct6775_write_fan_div_common(struct device *dev,
804 struct nct6775_data *data, int nr)
806 if (data->kind == nct6775)
807 nct6775_write_fan_div(data, nr);
810 static void nct6775_update_fan_div(struct nct6775_data *data)
814 i = nct6775_read_value(data, NCT6775_REG_FANDIV1);
815 data->fan_div[0] = i & 0x7;
816 data->fan_div[1] = (i & 0x70) >> 4;
817 i = nct6775_read_value(data, NCT6775_REG_FANDIV2);
818 data->fan_div[2] = i & 0x7;
819 if (data->has_fan & (1<<3))
820 data->fan_div[3] = (i & 0x70) >> 4;
823 static void nct6775_update_fan_div_common(struct device *dev,
824 struct nct6775_data *data)
826 if (data->kind == nct6775)
827 nct6775_update_fan_div(data);
830 static void nct6775_update_pwm(struct device *dev)
832 struct nct6775_data *data = dev_get_drvdata(dev);
837 for (i = 0; i < data->pwm_num; i++) {
838 if (!(data->has_pwm & (1 << i)))
841 duty_is_dc = data->REG_PWM_MODE[i] &&
842 (nct6775_read_value(data, data->REG_PWM_MODE[i])
843 & data->PWM_MODE_MASK[i]);
844 data->pwm_mode[i] = duty_is_dc;
846 fanmodecfg = nct6775_read_value(data, data->REG_FAN_MODE[i]);
847 for (j = 0; j < ARRAY_SIZE(data->REG_PWM); j++) {
848 if (data->REG_PWM[j] && data->REG_PWM[j][i]) {
850 = nct6775_read_value(data,
851 data->REG_PWM[j][i]);
855 data->pwm_enable[i] = reg_to_pwm_enable(data->pwm[0][i],
856 (fanmodecfg >> 4) & 7);
858 data->tolerance[i][0] = fanmodecfg & 0x0f;
859 if (data->kind == nct6779) {
860 reg = nct6775_read_value(data,
861 NCT6779_REG_TOLERANCE_H[i]);
862 data->tolerance[i][0] |= (reg & 0x70) >> 1;
864 data->tolerance[i][1] =
865 nct6775_read_value(data,
866 data->REG_CRITICAL_TEMP_TOLERANCE[i]);
868 data->pwm_sel_enable[0][i] =
869 nct6775_read_value(data, data->REG_TEMP_SEL[0][i])
871 data->pwm_sel_enable[1][i] =
872 nct6775_read_value(data, data->REG_TEMP_SEL[1][i])
874 data->pwm_sel_enable[2][i] =
875 nct6775_read_value(data, data->REG_TEMP_SEL[2][i])
879 for (j = 0; j < 2; j++) {
880 reg = nct6775_read_value(data,
881 data->REG_TEMP_SEL[j][i]);
882 data->pwm_temp_sel[j][i] = reg & 0x1f;
884 /* Weight temp data */
885 for (j = 0; j < 3; j++) {
886 data->weight_temp[j][i]
887 = nct6775_read_value(data,
888 data->REG_WEIGHT_TEMP[j][i]);
893 static void nct6775_update_pwm_limits(struct device *dev)
895 struct nct6775_data *data = dev_get_drvdata(dev);
899 for (i = 0; i < data->pwm_num; i++) {
900 if (!(data->has_pwm & (1 << i)))
903 for (j = 0; j < 3; j++) {
904 data->fan_time[j][i] =
905 nct6775_read_value(data, data->REG_FAN_TIME[j][i]);
908 data->target_temp[i] =
909 nct6775_read_value(data, data->REG_TARGET[i]) &
910 (data->pwm_mode[i] ? 0xff : 0x7f);
912 for (j = 0; j < data->auto_pwm_num; j++) {
913 data->auto_pwm[i][j] =
914 nct6775_read_value(data,
915 NCT6775_AUTO_PWM(data, i, j));
916 data->auto_temp[i][j] =
917 nct6775_read_value(data,
918 NCT6775_AUTO_TEMP(data, i, j));
921 /* critical auto_pwm temperature data */
922 data->auto_temp[i][data->auto_pwm_num] =
923 nct6775_read_value(data, data->REG_CRITICAL_TEMP[i]);
925 switch (data->kind) {
927 reg = nct6775_read_value(data,
928 NCT6775_REG_CRITICAL_ENAB[i]);
929 data->auto_pwm[i][data->auto_pwm_num] =
930 (reg & 0x02) ? 0xff : 0x00;
933 data->auto_pwm[i][data->auto_pwm_num] = 0xff;
936 reg = nct6775_read_value(data,
937 NCT6779_REG_CRITICAL_PWM_ENABLE[i]);
939 data->auto_pwm[i][data->auto_pwm_num] =
940 nct6775_read_value(data,
941 NCT6779_REG_CRITICAL_PWM[i]);
943 data->auto_pwm[i][data->auto_pwm_num] = 0xff;
949 static struct nct6775_data *nct6775_update_device(struct device *dev)
951 struct nct6775_data *data = dev_get_drvdata(dev);
954 mutex_lock(&data->update_lock);
956 if (time_after(jiffies, data->last_updated + HZ + HZ/2)
958 /* Fan clock dividers */
959 nct6775_update_fan_div_common(dev, data);
961 /* Measured voltages and limits */
962 for (i = 0; i < data->in_num; i++) {
963 if (!(data->have_in & (1 << i)))
966 data->in[i][0] = nct6775_read_value(data,
968 data->in[i][1] = nct6775_read_value(data,
969 data->REG_IN_MINMAX[0][i]);
970 data->in[i][2] = nct6775_read_value(data,
971 data->REG_IN_MINMAX[1][i]);
974 /* Measured fan speeds and limits */
975 for (i = 0; i < 5; i++) {
978 if (!(data->has_fan & (1 << i)))
981 reg = nct6775_read_value(data, data->REG_FAN[i]);
982 data->rpm[i] = data->fan_from_reg(reg,
985 if (data->has_fan_min & (1 << i))
986 data->fan_min[i] = nct6775_read_value(data,
987 data->REG_FAN_MIN[i]);
990 * If we failed to measure the fan speed and clock
991 * divider can be increased, let's try that for next
994 if (data->has_fan_div && (reg >= 0xff || reg == 0x00)
995 && data->fan_div[i] < 0x07) {
997 "Increasing fan%d clock divider from %u to %u\n",
998 i + 1, div_from_reg(data->fan_div[i]),
999 div_from_reg(data->fan_div[i] + 1));
1001 nct6775_write_fan_div_common(dev, data, i);
1002 /* Preserve min limit if possible */
1003 if ((data->has_fan_min & (1 << i))
1004 && data->fan_min[i] >= 2
1005 && data->fan_min[i] != 255)
1006 nct6775_write_value(data,
1007 data->REG_FAN_MIN[i],
1008 (data->fan_min[i] /= 2));
1012 nct6775_update_pwm(dev);
1013 nct6775_update_pwm_limits(dev);
1015 /* Measured temperatures and limits */
1016 for (i = 0; i < NUM_TEMP; i++) {
1017 if (!(data->have_temp & (1 << i)))
1019 for (j = 0; j < 4; j++) {
1020 if (data->reg_temp[j][i])
1022 = nct6775_read_temp(data,
1023 data->reg_temp[j][i]);
1025 if (!(data->have_temp_fixed & (1 << i)))
1027 data->temp_offset[i]
1028 = nct6775_read_value(data, data->REG_TEMP_OFFSET[i]);
1032 for (i = 0; i < 6; i++) {
1034 if (!data->REG_ALARM[i])
1036 alarm = nct6775_read_value(data, data->REG_ALARM[i]);
1037 data->alarms |= ((u64)alarm) << (i << 3);
1040 data->last_updated = jiffies;
1044 mutex_unlock(&data->update_lock);
1049 * Sysfs callback functions
1052 show_in_reg(struct device *dev, struct device_attribute *attr, char *buf)
1054 struct nct6775_data *data = nct6775_update_device(dev);
1055 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
1057 int index = sattr->index;
1058 return sprintf(buf, "%ld\n", in_from_reg(data->in[nr][index], nr));
1062 store_in_reg(struct device *dev, struct device_attribute *attr, const char *buf,
1065 struct nct6775_data *data = dev_get_drvdata(dev);
1066 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
1068 int index = sattr->index;
1070 int err = kstrtoul(buf, 10, &val);
1073 mutex_lock(&data->update_lock);
1074 data->in[nr][index] = in_to_reg(val, nr);
1075 nct6775_write_value(data, data->REG_IN_MINMAX[index-1][nr],
1076 data->in[nr][index]);
1077 mutex_unlock(&data->update_lock);
1082 show_alarm(struct device *dev, struct device_attribute *attr, char *buf)
1084 struct nct6775_data *data = nct6775_update_device(dev);
1085 struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr);
1086 int nr = data->ALARM_BITS[sattr->index];
1087 return sprintf(buf, "%u\n",
1088 (unsigned int)((data->alarms >> nr) & 0x01));
1091 static SENSOR_DEVICE_ATTR_2(in0_input, S_IRUGO, show_in_reg, NULL, 0, 0);
1092 static SENSOR_DEVICE_ATTR_2(in1_input, S_IRUGO, show_in_reg, NULL, 1, 0);
1093 static SENSOR_DEVICE_ATTR_2(in2_input, S_IRUGO, show_in_reg, NULL, 2, 0);
1094 static SENSOR_DEVICE_ATTR_2(in3_input, S_IRUGO, show_in_reg, NULL, 3, 0);
1095 static SENSOR_DEVICE_ATTR_2(in4_input, S_IRUGO, show_in_reg, NULL, 4, 0);
1096 static SENSOR_DEVICE_ATTR_2(in5_input, S_IRUGO, show_in_reg, NULL, 5, 0);
1097 static SENSOR_DEVICE_ATTR_2(in6_input, S_IRUGO, show_in_reg, NULL, 6, 0);
1098 static SENSOR_DEVICE_ATTR_2(in7_input, S_IRUGO, show_in_reg, NULL, 7, 0);
1099 static SENSOR_DEVICE_ATTR_2(in8_input, S_IRUGO, show_in_reg, NULL, 8, 0);
1100 static SENSOR_DEVICE_ATTR_2(in9_input, S_IRUGO, show_in_reg, NULL, 9, 0);
1101 static SENSOR_DEVICE_ATTR_2(in10_input, S_IRUGO, show_in_reg, NULL, 10, 0);
1102 static SENSOR_DEVICE_ATTR_2(in11_input, S_IRUGO, show_in_reg, NULL, 11, 0);
1103 static SENSOR_DEVICE_ATTR_2(in12_input, S_IRUGO, show_in_reg, NULL, 12, 0);
1104 static SENSOR_DEVICE_ATTR_2(in13_input, S_IRUGO, show_in_reg, NULL, 13, 0);
1105 static SENSOR_DEVICE_ATTR_2(in14_input, S_IRUGO, show_in_reg, NULL, 14, 0);
1107 static SENSOR_DEVICE_ATTR(in0_alarm, S_IRUGO, show_alarm, NULL, 0);
1108 static SENSOR_DEVICE_ATTR(in1_alarm, S_IRUGO, show_alarm, NULL, 1);
1109 static SENSOR_DEVICE_ATTR(in2_alarm, S_IRUGO, show_alarm, NULL, 2);
1110 static SENSOR_DEVICE_ATTR(in3_alarm, S_IRUGO, show_alarm, NULL, 3);
1111 static SENSOR_DEVICE_ATTR(in4_alarm, S_IRUGO, show_alarm, NULL, 4);
1112 static SENSOR_DEVICE_ATTR(in5_alarm, S_IRUGO, show_alarm, NULL, 5);
1113 static SENSOR_DEVICE_ATTR(in6_alarm, S_IRUGO, show_alarm, NULL, 6);
1114 static SENSOR_DEVICE_ATTR(in7_alarm, S_IRUGO, show_alarm, NULL, 7);
1115 static SENSOR_DEVICE_ATTR(in8_alarm, S_IRUGO, show_alarm, NULL, 8);
1116 static SENSOR_DEVICE_ATTR(in9_alarm, S_IRUGO, show_alarm, NULL, 9);
1117 static SENSOR_DEVICE_ATTR(in10_alarm, S_IRUGO, show_alarm, NULL, 10);
1118 static SENSOR_DEVICE_ATTR(in11_alarm, S_IRUGO, show_alarm, NULL, 11);
1119 static SENSOR_DEVICE_ATTR(in12_alarm, S_IRUGO, show_alarm, NULL, 12);
1120 static SENSOR_DEVICE_ATTR(in13_alarm, S_IRUGO, show_alarm, NULL, 13);
1121 static SENSOR_DEVICE_ATTR(in14_alarm, S_IRUGO, show_alarm, NULL, 14);
1123 static SENSOR_DEVICE_ATTR_2(in0_min, S_IWUSR | S_IRUGO, show_in_reg,
1124 store_in_reg, 0, 1);
1125 static SENSOR_DEVICE_ATTR_2(in1_min, S_IWUSR | S_IRUGO, show_in_reg,
1126 store_in_reg, 1, 1);
1127 static SENSOR_DEVICE_ATTR_2(in2_min, S_IWUSR | S_IRUGO, show_in_reg,
1128 store_in_reg, 2, 1);
1129 static SENSOR_DEVICE_ATTR_2(in3_min, S_IWUSR | S_IRUGO, show_in_reg,
1130 store_in_reg, 3, 1);
1131 static SENSOR_DEVICE_ATTR_2(in4_min, S_IWUSR | S_IRUGO, show_in_reg,
1132 store_in_reg, 4, 1);
1133 static SENSOR_DEVICE_ATTR_2(in5_min, S_IWUSR | S_IRUGO, show_in_reg,
1134 store_in_reg, 5, 1);
1135 static SENSOR_DEVICE_ATTR_2(in6_min, S_IWUSR | S_IRUGO, show_in_reg,
1136 store_in_reg, 6, 1);
1137 static SENSOR_DEVICE_ATTR_2(in7_min, S_IWUSR | S_IRUGO, show_in_reg,
1138 store_in_reg, 7, 1);
1139 static SENSOR_DEVICE_ATTR_2(in8_min, S_IWUSR | S_IRUGO, show_in_reg,
1140 store_in_reg, 8, 1);
1141 static SENSOR_DEVICE_ATTR_2(in9_min, S_IWUSR | S_IRUGO, show_in_reg,
1142 store_in_reg, 9, 1);
1143 static SENSOR_DEVICE_ATTR_2(in10_min, S_IWUSR | S_IRUGO, show_in_reg,
1144 store_in_reg, 10, 1);
1145 static SENSOR_DEVICE_ATTR_2(in11_min, S_IWUSR | S_IRUGO, show_in_reg,
1146 store_in_reg, 11, 1);
1147 static SENSOR_DEVICE_ATTR_2(in12_min, S_IWUSR | S_IRUGO, show_in_reg,
1148 store_in_reg, 12, 1);
1149 static SENSOR_DEVICE_ATTR_2(in13_min, S_IWUSR | S_IRUGO, show_in_reg,
1150 store_in_reg, 13, 1);
1151 static SENSOR_DEVICE_ATTR_2(in14_min, S_IWUSR | S_IRUGO, show_in_reg,
1152 store_in_reg, 14, 1);
1154 static SENSOR_DEVICE_ATTR_2(in0_max, S_IWUSR | S_IRUGO, show_in_reg,
1155 store_in_reg, 0, 2);
1156 static SENSOR_DEVICE_ATTR_2(in1_max, S_IWUSR | S_IRUGO, show_in_reg,
1157 store_in_reg, 1, 2);
1158 static SENSOR_DEVICE_ATTR_2(in2_max, S_IWUSR | S_IRUGO, show_in_reg,
1159 store_in_reg, 2, 2);
1160 static SENSOR_DEVICE_ATTR_2(in3_max, S_IWUSR | S_IRUGO, show_in_reg,
1161 store_in_reg, 3, 2);
1162 static SENSOR_DEVICE_ATTR_2(in4_max, S_IWUSR | S_IRUGO, show_in_reg,
1163 store_in_reg, 4, 2);
1164 static SENSOR_DEVICE_ATTR_2(in5_max, S_IWUSR | S_IRUGO, show_in_reg,
1165 store_in_reg, 5, 2);
1166 static SENSOR_DEVICE_ATTR_2(in6_max, S_IWUSR | S_IRUGO, show_in_reg,
1167 store_in_reg, 6, 2);
1168 static SENSOR_DEVICE_ATTR_2(in7_max, S_IWUSR | S_IRUGO, show_in_reg,
1169 store_in_reg, 7, 2);
1170 static SENSOR_DEVICE_ATTR_2(in8_max, S_IWUSR | S_IRUGO, show_in_reg,
1171 store_in_reg, 8, 2);
1172 static SENSOR_DEVICE_ATTR_2(in9_max, S_IWUSR | S_IRUGO, show_in_reg,
1173 store_in_reg, 9, 2);
1174 static SENSOR_DEVICE_ATTR_2(in10_max, S_IWUSR | S_IRUGO, show_in_reg,
1175 store_in_reg, 10, 2);
1176 static SENSOR_DEVICE_ATTR_2(in11_max, S_IWUSR | S_IRUGO, show_in_reg,
1177 store_in_reg, 11, 2);
1178 static SENSOR_DEVICE_ATTR_2(in12_max, S_IWUSR | S_IRUGO, show_in_reg,
1179 store_in_reg, 12, 2);
1180 static SENSOR_DEVICE_ATTR_2(in13_max, S_IWUSR | S_IRUGO, show_in_reg,
1181 store_in_reg, 13, 2);
1182 static SENSOR_DEVICE_ATTR_2(in14_max, S_IWUSR | S_IRUGO, show_in_reg,
1183 store_in_reg, 14, 2);
1185 static struct attribute *nct6775_attributes_in[15][5] = {
1187 &sensor_dev_attr_in0_input.dev_attr.attr,
1188 &sensor_dev_attr_in0_min.dev_attr.attr,
1189 &sensor_dev_attr_in0_max.dev_attr.attr,
1190 &sensor_dev_attr_in0_alarm.dev_attr.attr,
1194 &sensor_dev_attr_in1_input.dev_attr.attr,
1195 &sensor_dev_attr_in1_min.dev_attr.attr,
1196 &sensor_dev_attr_in1_max.dev_attr.attr,
1197 &sensor_dev_attr_in1_alarm.dev_attr.attr,
1201 &sensor_dev_attr_in2_input.dev_attr.attr,
1202 &sensor_dev_attr_in2_min.dev_attr.attr,
1203 &sensor_dev_attr_in2_max.dev_attr.attr,
1204 &sensor_dev_attr_in2_alarm.dev_attr.attr,
1208 &sensor_dev_attr_in3_input.dev_attr.attr,
1209 &sensor_dev_attr_in3_min.dev_attr.attr,
1210 &sensor_dev_attr_in3_max.dev_attr.attr,
1211 &sensor_dev_attr_in3_alarm.dev_attr.attr,
1215 &sensor_dev_attr_in4_input.dev_attr.attr,
1216 &sensor_dev_attr_in4_min.dev_attr.attr,
1217 &sensor_dev_attr_in4_max.dev_attr.attr,
1218 &sensor_dev_attr_in4_alarm.dev_attr.attr,
1222 &sensor_dev_attr_in5_input.dev_attr.attr,
1223 &sensor_dev_attr_in5_min.dev_attr.attr,
1224 &sensor_dev_attr_in5_max.dev_attr.attr,
1225 &sensor_dev_attr_in5_alarm.dev_attr.attr,
1229 &sensor_dev_attr_in6_input.dev_attr.attr,
1230 &sensor_dev_attr_in6_min.dev_attr.attr,
1231 &sensor_dev_attr_in6_max.dev_attr.attr,
1232 &sensor_dev_attr_in6_alarm.dev_attr.attr,
1236 &sensor_dev_attr_in7_input.dev_attr.attr,
1237 &sensor_dev_attr_in7_min.dev_attr.attr,
1238 &sensor_dev_attr_in7_max.dev_attr.attr,
1239 &sensor_dev_attr_in7_alarm.dev_attr.attr,
1243 &sensor_dev_attr_in8_input.dev_attr.attr,
1244 &sensor_dev_attr_in8_min.dev_attr.attr,
1245 &sensor_dev_attr_in8_max.dev_attr.attr,
1246 &sensor_dev_attr_in8_alarm.dev_attr.attr,
1250 &sensor_dev_attr_in9_input.dev_attr.attr,
1251 &sensor_dev_attr_in9_min.dev_attr.attr,
1252 &sensor_dev_attr_in9_max.dev_attr.attr,
1253 &sensor_dev_attr_in9_alarm.dev_attr.attr,
1257 &sensor_dev_attr_in10_input.dev_attr.attr,
1258 &sensor_dev_attr_in10_min.dev_attr.attr,
1259 &sensor_dev_attr_in10_max.dev_attr.attr,
1260 &sensor_dev_attr_in10_alarm.dev_attr.attr,
1264 &sensor_dev_attr_in11_input.dev_attr.attr,
1265 &sensor_dev_attr_in11_min.dev_attr.attr,
1266 &sensor_dev_attr_in11_max.dev_attr.attr,
1267 &sensor_dev_attr_in11_alarm.dev_attr.attr,
1271 &sensor_dev_attr_in12_input.dev_attr.attr,
1272 &sensor_dev_attr_in12_min.dev_attr.attr,
1273 &sensor_dev_attr_in12_max.dev_attr.attr,
1274 &sensor_dev_attr_in12_alarm.dev_attr.attr,
1278 &sensor_dev_attr_in13_input.dev_attr.attr,
1279 &sensor_dev_attr_in13_min.dev_attr.attr,
1280 &sensor_dev_attr_in13_max.dev_attr.attr,
1281 &sensor_dev_attr_in13_alarm.dev_attr.attr,
1285 &sensor_dev_attr_in14_input.dev_attr.attr,
1286 &sensor_dev_attr_in14_min.dev_attr.attr,
1287 &sensor_dev_attr_in14_max.dev_attr.attr,
1288 &sensor_dev_attr_in14_alarm.dev_attr.attr,
1293 static const struct attribute_group nct6775_group_in[15] = {
1294 { .attrs = nct6775_attributes_in[0] },
1295 { .attrs = nct6775_attributes_in[1] },
1296 { .attrs = nct6775_attributes_in[2] },
1297 { .attrs = nct6775_attributes_in[3] },
1298 { .attrs = nct6775_attributes_in[4] },
1299 { .attrs = nct6775_attributes_in[5] },
1300 { .attrs = nct6775_attributes_in[6] },
1301 { .attrs = nct6775_attributes_in[7] },
1302 { .attrs = nct6775_attributes_in[8] },
1303 { .attrs = nct6775_attributes_in[9] },
1304 { .attrs = nct6775_attributes_in[10] },
1305 { .attrs = nct6775_attributes_in[11] },
1306 { .attrs = nct6775_attributes_in[12] },
1307 { .attrs = nct6775_attributes_in[13] },
1308 { .attrs = nct6775_attributes_in[14] },
1312 show_fan(struct device *dev, struct device_attribute *attr, char *buf)
1314 struct nct6775_data *data = nct6775_update_device(dev);
1315 struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr);
1316 int nr = sattr->index;
1317 return sprintf(buf, "%d\n", data->rpm[nr]);
1321 show_fan_min(struct device *dev, struct device_attribute *attr, char *buf)
1323 struct nct6775_data *data = nct6775_update_device(dev);
1324 struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr);
1325 int nr = sattr->index;
1326 return sprintf(buf, "%d\n",
1327 data->fan_from_reg_min(data->fan_min[nr],
1328 data->fan_div[nr]));
1332 show_fan_div(struct device *dev, struct device_attribute *attr, char *buf)
1334 struct nct6775_data *data = nct6775_update_device(dev);
1335 struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr);
1336 int nr = sattr->index;
1337 return sprintf(buf, "%u\n", div_from_reg(data->fan_div[nr]));
1341 store_fan_min(struct device *dev, struct device_attribute *attr,
1342 const char *buf, size_t count)
1344 struct nct6775_data *data = dev_get_drvdata(dev);
1345 struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr);
1346 int nr = sattr->index;
1352 err = kstrtoul(buf, 10, &val);
1356 mutex_lock(&data->update_lock);
1357 if (!data->has_fan_div) {
1358 /* NCT6776F or NCT6779D; we know this is a 13 bit register */
1364 val = 1350000U / val;
1365 val = (val & 0x1f) | ((val << 3) & 0xff00);
1367 data->fan_min[nr] = val;
1368 goto write_min; /* Leave fan divider alone */
1371 /* No min limit, alarm disabled */
1372 data->fan_min[nr] = 255;
1373 new_div = data->fan_div[nr]; /* No change */
1374 dev_info(dev, "fan%u low limit and alarm disabled\n", nr + 1);
1377 reg = 1350000U / val;
1378 if (reg >= 128 * 255) {
1380 * Speed below this value cannot possibly be represented,
1381 * even with the highest divider (128)
1383 data->fan_min[nr] = 254;
1384 new_div = 7; /* 128 == (1 << 7) */
1386 "fan%u low limit %lu below minimum %u, set to minimum\n",
1387 nr + 1, val, data->fan_from_reg_min(254, 7));
1390 * Speed above this value cannot possibly be represented,
1391 * even with the lowest divider (1)
1393 data->fan_min[nr] = 1;
1394 new_div = 0; /* 1 == (1 << 0) */
1396 "fan%u low limit %lu above maximum %u, set to maximum\n",
1397 nr + 1, val, data->fan_from_reg_min(1, 0));
1400 * Automatically pick the best divider, i.e. the one such
1401 * that the min limit will correspond to a register value
1402 * in the 96..192 range
1405 while (reg > 192 && new_div < 7) {
1409 data->fan_min[nr] = reg;
1414 * Write both the fan clock divider (if it changed) and the new
1415 * fan min (unconditionally)
1417 if (new_div != data->fan_div[nr]) {
1418 dev_dbg(dev, "fan%u clock divider changed from %u to %u\n",
1419 nr + 1, div_from_reg(data->fan_div[nr]),
1420 div_from_reg(new_div));
1421 data->fan_div[nr] = new_div;
1422 nct6775_write_fan_div_common(dev, data, nr);
1423 /* Give the chip time to sample a new speed value */
1424 data->last_updated = jiffies;
1428 nct6775_write_value(data, data->REG_FAN_MIN[nr], data->fan_min[nr]);
1429 mutex_unlock(&data->update_lock);
1434 static struct sensor_device_attribute sda_fan_input[] = {
1435 SENSOR_ATTR(fan1_input, S_IRUGO, show_fan, NULL, 0),
1436 SENSOR_ATTR(fan2_input, S_IRUGO, show_fan, NULL, 1),
1437 SENSOR_ATTR(fan3_input, S_IRUGO, show_fan, NULL, 2),
1438 SENSOR_ATTR(fan4_input, S_IRUGO, show_fan, NULL, 3),
1439 SENSOR_ATTR(fan5_input, S_IRUGO, show_fan, NULL, 4),
1442 static struct sensor_device_attribute sda_fan_alarm[] = {
1443 SENSOR_ATTR(fan1_alarm, S_IRUGO, show_alarm, NULL, FAN_ALARM_BASE),
1444 SENSOR_ATTR(fan2_alarm, S_IRUGO, show_alarm, NULL, FAN_ALARM_BASE + 1),
1445 SENSOR_ATTR(fan3_alarm, S_IRUGO, show_alarm, NULL, FAN_ALARM_BASE + 2),
1446 SENSOR_ATTR(fan4_alarm, S_IRUGO, show_alarm, NULL, FAN_ALARM_BASE + 3),
1447 SENSOR_ATTR(fan5_alarm, S_IRUGO, show_alarm, NULL, FAN_ALARM_BASE + 4),
1450 static struct sensor_device_attribute sda_fan_min[] = {
1451 SENSOR_ATTR(fan1_min, S_IWUSR | S_IRUGO, show_fan_min,
1453 SENSOR_ATTR(fan2_min, S_IWUSR | S_IRUGO, show_fan_min,
1455 SENSOR_ATTR(fan3_min, S_IWUSR | S_IRUGO, show_fan_min,
1457 SENSOR_ATTR(fan4_min, S_IWUSR | S_IRUGO, show_fan_min,
1459 SENSOR_ATTR(fan5_min, S_IWUSR | S_IRUGO, show_fan_min,
1463 static struct sensor_device_attribute sda_fan_div[] = {
1464 SENSOR_ATTR(fan1_div, S_IRUGO, show_fan_div, NULL, 0),
1465 SENSOR_ATTR(fan2_div, S_IRUGO, show_fan_div, NULL, 1),
1466 SENSOR_ATTR(fan3_div, S_IRUGO, show_fan_div, NULL, 2),
1467 SENSOR_ATTR(fan4_div, S_IRUGO, show_fan_div, NULL, 3),
1468 SENSOR_ATTR(fan5_div, S_IRUGO, show_fan_div, NULL, 4),
1472 show_temp_label(struct device *dev, struct device_attribute *attr, char *buf)
1474 struct nct6775_data *data = nct6775_update_device(dev);
1475 struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr);
1476 int nr = sattr->index;
1477 return sprintf(buf, "%s\n", data->temp_label[data->temp_src[nr]]);
1481 show_temp(struct device *dev, struct device_attribute *attr, char *buf)
1483 struct nct6775_data *data = nct6775_update_device(dev);
1484 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
1486 int index = sattr->index;
1488 return sprintf(buf, "%d\n", LM75_TEMP_FROM_REG(data->temp[index][nr]));
1492 store_temp(struct device *dev, struct device_attribute *attr, const char *buf,
1495 struct nct6775_data *data = dev_get_drvdata(dev);
1496 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
1498 int index = sattr->index;
1502 err = kstrtol(buf, 10, &val);
1506 mutex_lock(&data->update_lock);
1507 data->temp[index][nr] = LM75_TEMP_TO_REG(val);
1508 nct6775_write_temp(data, data->reg_temp[index][nr],
1509 data->temp[index][nr]);
1510 mutex_unlock(&data->update_lock);
1515 show_temp_offset(struct device *dev, struct device_attribute *attr, char *buf)
1517 struct nct6775_data *data = nct6775_update_device(dev);
1518 struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr);
1520 return sprintf(buf, "%d\n", data->temp_offset[sattr->index] * 1000);
1524 store_temp_offset(struct device *dev, struct device_attribute *attr,
1525 const char *buf, size_t count)
1527 struct nct6775_data *data = dev_get_drvdata(dev);
1528 struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr);
1529 int nr = sattr->index;
1533 err = kstrtol(buf, 10, &val);
1537 val = SENSORS_LIMIT(DIV_ROUND_CLOSEST(val, 1000), -128, 127);
1539 mutex_lock(&data->update_lock);
1540 data->temp_offset[nr] = val;
1541 nct6775_write_value(data, data->REG_TEMP_OFFSET[nr], val);
1542 mutex_unlock(&data->update_lock);
1548 show_temp_type(struct device *dev, struct device_attribute *attr, char *buf)
1550 struct nct6775_data *data = nct6775_update_device(dev);
1551 struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr);
1552 int nr = sattr->index;
1553 return sprintf(buf, "%d\n", (int)data->temp_type[nr]);
1557 store_temp_type(struct device *dev, struct device_attribute *attr,
1558 const char *buf, size_t count)
1560 struct nct6775_data *data = nct6775_update_device(dev);
1561 struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr);
1562 int nr = sattr->index;
1565 u8 vbat, diode, bit;
1567 err = kstrtoul(buf, 10, &val);
1571 if (val != 1 && val != 3 && val != 4)
1574 mutex_lock(&data->update_lock);
1576 data->temp_type[nr] = val;
1577 vbat = nct6775_read_value(data, data->REG_VBAT) & ~(0x02 << nr);
1578 diode = nct6775_read_value(data, data->REG_DIODE) & ~(0x02 << nr);
1581 case 1: /* CPU diode (diode, current mode) */
1585 case 3: /* diode, voltage mode */
1588 case 4: /* thermistor */
1591 nct6775_write_value(data, data->REG_VBAT, vbat);
1592 nct6775_write_value(data, data->REG_DIODE, diode);
1594 mutex_unlock(&data->update_lock);
1598 static struct sensor_device_attribute_2 sda_temp_input[] = {
1599 SENSOR_ATTR_2(temp1_input, S_IRUGO, show_temp, NULL, 0, 0),
1600 SENSOR_ATTR_2(temp2_input, S_IRUGO, show_temp, NULL, 1, 0),
1601 SENSOR_ATTR_2(temp3_input, S_IRUGO, show_temp, NULL, 2, 0),
1602 SENSOR_ATTR_2(temp4_input, S_IRUGO, show_temp, NULL, 3, 0),
1603 SENSOR_ATTR_2(temp5_input, S_IRUGO, show_temp, NULL, 4, 0),
1604 SENSOR_ATTR_2(temp6_input, S_IRUGO, show_temp, NULL, 5, 0),
1605 SENSOR_ATTR_2(temp7_input, S_IRUGO, show_temp, NULL, 6, 0),
1606 SENSOR_ATTR_2(temp8_input, S_IRUGO, show_temp, NULL, 7, 0),
1607 SENSOR_ATTR_2(temp9_input, S_IRUGO, show_temp, NULL, 8, 0),
1608 SENSOR_ATTR_2(temp10_input, S_IRUGO, show_temp, NULL, 9, 0),
1611 static struct sensor_device_attribute sda_temp_label[] = {
1612 SENSOR_ATTR(temp1_label, S_IRUGO, show_temp_label, NULL, 0),
1613 SENSOR_ATTR(temp2_label, S_IRUGO, show_temp_label, NULL, 1),
1614 SENSOR_ATTR(temp3_label, S_IRUGO, show_temp_label, NULL, 2),
1615 SENSOR_ATTR(temp4_label, S_IRUGO, show_temp_label, NULL, 3),
1616 SENSOR_ATTR(temp5_label, S_IRUGO, show_temp_label, NULL, 4),
1617 SENSOR_ATTR(temp6_label, S_IRUGO, show_temp_label, NULL, 5),
1618 SENSOR_ATTR(temp7_label, S_IRUGO, show_temp_label, NULL, 6),
1619 SENSOR_ATTR(temp8_label, S_IRUGO, show_temp_label, NULL, 7),
1620 SENSOR_ATTR(temp9_label, S_IRUGO, show_temp_label, NULL, 8),
1621 SENSOR_ATTR(temp10_label, S_IRUGO, show_temp_label, NULL, 9),
1624 static struct sensor_device_attribute_2 sda_temp_max[] = {
1625 SENSOR_ATTR_2(temp1_max, S_IRUGO | S_IWUSR, show_temp, store_temp,
1627 SENSOR_ATTR_2(temp2_max, S_IRUGO | S_IWUSR, show_temp, store_temp,
1629 SENSOR_ATTR_2(temp3_max, S_IRUGO | S_IWUSR, show_temp, store_temp,
1631 SENSOR_ATTR_2(temp4_max, S_IRUGO | S_IWUSR, show_temp, store_temp,
1633 SENSOR_ATTR_2(temp5_max, S_IRUGO | S_IWUSR, show_temp, store_temp,
1635 SENSOR_ATTR_2(temp6_max, S_IRUGO | S_IWUSR, show_temp, store_temp,
1637 SENSOR_ATTR_2(temp7_max, S_IRUGO | S_IWUSR, show_temp, store_temp,
1639 SENSOR_ATTR_2(temp8_max, S_IRUGO | S_IWUSR, show_temp, store_temp,
1641 SENSOR_ATTR_2(temp9_max, S_IRUGO | S_IWUSR, show_temp, store_temp,
1643 SENSOR_ATTR_2(temp10_max, S_IRUGO | S_IWUSR, show_temp, store_temp,
1647 static struct sensor_device_attribute_2 sda_temp_max_hyst[] = {
1648 SENSOR_ATTR_2(temp1_max_hyst, S_IRUGO | S_IWUSR, show_temp, store_temp,
1650 SENSOR_ATTR_2(temp2_max_hyst, S_IRUGO | S_IWUSR, show_temp, store_temp,
1652 SENSOR_ATTR_2(temp3_max_hyst, S_IRUGO | S_IWUSR, show_temp, store_temp,
1654 SENSOR_ATTR_2(temp4_max_hyst, S_IRUGO | S_IWUSR, show_temp, store_temp,
1656 SENSOR_ATTR_2(temp5_max_hyst, S_IRUGO | S_IWUSR, show_temp, store_temp,
1658 SENSOR_ATTR_2(temp6_max_hyst, S_IRUGO | S_IWUSR, show_temp, store_temp,
1660 SENSOR_ATTR_2(temp7_max_hyst, S_IRUGO | S_IWUSR, show_temp, store_temp,
1662 SENSOR_ATTR_2(temp8_max_hyst, S_IRUGO | S_IWUSR, show_temp, store_temp,
1664 SENSOR_ATTR_2(temp9_max_hyst, S_IRUGO | S_IWUSR, show_temp, store_temp,
1666 SENSOR_ATTR_2(temp10_max_hyst, S_IRUGO | S_IWUSR, show_temp, store_temp,
1670 static struct sensor_device_attribute_2 sda_temp_crit[] = {
1671 SENSOR_ATTR_2(temp1_crit, S_IRUGO | S_IWUSR, show_temp, store_temp,
1673 SENSOR_ATTR_2(temp2_crit, S_IRUGO | S_IWUSR, show_temp, store_temp,
1675 SENSOR_ATTR_2(temp3_crit, S_IRUGO | S_IWUSR, show_temp, store_temp,
1677 SENSOR_ATTR_2(temp4_crit, S_IRUGO | S_IWUSR, show_temp, store_temp,
1679 SENSOR_ATTR_2(temp5_crit, S_IRUGO | S_IWUSR, show_temp, store_temp,
1681 SENSOR_ATTR_2(temp6_crit, S_IRUGO | S_IWUSR, show_temp, store_temp,
1683 SENSOR_ATTR_2(temp7_crit, S_IRUGO | S_IWUSR, show_temp, store_temp,
1685 SENSOR_ATTR_2(temp8_crit, S_IRUGO | S_IWUSR, show_temp, store_temp,
1687 SENSOR_ATTR_2(temp9_crit, S_IRUGO | S_IWUSR, show_temp, store_temp,
1689 SENSOR_ATTR_2(temp10_crit, S_IRUGO | S_IWUSR, show_temp, store_temp,
1693 static struct sensor_device_attribute sda_temp_offset[] = {
1694 SENSOR_ATTR(temp1_offset, S_IRUGO | S_IWUSR, show_temp_offset,
1695 store_temp_offset, 0),
1696 SENSOR_ATTR(temp2_offset, S_IRUGO | S_IWUSR, show_temp_offset,
1697 store_temp_offset, 1),
1698 SENSOR_ATTR(temp3_offset, S_IRUGO | S_IWUSR, show_temp_offset,
1699 store_temp_offset, 2),
1700 SENSOR_ATTR(temp4_offset, S_IRUGO | S_IWUSR, show_temp_offset,
1701 store_temp_offset, 3),
1702 SENSOR_ATTR(temp5_offset, S_IRUGO | S_IWUSR, show_temp_offset,
1703 store_temp_offset, 4),
1704 SENSOR_ATTR(temp6_offset, S_IRUGO | S_IWUSR, show_temp_offset,
1705 store_temp_offset, 5),
1708 static struct sensor_device_attribute sda_temp_type[] = {
1709 SENSOR_ATTR(temp1_type, S_IRUGO | S_IWUSR, show_temp_type,
1710 store_temp_type, 0),
1711 SENSOR_ATTR(temp2_type, S_IRUGO | S_IWUSR, show_temp_type,
1712 store_temp_type, 1),
1713 SENSOR_ATTR(temp3_type, S_IRUGO | S_IWUSR, show_temp_type,
1714 store_temp_type, 2),
1715 SENSOR_ATTR(temp4_type, S_IRUGO | S_IWUSR, show_temp_type,
1716 store_temp_type, 3),
1717 SENSOR_ATTR(temp5_type, S_IRUGO | S_IWUSR, show_temp_type,
1718 store_temp_type, 4),
1719 SENSOR_ATTR(temp6_type, S_IRUGO | S_IWUSR, show_temp_type,
1720 store_temp_type, 5),
1723 static struct sensor_device_attribute sda_temp_alarm[] = {
1724 SENSOR_ATTR(temp1_alarm, S_IRUGO, show_alarm, NULL,
1726 SENSOR_ATTR(temp2_alarm, S_IRUGO, show_alarm, NULL,
1727 TEMP_ALARM_BASE + 1),
1728 SENSOR_ATTR(temp3_alarm, S_IRUGO, show_alarm, NULL,
1729 TEMP_ALARM_BASE + 2),
1730 SENSOR_ATTR(temp4_alarm, S_IRUGO, show_alarm, NULL,
1731 TEMP_ALARM_BASE + 3),
1732 SENSOR_ATTR(temp5_alarm, S_IRUGO, show_alarm, NULL,
1733 TEMP_ALARM_BASE + 4),
1734 SENSOR_ATTR(temp6_alarm, S_IRUGO, show_alarm, NULL,
1735 TEMP_ALARM_BASE + 5),
1738 #define NUM_TEMP_ALARM ARRAY_SIZE(sda_temp_alarm)
1741 show_pwm_mode(struct device *dev, struct device_attribute *attr, char *buf)
1743 struct nct6775_data *data = nct6775_update_device(dev);
1744 struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr);
1746 return sprintf(buf, "%d\n", !data->pwm_mode[sattr->index]);
1750 store_pwm_mode(struct device *dev, struct device_attribute *attr,
1751 const char *buf, size_t count)
1753 struct nct6775_data *data = dev_get_drvdata(dev);
1754 struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr);
1755 int nr = sattr->index;
1760 err = kstrtoul(buf, 10, &val);
1767 /* Setting DC mode is not supported for all chips/channels */
1768 if (data->REG_PWM_MODE[nr] == 0) {
1774 mutex_lock(&data->update_lock);
1775 data->pwm_mode[nr] = val;
1776 reg = nct6775_read_value(data, data->REG_PWM_MODE[nr]);
1777 reg &= ~data->PWM_MODE_MASK[nr];
1779 reg |= data->PWM_MODE_MASK[nr];
1780 nct6775_write_value(data, data->REG_PWM_MODE[nr], reg);
1781 mutex_unlock(&data->update_lock);
1786 show_pwm_enable(struct device *dev, struct device_attribute *attr, char *buf)
1788 struct nct6775_data *data = nct6775_update_device(dev);
1789 struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr);
1791 return sprintf(buf, "%d\n", data->pwm_enable[sattr->index]);
1795 show_pwm(struct device *dev, struct device_attribute *attr, char *buf)
1797 struct nct6775_data *data = nct6775_update_device(dev);
1798 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
1800 int index = sattr->index;
1804 * For automatic fan control modes, show current pwm readings.
1805 * Otherwise, show the configured value.
1807 if (index == 0 && data->pwm_enable[nr] > 1)
1808 pwm = nct6775_read_value(data, data->REG_PWM_READ[nr]);
1810 pwm = data->pwm[index][nr];
1812 return sprintf(buf, "%d\n", pwm);
1816 store_pwm(struct device *dev, struct device_attribute *attr, const char *buf,
1819 struct nct6775_data *data = dev_get_drvdata(dev);
1820 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
1822 int index = sattr->index;
1824 int minval[7] = { 0, 1, 1, data->pwm[2][nr], 0, 0, 0 };
1826 = { 255, 255, data->pwm[3][nr] ? : 255, 255, 255, 255, 255 };
1829 err = kstrtoul(buf, 10, &val);
1832 val = SENSORS_LIMIT(val, minval[index], maxval[index]);
1834 mutex_lock(&data->update_lock);
1835 data->pwm[index][nr] = val;
1836 nct6775_write_value(data, data->REG_PWM[index][nr], val);
1837 mutex_unlock(&data->update_lock);
1841 /* Returns 0 if OK, -EINVAL otherwise */
1842 static int check_trip_points(struct nct6775_data *data, int nr)
1846 for (i = 0; i < data->auto_pwm_num - 1; i++) {
1847 if (data->auto_temp[nr][i] > data->auto_temp[nr][i + 1])
1850 for (i = 0; i < data->auto_pwm_num - 1; i++) {
1851 if (data->auto_pwm[nr][i] > data->auto_pwm[nr][i + 1])
1854 /* validate critical temperature and pwm if enabled (pwm > 0) */
1855 if (data->auto_pwm[nr][data->auto_pwm_num]) {
1856 if (data->auto_temp[nr][data->auto_pwm_num - 1] >
1857 data->auto_temp[nr][data->auto_pwm_num] ||
1858 data->auto_pwm[nr][data->auto_pwm_num - 1] >
1859 data->auto_pwm[nr][data->auto_pwm_num])
1866 store_pwm_enable(struct device *dev, struct device_attribute *attr,
1867 const char *buf, size_t count)
1869 struct nct6775_data *data = dev_get_drvdata(dev);
1870 struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr);
1871 int nr = sattr->index;
1876 err = kstrtoul(buf, 10, &val);
1883 if (val == 4 && data->kind != nct6775)
1886 if (val == 5 && check_trip_points(data, nr)) {
1887 dev_err(dev, "Inconsistent trip points, not switching to SmartFan IV mode\n");
1888 dev_err(dev, "Adjust trip points and try again\n");
1892 mutex_lock(&data->update_lock);
1893 data->pwm_enable[nr] = val;
1896 * turn off pwm control: select manual mode, set pwm to maximum
1898 data->pwm[0][nr] = 255;
1899 nct6775_write_value(data, data->REG_PWM[0][nr], 255);
1901 reg = nct6775_read_value(data, data->REG_FAN_MODE[nr]);
1903 reg |= pwm_enable_to_reg(val) << 4;
1904 nct6775_write_value(data, data->REG_FAN_MODE[nr], reg);
1905 mutex_unlock(&data->update_lock);
1910 show_pwm_temp_sel(struct device *dev, struct device_attribute *attr, char *buf)
1912 struct nct6775_data *data = nct6775_update_device(dev);
1913 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
1914 int i, src, sel = 0;
1916 src = data->pwm_temp_sel[sattr->index][sattr->nr];
1918 for (i = 0; i < NUM_TEMP; i++) {
1919 if (!(data->have_temp & (1 << i)))
1921 if (src == data->temp_src[i]) {
1927 return sprintf(buf, "%d\n", sel);
1931 store_pwm_temp_sel(struct device *dev, struct device_attribute *attr,
1932 const char *buf, size_t count)
1934 struct nct6775_data *data = nct6775_update_device(dev);
1935 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
1937 int index = sattr->index;
1941 err = kstrtoul(buf, 10, &val);
1944 if (val == 0 || val > NUM_TEMP)
1946 if (!(data->have_temp & (1 << (val - 1))) || !data->temp_src[val - 1])
1949 mutex_lock(&data->update_lock);
1950 src = data->temp_src[val - 1];
1951 data->pwm_temp_sel[index][nr] = src;
1952 reg = nct6775_read_value(data, data->REG_TEMP_SEL[index][nr]);
1955 nct6775_write_value(data, data->REG_TEMP_SEL[index][nr], reg);
1956 mutex_unlock(&data->update_lock);
1962 show_target_temp(struct device *dev, struct device_attribute *attr, char *buf)
1964 struct nct6775_data *data = nct6775_update_device(dev);
1965 struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr);
1967 return sprintf(buf, "%d\n",
1968 data->target_temp[sattr->index] * 1000);
1972 store_target_temp(struct device *dev, struct device_attribute *attr,
1973 const char *buf, size_t count)
1975 struct nct6775_data *data = dev_get_drvdata(dev);
1976 struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr);
1977 int nr = sattr->index;
1981 err = kstrtol(buf, 10, &val);
1985 val = SENSORS_LIMIT(DIV_ROUND_CLOSEST(val, 1000), 0, 127);
1987 mutex_lock(&data->update_lock);
1988 data->target_temp[nr] = val;
1989 nct6775_write_value(data, data->REG_TARGET[nr], val);
1990 mutex_unlock(&data->update_lock);
1995 show_auto_temp_hyst(struct device *dev, struct device_attribute *attr,
1998 struct nct6775_data *data = nct6775_update_device(dev);
1999 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
2001 int point = sattr->index >= data->auto_pwm_num ? 1 : 0;
2002 int tolerance = data->tolerance[nr][point];
2003 int temp = data->auto_temp[nr][sattr->index];
2005 return sprintf(buf, "%d\n", (temp - tolerance) * 1000);
2009 store_auto_temp_hyst(struct device *dev, struct device_attribute *attr,
2010 const char *buf, size_t count)
2012 struct nct6775_data *data = dev_get_drvdata(dev);
2013 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
2015 int point = sattr->index >= data->auto_pwm_num ? 1 : 0;
2019 int maxlimit[2][3] = { { 15, 7, 63 }, { 15, 7, 7 } };
2020 int mask[] = { 0x0f, 0x07, 0x07 };
2023 err = kstrtol(buf, 10, &val);
2027 temp = data->auto_temp[nr][sattr->index];
2028 val = temp - DIV_ROUND_CLOSEST(val, 1000);
2030 /* Limit tolerance as needed */
2031 val = SENSORS_LIMIT(val, 0, maxlimit[point][data->kind]);
2033 mutex_lock(&data->update_lock);
2035 nct6775_write_value(data,
2036 data->REG_CRITICAL_TEMP_TOLERANCE[nr],
2039 reg = nct6775_read_value(data, data->REG_FAN_MODE[nr]);
2040 reg = (reg & ~mask[nr]) | (val & mask[nr]);
2041 nct6775_write_value(data, data->REG_FAN_MODE[nr], reg);
2042 if (data->kind == nct6779) {
2043 reg = nct6775_read_value(data,
2044 NCT6779_REG_TOLERANCE_H[nr]);
2045 reg = (reg & 0x70) | ((val & 0x38) << 1);
2046 nct6775_write_value(data,
2047 NCT6779_REG_TOLERANCE_H[nr], reg);
2051 data->tolerance[nr][point] = val;
2052 mutex_unlock(&data->update_lock);
2056 static SENSOR_DEVICE_ATTR_2(pwm1, S_IWUSR | S_IRUGO, show_pwm, store_pwm, 0, 0);
2057 static SENSOR_DEVICE_ATTR_2(pwm2, S_IWUSR | S_IRUGO, show_pwm, store_pwm, 1, 0);
2058 static SENSOR_DEVICE_ATTR_2(pwm3, S_IWUSR | S_IRUGO, show_pwm, store_pwm, 2, 0);
2059 static SENSOR_DEVICE_ATTR_2(pwm4, S_IWUSR | S_IRUGO, show_pwm, store_pwm, 3, 0);
2060 static SENSOR_DEVICE_ATTR_2(pwm5, S_IWUSR | S_IRUGO, show_pwm, store_pwm, 4, 0);
2062 static SENSOR_DEVICE_ATTR(pwm1_mode, S_IWUSR | S_IRUGO, show_pwm_mode,
2064 static SENSOR_DEVICE_ATTR(pwm2_mode, S_IWUSR | S_IRUGO, show_pwm_mode,
2066 static SENSOR_DEVICE_ATTR(pwm3_mode, S_IWUSR | S_IRUGO, show_pwm_mode,
2068 static SENSOR_DEVICE_ATTR(pwm4_mode, S_IWUSR | S_IRUGO, show_pwm_mode,
2070 static SENSOR_DEVICE_ATTR(pwm5_mode, S_IWUSR | S_IRUGO, show_pwm_mode,
2073 static SENSOR_DEVICE_ATTR(pwm1_enable, S_IWUSR | S_IRUGO, show_pwm_enable,
2074 store_pwm_enable, 0);
2075 static SENSOR_DEVICE_ATTR(pwm2_enable, S_IWUSR | S_IRUGO, show_pwm_enable,
2076 store_pwm_enable, 1);
2077 static SENSOR_DEVICE_ATTR(pwm3_enable, S_IWUSR | S_IRUGO, show_pwm_enable,
2078 store_pwm_enable, 2);
2079 static SENSOR_DEVICE_ATTR(pwm4_enable, S_IWUSR | S_IRUGO, show_pwm_enable,
2080 store_pwm_enable, 3);
2081 static SENSOR_DEVICE_ATTR(pwm5_enable, S_IWUSR | S_IRUGO, show_pwm_enable,
2082 store_pwm_enable, 4);
2084 static SENSOR_DEVICE_ATTR_2(pwm1_temp_sel, S_IWUSR | S_IRUGO,
2085 show_pwm_temp_sel, store_pwm_temp_sel, 0, 0);
2086 static SENSOR_DEVICE_ATTR_2(pwm2_temp_sel, S_IWUSR | S_IRUGO,
2087 show_pwm_temp_sel, store_pwm_temp_sel, 1, 0);
2088 static SENSOR_DEVICE_ATTR_2(pwm3_temp_sel, S_IWUSR | S_IRUGO,
2089 show_pwm_temp_sel, store_pwm_temp_sel, 2, 0);
2090 static SENSOR_DEVICE_ATTR_2(pwm4_temp_sel, S_IWUSR | S_IRUGO,
2091 show_pwm_temp_sel, store_pwm_temp_sel, 3, 0);
2092 static SENSOR_DEVICE_ATTR_2(pwm5_temp_sel, S_IWUSR | S_IRUGO,
2093 show_pwm_temp_sel, store_pwm_temp_sel, 4, 0);
2095 static SENSOR_DEVICE_ATTR(pwm1_target, S_IWUSR | S_IRUGO, show_target_temp,
2096 store_target_temp, 0);
2097 static SENSOR_DEVICE_ATTR(pwm2_target, S_IWUSR | S_IRUGO, show_target_temp,
2098 store_target_temp, 1);
2099 static SENSOR_DEVICE_ATTR(pwm3_target, S_IWUSR | S_IRUGO, show_target_temp,
2100 store_target_temp, 2);
2101 static SENSOR_DEVICE_ATTR(pwm4_target, S_IWUSR | S_IRUGO, show_target_temp,
2102 store_target_temp, 3);
2103 static SENSOR_DEVICE_ATTR(pwm5_target, S_IWUSR | S_IRUGO, show_target_temp,
2104 store_target_temp, 4);
2106 /* Smart Fan registers */
2109 show_pwm_sel_enable(struct device *dev, struct device_attribute *attr,
2112 struct nct6775_data *data = nct6775_update_device(dev);
2113 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
2115 return sprintf(buf, "%d\n",
2116 data->pwm_sel_enable[sattr->index][sattr->nr]);
2120 store_pwm_sel_enable(struct device *dev, struct device_attribute *attr,
2121 const char *buf, size_t count)
2123 struct nct6775_data *data = dev_get_drvdata(dev);
2124 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
2126 int index = sattr->index;
2130 static const u8 bit[] = { 0x80, 0x80, 0x01 };
2133 err = kstrtoul(buf, 10, &val);
2140 mutex_lock(&data->update_lock);
2141 data->pwm_sel_enable[index][nr] = val;
2142 reg = nct6775_read_value(data, data->REG_TEMP_SEL[index][nr]);
2146 nct6775_write_value(data, data->REG_TEMP_SEL[index][nr], reg);
2147 mutex_unlock(&data->update_lock);
2152 show_weight_temp(struct device *dev, struct device_attribute *attr, char *buf)
2154 struct nct6775_data *data = nct6775_update_device(dev);
2155 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
2157 int index = sattr->index;
2159 return sprintf(buf, "%d\n", data->weight_temp[index][nr] * 1000);
2163 store_weight_temp(struct device *dev, struct device_attribute *attr,
2164 const char *buf, size_t count)
2166 struct nct6775_data *data = dev_get_drvdata(dev);
2167 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
2169 int index = sattr->index;
2173 err = kstrtoul(buf, 10, &val);
2177 val = SENSORS_LIMIT(DIV_ROUND_CLOSEST(val, 1000), 0, 255);
2179 mutex_lock(&data->update_lock);
2180 data->weight_temp[index][nr] = val;
2181 nct6775_write_value(data, data->REG_WEIGHT_TEMP[index][nr], val);
2182 mutex_unlock(&data->update_lock);
2186 static SENSOR_DEVICE_ATTR_2(pwm1_stop_output_enable, S_IWUSR | S_IRUGO,
2187 show_pwm_sel_enable, store_pwm_sel_enable, 0, 0);
2188 static SENSOR_DEVICE_ATTR_2(pwm2_stop_output_enable, S_IWUSR | S_IRUGO,
2189 show_pwm_sel_enable, store_pwm_sel_enable, 1, 0);
2190 static SENSOR_DEVICE_ATTR_2(pwm3_stop_output_enable, S_IWUSR | S_IRUGO,
2191 show_pwm_sel_enable, store_pwm_sel_enable, 2, 0);
2192 static SENSOR_DEVICE_ATTR_2(pwm4_stop_output_enable, S_IWUSR | S_IRUGO,
2193 show_pwm_sel_enable, store_pwm_sel_enable, 3, 0);
2194 static SENSOR_DEVICE_ATTR_2(pwm5_stop_output_enable, S_IWUSR | S_IRUGO,
2195 show_pwm_sel_enable, store_pwm_sel_enable, 4, 0);
2197 static SENSOR_DEVICE_ATTR_2(pwm1_weight_enable, S_IWUSR | S_IRUGO,
2198 show_pwm_sel_enable, store_pwm_sel_enable, 0, 1);
2199 static SENSOR_DEVICE_ATTR_2(pwm2_weight_enable, S_IWUSR | S_IRUGO,
2200 show_pwm_sel_enable, store_pwm_sel_enable, 1, 1);
2201 static SENSOR_DEVICE_ATTR_2(pwm3_weight_enable, S_IWUSR | S_IRUGO,
2202 show_pwm_sel_enable, store_pwm_sel_enable, 2, 1);
2203 static SENSOR_DEVICE_ATTR_2(pwm4_weight_enable, S_IWUSR | S_IRUGO,
2204 show_pwm_sel_enable, store_pwm_sel_enable, 3, 1);
2205 static SENSOR_DEVICE_ATTR_2(pwm5_weight_enable, S_IWUSR | S_IRUGO,
2206 show_pwm_sel_enable, store_pwm_sel_enable, 4, 1);
2208 static SENSOR_DEVICE_ATTR_2(pwm1_step_enable, S_IWUSR | S_IRUGO,
2209 show_pwm_sel_enable, store_pwm_sel_enable, 0, 2);
2210 static SENSOR_DEVICE_ATTR_2(pwm2_step_enable, S_IWUSR | S_IRUGO,
2211 show_pwm_sel_enable, store_pwm_sel_enable, 1, 2);
2212 static SENSOR_DEVICE_ATTR_2(pwm3_step_enable, S_IWUSR | S_IRUGO,
2213 show_pwm_sel_enable, store_pwm_sel_enable, 2, 2);
2214 static SENSOR_DEVICE_ATTR_2(pwm4_step_enable, S_IWUSR | S_IRUGO,
2215 show_pwm_sel_enable, store_pwm_sel_enable, 3, 2);
2216 static SENSOR_DEVICE_ATTR_2(pwm5_step_enable, S_IWUSR | S_IRUGO,
2217 show_pwm_sel_enable, store_pwm_sel_enable, 4, 2);
2219 static SENSOR_DEVICE_ATTR_2(pwm1_weight_temp_sel, S_IWUSR | S_IRUGO,
2220 show_pwm_temp_sel, store_pwm_temp_sel, 0, 1);
2221 static SENSOR_DEVICE_ATTR_2(pwm2_weight_temp_sel, S_IWUSR | S_IRUGO,
2222 show_pwm_temp_sel, store_pwm_temp_sel, 1, 1);
2223 static SENSOR_DEVICE_ATTR_2(pwm3_weight_temp_sel, S_IWUSR | S_IRUGO,
2224 show_pwm_temp_sel, store_pwm_temp_sel, 2, 1);
2225 static SENSOR_DEVICE_ATTR_2(pwm4_weight_temp_sel, S_IWUSR | S_IRUGO,
2226 show_pwm_temp_sel, store_pwm_temp_sel, 3, 1);
2227 static SENSOR_DEVICE_ATTR_2(pwm5_weight_temp_sel, S_IWUSR | S_IRUGO,
2228 show_pwm_temp_sel, store_pwm_temp_sel, 4, 1);
2230 static SENSOR_DEVICE_ATTR_2(pwm1_weight_temp_step, S_IWUSR | S_IRUGO,
2231 show_weight_temp, store_weight_temp, 0, 0);
2232 static SENSOR_DEVICE_ATTR_2(pwm2_weight_temp_step, S_IWUSR | S_IRUGO,
2233 show_weight_temp, store_weight_temp, 1, 0);
2234 static SENSOR_DEVICE_ATTR_2(pwm3_weight_temp_step, S_IWUSR | S_IRUGO,
2235 show_weight_temp, store_weight_temp, 2, 0);
2236 static SENSOR_DEVICE_ATTR_2(pwm4_weight_temp_step, S_IWUSR | S_IRUGO,
2237 show_weight_temp, store_weight_temp, 3, 0);
2238 static SENSOR_DEVICE_ATTR_2(pwm5_weight_temp_step, S_IWUSR | S_IRUGO,
2239 show_weight_temp, store_weight_temp, 4, 0);
2241 static SENSOR_DEVICE_ATTR_2(pwm1_weight_temp_step_tol, S_IWUSR | S_IRUGO,
2242 show_weight_temp, store_weight_temp, 0, 1);
2243 static SENSOR_DEVICE_ATTR_2(pwm2_weight_temp_step_tol, S_IWUSR | S_IRUGO,
2244 show_weight_temp, store_weight_temp, 1, 1);
2245 static SENSOR_DEVICE_ATTR_2(pwm3_weight_temp_step_tol, S_IWUSR | S_IRUGO,
2246 show_weight_temp, store_weight_temp, 2, 1);
2247 static SENSOR_DEVICE_ATTR_2(pwm4_weight_temp_step_tol, S_IWUSR | S_IRUGO,
2248 show_weight_temp, store_weight_temp, 3, 1);
2249 static SENSOR_DEVICE_ATTR_2(pwm5_weight_temp_step_tol, S_IWUSR | S_IRUGO,
2250 show_weight_temp, store_weight_temp, 4, 1);
2252 static SENSOR_DEVICE_ATTR_2(pwm1_weight_temp_step_base, S_IWUSR | S_IRUGO,
2253 show_weight_temp, store_weight_temp, 0, 2);
2254 static SENSOR_DEVICE_ATTR_2(pwm2_weight_temp_step_base, S_IWUSR | S_IRUGO,
2255 show_weight_temp, store_weight_temp, 1, 2);
2256 static SENSOR_DEVICE_ATTR_2(pwm3_weight_temp_step_base, S_IWUSR | S_IRUGO,
2257 show_weight_temp, store_weight_temp, 2, 2);
2258 static SENSOR_DEVICE_ATTR_2(pwm4_weight_temp_step_base, S_IWUSR | S_IRUGO,
2259 show_weight_temp, store_weight_temp, 3, 2);
2260 static SENSOR_DEVICE_ATTR_2(pwm5_weight_temp_step_base, S_IWUSR | S_IRUGO,
2261 show_weight_temp, store_weight_temp, 4, 2);
2263 static SENSOR_DEVICE_ATTR_2(pwm1_weight_duty_step, S_IWUSR | S_IRUGO,
2264 show_pwm, store_pwm, 0, 5);
2265 static SENSOR_DEVICE_ATTR_2(pwm2_weight_duty_step, S_IWUSR | S_IRUGO,
2266 show_pwm, store_pwm, 1, 5);
2267 static SENSOR_DEVICE_ATTR_2(pwm3_weight_duty_step, S_IWUSR | S_IRUGO,
2268 show_pwm, store_pwm, 2, 5);
2269 static SENSOR_DEVICE_ATTR_2(pwm4_weight_duty_step, S_IWUSR | S_IRUGO,
2270 show_pwm, store_pwm, 3, 5);
2271 static SENSOR_DEVICE_ATTR_2(pwm5_weight_duty_step, S_IWUSR | S_IRUGO,
2272 show_pwm, store_pwm, 4, 5);
2274 /* duty_base is not supported on all chips */
2275 static struct sensor_device_attribute_2 sda_weight_duty_base[] = {
2276 SENSOR_ATTR_2(pwm1_weight_duty_base, S_IWUSR | S_IRUGO,
2277 show_pwm, store_pwm, 0, 6),
2278 SENSOR_ATTR_2(pwm2_weight_duty_base, S_IWUSR | S_IRUGO,
2279 show_pwm, store_pwm, 1, 6),
2280 SENSOR_ATTR_2(pwm3_weight_duty_base, S_IWUSR | S_IRUGO,
2281 show_pwm, store_pwm, 2, 6),
2282 SENSOR_ATTR_2(pwm4_weight_duty_base, S_IWUSR | S_IRUGO,
2283 show_pwm, store_pwm, 3, 6),
2284 SENSOR_ATTR_2(pwm5_weight_duty_base, S_IWUSR | S_IRUGO,
2285 show_pwm, store_pwm, 4, 6),
2289 show_fan_time(struct device *dev, struct device_attribute *attr, char *buf)
2291 struct nct6775_data *data = nct6775_update_device(dev);
2292 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
2294 int index = sattr->index;
2296 return sprintf(buf, "%d\n",
2297 step_time_from_reg(data->fan_time[index][nr],
2298 data->pwm_mode[nr]));
2302 store_fan_time(struct device *dev, struct device_attribute *attr,
2303 const char *buf, size_t count)
2305 struct nct6775_data *data = dev_get_drvdata(dev);
2306 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
2308 int index = sattr->index;
2312 err = kstrtoul(buf, 10, &val);
2316 val = step_time_to_reg(val, data->pwm_mode[nr]);
2317 mutex_lock(&data->update_lock);
2318 data->fan_time[index][nr] = val;
2319 nct6775_write_value(data, data->REG_FAN_TIME[index][nr], val);
2320 mutex_unlock(&data->update_lock);
2325 show_name(struct device *dev, struct device_attribute *attr, char *buf)
2327 struct nct6775_data *data = dev_get_drvdata(dev);
2329 return sprintf(buf, "%s\n", data->name);
2332 static DEVICE_ATTR(name, S_IRUGO, show_name, NULL);
2334 static SENSOR_DEVICE_ATTR_2(pwm1_stop_time, S_IWUSR | S_IRUGO, show_fan_time,
2335 store_fan_time, 0, 0);
2336 static SENSOR_DEVICE_ATTR_2(pwm2_stop_time, S_IWUSR | S_IRUGO, show_fan_time,
2337 store_fan_time, 1, 0);
2338 static SENSOR_DEVICE_ATTR_2(pwm3_stop_time, S_IWUSR | S_IRUGO, show_fan_time,
2339 store_fan_time, 2, 0);
2340 static SENSOR_DEVICE_ATTR_2(pwm4_stop_time, S_IWUSR | S_IRUGO, show_fan_time,
2341 store_fan_time, 3, 0);
2342 static SENSOR_DEVICE_ATTR_2(pwm5_stop_time, S_IWUSR | S_IRUGO, show_fan_time,
2343 store_fan_time, 4, 0);
2345 static SENSOR_DEVICE_ATTR_2(pwm1_step_up_time, S_IWUSR | S_IRUGO, show_fan_time,
2346 store_fan_time, 0, 1);
2347 static SENSOR_DEVICE_ATTR_2(pwm2_step_up_time, S_IWUSR | S_IRUGO, show_fan_time,
2348 store_fan_time, 1, 1);
2349 static SENSOR_DEVICE_ATTR_2(pwm3_step_up_time, S_IWUSR | S_IRUGO, show_fan_time,
2350 store_fan_time, 2, 1);
2351 static SENSOR_DEVICE_ATTR_2(pwm4_step_up_time, S_IWUSR | S_IRUGO, show_fan_time,
2352 store_fan_time, 3, 1);
2353 static SENSOR_DEVICE_ATTR_2(pwm5_step_up_time, S_IWUSR | S_IRUGO, show_fan_time,
2354 store_fan_time, 4, 1);
2356 static SENSOR_DEVICE_ATTR_2(pwm1_step_down_time, S_IWUSR | S_IRUGO,
2357 show_fan_time, store_fan_time, 0, 2);
2358 static SENSOR_DEVICE_ATTR_2(pwm2_step_down_time, S_IWUSR | S_IRUGO,
2359 show_fan_time, store_fan_time, 1, 2);
2360 static SENSOR_DEVICE_ATTR_2(pwm3_step_down_time, S_IWUSR | S_IRUGO,
2361 show_fan_time, store_fan_time, 2, 2);
2362 static SENSOR_DEVICE_ATTR_2(pwm4_step_down_time, S_IWUSR | S_IRUGO,
2363 show_fan_time, store_fan_time, 3, 2);
2364 static SENSOR_DEVICE_ATTR_2(pwm5_step_down_time, S_IWUSR | S_IRUGO,
2365 show_fan_time, store_fan_time, 4, 2);
2367 static SENSOR_DEVICE_ATTR_2(pwm1_start_output, S_IWUSR | S_IRUGO, show_pwm,
2369 static SENSOR_DEVICE_ATTR_2(pwm2_start_output, S_IWUSR | S_IRUGO, show_pwm,
2371 static SENSOR_DEVICE_ATTR_2(pwm3_start_output, S_IWUSR | S_IRUGO, show_pwm,
2373 static SENSOR_DEVICE_ATTR_2(pwm4_start_output, S_IWUSR | S_IRUGO, show_pwm,
2375 static SENSOR_DEVICE_ATTR_2(pwm5_start_output, S_IWUSR | S_IRUGO, show_pwm,
2378 static SENSOR_DEVICE_ATTR_2(pwm1_stop_output, S_IWUSR | S_IRUGO, show_pwm,
2380 static SENSOR_DEVICE_ATTR_2(pwm2_stop_output, S_IWUSR | S_IRUGO, show_pwm,
2382 static SENSOR_DEVICE_ATTR_2(pwm3_stop_output, S_IWUSR | S_IRUGO, show_pwm,
2384 static SENSOR_DEVICE_ATTR_2(pwm4_stop_output, S_IWUSR | S_IRUGO, show_pwm,
2386 static SENSOR_DEVICE_ATTR_2(pwm5_stop_output, S_IWUSR | S_IRUGO, show_pwm,
2389 /* max_output is not supported on all chips */
2390 static struct sensor_device_attribute_2 sda_max_output[] = {
2391 SENSOR_ATTR_2(pwm1_max_output, S_IWUSR | S_IRUGO, show_pwm, store_pwm,
2393 SENSOR_ATTR_2(pwm2_max_output, S_IWUSR | S_IRUGO, show_pwm, store_pwm,
2395 SENSOR_ATTR_2(pwm3_max_output, S_IWUSR | S_IRUGO, show_pwm, store_pwm,
2397 SENSOR_ATTR_2(pwm4_max_output, S_IWUSR | S_IRUGO, show_pwm, store_pwm,
2399 SENSOR_ATTR_2(pwm5_max_output, S_IWUSR | S_IRUGO, show_pwm, store_pwm,
2403 /* step_output is not supported on all chips */
2404 static struct sensor_device_attribute_2 sda_step_output[] = {
2405 SENSOR_ATTR_2(pwm1_step_output, S_IWUSR | S_IRUGO, show_pwm, store_pwm,
2407 SENSOR_ATTR_2(pwm2_step_output, S_IWUSR | S_IRUGO, show_pwm, store_pwm,
2409 SENSOR_ATTR_2(pwm3_step_output, S_IWUSR | S_IRUGO, show_pwm, store_pwm,
2411 SENSOR_ATTR_2(pwm4_step_output, S_IWUSR | S_IRUGO, show_pwm, store_pwm,
2413 SENSOR_ATTR_2(pwm5_step_output, S_IWUSR | S_IRUGO, show_pwm, store_pwm,
2417 static struct attribute *nct6775_attributes_pwm[5][20] = {
2419 &sensor_dev_attr_pwm1.dev_attr.attr,
2420 &sensor_dev_attr_pwm1_mode.dev_attr.attr,
2421 &sensor_dev_attr_pwm1_enable.dev_attr.attr,
2422 &sensor_dev_attr_pwm1_stop_output_enable.dev_attr.attr,
2423 &sensor_dev_attr_pwm1_weight_enable.dev_attr.attr,
2424 &sensor_dev_attr_pwm1_step_enable.dev_attr.attr,
2425 &sensor_dev_attr_pwm1_temp_sel.dev_attr.attr,
2426 &sensor_dev_attr_pwm1_target.dev_attr.attr,
2427 &sensor_dev_attr_pwm1_stop_time.dev_attr.attr,
2428 &sensor_dev_attr_pwm1_step_up_time.dev_attr.attr,
2429 &sensor_dev_attr_pwm1_step_down_time.dev_attr.attr,
2430 &sensor_dev_attr_pwm1_start_output.dev_attr.attr,
2431 &sensor_dev_attr_pwm1_stop_output.dev_attr.attr,
2432 &sensor_dev_attr_pwm1_weight_temp_sel.dev_attr.attr,
2433 &sensor_dev_attr_pwm1_weight_temp_step.dev_attr.attr,
2434 &sensor_dev_attr_pwm1_weight_temp_step_tol.dev_attr.attr,
2435 &sensor_dev_attr_pwm1_weight_temp_step_base.dev_attr.attr,
2436 &sensor_dev_attr_pwm1_weight_duty_step.dev_attr.attr,
2440 &sensor_dev_attr_pwm2.dev_attr.attr,
2441 &sensor_dev_attr_pwm2_mode.dev_attr.attr,
2442 &sensor_dev_attr_pwm2_enable.dev_attr.attr,
2443 &sensor_dev_attr_pwm2_stop_output_enable.dev_attr.attr,
2444 &sensor_dev_attr_pwm2_weight_enable.dev_attr.attr,
2445 &sensor_dev_attr_pwm2_step_enable.dev_attr.attr,
2446 &sensor_dev_attr_pwm2_temp_sel.dev_attr.attr,
2447 &sensor_dev_attr_pwm2_target.dev_attr.attr,
2448 &sensor_dev_attr_pwm2_stop_time.dev_attr.attr,
2449 &sensor_dev_attr_pwm2_step_up_time.dev_attr.attr,
2450 &sensor_dev_attr_pwm2_step_down_time.dev_attr.attr,
2451 &sensor_dev_attr_pwm2_start_output.dev_attr.attr,
2452 &sensor_dev_attr_pwm2_stop_output.dev_attr.attr,
2453 &sensor_dev_attr_pwm2_weight_temp_sel.dev_attr.attr,
2454 &sensor_dev_attr_pwm2_weight_temp_step.dev_attr.attr,
2455 &sensor_dev_attr_pwm2_weight_temp_step_tol.dev_attr.attr,
2456 &sensor_dev_attr_pwm2_weight_temp_step_base.dev_attr.attr,
2457 &sensor_dev_attr_pwm2_weight_duty_step.dev_attr.attr,
2461 &sensor_dev_attr_pwm3.dev_attr.attr,
2462 &sensor_dev_attr_pwm3_mode.dev_attr.attr,
2463 &sensor_dev_attr_pwm3_enable.dev_attr.attr,
2464 &sensor_dev_attr_pwm3_stop_output_enable.dev_attr.attr,
2465 &sensor_dev_attr_pwm3_weight_enable.dev_attr.attr,
2466 &sensor_dev_attr_pwm3_step_enable.dev_attr.attr,
2467 &sensor_dev_attr_pwm3_temp_sel.dev_attr.attr,
2468 &sensor_dev_attr_pwm3_target.dev_attr.attr,
2469 &sensor_dev_attr_pwm3_stop_time.dev_attr.attr,
2470 &sensor_dev_attr_pwm3_step_up_time.dev_attr.attr,
2471 &sensor_dev_attr_pwm3_step_down_time.dev_attr.attr,
2472 &sensor_dev_attr_pwm3_start_output.dev_attr.attr,
2473 &sensor_dev_attr_pwm3_stop_output.dev_attr.attr,
2474 &sensor_dev_attr_pwm3_weight_temp_sel.dev_attr.attr,
2475 &sensor_dev_attr_pwm3_weight_temp_step.dev_attr.attr,
2476 &sensor_dev_attr_pwm3_weight_temp_step_tol.dev_attr.attr,
2477 &sensor_dev_attr_pwm3_weight_temp_step_base.dev_attr.attr,
2478 &sensor_dev_attr_pwm3_weight_duty_step.dev_attr.attr,
2482 &sensor_dev_attr_pwm4.dev_attr.attr,
2483 &sensor_dev_attr_pwm4_mode.dev_attr.attr,
2484 &sensor_dev_attr_pwm4_enable.dev_attr.attr,
2485 &sensor_dev_attr_pwm4_stop_output_enable.dev_attr.attr,
2486 &sensor_dev_attr_pwm4_weight_enable.dev_attr.attr,
2487 &sensor_dev_attr_pwm4_step_enable.dev_attr.attr,
2488 &sensor_dev_attr_pwm4_temp_sel.dev_attr.attr,
2489 &sensor_dev_attr_pwm4_target.dev_attr.attr,
2490 &sensor_dev_attr_pwm4_stop_time.dev_attr.attr,
2491 &sensor_dev_attr_pwm4_step_up_time.dev_attr.attr,
2492 &sensor_dev_attr_pwm4_step_down_time.dev_attr.attr,
2493 &sensor_dev_attr_pwm4_start_output.dev_attr.attr,
2494 &sensor_dev_attr_pwm4_stop_output.dev_attr.attr,
2495 &sensor_dev_attr_pwm4_weight_temp_sel.dev_attr.attr,
2496 &sensor_dev_attr_pwm4_weight_temp_step.dev_attr.attr,
2497 &sensor_dev_attr_pwm4_weight_temp_step_tol.dev_attr.attr,
2498 &sensor_dev_attr_pwm4_weight_temp_step_base.dev_attr.attr,
2499 &sensor_dev_attr_pwm4_weight_duty_step.dev_attr.attr,
2503 &sensor_dev_attr_pwm5.dev_attr.attr,
2504 &sensor_dev_attr_pwm5_mode.dev_attr.attr,
2505 &sensor_dev_attr_pwm5_enable.dev_attr.attr,
2506 &sensor_dev_attr_pwm5_stop_output_enable.dev_attr.attr,
2507 &sensor_dev_attr_pwm5_weight_enable.dev_attr.attr,
2508 &sensor_dev_attr_pwm5_step_enable.dev_attr.attr,
2509 &sensor_dev_attr_pwm5_temp_sel.dev_attr.attr,
2510 &sensor_dev_attr_pwm5_target.dev_attr.attr,
2511 &sensor_dev_attr_pwm5_stop_time.dev_attr.attr,
2512 &sensor_dev_attr_pwm5_step_up_time.dev_attr.attr,
2513 &sensor_dev_attr_pwm5_step_down_time.dev_attr.attr,
2514 &sensor_dev_attr_pwm5_start_output.dev_attr.attr,
2515 &sensor_dev_attr_pwm5_stop_output.dev_attr.attr,
2516 &sensor_dev_attr_pwm5_weight_temp_sel.dev_attr.attr,
2517 &sensor_dev_attr_pwm5_weight_temp_step.dev_attr.attr,
2518 &sensor_dev_attr_pwm5_weight_temp_step_tol.dev_attr.attr,
2519 &sensor_dev_attr_pwm5_weight_temp_step_base.dev_attr.attr,
2520 &sensor_dev_attr_pwm5_weight_duty_step.dev_attr.attr,
2525 static const struct attribute_group nct6775_group_pwm[5] = {
2526 { .attrs = nct6775_attributes_pwm[0] },
2527 { .attrs = nct6775_attributes_pwm[1] },
2528 { .attrs = nct6775_attributes_pwm[2] },
2529 { .attrs = nct6775_attributes_pwm[3] },
2530 { .attrs = nct6775_attributes_pwm[4] },
2534 show_auto_pwm(struct device *dev, struct device_attribute *attr, char *buf)
2536 struct nct6775_data *data = nct6775_update_device(dev);
2537 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
2539 return sprintf(buf, "%d\n", data->auto_pwm[sattr->nr][sattr->index]);
2543 store_auto_pwm(struct device *dev, struct device_attribute *attr,
2544 const char *buf, size_t count)
2546 struct nct6775_data *data = dev_get_drvdata(dev);
2547 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
2549 int point = sattr->index;
2554 err = kstrtoul(buf, 10, &val);
2560 if (point == data->auto_pwm_num) {
2561 if (data->kind != nct6775 && !val)
2563 if (data->kind != nct6779 && val)
2567 mutex_lock(&data->update_lock);
2568 data->auto_pwm[nr][point] = val;
2569 if (point < data->auto_pwm_num) {
2570 nct6775_write_value(data,
2571 NCT6775_AUTO_PWM(data, nr, point),
2572 data->auto_pwm[nr][point]);
2574 switch (data->kind) {
2576 /* disable if needed (pwm == 0) */
2577 reg = nct6775_read_value(data,
2578 NCT6775_REG_CRITICAL_ENAB[nr]);
2583 nct6775_write_value(data, NCT6775_REG_CRITICAL_ENAB[nr],
2587 break; /* always enabled, nothing to do */
2589 nct6775_write_value(data, NCT6779_REG_CRITICAL_PWM[nr],
2591 reg = nct6775_read_value(data,
2592 NCT6779_REG_CRITICAL_PWM_ENABLE[nr]);
2597 nct6775_write_value(data,
2598 NCT6779_REG_CRITICAL_PWM_ENABLE[nr],
2603 mutex_unlock(&data->update_lock);
2608 show_auto_temp(struct device *dev, struct device_attribute *attr, char *buf)
2610 struct nct6775_data *data = nct6775_update_device(dev);
2611 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
2613 int point = sattr->index;
2616 * We don't know for sure if the temperature is signed or unsigned.
2617 * Assume it is unsigned.
2619 return sprintf(buf, "%d\n", data->auto_temp[nr][point] * 1000);
2623 store_auto_temp(struct device *dev, struct device_attribute *attr,
2624 const char *buf, size_t count)
2626 struct nct6775_data *data = dev_get_drvdata(dev);
2627 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
2629 int point = sattr->index;
2633 err = kstrtoul(buf, 10, &val);
2639 mutex_lock(&data->update_lock);
2640 data->auto_temp[nr][point] = DIV_ROUND_CLOSEST(val, 1000);
2641 if (point < data->auto_pwm_num) {
2642 nct6775_write_value(data,
2643 NCT6775_AUTO_TEMP(data, nr, point),
2644 data->auto_temp[nr][point]);
2646 nct6775_write_value(data, data->REG_CRITICAL_TEMP[nr],
2647 data->auto_temp[nr][point]);
2649 mutex_unlock(&data->update_lock);
2654 * The number of auto-point trip points is chip dependent.
2655 * Need to check support while generating/removing attribute files.
2657 static struct sensor_device_attribute_2 sda_auto_pwm_arrays[] = {
2658 SENSOR_ATTR_2(pwm1_auto_point1_pwm, S_IWUSR | S_IRUGO,
2659 show_auto_pwm, store_auto_pwm, 0, 0),
2660 SENSOR_ATTR_2(pwm1_auto_point1_temp, S_IWUSR | S_IRUGO,
2661 show_auto_temp, store_auto_temp, 0, 0),
2662 SENSOR_ATTR_2(pwm1_auto_point1_temp_hyst, S_IWUSR | S_IRUGO,
2663 show_auto_temp_hyst, store_auto_temp_hyst, 0, 0),
2664 SENSOR_ATTR_2(pwm1_auto_point2_pwm, S_IWUSR | S_IRUGO,
2665 show_auto_pwm, store_auto_pwm, 0, 1),
2666 SENSOR_ATTR_2(pwm1_auto_point2_temp, S_IWUSR | S_IRUGO,
2667 show_auto_temp, store_auto_temp, 0, 1),
2668 SENSOR_ATTR_2(pwm1_auto_point2_temp_hyst, S_IWUSR | S_IRUGO,
2669 show_auto_temp_hyst, store_auto_temp_hyst, 0, 1),
2670 SENSOR_ATTR_2(pwm1_auto_point3_pwm, S_IWUSR | S_IRUGO,
2671 show_auto_pwm, store_auto_pwm, 0, 2),
2672 SENSOR_ATTR_2(pwm1_auto_point3_temp, S_IWUSR | S_IRUGO,
2673 show_auto_temp, store_auto_temp, 0, 2),
2674 SENSOR_ATTR_2(pwm1_auto_point3_temp_hyst, S_IWUSR | S_IRUGO,
2675 show_auto_temp_hyst, store_auto_temp_hyst, 0, 2),
2676 SENSOR_ATTR_2(pwm1_auto_point4_pwm, S_IWUSR | S_IRUGO,
2677 show_auto_pwm, store_auto_pwm, 0, 3),
2678 SENSOR_ATTR_2(pwm1_auto_point4_temp, S_IWUSR | S_IRUGO,
2679 show_auto_temp, store_auto_temp, 0, 3),
2680 SENSOR_ATTR_2(pwm1_auto_point4_temp_hyst, S_IWUSR | S_IRUGO,
2681 show_auto_temp_hyst, store_auto_temp_hyst, 0, 3),
2682 SENSOR_ATTR_2(pwm1_auto_point5_pwm, S_IWUSR | S_IRUGO,
2683 show_auto_pwm, store_auto_pwm, 0, 4),
2684 SENSOR_ATTR_2(pwm1_auto_point5_temp, S_IWUSR | S_IRUGO,
2685 show_auto_temp, store_auto_temp, 0, 4),
2686 SENSOR_ATTR_2(pwm1_auto_point5_temp_hyst, S_IWUSR | S_IRUGO,
2687 show_auto_temp_hyst, store_auto_temp_hyst, 0, 4),
2688 SENSOR_ATTR_2(pwm1_auto_point6_pwm, S_IWUSR | S_IRUGO,
2689 show_auto_pwm, store_auto_pwm, 0, 5),
2690 SENSOR_ATTR_2(pwm1_auto_point6_temp, S_IWUSR | S_IRUGO,
2691 show_auto_temp, store_auto_temp, 0, 5),
2692 SENSOR_ATTR_2(pwm1_auto_point6_temp_hyst, S_IWUSR | S_IRUGO,
2693 show_auto_temp_hyst, store_auto_temp_hyst, 0, 5),
2694 SENSOR_ATTR_2(pwm1_auto_point7_pwm, S_IWUSR | S_IRUGO,
2695 show_auto_pwm, store_auto_pwm, 0, 6),
2696 SENSOR_ATTR_2(pwm1_auto_point7_temp, S_IWUSR | S_IRUGO,
2697 show_auto_temp, store_auto_temp, 0, 6),
2698 SENSOR_ATTR_2(pwm1_auto_point7_temp_hyst, S_IWUSR | S_IRUGO,
2699 show_auto_temp_hyst, store_auto_temp_hyst, 0, 6),
2701 SENSOR_ATTR_2(pwm2_auto_point1_pwm, S_IWUSR | S_IRUGO,
2702 show_auto_pwm, store_auto_pwm, 1, 0),
2703 SENSOR_ATTR_2(pwm2_auto_point1_temp, S_IWUSR | S_IRUGO,
2704 show_auto_temp, store_auto_temp, 1, 0),
2705 SENSOR_ATTR_2(pwm2_auto_point1_temp_hyst, S_IWUSR | S_IRUGO,
2706 show_auto_temp_hyst, store_auto_temp_hyst, 1, 0),
2707 SENSOR_ATTR_2(pwm2_auto_point2_pwm, S_IWUSR | S_IRUGO,
2708 show_auto_pwm, store_auto_pwm, 1, 1),
2709 SENSOR_ATTR_2(pwm2_auto_point2_temp, S_IWUSR | S_IRUGO,
2710 show_auto_temp, store_auto_temp, 1, 1),
2711 SENSOR_ATTR_2(pwm2_auto_point2_temp_hyst, S_IWUSR | S_IRUGO,
2712 show_auto_temp_hyst, store_auto_temp_hyst, 1, 1),
2713 SENSOR_ATTR_2(pwm2_auto_point3_pwm, S_IWUSR | S_IRUGO,
2714 show_auto_pwm, store_auto_pwm, 1, 2),
2715 SENSOR_ATTR_2(pwm2_auto_point3_temp, S_IWUSR | S_IRUGO,
2716 show_auto_temp, store_auto_temp, 1, 2),
2717 SENSOR_ATTR_2(pwm2_auto_point3_temp_hyst, S_IWUSR | S_IRUGO,
2718 show_auto_temp_hyst, store_auto_temp_hyst, 1, 2),
2719 SENSOR_ATTR_2(pwm2_auto_point4_pwm, S_IWUSR | S_IRUGO,
2720 show_auto_pwm, store_auto_pwm, 1, 3),
2721 SENSOR_ATTR_2(pwm2_auto_point4_temp, S_IWUSR | S_IRUGO,
2722 show_auto_temp, store_auto_temp, 1, 3),
2723 SENSOR_ATTR_2(pwm2_auto_point4_temp_hyst, S_IWUSR | S_IRUGO,
2724 show_auto_temp_hyst, store_auto_temp_hyst, 1, 3),
2725 SENSOR_ATTR_2(pwm2_auto_point5_pwm, S_IWUSR | S_IRUGO,
2726 show_auto_pwm, store_auto_pwm, 1, 4),
2727 SENSOR_ATTR_2(pwm2_auto_point5_temp, S_IWUSR | S_IRUGO,
2728 show_auto_temp, store_auto_temp, 1, 4),
2729 SENSOR_ATTR_2(pwm2_auto_point5_temp_hyst, S_IWUSR | S_IRUGO,
2730 show_auto_temp_hyst, store_auto_temp_hyst, 1, 4),
2731 SENSOR_ATTR_2(pwm2_auto_point6_pwm, S_IWUSR | S_IRUGO,
2732 show_auto_pwm, store_auto_pwm, 1, 5),
2733 SENSOR_ATTR_2(pwm2_auto_point6_temp, S_IWUSR | S_IRUGO,
2734 show_auto_temp, store_auto_temp, 1, 5),
2735 SENSOR_ATTR_2(pwm2_auto_point6_temp_hyst, S_IWUSR | S_IRUGO,
2736 show_auto_temp_hyst, store_auto_temp_hyst, 1, 5),
2737 SENSOR_ATTR_2(pwm2_auto_point7_pwm, S_IWUSR | S_IRUGO,
2738 show_auto_pwm, store_auto_pwm, 1, 6),
2739 SENSOR_ATTR_2(pwm2_auto_point7_temp, S_IWUSR | S_IRUGO,
2740 show_auto_temp, store_auto_temp, 1, 6),
2741 SENSOR_ATTR_2(pwm2_auto_point7_temp_hyst, S_IWUSR | S_IRUGO,
2742 show_auto_temp_hyst, store_auto_temp_hyst, 1, 6),
2744 SENSOR_ATTR_2(pwm3_auto_point1_pwm, S_IWUSR | S_IRUGO,
2745 show_auto_pwm, store_auto_pwm, 2, 0),
2746 SENSOR_ATTR_2(pwm3_auto_point1_temp, S_IWUSR | S_IRUGO,
2747 show_auto_temp, store_auto_temp, 2, 0),
2748 SENSOR_ATTR_2(pwm3_auto_point1_temp_hyst, S_IWUSR | S_IRUGO,
2749 show_auto_temp_hyst, store_auto_temp_hyst, 2, 0),
2750 SENSOR_ATTR_2(pwm3_auto_point2_pwm, S_IWUSR | S_IRUGO,
2751 show_auto_pwm, store_auto_pwm, 2, 1),
2752 SENSOR_ATTR_2(pwm3_auto_point2_temp, S_IWUSR | S_IRUGO,
2753 show_auto_temp, store_auto_temp, 2, 1),
2754 SENSOR_ATTR_2(pwm3_auto_point2_temp_hyst, S_IWUSR | S_IRUGO,
2755 show_auto_temp_hyst, store_auto_temp_hyst, 2, 1),
2756 SENSOR_ATTR_2(pwm3_auto_point3_pwm, S_IWUSR | S_IRUGO,
2757 show_auto_pwm, store_auto_pwm, 2, 2),
2758 SENSOR_ATTR_2(pwm3_auto_point3_temp, S_IWUSR | S_IRUGO,
2759 show_auto_temp, store_auto_temp, 2, 2),
2760 SENSOR_ATTR_2(pwm3_auto_point3_temp_hyst, S_IWUSR | S_IRUGO,
2761 show_auto_temp_hyst, store_auto_temp_hyst, 2, 2),
2762 SENSOR_ATTR_2(pwm3_auto_point4_pwm, S_IWUSR | S_IRUGO,
2763 show_auto_pwm, store_auto_pwm, 2, 3),
2764 SENSOR_ATTR_2(pwm3_auto_point4_temp, S_IWUSR | S_IRUGO,
2765 show_auto_temp, store_auto_temp, 2, 3),
2766 SENSOR_ATTR_2(pwm3_auto_point4_temp_hyst, S_IWUSR | S_IRUGO,
2767 show_auto_temp_hyst, store_auto_temp_hyst, 2, 3),
2768 SENSOR_ATTR_2(pwm3_auto_point5_pwm, S_IWUSR | S_IRUGO,
2769 show_auto_pwm, store_auto_pwm, 2, 4),
2770 SENSOR_ATTR_2(pwm3_auto_point5_temp, S_IWUSR | S_IRUGO,
2771 show_auto_temp, store_auto_temp, 2, 4),
2772 SENSOR_ATTR_2(pwm3_auto_point5_temp_hyst, S_IWUSR | S_IRUGO,
2773 show_auto_temp_hyst, store_auto_temp_hyst, 2, 4),
2774 SENSOR_ATTR_2(pwm3_auto_point6_pwm, S_IWUSR | S_IRUGO,
2775 show_auto_pwm, store_auto_pwm, 2, 5),
2776 SENSOR_ATTR_2(pwm3_auto_point6_temp, S_IWUSR | S_IRUGO,
2777 show_auto_temp, store_auto_temp, 2, 5),
2778 SENSOR_ATTR_2(pwm3_auto_point6_temp_hyst, S_IWUSR | S_IRUGO,
2779 show_auto_temp_hyst, store_auto_temp_hyst, 2, 5),
2780 SENSOR_ATTR_2(pwm3_auto_point7_pwm, S_IWUSR | S_IRUGO,
2781 show_auto_pwm, store_auto_pwm, 2, 6),
2782 SENSOR_ATTR_2(pwm3_auto_point7_temp, S_IWUSR | S_IRUGO,
2783 show_auto_temp, store_auto_temp, 2, 6),
2784 SENSOR_ATTR_2(pwm3_auto_point7_temp_hyst, S_IWUSR | S_IRUGO,
2785 show_auto_temp_hyst, store_auto_temp_hyst, 2, 6),
2787 SENSOR_ATTR_2(pwm4_auto_point1_pwm, S_IWUSR | S_IRUGO,
2788 show_auto_pwm, store_auto_pwm, 3, 0),
2789 SENSOR_ATTR_2(pwm4_auto_point1_temp, S_IWUSR | S_IRUGO,
2790 show_auto_temp, store_auto_temp, 3, 0),
2791 SENSOR_ATTR_2(pwm4_auto_point1_temp_hyst, S_IWUSR | S_IRUGO,
2792 show_auto_temp_hyst, store_auto_temp_hyst, 3, 0),
2793 SENSOR_ATTR_2(pwm4_auto_point2_pwm, S_IWUSR | S_IRUGO,
2794 show_auto_pwm, store_auto_pwm, 3, 1),
2795 SENSOR_ATTR_2(pwm4_auto_point2_temp, S_IWUSR | S_IRUGO,
2796 show_auto_temp, store_auto_temp, 3, 1),
2797 SENSOR_ATTR_2(pwm4_auto_point2_temp_hyst, S_IWUSR | S_IRUGO,
2798 show_auto_temp_hyst, store_auto_temp_hyst, 3, 1),
2799 SENSOR_ATTR_2(pwm4_auto_point3_pwm, S_IWUSR | S_IRUGO,
2800 show_auto_pwm, store_auto_pwm, 3, 2),
2801 SENSOR_ATTR_2(pwm4_auto_point3_temp, S_IWUSR | S_IRUGO,
2802 show_auto_temp, store_auto_temp, 3, 2),
2803 SENSOR_ATTR_2(pwm4_auto_point3_temp_hyst, S_IWUSR | S_IRUGO,
2804 show_auto_temp_hyst, store_auto_temp_hyst, 3, 2),
2805 SENSOR_ATTR_2(pwm4_auto_point4_pwm, S_IWUSR | S_IRUGO,
2806 show_auto_pwm, store_auto_pwm, 3, 3),
2807 SENSOR_ATTR_2(pwm4_auto_point4_temp, S_IWUSR | S_IRUGO,
2808 show_auto_temp, store_auto_temp, 3, 3),
2809 SENSOR_ATTR_2(pwm4_auto_point4_temp_hyst, S_IWUSR | S_IRUGO,
2810 show_auto_temp_hyst, store_auto_temp_hyst, 3, 3),
2811 SENSOR_ATTR_2(pwm4_auto_point5_pwm, S_IWUSR | S_IRUGO,
2812 show_auto_pwm, store_auto_pwm, 3, 4),
2813 SENSOR_ATTR_2(pwm4_auto_point5_temp, S_IWUSR | S_IRUGO,
2814 show_auto_temp, store_auto_temp, 3, 4),
2815 SENSOR_ATTR_2(pwm4_auto_point5_temp_hyst, S_IWUSR | S_IRUGO,
2816 show_auto_temp_hyst, store_auto_temp_hyst, 3, 4),
2817 SENSOR_ATTR_2(pwm4_auto_point6_pwm, S_IWUSR | S_IRUGO,
2818 show_auto_pwm, store_auto_pwm, 3, 5),
2819 SENSOR_ATTR_2(pwm4_auto_point6_temp, S_IWUSR | S_IRUGO,
2820 show_auto_temp, store_auto_temp, 3, 5),
2821 SENSOR_ATTR_2(pwm4_auto_point6_temp_hyst, S_IWUSR | S_IRUGO,
2822 show_auto_temp_hyst, store_auto_temp_hyst, 3, 5),
2823 SENSOR_ATTR_2(pwm4_auto_point7_pwm, S_IWUSR | S_IRUGO,
2824 show_auto_pwm, store_auto_pwm, 3, 6),
2825 SENSOR_ATTR_2(pwm4_auto_point7_temp, S_IWUSR | S_IRUGO,
2826 show_auto_temp, store_auto_temp, 3, 6),
2827 SENSOR_ATTR_2(pwm4_auto_point7_temp_hyst, S_IWUSR | S_IRUGO,
2828 show_auto_temp_hyst, store_auto_temp_hyst, 3, 6),
2830 SENSOR_ATTR_2(pwm5_auto_point1_pwm, S_IWUSR | S_IRUGO,
2831 show_auto_pwm, store_auto_pwm, 4, 0),
2832 SENSOR_ATTR_2(pwm5_auto_point1_temp, S_IWUSR | S_IRUGO,
2833 show_auto_temp, store_auto_temp, 4, 0),
2834 SENSOR_ATTR_2(pwm5_auto_point1_temp_hyst, S_IWUSR | S_IRUGO,
2835 show_auto_temp_hyst, store_auto_temp_hyst, 4, 0),
2836 SENSOR_ATTR_2(pwm5_auto_point2_pwm, S_IWUSR | S_IRUGO,
2837 show_auto_pwm, store_auto_pwm, 4, 1),
2838 SENSOR_ATTR_2(pwm5_auto_point2_temp, S_IWUSR | S_IRUGO,
2839 show_auto_temp, store_auto_temp, 4, 1),
2840 SENSOR_ATTR_2(pwm5_auto_point2_temp_hyst, S_IWUSR | S_IRUGO,
2841 show_auto_temp_hyst, store_auto_temp_hyst, 4, 1),
2842 SENSOR_ATTR_2(pwm5_auto_point3_pwm, S_IWUSR | S_IRUGO,
2843 show_auto_pwm, store_auto_pwm, 4, 2),
2844 SENSOR_ATTR_2(pwm5_auto_point3_temp, S_IWUSR | S_IRUGO,
2845 show_auto_temp, store_auto_temp, 4, 2),
2846 SENSOR_ATTR_2(pwm5_auto_point3_temp_hyst, S_IWUSR | S_IRUGO,
2847 show_auto_temp_hyst, store_auto_temp_hyst, 4, 2),
2848 SENSOR_ATTR_2(pwm5_auto_point4_pwm, S_IWUSR | S_IRUGO,
2849 show_auto_pwm, store_auto_pwm, 4, 3),
2850 SENSOR_ATTR_2(pwm5_auto_point4_temp, S_IWUSR | S_IRUGO,
2851 show_auto_temp, store_auto_temp, 4, 3),
2852 SENSOR_ATTR_2(pwm5_auto_point4_temp_hyst, S_IWUSR | S_IRUGO,
2853 show_auto_temp_hyst, store_auto_temp_hyst, 4, 3),
2854 SENSOR_ATTR_2(pwm5_auto_point5_pwm, S_IWUSR | S_IRUGO,
2855 show_auto_pwm, store_auto_pwm, 4, 4),
2856 SENSOR_ATTR_2(pwm5_auto_point5_temp, S_IWUSR | S_IRUGO,
2857 show_auto_temp, store_auto_temp, 4, 4),
2858 SENSOR_ATTR_2(pwm5_auto_point5_temp_hyst, S_IWUSR | S_IRUGO,
2859 show_auto_temp_hyst, store_auto_temp_hyst, 4, 4),
2860 SENSOR_ATTR_2(pwm5_auto_point6_pwm, S_IWUSR | S_IRUGO,
2861 show_auto_pwm, store_auto_pwm, 4, 5),
2862 SENSOR_ATTR_2(pwm5_auto_point6_temp, S_IWUSR | S_IRUGO,
2863 show_auto_temp, store_auto_temp, 4, 5),
2864 SENSOR_ATTR_2(pwm5_auto_point6_temp_hyst, S_IWUSR | S_IRUGO,
2865 show_auto_temp_hyst, store_auto_temp_hyst, 4, 5),
2866 SENSOR_ATTR_2(pwm5_auto_point7_pwm, S_IWUSR | S_IRUGO,
2867 show_auto_pwm, store_auto_pwm, 4, 6),
2868 SENSOR_ATTR_2(pwm5_auto_point7_temp, S_IWUSR | S_IRUGO,
2869 show_auto_temp, store_auto_temp, 4, 6),
2870 SENSOR_ATTR_2(pwm5_auto_point7_temp_hyst, S_IWUSR | S_IRUGO,
2871 show_auto_temp_hyst, store_auto_temp_hyst, 4, 6),
2875 show_vid(struct device *dev, struct device_attribute *attr, char *buf)
2877 struct nct6775_data *data = dev_get_drvdata(dev);
2878 return sprintf(buf, "%d\n", vid_from_reg(data->vid, data->vrm));
2881 static DEVICE_ATTR(cpu0_vid, S_IRUGO, show_vid, NULL);
2883 /* Case open detection */
2886 clear_caseopen(struct device *dev, struct device_attribute *attr,
2887 const char *buf, size_t count)
2889 struct nct6775_data *data = dev_get_drvdata(dev);
2890 struct nct6775_sio_data *sio_data = dev->platform_data;
2891 int nr = to_sensor_dev_attr(attr)->index - INTRUSION_ALARM_BASE;
2896 if (kstrtoul(buf, 10, &val) || val != 0)
2899 mutex_lock(&data->update_lock);
2902 * Use CR registers to clear caseopen status.
2903 * The CR registers are the same for all chips, and not all chips
2904 * support clearing the caseopen status through "regular" registers.
2906 ret = superio_enter(sio_data->sioreg);
2912 superio_select(sio_data->sioreg, NCT6775_LD_ACPI);
2913 reg = superio_inb(sio_data->sioreg, NCT6775_REG_CR_CASEOPEN_CLR[nr]);
2914 reg |= NCT6775_CR_CASEOPEN_CLR_MASK[nr];
2915 superio_outb(sio_data->sioreg, NCT6775_REG_CR_CASEOPEN_CLR[nr], reg);
2916 reg &= ~NCT6775_CR_CASEOPEN_CLR_MASK[nr];
2917 superio_outb(sio_data->sioreg, NCT6775_REG_CR_CASEOPEN_CLR[nr], reg);
2918 superio_exit(sio_data->sioreg);
2920 data->valid = 0; /* Force cache refresh */
2922 mutex_unlock(&data->update_lock);
2926 static struct sensor_device_attribute sda_caseopen[] = {
2927 SENSOR_ATTR(intrusion0_alarm, S_IWUSR | S_IRUGO, show_alarm,
2928 clear_caseopen, INTRUSION_ALARM_BASE),
2929 SENSOR_ATTR(intrusion1_alarm, S_IWUSR | S_IRUGO, show_alarm,
2930 clear_caseopen, INTRUSION_ALARM_BASE + 1),
2934 * Driver and device management
2937 static void nct6775_device_remove_files(struct device *dev)
2940 * some entries in the following arrays may not have been used in
2941 * device_create_file(), but device_remove_file() will ignore them
2944 struct nct6775_data *data = dev_get_drvdata(dev);
2946 for (i = 0; i < data->pwm_num; i++)
2947 sysfs_remove_group(&dev->kobj, &nct6775_group_pwm[i]);
2949 for (i = 0; i < ARRAY_SIZE(sda_max_output); i++)
2950 device_remove_file(dev, &sda_max_output[i].dev_attr);
2952 for (i = 0; i < ARRAY_SIZE(sda_step_output); i++)
2953 device_remove_file(dev, &sda_max_output[i].dev_attr);
2955 for (i = 0; i < ARRAY_SIZE(sda_weight_duty_base); i++)
2956 device_remove_file(dev, &sda_weight_duty_base[i].dev_attr);
2958 for (i = 0; i < ARRAY_SIZE(sda_auto_pwm_arrays); i++)
2959 device_remove_file(dev, &sda_auto_pwm_arrays[i].dev_attr);
2961 for (i = 0; i < data->in_num; i++)
2962 sysfs_remove_group(&dev->kobj, &nct6775_group_in[i]);
2964 for (i = 0; i < 5; i++) {
2965 device_remove_file(dev, &sda_fan_input[i].dev_attr);
2966 device_remove_file(dev, &sda_fan_alarm[i].dev_attr);
2967 device_remove_file(dev, &sda_fan_div[i].dev_attr);
2968 device_remove_file(dev, &sda_fan_min[i].dev_attr);
2970 for (i = 0; i < NUM_TEMP; i++) {
2971 if (!(data->have_temp & (1 << i)))
2973 device_remove_file(dev, &sda_temp_input[i].dev_attr);
2974 device_remove_file(dev, &sda_temp_label[i].dev_attr);
2975 device_remove_file(dev, &sda_temp_max[i].dev_attr);
2976 device_remove_file(dev, &sda_temp_max_hyst[i].dev_attr);
2977 device_remove_file(dev, &sda_temp_crit[i].dev_attr);
2978 if (!(data->have_temp_fixed & (1 << i)))
2980 device_remove_file(dev, &sda_temp_type[i].dev_attr);
2981 device_remove_file(dev, &sda_temp_offset[i].dev_attr);
2982 if (i >= NUM_TEMP_ALARM)
2984 device_remove_file(dev, &sda_temp_alarm[i].dev_attr);
2987 device_remove_file(dev, &sda_caseopen[0].dev_attr);
2988 device_remove_file(dev, &sda_caseopen[1].dev_attr);
2990 device_remove_file(dev, &dev_attr_name);
2991 device_remove_file(dev, &dev_attr_cpu0_vid);
2994 /* Get the monitoring functions started */
2995 static inline void __devinit nct6775_init_device(struct nct6775_data *data)
3000 /* Start monitoring if needed */
3001 if (data->REG_CONFIG) {
3002 tmp = nct6775_read_value(data, data->REG_CONFIG);
3004 nct6775_write_value(data, data->REG_CONFIG, tmp | 0x01);
3007 /* Enable temperature sensors if needed */
3008 for (i = 0; i < NUM_TEMP; i++) {
3009 if (!(data->have_temp & (1 << i)))
3011 if (!data->reg_temp_config[i])
3013 tmp = nct6775_read_value(data, data->reg_temp_config[i]);
3015 nct6775_write_value(data, data->reg_temp_config[i],
3019 /* Enable VBAT monitoring if needed */
3020 tmp = nct6775_read_value(data, data->REG_VBAT);
3022 nct6775_write_value(data, data->REG_VBAT, tmp | 0x01);
3024 diode = nct6775_read_value(data, data->REG_DIODE);
3026 for (i = 0; i < data->temp_fixed_num; i++) {
3027 if (!(data->have_temp_fixed & (1 << i)))
3029 if ((tmp & (0x02 << i))) /* diode */
3030 data->temp_type[i] = 3 - ((diode >> i) & 0x02);
3031 else /* thermistor */
3032 data->temp_type[i] = 4;
3036 static int __devinit
3037 nct6775_check_fan_inputs(const struct nct6775_sio_data *sio_data,
3038 struct nct6775_data *data)
3041 bool fan3pin, fan3min, fan4pin, fan4min, fan5pin;
3042 bool pwm3pin, pwm4pin, pwm5pin;
3045 ret = superio_enter(sio_data->sioreg);
3049 /* fan4 and fan5 share some pins with the GPIO and serial flash */
3050 if (data->kind == nct6775) {
3051 regval = superio_inb(sio_data->sioreg, 0x2c);
3053 fan3pin = regval & (1 << 6);
3055 pwm3pin = regval & (1 << 7);
3057 /* On NCT6775, fan4 shares pins with the fdc interface */
3058 fan4pin = !(superio_inb(sio_data->sioreg, 0x2A) & 0x80);
3063 } else if (data->kind == nct6776) {
3064 bool gpok = superio_inb(sio_data->sioreg, 0x27) & 0x80;
3066 superio_select(sio_data->sioreg, NCT6775_LD_HWM);
3067 regval = superio_inb(sio_data->sioreg, SIO_REG_ENABLE);
3072 fan3pin = !(superio_inb(sio_data->sioreg, 0x24) & 0x40);
3077 fan4pin = superio_inb(sio_data->sioreg, 0x1C) & 0x01;
3082 fan5pin = superio_inb(sio_data->sioreg, 0x1C) & 0x02;
3089 } else { /* NCT6779D */
3090 regval = superio_inb(sio_data->sioreg, 0x1c);
3092 fan3pin = !(regval & (1 << 5));
3093 fan4pin = !(regval & (1 << 6));
3094 fan5pin = !(regval & (1 << 7));
3096 pwm3pin = !(regval & (1 << 0));
3097 pwm4pin = !(regval & (1 << 1));
3098 pwm5pin = !(regval & (1 << 2));
3104 superio_exit(sio_data->sioreg);
3106 data->has_fan = data->has_fan_min = 0x03; /* fan1 and fan2 */
3107 data->has_fan |= fan3pin << 2;
3108 data->has_fan_min |= fan3min << 2;
3110 data->has_fan |= (fan4pin << 3) | (fan5pin << 4);
3111 data->has_fan_min |= (fan4min << 3) | (fan5pin << 4);
3113 data->has_pwm = 0x03 | (pwm3pin << 2) | (pwm4pin << 3) | (pwm5pin << 4);
3118 static int __devinit nct6775_probe(struct platform_device *pdev)
3120 struct device *dev = &pdev->dev;
3121 struct nct6775_sio_data *sio_data = dev->platform_data;
3122 struct nct6775_data *data;
3123 struct resource *res;
3125 int src, mask, available;
3126 const u16 *reg_temp, *reg_temp_over, *reg_temp_hyst, *reg_temp_config;
3127 const u16 *reg_temp_alternate, *reg_temp_crit;
3130 res = platform_get_resource(pdev, IORESOURCE_IO, 0);
3131 if (!devm_request_region(&pdev->dev, res->start, IOREGION_LENGTH,
3135 data = devm_kzalloc(&pdev->dev, sizeof(struct nct6775_data),
3140 data->kind = sio_data->kind;
3141 data->addr = res->start;
3142 mutex_init(&data->lock);
3143 mutex_init(&data->update_lock);
3144 data->name = nct6775_device_names[data->kind];
3145 platform_set_drvdata(pdev, data);
3147 switch (data->kind) {
3151 data->auto_pwm_num = 6;
3152 data->has_fan_div = true;
3153 data->temp_fixed_num = 3;
3155 data->ALARM_BITS = NCT6775_ALARM_BITS;
3157 data->fan_from_reg = fan_from_reg16;
3158 data->fan_from_reg_min = fan_from_reg8;
3160 data->temp_label = nct6775_temp_label;
3161 data->temp_label_num = ARRAY_SIZE(nct6775_temp_label);
3163 data->REG_CONFIG = NCT6775_REG_CONFIG;
3164 data->REG_VBAT = NCT6775_REG_VBAT;
3165 data->REG_DIODE = NCT6775_REG_DIODE;
3166 data->REG_VIN = NCT6775_REG_IN;
3167 data->REG_IN_MINMAX[0] = NCT6775_REG_IN_MIN;
3168 data->REG_IN_MINMAX[1] = NCT6775_REG_IN_MAX;
3169 data->REG_TARGET = NCT6775_REG_TARGET;
3170 data->REG_FAN = NCT6775_REG_FAN;
3171 data->REG_FAN_MODE = NCT6775_REG_FAN_MODE;
3172 data->REG_FAN_MIN = NCT6775_REG_FAN_MIN;
3173 data->REG_FAN_TIME[0] = NCT6775_REG_FAN_STOP_TIME;
3174 data->REG_FAN_TIME[1] = NCT6775_REG_FAN_STEP_UP_TIME;
3175 data->REG_FAN_TIME[2] = NCT6775_REG_FAN_STEP_DOWN_TIME;
3176 data->REG_PWM[0] = NCT6775_REG_PWM;
3177 data->REG_PWM[1] = NCT6775_REG_FAN_START_OUTPUT;
3178 data->REG_PWM[2] = NCT6775_REG_FAN_STOP_OUTPUT;
3179 data->REG_PWM[3] = NCT6775_REG_FAN_MAX_OUTPUT;
3180 data->REG_PWM[4] = NCT6775_REG_FAN_STEP_OUTPUT;
3181 data->REG_PWM[5] = NCT6775_REG_WEIGHT_DUTY_STEP;
3182 data->REG_PWM_READ = NCT6775_REG_PWM_READ;
3183 data->REG_PWM_MODE = NCT6775_REG_PWM_MODE;
3184 data->PWM_MODE_MASK = NCT6775_PWM_MODE_MASK;
3185 data->REG_AUTO_TEMP = NCT6775_REG_AUTO_TEMP;
3186 data->REG_AUTO_PWM = NCT6775_REG_AUTO_PWM;
3187 data->REG_CRITICAL_TEMP = NCT6775_REG_CRITICAL_TEMP;
3188 data->REG_CRITICAL_TEMP_TOLERANCE
3189 = NCT6775_REG_CRITICAL_TEMP_TOLERANCE;
3190 data->REG_TEMP_OFFSET = NCT6775_REG_TEMP_OFFSET;
3191 data->REG_TEMP_SOURCE = NCT6775_REG_TEMP_SOURCE;
3192 data->REG_TEMP_SEL[0] = NCT6775_REG_TEMP_SEL;
3193 data->REG_TEMP_SEL[1] = NCT6775_REG_WEIGHT_TEMP_SEL;
3194 data->REG_TEMP_SEL[2] = NCT6775_REG_FAN_STEP_ENABLE;
3195 data->REG_WEIGHT_TEMP[0] = NCT6775_REG_WEIGHT_TEMP_STEP;
3196 data->REG_WEIGHT_TEMP[1] = NCT6775_REG_WEIGHT_TEMP_STEP_TOL;
3197 data->REG_WEIGHT_TEMP[2] = NCT6775_REG_WEIGHT_TEMP_BASE;
3198 data->REG_ALARM = NCT6775_REG_ALARM;
3200 reg_temp = NCT6775_REG_TEMP;
3201 num_reg_temp = ARRAY_SIZE(NCT6775_REG_TEMP);
3202 reg_temp_over = NCT6775_REG_TEMP_OVER;
3203 reg_temp_hyst = NCT6775_REG_TEMP_HYST;
3204 reg_temp_config = NCT6775_REG_TEMP_CONFIG;
3205 reg_temp_alternate = NCT6775_REG_TEMP_ALTERNATE;
3206 reg_temp_crit = NCT6775_REG_TEMP_CRIT;
3212 data->auto_pwm_num = 4;
3213 data->has_fan_div = false;
3214 data->temp_fixed_num = 3;
3216 data->ALARM_BITS = NCT6776_ALARM_BITS;
3218 data->fan_from_reg = fan_from_reg13;
3219 data->fan_from_reg_min = fan_from_reg13;
3221 data->temp_label = nct6776_temp_label;
3222 data->temp_label_num = ARRAY_SIZE(nct6776_temp_label);
3224 data->REG_CONFIG = NCT6775_REG_CONFIG;
3225 data->REG_VBAT = NCT6775_REG_VBAT;
3226 data->REG_DIODE = NCT6775_REG_DIODE;
3227 data->REG_VIN = NCT6775_REG_IN;
3228 data->REG_IN_MINMAX[0] = NCT6775_REG_IN_MIN;
3229 data->REG_IN_MINMAX[1] = NCT6775_REG_IN_MAX;
3230 data->REG_TARGET = NCT6775_REG_TARGET;
3231 data->REG_FAN = NCT6775_REG_FAN;
3232 data->REG_FAN_MODE = NCT6775_REG_FAN_MODE;
3233 data->REG_FAN_MIN = NCT6776_REG_FAN_MIN;
3234 data->REG_FAN_TIME[0] = NCT6775_REG_FAN_STOP_TIME;
3235 data->REG_FAN_TIME[1] = NCT6775_REG_FAN_STEP_UP_TIME;
3236 data->REG_FAN_TIME[2] = NCT6775_REG_FAN_STEP_DOWN_TIME;
3237 data->REG_PWM[0] = NCT6775_REG_PWM;
3238 data->REG_PWM[1] = NCT6775_REG_FAN_START_OUTPUT;
3239 data->REG_PWM[2] = NCT6775_REG_FAN_STOP_OUTPUT;
3240 data->REG_PWM[5] = NCT6775_REG_WEIGHT_DUTY_STEP;
3241 data->REG_PWM[6] = NCT6776_REG_WEIGHT_DUTY_BASE;
3242 data->REG_PWM_READ = NCT6775_REG_PWM_READ;
3243 data->REG_PWM_MODE = NCT6776_REG_PWM_MODE;
3244 data->PWM_MODE_MASK = NCT6776_PWM_MODE_MASK;
3245 data->REG_AUTO_TEMP = NCT6775_REG_AUTO_TEMP;
3246 data->REG_AUTO_PWM = NCT6775_REG_AUTO_PWM;
3247 data->REG_CRITICAL_TEMP = NCT6775_REG_CRITICAL_TEMP;
3248 data->REG_CRITICAL_TEMP_TOLERANCE
3249 = NCT6775_REG_CRITICAL_TEMP_TOLERANCE;
3250 data->REG_TEMP_OFFSET = NCT6775_REG_TEMP_OFFSET;
3251 data->REG_TEMP_SOURCE = NCT6775_REG_TEMP_SOURCE;
3252 data->REG_TEMP_SEL[0] = NCT6775_REG_TEMP_SEL;
3253 data->REG_TEMP_SEL[1] = NCT6775_REG_WEIGHT_TEMP_SEL;
3254 data->REG_TEMP_SEL[2] = NCT6775_REG_FAN_STEP_ENABLE;
3255 data->REG_WEIGHT_TEMP[0] = NCT6775_REG_WEIGHT_TEMP_STEP;
3256 data->REG_WEIGHT_TEMP[1] = NCT6775_REG_WEIGHT_TEMP_STEP_TOL;
3257 data->REG_WEIGHT_TEMP[2] = NCT6775_REG_WEIGHT_TEMP_BASE;
3258 data->REG_ALARM = NCT6775_REG_ALARM;
3260 reg_temp = NCT6775_REG_TEMP;
3261 num_reg_temp = ARRAY_SIZE(NCT6775_REG_TEMP);
3262 reg_temp_over = NCT6775_REG_TEMP_OVER;
3263 reg_temp_hyst = NCT6775_REG_TEMP_HYST;
3264 reg_temp_config = NCT6776_REG_TEMP_CONFIG;
3265 reg_temp_alternate = NCT6776_REG_TEMP_ALTERNATE;
3266 reg_temp_crit = NCT6776_REG_TEMP_CRIT;
3272 data->auto_pwm_num = 4;
3273 data->has_fan_div = false;
3274 data->temp_fixed_num = 6;
3276 data->ALARM_BITS = NCT6779_ALARM_BITS;
3278 data->fan_from_reg = fan_from_reg13;
3279 data->fan_from_reg_min = fan_from_reg13;
3281 data->temp_label = nct6779_temp_label;
3282 data->temp_label_num = ARRAY_SIZE(nct6779_temp_label);
3284 data->REG_CONFIG = NCT6775_REG_CONFIG;
3285 data->REG_VBAT = NCT6775_REG_VBAT;
3286 data->REG_DIODE = NCT6775_REG_DIODE;
3287 data->REG_VIN = NCT6779_REG_IN;
3288 data->REG_IN_MINMAX[0] = NCT6775_REG_IN_MIN;
3289 data->REG_IN_MINMAX[1] = NCT6775_REG_IN_MAX;
3290 data->REG_TARGET = NCT6775_REG_TARGET;
3291 data->REG_FAN = NCT6779_REG_FAN;
3292 data->REG_FAN_MODE = NCT6775_REG_FAN_MODE;
3293 data->REG_FAN_MIN = NCT6776_REG_FAN_MIN;
3294 data->REG_FAN_TIME[0] = NCT6775_REG_FAN_STOP_TIME;
3295 data->REG_FAN_TIME[1] = NCT6775_REG_FAN_STEP_UP_TIME;
3296 data->REG_FAN_TIME[2] = NCT6775_REG_FAN_STEP_DOWN_TIME;
3297 data->REG_PWM[0] = NCT6775_REG_PWM;
3298 data->REG_PWM[1] = NCT6775_REG_FAN_START_OUTPUT;
3299 data->REG_PWM[2] = NCT6775_REG_FAN_STOP_OUTPUT;
3300 data->REG_PWM[5] = NCT6775_REG_WEIGHT_DUTY_STEP;
3301 data->REG_PWM[6] = NCT6776_REG_WEIGHT_DUTY_BASE;
3302 data->REG_PWM_READ = NCT6775_REG_PWM_READ;
3303 data->REG_PWM_MODE = NCT6776_REG_PWM_MODE;
3304 data->PWM_MODE_MASK = NCT6776_PWM_MODE_MASK;
3305 data->REG_AUTO_TEMP = NCT6775_REG_AUTO_TEMP;
3306 data->REG_AUTO_PWM = NCT6775_REG_AUTO_PWM;
3307 data->REG_CRITICAL_TEMP = NCT6775_REG_CRITICAL_TEMP;
3308 data->REG_CRITICAL_TEMP_TOLERANCE
3309 = NCT6775_REG_CRITICAL_TEMP_TOLERANCE;
3310 data->REG_TEMP_OFFSET = NCT6779_REG_TEMP_OFFSET;
3311 data->REG_TEMP_SOURCE = NCT6775_REG_TEMP_SOURCE;
3312 data->REG_TEMP_SEL[0] = NCT6775_REG_TEMP_SEL;
3313 data->REG_TEMP_SEL[1] = NCT6775_REG_WEIGHT_TEMP_SEL;
3314 data->REG_TEMP_SEL[2] = NCT6775_REG_FAN_STEP_ENABLE;
3315 data->REG_WEIGHT_TEMP[0] = NCT6775_REG_WEIGHT_TEMP_STEP;
3316 data->REG_WEIGHT_TEMP[1] = NCT6775_REG_WEIGHT_TEMP_STEP_TOL;
3317 data->REG_WEIGHT_TEMP[2] = NCT6775_REG_WEIGHT_TEMP_BASE;
3318 data->REG_ALARM = NCT6779_REG_ALARM;
3320 reg_temp = NCT6779_REG_TEMP;
3321 num_reg_temp = ARRAY_SIZE(NCT6779_REG_TEMP);
3322 reg_temp_over = NCT6779_REG_TEMP_OVER;
3323 reg_temp_hyst = NCT6779_REG_TEMP_HYST;
3324 reg_temp_config = NCT6779_REG_TEMP_CONFIG;
3325 reg_temp_alternate = NCT6779_REG_TEMP_ALTERNATE;
3326 reg_temp_crit = NCT6779_REG_TEMP_CRIT;
3332 data->have_in = (1 << data->in_num) - 1;
3333 data->have_temp = 0;
3336 * On some boards, not all available temperature sources are monitored,
3337 * even though some of the monitoring registers are unused.
3338 * Get list of unused monitoring registers, then detect if any fan
3339 * controls are configured to use unmonitored temperature sources.
3340 * If so, assign the unmonitored temperature sources to available
3341 * monitoring registers.
3345 for (i = 0; i < num_reg_temp; i++) {
3346 if (reg_temp[i] == 0)
3349 src = nct6775_read_value(data, data->REG_TEMP_SOURCE[i]) & 0x1f;
3350 if (!src || (mask & (1 << src)))
3351 available |= 1 << i;
3357 * Now find unmonitored temperature registers and enable monitoring
3358 * if additional monitoring registers are available.
3360 for (i = 0; i < ARRAY_SIZE(data->REG_TEMP_SEL) && available; i++) {
3363 if (!data->REG_TEMP_SEL[i])
3365 for (j = 0; j < data->pwm_num && available; j++) {
3368 if (!data->REG_TEMP_SEL[i][j])
3370 src = nct6775_read_value(data,
3371 data->REG_TEMP_SEL[i][j]);
3373 if (!src || (mask & (1 << src)))
3375 if (src >= data->temp_label_num ||
3376 !strlen(data->temp_label[src])) {
3378 "Select source %d:%d reg 0x%x invalid (%d)\n",
3379 i, j, data->REG_TEMP_SEL[i][j], src);
3383 index = __ffs(available);
3384 nct6775_write_value(data,
3385 data->REG_TEMP_SOURCE[index],
3387 available &= ~(1 << index);
3393 s = NUM_TEMP_FIXED; /* First dynamic temperature attribute */
3394 for (i = 0; i < num_reg_temp; i++) {
3395 if (reg_temp[i] == 0)
3398 src = nct6775_read_value(data, data->REG_TEMP_SOURCE[i]) & 0x1f;
3399 if (!src || (mask & (1 << src)))
3402 if (src >= data->temp_label_num ||
3403 !strlen(data->temp_label[src])) {
3405 "Invalid temperature source %d at index %d, source register 0x%x, temp register 0x%x\n",
3406 src, i, data->REG_TEMP_SOURCE[i], reg_temp[i]);
3412 /* Use fixed index for SYSTIN(1), CPUTIN(2), AUXTIN(3) */
3413 if (src <= data->temp_fixed_num) {
3414 data->have_temp |= 1 << (src - 1);
3415 data->have_temp_fixed |= 1 << (src - 1);
3416 data->reg_temp[0][src - 1] = reg_temp[i];
3417 data->reg_temp[1][src - 1] = reg_temp_over[i];
3418 data->reg_temp[2][src - 1] = reg_temp_hyst[i];
3419 data->reg_temp_config[src - 1] = reg_temp_config[i];
3420 data->temp_src[src - 1] = src;
3427 /* Use dynamic index for other sources */
3428 data->have_temp |= 1 << s;
3429 data->reg_temp[0][s] = reg_temp[i];
3430 data->reg_temp[1][s] = reg_temp_over[i];
3431 data->reg_temp[2][s] = reg_temp_hyst[i];
3432 data->reg_temp_config[s] = reg_temp_config[i];
3433 if (reg_temp_crit[src - 1])
3434 data->reg_temp[3][s] = reg_temp_crit[src - 1];
3436 data->temp_src[s] = src;
3442 * Go through the list of alternate temp registers and enable
3444 * The temperature is already monitored if the respective bit in <mask>
3447 for (i = 0; i < data->temp_label_num - 1; i++) {
3448 if (!reg_temp_alternate[i])
3450 if (mask & (1 << (i + 1)))
3452 if (i < data->temp_fixed_num) {
3453 if (data->have_temp & (1 << i))
3455 data->have_temp |= 1 << i;
3456 data->have_temp_fixed |= 1 << i;
3457 data->reg_temp[0][i] = reg_temp_alternate[i];
3458 data->reg_temp[1][i] = reg_temp_over[i];
3459 data->reg_temp[2][i] = reg_temp_hyst[i];
3460 data->temp_src[i] = i + 1;
3464 if (s >= NUM_TEMP) /* Abort if no more space */
3467 data->have_temp |= 1 << s;
3468 data->reg_temp[0][s] = reg_temp_alternate[i];
3469 data->temp_src[s] = i + 1;
3472 #endif /* TESTING */
3474 switch (data->kind) {
3479 * On NCT6776, AUXTIN and VIN3 pins are shared.
3480 * Only way to detect it is to check if AUXTIN is used
3481 * as a temperature source, and if that source is
3484 * If that is the case, disable in6, which reports VIN3.
3485 * Otherwise disable temp3.
3487 if (data->have_temp & (1 << 2)) {
3488 u8 reg = nct6775_read_value(data,
3489 data->reg_temp_config[2]);
3491 data->have_temp &= ~(1 << 2);
3493 data->have_in &= ~(1 << 6);
3504 * There does not seem to be a clean way to detect if VINx or
3505 * AUXTINx is active, so for keep both sensor types enabled
3511 /* Initialize the chip */
3512 nct6775_init_device(data);
3514 data->vrm = vid_which_vrm();
3515 err = superio_enter(sio_data->sioreg);
3521 * We can get the VID input values directly at logical device D 0xe3.
3523 superio_select(sio_data->sioreg, NCT6775_LD_VID);
3524 data->vid = superio_inb(sio_data->sioreg, 0xe3);
3529 superio_select(sio_data->sioreg, NCT6775_LD_HWM);
3530 tmp = superio_inb(sio_data->sioreg,
3531 NCT6775_REG_CR_FAN_DEBOUNCE);
3532 switch (data->kind) {
3541 superio_outb(sio_data->sioreg, NCT6775_REG_CR_FAN_DEBOUNCE,
3543 dev_info(&pdev->dev, "Enabled fan debounce for chip %s\n",
3547 superio_exit(sio_data->sioreg);
3549 err = device_create_file(dev, &dev_attr_cpu0_vid);
3553 err = nct6775_check_fan_inputs(sio_data, data);
3557 /* Read fan clock dividers immediately */
3558 nct6775_update_fan_div_common(dev, data);
3560 /* Register sysfs hooks */
3561 for (i = 0; i < data->pwm_num; i++) {
3562 if (!(data->has_pwm & (1 << i)))
3565 err = sysfs_create_group(&dev->kobj, &nct6775_group_pwm[i]);
3569 if (data->REG_PWM[3]) {
3570 err = device_create_file(dev,
3571 &sda_max_output[i].dev_attr);
3575 if (data->REG_PWM[4]) {
3576 err = device_create_file(dev,
3577 &sda_step_output[i].dev_attr);
3581 if (data->REG_PWM[6]) {
3582 err = device_create_file(dev,
3583 &sda_weight_duty_base[i].dev_attr);
3588 for (i = 0; i < ARRAY_SIZE(sda_auto_pwm_arrays); i++) {
3589 struct sensor_device_attribute_2 *attr =
3590 &sda_auto_pwm_arrays[i];
3592 if (!(data->has_pwm & (1 << attr->nr)))
3594 if (attr->index > data->auto_pwm_num)
3596 err = device_create_file(dev, &attr->dev_attr);
3601 for (i = 0; i < data->in_num; i++) {
3602 if (!(data->have_in & (1 << i)))
3604 err = sysfs_create_group(&dev->kobj, &nct6775_group_in[i]);
3609 for (i = 0; i < 5; i++) {
3610 if (data->has_fan & (1 << i)) {
3611 err = device_create_file(dev,
3612 &sda_fan_input[i].dev_attr);
3615 err = device_create_file(dev,
3616 &sda_fan_alarm[i].dev_attr);
3619 if (data->kind != nct6776 &&
3620 data->kind != nct6779) {
3621 err = device_create_file(dev,
3622 &sda_fan_div[i].dev_attr);
3626 if (data->has_fan_min & (1 << i)) {
3627 err = device_create_file(dev,
3628 &sda_fan_min[i].dev_attr);
3635 for (i = 0; i < NUM_TEMP; i++) {
3636 if (!(data->have_temp & (1 << i)))
3638 err = device_create_file(dev, &sda_temp_input[i].dev_attr);
3641 if (data->temp_label) {
3642 err = device_create_file(dev,
3643 &sda_temp_label[i].dev_attr);
3647 if (data->reg_temp[1][i]) {
3648 err = device_create_file(dev,
3649 &sda_temp_max[i].dev_attr);
3653 if (data->reg_temp[2][i]) {
3654 err = device_create_file(dev,
3655 &sda_temp_max_hyst[i].dev_attr);
3659 if (data->reg_temp[3][i]) {
3660 err = device_create_file(dev,
3661 &sda_temp_crit[i].dev_attr);
3665 if (!(data->have_temp_fixed & (1 << i)))
3667 err = device_create_file(dev, &sda_temp_type[i].dev_attr);
3670 err = device_create_file(dev, &sda_temp_offset[i].dev_attr);
3673 if (i >= NUM_TEMP_ALARM ||
3674 data->ALARM_BITS[TEMP_ALARM_BASE + i] < 0)
3676 err = device_create_file(dev, &sda_temp_alarm[i].dev_attr);
3681 for (i = 0; i < ARRAY_SIZE(sda_caseopen); i++) {
3682 if (data->ALARM_BITS[INTRUSION_ALARM_BASE + i] < 0)
3684 err = device_create_file(dev, &sda_caseopen[i].dev_attr);
3689 err = device_create_file(dev, &dev_attr_name);
3693 data->hwmon_dev = hwmon_device_register(dev);
3694 if (IS_ERR(data->hwmon_dev)) {
3695 err = PTR_ERR(data->hwmon_dev);
3702 nct6775_device_remove_files(dev);
3706 static int __devexit nct6775_remove(struct platform_device *pdev)
3708 struct nct6775_data *data = platform_get_drvdata(pdev);
3710 hwmon_device_unregister(data->hwmon_dev);
3711 nct6775_device_remove_files(&pdev->dev);
3716 static struct platform_driver nct6775_driver = {
3718 .owner = THIS_MODULE,
3721 .probe = nct6775_probe,
3722 .remove = __devexit_p(nct6775_remove),
3725 /* nct6775_find() looks for a '627 in the Super-I/O config space */
3726 static int __init nct6775_find(int sioaddr, unsigned short *addr,
3727 struct nct6775_sio_data *sio_data)
3729 static const char sio_name_NCT6775[] __initconst = "NCT6775F";
3730 static const char sio_name_NCT6776[] __initconst = "NCT6776F";
3731 static const char sio_name_NCT6779[] __initconst = "NCT6779D";
3734 const char *sio_name;
3737 err = superio_enter(sioaddr);
3744 val = (superio_inb(sioaddr, SIO_REG_DEVID) << 8)
3745 | superio_inb(sioaddr, SIO_REG_DEVID + 1);
3746 switch (val & SIO_ID_MASK) {
3747 case SIO_NCT6775_ID:
3748 sio_data->kind = nct6775;
3749 sio_name = sio_name_NCT6775;
3751 case SIO_NCT6776_ID:
3752 sio_data->kind = nct6776;
3753 sio_name = sio_name_NCT6776;
3755 case SIO_NCT6779_ID:
3756 sio_data->kind = nct6779;
3757 sio_name = sio_name_NCT6779;
3761 pr_debug("unsupported chip ID: 0x%04x\n", val);
3762 superio_exit(sioaddr);
3766 /* We have a known chip, find the HWM I/O address */
3767 superio_select(sioaddr, NCT6775_LD_HWM);
3768 val = (superio_inb(sioaddr, SIO_REG_ADDR) << 8)
3769 | superio_inb(sioaddr, SIO_REG_ADDR + 1);
3770 *addr = val & IOREGION_ALIGNMENT;
3772 pr_err("Refusing to enable a Super-I/O device with a base I/O port 0\n");
3773 superio_exit(sioaddr);
3777 /* Activate logical device if needed */
3778 val = superio_inb(sioaddr, SIO_REG_ENABLE);
3779 if (!(val & 0x01)) {
3780 pr_warn("Forcibly enabling Super-I/O. Sensor is probably unusable.\n");
3781 superio_outb(sioaddr, SIO_REG_ENABLE, val | 0x01);
3784 superio_exit(sioaddr);
3785 pr_info("Found %s chip at %#x\n", sio_name, *addr);
3786 sio_data->sioreg = sioaddr;
3792 * when Super-I/O functions move to a separate file, the Super-I/O
3793 * bus will manage the lifetime of the device and this module will only keep
3794 * track of the nct6775 driver. But since we platform_device_alloc(), we
3795 * must keep track of the device
3797 static struct platform_device *pdev;
3799 static int __init sensors_nct6775_init(void)
3802 unsigned short address;
3803 struct resource res;
3804 struct nct6775_sio_data sio_data;
3807 * initialize sio_data->kind and sio_data->sioreg.
3809 * when Super-I/O functions move to a separate file, the Super-I/O
3810 * driver will probe 0x2e and 0x4e and auto-detect the presence of a
3811 * nct6775 hardware monitor, and call probe()
3813 if (nct6775_find(0x2e, &address, &sio_data) &&
3814 nct6775_find(0x4e, &address, &sio_data))
3817 err = platform_driver_register(&nct6775_driver);
3821 pdev = platform_device_alloc(DRVNAME, address);
3824 pr_err("Device allocation failed\n");
3825 goto exit_unregister;
3828 err = platform_device_add_data(pdev, &sio_data,
3829 sizeof(struct nct6775_sio_data));
3831 pr_err("Platform data allocation failed\n");
3832 goto exit_device_put;
3835 memset(&res, 0, sizeof(res));
3837 res.start = address + IOREGION_OFFSET;
3838 res.end = address + IOREGION_OFFSET + IOREGION_LENGTH - 1;
3839 res.flags = IORESOURCE_IO;
3841 err = acpi_check_resource_conflict(&res);
3843 goto exit_device_put;
3845 err = platform_device_add_resources(pdev, &res, 1);
3847 pr_err("Device resource addition failed (%d)\n", err);
3848 goto exit_device_put;
3851 /* platform_device_add calls probe() */
3852 err = platform_device_add(pdev);
3854 pr_err("Device addition failed (%d)\n", err);
3855 goto exit_device_put;
3861 platform_device_put(pdev);
3863 platform_driver_unregister(&nct6775_driver);
3868 static void __exit sensors_nct6775_exit(void)
3870 platform_device_unregister(pdev);
3871 platform_driver_unregister(&nct6775_driver);
3874 MODULE_AUTHOR("Guenter Roeck <linux@roeck-us.net>");
3875 MODULE_DESCRIPTION("NCT6775F/NCT6776F/NCT6779D driver");
3876 MODULE_LICENSE("GPL");
3878 module_init(sensors_nct6775_init);
3879 module_exit(sensors_nct6775_exit);