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 static const u16 NCT6775_REG_FAN_MIN[] = { 0x3b, 0x3c, 0x3d };
158 /* Voltage min/max registers for nr=7..14 are in bank 5 */
160 static const u16 NCT6775_REG_IN_MAX[] = {
161 0x2b, 0x2d, 0x2f, 0x31, 0x33, 0x35, 0x37, 0x554, 0x556 };
162 static const u16 NCT6775_REG_IN_MIN[] = {
163 0x2c, 0x2e, 0x30, 0x32, 0x34, 0x36, 0x38, 0x555, 0x557 };
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_TARGET[] = { 0x101, 0x201, 0x301, 0x801, 0x901 };
230 static const u16 NCT6775_REG_FAN_MODE[] = { 0x102, 0x202, 0x302, 0x802, 0x902 };
231 static const u16 NCT6775_REG_FAN_STEP_DOWN_TIME[] = {
232 0x103, 0x203, 0x303, 0x803, 0x903 };
233 static const u16 NCT6775_REG_FAN_STEP_UP_TIME[] = {
234 0x104, 0x204, 0x304, 0x804, 0x904 };
235 static const u16 NCT6775_REG_FAN_STOP_OUTPUT[] = {
236 0x105, 0x205, 0x305, 0x805, 0x905 };
237 static const u16 NCT6775_REG_FAN_START_OUTPUT[]
238 = { 0x106, 0x206, 0x306, 0x806, 0x906 };
239 static const u16 NCT6775_REG_FAN_MAX_OUTPUT[] = { 0x10a, 0x20a, 0x30a };
240 static const u16 NCT6775_REG_FAN_STEP_OUTPUT[] = { 0x10b, 0x20b, 0x30b };
242 static const u16 NCT6775_REG_FAN_STOP_TIME[]
243 = { 0x107, 0x207, 0x307, 0x807, 0x907 };
244 static const u16 NCT6775_REG_PWM[] = { 0x109, 0x209, 0x309, 0x809, 0x909 };
245 static const u16 NCT6775_REG_PWM_READ[] = { 0x01, 0x03, 0x11, 0x13, 0x15 };
247 static const u16 NCT6775_REG_FAN[] = { 0x630, 0x632, 0x634, 0x636, 0x638 };
249 static const u16 NCT6776_REG_FAN_MIN[] = { 0x63a, 0x63c, 0x63e, 0x640, 0x642};
251 static const u16 NCT6779_REG_TOLERANCE_H[]
252 = { 0x10c, 0x20c, 0x30c, 0x40c, 0x50c };
254 static const u16 NCT6779_REG_FAN[] = { 0x4c0, 0x4c2, 0x4c4, 0x4c6, 0x4c8 };
256 static const u16 NCT6775_REG_TEMP[]
257 = { 0x27, 0x150, 0x250, 0x62b, 0x62c, 0x62d };
258 static const u16 NCT6779_REG_TEMP[] = { 0x27, 0x150 };
260 static const u16 NCT6775_REG_TEMP_CONFIG[]
261 = { 0, 0x152, 0x252, 0x628, 0x629, 0x62A };
262 static const u16 NCT6775_REG_TEMP_HYST[]
263 = { 0x3a, 0x153, 0x253, 0x673, 0x678, 0x67D };
264 static const u16 NCT6775_REG_TEMP_OVER[]
265 = { 0x39, 0x155, 0x255, 0x672, 0x677, 0x67C };
267 static const u16 NCT6775_REG_TEMP_SOURCE[]
268 = { 0x621, 0x622, 0x623, 0x624, 0x625, 0x626 };
270 static const u16 NCT6775_REG_TEMP_SEL[]
271 = { 0x100, 0x200, 0x300, 0x800, 0x900 };
273 static const u16 NCT6775_REG_WEIGHT_TEMP_SEL[]
274 = { 0x139, 0x239, 0x339, 0x839, 0x939 };
275 static const u16 NCT6775_REG_WEIGHT_TEMP_STEP[]
276 = { 0x13a, 0x23a, 0x33a, 0x83a, 0x93a };
277 static const u16 NCT6775_REG_WEIGHT_TEMP_STEP_TOL[]
278 = { 0x13b, 0x23b, 0x33b, 0x83b, 0x93b };
279 static const u16 NCT6775_REG_WEIGHT_DUTY_STEP[]
280 = { 0x13c, 0x23c, 0x33c, 0x83c, 0x93c };
281 static const u16 NCT6775_REG_WEIGHT_TEMP_BASE[]
282 = { 0x13d, 0x23d, 0x33d, 0x83d, 0x93d };
284 static const u16 NCT6776_REG_WEIGHT_DUTY_BASE[]
285 = { 0x13e, 0x23e, 0x33e, 0x83e, 0x93e };
287 static const u16 NCT6775_REG_TEMP_OFFSET[] = { 0x454, 0x455, 0x456 };
289 static const u16 NCT6779_REG_TEMP_OFFSET[]
290 = { 0x454, 0x455, 0x456, 0x44a, 0x44b, 0x44c };
292 static const u16 NCT6776_REG_TEMP_CONFIG[11]
293 = { 0x18, 0x152, 0x252, 0x628, 0x629, 0x62A };
295 static const u16 NCT6775_REG_TEMP_MON[] = { 0x73, 0x75, 0x77, 0x79, 0x7b };
297 static const u16 NCT6775_REG_AUTO_TEMP[]
298 = { 0x121, 0x221, 0x321, 0x821, 0x921 };
299 static const u16 NCT6775_REG_AUTO_PWM[]
300 = { 0x127, 0x227, 0x327, 0x827, 0x927 };
302 #define NCT6775_AUTO_TEMP(data, nr, p) ((data)->REG_AUTO_TEMP[nr] + (p))
303 #define NCT6775_AUTO_PWM(data, nr, p) ((data)->REG_AUTO_PWM[nr] + (p))
305 static const u16 NCT6775_REG_CRITICAL_ENAB[] = { 0x134, 0x234, 0x334 };
307 static const u16 NCT6775_REG_CRITICAL_TEMP[] = {
308 0x135, 0x235, 0x335, 0x835, 0x935 };
309 static const u16 NCT6775_REG_CRITICAL_TEMP_TOLERANCE[] = {
310 0x138, 0x238, 0x338, 0x838, 0x938 };
312 static const u16 NCT6779_REG_CRITICAL_PWM_ENABLE[] = {
313 0x136, 0x236, 0x336, 0x836, 0x936 };
314 static const u16 NCT6779_REG_CRITICAL_PWM[] = {
315 0x137, 0x237, 0x337, 0x837, 0x937 };
317 static const char *const nct6775_temp_label[] = {
331 "PCH_CHIP_CPU_MAX_TEMP",
341 static const char *const nct6776_temp_label[] = {
356 "PCH_CHIP_CPU_MAX_TEMP",
367 static const char *const nct6779_temp_label[] = {
386 "PCH_CHIP_CPU_MAX_TEMP",
397 static const u16 NCT6775_REG_TEMP_ALTERNATE[ARRAY_SIZE(nct6779_temp_label)]
398 = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0x661, 0x662, 0x664 };
400 static const u16 NCT6776_REG_TEMP_ALTERNATE[ARRAY_SIZE(nct6776_temp_label)]
401 = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0x401, 0x402, 0x404 };
403 static const u16 NCT6779_REG_TEMP_ALTERNATE[ARRAY_SIZE(nct6779_temp_label)]
404 = { 0x490, 0x491, 0x492, 0x493, 0x494, 0x495, 0, 0,
405 0, 0, 0, 0, 0, 0, 0, 0,
406 0, 0, 0x401, 0x402, 0x404 };
408 #define NUM_TEMP 10 /* Max number of temp attribute sets w/ limits*/
409 #define NUM_TEMP_FIXED 6 /* Max number of fixed temp attribute sets */
411 #define NUM_REG_TEMP ARRAY_SIZE(NCT6775_REG_TEMP)
413 static inline int reg_to_pwm_enable(int pwm, int mode)
415 if (mode == 0 && pwm == 255)
420 static inline int pwm_enable_to_reg(int mode)
431 /* 1 is DC mode, output in ms */
432 static inline unsigned int step_time_from_reg(u8 reg, u8 mode)
434 return mode ? 400 * reg : 100 * reg;
437 static inline u8 step_time_to_reg(unsigned int msec, u8 mode)
439 return SENSORS_LIMIT((mode ? (msec + 200) / 400 :
440 (msec + 50) / 100), 1, 255);
443 static unsigned int fan_from_reg8(u16 reg, unsigned int divreg)
445 if (reg == 0 || reg == 255)
447 return 1350000U / (reg << divreg);
450 static unsigned int fan_from_reg13(u16 reg, unsigned int divreg)
452 if ((reg & 0xff1f) == 0xff1f)
455 reg = (reg & 0x1f) | ((reg & 0xff00) >> 3);
460 return 1350000U / reg;
463 static unsigned int fan_from_reg16(u16 reg, unsigned int divreg)
465 if (reg == 0 || reg == 0xffff)
469 * Even though the registers are 16 bit wide, the fan divisor
472 return 1350000U / (reg << divreg);
475 static inline unsigned int
482 * Some of the voltage inputs have internal scaling, the tables below
483 * contain 8 (the ADC LSB in mV) * scaling factor * 100
485 static const u16 scale_in[15] = {
486 800, 800, 1600, 1600, 800, 800, 800, 1600, 1600, 800, 800, 800, 800,
490 static inline long in_from_reg(u8 reg, u8 nr)
492 return DIV_ROUND_CLOSEST(reg * scale_in[nr], 100);
495 static inline u8 in_to_reg(u32 val, u8 nr)
497 return SENSORS_LIMIT(DIV_ROUND_CLOSEST(val * 100, scale_in[nr]), 0,
502 * Data structures and manipulation thereof
505 struct nct6775_data {
506 int addr; /* IO base of hw monitor block */
510 struct device *hwmon_dev;
513 u16 reg_temp[3][NUM_TEMP]; /* 0=temp, 1=temp_over, 2=temp_hyst */
514 u8 temp_src[NUM_TEMP];
515 u16 reg_temp_config[NUM_TEMP];
516 const char * const *temp_label;
523 const s8 *ALARM_BITS;
526 const u16 *REG_IN_MINMAX[2];
528 const u16 *REG_TARGET;
530 const u16 *REG_FAN_MODE;
531 const u16 *REG_FAN_MIN;
532 const u16 *REG_FAN_TIME[3];
534 const u8 *REG_PWM_MODE;
535 const u8 *PWM_MODE_MASK;
537 const u16 *REG_PWM[7]; /* [0]=pwm, [1]=start_output, [2]=stop_output,
538 * [3]=max_output, [4]=step_output,
539 * [5]=weight_duty_step, [6]=weight_duty_base
541 const u16 *REG_PWM_READ;
543 const u16 *REG_TEMP_ALTERNATE;
544 const u16 *REG_TEMP_MON;
545 const u16 *REG_AUTO_TEMP;
546 const u16 *REG_AUTO_PWM;
548 const u16 *REG_CRITICAL_TEMP;
549 const u16 *REG_CRITICAL_TEMP_TOLERANCE;
551 const u16 *REG_TEMP_SOURCE; /* temp register sources */
552 const u16 *REG_TEMP_SEL[2]; /* pwm temp, 0=base, 1=weight */
554 const u16 *REG_WEIGHT_TEMP[3]; /* 0=base, 1=hyst, 2=step */
556 const u16 *REG_TEMP_OFFSET;
558 const u16 *REG_ALARM;
560 unsigned int (*fan_from_reg)(u16 reg, unsigned int divreg);
561 unsigned int (*fan_from_reg_min)(u16 reg, unsigned int divreg);
563 struct mutex update_lock;
564 char valid; /* !=0 if following fields are valid */
565 unsigned long last_updated; /* In jiffies */
567 /* Register values */
568 u8 bank; /* current register bank */
569 u8 in_num; /* number of in inputs we have */
570 u8 in[15][3]; /* [0]=in, [1]=in_max, [2]=in_min */
575 u8 has_fan; /* some fan inputs can be disabled */
576 u8 has_fan_min; /* some fans don't have min register */
579 u8 temp_fixed_num; /* 3 or 6 */
580 u8 temp_type[NUM_TEMP_FIXED];
581 s8 temp_offset[NUM_TEMP_FIXED];
582 s16 temp[3][NUM_TEMP]; /* 0=temp, 1=temp_over, 2=temp_hyst */
585 u8 pwm_num; /* number of pwm */
587 u8 pwm_mode[5]; /* 1->DC variable voltage, 0->PWM variable duty cycle */
588 u8 pwm_enable[5]; /* 0->off
590 * 2->thermal cruise mode (also called SmartFan I)
591 * 3->fan speed cruise mode
593 * 5->enhanced variable thermal cruise (SmartFan IV)
595 u8 pwm[7][5]; /* [0]=pwm, [1]=start_output, [2]=stop_output,
596 * [3]=max_output, [4]=step_output,
597 * [5]=weight_duty_step, [6]=weight_duty_base
603 u8 fan_time[3][5]; /* 0 = stop_time, 1 = step_up, 2 = step_down */
605 /* Automatic fan speed control registers */
610 u8 pwm_temp_sel[2][5];
612 bool pwm_sel_enable[2][5];/* 0->stop_val, 1->weight;
613 * false->off, true->on
615 u8 weight_temp[3][5]; /* 0->temp_step, 1->temp_step_tol,
627 struct nct6775_sio_data {
632 static bool is_word_sized(struct nct6775_data *data, u16 reg)
634 switch (data->kind) {
636 return (((reg & 0xff00) == 0x100 ||
637 (reg & 0xff00) == 0x200) &&
638 ((reg & 0x00ff) == 0x50 ||
639 (reg & 0x00ff) == 0x53 ||
640 (reg & 0x00ff) == 0x55)) ||
641 (reg & 0xfff0) == 0x630 ||
642 reg == 0x640 || reg == 0x642 ||
644 ((reg & 0xfff0) == 0x650 && (reg & 0x000f) >= 0x06) ||
645 reg == 0x73 || reg == 0x75 || reg == 0x77;
647 return (((reg & 0xff00) == 0x100 ||
648 (reg & 0xff00) == 0x200) &&
649 ((reg & 0x00ff) == 0x50 ||
650 (reg & 0x00ff) == 0x53 ||
651 (reg & 0x00ff) == 0x55)) ||
652 (reg & 0xfff0) == 0x630 ||
654 reg == 0x640 || reg == 0x642 ||
655 ((reg & 0xfff0) == 0x650 && (reg & 0x000f) >= 0x06) ||
656 reg == 0x73 || reg == 0x75 || reg == 0x77;
658 return reg == 0x150 || reg == 0x153 || reg == 0x155 ||
659 ((reg & 0xfff0) == 0x4c0 && (reg & 0x000f) < 0x09) ||
661 reg == 0x63a || reg == 0x63c || reg == 0x63e ||
662 reg == 0x640 || reg == 0x642 ||
663 reg == 0x73 || reg == 0x75 || reg == 0x77 || reg == 0x79 ||
670 * On older chips, only registers 0x50-0x5f are banked.
671 * On more recent chips, all registers are banked.
672 * Assume that is the case and set the bank number for each access.
673 * Cache the bank number so it only needs to be set if it changes.
675 static inline void nct6775_set_bank(struct nct6775_data *data, u16 reg)
678 if (data->bank != bank) {
679 outb_p(NCT6775_REG_BANK, data->addr + ADDR_REG_OFFSET);
680 outb_p(bank, data->addr + DATA_REG_OFFSET);
685 static u16 nct6775_read_value(struct nct6775_data *data, u16 reg)
687 int res, word_sized = is_word_sized(data, reg);
689 mutex_lock(&data->lock);
691 nct6775_set_bank(data, reg);
692 outb_p(reg & 0xff, data->addr + ADDR_REG_OFFSET);
693 res = inb_p(data->addr + DATA_REG_OFFSET);
695 outb_p((reg & 0xff) + 1,
696 data->addr + ADDR_REG_OFFSET);
697 res = (res << 8) + inb_p(data->addr + DATA_REG_OFFSET);
700 mutex_unlock(&data->lock);
704 static int nct6775_write_value(struct nct6775_data *data, u16 reg, u16 value)
706 int word_sized = is_word_sized(data, reg);
708 mutex_lock(&data->lock);
710 nct6775_set_bank(data, reg);
711 outb_p(reg & 0xff, data->addr + ADDR_REG_OFFSET);
713 outb_p(value >> 8, data->addr + DATA_REG_OFFSET);
714 outb_p((reg & 0xff) + 1,
715 data->addr + ADDR_REG_OFFSET);
717 outb_p(value & 0xff, data->addr + DATA_REG_OFFSET);
719 mutex_unlock(&data->lock);
723 /* We left-align 8-bit temperature values to make the code simpler */
724 static u16 nct6775_read_temp(struct nct6775_data *data, u16 reg)
728 res = nct6775_read_value(data, reg);
729 if (!is_word_sized(data, reg))
735 static int nct6775_write_temp(struct nct6775_data *data, u16 reg, u16 value)
737 if (!is_word_sized(data, reg))
739 return nct6775_write_value(data, reg, value);
742 /* This function assumes that the caller holds data->update_lock */
743 static void nct6775_write_fan_div(struct nct6775_data *data, int nr)
749 reg = (nct6775_read_value(data, NCT6775_REG_FANDIV1) & 0x70)
750 | (data->fan_div[0] & 0x7);
751 nct6775_write_value(data, NCT6775_REG_FANDIV1, reg);
754 reg = (nct6775_read_value(data, NCT6775_REG_FANDIV1) & 0x7)
755 | ((data->fan_div[1] << 4) & 0x70);
756 nct6775_write_value(data, NCT6775_REG_FANDIV1, reg);
758 reg = (nct6775_read_value(data, NCT6775_REG_FANDIV2) & 0x70)
759 | (data->fan_div[2] & 0x7);
760 nct6775_write_value(data, NCT6775_REG_FANDIV2, reg);
763 reg = (nct6775_read_value(data, NCT6775_REG_FANDIV2) & 0x7)
764 | ((data->fan_div[3] << 4) & 0x70);
765 nct6775_write_value(data, NCT6775_REG_FANDIV2, reg);
770 static void nct6775_write_fan_div_common(struct device *dev,
771 struct nct6775_data *data, int nr)
773 if (data->kind == nct6775)
774 nct6775_write_fan_div(data, nr);
777 static void nct6775_update_fan_div(struct nct6775_data *data)
781 i = nct6775_read_value(data, NCT6775_REG_FANDIV1);
782 data->fan_div[0] = i & 0x7;
783 data->fan_div[1] = (i & 0x70) >> 4;
784 i = nct6775_read_value(data, NCT6775_REG_FANDIV2);
785 data->fan_div[2] = i & 0x7;
786 if (data->has_fan & (1<<3))
787 data->fan_div[3] = (i & 0x70) >> 4;
790 static void nct6775_update_fan_div_common(struct device *dev,
791 struct nct6775_data *data)
793 if (data->kind == nct6775)
794 nct6775_update_fan_div(data);
797 static void nct6775_update_pwm(struct device *dev)
799 struct nct6775_data *data = dev_get_drvdata(dev);
804 for (i = 0; i < data->pwm_num; i++) {
805 if (!(data->has_pwm & (1 << i)))
808 duty_is_dc = data->REG_PWM_MODE[i] &&
809 (nct6775_read_value(data, data->REG_PWM_MODE[i])
810 & data->PWM_MODE_MASK[i]);
811 data->pwm_mode[i] = duty_is_dc;
813 fanmodecfg = nct6775_read_value(data, data->REG_FAN_MODE[i]);
814 for (j = 0; j < ARRAY_SIZE(data->REG_PWM); j++) {
815 if (data->REG_PWM[j] && data->REG_PWM[j][i]) {
817 = nct6775_read_value(data,
818 data->REG_PWM[j][i]);
822 data->pwm_enable[i] = reg_to_pwm_enable(data->pwm[0][i],
823 (fanmodecfg >> 4) & 7);
825 data->tolerance[i][0] = fanmodecfg & 0x0f;
826 if (data->kind == nct6779) {
827 reg = nct6775_read_value(data,
828 NCT6779_REG_TOLERANCE_H[i]);
829 data->tolerance[i][0] |= (reg & 0x70) >> 1;
831 data->tolerance[i][1] =
832 nct6775_read_value(data,
833 data->REG_CRITICAL_TEMP_TOLERANCE[i]);
835 data->pwm_sel_enable[0][i] =
836 nct6775_read_value(data, data->REG_TEMP_SEL[0][i])
838 data->pwm_sel_enable[1][i] =
839 nct6775_read_value(data, data->REG_TEMP_SEL[1][i])
843 for (j = 0; j < 2; j++) {
844 reg = nct6775_read_value(data,
845 data->REG_TEMP_SEL[j][i]);
846 data->pwm_temp_sel[j][i] = reg & 0x1f;
848 /* Weight temp data */
849 for (j = 0; j < 3; j++) {
850 data->weight_temp[j][i]
851 = nct6775_read_value(data,
852 data->REG_WEIGHT_TEMP[j][i]);
857 static void nct6775_update_pwm_limits(struct device *dev)
859 struct nct6775_data *data = dev_get_drvdata(dev);
863 for (i = 0; i < data->pwm_num; i++) {
864 if (!(data->has_pwm & (1 << i)))
867 for (j = 0; j < 3; j++) {
868 data->fan_time[j][i] =
869 nct6775_read_value(data, data->REG_FAN_TIME[j][i]);
872 data->target_temp[i] =
873 nct6775_read_value(data, data->REG_TARGET[i]) &
874 (data->pwm_mode[i] ? 0xff : 0x7f);
876 nct6775_read_value(data, data->REG_TEMP_MON[i]);
878 for (j = 0; j < data->auto_pwm_num; j++) {
879 data->auto_pwm[i][j] =
880 nct6775_read_value(data,
881 NCT6775_AUTO_PWM(data, i, j));
882 data->auto_temp[i][j] =
883 nct6775_read_value(data,
884 NCT6775_AUTO_TEMP(data, i, j));
887 /* critical auto_pwm temperature data */
888 data->auto_temp[i][data->auto_pwm_num] =
889 nct6775_read_value(data, data->REG_CRITICAL_TEMP[i]);
891 switch (data->kind) {
893 reg = nct6775_read_value(data,
894 NCT6775_REG_CRITICAL_ENAB[i]);
895 data->auto_pwm[i][data->auto_pwm_num] =
896 (reg & 0x02) ? 0xff : 0x00;
899 data->auto_pwm[i][data->auto_pwm_num] = 0xff;
902 reg = nct6775_read_value(data,
903 NCT6779_REG_CRITICAL_PWM_ENABLE[i]);
905 data->auto_pwm[i][data->auto_pwm_num] =
906 nct6775_read_value(data,
907 NCT6779_REG_CRITICAL_PWM[i]);
909 data->auto_pwm[i][data->auto_pwm_num] = 0xff;
915 static struct nct6775_data *nct6775_update_device(struct device *dev)
917 struct nct6775_data *data = dev_get_drvdata(dev);
920 mutex_lock(&data->update_lock);
922 if (time_after(jiffies, data->last_updated + HZ + HZ/2)
924 /* Fan clock dividers */
925 nct6775_update_fan_div_common(dev, data);
927 /* Measured voltages and limits */
928 for (i = 0; i < data->in_num; i++) {
929 if (!(data->have_in & (1 << i)))
932 data->in[i][0] = nct6775_read_value(data,
934 data->in[i][1] = nct6775_read_value(data,
935 data->REG_IN_MINMAX[0][i]);
936 data->in[i][2] = nct6775_read_value(data,
937 data->REG_IN_MINMAX[1][i]);
940 /* Measured fan speeds and limits */
941 for (i = 0; i < 5; i++) {
944 if (!(data->has_fan & (1 << i)))
947 reg = nct6775_read_value(data, data->REG_FAN[i]);
948 data->rpm[i] = data->fan_from_reg(reg,
951 if (data->has_fan_min & (1 << i))
952 data->fan_min[i] = nct6775_read_value(data,
953 data->REG_FAN_MIN[i]);
956 * If we failed to measure the fan speed and clock
957 * divider can be increased, let's try that for next
960 if (data->has_fan_div
961 && (reg >= 0xff || (data->kind == nct6775
963 && data->fan_div[i] < 0x07) {
964 dev_dbg(dev, "Increasing fan%d "
965 "clock divider from %u to %u\n",
966 i + 1, div_from_reg(data->fan_div[i]),
967 div_from_reg(data->fan_div[i] + 1));
969 nct6775_write_fan_div_common(dev, data, i);
970 /* Preserve min limit if possible */
971 if ((data->has_fan_min & (1 << i))
972 && data->fan_min[i] >= 2
973 && data->fan_min[i] != 255)
974 nct6775_write_value(data,
975 data->REG_FAN_MIN[i],
976 (data->fan_min[i] /= 2));
980 nct6775_update_pwm(dev);
981 nct6775_update_pwm_limits(dev);
983 /* Measured temperatures and limits */
984 for (i = 0; i < NUM_TEMP; i++) {
985 if (!(data->have_temp & (1 << i)))
987 for (j = 0; j < 3; j++) {
988 if (data->reg_temp[j][i])
990 = nct6775_read_temp(data,
991 data->reg_temp[j][i]);
993 if (!(data->have_temp_fixed & (1 << i)))
996 = nct6775_read_value(data, data->REG_TEMP_OFFSET[i]);
1000 for (i = 0; i < 6; i++) {
1002 if (!data->REG_ALARM[i])
1004 alarm = nct6775_read_value(data, data->REG_ALARM[i]);
1005 data->alarms |= ((u64)alarm) << (i << 3);
1008 data->last_updated = jiffies;
1012 mutex_unlock(&data->update_lock);
1017 * Sysfs callback functions
1020 show_in_reg(struct device *dev, struct device_attribute *attr, char *buf)
1022 struct nct6775_data *data = nct6775_update_device(dev);
1023 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
1025 int index = sattr->index;
1026 return sprintf(buf, "%ld\n", in_from_reg(data->in[nr][index], nr));
1030 store_in_reg(struct device *dev, struct device_attribute *attr, const char *buf,
1033 struct nct6775_data *data = dev_get_drvdata(dev);
1034 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
1036 int index = sattr->index;
1038 int err = kstrtoul(buf, 10, &val);
1041 mutex_lock(&data->update_lock);
1042 data->in[nr][index] = in_to_reg(val, nr);
1043 nct6775_write_value(data, data->REG_IN_MINMAX[index-1][nr],
1044 data->in[nr][index]);
1045 mutex_unlock(&data->update_lock);
1050 show_alarm(struct device *dev, struct device_attribute *attr, char *buf)
1052 struct nct6775_data *data = nct6775_update_device(dev);
1053 struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr);
1054 int nr = data->ALARM_BITS[sattr->index];
1055 return sprintf(buf, "%u\n",
1056 (unsigned int)((data->alarms >> nr) & 0x01));
1059 static SENSOR_DEVICE_ATTR_2(in0_input, S_IRUGO, show_in_reg, NULL, 0, 0);
1060 static SENSOR_DEVICE_ATTR_2(in1_input, S_IRUGO, show_in_reg, NULL, 1, 0);
1061 static SENSOR_DEVICE_ATTR_2(in2_input, S_IRUGO, show_in_reg, NULL, 2, 0);
1062 static SENSOR_DEVICE_ATTR_2(in3_input, S_IRUGO, show_in_reg, NULL, 3, 0);
1063 static SENSOR_DEVICE_ATTR_2(in4_input, S_IRUGO, show_in_reg, NULL, 4, 0);
1064 static SENSOR_DEVICE_ATTR_2(in5_input, S_IRUGO, show_in_reg, NULL, 5, 0);
1065 static SENSOR_DEVICE_ATTR_2(in6_input, S_IRUGO, show_in_reg, NULL, 6, 0);
1066 static SENSOR_DEVICE_ATTR_2(in7_input, S_IRUGO, show_in_reg, NULL, 7, 0);
1067 static SENSOR_DEVICE_ATTR_2(in8_input, S_IRUGO, show_in_reg, NULL, 8, 0);
1068 static SENSOR_DEVICE_ATTR_2(in9_input, S_IRUGO, show_in_reg, NULL, 9, 0);
1069 static SENSOR_DEVICE_ATTR_2(in10_input, S_IRUGO, show_in_reg, NULL, 10, 0);
1070 static SENSOR_DEVICE_ATTR_2(in11_input, S_IRUGO, show_in_reg, NULL, 11, 0);
1071 static SENSOR_DEVICE_ATTR_2(in12_input, S_IRUGO, show_in_reg, NULL, 12, 0);
1072 static SENSOR_DEVICE_ATTR_2(in13_input, S_IRUGO, show_in_reg, NULL, 13, 0);
1073 static SENSOR_DEVICE_ATTR_2(in14_input, S_IRUGO, show_in_reg, NULL, 14, 0);
1075 static SENSOR_DEVICE_ATTR(in0_alarm, S_IRUGO, show_alarm, NULL, 0);
1076 static SENSOR_DEVICE_ATTR(in1_alarm, S_IRUGO, show_alarm, NULL, 1);
1077 static SENSOR_DEVICE_ATTR(in2_alarm, S_IRUGO, show_alarm, NULL, 2);
1078 static SENSOR_DEVICE_ATTR(in3_alarm, S_IRUGO, show_alarm, NULL, 3);
1079 static SENSOR_DEVICE_ATTR(in4_alarm, S_IRUGO, show_alarm, NULL, 4);
1080 static SENSOR_DEVICE_ATTR(in5_alarm, S_IRUGO, show_alarm, NULL, 5);
1081 static SENSOR_DEVICE_ATTR(in6_alarm, S_IRUGO, show_alarm, NULL, 6);
1082 static SENSOR_DEVICE_ATTR(in7_alarm, S_IRUGO, show_alarm, NULL, 7);
1083 static SENSOR_DEVICE_ATTR(in8_alarm, S_IRUGO, show_alarm, NULL, 8);
1084 static SENSOR_DEVICE_ATTR(in9_alarm, S_IRUGO, show_alarm, NULL, 9);
1085 static SENSOR_DEVICE_ATTR(in10_alarm, S_IRUGO, show_alarm, NULL, 10);
1086 static SENSOR_DEVICE_ATTR(in11_alarm, S_IRUGO, show_alarm, NULL, 11);
1087 static SENSOR_DEVICE_ATTR(in12_alarm, S_IRUGO, show_alarm, NULL, 12);
1088 static SENSOR_DEVICE_ATTR(in13_alarm, S_IRUGO, show_alarm, NULL, 13);
1089 static SENSOR_DEVICE_ATTR(in14_alarm, S_IRUGO, show_alarm, NULL, 14);
1091 static SENSOR_DEVICE_ATTR_2(in0_min, S_IWUSR | S_IRUGO, show_in_reg,
1092 store_in_reg, 0, 1);
1093 static SENSOR_DEVICE_ATTR_2(in1_min, S_IWUSR | S_IRUGO, show_in_reg,
1094 store_in_reg, 1, 1);
1095 static SENSOR_DEVICE_ATTR_2(in2_min, S_IWUSR | S_IRUGO, show_in_reg,
1096 store_in_reg, 2, 1);
1097 static SENSOR_DEVICE_ATTR_2(in3_min, S_IWUSR | S_IRUGO, show_in_reg,
1098 store_in_reg, 3, 1);
1099 static SENSOR_DEVICE_ATTR_2(in4_min, S_IWUSR | S_IRUGO, show_in_reg,
1100 store_in_reg, 4, 1);
1101 static SENSOR_DEVICE_ATTR_2(in5_min, S_IWUSR | S_IRUGO, show_in_reg,
1102 store_in_reg, 5, 1);
1103 static SENSOR_DEVICE_ATTR_2(in6_min, S_IWUSR | S_IRUGO, show_in_reg,
1104 store_in_reg, 6, 1);
1105 static SENSOR_DEVICE_ATTR_2(in7_min, S_IWUSR | S_IRUGO, show_in_reg,
1106 store_in_reg, 7, 1);
1107 static SENSOR_DEVICE_ATTR_2(in8_min, S_IWUSR | S_IRUGO, show_in_reg,
1108 store_in_reg, 8, 1);
1109 static SENSOR_DEVICE_ATTR_2(in9_min, S_IWUSR | S_IRUGO, show_in_reg,
1110 store_in_reg, 9, 1);
1111 static SENSOR_DEVICE_ATTR_2(in10_min, S_IWUSR | S_IRUGO, show_in_reg,
1112 store_in_reg, 10, 1);
1113 static SENSOR_DEVICE_ATTR_2(in11_min, S_IWUSR | S_IRUGO, show_in_reg,
1114 store_in_reg, 11, 1);
1115 static SENSOR_DEVICE_ATTR_2(in12_min, S_IWUSR | S_IRUGO, show_in_reg,
1116 store_in_reg, 12, 1);
1117 static SENSOR_DEVICE_ATTR_2(in13_min, S_IWUSR | S_IRUGO, show_in_reg,
1118 store_in_reg, 13, 1);
1119 static SENSOR_DEVICE_ATTR_2(in14_min, S_IWUSR | S_IRUGO, show_in_reg,
1120 store_in_reg, 14, 1);
1122 static SENSOR_DEVICE_ATTR_2(in0_max, S_IWUSR | S_IRUGO, show_in_reg,
1123 store_in_reg, 0, 2);
1124 static SENSOR_DEVICE_ATTR_2(in1_max, S_IWUSR | S_IRUGO, show_in_reg,
1125 store_in_reg, 1, 2);
1126 static SENSOR_DEVICE_ATTR_2(in2_max, S_IWUSR | S_IRUGO, show_in_reg,
1127 store_in_reg, 2, 2);
1128 static SENSOR_DEVICE_ATTR_2(in3_max, S_IWUSR | S_IRUGO, show_in_reg,
1129 store_in_reg, 3, 2);
1130 static SENSOR_DEVICE_ATTR_2(in4_max, S_IWUSR | S_IRUGO, show_in_reg,
1131 store_in_reg, 4, 2);
1132 static SENSOR_DEVICE_ATTR_2(in5_max, S_IWUSR | S_IRUGO, show_in_reg,
1133 store_in_reg, 5, 2);
1134 static SENSOR_DEVICE_ATTR_2(in6_max, S_IWUSR | S_IRUGO, show_in_reg,
1135 store_in_reg, 6, 2);
1136 static SENSOR_DEVICE_ATTR_2(in7_max, S_IWUSR | S_IRUGO, show_in_reg,
1137 store_in_reg, 7, 2);
1138 static SENSOR_DEVICE_ATTR_2(in8_max, S_IWUSR | S_IRUGO, show_in_reg,
1139 store_in_reg, 8, 2);
1140 static SENSOR_DEVICE_ATTR_2(in9_max, S_IWUSR | S_IRUGO, show_in_reg,
1141 store_in_reg, 9, 2);
1142 static SENSOR_DEVICE_ATTR_2(in10_max, S_IWUSR | S_IRUGO, show_in_reg,
1143 store_in_reg, 10, 2);
1144 static SENSOR_DEVICE_ATTR_2(in11_max, S_IWUSR | S_IRUGO, show_in_reg,
1145 store_in_reg, 11, 2);
1146 static SENSOR_DEVICE_ATTR_2(in12_max, S_IWUSR | S_IRUGO, show_in_reg,
1147 store_in_reg, 12, 2);
1148 static SENSOR_DEVICE_ATTR_2(in13_max, S_IWUSR | S_IRUGO, show_in_reg,
1149 store_in_reg, 13, 2);
1150 static SENSOR_DEVICE_ATTR_2(in14_max, S_IWUSR | S_IRUGO, show_in_reg,
1151 store_in_reg, 14, 2);
1153 static struct attribute *nct6775_attributes_in[15][5] = {
1155 &sensor_dev_attr_in0_input.dev_attr.attr,
1156 &sensor_dev_attr_in0_min.dev_attr.attr,
1157 &sensor_dev_attr_in0_max.dev_attr.attr,
1158 &sensor_dev_attr_in0_alarm.dev_attr.attr,
1162 &sensor_dev_attr_in1_input.dev_attr.attr,
1163 &sensor_dev_attr_in1_min.dev_attr.attr,
1164 &sensor_dev_attr_in1_max.dev_attr.attr,
1165 &sensor_dev_attr_in1_alarm.dev_attr.attr,
1169 &sensor_dev_attr_in2_input.dev_attr.attr,
1170 &sensor_dev_attr_in2_min.dev_attr.attr,
1171 &sensor_dev_attr_in2_max.dev_attr.attr,
1172 &sensor_dev_attr_in2_alarm.dev_attr.attr,
1176 &sensor_dev_attr_in3_input.dev_attr.attr,
1177 &sensor_dev_attr_in3_min.dev_attr.attr,
1178 &sensor_dev_attr_in3_max.dev_attr.attr,
1179 &sensor_dev_attr_in3_alarm.dev_attr.attr,
1183 &sensor_dev_attr_in4_input.dev_attr.attr,
1184 &sensor_dev_attr_in4_min.dev_attr.attr,
1185 &sensor_dev_attr_in4_max.dev_attr.attr,
1186 &sensor_dev_attr_in4_alarm.dev_attr.attr,
1190 &sensor_dev_attr_in5_input.dev_attr.attr,
1191 &sensor_dev_attr_in5_min.dev_attr.attr,
1192 &sensor_dev_attr_in5_max.dev_attr.attr,
1193 &sensor_dev_attr_in5_alarm.dev_attr.attr,
1197 &sensor_dev_attr_in6_input.dev_attr.attr,
1198 &sensor_dev_attr_in6_min.dev_attr.attr,
1199 &sensor_dev_attr_in6_max.dev_attr.attr,
1200 &sensor_dev_attr_in6_alarm.dev_attr.attr,
1204 &sensor_dev_attr_in7_input.dev_attr.attr,
1205 &sensor_dev_attr_in7_min.dev_attr.attr,
1206 &sensor_dev_attr_in7_max.dev_attr.attr,
1207 &sensor_dev_attr_in7_alarm.dev_attr.attr,
1211 &sensor_dev_attr_in8_input.dev_attr.attr,
1212 &sensor_dev_attr_in8_min.dev_attr.attr,
1213 &sensor_dev_attr_in8_max.dev_attr.attr,
1214 &sensor_dev_attr_in8_alarm.dev_attr.attr,
1218 &sensor_dev_attr_in9_input.dev_attr.attr,
1219 &sensor_dev_attr_in9_min.dev_attr.attr,
1220 &sensor_dev_attr_in9_max.dev_attr.attr,
1221 &sensor_dev_attr_in9_alarm.dev_attr.attr,
1225 &sensor_dev_attr_in10_input.dev_attr.attr,
1226 &sensor_dev_attr_in10_min.dev_attr.attr,
1227 &sensor_dev_attr_in10_max.dev_attr.attr,
1228 &sensor_dev_attr_in10_alarm.dev_attr.attr,
1232 &sensor_dev_attr_in11_input.dev_attr.attr,
1233 &sensor_dev_attr_in11_min.dev_attr.attr,
1234 &sensor_dev_attr_in11_max.dev_attr.attr,
1235 &sensor_dev_attr_in11_alarm.dev_attr.attr,
1239 &sensor_dev_attr_in12_input.dev_attr.attr,
1240 &sensor_dev_attr_in12_min.dev_attr.attr,
1241 &sensor_dev_attr_in12_max.dev_attr.attr,
1242 &sensor_dev_attr_in12_alarm.dev_attr.attr,
1246 &sensor_dev_attr_in13_input.dev_attr.attr,
1247 &sensor_dev_attr_in13_min.dev_attr.attr,
1248 &sensor_dev_attr_in13_max.dev_attr.attr,
1249 &sensor_dev_attr_in13_alarm.dev_attr.attr,
1253 &sensor_dev_attr_in14_input.dev_attr.attr,
1254 &sensor_dev_attr_in14_min.dev_attr.attr,
1255 &sensor_dev_attr_in14_max.dev_attr.attr,
1256 &sensor_dev_attr_in14_alarm.dev_attr.attr,
1261 static const struct attribute_group nct6775_group_in[15] = {
1262 { .attrs = nct6775_attributes_in[0] },
1263 { .attrs = nct6775_attributes_in[1] },
1264 { .attrs = nct6775_attributes_in[2] },
1265 { .attrs = nct6775_attributes_in[3] },
1266 { .attrs = nct6775_attributes_in[4] },
1267 { .attrs = nct6775_attributes_in[5] },
1268 { .attrs = nct6775_attributes_in[6] },
1269 { .attrs = nct6775_attributes_in[7] },
1270 { .attrs = nct6775_attributes_in[8] },
1271 { .attrs = nct6775_attributes_in[9] },
1272 { .attrs = nct6775_attributes_in[10] },
1273 { .attrs = nct6775_attributes_in[11] },
1274 { .attrs = nct6775_attributes_in[12] },
1275 { .attrs = nct6775_attributes_in[13] },
1276 { .attrs = nct6775_attributes_in[14] },
1280 show_fan(struct device *dev, struct device_attribute *attr, char *buf)
1282 struct nct6775_data *data = nct6775_update_device(dev);
1283 struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr);
1284 int nr = sattr->index;
1285 return sprintf(buf, "%d\n", data->rpm[nr]);
1289 show_fan_min(struct device *dev, struct device_attribute *attr, char *buf)
1291 struct nct6775_data *data = nct6775_update_device(dev);
1292 struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr);
1293 int nr = sattr->index;
1294 return sprintf(buf, "%d\n",
1295 data->fan_from_reg_min(data->fan_min[nr],
1296 data->fan_div[nr]));
1300 show_fan_div(struct device *dev, struct device_attribute *attr, char *buf)
1302 struct nct6775_data *data = nct6775_update_device(dev);
1303 struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr);
1304 int nr = sattr->index;
1305 return sprintf(buf, "%u\n", div_from_reg(data->fan_div[nr]));
1309 store_fan_min(struct device *dev, struct device_attribute *attr,
1310 const char *buf, size_t count)
1312 struct nct6775_data *data = dev_get_drvdata(dev);
1313 struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr);
1314 int nr = sattr->index;
1320 err = kstrtoul(buf, 10, &val);
1324 mutex_lock(&data->update_lock);
1325 if (!data->has_fan_div) {
1327 * Only NCT6776F for now, so we know that this is a 13 bit
1335 val = 1350000U / val;
1336 val = (val & 0x1f) | ((val << 3) & 0xff00);
1338 data->fan_min[nr] = val;
1339 goto write_min; /* Leave fan divider alone */
1342 /* No min limit, alarm disabled */
1343 data->fan_min[nr] = 255;
1344 new_div = data->fan_div[nr]; /* No change */
1345 dev_info(dev, "fan%u low limit and alarm disabled\n", nr + 1);
1348 reg = 1350000U / val;
1349 if (reg >= 128 * 255) {
1351 * Speed below this value cannot possibly be represented,
1352 * even with the highest divider (128)
1354 data->fan_min[nr] = 254;
1355 new_div = 7; /* 128 == (1 << 7) */
1356 dev_warn(dev, "fan%u low limit %lu below minimum %u, set to "
1357 "minimum\n", nr + 1, val,
1358 data->fan_from_reg_min(254, 7));
1361 * Speed above this value cannot possibly be represented,
1362 * even with the lowest divider (1)
1364 data->fan_min[nr] = 1;
1365 new_div = 0; /* 1 == (1 << 0) */
1366 dev_warn(dev, "fan%u low limit %lu above maximum %u, set to "
1367 "maximum\n", nr + 1, val,
1368 data->fan_from_reg_min(1, 0));
1371 * Automatically pick the best divider, i.e. the one such
1372 * that the min limit will correspond to a register value
1373 * in the 96..192 range
1376 while (reg > 192 && new_div < 7) {
1380 data->fan_min[nr] = reg;
1385 * Write both the fan clock divider (if it changed) and the new
1386 * fan min (unconditionally)
1388 if (new_div != data->fan_div[nr]) {
1389 dev_dbg(dev, "fan%u clock divider changed from %u to %u\n",
1390 nr + 1, div_from_reg(data->fan_div[nr]),
1391 div_from_reg(new_div));
1392 data->fan_div[nr] = new_div;
1393 nct6775_write_fan_div_common(dev, data, nr);
1394 /* Give the chip time to sample a new speed value */
1395 data->last_updated = jiffies;
1399 nct6775_write_value(data, data->REG_FAN_MIN[nr],
1401 mutex_unlock(&data->update_lock);
1406 static struct sensor_device_attribute sda_fan_input[] = {
1407 SENSOR_ATTR(fan1_input, S_IRUGO, show_fan, NULL, 0),
1408 SENSOR_ATTR(fan2_input, S_IRUGO, show_fan, NULL, 1),
1409 SENSOR_ATTR(fan3_input, S_IRUGO, show_fan, NULL, 2),
1410 SENSOR_ATTR(fan4_input, S_IRUGO, show_fan, NULL, 3),
1411 SENSOR_ATTR(fan5_input, S_IRUGO, show_fan, NULL, 4),
1414 static struct sensor_device_attribute sda_fan_alarm[] = {
1415 SENSOR_ATTR(fan1_alarm, S_IRUGO, show_alarm, NULL, FAN_ALARM_BASE),
1416 SENSOR_ATTR(fan2_alarm, S_IRUGO, show_alarm, NULL, FAN_ALARM_BASE + 1),
1417 SENSOR_ATTR(fan3_alarm, S_IRUGO, show_alarm, NULL, FAN_ALARM_BASE + 2),
1418 SENSOR_ATTR(fan4_alarm, S_IRUGO, show_alarm, NULL, FAN_ALARM_BASE + 3),
1419 SENSOR_ATTR(fan5_alarm, S_IRUGO, show_alarm, NULL, FAN_ALARM_BASE + 4),
1422 static struct sensor_device_attribute sda_fan_min[] = {
1423 SENSOR_ATTR(fan1_min, S_IWUSR | S_IRUGO, show_fan_min,
1425 SENSOR_ATTR(fan2_min, S_IWUSR | S_IRUGO, show_fan_min,
1427 SENSOR_ATTR(fan3_min, S_IWUSR | S_IRUGO, show_fan_min,
1429 SENSOR_ATTR(fan4_min, S_IWUSR | S_IRUGO, show_fan_min,
1431 SENSOR_ATTR(fan5_min, S_IWUSR | S_IRUGO, show_fan_min,
1435 static struct sensor_device_attribute sda_fan_div[] = {
1436 SENSOR_ATTR(fan1_div, S_IRUGO, show_fan_div, NULL, 0),
1437 SENSOR_ATTR(fan2_div, S_IRUGO, show_fan_div, NULL, 1),
1438 SENSOR_ATTR(fan3_div, S_IRUGO, show_fan_div, NULL, 2),
1439 SENSOR_ATTR(fan4_div, S_IRUGO, show_fan_div, NULL, 3),
1440 SENSOR_ATTR(fan5_div, S_IRUGO, show_fan_div, NULL, 4),
1444 show_temp_label(struct device *dev, struct device_attribute *attr, char *buf)
1446 struct nct6775_data *data = nct6775_update_device(dev);
1447 struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr);
1448 int nr = sattr->index;
1449 return sprintf(buf, "%s\n", data->temp_label[data->temp_src[nr]]);
1453 show_temp(struct device *dev, struct device_attribute *attr, char *buf)
1455 struct nct6775_data *data = nct6775_update_device(dev);
1456 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
1458 int index = sattr->index;
1460 return sprintf(buf, "%d\n", LM75_TEMP_FROM_REG(data->temp[index][nr]));
1464 store_temp(struct device *dev, struct device_attribute *attr, const char *buf,
1467 struct nct6775_data *data = dev_get_drvdata(dev);
1468 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
1470 int index = sattr->index;
1474 err = kstrtol(buf, 10, &val);
1478 mutex_lock(&data->update_lock);
1479 data->temp[index][nr] = LM75_TEMP_TO_REG(val);
1480 nct6775_write_temp(data, data->reg_temp[index][nr],
1481 data->temp[index][nr]);
1482 mutex_unlock(&data->update_lock);
1487 show_temp_offset(struct device *dev, struct device_attribute *attr, char *buf)
1489 struct nct6775_data *data = nct6775_update_device(dev);
1490 struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr);
1492 return sprintf(buf, "%d\n", data->temp_offset[sattr->index] * 1000);
1496 store_temp_offset(struct device *dev, struct device_attribute *attr,
1497 const char *buf, size_t count)
1499 struct nct6775_data *data = dev_get_drvdata(dev);
1500 struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr);
1501 int nr = sattr->index;
1505 err = kstrtol(buf, 10, &val);
1509 val = SENSORS_LIMIT(DIV_ROUND_CLOSEST(val, 1000), -128, 127);
1511 mutex_lock(&data->update_lock);
1512 data->temp_offset[nr] = val;
1513 nct6775_write_value(data, data->REG_TEMP_OFFSET[nr], val);
1514 mutex_unlock(&data->update_lock);
1520 show_temp_type(struct device *dev, struct device_attribute *attr, char *buf)
1522 struct nct6775_data *data = nct6775_update_device(dev);
1523 struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr);
1524 int nr = sattr->index;
1525 return sprintf(buf, "%d\n", (int)data->temp_type[nr]);
1529 store_temp_type(struct device *dev, struct device_attribute *attr,
1530 const char *buf, size_t count)
1532 struct nct6775_data *data = nct6775_update_device(dev);
1533 struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr);
1534 int nr = sattr->index;
1537 u8 vbat, diode, bit;
1539 err = kstrtoul(buf, 10, &val);
1543 if (val != 1 && val != 3 && val != 4)
1546 mutex_lock(&data->update_lock);
1548 data->temp_type[nr] = val;
1549 vbat = nct6775_read_value(data, data->REG_VBAT) & ~(0x02 << nr);
1550 diode = nct6775_read_value(data, data->REG_DIODE) & ~(0x02 << nr);
1553 case 1: /* CPU diode (diode, current mode) */
1557 case 3: /* diode, voltage mode */
1560 case 4: /* thermistor */
1563 nct6775_write_value(data, data->REG_VBAT, vbat);
1564 nct6775_write_value(data, data->REG_DIODE, diode);
1566 mutex_unlock(&data->update_lock);
1570 static struct sensor_device_attribute_2 sda_temp_input[] = {
1571 SENSOR_ATTR_2(temp1_input, S_IRUGO, show_temp, NULL, 0, 0),
1572 SENSOR_ATTR_2(temp2_input, S_IRUGO, show_temp, NULL, 1, 0),
1573 SENSOR_ATTR_2(temp3_input, S_IRUGO, show_temp, NULL, 2, 0),
1574 SENSOR_ATTR_2(temp4_input, S_IRUGO, show_temp, NULL, 3, 0),
1575 SENSOR_ATTR_2(temp5_input, S_IRUGO, show_temp, NULL, 4, 0),
1576 SENSOR_ATTR_2(temp6_input, S_IRUGO, show_temp, NULL, 5, 0),
1577 SENSOR_ATTR_2(temp7_input, S_IRUGO, show_temp, NULL, 6, 0),
1578 SENSOR_ATTR_2(temp8_input, S_IRUGO, show_temp, NULL, 7, 0),
1579 SENSOR_ATTR_2(temp9_input, S_IRUGO, show_temp, NULL, 8, 0),
1580 SENSOR_ATTR_2(temp10_input, S_IRUGO, show_temp, NULL, 9, 0),
1583 static struct sensor_device_attribute sda_temp_label[] = {
1584 SENSOR_ATTR(temp1_label, S_IRUGO, show_temp_label, NULL, 0),
1585 SENSOR_ATTR(temp2_label, S_IRUGO, show_temp_label, NULL, 1),
1586 SENSOR_ATTR(temp3_label, S_IRUGO, show_temp_label, NULL, 2),
1587 SENSOR_ATTR(temp4_label, S_IRUGO, show_temp_label, NULL, 3),
1588 SENSOR_ATTR(temp5_label, S_IRUGO, show_temp_label, NULL, 4),
1589 SENSOR_ATTR(temp6_label, S_IRUGO, show_temp_label, NULL, 5),
1590 SENSOR_ATTR(temp7_label, S_IRUGO, show_temp_label, NULL, 6),
1591 SENSOR_ATTR(temp8_label, S_IRUGO, show_temp_label, NULL, 7),
1592 SENSOR_ATTR(temp9_label, S_IRUGO, show_temp_label, NULL, 8),
1593 SENSOR_ATTR(temp10_label, S_IRUGO, show_temp_label, NULL, 9),
1596 static struct sensor_device_attribute_2 sda_temp_max[] = {
1597 SENSOR_ATTR_2(temp1_max, S_IRUGO | S_IWUSR, show_temp, store_temp,
1599 SENSOR_ATTR_2(temp2_max, S_IRUGO | S_IWUSR, show_temp, store_temp,
1601 SENSOR_ATTR_2(temp3_max, S_IRUGO | S_IWUSR, show_temp, store_temp,
1603 SENSOR_ATTR_2(temp4_max, S_IRUGO | S_IWUSR, show_temp, store_temp,
1605 SENSOR_ATTR_2(temp5_max, S_IRUGO | S_IWUSR, show_temp, store_temp,
1607 SENSOR_ATTR_2(temp6_max, S_IRUGO | S_IWUSR, show_temp, store_temp,
1609 SENSOR_ATTR_2(temp7_max, S_IRUGO | S_IWUSR, show_temp, store_temp,
1611 SENSOR_ATTR_2(temp8_max, S_IRUGO | S_IWUSR, show_temp, store_temp,
1613 SENSOR_ATTR_2(temp9_max, S_IRUGO | S_IWUSR, show_temp, store_temp,
1615 SENSOR_ATTR_2(temp10_max, S_IRUGO | S_IWUSR, show_temp, store_temp,
1619 static struct sensor_device_attribute_2 sda_temp_max_hyst[] = {
1620 SENSOR_ATTR_2(temp1_max_hyst, S_IRUGO | S_IWUSR, show_temp, store_temp,
1622 SENSOR_ATTR_2(temp2_max_hyst, S_IRUGO | S_IWUSR, show_temp, store_temp,
1624 SENSOR_ATTR_2(temp3_max_hyst, S_IRUGO | S_IWUSR, show_temp, store_temp,
1626 SENSOR_ATTR_2(temp4_max_hyst, S_IRUGO | S_IWUSR, show_temp, store_temp,
1628 SENSOR_ATTR_2(temp5_max_hyst, S_IRUGO | S_IWUSR, show_temp, store_temp,
1630 SENSOR_ATTR_2(temp6_max_hyst, S_IRUGO | S_IWUSR, show_temp, store_temp,
1632 SENSOR_ATTR_2(temp7_max_hyst, S_IRUGO | S_IWUSR, show_temp, store_temp,
1634 SENSOR_ATTR_2(temp8_max_hyst, S_IRUGO | S_IWUSR, show_temp, store_temp,
1636 SENSOR_ATTR_2(temp9_max_hyst, S_IRUGO | S_IWUSR, show_temp, store_temp,
1638 SENSOR_ATTR_2(temp9_max_hyst, S_IRUGO | S_IWUSR, show_temp, store_temp,
1642 static struct sensor_device_attribute sda_temp_offset[] = {
1643 SENSOR_ATTR(temp1_offset, S_IRUGO | S_IWUSR, show_temp_offset,
1644 store_temp_offset, 0),
1645 SENSOR_ATTR(temp2_offset, S_IRUGO | S_IWUSR, show_temp_offset,
1646 store_temp_offset, 1),
1647 SENSOR_ATTR(temp3_offset, S_IRUGO | S_IWUSR, show_temp_offset,
1648 store_temp_offset, 2),
1649 SENSOR_ATTR(temp4_offset, S_IRUGO | S_IWUSR, show_temp_offset,
1650 store_temp_offset, 3),
1651 SENSOR_ATTR(temp5_offset, S_IRUGO | S_IWUSR, show_temp_offset,
1652 store_temp_offset, 4),
1653 SENSOR_ATTR(temp6_offset, S_IRUGO | S_IWUSR, show_temp_offset,
1654 store_temp_offset, 5),
1657 static struct sensor_device_attribute sda_temp_type[] = {
1658 SENSOR_ATTR(temp1_type, S_IRUGO | S_IWUSR, show_temp_type,
1659 store_temp_type, 0),
1660 SENSOR_ATTR(temp2_type, S_IRUGO | S_IWUSR, show_temp_type,
1661 store_temp_type, 1),
1662 SENSOR_ATTR(temp3_type, S_IRUGO | S_IWUSR, show_temp_type,
1663 store_temp_type, 2),
1664 SENSOR_ATTR(temp4_type, S_IRUGO | S_IWUSR, show_temp_type,
1665 store_temp_type, 3),
1666 SENSOR_ATTR(temp5_type, S_IRUGO | S_IWUSR, show_temp_type,
1667 store_temp_type, 4),
1668 SENSOR_ATTR(temp6_type, S_IRUGO | S_IWUSR, show_temp_type,
1669 store_temp_type, 5),
1672 static struct sensor_device_attribute sda_temp_alarm[] = {
1673 SENSOR_ATTR(temp1_alarm, S_IRUGO, show_alarm, NULL,
1675 SENSOR_ATTR(temp2_alarm, S_IRUGO, show_alarm, NULL,
1676 TEMP_ALARM_BASE + 1),
1677 SENSOR_ATTR(temp3_alarm, S_IRUGO, show_alarm, NULL,
1678 TEMP_ALARM_BASE + 2),
1679 SENSOR_ATTR(temp4_alarm, S_IRUGO, show_alarm, NULL,
1680 TEMP_ALARM_BASE + 3),
1681 SENSOR_ATTR(temp5_alarm, S_IRUGO, show_alarm, NULL,
1682 TEMP_ALARM_BASE + 4),
1683 SENSOR_ATTR(temp6_alarm, S_IRUGO, show_alarm, NULL,
1684 TEMP_ALARM_BASE + 5),
1687 #define NUM_TEMP_ALARM ARRAY_SIZE(sda_temp_alarm)
1690 show_pwm_mode(struct device *dev, struct device_attribute *attr, char *buf)
1692 struct nct6775_data *data = nct6775_update_device(dev);
1693 struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr);
1695 return sprintf(buf, "%d\n", !data->pwm_mode[sattr->index]);
1699 store_pwm_mode(struct device *dev, struct device_attribute *attr,
1700 const char *buf, size_t count)
1702 struct nct6775_data *data = dev_get_drvdata(dev);
1703 struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr);
1704 int nr = sattr->index;
1709 err = kstrtoul(buf, 10, &val);
1716 /* Setting DC mode is not supported for all chips/channels */
1717 if (data->REG_PWM_MODE[nr] == 0) {
1723 mutex_lock(&data->update_lock);
1724 data->pwm_mode[nr] = val;
1725 reg = nct6775_read_value(data, data->REG_PWM_MODE[nr]);
1726 reg &= ~data->PWM_MODE_MASK[nr];
1728 reg |= data->PWM_MODE_MASK[nr];
1729 nct6775_write_value(data, data->REG_PWM_MODE[nr], reg);
1730 mutex_unlock(&data->update_lock);
1735 show_pwm_enable(struct device *dev, struct device_attribute *attr, char *buf)
1737 struct nct6775_data *data = nct6775_update_device(dev);
1738 struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr);
1740 return sprintf(buf, "%d\n", data->pwm_enable[sattr->index]);
1744 show_pwm(struct device *dev, struct device_attribute *attr, char *buf)
1746 struct nct6775_data *data = nct6775_update_device(dev);
1747 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
1749 int index = sattr->index;
1753 * For automatic fan control modes, show current pwm readings.
1754 * Otherwise, show the configured value.
1756 if (index == 0 && data->pwm_enable[nr] > 1)
1757 pwm = nct6775_read_value(data, data->REG_PWM_READ[nr]);
1759 pwm = data->pwm[index][nr];
1761 return sprintf(buf, "%d\n", pwm);
1765 store_pwm(struct device *dev, struct device_attribute *attr, const char *buf,
1768 struct nct6775_data *data = dev_get_drvdata(dev);
1769 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
1771 int index = sattr->index;
1773 int minval[7] = { 0, 1, 1, data->pwm[2][nr], 0, 0, 0 };
1775 = { 255, 255, data->pwm[3][nr] ? : 255, 255, 255, 255, 255 };
1778 err = kstrtoul(buf, 10, &val);
1781 val = SENSORS_LIMIT(val, minval[index], maxval[index]);
1783 mutex_lock(&data->update_lock);
1784 data->pwm[index][nr] = val;
1785 nct6775_write_value(data, data->REG_PWM[index][nr], val);
1786 mutex_unlock(&data->update_lock);
1790 /* Returns 0 if OK, -EINVAL otherwise */
1791 static int check_trip_points(struct nct6775_data *data, int nr)
1795 for (i = 0; i < data->auto_pwm_num - 1; i++) {
1796 if (data->auto_temp[nr][i] > data->auto_temp[nr][i + 1])
1799 for (i = 0; i < data->auto_pwm_num - 1; i++) {
1800 if (data->auto_pwm[nr][i] > data->auto_pwm[nr][i + 1])
1803 /* validate critical temperature and pwm if enabled (pwm > 0) */
1804 if (data->auto_pwm[nr][data->auto_pwm_num]) {
1805 if (data->auto_temp[nr][data->auto_pwm_num - 1] >
1806 data->auto_temp[nr][data->auto_pwm_num] ||
1807 data->auto_pwm[nr][data->auto_pwm_num - 1] >
1808 data->auto_pwm[nr][data->auto_pwm_num])
1815 store_pwm_enable(struct device *dev, struct device_attribute *attr,
1816 const char *buf, size_t count)
1818 struct nct6775_data *data = dev_get_drvdata(dev);
1819 struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr);
1820 int nr = sattr->index;
1825 err = kstrtoul(buf, 10, &val);
1832 if (val == 4 && data->kind != nct6775)
1835 if (val == 5 && check_trip_points(data, nr)) {
1836 dev_err(dev, "Inconsistent trip points, not switching to SmartFan IV mode\n");
1837 dev_err(dev, "Adjust trip points and try again\n");
1841 mutex_lock(&data->update_lock);
1842 data->pwm_enable[nr] = val;
1845 * turn off pwm control: select manual mode, set pwm to maximum
1847 data->pwm[0][nr] = 255;
1848 nct6775_write_value(data, data->REG_PWM[0][nr], 255);
1850 reg = nct6775_read_value(data, data->REG_FAN_MODE[nr]);
1852 reg |= pwm_enable_to_reg(val) << 4;
1853 nct6775_write_value(data, data->REG_FAN_MODE[nr], reg);
1854 mutex_unlock(&data->update_lock);
1859 show_pwm_temp(struct device *dev, struct device_attribute *attr, char *buf)
1861 struct nct6775_data *data = nct6775_update_device(dev);
1862 struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr);
1863 int nr = sattr->index;
1865 return sprintf(buf, "%d\n", LM75_TEMP_FROM_REG(data->pwm_temp[nr]));
1869 show_pwm_temp_sel(struct device *dev, struct device_attribute *attr, char *buf)
1871 struct nct6775_data *data = nct6775_update_device(dev);
1872 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
1874 return sprintf(buf, "%d\n",
1875 data->pwm_temp_sel[sattr->index][sattr->nr]);
1879 store_pwm_temp_sel(struct device *dev, struct device_attribute *attr,
1880 const char *buf, size_t count)
1882 struct nct6775_data *data = nct6775_update_device(dev);
1883 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
1885 int index = sattr->index;
1890 err = kstrtoul(buf, 10, &val);
1893 if (val == 0 || val > 0x1f)
1896 val = SENSORS_LIMIT(val, 1, data->temp_label_num - 1);
1898 if (!strlen(data->temp_label[val]))
1901 mutex_lock(&data->update_lock);
1902 data->pwm_temp_sel[index][nr] = val;
1903 reg = nct6775_read_value(data, data->REG_TEMP_SEL[index][nr]);
1906 nct6775_write_value(data, data->REG_TEMP_SEL[index][nr], reg);
1907 mutex_unlock(&data->update_lock);
1913 show_target_temp(struct device *dev, struct device_attribute *attr, char *buf)
1915 struct nct6775_data *data = nct6775_update_device(dev);
1916 struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr);
1918 return sprintf(buf, "%d\n",
1919 data->target_temp[sattr->index] * 1000);
1923 store_target_temp(struct device *dev, struct device_attribute *attr,
1924 const char *buf, size_t count)
1926 struct nct6775_data *data = dev_get_drvdata(dev);
1927 struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr);
1928 int nr = sattr->index;
1932 err = kstrtol(buf, 10, &val);
1936 val = SENSORS_LIMIT(DIV_ROUND_CLOSEST(val, 1000), 0, 127);
1938 mutex_lock(&data->update_lock);
1939 data->target_temp[nr] = val;
1940 nct6775_write_value(data, data->REG_TARGET[nr], val);
1941 mutex_unlock(&data->update_lock);
1946 show_auto_temp_hyst(struct device *dev, struct device_attribute *attr,
1949 struct nct6775_data *data = nct6775_update_device(dev);
1950 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
1952 int point = sattr->index >= data->auto_pwm_num ? 1 : 0;
1953 int tolerance = data->tolerance[nr][point];
1954 int temp = data->auto_temp[nr][sattr->index];
1956 return sprintf(buf, "%d\n", (temp - tolerance) * 1000);
1960 store_auto_temp_hyst(struct device *dev, struct device_attribute *attr,
1961 const char *buf, size_t count)
1963 struct nct6775_data *data = dev_get_drvdata(dev);
1964 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
1966 int point = sattr->index >= data->auto_pwm_num ? 1 : 0;
1970 int maxlimit[2][3] = { { 15, 7, 63 }, { 15, 7, 7 } };
1971 int mask[] = { 0x0f, 0x07, 0x07 };
1974 err = kstrtol(buf, 10, &val);
1978 temp = data->auto_temp[nr][sattr->index];
1979 val = temp - DIV_ROUND_CLOSEST(val, 1000);
1981 /* Limit tolerance as needed */
1982 val = SENSORS_LIMIT(val, 0, maxlimit[point][data->kind]);
1984 mutex_lock(&data->update_lock);
1986 nct6775_write_value(data,
1987 data->REG_CRITICAL_TEMP_TOLERANCE[nr],
1990 reg = nct6775_read_value(data, data->REG_FAN_MODE[nr]);
1991 reg = (reg & ~mask[nr]) | (val & mask[nr]);
1992 nct6775_write_value(data, data->REG_FAN_MODE[nr], reg);
1993 if (data->kind == nct6779) {
1994 reg = nct6775_read_value(data,
1995 NCT6779_REG_TOLERANCE_H[nr]);
1996 reg = (reg & 0x70) | ((val & 0x38) << 1);
1997 nct6775_write_value(data,
1998 NCT6779_REG_TOLERANCE_H[nr], reg);
2002 data->tolerance[nr][point] = val;
2003 mutex_unlock(&data->update_lock);
2007 static SENSOR_DEVICE_ATTR_2(pwm1, S_IWUSR | S_IRUGO, show_pwm, store_pwm, 0, 0);
2008 static SENSOR_DEVICE_ATTR_2(pwm2, S_IWUSR | S_IRUGO, show_pwm, store_pwm, 1, 0);
2009 static SENSOR_DEVICE_ATTR_2(pwm3, S_IWUSR | S_IRUGO, show_pwm, store_pwm, 2, 0);
2010 static SENSOR_DEVICE_ATTR_2(pwm4, S_IWUSR | S_IRUGO, show_pwm, store_pwm, 3, 0);
2011 static SENSOR_DEVICE_ATTR_2(pwm5, S_IWUSR | S_IRUGO, show_pwm, store_pwm, 4, 0);
2013 static SENSOR_DEVICE_ATTR(pwm1_mode, S_IWUSR | S_IRUGO, show_pwm_mode,
2015 static SENSOR_DEVICE_ATTR(pwm2_mode, S_IWUSR | S_IRUGO, show_pwm_mode,
2017 static SENSOR_DEVICE_ATTR(pwm3_mode, S_IWUSR | S_IRUGO, show_pwm_mode,
2019 static SENSOR_DEVICE_ATTR(pwm4_mode, S_IWUSR | S_IRUGO, show_pwm_mode,
2021 static SENSOR_DEVICE_ATTR(pwm5_mode, S_IWUSR | S_IRUGO, show_pwm_mode,
2024 static SENSOR_DEVICE_ATTR(pwm1_enable, S_IWUSR | S_IRUGO, show_pwm_enable,
2025 store_pwm_enable, 0);
2026 static SENSOR_DEVICE_ATTR(pwm2_enable, S_IWUSR | S_IRUGO, show_pwm_enable,
2027 store_pwm_enable, 1);
2028 static SENSOR_DEVICE_ATTR(pwm3_enable, S_IWUSR | S_IRUGO, show_pwm_enable,
2029 store_pwm_enable, 2);
2030 static SENSOR_DEVICE_ATTR(pwm4_enable, S_IWUSR | S_IRUGO, show_pwm_enable,
2031 store_pwm_enable, 3);
2032 static SENSOR_DEVICE_ATTR(pwm5_enable, S_IWUSR | S_IRUGO, show_pwm_enable,
2033 store_pwm_enable, 4);
2035 static SENSOR_DEVICE_ATTR_2(pwm1_temp_sel, S_IWUSR | S_IRUGO,
2036 show_pwm_temp_sel, store_pwm_temp_sel, 0, 0);
2037 static SENSOR_DEVICE_ATTR_2(pwm2_temp_sel, S_IWUSR | S_IRUGO,
2038 show_pwm_temp_sel, store_pwm_temp_sel, 1, 0);
2039 static SENSOR_DEVICE_ATTR_2(pwm3_temp_sel, S_IWUSR | S_IRUGO,
2040 show_pwm_temp_sel, store_pwm_temp_sel, 2, 0);
2041 static SENSOR_DEVICE_ATTR_2(pwm4_temp_sel, S_IWUSR | S_IRUGO,
2042 show_pwm_temp_sel, store_pwm_temp_sel, 3, 0);
2043 static SENSOR_DEVICE_ATTR_2(pwm5_temp_sel, S_IWUSR | S_IRUGO,
2044 show_pwm_temp_sel, store_pwm_temp_sel, 4, 0);
2046 static SENSOR_DEVICE_ATTR(pwm1_target, S_IWUSR | S_IRUGO, show_target_temp,
2047 store_target_temp, 0);
2048 static SENSOR_DEVICE_ATTR(pwm2_target, S_IWUSR | S_IRUGO, show_target_temp,
2049 store_target_temp, 1);
2050 static SENSOR_DEVICE_ATTR(pwm3_target, S_IWUSR | S_IRUGO, show_target_temp,
2051 store_target_temp, 2);
2052 static SENSOR_DEVICE_ATTR(pwm4_target, S_IWUSR | S_IRUGO, show_target_temp,
2053 store_target_temp, 3);
2054 static SENSOR_DEVICE_ATTR(pwm5_target, S_IWUSR | S_IRUGO, show_target_temp,
2055 store_target_temp, 4);
2057 /* Monitored pwm temperatures */
2058 static SENSOR_DEVICE_ATTR(temp11_input, S_IRUGO, show_pwm_temp, NULL, 0);
2059 static SENSOR_DEVICE_ATTR(temp12_input, S_IRUGO, show_pwm_temp, NULL, 1);
2060 static SENSOR_DEVICE_ATTR(temp13_input, S_IRUGO, show_pwm_temp, NULL, 2);
2061 static SENSOR_DEVICE_ATTR(temp14_input, S_IRUGO, show_pwm_temp, NULL, 3);
2062 static SENSOR_DEVICE_ATTR(temp15_input, S_IRUGO, show_pwm_temp, NULL, 4);
2064 /* Smart Fan registers */
2067 show_pwm_sel_enable(struct device *dev, struct device_attribute *attr,
2070 struct nct6775_data *data = nct6775_update_device(dev);
2071 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
2073 return sprintf(buf, "%d\n",
2074 data->pwm_sel_enable[sattr->index][sattr->nr]);
2078 store_pwm_sel_enable(struct device *dev, struct device_attribute *attr,
2079 const char *buf, size_t count)
2081 struct nct6775_data *data = dev_get_drvdata(dev);
2082 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
2084 int index = sattr->index;
2089 err = kstrtoul(buf, 10, &val);
2096 mutex_lock(&data->update_lock);
2097 data->pwm_sel_enable[index][nr] = val;
2098 reg = nct6775_read_value(data, data->REG_TEMP_SEL[index][nr]);
2102 nct6775_write_value(data, data->REG_TEMP_SEL[index][nr], reg);
2103 mutex_unlock(&data->update_lock);
2108 show_weight_temp(struct device *dev, struct device_attribute *attr, char *buf)
2110 struct nct6775_data *data = nct6775_update_device(dev);
2111 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
2113 int index = sattr->index;
2115 return sprintf(buf, "%d\n", data->weight_temp[index][nr] * 1000);
2119 store_weight_temp(struct device *dev, struct device_attribute *attr,
2120 const char *buf, size_t count)
2122 struct nct6775_data *data = dev_get_drvdata(dev);
2123 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
2125 int index = sattr->index;
2129 err = kstrtoul(buf, 10, &val);
2133 val = SENSORS_LIMIT(DIV_ROUND_CLOSEST(val, 1000), 0, 255);
2135 mutex_lock(&data->update_lock);
2136 data->weight_temp[index][nr] = val;
2137 nct6775_write_value(data, data->REG_WEIGHT_TEMP[index][nr], val);
2138 mutex_unlock(&data->update_lock);
2142 static SENSOR_DEVICE_ATTR_2(pwm1_stop_output_enable, S_IWUSR | S_IRUGO,
2143 show_pwm_sel_enable, store_pwm_sel_enable, 0, 0);
2144 static SENSOR_DEVICE_ATTR_2(pwm2_stop_output_enable, S_IWUSR | S_IRUGO,
2145 show_pwm_sel_enable, store_pwm_sel_enable, 1, 0);
2146 static SENSOR_DEVICE_ATTR_2(pwm3_stop_output_enable, S_IWUSR | S_IRUGO,
2147 show_pwm_sel_enable, store_pwm_sel_enable, 2, 0);
2148 static SENSOR_DEVICE_ATTR_2(pwm4_stop_output_enable, S_IWUSR | S_IRUGO,
2149 show_pwm_sel_enable, store_pwm_sel_enable, 3, 0);
2150 static SENSOR_DEVICE_ATTR_2(pwm5_stop_output_enable, S_IWUSR | S_IRUGO,
2151 show_pwm_sel_enable, store_pwm_sel_enable, 4, 0);
2153 static SENSOR_DEVICE_ATTR_2(pwm1_weight_enable, S_IWUSR | S_IRUGO,
2154 show_pwm_sel_enable, store_pwm_sel_enable, 0, 1);
2155 static SENSOR_DEVICE_ATTR_2(pwm2_weight_enable, S_IWUSR | S_IRUGO,
2156 show_pwm_sel_enable, store_pwm_sel_enable, 1, 1);
2157 static SENSOR_DEVICE_ATTR_2(pwm3_weight_enable, S_IWUSR | S_IRUGO,
2158 show_pwm_sel_enable, store_pwm_sel_enable, 2, 1);
2159 static SENSOR_DEVICE_ATTR_2(pwm4_weight_enable, S_IWUSR | S_IRUGO,
2160 show_pwm_sel_enable, store_pwm_sel_enable, 3, 1);
2161 static SENSOR_DEVICE_ATTR_2(pwm5_weight_enable, S_IWUSR | S_IRUGO,
2162 show_pwm_sel_enable, store_pwm_sel_enable, 4, 1);
2164 static SENSOR_DEVICE_ATTR_2(pwm1_weight_temp_sel, S_IWUSR | S_IRUGO,
2165 show_pwm_temp_sel, store_pwm_temp_sel, 0, 1);
2166 static SENSOR_DEVICE_ATTR_2(pwm2_weight_temp_sel, S_IWUSR | S_IRUGO,
2167 show_pwm_temp_sel, store_pwm_temp_sel, 1, 1);
2168 static SENSOR_DEVICE_ATTR_2(pwm3_weight_temp_sel, S_IWUSR | S_IRUGO,
2169 show_pwm_temp_sel, store_pwm_temp_sel, 2, 1);
2170 static SENSOR_DEVICE_ATTR_2(pwm4_weight_temp_sel, S_IWUSR | S_IRUGO,
2171 show_pwm_temp_sel, store_pwm_temp_sel, 3, 1);
2172 static SENSOR_DEVICE_ATTR_2(pwm5_weight_temp_sel, S_IWUSR | S_IRUGO,
2173 show_pwm_temp_sel, store_pwm_temp_sel, 4, 1);
2175 static SENSOR_DEVICE_ATTR_2(pwm1_weight_temp_step, S_IWUSR | S_IRUGO,
2176 show_weight_temp, store_weight_temp, 0, 0);
2177 static SENSOR_DEVICE_ATTR_2(pwm2_weight_temp_step, S_IWUSR | S_IRUGO,
2178 show_weight_temp, store_weight_temp, 1, 0);
2179 static SENSOR_DEVICE_ATTR_2(pwm3_weight_temp_step, S_IWUSR | S_IRUGO,
2180 show_weight_temp, store_weight_temp, 2, 0);
2181 static SENSOR_DEVICE_ATTR_2(pwm4_weight_temp_step, S_IWUSR | S_IRUGO,
2182 show_weight_temp, store_weight_temp, 3, 0);
2183 static SENSOR_DEVICE_ATTR_2(pwm5_weight_temp_step, S_IWUSR | S_IRUGO,
2184 show_weight_temp, store_weight_temp, 4, 0);
2186 static SENSOR_DEVICE_ATTR_2(pwm1_weight_temp_step_tol, S_IWUSR | S_IRUGO,
2187 show_weight_temp, store_weight_temp, 0, 1);
2188 static SENSOR_DEVICE_ATTR_2(pwm2_weight_temp_step_tol, S_IWUSR | S_IRUGO,
2189 show_weight_temp, store_weight_temp, 1, 1);
2190 static SENSOR_DEVICE_ATTR_2(pwm3_weight_temp_step_tol, S_IWUSR | S_IRUGO,
2191 show_weight_temp, store_weight_temp, 2, 1);
2192 static SENSOR_DEVICE_ATTR_2(pwm4_weight_temp_step_tol, S_IWUSR | S_IRUGO,
2193 show_weight_temp, store_weight_temp, 3, 1);
2194 static SENSOR_DEVICE_ATTR_2(pwm5_weight_temp_step_tol, S_IWUSR | S_IRUGO,
2195 show_weight_temp, store_weight_temp, 4, 1);
2197 static SENSOR_DEVICE_ATTR_2(pwm1_weight_temp_step_base, S_IWUSR | S_IRUGO,
2198 show_weight_temp, store_weight_temp, 0, 2);
2199 static SENSOR_DEVICE_ATTR_2(pwm2_weight_temp_step_base, S_IWUSR | S_IRUGO,
2200 show_weight_temp, store_weight_temp, 1, 2);
2201 static SENSOR_DEVICE_ATTR_2(pwm3_weight_temp_step_base, S_IWUSR | S_IRUGO,
2202 show_weight_temp, store_weight_temp, 2, 2);
2203 static SENSOR_DEVICE_ATTR_2(pwm4_weight_temp_step_base, S_IWUSR | S_IRUGO,
2204 show_weight_temp, store_weight_temp, 3, 2);
2205 static SENSOR_DEVICE_ATTR_2(pwm5_weight_temp_step_base, S_IWUSR | S_IRUGO,
2206 show_weight_temp, store_weight_temp, 4, 2);
2208 static SENSOR_DEVICE_ATTR_2(pwm1_weight_duty_step, S_IWUSR | S_IRUGO,
2209 show_pwm, store_pwm, 0, 5);
2210 static SENSOR_DEVICE_ATTR_2(pwm2_weight_duty_step, S_IWUSR | S_IRUGO,
2211 show_pwm, store_pwm, 1, 5);
2212 static SENSOR_DEVICE_ATTR_2(pwm3_weight_duty_step, S_IWUSR | S_IRUGO,
2213 show_pwm, store_pwm, 2, 5);
2214 static SENSOR_DEVICE_ATTR_2(pwm4_weight_duty_step, S_IWUSR | S_IRUGO,
2215 show_pwm, store_pwm, 3, 5);
2216 static SENSOR_DEVICE_ATTR_2(pwm5_weight_duty_step, S_IWUSR | S_IRUGO,
2217 show_pwm, store_pwm, 4, 5);
2219 /* duty_base is not supported on all chips */
2220 static struct sensor_device_attribute_2 sda_weight_duty_base[] = {
2221 SENSOR_ATTR_2(pwm1_weight_duty_base, S_IWUSR | S_IRUGO,
2222 show_pwm, store_pwm, 0, 6),
2223 SENSOR_ATTR_2(pwm2_weight_duty_base, S_IWUSR | S_IRUGO,
2224 show_pwm, store_pwm, 1, 6),
2225 SENSOR_ATTR_2(pwm3_weight_duty_base, S_IWUSR | S_IRUGO,
2226 show_pwm, store_pwm, 2, 6),
2227 SENSOR_ATTR_2(pwm4_weight_duty_base, S_IWUSR | S_IRUGO,
2228 show_pwm, store_pwm, 3, 6),
2229 SENSOR_ATTR_2(pwm5_weight_duty_base, S_IWUSR | S_IRUGO,
2230 show_pwm, store_pwm, 4, 6),
2234 show_fan_time(struct device *dev, struct device_attribute *attr, char *buf)
2236 struct nct6775_data *data = nct6775_update_device(dev);
2237 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
2239 int index = sattr->index;
2241 return sprintf(buf, "%d\n",
2242 step_time_from_reg(data->fan_time[index][nr],
2243 data->pwm_mode[nr]));
2247 store_fan_time(struct device *dev, struct device_attribute *attr,
2248 const char *buf, size_t count)
2250 struct nct6775_data *data = dev_get_drvdata(dev);
2251 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
2253 int index = sattr->index;
2257 err = kstrtoul(buf, 10, &val);
2261 val = step_time_to_reg(val, data->pwm_mode[nr]);
2262 mutex_lock(&data->update_lock);
2263 data->fan_time[index][nr] = val;
2264 nct6775_write_value(data, data->REG_FAN_TIME[index][nr], val);
2265 mutex_unlock(&data->update_lock);
2270 show_name(struct device *dev, struct device_attribute *attr, char *buf)
2272 struct nct6775_data *data = dev_get_drvdata(dev);
2274 return sprintf(buf, "%s\n", data->name);
2277 static DEVICE_ATTR(name, S_IRUGO, show_name, NULL);
2279 static SENSOR_DEVICE_ATTR_2(pwm1_stop_time, S_IWUSR | S_IRUGO, show_fan_time,
2280 store_fan_time, 0, 0);
2281 static SENSOR_DEVICE_ATTR_2(pwm2_stop_time, S_IWUSR | S_IRUGO, show_fan_time,
2282 store_fan_time, 1, 0);
2283 static SENSOR_DEVICE_ATTR_2(pwm3_stop_time, S_IWUSR | S_IRUGO, show_fan_time,
2284 store_fan_time, 2, 0);
2285 static SENSOR_DEVICE_ATTR_2(pwm4_stop_time, S_IWUSR | S_IRUGO, show_fan_time,
2286 store_fan_time, 3, 0);
2287 static SENSOR_DEVICE_ATTR_2(pwm5_stop_time, S_IWUSR | S_IRUGO, show_fan_time,
2288 store_fan_time, 4, 0);
2290 static SENSOR_DEVICE_ATTR_2(pwm1_step_up_time, S_IWUSR | S_IRUGO, show_fan_time,
2291 store_fan_time, 0, 1);
2292 static SENSOR_DEVICE_ATTR_2(pwm2_step_up_time, S_IWUSR | S_IRUGO, show_fan_time,
2293 store_fan_time, 1, 1);
2294 static SENSOR_DEVICE_ATTR_2(pwm3_step_up_time, S_IWUSR | S_IRUGO, show_fan_time,
2295 store_fan_time, 2, 1);
2296 static SENSOR_DEVICE_ATTR_2(pwm4_step_up_time, S_IWUSR | S_IRUGO, show_fan_time,
2297 store_fan_time, 3, 1);
2298 static SENSOR_DEVICE_ATTR_2(pwm5_step_up_time, S_IWUSR | S_IRUGO, show_fan_time,
2299 store_fan_time, 4, 1);
2301 static SENSOR_DEVICE_ATTR_2(pwm1_step_down_time, S_IWUSR | S_IRUGO,
2302 show_fan_time, store_fan_time, 0, 2);
2303 static SENSOR_DEVICE_ATTR_2(pwm2_step_down_time, S_IWUSR | S_IRUGO,
2304 show_fan_time, store_fan_time, 1, 2);
2305 static SENSOR_DEVICE_ATTR_2(pwm3_step_down_time, S_IWUSR | S_IRUGO,
2306 show_fan_time, store_fan_time, 2, 2);
2307 static SENSOR_DEVICE_ATTR_2(pwm4_step_down_time, S_IWUSR | S_IRUGO,
2308 show_fan_time, store_fan_time, 3, 2);
2309 static SENSOR_DEVICE_ATTR_2(pwm5_step_down_time, S_IWUSR | S_IRUGO,
2310 show_fan_time, store_fan_time, 4, 2);
2312 static SENSOR_DEVICE_ATTR_2(pwm1_start_output, S_IWUSR | S_IRUGO, show_pwm,
2314 static SENSOR_DEVICE_ATTR_2(pwm2_start_output, S_IWUSR | S_IRUGO, show_pwm,
2316 static SENSOR_DEVICE_ATTR_2(pwm3_start_output, S_IWUSR | S_IRUGO, show_pwm,
2318 static SENSOR_DEVICE_ATTR_2(pwm4_start_output, S_IWUSR | S_IRUGO, show_pwm,
2320 static SENSOR_DEVICE_ATTR_2(pwm5_start_output, S_IWUSR | S_IRUGO, show_pwm,
2323 static SENSOR_DEVICE_ATTR_2(pwm1_stop_output, S_IWUSR | S_IRUGO, show_pwm,
2325 static SENSOR_DEVICE_ATTR_2(pwm2_stop_output, S_IWUSR | S_IRUGO, show_pwm,
2327 static SENSOR_DEVICE_ATTR_2(pwm3_stop_output, S_IWUSR | S_IRUGO, show_pwm,
2329 static SENSOR_DEVICE_ATTR_2(pwm4_stop_output, S_IWUSR | S_IRUGO, show_pwm,
2331 static SENSOR_DEVICE_ATTR_2(pwm5_stop_output, S_IWUSR | S_IRUGO, show_pwm,
2334 /* max_output is not supported on all chips */
2335 static struct sensor_device_attribute_2 sda_max_output[] = {
2336 SENSOR_ATTR_2(pwm1_max_output, S_IWUSR | S_IRUGO, show_pwm, store_pwm,
2338 SENSOR_ATTR_2(pwm2_max_output, S_IWUSR | S_IRUGO, show_pwm, store_pwm,
2340 SENSOR_ATTR_2(pwm3_max_output, S_IWUSR | S_IRUGO, show_pwm, store_pwm,
2342 SENSOR_ATTR_2(pwm4_max_output, S_IWUSR | S_IRUGO, show_pwm, store_pwm,
2344 SENSOR_ATTR_2(pwm5_max_output, S_IWUSR | S_IRUGO, show_pwm, store_pwm,
2348 /* step_output is not supported on all chips */
2349 static struct sensor_device_attribute_2 sda_step_output[] = {
2350 SENSOR_ATTR_2(pwm1_step_output, S_IWUSR | S_IRUGO, show_pwm, store_pwm,
2352 SENSOR_ATTR_2(pwm2_step_output, S_IWUSR | S_IRUGO, show_pwm, store_pwm,
2354 SENSOR_ATTR_2(pwm3_step_output, S_IWUSR | S_IRUGO, show_pwm, store_pwm,
2356 SENSOR_ATTR_2(pwm4_step_output, S_IWUSR | S_IRUGO, show_pwm, store_pwm,
2358 SENSOR_ATTR_2(pwm5_step_output, S_IWUSR | S_IRUGO, show_pwm, store_pwm,
2362 static struct attribute *nct6775_attributes_pwm[5][20] = {
2364 &sensor_dev_attr_temp11_input.dev_attr.attr,
2365 &sensor_dev_attr_pwm1.dev_attr.attr,
2366 &sensor_dev_attr_pwm1_mode.dev_attr.attr,
2367 &sensor_dev_attr_pwm1_enable.dev_attr.attr,
2368 &sensor_dev_attr_pwm1_stop_output_enable.dev_attr.attr,
2369 &sensor_dev_attr_pwm1_weight_enable.dev_attr.attr,
2370 &sensor_dev_attr_pwm1_temp_sel.dev_attr.attr,
2371 &sensor_dev_attr_pwm1_target.dev_attr.attr,
2372 &sensor_dev_attr_pwm1_stop_time.dev_attr.attr,
2373 &sensor_dev_attr_pwm1_step_up_time.dev_attr.attr,
2374 &sensor_dev_attr_pwm1_step_down_time.dev_attr.attr,
2375 &sensor_dev_attr_pwm1_start_output.dev_attr.attr,
2376 &sensor_dev_attr_pwm1_stop_output.dev_attr.attr,
2377 &sensor_dev_attr_pwm1_weight_temp_sel.dev_attr.attr,
2378 &sensor_dev_attr_pwm1_weight_temp_step.dev_attr.attr,
2379 &sensor_dev_attr_pwm1_weight_temp_step_tol.dev_attr.attr,
2380 &sensor_dev_attr_pwm1_weight_temp_step_base.dev_attr.attr,
2381 &sensor_dev_attr_pwm1_weight_duty_step.dev_attr.attr,
2385 &sensor_dev_attr_temp12_input.dev_attr.attr,
2386 &sensor_dev_attr_pwm2.dev_attr.attr,
2387 &sensor_dev_attr_pwm2_mode.dev_attr.attr,
2388 &sensor_dev_attr_pwm2_enable.dev_attr.attr,
2389 &sensor_dev_attr_pwm2_stop_output_enable.dev_attr.attr,
2390 &sensor_dev_attr_pwm2_weight_enable.dev_attr.attr,
2391 &sensor_dev_attr_pwm2_temp_sel.dev_attr.attr,
2392 &sensor_dev_attr_pwm2_target.dev_attr.attr,
2393 &sensor_dev_attr_pwm2_stop_time.dev_attr.attr,
2394 &sensor_dev_attr_pwm2_step_up_time.dev_attr.attr,
2395 &sensor_dev_attr_pwm2_step_down_time.dev_attr.attr,
2396 &sensor_dev_attr_pwm2_start_output.dev_attr.attr,
2397 &sensor_dev_attr_pwm2_stop_output.dev_attr.attr,
2398 &sensor_dev_attr_pwm2_weight_temp_sel.dev_attr.attr,
2399 &sensor_dev_attr_pwm2_weight_temp_step.dev_attr.attr,
2400 &sensor_dev_attr_pwm2_weight_temp_step_tol.dev_attr.attr,
2401 &sensor_dev_attr_pwm2_weight_temp_step_base.dev_attr.attr,
2402 &sensor_dev_attr_pwm2_weight_duty_step.dev_attr.attr,
2406 &sensor_dev_attr_temp13_input.dev_attr.attr,
2407 &sensor_dev_attr_pwm3.dev_attr.attr,
2408 &sensor_dev_attr_pwm3_mode.dev_attr.attr,
2409 &sensor_dev_attr_pwm3_enable.dev_attr.attr,
2410 &sensor_dev_attr_pwm3_stop_output_enable.dev_attr.attr,
2411 &sensor_dev_attr_pwm3_weight_enable.dev_attr.attr,
2412 &sensor_dev_attr_pwm3_temp_sel.dev_attr.attr,
2413 &sensor_dev_attr_pwm3_target.dev_attr.attr,
2414 &sensor_dev_attr_pwm3_stop_time.dev_attr.attr,
2415 &sensor_dev_attr_pwm3_step_up_time.dev_attr.attr,
2416 &sensor_dev_attr_pwm3_step_down_time.dev_attr.attr,
2417 &sensor_dev_attr_pwm3_start_output.dev_attr.attr,
2418 &sensor_dev_attr_pwm3_stop_output.dev_attr.attr,
2419 &sensor_dev_attr_pwm3_weight_temp_sel.dev_attr.attr,
2420 &sensor_dev_attr_pwm3_weight_temp_step.dev_attr.attr,
2421 &sensor_dev_attr_pwm3_weight_temp_step_tol.dev_attr.attr,
2422 &sensor_dev_attr_pwm3_weight_temp_step_base.dev_attr.attr,
2423 &sensor_dev_attr_pwm3_weight_duty_step.dev_attr.attr,
2427 &sensor_dev_attr_temp14_input.dev_attr.attr,
2428 &sensor_dev_attr_pwm4.dev_attr.attr,
2429 &sensor_dev_attr_pwm4_mode.dev_attr.attr,
2430 &sensor_dev_attr_pwm4_enable.dev_attr.attr,
2431 &sensor_dev_attr_pwm4_stop_output_enable.dev_attr.attr,
2432 &sensor_dev_attr_pwm4_weight_enable.dev_attr.attr,
2433 &sensor_dev_attr_pwm4_temp_sel.dev_attr.attr,
2434 &sensor_dev_attr_pwm4_target.dev_attr.attr,
2435 &sensor_dev_attr_pwm4_stop_time.dev_attr.attr,
2436 &sensor_dev_attr_pwm4_step_up_time.dev_attr.attr,
2437 &sensor_dev_attr_pwm4_step_down_time.dev_attr.attr,
2438 &sensor_dev_attr_pwm4_start_output.dev_attr.attr,
2439 &sensor_dev_attr_pwm4_stop_output.dev_attr.attr,
2440 &sensor_dev_attr_pwm4_weight_temp_sel.dev_attr.attr,
2441 &sensor_dev_attr_pwm4_weight_temp_step.dev_attr.attr,
2442 &sensor_dev_attr_pwm4_weight_temp_step_tol.dev_attr.attr,
2443 &sensor_dev_attr_pwm4_weight_temp_step_base.dev_attr.attr,
2444 &sensor_dev_attr_pwm4_weight_duty_step.dev_attr.attr,
2448 &sensor_dev_attr_temp15_input.dev_attr.attr,
2449 &sensor_dev_attr_pwm5.dev_attr.attr,
2450 &sensor_dev_attr_pwm5_mode.dev_attr.attr,
2451 &sensor_dev_attr_pwm5_enable.dev_attr.attr,
2452 &sensor_dev_attr_pwm5_stop_output_enable.dev_attr.attr,
2453 &sensor_dev_attr_pwm5_weight_enable.dev_attr.attr,
2454 &sensor_dev_attr_pwm5_temp_sel.dev_attr.attr,
2455 &sensor_dev_attr_pwm5_target.dev_attr.attr,
2456 &sensor_dev_attr_pwm5_stop_time.dev_attr.attr,
2457 &sensor_dev_attr_pwm5_step_up_time.dev_attr.attr,
2458 &sensor_dev_attr_pwm5_step_down_time.dev_attr.attr,
2459 &sensor_dev_attr_pwm5_start_output.dev_attr.attr,
2460 &sensor_dev_attr_pwm5_stop_output.dev_attr.attr,
2461 &sensor_dev_attr_pwm5_weight_temp_sel.dev_attr.attr,
2462 &sensor_dev_attr_pwm5_weight_temp_step.dev_attr.attr,
2463 &sensor_dev_attr_pwm5_weight_temp_step_tol.dev_attr.attr,
2464 &sensor_dev_attr_pwm5_weight_temp_step_base.dev_attr.attr,
2465 &sensor_dev_attr_pwm5_weight_duty_step.dev_attr.attr,
2470 static const struct attribute_group nct6775_group_pwm[5] = {
2471 { .attrs = nct6775_attributes_pwm[0] },
2472 { .attrs = nct6775_attributes_pwm[1] },
2473 { .attrs = nct6775_attributes_pwm[2] },
2474 { .attrs = nct6775_attributes_pwm[3] },
2475 { .attrs = nct6775_attributes_pwm[4] },
2479 show_auto_pwm(struct device *dev, struct device_attribute *attr, char *buf)
2481 struct nct6775_data *data = nct6775_update_device(dev);
2482 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
2484 return sprintf(buf, "%d\n", data->auto_pwm[sattr->nr][sattr->index]);
2488 store_auto_pwm(struct device *dev, struct device_attribute *attr,
2489 const char *buf, size_t count)
2491 struct nct6775_data *data = dev_get_drvdata(dev);
2492 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
2494 int point = sattr->index;
2499 err = kstrtoul(buf, 10, &val);
2505 if (point == data->auto_pwm_num) {
2506 if (data->kind != nct6775 && !val)
2508 if (data->kind != nct6779 && val)
2512 mutex_lock(&data->update_lock);
2513 data->auto_pwm[nr][point] = val;
2514 if (point < data->auto_pwm_num) {
2515 nct6775_write_value(data,
2516 NCT6775_AUTO_PWM(data, nr, point),
2517 data->auto_pwm[nr][point]);
2519 switch (data->kind) {
2521 /* disable if needed (pwm == 0) */
2522 reg = nct6775_read_value(data,
2523 NCT6775_REG_CRITICAL_ENAB[nr]);
2528 nct6775_write_value(data, NCT6775_REG_CRITICAL_ENAB[nr],
2532 break; /* always enabled, nothing to do */
2534 nct6775_write_value(data, NCT6779_REG_CRITICAL_PWM[nr],
2536 reg = nct6775_read_value(data,
2537 NCT6779_REG_CRITICAL_PWM_ENABLE[nr]);
2542 nct6775_write_value(data,
2543 NCT6779_REG_CRITICAL_PWM_ENABLE[nr],
2548 mutex_unlock(&data->update_lock);
2553 show_auto_temp(struct device *dev, struct device_attribute *attr, char *buf)
2555 struct nct6775_data *data = nct6775_update_device(dev);
2556 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
2558 int point = sattr->index;
2561 * We don't know for sure if the temperature is signed or unsigned.
2562 * Assume it is unsigned.
2564 return sprintf(buf, "%d\n", data->auto_temp[nr][point] * 1000);
2568 store_auto_temp(struct device *dev, struct device_attribute *attr,
2569 const char *buf, size_t count)
2571 struct nct6775_data *data = dev_get_drvdata(dev);
2572 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
2574 int point = sattr->index;
2578 err = kstrtoul(buf, 10, &val);
2584 mutex_lock(&data->update_lock);
2585 data->auto_temp[nr][point] = DIV_ROUND_CLOSEST(val, 1000);
2586 if (point < data->auto_pwm_num) {
2587 nct6775_write_value(data,
2588 NCT6775_AUTO_TEMP(data, nr, point),
2589 data->auto_temp[nr][point]);
2591 nct6775_write_value(data, data->REG_CRITICAL_TEMP[nr],
2592 data->auto_temp[nr][point]);
2594 mutex_unlock(&data->update_lock);
2599 * The number of auto-point trip points is chip dependent.
2600 * Need to check support while generating/removing attribute files.
2602 static struct sensor_device_attribute_2 sda_auto_pwm_arrays[] = {
2603 SENSOR_ATTR_2(pwm1_auto_point1_pwm, S_IWUSR | S_IRUGO,
2604 show_auto_pwm, store_auto_pwm, 0, 0),
2605 SENSOR_ATTR_2(pwm1_auto_point1_temp, S_IWUSR | S_IRUGO,
2606 show_auto_temp, store_auto_temp, 0, 0),
2607 SENSOR_ATTR_2(pwm1_auto_point1_temp_hyst, S_IWUSR | S_IRUGO,
2608 show_auto_temp_hyst, store_auto_temp_hyst, 0, 0),
2609 SENSOR_ATTR_2(pwm1_auto_point2_pwm, S_IWUSR | S_IRUGO,
2610 show_auto_pwm, store_auto_pwm, 0, 1),
2611 SENSOR_ATTR_2(pwm1_auto_point2_temp, S_IWUSR | S_IRUGO,
2612 show_auto_temp, store_auto_temp, 0, 1),
2613 SENSOR_ATTR_2(pwm1_auto_point2_temp_hyst, S_IWUSR | S_IRUGO,
2614 show_auto_temp_hyst, store_auto_temp_hyst, 0, 1),
2615 SENSOR_ATTR_2(pwm1_auto_point3_pwm, S_IWUSR | S_IRUGO,
2616 show_auto_pwm, store_auto_pwm, 0, 2),
2617 SENSOR_ATTR_2(pwm1_auto_point3_temp, S_IWUSR | S_IRUGO,
2618 show_auto_temp, store_auto_temp, 0, 2),
2619 SENSOR_ATTR_2(pwm1_auto_point3_temp_hyst, S_IWUSR | S_IRUGO,
2620 show_auto_temp_hyst, store_auto_temp_hyst, 0, 2),
2621 SENSOR_ATTR_2(pwm1_auto_point4_pwm, S_IWUSR | S_IRUGO,
2622 show_auto_pwm, store_auto_pwm, 0, 3),
2623 SENSOR_ATTR_2(pwm1_auto_point4_temp, S_IWUSR | S_IRUGO,
2624 show_auto_temp, store_auto_temp, 0, 3),
2625 SENSOR_ATTR_2(pwm1_auto_point4_temp_hyst, S_IWUSR | S_IRUGO,
2626 show_auto_temp_hyst, store_auto_temp_hyst, 0, 3),
2627 SENSOR_ATTR_2(pwm1_auto_point5_pwm, S_IWUSR | S_IRUGO,
2628 show_auto_pwm, store_auto_pwm, 0, 4),
2629 SENSOR_ATTR_2(pwm1_auto_point5_temp, S_IWUSR | S_IRUGO,
2630 show_auto_temp, store_auto_temp, 0, 4),
2631 SENSOR_ATTR_2(pwm1_auto_point5_temp_hyst, S_IWUSR | S_IRUGO,
2632 show_auto_temp_hyst, store_auto_temp_hyst, 0, 4),
2633 SENSOR_ATTR_2(pwm1_auto_point6_pwm, S_IWUSR | S_IRUGO,
2634 show_auto_pwm, store_auto_pwm, 0, 5),
2635 SENSOR_ATTR_2(pwm1_auto_point6_temp, S_IWUSR | S_IRUGO,
2636 show_auto_temp, store_auto_temp, 0, 5),
2637 SENSOR_ATTR_2(pwm1_auto_point6_temp_hyst, S_IWUSR | S_IRUGO,
2638 show_auto_temp_hyst, store_auto_temp_hyst, 0, 5),
2639 SENSOR_ATTR_2(pwm1_auto_point7_pwm, S_IWUSR | S_IRUGO,
2640 show_auto_pwm, store_auto_pwm, 0, 6),
2641 SENSOR_ATTR_2(pwm1_auto_point7_temp, S_IWUSR | S_IRUGO,
2642 show_auto_temp, store_auto_temp, 0, 6),
2643 SENSOR_ATTR_2(pwm1_auto_point7_temp_hyst, S_IWUSR | S_IRUGO,
2644 show_auto_temp_hyst, store_auto_temp_hyst, 0, 6),
2646 SENSOR_ATTR_2(pwm2_auto_point1_pwm, S_IWUSR | S_IRUGO,
2647 show_auto_pwm, store_auto_pwm, 1, 0),
2648 SENSOR_ATTR_2(pwm2_auto_point1_temp, S_IWUSR | S_IRUGO,
2649 show_auto_temp, store_auto_temp, 1, 0),
2650 SENSOR_ATTR_2(pwm2_auto_point1_temp_hyst, S_IWUSR | S_IRUGO,
2651 show_auto_temp_hyst, store_auto_temp_hyst, 1, 0),
2652 SENSOR_ATTR_2(pwm2_auto_point2_pwm, S_IWUSR | S_IRUGO,
2653 show_auto_pwm, store_auto_pwm, 1, 1),
2654 SENSOR_ATTR_2(pwm2_auto_point2_temp, S_IWUSR | S_IRUGO,
2655 show_auto_temp, store_auto_temp, 1, 1),
2656 SENSOR_ATTR_2(pwm2_auto_point2_temp_hyst, S_IWUSR | S_IRUGO,
2657 show_auto_temp_hyst, store_auto_temp_hyst, 1, 1),
2658 SENSOR_ATTR_2(pwm2_auto_point3_pwm, S_IWUSR | S_IRUGO,
2659 show_auto_pwm, store_auto_pwm, 1, 2),
2660 SENSOR_ATTR_2(pwm2_auto_point3_temp, S_IWUSR | S_IRUGO,
2661 show_auto_temp, store_auto_temp, 1, 2),
2662 SENSOR_ATTR_2(pwm2_auto_point3_temp_hyst, S_IWUSR | S_IRUGO,
2663 show_auto_temp_hyst, store_auto_temp_hyst, 1, 2),
2664 SENSOR_ATTR_2(pwm2_auto_point4_pwm, S_IWUSR | S_IRUGO,
2665 show_auto_pwm, store_auto_pwm, 1, 3),
2666 SENSOR_ATTR_2(pwm2_auto_point4_temp, S_IWUSR | S_IRUGO,
2667 show_auto_temp, store_auto_temp, 1, 3),
2668 SENSOR_ATTR_2(pwm2_auto_point4_temp_hyst, S_IWUSR | S_IRUGO,
2669 show_auto_temp_hyst, store_auto_temp_hyst, 1, 3),
2670 SENSOR_ATTR_2(pwm2_auto_point5_pwm, S_IWUSR | S_IRUGO,
2671 show_auto_pwm, store_auto_pwm, 1, 4),
2672 SENSOR_ATTR_2(pwm2_auto_point5_temp, S_IWUSR | S_IRUGO,
2673 show_auto_temp, store_auto_temp, 1, 4),
2674 SENSOR_ATTR_2(pwm2_auto_point5_temp_hyst, S_IWUSR | S_IRUGO,
2675 show_auto_temp_hyst, store_auto_temp_hyst, 1, 4),
2676 SENSOR_ATTR_2(pwm2_auto_point6_pwm, S_IWUSR | S_IRUGO,
2677 show_auto_pwm, store_auto_pwm, 1, 5),
2678 SENSOR_ATTR_2(pwm2_auto_point6_temp, S_IWUSR | S_IRUGO,
2679 show_auto_temp, store_auto_temp, 1, 5),
2680 SENSOR_ATTR_2(pwm2_auto_point6_temp_hyst, S_IWUSR | S_IRUGO,
2681 show_auto_temp_hyst, store_auto_temp_hyst, 1, 5),
2682 SENSOR_ATTR_2(pwm2_auto_point7_pwm, S_IWUSR | S_IRUGO,
2683 show_auto_pwm, store_auto_pwm, 1, 6),
2684 SENSOR_ATTR_2(pwm2_auto_point7_temp, S_IWUSR | S_IRUGO,
2685 show_auto_temp, store_auto_temp, 1, 6),
2686 SENSOR_ATTR_2(pwm2_auto_point7_temp_hyst, S_IWUSR | S_IRUGO,
2687 show_auto_temp_hyst, store_auto_temp_hyst, 1, 6),
2689 SENSOR_ATTR_2(pwm3_auto_point1_pwm, S_IWUSR | S_IRUGO,
2690 show_auto_pwm, store_auto_pwm, 2, 0),
2691 SENSOR_ATTR_2(pwm3_auto_point1_temp, S_IWUSR | S_IRUGO,
2692 show_auto_temp, store_auto_temp, 2, 0),
2693 SENSOR_ATTR_2(pwm3_auto_point1_temp_hyst, S_IWUSR | S_IRUGO,
2694 show_auto_temp_hyst, store_auto_temp_hyst, 2, 0),
2695 SENSOR_ATTR_2(pwm3_auto_point2_pwm, S_IWUSR | S_IRUGO,
2696 show_auto_pwm, store_auto_pwm, 2, 1),
2697 SENSOR_ATTR_2(pwm3_auto_point2_temp, S_IWUSR | S_IRUGO,
2698 show_auto_temp, store_auto_temp, 2, 1),
2699 SENSOR_ATTR_2(pwm3_auto_point2_temp_hyst, S_IWUSR | S_IRUGO,
2700 show_auto_temp_hyst, store_auto_temp_hyst, 2, 1),
2701 SENSOR_ATTR_2(pwm3_auto_point3_pwm, S_IWUSR | S_IRUGO,
2702 show_auto_pwm, store_auto_pwm, 2, 2),
2703 SENSOR_ATTR_2(pwm3_auto_point3_temp, S_IWUSR | S_IRUGO,
2704 show_auto_temp, store_auto_temp, 2, 2),
2705 SENSOR_ATTR_2(pwm3_auto_point3_temp_hyst, S_IWUSR | S_IRUGO,
2706 show_auto_temp_hyst, store_auto_temp_hyst, 2, 2),
2707 SENSOR_ATTR_2(pwm3_auto_point4_pwm, S_IWUSR | S_IRUGO,
2708 show_auto_pwm, store_auto_pwm, 2, 3),
2709 SENSOR_ATTR_2(pwm3_auto_point4_temp, S_IWUSR | S_IRUGO,
2710 show_auto_temp, store_auto_temp, 2, 3),
2711 SENSOR_ATTR_2(pwm3_auto_point4_temp_hyst, S_IWUSR | S_IRUGO,
2712 show_auto_temp_hyst, store_auto_temp_hyst, 2, 3),
2713 SENSOR_ATTR_2(pwm3_auto_point5_pwm, S_IWUSR | S_IRUGO,
2714 show_auto_pwm, store_auto_pwm, 2, 4),
2715 SENSOR_ATTR_2(pwm3_auto_point5_temp, S_IWUSR | S_IRUGO,
2716 show_auto_temp, store_auto_temp, 2, 4),
2717 SENSOR_ATTR_2(pwm3_auto_point5_temp_hyst, S_IWUSR | S_IRUGO,
2718 show_auto_temp_hyst, store_auto_temp_hyst, 2, 4),
2719 SENSOR_ATTR_2(pwm3_auto_point6_pwm, S_IWUSR | S_IRUGO,
2720 show_auto_pwm, store_auto_pwm, 2, 5),
2721 SENSOR_ATTR_2(pwm3_auto_point6_temp, S_IWUSR | S_IRUGO,
2722 show_auto_temp, store_auto_temp, 2, 5),
2723 SENSOR_ATTR_2(pwm3_auto_point6_temp_hyst, S_IWUSR | S_IRUGO,
2724 show_auto_temp_hyst, store_auto_temp_hyst, 2, 5),
2725 SENSOR_ATTR_2(pwm3_auto_point7_pwm, S_IWUSR | S_IRUGO,
2726 show_auto_pwm, store_auto_pwm, 2, 6),
2727 SENSOR_ATTR_2(pwm3_auto_point7_temp, S_IWUSR | S_IRUGO,
2728 show_auto_temp, store_auto_temp, 2, 6),
2729 SENSOR_ATTR_2(pwm3_auto_point7_temp_hyst, S_IWUSR | S_IRUGO,
2730 show_auto_temp_hyst, store_auto_temp_hyst, 2, 6),
2732 SENSOR_ATTR_2(pwm4_auto_point1_pwm, S_IWUSR | S_IRUGO,
2733 show_auto_pwm, store_auto_pwm, 3, 0),
2734 SENSOR_ATTR_2(pwm4_auto_point1_temp, S_IWUSR | S_IRUGO,
2735 show_auto_temp, store_auto_temp, 3, 0),
2736 SENSOR_ATTR_2(pwm4_auto_point1_temp_hyst, S_IWUSR | S_IRUGO,
2737 show_auto_temp_hyst, store_auto_temp_hyst, 3, 0),
2738 SENSOR_ATTR_2(pwm4_auto_point2_pwm, S_IWUSR | S_IRUGO,
2739 show_auto_pwm, store_auto_pwm, 3, 1),
2740 SENSOR_ATTR_2(pwm4_auto_point2_temp, S_IWUSR | S_IRUGO,
2741 show_auto_temp, store_auto_temp, 3, 1),
2742 SENSOR_ATTR_2(pwm4_auto_point2_temp_hyst, S_IWUSR | S_IRUGO,
2743 show_auto_temp_hyst, store_auto_temp_hyst, 3, 1),
2744 SENSOR_ATTR_2(pwm4_auto_point3_pwm, S_IWUSR | S_IRUGO,
2745 show_auto_pwm, store_auto_pwm, 3, 2),
2746 SENSOR_ATTR_2(pwm4_auto_point3_temp, S_IWUSR | S_IRUGO,
2747 show_auto_temp, store_auto_temp, 3, 2),
2748 SENSOR_ATTR_2(pwm4_auto_point3_temp_hyst, S_IWUSR | S_IRUGO,
2749 show_auto_temp_hyst, store_auto_temp_hyst, 3, 2),
2750 SENSOR_ATTR_2(pwm4_auto_point4_pwm, S_IWUSR | S_IRUGO,
2751 show_auto_pwm, store_auto_pwm, 3, 3),
2752 SENSOR_ATTR_2(pwm4_auto_point4_temp, S_IWUSR | S_IRUGO,
2753 show_auto_temp, store_auto_temp, 3, 3),
2754 SENSOR_ATTR_2(pwm4_auto_point4_temp_hyst, S_IWUSR | S_IRUGO,
2755 show_auto_temp_hyst, store_auto_temp_hyst, 3, 3),
2756 SENSOR_ATTR_2(pwm4_auto_point5_pwm, S_IWUSR | S_IRUGO,
2757 show_auto_pwm, store_auto_pwm, 3, 4),
2758 SENSOR_ATTR_2(pwm4_auto_point5_temp, S_IWUSR | S_IRUGO,
2759 show_auto_temp, store_auto_temp, 3, 4),
2760 SENSOR_ATTR_2(pwm4_auto_point5_temp_hyst, S_IWUSR | S_IRUGO,
2761 show_auto_temp_hyst, store_auto_temp_hyst, 3, 4),
2762 SENSOR_ATTR_2(pwm4_auto_point6_pwm, S_IWUSR | S_IRUGO,
2763 show_auto_pwm, store_auto_pwm, 3, 5),
2764 SENSOR_ATTR_2(pwm4_auto_point6_temp, S_IWUSR | S_IRUGO,
2765 show_auto_temp, store_auto_temp, 3, 5),
2766 SENSOR_ATTR_2(pwm4_auto_point6_temp_hyst, S_IWUSR | S_IRUGO,
2767 show_auto_temp_hyst, store_auto_temp_hyst, 3, 5),
2768 SENSOR_ATTR_2(pwm4_auto_point7_pwm, S_IWUSR | S_IRUGO,
2769 show_auto_pwm, store_auto_pwm, 3, 6),
2770 SENSOR_ATTR_2(pwm4_auto_point7_temp, S_IWUSR | S_IRUGO,
2771 show_auto_temp, store_auto_temp, 3, 6),
2772 SENSOR_ATTR_2(pwm4_auto_point7_temp_hyst, S_IWUSR | S_IRUGO,
2773 show_auto_temp_hyst, store_auto_temp_hyst, 3, 6),
2775 SENSOR_ATTR_2(pwm5_auto_point1_pwm, S_IWUSR | S_IRUGO,
2776 show_auto_pwm, store_auto_pwm, 4, 0),
2777 SENSOR_ATTR_2(pwm5_auto_point1_temp, S_IWUSR | S_IRUGO,
2778 show_auto_temp, store_auto_temp, 4, 0),
2779 SENSOR_ATTR_2(pwm5_auto_point1_temp_hyst, S_IWUSR | S_IRUGO,
2780 show_auto_temp_hyst, store_auto_temp_hyst, 4, 0),
2781 SENSOR_ATTR_2(pwm5_auto_point2_pwm, S_IWUSR | S_IRUGO,
2782 show_auto_pwm, store_auto_pwm, 4, 1),
2783 SENSOR_ATTR_2(pwm5_auto_point2_temp, S_IWUSR | S_IRUGO,
2784 show_auto_temp, store_auto_temp, 4, 1),
2785 SENSOR_ATTR_2(pwm5_auto_point2_temp_hyst, S_IWUSR | S_IRUGO,
2786 show_auto_temp_hyst, store_auto_temp_hyst, 4, 1),
2787 SENSOR_ATTR_2(pwm5_auto_point3_pwm, S_IWUSR | S_IRUGO,
2788 show_auto_pwm, store_auto_pwm, 4, 2),
2789 SENSOR_ATTR_2(pwm5_auto_point3_temp, S_IWUSR | S_IRUGO,
2790 show_auto_temp, store_auto_temp, 4, 2),
2791 SENSOR_ATTR_2(pwm5_auto_point3_temp_hyst, S_IWUSR | S_IRUGO,
2792 show_auto_temp_hyst, store_auto_temp_hyst, 4, 2),
2793 SENSOR_ATTR_2(pwm5_auto_point4_pwm, S_IWUSR | S_IRUGO,
2794 show_auto_pwm, store_auto_pwm, 4, 3),
2795 SENSOR_ATTR_2(pwm5_auto_point4_temp, S_IWUSR | S_IRUGO,
2796 show_auto_temp, store_auto_temp, 4, 3),
2797 SENSOR_ATTR_2(pwm5_auto_point4_temp_hyst, S_IWUSR | S_IRUGO,
2798 show_auto_temp_hyst, store_auto_temp_hyst, 4, 3),
2799 SENSOR_ATTR_2(pwm5_auto_point5_pwm, S_IWUSR | S_IRUGO,
2800 show_auto_pwm, store_auto_pwm, 4, 4),
2801 SENSOR_ATTR_2(pwm5_auto_point5_temp, S_IWUSR | S_IRUGO,
2802 show_auto_temp, store_auto_temp, 4, 4),
2803 SENSOR_ATTR_2(pwm5_auto_point5_temp_hyst, S_IWUSR | S_IRUGO,
2804 show_auto_temp_hyst, store_auto_temp_hyst, 4, 4),
2805 SENSOR_ATTR_2(pwm5_auto_point6_pwm, S_IWUSR | S_IRUGO,
2806 show_auto_pwm, store_auto_pwm, 4, 5),
2807 SENSOR_ATTR_2(pwm5_auto_point6_temp, S_IWUSR | S_IRUGO,
2808 show_auto_temp, store_auto_temp, 4, 5),
2809 SENSOR_ATTR_2(pwm5_auto_point6_temp_hyst, S_IWUSR | S_IRUGO,
2810 show_auto_temp_hyst, store_auto_temp_hyst, 4, 5),
2811 SENSOR_ATTR_2(pwm5_auto_point7_pwm, S_IWUSR | S_IRUGO,
2812 show_auto_pwm, store_auto_pwm, 4, 6),
2813 SENSOR_ATTR_2(pwm5_auto_point7_temp, S_IWUSR | S_IRUGO,
2814 show_auto_temp, store_auto_temp, 4, 6),
2815 SENSOR_ATTR_2(pwm5_auto_point7_temp_hyst, S_IWUSR | S_IRUGO,
2816 show_auto_temp_hyst, store_auto_temp_hyst, 4, 6),
2820 show_vid(struct device *dev, struct device_attribute *attr, char *buf)
2822 struct nct6775_data *data = dev_get_drvdata(dev);
2823 return sprintf(buf, "%d\n", vid_from_reg(data->vid, data->vrm));
2826 static DEVICE_ATTR(cpu0_vid, S_IRUGO, show_vid, NULL);
2828 /* Case open detection */
2831 clear_caseopen(struct device *dev, struct device_attribute *attr,
2832 const char *buf, size_t count)
2834 struct nct6775_data *data = dev_get_drvdata(dev);
2835 struct nct6775_sio_data *sio_data = dev->platform_data;
2836 int nr = to_sensor_dev_attr(attr)->index - INTRUSION_ALARM_BASE;
2840 if (kstrtoul(buf, 10, &val) || val != 0)
2843 mutex_lock(&data->update_lock);
2846 * Use CR registers to clear caseopen status.
2847 * The CR registers are the same for all chips, and not all chips
2848 * support clearing the caseopen status through "regular" registers.
2850 superio_enter(sio_data->sioreg);
2851 superio_select(sio_data->sioreg, NCT6775_LD_ACPI);
2852 reg = superio_inb(sio_data->sioreg, NCT6775_REG_CR_CASEOPEN_CLR[nr]);
2853 reg |= NCT6775_CR_CASEOPEN_CLR_MASK[nr];
2854 superio_outb(sio_data->sioreg, NCT6775_REG_CR_CASEOPEN_CLR[nr], reg);
2855 superio_exit(sio_data->sioreg);
2857 data->valid = 0; /* Force cache refresh */
2859 mutex_unlock(&data->update_lock);
2864 static struct sensor_device_attribute sda_caseopen[] = {
2865 SENSOR_ATTR(intrusion0_alarm, S_IWUSR | S_IRUGO, show_alarm,
2866 clear_caseopen, INTRUSION_ALARM_BASE),
2867 SENSOR_ATTR(intrusion1_alarm, S_IWUSR | S_IRUGO, show_alarm,
2868 clear_caseopen, INTRUSION_ALARM_BASE + 1),
2872 * Driver and device management
2875 static void nct6775_device_remove_files(struct device *dev)
2878 * some entries in the following arrays may not have been used in
2879 * device_create_file(), but device_remove_file() will ignore them
2882 struct nct6775_data *data = dev_get_drvdata(dev);
2884 for (i = 0; i < data->pwm_num; i++)
2885 sysfs_remove_group(&dev->kobj, &nct6775_group_pwm[i]);
2887 for (i = 0; i < ARRAY_SIZE(sda_max_output); i++)
2888 device_remove_file(dev, &sda_max_output[i].dev_attr);
2890 for (i = 0; i < ARRAY_SIZE(sda_step_output); i++)
2891 device_remove_file(dev, &sda_max_output[i].dev_attr);
2893 for (i = 0; i < ARRAY_SIZE(sda_weight_duty_base); i++)
2894 device_remove_file(dev, &sda_weight_duty_base[i].dev_attr);
2896 for (i = 0; i < ARRAY_SIZE(sda_auto_pwm_arrays); i++)
2897 device_remove_file(dev, &sda_auto_pwm_arrays[i].dev_attr);
2899 for (i = 0; i < data->in_num; i++)
2900 sysfs_remove_group(&dev->kobj, &nct6775_group_in[i]);
2902 for (i = 0; i < 5; i++) {
2903 device_remove_file(dev, &sda_fan_input[i].dev_attr);
2904 device_remove_file(dev, &sda_fan_alarm[i].dev_attr);
2905 device_remove_file(dev, &sda_fan_div[i].dev_attr);
2906 device_remove_file(dev, &sda_fan_min[i].dev_attr);
2908 for (i = 0; i < NUM_TEMP; i++) {
2909 if (!(data->have_temp & (1 << i)))
2911 device_remove_file(dev, &sda_temp_input[i].dev_attr);
2912 device_remove_file(dev, &sda_temp_label[i].dev_attr);
2913 device_remove_file(dev, &sda_temp_max[i].dev_attr);
2914 device_remove_file(dev, &sda_temp_max_hyst[i].dev_attr);
2915 if (!(data->have_temp_fixed & (1 << i)))
2917 device_remove_file(dev, &sda_temp_type[i].dev_attr);
2918 device_remove_file(dev, &sda_temp_offset[i].dev_attr);
2919 if (i >= NUM_TEMP_ALARM)
2921 device_remove_file(dev, &sda_temp_alarm[i].dev_attr);
2924 device_remove_file(dev, &sda_caseopen[0].dev_attr);
2925 device_remove_file(dev, &sda_caseopen[1].dev_attr);
2927 device_remove_file(dev, &dev_attr_name);
2928 device_remove_file(dev, &dev_attr_cpu0_vid);
2931 /* Get the monitoring functions started */
2932 static inline void __devinit nct6775_init_device(struct nct6775_data *data)
2937 /* Start monitoring if needed */
2938 if (data->REG_CONFIG) {
2939 tmp = nct6775_read_value(data, data->REG_CONFIG);
2941 nct6775_write_value(data, data->REG_CONFIG, tmp | 0x01);
2944 /* Enable temperature sensors if needed */
2945 for (i = 0; i < NUM_TEMP; i++) {
2946 if (!(data->have_temp & (1 << i)))
2948 if (!data->reg_temp_config[i])
2950 tmp = nct6775_read_value(data, data->reg_temp_config[i]);
2952 nct6775_write_value(data, data->reg_temp_config[i],
2956 /* Enable VBAT monitoring if needed */
2957 tmp = nct6775_read_value(data, data->REG_VBAT);
2959 nct6775_write_value(data, data->REG_VBAT, tmp | 0x01);
2961 diode = nct6775_read_value(data, data->REG_DIODE);
2963 for (i = 0; i < data->temp_fixed_num; i++) {
2964 if (!(data->have_temp_fixed & (1 << i)))
2966 if ((tmp & (0x02 << i))) /* diode */
2967 data->temp_type[i] = 3 - ((diode >> i) & 0x02);
2968 else /* thermistor */
2969 data->temp_type[i] = 4;
2973 static void __devinit
2974 nct6775_check_fan_inputs(const struct nct6775_sio_data *sio_data,
2975 struct nct6775_data *data)
2978 bool fan3pin, fan3min, fan4pin, fan4min, fan5pin;
2979 bool pwm3pin, pwm4pin, pwm5pin;
2981 superio_enter(sio_data->sioreg);
2983 /* fan4 and fan5 share some pins with the GPIO and serial flash */
2984 if (data->kind == nct6775) {
2985 regval = superio_inb(sio_data->sioreg, 0x2c);
2987 fan3pin = regval & (1 << 6);
2989 pwm3pin = regval & (1 << 7);
2991 /* On NCT6775, fan4 shares pins with the fdc interface */
2992 fan4pin = !(superio_inb(sio_data->sioreg, 0x2A) & 0x80);
2997 } else if (data->kind == nct6776) {
2998 bool gpok = superio_inb(sio_data->sioreg, 0x27) & 0x80;
3000 superio_select(sio_data->sioreg, NCT6775_LD_HWM);
3001 regval = superio_inb(sio_data->sioreg, SIO_REG_ENABLE);
3006 fan3pin = !(superio_inb(sio_data->sioreg, 0x24) & 0x40);
3011 fan4pin = superio_inb(sio_data->sioreg, 0x1C) & 0x01;
3016 fan5pin = superio_inb(sio_data->sioreg, 0x1C) & 0x02;
3023 } else { /* NCT6779D */
3024 regval = superio_inb(sio_data->sioreg, 0x1c);
3026 fan3pin = !(regval & (1 << 5));
3027 fan4pin = !(regval & (1 << 6));
3028 fan5pin = !(regval & (1 << 7));
3030 pwm3pin = !(regval & (1 << 0));
3031 pwm4pin = !(regval & (1 << 1));
3032 pwm5pin = !(regval & (1 << 2));
3038 superio_exit(sio_data->sioreg);
3040 data->has_fan = data->has_fan_min = 0x03; /* fan1 and fan2 */
3041 data->has_fan |= fan3pin << 2;
3042 data->has_fan_min |= fan3min << 2;
3044 data->has_fan |= (fan4pin << 3) | (fan5pin << 4);
3045 data->has_fan_min |= (fan4min << 3) | (fan5pin << 4);
3047 data->has_pwm = 0x03 | (pwm3pin << 2) | (pwm4pin << 3) | (pwm5pin << 4);
3050 static int __devinit nct6775_probe(struct platform_device *pdev)
3052 struct device *dev = &pdev->dev;
3053 struct nct6775_sio_data *sio_data = dev->platform_data;
3054 struct nct6775_data *data;
3055 struct resource *res;
3057 int src, mask, available;
3058 const u16 *reg_temp, *reg_temp_over, *reg_temp_hyst, *reg_temp_config;
3060 res = platform_get_resource(pdev, IORESOURCE_IO, 0);
3061 if (!request_region(res->start, IOREGION_LENGTH, DRVNAME)) {
3063 dev_err(dev, "Failed to request region 0x%lx-0x%lx\n",
3064 (unsigned long)res->start,
3065 (unsigned long)res->start + IOREGION_LENGTH - 1);
3069 data = devm_kzalloc(&pdev->dev, sizeof(struct nct6775_data),
3076 data->kind = sio_data->kind;
3077 data->addr = res->start;
3078 mutex_init(&data->lock);
3079 mutex_init(&data->update_lock);
3080 data->name = nct6775_device_names[data->kind];
3081 platform_set_drvdata(pdev, data);
3083 switch (data->kind) {
3086 data->have_in = 0x1ff;
3088 data->auto_pwm_num = 6;
3089 data->has_fan_div = true;
3090 data->temp_fixed_num = 3;
3092 data->ALARM_BITS = NCT6775_ALARM_BITS;
3094 data->fan_from_reg = fan_from_reg16;
3095 data->fan_from_reg_min = fan_from_reg8;
3097 data->temp_label = nct6775_temp_label;
3098 data->temp_label_num = ARRAY_SIZE(nct6775_temp_label);
3100 data->REG_CONFIG = NCT6775_REG_CONFIG;
3101 data->REG_VBAT = NCT6775_REG_VBAT;
3102 data->REG_DIODE = NCT6775_REG_DIODE;
3103 data->REG_VIN = NCT6775_REG_IN;
3104 data->REG_IN_MINMAX[0] = NCT6775_REG_IN_MIN;
3105 data->REG_IN_MINMAX[1] = NCT6775_REG_IN_MAX;
3106 data->REG_TARGET = NCT6775_REG_TARGET;
3107 data->REG_FAN = NCT6775_REG_FAN;
3108 data->REG_FAN_MODE = NCT6775_REG_FAN_MODE;
3109 data->REG_FAN_MIN = NCT6775_REG_FAN_MIN;
3110 data->REG_FAN_TIME[0] = NCT6775_REG_FAN_STOP_TIME;
3111 data->REG_FAN_TIME[1] = NCT6775_REG_FAN_STEP_UP_TIME;
3112 data->REG_FAN_TIME[2] = NCT6775_REG_FAN_STEP_DOWN_TIME;
3113 data->REG_PWM[0] = NCT6775_REG_PWM;
3114 data->REG_PWM[1] = NCT6775_REG_FAN_START_OUTPUT;
3115 data->REG_PWM[2] = NCT6775_REG_FAN_STOP_OUTPUT;
3116 data->REG_PWM[3] = NCT6775_REG_FAN_MAX_OUTPUT;
3117 data->REG_PWM[4] = NCT6775_REG_FAN_STEP_OUTPUT;
3118 data->REG_PWM[5] = NCT6775_REG_WEIGHT_DUTY_STEP;
3119 data->REG_PWM_READ = NCT6775_REG_PWM_READ;
3120 data->REG_PWM_MODE = NCT6775_REG_PWM_MODE;
3121 data->PWM_MODE_MASK = NCT6775_PWM_MODE_MASK;
3122 data->REG_TEMP_ALTERNATE = NCT6775_REG_TEMP_ALTERNATE;
3123 data->REG_TEMP_MON = NCT6775_REG_TEMP_MON;
3124 data->REG_AUTO_TEMP = NCT6775_REG_AUTO_TEMP;
3125 data->REG_AUTO_PWM = NCT6775_REG_AUTO_PWM;
3126 data->REG_CRITICAL_TEMP = NCT6775_REG_CRITICAL_TEMP;
3127 data->REG_CRITICAL_TEMP_TOLERANCE
3128 = NCT6775_REG_CRITICAL_TEMP_TOLERANCE;
3129 data->REG_TEMP_OFFSET = NCT6775_REG_TEMP_OFFSET;
3130 data->REG_TEMP_SOURCE = NCT6775_REG_TEMP_SOURCE;
3131 data->REG_TEMP_SEL[0] = NCT6775_REG_TEMP_SEL;
3132 data->REG_TEMP_SEL[1] = NCT6775_REG_WEIGHT_TEMP_SEL;
3133 data->REG_WEIGHT_TEMP[0] = NCT6775_REG_WEIGHT_TEMP_STEP;
3134 data->REG_WEIGHT_TEMP[1] = NCT6775_REG_WEIGHT_TEMP_STEP_TOL;
3135 data->REG_WEIGHT_TEMP[2] = NCT6775_REG_WEIGHT_TEMP_BASE;
3136 data->REG_ALARM = NCT6775_REG_ALARM;
3138 reg_temp = NCT6775_REG_TEMP;
3139 reg_temp_over = NCT6775_REG_TEMP_OVER;
3140 reg_temp_hyst = NCT6775_REG_TEMP_HYST;
3141 reg_temp_config = NCT6775_REG_TEMP_CONFIG;
3146 data->have_in = 0x1ff;
3148 data->auto_pwm_num = 4;
3149 data->has_fan_div = false;
3150 data->temp_fixed_num = 3;
3152 data->ALARM_BITS = NCT6776_ALARM_BITS;
3154 data->fan_from_reg = fan_from_reg13;
3155 data->fan_from_reg_min = fan_from_reg13;
3157 data->temp_label = nct6776_temp_label;
3158 data->temp_label_num = ARRAY_SIZE(nct6776_temp_label);
3160 data->REG_CONFIG = NCT6775_REG_CONFIG;
3161 data->REG_VBAT = NCT6775_REG_VBAT;
3162 data->REG_DIODE = NCT6775_REG_DIODE;
3163 data->REG_VIN = NCT6775_REG_IN;
3164 data->REG_IN_MINMAX[0] = NCT6775_REG_IN_MIN;
3165 data->REG_IN_MINMAX[1] = NCT6775_REG_IN_MAX;
3166 data->REG_TARGET = NCT6775_REG_TARGET;
3167 data->REG_FAN = NCT6775_REG_FAN;
3168 data->REG_FAN_MODE = NCT6775_REG_FAN_MODE;
3169 data->REG_FAN_MIN = NCT6776_REG_FAN_MIN;
3170 data->REG_FAN_TIME[0] = NCT6775_REG_FAN_STOP_TIME;
3171 data->REG_FAN_TIME[1] = NCT6775_REG_FAN_STEP_UP_TIME;
3172 data->REG_FAN_TIME[2] = NCT6775_REG_FAN_STEP_DOWN_TIME;
3173 data->REG_PWM[0] = NCT6775_REG_PWM;
3174 data->REG_PWM[1] = NCT6775_REG_FAN_START_OUTPUT;
3175 data->REG_PWM[2] = NCT6775_REG_FAN_STOP_OUTPUT;
3176 data->REG_PWM[5] = NCT6775_REG_WEIGHT_DUTY_STEP;
3177 data->REG_PWM[6] = NCT6776_REG_WEIGHT_DUTY_BASE;
3178 data->REG_PWM_READ = NCT6775_REG_PWM_READ;
3179 data->REG_PWM_MODE = NCT6776_REG_PWM_MODE;
3180 data->PWM_MODE_MASK = NCT6776_PWM_MODE_MASK;
3181 data->REG_TEMP_ALTERNATE = NCT6776_REG_TEMP_ALTERNATE;
3182 data->REG_TEMP_MON = NCT6775_REG_TEMP_MON;
3183 data->REG_AUTO_TEMP = NCT6775_REG_AUTO_TEMP;
3184 data->REG_AUTO_PWM = NCT6775_REG_AUTO_PWM;
3185 data->REG_CRITICAL_TEMP = NCT6775_REG_CRITICAL_TEMP;
3186 data->REG_CRITICAL_TEMP_TOLERANCE
3187 = NCT6775_REG_CRITICAL_TEMP_TOLERANCE;
3188 data->REG_TEMP_OFFSET = NCT6775_REG_TEMP_OFFSET;
3189 data->REG_TEMP_SOURCE = NCT6775_REG_TEMP_SOURCE;
3190 data->REG_TEMP_SEL[0] = NCT6775_REG_TEMP_SEL;
3191 data->REG_TEMP_SEL[1] = NCT6775_REG_WEIGHT_TEMP_SEL;
3192 data->REG_WEIGHT_TEMP[0] = NCT6775_REG_WEIGHT_TEMP_STEP;
3193 data->REG_WEIGHT_TEMP[1] = NCT6775_REG_WEIGHT_TEMP_STEP_TOL;
3194 data->REG_WEIGHT_TEMP[2] = NCT6775_REG_WEIGHT_TEMP_BASE;
3195 data->REG_ALARM = NCT6775_REG_ALARM;
3197 reg_temp = NCT6775_REG_TEMP;
3198 reg_temp_over = NCT6775_REG_TEMP_OVER;
3199 reg_temp_hyst = NCT6775_REG_TEMP_HYST;
3200 reg_temp_config = NCT6776_REG_TEMP_CONFIG;
3205 data->have_in = 0x7fff;
3207 data->auto_pwm_num = 4;
3208 data->has_fan_div = false;
3209 data->temp_fixed_num = 6;
3211 data->ALARM_BITS = NCT6779_ALARM_BITS;
3213 data->fan_from_reg = fan_from_reg13;
3214 data->fan_from_reg_min = fan_from_reg13;
3216 data->temp_label = nct6779_temp_label;
3217 data->temp_label_num = ARRAY_SIZE(nct6779_temp_label);
3219 data->REG_CONFIG = NCT6775_REG_CONFIG;
3220 data->REG_VBAT = NCT6775_REG_VBAT;
3221 data->REG_DIODE = NCT6775_REG_DIODE;
3222 data->REG_VIN = NCT6779_REG_IN;
3223 data->REG_IN_MINMAX[0] = NCT6775_REG_IN_MIN;
3224 data->REG_IN_MINMAX[1] = NCT6775_REG_IN_MAX;
3225 data->REG_TARGET = NCT6775_REG_TARGET;
3226 data->REG_FAN = NCT6779_REG_FAN;
3227 data->REG_FAN_MODE = NCT6775_REG_FAN_MODE;
3228 data->REG_FAN_MIN = NCT6776_REG_FAN_MIN;
3229 data->REG_FAN_TIME[0] = NCT6775_REG_FAN_STOP_TIME;
3230 data->REG_FAN_TIME[1] = NCT6775_REG_FAN_STEP_UP_TIME;
3231 data->REG_FAN_TIME[2] = NCT6775_REG_FAN_STEP_DOWN_TIME;
3232 data->REG_PWM[0] = NCT6775_REG_PWM;
3233 data->REG_PWM[1] = NCT6775_REG_FAN_START_OUTPUT;
3234 data->REG_PWM[2] = NCT6775_REG_FAN_STOP_OUTPUT;
3235 data->REG_PWM[5] = NCT6775_REG_WEIGHT_DUTY_STEP;
3236 data->REG_PWM[6] = NCT6776_REG_WEIGHT_DUTY_BASE;
3237 data->REG_PWM_READ = NCT6775_REG_PWM_READ;
3238 data->REG_PWM_MODE = NCT6776_REG_PWM_MODE;
3239 data->PWM_MODE_MASK = NCT6776_PWM_MODE_MASK;
3240 data->REG_TEMP_ALTERNATE = NCT6779_REG_TEMP_ALTERNATE;
3241 data->REG_TEMP_MON = NCT6775_REG_TEMP_MON;
3242 data->REG_AUTO_TEMP = NCT6775_REG_AUTO_TEMP;
3243 data->REG_AUTO_PWM = NCT6775_REG_AUTO_PWM;
3244 data->REG_CRITICAL_TEMP = NCT6775_REG_CRITICAL_TEMP;
3245 data->REG_CRITICAL_TEMP_TOLERANCE
3246 = NCT6775_REG_CRITICAL_TEMP_TOLERANCE;
3247 data->REG_TEMP_OFFSET = NCT6779_REG_TEMP_OFFSET;
3248 data->REG_TEMP_SOURCE = NCT6775_REG_TEMP_SOURCE;
3249 data->REG_TEMP_SEL[0] = NCT6775_REG_TEMP_SEL;
3250 data->REG_TEMP_SEL[1] = NCT6775_REG_WEIGHT_TEMP_SEL;
3251 data->REG_WEIGHT_TEMP[0] = NCT6775_REG_WEIGHT_TEMP_STEP;
3252 data->REG_WEIGHT_TEMP[1] = NCT6775_REG_WEIGHT_TEMP_STEP_TOL;
3253 data->REG_WEIGHT_TEMP[2] = NCT6775_REG_WEIGHT_TEMP_BASE;
3254 data->REG_ALARM = NCT6779_REG_ALARM;
3256 reg_temp = NCT6779_REG_TEMP;
3257 reg_temp_over = NCT6775_REG_TEMP_OVER;
3258 reg_temp_hyst = NCT6775_REG_TEMP_HYST;
3259 reg_temp_config = NCT6776_REG_TEMP_CONFIG;
3267 data->have_temp = 0;
3270 * On some boards, not all available temperature sources are monitored,
3271 * even though some of the monitoring registers are unused.
3272 * Get list of unused monitoring registers, then detect if any fan
3273 * controls are configured to use unmonitored temperature sources.
3274 * If so, assign the unmonitored temperature sources to available
3275 * monitoring registers.
3279 for (i = 0; i < NUM_REG_TEMP; i++) {
3280 if (reg_temp[i] == 0)
3283 src = nct6775_read_value(data, data->REG_TEMP_SOURCE[i]) & 0x1f;
3284 if (!src || (mask & (1 << src)))
3285 available |= 1 << i;
3291 * Now find unmonitored temperature registers and enable monitoring
3292 * if additional monitoring registers are available.
3294 for (i = 0; i < ARRAY_SIZE(data->REG_TEMP_SEL) && available; i++) {
3297 if (!data->REG_TEMP_SEL[i])
3299 for (j = 0; j < data->pwm_num && available; j++) {
3302 if (!data->REG_TEMP_SEL[i][j])
3304 src = nct6775_read_value(data,
3305 data->REG_TEMP_SEL[i][j]);
3307 if (!src || (mask & (1 << src)))
3310 index = __ffs(available);
3311 nct6775_write_value(data,
3312 data->REG_TEMP_SOURCE[index],
3314 available &= ~(1 << index);
3320 s = NUM_TEMP_FIXED; /* First dynamic temperature attribute */
3321 for (i = 0; i < NUM_REG_TEMP; i++) {
3322 if (reg_temp[i] == 0)
3325 src = nct6775_read_value(data, data->REG_TEMP_SOURCE[i]) & 0x1f;
3326 if (!src || (mask & (1 << src)))
3331 /* Use fixed index for SYSTIN(1), CPUTIN(2), AUXTIN(3) */
3332 if (src <= data->temp_fixed_num) {
3333 data->have_temp |= 1 << (src - 1);
3334 data->have_temp_fixed |= 1 << (src - 1);
3335 data->reg_temp[0][src - 1] = reg_temp[i];
3336 data->reg_temp[1][src - 1] = reg_temp_over[i];
3337 data->reg_temp[2][src - 1] = reg_temp_hyst[i];
3338 data->reg_temp_config[src - 1] = reg_temp_config[i];
3339 data->temp_src[src - 1] = src;
3346 /* Use dynamic index for other sources */
3347 data->have_temp |= 1 << s;
3348 data->reg_temp[0][s] = reg_temp[i];
3349 data->reg_temp[1][s] = reg_temp_over[i];
3350 data->reg_temp[2][s] = reg_temp_hyst[i];
3351 data->reg_temp_config[s] = reg_temp_config[i];
3353 data->temp_src[s] = src;
3359 * We may have alternate registers for some sensors.
3360 * Go through the list and enable if possible.
3361 * The temperature is already monitored if the respective bit in <mask>
3364 if (data->REG_TEMP_ALTERNATE) {
3365 for (i = 0; i < data->temp_label_num; i++) {
3366 if (!data->REG_TEMP_ALTERNATE[i])
3368 if (mask & (1 << (i + 1)))
3370 if (i < data->temp_fixed_num) {
3371 if (data->have_temp & (1 << i))
3373 data->have_temp |= 1 << i;
3374 data->reg_temp[0][i] = data->REG_TEMP_ALTERNATE[i];
3375 data->temp_src[i] = i + 1;
3379 if (s >= NUM_TEMP) /* Abort if no more space */
3382 data->have_temp |= 1 << s;
3383 data->reg_temp[0][s] = data->REG_TEMP_ALTERNATE[i];
3384 data->temp_src[s] = i + 1;
3388 #endif /* TESTING */
3390 switch (data->kind) {
3395 * On NCT6776, AUXTIN and VIN3 pins are shared.
3396 * Only way to detect it is to check if AUXTIN is used
3397 * as a temperature source, and if that source is
3400 * If that is the case, disable in6, which reports VIN3.
3401 * Otherwise disable temp3.
3403 if (data->have_temp & (1 << 2)) {
3404 u8 reg = nct6775_read_value(data,
3405 data->reg_temp_config[2]);
3407 data->have_temp &= ~(1 << 2);
3409 data->have_in &= ~(1 << 6);
3419 * Assume voltage is disabled if the respective temperature is
3420 * used as temperature source.
3422 for (i = 0; i < ARRAY_SIZE(NCT6779_REG_TEMP); i++) {
3423 if (!(data->have_temp & (1 << i)))
3425 if (i == 2) /* AUXTIN0 */
3426 data->have_in &= ~(1 << 6); /* no VIN4 */
3427 if (i == 3) /* AUXTIN1 */
3428 data->have_in &= ~(1 << 10); /* no VIN5 */
3429 if (i == 4) /* AUXTIN2 */
3430 data->have_in &= ~(1 << 11); /* no VIN6 */
3431 if (i == 5) /* AUXTIN3 */
3432 data->have_in &= ~(1 << 14); /* no VIN7 */
3437 /* Initialize the chip */
3438 nct6775_init_device(data);
3440 data->vrm = vid_which_vrm();
3441 superio_enter(sio_data->sioreg);
3444 * We can get the VID input values directly at logical device D 0xe3.
3446 superio_select(sio_data->sioreg, NCT6775_LD_VID);
3447 data->vid = superio_inb(sio_data->sioreg, 0xe3);
3448 err = device_create_file(dev, &dev_attr_cpu0_vid);
3455 superio_select(sio_data->sioreg, NCT6775_LD_HWM);
3456 tmp = superio_inb(sio_data->sioreg,
3457 NCT6775_REG_CR_FAN_DEBOUNCE);
3458 switch (data->kind) {
3467 superio_outb(sio_data->sioreg, NCT6775_REG_CR_FAN_DEBOUNCE,
3469 pr_info("Enabled fan debounce for chip %s\n", data->name);
3472 superio_exit(sio_data->sioreg);
3474 nct6775_check_fan_inputs(sio_data, data);
3476 /* Read fan clock dividers immediately */
3477 nct6775_update_fan_div_common(dev, data);
3479 /* Register sysfs hooks */
3480 for (i = 0; i < data->pwm_num; i++) {
3481 if (!(data->has_pwm & (1 << i)))
3484 err = sysfs_create_group(&dev->kobj, &nct6775_group_pwm[i]);
3488 if (data->REG_PWM[3]) {
3489 err = device_create_file(dev,
3490 &sda_max_output[i].dev_attr);
3494 if (data->REG_PWM[4]) {
3495 err = device_create_file(dev,
3496 &sda_step_output[i].dev_attr);
3500 if (data->REG_PWM[6]) {
3501 err = device_create_file(dev,
3502 &sda_weight_duty_base[i].dev_attr);
3507 for (i = 0; i < ARRAY_SIZE(sda_auto_pwm_arrays); i++) {
3508 struct sensor_device_attribute_2 *attr =
3509 &sda_auto_pwm_arrays[i];
3511 if (!(data->has_pwm & (1 << attr->nr)))
3513 if (attr->index > data->auto_pwm_num)
3515 err = device_create_file(dev, &attr->dev_attr);
3520 for (i = 0; i < data->in_num; i++) {
3521 if (!(data->have_in & (1 << i)))
3523 err = sysfs_create_group(&dev->kobj, &nct6775_group_in[i]);
3528 for (i = 0; i < 5; i++) {
3529 if (data->has_fan & (1 << i)) {
3530 err = device_create_file(dev,
3531 &sda_fan_input[i].dev_attr);
3534 err = device_create_file(dev,
3535 &sda_fan_alarm[i].dev_attr);
3538 if (data->kind != nct6776 &&
3539 data->kind != nct6779) {
3540 err = device_create_file(dev,
3541 &sda_fan_div[i].dev_attr);
3545 if (data->has_fan_min & (1 << i)) {
3546 err = device_create_file(dev,
3547 &sda_fan_min[i].dev_attr);
3554 for (i = 0; i < NUM_TEMP; i++) {
3555 if (!(data->have_temp & (1 << i)))
3557 err = device_create_file(dev, &sda_temp_input[i].dev_attr);
3560 if (data->temp_label) {
3561 err = device_create_file(dev,
3562 &sda_temp_label[i].dev_attr);
3566 if (data->reg_temp[1][i]) {
3567 err = device_create_file(dev,
3568 &sda_temp_max[i].dev_attr);
3572 if (data->reg_temp[2][i]) {
3573 err = device_create_file(dev,
3574 &sda_temp_max_hyst[i].dev_attr);
3578 if (!(data->have_temp_fixed & (1 << i)))
3580 err = device_create_file(dev, &sda_temp_type[i].dev_attr);
3583 err = device_create_file(dev,
3584 &sda_temp_offset[i].dev_attr);
3587 if (i >= NUM_TEMP_ALARM ||
3588 data->ALARM_BITS[TEMP_ALARM_BASE + i] < 0)
3590 err = device_create_file(dev, &sda_temp_alarm[i].dev_attr);
3595 for (i = 0; i < ARRAY_SIZE(sda_caseopen); i++) {
3596 if (data->ALARM_BITS[INTRUSION_ALARM_BASE + i] < 0)
3598 err = device_create_file(dev, &sda_caseopen[i].dev_attr);
3603 err = device_create_file(dev, &dev_attr_name);
3607 data->hwmon_dev = hwmon_device_register(dev);
3608 if (IS_ERR(data->hwmon_dev)) {
3609 err = PTR_ERR(data->hwmon_dev);
3616 nct6775_device_remove_files(dev);
3618 release_region(res->start, IOREGION_LENGTH);
3623 static int __devexit nct6775_remove(struct platform_device *pdev)
3625 struct nct6775_data *data = platform_get_drvdata(pdev);
3627 hwmon_device_unregister(data->hwmon_dev);
3628 nct6775_device_remove_files(&pdev->dev);
3629 release_region(data->addr, IOREGION_LENGTH);
3634 static struct platform_driver nct6775_driver = {
3636 .owner = THIS_MODULE,
3639 .probe = nct6775_probe,
3640 .remove = __devexit_p(nct6775_remove),
3643 /* nct6775_find() looks for a '627 in the Super-I/O config space */
3644 static int __init nct6775_find(int sioaddr, unsigned short *addr,
3645 struct nct6775_sio_data *sio_data)
3647 static const char __initdata sio_name_NCT6775[] = "NCT6775F";
3648 static const char __initdata sio_name_NCT6776[] = "NCT6776F";
3649 static const char __initdata sio_name_NCT6779[] = "NCT6779D";
3652 const char *sio_name;
3654 superio_enter(sioaddr);
3659 val = (superio_inb(sioaddr, SIO_REG_DEVID) << 8)
3660 | superio_inb(sioaddr, SIO_REG_DEVID + 1);
3661 switch (val & SIO_ID_MASK) {
3662 case SIO_NCT6775_ID:
3663 sio_data->kind = nct6775;
3664 sio_name = sio_name_NCT6775;
3666 case SIO_NCT6776_ID:
3667 sio_data->kind = nct6776;
3668 sio_name = sio_name_NCT6776;
3670 case SIO_NCT6779_ID:
3671 sio_data->kind = nct6779;
3672 sio_name = sio_name_NCT6779;
3676 pr_debug("unsupported chip ID: 0x%04x\n", val);
3677 superio_exit(sioaddr);
3681 /* We have a known chip, find the HWM I/O address */
3682 superio_select(sioaddr, NCT6775_LD_HWM);
3683 val = (superio_inb(sioaddr, SIO_REG_ADDR) << 8)
3684 | superio_inb(sioaddr, SIO_REG_ADDR + 1);
3685 *addr = val & IOREGION_ALIGNMENT;
3687 pr_err("Refusing to enable a Super-I/O device with a base I/O port 0\n");
3688 superio_exit(sioaddr);
3692 /* Activate logical device if needed */
3693 val = superio_inb(sioaddr, SIO_REG_ENABLE);
3694 if (!(val & 0x01)) {
3695 pr_warn("Forcibly enabling Super-I/O. "
3696 "Sensor is probably unusable.\n");
3697 superio_outb(sioaddr, SIO_REG_ENABLE, val | 0x01);
3700 superio_exit(sioaddr);
3701 pr_info("Found %s chip at %#x\n", sio_name, *addr);
3702 sio_data->sioreg = sioaddr;
3708 * when Super-I/O functions move to a separate file, the Super-I/O
3709 * bus will manage the lifetime of the device and this module will only keep
3710 * track of the nct6775 driver. But since we platform_device_alloc(), we
3711 * must keep track of the device
3713 static struct platform_device *pdev;
3715 static int __init sensors_nct6775_init(void)
3718 unsigned short address;
3719 struct resource res;
3720 struct nct6775_sio_data sio_data;
3723 * initialize sio_data->kind and sio_data->sioreg.
3725 * when Super-I/O functions move to a separate file, the Super-I/O
3726 * driver will probe 0x2e and 0x4e and auto-detect the presence of a
3727 * nct6775 hardware monitor, and call probe()
3729 if (nct6775_find(0x2e, &address, &sio_data) &&
3730 nct6775_find(0x4e, &address, &sio_data))
3733 err = platform_driver_register(&nct6775_driver);
3737 pdev = platform_device_alloc(DRVNAME, address);
3740 pr_err("Device allocation failed\n");
3741 goto exit_unregister;
3744 err = platform_device_add_data(pdev, &sio_data,
3745 sizeof(struct nct6775_sio_data));
3747 pr_err("Platform data allocation failed\n");
3748 goto exit_device_put;
3751 memset(&res, 0, sizeof(res));
3753 res.start = address + IOREGION_OFFSET;
3754 res.end = address + IOREGION_OFFSET + IOREGION_LENGTH - 1;
3755 res.flags = IORESOURCE_IO;
3757 err = acpi_check_resource_conflict(&res);
3759 goto exit_device_put;
3761 err = platform_device_add_resources(pdev, &res, 1);
3763 pr_err("Device resource addition failed (%d)\n", err);
3764 goto exit_device_put;
3767 /* platform_device_add calls probe() */
3768 err = platform_device_add(pdev);
3770 pr_err("Device addition failed (%d)\n", err);
3771 goto exit_device_put;
3777 platform_device_put(pdev);
3779 platform_driver_unregister(&nct6775_driver);
3784 static void __exit sensors_nct6775_exit(void)
3786 platform_device_unregister(pdev);
3787 platform_driver_unregister(&nct6775_driver);
3790 MODULE_AUTHOR("Guenter Roeck <linux@roeck-us.net>");
3791 MODULE_DESCRIPTION("NCT6775F/NCT6776F/NCT6779D driver");
3792 MODULE_LICENSE("GPL");
3794 module_init(sensors_nct6775_init);
3795 module_exit(sensors_nct6775_exit);