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 };
210 static const u16 NCT6775_REG_PWM_READ[] = { 0x01, 0x03, 0x11, 0x13, 0x15 };
212 static const u16 NCT6775_REG_FAN_MAX_OUTPUT[] = { 0x10a, 0x20a, 0x30a };
213 static const u16 NCT6775_REG_FAN_STEP_OUTPUT[] = { 0x10b, 0x20b, 0x30b };
214 static const u16 NCT6775_REG_FAN[] = { 0x630, 0x632, 0x634, 0x636, 0x638 };
216 static const u16 NCT6776_REG_FAN_MIN[] = { 0x63a, 0x63c, 0x63e, 0x640, 0x642};
218 static const u16 NCT6779_REG_TOLERANCE_H[] = {
219 0x10c, 0x20c, 0x30c, 0x40c, 0x50c };
221 static const u16 NCT6779_REG_FAN[] = { 0x4c0, 0x4c2, 0x4c4, 0x4c6, 0x4c8 };
223 static const u16 NCT6775_REG_TEMP[]
224 = { 0x27, 0x150, 0x250, 0x73, 0x75, 0x77, 0x62b, 0x62c, 0x62d };
225 static const u16 NCT6775_REG_TEMP_CONFIG[]
226 = { 0, 0x152, 0x252, 0, 0, 0, 0x628, 0x629, 0x62A };
227 static const u16 NCT6775_REG_TEMP_HYST[]
228 = { 0x3a, 0x153, 0x253, 0, 0, 0, 0x673, 0x678, 0x67D };
229 static const u16 NCT6775_REG_TEMP_OVER[]
230 = { 0x39, 0x155, 0x255, 0, 0, 0, 0x672, 0x677, 0x67C };
231 static const u16 NCT6775_REG_TEMP_SOURCE[]
232 = { 0x621, 0x622, 0x623, 0x100, 0x200, 0x300, 0x624, 0x625, 0x626 };
234 static const u16 NCT6776_REG_TEMP_CONFIG[]
235 = { 0x18, 0x152, 0x252, 0, 0, 0, 0x628, 0x629, 0x62A };
237 static const u16 NCT6779_REG_TEMP[]
238 = { 0x27, 0x150, 0x73, 0x75, 0x77, 0x79, 0x7b };
239 static const u16 NCT6779_REG_TEMP_CONFIG[]
240 = { 0x18, 0x152, 0, 0, 0, 0, 0 };
241 static const u16 NCT6779_REG_TEMP_SOURCE[]
242 = { 0x621, 0x622, 0x100, 0x200, 0x300, 0x800, 0x900 };
243 static const u16 NCT6779_REG_TEMP_HYST[]
244 = { 0x3a, 0x153, 0, 0, 0, 0, 0, 0, 0 };
245 static const u16 NCT6779_REG_TEMP_OVER[]
246 = { 0x39, 0x155, 0, 0, 0, 0, 0, 0, 0 };
248 static const u16 NCT6775_REG_AUTO_BASE[] = {
249 0x100, 0x200, 0x300, 0x800, 0x900 };
251 #define NCT6775_REG_AUTO_TEMP(nr, p) (NCT6775_REG_AUTO_BASE[nr] + 0x21 + (p))
252 #define NCT6775_REG_AUTO_PWM(nr, p) (NCT6775_REG_AUTO_BASE[nr] + 0x27 + (p))
254 static const u16 NCT6775_REG_CRITICAL_ENAB[] = { 0x134, 0x234, 0x334 };
255 static const u16 NCT6775_REG_CRITICAL_TEMP[] = {
256 0x135, 0x235, 0x335, 0x835, 0x935 };
257 static const u16 NCT6775_REG_CRITICAL_TEMP_TOLERANCE[] = {
258 0x138, 0x238, 0x338, 0x838, 0x938 };
260 static const u16 NCT6779_REG_CRITICAL_PWM_ENABLE[] = {
261 0x136, 0x236, 0x336, 0x836, 0x936 };
262 static const u16 NCT6779_REG_CRITICAL_PWM[] = {
263 0x137, 0x237, 0x337, 0x837, 0x937 };
265 static const char *const nct6775_temp_label[] = {
279 "PCH_CHIP_CPU_MAX_TEMP",
289 static const char *const nct6776_temp_label[] = {
304 "PCH_CHIP_CPU_MAX_TEMP",
315 static const char *const nct6779_temp_label[] = {
334 "PCH_CHIP_CPU_MAX_TEMP",
345 #define NUM_REG_TEMP ARRAY_SIZE(NCT6775_REG_TEMP)
347 static int is_word_sized(u16 reg)
349 return ((((reg & 0xff00) == 0x100
350 || (reg & 0xff00) == 0x200)
351 && ((reg & 0x00ff) == 0x50
352 || (reg & 0x00ff) == 0x53
353 || (reg & 0x00ff) == 0x55))
354 || (reg & 0xfff0) == 0x630
355 || (reg & 0xfff0) == 0x4c0
356 || reg == 0x640 || reg == 0x642
357 || ((reg & 0xfff0) == 0x650
358 && (reg & 0x000f) >= 0x06)
359 || reg == 0x73 || reg == 0x75 || reg == 0x77 || reg == 0x77
368 /* 1 is PWM mode, output in ms */
369 static inline unsigned int step_time_from_reg(u8 reg, u8 mode)
371 return mode ? 100 * reg : 400 * reg;
374 static inline u8 step_time_to_reg(unsigned int msec, u8 mode)
376 return SENSORS_LIMIT((mode ? (msec + 50) / 100 :
377 (msec + 200) / 400), 1, 255);
380 static unsigned int fan_from_reg8(u16 reg, unsigned int divreg)
382 if (reg == 0 || reg == 255)
384 return 1350000U / (reg << divreg);
387 static unsigned int fan_from_reg13(u16 reg, unsigned int divreg)
389 if ((reg & 0xff1f) == 0xff1f)
392 reg = (reg & 0x1f) | ((reg & 0xff00) >> 3);
397 return 1350000U / reg;
400 static unsigned int fan_from_reg16(u16 reg, unsigned int divreg)
402 if (reg == 0 || reg == 0xffff)
406 * Even though the registers are 16 bit wide, the fan divisor
409 return 1350000U / (reg << divreg);
412 static inline unsigned int
419 * Some of the voltage inputs have internal scaling, the tables below
420 * contain 8 (the ADC LSB in mV) * scaling factor * 100
422 static const u16 scale_in[15] = {
423 800, 800, 1600, 1600, 800, 800, 800, 1600, 1600, 800, 800, 800, 800,
427 static inline long in_from_reg(u8 reg, u8 nr)
429 return DIV_ROUND_CLOSEST(reg * scale_in[nr], 100);
432 static inline u8 in_to_reg(u32 val, u8 nr)
434 return SENSORS_LIMIT(DIV_ROUND_CLOSEST(val * 100, scale_in[nr]), 0,
439 * Data structures and manipulation thereof
442 struct nct6775_data {
443 int addr; /* IO base of hw monitor block */
446 struct device *hwmon_dev;
449 u16 reg_temp[NUM_REG_TEMP];
450 u16 reg_temp_over[NUM_REG_TEMP];
451 u16 reg_temp_hyst[NUM_REG_TEMP];
452 u16 reg_temp_config[NUM_REG_TEMP];
453 u8 temp_src[NUM_REG_TEMP];
454 const char * const *temp_label;
458 const u16 *REG_TARGET;
460 const u16 *REG_FAN_MIN;
461 const u16 *REG_FAN_START_OUTPUT;
462 const u16 *REG_FAN_STOP_OUTPUT;
463 const u16 *REG_FAN_MAX_OUTPUT;
464 const u16 *REG_FAN_STEP_OUTPUT;
466 unsigned int (*fan_from_reg)(u16 reg, unsigned int divreg);
467 unsigned int (*fan_from_reg_min)(u16 reg, unsigned int divreg);
469 struct mutex update_lock;
470 char valid; /* !=0 if following fields are valid */
471 unsigned long last_updated; /* In jiffies */
473 /* Register values */
474 u8 bank; /* current register bank */
475 u8 in_num; /* number of in inputs we have */
476 u8 in[15]; /* Register value */
477 u8 in_max[15]; /* Register value */
478 u8 in_min[15]; /* Register value */
483 u8 has_fan; /* some fan inputs can be disabled */
484 u8 has_fan_min; /* some fans don't have min register */
489 s16 temp_max_hyst[9];
493 u8 pwm_mode[5]; /* 0->DC variable voltage, 1->PWM variable duty cycle */
494 u8 pwm_enable[5]; /* 1->manual
495 * 2->thermal cruise mode (also called SmartFan I)
496 * 3->fan speed cruise mode
497 * 4->variable thermal cruise (also called
499 * 5->enhanced variable thermal cruise (also called
502 u8 pwm_num; /* number of pwm */
507 u8 fan_start_output[5]; /* minimum fan speed when spinning up */
508 u8 fan_stop_output[5]; /* minimum fan speed when spinning down */
509 u8 fan_stop_time[5]; /* time at minimum before disabling fan */
510 u8 fan_step_up_time[5];
511 u8 fan_step_down_time[5];
512 u8 fan_max_output[5]; /* maximum fan speed */
513 u8 fan_step_output[5]; /* rate of change output value */
515 /* Automatic fan speed control registers */
527 struct nct6775_sio_data {
533 * On older chips, only registers 0x50-0x5f are banked.
534 * On more recent chips, all registers are banked.
535 * Assume that is the case and set the bank number for each access.
536 * Cache the bank number so it only needs to be set if it changes.
538 static inline void nct6775_set_bank(struct nct6775_data *data, u16 reg)
541 if (data->bank != bank) {
542 outb_p(NCT6775_REG_BANK, data->addr + ADDR_REG_OFFSET);
543 outb_p(bank, data->addr + DATA_REG_OFFSET);
548 static u16 nct6775_read_value(struct nct6775_data *data, u16 reg)
550 int res, word_sized = is_word_sized(reg);
552 mutex_lock(&data->lock);
554 nct6775_set_bank(data, reg);
555 outb_p(reg & 0xff, data->addr + ADDR_REG_OFFSET);
556 res = inb_p(data->addr + DATA_REG_OFFSET);
558 outb_p((reg & 0xff) + 1,
559 data->addr + ADDR_REG_OFFSET);
560 res = (res << 8) + inb_p(data->addr + DATA_REG_OFFSET);
563 mutex_unlock(&data->lock);
567 static int nct6775_write_value(struct nct6775_data *data, u16 reg,
570 int word_sized = is_word_sized(reg);
572 mutex_lock(&data->lock);
574 nct6775_set_bank(data, reg);
575 outb_p(reg & 0xff, data->addr + ADDR_REG_OFFSET);
577 outb_p(value >> 8, data->addr + DATA_REG_OFFSET);
578 outb_p((reg & 0xff) + 1,
579 data->addr + ADDR_REG_OFFSET);
581 outb_p(value & 0xff, data->addr + DATA_REG_OFFSET);
583 mutex_unlock(&data->lock);
587 /* We left-align 8-bit temperature values to make the code simpler */
588 static u16 nct6775_read_temp(struct nct6775_data *data, u16 reg)
592 res = nct6775_read_value(data, reg);
593 if (!is_word_sized(reg))
599 static int nct6775_write_temp(struct nct6775_data *data, u16 reg,
602 if (!is_word_sized(reg))
604 return nct6775_write_value(data, reg, value);
607 /* This function assumes that the caller holds data->update_lock */
608 static void nct6775_write_fan_div(struct nct6775_data *data, int nr)
614 reg = (nct6775_read_value(data, NCT6775_REG_FANDIV1) & 0x70)
615 | (data->fan_div[0] & 0x7);
616 nct6775_write_value(data, NCT6775_REG_FANDIV1, reg);
619 reg = (nct6775_read_value(data, NCT6775_REG_FANDIV1) & 0x7)
620 | ((data->fan_div[1] << 4) & 0x70);
621 nct6775_write_value(data, NCT6775_REG_FANDIV1, reg);
623 reg = (nct6775_read_value(data, NCT6775_REG_FANDIV2) & 0x70)
624 | (data->fan_div[2] & 0x7);
625 nct6775_write_value(data, NCT6775_REG_FANDIV2, reg);
628 reg = (nct6775_read_value(data, NCT6775_REG_FANDIV2) & 0x7)
629 | ((data->fan_div[3] << 4) & 0x70);
630 nct6775_write_value(data, NCT6775_REG_FANDIV2, reg);
635 static void nct6775_write_fan_div_common(struct device *dev,
636 struct nct6775_data *data, int nr)
638 struct nct6775_sio_data *sio_data = dev->platform_data;
640 if (sio_data->kind == nct6775)
641 nct6775_write_fan_div(data, nr);
644 static void nct6775_update_fan_div(struct nct6775_data *data)
648 i = nct6775_read_value(data, NCT6775_REG_FANDIV1);
649 data->fan_div[0] = i & 0x7;
650 data->fan_div[1] = (i & 0x70) >> 4;
651 i = nct6775_read_value(data, NCT6775_REG_FANDIV2);
652 data->fan_div[2] = i & 0x7;
653 if (data->has_fan & (1<<3))
654 data->fan_div[3] = (i & 0x70) >> 4;
657 static void nct6775_update_fan_div_common(struct device *dev,
658 struct nct6775_data *data)
660 struct nct6775_sio_data *sio_data = dev->platform_data;
662 if (sio_data->kind == nct6775)
663 nct6775_update_fan_div(data);
666 static void nct6775_update_pwm(struct device *dev)
668 struct nct6775_data *data = dev_get_drvdata(dev);
669 struct nct6775_sio_data *sio_data = dev->platform_data;
671 int pwmcfg, fanmodecfg, mode, tol;
673 for (i = 0; i < data->pwm_num; i++) {
676 !(nct6775_read_value(data,
677 NCT6775_REG_PWM_ENABLE[0]) & 1);
681 data->pwm_mode[i] = pwmcfg;
683 fanmodecfg = nct6775_read_value(data,
684 NCT6775_REG_FAN_MODE[i]);
685 data->pwm[i] = nct6775_read_value(data, NCT6775_REG_PWM[i]);
686 mode = ((fanmodecfg >> 4) & 7);
687 if (data->pwm[i] == 255 && mode == 0)
688 data->pwm_enable[i] = 0;
690 data->pwm_enable[i] = mode + 1;
691 data->tolerance[i][0] = fanmodecfg & 0x0f;
692 if (sio_data->kind == nct6779) {
693 tol = nct6775_read_value(data,
694 NCT6779_REG_TOLERANCE_H[i]);
695 data->tolerance[i][0] |= (tol & 0x70) >> 1;
697 data->tolerance[i][1] =
698 nct6775_read_value(data,
699 NCT6775_REG_CRITICAL_TEMP_TOLERANCE[i]);
703 static void nct6775_update_pwm_limits(struct device *dev)
705 struct nct6775_data *data = dev_get_drvdata(dev);
706 struct nct6775_sio_data *sio_data = dev->platform_data;
710 for (i = 0; i < data->pwm_num; i++) {
711 if (!(data->has_pwm & (1 << i)))
714 data->fan_start_output[i] =
715 nct6775_read_value(data, data->REG_FAN_START_OUTPUT[i]);
716 data->fan_stop_output[i] =
717 nct6775_read_value(data, data->REG_FAN_STOP_OUTPUT[i]);
718 data->fan_stop_time[i] =
719 nct6775_read_value(data, NCT6775_REG_FAN_STOP_TIME[i]);
720 data->fan_step_up_time[i] =
721 nct6775_read_value(data, NCT6775_REG_FAN_STEP_UP_TIME[i]);
722 data->fan_step_down_time[i] =
723 nct6775_read_value(data, NCT6775_REG_FAN_STEP_DOWN_TIME[i]);
725 if (data->REG_FAN_MAX_OUTPUT)
726 data->fan_max_output[i] =
727 nct6775_read_value(data,
728 data->REG_FAN_MAX_OUTPUT[i]);
730 if (data->REG_FAN_STEP_OUTPUT)
731 data->fan_step_output[i] =
732 nct6775_read_value(data,
733 data->REG_FAN_STEP_OUTPUT[i]);
735 data->target_temp[i] =
736 nct6775_read_value(data,
737 data->REG_TARGET[i]) &
738 (data->pwm_mode[i] == 1 ? 0x7f : 0xff);
739 for (j = 0; j < data->auto_pwm_num; j++) {
740 data->auto_pwm[i][j] =
741 nct6775_read_value(data, NCT6775_REG_AUTO_PWM(i, j));
742 data->auto_temp[i][j] =
743 nct6775_read_value(data, NCT6775_REG_AUTO_TEMP(i, j));
745 /* handle critical auto_pwm temperature data */
746 data->auto_temp[i][data->auto_pwm_num] =
747 nct6775_read_value(data, NCT6775_REG_CRITICAL_TEMP[i]);
749 switch (sio_data->kind) {
751 reg = nct6775_read_value(data,
752 NCT6775_REG_CRITICAL_ENAB[i]);
753 data->auto_pwm[i][data->auto_pwm_num] =
754 (reg & 0x02) ? 0xff : 0x00;
757 data->auto_pwm[i][data->auto_pwm_num] = 0xff;
760 reg = nct6775_read_value(data,
761 NCT6779_REG_CRITICAL_PWM_ENABLE[i]);
763 data->auto_pwm[i][data->auto_pwm_num] =
764 nct6775_read_value(data,
765 NCT6779_REG_CRITICAL_PWM[i]);
767 data->auto_pwm[i][data->auto_pwm_num] = 0xff;
773 static struct nct6775_data *nct6775_update_device(struct device *dev)
775 struct nct6775_data *data = dev_get_drvdata(dev);
776 struct nct6775_sio_data *sio_data = dev->platform_data;
780 mutex_lock(&data->update_lock);
782 if (time_after(jiffies, data->last_updated + HZ + HZ/2)
784 /* Fan clock dividers */
785 nct6775_update_fan_div_common(dev, data);
787 /* Measured voltages and limits */
788 for (i = 0; i < data->in_num; i++) {
789 if (!(data->have_in & (1 << i)))
792 data->in[i] = nct6775_read_value(data,
794 data->in_min[i] = nct6775_read_value(data,
795 NCT6775_REG_IN_MIN(i));
796 data->in_max[i] = nct6775_read_value(data,
797 NCT6775_REG_IN_MAX(i));
800 /* Measured fan speeds and limits */
801 for (i = 0; i < 5; i++) {
804 if (!(data->has_fan & (1 << i)))
807 reg = nct6775_read_value(data, data->REG_FAN[i]);
808 data->rpm[i] = data->fan_from_reg(reg,
811 if (data->has_fan_min & (1 << i))
812 data->fan_min[i] = nct6775_read_value(data,
813 data->REG_FAN_MIN[i]);
816 * If we failed to measure the fan speed and clock
817 * divider can be increased, let's try that for next
820 if (data->has_fan_div
821 && (reg >= 0xff || (sio_data->kind == nct6775
823 && data->fan_div[i] < 0x07) {
824 dev_dbg(dev, "Increasing fan%d "
825 "clock divider from %u to %u\n",
826 i + 1, div_from_reg(data->fan_div[i]),
827 div_from_reg(data->fan_div[i] + 1));
829 nct6775_write_fan_div_common(dev, data, i);
830 /* Preserve min limit if possible */
831 if ((data->has_fan_min & (1 << i))
832 && data->fan_min[i] >= 2
833 && data->fan_min[i] != 255)
834 nct6775_write_value(data,
835 data->REG_FAN_MIN[i],
836 (data->fan_min[i] /= 2));
840 nct6775_update_pwm(dev);
841 nct6775_update_pwm_limits(dev);
843 /* Measured temperatures and limits */
844 for (i = 0; i < NUM_REG_TEMP; i++) {
845 if (!(data->have_temp & (1 << i)))
847 data->temp[i] = nct6775_read_temp(data,
849 if (data->reg_temp_over[i])
851 = nct6775_read_temp(data,
852 data->reg_temp_over[i]);
853 if (data->reg_temp_hyst[i])
854 data->temp_max_hyst[i]
855 = nct6775_read_temp(data,
856 data->reg_temp_hyst[i]);
859 data->alarms = nct6775_read_value(data,
860 NCT6775_REG_ALARM1) |
861 (nct6775_read_value(data,
862 NCT6775_REG_ALARM2) << 8) |
863 (nct6775_read_value(data,
864 NCT6775_REG_ALARM3) << 16);
865 if (sio_data->kind == nct6779) {
866 data->alarms |= nct6775_read_value(data,
867 NCT6779_REG_ALARM4) << 24;
870 data->caseopen = nct6775_read_value(data,
871 NCT6775_REG_CASEOPEN_DET);
873 data->last_updated = jiffies;
877 mutex_unlock(&data->update_lock);
882 * Sysfs callback functions
884 #define show_in_reg(reg) \
886 show_##reg(struct device *dev, struct device_attribute *attr, \
889 struct nct6775_data *data = nct6775_update_device(dev); \
890 struct sensor_device_attribute *sensor_attr = \
891 to_sensor_dev_attr(attr); \
892 int nr = sensor_attr->index; \
893 return sprintf(buf, "%ld\n", in_from_reg(data->reg[nr], nr)); \
899 #define store_in_reg(REG, reg) \
901 store_in_##reg(struct device *dev, struct device_attribute *attr, \
902 const char *buf, size_t count) \
904 struct nct6775_data *data = dev_get_drvdata(dev); \
905 struct sensor_device_attribute *sensor_attr = \
906 to_sensor_dev_attr(attr); \
907 int nr = sensor_attr->index; \
909 int err = kstrtoul(buf, 10, &val); \
912 mutex_lock(&data->update_lock); \
913 data->in_##reg[nr] = in_to_reg(val, nr); \
914 nct6775_write_value(data, NCT6775_REG_IN_##REG(nr), \
915 data->in_##reg[nr]); \
916 mutex_unlock(&data->update_lock); \
920 store_in_reg(MIN, min)
921 store_in_reg(MAX, max)
923 static ssize_t show_alarm(struct device *dev, struct device_attribute *attr,
926 struct nct6775_data *data = nct6775_update_device(dev);
927 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
928 int nr = sensor_attr->index;
929 return sprintf(buf, "%u\n", (data->alarms >> nr) & 0x01);
932 static SENSOR_DEVICE_ATTR(in0_input, S_IRUGO, show_in, NULL, 0);
933 static SENSOR_DEVICE_ATTR(in1_input, S_IRUGO, show_in, NULL, 1);
934 static SENSOR_DEVICE_ATTR(in2_input, S_IRUGO, show_in, NULL, 2);
935 static SENSOR_DEVICE_ATTR(in3_input, S_IRUGO, show_in, NULL, 3);
936 static SENSOR_DEVICE_ATTR(in4_input, S_IRUGO, show_in, NULL, 4);
937 static SENSOR_DEVICE_ATTR(in5_input, S_IRUGO, show_in, NULL, 5);
938 static SENSOR_DEVICE_ATTR(in6_input, S_IRUGO, show_in, NULL, 6);
939 static SENSOR_DEVICE_ATTR(in7_input, S_IRUGO, show_in, NULL, 7);
940 static SENSOR_DEVICE_ATTR(in8_input, S_IRUGO, show_in, NULL, 8);
941 static SENSOR_DEVICE_ATTR(in9_input, S_IRUGO, show_in, NULL, 9);
942 static SENSOR_DEVICE_ATTR(in10_input, S_IRUGO, show_in, NULL, 10);
943 static SENSOR_DEVICE_ATTR(in11_input, S_IRUGO, show_in, NULL, 11);
944 static SENSOR_DEVICE_ATTR(in12_input, S_IRUGO, show_in, NULL, 12);
945 static SENSOR_DEVICE_ATTR(in13_input, S_IRUGO, show_in, NULL, 13);
946 static SENSOR_DEVICE_ATTR(in14_input, S_IRUGO, show_in, NULL, 14);
948 static SENSOR_DEVICE_ATTR(in0_alarm, S_IRUGO, show_alarm, NULL, 0);
949 static SENSOR_DEVICE_ATTR(in1_alarm, S_IRUGO, show_alarm, NULL, 1);
950 static SENSOR_DEVICE_ATTR(in2_alarm, S_IRUGO, show_alarm, NULL, 2);
951 static SENSOR_DEVICE_ATTR(in3_alarm, S_IRUGO, show_alarm, NULL, 3);
952 static SENSOR_DEVICE_ATTR(in4_alarm, S_IRUGO, show_alarm, NULL, 8);
953 static SENSOR_DEVICE_ATTR(in5_alarm, S_IRUGO, show_alarm, NULL, 21);
954 static SENSOR_DEVICE_ATTR(in6_alarm, S_IRUGO, show_alarm, NULL, 20);
955 static SENSOR_DEVICE_ATTR(in7_alarm, S_IRUGO, show_alarm, NULL, 16);
956 static SENSOR_DEVICE_ATTR(in8_alarm, S_IRUGO, show_alarm, NULL, 17);
957 static SENSOR_DEVICE_ATTR(in9_alarm, S_IRUGO, show_alarm, NULL, 24);
958 static SENSOR_DEVICE_ATTR(in10_alarm, S_IRUGO, show_alarm, NULL, 25);
959 static SENSOR_DEVICE_ATTR(in11_alarm, S_IRUGO, show_alarm, NULL, 26);
960 static SENSOR_DEVICE_ATTR(in12_alarm, S_IRUGO, show_alarm, NULL, 27);
961 static SENSOR_DEVICE_ATTR(in13_alarm, S_IRUGO, show_alarm, NULL, 28);
962 static SENSOR_DEVICE_ATTR(in14_alarm, S_IRUGO, show_alarm, NULL, 29);
964 static SENSOR_DEVICE_ATTR(in0_min, S_IWUSR | S_IRUGO, show_in_min,
966 static SENSOR_DEVICE_ATTR(in1_min, S_IWUSR | S_IRUGO, show_in_min,
968 static SENSOR_DEVICE_ATTR(in2_min, S_IWUSR | S_IRUGO, show_in_min,
970 static SENSOR_DEVICE_ATTR(in3_min, S_IWUSR | S_IRUGO, show_in_min,
972 static SENSOR_DEVICE_ATTR(in4_min, S_IWUSR | S_IRUGO, show_in_min,
974 static SENSOR_DEVICE_ATTR(in5_min, S_IWUSR | S_IRUGO, show_in_min,
976 static SENSOR_DEVICE_ATTR(in6_min, S_IWUSR | S_IRUGO, show_in_min,
978 static SENSOR_DEVICE_ATTR(in7_min, S_IWUSR | S_IRUGO, show_in_min,
980 static SENSOR_DEVICE_ATTR(in8_min, S_IWUSR | S_IRUGO, show_in_min,
982 static SENSOR_DEVICE_ATTR(in9_min, S_IWUSR | S_IRUGO, show_in_min,
984 static SENSOR_DEVICE_ATTR(in10_min, S_IWUSR | S_IRUGO, show_in_min,
986 static SENSOR_DEVICE_ATTR(in11_min, S_IWUSR | S_IRUGO, show_in_min,
988 static SENSOR_DEVICE_ATTR(in12_min, S_IWUSR | S_IRUGO, show_in_min,
990 static SENSOR_DEVICE_ATTR(in13_min, S_IWUSR | S_IRUGO, show_in_min,
992 static SENSOR_DEVICE_ATTR(in14_min, S_IWUSR | S_IRUGO, show_in_min,
995 static SENSOR_DEVICE_ATTR(in0_max, S_IWUSR | S_IRUGO, show_in_max,
997 static SENSOR_DEVICE_ATTR(in1_max, S_IWUSR | S_IRUGO, show_in_max,
999 static SENSOR_DEVICE_ATTR(in2_max, S_IWUSR | S_IRUGO, show_in_max,
1001 static SENSOR_DEVICE_ATTR(in3_max, S_IWUSR | S_IRUGO, show_in_max,
1003 static SENSOR_DEVICE_ATTR(in4_max, S_IWUSR | S_IRUGO, show_in_max,
1005 static SENSOR_DEVICE_ATTR(in5_max, S_IWUSR | S_IRUGO, show_in_max,
1007 static SENSOR_DEVICE_ATTR(in6_max, S_IWUSR | S_IRUGO, show_in_max,
1009 static SENSOR_DEVICE_ATTR(in7_max, S_IWUSR | S_IRUGO, show_in_max,
1011 static SENSOR_DEVICE_ATTR(in8_max, S_IWUSR | S_IRUGO, show_in_max,
1013 static SENSOR_DEVICE_ATTR(in9_max, S_IWUSR | S_IRUGO, show_in_max,
1015 static SENSOR_DEVICE_ATTR(in10_max, S_IWUSR | S_IRUGO, show_in_max,
1017 static SENSOR_DEVICE_ATTR(in11_max, S_IWUSR | S_IRUGO, show_in_max,
1019 static SENSOR_DEVICE_ATTR(in12_max, S_IWUSR | S_IRUGO, show_in_max,
1021 static SENSOR_DEVICE_ATTR(in13_max, S_IWUSR | S_IRUGO, show_in_max,
1023 static SENSOR_DEVICE_ATTR(in14_max, S_IWUSR | S_IRUGO, show_in_max,
1026 static struct attribute *nct6775_attributes_in[15][5] = {
1028 &sensor_dev_attr_in0_input.dev_attr.attr,
1029 &sensor_dev_attr_in0_min.dev_attr.attr,
1030 &sensor_dev_attr_in0_max.dev_attr.attr,
1031 &sensor_dev_attr_in0_alarm.dev_attr.attr,
1035 &sensor_dev_attr_in1_input.dev_attr.attr,
1036 &sensor_dev_attr_in1_min.dev_attr.attr,
1037 &sensor_dev_attr_in1_max.dev_attr.attr,
1038 &sensor_dev_attr_in1_alarm.dev_attr.attr,
1042 &sensor_dev_attr_in2_input.dev_attr.attr,
1043 &sensor_dev_attr_in2_min.dev_attr.attr,
1044 &sensor_dev_attr_in2_max.dev_attr.attr,
1045 &sensor_dev_attr_in2_alarm.dev_attr.attr,
1049 &sensor_dev_attr_in3_input.dev_attr.attr,
1050 &sensor_dev_attr_in3_min.dev_attr.attr,
1051 &sensor_dev_attr_in3_max.dev_attr.attr,
1052 &sensor_dev_attr_in3_alarm.dev_attr.attr,
1056 &sensor_dev_attr_in4_input.dev_attr.attr,
1057 &sensor_dev_attr_in4_min.dev_attr.attr,
1058 &sensor_dev_attr_in4_max.dev_attr.attr,
1059 &sensor_dev_attr_in4_alarm.dev_attr.attr,
1063 &sensor_dev_attr_in5_input.dev_attr.attr,
1064 &sensor_dev_attr_in5_min.dev_attr.attr,
1065 &sensor_dev_attr_in5_max.dev_attr.attr,
1066 &sensor_dev_attr_in5_alarm.dev_attr.attr,
1070 &sensor_dev_attr_in6_input.dev_attr.attr,
1071 &sensor_dev_attr_in6_min.dev_attr.attr,
1072 &sensor_dev_attr_in6_max.dev_attr.attr,
1073 &sensor_dev_attr_in6_alarm.dev_attr.attr,
1077 &sensor_dev_attr_in7_input.dev_attr.attr,
1078 &sensor_dev_attr_in7_min.dev_attr.attr,
1079 &sensor_dev_attr_in7_max.dev_attr.attr,
1080 &sensor_dev_attr_in7_alarm.dev_attr.attr,
1084 &sensor_dev_attr_in8_input.dev_attr.attr,
1085 &sensor_dev_attr_in8_min.dev_attr.attr,
1086 &sensor_dev_attr_in8_max.dev_attr.attr,
1087 &sensor_dev_attr_in8_alarm.dev_attr.attr,
1091 &sensor_dev_attr_in9_input.dev_attr.attr,
1092 &sensor_dev_attr_in9_min.dev_attr.attr,
1093 &sensor_dev_attr_in9_max.dev_attr.attr,
1094 &sensor_dev_attr_in9_alarm.dev_attr.attr,
1098 &sensor_dev_attr_in10_input.dev_attr.attr,
1099 &sensor_dev_attr_in10_min.dev_attr.attr,
1100 &sensor_dev_attr_in10_max.dev_attr.attr,
1101 &sensor_dev_attr_in10_alarm.dev_attr.attr,
1105 &sensor_dev_attr_in11_input.dev_attr.attr,
1106 &sensor_dev_attr_in11_min.dev_attr.attr,
1107 &sensor_dev_attr_in11_max.dev_attr.attr,
1108 &sensor_dev_attr_in11_alarm.dev_attr.attr,
1112 &sensor_dev_attr_in12_input.dev_attr.attr,
1113 &sensor_dev_attr_in12_min.dev_attr.attr,
1114 &sensor_dev_attr_in12_max.dev_attr.attr,
1115 &sensor_dev_attr_in12_alarm.dev_attr.attr,
1119 &sensor_dev_attr_in13_input.dev_attr.attr,
1120 &sensor_dev_attr_in13_min.dev_attr.attr,
1121 &sensor_dev_attr_in13_max.dev_attr.attr,
1122 &sensor_dev_attr_in13_alarm.dev_attr.attr,
1126 &sensor_dev_attr_in14_input.dev_attr.attr,
1127 &sensor_dev_attr_in14_min.dev_attr.attr,
1128 &sensor_dev_attr_in14_max.dev_attr.attr,
1129 &sensor_dev_attr_in14_alarm.dev_attr.attr,
1134 static const struct attribute_group nct6775_group_in[15] = {
1135 { .attrs = nct6775_attributes_in[0] },
1136 { .attrs = nct6775_attributes_in[1] },
1137 { .attrs = nct6775_attributes_in[2] },
1138 { .attrs = nct6775_attributes_in[3] },
1139 { .attrs = nct6775_attributes_in[4] },
1140 { .attrs = nct6775_attributes_in[5] },
1141 { .attrs = nct6775_attributes_in[6] },
1142 { .attrs = nct6775_attributes_in[7] },
1143 { .attrs = nct6775_attributes_in[8] },
1144 { .attrs = nct6775_attributes_in[9] },
1145 { .attrs = nct6775_attributes_in[10] },
1146 { .attrs = nct6775_attributes_in[11] },
1147 { .attrs = nct6775_attributes_in[12] },
1148 { .attrs = nct6775_attributes_in[13] },
1149 { .attrs = nct6775_attributes_in[14] },
1153 show_fan(struct device *dev, struct device_attribute *attr, char *buf)
1155 struct nct6775_data *data = nct6775_update_device(dev);
1156 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
1157 int nr = sensor_attr->index;
1158 return sprintf(buf, "%d\n", data->rpm[nr]);
1162 show_fan_min(struct device *dev, struct device_attribute *attr, char *buf)
1164 struct nct6775_data *data = nct6775_update_device(dev);
1165 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
1166 int nr = sensor_attr->index;
1167 return sprintf(buf, "%d\n",
1168 data->fan_from_reg_min(data->fan_min[nr],
1169 data->fan_div[nr]));
1173 show_fan_div(struct device *dev, struct device_attribute *attr,
1176 struct nct6775_data *data = nct6775_update_device(dev);
1177 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
1178 int nr = sensor_attr->index;
1179 return sprintf(buf, "%u\n", div_from_reg(data->fan_div[nr]));
1183 store_fan_min(struct device *dev, struct device_attribute *attr,
1184 const char *buf, size_t count)
1186 struct nct6775_data *data = dev_get_drvdata(dev);
1187 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
1188 int nr = sensor_attr->index;
1194 err = kstrtoul(buf, 10, &val);
1198 mutex_lock(&data->update_lock);
1199 if (!data->has_fan_div) {
1201 * Only NCT6776F for now, so we know that this is a 13 bit
1209 val = 1350000U / val;
1210 val = (val & 0x1f) | ((val << 3) & 0xff00);
1212 data->fan_min[nr] = val;
1213 goto write_min; /* Leave fan divider alone */
1216 /* No min limit, alarm disabled */
1217 data->fan_min[nr] = 255;
1218 new_div = data->fan_div[nr]; /* No change */
1219 dev_info(dev, "fan%u low limit and alarm disabled\n", nr + 1);
1222 reg = 1350000U / val;
1223 if (reg >= 128 * 255) {
1225 * Speed below this value cannot possibly be represented,
1226 * even with the highest divider (128)
1228 data->fan_min[nr] = 254;
1229 new_div = 7; /* 128 == (1 << 7) */
1230 dev_warn(dev, "fan%u low limit %lu below minimum %u, set to "
1231 "minimum\n", nr + 1, val,
1232 data->fan_from_reg_min(254, 7));
1235 * Speed above this value cannot possibly be represented,
1236 * even with the lowest divider (1)
1238 data->fan_min[nr] = 1;
1239 new_div = 0; /* 1 == (1 << 0) */
1240 dev_warn(dev, "fan%u low limit %lu above maximum %u, set to "
1241 "maximum\n", nr + 1, val,
1242 data->fan_from_reg_min(1, 0));
1245 * Automatically pick the best divider, i.e. the one such
1246 * that the min limit will correspond to a register value
1247 * in the 96..192 range
1250 while (reg > 192 && new_div < 7) {
1254 data->fan_min[nr] = reg;
1259 * Write both the fan clock divider (if it changed) and the new
1260 * fan min (unconditionally)
1262 if (new_div != data->fan_div[nr]) {
1263 dev_dbg(dev, "fan%u clock divider changed from %u to %u\n",
1264 nr + 1, div_from_reg(data->fan_div[nr]),
1265 div_from_reg(new_div));
1266 data->fan_div[nr] = new_div;
1267 nct6775_write_fan_div_common(dev, data, nr);
1268 /* Give the chip time to sample a new speed value */
1269 data->last_updated = jiffies;
1272 nct6775_write_value(data, data->REG_FAN_MIN[nr],
1274 mutex_unlock(&data->update_lock);
1279 static struct sensor_device_attribute sda_fan_input[] = {
1280 SENSOR_ATTR(fan1_input, S_IRUGO, show_fan, NULL, 0),
1281 SENSOR_ATTR(fan2_input, S_IRUGO, show_fan, NULL, 1),
1282 SENSOR_ATTR(fan3_input, S_IRUGO, show_fan, NULL, 2),
1283 SENSOR_ATTR(fan4_input, S_IRUGO, show_fan, NULL, 3),
1284 SENSOR_ATTR(fan5_input, S_IRUGO, show_fan, NULL, 4),
1287 static struct sensor_device_attribute sda_fan_alarm[] = {
1288 SENSOR_ATTR(fan1_alarm, S_IRUGO, show_alarm, NULL, 6),
1289 SENSOR_ATTR(fan2_alarm, S_IRUGO, show_alarm, NULL, 7),
1290 SENSOR_ATTR(fan3_alarm, S_IRUGO, show_alarm, NULL, 11),
1291 SENSOR_ATTR(fan4_alarm, S_IRUGO, show_alarm, NULL, 10),
1292 SENSOR_ATTR(fan5_alarm, S_IRUGO, show_alarm, NULL, 23),
1295 static struct sensor_device_attribute sda_fan_min[] = {
1296 SENSOR_ATTR(fan1_min, S_IWUSR | S_IRUGO, show_fan_min,
1298 SENSOR_ATTR(fan2_min, S_IWUSR | S_IRUGO, show_fan_min,
1300 SENSOR_ATTR(fan3_min, S_IWUSR | S_IRUGO, show_fan_min,
1302 SENSOR_ATTR(fan4_min, S_IWUSR | S_IRUGO, show_fan_min,
1304 SENSOR_ATTR(fan5_min, S_IWUSR | S_IRUGO, show_fan_min,
1308 static struct sensor_device_attribute sda_fan_div[] = {
1309 SENSOR_ATTR(fan1_div, S_IRUGO, show_fan_div, NULL, 0),
1310 SENSOR_ATTR(fan2_div, S_IRUGO, show_fan_div, NULL, 1),
1311 SENSOR_ATTR(fan3_div, S_IRUGO, show_fan_div, NULL, 2),
1312 SENSOR_ATTR(fan4_div, S_IRUGO, show_fan_div, NULL, 3),
1313 SENSOR_ATTR(fan5_div, S_IRUGO, show_fan_div, NULL, 4),
1317 show_temp_label(struct device *dev, struct device_attribute *attr, char *buf)
1319 struct nct6775_data *data = nct6775_update_device(dev);
1320 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
1321 int nr = sensor_attr->index;
1322 return sprintf(buf, "%s\n", data->temp_label[data->temp_src[nr]]);
1325 #define show_temp_reg(addr, reg) \
1327 show_##reg(struct device *dev, struct device_attribute *attr, \
1330 struct nct6775_data *data = nct6775_update_device(dev); \
1331 struct sensor_device_attribute *sensor_attr = \
1332 to_sensor_dev_attr(attr); \
1333 int nr = sensor_attr->index; \
1334 return sprintf(buf, "%d\n", LM75_TEMP_FROM_REG(data->reg[nr])); \
1336 show_temp_reg(reg_temp, temp);
1337 show_temp_reg(reg_temp_over, temp_max);
1338 show_temp_reg(reg_temp_hyst, temp_max_hyst);
1340 #define store_temp_reg(addr, reg) \
1342 store_##reg(struct device *dev, struct device_attribute *attr, \
1343 const char *buf, size_t count) \
1345 struct nct6775_data *data = dev_get_drvdata(dev); \
1346 struct sensor_device_attribute *sensor_attr = \
1347 to_sensor_dev_attr(attr); \
1348 int nr = sensor_attr->index; \
1351 err = kstrtol(buf, 10, &val); \
1354 mutex_lock(&data->update_lock); \
1355 data->reg[nr] = LM75_TEMP_TO_REG(val); \
1356 nct6775_write_temp(data, data->addr[nr], data->reg[nr]); \
1357 mutex_unlock(&data->update_lock); \
1360 store_temp_reg(reg_temp_over, temp_max);
1361 store_temp_reg(reg_temp_hyst, temp_max_hyst);
1364 show_temp_type(struct device *dev, struct device_attribute *attr, char *buf)
1366 struct nct6775_data *data = nct6775_update_device(dev);
1367 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
1368 int nr = sensor_attr->index;
1369 return sprintf(buf, "%d\n", (int)data->temp_type[nr]);
1372 static struct sensor_device_attribute sda_temp_input[] = {
1373 SENSOR_ATTR(temp1_input, S_IRUGO, show_temp, NULL, 0),
1374 SENSOR_ATTR(temp2_input, S_IRUGO, show_temp, NULL, 1),
1375 SENSOR_ATTR(temp3_input, S_IRUGO, show_temp, NULL, 2),
1376 SENSOR_ATTR(temp4_input, S_IRUGO, show_temp, NULL, 3),
1377 SENSOR_ATTR(temp5_input, S_IRUGO, show_temp, NULL, 4),
1378 SENSOR_ATTR(temp6_input, S_IRUGO, show_temp, NULL, 5),
1379 SENSOR_ATTR(temp7_input, S_IRUGO, show_temp, NULL, 6),
1380 SENSOR_ATTR(temp8_input, S_IRUGO, show_temp, NULL, 7),
1381 SENSOR_ATTR(temp9_input, S_IRUGO, show_temp, NULL, 8),
1384 static struct sensor_device_attribute sda_temp_label[] = {
1385 SENSOR_ATTR(temp1_label, S_IRUGO, show_temp_label, NULL, 0),
1386 SENSOR_ATTR(temp2_label, S_IRUGO, show_temp_label, NULL, 1),
1387 SENSOR_ATTR(temp3_label, S_IRUGO, show_temp_label, NULL, 2),
1388 SENSOR_ATTR(temp4_label, S_IRUGO, show_temp_label, NULL, 3),
1389 SENSOR_ATTR(temp5_label, S_IRUGO, show_temp_label, NULL, 4),
1390 SENSOR_ATTR(temp6_label, S_IRUGO, show_temp_label, NULL, 5),
1391 SENSOR_ATTR(temp7_label, S_IRUGO, show_temp_label, NULL, 6),
1392 SENSOR_ATTR(temp8_label, S_IRUGO, show_temp_label, NULL, 7),
1393 SENSOR_ATTR(temp9_label, S_IRUGO, show_temp_label, NULL, 8),
1396 static struct sensor_device_attribute sda_temp_max[] = {
1397 SENSOR_ATTR(temp1_max, S_IRUGO | S_IWUSR, show_temp_max,
1399 SENSOR_ATTR(temp2_max, S_IRUGO | S_IWUSR, show_temp_max,
1401 SENSOR_ATTR(temp3_max, S_IRUGO | S_IWUSR, show_temp_max,
1403 SENSOR_ATTR(temp4_max, S_IRUGO | S_IWUSR, show_temp_max,
1405 SENSOR_ATTR(temp5_max, S_IRUGO | S_IWUSR, show_temp_max,
1407 SENSOR_ATTR(temp6_max, S_IRUGO | S_IWUSR, show_temp_max,
1409 SENSOR_ATTR(temp7_max, S_IRUGO | S_IWUSR, show_temp_max,
1411 SENSOR_ATTR(temp8_max, S_IRUGO | S_IWUSR, show_temp_max,
1413 SENSOR_ATTR(temp9_max, S_IRUGO | S_IWUSR, show_temp_max,
1417 static struct sensor_device_attribute sda_temp_max_hyst[] = {
1418 SENSOR_ATTR(temp1_max_hyst, S_IRUGO | S_IWUSR, show_temp_max_hyst,
1419 store_temp_max_hyst, 0),
1420 SENSOR_ATTR(temp2_max_hyst, S_IRUGO | S_IWUSR, show_temp_max_hyst,
1421 store_temp_max_hyst, 1),
1422 SENSOR_ATTR(temp3_max_hyst, S_IRUGO | S_IWUSR, show_temp_max_hyst,
1423 store_temp_max_hyst, 2),
1424 SENSOR_ATTR(temp4_max_hyst, S_IRUGO | S_IWUSR, show_temp_max_hyst,
1425 store_temp_max_hyst, 3),
1426 SENSOR_ATTR(temp5_max_hyst, S_IRUGO | S_IWUSR, show_temp_max_hyst,
1427 store_temp_max_hyst, 4),
1428 SENSOR_ATTR(temp6_max_hyst, S_IRUGO | S_IWUSR, show_temp_max_hyst,
1429 store_temp_max_hyst, 5),
1430 SENSOR_ATTR(temp7_max_hyst, S_IRUGO | S_IWUSR, show_temp_max_hyst,
1431 store_temp_max_hyst, 6),
1432 SENSOR_ATTR(temp8_max_hyst, S_IRUGO | S_IWUSR, show_temp_max_hyst,
1433 store_temp_max_hyst, 7),
1434 SENSOR_ATTR(temp9_max_hyst, S_IRUGO | S_IWUSR, show_temp_max_hyst,
1435 store_temp_max_hyst, 8),
1438 static struct sensor_device_attribute sda_temp_alarm[] = {
1439 SENSOR_ATTR(temp1_alarm, S_IRUGO, show_alarm, NULL, 4),
1440 SENSOR_ATTR(temp2_alarm, S_IRUGO, show_alarm, NULL, 5),
1441 SENSOR_ATTR(temp3_alarm, S_IRUGO, show_alarm, NULL, 13),
1444 static struct sensor_device_attribute sda_temp_type[] = {
1445 SENSOR_ATTR(temp1_type, S_IRUGO, show_temp_type, NULL, 0),
1446 SENSOR_ATTR(temp2_type, S_IRUGO, show_temp_type, NULL, 1),
1447 SENSOR_ATTR(temp3_type, S_IRUGO, show_temp_type, NULL, 2),
1450 #define show_pwm_reg(reg) \
1451 static ssize_t show_##reg(struct device *dev, struct device_attribute *attr, \
1454 struct nct6775_data *data = nct6775_update_device(dev); \
1455 struct sensor_device_attribute *sensor_attr = \
1456 to_sensor_dev_attr(attr); \
1457 int nr = sensor_attr->index; \
1458 return sprintf(buf, "%d\n", data->reg[nr]); \
1461 show_pwm_reg(pwm_mode)
1462 show_pwm_reg(pwm_enable)
1464 static ssize_t show_pwm(struct device *dev, struct device_attribute *attr,
1467 struct nct6775_data *data = nct6775_update_device(dev);
1468 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
1469 int nr = sensor_attr->index;
1472 * For automatic fan control modes, show current pwm readings.
1473 * Otherwise, show the configured value.
1475 return sprintf(buf, "%d\n",
1476 data->pwm_mode[nr] > 1 ?
1478 nct6775_read_value(data,
1479 NCT6775_REG_PWM_READ[nr]));
1483 store_pwm_mode(struct device *dev, struct device_attribute *attr,
1484 const char *buf, size_t count)
1486 struct nct6775_data *data = dev_get_drvdata(dev);
1487 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
1488 struct nct6775_sio_data *sio_data = dev->platform_data;
1489 int nr = sensor_attr->index;
1494 err = kstrtoul(buf, 10, &val);
1501 /* On NCT6776F and NCT6779D, DC mode is only supported for pwm1 */
1502 if ((sio_data->kind == nct6776 || sio_data->kind == nct6779) && nr) {
1508 mutex_lock(&data->update_lock);
1509 reg = nct6775_read_value(data, NCT6775_REG_PWM_ENABLE[nr]);
1510 data->pwm_mode[nr] = val;
1511 reg &= ~(1 << NCT6775_PWM_MODE_SHIFT[nr]);
1513 reg |= 1 << NCT6775_PWM_MODE_SHIFT[nr];
1514 nct6775_write_value(data, NCT6775_REG_PWM_ENABLE[nr], reg);
1515 mutex_unlock(&data->update_lock);
1520 store_pwm(struct device *dev, struct device_attribute *attr,
1521 const char *buf, size_t count)
1523 struct nct6775_data *data = dev_get_drvdata(dev);
1524 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
1525 int nr = sensor_attr->index;
1529 err = kstrtoul(buf, 10, &val);
1533 val = SENSORS_LIMIT(val, 0, 255);
1535 mutex_lock(&data->update_lock);
1536 data->pwm[nr] = val;
1537 nct6775_write_value(data, NCT6775_REG_PWM[nr], val);
1538 mutex_unlock(&data->update_lock);
1542 /* Returns 0 if OK, -EINVAL otherwise */
1543 static int check_trip_points(struct nct6775_data *data, int nr)
1547 for (i = 0; i < data->auto_pwm_num - 1; i++) {
1548 if (data->auto_temp[nr][i] > data->auto_temp[nr][i + 1])
1551 for (i = 0; i < data->auto_pwm_num - 1; i++) {
1552 if (data->auto_pwm[nr][i] > data->auto_pwm[nr][i + 1])
1555 /* validate critical temperature and pwm if enabled (pwm > 0) */
1556 if (data->auto_pwm[nr][data->auto_pwm_num]) {
1557 if (data->auto_temp[nr][data->auto_pwm_num - 1] >
1558 data->auto_temp[nr][data->auto_pwm_num] ||
1559 data->auto_pwm[nr][data->auto_pwm_num - 1] >
1560 data->auto_pwm[nr][data->auto_pwm_num])
1567 store_pwm_enable(struct device *dev, struct device_attribute *attr,
1568 const char *buf, size_t count)
1570 struct nct6775_data *data = dev_get_drvdata(dev);
1571 struct nct6775_sio_data *sio_data = dev->platform_data;
1572 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
1573 int nr = sensor_attr->index;
1578 err = kstrtoul(buf, 10, &val);
1585 /* SmartFan III mode is only supported on NCT6775F */
1586 if (sio_data->kind != nct6775 && val == 4)
1589 if (val == 5 && check_trip_points(data, nr)) {
1590 dev_err(dev, "Inconsistent trip points, not switching to SmartFan IV mode\n");
1591 dev_err(dev, "Adjust trip points and try again\n");
1595 mutex_lock(&data->update_lock);
1596 data->pwm_enable[nr] = val;
1599 * turn off pwm control: select manual mode, set pwm to maximum
1602 data->pwm[nr] = 255;
1603 nct6775_write_value(data, NCT6775_REG_PWM[nr], 255);
1605 reg = nct6775_read_value(data, NCT6775_REG_FAN_MODE[nr]);
1607 reg |= (val - 1) << 4;
1608 nct6775_write_value(data, NCT6775_REG_FAN_MODE[nr], reg);
1609 mutex_unlock(&data->update_lock);
1614 static ssize_t show_target_temp(struct device *dev,
1615 struct device_attribute *attr, char *buf)
1617 struct nct6775_data *data = nct6775_update_device(dev);
1618 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
1620 return sprintf(buf, "%d\n",
1621 data->target_temp[sensor_attr->index] * 1000);
1625 store_target_temp(struct device *dev, struct device_attribute *attr,
1626 const char *buf, size_t count)
1628 struct nct6775_data *data = dev_get_drvdata(dev);
1629 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
1630 int nr = sensor_attr->index;
1634 err = kstrtol(buf, 10, &val);
1638 val = SENSORS_LIMIT(DIV_ROUND_CLOSEST(val, 1000), 0, 127);
1640 mutex_lock(&data->update_lock);
1641 data->target_temp[nr] = val;
1642 nct6775_write_value(data, data->REG_TARGET[nr], val);
1643 mutex_unlock(&data->update_lock);
1647 static ssize_t show_auto_temp_hyst(struct device *dev,
1648 struct device_attribute *attr, char *buf)
1650 struct nct6775_data *data = nct6775_update_device(dev);
1651 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
1653 int point = sattr->index >= data->auto_pwm_num ? 1 : 0;
1654 int tolerance = data->tolerance[nr][point];
1655 int temp = data->auto_temp[nr][sattr->index];
1657 return sprintf(buf, "%d\n", (temp - tolerance) * 1000);
1661 store_auto_temp_hyst(struct device *dev, struct device_attribute *attr,
1662 const char *buf, size_t count)
1664 struct nct6775_data *data = dev_get_drvdata(dev);
1665 struct nct6775_sio_data *sio_data = dev->platform_data;
1666 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
1668 int point = sattr->index >= data->auto_pwm_num ? 1 : 0;
1672 int maxlimit[2][3] = { { 15, 7, 63 }, { 15, 7, 7 } };
1673 int mask[] = { 0x0f, 0x07, 0x07 };
1676 err = kstrtol(buf, 10, &val);
1680 temp = data->auto_temp[nr][sattr->index];
1681 val = temp - DIV_ROUND_CLOSEST(val, 1000);
1683 /* Limit tolerance as needed */
1684 val = SENSORS_LIMIT(val, 0, maxlimit[point][sio_data->kind]);
1686 mutex_lock(&data->update_lock);
1688 nct6775_write_value(data,
1689 NCT6775_REG_CRITICAL_TEMP_TOLERANCE[nr],
1692 reg = nct6775_read_value(data, NCT6775_REG_FAN_MODE[nr]);
1693 reg = (reg & ~mask[nr]) | (val & mask[nr]);
1694 nct6775_write_value(data, NCT6775_REG_FAN_MODE[nr], reg);
1695 if (sio_data->kind == nct6779) {
1696 reg = nct6775_read_value(data,
1697 NCT6779_REG_TOLERANCE_H[nr]);
1698 reg = (reg & 0x70) | ((val & 0x38) << 1);
1699 nct6775_write_value(data,
1700 NCT6779_REG_TOLERANCE_H[nr], reg);
1704 data->tolerance[nr][point] = val;
1705 mutex_unlock(&data->update_lock);
1709 static SENSOR_DEVICE_ATTR(pwm1, S_IWUSR | S_IRUGO, show_pwm, store_pwm, 0);
1710 static SENSOR_DEVICE_ATTR(pwm2, S_IWUSR | S_IRUGO, show_pwm, store_pwm, 1);
1711 static SENSOR_DEVICE_ATTR(pwm3, S_IWUSR | S_IRUGO, show_pwm, store_pwm, 2);
1712 static SENSOR_DEVICE_ATTR(pwm4, S_IWUSR | S_IRUGO, show_pwm, store_pwm, 3);
1713 static SENSOR_DEVICE_ATTR(pwm5, S_IWUSR | S_IRUGO, show_pwm, store_pwm, 4);
1715 static SENSOR_DEVICE_ATTR(pwm1_mode, S_IWUSR | S_IRUGO, show_pwm_mode,
1717 static SENSOR_DEVICE_ATTR(pwm2_mode, S_IWUSR | S_IRUGO, show_pwm_mode,
1719 static SENSOR_DEVICE_ATTR(pwm3_mode, S_IWUSR | S_IRUGO, show_pwm_mode,
1721 static SENSOR_DEVICE_ATTR(pwm4_mode, S_IWUSR | S_IRUGO, show_pwm_mode,
1723 static SENSOR_DEVICE_ATTR(pwm5_mode, S_IWUSR | S_IRUGO, show_pwm_mode,
1726 static SENSOR_DEVICE_ATTR(pwm1_enable, S_IWUSR | S_IRUGO, show_pwm_enable,
1727 store_pwm_enable, 0);
1728 static SENSOR_DEVICE_ATTR(pwm2_enable, S_IWUSR | S_IRUGO, show_pwm_enable,
1729 store_pwm_enable, 1);
1730 static SENSOR_DEVICE_ATTR(pwm3_enable, S_IWUSR | S_IRUGO, show_pwm_enable,
1731 store_pwm_enable, 2);
1732 static SENSOR_DEVICE_ATTR(pwm4_enable, S_IWUSR | S_IRUGO, show_pwm_enable,
1733 store_pwm_enable, 3);
1734 static SENSOR_DEVICE_ATTR(pwm5_enable, S_IWUSR | S_IRUGO, show_pwm_enable,
1735 store_pwm_enable, 4);
1737 static SENSOR_DEVICE_ATTR(pwm1_target, S_IWUSR | S_IRUGO, show_target_temp,
1738 store_target_temp, 0);
1739 static SENSOR_DEVICE_ATTR(pwm2_target, S_IWUSR | S_IRUGO, show_target_temp,
1740 store_target_temp, 1);
1741 static SENSOR_DEVICE_ATTR(pwm3_target, S_IWUSR | S_IRUGO, show_target_temp,
1742 store_target_temp, 2);
1743 static SENSOR_DEVICE_ATTR(pwm4_target, S_IWUSR | S_IRUGO, show_target_temp,
1744 store_target_temp, 3);
1745 static SENSOR_DEVICE_ATTR(pwm5_target, S_IWUSR | S_IRUGO, show_target_temp,
1746 store_target_temp, 4);
1748 /* Smart Fan registers */
1750 #define fan_functions(reg, REG) \
1751 static ssize_t show_##reg(struct device *dev, struct device_attribute *attr, \
1754 struct nct6775_data *data = nct6775_update_device(dev); \
1755 struct sensor_device_attribute *sensor_attr = \
1756 to_sensor_dev_attr(attr); \
1757 int nr = sensor_attr->index; \
1758 return sprintf(buf, "%d\n", data->reg[nr]); \
1761 store_##reg(struct device *dev, struct device_attribute *attr, \
1762 const char *buf, size_t count) \
1764 struct nct6775_data *data = dev_get_drvdata(dev); \
1765 struct sensor_device_attribute *sensor_attr = \
1766 to_sensor_dev_attr(attr); \
1767 int nr = sensor_attr->index; \
1768 unsigned long val; \
1770 err = kstrtoul(buf, 10, &val); \
1773 val = SENSORS_LIMIT(val, 1, 255); \
1774 mutex_lock(&data->update_lock); \
1775 data->reg[nr] = val; \
1776 nct6775_write_value(data, data->REG_##REG[nr], val); \
1777 mutex_unlock(&data->update_lock); \
1781 fan_functions(fan_start_output, FAN_START_OUTPUT)
1782 fan_functions(fan_stop_output, FAN_STOP_OUTPUT)
1783 fan_functions(fan_max_output, FAN_MAX_OUTPUT)
1784 fan_functions(fan_step_output, FAN_STEP_OUTPUT)
1786 #define fan_step_functions(reg, REG) \
1787 static ssize_t show_##reg(struct device *dev, \
1788 struct device_attribute *attr, char *buf) \
1790 struct nct6775_data *data = nct6775_update_device(dev); \
1791 struct sensor_device_attribute *sensor_attr = \
1792 to_sensor_dev_attr(attr); \
1793 int nr = sensor_attr->index; \
1794 return sprintf(buf, "%d\n", \
1795 step_time_from_reg(data->reg[nr], \
1796 data->pwm_mode[nr])); \
1798 static ssize_t store_##reg(struct device *dev, \
1799 struct device_attribute *attr, \
1800 const char *buf, size_t count) \
1802 struct nct6775_data *data = dev_get_drvdata(dev); \
1803 struct sensor_device_attribute *sensor_attr = \
1804 to_sensor_dev_attr(attr); \
1805 int nr = sensor_attr->index; \
1806 unsigned long val; \
1808 err = kstrtoul(buf, 10, &val); \
1811 val = step_time_to_reg(val, data->pwm_mode[nr]); \
1812 mutex_lock(&data->update_lock); \
1813 data->reg[nr] = val; \
1814 nct6775_write_value(data, NCT6775_REG_##REG[nr], val); \
1815 mutex_unlock(&data->update_lock); \
1819 fan_step_functions(fan_stop_time, FAN_STOP_TIME)
1820 fan_step_functions(fan_step_up_time, FAN_STEP_UP_TIME)
1821 fan_step_functions(fan_step_down_time, FAN_STEP_DOWN_TIME)
1823 static ssize_t show_name(struct device *dev, struct device_attribute *attr,
1826 struct nct6775_data *data = dev_get_drvdata(dev);
1828 return sprintf(buf, "%s\n", data->name);
1830 static DEVICE_ATTR(name, S_IRUGO, show_name, NULL);
1832 static SENSOR_DEVICE_ATTR(pwm1_stop_time, S_IWUSR | S_IRUGO, show_fan_stop_time,
1833 store_fan_stop_time, 0);
1834 static SENSOR_DEVICE_ATTR(pwm2_stop_time, S_IWUSR | S_IRUGO, show_fan_stop_time,
1835 store_fan_stop_time, 1);
1836 static SENSOR_DEVICE_ATTR(pwm3_stop_time, S_IWUSR | S_IRUGO, show_fan_stop_time,
1837 store_fan_stop_time, 2);
1838 static SENSOR_DEVICE_ATTR(pwm4_stop_time, S_IWUSR | S_IRUGO, show_fan_stop_time,
1839 store_fan_stop_time, 3);
1840 static SENSOR_DEVICE_ATTR(pwm5_stop_time, S_IWUSR | S_IRUGO, show_fan_stop_time,
1841 store_fan_stop_time, 4);
1843 static SENSOR_DEVICE_ATTR(pwm1_step_up_time, S_IWUSR | S_IRUGO,
1844 show_fan_step_up_time, store_fan_step_up_time, 0);
1845 static SENSOR_DEVICE_ATTR(pwm2_step_up_time, S_IWUSR | S_IRUGO,
1846 show_fan_step_up_time, store_fan_step_up_time, 1);
1847 static SENSOR_DEVICE_ATTR(pwm3_step_up_time, S_IWUSR | S_IRUGO,
1848 show_fan_step_up_time, store_fan_step_up_time, 2);
1849 static SENSOR_DEVICE_ATTR(pwm4_step_up_time, S_IWUSR | S_IRUGO,
1850 show_fan_step_up_time, store_fan_step_up_time, 3);
1851 static SENSOR_DEVICE_ATTR(pwm5_step_up_time, S_IWUSR | S_IRUGO,
1852 show_fan_step_up_time, store_fan_step_up_time, 4);
1854 static SENSOR_DEVICE_ATTR(pwm1_step_down_time, S_IWUSR | S_IRUGO,
1855 show_fan_step_down_time, store_fan_step_down_time, 0);
1856 static SENSOR_DEVICE_ATTR(pwm2_step_down_time, S_IWUSR | S_IRUGO,
1857 show_fan_step_down_time, store_fan_step_down_time, 1);
1858 static SENSOR_DEVICE_ATTR(pwm3_step_down_time, S_IWUSR | S_IRUGO,
1859 show_fan_step_down_time, store_fan_step_down_time, 2);
1860 static SENSOR_DEVICE_ATTR(pwm4_step_down_time, S_IWUSR | S_IRUGO,
1861 show_fan_step_down_time, store_fan_step_down_time, 3);
1862 static SENSOR_DEVICE_ATTR(pwm5_step_down_time, S_IWUSR | S_IRUGO,
1863 show_fan_step_down_time, store_fan_step_down_time, 4);
1865 static SENSOR_DEVICE_ATTR(pwm1_start_output, S_IWUSR | S_IRUGO,
1866 show_fan_start_output, store_fan_start_output, 0);
1867 static SENSOR_DEVICE_ATTR(pwm2_start_output, S_IWUSR | S_IRUGO,
1868 show_fan_start_output, store_fan_start_output, 1);
1869 static SENSOR_DEVICE_ATTR(pwm3_start_output, S_IWUSR | S_IRUGO,
1870 show_fan_start_output, store_fan_start_output, 2);
1871 static SENSOR_DEVICE_ATTR(pwm4_start_output, S_IWUSR | S_IRUGO,
1872 show_fan_start_output, store_fan_start_output, 3);
1873 static SENSOR_DEVICE_ATTR(pwm5_start_output, S_IWUSR | S_IRUGO,
1874 show_fan_start_output, store_fan_start_output, 4);
1876 static SENSOR_DEVICE_ATTR(pwm1_stop_output, S_IWUSR | S_IRUGO,
1877 show_fan_stop_output, store_fan_stop_output, 0);
1878 static SENSOR_DEVICE_ATTR(pwm2_stop_output, S_IWUSR | S_IRUGO,
1879 show_fan_stop_output, store_fan_stop_output, 1);
1880 static SENSOR_DEVICE_ATTR(pwm3_stop_output, S_IWUSR | S_IRUGO,
1881 show_fan_stop_output, store_fan_stop_output, 2);
1882 static SENSOR_DEVICE_ATTR(pwm4_stop_output, S_IWUSR | S_IRUGO,
1883 show_fan_stop_output, store_fan_stop_output, 3);
1884 static SENSOR_DEVICE_ATTR(pwm5_stop_output, S_IWUSR | S_IRUGO,
1885 show_fan_stop_output, store_fan_stop_output, 4);
1887 static struct attribute *nct6775_attributes_pwm[5][10] = {
1889 &sensor_dev_attr_pwm1.dev_attr.attr,
1890 &sensor_dev_attr_pwm1_mode.dev_attr.attr,
1891 &sensor_dev_attr_pwm1_enable.dev_attr.attr,
1892 &sensor_dev_attr_pwm1_target.dev_attr.attr,
1893 &sensor_dev_attr_pwm1_stop_time.dev_attr.attr,
1894 &sensor_dev_attr_pwm1_step_up_time.dev_attr.attr,
1895 &sensor_dev_attr_pwm1_step_down_time.dev_attr.attr,
1896 &sensor_dev_attr_pwm1_start_output.dev_attr.attr,
1897 &sensor_dev_attr_pwm1_stop_output.dev_attr.attr,
1901 &sensor_dev_attr_pwm2.dev_attr.attr,
1902 &sensor_dev_attr_pwm2_mode.dev_attr.attr,
1903 &sensor_dev_attr_pwm2_enable.dev_attr.attr,
1904 &sensor_dev_attr_pwm2_target.dev_attr.attr,
1905 &sensor_dev_attr_pwm2_stop_time.dev_attr.attr,
1906 &sensor_dev_attr_pwm2_step_up_time.dev_attr.attr,
1907 &sensor_dev_attr_pwm2_step_down_time.dev_attr.attr,
1908 &sensor_dev_attr_pwm2_start_output.dev_attr.attr,
1909 &sensor_dev_attr_pwm2_stop_output.dev_attr.attr,
1913 &sensor_dev_attr_pwm3.dev_attr.attr,
1914 &sensor_dev_attr_pwm3_mode.dev_attr.attr,
1915 &sensor_dev_attr_pwm3_enable.dev_attr.attr,
1916 &sensor_dev_attr_pwm3_target.dev_attr.attr,
1917 &sensor_dev_attr_pwm3_stop_time.dev_attr.attr,
1918 &sensor_dev_attr_pwm3_step_up_time.dev_attr.attr,
1919 &sensor_dev_attr_pwm3_step_down_time.dev_attr.attr,
1920 &sensor_dev_attr_pwm3_start_output.dev_attr.attr,
1921 &sensor_dev_attr_pwm3_stop_output.dev_attr.attr,
1925 &sensor_dev_attr_pwm4.dev_attr.attr,
1926 &sensor_dev_attr_pwm4_mode.dev_attr.attr,
1927 &sensor_dev_attr_pwm4_enable.dev_attr.attr,
1928 &sensor_dev_attr_pwm4_target.dev_attr.attr,
1929 &sensor_dev_attr_pwm4_stop_time.dev_attr.attr,
1930 &sensor_dev_attr_pwm4_step_up_time.dev_attr.attr,
1931 &sensor_dev_attr_pwm4_step_down_time.dev_attr.attr,
1932 &sensor_dev_attr_pwm4_start_output.dev_attr.attr,
1933 &sensor_dev_attr_pwm4_stop_output.dev_attr.attr,
1937 &sensor_dev_attr_pwm5.dev_attr.attr,
1938 &sensor_dev_attr_pwm5_mode.dev_attr.attr,
1939 &sensor_dev_attr_pwm5_enable.dev_attr.attr,
1940 &sensor_dev_attr_pwm5_target.dev_attr.attr,
1941 &sensor_dev_attr_pwm5_stop_time.dev_attr.attr,
1942 &sensor_dev_attr_pwm5_step_up_time.dev_attr.attr,
1943 &sensor_dev_attr_pwm5_step_down_time.dev_attr.attr,
1944 &sensor_dev_attr_pwm5_start_output.dev_attr.attr,
1945 &sensor_dev_attr_pwm5_stop_output.dev_attr.attr,
1950 static const struct attribute_group nct6775_group_pwm[5] = {
1951 { .attrs = nct6775_attributes_pwm[0] },
1952 { .attrs = nct6775_attributes_pwm[1] },
1953 { .attrs = nct6775_attributes_pwm[2] },
1954 { .attrs = nct6775_attributes_pwm[3] },
1955 { .attrs = nct6775_attributes_pwm[4] },
1959 * max and step settings are not supported on all chips.
1960 * Need to check support while generating/removing attribute files.
1962 static struct sensor_device_attribute sda_sf3_max_step_arrays[] = {
1963 SENSOR_ATTR(pwm1_max_output, S_IWUSR | S_IRUGO, show_fan_max_output,
1964 store_fan_max_output, 0),
1965 SENSOR_ATTR(pwm1_step_output, S_IWUSR | S_IRUGO, show_fan_step_output,
1966 store_fan_step_output, 0),
1967 SENSOR_ATTR(pwm2_max_output, S_IWUSR | S_IRUGO, show_fan_max_output,
1968 store_fan_max_output, 1),
1969 SENSOR_ATTR(pwm2_step_output, S_IWUSR | S_IRUGO, show_fan_step_output,
1970 store_fan_step_output, 1),
1971 SENSOR_ATTR(pwm3_max_output, S_IWUSR | S_IRUGO, show_fan_max_output,
1972 store_fan_max_output, 2),
1973 SENSOR_ATTR(pwm3_step_output, S_IWUSR | S_IRUGO, show_fan_step_output,
1974 store_fan_step_output, 2),
1975 SENSOR_ATTR(pwm4_max_output, S_IWUSR | S_IRUGO, show_fan_max_output,
1976 store_fan_max_output, 3),
1977 SENSOR_ATTR(pwm4_step_output, S_IWUSR | S_IRUGO, show_fan_step_output,
1978 store_fan_step_output, 3),
1979 SENSOR_ATTR(pwm5_max_output, S_IWUSR | S_IRUGO, show_fan_max_output,
1980 store_fan_max_output, 4),
1981 SENSOR_ATTR(pwm5_step_output, S_IWUSR | S_IRUGO, show_fan_step_output,
1982 store_fan_step_output, 4),
1985 static ssize_t show_auto_pwm(struct device *dev, struct device_attribute *attr,
1988 struct nct6775_data *data = nct6775_update_device(dev);
1989 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
1991 return sprintf(buf, "%d\n", data->auto_pwm[sattr->nr][sattr->index]);
1994 static ssize_t store_auto_pwm(struct device *dev, struct device_attribute *attr,
1995 const char *buf, size_t count)
1997 struct nct6775_sio_data *sio_data = dev->platform_data;
1998 struct nct6775_data *data = dev_get_drvdata(dev);
1999 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
2001 int point = sattr->index;
2006 err = kstrtoul(buf, 10, &val);
2012 if (point == data->auto_pwm_num) {
2013 if (sio_data->kind != nct6775 && !val)
2015 if (sio_data->kind != nct6779 && val)
2019 mutex_lock(&data->update_lock);
2020 data->auto_pwm[nr][point] = val;
2021 if (point < data->auto_pwm_num) {
2022 nct6775_write_value(data, NCT6775_REG_AUTO_PWM(nr, point),
2023 data->auto_pwm[nr][point]);
2025 switch(sio_data->kind) {
2027 /* disable if needed (pwm == 0) */
2028 reg = nct6775_read_value(data,
2029 NCT6775_REG_CRITICAL_ENAB[nr]);
2034 nct6775_write_value(data, NCT6775_REG_CRITICAL_ENAB[nr],
2038 break; /* always enabled, nothing to do */
2040 nct6775_write_value(data, NCT6779_REG_CRITICAL_PWM[nr],
2042 reg = nct6775_read_value(data,
2043 NCT6779_REG_CRITICAL_PWM_ENABLE[nr]);
2048 nct6775_write_value(data,
2049 NCT6779_REG_CRITICAL_PWM_ENABLE[nr],
2054 mutex_unlock(&data->update_lock);
2058 static ssize_t show_auto_temp(struct device *dev, struct device_attribute *attr,
2061 struct nct6775_data *data = nct6775_update_device(dev);
2062 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
2064 int point = sattr->index;
2067 * We don't know for sure if the temperature is signed or unsigned.
2068 * Assume it is unsigned.
2070 return sprintf(buf, "%d\n", data->auto_temp[nr][point] * 1000);
2073 static ssize_t store_auto_temp(struct device *dev,
2074 struct device_attribute *attr,
2075 const char *buf, size_t count)
2077 struct nct6775_data *data = dev_get_drvdata(dev);
2078 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
2080 int point = sattr->index;
2084 err = kstrtoul(buf, 10, &val);
2090 mutex_lock(&data->update_lock);
2091 data->auto_temp[nr][point] = DIV_ROUND_CLOSEST(val, 1000);
2092 if (point < data->auto_pwm_num) {
2093 nct6775_write_value(data, NCT6775_REG_AUTO_TEMP(nr, point),
2094 data->auto_temp[nr][point]);
2096 nct6775_write_value(data, NCT6775_REG_CRITICAL_TEMP[nr],
2097 data->auto_temp[nr][point]);
2099 mutex_unlock(&data->update_lock);
2104 * The number of auto-point trip points is chip dependent.
2105 * Need to check support while generating/removing attribute files.
2107 static struct sensor_device_attribute_2 sda_auto_pwm_arrays[] = {
2108 SENSOR_ATTR_2(pwm1_auto_point1_pwm, S_IWUSR | S_IRUGO,
2109 show_auto_pwm, store_auto_pwm, 0, 0),
2110 SENSOR_ATTR_2(pwm1_auto_point1_temp, S_IWUSR | S_IRUGO,
2111 show_auto_temp, store_auto_temp, 0, 0),
2112 SENSOR_ATTR_2(pwm1_auto_point1_temp_hyst, S_IWUSR | S_IRUGO,
2113 show_auto_temp_hyst, store_auto_temp_hyst, 0, 0),
2114 SENSOR_ATTR_2(pwm1_auto_point2_pwm, S_IWUSR | S_IRUGO,
2115 show_auto_pwm, store_auto_pwm, 0, 1),
2116 SENSOR_ATTR_2(pwm1_auto_point2_temp, S_IWUSR | S_IRUGO,
2117 show_auto_temp, store_auto_temp, 0, 1),
2118 SENSOR_ATTR_2(pwm1_auto_point2_temp_hyst, S_IWUSR | S_IRUGO,
2119 show_auto_temp_hyst, store_auto_temp_hyst, 0, 1),
2120 SENSOR_ATTR_2(pwm1_auto_point3_pwm, S_IWUSR | S_IRUGO,
2121 show_auto_pwm, store_auto_pwm, 0, 2),
2122 SENSOR_ATTR_2(pwm1_auto_point3_temp, S_IWUSR | S_IRUGO,
2123 show_auto_temp, store_auto_temp, 0, 2),
2124 SENSOR_ATTR_2(pwm1_auto_point3_temp_hyst, S_IWUSR | S_IRUGO,
2125 show_auto_temp_hyst, store_auto_temp_hyst, 0, 2),
2126 SENSOR_ATTR_2(pwm1_auto_point4_pwm, S_IWUSR | S_IRUGO,
2127 show_auto_pwm, store_auto_pwm, 0, 3),
2128 SENSOR_ATTR_2(pwm1_auto_point4_temp, S_IWUSR | S_IRUGO,
2129 show_auto_temp, store_auto_temp, 0, 3),
2130 SENSOR_ATTR_2(pwm1_auto_point4_temp_hyst, S_IWUSR | S_IRUGO,
2131 show_auto_temp_hyst, store_auto_temp_hyst, 0, 3),
2132 SENSOR_ATTR_2(pwm1_auto_point5_pwm, S_IWUSR | S_IRUGO,
2133 show_auto_pwm, store_auto_pwm, 0, 4),
2134 SENSOR_ATTR_2(pwm1_auto_point5_temp, S_IWUSR | S_IRUGO,
2135 show_auto_temp, store_auto_temp, 0, 4),
2136 SENSOR_ATTR_2(pwm1_auto_point5_temp_hyst, S_IWUSR | S_IRUGO,
2137 show_auto_temp_hyst, store_auto_temp_hyst, 0, 4),
2138 SENSOR_ATTR_2(pwm1_auto_point6_pwm, S_IWUSR | S_IRUGO,
2139 show_auto_pwm, store_auto_pwm, 0, 5),
2140 SENSOR_ATTR_2(pwm1_auto_point6_temp, S_IWUSR | S_IRUGO,
2141 show_auto_temp, store_auto_temp, 0, 5),
2142 SENSOR_ATTR_2(pwm1_auto_point6_temp_hyst, S_IWUSR | S_IRUGO,
2143 show_auto_temp_hyst, store_auto_temp_hyst, 0, 5),
2144 SENSOR_ATTR_2(pwm1_auto_point7_pwm, S_IWUSR | S_IRUGO,
2145 show_auto_pwm, store_auto_pwm, 0, 6),
2146 SENSOR_ATTR_2(pwm1_auto_point7_temp, S_IWUSR | S_IRUGO,
2147 show_auto_temp, store_auto_temp, 0, 6),
2148 SENSOR_ATTR_2(pwm1_auto_point7_temp_hyst, S_IWUSR | S_IRUGO,
2149 show_auto_temp_hyst, store_auto_temp_hyst, 0, 6),
2151 SENSOR_ATTR_2(pwm2_auto_point1_pwm, S_IWUSR | S_IRUGO,
2152 show_auto_pwm, store_auto_pwm, 1, 0),
2153 SENSOR_ATTR_2(pwm2_auto_point1_temp, S_IWUSR | S_IRUGO,
2154 show_auto_temp, store_auto_temp, 1, 0),
2155 SENSOR_ATTR_2(pwm2_auto_point1_temp_hyst, S_IWUSR | S_IRUGO,
2156 show_auto_temp_hyst, store_auto_temp_hyst, 1, 0),
2157 SENSOR_ATTR_2(pwm2_auto_point2_pwm, S_IWUSR | S_IRUGO,
2158 show_auto_pwm, store_auto_pwm, 1, 1),
2159 SENSOR_ATTR_2(pwm2_auto_point2_temp, S_IWUSR | S_IRUGO,
2160 show_auto_temp, store_auto_temp, 1, 1),
2161 SENSOR_ATTR_2(pwm2_auto_point2_temp_hyst, S_IWUSR | S_IRUGO,
2162 show_auto_temp_hyst, store_auto_temp_hyst, 1, 1),
2163 SENSOR_ATTR_2(pwm2_auto_point3_pwm, S_IWUSR | S_IRUGO,
2164 show_auto_pwm, store_auto_pwm, 1, 2),
2165 SENSOR_ATTR_2(pwm2_auto_point3_temp, S_IWUSR | S_IRUGO,
2166 show_auto_temp, store_auto_temp, 1, 2),
2167 SENSOR_ATTR_2(pwm2_auto_point3_temp_hyst, S_IWUSR | S_IRUGO,
2168 show_auto_temp_hyst, store_auto_temp_hyst, 1, 2),
2169 SENSOR_ATTR_2(pwm2_auto_point4_pwm, S_IWUSR | S_IRUGO,
2170 show_auto_pwm, store_auto_pwm, 1, 3),
2171 SENSOR_ATTR_2(pwm2_auto_point4_temp, S_IWUSR | S_IRUGO,
2172 show_auto_temp, store_auto_temp, 1, 3),
2173 SENSOR_ATTR_2(pwm2_auto_point4_temp_hyst, S_IWUSR | S_IRUGO,
2174 show_auto_temp_hyst, store_auto_temp_hyst, 1, 3),
2175 SENSOR_ATTR_2(pwm2_auto_point5_pwm, S_IWUSR | S_IRUGO,
2176 show_auto_pwm, store_auto_pwm, 1, 4),
2177 SENSOR_ATTR_2(pwm2_auto_point5_temp, S_IWUSR | S_IRUGO,
2178 show_auto_temp, store_auto_temp, 1, 4),
2179 SENSOR_ATTR_2(pwm2_auto_point5_temp_hyst, S_IWUSR | S_IRUGO,
2180 show_auto_temp_hyst, store_auto_temp_hyst, 1, 4),
2181 SENSOR_ATTR_2(pwm2_auto_point6_pwm, S_IWUSR | S_IRUGO,
2182 show_auto_pwm, store_auto_pwm, 1, 5),
2183 SENSOR_ATTR_2(pwm2_auto_point6_temp, S_IWUSR | S_IRUGO,
2184 show_auto_temp, store_auto_temp, 1, 5),
2185 SENSOR_ATTR_2(pwm2_auto_point6_temp_hyst, S_IWUSR | S_IRUGO,
2186 show_auto_temp_hyst, store_auto_temp_hyst, 1, 5),
2187 SENSOR_ATTR_2(pwm2_auto_point7_pwm, S_IWUSR | S_IRUGO,
2188 show_auto_pwm, store_auto_pwm, 1, 6),
2189 SENSOR_ATTR_2(pwm2_auto_point7_temp, S_IWUSR | S_IRUGO,
2190 show_auto_temp, store_auto_temp, 1, 6),
2191 SENSOR_ATTR_2(pwm2_auto_point7_temp_hyst, S_IWUSR | S_IRUGO,
2192 show_auto_temp_hyst, store_auto_temp_hyst, 1, 6),
2194 SENSOR_ATTR_2(pwm3_auto_point1_pwm, S_IWUSR | S_IRUGO,
2195 show_auto_pwm, store_auto_pwm, 2, 0),
2196 SENSOR_ATTR_2(pwm3_auto_point1_temp, S_IWUSR | S_IRUGO,
2197 show_auto_temp, store_auto_temp, 2, 0),
2198 SENSOR_ATTR_2(pwm3_auto_point1_temp_hyst, S_IWUSR | S_IRUGO,
2199 show_auto_temp_hyst, store_auto_temp_hyst, 2, 0),
2200 SENSOR_ATTR_2(pwm3_auto_point2_pwm, S_IWUSR | S_IRUGO,
2201 show_auto_pwm, store_auto_pwm, 2, 1),
2202 SENSOR_ATTR_2(pwm3_auto_point2_temp, S_IWUSR | S_IRUGO,
2203 show_auto_temp, store_auto_temp, 2, 1),
2204 SENSOR_ATTR_2(pwm3_auto_point2_temp_hyst, S_IWUSR | S_IRUGO,
2205 show_auto_temp_hyst, store_auto_temp_hyst, 2, 1),
2206 SENSOR_ATTR_2(pwm3_auto_point3_pwm, S_IWUSR | S_IRUGO,
2207 show_auto_pwm, store_auto_pwm, 2, 2),
2208 SENSOR_ATTR_2(pwm3_auto_point3_temp, S_IWUSR | S_IRUGO,
2209 show_auto_temp, store_auto_temp, 2, 2),
2210 SENSOR_ATTR_2(pwm3_auto_point3_temp_hyst, S_IWUSR | S_IRUGO,
2211 show_auto_temp_hyst, store_auto_temp_hyst, 2, 2),
2212 SENSOR_ATTR_2(pwm3_auto_point4_pwm, S_IWUSR | S_IRUGO,
2213 show_auto_pwm, store_auto_pwm, 2, 3),
2214 SENSOR_ATTR_2(pwm3_auto_point4_temp, S_IWUSR | S_IRUGO,
2215 show_auto_temp, store_auto_temp, 2, 3),
2216 SENSOR_ATTR_2(pwm3_auto_point4_temp_hyst, S_IWUSR | S_IRUGO,
2217 show_auto_temp_hyst, store_auto_temp_hyst, 2, 3),
2218 SENSOR_ATTR_2(pwm3_auto_point5_pwm, S_IWUSR | S_IRUGO,
2219 show_auto_pwm, store_auto_pwm, 2, 4),
2220 SENSOR_ATTR_2(pwm3_auto_point5_temp, S_IWUSR | S_IRUGO,
2221 show_auto_temp, store_auto_temp, 2, 4),
2222 SENSOR_ATTR_2(pwm3_auto_point5_temp_hyst, S_IWUSR | S_IRUGO,
2223 show_auto_temp_hyst, store_auto_temp_hyst, 2, 4),
2224 SENSOR_ATTR_2(pwm3_auto_point6_pwm, S_IWUSR | S_IRUGO,
2225 show_auto_pwm, store_auto_pwm, 2, 5),
2226 SENSOR_ATTR_2(pwm3_auto_point6_temp, S_IWUSR | S_IRUGO,
2227 show_auto_temp, store_auto_temp, 2, 5),
2228 SENSOR_ATTR_2(pwm3_auto_point6_temp_hyst, S_IWUSR | S_IRUGO,
2229 show_auto_temp_hyst, store_auto_temp_hyst, 2, 5),
2230 SENSOR_ATTR_2(pwm3_auto_point7_pwm, S_IWUSR | S_IRUGO,
2231 show_auto_pwm, store_auto_pwm, 2, 6),
2232 SENSOR_ATTR_2(pwm3_auto_point7_temp, S_IWUSR | S_IRUGO,
2233 show_auto_temp, store_auto_temp, 2, 6),
2234 SENSOR_ATTR_2(pwm3_auto_point7_temp_hyst, S_IWUSR | S_IRUGO,
2235 show_auto_temp_hyst, store_auto_temp_hyst, 2, 6),
2237 SENSOR_ATTR_2(pwm4_auto_point1_pwm, S_IWUSR | S_IRUGO,
2238 show_auto_pwm, store_auto_pwm, 3, 0),
2239 SENSOR_ATTR_2(pwm4_auto_point1_temp, S_IWUSR | S_IRUGO,
2240 show_auto_temp, store_auto_temp, 3, 0),
2241 SENSOR_ATTR_2(pwm4_auto_point1_temp_hyst, S_IWUSR | S_IRUGO,
2242 show_auto_temp_hyst, store_auto_temp_hyst, 3, 0),
2243 SENSOR_ATTR_2(pwm4_auto_point2_pwm, S_IWUSR | S_IRUGO,
2244 show_auto_pwm, store_auto_pwm, 3, 1),
2245 SENSOR_ATTR_2(pwm4_auto_point2_temp, S_IWUSR | S_IRUGO,
2246 show_auto_temp, store_auto_temp, 3, 1),
2247 SENSOR_ATTR_2(pwm4_auto_point2_temp_hyst, S_IWUSR | S_IRUGO,
2248 show_auto_temp_hyst, store_auto_temp_hyst, 3, 1),
2249 SENSOR_ATTR_2(pwm4_auto_point3_pwm, S_IWUSR | S_IRUGO,
2250 show_auto_pwm, store_auto_pwm, 3, 2),
2251 SENSOR_ATTR_2(pwm4_auto_point3_temp, S_IWUSR | S_IRUGO,
2252 show_auto_temp, store_auto_temp, 3, 2),
2253 SENSOR_ATTR_2(pwm4_auto_point3_temp_hyst, S_IWUSR | S_IRUGO,
2254 show_auto_temp_hyst, store_auto_temp_hyst, 3, 2),
2255 SENSOR_ATTR_2(pwm4_auto_point4_pwm, S_IWUSR | S_IRUGO,
2256 show_auto_pwm, store_auto_pwm, 3, 3),
2257 SENSOR_ATTR_2(pwm4_auto_point4_temp, S_IWUSR | S_IRUGO,
2258 show_auto_temp, store_auto_temp, 3, 3),
2259 SENSOR_ATTR_2(pwm4_auto_point4_temp_hyst, S_IWUSR | S_IRUGO,
2260 show_auto_temp_hyst, store_auto_temp_hyst, 3, 3),
2261 SENSOR_ATTR_2(pwm4_auto_point5_pwm, S_IWUSR | S_IRUGO,
2262 show_auto_pwm, store_auto_pwm, 3, 4),
2263 SENSOR_ATTR_2(pwm4_auto_point5_temp, S_IWUSR | S_IRUGO,
2264 show_auto_temp, store_auto_temp, 3, 4),
2265 SENSOR_ATTR_2(pwm4_auto_point5_temp_hyst, S_IWUSR | S_IRUGO,
2266 show_auto_temp_hyst, store_auto_temp_hyst, 3, 4),
2267 SENSOR_ATTR_2(pwm4_auto_point6_pwm, S_IWUSR | S_IRUGO,
2268 show_auto_pwm, store_auto_pwm, 3, 5),
2269 SENSOR_ATTR_2(pwm4_auto_point6_temp, S_IWUSR | S_IRUGO,
2270 show_auto_temp, store_auto_temp, 3, 5),
2271 SENSOR_ATTR_2(pwm4_auto_point6_temp_hyst, S_IWUSR | S_IRUGO,
2272 show_auto_temp_hyst, store_auto_temp_hyst, 3, 5),
2273 SENSOR_ATTR_2(pwm4_auto_point7_pwm, S_IWUSR | S_IRUGO,
2274 show_auto_pwm, store_auto_pwm, 3, 6),
2275 SENSOR_ATTR_2(pwm4_auto_point7_temp, S_IWUSR | S_IRUGO,
2276 show_auto_temp, store_auto_temp, 3, 6),
2277 SENSOR_ATTR_2(pwm4_auto_point7_temp_hyst, S_IWUSR | S_IRUGO,
2278 show_auto_temp_hyst, store_auto_temp_hyst, 3, 6),
2280 SENSOR_ATTR_2(pwm5_auto_point1_pwm, S_IWUSR | S_IRUGO,
2281 show_auto_pwm, store_auto_pwm, 4, 0),
2282 SENSOR_ATTR_2(pwm5_auto_point1_temp, S_IWUSR | S_IRUGO,
2283 show_auto_temp, store_auto_temp, 4, 0),
2284 SENSOR_ATTR_2(pwm5_auto_point1_temp_hyst, S_IWUSR | S_IRUGO,
2285 show_auto_temp_hyst, store_auto_temp_hyst, 4, 0),
2286 SENSOR_ATTR_2(pwm5_auto_point2_pwm, S_IWUSR | S_IRUGO,
2287 show_auto_pwm, store_auto_pwm, 4, 1),
2288 SENSOR_ATTR_2(pwm5_auto_point2_temp, S_IWUSR | S_IRUGO,
2289 show_auto_temp, store_auto_temp, 4, 1),
2290 SENSOR_ATTR_2(pwm5_auto_point2_temp_hyst, S_IWUSR | S_IRUGO,
2291 show_auto_temp_hyst, store_auto_temp_hyst, 4, 1),
2292 SENSOR_ATTR_2(pwm5_auto_point3_pwm, S_IWUSR | S_IRUGO,
2293 show_auto_pwm, store_auto_pwm, 4, 2),
2294 SENSOR_ATTR_2(pwm5_auto_point3_temp, S_IWUSR | S_IRUGO,
2295 show_auto_temp, store_auto_temp, 4, 2),
2296 SENSOR_ATTR_2(pwm5_auto_point3_temp_hyst, S_IWUSR | S_IRUGO,
2297 show_auto_temp_hyst, store_auto_temp_hyst, 4, 2),
2298 SENSOR_ATTR_2(pwm5_auto_point4_pwm, S_IWUSR | S_IRUGO,
2299 show_auto_pwm, store_auto_pwm, 4, 3),
2300 SENSOR_ATTR_2(pwm5_auto_point4_temp, S_IWUSR | S_IRUGO,
2301 show_auto_temp, store_auto_temp, 4, 3),
2302 SENSOR_ATTR_2(pwm5_auto_point4_temp_hyst, S_IWUSR | S_IRUGO,
2303 show_auto_temp_hyst, store_auto_temp_hyst, 4, 3),
2304 SENSOR_ATTR_2(pwm5_auto_point5_pwm, S_IWUSR | S_IRUGO,
2305 show_auto_pwm, store_auto_pwm, 4, 4),
2306 SENSOR_ATTR_2(pwm5_auto_point5_temp, S_IWUSR | S_IRUGO,
2307 show_auto_temp, store_auto_temp, 4, 4),
2308 SENSOR_ATTR_2(pwm5_auto_point5_temp_hyst, S_IWUSR | S_IRUGO,
2309 show_auto_temp_hyst, store_auto_temp_hyst, 4, 4),
2310 SENSOR_ATTR_2(pwm5_auto_point6_pwm, S_IWUSR | S_IRUGO,
2311 show_auto_pwm, store_auto_pwm, 4, 5),
2312 SENSOR_ATTR_2(pwm5_auto_point6_temp, S_IWUSR | S_IRUGO,
2313 show_auto_temp, store_auto_temp, 4, 5),
2314 SENSOR_ATTR_2(pwm5_auto_point6_temp_hyst, S_IWUSR | S_IRUGO,
2315 show_auto_temp_hyst, store_auto_temp_hyst, 4, 5),
2316 SENSOR_ATTR_2(pwm5_auto_point7_pwm, S_IWUSR | S_IRUGO,
2317 show_auto_pwm, store_auto_pwm, 4, 6),
2318 SENSOR_ATTR_2(pwm5_auto_point7_temp, S_IWUSR | S_IRUGO,
2319 show_auto_temp, store_auto_temp, 4, 6),
2320 SENSOR_ATTR_2(pwm5_auto_point7_temp_hyst, S_IWUSR | S_IRUGO,
2321 show_auto_temp_hyst, store_auto_temp_hyst, 4, 6),
2325 show_vid(struct device *dev, struct device_attribute *attr, char *buf)
2327 struct nct6775_data *data = dev_get_drvdata(dev);
2328 return sprintf(buf, "%d\n", vid_from_reg(data->vid, data->vrm));
2330 static DEVICE_ATTR(cpu0_vid, S_IRUGO, show_vid, NULL);
2333 /* Case open detection */
2336 show_caseopen(struct device *dev, struct device_attribute *attr, char *buf)
2338 struct nct6775_data *data = nct6775_update_device(dev);
2340 return sprintf(buf, "%d\n",
2341 !!(data->caseopen & to_sensor_dev_attr_2(attr)->index));
2345 clear_caseopen(struct device *dev, struct device_attribute *attr,
2346 const char *buf, size_t count)
2348 struct nct6775_data *data = dev_get_drvdata(dev);
2352 if (kstrtoul(buf, 10, &val) || val != 0)
2355 mask = to_sensor_dev_attr_2(attr)->nr;
2357 mutex_lock(&data->update_lock);
2358 reg = nct6775_read_value(data, NCT6775_REG_CASEOPEN_CLR);
2359 nct6775_write_value(data, NCT6775_REG_CASEOPEN_CLR, reg | mask);
2360 nct6775_write_value(data, NCT6775_REG_CASEOPEN_CLR, reg & ~mask);
2361 data->valid = 0; /* Force cache refresh */
2362 mutex_unlock(&data->update_lock);
2367 static struct sensor_device_attribute_2 sda_caseopen[] = {
2368 SENSOR_ATTR_2(intrusion0_alarm, S_IWUSR | S_IRUGO, show_caseopen,
2369 clear_caseopen, 0x80, 0x10),
2370 SENSOR_ATTR_2(intrusion1_alarm, S_IWUSR | S_IRUGO, show_caseopen,
2371 clear_caseopen, 0x40, 0x40),
2375 * Driver and device management
2378 static void nct6775_device_remove_files(struct device *dev)
2381 * some entries in the following arrays may not have been used in
2382 * device_create_file(), but device_remove_file() will ignore them
2385 struct nct6775_data *data = dev_get_drvdata(dev);
2387 for (i = 0; i < data->pwm_num; i++) {
2388 sysfs_remove_group(&dev->kobj, &nct6775_group_pwm[i]);
2389 for (j = 0; j < ARRAY_SIZE(sda_auto_pwm_arrays); j++)
2390 device_remove_file(dev,
2391 &sda_auto_pwm_arrays[j].dev_attr);
2394 for (i = 0; i < ARRAY_SIZE(sda_sf3_max_step_arrays); i++)
2395 device_remove_file(dev, &sda_sf3_max_step_arrays[i].dev_attr);
2397 for (i = 0; i < data->in_num; i++)
2398 sysfs_remove_group(&dev->kobj, &nct6775_group_in[i]);
2400 for (i = 0; i < 5; i++) {
2401 device_remove_file(dev, &sda_fan_input[i].dev_attr);
2402 device_remove_file(dev, &sda_fan_alarm[i].dev_attr);
2403 device_remove_file(dev, &sda_fan_div[i].dev_attr);
2404 device_remove_file(dev, &sda_fan_min[i].dev_attr);
2406 for (i = 0; i < NUM_REG_TEMP; i++) {
2407 if (!(data->have_temp & (1 << i)))
2409 device_remove_file(dev, &sda_temp_input[i].dev_attr);
2410 device_remove_file(dev, &sda_temp_label[i].dev_attr);
2411 device_remove_file(dev, &sda_temp_max[i].dev_attr);
2412 device_remove_file(dev, &sda_temp_max_hyst[i].dev_attr);
2415 device_remove_file(dev, &sda_temp_alarm[i].dev_attr);
2416 device_remove_file(dev, &sda_temp_type[i].dev_attr);
2419 device_remove_file(dev, &sda_caseopen[0].dev_attr);
2420 device_remove_file(dev, &sda_caseopen[1].dev_attr);
2422 device_remove_file(dev, &dev_attr_name);
2423 device_remove_file(dev, &dev_attr_cpu0_vid);
2426 /* Get the monitoring functions started */
2427 static inline void __devinit nct6775_init_device(struct nct6775_data *data,
2433 /* Start monitoring if needed */
2434 tmp = nct6775_read_value(data, NCT6775_REG_CONFIG);
2436 nct6775_write_value(data, NCT6775_REG_CONFIG,
2439 /* Enable temperature sensors if needed */
2440 for (i = 0; i < NUM_REG_TEMP; i++) {
2441 if (!(data->have_temp & (1 << i)))
2443 if (!data->reg_temp_config[i])
2445 tmp = nct6775_read_value(data,
2446 data->reg_temp_config[i]);
2448 nct6775_write_value(data,
2449 data->reg_temp_config[i],
2453 /* Enable VBAT monitoring if needed */
2454 tmp = nct6775_read_value(data, NCT6775_REG_VBAT);
2456 nct6775_write_value(data, NCT6775_REG_VBAT, tmp | 0x01);
2458 for (i = 0; i < 3; i++) {
2459 const char *label = NULL;
2461 if (data->temp_label)
2462 label = data->temp_label[data->temp_src[i]];
2464 /* Digital source overrides analog type */
2465 if (label && strncmp(label, "PECI", 4) == 0)
2466 data->temp_type[i] = 6;
2467 else if (label && strncmp(label, "AMD", 3) == 0)
2468 data->temp_type[i] = 5;
2469 else if ((tmp & (0x02 << i)))
2470 data->temp_type[i] = 1; /* diode */
2472 data->temp_type[i] = 4; /* thermistor */
2476 static void w82627ehf_swap_tempreg(struct nct6775_data *data,
2481 tmp = data->temp_src[r1];
2482 data->temp_src[r1] = data->temp_src[r2];
2483 data->temp_src[r2] = tmp;
2485 tmp = data->reg_temp[r1];
2486 data->reg_temp[r1] = data->reg_temp[r2];
2487 data->reg_temp[r2] = tmp;
2489 tmp = data->reg_temp_over[r1];
2490 data->reg_temp_over[r1] = data->reg_temp_over[r2];
2491 data->reg_temp_over[r2] = tmp;
2493 tmp = data->reg_temp_hyst[r1];
2494 data->reg_temp_hyst[r1] = data->reg_temp_hyst[r2];
2495 data->reg_temp_hyst[r2] = tmp;
2497 tmp = data->reg_temp_config[r1];
2498 data->reg_temp_config[r1] = data->reg_temp_config[r2];
2499 data->reg_temp_config[r2] = tmp;
2502 static void __devinit
2503 nct6775_check_fan_inputs(const struct nct6775_sio_data *sio_data,
2504 struct nct6775_data *data)
2507 bool fan3pin, fan3min, fan4pin, fan4min, fan5pin;
2508 bool pwm3pin, pwm4pin, pwm5pin;
2510 superio_enter(sio_data->sioreg);
2512 /* fan4 and fan5 share some pins with the GPIO and serial flash */
2513 if (sio_data->kind == nct6775) {
2514 regval = superio_inb(sio_data->sioreg, 0x2c);
2516 fan3pin = regval & (1 << 6);
2518 pwm3pin = regval & (1 << 7);
2520 /* On NCT6775, fan4 shares pins with the fdc interface */
2521 fan4pin = !(superio_inb(sio_data->sioreg, 0x2A) & 0x80);
2526 } else if (sio_data->kind == nct6776) {
2527 bool gpok = superio_inb(sio_data->sioreg, 0x27) & 0x80;
2529 superio_select(sio_data->sioreg, NCT6775_LD_HWM);
2530 regval = superio_inb(sio_data->sioreg, SIO_REG_ENABLE);
2535 fan3pin = !(superio_inb(sio_data->sioreg, 0x24) & 0x40);
2540 fan4pin = superio_inb(sio_data->sioreg, 0x1C) & 0x01;
2545 fan5pin = superio_inb(sio_data->sioreg, 0x1C) & 0x02;
2552 } else { /* NCT6779D */
2553 regval = superio_inb(sio_data->sioreg, 0x1c);
2555 fan3pin = !(regval & (1 << 5));
2556 fan4pin = !(regval & (1 << 6));
2557 fan5pin = !(regval & (1 << 7));
2559 pwm3pin = !(regval & (1 << 0));
2560 pwm4pin = !(regval & (1 << 1));
2561 pwm5pin = !(regval & (1 << 2));
2567 superio_exit(sio_data->sioreg);
2569 data->has_fan = data->has_fan_min = 0x03; /* fan1 and fan2 */
2570 data->has_fan |= (fan3pin << 2);
2571 data->has_fan_min |= (fan3min << 2);
2573 data->has_fan |= (fan4pin << 3) | (fan5pin << 4);
2574 data->has_fan_min |= (fan4min << 3) | (fan5pin << 4);
2576 data->has_pwm = 0x03 | (pwm3pin << 2) | (pwm4pin << 3) | (pwm5pin << 4);
2579 static int __devinit nct6775_probe(struct platform_device *pdev)
2581 struct device *dev = &pdev->dev;
2582 struct nct6775_sio_data *sio_data = dev->platform_data;
2583 struct nct6775_data *data;
2584 struct resource *res;
2587 res = platform_get_resource(pdev, IORESOURCE_IO, 0);
2588 if (!request_region(res->start, IOREGION_LENGTH, DRVNAME)) {
2590 dev_err(dev, "Failed to request region 0x%lx-0x%lx\n",
2591 (unsigned long)res->start,
2592 (unsigned long)res->start + IOREGION_LENGTH - 1);
2596 data = devm_kzalloc(&pdev->dev, sizeof(struct nct6775_data),
2603 data->addr = res->start;
2604 mutex_init(&data->lock);
2605 mutex_init(&data->update_lock);
2606 data->name = nct6775_device_names[sio_data->kind];
2607 platform_set_drvdata(pdev, data);
2609 switch (sio_data->kind) {
2612 data->have_in = 0x1ff;
2616 data->have_in = 0x7fff;
2620 switch (sio_data->kind) {
2623 data->auto_pwm_num = 6;
2627 data->auto_pwm_num = 4;
2631 data->auto_pwm_num = 4;
2635 /* Default to no temperature inputs, code below will adjust as needed */
2636 data->have_temp = 0;
2638 /* Deal with temperature register setup first. */
2639 if (sio_data->kind == nct6775 || sio_data->kind == nct6776) {
2643 * Display temperature sensor output only if it monitors
2644 * a source other than one already reported. Always display
2645 * first three temperature registers, though.
2647 for (i = 0; i < NUM_REG_TEMP; i++) {
2650 data->reg_temp[i] = NCT6775_REG_TEMP[i];
2651 data->reg_temp_over[i] = NCT6775_REG_TEMP_OVER[i];
2652 data->reg_temp_hyst[i] = NCT6775_REG_TEMP_HYST[i];
2653 if (sio_data->kind == nct6775)
2654 data->reg_temp_config[i]
2655 = NCT6775_REG_TEMP_CONFIG[i];
2657 data->reg_temp_config[i]
2658 = NCT6776_REG_TEMP_CONFIG[i];
2660 src = nct6775_read_value(data,
2661 NCT6775_REG_TEMP_SOURCE[i]);
2663 if (src && !(mask & (1 << src))) {
2664 data->have_temp |= 1 << i;
2668 data->temp_src[i] = src;
2671 * Now do some register swapping if index 0..2 don't
2672 * point to SYSTIN(1), CPUIN(2), and AUXIN(3).
2673 * Idea is to have the first three attributes
2674 * report SYSTIN, CPUIN, and AUXIN if possible
2675 * without overriding the basic system configuration.
2677 if (i > 0 && data->temp_src[0] != 1
2678 && data->temp_src[i] == 1)
2679 w82627ehf_swap_tempreg(data, 0, i);
2680 if (i > 1 && data->temp_src[1] != 2
2681 && data->temp_src[i] == 2)
2682 w82627ehf_swap_tempreg(data, 1, i);
2683 if (i > 2 && data->temp_src[2] != 3
2684 && data->temp_src[i] == 3)
2685 w82627ehf_swap_tempreg(data, 2, i);
2687 if (sio_data->kind == nct6776) {
2689 * On NCT6776, AUXTIN and VIN3 pins are shared.
2690 * Only way to detect it is to check if AUXTIN is used
2691 * as a temperature source, and if that source is
2694 * If that is the case, disable in6, which reports VIN3.
2695 * Otherwise disable temp3.
2697 if (data->temp_src[2] == 3) {
2700 if (data->reg_temp_config[2])
2701 reg = nct6775_read_value(data,
2702 data->reg_temp_config[2]);
2704 reg = 0; /* Assume AUXTIN is used */
2707 data->have_temp &= ~(1 << 2);
2709 data->have_in &= ~(1 << 6);
2711 data->temp_label = nct6776_temp_label;
2713 data->temp_label = nct6775_temp_label;
2715 } else if (sio_data->kind == nct6779) {
2719 * Display temperature sensor output only if it monitors
2720 * a source other than one already reported. Always display
2721 * first three temperature registers, though.
2723 for (i = 0; i < ARRAY_SIZE(NCT6779_REG_TEMP); i++) {
2726 data->reg_temp[i] = NCT6779_REG_TEMP[i];
2727 data->reg_temp_over[i] = NCT6779_REG_TEMP_OVER[i];
2728 data->reg_temp_hyst[i] = NCT6779_REG_TEMP_HYST[i];
2729 data->reg_temp_config[i] = NCT6779_REG_TEMP_CONFIG[i];
2731 src = nct6775_read_value(data,
2732 NCT6779_REG_TEMP_SOURCE[i]);
2734 if (src && !(mask & (1 << src))) {
2735 data->have_temp |= 1 << i;
2739 data->temp_src[i] = src;
2742 * Now do some register swapping if index 0..2 don't
2743 * point to SYSTIN(1), CPUIN(2), and AUXIN(3).
2744 * Idea is to have the first three attributes
2745 * report SYSTIN, CPUIN, and AUXIN if possible
2746 * without overriding the basic system configuration.
2748 if (i > 0 && data->temp_src[0] != 1
2749 && data->temp_src[i] == 1)
2750 w82627ehf_swap_tempreg(data, 0, i);
2751 if (i > 1 && data->temp_src[1] != 2
2752 && data->temp_src[i] == 2)
2753 w82627ehf_swap_tempreg(data, 1, i);
2754 if (i > 2 && data->temp_src[2] != 3
2755 && data->temp_src[i] == 3)
2756 w82627ehf_swap_tempreg(data, 2, i);
2758 data->temp_label = nct6779_temp_label;
2766 * Assume voltage is disabled if the respective temperature is
2767 * used as temperature source.
2769 for (i = 0; i < ARRAY_SIZE(NCT6779_REG_TEMP); i++) {
2770 if (!(data->have_temp & (1 << i)))
2772 if (data->temp_src[i] == 3) /* AUXTIN0 */
2773 data->have_in &= ~(1 << 6); /* no VIN4 */
2774 if (data->temp_src[i] == 4) /* AUXTIN1 */
2775 data->have_in &= ~(1 << 10); /* no VIN5 */
2776 if (data->temp_src[i] == 5) /* AUXTIN2 */
2777 data->have_in &= ~(1 << 11); /* no VIN6 */
2778 if (data->temp_src[i] == 6) /* AUXTIN0 */
2779 data->have_in &= ~(1 << 14); /* no VIN7 */
2783 if (sio_data->kind == nct6775) {
2784 data->has_fan_div = true;
2785 data->fan_from_reg = fan_from_reg16;
2786 data->fan_from_reg_min = fan_from_reg8;
2787 data->REG_VIN = NCT6775_REG_IN;
2788 data->REG_TARGET = NCT6775_REG_TARGET;
2789 data->REG_FAN = NCT6775_REG_FAN;
2790 data->REG_FAN_MIN = NCT6775_REG_FAN_MIN;
2791 data->REG_FAN_START_OUTPUT = NCT6775_REG_FAN_START_OUTPUT;
2792 data->REG_FAN_STOP_OUTPUT = NCT6775_REG_FAN_STOP_OUTPUT;
2793 data->REG_FAN_MAX_OUTPUT = NCT6775_REG_FAN_MAX_OUTPUT;
2794 data->REG_FAN_STEP_OUTPUT = NCT6775_REG_FAN_STEP_OUTPUT;
2795 } else if (sio_data->kind == nct6776) {
2796 data->has_fan_div = false;
2797 data->fan_from_reg = fan_from_reg13;
2798 data->fan_from_reg_min = fan_from_reg13;
2799 data->REG_VIN = NCT6775_REG_IN;
2800 data->REG_TARGET = NCT6775_REG_TARGET;
2801 data->REG_FAN = NCT6775_REG_FAN;
2802 data->REG_FAN_MIN = NCT6776_REG_FAN_MIN;
2803 data->REG_FAN_START_OUTPUT = NCT6775_REG_FAN_START_OUTPUT;
2804 data->REG_FAN_STOP_OUTPUT = NCT6775_REG_FAN_STOP_OUTPUT;
2805 } else if (sio_data->kind == nct6779) {
2806 data->has_fan_div = false;
2807 data->fan_from_reg = fan_from_reg13;
2808 data->fan_from_reg_min = fan_from_reg13;
2809 data->REG_VIN = NCT6779_REG_IN;
2810 data->REG_TARGET = NCT6775_REG_TARGET;
2811 data->REG_FAN = NCT6779_REG_FAN;
2812 data->REG_FAN_MIN = NCT6776_REG_FAN_MIN;
2813 data->REG_FAN_START_OUTPUT = NCT6775_REG_FAN_START_OUTPUT;
2814 data->REG_FAN_STOP_OUTPUT = NCT6775_REG_FAN_STOP_OUTPUT;
2817 /* Initialize the chip */
2818 nct6775_init_device(data, sio_data->kind);
2820 data->vrm = vid_which_vrm();
2821 superio_enter(sio_data->sioreg);
2824 * We can get the VID input values directly at logical device D 0xe3.
2826 superio_select(sio_data->sioreg, NCT6775_LD_VID);
2827 data->vid = superio_inb(sio_data->sioreg, 0xe3);
2828 err = device_create_file(dev, &dev_attr_cpu0_vid);
2835 superio_select(sio_data->sioreg, NCT6775_LD_HWM);
2836 tmp = superio_inb(sio_data->sioreg, NCT6775_REG_FAN_DEBOUNCE);
2837 if (sio_data->kind == nct6776 || sio_data->kind == nct6779)
2838 superio_outb(sio_data->sioreg, NCT6775_REG_FAN_DEBOUNCE,
2841 superio_outb(sio_data->sioreg, NCT6775_REG_FAN_DEBOUNCE,
2843 pr_info("Enabled fan debounce for chip %s\n", data->name);
2846 superio_exit(sio_data->sioreg);
2848 nct6775_check_fan_inputs(sio_data, data);
2850 /* Read fan clock dividers immediately */
2851 nct6775_update_fan_div_common(dev, data);
2853 /* Register sysfs hooks */
2854 for (i = 0; i < data->pwm_num; i++) {
2855 if (!(data->has_pwm & (1 << i)))
2857 err = sysfs_create_group(&dev->kobj, &nct6775_group_pwm[i]);
2861 for (i = 0; i < ARRAY_SIZE(sda_auto_pwm_arrays); i++) {
2862 struct sensor_device_attribute_2 *attr =
2863 &sda_auto_pwm_arrays[i];
2865 if (!(data->has_pwm & (1 << attr->nr)))
2867 if (attr->index > data->auto_pwm_num)
2869 err = device_create_file(dev, &attr->dev_attr);
2874 if (data->REG_FAN_STEP_OUTPUT) {
2875 for (i = 0; i < ARRAY_SIZE(sda_sf3_max_step_arrays); i++) {
2876 struct sensor_device_attribute *attr =
2877 &sda_sf3_max_step_arrays[i];
2878 if (!(data->has_pwm & (1 << attr->index)))
2880 err = device_create_file(dev, &attr->dev_attr);
2886 for (i = 0; i < data->in_num; i++) {
2887 if (!(data->have_in & (1 << i)))
2889 err = sysfs_create_group(&dev->kobj, &nct6775_group_in[i]);
2894 for (i = 0; i < 5; i++) {
2895 if (data->has_fan & (1 << i)) {
2896 err = device_create_file(dev,
2897 &sda_fan_input[i].dev_attr);
2900 err = device_create_file(dev,
2901 &sda_fan_alarm[i].dev_attr);
2904 if (sio_data->kind != nct6776 &&
2905 sio_data->kind != nct6779) {
2906 err = device_create_file(dev,
2907 &sda_fan_div[i].dev_attr);
2911 if (data->has_fan_min & (1 << i)) {
2912 err = device_create_file(dev,
2913 &sda_fan_min[i].dev_attr);
2920 for (i = 0; i < NUM_REG_TEMP; i++) {
2921 if (!(data->have_temp & (1 << i)))
2923 err = device_create_file(dev, &sda_temp_input[i].dev_attr);
2926 if (data->temp_label) {
2927 err = device_create_file(dev,
2928 &sda_temp_label[i].dev_attr);
2932 if (data->reg_temp_over[i]) {
2933 err = device_create_file(dev,
2934 &sda_temp_max[i].dev_attr);
2938 if (data->reg_temp_hyst[i]) {
2939 err = device_create_file(dev,
2940 &sda_temp_max_hyst[i].dev_attr);
2946 err = device_create_file(dev, &sda_temp_alarm[i].dev_attr);
2949 err = device_create_file(dev, &sda_temp_type[i].dev_attr);
2954 err = device_create_file(dev, &sda_caseopen[0].dev_attr);
2958 if (sio_data->kind == nct6776 || sio_data->kind == nct6779) {
2959 err = device_create_file(dev, &sda_caseopen[1].dev_attr);
2964 err = device_create_file(dev, &dev_attr_name);
2968 data->hwmon_dev = hwmon_device_register(dev);
2969 if (IS_ERR(data->hwmon_dev)) {
2970 err = PTR_ERR(data->hwmon_dev);
2977 nct6775_device_remove_files(dev);
2979 platform_set_drvdata(pdev, NULL);
2980 release_region(res->start, IOREGION_LENGTH);
2985 static int __devexit nct6775_remove(struct platform_device *pdev)
2987 struct nct6775_data *data = platform_get_drvdata(pdev);
2989 hwmon_device_unregister(data->hwmon_dev);
2990 nct6775_device_remove_files(&pdev->dev);
2991 release_region(data->addr, IOREGION_LENGTH);
2992 platform_set_drvdata(pdev, NULL);
2997 static struct platform_driver nct6775_driver = {
2999 .owner = THIS_MODULE,
3002 .probe = nct6775_probe,
3003 .remove = __devexit_p(nct6775_remove),
3006 /* nct6775_find() looks for a '627 in the Super-I/O config space */
3007 static int __init nct6775_find(int sioaddr, unsigned short *addr,
3008 struct nct6775_sio_data *sio_data)
3010 static const char __initdata sio_name_NCT6775[] = "NCT6775F";
3011 static const char __initdata sio_name_NCT6776[] = "NCT6776F";
3012 static const char __initdata sio_name_NCT6779[] = "NCT6779F";
3015 const char *sio_name;
3017 superio_enter(sioaddr);
3022 val = (superio_inb(sioaddr, SIO_REG_DEVID) << 8)
3023 | superio_inb(sioaddr, SIO_REG_DEVID + 1);
3024 switch (val & SIO_ID_MASK) {
3025 case SIO_NCT6775_ID:
3026 sio_data->kind = nct6775;
3027 sio_name = sio_name_NCT6775;
3029 case SIO_NCT6776_ID:
3030 sio_data->kind = nct6776;
3031 sio_name = sio_name_NCT6776;
3033 case SIO_NCT6779_ID:
3034 sio_data->kind = nct6779;
3035 sio_name = sio_name_NCT6779;
3039 pr_debug("unsupported chip ID: 0x%04x\n", val);
3040 superio_exit(sioaddr);
3044 /* We have a known chip, find the HWM I/O address */
3045 superio_select(sioaddr, NCT6775_LD_HWM);
3046 val = (superio_inb(sioaddr, SIO_REG_ADDR) << 8)
3047 | superio_inb(sioaddr, SIO_REG_ADDR + 1);
3048 *addr = val & IOREGION_ALIGNMENT;
3050 pr_err("Refusing to enable a Super-I/O device with a base I/O port 0\n");
3051 superio_exit(sioaddr);
3055 /* Activate logical device if needed */
3056 val = superio_inb(sioaddr, SIO_REG_ENABLE);
3057 if (!(val & 0x01)) {
3058 pr_warn("Forcibly enabling Super-I/O. "
3059 "Sensor is probably unusable.\n");
3060 superio_outb(sioaddr, SIO_REG_ENABLE, val | 0x01);
3063 superio_exit(sioaddr);
3064 pr_info("Found %s chip at %#x\n", sio_name, *addr);
3065 sio_data->sioreg = sioaddr;
3071 * when Super-I/O functions move to a separate file, the Super-I/O
3072 * bus will manage the lifetime of the device and this module will only keep
3073 * track of the nct6775 driver. But since we platform_device_alloc(), we
3074 * must keep track of the device
3076 static struct platform_device *pdev;
3078 static int __init sensors_nct6775_init(void)
3081 unsigned short address;
3082 struct resource res;
3083 struct nct6775_sio_data sio_data;
3086 * initialize sio_data->kind and sio_data->sioreg.
3088 * when Super-I/O functions move to a separate file, the Super-I/O
3089 * driver will probe 0x2e and 0x4e and auto-detect the presence of a
3090 * nct6775 hardware monitor, and call probe()
3092 if (nct6775_find(0x2e, &address, &sio_data) &&
3093 nct6775_find(0x4e, &address, &sio_data))
3096 err = platform_driver_register(&nct6775_driver);
3100 pdev = platform_device_alloc(DRVNAME, address);
3103 pr_err("Device allocation failed\n");
3104 goto exit_unregister;
3107 err = platform_device_add_data(pdev, &sio_data,
3108 sizeof(struct nct6775_sio_data));
3110 pr_err("Platform data allocation failed\n");
3111 goto exit_device_put;
3114 memset(&res, 0, sizeof(res));
3116 res.start = address + IOREGION_OFFSET;
3117 res.end = address + IOREGION_OFFSET + IOREGION_LENGTH - 1;
3118 res.flags = IORESOURCE_IO;
3120 err = acpi_check_resource_conflict(&res);
3122 goto exit_device_put;
3124 err = platform_device_add_resources(pdev, &res, 1);
3126 pr_err("Device resource addition failed (%d)\n", err);
3127 goto exit_device_put;
3130 /* platform_device_add calls probe() */
3131 err = platform_device_add(pdev);
3133 pr_err("Device addition failed (%d)\n", err);
3134 goto exit_device_put;
3140 platform_device_put(pdev);
3142 platform_driver_unregister(&nct6775_driver);
3147 static void __exit sensors_nct6775_exit(void)
3149 platform_device_unregister(pdev);
3150 platform_driver_unregister(&nct6775_driver);
3153 MODULE_AUTHOR("Guenter Roeck <linux@roeck-us.net>");
3154 MODULE_DESCRIPTION("NCT677x driver");
3155 MODULE_LICENSE("GPL");
3157 module_init(sensors_nct6775_init);
3158 module_exit(sensors_nct6775_exit);