2 * nct6775 - Driver for the hardware monitoring functionality of
3 * Nuvoton NCT677x Super-I/O chips
5 * Copyright (C) 2012 Guenter Roeck <linux@roeck-us.net>
7 * Derived from w83627ehf driver
8 * Copyright (C) 2005-2012 Jean Delvare <khali@linux-fr.org>
9 * Copyright (C) 2006 Yuan Mu (Winbond),
10 * Rudolf Marek <r.marek@assembler.cz>
11 * David Hubbard <david.c.hubbard@gmail.com>
12 * Daniel J Blueman <daniel.blueman@gmail.com>
13 * Copyright (C) 2010 Sheng-Yuan Huang (Nuvoton) (PS00)
15 * Shamelessly ripped from the w83627hf driver
16 * Copyright (C) 2003 Mark Studebaker
18 * This program is free software; you can redistribute it and/or modify
19 * it under the terms of the GNU General Public License as published by
20 * the Free Software Foundation; either version 2 of the License, or
21 * (at your option) any later version.
23 * This program is distributed in the hope that it will be useful,
24 * but WITHOUT ANY WARRANTY; without even the implied warranty of
25 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
26 * GNU General Public License for more details.
28 * You should have received a copy of the GNU General Public License
29 * along with this program; if not, write to the Free Software
30 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
33 * Supports the following chips:
35 * Chip #vin #fan #pwm #temp chip IDs man ID
36 * 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
41 * #temp lists the number of monitored temperature sources (first value) plus
42 * the number of directly connectable temperature sensors (second value).
45 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
47 #include <linux/module.h>
48 #include <linux/init.h>
49 #include <linux/slab.h>
50 #include <linux/jiffies.h>
51 #include <linux/platform_device.h>
52 #include <linux/hwmon.h>
53 #include <linux/hwmon-sysfs.h>
54 #include <linux/hwmon-vid.h>
55 #include <linux/err.h>
56 #include <linux/mutex.h>
57 #include <linux/acpi.h>
63 enum kinds { nct6106, nct6775, nct6776, nct6779 };
65 /* used to set data->name = nct6775_device_names[data->sio_kind] */
66 static const char * const nct6775_device_names[] = {
73 static unsigned short force_id;
74 module_param(force_id, ushort, 0);
75 MODULE_PARM_DESC(force_id, "Override the detected device ID");
77 static unsigned short fan_debounce;
78 module_param(fan_debounce, ushort, 0);
79 MODULE_PARM_DESC(fan_debounce, "Enable debouncing for fan RPM signal");
81 #define DRVNAME "nct6775"
84 * Super-I/O constants and functions
87 #define NCT6775_LD_ACPI 0x0a
88 #define NCT6775_LD_HWM 0x0b
89 #define NCT6775_LD_VID 0x0d
91 #define SIO_REG_LDSEL 0x07 /* Logical device select */
92 #define SIO_REG_DEVID 0x20 /* Device ID (2 bytes) */
93 #define SIO_REG_ENABLE 0x30 /* Logical device enable */
94 #define SIO_REG_ADDR 0x60 /* Logical device address (2 bytes) */
96 #define SIO_NCT6106_ID 0xc450
97 #define SIO_NCT6775_ID 0xb470
98 #define SIO_NCT6776_ID 0xc330
99 #define SIO_NCT6779_ID 0xc560
100 #define SIO_ID_MASK 0xFFF0
102 enum pwm_enable { off, manual, thermal_cruise, speed_cruise, sf3, sf4 };
105 superio_outb(int ioreg, int reg, int val)
108 outb(val, ioreg + 1);
112 superio_inb(int ioreg, int reg)
115 return inb(ioreg + 1);
119 superio_select(int ioreg, int ld)
121 outb(SIO_REG_LDSEL, ioreg);
126 superio_enter(int ioreg)
129 * Try to reserve <ioreg> and <ioreg + 1> for exclusive access.
131 if (!request_muxed_region(ioreg, 2, DRVNAME))
141 superio_exit(int ioreg)
145 outb(0x02, ioreg + 1);
146 release_region(ioreg, 2);
153 #define IOREGION_ALIGNMENT (~7)
154 #define IOREGION_OFFSET 5
155 #define IOREGION_LENGTH 2
156 #define ADDR_REG_OFFSET 0
157 #define DATA_REG_OFFSET 1
159 #define NCT6775_REG_BANK 0x4E
160 #define NCT6775_REG_CONFIG 0x40
163 * Not currently used:
164 * REG_MAN_ID has the value 0x5ca3 for all supported chips.
165 * REG_CHIP_ID == 0x88/0xa1/0xc1 depending on chip model.
166 * REG_MAN_ID is at port 0x4f
167 * REG_CHIP_ID is at port 0x58
170 #define NUM_TEMP 10 /* Max number of temp attribute sets w/ limits*/
171 #define NUM_TEMP_FIXED 6 /* Max number of fixed temp attribute sets */
173 #define NUM_REG_ALARM 7 /* Max number of alarm registers */
174 #define NUM_REG_BEEP 5 /* Max number of beep registers */
176 /* Common and NCT6775 specific data */
178 /* Voltage min/max registers for nr=7..14 are in bank 5 */
180 static const u16 NCT6775_REG_IN_MAX[] = {
181 0x2b, 0x2d, 0x2f, 0x31, 0x33, 0x35, 0x37, 0x554, 0x556, 0x558, 0x55a,
182 0x55c, 0x55e, 0x560, 0x562 };
183 static const u16 NCT6775_REG_IN_MIN[] = {
184 0x2c, 0x2e, 0x30, 0x32, 0x34, 0x36, 0x38, 0x555, 0x557, 0x559, 0x55b,
185 0x55d, 0x55f, 0x561, 0x563 };
186 static const u16 NCT6775_REG_IN[] = {
187 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x550, 0x551, 0x552
190 #define NCT6775_REG_VBAT 0x5D
191 #define NCT6775_REG_DIODE 0x5E
192 #define NCT6775_DIODE_MASK 0x02
194 #define NCT6775_REG_FANDIV1 0x506
195 #define NCT6775_REG_FANDIV2 0x507
197 #define NCT6775_REG_CR_FAN_DEBOUNCE 0xf0
199 static const u16 NCT6775_REG_ALARM[NUM_REG_ALARM] = { 0x459, 0x45A, 0x45B };
201 /* 0..15 voltages, 16..23 fans, 24..29 temperatures, 30..31 intrusion */
203 static const s8 NCT6775_ALARM_BITS[] = {
204 0, 1, 2, 3, 8, 21, 20, 16, /* in0.. in7 */
205 17, -1, -1, -1, -1, -1, -1, /* in8..in14 */
207 6, 7, 11, -1, -1, /* fan1..fan5 */
208 -1, -1, -1, /* unused */
209 4, 5, 13, -1, -1, -1, /* temp1..temp6 */
210 12, -1 }; /* intrusion0, intrusion1 */
212 #define FAN_ALARM_BASE 16
213 #define TEMP_ALARM_BASE 24
214 #define INTRUSION_ALARM_BASE 30
216 static const u16 NCT6775_REG_BEEP[NUM_REG_BEEP] = { 0x56, 0x57, 0x453, 0x4e };
219 * 0..14 voltages, 15 global beep enable, 16..23 fans, 24..29 temperatures,
222 static const s8 NCT6775_BEEP_BITS[] = {
223 0, 1, 2, 3, 8, 9, 10, 16, /* in0.. in7 */
224 17, -1, -1, -1, -1, -1, -1, /* in8..in14 */
225 21, /* global beep enable */
226 6, 7, 11, 28, -1, /* fan1..fan5 */
227 -1, -1, -1, /* unused */
228 4, 5, 13, -1, -1, -1, /* temp1..temp6 */
229 12, -1 }; /* intrusion0, intrusion1 */
231 #define BEEP_ENABLE_BASE 15
233 static const u8 NCT6775_REG_CR_CASEOPEN_CLR[] = { 0xe6, 0xee };
234 static const u8 NCT6775_CR_CASEOPEN_CLR_MASK[] = { 0x20, 0x01 };
236 /* DC or PWM output fan configuration */
237 static const u8 NCT6775_REG_PWM_MODE[] = { 0x04, 0x04, 0x12 };
238 static const u8 NCT6775_PWM_MODE_MASK[] = { 0x01, 0x02, 0x01 };
240 /* Advanced Fan control, some values are common for all fans */
242 static const u16 NCT6775_REG_TARGET[] = { 0x101, 0x201, 0x301, 0x801, 0x901 };
243 static const u16 NCT6775_REG_FAN_MODE[] = { 0x102, 0x202, 0x302, 0x802, 0x902 };
244 static const u16 NCT6775_REG_FAN_STEP_DOWN_TIME[] = {
245 0x103, 0x203, 0x303, 0x803, 0x903 };
246 static const u16 NCT6775_REG_FAN_STEP_UP_TIME[] = {
247 0x104, 0x204, 0x304, 0x804, 0x904 };
248 static const u16 NCT6775_REG_FAN_STOP_OUTPUT[] = {
249 0x105, 0x205, 0x305, 0x805, 0x905 };
250 static const u16 NCT6775_REG_FAN_START_OUTPUT[]
251 = { 0x106, 0x206, 0x306, 0x806, 0x906 };
252 static const u16 NCT6775_REG_FAN_MAX_OUTPUT[] = { 0x10a, 0x20a, 0x30a };
253 static const u16 NCT6775_REG_FAN_STEP_OUTPUT[] = { 0x10b, 0x20b, 0x30b };
255 static const u16 NCT6775_REG_FAN_STOP_TIME[] = {
256 0x107, 0x207, 0x307, 0x807, 0x907 };
257 static const u16 NCT6775_REG_PWM[] = { 0x109, 0x209, 0x309, 0x809, 0x909 };
258 static const u16 NCT6775_REG_PWM_READ[] = { 0x01, 0x03, 0x11, 0x13, 0x15 };
260 static const u16 NCT6775_REG_FAN[] = { 0x630, 0x632, 0x634, 0x636, 0x638 };
261 static const u16 NCT6775_REG_FAN_MIN[] = { 0x3b, 0x3c, 0x3d };
262 static const u16 NCT6775_REG_FAN_PULSES[] = { 0x641, 0x642, 0x643, 0x644, 0 };
263 static const u16 NCT6775_FAN_PULSE_SHIFT[] = { 0, 0, 0, 0, 0 };
265 static const u16 NCT6775_REG_TEMP[] = {
266 0x27, 0x150, 0x250, 0x62b, 0x62c, 0x62d };
268 static const u16 NCT6775_REG_TEMP_CONFIG[ARRAY_SIZE(NCT6775_REG_TEMP)] = {
269 0, 0x152, 0x252, 0x628, 0x629, 0x62A };
270 static const u16 NCT6775_REG_TEMP_HYST[ARRAY_SIZE(NCT6775_REG_TEMP)] = {
271 0x3a, 0x153, 0x253, 0x673, 0x678, 0x67D };
272 static const u16 NCT6775_REG_TEMP_OVER[ARRAY_SIZE(NCT6775_REG_TEMP)] = {
273 0x39, 0x155, 0x255, 0x672, 0x677, 0x67C };
275 static const u16 NCT6775_REG_TEMP_SOURCE[ARRAY_SIZE(NCT6775_REG_TEMP)] = {
276 0x621, 0x622, 0x623, 0x624, 0x625, 0x626 };
278 static const u16 NCT6775_REG_TEMP_SEL[] = {
279 0x100, 0x200, 0x300, 0x800, 0x900 };
281 static const u16 NCT6775_REG_WEIGHT_TEMP_SEL[] = {
282 0x139, 0x239, 0x339, 0x839, 0x939 };
283 static const u16 NCT6775_REG_WEIGHT_TEMP_STEP[] = {
284 0x13a, 0x23a, 0x33a, 0x83a, 0x93a };
285 static const u16 NCT6775_REG_WEIGHT_TEMP_STEP_TOL[] = {
286 0x13b, 0x23b, 0x33b, 0x83b, 0x93b };
287 static const u16 NCT6775_REG_WEIGHT_DUTY_STEP[] = {
288 0x13c, 0x23c, 0x33c, 0x83c, 0x93c };
289 static const u16 NCT6775_REG_WEIGHT_TEMP_BASE[] = {
290 0x13d, 0x23d, 0x33d, 0x83d, 0x93d };
292 static const u16 NCT6775_REG_TEMP_OFFSET[] = { 0x454, 0x455, 0x456 };
294 static const u16 NCT6775_REG_AUTO_TEMP[] = {
295 0x121, 0x221, 0x321, 0x821, 0x921 };
296 static const u16 NCT6775_REG_AUTO_PWM[] = {
297 0x127, 0x227, 0x327, 0x827, 0x927 };
299 #define NCT6775_AUTO_TEMP(data, nr, p) ((data)->REG_AUTO_TEMP[nr] + (p))
300 #define NCT6775_AUTO_PWM(data, nr, p) ((data)->REG_AUTO_PWM[nr] + (p))
302 static const u16 NCT6775_REG_CRITICAL_ENAB[] = { 0x134, 0x234, 0x334 };
304 static const u16 NCT6775_REG_CRITICAL_TEMP[] = {
305 0x135, 0x235, 0x335, 0x835, 0x935 };
306 static const u16 NCT6775_REG_CRITICAL_TEMP_TOLERANCE[] = {
307 0x138, 0x238, 0x338, 0x838, 0x938 };
309 static const char *const nct6775_temp_label[] = {
323 "PCH_CHIP_CPU_MAX_TEMP",
333 static const u16 NCT6775_REG_TEMP_ALTERNATE[ARRAY_SIZE(nct6775_temp_label) - 1]
334 = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0x661, 0x662, 0x664 };
336 static const u16 NCT6775_REG_TEMP_CRIT[ARRAY_SIZE(nct6775_temp_label) - 1]
337 = { 0, 0, 0, 0, 0xa00, 0xa01, 0xa02, 0xa03, 0xa04, 0xa05, 0xa06,
340 /* NCT6776 specific data */
342 static const s8 NCT6776_ALARM_BITS[] = {
343 0, 1, 2, 3, 8, 21, 20, 16, /* in0.. in7 */
344 17, -1, -1, -1, -1, -1, -1, /* in8..in14 */
346 6, 7, 11, 10, 23, /* fan1..fan5 */
347 -1, -1, -1, /* unused */
348 4, 5, 13, -1, -1, -1, /* temp1..temp6 */
349 12, 9 }; /* intrusion0, intrusion1 */
351 static const u16 NCT6776_REG_BEEP[NUM_REG_BEEP] = { 0xb2, 0xb3, 0xb4, 0xb5 };
353 static const s8 NCT6776_BEEP_BITS[] = {
354 0, 1, 2, 3, 4, 5, 6, 7, /* in0.. in7 */
355 8, -1, -1, -1, -1, -1, -1, /* in8..in14 */
356 24, /* global beep enable */
357 25, 26, 27, 28, 29, /* fan1..fan5 */
358 -1, -1, -1, /* unused */
359 16, 17, 18, 19, 20, 21, /* temp1..temp6 */
360 30, 31 }; /* intrusion0, intrusion1 */
362 static const u16 NCT6776_REG_TOLERANCE_H[] = {
363 0x10c, 0x20c, 0x30c, 0x80c, 0x90c };
365 static const u8 NCT6776_REG_PWM_MODE[] = { 0x04, 0, 0 };
366 static const u8 NCT6776_PWM_MODE_MASK[] = { 0x01, 0, 0 };
368 static const u16 NCT6776_REG_FAN_MIN[] = { 0x63a, 0x63c, 0x63e, 0x640, 0x642 };
369 static const u16 NCT6776_REG_FAN_PULSES[] = { 0x644, 0x645, 0x646, 0, 0 };
371 static const u16 NCT6776_REG_WEIGHT_DUTY_BASE[] = {
372 0x13e, 0x23e, 0x33e, 0x83e, 0x93e };
374 static const u16 NCT6776_REG_TEMP_CONFIG[ARRAY_SIZE(NCT6775_REG_TEMP)] = {
375 0x18, 0x152, 0x252, 0x628, 0x629, 0x62A };
377 static const char *const nct6776_temp_label[] = {
392 "PCH_CHIP_CPU_MAX_TEMP",
403 static const u16 NCT6776_REG_TEMP_ALTERNATE[ARRAY_SIZE(nct6776_temp_label) - 1]
404 = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0x401, 0x402, 0x404 };
406 static const u16 NCT6776_REG_TEMP_CRIT[ARRAY_SIZE(nct6776_temp_label) - 1]
407 = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0x709, 0x70a };
409 /* NCT6779 specific data */
411 static const u16 NCT6779_REG_IN[] = {
412 0x480, 0x481, 0x482, 0x483, 0x484, 0x485, 0x486, 0x487,
413 0x488, 0x489, 0x48a, 0x48b, 0x48c, 0x48d, 0x48e };
415 static const u16 NCT6779_REG_ALARM[NUM_REG_ALARM] = {
416 0x459, 0x45A, 0x45B, 0x568 };
418 static const s8 NCT6779_ALARM_BITS[] = {
419 0, 1, 2, 3, 8, 21, 20, 16, /* in0.. in7 */
420 17, 24, 25, 26, 27, 28, 29, /* in8..in14 */
422 6, 7, 11, 10, 23, /* fan1..fan5 */
423 -1, -1, -1, /* unused */
424 4, 5, 13, -1, -1, -1, /* temp1..temp6 */
425 12, 9 }; /* intrusion0, intrusion1 */
427 static const s8 NCT6779_BEEP_BITS[] = {
428 0, 1, 2, 3, 4, 5, 6, 7, /* in0.. in7 */
429 8, 9, 10, 11, 12, 13, 14, /* in8..in14 */
430 24, /* global beep enable */
431 25, 26, 27, 28, 29, /* fan1..fan5 */
432 -1, -1, -1, /* unused */
433 16, 17, -1, -1, -1, -1, /* temp1..temp6 */
434 30, 31 }; /* intrusion0, intrusion1 */
436 static const u16 NCT6779_REG_FAN[] = { 0x4b0, 0x4b2, 0x4b4, 0x4b6, 0x4b8 };
437 static const u16 NCT6779_REG_FAN_PULSES[] = {
438 0x644, 0x645, 0x646, 0x647, 0x648 };
440 static const u16 NCT6779_REG_CRITICAL_PWM_ENABLE[] = {
441 0x136, 0x236, 0x336, 0x836, 0x936 };
442 #define NCT6779_CRITICAL_PWM_ENABLE_MASK 0x01
443 static const u16 NCT6779_REG_CRITICAL_PWM[] = {
444 0x137, 0x237, 0x337, 0x837, 0x937 };
446 static const u16 NCT6779_REG_TEMP[] = { 0x27, 0x150 };
447 static const u16 NCT6779_REG_TEMP_CONFIG[ARRAY_SIZE(NCT6779_REG_TEMP)] = {
449 static const u16 NCT6779_REG_TEMP_HYST[ARRAY_SIZE(NCT6779_REG_TEMP)] = {
451 static const u16 NCT6779_REG_TEMP_OVER[ARRAY_SIZE(NCT6779_REG_TEMP)] = {
454 static const u16 NCT6779_REG_TEMP_OFFSET[] = {
455 0x454, 0x455, 0x456, 0x44a, 0x44b, 0x44c };
457 static const char *const nct6779_temp_label[] = {
476 "PCH_CHIP_CPU_MAX_TEMP",
487 static const u16 NCT6779_REG_TEMP_ALTERNATE[ARRAY_SIZE(nct6779_temp_label) - 1]
488 = { 0x490, 0x491, 0x492, 0x493, 0x494, 0x495, 0, 0,
489 0, 0, 0, 0, 0, 0, 0, 0,
490 0, 0x400, 0x401, 0x402, 0x404, 0x405, 0x406, 0x407,
493 static const u16 NCT6779_REG_TEMP_CRIT[ARRAY_SIZE(nct6779_temp_label) - 1]
494 = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0x709, 0x70a };
496 /* NCT6102D/NCT6106D specific data */
498 #define NCT6106_REG_VBAT 0x318
499 #define NCT6106_REG_DIODE 0x319
500 #define NCT6106_DIODE_MASK 0x01
502 static const u16 NCT6106_REG_IN_MAX[] = {
503 0x90, 0x92, 0x94, 0x96, 0x98, 0x9a, 0x9e, 0xa0, 0xa2 };
504 static const u16 NCT6106_REG_IN_MIN[] = {
505 0x91, 0x93, 0x95, 0x97, 0x99, 0x9b, 0x9f, 0xa1, 0xa3 };
506 static const u16 NCT6106_REG_IN[] = {
507 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x07, 0x08, 0x09 };
509 static const u16 NCT6106_REG_TEMP[] = { 0x10, 0x11, 0x12, 0x13, 0x14, 0x15 };
510 static const u16 NCT6106_REG_TEMP_HYST[] = {
511 0xc3, 0xc7, 0xcb, 0xcf, 0xd3, 0xd7 };
512 static const u16 NCT6106_REG_TEMP_OVER[] = {
513 0xc2, 0xc6, 0xca, 0xce, 0xd2, 0xd6 };
514 static const u16 NCT6106_REG_TEMP_CRIT_L[] = {
515 0xc0, 0xc4, 0xc8, 0xcc, 0xd0, 0xd4 };
516 static const u16 NCT6106_REG_TEMP_CRIT_H[] = {
517 0xc1, 0xc5, 0xc9, 0xcf, 0xd1, 0xd5 };
518 static const u16 NCT6106_REG_TEMP_OFFSET[] = { 0x311, 0x312, 0x313 };
519 static const u16 NCT6106_REG_TEMP_CONFIG[] = {
520 0xb7, 0xb8, 0xb9, 0xba, 0xbb, 0xbc };
522 static const u16 NCT6106_REG_FAN[] = { 0x20, 0x22, 0x24 };
523 static const u16 NCT6106_REG_FAN_MIN[] = { 0xe0, 0xe2, 0xe4 };
524 static const u16 NCT6106_REG_FAN_PULSES[] = { 0xf6, 0xf6, 0xf6, 0, 0 };
525 static const u16 NCT6106_FAN_PULSE_SHIFT[] = { 0, 2, 4, 0, 0 };
527 static const u8 NCT6106_REG_PWM_MODE[] = { 0xf3, 0xf3, 0xf3 };
528 static const u8 NCT6106_PWM_MODE_MASK[] = { 0x01, 0x02, 0x04 };
529 static const u16 NCT6106_REG_PWM[] = { 0x119, 0x129, 0x139 };
530 static const u16 NCT6106_REG_PWM_READ[] = { 0x4a, 0x4b, 0x4c };
531 static const u16 NCT6106_REG_FAN_MODE[] = { 0x113, 0x123, 0x133 };
532 static const u16 NCT6106_REG_TEMP_SEL[] = { 0x110, 0x120, 0x130 };
533 static const u16 NCT6106_REG_TEMP_SOURCE[] = {
534 0xb0, 0xb1, 0xb2, 0xb3, 0xb4, 0xb5 };
536 static const u16 NCT6106_REG_CRITICAL_TEMP[] = { 0x11a, 0x12a, 0x13a };
537 static const u16 NCT6106_REG_CRITICAL_TEMP_TOLERANCE[] = {
538 0x11b, 0x12b, 0x13b };
540 static const u16 NCT6106_REG_CRITICAL_PWM_ENABLE[] = { 0x11c, 0x12c, 0x13c };
541 #define NCT6106_CRITICAL_PWM_ENABLE_MASK 0x10
542 static const u16 NCT6106_REG_CRITICAL_PWM[] = { 0x11d, 0x12d, 0x13d };
544 static const u16 NCT6106_REG_FAN_STEP_UP_TIME[] = { 0x114, 0x124, 0x134 };
545 static const u16 NCT6106_REG_FAN_STEP_DOWN_TIME[] = { 0x115, 0x125, 0x135 };
546 static const u16 NCT6106_REG_FAN_STOP_OUTPUT[] = { 0x116, 0x126, 0x136 };
547 static const u16 NCT6106_REG_FAN_START_OUTPUT[] = { 0x117, 0x127, 0x137 };
548 static const u16 NCT6106_REG_FAN_STOP_TIME[] = { 0x118, 0x128, 0x138 };
549 static const u16 NCT6106_REG_TOLERANCE_H[] = { 0x112, 0x122, 0x132 };
551 static const u16 NCT6106_REG_TARGET[] = { 0x111, 0x121, 0x131 };
553 static const u16 NCT6106_REG_WEIGHT_TEMP_SEL[] = { 0x168, 0x178, 0x188 };
554 static const u16 NCT6106_REG_WEIGHT_TEMP_STEP[] = { 0x169, 0x179, 0x189 };
555 static const u16 NCT6106_REG_WEIGHT_TEMP_STEP_TOL[] = { 0x16a, 0x17a, 0x18a };
556 static const u16 NCT6106_REG_WEIGHT_DUTY_STEP[] = { 0x16b, 0x17b, 0x17c };
557 static const u16 NCT6106_REG_WEIGHT_TEMP_BASE[] = { 0x16c, 0x17c, 0x18c };
558 static const u16 NCT6106_REG_WEIGHT_DUTY_BASE[] = { 0x16d, 0x17d, 0x18d };
560 static const u16 NCT6106_REG_AUTO_TEMP[] = { 0x160, 0x170, 0x180 };
561 static const u16 NCT6106_REG_AUTO_PWM[] = { 0x164, 0x174, 0x184 };
563 static const u16 NCT6106_REG_ALARM[NUM_REG_ALARM] = {
564 0x77, 0x78, 0x79, 0x7a, 0x7b, 0x7c, 0x7d };
566 static const s8 NCT6106_ALARM_BITS[] = {
567 0, 1, 2, 3, 4, 5, 7, 8, /* in0.. in7 */
568 9, -1, -1, -1, -1, -1, -1, /* in8..in14 */
570 32, 33, 34, -1, -1, /* fan1..fan5 */
571 -1, -1, -1, /* unused */
572 16, 17, 18, 19, 20, 21, /* temp1..temp6 */
573 48, -1 /* intrusion0, intrusion1 */
576 static const u16 NCT6106_REG_BEEP[NUM_REG_BEEP] = {
577 0x3c0, 0x3c1, 0x3c2, 0x3c3, 0x3c4 };
579 static const s8 NCT6106_BEEP_BITS[] = {
580 0, 1, 2, 3, 4, 5, 7, 8, /* in0.. in7 */
581 9, 10, 11, 12, -1, -1, -1, /* in8..in14 */
582 32, /* global beep enable */
583 24, 25, 26, 27, 28, /* fan1..fan5 */
584 -1, -1, -1, /* unused */
585 16, 17, 18, 19, 20, 21, /* temp1..temp6 */
586 34, -1 /* intrusion0, intrusion1 */
589 static const u16 NCT6106_REG_TEMP_ALTERNATE[ARRAY_SIZE(nct6776_temp_label) - 1]
590 = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0x51, 0x52, 0x54 };
592 static const u16 NCT6106_REG_TEMP_CRIT[ARRAY_SIZE(nct6776_temp_label) - 1]
593 = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0x204, 0x205 };
595 static enum pwm_enable reg_to_pwm_enable(int pwm, int mode)
597 if (mode == 0 && pwm == 255)
602 static int pwm_enable_to_reg(enum pwm_enable mode)
613 /* 1 is DC mode, output in ms */
614 static unsigned int step_time_from_reg(u8 reg, u8 mode)
616 return mode ? 400 * reg : 100 * reg;
619 static u8 step_time_to_reg(unsigned int msec, u8 mode)
621 return clamp_val((mode ? (msec + 200) / 400 :
622 (msec + 50) / 100), 1, 255);
625 static unsigned int fan_from_reg8(u16 reg, unsigned int divreg)
627 if (reg == 0 || reg == 255)
629 return 1350000U / (reg << divreg);
632 static unsigned int fan_from_reg13(u16 reg, unsigned int divreg)
634 if ((reg & 0xff1f) == 0xff1f)
637 reg = (reg & 0x1f) | ((reg & 0xff00) >> 3);
642 return 1350000U / reg;
645 static unsigned int fan_from_reg16(u16 reg, unsigned int divreg)
647 if (reg == 0 || reg == 0xffff)
651 * Even though the registers are 16 bit wide, the fan divisor
654 return 1350000U / (reg << divreg);
657 static u16 fan_to_reg(u32 fan, unsigned int divreg)
662 return (1350000U / fan) >> divreg;
665 static inline unsigned int
672 * Some of the voltage inputs have internal scaling, the tables below
673 * contain 8 (the ADC LSB in mV) * scaling factor * 100
675 static const u16 scale_in[15] = {
676 800, 800, 1600, 1600, 800, 800, 800, 1600, 1600, 800, 800, 800, 800,
680 static inline long in_from_reg(u8 reg, u8 nr)
682 return DIV_ROUND_CLOSEST(reg * scale_in[nr], 100);
685 static inline u8 in_to_reg(u32 val, u8 nr)
687 return clamp_val(DIV_ROUND_CLOSEST(val * 100, scale_in[nr]), 0, 255);
691 * Data structures and manipulation thereof
694 struct nct6775_data {
695 int addr; /* IO base of hw monitor block */
699 struct device *hwmon_dev;
700 struct attribute_group *group_in;
701 struct attribute_group *group_fan;
702 struct attribute_group *group_temp;
703 struct attribute_group *group_pwm;
705 u16 reg_temp[5][NUM_TEMP]; /* 0=temp, 1=temp_over, 2=temp_hyst,
706 * 3=temp_crit, 4=temp_lcrit
708 u8 temp_src[NUM_TEMP];
709 u16 reg_temp_config[NUM_TEMP];
710 const char * const *temp_label;
718 const s8 *ALARM_BITS;
722 const u16 *REG_IN_MINMAX[2];
724 const u16 *REG_TARGET;
726 const u16 *REG_FAN_MODE;
727 const u16 *REG_FAN_MIN;
728 const u16 *REG_FAN_PULSES;
729 const u16 *FAN_PULSE_SHIFT;
730 const u16 *REG_FAN_TIME[3];
732 const u16 *REG_TOLERANCE_H;
734 const u8 *REG_PWM_MODE;
735 const u8 *PWM_MODE_MASK;
737 const u16 *REG_PWM[7]; /* [0]=pwm, [1]=pwm_start, [2]=pwm_floor,
738 * [3]=pwm_max, [4]=pwm_step,
739 * [5]=weight_duty_step, [6]=weight_duty_base
741 const u16 *REG_PWM_READ;
743 const u16 *REG_CRITICAL_PWM_ENABLE;
744 u8 CRITICAL_PWM_ENABLE_MASK;
745 const u16 *REG_CRITICAL_PWM;
747 const u16 *REG_AUTO_TEMP;
748 const u16 *REG_AUTO_PWM;
750 const u16 *REG_CRITICAL_TEMP;
751 const u16 *REG_CRITICAL_TEMP_TOLERANCE;
753 const u16 *REG_TEMP_SOURCE; /* temp register sources */
754 const u16 *REG_TEMP_SEL;
755 const u16 *REG_WEIGHT_TEMP_SEL;
756 const u16 *REG_WEIGHT_TEMP[3]; /* 0=base, 1=tolerance, 2=step */
758 const u16 *REG_TEMP_OFFSET;
760 const u16 *REG_ALARM;
763 unsigned int (*fan_from_reg)(u16 reg, unsigned int divreg);
764 unsigned int (*fan_from_reg_min)(u16 reg, unsigned int divreg);
766 struct mutex update_lock;
767 bool valid; /* true if following fields are valid */
768 unsigned long last_updated; /* In jiffies */
770 /* Register values */
771 u8 bank; /* current register bank */
772 u8 in_num; /* number of in inputs we have */
773 u8 in[15][3]; /* [0]=in, [1]=in_max, [2]=in_min */
779 u8 has_fan; /* some fan inputs can be disabled */
780 u8 has_fan_min; /* some fans don't have min register */
783 u8 num_temp_alarms; /* 2, 3, or 6 */
784 u8 num_temp_beeps; /* 2, 3, or 6 */
785 u8 temp_fixed_num; /* 3 or 6 */
786 u8 temp_type[NUM_TEMP_FIXED];
787 s8 temp_offset[NUM_TEMP_FIXED];
788 s16 temp[4][NUM_TEMP]; /* 0=temp, 1=temp_over, 2=temp_hyst,
793 u8 pwm_num; /* number of pwm */
794 u8 pwm_mode[5]; /* 1->DC variable voltage, 0->PWM variable duty cycle */
795 enum pwm_enable pwm_enable[5];
798 * 2->thermal cruise mode (also called SmartFan I)
799 * 3->fan speed cruise mode
801 * 5->enhanced variable thermal cruise (SmartFan IV)
803 u8 pwm[7][5]; /* [0]=pwm, [1]=pwm_start, [2]=pwm_floor,
804 * [3]=pwm_max, [4]=pwm_step,
805 * [5]=weight_duty_step, [6]=weight_duty_base
811 u32 target_speed_tolerance[5];
812 u8 speed_tolerance_limit;
814 u8 temp_tolerance[2][5];
817 u8 fan_time[3][5]; /* 0 = stop_time, 1 = step_up, 2 = step_down */
819 /* Automatic fan speed control registers */
824 u8 pwm_weight_temp_sel[5];
825 u8 weight_temp[3][5]; /* 0->temp_step, 1->temp_step_tol,
838 /* Remember extra register values over suspend/resume */
845 struct nct6775_sio_data {
850 struct sensor_device_template {
851 struct device_attribute dev_attr;
859 bool s2; /* true if both index and nr are used */
862 struct sensor_device_attr_u {
864 struct sensor_device_attribute a1;
865 struct sensor_device_attribute_2 a2;
870 #define __TEMPLATE_ATTR(_template, _mode, _show, _store) { \
871 .attr = {.name = _template, .mode = _mode }, \
876 #define SENSOR_DEVICE_TEMPLATE(_template, _mode, _show, _store, _index) \
877 { .dev_attr = __TEMPLATE_ATTR(_template, _mode, _show, _store), \
881 #define SENSOR_DEVICE_TEMPLATE_2(_template, _mode, _show, _store, \
883 { .dev_attr = __TEMPLATE_ATTR(_template, _mode, _show, _store), \
884 .u.s.index = _index, \
888 #define SENSOR_TEMPLATE(_name, _template, _mode, _show, _store, _index) \
889 static struct sensor_device_template sensor_dev_template_##_name \
890 = SENSOR_DEVICE_TEMPLATE(_template, _mode, _show, _store, \
893 #define SENSOR_TEMPLATE_2(_name, _template, _mode, _show, _store, \
895 static struct sensor_device_template sensor_dev_template_##_name \
896 = SENSOR_DEVICE_TEMPLATE_2(_template, _mode, _show, _store, \
899 struct sensor_template_group {
900 struct sensor_device_template **templates;
901 umode_t (*is_visible)(struct kobject *, struct attribute *, int);
905 static struct attribute_group *
906 nct6775_create_attr_group(struct device *dev, struct sensor_template_group *tg,
909 struct attribute_group *group;
910 struct sensor_device_attr_u *su;
911 struct sensor_device_attribute *a;
912 struct sensor_device_attribute_2 *a2;
913 struct attribute **attrs;
914 struct sensor_device_template **t;
915 int err, i, j, count;
918 return ERR_PTR(-EINVAL);
921 for (count = 0; *t; t++, count++)
925 return ERR_PTR(-EINVAL);
927 group = devm_kzalloc(dev, sizeof(*group), GFP_KERNEL);
929 return ERR_PTR(-ENOMEM);
931 attrs = devm_kzalloc(dev, sizeof(*attrs) * (repeat * count + 1),
934 return ERR_PTR(-ENOMEM);
936 su = devm_kzalloc(dev, sizeof(*su) * repeat * count,
939 return ERR_PTR(-ENOMEM);
941 group->attrs = attrs;
942 group->is_visible = tg->is_visible;
944 for (i = 0; i < repeat; i++) {
946 for (j = 0; *t != NULL; j++) {
947 snprintf(su->name, sizeof(su->name),
948 (*t)->dev_attr.attr.name, tg->base + i);
951 a2->dev_attr.attr.name = su->name;
952 a2->nr = (*t)->u.s.nr + i;
953 a2->index = (*t)->u.s.index;
954 a2->dev_attr.attr.mode =
955 (*t)->dev_attr.attr.mode;
956 a2->dev_attr.show = (*t)->dev_attr.show;
957 a2->dev_attr.store = (*t)->dev_attr.store;
958 *attrs = &a2->dev_attr.attr;
961 a->dev_attr.attr.name = su->name;
962 a->index = (*t)->u.index + i;
963 a->dev_attr.attr.mode =
964 (*t)->dev_attr.attr.mode;
965 a->dev_attr.show = (*t)->dev_attr.show;
966 a->dev_attr.store = (*t)->dev_attr.store;
967 *attrs = &a->dev_attr.attr;
975 err = sysfs_create_group(&dev->kobj, group);
977 return ERR_PTR(-ENOMEM);
982 static bool is_word_sized(struct nct6775_data *data, u16 reg)
984 switch (data->kind) {
986 return reg == 0x20 || reg == 0x22 || reg == 0x24 ||
987 reg == 0xe0 || reg == 0xe2 || reg == 0xe4 ||
988 reg == 0x111 || reg == 0x121 || reg == 0x131;
990 return (((reg & 0xff00) == 0x100 ||
991 (reg & 0xff00) == 0x200) &&
992 ((reg & 0x00ff) == 0x50 ||
993 (reg & 0x00ff) == 0x53 ||
994 (reg & 0x00ff) == 0x55)) ||
995 (reg & 0xfff0) == 0x630 ||
996 reg == 0x640 || reg == 0x642 ||
998 ((reg & 0xfff0) == 0x650 && (reg & 0x000f) >= 0x06) ||
999 reg == 0x73 || reg == 0x75 || reg == 0x77;
1001 return (((reg & 0xff00) == 0x100 ||
1002 (reg & 0xff00) == 0x200) &&
1003 ((reg & 0x00ff) == 0x50 ||
1004 (reg & 0x00ff) == 0x53 ||
1005 (reg & 0x00ff) == 0x55)) ||
1006 (reg & 0xfff0) == 0x630 ||
1008 reg == 0x640 || reg == 0x642 ||
1009 ((reg & 0xfff0) == 0x650 && (reg & 0x000f) >= 0x06) ||
1010 reg == 0x73 || reg == 0x75 || reg == 0x77;
1012 return reg == 0x150 || reg == 0x153 || reg == 0x155 ||
1013 ((reg & 0xfff0) == 0x4b0 && (reg & 0x000f) < 0x09) ||
1015 reg == 0x63a || reg == 0x63c || reg == 0x63e ||
1016 reg == 0x640 || reg == 0x642 ||
1017 reg == 0x73 || reg == 0x75 || reg == 0x77 || reg == 0x79 ||
1024 * On older chips, only registers 0x50-0x5f are banked.
1025 * On more recent chips, all registers are banked.
1026 * Assume that is the case and set the bank number for each access.
1027 * Cache the bank number so it only needs to be set if it changes.
1029 static inline void nct6775_set_bank(struct nct6775_data *data, u16 reg)
1032 if (data->bank != bank) {
1033 outb_p(NCT6775_REG_BANK, data->addr + ADDR_REG_OFFSET);
1034 outb_p(bank, data->addr + DATA_REG_OFFSET);
1039 static u16 nct6775_read_value(struct nct6775_data *data, u16 reg)
1041 int res, word_sized = is_word_sized(data, reg);
1043 nct6775_set_bank(data, reg);
1044 outb_p(reg & 0xff, data->addr + ADDR_REG_OFFSET);
1045 res = inb_p(data->addr + DATA_REG_OFFSET);
1047 outb_p((reg & 0xff) + 1,
1048 data->addr + ADDR_REG_OFFSET);
1049 res = (res << 8) + inb_p(data->addr + DATA_REG_OFFSET);
1054 static int nct6775_write_value(struct nct6775_data *data, u16 reg, u16 value)
1056 int word_sized = is_word_sized(data, reg);
1058 nct6775_set_bank(data, reg);
1059 outb_p(reg & 0xff, data->addr + ADDR_REG_OFFSET);
1061 outb_p(value >> 8, data->addr + DATA_REG_OFFSET);
1062 outb_p((reg & 0xff) + 1,
1063 data->addr + ADDR_REG_OFFSET);
1065 outb_p(value & 0xff, data->addr + DATA_REG_OFFSET);
1069 /* We left-align 8-bit temperature values to make the code simpler */
1070 static u16 nct6775_read_temp(struct nct6775_data *data, u16 reg)
1074 res = nct6775_read_value(data, reg);
1075 if (!is_word_sized(data, reg))
1081 static int nct6775_write_temp(struct nct6775_data *data, u16 reg, u16 value)
1083 if (!is_word_sized(data, reg))
1085 return nct6775_write_value(data, reg, value);
1088 /* This function assumes that the caller holds data->update_lock */
1089 static void nct6775_write_fan_div(struct nct6775_data *data, int nr)
1095 reg = (nct6775_read_value(data, NCT6775_REG_FANDIV1) & 0x70)
1096 | (data->fan_div[0] & 0x7);
1097 nct6775_write_value(data, NCT6775_REG_FANDIV1, reg);
1100 reg = (nct6775_read_value(data, NCT6775_REG_FANDIV1) & 0x7)
1101 | ((data->fan_div[1] << 4) & 0x70);
1102 nct6775_write_value(data, NCT6775_REG_FANDIV1, reg);
1105 reg = (nct6775_read_value(data, NCT6775_REG_FANDIV2) & 0x70)
1106 | (data->fan_div[2] & 0x7);
1107 nct6775_write_value(data, NCT6775_REG_FANDIV2, reg);
1110 reg = (nct6775_read_value(data, NCT6775_REG_FANDIV2) & 0x7)
1111 | ((data->fan_div[3] << 4) & 0x70);
1112 nct6775_write_value(data, NCT6775_REG_FANDIV2, reg);
1117 static void nct6775_write_fan_div_common(struct nct6775_data *data, int nr)
1119 if (data->kind == nct6775)
1120 nct6775_write_fan_div(data, nr);
1123 static void nct6775_update_fan_div(struct nct6775_data *data)
1127 i = nct6775_read_value(data, NCT6775_REG_FANDIV1);
1128 data->fan_div[0] = i & 0x7;
1129 data->fan_div[1] = (i & 0x70) >> 4;
1130 i = nct6775_read_value(data, NCT6775_REG_FANDIV2);
1131 data->fan_div[2] = i & 0x7;
1132 if (data->has_fan & (1 << 3))
1133 data->fan_div[3] = (i & 0x70) >> 4;
1136 static void nct6775_update_fan_div_common(struct nct6775_data *data)
1138 if (data->kind == nct6775)
1139 nct6775_update_fan_div(data);
1142 static void nct6775_init_fan_div(struct nct6775_data *data)
1146 nct6775_update_fan_div_common(data);
1148 * For all fans, start with highest divider value if the divider
1149 * register is not initialized. This ensures that we get a
1150 * reading from the fan count register, even if it is not optimal.
1151 * We'll compute a better divider later on.
1153 for (i = 0; i < ARRAY_SIZE(data->fan_div); i++) {
1154 if (!(data->has_fan & (1 << i)))
1156 if (data->fan_div[i] == 0) {
1157 data->fan_div[i] = 7;
1158 nct6775_write_fan_div_common(data, i);
1163 static void nct6775_init_fan_common(struct device *dev,
1164 struct nct6775_data *data)
1169 if (data->has_fan_div)
1170 nct6775_init_fan_div(data);
1173 * If fan_min is not set (0), set it to 0xff to disable it. This
1174 * prevents the unnecessary warning when fanX_min is reported as 0.
1176 for (i = 0; i < ARRAY_SIZE(data->fan_min); i++) {
1177 if (data->has_fan_min & (1 << i)) {
1178 reg = nct6775_read_value(data, data->REG_FAN_MIN[i]);
1180 nct6775_write_value(data, data->REG_FAN_MIN[i],
1181 data->has_fan_div ? 0xff
1187 static void nct6775_select_fan_div(struct device *dev,
1188 struct nct6775_data *data, int nr, u16 reg)
1190 u8 fan_div = data->fan_div[nr];
1193 if (!data->has_fan_div)
1197 * If we failed to measure the fan speed, or the reported value is not
1198 * in the optimal range, and the clock divider can be modified,
1199 * let's try that for next time.
1201 if (reg == 0x00 && fan_div < 0x07)
1203 else if (reg != 0x00 && reg < 0x30 && fan_div > 0)
1206 if (fan_div != data->fan_div[nr]) {
1207 dev_dbg(dev, "Modifying fan%d clock divider from %u to %u\n",
1208 nr + 1, div_from_reg(data->fan_div[nr]),
1209 div_from_reg(fan_div));
1211 /* Preserve min limit if possible */
1212 if (data->has_fan_min & (1 << nr)) {
1213 fan_min = data->fan_min[nr];
1214 if (fan_div > data->fan_div[nr]) {
1215 if (fan_min != 255 && fan_min > 1)
1218 if (fan_min != 255) {
1224 if (fan_min != data->fan_min[nr]) {
1225 data->fan_min[nr] = fan_min;
1226 nct6775_write_value(data, data->REG_FAN_MIN[nr],
1230 data->fan_div[nr] = fan_div;
1231 nct6775_write_fan_div_common(data, nr);
1235 static void nct6775_update_pwm(struct device *dev)
1237 struct nct6775_data *data = dev_get_drvdata(dev);
1239 int fanmodecfg, reg;
1242 for (i = 0; i < data->pwm_num; i++) {
1243 if (!(data->has_pwm & (1 << i)))
1246 duty_is_dc = data->REG_PWM_MODE[i] &&
1247 (nct6775_read_value(data, data->REG_PWM_MODE[i])
1248 & data->PWM_MODE_MASK[i]);
1249 data->pwm_mode[i] = duty_is_dc;
1251 fanmodecfg = nct6775_read_value(data, data->REG_FAN_MODE[i]);
1252 for (j = 0; j < ARRAY_SIZE(data->REG_PWM); j++) {
1253 if (data->REG_PWM[j] && data->REG_PWM[j][i]) {
1255 = nct6775_read_value(data,
1256 data->REG_PWM[j][i]);
1260 data->pwm_enable[i] = reg_to_pwm_enable(data->pwm[0][i],
1261 (fanmodecfg >> 4) & 7);
1263 if (!data->temp_tolerance[0][i] ||
1264 data->pwm_enable[i] != speed_cruise)
1265 data->temp_tolerance[0][i] = fanmodecfg & 0x0f;
1266 if (!data->target_speed_tolerance[i] ||
1267 data->pwm_enable[i] == speed_cruise) {
1268 u8 t = fanmodecfg & 0x0f;
1269 if (data->REG_TOLERANCE_H) {
1270 t |= (nct6775_read_value(data,
1271 data->REG_TOLERANCE_H[i]) & 0x70) >> 1;
1273 data->target_speed_tolerance[i] = t;
1276 data->temp_tolerance[1][i] =
1277 nct6775_read_value(data,
1278 data->REG_CRITICAL_TEMP_TOLERANCE[i]);
1280 reg = nct6775_read_value(data, data->REG_TEMP_SEL[i]);
1281 data->pwm_temp_sel[i] = reg & 0x1f;
1282 /* If fan can stop, report floor as 0 */
1284 data->pwm[2][i] = 0;
1286 reg = nct6775_read_value(data, data->REG_WEIGHT_TEMP_SEL[i]);
1287 data->pwm_weight_temp_sel[i] = reg & 0x1f;
1288 /* If weight is disabled, report weight source as 0 */
1289 if (j == 1 && !(reg & 0x80))
1290 data->pwm_weight_temp_sel[i] = 0;
1292 /* Weight temp data */
1293 for (j = 0; j < ARRAY_SIZE(data->weight_temp); j++) {
1294 data->weight_temp[j][i]
1295 = nct6775_read_value(data,
1296 data->REG_WEIGHT_TEMP[j][i]);
1301 static void nct6775_update_pwm_limits(struct device *dev)
1303 struct nct6775_data *data = dev_get_drvdata(dev);
1308 for (i = 0; i < data->pwm_num; i++) {
1309 if (!(data->has_pwm & (1 << i)))
1312 for (j = 0; j < ARRAY_SIZE(data->fan_time); j++) {
1313 data->fan_time[j][i] =
1314 nct6775_read_value(data, data->REG_FAN_TIME[j][i]);
1317 reg_t = nct6775_read_value(data, data->REG_TARGET[i]);
1318 /* Update only in matching mode or if never updated */
1319 if (!data->target_temp[i] ||
1320 data->pwm_enable[i] == thermal_cruise)
1321 data->target_temp[i] = reg_t & data->target_temp_mask;
1322 if (!data->target_speed[i] ||
1323 data->pwm_enable[i] == speed_cruise) {
1324 if (data->REG_TOLERANCE_H) {
1325 reg_t |= (nct6775_read_value(data,
1326 data->REG_TOLERANCE_H[i]) & 0x0f) << 8;
1328 data->target_speed[i] = reg_t;
1331 for (j = 0; j < data->auto_pwm_num; j++) {
1332 data->auto_pwm[i][j] =
1333 nct6775_read_value(data,
1334 NCT6775_AUTO_PWM(data, i, j));
1335 data->auto_temp[i][j] =
1336 nct6775_read_value(data,
1337 NCT6775_AUTO_TEMP(data, i, j));
1340 /* critical auto_pwm temperature data */
1341 data->auto_temp[i][data->auto_pwm_num] =
1342 nct6775_read_value(data, data->REG_CRITICAL_TEMP[i]);
1344 switch (data->kind) {
1346 reg = nct6775_read_value(data,
1347 NCT6775_REG_CRITICAL_ENAB[i]);
1348 data->auto_pwm[i][data->auto_pwm_num] =
1349 (reg & 0x02) ? 0xff : 0x00;
1352 data->auto_pwm[i][data->auto_pwm_num] = 0xff;
1356 reg = nct6775_read_value(data,
1357 data->REG_CRITICAL_PWM_ENABLE[i]);
1358 if (reg & data->CRITICAL_PWM_ENABLE_MASK)
1359 reg = nct6775_read_value(data,
1360 data->REG_CRITICAL_PWM[i]);
1363 data->auto_pwm[i][data->auto_pwm_num] = reg;
1369 static struct nct6775_data *nct6775_update_device(struct device *dev)
1371 struct nct6775_data *data = dev_get_drvdata(dev);
1374 mutex_lock(&data->update_lock);
1376 if (time_after(jiffies, data->last_updated + HZ + HZ / 2)
1378 /* Fan clock dividers */
1379 nct6775_update_fan_div_common(data);
1381 /* Measured voltages and limits */
1382 for (i = 0; i < data->in_num; i++) {
1383 if (!(data->have_in & (1 << i)))
1386 data->in[i][0] = nct6775_read_value(data,
1388 data->in[i][1] = nct6775_read_value(data,
1389 data->REG_IN_MINMAX[0][i]);
1390 data->in[i][2] = nct6775_read_value(data,
1391 data->REG_IN_MINMAX[1][i]);
1394 /* Measured fan speeds and limits */
1395 for (i = 0; i < ARRAY_SIZE(data->rpm); i++) {
1398 if (!(data->has_fan & (1 << i)))
1401 reg = nct6775_read_value(data, data->REG_FAN[i]);
1402 data->rpm[i] = data->fan_from_reg(reg,
1405 if (data->has_fan_min & (1 << i))
1406 data->fan_min[i] = nct6775_read_value(data,
1407 data->REG_FAN_MIN[i]);
1408 data->fan_pulses[i] =
1409 (nct6775_read_value(data, data->REG_FAN_PULSES[i])
1410 >> data->FAN_PULSE_SHIFT[i]) & 0x03;
1412 nct6775_select_fan_div(dev, data, i, reg);
1415 nct6775_update_pwm(dev);
1416 nct6775_update_pwm_limits(dev);
1418 /* Measured temperatures and limits */
1419 for (i = 0; i < NUM_TEMP; i++) {
1420 if (!(data->have_temp & (1 << i)))
1422 for (j = 0; j < ARRAY_SIZE(data->reg_temp); j++) {
1423 if (data->reg_temp[j][i])
1425 = nct6775_read_temp(data,
1426 data->reg_temp[j][i]);
1428 if (!(data->have_temp_fixed & (1 << i)))
1430 data->temp_offset[i]
1431 = nct6775_read_value(data, data->REG_TEMP_OFFSET[i]);
1435 for (i = 0; i < NUM_REG_ALARM; i++) {
1437 if (!data->REG_ALARM[i])
1439 alarm = nct6775_read_value(data, data->REG_ALARM[i]);
1440 data->alarms |= ((u64)alarm) << (i << 3);
1444 for (i = 0; i < NUM_REG_BEEP; i++) {
1446 if (!data->REG_BEEP[i])
1448 beep = nct6775_read_value(data, data->REG_BEEP[i]);
1449 data->beeps |= ((u64)beep) << (i << 3);
1452 data->last_updated = jiffies;
1456 mutex_unlock(&data->update_lock);
1461 * Sysfs callback functions
1464 show_in_reg(struct device *dev, struct device_attribute *attr, char *buf)
1466 struct nct6775_data *data = nct6775_update_device(dev);
1467 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
1469 int index = sattr->index;
1470 return sprintf(buf, "%ld\n", in_from_reg(data->in[nr][index], nr));
1474 store_in_reg(struct device *dev, struct device_attribute *attr, const char *buf,
1477 struct nct6775_data *data = dev_get_drvdata(dev);
1478 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
1480 int index = sattr->index;
1482 int err = kstrtoul(buf, 10, &val);
1485 mutex_lock(&data->update_lock);
1486 data->in[nr][index] = in_to_reg(val, nr);
1487 nct6775_write_value(data, data->REG_IN_MINMAX[index - 1][nr],
1488 data->in[nr][index]);
1489 mutex_unlock(&data->update_lock);
1494 show_alarm(struct device *dev, struct device_attribute *attr, char *buf)
1496 struct nct6775_data *data = nct6775_update_device(dev);
1497 struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr);
1498 int nr = data->ALARM_BITS[sattr->index];
1499 return sprintf(buf, "%u\n",
1500 (unsigned int)((data->alarms >> nr) & 0x01));
1503 static int find_temp_source(struct nct6775_data *data, int index, int count)
1505 int source = data->temp_src[index];
1508 for (nr = 0; nr < count; nr++) {
1511 src = nct6775_read_value(data,
1512 data->REG_TEMP_SOURCE[nr]) & 0x1f;
1520 show_temp_alarm(struct device *dev, struct device_attribute *attr, char *buf)
1522 struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr);
1523 struct nct6775_data *data = nct6775_update_device(dev);
1524 unsigned int alarm = 0;
1528 * For temperatures, there is no fixed mapping from registers to alarm
1529 * bits. Alarm bits are determined by the temperature source mapping.
1531 nr = find_temp_source(data, sattr->index, data->num_temp_alarms);
1533 int bit = data->ALARM_BITS[nr + TEMP_ALARM_BASE];
1534 alarm = (data->alarms >> bit) & 0x01;
1536 return sprintf(buf, "%u\n", alarm);
1540 show_beep(struct device *dev, struct device_attribute *attr, char *buf)
1542 struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr);
1543 struct nct6775_data *data = nct6775_update_device(dev);
1544 int nr = data->BEEP_BITS[sattr->index];
1546 return sprintf(buf, "%u\n",
1547 (unsigned int)((data->beeps >> nr) & 0x01));
1551 store_beep(struct device *dev, struct device_attribute *attr, const char *buf,
1554 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
1555 struct nct6775_data *data = dev_get_drvdata(dev);
1556 int nr = data->BEEP_BITS[sattr->index];
1557 int regindex = nr >> 3;
1560 int err = kstrtoul(buf, 10, &val);
1566 mutex_lock(&data->update_lock);
1568 data->beeps |= (1ULL << nr);
1570 data->beeps &= ~(1ULL << nr);
1571 nct6775_write_value(data, data->REG_BEEP[regindex],
1572 (data->beeps >> (regindex << 3)) & 0xff);
1573 mutex_unlock(&data->update_lock);
1578 show_temp_beep(struct device *dev, struct device_attribute *attr, char *buf)
1580 struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr);
1581 struct nct6775_data *data = nct6775_update_device(dev);
1582 unsigned int beep = 0;
1586 * For temperatures, there is no fixed mapping from registers to beep
1587 * enable bits. Beep enable bits are determined by the temperature
1590 nr = find_temp_source(data, sattr->index, data->num_temp_beeps);
1592 int bit = data->BEEP_BITS[nr + TEMP_ALARM_BASE];
1593 beep = (data->beeps >> bit) & 0x01;
1595 return sprintf(buf, "%u\n", beep);
1599 store_temp_beep(struct device *dev, struct device_attribute *attr,
1600 const char *buf, size_t count)
1602 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
1603 struct nct6775_data *data = dev_get_drvdata(dev);
1604 int nr, bit, regindex;
1607 int err = kstrtoul(buf, 10, &val);
1613 nr = find_temp_source(data, sattr->index, data->num_temp_beeps);
1617 bit = data->BEEP_BITS[nr + TEMP_ALARM_BASE];
1618 regindex = bit >> 3;
1620 mutex_lock(&data->update_lock);
1622 data->beeps |= (1ULL << bit);
1624 data->beeps &= ~(1ULL << bit);
1625 nct6775_write_value(data, data->REG_BEEP[regindex],
1626 (data->beeps >> (regindex << 3)) & 0xff);
1627 mutex_unlock(&data->update_lock);
1632 static umode_t nct6775_in_is_visible(struct kobject *kobj,
1633 struct attribute *attr, int index)
1635 struct device *dev = container_of(kobj, struct device, kobj);
1636 struct nct6775_data *data = dev_get_drvdata(dev);
1637 int in = index / 5; /* voltage index */
1639 if (!(data->have_in & (1 << in)))
1645 SENSOR_TEMPLATE_2(in_input, "in%d_input", S_IRUGO, show_in_reg, NULL, 0, 0);
1646 SENSOR_TEMPLATE(in_alarm, "in%d_alarm", S_IRUGO, show_alarm, NULL, 0);
1647 SENSOR_TEMPLATE(in_beep, "in%d_beep", S_IWUSR | S_IRUGO, show_beep, store_beep,
1649 SENSOR_TEMPLATE_2(in_min, "in%d_min", S_IWUSR | S_IRUGO, show_in_reg,
1650 store_in_reg, 0, 1);
1651 SENSOR_TEMPLATE_2(in_max, "in%d_max", S_IWUSR | S_IRUGO, show_in_reg,
1652 store_in_reg, 0, 2);
1655 * nct6775_in_is_visible uses the index into the following array
1656 * to determine if attributes should be created or not.
1657 * Any change in order or content must be matched.
1659 static struct sensor_device_template *nct6775_attributes_in_template[] = {
1660 &sensor_dev_template_in_input,
1661 &sensor_dev_template_in_alarm,
1662 &sensor_dev_template_in_beep,
1663 &sensor_dev_template_in_min,
1664 &sensor_dev_template_in_max,
1668 static struct sensor_template_group nct6775_in_template_group = {
1669 .templates = nct6775_attributes_in_template,
1670 .is_visible = nct6775_in_is_visible,
1674 show_fan(struct device *dev, struct device_attribute *attr, char *buf)
1676 struct nct6775_data *data = nct6775_update_device(dev);
1677 struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr);
1678 int nr = sattr->index;
1679 return sprintf(buf, "%d\n", data->rpm[nr]);
1683 show_fan_min(struct device *dev, struct device_attribute *attr, char *buf)
1685 struct nct6775_data *data = nct6775_update_device(dev);
1686 struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr);
1687 int nr = sattr->index;
1688 return sprintf(buf, "%d\n",
1689 data->fan_from_reg_min(data->fan_min[nr],
1690 data->fan_div[nr]));
1694 show_fan_div(struct device *dev, struct device_attribute *attr, char *buf)
1696 struct nct6775_data *data = nct6775_update_device(dev);
1697 struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr);
1698 int nr = sattr->index;
1699 return sprintf(buf, "%u\n", div_from_reg(data->fan_div[nr]));
1703 store_fan_min(struct device *dev, struct device_attribute *attr,
1704 const char *buf, size_t count)
1706 struct nct6775_data *data = dev_get_drvdata(dev);
1707 struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr);
1708 int nr = sattr->index;
1714 err = kstrtoul(buf, 10, &val);
1718 mutex_lock(&data->update_lock);
1719 if (!data->has_fan_div) {
1720 /* NCT6776F or NCT6779D; we know this is a 13 bit register */
1726 val = 1350000U / val;
1727 val = (val & 0x1f) | ((val << 3) & 0xff00);
1729 data->fan_min[nr] = val;
1730 goto write_min; /* Leave fan divider alone */
1733 /* No min limit, alarm disabled */
1734 data->fan_min[nr] = 255;
1735 new_div = data->fan_div[nr]; /* No change */
1736 dev_info(dev, "fan%u low limit and alarm disabled\n", nr + 1);
1739 reg = 1350000U / val;
1740 if (reg >= 128 * 255) {
1742 * Speed below this value cannot possibly be represented,
1743 * even with the highest divider (128)
1745 data->fan_min[nr] = 254;
1746 new_div = 7; /* 128 == (1 << 7) */
1748 "fan%u low limit %lu below minimum %u, set to minimum\n",
1749 nr + 1, val, data->fan_from_reg_min(254, 7));
1752 * Speed above this value cannot possibly be represented,
1753 * even with the lowest divider (1)
1755 data->fan_min[nr] = 1;
1756 new_div = 0; /* 1 == (1 << 0) */
1758 "fan%u low limit %lu above maximum %u, set to maximum\n",
1759 nr + 1, val, data->fan_from_reg_min(1, 0));
1762 * Automatically pick the best divider, i.e. the one such
1763 * that the min limit will correspond to a register value
1764 * in the 96..192 range
1767 while (reg > 192 && new_div < 7) {
1771 data->fan_min[nr] = reg;
1776 * Write both the fan clock divider (if it changed) and the new
1777 * fan min (unconditionally)
1779 if (new_div != data->fan_div[nr]) {
1780 dev_dbg(dev, "fan%u clock divider changed from %u to %u\n",
1781 nr + 1, div_from_reg(data->fan_div[nr]),
1782 div_from_reg(new_div));
1783 data->fan_div[nr] = new_div;
1784 nct6775_write_fan_div_common(data, nr);
1785 /* Give the chip time to sample a new speed value */
1786 data->last_updated = jiffies;
1790 nct6775_write_value(data, data->REG_FAN_MIN[nr], data->fan_min[nr]);
1791 mutex_unlock(&data->update_lock);
1797 show_fan_pulses(struct device *dev, struct device_attribute *attr, char *buf)
1799 struct nct6775_data *data = nct6775_update_device(dev);
1800 struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr);
1801 int p = data->fan_pulses[sattr->index];
1803 return sprintf(buf, "%d\n", p ? : 4);
1807 store_fan_pulses(struct device *dev, struct device_attribute *attr,
1808 const char *buf, size_t count)
1810 struct nct6775_data *data = dev_get_drvdata(dev);
1811 struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr);
1812 int nr = sattr->index;
1817 err = kstrtoul(buf, 10, &val);
1824 mutex_lock(&data->update_lock);
1825 data->fan_pulses[nr] = val & 3;
1826 reg = nct6775_read_value(data, data->REG_FAN_PULSES[nr]);
1827 reg &= ~(0x03 << data->FAN_PULSE_SHIFT[nr]);
1828 reg |= (val & 3) << data->FAN_PULSE_SHIFT[nr];
1829 nct6775_write_value(data, data->REG_FAN_PULSES[nr], reg);
1830 mutex_unlock(&data->update_lock);
1835 static umode_t nct6775_fan_is_visible(struct kobject *kobj,
1836 struct attribute *attr, int index)
1838 struct device *dev = container_of(kobj, struct device, kobj);
1839 struct nct6775_data *data = dev_get_drvdata(dev);
1840 int fan = index / 6; /* fan index */
1841 int nr = index % 6; /* attribute index */
1843 if (!(data->has_fan & (1 << fan)))
1846 if (nr == 1 && data->ALARM_BITS[FAN_ALARM_BASE + fan] == -1)
1848 if (nr == 2 && data->BEEP_BITS[FAN_ALARM_BASE + fan] == -1)
1850 if (nr == 4 && !(data->has_fan_min & (1 << fan)))
1852 if (nr == 5 && data->kind != nct6775)
1858 SENSOR_TEMPLATE(fan_input, "fan%d_input", S_IRUGO, show_fan, NULL, 0);
1859 SENSOR_TEMPLATE(fan_alarm, "fan%d_alarm", S_IRUGO, show_alarm, NULL,
1861 SENSOR_TEMPLATE(fan_beep, "fan%d_beep", S_IWUSR | S_IRUGO, show_beep,
1862 store_beep, FAN_ALARM_BASE);
1863 SENSOR_TEMPLATE(fan_pulses, "fan%d_pulses", S_IWUSR | S_IRUGO, show_fan_pulses,
1864 store_fan_pulses, 0);
1865 SENSOR_TEMPLATE(fan_min, "fan%d_min", S_IWUSR | S_IRUGO, show_fan_min,
1867 SENSOR_TEMPLATE(fan_div, "fan%d_div", S_IRUGO, show_fan_div, NULL, 0);
1870 * nct6775_fan_is_visible uses the index into the following array
1871 * to determine if attributes should be created or not.
1872 * Any change in order or content must be matched.
1874 static struct sensor_device_template *nct6775_attributes_fan_template[] = {
1875 &sensor_dev_template_fan_input,
1876 &sensor_dev_template_fan_alarm, /* 1 */
1877 &sensor_dev_template_fan_beep, /* 2 */
1878 &sensor_dev_template_fan_pulses,
1879 &sensor_dev_template_fan_min, /* 4 */
1880 &sensor_dev_template_fan_div, /* 5 */
1884 static struct sensor_template_group nct6775_fan_template_group = {
1885 .templates = nct6775_attributes_fan_template,
1886 .is_visible = nct6775_fan_is_visible,
1891 show_temp_label(struct device *dev, struct device_attribute *attr, char *buf)
1893 struct nct6775_data *data = nct6775_update_device(dev);
1894 struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr);
1895 int nr = sattr->index;
1896 return sprintf(buf, "%s\n", data->temp_label[data->temp_src[nr]]);
1900 show_temp(struct device *dev, struct device_attribute *attr, char *buf)
1902 struct nct6775_data *data = nct6775_update_device(dev);
1903 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
1905 int index = sattr->index;
1907 return sprintf(buf, "%d\n", LM75_TEMP_FROM_REG(data->temp[index][nr]));
1911 store_temp(struct device *dev, struct device_attribute *attr, const char *buf,
1914 struct nct6775_data *data = dev_get_drvdata(dev);
1915 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
1917 int index = sattr->index;
1921 err = kstrtol(buf, 10, &val);
1925 mutex_lock(&data->update_lock);
1926 data->temp[index][nr] = LM75_TEMP_TO_REG(val);
1927 nct6775_write_temp(data, data->reg_temp[index][nr],
1928 data->temp[index][nr]);
1929 mutex_unlock(&data->update_lock);
1934 show_temp_offset(struct device *dev, struct device_attribute *attr, char *buf)
1936 struct nct6775_data *data = nct6775_update_device(dev);
1937 struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr);
1939 return sprintf(buf, "%d\n", data->temp_offset[sattr->index] * 1000);
1943 store_temp_offset(struct device *dev, struct device_attribute *attr,
1944 const char *buf, size_t count)
1946 struct nct6775_data *data = dev_get_drvdata(dev);
1947 struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr);
1948 int nr = sattr->index;
1952 err = kstrtol(buf, 10, &val);
1956 val = clamp_val(DIV_ROUND_CLOSEST(val, 1000), -128, 127);
1958 mutex_lock(&data->update_lock);
1959 data->temp_offset[nr] = val;
1960 nct6775_write_value(data, data->REG_TEMP_OFFSET[nr], val);
1961 mutex_unlock(&data->update_lock);
1967 show_temp_type(struct device *dev, struct device_attribute *attr, char *buf)
1969 struct nct6775_data *data = nct6775_update_device(dev);
1970 struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr);
1971 int nr = sattr->index;
1972 return sprintf(buf, "%d\n", (int)data->temp_type[nr]);
1976 store_temp_type(struct device *dev, struct device_attribute *attr,
1977 const char *buf, size_t count)
1979 struct nct6775_data *data = nct6775_update_device(dev);
1980 struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr);
1981 int nr = sattr->index;
1984 u8 vbat, diode, vbit, dbit;
1986 err = kstrtoul(buf, 10, &val);
1990 if (val != 1 && val != 3 && val != 4)
1993 mutex_lock(&data->update_lock);
1995 data->temp_type[nr] = val;
1997 dbit = data->DIODE_MASK << nr;
1998 vbat = nct6775_read_value(data, data->REG_VBAT) & ~vbit;
1999 diode = nct6775_read_value(data, data->REG_DIODE) & ~dbit;
2001 case 1: /* CPU diode (diode, current mode) */
2005 case 3: /* diode, voltage mode */
2008 case 4: /* thermistor */
2011 nct6775_write_value(data, data->REG_VBAT, vbat);
2012 nct6775_write_value(data, data->REG_DIODE, diode);
2014 mutex_unlock(&data->update_lock);
2018 static umode_t nct6775_temp_is_visible(struct kobject *kobj,
2019 struct attribute *attr, int index)
2021 struct device *dev = container_of(kobj, struct device, kobj);
2022 struct nct6775_data *data = dev_get_drvdata(dev);
2023 int temp = index / 10; /* temp index */
2024 int nr = index % 10; /* attribute index */
2026 if (!(data->have_temp & (1 << temp)))
2029 if (nr == 2 && find_temp_source(data, temp, data->num_temp_alarms) < 0)
2030 return 0; /* alarm */
2032 if (nr == 3 && find_temp_source(data, temp, data->num_temp_beeps) < 0)
2033 return 0; /* beep */
2035 if (nr == 4 && !data->reg_temp[1][temp]) /* max */
2038 if (nr == 5 && !data->reg_temp[2][temp]) /* max_hyst */
2041 if (nr == 6 && !data->reg_temp[3][temp]) /* crit */
2044 if (nr == 7 && !data->reg_temp[4][temp]) /* lcrit */
2047 /* offset and type only apply to fixed sensors */
2048 if (nr > 7 && !(data->have_temp_fixed & (1 << temp)))
2054 SENSOR_TEMPLATE_2(temp_input, "temp%d_input", S_IRUGO, show_temp, NULL, 0, 0);
2055 SENSOR_TEMPLATE(temp_label, "temp%d_label", S_IRUGO, show_temp_label, NULL, 0);
2056 SENSOR_TEMPLATE_2(temp_max, "temp%d_max", S_IRUGO | S_IWUSR, show_temp,
2058 SENSOR_TEMPLATE_2(temp_max_hyst, "temp%d_max_hyst", S_IRUGO | S_IWUSR,
2059 show_temp, store_temp, 0, 2);
2060 SENSOR_TEMPLATE_2(temp_crit, "temp%d_crit", S_IRUGO | S_IWUSR, show_temp,
2062 SENSOR_TEMPLATE_2(temp_lcrit, "temp%d_lcrit", S_IRUGO | S_IWUSR, show_temp,
2064 SENSOR_TEMPLATE(temp_offset, "temp%d_offset", S_IRUGO | S_IWUSR,
2065 show_temp_offset, store_temp_offset, 0);
2066 SENSOR_TEMPLATE(temp_type, "temp%d_type", S_IRUGO | S_IWUSR, show_temp_type,
2067 store_temp_type, 0);
2068 SENSOR_TEMPLATE(temp_alarm, "temp%d_alarm", S_IRUGO, show_temp_alarm, NULL, 0);
2069 SENSOR_TEMPLATE(temp_beep, "temp%d_beep", S_IRUGO | S_IWUSR, show_temp_beep,
2070 store_temp_beep, 0);
2073 * nct6775_temp_is_visible uses the index into the following array
2074 * to determine if attributes should be created or not.
2075 * Any change in order or content must be matched.
2077 static struct sensor_device_template *nct6775_attributes_temp_template[] = {
2078 &sensor_dev_template_temp_input,
2079 &sensor_dev_template_temp_label,
2080 &sensor_dev_template_temp_alarm, /* 2 */
2081 &sensor_dev_template_temp_beep, /* 3 */
2082 &sensor_dev_template_temp_max, /* 4 */
2083 &sensor_dev_template_temp_max_hyst, /* 5 */
2084 &sensor_dev_template_temp_crit, /* 6 */
2085 &sensor_dev_template_temp_lcrit, /* 7 */
2086 &sensor_dev_template_temp_offset, /* 8 */
2087 &sensor_dev_template_temp_type, /* 9 */
2091 static struct sensor_template_group nct6775_temp_template_group = {
2092 .templates = nct6775_attributes_temp_template,
2093 .is_visible = nct6775_temp_is_visible,
2098 show_pwm_mode(struct device *dev, struct device_attribute *attr, char *buf)
2100 struct nct6775_data *data = nct6775_update_device(dev);
2101 struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr);
2103 return sprintf(buf, "%d\n", !data->pwm_mode[sattr->index]);
2107 store_pwm_mode(struct device *dev, struct device_attribute *attr,
2108 const char *buf, size_t count)
2110 struct nct6775_data *data = dev_get_drvdata(dev);
2111 struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr);
2112 int nr = sattr->index;
2117 err = kstrtoul(buf, 10, &val);
2124 /* Setting DC mode is not supported for all chips/channels */
2125 if (data->REG_PWM_MODE[nr] == 0) {
2131 mutex_lock(&data->update_lock);
2132 data->pwm_mode[nr] = val;
2133 reg = nct6775_read_value(data, data->REG_PWM_MODE[nr]);
2134 reg &= ~data->PWM_MODE_MASK[nr];
2136 reg |= data->PWM_MODE_MASK[nr];
2137 nct6775_write_value(data, data->REG_PWM_MODE[nr], reg);
2138 mutex_unlock(&data->update_lock);
2143 show_pwm(struct device *dev, struct device_attribute *attr, char *buf)
2145 struct nct6775_data *data = nct6775_update_device(dev);
2146 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
2148 int index = sattr->index;
2152 * For automatic fan control modes, show current pwm readings.
2153 * Otherwise, show the configured value.
2155 if (index == 0 && data->pwm_enable[nr] > manual)
2156 pwm = nct6775_read_value(data, data->REG_PWM_READ[nr]);
2158 pwm = data->pwm[index][nr];
2160 return sprintf(buf, "%d\n", pwm);
2164 store_pwm(struct device *dev, struct device_attribute *attr, const char *buf,
2167 struct nct6775_data *data = dev_get_drvdata(dev);
2168 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
2170 int index = sattr->index;
2172 int minval[7] = { 0, 1, 1, data->pwm[2][nr], 0, 0, 0 };
2174 = { 255, 255, data->pwm[3][nr] ? : 255, 255, 255, 255, 255 };
2178 err = kstrtoul(buf, 10, &val);
2181 val = clamp_val(val, minval[index], maxval[index]);
2183 mutex_lock(&data->update_lock);
2184 data->pwm[index][nr] = val;
2185 nct6775_write_value(data, data->REG_PWM[index][nr], val);
2186 if (index == 2) { /* floor: disable if val == 0 */
2187 reg = nct6775_read_value(data, data->REG_TEMP_SEL[nr]);
2191 nct6775_write_value(data, data->REG_TEMP_SEL[nr], reg);
2193 mutex_unlock(&data->update_lock);
2197 /* Returns 0 if OK, -EINVAL otherwise */
2198 static int check_trip_points(struct nct6775_data *data, int nr)
2202 for (i = 0; i < data->auto_pwm_num - 1; i++) {
2203 if (data->auto_temp[nr][i] > data->auto_temp[nr][i + 1])
2206 for (i = 0; i < data->auto_pwm_num - 1; i++) {
2207 if (data->auto_pwm[nr][i] > data->auto_pwm[nr][i + 1])
2210 /* validate critical temperature and pwm if enabled (pwm > 0) */
2211 if (data->auto_pwm[nr][data->auto_pwm_num]) {
2212 if (data->auto_temp[nr][data->auto_pwm_num - 1] >
2213 data->auto_temp[nr][data->auto_pwm_num] ||
2214 data->auto_pwm[nr][data->auto_pwm_num - 1] >
2215 data->auto_pwm[nr][data->auto_pwm_num])
2221 static void pwm_update_registers(struct nct6775_data *data, int nr)
2225 switch (data->pwm_enable[nr]) {
2230 reg = nct6775_read_value(data, data->REG_FAN_MODE[nr]);
2231 reg = (reg & ~data->tolerance_mask) |
2232 (data->target_speed_tolerance[nr] & data->tolerance_mask);
2233 nct6775_write_value(data, data->REG_FAN_MODE[nr], reg);
2234 nct6775_write_value(data, data->REG_TARGET[nr],
2235 data->target_speed[nr] & 0xff);
2236 if (data->REG_TOLERANCE_H) {
2237 reg = (data->target_speed[nr] >> 8) & 0x0f;
2238 reg |= (data->target_speed_tolerance[nr] & 0x38) << 1;
2239 nct6775_write_value(data,
2240 data->REG_TOLERANCE_H[nr],
2244 case thermal_cruise:
2245 nct6775_write_value(data, data->REG_TARGET[nr],
2246 data->target_temp[nr]);
2249 reg = nct6775_read_value(data, data->REG_FAN_MODE[nr]);
2250 reg = (reg & ~data->tolerance_mask) |
2251 data->temp_tolerance[0][nr];
2252 nct6775_write_value(data, data->REG_FAN_MODE[nr], reg);
2258 show_pwm_enable(struct device *dev, struct device_attribute *attr, char *buf)
2260 struct nct6775_data *data = nct6775_update_device(dev);
2261 struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr);
2263 return sprintf(buf, "%d\n", data->pwm_enable[sattr->index]);
2267 store_pwm_enable(struct device *dev, struct device_attribute *attr,
2268 const char *buf, size_t count)
2270 struct nct6775_data *data = dev_get_drvdata(dev);
2271 struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr);
2272 int nr = sattr->index;
2277 err = kstrtoul(buf, 10, &val);
2284 if (val == sf3 && data->kind != nct6775)
2287 if (val == sf4 && check_trip_points(data, nr)) {
2288 dev_err(dev, "Inconsistent trip points, not switching to SmartFan IV mode\n");
2289 dev_err(dev, "Adjust trip points and try again\n");
2293 mutex_lock(&data->update_lock);
2294 data->pwm_enable[nr] = val;
2297 * turn off pwm control: select manual mode, set pwm to maximum
2299 data->pwm[0][nr] = 255;
2300 nct6775_write_value(data, data->REG_PWM[0][nr], 255);
2302 pwm_update_registers(data, nr);
2303 reg = nct6775_read_value(data, data->REG_FAN_MODE[nr]);
2305 reg |= pwm_enable_to_reg(val) << 4;
2306 nct6775_write_value(data, data->REG_FAN_MODE[nr], reg);
2307 mutex_unlock(&data->update_lock);
2312 show_pwm_temp_sel_common(struct nct6775_data *data, char *buf, int src)
2316 for (i = 0; i < NUM_TEMP; i++) {
2317 if (!(data->have_temp & (1 << i)))
2319 if (src == data->temp_src[i]) {
2325 return sprintf(buf, "%d\n", sel);
2329 show_pwm_temp_sel(struct device *dev, struct device_attribute *attr, char *buf)
2331 struct nct6775_data *data = nct6775_update_device(dev);
2332 struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr);
2333 int index = sattr->index;
2335 return show_pwm_temp_sel_common(data, buf, data->pwm_temp_sel[index]);
2339 store_pwm_temp_sel(struct device *dev, struct device_attribute *attr,
2340 const char *buf, size_t count)
2342 struct nct6775_data *data = nct6775_update_device(dev);
2343 struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr);
2344 int nr = sattr->index;
2348 err = kstrtoul(buf, 10, &val);
2351 if (val == 0 || val > NUM_TEMP)
2353 if (!(data->have_temp & (1 << (val - 1))) || !data->temp_src[val - 1])
2356 mutex_lock(&data->update_lock);
2357 src = data->temp_src[val - 1];
2358 data->pwm_temp_sel[nr] = src;
2359 reg = nct6775_read_value(data, data->REG_TEMP_SEL[nr]);
2362 nct6775_write_value(data, data->REG_TEMP_SEL[nr], reg);
2363 mutex_unlock(&data->update_lock);
2369 show_pwm_weight_temp_sel(struct device *dev, struct device_attribute *attr,
2372 struct nct6775_data *data = nct6775_update_device(dev);
2373 struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr);
2374 int index = sattr->index;
2376 return show_pwm_temp_sel_common(data, buf,
2377 data->pwm_weight_temp_sel[index]);
2381 store_pwm_weight_temp_sel(struct device *dev, struct device_attribute *attr,
2382 const char *buf, size_t count)
2384 struct nct6775_data *data = nct6775_update_device(dev);
2385 struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr);
2386 int nr = sattr->index;
2390 err = kstrtoul(buf, 10, &val);
2395 if (val && (!(data->have_temp & (1 << (val - 1))) ||
2396 !data->temp_src[val - 1]))
2399 mutex_lock(&data->update_lock);
2401 src = data->temp_src[val - 1];
2402 data->pwm_weight_temp_sel[nr] = src;
2403 reg = nct6775_read_value(data, data->REG_WEIGHT_TEMP_SEL[nr]);
2405 reg |= (src | 0x80);
2406 nct6775_write_value(data, data->REG_WEIGHT_TEMP_SEL[nr], reg);
2408 data->pwm_weight_temp_sel[nr] = 0;
2409 reg = nct6775_read_value(data, data->REG_WEIGHT_TEMP_SEL[nr]);
2411 nct6775_write_value(data, data->REG_WEIGHT_TEMP_SEL[nr], reg);
2413 mutex_unlock(&data->update_lock);
2419 show_target_temp(struct device *dev, struct device_attribute *attr, char *buf)
2421 struct nct6775_data *data = nct6775_update_device(dev);
2422 struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr);
2424 return sprintf(buf, "%d\n", data->target_temp[sattr->index] * 1000);
2428 store_target_temp(struct device *dev, struct device_attribute *attr,
2429 const char *buf, size_t count)
2431 struct nct6775_data *data = dev_get_drvdata(dev);
2432 struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr);
2433 int nr = sattr->index;
2437 err = kstrtoul(buf, 10, &val);
2441 val = clamp_val(DIV_ROUND_CLOSEST(val, 1000), 0,
2442 data->target_temp_mask);
2444 mutex_lock(&data->update_lock);
2445 data->target_temp[nr] = val;
2446 pwm_update_registers(data, nr);
2447 mutex_unlock(&data->update_lock);
2452 show_target_speed(struct device *dev, struct device_attribute *attr, char *buf)
2454 struct nct6775_data *data = nct6775_update_device(dev);
2455 struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr);
2456 int nr = sattr->index;
2458 return sprintf(buf, "%d\n",
2459 fan_from_reg16(data->target_speed[nr],
2460 data->fan_div[nr]));
2464 store_target_speed(struct device *dev, struct device_attribute *attr,
2465 const char *buf, size_t count)
2467 struct nct6775_data *data = dev_get_drvdata(dev);
2468 struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr);
2469 int nr = sattr->index;
2474 err = kstrtoul(buf, 10, &val);
2478 val = clamp_val(val, 0, 1350000U);
2479 speed = fan_to_reg(val, data->fan_div[nr]);
2481 mutex_lock(&data->update_lock);
2482 data->target_speed[nr] = speed;
2483 pwm_update_registers(data, nr);
2484 mutex_unlock(&data->update_lock);
2489 show_temp_tolerance(struct device *dev, struct device_attribute *attr,
2492 struct nct6775_data *data = nct6775_update_device(dev);
2493 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
2495 int index = sattr->index;
2497 return sprintf(buf, "%d\n", data->temp_tolerance[index][nr] * 1000);
2501 store_temp_tolerance(struct device *dev, struct device_attribute *attr,
2502 const char *buf, size_t count)
2504 struct nct6775_data *data = dev_get_drvdata(dev);
2505 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
2507 int index = sattr->index;
2511 err = kstrtoul(buf, 10, &val);
2515 /* Limit tolerance as needed */
2516 val = clamp_val(DIV_ROUND_CLOSEST(val, 1000), 0, data->tolerance_mask);
2518 mutex_lock(&data->update_lock);
2519 data->temp_tolerance[index][nr] = val;
2521 pwm_update_registers(data, nr);
2523 nct6775_write_value(data,
2524 data->REG_CRITICAL_TEMP_TOLERANCE[nr],
2526 mutex_unlock(&data->update_lock);
2531 * Fan speed tolerance is a tricky beast, since the associated register is
2532 * a tick counter, but the value is reported and configured as rpm.
2533 * Compute resulting low and high rpm values and report the difference.
2536 show_speed_tolerance(struct device *dev, struct device_attribute *attr,
2539 struct nct6775_data *data = nct6775_update_device(dev);
2540 struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr);
2541 int nr = sattr->index;
2542 int low = data->target_speed[nr] - data->target_speed_tolerance[nr];
2543 int high = data->target_speed[nr] + data->target_speed_tolerance[nr];
2553 tolerance = (fan_from_reg16(low, data->fan_div[nr])
2554 - fan_from_reg16(high, data->fan_div[nr])) / 2;
2556 return sprintf(buf, "%d\n", tolerance);
2560 store_speed_tolerance(struct device *dev, struct device_attribute *attr,
2561 const char *buf, size_t count)
2563 struct nct6775_data *data = dev_get_drvdata(dev);
2564 struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr);
2565 int nr = sattr->index;
2570 err = kstrtoul(buf, 10, &val);
2574 high = fan_from_reg16(data->target_speed[nr],
2575 data->fan_div[nr]) + val;
2576 low = fan_from_reg16(data->target_speed[nr],
2577 data->fan_div[nr]) - val;
2583 val = (fan_to_reg(low, data->fan_div[nr]) -
2584 fan_to_reg(high, data->fan_div[nr])) / 2;
2586 /* Limit tolerance as needed */
2587 val = clamp_val(val, 0, data->speed_tolerance_limit);
2589 mutex_lock(&data->update_lock);
2590 data->target_speed_tolerance[nr] = val;
2591 pwm_update_registers(data, nr);
2592 mutex_unlock(&data->update_lock);
2596 SENSOR_TEMPLATE_2(pwm, "pwm%d", S_IWUSR | S_IRUGO, show_pwm, store_pwm, 0, 0);
2597 SENSOR_TEMPLATE(pwm_mode, "pwm%d_mode", S_IWUSR | S_IRUGO, show_pwm_mode,
2599 SENSOR_TEMPLATE(pwm_enable, "pwm%d_enable", S_IWUSR | S_IRUGO, show_pwm_enable,
2600 store_pwm_enable, 0);
2601 SENSOR_TEMPLATE(pwm_temp_sel, "pwm%d_temp_sel", S_IWUSR | S_IRUGO,
2602 show_pwm_temp_sel, store_pwm_temp_sel, 0);
2603 SENSOR_TEMPLATE(pwm_target_temp, "pwm%d_target_temp", S_IWUSR | S_IRUGO,
2604 show_target_temp, store_target_temp, 0);
2605 SENSOR_TEMPLATE(fan_target, "fan%d_target", S_IWUSR | S_IRUGO,
2606 show_target_speed, store_target_speed, 0);
2607 SENSOR_TEMPLATE(fan_tolerance, "fan%d_tolerance", S_IWUSR | S_IRUGO,
2608 show_speed_tolerance, store_speed_tolerance, 0);
2610 /* Smart Fan registers */
2613 show_weight_temp(struct device *dev, struct device_attribute *attr, char *buf)
2615 struct nct6775_data *data = nct6775_update_device(dev);
2616 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
2618 int index = sattr->index;
2620 return sprintf(buf, "%d\n", data->weight_temp[index][nr] * 1000);
2624 store_weight_temp(struct device *dev, struct device_attribute *attr,
2625 const char *buf, size_t count)
2627 struct nct6775_data *data = dev_get_drvdata(dev);
2628 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
2630 int index = sattr->index;
2634 err = kstrtoul(buf, 10, &val);
2638 val = clamp_val(DIV_ROUND_CLOSEST(val, 1000), 0, 255);
2640 mutex_lock(&data->update_lock);
2641 data->weight_temp[index][nr] = val;
2642 nct6775_write_value(data, data->REG_WEIGHT_TEMP[index][nr], val);
2643 mutex_unlock(&data->update_lock);
2647 SENSOR_TEMPLATE(pwm_weight_temp_sel, "pwm%d_weight_temp_sel", S_IWUSR | S_IRUGO,
2648 show_pwm_weight_temp_sel, store_pwm_weight_temp_sel, 0);
2649 SENSOR_TEMPLATE_2(pwm_weight_temp_step, "pwm%d_weight_temp_step",
2650 S_IWUSR | S_IRUGO, show_weight_temp, store_weight_temp, 0, 0);
2651 SENSOR_TEMPLATE_2(pwm_weight_temp_step_tol, "pwm%d_weight_temp_step_tol",
2652 S_IWUSR | S_IRUGO, show_weight_temp, store_weight_temp, 0, 1);
2653 SENSOR_TEMPLATE_2(pwm_weight_temp_step_base, "pwm%d_weight_temp_step_base",
2654 S_IWUSR | S_IRUGO, show_weight_temp, store_weight_temp, 0, 2);
2655 SENSOR_TEMPLATE_2(pwm_weight_duty_step, "pwm%d_weight_duty_step",
2656 S_IWUSR | S_IRUGO, show_pwm, store_pwm, 0, 5);
2657 SENSOR_TEMPLATE_2(pwm_weight_duty_base, "pwm%d_weight_duty_base",
2658 S_IWUSR | S_IRUGO, show_pwm, store_pwm, 0, 6);
2661 show_fan_time(struct device *dev, struct device_attribute *attr, char *buf)
2663 struct nct6775_data *data = nct6775_update_device(dev);
2664 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
2666 int index = sattr->index;
2668 return sprintf(buf, "%d\n",
2669 step_time_from_reg(data->fan_time[index][nr],
2670 data->pwm_mode[nr]));
2674 store_fan_time(struct device *dev, struct device_attribute *attr,
2675 const char *buf, size_t count)
2677 struct nct6775_data *data = dev_get_drvdata(dev);
2678 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
2680 int index = sattr->index;
2684 err = kstrtoul(buf, 10, &val);
2688 val = step_time_to_reg(val, data->pwm_mode[nr]);
2689 mutex_lock(&data->update_lock);
2690 data->fan_time[index][nr] = val;
2691 nct6775_write_value(data, data->REG_FAN_TIME[index][nr], val);
2692 mutex_unlock(&data->update_lock);
2697 show_name(struct device *dev, struct device_attribute *attr, char *buf)
2699 struct nct6775_data *data = dev_get_drvdata(dev);
2701 return sprintf(buf, "%s\n", data->name);
2704 static DEVICE_ATTR(name, S_IRUGO, show_name, NULL);
2707 show_auto_pwm(struct device *dev, struct device_attribute *attr, char *buf)
2709 struct nct6775_data *data = nct6775_update_device(dev);
2710 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
2712 return sprintf(buf, "%d\n", data->auto_pwm[sattr->nr][sattr->index]);
2716 store_auto_pwm(struct device *dev, struct device_attribute *attr,
2717 const char *buf, size_t count)
2719 struct nct6775_data *data = dev_get_drvdata(dev);
2720 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
2722 int point = sattr->index;
2727 err = kstrtoul(buf, 10, &val);
2733 if (point == data->auto_pwm_num) {
2734 if (data->kind != nct6775 && !val)
2736 if (data->kind != nct6779 && val)
2740 mutex_lock(&data->update_lock);
2741 data->auto_pwm[nr][point] = val;
2742 if (point < data->auto_pwm_num) {
2743 nct6775_write_value(data,
2744 NCT6775_AUTO_PWM(data, nr, point),
2745 data->auto_pwm[nr][point]);
2747 switch (data->kind) {
2749 /* disable if needed (pwm == 0) */
2750 reg = nct6775_read_value(data,
2751 NCT6775_REG_CRITICAL_ENAB[nr]);
2756 nct6775_write_value(data, NCT6775_REG_CRITICAL_ENAB[nr],
2760 break; /* always enabled, nothing to do */
2763 nct6775_write_value(data, data->REG_CRITICAL_PWM[nr],
2765 reg = nct6775_read_value(data,
2766 data->REG_CRITICAL_PWM_ENABLE[nr]);
2768 reg &= ~data->CRITICAL_PWM_ENABLE_MASK;
2770 reg |= data->CRITICAL_PWM_ENABLE_MASK;
2771 nct6775_write_value(data,
2772 data->REG_CRITICAL_PWM_ENABLE[nr],
2777 mutex_unlock(&data->update_lock);
2782 show_auto_temp(struct device *dev, struct device_attribute *attr, char *buf)
2784 struct nct6775_data *data = nct6775_update_device(dev);
2785 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
2787 int point = sattr->index;
2790 * We don't know for sure if the temperature is signed or unsigned.
2791 * Assume it is unsigned.
2793 return sprintf(buf, "%d\n", data->auto_temp[nr][point] * 1000);
2797 store_auto_temp(struct device *dev, struct device_attribute *attr,
2798 const char *buf, size_t count)
2800 struct nct6775_data *data = dev_get_drvdata(dev);
2801 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
2803 int point = sattr->index;
2807 err = kstrtoul(buf, 10, &val);
2813 mutex_lock(&data->update_lock);
2814 data->auto_temp[nr][point] = DIV_ROUND_CLOSEST(val, 1000);
2815 if (point < data->auto_pwm_num) {
2816 nct6775_write_value(data,
2817 NCT6775_AUTO_TEMP(data, nr, point),
2818 data->auto_temp[nr][point]);
2820 nct6775_write_value(data, data->REG_CRITICAL_TEMP[nr],
2821 data->auto_temp[nr][point]);
2823 mutex_unlock(&data->update_lock);
2827 static umode_t nct6775_pwm_is_visible(struct kobject *kobj,
2828 struct attribute *attr, int index)
2830 struct device *dev = container_of(kobj, struct device, kobj);
2831 struct nct6775_data *data = dev_get_drvdata(dev);
2832 int pwm = index / 36; /* pwm index */
2833 int nr = index % 36; /* attribute index */
2835 if (!(data->has_pwm & (1 << pwm)))
2838 if (nr == 19 && data->REG_PWM[3] == NULL) /* pwm_max */
2840 if (nr == 20 && data->REG_PWM[4] == NULL) /* pwm_step */
2842 if (nr == 21 && data->REG_PWM[6] == NULL) /* weight_duty_base */
2845 if (nr >= 22 && nr <= 35) { /* auto point */
2846 int api = (nr - 22) / 2; /* auto point index */
2848 if (api > data->auto_pwm_num)
2854 SENSOR_TEMPLATE_2(pwm_stop_time, "pwm%d_stop_time", S_IWUSR | S_IRUGO,
2855 show_fan_time, store_fan_time, 0, 0);
2856 SENSOR_TEMPLATE_2(pwm_step_up_time, "pwm%d_step_up_time", S_IWUSR | S_IRUGO,
2857 show_fan_time, store_fan_time, 0, 1);
2858 SENSOR_TEMPLATE_2(pwm_step_down_time, "pwm%d_step_down_time", S_IWUSR | S_IRUGO,
2859 show_fan_time, store_fan_time, 0, 2);
2860 SENSOR_TEMPLATE_2(pwm_start, "pwm%d_start", S_IWUSR | S_IRUGO, show_pwm,
2862 SENSOR_TEMPLATE_2(pwm_floor, "pwm%d_floor", S_IWUSR | S_IRUGO, show_pwm,
2864 SENSOR_TEMPLATE_2(pwm_temp_tolerance, "pwm%d_temp_tolerance", S_IWUSR | S_IRUGO,
2865 show_temp_tolerance, store_temp_tolerance, 0, 0);
2866 SENSOR_TEMPLATE_2(pwm_crit_temp_tolerance, "pwm%d_crit_temp_tolerance",
2867 S_IWUSR | S_IRUGO, show_temp_tolerance, store_temp_tolerance,
2870 SENSOR_TEMPLATE_2(pwm_max, "pwm%d_max", S_IWUSR | S_IRUGO, show_pwm, store_pwm,
2873 SENSOR_TEMPLATE_2(pwm_step, "pwm%d_step", S_IWUSR | S_IRUGO, show_pwm,
2876 SENSOR_TEMPLATE_2(pwm_auto_point1_pwm, "pwm%d_auto_point1_pwm",
2877 S_IWUSR | S_IRUGO, show_auto_pwm, store_auto_pwm, 0, 0);
2878 SENSOR_TEMPLATE_2(pwm_auto_point1_temp, "pwm%d_auto_point1_temp",
2879 S_IWUSR | S_IRUGO, show_auto_temp, store_auto_temp, 0, 0);
2881 SENSOR_TEMPLATE_2(pwm_auto_point2_pwm, "pwm%d_auto_point2_pwm",
2882 S_IWUSR | S_IRUGO, show_auto_pwm, store_auto_pwm, 0, 1);
2883 SENSOR_TEMPLATE_2(pwm_auto_point2_temp, "pwm%d_auto_point2_temp",
2884 S_IWUSR | S_IRUGO, show_auto_temp, store_auto_temp, 0, 1);
2886 SENSOR_TEMPLATE_2(pwm_auto_point3_pwm, "pwm%d_auto_point3_pwm",
2887 S_IWUSR | S_IRUGO, show_auto_pwm, store_auto_pwm, 0, 2);
2888 SENSOR_TEMPLATE_2(pwm_auto_point3_temp, "pwm%d_auto_point3_temp",
2889 S_IWUSR | S_IRUGO, show_auto_temp, store_auto_temp, 0, 2);
2891 SENSOR_TEMPLATE_2(pwm_auto_point4_pwm, "pwm%d_auto_point4_pwm",
2892 S_IWUSR | S_IRUGO, show_auto_pwm, store_auto_pwm, 0, 3);
2893 SENSOR_TEMPLATE_2(pwm_auto_point4_temp, "pwm%d_auto_point4_temp",
2894 S_IWUSR | S_IRUGO, show_auto_temp, store_auto_temp, 0, 3);
2896 SENSOR_TEMPLATE_2(pwm_auto_point5_pwm, "pwm%d_auto_point5_pwm",
2897 S_IWUSR | S_IRUGO, show_auto_pwm, store_auto_pwm, 0, 4);
2898 SENSOR_TEMPLATE_2(pwm_auto_point5_temp, "pwm%d_auto_point5_temp",
2899 S_IWUSR | S_IRUGO, show_auto_temp, store_auto_temp, 0, 4);
2901 SENSOR_TEMPLATE_2(pwm_auto_point6_pwm, "pwm%d_auto_point6_pwm",
2902 S_IWUSR | S_IRUGO, show_auto_pwm, store_auto_pwm, 0, 5);
2903 SENSOR_TEMPLATE_2(pwm_auto_point6_temp, "pwm%d_auto_point6_temp",
2904 S_IWUSR | S_IRUGO, show_auto_temp, store_auto_temp, 0, 5);
2906 SENSOR_TEMPLATE_2(pwm_auto_point7_pwm, "pwm%d_auto_point7_pwm",
2907 S_IWUSR | S_IRUGO, show_auto_pwm, store_auto_pwm, 0, 6);
2908 SENSOR_TEMPLATE_2(pwm_auto_point7_temp, "pwm%d_auto_point7_temp",
2909 S_IWUSR | S_IRUGO, show_auto_temp, store_auto_temp, 0, 6);
2912 * nct6775_pwm_is_visible uses the index into the following array
2913 * to determine if attributes should be created or not.
2914 * Any change in order or content must be matched.
2916 static struct sensor_device_template *nct6775_attributes_pwm_template[] = {
2917 &sensor_dev_template_pwm,
2918 &sensor_dev_template_pwm_mode,
2919 &sensor_dev_template_pwm_enable,
2920 &sensor_dev_template_pwm_temp_sel,
2921 &sensor_dev_template_pwm_temp_tolerance,
2922 &sensor_dev_template_pwm_crit_temp_tolerance,
2923 &sensor_dev_template_pwm_target_temp,
2924 &sensor_dev_template_fan_target,
2925 &sensor_dev_template_fan_tolerance,
2926 &sensor_dev_template_pwm_stop_time,
2927 &sensor_dev_template_pwm_step_up_time,
2928 &sensor_dev_template_pwm_step_down_time,
2929 &sensor_dev_template_pwm_start,
2930 &sensor_dev_template_pwm_floor,
2931 &sensor_dev_template_pwm_weight_temp_sel,
2932 &sensor_dev_template_pwm_weight_temp_step,
2933 &sensor_dev_template_pwm_weight_temp_step_tol,
2934 &sensor_dev_template_pwm_weight_temp_step_base,
2935 &sensor_dev_template_pwm_weight_duty_step,
2936 &sensor_dev_template_pwm_max, /* 19 */
2937 &sensor_dev_template_pwm_step, /* 20 */
2938 &sensor_dev_template_pwm_weight_duty_base, /* 21 */
2939 &sensor_dev_template_pwm_auto_point1_pwm, /* 22 */
2940 &sensor_dev_template_pwm_auto_point1_temp,
2941 &sensor_dev_template_pwm_auto_point2_pwm,
2942 &sensor_dev_template_pwm_auto_point2_temp,
2943 &sensor_dev_template_pwm_auto_point3_pwm,
2944 &sensor_dev_template_pwm_auto_point3_temp,
2945 &sensor_dev_template_pwm_auto_point4_pwm,
2946 &sensor_dev_template_pwm_auto_point4_temp,
2947 &sensor_dev_template_pwm_auto_point5_pwm,
2948 &sensor_dev_template_pwm_auto_point5_temp,
2949 &sensor_dev_template_pwm_auto_point6_pwm,
2950 &sensor_dev_template_pwm_auto_point6_temp,
2951 &sensor_dev_template_pwm_auto_point7_pwm,
2952 &sensor_dev_template_pwm_auto_point7_temp, /* 35 */
2957 static struct sensor_template_group nct6775_pwm_template_group = {
2958 .templates = nct6775_attributes_pwm_template,
2959 .is_visible = nct6775_pwm_is_visible,
2964 show_vid(struct device *dev, struct device_attribute *attr, char *buf)
2966 struct nct6775_data *data = dev_get_drvdata(dev);
2967 return sprintf(buf, "%d\n", vid_from_reg(data->vid, data->vrm));
2970 static DEVICE_ATTR(cpu0_vid, S_IRUGO, show_vid, NULL);
2972 /* Case open detection */
2975 clear_caseopen(struct device *dev, struct device_attribute *attr,
2976 const char *buf, size_t count)
2978 struct nct6775_data *data = dev_get_drvdata(dev);
2979 struct nct6775_sio_data *sio_data = dev->platform_data;
2980 int nr = to_sensor_dev_attr(attr)->index - INTRUSION_ALARM_BASE;
2985 if (kstrtoul(buf, 10, &val) || val != 0)
2988 mutex_lock(&data->update_lock);
2991 * Use CR registers to clear caseopen status.
2992 * The CR registers are the same for all chips, and not all chips
2993 * support clearing the caseopen status through "regular" registers.
2995 ret = superio_enter(sio_data->sioreg);
3001 superio_select(sio_data->sioreg, NCT6775_LD_ACPI);
3002 reg = superio_inb(sio_data->sioreg, NCT6775_REG_CR_CASEOPEN_CLR[nr]);
3003 reg |= NCT6775_CR_CASEOPEN_CLR_MASK[nr];
3004 superio_outb(sio_data->sioreg, NCT6775_REG_CR_CASEOPEN_CLR[nr], reg);
3005 reg &= ~NCT6775_CR_CASEOPEN_CLR_MASK[nr];
3006 superio_outb(sio_data->sioreg, NCT6775_REG_CR_CASEOPEN_CLR[nr], reg);
3007 superio_exit(sio_data->sioreg);
3009 data->valid = false; /* Force cache refresh */
3011 mutex_unlock(&data->update_lock);
3015 static SENSOR_DEVICE_ATTR(intrusion0_alarm, S_IWUSR | S_IRUGO, show_alarm,
3016 clear_caseopen, INTRUSION_ALARM_BASE);
3017 static SENSOR_DEVICE_ATTR(intrusion1_alarm, S_IWUSR | S_IRUGO, show_alarm,
3018 clear_caseopen, INTRUSION_ALARM_BASE + 1);
3019 static SENSOR_DEVICE_ATTR(intrusion0_beep, S_IWUSR | S_IRUGO, show_beep,
3020 store_beep, INTRUSION_ALARM_BASE);
3021 static SENSOR_DEVICE_ATTR(intrusion1_beep, S_IWUSR | S_IRUGO, show_beep,
3022 store_beep, INTRUSION_ALARM_BASE + 1);
3023 static SENSOR_DEVICE_ATTR(beep_enable, S_IWUSR | S_IRUGO, show_beep,
3024 store_beep, BEEP_ENABLE_BASE);
3026 static umode_t nct6775_other_is_visible(struct kobject *kobj,
3027 struct attribute *attr, int index)
3029 struct device *dev = container_of(kobj, struct device, kobj);
3030 struct nct6775_data *data = dev_get_drvdata(dev);
3032 if (index == 1 && !data->have_vid)
3035 if (index == 2 || index == 3) {
3036 if (data->ALARM_BITS[INTRUSION_ALARM_BASE + index - 2] < 0)
3040 if (index == 4 || index == 5) {
3041 if (data->BEEP_BITS[INTRUSION_ALARM_BASE + index - 4] < 0)
3049 * nct6775_other_is_visible uses the index into the following array
3050 * to determine if attributes should be created or not.
3051 * Any change in order or content must be matched.
3053 static struct attribute *nct6775_attributes_other[] = {
3054 &dev_attr_name.attr,
3055 &dev_attr_cpu0_vid.attr, /* 1 */
3056 &sensor_dev_attr_intrusion0_alarm.dev_attr.attr, /* 2 */
3057 &sensor_dev_attr_intrusion1_alarm.dev_attr.attr, /* 3 */
3058 &sensor_dev_attr_intrusion0_beep.dev_attr.attr, /* 4 */
3059 &sensor_dev_attr_intrusion1_beep.dev_attr.attr, /* 5 */
3060 &sensor_dev_attr_beep_enable.dev_attr.attr, /* 6 */
3065 static const struct attribute_group nct6775_group_other = {
3066 .attrs = nct6775_attributes_other,
3067 .is_visible = nct6775_other_is_visible,
3071 * Driver and device management
3074 static void nct6775_device_remove_files(struct device *dev)
3076 struct nct6775_data *data = dev_get_drvdata(dev);
3078 if (data->group_pwm)
3079 sysfs_remove_group(&dev->kobj, data->group_pwm);
3081 sysfs_remove_group(&dev->kobj, data->group_in);
3082 if (data->group_fan)
3083 sysfs_remove_group(&dev->kobj, data->group_fan);
3084 if (data->group_temp)
3085 sysfs_remove_group(&dev->kobj, data->group_temp);
3087 sysfs_remove_group(&dev->kobj, &nct6775_group_other);
3090 /* Get the monitoring functions started */
3091 static inline void nct6775_init_device(struct nct6775_data *data)
3096 /* Start monitoring if needed */
3097 if (data->REG_CONFIG) {
3098 tmp = nct6775_read_value(data, data->REG_CONFIG);
3100 nct6775_write_value(data, data->REG_CONFIG, tmp | 0x01);
3103 /* Enable temperature sensors if needed */
3104 for (i = 0; i < NUM_TEMP; i++) {
3105 if (!(data->have_temp & (1 << i)))
3107 if (!data->reg_temp_config[i])
3109 tmp = nct6775_read_value(data, data->reg_temp_config[i]);
3111 nct6775_write_value(data, data->reg_temp_config[i],
3115 /* Enable VBAT monitoring if needed */
3116 tmp = nct6775_read_value(data, data->REG_VBAT);
3118 nct6775_write_value(data, data->REG_VBAT, tmp | 0x01);
3120 diode = nct6775_read_value(data, data->REG_DIODE);
3122 for (i = 0; i < data->temp_fixed_num; i++) {
3123 if (!(data->have_temp_fixed & (1 << i)))
3125 if ((tmp & (data->DIODE_MASK << i))) /* diode */
3127 = 3 - ((diode >> i) & data->DIODE_MASK);
3128 else /* thermistor */
3129 data->temp_type[i] = 4;
3134 nct6775_check_fan_inputs(const struct nct6775_sio_data *sio_data,
3135 struct nct6775_data *data)
3138 bool fan3pin, fan3min, fan4pin, fan4min, fan5pin;
3139 bool pwm3pin, pwm4pin, pwm5pin;
3141 /* fan4 and fan5 share some pins with the GPIO and serial flash */
3142 if (data->kind == nct6775) {
3143 regval = superio_inb(sio_data->sioreg, 0x2c);
3145 fan3pin = regval & (1 << 6);
3147 pwm3pin = regval & (1 << 7);
3149 /* On NCT6775, fan4 shares pins with the fdc interface */
3150 fan4pin = !(superio_inb(sio_data->sioreg, 0x2A) & 0x80);
3155 } else if (data->kind == nct6776) {
3156 bool gpok = superio_inb(sio_data->sioreg, 0x27) & 0x80;
3158 superio_select(sio_data->sioreg, NCT6775_LD_HWM);
3159 regval = superio_inb(sio_data->sioreg, SIO_REG_ENABLE);
3164 fan3pin = !(superio_inb(sio_data->sioreg, 0x24) & 0x40);
3169 fan4pin = superio_inb(sio_data->sioreg, 0x1C) & 0x01;
3174 fan5pin = superio_inb(sio_data->sioreg, 0x1C) & 0x02;
3181 } else if (data->kind == nct6106) {
3182 regval = superio_inb(sio_data->sioreg, 0x24);
3183 fan3pin = !(regval & 0x80);
3184 pwm3pin = regval & 0x08;
3192 } else { /* NCT6779D */
3193 regval = superio_inb(sio_data->sioreg, 0x1c);
3195 fan3pin = !(regval & (1 << 5));
3196 fan4pin = !(regval & (1 << 6));
3197 fan5pin = !(regval & (1 << 7));
3199 pwm3pin = !(regval & (1 << 0));
3200 pwm4pin = !(regval & (1 << 1));
3201 pwm5pin = !(regval & (1 << 2));
3207 data->has_fan = data->has_fan_min = 0x03; /* fan1 and fan2 */
3208 data->has_fan |= fan3pin << 2;
3209 data->has_fan_min |= fan3min << 2;
3211 data->has_fan |= (fan4pin << 3) | (fan5pin << 4);
3212 data->has_fan_min |= (fan4min << 3) | (fan5pin << 4);
3214 data->has_pwm = 0x03 | (pwm3pin << 2) | (pwm4pin << 3) | (pwm5pin << 4);
3217 static void add_temp_sensors(struct nct6775_data *data, const u16 *regp,
3218 int *available, int *mask)
3223 for (i = 0; i < data->pwm_num && *available; i++) {
3228 src = nct6775_read_value(data, regp[i]);
3230 if (!src || (*mask & (1 << src)))
3232 if (src >= data->temp_label_num ||
3233 !strlen(data->temp_label[src]))
3236 index = __ffs(*available);
3237 nct6775_write_value(data, data->REG_TEMP_SOURCE[index], src);
3238 *available &= ~(1 << index);
3243 static int nct6775_probe(struct platform_device *pdev)
3245 struct device *dev = &pdev->dev;
3246 struct nct6775_sio_data *sio_data = dev->platform_data;
3247 struct nct6775_data *data;
3248 struct resource *res;
3250 int src, mask, available;
3251 const u16 *reg_temp, *reg_temp_over, *reg_temp_hyst, *reg_temp_config;
3252 const u16 *reg_temp_alternate, *reg_temp_crit;
3253 const u16 *reg_temp_crit_l = NULL, *reg_temp_crit_h = NULL;
3256 struct attribute_group *group;
3258 res = platform_get_resource(pdev, IORESOURCE_IO, 0);
3259 if (!devm_request_region(&pdev->dev, res->start, IOREGION_LENGTH,
3263 data = devm_kzalloc(&pdev->dev, sizeof(struct nct6775_data),
3268 data->kind = sio_data->kind;
3269 data->addr = res->start;
3270 mutex_init(&data->update_lock);
3271 data->name = nct6775_device_names[data->kind];
3272 data->bank = 0xff; /* Force initial bank selection */
3273 platform_set_drvdata(pdev, data);
3275 switch (data->kind) {
3279 data->auto_pwm_num = 4;
3280 data->temp_fixed_num = 3;
3281 data->num_temp_alarms = 6;
3282 data->num_temp_beeps = 6;
3284 data->fan_from_reg = fan_from_reg13;
3285 data->fan_from_reg_min = fan_from_reg13;
3287 data->temp_label = nct6776_temp_label;
3288 data->temp_label_num = ARRAY_SIZE(nct6776_temp_label);
3290 data->REG_VBAT = NCT6106_REG_VBAT;
3291 data->REG_DIODE = NCT6106_REG_DIODE;
3292 data->DIODE_MASK = NCT6106_DIODE_MASK;
3293 data->REG_VIN = NCT6106_REG_IN;
3294 data->REG_IN_MINMAX[0] = NCT6106_REG_IN_MIN;
3295 data->REG_IN_MINMAX[1] = NCT6106_REG_IN_MAX;
3296 data->REG_TARGET = NCT6106_REG_TARGET;
3297 data->REG_FAN = NCT6106_REG_FAN;
3298 data->REG_FAN_MODE = NCT6106_REG_FAN_MODE;
3299 data->REG_FAN_MIN = NCT6106_REG_FAN_MIN;
3300 data->REG_FAN_PULSES = NCT6106_REG_FAN_PULSES;
3301 data->FAN_PULSE_SHIFT = NCT6106_FAN_PULSE_SHIFT;
3302 data->REG_FAN_TIME[0] = NCT6106_REG_FAN_STOP_TIME;
3303 data->REG_FAN_TIME[1] = NCT6106_REG_FAN_STEP_UP_TIME;
3304 data->REG_FAN_TIME[2] = NCT6106_REG_FAN_STEP_DOWN_TIME;
3305 data->REG_PWM[0] = NCT6106_REG_PWM;
3306 data->REG_PWM[1] = NCT6106_REG_FAN_START_OUTPUT;
3307 data->REG_PWM[2] = NCT6106_REG_FAN_STOP_OUTPUT;
3308 data->REG_PWM[5] = NCT6106_REG_WEIGHT_DUTY_STEP;
3309 data->REG_PWM[6] = NCT6106_REG_WEIGHT_DUTY_BASE;
3310 data->REG_PWM_READ = NCT6106_REG_PWM_READ;
3311 data->REG_PWM_MODE = NCT6106_REG_PWM_MODE;
3312 data->PWM_MODE_MASK = NCT6106_PWM_MODE_MASK;
3313 data->REG_AUTO_TEMP = NCT6106_REG_AUTO_TEMP;
3314 data->REG_AUTO_PWM = NCT6106_REG_AUTO_PWM;
3315 data->REG_CRITICAL_TEMP = NCT6106_REG_CRITICAL_TEMP;
3316 data->REG_CRITICAL_TEMP_TOLERANCE
3317 = NCT6106_REG_CRITICAL_TEMP_TOLERANCE;
3318 data->REG_CRITICAL_PWM_ENABLE = NCT6106_REG_CRITICAL_PWM_ENABLE;
3319 data->CRITICAL_PWM_ENABLE_MASK
3320 = NCT6106_CRITICAL_PWM_ENABLE_MASK;
3321 data->REG_CRITICAL_PWM = NCT6106_REG_CRITICAL_PWM;
3322 data->REG_TEMP_OFFSET = NCT6106_REG_TEMP_OFFSET;
3323 data->REG_TEMP_SOURCE = NCT6106_REG_TEMP_SOURCE;
3324 data->REG_TEMP_SEL = NCT6106_REG_TEMP_SEL;
3325 data->REG_WEIGHT_TEMP_SEL = NCT6106_REG_WEIGHT_TEMP_SEL;
3326 data->REG_WEIGHT_TEMP[0] = NCT6106_REG_WEIGHT_TEMP_STEP;
3327 data->REG_WEIGHT_TEMP[1] = NCT6106_REG_WEIGHT_TEMP_STEP_TOL;
3328 data->REG_WEIGHT_TEMP[2] = NCT6106_REG_WEIGHT_TEMP_BASE;
3329 data->REG_ALARM = NCT6106_REG_ALARM;
3330 data->ALARM_BITS = NCT6106_ALARM_BITS;
3331 data->REG_BEEP = NCT6106_REG_BEEP;
3332 data->BEEP_BITS = NCT6106_BEEP_BITS;
3334 reg_temp = NCT6106_REG_TEMP;
3335 num_reg_temp = ARRAY_SIZE(NCT6106_REG_TEMP);
3336 reg_temp_over = NCT6106_REG_TEMP_OVER;
3337 reg_temp_hyst = NCT6106_REG_TEMP_HYST;
3338 reg_temp_config = NCT6106_REG_TEMP_CONFIG;
3339 reg_temp_alternate = NCT6106_REG_TEMP_ALTERNATE;
3340 reg_temp_crit = NCT6106_REG_TEMP_CRIT;
3341 reg_temp_crit_l = NCT6106_REG_TEMP_CRIT_L;
3342 reg_temp_crit_h = NCT6106_REG_TEMP_CRIT_H;
3348 data->auto_pwm_num = 6;
3349 data->has_fan_div = true;
3350 data->temp_fixed_num = 3;
3351 data->num_temp_alarms = 3;
3352 data->num_temp_beeps = 3;
3354 data->ALARM_BITS = NCT6775_ALARM_BITS;
3355 data->BEEP_BITS = NCT6775_BEEP_BITS;
3357 data->fan_from_reg = fan_from_reg16;
3358 data->fan_from_reg_min = fan_from_reg8;
3359 data->target_temp_mask = 0x7f;
3360 data->tolerance_mask = 0x0f;
3361 data->speed_tolerance_limit = 15;
3363 data->temp_label = nct6775_temp_label;
3364 data->temp_label_num = ARRAY_SIZE(nct6775_temp_label);
3366 data->REG_CONFIG = NCT6775_REG_CONFIG;
3367 data->REG_VBAT = NCT6775_REG_VBAT;
3368 data->REG_DIODE = NCT6775_REG_DIODE;
3369 data->DIODE_MASK = NCT6775_DIODE_MASK;
3370 data->REG_VIN = NCT6775_REG_IN;
3371 data->REG_IN_MINMAX[0] = NCT6775_REG_IN_MIN;
3372 data->REG_IN_MINMAX[1] = NCT6775_REG_IN_MAX;
3373 data->REG_TARGET = NCT6775_REG_TARGET;
3374 data->REG_FAN = NCT6775_REG_FAN;
3375 data->REG_FAN_MODE = NCT6775_REG_FAN_MODE;
3376 data->REG_FAN_MIN = NCT6775_REG_FAN_MIN;
3377 data->REG_FAN_PULSES = NCT6775_REG_FAN_PULSES;
3378 data->FAN_PULSE_SHIFT = NCT6775_FAN_PULSE_SHIFT;
3379 data->REG_FAN_TIME[0] = NCT6775_REG_FAN_STOP_TIME;
3380 data->REG_FAN_TIME[1] = NCT6775_REG_FAN_STEP_UP_TIME;
3381 data->REG_FAN_TIME[2] = NCT6775_REG_FAN_STEP_DOWN_TIME;
3382 data->REG_PWM[0] = NCT6775_REG_PWM;
3383 data->REG_PWM[1] = NCT6775_REG_FAN_START_OUTPUT;
3384 data->REG_PWM[2] = NCT6775_REG_FAN_STOP_OUTPUT;
3385 data->REG_PWM[3] = NCT6775_REG_FAN_MAX_OUTPUT;
3386 data->REG_PWM[4] = NCT6775_REG_FAN_STEP_OUTPUT;
3387 data->REG_PWM[5] = NCT6775_REG_WEIGHT_DUTY_STEP;
3388 data->REG_PWM_READ = NCT6775_REG_PWM_READ;
3389 data->REG_PWM_MODE = NCT6775_REG_PWM_MODE;
3390 data->PWM_MODE_MASK = NCT6775_PWM_MODE_MASK;
3391 data->REG_AUTO_TEMP = NCT6775_REG_AUTO_TEMP;
3392 data->REG_AUTO_PWM = NCT6775_REG_AUTO_PWM;
3393 data->REG_CRITICAL_TEMP = NCT6775_REG_CRITICAL_TEMP;
3394 data->REG_CRITICAL_TEMP_TOLERANCE
3395 = NCT6775_REG_CRITICAL_TEMP_TOLERANCE;
3396 data->REG_TEMP_OFFSET = NCT6775_REG_TEMP_OFFSET;
3397 data->REG_TEMP_SOURCE = NCT6775_REG_TEMP_SOURCE;
3398 data->REG_TEMP_SEL = NCT6775_REG_TEMP_SEL;
3399 data->REG_WEIGHT_TEMP_SEL = NCT6775_REG_WEIGHT_TEMP_SEL;
3400 data->REG_WEIGHT_TEMP[0] = NCT6775_REG_WEIGHT_TEMP_STEP;
3401 data->REG_WEIGHT_TEMP[1] = NCT6775_REG_WEIGHT_TEMP_STEP_TOL;
3402 data->REG_WEIGHT_TEMP[2] = NCT6775_REG_WEIGHT_TEMP_BASE;
3403 data->REG_ALARM = NCT6775_REG_ALARM;
3404 data->REG_BEEP = NCT6775_REG_BEEP;
3406 reg_temp = NCT6775_REG_TEMP;
3407 num_reg_temp = ARRAY_SIZE(NCT6775_REG_TEMP);
3408 reg_temp_over = NCT6775_REG_TEMP_OVER;
3409 reg_temp_hyst = NCT6775_REG_TEMP_HYST;
3410 reg_temp_config = NCT6775_REG_TEMP_CONFIG;
3411 reg_temp_alternate = NCT6775_REG_TEMP_ALTERNATE;
3412 reg_temp_crit = NCT6775_REG_TEMP_CRIT;
3418 data->auto_pwm_num = 4;
3419 data->has_fan_div = false;
3420 data->temp_fixed_num = 3;
3421 data->num_temp_alarms = 3;
3422 data->num_temp_beeps = 6;
3424 data->ALARM_BITS = NCT6776_ALARM_BITS;
3425 data->BEEP_BITS = NCT6776_BEEP_BITS;
3427 data->fan_from_reg = fan_from_reg13;
3428 data->fan_from_reg_min = fan_from_reg13;
3429 data->target_temp_mask = 0xff;
3430 data->tolerance_mask = 0x07;
3431 data->speed_tolerance_limit = 63;
3433 data->temp_label = nct6776_temp_label;
3434 data->temp_label_num = ARRAY_SIZE(nct6776_temp_label);
3436 data->REG_CONFIG = NCT6775_REG_CONFIG;
3437 data->REG_VBAT = NCT6775_REG_VBAT;
3438 data->REG_DIODE = NCT6775_REG_DIODE;
3439 data->DIODE_MASK = NCT6775_DIODE_MASK;
3440 data->REG_VIN = NCT6775_REG_IN;
3441 data->REG_IN_MINMAX[0] = NCT6775_REG_IN_MIN;
3442 data->REG_IN_MINMAX[1] = NCT6775_REG_IN_MAX;
3443 data->REG_TARGET = NCT6775_REG_TARGET;
3444 data->REG_FAN = NCT6775_REG_FAN;
3445 data->REG_FAN_MODE = NCT6775_REG_FAN_MODE;
3446 data->REG_FAN_MIN = NCT6776_REG_FAN_MIN;
3447 data->REG_FAN_PULSES = NCT6776_REG_FAN_PULSES;
3448 data->FAN_PULSE_SHIFT = NCT6775_FAN_PULSE_SHIFT;
3449 data->REG_FAN_TIME[0] = NCT6775_REG_FAN_STOP_TIME;
3450 data->REG_FAN_TIME[1] = NCT6775_REG_FAN_STEP_UP_TIME;
3451 data->REG_FAN_TIME[2] = NCT6775_REG_FAN_STEP_DOWN_TIME;
3452 data->REG_TOLERANCE_H = NCT6776_REG_TOLERANCE_H;
3453 data->REG_PWM[0] = NCT6775_REG_PWM;
3454 data->REG_PWM[1] = NCT6775_REG_FAN_START_OUTPUT;
3455 data->REG_PWM[2] = NCT6775_REG_FAN_STOP_OUTPUT;
3456 data->REG_PWM[5] = NCT6775_REG_WEIGHT_DUTY_STEP;
3457 data->REG_PWM[6] = NCT6776_REG_WEIGHT_DUTY_BASE;
3458 data->REG_PWM_READ = NCT6775_REG_PWM_READ;
3459 data->REG_PWM_MODE = NCT6776_REG_PWM_MODE;
3460 data->PWM_MODE_MASK = NCT6776_PWM_MODE_MASK;
3461 data->REG_AUTO_TEMP = NCT6775_REG_AUTO_TEMP;
3462 data->REG_AUTO_PWM = NCT6775_REG_AUTO_PWM;
3463 data->REG_CRITICAL_TEMP = NCT6775_REG_CRITICAL_TEMP;
3464 data->REG_CRITICAL_TEMP_TOLERANCE
3465 = NCT6775_REG_CRITICAL_TEMP_TOLERANCE;
3466 data->REG_TEMP_OFFSET = NCT6775_REG_TEMP_OFFSET;
3467 data->REG_TEMP_SOURCE = NCT6775_REG_TEMP_SOURCE;
3468 data->REG_TEMP_SEL = NCT6775_REG_TEMP_SEL;
3469 data->REG_WEIGHT_TEMP_SEL = NCT6775_REG_WEIGHT_TEMP_SEL;
3470 data->REG_WEIGHT_TEMP[0] = NCT6775_REG_WEIGHT_TEMP_STEP;
3471 data->REG_WEIGHT_TEMP[1] = NCT6775_REG_WEIGHT_TEMP_STEP_TOL;
3472 data->REG_WEIGHT_TEMP[2] = NCT6775_REG_WEIGHT_TEMP_BASE;
3473 data->REG_ALARM = NCT6775_REG_ALARM;
3474 data->REG_BEEP = NCT6776_REG_BEEP;
3476 reg_temp = NCT6775_REG_TEMP;
3477 num_reg_temp = ARRAY_SIZE(NCT6775_REG_TEMP);
3478 reg_temp_over = NCT6775_REG_TEMP_OVER;
3479 reg_temp_hyst = NCT6775_REG_TEMP_HYST;
3480 reg_temp_config = NCT6776_REG_TEMP_CONFIG;
3481 reg_temp_alternate = NCT6776_REG_TEMP_ALTERNATE;
3482 reg_temp_crit = NCT6776_REG_TEMP_CRIT;
3488 data->auto_pwm_num = 4;
3489 data->has_fan_div = false;
3490 data->temp_fixed_num = 6;
3491 data->num_temp_alarms = 2;
3492 data->num_temp_beeps = 2;
3494 data->ALARM_BITS = NCT6779_ALARM_BITS;
3495 data->BEEP_BITS = NCT6779_BEEP_BITS;
3497 data->fan_from_reg = fan_from_reg13;
3498 data->fan_from_reg_min = fan_from_reg13;
3499 data->target_temp_mask = 0xff;
3500 data->tolerance_mask = 0x07;
3501 data->speed_tolerance_limit = 63;
3503 data->temp_label = nct6779_temp_label;
3504 data->temp_label_num = ARRAY_SIZE(nct6779_temp_label);
3506 data->REG_CONFIG = NCT6775_REG_CONFIG;
3507 data->REG_VBAT = NCT6775_REG_VBAT;
3508 data->REG_DIODE = NCT6775_REG_DIODE;
3509 data->DIODE_MASK = NCT6775_DIODE_MASK;
3510 data->REG_VIN = NCT6779_REG_IN;
3511 data->REG_IN_MINMAX[0] = NCT6775_REG_IN_MIN;
3512 data->REG_IN_MINMAX[1] = NCT6775_REG_IN_MAX;
3513 data->REG_TARGET = NCT6775_REG_TARGET;
3514 data->REG_FAN = NCT6779_REG_FAN;
3515 data->REG_FAN_MODE = NCT6775_REG_FAN_MODE;
3516 data->REG_FAN_MIN = NCT6776_REG_FAN_MIN;
3517 data->REG_FAN_PULSES = NCT6779_REG_FAN_PULSES;
3518 data->FAN_PULSE_SHIFT = NCT6775_FAN_PULSE_SHIFT;
3519 data->REG_FAN_TIME[0] = NCT6775_REG_FAN_STOP_TIME;
3520 data->REG_FAN_TIME[1] = NCT6775_REG_FAN_STEP_UP_TIME;
3521 data->REG_FAN_TIME[2] = NCT6775_REG_FAN_STEP_DOWN_TIME;
3522 data->REG_TOLERANCE_H = NCT6776_REG_TOLERANCE_H;
3523 data->REG_PWM[0] = NCT6775_REG_PWM;
3524 data->REG_PWM[1] = NCT6775_REG_FAN_START_OUTPUT;
3525 data->REG_PWM[2] = NCT6775_REG_FAN_STOP_OUTPUT;
3526 data->REG_PWM[5] = NCT6775_REG_WEIGHT_DUTY_STEP;
3527 data->REG_PWM[6] = NCT6776_REG_WEIGHT_DUTY_BASE;
3528 data->REG_PWM_READ = NCT6775_REG_PWM_READ;
3529 data->REG_PWM_MODE = NCT6776_REG_PWM_MODE;
3530 data->PWM_MODE_MASK = NCT6776_PWM_MODE_MASK;
3531 data->REG_AUTO_TEMP = NCT6775_REG_AUTO_TEMP;
3532 data->REG_AUTO_PWM = NCT6775_REG_AUTO_PWM;
3533 data->REG_CRITICAL_TEMP = NCT6775_REG_CRITICAL_TEMP;
3534 data->REG_CRITICAL_TEMP_TOLERANCE
3535 = NCT6775_REG_CRITICAL_TEMP_TOLERANCE;
3536 data->REG_CRITICAL_PWM_ENABLE = NCT6779_REG_CRITICAL_PWM_ENABLE;
3537 data->CRITICAL_PWM_ENABLE_MASK
3538 = NCT6779_CRITICAL_PWM_ENABLE_MASK;
3539 data->REG_CRITICAL_PWM = NCT6779_REG_CRITICAL_PWM;
3540 data->REG_TEMP_OFFSET = NCT6779_REG_TEMP_OFFSET;
3541 data->REG_TEMP_SOURCE = NCT6775_REG_TEMP_SOURCE;
3542 data->REG_TEMP_SEL = NCT6775_REG_TEMP_SEL;
3543 data->REG_WEIGHT_TEMP_SEL = NCT6775_REG_WEIGHT_TEMP_SEL;
3544 data->REG_WEIGHT_TEMP[0] = NCT6775_REG_WEIGHT_TEMP_STEP;
3545 data->REG_WEIGHT_TEMP[1] = NCT6775_REG_WEIGHT_TEMP_STEP_TOL;
3546 data->REG_WEIGHT_TEMP[2] = NCT6775_REG_WEIGHT_TEMP_BASE;
3547 data->REG_ALARM = NCT6779_REG_ALARM;
3548 data->REG_BEEP = NCT6776_REG_BEEP;
3550 reg_temp = NCT6779_REG_TEMP;
3551 num_reg_temp = ARRAY_SIZE(NCT6779_REG_TEMP);
3552 reg_temp_over = NCT6779_REG_TEMP_OVER;
3553 reg_temp_hyst = NCT6779_REG_TEMP_HYST;
3554 reg_temp_config = NCT6779_REG_TEMP_CONFIG;
3555 reg_temp_alternate = NCT6779_REG_TEMP_ALTERNATE;
3556 reg_temp_crit = NCT6779_REG_TEMP_CRIT;
3562 data->have_in = (1 << data->in_num) - 1;
3563 data->have_temp = 0;
3566 * On some boards, not all available temperature sources are monitored,
3567 * even though some of the monitoring registers are unused.
3568 * Get list of unused monitoring registers, then detect if any fan
3569 * controls are configured to use unmonitored temperature sources.
3570 * If so, assign the unmonitored temperature sources to available
3571 * monitoring registers.
3575 for (i = 0; i < num_reg_temp; i++) {
3576 if (reg_temp[i] == 0)
3579 src = nct6775_read_value(data, data->REG_TEMP_SOURCE[i]) & 0x1f;
3580 if (!src || (mask & (1 << src)))
3581 available |= 1 << i;
3587 * Now find unmonitored temperature registers and enable monitoring
3588 * if additional monitoring registers are available.
3590 add_temp_sensors(data, data->REG_TEMP_SEL, &available, &mask);
3591 add_temp_sensors(data, data->REG_WEIGHT_TEMP_SEL, &available, &mask);
3594 s = NUM_TEMP_FIXED; /* First dynamic temperature attribute */
3595 for (i = 0; i < num_reg_temp; i++) {
3596 if (reg_temp[i] == 0)
3599 src = nct6775_read_value(data, data->REG_TEMP_SOURCE[i]) & 0x1f;
3600 if (!src || (mask & (1 << src)))
3603 if (src >= data->temp_label_num ||
3604 !strlen(data->temp_label[src])) {
3606 "Invalid temperature source %d at index %d, source register 0x%x, temp register 0x%x\n",
3607 src, i, data->REG_TEMP_SOURCE[i], reg_temp[i]);
3613 /* Use fixed index for SYSTIN(1), CPUTIN(2), AUXTIN(3) */
3614 if (src <= data->temp_fixed_num) {
3615 data->have_temp |= 1 << (src - 1);
3616 data->have_temp_fixed |= 1 << (src - 1);
3617 data->reg_temp[0][src - 1] = reg_temp[i];
3618 data->reg_temp[1][src - 1] = reg_temp_over[i];
3619 data->reg_temp[2][src - 1] = reg_temp_hyst[i];
3620 if (reg_temp_crit_h && reg_temp_crit_h[i])
3621 data->reg_temp[3][src - 1] = reg_temp_crit_h[i];
3622 else if (reg_temp_crit[src - 1])
3623 data->reg_temp[3][src - 1]
3624 = reg_temp_crit[src - 1];
3625 if (reg_temp_crit_l && reg_temp_crit_l[i])
3626 data->reg_temp[4][src - 1] = reg_temp_crit_l[i];
3627 data->reg_temp_config[src - 1] = reg_temp_config[i];
3628 data->temp_src[src - 1] = src;
3635 /* Use dynamic index for other sources */
3636 data->have_temp |= 1 << s;
3637 data->reg_temp[0][s] = reg_temp[i];
3638 data->reg_temp[1][s] = reg_temp_over[i];
3639 data->reg_temp[2][s] = reg_temp_hyst[i];
3640 data->reg_temp_config[s] = reg_temp_config[i];
3641 if (reg_temp_crit_h && reg_temp_crit_h[i])
3642 data->reg_temp[3][s] = reg_temp_crit_h[i];
3643 else if (reg_temp_crit[src - 1])
3644 data->reg_temp[3][s] = reg_temp_crit[src - 1];
3645 if (reg_temp_crit_l && reg_temp_crit_l[i])
3646 data->reg_temp[4][s] = reg_temp_crit_l[i];
3648 data->temp_src[s] = src;
3652 #ifdef USE_ALTERNATE
3654 * Go through the list of alternate temp registers and enable
3656 * The temperature is already monitored if the respective bit in <mask>
3659 for (i = 0; i < data->temp_label_num - 1; i++) {
3660 if (!reg_temp_alternate[i])
3662 if (mask & (1 << (i + 1)))
3664 if (i < data->temp_fixed_num) {
3665 if (data->have_temp & (1 << i))
3667 data->have_temp |= 1 << i;
3668 data->have_temp_fixed |= 1 << i;
3669 data->reg_temp[0][i] = reg_temp_alternate[i];
3670 if (i < num_reg_temp) {
3671 data->reg_temp[1][i] = reg_temp_over[i];
3672 data->reg_temp[2][i] = reg_temp_hyst[i];
3674 data->temp_src[i] = i + 1;
3678 if (s >= NUM_TEMP) /* Abort if no more space */
3681 data->have_temp |= 1 << s;
3682 data->reg_temp[0][s] = reg_temp_alternate[i];
3683 data->temp_src[s] = i + 1;
3686 #endif /* USE_ALTERNATE */
3688 /* Initialize the chip */
3689 nct6775_init_device(data);
3691 err = superio_enter(sio_data->sioreg);
3695 cr2a = superio_inb(sio_data->sioreg, 0x2a);
3696 switch (data->kind) {
3698 data->have_vid = (cr2a & 0x40);
3701 data->have_vid = (cr2a & 0x60) == 0x40;
3710 * We can get the VID input values directly at logical device D 0xe3.
3712 if (data->have_vid) {
3713 superio_select(sio_data->sioreg, NCT6775_LD_VID);
3714 data->vid = superio_inb(sio_data->sioreg, 0xe3);
3715 data->vrm = vid_which_vrm();
3721 superio_select(sio_data->sioreg, NCT6775_LD_HWM);
3722 tmp = superio_inb(sio_data->sioreg,
3723 NCT6775_REG_CR_FAN_DEBOUNCE);
3724 switch (data->kind) {
3736 superio_outb(sio_data->sioreg, NCT6775_REG_CR_FAN_DEBOUNCE,
3738 dev_info(&pdev->dev, "Enabled fan debounce for chip %s\n",
3742 nct6775_check_fan_inputs(sio_data, data);
3744 superio_exit(sio_data->sioreg);
3746 /* Read fan clock dividers immediately */
3747 nct6775_init_fan_common(dev, data);
3749 /* Register sysfs hooks */
3750 group = nct6775_create_attr_group(dev, &nct6775_pwm_template_group,
3752 if (IS_ERR(group)) {
3753 err = PTR_ERR(group);
3756 data->group_pwm = group;
3758 group = nct6775_create_attr_group(dev, &nct6775_in_template_group,
3759 fls(data->have_in));
3760 if (IS_ERR(group)) {
3761 err = PTR_ERR(group);
3764 data->group_in = group;
3766 group = nct6775_create_attr_group(dev, &nct6775_fan_template_group,
3767 fls(data->has_fan));
3768 if (IS_ERR(group)) {
3769 err = PTR_ERR(group);
3772 data->group_fan = group;
3774 group = nct6775_create_attr_group(dev, &nct6775_temp_template_group,
3775 fls(data->have_temp));
3776 if (IS_ERR(group)) {
3777 err = PTR_ERR(group);
3780 data->group_temp = group;
3782 err = sysfs_create_group(&dev->kobj, &nct6775_group_other);
3786 data->hwmon_dev = hwmon_device_register(dev);
3787 if (IS_ERR(data->hwmon_dev)) {
3788 err = PTR_ERR(data->hwmon_dev);
3795 nct6775_device_remove_files(dev);
3799 static int nct6775_remove(struct platform_device *pdev)
3801 struct nct6775_data *data = platform_get_drvdata(pdev);
3803 hwmon_device_unregister(data->hwmon_dev);
3804 nct6775_device_remove_files(&pdev->dev);
3810 static int nct6775_suspend(struct device *dev)
3812 struct nct6775_data *data = nct6775_update_device(dev);
3813 struct nct6775_sio_data *sio_data = dev->platform_data;
3815 mutex_lock(&data->update_lock);
3816 data->vbat = nct6775_read_value(data, data->REG_VBAT);
3817 if (sio_data->kind == nct6775) {
3818 data->fandiv1 = nct6775_read_value(data, NCT6775_REG_FANDIV1);
3819 data->fandiv2 = nct6775_read_value(data, NCT6775_REG_FANDIV2);
3821 mutex_unlock(&data->update_lock);
3826 static int nct6775_resume(struct device *dev)
3828 struct nct6775_data *data = dev_get_drvdata(dev);
3829 struct nct6775_sio_data *sio_data = dev->platform_data;
3832 mutex_lock(&data->update_lock);
3833 data->bank = 0xff; /* Force initial bank selection */
3835 /* Restore limits */
3836 for (i = 0; i < data->in_num; i++) {
3837 if (!(data->have_in & (1 << i)))
3840 nct6775_write_value(data, data->REG_IN_MINMAX[0][i],
3842 nct6775_write_value(data, data->REG_IN_MINMAX[1][i],
3846 for (i = 0; i < ARRAY_SIZE(data->fan_min); i++) {
3847 if (!(data->has_fan_min & (1 << i)))
3850 nct6775_write_value(data, data->REG_FAN_MIN[i],
3854 for (i = 0; i < NUM_TEMP; i++) {
3855 if (!(data->have_temp & (1 << i)))
3858 for (j = 1; j < ARRAY_SIZE(data->reg_temp); j++)
3859 if (data->reg_temp[j][i])
3860 nct6775_write_temp(data, data->reg_temp[j][i],
3864 /* Restore other settings */
3865 nct6775_write_value(data, data->REG_VBAT, data->vbat);
3866 if (sio_data->kind == nct6775) {
3867 nct6775_write_value(data, NCT6775_REG_FANDIV1, data->fandiv1);
3868 nct6775_write_value(data, NCT6775_REG_FANDIV2, data->fandiv2);
3871 /* Force re-reading all values */
3872 data->valid = false;
3873 mutex_unlock(&data->update_lock);
3878 static const struct dev_pm_ops nct6775_dev_pm_ops = {
3879 .suspend = nct6775_suspend,
3880 .resume = nct6775_resume,
3883 #define NCT6775_DEV_PM_OPS (&nct6775_dev_pm_ops)
3885 #define NCT6775_DEV_PM_OPS NULL
3886 #endif /* CONFIG_PM */
3888 static struct platform_driver nct6775_driver = {
3890 .owner = THIS_MODULE,
3892 .pm = NCT6775_DEV_PM_OPS,
3894 .probe = nct6775_probe,
3895 .remove = nct6775_remove,
3898 static const char * const nct6775_sio_names[] __initconst = {
3905 /* nct6775_find() looks for a '627 in the Super-I/O config space */
3906 static int __init nct6775_find(int sioaddr, struct nct6775_sio_data *sio_data)
3912 err = superio_enter(sioaddr);
3919 val = (superio_inb(sioaddr, SIO_REG_DEVID) << 8)
3920 | superio_inb(sioaddr, SIO_REG_DEVID + 1);
3921 switch (val & SIO_ID_MASK) {
3922 case SIO_NCT6106_ID:
3923 sio_data->kind = nct6106;
3925 case SIO_NCT6775_ID:
3926 sio_data->kind = nct6775;
3928 case SIO_NCT6776_ID:
3929 sio_data->kind = nct6776;
3931 case SIO_NCT6779_ID:
3932 sio_data->kind = nct6779;
3936 pr_debug("unsupported chip ID: 0x%04x\n", val);
3937 superio_exit(sioaddr);
3941 /* We have a known chip, find the HWM I/O address */
3942 superio_select(sioaddr, NCT6775_LD_HWM);
3943 val = (superio_inb(sioaddr, SIO_REG_ADDR) << 8)
3944 | superio_inb(sioaddr, SIO_REG_ADDR + 1);
3945 addr = val & IOREGION_ALIGNMENT;
3947 pr_err("Refusing to enable a Super-I/O device with a base I/O port 0\n");
3948 superio_exit(sioaddr);
3952 /* Activate logical device if needed */
3953 val = superio_inb(sioaddr, SIO_REG_ENABLE);
3954 if (!(val & 0x01)) {
3955 pr_warn("Forcibly enabling Super-I/O. Sensor is probably unusable.\n");
3956 superio_outb(sioaddr, SIO_REG_ENABLE, val | 0x01);
3959 superio_exit(sioaddr);
3960 pr_info("Found %s or compatible chip at %#x:%#x\n",
3961 nct6775_sio_names[sio_data->kind], sioaddr, addr);
3962 sio_data->sioreg = sioaddr;
3968 * when Super-I/O functions move to a separate file, the Super-I/O
3969 * bus will manage the lifetime of the device and this module will only keep
3970 * track of the nct6775 driver. But since we platform_device_alloc(), we
3971 * must keep track of the device
3973 static struct platform_device *pdev[2];
3975 static int __init sensors_nct6775_init(void)
3980 struct resource res;
3981 struct nct6775_sio_data sio_data;
3982 int sioaddr[2] = { 0x2e, 0x4e };
3984 err = platform_driver_register(&nct6775_driver);
3989 * initialize sio_data->kind and sio_data->sioreg.
3991 * when Super-I/O functions move to a separate file, the Super-I/O
3992 * driver will probe 0x2e and 0x4e and auto-detect the presence of a
3993 * nct6775 hardware monitor, and call probe()
3995 for (i = 0; i < ARRAY_SIZE(pdev); i++) {
3996 address = nct6775_find(sioaddr[i], &sio_data);
4002 pdev[i] = platform_device_alloc(DRVNAME, address);
4005 goto exit_device_put;
4008 err = platform_device_add_data(pdev[i], &sio_data,
4009 sizeof(struct nct6775_sio_data));
4011 goto exit_device_put;
4013 memset(&res, 0, sizeof(res));
4015 res.start = address + IOREGION_OFFSET;
4016 res.end = address + IOREGION_OFFSET + IOREGION_LENGTH - 1;
4017 res.flags = IORESOURCE_IO;
4019 err = acpi_check_resource_conflict(&res);
4021 platform_device_put(pdev[i]);
4026 err = platform_device_add_resources(pdev[i], &res, 1);
4028 goto exit_device_put;
4030 /* platform_device_add calls probe() */
4031 err = platform_device_add(pdev[i]);
4033 goto exit_device_put;
4037 goto exit_unregister;
4043 for (i = 0; i < ARRAY_SIZE(pdev); i++) {
4045 platform_device_put(pdev[i]);
4048 platform_driver_unregister(&nct6775_driver);
4052 static void __exit sensors_nct6775_exit(void)
4056 for (i = 0; i < ARRAY_SIZE(pdev); i++) {
4058 platform_device_unregister(pdev[i]);
4060 platform_driver_unregister(&nct6775_driver);
4063 MODULE_AUTHOR("Guenter Roeck <linux@roeck-us.net>");
4064 MODULE_DESCRIPTION("NCT6775F/NCT6776F/NCT6779D driver");
4065 MODULE_LICENSE("GPL");
4067 module_init(sensors_nct6775_init);
4068 module_exit(sensors_nct6775_exit);