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-2012 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+6 0xc560 0xc1 0x5ca3
40 * #temp lists the number of monitored temperature sources (first value) plus
41 * the number of directly connectable temperature sensors (second value).
44 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
46 #include <linux/module.h>
47 #include <linux/init.h>
48 #include <linux/slab.h>
49 #include <linux/jiffies.h>
50 #include <linux/platform_device.h>
51 #include <linux/hwmon.h>
52 #include <linux/hwmon-sysfs.h>
53 #include <linux/hwmon-vid.h>
54 #include <linux/err.h>
55 #include <linux/mutex.h>
56 #include <linux/acpi.h>
63 enum kinds { nct6775, nct6776, nct6779 };
65 /* used to set data->name = nct6775_device_names[data->sio_kind] */
66 static const char * const nct6775_device_names[] = {
72 static unsigned short force_id;
73 module_param(force_id, ushort, 0);
74 MODULE_PARM_DESC(force_id, "Override the detected device ID");
76 static unsigned short fan_debounce;
77 module_param(fan_debounce, ushort, 0);
78 MODULE_PARM_DESC(fan_debounce, "Enable debouncing for fan RPM signal");
80 #define DRVNAME "nct6775"
83 * Super-I/O constants and functions
86 #define NCT6775_LD_ACPI 0x0a
87 #define NCT6775_LD_HWM 0x0b
88 #define NCT6775_LD_VID 0x0d
90 #define SIO_REG_LDSEL 0x07 /* Logical device select */
91 #define SIO_REG_DEVID 0x20 /* Device ID (2 bytes) */
92 #define SIO_REG_ENABLE 0x30 /* Logical device enable */
93 #define SIO_REG_ADDR 0x60 /* Logical device address (2 bytes) */
95 #define SIO_NCT6775_ID 0xb470
96 #define SIO_NCT6776_ID 0xc330
97 #define SIO_NCT6779_ID 0xc560
98 #define SIO_ID_MASK 0xFFF0
100 enum pwm_enable { off, manual, thermal_cruise, speed_cruise, sf3, sf4 };
103 superio_outb(int ioreg, int reg, int val)
106 outb(val, ioreg + 1);
110 superio_inb(int ioreg, int reg)
113 return inb(ioreg + 1);
117 superio_select(int ioreg, int ld)
119 outb(SIO_REG_LDSEL, ioreg);
124 superio_enter(int ioreg)
127 * Try to reserve <ioreg> and <ioreg + 1> for exclusive access.
129 if (!request_muxed_region(ioreg, 2, DRVNAME))
139 superio_exit(int ioreg)
143 outb(0x02, ioreg + 1);
144 release_region(ioreg, 2);
151 #define IOREGION_ALIGNMENT (~7)
152 #define IOREGION_OFFSET 5
153 #define IOREGION_LENGTH 2
154 #define ADDR_REG_OFFSET 0
155 #define DATA_REG_OFFSET 1
157 #define NCT6775_REG_BANK 0x4E
158 #define NCT6775_REG_CONFIG 0x40
161 * Not currently used:
162 * REG_MAN_ID has the value 0x5ca3 for all supported chips.
163 * REG_CHIP_ID == 0x88/0xa1/0xc1 depending on chip model.
164 * REG_MAN_ID is at port 0x4f
165 * REG_CHIP_ID is at port 0x58
168 #define NUM_TEMP 10 /* Max number of temp attribute sets w/ limits*/
169 #define NUM_TEMP_FIXED 6 /* Max number of fixed temp attribute sets */
171 #define NUM_REG_ALARM 4 /* Max number of alarm registers */
173 /* Common and NCT6775 specific data */
175 /* Voltage min/max registers for nr=7..14 are in bank 5 */
177 static const u16 NCT6775_REG_IN_MAX[] = {
178 0x2b, 0x2d, 0x2f, 0x31, 0x33, 0x35, 0x37, 0x554, 0x556, 0x558, 0x55a,
179 0x55c, 0x55e, 0x560, 0x562 };
180 static const u16 NCT6775_REG_IN_MIN[] = {
181 0x2c, 0x2e, 0x30, 0x32, 0x34, 0x36, 0x38, 0x555, 0x557, 0x559, 0x55b,
182 0x55d, 0x55f, 0x561, 0x563 };
183 static const u16 NCT6775_REG_IN[] = {
184 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x550, 0x551, 0x552
187 #define NCT6775_REG_VBAT 0x5D
188 #define NCT6775_REG_DIODE 0x5E
190 #define NCT6775_REG_FANDIV1 0x506
191 #define NCT6775_REG_FANDIV2 0x507
193 #define NCT6775_REG_CR_FAN_DEBOUNCE 0xf0
195 static const u16 NCT6775_REG_ALARM[NUM_REG_ALARM] = { 0x459, 0x45A, 0x45B };
197 /* 0..15 voltages, 16..23 fans, 24..31 temperatures */
199 static const s8 NCT6775_ALARM_BITS[] = {
200 0, 1, 2, 3, 8, 21, 20, 16, /* in0.. in7 */
201 17, -1, -1, -1, -1, -1, -1, /* in8..in14 */
203 6, 7, 11, 10, 23, /* fan1..fan5 */
204 -1, -1, -1, /* unused */
205 4, 5, 13, -1, -1, -1, /* temp1..temp6 */
206 12, -1 }; /* intrusion0, intrusion1 */
208 #define FAN_ALARM_BASE 16
209 #define TEMP_ALARM_BASE 24
210 #define INTRUSION_ALARM_BASE 30
212 static const u8 NCT6775_REG_CR_CASEOPEN_CLR[] = { 0xe6, 0xee };
213 static const u8 NCT6775_CR_CASEOPEN_CLR_MASK[] = { 0x20, 0x01 };
215 /* DC or PWM output fan configuration */
216 static const u8 NCT6775_REG_PWM_MODE[] = { 0x04, 0x04, 0x12 };
217 static const u8 NCT6775_PWM_MODE_MASK[] = { 0x01, 0x02, 0x01 };
219 /* Advanced Fan control, some values are common for all fans */
221 static const u16 NCT6775_REG_TARGET[] = { 0x101, 0x201, 0x301, 0x801, 0x901 };
222 static const u16 NCT6775_REG_FAN_MODE[] = { 0x102, 0x202, 0x302, 0x802, 0x902 };
223 static const u16 NCT6775_REG_FAN_STEP_DOWN_TIME[] = {
224 0x103, 0x203, 0x303, 0x803, 0x903 };
225 static const u16 NCT6775_REG_FAN_STEP_UP_TIME[] = {
226 0x104, 0x204, 0x304, 0x804, 0x904 };
227 static const u16 NCT6775_REG_FAN_STOP_OUTPUT[] = {
228 0x105, 0x205, 0x305, 0x805, 0x905 };
229 static const u16 NCT6775_REG_FAN_START_OUTPUT[]
230 = { 0x106, 0x206, 0x306, 0x806, 0x906 };
231 static const u16 NCT6775_REG_FAN_MAX_OUTPUT[] = { 0x10a, 0x20a, 0x30a };
232 static const u16 NCT6775_REG_FAN_STEP_OUTPUT[] = { 0x10b, 0x20b, 0x30b };
234 static const u16 NCT6775_REG_FAN_STOP_TIME[] = {
235 0x107, 0x207, 0x307, 0x807, 0x907 };
236 static const u16 NCT6775_REG_PWM[] = { 0x109, 0x209, 0x309, 0x809, 0x909 };
237 static const u16 NCT6775_REG_PWM_READ[] = { 0x01, 0x03, 0x11, 0x13, 0x15 };
239 static const u16 NCT6775_REG_FAN[] = { 0x630, 0x632, 0x634, 0x636, 0x638 };
240 static const u16 NCT6775_REG_FAN_MIN[] = { 0x3b, 0x3c, 0x3d };
241 static const u16 NCT6775_REG_FAN_PULSES[] = { 0x641, 0x642, 0x643, 0x644, 0 };
243 static const u16 NCT6775_REG_TEMP[] = {
244 0x27, 0x150, 0x250, 0x62b, 0x62c, 0x62d };
246 static const u16 NCT6775_REG_TEMP_CONFIG[ARRAY_SIZE(NCT6775_REG_TEMP)] = {
247 0, 0x152, 0x252, 0x628, 0x629, 0x62A };
248 static const u16 NCT6775_REG_TEMP_HYST[ARRAY_SIZE(NCT6775_REG_TEMP)] = {
249 0x3a, 0x153, 0x253, 0x673, 0x678, 0x67D };
250 static const u16 NCT6775_REG_TEMP_OVER[ARRAY_SIZE(NCT6775_REG_TEMP)] = {
251 0x39, 0x155, 0x255, 0x672, 0x677, 0x67C };
253 static const u16 NCT6775_REG_TEMP_SOURCE[ARRAY_SIZE(NCT6775_REG_TEMP)] = {
254 0x621, 0x622, 0x623, 0x624, 0x625, 0x626 };
256 static const u16 NCT6775_REG_TEMP_SEL[] = {
257 0x100, 0x200, 0x300, 0x800, 0x900 };
259 static const u16 NCT6775_REG_WEIGHT_TEMP_SEL[] = {
260 0x139, 0x239, 0x339, 0x839, 0x939 };
261 static const u16 NCT6775_REG_WEIGHT_TEMP_STEP[] = {
262 0x13a, 0x23a, 0x33a, 0x83a, 0x93a };
263 static const u16 NCT6775_REG_WEIGHT_TEMP_STEP_TOL[] = {
264 0x13b, 0x23b, 0x33b, 0x83b, 0x93b };
265 static const u16 NCT6775_REG_WEIGHT_DUTY_STEP[] = {
266 0x13c, 0x23c, 0x33c, 0x83c, 0x93c };
267 static const u16 NCT6775_REG_WEIGHT_TEMP_BASE[] = {
268 0x13d, 0x23d, 0x33d, 0x83d, 0x93d };
270 static const u16 NCT6775_REG_TEMP_OFFSET[] = { 0x454, 0x455, 0x456 };
272 static const u16 NCT6775_REG_AUTO_TEMP[] = {
273 0x121, 0x221, 0x321, 0x821, 0x921 };
274 static const u16 NCT6775_REG_AUTO_PWM[] = {
275 0x127, 0x227, 0x327, 0x827, 0x927 };
277 #define NCT6775_AUTO_TEMP(data, nr, p) ((data)->REG_AUTO_TEMP[nr] + (p))
278 #define NCT6775_AUTO_PWM(data, nr, p) ((data)->REG_AUTO_PWM[nr] + (p))
280 static const u16 NCT6775_REG_CRITICAL_ENAB[] = { 0x134, 0x234, 0x334 };
282 static const u16 NCT6775_REG_CRITICAL_TEMP[] = {
283 0x135, 0x235, 0x335, 0x835, 0x935 };
284 static const u16 NCT6775_REG_CRITICAL_TEMP_TOLERANCE[] = {
285 0x138, 0x238, 0x338, 0x838, 0x938 };
287 static const char *const nct6775_temp_label[] = {
301 "PCH_CHIP_CPU_MAX_TEMP",
311 static const u16 NCT6775_REG_TEMP_ALTERNATE[ARRAY_SIZE(nct6775_temp_label) - 1]
312 = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0x661, 0x662, 0x664 };
314 static const u16 NCT6775_REG_TEMP_CRIT[ARRAY_SIZE(nct6775_temp_label) - 1]
315 = { 0, 0, 0, 0, 0xa00, 0xa01, 0xa02, 0xa03, 0xa04, 0xa05, 0xa06,
318 /* NCT6776 specific data */
320 static const s8 NCT6776_ALARM_BITS[] = {
321 0, 1, 2, 3, 8, 21, 20, 16, /* in0.. in7 */
322 17, -1, -1, -1, -1, -1, -1, /* in8..in14 */
324 6, 7, 11, 10, 23, /* fan1..fan5 */
325 -1, -1, -1, /* unused */
326 4, 5, 13, -1, -1, -1, /* temp1..temp6 */
327 12, 9 }; /* intrusion0, intrusion1 */
329 static const u16 NCT6776_REG_TOLERANCE_H[] = {
330 0x10c, 0x20c, 0x30c, 0x80c, 0x90c };
332 static const u8 NCT6776_REG_PWM_MODE[] = { 0x04, 0, 0 };
333 static const u8 NCT6776_PWM_MODE_MASK[] = { 0x01, 0, 0 };
335 static const u16 NCT6776_REG_FAN_MIN[] = { 0x63a, 0x63c, 0x63e, 0x640, 0x642 };
336 static const u16 NCT6776_REG_FAN_PULSES[] = { 0x644, 0x645, 0x646, 0, 0 };
338 static const u16 NCT6776_REG_WEIGHT_DUTY_BASE[] = {
339 0x13e, 0x23e, 0x33e, 0x83e, 0x93e };
341 static const u16 NCT6776_REG_TEMP_CONFIG[ARRAY_SIZE(NCT6775_REG_TEMP)] = {
342 0x18, 0x152, 0x252, 0x628, 0x629, 0x62A };
344 static const char *const nct6776_temp_label[] = {
359 "PCH_CHIP_CPU_MAX_TEMP",
370 static const u16 NCT6776_REG_TEMP_ALTERNATE[ARRAY_SIZE(nct6776_temp_label) - 1]
371 = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0x401, 0x402, 0x404 };
373 static const u16 NCT6776_REG_TEMP_CRIT[ARRAY_SIZE(nct6776_temp_label) - 1]
374 = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0x709, 0x70a };
376 /* NCT6779 specific data */
378 static const u16 NCT6779_REG_IN[] = {
379 0x480, 0x481, 0x482, 0x483, 0x484, 0x485, 0x486, 0x487,
380 0x488, 0x489, 0x48a, 0x48b, 0x48c, 0x48d, 0x48e };
382 static const u16 NCT6779_REG_ALARM[NUM_REG_ALARM] = {
383 0x459, 0x45A, 0x45B, 0x568 };
385 static const s8 NCT6779_ALARM_BITS[] = {
386 0, 1, 2, 3, 8, 21, 20, 16, /* in0.. in7 */
387 17, 24, 25, 26, 27, 28, 29, /* in8..in14 */
389 6, 7, 11, 10, 23, /* fan1..fan5 */
390 -1, -1, -1, /* unused */
391 4, 5, 13, -1, -1, -1, /* temp1..temp6 */
392 12, 9 }; /* intrusion0, intrusion1 */
394 static const u16 NCT6779_REG_FAN[] = { 0x4b0, 0x4b2, 0x4b4, 0x4b6, 0x4b8 };
395 static const u16 NCT6779_REG_FAN_PULSES[] = {
396 0x644, 0x645, 0x646, 0x647, 0x648 };
398 static const u16 NCT6779_REG_CRITICAL_PWM_ENABLE[] = {
399 0x136, 0x236, 0x336, 0x836, 0x936 };
400 static const u16 NCT6779_REG_CRITICAL_PWM[] = {
401 0x137, 0x237, 0x337, 0x837, 0x937 };
403 static const u16 NCT6779_REG_TEMP[] = { 0x27, 0x150 };
404 static const u16 NCT6779_REG_TEMP_CONFIG[ARRAY_SIZE(NCT6779_REG_TEMP)] = {
406 static const u16 NCT6779_REG_TEMP_HYST[ARRAY_SIZE(NCT6779_REG_TEMP)] = {
408 static const u16 NCT6779_REG_TEMP_OVER[ARRAY_SIZE(NCT6779_REG_TEMP)] = {
411 static const u16 NCT6779_REG_TEMP_OFFSET[] = {
412 0x454, 0x455, 0x456, 0x44a, 0x44b, 0x44c };
414 static const char *const nct6779_temp_label[] = {
433 "PCH_CHIP_CPU_MAX_TEMP",
444 static const u16 NCT6779_REG_TEMP_ALTERNATE[ARRAY_SIZE(nct6779_temp_label) - 1]
445 = { 0x490, 0x491, 0x492, 0x493, 0x494, 0x495, 0, 0,
446 0, 0, 0, 0, 0, 0, 0, 0,
447 0, 0x400, 0x401, 0x402, 0x404, 0x405, 0x406, 0x407,
450 static const u16 NCT6779_REG_TEMP_CRIT[ARRAY_SIZE(nct6779_temp_label) - 1]
451 = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0x709, 0x70a };
453 static enum pwm_enable reg_to_pwm_enable(int pwm, int mode)
455 if (mode == 0 && pwm == 255)
460 static int pwm_enable_to_reg(enum pwm_enable mode)
471 /* 1 is DC mode, output in ms */
472 static unsigned int step_time_from_reg(u8 reg, u8 mode)
474 return mode ? 400 * reg : 100 * reg;
477 static u8 step_time_to_reg(unsigned int msec, u8 mode)
479 return clamp_val((mode ? (msec + 200) / 400 :
480 (msec + 50) / 100), 1, 255);
483 static unsigned int fan_from_reg8(u16 reg, unsigned int divreg)
485 if (reg == 0 || reg == 255)
487 return 1350000U / (reg << divreg);
490 static unsigned int fan_from_reg13(u16 reg, unsigned int divreg)
492 if ((reg & 0xff1f) == 0xff1f)
495 reg = (reg & 0x1f) | ((reg & 0xff00) >> 3);
500 return 1350000U / reg;
503 static unsigned int fan_from_reg16(u16 reg, unsigned int divreg)
505 if (reg == 0 || reg == 0xffff)
509 * Even though the registers are 16 bit wide, the fan divisor
512 return 1350000U / (reg << divreg);
515 static u16 fan_to_reg(u32 fan, unsigned int divreg)
520 return (1350000U / fan) >> divreg;
523 static inline unsigned int
530 * Some of the voltage inputs have internal scaling, the tables below
531 * contain 8 (the ADC LSB in mV) * scaling factor * 100
533 static const u16 scale_in[15] = {
534 800, 800, 1600, 1600, 800, 800, 800, 1600, 1600, 800, 800, 800, 800,
538 static inline long in_from_reg(u8 reg, u8 nr)
540 return DIV_ROUND_CLOSEST(reg * scale_in[nr], 100);
543 static inline u8 in_to_reg(u32 val, u8 nr)
545 return clamp_val(DIV_ROUND_CLOSEST(val * 100, scale_in[nr]), 0, 255);
549 * Data structures and manipulation thereof
552 struct nct6775_data {
553 int addr; /* IO base of hw monitor block */
557 struct device *hwmon_dev;
560 u16 reg_temp[4][NUM_TEMP]; /* 0=temp, 1=temp_over, 2=temp_hyst,
563 u8 temp_src[NUM_TEMP];
564 u16 reg_temp_config[NUM_TEMP];
565 const char * const *temp_label;
572 const s8 *ALARM_BITS;
575 const u16 *REG_IN_MINMAX[2];
577 const u16 *REG_TARGET;
579 const u16 *REG_FAN_MODE;
580 const u16 *REG_FAN_MIN;
581 const u16 *REG_FAN_PULSES;
582 const u16 *REG_FAN_TIME[3];
584 const u16 *REG_TOLERANCE_H;
586 const u8 *REG_PWM_MODE;
587 const u8 *PWM_MODE_MASK;
589 const u16 *REG_PWM[7]; /* [0]=pwm, [1]=pwm_start, [2]=pwm_floor,
590 * [3]=pwm_max, [4]=pwm_step,
591 * [5]=weight_duty_step, [6]=weight_duty_base
593 const u16 *REG_PWM_READ;
595 const u16 *REG_AUTO_TEMP;
596 const u16 *REG_AUTO_PWM;
598 const u16 *REG_CRITICAL_TEMP;
599 const u16 *REG_CRITICAL_TEMP_TOLERANCE;
601 const u16 *REG_TEMP_SOURCE; /* temp register sources */
602 const u16 *REG_TEMP_SEL;
603 const u16 *REG_WEIGHT_TEMP_SEL;
604 const u16 *REG_WEIGHT_TEMP[3]; /* 0=base, 1=tolerance, 2=step */
606 const u16 *REG_TEMP_OFFSET;
608 const u16 *REG_ALARM;
610 unsigned int (*fan_from_reg)(u16 reg, unsigned int divreg);
611 unsigned int (*fan_from_reg_min)(u16 reg, unsigned int divreg);
613 struct mutex update_lock;
614 bool valid; /* true if following fields are valid */
615 unsigned long last_updated; /* In jiffies */
617 /* Register values */
618 u8 bank; /* current register bank */
619 u8 in_num; /* number of in inputs we have */
620 u8 in[15][3]; /* [0]=in, [1]=in_max, [2]=in_min */
626 u8 has_fan; /* some fan inputs can be disabled */
627 u8 has_fan_min; /* some fans don't have min register */
630 u8 temp_fixed_num; /* 3 or 6 */
631 u8 temp_type[NUM_TEMP_FIXED];
632 s8 temp_offset[NUM_TEMP_FIXED];
633 s16 temp[4][NUM_TEMP]; /* 0=temp, 1=temp_over, 2=temp_hyst,
637 u8 pwm_num; /* number of pwm */
638 u8 pwm_mode[5]; /* 1->DC variable voltage, 0->PWM variable duty cycle */
639 enum pwm_enable pwm_enable[5];
642 * 2->thermal cruise mode (also called SmartFan I)
643 * 3->fan speed cruise mode
645 * 5->enhanced variable thermal cruise (SmartFan IV)
647 u8 pwm[7][5]; /* [0]=pwm, [1]=pwm_start, [2]=pwm_floor,
648 * [3]=pwm_max, [4]=pwm_step,
649 * [5]=weight_duty_step, [6]=weight_duty_base
655 u32 target_speed_tolerance[5];
656 u8 speed_tolerance_limit;
658 u8 temp_tolerance[2][5];
661 u8 fan_time[3][5]; /* 0 = stop_time, 1 = step_up, 2 = step_down */
663 /* Automatic fan speed control registers */
668 u8 pwm_weight_temp_sel[5];
669 u8 weight_temp[3][5]; /* 0->temp_step, 1->temp_step_tol,
680 /* Remember extra register values over suspend/resume */
687 struct nct6775_sio_data {
692 static bool is_word_sized(struct nct6775_data *data, u16 reg)
694 switch (data->kind) {
696 return (((reg & 0xff00) == 0x100 ||
697 (reg & 0xff00) == 0x200) &&
698 ((reg & 0x00ff) == 0x50 ||
699 (reg & 0x00ff) == 0x53 ||
700 (reg & 0x00ff) == 0x55)) ||
701 (reg & 0xfff0) == 0x630 ||
702 reg == 0x640 || reg == 0x642 ||
704 ((reg & 0xfff0) == 0x650 && (reg & 0x000f) >= 0x06) ||
705 reg == 0x73 || reg == 0x75 || reg == 0x77;
707 return (((reg & 0xff00) == 0x100 ||
708 (reg & 0xff00) == 0x200) &&
709 ((reg & 0x00ff) == 0x50 ||
710 (reg & 0x00ff) == 0x53 ||
711 (reg & 0x00ff) == 0x55)) ||
712 (reg & 0xfff0) == 0x630 ||
714 reg == 0x640 || reg == 0x642 ||
715 ((reg & 0xfff0) == 0x650 && (reg & 0x000f) >= 0x06) ||
716 reg == 0x73 || reg == 0x75 || reg == 0x77;
718 return reg == 0x150 || reg == 0x153 || reg == 0x155 ||
719 ((reg & 0xfff0) == 0x4b0 && (reg & 0x000f) < 0x09) ||
721 reg == 0x63a || reg == 0x63c || reg == 0x63e ||
722 reg == 0x640 || reg == 0x642 ||
723 reg == 0x73 || reg == 0x75 || reg == 0x77 || reg == 0x79 ||
730 * On older chips, only registers 0x50-0x5f are banked.
731 * On more recent chips, all registers are banked.
732 * Assume that is the case and set the bank number for each access.
733 * Cache the bank number so it only needs to be set if it changes.
735 static inline void nct6775_set_bank(struct nct6775_data *data, u16 reg)
738 if (data->bank != bank) {
739 outb_p(NCT6775_REG_BANK, data->addr + ADDR_REG_OFFSET);
740 outb_p(bank, data->addr + DATA_REG_OFFSET);
745 static u16 nct6775_read_value(struct nct6775_data *data, u16 reg)
747 int res, word_sized = is_word_sized(data, reg);
749 mutex_lock(&data->lock);
751 nct6775_set_bank(data, reg);
752 outb_p(reg & 0xff, data->addr + ADDR_REG_OFFSET);
753 res = inb_p(data->addr + DATA_REG_OFFSET);
755 outb_p((reg & 0xff) + 1,
756 data->addr + ADDR_REG_OFFSET);
757 res = (res << 8) + inb_p(data->addr + DATA_REG_OFFSET);
760 mutex_unlock(&data->lock);
764 static int nct6775_write_value(struct nct6775_data *data, u16 reg, u16 value)
766 int word_sized = is_word_sized(data, reg);
768 mutex_lock(&data->lock);
770 nct6775_set_bank(data, reg);
771 outb_p(reg & 0xff, data->addr + ADDR_REG_OFFSET);
773 outb_p(value >> 8, data->addr + DATA_REG_OFFSET);
774 outb_p((reg & 0xff) + 1,
775 data->addr + ADDR_REG_OFFSET);
777 outb_p(value & 0xff, data->addr + DATA_REG_OFFSET);
779 mutex_unlock(&data->lock);
783 /* We left-align 8-bit temperature values to make the code simpler */
784 static u16 nct6775_read_temp(struct nct6775_data *data, u16 reg)
788 res = nct6775_read_value(data, reg);
789 if (!is_word_sized(data, reg))
795 static int nct6775_write_temp(struct nct6775_data *data, u16 reg, u16 value)
797 if (!is_word_sized(data, reg))
799 return nct6775_write_value(data, reg, value);
802 /* This function assumes that the caller holds data->update_lock */
803 static void nct6775_write_fan_div(struct nct6775_data *data, int nr)
809 reg = (nct6775_read_value(data, NCT6775_REG_FANDIV1) & 0x70)
810 | (data->fan_div[0] & 0x7);
811 nct6775_write_value(data, NCT6775_REG_FANDIV1, reg);
814 reg = (nct6775_read_value(data, NCT6775_REG_FANDIV1) & 0x7)
815 | ((data->fan_div[1] << 4) & 0x70);
816 nct6775_write_value(data, NCT6775_REG_FANDIV1, reg);
819 reg = (nct6775_read_value(data, NCT6775_REG_FANDIV2) & 0x70)
820 | (data->fan_div[2] & 0x7);
821 nct6775_write_value(data, NCT6775_REG_FANDIV2, reg);
824 reg = (nct6775_read_value(data, NCT6775_REG_FANDIV2) & 0x7)
825 | ((data->fan_div[3] << 4) & 0x70);
826 nct6775_write_value(data, NCT6775_REG_FANDIV2, reg);
831 static void nct6775_write_fan_div_common(struct nct6775_data *data, int nr)
833 if (data->kind == nct6775)
834 nct6775_write_fan_div(data, nr);
837 static void nct6775_update_fan_div(struct nct6775_data *data)
841 i = nct6775_read_value(data, NCT6775_REG_FANDIV1);
842 data->fan_div[0] = i & 0x7;
843 data->fan_div[1] = (i & 0x70) >> 4;
844 i = nct6775_read_value(data, NCT6775_REG_FANDIV2);
845 data->fan_div[2] = i & 0x7;
846 if (data->has_fan & (1<<3))
847 data->fan_div[3] = (i & 0x70) >> 4;
850 static void nct6775_update_fan_div_common(struct nct6775_data *data)
852 if (data->kind == nct6775)
853 nct6775_update_fan_div(data);
856 static void nct6775_init_fan_div(struct nct6775_data *data)
860 nct6775_update_fan_div_common(data);
862 * For all fans, start with highest divider value if the divider
863 * register is not initialized. This ensures that we get a
864 * reading from the fan count register, even if it is not optimal.
865 * We'll compute a better divider later on.
867 for (i = 0; i < 3; i++) {
868 if (!(data->has_fan & (1 << i)))
870 if (data->fan_div[i] == 0) {
871 data->fan_div[i] = 7;
872 nct6775_write_fan_div_common(data, i);
877 static void nct6775_init_fan_common(struct device *dev,
878 struct nct6775_data *data)
883 if (data->has_fan_div)
884 nct6775_init_fan_div(data);
887 * If fan_min is not set (0), set it to 0xff to disable it. This
888 * prevents the unnecessary warning when fanX_min is reported as 0.
890 for (i = 0; i < 5; i++) {
891 if (data->has_fan_min & (1 << i)) {
892 reg = nct6775_read_value(data, data->REG_FAN_MIN[i]);
894 nct6775_write_value(data, data->REG_FAN_MIN[i],
895 data->has_fan_div ? 0xff
901 static void nct6775_select_fan_div(struct device *dev,
902 struct nct6775_data *data, int nr, u16 reg)
904 u8 fan_div = data->fan_div[nr];
907 if (!data->has_fan_div)
911 * If we failed to measure the fan speed, or the reported value is not
912 * in the optimal range, and the clock divider can be modified,
913 * let's try that for next time.
915 if (reg == 0x00 && fan_div < 0x07)
917 else if (reg != 0x00 && reg < 0x30 && fan_div > 0)
920 if (fan_div != data->fan_div[nr]) {
921 dev_dbg(dev, "Modifying fan%d clock divider from %u to %u\n",
922 nr + 1, div_from_reg(data->fan_div[nr]),
923 div_from_reg(fan_div));
925 /* Preserve min limit if possible */
926 if (data->has_fan_min & (1 << nr)) {
927 fan_min = data->fan_min[nr];
928 if (fan_div > data->fan_div[nr]) {
929 if (fan_min != 255 && fan_min > 1)
932 if (fan_min != 255) {
938 if (fan_min != data->fan_min[nr]) {
939 data->fan_min[nr] = fan_min;
940 nct6775_write_value(data, data->REG_FAN_MIN[nr],
944 data->fan_div[nr] = fan_div;
945 nct6775_write_fan_div_common(data, nr);
949 static void nct6775_update_pwm(struct device *dev)
951 struct nct6775_data *data = dev_get_drvdata(dev);
956 for (i = 0; i < data->pwm_num; i++) {
957 if (!(data->has_pwm & (1 << i)))
960 duty_is_dc = data->REG_PWM_MODE[i] &&
961 (nct6775_read_value(data, data->REG_PWM_MODE[i])
962 & data->PWM_MODE_MASK[i]);
963 data->pwm_mode[i] = duty_is_dc;
965 fanmodecfg = nct6775_read_value(data, data->REG_FAN_MODE[i]);
966 for (j = 0; j < ARRAY_SIZE(data->REG_PWM); j++) {
967 if (data->REG_PWM[j] && data->REG_PWM[j][i]) {
969 = nct6775_read_value(data,
970 data->REG_PWM[j][i]);
974 data->pwm_enable[i] = reg_to_pwm_enable(data->pwm[0][i],
975 (fanmodecfg >> 4) & 7);
977 if (!data->temp_tolerance[0][i] ||
978 data->pwm_enable[i] != speed_cruise)
979 data->temp_tolerance[0][i] = fanmodecfg & 0x0f;
980 if (!data->target_speed_tolerance[i] ||
981 data->pwm_enable[i] == speed_cruise) {
982 u8 t = fanmodecfg & 0x0f;
983 if (data->REG_TOLERANCE_H) {
984 t |= (nct6775_read_value(data,
985 data->REG_TOLERANCE_H[i]) & 0x70) >> 1;
987 data->target_speed_tolerance[i] = t;
990 data->temp_tolerance[1][i] =
991 nct6775_read_value(data,
992 data->REG_CRITICAL_TEMP_TOLERANCE[i]);
994 reg = nct6775_read_value(data, data->REG_TEMP_SEL[i]);
995 data->pwm_temp_sel[i] = reg & 0x1f;
996 /* If fan can stop, report floor as 0 */
1000 reg = nct6775_read_value(data, data->REG_WEIGHT_TEMP_SEL[i]);
1001 data->pwm_weight_temp_sel[i] = reg & 0x1f;
1002 /* If weight is disabled, report weight source as 0 */
1003 if (j == 1 && !(reg & 0x80))
1004 data->pwm_weight_temp_sel[i] = 0;
1006 /* Weight temp data */
1007 for (j = 0; j < 3; j++) {
1008 data->weight_temp[j][i]
1009 = nct6775_read_value(data,
1010 data->REG_WEIGHT_TEMP[j][i]);
1015 static void nct6775_update_pwm_limits(struct device *dev)
1017 struct nct6775_data *data = dev_get_drvdata(dev);
1022 for (i = 0; i < data->pwm_num; i++) {
1023 if (!(data->has_pwm & (1 << i)))
1026 for (j = 0; j < 3; j++) {
1027 data->fan_time[j][i] =
1028 nct6775_read_value(data, data->REG_FAN_TIME[j][i]);
1031 reg_t = nct6775_read_value(data, data->REG_TARGET[i]);
1032 /* Update only in matching mode or if never updated */
1033 if (!data->target_temp[i] ||
1034 data->pwm_enable[i] == thermal_cruise)
1035 data->target_temp[i] = reg_t & data->target_temp_mask;
1036 if (!data->target_speed[i] ||
1037 data->pwm_enable[i] == speed_cruise) {
1038 if (data->REG_TOLERANCE_H) {
1039 reg_t |= (nct6775_read_value(data,
1040 data->REG_TOLERANCE_H[i]) & 0x0f) << 8;
1042 data->target_speed[i] = reg_t;
1045 for (j = 0; j < data->auto_pwm_num; j++) {
1046 data->auto_pwm[i][j] =
1047 nct6775_read_value(data,
1048 NCT6775_AUTO_PWM(data, i, j));
1049 data->auto_temp[i][j] =
1050 nct6775_read_value(data,
1051 NCT6775_AUTO_TEMP(data, i, j));
1054 /* critical auto_pwm temperature data */
1055 data->auto_temp[i][data->auto_pwm_num] =
1056 nct6775_read_value(data, data->REG_CRITICAL_TEMP[i]);
1058 switch (data->kind) {
1060 reg = nct6775_read_value(data,
1061 NCT6775_REG_CRITICAL_ENAB[i]);
1062 data->auto_pwm[i][data->auto_pwm_num] =
1063 (reg & 0x02) ? 0xff : 0x00;
1066 data->auto_pwm[i][data->auto_pwm_num] = 0xff;
1069 reg = nct6775_read_value(data,
1070 NCT6779_REG_CRITICAL_PWM_ENABLE[i]);
1072 data->auto_pwm[i][data->auto_pwm_num] =
1073 nct6775_read_value(data,
1074 NCT6779_REG_CRITICAL_PWM[i]);
1076 data->auto_pwm[i][data->auto_pwm_num] = 0xff;
1082 static struct nct6775_data *nct6775_update_device(struct device *dev)
1084 struct nct6775_data *data = dev_get_drvdata(dev);
1087 mutex_lock(&data->update_lock);
1089 if (time_after(jiffies, data->last_updated + HZ + HZ/2)
1091 /* Fan clock dividers */
1092 nct6775_update_fan_div_common(data);
1094 /* Measured voltages and limits */
1095 for (i = 0; i < data->in_num; i++) {
1096 if (!(data->have_in & (1 << i)))
1099 data->in[i][0] = nct6775_read_value(data,
1101 data->in[i][1] = nct6775_read_value(data,
1102 data->REG_IN_MINMAX[0][i]);
1103 data->in[i][2] = nct6775_read_value(data,
1104 data->REG_IN_MINMAX[1][i]);
1107 /* Measured fan speeds and limits */
1108 for (i = 0; i < 5; i++) {
1111 if (!(data->has_fan & (1 << i)))
1114 reg = nct6775_read_value(data, data->REG_FAN[i]);
1115 data->rpm[i] = data->fan_from_reg(reg,
1118 if (data->has_fan_min & (1 << i))
1119 data->fan_min[i] = nct6775_read_value(data,
1120 data->REG_FAN_MIN[i]);
1121 data->fan_pulses[i] =
1122 nct6775_read_value(data, data->REG_FAN_PULSES[i]);
1124 nct6775_select_fan_div(dev, data, i, reg);
1127 nct6775_update_pwm(dev);
1128 nct6775_update_pwm_limits(dev);
1130 /* Measured temperatures and limits */
1131 for (i = 0; i < NUM_TEMP; i++) {
1132 if (!(data->have_temp & (1 << i)))
1134 for (j = 0; j < 4; j++) {
1135 if (data->reg_temp[j][i])
1137 = nct6775_read_temp(data,
1138 data->reg_temp[j][i]);
1140 if (!(data->have_temp_fixed & (1 << i)))
1142 data->temp_offset[i]
1143 = nct6775_read_value(data, data->REG_TEMP_OFFSET[i]);
1147 for (i = 0; i < NUM_REG_ALARM; i++) {
1149 if (!data->REG_ALARM[i])
1151 alarm = nct6775_read_value(data, data->REG_ALARM[i]);
1152 data->alarms |= ((u64)alarm) << (i << 3);
1155 data->last_updated = jiffies;
1159 mutex_unlock(&data->update_lock);
1164 * Sysfs callback functions
1167 show_in_reg(struct device *dev, struct device_attribute *attr, char *buf)
1169 struct nct6775_data *data = nct6775_update_device(dev);
1170 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
1172 int index = sattr->index;
1173 return sprintf(buf, "%ld\n", in_from_reg(data->in[nr][index], nr));
1177 store_in_reg(struct device *dev, struct device_attribute *attr, const char *buf,
1180 struct nct6775_data *data = dev_get_drvdata(dev);
1181 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
1183 int index = sattr->index;
1185 int err = kstrtoul(buf, 10, &val);
1188 mutex_lock(&data->update_lock);
1189 data->in[nr][index] = in_to_reg(val, nr);
1190 nct6775_write_value(data, data->REG_IN_MINMAX[index-1][nr],
1191 data->in[nr][index]);
1192 mutex_unlock(&data->update_lock);
1197 show_alarm(struct device *dev, struct device_attribute *attr, char *buf)
1199 struct nct6775_data *data = nct6775_update_device(dev);
1200 struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr);
1201 int nr = data->ALARM_BITS[sattr->index];
1202 return sprintf(buf, "%u\n",
1203 (unsigned int)((data->alarms >> nr) & 0x01));
1206 static SENSOR_DEVICE_ATTR_2(in0_input, S_IRUGO, show_in_reg, NULL, 0, 0);
1207 static SENSOR_DEVICE_ATTR_2(in1_input, S_IRUGO, show_in_reg, NULL, 1, 0);
1208 static SENSOR_DEVICE_ATTR_2(in2_input, S_IRUGO, show_in_reg, NULL, 2, 0);
1209 static SENSOR_DEVICE_ATTR_2(in3_input, S_IRUGO, show_in_reg, NULL, 3, 0);
1210 static SENSOR_DEVICE_ATTR_2(in4_input, S_IRUGO, show_in_reg, NULL, 4, 0);
1211 static SENSOR_DEVICE_ATTR_2(in5_input, S_IRUGO, show_in_reg, NULL, 5, 0);
1212 static SENSOR_DEVICE_ATTR_2(in6_input, S_IRUGO, show_in_reg, NULL, 6, 0);
1213 static SENSOR_DEVICE_ATTR_2(in7_input, S_IRUGO, show_in_reg, NULL, 7, 0);
1214 static SENSOR_DEVICE_ATTR_2(in8_input, S_IRUGO, show_in_reg, NULL, 8, 0);
1215 static SENSOR_DEVICE_ATTR_2(in9_input, S_IRUGO, show_in_reg, NULL, 9, 0);
1216 static SENSOR_DEVICE_ATTR_2(in10_input, S_IRUGO, show_in_reg, NULL, 10, 0);
1217 static SENSOR_DEVICE_ATTR_2(in11_input, S_IRUGO, show_in_reg, NULL, 11, 0);
1218 static SENSOR_DEVICE_ATTR_2(in12_input, S_IRUGO, show_in_reg, NULL, 12, 0);
1219 static SENSOR_DEVICE_ATTR_2(in13_input, S_IRUGO, show_in_reg, NULL, 13, 0);
1220 static SENSOR_DEVICE_ATTR_2(in14_input, S_IRUGO, show_in_reg, NULL, 14, 0);
1222 static SENSOR_DEVICE_ATTR(in0_alarm, S_IRUGO, show_alarm, NULL, 0);
1223 static SENSOR_DEVICE_ATTR(in1_alarm, S_IRUGO, show_alarm, NULL, 1);
1224 static SENSOR_DEVICE_ATTR(in2_alarm, S_IRUGO, show_alarm, NULL, 2);
1225 static SENSOR_DEVICE_ATTR(in3_alarm, S_IRUGO, show_alarm, NULL, 3);
1226 static SENSOR_DEVICE_ATTR(in4_alarm, S_IRUGO, show_alarm, NULL, 4);
1227 static SENSOR_DEVICE_ATTR(in5_alarm, S_IRUGO, show_alarm, NULL, 5);
1228 static SENSOR_DEVICE_ATTR(in6_alarm, S_IRUGO, show_alarm, NULL, 6);
1229 static SENSOR_DEVICE_ATTR(in7_alarm, S_IRUGO, show_alarm, NULL, 7);
1230 static SENSOR_DEVICE_ATTR(in8_alarm, S_IRUGO, show_alarm, NULL, 8);
1231 static SENSOR_DEVICE_ATTR(in9_alarm, S_IRUGO, show_alarm, NULL, 9);
1232 static SENSOR_DEVICE_ATTR(in10_alarm, S_IRUGO, show_alarm, NULL, 10);
1233 static SENSOR_DEVICE_ATTR(in11_alarm, S_IRUGO, show_alarm, NULL, 11);
1234 static SENSOR_DEVICE_ATTR(in12_alarm, S_IRUGO, show_alarm, NULL, 12);
1235 static SENSOR_DEVICE_ATTR(in13_alarm, S_IRUGO, show_alarm, NULL, 13);
1236 static SENSOR_DEVICE_ATTR(in14_alarm, S_IRUGO, show_alarm, NULL, 14);
1238 static SENSOR_DEVICE_ATTR_2(in0_min, S_IWUSR | S_IRUGO, show_in_reg,
1239 store_in_reg, 0, 1);
1240 static SENSOR_DEVICE_ATTR_2(in1_min, S_IWUSR | S_IRUGO, show_in_reg,
1241 store_in_reg, 1, 1);
1242 static SENSOR_DEVICE_ATTR_2(in2_min, S_IWUSR | S_IRUGO, show_in_reg,
1243 store_in_reg, 2, 1);
1244 static SENSOR_DEVICE_ATTR_2(in3_min, S_IWUSR | S_IRUGO, show_in_reg,
1245 store_in_reg, 3, 1);
1246 static SENSOR_DEVICE_ATTR_2(in4_min, S_IWUSR | S_IRUGO, show_in_reg,
1247 store_in_reg, 4, 1);
1248 static SENSOR_DEVICE_ATTR_2(in5_min, S_IWUSR | S_IRUGO, show_in_reg,
1249 store_in_reg, 5, 1);
1250 static SENSOR_DEVICE_ATTR_2(in6_min, S_IWUSR | S_IRUGO, show_in_reg,
1251 store_in_reg, 6, 1);
1252 static SENSOR_DEVICE_ATTR_2(in7_min, S_IWUSR | S_IRUGO, show_in_reg,
1253 store_in_reg, 7, 1);
1254 static SENSOR_DEVICE_ATTR_2(in8_min, S_IWUSR | S_IRUGO, show_in_reg,
1255 store_in_reg, 8, 1);
1256 static SENSOR_DEVICE_ATTR_2(in9_min, S_IWUSR | S_IRUGO, show_in_reg,
1257 store_in_reg, 9, 1);
1258 static SENSOR_DEVICE_ATTR_2(in10_min, S_IWUSR | S_IRUGO, show_in_reg,
1259 store_in_reg, 10, 1);
1260 static SENSOR_DEVICE_ATTR_2(in11_min, S_IWUSR | S_IRUGO, show_in_reg,
1261 store_in_reg, 11, 1);
1262 static SENSOR_DEVICE_ATTR_2(in12_min, S_IWUSR | S_IRUGO, show_in_reg,
1263 store_in_reg, 12, 1);
1264 static SENSOR_DEVICE_ATTR_2(in13_min, S_IWUSR | S_IRUGO, show_in_reg,
1265 store_in_reg, 13, 1);
1266 static SENSOR_DEVICE_ATTR_2(in14_min, S_IWUSR | S_IRUGO, show_in_reg,
1267 store_in_reg, 14, 1);
1269 static SENSOR_DEVICE_ATTR_2(in0_max, S_IWUSR | S_IRUGO, show_in_reg,
1270 store_in_reg, 0, 2);
1271 static SENSOR_DEVICE_ATTR_2(in1_max, S_IWUSR | S_IRUGO, show_in_reg,
1272 store_in_reg, 1, 2);
1273 static SENSOR_DEVICE_ATTR_2(in2_max, S_IWUSR | S_IRUGO, show_in_reg,
1274 store_in_reg, 2, 2);
1275 static SENSOR_DEVICE_ATTR_2(in3_max, S_IWUSR | S_IRUGO, show_in_reg,
1276 store_in_reg, 3, 2);
1277 static SENSOR_DEVICE_ATTR_2(in4_max, S_IWUSR | S_IRUGO, show_in_reg,
1278 store_in_reg, 4, 2);
1279 static SENSOR_DEVICE_ATTR_2(in5_max, S_IWUSR | S_IRUGO, show_in_reg,
1280 store_in_reg, 5, 2);
1281 static SENSOR_DEVICE_ATTR_2(in6_max, S_IWUSR | S_IRUGO, show_in_reg,
1282 store_in_reg, 6, 2);
1283 static SENSOR_DEVICE_ATTR_2(in7_max, S_IWUSR | S_IRUGO, show_in_reg,
1284 store_in_reg, 7, 2);
1285 static SENSOR_DEVICE_ATTR_2(in8_max, S_IWUSR | S_IRUGO, show_in_reg,
1286 store_in_reg, 8, 2);
1287 static SENSOR_DEVICE_ATTR_2(in9_max, S_IWUSR | S_IRUGO, show_in_reg,
1288 store_in_reg, 9, 2);
1289 static SENSOR_DEVICE_ATTR_2(in10_max, S_IWUSR | S_IRUGO, show_in_reg,
1290 store_in_reg, 10, 2);
1291 static SENSOR_DEVICE_ATTR_2(in11_max, S_IWUSR | S_IRUGO, show_in_reg,
1292 store_in_reg, 11, 2);
1293 static SENSOR_DEVICE_ATTR_2(in12_max, S_IWUSR | S_IRUGO, show_in_reg,
1294 store_in_reg, 12, 2);
1295 static SENSOR_DEVICE_ATTR_2(in13_max, S_IWUSR | S_IRUGO, show_in_reg,
1296 store_in_reg, 13, 2);
1297 static SENSOR_DEVICE_ATTR_2(in14_max, S_IWUSR | S_IRUGO, show_in_reg,
1298 store_in_reg, 14, 2);
1300 static struct attribute *nct6775_attributes_in[15][5] = {
1302 &sensor_dev_attr_in0_input.dev_attr.attr,
1303 &sensor_dev_attr_in0_min.dev_attr.attr,
1304 &sensor_dev_attr_in0_max.dev_attr.attr,
1305 &sensor_dev_attr_in0_alarm.dev_attr.attr,
1309 &sensor_dev_attr_in1_input.dev_attr.attr,
1310 &sensor_dev_attr_in1_min.dev_attr.attr,
1311 &sensor_dev_attr_in1_max.dev_attr.attr,
1312 &sensor_dev_attr_in1_alarm.dev_attr.attr,
1316 &sensor_dev_attr_in2_input.dev_attr.attr,
1317 &sensor_dev_attr_in2_min.dev_attr.attr,
1318 &sensor_dev_attr_in2_max.dev_attr.attr,
1319 &sensor_dev_attr_in2_alarm.dev_attr.attr,
1323 &sensor_dev_attr_in3_input.dev_attr.attr,
1324 &sensor_dev_attr_in3_min.dev_attr.attr,
1325 &sensor_dev_attr_in3_max.dev_attr.attr,
1326 &sensor_dev_attr_in3_alarm.dev_attr.attr,
1330 &sensor_dev_attr_in4_input.dev_attr.attr,
1331 &sensor_dev_attr_in4_min.dev_attr.attr,
1332 &sensor_dev_attr_in4_max.dev_attr.attr,
1333 &sensor_dev_attr_in4_alarm.dev_attr.attr,
1337 &sensor_dev_attr_in5_input.dev_attr.attr,
1338 &sensor_dev_attr_in5_min.dev_attr.attr,
1339 &sensor_dev_attr_in5_max.dev_attr.attr,
1340 &sensor_dev_attr_in5_alarm.dev_attr.attr,
1344 &sensor_dev_attr_in6_input.dev_attr.attr,
1345 &sensor_dev_attr_in6_min.dev_attr.attr,
1346 &sensor_dev_attr_in6_max.dev_attr.attr,
1347 &sensor_dev_attr_in6_alarm.dev_attr.attr,
1351 &sensor_dev_attr_in7_input.dev_attr.attr,
1352 &sensor_dev_attr_in7_min.dev_attr.attr,
1353 &sensor_dev_attr_in7_max.dev_attr.attr,
1354 &sensor_dev_attr_in7_alarm.dev_attr.attr,
1358 &sensor_dev_attr_in8_input.dev_attr.attr,
1359 &sensor_dev_attr_in8_min.dev_attr.attr,
1360 &sensor_dev_attr_in8_max.dev_attr.attr,
1361 &sensor_dev_attr_in8_alarm.dev_attr.attr,
1365 &sensor_dev_attr_in9_input.dev_attr.attr,
1366 &sensor_dev_attr_in9_min.dev_attr.attr,
1367 &sensor_dev_attr_in9_max.dev_attr.attr,
1368 &sensor_dev_attr_in9_alarm.dev_attr.attr,
1372 &sensor_dev_attr_in10_input.dev_attr.attr,
1373 &sensor_dev_attr_in10_min.dev_attr.attr,
1374 &sensor_dev_attr_in10_max.dev_attr.attr,
1375 &sensor_dev_attr_in10_alarm.dev_attr.attr,
1379 &sensor_dev_attr_in11_input.dev_attr.attr,
1380 &sensor_dev_attr_in11_min.dev_attr.attr,
1381 &sensor_dev_attr_in11_max.dev_attr.attr,
1382 &sensor_dev_attr_in11_alarm.dev_attr.attr,
1386 &sensor_dev_attr_in12_input.dev_attr.attr,
1387 &sensor_dev_attr_in12_min.dev_attr.attr,
1388 &sensor_dev_attr_in12_max.dev_attr.attr,
1389 &sensor_dev_attr_in12_alarm.dev_attr.attr,
1393 &sensor_dev_attr_in13_input.dev_attr.attr,
1394 &sensor_dev_attr_in13_min.dev_attr.attr,
1395 &sensor_dev_attr_in13_max.dev_attr.attr,
1396 &sensor_dev_attr_in13_alarm.dev_attr.attr,
1400 &sensor_dev_attr_in14_input.dev_attr.attr,
1401 &sensor_dev_attr_in14_min.dev_attr.attr,
1402 &sensor_dev_attr_in14_max.dev_attr.attr,
1403 &sensor_dev_attr_in14_alarm.dev_attr.attr,
1408 static const struct attribute_group nct6775_group_in[15] = {
1409 { .attrs = nct6775_attributes_in[0] },
1410 { .attrs = nct6775_attributes_in[1] },
1411 { .attrs = nct6775_attributes_in[2] },
1412 { .attrs = nct6775_attributes_in[3] },
1413 { .attrs = nct6775_attributes_in[4] },
1414 { .attrs = nct6775_attributes_in[5] },
1415 { .attrs = nct6775_attributes_in[6] },
1416 { .attrs = nct6775_attributes_in[7] },
1417 { .attrs = nct6775_attributes_in[8] },
1418 { .attrs = nct6775_attributes_in[9] },
1419 { .attrs = nct6775_attributes_in[10] },
1420 { .attrs = nct6775_attributes_in[11] },
1421 { .attrs = nct6775_attributes_in[12] },
1422 { .attrs = nct6775_attributes_in[13] },
1423 { .attrs = nct6775_attributes_in[14] },
1427 show_fan(struct device *dev, struct device_attribute *attr, char *buf)
1429 struct nct6775_data *data = nct6775_update_device(dev);
1430 struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr);
1431 int nr = sattr->index;
1432 return sprintf(buf, "%d\n", data->rpm[nr]);
1436 show_fan_min(struct device *dev, struct device_attribute *attr, char *buf)
1438 struct nct6775_data *data = nct6775_update_device(dev);
1439 struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr);
1440 int nr = sattr->index;
1441 return sprintf(buf, "%d\n",
1442 data->fan_from_reg_min(data->fan_min[nr],
1443 data->fan_div[nr]));
1447 show_fan_div(struct device *dev, struct device_attribute *attr, char *buf)
1449 struct nct6775_data *data = nct6775_update_device(dev);
1450 struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr);
1451 int nr = sattr->index;
1452 return sprintf(buf, "%u\n", div_from_reg(data->fan_div[nr]));
1456 store_fan_min(struct device *dev, struct device_attribute *attr,
1457 const char *buf, size_t count)
1459 struct nct6775_data *data = dev_get_drvdata(dev);
1460 struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr);
1461 int nr = sattr->index;
1467 err = kstrtoul(buf, 10, &val);
1471 mutex_lock(&data->update_lock);
1472 if (!data->has_fan_div) {
1473 /* NCT6776F or NCT6779D; we know this is a 13 bit register */
1479 val = 1350000U / val;
1480 val = (val & 0x1f) | ((val << 3) & 0xff00);
1482 data->fan_min[nr] = val;
1483 goto write_min; /* Leave fan divider alone */
1486 /* No min limit, alarm disabled */
1487 data->fan_min[nr] = 255;
1488 new_div = data->fan_div[nr]; /* No change */
1489 dev_info(dev, "fan%u low limit and alarm disabled\n", nr + 1);
1492 reg = 1350000U / val;
1493 if (reg >= 128 * 255) {
1495 * Speed below this value cannot possibly be represented,
1496 * even with the highest divider (128)
1498 data->fan_min[nr] = 254;
1499 new_div = 7; /* 128 == (1 << 7) */
1501 "fan%u low limit %lu below minimum %u, set to minimum\n",
1502 nr + 1, val, data->fan_from_reg_min(254, 7));
1505 * Speed above this value cannot possibly be represented,
1506 * even with the lowest divider (1)
1508 data->fan_min[nr] = 1;
1509 new_div = 0; /* 1 == (1 << 0) */
1511 "fan%u low limit %lu above maximum %u, set to maximum\n",
1512 nr + 1, val, data->fan_from_reg_min(1, 0));
1515 * Automatically pick the best divider, i.e. the one such
1516 * that the min limit will correspond to a register value
1517 * in the 96..192 range
1520 while (reg > 192 && new_div < 7) {
1524 data->fan_min[nr] = reg;
1529 * Write both the fan clock divider (if it changed) and the new
1530 * fan min (unconditionally)
1532 if (new_div != data->fan_div[nr]) {
1533 dev_dbg(dev, "fan%u clock divider changed from %u to %u\n",
1534 nr + 1, div_from_reg(data->fan_div[nr]),
1535 div_from_reg(new_div));
1536 data->fan_div[nr] = new_div;
1537 nct6775_write_fan_div_common(data, nr);
1538 /* Give the chip time to sample a new speed value */
1539 data->last_updated = jiffies;
1543 nct6775_write_value(data, data->REG_FAN_MIN[nr], data->fan_min[nr]);
1544 mutex_unlock(&data->update_lock);
1550 show_fan_pulses(struct device *dev, struct device_attribute *attr, char *buf)
1552 struct nct6775_data *data = nct6775_update_device(dev);
1553 struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr);
1554 int p = data->fan_pulses[sattr->index];
1556 return sprintf(buf, "%d\n", p ? : 4);
1560 store_fan_pulses(struct device *dev, struct device_attribute *attr,
1561 const char *buf, size_t count)
1563 struct nct6775_data *data = dev_get_drvdata(dev);
1564 struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr);
1565 int nr = sattr->index;
1569 err = kstrtoul(buf, 10, &val);
1576 mutex_lock(&data->update_lock);
1577 data->fan_pulses[nr] = val & 3;
1578 nct6775_write_value(data, data->REG_FAN_PULSES[nr], val & 3);
1579 mutex_unlock(&data->update_lock);
1584 static struct sensor_device_attribute sda_fan_input[] = {
1585 SENSOR_ATTR(fan1_input, S_IRUGO, show_fan, NULL, 0),
1586 SENSOR_ATTR(fan2_input, S_IRUGO, show_fan, NULL, 1),
1587 SENSOR_ATTR(fan3_input, S_IRUGO, show_fan, NULL, 2),
1588 SENSOR_ATTR(fan4_input, S_IRUGO, show_fan, NULL, 3),
1589 SENSOR_ATTR(fan5_input, S_IRUGO, show_fan, NULL, 4),
1592 static struct sensor_device_attribute sda_fan_alarm[] = {
1593 SENSOR_ATTR(fan1_alarm, S_IRUGO, show_alarm, NULL, FAN_ALARM_BASE),
1594 SENSOR_ATTR(fan2_alarm, S_IRUGO, show_alarm, NULL, FAN_ALARM_BASE + 1),
1595 SENSOR_ATTR(fan3_alarm, S_IRUGO, show_alarm, NULL, FAN_ALARM_BASE + 2),
1596 SENSOR_ATTR(fan4_alarm, S_IRUGO, show_alarm, NULL, FAN_ALARM_BASE + 3),
1597 SENSOR_ATTR(fan5_alarm, S_IRUGO, show_alarm, NULL, FAN_ALARM_BASE + 4),
1600 static struct sensor_device_attribute sda_fan_min[] = {
1601 SENSOR_ATTR(fan1_min, S_IWUSR | S_IRUGO, show_fan_min,
1603 SENSOR_ATTR(fan2_min, S_IWUSR | S_IRUGO, show_fan_min,
1605 SENSOR_ATTR(fan3_min, S_IWUSR | S_IRUGO, show_fan_min,
1607 SENSOR_ATTR(fan4_min, S_IWUSR | S_IRUGO, show_fan_min,
1609 SENSOR_ATTR(fan5_min, S_IWUSR | S_IRUGO, show_fan_min,
1613 static struct sensor_device_attribute sda_fan_pulses[] = {
1614 SENSOR_ATTR(fan1_pulses, S_IWUSR | S_IRUGO, show_fan_pulses,
1615 store_fan_pulses, 0),
1616 SENSOR_ATTR(fan2_pulses, S_IWUSR | S_IRUGO, show_fan_pulses,
1617 store_fan_pulses, 1),
1618 SENSOR_ATTR(fan3_pulses, S_IWUSR | S_IRUGO, show_fan_pulses,
1619 store_fan_pulses, 2),
1620 SENSOR_ATTR(fan4_pulses, S_IWUSR | S_IRUGO, show_fan_pulses,
1621 store_fan_pulses, 3),
1622 SENSOR_ATTR(fan5_pulses, S_IWUSR | S_IRUGO, show_fan_pulses,
1623 store_fan_pulses, 4),
1626 static struct sensor_device_attribute sda_fan_div[] = {
1627 SENSOR_ATTR(fan1_div, S_IRUGO, show_fan_div, NULL, 0),
1628 SENSOR_ATTR(fan2_div, S_IRUGO, show_fan_div, NULL, 1),
1629 SENSOR_ATTR(fan3_div, S_IRUGO, show_fan_div, NULL, 2),
1630 SENSOR_ATTR(fan4_div, S_IRUGO, show_fan_div, NULL, 3),
1631 SENSOR_ATTR(fan5_div, S_IRUGO, show_fan_div, NULL, 4),
1635 show_temp_label(struct device *dev, struct device_attribute *attr, char *buf)
1637 struct nct6775_data *data = nct6775_update_device(dev);
1638 struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr);
1639 int nr = sattr->index;
1640 return sprintf(buf, "%s\n", data->temp_label[data->temp_src[nr]]);
1644 show_temp(struct device *dev, struct device_attribute *attr, char *buf)
1646 struct nct6775_data *data = nct6775_update_device(dev);
1647 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
1649 int index = sattr->index;
1651 return sprintf(buf, "%d\n", LM75_TEMP_FROM_REG(data->temp[index][nr]));
1655 store_temp(struct device *dev, struct device_attribute *attr, const char *buf,
1658 struct nct6775_data *data = dev_get_drvdata(dev);
1659 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
1661 int index = sattr->index;
1665 err = kstrtol(buf, 10, &val);
1669 mutex_lock(&data->update_lock);
1670 data->temp[index][nr] = LM75_TEMP_TO_REG(val);
1671 nct6775_write_temp(data, data->reg_temp[index][nr],
1672 data->temp[index][nr]);
1673 mutex_unlock(&data->update_lock);
1678 show_temp_offset(struct device *dev, struct device_attribute *attr, char *buf)
1680 struct nct6775_data *data = nct6775_update_device(dev);
1681 struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr);
1683 return sprintf(buf, "%d\n", data->temp_offset[sattr->index] * 1000);
1687 store_temp_offset(struct device *dev, struct device_attribute *attr,
1688 const char *buf, size_t count)
1690 struct nct6775_data *data = dev_get_drvdata(dev);
1691 struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr);
1692 int nr = sattr->index;
1696 err = kstrtol(buf, 10, &val);
1700 val = clamp_val(DIV_ROUND_CLOSEST(val, 1000), -128, 127);
1702 mutex_lock(&data->update_lock);
1703 data->temp_offset[nr] = val;
1704 nct6775_write_value(data, data->REG_TEMP_OFFSET[nr], val);
1705 mutex_unlock(&data->update_lock);
1711 show_temp_type(struct device *dev, struct device_attribute *attr, char *buf)
1713 struct nct6775_data *data = nct6775_update_device(dev);
1714 struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr);
1715 int nr = sattr->index;
1716 return sprintf(buf, "%d\n", (int)data->temp_type[nr]);
1720 store_temp_type(struct device *dev, struct device_attribute *attr,
1721 const char *buf, size_t count)
1723 struct nct6775_data *data = nct6775_update_device(dev);
1724 struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr);
1725 int nr = sattr->index;
1728 u8 vbat, diode, bit;
1730 err = kstrtoul(buf, 10, &val);
1734 if (val != 1 && val != 3 && val != 4)
1737 mutex_lock(&data->update_lock);
1739 data->temp_type[nr] = val;
1740 vbat = nct6775_read_value(data, data->REG_VBAT) & ~(0x02 << nr);
1741 diode = nct6775_read_value(data, data->REG_DIODE) & ~(0x02 << nr);
1744 case 1: /* CPU diode (diode, current mode) */
1748 case 3: /* diode, voltage mode */
1751 case 4: /* thermistor */
1754 nct6775_write_value(data, data->REG_VBAT, vbat);
1755 nct6775_write_value(data, data->REG_DIODE, diode);
1757 mutex_unlock(&data->update_lock);
1761 static struct sensor_device_attribute_2 sda_temp_input[] = {
1762 SENSOR_ATTR_2(temp1_input, S_IRUGO, show_temp, NULL, 0, 0),
1763 SENSOR_ATTR_2(temp2_input, S_IRUGO, show_temp, NULL, 1, 0),
1764 SENSOR_ATTR_2(temp3_input, S_IRUGO, show_temp, NULL, 2, 0),
1765 SENSOR_ATTR_2(temp4_input, S_IRUGO, show_temp, NULL, 3, 0),
1766 SENSOR_ATTR_2(temp5_input, S_IRUGO, show_temp, NULL, 4, 0),
1767 SENSOR_ATTR_2(temp6_input, S_IRUGO, show_temp, NULL, 5, 0),
1768 SENSOR_ATTR_2(temp7_input, S_IRUGO, show_temp, NULL, 6, 0),
1769 SENSOR_ATTR_2(temp8_input, S_IRUGO, show_temp, NULL, 7, 0),
1770 SENSOR_ATTR_2(temp9_input, S_IRUGO, show_temp, NULL, 8, 0),
1771 SENSOR_ATTR_2(temp10_input, S_IRUGO, show_temp, NULL, 9, 0),
1774 static struct sensor_device_attribute sda_temp_label[] = {
1775 SENSOR_ATTR(temp1_label, S_IRUGO, show_temp_label, NULL, 0),
1776 SENSOR_ATTR(temp2_label, S_IRUGO, show_temp_label, NULL, 1),
1777 SENSOR_ATTR(temp3_label, S_IRUGO, show_temp_label, NULL, 2),
1778 SENSOR_ATTR(temp4_label, S_IRUGO, show_temp_label, NULL, 3),
1779 SENSOR_ATTR(temp5_label, S_IRUGO, show_temp_label, NULL, 4),
1780 SENSOR_ATTR(temp6_label, S_IRUGO, show_temp_label, NULL, 5),
1781 SENSOR_ATTR(temp7_label, S_IRUGO, show_temp_label, NULL, 6),
1782 SENSOR_ATTR(temp8_label, S_IRUGO, show_temp_label, NULL, 7),
1783 SENSOR_ATTR(temp9_label, S_IRUGO, show_temp_label, NULL, 8),
1784 SENSOR_ATTR(temp10_label, S_IRUGO, show_temp_label, NULL, 9),
1787 static struct sensor_device_attribute_2 sda_temp_max[] = {
1788 SENSOR_ATTR_2(temp1_max, S_IRUGO | S_IWUSR, show_temp, store_temp,
1790 SENSOR_ATTR_2(temp2_max, S_IRUGO | S_IWUSR, show_temp, store_temp,
1792 SENSOR_ATTR_2(temp3_max, S_IRUGO | S_IWUSR, show_temp, store_temp,
1794 SENSOR_ATTR_2(temp4_max, S_IRUGO | S_IWUSR, show_temp, store_temp,
1796 SENSOR_ATTR_2(temp5_max, S_IRUGO | S_IWUSR, show_temp, store_temp,
1798 SENSOR_ATTR_2(temp6_max, S_IRUGO | S_IWUSR, show_temp, store_temp,
1800 SENSOR_ATTR_2(temp7_max, S_IRUGO | S_IWUSR, show_temp, store_temp,
1802 SENSOR_ATTR_2(temp8_max, S_IRUGO | S_IWUSR, show_temp, store_temp,
1804 SENSOR_ATTR_2(temp9_max, S_IRUGO | S_IWUSR, show_temp, store_temp,
1806 SENSOR_ATTR_2(temp10_max, S_IRUGO | S_IWUSR, show_temp, store_temp,
1810 static struct sensor_device_attribute_2 sda_temp_max_hyst[] = {
1811 SENSOR_ATTR_2(temp1_max_hyst, S_IRUGO | S_IWUSR, show_temp, store_temp,
1813 SENSOR_ATTR_2(temp2_max_hyst, S_IRUGO | S_IWUSR, show_temp, store_temp,
1815 SENSOR_ATTR_2(temp3_max_hyst, S_IRUGO | S_IWUSR, show_temp, store_temp,
1817 SENSOR_ATTR_2(temp4_max_hyst, S_IRUGO | S_IWUSR, show_temp, store_temp,
1819 SENSOR_ATTR_2(temp5_max_hyst, S_IRUGO | S_IWUSR, show_temp, store_temp,
1821 SENSOR_ATTR_2(temp6_max_hyst, S_IRUGO | S_IWUSR, show_temp, store_temp,
1823 SENSOR_ATTR_2(temp7_max_hyst, S_IRUGO | S_IWUSR, show_temp, store_temp,
1825 SENSOR_ATTR_2(temp8_max_hyst, S_IRUGO | S_IWUSR, show_temp, store_temp,
1827 SENSOR_ATTR_2(temp9_max_hyst, S_IRUGO | S_IWUSR, show_temp, store_temp,
1829 SENSOR_ATTR_2(temp10_max_hyst, S_IRUGO | S_IWUSR, show_temp, store_temp,
1833 static struct sensor_device_attribute_2 sda_temp_crit[] = {
1834 SENSOR_ATTR_2(temp1_crit, S_IRUGO | S_IWUSR, show_temp, store_temp,
1836 SENSOR_ATTR_2(temp2_crit, S_IRUGO | S_IWUSR, show_temp, store_temp,
1838 SENSOR_ATTR_2(temp3_crit, S_IRUGO | S_IWUSR, show_temp, store_temp,
1840 SENSOR_ATTR_2(temp4_crit, S_IRUGO | S_IWUSR, show_temp, store_temp,
1842 SENSOR_ATTR_2(temp5_crit, S_IRUGO | S_IWUSR, show_temp, store_temp,
1844 SENSOR_ATTR_2(temp6_crit, S_IRUGO | S_IWUSR, show_temp, store_temp,
1846 SENSOR_ATTR_2(temp7_crit, S_IRUGO | S_IWUSR, show_temp, store_temp,
1848 SENSOR_ATTR_2(temp8_crit, S_IRUGO | S_IWUSR, show_temp, store_temp,
1850 SENSOR_ATTR_2(temp9_crit, S_IRUGO | S_IWUSR, show_temp, store_temp,
1852 SENSOR_ATTR_2(temp10_crit, S_IRUGO | S_IWUSR, show_temp, store_temp,
1856 static struct sensor_device_attribute sda_temp_offset[] = {
1857 SENSOR_ATTR(temp1_offset, S_IRUGO | S_IWUSR, show_temp_offset,
1858 store_temp_offset, 0),
1859 SENSOR_ATTR(temp2_offset, S_IRUGO | S_IWUSR, show_temp_offset,
1860 store_temp_offset, 1),
1861 SENSOR_ATTR(temp3_offset, S_IRUGO | S_IWUSR, show_temp_offset,
1862 store_temp_offset, 2),
1863 SENSOR_ATTR(temp4_offset, S_IRUGO | S_IWUSR, show_temp_offset,
1864 store_temp_offset, 3),
1865 SENSOR_ATTR(temp5_offset, S_IRUGO | S_IWUSR, show_temp_offset,
1866 store_temp_offset, 4),
1867 SENSOR_ATTR(temp6_offset, S_IRUGO | S_IWUSR, show_temp_offset,
1868 store_temp_offset, 5),
1871 static struct sensor_device_attribute sda_temp_type[] = {
1872 SENSOR_ATTR(temp1_type, S_IRUGO | S_IWUSR, show_temp_type,
1873 store_temp_type, 0),
1874 SENSOR_ATTR(temp2_type, S_IRUGO | S_IWUSR, show_temp_type,
1875 store_temp_type, 1),
1876 SENSOR_ATTR(temp3_type, S_IRUGO | S_IWUSR, show_temp_type,
1877 store_temp_type, 2),
1878 SENSOR_ATTR(temp4_type, S_IRUGO | S_IWUSR, show_temp_type,
1879 store_temp_type, 3),
1880 SENSOR_ATTR(temp5_type, S_IRUGO | S_IWUSR, show_temp_type,
1881 store_temp_type, 4),
1882 SENSOR_ATTR(temp6_type, S_IRUGO | S_IWUSR, show_temp_type,
1883 store_temp_type, 5),
1886 static struct sensor_device_attribute sda_temp_alarm[] = {
1887 SENSOR_ATTR(temp1_alarm, S_IRUGO, show_alarm, NULL,
1889 SENSOR_ATTR(temp2_alarm, S_IRUGO, show_alarm, NULL,
1890 TEMP_ALARM_BASE + 1),
1891 SENSOR_ATTR(temp3_alarm, S_IRUGO, show_alarm, NULL,
1892 TEMP_ALARM_BASE + 2),
1893 SENSOR_ATTR(temp4_alarm, S_IRUGO, show_alarm, NULL,
1894 TEMP_ALARM_BASE + 3),
1895 SENSOR_ATTR(temp5_alarm, S_IRUGO, show_alarm, NULL,
1896 TEMP_ALARM_BASE + 4),
1897 SENSOR_ATTR(temp6_alarm, S_IRUGO, show_alarm, NULL,
1898 TEMP_ALARM_BASE + 5),
1901 #define NUM_TEMP_ALARM ARRAY_SIZE(sda_temp_alarm)
1904 show_pwm_mode(struct device *dev, struct device_attribute *attr, char *buf)
1906 struct nct6775_data *data = nct6775_update_device(dev);
1907 struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr);
1909 return sprintf(buf, "%d\n", !data->pwm_mode[sattr->index]);
1913 store_pwm_mode(struct device *dev, struct device_attribute *attr,
1914 const char *buf, size_t count)
1916 struct nct6775_data *data = dev_get_drvdata(dev);
1917 struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr);
1918 int nr = sattr->index;
1923 err = kstrtoul(buf, 10, &val);
1930 /* Setting DC mode is not supported for all chips/channels */
1931 if (data->REG_PWM_MODE[nr] == 0) {
1937 mutex_lock(&data->update_lock);
1938 data->pwm_mode[nr] = val;
1939 reg = nct6775_read_value(data, data->REG_PWM_MODE[nr]);
1940 reg &= ~data->PWM_MODE_MASK[nr];
1942 reg |= data->PWM_MODE_MASK[nr];
1943 nct6775_write_value(data, data->REG_PWM_MODE[nr], reg);
1944 mutex_unlock(&data->update_lock);
1949 show_pwm(struct device *dev, struct device_attribute *attr, char *buf)
1951 struct nct6775_data *data = nct6775_update_device(dev);
1952 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
1954 int index = sattr->index;
1958 * For automatic fan control modes, show current pwm readings.
1959 * Otherwise, show the configured value.
1961 if (index == 0 && data->pwm_enable[nr] > manual)
1962 pwm = nct6775_read_value(data, data->REG_PWM_READ[nr]);
1964 pwm = data->pwm[index][nr];
1966 return sprintf(buf, "%d\n", pwm);
1970 store_pwm(struct device *dev, struct device_attribute *attr, const char *buf,
1973 struct nct6775_data *data = dev_get_drvdata(dev);
1974 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
1976 int index = sattr->index;
1978 int minval[7] = { 0, 1, 1, data->pwm[2][nr], 0, 0, 0 };
1980 = { 255, 255, data->pwm[3][nr] ? : 255, 255, 255, 255, 255 };
1984 err = kstrtoul(buf, 10, &val);
1987 val = clamp_val(val, minval[index], maxval[index]);
1989 mutex_lock(&data->update_lock);
1990 data->pwm[index][nr] = val;
1991 nct6775_write_value(data, data->REG_PWM[index][nr], val);
1992 if (index == 2) { /* floor: disable if val == 0 */
1993 reg = nct6775_read_value(data, data->REG_TEMP_SEL[nr]);
1997 nct6775_write_value(data, data->REG_TEMP_SEL[nr], reg);
1999 mutex_unlock(&data->update_lock);
2003 /* Returns 0 if OK, -EINVAL otherwise */
2004 static int check_trip_points(struct nct6775_data *data, int nr)
2008 for (i = 0; i < data->auto_pwm_num - 1; i++) {
2009 if (data->auto_temp[nr][i] > data->auto_temp[nr][i + 1])
2012 for (i = 0; i < data->auto_pwm_num - 1; i++) {
2013 if (data->auto_pwm[nr][i] > data->auto_pwm[nr][i + 1])
2016 /* validate critical temperature and pwm if enabled (pwm > 0) */
2017 if (data->auto_pwm[nr][data->auto_pwm_num]) {
2018 if (data->auto_temp[nr][data->auto_pwm_num - 1] >
2019 data->auto_temp[nr][data->auto_pwm_num] ||
2020 data->auto_pwm[nr][data->auto_pwm_num - 1] >
2021 data->auto_pwm[nr][data->auto_pwm_num])
2027 static void pwm_update_registers(struct nct6775_data *data, int nr)
2031 switch (data->pwm_enable[nr]) {
2036 reg = nct6775_read_value(data, data->REG_FAN_MODE[nr]);
2037 reg = (reg & ~data->tolerance_mask) |
2038 (data->target_speed_tolerance[nr] & data->tolerance_mask);
2039 nct6775_write_value(data, data->REG_FAN_MODE[nr], reg);
2040 nct6775_write_value(data, data->REG_TARGET[nr],
2041 data->target_speed[nr] & 0xff);
2042 if (data->REG_TOLERANCE_H) {
2043 reg = (data->target_speed[nr] >> 8) & 0x0f;
2044 reg |= (data->target_speed_tolerance[nr] & 0x38) << 1;
2045 nct6775_write_value(data,
2046 data->REG_TOLERANCE_H[nr],
2050 case thermal_cruise:
2051 nct6775_write_value(data, data->REG_TARGET[nr],
2052 data->target_temp[nr]);
2055 reg = nct6775_read_value(data, data->REG_FAN_MODE[nr]);
2056 reg = (reg & ~data->tolerance_mask) |
2057 data->temp_tolerance[0][nr];
2058 nct6775_write_value(data, data->REG_FAN_MODE[nr], reg);
2064 show_pwm_enable(struct device *dev, struct device_attribute *attr, char *buf)
2066 struct nct6775_data *data = nct6775_update_device(dev);
2067 struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr);
2069 return sprintf(buf, "%d\n", data->pwm_enable[sattr->index]);
2073 store_pwm_enable(struct device *dev, struct device_attribute *attr,
2074 const char *buf, size_t count)
2076 struct nct6775_data *data = dev_get_drvdata(dev);
2077 struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr);
2078 int nr = sattr->index;
2083 err = kstrtoul(buf, 10, &val);
2090 if (val == sf3 && data->kind != nct6775)
2093 if (val == sf4 && check_trip_points(data, nr)) {
2094 dev_err(dev, "Inconsistent trip points, not switching to SmartFan IV mode\n");
2095 dev_err(dev, "Adjust trip points and try again\n");
2099 mutex_lock(&data->update_lock);
2100 data->pwm_enable[nr] = val;
2103 * turn off pwm control: select manual mode, set pwm to maximum
2105 data->pwm[0][nr] = 255;
2106 nct6775_write_value(data, data->REG_PWM[0][nr], 255);
2108 pwm_update_registers(data, nr);
2109 reg = nct6775_read_value(data, data->REG_FAN_MODE[nr]);
2111 reg |= pwm_enable_to_reg(val) << 4;
2112 nct6775_write_value(data, data->REG_FAN_MODE[nr], reg);
2113 mutex_unlock(&data->update_lock);
2118 show_pwm_temp_sel_common(struct nct6775_data *data, char *buf, int src)
2122 for (i = 0; i < NUM_TEMP; i++) {
2123 if (!(data->have_temp & (1 << i)))
2125 if (src == data->temp_src[i]) {
2131 return sprintf(buf, "%d\n", sel);
2135 show_pwm_temp_sel(struct device *dev, struct device_attribute *attr, char *buf)
2137 struct nct6775_data *data = nct6775_update_device(dev);
2138 struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr);
2139 int index = sattr->index;
2141 return show_pwm_temp_sel_common(data, buf, data->pwm_temp_sel[index]);
2145 store_pwm_temp_sel(struct device *dev, struct device_attribute *attr,
2146 const char *buf, size_t count)
2148 struct nct6775_data *data = nct6775_update_device(dev);
2149 struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr);
2150 int nr = sattr->index;
2154 err = kstrtoul(buf, 10, &val);
2157 if (val == 0 || val > NUM_TEMP)
2159 if (!(data->have_temp & (1 << (val - 1))) || !data->temp_src[val - 1])
2162 mutex_lock(&data->update_lock);
2163 src = data->temp_src[val - 1];
2164 data->pwm_temp_sel[nr] = src;
2165 reg = nct6775_read_value(data, data->REG_TEMP_SEL[nr]);
2168 nct6775_write_value(data, data->REG_TEMP_SEL[nr], reg);
2169 mutex_unlock(&data->update_lock);
2175 show_pwm_weight_temp_sel(struct device *dev, struct device_attribute *attr,
2178 struct nct6775_data *data = nct6775_update_device(dev);
2179 struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr);
2180 int index = sattr->index;
2182 return show_pwm_temp_sel_common(data, buf,
2183 data->pwm_weight_temp_sel[index]);
2187 store_pwm_weight_temp_sel(struct device *dev, struct device_attribute *attr,
2188 const char *buf, size_t count)
2190 struct nct6775_data *data = nct6775_update_device(dev);
2191 struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr);
2192 int nr = sattr->index;
2196 err = kstrtoul(buf, 10, &val);
2201 if (val && (!(data->have_temp & (1 << (val - 1))) ||
2202 !data->temp_src[val - 1]))
2205 mutex_lock(&data->update_lock);
2207 src = data->temp_src[val - 1];
2208 data->pwm_weight_temp_sel[nr] = src;
2209 reg = nct6775_read_value(data, data->REG_WEIGHT_TEMP_SEL[nr]);
2211 reg |= (src | 0x80);
2212 nct6775_write_value(data, data->REG_WEIGHT_TEMP_SEL[nr], reg);
2214 data->pwm_weight_temp_sel[nr] = 0;
2215 reg = nct6775_read_value(data, data->REG_WEIGHT_TEMP_SEL[nr]);
2217 nct6775_write_value(data, data->REG_WEIGHT_TEMP_SEL[nr], reg);
2219 mutex_unlock(&data->update_lock);
2225 show_target_temp(struct device *dev, struct device_attribute *attr, char *buf)
2227 struct nct6775_data *data = nct6775_update_device(dev);
2228 struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr);
2230 return sprintf(buf, "%d\n", data->target_temp[sattr->index] * 1000);
2234 store_target_temp(struct device *dev, struct device_attribute *attr,
2235 const char *buf, size_t count)
2237 struct nct6775_data *data = dev_get_drvdata(dev);
2238 struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr);
2239 int nr = sattr->index;
2243 err = kstrtoul(buf, 10, &val);
2247 val = clamp_val(DIV_ROUND_CLOSEST(val, 1000), 0,
2248 data->target_temp_mask);
2250 mutex_lock(&data->update_lock);
2251 data->target_temp[nr] = val;
2252 pwm_update_registers(data, nr);
2253 mutex_unlock(&data->update_lock);
2258 show_target_speed(struct device *dev, struct device_attribute *attr, char *buf)
2260 struct nct6775_data *data = nct6775_update_device(dev);
2261 struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr);
2262 int nr = sattr->index;
2264 return sprintf(buf, "%d\n",
2265 fan_from_reg16(data->target_speed[nr],
2266 data->fan_div[nr]));
2270 store_target_speed(struct device *dev, struct device_attribute *attr,
2271 const char *buf, size_t count)
2273 struct nct6775_data *data = dev_get_drvdata(dev);
2274 struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr);
2275 int nr = sattr->index;
2280 err = kstrtoul(buf, 10, &val);
2284 val = clamp_val(val, 0, 1350000U);
2285 speed = fan_to_reg(val, data->fan_div[nr]);
2287 mutex_lock(&data->update_lock);
2288 data->target_speed[nr] = speed;
2289 pwm_update_registers(data, nr);
2290 mutex_unlock(&data->update_lock);
2295 show_temp_tolerance(struct device *dev, struct device_attribute *attr,
2298 struct nct6775_data *data = nct6775_update_device(dev);
2299 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
2301 int index = sattr->index;
2303 return sprintf(buf, "%d\n", data->temp_tolerance[index][nr] * 1000);
2307 store_temp_tolerance(struct device *dev, struct device_attribute *attr,
2308 const char *buf, size_t count)
2310 struct nct6775_data *data = dev_get_drvdata(dev);
2311 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
2313 int index = sattr->index;
2317 err = kstrtoul(buf, 10, &val);
2321 /* Limit tolerance as needed */
2322 val = clamp_val(DIV_ROUND_CLOSEST(val, 1000), 0, data->tolerance_mask);
2324 mutex_lock(&data->update_lock);
2325 data->temp_tolerance[index][nr] = val;
2327 pwm_update_registers(data, nr);
2329 nct6775_write_value(data,
2330 data->REG_CRITICAL_TEMP_TOLERANCE[nr],
2332 mutex_unlock(&data->update_lock);
2337 * Fan speed tolerance is a tricky beast, since the associated register is
2338 * a tick counter, but the value is reported and configured as rpm.
2339 * Compute resulting low and high rpm values and report the difference.
2342 show_speed_tolerance(struct device *dev, struct device_attribute *attr,
2345 struct nct6775_data *data = nct6775_update_device(dev);
2346 struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr);
2347 int nr = sattr->index;
2348 int low = data->target_speed[nr] - data->target_speed_tolerance[nr];
2349 int high = data->target_speed[nr] + data->target_speed_tolerance[nr];
2359 tolerance = (fan_from_reg16(low, data->fan_div[nr])
2360 - fan_from_reg16(high, data->fan_div[nr])) / 2;
2362 return sprintf(buf, "%d\n", tolerance);
2366 store_speed_tolerance(struct device *dev, struct device_attribute *attr,
2367 const char *buf, size_t count)
2369 struct nct6775_data *data = dev_get_drvdata(dev);
2370 struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr);
2371 int nr = sattr->index;
2376 err = kstrtoul(buf, 10, &val);
2380 high = fan_from_reg16(data->target_speed[nr],
2381 data->fan_div[nr]) + val;
2382 low = fan_from_reg16(data->target_speed[nr],
2383 data->fan_div[nr]) - val;
2389 val = (fan_to_reg(low, data->fan_div[nr]) -
2390 fan_to_reg(high, data->fan_div[nr])) / 2;
2392 /* Limit tolerance as needed */
2393 val = clamp_val(val, 0, data->speed_tolerance_limit);
2395 mutex_lock(&data->update_lock);
2396 data->target_speed_tolerance[nr] = val;
2397 pwm_update_registers(data, nr);
2398 mutex_unlock(&data->update_lock);
2402 static SENSOR_DEVICE_ATTR_2(pwm1, S_IWUSR | S_IRUGO, show_pwm, store_pwm, 0, 0);
2403 static SENSOR_DEVICE_ATTR_2(pwm2, S_IWUSR | S_IRUGO, show_pwm, store_pwm, 1, 0);
2404 static SENSOR_DEVICE_ATTR_2(pwm3, S_IWUSR | S_IRUGO, show_pwm, store_pwm, 2, 0);
2405 static SENSOR_DEVICE_ATTR_2(pwm4, S_IWUSR | S_IRUGO, show_pwm, store_pwm, 3, 0);
2406 static SENSOR_DEVICE_ATTR_2(pwm5, S_IWUSR | S_IRUGO, show_pwm, store_pwm, 4, 0);
2408 static SENSOR_DEVICE_ATTR(pwm1_mode, S_IWUSR | S_IRUGO, show_pwm_mode,
2410 static SENSOR_DEVICE_ATTR(pwm2_mode, S_IWUSR | S_IRUGO, show_pwm_mode,
2412 static SENSOR_DEVICE_ATTR(pwm3_mode, S_IWUSR | S_IRUGO, show_pwm_mode,
2414 static SENSOR_DEVICE_ATTR(pwm4_mode, S_IWUSR | S_IRUGO, show_pwm_mode,
2416 static SENSOR_DEVICE_ATTR(pwm5_mode, S_IWUSR | S_IRUGO, show_pwm_mode,
2419 static SENSOR_DEVICE_ATTR(pwm1_enable, S_IWUSR | S_IRUGO, show_pwm_enable,
2420 store_pwm_enable, 0);
2421 static SENSOR_DEVICE_ATTR(pwm2_enable, S_IWUSR | S_IRUGO, show_pwm_enable,
2422 store_pwm_enable, 1);
2423 static SENSOR_DEVICE_ATTR(pwm3_enable, S_IWUSR | S_IRUGO, show_pwm_enable,
2424 store_pwm_enable, 2);
2425 static SENSOR_DEVICE_ATTR(pwm4_enable, S_IWUSR | S_IRUGO, show_pwm_enable,
2426 store_pwm_enable, 3);
2427 static SENSOR_DEVICE_ATTR(pwm5_enable, S_IWUSR | S_IRUGO, show_pwm_enable,
2428 store_pwm_enable, 4);
2430 static SENSOR_DEVICE_ATTR(pwm1_temp_sel, S_IWUSR | S_IRUGO,
2431 show_pwm_temp_sel, store_pwm_temp_sel, 0);
2432 static SENSOR_DEVICE_ATTR(pwm2_temp_sel, S_IWUSR | S_IRUGO,
2433 show_pwm_temp_sel, store_pwm_temp_sel, 1);
2434 static SENSOR_DEVICE_ATTR(pwm3_temp_sel, S_IWUSR | S_IRUGO,
2435 show_pwm_temp_sel, store_pwm_temp_sel, 2);
2436 static SENSOR_DEVICE_ATTR(pwm4_temp_sel, S_IWUSR | S_IRUGO,
2437 show_pwm_temp_sel, store_pwm_temp_sel, 3);
2438 static SENSOR_DEVICE_ATTR(pwm5_temp_sel, S_IWUSR | S_IRUGO,
2439 show_pwm_temp_sel, store_pwm_temp_sel, 4);
2441 static SENSOR_DEVICE_ATTR(pwm1_target_temp, S_IWUSR | S_IRUGO, show_target_temp,
2442 store_target_temp, 0);
2443 static SENSOR_DEVICE_ATTR(pwm2_target_temp, S_IWUSR | S_IRUGO, show_target_temp,
2444 store_target_temp, 1);
2445 static SENSOR_DEVICE_ATTR(pwm3_target_temp, S_IWUSR | S_IRUGO, show_target_temp,
2446 store_target_temp, 2);
2447 static SENSOR_DEVICE_ATTR(pwm4_target_temp, S_IWUSR | S_IRUGO, show_target_temp,
2448 store_target_temp, 3);
2449 static SENSOR_DEVICE_ATTR(pwm5_target_temp, S_IWUSR | S_IRUGO, show_target_temp,
2450 store_target_temp, 4);
2452 static SENSOR_DEVICE_ATTR(fan1_target, S_IWUSR | S_IRUGO, show_target_speed,
2453 store_target_speed, 0);
2454 static SENSOR_DEVICE_ATTR(fan2_target, S_IWUSR | S_IRUGO, show_target_speed,
2455 store_target_speed, 1);
2456 static SENSOR_DEVICE_ATTR(fan3_target, S_IWUSR | S_IRUGO, show_target_speed,
2457 store_target_speed, 2);
2458 static SENSOR_DEVICE_ATTR(fan4_target, S_IWUSR | S_IRUGO, show_target_speed,
2459 store_target_speed, 3);
2460 static SENSOR_DEVICE_ATTR(fan5_target, S_IWUSR | S_IRUGO, show_target_speed,
2461 store_target_speed, 4);
2463 static SENSOR_DEVICE_ATTR(fan1_tolerance, S_IWUSR | S_IRUGO,
2464 show_speed_tolerance, store_speed_tolerance, 0);
2465 static SENSOR_DEVICE_ATTR(fan2_tolerance, S_IWUSR | S_IRUGO,
2466 show_speed_tolerance, store_speed_tolerance, 1);
2467 static SENSOR_DEVICE_ATTR(fan3_tolerance, S_IWUSR | S_IRUGO,
2468 show_speed_tolerance, store_speed_tolerance, 2);
2469 static SENSOR_DEVICE_ATTR(fan4_tolerance, S_IWUSR | S_IRUGO,
2470 show_speed_tolerance, store_speed_tolerance, 3);
2471 static SENSOR_DEVICE_ATTR(fan5_tolerance, S_IWUSR | S_IRUGO,
2472 show_speed_tolerance, store_speed_tolerance, 4);
2474 /* Smart Fan registers */
2477 show_weight_temp(struct device *dev, struct device_attribute *attr, char *buf)
2479 struct nct6775_data *data = nct6775_update_device(dev);
2480 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
2482 int index = sattr->index;
2484 return sprintf(buf, "%d\n", data->weight_temp[index][nr] * 1000);
2488 store_weight_temp(struct device *dev, struct device_attribute *attr,
2489 const char *buf, size_t count)
2491 struct nct6775_data *data = dev_get_drvdata(dev);
2492 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
2494 int index = sattr->index;
2498 err = kstrtoul(buf, 10, &val);
2502 val = clamp_val(DIV_ROUND_CLOSEST(val, 1000), 0, 255);
2504 mutex_lock(&data->update_lock);
2505 data->weight_temp[index][nr] = val;
2506 nct6775_write_value(data, data->REG_WEIGHT_TEMP[index][nr], val);
2507 mutex_unlock(&data->update_lock);
2511 static SENSOR_DEVICE_ATTR(pwm1_weight_temp_sel, S_IWUSR | S_IRUGO,
2512 show_pwm_weight_temp_sel, store_pwm_weight_temp_sel,
2514 static SENSOR_DEVICE_ATTR(pwm2_weight_temp_sel, S_IWUSR | S_IRUGO,
2515 show_pwm_weight_temp_sel, store_pwm_weight_temp_sel,
2517 static SENSOR_DEVICE_ATTR(pwm3_weight_temp_sel, S_IWUSR | S_IRUGO,
2518 show_pwm_weight_temp_sel, store_pwm_weight_temp_sel,
2520 static SENSOR_DEVICE_ATTR(pwm4_weight_temp_sel, S_IWUSR | S_IRUGO,
2521 show_pwm_weight_temp_sel, store_pwm_weight_temp_sel,
2523 static SENSOR_DEVICE_ATTR(pwm5_weight_temp_sel, S_IWUSR | S_IRUGO,
2524 show_pwm_weight_temp_sel, store_pwm_weight_temp_sel,
2527 static SENSOR_DEVICE_ATTR_2(pwm1_weight_temp_step, S_IWUSR | S_IRUGO,
2528 show_weight_temp, store_weight_temp, 0, 0);
2529 static SENSOR_DEVICE_ATTR_2(pwm2_weight_temp_step, S_IWUSR | S_IRUGO,
2530 show_weight_temp, store_weight_temp, 1, 0);
2531 static SENSOR_DEVICE_ATTR_2(pwm3_weight_temp_step, S_IWUSR | S_IRUGO,
2532 show_weight_temp, store_weight_temp, 2, 0);
2533 static SENSOR_DEVICE_ATTR_2(pwm4_weight_temp_step, S_IWUSR | S_IRUGO,
2534 show_weight_temp, store_weight_temp, 3, 0);
2535 static SENSOR_DEVICE_ATTR_2(pwm5_weight_temp_step, S_IWUSR | S_IRUGO,
2536 show_weight_temp, store_weight_temp, 4, 0);
2538 static SENSOR_DEVICE_ATTR_2(pwm1_weight_temp_step_tol, S_IWUSR | S_IRUGO,
2539 show_weight_temp, store_weight_temp, 0, 1);
2540 static SENSOR_DEVICE_ATTR_2(pwm2_weight_temp_step_tol, S_IWUSR | S_IRUGO,
2541 show_weight_temp, store_weight_temp, 1, 1);
2542 static SENSOR_DEVICE_ATTR_2(pwm3_weight_temp_step_tol, S_IWUSR | S_IRUGO,
2543 show_weight_temp, store_weight_temp, 2, 1);
2544 static SENSOR_DEVICE_ATTR_2(pwm4_weight_temp_step_tol, S_IWUSR | S_IRUGO,
2545 show_weight_temp, store_weight_temp, 3, 1);
2546 static SENSOR_DEVICE_ATTR_2(pwm5_weight_temp_step_tol, S_IWUSR | S_IRUGO,
2547 show_weight_temp, store_weight_temp, 4, 1);
2549 static SENSOR_DEVICE_ATTR_2(pwm1_weight_temp_step_base, S_IWUSR | S_IRUGO,
2550 show_weight_temp, store_weight_temp, 0, 2);
2551 static SENSOR_DEVICE_ATTR_2(pwm2_weight_temp_step_base, S_IWUSR | S_IRUGO,
2552 show_weight_temp, store_weight_temp, 1, 2);
2553 static SENSOR_DEVICE_ATTR_2(pwm3_weight_temp_step_base, S_IWUSR | S_IRUGO,
2554 show_weight_temp, store_weight_temp, 2, 2);
2555 static SENSOR_DEVICE_ATTR_2(pwm4_weight_temp_step_base, S_IWUSR | S_IRUGO,
2556 show_weight_temp, store_weight_temp, 3, 2);
2557 static SENSOR_DEVICE_ATTR_2(pwm5_weight_temp_step_base, S_IWUSR | S_IRUGO,
2558 show_weight_temp, store_weight_temp, 4, 2);
2560 static SENSOR_DEVICE_ATTR_2(pwm1_weight_duty_step, S_IWUSR | S_IRUGO,
2561 show_pwm, store_pwm, 0, 5);
2562 static SENSOR_DEVICE_ATTR_2(pwm2_weight_duty_step, S_IWUSR | S_IRUGO,
2563 show_pwm, store_pwm, 1, 5);
2564 static SENSOR_DEVICE_ATTR_2(pwm3_weight_duty_step, S_IWUSR | S_IRUGO,
2565 show_pwm, store_pwm, 2, 5);
2566 static SENSOR_DEVICE_ATTR_2(pwm4_weight_duty_step, S_IWUSR | S_IRUGO,
2567 show_pwm, store_pwm, 3, 5);
2568 static SENSOR_DEVICE_ATTR_2(pwm5_weight_duty_step, S_IWUSR | S_IRUGO,
2569 show_pwm, store_pwm, 4, 5);
2571 /* duty_base is not supported on all chips */
2572 static struct sensor_device_attribute_2 sda_weight_duty_base[] = {
2573 SENSOR_ATTR_2(pwm1_weight_duty_base, S_IWUSR | S_IRUGO,
2574 show_pwm, store_pwm, 0, 6),
2575 SENSOR_ATTR_2(pwm2_weight_duty_base, S_IWUSR | S_IRUGO,
2576 show_pwm, store_pwm, 1, 6),
2577 SENSOR_ATTR_2(pwm3_weight_duty_base, S_IWUSR | S_IRUGO,
2578 show_pwm, store_pwm, 2, 6),
2579 SENSOR_ATTR_2(pwm4_weight_duty_base, S_IWUSR | S_IRUGO,
2580 show_pwm, store_pwm, 3, 6),
2581 SENSOR_ATTR_2(pwm5_weight_duty_base, S_IWUSR | S_IRUGO,
2582 show_pwm, store_pwm, 4, 6),
2586 show_fan_time(struct device *dev, struct device_attribute *attr, char *buf)
2588 struct nct6775_data *data = nct6775_update_device(dev);
2589 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
2591 int index = sattr->index;
2593 return sprintf(buf, "%d\n",
2594 step_time_from_reg(data->fan_time[index][nr],
2595 data->pwm_mode[nr]));
2599 store_fan_time(struct device *dev, struct device_attribute *attr,
2600 const char *buf, size_t count)
2602 struct nct6775_data *data = dev_get_drvdata(dev);
2603 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
2605 int index = sattr->index;
2609 err = kstrtoul(buf, 10, &val);
2613 val = step_time_to_reg(val, data->pwm_mode[nr]);
2614 mutex_lock(&data->update_lock);
2615 data->fan_time[index][nr] = val;
2616 nct6775_write_value(data, data->REG_FAN_TIME[index][nr], val);
2617 mutex_unlock(&data->update_lock);
2622 show_name(struct device *dev, struct device_attribute *attr, char *buf)
2624 struct nct6775_data *data = dev_get_drvdata(dev);
2626 return sprintf(buf, "%s\n", data->name);
2629 static DEVICE_ATTR(name, S_IRUGO, show_name, NULL);
2631 static SENSOR_DEVICE_ATTR_2(pwm1_stop_time, S_IWUSR | S_IRUGO, show_fan_time,
2632 store_fan_time, 0, 0);
2633 static SENSOR_DEVICE_ATTR_2(pwm2_stop_time, S_IWUSR | S_IRUGO, show_fan_time,
2634 store_fan_time, 1, 0);
2635 static SENSOR_DEVICE_ATTR_2(pwm3_stop_time, S_IWUSR | S_IRUGO, show_fan_time,
2636 store_fan_time, 2, 0);
2637 static SENSOR_DEVICE_ATTR_2(pwm4_stop_time, S_IWUSR | S_IRUGO, show_fan_time,
2638 store_fan_time, 3, 0);
2639 static SENSOR_DEVICE_ATTR_2(pwm5_stop_time, S_IWUSR | S_IRUGO, show_fan_time,
2640 store_fan_time, 4, 0);
2642 static SENSOR_DEVICE_ATTR_2(pwm1_step_up_time, S_IWUSR | S_IRUGO, show_fan_time,
2643 store_fan_time, 0, 1);
2644 static SENSOR_DEVICE_ATTR_2(pwm2_step_up_time, S_IWUSR | S_IRUGO, show_fan_time,
2645 store_fan_time, 1, 1);
2646 static SENSOR_DEVICE_ATTR_2(pwm3_step_up_time, S_IWUSR | S_IRUGO, show_fan_time,
2647 store_fan_time, 2, 1);
2648 static SENSOR_DEVICE_ATTR_2(pwm4_step_up_time, S_IWUSR | S_IRUGO, show_fan_time,
2649 store_fan_time, 3, 1);
2650 static SENSOR_DEVICE_ATTR_2(pwm5_step_up_time, S_IWUSR | S_IRUGO, show_fan_time,
2651 store_fan_time, 4, 1);
2653 static SENSOR_DEVICE_ATTR_2(pwm1_step_down_time, S_IWUSR | S_IRUGO,
2654 show_fan_time, store_fan_time, 0, 2);
2655 static SENSOR_DEVICE_ATTR_2(pwm2_step_down_time, S_IWUSR | S_IRUGO,
2656 show_fan_time, store_fan_time, 1, 2);
2657 static SENSOR_DEVICE_ATTR_2(pwm3_step_down_time, S_IWUSR | S_IRUGO,
2658 show_fan_time, store_fan_time, 2, 2);
2659 static SENSOR_DEVICE_ATTR_2(pwm4_step_down_time, S_IWUSR | S_IRUGO,
2660 show_fan_time, store_fan_time, 3, 2);
2661 static SENSOR_DEVICE_ATTR_2(pwm5_step_down_time, S_IWUSR | S_IRUGO,
2662 show_fan_time, store_fan_time, 4, 2);
2664 static SENSOR_DEVICE_ATTR_2(pwm1_start, S_IWUSR | S_IRUGO, show_pwm,
2666 static SENSOR_DEVICE_ATTR_2(pwm2_start, S_IWUSR | S_IRUGO, show_pwm,
2668 static SENSOR_DEVICE_ATTR_2(pwm3_start, S_IWUSR | S_IRUGO, show_pwm,
2670 static SENSOR_DEVICE_ATTR_2(pwm4_start, S_IWUSR | S_IRUGO, show_pwm,
2672 static SENSOR_DEVICE_ATTR_2(pwm5_start, S_IWUSR | S_IRUGO, show_pwm,
2675 static SENSOR_DEVICE_ATTR_2(pwm1_floor, S_IWUSR | S_IRUGO, show_pwm,
2677 static SENSOR_DEVICE_ATTR_2(pwm2_floor, S_IWUSR | S_IRUGO, show_pwm,
2679 static SENSOR_DEVICE_ATTR_2(pwm3_floor, S_IWUSR | S_IRUGO, show_pwm,
2681 static SENSOR_DEVICE_ATTR_2(pwm4_floor, S_IWUSR | S_IRUGO, show_pwm,
2683 static SENSOR_DEVICE_ATTR_2(pwm5_floor, S_IWUSR | S_IRUGO, show_pwm,
2686 static SENSOR_DEVICE_ATTR_2(pwm1_temp_tolerance, S_IWUSR | S_IRUGO,
2687 show_temp_tolerance, store_temp_tolerance, 0, 0);
2688 static SENSOR_DEVICE_ATTR_2(pwm2_temp_tolerance, S_IWUSR | S_IRUGO,
2689 show_temp_tolerance, store_temp_tolerance, 1, 0);
2690 static SENSOR_DEVICE_ATTR_2(pwm3_temp_tolerance, S_IWUSR | S_IRUGO,
2691 show_temp_tolerance, store_temp_tolerance, 2, 0);
2692 static SENSOR_DEVICE_ATTR_2(pwm4_temp_tolerance, S_IWUSR | S_IRUGO,
2693 show_temp_tolerance, store_temp_tolerance, 3, 0);
2694 static SENSOR_DEVICE_ATTR_2(pwm5_temp_tolerance, S_IWUSR | S_IRUGO,
2695 show_temp_tolerance, store_temp_tolerance, 4, 0);
2697 static SENSOR_DEVICE_ATTR_2(pwm1_crit_temp_tolerance, S_IWUSR | S_IRUGO,
2698 show_temp_tolerance, store_temp_tolerance, 0, 1);
2699 static SENSOR_DEVICE_ATTR_2(pwm2_crit_temp_tolerance, S_IWUSR | S_IRUGO,
2700 show_temp_tolerance, store_temp_tolerance, 1, 1);
2701 static SENSOR_DEVICE_ATTR_2(pwm3_crit_temp_tolerance, S_IWUSR | S_IRUGO,
2702 show_temp_tolerance, store_temp_tolerance, 2, 1);
2703 static SENSOR_DEVICE_ATTR_2(pwm4_crit_temp_tolerance, S_IWUSR | S_IRUGO,
2704 show_temp_tolerance, store_temp_tolerance, 3, 1);
2705 static SENSOR_DEVICE_ATTR_2(pwm5_crit_temp_tolerance, S_IWUSR | S_IRUGO,
2706 show_temp_tolerance, store_temp_tolerance, 4, 1);
2708 /* pwm_max is not supported on all chips */
2709 static struct sensor_device_attribute_2 sda_pwm_max[] = {
2710 SENSOR_ATTR_2(pwm1_max, S_IWUSR | S_IRUGO, show_pwm, store_pwm,
2712 SENSOR_ATTR_2(pwm2_max, S_IWUSR | S_IRUGO, show_pwm, store_pwm,
2714 SENSOR_ATTR_2(pwm3_max, S_IWUSR | S_IRUGO, show_pwm, store_pwm,
2716 SENSOR_ATTR_2(pwm4_max, S_IWUSR | S_IRUGO, show_pwm, store_pwm,
2718 SENSOR_ATTR_2(pwm5_max, S_IWUSR | S_IRUGO, show_pwm, store_pwm,
2722 /* pwm_step is not supported on all chips */
2723 static struct sensor_device_attribute_2 sda_pwm_step[] = {
2724 SENSOR_ATTR_2(pwm1_step, S_IWUSR | S_IRUGO, show_pwm, store_pwm, 0, 4),
2725 SENSOR_ATTR_2(pwm2_step, S_IWUSR | S_IRUGO, show_pwm, store_pwm, 1, 4),
2726 SENSOR_ATTR_2(pwm3_step, S_IWUSR | S_IRUGO, show_pwm, store_pwm, 2, 4),
2727 SENSOR_ATTR_2(pwm4_step, S_IWUSR | S_IRUGO, show_pwm, store_pwm, 3, 4),
2728 SENSOR_ATTR_2(pwm5_step, S_IWUSR | S_IRUGO, show_pwm, store_pwm, 4, 4),
2731 static struct attribute *nct6775_attributes_pwm[5][20] = {
2733 &sensor_dev_attr_pwm1.dev_attr.attr,
2734 &sensor_dev_attr_pwm1_mode.dev_attr.attr,
2735 &sensor_dev_attr_pwm1_enable.dev_attr.attr,
2736 &sensor_dev_attr_pwm1_temp_sel.dev_attr.attr,
2737 &sensor_dev_attr_pwm1_temp_tolerance.dev_attr.attr,
2738 &sensor_dev_attr_pwm1_crit_temp_tolerance.dev_attr.attr,
2739 &sensor_dev_attr_pwm1_target_temp.dev_attr.attr,
2740 &sensor_dev_attr_fan1_target.dev_attr.attr,
2741 &sensor_dev_attr_fan1_tolerance.dev_attr.attr,
2742 &sensor_dev_attr_pwm1_stop_time.dev_attr.attr,
2743 &sensor_dev_attr_pwm1_step_up_time.dev_attr.attr,
2744 &sensor_dev_attr_pwm1_step_down_time.dev_attr.attr,
2745 &sensor_dev_attr_pwm1_start.dev_attr.attr,
2746 &sensor_dev_attr_pwm1_floor.dev_attr.attr,
2747 &sensor_dev_attr_pwm1_weight_temp_sel.dev_attr.attr,
2748 &sensor_dev_attr_pwm1_weight_temp_step.dev_attr.attr,
2749 &sensor_dev_attr_pwm1_weight_temp_step_tol.dev_attr.attr,
2750 &sensor_dev_attr_pwm1_weight_temp_step_base.dev_attr.attr,
2751 &sensor_dev_attr_pwm1_weight_duty_step.dev_attr.attr,
2755 &sensor_dev_attr_pwm2.dev_attr.attr,
2756 &sensor_dev_attr_pwm2_mode.dev_attr.attr,
2757 &sensor_dev_attr_pwm2_enable.dev_attr.attr,
2758 &sensor_dev_attr_pwm2_temp_sel.dev_attr.attr,
2759 &sensor_dev_attr_pwm2_temp_tolerance.dev_attr.attr,
2760 &sensor_dev_attr_pwm2_crit_temp_tolerance.dev_attr.attr,
2761 &sensor_dev_attr_pwm2_target_temp.dev_attr.attr,
2762 &sensor_dev_attr_fan2_target.dev_attr.attr,
2763 &sensor_dev_attr_fan2_tolerance.dev_attr.attr,
2764 &sensor_dev_attr_pwm2_stop_time.dev_attr.attr,
2765 &sensor_dev_attr_pwm2_step_up_time.dev_attr.attr,
2766 &sensor_dev_attr_pwm2_step_down_time.dev_attr.attr,
2767 &sensor_dev_attr_pwm2_start.dev_attr.attr,
2768 &sensor_dev_attr_pwm2_floor.dev_attr.attr,
2769 &sensor_dev_attr_pwm2_weight_temp_sel.dev_attr.attr,
2770 &sensor_dev_attr_pwm2_weight_temp_step.dev_attr.attr,
2771 &sensor_dev_attr_pwm2_weight_temp_step_tol.dev_attr.attr,
2772 &sensor_dev_attr_pwm2_weight_temp_step_base.dev_attr.attr,
2773 &sensor_dev_attr_pwm2_weight_duty_step.dev_attr.attr,
2777 &sensor_dev_attr_pwm3.dev_attr.attr,
2778 &sensor_dev_attr_pwm3_mode.dev_attr.attr,
2779 &sensor_dev_attr_pwm3_enable.dev_attr.attr,
2780 &sensor_dev_attr_pwm3_temp_sel.dev_attr.attr,
2781 &sensor_dev_attr_pwm3_temp_tolerance.dev_attr.attr,
2782 &sensor_dev_attr_pwm3_crit_temp_tolerance.dev_attr.attr,
2783 &sensor_dev_attr_pwm3_target_temp.dev_attr.attr,
2784 &sensor_dev_attr_fan3_target.dev_attr.attr,
2785 &sensor_dev_attr_fan3_tolerance.dev_attr.attr,
2786 &sensor_dev_attr_pwm3_stop_time.dev_attr.attr,
2787 &sensor_dev_attr_pwm3_step_up_time.dev_attr.attr,
2788 &sensor_dev_attr_pwm3_step_down_time.dev_attr.attr,
2789 &sensor_dev_attr_pwm3_start.dev_attr.attr,
2790 &sensor_dev_attr_pwm3_floor.dev_attr.attr,
2791 &sensor_dev_attr_pwm3_weight_temp_sel.dev_attr.attr,
2792 &sensor_dev_attr_pwm3_weight_temp_step.dev_attr.attr,
2793 &sensor_dev_attr_pwm3_weight_temp_step_tol.dev_attr.attr,
2794 &sensor_dev_attr_pwm3_weight_temp_step_base.dev_attr.attr,
2795 &sensor_dev_attr_pwm3_weight_duty_step.dev_attr.attr,
2799 &sensor_dev_attr_pwm4.dev_attr.attr,
2800 &sensor_dev_attr_pwm4_mode.dev_attr.attr,
2801 &sensor_dev_attr_pwm4_enable.dev_attr.attr,
2802 &sensor_dev_attr_pwm4_temp_sel.dev_attr.attr,
2803 &sensor_dev_attr_pwm4_temp_tolerance.dev_attr.attr,
2804 &sensor_dev_attr_pwm4_crit_temp_tolerance.dev_attr.attr,
2805 &sensor_dev_attr_pwm4_target_temp.dev_attr.attr,
2806 &sensor_dev_attr_fan4_target.dev_attr.attr,
2807 &sensor_dev_attr_fan4_tolerance.dev_attr.attr,
2808 &sensor_dev_attr_pwm4_stop_time.dev_attr.attr,
2809 &sensor_dev_attr_pwm4_step_up_time.dev_attr.attr,
2810 &sensor_dev_attr_pwm4_step_down_time.dev_attr.attr,
2811 &sensor_dev_attr_pwm4_start.dev_attr.attr,
2812 &sensor_dev_attr_pwm4_floor.dev_attr.attr,
2813 &sensor_dev_attr_pwm4_weight_temp_sel.dev_attr.attr,
2814 &sensor_dev_attr_pwm4_weight_temp_step.dev_attr.attr,
2815 &sensor_dev_attr_pwm4_weight_temp_step_tol.dev_attr.attr,
2816 &sensor_dev_attr_pwm4_weight_temp_step_base.dev_attr.attr,
2817 &sensor_dev_attr_pwm4_weight_duty_step.dev_attr.attr,
2821 &sensor_dev_attr_pwm5.dev_attr.attr,
2822 &sensor_dev_attr_pwm5_mode.dev_attr.attr,
2823 &sensor_dev_attr_pwm5_enable.dev_attr.attr,
2824 &sensor_dev_attr_pwm5_temp_sel.dev_attr.attr,
2825 &sensor_dev_attr_pwm5_temp_tolerance.dev_attr.attr,
2826 &sensor_dev_attr_pwm5_crit_temp_tolerance.dev_attr.attr,
2827 &sensor_dev_attr_pwm5_target_temp.dev_attr.attr,
2828 &sensor_dev_attr_fan5_target.dev_attr.attr,
2829 &sensor_dev_attr_fan5_tolerance.dev_attr.attr,
2830 &sensor_dev_attr_pwm5_stop_time.dev_attr.attr,
2831 &sensor_dev_attr_pwm5_step_up_time.dev_attr.attr,
2832 &sensor_dev_attr_pwm5_step_down_time.dev_attr.attr,
2833 &sensor_dev_attr_pwm5_start.dev_attr.attr,
2834 &sensor_dev_attr_pwm5_floor.dev_attr.attr,
2835 &sensor_dev_attr_pwm5_weight_temp_sel.dev_attr.attr,
2836 &sensor_dev_attr_pwm5_weight_temp_step.dev_attr.attr,
2837 &sensor_dev_attr_pwm5_weight_temp_step_tol.dev_attr.attr,
2838 &sensor_dev_attr_pwm5_weight_temp_step_base.dev_attr.attr,
2839 &sensor_dev_attr_pwm5_weight_duty_step.dev_attr.attr,
2844 static const struct attribute_group nct6775_group_pwm[5] = {
2845 { .attrs = nct6775_attributes_pwm[0] },
2846 { .attrs = nct6775_attributes_pwm[1] },
2847 { .attrs = nct6775_attributes_pwm[2] },
2848 { .attrs = nct6775_attributes_pwm[3] },
2849 { .attrs = nct6775_attributes_pwm[4] },
2853 show_auto_pwm(struct device *dev, struct device_attribute *attr, char *buf)
2855 struct nct6775_data *data = nct6775_update_device(dev);
2856 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
2858 return sprintf(buf, "%d\n", data->auto_pwm[sattr->nr][sattr->index]);
2862 store_auto_pwm(struct device *dev, struct device_attribute *attr,
2863 const char *buf, size_t count)
2865 struct nct6775_data *data = dev_get_drvdata(dev);
2866 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
2868 int point = sattr->index;
2873 err = kstrtoul(buf, 10, &val);
2879 if (point == data->auto_pwm_num) {
2880 if (data->kind != nct6775 && !val)
2882 if (data->kind != nct6779 && val)
2886 mutex_lock(&data->update_lock);
2887 data->auto_pwm[nr][point] = val;
2888 if (point < data->auto_pwm_num) {
2889 nct6775_write_value(data,
2890 NCT6775_AUTO_PWM(data, nr, point),
2891 data->auto_pwm[nr][point]);
2893 switch (data->kind) {
2895 /* disable if needed (pwm == 0) */
2896 reg = nct6775_read_value(data,
2897 NCT6775_REG_CRITICAL_ENAB[nr]);
2902 nct6775_write_value(data, NCT6775_REG_CRITICAL_ENAB[nr],
2906 break; /* always enabled, nothing to do */
2908 nct6775_write_value(data, NCT6779_REG_CRITICAL_PWM[nr],
2910 reg = nct6775_read_value(data,
2911 NCT6779_REG_CRITICAL_PWM_ENABLE[nr]);
2916 nct6775_write_value(data,
2917 NCT6779_REG_CRITICAL_PWM_ENABLE[nr],
2922 mutex_unlock(&data->update_lock);
2927 show_auto_temp(struct device *dev, struct device_attribute *attr, char *buf)
2929 struct nct6775_data *data = nct6775_update_device(dev);
2930 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
2932 int point = sattr->index;
2935 * We don't know for sure if the temperature is signed or unsigned.
2936 * Assume it is unsigned.
2938 return sprintf(buf, "%d\n", data->auto_temp[nr][point] * 1000);
2942 store_auto_temp(struct device *dev, struct device_attribute *attr,
2943 const char *buf, size_t count)
2945 struct nct6775_data *data = dev_get_drvdata(dev);
2946 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
2948 int point = sattr->index;
2952 err = kstrtoul(buf, 10, &val);
2958 mutex_lock(&data->update_lock);
2959 data->auto_temp[nr][point] = DIV_ROUND_CLOSEST(val, 1000);
2960 if (point < data->auto_pwm_num) {
2961 nct6775_write_value(data,
2962 NCT6775_AUTO_TEMP(data, nr, point),
2963 data->auto_temp[nr][point]);
2965 nct6775_write_value(data, data->REG_CRITICAL_TEMP[nr],
2966 data->auto_temp[nr][point]);
2968 mutex_unlock(&data->update_lock);
2973 * The number of auto-point trip points is chip dependent.
2974 * Need to check support while generating/removing attribute files.
2976 static struct sensor_device_attribute_2 sda_auto_pwm_arrays[] = {
2977 SENSOR_ATTR_2(pwm1_auto_point1_pwm, S_IWUSR | S_IRUGO,
2978 show_auto_pwm, store_auto_pwm, 0, 0),
2979 SENSOR_ATTR_2(pwm1_auto_point1_temp, S_IWUSR | S_IRUGO,
2980 show_auto_temp, store_auto_temp, 0, 0),
2981 SENSOR_ATTR_2(pwm1_auto_point2_pwm, S_IWUSR | S_IRUGO,
2982 show_auto_pwm, store_auto_pwm, 0, 1),
2983 SENSOR_ATTR_2(pwm1_auto_point2_temp, S_IWUSR | S_IRUGO,
2984 show_auto_temp, store_auto_temp, 0, 1),
2985 SENSOR_ATTR_2(pwm1_auto_point3_pwm, S_IWUSR | S_IRUGO,
2986 show_auto_pwm, store_auto_pwm, 0, 2),
2987 SENSOR_ATTR_2(pwm1_auto_point3_temp, S_IWUSR | S_IRUGO,
2988 show_auto_temp, store_auto_temp, 0, 2),
2989 SENSOR_ATTR_2(pwm1_auto_point4_pwm, S_IWUSR | S_IRUGO,
2990 show_auto_pwm, store_auto_pwm, 0, 3),
2991 SENSOR_ATTR_2(pwm1_auto_point4_temp, S_IWUSR | S_IRUGO,
2992 show_auto_temp, store_auto_temp, 0, 3),
2993 SENSOR_ATTR_2(pwm1_auto_point5_pwm, S_IWUSR | S_IRUGO,
2994 show_auto_pwm, store_auto_pwm, 0, 4),
2995 SENSOR_ATTR_2(pwm1_auto_point5_temp, S_IWUSR | S_IRUGO,
2996 show_auto_temp, store_auto_temp, 0, 4),
2997 SENSOR_ATTR_2(pwm1_auto_point6_pwm, S_IWUSR | S_IRUGO,
2998 show_auto_pwm, store_auto_pwm, 0, 5),
2999 SENSOR_ATTR_2(pwm1_auto_point6_temp, S_IWUSR | S_IRUGO,
3000 show_auto_temp, store_auto_temp, 0, 5),
3001 SENSOR_ATTR_2(pwm1_auto_point7_pwm, S_IWUSR | S_IRUGO,
3002 show_auto_pwm, store_auto_pwm, 0, 6),
3003 SENSOR_ATTR_2(pwm1_auto_point7_temp, S_IWUSR | S_IRUGO,
3004 show_auto_temp, store_auto_temp, 0, 6),
3006 SENSOR_ATTR_2(pwm2_auto_point1_pwm, S_IWUSR | S_IRUGO,
3007 show_auto_pwm, store_auto_pwm, 1, 0),
3008 SENSOR_ATTR_2(pwm2_auto_point1_temp, S_IWUSR | S_IRUGO,
3009 show_auto_temp, store_auto_temp, 1, 0),
3010 SENSOR_ATTR_2(pwm2_auto_point2_pwm, S_IWUSR | S_IRUGO,
3011 show_auto_pwm, store_auto_pwm, 1, 1),
3012 SENSOR_ATTR_2(pwm2_auto_point2_temp, S_IWUSR | S_IRUGO,
3013 show_auto_temp, store_auto_temp, 1, 1),
3014 SENSOR_ATTR_2(pwm2_auto_point3_pwm, S_IWUSR | S_IRUGO,
3015 show_auto_pwm, store_auto_pwm, 1, 2),
3016 SENSOR_ATTR_2(pwm2_auto_point3_temp, S_IWUSR | S_IRUGO,
3017 show_auto_temp, store_auto_temp, 1, 2),
3018 SENSOR_ATTR_2(pwm2_auto_point4_pwm, S_IWUSR | S_IRUGO,
3019 show_auto_pwm, store_auto_pwm, 1, 3),
3020 SENSOR_ATTR_2(pwm2_auto_point4_temp, S_IWUSR | S_IRUGO,
3021 show_auto_temp, store_auto_temp, 1, 3),
3022 SENSOR_ATTR_2(pwm2_auto_point5_pwm, S_IWUSR | S_IRUGO,
3023 show_auto_pwm, store_auto_pwm, 1, 4),
3024 SENSOR_ATTR_2(pwm2_auto_point5_temp, S_IWUSR | S_IRUGO,
3025 show_auto_temp, store_auto_temp, 1, 4),
3026 SENSOR_ATTR_2(pwm2_auto_point6_pwm, S_IWUSR | S_IRUGO,
3027 show_auto_pwm, store_auto_pwm, 1, 5),
3028 SENSOR_ATTR_2(pwm2_auto_point6_temp, S_IWUSR | S_IRUGO,
3029 show_auto_temp, store_auto_temp, 1, 5),
3030 SENSOR_ATTR_2(pwm2_auto_point7_pwm, S_IWUSR | S_IRUGO,
3031 show_auto_pwm, store_auto_pwm, 1, 6),
3032 SENSOR_ATTR_2(pwm2_auto_point7_temp, S_IWUSR | S_IRUGO,
3033 show_auto_temp, store_auto_temp, 1, 6),
3035 SENSOR_ATTR_2(pwm3_auto_point1_pwm, S_IWUSR | S_IRUGO,
3036 show_auto_pwm, store_auto_pwm, 2, 0),
3037 SENSOR_ATTR_2(pwm3_auto_point1_temp, S_IWUSR | S_IRUGO,
3038 show_auto_temp, store_auto_temp, 2, 0),
3039 SENSOR_ATTR_2(pwm3_auto_point2_pwm, S_IWUSR | S_IRUGO,
3040 show_auto_pwm, store_auto_pwm, 2, 1),
3041 SENSOR_ATTR_2(pwm3_auto_point2_temp, S_IWUSR | S_IRUGO,
3042 show_auto_temp, store_auto_temp, 2, 1),
3043 SENSOR_ATTR_2(pwm3_auto_point3_pwm, S_IWUSR | S_IRUGO,
3044 show_auto_pwm, store_auto_pwm, 2, 2),
3045 SENSOR_ATTR_2(pwm3_auto_point3_temp, S_IWUSR | S_IRUGO,
3046 show_auto_temp, store_auto_temp, 2, 2),
3047 SENSOR_ATTR_2(pwm3_auto_point4_pwm, S_IWUSR | S_IRUGO,
3048 show_auto_pwm, store_auto_pwm, 2, 3),
3049 SENSOR_ATTR_2(pwm3_auto_point4_temp, S_IWUSR | S_IRUGO,
3050 show_auto_temp, store_auto_temp, 2, 3),
3051 SENSOR_ATTR_2(pwm3_auto_point5_pwm, S_IWUSR | S_IRUGO,
3052 show_auto_pwm, store_auto_pwm, 2, 4),
3053 SENSOR_ATTR_2(pwm3_auto_point5_temp, S_IWUSR | S_IRUGO,
3054 show_auto_temp, store_auto_temp, 2, 4),
3055 SENSOR_ATTR_2(pwm3_auto_point6_pwm, S_IWUSR | S_IRUGO,
3056 show_auto_pwm, store_auto_pwm, 2, 5),
3057 SENSOR_ATTR_2(pwm3_auto_point6_temp, S_IWUSR | S_IRUGO,
3058 show_auto_temp, store_auto_temp, 2, 5),
3059 SENSOR_ATTR_2(pwm3_auto_point7_pwm, S_IWUSR | S_IRUGO,
3060 show_auto_pwm, store_auto_pwm, 2, 6),
3061 SENSOR_ATTR_2(pwm3_auto_point7_temp, S_IWUSR | S_IRUGO,
3062 show_auto_temp, store_auto_temp, 2, 6),
3064 SENSOR_ATTR_2(pwm4_auto_point1_pwm, S_IWUSR | S_IRUGO,
3065 show_auto_pwm, store_auto_pwm, 3, 0),
3066 SENSOR_ATTR_2(pwm4_auto_point1_temp, S_IWUSR | S_IRUGO,
3067 show_auto_temp, store_auto_temp, 3, 0),
3068 SENSOR_ATTR_2(pwm4_auto_point2_pwm, S_IWUSR | S_IRUGO,
3069 show_auto_pwm, store_auto_pwm, 3, 1),
3070 SENSOR_ATTR_2(pwm4_auto_point2_temp, S_IWUSR | S_IRUGO,
3071 show_auto_temp, store_auto_temp, 3, 1),
3072 SENSOR_ATTR_2(pwm4_auto_point3_pwm, S_IWUSR | S_IRUGO,
3073 show_auto_pwm, store_auto_pwm, 3, 2),
3074 SENSOR_ATTR_2(pwm4_auto_point3_temp, S_IWUSR | S_IRUGO,
3075 show_auto_temp, store_auto_temp, 3, 2),
3076 SENSOR_ATTR_2(pwm4_auto_point4_pwm, S_IWUSR | S_IRUGO,
3077 show_auto_pwm, store_auto_pwm, 3, 3),
3078 SENSOR_ATTR_2(pwm4_auto_point4_temp, S_IWUSR | S_IRUGO,
3079 show_auto_temp, store_auto_temp, 3, 3),
3080 SENSOR_ATTR_2(pwm4_auto_point5_pwm, S_IWUSR | S_IRUGO,
3081 show_auto_pwm, store_auto_pwm, 3, 4),
3082 SENSOR_ATTR_2(pwm4_auto_point5_temp, S_IWUSR | S_IRUGO,
3083 show_auto_temp, store_auto_temp, 3, 4),
3084 SENSOR_ATTR_2(pwm4_auto_point6_pwm, S_IWUSR | S_IRUGO,
3085 show_auto_pwm, store_auto_pwm, 3, 5),
3086 SENSOR_ATTR_2(pwm4_auto_point6_temp, S_IWUSR | S_IRUGO,
3087 show_auto_temp, store_auto_temp, 3, 5),
3088 SENSOR_ATTR_2(pwm4_auto_point7_pwm, S_IWUSR | S_IRUGO,
3089 show_auto_pwm, store_auto_pwm, 3, 6),
3090 SENSOR_ATTR_2(pwm4_auto_point7_temp, S_IWUSR | S_IRUGO,
3091 show_auto_temp, store_auto_temp, 3, 6),
3093 SENSOR_ATTR_2(pwm5_auto_point1_pwm, S_IWUSR | S_IRUGO,
3094 show_auto_pwm, store_auto_pwm, 4, 0),
3095 SENSOR_ATTR_2(pwm5_auto_point1_temp, S_IWUSR | S_IRUGO,
3096 show_auto_temp, store_auto_temp, 4, 0),
3097 SENSOR_ATTR_2(pwm5_auto_point2_pwm, S_IWUSR | S_IRUGO,
3098 show_auto_pwm, store_auto_pwm, 4, 1),
3099 SENSOR_ATTR_2(pwm5_auto_point2_temp, S_IWUSR | S_IRUGO,
3100 show_auto_temp, store_auto_temp, 4, 1),
3101 SENSOR_ATTR_2(pwm5_auto_point3_pwm, S_IWUSR | S_IRUGO,
3102 show_auto_pwm, store_auto_pwm, 4, 2),
3103 SENSOR_ATTR_2(pwm5_auto_point3_temp, S_IWUSR | S_IRUGO,
3104 show_auto_temp, store_auto_temp, 4, 2),
3105 SENSOR_ATTR_2(pwm5_auto_point4_pwm, S_IWUSR | S_IRUGO,
3106 show_auto_pwm, store_auto_pwm, 4, 3),
3107 SENSOR_ATTR_2(pwm5_auto_point4_temp, S_IWUSR | S_IRUGO,
3108 show_auto_temp, store_auto_temp, 4, 3),
3109 SENSOR_ATTR_2(pwm5_auto_point5_pwm, S_IWUSR | S_IRUGO,
3110 show_auto_pwm, store_auto_pwm, 4, 4),
3111 SENSOR_ATTR_2(pwm5_auto_point5_temp, S_IWUSR | S_IRUGO,
3112 show_auto_temp, store_auto_temp, 4, 4),
3113 SENSOR_ATTR_2(pwm5_auto_point6_pwm, S_IWUSR | S_IRUGO,
3114 show_auto_pwm, store_auto_pwm, 4, 5),
3115 SENSOR_ATTR_2(pwm5_auto_point6_temp, S_IWUSR | S_IRUGO,
3116 show_auto_temp, store_auto_temp, 4, 5),
3117 SENSOR_ATTR_2(pwm5_auto_point7_pwm, S_IWUSR | S_IRUGO,
3118 show_auto_pwm, store_auto_pwm, 4, 6),
3119 SENSOR_ATTR_2(pwm5_auto_point7_temp, S_IWUSR | S_IRUGO,
3120 show_auto_temp, store_auto_temp, 4, 6),
3124 show_vid(struct device *dev, struct device_attribute *attr, char *buf)
3126 struct nct6775_data *data = dev_get_drvdata(dev);
3127 return sprintf(buf, "%d\n", vid_from_reg(data->vid, data->vrm));
3130 static DEVICE_ATTR(cpu0_vid, S_IRUGO, show_vid, NULL);
3132 /* Case open detection */
3135 clear_caseopen(struct device *dev, struct device_attribute *attr,
3136 const char *buf, size_t count)
3138 struct nct6775_data *data = dev_get_drvdata(dev);
3139 struct nct6775_sio_data *sio_data = dev->platform_data;
3140 int nr = to_sensor_dev_attr(attr)->index - INTRUSION_ALARM_BASE;
3145 if (kstrtoul(buf, 10, &val) || val != 0)
3148 mutex_lock(&data->update_lock);
3151 * Use CR registers to clear caseopen status.
3152 * The CR registers are the same for all chips, and not all chips
3153 * support clearing the caseopen status through "regular" registers.
3155 ret = superio_enter(sio_data->sioreg);
3161 superio_select(sio_data->sioreg, NCT6775_LD_ACPI);
3162 reg = superio_inb(sio_data->sioreg, NCT6775_REG_CR_CASEOPEN_CLR[nr]);
3163 reg |= NCT6775_CR_CASEOPEN_CLR_MASK[nr];
3164 superio_outb(sio_data->sioreg, NCT6775_REG_CR_CASEOPEN_CLR[nr], reg);
3165 reg &= ~NCT6775_CR_CASEOPEN_CLR_MASK[nr];
3166 superio_outb(sio_data->sioreg, NCT6775_REG_CR_CASEOPEN_CLR[nr], reg);
3167 superio_exit(sio_data->sioreg);
3169 data->valid = false; /* Force cache refresh */
3171 mutex_unlock(&data->update_lock);
3175 static struct sensor_device_attribute sda_caseopen[] = {
3176 SENSOR_ATTR(intrusion0_alarm, S_IWUSR | S_IRUGO, show_alarm,
3177 clear_caseopen, INTRUSION_ALARM_BASE),
3178 SENSOR_ATTR(intrusion1_alarm, S_IWUSR | S_IRUGO, show_alarm,
3179 clear_caseopen, INTRUSION_ALARM_BASE + 1),
3183 * Driver and device management
3186 static void nct6775_device_remove_files(struct device *dev)
3189 * some entries in the following arrays may not have been used in
3190 * device_create_file(), but device_remove_file() will ignore them
3193 struct nct6775_data *data = dev_get_drvdata(dev);
3195 for (i = 0; i < data->pwm_num; i++)
3196 sysfs_remove_group(&dev->kobj, &nct6775_group_pwm[i]);
3198 for (i = 0; i < ARRAY_SIZE(sda_pwm_max); i++)
3199 device_remove_file(dev, &sda_pwm_max[i].dev_attr);
3201 for (i = 0; i < ARRAY_SIZE(sda_pwm_step); i++)
3202 device_remove_file(dev, &sda_pwm_step[i].dev_attr);
3204 for (i = 0; i < ARRAY_SIZE(sda_weight_duty_base); i++)
3205 device_remove_file(dev, &sda_weight_duty_base[i].dev_attr);
3207 for (i = 0; i < ARRAY_SIZE(sda_auto_pwm_arrays); i++)
3208 device_remove_file(dev, &sda_auto_pwm_arrays[i].dev_attr);
3210 for (i = 0; i < data->in_num; i++)
3211 sysfs_remove_group(&dev->kobj, &nct6775_group_in[i]);
3213 for (i = 0; i < 5; i++) {
3214 device_remove_file(dev, &sda_fan_input[i].dev_attr);
3215 device_remove_file(dev, &sda_fan_alarm[i].dev_attr);
3216 device_remove_file(dev, &sda_fan_div[i].dev_attr);
3217 device_remove_file(dev, &sda_fan_min[i].dev_attr);
3218 device_remove_file(dev, &sda_fan_pulses[i].dev_attr);
3220 for (i = 0; i < NUM_TEMP; i++) {
3221 if (!(data->have_temp & (1 << i)))
3223 device_remove_file(dev, &sda_temp_input[i].dev_attr);
3224 device_remove_file(dev, &sda_temp_label[i].dev_attr);
3225 device_remove_file(dev, &sda_temp_max[i].dev_attr);
3226 device_remove_file(dev, &sda_temp_max_hyst[i].dev_attr);
3227 device_remove_file(dev, &sda_temp_crit[i].dev_attr);
3228 if (!(data->have_temp_fixed & (1 << i)))
3230 device_remove_file(dev, &sda_temp_type[i].dev_attr);
3231 device_remove_file(dev, &sda_temp_offset[i].dev_attr);
3232 if (i >= NUM_TEMP_ALARM)
3234 device_remove_file(dev, &sda_temp_alarm[i].dev_attr);
3237 device_remove_file(dev, &sda_caseopen[0].dev_attr);
3238 device_remove_file(dev, &sda_caseopen[1].dev_attr);
3240 device_remove_file(dev, &dev_attr_name);
3241 device_remove_file(dev, &dev_attr_cpu0_vid);
3244 /* Get the monitoring functions started */
3245 static inline void nct6775_init_device(struct nct6775_data *data)
3250 /* Start monitoring if needed */
3251 if (data->REG_CONFIG) {
3252 tmp = nct6775_read_value(data, data->REG_CONFIG);
3254 nct6775_write_value(data, data->REG_CONFIG, tmp | 0x01);
3257 /* Enable temperature sensors if needed */
3258 for (i = 0; i < NUM_TEMP; i++) {
3259 if (!(data->have_temp & (1 << i)))
3261 if (!data->reg_temp_config[i])
3263 tmp = nct6775_read_value(data, data->reg_temp_config[i]);
3265 nct6775_write_value(data, data->reg_temp_config[i],
3269 /* Enable VBAT monitoring if needed */
3270 tmp = nct6775_read_value(data, data->REG_VBAT);
3272 nct6775_write_value(data, data->REG_VBAT, tmp | 0x01);
3274 diode = nct6775_read_value(data, data->REG_DIODE);
3276 for (i = 0; i < data->temp_fixed_num; i++) {
3277 if (!(data->have_temp_fixed & (1 << i)))
3279 if ((tmp & (0x02 << i))) /* diode */
3280 data->temp_type[i] = 3 - ((diode >> i) & 0x02);
3281 else /* thermistor */
3282 data->temp_type[i] = 4;
3287 nct6775_check_fan_inputs(const struct nct6775_sio_data *sio_data,
3288 struct nct6775_data *data)
3291 bool fan3pin, fan3min, fan4pin, fan4min, fan5pin;
3292 bool pwm3pin, pwm4pin, pwm5pin;
3295 ret = superio_enter(sio_data->sioreg);
3299 /* fan4 and fan5 share some pins with the GPIO and serial flash */
3300 if (data->kind == nct6775) {
3301 regval = superio_inb(sio_data->sioreg, 0x2c);
3303 fan3pin = regval & (1 << 6);
3305 pwm3pin = regval & (1 << 7);
3307 /* On NCT6775, fan4 shares pins with the fdc interface */
3308 fan4pin = !(superio_inb(sio_data->sioreg, 0x2A) & 0x80);
3313 } else if (data->kind == nct6776) {
3314 bool gpok = superio_inb(sio_data->sioreg, 0x27) & 0x80;
3316 superio_select(sio_data->sioreg, NCT6775_LD_HWM);
3317 regval = superio_inb(sio_data->sioreg, SIO_REG_ENABLE);
3322 fan3pin = !(superio_inb(sio_data->sioreg, 0x24) & 0x40);
3327 fan4pin = superio_inb(sio_data->sioreg, 0x1C) & 0x01;
3332 fan5pin = superio_inb(sio_data->sioreg, 0x1C) & 0x02;
3339 } else { /* NCT6779D */
3340 regval = superio_inb(sio_data->sioreg, 0x1c);
3342 fan3pin = !(regval & (1 << 5));
3343 fan4pin = !(regval & (1 << 6));
3344 fan5pin = !(regval & (1 << 7));
3346 pwm3pin = !(regval & (1 << 0));
3347 pwm4pin = !(regval & (1 << 1));
3348 pwm5pin = !(regval & (1 << 2));
3354 superio_exit(sio_data->sioreg);
3356 data->has_fan = data->has_fan_min = 0x03; /* fan1 and fan2 */
3357 data->has_fan |= fan3pin << 2;
3358 data->has_fan_min |= fan3min << 2;
3360 data->has_fan |= (fan4pin << 3) | (fan5pin << 4);
3361 data->has_fan_min |= (fan4min << 3) | (fan5pin << 4);
3363 data->has_pwm = 0x03 | (pwm3pin << 2) | (pwm4pin << 3) | (pwm5pin << 4);
3368 static void add_temp_sensors(struct nct6775_data *data, const u16 *regp,
3369 int *available, int *mask)
3374 for (i = 0; i < data->pwm_num && *available; i++) {
3379 src = nct6775_read_value(data, regp[i]);
3381 if (!src || (*mask & (1 << src)))
3383 if (src >= data->temp_label_num ||
3384 !strlen(data->temp_label[src]))
3387 index = __ffs(*available);
3388 nct6775_write_value(data, data->REG_TEMP_SOURCE[index], src);
3389 *available &= ~(1 << index);
3394 static int nct6775_probe(struct platform_device *pdev)
3396 struct device *dev = &pdev->dev;
3397 struct nct6775_sio_data *sio_data = dev->platform_data;
3398 struct nct6775_data *data;
3399 struct resource *res;
3401 int src, mask, available;
3402 const u16 *reg_temp, *reg_temp_over, *reg_temp_hyst, *reg_temp_config;
3403 const u16 *reg_temp_alternate, *reg_temp_crit;
3405 bool have_vid = false;
3408 res = platform_get_resource(pdev, IORESOURCE_IO, 0);
3409 if (!devm_request_region(&pdev->dev, res->start, IOREGION_LENGTH,
3413 data = devm_kzalloc(&pdev->dev, sizeof(struct nct6775_data),
3418 data->kind = sio_data->kind;
3419 data->addr = res->start;
3420 mutex_init(&data->lock);
3421 mutex_init(&data->update_lock);
3422 data->name = nct6775_device_names[data->kind];
3423 data->bank = 0xff; /* Force initial bank selection */
3424 platform_set_drvdata(pdev, data);
3426 switch (data->kind) {
3430 data->auto_pwm_num = 6;
3431 data->has_fan_div = true;
3432 data->temp_fixed_num = 3;
3434 data->ALARM_BITS = NCT6775_ALARM_BITS;
3436 data->fan_from_reg = fan_from_reg16;
3437 data->fan_from_reg_min = fan_from_reg8;
3438 data->target_temp_mask = 0x7f;
3439 data->tolerance_mask = 0x0f;
3440 data->speed_tolerance_limit = 15;
3442 data->temp_label = nct6775_temp_label;
3443 data->temp_label_num = ARRAY_SIZE(nct6775_temp_label);
3445 data->REG_CONFIG = NCT6775_REG_CONFIG;
3446 data->REG_VBAT = NCT6775_REG_VBAT;
3447 data->REG_DIODE = NCT6775_REG_DIODE;
3448 data->REG_VIN = NCT6775_REG_IN;
3449 data->REG_IN_MINMAX[0] = NCT6775_REG_IN_MIN;
3450 data->REG_IN_MINMAX[1] = NCT6775_REG_IN_MAX;
3451 data->REG_TARGET = NCT6775_REG_TARGET;
3452 data->REG_FAN = NCT6775_REG_FAN;
3453 data->REG_FAN_MODE = NCT6775_REG_FAN_MODE;
3454 data->REG_FAN_MIN = NCT6775_REG_FAN_MIN;
3455 data->REG_FAN_PULSES = NCT6775_REG_FAN_PULSES;
3456 data->REG_FAN_TIME[0] = NCT6775_REG_FAN_STOP_TIME;
3457 data->REG_FAN_TIME[1] = NCT6775_REG_FAN_STEP_UP_TIME;
3458 data->REG_FAN_TIME[2] = NCT6775_REG_FAN_STEP_DOWN_TIME;
3459 data->REG_PWM[0] = NCT6775_REG_PWM;
3460 data->REG_PWM[1] = NCT6775_REG_FAN_START_OUTPUT;
3461 data->REG_PWM[2] = NCT6775_REG_FAN_STOP_OUTPUT;
3462 data->REG_PWM[3] = NCT6775_REG_FAN_MAX_OUTPUT;
3463 data->REG_PWM[4] = NCT6775_REG_FAN_STEP_OUTPUT;
3464 data->REG_PWM[5] = NCT6775_REG_WEIGHT_DUTY_STEP;
3465 data->REG_PWM_READ = NCT6775_REG_PWM_READ;
3466 data->REG_PWM_MODE = NCT6775_REG_PWM_MODE;
3467 data->PWM_MODE_MASK = NCT6775_PWM_MODE_MASK;
3468 data->REG_AUTO_TEMP = NCT6775_REG_AUTO_TEMP;
3469 data->REG_AUTO_PWM = NCT6775_REG_AUTO_PWM;
3470 data->REG_CRITICAL_TEMP = NCT6775_REG_CRITICAL_TEMP;
3471 data->REG_CRITICAL_TEMP_TOLERANCE
3472 = NCT6775_REG_CRITICAL_TEMP_TOLERANCE;
3473 data->REG_TEMP_OFFSET = NCT6775_REG_TEMP_OFFSET;
3474 data->REG_TEMP_SOURCE = NCT6775_REG_TEMP_SOURCE;
3475 data->REG_TEMP_SEL = NCT6775_REG_TEMP_SEL;
3476 data->REG_WEIGHT_TEMP_SEL = NCT6775_REG_WEIGHT_TEMP_SEL;
3477 data->REG_WEIGHT_TEMP[0] = NCT6775_REG_WEIGHT_TEMP_STEP;
3478 data->REG_WEIGHT_TEMP[1] = NCT6775_REG_WEIGHT_TEMP_STEP_TOL;
3479 data->REG_WEIGHT_TEMP[2] = NCT6775_REG_WEIGHT_TEMP_BASE;
3480 data->REG_ALARM = NCT6775_REG_ALARM;
3482 reg_temp = NCT6775_REG_TEMP;
3483 num_reg_temp = ARRAY_SIZE(NCT6775_REG_TEMP);
3484 reg_temp_over = NCT6775_REG_TEMP_OVER;
3485 reg_temp_hyst = NCT6775_REG_TEMP_HYST;
3486 reg_temp_config = NCT6775_REG_TEMP_CONFIG;
3487 reg_temp_alternate = NCT6775_REG_TEMP_ALTERNATE;
3488 reg_temp_crit = NCT6775_REG_TEMP_CRIT;
3494 data->auto_pwm_num = 4;
3495 data->has_fan_div = false;
3496 data->temp_fixed_num = 3;
3498 data->ALARM_BITS = NCT6776_ALARM_BITS;
3500 data->fan_from_reg = fan_from_reg13;
3501 data->fan_from_reg_min = fan_from_reg13;
3502 data->target_temp_mask = 0xff;
3503 data->tolerance_mask = 0x07;
3504 data->speed_tolerance_limit = 63;
3506 data->temp_label = nct6776_temp_label;
3507 data->temp_label_num = ARRAY_SIZE(nct6776_temp_label);
3509 data->REG_CONFIG = NCT6775_REG_CONFIG;
3510 data->REG_VBAT = NCT6775_REG_VBAT;
3511 data->REG_DIODE = NCT6775_REG_DIODE;
3512 data->REG_VIN = NCT6775_REG_IN;
3513 data->REG_IN_MINMAX[0] = NCT6775_REG_IN_MIN;
3514 data->REG_IN_MINMAX[1] = NCT6775_REG_IN_MAX;
3515 data->REG_TARGET = NCT6775_REG_TARGET;
3516 data->REG_FAN = NCT6775_REG_FAN;
3517 data->REG_FAN_MODE = NCT6775_REG_FAN_MODE;
3518 data->REG_FAN_MIN = NCT6776_REG_FAN_MIN;
3519 data->REG_FAN_PULSES = NCT6776_REG_FAN_PULSES;
3520 data->REG_FAN_TIME[0] = NCT6775_REG_FAN_STOP_TIME;
3521 data->REG_FAN_TIME[1] = NCT6775_REG_FAN_STEP_UP_TIME;
3522 data->REG_FAN_TIME[2] = NCT6775_REG_FAN_STEP_DOWN_TIME;
3523 data->REG_TOLERANCE_H = NCT6776_REG_TOLERANCE_H;
3524 data->REG_PWM[0] = NCT6775_REG_PWM;
3525 data->REG_PWM[1] = NCT6775_REG_FAN_START_OUTPUT;
3526 data->REG_PWM[2] = NCT6775_REG_FAN_STOP_OUTPUT;
3527 data->REG_PWM[5] = NCT6775_REG_WEIGHT_DUTY_STEP;
3528 data->REG_PWM[6] = NCT6776_REG_WEIGHT_DUTY_BASE;
3529 data->REG_PWM_READ = NCT6775_REG_PWM_READ;
3530 data->REG_PWM_MODE = NCT6776_REG_PWM_MODE;
3531 data->PWM_MODE_MASK = NCT6776_PWM_MODE_MASK;
3532 data->REG_AUTO_TEMP = NCT6775_REG_AUTO_TEMP;
3533 data->REG_AUTO_PWM = NCT6775_REG_AUTO_PWM;
3534 data->REG_CRITICAL_TEMP = NCT6775_REG_CRITICAL_TEMP;
3535 data->REG_CRITICAL_TEMP_TOLERANCE
3536 = NCT6775_REG_CRITICAL_TEMP_TOLERANCE;
3537 data->REG_TEMP_OFFSET = NCT6775_REG_TEMP_OFFSET;
3538 data->REG_TEMP_SOURCE = NCT6775_REG_TEMP_SOURCE;
3539 data->REG_TEMP_SEL = NCT6775_REG_TEMP_SEL;
3540 data->REG_WEIGHT_TEMP_SEL = NCT6775_REG_WEIGHT_TEMP_SEL;
3541 data->REG_WEIGHT_TEMP[0] = NCT6775_REG_WEIGHT_TEMP_STEP;
3542 data->REG_WEIGHT_TEMP[1] = NCT6775_REG_WEIGHT_TEMP_STEP_TOL;
3543 data->REG_WEIGHT_TEMP[2] = NCT6775_REG_WEIGHT_TEMP_BASE;
3544 data->REG_ALARM = NCT6775_REG_ALARM;
3546 reg_temp = NCT6775_REG_TEMP;
3547 num_reg_temp = ARRAY_SIZE(NCT6775_REG_TEMP);
3548 reg_temp_over = NCT6775_REG_TEMP_OVER;
3549 reg_temp_hyst = NCT6775_REG_TEMP_HYST;
3550 reg_temp_config = NCT6776_REG_TEMP_CONFIG;
3551 reg_temp_alternate = NCT6776_REG_TEMP_ALTERNATE;
3552 reg_temp_crit = NCT6776_REG_TEMP_CRIT;
3558 data->auto_pwm_num = 4;
3559 data->has_fan_div = false;
3560 data->temp_fixed_num = 6;
3562 data->ALARM_BITS = NCT6779_ALARM_BITS;
3564 data->fan_from_reg = fan_from_reg13;
3565 data->fan_from_reg_min = fan_from_reg13;
3566 data->target_temp_mask = 0xff;
3567 data->tolerance_mask = 0x07;
3568 data->speed_tolerance_limit = 63;
3570 data->temp_label = nct6779_temp_label;
3571 data->temp_label_num = ARRAY_SIZE(nct6779_temp_label);
3573 data->REG_CONFIG = NCT6775_REG_CONFIG;
3574 data->REG_VBAT = NCT6775_REG_VBAT;
3575 data->REG_DIODE = NCT6775_REG_DIODE;
3576 data->REG_VIN = NCT6779_REG_IN;
3577 data->REG_IN_MINMAX[0] = NCT6775_REG_IN_MIN;
3578 data->REG_IN_MINMAX[1] = NCT6775_REG_IN_MAX;
3579 data->REG_TARGET = NCT6775_REG_TARGET;
3580 data->REG_FAN = NCT6779_REG_FAN;
3581 data->REG_FAN_MODE = NCT6775_REG_FAN_MODE;
3582 data->REG_FAN_MIN = NCT6776_REG_FAN_MIN;
3583 data->REG_FAN_PULSES = NCT6779_REG_FAN_PULSES;
3584 data->REG_FAN_TIME[0] = NCT6775_REG_FAN_STOP_TIME;
3585 data->REG_FAN_TIME[1] = NCT6775_REG_FAN_STEP_UP_TIME;
3586 data->REG_FAN_TIME[2] = NCT6775_REG_FAN_STEP_DOWN_TIME;
3587 data->REG_TOLERANCE_H = NCT6776_REG_TOLERANCE_H;
3588 data->REG_PWM[0] = NCT6775_REG_PWM;
3589 data->REG_PWM[1] = NCT6775_REG_FAN_START_OUTPUT;
3590 data->REG_PWM[2] = NCT6775_REG_FAN_STOP_OUTPUT;
3591 data->REG_PWM[5] = NCT6775_REG_WEIGHT_DUTY_STEP;
3592 data->REG_PWM[6] = NCT6776_REG_WEIGHT_DUTY_BASE;
3593 data->REG_PWM_READ = NCT6775_REG_PWM_READ;
3594 data->REG_PWM_MODE = NCT6776_REG_PWM_MODE;
3595 data->PWM_MODE_MASK = NCT6776_PWM_MODE_MASK;
3596 data->REG_AUTO_TEMP = NCT6775_REG_AUTO_TEMP;
3597 data->REG_AUTO_PWM = NCT6775_REG_AUTO_PWM;
3598 data->REG_CRITICAL_TEMP = NCT6775_REG_CRITICAL_TEMP;
3599 data->REG_CRITICAL_TEMP_TOLERANCE
3600 = NCT6775_REG_CRITICAL_TEMP_TOLERANCE;
3601 data->REG_TEMP_OFFSET = NCT6779_REG_TEMP_OFFSET;
3602 data->REG_TEMP_SOURCE = NCT6775_REG_TEMP_SOURCE;
3603 data->REG_TEMP_SEL = NCT6775_REG_TEMP_SEL;
3604 data->REG_WEIGHT_TEMP_SEL = NCT6775_REG_WEIGHT_TEMP_SEL;
3605 data->REG_WEIGHT_TEMP[0] = NCT6775_REG_WEIGHT_TEMP_STEP;
3606 data->REG_WEIGHT_TEMP[1] = NCT6775_REG_WEIGHT_TEMP_STEP_TOL;
3607 data->REG_WEIGHT_TEMP[2] = NCT6775_REG_WEIGHT_TEMP_BASE;
3608 data->REG_ALARM = NCT6779_REG_ALARM;
3610 reg_temp = NCT6779_REG_TEMP;
3611 num_reg_temp = ARRAY_SIZE(NCT6779_REG_TEMP);
3612 reg_temp_over = NCT6779_REG_TEMP_OVER;
3613 reg_temp_hyst = NCT6779_REG_TEMP_HYST;
3614 reg_temp_config = NCT6779_REG_TEMP_CONFIG;
3615 reg_temp_alternate = NCT6779_REG_TEMP_ALTERNATE;
3616 reg_temp_crit = NCT6779_REG_TEMP_CRIT;
3622 data->have_in = (1 << data->in_num) - 1;
3623 data->have_temp = 0;
3626 * On some boards, not all available temperature sources are monitored,
3627 * even though some of the monitoring registers are unused.
3628 * Get list of unused monitoring registers, then detect if any fan
3629 * controls are configured to use unmonitored temperature sources.
3630 * If so, assign the unmonitored temperature sources to available
3631 * monitoring registers.
3635 for (i = 0; i < num_reg_temp; i++) {
3636 if (reg_temp[i] == 0)
3639 src = nct6775_read_value(data, data->REG_TEMP_SOURCE[i]) & 0x1f;
3640 if (!src || (mask & (1 << src)))
3641 available |= 1 << i;
3647 * Now find unmonitored temperature registers and enable monitoring
3648 * if additional monitoring registers are available.
3650 add_temp_sensors(data, data->REG_TEMP_SEL, &available, &mask);
3651 add_temp_sensors(data, data->REG_WEIGHT_TEMP_SEL, &available, &mask);
3654 s = NUM_TEMP_FIXED; /* First dynamic temperature attribute */
3655 for (i = 0; i < num_reg_temp; i++) {
3656 if (reg_temp[i] == 0)
3659 src = nct6775_read_value(data, data->REG_TEMP_SOURCE[i]) & 0x1f;
3660 if (!src || (mask & (1 << src)))
3663 if (src >= data->temp_label_num ||
3664 !strlen(data->temp_label[src])) {
3666 "Invalid temperature source %d at index %d, source register 0x%x, temp register 0x%x\n",
3667 src, i, data->REG_TEMP_SOURCE[i], reg_temp[i]);
3673 /* Use fixed index for SYSTIN(1), CPUTIN(2), AUXTIN(3) */
3674 if (src <= data->temp_fixed_num) {
3675 data->have_temp |= 1 << (src - 1);
3676 data->have_temp_fixed |= 1 << (src - 1);
3677 data->reg_temp[0][src - 1] = reg_temp[i];
3678 data->reg_temp[1][src - 1] = reg_temp_over[i];
3679 data->reg_temp[2][src - 1] = reg_temp_hyst[i];
3680 data->reg_temp_config[src - 1] = reg_temp_config[i];
3681 data->temp_src[src - 1] = src;
3688 /* Use dynamic index for other sources */
3689 data->have_temp |= 1 << s;
3690 data->reg_temp[0][s] = reg_temp[i];
3691 data->reg_temp[1][s] = reg_temp_over[i];
3692 data->reg_temp[2][s] = reg_temp_hyst[i];
3693 data->reg_temp_config[s] = reg_temp_config[i];
3694 if (reg_temp_crit[src - 1])
3695 data->reg_temp[3][s] = reg_temp_crit[src - 1];
3697 data->temp_src[s] = src;
3701 #ifdef USE_ALTERNATE
3703 * Go through the list of alternate temp registers and enable
3705 * The temperature is already monitored if the respective bit in <mask>
3708 for (i = 0; i < data->temp_label_num - 1; i++) {
3709 if (!reg_temp_alternate[i])
3711 if (mask & (1 << (i + 1)))
3713 if (i < data->temp_fixed_num) {
3714 if (data->have_temp & (1 << i))
3716 data->have_temp |= 1 << i;
3717 data->have_temp_fixed |= 1 << i;
3718 data->reg_temp[0][i] = reg_temp_alternate[i];
3719 data->reg_temp[1][i] = reg_temp_over[i];
3720 data->reg_temp[2][i] = reg_temp_hyst[i];
3721 data->temp_src[i] = i + 1;
3725 if (s >= NUM_TEMP) /* Abort if no more space */
3728 data->have_temp |= 1 << s;
3729 data->reg_temp[0][s] = reg_temp_alternate[i];
3730 data->temp_src[s] = i + 1;
3733 #endif /* USE_ALTERNATE */
3735 switch (data->kind) {
3740 * On NCT6776, AUXTIN and VIN3 pins are shared.
3741 * Only way to detect it is to check if AUXTIN is used
3742 * as a temperature source, and if that source is
3745 * If that is the case, disable in6, which reports VIN3.
3746 * Otherwise disable temp3.
3748 if (data->have_temp & (1 << 2)) {
3749 u8 reg = nct6775_read_value(data,
3750 data->reg_temp_config[2]);
3752 data->have_temp &= ~(1 << 2);
3754 data->have_in &= ~(1 << 6);
3765 * There does not seem to be a clean way to detect if VINx or
3766 * AUXTINx is active, so for keep both sensor types enabled
3772 /* Initialize the chip */
3773 nct6775_init_device(data);
3775 err = superio_enter(sio_data->sioreg);
3779 cr2a = superio_inb(sio_data->sioreg, 0x2a);
3780 switch (data->kind) {
3782 have_vid = (cr2a & 0x40);
3785 have_vid = (cr2a & 0x60) == 0x40;
3793 * We can get the VID input values directly at logical device D 0xe3.
3796 superio_select(sio_data->sioreg, NCT6775_LD_VID);
3797 data->vid = superio_inb(sio_data->sioreg, 0xe3);
3798 data->vrm = vid_which_vrm();
3804 superio_select(sio_data->sioreg, NCT6775_LD_HWM);
3805 tmp = superio_inb(sio_data->sioreg,
3806 NCT6775_REG_CR_FAN_DEBOUNCE);
3807 switch (data->kind) {
3816 superio_outb(sio_data->sioreg, NCT6775_REG_CR_FAN_DEBOUNCE,
3818 dev_info(&pdev->dev, "Enabled fan debounce for chip %s\n",
3822 superio_exit(sio_data->sioreg);
3825 err = device_create_file(dev, &dev_attr_cpu0_vid);
3830 err = nct6775_check_fan_inputs(sio_data, data);
3834 /* Read fan clock dividers immediately */
3835 nct6775_init_fan_common(dev, data);
3837 /* Register sysfs hooks */
3838 for (i = 0; i < data->pwm_num; i++) {
3839 if (!(data->has_pwm & (1 << i)))
3842 err = sysfs_create_group(&dev->kobj, &nct6775_group_pwm[i]);
3846 if (data->REG_PWM[3]) {
3847 err = device_create_file(dev,
3848 &sda_pwm_max[i].dev_attr);
3852 if (data->REG_PWM[4]) {
3853 err = device_create_file(dev,
3854 &sda_pwm_step[i].dev_attr);
3858 if (data->REG_PWM[6]) {
3859 err = device_create_file(dev,
3860 &sda_weight_duty_base[i].dev_attr);
3865 for (i = 0; i < ARRAY_SIZE(sda_auto_pwm_arrays); i++) {
3866 struct sensor_device_attribute_2 *attr =
3867 &sda_auto_pwm_arrays[i];
3869 if (!(data->has_pwm & (1 << attr->nr)))
3871 if (attr->index > data->auto_pwm_num)
3873 err = device_create_file(dev, &attr->dev_attr);
3878 for (i = 0; i < data->in_num; i++) {
3879 if (!(data->have_in & (1 << i)))
3881 err = sysfs_create_group(&dev->kobj, &nct6775_group_in[i]);
3886 for (i = 0; i < 5; i++) {
3887 if (data->has_fan & (1 << i)) {
3888 err = device_create_file(dev,
3889 &sda_fan_input[i].dev_attr);
3892 err = device_create_file(dev,
3893 &sda_fan_alarm[i].dev_attr);
3896 if (data->kind != nct6776 &&
3897 data->kind != nct6779) {
3898 err = device_create_file(dev,
3899 &sda_fan_div[i].dev_attr);
3903 if (data->has_fan_min & (1 << i)) {
3904 err = device_create_file(dev,
3905 &sda_fan_min[i].dev_attr);
3909 err = device_create_file(dev,
3910 &sda_fan_pulses[i].dev_attr);
3916 for (i = 0; i < NUM_TEMP; i++) {
3917 if (!(data->have_temp & (1 << i)))
3919 err = device_create_file(dev, &sda_temp_input[i].dev_attr);
3922 if (data->temp_label) {
3923 err = device_create_file(dev,
3924 &sda_temp_label[i].dev_attr);
3928 if (data->reg_temp[1][i]) {
3929 err = device_create_file(dev,
3930 &sda_temp_max[i].dev_attr);
3934 if (data->reg_temp[2][i]) {
3935 err = device_create_file(dev,
3936 &sda_temp_max_hyst[i].dev_attr);
3940 if (data->reg_temp[3][i]) {
3941 err = device_create_file(dev,
3942 &sda_temp_crit[i].dev_attr);
3946 if (!(data->have_temp_fixed & (1 << i)))
3948 err = device_create_file(dev, &sda_temp_type[i].dev_attr);
3951 err = device_create_file(dev, &sda_temp_offset[i].dev_attr);
3954 if (i >= NUM_TEMP_ALARM ||
3955 data->ALARM_BITS[TEMP_ALARM_BASE + i] < 0)
3957 err = device_create_file(dev, &sda_temp_alarm[i].dev_attr);
3962 for (i = 0; i < ARRAY_SIZE(sda_caseopen); i++) {
3963 if (data->ALARM_BITS[INTRUSION_ALARM_BASE + i] < 0)
3965 err = device_create_file(dev, &sda_caseopen[i].dev_attr);
3970 err = device_create_file(dev, &dev_attr_name);
3974 data->hwmon_dev = hwmon_device_register(dev);
3975 if (IS_ERR(data->hwmon_dev)) {
3976 err = PTR_ERR(data->hwmon_dev);
3983 nct6775_device_remove_files(dev);
3987 static int nct6775_remove(struct platform_device *pdev)
3989 struct nct6775_data *data = platform_get_drvdata(pdev);
3991 hwmon_device_unregister(data->hwmon_dev);
3992 nct6775_device_remove_files(&pdev->dev);
3998 static int nct6775_suspend(struct device *dev)
4000 struct nct6775_data *data = nct6775_update_device(dev);
4001 struct nct6775_sio_data *sio_data = dev->platform_data;
4003 mutex_lock(&data->update_lock);
4004 data->vbat = nct6775_read_value(data, data->REG_VBAT);
4005 if (sio_data->kind == nct6775) {
4006 data->fandiv1 = nct6775_read_value(data, NCT6775_REG_FANDIV1);
4007 data->fandiv2 = nct6775_read_value(data, NCT6775_REG_FANDIV2);
4009 mutex_unlock(&data->update_lock);
4014 static int nct6775_resume(struct device *dev)
4016 struct nct6775_data *data = dev_get_drvdata(dev);
4017 struct nct6775_sio_data *sio_data = dev->platform_data;
4020 mutex_lock(&data->update_lock);
4021 data->bank = 0xff; /* Force initial bank selection */
4023 /* Restore limits */
4024 for (i = 0; i < data->in_num; i++) {
4025 if (!(data->have_in & (1 << i)))
4028 nct6775_write_value(data, data->REG_IN_MINMAX[0][i],
4030 nct6775_write_value(data, data->REG_IN_MINMAX[1][i],
4034 for (i = 0; i < 5; i++) {
4035 if (!(data->has_fan_min & (1 << i)))
4038 nct6775_write_value(data, data->REG_FAN_MIN[i],
4042 for (i = 0; i < NUM_TEMP; i++) {
4043 if (!(data->have_temp & (1 << i)))
4046 for (j = 1; j < 4; j++)
4047 if (data->reg_temp[j][i])
4048 nct6775_write_temp(data, data->reg_temp[j][i],
4052 /* Restore other settings */
4053 nct6775_write_value(data, data->REG_VBAT, data->vbat);
4054 if (sio_data->kind == nct6775) {
4055 nct6775_write_value(data, NCT6775_REG_FANDIV1, data->fandiv1);
4056 nct6775_write_value(data, NCT6775_REG_FANDIV2, data->fandiv2);
4059 /* Force re-reading all values */
4060 data->valid = false;
4061 mutex_unlock(&data->update_lock);
4066 static const struct dev_pm_ops nct6775_dev_pm_ops = {
4067 .suspend = nct6775_suspend,
4068 .resume = nct6775_resume,
4071 #define NCT6775_DEV_PM_OPS (&nct6775_dev_pm_ops)
4073 #define NCT6775_DEV_PM_OPS NULL
4074 #endif /* CONFIG_PM */
4076 static struct platform_driver nct6775_driver = {
4078 .owner = THIS_MODULE,
4080 .pm = NCT6775_DEV_PM_OPS,
4082 .probe = nct6775_probe,
4083 .remove = nct6775_remove,
4086 /* nct6775_find() looks for a '627 in the Super-I/O config space */
4087 static int __init nct6775_find(int sioaddr, unsigned short *addr,
4088 struct nct6775_sio_data *sio_data)
4090 static const char sio_name_NCT6775[] __initconst = "NCT6775F";
4091 static const char sio_name_NCT6776[] __initconst = "NCT6776F";
4092 static const char sio_name_NCT6779[] __initconst = "NCT6779D";
4095 const char *sio_name;
4098 err = superio_enter(sioaddr);
4105 val = (superio_inb(sioaddr, SIO_REG_DEVID) << 8)
4106 | superio_inb(sioaddr, SIO_REG_DEVID + 1);
4107 switch (val & SIO_ID_MASK) {
4108 case SIO_NCT6775_ID:
4109 sio_data->kind = nct6775;
4110 sio_name = sio_name_NCT6775;
4112 case SIO_NCT6776_ID:
4113 sio_data->kind = nct6776;
4114 sio_name = sio_name_NCT6776;
4116 case SIO_NCT6779_ID:
4117 sio_data->kind = nct6779;
4118 sio_name = sio_name_NCT6779;
4122 pr_debug("unsupported chip ID: 0x%04x\n", val);
4123 superio_exit(sioaddr);
4127 /* We have a known chip, find the HWM I/O address */
4128 superio_select(sioaddr, NCT6775_LD_HWM);
4129 val = (superio_inb(sioaddr, SIO_REG_ADDR) << 8)
4130 | superio_inb(sioaddr, SIO_REG_ADDR + 1);
4131 *addr = val & IOREGION_ALIGNMENT;
4133 pr_err("Refusing to enable a Super-I/O device with a base I/O port 0\n");
4134 superio_exit(sioaddr);
4138 /* Activate logical device if needed */
4139 val = superio_inb(sioaddr, SIO_REG_ENABLE);
4140 if (!(val & 0x01)) {
4141 pr_warn("Forcibly enabling Super-I/O. Sensor is probably unusable.\n");
4142 superio_outb(sioaddr, SIO_REG_ENABLE, val | 0x01);
4145 superio_exit(sioaddr);
4146 pr_info("Found %s chip at %#x\n", sio_name, *addr);
4147 sio_data->sioreg = sioaddr;
4153 * when Super-I/O functions move to a separate file, the Super-I/O
4154 * bus will manage the lifetime of the device and this module will only keep
4155 * track of the nct6775 driver. But since we platform_device_alloc(), we
4156 * must keep track of the device
4158 static struct platform_device *pdev;
4160 static int __init sensors_nct6775_init(void)
4163 unsigned short address;
4164 struct resource res;
4165 struct nct6775_sio_data sio_data;
4168 * initialize sio_data->kind and sio_data->sioreg.
4170 * when Super-I/O functions move to a separate file, the Super-I/O
4171 * driver will probe 0x2e and 0x4e and auto-detect the presence of a
4172 * nct6775 hardware monitor, and call probe()
4174 if (nct6775_find(0x2e, &address, &sio_data) &&
4175 nct6775_find(0x4e, &address, &sio_data))
4178 err = platform_driver_register(&nct6775_driver);
4182 pdev = platform_device_alloc(DRVNAME, address);
4185 pr_err("Device allocation failed\n");
4186 goto exit_unregister;
4189 err = platform_device_add_data(pdev, &sio_data,
4190 sizeof(struct nct6775_sio_data));
4192 pr_err("Platform data allocation failed\n");
4193 goto exit_device_put;
4196 memset(&res, 0, sizeof(res));
4198 res.start = address + IOREGION_OFFSET;
4199 res.end = address + IOREGION_OFFSET + IOREGION_LENGTH - 1;
4200 res.flags = IORESOURCE_IO;
4202 err = acpi_check_resource_conflict(&res);
4204 goto exit_device_put;
4206 err = platform_device_add_resources(pdev, &res, 1);
4208 pr_err("Device resource addition failed (%d)\n", err);
4209 goto exit_device_put;
4212 /* platform_device_add calls probe() */
4213 err = platform_device_add(pdev);
4215 pr_err("Device addition failed (%d)\n", err);
4216 goto exit_device_put;
4222 platform_device_put(pdev);
4224 platform_driver_unregister(&nct6775_driver);
4229 static void __exit sensors_nct6775_exit(void)
4231 platform_device_unregister(pdev);
4232 platform_driver_unregister(&nct6775_driver);
4235 MODULE_AUTHOR("Guenter Roeck <linux@roeck-us.net>");
4236 MODULE_DESCRIPTION("NCT6775F/NCT6776F/NCT6779D driver");
4237 MODULE_LICENSE("GPL");
4239 module_init(sensors_nct6775_init);
4240 module_exit(sensors_nct6775_exit);