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
43 * nct6795d 14 6 6 2+6 0xd350 0xc1 0x5ca3
46 * #temp lists the number of monitored temperature sources (first value) plus
47 * the number of directly connectable temperature sensors (second value).
50 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
52 #include <linux/module.h>
53 #include <linux/init.h>
54 #include <linux/slab.h>
55 #include <linux/jiffies.h>
56 #include <linux/platform_device.h>
57 #include <linux/hwmon.h>
58 #include <linux/hwmon-sysfs.h>
59 #include <linux/hwmon-vid.h>
60 #include <linux/err.h>
61 #include <linux/mutex.h>
62 #include <linux/acpi.h>
63 #include <linux/bitops.h>
64 #include <linux/dmi.h>
70 enum kinds { nct6106, nct6775, nct6776, nct6779, nct6791, nct6792, nct6793,
73 /* used to set data->name = nct6775_device_names[data->sio_kind] */
74 static const char * const nct6775_device_names[] = {
85 static const char * const nct6775_sio_names[] __initconst = {
96 static unsigned short force_id;
97 module_param(force_id, ushort, 0);
98 MODULE_PARM_DESC(force_id, "Override the detected device ID");
100 static unsigned short fan_debounce;
101 module_param(fan_debounce, ushort, 0);
102 MODULE_PARM_DESC(fan_debounce, "Enable debouncing for fan RPM signal");
104 #define DRVNAME "nct6775"
107 * Super-I/O constants and functions
110 #define NCT6775_LD_ACPI 0x0a
111 #define NCT6775_LD_HWM 0x0b
112 #define NCT6775_LD_VID 0x0d
113 #define NCT6775_LD_12 0x12
115 #define SIO_REG_LDSEL 0x07 /* Logical device select */
116 #define SIO_REG_DEVID 0x20 /* Device ID (2 bytes) */
117 #define SIO_REG_ENABLE 0x30 /* Logical device enable */
118 #define SIO_REG_ADDR 0x60 /* Logical device address (2 bytes) */
120 #define SIO_NCT6106_ID 0xc450
121 #define SIO_NCT6775_ID 0xb470
122 #define SIO_NCT6776_ID 0xc330
123 #define SIO_NCT6779_ID 0xc560
124 #define SIO_NCT6791_ID 0xc800
125 #define SIO_NCT6792_ID 0xc910
126 #define SIO_NCT6793_ID 0xd120
127 #define SIO_NCT6795_ID 0xd350
128 #define SIO_ID_MASK 0xFFF0
130 enum pwm_enable { off, manual, thermal_cruise, speed_cruise, sf3, sf4 };
133 superio_outb(int ioreg, int reg, int val)
136 outb(val, ioreg + 1);
140 superio_inb(int ioreg, int reg)
143 return inb(ioreg + 1);
147 superio_select(int ioreg, int ld)
149 outb(SIO_REG_LDSEL, ioreg);
154 superio_enter(int ioreg)
157 * Try to reserve <ioreg> and <ioreg + 1> for exclusive access.
159 if (!request_muxed_region(ioreg, 2, DRVNAME))
169 superio_exit(int ioreg)
173 outb(0x02, ioreg + 1);
174 release_region(ioreg, 2);
181 #define IOREGION_ALIGNMENT (~7)
182 #define IOREGION_OFFSET 5
183 #define IOREGION_LENGTH 2
184 #define ADDR_REG_OFFSET 0
185 #define DATA_REG_OFFSET 1
187 #define NCT6775_REG_BANK 0x4E
188 #define NCT6775_REG_CONFIG 0x40
191 * Not currently used:
192 * REG_MAN_ID has the value 0x5ca3 for all supported chips.
193 * REG_CHIP_ID == 0x88/0xa1/0xc1 depending on chip model.
194 * REG_MAN_ID is at port 0x4f
195 * REG_CHIP_ID is at port 0x58
198 #define NUM_TEMP 10 /* Max number of temp attribute sets w/ limits*/
199 #define NUM_TEMP_FIXED 6 /* Max number of fixed temp attribute sets */
201 #define NUM_REG_ALARM 7 /* Max number of alarm registers */
202 #define NUM_REG_BEEP 5 /* Max number of beep registers */
206 #define TEMP_SOURCE_VIRTUAL 0x1f
208 /* Common and NCT6775 specific data */
210 /* Voltage min/max registers for nr=7..14 are in bank 5 */
212 static const u16 NCT6775_REG_IN_MAX[] = {
213 0x2b, 0x2d, 0x2f, 0x31, 0x33, 0x35, 0x37, 0x554, 0x556, 0x558, 0x55a,
214 0x55c, 0x55e, 0x560, 0x562 };
215 static const u16 NCT6775_REG_IN_MIN[] = {
216 0x2c, 0x2e, 0x30, 0x32, 0x34, 0x36, 0x38, 0x555, 0x557, 0x559, 0x55b,
217 0x55d, 0x55f, 0x561, 0x563 };
218 static const u16 NCT6775_REG_IN[] = {
219 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x550, 0x551, 0x552
222 #define NCT6775_REG_VBAT 0x5D
223 #define NCT6775_REG_DIODE 0x5E
224 #define NCT6775_DIODE_MASK 0x02
226 #define NCT6775_REG_FANDIV1 0x506
227 #define NCT6775_REG_FANDIV2 0x507
229 #define NCT6775_REG_CR_FAN_DEBOUNCE 0xf0
231 static const u16 NCT6775_REG_ALARM[NUM_REG_ALARM] = { 0x459, 0x45A, 0x45B };
233 /* 0..15 voltages, 16..23 fans, 24..29 temperatures, 30..31 intrusion */
235 static const s8 NCT6775_ALARM_BITS[] = {
236 0, 1, 2, 3, 8, 21, 20, 16, /* in0.. in7 */
237 17, -1, -1, -1, -1, -1, -1, /* in8..in14 */
239 6, 7, 11, -1, -1, /* fan1..fan5 */
240 -1, -1, -1, /* unused */
241 4, 5, 13, -1, -1, -1, /* temp1..temp6 */
242 12, -1 }; /* intrusion0, intrusion1 */
244 #define FAN_ALARM_BASE 16
245 #define TEMP_ALARM_BASE 24
246 #define INTRUSION_ALARM_BASE 30
248 static const u16 NCT6775_REG_BEEP[NUM_REG_BEEP] = { 0x56, 0x57, 0x453, 0x4e };
251 * 0..14 voltages, 15 global beep enable, 16..23 fans, 24..29 temperatures,
254 static const s8 NCT6775_BEEP_BITS[] = {
255 0, 1, 2, 3, 8, 9, 10, 16, /* in0.. in7 */
256 17, -1, -1, -1, -1, -1, -1, /* in8..in14 */
257 21, /* global beep enable */
258 6, 7, 11, 28, -1, /* fan1..fan5 */
259 -1, -1, -1, /* unused */
260 4, 5, 13, -1, -1, -1, /* temp1..temp6 */
261 12, -1 }; /* intrusion0, intrusion1 */
263 #define BEEP_ENABLE_BASE 15
265 static const u8 NCT6775_REG_CR_CASEOPEN_CLR[] = { 0xe6, 0xee };
266 static const u8 NCT6775_CR_CASEOPEN_CLR_MASK[] = { 0x20, 0x01 };
268 /* DC or PWM output fan configuration */
269 static const u8 NCT6775_REG_PWM_MODE[] = { 0x04, 0x04, 0x12 };
270 static const u8 NCT6775_PWM_MODE_MASK[] = { 0x01, 0x02, 0x01 };
272 /* Advanced Fan control, some values are common for all fans */
274 static const u16 NCT6775_REG_TARGET[] = {
275 0x101, 0x201, 0x301, 0x801, 0x901, 0xa01 };
276 static const u16 NCT6775_REG_FAN_MODE[] = {
277 0x102, 0x202, 0x302, 0x802, 0x902, 0xa02 };
278 static const u16 NCT6775_REG_FAN_STEP_DOWN_TIME[] = {
279 0x103, 0x203, 0x303, 0x803, 0x903, 0xa03 };
280 static const u16 NCT6775_REG_FAN_STEP_UP_TIME[] = {
281 0x104, 0x204, 0x304, 0x804, 0x904, 0xa04 };
282 static const u16 NCT6775_REG_FAN_STOP_OUTPUT[] = {
283 0x105, 0x205, 0x305, 0x805, 0x905, 0xa05 };
284 static const u16 NCT6775_REG_FAN_START_OUTPUT[] = {
285 0x106, 0x206, 0x306, 0x806, 0x906, 0xa06 };
286 static const u16 NCT6775_REG_FAN_MAX_OUTPUT[] = { 0x10a, 0x20a, 0x30a };
287 static const u16 NCT6775_REG_FAN_STEP_OUTPUT[] = { 0x10b, 0x20b, 0x30b };
289 static const u16 NCT6775_REG_FAN_STOP_TIME[] = {
290 0x107, 0x207, 0x307, 0x807, 0x907, 0xa07 };
291 static const u16 NCT6775_REG_PWM[] = {
292 0x109, 0x209, 0x309, 0x809, 0x909, 0xa09 };
293 static const u16 NCT6775_REG_PWM_READ[] = {
294 0x01, 0x03, 0x11, 0x13, 0x15, 0xa09 };
296 static const u16 NCT6775_REG_FAN[] = { 0x630, 0x632, 0x634, 0x636, 0x638 };
297 static const u16 NCT6775_REG_FAN_MIN[] = { 0x3b, 0x3c, 0x3d };
298 static const u16 NCT6775_REG_FAN_PULSES[] = { 0x641, 0x642, 0x643, 0x644, 0 };
299 static const u16 NCT6775_FAN_PULSE_SHIFT[] = { 0, 0, 0, 0, 0, 0 };
301 static const u16 NCT6775_REG_TEMP[] = {
302 0x27, 0x150, 0x250, 0x62b, 0x62c, 0x62d };
304 static const u16 NCT6775_REG_TEMP_MON[] = { 0x73, 0x75, 0x77 };
306 static const u16 NCT6775_REG_TEMP_CONFIG[ARRAY_SIZE(NCT6775_REG_TEMP)] = {
307 0, 0x152, 0x252, 0x628, 0x629, 0x62A };
308 static const u16 NCT6775_REG_TEMP_HYST[ARRAY_SIZE(NCT6775_REG_TEMP)] = {
309 0x3a, 0x153, 0x253, 0x673, 0x678, 0x67D };
310 static const u16 NCT6775_REG_TEMP_OVER[ARRAY_SIZE(NCT6775_REG_TEMP)] = {
311 0x39, 0x155, 0x255, 0x672, 0x677, 0x67C };
313 static const u16 NCT6775_REG_TEMP_SOURCE[ARRAY_SIZE(NCT6775_REG_TEMP)] = {
314 0x621, 0x622, 0x623, 0x624, 0x625, 0x626 };
316 static const u16 NCT6775_REG_TEMP_SEL[] = {
317 0x100, 0x200, 0x300, 0x800, 0x900, 0xa00 };
319 static const u16 NCT6775_REG_WEIGHT_TEMP_SEL[] = {
320 0x139, 0x239, 0x339, 0x839, 0x939, 0xa39 };
321 static const u16 NCT6775_REG_WEIGHT_TEMP_STEP[] = {
322 0x13a, 0x23a, 0x33a, 0x83a, 0x93a, 0xa3a };
323 static const u16 NCT6775_REG_WEIGHT_TEMP_STEP_TOL[] = {
324 0x13b, 0x23b, 0x33b, 0x83b, 0x93b, 0xa3b };
325 static const u16 NCT6775_REG_WEIGHT_DUTY_STEP[] = {
326 0x13c, 0x23c, 0x33c, 0x83c, 0x93c, 0xa3c };
327 static const u16 NCT6775_REG_WEIGHT_TEMP_BASE[] = {
328 0x13d, 0x23d, 0x33d, 0x83d, 0x93d, 0xa3d };
330 static const u16 NCT6775_REG_TEMP_OFFSET[] = { 0x454, 0x455, 0x456 };
332 static const u16 NCT6775_REG_AUTO_TEMP[] = {
333 0x121, 0x221, 0x321, 0x821, 0x921, 0xa21 };
334 static const u16 NCT6775_REG_AUTO_PWM[] = {
335 0x127, 0x227, 0x327, 0x827, 0x927, 0xa27 };
337 #define NCT6775_AUTO_TEMP(data, nr, p) ((data)->REG_AUTO_TEMP[nr] + (p))
338 #define NCT6775_AUTO_PWM(data, nr, p) ((data)->REG_AUTO_PWM[nr] + (p))
340 static const u16 NCT6775_REG_CRITICAL_ENAB[] = { 0x134, 0x234, 0x334 };
342 static const u16 NCT6775_REG_CRITICAL_TEMP[] = {
343 0x135, 0x235, 0x335, 0x835, 0x935, 0xa35 };
344 static const u16 NCT6775_REG_CRITICAL_TEMP_TOLERANCE[] = {
345 0x138, 0x238, 0x338, 0x838, 0x938, 0xa38 };
347 static const char *const nct6775_temp_label[] = {
361 "PCH_CHIP_CPU_MAX_TEMP",
371 #define NCT6775_TEMP_MASK 0x001ffffe
373 static const u16 NCT6775_REG_TEMP_ALTERNATE[32] = {
379 static const u16 NCT6775_REG_TEMP_CRIT[32] = {
390 /* NCT6776 specific data */
392 /* STEP_UP_TIME and STEP_DOWN_TIME regs are swapped for all chips but NCT6775 */
393 #define NCT6776_REG_FAN_STEP_UP_TIME NCT6775_REG_FAN_STEP_DOWN_TIME
394 #define NCT6776_REG_FAN_STEP_DOWN_TIME NCT6775_REG_FAN_STEP_UP_TIME
396 static const s8 NCT6776_ALARM_BITS[] = {
397 0, 1, 2, 3, 8, 21, 20, 16, /* in0.. in7 */
398 17, -1, -1, -1, -1, -1, -1, /* in8..in14 */
400 6, 7, 11, 10, 23, /* fan1..fan5 */
401 -1, -1, -1, /* unused */
402 4, 5, 13, -1, -1, -1, /* temp1..temp6 */
403 12, 9 }; /* intrusion0, intrusion1 */
405 static const u16 NCT6776_REG_BEEP[NUM_REG_BEEP] = { 0xb2, 0xb3, 0xb4, 0xb5 };
407 static const s8 NCT6776_BEEP_BITS[] = {
408 0, 1, 2, 3, 4, 5, 6, 7, /* in0.. in7 */
409 8, -1, -1, -1, -1, -1, -1, /* in8..in14 */
410 24, /* global beep enable */
411 25, 26, 27, 28, 29, /* fan1..fan5 */
412 -1, -1, -1, /* unused */
413 16, 17, 18, 19, 20, 21, /* temp1..temp6 */
414 30, 31 }; /* intrusion0, intrusion1 */
416 static const u16 NCT6776_REG_TOLERANCE_H[] = {
417 0x10c, 0x20c, 0x30c, 0x80c, 0x90c, 0xa0c };
419 static const u8 NCT6776_REG_PWM_MODE[] = { 0x04, 0, 0, 0, 0, 0 };
420 static const u8 NCT6776_PWM_MODE_MASK[] = { 0x01, 0, 0, 0, 0, 0 };
422 static const u16 NCT6776_REG_FAN_MIN[] = { 0x63a, 0x63c, 0x63e, 0x640, 0x642 };
423 static const u16 NCT6776_REG_FAN_PULSES[] = { 0x644, 0x645, 0x646, 0, 0 };
425 static const u16 NCT6776_REG_WEIGHT_DUTY_BASE[] = {
426 0x13e, 0x23e, 0x33e, 0x83e, 0x93e, 0xa3e };
428 static const u16 NCT6776_REG_TEMP_CONFIG[ARRAY_SIZE(NCT6775_REG_TEMP)] = {
429 0x18, 0x152, 0x252, 0x628, 0x629, 0x62A };
431 static const char *const nct6776_temp_label[] = {
446 "PCH_CHIP_CPU_MAX_TEMP",
457 #define NCT6776_TEMP_MASK 0x007ffffe
459 static const u16 NCT6776_REG_TEMP_ALTERNATE[32] = {
465 static const u16 NCT6776_REG_TEMP_CRIT[32] = {
470 /* NCT6779 specific data */
472 static const u16 NCT6779_REG_IN[] = {
473 0x480, 0x481, 0x482, 0x483, 0x484, 0x485, 0x486, 0x487,
474 0x488, 0x489, 0x48a, 0x48b, 0x48c, 0x48d, 0x48e };
476 static const u16 NCT6779_REG_ALARM[NUM_REG_ALARM] = {
477 0x459, 0x45A, 0x45B, 0x568 };
479 static const s8 NCT6779_ALARM_BITS[] = {
480 0, 1, 2, 3, 8, 21, 20, 16, /* in0.. in7 */
481 17, 24, 25, 26, 27, 28, 29, /* in8..in14 */
483 6, 7, 11, 10, 23, /* fan1..fan5 */
484 -1, -1, -1, /* unused */
485 4, 5, 13, -1, -1, -1, /* temp1..temp6 */
486 12, 9 }; /* intrusion0, intrusion1 */
488 static const s8 NCT6779_BEEP_BITS[] = {
489 0, 1, 2, 3, 4, 5, 6, 7, /* in0.. in7 */
490 8, 9, 10, 11, 12, 13, 14, /* in8..in14 */
491 24, /* global beep enable */
492 25, 26, 27, 28, 29, /* fan1..fan5 */
493 -1, -1, -1, /* unused */
494 16, 17, -1, -1, -1, -1, /* temp1..temp6 */
495 30, 31 }; /* intrusion0, intrusion1 */
497 static const u16 NCT6779_REG_FAN[] = {
498 0x4b0, 0x4b2, 0x4b4, 0x4b6, 0x4b8, 0x4ba };
499 static const u16 NCT6779_REG_FAN_PULSES[] = {
500 0x644, 0x645, 0x646, 0x647, 0x648, 0x649 };
502 static const u16 NCT6779_REG_CRITICAL_PWM_ENABLE[] = {
503 0x136, 0x236, 0x336, 0x836, 0x936, 0xa36 };
504 #define NCT6779_CRITICAL_PWM_ENABLE_MASK 0x01
505 static const u16 NCT6779_REG_CRITICAL_PWM[] = {
506 0x137, 0x237, 0x337, 0x837, 0x937, 0xa37 };
508 static const u16 NCT6779_REG_TEMP[] = { 0x27, 0x150 };
509 static const u16 NCT6779_REG_TEMP_MON[] = { 0x73, 0x75, 0x77, 0x79, 0x7b };
510 static const u16 NCT6779_REG_TEMP_CONFIG[ARRAY_SIZE(NCT6779_REG_TEMP)] = {
512 static const u16 NCT6779_REG_TEMP_HYST[ARRAY_SIZE(NCT6779_REG_TEMP)] = {
514 static const u16 NCT6779_REG_TEMP_OVER[ARRAY_SIZE(NCT6779_REG_TEMP)] = {
517 static const u16 NCT6779_REG_TEMP_OFFSET[] = {
518 0x454, 0x455, 0x456, 0x44a, 0x44b, 0x44c };
520 static const char *const nct6779_temp_label[] = {
539 "PCH_CHIP_CPU_MAX_TEMP",
555 #define NCT6779_TEMP_MASK 0x07ffff7e
556 #define NCT6791_TEMP_MASK 0x87ffff7e
558 static const u16 NCT6779_REG_TEMP_ALTERNATE[32]
559 = { 0x490, 0x491, 0x492, 0x493, 0x494, 0x495, 0, 0,
560 0, 0, 0, 0, 0, 0, 0, 0,
561 0, 0x400, 0x401, 0x402, 0x404, 0x405, 0x406, 0x407,
564 static const u16 NCT6779_REG_TEMP_CRIT[32] = {
569 /* NCT6791 specific data */
571 #define NCT6791_REG_HM_IO_SPACE_LOCK_ENABLE 0x28
573 static const u16 NCT6791_REG_WEIGHT_TEMP_SEL[6] = { 0, 0x239 };
574 static const u16 NCT6791_REG_WEIGHT_TEMP_STEP[6] = { 0, 0x23a };
575 static const u16 NCT6791_REG_WEIGHT_TEMP_STEP_TOL[6] = { 0, 0x23b };
576 static const u16 NCT6791_REG_WEIGHT_DUTY_STEP[6] = { 0, 0x23c };
577 static const u16 NCT6791_REG_WEIGHT_TEMP_BASE[6] = { 0, 0x23d };
578 static const u16 NCT6791_REG_WEIGHT_DUTY_BASE[6] = { 0, 0x23e };
580 static const u16 NCT6791_REG_ALARM[NUM_REG_ALARM] = {
581 0x459, 0x45A, 0x45B, 0x568, 0x45D };
583 static const s8 NCT6791_ALARM_BITS[] = {
584 0, 1, 2, 3, 8, 21, 20, 16, /* in0.. in7 */
585 17, 24, 25, 26, 27, 28, 29, /* in8..in14 */
587 6, 7, 11, 10, 23, 33, /* fan1..fan6 */
589 4, 5, 13, -1, -1, -1, /* temp1..temp6 */
590 12, 9 }; /* intrusion0, intrusion1 */
592 /* NCT6792/NCT6793 specific data */
594 static const u16 NCT6792_REG_TEMP_MON[] = {
595 0x73, 0x75, 0x77, 0x79, 0x7b, 0x7d };
596 static const u16 NCT6792_REG_BEEP[NUM_REG_BEEP] = {
597 0xb2, 0xb3, 0xb4, 0xb5, 0xbf };
599 static const char *const nct6792_temp_label[] = {
618 "PCH_CHIP_CPU_MAX_TEMP",
627 "PECI Agent 0 Calibration",
628 "PECI Agent 1 Calibration",
634 #define NCT6792_TEMP_MASK 0x9fffff7e
636 static const char *const nct6793_temp_label[] = {
655 "PCH_CHIP_CPU_MAX_TEMP",
665 "PECI Agent 0 Calibration",
666 "PECI Agent 1 Calibration",
671 #define NCT6793_TEMP_MASK 0xbfff037e
673 static const char *const nct6795_temp_label[] = {
692 "PCH_CHIP_CPU_MAX_TEMP",
702 "PECI Agent 0 Calibration",
703 "PECI Agent 1 Calibration",
708 #define NCT6795_TEMP_MASK 0xbfffff7e
710 /* NCT6102D/NCT6106D specific data */
712 #define NCT6106_REG_VBAT 0x318
713 #define NCT6106_REG_DIODE 0x319
714 #define NCT6106_DIODE_MASK 0x01
716 static const u16 NCT6106_REG_IN_MAX[] = {
717 0x90, 0x92, 0x94, 0x96, 0x98, 0x9a, 0x9e, 0xa0, 0xa2 };
718 static const u16 NCT6106_REG_IN_MIN[] = {
719 0x91, 0x93, 0x95, 0x97, 0x99, 0x9b, 0x9f, 0xa1, 0xa3 };
720 static const u16 NCT6106_REG_IN[] = {
721 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x07, 0x08, 0x09 };
723 static const u16 NCT6106_REG_TEMP[] = { 0x10, 0x11, 0x12, 0x13, 0x14, 0x15 };
724 static const u16 NCT6106_REG_TEMP_MON[] = { 0x18, 0x19, 0x1a };
725 static const u16 NCT6106_REG_TEMP_HYST[] = {
726 0xc3, 0xc7, 0xcb, 0xcf, 0xd3, 0xd7 };
727 static const u16 NCT6106_REG_TEMP_OVER[] = {
728 0xc2, 0xc6, 0xca, 0xce, 0xd2, 0xd6 };
729 static const u16 NCT6106_REG_TEMP_CRIT_L[] = {
730 0xc0, 0xc4, 0xc8, 0xcc, 0xd0, 0xd4 };
731 static const u16 NCT6106_REG_TEMP_CRIT_H[] = {
732 0xc1, 0xc5, 0xc9, 0xcf, 0xd1, 0xd5 };
733 static const u16 NCT6106_REG_TEMP_OFFSET[] = { 0x311, 0x312, 0x313 };
734 static const u16 NCT6106_REG_TEMP_CONFIG[] = {
735 0xb7, 0xb8, 0xb9, 0xba, 0xbb, 0xbc };
737 static const u16 NCT6106_REG_FAN[] = { 0x20, 0x22, 0x24 };
738 static const u16 NCT6106_REG_FAN_MIN[] = { 0xe0, 0xe2, 0xe4 };
739 static const u16 NCT6106_REG_FAN_PULSES[] = { 0xf6, 0xf6, 0xf6, 0, 0 };
740 static const u16 NCT6106_FAN_PULSE_SHIFT[] = { 0, 2, 4, 0, 0 };
742 static const u8 NCT6106_REG_PWM_MODE[] = { 0xf3, 0xf3, 0xf3 };
743 static const u8 NCT6106_PWM_MODE_MASK[] = { 0x01, 0x02, 0x04 };
744 static const u16 NCT6106_REG_PWM[] = { 0x119, 0x129, 0x139 };
745 static const u16 NCT6106_REG_PWM_READ[] = { 0x4a, 0x4b, 0x4c };
746 static const u16 NCT6106_REG_FAN_MODE[] = { 0x113, 0x123, 0x133 };
747 static const u16 NCT6106_REG_TEMP_SEL[] = { 0x110, 0x120, 0x130 };
748 static const u16 NCT6106_REG_TEMP_SOURCE[] = {
749 0xb0, 0xb1, 0xb2, 0xb3, 0xb4, 0xb5 };
751 static const u16 NCT6106_REG_CRITICAL_TEMP[] = { 0x11a, 0x12a, 0x13a };
752 static const u16 NCT6106_REG_CRITICAL_TEMP_TOLERANCE[] = {
753 0x11b, 0x12b, 0x13b };
755 static const u16 NCT6106_REG_CRITICAL_PWM_ENABLE[] = { 0x11c, 0x12c, 0x13c };
756 #define NCT6106_CRITICAL_PWM_ENABLE_MASK 0x10
757 static const u16 NCT6106_REG_CRITICAL_PWM[] = { 0x11d, 0x12d, 0x13d };
759 static const u16 NCT6106_REG_FAN_STEP_UP_TIME[] = { 0x114, 0x124, 0x134 };
760 static const u16 NCT6106_REG_FAN_STEP_DOWN_TIME[] = { 0x115, 0x125, 0x135 };
761 static const u16 NCT6106_REG_FAN_STOP_OUTPUT[] = { 0x116, 0x126, 0x136 };
762 static const u16 NCT6106_REG_FAN_START_OUTPUT[] = { 0x117, 0x127, 0x137 };
763 static const u16 NCT6106_REG_FAN_STOP_TIME[] = { 0x118, 0x128, 0x138 };
764 static const u16 NCT6106_REG_TOLERANCE_H[] = { 0x112, 0x122, 0x132 };
766 static const u16 NCT6106_REG_TARGET[] = { 0x111, 0x121, 0x131 };
768 static const u16 NCT6106_REG_WEIGHT_TEMP_SEL[] = { 0x168, 0x178, 0x188 };
769 static const u16 NCT6106_REG_WEIGHT_TEMP_STEP[] = { 0x169, 0x179, 0x189 };
770 static const u16 NCT6106_REG_WEIGHT_TEMP_STEP_TOL[] = { 0x16a, 0x17a, 0x18a };
771 static const u16 NCT6106_REG_WEIGHT_DUTY_STEP[] = { 0x16b, 0x17b, 0x17c };
772 static const u16 NCT6106_REG_WEIGHT_TEMP_BASE[] = { 0x16c, 0x17c, 0x18c };
773 static const u16 NCT6106_REG_WEIGHT_DUTY_BASE[] = { 0x16d, 0x17d, 0x18d };
775 static const u16 NCT6106_REG_AUTO_TEMP[] = { 0x160, 0x170, 0x180 };
776 static const u16 NCT6106_REG_AUTO_PWM[] = { 0x164, 0x174, 0x184 };
778 static const u16 NCT6106_REG_ALARM[NUM_REG_ALARM] = {
779 0x77, 0x78, 0x79, 0x7a, 0x7b, 0x7c, 0x7d };
781 static const s8 NCT6106_ALARM_BITS[] = {
782 0, 1, 2, 3, 4, 5, 7, 8, /* in0.. in7 */
783 9, -1, -1, -1, -1, -1, -1, /* in8..in14 */
785 32, 33, 34, -1, -1, /* fan1..fan5 */
786 -1, -1, -1, /* unused */
787 16, 17, 18, 19, 20, 21, /* temp1..temp6 */
788 48, -1 /* intrusion0, intrusion1 */
791 static const u16 NCT6106_REG_BEEP[NUM_REG_BEEP] = {
792 0x3c0, 0x3c1, 0x3c2, 0x3c3, 0x3c4 };
794 static const s8 NCT6106_BEEP_BITS[] = {
795 0, 1, 2, 3, 4, 5, 7, 8, /* in0.. in7 */
796 9, 10, 11, 12, -1, -1, -1, /* in8..in14 */
797 32, /* global beep enable */
798 24, 25, 26, 27, 28, /* fan1..fan5 */
799 -1, -1, -1, /* unused */
800 16, 17, 18, 19, 20, 21, /* temp1..temp6 */
801 34, -1 /* intrusion0, intrusion1 */
804 static const u16 NCT6106_REG_TEMP_ALTERNATE[32] = {
810 static const u16 NCT6106_REG_TEMP_CRIT[32] = {
815 static enum pwm_enable reg_to_pwm_enable(int pwm, int mode)
817 if (mode == 0 && pwm == 255)
822 static int pwm_enable_to_reg(enum pwm_enable mode)
833 /* 1 is DC mode, output in ms */
834 static unsigned int step_time_from_reg(u8 reg, u8 mode)
836 return mode ? 400 * reg : 100 * reg;
839 static u8 step_time_to_reg(unsigned int msec, u8 mode)
841 return clamp_val((mode ? (msec + 200) / 400 :
842 (msec + 50) / 100), 1, 255);
845 static unsigned int fan_from_reg8(u16 reg, unsigned int divreg)
847 if (reg == 0 || reg == 255)
849 return 1350000U / (reg << divreg);
852 static unsigned int fan_from_reg13(u16 reg, unsigned int divreg)
854 if ((reg & 0xff1f) == 0xff1f)
857 reg = (reg & 0x1f) | ((reg & 0xff00) >> 3);
862 return 1350000U / reg;
865 static unsigned int fan_from_reg16(u16 reg, unsigned int divreg)
867 if (reg == 0 || reg == 0xffff)
871 * Even though the registers are 16 bit wide, the fan divisor
874 return 1350000U / (reg << divreg);
877 static u16 fan_to_reg(u32 fan, unsigned int divreg)
882 return (1350000U / fan) >> divreg;
885 static inline unsigned int
892 * Some of the voltage inputs have internal scaling, the tables below
893 * contain 8 (the ADC LSB in mV) * scaling factor * 100
895 static const u16 scale_in[15] = {
896 800, 800, 1600, 1600, 800, 800, 800, 1600, 1600, 800, 800, 800, 800,
900 static inline long in_from_reg(u8 reg, u8 nr)
902 return DIV_ROUND_CLOSEST(reg * scale_in[nr], 100);
905 static inline u8 in_to_reg(u32 val, u8 nr)
907 return clamp_val(DIV_ROUND_CLOSEST(val * 100, scale_in[nr]), 0, 255);
911 * Data structures and manipulation thereof
914 struct nct6775_data {
915 int addr; /* IO base of hw monitor block */
916 int sioreg; /* SIO register address */
920 const struct attribute_group *groups[6];
922 u16 reg_temp[5][NUM_TEMP]; /* 0=temp, 1=temp_over, 2=temp_hyst,
923 * 3=temp_crit, 4=temp_lcrit
925 u8 temp_src[NUM_TEMP];
926 u16 reg_temp_config[NUM_TEMP];
927 const char * const *temp_label;
935 const s8 *ALARM_BITS;
939 const u16 *REG_IN_MINMAX[2];
941 const u16 *REG_TARGET;
943 const u16 *REG_FAN_MODE;
944 const u16 *REG_FAN_MIN;
945 const u16 *REG_FAN_PULSES;
946 const u16 *FAN_PULSE_SHIFT;
947 const u16 *REG_FAN_TIME[3];
949 const u16 *REG_TOLERANCE_H;
951 const u8 *REG_PWM_MODE;
952 const u8 *PWM_MODE_MASK;
954 const u16 *REG_PWM[7]; /* [0]=pwm, [1]=pwm_start, [2]=pwm_floor,
955 * [3]=pwm_max, [4]=pwm_step,
956 * [5]=weight_duty_step, [6]=weight_duty_base
958 const u16 *REG_PWM_READ;
960 const u16 *REG_CRITICAL_PWM_ENABLE;
961 u8 CRITICAL_PWM_ENABLE_MASK;
962 const u16 *REG_CRITICAL_PWM;
964 const u16 *REG_AUTO_TEMP;
965 const u16 *REG_AUTO_PWM;
967 const u16 *REG_CRITICAL_TEMP;
968 const u16 *REG_CRITICAL_TEMP_TOLERANCE;
970 const u16 *REG_TEMP_SOURCE; /* temp register sources */
971 const u16 *REG_TEMP_SEL;
972 const u16 *REG_WEIGHT_TEMP_SEL;
973 const u16 *REG_WEIGHT_TEMP[3]; /* 0=base, 1=tolerance, 2=step */
975 const u16 *REG_TEMP_OFFSET;
977 const u16 *REG_ALARM;
980 unsigned int (*fan_from_reg)(u16 reg, unsigned int divreg);
981 unsigned int (*fan_from_reg_min)(u16 reg, unsigned int divreg);
983 struct mutex update_lock;
984 bool valid; /* true if following fields are valid */
985 unsigned long last_updated; /* In jiffies */
987 /* Register values */
988 u8 bank; /* current register bank */
989 u8 in_num; /* number of in inputs we have */
990 u8 in[15][3]; /* [0]=in, [1]=in_max, [2]=in_min */
991 unsigned int rpm[NUM_FAN];
992 u16 fan_min[NUM_FAN];
993 u8 fan_pulses[NUM_FAN];
996 u8 has_fan; /* some fan inputs can be disabled */
997 u8 has_fan_min; /* some fans don't have min register */
1000 u8 num_temp_alarms; /* 2, 3, or 6 */
1001 u8 num_temp_beeps; /* 2, 3, or 6 */
1002 u8 temp_fixed_num; /* 3 or 6 */
1003 u8 temp_type[NUM_TEMP_FIXED];
1004 s8 temp_offset[NUM_TEMP_FIXED];
1005 s16 temp[5][NUM_TEMP]; /* 0=temp, 1=temp_over, 2=temp_hyst,
1006 * 3=temp_crit, 4=temp_lcrit */
1010 u8 pwm_num; /* number of pwm */
1011 u8 pwm_mode[NUM_FAN]; /* 1->DC variable voltage,
1012 * 0->PWM variable duty cycle
1014 enum pwm_enable pwm_enable[NUM_FAN];
1017 * 2->thermal cruise mode (also called SmartFan I)
1018 * 3->fan speed cruise mode
1020 * 5->enhanced variable thermal cruise (SmartFan IV)
1022 u8 pwm[7][NUM_FAN]; /* [0]=pwm, [1]=pwm_start, [2]=pwm_floor,
1023 * [3]=pwm_max, [4]=pwm_step,
1024 * [5]=weight_duty_step, [6]=weight_duty_base
1027 u8 target_temp[NUM_FAN];
1028 u8 target_temp_mask;
1029 u32 target_speed[NUM_FAN];
1030 u32 target_speed_tolerance[NUM_FAN];
1031 u8 speed_tolerance_limit;
1033 u8 temp_tolerance[2][NUM_FAN];
1036 u8 fan_time[3][NUM_FAN]; /* 0 = stop_time, 1 = step_up, 2 = step_down */
1038 /* Automatic fan speed control registers */
1040 u8 auto_pwm[NUM_FAN][7];
1041 u8 auto_temp[NUM_FAN][7];
1042 u8 pwm_temp_sel[NUM_FAN];
1043 u8 pwm_weight_temp_sel[NUM_FAN];
1044 u8 weight_temp[3][NUM_FAN]; /* 0->temp_step, 1->temp_step_tol,
1054 u16 have_temp_fixed;
1057 /* Remember extra register values over suspend/resume */
1064 struct nct6775_sio_data {
1069 struct sensor_device_template {
1070 struct device_attribute dev_attr;
1078 bool s2; /* true if both index and nr are used */
1081 struct sensor_device_attr_u {
1083 struct sensor_device_attribute a1;
1084 struct sensor_device_attribute_2 a2;
1089 #define __TEMPLATE_ATTR(_template, _mode, _show, _store) { \
1090 .attr = {.name = _template, .mode = _mode }, \
1095 #define SENSOR_DEVICE_TEMPLATE(_template, _mode, _show, _store, _index) \
1096 { .dev_attr = __TEMPLATE_ATTR(_template, _mode, _show, _store), \
1097 .u.index = _index, \
1100 #define SENSOR_DEVICE_TEMPLATE_2(_template, _mode, _show, _store, \
1102 { .dev_attr = __TEMPLATE_ATTR(_template, _mode, _show, _store), \
1103 .u.s.index = _index, \
1107 #define SENSOR_TEMPLATE(_name, _template, _mode, _show, _store, _index) \
1108 static struct sensor_device_template sensor_dev_template_##_name \
1109 = SENSOR_DEVICE_TEMPLATE(_template, _mode, _show, _store, \
1112 #define SENSOR_TEMPLATE_2(_name, _template, _mode, _show, _store, \
1114 static struct sensor_device_template sensor_dev_template_##_name \
1115 = SENSOR_DEVICE_TEMPLATE_2(_template, _mode, _show, _store, \
1118 struct sensor_template_group {
1119 struct sensor_device_template **templates;
1120 umode_t (*is_visible)(struct kobject *, struct attribute *, int);
1124 static struct attribute_group *
1125 nct6775_create_attr_group(struct device *dev,
1126 const struct sensor_template_group *tg,
1129 struct attribute_group *group;
1130 struct sensor_device_attr_u *su;
1131 struct sensor_device_attribute *a;
1132 struct sensor_device_attribute_2 *a2;
1133 struct attribute **attrs;
1134 struct sensor_device_template **t;
1138 return ERR_PTR(-EINVAL);
1141 for (count = 0; *t; t++, count++)
1145 return ERR_PTR(-EINVAL);
1147 group = devm_kzalloc(dev, sizeof(*group), GFP_KERNEL);
1149 return ERR_PTR(-ENOMEM);
1151 attrs = devm_kzalloc(dev, sizeof(*attrs) * (repeat * count + 1),
1154 return ERR_PTR(-ENOMEM);
1156 su = devm_kzalloc(dev, sizeof(*su) * repeat * count,
1159 return ERR_PTR(-ENOMEM);
1161 group->attrs = attrs;
1162 group->is_visible = tg->is_visible;
1164 for (i = 0; i < repeat; i++) {
1166 while (*t != NULL) {
1167 snprintf(su->name, sizeof(su->name),
1168 (*t)->dev_attr.attr.name, tg->base + i);
1171 sysfs_attr_init(&a2->dev_attr.attr);
1172 a2->dev_attr.attr.name = su->name;
1173 a2->nr = (*t)->u.s.nr + i;
1174 a2->index = (*t)->u.s.index;
1175 a2->dev_attr.attr.mode =
1176 (*t)->dev_attr.attr.mode;
1177 a2->dev_attr.show = (*t)->dev_attr.show;
1178 a2->dev_attr.store = (*t)->dev_attr.store;
1179 *attrs = &a2->dev_attr.attr;
1182 sysfs_attr_init(&a->dev_attr.attr);
1183 a->dev_attr.attr.name = su->name;
1184 a->index = (*t)->u.index + i;
1185 a->dev_attr.attr.mode =
1186 (*t)->dev_attr.attr.mode;
1187 a->dev_attr.show = (*t)->dev_attr.show;
1188 a->dev_attr.store = (*t)->dev_attr.store;
1189 *attrs = &a->dev_attr.attr;
1200 static bool is_word_sized(struct nct6775_data *data, u16 reg)
1202 switch (data->kind) {
1204 return reg == 0x20 || reg == 0x22 || reg == 0x24 ||
1205 reg == 0xe0 || reg == 0xe2 || reg == 0xe4 ||
1206 reg == 0x111 || reg == 0x121 || reg == 0x131;
1208 return (((reg & 0xff00) == 0x100 ||
1209 (reg & 0xff00) == 0x200) &&
1210 ((reg & 0x00ff) == 0x50 ||
1211 (reg & 0x00ff) == 0x53 ||
1212 (reg & 0x00ff) == 0x55)) ||
1213 (reg & 0xfff0) == 0x630 ||
1214 reg == 0x640 || reg == 0x642 ||
1216 ((reg & 0xfff0) == 0x650 && (reg & 0x000f) >= 0x06) ||
1217 reg == 0x73 || reg == 0x75 || reg == 0x77;
1219 return (((reg & 0xff00) == 0x100 ||
1220 (reg & 0xff00) == 0x200) &&
1221 ((reg & 0x00ff) == 0x50 ||
1222 (reg & 0x00ff) == 0x53 ||
1223 (reg & 0x00ff) == 0x55)) ||
1224 (reg & 0xfff0) == 0x630 ||
1226 reg == 0x640 || reg == 0x642 ||
1227 ((reg & 0xfff0) == 0x650 && (reg & 0x000f) >= 0x06) ||
1228 reg == 0x73 || reg == 0x75 || reg == 0x77;
1234 return reg == 0x150 || reg == 0x153 || reg == 0x155 ||
1235 ((reg & 0xfff0) == 0x4b0 && (reg & 0x000f) < 0x0b) ||
1237 reg == 0x63a || reg == 0x63c || reg == 0x63e ||
1238 reg == 0x640 || reg == 0x642 ||
1239 reg == 0x73 || reg == 0x75 || reg == 0x77 || reg == 0x79 ||
1240 reg == 0x7b || reg == 0x7d;
1246 * On older chips, only registers 0x50-0x5f are banked.
1247 * On more recent chips, all registers are banked.
1248 * Assume that is the case and set the bank number for each access.
1249 * Cache the bank number so it only needs to be set if it changes.
1251 static inline void nct6775_set_bank(struct nct6775_data *data, u16 reg)
1255 if (data->bank != bank) {
1256 outb_p(NCT6775_REG_BANK, data->addr + ADDR_REG_OFFSET);
1257 outb_p(bank, data->addr + DATA_REG_OFFSET);
1262 static u16 nct6775_read_value(struct nct6775_data *data, u16 reg)
1264 int res, word_sized = is_word_sized(data, reg);
1266 nct6775_set_bank(data, reg);
1267 outb_p(reg & 0xff, data->addr + ADDR_REG_OFFSET);
1268 res = inb_p(data->addr + DATA_REG_OFFSET);
1270 outb_p((reg & 0xff) + 1,
1271 data->addr + ADDR_REG_OFFSET);
1272 res = (res << 8) + inb_p(data->addr + DATA_REG_OFFSET);
1277 static int nct6775_write_value(struct nct6775_data *data, u16 reg, u16 value)
1279 int word_sized = is_word_sized(data, reg);
1281 nct6775_set_bank(data, reg);
1282 outb_p(reg & 0xff, data->addr + ADDR_REG_OFFSET);
1284 outb_p(value >> 8, data->addr + DATA_REG_OFFSET);
1285 outb_p((reg & 0xff) + 1,
1286 data->addr + ADDR_REG_OFFSET);
1288 outb_p(value & 0xff, data->addr + DATA_REG_OFFSET);
1292 /* We left-align 8-bit temperature values to make the code simpler */
1293 static u16 nct6775_read_temp(struct nct6775_data *data, u16 reg)
1297 res = nct6775_read_value(data, reg);
1298 if (!is_word_sized(data, reg))
1304 static int nct6775_write_temp(struct nct6775_data *data, u16 reg, u16 value)
1306 if (!is_word_sized(data, reg))
1308 return nct6775_write_value(data, reg, value);
1311 /* This function assumes that the caller holds data->update_lock */
1312 static void nct6775_write_fan_div(struct nct6775_data *data, int nr)
1318 reg = (nct6775_read_value(data, NCT6775_REG_FANDIV1) & 0x70)
1319 | (data->fan_div[0] & 0x7);
1320 nct6775_write_value(data, NCT6775_REG_FANDIV1, reg);
1323 reg = (nct6775_read_value(data, NCT6775_REG_FANDIV1) & 0x7)
1324 | ((data->fan_div[1] << 4) & 0x70);
1325 nct6775_write_value(data, NCT6775_REG_FANDIV1, reg);
1328 reg = (nct6775_read_value(data, NCT6775_REG_FANDIV2) & 0x70)
1329 | (data->fan_div[2] & 0x7);
1330 nct6775_write_value(data, NCT6775_REG_FANDIV2, reg);
1333 reg = (nct6775_read_value(data, NCT6775_REG_FANDIV2) & 0x7)
1334 | ((data->fan_div[3] << 4) & 0x70);
1335 nct6775_write_value(data, NCT6775_REG_FANDIV2, reg);
1340 static void nct6775_write_fan_div_common(struct nct6775_data *data, int nr)
1342 if (data->kind == nct6775)
1343 nct6775_write_fan_div(data, nr);
1346 static void nct6775_update_fan_div(struct nct6775_data *data)
1350 i = nct6775_read_value(data, NCT6775_REG_FANDIV1);
1351 data->fan_div[0] = i & 0x7;
1352 data->fan_div[1] = (i & 0x70) >> 4;
1353 i = nct6775_read_value(data, NCT6775_REG_FANDIV2);
1354 data->fan_div[2] = i & 0x7;
1355 if (data->has_fan & BIT(3))
1356 data->fan_div[3] = (i & 0x70) >> 4;
1359 static void nct6775_update_fan_div_common(struct nct6775_data *data)
1361 if (data->kind == nct6775)
1362 nct6775_update_fan_div(data);
1365 static void nct6775_init_fan_div(struct nct6775_data *data)
1369 nct6775_update_fan_div_common(data);
1371 * For all fans, start with highest divider value if the divider
1372 * register is not initialized. This ensures that we get a
1373 * reading from the fan count register, even if it is not optimal.
1374 * We'll compute a better divider later on.
1376 for (i = 0; i < ARRAY_SIZE(data->fan_div); i++) {
1377 if (!(data->has_fan & BIT(i)))
1379 if (data->fan_div[i] == 0) {
1380 data->fan_div[i] = 7;
1381 nct6775_write_fan_div_common(data, i);
1386 static void nct6775_init_fan_common(struct device *dev,
1387 struct nct6775_data *data)
1392 if (data->has_fan_div)
1393 nct6775_init_fan_div(data);
1396 * If fan_min is not set (0), set it to 0xff to disable it. This
1397 * prevents the unnecessary warning when fanX_min is reported as 0.
1399 for (i = 0; i < ARRAY_SIZE(data->fan_min); i++) {
1400 if (data->has_fan_min & BIT(i)) {
1401 reg = nct6775_read_value(data, data->REG_FAN_MIN[i]);
1403 nct6775_write_value(data, data->REG_FAN_MIN[i],
1404 data->has_fan_div ? 0xff
1410 static void nct6775_select_fan_div(struct device *dev,
1411 struct nct6775_data *data, int nr, u16 reg)
1413 u8 fan_div = data->fan_div[nr];
1416 if (!data->has_fan_div)
1420 * If we failed to measure the fan speed, or the reported value is not
1421 * in the optimal range, and the clock divider can be modified,
1422 * let's try that for next time.
1424 if (reg == 0x00 && fan_div < 0x07)
1426 else if (reg != 0x00 && reg < 0x30 && fan_div > 0)
1429 if (fan_div != data->fan_div[nr]) {
1430 dev_dbg(dev, "Modifying fan%d clock divider from %u to %u\n",
1431 nr + 1, div_from_reg(data->fan_div[nr]),
1432 div_from_reg(fan_div));
1434 /* Preserve min limit if possible */
1435 if (data->has_fan_min & BIT(nr)) {
1436 fan_min = data->fan_min[nr];
1437 if (fan_div > data->fan_div[nr]) {
1438 if (fan_min != 255 && fan_min > 1)
1441 if (fan_min != 255) {
1447 if (fan_min != data->fan_min[nr]) {
1448 data->fan_min[nr] = fan_min;
1449 nct6775_write_value(data, data->REG_FAN_MIN[nr],
1453 data->fan_div[nr] = fan_div;
1454 nct6775_write_fan_div_common(data, nr);
1458 static void nct6775_update_pwm(struct device *dev)
1460 struct nct6775_data *data = dev_get_drvdata(dev);
1462 int fanmodecfg, reg;
1465 for (i = 0; i < data->pwm_num; i++) {
1466 if (!(data->has_pwm & BIT(i)))
1469 duty_is_dc = data->REG_PWM_MODE[i] &&
1470 (nct6775_read_value(data, data->REG_PWM_MODE[i])
1471 & data->PWM_MODE_MASK[i]);
1472 data->pwm_mode[i] = duty_is_dc;
1474 fanmodecfg = nct6775_read_value(data, data->REG_FAN_MODE[i]);
1475 for (j = 0; j < ARRAY_SIZE(data->REG_PWM); j++) {
1476 if (data->REG_PWM[j] && data->REG_PWM[j][i]) {
1478 = nct6775_read_value(data,
1479 data->REG_PWM[j][i]);
1483 data->pwm_enable[i] = reg_to_pwm_enable(data->pwm[0][i],
1484 (fanmodecfg >> 4) & 7);
1486 if (!data->temp_tolerance[0][i] ||
1487 data->pwm_enable[i] != speed_cruise)
1488 data->temp_tolerance[0][i] = fanmodecfg & 0x0f;
1489 if (!data->target_speed_tolerance[i] ||
1490 data->pwm_enable[i] == speed_cruise) {
1491 u8 t = fanmodecfg & 0x0f;
1493 if (data->REG_TOLERANCE_H) {
1494 t |= (nct6775_read_value(data,
1495 data->REG_TOLERANCE_H[i]) & 0x70) >> 1;
1497 data->target_speed_tolerance[i] = t;
1500 data->temp_tolerance[1][i] =
1501 nct6775_read_value(data,
1502 data->REG_CRITICAL_TEMP_TOLERANCE[i]);
1504 reg = nct6775_read_value(data, data->REG_TEMP_SEL[i]);
1505 data->pwm_temp_sel[i] = reg & 0x1f;
1506 /* If fan can stop, report floor as 0 */
1508 data->pwm[2][i] = 0;
1510 if (!data->REG_WEIGHT_TEMP_SEL[i])
1513 reg = nct6775_read_value(data, data->REG_WEIGHT_TEMP_SEL[i]);
1514 data->pwm_weight_temp_sel[i] = reg & 0x1f;
1515 /* If weight is disabled, report weight source as 0 */
1516 if (j == 1 && !(reg & 0x80))
1517 data->pwm_weight_temp_sel[i] = 0;
1519 /* Weight temp data */
1520 for (j = 0; j < ARRAY_SIZE(data->weight_temp); j++) {
1521 data->weight_temp[j][i]
1522 = nct6775_read_value(data,
1523 data->REG_WEIGHT_TEMP[j][i]);
1528 static void nct6775_update_pwm_limits(struct device *dev)
1530 struct nct6775_data *data = dev_get_drvdata(dev);
1535 for (i = 0; i < data->pwm_num; i++) {
1536 if (!(data->has_pwm & BIT(i)))
1539 for (j = 0; j < ARRAY_SIZE(data->fan_time); j++) {
1540 data->fan_time[j][i] =
1541 nct6775_read_value(data, data->REG_FAN_TIME[j][i]);
1544 reg_t = nct6775_read_value(data, data->REG_TARGET[i]);
1545 /* Update only in matching mode or if never updated */
1546 if (!data->target_temp[i] ||
1547 data->pwm_enable[i] == thermal_cruise)
1548 data->target_temp[i] = reg_t & data->target_temp_mask;
1549 if (!data->target_speed[i] ||
1550 data->pwm_enable[i] == speed_cruise) {
1551 if (data->REG_TOLERANCE_H) {
1552 reg_t |= (nct6775_read_value(data,
1553 data->REG_TOLERANCE_H[i]) & 0x0f) << 8;
1555 data->target_speed[i] = reg_t;
1558 for (j = 0; j < data->auto_pwm_num; j++) {
1559 data->auto_pwm[i][j] =
1560 nct6775_read_value(data,
1561 NCT6775_AUTO_PWM(data, i, j));
1562 data->auto_temp[i][j] =
1563 nct6775_read_value(data,
1564 NCT6775_AUTO_TEMP(data, i, j));
1567 /* critical auto_pwm temperature data */
1568 data->auto_temp[i][data->auto_pwm_num] =
1569 nct6775_read_value(data, data->REG_CRITICAL_TEMP[i]);
1571 switch (data->kind) {
1573 reg = nct6775_read_value(data,
1574 NCT6775_REG_CRITICAL_ENAB[i]);
1575 data->auto_pwm[i][data->auto_pwm_num] =
1576 (reg & 0x02) ? 0xff : 0x00;
1579 data->auto_pwm[i][data->auto_pwm_num] = 0xff;
1587 reg = nct6775_read_value(data,
1588 data->REG_CRITICAL_PWM_ENABLE[i]);
1589 if (reg & data->CRITICAL_PWM_ENABLE_MASK)
1590 reg = nct6775_read_value(data,
1591 data->REG_CRITICAL_PWM[i]);
1594 data->auto_pwm[i][data->auto_pwm_num] = reg;
1600 static struct nct6775_data *nct6775_update_device(struct device *dev)
1602 struct nct6775_data *data = dev_get_drvdata(dev);
1605 mutex_lock(&data->update_lock);
1607 if (time_after(jiffies, data->last_updated + HZ + HZ / 2)
1609 /* Fan clock dividers */
1610 nct6775_update_fan_div_common(data);
1612 /* Measured voltages and limits */
1613 for (i = 0; i < data->in_num; i++) {
1614 if (!(data->have_in & BIT(i)))
1617 data->in[i][0] = nct6775_read_value(data,
1619 data->in[i][1] = nct6775_read_value(data,
1620 data->REG_IN_MINMAX[0][i]);
1621 data->in[i][2] = nct6775_read_value(data,
1622 data->REG_IN_MINMAX[1][i]);
1625 /* Measured fan speeds and limits */
1626 for (i = 0; i < ARRAY_SIZE(data->rpm); i++) {
1629 if (!(data->has_fan & BIT(i)))
1632 reg = nct6775_read_value(data, data->REG_FAN[i]);
1633 data->rpm[i] = data->fan_from_reg(reg,
1636 if (data->has_fan_min & BIT(i))
1637 data->fan_min[i] = nct6775_read_value(data,
1638 data->REG_FAN_MIN[i]);
1639 data->fan_pulses[i] =
1640 (nct6775_read_value(data, data->REG_FAN_PULSES[i])
1641 >> data->FAN_PULSE_SHIFT[i]) & 0x03;
1643 nct6775_select_fan_div(dev, data, i, reg);
1646 nct6775_update_pwm(dev);
1647 nct6775_update_pwm_limits(dev);
1649 /* Measured temperatures and limits */
1650 for (i = 0; i < NUM_TEMP; i++) {
1651 if (!(data->have_temp & BIT(i)))
1653 for (j = 0; j < ARRAY_SIZE(data->reg_temp); j++) {
1654 if (data->reg_temp[j][i])
1656 = nct6775_read_temp(data,
1657 data->reg_temp[j][i]);
1659 if (i >= NUM_TEMP_FIXED ||
1660 !(data->have_temp_fixed & BIT(i)))
1662 data->temp_offset[i]
1663 = nct6775_read_value(data, data->REG_TEMP_OFFSET[i]);
1667 for (i = 0; i < NUM_REG_ALARM; i++) {
1670 if (!data->REG_ALARM[i])
1672 alarm = nct6775_read_value(data, data->REG_ALARM[i]);
1673 data->alarms |= ((u64)alarm) << (i << 3);
1677 for (i = 0; i < NUM_REG_BEEP; i++) {
1680 if (!data->REG_BEEP[i])
1682 beep = nct6775_read_value(data, data->REG_BEEP[i]);
1683 data->beeps |= ((u64)beep) << (i << 3);
1686 data->last_updated = jiffies;
1690 mutex_unlock(&data->update_lock);
1695 * Sysfs callback functions
1698 show_in_reg(struct device *dev, struct device_attribute *attr, char *buf)
1700 struct nct6775_data *data = nct6775_update_device(dev);
1701 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
1702 int index = sattr->index;
1705 return sprintf(buf, "%ld\n", in_from_reg(data->in[nr][index], nr));
1709 store_in_reg(struct device *dev, struct device_attribute *attr, const char *buf,
1712 struct nct6775_data *data = dev_get_drvdata(dev);
1713 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
1714 int index = sattr->index;
1719 err = kstrtoul(buf, 10, &val);
1722 mutex_lock(&data->update_lock);
1723 data->in[nr][index] = in_to_reg(val, nr);
1724 nct6775_write_value(data, data->REG_IN_MINMAX[index - 1][nr],
1725 data->in[nr][index]);
1726 mutex_unlock(&data->update_lock);
1731 show_alarm(struct device *dev, struct device_attribute *attr, char *buf)
1733 struct nct6775_data *data = nct6775_update_device(dev);
1734 struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr);
1735 int nr = data->ALARM_BITS[sattr->index];
1737 return sprintf(buf, "%u\n",
1738 (unsigned int)((data->alarms >> nr) & 0x01));
1741 static int find_temp_source(struct nct6775_data *data, int index, int count)
1743 int source = data->temp_src[index];
1746 for (nr = 0; nr < count; nr++) {
1749 src = nct6775_read_value(data,
1750 data->REG_TEMP_SOURCE[nr]) & 0x1f;
1758 show_temp_alarm(struct device *dev, struct device_attribute *attr, char *buf)
1760 struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr);
1761 struct nct6775_data *data = nct6775_update_device(dev);
1762 unsigned int alarm = 0;
1766 * For temperatures, there is no fixed mapping from registers to alarm
1767 * bits. Alarm bits are determined by the temperature source mapping.
1769 nr = find_temp_source(data, sattr->index, data->num_temp_alarms);
1771 int bit = data->ALARM_BITS[nr + TEMP_ALARM_BASE];
1773 alarm = (data->alarms >> bit) & 0x01;
1775 return sprintf(buf, "%u\n", alarm);
1779 show_beep(struct device *dev, struct device_attribute *attr, char *buf)
1781 struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr);
1782 struct nct6775_data *data = nct6775_update_device(dev);
1783 int nr = data->BEEP_BITS[sattr->index];
1785 return sprintf(buf, "%u\n",
1786 (unsigned int)((data->beeps >> nr) & 0x01));
1790 store_beep(struct device *dev, struct device_attribute *attr, const char *buf,
1793 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
1794 struct nct6775_data *data = dev_get_drvdata(dev);
1795 int nr = data->BEEP_BITS[sattr->index];
1796 int regindex = nr >> 3;
1800 err = kstrtoul(buf, 10, &val);
1806 mutex_lock(&data->update_lock);
1808 data->beeps |= (1ULL << nr);
1810 data->beeps &= ~(1ULL << nr);
1811 nct6775_write_value(data, data->REG_BEEP[regindex],
1812 (data->beeps >> (regindex << 3)) & 0xff);
1813 mutex_unlock(&data->update_lock);
1818 show_temp_beep(struct device *dev, struct device_attribute *attr, char *buf)
1820 struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr);
1821 struct nct6775_data *data = nct6775_update_device(dev);
1822 unsigned int beep = 0;
1826 * For temperatures, there is no fixed mapping from registers to beep
1827 * enable bits. Beep enable bits are determined by the temperature
1830 nr = find_temp_source(data, sattr->index, data->num_temp_beeps);
1832 int bit = data->BEEP_BITS[nr + TEMP_ALARM_BASE];
1834 beep = (data->beeps >> bit) & 0x01;
1836 return sprintf(buf, "%u\n", beep);
1840 store_temp_beep(struct device *dev, struct device_attribute *attr,
1841 const char *buf, size_t count)
1843 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
1844 struct nct6775_data *data = dev_get_drvdata(dev);
1845 int nr, bit, regindex;
1849 err = kstrtoul(buf, 10, &val);
1855 nr = find_temp_source(data, sattr->index, data->num_temp_beeps);
1859 bit = data->BEEP_BITS[nr + TEMP_ALARM_BASE];
1860 regindex = bit >> 3;
1862 mutex_lock(&data->update_lock);
1864 data->beeps |= (1ULL << bit);
1866 data->beeps &= ~(1ULL << bit);
1867 nct6775_write_value(data, data->REG_BEEP[regindex],
1868 (data->beeps >> (regindex << 3)) & 0xff);
1869 mutex_unlock(&data->update_lock);
1874 static umode_t nct6775_in_is_visible(struct kobject *kobj,
1875 struct attribute *attr, int index)
1877 struct device *dev = container_of(kobj, struct device, kobj);
1878 struct nct6775_data *data = dev_get_drvdata(dev);
1879 int in = index / 5; /* voltage index */
1881 if (!(data->have_in & BIT(in)))
1887 SENSOR_TEMPLATE_2(in_input, "in%d_input", S_IRUGO, show_in_reg, NULL, 0, 0);
1888 SENSOR_TEMPLATE(in_alarm, "in%d_alarm", S_IRUGO, show_alarm, NULL, 0);
1889 SENSOR_TEMPLATE(in_beep, "in%d_beep", S_IWUSR | S_IRUGO, show_beep, store_beep,
1891 SENSOR_TEMPLATE_2(in_min, "in%d_min", S_IWUSR | S_IRUGO, show_in_reg,
1892 store_in_reg, 0, 1);
1893 SENSOR_TEMPLATE_2(in_max, "in%d_max", S_IWUSR | S_IRUGO, show_in_reg,
1894 store_in_reg, 0, 2);
1897 * nct6775_in_is_visible uses the index into the following array
1898 * to determine if attributes should be created or not.
1899 * Any change in order or content must be matched.
1901 static struct sensor_device_template *nct6775_attributes_in_template[] = {
1902 &sensor_dev_template_in_input,
1903 &sensor_dev_template_in_alarm,
1904 &sensor_dev_template_in_beep,
1905 &sensor_dev_template_in_min,
1906 &sensor_dev_template_in_max,
1910 static const struct sensor_template_group nct6775_in_template_group = {
1911 .templates = nct6775_attributes_in_template,
1912 .is_visible = nct6775_in_is_visible,
1916 show_fan(struct device *dev, struct device_attribute *attr, char *buf)
1918 struct nct6775_data *data = nct6775_update_device(dev);
1919 struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr);
1920 int nr = sattr->index;
1922 return sprintf(buf, "%d\n", data->rpm[nr]);
1926 show_fan_min(struct device *dev, struct device_attribute *attr, char *buf)
1928 struct nct6775_data *data = nct6775_update_device(dev);
1929 struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr);
1930 int nr = sattr->index;
1932 return sprintf(buf, "%d\n",
1933 data->fan_from_reg_min(data->fan_min[nr],
1934 data->fan_div[nr]));
1938 show_fan_div(struct device *dev, struct device_attribute *attr, char *buf)
1940 struct nct6775_data *data = nct6775_update_device(dev);
1941 struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr);
1942 int nr = sattr->index;
1944 return sprintf(buf, "%u\n", div_from_reg(data->fan_div[nr]));
1948 store_fan_min(struct device *dev, struct device_attribute *attr,
1949 const char *buf, size_t count)
1951 struct nct6775_data *data = dev_get_drvdata(dev);
1952 struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr);
1953 int nr = sattr->index;
1959 err = kstrtoul(buf, 10, &val);
1963 mutex_lock(&data->update_lock);
1964 if (!data->has_fan_div) {
1965 /* NCT6776F or NCT6779D; we know this is a 13 bit register */
1971 val = 1350000U / val;
1972 val = (val & 0x1f) | ((val << 3) & 0xff00);
1974 data->fan_min[nr] = val;
1975 goto write_min; /* Leave fan divider alone */
1978 /* No min limit, alarm disabled */
1979 data->fan_min[nr] = 255;
1980 new_div = data->fan_div[nr]; /* No change */
1981 dev_info(dev, "fan%u low limit and alarm disabled\n", nr + 1);
1984 reg = 1350000U / val;
1985 if (reg >= 128 * 255) {
1987 * Speed below this value cannot possibly be represented,
1988 * even with the highest divider (128)
1990 data->fan_min[nr] = 254;
1991 new_div = 7; /* 128 == BIT(7) */
1993 "fan%u low limit %lu below minimum %u, set to minimum\n",
1994 nr + 1, val, data->fan_from_reg_min(254, 7));
1997 * Speed above this value cannot possibly be represented,
1998 * even with the lowest divider (1)
2000 data->fan_min[nr] = 1;
2001 new_div = 0; /* 1 == BIT(0) */
2003 "fan%u low limit %lu above maximum %u, set to maximum\n",
2004 nr + 1, val, data->fan_from_reg_min(1, 0));
2007 * Automatically pick the best divider, i.e. the one such
2008 * that the min limit will correspond to a register value
2009 * in the 96..192 range
2012 while (reg > 192 && new_div < 7) {
2016 data->fan_min[nr] = reg;
2021 * Write both the fan clock divider (if it changed) and the new
2022 * fan min (unconditionally)
2024 if (new_div != data->fan_div[nr]) {
2025 dev_dbg(dev, "fan%u clock divider changed from %u to %u\n",
2026 nr + 1, div_from_reg(data->fan_div[nr]),
2027 div_from_reg(new_div));
2028 data->fan_div[nr] = new_div;
2029 nct6775_write_fan_div_common(data, nr);
2030 /* Give the chip time to sample a new speed value */
2031 data->last_updated = jiffies;
2035 nct6775_write_value(data, data->REG_FAN_MIN[nr], data->fan_min[nr]);
2036 mutex_unlock(&data->update_lock);
2042 show_fan_pulses(struct device *dev, struct device_attribute *attr, char *buf)
2044 struct nct6775_data *data = nct6775_update_device(dev);
2045 struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr);
2046 int p = data->fan_pulses[sattr->index];
2048 return sprintf(buf, "%d\n", p ? : 4);
2052 store_fan_pulses(struct device *dev, struct device_attribute *attr,
2053 const char *buf, size_t count)
2055 struct nct6775_data *data = dev_get_drvdata(dev);
2056 struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr);
2057 int nr = sattr->index;
2062 err = kstrtoul(buf, 10, &val);
2069 mutex_lock(&data->update_lock);
2070 data->fan_pulses[nr] = val & 3;
2071 reg = nct6775_read_value(data, data->REG_FAN_PULSES[nr]);
2072 reg &= ~(0x03 << data->FAN_PULSE_SHIFT[nr]);
2073 reg |= (val & 3) << data->FAN_PULSE_SHIFT[nr];
2074 nct6775_write_value(data, data->REG_FAN_PULSES[nr], reg);
2075 mutex_unlock(&data->update_lock);
2080 static umode_t nct6775_fan_is_visible(struct kobject *kobj,
2081 struct attribute *attr, int index)
2083 struct device *dev = container_of(kobj, struct device, kobj);
2084 struct nct6775_data *data = dev_get_drvdata(dev);
2085 int fan = index / 6; /* fan index */
2086 int nr = index % 6; /* attribute index */
2088 if (!(data->has_fan & BIT(fan)))
2091 if (nr == 1 && data->ALARM_BITS[FAN_ALARM_BASE + fan] == -1)
2093 if (nr == 2 && data->BEEP_BITS[FAN_ALARM_BASE + fan] == -1)
2095 if (nr == 4 && !(data->has_fan_min & BIT(fan)))
2097 if (nr == 5 && data->kind != nct6775)
2103 SENSOR_TEMPLATE(fan_input, "fan%d_input", S_IRUGO, show_fan, NULL, 0);
2104 SENSOR_TEMPLATE(fan_alarm, "fan%d_alarm", S_IRUGO, show_alarm, NULL,
2106 SENSOR_TEMPLATE(fan_beep, "fan%d_beep", S_IWUSR | S_IRUGO, show_beep,
2107 store_beep, FAN_ALARM_BASE);
2108 SENSOR_TEMPLATE(fan_pulses, "fan%d_pulses", S_IWUSR | S_IRUGO, show_fan_pulses,
2109 store_fan_pulses, 0);
2110 SENSOR_TEMPLATE(fan_min, "fan%d_min", S_IWUSR | S_IRUGO, show_fan_min,
2112 SENSOR_TEMPLATE(fan_div, "fan%d_div", S_IRUGO, show_fan_div, NULL, 0);
2115 * nct6775_fan_is_visible uses the index into the following array
2116 * to determine if attributes should be created or not.
2117 * Any change in order or content must be matched.
2119 static struct sensor_device_template *nct6775_attributes_fan_template[] = {
2120 &sensor_dev_template_fan_input,
2121 &sensor_dev_template_fan_alarm, /* 1 */
2122 &sensor_dev_template_fan_beep, /* 2 */
2123 &sensor_dev_template_fan_pulses,
2124 &sensor_dev_template_fan_min, /* 4 */
2125 &sensor_dev_template_fan_div, /* 5 */
2129 static const struct sensor_template_group nct6775_fan_template_group = {
2130 .templates = nct6775_attributes_fan_template,
2131 .is_visible = nct6775_fan_is_visible,
2136 show_temp_label(struct device *dev, struct device_attribute *attr, char *buf)
2138 struct nct6775_data *data = nct6775_update_device(dev);
2139 struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr);
2140 int nr = sattr->index;
2142 return sprintf(buf, "%s\n", data->temp_label[data->temp_src[nr]]);
2146 show_temp(struct device *dev, struct device_attribute *attr, char *buf)
2148 struct nct6775_data *data = nct6775_update_device(dev);
2149 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
2151 int index = sattr->index;
2153 return sprintf(buf, "%d\n", LM75_TEMP_FROM_REG(data->temp[index][nr]));
2157 store_temp(struct device *dev, struct device_attribute *attr, const char *buf,
2160 struct nct6775_data *data = dev_get_drvdata(dev);
2161 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
2163 int index = sattr->index;
2167 err = kstrtol(buf, 10, &val);
2171 mutex_lock(&data->update_lock);
2172 data->temp[index][nr] = LM75_TEMP_TO_REG(val);
2173 nct6775_write_temp(data, data->reg_temp[index][nr],
2174 data->temp[index][nr]);
2175 mutex_unlock(&data->update_lock);
2180 show_temp_offset(struct device *dev, struct device_attribute *attr, char *buf)
2182 struct nct6775_data *data = nct6775_update_device(dev);
2183 struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr);
2185 return sprintf(buf, "%d\n", data->temp_offset[sattr->index] * 1000);
2189 store_temp_offset(struct device *dev, struct device_attribute *attr,
2190 const char *buf, size_t count)
2192 struct nct6775_data *data = dev_get_drvdata(dev);
2193 struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr);
2194 int nr = sattr->index;
2198 err = kstrtol(buf, 10, &val);
2202 val = clamp_val(DIV_ROUND_CLOSEST(val, 1000), -128, 127);
2204 mutex_lock(&data->update_lock);
2205 data->temp_offset[nr] = val;
2206 nct6775_write_value(data, data->REG_TEMP_OFFSET[nr], val);
2207 mutex_unlock(&data->update_lock);
2213 show_temp_type(struct device *dev, struct device_attribute *attr, char *buf)
2215 struct nct6775_data *data = nct6775_update_device(dev);
2216 struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr);
2217 int nr = sattr->index;
2219 return sprintf(buf, "%d\n", (int)data->temp_type[nr]);
2223 store_temp_type(struct device *dev, struct device_attribute *attr,
2224 const char *buf, size_t count)
2226 struct nct6775_data *data = nct6775_update_device(dev);
2227 struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr);
2228 int nr = sattr->index;
2231 u8 vbat, diode, vbit, dbit;
2233 err = kstrtoul(buf, 10, &val);
2237 if (val != 1 && val != 3 && val != 4)
2240 mutex_lock(&data->update_lock);
2242 data->temp_type[nr] = val;
2244 dbit = data->DIODE_MASK << nr;
2245 vbat = nct6775_read_value(data, data->REG_VBAT) & ~vbit;
2246 diode = nct6775_read_value(data, data->REG_DIODE) & ~dbit;
2248 case 1: /* CPU diode (diode, current mode) */
2252 case 3: /* diode, voltage mode */
2255 case 4: /* thermistor */
2258 nct6775_write_value(data, data->REG_VBAT, vbat);
2259 nct6775_write_value(data, data->REG_DIODE, diode);
2261 mutex_unlock(&data->update_lock);
2265 static umode_t nct6775_temp_is_visible(struct kobject *kobj,
2266 struct attribute *attr, int index)
2268 struct device *dev = container_of(kobj, struct device, kobj);
2269 struct nct6775_data *data = dev_get_drvdata(dev);
2270 int temp = index / 10; /* temp index */
2271 int nr = index % 10; /* attribute index */
2273 if (!(data->have_temp & BIT(temp)))
2276 if (nr == 1 && !data->temp_label)
2279 if (nr == 2 && find_temp_source(data, temp, data->num_temp_alarms) < 0)
2280 return 0; /* alarm */
2282 if (nr == 3 && find_temp_source(data, temp, data->num_temp_beeps) < 0)
2283 return 0; /* beep */
2285 if (nr == 4 && !data->reg_temp[1][temp]) /* max */
2288 if (nr == 5 && !data->reg_temp[2][temp]) /* max_hyst */
2291 if (nr == 6 && !data->reg_temp[3][temp]) /* crit */
2294 if (nr == 7 && !data->reg_temp[4][temp]) /* lcrit */
2297 /* offset and type only apply to fixed sensors */
2298 if (nr > 7 && !(data->have_temp_fixed & BIT(temp)))
2304 SENSOR_TEMPLATE_2(temp_input, "temp%d_input", S_IRUGO, show_temp, NULL, 0, 0);
2305 SENSOR_TEMPLATE(temp_label, "temp%d_label", S_IRUGO, show_temp_label, NULL, 0);
2306 SENSOR_TEMPLATE_2(temp_max, "temp%d_max", S_IRUGO | S_IWUSR, show_temp,
2308 SENSOR_TEMPLATE_2(temp_max_hyst, "temp%d_max_hyst", S_IRUGO | S_IWUSR,
2309 show_temp, store_temp, 0, 2);
2310 SENSOR_TEMPLATE_2(temp_crit, "temp%d_crit", S_IRUGO | S_IWUSR, show_temp,
2312 SENSOR_TEMPLATE_2(temp_lcrit, "temp%d_lcrit", S_IRUGO | S_IWUSR, show_temp,
2314 SENSOR_TEMPLATE(temp_offset, "temp%d_offset", S_IRUGO | S_IWUSR,
2315 show_temp_offset, store_temp_offset, 0);
2316 SENSOR_TEMPLATE(temp_type, "temp%d_type", S_IRUGO | S_IWUSR, show_temp_type,
2317 store_temp_type, 0);
2318 SENSOR_TEMPLATE(temp_alarm, "temp%d_alarm", S_IRUGO, show_temp_alarm, NULL, 0);
2319 SENSOR_TEMPLATE(temp_beep, "temp%d_beep", S_IRUGO | S_IWUSR, show_temp_beep,
2320 store_temp_beep, 0);
2323 * nct6775_temp_is_visible uses the index into the following array
2324 * to determine if attributes should be created or not.
2325 * Any change in order or content must be matched.
2327 static struct sensor_device_template *nct6775_attributes_temp_template[] = {
2328 &sensor_dev_template_temp_input,
2329 &sensor_dev_template_temp_label,
2330 &sensor_dev_template_temp_alarm, /* 2 */
2331 &sensor_dev_template_temp_beep, /* 3 */
2332 &sensor_dev_template_temp_max, /* 4 */
2333 &sensor_dev_template_temp_max_hyst, /* 5 */
2334 &sensor_dev_template_temp_crit, /* 6 */
2335 &sensor_dev_template_temp_lcrit, /* 7 */
2336 &sensor_dev_template_temp_offset, /* 8 */
2337 &sensor_dev_template_temp_type, /* 9 */
2341 static const struct sensor_template_group nct6775_temp_template_group = {
2342 .templates = nct6775_attributes_temp_template,
2343 .is_visible = nct6775_temp_is_visible,
2348 show_pwm_mode(struct device *dev, struct device_attribute *attr, char *buf)
2350 struct nct6775_data *data = nct6775_update_device(dev);
2351 struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr);
2353 return sprintf(buf, "%d\n", !data->pwm_mode[sattr->index]);
2357 store_pwm_mode(struct device *dev, struct device_attribute *attr,
2358 const char *buf, size_t count)
2360 struct nct6775_data *data = dev_get_drvdata(dev);
2361 struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr);
2362 int nr = sattr->index;
2367 err = kstrtoul(buf, 10, &val);
2374 /* Setting DC mode is not supported for all chips/channels */
2375 if (data->REG_PWM_MODE[nr] == 0) {
2381 mutex_lock(&data->update_lock);
2382 data->pwm_mode[nr] = val;
2383 reg = nct6775_read_value(data, data->REG_PWM_MODE[nr]);
2384 reg &= ~data->PWM_MODE_MASK[nr];
2386 reg |= data->PWM_MODE_MASK[nr];
2387 nct6775_write_value(data, data->REG_PWM_MODE[nr], reg);
2388 mutex_unlock(&data->update_lock);
2393 show_pwm(struct device *dev, struct device_attribute *attr, char *buf)
2395 struct nct6775_data *data = nct6775_update_device(dev);
2396 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
2398 int index = sattr->index;
2402 * For automatic fan control modes, show current pwm readings.
2403 * Otherwise, show the configured value.
2405 if (index == 0 && data->pwm_enable[nr] > manual)
2406 pwm = nct6775_read_value(data, data->REG_PWM_READ[nr]);
2408 pwm = data->pwm[index][nr];
2410 return sprintf(buf, "%d\n", pwm);
2414 store_pwm(struct device *dev, struct device_attribute *attr, const char *buf,
2417 struct nct6775_data *data = dev_get_drvdata(dev);
2418 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
2420 int index = sattr->index;
2422 int minval[7] = { 0, 1, 1, data->pwm[2][nr], 0, 0, 0 };
2424 = { 255, 255, data->pwm[3][nr] ? : 255, 255, 255, 255, 255 };
2428 err = kstrtoul(buf, 10, &val);
2431 val = clamp_val(val, minval[index], maxval[index]);
2433 mutex_lock(&data->update_lock);
2434 data->pwm[index][nr] = val;
2435 nct6775_write_value(data, data->REG_PWM[index][nr], val);
2436 if (index == 2) { /* floor: disable if val == 0 */
2437 reg = nct6775_read_value(data, data->REG_TEMP_SEL[nr]);
2441 nct6775_write_value(data, data->REG_TEMP_SEL[nr], reg);
2443 mutex_unlock(&data->update_lock);
2447 /* Returns 0 if OK, -EINVAL otherwise */
2448 static int check_trip_points(struct nct6775_data *data, int nr)
2452 for (i = 0; i < data->auto_pwm_num - 1; i++) {
2453 if (data->auto_temp[nr][i] > data->auto_temp[nr][i + 1])
2456 for (i = 0; i < data->auto_pwm_num - 1; i++) {
2457 if (data->auto_pwm[nr][i] > data->auto_pwm[nr][i + 1])
2460 /* validate critical temperature and pwm if enabled (pwm > 0) */
2461 if (data->auto_pwm[nr][data->auto_pwm_num]) {
2462 if (data->auto_temp[nr][data->auto_pwm_num - 1] >
2463 data->auto_temp[nr][data->auto_pwm_num] ||
2464 data->auto_pwm[nr][data->auto_pwm_num - 1] >
2465 data->auto_pwm[nr][data->auto_pwm_num])
2471 static void pwm_update_registers(struct nct6775_data *data, int nr)
2475 switch (data->pwm_enable[nr]) {
2480 reg = nct6775_read_value(data, data->REG_FAN_MODE[nr]);
2481 reg = (reg & ~data->tolerance_mask) |
2482 (data->target_speed_tolerance[nr] & data->tolerance_mask);
2483 nct6775_write_value(data, data->REG_FAN_MODE[nr], reg);
2484 nct6775_write_value(data, data->REG_TARGET[nr],
2485 data->target_speed[nr] & 0xff);
2486 if (data->REG_TOLERANCE_H) {
2487 reg = (data->target_speed[nr] >> 8) & 0x0f;
2488 reg |= (data->target_speed_tolerance[nr] & 0x38) << 1;
2489 nct6775_write_value(data,
2490 data->REG_TOLERANCE_H[nr],
2494 case thermal_cruise:
2495 nct6775_write_value(data, data->REG_TARGET[nr],
2496 data->target_temp[nr]);
2499 reg = nct6775_read_value(data, data->REG_FAN_MODE[nr]);
2500 reg = (reg & ~data->tolerance_mask) |
2501 data->temp_tolerance[0][nr];
2502 nct6775_write_value(data, data->REG_FAN_MODE[nr], reg);
2508 show_pwm_enable(struct device *dev, struct device_attribute *attr, char *buf)
2510 struct nct6775_data *data = nct6775_update_device(dev);
2511 struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr);
2513 return sprintf(buf, "%d\n", data->pwm_enable[sattr->index]);
2517 store_pwm_enable(struct device *dev, struct device_attribute *attr,
2518 const char *buf, size_t count)
2520 struct nct6775_data *data = dev_get_drvdata(dev);
2521 struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr);
2522 int nr = sattr->index;
2527 err = kstrtoul(buf, 10, &val);
2534 if (val == sf3 && data->kind != nct6775)
2537 if (val == sf4 && check_trip_points(data, nr)) {
2538 dev_err(dev, "Inconsistent trip points, not switching to SmartFan IV mode\n");
2539 dev_err(dev, "Adjust trip points and try again\n");
2543 mutex_lock(&data->update_lock);
2544 data->pwm_enable[nr] = val;
2547 * turn off pwm control: select manual mode, set pwm to maximum
2549 data->pwm[0][nr] = 255;
2550 nct6775_write_value(data, data->REG_PWM[0][nr], 255);
2552 pwm_update_registers(data, nr);
2553 reg = nct6775_read_value(data, data->REG_FAN_MODE[nr]);
2555 reg |= pwm_enable_to_reg(val) << 4;
2556 nct6775_write_value(data, data->REG_FAN_MODE[nr], reg);
2557 mutex_unlock(&data->update_lock);
2562 show_pwm_temp_sel_common(struct nct6775_data *data, char *buf, int src)
2566 for (i = 0; i < NUM_TEMP; i++) {
2567 if (!(data->have_temp & BIT(i)))
2569 if (src == data->temp_src[i]) {
2575 return sprintf(buf, "%d\n", sel);
2579 show_pwm_temp_sel(struct device *dev, struct device_attribute *attr, char *buf)
2581 struct nct6775_data *data = nct6775_update_device(dev);
2582 struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr);
2583 int index = sattr->index;
2585 return show_pwm_temp_sel_common(data, buf, data->pwm_temp_sel[index]);
2589 store_pwm_temp_sel(struct device *dev, struct device_attribute *attr,
2590 const char *buf, size_t count)
2592 struct nct6775_data *data = nct6775_update_device(dev);
2593 struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr);
2594 int nr = sattr->index;
2598 err = kstrtoul(buf, 10, &val);
2601 if (val == 0 || val > NUM_TEMP)
2603 if (!(data->have_temp & BIT(val - 1)) || !data->temp_src[val - 1])
2606 mutex_lock(&data->update_lock);
2607 src = data->temp_src[val - 1];
2608 data->pwm_temp_sel[nr] = src;
2609 reg = nct6775_read_value(data, data->REG_TEMP_SEL[nr]);
2612 nct6775_write_value(data, data->REG_TEMP_SEL[nr], reg);
2613 mutex_unlock(&data->update_lock);
2619 show_pwm_weight_temp_sel(struct device *dev, struct device_attribute *attr,
2622 struct nct6775_data *data = nct6775_update_device(dev);
2623 struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr);
2624 int index = sattr->index;
2626 return show_pwm_temp_sel_common(data, buf,
2627 data->pwm_weight_temp_sel[index]);
2631 store_pwm_weight_temp_sel(struct device *dev, struct device_attribute *attr,
2632 const char *buf, size_t count)
2634 struct nct6775_data *data = nct6775_update_device(dev);
2635 struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr);
2636 int nr = sattr->index;
2640 err = kstrtoul(buf, 10, &val);
2645 if (val && (!(data->have_temp & BIT(val - 1)) ||
2646 !data->temp_src[val - 1]))
2649 mutex_lock(&data->update_lock);
2651 src = data->temp_src[val - 1];
2652 data->pwm_weight_temp_sel[nr] = src;
2653 reg = nct6775_read_value(data, data->REG_WEIGHT_TEMP_SEL[nr]);
2655 reg |= (src | 0x80);
2656 nct6775_write_value(data, data->REG_WEIGHT_TEMP_SEL[nr], reg);
2658 data->pwm_weight_temp_sel[nr] = 0;
2659 reg = nct6775_read_value(data, data->REG_WEIGHT_TEMP_SEL[nr]);
2661 nct6775_write_value(data, data->REG_WEIGHT_TEMP_SEL[nr], reg);
2663 mutex_unlock(&data->update_lock);
2669 show_target_temp(struct device *dev, struct device_attribute *attr, char *buf)
2671 struct nct6775_data *data = nct6775_update_device(dev);
2672 struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr);
2674 return sprintf(buf, "%d\n", data->target_temp[sattr->index] * 1000);
2678 store_target_temp(struct device *dev, struct device_attribute *attr,
2679 const char *buf, size_t count)
2681 struct nct6775_data *data = dev_get_drvdata(dev);
2682 struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr);
2683 int nr = sattr->index;
2687 err = kstrtoul(buf, 10, &val);
2691 val = clamp_val(DIV_ROUND_CLOSEST(val, 1000), 0,
2692 data->target_temp_mask);
2694 mutex_lock(&data->update_lock);
2695 data->target_temp[nr] = val;
2696 pwm_update_registers(data, nr);
2697 mutex_unlock(&data->update_lock);
2702 show_target_speed(struct device *dev, struct device_attribute *attr, char *buf)
2704 struct nct6775_data *data = nct6775_update_device(dev);
2705 struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr);
2706 int nr = sattr->index;
2708 return sprintf(buf, "%d\n",
2709 fan_from_reg16(data->target_speed[nr],
2710 data->fan_div[nr]));
2714 store_target_speed(struct device *dev, struct device_attribute *attr,
2715 const char *buf, size_t count)
2717 struct nct6775_data *data = dev_get_drvdata(dev);
2718 struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr);
2719 int nr = sattr->index;
2724 err = kstrtoul(buf, 10, &val);
2728 val = clamp_val(val, 0, 1350000U);
2729 speed = fan_to_reg(val, data->fan_div[nr]);
2731 mutex_lock(&data->update_lock);
2732 data->target_speed[nr] = speed;
2733 pwm_update_registers(data, nr);
2734 mutex_unlock(&data->update_lock);
2739 show_temp_tolerance(struct device *dev, struct device_attribute *attr,
2742 struct nct6775_data *data = nct6775_update_device(dev);
2743 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
2745 int index = sattr->index;
2747 return sprintf(buf, "%d\n", data->temp_tolerance[index][nr] * 1000);
2751 store_temp_tolerance(struct device *dev, struct device_attribute *attr,
2752 const char *buf, size_t count)
2754 struct nct6775_data *data = dev_get_drvdata(dev);
2755 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
2757 int index = sattr->index;
2761 err = kstrtoul(buf, 10, &val);
2765 /* Limit tolerance as needed */
2766 val = clamp_val(DIV_ROUND_CLOSEST(val, 1000), 0, data->tolerance_mask);
2768 mutex_lock(&data->update_lock);
2769 data->temp_tolerance[index][nr] = val;
2771 pwm_update_registers(data, nr);
2773 nct6775_write_value(data,
2774 data->REG_CRITICAL_TEMP_TOLERANCE[nr],
2776 mutex_unlock(&data->update_lock);
2781 * Fan speed tolerance is a tricky beast, since the associated register is
2782 * a tick counter, but the value is reported and configured as rpm.
2783 * Compute resulting low and high rpm values and report the difference.
2786 show_speed_tolerance(struct device *dev, struct device_attribute *attr,
2789 struct nct6775_data *data = nct6775_update_device(dev);
2790 struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr);
2791 int nr = sattr->index;
2792 int low = data->target_speed[nr] - data->target_speed_tolerance[nr];
2793 int high = data->target_speed[nr] + data->target_speed_tolerance[nr];
2803 tolerance = (fan_from_reg16(low, data->fan_div[nr])
2804 - fan_from_reg16(high, data->fan_div[nr])) / 2;
2806 return sprintf(buf, "%d\n", tolerance);
2810 store_speed_tolerance(struct device *dev, struct device_attribute *attr,
2811 const char *buf, size_t count)
2813 struct nct6775_data *data = dev_get_drvdata(dev);
2814 struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr);
2815 int nr = sattr->index;
2820 err = kstrtoul(buf, 10, &val);
2824 high = fan_from_reg16(data->target_speed[nr],
2825 data->fan_div[nr]) + val;
2826 low = fan_from_reg16(data->target_speed[nr],
2827 data->fan_div[nr]) - val;
2833 val = (fan_to_reg(low, data->fan_div[nr]) -
2834 fan_to_reg(high, data->fan_div[nr])) / 2;
2836 /* Limit tolerance as needed */
2837 val = clamp_val(val, 0, data->speed_tolerance_limit);
2839 mutex_lock(&data->update_lock);
2840 data->target_speed_tolerance[nr] = val;
2841 pwm_update_registers(data, nr);
2842 mutex_unlock(&data->update_lock);
2846 SENSOR_TEMPLATE_2(pwm, "pwm%d", S_IWUSR | S_IRUGO, show_pwm, store_pwm, 0, 0);
2847 SENSOR_TEMPLATE(pwm_mode, "pwm%d_mode", S_IWUSR | S_IRUGO, show_pwm_mode,
2849 SENSOR_TEMPLATE(pwm_enable, "pwm%d_enable", S_IWUSR | S_IRUGO, show_pwm_enable,
2850 store_pwm_enable, 0);
2851 SENSOR_TEMPLATE(pwm_temp_sel, "pwm%d_temp_sel", S_IWUSR | S_IRUGO,
2852 show_pwm_temp_sel, store_pwm_temp_sel, 0);
2853 SENSOR_TEMPLATE(pwm_target_temp, "pwm%d_target_temp", S_IWUSR | S_IRUGO,
2854 show_target_temp, store_target_temp, 0);
2855 SENSOR_TEMPLATE(fan_target, "fan%d_target", S_IWUSR | S_IRUGO,
2856 show_target_speed, store_target_speed, 0);
2857 SENSOR_TEMPLATE(fan_tolerance, "fan%d_tolerance", S_IWUSR | S_IRUGO,
2858 show_speed_tolerance, store_speed_tolerance, 0);
2860 /* Smart Fan registers */
2863 show_weight_temp(struct device *dev, struct device_attribute *attr, char *buf)
2865 struct nct6775_data *data = nct6775_update_device(dev);
2866 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
2868 int index = sattr->index;
2870 return sprintf(buf, "%d\n", data->weight_temp[index][nr] * 1000);
2874 store_weight_temp(struct device *dev, struct device_attribute *attr,
2875 const char *buf, size_t count)
2877 struct nct6775_data *data = dev_get_drvdata(dev);
2878 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
2880 int index = sattr->index;
2884 err = kstrtoul(buf, 10, &val);
2888 val = clamp_val(DIV_ROUND_CLOSEST(val, 1000), 0, 255);
2890 mutex_lock(&data->update_lock);
2891 data->weight_temp[index][nr] = val;
2892 nct6775_write_value(data, data->REG_WEIGHT_TEMP[index][nr], val);
2893 mutex_unlock(&data->update_lock);
2897 SENSOR_TEMPLATE(pwm_weight_temp_sel, "pwm%d_weight_temp_sel", S_IWUSR | S_IRUGO,
2898 show_pwm_weight_temp_sel, store_pwm_weight_temp_sel, 0);
2899 SENSOR_TEMPLATE_2(pwm_weight_temp_step, "pwm%d_weight_temp_step",
2900 S_IWUSR | S_IRUGO, show_weight_temp, store_weight_temp, 0, 0);
2901 SENSOR_TEMPLATE_2(pwm_weight_temp_step_tol, "pwm%d_weight_temp_step_tol",
2902 S_IWUSR | S_IRUGO, show_weight_temp, store_weight_temp, 0, 1);
2903 SENSOR_TEMPLATE_2(pwm_weight_temp_step_base, "pwm%d_weight_temp_step_base",
2904 S_IWUSR | S_IRUGO, show_weight_temp, store_weight_temp, 0, 2);
2905 SENSOR_TEMPLATE_2(pwm_weight_duty_step, "pwm%d_weight_duty_step",
2906 S_IWUSR | S_IRUGO, show_pwm, store_pwm, 0, 5);
2907 SENSOR_TEMPLATE_2(pwm_weight_duty_base, "pwm%d_weight_duty_base",
2908 S_IWUSR | S_IRUGO, show_pwm, store_pwm, 0, 6);
2911 show_fan_time(struct device *dev, struct device_attribute *attr, char *buf)
2913 struct nct6775_data *data = nct6775_update_device(dev);
2914 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
2916 int index = sattr->index;
2918 return sprintf(buf, "%d\n",
2919 step_time_from_reg(data->fan_time[index][nr],
2920 data->pwm_mode[nr]));
2924 store_fan_time(struct device *dev, struct device_attribute *attr,
2925 const char *buf, size_t count)
2927 struct nct6775_data *data = dev_get_drvdata(dev);
2928 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
2930 int index = sattr->index;
2934 err = kstrtoul(buf, 10, &val);
2938 val = step_time_to_reg(val, data->pwm_mode[nr]);
2939 mutex_lock(&data->update_lock);
2940 data->fan_time[index][nr] = val;
2941 nct6775_write_value(data, data->REG_FAN_TIME[index][nr], val);
2942 mutex_unlock(&data->update_lock);
2947 show_auto_pwm(struct device *dev, struct device_attribute *attr, char *buf)
2949 struct nct6775_data *data = nct6775_update_device(dev);
2950 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
2952 return sprintf(buf, "%d\n", data->auto_pwm[sattr->nr][sattr->index]);
2956 store_auto_pwm(struct device *dev, struct device_attribute *attr,
2957 const char *buf, size_t count)
2959 struct nct6775_data *data = dev_get_drvdata(dev);
2960 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
2962 int point = sattr->index;
2967 err = kstrtoul(buf, 10, &val);
2973 if (point == data->auto_pwm_num) {
2974 if (data->kind != nct6775 && !val)
2976 if (data->kind != nct6779 && val)
2980 mutex_lock(&data->update_lock);
2981 data->auto_pwm[nr][point] = val;
2982 if (point < data->auto_pwm_num) {
2983 nct6775_write_value(data,
2984 NCT6775_AUTO_PWM(data, nr, point),
2985 data->auto_pwm[nr][point]);
2987 switch (data->kind) {
2989 /* disable if needed (pwm == 0) */
2990 reg = nct6775_read_value(data,
2991 NCT6775_REG_CRITICAL_ENAB[nr]);
2996 nct6775_write_value(data, NCT6775_REG_CRITICAL_ENAB[nr],
3000 break; /* always enabled, nothing to do */
3007 nct6775_write_value(data, data->REG_CRITICAL_PWM[nr],
3009 reg = nct6775_read_value(data,
3010 data->REG_CRITICAL_PWM_ENABLE[nr]);
3012 reg &= ~data->CRITICAL_PWM_ENABLE_MASK;
3014 reg |= data->CRITICAL_PWM_ENABLE_MASK;
3015 nct6775_write_value(data,
3016 data->REG_CRITICAL_PWM_ENABLE[nr],
3021 mutex_unlock(&data->update_lock);
3026 show_auto_temp(struct device *dev, struct device_attribute *attr, char *buf)
3028 struct nct6775_data *data = nct6775_update_device(dev);
3029 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
3031 int point = sattr->index;
3034 * We don't know for sure if the temperature is signed or unsigned.
3035 * Assume it is unsigned.
3037 return sprintf(buf, "%d\n", data->auto_temp[nr][point] * 1000);
3041 store_auto_temp(struct device *dev, struct device_attribute *attr,
3042 const char *buf, size_t count)
3044 struct nct6775_data *data = dev_get_drvdata(dev);
3045 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
3047 int point = sattr->index;
3051 err = kstrtoul(buf, 10, &val);
3057 mutex_lock(&data->update_lock);
3058 data->auto_temp[nr][point] = DIV_ROUND_CLOSEST(val, 1000);
3059 if (point < data->auto_pwm_num) {
3060 nct6775_write_value(data,
3061 NCT6775_AUTO_TEMP(data, nr, point),
3062 data->auto_temp[nr][point]);
3064 nct6775_write_value(data, data->REG_CRITICAL_TEMP[nr],
3065 data->auto_temp[nr][point]);
3067 mutex_unlock(&data->update_lock);
3071 static umode_t nct6775_pwm_is_visible(struct kobject *kobj,
3072 struct attribute *attr, int index)
3074 struct device *dev = container_of(kobj, struct device, kobj);
3075 struct nct6775_data *data = dev_get_drvdata(dev);
3076 int pwm = index / 36; /* pwm index */
3077 int nr = index % 36; /* attribute index */
3079 if (!(data->has_pwm & BIT(pwm)))
3082 if ((nr >= 14 && nr <= 18) || nr == 21) /* weight */
3083 if (!data->REG_WEIGHT_TEMP_SEL[pwm])
3085 if (nr == 19 && data->REG_PWM[3] == NULL) /* pwm_max */
3087 if (nr == 20 && data->REG_PWM[4] == NULL) /* pwm_step */
3089 if (nr == 21 && data->REG_PWM[6] == NULL) /* weight_duty_base */
3092 if (nr >= 22 && nr <= 35) { /* auto point */
3093 int api = (nr - 22) / 2; /* auto point index */
3095 if (api > data->auto_pwm_num)
3101 SENSOR_TEMPLATE_2(pwm_stop_time, "pwm%d_stop_time", S_IWUSR | S_IRUGO,
3102 show_fan_time, store_fan_time, 0, 0);
3103 SENSOR_TEMPLATE_2(pwm_step_up_time, "pwm%d_step_up_time", S_IWUSR | S_IRUGO,
3104 show_fan_time, store_fan_time, 0, 1);
3105 SENSOR_TEMPLATE_2(pwm_step_down_time, "pwm%d_step_down_time", S_IWUSR | S_IRUGO,
3106 show_fan_time, store_fan_time, 0, 2);
3107 SENSOR_TEMPLATE_2(pwm_start, "pwm%d_start", S_IWUSR | S_IRUGO, show_pwm,
3109 SENSOR_TEMPLATE_2(pwm_floor, "pwm%d_floor", S_IWUSR | S_IRUGO, show_pwm,
3111 SENSOR_TEMPLATE_2(pwm_temp_tolerance, "pwm%d_temp_tolerance", S_IWUSR | S_IRUGO,
3112 show_temp_tolerance, store_temp_tolerance, 0, 0);
3113 SENSOR_TEMPLATE_2(pwm_crit_temp_tolerance, "pwm%d_crit_temp_tolerance",
3114 S_IWUSR | S_IRUGO, show_temp_tolerance, store_temp_tolerance,
3117 SENSOR_TEMPLATE_2(pwm_max, "pwm%d_max", S_IWUSR | S_IRUGO, show_pwm, store_pwm,
3120 SENSOR_TEMPLATE_2(pwm_step, "pwm%d_step", S_IWUSR | S_IRUGO, show_pwm,
3123 SENSOR_TEMPLATE_2(pwm_auto_point1_pwm, "pwm%d_auto_point1_pwm",
3124 S_IWUSR | S_IRUGO, show_auto_pwm, store_auto_pwm, 0, 0);
3125 SENSOR_TEMPLATE_2(pwm_auto_point1_temp, "pwm%d_auto_point1_temp",
3126 S_IWUSR | S_IRUGO, show_auto_temp, store_auto_temp, 0, 0);
3128 SENSOR_TEMPLATE_2(pwm_auto_point2_pwm, "pwm%d_auto_point2_pwm",
3129 S_IWUSR | S_IRUGO, show_auto_pwm, store_auto_pwm, 0, 1);
3130 SENSOR_TEMPLATE_2(pwm_auto_point2_temp, "pwm%d_auto_point2_temp",
3131 S_IWUSR | S_IRUGO, show_auto_temp, store_auto_temp, 0, 1);
3133 SENSOR_TEMPLATE_2(pwm_auto_point3_pwm, "pwm%d_auto_point3_pwm",
3134 S_IWUSR | S_IRUGO, show_auto_pwm, store_auto_pwm, 0, 2);
3135 SENSOR_TEMPLATE_2(pwm_auto_point3_temp, "pwm%d_auto_point3_temp",
3136 S_IWUSR | S_IRUGO, show_auto_temp, store_auto_temp, 0, 2);
3138 SENSOR_TEMPLATE_2(pwm_auto_point4_pwm, "pwm%d_auto_point4_pwm",
3139 S_IWUSR | S_IRUGO, show_auto_pwm, store_auto_pwm, 0, 3);
3140 SENSOR_TEMPLATE_2(pwm_auto_point4_temp, "pwm%d_auto_point4_temp",
3141 S_IWUSR | S_IRUGO, show_auto_temp, store_auto_temp, 0, 3);
3143 SENSOR_TEMPLATE_2(pwm_auto_point5_pwm, "pwm%d_auto_point5_pwm",
3144 S_IWUSR | S_IRUGO, show_auto_pwm, store_auto_pwm, 0, 4);
3145 SENSOR_TEMPLATE_2(pwm_auto_point5_temp, "pwm%d_auto_point5_temp",
3146 S_IWUSR | S_IRUGO, show_auto_temp, store_auto_temp, 0, 4);
3148 SENSOR_TEMPLATE_2(pwm_auto_point6_pwm, "pwm%d_auto_point6_pwm",
3149 S_IWUSR | S_IRUGO, show_auto_pwm, store_auto_pwm, 0, 5);
3150 SENSOR_TEMPLATE_2(pwm_auto_point6_temp, "pwm%d_auto_point6_temp",
3151 S_IWUSR | S_IRUGO, show_auto_temp, store_auto_temp, 0, 5);
3153 SENSOR_TEMPLATE_2(pwm_auto_point7_pwm, "pwm%d_auto_point7_pwm",
3154 S_IWUSR | S_IRUGO, show_auto_pwm, store_auto_pwm, 0, 6);
3155 SENSOR_TEMPLATE_2(pwm_auto_point7_temp, "pwm%d_auto_point7_temp",
3156 S_IWUSR | S_IRUGO, show_auto_temp, store_auto_temp, 0, 6);
3159 * nct6775_pwm_is_visible uses the index into the following array
3160 * to determine if attributes should be created or not.
3161 * Any change in order or content must be matched.
3163 static struct sensor_device_template *nct6775_attributes_pwm_template[] = {
3164 &sensor_dev_template_pwm,
3165 &sensor_dev_template_pwm_mode,
3166 &sensor_dev_template_pwm_enable,
3167 &sensor_dev_template_pwm_temp_sel,
3168 &sensor_dev_template_pwm_temp_tolerance,
3169 &sensor_dev_template_pwm_crit_temp_tolerance,
3170 &sensor_dev_template_pwm_target_temp,
3171 &sensor_dev_template_fan_target,
3172 &sensor_dev_template_fan_tolerance,
3173 &sensor_dev_template_pwm_stop_time,
3174 &sensor_dev_template_pwm_step_up_time,
3175 &sensor_dev_template_pwm_step_down_time,
3176 &sensor_dev_template_pwm_start,
3177 &sensor_dev_template_pwm_floor,
3178 &sensor_dev_template_pwm_weight_temp_sel, /* 14 */
3179 &sensor_dev_template_pwm_weight_temp_step,
3180 &sensor_dev_template_pwm_weight_temp_step_tol,
3181 &sensor_dev_template_pwm_weight_temp_step_base,
3182 &sensor_dev_template_pwm_weight_duty_step, /* 18 */
3183 &sensor_dev_template_pwm_max, /* 19 */
3184 &sensor_dev_template_pwm_step, /* 20 */
3185 &sensor_dev_template_pwm_weight_duty_base, /* 21 */
3186 &sensor_dev_template_pwm_auto_point1_pwm, /* 22 */
3187 &sensor_dev_template_pwm_auto_point1_temp,
3188 &sensor_dev_template_pwm_auto_point2_pwm,
3189 &sensor_dev_template_pwm_auto_point2_temp,
3190 &sensor_dev_template_pwm_auto_point3_pwm,
3191 &sensor_dev_template_pwm_auto_point3_temp,
3192 &sensor_dev_template_pwm_auto_point4_pwm,
3193 &sensor_dev_template_pwm_auto_point4_temp,
3194 &sensor_dev_template_pwm_auto_point5_pwm,
3195 &sensor_dev_template_pwm_auto_point5_temp,
3196 &sensor_dev_template_pwm_auto_point6_pwm,
3197 &sensor_dev_template_pwm_auto_point6_temp,
3198 &sensor_dev_template_pwm_auto_point7_pwm,
3199 &sensor_dev_template_pwm_auto_point7_temp, /* 35 */
3204 static const struct sensor_template_group nct6775_pwm_template_group = {
3205 .templates = nct6775_attributes_pwm_template,
3206 .is_visible = nct6775_pwm_is_visible,
3211 cpu0_vid_show(struct device *dev, struct device_attribute *attr, char *buf)
3213 struct nct6775_data *data = dev_get_drvdata(dev);
3215 return sprintf(buf, "%d\n", vid_from_reg(data->vid, data->vrm));
3218 static DEVICE_ATTR_RO(cpu0_vid);
3220 /* Case open detection */
3223 clear_caseopen(struct device *dev, struct device_attribute *attr,
3224 const char *buf, size_t count)
3226 struct nct6775_data *data = dev_get_drvdata(dev);
3227 int nr = to_sensor_dev_attr(attr)->index - INTRUSION_ALARM_BASE;
3232 if (kstrtoul(buf, 10, &val) || val != 0)
3235 mutex_lock(&data->update_lock);
3238 * Use CR registers to clear caseopen status.
3239 * The CR registers are the same for all chips, and not all chips
3240 * support clearing the caseopen status through "regular" registers.
3242 ret = superio_enter(data->sioreg);
3248 superio_select(data->sioreg, NCT6775_LD_ACPI);
3249 reg = superio_inb(data->sioreg, NCT6775_REG_CR_CASEOPEN_CLR[nr]);
3250 reg |= NCT6775_CR_CASEOPEN_CLR_MASK[nr];
3251 superio_outb(data->sioreg, NCT6775_REG_CR_CASEOPEN_CLR[nr], reg);
3252 reg &= ~NCT6775_CR_CASEOPEN_CLR_MASK[nr];
3253 superio_outb(data->sioreg, NCT6775_REG_CR_CASEOPEN_CLR[nr], reg);
3254 superio_exit(data->sioreg);
3256 data->valid = false; /* Force cache refresh */
3258 mutex_unlock(&data->update_lock);
3262 static SENSOR_DEVICE_ATTR(intrusion0_alarm, S_IWUSR | S_IRUGO, show_alarm,
3263 clear_caseopen, INTRUSION_ALARM_BASE);
3264 static SENSOR_DEVICE_ATTR(intrusion1_alarm, S_IWUSR | S_IRUGO, show_alarm,
3265 clear_caseopen, INTRUSION_ALARM_BASE + 1);
3266 static SENSOR_DEVICE_ATTR(intrusion0_beep, S_IWUSR | S_IRUGO, show_beep,
3267 store_beep, INTRUSION_ALARM_BASE);
3268 static SENSOR_DEVICE_ATTR(intrusion1_beep, S_IWUSR | S_IRUGO, show_beep,
3269 store_beep, INTRUSION_ALARM_BASE + 1);
3270 static SENSOR_DEVICE_ATTR(beep_enable, S_IWUSR | S_IRUGO, show_beep,
3271 store_beep, BEEP_ENABLE_BASE);
3273 static umode_t nct6775_other_is_visible(struct kobject *kobj,
3274 struct attribute *attr, int index)
3276 struct device *dev = container_of(kobj, struct device, kobj);
3277 struct nct6775_data *data = dev_get_drvdata(dev);
3279 if (index == 0 && !data->have_vid)
3282 if (index == 1 || index == 2) {
3283 if (data->ALARM_BITS[INTRUSION_ALARM_BASE + index - 1] < 0)
3287 if (index == 3 || index == 4) {
3288 if (data->BEEP_BITS[INTRUSION_ALARM_BASE + index - 3] < 0)
3296 * nct6775_other_is_visible uses the index into the following array
3297 * to determine if attributes should be created or not.
3298 * Any change in order or content must be matched.
3300 static struct attribute *nct6775_attributes_other[] = {
3301 &dev_attr_cpu0_vid.attr, /* 0 */
3302 &sensor_dev_attr_intrusion0_alarm.dev_attr.attr, /* 1 */
3303 &sensor_dev_attr_intrusion1_alarm.dev_attr.attr, /* 2 */
3304 &sensor_dev_attr_intrusion0_beep.dev_attr.attr, /* 3 */
3305 &sensor_dev_attr_intrusion1_beep.dev_attr.attr, /* 4 */
3306 &sensor_dev_attr_beep_enable.dev_attr.attr, /* 5 */
3311 static const struct attribute_group nct6775_group_other = {
3312 .attrs = nct6775_attributes_other,
3313 .is_visible = nct6775_other_is_visible,
3316 static inline void nct6775_init_device(struct nct6775_data *data)
3321 /* Start monitoring if needed */
3322 if (data->REG_CONFIG) {
3323 tmp = nct6775_read_value(data, data->REG_CONFIG);
3325 nct6775_write_value(data, data->REG_CONFIG, tmp | 0x01);
3328 /* Enable temperature sensors if needed */
3329 for (i = 0; i < NUM_TEMP; i++) {
3330 if (!(data->have_temp & BIT(i)))
3332 if (!data->reg_temp_config[i])
3334 tmp = nct6775_read_value(data, data->reg_temp_config[i]);
3336 nct6775_write_value(data, data->reg_temp_config[i],
3340 /* Enable VBAT monitoring if needed */
3341 tmp = nct6775_read_value(data, data->REG_VBAT);
3343 nct6775_write_value(data, data->REG_VBAT, tmp | 0x01);
3345 diode = nct6775_read_value(data, data->REG_DIODE);
3347 for (i = 0; i < data->temp_fixed_num; i++) {
3348 if (!(data->have_temp_fixed & BIT(i)))
3350 if ((tmp & (data->DIODE_MASK << i))) /* diode */
3352 = 3 - ((diode >> i) & data->DIODE_MASK);
3353 else /* thermistor */
3354 data->temp_type[i] = 4;
3359 nct6775_check_fan_inputs(struct nct6775_data *data)
3361 bool fan3pin, fan4pin, fan4min, fan5pin, fan6pin;
3362 bool pwm3pin, pwm4pin, pwm5pin, pwm6pin;
3363 int sioreg = data->sioreg;
3366 /* Store SIO_REG_ENABLE for use during resume */
3367 superio_select(sioreg, NCT6775_LD_HWM);
3368 data->sio_reg_enable = superio_inb(sioreg, SIO_REG_ENABLE);
3370 /* fan4 and fan5 share some pins with the GPIO and serial flash */
3371 if (data->kind == nct6775) {
3372 regval = superio_inb(sioreg, 0x2c);
3374 fan3pin = regval & BIT(6);
3375 pwm3pin = regval & BIT(7);
3377 /* On NCT6775, fan4 shares pins with the fdc interface */
3378 fan4pin = !(superio_inb(sioreg, 0x2A) & 0x80);
3385 } else if (data->kind == nct6776) {
3386 bool gpok = superio_inb(sioreg, 0x27) & 0x80;
3387 const char *board_vendor, *board_name;
3389 board_vendor = dmi_get_system_info(DMI_BOARD_VENDOR);
3390 board_name = dmi_get_system_info(DMI_BOARD_NAME);
3392 if (board_name && board_vendor &&
3393 !strcmp(board_vendor, "ASRock")) {
3395 * Auxiliary fan monitoring is not enabled on ASRock
3396 * Z77 Pro4-M if booted in UEFI Ultra-FastBoot mode.
3397 * Observed with BIOS version 2.00.
3399 if (!strcmp(board_name, "Z77 Pro4-M")) {
3400 if ((data->sio_reg_enable & 0xe0) != 0xe0) {
3401 data->sio_reg_enable |= 0xe0;
3402 superio_outb(sioreg, SIO_REG_ENABLE,
3403 data->sio_reg_enable);
3408 if (data->sio_reg_enable & 0x80)
3411 fan3pin = !(superio_inb(sioreg, 0x24) & 0x40);
3413 if (data->sio_reg_enable & 0x40)
3416 fan4pin = superio_inb(sioreg, 0x1C) & 0x01;
3418 if (data->sio_reg_enable & 0x20)
3421 fan5pin = superio_inb(sioreg, 0x1C) & 0x02;
3429 } else if (data->kind == nct6106) {
3430 regval = superio_inb(sioreg, 0x24);
3431 fan3pin = !(regval & 0x80);
3432 pwm3pin = regval & 0x08;
3441 } else { /* NCT6779D, NCT6791D, NCT6792D, NCT6793D, or NCT6795D */
3442 int regval_1b, regval_2a, regval_eb;
3444 regval = superio_inb(sioreg, 0x1c);
3446 fan3pin = !(regval & BIT(5));
3447 fan4pin = !(regval & BIT(6));
3448 fan5pin = !(regval & BIT(7));
3450 pwm3pin = !(regval & BIT(0));
3451 pwm4pin = !(regval & BIT(1));
3452 pwm5pin = !(regval & BIT(2));
3454 regval = superio_inb(sioreg, 0x2d);
3455 switch (data->kind) {
3458 fan6pin = regval & BIT(1);
3459 pwm6pin = regval & BIT(0);
3463 regval_1b = superio_inb(sioreg, 0x1b);
3464 regval_2a = superio_inb(sioreg, 0x2a);
3467 pwm5pin = regval & BIT(7);
3468 fan6pin = regval & BIT(1);
3469 pwm6pin = regval & BIT(0);
3471 fan5pin = regval_1b & BIT(5);
3473 superio_select(sioreg, NCT6775_LD_12);
3474 regval_eb = superio_inb(sioreg, 0xeb);
3476 fan5pin = regval_eb & BIT(5);
3478 pwm5pin = (regval_eb & BIT(4)) &&
3479 !(regval_2a & BIT(0));
3481 fan6pin = regval_eb & BIT(3);
3483 pwm6pin = regval_eb & BIT(2);
3485 default: /* NCT6779D */
3494 /* fan 1 and 2 (0x03) are always present */
3495 data->has_fan = 0x03 | (fan3pin << 2) | (fan4pin << 3) |
3496 (fan5pin << 4) | (fan6pin << 5);
3497 data->has_fan_min = 0x03 | (fan3pin << 2) | (fan4min << 3) |
3499 data->has_pwm = 0x03 | (pwm3pin << 2) | (pwm4pin << 3) |
3500 (pwm5pin << 4) | (pwm6pin << 5);
3503 static void add_temp_sensors(struct nct6775_data *data, const u16 *regp,
3504 int *available, int *mask)
3509 for (i = 0; i < data->pwm_num && *available; i++) {
3514 src = nct6775_read_value(data, regp[i]);
3516 if (!src || (*mask & BIT(src)))
3518 if (!(data->temp_mask & BIT(src)))
3521 index = __ffs(*available);
3522 nct6775_write_value(data, data->REG_TEMP_SOURCE[index], src);
3523 *available &= ~BIT(index);
3528 static int nct6775_probe(struct platform_device *pdev)
3530 struct device *dev = &pdev->dev;
3531 struct nct6775_sio_data *sio_data = dev_get_platdata(dev);
3532 struct nct6775_data *data;
3533 struct resource *res;
3535 int src, mask, available;
3536 const u16 *reg_temp, *reg_temp_over, *reg_temp_hyst, *reg_temp_config;
3537 const u16 *reg_temp_mon, *reg_temp_alternate, *reg_temp_crit;
3538 const u16 *reg_temp_crit_l = NULL, *reg_temp_crit_h = NULL;
3539 int num_reg_temp, num_reg_temp_mon;
3541 struct attribute_group *group;
3542 struct device *hwmon_dev;
3543 int num_attr_groups = 0;
3545 res = platform_get_resource(pdev, IORESOURCE_IO, 0);
3546 if (!devm_request_region(&pdev->dev, res->start, IOREGION_LENGTH,
3550 data = devm_kzalloc(&pdev->dev, sizeof(struct nct6775_data),
3555 data->kind = sio_data->kind;
3556 data->sioreg = sio_data->sioreg;
3557 data->addr = res->start;
3558 mutex_init(&data->update_lock);
3559 data->name = nct6775_device_names[data->kind];
3560 data->bank = 0xff; /* Force initial bank selection */
3561 platform_set_drvdata(pdev, data);
3563 switch (data->kind) {
3567 data->auto_pwm_num = 4;
3568 data->temp_fixed_num = 3;
3569 data->num_temp_alarms = 6;
3570 data->num_temp_beeps = 6;
3572 data->fan_from_reg = fan_from_reg13;
3573 data->fan_from_reg_min = fan_from_reg13;
3575 data->temp_label = nct6776_temp_label;
3576 data->temp_mask = NCT6776_TEMP_MASK;
3578 data->REG_VBAT = NCT6106_REG_VBAT;
3579 data->REG_DIODE = NCT6106_REG_DIODE;
3580 data->DIODE_MASK = NCT6106_DIODE_MASK;
3581 data->REG_VIN = NCT6106_REG_IN;
3582 data->REG_IN_MINMAX[0] = NCT6106_REG_IN_MIN;
3583 data->REG_IN_MINMAX[1] = NCT6106_REG_IN_MAX;
3584 data->REG_TARGET = NCT6106_REG_TARGET;
3585 data->REG_FAN = NCT6106_REG_FAN;
3586 data->REG_FAN_MODE = NCT6106_REG_FAN_MODE;
3587 data->REG_FAN_MIN = NCT6106_REG_FAN_MIN;
3588 data->REG_FAN_PULSES = NCT6106_REG_FAN_PULSES;
3589 data->FAN_PULSE_SHIFT = NCT6106_FAN_PULSE_SHIFT;
3590 data->REG_FAN_TIME[0] = NCT6106_REG_FAN_STOP_TIME;
3591 data->REG_FAN_TIME[1] = NCT6106_REG_FAN_STEP_UP_TIME;
3592 data->REG_FAN_TIME[2] = NCT6106_REG_FAN_STEP_DOWN_TIME;
3593 data->REG_PWM[0] = NCT6106_REG_PWM;
3594 data->REG_PWM[1] = NCT6106_REG_FAN_START_OUTPUT;
3595 data->REG_PWM[2] = NCT6106_REG_FAN_STOP_OUTPUT;
3596 data->REG_PWM[5] = NCT6106_REG_WEIGHT_DUTY_STEP;
3597 data->REG_PWM[6] = NCT6106_REG_WEIGHT_DUTY_BASE;
3598 data->REG_PWM_READ = NCT6106_REG_PWM_READ;
3599 data->REG_PWM_MODE = NCT6106_REG_PWM_MODE;
3600 data->PWM_MODE_MASK = NCT6106_PWM_MODE_MASK;
3601 data->REG_AUTO_TEMP = NCT6106_REG_AUTO_TEMP;
3602 data->REG_AUTO_PWM = NCT6106_REG_AUTO_PWM;
3603 data->REG_CRITICAL_TEMP = NCT6106_REG_CRITICAL_TEMP;
3604 data->REG_CRITICAL_TEMP_TOLERANCE
3605 = NCT6106_REG_CRITICAL_TEMP_TOLERANCE;
3606 data->REG_CRITICAL_PWM_ENABLE = NCT6106_REG_CRITICAL_PWM_ENABLE;
3607 data->CRITICAL_PWM_ENABLE_MASK
3608 = NCT6106_CRITICAL_PWM_ENABLE_MASK;
3609 data->REG_CRITICAL_PWM = NCT6106_REG_CRITICAL_PWM;
3610 data->REG_TEMP_OFFSET = NCT6106_REG_TEMP_OFFSET;
3611 data->REG_TEMP_SOURCE = NCT6106_REG_TEMP_SOURCE;
3612 data->REG_TEMP_SEL = NCT6106_REG_TEMP_SEL;
3613 data->REG_WEIGHT_TEMP_SEL = NCT6106_REG_WEIGHT_TEMP_SEL;
3614 data->REG_WEIGHT_TEMP[0] = NCT6106_REG_WEIGHT_TEMP_STEP;
3615 data->REG_WEIGHT_TEMP[1] = NCT6106_REG_WEIGHT_TEMP_STEP_TOL;
3616 data->REG_WEIGHT_TEMP[2] = NCT6106_REG_WEIGHT_TEMP_BASE;
3617 data->REG_ALARM = NCT6106_REG_ALARM;
3618 data->ALARM_BITS = NCT6106_ALARM_BITS;
3619 data->REG_BEEP = NCT6106_REG_BEEP;
3620 data->BEEP_BITS = NCT6106_BEEP_BITS;
3622 reg_temp = NCT6106_REG_TEMP;
3623 reg_temp_mon = NCT6106_REG_TEMP_MON;
3624 num_reg_temp = ARRAY_SIZE(NCT6106_REG_TEMP);
3625 num_reg_temp_mon = ARRAY_SIZE(NCT6106_REG_TEMP_MON);
3626 reg_temp_over = NCT6106_REG_TEMP_OVER;
3627 reg_temp_hyst = NCT6106_REG_TEMP_HYST;
3628 reg_temp_config = NCT6106_REG_TEMP_CONFIG;
3629 reg_temp_alternate = NCT6106_REG_TEMP_ALTERNATE;
3630 reg_temp_crit = NCT6106_REG_TEMP_CRIT;
3631 reg_temp_crit_l = NCT6106_REG_TEMP_CRIT_L;
3632 reg_temp_crit_h = NCT6106_REG_TEMP_CRIT_H;
3638 data->auto_pwm_num = 6;
3639 data->has_fan_div = true;
3640 data->temp_fixed_num = 3;
3641 data->num_temp_alarms = 3;
3642 data->num_temp_beeps = 3;
3644 data->ALARM_BITS = NCT6775_ALARM_BITS;
3645 data->BEEP_BITS = NCT6775_BEEP_BITS;
3647 data->fan_from_reg = fan_from_reg16;
3648 data->fan_from_reg_min = fan_from_reg8;
3649 data->target_temp_mask = 0x7f;
3650 data->tolerance_mask = 0x0f;
3651 data->speed_tolerance_limit = 15;
3653 data->temp_label = nct6775_temp_label;
3654 data->temp_mask = NCT6775_TEMP_MASK;
3656 data->REG_CONFIG = NCT6775_REG_CONFIG;
3657 data->REG_VBAT = NCT6775_REG_VBAT;
3658 data->REG_DIODE = NCT6775_REG_DIODE;
3659 data->DIODE_MASK = NCT6775_DIODE_MASK;
3660 data->REG_VIN = NCT6775_REG_IN;
3661 data->REG_IN_MINMAX[0] = NCT6775_REG_IN_MIN;
3662 data->REG_IN_MINMAX[1] = NCT6775_REG_IN_MAX;
3663 data->REG_TARGET = NCT6775_REG_TARGET;
3664 data->REG_FAN = NCT6775_REG_FAN;
3665 data->REG_FAN_MODE = NCT6775_REG_FAN_MODE;
3666 data->REG_FAN_MIN = NCT6775_REG_FAN_MIN;
3667 data->REG_FAN_PULSES = NCT6775_REG_FAN_PULSES;
3668 data->FAN_PULSE_SHIFT = NCT6775_FAN_PULSE_SHIFT;
3669 data->REG_FAN_TIME[0] = NCT6775_REG_FAN_STOP_TIME;
3670 data->REG_FAN_TIME[1] = NCT6775_REG_FAN_STEP_UP_TIME;
3671 data->REG_FAN_TIME[2] = NCT6775_REG_FAN_STEP_DOWN_TIME;
3672 data->REG_PWM[0] = NCT6775_REG_PWM;
3673 data->REG_PWM[1] = NCT6775_REG_FAN_START_OUTPUT;
3674 data->REG_PWM[2] = NCT6775_REG_FAN_STOP_OUTPUT;
3675 data->REG_PWM[3] = NCT6775_REG_FAN_MAX_OUTPUT;
3676 data->REG_PWM[4] = NCT6775_REG_FAN_STEP_OUTPUT;
3677 data->REG_PWM[5] = NCT6775_REG_WEIGHT_DUTY_STEP;
3678 data->REG_PWM_READ = NCT6775_REG_PWM_READ;
3679 data->REG_PWM_MODE = NCT6775_REG_PWM_MODE;
3680 data->PWM_MODE_MASK = NCT6775_PWM_MODE_MASK;
3681 data->REG_AUTO_TEMP = NCT6775_REG_AUTO_TEMP;
3682 data->REG_AUTO_PWM = NCT6775_REG_AUTO_PWM;
3683 data->REG_CRITICAL_TEMP = NCT6775_REG_CRITICAL_TEMP;
3684 data->REG_CRITICAL_TEMP_TOLERANCE
3685 = NCT6775_REG_CRITICAL_TEMP_TOLERANCE;
3686 data->REG_TEMP_OFFSET = NCT6775_REG_TEMP_OFFSET;
3687 data->REG_TEMP_SOURCE = NCT6775_REG_TEMP_SOURCE;
3688 data->REG_TEMP_SEL = NCT6775_REG_TEMP_SEL;
3689 data->REG_WEIGHT_TEMP_SEL = NCT6775_REG_WEIGHT_TEMP_SEL;
3690 data->REG_WEIGHT_TEMP[0] = NCT6775_REG_WEIGHT_TEMP_STEP;
3691 data->REG_WEIGHT_TEMP[1] = NCT6775_REG_WEIGHT_TEMP_STEP_TOL;
3692 data->REG_WEIGHT_TEMP[2] = NCT6775_REG_WEIGHT_TEMP_BASE;
3693 data->REG_ALARM = NCT6775_REG_ALARM;
3694 data->REG_BEEP = NCT6775_REG_BEEP;
3696 reg_temp = NCT6775_REG_TEMP;
3697 reg_temp_mon = NCT6775_REG_TEMP_MON;
3698 num_reg_temp = ARRAY_SIZE(NCT6775_REG_TEMP);
3699 num_reg_temp_mon = ARRAY_SIZE(NCT6775_REG_TEMP_MON);
3700 reg_temp_over = NCT6775_REG_TEMP_OVER;
3701 reg_temp_hyst = NCT6775_REG_TEMP_HYST;
3702 reg_temp_config = NCT6775_REG_TEMP_CONFIG;
3703 reg_temp_alternate = NCT6775_REG_TEMP_ALTERNATE;
3704 reg_temp_crit = NCT6775_REG_TEMP_CRIT;
3710 data->auto_pwm_num = 4;
3711 data->has_fan_div = false;
3712 data->temp_fixed_num = 3;
3713 data->num_temp_alarms = 3;
3714 data->num_temp_beeps = 6;
3716 data->ALARM_BITS = NCT6776_ALARM_BITS;
3717 data->BEEP_BITS = NCT6776_BEEP_BITS;
3719 data->fan_from_reg = fan_from_reg13;
3720 data->fan_from_reg_min = fan_from_reg13;
3721 data->target_temp_mask = 0xff;
3722 data->tolerance_mask = 0x07;
3723 data->speed_tolerance_limit = 63;
3725 data->temp_label = nct6776_temp_label;
3726 data->temp_mask = NCT6776_TEMP_MASK;
3728 data->REG_CONFIG = NCT6775_REG_CONFIG;
3729 data->REG_VBAT = NCT6775_REG_VBAT;
3730 data->REG_DIODE = NCT6775_REG_DIODE;
3731 data->DIODE_MASK = NCT6775_DIODE_MASK;
3732 data->REG_VIN = NCT6775_REG_IN;
3733 data->REG_IN_MINMAX[0] = NCT6775_REG_IN_MIN;
3734 data->REG_IN_MINMAX[1] = NCT6775_REG_IN_MAX;
3735 data->REG_TARGET = NCT6775_REG_TARGET;
3736 data->REG_FAN = NCT6775_REG_FAN;
3737 data->REG_FAN_MODE = NCT6775_REG_FAN_MODE;
3738 data->REG_FAN_MIN = NCT6776_REG_FAN_MIN;
3739 data->REG_FAN_PULSES = NCT6776_REG_FAN_PULSES;
3740 data->FAN_PULSE_SHIFT = NCT6775_FAN_PULSE_SHIFT;
3741 data->REG_FAN_TIME[0] = NCT6775_REG_FAN_STOP_TIME;
3742 data->REG_FAN_TIME[1] = NCT6776_REG_FAN_STEP_UP_TIME;
3743 data->REG_FAN_TIME[2] = NCT6776_REG_FAN_STEP_DOWN_TIME;
3744 data->REG_TOLERANCE_H = NCT6776_REG_TOLERANCE_H;
3745 data->REG_PWM[0] = NCT6775_REG_PWM;
3746 data->REG_PWM[1] = NCT6775_REG_FAN_START_OUTPUT;
3747 data->REG_PWM[2] = NCT6775_REG_FAN_STOP_OUTPUT;
3748 data->REG_PWM[5] = NCT6775_REG_WEIGHT_DUTY_STEP;
3749 data->REG_PWM[6] = NCT6776_REG_WEIGHT_DUTY_BASE;
3750 data->REG_PWM_READ = NCT6775_REG_PWM_READ;
3751 data->REG_PWM_MODE = NCT6776_REG_PWM_MODE;
3752 data->PWM_MODE_MASK = NCT6776_PWM_MODE_MASK;
3753 data->REG_AUTO_TEMP = NCT6775_REG_AUTO_TEMP;
3754 data->REG_AUTO_PWM = NCT6775_REG_AUTO_PWM;
3755 data->REG_CRITICAL_TEMP = NCT6775_REG_CRITICAL_TEMP;
3756 data->REG_CRITICAL_TEMP_TOLERANCE
3757 = NCT6775_REG_CRITICAL_TEMP_TOLERANCE;
3758 data->REG_TEMP_OFFSET = NCT6775_REG_TEMP_OFFSET;
3759 data->REG_TEMP_SOURCE = NCT6775_REG_TEMP_SOURCE;
3760 data->REG_TEMP_SEL = NCT6775_REG_TEMP_SEL;
3761 data->REG_WEIGHT_TEMP_SEL = NCT6775_REG_WEIGHT_TEMP_SEL;
3762 data->REG_WEIGHT_TEMP[0] = NCT6775_REG_WEIGHT_TEMP_STEP;
3763 data->REG_WEIGHT_TEMP[1] = NCT6775_REG_WEIGHT_TEMP_STEP_TOL;
3764 data->REG_WEIGHT_TEMP[2] = NCT6775_REG_WEIGHT_TEMP_BASE;
3765 data->REG_ALARM = NCT6775_REG_ALARM;
3766 data->REG_BEEP = NCT6776_REG_BEEP;
3768 reg_temp = NCT6775_REG_TEMP;
3769 reg_temp_mon = NCT6775_REG_TEMP_MON;
3770 num_reg_temp = ARRAY_SIZE(NCT6775_REG_TEMP);
3771 num_reg_temp_mon = ARRAY_SIZE(NCT6775_REG_TEMP_MON);
3772 reg_temp_over = NCT6775_REG_TEMP_OVER;
3773 reg_temp_hyst = NCT6775_REG_TEMP_HYST;
3774 reg_temp_config = NCT6776_REG_TEMP_CONFIG;
3775 reg_temp_alternate = NCT6776_REG_TEMP_ALTERNATE;
3776 reg_temp_crit = NCT6776_REG_TEMP_CRIT;
3782 data->auto_pwm_num = 4;
3783 data->has_fan_div = false;
3784 data->temp_fixed_num = 6;
3785 data->num_temp_alarms = 2;
3786 data->num_temp_beeps = 2;
3788 data->ALARM_BITS = NCT6779_ALARM_BITS;
3789 data->BEEP_BITS = NCT6779_BEEP_BITS;
3791 data->fan_from_reg = fan_from_reg13;
3792 data->fan_from_reg_min = fan_from_reg13;
3793 data->target_temp_mask = 0xff;
3794 data->tolerance_mask = 0x07;
3795 data->speed_tolerance_limit = 63;
3797 data->temp_label = nct6779_temp_label;
3798 data->temp_mask = NCT6779_TEMP_MASK;
3800 data->REG_CONFIG = NCT6775_REG_CONFIG;
3801 data->REG_VBAT = NCT6775_REG_VBAT;
3802 data->REG_DIODE = NCT6775_REG_DIODE;
3803 data->DIODE_MASK = NCT6775_DIODE_MASK;
3804 data->REG_VIN = NCT6779_REG_IN;
3805 data->REG_IN_MINMAX[0] = NCT6775_REG_IN_MIN;
3806 data->REG_IN_MINMAX[1] = NCT6775_REG_IN_MAX;
3807 data->REG_TARGET = NCT6775_REG_TARGET;
3808 data->REG_FAN = NCT6779_REG_FAN;
3809 data->REG_FAN_MODE = NCT6775_REG_FAN_MODE;
3810 data->REG_FAN_MIN = NCT6776_REG_FAN_MIN;
3811 data->REG_FAN_PULSES = NCT6779_REG_FAN_PULSES;
3812 data->FAN_PULSE_SHIFT = NCT6775_FAN_PULSE_SHIFT;
3813 data->REG_FAN_TIME[0] = NCT6775_REG_FAN_STOP_TIME;
3814 data->REG_FAN_TIME[1] = NCT6776_REG_FAN_STEP_UP_TIME;
3815 data->REG_FAN_TIME[2] = NCT6776_REG_FAN_STEP_DOWN_TIME;
3816 data->REG_TOLERANCE_H = NCT6776_REG_TOLERANCE_H;
3817 data->REG_PWM[0] = NCT6775_REG_PWM;
3818 data->REG_PWM[1] = NCT6775_REG_FAN_START_OUTPUT;
3819 data->REG_PWM[2] = NCT6775_REG_FAN_STOP_OUTPUT;
3820 data->REG_PWM[5] = NCT6775_REG_WEIGHT_DUTY_STEP;
3821 data->REG_PWM[6] = NCT6776_REG_WEIGHT_DUTY_BASE;
3822 data->REG_PWM_READ = NCT6775_REG_PWM_READ;
3823 data->REG_PWM_MODE = NCT6776_REG_PWM_MODE;
3824 data->PWM_MODE_MASK = NCT6776_PWM_MODE_MASK;
3825 data->REG_AUTO_TEMP = NCT6775_REG_AUTO_TEMP;
3826 data->REG_AUTO_PWM = NCT6775_REG_AUTO_PWM;
3827 data->REG_CRITICAL_TEMP = NCT6775_REG_CRITICAL_TEMP;
3828 data->REG_CRITICAL_TEMP_TOLERANCE
3829 = NCT6775_REG_CRITICAL_TEMP_TOLERANCE;
3830 data->REG_CRITICAL_PWM_ENABLE = NCT6779_REG_CRITICAL_PWM_ENABLE;
3831 data->CRITICAL_PWM_ENABLE_MASK
3832 = NCT6779_CRITICAL_PWM_ENABLE_MASK;
3833 data->REG_CRITICAL_PWM = NCT6779_REG_CRITICAL_PWM;
3834 data->REG_TEMP_OFFSET = NCT6779_REG_TEMP_OFFSET;
3835 data->REG_TEMP_SOURCE = NCT6775_REG_TEMP_SOURCE;
3836 data->REG_TEMP_SEL = NCT6775_REG_TEMP_SEL;
3837 data->REG_WEIGHT_TEMP_SEL = NCT6775_REG_WEIGHT_TEMP_SEL;
3838 data->REG_WEIGHT_TEMP[0] = NCT6775_REG_WEIGHT_TEMP_STEP;
3839 data->REG_WEIGHT_TEMP[1] = NCT6775_REG_WEIGHT_TEMP_STEP_TOL;
3840 data->REG_WEIGHT_TEMP[2] = NCT6775_REG_WEIGHT_TEMP_BASE;
3841 data->REG_ALARM = NCT6779_REG_ALARM;
3842 data->REG_BEEP = NCT6776_REG_BEEP;
3844 reg_temp = NCT6779_REG_TEMP;
3845 reg_temp_mon = NCT6779_REG_TEMP_MON;
3846 num_reg_temp = ARRAY_SIZE(NCT6779_REG_TEMP);
3847 num_reg_temp_mon = ARRAY_SIZE(NCT6779_REG_TEMP_MON);
3848 reg_temp_over = NCT6779_REG_TEMP_OVER;
3849 reg_temp_hyst = NCT6779_REG_TEMP_HYST;
3850 reg_temp_config = NCT6779_REG_TEMP_CONFIG;
3851 reg_temp_alternate = NCT6779_REG_TEMP_ALTERNATE;
3852 reg_temp_crit = NCT6779_REG_TEMP_CRIT;
3861 data->auto_pwm_num = 4;
3862 data->has_fan_div = false;
3863 data->temp_fixed_num = 6;
3864 data->num_temp_alarms = 2;
3865 data->num_temp_beeps = 2;
3867 data->ALARM_BITS = NCT6791_ALARM_BITS;
3868 data->BEEP_BITS = NCT6779_BEEP_BITS;
3870 data->fan_from_reg = fan_from_reg13;
3871 data->fan_from_reg_min = fan_from_reg13;
3872 data->target_temp_mask = 0xff;
3873 data->tolerance_mask = 0x07;
3874 data->speed_tolerance_limit = 63;
3876 switch (data->kind) {
3879 data->temp_label = nct6779_temp_label;
3880 data->temp_mask = NCT6791_TEMP_MASK;
3883 data->temp_label = nct6792_temp_label;
3884 data->temp_mask = NCT6792_TEMP_MASK;
3887 data->temp_label = nct6793_temp_label;
3888 data->temp_mask = NCT6793_TEMP_MASK;
3891 data->temp_label = nct6795_temp_label;
3892 data->temp_mask = NCT6795_TEMP_MASK;
3896 data->REG_CONFIG = NCT6775_REG_CONFIG;
3897 data->REG_VBAT = NCT6775_REG_VBAT;
3898 data->REG_DIODE = NCT6775_REG_DIODE;
3899 data->DIODE_MASK = NCT6775_DIODE_MASK;
3900 data->REG_VIN = NCT6779_REG_IN;
3901 data->REG_IN_MINMAX[0] = NCT6775_REG_IN_MIN;
3902 data->REG_IN_MINMAX[1] = NCT6775_REG_IN_MAX;
3903 data->REG_TARGET = NCT6775_REG_TARGET;
3904 data->REG_FAN = NCT6779_REG_FAN;
3905 data->REG_FAN_MODE = NCT6775_REG_FAN_MODE;
3906 data->REG_FAN_MIN = NCT6776_REG_FAN_MIN;
3907 data->REG_FAN_PULSES = NCT6779_REG_FAN_PULSES;
3908 data->FAN_PULSE_SHIFT = NCT6775_FAN_PULSE_SHIFT;
3909 data->REG_FAN_TIME[0] = NCT6775_REG_FAN_STOP_TIME;
3910 data->REG_FAN_TIME[1] = NCT6776_REG_FAN_STEP_UP_TIME;
3911 data->REG_FAN_TIME[2] = NCT6776_REG_FAN_STEP_DOWN_TIME;
3912 data->REG_TOLERANCE_H = NCT6776_REG_TOLERANCE_H;
3913 data->REG_PWM[0] = NCT6775_REG_PWM;
3914 data->REG_PWM[1] = NCT6775_REG_FAN_START_OUTPUT;
3915 data->REG_PWM[2] = NCT6775_REG_FAN_STOP_OUTPUT;
3916 data->REG_PWM[5] = NCT6791_REG_WEIGHT_DUTY_STEP;
3917 data->REG_PWM[6] = NCT6791_REG_WEIGHT_DUTY_BASE;
3918 data->REG_PWM_READ = NCT6775_REG_PWM_READ;
3919 data->REG_PWM_MODE = NCT6776_REG_PWM_MODE;
3920 data->PWM_MODE_MASK = NCT6776_PWM_MODE_MASK;
3921 data->REG_AUTO_TEMP = NCT6775_REG_AUTO_TEMP;
3922 data->REG_AUTO_PWM = NCT6775_REG_AUTO_PWM;
3923 data->REG_CRITICAL_TEMP = NCT6775_REG_CRITICAL_TEMP;
3924 data->REG_CRITICAL_TEMP_TOLERANCE
3925 = NCT6775_REG_CRITICAL_TEMP_TOLERANCE;
3926 data->REG_CRITICAL_PWM_ENABLE = NCT6779_REG_CRITICAL_PWM_ENABLE;
3927 data->CRITICAL_PWM_ENABLE_MASK
3928 = NCT6779_CRITICAL_PWM_ENABLE_MASK;
3929 data->REG_CRITICAL_PWM = NCT6779_REG_CRITICAL_PWM;
3930 data->REG_TEMP_OFFSET = NCT6779_REG_TEMP_OFFSET;
3931 data->REG_TEMP_SOURCE = NCT6775_REG_TEMP_SOURCE;
3932 data->REG_TEMP_SEL = NCT6775_REG_TEMP_SEL;
3933 data->REG_WEIGHT_TEMP_SEL = NCT6791_REG_WEIGHT_TEMP_SEL;
3934 data->REG_WEIGHT_TEMP[0] = NCT6791_REG_WEIGHT_TEMP_STEP;
3935 data->REG_WEIGHT_TEMP[1] = NCT6791_REG_WEIGHT_TEMP_STEP_TOL;
3936 data->REG_WEIGHT_TEMP[2] = NCT6791_REG_WEIGHT_TEMP_BASE;
3937 data->REG_ALARM = NCT6791_REG_ALARM;
3938 if (data->kind == nct6791)
3939 data->REG_BEEP = NCT6776_REG_BEEP;
3941 data->REG_BEEP = NCT6792_REG_BEEP;
3943 reg_temp = NCT6779_REG_TEMP;
3944 num_reg_temp = ARRAY_SIZE(NCT6779_REG_TEMP);
3945 if (data->kind == nct6791) {
3946 reg_temp_mon = NCT6779_REG_TEMP_MON;
3947 num_reg_temp_mon = ARRAY_SIZE(NCT6779_REG_TEMP_MON);
3949 reg_temp_mon = NCT6792_REG_TEMP_MON;
3950 num_reg_temp_mon = ARRAY_SIZE(NCT6792_REG_TEMP_MON);
3952 reg_temp_over = NCT6779_REG_TEMP_OVER;
3953 reg_temp_hyst = NCT6779_REG_TEMP_HYST;
3954 reg_temp_config = NCT6779_REG_TEMP_CONFIG;
3955 reg_temp_alternate = NCT6779_REG_TEMP_ALTERNATE;
3956 reg_temp_crit = NCT6779_REG_TEMP_CRIT;
3962 data->have_in = BIT(data->in_num) - 1;
3963 data->have_temp = 0;
3966 * On some boards, not all available temperature sources are monitored,
3967 * even though some of the monitoring registers are unused.
3968 * Get list of unused monitoring registers, then detect if any fan
3969 * controls are configured to use unmonitored temperature sources.
3970 * If so, assign the unmonitored temperature sources to available
3971 * monitoring registers.
3975 for (i = 0; i < num_reg_temp; i++) {
3976 if (reg_temp[i] == 0)
3979 src = nct6775_read_value(data, data->REG_TEMP_SOURCE[i]) & 0x1f;
3980 if (!src || (mask & BIT(src)))
3981 available |= BIT(i);
3987 * Now find unmonitored temperature registers and enable monitoring
3988 * if additional monitoring registers are available.
3990 add_temp_sensors(data, data->REG_TEMP_SEL, &available, &mask);
3991 add_temp_sensors(data, data->REG_WEIGHT_TEMP_SEL, &available, &mask);
3994 s = NUM_TEMP_FIXED; /* First dynamic temperature attribute */
3995 for (i = 0; i < num_reg_temp; i++) {
3996 if (reg_temp[i] == 0)
3999 src = nct6775_read_value(data, data->REG_TEMP_SOURCE[i]) & 0x1f;
4000 if (!src || (mask & BIT(src)))
4003 if (!(data->temp_mask & BIT(src))) {
4005 "Invalid temperature source %d at index %d, source register 0x%x, temp register 0x%x\n",
4006 src, i, data->REG_TEMP_SOURCE[i], reg_temp[i]);
4012 /* Use fixed index for SYSTIN(1), CPUTIN(2), AUXTIN(3) */
4013 if (src <= data->temp_fixed_num) {
4014 data->have_temp |= BIT(src - 1);
4015 data->have_temp_fixed |= BIT(src - 1);
4016 data->reg_temp[0][src - 1] = reg_temp[i];
4017 data->reg_temp[1][src - 1] = reg_temp_over[i];
4018 data->reg_temp[2][src - 1] = reg_temp_hyst[i];
4019 if (reg_temp_crit_h && reg_temp_crit_h[i])
4020 data->reg_temp[3][src - 1] = reg_temp_crit_h[i];
4021 else if (reg_temp_crit[src - 1])
4022 data->reg_temp[3][src - 1]
4023 = reg_temp_crit[src - 1];
4024 if (reg_temp_crit_l && reg_temp_crit_l[i])
4025 data->reg_temp[4][src - 1] = reg_temp_crit_l[i];
4026 data->reg_temp_config[src - 1] = reg_temp_config[i];
4027 data->temp_src[src - 1] = src;
4034 /* Use dynamic index for other sources */
4035 data->have_temp |= BIT(s);
4036 data->reg_temp[0][s] = reg_temp[i];
4037 data->reg_temp[1][s] = reg_temp_over[i];
4038 data->reg_temp[2][s] = reg_temp_hyst[i];
4039 data->reg_temp_config[s] = reg_temp_config[i];
4040 if (reg_temp_crit_h && reg_temp_crit_h[i])
4041 data->reg_temp[3][s] = reg_temp_crit_h[i];
4042 else if (reg_temp_crit[src - 1])
4043 data->reg_temp[3][s] = reg_temp_crit[src - 1];
4044 if (reg_temp_crit_l && reg_temp_crit_l[i])
4045 data->reg_temp[4][s] = reg_temp_crit_l[i];
4047 data->temp_src[s] = src;
4052 * Repeat with temperatures used for fan control.
4053 * This set of registers does not support limits.
4055 for (i = 0; i < num_reg_temp_mon; i++) {
4056 if (reg_temp_mon[i] == 0)
4059 src = nct6775_read_value(data, data->REG_TEMP_SEL[i]) & 0x1f;
4063 if (!(data->temp_mask & BIT(src))) {
4065 "Invalid temperature source %d at index %d, source register 0x%x, temp register 0x%x\n",
4066 src, i, data->REG_TEMP_SEL[i],
4072 * For virtual temperature sources, the 'virtual' temperature
4073 * for each fan reflects a different temperature, and there
4074 * are no duplicates.
4076 if (src != TEMP_SOURCE_VIRTUAL) {
4077 if (mask & BIT(src))
4082 /* Use fixed index for SYSTIN(1), CPUTIN(2), AUXTIN(3) */
4083 if (src <= data->temp_fixed_num) {
4084 if (data->have_temp & BIT(src - 1))
4086 data->have_temp |= BIT(src - 1);
4087 data->have_temp_fixed |= BIT(src - 1);
4088 data->reg_temp[0][src - 1] = reg_temp_mon[i];
4089 data->temp_src[src - 1] = src;
4096 /* Use dynamic index for other sources */
4097 data->have_temp |= BIT(s);
4098 data->reg_temp[0][s] = reg_temp_mon[i];
4099 data->temp_src[s] = src;
4103 #ifdef USE_ALTERNATE
4105 * Go through the list of alternate temp registers and enable
4107 * The temperature is already monitored if the respective bit in <mask>
4110 for (i = 0; i < 32; i++) {
4111 if (!(data->temp_mask & BIT(i + 1)))
4113 if (!reg_temp_alternate[i])
4115 if (mask & BIT(i + 1))
4117 if (i < data->temp_fixed_num) {
4118 if (data->have_temp & BIT(i))
4120 data->have_temp |= BIT(i);
4121 data->have_temp_fixed |= BIT(i);
4122 data->reg_temp[0][i] = reg_temp_alternate[i];
4123 if (i < num_reg_temp) {
4124 data->reg_temp[1][i] = reg_temp_over[i];
4125 data->reg_temp[2][i] = reg_temp_hyst[i];
4127 data->temp_src[i] = i + 1;
4131 if (s >= NUM_TEMP) /* Abort if no more space */
4134 data->have_temp |= BIT(s);
4135 data->reg_temp[0][s] = reg_temp_alternate[i];
4136 data->temp_src[s] = i + 1;
4139 #endif /* USE_ALTERNATE */
4141 /* Initialize the chip */
4142 nct6775_init_device(data);
4144 err = superio_enter(sio_data->sioreg);
4148 cr2a = superio_inb(sio_data->sioreg, 0x2a);
4149 switch (data->kind) {
4151 data->have_vid = (cr2a & 0x40);
4154 data->have_vid = (cr2a & 0x60) == 0x40;
4167 * We can get the VID input values directly at logical device D 0xe3.
4169 if (data->have_vid) {
4170 superio_select(sio_data->sioreg, NCT6775_LD_VID);
4171 data->vid = superio_inb(sio_data->sioreg, 0xe3);
4172 data->vrm = vid_which_vrm();
4178 superio_select(sio_data->sioreg, NCT6775_LD_HWM);
4179 tmp = superio_inb(sio_data->sioreg,
4180 NCT6775_REG_CR_FAN_DEBOUNCE);
4181 switch (data->kind) {
4199 superio_outb(sio_data->sioreg, NCT6775_REG_CR_FAN_DEBOUNCE,
4201 dev_info(&pdev->dev, "Enabled fan debounce for chip %s\n",
4205 nct6775_check_fan_inputs(data);
4207 superio_exit(sio_data->sioreg);
4209 /* Read fan clock dividers immediately */
4210 nct6775_init_fan_common(dev, data);
4212 /* Register sysfs hooks */
4213 group = nct6775_create_attr_group(dev, &nct6775_pwm_template_group,
4216 return PTR_ERR(group);
4218 data->groups[num_attr_groups++] = group;
4220 group = nct6775_create_attr_group(dev, &nct6775_in_template_group,
4221 fls(data->have_in));
4223 return PTR_ERR(group);
4225 data->groups[num_attr_groups++] = group;
4227 group = nct6775_create_attr_group(dev, &nct6775_fan_template_group,
4228 fls(data->has_fan));
4230 return PTR_ERR(group);
4232 data->groups[num_attr_groups++] = group;
4234 group = nct6775_create_attr_group(dev, &nct6775_temp_template_group,
4235 fls(data->have_temp));
4237 return PTR_ERR(group);
4239 data->groups[num_attr_groups++] = group;
4240 data->groups[num_attr_groups++] = &nct6775_group_other;
4242 hwmon_dev = devm_hwmon_device_register_with_groups(dev, data->name,
4243 data, data->groups);
4244 return PTR_ERR_OR_ZERO(hwmon_dev);
4247 static void nct6791_enable_io_mapping(int sioaddr)
4251 val = superio_inb(sioaddr, NCT6791_REG_HM_IO_SPACE_LOCK_ENABLE);
4253 pr_info("Enabling hardware monitor logical device mappings.\n");
4254 superio_outb(sioaddr, NCT6791_REG_HM_IO_SPACE_LOCK_ENABLE,
4259 static int __maybe_unused nct6775_suspend(struct device *dev)
4261 struct nct6775_data *data = nct6775_update_device(dev);
4263 mutex_lock(&data->update_lock);
4264 data->vbat = nct6775_read_value(data, data->REG_VBAT);
4265 if (data->kind == nct6775) {
4266 data->fandiv1 = nct6775_read_value(data, NCT6775_REG_FANDIV1);
4267 data->fandiv2 = nct6775_read_value(data, NCT6775_REG_FANDIV2);
4269 mutex_unlock(&data->update_lock);
4274 static int __maybe_unused nct6775_resume(struct device *dev)
4276 struct nct6775_data *data = dev_get_drvdata(dev);
4277 int sioreg = data->sioreg;
4281 mutex_lock(&data->update_lock);
4282 data->bank = 0xff; /* Force initial bank selection */
4284 err = superio_enter(sioreg);
4288 superio_select(sioreg, NCT6775_LD_HWM);
4289 reg = superio_inb(sioreg, SIO_REG_ENABLE);
4290 if (reg != data->sio_reg_enable)
4291 superio_outb(sioreg, SIO_REG_ENABLE, data->sio_reg_enable);
4293 if (data->kind == nct6791 || data->kind == nct6792 ||
4294 data->kind == nct6793 || data->kind == nct6795)
4295 nct6791_enable_io_mapping(sioreg);
4297 superio_exit(sioreg);
4299 /* Restore limits */
4300 for (i = 0; i < data->in_num; i++) {
4301 if (!(data->have_in & BIT(i)))
4304 nct6775_write_value(data, data->REG_IN_MINMAX[0][i],
4306 nct6775_write_value(data, data->REG_IN_MINMAX[1][i],
4310 for (i = 0; i < ARRAY_SIZE(data->fan_min); i++) {
4311 if (!(data->has_fan_min & BIT(i)))
4314 nct6775_write_value(data, data->REG_FAN_MIN[i],
4318 for (i = 0; i < NUM_TEMP; i++) {
4319 if (!(data->have_temp & BIT(i)))
4322 for (j = 1; j < ARRAY_SIZE(data->reg_temp); j++)
4323 if (data->reg_temp[j][i])
4324 nct6775_write_temp(data, data->reg_temp[j][i],
4328 /* Restore other settings */
4329 nct6775_write_value(data, data->REG_VBAT, data->vbat);
4330 if (data->kind == nct6775) {
4331 nct6775_write_value(data, NCT6775_REG_FANDIV1, data->fandiv1);
4332 nct6775_write_value(data, NCT6775_REG_FANDIV2, data->fandiv2);
4336 /* Force re-reading all values */
4337 data->valid = false;
4338 mutex_unlock(&data->update_lock);
4343 static SIMPLE_DEV_PM_OPS(nct6775_dev_pm_ops, nct6775_suspend, nct6775_resume);
4345 static struct platform_driver nct6775_driver = {
4348 .pm = &nct6775_dev_pm_ops,
4350 .probe = nct6775_probe,
4353 /* nct6775_find() looks for a '627 in the Super-I/O config space */
4354 static int __init nct6775_find(int sioaddr, struct nct6775_sio_data *sio_data)
4360 err = superio_enter(sioaddr);
4364 val = (superio_inb(sioaddr, SIO_REG_DEVID) << 8) |
4365 superio_inb(sioaddr, SIO_REG_DEVID + 1);
4366 if (force_id && val != 0xffff)
4369 switch (val & SIO_ID_MASK) {
4370 case SIO_NCT6106_ID:
4371 sio_data->kind = nct6106;
4373 case SIO_NCT6775_ID:
4374 sio_data->kind = nct6775;
4376 case SIO_NCT6776_ID:
4377 sio_data->kind = nct6776;
4379 case SIO_NCT6779_ID:
4380 sio_data->kind = nct6779;
4382 case SIO_NCT6791_ID:
4383 sio_data->kind = nct6791;
4385 case SIO_NCT6792_ID:
4386 sio_data->kind = nct6792;
4388 case SIO_NCT6793_ID:
4389 sio_data->kind = nct6793;
4391 case SIO_NCT6795_ID:
4392 sio_data->kind = nct6795;
4396 pr_debug("unsupported chip ID: 0x%04x\n", val);
4397 superio_exit(sioaddr);
4401 /* We have a known chip, find the HWM I/O address */
4402 superio_select(sioaddr, NCT6775_LD_HWM);
4403 val = (superio_inb(sioaddr, SIO_REG_ADDR) << 8)
4404 | superio_inb(sioaddr, SIO_REG_ADDR + 1);
4405 addr = val & IOREGION_ALIGNMENT;
4407 pr_err("Refusing to enable a Super-I/O device with a base I/O port 0\n");
4408 superio_exit(sioaddr);
4412 /* Activate logical device if needed */
4413 val = superio_inb(sioaddr, SIO_REG_ENABLE);
4414 if (!(val & 0x01)) {
4415 pr_warn("Forcibly enabling Super-I/O. Sensor is probably unusable.\n");
4416 superio_outb(sioaddr, SIO_REG_ENABLE, val | 0x01);
4419 if (sio_data->kind == nct6791 || sio_data->kind == nct6792 ||
4420 sio_data->kind == nct6793 || sio_data->kind == nct6795)
4421 nct6791_enable_io_mapping(sioaddr);
4423 superio_exit(sioaddr);
4424 pr_info("Found %s or compatible chip at %#x:%#x\n",
4425 nct6775_sio_names[sio_data->kind], sioaddr, addr);
4426 sio_data->sioreg = sioaddr;
4432 * when Super-I/O functions move to a separate file, the Super-I/O
4433 * bus will manage the lifetime of the device and this module will only keep
4434 * track of the nct6775 driver. But since we use platform_device_alloc(), we
4435 * must keep track of the device
4437 static struct platform_device *pdev[2];
4439 static int __init sensors_nct6775_init(void)
4444 struct resource res;
4445 struct nct6775_sio_data sio_data;
4446 int sioaddr[2] = { 0x2e, 0x4e };
4448 err = platform_driver_register(&nct6775_driver);
4453 * initialize sio_data->kind and sio_data->sioreg.
4455 * when Super-I/O functions move to a separate file, the Super-I/O
4456 * driver will probe 0x2e and 0x4e and auto-detect the presence of a
4457 * nct6775 hardware monitor, and call probe()
4459 for (i = 0; i < ARRAY_SIZE(pdev); i++) {
4460 address = nct6775_find(sioaddr[i], &sio_data);
4466 pdev[i] = platform_device_alloc(DRVNAME, address);
4469 goto exit_device_unregister;
4472 err = platform_device_add_data(pdev[i], &sio_data,
4473 sizeof(struct nct6775_sio_data));
4475 goto exit_device_put;
4477 memset(&res, 0, sizeof(res));
4479 res.start = address + IOREGION_OFFSET;
4480 res.end = address + IOREGION_OFFSET + IOREGION_LENGTH - 1;
4481 res.flags = IORESOURCE_IO;
4483 err = acpi_check_resource_conflict(&res);
4485 platform_device_put(pdev[i]);
4490 err = platform_device_add_resources(pdev[i], &res, 1);
4492 goto exit_device_put;
4494 /* platform_device_add calls probe() */
4495 err = platform_device_add(pdev[i]);
4497 goto exit_device_put;
4501 goto exit_unregister;
4507 platform_device_put(pdev[i]);
4508 exit_device_unregister:
4511 platform_device_unregister(pdev[i]);
4514 platform_driver_unregister(&nct6775_driver);
4518 static void __exit sensors_nct6775_exit(void)
4522 for (i = 0; i < ARRAY_SIZE(pdev); i++) {
4524 platform_device_unregister(pdev[i]);
4526 platform_driver_unregister(&nct6775_driver);
4529 MODULE_AUTHOR("Guenter Roeck <linux@roeck-us.net>");
4530 MODULE_DESCRIPTION("Driver for NCT6775F and compatible chips");
4531 MODULE_LICENSE("GPL");
4533 module_init(sensors_nct6775_init);
4534 module_exit(sensors_nct6775_exit);