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)
128 superio_exit(int ioreg)
132 outb(0x02, ioreg + 1);
139 #define IOREGION_ALIGNMENT (~7)
140 #define IOREGION_OFFSET 5
141 #define IOREGION_LENGTH 2
142 #define ADDR_REG_OFFSET 0
143 #define DATA_REG_OFFSET 1
145 #define NCT6775_REG_BANK 0x4E
146 #define NCT6775_REG_CONFIG 0x40
149 * Not currently used:
150 * REG_MAN_ID has the value 0x5ca3 for all supported chips.
151 * REG_CHIP_ID == 0x88/0xa1/0xc1 depending on chip model.
152 * REG_MAN_ID is at port 0x4f
153 * REG_CHIP_ID is at port 0x58
156 /* Voltage min/max registers for nr=7..14 are in bank 5 */
158 static const u16 NCT6775_REG_IN_MAX[] = {
159 0x2b, 0x2d, 0x2f, 0x31, 0x33, 0x35, 0x37, 0x554, 0x556, 0x558, 0x55a,
160 0x55c, 0x55e, 0x560, 0x562 };
161 static const u16 NCT6775_REG_IN_MIN[] = {
162 0x2c, 0x2e, 0x30, 0x32, 0x34, 0x36, 0x38, 0x555, 0x557, 0x559, 0x55b,
163 0x55d, 0x55f, 0x561, 0x563 };
164 static const u16 NCT6775_REG_IN[] = {
165 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x550, 0x551, 0x552
168 static const u16 NCT6779_REG_IN[] = {
169 0x480, 0x481, 0x482, 0x483, 0x484, 0x485, 0x486, 0x487,
170 0x488, 0x489, 0x48a, 0x48b, 0x48c, 0x48d, 0x48e
173 #define NCT6775_REG_VBAT 0x5D
174 #define NCT6775_REG_DIODE 0x5E
176 #define NCT6775_REG_FANDIV1 0x506
177 #define NCT6775_REG_FANDIV2 0x507
179 #define NCT6775_REG_CR_FAN_DEBOUNCE 0xf0
181 static const u16 NCT6775_REG_ALARM[6] = { 0x459, 0x45A, 0x45B };
182 static const u16 NCT6779_REG_ALARM[6] = { 0x459, 0x45A, 0x45B, 0x568 };
184 /* 0..15 voltages, 16..23 fans, 24..31 temperatures */
186 static const s8 NCT6775_ALARM_BITS[]
187 = { 0, 1, 2, 3, 8, 21, 20, 16, /* in0.. in7 */
188 17, -1, -1, -1, -1, -1, -1, /* in8..in14 */
190 6, 7, 11, 10, 23, /* fan1..fan5 */
191 -1, -1, -1, /* unused */
192 4, 5, 13, -1, -1, -1, /* temp1..temp6 */
193 12, -1 }; /* intrusion0, intrusion1 */
195 static const s8 NCT6776_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, 9 }; /* intrusion0, intrusion1 */
204 static const s8 NCT6779_ALARM_BITS[]
205 = { 0, 1, 2, 3, 8, 21, 20, 16, /* in0.. in7 */
206 17, 24, 25, 26, 27, 28, 29, /* 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 #define FAN_ALARM_BASE 16
214 #define TEMP_ALARM_BASE 24
215 #define INTRUSION_ALARM_BASE 30
217 static const u8 NCT6775_REG_CR_CASEOPEN_CLR[] = { 0xe6, 0xee };
218 static const u8 NCT6775_CR_CASEOPEN_CLR_MASK[] = { 0x20, 0x01 };
220 /* DC or PWM output fan configuration */
221 static const u8 NCT6775_REG_PWM_MODE[] = { 0x04, 0x04, 0x12 };
222 static const u8 NCT6775_PWM_MODE_MASK[] = { 0x01, 0x02, 0x01 };
224 static const u8 NCT6776_REG_PWM_MODE[] = { 0x04, 0, 0 };
225 static const u8 NCT6776_PWM_MODE_MASK[] = { 0x01, 0, 0 };
227 /* Advanced Fan control, some values are common for all fans */
229 static const u16 NCT6775_REG_FAN_PULSES[] = { 0x641, 0x642, 0x643, 0x644, 0 };
230 static const u16 NCT6776_REG_FAN_PULSES[] = { 0x644, 0x645, 0x646, 0, 0 };
231 static const u16 NCT6779_REG_FAN_PULSES[]
232 = { 0x644, 0x645, 0x646, 0x647, 0x648 };
234 static const u16 NCT6775_REG_TARGET[] = { 0x101, 0x201, 0x301, 0x801, 0x901 };
235 static const u16 NCT6775_REG_FAN_MODE[] = { 0x102, 0x202, 0x302, 0x802, 0x902 };
236 static const u16 NCT6775_REG_FAN_STEP_DOWN_TIME[] = {
237 0x103, 0x203, 0x303, 0x803, 0x903 };
238 static const u16 NCT6775_REG_FAN_STEP_UP_TIME[] = {
239 0x104, 0x204, 0x304, 0x804, 0x904 };
240 static const u16 NCT6775_REG_FAN_STOP_OUTPUT[] = {
241 0x105, 0x205, 0x305, 0x805, 0x905 };
242 static const u16 NCT6775_REG_FAN_START_OUTPUT[]
243 = { 0x106, 0x206, 0x306, 0x806, 0x906 };
244 static const u16 NCT6775_REG_FAN_MAX_OUTPUT[] = { 0x10a, 0x20a, 0x30a };
245 static const u16 NCT6775_REG_FAN_STEP_OUTPUT[] = { 0x10b, 0x20b, 0x30b };
247 static const u16 NCT6775_REG_FAN_STOP_TIME[]
248 = { 0x107, 0x207, 0x307, 0x807, 0x907 };
249 static const u16 NCT6775_REG_PWM[] = { 0x109, 0x209, 0x309, 0x809, 0x909 };
250 static const u16 NCT6775_REG_PWM_READ[] = { 0x01, 0x03, 0x11, 0x13, 0x15 };
252 static const u16 NCT6775_REG_FAN[] = { 0x630, 0x632, 0x634, 0x636, 0x638 };
253 static const u16 NCT6775_REG_FAN_MIN[] = { 0x3b, 0x3c, 0x3d };
255 static const u16 NCT6776_REG_FAN_MIN[] = { 0x63a, 0x63c, 0x63e, 0x640, 0x642};
257 static const u16 NCT6779_REG_FAN[] = { 0x4b0, 0x4b2, 0x4b4, 0x4b6, 0x4b8 };
259 static const u16 NCT6779_REG_TOLERANCE_H[]
260 = { 0x10c, 0x20c, 0x30c, 0x80c, 0x90c };
262 static const u16 NCT6775_REG_TEMP[]
263 = { 0x27, 0x150, 0x250, 0x62b, 0x62c, 0x62d };
264 static const u16 NCT6779_REG_TEMP[] = { 0x27, 0x150 };
266 static const u16 NCT6775_REG_TEMP_CONFIG[]
267 = { 0, 0x152, 0x252, 0x628, 0x629, 0x62A };
268 static const u16 NCT6775_REG_TEMP_HYST[]
269 = { 0x3a, 0x153, 0x253, 0x673, 0x678, 0x67D };
270 static const u16 NCT6775_REG_TEMP_OVER[]
271 = { 0x39, 0x155, 0x255, 0x672, 0x677, 0x67C };
273 static const u16 NCT6779_REG_TEMP_HYST[]
274 = { 0x3a, 0x153, 0, 0, 0, 0 };
275 static const u16 NCT6779_REG_TEMP_OVER[]
276 = { 0x39, 0x155, 0, 0, 0, 0 };
278 static const u16 NCT6775_REG_TEMP_SOURCE[]
279 = { 0x621, 0x622, 0x623, 0x624, 0x625, 0x626 };
281 static const u16 NCT6775_REG_TEMP_SEL[]
282 = { 0x100, 0x200, 0x300, 0x800, 0x900 };
284 static const u16 NCT6775_REG_WEIGHT_TEMP_SEL[]
285 = { 0x139, 0x239, 0x339, 0x839, 0x939 };
286 static const u16 NCT6775_REG_WEIGHT_TEMP_STEP[]
287 = { 0x13a, 0x23a, 0x33a, 0x83a, 0x93a };
288 static const u16 NCT6775_REG_WEIGHT_TEMP_STEP_TOL[]
289 = { 0x13b, 0x23b, 0x33b, 0x83b, 0x93b };
290 static const u16 NCT6775_REG_WEIGHT_DUTY_STEP[]
291 = { 0x13c, 0x23c, 0x33c, 0x83c, 0x93c };
292 static const u16 NCT6775_REG_WEIGHT_TEMP_BASE[]
293 = { 0x13d, 0x23d, 0x33d, 0x83d, 0x93d };
295 static const u16 NCT6776_REG_WEIGHT_DUTY_BASE[]
296 = { 0x13e, 0x23e, 0x33e, 0x83e, 0x93e };
298 static const u16 NCT6775_REG_TEMP_OFFSET[] = { 0x454, 0x455, 0x456 };
300 static const u16 NCT6779_REG_TEMP_OFFSET[]
301 = { 0x454, 0x455, 0x456, 0x44a, 0x44b, 0x44c };
303 static const u16 NCT6776_REG_TEMP_CONFIG[11]
304 = { 0x18, 0x152, 0x252, 0x628, 0x629, 0x62A };
306 static const u16 NCT6779_REG_TEMP_CONFIG[11] = { 0x18, 0x152 };
308 static const u16 NCT6775_REG_TEMP_MON[] = { 0x73, 0x75, 0x77, 0x79, 0x7b };
310 static const u16 NCT6775_REG_AUTO_TEMP[]
311 = { 0x121, 0x221, 0x321, 0x821, 0x921 };
312 static const u16 NCT6775_REG_AUTO_PWM[]
313 = { 0x127, 0x227, 0x327, 0x827, 0x927 };
315 #define NCT6775_AUTO_TEMP(data, nr, p) ((data)->REG_AUTO_TEMP[nr] + (p))
316 #define NCT6775_AUTO_PWM(data, nr, p) ((data)->REG_AUTO_PWM[nr] + (p))
318 static const u16 NCT6775_REG_CRITICAL_ENAB[] = { 0x134, 0x234, 0x334 };
320 static const u16 NCT6775_REG_CRITICAL_TEMP[] = {
321 0x135, 0x235, 0x335, 0x835, 0x935 };
322 static const u16 NCT6775_REG_CRITICAL_TEMP_TOLERANCE[] = {
323 0x138, 0x238, 0x338, 0x838, 0x938 };
325 static const u16 NCT6779_REG_CRITICAL_PWM_ENABLE[] = {
326 0x136, 0x236, 0x336, 0x836, 0x936 };
327 static const u16 NCT6779_REG_CRITICAL_PWM[] = {
328 0x137, 0x237, 0x337, 0x837, 0x937 };
330 static const char *const nct6775_temp_label[] = {
344 "PCH_CHIP_CPU_MAX_TEMP",
354 static const char *const nct6776_temp_label[] = {
369 "PCH_CHIP_CPU_MAX_TEMP",
380 static const char *const nct6779_temp_label[] = {
399 "PCH_CHIP_CPU_MAX_TEMP",
410 static const u16 NCT6775_REG_TEMP_ALTERNATE[ARRAY_SIZE(nct6775_temp_label) - 1]
411 = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0x661, 0x662, 0x664 };
413 static const u16 NCT6776_REG_TEMP_ALTERNATE[ARRAY_SIZE(nct6776_temp_label) - 1]
414 = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0x401, 0x402, 0x404 };
416 static const u16 NCT6779_REG_TEMP_ALTERNATE[ARRAY_SIZE(nct6779_temp_label) - 1]
417 = { 0x490, 0x491, 0x492, 0x493, 0x494, 0x495, 0, 0,
418 0, 0, 0, 0, 0, 0, 0, 0,
419 0, 0x400, 0x401, 0x402, 0x404, 0x405, 0x406, 0x407,
422 static const u16 NCT6775_REG_TEMP_CRIT[ARRAY_SIZE(nct6775_temp_label) - 1]
423 = { 0, 0, 0, 0, 0xa00, 0xa01, 0xa02, 0xa03, 0xa04, 0xa05, 0xa06,
426 static const u16 NCT6776_REG_TEMP_CRIT[ARRAY_SIZE(nct6776_temp_label) - 1]
427 = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0x709, 0x70a };
429 static const u16 NCT6779_REG_TEMP_CRIT[ARRAY_SIZE(nct6779_temp_label) - 1]
430 = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0x709, 0x70a };
432 #define NUM_TEMP 10 /* Max number of temp attribute sets w/ limits*/
433 #define NUM_TEMP_FIXED 6 /* Max number of fixed temp attribute sets */
435 static inline int reg_to_pwm_enable(int pwm, int mode)
437 if (mode == 0 && pwm == 255)
442 static inline int pwm_enable_to_reg(int mode)
453 /* 1 is DC mode, output in ms */
454 static inline unsigned int step_time_from_reg(u8 reg, u8 mode)
456 return mode ? 400 * reg : 100 * reg;
459 static inline u8 step_time_to_reg(unsigned int msec, u8 mode)
461 return SENSORS_LIMIT((mode ? (msec + 200) / 400 :
462 (msec + 50) / 100), 1, 255);
465 static unsigned int fan_from_reg8(u16 reg, unsigned int divreg)
467 if (reg == 0 || reg == 255)
469 return 1350000U / (reg << divreg);
472 static unsigned int fan_from_reg13(u16 reg, unsigned int divreg)
474 if ((reg & 0xff1f) == 0xff1f)
477 reg = (reg & 0x1f) | ((reg & 0xff00) >> 3);
482 return 1350000U / reg;
485 static unsigned int fan_from_reg16(u16 reg, unsigned int divreg)
487 if (reg == 0 || reg == 0xffff)
491 * Even though the registers are 16 bit wide, the fan divisor
494 return 1350000U / (reg << divreg);
497 static inline unsigned int
504 * Some of the voltage inputs have internal scaling, the tables below
505 * contain 8 (the ADC LSB in mV) * scaling factor * 100
507 static const u16 scale_in[15] = {
508 800, 800, 1600, 1600, 800, 800, 800, 1600, 1600, 800, 800, 800, 800,
512 static inline long in_from_reg(u8 reg, u8 nr)
514 return DIV_ROUND_CLOSEST(reg * scale_in[nr], 100);
517 static inline u8 in_to_reg(u32 val, u8 nr)
519 return SENSORS_LIMIT(DIV_ROUND_CLOSEST(val * 100, scale_in[nr]), 0,
524 * Data structures and manipulation thereof
527 struct nct6775_data {
528 int addr; /* IO base of hw monitor block */
532 struct device *hwmon_dev;
535 u16 reg_temp[4][NUM_TEMP]; /* 0=temp, 1=temp_over, 2=temp_hyst,
538 u8 temp_src[NUM_TEMP];
539 u16 reg_temp_config[NUM_TEMP];
540 const char * const *temp_label;
547 const s8 *ALARM_BITS;
550 const u16 *REG_IN_MINMAX[2];
552 const u16 *REG_TARGET;
554 const u16 *REG_FAN_MODE;
555 const u16 *REG_FAN_MIN;
556 const u16 *REG_FAN_TIME[3];
558 const u8 *REG_PWM_MODE;
559 const u8 *PWM_MODE_MASK;
561 const u16 *REG_PWM[7]; /* [0]=pwm, [1]=start_output, [2]=stop_output,
562 * [3]=max_output, [4]=step_output,
563 * [5]=weight_duty_step, [6]=weight_duty_base
565 const u16 *REG_PWM_READ;
567 const u16 *REG_TEMP_MON;
568 const u16 *REG_AUTO_TEMP;
569 const u16 *REG_AUTO_PWM;
571 const u16 *REG_CRITICAL_TEMP;
572 const u16 *REG_CRITICAL_TEMP_TOLERANCE;
574 const u16 *REG_TEMP_SOURCE; /* temp register sources */
575 const u16 *REG_TEMP_SEL[2]; /* pwm temp, 0=base, 1=weight */
577 const u16 *REG_WEIGHT_TEMP[3]; /* 0=base, 1=hyst, 2=step */
579 const u16 *REG_TEMP_OFFSET;
581 const u16 *REG_ALARM;
583 unsigned int (*fan_from_reg)(u16 reg, unsigned int divreg);
584 unsigned int (*fan_from_reg_min)(u16 reg, unsigned int divreg);
586 struct mutex update_lock;
587 char valid; /* !=0 if following fields are valid */
588 unsigned long last_updated; /* In jiffies */
590 /* Register values */
591 u8 bank; /* current register bank */
592 u8 in_num; /* number of in inputs we have */
593 u8 in[15][3]; /* [0]=in, [1]=in_max, [2]=in_min */
598 u8 has_fan; /* some fan inputs can be disabled */
599 u8 has_fan_min; /* some fans don't have min register */
602 u8 temp_fixed_num; /* 3 or 6 */
603 u8 temp_type[NUM_TEMP_FIXED];
604 s8 temp_offset[NUM_TEMP_FIXED];
605 s16 temp[3][NUM_TEMP]; /* 0=temp, 1=temp_over, 2=temp_hyst */
608 u8 pwm_num; /* number of pwm */
610 u8 pwm_mode[5]; /* 1->DC variable voltage, 0->PWM variable duty cycle */
611 u8 pwm_enable[5]; /* 0->off
613 * 2->thermal cruise mode (also called SmartFan I)
614 * 3->fan speed cruise mode
616 * 5->enhanced variable thermal cruise (SmartFan IV)
618 u8 pwm[7][5]; /* [0]=pwm, [1]=start_output, [2]=stop_output,
619 * [3]=max_output, [4]=step_output,
620 * [5]=weight_duty_step, [6]=weight_duty_base
626 u8 fan_time[3][5]; /* 0 = stop_time, 1 = step_up, 2 = step_down */
628 /* Automatic fan speed control registers */
633 u8 pwm_temp_sel[2][5];
635 bool pwm_sel_enable[2][5];/* 0->stop_val, 1->weight;
636 * false->off, true->on
638 u8 weight_temp[3][5]; /* 0->temp_step, 1->temp_step_tol,
650 struct nct6775_sio_data {
655 static bool is_word_sized(struct nct6775_data *data, u16 reg)
657 switch (data->kind) {
659 return (((reg & 0xff00) == 0x100 ||
660 (reg & 0xff00) == 0x200) &&
661 ((reg & 0x00ff) == 0x50 ||
662 (reg & 0x00ff) == 0x53 ||
663 (reg & 0x00ff) == 0x55)) ||
664 (reg & 0xfff0) == 0x630 ||
665 reg == 0x640 || reg == 0x642 ||
667 ((reg & 0xfff0) == 0x650 && (reg & 0x000f) >= 0x06) ||
668 reg == 0x73 || reg == 0x75 || reg == 0x77;
670 return (((reg & 0xff00) == 0x100 ||
671 (reg & 0xff00) == 0x200) &&
672 ((reg & 0x00ff) == 0x50 ||
673 (reg & 0x00ff) == 0x53 ||
674 (reg & 0x00ff) == 0x55)) ||
675 (reg & 0xfff0) == 0x630 ||
677 reg == 0x640 || reg == 0x642 ||
678 ((reg & 0xfff0) == 0x650 && (reg & 0x000f) >= 0x06) ||
679 reg == 0x73 || reg == 0x75 || reg == 0x77;
681 return reg == 0x150 || reg == 0x153 || reg == 0x155 ||
682 ((reg & 0xfff0) == 0x4b0 && (reg & 0x000f) < 0x09) ||
684 reg == 0x63a || reg == 0x63c || reg == 0x63e ||
685 reg == 0x640 || reg == 0x642 ||
686 reg == 0x73 || reg == 0x75 || reg == 0x77 || reg == 0x79 ||
693 * On older chips, only registers 0x50-0x5f are banked.
694 * On more recent chips, all registers are banked.
695 * Assume that is the case and set the bank number for each access.
696 * Cache the bank number so it only needs to be set if it changes.
698 static inline void nct6775_set_bank(struct nct6775_data *data, u16 reg)
701 if (data->bank != bank) {
702 outb_p(NCT6775_REG_BANK, data->addr + ADDR_REG_OFFSET);
703 outb_p(bank, data->addr + DATA_REG_OFFSET);
708 static u16 nct6775_read_value(struct nct6775_data *data, u16 reg)
710 int res, word_sized = is_word_sized(data, reg);
712 mutex_lock(&data->lock);
714 nct6775_set_bank(data, reg);
715 outb_p(reg & 0xff, data->addr + ADDR_REG_OFFSET);
716 res = inb_p(data->addr + DATA_REG_OFFSET);
718 outb_p((reg & 0xff) + 1,
719 data->addr + ADDR_REG_OFFSET);
720 res = (res << 8) + inb_p(data->addr + DATA_REG_OFFSET);
723 mutex_unlock(&data->lock);
727 static int nct6775_write_value(struct nct6775_data *data, u16 reg, u16 value)
729 int word_sized = is_word_sized(data, reg);
731 mutex_lock(&data->lock);
733 nct6775_set_bank(data, reg);
734 outb_p(reg & 0xff, data->addr + ADDR_REG_OFFSET);
736 outb_p(value >> 8, data->addr + DATA_REG_OFFSET);
737 outb_p((reg & 0xff) + 1,
738 data->addr + ADDR_REG_OFFSET);
740 outb_p(value & 0xff, data->addr + DATA_REG_OFFSET);
742 mutex_unlock(&data->lock);
746 /* We left-align 8-bit temperature values to make the code simpler */
747 static u16 nct6775_read_temp(struct nct6775_data *data, u16 reg)
751 res = nct6775_read_value(data, reg);
752 if (!is_word_sized(data, reg))
758 static int nct6775_write_temp(struct nct6775_data *data, u16 reg, u16 value)
760 if (!is_word_sized(data, reg))
762 return nct6775_write_value(data, reg, value);
765 /* This function assumes that the caller holds data->update_lock */
766 static void nct6775_write_fan_div(struct nct6775_data *data, int nr)
772 reg = (nct6775_read_value(data, NCT6775_REG_FANDIV1) & 0x70)
773 | (data->fan_div[0] & 0x7);
774 nct6775_write_value(data, NCT6775_REG_FANDIV1, reg);
777 reg = (nct6775_read_value(data, NCT6775_REG_FANDIV1) & 0x7)
778 | ((data->fan_div[1] << 4) & 0x70);
779 nct6775_write_value(data, NCT6775_REG_FANDIV1, reg);
781 reg = (nct6775_read_value(data, NCT6775_REG_FANDIV2) & 0x70)
782 | (data->fan_div[2] & 0x7);
783 nct6775_write_value(data, NCT6775_REG_FANDIV2, reg);
786 reg = (nct6775_read_value(data, NCT6775_REG_FANDIV2) & 0x7)
787 | ((data->fan_div[3] << 4) & 0x70);
788 nct6775_write_value(data, NCT6775_REG_FANDIV2, reg);
793 static void nct6775_write_fan_div_common(struct device *dev,
794 struct nct6775_data *data, int nr)
796 if (data->kind == nct6775)
797 nct6775_write_fan_div(data, nr);
800 static void nct6775_update_fan_div(struct nct6775_data *data)
804 i = nct6775_read_value(data, NCT6775_REG_FANDIV1);
805 data->fan_div[0] = i & 0x7;
806 data->fan_div[1] = (i & 0x70) >> 4;
807 i = nct6775_read_value(data, NCT6775_REG_FANDIV2);
808 data->fan_div[2] = i & 0x7;
809 if (data->has_fan & (1<<3))
810 data->fan_div[3] = (i & 0x70) >> 4;
813 static void nct6775_update_fan_div_common(struct device *dev,
814 struct nct6775_data *data)
816 if (data->kind == nct6775)
817 nct6775_update_fan_div(data);
820 static void nct6775_update_pwm(struct device *dev)
822 struct nct6775_data *data = dev_get_drvdata(dev);
827 for (i = 0; i < data->pwm_num; i++) {
828 if (!(data->has_pwm & (1 << i)))
831 duty_is_dc = data->REG_PWM_MODE[i] &&
832 (nct6775_read_value(data, data->REG_PWM_MODE[i])
833 & data->PWM_MODE_MASK[i]);
834 data->pwm_mode[i] = duty_is_dc;
836 fanmodecfg = nct6775_read_value(data, data->REG_FAN_MODE[i]);
837 for (j = 0; j < ARRAY_SIZE(data->REG_PWM); j++) {
838 if (data->REG_PWM[j] && data->REG_PWM[j][i]) {
840 = nct6775_read_value(data,
841 data->REG_PWM[j][i]);
845 data->pwm_enable[i] = reg_to_pwm_enable(data->pwm[0][i],
846 (fanmodecfg >> 4) & 7);
848 data->tolerance[i][0] = fanmodecfg & 0x0f;
849 if (data->kind == nct6779) {
850 reg = nct6775_read_value(data,
851 NCT6779_REG_TOLERANCE_H[i]);
852 data->tolerance[i][0] |= (reg & 0x70) >> 1;
854 data->tolerance[i][1] =
855 nct6775_read_value(data,
856 data->REG_CRITICAL_TEMP_TOLERANCE[i]);
858 data->pwm_sel_enable[0][i] =
859 nct6775_read_value(data, data->REG_TEMP_SEL[0][i])
861 data->pwm_sel_enable[1][i] =
862 nct6775_read_value(data, data->REG_TEMP_SEL[1][i])
866 for (j = 0; j < 2; j++) {
867 reg = nct6775_read_value(data,
868 data->REG_TEMP_SEL[j][i]);
869 data->pwm_temp_sel[j][i] = reg & 0x1f;
871 /* Weight temp data */
872 for (j = 0; j < 3; j++) {
873 data->weight_temp[j][i]
874 = nct6775_read_value(data,
875 data->REG_WEIGHT_TEMP[j][i]);
880 static void nct6775_update_pwm_limits(struct device *dev)
882 struct nct6775_data *data = dev_get_drvdata(dev);
886 for (i = 0; i < data->pwm_num; i++) {
887 if (!(data->has_pwm & (1 << i)))
890 for (j = 0; j < 3; j++) {
891 data->fan_time[j][i] =
892 nct6775_read_value(data, data->REG_FAN_TIME[j][i]);
895 data->target_temp[i] =
896 nct6775_read_value(data, data->REG_TARGET[i]) &
897 (data->pwm_mode[i] ? 0xff : 0x7f);
899 nct6775_read_value(data, data->REG_TEMP_MON[i]);
901 for (j = 0; j < data->auto_pwm_num; j++) {
902 data->auto_pwm[i][j] =
903 nct6775_read_value(data,
904 NCT6775_AUTO_PWM(data, i, j));
905 data->auto_temp[i][j] =
906 nct6775_read_value(data,
907 NCT6775_AUTO_TEMP(data, i, j));
910 /* critical auto_pwm temperature data */
911 data->auto_temp[i][data->auto_pwm_num] =
912 nct6775_read_value(data, data->REG_CRITICAL_TEMP[i]);
914 switch (data->kind) {
916 reg = nct6775_read_value(data,
917 NCT6775_REG_CRITICAL_ENAB[i]);
918 data->auto_pwm[i][data->auto_pwm_num] =
919 (reg & 0x02) ? 0xff : 0x00;
922 data->auto_pwm[i][data->auto_pwm_num] = 0xff;
925 reg = nct6775_read_value(data,
926 NCT6779_REG_CRITICAL_PWM_ENABLE[i]);
928 data->auto_pwm[i][data->auto_pwm_num] =
929 nct6775_read_value(data,
930 NCT6779_REG_CRITICAL_PWM[i]);
932 data->auto_pwm[i][data->auto_pwm_num] = 0xff;
938 static struct nct6775_data *nct6775_update_device(struct device *dev)
940 struct nct6775_data *data = dev_get_drvdata(dev);
943 mutex_lock(&data->update_lock);
945 if (time_after(jiffies, data->last_updated + HZ + HZ/2)
947 /* Fan clock dividers */
948 nct6775_update_fan_div_common(dev, data);
950 /* Measured voltages and limits */
951 for (i = 0; i < data->in_num; i++) {
952 if (!(data->have_in & (1 << i)))
955 data->in[i][0] = nct6775_read_value(data,
957 data->in[i][1] = nct6775_read_value(data,
958 data->REG_IN_MINMAX[0][i]);
959 data->in[i][2] = nct6775_read_value(data,
960 data->REG_IN_MINMAX[1][i]);
963 /* Measured fan speeds and limits */
964 for (i = 0; i < 5; i++) {
967 if (!(data->has_fan & (1 << i)))
970 reg = nct6775_read_value(data, data->REG_FAN[i]);
971 data->rpm[i] = data->fan_from_reg(reg,
974 if (data->has_fan_min & (1 << i))
975 data->fan_min[i] = nct6775_read_value(data,
976 data->REG_FAN_MIN[i]);
979 * If we failed to measure the fan speed and clock
980 * divider can be increased, let's try that for next
983 if (data->has_fan_div && (reg >= 0xff || reg == 0x00)
984 && data->fan_div[i] < 0x07) {
986 "Increasing fan%d clock divider from %u to %u\n",
987 i + 1, div_from_reg(data->fan_div[i]),
988 div_from_reg(data->fan_div[i] + 1));
990 nct6775_write_fan_div_common(dev, data, i);
991 /* Preserve min limit if possible */
992 if ((data->has_fan_min & (1 << i))
993 && data->fan_min[i] >= 2
994 && data->fan_min[i] != 255)
995 nct6775_write_value(data,
996 data->REG_FAN_MIN[i],
997 (data->fan_min[i] /= 2));
1001 nct6775_update_pwm(dev);
1002 nct6775_update_pwm_limits(dev);
1004 /* Measured temperatures and limits */
1005 for (i = 0; i < NUM_TEMP; i++) {
1006 if (!(data->have_temp & (1 << i)))
1008 for (j = 0; j < 4; j++) {
1009 if (data->reg_temp[j][i])
1011 = nct6775_read_temp(data,
1012 data->reg_temp[j][i]);
1014 if (!(data->have_temp_fixed & (1 << i)))
1016 data->temp_offset[i]
1017 = nct6775_read_value(data, data->REG_TEMP_OFFSET[i]);
1021 for (i = 0; i < 6; i++) {
1023 if (!data->REG_ALARM[i])
1025 alarm = nct6775_read_value(data, data->REG_ALARM[i]);
1026 data->alarms |= ((u64)alarm) << (i << 3);
1029 data->last_updated = jiffies;
1033 mutex_unlock(&data->update_lock);
1038 * Sysfs callback functions
1041 show_in_reg(struct device *dev, struct device_attribute *attr, char *buf)
1043 struct nct6775_data *data = nct6775_update_device(dev);
1044 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
1046 int index = sattr->index;
1047 return sprintf(buf, "%ld\n", in_from_reg(data->in[nr][index], nr));
1051 store_in_reg(struct device *dev, struct device_attribute *attr, const char *buf,
1054 struct nct6775_data *data = dev_get_drvdata(dev);
1055 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
1057 int index = sattr->index;
1059 int err = kstrtoul(buf, 10, &val);
1062 mutex_lock(&data->update_lock);
1063 data->in[nr][index] = in_to_reg(val, nr);
1064 nct6775_write_value(data, data->REG_IN_MINMAX[index-1][nr],
1065 data->in[nr][index]);
1066 mutex_unlock(&data->update_lock);
1071 show_alarm(struct device *dev, struct device_attribute *attr, char *buf)
1073 struct nct6775_data *data = nct6775_update_device(dev);
1074 struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr);
1075 int nr = data->ALARM_BITS[sattr->index];
1076 return sprintf(buf, "%u\n",
1077 (unsigned int)((data->alarms >> nr) & 0x01));
1080 static SENSOR_DEVICE_ATTR_2(in0_input, S_IRUGO, show_in_reg, NULL, 0, 0);
1081 static SENSOR_DEVICE_ATTR_2(in1_input, S_IRUGO, show_in_reg, NULL, 1, 0);
1082 static SENSOR_DEVICE_ATTR_2(in2_input, S_IRUGO, show_in_reg, NULL, 2, 0);
1083 static SENSOR_DEVICE_ATTR_2(in3_input, S_IRUGO, show_in_reg, NULL, 3, 0);
1084 static SENSOR_DEVICE_ATTR_2(in4_input, S_IRUGO, show_in_reg, NULL, 4, 0);
1085 static SENSOR_DEVICE_ATTR_2(in5_input, S_IRUGO, show_in_reg, NULL, 5, 0);
1086 static SENSOR_DEVICE_ATTR_2(in6_input, S_IRUGO, show_in_reg, NULL, 6, 0);
1087 static SENSOR_DEVICE_ATTR_2(in7_input, S_IRUGO, show_in_reg, NULL, 7, 0);
1088 static SENSOR_DEVICE_ATTR_2(in8_input, S_IRUGO, show_in_reg, NULL, 8, 0);
1089 static SENSOR_DEVICE_ATTR_2(in9_input, S_IRUGO, show_in_reg, NULL, 9, 0);
1090 static SENSOR_DEVICE_ATTR_2(in10_input, S_IRUGO, show_in_reg, NULL, 10, 0);
1091 static SENSOR_DEVICE_ATTR_2(in11_input, S_IRUGO, show_in_reg, NULL, 11, 0);
1092 static SENSOR_DEVICE_ATTR_2(in12_input, S_IRUGO, show_in_reg, NULL, 12, 0);
1093 static SENSOR_DEVICE_ATTR_2(in13_input, S_IRUGO, show_in_reg, NULL, 13, 0);
1094 static SENSOR_DEVICE_ATTR_2(in14_input, S_IRUGO, show_in_reg, NULL, 14, 0);
1096 static SENSOR_DEVICE_ATTR(in0_alarm, S_IRUGO, show_alarm, NULL, 0);
1097 static SENSOR_DEVICE_ATTR(in1_alarm, S_IRUGO, show_alarm, NULL, 1);
1098 static SENSOR_DEVICE_ATTR(in2_alarm, S_IRUGO, show_alarm, NULL, 2);
1099 static SENSOR_DEVICE_ATTR(in3_alarm, S_IRUGO, show_alarm, NULL, 3);
1100 static SENSOR_DEVICE_ATTR(in4_alarm, S_IRUGO, show_alarm, NULL, 4);
1101 static SENSOR_DEVICE_ATTR(in5_alarm, S_IRUGO, show_alarm, NULL, 5);
1102 static SENSOR_DEVICE_ATTR(in6_alarm, S_IRUGO, show_alarm, NULL, 6);
1103 static SENSOR_DEVICE_ATTR(in7_alarm, S_IRUGO, show_alarm, NULL, 7);
1104 static SENSOR_DEVICE_ATTR(in8_alarm, S_IRUGO, show_alarm, NULL, 8);
1105 static SENSOR_DEVICE_ATTR(in9_alarm, S_IRUGO, show_alarm, NULL, 9);
1106 static SENSOR_DEVICE_ATTR(in10_alarm, S_IRUGO, show_alarm, NULL, 10);
1107 static SENSOR_DEVICE_ATTR(in11_alarm, S_IRUGO, show_alarm, NULL, 11);
1108 static SENSOR_DEVICE_ATTR(in12_alarm, S_IRUGO, show_alarm, NULL, 12);
1109 static SENSOR_DEVICE_ATTR(in13_alarm, S_IRUGO, show_alarm, NULL, 13);
1110 static SENSOR_DEVICE_ATTR(in14_alarm, S_IRUGO, show_alarm, NULL, 14);
1112 static SENSOR_DEVICE_ATTR_2(in0_min, S_IWUSR | S_IRUGO, show_in_reg,
1113 store_in_reg, 0, 1);
1114 static SENSOR_DEVICE_ATTR_2(in1_min, S_IWUSR | S_IRUGO, show_in_reg,
1115 store_in_reg, 1, 1);
1116 static SENSOR_DEVICE_ATTR_2(in2_min, S_IWUSR | S_IRUGO, show_in_reg,
1117 store_in_reg, 2, 1);
1118 static SENSOR_DEVICE_ATTR_2(in3_min, S_IWUSR | S_IRUGO, show_in_reg,
1119 store_in_reg, 3, 1);
1120 static SENSOR_DEVICE_ATTR_2(in4_min, S_IWUSR | S_IRUGO, show_in_reg,
1121 store_in_reg, 4, 1);
1122 static SENSOR_DEVICE_ATTR_2(in5_min, S_IWUSR | S_IRUGO, show_in_reg,
1123 store_in_reg, 5, 1);
1124 static SENSOR_DEVICE_ATTR_2(in6_min, S_IWUSR | S_IRUGO, show_in_reg,
1125 store_in_reg, 6, 1);
1126 static SENSOR_DEVICE_ATTR_2(in7_min, S_IWUSR | S_IRUGO, show_in_reg,
1127 store_in_reg, 7, 1);
1128 static SENSOR_DEVICE_ATTR_2(in8_min, S_IWUSR | S_IRUGO, show_in_reg,
1129 store_in_reg, 8, 1);
1130 static SENSOR_DEVICE_ATTR_2(in9_min, S_IWUSR | S_IRUGO, show_in_reg,
1131 store_in_reg, 9, 1);
1132 static SENSOR_DEVICE_ATTR_2(in10_min, S_IWUSR | S_IRUGO, show_in_reg,
1133 store_in_reg, 10, 1);
1134 static SENSOR_DEVICE_ATTR_2(in11_min, S_IWUSR | S_IRUGO, show_in_reg,
1135 store_in_reg, 11, 1);
1136 static SENSOR_DEVICE_ATTR_2(in12_min, S_IWUSR | S_IRUGO, show_in_reg,
1137 store_in_reg, 12, 1);
1138 static SENSOR_DEVICE_ATTR_2(in13_min, S_IWUSR | S_IRUGO, show_in_reg,
1139 store_in_reg, 13, 1);
1140 static SENSOR_DEVICE_ATTR_2(in14_min, S_IWUSR | S_IRUGO, show_in_reg,
1141 store_in_reg, 14, 1);
1143 static SENSOR_DEVICE_ATTR_2(in0_max, S_IWUSR | S_IRUGO, show_in_reg,
1144 store_in_reg, 0, 2);
1145 static SENSOR_DEVICE_ATTR_2(in1_max, S_IWUSR | S_IRUGO, show_in_reg,
1146 store_in_reg, 1, 2);
1147 static SENSOR_DEVICE_ATTR_2(in2_max, S_IWUSR | S_IRUGO, show_in_reg,
1148 store_in_reg, 2, 2);
1149 static SENSOR_DEVICE_ATTR_2(in3_max, S_IWUSR | S_IRUGO, show_in_reg,
1150 store_in_reg, 3, 2);
1151 static SENSOR_DEVICE_ATTR_2(in4_max, S_IWUSR | S_IRUGO, show_in_reg,
1152 store_in_reg, 4, 2);
1153 static SENSOR_DEVICE_ATTR_2(in5_max, S_IWUSR | S_IRUGO, show_in_reg,
1154 store_in_reg, 5, 2);
1155 static SENSOR_DEVICE_ATTR_2(in6_max, S_IWUSR | S_IRUGO, show_in_reg,
1156 store_in_reg, 6, 2);
1157 static SENSOR_DEVICE_ATTR_2(in7_max, S_IWUSR | S_IRUGO, show_in_reg,
1158 store_in_reg, 7, 2);
1159 static SENSOR_DEVICE_ATTR_2(in8_max, S_IWUSR | S_IRUGO, show_in_reg,
1160 store_in_reg, 8, 2);
1161 static SENSOR_DEVICE_ATTR_2(in9_max, S_IWUSR | S_IRUGO, show_in_reg,
1162 store_in_reg, 9, 2);
1163 static SENSOR_DEVICE_ATTR_2(in10_max, S_IWUSR | S_IRUGO, show_in_reg,
1164 store_in_reg, 10, 2);
1165 static SENSOR_DEVICE_ATTR_2(in11_max, S_IWUSR | S_IRUGO, show_in_reg,
1166 store_in_reg, 11, 2);
1167 static SENSOR_DEVICE_ATTR_2(in12_max, S_IWUSR | S_IRUGO, show_in_reg,
1168 store_in_reg, 12, 2);
1169 static SENSOR_DEVICE_ATTR_2(in13_max, S_IWUSR | S_IRUGO, show_in_reg,
1170 store_in_reg, 13, 2);
1171 static SENSOR_DEVICE_ATTR_2(in14_max, S_IWUSR | S_IRUGO, show_in_reg,
1172 store_in_reg, 14, 2);
1174 static struct attribute *nct6775_attributes_in[15][5] = {
1176 &sensor_dev_attr_in0_input.dev_attr.attr,
1177 &sensor_dev_attr_in0_min.dev_attr.attr,
1178 &sensor_dev_attr_in0_max.dev_attr.attr,
1179 &sensor_dev_attr_in0_alarm.dev_attr.attr,
1183 &sensor_dev_attr_in1_input.dev_attr.attr,
1184 &sensor_dev_attr_in1_min.dev_attr.attr,
1185 &sensor_dev_attr_in1_max.dev_attr.attr,
1186 &sensor_dev_attr_in1_alarm.dev_attr.attr,
1190 &sensor_dev_attr_in2_input.dev_attr.attr,
1191 &sensor_dev_attr_in2_min.dev_attr.attr,
1192 &sensor_dev_attr_in2_max.dev_attr.attr,
1193 &sensor_dev_attr_in2_alarm.dev_attr.attr,
1197 &sensor_dev_attr_in3_input.dev_attr.attr,
1198 &sensor_dev_attr_in3_min.dev_attr.attr,
1199 &sensor_dev_attr_in3_max.dev_attr.attr,
1200 &sensor_dev_attr_in3_alarm.dev_attr.attr,
1204 &sensor_dev_attr_in4_input.dev_attr.attr,
1205 &sensor_dev_attr_in4_min.dev_attr.attr,
1206 &sensor_dev_attr_in4_max.dev_attr.attr,
1207 &sensor_dev_attr_in4_alarm.dev_attr.attr,
1211 &sensor_dev_attr_in5_input.dev_attr.attr,
1212 &sensor_dev_attr_in5_min.dev_attr.attr,
1213 &sensor_dev_attr_in5_max.dev_attr.attr,
1214 &sensor_dev_attr_in5_alarm.dev_attr.attr,
1218 &sensor_dev_attr_in6_input.dev_attr.attr,
1219 &sensor_dev_attr_in6_min.dev_attr.attr,
1220 &sensor_dev_attr_in6_max.dev_attr.attr,
1221 &sensor_dev_attr_in6_alarm.dev_attr.attr,
1225 &sensor_dev_attr_in7_input.dev_attr.attr,
1226 &sensor_dev_attr_in7_min.dev_attr.attr,
1227 &sensor_dev_attr_in7_max.dev_attr.attr,
1228 &sensor_dev_attr_in7_alarm.dev_attr.attr,
1232 &sensor_dev_attr_in8_input.dev_attr.attr,
1233 &sensor_dev_attr_in8_min.dev_attr.attr,
1234 &sensor_dev_attr_in8_max.dev_attr.attr,
1235 &sensor_dev_attr_in8_alarm.dev_attr.attr,
1239 &sensor_dev_attr_in9_input.dev_attr.attr,
1240 &sensor_dev_attr_in9_min.dev_attr.attr,
1241 &sensor_dev_attr_in9_max.dev_attr.attr,
1242 &sensor_dev_attr_in9_alarm.dev_attr.attr,
1246 &sensor_dev_attr_in10_input.dev_attr.attr,
1247 &sensor_dev_attr_in10_min.dev_attr.attr,
1248 &sensor_dev_attr_in10_max.dev_attr.attr,
1249 &sensor_dev_attr_in10_alarm.dev_attr.attr,
1253 &sensor_dev_attr_in11_input.dev_attr.attr,
1254 &sensor_dev_attr_in11_min.dev_attr.attr,
1255 &sensor_dev_attr_in11_max.dev_attr.attr,
1256 &sensor_dev_attr_in11_alarm.dev_attr.attr,
1260 &sensor_dev_attr_in12_input.dev_attr.attr,
1261 &sensor_dev_attr_in12_min.dev_attr.attr,
1262 &sensor_dev_attr_in12_max.dev_attr.attr,
1263 &sensor_dev_attr_in12_alarm.dev_attr.attr,
1267 &sensor_dev_attr_in13_input.dev_attr.attr,
1268 &sensor_dev_attr_in13_min.dev_attr.attr,
1269 &sensor_dev_attr_in13_max.dev_attr.attr,
1270 &sensor_dev_attr_in13_alarm.dev_attr.attr,
1274 &sensor_dev_attr_in14_input.dev_attr.attr,
1275 &sensor_dev_attr_in14_min.dev_attr.attr,
1276 &sensor_dev_attr_in14_max.dev_attr.attr,
1277 &sensor_dev_attr_in14_alarm.dev_attr.attr,
1282 static const struct attribute_group nct6775_group_in[15] = {
1283 { .attrs = nct6775_attributes_in[0] },
1284 { .attrs = nct6775_attributes_in[1] },
1285 { .attrs = nct6775_attributes_in[2] },
1286 { .attrs = nct6775_attributes_in[3] },
1287 { .attrs = nct6775_attributes_in[4] },
1288 { .attrs = nct6775_attributes_in[5] },
1289 { .attrs = nct6775_attributes_in[6] },
1290 { .attrs = nct6775_attributes_in[7] },
1291 { .attrs = nct6775_attributes_in[8] },
1292 { .attrs = nct6775_attributes_in[9] },
1293 { .attrs = nct6775_attributes_in[10] },
1294 { .attrs = nct6775_attributes_in[11] },
1295 { .attrs = nct6775_attributes_in[12] },
1296 { .attrs = nct6775_attributes_in[13] },
1297 { .attrs = nct6775_attributes_in[14] },
1301 show_fan(struct device *dev, struct device_attribute *attr, char *buf)
1303 struct nct6775_data *data = nct6775_update_device(dev);
1304 struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr);
1305 int nr = sattr->index;
1306 return sprintf(buf, "%d\n", data->rpm[nr]);
1310 show_fan_min(struct device *dev, struct device_attribute *attr, char *buf)
1312 struct nct6775_data *data = nct6775_update_device(dev);
1313 struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr);
1314 int nr = sattr->index;
1315 return sprintf(buf, "%d\n",
1316 data->fan_from_reg_min(data->fan_min[nr],
1317 data->fan_div[nr]));
1321 show_fan_div(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, "%u\n", div_from_reg(data->fan_div[nr]));
1330 store_fan_min(struct device *dev, struct device_attribute *attr,
1331 const char *buf, size_t count)
1333 struct nct6775_data *data = dev_get_drvdata(dev);
1334 struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr);
1335 int nr = sattr->index;
1341 err = kstrtoul(buf, 10, &val);
1345 mutex_lock(&data->update_lock);
1346 if (!data->has_fan_div) {
1347 /* NCT6776F or NCT6779D; we know this is a 13 bit register */
1353 val = 1350000U / val;
1354 val = (val & 0x1f) | ((val << 3) & 0xff00);
1356 data->fan_min[nr] = val;
1357 goto write_min; /* Leave fan divider alone */
1360 /* No min limit, alarm disabled */
1361 data->fan_min[nr] = 255;
1362 new_div = data->fan_div[nr]; /* No change */
1363 dev_info(dev, "fan%u low limit and alarm disabled\n", nr + 1);
1366 reg = 1350000U / val;
1367 if (reg >= 128 * 255) {
1369 * Speed below this value cannot possibly be represented,
1370 * even with the highest divider (128)
1372 data->fan_min[nr] = 254;
1373 new_div = 7; /* 128 == (1 << 7) */
1375 "fan%u low limit %lu below minimum %u, set to minimum\n",
1376 nr + 1, val, data->fan_from_reg_min(254, 7));
1379 * Speed above this value cannot possibly be represented,
1380 * even with the lowest divider (1)
1382 data->fan_min[nr] = 1;
1383 new_div = 0; /* 1 == (1 << 0) */
1385 "fan%u low limit %lu above maximum %u, set to maximum\n",
1386 nr + 1, val, data->fan_from_reg_min(1, 0));
1389 * Automatically pick the best divider, i.e. the one such
1390 * that the min limit will correspond to a register value
1391 * in the 96..192 range
1394 while (reg > 192 && new_div < 7) {
1398 data->fan_min[nr] = reg;
1403 * Write both the fan clock divider (if it changed) and the new
1404 * fan min (unconditionally)
1406 if (new_div != data->fan_div[nr]) {
1407 dev_dbg(dev, "fan%u clock divider changed from %u to %u\n",
1408 nr + 1, div_from_reg(data->fan_div[nr]),
1409 div_from_reg(new_div));
1410 data->fan_div[nr] = new_div;
1411 nct6775_write_fan_div_common(dev, data, nr);
1412 /* Give the chip time to sample a new speed value */
1413 data->last_updated = jiffies;
1417 nct6775_write_value(data, data->REG_FAN_MIN[nr], data->fan_min[nr]);
1418 mutex_unlock(&data->update_lock);
1423 static struct sensor_device_attribute sda_fan_input[] = {
1424 SENSOR_ATTR(fan1_input, S_IRUGO, show_fan, NULL, 0),
1425 SENSOR_ATTR(fan2_input, S_IRUGO, show_fan, NULL, 1),
1426 SENSOR_ATTR(fan3_input, S_IRUGO, show_fan, NULL, 2),
1427 SENSOR_ATTR(fan4_input, S_IRUGO, show_fan, NULL, 3),
1428 SENSOR_ATTR(fan5_input, S_IRUGO, show_fan, NULL, 4),
1431 static struct sensor_device_attribute sda_fan_alarm[] = {
1432 SENSOR_ATTR(fan1_alarm, S_IRUGO, show_alarm, NULL, FAN_ALARM_BASE),
1433 SENSOR_ATTR(fan2_alarm, S_IRUGO, show_alarm, NULL, FAN_ALARM_BASE + 1),
1434 SENSOR_ATTR(fan3_alarm, S_IRUGO, show_alarm, NULL, FAN_ALARM_BASE + 2),
1435 SENSOR_ATTR(fan4_alarm, S_IRUGO, show_alarm, NULL, FAN_ALARM_BASE + 3),
1436 SENSOR_ATTR(fan5_alarm, S_IRUGO, show_alarm, NULL, FAN_ALARM_BASE + 4),
1439 static struct sensor_device_attribute sda_fan_min[] = {
1440 SENSOR_ATTR(fan1_min, S_IWUSR | S_IRUGO, show_fan_min,
1442 SENSOR_ATTR(fan2_min, S_IWUSR | S_IRUGO, show_fan_min,
1444 SENSOR_ATTR(fan3_min, S_IWUSR | S_IRUGO, show_fan_min,
1446 SENSOR_ATTR(fan4_min, S_IWUSR | S_IRUGO, show_fan_min,
1448 SENSOR_ATTR(fan5_min, S_IWUSR | S_IRUGO, show_fan_min,
1452 static struct sensor_device_attribute sda_fan_div[] = {
1453 SENSOR_ATTR(fan1_div, S_IRUGO, show_fan_div, NULL, 0),
1454 SENSOR_ATTR(fan2_div, S_IRUGO, show_fan_div, NULL, 1),
1455 SENSOR_ATTR(fan3_div, S_IRUGO, show_fan_div, NULL, 2),
1456 SENSOR_ATTR(fan4_div, S_IRUGO, show_fan_div, NULL, 3),
1457 SENSOR_ATTR(fan5_div, S_IRUGO, show_fan_div, NULL, 4),
1461 show_temp_label(struct device *dev, struct device_attribute *attr, char *buf)
1463 struct nct6775_data *data = nct6775_update_device(dev);
1464 struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr);
1465 int nr = sattr->index;
1466 return sprintf(buf, "%s\n", data->temp_label[data->temp_src[nr]]);
1470 show_temp(struct device *dev, struct device_attribute *attr, char *buf)
1472 struct nct6775_data *data = nct6775_update_device(dev);
1473 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
1475 int index = sattr->index;
1477 return sprintf(buf, "%d\n", LM75_TEMP_FROM_REG(data->temp[index][nr]));
1481 store_temp(struct device *dev, struct device_attribute *attr, const char *buf,
1484 struct nct6775_data *data = dev_get_drvdata(dev);
1485 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
1487 int index = sattr->index;
1491 err = kstrtol(buf, 10, &val);
1495 mutex_lock(&data->update_lock);
1496 data->temp[index][nr] = LM75_TEMP_TO_REG(val);
1497 nct6775_write_temp(data, data->reg_temp[index][nr],
1498 data->temp[index][nr]);
1499 mutex_unlock(&data->update_lock);
1504 show_temp_offset(struct device *dev, struct device_attribute *attr, char *buf)
1506 struct nct6775_data *data = nct6775_update_device(dev);
1507 struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr);
1509 return sprintf(buf, "%d\n", data->temp_offset[sattr->index] * 1000);
1513 store_temp_offset(struct device *dev, struct device_attribute *attr,
1514 const char *buf, size_t count)
1516 struct nct6775_data *data = dev_get_drvdata(dev);
1517 struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr);
1518 int nr = sattr->index;
1522 err = kstrtol(buf, 10, &val);
1526 val = SENSORS_LIMIT(DIV_ROUND_CLOSEST(val, 1000), -128, 127);
1528 mutex_lock(&data->update_lock);
1529 data->temp_offset[nr] = val;
1530 nct6775_write_value(data, data->REG_TEMP_OFFSET[nr], val);
1531 mutex_unlock(&data->update_lock);
1537 show_temp_type(struct device *dev, struct device_attribute *attr, char *buf)
1539 struct nct6775_data *data = nct6775_update_device(dev);
1540 struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr);
1541 int nr = sattr->index;
1542 return sprintf(buf, "%d\n", (int)data->temp_type[nr]);
1546 store_temp_type(struct device *dev, struct device_attribute *attr,
1547 const char *buf, size_t count)
1549 struct nct6775_data *data = nct6775_update_device(dev);
1550 struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr);
1551 int nr = sattr->index;
1554 u8 vbat, diode, bit;
1556 err = kstrtoul(buf, 10, &val);
1560 if (val != 1 && val != 3 && val != 4)
1563 mutex_lock(&data->update_lock);
1565 data->temp_type[nr] = val;
1566 vbat = nct6775_read_value(data, data->REG_VBAT) & ~(0x02 << nr);
1567 diode = nct6775_read_value(data, data->REG_DIODE) & ~(0x02 << nr);
1570 case 1: /* CPU diode (diode, current mode) */
1574 case 3: /* diode, voltage mode */
1577 case 4: /* thermistor */
1580 nct6775_write_value(data, data->REG_VBAT, vbat);
1581 nct6775_write_value(data, data->REG_DIODE, diode);
1583 mutex_unlock(&data->update_lock);
1587 static struct sensor_device_attribute_2 sda_temp_input[] = {
1588 SENSOR_ATTR_2(temp1_input, S_IRUGO, show_temp, NULL, 0, 0),
1589 SENSOR_ATTR_2(temp2_input, S_IRUGO, show_temp, NULL, 1, 0),
1590 SENSOR_ATTR_2(temp3_input, S_IRUGO, show_temp, NULL, 2, 0),
1591 SENSOR_ATTR_2(temp4_input, S_IRUGO, show_temp, NULL, 3, 0),
1592 SENSOR_ATTR_2(temp5_input, S_IRUGO, show_temp, NULL, 4, 0),
1593 SENSOR_ATTR_2(temp6_input, S_IRUGO, show_temp, NULL, 5, 0),
1594 SENSOR_ATTR_2(temp7_input, S_IRUGO, show_temp, NULL, 6, 0),
1595 SENSOR_ATTR_2(temp8_input, S_IRUGO, show_temp, NULL, 7, 0),
1596 SENSOR_ATTR_2(temp9_input, S_IRUGO, show_temp, NULL, 8, 0),
1597 SENSOR_ATTR_2(temp10_input, S_IRUGO, show_temp, NULL, 9, 0),
1600 static struct sensor_device_attribute sda_temp_label[] = {
1601 SENSOR_ATTR(temp1_label, S_IRUGO, show_temp_label, NULL, 0),
1602 SENSOR_ATTR(temp2_label, S_IRUGO, show_temp_label, NULL, 1),
1603 SENSOR_ATTR(temp3_label, S_IRUGO, show_temp_label, NULL, 2),
1604 SENSOR_ATTR(temp4_label, S_IRUGO, show_temp_label, NULL, 3),
1605 SENSOR_ATTR(temp5_label, S_IRUGO, show_temp_label, NULL, 4),
1606 SENSOR_ATTR(temp6_label, S_IRUGO, show_temp_label, NULL, 5),
1607 SENSOR_ATTR(temp7_label, S_IRUGO, show_temp_label, NULL, 6),
1608 SENSOR_ATTR(temp8_label, S_IRUGO, show_temp_label, NULL, 7),
1609 SENSOR_ATTR(temp9_label, S_IRUGO, show_temp_label, NULL, 8),
1610 SENSOR_ATTR(temp10_label, S_IRUGO, show_temp_label, NULL, 9),
1613 static struct sensor_device_attribute_2 sda_temp_max[] = {
1614 SENSOR_ATTR_2(temp1_max, S_IRUGO | S_IWUSR, show_temp, store_temp,
1616 SENSOR_ATTR_2(temp2_max, S_IRUGO | S_IWUSR, show_temp, store_temp,
1618 SENSOR_ATTR_2(temp3_max, S_IRUGO | S_IWUSR, show_temp, store_temp,
1620 SENSOR_ATTR_2(temp4_max, S_IRUGO | S_IWUSR, show_temp, store_temp,
1622 SENSOR_ATTR_2(temp5_max, S_IRUGO | S_IWUSR, show_temp, store_temp,
1624 SENSOR_ATTR_2(temp6_max, S_IRUGO | S_IWUSR, show_temp, store_temp,
1626 SENSOR_ATTR_2(temp7_max, S_IRUGO | S_IWUSR, show_temp, store_temp,
1628 SENSOR_ATTR_2(temp8_max, S_IRUGO | S_IWUSR, show_temp, store_temp,
1630 SENSOR_ATTR_2(temp9_max, S_IRUGO | S_IWUSR, show_temp, store_temp,
1632 SENSOR_ATTR_2(temp10_max, S_IRUGO | S_IWUSR, show_temp, store_temp,
1636 static struct sensor_device_attribute_2 sda_temp_max_hyst[] = {
1637 SENSOR_ATTR_2(temp1_max_hyst, S_IRUGO | S_IWUSR, show_temp, store_temp,
1639 SENSOR_ATTR_2(temp2_max_hyst, S_IRUGO | S_IWUSR, show_temp, store_temp,
1641 SENSOR_ATTR_2(temp3_max_hyst, S_IRUGO | S_IWUSR, show_temp, store_temp,
1643 SENSOR_ATTR_2(temp4_max_hyst, S_IRUGO | S_IWUSR, show_temp, store_temp,
1645 SENSOR_ATTR_2(temp5_max_hyst, S_IRUGO | S_IWUSR, show_temp, store_temp,
1647 SENSOR_ATTR_2(temp6_max_hyst, S_IRUGO | S_IWUSR, show_temp, store_temp,
1649 SENSOR_ATTR_2(temp7_max_hyst, S_IRUGO | S_IWUSR, show_temp, store_temp,
1651 SENSOR_ATTR_2(temp8_max_hyst, S_IRUGO | S_IWUSR, show_temp, store_temp,
1653 SENSOR_ATTR_2(temp9_max_hyst, S_IRUGO | S_IWUSR, show_temp, store_temp,
1655 SENSOR_ATTR_2(temp10_max_hyst, S_IRUGO | S_IWUSR, show_temp, store_temp,
1659 static struct sensor_device_attribute_2 sda_temp_crit[] = {
1660 SENSOR_ATTR_2(temp1_crit, S_IRUGO | S_IWUSR, show_temp, store_temp,
1662 SENSOR_ATTR_2(temp2_crit, S_IRUGO | S_IWUSR, show_temp, store_temp,
1664 SENSOR_ATTR_2(temp3_crit, S_IRUGO | S_IWUSR, show_temp, store_temp,
1666 SENSOR_ATTR_2(temp4_crit, S_IRUGO | S_IWUSR, show_temp, store_temp,
1668 SENSOR_ATTR_2(temp5_crit, S_IRUGO | S_IWUSR, show_temp, store_temp,
1670 SENSOR_ATTR_2(temp6_crit, S_IRUGO | S_IWUSR, show_temp, store_temp,
1672 SENSOR_ATTR_2(temp7_crit, S_IRUGO | S_IWUSR, show_temp, store_temp,
1674 SENSOR_ATTR_2(temp8_crit, S_IRUGO | S_IWUSR, show_temp, store_temp,
1676 SENSOR_ATTR_2(temp9_crit, S_IRUGO | S_IWUSR, show_temp, store_temp,
1678 SENSOR_ATTR_2(temp10_crit, S_IRUGO | S_IWUSR, show_temp, store_temp,
1682 static struct sensor_device_attribute sda_temp_offset[] = {
1683 SENSOR_ATTR(temp1_offset, S_IRUGO | S_IWUSR, show_temp_offset,
1684 store_temp_offset, 0),
1685 SENSOR_ATTR(temp2_offset, S_IRUGO | S_IWUSR, show_temp_offset,
1686 store_temp_offset, 1),
1687 SENSOR_ATTR(temp3_offset, S_IRUGO | S_IWUSR, show_temp_offset,
1688 store_temp_offset, 2),
1689 SENSOR_ATTR(temp4_offset, S_IRUGO | S_IWUSR, show_temp_offset,
1690 store_temp_offset, 3),
1691 SENSOR_ATTR(temp5_offset, S_IRUGO | S_IWUSR, show_temp_offset,
1692 store_temp_offset, 4),
1693 SENSOR_ATTR(temp6_offset, S_IRUGO | S_IWUSR, show_temp_offset,
1694 store_temp_offset, 5),
1697 static struct sensor_device_attribute sda_temp_type[] = {
1698 SENSOR_ATTR(temp1_type, S_IRUGO | S_IWUSR, show_temp_type,
1699 store_temp_type, 0),
1700 SENSOR_ATTR(temp2_type, S_IRUGO | S_IWUSR, show_temp_type,
1701 store_temp_type, 1),
1702 SENSOR_ATTR(temp3_type, S_IRUGO | S_IWUSR, show_temp_type,
1703 store_temp_type, 2),
1704 SENSOR_ATTR(temp4_type, S_IRUGO | S_IWUSR, show_temp_type,
1705 store_temp_type, 3),
1706 SENSOR_ATTR(temp5_type, S_IRUGO | S_IWUSR, show_temp_type,
1707 store_temp_type, 4),
1708 SENSOR_ATTR(temp6_type, S_IRUGO | S_IWUSR, show_temp_type,
1709 store_temp_type, 5),
1712 static struct sensor_device_attribute sda_temp_alarm[] = {
1713 SENSOR_ATTR(temp1_alarm, S_IRUGO, show_alarm, NULL,
1715 SENSOR_ATTR(temp2_alarm, S_IRUGO, show_alarm, NULL,
1716 TEMP_ALARM_BASE + 1),
1717 SENSOR_ATTR(temp3_alarm, S_IRUGO, show_alarm, NULL,
1718 TEMP_ALARM_BASE + 2),
1719 SENSOR_ATTR(temp4_alarm, S_IRUGO, show_alarm, NULL,
1720 TEMP_ALARM_BASE + 3),
1721 SENSOR_ATTR(temp5_alarm, S_IRUGO, show_alarm, NULL,
1722 TEMP_ALARM_BASE + 4),
1723 SENSOR_ATTR(temp6_alarm, S_IRUGO, show_alarm, NULL,
1724 TEMP_ALARM_BASE + 5),
1727 #define NUM_TEMP_ALARM ARRAY_SIZE(sda_temp_alarm)
1730 show_pwm_mode(struct device *dev, struct device_attribute *attr, char *buf)
1732 struct nct6775_data *data = nct6775_update_device(dev);
1733 struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr);
1735 return sprintf(buf, "%d\n", !data->pwm_mode[sattr->index]);
1739 store_pwm_mode(struct device *dev, struct device_attribute *attr,
1740 const char *buf, size_t count)
1742 struct nct6775_data *data = dev_get_drvdata(dev);
1743 struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr);
1744 int nr = sattr->index;
1749 err = kstrtoul(buf, 10, &val);
1756 /* Setting DC mode is not supported for all chips/channels */
1757 if (data->REG_PWM_MODE[nr] == 0) {
1763 mutex_lock(&data->update_lock);
1764 data->pwm_mode[nr] = val;
1765 reg = nct6775_read_value(data, data->REG_PWM_MODE[nr]);
1766 reg &= ~data->PWM_MODE_MASK[nr];
1768 reg |= data->PWM_MODE_MASK[nr];
1769 nct6775_write_value(data, data->REG_PWM_MODE[nr], reg);
1770 mutex_unlock(&data->update_lock);
1775 show_pwm_enable(struct device *dev, struct device_attribute *attr, char *buf)
1777 struct nct6775_data *data = nct6775_update_device(dev);
1778 struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr);
1780 return sprintf(buf, "%d\n", data->pwm_enable[sattr->index]);
1784 show_pwm(struct device *dev, struct device_attribute *attr, char *buf)
1786 struct nct6775_data *data = nct6775_update_device(dev);
1787 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
1789 int index = sattr->index;
1793 * For automatic fan control modes, show current pwm readings.
1794 * Otherwise, show the configured value.
1796 if (index == 0 && data->pwm_enable[nr] > 1)
1797 pwm = nct6775_read_value(data, data->REG_PWM_READ[nr]);
1799 pwm = data->pwm[index][nr];
1801 return sprintf(buf, "%d\n", pwm);
1805 store_pwm(struct device *dev, struct device_attribute *attr, const char *buf,
1808 struct nct6775_data *data = dev_get_drvdata(dev);
1809 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
1811 int index = sattr->index;
1813 int minval[7] = { 0, 1, 1, data->pwm[2][nr], 0, 0, 0 };
1815 = { 255, 255, data->pwm[3][nr] ? : 255, 255, 255, 255, 255 };
1818 err = kstrtoul(buf, 10, &val);
1821 val = SENSORS_LIMIT(val, minval[index], maxval[index]);
1823 mutex_lock(&data->update_lock);
1824 data->pwm[index][nr] = val;
1825 nct6775_write_value(data, data->REG_PWM[index][nr], val);
1826 mutex_unlock(&data->update_lock);
1830 /* Returns 0 if OK, -EINVAL otherwise */
1831 static int check_trip_points(struct nct6775_data *data, int nr)
1835 for (i = 0; i < data->auto_pwm_num - 1; i++) {
1836 if (data->auto_temp[nr][i] > data->auto_temp[nr][i + 1])
1839 for (i = 0; i < data->auto_pwm_num - 1; i++) {
1840 if (data->auto_pwm[nr][i] > data->auto_pwm[nr][i + 1])
1843 /* validate critical temperature and pwm if enabled (pwm > 0) */
1844 if (data->auto_pwm[nr][data->auto_pwm_num]) {
1845 if (data->auto_temp[nr][data->auto_pwm_num - 1] >
1846 data->auto_temp[nr][data->auto_pwm_num] ||
1847 data->auto_pwm[nr][data->auto_pwm_num - 1] >
1848 data->auto_pwm[nr][data->auto_pwm_num])
1855 store_pwm_enable(struct device *dev, struct device_attribute *attr,
1856 const char *buf, size_t count)
1858 struct nct6775_data *data = dev_get_drvdata(dev);
1859 struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr);
1860 int nr = sattr->index;
1865 err = kstrtoul(buf, 10, &val);
1872 if (val == 4 && data->kind != nct6775)
1875 if (val == 5 && check_trip_points(data, nr)) {
1876 dev_err(dev, "Inconsistent trip points, not switching to SmartFan IV mode\n");
1877 dev_err(dev, "Adjust trip points and try again\n");
1881 mutex_lock(&data->update_lock);
1882 data->pwm_enable[nr] = val;
1885 * turn off pwm control: select manual mode, set pwm to maximum
1887 data->pwm[0][nr] = 255;
1888 nct6775_write_value(data, data->REG_PWM[0][nr], 255);
1890 reg = nct6775_read_value(data, data->REG_FAN_MODE[nr]);
1892 reg |= pwm_enable_to_reg(val) << 4;
1893 nct6775_write_value(data, data->REG_FAN_MODE[nr], reg);
1894 mutex_unlock(&data->update_lock);
1899 show_pwm_temp_sel(struct device *dev, struct device_attribute *attr, char *buf)
1901 struct nct6775_data *data = nct6775_update_device(dev);
1902 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
1903 int i, src, sel = 0;
1905 src = data->pwm_temp_sel[sattr->index][sattr->nr];
1907 for (i = 0; i < NUM_TEMP; i++) {
1908 if (!(data->have_temp & (1 << i)))
1910 if (src == data->temp_src[i]) {
1916 return sprintf(buf, "%d\n", sel);
1920 store_pwm_temp_sel(struct device *dev, struct device_attribute *attr,
1921 const char *buf, size_t count)
1923 struct nct6775_data *data = nct6775_update_device(dev);
1924 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
1926 int index = sattr->index;
1930 err = kstrtoul(buf, 10, &val);
1933 if (val == 0 || val > NUM_TEMP)
1935 if (!(data->have_temp & (1 << (val - 1))) || !data->temp_src[val - 1])
1938 mutex_lock(&data->update_lock);
1939 src = data->temp_src[val - 1];
1940 data->pwm_temp_sel[index][nr] = src;
1941 reg = nct6775_read_value(data, data->REG_TEMP_SEL[index][nr]);
1944 nct6775_write_value(data, data->REG_TEMP_SEL[index][nr], reg);
1945 mutex_unlock(&data->update_lock);
1951 show_target_temp(struct device *dev, struct device_attribute *attr, char *buf)
1953 struct nct6775_data *data = nct6775_update_device(dev);
1954 struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr);
1956 return sprintf(buf, "%d\n",
1957 data->target_temp[sattr->index] * 1000);
1961 store_target_temp(struct device *dev, struct device_attribute *attr,
1962 const char *buf, size_t count)
1964 struct nct6775_data *data = dev_get_drvdata(dev);
1965 struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr);
1966 int nr = sattr->index;
1970 err = kstrtol(buf, 10, &val);
1974 val = SENSORS_LIMIT(DIV_ROUND_CLOSEST(val, 1000), 0, 127);
1976 mutex_lock(&data->update_lock);
1977 data->target_temp[nr] = val;
1978 nct6775_write_value(data, data->REG_TARGET[nr], val);
1979 mutex_unlock(&data->update_lock);
1984 show_auto_temp_hyst(struct device *dev, struct device_attribute *attr,
1987 struct nct6775_data *data = nct6775_update_device(dev);
1988 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
1990 int point = sattr->index >= data->auto_pwm_num ? 1 : 0;
1991 int tolerance = data->tolerance[nr][point];
1992 int temp = data->auto_temp[nr][sattr->index];
1994 return sprintf(buf, "%d\n", (temp - tolerance) * 1000);
1998 store_auto_temp_hyst(struct device *dev, struct device_attribute *attr,
1999 const char *buf, size_t count)
2001 struct nct6775_data *data = dev_get_drvdata(dev);
2002 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
2004 int point = sattr->index >= data->auto_pwm_num ? 1 : 0;
2008 int maxlimit[2][3] = { { 15, 7, 63 }, { 15, 7, 7 } };
2009 int mask[] = { 0x0f, 0x07, 0x07 };
2012 err = kstrtol(buf, 10, &val);
2016 temp = data->auto_temp[nr][sattr->index];
2017 val = temp - DIV_ROUND_CLOSEST(val, 1000);
2019 /* Limit tolerance as needed */
2020 val = SENSORS_LIMIT(val, 0, maxlimit[point][data->kind]);
2022 mutex_lock(&data->update_lock);
2024 nct6775_write_value(data,
2025 data->REG_CRITICAL_TEMP_TOLERANCE[nr],
2028 reg = nct6775_read_value(data, data->REG_FAN_MODE[nr]);
2029 reg = (reg & ~mask[nr]) | (val & mask[nr]);
2030 nct6775_write_value(data, data->REG_FAN_MODE[nr], reg);
2031 if (data->kind == nct6779) {
2032 reg = nct6775_read_value(data,
2033 NCT6779_REG_TOLERANCE_H[nr]);
2034 reg = (reg & 0x70) | ((val & 0x38) << 1);
2035 nct6775_write_value(data,
2036 NCT6779_REG_TOLERANCE_H[nr], reg);
2040 data->tolerance[nr][point] = val;
2041 mutex_unlock(&data->update_lock);
2045 static SENSOR_DEVICE_ATTR_2(pwm1, S_IWUSR | S_IRUGO, show_pwm, store_pwm, 0, 0);
2046 static SENSOR_DEVICE_ATTR_2(pwm2, S_IWUSR | S_IRUGO, show_pwm, store_pwm, 1, 0);
2047 static SENSOR_DEVICE_ATTR_2(pwm3, S_IWUSR | S_IRUGO, show_pwm, store_pwm, 2, 0);
2048 static SENSOR_DEVICE_ATTR_2(pwm4, S_IWUSR | S_IRUGO, show_pwm, store_pwm, 3, 0);
2049 static SENSOR_DEVICE_ATTR_2(pwm5, S_IWUSR | S_IRUGO, show_pwm, store_pwm, 4, 0);
2051 static SENSOR_DEVICE_ATTR(pwm1_mode, S_IWUSR | S_IRUGO, show_pwm_mode,
2053 static SENSOR_DEVICE_ATTR(pwm2_mode, S_IWUSR | S_IRUGO, show_pwm_mode,
2055 static SENSOR_DEVICE_ATTR(pwm3_mode, S_IWUSR | S_IRUGO, show_pwm_mode,
2057 static SENSOR_DEVICE_ATTR(pwm4_mode, S_IWUSR | S_IRUGO, show_pwm_mode,
2059 static SENSOR_DEVICE_ATTR(pwm5_mode, S_IWUSR | S_IRUGO, show_pwm_mode,
2062 static SENSOR_DEVICE_ATTR(pwm1_enable, S_IWUSR | S_IRUGO, show_pwm_enable,
2063 store_pwm_enable, 0);
2064 static SENSOR_DEVICE_ATTR(pwm2_enable, S_IWUSR | S_IRUGO, show_pwm_enable,
2065 store_pwm_enable, 1);
2066 static SENSOR_DEVICE_ATTR(pwm3_enable, S_IWUSR | S_IRUGO, show_pwm_enable,
2067 store_pwm_enable, 2);
2068 static SENSOR_DEVICE_ATTR(pwm4_enable, S_IWUSR | S_IRUGO, show_pwm_enable,
2069 store_pwm_enable, 3);
2070 static SENSOR_DEVICE_ATTR(pwm5_enable, S_IWUSR | S_IRUGO, show_pwm_enable,
2071 store_pwm_enable, 4);
2073 static SENSOR_DEVICE_ATTR_2(pwm1_temp_sel, S_IWUSR | S_IRUGO,
2074 show_pwm_temp_sel, store_pwm_temp_sel, 0, 0);
2075 static SENSOR_DEVICE_ATTR_2(pwm2_temp_sel, S_IWUSR | S_IRUGO,
2076 show_pwm_temp_sel, store_pwm_temp_sel, 1, 0);
2077 static SENSOR_DEVICE_ATTR_2(pwm3_temp_sel, S_IWUSR | S_IRUGO,
2078 show_pwm_temp_sel, store_pwm_temp_sel, 2, 0);
2079 static SENSOR_DEVICE_ATTR_2(pwm4_temp_sel, S_IWUSR | S_IRUGO,
2080 show_pwm_temp_sel, store_pwm_temp_sel, 3, 0);
2081 static SENSOR_DEVICE_ATTR_2(pwm5_temp_sel, S_IWUSR | S_IRUGO,
2082 show_pwm_temp_sel, store_pwm_temp_sel, 4, 0);
2084 static SENSOR_DEVICE_ATTR(pwm1_target, S_IWUSR | S_IRUGO, show_target_temp,
2085 store_target_temp, 0);
2086 static SENSOR_DEVICE_ATTR(pwm2_target, S_IWUSR | S_IRUGO, show_target_temp,
2087 store_target_temp, 1);
2088 static SENSOR_DEVICE_ATTR(pwm3_target, S_IWUSR | S_IRUGO, show_target_temp,
2089 store_target_temp, 2);
2090 static SENSOR_DEVICE_ATTR(pwm4_target, S_IWUSR | S_IRUGO, show_target_temp,
2091 store_target_temp, 3);
2092 static SENSOR_DEVICE_ATTR(pwm5_target, S_IWUSR | S_IRUGO, show_target_temp,
2093 store_target_temp, 4);
2095 /* Smart Fan registers */
2098 show_pwm_sel_enable(struct device *dev, struct device_attribute *attr,
2101 struct nct6775_data *data = nct6775_update_device(dev);
2102 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
2104 return sprintf(buf, "%d\n",
2105 data->pwm_sel_enable[sattr->index][sattr->nr]);
2109 store_pwm_sel_enable(struct device *dev, struct device_attribute *attr,
2110 const char *buf, size_t count)
2112 struct nct6775_data *data = dev_get_drvdata(dev);
2113 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
2115 int index = sattr->index;
2120 err = kstrtoul(buf, 10, &val);
2127 mutex_lock(&data->update_lock);
2128 data->pwm_sel_enable[index][nr] = val;
2129 reg = nct6775_read_value(data, data->REG_TEMP_SEL[index][nr]);
2133 nct6775_write_value(data, data->REG_TEMP_SEL[index][nr], reg);
2134 mutex_unlock(&data->update_lock);
2139 show_weight_temp(struct device *dev, struct device_attribute *attr, char *buf)
2141 struct nct6775_data *data = nct6775_update_device(dev);
2142 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
2144 int index = sattr->index;
2146 return sprintf(buf, "%d\n", data->weight_temp[index][nr] * 1000);
2150 store_weight_temp(struct device *dev, struct device_attribute *attr,
2151 const char *buf, size_t count)
2153 struct nct6775_data *data = dev_get_drvdata(dev);
2154 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
2156 int index = sattr->index;
2160 err = kstrtoul(buf, 10, &val);
2164 val = SENSORS_LIMIT(DIV_ROUND_CLOSEST(val, 1000), 0, 255);
2166 mutex_lock(&data->update_lock);
2167 data->weight_temp[index][nr] = val;
2168 nct6775_write_value(data, data->REG_WEIGHT_TEMP[index][nr], val);
2169 mutex_unlock(&data->update_lock);
2173 static SENSOR_DEVICE_ATTR_2(pwm1_stop_output_enable, S_IWUSR | S_IRUGO,
2174 show_pwm_sel_enable, store_pwm_sel_enable, 0, 0);
2175 static SENSOR_DEVICE_ATTR_2(pwm2_stop_output_enable, S_IWUSR | S_IRUGO,
2176 show_pwm_sel_enable, store_pwm_sel_enable, 1, 0);
2177 static SENSOR_DEVICE_ATTR_2(pwm3_stop_output_enable, S_IWUSR | S_IRUGO,
2178 show_pwm_sel_enable, store_pwm_sel_enable, 2, 0);
2179 static SENSOR_DEVICE_ATTR_2(pwm4_stop_output_enable, S_IWUSR | S_IRUGO,
2180 show_pwm_sel_enable, store_pwm_sel_enable, 3, 0);
2181 static SENSOR_DEVICE_ATTR_2(pwm5_stop_output_enable, S_IWUSR | S_IRUGO,
2182 show_pwm_sel_enable, store_pwm_sel_enable, 4, 0);
2184 static SENSOR_DEVICE_ATTR_2(pwm1_weight_enable, S_IWUSR | S_IRUGO,
2185 show_pwm_sel_enable, store_pwm_sel_enable, 0, 1);
2186 static SENSOR_DEVICE_ATTR_2(pwm2_weight_enable, S_IWUSR | S_IRUGO,
2187 show_pwm_sel_enable, store_pwm_sel_enable, 1, 1);
2188 static SENSOR_DEVICE_ATTR_2(pwm3_weight_enable, S_IWUSR | S_IRUGO,
2189 show_pwm_sel_enable, store_pwm_sel_enable, 2, 1);
2190 static SENSOR_DEVICE_ATTR_2(pwm4_weight_enable, S_IWUSR | S_IRUGO,
2191 show_pwm_sel_enable, store_pwm_sel_enable, 3, 1);
2192 static SENSOR_DEVICE_ATTR_2(pwm5_weight_enable, S_IWUSR | S_IRUGO,
2193 show_pwm_sel_enable, store_pwm_sel_enable, 4, 1);
2195 static SENSOR_DEVICE_ATTR_2(pwm1_weight_temp_sel, S_IWUSR | S_IRUGO,
2196 show_pwm_temp_sel, store_pwm_temp_sel, 0, 1);
2197 static SENSOR_DEVICE_ATTR_2(pwm2_weight_temp_sel, S_IWUSR | S_IRUGO,
2198 show_pwm_temp_sel, store_pwm_temp_sel, 1, 1);
2199 static SENSOR_DEVICE_ATTR_2(pwm3_weight_temp_sel, S_IWUSR | S_IRUGO,
2200 show_pwm_temp_sel, store_pwm_temp_sel, 2, 1);
2201 static SENSOR_DEVICE_ATTR_2(pwm4_weight_temp_sel, S_IWUSR | S_IRUGO,
2202 show_pwm_temp_sel, store_pwm_temp_sel, 3, 1);
2203 static SENSOR_DEVICE_ATTR_2(pwm5_weight_temp_sel, S_IWUSR | S_IRUGO,
2204 show_pwm_temp_sel, store_pwm_temp_sel, 4, 1);
2206 static SENSOR_DEVICE_ATTR_2(pwm1_weight_temp_step, S_IWUSR | S_IRUGO,
2207 show_weight_temp, store_weight_temp, 0, 0);
2208 static SENSOR_DEVICE_ATTR_2(pwm2_weight_temp_step, S_IWUSR | S_IRUGO,
2209 show_weight_temp, store_weight_temp, 1, 0);
2210 static SENSOR_DEVICE_ATTR_2(pwm3_weight_temp_step, S_IWUSR | S_IRUGO,
2211 show_weight_temp, store_weight_temp, 2, 0);
2212 static SENSOR_DEVICE_ATTR_2(pwm4_weight_temp_step, S_IWUSR | S_IRUGO,
2213 show_weight_temp, store_weight_temp, 3, 0);
2214 static SENSOR_DEVICE_ATTR_2(pwm5_weight_temp_step, S_IWUSR | S_IRUGO,
2215 show_weight_temp, store_weight_temp, 4, 0);
2217 static SENSOR_DEVICE_ATTR_2(pwm1_weight_temp_step_tol, S_IWUSR | S_IRUGO,
2218 show_weight_temp, store_weight_temp, 0, 1);
2219 static SENSOR_DEVICE_ATTR_2(pwm2_weight_temp_step_tol, S_IWUSR | S_IRUGO,
2220 show_weight_temp, store_weight_temp, 1, 1);
2221 static SENSOR_DEVICE_ATTR_2(pwm3_weight_temp_step_tol, S_IWUSR | S_IRUGO,
2222 show_weight_temp, store_weight_temp, 2, 1);
2223 static SENSOR_DEVICE_ATTR_2(pwm4_weight_temp_step_tol, S_IWUSR | S_IRUGO,
2224 show_weight_temp, store_weight_temp, 3, 1);
2225 static SENSOR_DEVICE_ATTR_2(pwm5_weight_temp_step_tol, S_IWUSR | S_IRUGO,
2226 show_weight_temp, store_weight_temp, 4, 1);
2228 static SENSOR_DEVICE_ATTR_2(pwm1_weight_temp_step_base, S_IWUSR | S_IRUGO,
2229 show_weight_temp, store_weight_temp, 0, 2);
2230 static SENSOR_DEVICE_ATTR_2(pwm2_weight_temp_step_base, S_IWUSR | S_IRUGO,
2231 show_weight_temp, store_weight_temp, 1, 2);
2232 static SENSOR_DEVICE_ATTR_2(pwm3_weight_temp_step_base, S_IWUSR | S_IRUGO,
2233 show_weight_temp, store_weight_temp, 2, 2);
2234 static SENSOR_DEVICE_ATTR_2(pwm4_weight_temp_step_base, S_IWUSR | S_IRUGO,
2235 show_weight_temp, store_weight_temp, 3, 2);
2236 static SENSOR_DEVICE_ATTR_2(pwm5_weight_temp_step_base, S_IWUSR | S_IRUGO,
2237 show_weight_temp, store_weight_temp, 4, 2);
2239 static SENSOR_DEVICE_ATTR_2(pwm1_weight_duty_step, S_IWUSR | S_IRUGO,
2240 show_pwm, store_pwm, 0, 5);
2241 static SENSOR_DEVICE_ATTR_2(pwm2_weight_duty_step, S_IWUSR | S_IRUGO,
2242 show_pwm, store_pwm, 1, 5);
2243 static SENSOR_DEVICE_ATTR_2(pwm3_weight_duty_step, S_IWUSR | S_IRUGO,
2244 show_pwm, store_pwm, 2, 5);
2245 static SENSOR_DEVICE_ATTR_2(pwm4_weight_duty_step, S_IWUSR | S_IRUGO,
2246 show_pwm, store_pwm, 3, 5);
2247 static SENSOR_DEVICE_ATTR_2(pwm5_weight_duty_step, S_IWUSR | S_IRUGO,
2248 show_pwm, store_pwm, 4, 5);
2250 /* duty_base is not supported on all chips */
2251 static struct sensor_device_attribute_2 sda_weight_duty_base[] = {
2252 SENSOR_ATTR_2(pwm1_weight_duty_base, S_IWUSR | S_IRUGO,
2253 show_pwm, store_pwm, 0, 6),
2254 SENSOR_ATTR_2(pwm2_weight_duty_base, S_IWUSR | S_IRUGO,
2255 show_pwm, store_pwm, 1, 6),
2256 SENSOR_ATTR_2(pwm3_weight_duty_base, S_IWUSR | S_IRUGO,
2257 show_pwm, store_pwm, 2, 6),
2258 SENSOR_ATTR_2(pwm4_weight_duty_base, S_IWUSR | S_IRUGO,
2259 show_pwm, store_pwm, 3, 6),
2260 SENSOR_ATTR_2(pwm5_weight_duty_base, S_IWUSR | S_IRUGO,
2261 show_pwm, store_pwm, 4, 6),
2265 show_fan_time(struct device *dev, struct device_attribute *attr, char *buf)
2267 struct nct6775_data *data = nct6775_update_device(dev);
2268 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
2270 int index = sattr->index;
2272 return sprintf(buf, "%d\n",
2273 step_time_from_reg(data->fan_time[index][nr],
2274 data->pwm_mode[nr]));
2278 store_fan_time(struct device *dev, struct device_attribute *attr,
2279 const char *buf, size_t count)
2281 struct nct6775_data *data = dev_get_drvdata(dev);
2282 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
2284 int index = sattr->index;
2288 err = kstrtoul(buf, 10, &val);
2292 val = step_time_to_reg(val, data->pwm_mode[nr]);
2293 mutex_lock(&data->update_lock);
2294 data->fan_time[index][nr] = val;
2295 nct6775_write_value(data, data->REG_FAN_TIME[index][nr], val);
2296 mutex_unlock(&data->update_lock);
2301 show_name(struct device *dev, struct device_attribute *attr, char *buf)
2303 struct nct6775_data *data = dev_get_drvdata(dev);
2305 return sprintf(buf, "%s\n", data->name);
2308 static DEVICE_ATTR(name, S_IRUGO, show_name, NULL);
2310 static SENSOR_DEVICE_ATTR_2(pwm1_stop_time, S_IWUSR | S_IRUGO, show_fan_time,
2311 store_fan_time, 0, 0);
2312 static SENSOR_DEVICE_ATTR_2(pwm2_stop_time, S_IWUSR | S_IRUGO, show_fan_time,
2313 store_fan_time, 1, 0);
2314 static SENSOR_DEVICE_ATTR_2(pwm3_stop_time, S_IWUSR | S_IRUGO, show_fan_time,
2315 store_fan_time, 2, 0);
2316 static SENSOR_DEVICE_ATTR_2(pwm4_stop_time, S_IWUSR | S_IRUGO, show_fan_time,
2317 store_fan_time, 3, 0);
2318 static SENSOR_DEVICE_ATTR_2(pwm5_stop_time, S_IWUSR | S_IRUGO, show_fan_time,
2319 store_fan_time, 4, 0);
2321 static SENSOR_DEVICE_ATTR_2(pwm1_step_up_time, S_IWUSR | S_IRUGO, show_fan_time,
2322 store_fan_time, 0, 1);
2323 static SENSOR_DEVICE_ATTR_2(pwm2_step_up_time, S_IWUSR | S_IRUGO, show_fan_time,
2324 store_fan_time, 1, 1);
2325 static SENSOR_DEVICE_ATTR_2(pwm3_step_up_time, S_IWUSR | S_IRUGO, show_fan_time,
2326 store_fan_time, 2, 1);
2327 static SENSOR_DEVICE_ATTR_2(pwm4_step_up_time, S_IWUSR | S_IRUGO, show_fan_time,
2328 store_fan_time, 3, 1);
2329 static SENSOR_DEVICE_ATTR_2(pwm5_step_up_time, S_IWUSR | S_IRUGO, show_fan_time,
2330 store_fan_time, 4, 1);
2332 static SENSOR_DEVICE_ATTR_2(pwm1_step_down_time, S_IWUSR | S_IRUGO,
2333 show_fan_time, store_fan_time, 0, 2);
2334 static SENSOR_DEVICE_ATTR_2(pwm2_step_down_time, S_IWUSR | S_IRUGO,
2335 show_fan_time, store_fan_time, 1, 2);
2336 static SENSOR_DEVICE_ATTR_2(pwm3_step_down_time, S_IWUSR | S_IRUGO,
2337 show_fan_time, store_fan_time, 2, 2);
2338 static SENSOR_DEVICE_ATTR_2(pwm4_step_down_time, S_IWUSR | S_IRUGO,
2339 show_fan_time, store_fan_time, 3, 2);
2340 static SENSOR_DEVICE_ATTR_2(pwm5_step_down_time, S_IWUSR | S_IRUGO,
2341 show_fan_time, store_fan_time, 4, 2);
2343 static SENSOR_DEVICE_ATTR_2(pwm1_start_output, S_IWUSR | S_IRUGO, show_pwm,
2345 static SENSOR_DEVICE_ATTR_2(pwm2_start_output, S_IWUSR | S_IRUGO, show_pwm,
2347 static SENSOR_DEVICE_ATTR_2(pwm3_start_output, S_IWUSR | S_IRUGO, show_pwm,
2349 static SENSOR_DEVICE_ATTR_2(pwm4_start_output, S_IWUSR | S_IRUGO, show_pwm,
2351 static SENSOR_DEVICE_ATTR_2(pwm5_start_output, S_IWUSR | S_IRUGO, show_pwm,
2354 static SENSOR_DEVICE_ATTR_2(pwm1_stop_output, S_IWUSR | S_IRUGO, show_pwm,
2356 static SENSOR_DEVICE_ATTR_2(pwm2_stop_output, S_IWUSR | S_IRUGO, show_pwm,
2358 static SENSOR_DEVICE_ATTR_2(pwm3_stop_output, S_IWUSR | S_IRUGO, show_pwm,
2360 static SENSOR_DEVICE_ATTR_2(pwm4_stop_output, S_IWUSR | S_IRUGO, show_pwm,
2362 static SENSOR_DEVICE_ATTR_2(pwm5_stop_output, S_IWUSR | S_IRUGO, show_pwm,
2365 /* max_output is not supported on all chips */
2366 static struct sensor_device_attribute_2 sda_max_output[] = {
2367 SENSOR_ATTR_2(pwm1_max_output, S_IWUSR | S_IRUGO, show_pwm, store_pwm,
2369 SENSOR_ATTR_2(pwm2_max_output, S_IWUSR | S_IRUGO, show_pwm, store_pwm,
2371 SENSOR_ATTR_2(pwm3_max_output, S_IWUSR | S_IRUGO, show_pwm, store_pwm,
2373 SENSOR_ATTR_2(pwm4_max_output, S_IWUSR | S_IRUGO, show_pwm, store_pwm,
2375 SENSOR_ATTR_2(pwm5_max_output, S_IWUSR | S_IRUGO, show_pwm, store_pwm,
2379 /* step_output is not supported on all chips */
2380 static struct sensor_device_attribute_2 sda_step_output[] = {
2381 SENSOR_ATTR_2(pwm1_step_output, S_IWUSR | S_IRUGO, show_pwm, store_pwm,
2383 SENSOR_ATTR_2(pwm2_step_output, S_IWUSR | S_IRUGO, show_pwm, store_pwm,
2385 SENSOR_ATTR_2(pwm3_step_output, S_IWUSR | S_IRUGO, show_pwm, store_pwm,
2387 SENSOR_ATTR_2(pwm4_step_output, S_IWUSR | S_IRUGO, show_pwm, store_pwm,
2389 SENSOR_ATTR_2(pwm5_step_output, S_IWUSR | S_IRUGO, show_pwm, store_pwm,
2393 static struct attribute *nct6775_attributes_pwm[5][19] = {
2395 &sensor_dev_attr_pwm1.dev_attr.attr,
2396 &sensor_dev_attr_pwm1_mode.dev_attr.attr,
2397 &sensor_dev_attr_pwm1_enable.dev_attr.attr,
2398 &sensor_dev_attr_pwm1_stop_output_enable.dev_attr.attr,
2399 &sensor_dev_attr_pwm1_weight_enable.dev_attr.attr,
2400 &sensor_dev_attr_pwm1_temp_sel.dev_attr.attr,
2401 &sensor_dev_attr_pwm1_target.dev_attr.attr,
2402 &sensor_dev_attr_pwm1_stop_time.dev_attr.attr,
2403 &sensor_dev_attr_pwm1_step_up_time.dev_attr.attr,
2404 &sensor_dev_attr_pwm1_step_down_time.dev_attr.attr,
2405 &sensor_dev_attr_pwm1_start_output.dev_attr.attr,
2406 &sensor_dev_attr_pwm1_stop_output.dev_attr.attr,
2407 &sensor_dev_attr_pwm1_weight_temp_sel.dev_attr.attr,
2408 &sensor_dev_attr_pwm1_weight_temp_step.dev_attr.attr,
2409 &sensor_dev_attr_pwm1_weight_temp_step_tol.dev_attr.attr,
2410 &sensor_dev_attr_pwm1_weight_temp_step_base.dev_attr.attr,
2411 &sensor_dev_attr_pwm1_weight_duty_step.dev_attr.attr,
2415 &sensor_dev_attr_pwm2.dev_attr.attr,
2416 &sensor_dev_attr_pwm2_mode.dev_attr.attr,
2417 &sensor_dev_attr_pwm2_enable.dev_attr.attr,
2418 &sensor_dev_attr_pwm2_stop_output_enable.dev_attr.attr,
2419 &sensor_dev_attr_pwm2_weight_enable.dev_attr.attr,
2420 &sensor_dev_attr_pwm2_temp_sel.dev_attr.attr,
2421 &sensor_dev_attr_pwm2_target.dev_attr.attr,
2422 &sensor_dev_attr_pwm2_stop_time.dev_attr.attr,
2423 &sensor_dev_attr_pwm2_step_up_time.dev_attr.attr,
2424 &sensor_dev_attr_pwm2_step_down_time.dev_attr.attr,
2425 &sensor_dev_attr_pwm2_start_output.dev_attr.attr,
2426 &sensor_dev_attr_pwm2_stop_output.dev_attr.attr,
2427 &sensor_dev_attr_pwm2_weight_temp_sel.dev_attr.attr,
2428 &sensor_dev_attr_pwm2_weight_temp_step.dev_attr.attr,
2429 &sensor_dev_attr_pwm2_weight_temp_step_tol.dev_attr.attr,
2430 &sensor_dev_attr_pwm2_weight_temp_step_base.dev_attr.attr,
2431 &sensor_dev_attr_pwm2_weight_duty_step.dev_attr.attr,
2435 &sensor_dev_attr_pwm3.dev_attr.attr,
2436 &sensor_dev_attr_pwm3_mode.dev_attr.attr,
2437 &sensor_dev_attr_pwm3_enable.dev_attr.attr,
2438 &sensor_dev_attr_pwm3_stop_output_enable.dev_attr.attr,
2439 &sensor_dev_attr_pwm3_weight_enable.dev_attr.attr,
2440 &sensor_dev_attr_pwm3_temp_sel.dev_attr.attr,
2441 &sensor_dev_attr_pwm3_target.dev_attr.attr,
2442 &sensor_dev_attr_pwm3_stop_time.dev_attr.attr,
2443 &sensor_dev_attr_pwm3_step_up_time.dev_attr.attr,
2444 &sensor_dev_attr_pwm3_step_down_time.dev_attr.attr,
2445 &sensor_dev_attr_pwm3_start_output.dev_attr.attr,
2446 &sensor_dev_attr_pwm3_stop_output.dev_attr.attr,
2447 &sensor_dev_attr_pwm3_weight_temp_sel.dev_attr.attr,
2448 &sensor_dev_attr_pwm3_weight_temp_step.dev_attr.attr,
2449 &sensor_dev_attr_pwm3_weight_temp_step_tol.dev_attr.attr,
2450 &sensor_dev_attr_pwm3_weight_temp_step_base.dev_attr.attr,
2451 &sensor_dev_attr_pwm3_weight_duty_step.dev_attr.attr,
2455 &sensor_dev_attr_pwm4.dev_attr.attr,
2456 &sensor_dev_attr_pwm4_mode.dev_attr.attr,
2457 &sensor_dev_attr_pwm4_enable.dev_attr.attr,
2458 &sensor_dev_attr_pwm4_stop_output_enable.dev_attr.attr,
2459 &sensor_dev_attr_pwm4_weight_enable.dev_attr.attr,
2460 &sensor_dev_attr_pwm4_temp_sel.dev_attr.attr,
2461 &sensor_dev_attr_pwm4_target.dev_attr.attr,
2462 &sensor_dev_attr_pwm4_stop_time.dev_attr.attr,
2463 &sensor_dev_attr_pwm4_step_up_time.dev_attr.attr,
2464 &sensor_dev_attr_pwm4_step_down_time.dev_attr.attr,
2465 &sensor_dev_attr_pwm4_start_output.dev_attr.attr,
2466 &sensor_dev_attr_pwm4_stop_output.dev_attr.attr,
2467 &sensor_dev_attr_pwm4_weight_temp_sel.dev_attr.attr,
2468 &sensor_dev_attr_pwm4_weight_temp_step.dev_attr.attr,
2469 &sensor_dev_attr_pwm4_weight_temp_step_tol.dev_attr.attr,
2470 &sensor_dev_attr_pwm4_weight_temp_step_base.dev_attr.attr,
2471 &sensor_dev_attr_pwm4_weight_duty_step.dev_attr.attr,
2475 &sensor_dev_attr_pwm5.dev_attr.attr,
2476 &sensor_dev_attr_pwm5_mode.dev_attr.attr,
2477 &sensor_dev_attr_pwm5_enable.dev_attr.attr,
2478 &sensor_dev_attr_pwm5_stop_output_enable.dev_attr.attr,
2479 &sensor_dev_attr_pwm5_weight_enable.dev_attr.attr,
2480 &sensor_dev_attr_pwm5_temp_sel.dev_attr.attr,
2481 &sensor_dev_attr_pwm5_target.dev_attr.attr,
2482 &sensor_dev_attr_pwm5_stop_time.dev_attr.attr,
2483 &sensor_dev_attr_pwm5_step_up_time.dev_attr.attr,
2484 &sensor_dev_attr_pwm5_step_down_time.dev_attr.attr,
2485 &sensor_dev_attr_pwm5_start_output.dev_attr.attr,
2486 &sensor_dev_attr_pwm5_stop_output.dev_attr.attr,
2487 &sensor_dev_attr_pwm5_weight_temp_sel.dev_attr.attr,
2488 &sensor_dev_attr_pwm5_weight_temp_step.dev_attr.attr,
2489 &sensor_dev_attr_pwm5_weight_temp_step_tol.dev_attr.attr,
2490 &sensor_dev_attr_pwm5_weight_temp_step_base.dev_attr.attr,
2491 &sensor_dev_attr_pwm5_weight_duty_step.dev_attr.attr,
2496 static const struct attribute_group nct6775_group_pwm[5] = {
2497 { .attrs = nct6775_attributes_pwm[0] },
2498 { .attrs = nct6775_attributes_pwm[1] },
2499 { .attrs = nct6775_attributes_pwm[2] },
2500 { .attrs = nct6775_attributes_pwm[3] },
2501 { .attrs = nct6775_attributes_pwm[4] },
2505 show_auto_pwm(struct device *dev, struct device_attribute *attr, char *buf)
2507 struct nct6775_data *data = nct6775_update_device(dev);
2508 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
2510 return sprintf(buf, "%d\n", data->auto_pwm[sattr->nr][sattr->index]);
2514 store_auto_pwm(struct device *dev, struct device_attribute *attr,
2515 const char *buf, size_t count)
2517 struct nct6775_data *data = dev_get_drvdata(dev);
2518 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
2520 int point = sattr->index;
2525 err = kstrtoul(buf, 10, &val);
2531 if (point == data->auto_pwm_num) {
2532 if (data->kind != nct6775 && !val)
2534 if (data->kind != nct6779 && val)
2538 mutex_lock(&data->update_lock);
2539 data->auto_pwm[nr][point] = val;
2540 if (point < data->auto_pwm_num) {
2541 nct6775_write_value(data,
2542 NCT6775_AUTO_PWM(data, nr, point),
2543 data->auto_pwm[nr][point]);
2545 switch (data->kind) {
2547 /* disable if needed (pwm == 0) */
2548 reg = nct6775_read_value(data,
2549 NCT6775_REG_CRITICAL_ENAB[nr]);
2554 nct6775_write_value(data, NCT6775_REG_CRITICAL_ENAB[nr],
2558 break; /* always enabled, nothing to do */
2560 nct6775_write_value(data, NCT6779_REG_CRITICAL_PWM[nr],
2562 reg = nct6775_read_value(data,
2563 NCT6779_REG_CRITICAL_PWM_ENABLE[nr]);
2568 nct6775_write_value(data,
2569 NCT6779_REG_CRITICAL_PWM_ENABLE[nr],
2574 mutex_unlock(&data->update_lock);
2579 show_auto_temp(struct device *dev, struct device_attribute *attr, char *buf)
2581 struct nct6775_data *data = nct6775_update_device(dev);
2582 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
2584 int point = sattr->index;
2587 * We don't know for sure if the temperature is signed or unsigned.
2588 * Assume it is unsigned.
2590 return sprintf(buf, "%d\n", data->auto_temp[nr][point] * 1000);
2594 store_auto_temp(struct device *dev, struct device_attribute *attr,
2595 const char *buf, size_t count)
2597 struct nct6775_data *data = dev_get_drvdata(dev);
2598 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
2600 int point = sattr->index;
2604 err = kstrtoul(buf, 10, &val);
2610 mutex_lock(&data->update_lock);
2611 data->auto_temp[nr][point] = DIV_ROUND_CLOSEST(val, 1000);
2612 if (point < data->auto_pwm_num) {
2613 nct6775_write_value(data,
2614 NCT6775_AUTO_TEMP(data, nr, point),
2615 data->auto_temp[nr][point]);
2617 nct6775_write_value(data, data->REG_CRITICAL_TEMP[nr],
2618 data->auto_temp[nr][point]);
2620 mutex_unlock(&data->update_lock);
2625 * The number of auto-point trip points is chip dependent.
2626 * Need to check support while generating/removing attribute files.
2628 static struct sensor_device_attribute_2 sda_auto_pwm_arrays[] = {
2629 SENSOR_ATTR_2(pwm1_auto_point1_pwm, S_IWUSR | S_IRUGO,
2630 show_auto_pwm, store_auto_pwm, 0, 0),
2631 SENSOR_ATTR_2(pwm1_auto_point1_temp, S_IWUSR | S_IRUGO,
2632 show_auto_temp, store_auto_temp, 0, 0),
2633 SENSOR_ATTR_2(pwm1_auto_point1_temp_hyst, S_IWUSR | S_IRUGO,
2634 show_auto_temp_hyst, store_auto_temp_hyst, 0, 0),
2635 SENSOR_ATTR_2(pwm1_auto_point2_pwm, S_IWUSR | S_IRUGO,
2636 show_auto_pwm, store_auto_pwm, 0, 1),
2637 SENSOR_ATTR_2(pwm1_auto_point2_temp, S_IWUSR | S_IRUGO,
2638 show_auto_temp, store_auto_temp, 0, 1),
2639 SENSOR_ATTR_2(pwm1_auto_point2_temp_hyst, S_IWUSR | S_IRUGO,
2640 show_auto_temp_hyst, store_auto_temp_hyst, 0, 1),
2641 SENSOR_ATTR_2(pwm1_auto_point3_pwm, S_IWUSR | S_IRUGO,
2642 show_auto_pwm, store_auto_pwm, 0, 2),
2643 SENSOR_ATTR_2(pwm1_auto_point3_temp, S_IWUSR | S_IRUGO,
2644 show_auto_temp, store_auto_temp, 0, 2),
2645 SENSOR_ATTR_2(pwm1_auto_point3_temp_hyst, S_IWUSR | S_IRUGO,
2646 show_auto_temp_hyst, store_auto_temp_hyst, 0, 2),
2647 SENSOR_ATTR_2(pwm1_auto_point4_pwm, S_IWUSR | S_IRUGO,
2648 show_auto_pwm, store_auto_pwm, 0, 3),
2649 SENSOR_ATTR_2(pwm1_auto_point4_temp, S_IWUSR | S_IRUGO,
2650 show_auto_temp, store_auto_temp, 0, 3),
2651 SENSOR_ATTR_2(pwm1_auto_point4_temp_hyst, S_IWUSR | S_IRUGO,
2652 show_auto_temp_hyst, store_auto_temp_hyst, 0, 3),
2653 SENSOR_ATTR_2(pwm1_auto_point5_pwm, S_IWUSR | S_IRUGO,
2654 show_auto_pwm, store_auto_pwm, 0, 4),
2655 SENSOR_ATTR_2(pwm1_auto_point5_temp, S_IWUSR | S_IRUGO,
2656 show_auto_temp, store_auto_temp, 0, 4),
2657 SENSOR_ATTR_2(pwm1_auto_point5_temp_hyst, S_IWUSR | S_IRUGO,
2658 show_auto_temp_hyst, store_auto_temp_hyst, 0, 4),
2659 SENSOR_ATTR_2(pwm1_auto_point6_pwm, S_IWUSR | S_IRUGO,
2660 show_auto_pwm, store_auto_pwm, 0, 5),
2661 SENSOR_ATTR_2(pwm1_auto_point6_temp, S_IWUSR | S_IRUGO,
2662 show_auto_temp, store_auto_temp, 0, 5),
2663 SENSOR_ATTR_2(pwm1_auto_point6_temp_hyst, S_IWUSR | S_IRUGO,
2664 show_auto_temp_hyst, store_auto_temp_hyst, 0, 5),
2665 SENSOR_ATTR_2(pwm1_auto_point7_pwm, S_IWUSR | S_IRUGO,
2666 show_auto_pwm, store_auto_pwm, 0, 6),
2667 SENSOR_ATTR_2(pwm1_auto_point7_temp, S_IWUSR | S_IRUGO,
2668 show_auto_temp, store_auto_temp, 0, 6),
2669 SENSOR_ATTR_2(pwm1_auto_point7_temp_hyst, S_IWUSR | S_IRUGO,
2670 show_auto_temp_hyst, store_auto_temp_hyst, 0, 6),
2672 SENSOR_ATTR_2(pwm2_auto_point1_pwm, S_IWUSR | S_IRUGO,
2673 show_auto_pwm, store_auto_pwm, 1, 0),
2674 SENSOR_ATTR_2(pwm2_auto_point1_temp, S_IWUSR | S_IRUGO,
2675 show_auto_temp, store_auto_temp, 1, 0),
2676 SENSOR_ATTR_2(pwm2_auto_point1_temp_hyst, S_IWUSR | S_IRUGO,
2677 show_auto_temp_hyst, store_auto_temp_hyst, 1, 0),
2678 SENSOR_ATTR_2(pwm2_auto_point2_pwm, S_IWUSR | S_IRUGO,
2679 show_auto_pwm, store_auto_pwm, 1, 1),
2680 SENSOR_ATTR_2(pwm2_auto_point2_temp, S_IWUSR | S_IRUGO,
2681 show_auto_temp, store_auto_temp, 1, 1),
2682 SENSOR_ATTR_2(pwm2_auto_point2_temp_hyst, S_IWUSR | S_IRUGO,
2683 show_auto_temp_hyst, store_auto_temp_hyst, 1, 1),
2684 SENSOR_ATTR_2(pwm2_auto_point3_pwm, S_IWUSR | S_IRUGO,
2685 show_auto_pwm, store_auto_pwm, 1, 2),
2686 SENSOR_ATTR_2(pwm2_auto_point3_temp, S_IWUSR | S_IRUGO,
2687 show_auto_temp, store_auto_temp, 1, 2),
2688 SENSOR_ATTR_2(pwm2_auto_point3_temp_hyst, S_IWUSR | S_IRUGO,
2689 show_auto_temp_hyst, store_auto_temp_hyst, 1, 2),
2690 SENSOR_ATTR_2(pwm2_auto_point4_pwm, S_IWUSR | S_IRUGO,
2691 show_auto_pwm, store_auto_pwm, 1, 3),
2692 SENSOR_ATTR_2(pwm2_auto_point4_temp, S_IWUSR | S_IRUGO,
2693 show_auto_temp, store_auto_temp, 1, 3),
2694 SENSOR_ATTR_2(pwm2_auto_point4_temp_hyst, S_IWUSR | S_IRUGO,
2695 show_auto_temp_hyst, store_auto_temp_hyst, 1, 3),
2696 SENSOR_ATTR_2(pwm2_auto_point5_pwm, S_IWUSR | S_IRUGO,
2697 show_auto_pwm, store_auto_pwm, 1, 4),
2698 SENSOR_ATTR_2(pwm2_auto_point5_temp, S_IWUSR | S_IRUGO,
2699 show_auto_temp, store_auto_temp, 1, 4),
2700 SENSOR_ATTR_2(pwm2_auto_point5_temp_hyst, S_IWUSR | S_IRUGO,
2701 show_auto_temp_hyst, store_auto_temp_hyst, 1, 4),
2702 SENSOR_ATTR_2(pwm2_auto_point6_pwm, S_IWUSR | S_IRUGO,
2703 show_auto_pwm, store_auto_pwm, 1, 5),
2704 SENSOR_ATTR_2(pwm2_auto_point6_temp, S_IWUSR | S_IRUGO,
2705 show_auto_temp, store_auto_temp, 1, 5),
2706 SENSOR_ATTR_2(pwm2_auto_point6_temp_hyst, S_IWUSR | S_IRUGO,
2707 show_auto_temp_hyst, store_auto_temp_hyst, 1, 5),
2708 SENSOR_ATTR_2(pwm2_auto_point7_pwm, S_IWUSR | S_IRUGO,
2709 show_auto_pwm, store_auto_pwm, 1, 6),
2710 SENSOR_ATTR_2(pwm2_auto_point7_temp, S_IWUSR | S_IRUGO,
2711 show_auto_temp, store_auto_temp, 1, 6),
2712 SENSOR_ATTR_2(pwm2_auto_point7_temp_hyst, S_IWUSR | S_IRUGO,
2713 show_auto_temp_hyst, store_auto_temp_hyst, 1, 6),
2715 SENSOR_ATTR_2(pwm3_auto_point1_pwm, S_IWUSR | S_IRUGO,
2716 show_auto_pwm, store_auto_pwm, 2, 0),
2717 SENSOR_ATTR_2(pwm3_auto_point1_temp, S_IWUSR | S_IRUGO,
2718 show_auto_temp, store_auto_temp, 2, 0),
2719 SENSOR_ATTR_2(pwm3_auto_point1_temp_hyst, S_IWUSR | S_IRUGO,
2720 show_auto_temp_hyst, store_auto_temp_hyst, 2, 0),
2721 SENSOR_ATTR_2(pwm3_auto_point2_pwm, S_IWUSR | S_IRUGO,
2722 show_auto_pwm, store_auto_pwm, 2, 1),
2723 SENSOR_ATTR_2(pwm3_auto_point2_temp, S_IWUSR | S_IRUGO,
2724 show_auto_temp, store_auto_temp, 2, 1),
2725 SENSOR_ATTR_2(pwm3_auto_point2_temp_hyst, S_IWUSR | S_IRUGO,
2726 show_auto_temp_hyst, store_auto_temp_hyst, 2, 1),
2727 SENSOR_ATTR_2(pwm3_auto_point3_pwm, S_IWUSR | S_IRUGO,
2728 show_auto_pwm, store_auto_pwm, 2, 2),
2729 SENSOR_ATTR_2(pwm3_auto_point3_temp, S_IWUSR | S_IRUGO,
2730 show_auto_temp, store_auto_temp, 2, 2),
2731 SENSOR_ATTR_2(pwm3_auto_point3_temp_hyst, S_IWUSR | S_IRUGO,
2732 show_auto_temp_hyst, store_auto_temp_hyst, 2, 2),
2733 SENSOR_ATTR_2(pwm3_auto_point4_pwm, S_IWUSR | S_IRUGO,
2734 show_auto_pwm, store_auto_pwm, 2, 3),
2735 SENSOR_ATTR_2(pwm3_auto_point4_temp, S_IWUSR | S_IRUGO,
2736 show_auto_temp, store_auto_temp, 2, 3),
2737 SENSOR_ATTR_2(pwm3_auto_point4_temp_hyst, S_IWUSR | S_IRUGO,
2738 show_auto_temp_hyst, store_auto_temp_hyst, 2, 3),
2739 SENSOR_ATTR_2(pwm3_auto_point5_pwm, S_IWUSR | S_IRUGO,
2740 show_auto_pwm, store_auto_pwm, 2, 4),
2741 SENSOR_ATTR_2(pwm3_auto_point5_temp, S_IWUSR | S_IRUGO,
2742 show_auto_temp, store_auto_temp, 2, 4),
2743 SENSOR_ATTR_2(pwm3_auto_point5_temp_hyst, S_IWUSR | S_IRUGO,
2744 show_auto_temp_hyst, store_auto_temp_hyst, 2, 4),
2745 SENSOR_ATTR_2(pwm3_auto_point6_pwm, S_IWUSR | S_IRUGO,
2746 show_auto_pwm, store_auto_pwm, 2, 5),
2747 SENSOR_ATTR_2(pwm3_auto_point6_temp, S_IWUSR | S_IRUGO,
2748 show_auto_temp, store_auto_temp, 2, 5),
2749 SENSOR_ATTR_2(pwm3_auto_point6_temp_hyst, S_IWUSR | S_IRUGO,
2750 show_auto_temp_hyst, store_auto_temp_hyst, 2, 5),
2751 SENSOR_ATTR_2(pwm3_auto_point7_pwm, S_IWUSR | S_IRUGO,
2752 show_auto_pwm, store_auto_pwm, 2, 6),
2753 SENSOR_ATTR_2(pwm3_auto_point7_temp, S_IWUSR | S_IRUGO,
2754 show_auto_temp, store_auto_temp, 2, 6),
2755 SENSOR_ATTR_2(pwm3_auto_point7_temp_hyst, S_IWUSR | S_IRUGO,
2756 show_auto_temp_hyst, store_auto_temp_hyst, 2, 6),
2758 SENSOR_ATTR_2(pwm4_auto_point1_pwm, S_IWUSR | S_IRUGO,
2759 show_auto_pwm, store_auto_pwm, 3, 0),
2760 SENSOR_ATTR_2(pwm4_auto_point1_temp, S_IWUSR | S_IRUGO,
2761 show_auto_temp, store_auto_temp, 3, 0),
2762 SENSOR_ATTR_2(pwm4_auto_point1_temp_hyst, S_IWUSR | S_IRUGO,
2763 show_auto_temp_hyst, store_auto_temp_hyst, 3, 0),
2764 SENSOR_ATTR_2(pwm4_auto_point2_pwm, S_IWUSR | S_IRUGO,
2765 show_auto_pwm, store_auto_pwm, 3, 1),
2766 SENSOR_ATTR_2(pwm4_auto_point2_temp, S_IWUSR | S_IRUGO,
2767 show_auto_temp, store_auto_temp, 3, 1),
2768 SENSOR_ATTR_2(pwm4_auto_point2_temp_hyst, S_IWUSR | S_IRUGO,
2769 show_auto_temp_hyst, store_auto_temp_hyst, 3, 1),
2770 SENSOR_ATTR_2(pwm4_auto_point3_pwm, S_IWUSR | S_IRUGO,
2771 show_auto_pwm, store_auto_pwm, 3, 2),
2772 SENSOR_ATTR_2(pwm4_auto_point3_temp, S_IWUSR | S_IRUGO,
2773 show_auto_temp, store_auto_temp, 3, 2),
2774 SENSOR_ATTR_2(pwm4_auto_point3_temp_hyst, S_IWUSR | S_IRUGO,
2775 show_auto_temp_hyst, store_auto_temp_hyst, 3, 2),
2776 SENSOR_ATTR_2(pwm4_auto_point4_pwm, S_IWUSR | S_IRUGO,
2777 show_auto_pwm, store_auto_pwm, 3, 3),
2778 SENSOR_ATTR_2(pwm4_auto_point4_temp, S_IWUSR | S_IRUGO,
2779 show_auto_temp, store_auto_temp, 3, 3),
2780 SENSOR_ATTR_2(pwm4_auto_point4_temp_hyst, S_IWUSR | S_IRUGO,
2781 show_auto_temp_hyst, store_auto_temp_hyst, 3, 3),
2782 SENSOR_ATTR_2(pwm4_auto_point5_pwm, S_IWUSR | S_IRUGO,
2783 show_auto_pwm, store_auto_pwm, 3, 4),
2784 SENSOR_ATTR_2(pwm4_auto_point5_temp, S_IWUSR | S_IRUGO,
2785 show_auto_temp, store_auto_temp, 3, 4),
2786 SENSOR_ATTR_2(pwm4_auto_point5_temp_hyst, S_IWUSR | S_IRUGO,
2787 show_auto_temp_hyst, store_auto_temp_hyst, 3, 4),
2788 SENSOR_ATTR_2(pwm4_auto_point6_pwm, S_IWUSR | S_IRUGO,
2789 show_auto_pwm, store_auto_pwm, 3, 5),
2790 SENSOR_ATTR_2(pwm4_auto_point6_temp, S_IWUSR | S_IRUGO,
2791 show_auto_temp, store_auto_temp, 3, 5),
2792 SENSOR_ATTR_2(pwm4_auto_point6_temp_hyst, S_IWUSR | S_IRUGO,
2793 show_auto_temp_hyst, store_auto_temp_hyst, 3, 5),
2794 SENSOR_ATTR_2(pwm4_auto_point7_pwm, S_IWUSR | S_IRUGO,
2795 show_auto_pwm, store_auto_pwm, 3, 6),
2796 SENSOR_ATTR_2(pwm4_auto_point7_temp, S_IWUSR | S_IRUGO,
2797 show_auto_temp, store_auto_temp, 3, 6),
2798 SENSOR_ATTR_2(pwm4_auto_point7_temp_hyst, S_IWUSR | S_IRUGO,
2799 show_auto_temp_hyst, store_auto_temp_hyst, 3, 6),
2801 SENSOR_ATTR_2(pwm5_auto_point1_pwm, S_IWUSR | S_IRUGO,
2802 show_auto_pwm, store_auto_pwm, 4, 0),
2803 SENSOR_ATTR_2(pwm5_auto_point1_temp, S_IWUSR | S_IRUGO,
2804 show_auto_temp, store_auto_temp, 4, 0),
2805 SENSOR_ATTR_2(pwm5_auto_point1_temp_hyst, S_IWUSR | S_IRUGO,
2806 show_auto_temp_hyst, store_auto_temp_hyst, 4, 0),
2807 SENSOR_ATTR_2(pwm5_auto_point2_pwm, S_IWUSR | S_IRUGO,
2808 show_auto_pwm, store_auto_pwm, 4, 1),
2809 SENSOR_ATTR_2(pwm5_auto_point2_temp, S_IWUSR | S_IRUGO,
2810 show_auto_temp, store_auto_temp, 4, 1),
2811 SENSOR_ATTR_2(pwm5_auto_point2_temp_hyst, S_IWUSR | S_IRUGO,
2812 show_auto_temp_hyst, store_auto_temp_hyst, 4, 1),
2813 SENSOR_ATTR_2(pwm5_auto_point3_pwm, S_IWUSR | S_IRUGO,
2814 show_auto_pwm, store_auto_pwm, 4, 2),
2815 SENSOR_ATTR_2(pwm5_auto_point3_temp, S_IWUSR | S_IRUGO,
2816 show_auto_temp, store_auto_temp, 4, 2),
2817 SENSOR_ATTR_2(pwm5_auto_point3_temp_hyst, S_IWUSR | S_IRUGO,
2818 show_auto_temp_hyst, store_auto_temp_hyst, 4, 2),
2819 SENSOR_ATTR_2(pwm5_auto_point4_pwm, S_IWUSR | S_IRUGO,
2820 show_auto_pwm, store_auto_pwm, 4, 3),
2821 SENSOR_ATTR_2(pwm5_auto_point4_temp, S_IWUSR | S_IRUGO,
2822 show_auto_temp, store_auto_temp, 4, 3),
2823 SENSOR_ATTR_2(pwm5_auto_point4_temp_hyst, S_IWUSR | S_IRUGO,
2824 show_auto_temp_hyst, store_auto_temp_hyst, 4, 3),
2825 SENSOR_ATTR_2(pwm5_auto_point5_pwm, S_IWUSR | S_IRUGO,
2826 show_auto_pwm, store_auto_pwm, 4, 4),
2827 SENSOR_ATTR_2(pwm5_auto_point5_temp, S_IWUSR | S_IRUGO,
2828 show_auto_temp, store_auto_temp, 4, 4),
2829 SENSOR_ATTR_2(pwm5_auto_point5_temp_hyst, S_IWUSR | S_IRUGO,
2830 show_auto_temp_hyst, store_auto_temp_hyst, 4, 4),
2831 SENSOR_ATTR_2(pwm5_auto_point6_pwm, S_IWUSR | S_IRUGO,
2832 show_auto_pwm, store_auto_pwm, 4, 5),
2833 SENSOR_ATTR_2(pwm5_auto_point6_temp, S_IWUSR | S_IRUGO,
2834 show_auto_temp, store_auto_temp, 4, 5),
2835 SENSOR_ATTR_2(pwm5_auto_point6_temp_hyst, S_IWUSR | S_IRUGO,
2836 show_auto_temp_hyst, store_auto_temp_hyst, 4, 5),
2837 SENSOR_ATTR_2(pwm5_auto_point7_pwm, S_IWUSR | S_IRUGO,
2838 show_auto_pwm, store_auto_pwm, 4, 6),
2839 SENSOR_ATTR_2(pwm5_auto_point7_temp, S_IWUSR | S_IRUGO,
2840 show_auto_temp, store_auto_temp, 4, 6),
2841 SENSOR_ATTR_2(pwm5_auto_point7_temp_hyst, S_IWUSR | S_IRUGO,
2842 show_auto_temp_hyst, store_auto_temp_hyst, 4, 6),
2846 show_vid(struct device *dev, struct device_attribute *attr, char *buf)
2848 struct nct6775_data *data = dev_get_drvdata(dev);
2849 return sprintf(buf, "%d\n", vid_from_reg(data->vid, data->vrm));
2852 static DEVICE_ATTR(cpu0_vid, S_IRUGO, show_vid, NULL);
2854 /* Case open detection */
2857 clear_caseopen(struct device *dev, struct device_attribute *attr,
2858 const char *buf, size_t count)
2860 struct nct6775_data *data = dev_get_drvdata(dev);
2861 struct nct6775_sio_data *sio_data = dev->platform_data;
2862 int nr = to_sensor_dev_attr(attr)->index - INTRUSION_ALARM_BASE;
2866 if (kstrtoul(buf, 10, &val) || val != 0)
2869 mutex_lock(&data->update_lock);
2872 * Use CR registers to clear caseopen status.
2873 * The CR registers are the same for all chips, and not all chips
2874 * support clearing the caseopen status through "regular" registers.
2876 superio_enter(sio_data->sioreg);
2877 superio_select(sio_data->sioreg, NCT6775_LD_ACPI);
2878 reg = superio_inb(sio_data->sioreg, NCT6775_REG_CR_CASEOPEN_CLR[nr]);
2879 reg |= NCT6775_CR_CASEOPEN_CLR_MASK[nr];
2880 superio_outb(sio_data->sioreg, NCT6775_REG_CR_CASEOPEN_CLR[nr], reg);
2881 reg &= ~NCT6775_CR_CASEOPEN_CLR_MASK[nr];
2882 superio_outb(sio_data->sioreg, NCT6775_REG_CR_CASEOPEN_CLR[nr], reg);
2883 superio_exit(sio_data->sioreg);
2885 data->valid = 0; /* Force cache refresh */
2887 mutex_unlock(&data->update_lock);
2892 static struct sensor_device_attribute sda_caseopen[] = {
2893 SENSOR_ATTR(intrusion0_alarm, S_IWUSR | S_IRUGO, show_alarm,
2894 clear_caseopen, INTRUSION_ALARM_BASE),
2895 SENSOR_ATTR(intrusion1_alarm, S_IWUSR | S_IRUGO, show_alarm,
2896 clear_caseopen, INTRUSION_ALARM_BASE + 1),
2900 * Driver and device management
2903 static void nct6775_device_remove_files(struct device *dev)
2906 * some entries in the following arrays may not have been used in
2907 * device_create_file(), but device_remove_file() will ignore them
2910 struct nct6775_data *data = dev_get_drvdata(dev);
2912 for (i = 0; i < data->pwm_num; i++)
2913 sysfs_remove_group(&dev->kobj, &nct6775_group_pwm[i]);
2915 for (i = 0; i < ARRAY_SIZE(sda_max_output); i++)
2916 device_remove_file(dev, &sda_max_output[i].dev_attr);
2918 for (i = 0; i < ARRAY_SIZE(sda_step_output); i++)
2919 device_remove_file(dev, &sda_max_output[i].dev_attr);
2921 for (i = 0; i < ARRAY_SIZE(sda_weight_duty_base); i++)
2922 device_remove_file(dev, &sda_weight_duty_base[i].dev_attr);
2924 for (i = 0; i < ARRAY_SIZE(sda_auto_pwm_arrays); i++)
2925 device_remove_file(dev, &sda_auto_pwm_arrays[i].dev_attr);
2927 for (i = 0; i < data->in_num; i++)
2928 sysfs_remove_group(&dev->kobj, &nct6775_group_in[i]);
2930 for (i = 0; i < 5; i++) {
2931 device_remove_file(dev, &sda_fan_input[i].dev_attr);
2932 device_remove_file(dev, &sda_fan_alarm[i].dev_attr);
2933 device_remove_file(dev, &sda_fan_div[i].dev_attr);
2934 device_remove_file(dev, &sda_fan_min[i].dev_attr);
2936 for (i = 0; i < NUM_TEMP; i++) {
2937 if (!(data->have_temp & (1 << i)))
2939 device_remove_file(dev, &sda_temp_input[i].dev_attr);
2940 device_remove_file(dev, &sda_temp_label[i].dev_attr);
2941 device_remove_file(dev, &sda_temp_max[i].dev_attr);
2942 device_remove_file(dev, &sda_temp_max_hyst[i].dev_attr);
2943 device_remove_file(dev, &sda_temp_crit[i].dev_attr);
2944 if (!(data->have_temp_fixed & (1 << i)))
2946 device_remove_file(dev, &sda_temp_type[i].dev_attr);
2947 device_remove_file(dev, &sda_temp_offset[i].dev_attr);
2948 if (i >= NUM_TEMP_ALARM)
2950 device_remove_file(dev, &sda_temp_alarm[i].dev_attr);
2953 device_remove_file(dev, &sda_caseopen[0].dev_attr);
2954 device_remove_file(dev, &sda_caseopen[1].dev_attr);
2956 device_remove_file(dev, &dev_attr_name);
2957 device_remove_file(dev, &dev_attr_cpu0_vid);
2960 /* Get the monitoring functions started */
2961 static inline void __devinit nct6775_init_device(struct nct6775_data *data)
2966 /* Start monitoring if needed */
2967 if (data->REG_CONFIG) {
2968 tmp = nct6775_read_value(data, data->REG_CONFIG);
2970 nct6775_write_value(data, data->REG_CONFIG, tmp | 0x01);
2973 /* Enable temperature sensors if needed */
2974 for (i = 0; i < NUM_TEMP; i++) {
2975 if (!(data->have_temp & (1 << i)))
2977 if (!data->reg_temp_config[i])
2979 tmp = nct6775_read_value(data, data->reg_temp_config[i]);
2981 nct6775_write_value(data, data->reg_temp_config[i],
2985 /* Enable VBAT monitoring if needed */
2986 tmp = nct6775_read_value(data, data->REG_VBAT);
2988 nct6775_write_value(data, data->REG_VBAT, tmp | 0x01);
2990 diode = nct6775_read_value(data, data->REG_DIODE);
2992 for (i = 0; i < data->temp_fixed_num; i++) {
2993 if (!(data->have_temp_fixed & (1 << i)))
2995 if ((tmp & (0x02 << i))) /* diode */
2996 data->temp_type[i] = 3 - ((diode >> i) & 0x02);
2997 else /* thermistor */
2998 data->temp_type[i] = 4;
3002 static void __devinit
3003 nct6775_check_fan_inputs(const struct nct6775_sio_data *sio_data,
3004 struct nct6775_data *data)
3007 bool fan3pin, fan3min, fan4pin, fan4min, fan5pin;
3008 bool pwm3pin, pwm4pin, pwm5pin;
3010 superio_enter(sio_data->sioreg);
3012 /* fan4 and fan5 share some pins with the GPIO and serial flash */
3013 if (data->kind == nct6775) {
3014 regval = superio_inb(sio_data->sioreg, 0x2c);
3016 fan3pin = regval & (1 << 6);
3018 pwm3pin = regval & (1 << 7);
3020 /* On NCT6775, fan4 shares pins with the fdc interface */
3021 fan4pin = !(superio_inb(sio_data->sioreg, 0x2A) & 0x80);
3026 } else if (data->kind == nct6776) {
3027 bool gpok = superio_inb(sio_data->sioreg, 0x27) & 0x80;
3029 superio_select(sio_data->sioreg, NCT6775_LD_HWM);
3030 regval = superio_inb(sio_data->sioreg, SIO_REG_ENABLE);
3035 fan3pin = !(superio_inb(sio_data->sioreg, 0x24) & 0x40);
3040 fan4pin = superio_inb(sio_data->sioreg, 0x1C) & 0x01;
3045 fan5pin = superio_inb(sio_data->sioreg, 0x1C) & 0x02;
3052 } else { /* NCT6779D */
3053 regval = superio_inb(sio_data->sioreg, 0x1c);
3055 fan3pin = !(regval & (1 << 5));
3056 fan4pin = !(regval & (1 << 6));
3057 fan5pin = !(regval & (1 << 7));
3059 pwm3pin = !(regval & (1 << 0));
3060 pwm4pin = !(regval & (1 << 1));
3061 pwm5pin = !(regval & (1 << 2));
3067 superio_exit(sio_data->sioreg);
3069 data->has_fan = data->has_fan_min = 0x03; /* fan1 and fan2 */
3070 data->has_fan |= fan3pin << 2;
3071 data->has_fan_min |= fan3min << 2;
3073 data->has_fan |= (fan4pin << 3) | (fan5pin << 4);
3074 data->has_fan_min |= (fan4min << 3) | (fan5pin << 4);
3076 data->has_pwm = 0x03 | (pwm3pin << 2) | (pwm4pin << 3) | (pwm5pin << 4);
3079 static int __devinit nct6775_probe(struct platform_device *pdev)
3081 struct device *dev = &pdev->dev;
3082 struct nct6775_sio_data *sio_data = dev->platform_data;
3083 struct nct6775_data *data;
3084 struct resource *res;
3086 int src, mask, available;
3087 const u16 *reg_temp, *reg_temp_over, *reg_temp_hyst, *reg_temp_config;
3088 const u16 *reg_temp_alternate, *reg_temp_crit;
3091 res = platform_get_resource(pdev, IORESOURCE_IO, 0);
3092 if (!devm_request_region(&pdev->dev, res->start, IOREGION_LENGTH,
3096 data = devm_kzalloc(&pdev->dev, sizeof(struct nct6775_data),
3101 data->kind = sio_data->kind;
3102 data->addr = res->start;
3103 mutex_init(&data->lock);
3104 mutex_init(&data->update_lock);
3105 data->name = nct6775_device_names[data->kind];
3106 platform_set_drvdata(pdev, data);
3108 switch (data->kind) {
3112 data->auto_pwm_num = 6;
3113 data->has_fan_div = true;
3114 data->temp_fixed_num = 3;
3116 data->ALARM_BITS = NCT6775_ALARM_BITS;
3118 data->fan_from_reg = fan_from_reg16;
3119 data->fan_from_reg_min = fan_from_reg8;
3121 data->temp_label = nct6775_temp_label;
3122 data->temp_label_num = ARRAY_SIZE(nct6775_temp_label);
3124 data->REG_CONFIG = NCT6775_REG_CONFIG;
3125 data->REG_VBAT = NCT6775_REG_VBAT;
3126 data->REG_DIODE = NCT6775_REG_DIODE;
3127 data->REG_VIN = NCT6775_REG_IN;
3128 data->REG_IN_MINMAX[0] = NCT6775_REG_IN_MIN;
3129 data->REG_IN_MINMAX[1] = NCT6775_REG_IN_MAX;
3130 data->REG_TARGET = NCT6775_REG_TARGET;
3131 data->REG_FAN = NCT6775_REG_FAN;
3132 data->REG_FAN_MODE = NCT6775_REG_FAN_MODE;
3133 data->REG_FAN_MIN = NCT6775_REG_FAN_MIN;
3134 data->REG_FAN_TIME[0] = NCT6775_REG_FAN_STOP_TIME;
3135 data->REG_FAN_TIME[1] = NCT6775_REG_FAN_STEP_UP_TIME;
3136 data->REG_FAN_TIME[2] = NCT6775_REG_FAN_STEP_DOWN_TIME;
3137 data->REG_PWM[0] = NCT6775_REG_PWM;
3138 data->REG_PWM[1] = NCT6775_REG_FAN_START_OUTPUT;
3139 data->REG_PWM[2] = NCT6775_REG_FAN_STOP_OUTPUT;
3140 data->REG_PWM[3] = NCT6775_REG_FAN_MAX_OUTPUT;
3141 data->REG_PWM[4] = NCT6775_REG_FAN_STEP_OUTPUT;
3142 data->REG_PWM[5] = NCT6775_REG_WEIGHT_DUTY_STEP;
3143 data->REG_PWM_READ = NCT6775_REG_PWM_READ;
3144 data->REG_PWM_MODE = NCT6775_REG_PWM_MODE;
3145 data->PWM_MODE_MASK = NCT6775_PWM_MODE_MASK;
3146 data->REG_TEMP_MON = NCT6775_REG_TEMP_MON;
3147 data->REG_AUTO_TEMP = NCT6775_REG_AUTO_TEMP;
3148 data->REG_AUTO_PWM = NCT6775_REG_AUTO_PWM;
3149 data->REG_CRITICAL_TEMP = NCT6775_REG_CRITICAL_TEMP;
3150 data->REG_CRITICAL_TEMP_TOLERANCE
3151 = NCT6775_REG_CRITICAL_TEMP_TOLERANCE;
3152 data->REG_TEMP_OFFSET = NCT6775_REG_TEMP_OFFSET;
3153 data->REG_TEMP_SOURCE = NCT6775_REG_TEMP_SOURCE;
3154 data->REG_TEMP_SEL[0] = NCT6775_REG_TEMP_SEL;
3155 data->REG_TEMP_SEL[1] = NCT6775_REG_WEIGHT_TEMP_SEL;
3156 data->REG_WEIGHT_TEMP[0] = NCT6775_REG_WEIGHT_TEMP_STEP;
3157 data->REG_WEIGHT_TEMP[1] = NCT6775_REG_WEIGHT_TEMP_STEP_TOL;
3158 data->REG_WEIGHT_TEMP[2] = NCT6775_REG_WEIGHT_TEMP_BASE;
3159 data->REG_ALARM = NCT6775_REG_ALARM;
3161 reg_temp = NCT6775_REG_TEMP;
3162 num_reg_temp = ARRAY_SIZE(NCT6775_REG_TEMP);
3163 reg_temp_over = NCT6775_REG_TEMP_OVER;
3164 reg_temp_hyst = NCT6775_REG_TEMP_HYST;
3165 reg_temp_config = NCT6775_REG_TEMP_CONFIG;
3166 reg_temp_alternate = NCT6775_REG_TEMP_ALTERNATE;
3167 reg_temp_crit = NCT6775_REG_TEMP_CRIT;
3173 data->auto_pwm_num = 4;
3174 data->has_fan_div = false;
3175 data->temp_fixed_num = 3;
3177 data->ALARM_BITS = NCT6776_ALARM_BITS;
3179 data->fan_from_reg = fan_from_reg13;
3180 data->fan_from_reg_min = fan_from_reg13;
3182 data->temp_label = nct6776_temp_label;
3183 data->temp_label_num = ARRAY_SIZE(nct6776_temp_label);
3185 data->REG_CONFIG = NCT6775_REG_CONFIG;
3186 data->REG_VBAT = NCT6775_REG_VBAT;
3187 data->REG_DIODE = NCT6775_REG_DIODE;
3188 data->REG_VIN = NCT6775_REG_IN;
3189 data->REG_IN_MINMAX[0] = NCT6775_REG_IN_MIN;
3190 data->REG_IN_MINMAX[1] = NCT6775_REG_IN_MAX;
3191 data->REG_TARGET = NCT6775_REG_TARGET;
3192 data->REG_FAN = NCT6775_REG_FAN;
3193 data->REG_FAN_MODE = NCT6775_REG_FAN_MODE;
3194 data->REG_FAN_MIN = NCT6776_REG_FAN_MIN;
3195 data->REG_FAN_TIME[0] = NCT6775_REG_FAN_STOP_TIME;
3196 data->REG_FAN_TIME[1] = NCT6775_REG_FAN_STEP_UP_TIME;
3197 data->REG_FAN_TIME[2] = NCT6775_REG_FAN_STEP_DOWN_TIME;
3198 data->REG_PWM[0] = NCT6775_REG_PWM;
3199 data->REG_PWM[1] = NCT6775_REG_FAN_START_OUTPUT;
3200 data->REG_PWM[2] = NCT6775_REG_FAN_STOP_OUTPUT;
3201 data->REG_PWM[5] = NCT6775_REG_WEIGHT_DUTY_STEP;
3202 data->REG_PWM[6] = NCT6776_REG_WEIGHT_DUTY_BASE;
3203 data->REG_PWM_READ = NCT6775_REG_PWM_READ;
3204 data->REG_PWM_MODE = NCT6776_REG_PWM_MODE;
3205 data->PWM_MODE_MASK = NCT6776_PWM_MODE_MASK;
3206 data->REG_TEMP_MON = NCT6775_REG_TEMP_MON;
3207 data->REG_AUTO_TEMP = NCT6775_REG_AUTO_TEMP;
3208 data->REG_AUTO_PWM = NCT6775_REG_AUTO_PWM;
3209 data->REG_CRITICAL_TEMP = NCT6775_REG_CRITICAL_TEMP;
3210 data->REG_CRITICAL_TEMP_TOLERANCE
3211 = NCT6775_REG_CRITICAL_TEMP_TOLERANCE;
3212 data->REG_TEMP_OFFSET = NCT6775_REG_TEMP_OFFSET;
3213 data->REG_TEMP_SOURCE = NCT6775_REG_TEMP_SOURCE;
3214 data->REG_TEMP_SEL[0] = NCT6775_REG_TEMP_SEL;
3215 data->REG_TEMP_SEL[1] = NCT6775_REG_WEIGHT_TEMP_SEL;
3216 data->REG_WEIGHT_TEMP[0] = NCT6775_REG_WEIGHT_TEMP_STEP;
3217 data->REG_WEIGHT_TEMP[1] = NCT6775_REG_WEIGHT_TEMP_STEP_TOL;
3218 data->REG_WEIGHT_TEMP[2] = NCT6775_REG_WEIGHT_TEMP_BASE;
3219 data->REG_ALARM = NCT6775_REG_ALARM;
3221 reg_temp = NCT6775_REG_TEMP;
3222 num_reg_temp = ARRAY_SIZE(NCT6775_REG_TEMP);
3223 reg_temp_over = NCT6775_REG_TEMP_OVER;
3224 reg_temp_hyst = NCT6775_REG_TEMP_HYST;
3225 reg_temp_config = NCT6776_REG_TEMP_CONFIG;
3226 reg_temp_alternate = NCT6776_REG_TEMP_ALTERNATE;
3227 reg_temp_crit = NCT6776_REG_TEMP_CRIT;
3233 data->auto_pwm_num = 4;
3234 data->has_fan_div = false;
3235 data->temp_fixed_num = 6;
3237 data->ALARM_BITS = NCT6779_ALARM_BITS;
3239 data->fan_from_reg = fan_from_reg13;
3240 data->fan_from_reg_min = fan_from_reg13;
3242 data->temp_label = nct6779_temp_label;
3243 data->temp_label_num = ARRAY_SIZE(nct6779_temp_label);
3245 data->REG_CONFIG = NCT6775_REG_CONFIG;
3246 data->REG_VBAT = NCT6775_REG_VBAT;
3247 data->REG_DIODE = NCT6775_REG_DIODE;
3248 data->REG_VIN = NCT6779_REG_IN;
3249 data->REG_IN_MINMAX[0] = NCT6775_REG_IN_MIN;
3250 data->REG_IN_MINMAX[1] = NCT6775_REG_IN_MAX;
3251 data->REG_TARGET = NCT6775_REG_TARGET;
3252 data->REG_FAN = NCT6779_REG_FAN;
3253 data->REG_FAN_MODE = NCT6775_REG_FAN_MODE;
3254 data->REG_FAN_MIN = NCT6776_REG_FAN_MIN;
3255 data->REG_FAN_TIME[0] = NCT6775_REG_FAN_STOP_TIME;
3256 data->REG_FAN_TIME[1] = NCT6775_REG_FAN_STEP_UP_TIME;
3257 data->REG_FAN_TIME[2] = NCT6775_REG_FAN_STEP_DOWN_TIME;
3258 data->REG_PWM[0] = NCT6775_REG_PWM;
3259 data->REG_PWM[1] = NCT6775_REG_FAN_START_OUTPUT;
3260 data->REG_PWM[2] = NCT6775_REG_FAN_STOP_OUTPUT;
3261 data->REG_PWM[5] = NCT6775_REG_WEIGHT_DUTY_STEP;
3262 data->REG_PWM[6] = NCT6776_REG_WEIGHT_DUTY_BASE;
3263 data->REG_PWM_READ = NCT6775_REG_PWM_READ;
3264 data->REG_PWM_MODE = NCT6776_REG_PWM_MODE;
3265 data->PWM_MODE_MASK = NCT6776_PWM_MODE_MASK;
3266 data->REG_TEMP_MON = NCT6775_REG_TEMP_MON;
3267 data->REG_AUTO_TEMP = NCT6775_REG_AUTO_TEMP;
3268 data->REG_AUTO_PWM = NCT6775_REG_AUTO_PWM;
3269 data->REG_CRITICAL_TEMP = NCT6775_REG_CRITICAL_TEMP;
3270 data->REG_CRITICAL_TEMP_TOLERANCE
3271 = NCT6775_REG_CRITICAL_TEMP_TOLERANCE;
3272 data->REG_TEMP_OFFSET = NCT6779_REG_TEMP_OFFSET;
3273 data->REG_TEMP_SOURCE = NCT6775_REG_TEMP_SOURCE;
3274 data->REG_TEMP_SEL[0] = NCT6775_REG_TEMP_SEL;
3275 data->REG_TEMP_SEL[1] = NCT6775_REG_WEIGHT_TEMP_SEL;
3276 data->REG_WEIGHT_TEMP[0] = NCT6775_REG_WEIGHT_TEMP_STEP;
3277 data->REG_WEIGHT_TEMP[1] = NCT6775_REG_WEIGHT_TEMP_STEP_TOL;
3278 data->REG_WEIGHT_TEMP[2] = NCT6775_REG_WEIGHT_TEMP_BASE;
3279 data->REG_ALARM = NCT6779_REG_ALARM;
3281 reg_temp = NCT6779_REG_TEMP;
3282 num_reg_temp = ARRAY_SIZE(NCT6779_REG_TEMP);
3283 reg_temp_over = NCT6779_REG_TEMP_OVER;
3284 reg_temp_hyst = NCT6779_REG_TEMP_HYST;
3285 reg_temp_config = NCT6779_REG_TEMP_CONFIG;
3286 reg_temp_alternate = NCT6779_REG_TEMP_ALTERNATE;
3287 reg_temp_crit = NCT6779_REG_TEMP_CRIT;
3293 data->have_in = (1 << data->in_num) - 1;
3294 data->have_temp = 0;
3297 * On some boards, not all available temperature sources are monitored,
3298 * even though some of the monitoring registers are unused.
3299 * Get list of unused monitoring registers, then detect if any fan
3300 * controls are configured to use unmonitored temperature sources.
3301 * If so, assign the unmonitored temperature sources to available
3302 * monitoring registers.
3306 for (i = 0; i < num_reg_temp; i++) {
3307 if (reg_temp[i] == 0)
3310 src = nct6775_read_value(data, data->REG_TEMP_SOURCE[i]) & 0x1f;
3311 if (!src || (mask & (1 << src)))
3312 available |= 1 << i;
3318 * Now find unmonitored temperature registers and enable monitoring
3319 * if additional monitoring registers are available.
3321 for (i = 0; i < ARRAY_SIZE(data->REG_TEMP_SEL) && available; i++) {
3324 if (!data->REG_TEMP_SEL[i])
3326 for (j = 0; j < data->pwm_num && available; j++) {
3329 if (!data->REG_TEMP_SEL[i][j])
3331 src = nct6775_read_value(data,
3332 data->REG_TEMP_SEL[i][j]);
3334 if (!src || (mask & (1 << src)))
3336 if (src >= data->temp_label_num ||
3337 !strlen(data->temp_label[src])) {
3339 "Select source %d:%d reg 0x%x invalid (%d)\n",
3340 i, j, data->REG_TEMP_SEL[i][j], src);
3344 index = __ffs(available);
3345 nct6775_write_value(data,
3346 data->REG_TEMP_SOURCE[index],
3348 available &= ~(1 << index);
3354 s = NUM_TEMP_FIXED; /* First dynamic temperature attribute */
3355 for (i = 0; i < num_reg_temp; i++) {
3356 if (reg_temp[i] == 0)
3359 src = nct6775_read_value(data, data->REG_TEMP_SOURCE[i]) & 0x1f;
3360 if (!src || (mask & (1 << src)))
3363 if (src >= data->temp_label_num ||
3364 !strlen(data->temp_label[src])) {
3366 "Invalid temperature source %d at index %d, source register 0x%x, temp register 0x%x\n",
3367 src, i, data->REG_TEMP_SOURCE[i], reg_temp[i]);
3373 /* Use fixed index for SYSTIN(1), CPUTIN(2), AUXTIN(3) */
3374 if (src <= data->temp_fixed_num) {
3375 data->have_temp |= 1 << (src - 1);
3376 data->have_temp_fixed |= 1 << (src - 1);
3377 data->reg_temp[0][src - 1] = reg_temp[i];
3378 data->reg_temp[1][src - 1] = reg_temp_over[i];
3379 data->reg_temp[2][src - 1] = reg_temp_hyst[i];
3380 data->reg_temp_config[src - 1] = reg_temp_config[i];
3381 data->temp_src[src - 1] = src;
3388 /* Use dynamic index for other sources */
3389 data->have_temp |= 1 << s;
3390 data->reg_temp[0][s] = reg_temp[i];
3391 data->reg_temp[1][s] = reg_temp_over[i];
3392 data->reg_temp[2][s] = reg_temp_hyst[i];
3393 data->reg_temp_config[s] = reg_temp_config[i];
3394 if (reg_temp_crit[src - 1])
3395 data->reg_temp[3][s] = reg_temp_crit[src - 1];
3397 data->temp_src[s] = src;
3403 * Go through the list of alternate temp registers and enable
3405 * The temperature is already monitored if the respective bit in <mask>
3408 for (i = 0; i < data->temp_label_num - 1; i++) {
3409 if (!reg_temp_alternate[i])
3411 if (mask & (1 << (i + 1)))
3413 if (i < data->temp_fixed_num) {
3414 if (data->have_temp & (1 << i))
3416 data->have_temp |= 1 << i;
3417 data->have_temp_fixed |= 1 << i;
3418 data->reg_temp[0][i] = reg_temp_alternate[i];
3419 data->reg_temp[1][i] = reg_temp_over[i];
3420 data->reg_temp[2][i] = reg_temp_hyst[i];
3421 data->temp_src[i] = i + 1;
3425 if (s >= NUM_TEMP) /* Abort if no more space */
3428 data->have_temp |= 1 << s;
3429 data->reg_temp[0][s] = reg_temp_alternate[i];
3430 data->temp_src[s] = i + 1;
3433 #endif /* TESTING */
3435 switch (data->kind) {
3440 * On NCT6776, AUXTIN and VIN3 pins are shared.
3441 * Only way to detect it is to check if AUXTIN is used
3442 * as a temperature source, and if that source is
3445 * If that is the case, disable in6, which reports VIN3.
3446 * Otherwise disable temp3.
3448 if (data->have_temp & (1 << 2)) {
3449 u8 reg = nct6775_read_value(data,
3450 data->reg_temp_config[2]);
3452 data->have_temp &= ~(1 << 2);
3454 data->have_in &= ~(1 << 6);
3465 * There does not seem to be a clean way to detect if VINx or
3466 * AUXTINx is active, so for keep both sensor types enabled
3472 /* Initialize the chip */
3473 nct6775_init_device(data);
3475 data->vrm = vid_which_vrm();
3476 superio_enter(sio_data->sioreg);
3479 * We can get the VID input values directly at logical device D 0xe3.
3481 superio_select(sio_data->sioreg, NCT6775_LD_VID);
3482 data->vid = superio_inb(sio_data->sioreg, 0xe3);
3487 superio_select(sio_data->sioreg, NCT6775_LD_HWM);
3488 tmp = superio_inb(sio_data->sioreg,
3489 NCT6775_REG_CR_FAN_DEBOUNCE);
3490 switch (data->kind) {
3499 superio_outb(sio_data->sioreg, NCT6775_REG_CR_FAN_DEBOUNCE,
3501 dev_info(&pdev->dev, "Enabled fan debounce for chip %s\n",
3505 superio_exit(sio_data->sioreg);
3507 err = device_create_file(dev, &dev_attr_cpu0_vid);
3511 nct6775_check_fan_inputs(sio_data, data);
3513 /* Read fan clock dividers immediately */
3514 nct6775_update_fan_div_common(dev, data);
3516 /* Register sysfs hooks */
3517 for (i = 0; i < data->pwm_num; i++) {
3518 if (!(data->has_pwm & (1 << i)))
3521 err = sysfs_create_group(&dev->kobj, &nct6775_group_pwm[i]);
3525 if (data->REG_PWM[3]) {
3526 err = device_create_file(dev,
3527 &sda_max_output[i].dev_attr);
3531 if (data->REG_PWM[4]) {
3532 err = device_create_file(dev,
3533 &sda_step_output[i].dev_attr);
3537 if (data->REG_PWM[6]) {
3538 err = device_create_file(dev,
3539 &sda_weight_duty_base[i].dev_attr);
3544 for (i = 0; i < ARRAY_SIZE(sda_auto_pwm_arrays); i++) {
3545 struct sensor_device_attribute_2 *attr =
3546 &sda_auto_pwm_arrays[i];
3548 if (!(data->has_pwm & (1 << attr->nr)))
3550 if (attr->index > data->auto_pwm_num)
3552 err = device_create_file(dev, &attr->dev_attr);
3557 for (i = 0; i < data->in_num; i++) {
3558 if (!(data->have_in & (1 << i)))
3560 err = sysfs_create_group(&dev->kobj, &nct6775_group_in[i]);
3565 for (i = 0; i < 5; i++) {
3566 if (data->has_fan & (1 << i)) {
3567 err = device_create_file(dev,
3568 &sda_fan_input[i].dev_attr);
3571 err = device_create_file(dev,
3572 &sda_fan_alarm[i].dev_attr);
3575 if (data->kind != nct6776 &&
3576 data->kind != nct6779) {
3577 err = device_create_file(dev,
3578 &sda_fan_div[i].dev_attr);
3582 if (data->has_fan_min & (1 << i)) {
3583 err = device_create_file(dev,
3584 &sda_fan_min[i].dev_attr);
3591 for (i = 0; i < NUM_TEMP; i++) {
3592 if (!(data->have_temp & (1 << i)))
3594 err = device_create_file(dev, &sda_temp_input[i].dev_attr);
3597 if (data->temp_label) {
3598 err = device_create_file(dev,
3599 &sda_temp_label[i].dev_attr);
3603 if (data->reg_temp[1][i]) {
3604 err = device_create_file(dev,
3605 &sda_temp_max[i].dev_attr);
3609 if (data->reg_temp[2][i]) {
3610 err = device_create_file(dev,
3611 &sda_temp_max_hyst[i].dev_attr);
3615 if (data->reg_temp[3][i]) {
3616 err = device_create_file(dev,
3617 &sda_temp_crit[i].dev_attr);
3621 if (!(data->have_temp_fixed & (1 << i)))
3623 err = device_create_file(dev, &sda_temp_type[i].dev_attr);
3626 err = device_create_file(dev, &sda_temp_offset[i].dev_attr);
3629 if (i >= NUM_TEMP_ALARM ||
3630 data->ALARM_BITS[TEMP_ALARM_BASE + i] < 0)
3632 err = device_create_file(dev, &sda_temp_alarm[i].dev_attr);
3637 for (i = 0; i < ARRAY_SIZE(sda_caseopen); i++) {
3638 if (data->ALARM_BITS[INTRUSION_ALARM_BASE + i] < 0)
3640 err = device_create_file(dev, &sda_caseopen[i].dev_attr);
3645 err = device_create_file(dev, &dev_attr_name);
3649 data->hwmon_dev = hwmon_device_register(dev);
3650 if (IS_ERR(data->hwmon_dev)) {
3651 err = PTR_ERR(data->hwmon_dev);
3658 nct6775_device_remove_files(dev);
3662 static int __devexit nct6775_remove(struct platform_device *pdev)
3664 struct nct6775_data *data = platform_get_drvdata(pdev);
3666 hwmon_device_unregister(data->hwmon_dev);
3667 nct6775_device_remove_files(&pdev->dev);
3672 static struct platform_driver nct6775_driver = {
3674 .owner = THIS_MODULE,
3677 .probe = nct6775_probe,
3678 .remove = __devexit_p(nct6775_remove),
3681 /* nct6775_find() looks for a '627 in the Super-I/O config space */
3682 static int __init nct6775_find(int sioaddr, unsigned short *addr,
3683 struct nct6775_sio_data *sio_data)
3685 static const char __initdata sio_name_NCT6775[] = "NCT6775F";
3686 static const char __initdata sio_name_NCT6776[] = "NCT6776F";
3687 static const char __initdata sio_name_NCT6779[] = "NCT6779D";
3690 const char *sio_name;
3692 superio_enter(sioaddr);
3697 val = (superio_inb(sioaddr, SIO_REG_DEVID) << 8)
3698 | superio_inb(sioaddr, SIO_REG_DEVID + 1);
3699 switch (val & SIO_ID_MASK) {
3700 case SIO_NCT6775_ID:
3701 sio_data->kind = nct6775;
3702 sio_name = sio_name_NCT6775;
3704 case SIO_NCT6776_ID:
3705 sio_data->kind = nct6776;
3706 sio_name = sio_name_NCT6776;
3708 case SIO_NCT6779_ID:
3709 sio_data->kind = nct6779;
3710 sio_name = sio_name_NCT6779;
3714 pr_debug("unsupported chip ID: 0x%04x\n", val);
3715 superio_exit(sioaddr);
3719 /* We have a known chip, find the HWM I/O address */
3720 superio_select(sioaddr, NCT6775_LD_HWM);
3721 val = (superio_inb(sioaddr, SIO_REG_ADDR) << 8)
3722 | superio_inb(sioaddr, SIO_REG_ADDR + 1);
3723 *addr = val & IOREGION_ALIGNMENT;
3725 pr_err("Refusing to enable a Super-I/O device with a base I/O port 0\n");
3726 superio_exit(sioaddr);
3730 /* Activate logical device if needed */
3731 val = superio_inb(sioaddr, SIO_REG_ENABLE);
3732 if (!(val & 0x01)) {
3733 pr_warn("Forcibly enabling Super-I/O. Sensor is probably unusable.\n");
3734 superio_outb(sioaddr, SIO_REG_ENABLE, val | 0x01);
3737 superio_exit(sioaddr);
3738 pr_info("Found %s chip at %#x\n", sio_name, *addr);
3739 sio_data->sioreg = sioaddr;
3745 * when Super-I/O functions move to a separate file, the Super-I/O
3746 * bus will manage the lifetime of the device and this module will only keep
3747 * track of the nct6775 driver. But since we platform_device_alloc(), we
3748 * must keep track of the device
3750 static struct platform_device *pdev;
3752 static int __init sensors_nct6775_init(void)
3755 unsigned short address;
3756 struct resource res;
3757 struct nct6775_sio_data sio_data;
3760 * initialize sio_data->kind and sio_data->sioreg.
3762 * when Super-I/O functions move to a separate file, the Super-I/O
3763 * driver will probe 0x2e and 0x4e and auto-detect the presence of a
3764 * nct6775 hardware monitor, and call probe()
3766 if (nct6775_find(0x2e, &address, &sio_data) &&
3767 nct6775_find(0x4e, &address, &sio_data))
3770 err = platform_driver_register(&nct6775_driver);
3774 pdev = platform_device_alloc(DRVNAME, address);
3777 pr_err("Device allocation failed\n");
3778 goto exit_unregister;
3781 err = platform_device_add_data(pdev, &sio_data,
3782 sizeof(struct nct6775_sio_data));
3784 pr_err("Platform data allocation failed\n");
3785 goto exit_device_put;
3788 memset(&res, 0, sizeof(res));
3790 res.start = address + IOREGION_OFFSET;
3791 res.end = address + IOREGION_OFFSET + IOREGION_LENGTH - 1;
3792 res.flags = IORESOURCE_IO;
3794 err = acpi_check_resource_conflict(&res);
3796 goto exit_device_put;
3798 err = platform_device_add_resources(pdev, &res, 1);
3800 pr_err("Device resource addition failed (%d)\n", err);
3801 goto exit_device_put;
3804 /* platform_device_add calls probe() */
3805 err = platform_device_add(pdev);
3807 pr_err("Device addition failed (%d)\n", err);
3808 goto exit_device_put;
3814 platform_device_put(pdev);
3816 platform_driver_unregister(&nct6775_driver);
3821 static void __exit sensors_nct6775_exit(void)
3823 platform_device_unregister(pdev);
3824 platform_driver_unregister(&nct6775_driver);
3827 MODULE_AUTHOR("Guenter Roeck <linux@roeck-us.net>");
3828 MODULE_DESCRIPTION("NCT6775F/NCT6776F/NCT6779D driver");
3829 MODULE_LICENSE("GPL");
3831 module_init(sensors_nct6775_init);
3832 module_exit(sensors_nct6775_exit);