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_FAN_PULSES[] = { 0x641, 0x642, 0x643, 0x644, 0 };
230 static const u16 NCT6776_REG_FAN_PULSES[] = { 0x644, 0x645, 0x646, 0, 0 };
231 static const u16 NCT6779_REG_FAN_PULSES[]
232 = { 0x644, 0x645, 0x646, 0x647, 0x648 };
234 static const u16 NCT6775_REG_TARGET[] = { 0x101, 0x201, 0x301, 0x801, 0x901 };
235 static const u16 NCT6775_REG_FAN_MODE[] = { 0x102, 0x202, 0x302, 0x802, 0x902 };
236 static const u16 NCT6775_REG_FAN_STEP_DOWN_TIME[] = {
237 0x103, 0x203, 0x303, 0x803, 0x903 };
238 static const u16 NCT6775_REG_FAN_STEP_UP_TIME[] = {
239 0x104, 0x204, 0x304, 0x804, 0x904 };
240 static const u16 NCT6775_REG_FAN_STOP_OUTPUT[] = {
241 0x105, 0x205, 0x305, 0x805, 0x905 };
242 static const u16 NCT6775_REG_FAN_START_OUTPUT[]
243 = { 0x106, 0x206, 0x306, 0x806, 0x906 };
244 static const u16 NCT6775_REG_FAN_MAX_OUTPUT[] = { 0x10a, 0x20a, 0x30a };
245 static const u16 NCT6775_REG_FAN_STEP_OUTPUT[] = { 0x10b, 0x20b, 0x30b };
247 static const u16 NCT6775_REG_FAN_STOP_TIME[]
248 = { 0x107, 0x207, 0x307, 0x807, 0x907 };
249 static const u16 NCT6775_REG_PWM[] = { 0x109, 0x209, 0x309, 0x809, 0x909 };
250 static const u16 NCT6775_REG_PWM_READ[] = { 0x01, 0x03, 0x11, 0x13, 0x15 };
252 static const u16 NCT6775_REG_FAN[] = { 0x630, 0x632, 0x634, 0x636, 0x638 };
254 static const u16 NCT6776_REG_FAN_MIN[] = { 0x63a, 0x63c, 0x63e, 0x640, 0x642};
256 static const u16 NCT6779_REG_TOLERANCE_H[]
257 = { 0x10c, 0x20c, 0x30c, 0x80c, 0x90c };
259 static const u16 NCT6779_REG_FAN[] = { 0x4b0, 0x4b2, 0x4b4, 0x4b6, 0x4b8 };
261 static const u16 NCT6775_REG_TEMP[]
262 = { 0x27, 0x150, 0x250, 0x62b, 0x62c, 0x62d };
263 static const u16 NCT6779_REG_TEMP[] = { 0x27, 0x150 };
265 static const u16 NCT6775_REG_TEMP_CONFIG[]
266 = { 0, 0x152, 0x252, 0x628, 0x629, 0x62A };
267 static const u16 NCT6775_REG_TEMP_HYST[]
268 = { 0x3a, 0x153, 0x253, 0x673, 0x678, 0x67D };
269 static const u16 NCT6775_REG_TEMP_OVER[]
270 = { 0x39, 0x155, 0x255, 0x672, 0x677, 0x67C };
272 static const u16 NCT6775_REG_TEMP_SOURCE[]
273 = { 0x621, 0x622, 0x623, 0x624, 0x625, 0x626 };
275 static const u16 NCT6775_REG_TEMP_SEL[]
276 = { 0x100, 0x200, 0x300, 0x800, 0x900 };
278 static const u16 NCT6775_REG_WEIGHT_TEMP_SEL[]
279 = { 0x139, 0x239, 0x339, 0x839, 0x939 };
280 static const u16 NCT6775_REG_WEIGHT_TEMP_STEP[]
281 = { 0x13a, 0x23a, 0x33a, 0x83a, 0x93a };
282 static const u16 NCT6775_REG_WEIGHT_TEMP_STEP_TOL[]
283 = { 0x13b, 0x23b, 0x33b, 0x83b, 0x93b };
284 static const u16 NCT6775_REG_WEIGHT_DUTY_STEP[]
285 = { 0x13c, 0x23c, 0x33c, 0x83c, 0x93c };
286 static const u16 NCT6775_REG_WEIGHT_TEMP_BASE[]
287 = { 0x13d, 0x23d, 0x33d, 0x83d, 0x93d };
289 static const u16 NCT6776_REG_WEIGHT_DUTY_BASE[]
290 = { 0x13e, 0x23e, 0x33e, 0x83e, 0x93e };
292 static const u16 NCT6775_REG_TEMP_OFFSET[] = { 0x454, 0x455, 0x456 };
294 static const u16 NCT6779_REG_TEMP_OFFSET[]
295 = { 0x454, 0x455, 0x456, 0x44a, 0x44b, 0x44c };
297 static const u16 NCT6776_REG_TEMP_CONFIG[11]
298 = { 0x18, 0x152, 0x252, 0x628, 0x629, 0x62A };
300 static const u16 NCT6775_REG_TEMP_MON[] = { 0x73, 0x75, 0x77, 0x79, 0x7b };
302 static const u16 NCT6775_REG_AUTO_TEMP[]
303 = { 0x121, 0x221, 0x321, 0x821, 0x921 };
304 static const u16 NCT6775_REG_AUTO_PWM[]
305 = { 0x127, 0x227, 0x327, 0x827, 0x927 };
307 #define NCT6775_AUTO_TEMP(data, nr, p) ((data)->REG_AUTO_TEMP[nr] + (p))
308 #define NCT6775_AUTO_PWM(data, nr, p) ((data)->REG_AUTO_PWM[nr] + (p))
310 static const u16 NCT6775_REG_CRITICAL_ENAB[] = { 0x134, 0x234, 0x334 };
312 static const u16 NCT6775_REG_CRITICAL_TEMP[] = {
313 0x135, 0x235, 0x335, 0x835, 0x935 };
314 static const u16 NCT6775_REG_CRITICAL_TEMP_TOLERANCE[] = {
315 0x138, 0x238, 0x338, 0x838, 0x938 };
317 static const u16 NCT6779_REG_CRITICAL_PWM_ENABLE[] = {
318 0x136, 0x236, 0x336, 0x836, 0x936 };
319 static const u16 NCT6779_REG_CRITICAL_PWM[] = {
320 0x137, 0x237, 0x337, 0x837, 0x937 };
322 static const char *const nct6775_temp_label[] = {
336 "PCH_CHIP_CPU_MAX_TEMP",
346 static const char *const nct6776_temp_label[] = {
361 "PCH_CHIP_CPU_MAX_TEMP",
372 static const char *const nct6779_temp_label[] = {
391 "PCH_CHIP_CPU_MAX_TEMP",
402 static const u16 NCT6775_REG_TEMP_ALTERNATE[ARRAY_SIZE(nct6775_temp_label)]
403 = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0x661, 0x662, 0x664 };
405 static const u16 NCT6776_REG_TEMP_ALTERNATE[ARRAY_SIZE(nct6776_temp_label)]
406 = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0x401, 0x402, 0x404 };
408 static const u16 NCT6779_REG_TEMP_ALTERNATE[ARRAY_SIZE(nct6779_temp_label)]
409 = { 0x490, 0x491, 0x492, 0x493, 0x494, 0x495, 0, 0,
410 0, 0, 0, 0, 0, 0, 0, 0,
411 0, 0, 0x401, 0x402, 0x404 };
413 #define NUM_TEMP 10 /* Max number of temp attribute sets w/ limits*/
414 #define NUM_TEMP_FIXED 6 /* Max number of fixed temp attribute sets */
416 #define NUM_REG_TEMP ARRAY_SIZE(NCT6775_REG_TEMP)
418 static inline int reg_to_pwm_enable(int pwm, int mode)
420 if (mode == 0 && pwm == 255)
425 static inline int pwm_enable_to_reg(int mode)
436 /* 1 is DC mode, output in ms */
437 static inline unsigned int step_time_from_reg(u8 reg, u8 mode)
439 return mode ? 400 * reg : 100 * reg;
442 static inline u8 step_time_to_reg(unsigned int msec, u8 mode)
444 return SENSORS_LIMIT((mode ? (msec + 200) / 400 :
445 (msec + 50) / 100), 1, 255);
448 static unsigned int fan_from_reg8(u16 reg, unsigned int divreg)
450 if (reg == 0 || reg == 255)
452 return 1350000U / (reg << divreg);
455 static unsigned int fan_from_reg13(u16 reg, unsigned int divreg)
457 if ((reg & 0xff1f) == 0xff1f)
460 reg = (reg & 0x1f) | ((reg & 0xff00) >> 3);
465 return 1350000U / reg;
468 static unsigned int fan_from_reg16(u16 reg, unsigned int divreg)
470 if (reg == 0 || reg == 0xffff)
474 * Even though the registers are 16 bit wide, the fan divisor
477 return 1350000U / (reg << divreg);
480 static inline unsigned int
487 * Some of the voltage inputs have internal scaling, the tables below
488 * contain 8 (the ADC LSB in mV) * scaling factor * 100
490 static const u16 scale_in[15] = {
491 800, 800, 1600, 1600, 800, 800, 800, 1600, 1600, 800, 800, 800, 800,
495 static inline long in_from_reg(u8 reg, u8 nr)
497 return DIV_ROUND_CLOSEST(reg * scale_in[nr], 100);
500 static inline u8 in_to_reg(u32 val, u8 nr)
502 return SENSORS_LIMIT(DIV_ROUND_CLOSEST(val * 100, scale_in[nr]), 0,
507 * Data structures and manipulation thereof
510 struct nct6775_data {
511 int addr; /* IO base of hw monitor block */
515 struct device *hwmon_dev;
518 u16 reg_temp[3][NUM_TEMP]; /* 0=temp, 1=temp_over, 2=temp_hyst */
519 u8 temp_src[NUM_TEMP];
520 u16 reg_temp_config[NUM_TEMP];
521 const char * const *temp_label;
528 const s8 *ALARM_BITS;
531 const u16 *REG_IN_MINMAX[2];
533 const u16 *REG_TARGET;
535 const u16 *REG_FAN_MODE;
536 const u16 *REG_FAN_MIN;
537 const u16 *REG_FAN_TIME[3];
539 const u8 *REG_PWM_MODE;
540 const u8 *PWM_MODE_MASK;
542 const u16 *REG_PWM[7]; /* [0]=pwm, [1]=start_output, [2]=stop_output,
543 * [3]=max_output, [4]=step_output,
544 * [5]=weight_duty_step, [6]=weight_duty_base
546 const u16 *REG_PWM_READ;
548 const u16 *REG_TEMP_ALTERNATE;
549 const u16 *REG_TEMP_MON;
550 const u16 *REG_AUTO_TEMP;
551 const u16 *REG_AUTO_PWM;
553 const u16 *REG_CRITICAL_TEMP;
554 const u16 *REG_CRITICAL_TEMP_TOLERANCE;
556 const u16 *REG_TEMP_SOURCE; /* temp register sources */
557 const u16 *REG_TEMP_SEL[2]; /* pwm temp, 0=base, 1=weight */
559 const u16 *REG_WEIGHT_TEMP[3]; /* 0=base, 1=hyst, 2=step */
561 const u16 *REG_TEMP_OFFSET;
563 const u16 *REG_ALARM;
565 unsigned int (*fan_from_reg)(u16 reg, unsigned int divreg);
566 unsigned int (*fan_from_reg_min)(u16 reg, unsigned int divreg);
568 struct mutex update_lock;
569 char valid; /* !=0 if following fields are valid */
570 unsigned long last_updated; /* In jiffies */
572 /* Register values */
573 u8 bank; /* current register bank */
574 u8 in_num; /* number of in inputs we have */
575 u8 in[15][3]; /* [0]=in, [1]=in_max, [2]=in_min */
580 u8 has_fan; /* some fan inputs can be disabled */
581 u8 has_fan_min; /* some fans don't have min register */
584 u8 temp_fixed_num; /* 3 or 6 */
585 u8 temp_type[NUM_TEMP_FIXED];
586 s8 temp_offset[NUM_TEMP_FIXED];
587 s16 temp[3][NUM_TEMP]; /* 0=temp, 1=temp_over, 2=temp_hyst */
590 u8 pwm_num; /* number of pwm */
592 u8 pwm_mode[5]; /* 1->DC variable voltage, 0->PWM variable duty cycle */
593 u8 pwm_enable[5]; /* 0->off
595 * 2->thermal cruise mode (also called SmartFan I)
596 * 3->fan speed cruise mode
598 * 5->enhanced variable thermal cruise (SmartFan IV)
600 u8 pwm[7][5]; /* [0]=pwm, [1]=start_output, [2]=stop_output,
601 * [3]=max_output, [4]=step_output,
602 * [5]=weight_duty_step, [6]=weight_duty_base
608 u8 fan_time[3][5]; /* 0 = stop_time, 1 = step_up, 2 = step_down */
610 /* Automatic fan speed control registers */
615 u8 pwm_temp_sel[2][5];
617 bool pwm_sel_enable[2][5];/* 0->stop_val, 1->weight;
618 * false->off, true->on
620 u8 weight_temp[3][5]; /* 0->temp_step, 1->temp_step_tol,
632 struct nct6775_sio_data {
637 static bool is_word_sized(struct nct6775_data *data, u16 reg)
639 switch (data->kind) {
641 return (((reg & 0xff00) == 0x100 ||
642 (reg & 0xff00) == 0x200) &&
643 ((reg & 0x00ff) == 0x50 ||
644 (reg & 0x00ff) == 0x53 ||
645 (reg & 0x00ff) == 0x55)) ||
646 (reg & 0xfff0) == 0x630 ||
647 reg == 0x640 || reg == 0x642 ||
649 ((reg & 0xfff0) == 0x650 && (reg & 0x000f) >= 0x06) ||
650 reg == 0x73 || reg == 0x75 || reg == 0x77;
652 return (((reg & 0xff00) == 0x100 ||
653 (reg & 0xff00) == 0x200) &&
654 ((reg & 0x00ff) == 0x50 ||
655 (reg & 0x00ff) == 0x53 ||
656 (reg & 0x00ff) == 0x55)) ||
657 (reg & 0xfff0) == 0x630 ||
659 reg == 0x640 || reg == 0x642 ||
660 ((reg & 0xfff0) == 0x650 && (reg & 0x000f) >= 0x06) ||
661 reg == 0x73 || reg == 0x75 || reg == 0x77;
663 return reg == 0x150 || reg == 0x153 || reg == 0x155 ||
664 ((reg & 0xfff0) == 0x4b0 && (reg & 0x000f) < 0x09) ||
666 reg == 0x63a || reg == 0x63c || reg == 0x63e ||
667 reg == 0x640 || reg == 0x642 ||
668 reg == 0x73 || reg == 0x75 || reg == 0x77 || reg == 0x79 ||
675 * On older chips, only registers 0x50-0x5f are banked.
676 * On more recent chips, all registers are banked.
677 * Assume that is the case and set the bank number for each access.
678 * Cache the bank number so it only needs to be set if it changes.
680 static inline void nct6775_set_bank(struct nct6775_data *data, u16 reg)
683 if (data->bank != bank) {
684 outb_p(NCT6775_REG_BANK, data->addr + ADDR_REG_OFFSET);
685 outb_p(bank, data->addr + DATA_REG_OFFSET);
690 static u16 nct6775_read_value(struct nct6775_data *data, u16 reg)
692 int res, word_sized = is_word_sized(data, reg);
694 mutex_lock(&data->lock);
696 nct6775_set_bank(data, reg);
697 outb_p(reg & 0xff, data->addr + ADDR_REG_OFFSET);
698 res = inb_p(data->addr + DATA_REG_OFFSET);
700 outb_p((reg & 0xff) + 1,
701 data->addr + ADDR_REG_OFFSET);
702 res = (res << 8) + inb_p(data->addr + DATA_REG_OFFSET);
705 mutex_unlock(&data->lock);
709 static int nct6775_write_value(struct nct6775_data *data, u16 reg, u16 value)
711 int word_sized = is_word_sized(data, reg);
713 mutex_lock(&data->lock);
715 nct6775_set_bank(data, reg);
716 outb_p(reg & 0xff, data->addr + ADDR_REG_OFFSET);
718 outb_p(value >> 8, data->addr + DATA_REG_OFFSET);
719 outb_p((reg & 0xff) + 1,
720 data->addr + ADDR_REG_OFFSET);
722 outb_p(value & 0xff, data->addr + DATA_REG_OFFSET);
724 mutex_unlock(&data->lock);
728 /* We left-align 8-bit temperature values to make the code simpler */
729 static u16 nct6775_read_temp(struct nct6775_data *data, u16 reg)
733 res = nct6775_read_value(data, reg);
734 if (!is_word_sized(data, reg))
740 static int nct6775_write_temp(struct nct6775_data *data, u16 reg, u16 value)
742 if (!is_word_sized(data, reg))
744 return nct6775_write_value(data, reg, value);
747 /* This function assumes that the caller holds data->update_lock */
748 static void nct6775_write_fan_div(struct nct6775_data *data, int nr)
754 reg = (nct6775_read_value(data, NCT6775_REG_FANDIV1) & 0x70)
755 | (data->fan_div[0] & 0x7);
756 nct6775_write_value(data, NCT6775_REG_FANDIV1, reg);
759 reg = (nct6775_read_value(data, NCT6775_REG_FANDIV1) & 0x7)
760 | ((data->fan_div[1] << 4) & 0x70);
761 nct6775_write_value(data, NCT6775_REG_FANDIV1, reg);
763 reg = (nct6775_read_value(data, NCT6775_REG_FANDIV2) & 0x70)
764 | (data->fan_div[2] & 0x7);
765 nct6775_write_value(data, NCT6775_REG_FANDIV2, reg);
768 reg = (nct6775_read_value(data, NCT6775_REG_FANDIV2) & 0x7)
769 | ((data->fan_div[3] << 4) & 0x70);
770 nct6775_write_value(data, NCT6775_REG_FANDIV2, reg);
775 static void nct6775_write_fan_div_common(struct device *dev,
776 struct nct6775_data *data, int nr)
778 if (data->kind == nct6775)
779 nct6775_write_fan_div(data, nr);
782 static void nct6775_update_fan_div(struct nct6775_data *data)
786 i = nct6775_read_value(data, NCT6775_REG_FANDIV1);
787 data->fan_div[0] = i & 0x7;
788 data->fan_div[1] = (i & 0x70) >> 4;
789 i = nct6775_read_value(data, NCT6775_REG_FANDIV2);
790 data->fan_div[2] = i & 0x7;
791 if (data->has_fan & (1<<3))
792 data->fan_div[3] = (i & 0x70) >> 4;
795 static void nct6775_update_fan_div_common(struct device *dev,
796 struct nct6775_data *data)
798 if (data->kind == nct6775)
799 nct6775_update_fan_div(data);
802 static void nct6775_update_pwm(struct device *dev)
804 struct nct6775_data *data = dev_get_drvdata(dev);
809 for (i = 0; i < data->pwm_num; i++) {
810 if (!(data->has_pwm & (1 << i)))
813 duty_is_dc = data->REG_PWM_MODE[i] &&
814 (nct6775_read_value(data, data->REG_PWM_MODE[i])
815 & data->PWM_MODE_MASK[i]);
816 data->pwm_mode[i] = duty_is_dc;
818 fanmodecfg = nct6775_read_value(data, data->REG_FAN_MODE[i]);
819 for (j = 0; j < ARRAY_SIZE(data->REG_PWM); j++) {
820 if (data->REG_PWM[j] && data->REG_PWM[j][i]) {
822 = nct6775_read_value(data,
823 data->REG_PWM[j][i]);
827 data->pwm_enable[i] = reg_to_pwm_enable(data->pwm[0][i],
828 (fanmodecfg >> 4) & 7);
830 data->tolerance[i][0] = fanmodecfg & 0x0f;
831 if (data->kind == nct6779) {
832 reg = nct6775_read_value(data,
833 NCT6779_REG_TOLERANCE_H[i]);
834 data->tolerance[i][0] |= (reg & 0x70) >> 1;
836 data->tolerance[i][1] =
837 nct6775_read_value(data,
838 data->REG_CRITICAL_TEMP_TOLERANCE[i]);
840 data->pwm_sel_enable[0][i] =
841 nct6775_read_value(data, data->REG_TEMP_SEL[0][i])
843 data->pwm_sel_enable[1][i] =
844 nct6775_read_value(data, data->REG_TEMP_SEL[1][i])
848 for (j = 0; j < 2; j++) {
849 reg = nct6775_read_value(data,
850 data->REG_TEMP_SEL[j][i]);
851 data->pwm_temp_sel[j][i] = reg & 0x1f;
853 /* Weight temp data */
854 for (j = 0; j < 3; j++) {
855 data->weight_temp[j][i]
856 = nct6775_read_value(data,
857 data->REG_WEIGHT_TEMP[j][i]);
862 static void nct6775_update_pwm_limits(struct device *dev)
864 struct nct6775_data *data = dev_get_drvdata(dev);
868 for (i = 0; i < data->pwm_num; i++) {
869 if (!(data->has_pwm & (1 << i)))
872 for (j = 0; j < 3; j++) {
873 data->fan_time[j][i] =
874 nct6775_read_value(data, data->REG_FAN_TIME[j][i]);
877 data->target_temp[i] =
878 nct6775_read_value(data, data->REG_TARGET[i]) &
879 (data->pwm_mode[i] ? 0xff : 0x7f);
881 nct6775_read_value(data, data->REG_TEMP_MON[i]);
883 for (j = 0; j < data->auto_pwm_num; j++) {
884 data->auto_pwm[i][j] =
885 nct6775_read_value(data,
886 NCT6775_AUTO_PWM(data, i, j));
887 data->auto_temp[i][j] =
888 nct6775_read_value(data,
889 NCT6775_AUTO_TEMP(data, i, j));
892 /* critical auto_pwm temperature data */
893 data->auto_temp[i][data->auto_pwm_num] =
894 nct6775_read_value(data, data->REG_CRITICAL_TEMP[i]);
896 switch (data->kind) {
898 reg = nct6775_read_value(data,
899 NCT6775_REG_CRITICAL_ENAB[i]);
900 data->auto_pwm[i][data->auto_pwm_num] =
901 (reg & 0x02) ? 0xff : 0x00;
904 data->auto_pwm[i][data->auto_pwm_num] = 0xff;
907 reg = nct6775_read_value(data,
908 NCT6779_REG_CRITICAL_PWM_ENABLE[i]);
910 data->auto_pwm[i][data->auto_pwm_num] =
911 nct6775_read_value(data,
912 NCT6779_REG_CRITICAL_PWM[i]);
914 data->auto_pwm[i][data->auto_pwm_num] = 0xff;
920 static struct nct6775_data *nct6775_update_device(struct device *dev)
922 struct nct6775_data *data = dev_get_drvdata(dev);
925 mutex_lock(&data->update_lock);
927 if (time_after(jiffies, data->last_updated + HZ + HZ/2)
929 /* Fan clock dividers */
930 nct6775_update_fan_div_common(dev, data);
932 /* Measured voltages and limits */
933 for (i = 0; i < data->in_num; i++) {
934 if (!(data->have_in & (1 << i)))
937 data->in[i][0] = nct6775_read_value(data,
939 data->in[i][1] = nct6775_read_value(data,
940 data->REG_IN_MINMAX[0][i]);
941 data->in[i][2] = nct6775_read_value(data,
942 data->REG_IN_MINMAX[1][i]);
945 /* Measured fan speeds and limits */
946 for (i = 0; i < 5; i++) {
949 if (!(data->has_fan & (1 << i)))
952 reg = nct6775_read_value(data, data->REG_FAN[i]);
953 data->rpm[i] = data->fan_from_reg(reg,
956 if (data->has_fan_min & (1 << i))
957 data->fan_min[i] = nct6775_read_value(data,
958 data->REG_FAN_MIN[i]);
961 * If we failed to measure the fan speed and clock
962 * divider can be increased, let's try that for next
965 if (data->has_fan_div
966 && (reg >= 0xff || (data->kind == nct6775
968 && data->fan_div[i] < 0x07) {
970 "Increasing fan%d clock divider from %u to %u\n",
971 i + 1, div_from_reg(data->fan_div[i]),
972 div_from_reg(data->fan_div[i] + 1));
974 nct6775_write_fan_div_common(dev, data, i);
975 /* Preserve min limit if possible */
976 if ((data->has_fan_min & (1 << i))
977 && data->fan_min[i] >= 2
978 && data->fan_min[i] != 255)
979 nct6775_write_value(data,
980 data->REG_FAN_MIN[i],
981 (data->fan_min[i] /= 2));
985 nct6775_update_pwm(dev);
986 nct6775_update_pwm_limits(dev);
988 /* Measured temperatures and limits */
989 for (i = 0; i < NUM_TEMP; i++) {
990 if (!(data->have_temp & (1 << i)))
992 for (j = 0; j < 3; j++) {
993 if (data->reg_temp[j][i])
995 = nct6775_read_temp(data,
996 data->reg_temp[j][i]);
998 if (!(data->have_temp_fixed & (1 << i)))
1000 data->temp_offset[i]
1001 = nct6775_read_value(data, data->REG_TEMP_OFFSET[i]);
1005 for (i = 0; i < 6; i++) {
1007 if (!data->REG_ALARM[i])
1009 alarm = nct6775_read_value(data, data->REG_ALARM[i]);
1010 data->alarms |= ((u64)alarm) << (i << 3);
1013 data->last_updated = jiffies;
1017 mutex_unlock(&data->update_lock);
1022 * Sysfs callback functions
1025 show_in_reg(struct device *dev, struct device_attribute *attr, char *buf)
1027 struct nct6775_data *data = nct6775_update_device(dev);
1028 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
1030 int index = sattr->index;
1031 return sprintf(buf, "%ld\n", in_from_reg(data->in[nr][index], nr));
1035 store_in_reg(struct device *dev, struct device_attribute *attr, const char *buf,
1038 struct nct6775_data *data = dev_get_drvdata(dev);
1039 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
1041 int index = sattr->index;
1043 int err = kstrtoul(buf, 10, &val);
1046 mutex_lock(&data->update_lock);
1047 data->in[nr][index] = in_to_reg(val, nr);
1048 nct6775_write_value(data, data->REG_IN_MINMAX[index-1][nr],
1049 data->in[nr][index]);
1050 mutex_unlock(&data->update_lock);
1055 show_alarm(struct device *dev, struct device_attribute *attr, char *buf)
1057 struct nct6775_data *data = nct6775_update_device(dev);
1058 struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr);
1059 int nr = data->ALARM_BITS[sattr->index];
1060 return sprintf(buf, "%u\n",
1061 (unsigned int)((data->alarms >> nr) & 0x01));
1064 static SENSOR_DEVICE_ATTR_2(in0_input, S_IRUGO, show_in_reg, NULL, 0, 0);
1065 static SENSOR_DEVICE_ATTR_2(in1_input, S_IRUGO, show_in_reg, NULL, 1, 0);
1066 static SENSOR_DEVICE_ATTR_2(in2_input, S_IRUGO, show_in_reg, NULL, 2, 0);
1067 static SENSOR_DEVICE_ATTR_2(in3_input, S_IRUGO, show_in_reg, NULL, 3, 0);
1068 static SENSOR_DEVICE_ATTR_2(in4_input, S_IRUGO, show_in_reg, NULL, 4, 0);
1069 static SENSOR_DEVICE_ATTR_2(in5_input, S_IRUGO, show_in_reg, NULL, 5, 0);
1070 static SENSOR_DEVICE_ATTR_2(in6_input, S_IRUGO, show_in_reg, NULL, 6, 0);
1071 static SENSOR_DEVICE_ATTR_2(in7_input, S_IRUGO, show_in_reg, NULL, 7, 0);
1072 static SENSOR_DEVICE_ATTR_2(in8_input, S_IRUGO, show_in_reg, NULL, 8, 0);
1073 static SENSOR_DEVICE_ATTR_2(in9_input, S_IRUGO, show_in_reg, NULL, 9, 0);
1074 static SENSOR_DEVICE_ATTR_2(in10_input, S_IRUGO, show_in_reg, NULL, 10, 0);
1075 static SENSOR_DEVICE_ATTR_2(in11_input, S_IRUGO, show_in_reg, NULL, 11, 0);
1076 static SENSOR_DEVICE_ATTR_2(in12_input, S_IRUGO, show_in_reg, NULL, 12, 0);
1077 static SENSOR_DEVICE_ATTR_2(in13_input, S_IRUGO, show_in_reg, NULL, 13, 0);
1078 static SENSOR_DEVICE_ATTR_2(in14_input, S_IRUGO, show_in_reg, NULL, 14, 0);
1080 static SENSOR_DEVICE_ATTR(in0_alarm, S_IRUGO, show_alarm, NULL, 0);
1081 static SENSOR_DEVICE_ATTR(in1_alarm, S_IRUGO, show_alarm, NULL, 1);
1082 static SENSOR_DEVICE_ATTR(in2_alarm, S_IRUGO, show_alarm, NULL, 2);
1083 static SENSOR_DEVICE_ATTR(in3_alarm, S_IRUGO, show_alarm, NULL, 3);
1084 static SENSOR_DEVICE_ATTR(in4_alarm, S_IRUGO, show_alarm, NULL, 4);
1085 static SENSOR_DEVICE_ATTR(in5_alarm, S_IRUGO, show_alarm, NULL, 5);
1086 static SENSOR_DEVICE_ATTR(in6_alarm, S_IRUGO, show_alarm, NULL, 6);
1087 static SENSOR_DEVICE_ATTR(in7_alarm, S_IRUGO, show_alarm, NULL, 7);
1088 static SENSOR_DEVICE_ATTR(in8_alarm, S_IRUGO, show_alarm, NULL, 8);
1089 static SENSOR_DEVICE_ATTR(in9_alarm, S_IRUGO, show_alarm, NULL, 9);
1090 static SENSOR_DEVICE_ATTR(in10_alarm, S_IRUGO, show_alarm, NULL, 10);
1091 static SENSOR_DEVICE_ATTR(in11_alarm, S_IRUGO, show_alarm, NULL, 11);
1092 static SENSOR_DEVICE_ATTR(in12_alarm, S_IRUGO, show_alarm, NULL, 12);
1093 static SENSOR_DEVICE_ATTR(in13_alarm, S_IRUGO, show_alarm, NULL, 13);
1094 static SENSOR_DEVICE_ATTR(in14_alarm, S_IRUGO, show_alarm, NULL, 14);
1096 static SENSOR_DEVICE_ATTR_2(in0_min, S_IWUSR | S_IRUGO, show_in_reg,
1097 store_in_reg, 0, 1);
1098 static SENSOR_DEVICE_ATTR_2(in1_min, S_IWUSR | S_IRUGO, show_in_reg,
1099 store_in_reg, 1, 1);
1100 static SENSOR_DEVICE_ATTR_2(in2_min, S_IWUSR | S_IRUGO, show_in_reg,
1101 store_in_reg, 2, 1);
1102 static SENSOR_DEVICE_ATTR_2(in3_min, S_IWUSR | S_IRUGO, show_in_reg,
1103 store_in_reg, 3, 1);
1104 static SENSOR_DEVICE_ATTR_2(in4_min, S_IWUSR | S_IRUGO, show_in_reg,
1105 store_in_reg, 4, 1);
1106 static SENSOR_DEVICE_ATTR_2(in5_min, S_IWUSR | S_IRUGO, show_in_reg,
1107 store_in_reg, 5, 1);
1108 static SENSOR_DEVICE_ATTR_2(in6_min, S_IWUSR | S_IRUGO, show_in_reg,
1109 store_in_reg, 6, 1);
1110 static SENSOR_DEVICE_ATTR_2(in7_min, S_IWUSR | S_IRUGO, show_in_reg,
1111 store_in_reg, 7, 1);
1112 static SENSOR_DEVICE_ATTR_2(in8_min, S_IWUSR | S_IRUGO, show_in_reg,
1113 store_in_reg, 8, 1);
1114 static SENSOR_DEVICE_ATTR_2(in9_min, S_IWUSR | S_IRUGO, show_in_reg,
1115 store_in_reg, 9, 1);
1116 static SENSOR_DEVICE_ATTR_2(in10_min, S_IWUSR | S_IRUGO, show_in_reg,
1117 store_in_reg, 10, 1);
1118 static SENSOR_DEVICE_ATTR_2(in11_min, S_IWUSR | S_IRUGO, show_in_reg,
1119 store_in_reg, 11, 1);
1120 static SENSOR_DEVICE_ATTR_2(in12_min, S_IWUSR | S_IRUGO, show_in_reg,
1121 store_in_reg, 12, 1);
1122 static SENSOR_DEVICE_ATTR_2(in13_min, S_IWUSR | S_IRUGO, show_in_reg,
1123 store_in_reg, 13, 1);
1124 static SENSOR_DEVICE_ATTR_2(in14_min, S_IWUSR | S_IRUGO, show_in_reg,
1125 store_in_reg, 14, 1);
1127 static SENSOR_DEVICE_ATTR_2(in0_max, S_IWUSR | S_IRUGO, show_in_reg,
1128 store_in_reg, 0, 2);
1129 static SENSOR_DEVICE_ATTR_2(in1_max, S_IWUSR | S_IRUGO, show_in_reg,
1130 store_in_reg, 1, 2);
1131 static SENSOR_DEVICE_ATTR_2(in2_max, S_IWUSR | S_IRUGO, show_in_reg,
1132 store_in_reg, 2, 2);
1133 static SENSOR_DEVICE_ATTR_2(in3_max, S_IWUSR | S_IRUGO, show_in_reg,
1134 store_in_reg, 3, 2);
1135 static SENSOR_DEVICE_ATTR_2(in4_max, S_IWUSR | S_IRUGO, show_in_reg,
1136 store_in_reg, 4, 2);
1137 static SENSOR_DEVICE_ATTR_2(in5_max, S_IWUSR | S_IRUGO, show_in_reg,
1138 store_in_reg, 5, 2);
1139 static SENSOR_DEVICE_ATTR_2(in6_max, S_IWUSR | S_IRUGO, show_in_reg,
1140 store_in_reg, 6, 2);
1141 static SENSOR_DEVICE_ATTR_2(in7_max, S_IWUSR | S_IRUGO, show_in_reg,
1142 store_in_reg, 7, 2);
1143 static SENSOR_DEVICE_ATTR_2(in8_max, S_IWUSR | S_IRUGO, show_in_reg,
1144 store_in_reg, 8, 2);
1145 static SENSOR_DEVICE_ATTR_2(in9_max, S_IWUSR | S_IRUGO, show_in_reg,
1146 store_in_reg, 9, 2);
1147 static SENSOR_DEVICE_ATTR_2(in10_max, S_IWUSR | S_IRUGO, show_in_reg,
1148 store_in_reg, 10, 2);
1149 static SENSOR_DEVICE_ATTR_2(in11_max, S_IWUSR | S_IRUGO, show_in_reg,
1150 store_in_reg, 11, 2);
1151 static SENSOR_DEVICE_ATTR_2(in12_max, S_IWUSR | S_IRUGO, show_in_reg,
1152 store_in_reg, 12, 2);
1153 static SENSOR_DEVICE_ATTR_2(in13_max, S_IWUSR | S_IRUGO, show_in_reg,
1154 store_in_reg, 13, 2);
1155 static SENSOR_DEVICE_ATTR_2(in14_max, S_IWUSR | S_IRUGO, show_in_reg,
1156 store_in_reg, 14, 2);
1158 static struct attribute *nct6775_attributes_in[15][5] = {
1160 &sensor_dev_attr_in0_input.dev_attr.attr,
1161 &sensor_dev_attr_in0_min.dev_attr.attr,
1162 &sensor_dev_attr_in0_max.dev_attr.attr,
1163 &sensor_dev_attr_in0_alarm.dev_attr.attr,
1167 &sensor_dev_attr_in1_input.dev_attr.attr,
1168 &sensor_dev_attr_in1_min.dev_attr.attr,
1169 &sensor_dev_attr_in1_max.dev_attr.attr,
1170 &sensor_dev_attr_in1_alarm.dev_attr.attr,
1174 &sensor_dev_attr_in2_input.dev_attr.attr,
1175 &sensor_dev_attr_in2_min.dev_attr.attr,
1176 &sensor_dev_attr_in2_max.dev_attr.attr,
1177 &sensor_dev_attr_in2_alarm.dev_attr.attr,
1181 &sensor_dev_attr_in3_input.dev_attr.attr,
1182 &sensor_dev_attr_in3_min.dev_attr.attr,
1183 &sensor_dev_attr_in3_max.dev_attr.attr,
1184 &sensor_dev_attr_in3_alarm.dev_attr.attr,
1188 &sensor_dev_attr_in4_input.dev_attr.attr,
1189 &sensor_dev_attr_in4_min.dev_attr.attr,
1190 &sensor_dev_attr_in4_max.dev_attr.attr,
1191 &sensor_dev_attr_in4_alarm.dev_attr.attr,
1195 &sensor_dev_attr_in5_input.dev_attr.attr,
1196 &sensor_dev_attr_in5_min.dev_attr.attr,
1197 &sensor_dev_attr_in5_max.dev_attr.attr,
1198 &sensor_dev_attr_in5_alarm.dev_attr.attr,
1202 &sensor_dev_attr_in6_input.dev_attr.attr,
1203 &sensor_dev_attr_in6_min.dev_attr.attr,
1204 &sensor_dev_attr_in6_max.dev_attr.attr,
1205 &sensor_dev_attr_in6_alarm.dev_attr.attr,
1209 &sensor_dev_attr_in7_input.dev_attr.attr,
1210 &sensor_dev_attr_in7_min.dev_attr.attr,
1211 &sensor_dev_attr_in7_max.dev_attr.attr,
1212 &sensor_dev_attr_in7_alarm.dev_attr.attr,
1216 &sensor_dev_attr_in8_input.dev_attr.attr,
1217 &sensor_dev_attr_in8_min.dev_attr.attr,
1218 &sensor_dev_attr_in8_max.dev_attr.attr,
1219 &sensor_dev_attr_in8_alarm.dev_attr.attr,
1223 &sensor_dev_attr_in9_input.dev_attr.attr,
1224 &sensor_dev_attr_in9_min.dev_attr.attr,
1225 &sensor_dev_attr_in9_max.dev_attr.attr,
1226 &sensor_dev_attr_in9_alarm.dev_attr.attr,
1230 &sensor_dev_attr_in10_input.dev_attr.attr,
1231 &sensor_dev_attr_in10_min.dev_attr.attr,
1232 &sensor_dev_attr_in10_max.dev_attr.attr,
1233 &sensor_dev_attr_in10_alarm.dev_attr.attr,
1237 &sensor_dev_attr_in11_input.dev_attr.attr,
1238 &sensor_dev_attr_in11_min.dev_attr.attr,
1239 &sensor_dev_attr_in11_max.dev_attr.attr,
1240 &sensor_dev_attr_in11_alarm.dev_attr.attr,
1244 &sensor_dev_attr_in12_input.dev_attr.attr,
1245 &sensor_dev_attr_in12_min.dev_attr.attr,
1246 &sensor_dev_attr_in12_max.dev_attr.attr,
1247 &sensor_dev_attr_in12_alarm.dev_attr.attr,
1251 &sensor_dev_attr_in13_input.dev_attr.attr,
1252 &sensor_dev_attr_in13_min.dev_attr.attr,
1253 &sensor_dev_attr_in13_max.dev_attr.attr,
1254 &sensor_dev_attr_in13_alarm.dev_attr.attr,
1258 &sensor_dev_attr_in14_input.dev_attr.attr,
1259 &sensor_dev_attr_in14_min.dev_attr.attr,
1260 &sensor_dev_attr_in14_max.dev_attr.attr,
1261 &sensor_dev_attr_in14_alarm.dev_attr.attr,
1266 static const struct attribute_group nct6775_group_in[15] = {
1267 { .attrs = nct6775_attributes_in[0] },
1268 { .attrs = nct6775_attributes_in[1] },
1269 { .attrs = nct6775_attributes_in[2] },
1270 { .attrs = nct6775_attributes_in[3] },
1271 { .attrs = nct6775_attributes_in[4] },
1272 { .attrs = nct6775_attributes_in[5] },
1273 { .attrs = nct6775_attributes_in[6] },
1274 { .attrs = nct6775_attributes_in[7] },
1275 { .attrs = nct6775_attributes_in[8] },
1276 { .attrs = nct6775_attributes_in[9] },
1277 { .attrs = nct6775_attributes_in[10] },
1278 { .attrs = nct6775_attributes_in[11] },
1279 { .attrs = nct6775_attributes_in[12] },
1280 { .attrs = nct6775_attributes_in[13] },
1281 { .attrs = nct6775_attributes_in[14] },
1285 show_fan(struct device *dev, struct device_attribute *attr, char *buf)
1287 struct nct6775_data *data = nct6775_update_device(dev);
1288 struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr);
1289 int nr = sattr->index;
1290 return sprintf(buf, "%d\n", data->rpm[nr]);
1294 show_fan_min(struct device *dev, struct device_attribute *attr, char *buf)
1296 struct nct6775_data *data = nct6775_update_device(dev);
1297 struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr);
1298 int nr = sattr->index;
1299 return sprintf(buf, "%d\n",
1300 data->fan_from_reg_min(data->fan_min[nr],
1301 data->fan_div[nr]));
1305 show_fan_div(struct device *dev, struct device_attribute *attr, char *buf)
1307 struct nct6775_data *data = nct6775_update_device(dev);
1308 struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr);
1309 int nr = sattr->index;
1310 return sprintf(buf, "%u\n", div_from_reg(data->fan_div[nr]));
1314 store_fan_min(struct device *dev, struct device_attribute *attr,
1315 const char *buf, size_t count)
1317 struct nct6775_data *data = dev_get_drvdata(dev);
1318 struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr);
1319 int nr = sattr->index;
1325 err = kstrtoul(buf, 10, &val);
1329 mutex_lock(&data->update_lock);
1330 if (!data->has_fan_div) {
1332 * Only NCT6776F for now, so we know that this is a 13 bit
1340 val = 1350000U / val;
1341 val = (val & 0x1f) | ((val << 3) & 0xff00);
1343 data->fan_min[nr] = val;
1344 goto write_min; /* Leave fan divider alone */
1347 /* No min limit, alarm disabled */
1348 data->fan_min[nr] = 255;
1349 new_div = data->fan_div[nr]; /* No change */
1350 dev_info(dev, "fan%u low limit and alarm disabled\n", nr + 1);
1353 reg = 1350000U / val;
1354 if (reg >= 128 * 255) {
1356 * Speed below this value cannot possibly be represented,
1357 * even with the highest divider (128)
1359 data->fan_min[nr] = 254;
1360 new_div = 7; /* 128 == (1 << 7) */
1362 "fan%u low limit %lu below minimum %u, set to minimum\n",
1363 nr + 1, val, data->fan_from_reg_min(254, 7));
1366 * Speed above this value cannot possibly be represented,
1367 * even with the lowest divider (1)
1369 data->fan_min[nr] = 1;
1370 new_div = 0; /* 1 == (1 << 0) */
1372 "fan%u low limit %lu above maximum %u, set to maximum\n",
1373 nr + 1, val, data->fan_from_reg_min(1, 0));
1376 * Automatically pick the best divider, i.e. the one such
1377 * that the min limit will correspond to a register value
1378 * in the 96..192 range
1381 while (reg > 192 && new_div < 7) {
1385 data->fan_min[nr] = reg;
1390 * Write both the fan clock divider (if it changed) and the new
1391 * fan min (unconditionally)
1393 if (new_div != data->fan_div[nr]) {
1394 dev_dbg(dev, "fan%u clock divider changed from %u to %u\n",
1395 nr + 1, div_from_reg(data->fan_div[nr]),
1396 div_from_reg(new_div));
1397 data->fan_div[nr] = new_div;
1398 nct6775_write_fan_div_common(dev, data, nr);
1399 /* Give the chip time to sample a new speed value */
1400 data->last_updated = jiffies;
1404 nct6775_write_value(data, data->REG_FAN_MIN[nr],
1406 mutex_unlock(&data->update_lock);
1411 static struct sensor_device_attribute sda_fan_input[] = {
1412 SENSOR_ATTR(fan1_input, S_IRUGO, show_fan, NULL, 0),
1413 SENSOR_ATTR(fan2_input, S_IRUGO, show_fan, NULL, 1),
1414 SENSOR_ATTR(fan3_input, S_IRUGO, show_fan, NULL, 2),
1415 SENSOR_ATTR(fan4_input, S_IRUGO, show_fan, NULL, 3),
1416 SENSOR_ATTR(fan5_input, S_IRUGO, show_fan, NULL, 4),
1419 static struct sensor_device_attribute sda_fan_alarm[] = {
1420 SENSOR_ATTR(fan1_alarm, S_IRUGO, show_alarm, NULL, FAN_ALARM_BASE),
1421 SENSOR_ATTR(fan2_alarm, S_IRUGO, show_alarm, NULL, FAN_ALARM_BASE + 1),
1422 SENSOR_ATTR(fan3_alarm, S_IRUGO, show_alarm, NULL, FAN_ALARM_BASE + 2),
1423 SENSOR_ATTR(fan4_alarm, S_IRUGO, show_alarm, NULL, FAN_ALARM_BASE + 3),
1424 SENSOR_ATTR(fan5_alarm, S_IRUGO, show_alarm, NULL, FAN_ALARM_BASE + 4),
1427 static struct sensor_device_attribute sda_fan_min[] = {
1428 SENSOR_ATTR(fan1_min, S_IWUSR | S_IRUGO, show_fan_min,
1430 SENSOR_ATTR(fan2_min, S_IWUSR | S_IRUGO, show_fan_min,
1432 SENSOR_ATTR(fan3_min, S_IWUSR | S_IRUGO, show_fan_min,
1434 SENSOR_ATTR(fan4_min, S_IWUSR | S_IRUGO, show_fan_min,
1436 SENSOR_ATTR(fan5_min, S_IWUSR | S_IRUGO, show_fan_min,
1440 static struct sensor_device_attribute sda_fan_div[] = {
1441 SENSOR_ATTR(fan1_div, S_IRUGO, show_fan_div, NULL, 0),
1442 SENSOR_ATTR(fan2_div, S_IRUGO, show_fan_div, NULL, 1),
1443 SENSOR_ATTR(fan3_div, S_IRUGO, show_fan_div, NULL, 2),
1444 SENSOR_ATTR(fan4_div, S_IRUGO, show_fan_div, NULL, 3),
1445 SENSOR_ATTR(fan5_div, S_IRUGO, show_fan_div, NULL, 4),
1449 show_temp_label(struct device *dev, struct device_attribute *attr, char *buf)
1451 struct nct6775_data *data = nct6775_update_device(dev);
1452 struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr);
1453 int nr = sattr->index;
1454 return sprintf(buf, "%s\n", data->temp_label[data->temp_src[nr]]);
1458 show_temp(struct device *dev, struct device_attribute *attr, char *buf)
1460 struct nct6775_data *data = nct6775_update_device(dev);
1461 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
1463 int index = sattr->index;
1465 return sprintf(buf, "%d\n", LM75_TEMP_FROM_REG(data->temp[index][nr]));
1469 store_temp(struct device *dev, struct device_attribute *attr, const char *buf,
1472 struct nct6775_data *data = dev_get_drvdata(dev);
1473 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
1475 int index = sattr->index;
1479 err = kstrtol(buf, 10, &val);
1483 mutex_lock(&data->update_lock);
1484 data->temp[index][nr] = LM75_TEMP_TO_REG(val);
1485 nct6775_write_temp(data, data->reg_temp[index][nr],
1486 data->temp[index][nr]);
1487 mutex_unlock(&data->update_lock);
1492 show_temp_offset(struct device *dev, struct device_attribute *attr, char *buf)
1494 struct nct6775_data *data = nct6775_update_device(dev);
1495 struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr);
1497 return sprintf(buf, "%d\n", data->temp_offset[sattr->index] * 1000);
1501 store_temp_offset(struct device *dev, struct device_attribute *attr,
1502 const char *buf, size_t count)
1504 struct nct6775_data *data = dev_get_drvdata(dev);
1505 struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr);
1506 int nr = sattr->index;
1510 err = kstrtol(buf, 10, &val);
1514 val = SENSORS_LIMIT(DIV_ROUND_CLOSEST(val, 1000), -128, 127);
1516 mutex_lock(&data->update_lock);
1517 data->temp_offset[nr] = val;
1518 nct6775_write_value(data, data->REG_TEMP_OFFSET[nr], val);
1519 mutex_unlock(&data->update_lock);
1525 show_temp_type(struct device *dev, struct device_attribute *attr, char *buf)
1527 struct nct6775_data *data = nct6775_update_device(dev);
1528 struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr);
1529 int nr = sattr->index;
1530 return sprintf(buf, "%d\n", (int)data->temp_type[nr]);
1534 store_temp_type(struct device *dev, struct device_attribute *attr,
1535 const char *buf, size_t count)
1537 struct nct6775_data *data = nct6775_update_device(dev);
1538 struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr);
1539 int nr = sattr->index;
1542 u8 vbat, diode, bit;
1544 err = kstrtoul(buf, 10, &val);
1548 if (val != 1 && val != 3 && val != 4)
1551 mutex_lock(&data->update_lock);
1553 data->temp_type[nr] = val;
1554 vbat = nct6775_read_value(data, data->REG_VBAT) & ~(0x02 << nr);
1555 diode = nct6775_read_value(data, data->REG_DIODE) & ~(0x02 << nr);
1558 case 1: /* CPU diode (diode, current mode) */
1562 case 3: /* diode, voltage mode */
1565 case 4: /* thermistor */
1568 nct6775_write_value(data, data->REG_VBAT, vbat);
1569 nct6775_write_value(data, data->REG_DIODE, diode);
1571 mutex_unlock(&data->update_lock);
1575 static struct sensor_device_attribute_2 sda_temp_input[] = {
1576 SENSOR_ATTR_2(temp1_input, S_IRUGO, show_temp, NULL, 0, 0),
1577 SENSOR_ATTR_2(temp2_input, S_IRUGO, show_temp, NULL, 1, 0),
1578 SENSOR_ATTR_2(temp3_input, S_IRUGO, show_temp, NULL, 2, 0),
1579 SENSOR_ATTR_2(temp4_input, S_IRUGO, show_temp, NULL, 3, 0),
1580 SENSOR_ATTR_2(temp5_input, S_IRUGO, show_temp, NULL, 4, 0),
1581 SENSOR_ATTR_2(temp6_input, S_IRUGO, show_temp, NULL, 5, 0),
1582 SENSOR_ATTR_2(temp7_input, S_IRUGO, show_temp, NULL, 6, 0),
1583 SENSOR_ATTR_2(temp8_input, S_IRUGO, show_temp, NULL, 7, 0),
1584 SENSOR_ATTR_2(temp9_input, S_IRUGO, show_temp, NULL, 8, 0),
1585 SENSOR_ATTR_2(temp10_input, S_IRUGO, show_temp, NULL, 9, 0),
1588 static struct sensor_device_attribute sda_temp_label[] = {
1589 SENSOR_ATTR(temp1_label, S_IRUGO, show_temp_label, NULL, 0),
1590 SENSOR_ATTR(temp2_label, S_IRUGO, show_temp_label, NULL, 1),
1591 SENSOR_ATTR(temp3_label, S_IRUGO, show_temp_label, NULL, 2),
1592 SENSOR_ATTR(temp4_label, S_IRUGO, show_temp_label, NULL, 3),
1593 SENSOR_ATTR(temp5_label, S_IRUGO, show_temp_label, NULL, 4),
1594 SENSOR_ATTR(temp6_label, S_IRUGO, show_temp_label, NULL, 5),
1595 SENSOR_ATTR(temp7_label, S_IRUGO, show_temp_label, NULL, 6),
1596 SENSOR_ATTR(temp8_label, S_IRUGO, show_temp_label, NULL, 7),
1597 SENSOR_ATTR(temp9_label, S_IRUGO, show_temp_label, NULL, 8),
1598 SENSOR_ATTR(temp10_label, S_IRUGO, show_temp_label, NULL, 9),
1601 static struct sensor_device_attribute_2 sda_temp_max[] = {
1602 SENSOR_ATTR_2(temp1_max, S_IRUGO | S_IWUSR, show_temp, store_temp,
1604 SENSOR_ATTR_2(temp2_max, S_IRUGO | S_IWUSR, show_temp, store_temp,
1606 SENSOR_ATTR_2(temp3_max, S_IRUGO | S_IWUSR, show_temp, store_temp,
1608 SENSOR_ATTR_2(temp4_max, S_IRUGO | S_IWUSR, show_temp, store_temp,
1610 SENSOR_ATTR_2(temp5_max, S_IRUGO | S_IWUSR, show_temp, store_temp,
1612 SENSOR_ATTR_2(temp6_max, S_IRUGO | S_IWUSR, show_temp, store_temp,
1614 SENSOR_ATTR_2(temp7_max, S_IRUGO | S_IWUSR, show_temp, store_temp,
1616 SENSOR_ATTR_2(temp8_max, S_IRUGO | S_IWUSR, show_temp, store_temp,
1618 SENSOR_ATTR_2(temp9_max, S_IRUGO | S_IWUSR, show_temp, store_temp,
1620 SENSOR_ATTR_2(temp10_max, S_IRUGO | S_IWUSR, show_temp, store_temp,
1624 static struct sensor_device_attribute_2 sda_temp_max_hyst[] = {
1625 SENSOR_ATTR_2(temp1_max_hyst, S_IRUGO | S_IWUSR, show_temp, store_temp,
1627 SENSOR_ATTR_2(temp2_max_hyst, S_IRUGO | S_IWUSR, show_temp, store_temp,
1629 SENSOR_ATTR_2(temp3_max_hyst, S_IRUGO | S_IWUSR, show_temp, store_temp,
1631 SENSOR_ATTR_2(temp4_max_hyst, S_IRUGO | S_IWUSR, show_temp, store_temp,
1633 SENSOR_ATTR_2(temp5_max_hyst, S_IRUGO | S_IWUSR, show_temp, store_temp,
1635 SENSOR_ATTR_2(temp6_max_hyst, S_IRUGO | S_IWUSR, show_temp, store_temp,
1637 SENSOR_ATTR_2(temp7_max_hyst, S_IRUGO | S_IWUSR, show_temp, store_temp,
1639 SENSOR_ATTR_2(temp8_max_hyst, S_IRUGO | S_IWUSR, show_temp, store_temp,
1641 SENSOR_ATTR_2(temp9_max_hyst, S_IRUGO | S_IWUSR, show_temp, store_temp,
1643 SENSOR_ATTR_2(temp10_max_hyst, S_IRUGO | S_IWUSR, show_temp, store_temp,
1647 static struct sensor_device_attribute sda_temp_offset[] = {
1648 SENSOR_ATTR(temp1_offset, S_IRUGO | S_IWUSR, show_temp_offset,
1649 store_temp_offset, 0),
1650 SENSOR_ATTR(temp2_offset, S_IRUGO | S_IWUSR, show_temp_offset,
1651 store_temp_offset, 1),
1652 SENSOR_ATTR(temp3_offset, S_IRUGO | S_IWUSR, show_temp_offset,
1653 store_temp_offset, 2),
1654 SENSOR_ATTR(temp4_offset, S_IRUGO | S_IWUSR, show_temp_offset,
1655 store_temp_offset, 3),
1656 SENSOR_ATTR(temp5_offset, S_IRUGO | S_IWUSR, show_temp_offset,
1657 store_temp_offset, 4),
1658 SENSOR_ATTR(temp6_offset, S_IRUGO | S_IWUSR, show_temp_offset,
1659 store_temp_offset, 5),
1662 static struct sensor_device_attribute sda_temp_type[] = {
1663 SENSOR_ATTR(temp1_type, S_IRUGO | S_IWUSR, show_temp_type,
1664 store_temp_type, 0),
1665 SENSOR_ATTR(temp2_type, S_IRUGO | S_IWUSR, show_temp_type,
1666 store_temp_type, 1),
1667 SENSOR_ATTR(temp3_type, S_IRUGO | S_IWUSR, show_temp_type,
1668 store_temp_type, 2),
1669 SENSOR_ATTR(temp4_type, S_IRUGO | S_IWUSR, show_temp_type,
1670 store_temp_type, 3),
1671 SENSOR_ATTR(temp5_type, S_IRUGO | S_IWUSR, show_temp_type,
1672 store_temp_type, 4),
1673 SENSOR_ATTR(temp6_type, S_IRUGO | S_IWUSR, show_temp_type,
1674 store_temp_type, 5),
1677 static struct sensor_device_attribute sda_temp_alarm[] = {
1678 SENSOR_ATTR(temp1_alarm, S_IRUGO, show_alarm, NULL,
1680 SENSOR_ATTR(temp2_alarm, S_IRUGO, show_alarm, NULL,
1681 TEMP_ALARM_BASE + 1),
1682 SENSOR_ATTR(temp3_alarm, S_IRUGO, show_alarm, NULL,
1683 TEMP_ALARM_BASE + 2),
1684 SENSOR_ATTR(temp4_alarm, S_IRUGO, show_alarm, NULL,
1685 TEMP_ALARM_BASE + 3),
1686 SENSOR_ATTR(temp5_alarm, S_IRUGO, show_alarm, NULL,
1687 TEMP_ALARM_BASE + 4),
1688 SENSOR_ATTR(temp6_alarm, S_IRUGO, show_alarm, NULL,
1689 TEMP_ALARM_BASE + 5),
1692 #define NUM_TEMP_ALARM ARRAY_SIZE(sda_temp_alarm)
1695 show_pwm_mode(struct device *dev, struct device_attribute *attr, char *buf)
1697 struct nct6775_data *data = nct6775_update_device(dev);
1698 struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr);
1700 return sprintf(buf, "%d\n", !data->pwm_mode[sattr->index]);
1704 store_pwm_mode(struct device *dev, struct device_attribute *attr,
1705 const char *buf, size_t count)
1707 struct nct6775_data *data = dev_get_drvdata(dev);
1708 struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr);
1709 int nr = sattr->index;
1714 err = kstrtoul(buf, 10, &val);
1721 /* Setting DC mode is not supported for all chips/channels */
1722 if (data->REG_PWM_MODE[nr] == 0) {
1728 mutex_lock(&data->update_lock);
1729 data->pwm_mode[nr] = val;
1730 reg = nct6775_read_value(data, data->REG_PWM_MODE[nr]);
1731 reg &= ~data->PWM_MODE_MASK[nr];
1733 reg |= data->PWM_MODE_MASK[nr];
1734 nct6775_write_value(data, data->REG_PWM_MODE[nr], reg);
1735 mutex_unlock(&data->update_lock);
1740 show_pwm_enable(struct device *dev, struct device_attribute *attr, char *buf)
1742 struct nct6775_data *data = nct6775_update_device(dev);
1743 struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr);
1745 return sprintf(buf, "%d\n", data->pwm_enable[sattr->index]);
1749 show_pwm(struct device *dev, struct device_attribute *attr, char *buf)
1751 struct nct6775_data *data = nct6775_update_device(dev);
1752 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
1754 int index = sattr->index;
1758 * For automatic fan control modes, show current pwm readings.
1759 * Otherwise, show the configured value.
1761 if (index == 0 && data->pwm_enable[nr] > 1)
1762 pwm = nct6775_read_value(data, data->REG_PWM_READ[nr]);
1764 pwm = data->pwm[index][nr];
1766 return sprintf(buf, "%d\n", pwm);
1770 store_pwm(struct device *dev, struct device_attribute *attr, const char *buf,
1773 struct nct6775_data *data = dev_get_drvdata(dev);
1774 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
1776 int index = sattr->index;
1778 int minval[7] = { 0, 1, 1, data->pwm[2][nr], 0, 0, 0 };
1780 = { 255, 255, data->pwm[3][nr] ? : 255, 255, 255, 255, 255 };
1783 err = kstrtoul(buf, 10, &val);
1786 val = SENSORS_LIMIT(val, minval[index], maxval[index]);
1788 mutex_lock(&data->update_lock);
1789 data->pwm[index][nr] = val;
1790 nct6775_write_value(data, data->REG_PWM[index][nr], val);
1791 mutex_unlock(&data->update_lock);
1795 /* Returns 0 if OK, -EINVAL otherwise */
1796 static int check_trip_points(struct nct6775_data *data, int nr)
1800 for (i = 0; i < data->auto_pwm_num - 1; i++) {
1801 if (data->auto_temp[nr][i] > data->auto_temp[nr][i + 1])
1804 for (i = 0; i < data->auto_pwm_num - 1; i++) {
1805 if (data->auto_pwm[nr][i] > data->auto_pwm[nr][i + 1])
1808 /* validate critical temperature and pwm if enabled (pwm > 0) */
1809 if (data->auto_pwm[nr][data->auto_pwm_num]) {
1810 if (data->auto_temp[nr][data->auto_pwm_num - 1] >
1811 data->auto_temp[nr][data->auto_pwm_num] ||
1812 data->auto_pwm[nr][data->auto_pwm_num - 1] >
1813 data->auto_pwm[nr][data->auto_pwm_num])
1820 store_pwm_enable(struct device *dev, struct device_attribute *attr,
1821 const char *buf, size_t count)
1823 struct nct6775_data *data = dev_get_drvdata(dev);
1824 struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr);
1825 int nr = sattr->index;
1830 err = kstrtoul(buf, 10, &val);
1837 if (val == 4 && data->kind != nct6775)
1840 if (val == 5 && check_trip_points(data, nr)) {
1841 dev_err(dev, "Inconsistent trip points, not switching to SmartFan IV mode\n");
1842 dev_err(dev, "Adjust trip points and try again\n");
1846 mutex_lock(&data->update_lock);
1847 data->pwm_enable[nr] = val;
1850 * turn off pwm control: select manual mode, set pwm to maximum
1852 data->pwm[0][nr] = 255;
1853 nct6775_write_value(data, data->REG_PWM[0][nr], 255);
1855 reg = nct6775_read_value(data, data->REG_FAN_MODE[nr]);
1857 reg |= pwm_enable_to_reg(val) << 4;
1858 nct6775_write_value(data, data->REG_FAN_MODE[nr], reg);
1859 mutex_unlock(&data->update_lock);
1864 show_pwm_temp(struct device *dev, struct device_attribute *attr, char *buf)
1866 struct nct6775_data *data = nct6775_update_device(dev);
1867 struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr);
1868 int nr = sattr->index;
1870 return sprintf(buf, "%d\n", LM75_TEMP_FROM_REG(data->pwm_temp[nr]));
1874 show_pwm_temp_sel(struct device *dev, struct device_attribute *attr, char *buf)
1876 struct nct6775_data *data = nct6775_update_device(dev);
1877 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
1879 return sprintf(buf, "%d\n",
1880 data->pwm_temp_sel[sattr->index][sattr->nr]);
1884 store_pwm_temp_sel(struct device *dev, struct device_attribute *attr,
1885 const char *buf, size_t count)
1887 struct nct6775_data *data = nct6775_update_device(dev);
1888 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
1890 int index = sattr->index;
1895 err = kstrtoul(buf, 10, &val);
1898 if (val == 0 || val > 0x1f)
1901 val = SENSORS_LIMIT(val, 1, data->temp_label_num - 1);
1903 if (!strlen(data->temp_label[val]))
1906 mutex_lock(&data->update_lock);
1907 data->pwm_temp_sel[index][nr] = val;
1908 reg = nct6775_read_value(data, data->REG_TEMP_SEL[index][nr]);
1911 nct6775_write_value(data, data->REG_TEMP_SEL[index][nr], reg);
1912 mutex_unlock(&data->update_lock);
1918 show_target_temp(struct device *dev, struct device_attribute *attr, char *buf)
1920 struct nct6775_data *data = nct6775_update_device(dev);
1921 struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr);
1923 return sprintf(buf, "%d\n",
1924 data->target_temp[sattr->index] * 1000);
1928 store_target_temp(struct device *dev, struct device_attribute *attr,
1929 const char *buf, size_t count)
1931 struct nct6775_data *data = dev_get_drvdata(dev);
1932 struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr);
1933 int nr = sattr->index;
1937 err = kstrtol(buf, 10, &val);
1941 val = SENSORS_LIMIT(DIV_ROUND_CLOSEST(val, 1000), 0, 127);
1943 mutex_lock(&data->update_lock);
1944 data->target_temp[nr] = val;
1945 nct6775_write_value(data, data->REG_TARGET[nr], val);
1946 mutex_unlock(&data->update_lock);
1951 show_auto_temp_hyst(struct device *dev, struct device_attribute *attr,
1954 struct nct6775_data *data = nct6775_update_device(dev);
1955 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
1957 int point = sattr->index >= data->auto_pwm_num ? 1 : 0;
1958 int tolerance = data->tolerance[nr][point];
1959 int temp = data->auto_temp[nr][sattr->index];
1961 return sprintf(buf, "%d\n", (temp - tolerance) * 1000);
1965 store_auto_temp_hyst(struct device *dev, struct device_attribute *attr,
1966 const char *buf, size_t count)
1968 struct nct6775_data *data = dev_get_drvdata(dev);
1969 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
1971 int point = sattr->index >= data->auto_pwm_num ? 1 : 0;
1975 int maxlimit[2][3] = { { 15, 7, 63 }, { 15, 7, 7 } };
1976 int mask[] = { 0x0f, 0x07, 0x07 };
1979 err = kstrtol(buf, 10, &val);
1983 temp = data->auto_temp[nr][sattr->index];
1984 val = temp - DIV_ROUND_CLOSEST(val, 1000);
1986 /* Limit tolerance as needed */
1987 val = SENSORS_LIMIT(val, 0, maxlimit[point][data->kind]);
1989 mutex_lock(&data->update_lock);
1991 nct6775_write_value(data,
1992 data->REG_CRITICAL_TEMP_TOLERANCE[nr],
1995 reg = nct6775_read_value(data, data->REG_FAN_MODE[nr]);
1996 reg = (reg & ~mask[nr]) | (val & mask[nr]);
1997 nct6775_write_value(data, data->REG_FAN_MODE[nr], reg);
1998 if (data->kind == nct6779) {
1999 reg = nct6775_read_value(data,
2000 NCT6779_REG_TOLERANCE_H[nr]);
2001 reg = (reg & 0x70) | ((val & 0x38) << 1);
2002 nct6775_write_value(data,
2003 NCT6779_REG_TOLERANCE_H[nr], reg);
2007 data->tolerance[nr][point] = val;
2008 mutex_unlock(&data->update_lock);
2012 static SENSOR_DEVICE_ATTR_2(pwm1, S_IWUSR | S_IRUGO, show_pwm, store_pwm, 0, 0);
2013 static SENSOR_DEVICE_ATTR_2(pwm2, S_IWUSR | S_IRUGO, show_pwm, store_pwm, 1, 0);
2014 static SENSOR_DEVICE_ATTR_2(pwm3, S_IWUSR | S_IRUGO, show_pwm, store_pwm, 2, 0);
2015 static SENSOR_DEVICE_ATTR_2(pwm4, S_IWUSR | S_IRUGO, show_pwm, store_pwm, 3, 0);
2016 static SENSOR_DEVICE_ATTR_2(pwm5, S_IWUSR | S_IRUGO, show_pwm, store_pwm, 4, 0);
2018 static SENSOR_DEVICE_ATTR(pwm1_mode, S_IWUSR | S_IRUGO, show_pwm_mode,
2020 static SENSOR_DEVICE_ATTR(pwm2_mode, S_IWUSR | S_IRUGO, show_pwm_mode,
2022 static SENSOR_DEVICE_ATTR(pwm3_mode, S_IWUSR | S_IRUGO, show_pwm_mode,
2024 static SENSOR_DEVICE_ATTR(pwm4_mode, S_IWUSR | S_IRUGO, show_pwm_mode,
2026 static SENSOR_DEVICE_ATTR(pwm5_mode, S_IWUSR | S_IRUGO, show_pwm_mode,
2029 static SENSOR_DEVICE_ATTR(pwm1_enable, S_IWUSR | S_IRUGO, show_pwm_enable,
2030 store_pwm_enable, 0);
2031 static SENSOR_DEVICE_ATTR(pwm2_enable, S_IWUSR | S_IRUGO, show_pwm_enable,
2032 store_pwm_enable, 1);
2033 static SENSOR_DEVICE_ATTR(pwm3_enable, S_IWUSR | S_IRUGO, show_pwm_enable,
2034 store_pwm_enable, 2);
2035 static SENSOR_DEVICE_ATTR(pwm4_enable, S_IWUSR | S_IRUGO, show_pwm_enable,
2036 store_pwm_enable, 3);
2037 static SENSOR_DEVICE_ATTR(pwm5_enable, S_IWUSR | S_IRUGO, show_pwm_enable,
2038 store_pwm_enable, 4);
2040 static SENSOR_DEVICE_ATTR_2(pwm1_temp_sel, S_IWUSR | S_IRUGO,
2041 show_pwm_temp_sel, store_pwm_temp_sel, 0, 0);
2042 static SENSOR_DEVICE_ATTR_2(pwm2_temp_sel, S_IWUSR | S_IRUGO,
2043 show_pwm_temp_sel, store_pwm_temp_sel, 1, 0);
2044 static SENSOR_DEVICE_ATTR_2(pwm3_temp_sel, S_IWUSR | S_IRUGO,
2045 show_pwm_temp_sel, store_pwm_temp_sel, 2, 0);
2046 static SENSOR_DEVICE_ATTR_2(pwm4_temp_sel, S_IWUSR | S_IRUGO,
2047 show_pwm_temp_sel, store_pwm_temp_sel, 3, 0);
2048 static SENSOR_DEVICE_ATTR_2(pwm5_temp_sel, S_IWUSR | S_IRUGO,
2049 show_pwm_temp_sel, store_pwm_temp_sel, 4, 0);
2051 static SENSOR_DEVICE_ATTR(pwm1_target, S_IWUSR | S_IRUGO, show_target_temp,
2052 store_target_temp, 0);
2053 static SENSOR_DEVICE_ATTR(pwm2_target, S_IWUSR | S_IRUGO, show_target_temp,
2054 store_target_temp, 1);
2055 static SENSOR_DEVICE_ATTR(pwm3_target, S_IWUSR | S_IRUGO, show_target_temp,
2056 store_target_temp, 2);
2057 static SENSOR_DEVICE_ATTR(pwm4_target, S_IWUSR | S_IRUGO, show_target_temp,
2058 store_target_temp, 3);
2059 static SENSOR_DEVICE_ATTR(pwm5_target, S_IWUSR | S_IRUGO, show_target_temp,
2060 store_target_temp, 4);
2062 /* Monitored pwm temperatures */
2063 static SENSOR_DEVICE_ATTR(temp11_input, S_IRUGO, show_pwm_temp, NULL, 0);
2064 static SENSOR_DEVICE_ATTR(temp12_input, S_IRUGO, show_pwm_temp, NULL, 1);
2065 static SENSOR_DEVICE_ATTR(temp13_input, S_IRUGO, show_pwm_temp, NULL, 2);
2066 static SENSOR_DEVICE_ATTR(temp14_input, S_IRUGO, show_pwm_temp, NULL, 3);
2067 static SENSOR_DEVICE_ATTR(temp15_input, S_IRUGO, show_pwm_temp, NULL, 4);
2069 /* Smart Fan registers */
2072 show_pwm_sel_enable(struct device *dev, struct device_attribute *attr,
2075 struct nct6775_data *data = nct6775_update_device(dev);
2076 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
2078 return sprintf(buf, "%d\n",
2079 data->pwm_sel_enable[sattr->index][sattr->nr]);
2083 store_pwm_sel_enable(struct device *dev, struct device_attribute *attr,
2084 const char *buf, size_t count)
2086 struct nct6775_data *data = dev_get_drvdata(dev);
2087 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
2089 int index = sattr->index;
2094 err = kstrtoul(buf, 10, &val);
2101 mutex_lock(&data->update_lock);
2102 data->pwm_sel_enable[index][nr] = val;
2103 reg = nct6775_read_value(data, data->REG_TEMP_SEL[index][nr]);
2107 nct6775_write_value(data, data->REG_TEMP_SEL[index][nr], reg);
2108 mutex_unlock(&data->update_lock);
2113 show_weight_temp(struct device *dev, struct device_attribute *attr, char *buf)
2115 struct nct6775_data *data = nct6775_update_device(dev);
2116 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
2118 int index = sattr->index;
2120 return sprintf(buf, "%d\n", data->weight_temp[index][nr] * 1000);
2124 store_weight_temp(struct device *dev, struct device_attribute *attr,
2125 const char *buf, size_t count)
2127 struct nct6775_data *data = dev_get_drvdata(dev);
2128 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
2130 int index = sattr->index;
2134 err = kstrtoul(buf, 10, &val);
2138 val = SENSORS_LIMIT(DIV_ROUND_CLOSEST(val, 1000), 0, 255);
2140 mutex_lock(&data->update_lock);
2141 data->weight_temp[index][nr] = val;
2142 nct6775_write_value(data, data->REG_WEIGHT_TEMP[index][nr], val);
2143 mutex_unlock(&data->update_lock);
2147 static SENSOR_DEVICE_ATTR_2(pwm1_stop_output_enable, S_IWUSR | S_IRUGO,
2148 show_pwm_sel_enable, store_pwm_sel_enable, 0, 0);
2149 static SENSOR_DEVICE_ATTR_2(pwm2_stop_output_enable, S_IWUSR | S_IRUGO,
2150 show_pwm_sel_enable, store_pwm_sel_enable, 1, 0);
2151 static SENSOR_DEVICE_ATTR_2(pwm3_stop_output_enable, S_IWUSR | S_IRUGO,
2152 show_pwm_sel_enable, store_pwm_sel_enable, 2, 0);
2153 static SENSOR_DEVICE_ATTR_2(pwm4_stop_output_enable, S_IWUSR | S_IRUGO,
2154 show_pwm_sel_enable, store_pwm_sel_enable, 3, 0);
2155 static SENSOR_DEVICE_ATTR_2(pwm5_stop_output_enable, S_IWUSR | S_IRUGO,
2156 show_pwm_sel_enable, store_pwm_sel_enable, 4, 0);
2158 static SENSOR_DEVICE_ATTR_2(pwm1_weight_enable, S_IWUSR | S_IRUGO,
2159 show_pwm_sel_enable, store_pwm_sel_enable, 0, 1);
2160 static SENSOR_DEVICE_ATTR_2(pwm2_weight_enable, S_IWUSR | S_IRUGO,
2161 show_pwm_sel_enable, store_pwm_sel_enable, 1, 1);
2162 static SENSOR_DEVICE_ATTR_2(pwm3_weight_enable, S_IWUSR | S_IRUGO,
2163 show_pwm_sel_enable, store_pwm_sel_enable, 2, 1);
2164 static SENSOR_DEVICE_ATTR_2(pwm4_weight_enable, S_IWUSR | S_IRUGO,
2165 show_pwm_sel_enable, store_pwm_sel_enable, 3, 1);
2166 static SENSOR_DEVICE_ATTR_2(pwm5_weight_enable, S_IWUSR | S_IRUGO,
2167 show_pwm_sel_enable, store_pwm_sel_enable, 4, 1);
2169 static SENSOR_DEVICE_ATTR_2(pwm1_weight_temp_sel, S_IWUSR | S_IRUGO,
2170 show_pwm_temp_sel, store_pwm_temp_sel, 0, 1);
2171 static SENSOR_DEVICE_ATTR_2(pwm2_weight_temp_sel, S_IWUSR | S_IRUGO,
2172 show_pwm_temp_sel, store_pwm_temp_sel, 1, 1);
2173 static SENSOR_DEVICE_ATTR_2(pwm3_weight_temp_sel, S_IWUSR | S_IRUGO,
2174 show_pwm_temp_sel, store_pwm_temp_sel, 2, 1);
2175 static SENSOR_DEVICE_ATTR_2(pwm4_weight_temp_sel, S_IWUSR | S_IRUGO,
2176 show_pwm_temp_sel, store_pwm_temp_sel, 3, 1);
2177 static SENSOR_DEVICE_ATTR_2(pwm5_weight_temp_sel, S_IWUSR | S_IRUGO,
2178 show_pwm_temp_sel, store_pwm_temp_sel, 4, 1);
2180 static SENSOR_DEVICE_ATTR_2(pwm1_weight_temp_step, S_IWUSR | S_IRUGO,
2181 show_weight_temp, store_weight_temp, 0, 0);
2182 static SENSOR_DEVICE_ATTR_2(pwm2_weight_temp_step, S_IWUSR | S_IRUGO,
2183 show_weight_temp, store_weight_temp, 1, 0);
2184 static SENSOR_DEVICE_ATTR_2(pwm3_weight_temp_step, S_IWUSR | S_IRUGO,
2185 show_weight_temp, store_weight_temp, 2, 0);
2186 static SENSOR_DEVICE_ATTR_2(pwm4_weight_temp_step, S_IWUSR | S_IRUGO,
2187 show_weight_temp, store_weight_temp, 3, 0);
2188 static SENSOR_DEVICE_ATTR_2(pwm5_weight_temp_step, S_IWUSR | S_IRUGO,
2189 show_weight_temp, store_weight_temp, 4, 0);
2191 static SENSOR_DEVICE_ATTR_2(pwm1_weight_temp_step_tol, S_IWUSR | S_IRUGO,
2192 show_weight_temp, store_weight_temp, 0, 1);
2193 static SENSOR_DEVICE_ATTR_2(pwm2_weight_temp_step_tol, S_IWUSR | S_IRUGO,
2194 show_weight_temp, store_weight_temp, 1, 1);
2195 static SENSOR_DEVICE_ATTR_2(pwm3_weight_temp_step_tol, S_IWUSR | S_IRUGO,
2196 show_weight_temp, store_weight_temp, 2, 1);
2197 static SENSOR_DEVICE_ATTR_2(pwm4_weight_temp_step_tol, S_IWUSR | S_IRUGO,
2198 show_weight_temp, store_weight_temp, 3, 1);
2199 static SENSOR_DEVICE_ATTR_2(pwm5_weight_temp_step_tol, S_IWUSR | S_IRUGO,
2200 show_weight_temp, store_weight_temp, 4, 1);
2202 static SENSOR_DEVICE_ATTR_2(pwm1_weight_temp_step_base, S_IWUSR | S_IRUGO,
2203 show_weight_temp, store_weight_temp, 0, 2);
2204 static SENSOR_DEVICE_ATTR_2(pwm2_weight_temp_step_base, S_IWUSR | S_IRUGO,
2205 show_weight_temp, store_weight_temp, 1, 2);
2206 static SENSOR_DEVICE_ATTR_2(pwm3_weight_temp_step_base, S_IWUSR | S_IRUGO,
2207 show_weight_temp, store_weight_temp, 2, 2);
2208 static SENSOR_DEVICE_ATTR_2(pwm4_weight_temp_step_base, S_IWUSR | S_IRUGO,
2209 show_weight_temp, store_weight_temp, 3, 2);
2210 static SENSOR_DEVICE_ATTR_2(pwm5_weight_temp_step_base, S_IWUSR | S_IRUGO,
2211 show_weight_temp, store_weight_temp, 4, 2);
2213 static SENSOR_DEVICE_ATTR_2(pwm1_weight_duty_step, S_IWUSR | S_IRUGO,
2214 show_pwm, store_pwm, 0, 5);
2215 static SENSOR_DEVICE_ATTR_2(pwm2_weight_duty_step, S_IWUSR | S_IRUGO,
2216 show_pwm, store_pwm, 1, 5);
2217 static SENSOR_DEVICE_ATTR_2(pwm3_weight_duty_step, S_IWUSR | S_IRUGO,
2218 show_pwm, store_pwm, 2, 5);
2219 static SENSOR_DEVICE_ATTR_2(pwm4_weight_duty_step, S_IWUSR | S_IRUGO,
2220 show_pwm, store_pwm, 3, 5);
2221 static SENSOR_DEVICE_ATTR_2(pwm5_weight_duty_step, S_IWUSR | S_IRUGO,
2222 show_pwm, store_pwm, 4, 5);
2224 /* duty_base is not supported on all chips */
2225 static struct sensor_device_attribute_2 sda_weight_duty_base[] = {
2226 SENSOR_ATTR_2(pwm1_weight_duty_base, S_IWUSR | S_IRUGO,
2227 show_pwm, store_pwm, 0, 6),
2228 SENSOR_ATTR_2(pwm2_weight_duty_base, S_IWUSR | S_IRUGO,
2229 show_pwm, store_pwm, 1, 6),
2230 SENSOR_ATTR_2(pwm3_weight_duty_base, S_IWUSR | S_IRUGO,
2231 show_pwm, store_pwm, 2, 6),
2232 SENSOR_ATTR_2(pwm4_weight_duty_base, S_IWUSR | S_IRUGO,
2233 show_pwm, store_pwm, 3, 6),
2234 SENSOR_ATTR_2(pwm5_weight_duty_base, S_IWUSR | S_IRUGO,
2235 show_pwm, store_pwm, 4, 6),
2239 show_fan_time(struct device *dev, struct device_attribute *attr, char *buf)
2241 struct nct6775_data *data = nct6775_update_device(dev);
2242 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
2244 int index = sattr->index;
2246 return sprintf(buf, "%d\n",
2247 step_time_from_reg(data->fan_time[index][nr],
2248 data->pwm_mode[nr]));
2252 store_fan_time(struct device *dev, struct device_attribute *attr,
2253 const char *buf, size_t count)
2255 struct nct6775_data *data = dev_get_drvdata(dev);
2256 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
2258 int index = sattr->index;
2262 err = kstrtoul(buf, 10, &val);
2266 val = step_time_to_reg(val, data->pwm_mode[nr]);
2267 mutex_lock(&data->update_lock);
2268 data->fan_time[index][nr] = val;
2269 nct6775_write_value(data, data->REG_FAN_TIME[index][nr], val);
2270 mutex_unlock(&data->update_lock);
2275 show_name(struct device *dev, struct device_attribute *attr, char *buf)
2277 struct nct6775_data *data = dev_get_drvdata(dev);
2279 return sprintf(buf, "%s\n", data->name);
2282 static DEVICE_ATTR(name, S_IRUGO, show_name, NULL);
2284 static SENSOR_DEVICE_ATTR_2(pwm1_stop_time, S_IWUSR | S_IRUGO, show_fan_time,
2285 store_fan_time, 0, 0);
2286 static SENSOR_DEVICE_ATTR_2(pwm2_stop_time, S_IWUSR | S_IRUGO, show_fan_time,
2287 store_fan_time, 1, 0);
2288 static SENSOR_DEVICE_ATTR_2(pwm3_stop_time, S_IWUSR | S_IRUGO, show_fan_time,
2289 store_fan_time, 2, 0);
2290 static SENSOR_DEVICE_ATTR_2(pwm4_stop_time, S_IWUSR | S_IRUGO, show_fan_time,
2291 store_fan_time, 3, 0);
2292 static SENSOR_DEVICE_ATTR_2(pwm5_stop_time, S_IWUSR | S_IRUGO, show_fan_time,
2293 store_fan_time, 4, 0);
2295 static SENSOR_DEVICE_ATTR_2(pwm1_step_up_time, S_IWUSR | S_IRUGO, show_fan_time,
2296 store_fan_time, 0, 1);
2297 static SENSOR_DEVICE_ATTR_2(pwm2_step_up_time, S_IWUSR | S_IRUGO, show_fan_time,
2298 store_fan_time, 1, 1);
2299 static SENSOR_DEVICE_ATTR_2(pwm3_step_up_time, S_IWUSR | S_IRUGO, show_fan_time,
2300 store_fan_time, 2, 1);
2301 static SENSOR_DEVICE_ATTR_2(pwm4_step_up_time, S_IWUSR | S_IRUGO, show_fan_time,
2302 store_fan_time, 3, 1);
2303 static SENSOR_DEVICE_ATTR_2(pwm5_step_up_time, S_IWUSR | S_IRUGO, show_fan_time,
2304 store_fan_time, 4, 1);
2306 static SENSOR_DEVICE_ATTR_2(pwm1_step_down_time, S_IWUSR | S_IRUGO,
2307 show_fan_time, store_fan_time, 0, 2);
2308 static SENSOR_DEVICE_ATTR_2(pwm2_step_down_time, S_IWUSR | S_IRUGO,
2309 show_fan_time, store_fan_time, 1, 2);
2310 static SENSOR_DEVICE_ATTR_2(pwm3_step_down_time, S_IWUSR | S_IRUGO,
2311 show_fan_time, store_fan_time, 2, 2);
2312 static SENSOR_DEVICE_ATTR_2(pwm4_step_down_time, S_IWUSR | S_IRUGO,
2313 show_fan_time, store_fan_time, 3, 2);
2314 static SENSOR_DEVICE_ATTR_2(pwm5_step_down_time, S_IWUSR | S_IRUGO,
2315 show_fan_time, store_fan_time, 4, 2);
2317 static SENSOR_DEVICE_ATTR_2(pwm1_start_output, S_IWUSR | S_IRUGO, show_pwm,
2319 static SENSOR_DEVICE_ATTR_2(pwm2_start_output, S_IWUSR | S_IRUGO, show_pwm,
2321 static SENSOR_DEVICE_ATTR_2(pwm3_start_output, S_IWUSR | S_IRUGO, show_pwm,
2323 static SENSOR_DEVICE_ATTR_2(pwm4_start_output, S_IWUSR | S_IRUGO, show_pwm,
2325 static SENSOR_DEVICE_ATTR_2(pwm5_start_output, S_IWUSR | S_IRUGO, show_pwm,
2328 static SENSOR_DEVICE_ATTR_2(pwm1_stop_output, S_IWUSR | S_IRUGO, show_pwm,
2330 static SENSOR_DEVICE_ATTR_2(pwm2_stop_output, S_IWUSR | S_IRUGO, show_pwm,
2332 static SENSOR_DEVICE_ATTR_2(pwm3_stop_output, S_IWUSR | S_IRUGO, show_pwm,
2334 static SENSOR_DEVICE_ATTR_2(pwm4_stop_output, S_IWUSR | S_IRUGO, show_pwm,
2336 static SENSOR_DEVICE_ATTR_2(pwm5_stop_output, S_IWUSR | S_IRUGO, show_pwm,
2339 /* max_output is not supported on all chips */
2340 static struct sensor_device_attribute_2 sda_max_output[] = {
2341 SENSOR_ATTR_2(pwm1_max_output, S_IWUSR | S_IRUGO, show_pwm, store_pwm,
2343 SENSOR_ATTR_2(pwm2_max_output, S_IWUSR | S_IRUGO, show_pwm, store_pwm,
2345 SENSOR_ATTR_2(pwm3_max_output, S_IWUSR | S_IRUGO, show_pwm, store_pwm,
2347 SENSOR_ATTR_2(pwm4_max_output, S_IWUSR | S_IRUGO, show_pwm, store_pwm,
2349 SENSOR_ATTR_2(pwm5_max_output, S_IWUSR | S_IRUGO, show_pwm, store_pwm,
2353 /* step_output is not supported on all chips */
2354 static struct sensor_device_attribute_2 sda_step_output[] = {
2355 SENSOR_ATTR_2(pwm1_step_output, S_IWUSR | S_IRUGO, show_pwm, store_pwm,
2357 SENSOR_ATTR_2(pwm2_step_output, S_IWUSR | S_IRUGO, show_pwm, store_pwm,
2359 SENSOR_ATTR_2(pwm3_step_output, S_IWUSR | S_IRUGO, show_pwm, store_pwm,
2361 SENSOR_ATTR_2(pwm4_step_output, S_IWUSR | S_IRUGO, show_pwm, store_pwm,
2363 SENSOR_ATTR_2(pwm5_step_output, S_IWUSR | S_IRUGO, show_pwm, store_pwm,
2367 static struct attribute *nct6775_attributes_pwm[5][20] = {
2369 &sensor_dev_attr_temp11_input.dev_attr.attr,
2370 &sensor_dev_attr_pwm1.dev_attr.attr,
2371 &sensor_dev_attr_pwm1_mode.dev_attr.attr,
2372 &sensor_dev_attr_pwm1_enable.dev_attr.attr,
2373 &sensor_dev_attr_pwm1_stop_output_enable.dev_attr.attr,
2374 &sensor_dev_attr_pwm1_weight_enable.dev_attr.attr,
2375 &sensor_dev_attr_pwm1_temp_sel.dev_attr.attr,
2376 &sensor_dev_attr_pwm1_target.dev_attr.attr,
2377 &sensor_dev_attr_pwm1_stop_time.dev_attr.attr,
2378 &sensor_dev_attr_pwm1_step_up_time.dev_attr.attr,
2379 &sensor_dev_attr_pwm1_step_down_time.dev_attr.attr,
2380 &sensor_dev_attr_pwm1_start_output.dev_attr.attr,
2381 &sensor_dev_attr_pwm1_stop_output.dev_attr.attr,
2382 &sensor_dev_attr_pwm1_weight_temp_sel.dev_attr.attr,
2383 &sensor_dev_attr_pwm1_weight_temp_step.dev_attr.attr,
2384 &sensor_dev_attr_pwm1_weight_temp_step_tol.dev_attr.attr,
2385 &sensor_dev_attr_pwm1_weight_temp_step_base.dev_attr.attr,
2386 &sensor_dev_attr_pwm1_weight_duty_step.dev_attr.attr,
2390 &sensor_dev_attr_temp12_input.dev_attr.attr,
2391 &sensor_dev_attr_pwm2.dev_attr.attr,
2392 &sensor_dev_attr_pwm2_mode.dev_attr.attr,
2393 &sensor_dev_attr_pwm2_enable.dev_attr.attr,
2394 &sensor_dev_attr_pwm2_stop_output_enable.dev_attr.attr,
2395 &sensor_dev_attr_pwm2_weight_enable.dev_attr.attr,
2396 &sensor_dev_attr_pwm2_temp_sel.dev_attr.attr,
2397 &sensor_dev_attr_pwm2_target.dev_attr.attr,
2398 &sensor_dev_attr_pwm2_stop_time.dev_attr.attr,
2399 &sensor_dev_attr_pwm2_step_up_time.dev_attr.attr,
2400 &sensor_dev_attr_pwm2_step_down_time.dev_attr.attr,
2401 &sensor_dev_attr_pwm2_start_output.dev_attr.attr,
2402 &sensor_dev_attr_pwm2_stop_output.dev_attr.attr,
2403 &sensor_dev_attr_pwm2_weight_temp_sel.dev_attr.attr,
2404 &sensor_dev_attr_pwm2_weight_temp_step.dev_attr.attr,
2405 &sensor_dev_attr_pwm2_weight_temp_step_tol.dev_attr.attr,
2406 &sensor_dev_attr_pwm2_weight_temp_step_base.dev_attr.attr,
2407 &sensor_dev_attr_pwm2_weight_duty_step.dev_attr.attr,
2411 &sensor_dev_attr_temp13_input.dev_attr.attr,
2412 &sensor_dev_attr_pwm3.dev_attr.attr,
2413 &sensor_dev_attr_pwm3_mode.dev_attr.attr,
2414 &sensor_dev_attr_pwm3_enable.dev_attr.attr,
2415 &sensor_dev_attr_pwm3_stop_output_enable.dev_attr.attr,
2416 &sensor_dev_attr_pwm3_weight_enable.dev_attr.attr,
2417 &sensor_dev_attr_pwm3_temp_sel.dev_attr.attr,
2418 &sensor_dev_attr_pwm3_target.dev_attr.attr,
2419 &sensor_dev_attr_pwm3_stop_time.dev_attr.attr,
2420 &sensor_dev_attr_pwm3_step_up_time.dev_attr.attr,
2421 &sensor_dev_attr_pwm3_step_down_time.dev_attr.attr,
2422 &sensor_dev_attr_pwm3_start_output.dev_attr.attr,
2423 &sensor_dev_attr_pwm3_stop_output.dev_attr.attr,
2424 &sensor_dev_attr_pwm3_weight_temp_sel.dev_attr.attr,
2425 &sensor_dev_attr_pwm3_weight_temp_step.dev_attr.attr,
2426 &sensor_dev_attr_pwm3_weight_temp_step_tol.dev_attr.attr,
2427 &sensor_dev_attr_pwm3_weight_temp_step_base.dev_attr.attr,
2428 &sensor_dev_attr_pwm3_weight_duty_step.dev_attr.attr,
2432 &sensor_dev_attr_temp14_input.dev_attr.attr,
2433 &sensor_dev_attr_pwm4.dev_attr.attr,
2434 &sensor_dev_attr_pwm4_mode.dev_attr.attr,
2435 &sensor_dev_attr_pwm4_enable.dev_attr.attr,
2436 &sensor_dev_attr_pwm4_stop_output_enable.dev_attr.attr,
2437 &sensor_dev_attr_pwm4_weight_enable.dev_attr.attr,
2438 &sensor_dev_attr_pwm4_temp_sel.dev_attr.attr,
2439 &sensor_dev_attr_pwm4_target.dev_attr.attr,
2440 &sensor_dev_attr_pwm4_stop_time.dev_attr.attr,
2441 &sensor_dev_attr_pwm4_step_up_time.dev_attr.attr,
2442 &sensor_dev_attr_pwm4_step_down_time.dev_attr.attr,
2443 &sensor_dev_attr_pwm4_start_output.dev_attr.attr,
2444 &sensor_dev_attr_pwm4_stop_output.dev_attr.attr,
2445 &sensor_dev_attr_pwm4_weight_temp_sel.dev_attr.attr,
2446 &sensor_dev_attr_pwm4_weight_temp_step.dev_attr.attr,
2447 &sensor_dev_attr_pwm4_weight_temp_step_tol.dev_attr.attr,
2448 &sensor_dev_attr_pwm4_weight_temp_step_base.dev_attr.attr,
2449 &sensor_dev_attr_pwm4_weight_duty_step.dev_attr.attr,
2453 &sensor_dev_attr_temp15_input.dev_attr.attr,
2454 &sensor_dev_attr_pwm5.dev_attr.attr,
2455 &sensor_dev_attr_pwm5_mode.dev_attr.attr,
2456 &sensor_dev_attr_pwm5_enable.dev_attr.attr,
2457 &sensor_dev_attr_pwm5_stop_output_enable.dev_attr.attr,
2458 &sensor_dev_attr_pwm5_weight_enable.dev_attr.attr,
2459 &sensor_dev_attr_pwm5_temp_sel.dev_attr.attr,
2460 &sensor_dev_attr_pwm5_target.dev_attr.attr,
2461 &sensor_dev_attr_pwm5_stop_time.dev_attr.attr,
2462 &sensor_dev_attr_pwm5_step_up_time.dev_attr.attr,
2463 &sensor_dev_attr_pwm5_step_down_time.dev_attr.attr,
2464 &sensor_dev_attr_pwm5_start_output.dev_attr.attr,
2465 &sensor_dev_attr_pwm5_stop_output.dev_attr.attr,
2466 &sensor_dev_attr_pwm5_weight_temp_sel.dev_attr.attr,
2467 &sensor_dev_attr_pwm5_weight_temp_step.dev_attr.attr,
2468 &sensor_dev_attr_pwm5_weight_temp_step_tol.dev_attr.attr,
2469 &sensor_dev_attr_pwm5_weight_temp_step_base.dev_attr.attr,
2470 &sensor_dev_attr_pwm5_weight_duty_step.dev_attr.attr,
2475 static const struct attribute_group nct6775_group_pwm[5] = {
2476 { .attrs = nct6775_attributes_pwm[0] },
2477 { .attrs = nct6775_attributes_pwm[1] },
2478 { .attrs = nct6775_attributes_pwm[2] },
2479 { .attrs = nct6775_attributes_pwm[3] },
2480 { .attrs = nct6775_attributes_pwm[4] },
2484 show_auto_pwm(struct device *dev, struct device_attribute *attr, char *buf)
2486 struct nct6775_data *data = nct6775_update_device(dev);
2487 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
2489 return sprintf(buf, "%d\n", data->auto_pwm[sattr->nr][sattr->index]);
2493 store_auto_pwm(struct device *dev, struct device_attribute *attr,
2494 const char *buf, size_t count)
2496 struct nct6775_data *data = dev_get_drvdata(dev);
2497 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
2499 int point = sattr->index;
2504 err = kstrtoul(buf, 10, &val);
2510 if (point == data->auto_pwm_num) {
2511 if (data->kind != nct6775 && !val)
2513 if (data->kind != nct6779 && val)
2517 mutex_lock(&data->update_lock);
2518 data->auto_pwm[nr][point] = val;
2519 if (point < data->auto_pwm_num) {
2520 nct6775_write_value(data,
2521 NCT6775_AUTO_PWM(data, nr, point),
2522 data->auto_pwm[nr][point]);
2524 switch (data->kind) {
2526 /* disable if needed (pwm == 0) */
2527 reg = nct6775_read_value(data,
2528 NCT6775_REG_CRITICAL_ENAB[nr]);
2533 nct6775_write_value(data, NCT6775_REG_CRITICAL_ENAB[nr],
2537 break; /* always enabled, nothing to do */
2539 nct6775_write_value(data, NCT6779_REG_CRITICAL_PWM[nr],
2541 reg = nct6775_read_value(data,
2542 NCT6779_REG_CRITICAL_PWM_ENABLE[nr]);
2547 nct6775_write_value(data,
2548 NCT6779_REG_CRITICAL_PWM_ENABLE[nr],
2553 mutex_unlock(&data->update_lock);
2558 show_auto_temp(struct device *dev, struct device_attribute *attr, char *buf)
2560 struct nct6775_data *data = nct6775_update_device(dev);
2561 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
2563 int point = sattr->index;
2566 * We don't know for sure if the temperature is signed or unsigned.
2567 * Assume it is unsigned.
2569 return sprintf(buf, "%d\n", data->auto_temp[nr][point] * 1000);
2573 store_auto_temp(struct device *dev, struct device_attribute *attr,
2574 const char *buf, size_t count)
2576 struct nct6775_data *data = dev_get_drvdata(dev);
2577 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
2579 int point = sattr->index;
2583 err = kstrtoul(buf, 10, &val);
2589 mutex_lock(&data->update_lock);
2590 data->auto_temp[nr][point] = DIV_ROUND_CLOSEST(val, 1000);
2591 if (point < data->auto_pwm_num) {
2592 nct6775_write_value(data,
2593 NCT6775_AUTO_TEMP(data, nr, point),
2594 data->auto_temp[nr][point]);
2596 nct6775_write_value(data, data->REG_CRITICAL_TEMP[nr],
2597 data->auto_temp[nr][point]);
2599 mutex_unlock(&data->update_lock);
2604 * The number of auto-point trip points is chip dependent.
2605 * Need to check support while generating/removing attribute files.
2607 static struct sensor_device_attribute_2 sda_auto_pwm_arrays[] = {
2608 SENSOR_ATTR_2(pwm1_auto_point1_pwm, S_IWUSR | S_IRUGO,
2609 show_auto_pwm, store_auto_pwm, 0, 0),
2610 SENSOR_ATTR_2(pwm1_auto_point1_temp, S_IWUSR | S_IRUGO,
2611 show_auto_temp, store_auto_temp, 0, 0),
2612 SENSOR_ATTR_2(pwm1_auto_point1_temp_hyst, S_IWUSR | S_IRUGO,
2613 show_auto_temp_hyst, store_auto_temp_hyst, 0, 0),
2614 SENSOR_ATTR_2(pwm1_auto_point2_pwm, S_IWUSR | S_IRUGO,
2615 show_auto_pwm, store_auto_pwm, 0, 1),
2616 SENSOR_ATTR_2(pwm1_auto_point2_temp, S_IWUSR | S_IRUGO,
2617 show_auto_temp, store_auto_temp, 0, 1),
2618 SENSOR_ATTR_2(pwm1_auto_point2_temp_hyst, S_IWUSR | S_IRUGO,
2619 show_auto_temp_hyst, store_auto_temp_hyst, 0, 1),
2620 SENSOR_ATTR_2(pwm1_auto_point3_pwm, S_IWUSR | S_IRUGO,
2621 show_auto_pwm, store_auto_pwm, 0, 2),
2622 SENSOR_ATTR_2(pwm1_auto_point3_temp, S_IWUSR | S_IRUGO,
2623 show_auto_temp, store_auto_temp, 0, 2),
2624 SENSOR_ATTR_2(pwm1_auto_point3_temp_hyst, S_IWUSR | S_IRUGO,
2625 show_auto_temp_hyst, store_auto_temp_hyst, 0, 2),
2626 SENSOR_ATTR_2(pwm1_auto_point4_pwm, S_IWUSR | S_IRUGO,
2627 show_auto_pwm, store_auto_pwm, 0, 3),
2628 SENSOR_ATTR_2(pwm1_auto_point4_temp, S_IWUSR | S_IRUGO,
2629 show_auto_temp, store_auto_temp, 0, 3),
2630 SENSOR_ATTR_2(pwm1_auto_point4_temp_hyst, S_IWUSR | S_IRUGO,
2631 show_auto_temp_hyst, store_auto_temp_hyst, 0, 3),
2632 SENSOR_ATTR_2(pwm1_auto_point5_pwm, S_IWUSR | S_IRUGO,
2633 show_auto_pwm, store_auto_pwm, 0, 4),
2634 SENSOR_ATTR_2(pwm1_auto_point5_temp, S_IWUSR | S_IRUGO,
2635 show_auto_temp, store_auto_temp, 0, 4),
2636 SENSOR_ATTR_2(pwm1_auto_point5_temp_hyst, S_IWUSR | S_IRUGO,
2637 show_auto_temp_hyst, store_auto_temp_hyst, 0, 4),
2638 SENSOR_ATTR_2(pwm1_auto_point6_pwm, S_IWUSR | S_IRUGO,
2639 show_auto_pwm, store_auto_pwm, 0, 5),
2640 SENSOR_ATTR_2(pwm1_auto_point6_temp, S_IWUSR | S_IRUGO,
2641 show_auto_temp, store_auto_temp, 0, 5),
2642 SENSOR_ATTR_2(pwm1_auto_point6_temp_hyst, S_IWUSR | S_IRUGO,
2643 show_auto_temp_hyst, store_auto_temp_hyst, 0, 5),
2644 SENSOR_ATTR_2(pwm1_auto_point7_pwm, S_IWUSR | S_IRUGO,
2645 show_auto_pwm, store_auto_pwm, 0, 6),
2646 SENSOR_ATTR_2(pwm1_auto_point7_temp, S_IWUSR | S_IRUGO,
2647 show_auto_temp, store_auto_temp, 0, 6),
2648 SENSOR_ATTR_2(pwm1_auto_point7_temp_hyst, S_IWUSR | S_IRUGO,
2649 show_auto_temp_hyst, store_auto_temp_hyst, 0, 6),
2651 SENSOR_ATTR_2(pwm2_auto_point1_pwm, S_IWUSR | S_IRUGO,
2652 show_auto_pwm, store_auto_pwm, 1, 0),
2653 SENSOR_ATTR_2(pwm2_auto_point1_temp, S_IWUSR | S_IRUGO,
2654 show_auto_temp, store_auto_temp, 1, 0),
2655 SENSOR_ATTR_2(pwm2_auto_point1_temp_hyst, S_IWUSR | S_IRUGO,
2656 show_auto_temp_hyst, store_auto_temp_hyst, 1, 0),
2657 SENSOR_ATTR_2(pwm2_auto_point2_pwm, S_IWUSR | S_IRUGO,
2658 show_auto_pwm, store_auto_pwm, 1, 1),
2659 SENSOR_ATTR_2(pwm2_auto_point2_temp, S_IWUSR | S_IRUGO,
2660 show_auto_temp, store_auto_temp, 1, 1),
2661 SENSOR_ATTR_2(pwm2_auto_point2_temp_hyst, S_IWUSR | S_IRUGO,
2662 show_auto_temp_hyst, store_auto_temp_hyst, 1, 1),
2663 SENSOR_ATTR_2(pwm2_auto_point3_pwm, S_IWUSR | S_IRUGO,
2664 show_auto_pwm, store_auto_pwm, 1, 2),
2665 SENSOR_ATTR_2(pwm2_auto_point3_temp, S_IWUSR | S_IRUGO,
2666 show_auto_temp, store_auto_temp, 1, 2),
2667 SENSOR_ATTR_2(pwm2_auto_point3_temp_hyst, S_IWUSR | S_IRUGO,
2668 show_auto_temp_hyst, store_auto_temp_hyst, 1, 2),
2669 SENSOR_ATTR_2(pwm2_auto_point4_pwm, S_IWUSR | S_IRUGO,
2670 show_auto_pwm, store_auto_pwm, 1, 3),
2671 SENSOR_ATTR_2(pwm2_auto_point4_temp, S_IWUSR | S_IRUGO,
2672 show_auto_temp, store_auto_temp, 1, 3),
2673 SENSOR_ATTR_2(pwm2_auto_point4_temp_hyst, S_IWUSR | S_IRUGO,
2674 show_auto_temp_hyst, store_auto_temp_hyst, 1, 3),
2675 SENSOR_ATTR_2(pwm2_auto_point5_pwm, S_IWUSR | S_IRUGO,
2676 show_auto_pwm, store_auto_pwm, 1, 4),
2677 SENSOR_ATTR_2(pwm2_auto_point5_temp, S_IWUSR | S_IRUGO,
2678 show_auto_temp, store_auto_temp, 1, 4),
2679 SENSOR_ATTR_2(pwm2_auto_point5_temp_hyst, S_IWUSR | S_IRUGO,
2680 show_auto_temp_hyst, store_auto_temp_hyst, 1, 4),
2681 SENSOR_ATTR_2(pwm2_auto_point6_pwm, S_IWUSR | S_IRUGO,
2682 show_auto_pwm, store_auto_pwm, 1, 5),
2683 SENSOR_ATTR_2(pwm2_auto_point6_temp, S_IWUSR | S_IRUGO,
2684 show_auto_temp, store_auto_temp, 1, 5),
2685 SENSOR_ATTR_2(pwm2_auto_point6_temp_hyst, S_IWUSR | S_IRUGO,
2686 show_auto_temp_hyst, store_auto_temp_hyst, 1, 5),
2687 SENSOR_ATTR_2(pwm2_auto_point7_pwm, S_IWUSR | S_IRUGO,
2688 show_auto_pwm, store_auto_pwm, 1, 6),
2689 SENSOR_ATTR_2(pwm2_auto_point7_temp, S_IWUSR | S_IRUGO,
2690 show_auto_temp, store_auto_temp, 1, 6),
2691 SENSOR_ATTR_2(pwm2_auto_point7_temp_hyst, S_IWUSR | S_IRUGO,
2692 show_auto_temp_hyst, store_auto_temp_hyst, 1, 6),
2694 SENSOR_ATTR_2(pwm3_auto_point1_pwm, S_IWUSR | S_IRUGO,
2695 show_auto_pwm, store_auto_pwm, 2, 0),
2696 SENSOR_ATTR_2(pwm3_auto_point1_temp, S_IWUSR | S_IRUGO,
2697 show_auto_temp, store_auto_temp, 2, 0),
2698 SENSOR_ATTR_2(pwm3_auto_point1_temp_hyst, S_IWUSR | S_IRUGO,
2699 show_auto_temp_hyst, store_auto_temp_hyst, 2, 0),
2700 SENSOR_ATTR_2(pwm3_auto_point2_pwm, S_IWUSR | S_IRUGO,
2701 show_auto_pwm, store_auto_pwm, 2, 1),
2702 SENSOR_ATTR_2(pwm3_auto_point2_temp, S_IWUSR | S_IRUGO,
2703 show_auto_temp, store_auto_temp, 2, 1),
2704 SENSOR_ATTR_2(pwm3_auto_point2_temp_hyst, S_IWUSR | S_IRUGO,
2705 show_auto_temp_hyst, store_auto_temp_hyst, 2, 1),
2706 SENSOR_ATTR_2(pwm3_auto_point3_pwm, S_IWUSR | S_IRUGO,
2707 show_auto_pwm, store_auto_pwm, 2, 2),
2708 SENSOR_ATTR_2(pwm3_auto_point3_temp, S_IWUSR | S_IRUGO,
2709 show_auto_temp, store_auto_temp, 2, 2),
2710 SENSOR_ATTR_2(pwm3_auto_point3_temp_hyst, S_IWUSR | S_IRUGO,
2711 show_auto_temp_hyst, store_auto_temp_hyst, 2, 2),
2712 SENSOR_ATTR_2(pwm3_auto_point4_pwm, S_IWUSR | S_IRUGO,
2713 show_auto_pwm, store_auto_pwm, 2, 3),
2714 SENSOR_ATTR_2(pwm3_auto_point4_temp, S_IWUSR | S_IRUGO,
2715 show_auto_temp, store_auto_temp, 2, 3),
2716 SENSOR_ATTR_2(pwm3_auto_point4_temp_hyst, S_IWUSR | S_IRUGO,
2717 show_auto_temp_hyst, store_auto_temp_hyst, 2, 3),
2718 SENSOR_ATTR_2(pwm3_auto_point5_pwm, S_IWUSR | S_IRUGO,
2719 show_auto_pwm, store_auto_pwm, 2, 4),
2720 SENSOR_ATTR_2(pwm3_auto_point5_temp, S_IWUSR | S_IRUGO,
2721 show_auto_temp, store_auto_temp, 2, 4),
2722 SENSOR_ATTR_2(pwm3_auto_point5_temp_hyst, S_IWUSR | S_IRUGO,
2723 show_auto_temp_hyst, store_auto_temp_hyst, 2, 4),
2724 SENSOR_ATTR_2(pwm3_auto_point6_pwm, S_IWUSR | S_IRUGO,
2725 show_auto_pwm, store_auto_pwm, 2, 5),
2726 SENSOR_ATTR_2(pwm3_auto_point6_temp, S_IWUSR | S_IRUGO,
2727 show_auto_temp, store_auto_temp, 2, 5),
2728 SENSOR_ATTR_2(pwm3_auto_point6_temp_hyst, S_IWUSR | S_IRUGO,
2729 show_auto_temp_hyst, store_auto_temp_hyst, 2, 5),
2730 SENSOR_ATTR_2(pwm3_auto_point7_pwm, S_IWUSR | S_IRUGO,
2731 show_auto_pwm, store_auto_pwm, 2, 6),
2732 SENSOR_ATTR_2(pwm3_auto_point7_temp, S_IWUSR | S_IRUGO,
2733 show_auto_temp, store_auto_temp, 2, 6),
2734 SENSOR_ATTR_2(pwm3_auto_point7_temp_hyst, S_IWUSR | S_IRUGO,
2735 show_auto_temp_hyst, store_auto_temp_hyst, 2, 6),
2737 SENSOR_ATTR_2(pwm4_auto_point1_pwm, S_IWUSR | S_IRUGO,
2738 show_auto_pwm, store_auto_pwm, 3, 0),
2739 SENSOR_ATTR_2(pwm4_auto_point1_temp, S_IWUSR | S_IRUGO,
2740 show_auto_temp, store_auto_temp, 3, 0),
2741 SENSOR_ATTR_2(pwm4_auto_point1_temp_hyst, S_IWUSR | S_IRUGO,
2742 show_auto_temp_hyst, store_auto_temp_hyst, 3, 0),
2743 SENSOR_ATTR_2(pwm4_auto_point2_pwm, S_IWUSR | S_IRUGO,
2744 show_auto_pwm, store_auto_pwm, 3, 1),
2745 SENSOR_ATTR_2(pwm4_auto_point2_temp, S_IWUSR | S_IRUGO,
2746 show_auto_temp, store_auto_temp, 3, 1),
2747 SENSOR_ATTR_2(pwm4_auto_point2_temp_hyst, S_IWUSR | S_IRUGO,
2748 show_auto_temp_hyst, store_auto_temp_hyst, 3, 1),
2749 SENSOR_ATTR_2(pwm4_auto_point3_pwm, S_IWUSR | S_IRUGO,
2750 show_auto_pwm, store_auto_pwm, 3, 2),
2751 SENSOR_ATTR_2(pwm4_auto_point3_temp, S_IWUSR | S_IRUGO,
2752 show_auto_temp, store_auto_temp, 3, 2),
2753 SENSOR_ATTR_2(pwm4_auto_point3_temp_hyst, S_IWUSR | S_IRUGO,
2754 show_auto_temp_hyst, store_auto_temp_hyst, 3, 2),
2755 SENSOR_ATTR_2(pwm4_auto_point4_pwm, S_IWUSR | S_IRUGO,
2756 show_auto_pwm, store_auto_pwm, 3, 3),
2757 SENSOR_ATTR_2(pwm4_auto_point4_temp, S_IWUSR | S_IRUGO,
2758 show_auto_temp, store_auto_temp, 3, 3),
2759 SENSOR_ATTR_2(pwm4_auto_point4_temp_hyst, S_IWUSR | S_IRUGO,
2760 show_auto_temp_hyst, store_auto_temp_hyst, 3, 3),
2761 SENSOR_ATTR_2(pwm4_auto_point5_pwm, S_IWUSR | S_IRUGO,
2762 show_auto_pwm, store_auto_pwm, 3, 4),
2763 SENSOR_ATTR_2(pwm4_auto_point5_temp, S_IWUSR | S_IRUGO,
2764 show_auto_temp, store_auto_temp, 3, 4),
2765 SENSOR_ATTR_2(pwm4_auto_point5_temp_hyst, S_IWUSR | S_IRUGO,
2766 show_auto_temp_hyst, store_auto_temp_hyst, 3, 4),
2767 SENSOR_ATTR_2(pwm4_auto_point6_pwm, S_IWUSR | S_IRUGO,
2768 show_auto_pwm, store_auto_pwm, 3, 5),
2769 SENSOR_ATTR_2(pwm4_auto_point6_temp, S_IWUSR | S_IRUGO,
2770 show_auto_temp, store_auto_temp, 3, 5),
2771 SENSOR_ATTR_2(pwm4_auto_point6_temp_hyst, S_IWUSR | S_IRUGO,
2772 show_auto_temp_hyst, store_auto_temp_hyst, 3, 5),
2773 SENSOR_ATTR_2(pwm4_auto_point7_pwm, S_IWUSR | S_IRUGO,
2774 show_auto_pwm, store_auto_pwm, 3, 6),
2775 SENSOR_ATTR_2(pwm4_auto_point7_temp, S_IWUSR | S_IRUGO,
2776 show_auto_temp, store_auto_temp, 3, 6),
2777 SENSOR_ATTR_2(pwm4_auto_point7_temp_hyst, S_IWUSR | S_IRUGO,
2778 show_auto_temp_hyst, store_auto_temp_hyst, 3, 6),
2780 SENSOR_ATTR_2(pwm5_auto_point1_pwm, S_IWUSR | S_IRUGO,
2781 show_auto_pwm, store_auto_pwm, 4, 0),
2782 SENSOR_ATTR_2(pwm5_auto_point1_temp, S_IWUSR | S_IRUGO,
2783 show_auto_temp, store_auto_temp, 4, 0),
2784 SENSOR_ATTR_2(pwm5_auto_point1_temp_hyst, S_IWUSR | S_IRUGO,
2785 show_auto_temp_hyst, store_auto_temp_hyst, 4, 0),
2786 SENSOR_ATTR_2(pwm5_auto_point2_pwm, S_IWUSR | S_IRUGO,
2787 show_auto_pwm, store_auto_pwm, 4, 1),
2788 SENSOR_ATTR_2(pwm5_auto_point2_temp, S_IWUSR | S_IRUGO,
2789 show_auto_temp, store_auto_temp, 4, 1),
2790 SENSOR_ATTR_2(pwm5_auto_point2_temp_hyst, S_IWUSR | S_IRUGO,
2791 show_auto_temp_hyst, store_auto_temp_hyst, 4, 1),
2792 SENSOR_ATTR_2(pwm5_auto_point3_pwm, S_IWUSR | S_IRUGO,
2793 show_auto_pwm, store_auto_pwm, 4, 2),
2794 SENSOR_ATTR_2(pwm5_auto_point3_temp, S_IWUSR | S_IRUGO,
2795 show_auto_temp, store_auto_temp, 4, 2),
2796 SENSOR_ATTR_2(pwm5_auto_point3_temp_hyst, S_IWUSR | S_IRUGO,
2797 show_auto_temp_hyst, store_auto_temp_hyst, 4, 2),
2798 SENSOR_ATTR_2(pwm5_auto_point4_pwm, S_IWUSR | S_IRUGO,
2799 show_auto_pwm, store_auto_pwm, 4, 3),
2800 SENSOR_ATTR_2(pwm5_auto_point4_temp, S_IWUSR | S_IRUGO,
2801 show_auto_temp, store_auto_temp, 4, 3),
2802 SENSOR_ATTR_2(pwm5_auto_point4_temp_hyst, S_IWUSR | S_IRUGO,
2803 show_auto_temp_hyst, store_auto_temp_hyst, 4, 3),
2804 SENSOR_ATTR_2(pwm5_auto_point5_pwm, S_IWUSR | S_IRUGO,
2805 show_auto_pwm, store_auto_pwm, 4, 4),
2806 SENSOR_ATTR_2(pwm5_auto_point5_temp, S_IWUSR | S_IRUGO,
2807 show_auto_temp, store_auto_temp, 4, 4),
2808 SENSOR_ATTR_2(pwm5_auto_point5_temp_hyst, S_IWUSR | S_IRUGO,
2809 show_auto_temp_hyst, store_auto_temp_hyst, 4, 4),
2810 SENSOR_ATTR_2(pwm5_auto_point6_pwm, S_IWUSR | S_IRUGO,
2811 show_auto_pwm, store_auto_pwm, 4, 5),
2812 SENSOR_ATTR_2(pwm5_auto_point6_temp, S_IWUSR | S_IRUGO,
2813 show_auto_temp, store_auto_temp, 4, 5),
2814 SENSOR_ATTR_2(pwm5_auto_point6_temp_hyst, S_IWUSR | S_IRUGO,
2815 show_auto_temp_hyst, store_auto_temp_hyst, 4, 5),
2816 SENSOR_ATTR_2(pwm5_auto_point7_pwm, S_IWUSR | S_IRUGO,
2817 show_auto_pwm, store_auto_pwm, 4, 6),
2818 SENSOR_ATTR_2(pwm5_auto_point7_temp, S_IWUSR | S_IRUGO,
2819 show_auto_temp, store_auto_temp, 4, 6),
2820 SENSOR_ATTR_2(pwm5_auto_point7_temp_hyst, S_IWUSR | S_IRUGO,
2821 show_auto_temp_hyst, store_auto_temp_hyst, 4, 6),
2825 show_vid(struct device *dev, struct device_attribute *attr, char *buf)
2827 struct nct6775_data *data = dev_get_drvdata(dev);
2828 return sprintf(buf, "%d\n", vid_from_reg(data->vid, data->vrm));
2831 static DEVICE_ATTR(cpu0_vid, S_IRUGO, show_vid, NULL);
2833 /* Case open detection */
2836 clear_caseopen(struct device *dev, struct device_attribute *attr,
2837 const char *buf, size_t count)
2839 struct nct6775_data *data = dev_get_drvdata(dev);
2840 struct nct6775_sio_data *sio_data = dev->platform_data;
2841 int nr = to_sensor_dev_attr(attr)->index - INTRUSION_ALARM_BASE;
2845 if (kstrtoul(buf, 10, &val) || val != 0)
2848 mutex_lock(&data->update_lock);
2851 * Use CR registers to clear caseopen status.
2852 * The CR registers are the same for all chips, and not all chips
2853 * support clearing the caseopen status through "regular" registers.
2855 superio_enter(sio_data->sioreg);
2856 superio_select(sio_data->sioreg, NCT6775_LD_ACPI);
2857 reg = superio_inb(sio_data->sioreg, NCT6775_REG_CR_CASEOPEN_CLR[nr]);
2858 reg |= NCT6775_CR_CASEOPEN_CLR_MASK[nr];
2859 superio_outb(sio_data->sioreg, NCT6775_REG_CR_CASEOPEN_CLR[nr], reg);
2860 reg &= ~NCT6775_CR_CASEOPEN_CLR_MASK[nr];
2861 superio_outb(sio_data->sioreg, NCT6775_REG_CR_CASEOPEN_CLR[nr], reg);
2862 superio_exit(sio_data->sioreg);
2864 data->valid = 0; /* Force cache refresh */
2866 mutex_unlock(&data->update_lock);
2871 static struct sensor_device_attribute sda_caseopen[] = {
2872 SENSOR_ATTR(intrusion0_alarm, S_IWUSR | S_IRUGO, show_alarm,
2873 clear_caseopen, INTRUSION_ALARM_BASE),
2874 SENSOR_ATTR(intrusion1_alarm, S_IWUSR | S_IRUGO, show_alarm,
2875 clear_caseopen, INTRUSION_ALARM_BASE + 1),
2879 * Driver and device management
2882 static void nct6775_device_remove_files(struct device *dev)
2885 * some entries in the following arrays may not have been used in
2886 * device_create_file(), but device_remove_file() will ignore them
2889 struct nct6775_data *data = dev_get_drvdata(dev);
2891 for (i = 0; i < data->pwm_num; i++)
2892 sysfs_remove_group(&dev->kobj, &nct6775_group_pwm[i]);
2894 for (i = 0; i < ARRAY_SIZE(sda_max_output); i++)
2895 device_remove_file(dev, &sda_max_output[i].dev_attr);
2897 for (i = 0; i < ARRAY_SIZE(sda_step_output); i++)
2898 device_remove_file(dev, &sda_max_output[i].dev_attr);
2900 for (i = 0; i < ARRAY_SIZE(sda_weight_duty_base); i++)
2901 device_remove_file(dev, &sda_weight_duty_base[i].dev_attr);
2903 for (i = 0; i < ARRAY_SIZE(sda_auto_pwm_arrays); i++)
2904 device_remove_file(dev, &sda_auto_pwm_arrays[i].dev_attr);
2906 for (i = 0; i < data->in_num; i++)
2907 sysfs_remove_group(&dev->kobj, &nct6775_group_in[i]);
2909 for (i = 0; i < 5; i++) {
2910 device_remove_file(dev, &sda_fan_input[i].dev_attr);
2911 device_remove_file(dev, &sda_fan_alarm[i].dev_attr);
2912 device_remove_file(dev, &sda_fan_div[i].dev_attr);
2913 device_remove_file(dev, &sda_fan_min[i].dev_attr);
2915 for (i = 0; i < NUM_TEMP; i++) {
2916 if (!(data->have_temp & (1 << i)))
2918 device_remove_file(dev, &sda_temp_input[i].dev_attr);
2919 device_remove_file(dev, &sda_temp_label[i].dev_attr);
2920 device_remove_file(dev, &sda_temp_max[i].dev_attr);
2921 device_remove_file(dev, &sda_temp_max_hyst[i].dev_attr);
2922 if (!(data->have_temp_fixed & (1 << i)))
2924 device_remove_file(dev, &sda_temp_type[i].dev_attr);
2925 device_remove_file(dev, &sda_temp_offset[i].dev_attr);
2926 if (i >= NUM_TEMP_ALARM)
2928 device_remove_file(dev, &sda_temp_alarm[i].dev_attr);
2931 device_remove_file(dev, &sda_caseopen[0].dev_attr);
2932 device_remove_file(dev, &sda_caseopen[1].dev_attr);
2934 device_remove_file(dev, &dev_attr_name);
2935 device_remove_file(dev, &dev_attr_cpu0_vid);
2938 /* Get the monitoring functions started */
2939 static inline void __devinit nct6775_init_device(struct nct6775_data *data)
2944 /* Start monitoring if needed */
2945 if (data->REG_CONFIG) {
2946 tmp = nct6775_read_value(data, data->REG_CONFIG);
2948 nct6775_write_value(data, data->REG_CONFIG, tmp | 0x01);
2951 /* Enable temperature sensors if needed */
2952 for (i = 0; i < NUM_TEMP; i++) {
2953 if (!(data->have_temp & (1 << i)))
2955 if (!data->reg_temp_config[i])
2957 tmp = nct6775_read_value(data, data->reg_temp_config[i]);
2959 nct6775_write_value(data, data->reg_temp_config[i],
2963 /* Enable VBAT monitoring if needed */
2964 tmp = nct6775_read_value(data, data->REG_VBAT);
2966 nct6775_write_value(data, data->REG_VBAT, tmp | 0x01);
2968 diode = nct6775_read_value(data, data->REG_DIODE);
2970 for (i = 0; i < data->temp_fixed_num; i++) {
2971 if (!(data->have_temp_fixed & (1 << i)))
2973 if ((tmp & (0x02 << i))) /* diode */
2974 data->temp_type[i] = 3 - ((diode >> i) & 0x02);
2975 else /* thermistor */
2976 data->temp_type[i] = 4;
2980 static void __devinit
2981 nct6775_check_fan_inputs(const struct nct6775_sio_data *sio_data,
2982 struct nct6775_data *data)
2985 bool fan3pin, fan3min, fan4pin, fan4min, fan5pin;
2986 bool pwm3pin, pwm4pin, pwm5pin;
2988 superio_enter(sio_data->sioreg);
2990 /* fan4 and fan5 share some pins with the GPIO and serial flash */
2991 if (data->kind == nct6775) {
2992 regval = superio_inb(sio_data->sioreg, 0x2c);
2994 fan3pin = regval & (1 << 6);
2996 pwm3pin = regval & (1 << 7);
2998 /* On NCT6775, fan4 shares pins with the fdc interface */
2999 fan4pin = !(superio_inb(sio_data->sioreg, 0x2A) & 0x80);
3004 } else if (data->kind == nct6776) {
3005 bool gpok = superio_inb(sio_data->sioreg, 0x27) & 0x80;
3007 superio_select(sio_data->sioreg, NCT6775_LD_HWM);
3008 regval = superio_inb(sio_data->sioreg, SIO_REG_ENABLE);
3013 fan3pin = !(superio_inb(sio_data->sioreg, 0x24) & 0x40);
3018 fan4pin = superio_inb(sio_data->sioreg, 0x1C) & 0x01;
3023 fan5pin = superio_inb(sio_data->sioreg, 0x1C) & 0x02;
3030 } else { /* NCT6779D */
3031 regval = superio_inb(sio_data->sioreg, 0x1c);
3033 fan3pin = !(regval & (1 << 5));
3034 fan4pin = !(regval & (1 << 6));
3035 fan5pin = !(regval & (1 << 7));
3037 pwm3pin = !(regval & (1 << 0));
3038 pwm4pin = !(regval & (1 << 1));
3039 pwm5pin = !(regval & (1 << 2));
3045 superio_exit(sio_data->sioreg);
3047 data->has_fan = data->has_fan_min = 0x03; /* fan1 and fan2 */
3048 data->has_fan |= fan3pin << 2;
3049 data->has_fan_min |= fan3min << 2;
3051 data->has_fan |= (fan4pin << 3) | (fan5pin << 4);
3052 data->has_fan_min |= (fan4min << 3) | (fan5pin << 4);
3054 data->has_pwm = 0x03 | (pwm3pin << 2) | (pwm4pin << 3) | (pwm5pin << 4);
3057 static int __devinit nct6775_probe(struct platform_device *pdev)
3059 struct device *dev = &pdev->dev;
3060 struct nct6775_sio_data *sio_data = dev->platform_data;
3061 struct nct6775_data *data;
3062 struct resource *res;
3064 int src, mask, available;
3065 const u16 *reg_temp, *reg_temp_over, *reg_temp_hyst, *reg_temp_config;
3067 res = platform_get_resource(pdev, IORESOURCE_IO, 0);
3068 if (!request_region(res->start, IOREGION_LENGTH, DRVNAME)) {
3070 dev_err(dev, "Failed to request region 0x%lx-0x%lx\n",
3071 (unsigned long)res->start,
3072 (unsigned long)res->start + IOREGION_LENGTH - 1);
3076 data = devm_kzalloc(&pdev->dev, sizeof(struct nct6775_data),
3083 data->kind = sio_data->kind;
3084 data->addr = res->start;
3085 mutex_init(&data->lock);
3086 mutex_init(&data->update_lock);
3087 data->name = nct6775_device_names[data->kind];
3088 platform_set_drvdata(pdev, data);
3090 switch (data->kind) {
3094 data->auto_pwm_num = 6;
3095 data->has_fan_div = true;
3096 data->temp_fixed_num = 3;
3098 data->ALARM_BITS = NCT6775_ALARM_BITS;
3100 data->fan_from_reg = fan_from_reg16;
3101 data->fan_from_reg_min = fan_from_reg8;
3103 data->temp_label = nct6775_temp_label;
3104 data->temp_label_num = ARRAY_SIZE(nct6775_temp_label);
3106 data->REG_CONFIG = NCT6775_REG_CONFIG;
3107 data->REG_VBAT = NCT6775_REG_VBAT;
3108 data->REG_DIODE = NCT6775_REG_DIODE;
3109 data->REG_VIN = NCT6775_REG_IN;
3110 data->REG_IN_MINMAX[0] = NCT6775_REG_IN_MIN;
3111 data->REG_IN_MINMAX[1] = NCT6775_REG_IN_MAX;
3112 data->REG_TARGET = NCT6775_REG_TARGET;
3113 data->REG_FAN = NCT6775_REG_FAN;
3114 data->REG_FAN_MODE = NCT6775_REG_FAN_MODE;
3115 data->REG_FAN_MIN = NCT6775_REG_FAN_MIN;
3116 data->REG_FAN_TIME[0] = NCT6775_REG_FAN_STOP_TIME;
3117 data->REG_FAN_TIME[1] = NCT6775_REG_FAN_STEP_UP_TIME;
3118 data->REG_FAN_TIME[2] = NCT6775_REG_FAN_STEP_DOWN_TIME;
3119 data->REG_PWM[0] = NCT6775_REG_PWM;
3120 data->REG_PWM[1] = NCT6775_REG_FAN_START_OUTPUT;
3121 data->REG_PWM[2] = NCT6775_REG_FAN_STOP_OUTPUT;
3122 data->REG_PWM[3] = NCT6775_REG_FAN_MAX_OUTPUT;
3123 data->REG_PWM[4] = NCT6775_REG_FAN_STEP_OUTPUT;
3124 data->REG_PWM[5] = NCT6775_REG_WEIGHT_DUTY_STEP;
3125 data->REG_PWM_READ = NCT6775_REG_PWM_READ;
3126 data->REG_PWM_MODE = NCT6775_REG_PWM_MODE;
3127 data->PWM_MODE_MASK = NCT6775_PWM_MODE_MASK;
3128 data->REG_TEMP_ALTERNATE = NCT6775_REG_TEMP_ALTERNATE;
3129 data->REG_TEMP_MON = NCT6775_REG_TEMP_MON;
3130 data->REG_AUTO_TEMP = NCT6775_REG_AUTO_TEMP;
3131 data->REG_AUTO_PWM = NCT6775_REG_AUTO_PWM;
3132 data->REG_CRITICAL_TEMP = NCT6775_REG_CRITICAL_TEMP;
3133 data->REG_CRITICAL_TEMP_TOLERANCE
3134 = NCT6775_REG_CRITICAL_TEMP_TOLERANCE;
3135 data->REG_TEMP_OFFSET = NCT6775_REG_TEMP_OFFSET;
3136 data->REG_TEMP_SOURCE = NCT6775_REG_TEMP_SOURCE;
3137 data->REG_TEMP_SEL[0] = NCT6775_REG_TEMP_SEL;
3138 data->REG_TEMP_SEL[1] = NCT6775_REG_WEIGHT_TEMP_SEL;
3139 data->REG_WEIGHT_TEMP[0] = NCT6775_REG_WEIGHT_TEMP_STEP;
3140 data->REG_WEIGHT_TEMP[1] = NCT6775_REG_WEIGHT_TEMP_STEP_TOL;
3141 data->REG_WEIGHT_TEMP[2] = NCT6775_REG_WEIGHT_TEMP_BASE;
3142 data->REG_ALARM = NCT6775_REG_ALARM;
3144 reg_temp = NCT6775_REG_TEMP;
3145 reg_temp_over = NCT6775_REG_TEMP_OVER;
3146 reg_temp_hyst = NCT6775_REG_TEMP_HYST;
3147 reg_temp_config = NCT6775_REG_TEMP_CONFIG;
3153 data->auto_pwm_num = 4;
3154 data->has_fan_div = false;
3155 data->temp_fixed_num = 3;
3157 data->ALARM_BITS = NCT6776_ALARM_BITS;
3159 data->fan_from_reg = fan_from_reg13;
3160 data->fan_from_reg_min = fan_from_reg13;
3162 data->temp_label = nct6776_temp_label;
3163 data->temp_label_num = ARRAY_SIZE(nct6776_temp_label);
3165 data->REG_CONFIG = NCT6775_REG_CONFIG;
3166 data->REG_VBAT = NCT6775_REG_VBAT;
3167 data->REG_DIODE = NCT6775_REG_DIODE;
3168 data->REG_VIN = NCT6775_REG_IN;
3169 data->REG_IN_MINMAX[0] = NCT6775_REG_IN_MIN;
3170 data->REG_IN_MINMAX[1] = NCT6775_REG_IN_MAX;
3171 data->REG_TARGET = NCT6775_REG_TARGET;
3172 data->REG_FAN = NCT6775_REG_FAN;
3173 data->REG_FAN_MODE = NCT6775_REG_FAN_MODE;
3174 data->REG_FAN_MIN = NCT6776_REG_FAN_MIN;
3175 data->REG_FAN_TIME[0] = NCT6775_REG_FAN_STOP_TIME;
3176 data->REG_FAN_TIME[1] = NCT6775_REG_FAN_STEP_UP_TIME;
3177 data->REG_FAN_TIME[2] = NCT6775_REG_FAN_STEP_DOWN_TIME;
3178 data->REG_PWM[0] = NCT6775_REG_PWM;
3179 data->REG_PWM[1] = NCT6775_REG_FAN_START_OUTPUT;
3180 data->REG_PWM[2] = NCT6775_REG_FAN_STOP_OUTPUT;
3181 data->REG_PWM[5] = NCT6775_REG_WEIGHT_DUTY_STEP;
3182 data->REG_PWM[6] = NCT6776_REG_WEIGHT_DUTY_BASE;
3183 data->REG_PWM_READ = NCT6775_REG_PWM_READ;
3184 data->REG_PWM_MODE = NCT6776_REG_PWM_MODE;
3185 data->PWM_MODE_MASK = NCT6776_PWM_MODE_MASK;
3186 data->REG_TEMP_ALTERNATE = NCT6776_REG_TEMP_ALTERNATE;
3187 data->REG_TEMP_MON = NCT6775_REG_TEMP_MON;
3188 data->REG_AUTO_TEMP = NCT6775_REG_AUTO_TEMP;
3189 data->REG_AUTO_PWM = NCT6775_REG_AUTO_PWM;
3190 data->REG_CRITICAL_TEMP = NCT6775_REG_CRITICAL_TEMP;
3191 data->REG_CRITICAL_TEMP_TOLERANCE
3192 = NCT6775_REG_CRITICAL_TEMP_TOLERANCE;
3193 data->REG_TEMP_OFFSET = NCT6775_REG_TEMP_OFFSET;
3194 data->REG_TEMP_SOURCE = NCT6775_REG_TEMP_SOURCE;
3195 data->REG_TEMP_SEL[0] = NCT6775_REG_TEMP_SEL;
3196 data->REG_TEMP_SEL[1] = NCT6775_REG_WEIGHT_TEMP_SEL;
3197 data->REG_WEIGHT_TEMP[0] = NCT6775_REG_WEIGHT_TEMP_STEP;
3198 data->REG_WEIGHT_TEMP[1] = NCT6775_REG_WEIGHT_TEMP_STEP_TOL;
3199 data->REG_WEIGHT_TEMP[2] = NCT6775_REG_WEIGHT_TEMP_BASE;
3200 data->REG_ALARM = NCT6775_REG_ALARM;
3202 reg_temp = NCT6775_REG_TEMP;
3203 reg_temp_over = NCT6775_REG_TEMP_OVER;
3204 reg_temp_hyst = NCT6775_REG_TEMP_HYST;
3205 reg_temp_config = NCT6776_REG_TEMP_CONFIG;
3211 data->auto_pwm_num = 4;
3212 data->has_fan_div = false;
3213 data->temp_fixed_num = 6;
3215 data->ALARM_BITS = NCT6779_ALARM_BITS;
3217 data->fan_from_reg = fan_from_reg13;
3218 data->fan_from_reg_min = fan_from_reg13;
3220 data->temp_label = nct6779_temp_label;
3221 data->temp_label_num = ARRAY_SIZE(nct6779_temp_label);
3223 data->REG_CONFIG = NCT6775_REG_CONFIG;
3224 data->REG_VBAT = NCT6775_REG_VBAT;
3225 data->REG_DIODE = NCT6775_REG_DIODE;
3226 data->REG_VIN = NCT6779_REG_IN;
3227 data->REG_IN_MINMAX[0] = NCT6775_REG_IN_MIN;
3228 data->REG_IN_MINMAX[1] = NCT6775_REG_IN_MAX;
3229 data->REG_TARGET = NCT6775_REG_TARGET;
3230 data->REG_FAN = NCT6779_REG_FAN;
3231 data->REG_FAN_MODE = NCT6775_REG_FAN_MODE;
3232 data->REG_FAN_MIN = NCT6776_REG_FAN_MIN;
3233 data->REG_FAN_TIME[0] = NCT6775_REG_FAN_STOP_TIME;
3234 data->REG_FAN_TIME[1] = NCT6775_REG_FAN_STEP_UP_TIME;
3235 data->REG_FAN_TIME[2] = NCT6775_REG_FAN_STEP_DOWN_TIME;
3236 data->REG_PWM[0] = NCT6775_REG_PWM;
3237 data->REG_PWM[1] = NCT6775_REG_FAN_START_OUTPUT;
3238 data->REG_PWM[2] = NCT6775_REG_FAN_STOP_OUTPUT;
3239 data->REG_PWM[5] = NCT6775_REG_WEIGHT_DUTY_STEP;
3240 data->REG_PWM[6] = NCT6776_REG_WEIGHT_DUTY_BASE;
3241 data->REG_PWM_READ = NCT6775_REG_PWM_READ;
3242 data->REG_PWM_MODE = NCT6776_REG_PWM_MODE;
3243 data->PWM_MODE_MASK = NCT6776_PWM_MODE_MASK;
3244 data->REG_TEMP_ALTERNATE = NCT6779_REG_TEMP_ALTERNATE;
3245 data->REG_TEMP_MON = NCT6775_REG_TEMP_MON;
3246 data->REG_AUTO_TEMP = NCT6775_REG_AUTO_TEMP;
3247 data->REG_AUTO_PWM = NCT6775_REG_AUTO_PWM;
3248 data->REG_CRITICAL_TEMP = NCT6775_REG_CRITICAL_TEMP;
3249 data->REG_CRITICAL_TEMP_TOLERANCE
3250 = NCT6775_REG_CRITICAL_TEMP_TOLERANCE;
3251 data->REG_TEMP_OFFSET = NCT6779_REG_TEMP_OFFSET;
3252 data->REG_TEMP_SOURCE = NCT6775_REG_TEMP_SOURCE;
3253 data->REG_TEMP_SEL[0] = NCT6775_REG_TEMP_SEL;
3254 data->REG_TEMP_SEL[1] = NCT6775_REG_WEIGHT_TEMP_SEL;
3255 data->REG_WEIGHT_TEMP[0] = NCT6775_REG_WEIGHT_TEMP_STEP;
3256 data->REG_WEIGHT_TEMP[1] = NCT6775_REG_WEIGHT_TEMP_STEP_TOL;
3257 data->REG_WEIGHT_TEMP[2] = NCT6775_REG_WEIGHT_TEMP_BASE;
3258 data->REG_ALARM = NCT6779_REG_ALARM;
3260 reg_temp = NCT6779_REG_TEMP;
3261 reg_temp_over = NCT6775_REG_TEMP_OVER;
3262 reg_temp_hyst = NCT6775_REG_TEMP_HYST;
3263 reg_temp_config = NCT6776_REG_TEMP_CONFIG;
3270 data->have_in = (1 << data->in_num) - 1;
3271 data->have_temp = 0;
3274 * On some boards, not all available temperature sources are monitored,
3275 * even though some of the monitoring registers are unused.
3276 * Get list of unused monitoring registers, then detect if any fan
3277 * controls are configured to use unmonitored temperature sources.
3278 * If so, assign the unmonitored temperature sources to available
3279 * monitoring registers.
3283 for (i = 0; i < NUM_REG_TEMP; i++) {
3284 if (reg_temp[i] == 0)
3287 src = nct6775_read_value(data, data->REG_TEMP_SOURCE[i]) & 0x1f;
3288 if (!src || (mask & (1 << src)))
3289 available |= 1 << i;
3295 * Now find unmonitored temperature registers and enable monitoring
3296 * if additional monitoring registers are available.
3298 for (i = 0; i < ARRAY_SIZE(data->REG_TEMP_SEL) && available; i++) {
3301 if (!data->REG_TEMP_SEL[i])
3303 for (j = 0; j < data->pwm_num && available; j++) {
3306 if (!data->REG_TEMP_SEL[i][j])
3308 src = nct6775_read_value(data,
3309 data->REG_TEMP_SEL[i][j]);
3311 if (!src || (mask & (1 << src)))
3314 index = __ffs(available);
3315 nct6775_write_value(data,
3316 data->REG_TEMP_SOURCE[index],
3318 available &= ~(1 << index);
3324 s = NUM_TEMP_FIXED; /* First dynamic temperature attribute */
3325 for (i = 0; i < NUM_REG_TEMP; i++) {
3326 if (reg_temp[i] == 0)
3329 src = nct6775_read_value(data, data->REG_TEMP_SOURCE[i]) & 0x1f;
3330 if (!src || (mask & (1 << src)))
3333 if (src >= data->temp_label_num ||
3334 !strlen(data->temp_label[src])) {
3336 "Invalid temperature source %d at index %d, source register 0x%x, temp register 0x%x\n",
3337 src, i, data->REG_TEMP_SOURCE[i], reg_temp[i]);
3343 /* Use fixed index for SYSTIN(1), CPUTIN(2), AUXTIN(3) */
3344 if (src <= data->temp_fixed_num) {
3345 data->have_temp |= 1 << (src - 1);
3346 data->have_temp_fixed |= 1 << (src - 1);
3347 data->reg_temp[0][src - 1] = reg_temp[i];
3348 data->reg_temp[1][src - 1] = reg_temp_over[i];
3349 data->reg_temp[2][src - 1] = reg_temp_hyst[i];
3350 data->reg_temp_config[src - 1] = reg_temp_config[i];
3351 data->temp_src[src - 1] = src;
3358 /* Use dynamic index for other sources */
3359 data->have_temp |= 1 << s;
3360 data->reg_temp[0][s] = reg_temp[i];
3361 data->reg_temp[1][s] = reg_temp_over[i];
3362 data->reg_temp[2][s] = reg_temp_hyst[i];
3363 data->reg_temp_config[s] = reg_temp_config[i];
3365 data->temp_src[s] = src;
3371 * We may have alternate registers for some sensors.
3372 * Go through the list and enable if possible.
3373 * The temperature is already monitored if the respective bit in <mask>
3376 if (data->REG_TEMP_ALTERNATE) {
3377 for (i = 0; i < data->temp_label_num; i++) {
3378 if (!data->REG_TEMP_ALTERNATE[i])
3380 if (mask & (1 << (i + 1)))
3382 if (i < data->temp_fixed_num) {
3383 if (data->have_temp & (1 << i))
3385 data->have_temp |= 1 << i;
3386 data->reg_temp[0][i]
3387 = data->REG_TEMP_ALTERNATE[i];
3388 data->temp_src[i] = i + 1;
3392 if (s >= NUM_TEMP) /* Abort if no more space */
3395 data->have_temp |= 1 << s;
3396 data->reg_temp[0][s] = data->REG_TEMP_ALTERNATE[i];
3397 data->temp_src[s] = i + 1;
3401 #endif /* TESTING */
3403 switch (data->kind) {
3408 * On NCT6776, AUXTIN and VIN3 pins are shared.
3409 * Only way to detect it is to check if AUXTIN is used
3410 * as a temperature source, and if that source is
3413 * If that is the case, disable in6, which reports VIN3.
3414 * Otherwise disable temp3.
3416 if (data->have_temp & (1 << 2)) {
3417 u8 reg = nct6775_read_value(data,
3418 data->reg_temp_config[2]);
3420 data->have_temp &= ~(1 << 2);
3422 data->have_in &= ~(1 << 6);
3432 * Assume voltage is disabled if the respective temperature is
3433 * used as temperature source.
3435 for (i = 0; i < ARRAY_SIZE(NCT6779_REG_TEMP); i++) {
3436 if (!(data->have_temp & (1 << i)))
3438 if (i == 2) /* AUXTIN0 */
3439 data->have_in &= ~(1 << 6); /* no VIN4 */
3440 if (i == 3) /* AUXTIN1 */
3441 data->have_in &= ~(1 << 10); /* no VIN5 */
3442 if (i == 4) /* AUXTIN2 */
3443 data->have_in &= ~(1 << 11); /* no VIN6 */
3444 if (i == 5) /* AUXTIN3 */
3445 data->have_in &= ~(1 << 14); /* no VIN7 */
3450 /* Initialize the chip */
3451 nct6775_init_device(data);
3453 data->vrm = vid_which_vrm();
3454 superio_enter(sio_data->sioreg);
3457 * We can get the VID input values directly at logical device D 0xe3.
3459 superio_select(sio_data->sioreg, NCT6775_LD_VID);
3460 data->vid = superio_inb(sio_data->sioreg, 0xe3);
3461 err = device_create_file(dev, &dev_attr_cpu0_vid);
3468 superio_select(sio_data->sioreg, NCT6775_LD_HWM);
3469 tmp = superio_inb(sio_data->sioreg,
3470 NCT6775_REG_CR_FAN_DEBOUNCE);
3471 switch (data->kind) {
3480 superio_outb(sio_data->sioreg, NCT6775_REG_CR_FAN_DEBOUNCE,
3482 dev_info(&pdev->dev, "Enabled fan debounce for chip %s\n",
3486 superio_exit(sio_data->sioreg);
3488 nct6775_check_fan_inputs(sio_data, data);
3490 /* Read fan clock dividers immediately */
3491 nct6775_update_fan_div_common(dev, data);
3493 /* Register sysfs hooks */
3494 for (i = 0; i < data->pwm_num; i++) {
3495 if (!(data->has_pwm & (1 << i)))
3498 err = sysfs_create_group(&dev->kobj, &nct6775_group_pwm[i]);
3502 if (data->REG_PWM[3]) {
3503 err = device_create_file(dev,
3504 &sda_max_output[i].dev_attr);
3508 if (data->REG_PWM[4]) {
3509 err = device_create_file(dev,
3510 &sda_step_output[i].dev_attr);
3514 if (data->REG_PWM[6]) {
3515 err = device_create_file(dev,
3516 &sda_weight_duty_base[i].dev_attr);
3521 for (i = 0; i < ARRAY_SIZE(sda_auto_pwm_arrays); i++) {
3522 struct sensor_device_attribute_2 *attr =
3523 &sda_auto_pwm_arrays[i];
3525 if (!(data->has_pwm & (1 << attr->nr)))
3527 if (attr->index > data->auto_pwm_num)
3529 err = device_create_file(dev, &attr->dev_attr);
3534 for (i = 0; i < data->in_num; i++) {
3535 if (!(data->have_in & (1 << i)))
3537 err = sysfs_create_group(&dev->kobj, &nct6775_group_in[i]);
3542 for (i = 0; i < 5; i++) {
3543 if (data->has_fan & (1 << i)) {
3544 err = device_create_file(dev,
3545 &sda_fan_input[i].dev_attr);
3548 err = device_create_file(dev,
3549 &sda_fan_alarm[i].dev_attr);
3552 if (data->kind != nct6776 &&
3553 data->kind != nct6779) {
3554 err = device_create_file(dev,
3555 &sda_fan_div[i].dev_attr);
3559 if (data->has_fan_min & (1 << i)) {
3560 err = device_create_file(dev,
3561 &sda_fan_min[i].dev_attr);
3568 for (i = 0; i < NUM_TEMP; i++) {
3569 if (!(data->have_temp & (1 << i)))
3571 err = device_create_file(dev, &sda_temp_input[i].dev_attr);
3574 if (data->temp_label) {
3575 err = device_create_file(dev,
3576 &sda_temp_label[i].dev_attr);
3580 if (data->reg_temp[1][i]) {
3581 err = device_create_file(dev,
3582 &sda_temp_max[i].dev_attr);
3586 if (data->reg_temp[2][i]) {
3587 err = device_create_file(dev,
3588 &sda_temp_max_hyst[i].dev_attr);
3592 if (!(data->have_temp_fixed & (1 << i)))
3594 err = device_create_file(dev, &sda_temp_type[i].dev_attr);
3597 err = device_create_file(dev, &sda_temp_offset[i].dev_attr);
3600 if (i >= NUM_TEMP_ALARM ||
3601 data->ALARM_BITS[TEMP_ALARM_BASE + i] < 0)
3603 err = device_create_file(dev, &sda_temp_alarm[i].dev_attr);
3608 for (i = 0; i < ARRAY_SIZE(sda_caseopen); i++) {
3609 if (data->ALARM_BITS[INTRUSION_ALARM_BASE + i] < 0)
3611 err = device_create_file(dev, &sda_caseopen[i].dev_attr);
3616 err = device_create_file(dev, &dev_attr_name);
3620 data->hwmon_dev = hwmon_device_register(dev);
3621 if (IS_ERR(data->hwmon_dev)) {
3622 err = PTR_ERR(data->hwmon_dev);
3629 nct6775_device_remove_files(dev);
3631 release_region(res->start, IOREGION_LENGTH);
3636 static int __devexit nct6775_remove(struct platform_device *pdev)
3638 struct nct6775_data *data = platform_get_drvdata(pdev);
3640 hwmon_device_unregister(data->hwmon_dev);
3641 nct6775_device_remove_files(&pdev->dev);
3642 release_region(data->addr, IOREGION_LENGTH);
3647 static struct platform_driver nct6775_driver = {
3649 .owner = THIS_MODULE,
3652 .probe = nct6775_probe,
3653 .remove = __devexit_p(nct6775_remove),
3656 /* nct6775_find() looks for a '627 in the Super-I/O config space */
3657 static int __init nct6775_find(int sioaddr, unsigned short *addr,
3658 struct nct6775_sio_data *sio_data)
3660 static const char __initdata sio_name_NCT6775[] = "NCT6775F";
3661 static const char __initdata sio_name_NCT6776[] = "NCT6776F";
3662 static const char __initdata sio_name_NCT6779[] = "NCT6779D";
3665 const char *sio_name;
3667 superio_enter(sioaddr);
3672 val = (superio_inb(sioaddr, SIO_REG_DEVID) << 8)
3673 | superio_inb(sioaddr, SIO_REG_DEVID + 1);
3674 switch (val & SIO_ID_MASK) {
3675 case SIO_NCT6775_ID:
3676 sio_data->kind = nct6775;
3677 sio_name = sio_name_NCT6775;
3679 case SIO_NCT6776_ID:
3680 sio_data->kind = nct6776;
3681 sio_name = sio_name_NCT6776;
3683 case SIO_NCT6779_ID:
3684 sio_data->kind = nct6779;
3685 sio_name = sio_name_NCT6779;
3689 pr_debug("unsupported chip ID: 0x%04x\n", val);
3690 superio_exit(sioaddr);
3694 /* We have a known chip, find the HWM I/O address */
3695 superio_select(sioaddr, NCT6775_LD_HWM);
3696 val = (superio_inb(sioaddr, SIO_REG_ADDR) << 8)
3697 | superio_inb(sioaddr, SIO_REG_ADDR + 1);
3698 *addr = val & IOREGION_ALIGNMENT;
3700 pr_err("Refusing to enable a Super-I/O device with a base I/O port 0\n");
3701 superio_exit(sioaddr);
3705 /* Activate logical device if needed */
3706 val = superio_inb(sioaddr, SIO_REG_ENABLE);
3707 if (!(val & 0x01)) {
3708 pr_warn("Forcibly enabling Super-I/O. Sensor is probably unusable.\n");
3709 superio_outb(sioaddr, SIO_REG_ENABLE, val | 0x01);
3712 superio_exit(sioaddr);
3713 pr_info("Found %s chip at %#x\n", sio_name, *addr);
3714 sio_data->sioreg = sioaddr;
3720 * when Super-I/O functions move to a separate file, the Super-I/O
3721 * bus will manage the lifetime of the device and this module will only keep
3722 * track of the nct6775 driver. But since we platform_device_alloc(), we
3723 * must keep track of the device
3725 static struct platform_device *pdev;
3727 static int __init sensors_nct6775_init(void)
3730 unsigned short address;
3731 struct resource res;
3732 struct nct6775_sio_data sio_data;
3735 * initialize sio_data->kind and sio_data->sioreg.
3737 * when Super-I/O functions move to a separate file, the Super-I/O
3738 * driver will probe 0x2e and 0x4e and auto-detect the presence of a
3739 * nct6775 hardware monitor, and call probe()
3741 if (nct6775_find(0x2e, &address, &sio_data) &&
3742 nct6775_find(0x4e, &address, &sio_data))
3745 err = platform_driver_register(&nct6775_driver);
3749 pdev = platform_device_alloc(DRVNAME, address);
3752 pr_err("Device allocation failed\n");
3753 goto exit_unregister;
3756 err = platform_device_add_data(pdev, &sio_data,
3757 sizeof(struct nct6775_sio_data));
3759 pr_err("Platform data allocation failed\n");
3760 goto exit_device_put;
3763 memset(&res, 0, sizeof(res));
3765 res.start = address + IOREGION_OFFSET;
3766 res.end = address + IOREGION_OFFSET + IOREGION_LENGTH - 1;
3767 res.flags = IORESOURCE_IO;
3769 err = acpi_check_resource_conflict(&res);
3771 goto exit_device_put;
3773 err = platform_device_add_resources(pdev, &res, 1);
3775 pr_err("Device resource addition failed (%d)\n", err);
3776 goto exit_device_put;
3779 /* platform_device_add calls probe() */
3780 err = platform_device_add(pdev);
3782 pr_err("Device addition failed (%d)\n", err);
3783 goto exit_device_put;
3789 platform_device_put(pdev);
3791 platform_driver_unregister(&nct6775_driver);
3796 static void __exit sensors_nct6775_exit(void)
3798 platform_device_unregister(pdev);
3799 platform_driver_unregister(&nct6775_driver);
3802 MODULE_AUTHOR("Guenter Roeck <linux@roeck-us.net>");
3803 MODULE_DESCRIPTION("NCT6775F/NCT6776F/NCT6779D driver");
3804 MODULE_LICENSE("GPL");
3806 module_init(sensors_nct6775_init);
3807 module_exit(sensors_nct6775_exit);