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[] = { 0x630, 0x632, 0x634, 0x636, 0x638 };
214 static const u16 NCT6776_REG_FAN_MIN[] = { 0x63a, 0x63c, 0x63e, 0x640, 0x642};
216 static const u16 NCT6779_REG_TOLERANCE_H[] = {
217 0x10c, 0x20c, 0x30c, 0x40c, 0x50c };
219 static const u16 NCT6779_REG_FAN[] = { 0x4c0, 0x4c2, 0x4c4, 0x4c6, 0x4c8 };
221 static const u16 NCT6775_REG_TEMP[]
222 = { 0x27, 0x150, 0x250, 0x73, 0x75, 0x77, 0x62b, 0x62c, 0x62d };
223 static const u16 NCT6775_REG_TEMP_CONFIG[]
224 = { 0, 0x152, 0x252, 0, 0, 0, 0x628, 0x629, 0x62A };
225 static const u16 NCT6775_REG_TEMP_HYST[]
226 = { 0x3a, 0x153, 0x253, 0, 0, 0, 0x673, 0x678, 0x67D };
227 static const u16 NCT6775_REG_TEMP_OVER[]
228 = { 0x39, 0x155, 0x255, 0, 0, 0, 0x672, 0x677, 0x67C };
229 static const u16 NCT6775_REG_TEMP_SOURCE[]
230 = { 0x621, 0x622, 0x623, 0x100, 0x200, 0x300, 0x624, 0x625, 0x626 };
232 static const u16 NCT6776_REG_TEMP_CONFIG[]
233 = { 0x18, 0x152, 0x252, 0, 0, 0, 0x628, 0x629, 0x62A };
235 static const u16 NCT6779_REG_TEMP[]
236 = { 0x27, 0x150, 0x73, 0x75, 0x77, 0x79, 0x7b };
237 static const u16 NCT6779_REG_TEMP_CONFIG[]
238 = { 0x18, 0x152, 0, 0, 0, 0, 0 };
239 static const u16 NCT6779_REG_TEMP_SOURCE[]
240 = { 0x621, 0x622, 0x100, 0x200, 0x300, 0x800, 0x900 };
241 static const u16 NCT6779_REG_TEMP_HYST[]
242 = { 0x3a, 0x153, 0, 0, 0, 0, 0, 0, 0 };
243 static const u16 NCT6779_REG_TEMP_OVER[]
244 = { 0x39, 0x155, 0, 0, 0, 0, 0, 0, 0 };
246 static const u16 NCT6775_REG_AUTO_BASE[] = {
247 0x100, 0x200, 0x300, 0x800, 0x900 };
249 #define NCT6775_REG_AUTO_TEMP(nr, p) (NCT6775_REG_AUTO_BASE[nr] + 0x21 + (p))
250 #define NCT6775_REG_AUTO_PWM(nr, p) (NCT6775_REG_AUTO_BASE[nr] + 0x27 + (p))
252 static const u16 NCT6775_REG_CRITICAL_ENAB[] = { 0x134, 0x234, 0x334 };
253 static const u16 NCT6775_REG_CRITICAL_TEMP[] = {
254 0x135, 0x235, 0x335, 0x835, 0x935 };
255 static const u16 NCT6775_REG_CRITICAL_TEMP_TOLERANCE[] = {
256 0x138, 0x238, 0x338, 0x838, 0x938 };
258 static const u16 NCT6779_REG_CRITICAL_PWM_ENABLE[] = {
259 0x136, 0x236, 0x336, 0x836, 0x936 };
260 static const u16 NCT6779_REG_CRITICAL_PWM[] = {
261 0x137, 0x237, 0x337, 0x837, 0x937 };
263 static const char *const nct6775_temp_label[] = {
277 "PCH_CHIP_CPU_MAX_TEMP",
287 static const char *const nct6776_temp_label[] = {
302 "PCH_CHIP_CPU_MAX_TEMP",
313 static const char *const nct6779_temp_label[] = {
332 "PCH_CHIP_CPU_MAX_TEMP",
343 #define NUM_REG_TEMP ARRAY_SIZE(NCT6775_REG_TEMP)
345 static inline int reg_to_pwm_enable(int pwm, int mode)
347 if (mode == 0 && pwm == 255)
349 if (mode == 3) /* SmartFan III */
350 return 2; /* convert to thermal cruise */
353 return 4; /* SmartFan IV */
356 static inline int pwm_enable_to_reg(int mode)
365 static int is_word_sized(u16 reg)
367 return ((((reg & 0xff00) == 0x100
368 || (reg & 0xff00) == 0x200)
369 && ((reg & 0x00ff) == 0x50
370 || (reg & 0x00ff) == 0x53
371 || (reg & 0x00ff) == 0x55))
372 || (reg & 0xfff0) == 0x630
373 || (reg & 0xfff0) == 0x4c0
374 || reg == 0x640 || reg == 0x642
375 || ((reg & 0xfff0) == 0x650
376 && (reg & 0x000f) >= 0x06)
377 || reg == 0x73 || reg == 0x75 || reg == 0x77 || reg == 0x77
386 /* 1 is PWM mode, output in ms */
387 static inline unsigned int step_time_from_reg(u8 reg, u8 mode)
389 return mode ? 100 * reg : 400 * reg;
392 static inline u8 step_time_to_reg(unsigned int msec, u8 mode)
394 return SENSORS_LIMIT((mode ? (msec + 50) / 100 :
395 (msec + 200) / 400), 1, 255);
398 static unsigned int fan_from_reg8(u16 reg, unsigned int divreg)
400 if (reg == 0 || reg == 255)
402 return 1350000U / (reg << divreg);
405 static unsigned int fan_from_reg13(u16 reg, unsigned int divreg)
407 if ((reg & 0xff1f) == 0xff1f)
410 reg = (reg & 0x1f) | ((reg & 0xff00) >> 3);
415 return 1350000U / reg;
418 static unsigned int fan_from_reg16(u16 reg, unsigned int divreg)
420 if (reg == 0 || reg == 0xffff)
424 * Even though the registers are 16 bit wide, the fan divisor
427 return 1350000U / (reg << divreg);
430 static inline unsigned int
437 * Some of the voltage inputs have internal scaling, the tables below
438 * contain 8 (the ADC LSB in mV) * scaling factor * 100
440 static const u16 scale_in[15] = {
441 800, 800, 1600, 1600, 800, 800, 800, 1600, 1600, 800, 800, 800, 800,
445 static inline long in_from_reg(u8 reg, u8 nr)
447 return DIV_ROUND_CLOSEST(reg * scale_in[nr], 100);
450 static inline u8 in_to_reg(u32 val, u8 nr)
452 return SENSORS_LIMIT(DIV_ROUND_CLOSEST(val * 100, scale_in[nr]), 0,
457 * Data structures and manipulation thereof
460 struct nct6775_data {
461 int addr; /* IO base of hw monitor block */
464 struct device *hwmon_dev;
467 u16 reg_temp[NUM_REG_TEMP];
468 u16 reg_temp_over[NUM_REG_TEMP];
469 u16 reg_temp_hyst[NUM_REG_TEMP];
470 u16 reg_temp_config[NUM_REG_TEMP];
471 u8 temp_src[NUM_REG_TEMP];
472 const char * const *temp_label;
476 const u16 *REG_TARGET;
478 const u16 *REG_FAN_MIN;
479 const u16 *REG_FAN_START_OUTPUT;
480 const u16 *REG_FAN_STOP_OUTPUT;
482 unsigned int (*fan_from_reg)(u16 reg, unsigned int divreg);
483 unsigned int (*fan_from_reg_min)(u16 reg, unsigned int divreg);
485 struct mutex update_lock;
486 char valid; /* !=0 if following fields are valid */
487 unsigned long last_updated; /* In jiffies */
489 /* Register values */
490 u8 bank; /* current register bank */
491 u8 in_num; /* number of in inputs we have */
492 u8 in[15]; /* Register value */
493 u8 in_max[15]; /* Register value */
494 u8 in_min[15]; /* Register value */
499 u8 has_fan; /* some fan inputs can be disabled */
500 u8 has_fan_min; /* some fans don't have min register */
505 s16 temp_max_hyst[9];
509 u8 pwm_mode[5]; /* 0->DC variable voltage, 1->PWM variable duty cycle */
510 u8 pwm_enable[5]; /* 0->off
512 * 2->thermal cruise mode (also called SmartFan I)
513 * 3->fan speed cruise mode
514 * 4->enhanced variable thermal cruise (also called
517 u8 pwm_num; /* number of pwm */
522 u8 fan_start_output[5]; /* minimum fan speed when spinning up */
523 u8 fan_stop_output[5]; /* minimum fan speed when spinning down */
524 u8 fan_stop_time[5]; /* time at minimum before disabling fan */
525 u8 fan_step_up_time[5];
526 u8 fan_step_down_time[5];
528 /* Automatic fan speed control registers */
540 struct nct6775_sio_data {
546 * On older chips, only registers 0x50-0x5f are banked.
547 * On more recent chips, all registers are banked.
548 * Assume that is the case and set the bank number for each access.
549 * Cache the bank number so it only needs to be set if it changes.
551 static inline void nct6775_set_bank(struct nct6775_data *data, u16 reg)
554 if (data->bank != bank) {
555 outb_p(NCT6775_REG_BANK, data->addr + ADDR_REG_OFFSET);
556 outb_p(bank, data->addr + DATA_REG_OFFSET);
561 static u16 nct6775_read_value(struct nct6775_data *data, u16 reg)
563 int res, word_sized = is_word_sized(reg);
565 mutex_lock(&data->lock);
567 nct6775_set_bank(data, reg);
568 outb_p(reg & 0xff, data->addr + ADDR_REG_OFFSET);
569 res = inb_p(data->addr + DATA_REG_OFFSET);
571 outb_p((reg & 0xff) + 1,
572 data->addr + ADDR_REG_OFFSET);
573 res = (res << 8) + inb_p(data->addr + DATA_REG_OFFSET);
576 mutex_unlock(&data->lock);
580 static int nct6775_write_value(struct nct6775_data *data, u16 reg,
583 int word_sized = is_word_sized(reg);
585 mutex_lock(&data->lock);
587 nct6775_set_bank(data, reg);
588 outb_p(reg & 0xff, data->addr + ADDR_REG_OFFSET);
590 outb_p(value >> 8, data->addr + DATA_REG_OFFSET);
591 outb_p((reg & 0xff) + 1,
592 data->addr + ADDR_REG_OFFSET);
594 outb_p(value & 0xff, data->addr + DATA_REG_OFFSET);
596 mutex_unlock(&data->lock);
600 /* We left-align 8-bit temperature values to make the code simpler */
601 static u16 nct6775_read_temp(struct nct6775_data *data, u16 reg)
605 res = nct6775_read_value(data, reg);
606 if (!is_word_sized(reg))
612 static int nct6775_write_temp(struct nct6775_data *data, u16 reg,
615 if (!is_word_sized(reg))
617 return nct6775_write_value(data, reg, value);
620 /* This function assumes that the caller holds data->update_lock */
621 static void nct6775_write_fan_div(struct nct6775_data *data, int nr)
627 reg = (nct6775_read_value(data, NCT6775_REG_FANDIV1) & 0x70)
628 | (data->fan_div[0] & 0x7);
629 nct6775_write_value(data, NCT6775_REG_FANDIV1, reg);
632 reg = (nct6775_read_value(data, NCT6775_REG_FANDIV1) & 0x7)
633 | ((data->fan_div[1] << 4) & 0x70);
634 nct6775_write_value(data, NCT6775_REG_FANDIV1, reg);
636 reg = (nct6775_read_value(data, NCT6775_REG_FANDIV2) & 0x70)
637 | (data->fan_div[2] & 0x7);
638 nct6775_write_value(data, NCT6775_REG_FANDIV2, reg);
641 reg = (nct6775_read_value(data, NCT6775_REG_FANDIV2) & 0x7)
642 | ((data->fan_div[3] << 4) & 0x70);
643 nct6775_write_value(data, NCT6775_REG_FANDIV2, reg);
648 static void nct6775_write_fan_div_common(struct device *dev,
649 struct nct6775_data *data, int nr)
651 struct nct6775_sio_data *sio_data = dev->platform_data;
653 if (sio_data->kind == nct6775)
654 nct6775_write_fan_div(data, nr);
657 static void nct6775_update_fan_div(struct nct6775_data *data)
661 i = nct6775_read_value(data, NCT6775_REG_FANDIV1);
662 data->fan_div[0] = i & 0x7;
663 data->fan_div[1] = (i & 0x70) >> 4;
664 i = nct6775_read_value(data, NCT6775_REG_FANDIV2);
665 data->fan_div[2] = i & 0x7;
666 if (data->has_fan & (1<<3))
667 data->fan_div[3] = (i & 0x70) >> 4;
670 static void nct6775_update_fan_div_common(struct device *dev,
671 struct nct6775_data *data)
673 struct nct6775_sio_data *sio_data = dev->platform_data;
675 if (sio_data->kind == nct6775)
676 nct6775_update_fan_div(data);
679 static void nct6775_update_pwm(struct device *dev)
681 struct nct6775_data *data = dev_get_drvdata(dev);
682 struct nct6775_sio_data *sio_data = dev->platform_data;
684 int pwmcfg, fanmodecfg, tol;
686 for (i = 0; i < data->pwm_num; i++) {
689 !(nct6775_read_value(data,
690 NCT6775_REG_PWM_ENABLE[0]) & 1);
694 data->pwm_mode[i] = pwmcfg;
696 fanmodecfg = nct6775_read_value(data,
697 NCT6775_REG_FAN_MODE[i]);
698 data->pwm[i] = nct6775_read_value(data, NCT6775_REG_PWM[i]);
699 data->pwm_enable[i] = reg_to_pwm_enable(data->pwm[i],
700 (fanmodecfg >> 4) & 7);
702 data->tolerance[i][0] = fanmodecfg & 0x0f;
703 if (sio_data->kind == nct6779) {
704 tol = nct6775_read_value(data,
705 NCT6779_REG_TOLERANCE_H[i]);
706 data->tolerance[i][0] |= (tol & 0x70) >> 1;
708 data->tolerance[i][1] =
709 nct6775_read_value(data,
710 NCT6775_REG_CRITICAL_TEMP_TOLERANCE[i]);
714 static void nct6775_update_pwm_limits(struct device *dev)
716 struct nct6775_data *data = dev_get_drvdata(dev);
717 struct nct6775_sio_data *sio_data = dev->platform_data;
721 for (i = 0; i < data->pwm_num; i++) {
722 if (!(data->has_pwm & (1 << i)))
725 data->fan_start_output[i] =
726 nct6775_read_value(data, data->REG_FAN_START_OUTPUT[i]);
727 data->fan_stop_output[i] =
728 nct6775_read_value(data, data->REG_FAN_STOP_OUTPUT[i]);
729 data->fan_stop_time[i] =
730 nct6775_read_value(data, NCT6775_REG_FAN_STOP_TIME[i]);
731 data->fan_step_up_time[i] =
732 nct6775_read_value(data, NCT6775_REG_FAN_STEP_UP_TIME[i]);
733 data->fan_step_down_time[i] =
734 nct6775_read_value(data, NCT6775_REG_FAN_STEP_DOWN_TIME[i]);
736 data->target_temp[i] =
737 nct6775_read_value(data,
738 data->REG_TARGET[i]) &
739 (data->pwm_mode[i] == 1 ? 0x7f : 0xff);
740 for (j = 0; j < data->auto_pwm_num; j++) {
741 data->auto_pwm[i][j] =
742 nct6775_read_value(data, NCT6775_REG_AUTO_PWM(i, j));
743 data->auto_temp[i][j] =
744 nct6775_read_value(data, NCT6775_REG_AUTO_TEMP(i, j));
746 /* handle critical auto_pwm temperature data */
747 data->auto_temp[i][data->auto_pwm_num] =
748 nct6775_read_value(data, NCT6775_REG_CRITICAL_TEMP[i]);
750 switch (sio_data->kind) {
752 reg = nct6775_read_value(data,
753 NCT6775_REG_CRITICAL_ENAB[i]);
754 data->auto_pwm[i][data->auto_pwm_num] =
755 (reg & 0x02) ? 0xff : 0x00;
758 data->auto_pwm[i][data->auto_pwm_num] = 0xff;
761 reg = nct6775_read_value(data,
762 NCT6779_REG_CRITICAL_PWM_ENABLE[i]);
764 data->auto_pwm[i][data->auto_pwm_num] =
765 nct6775_read_value(data,
766 NCT6779_REG_CRITICAL_PWM[i]);
768 data->auto_pwm[i][data->auto_pwm_num] = 0xff;
774 static struct nct6775_data *nct6775_update_device(struct device *dev)
776 struct nct6775_data *data = dev_get_drvdata(dev);
777 struct nct6775_sio_data *sio_data = dev->platform_data;
781 mutex_lock(&data->update_lock);
783 if (time_after(jiffies, data->last_updated + HZ + HZ/2)
785 /* Fan clock dividers */
786 nct6775_update_fan_div_common(dev, data);
788 /* Measured voltages and limits */
789 for (i = 0; i < data->in_num; i++) {
790 if (!(data->have_in & (1 << i)))
793 data->in[i] = nct6775_read_value(data,
795 data->in_min[i] = nct6775_read_value(data,
796 NCT6775_REG_IN_MIN(i));
797 data->in_max[i] = nct6775_read_value(data,
798 NCT6775_REG_IN_MAX(i));
801 /* Measured fan speeds and limits */
802 for (i = 0; i < 5; i++) {
805 if (!(data->has_fan & (1 << i)))
808 reg = nct6775_read_value(data, data->REG_FAN[i]);
809 data->rpm[i] = data->fan_from_reg(reg,
812 if (data->has_fan_min & (1 << i))
813 data->fan_min[i] = nct6775_read_value(data,
814 data->REG_FAN_MIN[i]);
817 * If we failed to measure the fan speed and clock
818 * divider can be increased, let's try that for next
821 if (data->has_fan_div
822 && (reg >= 0xff || (sio_data->kind == nct6775
824 && data->fan_div[i] < 0x07) {
825 dev_dbg(dev, "Increasing fan%d "
826 "clock divider from %u to %u\n",
827 i + 1, div_from_reg(data->fan_div[i]),
828 div_from_reg(data->fan_div[i] + 1));
830 nct6775_write_fan_div_common(dev, data, i);
831 /* Preserve min limit if possible */
832 if ((data->has_fan_min & (1 << i))
833 && data->fan_min[i] >= 2
834 && data->fan_min[i] != 255)
835 nct6775_write_value(data,
836 data->REG_FAN_MIN[i],
837 (data->fan_min[i] /= 2));
841 nct6775_update_pwm(dev);
842 nct6775_update_pwm_limits(dev);
844 /* Measured temperatures and limits */
845 for (i = 0; i < NUM_REG_TEMP; i++) {
846 if (!(data->have_temp & (1 << i)))
848 data->temp[i] = nct6775_read_temp(data,
850 if (data->reg_temp_over[i])
852 = nct6775_read_temp(data,
853 data->reg_temp_over[i]);
854 if (data->reg_temp_hyst[i])
855 data->temp_max_hyst[i]
856 = nct6775_read_temp(data,
857 data->reg_temp_hyst[i]);
860 data->alarms = nct6775_read_value(data,
861 NCT6775_REG_ALARM1) |
862 (nct6775_read_value(data,
863 NCT6775_REG_ALARM2) << 8) |
864 (nct6775_read_value(data,
865 NCT6775_REG_ALARM3) << 16);
866 if (sio_data->kind == nct6779) {
867 data->alarms |= nct6775_read_value(data,
868 NCT6779_REG_ALARM4) << 24;
871 data->caseopen = nct6775_read_value(data,
872 NCT6775_REG_CASEOPEN_DET);
874 data->last_updated = jiffies;
878 mutex_unlock(&data->update_lock);
883 * Sysfs callback functions
885 #define show_in_reg(reg) \
887 show_##reg(struct device *dev, struct device_attribute *attr, \
890 struct nct6775_data *data = nct6775_update_device(dev); \
891 struct sensor_device_attribute *sensor_attr = \
892 to_sensor_dev_attr(attr); \
893 int nr = sensor_attr->index; \
894 return sprintf(buf, "%ld\n", in_from_reg(data->reg[nr], nr)); \
900 #define store_in_reg(REG, reg) \
902 store_in_##reg(struct device *dev, struct device_attribute *attr, \
903 const char *buf, size_t count) \
905 struct nct6775_data *data = dev_get_drvdata(dev); \
906 struct sensor_device_attribute *sensor_attr = \
907 to_sensor_dev_attr(attr); \
908 int nr = sensor_attr->index; \
910 int err = kstrtoul(buf, 10, &val); \
913 mutex_lock(&data->update_lock); \
914 data->in_##reg[nr] = in_to_reg(val, nr); \
915 nct6775_write_value(data, NCT6775_REG_IN_##REG(nr), \
916 data->in_##reg[nr]); \
917 mutex_unlock(&data->update_lock); \
921 store_in_reg(MIN, min)
922 store_in_reg(MAX, max)
924 static ssize_t show_alarm(struct device *dev, struct device_attribute *attr,
927 struct nct6775_data *data = nct6775_update_device(dev);
928 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
929 int nr = sensor_attr->index;
930 return sprintf(buf, "%u\n", (data->alarms >> nr) & 0x01);
933 static SENSOR_DEVICE_ATTR(in0_input, S_IRUGO, show_in, NULL, 0);
934 static SENSOR_DEVICE_ATTR(in1_input, S_IRUGO, show_in, NULL, 1);
935 static SENSOR_DEVICE_ATTR(in2_input, S_IRUGO, show_in, NULL, 2);
936 static SENSOR_DEVICE_ATTR(in3_input, S_IRUGO, show_in, NULL, 3);
937 static SENSOR_DEVICE_ATTR(in4_input, S_IRUGO, show_in, NULL, 4);
938 static SENSOR_DEVICE_ATTR(in5_input, S_IRUGO, show_in, NULL, 5);
939 static SENSOR_DEVICE_ATTR(in6_input, S_IRUGO, show_in, NULL, 6);
940 static SENSOR_DEVICE_ATTR(in7_input, S_IRUGO, show_in, NULL, 7);
941 static SENSOR_DEVICE_ATTR(in8_input, S_IRUGO, show_in, NULL, 8);
942 static SENSOR_DEVICE_ATTR(in9_input, S_IRUGO, show_in, NULL, 9);
943 static SENSOR_DEVICE_ATTR(in10_input, S_IRUGO, show_in, NULL, 10);
944 static SENSOR_DEVICE_ATTR(in11_input, S_IRUGO, show_in, NULL, 11);
945 static SENSOR_DEVICE_ATTR(in12_input, S_IRUGO, show_in, NULL, 12);
946 static SENSOR_DEVICE_ATTR(in13_input, S_IRUGO, show_in, NULL, 13);
947 static SENSOR_DEVICE_ATTR(in14_input, S_IRUGO, show_in, NULL, 14);
949 static SENSOR_DEVICE_ATTR(in0_alarm, S_IRUGO, show_alarm, NULL, 0);
950 static SENSOR_DEVICE_ATTR(in1_alarm, S_IRUGO, show_alarm, NULL, 1);
951 static SENSOR_DEVICE_ATTR(in2_alarm, S_IRUGO, show_alarm, NULL, 2);
952 static SENSOR_DEVICE_ATTR(in3_alarm, S_IRUGO, show_alarm, NULL, 3);
953 static SENSOR_DEVICE_ATTR(in4_alarm, S_IRUGO, show_alarm, NULL, 8);
954 static SENSOR_DEVICE_ATTR(in5_alarm, S_IRUGO, show_alarm, NULL, 21);
955 static SENSOR_DEVICE_ATTR(in6_alarm, S_IRUGO, show_alarm, NULL, 20);
956 static SENSOR_DEVICE_ATTR(in7_alarm, S_IRUGO, show_alarm, NULL, 16);
957 static SENSOR_DEVICE_ATTR(in8_alarm, S_IRUGO, show_alarm, NULL, 17);
958 static SENSOR_DEVICE_ATTR(in9_alarm, S_IRUGO, show_alarm, NULL, 24);
959 static SENSOR_DEVICE_ATTR(in10_alarm, S_IRUGO, show_alarm, NULL, 25);
960 static SENSOR_DEVICE_ATTR(in11_alarm, S_IRUGO, show_alarm, NULL, 26);
961 static SENSOR_DEVICE_ATTR(in12_alarm, S_IRUGO, show_alarm, NULL, 27);
962 static SENSOR_DEVICE_ATTR(in13_alarm, S_IRUGO, show_alarm, NULL, 28);
963 static SENSOR_DEVICE_ATTR(in14_alarm, S_IRUGO, show_alarm, NULL, 29);
965 static SENSOR_DEVICE_ATTR(in0_min, S_IWUSR | S_IRUGO, show_in_min,
967 static SENSOR_DEVICE_ATTR(in1_min, S_IWUSR | S_IRUGO, show_in_min,
969 static SENSOR_DEVICE_ATTR(in2_min, S_IWUSR | S_IRUGO, show_in_min,
971 static SENSOR_DEVICE_ATTR(in3_min, S_IWUSR | S_IRUGO, show_in_min,
973 static SENSOR_DEVICE_ATTR(in4_min, S_IWUSR | S_IRUGO, show_in_min,
975 static SENSOR_DEVICE_ATTR(in5_min, S_IWUSR | S_IRUGO, show_in_min,
977 static SENSOR_DEVICE_ATTR(in6_min, S_IWUSR | S_IRUGO, show_in_min,
979 static SENSOR_DEVICE_ATTR(in7_min, S_IWUSR | S_IRUGO, show_in_min,
981 static SENSOR_DEVICE_ATTR(in8_min, S_IWUSR | S_IRUGO, show_in_min,
983 static SENSOR_DEVICE_ATTR(in9_min, S_IWUSR | S_IRUGO, show_in_min,
985 static SENSOR_DEVICE_ATTR(in10_min, S_IWUSR | S_IRUGO, show_in_min,
987 static SENSOR_DEVICE_ATTR(in11_min, S_IWUSR | S_IRUGO, show_in_min,
989 static SENSOR_DEVICE_ATTR(in12_min, S_IWUSR | S_IRUGO, show_in_min,
991 static SENSOR_DEVICE_ATTR(in13_min, S_IWUSR | S_IRUGO, show_in_min,
993 static SENSOR_DEVICE_ATTR(in14_min, S_IWUSR | S_IRUGO, show_in_min,
996 static SENSOR_DEVICE_ATTR(in0_max, S_IWUSR | S_IRUGO, show_in_max,
998 static SENSOR_DEVICE_ATTR(in1_max, S_IWUSR | S_IRUGO, show_in_max,
1000 static SENSOR_DEVICE_ATTR(in2_max, S_IWUSR | S_IRUGO, show_in_max,
1002 static SENSOR_DEVICE_ATTR(in3_max, S_IWUSR | S_IRUGO, show_in_max,
1004 static SENSOR_DEVICE_ATTR(in4_max, S_IWUSR | S_IRUGO, show_in_max,
1006 static SENSOR_DEVICE_ATTR(in5_max, S_IWUSR | S_IRUGO, show_in_max,
1008 static SENSOR_DEVICE_ATTR(in6_max, S_IWUSR | S_IRUGO, show_in_max,
1010 static SENSOR_DEVICE_ATTR(in7_max, S_IWUSR | S_IRUGO, show_in_max,
1012 static SENSOR_DEVICE_ATTR(in8_max, S_IWUSR | S_IRUGO, show_in_max,
1014 static SENSOR_DEVICE_ATTR(in9_max, S_IWUSR | S_IRUGO, show_in_max,
1016 static SENSOR_DEVICE_ATTR(in10_max, S_IWUSR | S_IRUGO, show_in_max,
1018 static SENSOR_DEVICE_ATTR(in11_max, S_IWUSR | S_IRUGO, show_in_max,
1020 static SENSOR_DEVICE_ATTR(in12_max, S_IWUSR | S_IRUGO, show_in_max,
1022 static SENSOR_DEVICE_ATTR(in13_max, S_IWUSR | S_IRUGO, show_in_max,
1024 static SENSOR_DEVICE_ATTR(in14_max, S_IWUSR | S_IRUGO, show_in_max,
1027 static struct attribute *nct6775_attributes_in[15][5] = {
1029 &sensor_dev_attr_in0_input.dev_attr.attr,
1030 &sensor_dev_attr_in0_min.dev_attr.attr,
1031 &sensor_dev_attr_in0_max.dev_attr.attr,
1032 &sensor_dev_attr_in0_alarm.dev_attr.attr,
1036 &sensor_dev_attr_in1_input.dev_attr.attr,
1037 &sensor_dev_attr_in1_min.dev_attr.attr,
1038 &sensor_dev_attr_in1_max.dev_attr.attr,
1039 &sensor_dev_attr_in1_alarm.dev_attr.attr,
1043 &sensor_dev_attr_in2_input.dev_attr.attr,
1044 &sensor_dev_attr_in2_min.dev_attr.attr,
1045 &sensor_dev_attr_in2_max.dev_attr.attr,
1046 &sensor_dev_attr_in2_alarm.dev_attr.attr,
1050 &sensor_dev_attr_in3_input.dev_attr.attr,
1051 &sensor_dev_attr_in3_min.dev_attr.attr,
1052 &sensor_dev_attr_in3_max.dev_attr.attr,
1053 &sensor_dev_attr_in3_alarm.dev_attr.attr,
1057 &sensor_dev_attr_in4_input.dev_attr.attr,
1058 &sensor_dev_attr_in4_min.dev_attr.attr,
1059 &sensor_dev_attr_in4_max.dev_attr.attr,
1060 &sensor_dev_attr_in4_alarm.dev_attr.attr,
1064 &sensor_dev_attr_in5_input.dev_attr.attr,
1065 &sensor_dev_attr_in5_min.dev_attr.attr,
1066 &sensor_dev_attr_in5_max.dev_attr.attr,
1067 &sensor_dev_attr_in5_alarm.dev_attr.attr,
1071 &sensor_dev_attr_in6_input.dev_attr.attr,
1072 &sensor_dev_attr_in6_min.dev_attr.attr,
1073 &sensor_dev_attr_in6_max.dev_attr.attr,
1074 &sensor_dev_attr_in6_alarm.dev_attr.attr,
1078 &sensor_dev_attr_in7_input.dev_attr.attr,
1079 &sensor_dev_attr_in7_min.dev_attr.attr,
1080 &sensor_dev_attr_in7_max.dev_attr.attr,
1081 &sensor_dev_attr_in7_alarm.dev_attr.attr,
1085 &sensor_dev_attr_in8_input.dev_attr.attr,
1086 &sensor_dev_attr_in8_min.dev_attr.attr,
1087 &sensor_dev_attr_in8_max.dev_attr.attr,
1088 &sensor_dev_attr_in8_alarm.dev_attr.attr,
1092 &sensor_dev_attr_in9_input.dev_attr.attr,
1093 &sensor_dev_attr_in9_min.dev_attr.attr,
1094 &sensor_dev_attr_in9_max.dev_attr.attr,
1095 &sensor_dev_attr_in9_alarm.dev_attr.attr,
1099 &sensor_dev_attr_in10_input.dev_attr.attr,
1100 &sensor_dev_attr_in10_min.dev_attr.attr,
1101 &sensor_dev_attr_in10_max.dev_attr.attr,
1102 &sensor_dev_attr_in10_alarm.dev_attr.attr,
1106 &sensor_dev_attr_in11_input.dev_attr.attr,
1107 &sensor_dev_attr_in11_min.dev_attr.attr,
1108 &sensor_dev_attr_in11_max.dev_attr.attr,
1109 &sensor_dev_attr_in11_alarm.dev_attr.attr,
1113 &sensor_dev_attr_in12_input.dev_attr.attr,
1114 &sensor_dev_attr_in12_min.dev_attr.attr,
1115 &sensor_dev_attr_in12_max.dev_attr.attr,
1116 &sensor_dev_attr_in12_alarm.dev_attr.attr,
1120 &sensor_dev_attr_in13_input.dev_attr.attr,
1121 &sensor_dev_attr_in13_min.dev_attr.attr,
1122 &sensor_dev_attr_in13_max.dev_attr.attr,
1123 &sensor_dev_attr_in13_alarm.dev_attr.attr,
1127 &sensor_dev_attr_in14_input.dev_attr.attr,
1128 &sensor_dev_attr_in14_min.dev_attr.attr,
1129 &sensor_dev_attr_in14_max.dev_attr.attr,
1130 &sensor_dev_attr_in14_alarm.dev_attr.attr,
1135 static const struct attribute_group nct6775_group_in[15] = {
1136 { .attrs = nct6775_attributes_in[0] },
1137 { .attrs = nct6775_attributes_in[1] },
1138 { .attrs = nct6775_attributes_in[2] },
1139 { .attrs = nct6775_attributes_in[3] },
1140 { .attrs = nct6775_attributes_in[4] },
1141 { .attrs = nct6775_attributes_in[5] },
1142 { .attrs = nct6775_attributes_in[6] },
1143 { .attrs = nct6775_attributes_in[7] },
1144 { .attrs = nct6775_attributes_in[8] },
1145 { .attrs = nct6775_attributes_in[9] },
1146 { .attrs = nct6775_attributes_in[10] },
1147 { .attrs = nct6775_attributes_in[11] },
1148 { .attrs = nct6775_attributes_in[12] },
1149 { .attrs = nct6775_attributes_in[13] },
1150 { .attrs = nct6775_attributes_in[14] },
1154 show_fan(struct device *dev, struct device_attribute *attr, char *buf)
1156 struct nct6775_data *data = nct6775_update_device(dev);
1157 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
1158 int nr = sensor_attr->index;
1159 return sprintf(buf, "%d\n", data->rpm[nr]);
1163 show_fan_min(struct device *dev, struct device_attribute *attr, char *buf)
1165 struct nct6775_data *data = nct6775_update_device(dev);
1166 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
1167 int nr = sensor_attr->index;
1168 return sprintf(buf, "%d\n",
1169 data->fan_from_reg_min(data->fan_min[nr],
1170 data->fan_div[nr]));
1174 show_fan_div(struct device *dev, struct device_attribute *attr,
1177 struct nct6775_data *data = nct6775_update_device(dev);
1178 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
1179 int nr = sensor_attr->index;
1180 return sprintf(buf, "%u\n", div_from_reg(data->fan_div[nr]));
1184 store_fan_min(struct device *dev, struct device_attribute *attr,
1185 const char *buf, size_t count)
1187 struct nct6775_data *data = dev_get_drvdata(dev);
1188 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
1189 int nr = sensor_attr->index;
1195 err = kstrtoul(buf, 10, &val);
1199 mutex_lock(&data->update_lock);
1200 if (!data->has_fan_div) {
1202 * Only NCT6776F for now, so we know that this is a 13 bit
1210 val = 1350000U / val;
1211 val = (val & 0x1f) | ((val << 3) & 0xff00);
1213 data->fan_min[nr] = val;
1214 goto write_min; /* Leave fan divider alone */
1217 /* No min limit, alarm disabled */
1218 data->fan_min[nr] = 255;
1219 new_div = data->fan_div[nr]; /* No change */
1220 dev_info(dev, "fan%u low limit and alarm disabled\n", nr + 1);
1223 reg = 1350000U / val;
1224 if (reg >= 128 * 255) {
1226 * Speed below this value cannot possibly be represented,
1227 * even with the highest divider (128)
1229 data->fan_min[nr] = 254;
1230 new_div = 7; /* 128 == (1 << 7) */
1231 dev_warn(dev, "fan%u low limit %lu below minimum %u, set to "
1232 "minimum\n", nr + 1, val,
1233 data->fan_from_reg_min(254, 7));
1236 * Speed above this value cannot possibly be represented,
1237 * even with the lowest divider (1)
1239 data->fan_min[nr] = 1;
1240 new_div = 0; /* 1 == (1 << 0) */
1241 dev_warn(dev, "fan%u low limit %lu above maximum %u, set to "
1242 "maximum\n", nr + 1, val,
1243 data->fan_from_reg_min(1, 0));
1246 * Automatically pick the best divider, i.e. the one such
1247 * that the min limit will correspond to a register value
1248 * in the 96..192 range
1251 while (reg > 192 && new_div < 7) {
1255 data->fan_min[nr] = reg;
1260 * Write both the fan clock divider (if it changed) and the new
1261 * fan min (unconditionally)
1263 if (new_div != data->fan_div[nr]) {
1264 dev_dbg(dev, "fan%u clock divider changed from %u to %u\n",
1265 nr + 1, div_from_reg(data->fan_div[nr]),
1266 div_from_reg(new_div));
1267 data->fan_div[nr] = new_div;
1268 nct6775_write_fan_div_common(dev, data, nr);
1269 /* Give the chip time to sample a new speed value */
1270 data->last_updated = jiffies;
1273 nct6775_write_value(data, data->REG_FAN_MIN[nr],
1275 mutex_unlock(&data->update_lock);
1280 static struct sensor_device_attribute sda_fan_input[] = {
1281 SENSOR_ATTR(fan1_input, S_IRUGO, show_fan, NULL, 0),
1282 SENSOR_ATTR(fan2_input, S_IRUGO, show_fan, NULL, 1),
1283 SENSOR_ATTR(fan3_input, S_IRUGO, show_fan, NULL, 2),
1284 SENSOR_ATTR(fan4_input, S_IRUGO, show_fan, NULL, 3),
1285 SENSOR_ATTR(fan5_input, S_IRUGO, show_fan, NULL, 4),
1288 static struct sensor_device_attribute sda_fan_alarm[] = {
1289 SENSOR_ATTR(fan1_alarm, S_IRUGO, show_alarm, NULL, 6),
1290 SENSOR_ATTR(fan2_alarm, S_IRUGO, show_alarm, NULL, 7),
1291 SENSOR_ATTR(fan3_alarm, S_IRUGO, show_alarm, NULL, 11),
1292 SENSOR_ATTR(fan4_alarm, S_IRUGO, show_alarm, NULL, 10),
1293 SENSOR_ATTR(fan5_alarm, S_IRUGO, show_alarm, NULL, 23),
1296 static struct sensor_device_attribute sda_fan_min[] = {
1297 SENSOR_ATTR(fan1_min, S_IWUSR | S_IRUGO, show_fan_min,
1299 SENSOR_ATTR(fan2_min, S_IWUSR | S_IRUGO, show_fan_min,
1301 SENSOR_ATTR(fan3_min, S_IWUSR | S_IRUGO, show_fan_min,
1303 SENSOR_ATTR(fan4_min, S_IWUSR | S_IRUGO, show_fan_min,
1305 SENSOR_ATTR(fan5_min, S_IWUSR | S_IRUGO, show_fan_min,
1309 static struct sensor_device_attribute sda_fan_div[] = {
1310 SENSOR_ATTR(fan1_div, S_IRUGO, show_fan_div, NULL, 0),
1311 SENSOR_ATTR(fan2_div, S_IRUGO, show_fan_div, NULL, 1),
1312 SENSOR_ATTR(fan3_div, S_IRUGO, show_fan_div, NULL, 2),
1313 SENSOR_ATTR(fan4_div, S_IRUGO, show_fan_div, NULL, 3),
1314 SENSOR_ATTR(fan5_div, S_IRUGO, show_fan_div, NULL, 4),
1318 show_temp_label(struct device *dev, struct device_attribute *attr, char *buf)
1320 struct nct6775_data *data = nct6775_update_device(dev);
1321 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
1322 int nr = sensor_attr->index;
1323 return sprintf(buf, "%s\n", data->temp_label[data->temp_src[nr]]);
1326 #define show_temp_reg(addr, reg) \
1328 show_##reg(struct device *dev, struct device_attribute *attr, \
1331 struct nct6775_data *data = nct6775_update_device(dev); \
1332 struct sensor_device_attribute *sensor_attr = \
1333 to_sensor_dev_attr(attr); \
1334 int nr = sensor_attr->index; \
1335 return sprintf(buf, "%d\n", LM75_TEMP_FROM_REG(data->reg[nr])); \
1337 show_temp_reg(reg_temp, temp);
1338 show_temp_reg(reg_temp_over, temp_max);
1339 show_temp_reg(reg_temp_hyst, temp_max_hyst);
1341 #define store_temp_reg(addr, reg) \
1343 store_##reg(struct device *dev, struct device_attribute *attr, \
1344 const char *buf, size_t count) \
1346 struct nct6775_data *data = dev_get_drvdata(dev); \
1347 struct sensor_device_attribute *sensor_attr = \
1348 to_sensor_dev_attr(attr); \
1349 int nr = sensor_attr->index; \
1352 err = kstrtol(buf, 10, &val); \
1355 mutex_lock(&data->update_lock); \
1356 data->reg[nr] = LM75_TEMP_TO_REG(val); \
1357 nct6775_write_temp(data, data->addr[nr], data->reg[nr]); \
1358 mutex_unlock(&data->update_lock); \
1361 store_temp_reg(reg_temp_over, temp_max);
1362 store_temp_reg(reg_temp_hyst, temp_max_hyst);
1365 show_temp_type(struct device *dev, struct device_attribute *attr, char *buf)
1367 struct nct6775_data *data = nct6775_update_device(dev);
1368 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
1369 int nr = sensor_attr->index;
1370 return sprintf(buf, "%d\n", (int)data->temp_type[nr]);
1373 static struct sensor_device_attribute sda_temp_input[] = {
1374 SENSOR_ATTR(temp1_input, S_IRUGO, show_temp, NULL, 0),
1375 SENSOR_ATTR(temp2_input, S_IRUGO, show_temp, NULL, 1),
1376 SENSOR_ATTR(temp3_input, S_IRUGO, show_temp, NULL, 2),
1377 SENSOR_ATTR(temp4_input, S_IRUGO, show_temp, NULL, 3),
1378 SENSOR_ATTR(temp5_input, S_IRUGO, show_temp, NULL, 4),
1379 SENSOR_ATTR(temp6_input, S_IRUGO, show_temp, NULL, 5),
1380 SENSOR_ATTR(temp7_input, S_IRUGO, show_temp, NULL, 6),
1381 SENSOR_ATTR(temp8_input, S_IRUGO, show_temp, NULL, 7),
1382 SENSOR_ATTR(temp9_input, S_IRUGO, show_temp, NULL, 8),
1385 static struct sensor_device_attribute sda_temp_label[] = {
1386 SENSOR_ATTR(temp1_label, S_IRUGO, show_temp_label, NULL, 0),
1387 SENSOR_ATTR(temp2_label, S_IRUGO, show_temp_label, NULL, 1),
1388 SENSOR_ATTR(temp3_label, S_IRUGO, show_temp_label, NULL, 2),
1389 SENSOR_ATTR(temp4_label, S_IRUGO, show_temp_label, NULL, 3),
1390 SENSOR_ATTR(temp5_label, S_IRUGO, show_temp_label, NULL, 4),
1391 SENSOR_ATTR(temp6_label, S_IRUGO, show_temp_label, NULL, 5),
1392 SENSOR_ATTR(temp7_label, S_IRUGO, show_temp_label, NULL, 6),
1393 SENSOR_ATTR(temp8_label, S_IRUGO, show_temp_label, NULL, 7),
1394 SENSOR_ATTR(temp9_label, S_IRUGO, show_temp_label, NULL, 8),
1397 static struct sensor_device_attribute sda_temp_max[] = {
1398 SENSOR_ATTR(temp1_max, S_IRUGO | S_IWUSR, show_temp_max,
1400 SENSOR_ATTR(temp2_max, S_IRUGO | S_IWUSR, show_temp_max,
1402 SENSOR_ATTR(temp3_max, S_IRUGO | S_IWUSR, show_temp_max,
1404 SENSOR_ATTR(temp4_max, S_IRUGO | S_IWUSR, show_temp_max,
1406 SENSOR_ATTR(temp5_max, S_IRUGO | S_IWUSR, show_temp_max,
1408 SENSOR_ATTR(temp6_max, S_IRUGO | S_IWUSR, show_temp_max,
1410 SENSOR_ATTR(temp7_max, S_IRUGO | S_IWUSR, show_temp_max,
1412 SENSOR_ATTR(temp8_max, S_IRUGO | S_IWUSR, show_temp_max,
1414 SENSOR_ATTR(temp9_max, S_IRUGO | S_IWUSR, show_temp_max,
1418 static struct sensor_device_attribute sda_temp_max_hyst[] = {
1419 SENSOR_ATTR(temp1_max_hyst, S_IRUGO | S_IWUSR, show_temp_max_hyst,
1420 store_temp_max_hyst, 0),
1421 SENSOR_ATTR(temp2_max_hyst, S_IRUGO | S_IWUSR, show_temp_max_hyst,
1422 store_temp_max_hyst, 1),
1423 SENSOR_ATTR(temp3_max_hyst, S_IRUGO | S_IWUSR, show_temp_max_hyst,
1424 store_temp_max_hyst, 2),
1425 SENSOR_ATTR(temp4_max_hyst, S_IRUGO | S_IWUSR, show_temp_max_hyst,
1426 store_temp_max_hyst, 3),
1427 SENSOR_ATTR(temp5_max_hyst, S_IRUGO | S_IWUSR, show_temp_max_hyst,
1428 store_temp_max_hyst, 4),
1429 SENSOR_ATTR(temp6_max_hyst, S_IRUGO | S_IWUSR, show_temp_max_hyst,
1430 store_temp_max_hyst, 5),
1431 SENSOR_ATTR(temp7_max_hyst, S_IRUGO | S_IWUSR, show_temp_max_hyst,
1432 store_temp_max_hyst, 6),
1433 SENSOR_ATTR(temp8_max_hyst, S_IRUGO | S_IWUSR, show_temp_max_hyst,
1434 store_temp_max_hyst, 7),
1435 SENSOR_ATTR(temp9_max_hyst, S_IRUGO | S_IWUSR, show_temp_max_hyst,
1436 store_temp_max_hyst, 8),
1439 static struct sensor_device_attribute sda_temp_alarm[] = {
1440 SENSOR_ATTR(temp1_alarm, S_IRUGO, show_alarm, NULL, 4),
1441 SENSOR_ATTR(temp2_alarm, S_IRUGO, show_alarm, NULL, 5),
1442 SENSOR_ATTR(temp3_alarm, S_IRUGO, show_alarm, NULL, 13),
1445 static struct sensor_device_attribute sda_temp_type[] = {
1446 SENSOR_ATTR(temp1_type, S_IRUGO, show_temp_type, NULL, 0),
1447 SENSOR_ATTR(temp2_type, S_IRUGO, show_temp_type, NULL, 1),
1448 SENSOR_ATTR(temp3_type, S_IRUGO, show_temp_type, NULL, 2),
1451 #define show_pwm_reg(reg) \
1452 static ssize_t show_##reg(struct device *dev, struct device_attribute *attr, \
1455 struct nct6775_data *data = nct6775_update_device(dev); \
1456 struct sensor_device_attribute *sensor_attr = \
1457 to_sensor_dev_attr(attr); \
1458 int nr = sensor_attr->index; \
1459 return sprintf(buf, "%d\n", data->reg[nr]); \
1462 show_pwm_reg(pwm_mode)
1463 show_pwm_reg(pwm_enable)
1465 static ssize_t show_pwm(struct device *dev, struct device_attribute *attr,
1468 struct nct6775_data *data = nct6775_update_device(dev);
1469 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
1470 int nr = sensor_attr->index;
1473 * For automatic fan control modes, show current pwm readings.
1474 * Otherwise, show the configured value.
1476 return sprintf(buf, "%d\n",
1477 data->pwm_mode[nr] > 1 ?
1479 nct6775_read_value(data,
1480 NCT6775_REG_PWM_READ[nr]));
1484 store_pwm_mode(struct device *dev, struct device_attribute *attr,
1485 const char *buf, size_t count)
1487 struct nct6775_data *data = dev_get_drvdata(dev);
1488 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
1489 struct nct6775_sio_data *sio_data = dev->platform_data;
1490 int nr = sensor_attr->index;
1495 err = kstrtoul(buf, 10, &val);
1502 /* On NCT6776F and NCT6779D, DC mode is only supported for pwm1 */
1503 if ((sio_data->kind == nct6776 || sio_data->kind == nct6779) && nr) {
1509 mutex_lock(&data->update_lock);
1510 reg = nct6775_read_value(data, NCT6775_REG_PWM_ENABLE[nr]);
1511 data->pwm_mode[nr] = val;
1512 reg &= ~(1 << NCT6775_PWM_MODE_SHIFT[nr]);
1514 reg |= 1 << NCT6775_PWM_MODE_SHIFT[nr];
1515 nct6775_write_value(data, NCT6775_REG_PWM_ENABLE[nr], reg);
1516 mutex_unlock(&data->update_lock);
1521 store_pwm(struct device *dev, struct device_attribute *attr,
1522 const char *buf, size_t count)
1524 struct nct6775_data *data = dev_get_drvdata(dev);
1525 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
1526 int nr = sensor_attr->index;
1530 err = kstrtoul(buf, 10, &val);
1534 val = SENSORS_LIMIT(val, 0, 255);
1536 mutex_lock(&data->update_lock);
1537 data->pwm[nr] = val;
1538 nct6775_write_value(data, NCT6775_REG_PWM[nr], val);
1539 mutex_unlock(&data->update_lock);
1543 /* Returns 0 if OK, -EINVAL otherwise */
1544 static int check_trip_points(struct nct6775_data *data, int nr)
1548 for (i = 0; i < data->auto_pwm_num - 1; i++) {
1549 if (data->auto_temp[nr][i] > data->auto_temp[nr][i + 1])
1552 for (i = 0; i < data->auto_pwm_num - 1; i++) {
1553 if (data->auto_pwm[nr][i] > data->auto_pwm[nr][i + 1])
1556 /* validate critical temperature and pwm if enabled (pwm > 0) */
1557 if (data->auto_pwm[nr][data->auto_pwm_num]) {
1558 if (data->auto_temp[nr][data->auto_pwm_num - 1] >
1559 data->auto_temp[nr][data->auto_pwm_num] ||
1560 data->auto_pwm[nr][data->auto_pwm_num - 1] >
1561 data->auto_pwm[nr][data->auto_pwm_num])
1568 store_pwm_enable(struct device *dev, struct device_attribute *attr,
1569 const char *buf, size_t count)
1571 struct nct6775_data *data = dev_get_drvdata(dev);
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 if (val == 4 && check_trip_points(data, nr)) {
1586 dev_err(dev, "Inconsistent trip points, not switching to SmartFan IV mode\n");
1587 dev_err(dev, "Adjust trip points and try again\n");
1591 mutex_lock(&data->update_lock);
1592 data->pwm_enable[nr] = val;
1595 * turn off pwm control: select manual mode, set pwm to maximum
1597 data->pwm[nr] = 255;
1598 nct6775_write_value(data, NCT6775_REG_PWM[nr], 255);
1600 reg = nct6775_read_value(data, NCT6775_REG_FAN_MODE[nr]);
1602 reg |= (pwm_enable_to_reg(val) << 4);
1603 nct6775_write_value(data, NCT6775_REG_FAN_MODE[nr], reg);
1604 mutex_unlock(&data->update_lock);
1609 static ssize_t show_target_temp(struct device *dev,
1610 struct device_attribute *attr, char *buf)
1612 struct nct6775_data *data = nct6775_update_device(dev);
1613 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
1615 return sprintf(buf, "%d\n",
1616 data->target_temp[sensor_attr->index] * 1000);
1620 store_target_temp(struct device *dev, struct device_attribute *attr,
1621 const char *buf, size_t count)
1623 struct nct6775_data *data = dev_get_drvdata(dev);
1624 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
1625 int nr = sensor_attr->index;
1629 err = kstrtol(buf, 10, &val);
1633 val = SENSORS_LIMIT(DIV_ROUND_CLOSEST(val, 1000), 0, 127);
1635 mutex_lock(&data->update_lock);
1636 data->target_temp[nr] = val;
1637 nct6775_write_value(data, data->REG_TARGET[nr], val);
1638 mutex_unlock(&data->update_lock);
1642 static ssize_t show_auto_temp_hyst(struct device *dev,
1643 struct device_attribute *attr, char *buf)
1645 struct nct6775_data *data = nct6775_update_device(dev);
1646 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
1648 int point = sattr->index >= data->auto_pwm_num ? 1 : 0;
1649 int tolerance = data->tolerance[nr][point];
1650 int temp = data->auto_temp[nr][sattr->index];
1652 return sprintf(buf, "%d\n", (temp - tolerance) * 1000);
1656 store_auto_temp_hyst(struct device *dev, struct device_attribute *attr,
1657 const char *buf, size_t count)
1659 struct nct6775_data *data = dev_get_drvdata(dev);
1660 struct nct6775_sio_data *sio_data = dev->platform_data;
1661 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
1663 int point = sattr->index >= data->auto_pwm_num ? 1 : 0;
1667 int maxlimit[2][3] = { { 15, 7, 63 }, { 15, 7, 7 } };
1668 int mask[] = { 0x0f, 0x07, 0x07 };
1671 err = kstrtol(buf, 10, &val);
1675 temp = data->auto_temp[nr][sattr->index];
1676 val = temp - DIV_ROUND_CLOSEST(val, 1000);
1678 /* Limit tolerance as needed */
1679 val = SENSORS_LIMIT(val, 0, maxlimit[point][sio_data->kind]);
1681 mutex_lock(&data->update_lock);
1683 nct6775_write_value(data,
1684 NCT6775_REG_CRITICAL_TEMP_TOLERANCE[nr],
1687 reg = nct6775_read_value(data, NCT6775_REG_FAN_MODE[nr]);
1688 reg = (reg & ~mask[nr]) | (val & mask[nr]);
1689 nct6775_write_value(data, NCT6775_REG_FAN_MODE[nr], reg);
1690 if (sio_data->kind == nct6779) {
1691 reg = nct6775_read_value(data,
1692 NCT6779_REG_TOLERANCE_H[nr]);
1693 reg = (reg & 0x70) | ((val & 0x38) << 1);
1694 nct6775_write_value(data,
1695 NCT6779_REG_TOLERANCE_H[nr], reg);
1699 data->tolerance[nr][point] = val;
1700 mutex_unlock(&data->update_lock);
1704 static SENSOR_DEVICE_ATTR(pwm1, S_IWUSR | S_IRUGO, show_pwm, store_pwm, 0);
1705 static SENSOR_DEVICE_ATTR(pwm2, S_IWUSR | S_IRUGO, show_pwm, store_pwm, 1);
1706 static SENSOR_DEVICE_ATTR(pwm3, S_IWUSR | S_IRUGO, show_pwm, store_pwm, 2);
1707 static SENSOR_DEVICE_ATTR(pwm4, S_IWUSR | S_IRUGO, show_pwm, store_pwm, 3);
1708 static SENSOR_DEVICE_ATTR(pwm5, S_IWUSR | S_IRUGO, show_pwm, store_pwm, 4);
1710 static SENSOR_DEVICE_ATTR(pwm1_mode, S_IWUSR | S_IRUGO, show_pwm_mode,
1712 static SENSOR_DEVICE_ATTR(pwm2_mode, S_IWUSR | S_IRUGO, show_pwm_mode,
1714 static SENSOR_DEVICE_ATTR(pwm3_mode, S_IWUSR | S_IRUGO, show_pwm_mode,
1716 static SENSOR_DEVICE_ATTR(pwm4_mode, S_IWUSR | S_IRUGO, show_pwm_mode,
1718 static SENSOR_DEVICE_ATTR(pwm5_mode, S_IWUSR | S_IRUGO, show_pwm_mode,
1721 static SENSOR_DEVICE_ATTR(pwm1_enable, S_IWUSR | S_IRUGO, show_pwm_enable,
1722 store_pwm_enable, 0);
1723 static SENSOR_DEVICE_ATTR(pwm2_enable, S_IWUSR | S_IRUGO, show_pwm_enable,
1724 store_pwm_enable, 1);
1725 static SENSOR_DEVICE_ATTR(pwm3_enable, S_IWUSR | S_IRUGO, show_pwm_enable,
1726 store_pwm_enable, 2);
1727 static SENSOR_DEVICE_ATTR(pwm4_enable, S_IWUSR | S_IRUGO, show_pwm_enable,
1728 store_pwm_enable, 3);
1729 static SENSOR_DEVICE_ATTR(pwm5_enable, S_IWUSR | S_IRUGO, show_pwm_enable,
1730 store_pwm_enable, 4);
1732 static SENSOR_DEVICE_ATTR(pwm1_target, S_IWUSR | S_IRUGO, show_target_temp,
1733 store_target_temp, 0);
1734 static SENSOR_DEVICE_ATTR(pwm2_target, S_IWUSR | S_IRUGO, show_target_temp,
1735 store_target_temp, 1);
1736 static SENSOR_DEVICE_ATTR(pwm3_target, S_IWUSR | S_IRUGO, show_target_temp,
1737 store_target_temp, 2);
1738 static SENSOR_DEVICE_ATTR(pwm4_target, S_IWUSR | S_IRUGO, show_target_temp,
1739 store_target_temp, 3);
1740 static SENSOR_DEVICE_ATTR(pwm5_target, S_IWUSR | S_IRUGO, show_target_temp,
1741 store_target_temp, 4);
1743 /* Smart Fan registers */
1745 #define fan_functions(reg, REG) \
1746 static ssize_t show_##reg(struct device *dev, struct device_attribute *attr, \
1749 struct nct6775_data *data = nct6775_update_device(dev); \
1750 struct sensor_device_attribute *sensor_attr = \
1751 to_sensor_dev_attr(attr); \
1752 int nr = sensor_attr->index; \
1753 return sprintf(buf, "%d\n", data->reg[nr]); \
1756 store_##reg(struct device *dev, struct device_attribute *attr, \
1757 const char *buf, size_t count) \
1759 struct nct6775_data *data = dev_get_drvdata(dev); \
1760 struct sensor_device_attribute *sensor_attr = \
1761 to_sensor_dev_attr(attr); \
1762 int nr = sensor_attr->index; \
1763 unsigned long val; \
1765 err = kstrtoul(buf, 10, &val); \
1768 val = SENSORS_LIMIT(val, 1, 255); \
1769 mutex_lock(&data->update_lock); \
1770 data->reg[nr] = val; \
1771 nct6775_write_value(data, data->REG_##REG[nr], val); \
1772 mutex_unlock(&data->update_lock); \
1776 fan_functions(fan_start_output, FAN_START_OUTPUT)
1777 fan_functions(fan_stop_output, FAN_STOP_OUTPUT)
1779 #define fan_step_functions(reg, REG) \
1780 static ssize_t show_##reg(struct device *dev, \
1781 struct device_attribute *attr, char *buf) \
1783 struct nct6775_data *data = nct6775_update_device(dev); \
1784 struct sensor_device_attribute *sensor_attr = \
1785 to_sensor_dev_attr(attr); \
1786 int nr = sensor_attr->index; \
1787 return sprintf(buf, "%d\n", \
1788 step_time_from_reg(data->reg[nr], \
1789 data->pwm_mode[nr])); \
1791 static ssize_t store_##reg(struct device *dev, \
1792 struct device_attribute *attr, \
1793 const char *buf, size_t count) \
1795 struct nct6775_data *data = dev_get_drvdata(dev); \
1796 struct sensor_device_attribute *sensor_attr = \
1797 to_sensor_dev_attr(attr); \
1798 int nr = sensor_attr->index; \
1799 unsigned long val; \
1801 err = kstrtoul(buf, 10, &val); \
1804 val = step_time_to_reg(val, data->pwm_mode[nr]); \
1805 mutex_lock(&data->update_lock); \
1806 data->reg[nr] = val; \
1807 nct6775_write_value(data, NCT6775_REG_##REG[nr], val); \
1808 mutex_unlock(&data->update_lock); \
1812 fan_step_functions(fan_stop_time, FAN_STOP_TIME)
1813 fan_step_functions(fan_step_up_time, FAN_STEP_UP_TIME)
1814 fan_step_functions(fan_step_down_time, FAN_STEP_DOWN_TIME)
1816 static ssize_t show_name(struct device *dev, struct device_attribute *attr,
1819 struct nct6775_data *data = dev_get_drvdata(dev);
1821 return sprintf(buf, "%s\n", data->name);
1823 static DEVICE_ATTR(name, S_IRUGO, show_name, NULL);
1825 static SENSOR_DEVICE_ATTR(pwm1_stop_time, S_IWUSR | S_IRUGO, show_fan_stop_time,
1826 store_fan_stop_time, 0);
1827 static SENSOR_DEVICE_ATTR(pwm2_stop_time, S_IWUSR | S_IRUGO, show_fan_stop_time,
1828 store_fan_stop_time, 1);
1829 static SENSOR_DEVICE_ATTR(pwm3_stop_time, S_IWUSR | S_IRUGO, show_fan_stop_time,
1830 store_fan_stop_time, 2);
1831 static SENSOR_DEVICE_ATTR(pwm4_stop_time, S_IWUSR | S_IRUGO, show_fan_stop_time,
1832 store_fan_stop_time, 3);
1833 static SENSOR_DEVICE_ATTR(pwm5_stop_time, S_IWUSR | S_IRUGO, show_fan_stop_time,
1834 store_fan_stop_time, 4);
1836 static SENSOR_DEVICE_ATTR(pwm1_step_up_time, S_IWUSR | S_IRUGO,
1837 show_fan_step_up_time, store_fan_step_up_time, 0);
1838 static SENSOR_DEVICE_ATTR(pwm2_step_up_time, S_IWUSR | S_IRUGO,
1839 show_fan_step_up_time, store_fan_step_up_time, 1);
1840 static SENSOR_DEVICE_ATTR(pwm3_step_up_time, S_IWUSR | S_IRUGO,
1841 show_fan_step_up_time, store_fan_step_up_time, 2);
1842 static SENSOR_DEVICE_ATTR(pwm4_step_up_time, S_IWUSR | S_IRUGO,
1843 show_fan_step_up_time, store_fan_step_up_time, 3);
1844 static SENSOR_DEVICE_ATTR(pwm5_step_up_time, S_IWUSR | S_IRUGO,
1845 show_fan_step_up_time, store_fan_step_up_time, 4);
1847 static SENSOR_DEVICE_ATTR(pwm1_step_down_time, S_IWUSR | S_IRUGO,
1848 show_fan_step_down_time, store_fan_step_down_time, 0);
1849 static SENSOR_DEVICE_ATTR(pwm2_step_down_time, S_IWUSR | S_IRUGO,
1850 show_fan_step_down_time, store_fan_step_down_time, 1);
1851 static SENSOR_DEVICE_ATTR(pwm3_step_down_time, S_IWUSR | S_IRUGO,
1852 show_fan_step_down_time, store_fan_step_down_time, 2);
1853 static SENSOR_DEVICE_ATTR(pwm4_step_down_time, S_IWUSR | S_IRUGO,
1854 show_fan_step_down_time, store_fan_step_down_time, 3);
1855 static SENSOR_DEVICE_ATTR(pwm5_step_down_time, S_IWUSR | S_IRUGO,
1856 show_fan_step_down_time, store_fan_step_down_time, 4);
1858 static SENSOR_DEVICE_ATTR(pwm1_start_output, S_IWUSR | S_IRUGO,
1859 show_fan_start_output, store_fan_start_output, 0);
1860 static SENSOR_DEVICE_ATTR(pwm2_start_output, S_IWUSR | S_IRUGO,
1861 show_fan_start_output, store_fan_start_output, 1);
1862 static SENSOR_DEVICE_ATTR(pwm3_start_output, S_IWUSR | S_IRUGO,
1863 show_fan_start_output, store_fan_start_output, 2);
1864 static SENSOR_DEVICE_ATTR(pwm4_start_output, S_IWUSR | S_IRUGO,
1865 show_fan_start_output, store_fan_start_output, 3);
1866 static SENSOR_DEVICE_ATTR(pwm5_start_output, S_IWUSR | S_IRUGO,
1867 show_fan_start_output, store_fan_start_output, 4);
1869 static SENSOR_DEVICE_ATTR(pwm1_stop_output, S_IWUSR | S_IRUGO,
1870 show_fan_stop_output, store_fan_stop_output, 0);
1871 static SENSOR_DEVICE_ATTR(pwm2_stop_output, S_IWUSR | S_IRUGO,
1872 show_fan_stop_output, store_fan_stop_output, 1);
1873 static SENSOR_DEVICE_ATTR(pwm3_stop_output, S_IWUSR | S_IRUGO,
1874 show_fan_stop_output, store_fan_stop_output, 2);
1875 static SENSOR_DEVICE_ATTR(pwm4_stop_output, S_IWUSR | S_IRUGO,
1876 show_fan_stop_output, store_fan_stop_output, 3);
1877 static SENSOR_DEVICE_ATTR(pwm5_stop_output, S_IWUSR | S_IRUGO,
1878 show_fan_stop_output, store_fan_stop_output, 4);
1880 static struct attribute *nct6775_attributes_pwm[5][10] = {
1882 &sensor_dev_attr_pwm1.dev_attr.attr,
1883 &sensor_dev_attr_pwm1_mode.dev_attr.attr,
1884 &sensor_dev_attr_pwm1_enable.dev_attr.attr,
1885 &sensor_dev_attr_pwm1_target.dev_attr.attr,
1886 &sensor_dev_attr_pwm1_stop_time.dev_attr.attr,
1887 &sensor_dev_attr_pwm1_step_up_time.dev_attr.attr,
1888 &sensor_dev_attr_pwm1_step_down_time.dev_attr.attr,
1889 &sensor_dev_attr_pwm1_start_output.dev_attr.attr,
1890 &sensor_dev_attr_pwm1_stop_output.dev_attr.attr,
1894 &sensor_dev_attr_pwm2.dev_attr.attr,
1895 &sensor_dev_attr_pwm2_mode.dev_attr.attr,
1896 &sensor_dev_attr_pwm2_enable.dev_attr.attr,
1897 &sensor_dev_attr_pwm2_target.dev_attr.attr,
1898 &sensor_dev_attr_pwm2_stop_time.dev_attr.attr,
1899 &sensor_dev_attr_pwm2_step_up_time.dev_attr.attr,
1900 &sensor_dev_attr_pwm2_step_down_time.dev_attr.attr,
1901 &sensor_dev_attr_pwm2_start_output.dev_attr.attr,
1902 &sensor_dev_attr_pwm2_stop_output.dev_attr.attr,
1906 &sensor_dev_attr_pwm3.dev_attr.attr,
1907 &sensor_dev_attr_pwm3_mode.dev_attr.attr,
1908 &sensor_dev_attr_pwm3_enable.dev_attr.attr,
1909 &sensor_dev_attr_pwm3_target.dev_attr.attr,
1910 &sensor_dev_attr_pwm3_stop_time.dev_attr.attr,
1911 &sensor_dev_attr_pwm3_step_up_time.dev_attr.attr,
1912 &sensor_dev_attr_pwm3_step_down_time.dev_attr.attr,
1913 &sensor_dev_attr_pwm3_start_output.dev_attr.attr,
1914 &sensor_dev_attr_pwm3_stop_output.dev_attr.attr,
1918 &sensor_dev_attr_pwm4.dev_attr.attr,
1919 &sensor_dev_attr_pwm4_mode.dev_attr.attr,
1920 &sensor_dev_attr_pwm4_enable.dev_attr.attr,
1921 &sensor_dev_attr_pwm4_target.dev_attr.attr,
1922 &sensor_dev_attr_pwm4_stop_time.dev_attr.attr,
1923 &sensor_dev_attr_pwm4_step_up_time.dev_attr.attr,
1924 &sensor_dev_attr_pwm4_step_down_time.dev_attr.attr,
1925 &sensor_dev_attr_pwm4_start_output.dev_attr.attr,
1926 &sensor_dev_attr_pwm4_stop_output.dev_attr.attr,
1930 &sensor_dev_attr_pwm5.dev_attr.attr,
1931 &sensor_dev_attr_pwm5_mode.dev_attr.attr,
1932 &sensor_dev_attr_pwm5_enable.dev_attr.attr,
1933 &sensor_dev_attr_pwm5_target.dev_attr.attr,
1934 &sensor_dev_attr_pwm5_stop_time.dev_attr.attr,
1935 &sensor_dev_attr_pwm5_step_up_time.dev_attr.attr,
1936 &sensor_dev_attr_pwm5_step_down_time.dev_attr.attr,
1937 &sensor_dev_attr_pwm5_start_output.dev_attr.attr,
1938 &sensor_dev_attr_pwm5_stop_output.dev_attr.attr,
1943 static const struct attribute_group nct6775_group_pwm[5] = {
1944 { .attrs = nct6775_attributes_pwm[0] },
1945 { .attrs = nct6775_attributes_pwm[1] },
1946 { .attrs = nct6775_attributes_pwm[2] },
1947 { .attrs = nct6775_attributes_pwm[3] },
1948 { .attrs = nct6775_attributes_pwm[4] },
1951 static ssize_t show_auto_pwm(struct device *dev, struct device_attribute *attr,
1954 struct nct6775_data *data = nct6775_update_device(dev);
1955 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
1957 return sprintf(buf, "%d\n", data->auto_pwm[sattr->nr][sattr->index]);
1960 static ssize_t store_auto_pwm(struct device *dev, struct device_attribute *attr,
1961 const char *buf, size_t count)
1963 struct nct6775_sio_data *sio_data = dev->platform_data;
1964 struct nct6775_data *data = dev_get_drvdata(dev);
1965 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
1967 int point = sattr->index;
1972 err = kstrtoul(buf, 10, &val);
1978 if (point == data->auto_pwm_num) {
1979 if (sio_data->kind != nct6775 && !val)
1981 if (sio_data->kind != nct6779 && val)
1985 mutex_lock(&data->update_lock);
1986 data->auto_pwm[nr][point] = val;
1987 if (point < data->auto_pwm_num) {
1988 nct6775_write_value(data, NCT6775_REG_AUTO_PWM(nr, point),
1989 data->auto_pwm[nr][point]);
1991 switch(sio_data->kind) {
1993 /* disable if needed (pwm == 0) */
1994 reg = nct6775_read_value(data,
1995 NCT6775_REG_CRITICAL_ENAB[nr]);
2000 nct6775_write_value(data, NCT6775_REG_CRITICAL_ENAB[nr],
2004 break; /* always enabled, nothing to do */
2006 nct6775_write_value(data, NCT6779_REG_CRITICAL_PWM[nr],
2008 reg = nct6775_read_value(data,
2009 NCT6779_REG_CRITICAL_PWM_ENABLE[nr]);
2014 nct6775_write_value(data,
2015 NCT6779_REG_CRITICAL_PWM_ENABLE[nr],
2020 mutex_unlock(&data->update_lock);
2024 static ssize_t show_auto_temp(struct device *dev, struct device_attribute *attr,
2027 struct nct6775_data *data = nct6775_update_device(dev);
2028 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
2030 int point = sattr->index;
2033 * We don't know for sure if the temperature is signed or unsigned.
2034 * Assume it is unsigned.
2036 return sprintf(buf, "%d\n", data->auto_temp[nr][point] * 1000);
2039 static ssize_t store_auto_temp(struct device *dev,
2040 struct device_attribute *attr,
2041 const char *buf, size_t count)
2043 struct nct6775_data *data = dev_get_drvdata(dev);
2044 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
2046 int point = sattr->index;
2050 err = kstrtoul(buf, 10, &val);
2056 mutex_lock(&data->update_lock);
2057 data->auto_temp[nr][point] = DIV_ROUND_CLOSEST(val, 1000);
2058 if (point < data->auto_pwm_num) {
2059 nct6775_write_value(data, NCT6775_REG_AUTO_TEMP(nr, point),
2060 data->auto_temp[nr][point]);
2062 nct6775_write_value(data, NCT6775_REG_CRITICAL_TEMP[nr],
2063 data->auto_temp[nr][point]);
2065 mutex_unlock(&data->update_lock);
2070 * The number of auto-point trip points is chip dependent.
2071 * Need to check support while generating/removing attribute files.
2073 static struct sensor_device_attribute_2 sda_auto_pwm_arrays[] = {
2074 SENSOR_ATTR_2(pwm1_auto_point1_pwm, S_IWUSR | S_IRUGO,
2075 show_auto_pwm, store_auto_pwm, 0, 0),
2076 SENSOR_ATTR_2(pwm1_auto_point1_temp, S_IWUSR | S_IRUGO,
2077 show_auto_temp, store_auto_temp, 0, 0),
2078 SENSOR_ATTR_2(pwm1_auto_point1_temp_hyst, S_IWUSR | S_IRUGO,
2079 show_auto_temp_hyst, store_auto_temp_hyst, 0, 0),
2080 SENSOR_ATTR_2(pwm1_auto_point2_pwm, S_IWUSR | S_IRUGO,
2081 show_auto_pwm, store_auto_pwm, 0, 1),
2082 SENSOR_ATTR_2(pwm1_auto_point2_temp, S_IWUSR | S_IRUGO,
2083 show_auto_temp, store_auto_temp, 0, 1),
2084 SENSOR_ATTR_2(pwm1_auto_point2_temp_hyst, S_IWUSR | S_IRUGO,
2085 show_auto_temp_hyst, store_auto_temp_hyst, 0, 1),
2086 SENSOR_ATTR_2(pwm1_auto_point3_pwm, S_IWUSR | S_IRUGO,
2087 show_auto_pwm, store_auto_pwm, 0, 2),
2088 SENSOR_ATTR_2(pwm1_auto_point3_temp, S_IWUSR | S_IRUGO,
2089 show_auto_temp, store_auto_temp, 0, 2),
2090 SENSOR_ATTR_2(pwm1_auto_point3_temp_hyst, S_IWUSR | S_IRUGO,
2091 show_auto_temp_hyst, store_auto_temp_hyst, 0, 2),
2092 SENSOR_ATTR_2(pwm1_auto_point4_pwm, S_IWUSR | S_IRUGO,
2093 show_auto_pwm, store_auto_pwm, 0, 3),
2094 SENSOR_ATTR_2(pwm1_auto_point4_temp, S_IWUSR | S_IRUGO,
2095 show_auto_temp, store_auto_temp, 0, 3),
2096 SENSOR_ATTR_2(pwm1_auto_point4_temp_hyst, S_IWUSR | S_IRUGO,
2097 show_auto_temp_hyst, store_auto_temp_hyst, 0, 3),
2098 SENSOR_ATTR_2(pwm1_auto_point5_pwm, S_IWUSR | S_IRUGO,
2099 show_auto_pwm, store_auto_pwm, 0, 4),
2100 SENSOR_ATTR_2(pwm1_auto_point5_temp, S_IWUSR | S_IRUGO,
2101 show_auto_temp, store_auto_temp, 0, 4),
2102 SENSOR_ATTR_2(pwm1_auto_point5_temp_hyst, S_IWUSR | S_IRUGO,
2103 show_auto_temp_hyst, store_auto_temp_hyst, 0, 4),
2104 SENSOR_ATTR_2(pwm1_auto_point6_pwm, S_IWUSR | S_IRUGO,
2105 show_auto_pwm, store_auto_pwm, 0, 5),
2106 SENSOR_ATTR_2(pwm1_auto_point6_temp, S_IWUSR | S_IRUGO,
2107 show_auto_temp, store_auto_temp, 0, 5),
2108 SENSOR_ATTR_2(pwm1_auto_point6_temp_hyst, S_IWUSR | S_IRUGO,
2109 show_auto_temp_hyst, store_auto_temp_hyst, 0, 5),
2110 SENSOR_ATTR_2(pwm1_auto_point7_pwm, S_IWUSR | S_IRUGO,
2111 show_auto_pwm, store_auto_pwm, 0, 6),
2112 SENSOR_ATTR_2(pwm1_auto_point7_temp, S_IWUSR | S_IRUGO,
2113 show_auto_temp, store_auto_temp, 0, 6),
2114 SENSOR_ATTR_2(pwm1_auto_point7_temp_hyst, S_IWUSR | S_IRUGO,
2115 show_auto_temp_hyst, store_auto_temp_hyst, 0, 6),
2117 SENSOR_ATTR_2(pwm2_auto_point1_pwm, S_IWUSR | S_IRUGO,
2118 show_auto_pwm, store_auto_pwm, 1, 0),
2119 SENSOR_ATTR_2(pwm2_auto_point1_temp, S_IWUSR | S_IRUGO,
2120 show_auto_temp, store_auto_temp, 1, 0),
2121 SENSOR_ATTR_2(pwm2_auto_point1_temp_hyst, S_IWUSR | S_IRUGO,
2122 show_auto_temp_hyst, store_auto_temp_hyst, 1, 0),
2123 SENSOR_ATTR_2(pwm2_auto_point2_pwm, S_IWUSR | S_IRUGO,
2124 show_auto_pwm, store_auto_pwm, 1, 1),
2125 SENSOR_ATTR_2(pwm2_auto_point2_temp, S_IWUSR | S_IRUGO,
2126 show_auto_temp, store_auto_temp, 1, 1),
2127 SENSOR_ATTR_2(pwm2_auto_point2_temp_hyst, S_IWUSR | S_IRUGO,
2128 show_auto_temp_hyst, store_auto_temp_hyst, 1, 1),
2129 SENSOR_ATTR_2(pwm2_auto_point3_pwm, S_IWUSR | S_IRUGO,
2130 show_auto_pwm, store_auto_pwm, 1, 2),
2131 SENSOR_ATTR_2(pwm2_auto_point3_temp, S_IWUSR | S_IRUGO,
2132 show_auto_temp, store_auto_temp, 1, 2),
2133 SENSOR_ATTR_2(pwm2_auto_point3_temp_hyst, S_IWUSR | S_IRUGO,
2134 show_auto_temp_hyst, store_auto_temp_hyst, 1, 2),
2135 SENSOR_ATTR_2(pwm2_auto_point4_pwm, S_IWUSR | S_IRUGO,
2136 show_auto_pwm, store_auto_pwm, 1, 3),
2137 SENSOR_ATTR_2(pwm2_auto_point4_temp, S_IWUSR | S_IRUGO,
2138 show_auto_temp, store_auto_temp, 1, 3),
2139 SENSOR_ATTR_2(pwm2_auto_point4_temp_hyst, S_IWUSR | S_IRUGO,
2140 show_auto_temp_hyst, store_auto_temp_hyst, 1, 3),
2141 SENSOR_ATTR_2(pwm2_auto_point5_pwm, S_IWUSR | S_IRUGO,
2142 show_auto_pwm, store_auto_pwm, 1, 4),
2143 SENSOR_ATTR_2(pwm2_auto_point5_temp, S_IWUSR | S_IRUGO,
2144 show_auto_temp, store_auto_temp, 1, 4),
2145 SENSOR_ATTR_2(pwm2_auto_point5_temp_hyst, S_IWUSR | S_IRUGO,
2146 show_auto_temp_hyst, store_auto_temp_hyst, 1, 4),
2147 SENSOR_ATTR_2(pwm2_auto_point6_pwm, S_IWUSR | S_IRUGO,
2148 show_auto_pwm, store_auto_pwm, 1, 5),
2149 SENSOR_ATTR_2(pwm2_auto_point6_temp, S_IWUSR | S_IRUGO,
2150 show_auto_temp, store_auto_temp, 1, 5),
2151 SENSOR_ATTR_2(pwm2_auto_point6_temp_hyst, S_IWUSR | S_IRUGO,
2152 show_auto_temp_hyst, store_auto_temp_hyst, 1, 5),
2153 SENSOR_ATTR_2(pwm2_auto_point7_pwm, S_IWUSR | S_IRUGO,
2154 show_auto_pwm, store_auto_pwm, 1, 6),
2155 SENSOR_ATTR_2(pwm2_auto_point7_temp, S_IWUSR | S_IRUGO,
2156 show_auto_temp, store_auto_temp, 1, 6),
2157 SENSOR_ATTR_2(pwm2_auto_point7_temp_hyst, S_IWUSR | S_IRUGO,
2158 show_auto_temp_hyst, store_auto_temp_hyst, 1, 6),
2160 SENSOR_ATTR_2(pwm3_auto_point1_pwm, S_IWUSR | S_IRUGO,
2161 show_auto_pwm, store_auto_pwm, 2, 0),
2162 SENSOR_ATTR_2(pwm3_auto_point1_temp, S_IWUSR | S_IRUGO,
2163 show_auto_temp, store_auto_temp, 2, 0),
2164 SENSOR_ATTR_2(pwm3_auto_point1_temp_hyst, S_IWUSR | S_IRUGO,
2165 show_auto_temp_hyst, store_auto_temp_hyst, 2, 0),
2166 SENSOR_ATTR_2(pwm3_auto_point2_pwm, S_IWUSR | S_IRUGO,
2167 show_auto_pwm, store_auto_pwm, 2, 1),
2168 SENSOR_ATTR_2(pwm3_auto_point2_temp, S_IWUSR | S_IRUGO,
2169 show_auto_temp, store_auto_temp, 2, 1),
2170 SENSOR_ATTR_2(pwm3_auto_point2_temp_hyst, S_IWUSR | S_IRUGO,
2171 show_auto_temp_hyst, store_auto_temp_hyst, 2, 1),
2172 SENSOR_ATTR_2(pwm3_auto_point3_pwm, S_IWUSR | S_IRUGO,
2173 show_auto_pwm, store_auto_pwm, 2, 2),
2174 SENSOR_ATTR_2(pwm3_auto_point3_temp, S_IWUSR | S_IRUGO,
2175 show_auto_temp, store_auto_temp, 2, 2),
2176 SENSOR_ATTR_2(pwm3_auto_point3_temp_hyst, S_IWUSR | S_IRUGO,
2177 show_auto_temp_hyst, store_auto_temp_hyst, 2, 2),
2178 SENSOR_ATTR_2(pwm3_auto_point4_pwm, S_IWUSR | S_IRUGO,
2179 show_auto_pwm, store_auto_pwm, 2, 3),
2180 SENSOR_ATTR_2(pwm3_auto_point4_temp, S_IWUSR | S_IRUGO,
2181 show_auto_temp, store_auto_temp, 2, 3),
2182 SENSOR_ATTR_2(pwm3_auto_point4_temp_hyst, S_IWUSR | S_IRUGO,
2183 show_auto_temp_hyst, store_auto_temp_hyst, 2, 3),
2184 SENSOR_ATTR_2(pwm3_auto_point5_pwm, S_IWUSR | S_IRUGO,
2185 show_auto_pwm, store_auto_pwm, 2, 4),
2186 SENSOR_ATTR_2(pwm3_auto_point5_temp, S_IWUSR | S_IRUGO,
2187 show_auto_temp, store_auto_temp, 2, 4),
2188 SENSOR_ATTR_2(pwm3_auto_point5_temp_hyst, S_IWUSR | S_IRUGO,
2189 show_auto_temp_hyst, store_auto_temp_hyst, 2, 4),
2190 SENSOR_ATTR_2(pwm3_auto_point6_pwm, S_IWUSR | S_IRUGO,
2191 show_auto_pwm, store_auto_pwm, 2, 5),
2192 SENSOR_ATTR_2(pwm3_auto_point6_temp, S_IWUSR | S_IRUGO,
2193 show_auto_temp, store_auto_temp, 2, 5),
2194 SENSOR_ATTR_2(pwm3_auto_point6_temp_hyst, S_IWUSR | S_IRUGO,
2195 show_auto_temp_hyst, store_auto_temp_hyst, 2, 5),
2196 SENSOR_ATTR_2(pwm3_auto_point7_pwm, S_IWUSR | S_IRUGO,
2197 show_auto_pwm, store_auto_pwm, 2, 6),
2198 SENSOR_ATTR_2(pwm3_auto_point7_temp, S_IWUSR | S_IRUGO,
2199 show_auto_temp, store_auto_temp, 2, 6),
2200 SENSOR_ATTR_2(pwm3_auto_point7_temp_hyst, S_IWUSR | S_IRUGO,
2201 show_auto_temp_hyst, store_auto_temp_hyst, 2, 6),
2203 SENSOR_ATTR_2(pwm4_auto_point1_pwm, S_IWUSR | S_IRUGO,
2204 show_auto_pwm, store_auto_pwm, 3, 0),
2205 SENSOR_ATTR_2(pwm4_auto_point1_temp, S_IWUSR | S_IRUGO,
2206 show_auto_temp, store_auto_temp, 3, 0),
2207 SENSOR_ATTR_2(pwm4_auto_point1_temp_hyst, S_IWUSR | S_IRUGO,
2208 show_auto_temp_hyst, store_auto_temp_hyst, 3, 0),
2209 SENSOR_ATTR_2(pwm4_auto_point2_pwm, S_IWUSR | S_IRUGO,
2210 show_auto_pwm, store_auto_pwm, 3, 1),
2211 SENSOR_ATTR_2(pwm4_auto_point2_temp, S_IWUSR | S_IRUGO,
2212 show_auto_temp, store_auto_temp, 3, 1),
2213 SENSOR_ATTR_2(pwm4_auto_point2_temp_hyst, S_IWUSR | S_IRUGO,
2214 show_auto_temp_hyst, store_auto_temp_hyst, 3, 1),
2215 SENSOR_ATTR_2(pwm4_auto_point3_pwm, S_IWUSR | S_IRUGO,
2216 show_auto_pwm, store_auto_pwm, 3, 2),
2217 SENSOR_ATTR_2(pwm4_auto_point3_temp, S_IWUSR | S_IRUGO,
2218 show_auto_temp, store_auto_temp, 3, 2),
2219 SENSOR_ATTR_2(pwm4_auto_point3_temp_hyst, S_IWUSR | S_IRUGO,
2220 show_auto_temp_hyst, store_auto_temp_hyst, 3, 2),
2221 SENSOR_ATTR_2(pwm4_auto_point4_pwm, S_IWUSR | S_IRUGO,
2222 show_auto_pwm, store_auto_pwm, 3, 3),
2223 SENSOR_ATTR_2(pwm4_auto_point4_temp, S_IWUSR | S_IRUGO,
2224 show_auto_temp, store_auto_temp, 3, 3),
2225 SENSOR_ATTR_2(pwm4_auto_point4_temp_hyst, S_IWUSR | S_IRUGO,
2226 show_auto_temp_hyst, store_auto_temp_hyst, 3, 3),
2227 SENSOR_ATTR_2(pwm4_auto_point5_pwm, S_IWUSR | S_IRUGO,
2228 show_auto_pwm, store_auto_pwm, 3, 4),
2229 SENSOR_ATTR_2(pwm4_auto_point5_temp, S_IWUSR | S_IRUGO,
2230 show_auto_temp, store_auto_temp, 3, 4),
2231 SENSOR_ATTR_2(pwm4_auto_point5_temp_hyst, S_IWUSR | S_IRUGO,
2232 show_auto_temp_hyst, store_auto_temp_hyst, 3, 4),
2233 SENSOR_ATTR_2(pwm4_auto_point6_pwm, S_IWUSR | S_IRUGO,
2234 show_auto_pwm, store_auto_pwm, 3, 5),
2235 SENSOR_ATTR_2(pwm4_auto_point6_temp, S_IWUSR | S_IRUGO,
2236 show_auto_temp, store_auto_temp, 3, 5),
2237 SENSOR_ATTR_2(pwm4_auto_point6_temp_hyst, S_IWUSR | S_IRUGO,
2238 show_auto_temp_hyst, store_auto_temp_hyst, 3, 5),
2239 SENSOR_ATTR_2(pwm4_auto_point7_pwm, S_IWUSR | S_IRUGO,
2240 show_auto_pwm, store_auto_pwm, 3, 6),
2241 SENSOR_ATTR_2(pwm4_auto_point7_temp, S_IWUSR | S_IRUGO,
2242 show_auto_temp, store_auto_temp, 3, 6),
2243 SENSOR_ATTR_2(pwm4_auto_point7_temp_hyst, S_IWUSR | S_IRUGO,
2244 show_auto_temp_hyst, store_auto_temp_hyst, 3, 6),
2246 SENSOR_ATTR_2(pwm5_auto_point1_pwm, S_IWUSR | S_IRUGO,
2247 show_auto_pwm, store_auto_pwm, 4, 0),
2248 SENSOR_ATTR_2(pwm5_auto_point1_temp, S_IWUSR | S_IRUGO,
2249 show_auto_temp, store_auto_temp, 4, 0),
2250 SENSOR_ATTR_2(pwm5_auto_point1_temp_hyst, S_IWUSR | S_IRUGO,
2251 show_auto_temp_hyst, store_auto_temp_hyst, 4, 0),
2252 SENSOR_ATTR_2(pwm5_auto_point2_pwm, S_IWUSR | S_IRUGO,
2253 show_auto_pwm, store_auto_pwm, 4, 1),
2254 SENSOR_ATTR_2(pwm5_auto_point2_temp, S_IWUSR | S_IRUGO,
2255 show_auto_temp, store_auto_temp, 4, 1),
2256 SENSOR_ATTR_2(pwm5_auto_point2_temp_hyst, S_IWUSR | S_IRUGO,
2257 show_auto_temp_hyst, store_auto_temp_hyst, 4, 1),
2258 SENSOR_ATTR_2(pwm5_auto_point3_pwm, S_IWUSR | S_IRUGO,
2259 show_auto_pwm, store_auto_pwm, 4, 2),
2260 SENSOR_ATTR_2(pwm5_auto_point3_temp, S_IWUSR | S_IRUGO,
2261 show_auto_temp, store_auto_temp, 4, 2),
2262 SENSOR_ATTR_2(pwm5_auto_point3_temp_hyst, S_IWUSR | S_IRUGO,
2263 show_auto_temp_hyst, store_auto_temp_hyst, 4, 2),
2264 SENSOR_ATTR_2(pwm5_auto_point4_pwm, S_IWUSR | S_IRUGO,
2265 show_auto_pwm, store_auto_pwm, 4, 3),
2266 SENSOR_ATTR_2(pwm5_auto_point4_temp, S_IWUSR | S_IRUGO,
2267 show_auto_temp, store_auto_temp, 4, 3),
2268 SENSOR_ATTR_2(pwm5_auto_point4_temp_hyst, S_IWUSR | S_IRUGO,
2269 show_auto_temp_hyst, store_auto_temp_hyst, 4, 3),
2270 SENSOR_ATTR_2(pwm5_auto_point5_pwm, S_IWUSR | S_IRUGO,
2271 show_auto_pwm, store_auto_pwm, 4, 4),
2272 SENSOR_ATTR_2(pwm5_auto_point5_temp, S_IWUSR | S_IRUGO,
2273 show_auto_temp, store_auto_temp, 4, 4),
2274 SENSOR_ATTR_2(pwm5_auto_point5_temp_hyst, S_IWUSR | S_IRUGO,
2275 show_auto_temp_hyst, store_auto_temp_hyst, 4, 4),
2276 SENSOR_ATTR_2(pwm5_auto_point6_pwm, S_IWUSR | S_IRUGO,
2277 show_auto_pwm, store_auto_pwm, 4, 5),
2278 SENSOR_ATTR_2(pwm5_auto_point6_temp, S_IWUSR | S_IRUGO,
2279 show_auto_temp, store_auto_temp, 4, 5),
2280 SENSOR_ATTR_2(pwm5_auto_point6_temp_hyst, S_IWUSR | S_IRUGO,
2281 show_auto_temp_hyst, store_auto_temp_hyst, 4, 5),
2282 SENSOR_ATTR_2(pwm5_auto_point7_pwm, S_IWUSR | S_IRUGO,
2283 show_auto_pwm, store_auto_pwm, 4, 6),
2284 SENSOR_ATTR_2(pwm5_auto_point7_temp, S_IWUSR | S_IRUGO,
2285 show_auto_temp, store_auto_temp, 4, 6),
2286 SENSOR_ATTR_2(pwm5_auto_point7_temp_hyst, S_IWUSR | S_IRUGO,
2287 show_auto_temp_hyst, store_auto_temp_hyst, 4, 6),
2291 show_vid(struct device *dev, struct device_attribute *attr, char *buf)
2293 struct nct6775_data *data = dev_get_drvdata(dev);
2294 return sprintf(buf, "%d\n", vid_from_reg(data->vid, data->vrm));
2296 static DEVICE_ATTR(cpu0_vid, S_IRUGO, show_vid, NULL);
2299 /* Case open detection */
2302 show_caseopen(struct device *dev, struct device_attribute *attr, char *buf)
2304 struct nct6775_data *data = nct6775_update_device(dev);
2306 return sprintf(buf, "%d\n",
2307 !!(data->caseopen & to_sensor_dev_attr_2(attr)->index));
2311 clear_caseopen(struct device *dev, struct device_attribute *attr,
2312 const char *buf, size_t count)
2314 struct nct6775_data *data = dev_get_drvdata(dev);
2318 if (kstrtoul(buf, 10, &val) || val != 0)
2321 mask = to_sensor_dev_attr_2(attr)->nr;
2323 mutex_lock(&data->update_lock);
2324 reg = nct6775_read_value(data, NCT6775_REG_CASEOPEN_CLR);
2325 nct6775_write_value(data, NCT6775_REG_CASEOPEN_CLR, reg | mask);
2326 nct6775_write_value(data, NCT6775_REG_CASEOPEN_CLR, reg & ~mask);
2327 data->valid = 0; /* Force cache refresh */
2328 mutex_unlock(&data->update_lock);
2333 static struct sensor_device_attribute_2 sda_caseopen[] = {
2334 SENSOR_ATTR_2(intrusion0_alarm, S_IWUSR | S_IRUGO, show_caseopen,
2335 clear_caseopen, 0x80, 0x10),
2336 SENSOR_ATTR_2(intrusion1_alarm, S_IWUSR | S_IRUGO, show_caseopen,
2337 clear_caseopen, 0x40, 0x40),
2341 * Driver and device management
2344 static void nct6775_device_remove_files(struct device *dev)
2347 * some entries in the following arrays may not have been used in
2348 * device_create_file(), but device_remove_file() will ignore them
2351 struct nct6775_data *data = dev_get_drvdata(dev);
2353 for (i = 0; i < data->pwm_num; i++) {
2354 sysfs_remove_group(&dev->kobj, &nct6775_group_pwm[i]);
2355 for (j = 0; j < ARRAY_SIZE(sda_auto_pwm_arrays); j++)
2356 device_remove_file(dev,
2357 &sda_auto_pwm_arrays[j].dev_attr);
2360 for (i = 0; i < data->in_num; i++)
2361 sysfs_remove_group(&dev->kobj, &nct6775_group_in[i]);
2363 for (i = 0; i < 5; i++) {
2364 device_remove_file(dev, &sda_fan_input[i].dev_attr);
2365 device_remove_file(dev, &sda_fan_alarm[i].dev_attr);
2366 device_remove_file(dev, &sda_fan_div[i].dev_attr);
2367 device_remove_file(dev, &sda_fan_min[i].dev_attr);
2369 for (i = 0; i < NUM_REG_TEMP; i++) {
2370 if (!(data->have_temp & (1 << i)))
2372 device_remove_file(dev, &sda_temp_input[i].dev_attr);
2373 device_remove_file(dev, &sda_temp_label[i].dev_attr);
2374 device_remove_file(dev, &sda_temp_max[i].dev_attr);
2375 device_remove_file(dev, &sda_temp_max_hyst[i].dev_attr);
2378 device_remove_file(dev, &sda_temp_alarm[i].dev_attr);
2379 device_remove_file(dev, &sda_temp_type[i].dev_attr);
2382 device_remove_file(dev, &sda_caseopen[0].dev_attr);
2383 device_remove_file(dev, &sda_caseopen[1].dev_attr);
2385 device_remove_file(dev, &dev_attr_name);
2386 device_remove_file(dev, &dev_attr_cpu0_vid);
2389 /* Get the monitoring functions started */
2390 static inline void __devinit nct6775_init_device(struct nct6775_data *data,
2396 /* Start monitoring if needed */
2397 tmp = nct6775_read_value(data, NCT6775_REG_CONFIG);
2399 nct6775_write_value(data, NCT6775_REG_CONFIG,
2402 /* Enable temperature sensors if needed */
2403 for (i = 0; i < NUM_REG_TEMP; i++) {
2404 if (!(data->have_temp & (1 << i)))
2406 if (!data->reg_temp_config[i])
2408 tmp = nct6775_read_value(data,
2409 data->reg_temp_config[i]);
2411 nct6775_write_value(data,
2412 data->reg_temp_config[i],
2416 /* Enable VBAT monitoring if needed */
2417 tmp = nct6775_read_value(data, NCT6775_REG_VBAT);
2419 nct6775_write_value(data, NCT6775_REG_VBAT, tmp | 0x01);
2421 for (i = 0; i < 3; i++) {
2422 const char *label = NULL;
2424 if (data->temp_label)
2425 label = data->temp_label[data->temp_src[i]];
2427 /* Digital source overrides analog type */
2428 if (label && strncmp(label, "PECI", 4) == 0)
2429 data->temp_type[i] = 6;
2430 else if (label && strncmp(label, "AMD", 3) == 0)
2431 data->temp_type[i] = 5;
2432 else if ((tmp & (0x02 << i)))
2433 data->temp_type[i] = 1; /* diode */
2435 data->temp_type[i] = 4; /* thermistor */
2439 static void w82627ehf_swap_tempreg(struct nct6775_data *data,
2444 tmp = data->temp_src[r1];
2445 data->temp_src[r1] = data->temp_src[r2];
2446 data->temp_src[r2] = tmp;
2448 tmp = data->reg_temp[r1];
2449 data->reg_temp[r1] = data->reg_temp[r2];
2450 data->reg_temp[r2] = tmp;
2452 tmp = data->reg_temp_over[r1];
2453 data->reg_temp_over[r1] = data->reg_temp_over[r2];
2454 data->reg_temp_over[r2] = tmp;
2456 tmp = data->reg_temp_hyst[r1];
2457 data->reg_temp_hyst[r1] = data->reg_temp_hyst[r2];
2458 data->reg_temp_hyst[r2] = tmp;
2460 tmp = data->reg_temp_config[r1];
2461 data->reg_temp_config[r1] = data->reg_temp_config[r2];
2462 data->reg_temp_config[r2] = tmp;
2465 static void __devinit
2466 nct6775_check_fan_inputs(const struct nct6775_sio_data *sio_data,
2467 struct nct6775_data *data)
2470 bool fan3pin, fan3min, fan4pin, fan4min, fan5pin;
2471 bool pwm3pin, pwm4pin, pwm5pin;
2473 superio_enter(sio_data->sioreg);
2475 /* fan4 and fan5 share some pins with the GPIO and serial flash */
2476 if (sio_data->kind == nct6775) {
2477 regval = superio_inb(sio_data->sioreg, 0x2c);
2479 fan3pin = regval & (1 << 6);
2481 pwm3pin = regval & (1 << 7);
2483 /* On NCT6775, fan4 shares pins with the fdc interface */
2484 fan4pin = !(superio_inb(sio_data->sioreg, 0x2A) & 0x80);
2489 } else if (sio_data->kind == nct6776) {
2490 bool gpok = superio_inb(sio_data->sioreg, 0x27) & 0x80;
2492 superio_select(sio_data->sioreg, NCT6775_LD_HWM);
2493 regval = superio_inb(sio_data->sioreg, SIO_REG_ENABLE);
2498 fan3pin = !(superio_inb(sio_data->sioreg, 0x24) & 0x40);
2503 fan4pin = superio_inb(sio_data->sioreg, 0x1C) & 0x01;
2508 fan5pin = superio_inb(sio_data->sioreg, 0x1C) & 0x02;
2515 } else { /* NCT6779D */
2516 regval = superio_inb(sio_data->sioreg, 0x1c);
2518 fan3pin = !(regval & (1 << 5));
2519 fan4pin = !(regval & (1 << 6));
2520 fan5pin = !(regval & (1 << 7));
2522 pwm3pin = !(regval & (1 << 0));
2523 pwm4pin = !(regval & (1 << 1));
2524 pwm5pin = !(regval & (1 << 2));
2530 superio_exit(sio_data->sioreg);
2532 data->has_fan = data->has_fan_min = 0x03; /* fan1 and fan2 */
2533 data->has_fan |= (fan3pin << 2);
2534 data->has_fan_min |= (fan3min << 2);
2536 data->has_fan |= (fan4pin << 3) | (fan5pin << 4);
2537 data->has_fan_min |= (fan4min << 3) | (fan5pin << 4);
2539 data->has_pwm = 0x03 | (pwm3pin << 2) | (pwm4pin << 3) | (pwm5pin << 4);
2542 static int __devinit nct6775_probe(struct platform_device *pdev)
2544 struct device *dev = &pdev->dev;
2545 struct nct6775_sio_data *sio_data = dev->platform_data;
2546 struct nct6775_data *data;
2547 struct resource *res;
2550 res = platform_get_resource(pdev, IORESOURCE_IO, 0);
2551 if (!request_region(res->start, IOREGION_LENGTH, DRVNAME)) {
2553 dev_err(dev, "Failed to request region 0x%lx-0x%lx\n",
2554 (unsigned long)res->start,
2555 (unsigned long)res->start + IOREGION_LENGTH - 1);
2559 data = devm_kzalloc(&pdev->dev, sizeof(struct nct6775_data),
2566 data->addr = res->start;
2567 mutex_init(&data->lock);
2568 mutex_init(&data->update_lock);
2569 data->name = nct6775_device_names[sio_data->kind];
2570 platform_set_drvdata(pdev, data);
2572 switch (sio_data->kind) {
2575 data->have_in = 0x1ff;
2579 data->have_in = 0x7fff;
2583 switch (sio_data->kind) {
2586 data->auto_pwm_num = 6;
2590 data->auto_pwm_num = 4;
2594 data->auto_pwm_num = 4;
2598 /* Default to no temperature inputs, code below will adjust as needed */
2599 data->have_temp = 0;
2601 /* Deal with temperature register setup first. */
2602 if (sio_data->kind == nct6775 || sio_data->kind == nct6776) {
2606 * Display temperature sensor output only if it monitors
2607 * a source other than one already reported. Always display
2608 * first three temperature registers, though.
2610 for (i = 0; i < NUM_REG_TEMP; i++) {
2613 data->reg_temp[i] = NCT6775_REG_TEMP[i];
2614 data->reg_temp_over[i] = NCT6775_REG_TEMP_OVER[i];
2615 data->reg_temp_hyst[i] = NCT6775_REG_TEMP_HYST[i];
2616 if (sio_data->kind == nct6775)
2617 data->reg_temp_config[i]
2618 = NCT6775_REG_TEMP_CONFIG[i];
2620 data->reg_temp_config[i]
2621 = NCT6776_REG_TEMP_CONFIG[i];
2623 src = nct6775_read_value(data,
2624 NCT6775_REG_TEMP_SOURCE[i]);
2626 if (src && !(mask & (1 << src))) {
2627 data->have_temp |= 1 << i;
2631 data->temp_src[i] = src;
2634 * Now do some register swapping if index 0..2 don't
2635 * point to SYSTIN(1), CPUIN(2), and AUXIN(3).
2636 * Idea is to have the first three attributes
2637 * report SYSTIN, CPUIN, and AUXIN if possible
2638 * without overriding the basic system configuration.
2640 if (i > 0 && data->temp_src[0] != 1
2641 && data->temp_src[i] == 1)
2642 w82627ehf_swap_tempreg(data, 0, i);
2643 if (i > 1 && data->temp_src[1] != 2
2644 && data->temp_src[i] == 2)
2645 w82627ehf_swap_tempreg(data, 1, i);
2646 if (i > 2 && data->temp_src[2] != 3
2647 && data->temp_src[i] == 3)
2648 w82627ehf_swap_tempreg(data, 2, i);
2650 if (sio_data->kind == nct6776) {
2652 * On NCT6776, AUXTIN and VIN3 pins are shared.
2653 * Only way to detect it is to check if AUXTIN is used
2654 * as a temperature source, and if that source is
2657 * If that is the case, disable in6, which reports VIN3.
2658 * Otherwise disable temp3.
2660 if (data->temp_src[2] == 3) {
2663 if (data->reg_temp_config[2])
2664 reg = nct6775_read_value(data,
2665 data->reg_temp_config[2]);
2667 reg = 0; /* Assume AUXTIN is used */
2670 data->have_temp &= ~(1 << 2);
2672 data->have_in &= ~(1 << 6);
2674 data->temp_label = nct6776_temp_label;
2676 data->temp_label = nct6775_temp_label;
2678 } else if (sio_data->kind == nct6779) {
2682 * Display temperature sensor output only if it monitors
2683 * a source other than one already reported. Always display
2684 * first three temperature registers, though.
2686 for (i = 0; i < ARRAY_SIZE(NCT6779_REG_TEMP); i++) {
2689 data->reg_temp[i] = NCT6779_REG_TEMP[i];
2690 data->reg_temp_over[i] = NCT6779_REG_TEMP_OVER[i];
2691 data->reg_temp_hyst[i] = NCT6779_REG_TEMP_HYST[i];
2692 data->reg_temp_config[i] = NCT6779_REG_TEMP_CONFIG[i];
2694 src = nct6775_read_value(data,
2695 NCT6779_REG_TEMP_SOURCE[i]);
2697 if (src && !(mask & (1 << src))) {
2698 data->have_temp |= 1 << i;
2702 data->temp_src[i] = src;
2705 * Now do some register swapping if index 0..2 don't
2706 * point to SYSTIN(1), CPUIN(2), and AUXIN(3).
2707 * Idea is to have the first three attributes
2708 * report SYSTIN, CPUIN, and AUXIN if possible
2709 * without overriding the basic system configuration.
2711 if (i > 0 && data->temp_src[0] != 1
2712 && data->temp_src[i] == 1)
2713 w82627ehf_swap_tempreg(data, 0, i);
2714 if (i > 1 && data->temp_src[1] != 2
2715 && data->temp_src[i] == 2)
2716 w82627ehf_swap_tempreg(data, 1, i);
2717 if (i > 2 && data->temp_src[2] != 3
2718 && data->temp_src[i] == 3)
2719 w82627ehf_swap_tempreg(data, 2, i);
2721 data->temp_label = nct6779_temp_label;
2729 * Assume voltage is disabled if the respective temperature is
2730 * used as temperature source.
2732 for (i = 0; i < ARRAY_SIZE(NCT6779_REG_TEMP); i++) {
2733 if (!(data->have_temp & (1 << i)))
2735 if (data->temp_src[i] == 3) /* AUXTIN0 */
2736 data->have_in &= ~(1 << 6); /* no VIN4 */
2737 if (data->temp_src[i] == 4) /* AUXTIN1 */
2738 data->have_in &= ~(1 << 10); /* no VIN5 */
2739 if (data->temp_src[i] == 5) /* AUXTIN2 */
2740 data->have_in &= ~(1 << 11); /* no VIN6 */
2741 if (data->temp_src[i] == 6) /* AUXTIN0 */
2742 data->have_in &= ~(1 << 14); /* no VIN7 */
2746 if (sio_data->kind == nct6775) {
2747 data->has_fan_div = true;
2748 data->fan_from_reg = fan_from_reg16;
2749 data->fan_from_reg_min = fan_from_reg8;
2750 data->REG_VIN = NCT6775_REG_IN;
2751 data->REG_TARGET = NCT6775_REG_TARGET;
2752 data->REG_FAN = NCT6775_REG_FAN;
2753 data->REG_FAN_MIN = NCT6775_REG_FAN_MIN;
2754 data->REG_FAN_START_OUTPUT = NCT6775_REG_FAN_START_OUTPUT;
2755 data->REG_FAN_STOP_OUTPUT = NCT6775_REG_FAN_STOP_OUTPUT;
2756 } else if (sio_data->kind == nct6776) {
2757 data->has_fan_div = false;
2758 data->fan_from_reg = fan_from_reg13;
2759 data->fan_from_reg_min = fan_from_reg13;
2760 data->REG_VIN = NCT6775_REG_IN;
2761 data->REG_TARGET = NCT6775_REG_TARGET;
2762 data->REG_FAN = NCT6775_REG_FAN;
2763 data->REG_FAN_MIN = NCT6776_REG_FAN_MIN;
2764 data->REG_FAN_START_OUTPUT = NCT6775_REG_FAN_START_OUTPUT;
2765 data->REG_FAN_STOP_OUTPUT = NCT6775_REG_FAN_STOP_OUTPUT;
2766 } else if (sio_data->kind == nct6779) {
2767 data->has_fan_div = false;
2768 data->fan_from_reg = fan_from_reg13;
2769 data->fan_from_reg_min = fan_from_reg13;
2770 data->REG_VIN = NCT6779_REG_IN;
2771 data->REG_TARGET = NCT6775_REG_TARGET;
2772 data->REG_FAN = NCT6779_REG_FAN;
2773 data->REG_FAN_MIN = NCT6776_REG_FAN_MIN;
2774 data->REG_FAN_START_OUTPUT = NCT6775_REG_FAN_START_OUTPUT;
2775 data->REG_FAN_STOP_OUTPUT = NCT6775_REG_FAN_STOP_OUTPUT;
2778 /* Initialize the chip */
2779 nct6775_init_device(data, sio_data->kind);
2781 data->vrm = vid_which_vrm();
2782 superio_enter(sio_data->sioreg);
2785 * We can get the VID input values directly at logical device D 0xe3.
2787 superio_select(sio_data->sioreg, NCT6775_LD_VID);
2788 data->vid = superio_inb(sio_data->sioreg, 0xe3);
2789 err = device_create_file(dev, &dev_attr_cpu0_vid);
2796 superio_select(sio_data->sioreg, NCT6775_LD_HWM);
2797 tmp = superio_inb(sio_data->sioreg, NCT6775_REG_FAN_DEBOUNCE);
2798 if (sio_data->kind == nct6776 || sio_data->kind == nct6779)
2799 superio_outb(sio_data->sioreg, NCT6775_REG_FAN_DEBOUNCE,
2802 superio_outb(sio_data->sioreg, NCT6775_REG_FAN_DEBOUNCE,
2804 pr_info("Enabled fan debounce for chip %s\n", data->name);
2807 superio_exit(sio_data->sioreg);
2809 nct6775_check_fan_inputs(sio_data, data);
2811 /* Read fan clock dividers immediately */
2812 nct6775_update_fan_div_common(dev, data);
2814 /* Register sysfs hooks */
2815 for (i = 0; i < data->pwm_num; i++) {
2816 if (!(data->has_pwm & (1 << i)))
2818 err = sysfs_create_group(&dev->kobj, &nct6775_group_pwm[i]);
2822 for (i = 0; i < ARRAY_SIZE(sda_auto_pwm_arrays); i++) {
2823 struct sensor_device_attribute_2 *attr =
2824 &sda_auto_pwm_arrays[i];
2826 if (!(data->has_pwm & (1 << attr->nr)))
2828 if (attr->index > data->auto_pwm_num)
2830 err = device_create_file(dev, &attr->dev_attr);
2835 for (i = 0; i < data->in_num; i++) {
2836 if (!(data->have_in & (1 << i)))
2838 err = sysfs_create_group(&dev->kobj, &nct6775_group_in[i]);
2843 for (i = 0; i < 5; i++) {
2844 if (data->has_fan & (1 << i)) {
2845 err = device_create_file(dev,
2846 &sda_fan_input[i].dev_attr);
2849 err = device_create_file(dev,
2850 &sda_fan_alarm[i].dev_attr);
2853 if (sio_data->kind != nct6776 &&
2854 sio_data->kind != nct6779) {
2855 err = device_create_file(dev,
2856 &sda_fan_div[i].dev_attr);
2860 if (data->has_fan_min & (1 << i)) {
2861 err = device_create_file(dev,
2862 &sda_fan_min[i].dev_attr);
2869 for (i = 0; i < NUM_REG_TEMP; i++) {
2870 if (!(data->have_temp & (1 << i)))
2872 err = device_create_file(dev, &sda_temp_input[i].dev_attr);
2875 if (data->temp_label) {
2876 err = device_create_file(dev,
2877 &sda_temp_label[i].dev_attr);
2881 if (data->reg_temp_over[i]) {
2882 err = device_create_file(dev,
2883 &sda_temp_max[i].dev_attr);
2887 if (data->reg_temp_hyst[i]) {
2888 err = device_create_file(dev,
2889 &sda_temp_max_hyst[i].dev_attr);
2895 err = device_create_file(dev, &sda_temp_alarm[i].dev_attr);
2898 err = device_create_file(dev, &sda_temp_type[i].dev_attr);
2903 err = device_create_file(dev, &sda_caseopen[0].dev_attr);
2907 if (sio_data->kind == nct6776 || sio_data->kind == nct6779) {
2908 err = device_create_file(dev, &sda_caseopen[1].dev_attr);
2913 err = device_create_file(dev, &dev_attr_name);
2917 data->hwmon_dev = hwmon_device_register(dev);
2918 if (IS_ERR(data->hwmon_dev)) {
2919 err = PTR_ERR(data->hwmon_dev);
2926 nct6775_device_remove_files(dev);
2928 platform_set_drvdata(pdev, NULL);
2929 release_region(res->start, IOREGION_LENGTH);
2934 static int __devexit nct6775_remove(struct platform_device *pdev)
2936 struct nct6775_data *data = platform_get_drvdata(pdev);
2938 hwmon_device_unregister(data->hwmon_dev);
2939 nct6775_device_remove_files(&pdev->dev);
2940 release_region(data->addr, IOREGION_LENGTH);
2941 platform_set_drvdata(pdev, NULL);
2946 static struct platform_driver nct6775_driver = {
2948 .owner = THIS_MODULE,
2951 .probe = nct6775_probe,
2952 .remove = __devexit_p(nct6775_remove),
2955 /* nct6775_find() looks for a '627 in the Super-I/O config space */
2956 static int __init nct6775_find(int sioaddr, unsigned short *addr,
2957 struct nct6775_sio_data *sio_data)
2959 static const char __initdata sio_name_NCT6775[] = "NCT6775F";
2960 static const char __initdata sio_name_NCT6776[] = "NCT6776F";
2961 static const char __initdata sio_name_NCT6779[] = "NCT6779F";
2964 const char *sio_name;
2966 superio_enter(sioaddr);
2971 val = (superio_inb(sioaddr, SIO_REG_DEVID) << 8)
2972 | superio_inb(sioaddr, SIO_REG_DEVID + 1);
2973 switch (val & SIO_ID_MASK) {
2974 case SIO_NCT6775_ID:
2975 sio_data->kind = nct6775;
2976 sio_name = sio_name_NCT6775;
2978 case SIO_NCT6776_ID:
2979 sio_data->kind = nct6776;
2980 sio_name = sio_name_NCT6776;
2982 case SIO_NCT6779_ID:
2983 sio_data->kind = nct6779;
2984 sio_name = sio_name_NCT6779;
2988 pr_debug("unsupported chip ID: 0x%04x\n", val);
2989 superio_exit(sioaddr);
2993 /* We have a known chip, find the HWM I/O address */
2994 superio_select(sioaddr, NCT6775_LD_HWM);
2995 val = (superio_inb(sioaddr, SIO_REG_ADDR) << 8)
2996 | superio_inb(sioaddr, SIO_REG_ADDR + 1);
2997 *addr = val & IOREGION_ALIGNMENT;
2999 pr_err("Refusing to enable a Super-I/O device with a base I/O port 0\n");
3000 superio_exit(sioaddr);
3004 /* Activate logical device if needed */
3005 val = superio_inb(sioaddr, SIO_REG_ENABLE);
3006 if (!(val & 0x01)) {
3007 pr_warn("Forcibly enabling Super-I/O. "
3008 "Sensor is probably unusable.\n");
3009 superio_outb(sioaddr, SIO_REG_ENABLE, val | 0x01);
3012 superio_exit(sioaddr);
3013 pr_info("Found %s chip at %#x\n", sio_name, *addr);
3014 sio_data->sioreg = sioaddr;
3020 * when Super-I/O functions move to a separate file, the Super-I/O
3021 * bus will manage the lifetime of the device and this module will only keep
3022 * track of the nct6775 driver. But since we platform_device_alloc(), we
3023 * must keep track of the device
3025 static struct platform_device *pdev;
3027 static int __init sensors_nct6775_init(void)
3030 unsigned short address;
3031 struct resource res;
3032 struct nct6775_sio_data sio_data;
3035 * initialize sio_data->kind and sio_data->sioreg.
3037 * when Super-I/O functions move to a separate file, the Super-I/O
3038 * driver will probe 0x2e and 0x4e and auto-detect the presence of a
3039 * nct6775 hardware monitor, and call probe()
3041 if (nct6775_find(0x2e, &address, &sio_data) &&
3042 nct6775_find(0x4e, &address, &sio_data))
3045 err = platform_driver_register(&nct6775_driver);
3049 pdev = platform_device_alloc(DRVNAME, address);
3052 pr_err("Device allocation failed\n");
3053 goto exit_unregister;
3056 err = platform_device_add_data(pdev, &sio_data,
3057 sizeof(struct nct6775_sio_data));
3059 pr_err("Platform data allocation failed\n");
3060 goto exit_device_put;
3063 memset(&res, 0, sizeof(res));
3065 res.start = address + IOREGION_OFFSET;
3066 res.end = address + IOREGION_OFFSET + IOREGION_LENGTH - 1;
3067 res.flags = IORESOURCE_IO;
3069 err = acpi_check_resource_conflict(&res);
3071 goto exit_device_put;
3073 err = platform_device_add_resources(pdev, &res, 1);
3075 pr_err("Device resource addition failed (%d)\n", err);
3076 goto exit_device_put;
3079 /* platform_device_add calls probe() */
3080 err = platform_device_add(pdev);
3082 pr_err("Device addition failed (%d)\n", err);
3083 goto exit_device_put;
3089 platform_device_put(pdev);
3091 platform_driver_unregister(&nct6775_driver);
3096 static void __exit sensors_nct6775_exit(void)
3098 platform_device_unregister(pdev);
3099 platform_driver_unregister(&nct6775_driver);
3102 MODULE_AUTHOR("Guenter Roeck <linux@roeck-us.net>");
3103 MODULE_DESCRIPTION("NCT677x driver");
3104 MODULE_LICENSE("GPL");
3106 module_init(sensors_nct6775_init);
3107 module_exit(sensors_nct6775_exit);