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
8 * Derived from w83627ehf driver
10 * Copyright (C) 2006 Yuan Mu (Winbond),
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
37 * #temp lists the number of monitored temperature sources (first value) plus
38 * the number of directly connectable temperature sensors (second value).
41 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
43 #include <linux/module.h>
44 #include <linux/init.h>
45 #include <linux/slab.h>
46 #include <linux/jiffies.h>
47 #include <linux/hwmon.h>
48 #include <linux/hwmon-sysfs.h>
49 #include <linux/err.h>
50 #include <linux/mutex.h>
51 #include <linux/bitops.h>
52 #include <linux/nospec.h>
53 #include <linux/regmap.h>
57 #undef DEFAULT_SYMBOL_NAMESPACE
58 #define DEFAULT_SYMBOL_NAMESPACE HWMON_NCT6775
62 /* used to set data->name = nct6775_device_names[data->sio_kind] */
63 static const char * const nct6775_device_names[] = {
78 /* Common and NCT6775 specific data */
80 /* Voltage min/max registers for nr=7..14 are in bank 5 */
82 static const u16 NCT6775_REG_IN_MAX[] = {
83 0x2b, 0x2d, 0x2f, 0x31, 0x33, 0x35, 0x37, 0x554, 0x556, 0x558, 0x55a,
84 0x55c, 0x55e, 0x560, 0x562 };
85 static const u16 NCT6775_REG_IN_MIN[] = {
86 0x2c, 0x2e, 0x30, 0x32, 0x34, 0x36, 0x38, 0x555, 0x557, 0x559, 0x55b,
87 0x55d, 0x55f, 0x561, 0x563 };
88 static const u16 NCT6775_REG_IN[] = {
89 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x550, 0x551, 0x552
92 #define NCT6775_REG_VBAT 0x5D
93 #define NCT6775_REG_DIODE 0x5E
94 #define NCT6775_DIODE_MASK 0x02
96 static const u16 NCT6775_REG_ALARM[NUM_REG_ALARM] = { 0x459, 0x45A, 0x45B };
98 /* 0..15 voltages, 16..23 fans, 24..29 temperatures, 30..31 intrusion */
100 static const s8 NCT6775_ALARM_BITS[] = {
101 0, 1, 2, 3, 8, 21, 20, 16, /* in0.. in7 */
102 17, -1, -1, -1, -1, -1, -1, /* in8..in14 */
104 6, 7, 11, -1, -1, /* fan1..fan5 */
105 -1, -1, -1, /* unused */
106 4, 5, 13, -1, -1, -1, /* temp1..temp6 */
107 12, -1 }; /* intrusion0, intrusion1 */
109 static const u16 NCT6775_REG_BEEP[NUM_REG_BEEP] = { 0x56, 0x57, 0x453, 0x4e };
112 * 0..14 voltages, 15 global beep enable, 16..23 fans, 24..29 temperatures,
115 static const s8 NCT6775_BEEP_BITS[] = {
116 0, 1, 2, 3, 8, 9, 10, 16, /* in0.. in7 */
117 17, -1, -1, -1, -1, -1, -1, /* in8..in14 */
118 21, /* global beep enable */
119 6, 7, 11, 28, -1, /* fan1..fan5 */
120 -1, -1, -1, /* unused */
121 4, 5, 13, -1, -1, -1, /* temp1..temp6 */
122 12, -1 }; /* intrusion0, intrusion1 */
124 /* DC or PWM output fan configuration */
125 static const u8 NCT6775_REG_PWM_MODE[] = { 0x04, 0x04, 0x12 };
126 static const u8 NCT6775_PWM_MODE_MASK[] = { 0x01, 0x02, 0x01 };
128 /* Advanced Fan control, some values are common for all fans */
130 static const u16 NCT6775_REG_TARGET[] = {
131 0x101, 0x201, 0x301, 0x801, 0x901, 0xa01, 0xb01 };
132 static const u16 NCT6775_REG_FAN_MODE[] = {
133 0x102, 0x202, 0x302, 0x802, 0x902, 0xa02, 0xb02 };
134 static const u16 NCT6775_REG_FAN_STEP_DOWN_TIME[] = {
135 0x103, 0x203, 0x303, 0x803, 0x903, 0xa03, 0xb03 };
136 static const u16 NCT6775_REG_FAN_STEP_UP_TIME[] = {
137 0x104, 0x204, 0x304, 0x804, 0x904, 0xa04, 0xb04 };
138 static const u16 NCT6775_REG_FAN_STOP_OUTPUT[] = {
139 0x105, 0x205, 0x305, 0x805, 0x905, 0xa05, 0xb05 };
140 static const u16 NCT6775_REG_FAN_START_OUTPUT[] = {
141 0x106, 0x206, 0x306, 0x806, 0x906, 0xa06, 0xb06 };
142 static const u16 NCT6775_REG_FAN_MAX_OUTPUT[] = { 0x10a, 0x20a, 0x30a };
143 static const u16 NCT6775_REG_FAN_STEP_OUTPUT[] = { 0x10b, 0x20b, 0x30b };
145 static const u16 NCT6775_REG_FAN_STOP_TIME[] = {
146 0x107, 0x207, 0x307, 0x807, 0x907, 0xa07, 0xb07 };
147 static const u16 NCT6775_REG_PWM[] = {
148 0x109, 0x209, 0x309, 0x809, 0x909, 0xa09, 0xb09 };
149 static const u16 NCT6775_REG_PWM_READ[] = {
150 0x01, 0x03, 0x11, 0x13, 0x15, 0xa09, 0xb09 };
152 static const u16 NCT6775_REG_FAN[] = { 0x630, 0x632, 0x634, 0x636, 0x638 };
153 static const u16 NCT6775_REG_FAN_MIN[] = { 0x3b, 0x3c, 0x3d };
154 static const u16 NCT6775_REG_FAN_PULSES[NUM_FAN] = {
155 0x641, 0x642, 0x643, 0x644 };
156 static const u16 NCT6775_FAN_PULSE_SHIFT[NUM_FAN] = { };
158 static const u16 NCT6775_REG_TEMP[] = {
159 0x27, 0x150, 0x250, 0x62b, 0x62c, 0x62d };
161 static const u16 NCT6775_REG_TEMP_MON[] = { 0x73, 0x75, 0x77 };
163 static const u16 NCT6775_REG_TEMP_CONFIG[ARRAY_SIZE(NCT6775_REG_TEMP)] = {
164 0, 0x152, 0x252, 0x628, 0x629, 0x62A };
165 static const u16 NCT6775_REG_TEMP_HYST[ARRAY_SIZE(NCT6775_REG_TEMP)] = {
166 0x3a, 0x153, 0x253, 0x673, 0x678, 0x67D };
167 static const u16 NCT6775_REG_TEMP_OVER[ARRAY_SIZE(NCT6775_REG_TEMP)] = {
168 0x39, 0x155, 0x255, 0x672, 0x677, 0x67C };
170 static const u16 NCT6775_REG_TEMP_SOURCE[ARRAY_SIZE(NCT6775_REG_TEMP)] = {
171 0x621, 0x622, 0x623, 0x624, 0x625, 0x626 };
173 static const u16 NCT6775_REG_TEMP_SEL[] = {
174 0x100, 0x200, 0x300, 0x800, 0x900, 0xa00, 0xb00 };
176 static const u16 NCT6775_REG_WEIGHT_TEMP_SEL[] = {
177 0x139, 0x239, 0x339, 0x839, 0x939, 0xa39 };
178 static const u16 NCT6775_REG_WEIGHT_TEMP_STEP[] = {
179 0x13a, 0x23a, 0x33a, 0x83a, 0x93a, 0xa3a };
180 static const u16 NCT6775_REG_WEIGHT_TEMP_STEP_TOL[] = {
181 0x13b, 0x23b, 0x33b, 0x83b, 0x93b, 0xa3b };
182 static const u16 NCT6775_REG_WEIGHT_DUTY_STEP[] = {
183 0x13c, 0x23c, 0x33c, 0x83c, 0x93c, 0xa3c };
184 static const u16 NCT6775_REG_WEIGHT_TEMP_BASE[] = {
185 0x13d, 0x23d, 0x33d, 0x83d, 0x93d, 0xa3d };
187 static const u16 NCT6775_REG_TEMP_OFFSET[] = { 0x454, 0x455, 0x456 };
189 static const u16 NCT6775_REG_AUTO_TEMP[] = {
190 0x121, 0x221, 0x321, 0x821, 0x921, 0xa21, 0xb21 };
191 static const u16 NCT6775_REG_AUTO_PWM[] = {
192 0x127, 0x227, 0x327, 0x827, 0x927, 0xa27, 0xb27 };
194 #define NCT6775_AUTO_TEMP(data, nr, p) ((data)->REG_AUTO_TEMP[nr] + (p))
195 #define NCT6775_AUTO_PWM(data, nr, p) ((data)->REG_AUTO_PWM[nr] + (p))
197 static const u16 NCT6775_REG_CRITICAL_ENAB[] = { 0x134, 0x234, 0x334 };
199 static const u16 NCT6775_REG_CRITICAL_TEMP[] = {
200 0x135, 0x235, 0x335, 0x835, 0x935, 0xa35, 0xb35 };
201 static const u16 NCT6775_REG_CRITICAL_TEMP_TOLERANCE[] = {
202 0x138, 0x238, 0x338, 0x838, 0x938, 0xa38, 0xb38 };
204 static const char *const nct6775_temp_label[] = {
218 "PCH_CHIP_CPU_MAX_TEMP",
228 #define NCT6775_TEMP_MASK 0x001ffffe
229 #define NCT6775_VIRT_TEMP_MASK 0x00000000
231 static const u16 NCT6775_REG_TEMP_ALTERNATE[32] = {
237 static const u16 NCT6775_REG_TEMP_CRIT[32] = {
248 static const u16 NCT6775_REG_TSI_TEMP[] = { 0x669 };
250 /* NCT6776 specific data */
252 /* STEP_UP_TIME and STEP_DOWN_TIME regs are swapped for all chips but NCT6775 */
253 #define NCT6776_REG_FAN_STEP_UP_TIME NCT6775_REG_FAN_STEP_DOWN_TIME
254 #define NCT6776_REG_FAN_STEP_DOWN_TIME NCT6775_REG_FAN_STEP_UP_TIME
256 static const s8 NCT6776_ALARM_BITS[] = {
257 0, 1, 2, 3, 8, 21, 20, 16, /* in0.. in7 */
258 17, -1, -1, -1, -1, -1, -1, /* in8..in14 */
260 6, 7, 11, 10, 23, /* fan1..fan5 */
261 -1, -1, -1, /* unused */
262 4, 5, 13, -1, -1, -1, /* temp1..temp6 */
263 12, 9 }; /* intrusion0, intrusion1 */
265 static const u16 NCT6776_REG_BEEP[NUM_REG_BEEP] = { 0xb2, 0xb3, 0xb4, 0xb5 };
267 static const s8 NCT6776_BEEP_BITS[] = {
268 0, 1, 2, 3, 4, 5, 6, 7, /* in0.. in7 */
269 8, -1, -1, -1, -1, -1, -1, /* in8..in14 */
270 24, /* global beep enable */
271 25, 26, 27, 28, 29, /* fan1..fan5 */
272 -1, -1, -1, /* unused */
273 16, 17, 18, 19, 20, 21, /* temp1..temp6 */
274 30, 31 }; /* intrusion0, intrusion1 */
276 static const u16 NCT6776_REG_TOLERANCE_H[] = {
277 0x10c, 0x20c, 0x30c, 0x80c, 0x90c, 0xa0c, 0xb0c };
279 static const u8 NCT6776_REG_PWM_MODE[] = { 0x04, 0, 0, 0, 0, 0 };
280 static const u8 NCT6776_PWM_MODE_MASK[] = { 0x01, 0, 0, 0, 0, 0 };
282 static const u16 NCT6776_REG_FAN_MIN[] = {
283 0x63a, 0x63c, 0x63e, 0x640, 0x642, 0x64a, 0x64c };
284 static const u16 NCT6776_REG_FAN_PULSES[NUM_FAN] = {
285 0x644, 0x645, 0x646, 0x647, 0x648, 0x649 };
287 static const u16 NCT6776_REG_WEIGHT_DUTY_BASE[] = {
288 0x13e, 0x23e, 0x33e, 0x83e, 0x93e, 0xa3e };
290 static const u16 NCT6776_REG_TEMP_CONFIG[ARRAY_SIZE(NCT6775_REG_TEMP)] = {
291 0x18, 0x152, 0x252, 0x628, 0x629, 0x62A };
293 static const char *const nct6776_temp_label[] = {
308 "PCH_CHIP_CPU_MAX_TEMP",
319 #define NCT6776_TEMP_MASK 0x007ffffe
320 #define NCT6776_VIRT_TEMP_MASK 0x00000000
322 static const u16 NCT6776_REG_TEMP_ALTERNATE[32] = {
328 static const u16 NCT6776_REG_TEMP_CRIT[32] = {
333 static const u16 NCT6776_REG_TSI_TEMP[] = {
334 0x409, 0x40b, 0x40d, 0x40f, 0x411, 0x413, 0x415, 0x417 };
336 /* NCT6779 specific data */
338 static const u16 NCT6779_REG_IN[] = {
339 0x480, 0x481, 0x482, 0x483, 0x484, 0x485, 0x486, 0x487,
340 0x488, 0x489, 0x48a, 0x48b, 0x48c, 0x48d, 0x48e };
342 static const u16 NCT6779_REG_ALARM[NUM_REG_ALARM] = {
343 0x459, 0x45A, 0x45B, 0x568 };
345 static const s8 NCT6779_ALARM_BITS[] = {
346 0, 1, 2, 3, 8, 21, 20, 16, /* in0.. in7 */
347 17, 24, 25, 26, 27, 28, 29, /* in8..in14 */
349 6, 7, 11, 10, 23, /* fan1..fan5 */
350 -1, -1, -1, /* unused */
351 4, 5, 13, -1, -1, -1, /* temp1..temp6 */
352 12, 9 }; /* intrusion0, intrusion1 */
354 static const s8 NCT6779_BEEP_BITS[] = {
355 0, 1, 2, 3, 4, 5, 6, 7, /* in0.. in7 */
356 8, 9, 10, 11, 12, 13, 14, /* in8..in14 */
357 24, /* global beep enable */
358 25, 26, 27, 28, 29, /* fan1..fan5 */
359 -1, -1, -1, /* unused */
360 16, 17, -1, -1, -1, -1, /* temp1..temp6 */
361 30, 31 }; /* intrusion0, intrusion1 */
363 static const u16 NCT6779_REG_FAN[] = {
364 0x4c0, 0x4c2, 0x4c4, 0x4c6, 0x4c8, 0x4ca, 0x4ce };
365 static const u16 NCT6779_REG_FAN_PULSES[NUM_FAN] = {
366 0x644, 0x645, 0x646, 0x647, 0x648, 0x649, 0x64f };
368 static const u16 NCT6779_REG_CRITICAL_PWM_ENABLE[] = {
369 0x136, 0x236, 0x336, 0x836, 0x936, 0xa36, 0xb36 };
370 #define NCT6779_CRITICAL_PWM_ENABLE_MASK 0x01
371 static const u16 NCT6779_REG_CRITICAL_PWM[] = {
372 0x137, 0x237, 0x337, 0x837, 0x937, 0xa37, 0xb37 };
374 static const u16 NCT6779_REG_TEMP[] = { 0x27, 0x150 };
375 static const u16 NCT6779_REG_TEMP_MON[] = { 0x73, 0x75, 0x77, 0x79, 0x7b };
376 static const u16 NCT6779_REG_TEMP_CONFIG[ARRAY_SIZE(NCT6779_REG_TEMP)] = {
378 static const u16 NCT6779_REG_TEMP_HYST[ARRAY_SIZE(NCT6779_REG_TEMP)] = {
380 static const u16 NCT6779_REG_TEMP_OVER[ARRAY_SIZE(NCT6779_REG_TEMP)] = {
383 static const u16 NCT6779_REG_TEMP_OFFSET[] = {
384 0x454, 0x455, 0x456, 0x44a, 0x44b, 0x44c };
386 static const char *const nct6779_temp_label[] = {
405 "PCH_CHIP_CPU_MAX_TEMP",
421 #define NCT6779_TEMP_MASK 0x07ffff7e
422 #define NCT6779_VIRT_TEMP_MASK 0x00000000
423 #define NCT6791_TEMP_MASK 0x87ffff7e
424 #define NCT6791_VIRT_TEMP_MASK 0x80000000
426 static const u16 NCT6779_REG_TEMP_ALTERNATE[32]
427 = { 0x490, 0x491, 0x492, 0x493, 0x494, 0x495, 0, 0,
428 0, 0, 0, 0, 0, 0, 0, 0,
429 0, 0x400, 0x401, 0x402, 0x404, 0x405, 0x406, 0x407,
432 static const u16 NCT6779_REG_TEMP_CRIT[32] = {
437 /* NCT6791 specific data */
439 static const u16 NCT6791_REG_WEIGHT_TEMP_SEL[NUM_FAN] = { 0, 0x239 };
440 static const u16 NCT6791_REG_WEIGHT_TEMP_STEP[NUM_FAN] = { 0, 0x23a };
441 static const u16 NCT6791_REG_WEIGHT_TEMP_STEP_TOL[NUM_FAN] = { 0, 0x23b };
442 static const u16 NCT6791_REG_WEIGHT_DUTY_STEP[NUM_FAN] = { 0, 0x23c };
443 static const u16 NCT6791_REG_WEIGHT_TEMP_BASE[NUM_FAN] = { 0, 0x23d };
444 static const u16 NCT6791_REG_WEIGHT_DUTY_BASE[NUM_FAN] = { 0, 0x23e };
446 static const u16 NCT6791_REG_ALARM[NUM_REG_ALARM] = {
447 0x459, 0x45A, 0x45B, 0x568, 0x45D };
449 static const s8 NCT6791_ALARM_BITS[] = {
450 0, 1, 2, 3, 8, 21, 20, 16, /* in0.. in7 */
451 17, 24, 25, 26, 27, 28, 29, /* in8..in14 */
453 6, 7, 11, 10, 23, 33, /* fan1..fan6 */
455 4, 5, 13, -1, -1, -1, /* temp1..temp6 */
456 12, 9 }; /* intrusion0, intrusion1 */
458 /* NCT6792/NCT6793 specific data */
460 static const u16 NCT6792_REG_TEMP_MON[] = {
461 0x73, 0x75, 0x77, 0x79, 0x7b, 0x7d };
462 static const u16 NCT6792_REG_BEEP[NUM_REG_BEEP] = {
463 0xb2, 0xb3, 0xb4, 0xb5, 0xbf };
465 static const char *const nct6792_temp_label[] = {
484 "PCH_CHIP_CPU_MAX_TEMP",
493 "PECI Agent 0 Calibration",
494 "PECI Agent 1 Calibration",
500 #define NCT6792_TEMP_MASK 0x9fffff7e
501 #define NCT6792_VIRT_TEMP_MASK 0x80000000
503 static const char *const nct6793_temp_label[] = {
522 "PCH_CHIP_CPU_MAX_TEMP",
532 "PECI Agent 0 Calibration",
533 "PECI Agent 1 Calibration",
538 #define NCT6793_TEMP_MASK 0xbfff037e
539 #define NCT6793_VIRT_TEMP_MASK 0x80000000
541 static const char *const nct6795_temp_label[] = {
560 "PCH_CHIP_CPU_MAX_TEMP",
570 "PECI Agent 0 Calibration",
571 "PECI Agent 1 Calibration",
576 #define NCT6795_TEMP_MASK 0xbfffff7e
577 #define NCT6795_VIRT_TEMP_MASK 0x80000000
579 static const char *const nct6796_temp_label[] = {
598 "PCH_CHIP_CPU_MAX_TEMP",
608 "PECI Agent 0 Calibration",
609 "PECI Agent 1 Calibration",
614 #define NCT6796_TEMP_MASK 0xbfff0ffe
615 #define NCT6796_VIRT_TEMP_MASK 0x80000c00
617 static const u16 NCT6796_REG_TSI_TEMP[] = { 0x409, 0x40b };
619 static const char *const nct6798_temp_label[] = {
638 "PCH_CHIP_CPU_MAX_TEMP",
648 "PECI Agent 0 Calibration", /* undocumented */
649 "PECI Agent 1 Calibration", /* undocumented */
654 #define NCT6798_TEMP_MASK 0xbfff0ffe
655 #define NCT6798_VIRT_TEMP_MASK 0x80000c00
657 /* NCT6102D/NCT6106D specific data */
659 #define NCT6106_REG_VBAT 0x318
660 #define NCT6106_REG_DIODE 0x319
661 #define NCT6106_DIODE_MASK 0x01
663 static const u16 NCT6106_REG_IN_MAX[] = {
664 0x90, 0x92, 0x94, 0x96, 0x98, 0x9a, 0x9e, 0xa0, 0xa2 };
665 static const u16 NCT6106_REG_IN_MIN[] = {
666 0x91, 0x93, 0x95, 0x97, 0x99, 0x9b, 0x9f, 0xa1, 0xa3 };
667 static const u16 NCT6106_REG_IN[] = {
668 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x07, 0x08, 0x09 };
670 static const u16 NCT6106_REG_TEMP[] = { 0x10, 0x11, 0x12, 0x13, 0x14, 0x15 };
671 static const u16 NCT6106_REG_TEMP_MON[] = { 0x18, 0x19, 0x1a };
672 static const u16 NCT6106_REG_TEMP_HYST[] = {
673 0xc3, 0xc7, 0xcb, 0xcf, 0xd3, 0xd7 };
674 static const u16 NCT6106_REG_TEMP_OVER[] = {
675 0xc2, 0xc6, 0xca, 0xce, 0xd2, 0xd6 };
676 static const u16 NCT6106_REG_TEMP_CRIT_L[] = {
677 0xc0, 0xc4, 0xc8, 0xcc, 0xd0, 0xd4 };
678 static const u16 NCT6106_REG_TEMP_CRIT_H[] = {
679 0xc1, 0xc5, 0xc9, 0xcf, 0xd1, 0xd5 };
680 static const u16 NCT6106_REG_TEMP_OFFSET[] = { 0x311, 0x312, 0x313 };
681 static const u16 NCT6106_REG_TEMP_CONFIG[] = {
682 0xb7, 0xb8, 0xb9, 0xba, 0xbb, 0xbc };
684 static const u16 NCT6106_REG_FAN[] = { 0x20, 0x22, 0x24 };
685 static const u16 NCT6106_REG_FAN_MIN[] = { 0xe0, 0xe2, 0xe4 };
686 static const u16 NCT6106_REG_FAN_PULSES[] = { 0xf6, 0xf6, 0xf6 };
687 static const u16 NCT6106_FAN_PULSE_SHIFT[] = { 0, 2, 4 };
689 static const u8 NCT6106_REG_PWM_MODE[] = { 0xf3, 0xf3, 0xf3 };
690 static const u8 NCT6106_PWM_MODE_MASK[] = { 0x01, 0x02, 0x04 };
691 static const u16 NCT6106_REG_PWM_READ[] = { 0x4a, 0x4b, 0x4c };
692 static const u16 NCT6106_REG_FAN_MODE[] = { 0x113, 0x123, 0x133 };
693 static const u16 NCT6106_REG_TEMP_SOURCE[] = {
694 0xb0, 0xb1, 0xb2, 0xb3, 0xb4, 0xb5 };
696 static const u16 NCT6106_REG_CRITICAL_TEMP[] = { 0x11a, 0x12a, 0x13a };
697 static const u16 NCT6106_REG_CRITICAL_TEMP_TOLERANCE[] = {
698 0x11b, 0x12b, 0x13b };
700 static const u16 NCT6106_REG_CRITICAL_PWM_ENABLE[] = { 0x11c, 0x12c, 0x13c };
701 #define NCT6106_CRITICAL_PWM_ENABLE_MASK 0x10
702 static const u16 NCT6106_REG_CRITICAL_PWM[] = { 0x11d, 0x12d, 0x13d };
704 static const u16 NCT6106_REG_FAN_STEP_UP_TIME[] = { 0x114, 0x124, 0x134 };
705 static const u16 NCT6106_REG_FAN_STEP_DOWN_TIME[] = { 0x115, 0x125, 0x135 };
706 static const u16 NCT6106_REG_FAN_STOP_OUTPUT[] = { 0x116, 0x126, 0x136 };
707 static const u16 NCT6106_REG_FAN_START_OUTPUT[] = { 0x117, 0x127, 0x137 };
708 static const u16 NCT6106_REG_FAN_STOP_TIME[] = { 0x118, 0x128, 0x138 };
709 static const u16 NCT6106_REG_TOLERANCE_H[] = { 0x112, 0x122, 0x132 };
711 static const u16 NCT6106_REG_TARGET[] = { 0x111, 0x121, 0x131 };
713 static const u16 NCT6106_REG_WEIGHT_TEMP_SEL[] = { 0x168, 0x178, 0x188 };
714 static const u16 NCT6106_REG_WEIGHT_TEMP_STEP[] = { 0x169, 0x179, 0x189 };
715 static const u16 NCT6106_REG_WEIGHT_TEMP_STEP_TOL[] = { 0x16a, 0x17a, 0x18a };
716 static const u16 NCT6106_REG_WEIGHT_DUTY_STEP[] = { 0x16b, 0x17b, 0x18b };
717 static const u16 NCT6106_REG_WEIGHT_TEMP_BASE[] = { 0x16c, 0x17c, 0x18c };
718 static const u16 NCT6106_REG_WEIGHT_DUTY_BASE[] = { 0x16d, 0x17d, 0x18d };
720 static const u16 NCT6106_REG_AUTO_TEMP[] = { 0x160, 0x170, 0x180 };
721 static const u16 NCT6106_REG_AUTO_PWM[] = { 0x164, 0x174, 0x184 };
723 static const u16 NCT6106_REG_ALARM[NUM_REG_ALARM] = {
724 0x77, 0x78, 0x79, 0x7a, 0x7b, 0x7c, 0x7d };
726 static const s8 NCT6106_ALARM_BITS[] = {
727 0, 1, 2, 3, 4, 5, 7, 8, /* in0.. in7 */
728 9, -1, -1, -1, -1, -1, -1, /* in8..in14 */
730 32, 33, 34, -1, -1, /* fan1..fan5 */
731 -1, -1, -1, /* unused */
732 16, 17, 18, 19, 20, 21, /* temp1..temp6 */
733 48, -1 /* intrusion0, intrusion1 */
736 static const u16 NCT6106_REG_BEEP[NUM_REG_BEEP] = {
737 0x3c0, 0x3c1, 0x3c2, 0x3c3, 0x3c4 };
739 static const s8 NCT6106_BEEP_BITS[] = {
740 0, 1, 2, 3, 4, 5, 7, 8, /* in0.. in7 */
741 9, 10, 11, 12, -1, -1, -1, /* in8..in14 */
742 32, /* global beep enable */
743 24, 25, 26, 27, 28, /* fan1..fan5 */
744 -1, -1, -1, /* unused */
745 16, 17, 18, 19, 20, 21, /* temp1..temp6 */
746 34, -1 /* intrusion0, intrusion1 */
749 static const u16 NCT6106_REG_TEMP_ALTERNATE[32] = {
755 static const u16 NCT6106_REG_TEMP_CRIT[32] = {
760 static const u16 NCT6106_REG_TSI_TEMP[] = { 0x59, 0x5b, 0x5d, 0x5f, 0x61, 0x63, 0x65, 0x67 };
762 /* NCT6112D/NCT6114D/NCT6116D specific data */
764 static const u16 NCT6116_REG_FAN[] = { 0x20, 0x22, 0x24, 0x26, 0x28 };
765 static const u16 NCT6116_REG_FAN_MIN[] = { 0xe0, 0xe2, 0xe4, 0xe6, 0xe8 };
766 static const u16 NCT6116_REG_FAN_PULSES[] = { 0xf6, 0xf6, 0xf6, 0xf6, 0xf5 };
767 static const u16 NCT6116_FAN_PULSE_SHIFT[] = { 0, 2, 4, 6, 6 };
769 static const u16 NCT6116_REG_PWM[] = { 0x119, 0x129, 0x139, 0x199, 0x1a9 };
770 static const u16 NCT6116_REG_FAN_MODE[] = { 0x113, 0x123, 0x133, 0x193, 0x1a3 };
771 static const u16 NCT6116_REG_TEMP_SEL[] = { 0x110, 0x120, 0x130, 0x190, 0x1a0 };
772 static const u16 NCT6116_REG_TEMP_SOURCE[] = {
775 static const u16 NCT6116_REG_CRITICAL_TEMP[] = {
776 0x11a, 0x12a, 0x13a, 0x19a, 0x1aa };
777 static const u16 NCT6116_REG_CRITICAL_TEMP_TOLERANCE[] = {
778 0x11b, 0x12b, 0x13b, 0x19b, 0x1ab };
780 static const u16 NCT6116_REG_CRITICAL_PWM_ENABLE[] = {
781 0x11c, 0x12c, 0x13c, 0x19c, 0x1ac };
782 static const u16 NCT6116_REG_CRITICAL_PWM[] = {
783 0x11d, 0x12d, 0x13d, 0x19d, 0x1ad };
785 static const u16 NCT6116_REG_FAN_STEP_UP_TIME[] = {
786 0x114, 0x124, 0x134, 0x194, 0x1a4 };
787 static const u16 NCT6116_REG_FAN_STEP_DOWN_TIME[] = {
788 0x115, 0x125, 0x135, 0x195, 0x1a5 };
789 static const u16 NCT6116_REG_FAN_STOP_OUTPUT[] = {
790 0x116, 0x126, 0x136, 0x196, 0x1a6 };
791 static const u16 NCT6116_REG_FAN_START_OUTPUT[] = {
792 0x117, 0x127, 0x137, 0x197, 0x1a7 };
793 static const u16 NCT6116_REG_FAN_STOP_TIME[] = {
794 0x118, 0x128, 0x138, 0x198, 0x1a8 };
795 static const u16 NCT6116_REG_TOLERANCE_H[] = {
796 0x112, 0x122, 0x132, 0x192, 0x1a2 };
798 static const u16 NCT6116_REG_TARGET[] = {
799 0x111, 0x121, 0x131, 0x191, 0x1a1 };
801 static const u16 NCT6116_REG_AUTO_TEMP[] = {
802 0x160, 0x170, 0x180, 0x1d0, 0x1e0 };
803 static const u16 NCT6116_REG_AUTO_PWM[] = {
804 0x164, 0x174, 0x184, 0x1d4, 0x1e4 };
806 static const s8 NCT6116_ALARM_BITS[] = {
807 0, 1, 2, 3, 4, 5, 7, 8, /* in0.. in7 */
808 9, -1, -1, -1, -1, -1, -1, /* in8..in9 */
810 32, 33, 34, 35, 36, /* fan1..fan5 */
811 -1, -1, -1, /* unused */
812 16, 17, 18, -1, -1, -1, /* temp1..temp6 */
813 48, -1 /* intrusion0, intrusion1 */
816 static const s8 NCT6116_BEEP_BITS[] = {
817 0, 1, 2, 3, 4, 5, 7, 8, /* in0.. in7 */
818 9, 10, 11, 12, -1, -1, -1, /* in8..in14 */
819 32, /* global beep enable */
820 24, 25, 26, 27, 28, /* fan1..fan5 */
821 -1, -1, -1, /* unused */
822 16, 17, 18, -1, -1, -1, /* temp1..temp6 */
823 34, -1 /* intrusion0, intrusion1 */
826 static const u16 NCT6116_REG_TSI_TEMP[] = { 0x59, 0x5b };
828 static enum pwm_enable reg_to_pwm_enable(int pwm, int mode)
830 if (mode == 0 && pwm == 255)
835 static int pwm_enable_to_reg(enum pwm_enable mode)
846 /* 1 is DC mode, output in ms */
847 static unsigned int step_time_from_reg(u8 reg, u8 mode)
849 return mode ? 400 * reg : 100 * reg;
852 static u8 step_time_to_reg(unsigned int msec, u8 mode)
854 return clamp_val((mode ? (msec + 200) / 400 :
855 (msec + 50) / 100), 1, 255);
858 static unsigned int fan_from_reg8(u16 reg, unsigned int divreg)
860 if (reg == 0 || reg == 255)
862 return 1350000U / (reg << divreg);
865 static unsigned int fan_from_reg13(u16 reg, unsigned int divreg)
867 if ((reg & 0xff1f) == 0xff1f)
870 reg = (reg & 0x1f) | ((reg & 0xff00) >> 3);
875 return 1350000U / reg;
878 static unsigned int fan_from_reg16(u16 reg, unsigned int divreg)
880 if (reg == 0 || reg == 0xffff)
884 * Even though the registers are 16 bit wide, the fan divisor
887 return 1350000U / (reg << divreg);
890 static unsigned int fan_from_reg_rpm(u16 reg, unsigned int divreg)
895 static u16 fan_to_reg(u32 fan, unsigned int divreg)
900 return (1350000U / fan) >> divreg;
903 static inline unsigned int
910 * Some of the voltage inputs have internal scaling, the tables below
911 * contain 8 (the ADC LSB in mV) * scaling factor * 100
913 static const u16 scale_in[15] = {
914 800, 800, 1600, 1600, 800, 800, 800, 1600, 1600, 800, 800, 800, 800,
918 static inline long in_from_reg(u8 reg, u8 nr)
920 return DIV_ROUND_CLOSEST(reg * scale_in[nr], 100);
923 static inline u8 in_to_reg(u32 val, u8 nr)
925 return clamp_val(DIV_ROUND_CLOSEST(val * 100, scale_in[nr]), 0, 255);
928 /* TSI temperatures are in 8.3 format */
929 static inline unsigned int tsi_temp_from_reg(unsigned int reg)
931 return (reg >> 5) * 125;
935 * Data structures and manipulation thereof
938 struct sensor_device_template {
939 struct device_attribute dev_attr;
947 bool s2; /* true if both index and nr are used */
950 struct sensor_device_attr_u {
952 struct sensor_device_attribute a1;
953 struct sensor_device_attribute_2 a2;
958 #define __TEMPLATE_ATTR(_template, _mode, _show, _store) { \
959 .attr = {.name = _template, .mode = _mode }, \
964 #define SENSOR_DEVICE_TEMPLATE(_template, _mode, _show, _store, _index) \
965 { .dev_attr = __TEMPLATE_ATTR(_template, _mode, _show, _store), \
969 #define SENSOR_DEVICE_TEMPLATE_2(_template, _mode, _show, _store, \
971 { .dev_attr = __TEMPLATE_ATTR(_template, _mode, _show, _store), \
972 .u.s.index = _index, \
976 #define SENSOR_TEMPLATE(_name, _template, _mode, _show, _store, _index) \
977 static struct sensor_device_template sensor_dev_template_##_name \
978 = SENSOR_DEVICE_TEMPLATE(_template, _mode, _show, _store, \
981 #define SENSOR_TEMPLATE_2(_name, _template, _mode, _show, _store, \
983 static struct sensor_device_template sensor_dev_template_##_name \
984 = SENSOR_DEVICE_TEMPLATE_2(_template, _mode, _show, _store, \
987 struct sensor_template_group {
988 struct sensor_device_template **templates;
989 umode_t (*is_visible)(struct kobject *, struct attribute *, int);
993 static int nct6775_add_template_attr_group(struct device *dev, struct nct6775_data *data,
994 const struct sensor_template_group *tg, int repeat)
996 struct attribute_group *group;
997 struct sensor_device_attr_u *su;
998 struct sensor_device_attribute *a;
999 struct sensor_device_attribute_2 *a2;
1000 struct attribute **attrs;
1001 struct sensor_device_template **t;
1008 for (count = 0; *t; t++, count++)
1014 group = devm_kzalloc(dev, sizeof(*group), GFP_KERNEL);
1018 attrs = devm_kcalloc(dev, repeat * count + 1, sizeof(*attrs),
1023 su = devm_kzalloc(dev, array3_size(repeat, count, sizeof(*su)),
1028 group->attrs = attrs;
1029 group->is_visible = tg->is_visible;
1031 for (i = 0; i < repeat; i++) {
1033 while (*t != NULL) {
1034 snprintf(su->name, sizeof(su->name),
1035 (*t)->dev_attr.attr.name, tg->base + i);
1038 sysfs_attr_init(&a2->dev_attr.attr);
1039 a2->dev_attr.attr.name = su->name;
1040 a2->nr = (*t)->u.s.nr + i;
1041 a2->index = (*t)->u.s.index;
1042 a2->dev_attr.attr.mode =
1043 (*t)->dev_attr.attr.mode;
1044 a2->dev_attr.show = (*t)->dev_attr.show;
1045 a2->dev_attr.store = (*t)->dev_attr.store;
1046 *attrs = &a2->dev_attr.attr;
1049 sysfs_attr_init(&a->dev_attr.attr);
1050 a->dev_attr.attr.name = su->name;
1051 a->index = (*t)->u.index + i;
1052 a->dev_attr.attr.mode =
1053 (*t)->dev_attr.attr.mode;
1054 a->dev_attr.show = (*t)->dev_attr.show;
1055 a->dev_attr.store = (*t)->dev_attr.store;
1056 *attrs = &a->dev_attr.attr;
1064 return nct6775_add_attr_group(data, group);
1067 bool nct6775_reg_is_word_sized(struct nct6775_data *data, u16 reg)
1069 switch (data->kind) {
1071 return reg == 0x20 || reg == 0x22 || reg == 0x24 ||
1072 (reg >= 0x59 && reg < 0x69 && (reg & 1)) ||
1073 reg == 0xe0 || reg == 0xe2 || reg == 0xe4 ||
1074 reg == 0x111 || reg == 0x121 || reg == 0x131;
1076 return reg == 0x20 || reg == 0x22 || reg == 0x24 ||
1077 reg == 0x26 || reg == 0x28 || reg == 0x59 || reg == 0x5b ||
1078 reg == 0xe0 || reg == 0xe2 || reg == 0xe4 || reg == 0xe6 ||
1079 reg == 0xe8 || reg == 0x111 || reg == 0x121 || reg == 0x131 ||
1080 reg == 0x191 || reg == 0x1a1;
1082 return (((reg & 0xff00) == 0x100 ||
1083 (reg & 0xff00) == 0x200) &&
1084 ((reg & 0x00ff) == 0x50 ||
1085 (reg & 0x00ff) == 0x53 ||
1086 (reg & 0x00ff) == 0x55)) ||
1087 (reg & 0xfff0) == 0x630 ||
1088 reg == 0x640 || reg == 0x642 ||
1089 reg == 0x662 || reg == 0x669 ||
1090 ((reg & 0xfff0) == 0x650 && (reg & 0x000f) >= 0x06) ||
1091 reg == 0x73 || reg == 0x75 || reg == 0x77;
1093 return (((reg & 0xff00) == 0x100 ||
1094 (reg & 0xff00) == 0x200) &&
1095 ((reg & 0x00ff) == 0x50 ||
1096 (reg & 0x00ff) == 0x53 ||
1097 (reg & 0x00ff) == 0x55)) ||
1098 (reg & 0xfff0) == 0x630 ||
1100 (reg >= 0x409 && reg < 0x419 && (reg & 1)) ||
1101 reg == 0x640 || reg == 0x642 ||
1102 ((reg & 0xfff0) == 0x650 && (reg & 0x000f) >= 0x06) ||
1103 reg == 0x73 || reg == 0x75 || reg == 0x77;
1112 return reg == 0x150 || reg == 0x153 || reg == 0x155 ||
1113 (reg & 0xfff0) == 0x4c0 ||
1115 (reg >= 0x409 && reg < 0x419 && (reg & 1)) ||
1116 reg == 0x63a || reg == 0x63c || reg == 0x63e ||
1117 reg == 0x640 || reg == 0x642 || reg == 0x64a ||
1119 reg == 0x73 || reg == 0x75 || reg == 0x77 || reg == 0x79 ||
1120 reg == 0x7b || reg == 0x7d;
1124 EXPORT_SYMBOL_GPL(nct6775_reg_is_word_sized);
1126 /* We left-align 8-bit temperature values to make the code simpler */
1127 static int nct6775_read_temp(struct nct6775_data *data, u16 reg, u16 *val)
1131 err = nct6775_read_value(data, reg, val);
1135 if (!nct6775_reg_is_word_sized(data, reg))
1141 /* This function assumes that the caller holds data->update_lock */
1142 static int nct6775_write_fan_div(struct nct6775_data *data, int nr)
1146 u16 fandiv_reg = nr < 2 ? NCT6775_REG_FANDIV1 : NCT6775_REG_FANDIV2;
1147 unsigned int oddshift = (nr & 1) * 4; /* masks shift by four if nr is odd */
1149 err = nct6775_read_value(data, fandiv_reg, ®);
1152 reg &= 0x70 >> oddshift;
1153 reg |= data->fan_div[nr] & (0x7 << oddshift);
1154 return nct6775_write_value(data, fandiv_reg, reg);
1157 static int nct6775_write_fan_div_common(struct nct6775_data *data, int nr)
1159 if (data->kind == nct6775)
1160 return nct6775_write_fan_div(data, nr);
1164 static int nct6775_update_fan_div(struct nct6775_data *data)
1169 err = nct6775_read_value(data, NCT6775_REG_FANDIV1, &i);
1172 data->fan_div[0] = i & 0x7;
1173 data->fan_div[1] = (i & 0x70) >> 4;
1174 err = nct6775_read_value(data, NCT6775_REG_FANDIV2, &i);
1177 data->fan_div[2] = i & 0x7;
1178 if (data->has_fan & BIT(3))
1179 data->fan_div[3] = (i & 0x70) >> 4;
1184 static int nct6775_update_fan_div_common(struct nct6775_data *data)
1186 if (data->kind == nct6775)
1187 return nct6775_update_fan_div(data);
1191 static int nct6775_init_fan_div(struct nct6775_data *data)
1195 err = nct6775_update_fan_div_common(data);
1200 * For all fans, start with highest divider value if the divider
1201 * register is not initialized. This ensures that we get a
1202 * reading from the fan count register, even if it is not optimal.
1203 * We'll compute a better divider later on.
1205 for (i = 0; i < ARRAY_SIZE(data->fan_div); i++) {
1206 if (!(data->has_fan & BIT(i)))
1208 if (data->fan_div[i] == 0) {
1209 data->fan_div[i] = 7;
1210 err = nct6775_write_fan_div_common(data, i);
1219 static int nct6775_init_fan_common(struct device *dev,
1220 struct nct6775_data *data)
1225 if (data->has_fan_div) {
1226 err = nct6775_init_fan_div(data);
1232 * If fan_min is not set (0), set it to 0xff to disable it. This
1233 * prevents the unnecessary warning when fanX_min is reported as 0.
1235 for (i = 0; i < ARRAY_SIZE(data->fan_min); i++) {
1236 if (data->has_fan_min & BIT(i)) {
1237 err = nct6775_read_value(data, data->REG_FAN_MIN[i], ®);
1241 err = nct6775_write_value(data, data->REG_FAN_MIN[i],
1242 data->has_fan_div ? 0xff : 0xff1f);
1252 static int nct6775_select_fan_div(struct device *dev,
1253 struct nct6775_data *data, int nr, u16 reg)
1256 u8 fan_div = data->fan_div[nr];
1259 if (!data->has_fan_div)
1263 * If we failed to measure the fan speed, or the reported value is not
1264 * in the optimal range, and the clock divider can be modified,
1265 * let's try that for next time.
1267 if (reg == 0x00 && fan_div < 0x07)
1269 else if (reg != 0x00 && reg < 0x30 && fan_div > 0)
1272 if (fan_div != data->fan_div[nr]) {
1273 dev_dbg(dev, "Modifying fan%d clock divider from %u to %u\n",
1274 nr + 1, div_from_reg(data->fan_div[nr]),
1275 div_from_reg(fan_div));
1277 /* Preserve min limit if possible */
1278 if (data->has_fan_min & BIT(nr)) {
1279 fan_min = data->fan_min[nr];
1280 if (fan_div > data->fan_div[nr]) {
1281 if (fan_min != 255 && fan_min > 1)
1284 if (fan_min != 255) {
1290 if (fan_min != data->fan_min[nr]) {
1291 data->fan_min[nr] = fan_min;
1292 err = nct6775_write_value(data, data->REG_FAN_MIN[nr], fan_min);
1297 data->fan_div[nr] = fan_div;
1298 err = nct6775_write_fan_div_common(data, nr);
1306 static int nct6775_update_pwm(struct device *dev)
1308 struct nct6775_data *data = dev_get_drvdata(dev);
1310 u16 fanmodecfg, reg;
1313 for (i = 0; i < data->pwm_num; i++) {
1314 if (!(data->has_pwm & BIT(i)))
1317 err = nct6775_read_value(data, data->REG_PWM_MODE[i], ®);
1320 duty_is_dc = data->REG_PWM_MODE[i] && (reg & data->PWM_MODE_MASK[i]);
1321 data->pwm_mode[i] = !duty_is_dc;
1323 err = nct6775_read_value(data, data->REG_FAN_MODE[i], &fanmodecfg);
1326 for (j = 0; j < ARRAY_SIZE(data->REG_PWM); j++) {
1327 if (data->REG_PWM[j] && data->REG_PWM[j][i]) {
1328 err = nct6775_read_value(data, data->REG_PWM[j][i], ®);
1331 data->pwm[j][i] = reg;
1335 data->pwm_enable[i] = reg_to_pwm_enable(data->pwm[0][i],
1336 (fanmodecfg >> 4) & 7);
1338 if (!data->temp_tolerance[0][i] ||
1339 data->pwm_enable[i] != speed_cruise)
1340 data->temp_tolerance[0][i] = fanmodecfg & 0x0f;
1341 if (!data->target_speed_tolerance[i] ||
1342 data->pwm_enable[i] == speed_cruise) {
1343 u8 t = fanmodecfg & 0x0f;
1345 if (data->REG_TOLERANCE_H) {
1346 err = nct6775_read_value(data, data->REG_TOLERANCE_H[i], ®);
1349 t |= (reg & 0x70) >> 1;
1351 data->target_speed_tolerance[i] = t;
1354 err = nct6775_read_value(data, data->REG_CRITICAL_TEMP_TOLERANCE[i], ®);
1357 data->temp_tolerance[1][i] = reg;
1359 err = nct6775_read_value(data, data->REG_TEMP_SEL[i], ®);
1362 data->pwm_temp_sel[i] = reg & 0x1f;
1363 /* If fan can stop, report floor as 0 */
1365 data->pwm[2][i] = 0;
1367 if (!data->REG_WEIGHT_TEMP_SEL[i])
1370 err = nct6775_read_value(data, data->REG_WEIGHT_TEMP_SEL[i], ®);
1373 data->pwm_weight_temp_sel[i] = reg & 0x1f;
1374 /* If weight is disabled, report weight source as 0 */
1376 data->pwm_weight_temp_sel[i] = 0;
1378 /* Weight temp data */
1379 for (j = 0; j < ARRAY_SIZE(data->weight_temp); j++) {
1380 err = nct6775_read_value(data, data->REG_WEIGHT_TEMP[j][i], ®);
1383 data->weight_temp[j][i] = reg;
1390 static int nct6775_update_pwm_limits(struct device *dev)
1392 struct nct6775_data *data = dev_get_drvdata(dev);
1396 for (i = 0; i < data->pwm_num; i++) {
1397 if (!(data->has_pwm & BIT(i)))
1400 for (j = 0; j < ARRAY_SIZE(data->fan_time); j++) {
1401 err = nct6775_read_value(data, data->REG_FAN_TIME[j][i], ®);
1404 data->fan_time[j][i] = reg;
1407 err = nct6775_read_value(data, data->REG_TARGET[i], ®_t);
1411 /* Update only in matching mode or if never updated */
1412 if (!data->target_temp[i] ||
1413 data->pwm_enable[i] == thermal_cruise)
1414 data->target_temp[i] = reg_t & data->target_temp_mask;
1415 if (!data->target_speed[i] ||
1416 data->pwm_enable[i] == speed_cruise) {
1417 if (data->REG_TOLERANCE_H) {
1418 err = nct6775_read_value(data, data->REG_TOLERANCE_H[i], ®);
1421 reg_t |= (reg & 0x0f) << 8;
1423 data->target_speed[i] = reg_t;
1426 for (j = 0; j < data->auto_pwm_num; j++) {
1427 err = nct6775_read_value(data, NCT6775_AUTO_PWM(data, i, j), ®);
1430 data->auto_pwm[i][j] = reg;
1432 err = nct6775_read_value(data, NCT6775_AUTO_TEMP(data, i, j), ®);
1435 data->auto_temp[i][j] = reg;
1438 /* critical auto_pwm temperature data */
1439 err = nct6775_read_value(data, data->REG_CRITICAL_TEMP[i], ®);
1442 data->auto_temp[i][data->auto_pwm_num] = reg;
1444 switch (data->kind) {
1446 err = nct6775_read_value(data, NCT6775_REG_CRITICAL_ENAB[i], ®);
1449 data->auto_pwm[i][data->auto_pwm_num] =
1450 (reg & 0x02) ? 0xff : 0x00;
1453 data->auto_pwm[i][data->auto_pwm_num] = 0xff;
1465 err = nct6775_read_value(data, data->REG_CRITICAL_PWM_ENABLE[i], ®);
1468 if (reg & data->CRITICAL_PWM_ENABLE_MASK) {
1469 err = nct6775_read_value(data, data->REG_CRITICAL_PWM[i], ®);
1475 data->auto_pwm[i][data->auto_pwm_num] = reg;
1483 struct nct6775_data *nct6775_update_device(struct device *dev)
1485 struct nct6775_data *data = dev_get_drvdata(dev);
1489 mutex_lock(&data->update_lock);
1491 if (time_after(jiffies, data->last_updated + HZ + HZ / 2)
1493 /* Fan clock dividers */
1494 err = nct6775_update_fan_div_common(data);
1498 /* Measured voltages and limits */
1499 for (i = 0; i < data->in_num; i++) {
1500 if (!(data->have_in & BIT(i)))
1503 err = nct6775_read_value(data, data->REG_VIN[i], ®);
1506 data->in[i][0] = reg;
1508 err = nct6775_read_value(data, data->REG_IN_MINMAX[0][i], ®);
1511 data->in[i][1] = reg;
1513 err = nct6775_read_value(data, data->REG_IN_MINMAX[1][i], ®);
1516 data->in[i][2] = reg;
1519 /* Measured fan speeds and limits */
1520 for (i = 0; i < ARRAY_SIZE(data->rpm); i++) {
1521 if (!(data->has_fan & BIT(i)))
1524 err = nct6775_read_value(data, data->REG_FAN[i], ®);
1527 data->rpm[i] = data->fan_from_reg(reg,
1530 if (data->has_fan_min & BIT(i)) {
1531 err = nct6775_read_value(data, data->REG_FAN_MIN[i], ®);
1534 data->fan_min[i] = reg;
1537 if (data->REG_FAN_PULSES[i]) {
1538 err = nct6775_read_value(data, data->REG_FAN_PULSES[i], ®);
1541 data->fan_pulses[i] = (reg >> data->FAN_PULSE_SHIFT[i]) & 0x03;
1544 err = nct6775_select_fan_div(dev, data, i, reg);
1549 err = nct6775_update_pwm(dev);
1553 err = nct6775_update_pwm_limits(dev);
1557 /* Measured temperatures and limits */
1558 for (i = 0; i < NUM_TEMP; i++) {
1559 if (!(data->have_temp & BIT(i)))
1561 for (j = 0; j < ARRAY_SIZE(data->reg_temp); j++) {
1562 if (data->reg_temp[j][i]) {
1563 err = nct6775_read_temp(data, data->reg_temp[j][i], ®);
1566 data->temp[j][i] = reg;
1569 if (i >= NUM_TEMP_FIXED ||
1570 !(data->have_temp_fixed & BIT(i)))
1572 err = nct6775_read_value(data, data->REG_TEMP_OFFSET[i], ®);
1575 data->temp_offset[i] = reg;
1578 for (i = 0; i < NUM_TSI_TEMP; i++) {
1579 if (!(data->have_tsi_temp & BIT(i)))
1581 err = nct6775_read_value(data, data->REG_TSI_TEMP[i], ®);
1584 data->tsi_temp[i] = reg;
1588 for (i = 0; i < NUM_REG_ALARM; i++) {
1591 if (!data->REG_ALARM[i])
1593 err = nct6775_read_value(data, data->REG_ALARM[i], &alarm);
1596 data->alarms |= ((u64)alarm) << (i << 3);
1600 for (i = 0; i < NUM_REG_BEEP; i++) {
1603 if (!data->REG_BEEP[i])
1605 err = nct6775_read_value(data, data->REG_BEEP[i], &beep);
1608 data->beeps |= ((u64)beep) << (i << 3);
1611 data->last_updated = jiffies;
1615 mutex_unlock(&data->update_lock);
1616 return err ? ERR_PTR(err) : data;
1618 EXPORT_SYMBOL_GPL(nct6775_update_device);
1621 * Sysfs callback functions
1624 show_in_reg(struct device *dev, struct device_attribute *attr, char *buf)
1626 struct nct6775_data *data = nct6775_update_device(dev);
1627 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
1628 int index = sattr->index;
1632 return PTR_ERR(data);
1634 return sprintf(buf, "%ld\n", in_from_reg(data->in[nr][index], nr));
1638 store_in_reg(struct device *dev, struct device_attribute *attr, const char *buf,
1641 struct nct6775_data *data = dev_get_drvdata(dev);
1642 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
1643 int index = sattr->index;
1648 err = kstrtoul(buf, 10, &val);
1651 mutex_lock(&data->update_lock);
1652 data->in[nr][index] = in_to_reg(val, nr);
1653 err = nct6775_write_value(data, data->REG_IN_MINMAX[index - 1][nr], data->in[nr][index]);
1654 mutex_unlock(&data->update_lock);
1655 return err ? : count;
1659 nct6775_show_alarm(struct device *dev, struct device_attribute *attr, char *buf)
1661 struct nct6775_data *data = nct6775_update_device(dev);
1662 struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr);
1666 return PTR_ERR(data);
1668 nr = data->ALARM_BITS[sattr->index];
1669 return sprintf(buf, "%u\n",
1670 (unsigned int)((data->alarms >> nr) & 0x01));
1672 EXPORT_SYMBOL_GPL(nct6775_show_alarm);
1674 static int find_temp_source(struct nct6775_data *data, int index, int count)
1676 int source = data->temp_src[index];
1679 for (nr = 0; nr < count; nr++) {
1682 err = nct6775_read_value(data, data->REG_TEMP_SOURCE[nr], &src);
1685 if ((src & 0x1f) == source)
1692 show_temp_alarm(struct device *dev, struct device_attribute *attr, char *buf)
1694 struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr);
1695 struct nct6775_data *data = nct6775_update_device(dev);
1696 unsigned int alarm = 0;
1700 return PTR_ERR(data);
1703 * For temperatures, there is no fixed mapping from registers to alarm
1704 * bits. Alarm bits are determined by the temperature source mapping.
1706 nr = find_temp_source(data, sattr->index, data->num_temp_alarms);
1708 int bit = data->ALARM_BITS[nr + TEMP_ALARM_BASE];
1710 alarm = (data->alarms >> bit) & 0x01;
1712 return sprintf(buf, "%u\n", alarm);
1716 nct6775_show_beep(struct device *dev, struct device_attribute *attr, char *buf)
1718 struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr);
1719 struct nct6775_data *data = nct6775_update_device(dev);
1723 return PTR_ERR(data);
1725 nr = data->BEEP_BITS[sattr->index];
1727 return sprintf(buf, "%u\n",
1728 (unsigned int)((data->beeps >> nr) & 0x01));
1730 EXPORT_SYMBOL_GPL(nct6775_show_beep);
1733 nct6775_store_beep(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
1735 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
1736 struct nct6775_data *data = dev_get_drvdata(dev);
1737 int nr = data->BEEP_BITS[sattr->index];
1738 int regindex = nr >> 3;
1742 err = kstrtoul(buf, 10, &val);
1748 mutex_lock(&data->update_lock);
1750 data->beeps |= (1ULL << nr);
1752 data->beeps &= ~(1ULL << nr);
1753 err = nct6775_write_value(data, data->REG_BEEP[regindex],
1754 (data->beeps >> (regindex << 3)) & 0xff);
1755 mutex_unlock(&data->update_lock);
1756 return err ? : count;
1758 EXPORT_SYMBOL_GPL(nct6775_store_beep);
1761 show_temp_beep(struct device *dev, struct device_attribute *attr, char *buf)
1763 struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr);
1764 struct nct6775_data *data = nct6775_update_device(dev);
1765 unsigned int beep = 0;
1769 return PTR_ERR(data);
1772 * For temperatures, there is no fixed mapping from registers to beep
1773 * enable bits. Beep enable bits are determined by the temperature
1776 nr = find_temp_source(data, sattr->index, data->num_temp_beeps);
1778 int bit = data->BEEP_BITS[nr + TEMP_ALARM_BASE];
1780 beep = (data->beeps >> bit) & 0x01;
1782 return sprintf(buf, "%u\n", beep);
1786 store_temp_beep(struct device *dev, struct device_attribute *attr,
1787 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, bit, regindex;
1795 err = kstrtoul(buf, 10, &val);
1801 nr = find_temp_source(data, sattr->index, data->num_temp_beeps);
1805 bit = data->BEEP_BITS[nr + TEMP_ALARM_BASE];
1806 regindex = bit >> 3;
1808 mutex_lock(&data->update_lock);
1810 data->beeps |= (1ULL << bit);
1812 data->beeps &= ~(1ULL << bit);
1813 err = nct6775_write_value(data, data->REG_BEEP[regindex],
1814 (data->beeps >> (regindex << 3)) & 0xff);
1815 mutex_unlock(&data->update_lock);
1817 return err ? : count;
1820 static umode_t nct6775_in_is_visible(struct kobject *kobj,
1821 struct attribute *attr, int index)
1823 struct device *dev = kobj_to_dev(kobj);
1824 struct nct6775_data *data = dev_get_drvdata(dev);
1825 int in = index / 5; /* voltage index */
1827 if (!(data->have_in & BIT(in)))
1830 return nct6775_attr_mode(data, attr);
1833 SENSOR_TEMPLATE_2(in_input, "in%d_input", 0444, show_in_reg, NULL, 0, 0);
1834 SENSOR_TEMPLATE(in_alarm, "in%d_alarm", 0444, nct6775_show_alarm, NULL, 0);
1835 SENSOR_TEMPLATE(in_beep, "in%d_beep", 0644, nct6775_show_beep, nct6775_store_beep, 0);
1836 SENSOR_TEMPLATE_2(in_min, "in%d_min", 0644, show_in_reg, store_in_reg, 0, 1);
1837 SENSOR_TEMPLATE_2(in_max, "in%d_max", 0644, show_in_reg, store_in_reg, 0, 2);
1840 * nct6775_in_is_visible uses the index into the following array
1841 * to determine if attributes should be created or not.
1842 * Any change in order or content must be matched.
1844 static struct sensor_device_template *nct6775_attributes_in_template[] = {
1845 &sensor_dev_template_in_input,
1846 &sensor_dev_template_in_alarm,
1847 &sensor_dev_template_in_beep,
1848 &sensor_dev_template_in_min,
1849 &sensor_dev_template_in_max,
1853 static const struct sensor_template_group nct6775_in_template_group = {
1854 .templates = nct6775_attributes_in_template,
1855 .is_visible = nct6775_in_is_visible,
1859 show_fan(struct device *dev, struct device_attribute *attr, char *buf)
1861 struct nct6775_data *data = nct6775_update_device(dev);
1862 struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr);
1863 int nr = sattr->index;
1866 return PTR_ERR(data);
1868 return sprintf(buf, "%d\n", data->rpm[nr]);
1872 show_fan_min(struct device *dev, struct device_attribute *attr, char *buf)
1874 struct nct6775_data *data = nct6775_update_device(dev);
1875 struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr);
1876 int nr = sattr->index;
1879 return PTR_ERR(data);
1881 return sprintf(buf, "%d\n",
1882 data->fan_from_reg_min(data->fan_min[nr],
1883 data->fan_div[nr]));
1887 show_fan_div(struct device *dev, struct device_attribute *attr, char *buf)
1889 struct nct6775_data *data = nct6775_update_device(dev);
1890 struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr);
1891 int nr = sattr->index;
1894 return PTR_ERR(data);
1896 return sprintf(buf, "%u\n", div_from_reg(data->fan_div[nr]));
1900 store_fan_min(struct device *dev, struct device_attribute *attr,
1901 const char *buf, size_t count)
1903 struct nct6775_data *data = dev_get_drvdata(dev);
1904 struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr);
1905 int nr = sattr->index;
1911 err = kstrtoul(buf, 10, &val);
1915 mutex_lock(&data->update_lock);
1916 if (!data->has_fan_div) {
1917 /* NCT6776F or NCT6779D; we know this is a 13 bit register */
1923 val = 1350000U / val;
1924 val = (val & 0x1f) | ((val << 3) & 0xff00);
1926 data->fan_min[nr] = val;
1927 goto write_min; /* Leave fan divider alone */
1930 /* No min limit, alarm disabled */
1931 data->fan_min[nr] = 255;
1932 new_div = data->fan_div[nr]; /* No change */
1933 dev_info(dev, "fan%u low limit and alarm disabled\n", nr + 1);
1936 reg = 1350000U / val;
1937 if (reg >= 128 * 255) {
1939 * Speed below this value cannot possibly be represented,
1940 * even with the highest divider (128)
1942 data->fan_min[nr] = 254;
1943 new_div = 7; /* 128 == BIT(7) */
1945 "fan%u low limit %lu below minimum %u, set to minimum\n",
1946 nr + 1, val, data->fan_from_reg_min(254, 7));
1949 * Speed above this value cannot possibly be represented,
1950 * even with the lowest divider (1)
1952 data->fan_min[nr] = 1;
1953 new_div = 0; /* 1 == BIT(0) */
1955 "fan%u low limit %lu above maximum %u, set to maximum\n",
1956 nr + 1, val, data->fan_from_reg_min(1, 0));
1959 * Automatically pick the best divider, i.e. the one such
1960 * that the min limit will correspond to a register value
1961 * in the 96..192 range
1964 while (reg > 192 && new_div < 7) {
1968 data->fan_min[nr] = reg;
1973 * Write both the fan clock divider (if it changed) and the new
1974 * fan min (unconditionally)
1976 if (new_div != data->fan_div[nr]) {
1977 dev_dbg(dev, "fan%u clock divider changed from %u to %u\n",
1978 nr + 1, div_from_reg(data->fan_div[nr]),
1979 div_from_reg(new_div));
1980 data->fan_div[nr] = new_div;
1981 err = nct6775_write_fan_div_common(data, nr);
1984 /* Give the chip time to sample a new speed value */
1985 data->last_updated = jiffies;
1989 err = nct6775_write_value(data, data->REG_FAN_MIN[nr], data->fan_min[nr]);
1990 mutex_unlock(&data->update_lock);
1992 return err ? : count;
1996 show_fan_pulses(struct device *dev, struct device_attribute *attr, char *buf)
1998 struct nct6775_data *data = nct6775_update_device(dev);
1999 struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr);
2003 return PTR_ERR(data);
2005 p = data->fan_pulses[sattr->index];
2006 return sprintf(buf, "%d\n", p ? : 4);
2010 store_fan_pulses(struct device *dev, struct device_attribute *attr,
2011 const char *buf, size_t count)
2013 struct nct6775_data *data = dev_get_drvdata(dev);
2014 struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr);
2015 int nr = sattr->index;
2020 err = kstrtoul(buf, 10, &val);
2027 mutex_lock(&data->update_lock);
2028 data->fan_pulses[nr] = val & 3;
2029 err = nct6775_read_value(data, data->REG_FAN_PULSES[nr], ®);
2032 reg &= ~(0x03 << data->FAN_PULSE_SHIFT[nr]);
2033 reg |= (val & 3) << data->FAN_PULSE_SHIFT[nr];
2034 err = nct6775_write_value(data, data->REG_FAN_PULSES[nr], reg);
2036 mutex_unlock(&data->update_lock);
2038 return err ? : count;
2041 static umode_t nct6775_fan_is_visible(struct kobject *kobj,
2042 struct attribute *attr, int index)
2044 struct device *dev = kobj_to_dev(kobj);
2045 struct nct6775_data *data = dev_get_drvdata(dev);
2046 int fan = index / 6; /* fan index */
2047 int nr = index % 6; /* attribute index */
2049 if (!(data->has_fan & BIT(fan)))
2052 if (nr == 1 && data->ALARM_BITS[FAN_ALARM_BASE + fan] == -1)
2054 if (nr == 2 && data->BEEP_BITS[FAN_ALARM_BASE + fan] == -1)
2056 if (nr == 3 && !data->REG_FAN_PULSES[fan])
2058 if (nr == 4 && !(data->has_fan_min & BIT(fan)))
2060 if (nr == 5 && data->kind != nct6775)
2063 return nct6775_attr_mode(data, attr);
2066 SENSOR_TEMPLATE(fan_input, "fan%d_input", 0444, show_fan, NULL, 0);
2067 SENSOR_TEMPLATE(fan_alarm, "fan%d_alarm", 0444, nct6775_show_alarm, NULL, FAN_ALARM_BASE);
2068 SENSOR_TEMPLATE(fan_beep, "fan%d_beep", 0644, nct6775_show_beep,
2069 nct6775_store_beep, FAN_ALARM_BASE);
2070 SENSOR_TEMPLATE(fan_pulses, "fan%d_pulses", 0644, show_fan_pulses, store_fan_pulses, 0);
2071 SENSOR_TEMPLATE(fan_min, "fan%d_min", 0644, show_fan_min, store_fan_min, 0);
2072 SENSOR_TEMPLATE(fan_div, "fan%d_div", 0444, show_fan_div, NULL, 0);
2075 * nct6775_fan_is_visible uses the index into the following array
2076 * to determine if attributes should be created or not.
2077 * Any change in order or content must be matched.
2079 static struct sensor_device_template *nct6775_attributes_fan_template[] = {
2080 &sensor_dev_template_fan_input,
2081 &sensor_dev_template_fan_alarm, /* 1 */
2082 &sensor_dev_template_fan_beep, /* 2 */
2083 &sensor_dev_template_fan_pulses,
2084 &sensor_dev_template_fan_min, /* 4 */
2085 &sensor_dev_template_fan_div, /* 5 */
2089 static const struct sensor_template_group nct6775_fan_template_group = {
2090 .templates = nct6775_attributes_fan_template,
2091 .is_visible = nct6775_fan_is_visible,
2096 show_temp_label(struct device *dev, struct device_attribute *attr, char *buf)
2098 struct nct6775_data *data = nct6775_update_device(dev);
2099 struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr);
2100 int nr = sattr->index;
2103 return PTR_ERR(data);
2105 return sprintf(buf, "%s\n", data->temp_label[data->temp_src[nr]]);
2109 show_temp(struct device *dev, struct device_attribute *attr, char *buf)
2111 struct nct6775_data *data = nct6775_update_device(dev);
2112 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
2114 int index = sattr->index;
2117 return PTR_ERR(data);
2119 return sprintf(buf, "%d\n", LM75_TEMP_FROM_REG(data->temp[index][nr]));
2123 store_temp(struct device *dev, struct device_attribute *attr, const char *buf,
2126 struct nct6775_data *data = dev_get_drvdata(dev);
2127 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
2129 int index = sattr->index;
2133 err = kstrtol(buf, 10, &val);
2137 mutex_lock(&data->update_lock);
2138 data->temp[index][nr] = LM75_TEMP_TO_REG(val);
2139 err = nct6775_write_temp(data, data->reg_temp[index][nr], data->temp[index][nr]);
2140 mutex_unlock(&data->update_lock);
2141 return err ? : count;
2145 show_temp_offset(struct device *dev, struct device_attribute *attr, char *buf)
2147 struct nct6775_data *data = nct6775_update_device(dev);
2148 struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr);
2151 return PTR_ERR(data);
2153 return sprintf(buf, "%d\n", data->temp_offset[sattr->index] * 1000);
2157 store_temp_offset(struct device *dev, struct device_attribute *attr,
2158 const char *buf, size_t count)
2160 struct nct6775_data *data = dev_get_drvdata(dev);
2161 struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr);
2162 int nr = sattr->index;
2166 err = kstrtol(buf, 10, &val);
2170 val = clamp_val(DIV_ROUND_CLOSEST(val, 1000), -128, 127);
2172 mutex_lock(&data->update_lock);
2173 data->temp_offset[nr] = val;
2174 err = nct6775_write_value(data, data->REG_TEMP_OFFSET[nr], val);
2175 mutex_unlock(&data->update_lock);
2177 return err ? : count;
2181 show_temp_type(struct device *dev, struct device_attribute *attr, char *buf)
2183 struct nct6775_data *data = nct6775_update_device(dev);
2184 struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr);
2185 int nr = sattr->index;
2188 return PTR_ERR(data);
2190 return sprintf(buf, "%d\n", (int)data->temp_type[nr]);
2194 store_temp_type(struct device *dev, struct device_attribute *attr,
2195 const char *buf, size_t count)
2197 struct nct6775_data *data = nct6775_update_device(dev);
2198 struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr);
2199 int nr = sattr->index;
2206 return PTR_ERR(data);
2208 err = kstrtoul(buf, 10, &val);
2212 if (val != 1 && val != 3 && val != 4)
2215 mutex_lock(&data->update_lock);
2217 data->temp_type[nr] = val;
2219 dbit = data->DIODE_MASK << nr;
2221 err = nct6775_read_value(data, data->REG_VBAT, &vbat);
2226 err = nct6775_read_value(data, data->REG_DIODE, &diode);
2232 case 1: /* CPU diode (diode, current mode) */
2236 case 3: /* diode, voltage mode */
2239 case 4: /* thermistor */
2242 err = nct6775_write_value(data, data->REG_VBAT, vbat);
2245 err = nct6775_write_value(data, data->REG_DIODE, diode);
2247 mutex_unlock(&data->update_lock);
2248 return err ? : count;
2251 static umode_t nct6775_temp_is_visible(struct kobject *kobj,
2252 struct attribute *attr, int index)
2254 struct device *dev = kobj_to_dev(kobj);
2255 struct nct6775_data *data = dev_get_drvdata(dev);
2256 int temp = index / 10; /* temp index */
2257 int nr = index % 10; /* attribute index */
2259 if (!(data->have_temp & BIT(temp)))
2262 if (nr == 1 && !data->temp_label)
2265 if (nr == 2 && find_temp_source(data, temp, data->num_temp_alarms) < 0)
2266 return 0; /* alarm */
2268 if (nr == 3 && find_temp_source(data, temp, data->num_temp_beeps) < 0)
2269 return 0; /* beep */
2271 if (nr == 4 && !data->reg_temp[1][temp]) /* max */
2274 if (nr == 5 && !data->reg_temp[2][temp]) /* max_hyst */
2277 if (nr == 6 && !data->reg_temp[3][temp]) /* crit */
2280 if (nr == 7 && !data->reg_temp[4][temp]) /* lcrit */
2283 /* offset and type only apply to fixed sensors */
2284 if (nr > 7 && !(data->have_temp_fixed & BIT(temp)))
2287 return nct6775_attr_mode(data, attr);
2290 SENSOR_TEMPLATE_2(temp_input, "temp%d_input", 0444, show_temp, NULL, 0, 0);
2291 SENSOR_TEMPLATE(temp_label, "temp%d_label", 0444, show_temp_label, NULL, 0);
2292 SENSOR_TEMPLATE_2(temp_max, "temp%d_max", 0644, show_temp, store_temp, 0, 1);
2293 SENSOR_TEMPLATE_2(temp_max_hyst, "temp%d_max_hyst", 0644, show_temp, store_temp, 0, 2);
2294 SENSOR_TEMPLATE_2(temp_crit, "temp%d_crit", 0644, show_temp, store_temp, 0, 3);
2295 SENSOR_TEMPLATE_2(temp_lcrit, "temp%d_lcrit", 0644, show_temp, store_temp, 0, 4);
2296 SENSOR_TEMPLATE(temp_offset, "temp%d_offset", 0644, show_temp_offset, store_temp_offset, 0);
2297 SENSOR_TEMPLATE(temp_type, "temp%d_type", 0644, show_temp_type, store_temp_type, 0);
2298 SENSOR_TEMPLATE(temp_alarm, "temp%d_alarm", 0444, show_temp_alarm, NULL, 0);
2299 SENSOR_TEMPLATE(temp_beep, "temp%d_beep", 0644, show_temp_beep, store_temp_beep, 0);
2302 * nct6775_temp_is_visible uses the index into the following array
2303 * to determine if attributes should be created or not.
2304 * Any change in order or content must be matched.
2306 static struct sensor_device_template *nct6775_attributes_temp_template[] = {
2307 &sensor_dev_template_temp_input,
2308 &sensor_dev_template_temp_label,
2309 &sensor_dev_template_temp_alarm, /* 2 */
2310 &sensor_dev_template_temp_beep, /* 3 */
2311 &sensor_dev_template_temp_max, /* 4 */
2312 &sensor_dev_template_temp_max_hyst, /* 5 */
2313 &sensor_dev_template_temp_crit, /* 6 */
2314 &sensor_dev_template_temp_lcrit, /* 7 */
2315 &sensor_dev_template_temp_offset, /* 8 */
2316 &sensor_dev_template_temp_type, /* 9 */
2320 static const struct sensor_template_group nct6775_temp_template_group = {
2321 .templates = nct6775_attributes_temp_template,
2322 .is_visible = nct6775_temp_is_visible,
2326 static ssize_t show_tsi_temp(struct device *dev, struct device_attribute *attr, char *buf)
2328 struct nct6775_data *data = nct6775_update_device(dev);
2329 struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr);
2332 return PTR_ERR(data);
2334 return sysfs_emit(buf, "%u\n", tsi_temp_from_reg(data->tsi_temp[sattr->index]));
2337 static ssize_t show_tsi_temp_label(struct device *dev, struct device_attribute *attr, char *buf)
2339 struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr);
2341 return sysfs_emit(buf, "TSI%d_TEMP\n", sattr->index);
2344 SENSOR_TEMPLATE(tsi_temp_input, "temp%d_input", 0444, show_tsi_temp, NULL, 0);
2345 SENSOR_TEMPLATE(tsi_temp_label, "temp%d_label", 0444, show_tsi_temp_label, NULL, 0);
2347 static umode_t nct6775_tsi_temp_is_visible(struct kobject *kobj, struct attribute *attr,
2350 struct device *dev = kobj_to_dev(kobj);
2351 struct nct6775_data *data = dev_get_drvdata(dev);
2352 int temp = index / 2;
2354 return (data->have_tsi_temp & BIT(temp)) ? nct6775_attr_mode(data, attr) : 0;
2358 * The index calculation in nct6775_tsi_temp_is_visible() must be kept in
2359 * sync with the size of this array.
2361 static struct sensor_device_template *nct6775_tsi_temp_template[] = {
2362 &sensor_dev_template_tsi_temp_input,
2363 &sensor_dev_template_tsi_temp_label,
2368 show_pwm_mode(struct device *dev, struct device_attribute *attr, char *buf)
2370 struct nct6775_data *data = nct6775_update_device(dev);
2371 struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr);
2374 return PTR_ERR(data);
2376 return sprintf(buf, "%d\n", data->pwm_mode[sattr->index]);
2380 store_pwm_mode(struct device *dev, struct device_attribute *attr,
2381 const char *buf, size_t count)
2383 struct nct6775_data *data = dev_get_drvdata(dev);
2384 struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr);
2385 int nr = sattr->index;
2390 err = kstrtoul(buf, 10, &val);
2397 /* Setting DC mode (0) is not supported for all chips/channels */
2398 if (data->REG_PWM_MODE[nr] == 0) {
2404 mutex_lock(&data->update_lock);
2405 data->pwm_mode[nr] = val;
2406 err = nct6775_read_value(data, data->REG_PWM_MODE[nr], ®);
2409 reg &= ~data->PWM_MODE_MASK[nr];
2411 reg |= data->PWM_MODE_MASK[nr];
2412 err = nct6775_write_value(data, data->REG_PWM_MODE[nr], reg);
2414 mutex_unlock(&data->update_lock);
2415 return err ? : count;
2419 show_pwm(struct device *dev, struct device_attribute *attr, char *buf)
2421 struct nct6775_data *data = nct6775_update_device(dev);
2422 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
2424 int index = sattr->index;
2429 return PTR_ERR(data);
2432 * For automatic fan control modes, show current pwm readings.
2433 * Otherwise, show the configured value.
2435 if (index == 0 && data->pwm_enable[nr] > manual) {
2436 err = nct6775_read_value(data, data->REG_PWM_READ[nr], &pwm);
2440 pwm = data->pwm[index][nr];
2443 return sprintf(buf, "%d\n", pwm);
2447 store_pwm(struct device *dev, struct device_attribute *attr, const char *buf,
2450 struct nct6775_data *data = dev_get_drvdata(dev);
2451 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
2453 int index = sattr->index;
2455 int minval[7] = { 0, 1, 1, data->pwm[2][nr], 0, 0, 0 };
2457 = { 255, 255, data->pwm[3][nr] ? : 255, 255, 255, 255, 255 };
2461 err = kstrtoul(buf, 10, &val);
2464 val = clamp_val(val, minval[index], maxval[index]);
2466 mutex_lock(&data->update_lock);
2467 data->pwm[index][nr] = val;
2468 err = nct6775_write_value(data, data->REG_PWM[index][nr], val);
2471 if (index == 2) { /* floor: disable if val == 0 */
2472 err = nct6775_read_value(data, data->REG_TEMP_SEL[nr], ®);
2478 err = nct6775_write_value(data, data->REG_TEMP_SEL[nr], reg);
2481 mutex_unlock(&data->update_lock);
2482 return err ? : count;
2485 /* Returns 0 if OK, -EINVAL otherwise */
2486 static int check_trip_points(struct nct6775_data *data, int nr)
2490 for (i = 0; i < data->auto_pwm_num - 1; i++) {
2491 if (data->auto_temp[nr][i] > data->auto_temp[nr][i + 1])
2494 for (i = 0; i < data->auto_pwm_num - 1; i++) {
2495 if (data->auto_pwm[nr][i] > data->auto_pwm[nr][i + 1])
2498 /* validate critical temperature and pwm if enabled (pwm > 0) */
2499 if (data->auto_pwm[nr][data->auto_pwm_num]) {
2500 if (data->auto_temp[nr][data->auto_pwm_num - 1] >
2501 data->auto_temp[nr][data->auto_pwm_num] ||
2502 data->auto_pwm[nr][data->auto_pwm_num - 1] >
2503 data->auto_pwm[nr][data->auto_pwm_num])
2509 static int pwm_update_registers(struct nct6775_data *data, int nr)
2514 switch (data->pwm_enable[nr]) {
2519 err = nct6775_read_value(data, data->REG_FAN_MODE[nr], ®);
2522 reg = (reg & ~data->tolerance_mask) |
2523 (data->target_speed_tolerance[nr] & data->tolerance_mask);
2524 err = nct6775_write_value(data, data->REG_FAN_MODE[nr], reg);
2527 err = nct6775_write_value(data, data->REG_TARGET[nr],
2528 data->target_speed[nr] & 0xff);
2531 if (data->REG_TOLERANCE_H) {
2532 reg = (data->target_speed[nr] >> 8) & 0x0f;
2533 reg |= (data->target_speed_tolerance[nr] & 0x38) << 1;
2534 err = nct6775_write_value(data, data->REG_TOLERANCE_H[nr], reg);
2539 case thermal_cruise:
2540 err = nct6775_write_value(data, data->REG_TARGET[nr], data->target_temp[nr]);
2545 err = nct6775_read_value(data, data->REG_FAN_MODE[nr], ®);
2548 reg = (reg & ~data->tolerance_mask) |
2549 data->temp_tolerance[0][nr];
2550 err = nct6775_write_value(data, data->REG_FAN_MODE[nr], reg);
2560 show_pwm_enable(struct device *dev, struct device_attribute *attr, char *buf)
2562 struct nct6775_data *data = nct6775_update_device(dev);
2563 struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr);
2566 return PTR_ERR(data);
2568 return sprintf(buf, "%d\n", data->pwm_enable[sattr->index]);
2572 store_pwm_enable(struct device *dev, struct device_attribute *attr,
2573 const char *buf, size_t count)
2575 struct nct6775_data *data = dev_get_drvdata(dev);
2576 struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr);
2577 int nr = sattr->index;
2582 err = kstrtoul(buf, 10, &val);
2589 if (val == sf3 && data->kind != nct6775)
2592 if (val == sf4 && check_trip_points(data, nr)) {
2593 dev_err(dev, "Inconsistent trip points, not switching to SmartFan IV mode\n");
2594 dev_err(dev, "Adjust trip points and try again\n");
2598 mutex_lock(&data->update_lock);
2599 data->pwm_enable[nr] = val;
2602 * turn off pwm control: select manual mode, set pwm to maximum
2604 data->pwm[0][nr] = 255;
2605 err = nct6775_write_value(data, data->REG_PWM[0][nr], 255);
2609 err = pwm_update_registers(data, nr);
2612 err = nct6775_read_value(data, data->REG_FAN_MODE[nr], ®);
2616 reg |= pwm_enable_to_reg(val) << 4;
2617 err = nct6775_write_value(data, data->REG_FAN_MODE[nr], reg);
2619 mutex_unlock(&data->update_lock);
2620 return err ? : count;
2624 show_pwm_temp_sel_common(struct nct6775_data *data, char *buf, int src)
2628 for (i = 0; i < NUM_TEMP; i++) {
2629 if (!(data->have_temp & BIT(i)))
2631 if (src == data->temp_src[i]) {
2637 return sprintf(buf, "%d\n", sel);
2641 show_pwm_temp_sel(struct device *dev, struct device_attribute *attr, char *buf)
2643 struct nct6775_data *data = nct6775_update_device(dev);
2644 struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr);
2645 int index = sattr->index;
2648 return PTR_ERR(data);
2650 return show_pwm_temp_sel_common(data, buf, data->pwm_temp_sel[index]);
2654 store_pwm_temp_sel(struct device *dev, struct device_attribute *attr,
2655 const char *buf, size_t count)
2657 struct nct6775_data *data = nct6775_update_device(dev);
2658 struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr);
2659 int nr = sattr->index;
2665 return PTR_ERR(data);
2667 err = kstrtoul(buf, 10, &val);
2670 if (val == 0 || val > NUM_TEMP)
2672 if (!(data->have_temp & BIT(val - 1)) || !data->temp_src[val - 1])
2675 mutex_lock(&data->update_lock);
2676 src = data->temp_src[val - 1];
2677 data->pwm_temp_sel[nr] = src;
2678 err = nct6775_read_value(data, data->REG_TEMP_SEL[nr], ®);
2683 err = nct6775_write_value(data, data->REG_TEMP_SEL[nr], reg);
2685 mutex_unlock(&data->update_lock);
2687 return err ? : count;
2691 show_pwm_weight_temp_sel(struct device *dev, struct device_attribute *attr,
2694 struct nct6775_data *data = nct6775_update_device(dev);
2695 struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr);
2696 int index = sattr->index;
2699 return PTR_ERR(data);
2701 return show_pwm_temp_sel_common(data, buf,
2702 data->pwm_weight_temp_sel[index]);
2706 store_pwm_weight_temp_sel(struct device *dev, struct device_attribute *attr,
2707 const char *buf, size_t count)
2709 struct nct6775_data *data = nct6775_update_device(dev);
2710 struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr);
2711 int nr = sattr->index;
2717 return PTR_ERR(data);
2719 err = kstrtoul(buf, 10, &val);
2724 val = array_index_nospec(val, NUM_TEMP + 1);
2725 if (val && (!(data->have_temp & BIT(val - 1)) ||
2726 !data->temp_src[val - 1]))
2729 mutex_lock(&data->update_lock);
2731 src = data->temp_src[val - 1];
2732 data->pwm_weight_temp_sel[nr] = src;
2733 err = nct6775_read_value(data, data->REG_WEIGHT_TEMP_SEL[nr], ®);
2737 reg |= (src | 0x80);
2738 err = nct6775_write_value(data, data->REG_WEIGHT_TEMP_SEL[nr], reg);
2740 data->pwm_weight_temp_sel[nr] = 0;
2741 err = nct6775_read_value(data, data->REG_WEIGHT_TEMP_SEL[nr], ®);
2745 err = nct6775_write_value(data, data->REG_WEIGHT_TEMP_SEL[nr], reg);
2748 mutex_unlock(&data->update_lock);
2750 return err ? : count;
2754 show_target_temp(struct device *dev, struct device_attribute *attr, char *buf)
2756 struct nct6775_data *data = nct6775_update_device(dev);
2757 struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr);
2760 return PTR_ERR(data);
2762 return sprintf(buf, "%d\n", data->target_temp[sattr->index] * 1000);
2766 store_target_temp(struct device *dev, struct device_attribute *attr,
2767 const char *buf, size_t count)
2769 struct nct6775_data *data = dev_get_drvdata(dev);
2770 struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr);
2771 int nr = sattr->index;
2775 err = kstrtoul(buf, 10, &val);
2779 val = clamp_val(DIV_ROUND_CLOSEST(val, 1000), 0,
2780 data->target_temp_mask);
2782 mutex_lock(&data->update_lock);
2783 data->target_temp[nr] = val;
2784 err = pwm_update_registers(data, nr);
2785 mutex_unlock(&data->update_lock);
2786 return err ? : count;
2790 show_target_speed(struct device *dev, struct device_attribute *attr, char *buf)
2792 struct nct6775_data *data = nct6775_update_device(dev);
2793 struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr);
2794 int nr = sattr->index;
2797 return PTR_ERR(data);
2799 return sprintf(buf, "%d\n",
2800 fan_from_reg16(data->target_speed[nr],
2801 data->fan_div[nr]));
2805 store_target_speed(struct device *dev, struct device_attribute *attr,
2806 const char *buf, size_t count)
2808 struct nct6775_data *data = dev_get_drvdata(dev);
2809 struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr);
2810 int nr = sattr->index;
2815 err = kstrtoul(buf, 10, &val);
2819 val = clamp_val(val, 0, 1350000U);
2820 speed = fan_to_reg(val, data->fan_div[nr]);
2822 mutex_lock(&data->update_lock);
2823 data->target_speed[nr] = speed;
2824 err = pwm_update_registers(data, nr);
2825 mutex_unlock(&data->update_lock);
2826 return err ? : count;
2830 show_temp_tolerance(struct device *dev, struct device_attribute *attr,
2833 struct nct6775_data *data = nct6775_update_device(dev);
2834 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
2836 int index = sattr->index;
2839 return PTR_ERR(data);
2841 return sprintf(buf, "%d\n", data->temp_tolerance[index][nr] * 1000);
2845 store_temp_tolerance(struct device *dev, struct device_attribute *attr,
2846 const char *buf, size_t count)
2848 struct nct6775_data *data = dev_get_drvdata(dev);
2849 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
2851 int index = sattr->index;
2855 err = kstrtoul(buf, 10, &val);
2859 /* Limit tolerance as needed */
2860 val = clamp_val(DIV_ROUND_CLOSEST(val, 1000), 0, data->tolerance_mask);
2862 mutex_lock(&data->update_lock);
2863 data->temp_tolerance[index][nr] = val;
2865 err = pwm_update_registers(data, nr);
2867 err = nct6775_write_value(data, data->REG_CRITICAL_TEMP_TOLERANCE[nr], val);
2868 mutex_unlock(&data->update_lock);
2869 return err ? : count;
2873 * Fan speed tolerance is a tricky beast, since the associated register is
2874 * a tick counter, but the value is reported and configured as rpm.
2875 * Compute resulting low and high rpm values and report the difference.
2876 * A fan speed tolerance only makes sense if a fan target speed has been
2877 * configured, so only display values other than 0 if that is the case.
2880 show_speed_tolerance(struct device *dev, struct device_attribute *attr,
2883 struct nct6775_data *data = nct6775_update_device(dev);
2884 struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr);
2885 int nr = sattr->index;
2886 int target, tolerance = 0;
2889 return PTR_ERR(data);
2891 target = data->target_speed[nr];
2894 int low = target - data->target_speed_tolerance[nr];
2895 int high = target + data->target_speed_tolerance[nr];
2904 tolerance = (fan_from_reg16(low, data->fan_div[nr])
2905 - fan_from_reg16(high, data->fan_div[nr])) / 2;
2908 return sprintf(buf, "%d\n", tolerance);
2912 store_speed_tolerance(struct device *dev, struct device_attribute *attr,
2913 const char *buf, size_t count)
2915 struct nct6775_data *data = dev_get_drvdata(dev);
2916 struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr);
2917 int nr = sattr->index;
2922 err = kstrtoul(buf, 10, &val);
2926 high = fan_from_reg16(data->target_speed[nr], data->fan_div[nr]) + val;
2927 low = fan_from_reg16(data->target_speed[nr], data->fan_div[nr]) - val;
2933 val = (fan_to_reg(low, data->fan_div[nr]) -
2934 fan_to_reg(high, data->fan_div[nr])) / 2;
2936 /* Limit tolerance as needed */
2937 val = clamp_val(val, 0, data->speed_tolerance_limit);
2939 mutex_lock(&data->update_lock);
2940 data->target_speed_tolerance[nr] = val;
2941 err = pwm_update_registers(data, nr);
2942 mutex_unlock(&data->update_lock);
2943 return err ? : count;
2946 SENSOR_TEMPLATE_2(pwm, "pwm%d", 0644, show_pwm, store_pwm, 0, 0);
2947 SENSOR_TEMPLATE(pwm_mode, "pwm%d_mode", 0644, show_pwm_mode, store_pwm_mode, 0);
2948 SENSOR_TEMPLATE(pwm_enable, "pwm%d_enable", 0644, show_pwm_enable, store_pwm_enable, 0);
2949 SENSOR_TEMPLATE(pwm_temp_sel, "pwm%d_temp_sel", 0644, show_pwm_temp_sel, store_pwm_temp_sel, 0);
2950 SENSOR_TEMPLATE(pwm_target_temp, "pwm%d_target_temp", 0644, show_target_temp, store_target_temp, 0);
2951 SENSOR_TEMPLATE(fan_target, "fan%d_target", 0644, show_target_speed, store_target_speed, 0);
2952 SENSOR_TEMPLATE(fan_tolerance, "fan%d_tolerance", 0644, show_speed_tolerance,
2953 store_speed_tolerance, 0);
2955 /* Smart Fan registers */
2958 show_weight_temp(struct device *dev, struct device_attribute *attr, char *buf)
2960 struct nct6775_data *data = nct6775_update_device(dev);
2961 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
2963 int index = sattr->index;
2966 return PTR_ERR(data);
2968 return sprintf(buf, "%d\n", data->weight_temp[index][nr] * 1000);
2972 store_weight_temp(struct device *dev, struct device_attribute *attr,
2973 const char *buf, size_t count)
2975 struct nct6775_data *data = dev_get_drvdata(dev);
2976 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
2978 int index = sattr->index;
2982 err = kstrtoul(buf, 10, &val);
2986 val = clamp_val(DIV_ROUND_CLOSEST(val, 1000), 0, 255);
2988 mutex_lock(&data->update_lock);
2989 data->weight_temp[index][nr] = val;
2990 err = nct6775_write_value(data, data->REG_WEIGHT_TEMP[index][nr], val);
2991 mutex_unlock(&data->update_lock);
2992 return err ? : count;
2995 SENSOR_TEMPLATE(pwm_weight_temp_sel, "pwm%d_weight_temp_sel", 0644,
2996 show_pwm_weight_temp_sel, store_pwm_weight_temp_sel, 0);
2997 SENSOR_TEMPLATE_2(pwm_weight_temp_step, "pwm%d_weight_temp_step",
2998 0644, show_weight_temp, store_weight_temp, 0, 0);
2999 SENSOR_TEMPLATE_2(pwm_weight_temp_step_tol, "pwm%d_weight_temp_step_tol",
3000 0644, show_weight_temp, store_weight_temp, 0, 1);
3001 SENSOR_TEMPLATE_2(pwm_weight_temp_step_base, "pwm%d_weight_temp_step_base",
3002 0644, show_weight_temp, store_weight_temp, 0, 2);
3003 SENSOR_TEMPLATE_2(pwm_weight_duty_step, "pwm%d_weight_duty_step", 0644, show_pwm, store_pwm, 0, 5);
3004 SENSOR_TEMPLATE_2(pwm_weight_duty_base, "pwm%d_weight_duty_base", 0644, show_pwm, store_pwm, 0, 6);
3007 show_fan_time(struct device *dev, struct device_attribute *attr, char *buf)
3009 struct nct6775_data *data = nct6775_update_device(dev);
3010 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
3012 int index = sattr->index;
3015 return PTR_ERR(data);
3017 return sprintf(buf, "%d\n",
3018 step_time_from_reg(data->fan_time[index][nr],
3019 data->pwm_mode[nr]));
3023 store_fan_time(struct device *dev, struct device_attribute *attr,
3024 const char *buf, size_t count)
3026 struct nct6775_data *data = dev_get_drvdata(dev);
3027 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
3029 int index = sattr->index;
3033 err = kstrtoul(buf, 10, &val);
3037 val = step_time_to_reg(val, data->pwm_mode[nr]);
3038 mutex_lock(&data->update_lock);
3039 data->fan_time[index][nr] = val;
3040 err = nct6775_write_value(data, data->REG_FAN_TIME[index][nr], val);
3041 mutex_unlock(&data->update_lock);
3042 return err ? : count;
3046 show_auto_pwm(struct device *dev, struct device_attribute *attr, char *buf)
3048 struct nct6775_data *data = nct6775_update_device(dev);
3049 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
3052 return PTR_ERR(data);
3054 return sprintf(buf, "%d\n", data->auto_pwm[sattr->nr][sattr->index]);
3058 store_auto_pwm(struct device *dev, struct device_attribute *attr,
3059 const char *buf, size_t count)
3061 struct nct6775_data *data = dev_get_drvdata(dev);
3062 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
3064 int point = sattr->index;
3069 err = kstrtoul(buf, 10, &val);
3075 if (point == data->auto_pwm_num) {
3076 if (data->kind != nct6775 && !val)
3078 if (data->kind != nct6779 && val)
3082 mutex_lock(&data->update_lock);
3083 data->auto_pwm[nr][point] = val;
3084 if (point < data->auto_pwm_num) {
3085 err = nct6775_write_value(data, NCT6775_AUTO_PWM(data, nr, point),
3086 data->auto_pwm[nr][point]);
3088 switch (data->kind) {
3090 /* disable if needed (pwm == 0) */
3091 err = nct6775_read_value(data, NCT6775_REG_CRITICAL_ENAB[nr], ®);
3098 err = nct6775_write_value(data, NCT6775_REG_CRITICAL_ENAB[nr], reg);
3101 break; /* always enabled, nothing to do */
3112 err = nct6775_write_value(data, data->REG_CRITICAL_PWM[nr], val);
3115 err = nct6775_read_value(data, data->REG_CRITICAL_PWM_ENABLE[nr], ®);
3119 reg &= ~data->CRITICAL_PWM_ENABLE_MASK;
3121 reg |= data->CRITICAL_PWM_ENABLE_MASK;
3122 err = nct6775_write_value(data, data->REG_CRITICAL_PWM_ENABLE[nr], reg);
3126 mutex_unlock(&data->update_lock);
3127 return err ? : count;
3131 show_auto_temp(struct device *dev, struct device_attribute *attr, char *buf)
3133 struct nct6775_data *data = nct6775_update_device(dev);
3134 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
3136 int point = sattr->index;
3139 return PTR_ERR(data);
3142 * We don't know for sure if the temperature is signed or unsigned.
3143 * Assume it is unsigned.
3145 return sprintf(buf, "%d\n", data->auto_temp[nr][point] * 1000);
3149 store_auto_temp(struct device *dev, struct device_attribute *attr,
3150 const char *buf, size_t count)
3152 struct nct6775_data *data = dev_get_drvdata(dev);
3153 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
3155 int point = sattr->index;
3159 err = kstrtoul(buf, 10, &val);
3165 mutex_lock(&data->update_lock);
3166 data->auto_temp[nr][point] = DIV_ROUND_CLOSEST(val, 1000);
3167 if (point < data->auto_pwm_num) {
3168 err = nct6775_write_value(data, NCT6775_AUTO_TEMP(data, nr, point),
3169 data->auto_temp[nr][point]);
3171 err = nct6775_write_value(data, data->REG_CRITICAL_TEMP[nr],
3172 data->auto_temp[nr][point]);
3174 mutex_unlock(&data->update_lock);
3175 return err ? : count;
3178 static umode_t nct6775_pwm_is_visible(struct kobject *kobj,
3179 struct attribute *attr, int index)
3181 struct device *dev = kobj_to_dev(kobj);
3182 struct nct6775_data *data = dev_get_drvdata(dev);
3183 int pwm = index / 36; /* pwm index */
3184 int nr = index % 36; /* attribute index */
3186 if (!(data->has_pwm & BIT(pwm)))
3189 if ((nr >= 14 && nr <= 18) || nr == 21) /* weight */
3190 if (!data->REG_WEIGHT_TEMP_SEL[pwm])
3192 if (nr == 19 && data->REG_PWM[3] == NULL) /* pwm_max */
3194 if (nr == 20 && data->REG_PWM[4] == NULL) /* pwm_step */
3196 if (nr == 21 && data->REG_PWM[6] == NULL) /* weight_duty_base */
3199 if (nr >= 22 && nr <= 35) { /* auto point */
3200 int api = (nr - 22) / 2; /* auto point index */
3202 if (api > data->auto_pwm_num)
3205 return nct6775_attr_mode(data, attr);
3208 SENSOR_TEMPLATE_2(pwm_stop_time, "pwm%d_stop_time", 0644, show_fan_time, store_fan_time, 0, 0);
3209 SENSOR_TEMPLATE_2(pwm_step_up_time, "pwm%d_step_up_time", 0644,
3210 show_fan_time, store_fan_time, 0, 1);
3211 SENSOR_TEMPLATE_2(pwm_step_down_time, "pwm%d_step_down_time", 0644,
3212 show_fan_time, store_fan_time, 0, 2);
3213 SENSOR_TEMPLATE_2(pwm_start, "pwm%d_start", 0644, show_pwm, store_pwm, 0, 1);
3214 SENSOR_TEMPLATE_2(pwm_floor, "pwm%d_floor", 0644, show_pwm, store_pwm, 0, 2);
3215 SENSOR_TEMPLATE_2(pwm_temp_tolerance, "pwm%d_temp_tolerance", 0644,
3216 show_temp_tolerance, store_temp_tolerance, 0, 0);
3217 SENSOR_TEMPLATE_2(pwm_crit_temp_tolerance, "pwm%d_crit_temp_tolerance",
3218 0644, show_temp_tolerance, store_temp_tolerance, 0, 1);
3220 SENSOR_TEMPLATE_2(pwm_max, "pwm%d_max", 0644, show_pwm, store_pwm, 0, 3);
3222 SENSOR_TEMPLATE_2(pwm_step, "pwm%d_step", 0644, show_pwm, store_pwm, 0, 4);
3224 SENSOR_TEMPLATE_2(pwm_auto_point1_pwm, "pwm%d_auto_point1_pwm",
3225 0644, show_auto_pwm, store_auto_pwm, 0, 0);
3226 SENSOR_TEMPLATE_2(pwm_auto_point1_temp, "pwm%d_auto_point1_temp",
3227 0644, show_auto_temp, store_auto_temp, 0, 0);
3229 SENSOR_TEMPLATE_2(pwm_auto_point2_pwm, "pwm%d_auto_point2_pwm",
3230 0644, show_auto_pwm, store_auto_pwm, 0, 1);
3231 SENSOR_TEMPLATE_2(pwm_auto_point2_temp, "pwm%d_auto_point2_temp",
3232 0644, show_auto_temp, store_auto_temp, 0, 1);
3234 SENSOR_TEMPLATE_2(pwm_auto_point3_pwm, "pwm%d_auto_point3_pwm",
3235 0644, show_auto_pwm, store_auto_pwm, 0, 2);
3236 SENSOR_TEMPLATE_2(pwm_auto_point3_temp, "pwm%d_auto_point3_temp",
3237 0644, show_auto_temp, store_auto_temp, 0, 2);
3239 SENSOR_TEMPLATE_2(pwm_auto_point4_pwm, "pwm%d_auto_point4_pwm",
3240 0644, show_auto_pwm, store_auto_pwm, 0, 3);
3241 SENSOR_TEMPLATE_2(pwm_auto_point4_temp, "pwm%d_auto_point4_temp",
3242 0644, show_auto_temp, store_auto_temp, 0, 3);
3244 SENSOR_TEMPLATE_2(pwm_auto_point5_pwm, "pwm%d_auto_point5_pwm",
3245 0644, show_auto_pwm, store_auto_pwm, 0, 4);
3246 SENSOR_TEMPLATE_2(pwm_auto_point5_temp, "pwm%d_auto_point5_temp",
3247 0644, show_auto_temp, store_auto_temp, 0, 4);
3249 SENSOR_TEMPLATE_2(pwm_auto_point6_pwm, "pwm%d_auto_point6_pwm",
3250 0644, show_auto_pwm, store_auto_pwm, 0, 5);
3251 SENSOR_TEMPLATE_2(pwm_auto_point6_temp, "pwm%d_auto_point6_temp",
3252 0644, show_auto_temp, store_auto_temp, 0, 5);
3254 SENSOR_TEMPLATE_2(pwm_auto_point7_pwm, "pwm%d_auto_point7_pwm",
3255 0644, show_auto_pwm, store_auto_pwm, 0, 6);
3256 SENSOR_TEMPLATE_2(pwm_auto_point7_temp, "pwm%d_auto_point7_temp",
3257 0644, show_auto_temp, store_auto_temp, 0, 6);
3260 * nct6775_pwm_is_visible uses the index into the following array
3261 * to determine if attributes should be created or not.
3262 * Any change in order or content must be matched.
3264 static struct sensor_device_template *nct6775_attributes_pwm_template[] = {
3265 &sensor_dev_template_pwm,
3266 &sensor_dev_template_pwm_mode,
3267 &sensor_dev_template_pwm_enable,
3268 &sensor_dev_template_pwm_temp_sel,
3269 &sensor_dev_template_pwm_temp_tolerance,
3270 &sensor_dev_template_pwm_crit_temp_tolerance,
3271 &sensor_dev_template_pwm_target_temp,
3272 &sensor_dev_template_fan_target,
3273 &sensor_dev_template_fan_tolerance,
3274 &sensor_dev_template_pwm_stop_time,
3275 &sensor_dev_template_pwm_step_up_time,
3276 &sensor_dev_template_pwm_step_down_time,
3277 &sensor_dev_template_pwm_start,
3278 &sensor_dev_template_pwm_floor,
3279 &sensor_dev_template_pwm_weight_temp_sel, /* 14 */
3280 &sensor_dev_template_pwm_weight_temp_step,
3281 &sensor_dev_template_pwm_weight_temp_step_tol,
3282 &sensor_dev_template_pwm_weight_temp_step_base,
3283 &sensor_dev_template_pwm_weight_duty_step, /* 18 */
3284 &sensor_dev_template_pwm_max, /* 19 */
3285 &sensor_dev_template_pwm_step, /* 20 */
3286 &sensor_dev_template_pwm_weight_duty_base, /* 21 */
3287 &sensor_dev_template_pwm_auto_point1_pwm, /* 22 */
3288 &sensor_dev_template_pwm_auto_point1_temp,
3289 &sensor_dev_template_pwm_auto_point2_pwm,
3290 &sensor_dev_template_pwm_auto_point2_temp,
3291 &sensor_dev_template_pwm_auto_point3_pwm,
3292 &sensor_dev_template_pwm_auto_point3_temp,
3293 &sensor_dev_template_pwm_auto_point4_pwm,
3294 &sensor_dev_template_pwm_auto_point4_temp,
3295 &sensor_dev_template_pwm_auto_point5_pwm,
3296 &sensor_dev_template_pwm_auto_point5_temp,
3297 &sensor_dev_template_pwm_auto_point6_pwm,
3298 &sensor_dev_template_pwm_auto_point6_temp,
3299 &sensor_dev_template_pwm_auto_point7_pwm,
3300 &sensor_dev_template_pwm_auto_point7_temp, /* 35 */
3305 static const struct sensor_template_group nct6775_pwm_template_group = {
3306 .templates = nct6775_attributes_pwm_template,
3307 .is_visible = nct6775_pwm_is_visible,
3311 static inline int nct6775_init_device(struct nct6775_data *data)
3316 /* Start monitoring if needed */
3317 if (data->REG_CONFIG) {
3318 err = nct6775_read_value(data, data->REG_CONFIG, &tmp);
3321 if (!(tmp & 0x01)) {
3322 err = nct6775_write_value(data, data->REG_CONFIG, tmp | 0x01);
3328 /* Enable temperature sensors if needed */
3329 for (i = 0; i < NUM_TEMP; i++) {
3330 if (!(data->have_temp & BIT(i)))
3332 if (!data->reg_temp_config[i])
3334 err = nct6775_read_value(data, data->reg_temp_config[i], &tmp);
3338 err = nct6775_write_value(data, data->reg_temp_config[i], tmp & 0xfe);
3344 /* Enable VBAT monitoring if needed */
3345 err = nct6775_read_value(data, data->REG_VBAT, &tmp);
3348 if (!(tmp & 0x01)) {
3349 err = nct6775_write_value(data, data->REG_VBAT, tmp | 0x01);
3354 err = nct6775_read_value(data, data->REG_DIODE, &diode);
3358 for (i = 0; i < data->temp_fixed_num; i++) {
3359 if (!(data->have_temp_fixed & BIT(i)))
3361 if ((tmp & (data->DIODE_MASK << i))) /* diode */
3363 = 3 - ((diode >> i) & data->DIODE_MASK);
3364 else /* thermistor */
3365 data->temp_type[i] = 4;
3371 static int add_temp_sensors(struct nct6775_data *data, const u16 *regp,
3372 int *available, int *mask)
3377 for (i = 0; i < data->pwm_num && *available; i++) {
3382 err = nct6775_read_value(data, regp[i], &src);
3386 if (!src || (*mask & BIT(src)))
3388 if (!(data->temp_mask & BIT(src)))
3391 index = __ffs(*available);
3392 err = nct6775_write_value(data, data->REG_TEMP_SOURCE[index], src);
3395 *available &= ~BIT(index);
3402 int nct6775_probe(struct device *dev, struct nct6775_data *data,
3403 const struct regmap_config *regmapcfg)
3406 int mask, available;
3408 const u16 *reg_temp, *reg_temp_over, *reg_temp_hyst, *reg_temp_config;
3409 const u16 *reg_temp_mon, *reg_temp_alternate, *reg_temp_crit;
3410 const u16 *reg_temp_crit_l = NULL, *reg_temp_crit_h = NULL;
3411 int num_reg_temp, num_reg_temp_mon, num_reg_tsi_temp;
3412 struct device *hwmon_dev;
3413 struct sensor_template_group tsi_temp_tg;
3415 data->regmap = devm_regmap_init(dev, NULL, data, regmapcfg);
3416 if (IS_ERR(data->regmap))
3417 return PTR_ERR(data->regmap);
3419 mutex_init(&data->update_lock);
3420 data->name = nct6775_device_names[data->kind];
3421 data->bank = 0xff; /* Force initial bank selection */
3423 switch (data->kind) {
3427 data->auto_pwm_num = 4;
3428 data->temp_fixed_num = 3;
3429 data->num_temp_alarms = 6;
3430 data->num_temp_beeps = 6;
3432 data->fan_from_reg = fan_from_reg13;
3433 data->fan_from_reg_min = fan_from_reg13;
3435 data->temp_label = nct6776_temp_label;
3436 data->temp_mask = NCT6776_TEMP_MASK;
3437 data->virt_temp_mask = NCT6776_VIRT_TEMP_MASK;
3439 data->REG_VBAT = NCT6106_REG_VBAT;
3440 data->REG_DIODE = NCT6106_REG_DIODE;
3441 data->DIODE_MASK = NCT6106_DIODE_MASK;
3442 data->REG_VIN = NCT6106_REG_IN;
3443 data->REG_IN_MINMAX[0] = NCT6106_REG_IN_MIN;
3444 data->REG_IN_MINMAX[1] = NCT6106_REG_IN_MAX;
3445 data->REG_TARGET = NCT6106_REG_TARGET;
3446 data->REG_FAN = NCT6106_REG_FAN;
3447 data->REG_FAN_MODE = NCT6106_REG_FAN_MODE;
3448 data->REG_FAN_MIN = NCT6106_REG_FAN_MIN;
3449 data->REG_FAN_PULSES = NCT6106_REG_FAN_PULSES;
3450 data->FAN_PULSE_SHIFT = NCT6106_FAN_PULSE_SHIFT;
3451 data->REG_FAN_TIME[0] = NCT6106_REG_FAN_STOP_TIME;
3452 data->REG_FAN_TIME[1] = NCT6106_REG_FAN_STEP_UP_TIME;
3453 data->REG_FAN_TIME[2] = NCT6106_REG_FAN_STEP_DOWN_TIME;
3454 data->REG_TOLERANCE_H = NCT6106_REG_TOLERANCE_H;
3455 data->REG_PWM[0] = NCT6116_REG_PWM;
3456 data->REG_PWM[1] = NCT6106_REG_FAN_START_OUTPUT;
3457 data->REG_PWM[2] = NCT6106_REG_FAN_STOP_OUTPUT;
3458 data->REG_PWM[5] = NCT6106_REG_WEIGHT_DUTY_STEP;
3459 data->REG_PWM[6] = NCT6106_REG_WEIGHT_DUTY_BASE;
3460 data->REG_PWM_READ = NCT6106_REG_PWM_READ;
3461 data->REG_PWM_MODE = NCT6106_REG_PWM_MODE;
3462 data->PWM_MODE_MASK = NCT6106_PWM_MODE_MASK;
3463 data->REG_AUTO_TEMP = NCT6106_REG_AUTO_TEMP;
3464 data->REG_AUTO_PWM = NCT6106_REG_AUTO_PWM;
3465 data->REG_CRITICAL_TEMP = NCT6106_REG_CRITICAL_TEMP;
3466 data->REG_CRITICAL_TEMP_TOLERANCE
3467 = NCT6106_REG_CRITICAL_TEMP_TOLERANCE;
3468 data->REG_CRITICAL_PWM_ENABLE = NCT6106_REG_CRITICAL_PWM_ENABLE;
3469 data->CRITICAL_PWM_ENABLE_MASK
3470 = NCT6106_CRITICAL_PWM_ENABLE_MASK;
3471 data->REG_CRITICAL_PWM = NCT6106_REG_CRITICAL_PWM;
3472 data->REG_TEMP_OFFSET = NCT6106_REG_TEMP_OFFSET;
3473 data->REG_TEMP_SOURCE = NCT6106_REG_TEMP_SOURCE;
3474 data->REG_TEMP_SEL = NCT6116_REG_TEMP_SEL;
3475 data->REG_WEIGHT_TEMP_SEL = NCT6106_REG_WEIGHT_TEMP_SEL;
3476 data->REG_WEIGHT_TEMP[0] = NCT6106_REG_WEIGHT_TEMP_STEP;
3477 data->REG_WEIGHT_TEMP[1] = NCT6106_REG_WEIGHT_TEMP_STEP_TOL;
3478 data->REG_WEIGHT_TEMP[2] = NCT6106_REG_WEIGHT_TEMP_BASE;
3479 data->REG_ALARM = NCT6106_REG_ALARM;
3480 data->ALARM_BITS = NCT6106_ALARM_BITS;
3481 data->REG_BEEP = NCT6106_REG_BEEP;
3482 data->BEEP_BITS = NCT6106_BEEP_BITS;
3483 data->REG_TSI_TEMP = NCT6106_REG_TSI_TEMP;
3485 reg_temp = NCT6106_REG_TEMP;
3486 reg_temp_mon = NCT6106_REG_TEMP_MON;
3487 num_reg_temp = ARRAY_SIZE(NCT6106_REG_TEMP);
3488 num_reg_temp_mon = ARRAY_SIZE(NCT6106_REG_TEMP_MON);
3489 num_reg_tsi_temp = ARRAY_SIZE(NCT6106_REG_TSI_TEMP);
3490 reg_temp_over = NCT6106_REG_TEMP_OVER;
3491 reg_temp_hyst = NCT6106_REG_TEMP_HYST;
3492 reg_temp_config = NCT6106_REG_TEMP_CONFIG;
3493 reg_temp_alternate = NCT6106_REG_TEMP_ALTERNATE;
3494 reg_temp_crit = NCT6106_REG_TEMP_CRIT;
3495 reg_temp_crit_l = NCT6106_REG_TEMP_CRIT_L;
3496 reg_temp_crit_h = NCT6106_REG_TEMP_CRIT_H;
3502 data->auto_pwm_num = 4;
3503 data->temp_fixed_num = 3;
3504 data->num_temp_alarms = 3;
3505 data->num_temp_beeps = 3;
3507 data->fan_from_reg = fan_from_reg13;
3508 data->fan_from_reg_min = fan_from_reg13;
3510 data->temp_label = nct6776_temp_label;
3511 data->temp_mask = NCT6776_TEMP_MASK;
3512 data->virt_temp_mask = NCT6776_VIRT_TEMP_MASK;
3514 data->REG_VBAT = NCT6106_REG_VBAT;
3515 data->REG_DIODE = NCT6106_REG_DIODE;
3516 data->DIODE_MASK = NCT6106_DIODE_MASK;
3517 data->REG_VIN = NCT6106_REG_IN;
3518 data->REG_IN_MINMAX[0] = NCT6106_REG_IN_MIN;
3519 data->REG_IN_MINMAX[1] = NCT6106_REG_IN_MAX;
3520 data->REG_TARGET = NCT6116_REG_TARGET;
3521 data->REG_FAN = NCT6116_REG_FAN;
3522 data->REG_FAN_MODE = NCT6116_REG_FAN_MODE;
3523 data->REG_FAN_MIN = NCT6116_REG_FAN_MIN;
3524 data->REG_FAN_PULSES = NCT6116_REG_FAN_PULSES;
3525 data->FAN_PULSE_SHIFT = NCT6116_FAN_PULSE_SHIFT;
3526 data->REG_FAN_TIME[0] = NCT6116_REG_FAN_STOP_TIME;
3527 data->REG_FAN_TIME[1] = NCT6116_REG_FAN_STEP_UP_TIME;
3528 data->REG_FAN_TIME[2] = NCT6116_REG_FAN_STEP_DOWN_TIME;
3529 data->REG_TOLERANCE_H = NCT6116_REG_TOLERANCE_H;
3530 data->REG_PWM[0] = NCT6116_REG_PWM;
3531 data->REG_PWM[1] = NCT6116_REG_FAN_START_OUTPUT;
3532 data->REG_PWM[2] = NCT6116_REG_FAN_STOP_OUTPUT;
3533 data->REG_PWM[5] = NCT6106_REG_WEIGHT_DUTY_STEP;
3534 data->REG_PWM[6] = NCT6106_REG_WEIGHT_DUTY_BASE;
3535 data->REG_PWM_READ = NCT6106_REG_PWM_READ;
3536 data->REG_PWM_MODE = NCT6106_REG_PWM_MODE;
3537 data->PWM_MODE_MASK = NCT6106_PWM_MODE_MASK;
3538 data->REG_AUTO_TEMP = NCT6116_REG_AUTO_TEMP;
3539 data->REG_AUTO_PWM = NCT6116_REG_AUTO_PWM;
3540 data->REG_CRITICAL_TEMP = NCT6116_REG_CRITICAL_TEMP;
3541 data->REG_CRITICAL_TEMP_TOLERANCE
3542 = NCT6116_REG_CRITICAL_TEMP_TOLERANCE;
3543 data->REG_CRITICAL_PWM_ENABLE = NCT6116_REG_CRITICAL_PWM_ENABLE;
3544 data->CRITICAL_PWM_ENABLE_MASK
3545 = NCT6106_CRITICAL_PWM_ENABLE_MASK;
3546 data->REG_CRITICAL_PWM = NCT6116_REG_CRITICAL_PWM;
3547 data->REG_TEMP_OFFSET = NCT6106_REG_TEMP_OFFSET;
3548 data->REG_TEMP_SOURCE = NCT6116_REG_TEMP_SOURCE;
3549 data->REG_TEMP_SEL = NCT6116_REG_TEMP_SEL;
3550 data->REG_WEIGHT_TEMP_SEL = NCT6106_REG_WEIGHT_TEMP_SEL;
3551 data->REG_WEIGHT_TEMP[0] = NCT6106_REG_WEIGHT_TEMP_STEP;
3552 data->REG_WEIGHT_TEMP[1] = NCT6106_REG_WEIGHT_TEMP_STEP_TOL;
3553 data->REG_WEIGHT_TEMP[2] = NCT6106_REG_WEIGHT_TEMP_BASE;
3554 data->REG_ALARM = NCT6106_REG_ALARM;
3555 data->ALARM_BITS = NCT6116_ALARM_BITS;
3556 data->REG_BEEP = NCT6106_REG_BEEP;
3557 data->BEEP_BITS = NCT6116_BEEP_BITS;
3558 data->REG_TSI_TEMP = NCT6116_REG_TSI_TEMP;
3560 reg_temp = NCT6106_REG_TEMP;
3561 reg_temp_mon = NCT6106_REG_TEMP_MON;
3562 num_reg_temp = ARRAY_SIZE(NCT6106_REG_TEMP);
3563 num_reg_temp_mon = ARRAY_SIZE(NCT6106_REG_TEMP_MON);
3564 num_reg_tsi_temp = ARRAY_SIZE(NCT6116_REG_TSI_TEMP);
3565 reg_temp_over = NCT6106_REG_TEMP_OVER;
3566 reg_temp_hyst = NCT6106_REG_TEMP_HYST;
3567 reg_temp_config = NCT6106_REG_TEMP_CONFIG;
3568 reg_temp_alternate = NCT6106_REG_TEMP_ALTERNATE;
3569 reg_temp_crit = NCT6106_REG_TEMP_CRIT;
3570 reg_temp_crit_l = NCT6106_REG_TEMP_CRIT_L;
3571 reg_temp_crit_h = NCT6106_REG_TEMP_CRIT_H;
3577 data->auto_pwm_num = 6;
3578 data->has_fan_div = true;
3579 data->temp_fixed_num = 3;
3580 data->num_temp_alarms = 3;
3581 data->num_temp_beeps = 3;
3583 data->ALARM_BITS = NCT6775_ALARM_BITS;
3584 data->BEEP_BITS = NCT6775_BEEP_BITS;
3586 data->fan_from_reg = fan_from_reg16;
3587 data->fan_from_reg_min = fan_from_reg8;
3588 data->target_temp_mask = 0x7f;
3589 data->tolerance_mask = 0x0f;
3590 data->speed_tolerance_limit = 15;
3592 data->temp_label = nct6775_temp_label;
3593 data->temp_mask = NCT6775_TEMP_MASK;
3594 data->virt_temp_mask = NCT6775_VIRT_TEMP_MASK;
3596 data->REG_CONFIG = NCT6775_REG_CONFIG;
3597 data->REG_VBAT = NCT6775_REG_VBAT;
3598 data->REG_DIODE = NCT6775_REG_DIODE;
3599 data->DIODE_MASK = NCT6775_DIODE_MASK;
3600 data->REG_VIN = NCT6775_REG_IN;
3601 data->REG_IN_MINMAX[0] = NCT6775_REG_IN_MIN;
3602 data->REG_IN_MINMAX[1] = NCT6775_REG_IN_MAX;
3603 data->REG_TARGET = NCT6775_REG_TARGET;
3604 data->REG_FAN = NCT6775_REG_FAN;
3605 data->REG_FAN_MODE = NCT6775_REG_FAN_MODE;
3606 data->REG_FAN_MIN = NCT6775_REG_FAN_MIN;
3607 data->REG_FAN_PULSES = NCT6775_REG_FAN_PULSES;
3608 data->FAN_PULSE_SHIFT = NCT6775_FAN_PULSE_SHIFT;
3609 data->REG_FAN_TIME[0] = NCT6775_REG_FAN_STOP_TIME;
3610 data->REG_FAN_TIME[1] = NCT6775_REG_FAN_STEP_UP_TIME;
3611 data->REG_FAN_TIME[2] = NCT6775_REG_FAN_STEP_DOWN_TIME;
3612 data->REG_PWM[0] = NCT6775_REG_PWM;
3613 data->REG_PWM[1] = NCT6775_REG_FAN_START_OUTPUT;
3614 data->REG_PWM[2] = NCT6775_REG_FAN_STOP_OUTPUT;
3615 data->REG_PWM[3] = NCT6775_REG_FAN_MAX_OUTPUT;
3616 data->REG_PWM[4] = NCT6775_REG_FAN_STEP_OUTPUT;
3617 data->REG_PWM[5] = NCT6775_REG_WEIGHT_DUTY_STEP;
3618 data->REG_PWM_READ = NCT6775_REG_PWM_READ;
3619 data->REG_PWM_MODE = NCT6775_REG_PWM_MODE;
3620 data->PWM_MODE_MASK = NCT6775_PWM_MODE_MASK;
3621 data->REG_AUTO_TEMP = NCT6775_REG_AUTO_TEMP;
3622 data->REG_AUTO_PWM = NCT6775_REG_AUTO_PWM;
3623 data->REG_CRITICAL_TEMP = NCT6775_REG_CRITICAL_TEMP;
3624 data->REG_CRITICAL_TEMP_TOLERANCE
3625 = NCT6775_REG_CRITICAL_TEMP_TOLERANCE;
3626 data->REG_TEMP_OFFSET = NCT6775_REG_TEMP_OFFSET;
3627 data->REG_TEMP_SOURCE = NCT6775_REG_TEMP_SOURCE;
3628 data->REG_TEMP_SEL = NCT6775_REG_TEMP_SEL;
3629 data->REG_WEIGHT_TEMP_SEL = NCT6775_REG_WEIGHT_TEMP_SEL;
3630 data->REG_WEIGHT_TEMP[0] = NCT6775_REG_WEIGHT_TEMP_STEP;
3631 data->REG_WEIGHT_TEMP[1] = NCT6775_REG_WEIGHT_TEMP_STEP_TOL;
3632 data->REG_WEIGHT_TEMP[2] = NCT6775_REG_WEIGHT_TEMP_BASE;
3633 data->REG_ALARM = NCT6775_REG_ALARM;
3634 data->REG_BEEP = NCT6775_REG_BEEP;
3635 data->REG_TSI_TEMP = NCT6775_REG_TSI_TEMP;
3637 reg_temp = NCT6775_REG_TEMP;
3638 reg_temp_mon = NCT6775_REG_TEMP_MON;
3639 num_reg_temp = ARRAY_SIZE(NCT6775_REG_TEMP);
3640 num_reg_temp_mon = ARRAY_SIZE(NCT6775_REG_TEMP_MON);
3641 num_reg_tsi_temp = ARRAY_SIZE(NCT6775_REG_TSI_TEMP);
3642 reg_temp_over = NCT6775_REG_TEMP_OVER;
3643 reg_temp_hyst = NCT6775_REG_TEMP_HYST;
3644 reg_temp_config = NCT6775_REG_TEMP_CONFIG;
3645 reg_temp_alternate = NCT6775_REG_TEMP_ALTERNATE;
3646 reg_temp_crit = NCT6775_REG_TEMP_CRIT;
3652 data->auto_pwm_num = 4;
3653 data->has_fan_div = false;
3654 data->temp_fixed_num = 3;
3655 data->num_temp_alarms = 3;
3656 data->num_temp_beeps = 6;
3658 data->ALARM_BITS = NCT6776_ALARM_BITS;
3659 data->BEEP_BITS = NCT6776_BEEP_BITS;
3661 data->fan_from_reg = fan_from_reg13;
3662 data->fan_from_reg_min = fan_from_reg13;
3663 data->target_temp_mask = 0xff;
3664 data->tolerance_mask = 0x07;
3665 data->speed_tolerance_limit = 63;
3667 data->temp_label = nct6776_temp_label;
3668 data->temp_mask = NCT6776_TEMP_MASK;
3669 data->virt_temp_mask = NCT6776_VIRT_TEMP_MASK;
3671 data->REG_CONFIG = NCT6775_REG_CONFIG;
3672 data->REG_VBAT = NCT6775_REG_VBAT;
3673 data->REG_DIODE = NCT6775_REG_DIODE;
3674 data->DIODE_MASK = NCT6775_DIODE_MASK;
3675 data->REG_VIN = NCT6775_REG_IN;
3676 data->REG_IN_MINMAX[0] = NCT6775_REG_IN_MIN;
3677 data->REG_IN_MINMAX[1] = NCT6775_REG_IN_MAX;
3678 data->REG_TARGET = NCT6775_REG_TARGET;
3679 data->REG_FAN = NCT6775_REG_FAN;
3680 data->REG_FAN_MODE = NCT6775_REG_FAN_MODE;
3681 data->REG_FAN_MIN = NCT6776_REG_FAN_MIN;
3682 data->REG_FAN_PULSES = NCT6776_REG_FAN_PULSES;
3683 data->FAN_PULSE_SHIFT = NCT6775_FAN_PULSE_SHIFT;
3684 data->REG_FAN_TIME[0] = NCT6775_REG_FAN_STOP_TIME;
3685 data->REG_FAN_TIME[1] = NCT6776_REG_FAN_STEP_UP_TIME;
3686 data->REG_FAN_TIME[2] = NCT6776_REG_FAN_STEP_DOWN_TIME;
3687 data->REG_TOLERANCE_H = NCT6776_REG_TOLERANCE_H;
3688 data->REG_PWM[0] = NCT6775_REG_PWM;
3689 data->REG_PWM[1] = NCT6775_REG_FAN_START_OUTPUT;
3690 data->REG_PWM[2] = NCT6775_REG_FAN_STOP_OUTPUT;
3691 data->REG_PWM[5] = NCT6775_REG_WEIGHT_DUTY_STEP;
3692 data->REG_PWM[6] = NCT6776_REG_WEIGHT_DUTY_BASE;
3693 data->REG_PWM_READ = NCT6775_REG_PWM_READ;
3694 data->REG_PWM_MODE = NCT6776_REG_PWM_MODE;
3695 data->PWM_MODE_MASK = NCT6776_PWM_MODE_MASK;
3696 data->REG_AUTO_TEMP = NCT6775_REG_AUTO_TEMP;
3697 data->REG_AUTO_PWM = NCT6775_REG_AUTO_PWM;
3698 data->REG_CRITICAL_TEMP = NCT6775_REG_CRITICAL_TEMP;
3699 data->REG_CRITICAL_TEMP_TOLERANCE
3700 = NCT6775_REG_CRITICAL_TEMP_TOLERANCE;
3701 data->REG_TEMP_OFFSET = NCT6775_REG_TEMP_OFFSET;
3702 data->REG_TEMP_SOURCE = NCT6775_REG_TEMP_SOURCE;
3703 data->REG_TEMP_SEL = NCT6775_REG_TEMP_SEL;
3704 data->REG_WEIGHT_TEMP_SEL = NCT6775_REG_WEIGHT_TEMP_SEL;
3705 data->REG_WEIGHT_TEMP[0] = NCT6775_REG_WEIGHT_TEMP_STEP;
3706 data->REG_WEIGHT_TEMP[1] = NCT6775_REG_WEIGHT_TEMP_STEP_TOL;
3707 data->REG_WEIGHT_TEMP[2] = NCT6775_REG_WEIGHT_TEMP_BASE;
3708 data->REG_ALARM = NCT6775_REG_ALARM;
3709 data->REG_BEEP = NCT6776_REG_BEEP;
3710 data->REG_TSI_TEMP = NCT6776_REG_TSI_TEMP;
3712 reg_temp = NCT6775_REG_TEMP;
3713 reg_temp_mon = NCT6775_REG_TEMP_MON;
3714 num_reg_temp = ARRAY_SIZE(NCT6775_REG_TEMP);
3715 num_reg_temp_mon = ARRAY_SIZE(NCT6775_REG_TEMP_MON);
3716 num_reg_tsi_temp = ARRAY_SIZE(NCT6776_REG_TSI_TEMP);
3717 reg_temp_over = NCT6775_REG_TEMP_OVER;
3718 reg_temp_hyst = NCT6775_REG_TEMP_HYST;
3719 reg_temp_config = NCT6776_REG_TEMP_CONFIG;
3720 reg_temp_alternate = NCT6776_REG_TEMP_ALTERNATE;
3721 reg_temp_crit = NCT6776_REG_TEMP_CRIT;
3727 data->auto_pwm_num = 4;
3728 data->has_fan_div = false;
3729 data->temp_fixed_num = 6;
3730 data->num_temp_alarms = 2;
3731 data->num_temp_beeps = 2;
3733 data->ALARM_BITS = NCT6779_ALARM_BITS;
3734 data->BEEP_BITS = NCT6779_BEEP_BITS;
3736 data->fan_from_reg = fan_from_reg_rpm;
3737 data->fan_from_reg_min = fan_from_reg13;
3738 data->target_temp_mask = 0xff;
3739 data->tolerance_mask = 0x07;
3740 data->speed_tolerance_limit = 63;
3742 data->temp_label = nct6779_temp_label;
3743 data->temp_mask = NCT6779_TEMP_MASK;
3744 data->virt_temp_mask = NCT6779_VIRT_TEMP_MASK;
3746 data->REG_CONFIG = NCT6775_REG_CONFIG;
3747 data->REG_VBAT = NCT6775_REG_VBAT;
3748 data->REG_DIODE = NCT6775_REG_DIODE;
3749 data->DIODE_MASK = NCT6775_DIODE_MASK;
3750 data->REG_VIN = NCT6779_REG_IN;
3751 data->REG_IN_MINMAX[0] = NCT6775_REG_IN_MIN;
3752 data->REG_IN_MINMAX[1] = NCT6775_REG_IN_MAX;
3753 data->REG_TARGET = NCT6775_REG_TARGET;
3754 data->REG_FAN = NCT6779_REG_FAN;
3755 data->REG_FAN_MODE = NCT6775_REG_FAN_MODE;
3756 data->REG_FAN_MIN = NCT6776_REG_FAN_MIN;
3757 data->REG_FAN_PULSES = NCT6779_REG_FAN_PULSES;
3758 data->FAN_PULSE_SHIFT = NCT6775_FAN_PULSE_SHIFT;
3759 data->REG_FAN_TIME[0] = NCT6775_REG_FAN_STOP_TIME;
3760 data->REG_FAN_TIME[1] = NCT6776_REG_FAN_STEP_UP_TIME;
3761 data->REG_FAN_TIME[2] = NCT6776_REG_FAN_STEP_DOWN_TIME;
3762 data->REG_TOLERANCE_H = NCT6776_REG_TOLERANCE_H;
3763 data->REG_PWM[0] = NCT6775_REG_PWM;
3764 data->REG_PWM[1] = NCT6775_REG_FAN_START_OUTPUT;
3765 data->REG_PWM[2] = NCT6775_REG_FAN_STOP_OUTPUT;
3766 data->REG_PWM[5] = NCT6775_REG_WEIGHT_DUTY_STEP;
3767 data->REG_PWM[6] = NCT6776_REG_WEIGHT_DUTY_BASE;
3768 data->REG_PWM_READ = NCT6775_REG_PWM_READ;
3769 data->REG_PWM_MODE = NCT6776_REG_PWM_MODE;
3770 data->PWM_MODE_MASK = NCT6776_PWM_MODE_MASK;
3771 data->REG_AUTO_TEMP = NCT6775_REG_AUTO_TEMP;
3772 data->REG_AUTO_PWM = NCT6775_REG_AUTO_PWM;
3773 data->REG_CRITICAL_TEMP = NCT6775_REG_CRITICAL_TEMP;
3774 data->REG_CRITICAL_TEMP_TOLERANCE
3775 = NCT6775_REG_CRITICAL_TEMP_TOLERANCE;
3776 data->REG_CRITICAL_PWM_ENABLE = NCT6779_REG_CRITICAL_PWM_ENABLE;
3777 data->CRITICAL_PWM_ENABLE_MASK
3778 = NCT6779_CRITICAL_PWM_ENABLE_MASK;
3779 data->REG_CRITICAL_PWM = NCT6779_REG_CRITICAL_PWM;
3780 data->REG_TEMP_OFFSET = NCT6779_REG_TEMP_OFFSET;
3781 data->REG_TEMP_SOURCE = NCT6775_REG_TEMP_SOURCE;
3782 data->REG_TEMP_SEL = NCT6775_REG_TEMP_SEL;
3783 data->REG_WEIGHT_TEMP_SEL = NCT6775_REG_WEIGHT_TEMP_SEL;
3784 data->REG_WEIGHT_TEMP[0] = NCT6775_REG_WEIGHT_TEMP_STEP;
3785 data->REG_WEIGHT_TEMP[1] = NCT6775_REG_WEIGHT_TEMP_STEP_TOL;
3786 data->REG_WEIGHT_TEMP[2] = NCT6775_REG_WEIGHT_TEMP_BASE;
3787 data->REG_ALARM = NCT6779_REG_ALARM;
3788 data->REG_BEEP = NCT6776_REG_BEEP;
3789 data->REG_TSI_TEMP = NCT6776_REG_TSI_TEMP;
3791 reg_temp = NCT6779_REG_TEMP;
3792 reg_temp_mon = NCT6779_REG_TEMP_MON;
3793 num_reg_temp = ARRAY_SIZE(NCT6779_REG_TEMP);
3794 num_reg_temp_mon = ARRAY_SIZE(NCT6779_REG_TEMP_MON);
3795 num_reg_tsi_temp = ARRAY_SIZE(NCT6776_REG_TSI_TEMP);
3796 reg_temp_over = NCT6779_REG_TEMP_OVER;
3797 reg_temp_hyst = NCT6779_REG_TEMP_HYST;
3798 reg_temp_config = NCT6779_REG_TEMP_CONFIG;
3799 reg_temp_alternate = NCT6779_REG_TEMP_ALTERNATE;
3800 reg_temp_crit = NCT6779_REG_TEMP_CRIT;
3811 data->pwm_num = (data->kind == nct6796 ||
3812 data->kind == nct6797 ||
3813 data->kind == nct6798) ? 7 : 6;
3814 data->auto_pwm_num = 4;
3815 data->has_fan_div = false;
3816 data->temp_fixed_num = 6;
3817 data->num_temp_alarms = 2;
3818 data->num_temp_beeps = 2;
3820 data->ALARM_BITS = NCT6791_ALARM_BITS;
3821 data->BEEP_BITS = NCT6779_BEEP_BITS;
3823 data->fan_from_reg = fan_from_reg_rpm;
3824 data->fan_from_reg_min = fan_from_reg13;
3825 data->target_temp_mask = 0xff;
3826 data->tolerance_mask = 0x07;
3827 data->speed_tolerance_limit = 63;
3829 switch (data->kind) {
3832 data->temp_label = nct6779_temp_label;
3833 data->temp_mask = NCT6791_TEMP_MASK;
3834 data->virt_temp_mask = NCT6791_VIRT_TEMP_MASK;
3837 data->temp_label = nct6792_temp_label;
3838 data->temp_mask = NCT6792_TEMP_MASK;
3839 data->virt_temp_mask = NCT6792_VIRT_TEMP_MASK;
3842 data->temp_label = nct6793_temp_label;
3843 data->temp_mask = NCT6793_TEMP_MASK;
3844 data->virt_temp_mask = NCT6793_VIRT_TEMP_MASK;
3848 data->temp_label = nct6795_temp_label;
3849 data->temp_mask = NCT6795_TEMP_MASK;
3850 data->virt_temp_mask = NCT6795_VIRT_TEMP_MASK;
3853 data->temp_label = nct6796_temp_label;
3854 data->temp_mask = NCT6796_TEMP_MASK;
3855 data->virt_temp_mask = NCT6796_VIRT_TEMP_MASK;
3858 data->temp_label = nct6798_temp_label;
3859 data->temp_mask = NCT6798_TEMP_MASK;
3860 data->virt_temp_mask = NCT6798_VIRT_TEMP_MASK;
3864 data->REG_CONFIG = NCT6775_REG_CONFIG;
3865 data->REG_VBAT = NCT6775_REG_VBAT;
3866 data->REG_DIODE = NCT6775_REG_DIODE;
3867 data->DIODE_MASK = NCT6775_DIODE_MASK;
3868 data->REG_VIN = NCT6779_REG_IN;
3869 data->REG_IN_MINMAX[0] = NCT6775_REG_IN_MIN;
3870 data->REG_IN_MINMAX[1] = NCT6775_REG_IN_MAX;
3871 data->REG_TARGET = NCT6775_REG_TARGET;
3872 data->REG_FAN = NCT6779_REG_FAN;
3873 data->REG_FAN_MODE = NCT6775_REG_FAN_MODE;
3874 data->REG_FAN_MIN = NCT6776_REG_FAN_MIN;
3875 data->REG_FAN_PULSES = NCT6779_REG_FAN_PULSES;
3876 data->FAN_PULSE_SHIFT = NCT6775_FAN_PULSE_SHIFT;
3877 data->REG_FAN_TIME[0] = NCT6775_REG_FAN_STOP_TIME;
3878 data->REG_FAN_TIME[1] = NCT6776_REG_FAN_STEP_UP_TIME;
3879 data->REG_FAN_TIME[2] = NCT6776_REG_FAN_STEP_DOWN_TIME;
3880 data->REG_TOLERANCE_H = NCT6776_REG_TOLERANCE_H;
3881 data->REG_PWM[0] = NCT6775_REG_PWM;
3882 data->REG_PWM[1] = NCT6775_REG_FAN_START_OUTPUT;
3883 data->REG_PWM[2] = NCT6775_REG_FAN_STOP_OUTPUT;
3884 data->REG_PWM[5] = NCT6791_REG_WEIGHT_DUTY_STEP;
3885 data->REG_PWM[6] = NCT6791_REG_WEIGHT_DUTY_BASE;
3886 data->REG_PWM_READ = NCT6775_REG_PWM_READ;
3887 data->REG_PWM_MODE = NCT6776_REG_PWM_MODE;
3888 data->PWM_MODE_MASK = NCT6776_PWM_MODE_MASK;
3889 data->REG_AUTO_TEMP = NCT6775_REG_AUTO_TEMP;
3890 data->REG_AUTO_PWM = NCT6775_REG_AUTO_PWM;
3891 data->REG_CRITICAL_TEMP = NCT6775_REG_CRITICAL_TEMP;
3892 data->REG_CRITICAL_TEMP_TOLERANCE
3893 = NCT6775_REG_CRITICAL_TEMP_TOLERANCE;
3894 data->REG_CRITICAL_PWM_ENABLE = NCT6779_REG_CRITICAL_PWM_ENABLE;
3895 data->CRITICAL_PWM_ENABLE_MASK
3896 = NCT6779_CRITICAL_PWM_ENABLE_MASK;
3897 data->REG_CRITICAL_PWM = NCT6779_REG_CRITICAL_PWM;
3898 data->REG_TEMP_OFFSET = NCT6779_REG_TEMP_OFFSET;
3899 data->REG_TEMP_SOURCE = NCT6775_REG_TEMP_SOURCE;
3900 data->REG_TEMP_SEL = NCT6775_REG_TEMP_SEL;
3901 data->REG_WEIGHT_TEMP_SEL = NCT6791_REG_WEIGHT_TEMP_SEL;
3902 data->REG_WEIGHT_TEMP[0] = NCT6791_REG_WEIGHT_TEMP_STEP;
3903 data->REG_WEIGHT_TEMP[1] = NCT6791_REG_WEIGHT_TEMP_STEP_TOL;
3904 data->REG_WEIGHT_TEMP[2] = NCT6791_REG_WEIGHT_TEMP_BASE;
3905 data->REG_ALARM = NCT6791_REG_ALARM;
3906 if (data->kind == nct6791)
3907 data->REG_BEEP = NCT6776_REG_BEEP;
3909 data->REG_BEEP = NCT6792_REG_BEEP;
3910 switch (data->kind) {
3914 data->REG_TSI_TEMP = NCT6776_REG_TSI_TEMP;
3915 num_reg_tsi_temp = ARRAY_SIZE(NCT6776_REG_TSI_TEMP);
3921 data->REG_TSI_TEMP = NCT6796_REG_TSI_TEMP;
3922 num_reg_tsi_temp = ARRAY_SIZE(NCT6796_REG_TSI_TEMP);
3925 num_reg_tsi_temp = 0;
3929 reg_temp = NCT6779_REG_TEMP;
3930 num_reg_temp = ARRAY_SIZE(NCT6779_REG_TEMP);
3931 if (data->kind == nct6791) {
3932 reg_temp_mon = NCT6779_REG_TEMP_MON;
3933 num_reg_temp_mon = ARRAY_SIZE(NCT6779_REG_TEMP_MON);
3935 reg_temp_mon = NCT6792_REG_TEMP_MON;
3936 num_reg_temp_mon = ARRAY_SIZE(NCT6792_REG_TEMP_MON);
3938 reg_temp_over = NCT6779_REG_TEMP_OVER;
3939 reg_temp_hyst = NCT6779_REG_TEMP_HYST;
3940 reg_temp_config = NCT6779_REG_TEMP_CONFIG;
3941 reg_temp_alternate = NCT6779_REG_TEMP_ALTERNATE;
3942 reg_temp_crit = NCT6779_REG_TEMP_CRIT;
3948 data->have_in = BIT(data->in_num) - 1;
3949 data->have_temp = 0;
3952 * On some boards, not all available temperature sources are monitored,
3953 * even though some of the monitoring registers are unused.
3954 * Get list of unused monitoring registers, then detect if any fan
3955 * controls are configured to use unmonitored temperature sources.
3956 * If so, assign the unmonitored temperature sources to available
3957 * monitoring registers.
3961 for (i = 0; i < num_reg_temp; i++) {
3962 if (reg_temp[i] == 0)
3965 err = nct6775_read_value(data, data->REG_TEMP_SOURCE[i], &src);
3969 if (!src || (mask & BIT(src)))
3970 available |= BIT(i);
3976 * Now find unmonitored temperature registers and enable monitoring
3977 * if additional monitoring registers are available.
3979 err = add_temp_sensors(data, data->REG_TEMP_SEL, &available, &mask);
3982 err = add_temp_sensors(data, data->REG_WEIGHT_TEMP_SEL, &available, &mask);
3987 s = NUM_TEMP_FIXED; /* First dynamic temperature attribute */
3988 for (i = 0; i < num_reg_temp; i++) {
3989 if (reg_temp[i] == 0)
3992 err = nct6775_read_value(data, data->REG_TEMP_SOURCE[i], &src);
3996 if (!src || (mask & BIT(src)))
3999 if (!(data->temp_mask & BIT(src))) {
4001 "Invalid temperature source %d at index %d, source register 0x%x, temp register 0x%x\n",
4002 src, i, data->REG_TEMP_SOURCE[i], reg_temp[i]);
4008 /* Use fixed index for SYSTIN(1), CPUTIN(2), AUXTIN(3) */
4009 if (src <= data->temp_fixed_num) {
4010 data->have_temp |= BIT(src - 1);
4011 data->have_temp_fixed |= BIT(src - 1);
4012 data->reg_temp[0][src - 1] = reg_temp[i];
4013 data->reg_temp[1][src - 1] = reg_temp_over[i];
4014 data->reg_temp[2][src - 1] = reg_temp_hyst[i];
4015 if (reg_temp_crit_h && reg_temp_crit_h[i])
4016 data->reg_temp[3][src - 1] = reg_temp_crit_h[i];
4017 else if (reg_temp_crit[src - 1])
4018 data->reg_temp[3][src - 1]
4019 = reg_temp_crit[src - 1];
4020 if (reg_temp_crit_l && reg_temp_crit_l[i])
4021 data->reg_temp[4][src - 1] = reg_temp_crit_l[i];
4022 data->reg_temp_config[src - 1] = reg_temp_config[i];
4023 data->temp_src[src - 1] = src;
4030 /* Use dynamic index for other sources */
4031 data->have_temp |= BIT(s);
4032 data->reg_temp[0][s] = reg_temp[i];
4033 data->reg_temp[1][s] = reg_temp_over[i];
4034 data->reg_temp[2][s] = reg_temp_hyst[i];
4035 data->reg_temp_config[s] = reg_temp_config[i];
4036 if (reg_temp_crit_h && reg_temp_crit_h[i])
4037 data->reg_temp[3][s] = reg_temp_crit_h[i];
4038 else if (reg_temp_crit[src - 1])
4039 data->reg_temp[3][s] = reg_temp_crit[src - 1];
4040 if (reg_temp_crit_l && reg_temp_crit_l[i])
4041 data->reg_temp[4][s] = reg_temp_crit_l[i];
4043 data->temp_src[s] = src;
4048 * Repeat with temperatures used for fan control.
4049 * This set of registers does not support limits.
4051 for (i = 0; i < num_reg_temp_mon; i++) {
4052 if (reg_temp_mon[i] == 0)
4055 err = nct6775_read_value(data, data->REG_TEMP_SEL[i], &src);
4062 if (!(data->temp_mask & BIT(src))) {
4064 "Invalid temperature source %d at index %d, source register 0x%x, temp register 0x%x\n",
4065 src, i, data->REG_TEMP_SEL[i],
4071 * For virtual temperature sources, the 'virtual' temperature
4072 * for each fan reflects a different temperature, and there
4073 * are no duplicates.
4075 if (!(data->virt_temp_mask & BIT(src))) {
4076 if (mask & BIT(src))
4081 /* Use fixed index for SYSTIN(1), CPUTIN(2), AUXTIN(3) */
4082 if (src <= data->temp_fixed_num) {
4083 if (data->have_temp & BIT(src - 1))
4085 data->have_temp |= BIT(src - 1);
4086 data->have_temp_fixed |= BIT(src - 1);
4087 data->reg_temp[0][src - 1] = reg_temp_mon[i];
4088 data->temp_src[src - 1] = src;
4095 /* Use dynamic index for other sources */
4096 data->have_temp |= BIT(s);
4097 data->reg_temp[0][s] = reg_temp_mon[i];
4098 data->temp_src[s] = src;
4102 #ifdef USE_ALTERNATE
4104 * Go through the list of alternate temp registers and enable
4106 * The temperature is already monitored if the respective bit in <mask>
4109 for (i = 0; i < 31; i++) {
4110 if (!(data->temp_mask & BIT(i + 1)))
4112 if (!reg_temp_alternate[i])
4114 if (mask & BIT(i + 1))
4116 if (i < data->temp_fixed_num) {
4117 if (data->have_temp & BIT(i))
4119 data->have_temp |= BIT(i);
4120 data->have_temp_fixed |= BIT(i);
4121 data->reg_temp[0][i] = reg_temp_alternate[i];
4122 if (i < num_reg_temp) {
4123 data->reg_temp[1][i] = reg_temp_over[i];
4124 data->reg_temp[2][i] = reg_temp_hyst[i];
4126 data->temp_src[i] = i + 1;
4130 if (s >= NUM_TEMP) /* Abort if no more space */
4133 data->have_temp |= BIT(s);
4134 data->reg_temp[0][s] = reg_temp_alternate[i];
4135 data->temp_src[s] = i + 1;
4138 #endif /* USE_ALTERNATE */
4140 /* Check which TSIx_TEMP registers are active */
4141 for (i = 0; i < num_reg_tsi_temp; i++) {
4144 err = nct6775_read_value(data, data->REG_TSI_TEMP[i], &tmp);
4148 data->have_tsi_temp |= BIT(i);
4151 /* Initialize the chip */
4152 err = nct6775_init_device(data);
4156 if (data->driver_init) {
4157 err = data->driver_init(data);
4162 /* Read fan clock dividers immediately */
4163 err = nct6775_init_fan_common(dev, data);
4167 /* Register sysfs hooks */
4168 err = nct6775_add_template_attr_group(dev, data, &nct6775_pwm_template_group,
4173 err = nct6775_add_template_attr_group(dev, data, &nct6775_in_template_group,
4174 fls(data->have_in));
4178 err = nct6775_add_template_attr_group(dev, data, &nct6775_fan_template_group,
4179 fls(data->has_fan));
4183 err = nct6775_add_template_attr_group(dev, data, &nct6775_temp_template_group,
4184 fls(data->have_temp));
4188 if (data->have_tsi_temp) {
4189 tsi_temp_tg.templates = nct6775_tsi_temp_template;
4190 tsi_temp_tg.is_visible = nct6775_tsi_temp_is_visible;
4191 tsi_temp_tg.base = fls(data->have_temp) + 1;
4192 err = nct6775_add_template_attr_group(dev, data, &tsi_temp_tg,
4193 fls(data->have_tsi_temp));
4198 hwmon_dev = devm_hwmon_device_register_with_groups(dev, data->name,
4199 data, data->groups);
4200 return PTR_ERR_OR_ZERO(hwmon_dev);
4202 EXPORT_SYMBOL_GPL(nct6775_probe);
4205 MODULE_DESCRIPTION("Core driver for NCT6775F and compatible chips");
4206 MODULE_LICENSE("GPL");