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;
559 u16 reg_temp[4][NUM_TEMP]; /* 0=temp, 1=temp_over, 2=temp_hyst,
562 u8 temp_src[NUM_TEMP];
563 u16 reg_temp_config[NUM_TEMP];
564 const char * const *temp_label;
571 const s8 *ALARM_BITS;
574 const u16 *REG_IN_MINMAX[2];
576 const u16 *REG_TARGET;
578 const u16 *REG_FAN_MODE;
579 const u16 *REG_FAN_MIN;
580 const u16 *REG_FAN_PULSES;
581 const u16 *REG_FAN_TIME[3];
583 const u16 *REG_TOLERANCE_H;
585 const u8 *REG_PWM_MODE;
586 const u8 *PWM_MODE_MASK;
588 const u16 *REG_PWM[7]; /* [0]=pwm, [1]=pwm_start, [2]=pwm_floor,
589 * [3]=pwm_max, [4]=pwm_step,
590 * [5]=weight_duty_step, [6]=weight_duty_base
592 const u16 *REG_PWM_READ;
594 const u16 *REG_AUTO_TEMP;
595 const u16 *REG_AUTO_PWM;
597 const u16 *REG_CRITICAL_TEMP;
598 const u16 *REG_CRITICAL_TEMP_TOLERANCE;
600 const u16 *REG_TEMP_SOURCE; /* temp register sources */
601 const u16 *REG_TEMP_SEL;
602 const u16 *REG_WEIGHT_TEMP_SEL;
603 const u16 *REG_WEIGHT_TEMP[3]; /* 0=base, 1=tolerance, 2=step */
605 const u16 *REG_TEMP_OFFSET;
607 const u16 *REG_ALARM;
609 unsigned int (*fan_from_reg)(u16 reg, unsigned int divreg);
610 unsigned int (*fan_from_reg_min)(u16 reg, unsigned int divreg);
612 struct mutex update_lock;
613 bool valid; /* true if following fields are valid */
614 unsigned long last_updated; /* In jiffies */
616 /* Register values */
617 u8 bank; /* current register bank */
618 u8 in_num; /* number of in inputs we have */
619 u8 in[15][3]; /* [0]=in, [1]=in_max, [2]=in_min */
625 u8 has_fan; /* some fan inputs can be disabled */
626 u8 has_fan_min; /* some fans don't have min register */
629 u8 temp_fixed_num; /* 3 or 6 */
630 u8 temp_type[NUM_TEMP_FIXED];
631 s8 temp_offset[NUM_TEMP_FIXED];
632 s16 temp[4][NUM_TEMP]; /* 0=temp, 1=temp_over, 2=temp_hyst,
636 u8 pwm_num; /* number of pwm */
637 u8 pwm_mode[5]; /* 1->DC variable voltage, 0->PWM variable duty cycle */
638 enum pwm_enable pwm_enable[5];
641 * 2->thermal cruise mode (also called SmartFan I)
642 * 3->fan speed cruise mode
644 * 5->enhanced variable thermal cruise (SmartFan IV)
646 u8 pwm[7][5]; /* [0]=pwm, [1]=pwm_start, [2]=pwm_floor,
647 * [3]=pwm_max, [4]=pwm_step,
648 * [5]=weight_duty_step, [6]=weight_duty_base
654 u32 target_speed_tolerance[5];
655 u8 speed_tolerance_limit;
657 u8 temp_tolerance[2][5];
660 u8 fan_time[3][5]; /* 0 = stop_time, 1 = step_up, 2 = step_down */
662 /* Automatic fan speed control registers */
667 u8 pwm_weight_temp_sel[5];
668 u8 weight_temp[3][5]; /* 0->temp_step, 1->temp_step_tol,
679 /* Remember extra register values over suspend/resume */
686 struct nct6775_sio_data {
691 static bool is_word_sized(struct nct6775_data *data, u16 reg)
693 switch (data->kind) {
695 return (((reg & 0xff00) == 0x100 ||
696 (reg & 0xff00) == 0x200) &&
697 ((reg & 0x00ff) == 0x50 ||
698 (reg & 0x00ff) == 0x53 ||
699 (reg & 0x00ff) == 0x55)) ||
700 (reg & 0xfff0) == 0x630 ||
701 reg == 0x640 || reg == 0x642 ||
703 ((reg & 0xfff0) == 0x650 && (reg & 0x000f) >= 0x06) ||
704 reg == 0x73 || reg == 0x75 || reg == 0x77;
706 return (((reg & 0xff00) == 0x100 ||
707 (reg & 0xff00) == 0x200) &&
708 ((reg & 0x00ff) == 0x50 ||
709 (reg & 0x00ff) == 0x53 ||
710 (reg & 0x00ff) == 0x55)) ||
711 (reg & 0xfff0) == 0x630 ||
713 reg == 0x640 || reg == 0x642 ||
714 ((reg & 0xfff0) == 0x650 && (reg & 0x000f) >= 0x06) ||
715 reg == 0x73 || reg == 0x75 || reg == 0x77;
717 return reg == 0x150 || reg == 0x153 || reg == 0x155 ||
718 ((reg & 0xfff0) == 0x4b0 && (reg & 0x000f) < 0x09) ||
720 reg == 0x63a || reg == 0x63c || reg == 0x63e ||
721 reg == 0x640 || reg == 0x642 ||
722 reg == 0x73 || reg == 0x75 || reg == 0x77 || reg == 0x79 ||
729 * On older chips, only registers 0x50-0x5f are banked.
730 * On more recent chips, all registers are banked.
731 * Assume that is the case and set the bank number for each access.
732 * Cache the bank number so it only needs to be set if it changes.
734 static inline void nct6775_set_bank(struct nct6775_data *data, u16 reg)
737 if (data->bank != bank) {
738 outb_p(NCT6775_REG_BANK, data->addr + ADDR_REG_OFFSET);
739 outb_p(bank, data->addr + DATA_REG_OFFSET);
744 static u16 nct6775_read_value(struct nct6775_data *data, u16 reg)
746 int res, word_sized = is_word_sized(data, reg);
748 nct6775_set_bank(data, reg);
749 outb_p(reg & 0xff, data->addr + ADDR_REG_OFFSET);
750 res = inb_p(data->addr + DATA_REG_OFFSET);
752 outb_p((reg & 0xff) + 1,
753 data->addr + ADDR_REG_OFFSET);
754 res = (res << 8) + inb_p(data->addr + DATA_REG_OFFSET);
759 static int nct6775_write_value(struct nct6775_data *data, u16 reg, u16 value)
761 int word_sized = is_word_sized(data, reg);
763 nct6775_set_bank(data, reg);
764 outb_p(reg & 0xff, data->addr + ADDR_REG_OFFSET);
766 outb_p(value >> 8, data->addr + DATA_REG_OFFSET);
767 outb_p((reg & 0xff) + 1,
768 data->addr + ADDR_REG_OFFSET);
770 outb_p(value & 0xff, data->addr + DATA_REG_OFFSET);
774 /* We left-align 8-bit temperature values to make the code simpler */
775 static u16 nct6775_read_temp(struct nct6775_data *data, u16 reg)
779 res = nct6775_read_value(data, reg);
780 if (!is_word_sized(data, reg))
786 static int nct6775_write_temp(struct nct6775_data *data, u16 reg, u16 value)
788 if (!is_word_sized(data, reg))
790 return nct6775_write_value(data, reg, value);
793 /* This function assumes that the caller holds data->update_lock */
794 static void nct6775_write_fan_div(struct nct6775_data *data, int nr)
800 reg = (nct6775_read_value(data, NCT6775_REG_FANDIV1) & 0x70)
801 | (data->fan_div[0] & 0x7);
802 nct6775_write_value(data, NCT6775_REG_FANDIV1, reg);
805 reg = (nct6775_read_value(data, NCT6775_REG_FANDIV1) & 0x7)
806 | ((data->fan_div[1] << 4) & 0x70);
807 nct6775_write_value(data, NCT6775_REG_FANDIV1, reg);
810 reg = (nct6775_read_value(data, NCT6775_REG_FANDIV2) & 0x70)
811 | (data->fan_div[2] & 0x7);
812 nct6775_write_value(data, NCT6775_REG_FANDIV2, reg);
815 reg = (nct6775_read_value(data, NCT6775_REG_FANDIV2) & 0x7)
816 | ((data->fan_div[3] << 4) & 0x70);
817 nct6775_write_value(data, NCT6775_REG_FANDIV2, reg);
822 static void nct6775_write_fan_div_common(struct nct6775_data *data, int nr)
824 if (data->kind == nct6775)
825 nct6775_write_fan_div(data, nr);
828 static void nct6775_update_fan_div(struct nct6775_data *data)
832 i = nct6775_read_value(data, NCT6775_REG_FANDIV1);
833 data->fan_div[0] = i & 0x7;
834 data->fan_div[1] = (i & 0x70) >> 4;
835 i = nct6775_read_value(data, NCT6775_REG_FANDIV2);
836 data->fan_div[2] = i & 0x7;
837 if (data->has_fan & (1<<3))
838 data->fan_div[3] = (i & 0x70) >> 4;
841 static void nct6775_update_fan_div_common(struct nct6775_data *data)
843 if (data->kind == nct6775)
844 nct6775_update_fan_div(data);
847 static void nct6775_init_fan_div(struct nct6775_data *data)
851 nct6775_update_fan_div_common(data);
853 * For all fans, start with highest divider value if the divider
854 * register is not initialized. This ensures that we get a
855 * reading from the fan count register, even if it is not optimal.
856 * We'll compute a better divider later on.
858 for (i = 0; i < 3; i++) {
859 if (!(data->has_fan & (1 << i)))
861 if (data->fan_div[i] == 0) {
862 data->fan_div[i] = 7;
863 nct6775_write_fan_div_common(data, i);
868 static void nct6775_init_fan_common(struct device *dev,
869 struct nct6775_data *data)
874 if (data->has_fan_div)
875 nct6775_init_fan_div(data);
878 * If fan_min is not set (0), set it to 0xff to disable it. This
879 * prevents the unnecessary warning when fanX_min is reported as 0.
881 for (i = 0; i < 5; i++) {
882 if (data->has_fan_min & (1 << i)) {
883 reg = nct6775_read_value(data, data->REG_FAN_MIN[i]);
885 nct6775_write_value(data, data->REG_FAN_MIN[i],
886 data->has_fan_div ? 0xff
892 static void nct6775_select_fan_div(struct device *dev,
893 struct nct6775_data *data, int nr, u16 reg)
895 u8 fan_div = data->fan_div[nr];
898 if (!data->has_fan_div)
902 * If we failed to measure the fan speed, or the reported value is not
903 * in the optimal range, and the clock divider can be modified,
904 * let's try that for next time.
906 if (reg == 0x00 && fan_div < 0x07)
908 else if (reg != 0x00 && reg < 0x30 && fan_div > 0)
911 if (fan_div != data->fan_div[nr]) {
912 dev_dbg(dev, "Modifying fan%d clock divider from %u to %u\n",
913 nr + 1, div_from_reg(data->fan_div[nr]),
914 div_from_reg(fan_div));
916 /* Preserve min limit if possible */
917 if (data->has_fan_min & (1 << nr)) {
918 fan_min = data->fan_min[nr];
919 if (fan_div > data->fan_div[nr]) {
920 if (fan_min != 255 && fan_min > 1)
923 if (fan_min != 255) {
929 if (fan_min != data->fan_min[nr]) {
930 data->fan_min[nr] = fan_min;
931 nct6775_write_value(data, data->REG_FAN_MIN[nr],
935 data->fan_div[nr] = fan_div;
936 nct6775_write_fan_div_common(data, nr);
940 static void nct6775_update_pwm(struct device *dev)
942 struct nct6775_data *data = dev_get_drvdata(dev);
947 for (i = 0; i < data->pwm_num; i++) {
948 if (!(data->has_pwm & (1 << i)))
951 duty_is_dc = data->REG_PWM_MODE[i] &&
952 (nct6775_read_value(data, data->REG_PWM_MODE[i])
953 & data->PWM_MODE_MASK[i]);
954 data->pwm_mode[i] = duty_is_dc;
956 fanmodecfg = nct6775_read_value(data, data->REG_FAN_MODE[i]);
957 for (j = 0; j < ARRAY_SIZE(data->REG_PWM); j++) {
958 if (data->REG_PWM[j] && data->REG_PWM[j][i]) {
960 = nct6775_read_value(data,
961 data->REG_PWM[j][i]);
965 data->pwm_enable[i] = reg_to_pwm_enable(data->pwm[0][i],
966 (fanmodecfg >> 4) & 7);
968 if (!data->temp_tolerance[0][i] ||
969 data->pwm_enable[i] != speed_cruise)
970 data->temp_tolerance[0][i] = fanmodecfg & 0x0f;
971 if (!data->target_speed_tolerance[i] ||
972 data->pwm_enable[i] == speed_cruise) {
973 u8 t = fanmodecfg & 0x0f;
974 if (data->REG_TOLERANCE_H) {
975 t |= (nct6775_read_value(data,
976 data->REG_TOLERANCE_H[i]) & 0x70) >> 1;
978 data->target_speed_tolerance[i] = t;
981 data->temp_tolerance[1][i] =
982 nct6775_read_value(data,
983 data->REG_CRITICAL_TEMP_TOLERANCE[i]);
985 reg = nct6775_read_value(data, data->REG_TEMP_SEL[i]);
986 data->pwm_temp_sel[i] = reg & 0x1f;
987 /* If fan can stop, report floor as 0 */
991 reg = nct6775_read_value(data, data->REG_WEIGHT_TEMP_SEL[i]);
992 data->pwm_weight_temp_sel[i] = reg & 0x1f;
993 /* If weight is disabled, report weight source as 0 */
994 if (j == 1 && !(reg & 0x80))
995 data->pwm_weight_temp_sel[i] = 0;
997 /* Weight temp data */
998 for (j = 0; j < 3; j++) {
999 data->weight_temp[j][i]
1000 = nct6775_read_value(data,
1001 data->REG_WEIGHT_TEMP[j][i]);
1006 static void nct6775_update_pwm_limits(struct device *dev)
1008 struct nct6775_data *data = dev_get_drvdata(dev);
1013 for (i = 0; i < data->pwm_num; i++) {
1014 if (!(data->has_pwm & (1 << i)))
1017 for (j = 0; j < 3; j++) {
1018 data->fan_time[j][i] =
1019 nct6775_read_value(data, data->REG_FAN_TIME[j][i]);
1022 reg_t = nct6775_read_value(data, data->REG_TARGET[i]);
1023 /* Update only in matching mode or if never updated */
1024 if (!data->target_temp[i] ||
1025 data->pwm_enable[i] == thermal_cruise)
1026 data->target_temp[i] = reg_t & data->target_temp_mask;
1027 if (!data->target_speed[i] ||
1028 data->pwm_enable[i] == speed_cruise) {
1029 if (data->REG_TOLERANCE_H) {
1030 reg_t |= (nct6775_read_value(data,
1031 data->REG_TOLERANCE_H[i]) & 0x0f) << 8;
1033 data->target_speed[i] = reg_t;
1036 for (j = 0; j < data->auto_pwm_num; j++) {
1037 data->auto_pwm[i][j] =
1038 nct6775_read_value(data,
1039 NCT6775_AUTO_PWM(data, i, j));
1040 data->auto_temp[i][j] =
1041 nct6775_read_value(data,
1042 NCT6775_AUTO_TEMP(data, i, j));
1045 /* critical auto_pwm temperature data */
1046 data->auto_temp[i][data->auto_pwm_num] =
1047 nct6775_read_value(data, data->REG_CRITICAL_TEMP[i]);
1049 switch (data->kind) {
1051 reg = nct6775_read_value(data,
1052 NCT6775_REG_CRITICAL_ENAB[i]);
1053 data->auto_pwm[i][data->auto_pwm_num] =
1054 (reg & 0x02) ? 0xff : 0x00;
1057 data->auto_pwm[i][data->auto_pwm_num] = 0xff;
1060 reg = nct6775_read_value(data,
1061 NCT6779_REG_CRITICAL_PWM_ENABLE[i]);
1063 data->auto_pwm[i][data->auto_pwm_num] =
1064 nct6775_read_value(data,
1065 NCT6779_REG_CRITICAL_PWM[i]);
1067 data->auto_pwm[i][data->auto_pwm_num] = 0xff;
1073 static struct nct6775_data *nct6775_update_device(struct device *dev)
1075 struct nct6775_data *data = dev_get_drvdata(dev);
1078 mutex_lock(&data->update_lock);
1080 if (time_after(jiffies, data->last_updated + HZ + HZ/2)
1082 /* Fan clock dividers */
1083 nct6775_update_fan_div_common(data);
1085 /* Measured voltages and limits */
1086 for (i = 0; i < data->in_num; i++) {
1087 if (!(data->have_in & (1 << i)))
1090 data->in[i][0] = nct6775_read_value(data,
1092 data->in[i][1] = nct6775_read_value(data,
1093 data->REG_IN_MINMAX[0][i]);
1094 data->in[i][2] = nct6775_read_value(data,
1095 data->REG_IN_MINMAX[1][i]);
1098 /* Measured fan speeds and limits */
1099 for (i = 0; i < 5; i++) {
1102 if (!(data->has_fan & (1 << i)))
1105 reg = nct6775_read_value(data, data->REG_FAN[i]);
1106 data->rpm[i] = data->fan_from_reg(reg,
1109 if (data->has_fan_min & (1 << i))
1110 data->fan_min[i] = nct6775_read_value(data,
1111 data->REG_FAN_MIN[i]);
1112 data->fan_pulses[i] =
1113 nct6775_read_value(data, data->REG_FAN_PULSES[i]);
1115 nct6775_select_fan_div(dev, data, i, reg);
1118 nct6775_update_pwm(dev);
1119 nct6775_update_pwm_limits(dev);
1121 /* Measured temperatures and limits */
1122 for (i = 0; i < NUM_TEMP; i++) {
1123 if (!(data->have_temp & (1 << i)))
1125 for (j = 0; j < 4; j++) {
1126 if (data->reg_temp[j][i])
1128 = nct6775_read_temp(data,
1129 data->reg_temp[j][i]);
1131 if (!(data->have_temp_fixed & (1 << i)))
1133 data->temp_offset[i]
1134 = nct6775_read_value(data, data->REG_TEMP_OFFSET[i]);
1138 for (i = 0; i < NUM_REG_ALARM; i++) {
1140 if (!data->REG_ALARM[i])
1142 alarm = nct6775_read_value(data, data->REG_ALARM[i]);
1143 data->alarms |= ((u64)alarm) << (i << 3);
1146 data->last_updated = jiffies;
1150 mutex_unlock(&data->update_lock);
1155 * Sysfs callback functions
1158 show_in_reg(struct device *dev, struct device_attribute *attr, char *buf)
1160 struct nct6775_data *data = nct6775_update_device(dev);
1161 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
1163 int index = sattr->index;
1164 return sprintf(buf, "%ld\n", in_from_reg(data->in[nr][index], nr));
1168 store_in_reg(struct device *dev, struct device_attribute *attr, const char *buf,
1171 struct nct6775_data *data = dev_get_drvdata(dev);
1172 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
1174 int index = sattr->index;
1176 int err = kstrtoul(buf, 10, &val);
1179 mutex_lock(&data->update_lock);
1180 data->in[nr][index] = in_to_reg(val, nr);
1181 nct6775_write_value(data, data->REG_IN_MINMAX[index-1][nr],
1182 data->in[nr][index]);
1183 mutex_unlock(&data->update_lock);
1188 show_alarm(struct device *dev, struct device_attribute *attr, char *buf)
1190 struct nct6775_data *data = nct6775_update_device(dev);
1191 struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr);
1192 int nr = data->ALARM_BITS[sattr->index];
1193 return sprintf(buf, "%u\n",
1194 (unsigned int)((data->alarms >> nr) & 0x01));
1197 static SENSOR_DEVICE_ATTR_2(in0_input, S_IRUGO, show_in_reg, NULL, 0, 0);
1198 static SENSOR_DEVICE_ATTR_2(in1_input, S_IRUGO, show_in_reg, NULL, 1, 0);
1199 static SENSOR_DEVICE_ATTR_2(in2_input, S_IRUGO, show_in_reg, NULL, 2, 0);
1200 static SENSOR_DEVICE_ATTR_2(in3_input, S_IRUGO, show_in_reg, NULL, 3, 0);
1201 static SENSOR_DEVICE_ATTR_2(in4_input, S_IRUGO, show_in_reg, NULL, 4, 0);
1202 static SENSOR_DEVICE_ATTR_2(in5_input, S_IRUGO, show_in_reg, NULL, 5, 0);
1203 static SENSOR_DEVICE_ATTR_2(in6_input, S_IRUGO, show_in_reg, NULL, 6, 0);
1204 static SENSOR_DEVICE_ATTR_2(in7_input, S_IRUGO, show_in_reg, NULL, 7, 0);
1205 static SENSOR_DEVICE_ATTR_2(in8_input, S_IRUGO, show_in_reg, NULL, 8, 0);
1206 static SENSOR_DEVICE_ATTR_2(in9_input, S_IRUGO, show_in_reg, NULL, 9, 0);
1207 static SENSOR_DEVICE_ATTR_2(in10_input, S_IRUGO, show_in_reg, NULL, 10, 0);
1208 static SENSOR_DEVICE_ATTR_2(in11_input, S_IRUGO, show_in_reg, NULL, 11, 0);
1209 static SENSOR_DEVICE_ATTR_2(in12_input, S_IRUGO, show_in_reg, NULL, 12, 0);
1210 static SENSOR_DEVICE_ATTR_2(in13_input, S_IRUGO, show_in_reg, NULL, 13, 0);
1211 static SENSOR_DEVICE_ATTR_2(in14_input, S_IRUGO, show_in_reg, NULL, 14, 0);
1213 static SENSOR_DEVICE_ATTR(in0_alarm, S_IRUGO, show_alarm, NULL, 0);
1214 static SENSOR_DEVICE_ATTR(in1_alarm, S_IRUGO, show_alarm, NULL, 1);
1215 static SENSOR_DEVICE_ATTR(in2_alarm, S_IRUGO, show_alarm, NULL, 2);
1216 static SENSOR_DEVICE_ATTR(in3_alarm, S_IRUGO, show_alarm, NULL, 3);
1217 static SENSOR_DEVICE_ATTR(in4_alarm, S_IRUGO, show_alarm, NULL, 4);
1218 static SENSOR_DEVICE_ATTR(in5_alarm, S_IRUGO, show_alarm, NULL, 5);
1219 static SENSOR_DEVICE_ATTR(in6_alarm, S_IRUGO, show_alarm, NULL, 6);
1220 static SENSOR_DEVICE_ATTR(in7_alarm, S_IRUGO, show_alarm, NULL, 7);
1221 static SENSOR_DEVICE_ATTR(in8_alarm, S_IRUGO, show_alarm, NULL, 8);
1222 static SENSOR_DEVICE_ATTR(in9_alarm, S_IRUGO, show_alarm, NULL, 9);
1223 static SENSOR_DEVICE_ATTR(in10_alarm, S_IRUGO, show_alarm, NULL, 10);
1224 static SENSOR_DEVICE_ATTR(in11_alarm, S_IRUGO, show_alarm, NULL, 11);
1225 static SENSOR_DEVICE_ATTR(in12_alarm, S_IRUGO, show_alarm, NULL, 12);
1226 static SENSOR_DEVICE_ATTR(in13_alarm, S_IRUGO, show_alarm, NULL, 13);
1227 static SENSOR_DEVICE_ATTR(in14_alarm, S_IRUGO, show_alarm, NULL, 14);
1229 static SENSOR_DEVICE_ATTR_2(in0_min, S_IWUSR | S_IRUGO, show_in_reg,
1230 store_in_reg, 0, 1);
1231 static SENSOR_DEVICE_ATTR_2(in1_min, S_IWUSR | S_IRUGO, show_in_reg,
1232 store_in_reg, 1, 1);
1233 static SENSOR_DEVICE_ATTR_2(in2_min, S_IWUSR | S_IRUGO, show_in_reg,
1234 store_in_reg, 2, 1);
1235 static SENSOR_DEVICE_ATTR_2(in3_min, S_IWUSR | S_IRUGO, show_in_reg,
1236 store_in_reg, 3, 1);
1237 static SENSOR_DEVICE_ATTR_2(in4_min, S_IWUSR | S_IRUGO, show_in_reg,
1238 store_in_reg, 4, 1);
1239 static SENSOR_DEVICE_ATTR_2(in5_min, S_IWUSR | S_IRUGO, show_in_reg,
1240 store_in_reg, 5, 1);
1241 static SENSOR_DEVICE_ATTR_2(in6_min, S_IWUSR | S_IRUGO, show_in_reg,
1242 store_in_reg, 6, 1);
1243 static SENSOR_DEVICE_ATTR_2(in7_min, S_IWUSR | S_IRUGO, show_in_reg,
1244 store_in_reg, 7, 1);
1245 static SENSOR_DEVICE_ATTR_2(in8_min, S_IWUSR | S_IRUGO, show_in_reg,
1246 store_in_reg, 8, 1);
1247 static SENSOR_DEVICE_ATTR_2(in9_min, S_IWUSR | S_IRUGO, show_in_reg,
1248 store_in_reg, 9, 1);
1249 static SENSOR_DEVICE_ATTR_2(in10_min, S_IWUSR | S_IRUGO, show_in_reg,
1250 store_in_reg, 10, 1);
1251 static SENSOR_DEVICE_ATTR_2(in11_min, S_IWUSR | S_IRUGO, show_in_reg,
1252 store_in_reg, 11, 1);
1253 static SENSOR_DEVICE_ATTR_2(in12_min, S_IWUSR | S_IRUGO, show_in_reg,
1254 store_in_reg, 12, 1);
1255 static SENSOR_DEVICE_ATTR_2(in13_min, S_IWUSR | S_IRUGO, show_in_reg,
1256 store_in_reg, 13, 1);
1257 static SENSOR_DEVICE_ATTR_2(in14_min, S_IWUSR | S_IRUGO, show_in_reg,
1258 store_in_reg, 14, 1);
1260 static SENSOR_DEVICE_ATTR_2(in0_max, S_IWUSR | S_IRUGO, show_in_reg,
1261 store_in_reg, 0, 2);
1262 static SENSOR_DEVICE_ATTR_2(in1_max, S_IWUSR | S_IRUGO, show_in_reg,
1263 store_in_reg, 1, 2);
1264 static SENSOR_DEVICE_ATTR_2(in2_max, S_IWUSR | S_IRUGO, show_in_reg,
1265 store_in_reg, 2, 2);
1266 static SENSOR_DEVICE_ATTR_2(in3_max, S_IWUSR | S_IRUGO, show_in_reg,
1267 store_in_reg, 3, 2);
1268 static SENSOR_DEVICE_ATTR_2(in4_max, S_IWUSR | S_IRUGO, show_in_reg,
1269 store_in_reg, 4, 2);
1270 static SENSOR_DEVICE_ATTR_2(in5_max, S_IWUSR | S_IRUGO, show_in_reg,
1271 store_in_reg, 5, 2);
1272 static SENSOR_DEVICE_ATTR_2(in6_max, S_IWUSR | S_IRUGO, show_in_reg,
1273 store_in_reg, 6, 2);
1274 static SENSOR_DEVICE_ATTR_2(in7_max, S_IWUSR | S_IRUGO, show_in_reg,
1275 store_in_reg, 7, 2);
1276 static SENSOR_DEVICE_ATTR_2(in8_max, S_IWUSR | S_IRUGO, show_in_reg,
1277 store_in_reg, 8, 2);
1278 static SENSOR_DEVICE_ATTR_2(in9_max, S_IWUSR | S_IRUGO, show_in_reg,
1279 store_in_reg, 9, 2);
1280 static SENSOR_DEVICE_ATTR_2(in10_max, S_IWUSR | S_IRUGO, show_in_reg,
1281 store_in_reg, 10, 2);
1282 static SENSOR_DEVICE_ATTR_2(in11_max, S_IWUSR | S_IRUGO, show_in_reg,
1283 store_in_reg, 11, 2);
1284 static SENSOR_DEVICE_ATTR_2(in12_max, S_IWUSR | S_IRUGO, show_in_reg,
1285 store_in_reg, 12, 2);
1286 static SENSOR_DEVICE_ATTR_2(in13_max, S_IWUSR | S_IRUGO, show_in_reg,
1287 store_in_reg, 13, 2);
1288 static SENSOR_DEVICE_ATTR_2(in14_max, S_IWUSR | S_IRUGO, show_in_reg,
1289 store_in_reg, 14, 2);
1291 static struct attribute *nct6775_attributes_in[15][5] = {
1293 &sensor_dev_attr_in0_input.dev_attr.attr,
1294 &sensor_dev_attr_in0_min.dev_attr.attr,
1295 &sensor_dev_attr_in0_max.dev_attr.attr,
1296 &sensor_dev_attr_in0_alarm.dev_attr.attr,
1300 &sensor_dev_attr_in1_input.dev_attr.attr,
1301 &sensor_dev_attr_in1_min.dev_attr.attr,
1302 &sensor_dev_attr_in1_max.dev_attr.attr,
1303 &sensor_dev_attr_in1_alarm.dev_attr.attr,
1307 &sensor_dev_attr_in2_input.dev_attr.attr,
1308 &sensor_dev_attr_in2_min.dev_attr.attr,
1309 &sensor_dev_attr_in2_max.dev_attr.attr,
1310 &sensor_dev_attr_in2_alarm.dev_attr.attr,
1314 &sensor_dev_attr_in3_input.dev_attr.attr,
1315 &sensor_dev_attr_in3_min.dev_attr.attr,
1316 &sensor_dev_attr_in3_max.dev_attr.attr,
1317 &sensor_dev_attr_in3_alarm.dev_attr.attr,
1321 &sensor_dev_attr_in4_input.dev_attr.attr,
1322 &sensor_dev_attr_in4_min.dev_attr.attr,
1323 &sensor_dev_attr_in4_max.dev_attr.attr,
1324 &sensor_dev_attr_in4_alarm.dev_attr.attr,
1328 &sensor_dev_attr_in5_input.dev_attr.attr,
1329 &sensor_dev_attr_in5_min.dev_attr.attr,
1330 &sensor_dev_attr_in5_max.dev_attr.attr,
1331 &sensor_dev_attr_in5_alarm.dev_attr.attr,
1335 &sensor_dev_attr_in6_input.dev_attr.attr,
1336 &sensor_dev_attr_in6_min.dev_attr.attr,
1337 &sensor_dev_attr_in6_max.dev_attr.attr,
1338 &sensor_dev_attr_in6_alarm.dev_attr.attr,
1342 &sensor_dev_attr_in7_input.dev_attr.attr,
1343 &sensor_dev_attr_in7_min.dev_attr.attr,
1344 &sensor_dev_attr_in7_max.dev_attr.attr,
1345 &sensor_dev_attr_in7_alarm.dev_attr.attr,
1349 &sensor_dev_attr_in8_input.dev_attr.attr,
1350 &sensor_dev_attr_in8_min.dev_attr.attr,
1351 &sensor_dev_attr_in8_max.dev_attr.attr,
1352 &sensor_dev_attr_in8_alarm.dev_attr.attr,
1356 &sensor_dev_attr_in9_input.dev_attr.attr,
1357 &sensor_dev_attr_in9_min.dev_attr.attr,
1358 &sensor_dev_attr_in9_max.dev_attr.attr,
1359 &sensor_dev_attr_in9_alarm.dev_attr.attr,
1363 &sensor_dev_attr_in10_input.dev_attr.attr,
1364 &sensor_dev_attr_in10_min.dev_attr.attr,
1365 &sensor_dev_attr_in10_max.dev_attr.attr,
1366 &sensor_dev_attr_in10_alarm.dev_attr.attr,
1370 &sensor_dev_attr_in11_input.dev_attr.attr,
1371 &sensor_dev_attr_in11_min.dev_attr.attr,
1372 &sensor_dev_attr_in11_max.dev_attr.attr,
1373 &sensor_dev_attr_in11_alarm.dev_attr.attr,
1377 &sensor_dev_attr_in12_input.dev_attr.attr,
1378 &sensor_dev_attr_in12_min.dev_attr.attr,
1379 &sensor_dev_attr_in12_max.dev_attr.attr,
1380 &sensor_dev_attr_in12_alarm.dev_attr.attr,
1384 &sensor_dev_attr_in13_input.dev_attr.attr,
1385 &sensor_dev_attr_in13_min.dev_attr.attr,
1386 &sensor_dev_attr_in13_max.dev_attr.attr,
1387 &sensor_dev_attr_in13_alarm.dev_attr.attr,
1391 &sensor_dev_attr_in14_input.dev_attr.attr,
1392 &sensor_dev_attr_in14_min.dev_attr.attr,
1393 &sensor_dev_attr_in14_max.dev_attr.attr,
1394 &sensor_dev_attr_in14_alarm.dev_attr.attr,
1399 static const struct attribute_group nct6775_group_in[15] = {
1400 { .attrs = nct6775_attributes_in[0] },
1401 { .attrs = nct6775_attributes_in[1] },
1402 { .attrs = nct6775_attributes_in[2] },
1403 { .attrs = nct6775_attributes_in[3] },
1404 { .attrs = nct6775_attributes_in[4] },
1405 { .attrs = nct6775_attributes_in[5] },
1406 { .attrs = nct6775_attributes_in[6] },
1407 { .attrs = nct6775_attributes_in[7] },
1408 { .attrs = nct6775_attributes_in[8] },
1409 { .attrs = nct6775_attributes_in[9] },
1410 { .attrs = nct6775_attributes_in[10] },
1411 { .attrs = nct6775_attributes_in[11] },
1412 { .attrs = nct6775_attributes_in[12] },
1413 { .attrs = nct6775_attributes_in[13] },
1414 { .attrs = nct6775_attributes_in[14] },
1418 show_fan(struct device *dev, struct device_attribute *attr, char *buf)
1420 struct nct6775_data *data = nct6775_update_device(dev);
1421 struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr);
1422 int nr = sattr->index;
1423 return sprintf(buf, "%d\n", data->rpm[nr]);
1427 show_fan_min(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",
1433 data->fan_from_reg_min(data->fan_min[nr],
1434 data->fan_div[nr]));
1438 show_fan_div(struct device *dev, struct device_attribute *attr, char *buf)
1440 struct nct6775_data *data = nct6775_update_device(dev);
1441 struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr);
1442 int nr = sattr->index;
1443 return sprintf(buf, "%u\n", div_from_reg(data->fan_div[nr]));
1447 store_fan_min(struct device *dev, struct device_attribute *attr,
1448 const char *buf, size_t count)
1450 struct nct6775_data *data = dev_get_drvdata(dev);
1451 struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr);
1452 int nr = sattr->index;
1458 err = kstrtoul(buf, 10, &val);
1462 mutex_lock(&data->update_lock);
1463 if (!data->has_fan_div) {
1464 /* NCT6776F or NCT6779D; we know this is a 13 bit register */
1470 val = 1350000U / val;
1471 val = (val & 0x1f) | ((val << 3) & 0xff00);
1473 data->fan_min[nr] = val;
1474 goto write_min; /* Leave fan divider alone */
1477 /* No min limit, alarm disabled */
1478 data->fan_min[nr] = 255;
1479 new_div = data->fan_div[nr]; /* No change */
1480 dev_info(dev, "fan%u low limit and alarm disabled\n", nr + 1);
1483 reg = 1350000U / val;
1484 if (reg >= 128 * 255) {
1486 * Speed below this value cannot possibly be represented,
1487 * even with the highest divider (128)
1489 data->fan_min[nr] = 254;
1490 new_div = 7; /* 128 == (1 << 7) */
1492 "fan%u low limit %lu below minimum %u, set to minimum\n",
1493 nr + 1, val, data->fan_from_reg_min(254, 7));
1496 * Speed above this value cannot possibly be represented,
1497 * even with the lowest divider (1)
1499 data->fan_min[nr] = 1;
1500 new_div = 0; /* 1 == (1 << 0) */
1502 "fan%u low limit %lu above maximum %u, set to maximum\n",
1503 nr + 1, val, data->fan_from_reg_min(1, 0));
1506 * Automatically pick the best divider, i.e. the one such
1507 * that the min limit will correspond to a register value
1508 * in the 96..192 range
1511 while (reg > 192 && new_div < 7) {
1515 data->fan_min[nr] = reg;
1520 * Write both the fan clock divider (if it changed) and the new
1521 * fan min (unconditionally)
1523 if (new_div != data->fan_div[nr]) {
1524 dev_dbg(dev, "fan%u clock divider changed from %u to %u\n",
1525 nr + 1, div_from_reg(data->fan_div[nr]),
1526 div_from_reg(new_div));
1527 data->fan_div[nr] = new_div;
1528 nct6775_write_fan_div_common(data, nr);
1529 /* Give the chip time to sample a new speed value */
1530 data->last_updated = jiffies;
1534 nct6775_write_value(data, data->REG_FAN_MIN[nr], data->fan_min[nr]);
1535 mutex_unlock(&data->update_lock);
1541 show_fan_pulses(struct device *dev, struct device_attribute *attr, char *buf)
1543 struct nct6775_data *data = nct6775_update_device(dev);
1544 struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr);
1545 int p = data->fan_pulses[sattr->index];
1547 return sprintf(buf, "%d\n", p ? : 4);
1551 store_fan_pulses(struct device *dev, struct device_attribute *attr,
1552 const char *buf, size_t count)
1554 struct nct6775_data *data = dev_get_drvdata(dev);
1555 struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr);
1556 int nr = sattr->index;
1560 err = kstrtoul(buf, 10, &val);
1567 mutex_lock(&data->update_lock);
1568 data->fan_pulses[nr] = val & 3;
1569 nct6775_write_value(data, data->REG_FAN_PULSES[nr], val & 3);
1570 mutex_unlock(&data->update_lock);
1575 static struct sensor_device_attribute sda_fan_input[] = {
1576 SENSOR_ATTR(fan1_input, S_IRUGO, show_fan, NULL, 0),
1577 SENSOR_ATTR(fan2_input, S_IRUGO, show_fan, NULL, 1),
1578 SENSOR_ATTR(fan3_input, S_IRUGO, show_fan, NULL, 2),
1579 SENSOR_ATTR(fan4_input, S_IRUGO, show_fan, NULL, 3),
1580 SENSOR_ATTR(fan5_input, S_IRUGO, show_fan, NULL, 4),
1583 static struct sensor_device_attribute sda_fan_alarm[] = {
1584 SENSOR_ATTR(fan1_alarm, S_IRUGO, show_alarm, NULL, FAN_ALARM_BASE),
1585 SENSOR_ATTR(fan2_alarm, S_IRUGO, show_alarm, NULL, FAN_ALARM_BASE + 1),
1586 SENSOR_ATTR(fan3_alarm, S_IRUGO, show_alarm, NULL, FAN_ALARM_BASE + 2),
1587 SENSOR_ATTR(fan4_alarm, S_IRUGO, show_alarm, NULL, FAN_ALARM_BASE + 3),
1588 SENSOR_ATTR(fan5_alarm, S_IRUGO, show_alarm, NULL, FAN_ALARM_BASE + 4),
1591 static struct sensor_device_attribute sda_fan_min[] = {
1592 SENSOR_ATTR(fan1_min, S_IWUSR | S_IRUGO, show_fan_min,
1594 SENSOR_ATTR(fan2_min, S_IWUSR | S_IRUGO, show_fan_min,
1596 SENSOR_ATTR(fan3_min, S_IWUSR | S_IRUGO, show_fan_min,
1598 SENSOR_ATTR(fan4_min, S_IWUSR | S_IRUGO, show_fan_min,
1600 SENSOR_ATTR(fan5_min, S_IWUSR | S_IRUGO, show_fan_min,
1604 static struct sensor_device_attribute sda_fan_pulses[] = {
1605 SENSOR_ATTR(fan1_pulses, S_IWUSR | S_IRUGO, show_fan_pulses,
1606 store_fan_pulses, 0),
1607 SENSOR_ATTR(fan2_pulses, S_IWUSR | S_IRUGO, show_fan_pulses,
1608 store_fan_pulses, 1),
1609 SENSOR_ATTR(fan3_pulses, S_IWUSR | S_IRUGO, show_fan_pulses,
1610 store_fan_pulses, 2),
1611 SENSOR_ATTR(fan4_pulses, S_IWUSR | S_IRUGO, show_fan_pulses,
1612 store_fan_pulses, 3),
1613 SENSOR_ATTR(fan5_pulses, S_IWUSR | S_IRUGO, show_fan_pulses,
1614 store_fan_pulses, 4),
1617 static struct sensor_device_attribute sda_fan_div[] = {
1618 SENSOR_ATTR(fan1_div, S_IRUGO, show_fan_div, NULL, 0),
1619 SENSOR_ATTR(fan2_div, S_IRUGO, show_fan_div, NULL, 1),
1620 SENSOR_ATTR(fan3_div, S_IRUGO, show_fan_div, NULL, 2),
1621 SENSOR_ATTR(fan4_div, S_IRUGO, show_fan_div, NULL, 3),
1622 SENSOR_ATTR(fan5_div, S_IRUGO, show_fan_div, NULL, 4),
1626 show_temp_label(struct device *dev, struct device_attribute *attr, char *buf)
1628 struct nct6775_data *data = nct6775_update_device(dev);
1629 struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr);
1630 int nr = sattr->index;
1631 return sprintf(buf, "%s\n", data->temp_label[data->temp_src[nr]]);
1635 show_temp(struct device *dev, struct device_attribute *attr, char *buf)
1637 struct nct6775_data *data = nct6775_update_device(dev);
1638 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
1640 int index = sattr->index;
1642 return sprintf(buf, "%d\n", LM75_TEMP_FROM_REG(data->temp[index][nr]));
1646 store_temp(struct device *dev, struct device_attribute *attr, const char *buf,
1649 struct nct6775_data *data = dev_get_drvdata(dev);
1650 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
1652 int index = sattr->index;
1656 err = kstrtol(buf, 10, &val);
1660 mutex_lock(&data->update_lock);
1661 data->temp[index][nr] = LM75_TEMP_TO_REG(val);
1662 nct6775_write_temp(data, data->reg_temp[index][nr],
1663 data->temp[index][nr]);
1664 mutex_unlock(&data->update_lock);
1669 show_temp_offset(struct device *dev, struct device_attribute *attr, char *buf)
1671 struct nct6775_data *data = nct6775_update_device(dev);
1672 struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr);
1674 return sprintf(buf, "%d\n", data->temp_offset[sattr->index] * 1000);
1678 store_temp_offset(struct device *dev, struct device_attribute *attr,
1679 const char *buf, size_t count)
1681 struct nct6775_data *data = dev_get_drvdata(dev);
1682 struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr);
1683 int nr = sattr->index;
1687 err = kstrtol(buf, 10, &val);
1691 val = clamp_val(DIV_ROUND_CLOSEST(val, 1000), -128, 127);
1693 mutex_lock(&data->update_lock);
1694 data->temp_offset[nr] = val;
1695 nct6775_write_value(data, data->REG_TEMP_OFFSET[nr], val);
1696 mutex_unlock(&data->update_lock);
1702 show_temp_type(struct device *dev, struct device_attribute *attr, char *buf)
1704 struct nct6775_data *data = nct6775_update_device(dev);
1705 struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr);
1706 int nr = sattr->index;
1707 return sprintf(buf, "%d\n", (int)data->temp_type[nr]);
1711 store_temp_type(struct device *dev, struct device_attribute *attr,
1712 const char *buf, size_t count)
1714 struct nct6775_data *data = nct6775_update_device(dev);
1715 struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr);
1716 int nr = sattr->index;
1719 u8 vbat, diode, bit;
1721 err = kstrtoul(buf, 10, &val);
1725 if (val != 1 && val != 3 && val != 4)
1728 mutex_lock(&data->update_lock);
1730 data->temp_type[nr] = val;
1731 vbat = nct6775_read_value(data, data->REG_VBAT) & ~(0x02 << nr);
1732 diode = nct6775_read_value(data, data->REG_DIODE) & ~(0x02 << nr);
1735 case 1: /* CPU diode (diode, current mode) */
1739 case 3: /* diode, voltage mode */
1742 case 4: /* thermistor */
1745 nct6775_write_value(data, data->REG_VBAT, vbat);
1746 nct6775_write_value(data, data->REG_DIODE, diode);
1748 mutex_unlock(&data->update_lock);
1752 static struct sensor_device_attribute_2 sda_temp_input[] = {
1753 SENSOR_ATTR_2(temp1_input, S_IRUGO, show_temp, NULL, 0, 0),
1754 SENSOR_ATTR_2(temp2_input, S_IRUGO, show_temp, NULL, 1, 0),
1755 SENSOR_ATTR_2(temp3_input, S_IRUGO, show_temp, NULL, 2, 0),
1756 SENSOR_ATTR_2(temp4_input, S_IRUGO, show_temp, NULL, 3, 0),
1757 SENSOR_ATTR_2(temp5_input, S_IRUGO, show_temp, NULL, 4, 0),
1758 SENSOR_ATTR_2(temp6_input, S_IRUGO, show_temp, NULL, 5, 0),
1759 SENSOR_ATTR_2(temp7_input, S_IRUGO, show_temp, NULL, 6, 0),
1760 SENSOR_ATTR_2(temp8_input, S_IRUGO, show_temp, NULL, 7, 0),
1761 SENSOR_ATTR_2(temp9_input, S_IRUGO, show_temp, NULL, 8, 0),
1762 SENSOR_ATTR_2(temp10_input, S_IRUGO, show_temp, NULL, 9, 0),
1765 static struct sensor_device_attribute sda_temp_label[] = {
1766 SENSOR_ATTR(temp1_label, S_IRUGO, show_temp_label, NULL, 0),
1767 SENSOR_ATTR(temp2_label, S_IRUGO, show_temp_label, NULL, 1),
1768 SENSOR_ATTR(temp3_label, S_IRUGO, show_temp_label, NULL, 2),
1769 SENSOR_ATTR(temp4_label, S_IRUGO, show_temp_label, NULL, 3),
1770 SENSOR_ATTR(temp5_label, S_IRUGO, show_temp_label, NULL, 4),
1771 SENSOR_ATTR(temp6_label, S_IRUGO, show_temp_label, NULL, 5),
1772 SENSOR_ATTR(temp7_label, S_IRUGO, show_temp_label, NULL, 6),
1773 SENSOR_ATTR(temp8_label, S_IRUGO, show_temp_label, NULL, 7),
1774 SENSOR_ATTR(temp9_label, S_IRUGO, show_temp_label, NULL, 8),
1775 SENSOR_ATTR(temp10_label, S_IRUGO, show_temp_label, NULL, 9),
1778 static struct sensor_device_attribute_2 sda_temp_max[] = {
1779 SENSOR_ATTR_2(temp1_max, S_IRUGO | S_IWUSR, show_temp, store_temp,
1781 SENSOR_ATTR_2(temp2_max, S_IRUGO | S_IWUSR, show_temp, store_temp,
1783 SENSOR_ATTR_2(temp3_max, S_IRUGO | S_IWUSR, show_temp, store_temp,
1785 SENSOR_ATTR_2(temp4_max, S_IRUGO | S_IWUSR, show_temp, store_temp,
1787 SENSOR_ATTR_2(temp5_max, S_IRUGO | S_IWUSR, show_temp, store_temp,
1789 SENSOR_ATTR_2(temp6_max, S_IRUGO | S_IWUSR, show_temp, store_temp,
1791 SENSOR_ATTR_2(temp7_max, S_IRUGO | S_IWUSR, show_temp, store_temp,
1793 SENSOR_ATTR_2(temp8_max, S_IRUGO | S_IWUSR, show_temp, store_temp,
1795 SENSOR_ATTR_2(temp9_max, S_IRUGO | S_IWUSR, show_temp, store_temp,
1797 SENSOR_ATTR_2(temp10_max, S_IRUGO | S_IWUSR, show_temp, store_temp,
1801 static struct sensor_device_attribute_2 sda_temp_max_hyst[] = {
1802 SENSOR_ATTR_2(temp1_max_hyst, S_IRUGO | S_IWUSR, show_temp, store_temp,
1804 SENSOR_ATTR_2(temp2_max_hyst, S_IRUGO | S_IWUSR, show_temp, store_temp,
1806 SENSOR_ATTR_2(temp3_max_hyst, S_IRUGO | S_IWUSR, show_temp, store_temp,
1808 SENSOR_ATTR_2(temp4_max_hyst, S_IRUGO | S_IWUSR, show_temp, store_temp,
1810 SENSOR_ATTR_2(temp5_max_hyst, S_IRUGO | S_IWUSR, show_temp, store_temp,
1812 SENSOR_ATTR_2(temp6_max_hyst, S_IRUGO | S_IWUSR, show_temp, store_temp,
1814 SENSOR_ATTR_2(temp7_max_hyst, S_IRUGO | S_IWUSR, show_temp, store_temp,
1816 SENSOR_ATTR_2(temp8_max_hyst, S_IRUGO | S_IWUSR, show_temp, store_temp,
1818 SENSOR_ATTR_2(temp9_max_hyst, S_IRUGO | S_IWUSR, show_temp, store_temp,
1820 SENSOR_ATTR_2(temp10_max_hyst, S_IRUGO | S_IWUSR, show_temp, store_temp,
1824 static struct sensor_device_attribute_2 sda_temp_crit[] = {
1825 SENSOR_ATTR_2(temp1_crit, S_IRUGO | S_IWUSR, show_temp, store_temp,
1827 SENSOR_ATTR_2(temp2_crit, S_IRUGO | S_IWUSR, show_temp, store_temp,
1829 SENSOR_ATTR_2(temp3_crit, S_IRUGO | S_IWUSR, show_temp, store_temp,
1831 SENSOR_ATTR_2(temp4_crit, S_IRUGO | S_IWUSR, show_temp, store_temp,
1833 SENSOR_ATTR_2(temp5_crit, S_IRUGO | S_IWUSR, show_temp, store_temp,
1835 SENSOR_ATTR_2(temp6_crit, S_IRUGO | S_IWUSR, show_temp, store_temp,
1837 SENSOR_ATTR_2(temp7_crit, S_IRUGO | S_IWUSR, show_temp, store_temp,
1839 SENSOR_ATTR_2(temp8_crit, S_IRUGO | S_IWUSR, show_temp, store_temp,
1841 SENSOR_ATTR_2(temp9_crit, S_IRUGO | S_IWUSR, show_temp, store_temp,
1843 SENSOR_ATTR_2(temp10_crit, S_IRUGO | S_IWUSR, show_temp, store_temp,
1847 static struct sensor_device_attribute sda_temp_offset[] = {
1848 SENSOR_ATTR(temp1_offset, S_IRUGO | S_IWUSR, show_temp_offset,
1849 store_temp_offset, 0),
1850 SENSOR_ATTR(temp2_offset, S_IRUGO | S_IWUSR, show_temp_offset,
1851 store_temp_offset, 1),
1852 SENSOR_ATTR(temp3_offset, S_IRUGO | S_IWUSR, show_temp_offset,
1853 store_temp_offset, 2),
1854 SENSOR_ATTR(temp4_offset, S_IRUGO | S_IWUSR, show_temp_offset,
1855 store_temp_offset, 3),
1856 SENSOR_ATTR(temp5_offset, S_IRUGO | S_IWUSR, show_temp_offset,
1857 store_temp_offset, 4),
1858 SENSOR_ATTR(temp6_offset, S_IRUGO | S_IWUSR, show_temp_offset,
1859 store_temp_offset, 5),
1862 static struct sensor_device_attribute sda_temp_type[] = {
1863 SENSOR_ATTR(temp1_type, S_IRUGO | S_IWUSR, show_temp_type,
1864 store_temp_type, 0),
1865 SENSOR_ATTR(temp2_type, S_IRUGO | S_IWUSR, show_temp_type,
1866 store_temp_type, 1),
1867 SENSOR_ATTR(temp3_type, S_IRUGO | S_IWUSR, show_temp_type,
1868 store_temp_type, 2),
1869 SENSOR_ATTR(temp4_type, S_IRUGO | S_IWUSR, show_temp_type,
1870 store_temp_type, 3),
1871 SENSOR_ATTR(temp5_type, S_IRUGO | S_IWUSR, show_temp_type,
1872 store_temp_type, 4),
1873 SENSOR_ATTR(temp6_type, S_IRUGO | S_IWUSR, show_temp_type,
1874 store_temp_type, 5),
1877 static struct sensor_device_attribute sda_temp_alarm[] = {
1878 SENSOR_ATTR(temp1_alarm, S_IRUGO, show_alarm, NULL,
1880 SENSOR_ATTR(temp2_alarm, S_IRUGO, show_alarm, NULL,
1881 TEMP_ALARM_BASE + 1),
1882 SENSOR_ATTR(temp3_alarm, S_IRUGO, show_alarm, NULL,
1883 TEMP_ALARM_BASE + 2),
1884 SENSOR_ATTR(temp4_alarm, S_IRUGO, show_alarm, NULL,
1885 TEMP_ALARM_BASE + 3),
1886 SENSOR_ATTR(temp5_alarm, S_IRUGO, show_alarm, NULL,
1887 TEMP_ALARM_BASE + 4),
1888 SENSOR_ATTR(temp6_alarm, S_IRUGO, show_alarm, NULL,
1889 TEMP_ALARM_BASE + 5),
1892 #define NUM_TEMP_ALARM ARRAY_SIZE(sda_temp_alarm)
1895 show_pwm_mode(struct device *dev, struct device_attribute *attr, char *buf)
1897 struct nct6775_data *data = nct6775_update_device(dev);
1898 struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr);
1900 return sprintf(buf, "%d\n", !data->pwm_mode[sattr->index]);
1904 store_pwm_mode(struct device *dev, struct device_attribute *attr,
1905 const char *buf, size_t count)
1907 struct nct6775_data *data = dev_get_drvdata(dev);
1908 struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr);
1909 int nr = sattr->index;
1914 err = kstrtoul(buf, 10, &val);
1921 /* Setting DC mode is not supported for all chips/channels */
1922 if (data->REG_PWM_MODE[nr] == 0) {
1928 mutex_lock(&data->update_lock);
1929 data->pwm_mode[nr] = val;
1930 reg = nct6775_read_value(data, data->REG_PWM_MODE[nr]);
1931 reg &= ~data->PWM_MODE_MASK[nr];
1933 reg |= data->PWM_MODE_MASK[nr];
1934 nct6775_write_value(data, data->REG_PWM_MODE[nr], reg);
1935 mutex_unlock(&data->update_lock);
1940 show_pwm(struct device *dev, struct device_attribute *attr, char *buf)
1942 struct nct6775_data *data = nct6775_update_device(dev);
1943 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
1945 int index = sattr->index;
1949 * For automatic fan control modes, show current pwm readings.
1950 * Otherwise, show the configured value.
1952 if (index == 0 && data->pwm_enable[nr] > manual)
1953 pwm = nct6775_read_value(data, data->REG_PWM_READ[nr]);
1955 pwm = data->pwm[index][nr];
1957 return sprintf(buf, "%d\n", pwm);
1961 store_pwm(struct device *dev, struct device_attribute *attr, const char *buf,
1964 struct nct6775_data *data = dev_get_drvdata(dev);
1965 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
1967 int index = sattr->index;
1969 int minval[7] = { 0, 1, 1, data->pwm[2][nr], 0, 0, 0 };
1971 = { 255, 255, data->pwm[3][nr] ? : 255, 255, 255, 255, 255 };
1975 err = kstrtoul(buf, 10, &val);
1978 val = clamp_val(val, minval[index], maxval[index]);
1980 mutex_lock(&data->update_lock);
1981 data->pwm[index][nr] = val;
1982 nct6775_write_value(data, data->REG_PWM[index][nr], val);
1983 if (index == 2) { /* floor: disable if val == 0 */
1984 reg = nct6775_read_value(data, data->REG_TEMP_SEL[nr]);
1988 nct6775_write_value(data, data->REG_TEMP_SEL[nr], reg);
1990 mutex_unlock(&data->update_lock);
1994 /* Returns 0 if OK, -EINVAL otherwise */
1995 static int check_trip_points(struct nct6775_data *data, int nr)
1999 for (i = 0; i < data->auto_pwm_num - 1; i++) {
2000 if (data->auto_temp[nr][i] > data->auto_temp[nr][i + 1])
2003 for (i = 0; i < data->auto_pwm_num - 1; i++) {
2004 if (data->auto_pwm[nr][i] > data->auto_pwm[nr][i + 1])
2007 /* validate critical temperature and pwm if enabled (pwm > 0) */
2008 if (data->auto_pwm[nr][data->auto_pwm_num]) {
2009 if (data->auto_temp[nr][data->auto_pwm_num - 1] >
2010 data->auto_temp[nr][data->auto_pwm_num] ||
2011 data->auto_pwm[nr][data->auto_pwm_num - 1] >
2012 data->auto_pwm[nr][data->auto_pwm_num])
2018 static void pwm_update_registers(struct nct6775_data *data, int nr)
2022 switch (data->pwm_enable[nr]) {
2027 reg = nct6775_read_value(data, data->REG_FAN_MODE[nr]);
2028 reg = (reg & ~data->tolerance_mask) |
2029 (data->target_speed_tolerance[nr] & data->tolerance_mask);
2030 nct6775_write_value(data, data->REG_FAN_MODE[nr], reg);
2031 nct6775_write_value(data, data->REG_TARGET[nr],
2032 data->target_speed[nr] & 0xff);
2033 if (data->REG_TOLERANCE_H) {
2034 reg = (data->target_speed[nr] >> 8) & 0x0f;
2035 reg |= (data->target_speed_tolerance[nr] & 0x38) << 1;
2036 nct6775_write_value(data,
2037 data->REG_TOLERANCE_H[nr],
2041 case thermal_cruise:
2042 nct6775_write_value(data, data->REG_TARGET[nr],
2043 data->target_temp[nr]);
2046 reg = nct6775_read_value(data, data->REG_FAN_MODE[nr]);
2047 reg = (reg & ~data->tolerance_mask) |
2048 data->temp_tolerance[0][nr];
2049 nct6775_write_value(data, data->REG_FAN_MODE[nr], reg);
2055 show_pwm_enable(struct device *dev, struct device_attribute *attr, char *buf)
2057 struct nct6775_data *data = nct6775_update_device(dev);
2058 struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr);
2060 return sprintf(buf, "%d\n", data->pwm_enable[sattr->index]);
2064 store_pwm_enable(struct device *dev, struct device_attribute *attr,
2065 const char *buf, size_t count)
2067 struct nct6775_data *data = dev_get_drvdata(dev);
2068 struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr);
2069 int nr = sattr->index;
2074 err = kstrtoul(buf, 10, &val);
2081 if (val == sf3 && data->kind != nct6775)
2084 if (val == sf4 && check_trip_points(data, nr)) {
2085 dev_err(dev, "Inconsistent trip points, not switching to SmartFan IV mode\n");
2086 dev_err(dev, "Adjust trip points and try again\n");
2090 mutex_lock(&data->update_lock);
2091 data->pwm_enable[nr] = val;
2094 * turn off pwm control: select manual mode, set pwm to maximum
2096 data->pwm[0][nr] = 255;
2097 nct6775_write_value(data, data->REG_PWM[0][nr], 255);
2099 pwm_update_registers(data, nr);
2100 reg = nct6775_read_value(data, data->REG_FAN_MODE[nr]);
2102 reg |= pwm_enable_to_reg(val) << 4;
2103 nct6775_write_value(data, data->REG_FAN_MODE[nr], reg);
2104 mutex_unlock(&data->update_lock);
2109 show_pwm_temp_sel_common(struct nct6775_data *data, char *buf, int src)
2113 for (i = 0; i < NUM_TEMP; i++) {
2114 if (!(data->have_temp & (1 << i)))
2116 if (src == data->temp_src[i]) {
2122 return sprintf(buf, "%d\n", sel);
2126 show_pwm_temp_sel(struct device *dev, struct device_attribute *attr, char *buf)
2128 struct nct6775_data *data = nct6775_update_device(dev);
2129 struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr);
2130 int index = sattr->index;
2132 return show_pwm_temp_sel_common(data, buf, data->pwm_temp_sel[index]);
2136 store_pwm_temp_sel(struct device *dev, struct device_attribute *attr,
2137 const char *buf, size_t count)
2139 struct nct6775_data *data = nct6775_update_device(dev);
2140 struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr);
2141 int nr = sattr->index;
2145 err = kstrtoul(buf, 10, &val);
2148 if (val == 0 || val > NUM_TEMP)
2150 if (!(data->have_temp & (1 << (val - 1))) || !data->temp_src[val - 1])
2153 mutex_lock(&data->update_lock);
2154 src = data->temp_src[val - 1];
2155 data->pwm_temp_sel[nr] = src;
2156 reg = nct6775_read_value(data, data->REG_TEMP_SEL[nr]);
2159 nct6775_write_value(data, data->REG_TEMP_SEL[nr], reg);
2160 mutex_unlock(&data->update_lock);
2166 show_pwm_weight_temp_sel(struct device *dev, struct device_attribute *attr,
2169 struct nct6775_data *data = nct6775_update_device(dev);
2170 struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr);
2171 int index = sattr->index;
2173 return show_pwm_temp_sel_common(data, buf,
2174 data->pwm_weight_temp_sel[index]);
2178 store_pwm_weight_temp_sel(struct device *dev, struct device_attribute *attr,
2179 const char *buf, size_t count)
2181 struct nct6775_data *data = nct6775_update_device(dev);
2182 struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr);
2183 int nr = sattr->index;
2187 err = kstrtoul(buf, 10, &val);
2192 if (val && (!(data->have_temp & (1 << (val - 1))) ||
2193 !data->temp_src[val - 1]))
2196 mutex_lock(&data->update_lock);
2198 src = data->temp_src[val - 1];
2199 data->pwm_weight_temp_sel[nr] = src;
2200 reg = nct6775_read_value(data, data->REG_WEIGHT_TEMP_SEL[nr]);
2202 reg |= (src | 0x80);
2203 nct6775_write_value(data, data->REG_WEIGHT_TEMP_SEL[nr], reg);
2205 data->pwm_weight_temp_sel[nr] = 0;
2206 reg = nct6775_read_value(data, data->REG_WEIGHT_TEMP_SEL[nr]);
2208 nct6775_write_value(data, data->REG_WEIGHT_TEMP_SEL[nr], reg);
2210 mutex_unlock(&data->update_lock);
2216 show_target_temp(struct device *dev, struct device_attribute *attr, char *buf)
2218 struct nct6775_data *data = nct6775_update_device(dev);
2219 struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr);
2221 return sprintf(buf, "%d\n", data->target_temp[sattr->index] * 1000);
2225 store_target_temp(struct device *dev, struct device_attribute *attr,
2226 const char *buf, size_t count)
2228 struct nct6775_data *data = dev_get_drvdata(dev);
2229 struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr);
2230 int nr = sattr->index;
2234 err = kstrtoul(buf, 10, &val);
2238 val = clamp_val(DIV_ROUND_CLOSEST(val, 1000), 0,
2239 data->target_temp_mask);
2241 mutex_lock(&data->update_lock);
2242 data->target_temp[nr] = val;
2243 pwm_update_registers(data, nr);
2244 mutex_unlock(&data->update_lock);
2249 show_target_speed(struct device *dev, struct device_attribute *attr, char *buf)
2251 struct nct6775_data *data = nct6775_update_device(dev);
2252 struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr);
2253 int nr = sattr->index;
2255 return sprintf(buf, "%d\n",
2256 fan_from_reg16(data->target_speed[nr],
2257 data->fan_div[nr]));
2261 store_target_speed(struct device *dev, struct device_attribute *attr,
2262 const char *buf, size_t count)
2264 struct nct6775_data *data = dev_get_drvdata(dev);
2265 struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr);
2266 int nr = sattr->index;
2271 err = kstrtoul(buf, 10, &val);
2275 val = clamp_val(val, 0, 1350000U);
2276 speed = fan_to_reg(val, data->fan_div[nr]);
2278 mutex_lock(&data->update_lock);
2279 data->target_speed[nr] = speed;
2280 pwm_update_registers(data, nr);
2281 mutex_unlock(&data->update_lock);
2286 show_temp_tolerance(struct device *dev, struct device_attribute *attr,
2289 struct nct6775_data *data = nct6775_update_device(dev);
2290 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
2292 int index = sattr->index;
2294 return sprintf(buf, "%d\n", data->temp_tolerance[index][nr] * 1000);
2298 store_temp_tolerance(struct device *dev, struct device_attribute *attr,
2299 const char *buf, size_t count)
2301 struct nct6775_data *data = dev_get_drvdata(dev);
2302 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
2304 int index = sattr->index;
2308 err = kstrtoul(buf, 10, &val);
2312 /* Limit tolerance as needed */
2313 val = clamp_val(DIV_ROUND_CLOSEST(val, 1000), 0, data->tolerance_mask);
2315 mutex_lock(&data->update_lock);
2316 data->temp_tolerance[index][nr] = val;
2318 pwm_update_registers(data, nr);
2320 nct6775_write_value(data,
2321 data->REG_CRITICAL_TEMP_TOLERANCE[nr],
2323 mutex_unlock(&data->update_lock);
2328 * Fan speed tolerance is a tricky beast, since the associated register is
2329 * a tick counter, but the value is reported and configured as rpm.
2330 * Compute resulting low and high rpm values and report the difference.
2333 show_speed_tolerance(struct device *dev, struct device_attribute *attr,
2336 struct nct6775_data *data = nct6775_update_device(dev);
2337 struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr);
2338 int nr = sattr->index;
2339 int low = data->target_speed[nr] - data->target_speed_tolerance[nr];
2340 int high = data->target_speed[nr] + data->target_speed_tolerance[nr];
2350 tolerance = (fan_from_reg16(low, data->fan_div[nr])
2351 - fan_from_reg16(high, data->fan_div[nr])) / 2;
2353 return sprintf(buf, "%d\n", tolerance);
2357 store_speed_tolerance(struct device *dev, struct device_attribute *attr,
2358 const char *buf, size_t count)
2360 struct nct6775_data *data = dev_get_drvdata(dev);
2361 struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr);
2362 int nr = sattr->index;
2367 err = kstrtoul(buf, 10, &val);
2371 high = fan_from_reg16(data->target_speed[nr],
2372 data->fan_div[nr]) + val;
2373 low = fan_from_reg16(data->target_speed[nr],
2374 data->fan_div[nr]) - val;
2380 val = (fan_to_reg(low, data->fan_div[nr]) -
2381 fan_to_reg(high, data->fan_div[nr])) / 2;
2383 /* Limit tolerance as needed */
2384 val = clamp_val(val, 0, data->speed_tolerance_limit);
2386 mutex_lock(&data->update_lock);
2387 data->target_speed_tolerance[nr] = val;
2388 pwm_update_registers(data, nr);
2389 mutex_unlock(&data->update_lock);
2393 static SENSOR_DEVICE_ATTR_2(pwm1, S_IWUSR | S_IRUGO, show_pwm, store_pwm, 0, 0);
2394 static SENSOR_DEVICE_ATTR_2(pwm2, S_IWUSR | S_IRUGO, show_pwm, store_pwm, 1, 0);
2395 static SENSOR_DEVICE_ATTR_2(pwm3, S_IWUSR | S_IRUGO, show_pwm, store_pwm, 2, 0);
2396 static SENSOR_DEVICE_ATTR_2(pwm4, S_IWUSR | S_IRUGO, show_pwm, store_pwm, 3, 0);
2397 static SENSOR_DEVICE_ATTR_2(pwm5, S_IWUSR | S_IRUGO, show_pwm, store_pwm, 4, 0);
2399 static SENSOR_DEVICE_ATTR(pwm1_mode, S_IWUSR | S_IRUGO, show_pwm_mode,
2401 static SENSOR_DEVICE_ATTR(pwm2_mode, S_IWUSR | S_IRUGO, show_pwm_mode,
2403 static SENSOR_DEVICE_ATTR(pwm3_mode, S_IWUSR | S_IRUGO, show_pwm_mode,
2405 static SENSOR_DEVICE_ATTR(pwm4_mode, S_IWUSR | S_IRUGO, show_pwm_mode,
2407 static SENSOR_DEVICE_ATTR(pwm5_mode, S_IWUSR | S_IRUGO, show_pwm_mode,
2410 static SENSOR_DEVICE_ATTR(pwm1_enable, S_IWUSR | S_IRUGO, show_pwm_enable,
2411 store_pwm_enable, 0);
2412 static SENSOR_DEVICE_ATTR(pwm2_enable, S_IWUSR | S_IRUGO, show_pwm_enable,
2413 store_pwm_enable, 1);
2414 static SENSOR_DEVICE_ATTR(pwm3_enable, S_IWUSR | S_IRUGO, show_pwm_enable,
2415 store_pwm_enable, 2);
2416 static SENSOR_DEVICE_ATTR(pwm4_enable, S_IWUSR | S_IRUGO, show_pwm_enable,
2417 store_pwm_enable, 3);
2418 static SENSOR_DEVICE_ATTR(pwm5_enable, S_IWUSR | S_IRUGO, show_pwm_enable,
2419 store_pwm_enable, 4);
2421 static SENSOR_DEVICE_ATTR(pwm1_temp_sel, S_IWUSR | S_IRUGO,
2422 show_pwm_temp_sel, store_pwm_temp_sel, 0);
2423 static SENSOR_DEVICE_ATTR(pwm2_temp_sel, S_IWUSR | S_IRUGO,
2424 show_pwm_temp_sel, store_pwm_temp_sel, 1);
2425 static SENSOR_DEVICE_ATTR(pwm3_temp_sel, S_IWUSR | S_IRUGO,
2426 show_pwm_temp_sel, store_pwm_temp_sel, 2);
2427 static SENSOR_DEVICE_ATTR(pwm4_temp_sel, S_IWUSR | S_IRUGO,
2428 show_pwm_temp_sel, store_pwm_temp_sel, 3);
2429 static SENSOR_DEVICE_ATTR(pwm5_temp_sel, S_IWUSR | S_IRUGO,
2430 show_pwm_temp_sel, store_pwm_temp_sel, 4);
2432 static SENSOR_DEVICE_ATTR(pwm1_target_temp, S_IWUSR | S_IRUGO, show_target_temp,
2433 store_target_temp, 0);
2434 static SENSOR_DEVICE_ATTR(pwm2_target_temp, S_IWUSR | S_IRUGO, show_target_temp,
2435 store_target_temp, 1);
2436 static SENSOR_DEVICE_ATTR(pwm3_target_temp, S_IWUSR | S_IRUGO, show_target_temp,
2437 store_target_temp, 2);
2438 static SENSOR_DEVICE_ATTR(pwm4_target_temp, S_IWUSR | S_IRUGO, show_target_temp,
2439 store_target_temp, 3);
2440 static SENSOR_DEVICE_ATTR(pwm5_target_temp, S_IWUSR | S_IRUGO, show_target_temp,
2441 store_target_temp, 4);
2443 static SENSOR_DEVICE_ATTR(fan1_target, S_IWUSR | S_IRUGO, show_target_speed,
2444 store_target_speed, 0);
2445 static SENSOR_DEVICE_ATTR(fan2_target, S_IWUSR | S_IRUGO, show_target_speed,
2446 store_target_speed, 1);
2447 static SENSOR_DEVICE_ATTR(fan3_target, S_IWUSR | S_IRUGO, show_target_speed,
2448 store_target_speed, 2);
2449 static SENSOR_DEVICE_ATTR(fan4_target, S_IWUSR | S_IRUGO, show_target_speed,
2450 store_target_speed, 3);
2451 static SENSOR_DEVICE_ATTR(fan5_target, S_IWUSR | S_IRUGO, show_target_speed,
2452 store_target_speed, 4);
2454 static SENSOR_DEVICE_ATTR(fan1_tolerance, S_IWUSR | S_IRUGO,
2455 show_speed_tolerance, store_speed_tolerance, 0);
2456 static SENSOR_DEVICE_ATTR(fan2_tolerance, S_IWUSR | S_IRUGO,
2457 show_speed_tolerance, store_speed_tolerance, 1);
2458 static SENSOR_DEVICE_ATTR(fan3_tolerance, S_IWUSR | S_IRUGO,
2459 show_speed_tolerance, store_speed_tolerance, 2);
2460 static SENSOR_DEVICE_ATTR(fan4_tolerance, S_IWUSR | S_IRUGO,
2461 show_speed_tolerance, store_speed_tolerance, 3);
2462 static SENSOR_DEVICE_ATTR(fan5_tolerance, S_IWUSR | S_IRUGO,
2463 show_speed_tolerance, store_speed_tolerance, 4);
2465 /* Smart Fan registers */
2468 show_weight_temp(struct device *dev, struct device_attribute *attr, char *buf)
2470 struct nct6775_data *data = nct6775_update_device(dev);
2471 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
2473 int index = sattr->index;
2475 return sprintf(buf, "%d\n", data->weight_temp[index][nr] * 1000);
2479 store_weight_temp(struct device *dev, struct device_attribute *attr,
2480 const char *buf, size_t count)
2482 struct nct6775_data *data = dev_get_drvdata(dev);
2483 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
2485 int index = sattr->index;
2489 err = kstrtoul(buf, 10, &val);
2493 val = clamp_val(DIV_ROUND_CLOSEST(val, 1000), 0, 255);
2495 mutex_lock(&data->update_lock);
2496 data->weight_temp[index][nr] = val;
2497 nct6775_write_value(data, data->REG_WEIGHT_TEMP[index][nr], val);
2498 mutex_unlock(&data->update_lock);
2502 static SENSOR_DEVICE_ATTR(pwm1_weight_temp_sel, S_IWUSR | S_IRUGO,
2503 show_pwm_weight_temp_sel, store_pwm_weight_temp_sel,
2505 static SENSOR_DEVICE_ATTR(pwm2_weight_temp_sel, S_IWUSR | S_IRUGO,
2506 show_pwm_weight_temp_sel, store_pwm_weight_temp_sel,
2508 static SENSOR_DEVICE_ATTR(pwm3_weight_temp_sel, S_IWUSR | S_IRUGO,
2509 show_pwm_weight_temp_sel, store_pwm_weight_temp_sel,
2511 static SENSOR_DEVICE_ATTR(pwm4_weight_temp_sel, S_IWUSR | S_IRUGO,
2512 show_pwm_weight_temp_sel, store_pwm_weight_temp_sel,
2514 static SENSOR_DEVICE_ATTR(pwm5_weight_temp_sel, S_IWUSR | S_IRUGO,
2515 show_pwm_weight_temp_sel, store_pwm_weight_temp_sel,
2518 static SENSOR_DEVICE_ATTR_2(pwm1_weight_temp_step, S_IWUSR | S_IRUGO,
2519 show_weight_temp, store_weight_temp, 0, 0);
2520 static SENSOR_DEVICE_ATTR_2(pwm2_weight_temp_step, S_IWUSR | S_IRUGO,
2521 show_weight_temp, store_weight_temp, 1, 0);
2522 static SENSOR_DEVICE_ATTR_2(pwm3_weight_temp_step, S_IWUSR | S_IRUGO,
2523 show_weight_temp, store_weight_temp, 2, 0);
2524 static SENSOR_DEVICE_ATTR_2(pwm4_weight_temp_step, S_IWUSR | S_IRUGO,
2525 show_weight_temp, store_weight_temp, 3, 0);
2526 static SENSOR_DEVICE_ATTR_2(pwm5_weight_temp_step, S_IWUSR | S_IRUGO,
2527 show_weight_temp, store_weight_temp, 4, 0);
2529 static SENSOR_DEVICE_ATTR_2(pwm1_weight_temp_step_tol, S_IWUSR | S_IRUGO,
2530 show_weight_temp, store_weight_temp, 0, 1);
2531 static SENSOR_DEVICE_ATTR_2(pwm2_weight_temp_step_tol, S_IWUSR | S_IRUGO,
2532 show_weight_temp, store_weight_temp, 1, 1);
2533 static SENSOR_DEVICE_ATTR_2(pwm3_weight_temp_step_tol, S_IWUSR | S_IRUGO,
2534 show_weight_temp, store_weight_temp, 2, 1);
2535 static SENSOR_DEVICE_ATTR_2(pwm4_weight_temp_step_tol, S_IWUSR | S_IRUGO,
2536 show_weight_temp, store_weight_temp, 3, 1);
2537 static SENSOR_DEVICE_ATTR_2(pwm5_weight_temp_step_tol, S_IWUSR | S_IRUGO,
2538 show_weight_temp, store_weight_temp, 4, 1);
2540 static SENSOR_DEVICE_ATTR_2(pwm1_weight_temp_step_base, S_IWUSR | S_IRUGO,
2541 show_weight_temp, store_weight_temp, 0, 2);
2542 static SENSOR_DEVICE_ATTR_2(pwm2_weight_temp_step_base, S_IWUSR | S_IRUGO,
2543 show_weight_temp, store_weight_temp, 1, 2);
2544 static SENSOR_DEVICE_ATTR_2(pwm3_weight_temp_step_base, S_IWUSR | S_IRUGO,
2545 show_weight_temp, store_weight_temp, 2, 2);
2546 static SENSOR_DEVICE_ATTR_2(pwm4_weight_temp_step_base, S_IWUSR | S_IRUGO,
2547 show_weight_temp, store_weight_temp, 3, 2);
2548 static SENSOR_DEVICE_ATTR_2(pwm5_weight_temp_step_base, S_IWUSR | S_IRUGO,
2549 show_weight_temp, store_weight_temp, 4, 2);
2551 static SENSOR_DEVICE_ATTR_2(pwm1_weight_duty_step, S_IWUSR | S_IRUGO,
2552 show_pwm, store_pwm, 0, 5);
2553 static SENSOR_DEVICE_ATTR_2(pwm2_weight_duty_step, S_IWUSR | S_IRUGO,
2554 show_pwm, store_pwm, 1, 5);
2555 static SENSOR_DEVICE_ATTR_2(pwm3_weight_duty_step, S_IWUSR | S_IRUGO,
2556 show_pwm, store_pwm, 2, 5);
2557 static SENSOR_DEVICE_ATTR_2(pwm4_weight_duty_step, S_IWUSR | S_IRUGO,
2558 show_pwm, store_pwm, 3, 5);
2559 static SENSOR_DEVICE_ATTR_2(pwm5_weight_duty_step, S_IWUSR | S_IRUGO,
2560 show_pwm, store_pwm, 4, 5);
2562 /* duty_base is not supported on all chips */
2563 static struct sensor_device_attribute_2 sda_weight_duty_base[] = {
2564 SENSOR_ATTR_2(pwm1_weight_duty_base, S_IWUSR | S_IRUGO,
2565 show_pwm, store_pwm, 0, 6),
2566 SENSOR_ATTR_2(pwm2_weight_duty_base, S_IWUSR | S_IRUGO,
2567 show_pwm, store_pwm, 1, 6),
2568 SENSOR_ATTR_2(pwm3_weight_duty_base, S_IWUSR | S_IRUGO,
2569 show_pwm, store_pwm, 2, 6),
2570 SENSOR_ATTR_2(pwm4_weight_duty_base, S_IWUSR | S_IRUGO,
2571 show_pwm, store_pwm, 3, 6),
2572 SENSOR_ATTR_2(pwm5_weight_duty_base, S_IWUSR | S_IRUGO,
2573 show_pwm, store_pwm, 4, 6),
2577 show_fan_time(struct device *dev, struct device_attribute *attr, char *buf)
2579 struct nct6775_data *data = nct6775_update_device(dev);
2580 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
2582 int index = sattr->index;
2584 return sprintf(buf, "%d\n",
2585 step_time_from_reg(data->fan_time[index][nr],
2586 data->pwm_mode[nr]));
2590 store_fan_time(struct device *dev, struct device_attribute *attr,
2591 const char *buf, size_t count)
2593 struct nct6775_data *data = dev_get_drvdata(dev);
2594 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
2596 int index = sattr->index;
2600 err = kstrtoul(buf, 10, &val);
2604 val = step_time_to_reg(val, data->pwm_mode[nr]);
2605 mutex_lock(&data->update_lock);
2606 data->fan_time[index][nr] = val;
2607 nct6775_write_value(data, data->REG_FAN_TIME[index][nr], val);
2608 mutex_unlock(&data->update_lock);
2613 show_name(struct device *dev, struct device_attribute *attr, char *buf)
2615 struct nct6775_data *data = dev_get_drvdata(dev);
2617 return sprintf(buf, "%s\n", data->name);
2620 static DEVICE_ATTR(name, S_IRUGO, show_name, NULL);
2622 static SENSOR_DEVICE_ATTR_2(pwm1_stop_time, S_IWUSR | S_IRUGO, show_fan_time,
2623 store_fan_time, 0, 0);
2624 static SENSOR_DEVICE_ATTR_2(pwm2_stop_time, S_IWUSR | S_IRUGO, show_fan_time,
2625 store_fan_time, 1, 0);
2626 static SENSOR_DEVICE_ATTR_2(pwm3_stop_time, S_IWUSR | S_IRUGO, show_fan_time,
2627 store_fan_time, 2, 0);
2628 static SENSOR_DEVICE_ATTR_2(pwm4_stop_time, S_IWUSR | S_IRUGO, show_fan_time,
2629 store_fan_time, 3, 0);
2630 static SENSOR_DEVICE_ATTR_2(pwm5_stop_time, S_IWUSR | S_IRUGO, show_fan_time,
2631 store_fan_time, 4, 0);
2633 static SENSOR_DEVICE_ATTR_2(pwm1_step_up_time, S_IWUSR | S_IRUGO, show_fan_time,
2634 store_fan_time, 0, 1);
2635 static SENSOR_DEVICE_ATTR_2(pwm2_step_up_time, S_IWUSR | S_IRUGO, show_fan_time,
2636 store_fan_time, 1, 1);
2637 static SENSOR_DEVICE_ATTR_2(pwm3_step_up_time, S_IWUSR | S_IRUGO, show_fan_time,
2638 store_fan_time, 2, 1);
2639 static SENSOR_DEVICE_ATTR_2(pwm4_step_up_time, S_IWUSR | S_IRUGO, show_fan_time,
2640 store_fan_time, 3, 1);
2641 static SENSOR_DEVICE_ATTR_2(pwm5_step_up_time, S_IWUSR | S_IRUGO, show_fan_time,
2642 store_fan_time, 4, 1);
2644 static SENSOR_DEVICE_ATTR_2(pwm1_step_down_time, S_IWUSR | S_IRUGO,
2645 show_fan_time, store_fan_time, 0, 2);
2646 static SENSOR_DEVICE_ATTR_2(pwm2_step_down_time, S_IWUSR | S_IRUGO,
2647 show_fan_time, store_fan_time, 1, 2);
2648 static SENSOR_DEVICE_ATTR_2(pwm3_step_down_time, S_IWUSR | S_IRUGO,
2649 show_fan_time, store_fan_time, 2, 2);
2650 static SENSOR_DEVICE_ATTR_2(pwm4_step_down_time, S_IWUSR | S_IRUGO,
2651 show_fan_time, store_fan_time, 3, 2);
2652 static SENSOR_DEVICE_ATTR_2(pwm5_step_down_time, S_IWUSR | S_IRUGO,
2653 show_fan_time, store_fan_time, 4, 2);
2655 static SENSOR_DEVICE_ATTR_2(pwm1_start, S_IWUSR | S_IRUGO, show_pwm,
2657 static SENSOR_DEVICE_ATTR_2(pwm2_start, S_IWUSR | S_IRUGO, show_pwm,
2659 static SENSOR_DEVICE_ATTR_2(pwm3_start, S_IWUSR | S_IRUGO, show_pwm,
2661 static SENSOR_DEVICE_ATTR_2(pwm4_start, S_IWUSR | S_IRUGO, show_pwm,
2663 static SENSOR_DEVICE_ATTR_2(pwm5_start, S_IWUSR | S_IRUGO, show_pwm,
2666 static SENSOR_DEVICE_ATTR_2(pwm1_floor, S_IWUSR | S_IRUGO, show_pwm,
2668 static SENSOR_DEVICE_ATTR_2(pwm2_floor, S_IWUSR | S_IRUGO, show_pwm,
2670 static SENSOR_DEVICE_ATTR_2(pwm3_floor, S_IWUSR | S_IRUGO, show_pwm,
2672 static SENSOR_DEVICE_ATTR_2(pwm4_floor, S_IWUSR | S_IRUGO, show_pwm,
2674 static SENSOR_DEVICE_ATTR_2(pwm5_floor, S_IWUSR | S_IRUGO, show_pwm,
2677 static SENSOR_DEVICE_ATTR_2(pwm1_temp_tolerance, S_IWUSR | S_IRUGO,
2678 show_temp_tolerance, store_temp_tolerance, 0, 0);
2679 static SENSOR_DEVICE_ATTR_2(pwm2_temp_tolerance, S_IWUSR | S_IRUGO,
2680 show_temp_tolerance, store_temp_tolerance, 1, 0);
2681 static SENSOR_DEVICE_ATTR_2(pwm3_temp_tolerance, S_IWUSR | S_IRUGO,
2682 show_temp_tolerance, store_temp_tolerance, 2, 0);
2683 static SENSOR_DEVICE_ATTR_2(pwm4_temp_tolerance, S_IWUSR | S_IRUGO,
2684 show_temp_tolerance, store_temp_tolerance, 3, 0);
2685 static SENSOR_DEVICE_ATTR_2(pwm5_temp_tolerance, S_IWUSR | S_IRUGO,
2686 show_temp_tolerance, store_temp_tolerance, 4, 0);
2688 static SENSOR_DEVICE_ATTR_2(pwm1_crit_temp_tolerance, S_IWUSR | S_IRUGO,
2689 show_temp_tolerance, store_temp_tolerance, 0, 1);
2690 static SENSOR_DEVICE_ATTR_2(pwm2_crit_temp_tolerance, S_IWUSR | S_IRUGO,
2691 show_temp_tolerance, store_temp_tolerance, 1, 1);
2692 static SENSOR_DEVICE_ATTR_2(pwm3_crit_temp_tolerance, S_IWUSR | S_IRUGO,
2693 show_temp_tolerance, store_temp_tolerance, 2, 1);
2694 static SENSOR_DEVICE_ATTR_2(pwm4_crit_temp_tolerance, S_IWUSR | S_IRUGO,
2695 show_temp_tolerance, store_temp_tolerance, 3, 1);
2696 static SENSOR_DEVICE_ATTR_2(pwm5_crit_temp_tolerance, S_IWUSR | S_IRUGO,
2697 show_temp_tolerance, store_temp_tolerance, 4, 1);
2699 /* pwm_max is not supported on all chips */
2700 static struct sensor_device_attribute_2 sda_pwm_max[] = {
2701 SENSOR_ATTR_2(pwm1_max, S_IWUSR | S_IRUGO, show_pwm, store_pwm,
2703 SENSOR_ATTR_2(pwm2_max, S_IWUSR | S_IRUGO, show_pwm, store_pwm,
2705 SENSOR_ATTR_2(pwm3_max, S_IWUSR | S_IRUGO, show_pwm, store_pwm,
2707 SENSOR_ATTR_2(pwm4_max, S_IWUSR | S_IRUGO, show_pwm, store_pwm,
2709 SENSOR_ATTR_2(pwm5_max, S_IWUSR | S_IRUGO, show_pwm, store_pwm,
2713 /* pwm_step is not supported on all chips */
2714 static struct sensor_device_attribute_2 sda_pwm_step[] = {
2715 SENSOR_ATTR_2(pwm1_step, S_IWUSR | S_IRUGO, show_pwm, store_pwm, 0, 4),
2716 SENSOR_ATTR_2(pwm2_step, S_IWUSR | S_IRUGO, show_pwm, store_pwm, 1, 4),
2717 SENSOR_ATTR_2(pwm3_step, S_IWUSR | S_IRUGO, show_pwm, store_pwm, 2, 4),
2718 SENSOR_ATTR_2(pwm4_step, S_IWUSR | S_IRUGO, show_pwm, store_pwm, 3, 4),
2719 SENSOR_ATTR_2(pwm5_step, S_IWUSR | S_IRUGO, show_pwm, store_pwm, 4, 4),
2722 static struct attribute *nct6775_attributes_pwm[5][20] = {
2724 &sensor_dev_attr_pwm1.dev_attr.attr,
2725 &sensor_dev_attr_pwm1_mode.dev_attr.attr,
2726 &sensor_dev_attr_pwm1_enable.dev_attr.attr,
2727 &sensor_dev_attr_pwm1_temp_sel.dev_attr.attr,
2728 &sensor_dev_attr_pwm1_temp_tolerance.dev_attr.attr,
2729 &sensor_dev_attr_pwm1_crit_temp_tolerance.dev_attr.attr,
2730 &sensor_dev_attr_pwm1_target_temp.dev_attr.attr,
2731 &sensor_dev_attr_fan1_target.dev_attr.attr,
2732 &sensor_dev_attr_fan1_tolerance.dev_attr.attr,
2733 &sensor_dev_attr_pwm1_stop_time.dev_attr.attr,
2734 &sensor_dev_attr_pwm1_step_up_time.dev_attr.attr,
2735 &sensor_dev_attr_pwm1_step_down_time.dev_attr.attr,
2736 &sensor_dev_attr_pwm1_start.dev_attr.attr,
2737 &sensor_dev_attr_pwm1_floor.dev_attr.attr,
2738 &sensor_dev_attr_pwm1_weight_temp_sel.dev_attr.attr,
2739 &sensor_dev_attr_pwm1_weight_temp_step.dev_attr.attr,
2740 &sensor_dev_attr_pwm1_weight_temp_step_tol.dev_attr.attr,
2741 &sensor_dev_attr_pwm1_weight_temp_step_base.dev_attr.attr,
2742 &sensor_dev_attr_pwm1_weight_duty_step.dev_attr.attr,
2746 &sensor_dev_attr_pwm2.dev_attr.attr,
2747 &sensor_dev_attr_pwm2_mode.dev_attr.attr,
2748 &sensor_dev_attr_pwm2_enable.dev_attr.attr,
2749 &sensor_dev_attr_pwm2_temp_sel.dev_attr.attr,
2750 &sensor_dev_attr_pwm2_temp_tolerance.dev_attr.attr,
2751 &sensor_dev_attr_pwm2_crit_temp_tolerance.dev_attr.attr,
2752 &sensor_dev_attr_pwm2_target_temp.dev_attr.attr,
2753 &sensor_dev_attr_fan2_target.dev_attr.attr,
2754 &sensor_dev_attr_fan2_tolerance.dev_attr.attr,
2755 &sensor_dev_attr_pwm2_stop_time.dev_attr.attr,
2756 &sensor_dev_attr_pwm2_step_up_time.dev_attr.attr,
2757 &sensor_dev_attr_pwm2_step_down_time.dev_attr.attr,
2758 &sensor_dev_attr_pwm2_start.dev_attr.attr,
2759 &sensor_dev_attr_pwm2_floor.dev_attr.attr,
2760 &sensor_dev_attr_pwm2_weight_temp_sel.dev_attr.attr,
2761 &sensor_dev_attr_pwm2_weight_temp_step.dev_attr.attr,
2762 &sensor_dev_attr_pwm2_weight_temp_step_tol.dev_attr.attr,
2763 &sensor_dev_attr_pwm2_weight_temp_step_base.dev_attr.attr,
2764 &sensor_dev_attr_pwm2_weight_duty_step.dev_attr.attr,
2768 &sensor_dev_attr_pwm3.dev_attr.attr,
2769 &sensor_dev_attr_pwm3_mode.dev_attr.attr,
2770 &sensor_dev_attr_pwm3_enable.dev_attr.attr,
2771 &sensor_dev_attr_pwm3_temp_sel.dev_attr.attr,
2772 &sensor_dev_attr_pwm3_temp_tolerance.dev_attr.attr,
2773 &sensor_dev_attr_pwm3_crit_temp_tolerance.dev_attr.attr,
2774 &sensor_dev_attr_pwm3_target_temp.dev_attr.attr,
2775 &sensor_dev_attr_fan3_target.dev_attr.attr,
2776 &sensor_dev_attr_fan3_tolerance.dev_attr.attr,
2777 &sensor_dev_attr_pwm3_stop_time.dev_attr.attr,
2778 &sensor_dev_attr_pwm3_step_up_time.dev_attr.attr,
2779 &sensor_dev_attr_pwm3_step_down_time.dev_attr.attr,
2780 &sensor_dev_attr_pwm3_start.dev_attr.attr,
2781 &sensor_dev_attr_pwm3_floor.dev_attr.attr,
2782 &sensor_dev_attr_pwm3_weight_temp_sel.dev_attr.attr,
2783 &sensor_dev_attr_pwm3_weight_temp_step.dev_attr.attr,
2784 &sensor_dev_attr_pwm3_weight_temp_step_tol.dev_attr.attr,
2785 &sensor_dev_attr_pwm3_weight_temp_step_base.dev_attr.attr,
2786 &sensor_dev_attr_pwm3_weight_duty_step.dev_attr.attr,
2790 &sensor_dev_attr_pwm4.dev_attr.attr,
2791 &sensor_dev_attr_pwm4_mode.dev_attr.attr,
2792 &sensor_dev_attr_pwm4_enable.dev_attr.attr,
2793 &sensor_dev_attr_pwm4_temp_sel.dev_attr.attr,
2794 &sensor_dev_attr_pwm4_temp_tolerance.dev_attr.attr,
2795 &sensor_dev_attr_pwm4_crit_temp_tolerance.dev_attr.attr,
2796 &sensor_dev_attr_pwm4_target_temp.dev_attr.attr,
2797 &sensor_dev_attr_fan4_target.dev_attr.attr,
2798 &sensor_dev_attr_fan4_tolerance.dev_attr.attr,
2799 &sensor_dev_attr_pwm4_stop_time.dev_attr.attr,
2800 &sensor_dev_attr_pwm4_step_up_time.dev_attr.attr,
2801 &sensor_dev_attr_pwm4_step_down_time.dev_attr.attr,
2802 &sensor_dev_attr_pwm4_start.dev_attr.attr,
2803 &sensor_dev_attr_pwm4_floor.dev_attr.attr,
2804 &sensor_dev_attr_pwm4_weight_temp_sel.dev_attr.attr,
2805 &sensor_dev_attr_pwm4_weight_temp_step.dev_attr.attr,
2806 &sensor_dev_attr_pwm4_weight_temp_step_tol.dev_attr.attr,
2807 &sensor_dev_attr_pwm4_weight_temp_step_base.dev_attr.attr,
2808 &sensor_dev_attr_pwm4_weight_duty_step.dev_attr.attr,
2812 &sensor_dev_attr_pwm5.dev_attr.attr,
2813 &sensor_dev_attr_pwm5_mode.dev_attr.attr,
2814 &sensor_dev_attr_pwm5_enable.dev_attr.attr,
2815 &sensor_dev_attr_pwm5_temp_sel.dev_attr.attr,
2816 &sensor_dev_attr_pwm5_temp_tolerance.dev_attr.attr,
2817 &sensor_dev_attr_pwm5_crit_temp_tolerance.dev_attr.attr,
2818 &sensor_dev_attr_pwm5_target_temp.dev_attr.attr,
2819 &sensor_dev_attr_fan5_target.dev_attr.attr,
2820 &sensor_dev_attr_fan5_tolerance.dev_attr.attr,
2821 &sensor_dev_attr_pwm5_stop_time.dev_attr.attr,
2822 &sensor_dev_attr_pwm5_step_up_time.dev_attr.attr,
2823 &sensor_dev_attr_pwm5_step_down_time.dev_attr.attr,
2824 &sensor_dev_attr_pwm5_start.dev_attr.attr,
2825 &sensor_dev_attr_pwm5_floor.dev_attr.attr,
2826 &sensor_dev_attr_pwm5_weight_temp_sel.dev_attr.attr,
2827 &sensor_dev_attr_pwm5_weight_temp_step.dev_attr.attr,
2828 &sensor_dev_attr_pwm5_weight_temp_step_tol.dev_attr.attr,
2829 &sensor_dev_attr_pwm5_weight_temp_step_base.dev_attr.attr,
2830 &sensor_dev_attr_pwm5_weight_duty_step.dev_attr.attr,
2835 static const struct attribute_group nct6775_group_pwm[5] = {
2836 { .attrs = nct6775_attributes_pwm[0] },
2837 { .attrs = nct6775_attributes_pwm[1] },
2838 { .attrs = nct6775_attributes_pwm[2] },
2839 { .attrs = nct6775_attributes_pwm[3] },
2840 { .attrs = nct6775_attributes_pwm[4] },
2844 show_auto_pwm(struct device *dev, struct device_attribute *attr, char *buf)
2846 struct nct6775_data *data = nct6775_update_device(dev);
2847 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
2849 return sprintf(buf, "%d\n", data->auto_pwm[sattr->nr][sattr->index]);
2853 store_auto_pwm(struct device *dev, struct device_attribute *attr,
2854 const char *buf, size_t count)
2856 struct nct6775_data *data = dev_get_drvdata(dev);
2857 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
2859 int point = sattr->index;
2864 err = kstrtoul(buf, 10, &val);
2870 if (point == data->auto_pwm_num) {
2871 if (data->kind != nct6775 && !val)
2873 if (data->kind != nct6779 && val)
2877 mutex_lock(&data->update_lock);
2878 data->auto_pwm[nr][point] = val;
2879 if (point < data->auto_pwm_num) {
2880 nct6775_write_value(data,
2881 NCT6775_AUTO_PWM(data, nr, point),
2882 data->auto_pwm[nr][point]);
2884 switch (data->kind) {
2886 /* disable if needed (pwm == 0) */
2887 reg = nct6775_read_value(data,
2888 NCT6775_REG_CRITICAL_ENAB[nr]);
2893 nct6775_write_value(data, NCT6775_REG_CRITICAL_ENAB[nr],
2897 break; /* always enabled, nothing to do */
2899 nct6775_write_value(data, NCT6779_REG_CRITICAL_PWM[nr],
2901 reg = nct6775_read_value(data,
2902 NCT6779_REG_CRITICAL_PWM_ENABLE[nr]);
2907 nct6775_write_value(data,
2908 NCT6779_REG_CRITICAL_PWM_ENABLE[nr],
2913 mutex_unlock(&data->update_lock);
2918 show_auto_temp(struct device *dev, struct device_attribute *attr, char *buf)
2920 struct nct6775_data *data = nct6775_update_device(dev);
2921 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
2923 int point = sattr->index;
2926 * We don't know for sure if the temperature is signed or unsigned.
2927 * Assume it is unsigned.
2929 return sprintf(buf, "%d\n", data->auto_temp[nr][point] * 1000);
2933 store_auto_temp(struct device *dev, struct device_attribute *attr,
2934 const char *buf, size_t count)
2936 struct nct6775_data *data = dev_get_drvdata(dev);
2937 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
2939 int point = sattr->index;
2943 err = kstrtoul(buf, 10, &val);
2949 mutex_lock(&data->update_lock);
2950 data->auto_temp[nr][point] = DIV_ROUND_CLOSEST(val, 1000);
2951 if (point < data->auto_pwm_num) {
2952 nct6775_write_value(data,
2953 NCT6775_AUTO_TEMP(data, nr, point),
2954 data->auto_temp[nr][point]);
2956 nct6775_write_value(data, data->REG_CRITICAL_TEMP[nr],
2957 data->auto_temp[nr][point]);
2959 mutex_unlock(&data->update_lock);
2964 * The number of auto-point trip points is chip dependent.
2965 * Need to check support while generating/removing attribute files.
2967 static struct sensor_device_attribute_2 sda_auto_pwm_arrays[] = {
2968 SENSOR_ATTR_2(pwm1_auto_point1_pwm, S_IWUSR | S_IRUGO,
2969 show_auto_pwm, store_auto_pwm, 0, 0),
2970 SENSOR_ATTR_2(pwm1_auto_point1_temp, S_IWUSR | S_IRUGO,
2971 show_auto_temp, store_auto_temp, 0, 0),
2972 SENSOR_ATTR_2(pwm1_auto_point2_pwm, S_IWUSR | S_IRUGO,
2973 show_auto_pwm, store_auto_pwm, 0, 1),
2974 SENSOR_ATTR_2(pwm1_auto_point2_temp, S_IWUSR | S_IRUGO,
2975 show_auto_temp, store_auto_temp, 0, 1),
2976 SENSOR_ATTR_2(pwm1_auto_point3_pwm, S_IWUSR | S_IRUGO,
2977 show_auto_pwm, store_auto_pwm, 0, 2),
2978 SENSOR_ATTR_2(pwm1_auto_point3_temp, S_IWUSR | S_IRUGO,
2979 show_auto_temp, store_auto_temp, 0, 2),
2980 SENSOR_ATTR_2(pwm1_auto_point4_pwm, S_IWUSR | S_IRUGO,
2981 show_auto_pwm, store_auto_pwm, 0, 3),
2982 SENSOR_ATTR_2(pwm1_auto_point4_temp, S_IWUSR | S_IRUGO,
2983 show_auto_temp, store_auto_temp, 0, 3),
2984 SENSOR_ATTR_2(pwm1_auto_point5_pwm, S_IWUSR | S_IRUGO,
2985 show_auto_pwm, store_auto_pwm, 0, 4),
2986 SENSOR_ATTR_2(pwm1_auto_point5_temp, S_IWUSR | S_IRUGO,
2987 show_auto_temp, store_auto_temp, 0, 4),
2988 SENSOR_ATTR_2(pwm1_auto_point6_pwm, S_IWUSR | S_IRUGO,
2989 show_auto_pwm, store_auto_pwm, 0, 5),
2990 SENSOR_ATTR_2(pwm1_auto_point6_temp, S_IWUSR | S_IRUGO,
2991 show_auto_temp, store_auto_temp, 0, 5),
2992 SENSOR_ATTR_2(pwm1_auto_point7_pwm, S_IWUSR | S_IRUGO,
2993 show_auto_pwm, store_auto_pwm, 0, 6),
2994 SENSOR_ATTR_2(pwm1_auto_point7_temp, S_IWUSR | S_IRUGO,
2995 show_auto_temp, store_auto_temp, 0, 6),
2997 SENSOR_ATTR_2(pwm2_auto_point1_pwm, S_IWUSR | S_IRUGO,
2998 show_auto_pwm, store_auto_pwm, 1, 0),
2999 SENSOR_ATTR_2(pwm2_auto_point1_temp, S_IWUSR | S_IRUGO,
3000 show_auto_temp, store_auto_temp, 1, 0),
3001 SENSOR_ATTR_2(pwm2_auto_point2_pwm, S_IWUSR | S_IRUGO,
3002 show_auto_pwm, store_auto_pwm, 1, 1),
3003 SENSOR_ATTR_2(pwm2_auto_point2_temp, S_IWUSR | S_IRUGO,
3004 show_auto_temp, store_auto_temp, 1, 1),
3005 SENSOR_ATTR_2(pwm2_auto_point3_pwm, S_IWUSR | S_IRUGO,
3006 show_auto_pwm, store_auto_pwm, 1, 2),
3007 SENSOR_ATTR_2(pwm2_auto_point3_temp, S_IWUSR | S_IRUGO,
3008 show_auto_temp, store_auto_temp, 1, 2),
3009 SENSOR_ATTR_2(pwm2_auto_point4_pwm, S_IWUSR | S_IRUGO,
3010 show_auto_pwm, store_auto_pwm, 1, 3),
3011 SENSOR_ATTR_2(pwm2_auto_point4_temp, S_IWUSR | S_IRUGO,
3012 show_auto_temp, store_auto_temp, 1, 3),
3013 SENSOR_ATTR_2(pwm2_auto_point5_pwm, S_IWUSR | S_IRUGO,
3014 show_auto_pwm, store_auto_pwm, 1, 4),
3015 SENSOR_ATTR_2(pwm2_auto_point5_temp, S_IWUSR | S_IRUGO,
3016 show_auto_temp, store_auto_temp, 1, 4),
3017 SENSOR_ATTR_2(pwm2_auto_point6_pwm, S_IWUSR | S_IRUGO,
3018 show_auto_pwm, store_auto_pwm, 1, 5),
3019 SENSOR_ATTR_2(pwm2_auto_point6_temp, S_IWUSR | S_IRUGO,
3020 show_auto_temp, store_auto_temp, 1, 5),
3021 SENSOR_ATTR_2(pwm2_auto_point7_pwm, S_IWUSR | S_IRUGO,
3022 show_auto_pwm, store_auto_pwm, 1, 6),
3023 SENSOR_ATTR_2(pwm2_auto_point7_temp, S_IWUSR | S_IRUGO,
3024 show_auto_temp, store_auto_temp, 1, 6),
3026 SENSOR_ATTR_2(pwm3_auto_point1_pwm, S_IWUSR | S_IRUGO,
3027 show_auto_pwm, store_auto_pwm, 2, 0),
3028 SENSOR_ATTR_2(pwm3_auto_point1_temp, S_IWUSR | S_IRUGO,
3029 show_auto_temp, store_auto_temp, 2, 0),
3030 SENSOR_ATTR_2(pwm3_auto_point2_pwm, S_IWUSR | S_IRUGO,
3031 show_auto_pwm, store_auto_pwm, 2, 1),
3032 SENSOR_ATTR_2(pwm3_auto_point2_temp, S_IWUSR | S_IRUGO,
3033 show_auto_temp, store_auto_temp, 2, 1),
3034 SENSOR_ATTR_2(pwm3_auto_point3_pwm, S_IWUSR | S_IRUGO,
3035 show_auto_pwm, store_auto_pwm, 2, 2),
3036 SENSOR_ATTR_2(pwm3_auto_point3_temp, S_IWUSR | S_IRUGO,
3037 show_auto_temp, store_auto_temp, 2, 2),
3038 SENSOR_ATTR_2(pwm3_auto_point4_pwm, S_IWUSR | S_IRUGO,
3039 show_auto_pwm, store_auto_pwm, 2, 3),
3040 SENSOR_ATTR_2(pwm3_auto_point4_temp, S_IWUSR | S_IRUGO,
3041 show_auto_temp, store_auto_temp, 2, 3),
3042 SENSOR_ATTR_2(pwm3_auto_point5_pwm, S_IWUSR | S_IRUGO,
3043 show_auto_pwm, store_auto_pwm, 2, 4),
3044 SENSOR_ATTR_2(pwm3_auto_point5_temp, S_IWUSR | S_IRUGO,
3045 show_auto_temp, store_auto_temp, 2, 4),
3046 SENSOR_ATTR_2(pwm3_auto_point6_pwm, S_IWUSR | S_IRUGO,
3047 show_auto_pwm, store_auto_pwm, 2, 5),
3048 SENSOR_ATTR_2(pwm3_auto_point6_temp, S_IWUSR | S_IRUGO,
3049 show_auto_temp, store_auto_temp, 2, 5),
3050 SENSOR_ATTR_2(pwm3_auto_point7_pwm, S_IWUSR | S_IRUGO,
3051 show_auto_pwm, store_auto_pwm, 2, 6),
3052 SENSOR_ATTR_2(pwm3_auto_point7_temp, S_IWUSR | S_IRUGO,
3053 show_auto_temp, store_auto_temp, 2, 6),
3055 SENSOR_ATTR_2(pwm4_auto_point1_pwm, S_IWUSR | S_IRUGO,
3056 show_auto_pwm, store_auto_pwm, 3, 0),
3057 SENSOR_ATTR_2(pwm4_auto_point1_temp, S_IWUSR | S_IRUGO,
3058 show_auto_temp, store_auto_temp, 3, 0),
3059 SENSOR_ATTR_2(pwm4_auto_point2_pwm, S_IWUSR | S_IRUGO,
3060 show_auto_pwm, store_auto_pwm, 3, 1),
3061 SENSOR_ATTR_2(pwm4_auto_point2_temp, S_IWUSR | S_IRUGO,
3062 show_auto_temp, store_auto_temp, 3, 1),
3063 SENSOR_ATTR_2(pwm4_auto_point3_pwm, S_IWUSR | S_IRUGO,
3064 show_auto_pwm, store_auto_pwm, 3, 2),
3065 SENSOR_ATTR_2(pwm4_auto_point3_temp, S_IWUSR | S_IRUGO,
3066 show_auto_temp, store_auto_temp, 3, 2),
3067 SENSOR_ATTR_2(pwm4_auto_point4_pwm, S_IWUSR | S_IRUGO,
3068 show_auto_pwm, store_auto_pwm, 3, 3),
3069 SENSOR_ATTR_2(pwm4_auto_point4_temp, S_IWUSR | S_IRUGO,
3070 show_auto_temp, store_auto_temp, 3, 3),
3071 SENSOR_ATTR_2(pwm4_auto_point5_pwm, S_IWUSR | S_IRUGO,
3072 show_auto_pwm, store_auto_pwm, 3, 4),
3073 SENSOR_ATTR_2(pwm4_auto_point5_temp, S_IWUSR | S_IRUGO,
3074 show_auto_temp, store_auto_temp, 3, 4),
3075 SENSOR_ATTR_2(pwm4_auto_point6_pwm, S_IWUSR | S_IRUGO,
3076 show_auto_pwm, store_auto_pwm, 3, 5),
3077 SENSOR_ATTR_2(pwm4_auto_point6_temp, S_IWUSR | S_IRUGO,
3078 show_auto_temp, store_auto_temp, 3, 5),
3079 SENSOR_ATTR_2(pwm4_auto_point7_pwm, S_IWUSR | S_IRUGO,
3080 show_auto_pwm, store_auto_pwm, 3, 6),
3081 SENSOR_ATTR_2(pwm4_auto_point7_temp, S_IWUSR | S_IRUGO,
3082 show_auto_temp, store_auto_temp, 3, 6),
3084 SENSOR_ATTR_2(pwm5_auto_point1_pwm, S_IWUSR | S_IRUGO,
3085 show_auto_pwm, store_auto_pwm, 4, 0),
3086 SENSOR_ATTR_2(pwm5_auto_point1_temp, S_IWUSR | S_IRUGO,
3087 show_auto_temp, store_auto_temp, 4, 0),
3088 SENSOR_ATTR_2(pwm5_auto_point2_pwm, S_IWUSR | S_IRUGO,
3089 show_auto_pwm, store_auto_pwm, 4, 1),
3090 SENSOR_ATTR_2(pwm5_auto_point2_temp, S_IWUSR | S_IRUGO,
3091 show_auto_temp, store_auto_temp, 4, 1),
3092 SENSOR_ATTR_2(pwm5_auto_point3_pwm, S_IWUSR | S_IRUGO,
3093 show_auto_pwm, store_auto_pwm, 4, 2),
3094 SENSOR_ATTR_2(pwm5_auto_point3_temp, S_IWUSR | S_IRUGO,
3095 show_auto_temp, store_auto_temp, 4, 2),
3096 SENSOR_ATTR_2(pwm5_auto_point4_pwm, S_IWUSR | S_IRUGO,
3097 show_auto_pwm, store_auto_pwm, 4, 3),
3098 SENSOR_ATTR_2(pwm5_auto_point4_temp, S_IWUSR | S_IRUGO,
3099 show_auto_temp, store_auto_temp, 4, 3),
3100 SENSOR_ATTR_2(pwm5_auto_point5_pwm, S_IWUSR | S_IRUGO,
3101 show_auto_pwm, store_auto_pwm, 4, 4),
3102 SENSOR_ATTR_2(pwm5_auto_point5_temp, S_IWUSR | S_IRUGO,
3103 show_auto_temp, store_auto_temp, 4, 4),
3104 SENSOR_ATTR_2(pwm5_auto_point6_pwm, S_IWUSR | S_IRUGO,
3105 show_auto_pwm, store_auto_pwm, 4, 5),
3106 SENSOR_ATTR_2(pwm5_auto_point6_temp, S_IWUSR | S_IRUGO,
3107 show_auto_temp, store_auto_temp, 4, 5),
3108 SENSOR_ATTR_2(pwm5_auto_point7_pwm, S_IWUSR | S_IRUGO,
3109 show_auto_pwm, store_auto_pwm, 4, 6),
3110 SENSOR_ATTR_2(pwm5_auto_point7_temp, S_IWUSR | S_IRUGO,
3111 show_auto_temp, store_auto_temp, 4, 6),
3115 show_vid(struct device *dev, struct device_attribute *attr, char *buf)
3117 struct nct6775_data *data = dev_get_drvdata(dev);
3118 return sprintf(buf, "%d\n", vid_from_reg(data->vid, data->vrm));
3121 static DEVICE_ATTR(cpu0_vid, S_IRUGO, show_vid, NULL);
3123 /* Case open detection */
3126 clear_caseopen(struct device *dev, struct device_attribute *attr,
3127 const char *buf, size_t count)
3129 struct nct6775_data *data = dev_get_drvdata(dev);
3130 struct nct6775_sio_data *sio_data = dev->platform_data;
3131 int nr = to_sensor_dev_attr(attr)->index - INTRUSION_ALARM_BASE;
3136 if (kstrtoul(buf, 10, &val) || val != 0)
3139 mutex_lock(&data->update_lock);
3142 * Use CR registers to clear caseopen status.
3143 * The CR registers are the same for all chips, and not all chips
3144 * support clearing the caseopen status through "regular" registers.
3146 ret = superio_enter(sio_data->sioreg);
3152 superio_select(sio_data->sioreg, NCT6775_LD_ACPI);
3153 reg = superio_inb(sio_data->sioreg, NCT6775_REG_CR_CASEOPEN_CLR[nr]);
3154 reg |= NCT6775_CR_CASEOPEN_CLR_MASK[nr];
3155 superio_outb(sio_data->sioreg, NCT6775_REG_CR_CASEOPEN_CLR[nr], reg);
3156 reg &= ~NCT6775_CR_CASEOPEN_CLR_MASK[nr];
3157 superio_outb(sio_data->sioreg, NCT6775_REG_CR_CASEOPEN_CLR[nr], reg);
3158 superio_exit(sio_data->sioreg);
3160 data->valid = false; /* Force cache refresh */
3162 mutex_unlock(&data->update_lock);
3166 static struct sensor_device_attribute sda_caseopen[] = {
3167 SENSOR_ATTR(intrusion0_alarm, S_IWUSR | S_IRUGO, show_alarm,
3168 clear_caseopen, INTRUSION_ALARM_BASE),
3169 SENSOR_ATTR(intrusion1_alarm, S_IWUSR | S_IRUGO, show_alarm,
3170 clear_caseopen, INTRUSION_ALARM_BASE + 1),
3174 * Driver and device management
3177 static void nct6775_device_remove_files(struct device *dev)
3180 * some entries in the following arrays may not have been used in
3181 * device_create_file(), but device_remove_file() will ignore them
3184 struct nct6775_data *data = dev_get_drvdata(dev);
3186 for (i = 0; i < data->pwm_num; i++)
3187 sysfs_remove_group(&dev->kobj, &nct6775_group_pwm[i]);
3189 for (i = 0; i < ARRAY_SIZE(sda_pwm_max); i++)
3190 device_remove_file(dev, &sda_pwm_max[i].dev_attr);
3192 for (i = 0; i < ARRAY_SIZE(sda_pwm_step); i++)
3193 device_remove_file(dev, &sda_pwm_step[i].dev_attr);
3195 for (i = 0; i < ARRAY_SIZE(sda_weight_duty_base); i++)
3196 device_remove_file(dev, &sda_weight_duty_base[i].dev_attr);
3198 for (i = 0; i < ARRAY_SIZE(sda_auto_pwm_arrays); i++)
3199 device_remove_file(dev, &sda_auto_pwm_arrays[i].dev_attr);
3201 for (i = 0; i < data->in_num; i++)
3202 sysfs_remove_group(&dev->kobj, &nct6775_group_in[i]);
3204 for (i = 0; i < 5; i++) {
3205 device_remove_file(dev, &sda_fan_input[i].dev_attr);
3206 device_remove_file(dev, &sda_fan_alarm[i].dev_attr);
3207 device_remove_file(dev, &sda_fan_div[i].dev_attr);
3208 device_remove_file(dev, &sda_fan_min[i].dev_attr);
3209 device_remove_file(dev, &sda_fan_pulses[i].dev_attr);
3211 for (i = 0; i < NUM_TEMP; i++) {
3212 if (!(data->have_temp & (1 << i)))
3214 device_remove_file(dev, &sda_temp_input[i].dev_attr);
3215 device_remove_file(dev, &sda_temp_label[i].dev_attr);
3216 device_remove_file(dev, &sda_temp_max[i].dev_attr);
3217 device_remove_file(dev, &sda_temp_max_hyst[i].dev_attr);
3218 device_remove_file(dev, &sda_temp_crit[i].dev_attr);
3219 if (!(data->have_temp_fixed & (1 << i)))
3221 device_remove_file(dev, &sda_temp_type[i].dev_attr);
3222 device_remove_file(dev, &sda_temp_offset[i].dev_attr);
3223 if (i >= NUM_TEMP_ALARM)
3225 device_remove_file(dev, &sda_temp_alarm[i].dev_attr);
3228 device_remove_file(dev, &sda_caseopen[0].dev_attr);
3229 device_remove_file(dev, &sda_caseopen[1].dev_attr);
3231 device_remove_file(dev, &dev_attr_name);
3232 device_remove_file(dev, &dev_attr_cpu0_vid);
3235 /* Get the monitoring functions started */
3236 static inline void nct6775_init_device(struct nct6775_data *data)
3241 /* Start monitoring if needed */
3242 if (data->REG_CONFIG) {
3243 tmp = nct6775_read_value(data, data->REG_CONFIG);
3245 nct6775_write_value(data, data->REG_CONFIG, tmp | 0x01);
3248 /* Enable temperature sensors if needed */
3249 for (i = 0; i < NUM_TEMP; i++) {
3250 if (!(data->have_temp & (1 << i)))
3252 if (!data->reg_temp_config[i])
3254 tmp = nct6775_read_value(data, data->reg_temp_config[i]);
3256 nct6775_write_value(data, data->reg_temp_config[i],
3260 /* Enable VBAT monitoring if needed */
3261 tmp = nct6775_read_value(data, data->REG_VBAT);
3263 nct6775_write_value(data, data->REG_VBAT, tmp | 0x01);
3265 diode = nct6775_read_value(data, data->REG_DIODE);
3267 for (i = 0; i < data->temp_fixed_num; i++) {
3268 if (!(data->have_temp_fixed & (1 << i)))
3270 if ((tmp & (0x02 << i))) /* diode */
3271 data->temp_type[i] = 3 - ((diode >> i) & 0x02);
3272 else /* thermistor */
3273 data->temp_type[i] = 4;
3278 nct6775_check_fan_inputs(const struct nct6775_sio_data *sio_data,
3279 struct nct6775_data *data)
3282 bool fan3pin, fan3min, fan4pin, fan4min, fan5pin;
3283 bool pwm3pin, pwm4pin, pwm5pin;
3286 ret = superio_enter(sio_data->sioreg);
3290 /* fan4 and fan5 share some pins with the GPIO and serial flash */
3291 if (data->kind == nct6775) {
3292 regval = superio_inb(sio_data->sioreg, 0x2c);
3294 fan3pin = regval & (1 << 6);
3296 pwm3pin = regval & (1 << 7);
3298 /* On NCT6775, fan4 shares pins with the fdc interface */
3299 fan4pin = !(superio_inb(sio_data->sioreg, 0x2A) & 0x80);
3304 } else if (data->kind == nct6776) {
3305 bool gpok = superio_inb(sio_data->sioreg, 0x27) & 0x80;
3307 superio_select(sio_data->sioreg, NCT6775_LD_HWM);
3308 regval = superio_inb(sio_data->sioreg, SIO_REG_ENABLE);
3313 fan3pin = !(superio_inb(sio_data->sioreg, 0x24) & 0x40);
3318 fan4pin = superio_inb(sio_data->sioreg, 0x1C) & 0x01;
3323 fan5pin = superio_inb(sio_data->sioreg, 0x1C) & 0x02;
3330 } else { /* NCT6779D */
3331 regval = superio_inb(sio_data->sioreg, 0x1c);
3333 fan3pin = !(regval & (1 << 5));
3334 fan4pin = !(regval & (1 << 6));
3335 fan5pin = !(regval & (1 << 7));
3337 pwm3pin = !(regval & (1 << 0));
3338 pwm4pin = !(regval & (1 << 1));
3339 pwm5pin = !(regval & (1 << 2));
3345 superio_exit(sio_data->sioreg);
3347 data->has_fan = data->has_fan_min = 0x03; /* fan1 and fan2 */
3348 data->has_fan |= fan3pin << 2;
3349 data->has_fan_min |= fan3min << 2;
3351 data->has_fan |= (fan4pin << 3) | (fan5pin << 4);
3352 data->has_fan_min |= (fan4min << 3) | (fan5pin << 4);
3354 data->has_pwm = 0x03 | (pwm3pin << 2) | (pwm4pin << 3) | (pwm5pin << 4);
3359 static void add_temp_sensors(struct nct6775_data *data, const u16 *regp,
3360 int *available, int *mask)
3365 for (i = 0; i < data->pwm_num && *available; i++) {
3370 src = nct6775_read_value(data, regp[i]);
3372 if (!src || (*mask & (1 << src)))
3374 if (src >= data->temp_label_num ||
3375 !strlen(data->temp_label[src]))
3378 index = __ffs(*available);
3379 nct6775_write_value(data, data->REG_TEMP_SOURCE[index], src);
3380 *available &= ~(1 << index);
3385 static int nct6775_probe(struct platform_device *pdev)
3387 struct device *dev = &pdev->dev;
3388 struct nct6775_sio_data *sio_data = dev->platform_data;
3389 struct nct6775_data *data;
3390 struct resource *res;
3392 int src, mask, available;
3393 const u16 *reg_temp, *reg_temp_over, *reg_temp_hyst, *reg_temp_config;
3394 const u16 *reg_temp_alternate, *reg_temp_crit;
3396 bool have_vid = false;
3399 res = platform_get_resource(pdev, IORESOURCE_IO, 0);
3400 if (!devm_request_region(&pdev->dev, res->start, IOREGION_LENGTH,
3404 data = devm_kzalloc(&pdev->dev, sizeof(struct nct6775_data),
3409 data->kind = sio_data->kind;
3410 data->addr = res->start;
3411 mutex_init(&data->update_lock);
3412 data->name = nct6775_device_names[data->kind];
3413 data->bank = 0xff; /* Force initial bank selection */
3414 platform_set_drvdata(pdev, data);
3416 switch (data->kind) {
3420 data->auto_pwm_num = 6;
3421 data->has_fan_div = true;
3422 data->temp_fixed_num = 3;
3424 data->ALARM_BITS = NCT6775_ALARM_BITS;
3426 data->fan_from_reg = fan_from_reg16;
3427 data->fan_from_reg_min = fan_from_reg8;
3428 data->target_temp_mask = 0x7f;
3429 data->tolerance_mask = 0x0f;
3430 data->speed_tolerance_limit = 15;
3432 data->temp_label = nct6775_temp_label;
3433 data->temp_label_num = ARRAY_SIZE(nct6775_temp_label);
3435 data->REG_CONFIG = NCT6775_REG_CONFIG;
3436 data->REG_VBAT = NCT6775_REG_VBAT;
3437 data->REG_DIODE = NCT6775_REG_DIODE;
3438 data->REG_VIN = NCT6775_REG_IN;
3439 data->REG_IN_MINMAX[0] = NCT6775_REG_IN_MIN;
3440 data->REG_IN_MINMAX[1] = NCT6775_REG_IN_MAX;
3441 data->REG_TARGET = NCT6775_REG_TARGET;
3442 data->REG_FAN = NCT6775_REG_FAN;
3443 data->REG_FAN_MODE = NCT6775_REG_FAN_MODE;
3444 data->REG_FAN_MIN = NCT6775_REG_FAN_MIN;
3445 data->REG_FAN_PULSES = NCT6775_REG_FAN_PULSES;
3446 data->REG_FAN_TIME[0] = NCT6775_REG_FAN_STOP_TIME;
3447 data->REG_FAN_TIME[1] = NCT6775_REG_FAN_STEP_UP_TIME;
3448 data->REG_FAN_TIME[2] = NCT6775_REG_FAN_STEP_DOWN_TIME;
3449 data->REG_PWM[0] = NCT6775_REG_PWM;
3450 data->REG_PWM[1] = NCT6775_REG_FAN_START_OUTPUT;
3451 data->REG_PWM[2] = NCT6775_REG_FAN_STOP_OUTPUT;
3452 data->REG_PWM[3] = NCT6775_REG_FAN_MAX_OUTPUT;
3453 data->REG_PWM[4] = NCT6775_REG_FAN_STEP_OUTPUT;
3454 data->REG_PWM[5] = NCT6775_REG_WEIGHT_DUTY_STEP;
3455 data->REG_PWM_READ = NCT6775_REG_PWM_READ;
3456 data->REG_PWM_MODE = NCT6775_REG_PWM_MODE;
3457 data->PWM_MODE_MASK = NCT6775_PWM_MODE_MASK;
3458 data->REG_AUTO_TEMP = NCT6775_REG_AUTO_TEMP;
3459 data->REG_AUTO_PWM = NCT6775_REG_AUTO_PWM;
3460 data->REG_CRITICAL_TEMP = NCT6775_REG_CRITICAL_TEMP;
3461 data->REG_CRITICAL_TEMP_TOLERANCE
3462 = NCT6775_REG_CRITICAL_TEMP_TOLERANCE;
3463 data->REG_TEMP_OFFSET = NCT6775_REG_TEMP_OFFSET;
3464 data->REG_TEMP_SOURCE = NCT6775_REG_TEMP_SOURCE;
3465 data->REG_TEMP_SEL = NCT6775_REG_TEMP_SEL;
3466 data->REG_WEIGHT_TEMP_SEL = NCT6775_REG_WEIGHT_TEMP_SEL;
3467 data->REG_WEIGHT_TEMP[0] = NCT6775_REG_WEIGHT_TEMP_STEP;
3468 data->REG_WEIGHT_TEMP[1] = NCT6775_REG_WEIGHT_TEMP_STEP_TOL;
3469 data->REG_WEIGHT_TEMP[2] = NCT6775_REG_WEIGHT_TEMP_BASE;
3470 data->REG_ALARM = NCT6775_REG_ALARM;
3472 reg_temp = NCT6775_REG_TEMP;
3473 num_reg_temp = ARRAY_SIZE(NCT6775_REG_TEMP);
3474 reg_temp_over = NCT6775_REG_TEMP_OVER;
3475 reg_temp_hyst = NCT6775_REG_TEMP_HYST;
3476 reg_temp_config = NCT6775_REG_TEMP_CONFIG;
3477 reg_temp_alternate = NCT6775_REG_TEMP_ALTERNATE;
3478 reg_temp_crit = NCT6775_REG_TEMP_CRIT;
3484 data->auto_pwm_num = 4;
3485 data->has_fan_div = false;
3486 data->temp_fixed_num = 3;
3488 data->ALARM_BITS = NCT6776_ALARM_BITS;
3490 data->fan_from_reg = fan_from_reg13;
3491 data->fan_from_reg_min = fan_from_reg13;
3492 data->target_temp_mask = 0xff;
3493 data->tolerance_mask = 0x07;
3494 data->speed_tolerance_limit = 63;
3496 data->temp_label = nct6776_temp_label;
3497 data->temp_label_num = ARRAY_SIZE(nct6776_temp_label);
3499 data->REG_CONFIG = NCT6775_REG_CONFIG;
3500 data->REG_VBAT = NCT6775_REG_VBAT;
3501 data->REG_DIODE = NCT6775_REG_DIODE;
3502 data->REG_VIN = NCT6775_REG_IN;
3503 data->REG_IN_MINMAX[0] = NCT6775_REG_IN_MIN;
3504 data->REG_IN_MINMAX[1] = NCT6775_REG_IN_MAX;
3505 data->REG_TARGET = NCT6775_REG_TARGET;
3506 data->REG_FAN = NCT6775_REG_FAN;
3507 data->REG_FAN_MODE = NCT6775_REG_FAN_MODE;
3508 data->REG_FAN_MIN = NCT6776_REG_FAN_MIN;
3509 data->REG_FAN_PULSES = NCT6776_REG_FAN_PULSES;
3510 data->REG_FAN_TIME[0] = NCT6775_REG_FAN_STOP_TIME;
3511 data->REG_FAN_TIME[1] = NCT6775_REG_FAN_STEP_UP_TIME;
3512 data->REG_FAN_TIME[2] = NCT6775_REG_FAN_STEP_DOWN_TIME;
3513 data->REG_TOLERANCE_H = NCT6776_REG_TOLERANCE_H;
3514 data->REG_PWM[0] = NCT6775_REG_PWM;
3515 data->REG_PWM[1] = NCT6775_REG_FAN_START_OUTPUT;
3516 data->REG_PWM[2] = NCT6775_REG_FAN_STOP_OUTPUT;
3517 data->REG_PWM[5] = NCT6775_REG_WEIGHT_DUTY_STEP;
3518 data->REG_PWM[6] = NCT6776_REG_WEIGHT_DUTY_BASE;
3519 data->REG_PWM_READ = NCT6775_REG_PWM_READ;
3520 data->REG_PWM_MODE = NCT6776_REG_PWM_MODE;
3521 data->PWM_MODE_MASK = NCT6776_PWM_MODE_MASK;
3522 data->REG_AUTO_TEMP = NCT6775_REG_AUTO_TEMP;
3523 data->REG_AUTO_PWM = NCT6775_REG_AUTO_PWM;
3524 data->REG_CRITICAL_TEMP = NCT6775_REG_CRITICAL_TEMP;
3525 data->REG_CRITICAL_TEMP_TOLERANCE
3526 = NCT6775_REG_CRITICAL_TEMP_TOLERANCE;
3527 data->REG_TEMP_OFFSET = NCT6775_REG_TEMP_OFFSET;
3528 data->REG_TEMP_SOURCE = NCT6775_REG_TEMP_SOURCE;
3529 data->REG_TEMP_SEL = NCT6775_REG_TEMP_SEL;
3530 data->REG_WEIGHT_TEMP_SEL = NCT6775_REG_WEIGHT_TEMP_SEL;
3531 data->REG_WEIGHT_TEMP[0] = NCT6775_REG_WEIGHT_TEMP_STEP;
3532 data->REG_WEIGHT_TEMP[1] = NCT6775_REG_WEIGHT_TEMP_STEP_TOL;
3533 data->REG_WEIGHT_TEMP[2] = NCT6775_REG_WEIGHT_TEMP_BASE;
3534 data->REG_ALARM = NCT6775_REG_ALARM;
3536 reg_temp = NCT6775_REG_TEMP;
3537 num_reg_temp = ARRAY_SIZE(NCT6775_REG_TEMP);
3538 reg_temp_over = NCT6775_REG_TEMP_OVER;
3539 reg_temp_hyst = NCT6775_REG_TEMP_HYST;
3540 reg_temp_config = NCT6776_REG_TEMP_CONFIG;
3541 reg_temp_alternate = NCT6776_REG_TEMP_ALTERNATE;
3542 reg_temp_crit = NCT6776_REG_TEMP_CRIT;
3548 data->auto_pwm_num = 4;
3549 data->has_fan_div = false;
3550 data->temp_fixed_num = 6;
3552 data->ALARM_BITS = NCT6779_ALARM_BITS;
3554 data->fan_from_reg = fan_from_reg13;
3555 data->fan_from_reg_min = fan_from_reg13;
3556 data->target_temp_mask = 0xff;
3557 data->tolerance_mask = 0x07;
3558 data->speed_tolerance_limit = 63;
3560 data->temp_label = nct6779_temp_label;
3561 data->temp_label_num = ARRAY_SIZE(nct6779_temp_label);
3563 data->REG_CONFIG = NCT6775_REG_CONFIG;
3564 data->REG_VBAT = NCT6775_REG_VBAT;
3565 data->REG_DIODE = NCT6775_REG_DIODE;
3566 data->REG_VIN = NCT6779_REG_IN;
3567 data->REG_IN_MINMAX[0] = NCT6775_REG_IN_MIN;
3568 data->REG_IN_MINMAX[1] = NCT6775_REG_IN_MAX;
3569 data->REG_TARGET = NCT6775_REG_TARGET;
3570 data->REG_FAN = NCT6779_REG_FAN;
3571 data->REG_FAN_MODE = NCT6775_REG_FAN_MODE;
3572 data->REG_FAN_MIN = NCT6776_REG_FAN_MIN;
3573 data->REG_FAN_PULSES = NCT6779_REG_FAN_PULSES;
3574 data->REG_FAN_TIME[0] = NCT6775_REG_FAN_STOP_TIME;
3575 data->REG_FAN_TIME[1] = NCT6775_REG_FAN_STEP_UP_TIME;
3576 data->REG_FAN_TIME[2] = NCT6775_REG_FAN_STEP_DOWN_TIME;
3577 data->REG_TOLERANCE_H = NCT6776_REG_TOLERANCE_H;
3578 data->REG_PWM[0] = NCT6775_REG_PWM;
3579 data->REG_PWM[1] = NCT6775_REG_FAN_START_OUTPUT;
3580 data->REG_PWM[2] = NCT6775_REG_FAN_STOP_OUTPUT;
3581 data->REG_PWM[5] = NCT6775_REG_WEIGHT_DUTY_STEP;
3582 data->REG_PWM[6] = NCT6776_REG_WEIGHT_DUTY_BASE;
3583 data->REG_PWM_READ = NCT6775_REG_PWM_READ;
3584 data->REG_PWM_MODE = NCT6776_REG_PWM_MODE;
3585 data->PWM_MODE_MASK = NCT6776_PWM_MODE_MASK;
3586 data->REG_AUTO_TEMP = NCT6775_REG_AUTO_TEMP;
3587 data->REG_AUTO_PWM = NCT6775_REG_AUTO_PWM;
3588 data->REG_CRITICAL_TEMP = NCT6775_REG_CRITICAL_TEMP;
3589 data->REG_CRITICAL_TEMP_TOLERANCE
3590 = NCT6775_REG_CRITICAL_TEMP_TOLERANCE;
3591 data->REG_TEMP_OFFSET = NCT6779_REG_TEMP_OFFSET;
3592 data->REG_TEMP_SOURCE = NCT6775_REG_TEMP_SOURCE;
3593 data->REG_TEMP_SEL = NCT6775_REG_TEMP_SEL;
3594 data->REG_WEIGHT_TEMP_SEL = NCT6775_REG_WEIGHT_TEMP_SEL;
3595 data->REG_WEIGHT_TEMP[0] = NCT6775_REG_WEIGHT_TEMP_STEP;
3596 data->REG_WEIGHT_TEMP[1] = NCT6775_REG_WEIGHT_TEMP_STEP_TOL;
3597 data->REG_WEIGHT_TEMP[2] = NCT6775_REG_WEIGHT_TEMP_BASE;
3598 data->REG_ALARM = NCT6779_REG_ALARM;
3600 reg_temp = NCT6779_REG_TEMP;
3601 num_reg_temp = ARRAY_SIZE(NCT6779_REG_TEMP);
3602 reg_temp_over = NCT6779_REG_TEMP_OVER;
3603 reg_temp_hyst = NCT6779_REG_TEMP_HYST;
3604 reg_temp_config = NCT6779_REG_TEMP_CONFIG;
3605 reg_temp_alternate = NCT6779_REG_TEMP_ALTERNATE;
3606 reg_temp_crit = NCT6779_REG_TEMP_CRIT;
3612 data->have_in = (1 << data->in_num) - 1;
3613 data->have_temp = 0;
3616 * On some boards, not all available temperature sources are monitored,
3617 * even though some of the monitoring registers are unused.
3618 * Get list of unused monitoring registers, then detect if any fan
3619 * controls are configured to use unmonitored temperature sources.
3620 * If so, assign the unmonitored temperature sources to available
3621 * monitoring registers.
3625 for (i = 0; i < num_reg_temp; i++) {
3626 if (reg_temp[i] == 0)
3629 src = nct6775_read_value(data, data->REG_TEMP_SOURCE[i]) & 0x1f;
3630 if (!src || (mask & (1 << src)))
3631 available |= 1 << i;
3637 * Now find unmonitored temperature registers and enable monitoring
3638 * if additional monitoring registers are available.
3640 add_temp_sensors(data, data->REG_TEMP_SEL, &available, &mask);
3641 add_temp_sensors(data, data->REG_WEIGHT_TEMP_SEL, &available, &mask);
3644 s = NUM_TEMP_FIXED; /* First dynamic temperature attribute */
3645 for (i = 0; i < num_reg_temp; i++) {
3646 if (reg_temp[i] == 0)
3649 src = nct6775_read_value(data, data->REG_TEMP_SOURCE[i]) & 0x1f;
3650 if (!src || (mask & (1 << src)))
3653 if (src >= data->temp_label_num ||
3654 !strlen(data->temp_label[src])) {
3656 "Invalid temperature source %d at index %d, source register 0x%x, temp register 0x%x\n",
3657 src, i, data->REG_TEMP_SOURCE[i], reg_temp[i]);
3663 /* Use fixed index for SYSTIN(1), CPUTIN(2), AUXTIN(3) */
3664 if (src <= data->temp_fixed_num) {
3665 data->have_temp |= 1 << (src - 1);
3666 data->have_temp_fixed |= 1 << (src - 1);
3667 data->reg_temp[0][src - 1] = reg_temp[i];
3668 data->reg_temp[1][src - 1] = reg_temp_over[i];
3669 data->reg_temp[2][src - 1] = reg_temp_hyst[i];
3670 data->reg_temp_config[src - 1] = reg_temp_config[i];
3671 data->temp_src[src - 1] = src;
3678 /* Use dynamic index for other sources */
3679 data->have_temp |= 1 << s;
3680 data->reg_temp[0][s] = reg_temp[i];
3681 data->reg_temp[1][s] = reg_temp_over[i];
3682 data->reg_temp[2][s] = reg_temp_hyst[i];
3683 data->reg_temp_config[s] = reg_temp_config[i];
3684 if (reg_temp_crit[src - 1])
3685 data->reg_temp[3][s] = reg_temp_crit[src - 1];
3687 data->temp_src[s] = src;
3691 #ifdef USE_ALTERNATE
3693 * Go through the list of alternate temp registers and enable
3695 * The temperature is already monitored if the respective bit in <mask>
3698 for (i = 0; i < data->temp_label_num - 1; i++) {
3699 if (!reg_temp_alternate[i])
3701 if (mask & (1 << (i + 1)))
3703 if (i < data->temp_fixed_num) {
3704 if (data->have_temp & (1 << i))
3706 data->have_temp |= 1 << i;
3707 data->have_temp_fixed |= 1 << i;
3708 data->reg_temp[0][i] = reg_temp_alternate[i];
3709 data->reg_temp[1][i] = reg_temp_over[i];
3710 data->reg_temp[2][i] = reg_temp_hyst[i];
3711 data->temp_src[i] = i + 1;
3715 if (s >= NUM_TEMP) /* Abort if no more space */
3718 data->have_temp |= 1 << s;
3719 data->reg_temp[0][s] = reg_temp_alternate[i];
3720 data->temp_src[s] = i + 1;
3723 #endif /* USE_ALTERNATE */
3725 switch (data->kind) {
3730 * On NCT6776, AUXTIN and VIN3 pins are shared.
3731 * Only way to detect it is to check if AUXTIN is used
3732 * as a temperature source, and if that source is
3735 * If that is the case, disable in6, which reports VIN3.
3736 * Otherwise disable temp3.
3738 if (data->have_temp & (1 << 2)) {
3739 u8 reg = nct6775_read_value(data,
3740 data->reg_temp_config[2]);
3742 data->have_temp &= ~(1 << 2);
3744 data->have_in &= ~(1 << 6);
3755 * There does not seem to be a clean way to detect if VINx or
3756 * AUXTINx is active, so for keep both sensor types enabled
3762 /* Initialize the chip */
3763 nct6775_init_device(data);
3765 err = superio_enter(sio_data->sioreg);
3769 cr2a = superio_inb(sio_data->sioreg, 0x2a);
3770 switch (data->kind) {
3772 have_vid = (cr2a & 0x40);
3775 have_vid = (cr2a & 0x60) == 0x40;
3783 * We can get the VID input values directly at logical device D 0xe3.
3786 superio_select(sio_data->sioreg, NCT6775_LD_VID);
3787 data->vid = superio_inb(sio_data->sioreg, 0xe3);
3788 data->vrm = vid_which_vrm();
3794 superio_select(sio_data->sioreg, NCT6775_LD_HWM);
3795 tmp = superio_inb(sio_data->sioreg,
3796 NCT6775_REG_CR_FAN_DEBOUNCE);
3797 switch (data->kind) {
3806 superio_outb(sio_data->sioreg, NCT6775_REG_CR_FAN_DEBOUNCE,
3808 dev_info(&pdev->dev, "Enabled fan debounce for chip %s\n",
3812 superio_exit(sio_data->sioreg);
3815 err = device_create_file(dev, &dev_attr_cpu0_vid);
3820 err = nct6775_check_fan_inputs(sio_data, data);
3824 /* Read fan clock dividers immediately */
3825 nct6775_init_fan_common(dev, data);
3827 /* Register sysfs hooks */
3828 for (i = 0; i < data->pwm_num; i++) {
3829 if (!(data->has_pwm & (1 << i)))
3832 err = sysfs_create_group(&dev->kobj, &nct6775_group_pwm[i]);
3836 if (data->REG_PWM[3]) {
3837 err = device_create_file(dev,
3838 &sda_pwm_max[i].dev_attr);
3842 if (data->REG_PWM[4]) {
3843 err = device_create_file(dev,
3844 &sda_pwm_step[i].dev_attr);
3848 if (data->REG_PWM[6]) {
3849 err = device_create_file(dev,
3850 &sda_weight_duty_base[i].dev_attr);
3855 for (i = 0; i < ARRAY_SIZE(sda_auto_pwm_arrays); i++) {
3856 struct sensor_device_attribute_2 *attr =
3857 &sda_auto_pwm_arrays[i];
3859 if (!(data->has_pwm & (1 << attr->nr)))
3861 if (attr->index > data->auto_pwm_num)
3863 err = device_create_file(dev, &attr->dev_attr);
3868 for (i = 0; i < data->in_num; i++) {
3869 if (!(data->have_in & (1 << i)))
3871 err = sysfs_create_group(&dev->kobj, &nct6775_group_in[i]);
3876 for (i = 0; i < 5; i++) {
3877 if (data->has_fan & (1 << i)) {
3878 err = device_create_file(dev,
3879 &sda_fan_input[i].dev_attr);
3882 err = device_create_file(dev,
3883 &sda_fan_alarm[i].dev_attr);
3886 if (data->kind != nct6776 &&
3887 data->kind != nct6779) {
3888 err = device_create_file(dev,
3889 &sda_fan_div[i].dev_attr);
3893 if (data->has_fan_min & (1 << i)) {
3894 err = device_create_file(dev,
3895 &sda_fan_min[i].dev_attr);
3899 err = device_create_file(dev,
3900 &sda_fan_pulses[i].dev_attr);
3906 for (i = 0; i < NUM_TEMP; i++) {
3907 if (!(data->have_temp & (1 << i)))
3909 err = device_create_file(dev, &sda_temp_input[i].dev_attr);
3912 if (data->temp_label) {
3913 err = device_create_file(dev,
3914 &sda_temp_label[i].dev_attr);
3918 if (data->reg_temp[1][i]) {
3919 err = device_create_file(dev,
3920 &sda_temp_max[i].dev_attr);
3924 if (data->reg_temp[2][i]) {
3925 err = device_create_file(dev,
3926 &sda_temp_max_hyst[i].dev_attr);
3930 if (data->reg_temp[3][i]) {
3931 err = device_create_file(dev,
3932 &sda_temp_crit[i].dev_attr);
3936 if (!(data->have_temp_fixed & (1 << i)))
3938 err = device_create_file(dev, &sda_temp_type[i].dev_attr);
3941 err = device_create_file(dev, &sda_temp_offset[i].dev_attr);
3944 if (i >= NUM_TEMP_ALARM ||
3945 data->ALARM_BITS[TEMP_ALARM_BASE + i] < 0)
3947 err = device_create_file(dev, &sda_temp_alarm[i].dev_attr);
3952 for (i = 0; i < ARRAY_SIZE(sda_caseopen); i++) {
3953 if (data->ALARM_BITS[INTRUSION_ALARM_BASE + i] < 0)
3955 err = device_create_file(dev, &sda_caseopen[i].dev_attr);
3960 err = device_create_file(dev, &dev_attr_name);
3964 data->hwmon_dev = hwmon_device_register(dev);
3965 if (IS_ERR(data->hwmon_dev)) {
3966 err = PTR_ERR(data->hwmon_dev);
3973 nct6775_device_remove_files(dev);
3977 static int nct6775_remove(struct platform_device *pdev)
3979 struct nct6775_data *data = platform_get_drvdata(pdev);
3981 hwmon_device_unregister(data->hwmon_dev);
3982 nct6775_device_remove_files(&pdev->dev);
3988 static int nct6775_suspend(struct device *dev)
3990 struct nct6775_data *data = nct6775_update_device(dev);
3991 struct nct6775_sio_data *sio_data = dev->platform_data;
3993 mutex_lock(&data->update_lock);
3994 data->vbat = nct6775_read_value(data, data->REG_VBAT);
3995 if (sio_data->kind == nct6775) {
3996 data->fandiv1 = nct6775_read_value(data, NCT6775_REG_FANDIV1);
3997 data->fandiv2 = nct6775_read_value(data, NCT6775_REG_FANDIV2);
3999 mutex_unlock(&data->update_lock);
4004 static int nct6775_resume(struct device *dev)
4006 struct nct6775_data *data = dev_get_drvdata(dev);
4007 struct nct6775_sio_data *sio_data = dev->platform_data;
4010 mutex_lock(&data->update_lock);
4011 data->bank = 0xff; /* Force initial bank selection */
4013 /* Restore limits */
4014 for (i = 0; i < data->in_num; i++) {
4015 if (!(data->have_in & (1 << i)))
4018 nct6775_write_value(data, data->REG_IN_MINMAX[0][i],
4020 nct6775_write_value(data, data->REG_IN_MINMAX[1][i],
4024 for (i = 0; i < 5; i++) {
4025 if (!(data->has_fan_min & (1 << i)))
4028 nct6775_write_value(data, data->REG_FAN_MIN[i],
4032 for (i = 0; i < NUM_TEMP; i++) {
4033 if (!(data->have_temp & (1 << i)))
4036 for (j = 1; j < 4; j++)
4037 if (data->reg_temp[j][i])
4038 nct6775_write_temp(data, data->reg_temp[j][i],
4042 /* Restore other settings */
4043 nct6775_write_value(data, data->REG_VBAT, data->vbat);
4044 if (sio_data->kind == nct6775) {
4045 nct6775_write_value(data, NCT6775_REG_FANDIV1, data->fandiv1);
4046 nct6775_write_value(data, NCT6775_REG_FANDIV2, data->fandiv2);
4049 /* Force re-reading all values */
4050 data->valid = false;
4051 mutex_unlock(&data->update_lock);
4056 static const struct dev_pm_ops nct6775_dev_pm_ops = {
4057 .suspend = nct6775_suspend,
4058 .resume = nct6775_resume,
4061 #define NCT6775_DEV_PM_OPS (&nct6775_dev_pm_ops)
4063 #define NCT6775_DEV_PM_OPS NULL
4064 #endif /* CONFIG_PM */
4066 static struct platform_driver nct6775_driver = {
4068 .owner = THIS_MODULE,
4070 .pm = NCT6775_DEV_PM_OPS,
4072 .probe = nct6775_probe,
4073 .remove = nct6775_remove,
4076 static const char *nct6775_sio_names[] __initconst = {
4082 /* nct6775_find() looks for a '627 in the Super-I/O config space */
4083 static int __init nct6775_find(int sioaddr, unsigned short *addr,
4084 struct nct6775_sio_data *sio_data)
4089 err = superio_enter(sioaddr);
4096 val = (superio_inb(sioaddr, SIO_REG_DEVID) << 8)
4097 | superio_inb(sioaddr, SIO_REG_DEVID + 1);
4098 switch (val & SIO_ID_MASK) {
4099 case SIO_NCT6775_ID:
4100 sio_data->kind = nct6775;
4102 case SIO_NCT6776_ID:
4103 sio_data->kind = nct6776;
4105 case SIO_NCT6779_ID:
4106 sio_data->kind = nct6779;
4110 pr_debug("unsupported chip ID: 0x%04x\n", val);
4111 superio_exit(sioaddr);
4115 /* We have a known chip, find the HWM I/O address */
4116 superio_select(sioaddr, NCT6775_LD_HWM);
4117 val = (superio_inb(sioaddr, SIO_REG_ADDR) << 8)
4118 | superio_inb(sioaddr, SIO_REG_ADDR + 1);
4119 *addr = val & IOREGION_ALIGNMENT;
4121 pr_err("Refusing to enable a Super-I/O device with a base I/O port 0\n");
4122 superio_exit(sioaddr);
4126 /* Activate logical device if needed */
4127 val = superio_inb(sioaddr, SIO_REG_ENABLE);
4128 if (!(val & 0x01)) {
4129 pr_warn("Forcibly enabling Super-I/O. Sensor is probably unusable.\n");
4130 superio_outb(sioaddr, SIO_REG_ENABLE, val | 0x01);
4133 superio_exit(sioaddr);
4134 pr_info("Found %s or compatible chip at %#x\n",
4135 nct6775_sio_names[sio_data->kind], *addr);
4136 sio_data->sioreg = sioaddr;
4142 * when Super-I/O functions move to a separate file, the Super-I/O
4143 * bus will manage the lifetime of the device and this module will only keep
4144 * track of the nct6775 driver. But since we platform_device_alloc(), we
4145 * must keep track of the device
4147 static struct platform_device *pdev;
4149 static int __init sensors_nct6775_init(void)
4152 unsigned short address;
4153 struct resource res;
4154 struct nct6775_sio_data sio_data;
4157 * initialize sio_data->kind and sio_data->sioreg.
4159 * when Super-I/O functions move to a separate file, the Super-I/O
4160 * driver will probe 0x2e and 0x4e and auto-detect the presence of a
4161 * nct6775 hardware monitor, and call probe()
4163 if (nct6775_find(0x2e, &address, &sio_data) &&
4164 nct6775_find(0x4e, &address, &sio_data))
4167 err = platform_driver_register(&nct6775_driver);
4171 pdev = platform_device_alloc(DRVNAME, address);
4174 pr_err("Device allocation failed\n");
4175 goto exit_unregister;
4178 err = platform_device_add_data(pdev, &sio_data,
4179 sizeof(struct nct6775_sio_data));
4181 pr_err("Platform data allocation failed\n");
4182 goto exit_device_put;
4185 memset(&res, 0, sizeof(res));
4187 res.start = address + IOREGION_OFFSET;
4188 res.end = address + IOREGION_OFFSET + IOREGION_LENGTH - 1;
4189 res.flags = IORESOURCE_IO;
4191 err = acpi_check_resource_conflict(&res);
4193 goto exit_device_put;
4195 err = platform_device_add_resources(pdev, &res, 1);
4197 pr_err("Device resource addition failed (%d)\n", err);
4198 goto exit_device_put;
4201 /* platform_device_add calls probe() */
4202 err = platform_device_add(pdev);
4204 pr_err("Device addition failed (%d)\n", err);
4205 goto exit_device_put;
4211 platform_device_put(pdev);
4213 platform_driver_unregister(&nct6775_driver);
4218 static void __exit sensors_nct6775_exit(void)
4220 platform_device_unregister(pdev);
4221 platform_driver_unregister(&nct6775_driver);
4224 MODULE_AUTHOR("Guenter Roeck <linux@roeck-us.net>");
4225 MODULE_DESCRIPTION("NCT6775F/NCT6776F/NCT6779D driver");
4226 MODULE_LICENSE("GPL");
4228 module_init(sensors_nct6775_init);
4229 module_exit(sensors_nct6775_exit);