2 * nct6775 - Driver for the hardware monitoring functionality of
3 * Nuvoton NCT677x Super-I/O chips
5 * Copyright (C) 2012 Guenter Roeck <linux@roeck-us.net>
7 * Derived from w83627ehf driver
8 * Copyright (C) 2005-2012 Jean Delvare <jdelvare@suse.de>
9 * Copyright (C) 2006 Yuan Mu (Winbond),
10 * Rudolf Marek <r.marek@assembler.cz>
11 * David Hubbard <david.c.hubbard@gmail.com>
12 * Daniel J Blueman <daniel.blueman@gmail.com>
13 * Copyright (C) 2010 Sheng-Yuan Huang (Nuvoton) (PS00)
15 * Shamelessly ripped from the w83627hf driver
16 * Copyright (C) 2003 Mark Studebaker
18 * This program is free software; you can redistribute it and/or modify
19 * it under the terms of the GNU General Public License as published by
20 * the Free Software Foundation; either version 2 of the License, or
21 * (at your option) any later version.
23 * This program is distributed in the hope that it will be useful,
24 * but WITHOUT ANY WARRANTY; without even the implied warranty of
25 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
26 * GNU General Public License for more details.
28 * You should have received a copy of the GNU General Public License
29 * along with this program; if not, write to the Free Software
30 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
33 * Supports the following chips:
35 * Chip #vin #fan #pwm #temp chip IDs man ID
36 * nct6106d 9 3 3 6+3 0xc450 0xc1 0x5ca3
37 * nct6775f 9 4 3 6+3 0xb470 0xc1 0x5ca3
38 * nct6776f 9 5 3 6+3 0xc330 0xc1 0x5ca3
39 * nct6779d 15 5 5 2+6 0xc560 0xc1 0x5ca3
40 * nct6791d 15 6 6 2+6 0xc800 0xc1 0x5ca3
41 * nct6792d 15 6 6 2+6 0xc910 0xc1 0x5ca3
42 * nct6793d 15 6 6 2+6 0xd120 0xc1 0x5ca3
44 * #temp lists the number of monitored temperature sources (first value) plus
45 * the number of directly connectable temperature sensors (second value).
48 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
50 #include <linux/module.h>
51 #include <linux/init.h>
52 #include <linux/slab.h>
53 #include <linux/jiffies.h>
54 #include <linux/platform_device.h>
55 #include <linux/hwmon.h>
56 #include <linux/hwmon-sysfs.h>
57 #include <linux/hwmon-vid.h>
58 #include <linux/err.h>
59 #include <linux/mutex.h>
60 #include <linux/acpi.h>
61 #include <linux/dmi.h>
68 enum kinds { nct6106, nct6775, nct6776, nct6779, nct6791, nct6792, nct6793 };
70 /* used to set data->name = nct6775_device_names[data->sio_kind] */
71 static const char * const nct6775_device_names[] = {
81 static const char * const nct6775_sio_names[] __initconst = {
91 static unsigned short force_id;
92 module_param(force_id, ushort, 0);
93 MODULE_PARM_DESC(force_id, "Override the detected device ID");
95 static unsigned short fan_debounce;
96 module_param(fan_debounce, ushort, 0);
97 MODULE_PARM_DESC(fan_debounce, "Enable debouncing for fan RPM signal");
99 #define DRVNAME "nct6775"
102 * Super-I/O constants and functions
105 #define NCT6775_LD_ACPI 0x0a
106 #define NCT6775_LD_HWM 0x0b
107 #define NCT6775_LD_VID 0x0d
109 #define SIO_REG_LDSEL 0x07 /* Logical device select */
110 #define SIO_REG_DEVID 0x20 /* Device ID (2 bytes) */
111 #define SIO_REG_ENABLE 0x30 /* Logical device enable */
112 #define SIO_REG_ADDR 0x60 /* Logical device address (2 bytes) */
114 #define SIO_NCT6106_ID 0xc450
115 #define SIO_NCT6775_ID 0xb470
116 #define SIO_NCT6776_ID 0xc330
117 #define SIO_NCT6779_ID 0xc560
118 #define SIO_NCT6791_ID 0xc800
119 #define SIO_NCT6792_ID 0xc910
120 #define SIO_NCT6793_ID 0xd120
121 #define SIO_ID_MASK 0xFFF0
123 enum pwm_enable { off, manual, thermal_cruise, speed_cruise, sf3, sf4 };
126 superio_outb(int ioreg, int reg, int val)
129 outb(val, ioreg + 1);
133 superio_inb(int ioreg, int reg)
136 return inb(ioreg + 1);
140 superio_select(int ioreg, int ld)
142 outb(SIO_REG_LDSEL, ioreg);
147 superio_enter(int ioreg)
150 * Try to reserve <ioreg> and <ioreg + 1> for exclusive access.
152 if (!request_muxed_region(ioreg, 2, DRVNAME))
162 superio_exit(int ioreg)
166 outb(0x02, ioreg + 1);
167 release_region(ioreg, 2);
174 #define IOREGION_ALIGNMENT (~7)
175 #define IOREGION_OFFSET 5
176 #define IOREGION_LENGTH 2
177 #define ADDR_REG_OFFSET 0
178 #define DATA_REG_OFFSET 1
180 #define NCT6775_REG_BANK 0x4E
181 #define NCT6775_REG_CONFIG 0x40
184 * Not currently used:
185 * REG_MAN_ID has the value 0x5ca3 for all supported chips.
186 * REG_CHIP_ID == 0x88/0xa1/0xc1 depending on chip model.
187 * REG_MAN_ID is at port 0x4f
188 * REG_CHIP_ID is at port 0x58
191 #define NUM_TEMP 10 /* Max number of temp attribute sets w/ limits*/
192 #define NUM_TEMP_FIXED 6 /* Max number of fixed temp attribute sets */
194 #define NUM_REG_ALARM 7 /* Max number of alarm registers */
195 #define NUM_REG_BEEP 5 /* Max number of beep registers */
199 /* Common and NCT6775 specific data */
201 /* Voltage min/max registers for nr=7..14 are in bank 5 */
203 static const u16 NCT6775_REG_IN_MAX[] = {
204 0x2b, 0x2d, 0x2f, 0x31, 0x33, 0x35, 0x37, 0x554, 0x556, 0x558, 0x55a,
205 0x55c, 0x55e, 0x560, 0x562 };
206 static const u16 NCT6775_REG_IN_MIN[] = {
207 0x2c, 0x2e, 0x30, 0x32, 0x34, 0x36, 0x38, 0x555, 0x557, 0x559, 0x55b,
208 0x55d, 0x55f, 0x561, 0x563 };
209 static const u16 NCT6775_REG_IN[] = {
210 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x550, 0x551, 0x552
213 #define NCT6775_REG_VBAT 0x5D
214 #define NCT6775_REG_DIODE 0x5E
215 #define NCT6775_DIODE_MASK 0x02
217 #define NCT6775_REG_FANDIV1 0x506
218 #define NCT6775_REG_FANDIV2 0x507
220 #define NCT6775_REG_CR_FAN_DEBOUNCE 0xf0
222 static const u16 NCT6775_REG_ALARM[NUM_REG_ALARM] = { 0x459, 0x45A, 0x45B };
224 /* 0..15 voltages, 16..23 fans, 24..29 temperatures, 30..31 intrusion */
226 static const s8 NCT6775_ALARM_BITS[] = {
227 0, 1, 2, 3, 8, 21, 20, 16, /* in0.. in7 */
228 17, -1, -1, -1, -1, -1, -1, /* in8..in14 */
230 6, 7, 11, -1, -1, /* fan1..fan5 */
231 -1, -1, -1, /* unused */
232 4, 5, 13, -1, -1, -1, /* temp1..temp6 */
233 12, -1 }; /* intrusion0, intrusion1 */
235 #define FAN_ALARM_BASE 16
236 #define TEMP_ALARM_BASE 24
237 #define INTRUSION_ALARM_BASE 30
239 static const u16 NCT6775_REG_BEEP[NUM_REG_BEEP] = { 0x56, 0x57, 0x453, 0x4e };
242 * 0..14 voltages, 15 global beep enable, 16..23 fans, 24..29 temperatures,
245 static const s8 NCT6775_BEEP_BITS[] = {
246 0, 1, 2, 3, 8, 9, 10, 16, /* in0.. in7 */
247 17, -1, -1, -1, -1, -1, -1, /* in8..in14 */
248 21, /* global beep enable */
249 6, 7, 11, 28, -1, /* fan1..fan5 */
250 -1, -1, -1, /* unused */
251 4, 5, 13, -1, -1, -1, /* temp1..temp6 */
252 12, -1 }; /* intrusion0, intrusion1 */
254 #define BEEP_ENABLE_BASE 15
256 static const u8 NCT6775_REG_CR_CASEOPEN_CLR[] = { 0xe6, 0xee };
257 static const u8 NCT6775_CR_CASEOPEN_CLR_MASK[] = { 0x20, 0x01 };
259 /* DC or PWM output fan configuration */
260 static const u8 NCT6775_REG_PWM_MODE[] = { 0x04, 0x04, 0x12 };
261 static const u8 NCT6775_PWM_MODE_MASK[] = { 0x01, 0x02, 0x01 };
263 /* Advanced Fan control, some values are common for all fans */
265 static const u16 NCT6775_REG_TARGET[] = {
266 0x101, 0x201, 0x301, 0x801, 0x901, 0xa01 };
267 static const u16 NCT6775_REG_FAN_MODE[] = {
268 0x102, 0x202, 0x302, 0x802, 0x902, 0xa02 };
269 /* STEP_DOWN_TIME and STEP_UP_TIME regs are swapped for all chips but NCT6775 */
270 static const u16 NCT6775_REG_FAN_STEP_DOWN_TIME[] = {
271 0x103, 0x203, 0x303, 0x803, 0x903, 0xa03 };
272 static const u16 NCT6775_REG_FAN_STEP_UP_TIME[] = {
273 0x104, 0x204, 0x304, 0x804, 0x904, 0xa04 };
274 static const u16 NCT6775_REG_FAN_STOP_OUTPUT[] = {
275 0x105, 0x205, 0x305, 0x805, 0x905, 0xa05 };
276 static const u16 NCT6775_REG_FAN_START_OUTPUT[] = {
277 0x106, 0x206, 0x306, 0x806, 0x906, 0xa06 };
278 static const u16 NCT6775_REG_FAN_MAX_OUTPUT[] = { 0x10a, 0x20a, 0x30a };
279 static const u16 NCT6775_REG_FAN_STEP_OUTPUT[] = { 0x10b, 0x20b, 0x30b };
281 static const u16 NCT6775_REG_FAN_STOP_TIME[] = {
282 0x107, 0x207, 0x307, 0x807, 0x907, 0xa07 };
283 static const u16 NCT6775_REG_PWM[] = {
284 0x109, 0x209, 0x309, 0x809, 0x909, 0xa09 };
285 static const u16 NCT6775_REG_PWM_READ[] = {
286 0x01, 0x03, 0x11, 0x13, 0x15, 0xa09 };
288 static const u16 NCT6775_REG_FAN[] = { 0x630, 0x632, 0x634, 0x636, 0x638 };
289 static const u16 NCT6775_REG_FAN_MIN[] = { 0x3b, 0x3c, 0x3d };
290 static const u16 NCT6775_REG_FAN_PULSES[] = { 0x641, 0x642, 0x643, 0x644, 0 };
291 static const u16 NCT6775_FAN_PULSE_SHIFT[] = { 0, 0, 0, 0, 0, 0 };
293 static const u16 NCT6775_REG_TEMP[] = {
294 0x27, 0x150, 0x250, 0x62b, 0x62c, 0x62d };
296 static const u16 NCT6775_REG_TEMP_MON[] = { 0x73, 0x75, 0x77 };
298 static const u16 NCT6775_REG_TEMP_CONFIG[ARRAY_SIZE(NCT6775_REG_TEMP)] = {
299 0, 0x152, 0x252, 0x628, 0x629, 0x62A };
300 static const u16 NCT6775_REG_TEMP_HYST[ARRAY_SIZE(NCT6775_REG_TEMP)] = {
301 0x3a, 0x153, 0x253, 0x673, 0x678, 0x67D };
302 static const u16 NCT6775_REG_TEMP_OVER[ARRAY_SIZE(NCT6775_REG_TEMP)] = {
303 0x39, 0x155, 0x255, 0x672, 0x677, 0x67C };
305 static const u16 NCT6775_REG_TEMP_SOURCE[ARRAY_SIZE(NCT6775_REG_TEMP)] = {
306 0x621, 0x622, 0x623, 0x624, 0x625, 0x626 };
308 static const u16 NCT6775_REG_TEMP_SEL[] = {
309 0x100, 0x200, 0x300, 0x800, 0x900, 0xa00 };
311 static const u16 NCT6775_REG_WEIGHT_TEMP_SEL[] = {
312 0x139, 0x239, 0x339, 0x839, 0x939, 0xa39 };
313 static const u16 NCT6775_REG_WEIGHT_TEMP_STEP[] = {
314 0x13a, 0x23a, 0x33a, 0x83a, 0x93a, 0xa3a };
315 static const u16 NCT6775_REG_WEIGHT_TEMP_STEP_TOL[] = {
316 0x13b, 0x23b, 0x33b, 0x83b, 0x93b, 0xa3b };
317 static const u16 NCT6775_REG_WEIGHT_DUTY_STEP[] = {
318 0x13c, 0x23c, 0x33c, 0x83c, 0x93c, 0xa3c };
319 static const u16 NCT6775_REG_WEIGHT_TEMP_BASE[] = {
320 0x13d, 0x23d, 0x33d, 0x83d, 0x93d, 0xa3d };
322 static const u16 NCT6775_REG_TEMP_OFFSET[] = { 0x454, 0x455, 0x456 };
324 static const u16 NCT6775_REG_AUTO_TEMP[] = {
325 0x121, 0x221, 0x321, 0x821, 0x921, 0xa21 };
326 static const u16 NCT6775_REG_AUTO_PWM[] = {
327 0x127, 0x227, 0x327, 0x827, 0x927, 0xa27 };
329 #define NCT6775_AUTO_TEMP(data, nr, p) ((data)->REG_AUTO_TEMP[nr] + (p))
330 #define NCT6775_AUTO_PWM(data, nr, p) ((data)->REG_AUTO_PWM[nr] + (p))
332 static const u16 NCT6775_REG_CRITICAL_ENAB[] = { 0x134, 0x234, 0x334 };
334 static const u16 NCT6775_REG_CRITICAL_TEMP[] = {
335 0x135, 0x235, 0x335, 0x835, 0x935, 0xa35 };
336 static const u16 NCT6775_REG_CRITICAL_TEMP_TOLERANCE[] = {
337 0x138, 0x238, 0x338, 0x838, 0x938, 0xa38 };
339 static const char *const nct6775_temp_label[] = {
353 "PCH_CHIP_CPU_MAX_TEMP",
363 static const u16 NCT6775_REG_TEMP_ALTERNATE[ARRAY_SIZE(nct6775_temp_label) - 1]
364 = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0x661, 0x662, 0x664 };
366 static const u16 NCT6775_REG_TEMP_CRIT[ARRAY_SIZE(nct6775_temp_label) - 1]
367 = { 0, 0, 0, 0, 0xa00, 0xa01, 0xa02, 0xa03, 0xa04, 0xa05, 0xa06,
370 /* NCT6776 specific data */
372 static const s8 NCT6776_ALARM_BITS[] = {
373 0, 1, 2, 3, 8, 21, 20, 16, /* in0.. in7 */
374 17, -1, -1, -1, -1, -1, -1, /* in8..in14 */
376 6, 7, 11, 10, 23, /* fan1..fan5 */
377 -1, -1, -1, /* unused */
378 4, 5, 13, -1, -1, -1, /* temp1..temp6 */
379 12, 9 }; /* intrusion0, intrusion1 */
381 static const u16 NCT6776_REG_BEEP[NUM_REG_BEEP] = { 0xb2, 0xb3, 0xb4, 0xb5 };
383 static const s8 NCT6776_BEEP_BITS[] = {
384 0, 1, 2, 3, 4, 5, 6, 7, /* in0.. in7 */
385 8, -1, -1, -1, -1, -1, -1, /* in8..in14 */
386 24, /* global beep enable */
387 25, 26, 27, 28, 29, /* fan1..fan5 */
388 -1, -1, -1, /* unused */
389 16, 17, 18, 19, 20, 21, /* temp1..temp6 */
390 30, 31 }; /* intrusion0, intrusion1 */
392 static const u16 NCT6776_REG_TOLERANCE_H[] = {
393 0x10c, 0x20c, 0x30c, 0x80c, 0x90c, 0xa0c };
395 static const u8 NCT6776_REG_PWM_MODE[] = { 0x04, 0, 0, 0, 0, 0 };
396 static const u8 NCT6776_PWM_MODE_MASK[] = { 0x01, 0, 0, 0, 0, 0 };
398 static const u16 NCT6776_REG_FAN_MIN[] = { 0x63a, 0x63c, 0x63e, 0x640, 0x642 };
399 static const u16 NCT6776_REG_FAN_PULSES[] = { 0x644, 0x645, 0x646, 0, 0 };
401 static const u16 NCT6776_REG_WEIGHT_DUTY_BASE[] = {
402 0x13e, 0x23e, 0x33e, 0x83e, 0x93e, 0xa3e };
404 static const u16 NCT6776_REG_TEMP_CONFIG[ARRAY_SIZE(NCT6775_REG_TEMP)] = {
405 0x18, 0x152, 0x252, 0x628, 0x629, 0x62A };
407 static const char *const nct6776_temp_label[] = {
422 "PCH_CHIP_CPU_MAX_TEMP",
433 static const u16 NCT6776_REG_TEMP_ALTERNATE[ARRAY_SIZE(nct6776_temp_label) - 1]
434 = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0x401, 0x402, 0x404 };
436 static const u16 NCT6776_REG_TEMP_CRIT[ARRAY_SIZE(nct6776_temp_label) - 1]
437 = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0x709, 0x70a };
439 /* NCT6779 specific data */
441 static const u16 NCT6779_REG_IN[] = {
442 0x480, 0x481, 0x482, 0x483, 0x484, 0x485, 0x486, 0x487,
443 0x488, 0x489, 0x48a, 0x48b, 0x48c, 0x48d, 0x48e };
445 static const u16 NCT6779_REG_ALARM[NUM_REG_ALARM] = {
446 0x459, 0x45A, 0x45B, 0x568 };
448 static const s8 NCT6779_ALARM_BITS[] = {
449 0, 1, 2, 3, 8, 21, 20, 16, /* in0.. in7 */
450 17, 24, 25, 26, 27, 28, 29, /* in8..in14 */
452 6, 7, 11, 10, 23, /* fan1..fan5 */
453 -1, -1, -1, /* unused */
454 4, 5, 13, -1, -1, -1, /* temp1..temp6 */
455 12, 9 }; /* intrusion0, intrusion1 */
457 static const s8 NCT6779_BEEP_BITS[] = {
458 0, 1, 2, 3, 4, 5, 6, 7, /* in0.. in7 */
459 8, 9, 10, 11, 12, 13, 14, /* in8..in14 */
460 24, /* global beep enable */
461 25, 26, 27, 28, 29, /* fan1..fan5 */
462 -1, -1, -1, /* unused */
463 16, 17, -1, -1, -1, -1, /* temp1..temp6 */
464 30, 31 }; /* intrusion0, intrusion1 */
466 static const u16 NCT6779_REG_FAN[] = {
467 0x4b0, 0x4b2, 0x4b4, 0x4b6, 0x4b8, 0x4ba };
468 static const u16 NCT6779_REG_FAN_PULSES[] = {
469 0x644, 0x645, 0x646, 0x647, 0x648, 0x649 };
471 static const u16 NCT6779_REG_CRITICAL_PWM_ENABLE[] = {
472 0x136, 0x236, 0x336, 0x836, 0x936, 0xa36 };
473 #define NCT6779_CRITICAL_PWM_ENABLE_MASK 0x01
474 static const u16 NCT6779_REG_CRITICAL_PWM[] = {
475 0x137, 0x237, 0x337, 0x837, 0x937, 0xa37 };
477 static const u16 NCT6779_REG_TEMP[] = { 0x27, 0x150 };
478 static const u16 NCT6779_REG_TEMP_MON[] = { 0x73, 0x75, 0x77, 0x79, 0x7b };
479 static const u16 NCT6779_REG_TEMP_CONFIG[ARRAY_SIZE(NCT6779_REG_TEMP)] = {
481 static const u16 NCT6779_REG_TEMP_HYST[ARRAY_SIZE(NCT6779_REG_TEMP)] = {
483 static const u16 NCT6779_REG_TEMP_OVER[ARRAY_SIZE(NCT6779_REG_TEMP)] = {
486 static const u16 NCT6779_REG_TEMP_OFFSET[] = {
487 0x454, 0x455, 0x456, 0x44a, 0x44b, 0x44c };
489 static const char *const nct6779_temp_label[] = {
508 "PCH_CHIP_CPU_MAX_TEMP",
524 #define NCT6779_NUM_LABELS (ARRAY_SIZE(nct6779_temp_label) - 5)
525 #define NCT6791_NUM_LABELS ARRAY_SIZE(nct6779_temp_label)
527 static const u16 NCT6779_REG_TEMP_ALTERNATE[NCT6791_NUM_LABELS - 1]
528 = { 0x490, 0x491, 0x492, 0x493, 0x494, 0x495, 0, 0,
529 0, 0, 0, 0, 0, 0, 0, 0,
530 0, 0x400, 0x401, 0x402, 0x404, 0x405, 0x406, 0x407,
533 static const u16 NCT6779_REG_TEMP_CRIT[NCT6791_NUM_LABELS - 1]
534 = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0x709, 0x70a };
536 /* NCT6791 specific data */
538 #define NCT6791_REG_HM_IO_SPACE_LOCK_ENABLE 0x28
540 static const u16 NCT6791_REG_WEIGHT_TEMP_SEL[6] = { 0, 0x239 };
541 static const u16 NCT6791_REG_WEIGHT_TEMP_STEP[6] = { 0, 0x23a };
542 static const u16 NCT6791_REG_WEIGHT_TEMP_STEP_TOL[6] = { 0, 0x23b };
543 static const u16 NCT6791_REG_WEIGHT_DUTY_STEP[6] = { 0, 0x23c };
544 static const u16 NCT6791_REG_WEIGHT_TEMP_BASE[6] = { 0, 0x23d };
545 static const u16 NCT6791_REG_WEIGHT_DUTY_BASE[6] = { 0, 0x23e };
547 static const u16 NCT6791_REG_ALARM[NUM_REG_ALARM] = {
548 0x459, 0x45A, 0x45B, 0x568, 0x45D };
550 static const s8 NCT6791_ALARM_BITS[] = {
551 0, 1, 2, 3, 8, 21, 20, 16, /* in0.. in7 */
552 17, 24, 25, 26, 27, 28, 29, /* in8..in14 */
554 6, 7, 11, 10, 23, 33, /* fan1..fan6 */
556 4, 5, 13, -1, -1, -1, /* temp1..temp6 */
557 12, 9 }; /* intrusion0, intrusion1 */
559 /* NCT6792/NCT6793 specific data */
561 static const u16 NCT6792_REG_TEMP_MON[] = {
562 0x73, 0x75, 0x77, 0x79, 0x7b, 0x7d };
563 static const u16 NCT6792_REG_BEEP[NUM_REG_BEEP] = {
564 0xb2, 0xb3, 0xb4, 0xb5, 0xbf };
566 /* NCT6102D/NCT6106D specific data */
568 #define NCT6106_REG_VBAT 0x318
569 #define NCT6106_REG_DIODE 0x319
570 #define NCT6106_DIODE_MASK 0x01
572 static const u16 NCT6106_REG_IN_MAX[] = {
573 0x90, 0x92, 0x94, 0x96, 0x98, 0x9a, 0x9e, 0xa0, 0xa2 };
574 static const u16 NCT6106_REG_IN_MIN[] = {
575 0x91, 0x93, 0x95, 0x97, 0x99, 0x9b, 0x9f, 0xa1, 0xa3 };
576 static const u16 NCT6106_REG_IN[] = {
577 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x07, 0x08, 0x09 };
579 static const u16 NCT6106_REG_TEMP[] = { 0x10, 0x11, 0x12, 0x13, 0x14, 0x15 };
580 static const u16 NCT6106_REG_TEMP_MON[] = { 0x18, 0x19, 0x1a };
581 static const u16 NCT6106_REG_TEMP_HYST[] = {
582 0xc3, 0xc7, 0xcb, 0xcf, 0xd3, 0xd7 };
583 static const u16 NCT6106_REG_TEMP_OVER[] = {
584 0xc2, 0xc6, 0xca, 0xce, 0xd2, 0xd6 };
585 static const u16 NCT6106_REG_TEMP_CRIT_L[] = {
586 0xc0, 0xc4, 0xc8, 0xcc, 0xd0, 0xd4 };
587 static const u16 NCT6106_REG_TEMP_CRIT_H[] = {
588 0xc1, 0xc5, 0xc9, 0xcf, 0xd1, 0xd5 };
589 static const u16 NCT6106_REG_TEMP_OFFSET[] = { 0x311, 0x312, 0x313 };
590 static const u16 NCT6106_REG_TEMP_CONFIG[] = {
591 0xb7, 0xb8, 0xb9, 0xba, 0xbb, 0xbc };
593 static const u16 NCT6106_REG_FAN[] = { 0x20, 0x22, 0x24 };
594 static const u16 NCT6106_REG_FAN_MIN[] = { 0xe0, 0xe2, 0xe4 };
595 static const u16 NCT6106_REG_FAN_PULSES[] = { 0xf6, 0xf6, 0xf6, 0, 0 };
596 static const u16 NCT6106_FAN_PULSE_SHIFT[] = { 0, 2, 4, 0, 0 };
598 static const u8 NCT6106_REG_PWM_MODE[] = { 0xf3, 0xf3, 0xf3 };
599 static const u8 NCT6106_PWM_MODE_MASK[] = { 0x01, 0x02, 0x04 };
600 static const u16 NCT6106_REG_PWM[] = { 0x119, 0x129, 0x139 };
601 static const u16 NCT6106_REG_PWM_READ[] = { 0x4a, 0x4b, 0x4c };
602 static const u16 NCT6106_REG_FAN_MODE[] = { 0x113, 0x123, 0x133 };
603 static const u16 NCT6106_REG_TEMP_SEL[] = { 0x110, 0x120, 0x130 };
604 static const u16 NCT6106_REG_TEMP_SOURCE[] = {
605 0xb0, 0xb1, 0xb2, 0xb3, 0xb4, 0xb5 };
607 static const u16 NCT6106_REG_CRITICAL_TEMP[] = { 0x11a, 0x12a, 0x13a };
608 static const u16 NCT6106_REG_CRITICAL_TEMP_TOLERANCE[] = {
609 0x11b, 0x12b, 0x13b };
611 static const u16 NCT6106_REG_CRITICAL_PWM_ENABLE[] = { 0x11c, 0x12c, 0x13c };
612 #define NCT6106_CRITICAL_PWM_ENABLE_MASK 0x10
613 static const u16 NCT6106_REG_CRITICAL_PWM[] = { 0x11d, 0x12d, 0x13d };
615 static const u16 NCT6106_REG_FAN_STEP_UP_TIME[] = { 0x114, 0x124, 0x134 };
616 static const u16 NCT6106_REG_FAN_STEP_DOWN_TIME[] = { 0x115, 0x125, 0x135 };
617 static const u16 NCT6106_REG_FAN_STOP_OUTPUT[] = { 0x116, 0x126, 0x136 };
618 static const u16 NCT6106_REG_FAN_START_OUTPUT[] = { 0x117, 0x127, 0x137 };
619 static const u16 NCT6106_REG_FAN_STOP_TIME[] = { 0x118, 0x128, 0x138 };
620 static const u16 NCT6106_REG_TOLERANCE_H[] = { 0x112, 0x122, 0x132 };
622 static const u16 NCT6106_REG_TARGET[] = { 0x111, 0x121, 0x131 };
624 static const u16 NCT6106_REG_WEIGHT_TEMP_SEL[] = { 0x168, 0x178, 0x188 };
625 static const u16 NCT6106_REG_WEIGHT_TEMP_STEP[] = { 0x169, 0x179, 0x189 };
626 static const u16 NCT6106_REG_WEIGHT_TEMP_STEP_TOL[] = { 0x16a, 0x17a, 0x18a };
627 static const u16 NCT6106_REG_WEIGHT_DUTY_STEP[] = { 0x16b, 0x17b, 0x17c };
628 static const u16 NCT6106_REG_WEIGHT_TEMP_BASE[] = { 0x16c, 0x17c, 0x18c };
629 static const u16 NCT6106_REG_WEIGHT_DUTY_BASE[] = { 0x16d, 0x17d, 0x18d };
631 static const u16 NCT6106_REG_AUTO_TEMP[] = { 0x160, 0x170, 0x180 };
632 static const u16 NCT6106_REG_AUTO_PWM[] = { 0x164, 0x174, 0x184 };
634 static const u16 NCT6106_REG_ALARM[NUM_REG_ALARM] = {
635 0x77, 0x78, 0x79, 0x7a, 0x7b, 0x7c, 0x7d };
637 static const s8 NCT6106_ALARM_BITS[] = {
638 0, 1, 2, 3, 4, 5, 7, 8, /* in0.. in7 */
639 9, -1, -1, -1, -1, -1, -1, /* in8..in14 */
641 32, 33, 34, -1, -1, /* fan1..fan5 */
642 -1, -1, -1, /* unused */
643 16, 17, 18, 19, 20, 21, /* temp1..temp6 */
644 48, -1 /* intrusion0, intrusion1 */
647 static const u16 NCT6106_REG_BEEP[NUM_REG_BEEP] = {
648 0x3c0, 0x3c1, 0x3c2, 0x3c3, 0x3c4 };
650 static const s8 NCT6106_BEEP_BITS[] = {
651 0, 1, 2, 3, 4, 5, 7, 8, /* in0.. in7 */
652 9, 10, 11, 12, -1, -1, -1, /* in8..in14 */
653 32, /* global beep enable */
654 24, 25, 26, 27, 28, /* fan1..fan5 */
655 -1, -1, -1, /* unused */
656 16, 17, 18, 19, 20, 21, /* temp1..temp6 */
657 34, -1 /* intrusion0, intrusion1 */
660 static const u16 NCT6106_REG_TEMP_ALTERNATE[ARRAY_SIZE(nct6776_temp_label) - 1]
661 = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0x51, 0x52, 0x54 };
663 static const u16 NCT6106_REG_TEMP_CRIT[ARRAY_SIZE(nct6776_temp_label) - 1]
664 = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0x204, 0x205 };
666 static enum pwm_enable reg_to_pwm_enable(int pwm, int mode)
668 if (mode == 0 && pwm == 255)
673 static int pwm_enable_to_reg(enum pwm_enable mode)
684 /* 1 is DC mode, output in ms */
685 static unsigned int step_time_from_reg(u8 reg, u8 mode)
687 return mode ? 400 * reg : 100 * reg;
690 static u8 step_time_to_reg(unsigned int msec, u8 mode)
692 return clamp_val((mode ? (msec + 200) / 400 :
693 (msec + 50) / 100), 1, 255);
696 static unsigned int fan_from_reg8(u16 reg, unsigned int divreg)
698 if (reg == 0 || reg == 255)
700 return 1350000U / (reg << divreg);
703 static unsigned int fan_from_reg13(u16 reg, unsigned int divreg)
705 if ((reg & 0xff1f) == 0xff1f)
708 reg = (reg & 0x1f) | ((reg & 0xff00) >> 3);
713 return 1350000U / reg;
716 static unsigned int fan_from_reg16(u16 reg, unsigned int divreg)
718 if (reg == 0 || reg == 0xffff)
722 * Even though the registers are 16 bit wide, the fan divisor
725 return 1350000U / (reg << divreg);
728 static u16 fan_to_reg(u32 fan, unsigned int divreg)
733 return (1350000U / fan) >> divreg;
736 static inline unsigned int
743 * Some of the voltage inputs have internal scaling, the tables below
744 * contain 8 (the ADC LSB in mV) * scaling factor * 100
746 static const u16 scale_in[15] = {
747 800, 800, 1600, 1600, 800, 800, 800, 1600, 1600, 800, 800, 800, 800,
751 static inline long in_from_reg(u8 reg, u8 nr)
753 return DIV_ROUND_CLOSEST(reg * scale_in[nr], 100);
756 static inline u8 in_to_reg(u32 val, u8 nr)
758 return clamp_val(DIV_ROUND_CLOSEST(val * 100, scale_in[nr]), 0, 255);
762 * Data structures and manipulation thereof
765 struct nct6775_data {
766 int addr; /* IO base of hw monitor block */
767 int sioreg; /* SIO register address */
771 #if LINUX_VERSION_CODE < KERNEL_VERSION(3, 13, 0)
772 struct device *hwmon_dev;
775 const struct attribute_group *groups[6];
777 u16 reg_temp[5][NUM_TEMP]; /* 0=temp, 1=temp_over, 2=temp_hyst,
778 * 3=temp_crit, 4=temp_lcrit
780 u8 temp_src[NUM_TEMP];
781 u16 reg_temp_config[NUM_TEMP];
782 const char * const *temp_label;
790 const s8 *ALARM_BITS;
794 const u16 *REG_IN_MINMAX[2];
796 const u16 *REG_TARGET;
798 const u16 *REG_FAN_MODE;
799 const u16 *REG_FAN_MIN;
800 const u16 *REG_FAN_PULSES;
801 const u16 *FAN_PULSE_SHIFT;
802 const u16 *REG_FAN_TIME[3];
804 const u16 *REG_TOLERANCE_H;
806 const u8 *REG_PWM_MODE;
807 const u8 *PWM_MODE_MASK;
809 const u16 *REG_PWM[7]; /* [0]=pwm, [1]=pwm_start, [2]=pwm_floor,
810 * [3]=pwm_max, [4]=pwm_step,
811 * [5]=weight_duty_step, [6]=weight_duty_base
813 const u16 *REG_PWM_READ;
815 const u16 *REG_CRITICAL_PWM_ENABLE;
816 u8 CRITICAL_PWM_ENABLE_MASK;
817 const u16 *REG_CRITICAL_PWM;
819 const u16 *REG_AUTO_TEMP;
820 const u16 *REG_AUTO_PWM;
822 const u16 *REG_CRITICAL_TEMP;
823 const u16 *REG_CRITICAL_TEMP_TOLERANCE;
825 const u16 *REG_TEMP_SOURCE; /* temp register sources */
826 const u16 *REG_TEMP_SEL;
827 const u16 *REG_WEIGHT_TEMP_SEL;
828 const u16 *REG_WEIGHT_TEMP[3]; /* 0=base, 1=tolerance, 2=step */
830 const u16 *REG_TEMP_OFFSET;
832 const u16 *REG_ALARM;
835 unsigned int (*fan_from_reg)(u16 reg, unsigned int divreg);
836 unsigned int (*fan_from_reg_min)(u16 reg, unsigned int divreg);
838 struct mutex update_lock;
839 bool valid; /* true if following fields are valid */
840 unsigned long last_updated; /* In jiffies */
842 /* Register values */
843 u8 bank; /* current register bank */
844 u8 in_num; /* number of in inputs we have */
845 u8 in[15][3]; /* [0]=in, [1]=in_max, [2]=in_min */
846 unsigned int rpm[NUM_FAN];
847 u16 fan_min[NUM_FAN];
848 u8 fan_pulses[NUM_FAN];
851 u8 has_fan; /* some fan inputs can be disabled */
852 u8 has_fan_min; /* some fans don't have min register */
855 u8 num_temp_alarms; /* 2, 3, or 6 */
856 u8 num_temp_beeps; /* 2, 3, or 6 */
857 u8 temp_fixed_num; /* 3 or 6 */
858 u8 temp_type[NUM_TEMP_FIXED];
859 s8 temp_offset[NUM_TEMP_FIXED];
860 s16 temp[5][NUM_TEMP]; /* 0=temp, 1=temp_over, 2=temp_hyst,
861 * 3=temp_crit, 4=temp_lcrit */
865 u8 pwm_num; /* number of pwm */
866 u8 pwm_mode[NUM_FAN]; /* 1->DC variable voltage,
867 * 0->PWM variable duty cycle
869 enum pwm_enable pwm_enable[NUM_FAN];
872 * 2->thermal cruise mode (also called SmartFan I)
873 * 3->fan speed cruise mode
875 * 5->enhanced variable thermal cruise (SmartFan IV)
877 u8 pwm[7][NUM_FAN]; /* [0]=pwm, [1]=pwm_start, [2]=pwm_floor,
878 * [3]=pwm_max, [4]=pwm_step,
879 * [5]=weight_duty_step, [6]=weight_duty_base
882 u8 target_temp[NUM_FAN];
884 u32 target_speed[NUM_FAN];
885 u32 target_speed_tolerance[NUM_FAN];
886 u8 speed_tolerance_limit;
888 u8 temp_tolerance[2][NUM_FAN];
891 u8 fan_time[3][NUM_FAN]; /* 0 = stop_time, 1 = step_up, 2 = step_down */
893 /* Automatic fan speed control registers */
895 u8 auto_pwm[NUM_FAN][7];
896 u8 auto_temp[NUM_FAN][7];
897 u8 pwm_temp_sel[NUM_FAN];
898 u8 pwm_weight_temp_sel[NUM_FAN];
899 u8 weight_temp[3][NUM_FAN]; /* 0->temp_step, 1->temp_step_tol,
912 /* Remember extra register values over suspend/resume */
919 struct nct6775_sio_data {
924 struct sensor_device_template {
925 struct device_attribute dev_attr;
933 bool s2; /* true if both index and nr are used */
936 struct sensor_device_attr_u {
938 struct sensor_device_attribute a1;
939 struct sensor_device_attribute_2 a2;
944 #define __TEMPLATE_ATTR(_template, _mode, _show, _store) { \
945 .attr = {.name = _template, .mode = _mode }, \
950 #define SENSOR_DEVICE_TEMPLATE(_template, _mode, _show, _store, _index) \
951 { .dev_attr = __TEMPLATE_ATTR(_template, _mode, _show, _store), \
955 #define SENSOR_DEVICE_TEMPLATE_2(_template, _mode, _show, _store, \
957 { .dev_attr = __TEMPLATE_ATTR(_template, _mode, _show, _store), \
958 .u.s.index = _index, \
962 #define SENSOR_TEMPLATE(_name, _template, _mode, _show, _store, _index) \
963 static struct sensor_device_template sensor_dev_template_##_name \
964 = SENSOR_DEVICE_TEMPLATE(_template, _mode, _show, _store, \
967 #define SENSOR_TEMPLATE_2(_name, _template, _mode, _show, _store, \
969 static struct sensor_device_template sensor_dev_template_##_name \
970 = SENSOR_DEVICE_TEMPLATE_2(_template, _mode, _show, _store, \
973 struct sensor_template_group {
974 struct sensor_device_template **templates;
975 umode_t (*is_visible)(struct kobject *, struct attribute *, int);
979 static struct attribute_group *
980 nct6775_create_attr_group(struct device *dev, struct sensor_template_group *tg,
983 struct attribute_group *group;
984 struct sensor_device_attr_u *su;
985 struct sensor_device_attribute *a;
986 struct sensor_device_attribute_2 *a2;
987 struct attribute **attrs;
988 struct sensor_device_template **t;
992 return ERR_PTR(-EINVAL);
995 for (count = 0; *t; t++, count++)
999 return ERR_PTR(-EINVAL);
1001 group = devm_kzalloc(dev, sizeof(*group), GFP_KERNEL);
1003 return ERR_PTR(-ENOMEM);
1005 attrs = devm_kzalloc(dev, sizeof(*attrs) * (repeat * count + 1),
1008 return ERR_PTR(-ENOMEM);
1010 su = devm_kzalloc(dev, sizeof(*su) * repeat * count,
1013 return ERR_PTR(-ENOMEM);
1015 group->attrs = attrs;
1016 group->is_visible = tg->is_visible;
1018 for (i = 0; i < repeat; i++) {
1020 while (*t != NULL) {
1021 snprintf(su->name, sizeof(su->name),
1022 (*t)->dev_attr.attr.name, tg->base + i);
1025 a2->dev_attr.attr.name = su->name;
1026 a2->nr = (*t)->u.s.nr + i;
1027 a2->index = (*t)->u.s.index;
1028 a2->dev_attr.attr.mode =
1029 (*t)->dev_attr.attr.mode;
1030 a2->dev_attr.show = (*t)->dev_attr.show;
1031 a2->dev_attr.store = (*t)->dev_attr.store;
1032 *attrs = &a2->dev_attr.attr;
1035 a->dev_attr.attr.name = su->name;
1036 a->index = (*t)->u.index + i;
1037 a->dev_attr.attr.mode =
1038 (*t)->dev_attr.attr.mode;
1039 a->dev_attr.show = (*t)->dev_attr.show;
1040 a->dev_attr.store = (*t)->dev_attr.store;
1041 *attrs = &a->dev_attr.attr;
1052 static bool is_word_sized(struct nct6775_data *data, u16 reg)
1054 switch (data->kind) {
1056 return reg == 0x20 || reg == 0x22 || reg == 0x24 ||
1057 reg == 0xe0 || reg == 0xe2 || reg == 0xe4 ||
1058 reg == 0x111 || reg == 0x121 || reg == 0x131;
1060 return (((reg & 0xff00) == 0x100 ||
1061 (reg & 0xff00) == 0x200) &&
1062 ((reg & 0x00ff) == 0x50 ||
1063 (reg & 0x00ff) == 0x53 ||
1064 (reg & 0x00ff) == 0x55)) ||
1065 (reg & 0xfff0) == 0x630 ||
1066 reg == 0x640 || reg == 0x642 ||
1068 ((reg & 0xfff0) == 0x650 && (reg & 0x000f) >= 0x06) ||
1069 reg == 0x73 || reg == 0x75 || reg == 0x77;
1071 return (((reg & 0xff00) == 0x100 ||
1072 (reg & 0xff00) == 0x200) &&
1073 ((reg & 0x00ff) == 0x50 ||
1074 (reg & 0x00ff) == 0x53 ||
1075 (reg & 0x00ff) == 0x55)) ||
1076 (reg & 0xfff0) == 0x630 ||
1078 reg == 0x640 || reg == 0x642 ||
1079 ((reg & 0xfff0) == 0x650 && (reg & 0x000f) >= 0x06) ||
1080 reg == 0x73 || reg == 0x75 || reg == 0x77;
1085 return reg == 0x150 || reg == 0x153 || reg == 0x155 ||
1086 ((reg & 0xfff0) == 0x4b0 && (reg & 0x000f) < 0x0b) ||
1088 reg == 0x63a || reg == 0x63c || reg == 0x63e ||
1089 reg == 0x640 || reg == 0x642 ||
1090 reg == 0x73 || reg == 0x75 || reg == 0x77 || reg == 0x79 ||
1091 reg == 0x7b || reg == 0x7d;
1097 * On older chips, only registers 0x50-0x5f are banked.
1098 * On more recent chips, all registers are banked.
1099 * Assume that is the case and set the bank number for each access.
1100 * Cache the bank number so it only needs to be set if it changes.
1102 static inline void nct6775_set_bank(struct nct6775_data *data, u16 reg)
1106 if (data->bank != bank) {
1107 outb_p(NCT6775_REG_BANK, data->addr + ADDR_REG_OFFSET);
1108 outb_p(bank, data->addr + DATA_REG_OFFSET);
1113 static u16 nct6775_read_value(struct nct6775_data *data, u16 reg)
1115 int res, word_sized = is_word_sized(data, reg);
1117 nct6775_set_bank(data, reg);
1118 outb_p(reg & 0xff, data->addr + ADDR_REG_OFFSET);
1119 res = inb_p(data->addr + DATA_REG_OFFSET);
1121 outb_p((reg & 0xff) + 1,
1122 data->addr + ADDR_REG_OFFSET);
1123 res = (res << 8) + inb_p(data->addr + DATA_REG_OFFSET);
1128 static int nct6775_write_value(struct nct6775_data *data, u16 reg, u16 value)
1130 int word_sized = is_word_sized(data, reg);
1132 nct6775_set_bank(data, reg);
1133 outb_p(reg & 0xff, data->addr + ADDR_REG_OFFSET);
1135 outb_p(value >> 8, data->addr + DATA_REG_OFFSET);
1136 outb_p((reg & 0xff) + 1,
1137 data->addr + ADDR_REG_OFFSET);
1139 outb_p(value & 0xff, data->addr + DATA_REG_OFFSET);
1143 /* We left-align 8-bit temperature values to make the code simpler */
1144 static u16 nct6775_read_temp(struct nct6775_data *data, u16 reg)
1148 res = nct6775_read_value(data, reg);
1149 if (!is_word_sized(data, reg))
1155 static int nct6775_write_temp(struct nct6775_data *data, u16 reg, u16 value)
1157 if (!is_word_sized(data, reg))
1159 return nct6775_write_value(data, reg, value);
1162 /* This function assumes that the caller holds data->update_lock */
1163 static void nct6775_write_fan_div(struct nct6775_data *data, int nr)
1169 reg = (nct6775_read_value(data, NCT6775_REG_FANDIV1) & 0x70)
1170 | (data->fan_div[0] & 0x7);
1171 nct6775_write_value(data, NCT6775_REG_FANDIV1, reg);
1174 reg = (nct6775_read_value(data, NCT6775_REG_FANDIV1) & 0x7)
1175 | ((data->fan_div[1] << 4) & 0x70);
1176 nct6775_write_value(data, NCT6775_REG_FANDIV1, reg);
1179 reg = (nct6775_read_value(data, NCT6775_REG_FANDIV2) & 0x70)
1180 | (data->fan_div[2] & 0x7);
1181 nct6775_write_value(data, NCT6775_REG_FANDIV2, reg);
1184 reg = (nct6775_read_value(data, NCT6775_REG_FANDIV2) & 0x7)
1185 | ((data->fan_div[3] << 4) & 0x70);
1186 nct6775_write_value(data, NCT6775_REG_FANDIV2, reg);
1191 static void nct6775_write_fan_div_common(struct nct6775_data *data, int nr)
1193 if (data->kind == nct6775)
1194 nct6775_write_fan_div(data, nr);
1197 static void nct6775_update_fan_div(struct nct6775_data *data)
1201 i = nct6775_read_value(data, NCT6775_REG_FANDIV1);
1202 data->fan_div[0] = i & 0x7;
1203 data->fan_div[1] = (i & 0x70) >> 4;
1204 i = nct6775_read_value(data, NCT6775_REG_FANDIV2);
1205 data->fan_div[2] = i & 0x7;
1206 if (data->has_fan & (1 << 3))
1207 data->fan_div[3] = (i & 0x70) >> 4;
1210 static void nct6775_update_fan_div_common(struct nct6775_data *data)
1212 if (data->kind == nct6775)
1213 nct6775_update_fan_div(data);
1216 static void nct6775_init_fan_div(struct nct6775_data *data)
1220 nct6775_update_fan_div_common(data);
1222 * For all fans, start with highest divider value if the divider
1223 * register is not initialized. This ensures that we get a
1224 * reading from the fan count register, even if it is not optimal.
1225 * We'll compute a better divider later on.
1227 for (i = 0; i < ARRAY_SIZE(data->fan_div); i++) {
1228 if (!(data->has_fan & (1 << i)))
1230 if (data->fan_div[i] == 0) {
1231 data->fan_div[i] = 7;
1232 nct6775_write_fan_div_common(data, i);
1237 static void nct6775_init_fan_common(struct device *dev,
1238 struct nct6775_data *data)
1243 if (data->has_fan_div)
1244 nct6775_init_fan_div(data);
1247 * If fan_min is not set (0), set it to 0xff to disable it. This
1248 * prevents the unnecessary warning when fanX_min is reported as 0.
1250 for (i = 0; i < ARRAY_SIZE(data->fan_min); i++) {
1251 if (data->has_fan_min & (1 << i)) {
1252 reg = nct6775_read_value(data, data->REG_FAN_MIN[i]);
1254 nct6775_write_value(data, data->REG_FAN_MIN[i],
1255 data->has_fan_div ? 0xff
1261 static void nct6775_select_fan_div(struct device *dev,
1262 struct nct6775_data *data, int nr, u16 reg)
1264 u8 fan_div = data->fan_div[nr];
1267 if (!data->has_fan_div)
1271 * If we failed to measure the fan speed, or the reported value is not
1272 * in the optimal range, and the clock divider can be modified,
1273 * let's try that for next time.
1275 if (reg == 0x00 && fan_div < 0x07)
1277 else if (reg != 0x00 && reg < 0x30 && fan_div > 0)
1280 if (fan_div != data->fan_div[nr]) {
1281 dev_dbg(dev, "Modifying fan%d clock divider from %u to %u\n",
1282 nr + 1, div_from_reg(data->fan_div[nr]),
1283 div_from_reg(fan_div));
1285 /* Preserve min limit if possible */
1286 if (data->has_fan_min & (1 << nr)) {
1287 fan_min = data->fan_min[nr];
1288 if (fan_div > data->fan_div[nr]) {
1289 if (fan_min != 255 && fan_min > 1)
1292 if (fan_min != 255) {
1298 if (fan_min != data->fan_min[nr]) {
1299 data->fan_min[nr] = fan_min;
1300 nct6775_write_value(data, data->REG_FAN_MIN[nr],
1304 data->fan_div[nr] = fan_div;
1305 nct6775_write_fan_div_common(data, nr);
1309 static void nct6775_update_pwm(struct device *dev)
1311 struct nct6775_data *data = dev_get_drvdata(dev);
1313 int fanmodecfg, reg;
1316 for (i = 0; i < data->pwm_num; i++) {
1317 if (!(data->has_pwm & (1 << i)))
1320 duty_is_dc = data->REG_PWM_MODE[i] &&
1321 (nct6775_read_value(data, data->REG_PWM_MODE[i])
1322 & data->PWM_MODE_MASK[i]);
1323 data->pwm_mode[i] = duty_is_dc;
1325 fanmodecfg = nct6775_read_value(data, data->REG_FAN_MODE[i]);
1326 for (j = 0; j < ARRAY_SIZE(data->REG_PWM); j++) {
1327 if (data->REG_PWM[j] && data->REG_PWM[j][i]) {
1329 = nct6775_read_value(data,
1330 data->REG_PWM[j][i]);
1334 data->pwm_enable[i] = reg_to_pwm_enable(data->pwm[0][i],
1335 (fanmodecfg >> 4) & 7);
1337 if (!data->temp_tolerance[0][i] ||
1338 data->pwm_enable[i] != speed_cruise)
1339 data->temp_tolerance[0][i] = fanmodecfg & 0x0f;
1340 if (!data->target_speed_tolerance[i] ||
1341 data->pwm_enable[i] == speed_cruise) {
1342 u8 t = fanmodecfg & 0x0f;
1344 if (data->REG_TOLERANCE_H) {
1345 t |= (nct6775_read_value(data,
1346 data->REG_TOLERANCE_H[i]) & 0x70) >> 1;
1348 data->target_speed_tolerance[i] = t;
1351 data->temp_tolerance[1][i] =
1352 nct6775_read_value(data,
1353 data->REG_CRITICAL_TEMP_TOLERANCE[i]);
1355 reg = nct6775_read_value(data, data->REG_TEMP_SEL[i]);
1356 data->pwm_temp_sel[i] = reg & 0x1f;
1357 /* If fan can stop, report floor as 0 */
1359 data->pwm[2][i] = 0;
1361 if (!data->REG_WEIGHT_TEMP_SEL[i])
1364 reg = nct6775_read_value(data, data->REG_WEIGHT_TEMP_SEL[i]);
1365 data->pwm_weight_temp_sel[i] = reg & 0x1f;
1366 /* If weight is disabled, report weight source as 0 */
1367 if (j == 1 && !(reg & 0x80))
1368 data->pwm_weight_temp_sel[i] = 0;
1370 /* Weight temp data */
1371 for (j = 0; j < ARRAY_SIZE(data->weight_temp); j++) {
1372 data->weight_temp[j][i]
1373 = nct6775_read_value(data,
1374 data->REG_WEIGHT_TEMP[j][i]);
1379 static void nct6775_update_pwm_limits(struct device *dev)
1381 struct nct6775_data *data = dev_get_drvdata(dev);
1386 for (i = 0; i < data->pwm_num; i++) {
1387 if (!(data->has_pwm & (1 << i)))
1390 for (j = 0; j < ARRAY_SIZE(data->fan_time); j++) {
1391 data->fan_time[j][i] =
1392 nct6775_read_value(data, data->REG_FAN_TIME[j][i]);
1395 reg_t = nct6775_read_value(data, data->REG_TARGET[i]);
1396 /* Update only in matching mode or if never updated */
1397 if (!data->target_temp[i] ||
1398 data->pwm_enable[i] == thermal_cruise)
1399 data->target_temp[i] = reg_t & data->target_temp_mask;
1400 if (!data->target_speed[i] ||
1401 data->pwm_enable[i] == speed_cruise) {
1402 if (data->REG_TOLERANCE_H) {
1403 reg_t |= (nct6775_read_value(data,
1404 data->REG_TOLERANCE_H[i]) & 0x0f) << 8;
1406 data->target_speed[i] = reg_t;
1409 for (j = 0; j < data->auto_pwm_num; j++) {
1410 data->auto_pwm[i][j] =
1411 nct6775_read_value(data,
1412 NCT6775_AUTO_PWM(data, i, j));
1413 data->auto_temp[i][j] =
1414 nct6775_read_value(data,
1415 NCT6775_AUTO_TEMP(data, i, j));
1418 /* critical auto_pwm temperature data */
1419 data->auto_temp[i][data->auto_pwm_num] =
1420 nct6775_read_value(data, data->REG_CRITICAL_TEMP[i]);
1422 switch (data->kind) {
1424 reg = nct6775_read_value(data,
1425 NCT6775_REG_CRITICAL_ENAB[i]);
1426 data->auto_pwm[i][data->auto_pwm_num] =
1427 (reg & 0x02) ? 0xff : 0x00;
1430 data->auto_pwm[i][data->auto_pwm_num] = 0xff;
1437 reg = nct6775_read_value(data,
1438 data->REG_CRITICAL_PWM_ENABLE[i]);
1439 if (reg & data->CRITICAL_PWM_ENABLE_MASK)
1440 reg = nct6775_read_value(data,
1441 data->REG_CRITICAL_PWM[i]);
1444 data->auto_pwm[i][data->auto_pwm_num] = reg;
1450 static struct nct6775_data *nct6775_update_device(struct device *dev)
1452 struct nct6775_data *data = dev_get_drvdata(dev);
1455 mutex_lock(&data->update_lock);
1457 if (time_after(jiffies, data->last_updated + HZ + HZ / 2)
1459 /* Fan clock dividers */
1460 nct6775_update_fan_div_common(data);
1462 /* Measured voltages and limits */
1463 for (i = 0; i < data->in_num; i++) {
1464 if (!(data->have_in & (1 << i)))
1467 data->in[i][0] = nct6775_read_value(data,
1469 data->in[i][1] = nct6775_read_value(data,
1470 data->REG_IN_MINMAX[0][i]);
1471 data->in[i][2] = nct6775_read_value(data,
1472 data->REG_IN_MINMAX[1][i]);
1475 /* Measured fan speeds and limits */
1476 for (i = 0; i < ARRAY_SIZE(data->rpm); i++) {
1479 if (!(data->has_fan & (1 << i)))
1482 reg = nct6775_read_value(data, data->REG_FAN[i]);
1483 data->rpm[i] = data->fan_from_reg(reg,
1486 if (data->has_fan_min & (1 << i))
1487 data->fan_min[i] = nct6775_read_value(data,
1488 data->REG_FAN_MIN[i]);
1489 data->fan_pulses[i] =
1490 (nct6775_read_value(data, data->REG_FAN_PULSES[i])
1491 >> data->FAN_PULSE_SHIFT[i]) & 0x03;
1493 nct6775_select_fan_div(dev, data, i, reg);
1496 nct6775_update_pwm(dev);
1497 nct6775_update_pwm_limits(dev);
1499 /* Measured temperatures and limits */
1500 for (i = 0; i < NUM_TEMP; i++) {
1501 if (!(data->have_temp & (1 << i)))
1503 for (j = 0; j < ARRAY_SIZE(data->reg_temp); j++) {
1504 if (data->reg_temp[j][i])
1506 = nct6775_read_temp(data,
1507 data->reg_temp[j][i]);
1509 if (i >= NUM_TEMP_FIXED ||
1510 !(data->have_temp_fixed & (1 << i)))
1512 data->temp_offset[i]
1513 = nct6775_read_value(data, data->REG_TEMP_OFFSET[i]);
1517 for (i = 0; i < NUM_REG_ALARM; i++) {
1520 if (!data->REG_ALARM[i])
1522 alarm = nct6775_read_value(data, data->REG_ALARM[i]);
1523 data->alarms |= ((u64)alarm) << (i << 3);
1527 for (i = 0; i < NUM_REG_BEEP; i++) {
1530 if (!data->REG_BEEP[i])
1532 beep = nct6775_read_value(data, data->REG_BEEP[i]);
1533 data->beeps |= ((u64)beep) << (i << 3);
1536 data->last_updated = jiffies;
1540 mutex_unlock(&data->update_lock);
1545 * Sysfs callback functions
1548 show_in_reg(struct device *dev, struct device_attribute *attr, char *buf)
1550 struct nct6775_data *data = nct6775_update_device(dev);
1551 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
1552 int index = sattr->index;
1555 return sprintf(buf, "%ld\n", in_from_reg(data->in[nr][index], nr));
1559 store_in_reg(struct device *dev, struct device_attribute *attr, const char *buf,
1562 struct nct6775_data *data = dev_get_drvdata(dev);
1563 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
1564 int index = sattr->index;
1569 err = kstrtoul(buf, 10, &val);
1572 mutex_lock(&data->update_lock);
1573 data->in[nr][index] = in_to_reg(val, nr);
1574 nct6775_write_value(data, data->REG_IN_MINMAX[index - 1][nr],
1575 data->in[nr][index]);
1576 mutex_unlock(&data->update_lock);
1581 show_alarm(struct device *dev, struct device_attribute *attr, char *buf)
1583 struct nct6775_data *data = nct6775_update_device(dev);
1584 struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr);
1585 int nr = data->ALARM_BITS[sattr->index];
1587 return sprintf(buf, "%u\n",
1588 (unsigned int)((data->alarms >> nr) & 0x01));
1591 static int find_temp_source(struct nct6775_data *data, int index, int count)
1593 int source = data->temp_src[index];
1596 for (nr = 0; nr < count; nr++) {
1599 src = nct6775_read_value(data,
1600 data->REG_TEMP_SOURCE[nr]) & 0x1f;
1608 show_temp_alarm(struct device *dev, struct device_attribute *attr, char *buf)
1610 struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr);
1611 struct nct6775_data *data = nct6775_update_device(dev);
1612 unsigned int alarm = 0;
1616 * For temperatures, there is no fixed mapping from registers to alarm
1617 * bits. Alarm bits are determined by the temperature source mapping.
1619 nr = find_temp_source(data, sattr->index, data->num_temp_alarms);
1621 int bit = data->ALARM_BITS[nr + TEMP_ALARM_BASE];
1623 alarm = (data->alarms >> bit) & 0x01;
1625 return sprintf(buf, "%u\n", alarm);
1629 show_beep(struct device *dev, struct device_attribute *attr, char *buf)
1631 struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr);
1632 struct nct6775_data *data = nct6775_update_device(dev);
1633 int nr = data->BEEP_BITS[sattr->index];
1635 return sprintf(buf, "%u\n",
1636 (unsigned int)((data->beeps >> nr) & 0x01));
1640 store_beep(struct device *dev, struct device_attribute *attr, const char *buf,
1643 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
1644 struct nct6775_data *data = dev_get_drvdata(dev);
1645 int nr = data->BEEP_BITS[sattr->index];
1646 int regindex = nr >> 3;
1650 err = kstrtoul(buf, 10, &val);
1656 mutex_lock(&data->update_lock);
1658 data->beeps |= (1ULL << nr);
1660 data->beeps &= ~(1ULL << nr);
1661 nct6775_write_value(data, data->REG_BEEP[regindex],
1662 (data->beeps >> (regindex << 3)) & 0xff);
1663 mutex_unlock(&data->update_lock);
1668 show_temp_beep(struct device *dev, struct device_attribute *attr, char *buf)
1670 struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr);
1671 struct nct6775_data *data = nct6775_update_device(dev);
1672 unsigned int beep = 0;
1676 * For temperatures, there is no fixed mapping from registers to beep
1677 * enable bits. Beep enable bits are determined by the temperature
1680 nr = find_temp_source(data, sattr->index, data->num_temp_beeps);
1682 int bit = data->BEEP_BITS[nr + TEMP_ALARM_BASE];
1684 beep = (data->beeps >> bit) & 0x01;
1686 return sprintf(buf, "%u\n", beep);
1690 store_temp_beep(struct device *dev, struct device_attribute *attr,
1691 const char *buf, size_t count)
1693 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
1694 struct nct6775_data *data = dev_get_drvdata(dev);
1695 int nr, bit, regindex;
1699 err = kstrtoul(buf, 10, &val);
1705 nr = find_temp_source(data, sattr->index, data->num_temp_beeps);
1709 bit = data->BEEP_BITS[nr + TEMP_ALARM_BASE];
1710 regindex = bit >> 3;
1712 mutex_lock(&data->update_lock);
1714 data->beeps |= (1ULL << bit);
1716 data->beeps &= ~(1ULL << bit);
1717 nct6775_write_value(data, data->REG_BEEP[regindex],
1718 (data->beeps >> (regindex << 3)) & 0xff);
1719 mutex_unlock(&data->update_lock);
1724 static umode_t nct6775_in_is_visible(struct kobject *kobj,
1725 struct attribute *attr, int index)
1727 struct device *dev = container_of(kobj, struct device, kobj);
1728 struct nct6775_data *data = dev_get_drvdata(dev);
1729 int in = index / 5; /* voltage index */
1731 if (!(data->have_in & (1 << in)))
1737 SENSOR_TEMPLATE_2(in_input, "in%d_input", S_IRUGO, show_in_reg, NULL, 0, 0);
1738 SENSOR_TEMPLATE(in_alarm, "in%d_alarm", S_IRUGO, show_alarm, NULL, 0);
1739 SENSOR_TEMPLATE(in_beep, "in%d_beep", S_IWUSR | S_IRUGO, show_beep, store_beep,
1741 SENSOR_TEMPLATE_2(in_min, "in%d_min", S_IWUSR | S_IRUGO, show_in_reg,
1742 store_in_reg, 0, 1);
1743 SENSOR_TEMPLATE_2(in_max, "in%d_max", S_IWUSR | S_IRUGO, show_in_reg,
1744 store_in_reg, 0, 2);
1747 * nct6775_in_is_visible uses the index into the following array
1748 * to determine if attributes should be created or not.
1749 * Any change in order or content must be matched.
1751 static struct sensor_device_template *nct6775_attributes_in_template[] = {
1752 &sensor_dev_template_in_input,
1753 &sensor_dev_template_in_alarm,
1754 &sensor_dev_template_in_beep,
1755 &sensor_dev_template_in_min,
1756 &sensor_dev_template_in_max,
1760 static struct sensor_template_group nct6775_in_template_group = {
1761 .templates = nct6775_attributes_in_template,
1762 .is_visible = nct6775_in_is_visible,
1766 show_fan(struct device *dev, struct device_attribute *attr, char *buf)
1768 struct nct6775_data *data = nct6775_update_device(dev);
1769 struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr);
1770 int nr = sattr->index;
1772 return sprintf(buf, "%d\n", data->rpm[nr]);
1776 show_fan_min(struct device *dev, struct device_attribute *attr, char *buf)
1778 struct nct6775_data *data = nct6775_update_device(dev);
1779 struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr);
1780 int nr = sattr->index;
1782 return sprintf(buf, "%d\n",
1783 data->fan_from_reg_min(data->fan_min[nr],
1784 data->fan_div[nr]));
1788 show_fan_div(struct device *dev, struct device_attribute *attr, char *buf)
1790 struct nct6775_data *data = nct6775_update_device(dev);
1791 struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr);
1792 int nr = sattr->index;
1794 return sprintf(buf, "%u\n", div_from_reg(data->fan_div[nr]));
1798 store_fan_min(struct device *dev, struct device_attribute *attr,
1799 const char *buf, size_t count)
1801 struct nct6775_data *data = dev_get_drvdata(dev);
1802 struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr);
1803 int nr = sattr->index;
1809 err = kstrtoul(buf, 10, &val);
1813 mutex_lock(&data->update_lock);
1814 if (!data->has_fan_div) {
1815 /* NCT6776F or NCT6779D; we know this is a 13 bit register */
1821 val = 1350000U / val;
1822 val = (val & 0x1f) | ((val << 3) & 0xff00);
1824 data->fan_min[nr] = val;
1825 goto write_min; /* Leave fan divider alone */
1828 /* No min limit, alarm disabled */
1829 data->fan_min[nr] = 255;
1830 new_div = data->fan_div[nr]; /* No change */
1831 dev_info(dev, "fan%u low limit and alarm disabled\n", nr + 1);
1834 reg = 1350000U / val;
1835 if (reg >= 128 * 255) {
1837 * Speed below this value cannot possibly be represented,
1838 * even with the highest divider (128)
1840 data->fan_min[nr] = 254;
1841 new_div = 7; /* 128 == (1 << 7) */
1843 "fan%u low limit %lu below minimum %u, set to minimum\n",
1844 nr + 1, val, data->fan_from_reg_min(254, 7));
1847 * Speed above this value cannot possibly be represented,
1848 * even with the lowest divider (1)
1850 data->fan_min[nr] = 1;
1851 new_div = 0; /* 1 == (1 << 0) */
1853 "fan%u low limit %lu above maximum %u, set to maximum\n",
1854 nr + 1, val, data->fan_from_reg_min(1, 0));
1857 * Automatically pick the best divider, i.e. the one such
1858 * that the min limit will correspond to a register value
1859 * in the 96..192 range
1862 while (reg > 192 && new_div < 7) {
1866 data->fan_min[nr] = reg;
1871 * Write both the fan clock divider (if it changed) and the new
1872 * fan min (unconditionally)
1874 if (new_div != data->fan_div[nr]) {
1875 dev_dbg(dev, "fan%u clock divider changed from %u to %u\n",
1876 nr + 1, div_from_reg(data->fan_div[nr]),
1877 div_from_reg(new_div));
1878 data->fan_div[nr] = new_div;
1879 nct6775_write_fan_div_common(data, nr);
1880 /* Give the chip time to sample a new speed value */
1881 data->last_updated = jiffies;
1885 nct6775_write_value(data, data->REG_FAN_MIN[nr], data->fan_min[nr]);
1886 mutex_unlock(&data->update_lock);
1892 show_fan_pulses(struct device *dev, struct device_attribute *attr, char *buf)
1894 struct nct6775_data *data = nct6775_update_device(dev);
1895 struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr);
1896 int p = data->fan_pulses[sattr->index];
1898 return sprintf(buf, "%d\n", p ? : 4);
1902 store_fan_pulses(struct device *dev, struct device_attribute *attr,
1903 const char *buf, size_t count)
1905 struct nct6775_data *data = dev_get_drvdata(dev);
1906 struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr);
1907 int nr = sattr->index;
1912 err = kstrtoul(buf, 10, &val);
1919 mutex_lock(&data->update_lock);
1920 data->fan_pulses[nr] = val & 3;
1921 reg = nct6775_read_value(data, data->REG_FAN_PULSES[nr]);
1922 reg &= ~(0x03 << data->FAN_PULSE_SHIFT[nr]);
1923 reg |= (val & 3) << data->FAN_PULSE_SHIFT[nr];
1924 nct6775_write_value(data, data->REG_FAN_PULSES[nr], reg);
1925 mutex_unlock(&data->update_lock);
1930 static umode_t nct6775_fan_is_visible(struct kobject *kobj,
1931 struct attribute *attr, int index)
1933 struct device *dev = container_of(kobj, struct device, kobj);
1934 struct nct6775_data *data = dev_get_drvdata(dev);
1935 int fan = index / 6; /* fan index */
1936 int nr = index % 6; /* attribute index */
1938 if (!(data->has_fan & (1 << fan)))
1941 if (nr == 1 && data->ALARM_BITS[FAN_ALARM_BASE + fan] == -1)
1943 if (nr == 2 && data->BEEP_BITS[FAN_ALARM_BASE + fan] == -1)
1945 if (nr == 4 && !(data->has_fan_min & (1 << fan)))
1947 if (nr == 5 && data->kind != nct6775)
1953 SENSOR_TEMPLATE(fan_input, "fan%d_input", S_IRUGO, show_fan, NULL, 0);
1954 SENSOR_TEMPLATE(fan_alarm, "fan%d_alarm", S_IRUGO, show_alarm, NULL,
1956 SENSOR_TEMPLATE(fan_beep, "fan%d_beep", S_IWUSR | S_IRUGO, show_beep,
1957 store_beep, FAN_ALARM_BASE);
1958 SENSOR_TEMPLATE(fan_pulses, "fan%d_pulses", S_IWUSR | S_IRUGO, show_fan_pulses,
1959 store_fan_pulses, 0);
1960 SENSOR_TEMPLATE(fan_min, "fan%d_min", S_IWUSR | S_IRUGO, show_fan_min,
1962 SENSOR_TEMPLATE(fan_div, "fan%d_div", S_IRUGO, show_fan_div, NULL, 0);
1965 * nct6775_fan_is_visible uses the index into the following array
1966 * to determine if attributes should be created or not.
1967 * Any change in order or content must be matched.
1969 static struct sensor_device_template *nct6775_attributes_fan_template[] = {
1970 &sensor_dev_template_fan_input,
1971 &sensor_dev_template_fan_alarm, /* 1 */
1972 &sensor_dev_template_fan_beep, /* 2 */
1973 &sensor_dev_template_fan_pulses,
1974 &sensor_dev_template_fan_min, /* 4 */
1975 &sensor_dev_template_fan_div, /* 5 */
1979 static struct sensor_template_group nct6775_fan_template_group = {
1980 .templates = nct6775_attributes_fan_template,
1981 .is_visible = nct6775_fan_is_visible,
1986 show_temp_label(struct device *dev, struct device_attribute *attr, char *buf)
1988 struct nct6775_data *data = nct6775_update_device(dev);
1989 struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr);
1990 int nr = sattr->index;
1992 return sprintf(buf, "%s\n", data->temp_label[data->temp_src[nr]]);
1996 show_temp(struct device *dev, struct device_attribute *attr, char *buf)
1998 struct nct6775_data *data = nct6775_update_device(dev);
1999 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
2001 int index = sattr->index;
2003 return sprintf(buf, "%d\n", LM75_TEMP_FROM_REG(data->temp[index][nr]));
2007 store_temp(struct device *dev, struct device_attribute *attr, const char *buf,
2010 struct nct6775_data *data = dev_get_drvdata(dev);
2011 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
2013 int index = sattr->index;
2017 err = kstrtol(buf, 10, &val);
2021 mutex_lock(&data->update_lock);
2022 data->temp[index][nr] = LM75_TEMP_TO_REG(val);
2023 nct6775_write_temp(data, data->reg_temp[index][nr],
2024 data->temp[index][nr]);
2025 mutex_unlock(&data->update_lock);
2030 show_temp_offset(struct device *dev, struct device_attribute *attr, char *buf)
2032 struct nct6775_data *data = nct6775_update_device(dev);
2033 struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr);
2035 return sprintf(buf, "%d\n", data->temp_offset[sattr->index] * 1000);
2039 store_temp_offset(struct device *dev, struct device_attribute *attr,
2040 const char *buf, size_t count)
2042 struct nct6775_data *data = dev_get_drvdata(dev);
2043 struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr);
2044 int nr = sattr->index;
2048 err = kstrtol(buf, 10, &val);
2052 val = clamp_val(DIV_ROUND_CLOSEST(val, 1000), -128, 127);
2054 mutex_lock(&data->update_lock);
2055 data->temp_offset[nr] = val;
2056 nct6775_write_value(data, data->REG_TEMP_OFFSET[nr], val);
2057 mutex_unlock(&data->update_lock);
2063 show_temp_type(struct device *dev, struct device_attribute *attr, char *buf)
2065 struct nct6775_data *data = nct6775_update_device(dev);
2066 struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr);
2067 int nr = sattr->index;
2069 return sprintf(buf, "%d\n", (int)data->temp_type[nr]);
2073 store_temp_type(struct device *dev, struct device_attribute *attr,
2074 const char *buf, size_t count)
2076 struct nct6775_data *data = nct6775_update_device(dev);
2077 struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr);
2078 int nr = sattr->index;
2081 u8 vbat, diode, vbit, dbit;
2083 err = kstrtoul(buf, 10, &val);
2087 if (val != 1 && val != 3 && val != 4)
2090 mutex_lock(&data->update_lock);
2092 data->temp_type[nr] = val;
2094 dbit = data->DIODE_MASK << nr;
2095 vbat = nct6775_read_value(data, data->REG_VBAT) & ~vbit;
2096 diode = nct6775_read_value(data, data->REG_DIODE) & ~dbit;
2098 case 1: /* CPU diode (diode, current mode) */
2102 case 3: /* diode, voltage mode */
2105 case 4: /* thermistor */
2108 nct6775_write_value(data, data->REG_VBAT, vbat);
2109 nct6775_write_value(data, data->REG_DIODE, diode);
2111 mutex_unlock(&data->update_lock);
2115 static umode_t nct6775_temp_is_visible(struct kobject *kobj,
2116 struct attribute *attr, int index)
2118 struct device *dev = container_of(kobj, struct device, kobj);
2119 struct nct6775_data *data = dev_get_drvdata(dev);
2120 int temp = index / 10; /* temp index */
2121 int nr = index % 10; /* attribute index */
2123 if (!(data->have_temp & (1 << temp)))
2126 if (nr == 2 && find_temp_source(data, temp, data->num_temp_alarms) < 0)
2127 return 0; /* alarm */
2129 if (nr == 3 && find_temp_source(data, temp, data->num_temp_beeps) < 0)
2130 return 0; /* beep */
2132 if (nr == 4 && !data->reg_temp[1][temp]) /* max */
2135 if (nr == 5 && !data->reg_temp[2][temp]) /* max_hyst */
2138 if (nr == 6 && !data->reg_temp[3][temp]) /* crit */
2141 if (nr == 7 && !data->reg_temp[4][temp]) /* lcrit */
2144 /* offset and type only apply to fixed sensors */
2145 if (nr > 7 && !(data->have_temp_fixed & (1 << temp)))
2151 SENSOR_TEMPLATE_2(temp_input, "temp%d_input", S_IRUGO, show_temp, NULL, 0, 0);
2152 SENSOR_TEMPLATE(temp_label, "temp%d_label", S_IRUGO, show_temp_label, NULL, 0);
2153 SENSOR_TEMPLATE_2(temp_max, "temp%d_max", S_IRUGO | S_IWUSR, show_temp,
2155 SENSOR_TEMPLATE_2(temp_max_hyst, "temp%d_max_hyst", S_IRUGO | S_IWUSR,
2156 show_temp, store_temp, 0, 2);
2157 SENSOR_TEMPLATE_2(temp_crit, "temp%d_crit", S_IRUGO | S_IWUSR, show_temp,
2159 SENSOR_TEMPLATE_2(temp_lcrit, "temp%d_lcrit", S_IRUGO | S_IWUSR, show_temp,
2161 SENSOR_TEMPLATE(temp_offset, "temp%d_offset", S_IRUGO | S_IWUSR,
2162 show_temp_offset, store_temp_offset, 0);
2163 SENSOR_TEMPLATE(temp_type, "temp%d_type", S_IRUGO | S_IWUSR, show_temp_type,
2164 store_temp_type, 0);
2165 SENSOR_TEMPLATE(temp_alarm, "temp%d_alarm", S_IRUGO, show_temp_alarm, NULL, 0);
2166 SENSOR_TEMPLATE(temp_beep, "temp%d_beep", S_IRUGO | S_IWUSR, show_temp_beep,
2167 store_temp_beep, 0);
2170 * nct6775_temp_is_visible uses the index into the following array
2171 * to determine if attributes should be created or not.
2172 * Any change in order or content must be matched.
2174 static struct sensor_device_template *nct6775_attributes_temp_template[] = {
2175 &sensor_dev_template_temp_input,
2176 &sensor_dev_template_temp_label,
2177 &sensor_dev_template_temp_alarm, /* 2 */
2178 &sensor_dev_template_temp_beep, /* 3 */
2179 &sensor_dev_template_temp_max, /* 4 */
2180 &sensor_dev_template_temp_max_hyst, /* 5 */
2181 &sensor_dev_template_temp_crit, /* 6 */
2182 &sensor_dev_template_temp_lcrit, /* 7 */
2183 &sensor_dev_template_temp_offset, /* 8 */
2184 &sensor_dev_template_temp_type, /* 9 */
2188 static struct sensor_template_group nct6775_temp_template_group = {
2189 .templates = nct6775_attributes_temp_template,
2190 .is_visible = nct6775_temp_is_visible,
2195 show_pwm_mode(struct device *dev, struct device_attribute *attr, char *buf)
2197 struct nct6775_data *data = nct6775_update_device(dev);
2198 struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr);
2200 return sprintf(buf, "%d\n", !data->pwm_mode[sattr->index]);
2204 store_pwm_mode(struct device *dev, struct device_attribute *attr,
2205 const char *buf, size_t count)
2207 struct nct6775_data *data = dev_get_drvdata(dev);
2208 struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr);
2209 int nr = sattr->index;
2214 err = kstrtoul(buf, 10, &val);
2221 /* Setting DC mode is not supported for all chips/channels */
2222 if (data->REG_PWM_MODE[nr] == 0) {
2228 mutex_lock(&data->update_lock);
2229 data->pwm_mode[nr] = val;
2230 reg = nct6775_read_value(data, data->REG_PWM_MODE[nr]);
2231 reg &= ~data->PWM_MODE_MASK[nr];
2233 reg |= data->PWM_MODE_MASK[nr];
2234 nct6775_write_value(data, data->REG_PWM_MODE[nr], reg);
2235 mutex_unlock(&data->update_lock);
2240 show_pwm(struct device *dev, struct device_attribute *attr, char *buf)
2242 struct nct6775_data *data = nct6775_update_device(dev);
2243 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
2245 int index = sattr->index;
2249 * For automatic fan control modes, show current pwm readings.
2250 * Otherwise, show the configured value.
2252 if (index == 0 && data->pwm_enable[nr] > manual)
2253 pwm = nct6775_read_value(data, data->REG_PWM_READ[nr]);
2255 pwm = data->pwm[index][nr];
2257 return sprintf(buf, "%d\n", pwm);
2261 store_pwm(struct device *dev, struct device_attribute *attr, const char *buf,
2264 struct nct6775_data *data = dev_get_drvdata(dev);
2265 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
2267 int index = sattr->index;
2269 int minval[7] = { 0, 1, 1, data->pwm[2][nr], 0, 0, 0 };
2271 = { 255, 255, data->pwm[3][nr] ? : 255, 255, 255, 255, 255 };
2275 err = kstrtoul(buf, 10, &val);
2278 val = clamp_val(val, minval[index], maxval[index]);
2280 mutex_lock(&data->update_lock);
2281 data->pwm[index][nr] = val;
2282 nct6775_write_value(data, data->REG_PWM[index][nr], val);
2283 if (index == 2) { /* floor: disable if val == 0 */
2284 reg = nct6775_read_value(data, data->REG_TEMP_SEL[nr]);
2288 nct6775_write_value(data, data->REG_TEMP_SEL[nr], reg);
2290 mutex_unlock(&data->update_lock);
2294 /* Returns 0 if OK, -EINVAL otherwise */
2295 static int check_trip_points(struct nct6775_data *data, int nr)
2299 for (i = 0; i < data->auto_pwm_num - 1; i++) {
2300 if (data->auto_temp[nr][i] > data->auto_temp[nr][i + 1])
2303 for (i = 0; i < data->auto_pwm_num - 1; i++) {
2304 if (data->auto_pwm[nr][i] > data->auto_pwm[nr][i + 1])
2307 /* validate critical temperature and pwm if enabled (pwm > 0) */
2308 if (data->auto_pwm[nr][data->auto_pwm_num]) {
2309 if (data->auto_temp[nr][data->auto_pwm_num - 1] >
2310 data->auto_temp[nr][data->auto_pwm_num] ||
2311 data->auto_pwm[nr][data->auto_pwm_num - 1] >
2312 data->auto_pwm[nr][data->auto_pwm_num])
2318 static void pwm_update_registers(struct nct6775_data *data, int nr)
2322 switch (data->pwm_enable[nr]) {
2327 reg = nct6775_read_value(data, data->REG_FAN_MODE[nr]);
2328 reg = (reg & ~data->tolerance_mask) |
2329 (data->target_speed_tolerance[nr] & data->tolerance_mask);
2330 nct6775_write_value(data, data->REG_FAN_MODE[nr], reg);
2331 nct6775_write_value(data, data->REG_TARGET[nr],
2332 data->target_speed[nr] & 0xff);
2333 if (data->REG_TOLERANCE_H) {
2334 reg = (data->target_speed[nr] >> 8) & 0x0f;
2335 reg |= (data->target_speed_tolerance[nr] & 0x38) << 1;
2336 nct6775_write_value(data,
2337 data->REG_TOLERANCE_H[nr],
2341 case thermal_cruise:
2342 nct6775_write_value(data, data->REG_TARGET[nr],
2343 data->target_temp[nr]);
2346 reg = nct6775_read_value(data, data->REG_FAN_MODE[nr]);
2347 reg = (reg & ~data->tolerance_mask) |
2348 data->temp_tolerance[0][nr];
2349 nct6775_write_value(data, data->REG_FAN_MODE[nr], reg);
2355 show_pwm_enable(struct device *dev, struct device_attribute *attr, char *buf)
2357 struct nct6775_data *data = nct6775_update_device(dev);
2358 struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr);
2360 return sprintf(buf, "%d\n", data->pwm_enable[sattr->index]);
2364 store_pwm_enable(struct device *dev, struct device_attribute *attr,
2365 const char *buf, size_t count)
2367 struct nct6775_data *data = dev_get_drvdata(dev);
2368 struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr);
2369 int nr = sattr->index;
2374 err = kstrtoul(buf, 10, &val);
2381 if (val == sf3 && data->kind != nct6775)
2384 if (val == sf4 && check_trip_points(data, nr)) {
2385 dev_err(dev, "Inconsistent trip points, not switching to SmartFan IV mode\n");
2386 dev_err(dev, "Adjust trip points and try again\n");
2390 mutex_lock(&data->update_lock);
2391 data->pwm_enable[nr] = val;
2394 * turn off pwm control: select manual mode, set pwm to maximum
2396 data->pwm[0][nr] = 255;
2397 nct6775_write_value(data, data->REG_PWM[0][nr], 255);
2399 pwm_update_registers(data, nr);
2400 reg = nct6775_read_value(data, data->REG_FAN_MODE[nr]);
2402 reg |= pwm_enable_to_reg(val) << 4;
2403 nct6775_write_value(data, data->REG_FAN_MODE[nr], reg);
2404 mutex_unlock(&data->update_lock);
2409 show_pwm_temp_sel_common(struct nct6775_data *data, char *buf, int src)
2413 for (i = 0; i < NUM_TEMP; i++) {
2414 if (!(data->have_temp & (1 << i)))
2416 if (src == data->temp_src[i]) {
2422 return sprintf(buf, "%d\n", sel);
2426 show_pwm_temp_sel(struct device *dev, struct device_attribute *attr, char *buf)
2428 struct nct6775_data *data = nct6775_update_device(dev);
2429 struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr);
2430 int index = sattr->index;
2432 return show_pwm_temp_sel_common(data, buf, data->pwm_temp_sel[index]);
2436 store_pwm_temp_sel(struct device *dev, struct device_attribute *attr,
2437 const char *buf, size_t count)
2439 struct nct6775_data *data = nct6775_update_device(dev);
2440 struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr);
2441 int nr = sattr->index;
2445 err = kstrtoul(buf, 10, &val);
2448 if (val == 0 || val > NUM_TEMP)
2450 if (!(data->have_temp & (1 << (val - 1))) || !data->temp_src[val - 1])
2453 mutex_lock(&data->update_lock);
2454 src = data->temp_src[val - 1];
2455 data->pwm_temp_sel[nr] = src;
2456 reg = nct6775_read_value(data, data->REG_TEMP_SEL[nr]);
2459 nct6775_write_value(data, data->REG_TEMP_SEL[nr], reg);
2460 mutex_unlock(&data->update_lock);
2466 show_pwm_weight_temp_sel(struct device *dev, struct device_attribute *attr,
2469 struct nct6775_data *data = nct6775_update_device(dev);
2470 struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr);
2471 int index = sattr->index;
2473 return show_pwm_temp_sel_common(data, buf,
2474 data->pwm_weight_temp_sel[index]);
2478 store_pwm_weight_temp_sel(struct device *dev, struct device_attribute *attr,
2479 const char *buf, size_t count)
2481 struct nct6775_data *data = nct6775_update_device(dev);
2482 struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr);
2483 int nr = sattr->index;
2487 err = kstrtoul(buf, 10, &val);
2492 if (val && (!(data->have_temp & (1 << (val - 1))) ||
2493 !data->temp_src[val - 1]))
2496 mutex_lock(&data->update_lock);
2498 src = data->temp_src[val - 1];
2499 data->pwm_weight_temp_sel[nr] = src;
2500 reg = nct6775_read_value(data, data->REG_WEIGHT_TEMP_SEL[nr]);
2502 reg |= (src | 0x80);
2503 nct6775_write_value(data, data->REG_WEIGHT_TEMP_SEL[nr], reg);
2505 data->pwm_weight_temp_sel[nr] = 0;
2506 reg = nct6775_read_value(data, data->REG_WEIGHT_TEMP_SEL[nr]);
2508 nct6775_write_value(data, data->REG_WEIGHT_TEMP_SEL[nr], reg);
2510 mutex_unlock(&data->update_lock);
2516 show_target_temp(struct device *dev, struct device_attribute *attr, char *buf)
2518 struct nct6775_data *data = nct6775_update_device(dev);
2519 struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr);
2521 return sprintf(buf, "%d\n", data->target_temp[sattr->index] * 1000);
2525 store_target_temp(struct device *dev, struct device_attribute *attr,
2526 const char *buf, size_t count)
2528 struct nct6775_data *data = dev_get_drvdata(dev);
2529 struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr);
2530 int nr = sattr->index;
2534 err = kstrtoul(buf, 10, &val);
2538 val = clamp_val(DIV_ROUND_CLOSEST(val, 1000), 0,
2539 data->target_temp_mask);
2541 mutex_lock(&data->update_lock);
2542 data->target_temp[nr] = val;
2543 pwm_update_registers(data, nr);
2544 mutex_unlock(&data->update_lock);
2549 show_target_speed(struct device *dev, struct device_attribute *attr, char *buf)
2551 struct nct6775_data *data = nct6775_update_device(dev);
2552 struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr);
2553 int nr = sattr->index;
2555 return sprintf(buf, "%d\n",
2556 fan_from_reg16(data->target_speed[nr],
2557 data->fan_div[nr]));
2561 store_target_speed(struct device *dev, struct device_attribute *attr,
2562 const char *buf, size_t count)
2564 struct nct6775_data *data = dev_get_drvdata(dev);
2565 struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr);
2566 int nr = sattr->index;
2571 err = kstrtoul(buf, 10, &val);
2575 val = clamp_val(val, 0, 1350000U);
2576 speed = fan_to_reg(val, data->fan_div[nr]);
2578 mutex_lock(&data->update_lock);
2579 data->target_speed[nr] = speed;
2580 pwm_update_registers(data, nr);
2581 mutex_unlock(&data->update_lock);
2586 show_temp_tolerance(struct device *dev, struct device_attribute *attr,
2589 struct nct6775_data *data = nct6775_update_device(dev);
2590 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
2592 int index = sattr->index;
2594 return sprintf(buf, "%d\n", data->temp_tolerance[index][nr] * 1000);
2598 store_temp_tolerance(struct device *dev, struct device_attribute *attr,
2599 const char *buf, size_t count)
2601 struct nct6775_data *data = dev_get_drvdata(dev);
2602 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
2604 int index = sattr->index;
2608 err = kstrtoul(buf, 10, &val);
2612 /* Limit tolerance as needed */
2613 val = clamp_val(DIV_ROUND_CLOSEST(val, 1000), 0, data->tolerance_mask);
2615 mutex_lock(&data->update_lock);
2616 data->temp_tolerance[index][nr] = val;
2618 pwm_update_registers(data, nr);
2620 nct6775_write_value(data,
2621 data->REG_CRITICAL_TEMP_TOLERANCE[nr],
2623 mutex_unlock(&data->update_lock);
2628 * Fan speed tolerance is a tricky beast, since the associated register is
2629 * a tick counter, but the value is reported and configured as rpm.
2630 * Compute resulting low and high rpm values and report the difference.
2633 show_speed_tolerance(struct device *dev, struct device_attribute *attr,
2636 struct nct6775_data *data = nct6775_update_device(dev);
2637 struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr);
2638 int nr = sattr->index;
2639 int low = data->target_speed[nr] - data->target_speed_tolerance[nr];
2640 int high = data->target_speed[nr] + data->target_speed_tolerance[nr];
2650 tolerance = (fan_from_reg16(low, data->fan_div[nr])
2651 - fan_from_reg16(high, data->fan_div[nr])) / 2;
2653 return sprintf(buf, "%d\n", tolerance);
2657 store_speed_tolerance(struct device *dev, struct device_attribute *attr,
2658 const char *buf, size_t count)
2660 struct nct6775_data *data = dev_get_drvdata(dev);
2661 struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr);
2662 int nr = sattr->index;
2667 err = kstrtoul(buf, 10, &val);
2671 high = fan_from_reg16(data->target_speed[nr],
2672 data->fan_div[nr]) + val;
2673 low = fan_from_reg16(data->target_speed[nr],
2674 data->fan_div[nr]) - val;
2680 val = (fan_to_reg(low, data->fan_div[nr]) -
2681 fan_to_reg(high, data->fan_div[nr])) / 2;
2683 /* Limit tolerance as needed */
2684 val = clamp_val(val, 0, data->speed_tolerance_limit);
2686 mutex_lock(&data->update_lock);
2687 data->target_speed_tolerance[nr] = val;
2688 pwm_update_registers(data, nr);
2689 mutex_unlock(&data->update_lock);
2693 SENSOR_TEMPLATE_2(pwm, "pwm%d", S_IWUSR | S_IRUGO, show_pwm, store_pwm, 0, 0);
2694 SENSOR_TEMPLATE(pwm_mode, "pwm%d_mode", S_IWUSR | S_IRUGO, show_pwm_mode,
2696 SENSOR_TEMPLATE(pwm_enable, "pwm%d_enable", S_IWUSR | S_IRUGO, show_pwm_enable,
2697 store_pwm_enable, 0);
2698 SENSOR_TEMPLATE(pwm_temp_sel, "pwm%d_temp_sel", S_IWUSR | S_IRUGO,
2699 show_pwm_temp_sel, store_pwm_temp_sel, 0);
2700 SENSOR_TEMPLATE(pwm_target_temp, "pwm%d_target_temp", S_IWUSR | S_IRUGO,
2701 show_target_temp, store_target_temp, 0);
2702 SENSOR_TEMPLATE(fan_target, "fan%d_target", S_IWUSR | S_IRUGO,
2703 show_target_speed, store_target_speed, 0);
2704 SENSOR_TEMPLATE(fan_tolerance, "fan%d_tolerance", S_IWUSR | S_IRUGO,
2705 show_speed_tolerance, store_speed_tolerance, 0);
2707 /* Smart Fan registers */
2710 show_weight_temp(struct device *dev, struct device_attribute *attr, char *buf)
2712 struct nct6775_data *data = nct6775_update_device(dev);
2713 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
2715 int index = sattr->index;
2717 return sprintf(buf, "%d\n", data->weight_temp[index][nr] * 1000);
2721 store_weight_temp(struct device *dev, struct device_attribute *attr,
2722 const char *buf, size_t count)
2724 struct nct6775_data *data = dev_get_drvdata(dev);
2725 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
2727 int index = sattr->index;
2731 err = kstrtoul(buf, 10, &val);
2735 val = clamp_val(DIV_ROUND_CLOSEST(val, 1000), 0, 255);
2737 mutex_lock(&data->update_lock);
2738 data->weight_temp[index][nr] = val;
2739 nct6775_write_value(data, data->REG_WEIGHT_TEMP[index][nr], val);
2740 mutex_unlock(&data->update_lock);
2744 SENSOR_TEMPLATE(pwm_weight_temp_sel, "pwm%d_weight_temp_sel", S_IWUSR | S_IRUGO,
2745 show_pwm_weight_temp_sel, store_pwm_weight_temp_sel, 0);
2746 SENSOR_TEMPLATE_2(pwm_weight_temp_step, "pwm%d_weight_temp_step",
2747 S_IWUSR | S_IRUGO, show_weight_temp, store_weight_temp, 0, 0);
2748 SENSOR_TEMPLATE_2(pwm_weight_temp_step_tol, "pwm%d_weight_temp_step_tol",
2749 S_IWUSR | S_IRUGO, show_weight_temp, store_weight_temp, 0, 1);
2750 SENSOR_TEMPLATE_2(pwm_weight_temp_step_base, "pwm%d_weight_temp_step_base",
2751 S_IWUSR | S_IRUGO, show_weight_temp, store_weight_temp, 0, 2);
2752 SENSOR_TEMPLATE_2(pwm_weight_duty_step, "pwm%d_weight_duty_step",
2753 S_IWUSR | S_IRUGO, show_pwm, store_pwm, 0, 5);
2754 SENSOR_TEMPLATE_2(pwm_weight_duty_base, "pwm%d_weight_duty_base",
2755 S_IWUSR | S_IRUGO, show_pwm, store_pwm, 0, 6);
2758 show_fan_time(struct device *dev, struct device_attribute *attr, char *buf)
2760 struct nct6775_data *data = nct6775_update_device(dev);
2761 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
2763 int index = sattr->index;
2765 return sprintf(buf, "%d\n",
2766 step_time_from_reg(data->fan_time[index][nr],
2767 data->pwm_mode[nr]));
2771 store_fan_time(struct device *dev, struct device_attribute *attr,
2772 const char *buf, size_t count)
2774 struct nct6775_data *data = dev_get_drvdata(dev);
2775 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
2777 int index = sattr->index;
2781 err = kstrtoul(buf, 10, &val);
2785 val = step_time_to_reg(val, data->pwm_mode[nr]);
2786 mutex_lock(&data->update_lock);
2787 data->fan_time[index][nr] = val;
2788 nct6775_write_value(data, data->REG_FAN_TIME[index][nr], val);
2789 mutex_unlock(&data->update_lock);
2793 #if LINUX_VERSION_CODE < KERNEL_VERSION(3, 13, 0)
2795 show_name(struct device *dev, struct device_attribute *attr, char *buf)
2797 struct nct6775_data *data = dev_get_drvdata(dev);
2799 return sprintf(buf, "%s\n", data->name);
2802 static DEVICE_ATTR(name, S_IRUGO, show_name, NULL);
2806 show_auto_pwm(struct device *dev, struct device_attribute *attr, char *buf)
2808 struct nct6775_data *data = nct6775_update_device(dev);
2809 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
2811 return sprintf(buf, "%d\n", data->auto_pwm[sattr->nr][sattr->index]);
2815 store_auto_pwm(struct device *dev, struct device_attribute *attr,
2816 const char *buf, size_t count)
2818 struct nct6775_data *data = dev_get_drvdata(dev);
2819 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
2821 int point = sattr->index;
2826 err = kstrtoul(buf, 10, &val);
2832 if (point == data->auto_pwm_num) {
2833 if (data->kind != nct6775 && !val)
2835 if (data->kind != nct6779 && val)
2839 mutex_lock(&data->update_lock);
2840 data->auto_pwm[nr][point] = val;
2841 if (point < data->auto_pwm_num) {
2842 nct6775_write_value(data,
2843 NCT6775_AUTO_PWM(data, nr, point),
2844 data->auto_pwm[nr][point]);
2846 switch (data->kind) {
2848 /* disable if needed (pwm == 0) */
2849 reg = nct6775_read_value(data,
2850 NCT6775_REG_CRITICAL_ENAB[nr]);
2855 nct6775_write_value(data, NCT6775_REG_CRITICAL_ENAB[nr],
2859 break; /* always enabled, nothing to do */
2865 nct6775_write_value(data, data->REG_CRITICAL_PWM[nr],
2867 reg = nct6775_read_value(data,
2868 data->REG_CRITICAL_PWM_ENABLE[nr]);
2870 reg &= ~data->CRITICAL_PWM_ENABLE_MASK;
2872 reg |= data->CRITICAL_PWM_ENABLE_MASK;
2873 nct6775_write_value(data,
2874 data->REG_CRITICAL_PWM_ENABLE[nr],
2879 mutex_unlock(&data->update_lock);
2884 show_auto_temp(struct device *dev, struct device_attribute *attr, char *buf)
2886 struct nct6775_data *data = nct6775_update_device(dev);
2887 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
2889 int point = sattr->index;
2892 * We don't know for sure if the temperature is signed or unsigned.
2893 * Assume it is unsigned.
2895 return sprintf(buf, "%d\n", data->auto_temp[nr][point] * 1000);
2899 store_auto_temp(struct device *dev, struct device_attribute *attr,
2900 const char *buf, size_t count)
2902 struct nct6775_data *data = dev_get_drvdata(dev);
2903 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
2905 int point = sattr->index;
2909 err = kstrtoul(buf, 10, &val);
2915 mutex_lock(&data->update_lock);
2916 data->auto_temp[nr][point] = DIV_ROUND_CLOSEST(val, 1000);
2917 if (point < data->auto_pwm_num) {
2918 nct6775_write_value(data,
2919 NCT6775_AUTO_TEMP(data, nr, point),
2920 data->auto_temp[nr][point]);
2922 nct6775_write_value(data, data->REG_CRITICAL_TEMP[nr],
2923 data->auto_temp[nr][point]);
2925 mutex_unlock(&data->update_lock);
2929 static umode_t nct6775_pwm_is_visible(struct kobject *kobj,
2930 struct attribute *attr, int index)
2932 struct device *dev = container_of(kobj, struct device, kobj);
2933 struct nct6775_data *data = dev_get_drvdata(dev);
2934 int pwm = index / 36; /* pwm index */
2935 int nr = index % 36; /* attribute index */
2937 if (!(data->has_pwm & (1 << pwm)))
2940 if ((nr >= 14 && nr <= 18) || nr == 21) /* weight */
2941 if (!data->REG_WEIGHT_TEMP_SEL[pwm])
2943 if (nr == 19 && data->REG_PWM[3] == NULL) /* pwm_max */
2945 if (nr == 20 && data->REG_PWM[4] == NULL) /* pwm_step */
2947 if (nr == 21 && data->REG_PWM[6] == NULL) /* weight_duty_base */
2950 if (nr >= 22 && nr <= 35) { /* auto point */
2951 int api = (nr - 22) / 2; /* auto point index */
2953 if (api > data->auto_pwm_num)
2959 SENSOR_TEMPLATE_2(pwm_stop_time, "pwm%d_stop_time", S_IWUSR | S_IRUGO,
2960 show_fan_time, store_fan_time, 0, 0);
2961 SENSOR_TEMPLATE_2(pwm_step_up_time, "pwm%d_step_up_time", S_IWUSR | S_IRUGO,
2962 show_fan_time, store_fan_time, 0, 1);
2963 SENSOR_TEMPLATE_2(pwm_step_down_time, "pwm%d_step_down_time", S_IWUSR | S_IRUGO,
2964 show_fan_time, store_fan_time, 0, 2);
2965 SENSOR_TEMPLATE_2(pwm_start, "pwm%d_start", S_IWUSR | S_IRUGO, show_pwm,
2967 SENSOR_TEMPLATE_2(pwm_floor, "pwm%d_floor", S_IWUSR | S_IRUGO, show_pwm,
2969 SENSOR_TEMPLATE_2(pwm_temp_tolerance, "pwm%d_temp_tolerance", S_IWUSR | S_IRUGO,
2970 show_temp_tolerance, store_temp_tolerance, 0, 0);
2971 SENSOR_TEMPLATE_2(pwm_crit_temp_tolerance, "pwm%d_crit_temp_tolerance",
2972 S_IWUSR | S_IRUGO, show_temp_tolerance, store_temp_tolerance,
2975 SENSOR_TEMPLATE_2(pwm_max, "pwm%d_max", S_IWUSR | S_IRUGO, show_pwm, store_pwm,
2978 SENSOR_TEMPLATE_2(pwm_step, "pwm%d_step", S_IWUSR | S_IRUGO, show_pwm,
2981 SENSOR_TEMPLATE_2(pwm_auto_point1_pwm, "pwm%d_auto_point1_pwm",
2982 S_IWUSR | S_IRUGO, show_auto_pwm, store_auto_pwm, 0, 0);
2983 SENSOR_TEMPLATE_2(pwm_auto_point1_temp, "pwm%d_auto_point1_temp",
2984 S_IWUSR | S_IRUGO, show_auto_temp, store_auto_temp, 0, 0);
2986 SENSOR_TEMPLATE_2(pwm_auto_point2_pwm, "pwm%d_auto_point2_pwm",
2987 S_IWUSR | S_IRUGO, show_auto_pwm, store_auto_pwm, 0, 1);
2988 SENSOR_TEMPLATE_2(pwm_auto_point2_temp, "pwm%d_auto_point2_temp",
2989 S_IWUSR | S_IRUGO, show_auto_temp, store_auto_temp, 0, 1);
2991 SENSOR_TEMPLATE_2(pwm_auto_point3_pwm, "pwm%d_auto_point3_pwm",
2992 S_IWUSR | S_IRUGO, show_auto_pwm, store_auto_pwm, 0, 2);
2993 SENSOR_TEMPLATE_2(pwm_auto_point3_temp, "pwm%d_auto_point3_temp",
2994 S_IWUSR | S_IRUGO, show_auto_temp, store_auto_temp, 0, 2);
2996 SENSOR_TEMPLATE_2(pwm_auto_point4_pwm, "pwm%d_auto_point4_pwm",
2997 S_IWUSR | S_IRUGO, show_auto_pwm, store_auto_pwm, 0, 3);
2998 SENSOR_TEMPLATE_2(pwm_auto_point4_temp, "pwm%d_auto_point4_temp",
2999 S_IWUSR | S_IRUGO, show_auto_temp, store_auto_temp, 0, 3);
3001 SENSOR_TEMPLATE_2(pwm_auto_point5_pwm, "pwm%d_auto_point5_pwm",
3002 S_IWUSR | S_IRUGO, show_auto_pwm, store_auto_pwm, 0, 4);
3003 SENSOR_TEMPLATE_2(pwm_auto_point5_temp, "pwm%d_auto_point5_temp",
3004 S_IWUSR | S_IRUGO, show_auto_temp, store_auto_temp, 0, 4);
3006 SENSOR_TEMPLATE_2(pwm_auto_point6_pwm, "pwm%d_auto_point6_pwm",
3007 S_IWUSR | S_IRUGO, show_auto_pwm, store_auto_pwm, 0, 5);
3008 SENSOR_TEMPLATE_2(pwm_auto_point6_temp, "pwm%d_auto_point6_temp",
3009 S_IWUSR | S_IRUGO, show_auto_temp, store_auto_temp, 0, 5);
3011 SENSOR_TEMPLATE_2(pwm_auto_point7_pwm, "pwm%d_auto_point7_pwm",
3012 S_IWUSR | S_IRUGO, show_auto_pwm, store_auto_pwm, 0, 6);
3013 SENSOR_TEMPLATE_2(pwm_auto_point7_temp, "pwm%d_auto_point7_temp",
3014 S_IWUSR | S_IRUGO, show_auto_temp, store_auto_temp, 0, 6);
3017 * nct6775_pwm_is_visible uses the index into the following array
3018 * to determine if attributes should be created or not.
3019 * Any change in order or content must be matched.
3021 static struct sensor_device_template *nct6775_attributes_pwm_template[] = {
3022 &sensor_dev_template_pwm,
3023 &sensor_dev_template_pwm_mode,
3024 &sensor_dev_template_pwm_enable,
3025 &sensor_dev_template_pwm_temp_sel,
3026 &sensor_dev_template_pwm_temp_tolerance,
3027 &sensor_dev_template_pwm_crit_temp_tolerance,
3028 &sensor_dev_template_pwm_target_temp,
3029 &sensor_dev_template_fan_target,
3030 &sensor_dev_template_fan_tolerance,
3031 &sensor_dev_template_pwm_stop_time,
3032 &sensor_dev_template_pwm_step_up_time,
3033 &sensor_dev_template_pwm_step_down_time,
3034 &sensor_dev_template_pwm_start,
3035 &sensor_dev_template_pwm_floor,
3036 &sensor_dev_template_pwm_weight_temp_sel, /* 14 */
3037 &sensor_dev_template_pwm_weight_temp_step,
3038 &sensor_dev_template_pwm_weight_temp_step_tol,
3039 &sensor_dev_template_pwm_weight_temp_step_base,
3040 &sensor_dev_template_pwm_weight_duty_step, /* 18 */
3041 &sensor_dev_template_pwm_max, /* 19 */
3042 &sensor_dev_template_pwm_step, /* 20 */
3043 &sensor_dev_template_pwm_weight_duty_base, /* 21 */
3044 &sensor_dev_template_pwm_auto_point1_pwm, /* 22 */
3045 &sensor_dev_template_pwm_auto_point1_temp,
3046 &sensor_dev_template_pwm_auto_point2_pwm,
3047 &sensor_dev_template_pwm_auto_point2_temp,
3048 &sensor_dev_template_pwm_auto_point3_pwm,
3049 &sensor_dev_template_pwm_auto_point3_temp,
3050 &sensor_dev_template_pwm_auto_point4_pwm,
3051 &sensor_dev_template_pwm_auto_point4_temp,
3052 &sensor_dev_template_pwm_auto_point5_pwm,
3053 &sensor_dev_template_pwm_auto_point5_temp,
3054 &sensor_dev_template_pwm_auto_point6_pwm,
3055 &sensor_dev_template_pwm_auto_point6_temp,
3056 &sensor_dev_template_pwm_auto_point7_pwm,
3057 &sensor_dev_template_pwm_auto_point7_temp, /* 35 */
3062 static struct sensor_template_group nct6775_pwm_template_group = {
3063 .templates = nct6775_attributes_pwm_template,
3064 .is_visible = nct6775_pwm_is_visible,
3069 show_vid(struct device *dev, struct device_attribute *attr, char *buf)
3071 struct nct6775_data *data = dev_get_drvdata(dev);
3073 return sprintf(buf, "%d\n", vid_from_reg(data->vid, data->vrm));
3076 static DEVICE_ATTR(cpu0_vid, S_IRUGO, show_vid, NULL);
3078 /* Case open detection */
3081 clear_caseopen(struct device *dev, struct device_attribute *attr,
3082 const char *buf, size_t count)
3084 struct nct6775_data *data = dev_get_drvdata(dev);
3085 int nr = to_sensor_dev_attr(attr)->index - INTRUSION_ALARM_BASE;
3090 if (kstrtoul(buf, 10, &val) || val != 0)
3093 mutex_lock(&data->update_lock);
3096 * Use CR registers to clear caseopen status.
3097 * The CR registers are the same for all chips, and not all chips
3098 * support clearing the caseopen status through "regular" registers.
3100 ret = superio_enter(data->sioreg);
3106 superio_select(data->sioreg, NCT6775_LD_ACPI);
3107 reg = superio_inb(data->sioreg, NCT6775_REG_CR_CASEOPEN_CLR[nr]);
3108 reg |= NCT6775_CR_CASEOPEN_CLR_MASK[nr];
3109 superio_outb(data->sioreg, NCT6775_REG_CR_CASEOPEN_CLR[nr], reg);
3110 reg &= ~NCT6775_CR_CASEOPEN_CLR_MASK[nr];
3111 superio_outb(data->sioreg, NCT6775_REG_CR_CASEOPEN_CLR[nr], reg);
3112 superio_exit(data->sioreg);
3114 data->valid = false; /* Force cache refresh */
3116 mutex_unlock(&data->update_lock);
3120 static SENSOR_DEVICE_ATTR(intrusion0_alarm, S_IWUSR | S_IRUGO, show_alarm,
3121 clear_caseopen, INTRUSION_ALARM_BASE);
3122 static SENSOR_DEVICE_ATTR(intrusion1_alarm, S_IWUSR | S_IRUGO, show_alarm,
3123 clear_caseopen, INTRUSION_ALARM_BASE + 1);
3124 static SENSOR_DEVICE_ATTR(intrusion0_beep, S_IWUSR | S_IRUGO, show_beep,
3125 store_beep, INTRUSION_ALARM_BASE);
3126 static SENSOR_DEVICE_ATTR(intrusion1_beep, S_IWUSR | S_IRUGO, show_beep,
3127 store_beep, INTRUSION_ALARM_BASE + 1);
3128 static SENSOR_DEVICE_ATTR(beep_enable, S_IWUSR | S_IRUGO, show_beep,
3129 store_beep, BEEP_ENABLE_BASE);
3131 static umode_t nct6775_other_is_visible(struct kobject *kobj,
3132 struct attribute *attr, int index)
3134 struct device *dev = container_of(kobj, struct device, kobj);
3135 struct nct6775_data *data = dev_get_drvdata(dev);
3137 if (index == 0 && !data->have_vid)
3140 if (index == 1 || index == 2) {
3141 if (data->ALARM_BITS[INTRUSION_ALARM_BASE + index - 1] < 0)
3145 if (index == 3 || index == 4) {
3146 if (data->BEEP_BITS[INTRUSION_ALARM_BASE + index - 3] < 0)
3154 * nct6775_other_is_visible uses the index into the following array
3155 * to determine if attributes should be created or not.
3156 * Any change in order or content must be matched.
3158 static struct attribute *nct6775_attributes_other[] = {
3159 &dev_attr_cpu0_vid.attr, /* 0 */
3160 &sensor_dev_attr_intrusion0_alarm.dev_attr.attr, /* 1 */
3161 &sensor_dev_attr_intrusion1_alarm.dev_attr.attr, /* 2 */
3162 &sensor_dev_attr_intrusion0_beep.dev_attr.attr, /* 3 */
3163 &sensor_dev_attr_intrusion1_beep.dev_attr.attr, /* 4 */
3164 &sensor_dev_attr_beep_enable.dev_attr.attr, /* 5 */
3165 #if LINUX_VERSION_CODE < KERNEL_VERSION(3, 13, 0)
3166 &dev_attr_name.attr,
3171 static const struct attribute_group nct6775_group_other = {
3172 .attrs = nct6775_attributes_other,
3173 .is_visible = nct6775_other_is_visible,
3176 static inline void nct6775_init_device(struct nct6775_data *data)
3181 /* Start monitoring if needed */
3182 if (data->REG_CONFIG) {
3183 tmp = nct6775_read_value(data, data->REG_CONFIG);
3185 nct6775_write_value(data, data->REG_CONFIG, tmp | 0x01);
3188 /* Enable temperature sensors if needed */
3189 for (i = 0; i < NUM_TEMP; i++) {
3190 if (!(data->have_temp & (1 << i)))
3192 if (!data->reg_temp_config[i])
3194 tmp = nct6775_read_value(data, data->reg_temp_config[i]);
3196 nct6775_write_value(data, data->reg_temp_config[i],
3200 /* Enable VBAT monitoring if needed */
3201 tmp = nct6775_read_value(data, data->REG_VBAT);
3203 nct6775_write_value(data, data->REG_VBAT, tmp | 0x01);
3205 diode = nct6775_read_value(data, data->REG_DIODE);
3207 for (i = 0; i < data->temp_fixed_num; i++) {
3208 if (!(data->have_temp_fixed & (1 << i)))
3210 if ((tmp & (data->DIODE_MASK << i))) /* diode */
3212 = 3 - ((diode >> i) & data->DIODE_MASK);
3213 else /* thermistor */
3214 data->temp_type[i] = 4;
3219 nct6775_check_fan_inputs(struct nct6775_data *data)
3221 bool fan3pin, fan4pin, fan4min, fan5pin, fan6pin;
3222 bool pwm3pin, pwm4pin, pwm5pin, pwm6pin;
3223 int sioreg = data->sioreg;
3226 /* Store SIO_REG_ENABLE for use during resume */
3227 superio_select(sioreg, NCT6775_LD_HWM);
3228 data->sio_reg_enable = superio_inb(sioreg, SIO_REG_ENABLE);
3230 /* fan4 and fan5 share some pins with the GPIO and serial flash */
3231 if (data->kind == nct6775) {
3232 regval = superio_inb(sioreg, 0x2c);
3234 fan3pin = regval & (1 << 6);
3235 pwm3pin = regval & (1 << 7);
3237 /* On NCT6775, fan4 shares pins with the fdc interface */
3238 fan4pin = !(superio_inb(sioreg, 0x2A) & 0x80);
3245 } else if (data->kind == nct6776) {
3246 bool gpok = superio_inb(sioreg, 0x27) & 0x80;
3247 const char *board_vendor, *board_name;
3249 board_vendor = dmi_get_system_info(DMI_BOARD_VENDOR);
3250 board_name = dmi_get_system_info(DMI_BOARD_NAME);
3252 if (board_name && board_vendor &&
3253 !strcmp(board_vendor, "ASRock")) {
3255 * Auxiliary fan monitoring is not enabled on ASRock
3256 * Z77 Pro4-M if booted in UEFI Ultra-FastBoot mode.
3257 * Observed with BIOS version 2.00.
3259 if (!strcmp(board_name, "Z77 Pro4-M")) {
3260 if ((data->sio_reg_enable & 0xe0) != 0xe0) {
3261 data->sio_reg_enable |= 0xe0;
3262 superio_outb(sioreg, SIO_REG_ENABLE,
3263 data->sio_reg_enable);
3268 if (data->sio_reg_enable & 0x80)
3271 fan3pin = !(superio_inb(sioreg, 0x24) & 0x40);
3273 if (data->sio_reg_enable & 0x40)
3276 fan4pin = superio_inb(sioreg, 0x1C) & 0x01;
3278 if (data->sio_reg_enable & 0x20)
3281 fan5pin = superio_inb(sioreg, 0x1C) & 0x02;
3289 } else if (data->kind == nct6106) {
3290 regval = superio_inb(sioreg, 0x24);
3291 fan3pin = !(regval & 0x80);
3292 pwm3pin = regval & 0x08;
3301 } else { /* NCT6779D, NCT6791D, NCT6792D, or NCT6793D */
3302 regval = superio_inb(sioreg, 0x1c);
3304 fan3pin = !(regval & (1 << 5));
3305 fan4pin = !(regval & (1 << 6));
3306 fan5pin = !(regval & (1 << 7));
3308 pwm3pin = !(regval & (1 << 0));
3309 pwm4pin = !(regval & (1 << 1));
3310 pwm5pin = !(regval & (1 << 2));
3314 if (data->kind == nct6791 || data->kind == nct6792 ||
3315 data->kind == nct6793) {
3316 regval = superio_inb(sioreg, 0x2d);
3317 fan6pin = (regval & (1 << 1));
3318 pwm6pin = (regval & (1 << 0));
3319 } else { /* NCT6779D */
3325 /* fan 1 and 2 (0x03) are always present */
3326 data->has_fan = 0x03 | (fan3pin << 2) | (fan4pin << 3) |
3327 (fan5pin << 4) | (fan6pin << 5);
3328 data->has_fan_min = 0x03 | (fan3pin << 2) | (fan4min << 3) |
3330 data->has_pwm = 0x03 | (pwm3pin << 2) | (pwm4pin << 3) |
3331 (pwm5pin << 4) | (pwm6pin << 5);
3334 static void add_temp_sensors(struct nct6775_data *data, const u16 *regp,
3335 int *available, int *mask)
3340 for (i = 0; i < data->pwm_num && *available; i++) {
3345 src = nct6775_read_value(data, regp[i]);
3347 if (!src || (*mask & (1 << src)))
3349 if (src >= data->temp_label_num ||
3350 !strlen(data->temp_label[src]))
3353 index = __ffs(*available);
3354 nct6775_write_value(data, data->REG_TEMP_SOURCE[index], src);
3355 *available &= ~(1 << index);
3360 static int nct6775_probe(struct platform_device *pdev)
3362 struct device *dev = &pdev->dev;
3363 struct nct6775_sio_data *sio_data = dev_get_platdata(dev);
3364 struct nct6775_data *data;
3365 struct resource *res;
3367 int src, mask, available;
3368 const u16 *reg_temp, *reg_temp_over, *reg_temp_hyst, *reg_temp_config;
3369 const u16 *reg_temp_mon, *reg_temp_alternate, *reg_temp_crit;
3370 const u16 *reg_temp_crit_l = NULL, *reg_temp_crit_h = NULL;
3371 int num_reg_temp, num_reg_temp_mon;
3373 struct attribute_group *group;
3374 struct device *hwmon_dev;
3375 int num_attr_groups = 0;
3377 res = platform_get_resource(pdev, IORESOURCE_IO, 0);
3378 if (!devm_request_region(&pdev->dev, res->start, IOREGION_LENGTH,
3382 data = devm_kzalloc(&pdev->dev, sizeof(struct nct6775_data),
3387 data->kind = sio_data->kind;
3388 data->sioreg = sio_data->sioreg;
3389 data->addr = res->start;
3390 mutex_init(&data->update_lock);
3391 data->name = nct6775_device_names[data->kind];
3392 data->bank = 0xff; /* Force initial bank selection */
3393 platform_set_drvdata(pdev, data);
3395 switch (data->kind) {
3399 data->auto_pwm_num = 4;
3400 data->temp_fixed_num = 3;
3401 data->num_temp_alarms = 6;
3402 data->num_temp_beeps = 6;
3404 data->fan_from_reg = fan_from_reg13;
3405 data->fan_from_reg_min = fan_from_reg13;
3407 data->temp_label = nct6776_temp_label;
3408 data->temp_label_num = ARRAY_SIZE(nct6776_temp_label);
3410 data->REG_VBAT = NCT6106_REG_VBAT;
3411 data->REG_DIODE = NCT6106_REG_DIODE;
3412 data->DIODE_MASK = NCT6106_DIODE_MASK;
3413 data->REG_VIN = NCT6106_REG_IN;
3414 data->REG_IN_MINMAX[0] = NCT6106_REG_IN_MIN;
3415 data->REG_IN_MINMAX[1] = NCT6106_REG_IN_MAX;
3416 data->REG_TARGET = NCT6106_REG_TARGET;
3417 data->REG_FAN = NCT6106_REG_FAN;
3418 data->REG_FAN_MODE = NCT6106_REG_FAN_MODE;
3419 data->REG_FAN_MIN = NCT6106_REG_FAN_MIN;
3420 data->REG_FAN_PULSES = NCT6106_REG_FAN_PULSES;
3421 data->FAN_PULSE_SHIFT = NCT6106_FAN_PULSE_SHIFT;
3422 data->REG_FAN_TIME[0] = NCT6106_REG_FAN_STOP_TIME;
3423 data->REG_FAN_TIME[1] = NCT6106_REG_FAN_STEP_UP_TIME;
3424 data->REG_FAN_TIME[2] = NCT6106_REG_FAN_STEP_DOWN_TIME;
3425 data->REG_PWM[0] = NCT6106_REG_PWM;
3426 data->REG_PWM[1] = NCT6106_REG_FAN_START_OUTPUT;
3427 data->REG_PWM[2] = NCT6106_REG_FAN_STOP_OUTPUT;
3428 data->REG_PWM[5] = NCT6106_REG_WEIGHT_DUTY_STEP;
3429 data->REG_PWM[6] = NCT6106_REG_WEIGHT_DUTY_BASE;
3430 data->REG_PWM_READ = NCT6106_REG_PWM_READ;
3431 data->REG_PWM_MODE = NCT6106_REG_PWM_MODE;
3432 data->PWM_MODE_MASK = NCT6106_PWM_MODE_MASK;
3433 data->REG_AUTO_TEMP = NCT6106_REG_AUTO_TEMP;
3434 data->REG_AUTO_PWM = NCT6106_REG_AUTO_PWM;
3435 data->REG_CRITICAL_TEMP = NCT6106_REG_CRITICAL_TEMP;
3436 data->REG_CRITICAL_TEMP_TOLERANCE
3437 = NCT6106_REG_CRITICAL_TEMP_TOLERANCE;
3438 data->REG_CRITICAL_PWM_ENABLE = NCT6106_REG_CRITICAL_PWM_ENABLE;
3439 data->CRITICAL_PWM_ENABLE_MASK
3440 = NCT6106_CRITICAL_PWM_ENABLE_MASK;
3441 data->REG_CRITICAL_PWM = NCT6106_REG_CRITICAL_PWM;
3442 data->REG_TEMP_OFFSET = NCT6106_REG_TEMP_OFFSET;
3443 data->REG_TEMP_SOURCE = NCT6106_REG_TEMP_SOURCE;
3444 data->REG_TEMP_SEL = NCT6106_REG_TEMP_SEL;
3445 data->REG_WEIGHT_TEMP_SEL = NCT6106_REG_WEIGHT_TEMP_SEL;
3446 data->REG_WEIGHT_TEMP[0] = NCT6106_REG_WEIGHT_TEMP_STEP;
3447 data->REG_WEIGHT_TEMP[1] = NCT6106_REG_WEIGHT_TEMP_STEP_TOL;
3448 data->REG_WEIGHT_TEMP[2] = NCT6106_REG_WEIGHT_TEMP_BASE;
3449 data->REG_ALARM = NCT6106_REG_ALARM;
3450 data->ALARM_BITS = NCT6106_ALARM_BITS;
3451 data->REG_BEEP = NCT6106_REG_BEEP;
3452 data->BEEP_BITS = NCT6106_BEEP_BITS;
3454 reg_temp = NCT6106_REG_TEMP;
3455 reg_temp_mon = NCT6106_REG_TEMP_MON;
3456 num_reg_temp = ARRAY_SIZE(NCT6106_REG_TEMP);
3457 num_reg_temp_mon = ARRAY_SIZE(NCT6106_REG_TEMP_MON);
3458 reg_temp_over = NCT6106_REG_TEMP_OVER;
3459 reg_temp_hyst = NCT6106_REG_TEMP_HYST;
3460 reg_temp_config = NCT6106_REG_TEMP_CONFIG;
3461 reg_temp_alternate = NCT6106_REG_TEMP_ALTERNATE;
3462 reg_temp_crit = NCT6106_REG_TEMP_CRIT;
3463 reg_temp_crit_l = NCT6106_REG_TEMP_CRIT_L;
3464 reg_temp_crit_h = NCT6106_REG_TEMP_CRIT_H;
3470 data->auto_pwm_num = 6;
3471 data->has_fan_div = true;
3472 data->temp_fixed_num = 3;
3473 data->num_temp_alarms = 3;
3474 data->num_temp_beeps = 3;
3476 data->ALARM_BITS = NCT6775_ALARM_BITS;
3477 data->BEEP_BITS = NCT6775_BEEP_BITS;
3479 data->fan_from_reg = fan_from_reg16;
3480 data->fan_from_reg_min = fan_from_reg8;
3481 data->target_temp_mask = 0x7f;
3482 data->tolerance_mask = 0x0f;
3483 data->speed_tolerance_limit = 15;
3485 data->temp_label = nct6775_temp_label;
3486 data->temp_label_num = ARRAY_SIZE(nct6775_temp_label);
3488 data->REG_CONFIG = NCT6775_REG_CONFIG;
3489 data->REG_VBAT = NCT6775_REG_VBAT;
3490 data->REG_DIODE = NCT6775_REG_DIODE;
3491 data->DIODE_MASK = NCT6775_DIODE_MASK;
3492 data->REG_VIN = NCT6775_REG_IN;
3493 data->REG_IN_MINMAX[0] = NCT6775_REG_IN_MIN;
3494 data->REG_IN_MINMAX[1] = NCT6775_REG_IN_MAX;
3495 data->REG_TARGET = NCT6775_REG_TARGET;
3496 data->REG_FAN = NCT6775_REG_FAN;
3497 data->REG_FAN_MODE = NCT6775_REG_FAN_MODE;
3498 data->REG_FAN_MIN = NCT6775_REG_FAN_MIN;
3499 data->REG_FAN_PULSES = NCT6775_REG_FAN_PULSES;
3500 data->FAN_PULSE_SHIFT = NCT6775_FAN_PULSE_SHIFT;
3501 data->REG_FAN_TIME[0] = NCT6775_REG_FAN_STOP_TIME;
3502 data->REG_FAN_TIME[1] = NCT6775_REG_FAN_STEP_UP_TIME;
3503 data->REG_FAN_TIME[2] = NCT6775_REG_FAN_STEP_DOWN_TIME;
3504 data->REG_PWM[0] = NCT6775_REG_PWM;
3505 data->REG_PWM[1] = NCT6775_REG_FAN_START_OUTPUT;
3506 data->REG_PWM[2] = NCT6775_REG_FAN_STOP_OUTPUT;
3507 data->REG_PWM[3] = NCT6775_REG_FAN_MAX_OUTPUT;
3508 data->REG_PWM[4] = NCT6775_REG_FAN_STEP_OUTPUT;
3509 data->REG_PWM[5] = NCT6775_REG_WEIGHT_DUTY_STEP;
3510 data->REG_PWM_READ = NCT6775_REG_PWM_READ;
3511 data->REG_PWM_MODE = NCT6775_REG_PWM_MODE;
3512 data->PWM_MODE_MASK = NCT6775_PWM_MODE_MASK;
3513 data->REG_AUTO_TEMP = NCT6775_REG_AUTO_TEMP;
3514 data->REG_AUTO_PWM = NCT6775_REG_AUTO_PWM;
3515 data->REG_CRITICAL_TEMP = NCT6775_REG_CRITICAL_TEMP;
3516 data->REG_CRITICAL_TEMP_TOLERANCE
3517 = NCT6775_REG_CRITICAL_TEMP_TOLERANCE;
3518 data->REG_TEMP_OFFSET = NCT6775_REG_TEMP_OFFSET;
3519 data->REG_TEMP_SOURCE = NCT6775_REG_TEMP_SOURCE;
3520 data->REG_TEMP_SEL = NCT6775_REG_TEMP_SEL;
3521 data->REG_WEIGHT_TEMP_SEL = NCT6775_REG_WEIGHT_TEMP_SEL;
3522 data->REG_WEIGHT_TEMP[0] = NCT6775_REG_WEIGHT_TEMP_STEP;
3523 data->REG_WEIGHT_TEMP[1] = NCT6775_REG_WEIGHT_TEMP_STEP_TOL;
3524 data->REG_WEIGHT_TEMP[2] = NCT6775_REG_WEIGHT_TEMP_BASE;
3525 data->REG_ALARM = NCT6775_REG_ALARM;
3526 data->REG_BEEP = NCT6775_REG_BEEP;
3528 reg_temp = NCT6775_REG_TEMP;
3529 reg_temp_mon = NCT6775_REG_TEMP_MON;
3530 num_reg_temp = ARRAY_SIZE(NCT6775_REG_TEMP);
3531 num_reg_temp_mon = ARRAY_SIZE(NCT6775_REG_TEMP_MON);
3532 reg_temp_over = NCT6775_REG_TEMP_OVER;
3533 reg_temp_hyst = NCT6775_REG_TEMP_HYST;
3534 reg_temp_config = NCT6775_REG_TEMP_CONFIG;
3535 reg_temp_alternate = NCT6775_REG_TEMP_ALTERNATE;
3536 reg_temp_crit = NCT6775_REG_TEMP_CRIT;
3542 data->auto_pwm_num = 4;
3543 data->has_fan_div = false;
3544 data->temp_fixed_num = 3;
3545 data->num_temp_alarms = 3;
3546 data->num_temp_beeps = 6;
3548 data->ALARM_BITS = NCT6776_ALARM_BITS;
3549 data->BEEP_BITS = NCT6776_BEEP_BITS;
3551 data->fan_from_reg = fan_from_reg13;
3552 data->fan_from_reg_min = fan_from_reg13;
3553 data->target_temp_mask = 0xff;
3554 data->tolerance_mask = 0x07;
3555 data->speed_tolerance_limit = 63;
3557 data->temp_label = nct6776_temp_label;
3558 data->temp_label_num = ARRAY_SIZE(nct6776_temp_label);
3560 data->REG_CONFIG = NCT6775_REG_CONFIG;
3561 data->REG_VBAT = NCT6775_REG_VBAT;
3562 data->REG_DIODE = NCT6775_REG_DIODE;
3563 data->DIODE_MASK = NCT6775_DIODE_MASK;
3564 data->REG_VIN = NCT6775_REG_IN;
3565 data->REG_IN_MINMAX[0] = NCT6775_REG_IN_MIN;
3566 data->REG_IN_MINMAX[1] = NCT6775_REG_IN_MAX;
3567 data->REG_TARGET = NCT6775_REG_TARGET;
3568 data->REG_FAN = NCT6775_REG_FAN;
3569 data->REG_FAN_MODE = NCT6775_REG_FAN_MODE;
3570 data->REG_FAN_MIN = NCT6776_REG_FAN_MIN;
3571 data->REG_FAN_PULSES = NCT6776_REG_FAN_PULSES;
3572 data->FAN_PULSE_SHIFT = NCT6775_FAN_PULSE_SHIFT;
3573 data->REG_FAN_TIME[0] = NCT6775_REG_FAN_STOP_TIME;
3574 data->REG_FAN_TIME[1] = NCT6775_REG_FAN_STEP_DOWN_TIME;
3575 data->REG_FAN_TIME[2] = NCT6775_REG_FAN_STEP_UP_TIME;
3576 data->REG_TOLERANCE_H = NCT6776_REG_TOLERANCE_H;
3577 data->REG_PWM[0] = NCT6775_REG_PWM;
3578 data->REG_PWM[1] = NCT6775_REG_FAN_START_OUTPUT;
3579 data->REG_PWM[2] = NCT6775_REG_FAN_STOP_OUTPUT;
3580 data->REG_PWM[5] = NCT6775_REG_WEIGHT_DUTY_STEP;
3581 data->REG_PWM[6] = NCT6776_REG_WEIGHT_DUTY_BASE;
3582 data->REG_PWM_READ = NCT6775_REG_PWM_READ;
3583 data->REG_PWM_MODE = NCT6776_REG_PWM_MODE;
3584 data->PWM_MODE_MASK = NCT6776_PWM_MODE_MASK;
3585 data->REG_AUTO_TEMP = NCT6775_REG_AUTO_TEMP;
3586 data->REG_AUTO_PWM = NCT6775_REG_AUTO_PWM;
3587 data->REG_CRITICAL_TEMP = NCT6775_REG_CRITICAL_TEMP;
3588 data->REG_CRITICAL_TEMP_TOLERANCE
3589 = NCT6775_REG_CRITICAL_TEMP_TOLERANCE;
3590 data->REG_TEMP_OFFSET = NCT6775_REG_TEMP_OFFSET;
3591 data->REG_TEMP_SOURCE = NCT6775_REG_TEMP_SOURCE;
3592 data->REG_TEMP_SEL = NCT6775_REG_TEMP_SEL;
3593 data->REG_WEIGHT_TEMP_SEL = NCT6775_REG_WEIGHT_TEMP_SEL;
3594 data->REG_WEIGHT_TEMP[0] = NCT6775_REG_WEIGHT_TEMP_STEP;
3595 data->REG_WEIGHT_TEMP[1] = NCT6775_REG_WEIGHT_TEMP_STEP_TOL;
3596 data->REG_WEIGHT_TEMP[2] = NCT6775_REG_WEIGHT_TEMP_BASE;
3597 data->REG_ALARM = NCT6775_REG_ALARM;
3598 data->REG_BEEP = NCT6776_REG_BEEP;
3600 reg_temp = NCT6775_REG_TEMP;
3601 reg_temp_mon = NCT6775_REG_TEMP_MON;
3602 num_reg_temp = ARRAY_SIZE(NCT6775_REG_TEMP);
3603 num_reg_temp_mon = ARRAY_SIZE(NCT6775_REG_TEMP_MON);
3604 reg_temp_over = NCT6775_REG_TEMP_OVER;
3605 reg_temp_hyst = NCT6775_REG_TEMP_HYST;
3606 reg_temp_config = NCT6776_REG_TEMP_CONFIG;
3607 reg_temp_alternate = NCT6776_REG_TEMP_ALTERNATE;
3608 reg_temp_crit = NCT6776_REG_TEMP_CRIT;
3614 data->auto_pwm_num = 4;
3615 data->has_fan_div = false;
3616 data->temp_fixed_num = 6;
3617 data->num_temp_alarms = 2;
3618 data->num_temp_beeps = 2;
3620 data->ALARM_BITS = NCT6779_ALARM_BITS;
3621 data->BEEP_BITS = NCT6779_BEEP_BITS;
3623 data->fan_from_reg = fan_from_reg13;
3624 data->fan_from_reg_min = fan_from_reg13;
3625 data->target_temp_mask = 0xff;
3626 data->tolerance_mask = 0x07;
3627 data->speed_tolerance_limit = 63;
3629 data->temp_label = nct6779_temp_label;
3630 data->temp_label_num = NCT6779_NUM_LABELS;
3632 data->REG_CONFIG = NCT6775_REG_CONFIG;
3633 data->REG_VBAT = NCT6775_REG_VBAT;
3634 data->REG_DIODE = NCT6775_REG_DIODE;
3635 data->DIODE_MASK = NCT6775_DIODE_MASK;
3636 data->REG_VIN = NCT6779_REG_IN;
3637 data->REG_IN_MINMAX[0] = NCT6775_REG_IN_MIN;
3638 data->REG_IN_MINMAX[1] = NCT6775_REG_IN_MAX;
3639 data->REG_TARGET = NCT6775_REG_TARGET;
3640 data->REG_FAN = NCT6779_REG_FAN;
3641 data->REG_FAN_MODE = NCT6775_REG_FAN_MODE;
3642 data->REG_FAN_MIN = NCT6776_REG_FAN_MIN;
3643 data->REG_FAN_PULSES = NCT6779_REG_FAN_PULSES;
3644 data->FAN_PULSE_SHIFT = NCT6775_FAN_PULSE_SHIFT;
3645 data->REG_FAN_TIME[0] = NCT6775_REG_FAN_STOP_TIME;
3646 data->REG_FAN_TIME[1] = NCT6775_REG_FAN_STEP_DOWN_TIME;
3647 data->REG_FAN_TIME[2] = NCT6775_REG_FAN_STEP_UP_TIME;
3648 data->REG_TOLERANCE_H = NCT6776_REG_TOLERANCE_H;
3649 data->REG_PWM[0] = NCT6775_REG_PWM;
3650 data->REG_PWM[1] = NCT6775_REG_FAN_START_OUTPUT;
3651 data->REG_PWM[2] = NCT6775_REG_FAN_STOP_OUTPUT;
3652 data->REG_PWM[5] = NCT6775_REG_WEIGHT_DUTY_STEP;
3653 data->REG_PWM[6] = NCT6776_REG_WEIGHT_DUTY_BASE;
3654 data->REG_PWM_READ = NCT6775_REG_PWM_READ;
3655 data->REG_PWM_MODE = NCT6776_REG_PWM_MODE;
3656 data->PWM_MODE_MASK = NCT6776_PWM_MODE_MASK;
3657 data->REG_AUTO_TEMP = NCT6775_REG_AUTO_TEMP;
3658 data->REG_AUTO_PWM = NCT6775_REG_AUTO_PWM;
3659 data->REG_CRITICAL_TEMP = NCT6775_REG_CRITICAL_TEMP;
3660 data->REG_CRITICAL_TEMP_TOLERANCE
3661 = NCT6775_REG_CRITICAL_TEMP_TOLERANCE;
3662 data->REG_CRITICAL_PWM_ENABLE = NCT6779_REG_CRITICAL_PWM_ENABLE;
3663 data->CRITICAL_PWM_ENABLE_MASK
3664 = NCT6779_CRITICAL_PWM_ENABLE_MASK;
3665 data->REG_CRITICAL_PWM = NCT6779_REG_CRITICAL_PWM;
3666 data->REG_TEMP_OFFSET = NCT6779_REG_TEMP_OFFSET;
3667 data->REG_TEMP_SOURCE = NCT6775_REG_TEMP_SOURCE;
3668 data->REG_TEMP_SEL = NCT6775_REG_TEMP_SEL;
3669 data->REG_WEIGHT_TEMP_SEL = NCT6775_REG_WEIGHT_TEMP_SEL;
3670 data->REG_WEIGHT_TEMP[0] = NCT6775_REG_WEIGHT_TEMP_STEP;
3671 data->REG_WEIGHT_TEMP[1] = NCT6775_REG_WEIGHT_TEMP_STEP_TOL;
3672 data->REG_WEIGHT_TEMP[2] = NCT6775_REG_WEIGHT_TEMP_BASE;
3673 data->REG_ALARM = NCT6779_REG_ALARM;
3674 data->REG_BEEP = NCT6776_REG_BEEP;
3676 reg_temp = NCT6779_REG_TEMP;
3677 reg_temp_mon = NCT6779_REG_TEMP_MON;
3678 num_reg_temp = ARRAY_SIZE(NCT6779_REG_TEMP);
3679 num_reg_temp_mon = ARRAY_SIZE(NCT6779_REG_TEMP_MON);
3680 reg_temp_over = NCT6779_REG_TEMP_OVER;
3681 reg_temp_hyst = NCT6779_REG_TEMP_HYST;
3682 reg_temp_config = NCT6779_REG_TEMP_CONFIG;
3683 reg_temp_alternate = NCT6779_REG_TEMP_ALTERNATE;
3684 reg_temp_crit = NCT6779_REG_TEMP_CRIT;
3692 data->auto_pwm_num = 4;
3693 data->has_fan_div = false;
3694 data->temp_fixed_num = 6;
3695 data->num_temp_alarms = 2;
3696 data->num_temp_beeps = 2;
3698 data->ALARM_BITS = NCT6791_ALARM_BITS;
3699 data->BEEP_BITS = NCT6779_BEEP_BITS;
3701 data->fan_from_reg = fan_from_reg13;
3702 data->fan_from_reg_min = fan_from_reg13;
3703 data->target_temp_mask = 0xff;
3704 data->tolerance_mask = 0x07;
3705 data->speed_tolerance_limit = 63;
3707 data->temp_label = nct6779_temp_label;
3708 data->temp_label_num = NCT6791_NUM_LABELS;
3710 data->REG_CONFIG = NCT6775_REG_CONFIG;
3711 data->REG_VBAT = NCT6775_REG_VBAT;
3712 data->REG_DIODE = NCT6775_REG_DIODE;
3713 data->DIODE_MASK = NCT6775_DIODE_MASK;
3714 data->REG_VIN = NCT6779_REG_IN;
3715 data->REG_IN_MINMAX[0] = NCT6775_REG_IN_MIN;
3716 data->REG_IN_MINMAX[1] = NCT6775_REG_IN_MAX;
3717 data->REG_TARGET = NCT6775_REG_TARGET;
3718 data->REG_FAN = NCT6779_REG_FAN;
3719 data->REG_FAN_MODE = NCT6775_REG_FAN_MODE;
3720 data->REG_FAN_MIN = NCT6776_REG_FAN_MIN;
3721 data->REG_FAN_PULSES = NCT6779_REG_FAN_PULSES;
3722 data->FAN_PULSE_SHIFT = NCT6775_FAN_PULSE_SHIFT;
3723 data->REG_FAN_TIME[0] = NCT6775_REG_FAN_STOP_TIME;
3724 data->REG_FAN_TIME[1] = NCT6775_REG_FAN_STEP_DOWN_TIME;
3725 data->REG_FAN_TIME[2] = NCT6775_REG_FAN_STEP_UP_TIME;
3726 data->REG_TOLERANCE_H = NCT6776_REG_TOLERANCE_H;
3727 data->REG_PWM[0] = NCT6775_REG_PWM;
3728 data->REG_PWM[1] = NCT6775_REG_FAN_START_OUTPUT;
3729 data->REG_PWM[2] = NCT6775_REG_FAN_STOP_OUTPUT;
3730 data->REG_PWM[5] = NCT6791_REG_WEIGHT_DUTY_STEP;
3731 data->REG_PWM[6] = NCT6791_REG_WEIGHT_DUTY_BASE;
3732 data->REG_PWM_READ = NCT6775_REG_PWM_READ;
3733 data->REG_PWM_MODE = NCT6776_REG_PWM_MODE;
3734 data->PWM_MODE_MASK = NCT6776_PWM_MODE_MASK;
3735 data->REG_AUTO_TEMP = NCT6775_REG_AUTO_TEMP;
3736 data->REG_AUTO_PWM = NCT6775_REG_AUTO_PWM;
3737 data->REG_CRITICAL_TEMP = NCT6775_REG_CRITICAL_TEMP;
3738 data->REG_CRITICAL_TEMP_TOLERANCE
3739 = NCT6775_REG_CRITICAL_TEMP_TOLERANCE;
3740 data->REG_CRITICAL_PWM_ENABLE = NCT6779_REG_CRITICAL_PWM_ENABLE;
3741 data->CRITICAL_PWM_ENABLE_MASK
3742 = NCT6779_CRITICAL_PWM_ENABLE_MASK;
3743 data->REG_CRITICAL_PWM = NCT6779_REG_CRITICAL_PWM;
3744 data->REG_TEMP_OFFSET = NCT6779_REG_TEMP_OFFSET;
3745 data->REG_TEMP_SOURCE = NCT6775_REG_TEMP_SOURCE;
3746 data->REG_TEMP_SEL = NCT6775_REG_TEMP_SEL;
3747 data->REG_WEIGHT_TEMP_SEL = NCT6791_REG_WEIGHT_TEMP_SEL;
3748 data->REG_WEIGHT_TEMP[0] = NCT6791_REG_WEIGHT_TEMP_STEP;
3749 data->REG_WEIGHT_TEMP[1] = NCT6791_REG_WEIGHT_TEMP_STEP_TOL;
3750 data->REG_WEIGHT_TEMP[2] = NCT6791_REG_WEIGHT_TEMP_BASE;
3751 data->REG_ALARM = NCT6791_REG_ALARM;
3752 if (data->kind == nct6791)
3753 data->REG_BEEP = NCT6776_REG_BEEP;
3755 data->REG_BEEP = NCT6792_REG_BEEP;
3757 reg_temp = NCT6779_REG_TEMP;
3758 num_reg_temp = ARRAY_SIZE(NCT6779_REG_TEMP);
3759 if (data->kind == nct6791) {
3760 reg_temp_mon = NCT6779_REG_TEMP_MON;
3761 num_reg_temp_mon = ARRAY_SIZE(NCT6779_REG_TEMP_MON);
3763 reg_temp_mon = NCT6792_REG_TEMP_MON;
3764 num_reg_temp_mon = ARRAY_SIZE(NCT6792_REG_TEMP_MON);
3766 reg_temp_over = NCT6779_REG_TEMP_OVER;
3767 reg_temp_hyst = NCT6779_REG_TEMP_HYST;
3768 reg_temp_config = NCT6779_REG_TEMP_CONFIG;
3769 reg_temp_alternate = NCT6779_REG_TEMP_ALTERNATE;
3770 reg_temp_crit = NCT6779_REG_TEMP_CRIT;
3776 data->have_in = (1 << data->in_num) - 1;
3777 data->have_temp = 0;
3780 * On some boards, not all available temperature sources are monitored,
3781 * even though some of the monitoring registers are unused.
3782 * Get list of unused monitoring registers, then detect if any fan
3783 * controls are configured to use unmonitored temperature sources.
3784 * If so, assign the unmonitored temperature sources to available
3785 * monitoring registers.
3789 for (i = 0; i < num_reg_temp; i++) {
3790 if (reg_temp[i] == 0)
3793 src = nct6775_read_value(data, data->REG_TEMP_SOURCE[i]) & 0x1f;
3794 if (!src || (mask & (1 << src)))
3795 available |= 1 << i;
3801 * Now find unmonitored temperature registers and enable monitoring
3802 * if additional monitoring registers are available.
3804 add_temp_sensors(data, data->REG_TEMP_SEL, &available, &mask);
3805 add_temp_sensors(data, data->REG_WEIGHT_TEMP_SEL, &available, &mask);
3808 s = NUM_TEMP_FIXED; /* First dynamic temperature attribute */
3809 for (i = 0; i < num_reg_temp; i++) {
3810 if (reg_temp[i] == 0)
3813 src = nct6775_read_value(data, data->REG_TEMP_SOURCE[i]) & 0x1f;
3814 if (!src || (mask & (1 << src)))
3817 if (src >= data->temp_label_num ||
3818 !strlen(data->temp_label[src])) {
3820 "Invalid temperature source %d at index %d, source register 0x%x, temp register 0x%x\n",
3821 src, i, data->REG_TEMP_SOURCE[i], reg_temp[i]);
3827 /* Use fixed index for SYSTIN(1), CPUTIN(2), AUXTIN(3) */
3828 if (src <= data->temp_fixed_num) {
3829 data->have_temp |= 1 << (src - 1);
3830 data->have_temp_fixed |= 1 << (src - 1);
3831 data->reg_temp[0][src - 1] = reg_temp[i];
3832 data->reg_temp[1][src - 1] = reg_temp_over[i];
3833 data->reg_temp[2][src - 1] = reg_temp_hyst[i];
3834 if (reg_temp_crit_h && reg_temp_crit_h[i])
3835 data->reg_temp[3][src - 1] = reg_temp_crit_h[i];
3836 else if (reg_temp_crit[src - 1])
3837 data->reg_temp[3][src - 1]
3838 = reg_temp_crit[src - 1];
3839 if (reg_temp_crit_l && reg_temp_crit_l[i])
3840 data->reg_temp[4][src - 1] = reg_temp_crit_l[i];
3841 data->reg_temp_config[src - 1] = reg_temp_config[i];
3842 data->temp_src[src - 1] = src;
3849 /* Use dynamic index for other sources */
3850 data->have_temp |= 1 << s;
3851 data->reg_temp[0][s] = reg_temp[i];
3852 data->reg_temp[1][s] = reg_temp_over[i];
3853 data->reg_temp[2][s] = reg_temp_hyst[i];
3854 data->reg_temp_config[s] = reg_temp_config[i];
3855 if (reg_temp_crit_h && reg_temp_crit_h[i])
3856 data->reg_temp[3][s] = reg_temp_crit_h[i];
3857 else if (reg_temp_crit[src - 1])
3858 data->reg_temp[3][s] = reg_temp_crit[src - 1];
3859 if (reg_temp_crit_l && reg_temp_crit_l[i])
3860 data->reg_temp[4][s] = reg_temp_crit_l[i];
3862 data->temp_src[s] = src;
3867 * Repeat with temperatures used for fan control.
3868 * This set of registers does not support limits.
3870 for (i = 0; i < num_reg_temp_mon; i++) {
3871 if (reg_temp_mon[i] == 0)
3874 src = nct6775_read_value(data, data->REG_TEMP_SEL[i]) & 0x1f;
3875 if (!src || (mask & (1 << src)))
3878 if (src >= data->temp_label_num ||
3879 !strlen(data->temp_label[src])) {
3881 "Invalid temperature source %d at index %d, source register 0x%x, temp register 0x%x\n",
3882 src, i, data->REG_TEMP_SEL[i],
3889 /* Use fixed index for SYSTIN(1), CPUTIN(2), AUXTIN(3) */
3890 if (src <= data->temp_fixed_num) {
3891 if (data->have_temp & (1 << (src - 1)))
3893 data->have_temp |= 1 << (src - 1);
3894 data->have_temp_fixed |= 1 << (src - 1);
3895 data->reg_temp[0][src - 1] = reg_temp_mon[i];
3896 data->temp_src[src - 1] = src;
3903 /* Use dynamic index for other sources */
3904 data->have_temp |= 1 << s;
3905 data->reg_temp[0][s] = reg_temp_mon[i];
3906 data->temp_src[s] = src;
3910 #ifdef USE_ALTERNATE
3912 * Go through the list of alternate temp registers and enable
3914 * The temperature is already monitored if the respective bit in <mask>
3917 for (i = 0; i < data->temp_label_num - 1; i++) {
3918 if (!reg_temp_alternate[i])
3920 if (mask & (1 << (i + 1)))
3922 if (i < data->temp_fixed_num) {
3923 if (data->have_temp & (1 << i))
3925 data->have_temp |= 1 << i;
3926 data->have_temp_fixed |= 1 << i;
3927 data->reg_temp[0][i] = reg_temp_alternate[i];
3928 if (i < num_reg_temp) {
3929 data->reg_temp[1][i] = reg_temp_over[i];
3930 data->reg_temp[2][i] = reg_temp_hyst[i];
3932 data->temp_src[i] = i + 1;
3936 if (s >= NUM_TEMP) /* Abort if no more space */
3939 data->have_temp |= 1 << s;
3940 data->reg_temp[0][s] = reg_temp_alternate[i];
3941 data->temp_src[s] = i + 1;
3944 #endif /* USE_ALTERNATE */
3946 /* Initialize the chip */
3947 nct6775_init_device(data);
3949 err = superio_enter(sio_data->sioreg);
3953 cr2a = superio_inb(sio_data->sioreg, 0x2a);
3954 switch (data->kind) {
3956 data->have_vid = (cr2a & 0x40);
3959 data->have_vid = (cr2a & 0x60) == 0x40;
3971 * We can get the VID input values directly at logical device D 0xe3.
3973 if (data->have_vid) {
3974 superio_select(sio_data->sioreg, NCT6775_LD_VID);
3975 data->vid = superio_inb(sio_data->sioreg, 0xe3);
3976 data->vrm = vid_which_vrm();
3982 superio_select(sio_data->sioreg, NCT6775_LD_HWM);
3983 tmp = superio_inb(sio_data->sioreg,
3984 NCT6775_REG_CR_FAN_DEBOUNCE);
3985 switch (data->kind) {
4002 superio_outb(sio_data->sioreg, NCT6775_REG_CR_FAN_DEBOUNCE,
4004 dev_info(&pdev->dev, "Enabled fan debounce for chip %s\n",
4008 nct6775_check_fan_inputs(data);
4010 superio_exit(sio_data->sioreg);
4012 /* Read fan clock dividers immediately */
4013 nct6775_init_fan_common(dev, data);
4015 /* Register sysfs hooks */
4016 group = nct6775_create_attr_group(dev, &nct6775_pwm_template_group,
4019 return PTR_ERR(group);
4021 data->groups[num_attr_groups++] = group;
4023 group = nct6775_create_attr_group(dev, &nct6775_in_template_group,
4024 fls(data->have_in));
4026 return PTR_ERR(group);
4028 data->groups[num_attr_groups++] = group;
4030 group = nct6775_create_attr_group(dev, &nct6775_fan_template_group,
4031 fls(data->has_fan));
4033 return PTR_ERR(group);
4035 data->groups[num_attr_groups++] = group;
4037 group = nct6775_create_attr_group(dev, &nct6775_temp_template_group,
4038 fls(data->have_temp));
4040 return PTR_ERR(group);
4042 data->groups[num_attr_groups++] = group;
4043 data->groups[num_attr_groups++] = &nct6775_group_other;
4045 #if LINUX_VERSION_CODE < KERNEL_VERSION(3, 13, 0)
4046 err = sysfs_create_groups(&dev->kobj, data->groups);
4049 hwmon_dev = hwmon_device_register(dev);
4050 if (IS_ERR(hwmon_dev)) {
4051 sysfs_remove_groups(&dev->kobj, data->groups);
4052 return PTR_ERR(hwmon_dev);
4054 data->hwmon_dev = hwmon_dev;
4056 hwmon_dev = devm_hwmon_device_register_with_groups(dev, data->name,
4057 data, data->groups);
4059 return PTR_ERR_OR_ZERO(hwmon_dev);
4062 #if LINUX_VERSION_CODE < KERNEL_VERSION(3, 13, 0)
4063 static int nct6775_remove(struct platform_device *pdev)
4065 struct nct6775_data *data = platform_get_drvdata(pdev);
4067 hwmon_device_unregister(data->hwmon_dev);
4068 sysfs_remove_groups(&pdev->dev.kobj, data->groups);
4073 static void nct6791_enable_io_mapping(int sioaddr)
4077 val = superio_inb(sioaddr, NCT6791_REG_HM_IO_SPACE_LOCK_ENABLE);
4079 pr_info("Enabling hardware monitor logical device mappings.\n");
4080 superio_outb(sioaddr, NCT6791_REG_HM_IO_SPACE_LOCK_ENABLE,
4085 static int __maybe_unused nct6775_suspend(struct device *dev)
4087 struct nct6775_data *data = nct6775_update_device(dev);
4089 mutex_lock(&data->update_lock);
4090 data->vbat = nct6775_read_value(data, data->REG_VBAT);
4091 if (data->kind == nct6775) {
4092 data->fandiv1 = nct6775_read_value(data, NCT6775_REG_FANDIV1);
4093 data->fandiv2 = nct6775_read_value(data, NCT6775_REG_FANDIV2);
4095 mutex_unlock(&data->update_lock);
4100 static int __maybe_unused nct6775_resume(struct device *dev)
4102 struct nct6775_data *data = dev_get_drvdata(dev);
4103 int sioreg = data->sioreg;
4107 mutex_lock(&data->update_lock);
4108 data->bank = 0xff; /* Force initial bank selection */
4110 err = superio_enter(sioreg);
4114 superio_select(sioreg, NCT6775_LD_HWM);
4115 reg = superio_inb(sioreg, SIO_REG_ENABLE);
4116 if (reg != data->sio_reg_enable)
4117 superio_outb(sioreg, SIO_REG_ENABLE, data->sio_reg_enable);
4119 if (data->kind == nct6791 || data->kind == nct6792 ||
4120 data->kind == nct6793)
4121 nct6791_enable_io_mapping(sioreg);
4123 superio_exit(sioreg);
4125 /* Restore limits */
4126 for (i = 0; i < data->in_num; i++) {
4127 if (!(data->have_in & (1 << i)))
4130 nct6775_write_value(data, data->REG_IN_MINMAX[0][i],
4132 nct6775_write_value(data, data->REG_IN_MINMAX[1][i],
4136 for (i = 0; i < ARRAY_SIZE(data->fan_min); i++) {
4137 if (!(data->has_fan_min & (1 << i)))
4140 nct6775_write_value(data, data->REG_FAN_MIN[i],
4144 for (i = 0; i < NUM_TEMP; i++) {
4145 if (!(data->have_temp & (1 << i)))
4148 for (j = 1; j < ARRAY_SIZE(data->reg_temp); j++)
4149 if (data->reg_temp[j][i])
4150 nct6775_write_temp(data, data->reg_temp[j][i],
4154 /* Restore other settings */
4155 nct6775_write_value(data, data->REG_VBAT, data->vbat);
4156 if (data->kind == nct6775) {
4157 nct6775_write_value(data, NCT6775_REG_FANDIV1, data->fandiv1);
4158 nct6775_write_value(data, NCT6775_REG_FANDIV2, data->fandiv2);
4162 /* Force re-reading all values */
4163 data->valid = false;
4164 mutex_unlock(&data->update_lock);
4169 static SIMPLE_DEV_PM_OPS(nct6775_dev_pm_ops, nct6775_suspend, nct6775_resume);
4171 static struct platform_driver nct6775_driver = {
4173 .owner = THIS_MODULE,
4175 .pm = &nct6775_dev_pm_ops,
4177 .probe = nct6775_probe,
4178 #if LINUX_VERSION_CODE < KERNEL_VERSION(3, 13, 0)
4179 .remove = nct6775_remove,
4183 /* nct6775_find() looks for a '627 in the Super-I/O config space */
4184 static int __init nct6775_find(int sioaddr, struct nct6775_sio_data *sio_data)
4190 err = superio_enter(sioaddr);
4197 val = (superio_inb(sioaddr, SIO_REG_DEVID) << 8)
4198 | superio_inb(sioaddr, SIO_REG_DEVID + 1);
4199 switch (val & SIO_ID_MASK) {
4200 case SIO_NCT6106_ID:
4201 sio_data->kind = nct6106;
4203 case SIO_NCT6775_ID:
4204 sio_data->kind = nct6775;
4206 case SIO_NCT6776_ID:
4207 sio_data->kind = nct6776;
4209 case SIO_NCT6779_ID:
4210 sio_data->kind = nct6779;
4212 case SIO_NCT6791_ID:
4213 sio_data->kind = nct6791;
4215 case SIO_NCT6792_ID:
4216 sio_data->kind = nct6792;
4218 case SIO_NCT6793_ID:
4219 sio_data->kind = nct6793;
4223 pr_debug("unsupported chip ID: 0x%04x\n", val);
4224 superio_exit(sioaddr);
4228 /* We have a known chip, find the HWM I/O address */
4229 superio_select(sioaddr, NCT6775_LD_HWM);
4230 val = (superio_inb(sioaddr, SIO_REG_ADDR) << 8)
4231 | superio_inb(sioaddr, SIO_REG_ADDR + 1);
4232 addr = val & IOREGION_ALIGNMENT;
4234 pr_err("Refusing to enable a Super-I/O device with a base I/O port 0\n");
4235 superio_exit(sioaddr);
4239 /* Activate logical device if needed */
4240 val = superio_inb(sioaddr, SIO_REG_ENABLE);
4241 if (!(val & 0x01)) {
4242 pr_warn("Forcibly enabling Super-I/O. Sensor is probably unusable.\n");
4243 superio_outb(sioaddr, SIO_REG_ENABLE, val | 0x01);
4246 if (sio_data->kind == nct6791 || sio_data->kind == nct6792 ||
4247 sio_data->kind == nct6793)
4248 nct6791_enable_io_mapping(sioaddr);
4250 superio_exit(sioaddr);
4251 pr_info("Found %s or compatible chip at %#x:%#x\n",
4252 nct6775_sio_names[sio_data->kind], sioaddr, addr);
4253 sio_data->sioreg = sioaddr;
4259 * when Super-I/O functions move to a separate file, the Super-I/O
4260 * bus will manage the lifetime of the device and this module will only keep
4261 * track of the nct6775 driver. But since we use platform_device_alloc(), we
4262 * must keep track of the device
4264 static struct platform_device *pdev[2];
4266 static int __init sensors_nct6775_init(void)
4271 struct resource res;
4272 struct nct6775_sio_data sio_data;
4273 int sioaddr[2] = { 0x2e, 0x4e };
4275 err = platform_driver_register(&nct6775_driver);
4280 * initialize sio_data->kind and sio_data->sioreg.
4282 * when Super-I/O functions move to a separate file, the Super-I/O
4283 * driver will probe 0x2e and 0x4e and auto-detect the presence of a
4284 * nct6775 hardware monitor, and call probe()
4286 for (i = 0; i < ARRAY_SIZE(pdev); i++) {
4287 address = nct6775_find(sioaddr[i], &sio_data);
4293 pdev[i] = platform_device_alloc(DRVNAME, address);
4296 goto exit_device_unregister;
4299 err = platform_device_add_data(pdev[i], &sio_data,
4300 sizeof(struct nct6775_sio_data));
4302 goto exit_device_put;
4304 memset(&res, 0, sizeof(res));
4306 res.start = address + IOREGION_OFFSET;
4307 res.end = address + IOREGION_OFFSET + IOREGION_LENGTH - 1;
4308 res.flags = IORESOURCE_IO;
4310 err = acpi_check_resource_conflict(&res);
4312 platform_device_put(pdev[i]);
4317 err = platform_device_add_resources(pdev[i], &res, 1);
4319 goto exit_device_put;
4321 /* platform_device_add calls probe() */
4322 err = platform_device_add(pdev[i]);
4324 goto exit_device_put;
4328 goto exit_unregister;
4334 platform_device_put(pdev[i]);
4335 exit_device_unregister:
4338 platform_device_unregister(pdev[i]);
4341 platform_driver_unregister(&nct6775_driver);
4345 static void __exit sensors_nct6775_exit(void)
4349 for (i = 0; i < ARRAY_SIZE(pdev); i++) {
4351 platform_device_unregister(pdev[i]);
4353 platform_driver_unregister(&nct6775_driver);
4356 MODULE_AUTHOR("Guenter Roeck <linux@roeck-us.net>");
4357 MODULE_DESCRIPTION("Driver for NCT6775F and compatible chips");
4358 MODULE_LICENSE("GPL");
4360 module_init(sensors_nct6775_init);
4361 module_exit(sensors_nct6775_exit);