1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * nct6775 - Driver for the hardware monitoring functionality of
4 * Nuvoton NCT677x Super-I/O chips
6 * Copyright (C) 2012 Guenter Roeck <linux@roeck-us.net>
8 * Derived from w83627ehf driver
9 * Copyright (C) 2005-2012 Jean Delvare <jdelvare@suse.de>
10 * Copyright (C) 2006 Yuan Mu (Winbond),
11 * Rudolf Marek <r.marek@assembler.cz>
12 * David Hubbard <david.c.hubbard@gmail.com>
13 * Daniel J Blueman <daniel.blueman@gmail.com>
14 * Copyright (C) 2010 Sheng-Yuan Huang (Nuvoton) (PS00)
16 * Shamelessly ripped from the w83627hf driver
17 * Copyright (C) 2003 Mark Studebaker
19 * Supports the following chips:
21 * Chip #vin #fan #pwm #temp chip IDs man ID
22 * nct6106d 9 3 3 6+3 0xc450 0xc1 0x5ca3
23 * nct6116d 9 5 5 3+3 0xd280 0xc1 0x5ca3
24 * nct6775f 9 4 3 6+3 0xb470 0xc1 0x5ca3
25 * nct6776f 9 5 3 6+3 0xc330 0xc1 0x5ca3
26 * nct6779d 15 5 5 2+6 0xc560 0xc1 0x5ca3
27 * nct6791d 15 6 6 2+6 0xc800 0xc1 0x5ca3
28 * nct6792d 15 6 6 2+6 0xc910 0xc1 0x5ca3
29 * nct6793d 15 6 6 2+6 0xd120 0xc1 0x5ca3
30 * nct6795d 14 6 6 2+6 0xd350 0xc1 0x5ca3
31 * nct6796d 14 7 7 2+6 0xd420 0xc1 0x5ca3
32 * nct6797d 14 7 7 2+6 0xd450 0xc1 0x5ca3
34 * nct6798d 14 7 7 2+6 0xd428 0xc1 0x5ca3
36 * nct6799d 14 7 7 2+6 0xd802 0xc1 0x5ca3
38 * #temp lists the number of monitored temperature sources (first value) plus
39 * the number of directly connectable temperature sensors (second value).
42 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
44 #include <linux/module.h>
45 #include <linux/init.h>
46 #include <linux/slab.h>
47 #include <linux/jiffies.h>
48 #include <linux/hwmon.h>
49 #include <linux/hwmon-sysfs.h>
50 #include <linux/err.h>
51 #include <linux/mutex.h>
52 #include <linux/bitops.h>
53 #include <linux/nospec.h>
54 #include <linux/regmap.h>
58 #undef DEFAULT_SYMBOL_NAMESPACE
59 #define DEFAULT_SYMBOL_NAMESPACE HWMON_NCT6775
63 /* used to set data->name = nct6775_device_names[data->sio_kind] */
64 static const char * const nct6775_device_names[] = {
80 /* Common and NCT6775 specific data */
82 /* Voltage min/max registers for nr=7..14 are in bank 5 */
84 static const u16 NCT6775_REG_IN_MAX[] = {
85 0x2b, 0x2d, 0x2f, 0x31, 0x33, 0x35, 0x37, 0x554, 0x556, 0x558, 0x55a,
86 0x55c, 0x55e, 0x560, 0x562 };
87 static const u16 NCT6775_REG_IN_MIN[] = {
88 0x2c, 0x2e, 0x30, 0x32, 0x34, 0x36, 0x38, 0x555, 0x557, 0x559, 0x55b,
89 0x55d, 0x55f, 0x561, 0x563 };
90 static const u16 NCT6775_REG_IN[] = {
91 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x550, 0x551, 0x552
94 #define NCT6775_REG_VBAT 0x5D
95 #define NCT6775_REG_DIODE 0x5E
96 #define NCT6775_DIODE_MASK 0x02
98 static const u16 NCT6775_REG_ALARM[NUM_REG_ALARM] = { 0x459, 0x45A, 0x45B };
100 /* 0..15 voltages, 16..23 fans, 24..29 temperatures, 30..31 intrusion */
102 static const s8 NCT6775_ALARM_BITS[] = {
103 0, 1, 2, 3, 8, 21, 20, 16, /* in0.. in7 */
104 17, -1, -1, -1, -1, -1, -1, /* in8..in14 */
106 6, 7, 11, -1, -1, /* fan1..fan5 */
107 -1, -1, -1, /* unused */
108 4, 5, 13, -1, -1, -1, /* temp1..temp6 */
109 12, -1 }; /* intrusion0, intrusion1 */
111 static const u16 NCT6775_REG_BEEP[NUM_REG_BEEP] = { 0x56, 0x57, 0x453, 0x4e };
114 * 0..14 voltages, 15 global beep enable, 16..23 fans, 24..29 temperatures,
117 static const s8 NCT6775_BEEP_BITS[] = {
118 0, 1, 2, 3, 8, 9, 10, 16, /* in0.. in7 */
119 17, -1, -1, -1, -1, -1, -1, /* in8..in14 */
120 21, /* global beep enable */
121 6, 7, 11, 28, -1, /* fan1..fan5 */
122 -1, -1, -1, /* unused */
123 4, 5, 13, -1, -1, -1, /* temp1..temp6 */
124 12, -1 }; /* intrusion0, intrusion1 */
126 /* DC or PWM output fan configuration */
127 static const u8 NCT6775_REG_PWM_MODE[] = { 0x04, 0x04, 0x12 };
128 static const u8 NCT6775_PWM_MODE_MASK[] = { 0x01, 0x02, 0x01 };
130 /* Advanced Fan control, some values are common for all fans */
132 static const u16 NCT6775_REG_TARGET[] = {
133 0x101, 0x201, 0x301, 0x801, 0x901, 0xa01, 0xb01 };
134 static const u16 NCT6775_REG_FAN_MODE[] = {
135 0x102, 0x202, 0x302, 0x802, 0x902, 0xa02, 0xb02 };
136 static const u16 NCT6775_REG_FAN_STEP_DOWN_TIME[] = {
137 0x103, 0x203, 0x303, 0x803, 0x903, 0xa03, 0xb03 };
138 static const u16 NCT6775_REG_FAN_STEP_UP_TIME[] = {
139 0x104, 0x204, 0x304, 0x804, 0x904, 0xa04, 0xb04 };
140 static const u16 NCT6775_REG_FAN_STOP_OUTPUT[] = {
141 0x105, 0x205, 0x305, 0x805, 0x905, 0xa05, 0xb05 };
142 static const u16 NCT6775_REG_FAN_START_OUTPUT[] = {
143 0x106, 0x206, 0x306, 0x806, 0x906, 0xa06, 0xb06 };
144 static const u16 NCT6775_REG_FAN_MAX_OUTPUT[] = { 0x10a, 0x20a, 0x30a };
145 static const u16 NCT6775_REG_FAN_STEP_OUTPUT[] = { 0x10b, 0x20b, 0x30b };
147 static const u16 NCT6775_REG_FAN_STOP_TIME[] = {
148 0x107, 0x207, 0x307, 0x807, 0x907, 0xa07, 0xb07 };
149 static const u16 NCT6775_REG_PWM[] = {
150 0x109, 0x209, 0x309, 0x809, 0x909, 0xa09, 0xb09 };
151 static const u16 NCT6775_REG_PWM_READ[] = {
152 0x01, 0x03, 0x11, 0x13, 0x15, 0xa09, 0xb09 };
154 static const u16 NCT6775_REG_FAN[] = { 0x630, 0x632, 0x634, 0x636, 0x638 };
155 static const u16 NCT6775_REG_FAN_MIN[] = { 0x3b, 0x3c, 0x3d };
156 static const u16 NCT6775_REG_FAN_PULSES[NUM_FAN] = {
157 0x641, 0x642, 0x643, 0x644 };
158 static const u16 NCT6775_FAN_PULSE_SHIFT[NUM_FAN] = { };
160 static const u16 NCT6775_REG_TEMP[] = {
161 0x27, 0x150, 0x250, 0x62b, 0x62c, 0x62d };
163 static const u16 NCT6775_REG_TEMP_MON[] = { 0x73, 0x75, 0x77 };
165 static const u16 NCT6775_REG_TEMP_CONFIG[ARRAY_SIZE(NCT6775_REG_TEMP)] = {
166 0, 0x152, 0x252, 0x628, 0x629, 0x62A };
167 static const u16 NCT6775_REG_TEMP_HYST[ARRAY_SIZE(NCT6775_REG_TEMP)] = {
168 0x3a, 0x153, 0x253, 0x673, 0x678, 0x67D };
169 static const u16 NCT6775_REG_TEMP_OVER[ARRAY_SIZE(NCT6775_REG_TEMP)] = {
170 0x39, 0x155, 0x255, 0x672, 0x677, 0x67C };
172 static const u16 NCT6775_REG_TEMP_SOURCE[ARRAY_SIZE(NCT6775_REG_TEMP)] = {
173 0x621, 0x622, 0x623, 0x624, 0x625, 0x626 };
175 static const u16 NCT6775_REG_TEMP_SEL[] = {
176 0x100, 0x200, 0x300, 0x800, 0x900, 0xa00, 0xb00 };
178 static const u16 NCT6775_REG_WEIGHT_TEMP_SEL[] = {
179 0x139, 0x239, 0x339, 0x839, 0x939, 0xa39 };
180 static const u16 NCT6775_REG_WEIGHT_TEMP_STEP[] = {
181 0x13a, 0x23a, 0x33a, 0x83a, 0x93a, 0xa3a };
182 static const u16 NCT6775_REG_WEIGHT_TEMP_STEP_TOL[] = {
183 0x13b, 0x23b, 0x33b, 0x83b, 0x93b, 0xa3b };
184 static const u16 NCT6775_REG_WEIGHT_DUTY_STEP[] = {
185 0x13c, 0x23c, 0x33c, 0x83c, 0x93c, 0xa3c };
186 static const u16 NCT6775_REG_WEIGHT_TEMP_BASE[] = {
187 0x13d, 0x23d, 0x33d, 0x83d, 0x93d, 0xa3d };
189 static const u16 NCT6775_REG_TEMP_OFFSET[] = { 0x454, 0x455, 0x456 };
191 static const u16 NCT6775_REG_AUTO_TEMP[] = {
192 0x121, 0x221, 0x321, 0x821, 0x921, 0xa21, 0xb21 };
193 static const u16 NCT6775_REG_AUTO_PWM[] = {
194 0x127, 0x227, 0x327, 0x827, 0x927, 0xa27, 0xb27 };
196 #define NCT6775_AUTO_TEMP(data, nr, p) ((data)->REG_AUTO_TEMP[nr] + (p))
197 #define NCT6775_AUTO_PWM(data, nr, p) ((data)->REG_AUTO_PWM[nr] + (p))
199 static const u16 NCT6775_REG_CRITICAL_ENAB[] = { 0x134, 0x234, 0x334 };
201 static const u16 NCT6775_REG_CRITICAL_TEMP[] = {
202 0x135, 0x235, 0x335, 0x835, 0x935, 0xa35, 0xb35 };
203 static const u16 NCT6775_REG_CRITICAL_TEMP_TOLERANCE[] = {
204 0x138, 0x238, 0x338, 0x838, 0x938, 0xa38, 0xb38 };
206 static const char *const nct6775_temp_label[] = {
220 "PCH_CHIP_CPU_MAX_TEMP",
230 #define NCT6775_TEMP_MASK 0x001ffffe
231 #define NCT6775_VIRT_TEMP_MASK 0x00000000
233 static const u16 NCT6775_REG_TEMP_ALTERNATE[32] = {
239 static const u16 NCT6775_REG_TEMP_CRIT[32] = {
250 static const u16 NCT6775_REG_TSI_TEMP[] = { 0x669 };
252 /* NCT6776 specific data */
254 /* STEP_UP_TIME and STEP_DOWN_TIME regs are swapped for all chips but NCT6775 */
255 #define NCT6776_REG_FAN_STEP_UP_TIME NCT6775_REG_FAN_STEP_DOWN_TIME
256 #define NCT6776_REG_FAN_STEP_DOWN_TIME NCT6775_REG_FAN_STEP_UP_TIME
258 static const s8 NCT6776_ALARM_BITS[] = {
259 0, 1, 2, 3, 8, 21, 20, 16, /* in0.. in7 */
260 17, -1, -1, -1, -1, -1, -1, /* in8..in14 */
262 6, 7, 11, 10, 23, /* fan1..fan5 */
263 -1, -1, -1, /* unused */
264 4, 5, 13, -1, -1, -1, /* temp1..temp6 */
265 12, 9 }; /* intrusion0, intrusion1 */
267 static const u16 NCT6776_REG_BEEP[NUM_REG_BEEP] = { 0xb2, 0xb3, 0xb4, 0xb5 };
269 static const s8 NCT6776_BEEP_BITS[] = {
270 0, 1, 2, 3, 4, 5, 6, 7, /* in0.. in7 */
271 8, -1, -1, -1, -1, -1, -1, /* in8..in14 */
272 24, /* global beep enable */
273 25, 26, 27, 28, 29, /* fan1..fan5 */
274 -1, -1, -1, /* unused */
275 16, 17, 18, 19, 20, 21, /* temp1..temp6 */
276 30, 31 }; /* intrusion0, intrusion1 */
278 static const u16 NCT6776_REG_TOLERANCE_H[] = {
279 0x10c, 0x20c, 0x30c, 0x80c, 0x90c, 0xa0c, 0xb0c };
281 static const u8 NCT6776_REG_PWM_MODE[] = { 0x04, 0, 0, 0, 0, 0 };
282 static const u8 NCT6776_PWM_MODE_MASK[] = { 0x01, 0, 0, 0, 0, 0 };
284 static const u16 NCT6776_REG_FAN_MIN[] = {
285 0x63a, 0x63c, 0x63e, 0x640, 0x642, 0x64a, 0x64c };
286 static const u16 NCT6776_REG_FAN_PULSES[NUM_FAN] = {
287 0x644, 0x645, 0x646, 0x647, 0x648, 0x649 };
289 static const u16 NCT6776_REG_WEIGHT_DUTY_BASE[] = {
290 0x13e, 0x23e, 0x33e, 0x83e, 0x93e, 0xa3e };
292 static const u16 NCT6776_REG_TEMP_CONFIG[ARRAY_SIZE(NCT6775_REG_TEMP)] = {
293 0x18, 0x152, 0x252, 0x628, 0x629, 0x62A };
295 static const char *const nct6776_temp_label[] = {
310 "PCH_CHIP_CPU_MAX_TEMP",
321 #define NCT6776_TEMP_MASK 0x007ffffe
322 #define NCT6776_VIRT_TEMP_MASK 0x00000000
324 static const u16 NCT6776_REG_TEMP_ALTERNATE[32] = {
330 static const u16 NCT6776_REG_TEMP_CRIT[32] = {
335 static const u16 NCT6776_REG_TSI_TEMP[] = {
336 0x409, 0x40b, 0x40d, 0x40f, 0x411, 0x413, 0x415, 0x417 };
338 /* NCT6779 specific data */
340 static const u16 NCT6779_REG_IN[] = {
341 0x480, 0x481, 0x482, 0x483, 0x484, 0x485, 0x486, 0x487,
342 0x488, 0x489, 0x48a, 0x48b, 0x48c, 0x48d, 0x48e };
344 static const u16 NCT6779_REG_ALARM[NUM_REG_ALARM] = {
345 0x459, 0x45A, 0x45B, 0x568 };
347 static const s8 NCT6779_ALARM_BITS[] = {
348 0, 1, 2, 3, 8, 21, 20, 16, /* in0.. in7 */
349 17, 24, 25, 26, 27, 28, 29, /* in8..in14 */
351 6, 7, 11, 10, 23, /* fan1..fan5 */
352 -1, -1, -1, /* unused */
353 4, 5, 13, -1, -1, -1, /* temp1..temp6 */
354 12, 9 }; /* intrusion0, intrusion1 */
356 static const s8 NCT6779_BEEP_BITS[] = {
357 0, 1, 2, 3, 4, 5, 6, 7, /* in0.. in7 */
358 8, 9, 10, 11, 12, 13, 14, /* in8..in14 */
359 24, /* global beep enable */
360 25, 26, 27, 28, 29, /* fan1..fan5 */
361 -1, -1, -1, /* unused */
362 16, 17, -1, -1, -1, -1, /* temp1..temp6 */
363 30, 31 }; /* intrusion0, intrusion1 */
365 static const u16 NCT6779_REG_FAN[] = {
366 0x4c0, 0x4c2, 0x4c4, 0x4c6, 0x4c8, 0x4ca, 0x4ce };
367 static const u16 NCT6779_REG_FAN_PULSES[NUM_FAN] = {
368 0x644, 0x645, 0x646, 0x647, 0x648, 0x649, 0x64f };
370 static const u16 NCT6779_REG_CRITICAL_PWM_ENABLE[] = {
371 0x136, 0x236, 0x336, 0x836, 0x936, 0xa36, 0xb36 };
372 #define NCT6779_CRITICAL_PWM_ENABLE_MASK 0x01
373 static const u16 NCT6779_REG_CRITICAL_PWM[] = {
374 0x137, 0x237, 0x337, 0x837, 0x937, 0xa37, 0xb37 };
376 static const u16 NCT6779_REG_TEMP[] = { 0x27, 0x150 };
377 static const u16 NCT6779_REG_TEMP_MON[] = { 0x73, 0x75, 0x77, 0x79, 0x7b };
378 static const u16 NCT6779_REG_TEMP_CONFIG[ARRAY_SIZE(NCT6779_REG_TEMP)] = {
380 static const u16 NCT6779_REG_TEMP_HYST[ARRAY_SIZE(NCT6779_REG_TEMP)] = {
382 static const u16 NCT6779_REG_TEMP_OVER[ARRAY_SIZE(NCT6779_REG_TEMP)] = {
385 static const u16 NCT6779_REG_TEMP_OFFSET[] = {
386 0x454, 0x455, 0x456, 0x44a, 0x44b, 0x44c, 0x44d, 0x449 };
388 static const char *const nct6779_temp_label[] = {
407 "PCH_CHIP_CPU_MAX_TEMP",
423 #define NCT6779_TEMP_MASK 0x07ffff7e
424 #define NCT6779_VIRT_TEMP_MASK 0x00000000
425 #define NCT6791_TEMP_MASK 0x87ffff7e
426 #define NCT6791_VIRT_TEMP_MASK 0x80000000
428 static const u16 NCT6779_REG_TEMP_ALTERNATE[32]
429 = { 0x490, 0x491, 0x492, 0x493, 0x494, 0x495, 0, 0,
430 0, 0, 0, 0, 0, 0, 0, 0,
431 0, 0x400, 0x401, 0x402, 0x404, 0x405, 0x406, 0x407,
434 static const u16 NCT6779_REG_TEMP_CRIT[32] = {
439 /* NCT6791 specific data */
441 static const u16 NCT6791_REG_WEIGHT_TEMP_SEL[NUM_FAN] = { 0, 0x239 };
442 static const u16 NCT6791_REG_WEIGHT_TEMP_STEP[NUM_FAN] = { 0, 0x23a };
443 static const u16 NCT6791_REG_WEIGHT_TEMP_STEP_TOL[NUM_FAN] = { 0, 0x23b };
444 static const u16 NCT6791_REG_WEIGHT_DUTY_STEP[NUM_FAN] = { 0, 0x23c };
445 static const u16 NCT6791_REG_WEIGHT_TEMP_BASE[NUM_FAN] = { 0, 0x23d };
446 static const u16 NCT6791_REG_WEIGHT_DUTY_BASE[NUM_FAN] = { 0, 0x23e };
448 static const u16 NCT6791_REG_ALARM[NUM_REG_ALARM] = {
449 0x459, 0x45A, 0x45B, 0x568, 0x45D };
451 static const s8 NCT6791_ALARM_BITS[] = {
452 0, 1, 2, 3, 8, 21, 20, 16, /* in0.. in7 */
453 17, 24, 25, 26, 27, 28, 29, /* in8..in14 */
455 6, 7, 11, 10, 23, 33, /* fan1..fan6 */
457 4, 5, 13, -1, -1, -1, /* temp1..temp6 */
458 12, 9 }; /* intrusion0, intrusion1 */
460 /* NCT6792/NCT6793 specific data */
462 static const u16 NCT6792_REG_TEMP_MON[] = {
463 0x73, 0x75, 0x77, 0x79, 0x7b, 0x7d };
464 static const u16 NCT6792_REG_BEEP[NUM_REG_BEEP] = {
465 0xb2, 0xb3, 0xb4, 0xb5, 0xbf };
467 static const char *const nct6792_temp_label[] = {
486 "PCH_CHIP_CPU_MAX_TEMP",
495 "PECI Agent 0 Calibration",
496 "PECI Agent 1 Calibration",
502 #define NCT6792_TEMP_MASK 0x9fffff7e
503 #define NCT6792_VIRT_TEMP_MASK 0x80000000
505 static const char *const nct6793_temp_label[] = {
524 "PCH_CHIP_CPU_MAX_TEMP",
534 "PECI Agent 0 Calibration",
535 "PECI Agent 1 Calibration",
540 #define NCT6793_TEMP_MASK 0xbfff037e
541 #define NCT6793_VIRT_TEMP_MASK 0x80000000
543 static const char *const nct6795_temp_label[] = {
562 "PCH_CHIP_CPU_MAX_TEMP",
572 "PECI Agent 0 Calibration",
573 "PECI Agent 1 Calibration",
578 #define NCT6795_TEMP_MASK 0xbfffff7e
579 #define NCT6795_VIRT_TEMP_MASK 0x80000000
581 static const char *const nct6796_temp_label[] = {
600 "PCH_CHIP_CPU_MAX_TEMP",
610 "PECI Agent 0 Calibration",
611 "PECI Agent 1 Calibration",
616 #define NCT6796_TEMP_MASK 0xbfff0ffe
617 #define NCT6796_VIRT_TEMP_MASK 0x80000c00
619 static const u16 NCT6796_REG_TSI_TEMP[] = { 0x409, 0x40b };
621 static const char *const nct6798_temp_label[] = {
640 "PCH_CHIP_CPU_MAX_TEMP",
650 "PECI Agent 0 Calibration", /* undocumented */
651 "PECI Agent 1 Calibration", /* undocumented */
656 #define NCT6798_TEMP_MASK 0xbfff0ffe
657 #define NCT6798_VIRT_TEMP_MASK 0x80000c00
659 static const char *const nct6799_temp_label[] = {
678 "PCH_CHIP_CPU_MAX_TEMP",
688 "PECI Agent 0 Calibration", /* undocumented */
689 "PECI Agent 1 Calibration", /* undocumented */
694 #define NCT6799_TEMP_MASK 0xbfff2ffe
695 #define NCT6799_VIRT_TEMP_MASK 0x80000c00
697 /* NCT6102D/NCT6106D specific data */
699 #define NCT6106_REG_VBAT 0x318
700 #define NCT6106_REG_DIODE 0x319
701 #define NCT6106_DIODE_MASK 0x01
703 static const u16 NCT6106_REG_IN_MAX[] = {
704 0x90, 0x92, 0x94, 0x96, 0x98, 0x9a, 0x9e, 0xa0, 0xa2 };
705 static const u16 NCT6106_REG_IN_MIN[] = {
706 0x91, 0x93, 0x95, 0x97, 0x99, 0x9b, 0x9f, 0xa1, 0xa3 };
707 static const u16 NCT6106_REG_IN[] = {
708 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x07, 0x08, 0x09 };
710 static const u16 NCT6106_REG_TEMP[] = { 0x10, 0x11, 0x12, 0x13, 0x14, 0x15 };
711 static const u16 NCT6106_REG_TEMP_MON[] = { 0x18, 0x19, 0x1a };
712 static const u16 NCT6106_REG_TEMP_HYST[] = {
713 0xc3, 0xc7, 0xcb, 0xcf, 0xd3, 0xd7 };
714 static const u16 NCT6106_REG_TEMP_OVER[] = {
715 0xc2, 0xc6, 0xca, 0xce, 0xd2, 0xd6 };
716 static const u16 NCT6106_REG_TEMP_CRIT_L[] = {
717 0xc0, 0xc4, 0xc8, 0xcc, 0xd0, 0xd4 };
718 static const u16 NCT6106_REG_TEMP_CRIT_H[] = {
719 0xc1, 0xc5, 0xc9, 0xcf, 0xd1, 0xd5 };
720 static const u16 NCT6106_REG_TEMP_OFFSET[] = { 0x311, 0x312, 0x313 };
721 static const u16 NCT6106_REG_TEMP_CONFIG[] = {
722 0xb7, 0xb8, 0xb9, 0xba, 0xbb, 0xbc };
724 static const u16 NCT6106_REG_FAN[] = { 0x20, 0x22, 0x24 };
725 static const u16 NCT6106_REG_FAN_MIN[] = { 0xe0, 0xe2, 0xe4 };
726 static const u16 NCT6106_REG_FAN_PULSES[] = { 0xf6, 0xf6, 0xf6 };
727 static const u16 NCT6106_FAN_PULSE_SHIFT[] = { 0, 2, 4 };
729 static const u8 NCT6106_REG_PWM_MODE[] = { 0xf3, 0xf3, 0xf3 };
730 static const u8 NCT6106_PWM_MODE_MASK[] = { 0x01, 0x02, 0x04 };
731 static const u16 NCT6106_REG_PWM_READ[] = { 0x4a, 0x4b, 0x4c };
732 static const u16 NCT6106_REG_FAN_MODE[] = { 0x113, 0x123, 0x133 };
733 static const u16 NCT6106_REG_TEMP_SOURCE[] = {
734 0xb0, 0xb1, 0xb2, 0xb3, 0xb4, 0xb5 };
736 static const u16 NCT6106_REG_CRITICAL_TEMP[] = { 0x11a, 0x12a, 0x13a };
737 static const u16 NCT6106_REG_CRITICAL_TEMP_TOLERANCE[] = {
738 0x11b, 0x12b, 0x13b };
740 static const u16 NCT6106_REG_CRITICAL_PWM_ENABLE[] = { 0x11c, 0x12c, 0x13c };
741 #define NCT6106_CRITICAL_PWM_ENABLE_MASK 0x10
742 static const u16 NCT6106_REG_CRITICAL_PWM[] = { 0x11d, 0x12d, 0x13d };
744 static const u16 NCT6106_REG_FAN_STEP_UP_TIME[] = { 0x114, 0x124, 0x134 };
745 static const u16 NCT6106_REG_FAN_STEP_DOWN_TIME[] = { 0x115, 0x125, 0x135 };
746 static const u16 NCT6106_REG_FAN_STOP_OUTPUT[] = { 0x116, 0x126, 0x136 };
747 static const u16 NCT6106_REG_FAN_START_OUTPUT[] = { 0x117, 0x127, 0x137 };
748 static const u16 NCT6106_REG_FAN_STOP_TIME[] = { 0x118, 0x128, 0x138 };
749 static const u16 NCT6106_REG_TOLERANCE_H[] = { 0x112, 0x122, 0x132 };
751 static const u16 NCT6106_REG_TARGET[] = { 0x111, 0x121, 0x131 };
753 static const u16 NCT6106_REG_WEIGHT_TEMP_SEL[] = { 0x168, 0x178, 0x188 };
754 static const u16 NCT6106_REG_WEIGHT_TEMP_STEP[] = { 0x169, 0x179, 0x189 };
755 static const u16 NCT6106_REG_WEIGHT_TEMP_STEP_TOL[] = { 0x16a, 0x17a, 0x18a };
756 static const u16 NCT6106_REG_WEIGHT_DUTY_STEP[] = { 0x16b, 0x17b, 0x18b };
757 static const u16 NCT6106_REG_WEIGHT_TEMP_BASE[] = { 0x16c, 0x17c, 0x18c };
758 static const u16 NCT6106_REG_WEIGHT_DUTY_BASE[] = { 0x16d, 0x17d, 0x18d };
760 static const u16 NCT6106_REG_AUTO_TEMP[] = { 0x160, 0x170, 0x180 };
761 static const u16 NCT6106_REG_AUTO_PWM[] = { 0x164, 0x174, 0x184 };
763 static const u16 NCT6106_REG_ALARM[NUM_REG_ALARM] = {
764 0x77, 0x78, 0x79, 0x7a, 0x7b, 0x7c, 0x7d };
766 static const s8 NCT6106_ALARM_BITS[] = {
767 0, 1, 2, 3, 4, 5, 7, 8, /* in0.. in7 */
768 9, -1, -1, -1, -1, -1, -1, /* in8..in14 */
770 32, 33, 34, -1, -1, /* fan1..fan5 */
771 -1, -1, -1, /* unused */
772 16, 17, 18, 19, 20, 21, /* temp1..temp6 */
773 48, -1 /* intrusion0, intrusion1 */
776 static const u16 NCT6106_REG_BEEP[NUM_REG_BEEP] = {
777 0x3c0, 0x3c1, 0x3c2, 0x3c3, 0x3c4 };
779 static const s8 NCT6106_BEEP_BITS[] = {
780 0, 1, 2, 3, 4, 5, 7, 8, /* in0.. in7 */
781 9, 10, 11, 12, -1, -1, -1, /* in8..in14 */
782 32, /* global beep enable */
783 24, 25, 26, 27, 28, /* fan1..fan5 */
784 -1, -1, -1, /* unused */
785 16, 17, 18, 19, 20, 21, /* temp1..temp6 */
786 34, -1 /* intrusion0, intrusion1 */
789 static const u16 NCT6106_REG_TEMP_ALTERNATE[32] = {
795 static const u16 NCT6106_REG_TEMP_CRIT[32] = {
800 static const u16 NCT6106_REG_TSI_TEMP[] = { 0x59, 0x5b, 0x5d, 0x5f, 0x61, 0x63, 0x65, 0x67 };
802 /* NCT6112D/NCT6114D/NCT6116D specific data */
804 static const u16 NCT6116_REG_FAN[] = { 0x20, 0x22, 0x24, 0x26, 0x28 };
805 static const u16 NCT6116_REG_FAN_MIN[] = { 0xe0, 0xe2, 0xe4, 0xe6, 0xe8 };
806 static const u16 NCT6116_REG_FAN_PULSES[] = { 0xf6, 0xf6, 0xf6, 0xf6, 0xf5 };
807 static const u16 NCT6116_FAN_PULSE_SHIFT[] = { 0, 2, 4, 6, 6 };
809 static const u16 NCT6116_REG_PWM[] = { 0x119, 0x129, 0x139, 0x199, 0x1a9 };
810 static const u16 NCT6116_REG_FAN_MODE[] = { 0x113, 0x123, 0x133, 0x193, 0x1a3 };
811 static const u16 NCT6116_REG_TEMP_SEL[] = { 0x110, 0x120, 0x130, 0x190, 0x1a0 };
812 static const u16 NCT6116_REG_TEMP_SOURCE[] = {
815 static const u16 NCT6116_REG_CRITICAL_TEMP[] = {
816 0x11a, 0x12a, 0x13a, 0x19a, 0x1aa };
817 static const u16 NCT6116_REG_CRITICAL_TEMP_TOLERANCE[] = {
818 0x11b, 0x12b, 0x13b, 0x19b, 0x1ab };
820 static const u16 NCT6116_REG_CRITICAL_PWM_ENABLE[] = {
821 0x11c, 0x12c, 0x13c, 0x19c, 0x1ac };
822 static const u16 NCT6116_REG_CRITICAL_PWM[] = {
823 0x11d, 0x12d, 0x13d, 0x19d, 0x1ad };
825 static const u16 NCT6116_REG_FAN_STEP_UP_TIME[] = {
826 0x114, 0x124, 0x134, 0x194, 0x1a4 };
827 static const u16 NCT6116_REG_FAN_STEP_DOWN_TIME[] = {
828 0x115, 0x125, 0x135, 0x195, 0x1a5 };
829 static const u16 NCT6116_REG_FAN_STOP_OUTPUT[] = {
830 0x116, 0x126, 0x136, 0x196, 0x1a6 };
831 static const u16 NCT6116_REG_FAN_START_OUTPUT[] = {
832 0x117, 0x127, 0x137, 0x197, 0x1a7 };
833 static const u16 NCT6116_REG_FAN_STOP_TIME[] = {
834 0x118, 0x128, 0x138, 0x198, 0x1a8 };
835 static const u16 NCT6116_REG_TOLERANCE_H[] = {
836 0x112, 0x122, 0x132, 0x192, 0x1a2 };
838 static const u16 NCT6116_REG_TARGET[] = {
839 0x111, 0x121, 0x131, 0x191, 0x1a1 };
841 static const u16 NCT6116_REG_AUTO_TEMP[] = {
842 0x160, 0x170, 0x180, 0x1d0, 0x1e0 };
843 static const u16 NCT6116_REG_AUTO_PWM[] = {
844 0x164, 0x174, 0x184, 0x1d4, 0x1e4 };
846 static const s8 NCT6116_ALARM_BITS[] = {
847 0, 1, 2, 3, 4, 5, 7, 8, /* in0.. in7 */
848 9, -1, -1, -1, -1, -1, -1, /* in8..in9 */
850 32, 33, 34, 35, 36, /* fan1..fan5 */
851 -1, -1, -1, /* unused */
852 16, 17, 18, -1, -1, -1, /* temp1..temp6 */
853 48, -1 /* intrusion0, intrusion1 */
856 static const s8 NCT6116_BEEP_BITS[] = {
857 0, 1, 2, 3, 4, 5, 7, 8, /* in0.. in7 */
858 9, 10, 11, 12, -1, -1, -1, /* in8..in14 */
859 32, /* global beep enable */
860 24, 25, 26, 27, 28, /* fan1..fan5 */
861 -1, -1, -1, /* unused */
862 16, 17, 18, -1, -1, -1, /* temp1..temp6 */
863 34, -1 /* intrusion0, intrusion1 */
866 static const u16 NCT6116_REG_TSI_TEMP[] = { 0x59, 0x5b };
868 static enum pwm_enable reg_to_pwm_enable(int pwm, int mode)
870 if (mode == 0 && pwm == 255)
875 static int pwm_enable_to_reg(enum pwm_enable mode)
886 /* 1 is DC mode, output in ms */
887 static unsigned int step_time_from_reg(u8 reg, u8 mode)
889 return mode ? 400 * reg : 100 * reg;
892 static u8 step_time_to_reg(unsigned int msec, u8 mode)
894 return clamp_val((mode ? (msec + 200) / 400 :
895 (msec + 50) / 100), 1, 255);
898 static unsigned int fan_from_reg8(u16 reg, unsigned int divreg)
900 if (reg == 0 || reg == 255)
902 return 1350000U / (reg << divreg);
905 static unsigned int fan_from_reg13(u16 reg, unsigned int divreg)
907 if ((reg & 0xff1f) == 0xff1f)
910 reg = (reg & 0x1f) | ((reg & 0xff00) >> 3);
915 return 1350000U / reg;
918 static unsigned int fan_from_reg16(u16 reg, unsigned int divreg)
920 if (reg == 0 || reg == 0xffff)
924 * Even though the registers are 16 bit wide, the fan divisor
927 return 1350000U / (reg << divreg);
930 static unsigned int fan_from_reg_rpm(u16 reg, unsigned int divreg)
935 static u16 fan_to_reg(u32 fan, unsigned int divreg)
940 return (1350000U / fan) >> divreg;
943 static inline unsigned int
950 * Some of the voltage inputs have internal scaling, the tables below
951 * contain 8 (the ADC LSB in mV) * scaling factor * 100
953 static const u16 scale_in[15] = {
954 800, 800, 1600, 1600, 800, 800, 800, 1600, 1600, 800, 800, 800, 800,
960 * CPUVC, IN1, AVSB, 3VCC, IN0, IN8, IN4, 3VSB, VBAT, VTT, IN5, IN6, IN2,
962 * Additional scales to be added later: IN9 (800), VHIF (1600)
964 static const u16 scale_in_6798[15] = {
965 800, 800, 1600, 1600, 800, 800, 800, 1600, 1600, 1600, 1600, 1600, 800,
969 static inline long in_from_reg(u8 reg, u8 nr, const u16 *scales)
971 return DIV_ROUND_CLOSEST(reg * scales[nr], 100);
974 static inline u8 in_to_reg(u32 val, u8 nr, const u16 *scales)
976 return clamp_val(DIV_ROUND_CLOSEST(val * 100, scales[nr]), 0, 255);
979 /* TSI temperatures are in 8.3 format */
980 static inline unsigned int tsi_temp_from_reg(unsigned int reg)
982 return (reg >> 5) * 125;
986 * Data structures and manipulation thereof
989 struct sensor_device_template {
990 struct device_attribute dev_attr;
998 bool s2; /* true if both index and nr are used */
1001 struct sensor_device_attr_u {
1003 struct sensor_device_attribute a1;
1004 struct sensor_device_attribute_2 a2;
1009 #define __TEMPLATE_ATTR(_template, _mode, _show, _store) { \
1010 .attr = {.name = _template, .mode = _mode }, \
1015 #define SENSOR_DEVICE_TEMPLATE(_template, _mode, _show, _store, _index) \
1016 { .dev_attr = __TEMPLATE_ATTR(_template, _mode, _show, _store), \
1017 .u.index = _index, \
1020 #define SENSOR_DEVICE_TEMPLATE_2(_template, _mode, _show, _store, \
1022 { .dev_attr = __TEMPLATE_ATTR(_template, _mode, _show, _store), \
1023 .u.s.index = _index, \
1027 #define SENSOR_TEMPLATE(_name, _template, _mode, _show, _store, _index) \
1028 static struct sensor_device_template sensor_dev_template_##_name \
1029 = SENSOR_DEVICE_TEMPLATE(_template, _mode, _show, _store, \
1032 #define SENSOR_TEMPLATE_2(_name, _template, _mode, _show, _store, \
1034 static struct sensor_device_template sensor_dev_template_##_name \
1035 = SENSOR_DEVICE_TEMPLATE_2(_template, _mode, _show, _store, \
1038 struct sensor_template_group {
1039 struct sensor_device_template **templates;
1040 umode_t (*is_visible)(struct kobject *, struct attribute *, int);
1044 static int nct6775_add_template_attr_group(struct device *dev, struct nct6775_data *data,
1045 const struct sensor_template_group *tg, int repeat)
1047 struct attribute_group *group;
1048 struct sensor_device_attr_u *su;
1049 struct sensor_device_attribute *a;
1050 struct sensor_device_attribute_2 *a2;
1051 struct attribute **attrs;
1052 struct sensor_device_template **t;
1059 for (count = 0; *t; t++, count++)
1065 group = devm_kzalloc(dev, sizeof(*group), GFP_KERNEL);
1069 attrs = devm_kcalloc(dev, repeat * count + 1, sizeof(*attrs),
1074 su = devm_kzalloc(dev, array3_size(repeat, count, sizeof(*su)),
1079 group->attrs = attrs;
1080 group->is_visible = tg->is_visible;
1082 for (i = 0; i < repeat; i++) {
1084 while (*t != NULL) {
1085 snprintf(su->name, sizeof(su->name),
1086 (*t)->dev_attr.attr.name, tg->base + i);
1089 sysfs_attr_init(&a2->dev_attr.attr);
1090 a2->dev_attr.attr.name = su->name;
1091 a2->nr = (*t)->u.s.nr + i;
1092 a2->index = (*t)->u.s.index;
1093 a2->dev_attr.attr.mode =
1094 (*t)->dev_attr.attr.mode;
1095 a2->dev_attr.show = (*t)->dev_attr.show;
1096 a2->dev_attr.store = (*t)->dev_attr.store;
1097 *attrs = &a2->dev_attr.attr;
1100 sysfs_attr_init(&a->dev_attr.attr);
1101 a->dev_attr.attr.name = su->name;
1102 a->index = (*t)->u.index + i;
1103 a->dev_attr.attr.mode =
1104 (*t)->dev_attr.attr.mode;
1105 a->dev_attr.show = (*t)->dev_attr.show;
1106 a->dev_attr.store = (*t)->dev_attr.store;
1107 *attrs = &a->dev_attr.attr;
1115 return nct6775_add_attr_group(data, group);
1118 bool nct6775_reg_is_word_sized(struct nct6775_data *data, u16 reg)
1120 switch (data->kind) {
1122 return reg == 0x20 || reg == 0x22 || reg == 0x24 ||
1123 (reg >= 0x59 && reg < 0x69 && (reg & 1)) ||
1124 reg == 0xe0 || reg == 0xe2 || reg == 0xe4 ||
1125 reg == 0x111 || reg == 0x121 || reg == 0x131;
1127 return reg == 0x20 || reg == 0x22 || reg == 0x24 ||
1128 reg == 0x26 || reg == 0x28 || reg == 0x59 || reg == 0x5b ||
1129 reg == 0xe0 || reg == 0xe2 || reg == 0xe4 || reg == 0xe6 ||
1130 reg == 0xe8 || reg == 0x111 || reg == 0x121 || reg == 0x131 ||
1131 reg == 0x191 || reg == 0x1a1;
1133 return (((reg & 0xff00) == 0x100 ||
1134 (reg & 0xff00) == 0x200) &&
1135 ((reg & 0x00ff) == 0x50 ||
1136 (reg & 0x00ff) == 0x53 ||
1137 (reg & 0x00ff) == 0x55)) ||
1138 (reg & 0xfff0) == 0x630 ||
1139 reg == 0x640 || reg == 0x642 ||
1140 reg == 0x662 || reg == 0x669 ||
1141 ((reg & 0xfff0) == 0x650 && (reg & 0x000f) >= 0x06) ||
1142 reg == 0x73 || reg == 0x75 || reg == 0x77;
1144 return (((reg & 0xff00) == 0x100 ||
1145 (reg & 0xff00) == 0x200) &&
1146 ((reg & 0x00ff) == 0x50 ||
1147 (reg & 0x00ff) == 0x53 ||
1148 (reg & 0x00ff) == 0x55)) ||
1149 (reg & 0xfff0) == 0x630 ||
1151 (reg >= 0x409 && reg < 0x419 && (reg & 1)) ||
1152 reg == 0x640 || reg == 0x642 ||
1153 ((reg & 0xfff0) == 0x650 && (reg & 0x000f) >= 0x06) ||
1154 reg == 0x73 || reg == 0x75 || reg == 0x77;
1164 return reg == 0x150 || reg == 0x153 || reg == 0x155 ||
1165 (reg & 0xfff0) == 0x4c0 ||
1167 (reg >= 0x409 && reg < 0x419 && (reg & 1)) ||
1168 reg == 0x63a || reg == 0x63c || reg == 0x63e ||
1169 reg == 0x640 || reg == 0x642 || reg == 0x64a ||
1171 reg == 0x73 || reg == 0x75 || reg == 0x77 || reg == 0x79 ||
1172 reg == 0x7b || reg == 0x7d;
1176 EXPORT_SYMBOL_GPL(nct6775_reg_is_word_sized);
1178 /* We left-align 8-bit temperature values to make the code simpler */
1179 static int nct6775_read_temp(struct nct6775_data *data, u16 reg, u16 *val)
1183 err = nct6775_read_value(data, reg, val);
1187 if (!nct6775_reg_is_word_sized(data, reg))
1193 /* This function assumes that the caller holds data->update_lock */
1194 static int nct6775_write_fan_div(struct nct6775_data *data, int nr)
1198 u16 fandiv_reg = nr < 2 ? NCT6775_REG_FANDIV1 : NCT6775_REG_FANDIV2;
1199 unsigned int oddshift = (nr & 1) * 4; /* masks shift by four if nr is odd */
1201 err = nct6775_read_value(data, fandiv_reg, ®);
1204 reg &= 0x70 >> oddshift;
1205 reg |= (data->fan_div[nr] & 0x7) << oddshift;
1206 return nct6775_write_value(data, fandiv_reg, reg);
1209 static int nct6775_write_fan_div_common(struct nct6775_data *data, int nr)
1211 if (data->kind == nct6775)
1212 return nct6775_write_fan_div(data, nr);
1216 static int nct6775_update_fan_div(struct nct6775_data *data)
1221 err = nct6775_read_value(data, NCT6775_REG_FANDIV1, &i);
1224 data->fan_div[0] = i & 0x7;
1225 data->fan_div[1] = (i & 0x70) >> 4;
1226 err = nct6775_read_value(data, NCT6775_REG_FANDIV2, &i);
1229 data->fan_div[2] = i & 0x7;
1230 if (data->has_fan & BIT(3))
1231 data->fan_div[3] = (i & 0x70) >> 4;
1236 static int nct6775_update_fan_div_common(struct nct6775_data *data)
1238 if (data->kind == nct6775)
1239 return nct6775_update_fan_div(data);
1243 static int nct6775_init_fan_div(struct nct6775_data *data)
1247 err = nct6775_update_fan_div_common(data);
1252 * For all fans, start with highest divider value if the divider
1253 * register is not initialized. This ensures that we get a
1254 * reading from the fan count register, even if it is not optimal.
1255 * We'll compute a better divider later on.
1257 for (i = 0; i < ARRAY_SIZE(data->fan_div); i++) {
1258 if (!(data->has_fan & BIT(i)))
1260 if (data->fan_div[i] == 0) {
1261 data->fan_div[i] = 7;
1262 err = nct6775_write_fan_div_common(data, i);
1271 static int nct6775_init_fan_common(struct device *dev,
1272 struct nct6775_data *data)
1277 if (data->has_fan_div) {
1278 err = nct6775_init_fan_div(data);
1284 * If fan_min is not set (0), set it to 0xff to disable it. This
1285 * prevents the unnecessary warning when fanX_min is reported as 0.
1287 for (i = 0; i < ARRAY_SIZE(data->fan_min); i++) {
1288 if (data->has_fan_min & BIT(i)) {
1289 err = nct6775_read_value(data, data->REG_FAN_MIN[i], ®);
1293 err = nct6775_write_value(data, data->REG_FAN_MIN[i],
1294 data->has_fan_div ? 0xff : 0xff1f);
1304 static int nct6775_select_fan_div(struct device *dev,
1305 struct nct6775_data *data, int nr, u16 reg)
1308 u8 fan_div = data->fan_div[nr];
1311 if (!data->has_fan_div)
1315 * If we failed to measure the fan speed, or the reported value is not
1316 * in the optimal range, and the clock divider can be modified,
1317 * let's try that for next time.
1319 if (reg == 0x00 && fan_div < 0x07)
1321 else if (reg != 0x00 && reg < 0x30 && fan_div > 0)
1324 if (fan_div != data->fan_div[nr]) {
1325 dev_dbg(dev, "Modifying fan%d clock divider from %u to %u\n",
1326 nr + 1, div_from_reg(data->fan_div[nr]),
1327 div_from_reg(fan_div));
1329 /* Preserve min limit if possible */
1330 if (data->has_fan_min & BIT(nr)) {
1331 fan_min = data->fan_min[nr];
1332 if (fan_div > data->fan_div[nr]) {
1333 if (fan_min != 255 && fan_min > 1)
1336 if (fan_min != 255) {
1342 if (fan_min != data->fan_min[nr]) {
1343 data->fan_min[nr] = fan_min;
1344 err = nct6775_write_value(data, data->REG_FAN_MIN[nr], fan_min);
1349 data->fan_div[nr] = fan_div;
1350 err = nct6775_write_fan_div_common(data, nr);
1358 static int nct6775_update_pwm(struct device *dev)
1360 struct nct6775_data *data = dev_get_drvdata(dev);
1362 u16 fanmodecfg, reg;
1365 for (i = 0; i < data->pwm_num; i++) {
1366 if (!(data->has_pwm & BIT(i)))
1369 err = nct6775_read_value(data, data->REG_PWM_MODE[i], ®);
1372 duty_is_dc = data->REG_PWM_MODE[i] && (reg & data->PWM_MODE_MASK[i]);
1373 data->pwm_mode[i] = !duty_is_dc;
1375 err = nct6775_read_value(data, data->REG_FAN_MODE[i], &fanmodecfg);
1378 for (j = 0; j < ARRAY_SIZE(data->REG_PWM); j++) {
1379 if (data->REG_PWM[j] && data->REG_PWM[j][i]) {
1380 err = nct6775_read_value(data, data->REG_PWM[j][i], ®);
1383 data->pwm[j][i] = reg;
1387 data->pwm_enable[i] = reg_to_pwm_enable(data->pwm[0][i],
1388 (fanmodecfg >> 4) & 7);
1390 if (!data->temp_tolerance[0][i] ||
1391 data->pwm_enable[i] != speed_cruise)
1392 data->temp_tolerance[0][i] = fanmodecfg & 0x0f;
1393 if (!data->target_speed_tolerance[i] ||
1394 data->pwm_enable[i] == speed_cruise) {
1395 u8 t = fanmodecfg & 0x0f;
1397 if (data->REG_TOLERANCE_H) {
1398 err = nct6775_read_value(data, data->REG_TOLERANCE_H[i], ®);
1401 t |= (reg & 0x70) >> 1;
1403 data->target_speed_tolerance[i] = t;
1406 err = nct6775_read_value(data, data->REG_CRITICAL_TEMP_TOLERANCE[i], ®);
1409 data->temp_tolerance[1][i] = reg;
1411 err = nct6775_read_value(data, data->REG_TEMP_SEL[i], ®);
1414 data->pwm_temp_sel[i] = reg & 0x1f;
1415 /* If fan can stop, report floor as 0 */
1417 data->pwm[2][i] = 0;
1419 if (!data->REG_WEIGHT_TEMP_SEL[i])
1422 err = nct6775_read_value(data, data->REG_WEIGHT_TEMP_SEL[i], ®);
1425 data->pwm_weight_temp_sel[i] = reg & 0x1f;
1426 /* If weight is disabled, report weight source as 0 */
1428 data->pwm_weight_temp_sel[i] = 0;
1430 /* Weight temp data */
1431 for (j = 0; j < ARRAY_SIZE(data->weight_temp); j++) {
1432 err = nct6775_read_value(data, data->REG_WEIGHT_TEMP[j][i], ®);
1435 data->weight_temp[j][i] = reg;
1442 static int nct6775_update_pwm_limits(struct device *dev)
1444 struct nct6775_data *data = dev_get_drvdata(dev);
1448 for (i = 0; i < data->pwm_num; i++) {
1449 if (!(data->has_pwm & BIT(i)))
1452 for (j = 0; j < ARRAY_SIZE(data->fan_time); j++) {
1453 err = nct6775_read_value(data, data->REG_FAN_TIME[j][i], ®);
1456 data->fan_time[j][i] = reg;
1459 err = nct6775_read_value(data, data->REG_TARGET[i], ®_t);
1463 /* Update only in matching mode or if never updated */
1464 if (!data->target_temp[i] ||
1465 data->pwm_enable[i] == thermal_cruise)
1466 data->target_temp[i] = reg_t & data->target_temp_mask;
1467 if (!data->target_speed[i] ||
1468 data->pwm_enable[i] == speed_cruise) {
1469 if (data->REG_TOLERANCE_H) {
1470 err = nct6775_read_value(data, data->REG_TOLERANCE_H[i], ®);
1473 reg_t |= (reg & 0x0f) << 8;
1475 data->target_speed[i] = reg_t;
1478 for (j = 0; j < data->auto_pwm_num; j++) {
1479 err = nct6775_read_value(data, NCT6775_AUTO_PWM(data, i, j), ®);
1482 data->auto_pwm[i][j] = reg;
1484 err = nct6775_read_value(data, NCT6775_AUTO_TEMP(data, i, j), ®);
1487 data->auto_temp[i][j] = reg;
1490 /* critical auto_pwm temperature data */
1491 err = nct6775_read_value(data, data->REG_CRITICAL_TEMP[i], ®);
1494 data->auto_temp[i][data->auto_pwm_num] = reg;
1496 switch (data->kind) {
1498 err = nct6775_read_value(data, NCT6775_REG_CRITICAL_ENAB[i], ®);
1501 data->auto_pwm[i][data->auto_pwm_num] =
1502 (reg & 0x02) ? 0xff : 0x00;
1505 data->auto_pwm[i][data->auto_pwm_num] = 0xff;
1518 err = nct6775_read_value(data, data->REG_CRITICAL_PWM_ENABLE[i], ®);
1521 if (reg & data->CRITICAL_PWM_ENABLE_MASK) {
1522 err = nct6775_read_value(data, data->REG_CRITICAL_PWM[i], ®);
1528 data->auto_pwm[i][data->auto_pwm_num] = reg;
1536 struct nct6775_data *nct6775_update_device(struct device *dev)
1538 struct nct6775_data *data = dev_get_drvdata(dev);
1542 mutex_lock(&data->update_lock);
1544 if (time_after(jiffies, data->last_updated + HZ + HZ / 2)
1546 /* Fan clock dividers */
1547 err = nct6775_update_fan_div_common(data);
1551 /* Measured voltages and limits */
1552 for (i = 0; i < data->in_num; i++) {
1553 if (!(data->have_in & BIT(i)))
1556 err = nct6775_read_value(data, data->REG_VIN[i], ®);
1559 data->in[i][0] = reg;
1561 err = nct6775_read_value(data, data->REG_IN_MINMAX[0][i], ®);
1564 data->in[i][1] = reg;
1566 err = nct6775_read_value(data, data->REG_IN_MINMAX[1][i], ®);
1569 data->in[i][2] = reg;
1572 /* Measured fan speeds and limits */
1573 for (i = 0; i < ARRAY_SIZE(data->rpm); i++) {
1574 if (!(data->has_fan & BIT(i)))
1577 err = nct6775_read_value(data, data->REG_FAN[i], ®);
1580 data->rpm[i] = data->fan_from_reg(reg,
1583 if (data->has_fan_min & BIT(i)) {
1584 err = nct6775_read_value(data, data->REG_FAN_MIN[i], ®);
1587 data->fan_min[i] = reg;
1590 if (data->REG_FAN_PULSES[i]) {
1591 err = nct6775_read_value(data, data->REG_FAN_PULSES[i], ®);
1594 data->fan_pulses[i] = (reg >> data->FAN_PULSE_SHIFT[i]) & 0x03;
1597 err = nct6775_select_fan_div(dev, data, i, reg);
1602 err = nct6775_update_pwm(dev);
1606 err = nct6775_update_pwm_limits(dev);
1610 /* Measured temperatures and limits */
1611 for (i = 0; i < NUM_TEMP; i++) {
1612 if (!(data->have_temp & BIT(i)))
1614 for (j = 0; j < ARRAY_SIZE(data->reg_temp); j++) {
1615 if (data->reg_temp[j][i]) {
1616 err = nct6775_read_temp(data, data->reg_temp[j][i], ®);
1619 data->temp[j][i] = reg;
1622 if (i >= NUM_TEMP_FIXED ||
1623 !(data->have_temp_fixed & BIT(i)))
1625 err = nct6775_read_value(data, data->REG_TEMP_OFFSET[i], ®);
1628 data->temp_offset[i] = reg;
1631 for (i = 0; i < NUM_TSI_TEMP; i++) {
1632 if (!(data->have_tsi_temp & BIT(i)))
1634 err = nct6775_read_value(data, data->REG_TSI_TEMP[i], ®);
1637 data->tsi_temp[i] = reg;
1641 for (i = 0; i < NUM_REG_ALARM; i++) {
1644 if (!data->REG_ALARM[i])
1646 err = nct6775_read_value(data, data->REG_ALARM[i], &alarm);
1649 data->alarms |= ((u64)alarm) << (i << 3);
1653 for (i = 0; i < NUM_REG_BEEP; i++) {
1656 if (!data->REG_BEEP[i])
1658 err = nct6775_read_value(data, data->REG_BEEP[i], &beep);
1661 data->beeps |= ((u64)beep) << (i << 3);
1664 data->last_updated = jiffies;
1668 mutex_unlock(&data->update_lock);
1669 return err ? ERR_PTR(err) : data;
1671 EXPORT_SYMBOL_GPL(nct6775_update_device);
1674 * Sysfs callback functions
1677 show_in_reg(struct device *dev, struct device_attribute *attr, char *buf)
1679 struct nct6775_data *data = nct6775_update_device(dev);
1680 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
1681 int index = sattr->index;
1685 return PTR_ERR(data);
1687 return sprintf(buf, "%ld\n",
1688 in_from_reg(data->in[nr][index], nr, data->scale_in));
1692 store_in_reg(struct device *dev, struct device_attribute *attr, const char *buf,
1695 struct nct6775_data *data = dev_get_drvdata(dev);
1696 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
1697 int index = sattr->index;
1702 err = kstrtoul(buf, 10, &val);
1705 mutex_lock(&data->update_lock);
1706 data->in[nr][index] = in_to_reg(val, nr, data->scale_in);
1707 err = nct6775_write_value(data, data->REG_IN_MINMAX[index - 1][nr], data->in[nr][index]);
1708 mutex_unlock(&data->update_lock);
1709 return err ? : count;
1713 nct6775_show_alarm(struct device *dev, struct device_attribute *attr, char *buf)
1715 struct nct6775_data *data = nct6775_update_device(dev);
1716 struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr);
1720 return PTR_ERR(data);
1722 nr = data->ALARM_BITS[sattr->index];
1723 return sprintf(buf, "%u\n",
1724 (unsigned int)((data->alarms >> nr) & 0x01));
1726 EXPORT_SYMBOL_GPL(nct6775_show_alarm);
1728 static int find_temp_source(struct nct6775_data *data, int index, int count)
1730 int source = data->temp_src[index];
1733 for (nr = 0; nr < count; nr++) {
1736 err = nct6775_read_value(data, data->REG_TEMP_SOURCE[nr], &src);
1739 if ((src & 0x1f) == source)
1746 show_temp_alarm(struct device *dev, struct device_attribute *attr, char *buf)
1748 struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr);
1749 struct nct6775_data *data = nct6775_update_device(dev);
1750 unsigned int alarm = 0;
1754 return PTR_ERR(data);
1757 * For temperatures, there is no fixed mapping from registers to alarm
1758 * bits. Alarm bits are determined by the temperature source mapping.
1760 nr = find_temp_source(data, sattr->index, data->num_temp_alarms);
1762 int bit = data->ALARM_BITS[nr + TEMP_ALARM_BASE];
1764 alarm = (data->alarms >> bit) & 0x01;
1766 return sprintf(buf, "%u\n", alarm);
1770 nct6775_show_beep(struct device *dev, struct device_attribute *attr, char *buf)
1772 struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr);
1773 struct nct6775_data *data = nct6775_update_device(dev);
1777 return PTR_ERR(data);
1779 nr = data->BEEP_BITS[sattr->index];
1781 return sprintf(buf, "%u\n",
1782 (unsigned int)((data->beeps >> nr) & 0x01));
1784 EXPORT_SYMBOL_GPL(nct6775_show_beep);
1787 nct6775_store_beep(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
1789 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
1790 struct nct6775_data *data = dev_get_drvdata(dev);
1791 int nr = data->BEEP_BITS[sattr->index];
1792 int regindex = nr >> 3;
1796 err = kstrtoul(buf, 10, &val);
1802 mutex_lock(&data->update_lock);
1804 data->beeps |= (1ULL << nr);
1806 data->beeps &= ~(1ULL << nr);
1807 err = nct6775_write_value(data, data->REG_BEEP[regindex],
1808 (data->beeps >> (regindex << 3)) & 0xff);
1809 mutex_unlock(&data->update_lock);
1810 return err ? : count;
1812 EXPORT_SYMBOL_GPL(nct6775_store_beep);
1815 show_temp_beep(struct device *dev, struct device_attribute *attr, char *buf)
1817 struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr);
1818 struct nct6775_data *data = nct6775_update_device(dev);
1819 unsigned int beep = 0;
1823 return PTR_ERR(data);
1826 * For temperatures, there is no fixed mapping from registers to beep
1827 * enable bits. Beep enable bits are determined by the temperature
1830 nr = find_temp_source(data, sattr->index, data->num_temp_beeps);
1832 int bit = data->BEEP_BITS[nr + TEMP_ALARM_BASE];
1834 beep = (data->beeps >> bit) & 0x01;
1836 return sprintf(buf, "%u\n", beep);
1840 store_temp_beep(struct device *dev, struct device_attribute *attr,
1841 const char *buf, size_t count)
1843 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
1844 struct nct6775_data *data = dev_get_drvdata(dev);
1845 int nr, bit, regindex;
1849 err = kstrtoul(buf, 10, &val);
1855 nr = find_temp_source(data, sattr->index, data->num_temp_beeps);
1859 bit = data->BEEP_BITS[nr + TEMP_ALARM_BASE];
1860 regindex = bit >> 3;
1862 mutex_lock(&data->update_lock);
1864 data->beeps |= (1ULL << bit);
1866 data->beeps &= ~(1ULL << bit);
1867 err = nct6775_write_value(data, data->REG_BEEP[regindex],
1868 (data->beeps >> (regindex << 3)) & 0xff);
1869 mutex_unlock(&data->update_lock);
1871 return err ? : count;
1874 static umode_t nct6775_in_is_visible(struct kobject *kobj,
1875 struct attribute *attr, int index)
1877 struct device *dev = kobj_to_dev(kobj);
1878 struct nct6775_data *data = dev_get_drvdata(dev);
1879 int in = index / 5; /* voltage index */
1881 if (!(data->have_in & BIT(in)))
1884 return nct6775_attr_mode(data, attr);
1887 SENSOR_TEMPLATE_2(in_input, "in%d_input", 0444, show_in_reg, NULL, 0, 0);
1888 SENSOR_TEMPLATE(in_alarm, "in%d_alarm", 0444, nct6775_show_alarm, NULL, 0);
1889 SENSOR_TEMPLATE(in_beep, "in%d_beep", 0644, nct6775_show_beep, nct6775_store_beep, 0);
1890 SENSOR_TEMPLATE_2(in_min, "in%d_min", 0644, show_in_reg, store_in_reg, 0, 1);
1891 SENSOR_TEMPLATE_2(in_max, "in%d_max", 0644, show_in_reg, store_in_reg, 0, 2);
1894 * nct6775_in_is_visible uses the index into the following array
1895 * to determine if attributes should be created or not.
1896 * Any change in order or content must be matched.
1898 static struct sensor_device_template *nct6775_attributes_in_template[] = {
1899 &sensor_dev_template_in_input,
1900 &sensor_dev_template_in_alarm,
1901 &sensor_dev_template_in_beep,
1902 &sensor_dev_template_in_min,
1903 &sensor_dev_template_in_max,
1907 static const struct sensor_template_group nct6775_in_template_group = {
1908 .templates = nct6775_attributes_in_template,
1909 .is_visible = nct6775_in_is_visible,
1913 show_fan(struct device *dev, struct device_attribute *attr, char *buf)
1915 struct nct6775_data *data = nct6775_update_device(dev);
1916 struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr);
1917 int nr = sattr->index;
1920 return PTR_ERR(data);
1922 return sprintf(buf, "%d\n", data->rpm[nr]);
1926 show_fan_min(struct device *dev, struct device_attribute *attr, char *buf)
1928 struct nct6775_data *data = nct6775_update_device(dev);
1929 struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr);
1930 int nr = sattr->index;
1933 return PTR_ERR(data);
1935 return sprintf(buf, "%d\n",
1936 data->fan_from_reg_min(data->fan_min[nr],
1937 data->fan_div[nr]));
1941 show_fan_div(struct device *dev, struct device_attribute *attr, char *buf)
1943 struct nct6775_data *data = nct6775_update_device(dev);
1944 struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr);
1945 int nr = sattr->index;
1948 return PTR_ERR(data);
1950 return sprintf(buf, "%u\n", div_from_reg(data->fan_div[nr]));
1954 store_fan_min(struct device *dev, struct device_attribute *attr,
1955 const char *buf, size_t count)
1957 struct nct6775_data *data = dev_get_drvdata(dev);
1958 struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr);
1959 int nr = sattr->index;
1965 err = kstrtoul(buf, 10, &val);
1969 mutex_lock(&data->update_lock);
1970 if (!data->has_fan_div) {
1971 /* NCT6776F or NCT6779D; we know this is a 13 bit register */
1977 val = 1350000U / val;
1978 val = (val & 0x1f) | ((val << 3) & 0xff00);
1980 data->fan_min[nr] = val;
1981 goto write_min; /* Leave fan divider alone */
1984 /* No min limit, alarm disabled */
1985 data->fan_min[nr] = 255;
1986 new_div = data->fan_div[nr]; /* No change */
1987 dev_info(dev, "fan%u low limit and alarm disabled\n", nr + 1);
1990 reg = 1350000U / val;
1991 if (reg >= 128 * 255) {
1993 * Speed below this value cannot possibly be represented,
1994 * even with the highest divider (128)
1996 data->fan_min[nr] = 254;
1997 new_div = 7; /* 128 == BIT(7) */
1999 "fan%u low limit %lu below minimum %u, set to minimum\n",
2000 nr + 1, val, data->fan_from_reg_min(254, 7));
2003 * Speed above this value cannot possibly be represented,
2004 * even with the lowest divider (1)
2006 data->fan_min[nr] = 1;
2007 new_div = 0; /* 1 == BIT(0) */
2009 "fan%u low limit %lu above maximum %u, set to maximum\n",
2010 nr + 1, val, data->fan_from_reg_min(1, 0));
2013 * Automatically pick the best divider, i.e. the one such
2014 * that the min limit will correspond to a register value
2015 * in the 96..192 range
2018 while (reg > 192 && new_div < 7) {
2022 data->fan_min[nr] = reg;
2027 * Write both the fan clock divider (if it changed) and the new
2028 * fan min (unconditionally)
2030 if (new_div != data->fan_div[nr]) {
2031 dev_dbg(dev, "fan%u clock divider changed from %u to %u\n",
2032 nr + 1, div_from_reg(data->fan_div[nr]),
2033 div_from_reg(new_div));
2034 data->fan_div[nr] = new_div;
2035 err = nct6775_write_fan_div_common(data, nr);
2038 /* Give the chip time to sample a new speed value */
2039 data->last_updated = jiffies;
2043 err = nct6775_write_value(data, data->REG_FAN_MIN[nr], data->fan_min[nr]);
2044 mutex_unlock(&data->update_lock);
2046 return err ? : count;
2050 show_fan_pulses(struct device *dev, struct device_attribute *attr, char *buf)
2052 struct nct6775_data *data = nct6775_update_device(dev);
2053 struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr);
2057 return PTR_ERR(data);
2059 p = data->fan_pulses[sattr->index];
2060 return sprintf(buf, "%d\n", p ? : 4);
2064 store_fan_pulses(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 mutex_lock(&data->update_lock);
2082 data->fan_pulses[nr] = val & 3;
2083 err = nct6775_read_value(data, data->REG_FAN_PULSES[nr], ®);
2086 reg &= ~(0x03 << data->FAN_PULSE_SHIFT[nr]);
2087 reg |= (val & 3) << data->FAN_PULSE_SHIFT[nr];
2088 err = nct6775_write_value(data, data->REG_FAN_PULSES[nr], reg);
2090 mutex_unlock(&data->update_lock);
2092 return err ? : count;
2095 static umode_t nct6775_fan_is_visible(struct kobject *kobj,
2096 struct attribute *attr, int index)
2098 struct device *dev = kobj_to_dev(kobj);
2099 struct nct6775_data *data = dev_get_drvdata(dev);
2100 int fan = index / 6; /* fan index */
2101 int nr = index % 6; /* attribute index */
2103 if (!(data->has_fan & BIT(fan)))
2106 if (nr == 1 && data->ALARM_BITS[FAN_ALARM_BASE + fan] == -1)
2108 if (nr == 2 && data->BEEP_BITS[FAN_ALARM_BASE + fan] == -1)
2110 if (nr == 3 && !data->REG_FAN_PULSES[fan])
2112 if (nr == 4 && !(data->has_fan_min & BIT(fan)))
2114 if (nr == 5 && data->kind != nct6775)
2117 return nct6775_attr_mode(data, attr);
2120 SENSOR_TEMPLATE(fan_input, "fan%d_input", 0444, show_fan, NULL, 0);
2121 SENSOR_TEMPLATE(fan_alarm, "fan%d_alarm", 0444, nct6775_show_alarm, NULL, FAN_ALARM_BASE);
2122 SENSOR_TEMPLATE(fan_beep, "fan%d_beep", 0644, nct6775_show_beep,
2123 nct6775_store_beep, FAN_ALARM_BASE);
2124 SENSOR_TEMPLATE(fan_pulses, "fan%d_pulses", 0644, show_fan_pulses, store_fan_pulses, 0);
2125 SENSOR_TEMPLATE(fan_min, "fan%d_min", 0644, show_fan_min, store_fan_min, 0);
2126 SENSOR_TEMPLATE(fan_div, "fan%d_div", 0444, show_fan_div, NULL, 0);
2129 * nct6775_fan_is_visible uses the index into the following array
2130 * to determine if attributes should be created or not.
2131 * Any change in order or content must be matched.
2133 static struct sensor_device_template *nct6775_attributes_fan_template[] = {
2134 &sensor_dev_template_fan_input,
2135 &sensor_dev_template_fan_alarm, /* 1 */
2136 &sensor_dev_template_fan_beep, /* 2 */
2137 &sensor_dev_template_fan_pulses,
2138 &sensor_dev_template_fan_min, /* 4 */
2139 &sensor_dev_template_fan_div, /* 5 */
2143 static const struct sensor_template_group nct6775_fan_template_group = {
2144 .templates = nct6775_attributes_fan_template,
2145 .is_visible = nct6775_fan_is_visible,
2150 show_temp_label(struct device *dev, struct device_attribute *attr, char *buf)
2152 struct nct6775_data *data = nct6775_update_device(dev);
2153 struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr);
2154 int nr = sattr->index;
2157 return PTR_ERR(data);
2159 return sprintf(buf, "%s\n", data->temp_label[data->temp_src[nr]]);
2163 show_temp(struct device *dev, struct device_attribute *attr, char *buf)
2165 struct nct6775_data *data = nct6775_update_device(dev);
2166 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
2168 int index = sattr->index;
2171 return PTR_ERR(data);
2173 return sprintf(buf, "%d\n", LM75_TEMP_FROM_REG(data->temp[index][nr]));
2177 store_temp(struct device *dev, struct device_attribute *attr, const char *buf,
2180 struct nct6775_data *data = dev_get_drvdata(dev);
2181 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
2183 int index = sattr->index;
2187 err = kstrtol(buf, 10, &val);
2191 mutex_lock(&data->update_lock);
2192 data->temp[index][nr] = LM75_TEMP_TO_REG(val);
2193 err = nct6775_write_temp(data, data->reg_temp[index][nr], data->temp[index][nr]);
2194 mutex_unlock(&data->update_lock);
2195 return err ? : count;
2199 show_temp_offset(struct device *dev, struct device_attribute *attr, char *buf)
2201 struct nct6775_data *data = nct6775_update_device(dev);
2202 struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr);
2205 return PTR_ERR(data);
2207 return sprintf(buf, "%d\n", data->temp_offset[sattr->index] * 1000);
2211 store_temp_offset(struct device *dev, struct device_attribute *attr,
2212 const char *buf, size_t count)
2214 struct nct6775_data *data = dev_get_drvdata(dev);
2215 struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr);
2216 int nr = sattr->index;
2220 err = kstrtol(buf, 10, &val);
2224 val = clamp_val(DIV_ROUND_CLOSEST(val, 1000), -128, 127);
2226 mutex_lock(&data->update_lock);
2227 data->temp_offset[nr] = val;
2228 err = nct6775_write_value(data, data->REG_TEMP_OFFSET[nr], val);
2229 mutex_unlock(&data->update_lock);
2231 return err ? : count;
2235 show_temp_type(struct device *dev, struct device_attribute *attr, char *buf)
2237 struct nct6775_data *data = nct6775_update_device(dev);
2238 struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr);
2239 int nr = sattr->index;
2242 return PTR_ERR(data);
2244 return sprintf(buf, "%d\n", (int)data->temp_type[nr]);
2248 store_temp_type(struct device *dev, struct device_attribute *attr,
2249 const char *buf, size_t count)
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;
2260 return PTR_ERR(data);
2262 err = kstrtoul(buf, 10, &val);
2266 if (val != 1 && val != 3 && val != 4)
2269 mutex_lock(&data->update_lock);
2271 data->temp_type[nr] = val;
2273 dbit = data->DIODE_MASK << nr;
2275 err = nct6775_read_value(data, data->REG_VBAT, &vbat);
2280 err = nct6775_read_value(data, data->REG_DIODE, &diode);
2286 case 1: /* CPU diode (diode, current mode) */
2290 case 3: /* diode, voltage mode */
2293 case 4: /* thermistor */
2296 err = nct6775_write_value(data, data->REG_VBAT, vbat);
2299 err = nct6775_write_value(data, data->REG_DIODE, diode);
2301 mutex_unlock(&data->update_lock);
2302 return err ? : count;
2305 static umode_t nct6775_temp_is_visible(struct kobject *kobj,
2306 struct attribute *attr, int index)
2308 struct device *dev = kobj_to_dev(kobj);
2309 struct nct6775_data *data = dev_get_drvdata(dev);
2310 int temp = index / 10; /* temp index */
2311 int nr = index % 10; /* attribute index */
2313 if (!(data->have_temp & BIT(temp)))
2316 if (nr == 1 && !data->temp_label)
2319 if (nr == 2 && find_temp_source(data, temp, data->num_temp_alarms) < 0)
2320 return 0; /* alarm */
2322 if (nr == 3 && find_temp_source(data, temp, data->num_temp_beeps) < 0)
2323 return 0; /* beep */
2325 if (nr == 4 && !data->reg_temp[1][temp]) /* max */
2328 if (nr == 5 && !data->reg_temp[2][temp]) /* max_hyst */
2331 if (nr == 6 && !data->reg_temp[3][temp]) /* crit */
2334 if (nr == 7 && !data->reg_temp[4][temp]) /* lcrit */
2337 /* offset and type only apply to fixed sensors */
2338 if (nr > 7 && !(data->have_temp_fixed & BIT(temp)))
2341 return nct6775_attr_mode(data, attr);
2344 SENSOR_TEMPLATE_2(temp_input, "temp%d_input", 0444, show_temp, NULL, 0, 0);
2345 SENSOR_TEMPLATE(temp_label, "temp%d_label", 0444, show_temp_label, NULL, 0);
2346 SENSOR_TEMPLATE_2(temp_max, "temp%d_max", 0644, show_temp, store_temp, 0, 1);
2347 SENSOR_TEMPLATE_2(temp_max_hyst, "temp%d_max_hyst", 0644, show_temp, store_temp, 0, 2);
2348 SENSOR_TEMPLATE_2(temp_crit, "temp%d_crit", 0644, show_temp, store_temp, 0, 3);
2349 SENSOR_TEMPLATE_2(temp_lcrit, "temp%d_lcrit", 0644, show_temp, store_temp, 0, 4);
2350 SENSOR_TEMPLATE(temp_offset, "temp%d_offset", 0644, show_temp_offset, store_temp_offset, 0);
2351 SENSOR_TEMPLATE(temp_type, "temp%d_type", 0644, show_temp_type, store_temp_type, 0);
2352 SENSOR_TEMPLATE(temp_alarm, "temp%d_alarm", 0444, show_temp_alarm, NULL, 0);
2353 SENSOR_TEMPLATE(temp_beep, "temp%d_beep", 0644, show_temp_beep, store_temp_beep, 0);
2356 * nct6775_temp_is_visible uses the index into the following array
2357 * to determine if attributes should be created or not.
2358 * Any change in order or content must be matched.
2360 static struct sensor_device_template *nct6775_attributes_temp_template[] = {
2361 &sensor_dev_template_temp_input,
2362 &sensor_dev_template_temp_label,
2363 &sensor_dev_template_temp_alarm, /* 2 */
2364 &sensor_dev_template_temp_beep, /* 3 */
2365 &sensor_dev_template_temp_max, /* 4 */
2366 &sensor_dev_template_temp_max_hyst, /* 5 */
2367 &sensor_dev_template_temp_crit, /* 6 */
2368 &sensor_dev_template_temp_lcrit, /* 7 */
2369 &sensor_dev_template_temp_offset, /* 8 */
2370 &sensor_dev_template_temp_type, /* 9 */
2374 static const struct sensor_template_group nct6775_temp_template_group = {
2375 .templates = nct6775_attributes_temp_template,
2376 .is_visible = nct6775_temp_is_visible,
2380 static ssize_t show_tsi_temp(struct device *dev, struct device_attribute *attr, char *buf)
2382 struct nct6775_data *data = nct6775_update_device(dev);
2383 struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr);
2386 return PTR_ERR(data);
2388 return sysfs_emit(buf, "%u\n", tsi_temp_from_reg(data->tsi_temp[sattr->index]));
2391 static ssize_t show_tsi_temp_label(struct device *dev, struct device_attribute *attr, char *buf)
2393 struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr);
2395 return sysfs_emit(buf, "TSI%d_TEMP\n", sattr->index);
2398 SENSOR_TEMPLATE(tsi_temp_input, "temp%d_input", 0444, show_tsi_temp, NULL, 0);
2399 SENSOR_TEMPLATE(tsi_temp_label, "temp%d_label", 0444, show_tsi_temp_label, NULL, 0);
2401 static umode_t nct6775_tsi_temp_is_visible(struct kobject *kobj, struct attribute *attr,
2404 struct device *dev = kobj_to_dev(kobj);
2405 struct nct6775_data *data = dev_get_drvdata(dev);
2406 int temp = index / 2;
2408 return (data->have_tsi_temp & BIT(temp)) ? nct6775_attr_mode(data, attr) : 0;
2412 * The index calculation in nct6775_tsi_temp_is_visible() must be kept in
2413 * sync with the size of this array.
2415 static struct sensor_device_template *nct6775_tsi_temp_template[] = {
2416 &sensor_dev_template_tsi_temp_input,
2417 &sensor_dev_template_tsi_temp_label,
2422 show_pwm_mode(struct device *dev, struct device_attribute *attr, char *buf)
2424 struct nct6775_data *data = nct6775_update_device(dev);
2425 struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr);
2428 return PTR_ERR(data);
2430 return sprintf(buf, "%d\n", data->pwm_mode[sattr->index]);
2434 store_pwm_mode(struct device *dev, struct device_attribute *attr,
2435 const char *buf, size_t count)
2437 struct nct6775_data *data = dev_get_drvdata(dev);
2438 struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr);
2439 int nr = sattr->index;
2444 err = kstrtoul(buf, 10, &val);
2451 /* Setting DC mode (0) is not supported for all chips/channels */
2452 if (data->REG_PWM_MODE[nr] == 0) {
2458 mutex_lock(&data->update_lock);
2459 data->pwm_mode[nr] = val;
2460 err = nct6775_read_value(data, data->REG_PWM_MODE[nr], ®);
2463 reg &= ~data->PWM_MODE_MASK[nr];
2465 reg |= data->PWM_MODE_MASK[nr];
2466 err = nct6775_write_value(data, data->REG_PWM_MODE[nr], reg);
2468 mutex_unlock(&data->update_lock);
2469 return err ? : count;
2473 show_pwm(struct device *dev, struct device_attribute *attr, char *buf)
2475 struct nct6775_data *data = nct6775_update_device(dev);
2476 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
2478 int index = sattr->index;
2483 return PTR_ERR(data);
2486 * For automatic fan control modes, show current pwm readings.
2487 * Otherwise, show the configured value.
2489 if (index == 0 && data->pwm_enable[nr] > manual) {
2490 err = nct6775_read_value(data, data->REG_PWM_READ[nr], &pwm);
2494 pwm = data->pwm[index][nr];
2497 return sprintf(buf, "%d\n", pwm);
2501 store_pwm(struct device *dev, struct device_attribute *attr, const char *buf,
2504 struct nct6775_data *data = dev_get_drvdata(dev);
2505 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
2507 int index = sattr->index;
2509 int minval[7] = { 0, 1, 1, data->pwm[2][nr], 0, 0, 0 };
2511 = { 255, 255, data->pwm[3][nr] ? : 255, 255, 255, 255, 255 };
2515 err = kstrtoul(buf, 10, &val);
2518 val = clamp_val(val, minval[index], maxval[index]);
2520 mutex_lock(&data->update_lock);
2521 data->pwm[index][nr] = val;
2522 err = nct6775_write_value(data, data->REG_PWM[index][nr], val);
2525 if (index == 2) { /* floor: disable if val == 0 */
2526 err = nct6775_read_value(data, data->REG_TEMP_SEL[nr], ®);
2532 err = nct6775_write_value(data, data->REG_TEMP_SEL[nr], reg);
2535 mutex_unlock(&data->update_lock);
2536 return err ? : count;
2539 /* Returns 0 if OK, -EINVAL otherwise */
2540 static int check_trip_points(struct nct6775_data *data, int nr)
2544 for (i = 0; i < data->auto_pwm_num - 1; i++) {
2545 if (data->auto_temp[nr][i] > data->auto_temp[nr][i + 1])
2548 for (i = 0; i < data->auto_pwm_num - 1; i++) {
2549 if (data->auto_pwm[nr][i] > data->auto_pwm[nr][i + 1])
2552 /* validate critical temperature and pwm if enabled (pwm > 0) */
2553 if (data->auto_pwm[nr][data->auto_pwm_num]) {
2554 if (data->auto_temp[nr][data->auto_pwm_num - 1] >
2555 data->auto_temp[nr][data->auto_pwm_num] ||
2556 data->auto_pwm[nr][data->auto_pwm_num - 1] >
2557 data->auto_pwm[nr][data->auto_pwm_num])
2563 static int pwm_update_registers(struct nct6775_data *data, int nr)
2568 switch (data->pwm_enable[nr]) {
2573 err = nct6775_read_value(data, data->REG_FAN_MODE[nr], ®);
2576 reg = (reg & ~data->tolerance_mask) |
2577 (data->target_speed_tolerance[nr] & data->tolerance_mask);
2578 err = nct6775_write_value(data, data->REG_FAN_MODE[nr], reg);
2581 err = nct6775_write_value(data, data->REG_TARGET[nr],
2582 data->target_speed[nr] & 0xff);
2585 if (data->REG_TOLERANCE_H) {
2586 reg = (data->target_speed[nr] >> 8) & 0x0f;
2587 reg |= (data->target_speed_tolerance[nr] & 0x38) << 1;
2588 err = nct6775_write_value(data, data->REG_TOLERANCE_H[nr], reg);
2593 case thermal_cruise:
2594 err = nct6775_write_value(data, data->REG_TARGET[nr], data->target_temp[nr]);
2599 err = nct6775_read_value(data, data->REG_FAN_MODE[nr], ®);
2602 reg = (reg & ~data->tolerance_mask) |
2603 data->temp_tolerance[0][nr];
2604 err = nct6775_write_value(data, data->REG_FAN_MODE[nr], reg);
2614 show_pwm_enable(struct device *dev, struct device_attribute *attr, char *buf)
2616 struct nct6775_data *data = nct6775_update_device(dev);
2617 struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr);
2620 return PTR_ERR(data);
2622 return sprintf(buf, "%d\n", data->pwm_enable[sattr->index]);
2626 store_pwm_enable(struct device *dev, struct device_attribute *attr,
2627 const char *buf, size_t count)
2629 struct nct6775_data *data = dev_get_drvdata(dev);
2630 struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr);
2631 int nr = sattr->index;
2636 err = kstrtoul(buf, 10, &val);
2643 if (val == sf3 && data->kind != nct6775)
2646 if (val == sf4 && check_trip_points(data, nr)) {
2647 dev_err(dev, "Inconsistent trip points, not switching to SmartFan IV mode\n");
2648 dev_err(dev, "Adjust trip points and try again\n");
2652 mutex_lock(&data->update_lock);
2653 data->pwm_enable[nr] = val;
2656 * turn off pwm control: select manual mode, set pwm to maximum
2658 data->pwm[0][nr] = 255;
2659 err = nct6775_write_value(data, data->REG_PWM[0][nr], 255);
2663 err = pwm_update_registers(data, nr);
2666 err = nct6775_read_value(data, data->REG_FAN_MODE[nr], ®);
2670 reg |= pwm_enable_to_reg(val) << 4;
2671 err = nct6775_write_value(data, data->REG_FAN_MODE[nr], reg);
2673 mutex_unlock(&data->update_lock);
2674 return err ? : count;
2678 show_pwm_temp_sel_common(struct nct6775_data *data, char *buf, int src)
2682 for (i = 0; i < NUM_TEMP; i++) {
2683 if (!(data->have_temp & BIT(i)))
2685 if (src == data->temp_src[i]) {
2691 return sprintf(buf, "%d\n", sel);
2695 show_pwm_temp_sel(struct device *dev, struct device_attribute *attr, char *buf)
2697 struct nct6775_data *data = nct6775_update_device(dev);
2698 struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr);
2699 int index = sattr->index;
2702 return PTR_ERR(data);
2704 return show_pwm_temp_sel_common(data, buf, data->pwm_temp_sel[index]);
2708 store_pwm_temp_sel(struct device *dev, struct device_attribute *attr,
2709 const char *buf, size_t count)
2711 struct nct6775_data *data = nct6775_update_device(dev);
2712 struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr);
2713 int nr = sattr->index;
2719 return PTR_ERR(data);
2721 err = kstrtoul(buf, 10, &val);
2724 if (val == 0 || val > NUM_TEMP)
2726 if (!(data->have_temp & BIT(val - 1)) || !data->temp_src[val - 1])
2729 mutex_lock(&data->update_lock);
2730 src = data->temp_src[val - 1];
2731 data->pwm_temp_sel[nr] = src;
2732 err = nct6775_read_value(data, data->REG_TEMP_SEL[nr], ®);
2737 err = nct6775_write_value(data, data->REG_TEMP_SEL[nr], reg);
2739 mutex_unlock(&data->update_lock);
2741 return err ? : count;
2745 show_pwm_weight_temp_sel(struct device *dev, struct device_attribute *attr,
2748 struct nct6775_data *data = nct6775_update_device(dev);
2749 struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr);
2750 int index = sattr->index;
2753 return PTR_ERR(data);
2755 return show_pwm_temp_sel_common(data, buf,
2756 data->pwm_weight_temp_sel[index]);
2760 store_pwm_weight_temp_sel(struct device *dev, struct device_attribute *attr,
2761 const char *buf, size_t count)
2763 struct nct6775_data *data = nct6775_update_device(dev);
2764 struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr);
2765 int nr = sattr->index;
2771 return PTR_ERR(data);
2773 err = kstrtoul(buf, 10, &val);
2778 val = array_index_nospec(val, NUM_TEMP + 1);
2779 if (val && (!(data->have_temp & BIT(val - 1)) ||
2780 !data->temp_src[val - 1]))
2783 mutex_lock(&data->update_lock);
2785 src = data->temp_src[val - 1];
2786 data->pwm_weight_temp_sel[nr] = src;
2787 err = nct6775_read_value(data, data->REG_WEIGHT_TEMP_SEL[nr], ®);
2791 reg |= (src | 0x80);
2792 err = nct6775_write_value(data, data->REG_WEIGHT_TEMP_SEL[nr], reg);
2794 data->pwm_weight_temp_sel[nr] = 0;
2795 err = nct6775_read_value(data, data->REG_WEIGHT_TEMP_SEL[nr], ®);
2799 err = nct6775_write_value(data, data->REG_WEIGHT_TEMP_SEL[nr], reg);
2802 mutex_unlock(&data->update_lock);
2804 return err ? : count;
2808 show_target_temp(struct device *dev, struct device_attribute *attr, char *buf)
2810 struct nct6775_data *data = nct6775_update_device(dev);
2811 struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr);
2814 return PTR_ERR(data);
2816 return sprintf(buf, "%d\n", data->target_temp[sattr->index] * 1000);
2820 store_target_temp(struct device *dev, struct device_attribute *attr,
2821 const char *buf, size_t count)
2823 struct nct6775_data *data = dev_get_drvdata(dev);
2824 struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr);
2825 int nr = sattr->index;
2829 err = kstrtoul(buf, 10, &val);
2833 val = clamp_val(DIV_ROUND_CLOSEST(val, 1000), 0,
2834 data->target_temp_mask);
2836 mutex_lock(&data->update_lock);
2837 data->target_temp[nr] = val;
2838 err = pwm_update_registers(data, nr);
2839 mutex_unlock(&data->update_lock);
2840 return err ? : count;
2844 show_target_speed(struct device *dev, struct device_attribute *attr, char *buf)
2846 struct nct6775_data *data = nct6775_update_device(dev);
2847 struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr);
2848 int nr = sattr->index;
2851 return PTR_ERR(data);
2853 return sprintf(buf, "%d\n",
2854 fan_from_reg16(data->target_speed[nr],
2855 data->fan_div[nr]));
2859 store_target_speed(struct device *dev, struct device_attribute *attr,
2860 const char *buf, size_t count)
2862 struct nct6775_data *data = dev_get_drvdata(dev);
2863 struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr);
2864 int nr = sattr->index;
2869 err = kstrtoul(buf, 10, &val);
2873 val = clamp_val(val, 0, 1350000U);
2874 speed = fan_to_reg(val, data->fan_div[nr]);
2876 mutex_lock(&data->update_lock);
2877 data->target_speed[nr] = speed;
2878 err = pwm_update_registers(data, nr);
2879 mutex_unlock(&data->update_lock);
2880 return err ? : count;
2884 show_temp_tolerance(struct device *dev, struct device_attribute *attr,
2887 struct nct6775_data *data = nct6775_update_device(dev);
2888 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
2890 int index = sattr->index;
2893 return PTR_ERR(data);
2895 return sprintf(buf, "%d\n", data->temp_tolerance[index][nr] * 1000);
2899 store_temp_tolerance(struct device *dev, struct device_attribute *attr,
2900 const char *buf, size_t count)
2902 struct nct6775_data *data = dev_get_drvdata(dev);
2903 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
2905 int index = sattr->index;
2909 err = kstrtoul(buf, 10, &val);
2913 /* Limit tolerance as needed */
2914 val = clamp_val(DIV_ROUND_CLOSEST(val, 1000), 0, data->tolerance_mask);
2916 mutex_lock(&data->update_lock);
2917 data->temp_tolerance[index][nr] = val;
2919 err = pwm_update_registers(data, nr);
2921 err = nct6775_write_value(data, data->REG_CRITICAL_TEMP_TOLERANCE[nr], val);
2922 mutex_unlock(&data->update_lock);
2923 return err ? : count;
2927 * Fan speed tolerance is a tricky beast, since the associated register is
2928 * a tick counter, but the value is reported and configured as rpm.
2929 * Compute resulting low and high rpm values and report the difference.
2930 * A fan speed tolerance only makes sense if a fan target speed has been
2931 * configured, so only display values other than 0 if that is the case.
2934 show_speed_tolerance(struct device *dev, struct device_attribute *attr,
2937 struct nct6775_data *data = nct6775_update_device(dev);
2938 struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr);
2939 int nr = sattr->index;
2940 int target, tolerance = 0;
2943 return PTR_ERR(data);
2945 target = data->target_speed[nr];
2948 int low = target - data->target_speed_tolerance[nr];
2949 int high = target + data->target_speed_tolerance[nr];
2958 tolerance = (fan_from_reg16(low, data->fan_div[nr])
2959 - fan_from_reg16(high, data->fan_div[nr])) / 2;
2962 return sprintf(buf, "%d\n", tolerance);
2966 store_speed_tolerance(struct device *dev, struct device_attribute *attr,
2967 const char *buf, size_t count)
2969 struct nct6775_data *data = dev_get_drvdata(dev);
2970 struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr);
2971 int nr = sattr->index;
2976 err = kstrtoul(buf, 10, &val);
2980 high = fan_from_reg16(data->target_speed[nr], data->fan_div[nr]) + val;
2981 low = fan_from_reg16(data->target_speed[nr], data->fan_div[nr]) - val;
2987 val = (fan_to_reg(low, data->fan_div[nr]) -
2988 fan_to_reg(high, data->fan_div[nr])) / 2;
2990 /* Limit tolerance as needed */
2991 val = clamp_val(val, 0, data->speed_tolerance_limit);
2993 mutex_lock(&data->update_lock);
2994 data->target_speed_tolerance[nr] = val;
2995 err = pwm_update_registers(data, nr);
2996 mutex_unlock(&data->update_lock);
2997 return err ? : count;
3000 SENSOR_TEMPLATE_2(pwm, "pwm%d", 0644, show_pwm, store_pwm, 0, 0);
3001 SENSOR_TEMPLATE(pwm_mode, "pwm%d_mode", 0644, show_pwm_mode, store_pwm_mode, 0);
3002 SENSOR_TEMPLATE(pwm_enable, "pwm%d_enable", 0644, show_pwm_enable, store_pwm_enable, 0);
3003 SENSOR_TEMPLATE(pwm_temp_sel, "pwm%d_temp_sel", 0644, show_pwm_temp_sel, store_pwm_temp_sel, 0);
3004 SENSOR_TEMPLATE(pwm_target_temp, "pwm%d_target_temp", 0644, show_target_temp, store_target_temp, 0);
3005 SENSOR_TEMPLATE(fan_target, "fan%d_target", 0644, show_target_speed, store_target_speed, 0);
3006 SENSOR_TEMPLATE(fan_tolerance, "fan%d_tolerance", 0644, show_speed_tolerance,
3007 store_speed_tolerance, 0);
3009 /* Smart Fan registers */
3012 show_weight_temp(struct device *dev, struct device_attribute *attr, char *buf)
3014 struct nct6775_data *data = nct6775_update_device(dev);
3015 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
3017 int index = sattr->index;
3020 return PTR_ERR(data);
3022 return sprintf(buf, "%d\n", data->weight_temp[index][nr] * 1000);
3026 store_weight_temp(struct device *dev, struct device_attribute *attr,
3027 const char *buf, size_t count)
3029 struct nct6775_data *data = dev_get_drvdata(dev);
3030 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
3032 int index = sattr->index;
3036 err = kstrtoul(buf, 10, &val);
3040 val = clamp_val(DIV_ROUND_CLOSEST(val, 1000), 0, 255);
3042 mutex_lock(&data->update_lock);
3043 data->weight_temp[index][nr] = val;
3044 err = nct6775_write_value(data, data->REG_WEIGHT_TEMP[index][nr], val);
3045 mutex_unlock(&data->update_lock);
3046 return err ? : count;
3049 SENSOR_TEMPLATE(pwm_weight_temp_sel, "pwm%d_weight_temp_sel", 0644,
3050 show_pwm_weight_temp_sel, store_pwm_weight_temp_sel, 0);
3051 SENSOR_TEMPLATE_2(pwm_weight_temp_step, "pwm%d_weight_temp_step",
3052 0644, show_weight_temp, store_weight_temp, 0, 0);
3053 SENSOR_TEMPLATE_2(pwm_weight_temp_step_tol, "pwm%d_weight_temp_step_tol",
3054 0644, show_weight_temp, store_weight_temp, 0, 1);
3055 SENSOR_TEMPLATE_2(pwm_weight_temp_step_base, "pwm%d_weight_temp_step_base",
3056 0644, show_weight_temp, store_weight_temp, 0, 2);
3057 SENSOR_TEMPLATE_2(pwm_weight_duty_step, "pwm%d_weight_duty_step", 0644, show_pwm, store_pwm, 0, 5);
3058 SENSOR_TEMPLATE_2(pwm_weight_duty_base, "pwm%d_weight_duty_base", 0644, show_pwm, store_pwm, 0, 6);
3061 show_fan_time(struct device *dev, struct device_attribute *attr, char *buf)
3063 struct nct6775_data *data = nct6775_update_device(dev);
3064 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
3066 int index = sattr->index;
3069 return PTR_ERR(data);
3071 return sprintf(buf, "%d\n",
3072 step_time_from_reg(data->fan_time[index][nr],
3073 data->pwm_mode[nr]));
3077 store_fan_time(struct device *dev, struct device_attribute *attr,
3078 const char *buf, size_t count)
3080 struct nct6775_data *data = dev_get_drvdata(dev);
3081 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
3083 int index = sattr->index;
3087 err = kstrtoul(buf, 10, &val);
3091 val = step_time_to_reg(val, data->pwm_mode[nr]);
3092 mutex_lock(&data->update_lock);
3093 data->fan_time[index][nr] = val;
3094 err = nct6775_write_value(data, data->REG_FAN_TIME[index][nr], val);
3095 mutex_unlock(&data->update_lock);
3096 return err ? : count;
3100 show_auto_pwm(struct device *dev, struct device_attribute *attr, char *buf)
3102 struct nct6775_data *data = nct6775_update_device(dev);
3103 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
3106 return PTR_ERR(data);
3108 return sprintf(buf, "%d\n", data->auto_pwm[sattr->nr][sattr->index]);
3112 store_auto_pwm(struct device *dev, struct device_attribute *attr,
3113 const char *buf, size_t count)
3115 struct nct6775_data *data = dev_get_drvdata(dev);
3116 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
3118 int point = sattr->index;
3123 err = kstrtoul(buf, 10, &val);
3129 if (point == data->auto_pwm_num) {
3130 if (data->kind != nct6775 && !val)
3132 if (data->kind != nct6779 && val)
3136 mutex_lock(&data->update_lock);
3137 data->auto_pwm[nr][point] = val;
3138 if (point < data->auto_pwm_num) {
3139 err = nct6775_write_value(data, NCT6775_AUTO_PWM(data, nr, point),
3140 data->auto_pwm[nr][point]);
3142 switch (data->kind) {
3144 /* disable if needed (pwm == 0) */
3145 err = nct6775_read_value(data, NCT6775_REG_CRITICAL_ENAB[nr], ®);
3152 err = nct6775_write_value(data, NCT6775_REG_CRITICAL_ENAB[nr], reg);
3155 break; /* always enabled, nothing to do */
3167 err = nct6775_write_value(data, data->REG_CRITICAL_PWM[nr], val);
3170 err = nct6775_read_value(data, data->REG_CRITICAL_PWM_ENABLE[nr], ®);
3174 reg &= ~data->CRITICAL_PWM_ENABLE_MASK;
3176 reg |= data->CRITICAL_PWM_ENABLE_MASK;
3177 err = nct6775_write_value(data, data->REG_CRITICAL_PWM_ENABLE[nr], reg);
3181 mutex_unlock(&data->update_lock);
3182 return err ? : count;
3186 show_auto_temp(struct device *dev, struct device_attribute *attr, char *buf)
3188 struct nct6775_data *data = nct6775_update_device(dev);
3189 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
3191 int point = sattr->index;
3194 return PTR_ERR(data);
3197 * We don't know for sure if the temperature is signed or unsigned.
3198 * Assume it is unsigned.
3200 return sprintf(buf, "%d\n", data->auto_temp[nr][point] * 1000);
3204 store_auto_temp(struct device *dev, struct device_attribute *attr,
3205 const char *buf, size_t count)
3207 struct nct6775_data *data = dev_get_drvdata(dev);
3208 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
3210 int point = sattr->index;
3214 err = kstrtoul(buf, 10, &val);
3220 mutex_lock(&data->update_lock);
3221 data->auto_temp[nr][point] = DIV_ROUND_CLOSEST(val, 1000);
3222 if (point < data->auto_pwm_num) {
3223 err = nct6775_write_value(data, NCT6775_AUTO_TEMP(data, nr, point),
3224 data->auto_temp[nr][point]);
3226 err = nct6775_write_value(data, data->REG_CRITICAL_TEMP[nr],
3227 data->auto_temp[nr][point]);
3229 mutex_unlock(&data->update_lock);
3230 return err ? : count;
3233 static umode_t nct6775_pwm_is_visible(struct kobject *kobj,
3234 struct attribute *attr, int index)
3236 struct device *dev = kobj_to_dev(kobj);
3237 struct nct6775_data *data = dev_get_drvdata(dev);
3238 int pwm = index / 36; /* pwm index */
3239 int nr = index % 36; /* attribute index */
3241 if (!(data->has_pwm & BIT(pwm)))
3244 if ((nr >= 14 && nr <= 18) || nr == 21) /* weight */
3245 if (!data->REG_WEIGHT_TEMP_SEL[pwm])
3247 if (nr == 19 && data->REG_PWM[3] == NULL) /* pwm_max */
3249 if (nr == 20 && data->REG_PWM[4] == NULL) /* pwm_step */
3251 if (nr == 21 && data->REG_PWM[6] == NULL) /* weight_duty_base */
3254 if (nr >= 22 && nr <= 35) { /* auto point */
3255 int api = (nr - 22) / 2; /* auto point index */
3257 if (api > data->auto_pwm_num)
3260 return nct6775_attr_mode(data, attr);
3263 SENSOR_TEMPLATE_2(pwm_stop_time, "pwm%d_stop_time", 0644, show_fan_time, store_fan_time, 0, 0);
3264 SENSOR_TEMPLATE_2(pwm_step_up_time, "pwm%d_step_up_time", 0644,
3265 show_fan_time, store_fan_time, 0, 1);
3266 SENSOR_TEMPLATE_2(pwm_step_down_time, "pwm%d_step_down_time", 0644,
3267 show_fan_time, store_fan_time, 0, 2);
3268 SENSOR_TEMPLATE_2(pwm_start, "pwm%d_start", 0644, show_pwm, store_pwm, 0, 1);
3269 SENSOR_TEMPLATE_2(pwm_floor, "pwm%d_floor", 0644, show_pwm, store_pwm, 0, 2);
3270 SENSOR_TEMPLATE_2(pwm_temp_tolerance, "pwm%d_temp_tolerance", 0644,
3271 show_temp_tolerance, store_temp_tolerance, 0, 0);
3272 SENSOR_TEMPLATE_2(pwm_crit_temp_tolerance, "pwm%d_crit_temp_tolerance",
3273 0644, show_temp_tolerance, store_temp_tolerance, 0, 1);
3275 SENSOR_TEMPLATE_2(pwm_max, "pwm%d_max", 0644, show_pwm, store_pwm, 0, 3);
3277 SENSOR_TEMPLATE_2(pwm_step, "pwm%d_step", 0644, show_pwm, store_pwm, 0, 4);
3279 SENSOR_TEMPLATE_2(pwm_auto_point1_pwm, "pwm%d_auto_point1_pwm",
3280 0644, show_auto_pwm, store_auto_pwm, 0, 0);
3281 SENSOR_TEMPLATE_2(pwm_auto_point1_temp, "pwm%d_auto_point1_temp",
3282 0644, show_auto_temp, store_auto_temp, 0, 0);
3284 SENSOR_TEMPLATE_2(pwm_auto_point2_pwm, "pwm%d_auto_point2_pwm",
3285 0644, show_auto_pwm, store_auto_pwm, 0, 1);
3286 SENSOR_TEMPLATE_2(pwm_auto_point2_temp, "pwm%d_auto_point2_temp",
3287 0644, show_auto_temp, store_auto_temp, 0, 1);
3289 SENSOR_TEMPLATE_2(pwm_auto_point3_pwm, "pwm%d_auto_point3_pwm",
3290 0644, show_auto_pwm, store_auto_pwm, 0, 2);
3291 SENSOR_TEMPLATE_2(pwm_auto_point3_temp, "pwm%d_auto_point3_temp",
3292 0644, show_auto_temp, store_auto_temp, 0, 2);
3294 SENSOR_TEMPLATE_2(pwm_auto_point4_pwm, "pwm%d_auto_point4_pwm",
3295 0644, show_auto_pwm, store_auto_pwm, 0, 3);
3296 SENSOR_TEMPLATE_2(pwm_auto_point4_temp, "pwm%d_auto_point4_temp",
3297 0644, show_auto_temp, store_auto_temp, 0, 3);
3299 SENSOR_TEMPLATE_2(pwm_auto_point5_pwm, "pwm%d_auto_point5_pwm",
3300 0644, show_auto_pwm, store_auto_pwm, 0, 4);
3301 SENSOR_TEMPLATE_2(pwm_auto_point5_temp, "pwm%d_auto_point5_temp",
3302 0644, show_auto_temp, store_auto_temp, 0, 4);
3304 SENSOR_TEMPLATE_2(pwm_auto_point6_pwm, "pwm%d_auto_point6_pwm",
3305 0644, show_auto_pwm, store_auto_pwm, 0, 5);
3306 SENSOR_TEMPLATE_2(pwm_auto_point6_temp, "pwm%d_auto_point6_temp",
3307 0644, show_auto_temp, store_auto_temp, 0, 5);
3309 SENSOR_TEMPLATE_2(pwm_auto_point7_pwm, "pwm%d_auto_point7_pwm",
3310 0644, show_auto_pwm, store_auto_pwm, 0, 6);
3311 SENSOR_TEMPLATE_2(pwm_auto_point7_temp, "pwm%d_auto_point7_temp",
3312 0644, show_auto_temp, store_auto_temp, 0, 6);
3315 * nct6775_pwm_is_visible uses the index into the following array
3316 * to determine if attributes should be created or not.
3317 * Any change in order or content must be matched.
3319 static struct sensor_device_template *nct6775_attributes_pwm_template[] = {
3320 &sensor_dev_template_pwm,
3321 &sensor_dev_template_pwm_mode,
3322 &sensor_dev_template_pwm_enable,
3323 &sensor_dev_template_pwm_temp_sel,
3324 &sensor_dev_template_pwm_temp_tolerance,
3325 &sensor_dev_template_pwm_crit_temp_tolerance,
3326 &sensor_dev_template_pwm_target_temp,
3327 &sensor_dev_template_fan_target,
3328 &sensor_dev_template_fan_tolerance,
3329 &sensor_dev_template_pwm_stop_time,
3330 &sensor_dev_template_pwm_step_up_time,
3331 &sensor_dev_template_pwm_step_down_time,
3332 &sensor_dev_template_pwm_start,
3333 &sensor_dev_template_pwm_floor,
3334 &sensor_dev_template_pwm_weight_temp_sel, /* 14 */
3335 &sensor_dev_template_pwm_weight_temp_step,
3336 &sensor_dev_template_pwm_weight_temp_step_tol,
3337 &sensor_dev_template_pwm_weight_temp_step_base,
3338 &sensor_dev_template_pwm_weight_duty_step, /* 18 */
3339 &sensor_dev_template_pwm_max, /* 19 */
3340 &sensor_dev_template_pwm_step, /* 20 */
3341 &sensor_dev_template_pwm_weight_duty_base, /* 21 */
3342 &sensor_dev_template_pwm_auto_point1_pwm, /* 22 */
3343 &sensor_dev_template_pwm_auto_point1_temp,
3344 &sensor_dev_template_pwm_auto_point2_pwm,
3345 &sensor_dev_template_pwm_auto_point2_temp,
3346 &sensor_dev_template_pwm_auto_point3_pwm,
3347 &sensor_dev_template_pwm_auto_point3_temp,
3348 &sensor_dev_template_pwm_auto_point4_pwm,
3349 &sensor_dev_template_pwm_auto_point4_temp,
3350 &sensor_dev_template_pwm_auto_point5_pwm,
3351 &sensor_dev_template_pwm_auto_point5_temp,
3352 &sensor_dev_template_pwm_auto_point6_pwm,
3353 &sensor_dev_template_pwm_auto_point6_temp,
3354 &sensor_dev_template_pwm_auto_point7_pwm,
3355 &sensor_dev_template_pwm_auto_point7_temp, /* 35 */
3360 static const struct sensor_template_group nct6775_pwm_template_group = {
3361 .templates = nct6775_attributes_pwm_template,
3362 .is_visible = nct6775_pwm_is_visible,
3366 static inline int nct6775_init_device(struct nct6775_data *data)
3371 /* Start monitoring if needed */
3372 if (data->REG_CONFIG) {
3373 err = nct6775_read_value(data, data->REG_CONFIG, &tmp);
3376 if (!(tmp & 0x01)) {
3377 err = nct6775_write_value(data, data->REG_CONFIG, tmp | 0x01);
3383 /* Enable temperature sensors if needed */
3384 for (i = 0; i < NUM_TEMP; i++) {
3385 if (!(data->have_temp & BIT(i)))
3387 if (!data->reg_temp_config[i])
3389 err = nct6775_read_value(data, data->reg_temp_config[i], &tmp);
3393 err = nct6775_write_value(data, data->reg_temp_config[i], tmp & 0xfe);
3399 /* Enable VBAT monitoring if needed */
3400 err = nct6775_read_value(data, data->REG_VBAT, &tmp);
3403 if (!(tmp & 0x01)) {
3404 err = nct6775_write_value(data, data->REG_VBAT, tmp | 0x01);
3409 err = nct6775_read_value(data, data->REG_DIODE, &diode);
3413 for (i = 0; i < data->temp_fixed_num; i++) {
3414 if (!(data->have_temp_fixed & BIT(i)))
3416 if ((tmp & (data->DIODE_MASK << i))) /* diode */
3418 = 3 - ((diode >> i) & data->DIODE_MASK);
3419 else /* thermistor */
3420 data->temp_type[i] = 4;
3426 static int add_temp_sensors(struct nct6775_data *data, const u16 *regp,
3427 int *available, int *mask)
3432 for (i = 0; i < data->pwm_num && *available; i++) {
3437 err = nct6775_read_value(data, regp[i], &src);
3441 if (!src || (*mask & BIT(src)))
3443 if (!(data->temp_mask & BIT(src)))
3446 index = __ffs(*available);
3447 err = nct6775_write_value(data, data->REG_TEMP_SOURCE[index], src);
3450 *available &= ~BIT(index);
3457 int nct6775_probe(struct device *dev, struct nct6775_data *data,
3458 const struct regmap_config *regmapcfg)
3461 int mask, available;
3463 const u16 *reg_temp, *reg_temp_over, *reg_temp_hyst, *reg_temp_config;
3464 const u16 *reg_temp_mon, *reg_temp_alternate, *reg_temp_crit;
3465 const u16 *reg_temp_crit_l = NULL, *reg_temp_crit_h = NULL;
3466 int num_reg_temp, num_reg_temp_mon, num_reg_tsi_temp;
3467 struct device *hwmon_dev;
3468 struct sensor_template_group tsi_temp_tg;
3470 data->regmap = devm_regmap_init(dev, NULL, data, regmapcfg);
3471 if (IS_ERR(data->regmap))
3472 return PTR_ERR(data->regmap);
3474 mutex_init(&data->update_lock);
3475 data->name = nct6775_device_names[data->kind];
3476 data->bank = 0xff; /* Force initial bank selection */
3477 data->scale_in = scale_in;
3479 switch (data->kind) {
3483 data->auto_pwm_num = 4;
3484 data->temp_fixed_num = 3;
3485 data->num_temp_alarms = 6;
3486 data->num_temp_beeps = 6;
3488 data->fan_from_reg = fan_from_reg13;
3489 data->fan_from_reg_min = fan_from_reg13;
3491 data->temp_label = nct6776_temp_label;
3492 data->temp_mask = NCT6776_TEMP_MASK;
3493 data->virt_temp_mask = NCT6776_VIRT_TEMP_MASK;
3495 data->REG_VBAT = NCT6106_REG_VBAT;
3496 data->REG_DIODE = NCT6106_REG_DIODE;
3497 data->DIODE_MASK = NCT6106_DIODE_MASK;
3498 data->REG_VIN = NCT6106_REG_IN;
3499 data->REG_IN_MINMAX[0] = NCT6106_REG_IN_MIN;
3500 data->REG_IN_MINMAX[1] = NCT6106_REG_IN_MAX;
3501 data->REG_TARGET = NCT6106_REG_TARGET;
3502 data->REG_FAN = NCT6106_REG_FAN;
3503 data->REG_FAN_MODE = NCT6106_REG_FAN_MODE;
3504 data->REG_FAN_MIN = NCT6106_REG_FAN_MIN;
3505 data->REG_FAN_PULSES = NCT6106_REG_FAN_PULSES;
3506 data->FAN_PULSE_SHIFT = NCT6106_FAN_PULSE_SHIFT;
3507 data->REG_FAN_TIME[0] = NCT6106_REG_FAN_STOP_TIME;
3508 data->REG_FAN_TIME[1] = NCT6106_REG_FAN_STEP_UP_TIME;
3509 data->REG_FAN_TIME[2] = NCT6106_REG_FAN_STEP_DOWN_TIME;
3510 data->REG_TOLERANCE_H = NCT6106_REG_TOLERANCE_H;
3511 data->REG_PWM[0] = NCT6116_REG_PWM;
3512 data->REG_PWM[1] = NCT6106_REG_FAN_START_OUTPUT;
3513 data->REG_PWM[2] = NCT6106_REG_FAN_STOP_OUTPUT;
3514 data->REG_PWM[5] = NCT6106_REG_WEIGHT_DUTY_STEP;
3515 data->REG_PWM[6] = NCT6106_REG_WEIGHT_DUTY_BASE;
3516 data->REG_PWM_READ = NCT6106_REG_PWM_READ;
3517 data->REG_PWM_MODE = NCT6106_REG_PWM_MODE;
3518 data->PWM_MODE_MASK = NCT6106_PWM_MODE_MASK;
3519 data->REG_AUTO_TEMP = NCT6106_REG_AUTO_TEMP;
3520 data->REG_AUTO_PWM = NCT6106_REG_AUTO_PWM;
3521 data->REG_CRITICAL_TEMP = NCT6106_REG_CRITICAL_TEMP;
3522 data->REG_CRITICAL_TEMP_TOLERANCE
3523 = NCT6106_REG_CRITICAL_TEMP_TOLERANCE;
3524 data->REG_CRITICAL_PWM_ENABLE = NCT6106_REG_CRITICAL_PWM_ENABLE;
3525 data->CRITICAL_PWM_ENABLE_MASK
3526 = NCT6106_CRITICAL_PWM_ENABLE_MASK;
3527 data->REG_CRITICAL_PWM = NCT6106_REG_CRITICAL_PWM;
3528 data->REG_TEMP_OFFSET = NCT6106_REG_TEMP_OFFSET;
3529 data->REG_TEMP_SOURCE = NCT6106_REG_TEMP_SOURCE;
3530 data->REG_TEMP_SEL = NCT6116_REG_TEMP_SEL;
3531 data->REG_WEIGHT_TEMP_SEL = NCT6106_REG_WEIGHT_TEMP_SEL;
3532 data->REG_WEIGHT_TEMP[0] = NCT6106_REG_WEIGHT_TEMP_STEP;
3533 data->REG_WEIGHT_TEMP[1] = NCT6106_REG_WEIGHT_TEMP_STEP_TOL;
3534 data->REG_WEIGHT_TEMP[2] = NCT6106_REG_WEIGHT_TEMP_BASE;
3535 data->REG_ALARM = NCT6106_REG_ALARM;
3536 data->ALARM_BITS = NCT6106_ALARM_BITS;
3537 data->REG_BEEP = NCT6106_REG_BEEP;
3538 data->BEEP_BITS = NCT6106_BEEP_BITS;
3539 data->REG_TSI_TEMP = NCT6106_REG_TSI_TEMP;
3541 reg_temp = NCT6106_REG_TEMP;
3542 reg_temp_mon = NCT6106_REG_TEMP_MON;
3543 num_reg_temp = ARRAY_SIZE(NCT6106_REG_TEMP);
3544 num_reg_temp_mon = ARRAY_SIZE(NCT6106_REG_TEMP_MON);
3545 num_reg_tsi_temp = ARRAY_SIZE(NCT6106_REG_TSI_TEMP);
3546 reg_temp_over = NCT6106_REG_TEMP_OVER;
3547 reg_temp_hyst = NCT6106_REG_TEMP_HYST;
3548 reg_temp_config = NCT6106_REG_TEMP_CONFIG;
3549 reg_temp_alternate = NCT6106_REG_TEMP_ALTERNATE;
3550 reg_temp_crit = NCT6106_REG_TEMP_CRIT;
3551 reg_temp_crit_l = NCT6106_REG_TEMP_CRIT_L;
3552 reg_temp_crit_h = NCT6106_REG_TEMP_CRIT_H;
3558 data->auto_pwm_num = 4;
3559 data->temp_fixed_num = 3;
3560 data->num_temp_alarms = 3;
3561 data->num_temp_beeps = 3;
3563 data->fan_from_reg = fan_from_reg13;
3564 data->fan_from_reg_min = fan_from_reg13;
3566 data->temp_label = nct6776_temp_label;
3567 data->temp_mask = NCT6776_TEMP_MASK;
3568 data->virt_temp_mask = NCT6776_VIRT_TEMP_MASK;
3570 data->REG_VBAT = NCT6106_REG_VBAT;
3571 data->REG_DIODE = NCT6106_REG_DIODE;
3572 data->DIODE_MASK = NCT6106_DIODE_MASK;
3573 data->REG_VIN = NCT6106_REG_IN;
3574 data->REG_IN_MINMAX[0] = NCT6106_REG_IN_MIN;
3575 data->REG_IN_MINMAX[1] = NCT6106_REG_IN_MAX;
3576 data->REG_TARGET = NCT6116_REG_TARGET;
3577 data->REG_FAN = NCT6116_REG_FAN;
3578 data->REG_FAN_MODE = NCT6116_REG_FAN_MODE;
3579 data->REG_FAN_MIN = NCT6116_REG_FAN_MIN;
3580 data->REG_FAN_PULSES = NCT6116_REG_FAN_PULSES;
3581 data->FAN_PULSE_SHIFT = NCT6116_FAN_PULSE_SHIFT;
3582 data->REG_FAN_TIME[0] = NCT6116_REG_FAN_STOP_TIME;
3583 data->REG_FAN_TIME[1] = NCT6116_REG_FAN_STEP_UP_TIME;
3584 data->REG_FAN_TIME[2] = NCT6116_REG_FAN_STEP_DOWN_TIME;
3585 data->REG_TOLERANCE_H = NCT6116_REG_TOLERANCE_H;
3586 data->REG_PWM[0] = NCT6116_REG_PWM;
3587 data->REG_PWM[1] = NCT6116_REG_FAN_START_OUTPUT;
3588 data->REG_PWM[2] = NCT6116_REG_FAN_STOP_OUTPUT;
3589 data->REG_PWM[5] = NCT6106_REG_WEIGHT_DUTY_STEP;
3590 data->REG_PWM[6] = NCT6106_REG_WEIGHT_DUTY_BASE;
3591 data->REG_PWM_READ = NCT6106_REG_PWM_READ;
3592 data->REG_PWM_MODE = NCT6106_REG_PWM_MODE;
3593 data->PWM_MODE_MASK = NCT6106_PWM_MODE_MASK;
3594 data->REG_AUTO_TEMP = NCT6116_REG_AUTO_TEMP;
3595 data->REG_AUTO_PWM = NCT6116_REG_AUTO_PWM;
3596 data->REG_CRITICAL_TEMP = NCT6116_REG_CRITICAL_TEMP;
3597 data->REG_CRITICAL_TEMP_TOLERANCE
3598 = NCT6116_REG_CRITICAL_TEMP_TOLERANCE;
3599 data->REG_CRITICAL_PWM_ENABLE = NCT6116_REG_CRITICAL_PWM_ENABLE;
3600 data->CRITICAL_PWM_ENABLE_MASK
3601 = NCT6106_CRITICAL_PWM_ENABLE_MASK;
3602 data->REG_CRITICAL_PWM = NCT6116_REG_CRITICAL_PWM;
3603 data->REG_TEMP_OFFSET = NCT6106_REG_TEMP_OFFSET;
3604 data->REG_TEMP_SOURCE = NCT6116_REG_TEMP_SOURCE;
3605 data->REG_TEMP_SEL = NCT6116_REG_TEMP_SEL;
3606 data->REG_WEIGHT_TEMP_SEL = NCT6106_REG_WEIGHT_TEMP_SEL;
3607 data->REG_WEIGHT_TEMP[0] = NCT6106_REG_WEIGHT_TEMP_STEP;
3608 data->REG_WEIGHT_TEMP[1] = NCT6106_REG_WEIGHT_TEMP_STEP_TOL;
3609 data->REG_WEIGHT_TEMP[2] = NCT6106_REG_WEIGHT_TEMP_BASE;
3610 data->REG_ALARM = NCT6106_REG_ALARM;
3611 data->ALARM_BITS = NCT6116_ALARM_BITS;
3612 data->REG_BEEP = NCT6106_REG_BEEP;
3613 data->BEEP_BITS = NCT6116_BEEP_BITS;
3614 data->REG_TSI_TEMP = NCT6116_REG_TSI_TEMP;
3616 reg_temp = NCT6106_REG_TEMP;
3617 reg_temp_mon = NCT6106_REG_TEMP_MON;
3618 num_reg_temp = ARRAY_SIZE(NCT6106_REG_TEMP);
3619 num_reg_temp_mon = ARRAY_SIZE(NCT6106_REG_TEMP_MON);
3620 num_reg_tsi_temp = ARRAY_SIZE(NCT6116_REG_TSI_TEMP);
3621 reg_temp_over = NCT6106_REG_TEMP_OVER;
3622 reg_temp_hyst = NCT6106_REG_TEMP_HYST;
3623 reg_temp_config = NCT6106_REG_TEMP_CONFIG;
3624 reg_temp_alternate = NCT6106_REG_TEMP_ALTERNATE;
3625 reg_temp_crit = NCT6106_REG_TEMP_CRIT;
3626 reg_temp_crit_l = NCT6106_REG_TEMP_CRIT_L;
3627 reg_temp_crit_h = NCT6106_REG_TEMP_CRIT_H;
3633 data->auto_pwm_num = 6;
3634 data->has_fan_div = true;
3635 data->temp_fixed_num = 3;
3636 data->num_temp_alarms = 3;
3637 data->num_temp_beeps = 3;
3639 data->ALARM_BITS = NCT6775_ALARM_BITS;
3640 data->BEEP_BITS = NCT6775_BEEP_BITS;
3642 data->fan_from_reg = fan_from_reg16;
3643 data->fan_from_reg_min = fan_from_reg8;
3644 data->target_temp_mask = 0x7f;
3645 data->tolerance_mask = 0x0f;
3646 data->speed_tolerance_limit = 15;
3648 data->temp_label = nct6775_temp_label;
3649 data->temp_mask = NCT6775_TEMP_MASK;
3650 data->virt_temp_mask = NCT6775_VIRT_TEMP_MASK;
3652 data->REG_CONFIG = NCT6775_REG_CONFIG;
3653 data->REG_VBAT = NCT6775_REG_VBAT;
3654 data->REG_DIODE = NCT6775_REG_DIODE;
3655 data->DIODE_MASK = NCT6775_DIODE_MASK;
3656 data->REG_VIN = NCT6775_REG_IN;
3657 data->REG_IN_MINMAX[0] = NCT6775_REG_IN_MIN;
3658 data->REG_IN_MINMAX[1] = NCT6775_REG_IN_MAX;
3659 data->REG_TARGET = NCT6775_REG_TARGET;
3660 data->REG_FAN = NCT6775_REG_FAN;
3661 data->REG_FAN_MODE = NCT6775_REG_FAN_MODE;
3662 data->REG_FAN_MIN = NCT6775_REG_FAN_MIN;
3663 data->REG_FAN_PULSES = NCT6775_REG_FAN_PULSES;
3664 data->FAN_PULSE_SHIFT = NCT6775_FAN_PULSE_SHIFT;
3665 data->REG_FAN_TIME[0] = NCT6775_REG_FAN_STOP_TIME;
3666 data->REG_FAN_TIME[1] = NCT6775_REG_FAN_STEP_UP_TIME;
3667 data->REG_FAN_TIME[2] = NCT6775_REG_FAN_STEP_DOWN_TIME;
3668 data->REG_PWM[0] = NCT6775_REG_PWM;
3669 data->REG_PWM[1] = NCT6775_REG_FAN_START_OUTPUT;
3670 data->REG_PWM[2] = NCT6775_REG_FAN_STOP_OUTPUT;
3671 data->REG_PWM[3] = NCT6775_REG_FAN_MAX_OUTPUT;
3672 data->REG_PWM[4] = NCT6775_REG_FAN_STEP_OUTPUT;
3673 data->REG_PWM[5] = NCT6775_REG_WEIGHT_DUTY_STEP;
3674 data->REG_PWM_READ = NCT6775_REG_PWM_READ;
3675 data->REG_PWM_MODE = NCT6775_REG_PWM_MODE;
3676 data->PWM_MODE_MASK = NCT6775_PWM_MODE_MASK;
3677 data->REG_AUTO_TEMP = NCT6775_REG_AUTO_TEMP;
3678 data->REG_AUTO_PWM = NCT6775_REG_AUTO_PWM;
3679 data->REG_CRITICAL_TEMP = NCT6775_REG_CRITICAL_TEMP;
3680 data->REG_CRITICAL_TEMP_TOLERANCE
3681 = NCT6775_REG_CRITICAL_TEMP_TOLERANCE;
3682 data->REG_TEMP_OFFSET = NCT6775_REG_TEMP_OFFSET;
3683 data->REG_TEMP_SOURCE = NCT6775_REG_TEMP_SOURCE;
3684 data->REG_TEMP_SEL = NCT6775_REG_TEMP_SEL;
3685 data->REG_WEIGHT_TEMP_SEL = NCT6775_REG_WEIGHT_TEMP_SEL;
3686 data->REG_WEIGHT_TEMP[0] = NCT6775_REG_WEIGHT_TEMP_STEP;
3687 data->REG_WEIGHT_TEMP[1] = NCT6775_REG_WEIGHT_TEMP_STEP_TOL;
3688 data->REG_WEIGHT_TEMP[2] = NCT6775_REG_WEIGHT_TEMP_BASE;
3689 data->REG_ALARM = NCT6775_REG_ALARM;
3690 data->REG_BEEP = NCT6775_REG_BEEP;
3691 data->REG_TSI_TEMP = NCT6775_REG_TSI_TEMP;
3693 reg_temp = NCT6775_REG_TEMP;
3694 reg_temp_mon = NCT6775_REG_TEMP_MON;
3695 num_reg_temp = ARRAY_SIZE(NCT6775_REG_TEMP);
3696 num_reg_temp_mon = ARRAY_SIZE(NCT6775_REG_TEMP_MON);
3697 num_reg_tsi_temp = ARRAY_SIZE(NCT6775_REG_TSI_TEMP);
3698 reg_temp_over = NCT6775_REG_TEMP_OVER;
3699 reg_temp_hyst = NCT6775_REG_TEMP_HYST;
3700 reg_temp_config = NCT6775_REG_TEMP_CONFIG;
3701 reg_temp_alternate = NCT6775_REG_TEMP_ALTERNATE;
3702 reg_temp_crit = NCT6775_REG_TEMP_CRIT;
3708 data->auto_pwm_num = 4;
3709 data->has_fan_div = false;
3710 data->temp_fixed_num = 3;
3711 data->num_temp_alarms = 3;
3712 data->num_temp_beeps = 6;
3714 data->ALARM_BITS = NCT6776_ALARM_BITS;
3715 data->BEEP_BITS = NCT6776_BEEP_BITS;
3717 data->fan_from_reg = fan_from_reg13;
3718 data->fan_from_reg_min = fan_from_reg13;
3719 data->target_temp_mask = 0xff;
3720 data->tolerance_mask = 0x07;
3721 data->speed_tolerance_limit = 63;
3723 data->temp_label = nct6776_temp_label;
3724 data->temp_mask = NCT6776_TEMP_MASK;
3725 data->virt_temp_mask = NCT6776_VIRT_TEMP_MASK;
3727 data->REG_CONFIG = NCT6775_REG_CONFIG;
3728 data->REG_VBAT = NCT6775_REG_VBAT;
3729 data->REG_DIODE = NCT6775_REG_DIODE;
3730 data->DIODE_MASK = NCT6775_DIODE_MASK;
3731 data->REG_VIN = NCT6775_REG_IN;
3732 data->REG_IN_MINMAX[0] = NCT6775_REG_IN_MIN;
3733 data->REG_IN_MINMAX[1] = NCT6775_REG_IN_MAX;
3734 data->REG_TARGET = NCT6775_REG_TARGET;
3735 data->REG_FAN = NCT6775_REG_FAN;
3736 data->REG_FAN_MODE = NCT6775_REG_FAN_MODE;
3737 data->REG_FAN_MIN = NCT6776_REG_FAN_MIN;
3738 data->REG_FAN_PULSES = NCT6776_REG_FAN_PULSES;
3739 data->FAN_PULSE_SHIFT = NCT6775_FAN_PULSE_SHIFT;
3740 data->REG_FAN_TIME[0] = NCT6775_REG_FAN_STOP_TIME;
3741 data->REG_FAN_TIME[1] = NCT6776_REG_FAN_STEP_UP_TIME;
3742 data->REG_FAN_TIME[2] = NCT6776_REG_FAN_STEP_DOWN_TIME;
3743 data->REG_TOLERANCE_H = NCT6776_REG_TOLERANCE_H;
3744 data->REG_PWM[0] = NCT6775_REG_PWM;
3745 data->REG_PWM[1] = NCT6775_REG_FAN_START_OUTPUT;
3746 data->REG_PWM[2] = NCT6775_REG_FAN_STOP_OUTPUT;
3747 data->REG_PWM[5] = NCT6775_REG_WEIGHT_DUTY_STEP;
3748 data->REG_PWM[6] = NCT6776_REG_WEIGHT_DUTY_BASE;
3749 data->REG_PWM_READ = NCT6775_REG_PWM_READ;
3750 data->REG_PWM_MODE = NCT6776_REG_PWM_MODE;
3751 data->PWM_MODE_MASK = NCT6776_PWM_MODE_MASK;
3752 data->REG_AUTO_TEMP = NCT6775_REG_AUTO_TEMP;
3753 data->REG_AUTO_PWM = NCT6775_REG_AUTO_PWM;
3754 data->REG_CRITICAL_TEMP = NCT6775_REG_CRITICAL_TEMP;
3755 data->REG_CRITICAL_TEMP_TOLERANCE
3756 = NCT6775_REG_CRITICAL_TEMP_TOLERANCE;
3757 data->REG_TEMP_OFFSET = NCT6775_REG_TEMP_OFFSET;
3758 data->REG_TEMP_SOURCE = NCT6775_REG_TEMP_SOURCE;
3759 data->REG_TEMP_SEL = NCT6775_REG_TEMP_SEL;
3760 data->REG_WEIGHT_TEMP_SEL = NCT6775_REG_WEIGHT_TEMP_SEL;
3761 data->REG_WEIGHT_TEMP[0] = NCT6775_REG_WEIGHT_TEMP_STEP;
3762 data->REG_WEIGHT_TEMP[1] = NCT6775_REG_WEIGHT_TEMP_STEP_TOL;
3763 data->REG_WEIGHT_TEMP[2] = NCT6775_REG_WEIGHT_TEMP_BASE;
3764 data->REG_ALARM = NCT6775_REG_ALARM;
3765 data->REG_BEEP = NCT6776_REG_BEEP;
3766 data->REG_TSI_TEMP = NCT6776_REG_TSI_TEMP;
3768 reg_temp = NCT6775_REG_TEMP;
3769 reg_temp_mon = NCT6775_REG_TEMP_MON;
3770 num_reg_temp = ARRAY_SIZE(NCT6775_REG_TEMP);
3771 num_reg_temp_mon = ARRAY_SIZE(NCT6775_REG_TEMP_MON);
3772 num_reg_tsi_temp = ARRAY_SIZE(NCT6776_REG_TSI_TEMP);
3773 reg_temp_over = NCT6775_REG_TEMP_OVER;
3774 reg_temp_hyst = NCT6775_REG_TEMP_HYST;
3775 reg_temp_config = NCT6776_REG_TEMP_CONFIG;
3776 reg_temp_alternate = NCT6776_REG_TEMP_ALTERNATE;
3777 reg_temp_crit = NCT6776_REG_TEMP_CRIT;
3783 data->auto_pwm_num = 4;
3784 data->has_fan_div = false;
3785 data->temp_fixed_num = 6;
3786 data->num_temp_alarms = 2;
3787 data->num_temp_beeps = 2;
3789 data->ALARM_BITS = NCT6779_ALARM_BITS;
3790 data->BEEP_BITS = NCT6779_BEEP_BITS;
3792 data->fan_from_reg = fan_from_reg_rpm;
3793 data->fan_from_reg_min = fan_from_reg13;
3794 data->target_temp_mask = 0xff;
3795 data->tolerance_mask = 0x07;
3796 data->speed_tolerance_limit = 63;
3798 data->temp_label = nct6779_temp_label;
3799 data->temp_mask = NCT6779_TEMP_MASK;
3800 data->virt_temp_mask = NCT6779_VIRT_TEMP_MASK;
3802 data->REG_CONFIG = NCT6775_REG_CONFIG;
3803 data->REG_VBAT = NCT6775_REG_VBAT;
3804 data->REG_DIODE = NCT6775_REG_DIODE;
3805 data->DIODE_MASK = NCT6775_DIODE_MASK;
3806 data->REG_VIN = NCT6779_REG_IN;
3807 data->REG_IN_MINMAX[0] = NCT6775_REG_IN_MIN;
3808 data->REG_IN_MINMAX[1] = NCT6775_REG_IN_MAX;
3809 data->REG_TARGET = NCT6775_REG_TARGET;
3810 data->REG_FAN = NCT6779_REG_FAN;
3811 data->REG_FAN_MODE = NCT6775_REG_FAN_MODE;
3812 data->REG_FAN_MIN = NCT6776_REG_FAN_MIN;
3813 data->REG_FAN_PULSES = NCT6779_REG_FAN_PULSES;
3814 data->FAN_PULSE_SHIFT = NCT6775_FAN_PULSE_SHIFT;
3815 data->REG_FAN_TIME[0] = NCT6775_REG_FAN_STOP_TIME;
3816 data->REG_FAN_TIME[1] = NCT6776_REG_FAN_STEP_UP_TIME;
3817 data->REG_FAN_TIME[2] = NCT6776_REG_FAN_STEP_DOWN_TIME;
3818 data->REG_TOLERANCE_H = NCT6776_REG_TOLERANCE_H;
3819 data->REG_PWM[0] = NCT6775_REG_PWM;
3820 data->REG_PWM[1] = NCT6775_REG_FAN_START_OUTPUT;
3821 data->REG_PWM[2] = NCT6775_REG_FAN_STOP_OUTPUT;
3822 data->REG_PWM[5] = NCT6775_REG_WEIGHT_DUTY_STEP;
3823 data->REG_PWM[6] = NCT6776_REG_WEIGHT_DUTY_BASE;
3824 data->REG_PWM_READ = NCT6775_REG_PWM_READ;
3825 data->REG_PWM_MODE = NCT6776_REG_PWM_MODE;
3826 data->PWM_MODE_MASK = NCT6776_PWM_MODE_MASK;
3827 data->REG_AUTO_TEMP = NCT6775_REG_AUTO_TEMP;
3828 data->REG_AUTO_PWM = NCT6775_REG_AUTO_PWM;
3829 data->REG_CRITICAL_TEMP = NCT6775_REG_CRITICAL_TEMP;
3830 data->REG_CRITICAL_TEMP_TOLERANCE
3831 = NCT6775_REG_CRITICAL_TEMP_TOLERANCE;
3832 data->REG_CRITICAL_PWM_ENABLE = NCT6779_REG_CRITICAL_PWM_ENABLE;
3833 data->CRITICAL_PWM_ENABLE_MASK
3834 = NCT6779_CRITICAL_PWM_ENABLE_MASK;
3835 data->REG_CRITICAL_PWM = NCT6779_REG_CRITICAL_PWM;
3836 data->REG_TEMP_OFFSET = NCT6779_REG_TEMP_OFFSET;
3837 data->REG_TEMP_SOURCE = NCT6775_REG_TEMP_SOURCE;
3838 data->REG_TEMP_SEL = NCT6775_REG_TEMP_SEL;
3839 data->REG_WEIGHT_TEMP_SEL = NCT6775_REG_WEIGHT_TEMP_SEL;
3840 data->REG_WEIGHT_TEMP[0] = NCT6775_REG_WEIGHT_TEMP_STEP;
3841 data->REG_WEIGHT_TEMP[1] = NCT6775_REG_WEIGHT_TEMP_STEP_TOL;
3842 data->REG_WEIGHT_TEMP[2] = NCT6775_REG_WEIGHT_TEMP_BASE;
3843 data->REG_ALARM = NCT6779_REG_ALARM;
3844 data->REG_BEEP = NCT6776_REG_BEEP;
3845 data->REG_TSI_TEMP = NCT6776_REG_TSI_TEMP;
3847 reg_temp = NCT6779_REG_TEMP;
3848 reg_temp_mon = NCT6779_REG_TEMP_MON;
3849 num_reg_temp = ARRAY_SIZE(NCT6779_REG_TEMP);
3850 num_reg_temp_mon = ARRAY_SIZE(NCT6779_REG_TEMP_MON);
3851 num_reg_tsi_temp = ARRAY_SIZE(NCT6776_REG_TSI_TEMP);
3852 reg_temp_over = NCT6779_REG_TEMP_OVER;
3853 reg_temp_hyst = NCT6779_REG_TEMP_HYST;
3854 reg_temp_config = NCT6779_REG_TEMP_CONFIG;
3855 reg_temp_alternate = NCT6779_REG_TEMP_ALTERNATE;
3856 reg_temp_crit = NCT6779_REG_TEMP_CRIT;
3868 data->pwm_num = (data->kind == nct6796 ||
3869 data->kind == nct6797 ||
3870 data->kind == nct6798 ||
3871 data->kind == nct6799) ? 7 : 6;
3872 data->auto_pwm_num = 4;
3873 data->has_fan_div = false;
3874 data->temp_fixed_num = 6;
3875 data->num_temp_alarms = 2;
3876 data->num_temp_beeps = 2;
3878 data->ALARM_BITS = NCT6791_ALARM_BITS;
3879 data->BEEP_BITS = NCT6779_BEEP_BITS;
3881 data->fan_from_reg = fan_from_reg_rpm;
3882 data->fan_from_reg_min = fan_from_reg13;
3883 data->target_temp_mask = 0xff;
3884 data->tolerance_mask = 0x07;
3885 data->speed_tolerance_limit = 63;
3887 switch (data->kind) {
3890 data->temp_label = nct6779_temp_label;
3891 data->temp_mask = NCT6791_TEMP_MASK;
3892 data->virt_temp_mask = NCT6791_VIRT_TEMP_MASK;
3895 data->temp_label = nct6792_temp_label;
3896 data->temp_mask = NCT6792_TEMP_MASK;
3897 data->virt_temp_mask = NCT6792_VIRT_TEMP_MASK;
3900 data->temp_label = nct6793_temp_label;
3901 data->temp_mask = NCT6793_TEMP_MASK;
3902 data->virt_temp_mask = NCT6793_VIRT_TEMP_MASK;
3906 data->temp_label = nct6795_temp_label;
3907 data->temp_mask = NCT6795_TEMP_MASK;
3908 data->virt_temp_mask = NCT6795_VIRT_TEMP_MASK;
3911 data->temp_label = nct6796_temp_label;
3912 data->temp_mask = NCT6796_TEMP_MASK;
3913 data->virt_temp_mask = NCT6796_VIRT_TEMP_MASK;
3916 data->temp_label = nct6798_temp_label;
3917 data->temp_mask = NCT6798_TEMP_MASK;
3918 data->virt_temp_mask = NCT6798_VIRT_TEMP_MASK;
3921 data->temp_label = nct6799_temp_label;
3922 data->temp_mask = NCT6799_TEMP_MASK;
3923 data->virt_temp_mask = NCT6799_VIRT_TEMP_MASK;
3927 data->REG_CONFIG = NCT6775_REG_CONFIG;
3928 data->REG_VBAT = NCT6775_REG_VBAT;
3929 data->REG_DIODE = NCT6775_REG_DIODE;
3930 data->DIODE_MASK = NCT6775_DIODE_MASK;
3931 data->REG_VIN = NCT6779_REG_IN;
3932 data->REG_IN_MINMAX[0] = NCT6775_REG_IN_MIN;
3933 data->REG_IN_MINMAX[1] = NCT6775_REG_IN_MAX;
3934 data->REG_TARGET = NCT6775_REG_TARGET;
3935 data->REG_FAN = NCT6779_REG_FAN;
3936 data->REG_FAN_MODE = NCT6775_REG_FAN_MODE;
3937 data->REG_FAN_MIN = NCT6776_REG_FAN_MIN;
3938 data->REG_FAN_PULSES = NCT6779_REG_FAN_PULSES;
3939 data->FAN_PULSE_SHIFT = NCT6775_FAN_PULSE_SHIFT;
3940 data->REG_FAN_TIME[0] = NCT6775_REG_FAN_STOP_TIME;
3941 data->REG_FAN_TIME[1] = NCT6776_REG_FAN_STEP_UP_TIME;
3942 data->REG_FAN_TIME[2] = NCT6776_REG_FAN_STEP_DOWN_TIME;
3943 data->REG_TOLERANCE_H = NCT6776_REG_TOLERANCE_H;
3944 data->REG_PWM[0] = NCT6775_REG_PWM;
3945 data->REG_PWM[1] = NCT6775_REG_FAN_START_OUTPUT;
3946 data->REG_PWM[2] = NCT6775_REG_FAN_STOP_OUTPUT;
3947 data->REG_PWM[5] = NCT6791_REG_WEIGHT_DUTY_STEP;
3948 data->REG_PWM[6] = NCT6791_REG_WEIGHT_DUTY_BASE;
3949 data->REG_PWM_READ = NCT6775_REG_PWM_READ;
3950 data->REG_PWM_MODE = NCT6776_REG_PWM_MODE;
3951 data->PWM_MODE_MASK = NCT6776_PWM_MODE_MASK;
3952 data->REG_AUTO_TEMP = NCT6775_REG_AUTO_TEMP;
3953 data->REG_AUTO_PWM = NCT6775_REG_AUTO_PWM;
3954 data->REG_CRITICAL_TEMP = NCT6775_REG_CRITICAL_TEMP;
3955 data->REG_CRITICAL_TEMP_TOLERANCE
3956 = NCT6775_REG_CRITICAL_TEMP_TOLERANCE;
3957 data->REG_CRITICAL_PWM_ENABLE = NCT6779_REG_CRITICAL_PWM_ENABLE;
3958 data->CRITICAL_PWM_ENABLE_MASK
3959 = NCT6779_CRITICAL_PWM_ENABLE_MASK;
3960 data->REG_CRITICAL_PWM = NCT6779_REG_CRITICAL_PWM;
3961 data->REG_TEMP_OFFSET = NCT6779_REG_TEMP_OFFSET;
3962 data->REG_TEMP_SOURCE = NCT6775_REG_TEMP_SOURCE;
3963 data->REG_TEMP_SEL = NCT6775_REG_TEMP_SEL;
3964 data->REG_WEIGHT_TEMP_SEL = NCT6791_REG_WEIGHT_TEMP_SEL;
3965 data->REG_WEIGHT_TEMP[0] = NCT6791_REG_WEIGHT_TEMP_STEP;
3966 data->REG_WEIGHT_TEMP[1] = NCT6791_REG_WEIGHT_TEMP_STEP_TOL;
3967 data->REG_WEIGHT_TEMP[2] = NCT6791_REG_WEIGHT_TEMP_BASE;
3968 data->REG_ALARM = NCT6791_REG_ALARM;
3969 if (data->kind == nct6791)
3970 data->REG_BEEP = NCT6776_REG_BEEP;
3972 data->REG_BEEP = NCT6792_REG_BEEP;
3973 switch (data->kind) {
3977 data->REG_TSI_TEMP = NCT6776_REG_TSI_TEMP;
3978 num_reg_tsi_temp = ARRAY_SIZE(NCT6776_REG_TSI_TEMP);
3985 data->REG_TSI_TEMP = NCT6796_REG_TSI_TEMP;
3986 num_reg_tsi_temp = ARRAY_SIZE(NCT6796_REG_TSI_TEMP);
3989 num_reg_tsi_temp = 0;
3993 if (data->kind == nct6798 || data->kind == nct6799)
3994 data->scale_in = scale_in_6798;
3996 reg_temp = NCT6779_REG_TEMP;
3997 num_reg_temp = ARRAY_SIZE(NCT6779_REG_TEMP);
3998 if (data->kind == nct6791) {
3999 reg_temp_mon = NCT6779_REG_TEMP_MON;
4000 num_reg_temp_mon = ARRAY_SIZE(NCT6779_REG_TEMP_MON);
4002 reg_temp_mon = NCT6792_REG_TEMP_MON;
4003 num_reg_temp_mon = ARRAY_SIZE(NCT6792_REG_TEMP_MON);
4005 reg_temp_over = NCT6779_REG_TEMP_OVER;
4006 reg_temp_hyst = NCT6779_REG_TEMP_HYST;
4007 reg_temp_config = NCT6779_REG_TEMP_CONFIG;
4008 reg_temp_alternate = NCT6779_REG_TEMP_ALTERNATE;
4009 reg_temp_crit = NCT6779_REG_TEMP_CRIT;
4015 data->have_in = BIT(data->in_num) - 1;
4016 data->have_temp = 0;
4019 * On some boards, not all available temperature sources are monitored,
4020 * even though some of the monitoring registers are unused.
4021 * Get list of unused monitoring registers, then detect if any fan
4022 * controls are configured to use unmonitored temperature sources.
4023 * If so, assign the unmonitored temperature sources to available
4024 * monitoring registers.
4028 for (i = 0; i < num_reg_temp; i++) {
4029 if (reg_temp[i] == 0)
4032 err = nct6775_read_value(data, data->REG_TEMP_SOURCE[i], &src);
4036 if (!src || (mask & BIT(src)))
4037 available |= BIT(i);
4043 * Now find unmonitored temperature registers and enable monitoring
4044 * if additional monitoring registers are available.
4046 err = add_temp_sensors(data, data->REG_TEMP_SEL, &available, &mask);
4049 err = add_temp_sensors(data, data->REG_WEIGHT_TEMP_SEL, &available, &mask);
4054 s = NUM_TEMP_FIXED; /* First dynamic temperature attribute */
4055 for (i = 0; i < num_reg_temp; i++) {
4056 if (reg_temp[i] == 0)
4059 err = nct6775_read_value(data, data->REG_TEMP_SOURCE[i], &src);
4063 if (!src || (mask & BIT(src)))
4066 if (!(data->temp_mask & BIT(src))) {
4068 "Invalid temperature source %d at index %d, source register 0x%x, temp register 0x%x\n",
4069 src, i, data->REG_TEMP_SOURCE[i], reg_temp[i]);
4075 /* Use fixed index for SYSTIN(1), CPUTIN(2), AUXTIN(3) */
4076 if (src <= data->temp_fixed_num) {
4077 data->have_temp |= BIT(src - 1);
4078 data->have_temp_fixed |= BIT(src - 1);
4079 data->reg_temp[0][src - 1] = reg_temp[i];
4080 data->reg_temp[1][src - 1] = reg_temp_over[i];
4081 data->reg_temp[2][src - 1] = reg_temp_hyst[i];
4082 if (reg_temp_crit_h && reg_temp_crit_h[i])
4083 data->reg_temp[3][src - 1] = reg_temp_crit_h[i];
4084 else if (reg_temp_crit[src - 1])
4085 data->reg_temp[3][src - 1]
4086 = reg_temp_crit[src - 1];
4087 if (reg_temp_crit_l && reg_temp_crit_l[i])
4088 data->reg_temp[4][src - 1] = reg_temp_crit_l[i];
4089 data->reg_temp_config[src - 1] = reg_temp_config[i];
4090 data->temp_src[src - 1] = src;
4097 /* Use dynamic index for other sources */
4098 data->have_temp |= BIT(s);
4099 data->reg_temp[0][s] = reg_temp[i];
4100 data->reg_temp[1][s] = reg_temp_over[i];
4101 data->reg_temp[2][s] = reg_temp_hyst[i];
4102 data->reg_temp_config[s] = reg_temp_config[i];
4103 if (reg_temp_crit_h && reg_temp_crit_h[i])
4104 data->reg_temp[3][s] = reg_temp_crit_h[i];
4105 else if (reg_temp_crit[src - 1])
4106 data->reg_temp[3][s] = reg_temp_crit[src - 1];
4107 if (reg_temp_crit_l && reg_temp_crit_l[i])
4108 data->reg_temp[4][s] = reg_temp_crit_l[i];
4110 data->temp_src[s] = src;
4115 * Repeat with temperatures used for fan control.
4116 * This set of registers does not support limits.
4118 for (i = 0; i < num_reg_temp_mon; i++) {
4119 if (reg_temp_mon[i] == 0)
4122 err = nct6775_read_value(data, data->REG_TEMP_SEL[i], &src);
4129 if (!(data->temp_mask & BIT(src))) {
4131 "Invalid temperature source %d at index %d, source register 0x%x, temp register 0x%x\n",
4132 src, i, data->REG_TEMP_SEL[i],
4138 * For virtual temperature sources, the 'virtual' temperature
4139 * for each fan reflects a different temperature, and there
4140 * are no duplicates.
4142 if (!(data->virt_temp_mask & BIT(src))) {
4143 if (mask & BIT(src))
4148 /* Use fixed index for SYSTIN(1), CPUTIN(2), AUXTIN(3) */
4149 if (src <= data->temp_fixed_num) {
4150 if (data->have_temp & BIT(src - 1))
4152 data->have_temp |= BIT(src - 1);
4153 data->have_temp_fixed |= BIT(src - 1);
4154 data->reg_temp[0][src - 1] = reg_temp_mon[i];
4155 data->temp_src[src - 1] = src;
4162 /* Use dynamic index for other sources */
4163 data->have_temp |= BIT(s);
4164 data->reg_temp[0][s] = reg_temp_mon[i];
4165 data->temp_src[s] = src;
4169 #ifdef USE_ALTERNATE
4171 * Go through the list of alternate temp registers and enable
4173 * The temperature is already monitored if the respective bit in <mask>
4176 for (i = 0; i < 31; i++) {
4177 if (!(data->temp_mask & BIT(i + 1)))
4179 if (!reg_temp_alternate[i])
4181 if (mask & BIT(i + 1))
4183 if (i < data->temp_fixed_num) {
4184 if (data->have_temp & BIT(i))
4186 data->have_temp |= BIT(i);
4187 data->have_temp_fixed |= BIT(i);
4188 data->reg_temp[0][i] = reg_temp_alternate[i];
4189 if (i < num_reg_temp) {
4190 data->reg_temp[1][i] = reg_temp_over[i];
4191 data->reg_temp[2][i] = reg_temp_hyst[i];
4193 data->temp_src[i] = i + 1;
4197 if (s >= NUM_TEMP) /* Abort if no more space */
4200 data->have_temp |= BIT(s);
4201 data->reg_temp[0][s] = reg_temp_alternate[i];
4202 data->temp_src[s] = i + 1;
4205 #endif /* USE_ALTERNATE */
4207 /* Check which TSIx_TEMP registers are active */
4208 for (i = 0; i < num_reg_tsi_temp; i++) {
4211 err = nct6775_read_value(data, data->REG_TSI_TEMP[i], &tmp);
4215 data->have_tsi_temp |= BIT(i);
4218 /* Initialize the chip */
4219 err = nct6775_init_device(data);
4223 if (data->driver_init) {
4224 err = data->driver_init(data);
4229 /* Read fan clock dividers immediately */
4230 err = nct6775_init_fan_common(dev, data);
4234 /* Register sysfs hooks */
4235 err = nct6775_add_template_attr_group(dev, data, &nct6775_pwm_template_group,
4240 err = nct6775_add_template_attr_group(dev, data, &nct6775_in_template_group,
4241 fls(data->have_in));
4245 err = nct6775_add_template_attr_group(dev, data, &nct6775_fan_template_group,
4246 fls(data->has_fan));
4250 err = nct6775_add_template_attr_group(dev, data, &nct6775_temp_template_group,
4251 fls(data->have_temp));
4255 if (data->have_tsi_temp) {
4256 tsi_temp_tg.templates = nct6775_tsi_temp_template;
4257 tsi_temp_tg.is_visible = nct6775_tsi_temp_is_visible;
4258 tsi_temp_tg.base = fls(data->have_temp) + 1;
4259 err = nct6775_add_template_attr_group(dev, data, &tsi_temp_tg,
4260 fls(data->have_tsi_temp));
4265 hwmon_dev = devm_hwmon_device_register_with_groups(dev, data->name,
4266 data, data->groups);
4267 return PTR_ERR_OR_ZERO(hwmon_dev);
4269 EXPORT_SYMBOL_GPL(nct6775_probe);
4271 MODULE_AUTHOR("Guenter Roeck <linux@roeck-us.net>");
4272 MODULE_DESCRIPTION("Core driver for NCT6775F and compatible chips");
4273 MODULE_LICENSE("GPL");