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-2011 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 * nct6775f 9 4 3 9 0xb470 0xc1 0x5ca3
37 * nct6776f 9 5 3 9 0xc330 0xc1 0x5ca3
38 * nct6779d 15 5 5 8 0xc560 0xc1 0x5ca3
41 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
43 #include <linux/module.h>
44 #include <linux/init.h>
45 #include <linux/slab.h>
46 #include <linux/jiffies.h>
47 #include <linux/platform_device.h>
48 #include <linux/hwmon.h>
49 #include <linux/hwmon-sysfs.h>
50 #include <linux/hwmon-vid.h>
51 #include <linux/err.h>
52 #include <linux/mutex.h>
53 #include <linux/acpi.h>
57 enum kinds { nct6775, nct6776, nct6779, };
59 /* used to set data->name = nct6775_device_names[data->sio_kind] */
60 static const char * const nct6775_device_names[] = {
66 static unsigned short force_id;
67 module_param(force_id, ushort, 0);
68 MODULE_PARM_DESC(force_id, "Override the detected device ID");
70 static unsigned short fan_debounce;
71 module_param(fan_debounce, ushort, 0);
72 MODULE_PARM_DESC(fan_debounce, "Enable debouncing for fan RPM signal");
74 #define DRVNAME "nct6775"
77 * Super-I/O constants and functions
80 #define NCT6775_LD_HWM 0x0b
81 #define NCT6775_LD_VID 0x0d
83 #define SIO_REG_LDSEL 0x07 /* Logical device select */
84 #define SIO_REG_DEVID 0x20 /* Device ID (2 bytes) */
85 #define SIO_REG_EN_VRM10 0x2C /* GPIO3, GPIO4 selection */
86 #define SIO_REG_ENABLE 0x30 /* Logical device enable */
87 #define SIO_REG_ADDR 0x60 /* Logical device address (2 bytes) */
88 #define SIO_REG_VID_CTRL 0xF0 /* VID control */
89 #define SIO_REG_VID_DATA 0xF1 /* VID data */
91 #define SIO_NCT6775_ID 0xb470
92 #define SIO_NCT6776_ID 0xc330
93 #define SIO_NCT6779_ID 0xc560
94 #define SIO_ID_MASK 0xFFF0
97 superio_outb(int ioreg, int reg, int val)
100 outb(val, ioreg + 1);
104 superio_inb(int ioreg, int reg)
107 return inb(ioreg + 1);
111 superio_select(int ioreg, int ld)
113 outb(SIO_REG_LDSEL, ioreg);
118 superio_enter(int ioreg)
125 superio_exit(int ioreg)
129 outb(0x02, ioreg + 1);
136 #define IOREGION_ALIGNMENT (~7)
137 #define IOREGION_OFFSET 5
138 #define IOREGION_LENGTH 2
139 #define ADDR_REG_OFFSET 0
140 #define DATA_REG_OFFSET 1
142 #define NCT6775_REG_BANK 0x4E
143 #define NCT6775_REG_CONFIG 0x40
146 * Not currently used:
147 * REG_MAN_ID has the value 0x5ca3 for all supported chips.
148 * REG_CHIP_ID == 0x88/0xa1/0xc1 depending on chip model.
149 * REG_MAN_ID is at port 0x4f
150 * REG_CHIP_ID is at port 0x58
153 static const u16 NCT6775_REG_FAN_MIN[] = { 0x3b, 0x3c, 0x3d };
155 /* Voltage min/max registers for nr=7..14 are in bank 5 */
156 #define NCT6775_REG_IN_MAX(nr) ((nr < 7) ? (0x2b + (nr) * 2) : \
157 (0x554 + (((nr) - 7) * 2)))
158 #define NCT6775_REG_IN_MIN(nr) ((nr < 7) ? (0x2c + (nr) * 2) : \
159 (0x555 + (((nr) - 7) * 2)))
161 static const u16 NCT6775_REG_IN[] = {
162 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x550, 0x551, 0x552
165 static const u16 NCT6779_REG_IN[] = {
166 0x480, 0x481, 0x482, 0x483, 0x484, 0x485, 0x486, 0x487,
167 0x488, 0x489, 0x48a, 0x48b, 0x48c, 0x48d, 0x48e
170 #define NCT6775_REG_VBAT 0x5D
171 #define NCT6775_REG_DIODE 0x59
173 #define NCT6775_REG_FANDIV1 0x506
174 #define NCT6775_REG_FANDIV2 0x507
176 #define NCT6775_REG_FAN_DEBOUNCE 0xf0
178 #define NCT6775_REG_ALARM1 0x459
179 #define NCT6775_REG_ALARM2 0x45A
180 #define NCT6775_REG_ALARM3 0x45B
181 #define NCT6779_REG_ALARM4 0x568
183 #define NCT6775_REG_CASEOPEN_DET 0x42 /* SMI STATUS #2 */
184 #define NCT6775_REG_CASEOPEN_CLR 0x46 /* SMI MASK #3 */
186 /* DC or PWM output fan configuration */
187 static const u8 NCT6775_REG_PWM_ENABLE[] = {
188 0x04, /* SYSFAN output mode */
189 0x04, /* CPUFAN output mode */
190 0x12, /* AUXFAN output mode */
193 static const u8 NCT6775_PWM_MODE_SHIFT[] = { 0, 1, 0 };
195 /* Advanced Fan control, some values are common for all fans */
197 static const u16 NCT6775_REG_TARGET[] = { 0x101, 0x201, 0x301, 0x801, 0x901 };
198 static const u16 NCT6775_REG_FAN_MODE[] = { 0x102, 0x202, 0x302, 0x802, 0x902 };
199 static const u16 NCT6775_REG_FAN_STEP_DOWN_TIME[] = {
200 0x103, 0x203, 0x303, 0x803, 0x903 };
201 static const u16 NCT6775_REG_FAN_STEP_UP_TIME[] = {
202 0x104, 0x204, 0x304, 0x804, 0x904 };
203 static const u16 NCT6775_REG_FAN_STOP_OUTPUT[] = {
204 0x105, 0x205, 0x305, 0x805, 0x905 };
205 static const u16 NCT6775_REG_FAN_START_OUTPUT[] = {
206 0x106, 0x206, 0x306, 0x806, 0x906 };
207 static const u16 NCT6775_REG_FAN_STOP_TIME[] = {
208 0x107, 0x207, 0x307, 0x807, 0x907 };
209 static const u16 NCT6775_REG_PWM[] = { 0x109, 0x209, 0x309, 0x809, 0x909 };
211 static const u16 NCT6775_REG_FAN_MAX_OUTPUT[] = { 0x10a, 0x20a, 0x30a };
212 static const u16 NCT6775_REG_FAN_STEP_OUTPUT[] = { 0x10b, 0x20b, 0x30b };
213 static const u16 NCT6775_REG_FAN[] = { 0x630, 0x632, 0x634, 0x636, 0x638 };
215 static const u16 NCT6776_REG_FAN_MIN[] = { 0x63a, 0x63c, 0x63e, 0x640, 0x642};
217 static const u16 NCT6779_REG_TOLERANCE_H[] = {
218 0x10c, 0x20c, 0x30c, 0x40c, 0x50c };
220 static const u16 NCT6779_REG_FAN[] = { 0x4c0, 0x4c2, 0x4c4, 0x4c6, 0x4c8 };
222 static const u16 NCT6775_REG_TEMP[]
223 = { 0x27, 0x150, 0x250, 0x73, 0x75, 0x77, 0x62b, 0x62c, 0x62d };
224 static const u16 NCT6775_REG_TEMP_CONFIG[]
225 = { 0, 0x152, 0x252, 0, 0, 0, 0x628, 0x629, 0x62A };
226 static const u16 NCT6775_REG_TEMP_HYST[]
227 = { 0x3a, 0x153, 0x253, 0, 0, 0, 0x673, 0x678, 0x67D };
228 static const u16 NCT6775_REG_TEMP_OVER[]
229 = { 0x39, 0x155, 0x255, 0, 0, 0, 0x672, 0x677, 0x67C };
230 static const u16 NCT6775_REG_TEMP_SOURCE[]
231 = { 0x621, 0x622, 0x623, 0x100, 0x200, 0x300, 0x624, 0x625, 0x626 };
233 static const u16 NCT6776_REG_TEMP_CONFIG[]
234 = { 0x18, 0x152, 0x252, 0, 0, 0, 0x628, 0x629, 0x62A };
236 static const u16 NCT6779_REG_TEMP[]
237 = { 0x27, 0x150, 0x73, 0x75, 0x77, 0x79, 0x7b };
238 static const u16 NCT6779_REG_TEMP_CONFIG[]
239 = { 0x18, 0x152, 0, 0, 0, 0, 0 };
240 static const u16 NCT6779_REG_TEMP_SOURCE[]
241 = { 0x621, 0x622, 0x100, 0x200, 0x300, 0x800, 0x900 };
242 static const u16 NCT6779_REG_TEMP_HYST[]
243 = { 0x3a, 0x153, 0, 0, 0, 0, 0, 0, 0 };
244 static const u16 NCT6779_REG_TEMP_OVER[]
245 = { 0x39, 0x155, 0, 0, 0, 0, 0, 0, 0 };
247 static const u16 NCT6775_REG_AUTO_BASE[] = {
248 0x100, 0x200, 0x300, 0x800, 0x900 };
250 #define NCT6775_REG_AUTO_TEMP(nr, p) (NCT6775_REG_AUTO_BASE[nr] + 0x21 + (p))
251 #define NCT6775_REG_AUTO_PWM(nr, p) (NCT6775_REG_AUTO_BASE[nr] + 0x27 + (p))
253 static const u16 NCT6775_REG_CRITICAL_ENAB[] = { 0x134, 0x234, 0x334 };
254 static const u16 NCT6775_REG_CRITICAL_TEMP[] = {
255 0x135, 0x235, 0x335, 0x835, 0x935 };
256 static const u16 NCT6775_REG_CRITICAL_TEMP_TOLERANCE[] = {
257 0x138, 0x238, 0x338, 0x838, 0x938 };
259 static const u16 NCT6779_REG_CRITICAL_PWM_ENABLE[] = {
260 0x136, 0x236, 0x336, 0x836, 0x936 };
261 static const u16 NCT6779_REG_CRITICAL_PWM[] = {
262 0x137, 0x237, 0x337, 0x837, 0x937 };
264 static const char *const nct6775_temp_label[] = {
278 "PCH_CHIP_CPU_MAX_TEMP",
288 static const char *const nct6776_temp_label[] = {
303 "PCH_CHIP_CPU_MAX_TEMP",
314 static const char *const nct6779_temp_label[] = {
333 "PCH_CHIP_CPU_MAX_TEMP",
344 #define NUM_REG_TEMP ARRAY_SIZE(NCT6775_REG_TEMP)
346 static int is_word_sized(u16 reg)
348 return ((((reg & 0xff00) == 0x100
349 || (reg & 0xff00) == 0x200)
350 && ((reg & 0x00ff) == 0x50
351 || (reg & 0x00ff) == 0x53
352 || (reg & 0x00ff) == 0x55))
353 || (reg & 0xfff0) == 0x630
354 || (reg & 0xfff0) == 0x4c0
355 || reg == 0x640 || reg == 0x642
356 || ((reg & 0xfff0) == 0x650
357 && (reg & 0x000f) >= 0x06)
358 || reg == 0x73 || reg == 0x75 || reg == 0x77 || reg == 0x77
367 /* 1 is PWM mode, output in ms */
368 static inline unsigned int step_time_from_reg(u8 reg, u8 mode)
370 return mode ? 100 * reg : 400 * reg;
373 static inline u8 step_time_to_reg(unsigned int msec, u8 mode)
375 return SENSORS_LIMIT((mode ? (msec + 50) / 100 :
376 (msec + 200) / 400), 1, 255);
379 static unsigned int fan_from_reg8(u16 reg, unsigned int divreg)
381 if (reg == 0 || reg == 255)
383 return 1350000U / (reg << divreg);
386 static unsigned int fan_from_reg13(u16 reg, unsigned int divreg)
388 if ((reg & 0xff1f) == 0xff1f)
391 reg = (reg & 0x1f) | ((reg & 0xff00) >> 3);
396 return 1350000U / reg;
399 static unsigned int fan_from_reg16(u16 reg, unsigned int divreg)
401 if (reg == 0 || reg == 0xffff)
405 * Even though the registers are 16 bit wide, the fan divisor
408 return 1350000U / (reg << divreg);
411 static inline unsigned int
418 * Some of the voltage inputs have internal scaling, the tables below
419 * contain 8 (the ADC LSB in mV) * scaling factor * 100
421 static const u16 scale_in[15] = {
422 800, 800, 1600, 1600, 800, 800, 800, 1600, 1600, 800, 800, 800, 800,
426 static inline long in_from_reg(u8 reg, u8 nr)
428 return DIV_ROUND_CLOSEST(reg * scale_in[nr], 100);
431 static inline u8 in_to_reg(u32 val, u8 nr)
433 return SENSORS_LIMIT(DIV_ROUND_CLOSEST(val * 100, scale_in[nr]), 0,
438 * Data structures and manipulation thereof
441 struct nct6775_data {
442 int addr; /* IO base of hw monitor block */
445 struct device *hwmon_dev;
448 u16 reg_temp[NUM_REG_TEMP];
449 u16 reg_temp_over[NUM_REG_TEMP];
450 u16 reg_temp_hyst[NUM_REG_TEMP];
451 u16 reg_temp_config[NUM_REG_TEMP];
452 u8 temp_src[NUM_REG_TEMP];
453 const char * const *temp_label;
457 const u16 *REG_TARGET;
459 const u16 *REG_FAN_MIN;
460 const u16 *REG_FAN_START_OUTPUT;
461 const u16 *REG_FAN_STOP_OUTPUT;
462 const u16 *REG_FAN_MAX_OUTPUT;
463 const u16 *REG_FAN_STEP_OUTPUT;
465 unsigned int (*fan_from_reg)(u16 reg, unsigned int divreg);
466 unsigned int (*fan_from_reg_min)(u16 reg, unsigned int divreg);
468 struct mutex update_lock;
469 char valid; /* !=0 if following fields are valid */
470 unsigned long last_updated; /* In jiffies */
472 /* Register values */
473 u8 bank; /* current register bank */
474 u8 in_num; /* number of in inputs we have */
475 u8 in[15]; /* Register value */
476 u8 in_max[15]; /* Register value */
477 u8 in_min[15]; /* Register value */
482 u8 has_fan; /* some fan inputs can be disabled */
483 u8 has_fan_min; /* some fans don't have min register */
488 s16 temp_max_hyst[9];
492 u8 pwm_mode[5]; /* 0->DC variable voltage, 1->PWM variable duty cycle */
493 u8 pwm_enable[5]; /* 1->manual
494 * 2->thermal cruise mode (also called SmartFan I)
495 * 3->fan speed cruise mode
496 * 4->variable thermal cruise (also called
498 * 5->enhanced variable thermal cruise (also called
501 u8 pwm_num; /* number of pwm */
506 u8 fan_start_output[5]; /* minimum fan speed when spinning up */
507 u8 fan_stop_output[5]; /* minimum fan speed when spinning down */
508 u8 fan_stop_time[5]; /* time at minimum before disabling fan */
509 u8 fan_step_up_time[5];
510 u8 fan_step_down_time[5];
511 u8 fan_max_output[5]; /* maximum fan speed */
512 u8 fan_step_output[5]; /* rate of change output value */
514 /* Automatic fan speed control registers */
526 struct nct6775_sio_data {
532 * On older chips, only registers 0x50-0x5f are banked.
533 * On more recent chips, all registers are banked.
534 * Assume that is the case and set the bank number for each access.
535 * Cache the bank number so it only needs to be set if it changes.
537 static inline void nct6775_set_bank(struct nct6775_data *data, u16 reg)
540 if (data->bank != bank) {
541 outb_p(NCT6775_REG_BANK, data->addr + ADDR_REG_OFFSET);
542 outb_p(bank, data->addr + DATA_REG_OFFSET);
547 static u16 nct6775_read_value(struct nct6775_data *data, u16 reg)
549 int res, word_sized = is_word_sized(reg);
551 mutex_lock(&data->lock);
553 nct6775_set_bank(data, reg);
554 outb_p(reg & 0xff, data->addr + ADDR_REG_OFFSET);
555 res = inb_p(data->addr + DATA_REG_OFFSET);
557 outb_p((reg & 0xff) + 1,
558 data->addr + ADDR_REG_OFFSET);
559 res = (res << 8) + inb_p(data->addr + DATA_REG_OFFSET);
562 mutex_unlock(&data->lock);
566 static int nct6775_write_value(struct nct6775_data *data, u16 reg,
569 int word_sized = is_word_sized(reg);
571 mutex_lock(&data->lock);
573 nct6775_set_bank(data, reg);
574 outb_p(reg & 0xff, data->addr + ADDR_REG_OFFSET);
576 outb_p(value >> 8, data->addr + DATA_REG_OFFSET);
577 outb_p((reg & 0xff) + 1,
578 data->addr + ADDR_REG_OFFSET);
580 outb_p(value & 0xff, data->addr + DATA_REG_OFFSET);
582 mutex_unlock(&data->lock);
586 /* We left-align 8-bit temperature values to make the code simpler */
587 static u16 nct6775_read_temp(struct nct6775_data *data, u16 reg)
591 res = nct6775_read_value(data, reg);
592 if (!is_word_sized(reg))
598 static int nct6775_write_temp(struct nct6775_data *data, u16 reg,
601 if (!is_word_sized(reg))
603 return nct6775_write_value(data, reg, value);
606 /* This function assumes that the caller holds data->update_lock */
607 static void nct6775_write_fan_div(struct nct6775_data *data, int nr)
613 reg = (nct6775_read_value(data, NCT6775_REG_FANDIV1) & 0x70)
614 | (data->fan_div[0] & 0x7);
615 nct6775_write_value(data, NCT6775_REG_FANDIV1, reg);
618 reg = (nct6775_read_value(data, NCT6775_REG_FANDIV1) & 0x7)
619 | ((data->fan_div[1] << 4) & 0x70);
620 nct6775_write_value(data, NCT6775_REG_FANDIV1, reg);
622 reg = (nct6775_read_value(data, NCT6775_REG_FANDIV2) & 0x70)
623 | (data->fan_div[2] & 0x7);
624 nct6775_write_value(data, NCT6775_REG_FANDIV2, reg);
627 reg = (nct6775_read_value(data, NCT6775_REG_FANDIV2) & 0x7)
628 | ((data->fan_div[3] << 4) & 0x70);
629 nct6775_write_value(data, NCT6775_REG_FANDIV2, reg);
634 static void nct6775_write_fan_div_common(struct device *dev,
635 struct nct6775_data *data, int nr)
637 struct nct6775_sio_data *sio_data = dev->platform_data;
639 if (sio_data->kind == nct6775)
640 nct6775_write_fan_div(data, nr);
643 static void nct6775_update_fan_div(struct nct6775_data *data)
647 i = nct6775_read_value(data, NCT6775_REG_FANDIV1);
648 data->fan_div[0] = i & 0x7;
649 data->fan_div[1] = (i & 0x70) >> 4;
650 i = nct6775_read_value(data, NCT6775_REG_FANDIV2);
651 data->fan_div[2] = i & 0x7;
652 if (data->has_fan & (1<<3))
653 data->fan_div[3] = (i & 0x70) >> 4;
656 static void nct6775_update_fan_div_common(struct device *dev,
657 struct nct6775_data *data)
659 struct nct6775_sio_data *sio_data = dev->platform_data;
661 if (sio_data->kind == nct6775)
662 nct6775_update_fan_div(data);
665 static void nct6775_update_pwm(struct device *dev)
667 struct nct6775_data *data = dev_get_drvdata(dev);
668 struct nct6775_sio_data *sio_data = dev->platform_data;
670 int pwmcfg, fanmodecfg, tol;
672 for (i = 0; i < data->pwm_num; i++) {
675 !(nct6775_read_value(data,
676 NCT6775_REG_PWM_ENABLE[0]) & 1);
680 data->pwm_mode[i] = pwmcfg;
682 fanmodecfg = nct6775_read_value(data,
683 NCT6775_REG_FAN_MODE[i]);
684 data->pwm_enable[i] = ((fanmodecfg >> 4) & 7) + 1;
685 data->tolerance[i][0] = fanmodecfg & 0x0f;
686 if (sio_data->kind == nct6779) {
687 tol = nct6775_read_value(data,
688 NCT6779_REG_TOLERANCE_H[i]);
689 data->tolerance[i][0] |= (tol & 0x70) >> 1;
691 data->tolerance[i][1] =
692 nct6775_read_value(data,
693 NCT6775_REG_CRITICAL_TEMP_TOLERANCE[i]);
694 data->pwm[i] = nct6775_read_value(data, NCT6775_REG_PWM[i]);
698 static void nct6775_update_pwm_limits(struct device *dev)
700 struct nct6775_data *data = dev_get_drvdata(dev);
701 struct nct6775_sio_data *sio_data = dev->platform_data;
705 for (i = 0; i < data->pwm_num; i++) {
706 if (!(data->has_pwm & (1 << i)))
709 data->fan_start_output[i] =
710 nct6775_read_value(data, data->REG_FAN_START_OUTPUT[i]);
711 data->fan_stop_output[i] =
712 nct6775_read_value(data, data->REG_FAN_STOP_OUTPUT[i]);
713 data->fan_stop_time[i] =
714 nct6775_read_value(data, NCT6775_REG_FAN_STOP_TIME[i]);
715 data->fan_step_up_time[i] =
716 nct6775_read_value(data, NCT6775_REG_FAN_STEP_UP_TIME[i]);
717 data->fan_step_down_time[i] =
718 nct6775_read_value(data, NCT6775_REG_FAN_STEP_DOWN_TIME[i]);
720 if (data->REG_FAN_MAX_OUTPUT)
721 data->fan_max_output[i] =
722 nct6775_read_value(data,
723 data->REG_FAN_MAX_OUTPUT[i]);
725 if (data->REG_FAN_STEP_OUTPUT)
726 data->fan_step_output[i] =
727 nct6775_read_value(data,
728 data->REG_FAN_STEP_OUTPUT[i]);
730 data->target_temp[i] =
731 nct6775_read_value(data,
732 data->REG_TARGET[i]) &
733 (data->pwm_mode[i] == 1 ? 0x7f : 0xff);
734 for (j = 0; j < data->auto_pwm_num; j++) {
735 data->auto_pwm[i][j] =
736 nct6775_read_value(data, NCT6775_REG_AUTO_PWM(i, j));
737 data->auto_temp[i][j] =
738 nct6775_read_value(data, NCT6775_REG_AUTO_TEMP(i, j));
740 /* handle critical auto_pwm temperature data */
741 data->auto_temp[i][data->auto_pwm_num] =
742 nct6775_read_value(data, NCT6775_REG_CRITICAL_TEMP[i]);
744 switch (sio_data->kind) {
746 reg = nct6775_read_value(data,
747 NCT6775_REG_CRITICAL_ENAB[i]);
748 data->auto_pwm[i][data->auto_pwm_num] =
749 (reg & 0x02) ? 0xff : 0x00;
752 data->auto_pwm[i][data->auto_pwm_num] = 0xff;
755 reg = nct6775_read_value(data,
756 NCT6779_REG_CRITICAL_PWM_ENABLE[i]);
758 data->auto_pwm[i][data->auto_pwm_num] =
759 nct6775_read_value(data,
760 NCT6779_REG_CRITICAL_PWM[i]);
762 data->auto_pwm[i][data->auto_pwm_num] = 0xff;
768 static struct nct6775_data *nct6775_update_device(struct device *dev)
770 struct nct6775_data *data = dev_get_drvdata(dev);
771 struct nct6775_sio_data *sio_data = dev->platform_data;
775 mutex_lock(&data->update_lock);
777 if (time_after(jiffies, data->last_updated + HZ + HZ/2)
779 /* Fan clock dividers */
780 nct6775_update_fan_div_common(dev, data);
782 /* Measured voltages and limits */
783 for (i = 0; i < data->in_num; i++) {
784 if (!(data->have_in & (1 << i)))
787 data->in[i] = nct6775_read_value(data,
789 data->in_min[i] = nct6775_read_value(data,
790 NCT6775_REG_IN_MIN(i));
791 data->in_max[i] = nct6775_read_value(data,
792 NCT6775_REG_IN_MAX(i));
795 /* Measured fan speeds and limits */
796 for (i = 0; i < 5; i++) {
799 if (!(data->has_fan & (1 << i)))
802 reg = nct6775_read_value(data, data->REG_FAN[i]);
803 data->rpm[i] = data->fan_from_reg(reg,
806 if (data->has_fan_min & (1 << i))
807 data->fan_min[i] = nct6775_read_value(data,
808 data->REG_FAN_MIN[i]);
811 * If we failed to measure the fan speed and clock
812 * divider can be increased, let's try that for next
815 if (data->has_fan_div
816 && (reg >= 0xff || (sio_data->kind == nct6775
818 && data->fan_div[i] < 0x07) {
819 dev_dbg(dev, "Increasing fan%d "
820 "clock divider from %u to %u\n",
821 i + 1, div_from_reg(data->fan_div[i]),
822 div_from_reg(data->fan_div[i] + 1));
824 nct6775_write_fan_div_common(dev, data, i);
825 /* Preserve min limit if possible */
826 if ((data->has_fan_min & (1 << i))
827 && data->fan_min[i] >= 2
828 && data->fan_min[i] != 255)
829 nct6775_write_value(data,
830 data->REG_FAN_MIN[i],
831 (data->fan_min[i] /= 2));
835 nct6775_update_pwm(dev);
836 nct6775_update_pwm_limits(dev);
838 /* Measured temperatures and limits */
839 for (i = 0; i < NUM_REG_TEMP; i++) {
840 if (!(data->have_temp & (1 << i)))
842 data->temp[i] = nct6775_read_temp(data,
844 if (data->reg_temp_over[i])
846 = nct6775_read_temp(data,
847 data->reg_temp_over[i]);
848 if (data->reg_temp_hyst[i])
849 data->temp_max_hyst[i]
850 = nct6775_read_temp(data,
851 data->reg_temp_hyst[i]);
854 data->alarms = nct6775_read_value(data,
855 NCT6775_REG_ALARM1) |
856 (nct6775_read_value(data,
857 NCT6775_REG_ALARM2) << 8) |
858 (nct6775_read_value(data,
859 NCT6775_REG_ALARM3) << 16);
860 if (sio_data->kind == nct6779) {
861 data->alarms |= nct6775_read_value(data,
862 NCT6779_REG_ALARM4) << 24;
865 data->caseopen = nct6775_read_value(data,
866 NCT6775_REG_CASEOPEN_DET);
868 data->last_updated = jiffies;
872 mutex_unlock(&data->update_lock);
877 * Sysfs callback functions
879 #define show_in_reg(reg) \
881 show_##reg(struct device *dev, struct device_attribute *attr, \
884 struct nct6775_data *data = nct6775_update_device(dev); \
885 struct sensor_device_attribute *sensor_attr = \
886 to_sensor_dev_attr(attr); \
887 int nr = sensor_attr->index; \
888 return sprintf(buf, "%ld\n", in_from_reg(data->reg[nr], nr)); \
894 #define store_in_reg(REG, reg) \
896 store_in_##reg(struct device *dev, struct device_attribute *attr, \
897 const char *buf, size_t count) \
899 struct nct6775_data *data = dev_get_drvdata(dev); \
900 struct sensor_device_attribute *sensor_attr = \
901 to_sensor_dev_attr(attr); \
902 int nr = sensor_attr->index; \
904 int err = kstrtoul(buf, 10, &val); \
907 mutex_lock(&data->update_lock); \
908 data->in_##reg[nr] = in_to_reg(val, nr); \
909 nct6775_write_value(data, NCT6775_REG_IN_##REG(nr), \
910 data->in_##reg[nr]); \
911 mutex_unlock(&data->update_lock); \
915 store_in_reg(MIN, min)
916 store_in_reg(MAX, max)
918 static ssize_t show_alarm(struct device *dev, struct device_attribute *attr,
921 struct nct6775_data *data = nct6775_update_device(dev);
922 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
923 int nr = sensor_attr->index;
924 return sprintf(buf, "%u\n", (data->alarms >> nr) & 0x01);
927 static SENSOR_DEVICE_ATTR(in0_input, S_IRUGO, show_in, NULL, 0);
928 static SENSOR_DEVICE_ATTR(in1_input, S_IRUGO, show_in, NULL, 1);
929 static SENSOR_DEVICE_ATTR(in2_input, S_IRUGO, show_in, NULL, 2);
930 static SENSOR_DEVICE_ATTR(in3_input, S_IRUGO, show_in, NULL, 3);
931 static SENSOR_DEVICE_ATTR(in4_input, S_IRUGO, show_in, NULL, 4);
932 static SENSOR_DEVICE_ATTR(in5_input, S_IRUGO, show_in, NULL, 5);
933 static SENSOR_DEVICE_ATTR(in6_input, S_IRUGO, show_in, NULL, 6);
934 static SENSOR_DEVICE_ATTR(in7_input, S_IRUGO, show_in, NULL, 7);
935 static SENSOR_DEVICE_ATTR(in8_input, S_IRUGO, show_in, NULL, 8);
936 static SENSOR_DEVICE_ATTR(in9_input, S_IRUGO, show_in, NULL, 9);
937 static SENSOR_DEVICE_ATTR(in10_input, S_IRUGO, show_in, NULL, 10);
938 static SENSOR_DEVICE_ATTR(in11_input, S_IRUGO, show_in, NULL, 11);
939 static SENSOR_DEVICE_ATTR(in12_input, S_IRUGO, show_in, NULL, 12);
940 static SENSOR_DEVICE_ATTR(in13_input, S_IRUGO, show_in, NULL, 13);
941 static SENSOR_DEVICE_ATTR(in14_input, S_IRUGO, show_in, NULL, 14);
943 static SENSOR_DEVICE_ATTR(in0_alarm, S_IRUGO, show_alarm, NULL, 0);
944 static SENSOR_DEVICE_ATTR(in1_alarm, S_IRUGO, show_alarm, NULL, 1);
945 static SENSOR_DEVICE_ATTR(in2_alarm, S_IRUGO, show_alarm, NULL, 2);
946 static SENSOR_DEVICE_ATTR(in3_alarm, S_IRUGO, show_alarm, NULL, 3);
947 static SENSOR_DEVICE_ATTR(in4_alarm, S_IRUGO, show_alarm, NULL, 8);
948 static SENSOR_DEVICE_ATTR(in5_alarm, S_IRUGO, show_alarm, NULL, 21);
949 static SENSOR_DEVICE_ATTR(in6_alarm, S_IRUGO, show_alarm, NULL, 20);
950 static SENSOR_DEVICE_ATTR(in7_alarm, S_IRUGO, show_alarm, NULL, 16);
951 static SENSOR_DEVICE_ATTR(in8_alarm, S_IRUGO, show_alarm, NULL, 17);
952 static SENSOR_DEVICE_ATTR(in9_alarm, S_IRUGO, show_alarm, NULL, 24);
953 static SENSOR_DEVICE_ATTR(in10_alarm, S_IRUGO, show_alarm, NULL, 25);
954 static SENSOR_DEVICE_ATTR(in11_alarm, S_IRUGO, show_alarm, NULL, 26);
955 static SENSOR_DEVICE_ATTR(in12_alarm, S_IRUGO, show_alarm, NULL, 27);
956 static SENSOR_DEVICE_ATTR(in13_alarm, S_IRUGO, show_alarm, NULL, 28);
957 static SENSOR_DEVICE_ATTR(in14_alarm, S_IRUGO, show_alarm, NULL, 29);
959 static SENSOR_DEVICE_ATTR(in0_min, S_IWUSR | S_IRUGO, show_in_min,
961 static SENSOR_DEVICE_ATTR(in1_min, S_IWUSR | S_IRUGO, show_in_min,
963 static SENSOR_DEVICE_ATTR(in2_min, S_IWUSR | S_IRUGO, show_in_min,
965 static SENSOR_DEVICE_ATTR(in3_min, S_IWUSR | S_IRUGO, show_in_min,
967 static SENSOR_DEVICE_ATTR(in4_min, S_IWUSR | S_IRUGO, show_in_min,
969 static SENSOR_DEVICE_ATTR(in5_min, S_IWUSR | S_IRUGO, show_in_min,
971 static SENSOR_DEVICE_ATTR(in6_min, S_IWUSR | S_IRUGO, show_in_min,
973 static SENSOR_DEVICE_ATTR(in7_min, S_IWUSR | S_IRUGO, show_in_min,
975 static SENSOR_DEVICE_ATTR(in8_min, S_IWUSR | S_IRUGO, show_in_min,
977 static SENSOR_DEVICE_ATTR(in9_min, S_IWUSR | S_IRUGO, show_in_min,
979 static SENSOR_DEVICE_ATTR(in10_min, S_IWUSR | S_IRUGO, show_in_min,
981 static SENSOR_DEVICE_ATTR(in11_min, S_IWUSR | S_IRUGO, show_in_min,
983 static SENSOR_DEVICE_ATTR(in12_min, S_IWUSR | S_IRUGO, show_in_min,
985 static SENSOR_DEVICE_ATTR(in13_min, S_IWUSR | S_IRUGO, show_in_min,
987 static SENSOR_DEVICE_ATTR(in14_min, S_IWUSR | S_IRUGO, show_in_min,
990 static SENSOR_DEVICE_ATTR(in0_max, S_IWUSR | S_IRUGO, show_in_max,
992 static SENSOR_DEVICE_ATTR(in1_max, S_IWUSR | S_IRUGO, show_in_max,
994 static SENSOR_DEVICE_ATTR(in2_max, S_IWUSR | S_IRUGO, show_in_max,
996 static SENSOR_DEVICE_ATTR(in3_max, S_IWUSR | S_IRUGO, show_in_max,
998 static SENSOR_DEVICE_ATTR(in4_max, S_IWUSR | S_IRUGO, show_in_max,
1000 static SENSOR_DEVICE_ATTR(in5_max, S_IWUSR | S_IRUGO, show_in_max,
1002 static SENSOR_DEVICE_ATTR(in6_max, S_IWUSR | S_IRUGO, show_in_max,
1004 static SENSOR_DEVICE_ATTR(in7_max, S_IWUSR | S_IRUGO, show_in_max,
1006 static SENSOR_DEVICE_ATTR(in8_max, S_IWUSR | S_IRUGO, show_in_max,
1008 static SENSOR_DEVICE_ATTR(in9_max, S_IWUSR | S_IRUGO, show_in_max,
1010 static SENSOR_DEVICE_ATTR(in10_max, S_IWUSR | S_IRUGO, show_in_max,
1012 static SENSOR_DEVICE_ATTR(in11_max, S_IWUSR | S_IRUGO, show_in_max,
1014 static SENSOR_DEVICE_ATTR(in12_max, S_IWUSR | S_IRUGO, show_in_max,
1016 static SENSOR_DEVICE_ATTR(in13_max, S_IWUSR | S_IRUGO, show_in_max,
1018 static SENSOR_DEVICE_ATTR(in14_max, S_IWUSR | S_IRUGO, show_in_max,
1021 static struct attribute *nct6775_attributes_in[15][5] = {
1023 &sensor_dev_attr_in0_input.dev_attr.attr,
1024 &sensor_dev_attr_in0_min.dev_attr.attr,
1025 &sensor_dev_attr_in0_max.dev_attr.attr,
1026 &sensor_dev_attr_in0_alarm.dev_attr.attr,
1030 &sensor_dev_attr_in1_input.dev_attr.attr,
1031 &sensor_dev_attr_in1_min.dev_attr.attr,
1032 &sensor_dev_attr_in1_max.dev_attr.attr,
1033 &sensor_dev_attr_in1_alarm.dev_attr.attr,
1037 &sensor_dev_attr_in2_input.dev_attr.attr,
1038 &sensor_dev_attr_in2_min.dev_attr.attr,
1039 &sensor_dev_attr_in2_max.dev_attr.attr,
1040 &sensor_dev_attr_in2_alarm.dev_attr.attr,
1044 &sensor_dev_attr_in3_input.dev_attr.attr,
1045 &sensor_dev_attr_in3_min.dev_attr.attr,
1046 &sensor_dev_attr_in3_max.dev_attr.attr,
1047 &sensor_dev_attr_in3_alarm.dev_attr.attr,
1051 &sensor_dev_attr_in4_input.dev_attr.attr,
1052 &sensor_dev_attr_in4_min.dev_attr.attr,
1053 &sensor_dev_attr_in4_max.dev_attr.attr,
1054 &sensor_dev_attr_in4_alarm.dev_attr.attr,
1058 &sensor_dev_attr_in5_input.dev_attr.attr,
1059 &sensor_dev_attr_in5_min.dev_attr.attr,
1060 &sensor_dev_attr_in5_max.dev_attr.attr,
1061 &sensor_dev_attr_in5_alarm.dev_attr.attr,
1065 &sensor_dev_attr_in6_input.dev_attr.attr,
1066 &sensor_dev_attr_in6_min.dev_attr.attr,
1067 &sensor_dev_attr_in6_max.dev_attr.attr,
1068 &sensor_dev_attr_in6_alarm.dev_attr.attr,
1072 &sensor_dev_attr_in7_input.dev_attr.attr,
1073 &sensor_dev_attr_in7_min.dev_attr.attr,
1074 &sensor_dev_attr_in7_max.dev_attr.attr,
1075 &sensor_dev_attr_in7_alarm.dev_attr.attr,
1079 &sensor_dev_attr_in8_input.dev_attr.attr,
1080 &sensor_dev_attr_in8_min.dev_attr.attr,
1081 &sensor_dev_attr_in8_max.dev_attr.attr,
1082 &sensor_dev_attr_in8_alarm.dev_attr.attr,
1086 &sensor_dev_attr_in9_input.dev_attr.attr,
1087 &sensor_dev_attr_in9_min.dev_attr.attr,
1088 &sensor_dev_attr_in9_max.dev_attr.attr,
1089 &sensor_dev_attr_in9_alarm.dev_attr.attr,
1093 &sensor_dev_attr_in10_input.dev_attr.attr,
1094 &sensor_dev_attr_in10_min.dev_attr.attr,
1095 &sensor_dev_attr_in10_max.dev_attr.attr,
1096 &sensor_dev_attr_in10_alarm.dev_attr.attr,
1100 &sensor_dev_attr_in11_input.dev_attr.attr,
1101 &sensor_dev_attr_in11_min.dev_attr.attr,
1102 &sensor_dev_attr_in11_max.dev_attr.attr,
1103 &sensor_dev_attr_in11_alarm.dev_attr.attr,
1107 &sensor_dev_attr_in12_input.dev_attr.attr,
1108 &sensor_dev_attr_in12_min.dev_attr.attr,
1109 &sensor_dev_attr_in12_max.dev_attr.attr,
1110 &sensor_dev_attr_in12_alarm.dev_attr.attr,
1114 &sensor_dev_attr_in13_input.dev_attr.attr,
1115 &sensor_dev_attr_in13_min.dev_attr.attr,
1116 &sensor_dev_attr_in13_max.dev_attr.attr,
1117 &sensor_dev_attr_in13_alarm.dev_attr.attr,
1121 &sensor_dev_attr_in14_input.dev_attr.attr,
1122 &sensor_dev_attr_in14_min.dev_attr.attr,
1123 &sensor_dev_attr_in14_max.dev_attr.attr,
1124 &sensor_dev_attr_in14_alarm.dev_attr.attr,
1129 static const struct attribute_group nct6775_group_in[15] = {
1130 { .attrs = nct6775_attributes_in[0] },
1131 { .attrs = nct6775_attributes_in[1] },
1132 { .attrs = nct6775_attributes_in[2] },
1133 { .attrs = nct6775_attributes_in[3] },
1134 { .attrs = nct6775_attributes_in[4] },
1135 { .attrs = nct6775_attributes_in[5] },
1136 { .attrs = nct6775_attributes_in[6] },
1137 { .attrs = nct6775_attributes_in[7] },
1138 { .attrs = nct6775_attributes_in[8] },
1139 { .attrs = nct6775_attributes_in[9] },
1140 { .attrs = nct6775_attributes_in[10] },
1141 { .attrs = nct6775_attributes_in[11] },
1142 { .attrs = nct6775_attributes_in[12] },
1143 { .attrs = nct6775_attributes_in[13] },
1144 { .attrs = nct6775_attributes_in[14] },
1148 show_fan(struct device *dev, struct device_attribute *attr, char *buf)
1150 struct nct6775_data *data = nct6775_update_device(dev);
1151 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
1152 int nr = sensor_attr->index;
1153 return sprintf(buf, "%d\n", data->rpm[nr]);
1157 show_fan_min(struct device *dev, struct device_attribute *attr, char *buf)
1159 struct nct6775_data *data = nct6775_update_device(dev);
1160 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
1161 int nr = sensor_attr->index;
1162 return sprintf(buf, "%d\n",
1163 data->fan_from_reg_min(data->fan_min[nr],
1164 data->fan_div[nr]));
1168 show_fan_div(struct device *dev, struct device_attribute *attr,
1171 struct nct6775_data *data = nct6775_update_device(dev);
1172 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
1173 int nr = sensor_attr->index;
1174 return sprintf(buf, "%u\n", div_from_reg(data->fan_div[nr]));
1178 store_fan_min(struct device *dev, struct device_attribute *attr,
1179 const char *buf, size_t count)
1181 struct nct6775_data *data = dev_get_drvdata(dev);
1182 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
1183 int nr = sensor_attr->index;
1189 err = kstrtoul(buf, 10, &val);
1193 mutex_lock(&data->update_lock);
1194 if (!data->has_fan_div) {
1196 * Only NCT6776F for now, so we know that this is a 13 bit
1204 val = 1350000U / val;
1205 val = (val & 0x1f) | ((val << 3) & 0xff00);
1207 data->fan_min[nr] = val;
1208 goto write_min; /* Leave fan divider alone */
1211 /* No min limit, alarm disabled */
1212 data->fan_min[nr] = 255;
1213 new_div = data->fan_div[nr]; /* No change */
1214 dev_info(dev, "fan%u low limit and alarm disabled\n", nr + 1);
1217 reg = 1350000U / val;
1218 if (reg >= 128 * 255) {
1220 * Speed below this value cannot possibly be represented,
1221 * even with the highest divider (128)
1223 data->fan_min[nr] = 254;
1224 new_div = 7; /* 128 == (1 << 7) */
1225 dev_warn(dev, "fan%u low limit %lu below minimum %u, set to "
1226 "minimum\n", nr + 1, val,
1227 data->fan_from_reg_min(254, 7));
1230 * Speed above this value cannot possibly be represented,
1231 * even with the lowest divider (1)
1233 data->fan_min[nr] = 1;
1234 new_div = 0; /* 1 == (1 << 0) */
1235 dev_warn(dev, "fan%u low limit %lu above maximum %u, set to "
1236 "maximum\n", nr + 1, val,
1237 data->fan_from_reg_min(1, 0));
1240 * Automatically pick the best divider, i.e. the one such
1241 * that the min limit will correspond to a register value
1242 * in the 96..192 range
1245 while (reg > 192 && new_div < 7) {
1249 data->fan_min[nr] = reg;
1254 * Write both the fan clock divider (if it changed) and the new
1255 * fan min (unconditionally)
1257 if (new_div != data->fan_div[nr]) {
1258 dev_dbg(dev, "fan%u clock divider changed from %u to %u\n",
1259 nr + 1, div_from_reg(data->fan_div[nr]),
1260 div_from_reg(new_div));
1261 data->fan_div[nr] = new_div;
1262 nct6775_write_fan_div_common(dev, data, nr);
1263 /* Give the chip time to sample a new speed value */
1264 data->last_updated = jiffies;
1267 nct6775_write_value(data, data->REG_FAN_MIN[nr],
1269 mutex_unlock(&data->update_lock);
1274 static struct sensor_device_attribute sda_fan_input[] = {
1275 SENSOR_ATTR(fan1_input, S_IRUGO, show_fan, NULL, 0),
1276 SENSOR_ATTR(fan2_input, S_IRUGO, show_fan, NULL, 1),
1277 SENSOR_ATTR(fan3_input, S_IRUGO, show_fan, NULL, 2),
1278 SENSOR_ATTR(fan4_input, S_IRUGO, show_fan, NULL, 3),
1279 SENSOR_ATTR(fan5_input, S_IRUGO, show_fan, NULL, 4),
1282 static struct sensor_device_attribute sda_fan_alarm[] = {
1283 SENSOR_ATTR(fan1_alarm, S_IRUGO, show_alarm, NULL, 6),
1284 SENSOR_ATTR(fan2_alarm, S_IRUGO, show_alarm, NULL, 7),
1285 SENSOR_ATTR(fan3_alarm, S_IRUGO, show_alarm, NULL, 11),
1286 SENSOR_ATTR(fan4_alarm, S_IRUGO, show_alarm, NULL, 10),
1287 SENSOR_ATTR(fan5_alarm, S_IRUGO, show_alarm, NULL, 23),
1290 static struct sensor_device_attribute sda_fan_min[] = {
1291 SENSOR_ATTR(fan1_min, S_IWUSR | S_IRUGO, show_fan_min,
1293 SENSOR_ATTR(fan2_min, S_IWUSR | S_IRUGO, show_fan_min,
1295 SENSOR_ATTR(fan3_min, S_IWUSR | S_IRUGO, show_fan_min,
1297 SENSOR_ATTR(fan4_min, S_IWUSR | S_IRUGO, show_fan_min,
1299 SENSOR_ATTR(fan5_min, S_IWUSR | S_IRUGO, show_fan_min,
1303 static struct sensor_device_attribute sda_fan_div[] = {
1304 SENSOR_ATTR(fan1_div, S_IRUGO, show_fan_div, NULL, 0),
1305 SENSOR_ATTR(fan2_div, S_IRUGO, show_fan_div, NULL, 1),
1306 SENSOR_ATTR(fan3_div, S_IRUGO, show_fan_div, NULL, 2),
1307 SENSOR_ATTR(fan4_div, S_IRUGO, show_fan_div, NULL, 3),
1308 SENSOR_ATTR(fan5_div, S_IRUGO, show_fan_div, NULL, 4),
1312 show_temp_label(struct device *dev, struct device_attribute *attr, char *buf)
1314 struct nct6775_data *data = nct6775_update_device(dev);
1315 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
1316 int nr = sensor_attr->index;
1317 return sprintf(buf, "%s\n", data->temp_label[data->temp_src[nr]]);
1320 #define show_temp_reg(addr, reg) \
1322 show_##reg(struct device *dev, struct device_attribute *attr, \
1325 struct nct6775_data *data = nct6775_update_device(dev); \
1326 struct sensor_device_attribute *sensor_attr = \
1327 to_sensor_dev_attr(attr); \
1328 int nr = sensor_attr->index; \
1329 return sprintf(buf, "%d\n", LM75_TEMP_FROM_REG(data->reg[nr])); \
1331 show_temp_reg(reg_temp, temp);
1332 show_temp_reg(reg_temp_over, temp_max);
1333 show_temp_reg(reg_temp_hyst, temp_max_hyst);
1335 #define store_temp_reg(addr, reg) \
1337 store_##reg(struct device *dev, struct device_attribute *attr, \
1338 const char *buf, size_t count) \
1340 struct nct6775_data *data = dev_get_drvdata(dev); \
1341 struct sensor_device_attribute *sensor_attr = \
1342 to_sensor_dev_attr(attr); \
1343 int nr = sensor_attr->index; \
1346 err = kstrtol(buf, 10, &val); \
1349 mutex_lock(&data->update_lock); \
1350 data->reg[nr] = LM75_TEMP_TO_REG(val); \
1351 nct6775_write_temp(data, data->addr[nr], data->reg[nr]); \
1352 mutex_unlock(&data->update_lock); \
1355 store_temp_reg(reg_temp_over, temp_max);
1356 store_temp_reg(reg_temp_hyst, temp_max_hyst);
1359 show_temp_type(struct device *dev, struct device_attribute *attr, char *buf)
1361 struct nct6775_data *data = nct6775_update_device(dev);
1362 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
1363 int nr = sensor_attr->index;
1364 return sprintf(buf, "%d\n", (int)data->temp_type[nr]);
1367 static struct sensor_device_attribute sda_temp_input[] = {
1368 SENSOR_ATTR(temp1_input, S_IRUGO, show_temp, NULL, 0),
1369 SENSOR_ATTR(temp2_input, S_IRUGO, show_temp, NULL, 1),
1370 SENSOR_ATTR(temp3_input, S_IRUGO, show_temp, NULL, 2),
1371 SENSOR_ATTR(temp4_input, S_IRUGO, show_temp, NULL, 3),
1372 SENSOR_ATTR(temp5_input, S_IRUGO, show_temp, NULL, 4),
1373 SENSOR_ATTR(temp6_input, S_IRUGO, show_temp, NULL, 5),
1374 SENSOR_ATTR(temp7_input, S_IRUGO, show_temp, NULL, 6),
1375 SENSOR_ATTR(temp8_input, S_IRUGO, show_temp, NULL, 7),
1376 SENSOR_ATTR(temp9_input, S_IRUGO, show_temp, NULL, 8),
1379 static struct sensor_device_attribute sda_temp_label[] = {
1380 SENSOR_ATTR(temp1_label, S_IRUGO, show_temp_label, NULL, 0),
1381 SENSOR_ATTR(temp2_label, S_IRUGO, show_temp_label, NULL, 1),
1382 SENSOR_ATTR(temp3_label, S_IRUGO, show_temp_label, NULL, 2),
1383 SENSOR_ATTR(temp4_label, S_IRUGO, show_temp_label, NULL, 3),
1384 SENSOR_ATTR(temp5_label, S_IRUGO, show_temp_label, NULL, 4),
1385 SENSOR_ATTR(temp6_label, S_IRUGO, show_temp_label, NULL, 5),
1386 SENSOR_ATTR(temp7_label, S_IRUGO, show_temp_label, NULL, 6),
1387 SENSOR_ATTR(temp8_label, S_IRUGO, show_temp_label, NULL, 7),
1388 SENSOR_ATTR(temp9_label, S_IRUGO, show_temp_label, NULL, 8),
1391 static struct sensor_device_attribute sda_temp_max[] = {
1392 SENSOR_ATTR(temp1_max, S_IRUGO | S_IWUSR, show_temp_max,
1394 SENSOR_ATTR(temp2_max, S_IRUGO | S_IWUSR, show_temp_max,
1396 SENSOR_ATTR(temp3_max, S_IRUGO | S_IWUSR, show_temp_max,
1398 SENSOR_ATTR(temp4_max, S_IRUGO | S_IWUSR, show_temp_max,
1400 SENSOR_ATTR(temp5_max, S_IRUGO | S_IWUSR, show_temp_max,
1402 SENSOR_ATTR(temp6_max, S_IRUGO | S_IWUSR, show_temp_max,
1404 SENSOR_ATTR(temp7_max, S_IRUGO | S_IWUSR, show_temp_max,
1406 SENSOR_ATTR(temp8_max, S_IRUGO | S_IWUSR, show_temp_max,
1408 SENSOR_ATTR(temp9_max, S_IRUGO | S_IWUSR, show_temp_max,
1412 static struct sensor_device_attribute sda_temp_max_hyst[] = {
1413 SENSOR_ATTR(temp1_max_hyst, S_IRUGO | S_IWUSR, show_temp_max_hyst,
1414 store_temp_max_hyst, 0),
1415 SENSOR_ATTR(temp2_max_hyst, S_IRUGO | S_IWUSR, show_temp_max_hyst,
1416 store_temp_max_hyst, 1),
1417 SENSOR_ATTR(temp3_max_hyst, S_IRUGO | S_IWUSR, show_temp_max_hyst,
1418 store_temp_max_hyst, 2),
1419 SENSOR_ATTR(temp4_max_hyst, S_IRUGO | S_IWUSR, show_temp_max_hyst,
1420 store_temp_max_hyst, 3),
1421 SENSOR_ATTR(temp5_max_hyst, S_IRUGO | S_IWUSR, show_temp_max_hyst,
1422 store_temp_max_hyst, 4),
1423 SENSOR_ATTR(temp6_max_hyst, S_IRUGO | S_IWUSR, show_temp_max_hyst,
1424 store_temp_max_hyst, 5),
1425 SENSOR_ATTR(temp7_max_hyst, S_IRUGO | S_IWUSR, show_temp_max_hyst,
1426 store_temp_max_hyst, 6),
1427 SENSOR_ATTR(temp8_max_hyst, S_IRUGO | S_IWUSR, show_temp_max_hyst,
1428 store_temp_max_hyst, 7),
1429 SENSOR_ATTR(temp9_max_hyst, S_IRUGO | S_IWUSR, show_temp_max_hyst,
1430 store_temp_max_hyst, 8),
1433 static struct sensor_device_attribute sda_temp_alarm[] = {
1434 SENSOR_ATTR(temp1_alarm, S_IRUGO, show_alarm, NULL, 4),
1435 SENSOR_ATTR(temp2_alarm, S_IRUGO, show_alarm, NULL, 5),
1436 SENSOR_ATTR(temp3_alarm, S_IRUGO, show_alarm, NULL, 13),
1439 static struct sensor_device_attribute sda_temp_type[] = {
1440 SENSOR_ATTR(temp1_type, S_IRUGO, show_temp_type, NULL, 0),
1441 SENSOR_ATTR(temp2_type, S_IRUGO, show_temp_type, NULL, 1),
1442 SENSOR_ATTR(temp3_type, S_IRUGO, show_temp_type, NULL, 2),
1445 #define show_pwm_reg(reg) \
1446 static ssize_t show_##reg(struct device *dev, struct device_attribute *attr, \
1449 struct nct6775_data *data = nct6775_update_device(dev); \
1450 struct sensor_device_attribute *sensor_attr = \
1451 to_sensor_dev_attr(attr); \
1452 int nr = sensor_attr->index; \
1453 return sprintf(buf, "%d\n", data->reg[nr]); \
1456 show_pwm_reg(pwm_mode)
1457 show_pwm_reg(pwm_enable)
1461 store_pwm_mode(struct device *dev, struct device_attribute *attr,
1462 const char *buf, size_t count)
1464 struct nct6775_data *data = dev_get_drvdata(dev);
1465 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
1466 struct nct6775_sio_data *sio_data = dev->platform_data;
1467 int nr = sensor_attr->index;
1472 err = kstrtoul(buf, 10, &val);
1479 /* On NCT6776F and NCT6779D, DC mode is only supported for pwm1 */
1480 if ((sio_data->kind == nct6776 || sio_data->kind == nct6779) && nr) {
1486 mutex_lock(&data->update_lock);
1487 reg = nct6775_read_value(data, NCT6775_REG_PWM_ENABLE[nr]);
1488 data->pwm_mode[nr] = val;
1489 reg &= ~(1 << NCT6775_PWM_MODE_SHIFT[nr]);
1491 reg |= 1 << NCT6775_PWM_MODE_SHIFT[nr];
1492 nct6775_write_value(data, NCT6775_REG_PWM_ENABLE[nr], reg);
1493 mutex_unlock(&data->update_lock);
1498 store_pwm(struct device *dev, struct device_attribute *attr,
1499 const char *buf, size_t count)
1501 struct nct6775_data *data = dev_get_drvdata(dev);
1502 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
1503 int nr = sensor_attr->index;
1507 err = kstrtoul(buf, 10, &val);
1511 val = SENSORS_LIMIT(val, 0, 255);
1513 mutex_lock(&data->update_lock);
1514 data->pwm[nr] = val;
1515 nct6775_write_value(data, NCT6775_REG_PWM[nr], val);
1516 mutex_unlock(&data->update_lock);
1520 /* Returns 0 if OK, -EINVAL otherwise */
1521 static int check_trip_points(struct nct6775_data *data, int nr)
1525 for (i = 0; i < data->auto_pwm_num - 1; i++) {
1526 if (data->auto_temp[nr][i] > data->auto_temp[nr][i + 1])
1529 for (i = 0; i < data->auto_pwm_num - 1; i++) {
1530 if (data->auto_pwm[nr][i] > data->auto_pwm[nr][i + 1])
1533 /* validate critical temperature and pwm if enabled (pwm > 0) */
1534 if (data->auto_pwm[nr][data->auto_pwm_num]) {
1535 if (data->auto_temp[nr][data->auto_pwm_num - 1] >
1536 data->auto_temp[nr][data->auto_pwm_num] ||
1537 data->auto_pwm[nr][data->auto_pwm_num - 1] >
1538 data->auto_pwm[nr][data->auto_pwm_num])
1545 store_pwm_enable(struct device *dev, struct device_attribute *attr,
1546 const char *buf, size_t count)
1548 struct nct6775_data *data = dev_get_drvdata(dev);
1549 struct nct6775_sio_data *sio_data = dev->platform_data;
1550 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
1551 int nr = sensor_attr->index;
1556 err = kstrtoul(buf, 10, &val);
1560 if (!val || val > 5)
1563 /* SmartFan III mode is only supported on NCT6775F */
1564 if (sio_data->kind != nct6775 && val == 4)
1567 if (val == 5 && check_trip_points(data, nr)) {
1568 dev_err(dev, "Inconsistent trip points, not switching to SmartFan IV mode\n");
1569 dev_err(dev, "Adjust trip points and try again\n");
1573 mutex_lock(&data->update_lock);
1574 data->pwm_enable[nr] = val;
1575 reg = nct6775_read_value(data, NCT6775_REG_FAN_MODE[nr]);
1577 reg |= (val - 1) << 4;
1578 nct6775_write_value(data, NCT6775_REG_FAN_MODE[nr], reg);
1579 mutex_unlock(&data->update_lock);
1584 static ssize_t show_target_temp(struct device *dev,
1585 struct device_attribute *attr, char *buf)
1587 struct nct6775_data *data = nct6775_update_device(dev);
1588 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
1590 return sprintf(buf, "%d\n",
1591 data->target_temp[sensor_attr->index] * 1000);
1595 store_target_temp(struct device *dev, struct device_attribute *attr,
1596 const char *buf, size_t count)
1598 struct nct6775_data *data = dev_get_drvdata(dev);
1599 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
1600 int nr = sensor_attr->index;
1604 err = kstrtol(buf, 10, &val);
1608 val = SENSORS_LIMIT(DIV_ROUND_CLOSEST(val, 1000), 0, 127);
1610 mutex_lock(&data->update_lock);
1611 data->target_temp[nr] = val;
1612 nct6775_write_value(data, data->REG_TARGET[nr], val);
1613 mutex_unlock(&data->update_lock);
1617 static ssize_t show_auto_hysteresis(struct device *dev,
1618 struct device_attribute *attr, char *buf)
1620 struct nct6775_data *data = nct6775_update_device(dev);
1621 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
1623 int point = sattr->index >= data->auto_pwm_num ? 1 : 0;
1624 int tolerance = data->tolerance[nr][point];
1625 int temp = data->auto_temp[nr][sattr->index];
1627 return sprintf(buf, "%d\n", (temp - tolerance) * 1000);
1631 store_auto_hysteresis(struct device *dev, struct device_attribute *attr,
1632 const char *buf, size_t count)
1634 struct nct6775_data *data = dev_get_drvdata(dev);
1635 struct nct6775_sio_data *sio_data = dev->platform_data;
1636 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
1638 int point = sattr->index >= data->auto_pwm_num ? 1 : 0;
1642 int maxlimit[2][3] = { { 15, 7, 63 }, { 15, 7, 7 } };
1643 int mask[] = { 0x0f, 0x07, 0x07 };
1646 err = kstrtol(buf, 10, &val);
1650 temp = data->auto_temp[nr][sattr->index];
1651 val = temp - DIV_ROUND_CLOSEST(val, 1000);
1653 /* Limit tolerance as needed */
1654 val = SENSORS_LIMIT(val, 0, maxlimit[point][sio_data->kind]);
1656 mutex_lock(&data->update_lock);
1658 nct6775_write_value(data,
1659 NCT6775_REG_CRITICAL_TEMP_TOLERANCE[nr],
1662 reg = nct6775_read_value(data, NCT6775_REG_FAN_MODE[nr]);
1663 reg = (reg & ~mask[nr]) | (val & mask[nr]);
1664 nct6775_write_value(data, NCT6775_REG_FAN_MODE[nr], reg);
1665 if (sio_data->kind == nct6779) {
1666 reg = nct6775_read_value(data,
1667 NCT6779_REG_TOLERANCE_H[nr]);
1668 reg = (reg & 0x70) | ((val & 0x38) << 1);
1669 nct6775_write_value(data,
1670 NCT6779_REG_TOLERANCE_H[nr], reg);
1674 data->tolerance[nr][point] = val;
1675 mutex_unlock(&data->update_lock);
1679 static SENSOR_DEVICE_ATTR(pwm1, S_IWUSR | S_IRUGO, show_pwm, store_pwm, 0);
1680 static SENSOR_DEVICE_ATTR(pwm2, S_IWUSR | S_IRUGO, show_pwm, store_pwm, 1);
1681 static SENSOR_DEVICE_ATTR(pwm3, S_IWUSR | S_IRUGO, show_pwm, store_pwm, 2);
1682 static SENSOR_DEVICE_ATTR(pwm4, S_IWUSR | S_IRUGO, show_pwm, store_pwm, 3);
1683 static SENSOR_DEVICE_ATTR(pwm5, S_IWUSR | S_IRUGO, show_pwm, store_pwm, 4);
1685 static SENSOR_DEVICE_ATTR(pwm1_mode, S_IWUSR | S_IRUGO, show_pwm_mode,
1687 static SENSOR_DEVICE_ATTR(pwm2_mode, S_IWUSR | S_IRUGO, show_pwm_mode,
1689 static SENSOR_DEVICE_ATTR(pwm3_mode, S_IWUSR | S_IRUGO, show_pwm_mode,
1691 static SENSOR_DEVICE_ATTR(pwm4_mode, S_IWUSR | S_IRUGO, show_pwm_mode,
1693 static SENSOR_DEVICE_ATTR(pwm5_mode, S_IWUSR | S_IRUGO, show_pwm_mode,
1696 static SENSOR_DEVICE_ATTR(pwm1_enable, S_IWUSR | S_IRUGO, show_pwm_enable,
1697 store_pwm_enable, 0);
1698 static SENSOR_DEVICE_ATTR(pwm2_enable, S_IWUSR | S_IRUGO, show_pwm_enable,
1699 store_pwm_enable, 1);
1700 static SENSOR_DEVICE_ATTR(pwm3_enable, S_IWUSR | S_IRUGO, show_pwm_enable,
1701 store_pwm_enable, 2);
1702 static SENSOR_DEVICE_ATTR(pwm4_enable, S_IWUSR | S_IRUGO, show_pwm_enable,
1703 store_pwm_enable, 3);
1704 static SENSOR_DEVICE_ATTR(pwm5_enable, S_IWUSR | S_IRUGO, show_pwm_enable,
1705 store_pwm_enable, 4);
1707 static SENSOR_DEVICE_ATTR(pwm1_target, S_IWUSR | S_IRUGO, show_target_temp,
1708 store_target_temp, 0);
1709 static SENSOR_DEVICE_ATTR(pwm2_target, S_IWUSR | S_IRUGO, show_target_temp,
1710 store_target_temp, 1);
1711 static SENSOR_DEVICE_ATTR(pwm3_target, S_IWUSR | S_IRUGO, show_target_temp,
1712 store_target_temp, 2);
1713 static SENSOR_DEVICE_ATTR(pwm4_target, S_IWUSR | S_IRUGO, show_target_temp,
1714 store_target_temp, 3);
1715 static SENSOR_DEVICE_ATTR(pwm5_target, S_IWUSR | S_IRUGO, show_target_temp,
1716 store_target_temp, 4);
1718 /* Smart Fan registers */
1720 #define fan_functions(reg, REG) \
1721 static ssize_t show_##reg(struct device *dev, struct device_attribute *attr, \
1724 struct nct6775_data *data = nct6775_update_device(dev); \
1725 struct sensor_device_attribute *sensor_attr = \
1726 to_sensor_dev_attr(attr); \
1727 int nr = sensor_attr->index; \
1728 return sprintf(buf, "%d\n", data->reg[nr]); \
1731 store_##reg(struct device *dev, struct device_attribute *attr, \
1732 const char *buf, size_t count) \
1734 struct nct6775_data *data = dev_get_drvdata(dev); \
1735 struct sensor_device_attribute *sensor_attr = \
1736 to_sensor_dev_attr(attr); \
1737 int nr = sensor_attr->index; \
1738 unsigned long val; \
1740 err = kstrtoul(buf, 10, &val); \
1743 val = SENSORS_LIMIT(val, 1, 255); \
1744 mutex_lock(&data->update_lock); \
1745 data->reg[nr] = val; \
1746 nct6775_write_value(data, data->REG_##REG[nr], val); \
1747 mutex_unlock(&data->update_lock); \
1751 fan_functions(fan_start_output, FAN_START_OUTPUT)
1752 fan_functions(fan_stop_output, FAN_STOP_OUTPUT)
1753 fan_functions(fan_max_output, FAN_MAX_OUTPUT)
1754 fan_functions(fan_step_output, FAN_STEP_OUTPUT)
1756 #define fan_step_functions(reg, REG) \
1757 static ssize_t show_##reg(struct device *dev, \
1758 struct device_attribute *attr, char *buf) \
1760 struct nct6775_data *data = nct6775_update_device(dev); \
1761 struct sensor_device_attribute *sensor_attr = \
1762 to_sensor_dev_attr(attr); \
1763 int nr = sensor_attr->index; \
1764 return sprintf(buf, "%d\n", \
1765 step_time_from_reg(data->reg[nr], \
1766 data->pwm_mode[nr])); \
1768 static ssize_t store_##reg(struct device *dev, \
1769 struct device_attribute *attr, \
1770 const char *buf, size_t count) \
1772 struct nct6775_data *data = dev_get_drvdata(dev); \
1773 struct sensor_device_attribute *sensor_attr = \
1774 to_sensor_dev_attr(attr); \
1775 int nr = sensor_attr->index; \
1776 unsigned long val; \
1778 err = kstrtoul(buf, 10, &val); \
1781 val = step_time_to_reg(val, data->pwm_mode[nr]); \
1782 mutex_lock(&data->update_lock); \
1783 data->reg[nr] = val; \
1784 nct6775_write_value(data, NCT6775_REG_##REG[nr], val); \
1785 mutex_unlock(&data->update_lock); \
1789 fan_step_functions(fan_stop_time, FAN_STOP_TIME)
1790 fan_step_functions(fan_step_up_time, FAN_STEP_UP_TIME)
1791 fan_step_functions(fan_step_down_time, FAN_STEP_DOWN_TIME)
1793 static ssize_t show_name(struct device *dev, struct device_attribute *attr,
1796 struct nct6775_data *data = dev_get_drvdata(dev);
1798 return sprintf(buf, "%s\n", data->name);
1800 static DEVICE_ATTR(name, S_IRUGO, show_name, NULL);
1802 static SENSOR_DEVICE_ATTR(pwm1_stop_time, S_IWUSR | S_IRUGO, show_fan_stop_time,
1803 store_fan_stop_time, 0);
1804 static SENSOR_DEVICE_ATTR(pwm2_stop_time, S_IWUSR | S_IRUGO, show_fan_stop_time,
1805 store_fan_stop_time, 1);
1806 static SENSOR_DEVICE_ATTR(pwm3_stop_time, S_IWUSR | S_IRUGO, show_fan_stop_time,
1807 store_fan_stop_time, 2);
1808 static SENSOR_DEVICE_ATTR(pwm4_stop_time, S_IWUSR | S_IRUGO, show_fan_stop_time,
1809 store_fan_stop_time, 3);
1810 static SENSOR_DEVICE_ATTR(pwm5_stop_time, S_IWUSR | S_IRUGO, show_fan_stop_time,
1811 store_fan_stop_time, 4);
1813 static SENSOR_DEVICE_ATTR(pwm1_step_up_time, S_IWUSR | S_IRUGO,
1814 show_fan_step_up_time, store_fan_step_up_time, 0);
1815 static SENSOR_DEVICE_ATTR(pwm2_step_up_time, S_IWUSR | S_IRUGO,
1816 show_fan_step_up_time, store_fan_step_up_time, 1);
1817 static SENSOR_DEVICE_ATTR(pwm3_step_up_time, S_IWUSR | S_IRUGO,
1818 show_fan_step_up_time, store_fan_step_up_time, 2);
1819 static SENSOR_DEVICE_ATTR(pwm4_step_up_time, S_IWUSR | S_IRUGO,
1820 show_fan_step_up_time, store_fan_step_up_time, 3);
1821 static SENSOR_DEVICE_ATTR(pwm5_step_up_time, S_IWUSR | S_IRUGO,
1822 show_fan_step_up_time, store_fan_step_up_time, 4);
1824 static SENSOR_DEVICE_ATTR(pwm1_step_down_time, S_IWUSR | S_IRUGO,
1825 show_fan_step_down_time, store_fan_step_down_time, 0);
1826 static SENSOR_DEVICE_ATTR(pwm2_step_down_time, S_IWUSR | S_IRUGO,
1827 show_fan_step_down_time, store_fan_step_down_time, 1);
1828 static SENSOR_DEVICE_ATTR(pwm3_step_down_time, S_IWUSR | S_IRUGO,
1829 show_fan_step_down_time, store_fan_step_down_time, 2);
1830 static SENSOR_DEVICE_ATTR(pwm4_step_down_time, S_IWUSR | S_IRUGO,
1831 show_fan_step_down_time, store_fan_step_down_time, 3);
1832 static SENSOR_DEVICE_ATTR(pwm5_step_down_time, S_IWUSR | S_IRUGO,
1833 show_fan_step_down_time, store_fan_step_down_time, 4);
1835 static SENSOR_DEVICE_ATTR(pwm1_start_output, S_IWUSR | S_IRUGO,
1836 show_fan_start_output, store_fan_start_output, 0);
1837 static SENSOR_DEVICE_ATTR(pwm2_start_output, S_IWUSR | S_IRUGO,
1838 show_fan_start_output, store_fan_start_output, 1);
1839 static SENSOR_DEVICE_ATTR(pwm3_start_output, S_IWUSR | S_IRUGO,
1840 show_fan_start_output, store_fan_start_output, 2);
1841 static SENSOR_DEVICE_ATTR(pwm4_start_output, S_IWUSR | S_IRUGO,
1842 show_fan_start_output, store_fan_start_output, 3);
1843 static SENSOR_DEVICE_ATTR(pwm5_start_output, S_IWUSR | S_IRUGO,
1844 show_fan_start_output, store_fan_start_output, 4);
1846 static SENSOR_DEVICE_ATTR(pwm1_stop_output, S_IWUSR | S_IRUGO,
1847 show_fan_stop_output, store_fan_stop_output, 0);
1848 static SENSOR_DEVICE_ATTR(pwm2_stop_output, S_IWUSR | S_IRUGO,
1849 show_fan_stop_output, store_fan_stop_output, 1);
1850 static SENSOR_DEVICE_ATTR(pwm3_stop_output, S_IWUSR | S_IRUGO,
1851 show_fan_stop_output, store_fan_stop_output, 2);
1852 static SENSOR_DEVICE_ATTR(pwm4_stop_output, S_IWUSR | S_IRUGO,
1853 show_fan_stop_output, store_fan_stop_output, 3);
1854 static SENSOR_DEVICE_ATTR(pwm5_stop_output, S_IWUSR | S_IRUGO,
1855 show_fan_stop_output, store_fan_stop_output, 4);
1857 static struct attribute *nct6775_attributes_pwm[5][10] = {
1859 &sensor_dev_attr_pwm1.dev_attr.attr,
1860 &sensor_dev_attr_pwm1_mode.dev_attr.attr,
1861 &sensor_dev_attr_pwm1_enable.dev_attr.attr,
1862 &sensor_dev_attr_pwm1_target.dev_attr.attr,
1863 &sensor_dev_attr_pwm1_stop_time.dev_attr.attr,
1864 &sensor_dev_attr_pwm1_step_up_time.dev_attr.attr,
1865 &sensor_dev_attr_pwm1_step_down_time.dev_attr.attr,
1866 &sensor_dev_attr_pwm1_start_output.dev_attr.attr,
1867 &sensor_dev_attr_pwm1_stop_output.dev_attr.attr,
1871 &sensor_dev_attr_pwm2.dev_attr.attr,
1872 &sensor_dev_attr_pwm2_mode.dev_attr.attr,
1873 &sensor_dev_attr_pwm2_enable.dev_attr.attr,
1874 &sensor_dev_attr_pwm2_target.dev_attr.attr,
1875 &sensor_dev_attr_pwm2_stop_time.dev_attr.attr,
1876 &sensor_dev_attr_pwm2_step_up_time.dev_attr.attr,
1877 &sensor_dev_attr_pwm2_step_down_time.dev_attr.attr,
1878 &sensor_dev_attr_pwm2_start_output.dev_attr.attr,
1879 &sensor_dev_attr_pwm2_stop_output.dev_attr.attr,
1883 &sensor_dev_attr_pwm3.dev_attr.attr,
1884 &sensor_dev_attr_pwm3_mode.dev_attr.attr,
1885 &sensor_dev_attr_pwm3_enable.dev_attr.attr,
1886 &sensor_dev_attr_pwm3_target.dev_attr.attr,
1887 &sensor_dev_attr_pwm3_stop_time.dev_attr.attr,
1888 &sensor_dev_attr_pwm3_step_up_time.dev_attr.attr,
1889 &sensor_dev_attr_pwm3_step_down_time.dev_attr.attr,
1890 &sensor_dev_attr_pwm3_start_output.dev_attr.attr,
1891 &sensor_dev_attr_pwm3_stop_output.dev_attr.attr,
1895 &sensor_dev_attr_pwm4.dev_attr.attr,
1896 &sensor_dev_attr_pwm4_mode.dev_attr.attr,
1897 &sensor_dev_attr_pwm4_enable.dev_attr.attr,
1898 &sensor_dev_attr_pwm4_target.dev_attr.attr,
1899 &sensor_dev_attr_pwm4_stop_time.dev_attr.attr,
1900 &sensor_dev_attr_pwm4_step_up_time.dev_attr.attr,
1901 &sensor_dev_attr_pwm4_step_down_time.dev_attr.attr,
1902 &sensor_dev_attr_pwm4_start_output.dev_attr.attr,
1903 &sensor_dev_attr_pwm4_stop_output.dev_attr.attr,
1907 &sensor_dev_attr_pwm5.dev_attr.attr,
1908 &sensor_dev_attr_pwm5_mode.dev_attr.attr,
1909 &sensor_dev_attr_pwm5_enable.dev_attr.attr,
1910 &sensor_dev_attr_pwm5_target.dev_attr.attr,
1911 &sensor_dev_attr_pwm5_stop_time.dev_attr.attr,
1912 &sensor_dev_attr_pwm5_step_up_time.dev_attr.attr,
1913 &sensor_dev_attr_pwm5_step_down_time.dev_attr.attr,
1914 &sensor_dev_attr_pwm5_start_output.dev_attr.attr,
1915 &sensor_dev_attr_pwm5_stop_output.dev_attr.attr,
1920 static const struct attribute_group nct6775_group_pwm[5] = {
1921 { .attrs = nct6775_attributes_pwm[0] },
1922 { .attrs = nct6775_attributes_pwm[1] },
1923 { .attrs = nct6775_attributes_pwm[2] },
1924 { .attrs = nct6775_attributes_pwm[3] },
1925 { .attrs = nct6775_attributes_pwm[4] },
1929 * max and step settings are not supported on all chips.
1930 * Need to check support while generating/removing attribute files.
1932 static struct sensor_device_attribute sda_sf3_max_step_arrays[] = {
1933 SENSOR_ATTR(pwm1_max_output, S_IWUSR | S_IRUGO, show_fan_max_output,
1934 store_fan_max_output, 0),
1935 SENSOR_ATTR(pwm1_step_output, S_IWUSR | S_IRUGO, show_fan_step_output,
1936 store_fan_step_output, 0),
1937 SENSOR_ATTR(pwm2_max_output, S_IWUSR | S_IRUGO, show_fan_max_output,
1938 store_fan_max_output, 1),
1939 SENSOR_ATTR(pwm2_step_output, S_IWUSR | S_IRUGO, show_fan_step_output,
1940 store_fan_step_output, 1),
1941 SENSOR_ATTR(pwm3_max_output, S_IWUSR | S_IRUGO, show_fan_max_output,
1942 store_fan_max_output, 2),
1943 SENSOR_ATTR(pwm3_step_output, S_IWUSR | S_IRUGO, show_fan_step_output,
1944 store_fan_step_output, 2),
1945 SENSOR_ATTR(pwm4_max_output, S_IWUSR | S_IRUGO, show_fan_max_output,
1946 store_fan_max_output, 3),
1947 SENSOR_ATTR(pwm4_step_output, S_IWUSR | S_IRUGO, show_fan_step_output,
1948 store_fan_step_output, 3),
1949 SENSOR_ATTR(pwm5_max_output, S_IWUSR | S_IRUGO, show_fan_max_output,
1950 store_fan_max_output, 4),
1951 SENSOR_ATTR(pwm5_step_output, S_IWUSR | S_IRUGO, show_fan_step_output,
1952 store_fan_step_output, 4),
1955 static ssize_t show_auto_pwm(struct device *dev, struct device_attribute *attr,
1958 struct nct6775_data *data = nct6775_update_device(dev);
1959 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
1961 return sprintf(buf, "%d\n", data->auto_pwm[sattr->nr][sattr->index]);
1964 static ssize_t store_auto_pwm(struct device *dev, struct device_attribute *attr,
1965 const char *buf, size_t count)
1967 struct nct6775_sio_data *sio_data = dev->platform_data;
1968 struct nct6775_data *data = dev_get_drvdata(dev);
1969 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
1971 int point = sattr->index;
1976 err = kstrtoul(buf, 10, &val);
1982 if (point == data->auto_pwm_num) {
1983 if (sio_data->kind != nct6775 && !val)
1985 if (sio_data->kind != nct6779 && val)
1989 mutex_lock(&data->update_lock);
1990 data->auto_pwm[nr][point] = val;
1991 if (point < data->auto_pwm_num) {
1992 nct6775_write_value(data, NCT6775_REG_AUTO_PWM(nr, point),
1993 data->auto_pwm[nr][point]);
1995 switch(sio_data->kind) {
1997 /* disable if needed (pwm == 0) */
1998 reg = nct6775_read_value(data,
1999 NCT6775_REG_CRITICAL_ENAB[nr]);
2004 nct6775_write_value(data, NCT6775_REG_CRITICAL_ENAB[nr],
2008 break; /* always enabled, nothing to do */
2010 nct6775_write_value(data, NCT6779_REG_CRITICAL_PWM[nr],
2012 reg = nct6775_read_value(data,
2013 NCT6779_REG_CRITICAL_PWM_ENABLE[nr]);
2018 nct6775_write_value(data,
2019 NCT6779_REG_CRITICAL_PWM_ENABLE[nr],
2024 mutex_unlock(&data->update_lock);
2028 static ssize_t show_auto_temp(struct device *dev, struct device_attribute *attr,
2031 struct nct6775_data *data = nct6775_update_device(dev);
2032 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
2034 int point = sattr->index;
2037 * We don't know for sure if the temperature is signed or unsigned.
2038 * Assume it is unsigned.
2040 return sprintf(buf, "%d\n", data->auto_temp[nr][point] * 1000);
2043 static ssize_t store_auto_temp(struct device *dev,
2044 struct device_attribute *attr,
2045 const char *buf, size_t count)
2047 struct nct6775_data *data = dev_get_drvdata(dev);
2048 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
2050 int point = sattr->index;
2054 err = kstrtoul(buf, 10, &val);
2060 mutex_lock(&data->update_lock);
2061 data->auto_temp[nr][point] = DIV_ROUND_CLOSEST(val, 1000);
2062 if (point < data->auto_pwm_num) {
2063 nct6775_write_value(data, NCT6775_REG_AUTO_TEMP(nr, point),
2064 data->auto_temp[nr][point]);
2066 nct6775_write_value(data, NCT6775_REG_CRITICAL_TEMP[nr],
2067 data->auto_temp[nr][point]);
2069 mutex_unlock(&data->update_lock);
2074 * The number of auto-point trip points is chip dependent.
2075 * Need to check support while generating/removing attribute files.
2077 static struct sensor_device_attribute_2 sda_auto_pwm_arrays[] = {
2078 SENSOR_ATTR_2(pwm1_auto_point1_pwm, S_IWUSR | S_IRUGO,
2079 show_auto_pwm, store_auto_pwm, 0, 0),
2080 SENSOR_ATTR_2(pwm1_auto_point1_temp, S_IWUSR | S_IRUGO,
2081 show_auto_temp, store_auto_temp, 0, 0),
2082 SENSOR_ATTR_2(pwm1_auto_point1_hysteresis, S_IWUSR | S_IRUGO,
2083 show_auto_hysteresis, store_auto_hysteresis, 0, 0),
2084 SENSOR_ATTR_2(pwm1_auto_point2_pwm, S_IWUSR | S_IRUGO,
2085 show_auto_pwm, store_auto_pwm, 0, 1),
2086 SENSOR_ATTR_2(pwm1_auto_point2_temp, S_IWUSR | S_IRUGO,
2087 show_auto_temp, store_auto_temp, 0, 1),
2088 SENSOR_ATTR_2(pwm1_auto_point2_hysteresis, S_IWUSR | S_IRUGO,
2089 show_auto_hysteresis, store_auto_hysteresis, 0, 1),
2090 SENSOR_ATTR_2(pwm1_auto_point3_pwm, S_IWUSR | S_IRUGO,
2091 show_auto_pwm, store_auto_pwm, 0, 2),
2092 SENSOR_ATTR_2(pwm1_auto_point3_temp, S_IWUSR | S_IRUGO,
2093 show_auto_temp, store_auto_temp, 0, 2),
2094 SENSOR_ATTR_2(pwm1_auto_point3_hysteresis, S_IWUSR | S_IRUGO,
2095 show_auto_hysteresis, store_auto_hysteresis, 0, 2),
2096 SENSOR_ATTR_2(pwm1_auto_point4_pwm, S_IWUSR | S_IRUGO,
2097 show_auto_pwm, store_auto_pwm, 0, 3),
2098 SENSOR_ATTR_2(pwm1_auto_point4_temp, S_IWUSR | S_IRUGO,
2099 show_auto_temp, store_auto_temp, 0, 3),
2100 SENSOR_ATTR_2(pwm1_auto_point4_hysteresis, S_IWUSR | S_IRUGO,
2101 show_auto_hysteresis, store_auto_hysteresis, 0, 3),
2102 SENSOR_ATTR_2(pwm1_auto_point5_pwm, S_IWUSR | S_IRUGO,
2103 show_auto_pwm, store_auto_pwm, 0, 4),
2104 SENSOR_ATTR_2(pwm1_auto_point5_temp, S_IWUSR | S_IRUGO,
2105 show_auto_temp, store_auto_temp, 0, 4),
2106 SENSOR_ATTR_2(pwm1_auto_point5_hysteresis, S_IWUSR | S_IRUGO,
2107 show_auto_hysteresis, store_auto_hysteresis, 0, 4),
2108 SENSOR_ATTR_2(pwm1_auto_point6_pwm, S_IWUSR | S_IRUGO,
2109 show_auto_pwm, store_auto_pwm, 0, 5),
2110 SENSOR_ATTR_2(pwm1_auto_point6_temp, S_IWUSR | S_IRUGO,
2111 show_auto_temp, store_auto_temp, 0, 5),
2112 SENSOR_ATTR_2(pwm1_auto_point6_hysteresis, S_IWUSR | S_IRUGO,
2113 show_auto_hysteresis, store_auto_hysteresis, 0, 5),
2114 SENSOR_ATTR_2(pwm1_auto_point7_pwm, S_IWUSR | S_IRUGO,
2115 show_auto_pwm, store_auto_pwm, 0, 6),
2116 SENSOR_ATTR_2(pwm1_auto_point7_temp, S_IWUSR | S_IRUGO,
2117 show_auto_temp, store_auto_temp, 0, 6),
2118 SENSOR_ATTR_2(pwm1_auto_point7_hysteresis, S_IWUSR | S_IRUGO,
2119 show_auto_hysteresis, store_auto_hysteresis, 0, 6),
2121 SENSOR_ATTR_2(pwm2_auto_point1_pwm, S_IWUSR | S_IRUGO,
2122 show_auto_pwm, store_auto_pwm, 1, 0),
2123 SENSOR_ATTR_2(pwm2_auto_point1_temp, S_IWUSR | S_IRUGO,
2124 show_auto_temp, store_auto_temp, 1, 0),
2125 SENSOR_ATTR_2(pwm2_auto_point1_hysteresis, S_IWUSR | S_IRUGO,
2126 show_auto_hysteresis, store_auto_hysteresis, 1, 0),
2127 SENSOR_ATTR_2(pwm2_auto_point2_pwm, S_IWUSR | S_IRUGO,
2128 show_auto_pwm, store_auto_pwm, 1, 1),
2129 SENSOR_ATTR_2(pwm2_auto_point2_temp, S_IWUSR | S_IRUGO,
2130 show_auto_temp, store_auto_temp, 1, 1),
2131 SENSOR_ATTR_2(pwm2_auto_point2_hysteresis, S_IWUSR | S_IRUGO,
2132 show_auto_hysteresis, store_auto_hysteresis, 1, 1),
2133 SENSOR_ATTR_2(pwm2_auto_point3_pwm, S_IWUSR | S_IRUGO,
2134 show_auto_pwm, store_auto_pwm, 1, 2),
2135 SENSOR_ATTR_2(pwm2_auto_point3_temp, S_IWUSR | S_IRUGO,
2136 show_auto_temp, store_auto_temp, 1, 2),
2137 SENSOR_ATTR_2(pwm2_auto_point3_hysteresis, S_IWUSR | S_IRUGO,
2138 show_auto_hysteresis, store_auto_hysteresis, 1, 2),
2139 SENSOR_ATTR_2(pwm2_auto_point4_pwm, S_IWUSR | S_IRUGO,
2140 show_auto_pwm, store_auto_pwm, 1, 3),
2141 SENSOR_ATTR_2(pwm2_auto_point4_temp, S_IWUSR | S_IRUGO,
2142 show_auto_temp, store_auto_temp, 1, 3),
2143 SENSOR_ATTR_2(pwm2_auto_point4_hysteresis, S_IWUSR | S_IRUGO,
2144 show_auto_hysteresis, store_auto_hysteresis, 1, 3),
2145 SENSOR_ATTR_2(pwm2_auto_point5_pwm, S_IWUSR | S_IRUGO,
2146 show_auto_pwm, store_auto_pwm, 1, 4),
2147 SENSOR_ATTR_2(pwm2_auto_point5_temp, S_IWUSR | S_IRUGO,
2148 show_auto_temp, store_auto_temp, 1, 4),
2149 SENSOR_ATTR_2(pwm2_auto_point5_hysteresis, S_IWUSR | S_IRUGO,
2150 show_auto_hysteresis, store_auto_hysteresis, 1, 4),
2151 SENSOR_ATTR_2(pwm2_auto_point6_pwm, S_IWUSR | S_IRUGO,
2152 show_auto_pwm, store_auto_pwm, 1, 5),
2153 SENSOR_ATTR_2(pwm2_auto_point6_temp, S_IWUSR | S_IRUGO,
2154 show_auto_temp, store_auto_temp, 1, 5),
2155 SENSOR_ATTR_2(pwm2_auto_point6_hysteresis, S_IWUSR | S_IRUGO,
2156 show_auto_hysteresis, store_auto_hysteresis, 1, 5),
2157 SENSOR_ATTR_2(pwm2_auto_point7_pwm, S_IWUSR | S_IRUGO,
2158 show_auto_pwm, store_auto_pwm, 1, 6),
2159 SENSOR_ATTR_2(pwm2_auto_point7_temp, S_IWUSR | S_IRUGO,
2160 show_auto_temp, store_auto_temp, 1, 6),
2161 SENSOR_ATTR_2(pwm2_auto_point7_hysteresis, S_IWUSR | S_IRUGO,
2162 show_auto_hysteresis, store_auto_hysteresis, 1, 6),
2164 SENSOR_ATTR_2(pwm3_auto_point1_pwm, S_IWUSR | S_IRUGO,
2165 show_auto_pwm, store_auto_pwm, 2, 0),
2166 SENSOR_ATTR_2(pwm3_auto_point1_temp, S_IWUSR | S_IRUGO,
2167 show_auto_temp, store_auto_temp, 2, 0),
2168 SENSOR_ATTR_2(pwm3_auto_point1_hysteresis, S_IWUSR | S_IRUGO,
2169 show_auto_hysteresis, store_auto_hysteresis, 2, 0),
2170 SENSOR_ATTR_2(pwm3_auto_point2_pwm, S_IWUSR | S_IRUGO,
2171 show_auto_pwm, store_auto_pwm, 2, 1),
2172 SENSOR_ATTR_2(pwm3_auto_point2_temp, S_IWUSR | S_IRUGO,
2173 show_auto_temp, store_auto_temp, 2, 1),
2174 SENSOR_ATTR_2(pwm3_auto_point2_hysteresis, S_IWUSR | S_IRUGO,
2175 show_auto_hysteresis, store_auto_hysteresis, 2, 1),
2176 SENSOR_ATTR_2(pwm3_auto_point3_pwm, S_IWUSR | S_IRUGO,
2177 show_auto_pwm, store_auto_pwm, 2, 2),
2178 SENSOR_ATTR_2(pwm3_auto_point3_temp, S_IWUSR | S_IRUGO,
2179 show_auto_temp, store_auto_temp, 2, 2),
2180 SENSOR_ATTR_2(pwm3_auto_point3_hysteresis, S_IWUSR | S_IRUGO,
2181 show_auto_hysteresis, store_auto_hysteresis, 2, 2),
2182 SENSOR_ATTR_2(pwm3_auto_point4_pwm, S_IWUSR | S_IRUGO,
2183 show_auto_pwm, store_auto_pwm, 2, 3),
2184 SENSOR_ATTR_2(pwm3_auto_point4_temp, S_IWUSR | S_IRUGO,
2185 show_auto_temp, store_auto_temp, 2, 3),
2186 SENSOR_ATTR_2(pwm3_auto_point4_hysteresis, S_IWUSR | S_IRUGO,
2187 show_auto_hysteresis, store_auto_hysteresis, 2, 3),
2188 SENSOR_ATTR_2(pwm3_auto_point5_pwm, S_IWUSR | S_IRUGO,
2189 show_auto_pwm, store_auto_pwm, 2, 4),
2190 SENSOR_ATTR_2(pwm3_auto_point5_temp, S_IWUSR | S_IRUGO,
2191 show_auto_temp, store_auto_temp, 2, 4),
2192 SENSOR_ATTR_2(pwm3_auto_point5_hysteresis, S_IWUSR | S_IRUGO,
2193 show_auto_hysteresis, store_auto_hysteresis, 2, 4),
2194 SENSOR_ATTR_2(pwm3_auto_point6_pwm, S_IWUSR | S_IRUGO,
2195 show_auto_pwm, store_auto_pwm, 2, 5),
2196 SENSOR_ATTR_2(pwm3_auto_point6_temp, S_IWUSR | S_IRUGO,
2197 show_auto_temp, store_auto_temp, 2, 5),
2198 SENSOR_ATTR_2(pwm3_auto_point6_hysteresis, S_IWUSR | S_IRUGO,
2199 show_auto_hysteresis, store_auto_hysteresis, 2, 5),
2200 SENSOR_ATTR_2(pwm3_auto_point7_pwm, S_IWUSR | S_IRUGO,
2201 show_auto_pwm, store_auto_pwm, 2, 6),
2202 SENSOR_ATTR_2(pwm3_auto_point7_temp, S_IWUSR | S_IRUGO,
2203 show_auto_temp, store_auto_temp, 2, 6),
2204 SENSOR_ATTR_2(pwm3_auto_point7_hysteresis, S_IWUSR | S_IRUGO,
2205 show_auto_hysteresis, store_auto_hysteresis, 2, 6),
2207 SENSOR_ATTR_2(pwm4_auto_point1_pwm, S_IWUSR | S_IRUGO,
2208 show_auto_pwm, store_auto_pwm, 3, 0),
2209 SENSOR_ATTR_2(pwm4_auto_point1_temp, S_IWUSR | S_IRUGO,
2210 show_auto_temp, store_auto_temp, 3, 0),
2211 SENSOR_ATTR_2(pwm4_auto_point1_hysteresis, S_IWUSR | S_IRUGO,
2212 show_auto_hysteresis, store_auto_hysteresis, 3, 0),
2213 SENSOR_ATTR_2(pwm4_auto_point2_pwm, S_IWUSR | S_IRUGO,
2214 show_auto_pwm, store_auto_pwm, 3, 1),
2215 SENSOR_ATTR_2(pwm4_auto_point2_temp, S_IWUSR | S_IRUGO,
2216 show_auto_temp, store_auto_temp, 3, 1),
2217 SENSOR_ATTR_2(pwm4_auto_point2_hysteresis, S_IWUSR | S_IRUGO,
2218 show_auto_hysteresis, store_auto_hysteresis, 3, 1),
2219 SENSOR_ATTR_2(pwm4_auto_point3_pwm, S_IWUSR | S_IRUGO,
2220 show_auto_pwm, store_auto_pwm, 3, 2),
2221 SENSOR_ATTR_2(pwm4_auto_point3_temp, S_IWUSR | S_IRUGO,
2222 show_auto_temp, store_auto_temp, 3, 2),
2223 SENSOR_ATTR_2(pwm4_auto_point3_hysteresis, S_IWUSR | S_IRUGO,
2224 show_auto_hysteresis, store_auto_hysteresis, 3, 2),
2225 SENSOR_ATTR_2(pwm4_auto_point4_pwm, S_IWUSR | S_IRUGO,
2226 show_auto_pwm, store_auto_pwm, 3, 3),
2227 SENSOR_ATTR_2(pwm4_auto_point4_temp, S_IWUSR | S_IRUGO,
2228 show_auto_temp, store_auto_temp, 3, 3),
2229 SENSOR_ATTR_2(pwm4_auto_point4_hysteresis, S_IWUSR | S_IRUGO,
2230 show_auto_hysteresis, store_auto_hysteresis, 3, 3),
2231 SENSOR_ATTR_2(pwm4_auto_point5_pwm, S_IWUSR | S_IRUGO,
2232 show_auto_pwm, store_auto_pwm, 3, 4),
2233 SENSOR_ATTR_2(pwm4_auto_point5_temp, S_IWUSR | S_IRUGO,
2234 show_auto_temp, store_auto_temp, 3, 4),
2235 SENSOR_ATTR_2(pwm4_auto_point5_hysteresis, S_IWUSR | S_IRUGO,
2236 show_auto_hysteresis, store_auto_hysteresis, 3, 4),
2237 SENSOR_ATTR_2(pwm4_auto_point6_pwm, S_IWUSR | S_IRUGO,
2238 show_auto_pwm, store_auto_pwm, 3, 5),
2239 SENSOR_ATTR_2(pwm4_auto_point6_temp, S_IWUSR | S_IRUGO,
2240 show_auto_temp, store_auto_temp, 3, 5),
2241 SENSOR_ATTR_2(pwm4_auto_point6_hysteresis, S_IWUSR | S_IRUGO,
2242 show_auto_hysteresis, store_auto_hysteresis, 3, 5),
2243 SENSOR_ATTR_2(pwm4_auto_point7_pwm, S_IWUSR | S_IRUGO,
2244 show_auto_pwm, store_auto_pwm, 3, 6),
2245 SENSOR_ATTR_2(pwm4_auto_point7_temp, S_IWUSR | S_IRUGO,
2246 show_auto_temp, store_auto_temp, 3, 6),
2247 SENSOR_ATTR_2(pwm4_auto_point7_hysteresis, S_IWUSR | S_IRUGO,
2248 show_auto_hysteresis, store_auto_hysteresis, 3, 6),
2250 SENSOR_ATTR_2(pwm5_auto_point1_pwm, S_IWUSR | S_IRUGO,
2251 show_auto_pwm, store_auto_pwm, 4, 0),
2252 SENSOR_ATTR_2(pwm5_auto_point1_temp, S_IWUSR | S_IRUGO,
2253 show_auto_temp, store_auto_temp, 4, 0),
2254 SENSOR_ATTR_2(pwm5_auto_point1_hysteresis, S_IWUSR | S_IRUGO,
2255 show_auto_hysteresis, store_auto_hysteresis, 4, 0),
2256 SENSOR_ATTR_2(pwm5_auto_point2_pwm, S_IWUSR | S_IRUGO,
2257 show_auto_pwm, store_auto_pwm, 4, 1),
2258 SENSOR_ATTR_2(pwm5_auto_point2_temp, S_IWUSR | S_IRUGO,
2259 show_auto_temp, store_auto_temp, 4, 1),
2260 SENSOR_ATTR_2(pwm5_auto_point2_hysteresis, S_IWUSR | S_IRUGO,
2261 show_auto_hysteresis, store_auto_hysteresis, 4, 1),
2262 SENSOR_ATTR_2(pwm5_auto_point3_pwm, S_IWUSR | S_IRUGO,
2263 show_auto_pwm, store_auto_pwm, 4, 2),
2264 SENSOR_ATTR_2(pwm5_auto_point3_temp, S_IWUSR | S_IRUGO,
2265 show_auto_temp, store_auto_temp, 4, 2),
2266 SENSOR_ATTR_2(pwm5_auto_point3_hysteresis, S_IWUSR | S_IRUGO,
2267 show_auto_hysteresis, store_auto_hysteresis, 4, 2),
2268 SENSOR_ATTR_2(pwm5_auto_point4_pwm, S_IWUSR | S_IRUGO,
2269 show_auto_pwm, store_auto_pwm, 4, 3),
2270 SENSOR_ATTR_2(pwm5_auto_point4_temp, S_IWUSR | S_IRUGO,
2271 show_auto_temp, store_auto_temp, 4, 3),
2272 SENSOR_ATTR_2(pwm5_auto_point4_hysteresis, S_IWUSR | S_IRUGO,
2273 show_auto_hysteresis, store_auto_hysteresis, 4, 3),
2274 SENSOR_ATTR_2(pwm5_auto_point5_pwm, S_IWUSR | S_IRUGO,
2275 show_auto_pwm, store_auto_pwm, 4, 4),
2276 SENSOR_ATTR_2(pwm5_auto_point5_temp, S_IWUSR | S_IRUGO,
2277 show_auto_temp, store_auto_temp, 4, 4),
2278 SENSOR_ATTR_2(pwm5_auto_point5_hysteresis, S_IWUSR | S_IRUGO,
2279 show_auto_hysteresis, store_auto_hysteresis, 4, 4),
2280 SENSOR_ATTR_2(pwm5_auto_point6_pwm, S_IWUSR | S_IRUGO,
2281 show_auto_pwm, store_auto_pwm, 4, 5),
2282 SENSOR_ATTR_2(pwm5_auto_point6_temp, S_IWUSR | S_IRUGO,
2283 show_auto_temp, store_auto_temp, 4, 5),
2284 SENSOR_ATTR_2(pwm5_auto_point6_hysteresis, S_IWUSR | S_IRUGO,
2285 show_auto_hysteresis, store_auto_hysteresis, 4, 5),
2286 SENSOR_ATTR_2(pwm5_auto_point7_pwm, S_IWUSR | S_IRUGO,
2287 show_auto_pwm, store_auto_pwm, 4, 6),
2288 SENSOR_ATTR_2(pwm5_auto_point7_temp, S_IWUSR | S_IRUGO,
2289 show_auto_temp, store_auto_temp, 4, 6),
2290 SENSOR_ATTR_2(pwm5_auto_point7_hysteresis, S_IWUSR | S_IRUGO,
2291 show_auto_hysteresis, store_auto_hysteresis, 4, 6),
2295 show_vid(struct device *dev, struct device_attribute *attr, char *buf)
2297 struct nct6775_data *data = dev_get_drvdata(dev);
2298 return sprintf(buf, "%d\n", vid_from_reg(data->vid, data->vrm));
2300 static DEVICE_ATTR(cpu0_vid, S_IRUGO, show_vid, NULL);
2303 /* Case open detection */
2306 show_caseopen(struct device *dev, struct device_attribute *attr, char *buf)
2308 struct nct6775_data *data = nct6775_update_device(dev);
2310 return sprintf(buf, "%d\n",
2311 !!(data->caseopen & to_sensor_dev_attr_2(attr)->index));
2315 clear_caseopen(struct device *dev, struct device_attribute *attr,
2316 const char *buf, size_t count)
2318 struct nct6775_data *data = dev_get_drvdata(dev);
2322 if (kstrtoul(buf, 10, &val) || val != 0)
2325 mask = to_sensor_dev_attr_2(attr)->nr;
2327 mutex_lock(&data->update_lock);
2328 reg = nct6775_read_value(data, NCT6775_REG_CASEOPEN_CLR);
2329 nct6775_write_value(data, NCT6775_REG_CASEOPEN_CLR, reg | mask);
2330 nct6775_write_value(data, NCT6775_REG_CASEOPEN_CLR, reg & ~mask);
2331 data->valid = 0; /* Force cache refresh */
2332 mutex_unlock(&data->update_lock);
2337 static struct sensor_device_attribute_2 sda_caseopen[] = {
2338 SENSOR_ATTR_2(intrusion0_alarm, S_IWUSR | S_IRUGO, show_caseopen,
2339 clear_caseopen, 0x80, 0x10),
2340 SENSOR_ATTR_2(intrusion1_alarm, S_IWUSR | S_IRUGO, show_caseopen,
2341 clear_caseopen, 0x40, 0x40),
2345 * Driver and device management
2348 static void nct6775_device_remove_files(struct device *dev)
2351 * some entries in the following arrays may not have been used in
2352 * device_create_file(), but device_remove_file() will ignore them
2355 struct nct6775_data *data = dev_get_drvdata(dev);
2357 for (i = 0; i < data->pwm_num; i++) {
2358 sysfs_remove_group(&dev->kobj, &nct6775_group_pwm[i]);
2359 for (j = 0; j < ARRAY_SIZE(sda_auto_pwm_arrays); j++)
2360 device_remove_file(dev,
2361 &sda_auto_pwm_arrays[j].dev_attr);
2364 for (i = 0; i < ARRAY_SIZE(sda_sf3_max_step_arrays); i++)
2365 device_remove_file(dev, &sda_sf3_max_step_arrays[i].dev_attr);
2367 for (i = 0; i < data->in_num; i++)
2368 sysfs_remove_group(&dev->kobj, &nct6775_group_in[i]);
2370 for (i = 0; i < 5; i++) {
2371 device_remove_file(dev, &sda_fan_input[i].dev_attr);
2372 device_remove_file(dev, &sda_fan_alarm[i].dev_attr);
2373 device_remove_file(dev, &sda_fan_div[i].dev_attr);
2374 device_remove_file(dev, &sda_fan_min[i].dev_attr);
2376 for (i = 0; i < NUM_REG_TEMP; i++) {
2377 if (!(data->have_temp & (1 << i)))
2379 device_remove_file(dev, &sda_temp_input[i].dev_attr);
2380 device_remove_file(dev, &sda_temp_label[i].dev_attr);
2381 device_remove_file(dev, &sda_temp_max[i].dev_attr);
2382 device_remove_file(dev, &sda_temp_max_hyst[i].dev_attr);
2385 device_remove_file(dev, &sda_temp_alarm[i].dev_attr);
2386 device_remove_file(dev, &sda_temp_type[i].dev_attr);
2389 device_remove_file(dev, &sda_caseopen[0].dev_attr);
2390 device_remove_file(dev, &sda_caseopen[1].dev_attr);
2392 device_remove_file(dev, &dev_attr_name);
2393 device_remove_file(dev, &dev_attr_cpu0_vid);
2396 /* Get the monitoring functions started */
2397 static inline void __devinit nct6775_init_device(struct nct6775_data *data,
2403 /* Start monitoring if needed */
2404 tmp = nct6775_read_value(data, NCT6775_REG_CONFIG);
2406 nct6775_write_value(data, NCT6775_REG_CONFIG,
2409 /* Enable temperature sensors if needed */
2410 for (i = 0; i < NUM_REG_TEMP; i++) {
2411 if (!(data->have_temp & (1 << i)))
2413 if (!data->reg_temp_config[i])
2415 tmp = nct6775_read_value(data,
2416 data->reg_temp_config[i]);
2418 nct6775_write_value(data,
2419 data->reg_temp_config[i],
2423 /* Enable VBAT monitoring if needed */
2424 tmp = nct6775_read_value(data, NCT6775_REG_VBAT);
2426 nct6775_write_value(data, NCT6775_REG_VBAT, tmp | 0x01);
2428 for (i = 0; i < 3; i++) {
2429 const char *label = NULL;
2431 if (data->temp_label)
2432 label = data->temp_label[data->temp_src[i]];
2434 /* Digital source overrides analog type */
2435 if (label && strncmp(label, "PECI", 4) == 0)
2436 data->temp_type[i] = 6;
2437 else if (label && strncmp(label, "AMD", 3) == 0)
2438 data->temp_type[i] = 5;
2439 else if ((tmp & (0x02 << i)))
2440 data->temp_type[i] = 1; /* diode */
2442 data->temp_type[i] = 4; /* thermistor */
2446 static void w82627ehf_swap_tempreg(struct nct6775_data *data,
2451 tmp = data->temp_src[r1];
2452 data->temp_src[r1] = data->temp_src[r2];
2453 data->temp_src[r2] = tmp;
2455 tmp = data->reg_temp[r1];
2456 data->reg_temp[r1] = data->reg_temp[r2];
2457 data->reg_temp[r2] = tmp;
2459 tmp = data->reg_temp_over[r1];
2460 data->reg_temp_over[r1] = data->reg_temp_over[r2];
2461 data->reg_temp_over[r2] = tmp;
2463 tmp = data->reg_temp_hyst[r1];
2464 data->reg_temp_hyst[r1] = data->reg_temp_hyst[r2];
2465 data->reg_temp_hyst[r2] = tmp;
2467 tmp = data->reg_temp_config[r1];
2468 data->reg_temp_config[r1] = data->reg_temp_config[r2];
2469 data->reg_temp_config[r2] = tmp;
2472 static void __devinit
2473 nct6775_check_fan_inputs(const struct nct6775_sio_data *sio_data,
2474 struct nct6775_data *data)
2477 bool fan3pin, fan3min, fan4pin, fan4min, fan5pin;
2478 bool pwm3pin, pwm4pin, pwm5pin;
2480 superio_enter(sio_data->sioreg);
2482 /* fan4 and fan5 share some pins with the GPIO and serial flash */
2483 if (sio_data->kind == nct6775) {
2484 regval = superio_inb(sio_data->sioreg, 0x2c);
2486 fan3pin = regval & (1 << 6);
2488 pwm3pin = regval & (1 << 7);
2490 /* On NCT6775, fan4 shares pins with the fdc interface */
2491 fan4pin = !(superio_inb(sio_data->sioreg, 0x2A) & 0x80);
2496 } else if (sio_data->kind == nct6776) {
2497 bool gpok = superio_inb(sio_data->sioreg, 0x27) & 0x80;
2499 superio_select(sio_data->sioreg, NCT6775_LD_HWM);
2500 regval = superio_inb(sio_data->sioreg, SIO_REG_ENABLE);
2505 fan3pin = !(superio_inb(sio_data->sioreg, 0x24) & 0x40);
2510 fan4pin = superio_inb(sio_data->sioreg, 0x1C) & 0x01;
2515 fan5pin = superio_inb(sio_data->sioreg, 0x1C) & 0x02;
2522 } else { /* NCT6779D */
2523 regval = superio_inb(sio_data->sioreg, 0x1c);
2525 fan3pin = !(regval & (1 << 5));
2526 fan4pin = !(regval & (1 << 6));
2527 fan5pin = !(regval & (1 << 7));
2529 pwm3pin = !(regval & (1 << 0));
2530 pwm4pin = !(regval & (1 << 1));
2531 pwm5pin = !(regval & (1 << 2));
2537 superio_exit(sio_data->sioreg);
2539 data->has_fan = data->has_fan_min = 0x03; /* fan1 and fan2 */
2540 data->has_fan |= (fan3pin << 2);
2541 data->has_fan_min |= (fan3min << 2);
2543 data->has_fan |= (fan4pin << 3) | (fan5pin << 4);
2544 data->has_fan_min |= (fan4min << 3) | (fan5pin << 4);
2546 data->has_pwm = 0x03 | (pwm3pin << 2) | (pwm4pin << 3) | (pwm5pin << 4);
2549 static int __devinit nct6775_probe(struct platform_device *pdev)
2551 struct device *dev = &pdev->dev;
2552 struct nct6775_sio_data *sio_data = dev->platform_data;
2553 struct nct6775_data *data;
2554 struct resource *res;
2557 res = platform_get_resource(pdev, IORESOURCE_IO, 0);
2558 if (!request_region(res->start, IOREGION_LENGTH, DRVNAME)) {
2560 dev_err(dev, "Failed to request region 0x%lx-0x%lx\n",
2561 (unsigned long)res->start,
2562 (unsigned long)res->start + IOREGION_LENGTH - 1);
2566 data = devm_kzalloc(&pdev->dev, sizeof(struct nct6775_data),
2573 data->addr = res->start;
2574 mutex_init(&data->lock);
2575 mutex_init(&data->update_lock);
2576 data->name = nct6775_device_names[sio_data->kind];
2577 platform_set_drvdata(pdev, data);
2579 switch (sio_data->kind) {
2582 data->have_in = 0x1ff;
2586 data->have_in = 0x7fff;
2590 switch (sio_data->kind) {
2593 data->auto_pwm_num = 6;
2597 data->auto_pwm_num = 4;
2601 data->auto_pwm_num = 4;
2605 /* Default to no temperature inputs, code below will adjust as needed */
2606 data->have_temp = 0;
2608 /* Deal with temperature register setup first. */
2609 if (sio_data->kind == nct6775 || sio_data->kind == nct6776) {
2613 * Display temperature sensor output only if it monitors
2614 * a source other than one already reported. Always display
2615 * first three temperature registers, though.
2617 for (i = 0; i < NUM_REG_TEMP; i++) {
2620 data->reg_temp[i] = NCT6775_REG_TEMP[i];
2621 data->reg_temp_over[i] = NCT6775_REG_TEMP_OVER[i];
2622 data->reg_temp_hyst[i] = NCT6775_REG_TEMP_HYST[i];
2623 if (sio_data->kind == nct6775)
2624 data->reg_temp_config[i]
2625 = NCT6775_REG_TEMP_CONFIG[i];
2627 data->reg_temp_config[i]
2628 = NCT6776_REG_TEMP_CONFIG[i];
2630 src = nct6775_read_value(data,
2631 NCT6775_REG_TEMP_SOURCE[i]);
2633 if (src && !(mask & (1 << src))) {
2634 data->have_temp |= 1 << i;
2638 data->temp_src[i] = src;
2641 * Now do some register swapping if index 0..2 don't
2642 * point to SYSTIN(1), CPUIN(2), and AUXIN(3).
2643 * Idea is to have the first three attributes
2644 * report SYSTIN, CPUIN, and AUXIN if possible
2645 * without overriding the basic system configuration.
2647 if (i > 0 && data->temp_src[0] != 1
2648 && data->temp_src[i] == 1)
2649 w82627ehf_swap_tempreg(data, 0, i);
2650 if (i > 1 && data->temp_src[1] != 2
2651 && data->temp_src[i] == 2)
2652 w82627ehf_swap_tempreg(data, 1, i);
2653 if (i > 2 && data->temp_src[2] != 3
2654 && data->temp_src[i] == 3)
2655 w82627ehf_swap_tempreg(data, 2, i);
2657 if (sio_data->kind == nct6776) {
2659 * On NCT6776, AUXTIN and VIN3 pins are shared.
2660 * Only way to detect it is to check if AUXTIN is used
2661 * as a temperature source, and if that source is
2664 * If that is the case, disable in6, which reports VIN3.
2665 * Otherwise disable temp3.
2667 if (data->temp_src[2] == 3) {
2670 if (data->reg_temp_config[2])
2671 reg = nct6775_read_value(data,
2672 data->reg_temp_config[2]);
2674 reg = 0; /* Assume AUXTIN is used */
2677 data->have_temp &= ~(1 << 2);
2679 data->have_in &= ~(1 << 6);
2681 data->temp_label = nct6776_temp_label;
2683 data->temp_label = nct6775_temp_label;
2685 } else if (sio_data->kind == nct6779) {
2689 * Display temperature sensor output only if it monitors
2690 * a source other than one already reported. Always display
2691 * first three temperature registers, though.
2693 for (i = 0; i < ARRAY_SIZE(NCT6779_REG_TEMP); i++) {
2696 data->reg_temp[i] = NCT6779_REG_TEMP[i];
2697 data->reg_temp_over[i] = NCT6779_REG_TEMP_OVER[i];
2698 data->reg_temp_hyst[i] = NCT6779_REG_TEMP_HYST[i];
2699 data->reg_temp_config[i] = NCT6779_REG_TEMP_CONFIG[i];
2701 src = nct6775_read_value(data,
2702 NCT6779_REG_TEMP_SOURCE[i]);
2704 if (src && !(mask & (1 << src))) {
2705 data->have_temp |= 1 << i;
2709 data->temp_src[i] = src;
2712 * Now do some register swapping if index 0..2 don't
2713 * point to SYSTIN(1), CPUIN(2), and AUXIN(3).
2714 * Idea is to have the first three attributes
2715 * report SYSTIN, CPUIN, and AUXIN if possible
2716 * without overriding the basic system configuration.
2718 if (i > 0 && data->temp_src[0] != 1
2719 && data->temp_src[i] == 1)
2720 w82627ehf_swap_tempreg(data, 0, i);
2721 if (i > 1 && data->temp_src[1] != 2
2722 && data->temp_src[i] == 2)
2723 w82627ehf_swap_tempreg(data, 1, i);
2724 if (i > 2 && data->temp_src[2] != 3
2725 && data->temp_src[i] == 3)
2726 w82627ehf_swap_tempreg(data, 2, i);
2728 data->temp_label = nct6779_temp_label;
2736 * Assume voltage is disabled if the respective temperature is
2737 * used as temperature source.
2739 for (i = 0; i < ARRAY_SIZE(NCT6779_REG_TEMP); i++) {
2740 if (!(data->have_temp & (1 << i)))
2742 if (data->temp_src[i] == 3) /* AUXTIN0 */
2743 data->have_in &= ~(1 << 6); /* no VIN4 */
2744 if (data->temp_src[i] == 4) /* AUXTIN1 */
2745 data->have_in &= ~(1 << 10); /* no VIN5 */
2746 if (data->temp_src[i] == 5) /* AUXTIN2 */
2747 data->have_in &= ~(1 << 11); /* no VIN6 */
2748 if (data->temp_src[i] == 6) /* AUXTIN0 */
2749 data->have_in &= ~(1 << 14); /* no VIN7 */
2753 if (sio_data->kind == nct6775) {
2754 data->has_fan_div = true;
2755 data->fan_from_reg = fan_from_reg16;
2756 data->fan_from_reg_min = fan_from_reg8;
2757 data->REG_VIN = NCT6775_REG_IN;
2758 data->REG_TARGET = NCT6775_REG_TARGET;
2759 data->REG_FAN = NCT6775_REG_FAN;
2760 data->REG_FAN_MIN = NCT6775_REG_FAN_MIN;
2761 data->REG_FAN_START_OUTPUT = NCT6775_REG_FAN_START_OUTPUT;
2762 data->REG_FAN_STOP_OUTPUT = NCT6775_REG_FAN_STOP_OUTPUT;
2763 data->REG_FAN_MAX_OUTPUT = NCT6775_REG_FAN_MAX_OUTPUT;
2764 data->REG_FAN_STEP_OUTPUT = NCT6775_REG_FAN_STEP_OUTPUT;
2765 } else if (sio_data->kind == nct6776) {
2766 data->has_fan_div = false;
2767 data->fan_from_reg = fan_from_reg13;
2768 data->fan_from_reg_min = fan_from_reg13;
2769 data->REG_VIN = NCT6775_REG_IN;
2770 data->REG_TARGET = NCT6775_REG_TARGET;
2771 data->REG_FAN = NCT6775_REG_FAN;
2772 data->REG_FAN_MIN = NCT6776_REG_FAN_MIN;
2773 data->REG_FAN_START_OUTPUT = NCT6775_REG_FAN_START_OUTPUT;
2774 data->REG_FAN_STOP_OUTPUT = NCT6775_REG_FAN_STOP_OUTPUT;
2775 } else if (sio_data->kind == nct6779) {
2776 data->has_fan_div = false;
2777 data->fan_from_reg = fan_from_reg13;
2778 data->fan_from_reg_min = fan_from_reg13;
2779 data->REG_VIN = NCT6779_REG_IN;
2780 data->REG_TARGET = NCT6775_REG_TARGET;
2781 data->REG_FAN = NCT6779_REG_FAN;
2782 data->REG_FAN_MIN = NCT6776_REG_FAN_MIN;
2783 data->REG_FAN_START_OUTPUT = NCT6775_REG_FAN_START_OUTPUT;
2784 data->REG_FAN_STOP_OUTPUT = NCT6775_REG_FAN_STOP_OUTPUT;
2787 /* Initialize the chip */
2788 nct6775_init_device(data, sio_data->kind);
2790 data->vrm = vid_which_vrm();
2791 superio_enter(sio_data->sioreg);
2794 * We can get the VID input values directly at logical device D 0xe3.
2796 superio_select(sio_data->sioreg, NCT6775_LD_VID);
2797 data->vid = superio_inb(sio_data->sioreg, 0xe3);
2798 err = device_create_file(dev, &dev_attr_cpu0_vid);
2805 superio_select(sio_data->sioreg, NCT6775_LD_HWM);
2806 tmp = superio_inb(sio_data->sioreg, NCT6775_REG_FAN_DEBOUNCE);
2807 if (sio_data->kind == nct6776 || sio_data->kind == nct6779)
2808 superio_outb(sio_data->sioreg, NCT6775_REG_FAN_DEBOUNCE,
2811 superio_outb(sio_data->sioreg, NCT6775_REG_FAN_DEBOUNCE,
2813 pr_info("Enabled fan debounce for chip %s\n", data->name);
2816 superio_exit(sio_data->sioreg);
2818 nct6775_check_fan_inputs(sio_data, data);
2820 /* Read fan clock dividers immediately */
2821 nct6775_update_fan_div_common(dev, data);
2823 /* Register sysfs hooks */
2824 for (i = 0; i < data->pwm_num; i++) {
2825 if (!(data->has_pwm & (1 << i)))
2827 err = sysfs_create_group(&dev->kobj, &nct6775_group_pwm[i]);
2831 for (i = 0; i < ARRAY_SIZE(sda_auto_pwm_arrays); i++) {
2832 struct sensor_device_attribute_2 *attr =
2833 &sda_auto_pwm_arrays[i];
2835 if (!(data->has_pwm & (1 << attr->nr)))
2837 if (attr->index > data->auto_pwm_num)
2839 err = device_create_file(dev, &attr->dev_attr);
2844 if (data->REG_FAN_STEP_OUTPUT) {
2845 for (i = 0; i < ARRAY_SIZE(sda_sf3_max_step_arrays); i++) {
2846 struct sensor_device_attribute *attr =
2847 &sda_sf3_max_step_arrays[i];
2848 if (!(data->has_pwm & (1 << attr->index)))
2850 err = device_create_file(dev, &attr->dev_attr);
2856 for (i = 0; i < data->in_num; i++) {
2857 if (!(data->have_in & (1 << i)))
2859 err = sysfs_create_group(&dev->kobj, &nct6775_group_in[i]);
2864 for (i = 0; i < 5; i++) {
2865 if (data->has_fan & (1 << i)) {
2866 err = device_create_file(dev,
2867 &sda_fan_input[i].dev_attr);
2870 err = device_create_file(dev,
2871 &sda_fan_alarm[i].dev_attr);
2874 if (sio_data->kind != nct6776 &&
2875 sio_data->kind != nct6779) {
2876 err = device_create_file(dev,
2877 &sda_fan_div[i].dev_attr);
2881 if (data->has_fan_min & (1 << i)) {
2882 err = device_create_file(dev,
2883 &sda_fan_min[i].dev_attr);
2890 for (i = 0; i < NUM_REG_TEMP; i++) {
2891 if (!(data->have_temp & (1 << i)))
2893 err = device_create_file(dev, &sda_temp_input[i].dev_attr);
2896 if (data->temp_label) {
2897 err = device_create_file(dev,
2898 &sda_temp_label[i].dev_attr);
2902 if (data->reg_temp_over[i]) {
2903 err = device_create_file(dev,
2904 &sda_temp_max[i].dev_attr);
2908 if (data->reg_temp_hyst[i]) {
2909 err = device_create_file(dev,
2910 &sda_temp_max_hyst[i].dev_attr);
2916 err = device_create_file(dev, &sda_temp_alarm[i].dev_attr);
2919 err = device_create_file(dev, &sda_temp_type[i].dev_attr);
2924 err = device_create_file(dev, &sda_caseopen[0].dev_attr);
2928 if (sio_data->kind == nct6776 || sio_data->kind == nct6779) {
2929 err = device_create_file(dev, &sda_caseopen[1].dev_attr);
2934 err = device_create_file(dev, &dev_attr_name);
2938 data->hwmon_dev = hwmon_device_register(dev);
2939 if (IS_ERR(data->hwmon_dev)) {
2940 err = PTR_ERR(data->hwmon_dev);
2947 nct6775_device_remove_files(dev);
2949 platform_set_drvdata(pdev, NULL);
2950 release_region(res->start, IOREGION_LENGTH);
2955 static int __devexit nct6775_remove(struct platform_device *pdev)
2957 struct nct6775_data *data = platform_get_drvdata(pdev);
2959 hwmon_device_unregister(data->hwmon_dev);
2960 nct6775_device_remove_files(&pdev->dev);
2961 release_region(data->addr, IOREGION_LENGTH);
2962 platform_set_drvdata(pdev, NULL);
2967 static struct platform_driver nct6775_driver = {
2969 .owner = THIS_MODULE,
2972 .probe = nct6775_probe,
2973 .remove = __devexit_p(nct6775_remove),
2976 /* nct6775_find() looks for a '627 in the Super-I/O config space */
2977 static int __init nct6775_find(int sioaddr, unsigned short *addr,
2978 struct nct6775_sio_data *sio_data)
2980 static const char __initdata sio_name_NCT6775[] = "NCT6775F";
2981 static const char __initdata sio_name_NCT6776[] = "NCT6776F";
2982 static const char __initdata sio_name_NCT6779[] = "NCT6779F";
2985 const char *sio_name;
2987 superio_enter(sioaddr);
2992 val = (superio_inb(sioaddr, SIO_REG_DEVID) << 8)
2993 | superio_inb(sioaddr, SIO_REG_DEVID + 1);
2994 switch (val & SIO_ID_MASK) {
2995 case SIO_NCT6775_ID:
2996 sio_data->kind = nct6775;
2997 sio_name = sio_name_NCT6775;
2999 case SIO_NCT6776_ID:
3000 sio_data->kind = nct6776;
3001 sio_name = sio_name_NCT6776;
3003 case SIO_NCT6779_ID:
3004 sio_data->kind = nct6779;
3005 sio_name = sio_name_NCT6779;
3009 pr_debug("unsupported chip ID: 0x%04x\n", val);
3010 superio_exit(sioaddr);
3014 /* We have a known chip, find the HWM I/O address */
3015 superio_select(sioaddr, NCT6775_LD_HWM);
3016 val = (superio_inb(sioaddr, SIO_REG_ADDR) << 8)
3017 | superio_inb(sioaddr, SIO_REG_ADDR + 1);
3018 *addr = val & IOREGION_ALIGNMENT;
3020 pr_err("Refusing to enable a Super-I/O device with a base I/O port 0\n");
3021 superio_exit(sioaddr);
3025 /* Activate logical device if needed */
3026 val = superio_inb(sioaddr, SIO_REG_ENABLE);
3027 if (!(val & 0x01)) {
3028 pr_warn("Forcibly enabling Super-I/O. "
3029 "Sensor is probably unusable.\n");
3030 superio_outb(sioaddr, SIO_REG_ENABLE, val | 0x01);
3033 superio_exit(sioaddr);
3034 pr_info("Found %s chip at %#x\n", sio_name, *addr);
3035 sio_data->sioreg = sioaddr;
3041 * when Super-I/O functions move to a separate file, the Super-I/O
3042 * bus will manage the lifetime of the device and this module will only keep
3043 * track of the nct6775 driver. But since we platform_device_alloc(), we
3044 * must keep track of the device
3046 static struct platform_device *pdev;
3048 static int __init sensors_nct6775_init(void)
3051 unsigned short address;
3052 struct resource res;
3053 struct nct6775_sio_data sio_data;
3056 * initialize sio_data->kind and sio_data->sioreg.
3058 * when Super-I/O functions move to a separate file, the Super-I/O
3059 * driver will probe 0x2e and 0x4e and auto-detect the presence of a
3060 * nct6775 hardware monitor, and call probe()
3062 if (nct6775_find(0x2e, &address, &sio_data) &&
3063 nct6775_find(0x4e, &address, &sio_data))
3066 err = platform_driver_register(&nct6775_driver);
3070 pdev = platform_device_alloc(DRVNAME, address);
3073 pr_err("Device allocation failed\n");
3074 goto exit_unregister;
3077 err = platform_device_add_data(pdev, &sio_data,
3078 sizeof(struct nct6775_sio_data));
3080 pr_err("Platform data allocation failed\n");
3081 goto exit_device_put;
3084 memset(&res, 0, sizeof(res));
3086 res.start = address + IOREGION_OFFSET;
3087 res.end = address + IOREGION_OFFSET + IOREGION_LENGTH - 1;
3088 res.flags = IORESOURCE_IO;
3090 err = acpi_check_resource_conflict(&res);
3092 goto exit_device_put;
3094 err = platform_device_add_resources(pdev, &res, 1);
3096 pr_err("Device resource addition failed (%d)\n", err);
3097 goto exit_device_put;
3100 /* platform_device_add calls probe() */
3101 err = platform_device_add(pdev);
3103 pr_err("Device addition failed (%d)\n", err);
3104 goto exit_device_put;
3110 platform_device_put(pdev);
3112 platform_driver_unregister(&nct6775_driver);
3117 static void __exit sensors_nct6775_exit(void)
3119 platform_device_unregister(pdev);
3120 platform_driver_unregister(&nct6775_driver);
3123 MODULE_AUTHOR("Guenter Roeck <linux@roeck-us.net>");
3124 MODULE_DESCRIPTION("NCT677x driver");
3125 MODULE_LICENSE("GPL");
3127 module_init(sensors_nct6775_init);
3128 module_exit(sensors_nct6775_exit);