]> git.sur5r.net Git - groeck-nct6775/blob - nct6775.c
Distinguish thermal temp diode / diode (types 1, 3)
[groeck-nct6775] / nct6775.c
1 /*
2  * nct6775 - Driver for the hardware monitoring functionality of
3  *             Nuvoton NCT677x Super-I/O chips
4  *
5  * Copyright (C) 2012  Guenter Roeck <linux@roeck-us.net>
6  *
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)
14  *
15  * Shamelessly ripped from the w83627hf driver
16  * Copyright (C) 2003  Mark Studebaker
17  *
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.
22  *
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.
27  *
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.
31  *
32  *
33  * Supports the following chips:
34  *
35  * Chip        #vin    #fan    #pwm    #temp  chip IDs       man ID
36  * nct6775f     9      4       3       6+3    0xb470 0xc1    0x5ca3
37  * nct6776f     9      5       3       6+3    0xc330 0xc1    0x5ca3
38  * nct6779d    15      5       5       2+5    0xc560 0xc1    0x5ca3
39  */
40
41 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
42
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>
54 #include <linux/io.h>
55 #include "lm75.h"
56
57 enum kinds { nct6775, nct6776, nct6779 };
58
59 /* used to set data->name = nct6775_device_names[data->sio_kind] */
60 static const char * const nct6775_device_names[] = {
61         "nct6775",
62         "nct6776",
63         "nct6779",
64 };
65
66 static unsigned short force_id;
67 module_param(force_id, ushort, 0);
68 MODULE_PARM_DESC(force_id, "Override the detected device ID");
69
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");
73
74 #define DRVNAME "nct6775"
75
76 /*
77  * Super-I/O constants and functions
78  */
79
80 #define NCT6775_LD_ACPI         0x0a
81 #define NCT6775_LD_HWM          0x0b
82 #define NCT6775_LD_VID          0x0d
83
84 #define SIO_REG_LDSEL           0x07    /* Logical device select */
85 #define SIO_REG_DEVID           0x20    /* Device ID (2 bytes) */
86 #define SIO_REG_EN_VRM10        0x2C    /* GPIO3, GPIO4 selection */
87 #define SIO_REG_ENABLE          0x30    /* Logical device enable */
88 #define SIO_REG_ADDR            0x60    /* Logical device address (2 bytes) */
89 #define SIO_REG_VID_CTRL        0xF0    /* VID control */
90 #define SIO_REG_VID_DATA        0xF1    /* VID data */
91
92 #define SIO_NCT6775_ID          0xb470
93 #define SIO_NCT6776_ID          0xc330
94 #define SIO_NCT6779_ID          0xc560
95 #define SIO_ID_MASK             0xFFF0
96
97 static inline void
98 superio_outb(int ioreg, int reg, int val)
99 {
100         outb(reg, ioreg);
101         outb(val, ioreg + 1);
102 }
103
104 static inline int
105 superio_inb(int ioreg, int reg)
106 {
107         outb(reg, ioreg);
108         return inb(ioreg + 1);
109 }
110
111 static inline void
112 superio_select(int ioreg, int ld)
113 {
114         outb(SIO_REG_LDSEL, ioreg);
115         outb(ld, ioreg + 1);
116 }
117
118 static inline void
119 superio_enter(int ioreg)
120 {
121         outb(0x87, ioreg);
122         outb(0x87, ioreg);
123 }
124
125 static inline void
126 superio_exit(int ioreg)
127 {
128         outb(0xaa, ioreg);
129         outb(0x02, ioreg);
130         outb(0x02, ioreg + 1);
131 }
132
133 /*
134  * ISA constants
135  */
136
137 #define IOREGION_ALIGNMENT      (~7)
138 #define IOREGION_OFFSET         5
139 #define IOREGION_LENGTH         2
140 #define ADDR_REG_OFFSET         0
141 #define DATA_REG_OFFSET         1
142
143 #define NCT6775_REG_BANK        0x4E
144 #define NCT6775_REG_CONFIG      0x40
145
146 /*
147  * Not currently used:
148  * REG_MAN_ID has the value 0x5ca3 for all supported chips.
149  * REG_CHIP_ID == 0x88/0xa1/0xc1 depending on chip model.
150  * REG_MAN_ID is at port 0x4f
151  * REG_CHIP_ID is at port 0x58
152  */
153
154 static const u16 NCT6775_REG_FAN_MIN[] = { 0x3b, 0x3c, 0x3d };
155
156 /* Voltage min/max registers for nr=7..14 are in bank 5 */
157
158 static const u16 NCT6775_REG_IN_MAX[] = {
159         0x2b, 0x2d, 0x2f, 0x31, 0x33, 0x35, 0x37, 0x554, 0x556 };
160 static const u16 NCT6775_REG_IN_MIN[] = {
161         0x2c, 0x2e, 0x30, 0x32, 0x34, 0x36, 0x38, 0x555, 0x557 };
162 static const u16 NCT6775_REG_IN[] = {
163         0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x550, 0x551, 0x552
164 };
165
166 static const u16 NCT6779_REG_IN[] = {
167         0x480, 0x481, 0x482, 0x483, 0x484, 0x485, 0x486, 0x487,
168         0x488, 0x489, 0x48a, 0x48b, 0x48c, 0x48d, 0x48e
169 };
170
171 #define NCT6775_REG_VBAT                0x5D
172 #define NCT6775_REG_DIODE               0x5E
173
174 #define NCT6775_REG_FANDIV1             0x506
175 #define NCT6775_REG_FANDIV2             0x507
176
177 #define NCT6775_REG_CR_FAN_DEBOUNCE     0xf0
178
179 static const u16 NCT6775_REG_ALARM[6] = { 0x459, 0x45A, 0x45B };
180 static const u16 NCT6779_REG_ALARM[6] = { 0x459, 0x45A, 0x45B, 0x568 };
181
182 #define NCT6775_REG_CASEOPEN            0x42
183
184 static const u8 NCT6775_CASEOPEN_MASK[] = { 0x10, 0x00 };
185 static const u8 NCT6776_CASEOPEN_MASK[] = { 0x10, 0x40 };
186
187 static const u8 NCT6775_REG_CR_CASEOPEN_CLR[] = { 0xe6, 0xee };
188 static const u8 NCT6775_CR_CASEOPEN_CLR_MASK[] = { 0x20, 0x01 };
189
190 /* DC or PWM output fan configuration */
191 static const u8 NCT6775_REG_PWM_MODE[] = { 0x04, 0x04, 0x12 };
192 static const u8 NCT6775_PWM_MODE_MASK[] = { 0x01, 0x02, 0x01 };
193
194 static const u8 NCT6776_REG_PWM_MODE[] = { 0x04, 0, 0 };
195 static const u8 NCT6776_PWM_MODE_MASK[] = { 0x01, 0, 0 };
196
197 /* Advanced Fan control, some values are common for all fans */
198
199 static const u16 NCT6775_REG_TARGET[] = { 0x101, 0x201, 0x301, 0x801, 0x901 };
200 static const u16 NCT6775_REG_FAN_MODE[] = { 0x102, 0x202, 0x302, 0x802, 0x902 };
201 static const u16 NCT6775_REG_FAN_STEP_DOWN_TIME[] = {
202         0x103, 0x203, 0x303, 0x803, 0x903 };
203 static const u16 NCT6775_REG_FAN_STEP_UP_TIME[] = {
204         0x104, 0x204, 0x304, 0x804, 0x904 };
205 static const u16 NCT6775_REG_FAN_STOP_OUTPUT[] = {
206         0x105, 0x205, 0x305, 0x805, 0x905 };
207 static const u16 NCT6775_REG_FAN_START_OUTPUT[]
208         = { 0x106, 0x206, 0x306, 0x806, 0x906 };
209 static const u16 NCT6775_REG_FAN_STOP_TIME[]
210         = { 0x107, 0x207, 0x307, 0x807, 0x907 };
211 static const u16 NCT6775_REG_PWM[] = { 0x109, 0x209, 0x309, 0x809, 0x909 };
212 static const u16 NCT6775_REG_PWM_READ[] = { 0x01, 0x03, 0x11, 0x13, 0x15 };
213
214 static const u16 NCT6775_REG_FAN[] = { 0x630, 0x632, 0x634, 0x636, 0x638 };
215
216 static const u16 NCT6776_REG_FAN_MIN[] = { 0x63a, 0x63c, 0x63e, 0x640, 0x642};
217
218 static const u16 NCT6779_REG_TOLERANCE_H[]
219         = { 0x10c, 0x20c, 0x30c, 0x40c, 0x50c };
220
221 static const u16 NCT6779_REG_FAN[] = { 0x4c0, 0x4c2, 0x4c4, 0x4c6, 0x4c8 };
222
223 static const u16 NCT6775_REG_TEMP[]
224         = { 0x27, 0x150, 0x250, 0x62b, 0x62c, 0x62d };
225 static const u16 NCT6779_REG_TEMP[] = { 0x27, 0x150 };
226 static const u16 NCT6775_REG_TEMP_CONFIG[]
227         = { 0, 0x152, 0x252, 0x628, 0x629, 0x62A };
228 static const u16 NCT6775_REG_TEMP_HYST[]
229         = { 0x3a, 0x153, 0x253, 0x673, 0x678, 0x67D };
230 static const u16 NCT6775_REG_TEMP_OVER[]
231         = { 0x39, 0x155, 0x255, 0x672, 0x677, 0x67C };
232
233 static const u16 NCT6775_REG_TEMP_SOURCE[]
234         = { 0x621, 0x622, 0x623, 0x624, 0x625, 0x626 };
235
236 static const u16 NCT6775_REG_TEMP_SEL[]
237         = { 0x100, 0x200, 0x300, 0x800, 0x900 };
238
239 static const u16 NCT6775_REG_WEIGHT_TEMP_SEL[]
240         = { 0x139, 0x239, 0x339, 0x839, 0x939 };
241 static const u16 NCT6775_REG_WEIGHT_TEMP_STEP[]
242         = { 0x13a, 0x23a, 0x33a, 0x83a, 0x93a };
243 static const u16 NCT6775_REG_WEIGHT_TEMP_STEP_TOL[]
244         = { 0x13b, 0x23b, 0x33b, 0x83b, 0x93b };
245 static const u16 NCT6775_REG_WEIGHT_DUTY_STEP[]
246         = { 0x13c, 0x23c, 0x33c, 0x83c, 0x93c };
247 static const u16 NCT6775_REG_WEIGHT_TEMP_BASE[]
248         = { 0x13d, 0x23d, 0x33d, 0x83d, 0x93d };
249
250 static const u16 NCT6776_REG_WEIGHT_DUTY_BASE[]
251         = { 0x13e, 0x23e, 0x33e, 0x83e, 0x93e };
252
253 static const u16 NCT6775_REG_TEMP_OFFSET[] = { 0x454, 0x455, 0x456 };
254
255 static const u16 NCT6779_REG_TEMP_OFFSET[]
256         = { 0x454, 0x455, 0x456, 0x44a, 0x44b, 0x44c };
257
258 static const u16 NCT6776_REG_TEMP_CONFIG[11]
259         = { 0x18, 0x152, 0x252, 0x628, 0x629, 0x62A };
260
261 static const u16 NCT6775_REG_TEMP_MON[] = { 0x73, 0x75, 0x77, 0x79, 0x7b };
262
263 static const u16 NCT6775_REG_AUTO_TEMP[]
264         = { 0x121, 0x221, 0x321, 0x821, 0x921 };
265 static const u16 NCT6775_REG_AUTO_PWM[]
266         = { 0x127, 0x227, 0x327, 0x827, 0x927 };
267
268 #define NCT6775_AUTO_TEMP(data, nr, p)  ((data)->REG_AUTO_TEMP[nr] + (p))
269 #define NCT6775_AUTO_PWM(data, nr, p)   ((data)->REG_AUTO_PWM[nr] + (p))
270
271 static const u16 NCT6775_REG_CRITICAL_ENAB[] = { 0x134, 0x234, 0x334 };
272
273 static const u16 NCT6775_REG_CRITICAL_TEMP[] = {
274         0x135, 0x235, 0x335, 0x835, 0x935 };
275 static const u16 NCT6775_REG_CRITICAL_TEMP_TOLERANCE[] = {
276         0x138, 0x238, 0x338, 0x838, 0x938 };
277
278 static const u16 NCT6779_REG_CRITICAL_PWM_ENABLE[] = {
279         0x136, 0x236, 0x336, 0x836, 0x936 };
280 static const u16 NCT6779_REG_CRITICAL_PWM[] = {
281         0x137, 0x237, 0x337, 0x837, 0x937 };
282
283 static const char *const nct6775_temp_label[] = {
284         "",
285         "SYSTIN",
286         "CPUTIN",
287         "AUXTIN",
288         "AMD SB-TSI",
289         "PECI Agent 0",
290         "PECI Agent 1",
291         "PECI Agent 2",
292         "PECI Agent 3",
293         "PECI Agent 4",
294         "PECI Agent 5",
295         "PECI Agent 6",
296         "PECI Agent 7",
297         "PCH_CHIP_CPU_MAX_TEMP",
298         "PCH_CHIP_TEMP",
299         "PCH_CPU_TEMP",
300         "PCH_MCH_TEMP",
301         "PCH_DIM0_TEMP",
302         "PCH_DIM1_TEMP",
303         "PCH_DIM2_TEMP",
304         "PCH_DIM3_TEMP"
305 };
306
307 static const char *const nct6776_temp_label[] = {
308         "",
309         "SYSTIN",
310         "CPUTIN",
311         "AUXTIN",
312         "SMBUSMASTER 0",
313         "SMBUSMASTER 1",
314         "SMBUSMASTER 2",
315         "SMBUSMASTER 3",
316         "SMBUSMASTER 4",
317         "SMBUSMASTER 5",
318         "SMBUSMASTER 6",
319         "SMBUSMASTER 7",
320         "PECI Agent 0",
321         "PECI Agent 1",
322         "PCH_CHIP_CPU_MAX_TEMP",
323         "PCH_CHIP_TEMP",
324         "PCH_CPU_TEMP",
325         "PCH_MCH_TEMP",
326         "PCH_DIM0_TEMP",
327         "PCH_DIM1_TEMP",
328         "PCH_DIM2_TEMP",
329         "PCH_DIM3_TEMP",
330         "BYTE_TEMP"
331 };
332
333 static const char *const nct6779_temp_label[] = {
334         "",
335         "SYSTIN",
336         "CPUTIN",
337         "AUXTIN0",
338         "AUXTIN1",
339         "AUXTIN2",
340         "AUXTIN3",
341         "",
342         "SMBUSMASTER 0",
343         "SMBUSMASTER 1",
344         "SMBUSMASTER 2",
345         "SMBUSMASTER 3",
346         "SMBUSMASTER 4",
347         "SMBUSMASTER 5",
348         "SMBUSMASTER 6",
349         "SMBUSMASTER 7",
350         "PECI Agent 0",
351         "PECI Agent 1",
352         "PCH_CHIP_CPU_MAX_TEMP",
353         "PCH_CHIP_TEMP",
354         "PCH_CPU_TEMP",
355         "PCH_MCH_TEMP",
356         "PCH_DIM0_TEMP",
357         "PCH_DIM1_TEMP",
358         "PCH_DIM2_TEMP",
359         "PCH_DIM3_TEMP",
360         "BYTE_TEMP"
361 };
362
363 #define NUM_REG_TEMP    ARRAY_SIZE(NCT6775_REG_TEMP)
364
365 static inline int reg_to_pwm_enable(int pwm, int mode)
366 {
367         if (mode == 0 && pwm == 255)
368                 return 0;       /* off  */
369         if (mode == 3)          /* SmartFan III */
370                 return 2;       /* convert to thermal cruise */
371         if (mode < 3)
372                 return mode + 1;
373         return 4;               /* SmartFan IV */
374 }
375
376 static inline int pwm_enable_to_reg(int mode)
377 {
378         if (mode == 0)
379                 return 0;
380         if (mode < 4)
381                 return mode - 1;
382         return 4;
383 }
384
385 /*
386  * Conversions
387  */
388
389 /* 1 is DC mode, output in ms */
390 static inline unsigned int step_time_from_reg(u8 reg, u8 mode)
391 {
392         return mode ? 400 * reg : 100 * reg;
393 }
394
395 static inline u8 step_time_to_reg(unsigned int msec, u8 mode)
396 {
397         return SENSORS_LIMIT((mode ? (msec + 200) / 400 :
398                                         (msec + 50) / 100), 1, 255);
399 }
400
401 static unsigned int fan_from_reg8(u16 reg, unsigned int divreg)
402 {
403         if (reg == 0 || reg == 255)
404                 return 0;
405         return 1350000U / (reg << divreg);
406 }
407
408 static unsigned int fan_from_reg13(u16 reg, unsigned int divreg)
409 {
410         if ((reg & 0xff1f) == 0xff1f)
411                 return 0;
412
413         reg = (reg & 0x1f) | ((reg & 0xff00) >> 3);
414
415         if (reg == 0)
416                 return 0;
417
418         return 1350000U / reg;
419 }
420
421 static unsigned int fan_from_reg16(u16 reg, unsigned int divreg)
422 {
423         if (reg == 0 || reg == 0xffff)
424                 return 0;
425
426         /*
427          * Even though the registers are 16 bit wide, the fan divisor
428          * still applies.
429          */
430         return 1350000U / (reg << divreg);
431 }
432
433 static inline unsigned int
434 div_from_reg(u8 reg)
435 {
436         return 1 << reg;
437 }
438
439 /*
440  * Some of the voltage inputs have internal scaling, the tables below
441  * contain 8 (the ADC LSB in mV) * scaling factor * 100
442  */
443 static const u16 scale_in[15] = {
444         800, 800, 1600, 1600, 800, 800, 800, 1600, 1600, 800, 800, 800, 800,
445         800, 800
446 };
447
448 static inline long in_from_reg(u8 reg, u8 nr)
449 {
450         return DIV_ROUND_CLOSEST(reg * scale_in[nr], 100);
451 }
452
453 static inline u8 in_to_reg(u32 val, u8 nr)
454 {
455         return SENSORS_LIMIT(DIV_ROUND_CLOSEST(val * 100, scale_in[nr]), 0,
456                              255);
457 }
458
459 /*
460  * Data structures and manipulation thereof
461  */
462
463 struct nct6775_data {
464         int addr;       /* IO base of hw monitor block */
465         enum kinds kind;
466         const char *name;
467
468         struct device *hwmon_dev;
469         struct mutex lock;
470
471         u16 reg_temp[3][NUM_REG_TEMP]; /* 0=temp, 1=temp_over, 2=temp_hyst */
472         u8 temp_src[NUM_REG_TEMP];
473         u16 reg_temp_config[NUM_REG_TEMP];
474         const char * const *temp_label;
475
476         u16 REG_CONFIG;
477         u16 REG_VBAT;
478         u16 REG_DIODE;
479
480         const u16 *REG_VIN;
481         const u16 *REG_IN_MINMAX[2];
482
483         const u16 *REG_TARGET;
484         const u16 *REG_FAN;
485         const u16 *REG_FAN_MODE;
486         const u16 *REG_FAN_MIN;
487         const u16 *REG_FAN_TIME[3];
488
489         const u8 *REG_PWM_MODE;
490         const u8 *PWM_MODE_MASK;
491
492         const u16 *REG_PWM[3];
493         const u16 *REG_PWM_READ;
494
495         const u16 *REG_TEMP_MON;
496         const u16 *REG_AUTO_TEMP;
497         const u16 *REG_AUTO_PWM;
498
499         const u16 *REG_CRITICAL_TEMP;
500         const u16 *REG_CRITICAL_TEMP_TOLERANCE;
501
502         const u16 *REG_TEMP_SOURCE;     /* temp register sources */
503         const u16 *REG_TEMP_SEL[2];     /* pwm temp, 0=base, 1=weight */
504
505         const u16 *REG_WEIGHT_TEMP[3];  /* 0=base, 1=hyst, 2=step */
506         const u16 *REG_WEIGHT_DUTY[2];  /* 0=step, 1=base */
507
508         const u16 *REG_TEMP_OFFSET;
509
510         const u16 *REG_ALARM;
511
512         u8 REG_CASEOPEN;
513         const u8 *CASEOPEN_MASK;
514
515         unsigned int (*fan_from_reg)(u16 reg, unsigned int divreg);
516         unsigned int (*fan_from_reg_min)(u16 reg, unsigned int divreg);
517
518         struct mutex update_lock;
519         char valid;             /* !=0 if following fields are valid */
520         unsigned long last_updated;     /* In jiffies */
521
522         /* Register values */
523         u8 bank;                /* current register bank */
524         u8 in_num;              /* number of in inputs we have */
525         u8 in[15][3];           /* [0]=in, [1]=in_max, [2]=in_min */
526         unsigned int rpm[5];
527         u16 fan_min[5];
528         u8 fan_div[5];
529         u8 has_pwm;
530         u8 has_fan;             /* some fan inputs can be disabled */
531         u8 has_fan_min;         /* some fans don't have min register */
532         bool has_fan_div;
533
534         u8 temp_num_fixed;      /* 3 or 6 */
535         u8 temp_type[6];
536         s8 temp_offset[6];
537         s16 temp[3][NUM_REG_TEMP]; /* 0=temp, 1=temp_over, 2=temp_hyst */
538         u64 alarms;
539         u8 caseopen;
540
541         u8 pwm_num;     /* number of pwm */
542
543         u8 pwm_mode[5]; /* 1->DC variable voltage, 0->PWM variable duty cycle */
544         u8 pwm_enable[5]; /* 0->off
545                            * 1->manual
546                            * 2->thermal cruise mode (also called SmartFan I)
547                            * 3->fan speed cruise mode
548                            * 4->enhanced variable thermal cruise (also called
549                            *    SmartFan IV)
550                            */
551         u8 pwm[3][5];   /* [0]=pwm, [1]=fan_start_output, [2]=fan_stop_output */
552         u8 target_temp[5];
553         s16 pwm_temp[5];
554         u8 tolerance[5][2];
555
556         u8 fan_time[3][5]; /* 0 = stop_time, 1 = step_up, 2 = step_down */
557
558         /* Automatic fan speed control registers */
559         int auto_pwm_num;
560         u8 auto_pwm[5][7];
561         u8 auto_temp[5][7];
562
563         u8 pwm_temp_sel[2][5];
564
565         u8 pwm_weight_enable[5]; /* 0->off, 1->on */
566         u8 weight_temp[3][5];   /* 0->temp_step, 1->temp_step_tol,
567                                  * 2->temp_base
568                                  */
569         u8 weight_duty[2][5];   /* 0->duty_step, 1->duty_base */
570
571         u8 vid;
572         u8 vrm;
573
574         u16 have_temp;
575         u16 have_in;
576 };
577
578 struct nct6775_sio_data {
579         int sioreg;
580         enum kinds kind;
581 };
582
583 static bool is_word_sized(struct nct6775_data *data, u16 reg)
584 {
585         switch (data->kind) {
586         case nct6775:
587         case nct6776:
588                 return (((reg & 0xff00) == 0x100 ||
589                     (reg & 0xff00) == 0x200) &&
590                    ((reg & 0x00ff) == 0x50 ||
591                     (reg & 0x00ff) == 0x53 ||
592                     (reg & 0x00ff) == 0x55)) ||
593                   (reg & 0xfff0) == 0x630 ||
594                   reg == 0x640 || reg == 0x642 ||
595                   ((reg & 0xfff0) == 0x650 && (reg & 0x000f) >= 0x06) ||
596                   reg == 0x73 || reg == 0x75 || reg == 0x77;
597         case nct6779:
598                 return reg == 0x150 || reg == 0x153 || reg == 0x155 ||
599                   ((reg & 0xfff0) == 0x4c0 && (reg & 0x000f) < 0x09) ||
600                   reg == 0x63a || reg == 0x63c || reg == 0x63e ||
601                   reg == 0x640 || reg == 0x642 ||
602                   reg == 0x73 || reg == 0x75 || reg == 0x77 || reg == 0x79 ||
603                   reg == 0x7b;
604         }
605         return false;
606 }
607
608 /*
609  * On older chips, only registers 0x50-0x5f are banked.
610  * On more recent chips, all registers are banked.
611  * Assume that is the case and set the bank number for each access.
612  * Cache the bank number so it only needs to be set if it changes.
613  */
614 static inline void nct6775_set_bank(struct nct6775_data *data, u16 reg)
615 {
616         u8 bank = reg >> 8;
617         if (data->bank != bank) {
618                 outb_p(NCT6775_REG_BANK, data->addr + ADDR_REG_OFFSET);
619                 outb_p(bank, data->addr + DATA_REG_OFFSET);
620                 data->bank = bank;
621         }
622 }
623
624 static u16 nct6775_read_value(struct nct6775_data *data, u16 reg)
625 {
626         int res, word_sized = is_word_sized(data, reg);
627
628         mutex_lock(&data->lock);
629
630         nct6775_set_bank(data, reg);
631         outb_p(reg & 0xff, data->addr + ADDR_REG_OFFSET);
632         res = inb_p(data->addr + DATA_REG_OFFSET);
633         if (word_sized) {
634                 outb_p((reg & 0xff) + 1,
635                        data->addr + ADDR_REG_OFFSET);
636                 res = (res << 8) + inb_p(data->addr + DATA_REG_OFFSET);
637         }
638
639         mutex_unlock(&data->lock);
640         return res;
641 }
642
643 static int nct6775_write_value(struct nct6775_data *data, u16 reg, u16 value)
644 {
645         int word_sized = is_word_sized(data, reg);
646
647         mutex_lock(&data->lock);
648
649         nct6775_set_bank(data, reg);
650         outb_p(reg & 0xff, data->addr + ADDR_REG_OFFSET);
651         if (word_sized) {
652                 outb_p(value >> 8, data->addr + DATA_REG_OFFSET);
653                 outb_p((reg & 0xff) + 1,
654                        data->addr + ADDR_REG_OFFSET);
655         }
656         outb_p(value & 0xff, data->addr + DATA_REG_OFFSET);
657
658         mutex_unlock(&data->lock);
659         return 0;
660 }
661
662 /* We left-align 8-bit temperature values to make the code simpler */
663 static u16 nct6775_read_temp(struct nct6775_data *data, u16 reg)
664 {
665         u16 res;
666
667         res = nct6775_read_value(data, reg);
668         if (!is_word_sized(data, reg))
669                 res <<= 8;
670
671         return res;
672 }
673
674 static int nct6775_write_temp(struct nct6775_data *data, u16 reg, u16 value)
675 {
676         if (!is_word_sized(data, reg))
677                 value >>= 8;
678         return nct6775_write_value(data, reg, value);
679 }
680
681 /* This function assumes that the caller holds data->update_lock */
682 static void nct6775_write_fan_div(struct nct6775_data *data, int nr)
683 {
684         u8 reg;
685
686         switch (nr) {
687         case 0:
688                 reg = (nct6775_read_value(data, NCT6775_REG_FANDIV1) & 0x70)
689                     | (data->fan_div[0] & 0x7);
690                 nct6775_write_value(data, NCT6775_REG_FANDIV1, reg);
691                 break;
692         case 1:
693                 reg = (nct6775_read_value(data, NCT6775_REG_FANDIV1) & 0x7)
694                     | ((data->fan_div[1] << 4) & 0x70);
695                 nct6775_write_value(data, NCT6775_REG_FANDIV1, reg);
696         case 2:
697                 reg = (nct6775_read_value(data, NCT6775_REG_FANDIV2) & 0x70)
698                     | (data->fan_div[2] & 0x7);
699                 nct6775_write_value(data, NCT6775_REG_FANDIV2, reg);
700                 break;
701         case 3:
702                 reg = (nct6775_read_value(data, NCT6775_REG_FANDIV2) & 0x7)
703                     | ((data->fan_div[3] << 4) & 0x70);
704                 nct6775_write_value(data, NCT6775_REG_FANDIV2, reg);
705                 break;
706         }
707 }
708
709 static void nct6775_write_fan_div_common(struct device *dev,
710                                          struct nct6775_data *data, int nr)
711 {
712         if (data->kind == nct6775)
713                 nct6775_write_fan_div(data, nr);
714 }
715
716 static void nct6775_update_fan_div(struct nct6775_data *data)
717 {
718         u8 i;
719
720         i = nct6775_read_value(data, NCT6775_REG_FANDIV1);
721         data->fan_div[0] = i & 0x7;
722         data->fan_div[1] = (i & 0x70) >> 4;
723         i = nct6775_read_value(data, NCT6775_REG_FANDIV2);
724         data->fan_div[2] = i & 0x7;
725         if (data->has_fan & (1<<3))
726                 data->fan_div[3] = (i & 0x70) >> 4;
727 }
728
729 static void nct6775_update_fan_div_common(struct device *dev,
730                                           struct nct6775_data *data)
731 {
732         if (data->kind == nct6775)
733                 nct6775_update_fan_div(data);
734 }
735
736 static void nct6775_update_pwm(struct device *dev)
737 {
738         struct nct6775_data *data = dev_get_drvdata(dev);
739         int i, j;
740         int fanmodecfg, reg;
741         bool duty_is_dc;
742
743         for (i = 0; i < data->pwm_num; i++) {
744                 if (!(data->has_pwm & (1 << i)))
745                         continue;
746
747                 duty_is_dc = data->REG_PWM_MODE[i] &&
748                   (nct6775_read_value(data, data->REG_PWM_MODE[i])
749                    & data->PWM_MODE_MASK[i]);
750                 data->pwm_mode[i] = duty_is_dc;
751
752                 fanmodecfg = nct6775_read_value(data, data->REG_FAN_MODE[i]);
753                 for (j = 0; j < 3; j++)
754                         data->pwm[j][i]
755                           = nct6775_read_value(data, data->REG_PWM[j][i]);
756
757                 data->pwm_enable[i] = reg_to_pwm_enable(data->pwm[0][i],
758                                                         (fanmodecfg >> 4) & 7);
759
760                 data->tolerance[i][0] = fanmodecfg & 0x0f;
761                 if (data->kind == nct6779) {
762                         reg = nct6775_read_value(data,
763                                                  NCT6779_REG_TOLERANCE_H[i]);
764                         data->tolerance[i][0] |= (reg & 0x70) >> 1;
765                 }
766                 data->tolerance[i][1] =
767                         nct6775_read_value(data,
768                                         data->REG_CRITICAL_TEMP_TOLERANCE[i]);
769
770                 data->pwm_weight_enable[i] =
771                         nct6775_read_value(data, data->REG_TEMP_SEL[1][i])
772                                 & 0x80 ? 1 : 0;
773
774                 /* Weight data */
775                 for (j = 0; j < 2; j++) {
776                         reg = nct6775_read_value(data,
777                                                  data->REG_TEMP_SEL[j][i]);
778                         data->pwm_temp_sel[j][i] = reg & 0x1f;
779                 }
780                 /* Weight temp data */
781                 for (j = 0; j < 3; j++) {
782                         data->weight_temp[j][i]
783                           = nct6775_read_value(data,
784                                                data->REG_WEIGHT_TEMP[j][i]);
785                 }
786                 /* Weight duty (pwm) data */
787                 for (j = 0; j < 2; j++) {
788                         if (!data->REG_WEIGHT_DUTY[j])
789                                 continue;
790                         data->weight_duty[j][i]
791                           = nct6775_read_value(data,
792                                                data->REG_WEIGHT_DUTY[j][i]);
793                 }
794         }
795 }
796
797 static void nct6775_update_pwm_limits(struct device *dev)
798 {
799         struct nct6775_data *data = dev_get_drvdata(dev);
800         int i, j;
801         u8 reg;
802
803         for (i = 0; i < data->pwm_num; i++) {
804                 if (!(data->has_pwm & (1 << i)))
805                         continue;
806
807                 for (j = 0; j < 3; j++) {
808                         data->fan_time[j][i] =
809                           nct6775_read_value(data, data->REG_FAN_TIME[j][i]);
810                 }
811
812                 data->target_temp[i] =
813                         nct6775_read_value(data, data->REG_TARGET[i]) &
814                                         (data->pwm_mode[i] ? 0xff : 0x7f);
815                 data->pwm_temp[i] =
816                         nct6775_read_value(data, data->REG_TEMP_MON[i]);
817
818                 for (j = 0; j < data->auto_pwm_num; j++) {
819                         data->auto_pwm[i][j] =
820                           nct6775_read_value(data,
821                                              NCT6775_AUTO_PWM(data, i, j));
822                         data->auto_temp[i][j] =
823                           nct6775_read_value(data,
824                                              NCT6775_AUTO_TEMP(data, i, j));
825                 }
826
827                 /* critical auto_pwm temperature data */
828                 data->auto_temp[i][data->auto_pwm_num] =
829                         nct6775_read_value(data, data->REG_CRITICAL_TEMP[i]);
830
831                 switch (data->kind) {
832                 case nct6775:
833                         reg = nct6775_read_value(data,
834                                                  NCT6775_REG_CRITICAL_ENAB[i]);
835                         data->auto_pwm[i][data->auto_pwm_num] =
836                                                 (reg & 0x02) ? 0xff : 0x00;
837                         break;
838                 case nct6776:
839                         data->auto_pwm[i][data->auto_pwm_num] = 0xff;
840                         break;
841                 case nct6779:
842                         reg = nct6775_read_value(data,
843                                         NCT6779_REG_CRITICAL_PWM_ENABLE[i]);
844                         if (reg & 1)
845                                 data->auto_pwm[i][data->auto_pwm_num] =
846                                   nct6775_read_value(data,
847                                         NCT6779_REG_CRITICAL_PWM[i]);
848                         else
849                                 data->auto_pwm[i][data->auto_pwm_num] = 0xff;
850                         break;
851                 }
852         }
853 }
854
855 static struct nct6775_data *nct6775_update_device(struct device *dev)
856 {
857         struct nct6775_data *data = dev_get_drvdata(dev);
858         int i, j;
859
860         mutex_lock(&data->update_lock);
861
862         if (time_after(jiffies, data->last_updated + HZ + HZ/2)
863          || !data->valid) {
864                 /* Fan clock dividers */
865                 nct6775_update_fan_div_common(dev, data);
866
867                 /* Measured voltages and limits */
868                 for (i = 0; i < data->in_num; i++) {
869                         if (!(data->have_in & (1 << i)))
870                                 continue;
871
872                         data->in[i][0] = nct6775_read_value(data,
873                                                             data->REG_VIN[i]);
874                         data->in[i][1] = nct6775_read_value(data,
875                                           data->REG_IN_MINMAX[0][i]);
876                         data->in[i][2] = nct6775_read_value(data,
877                                           data->REG_IN_MINMAX[1][i]);
878                 }
879
880                 /* Measured fan speeds and limits */
881                 for (i = 0; i < 5; i++) {
882                         u16 reg;
883
884                         if (!(data->has_fan & (1 << i)))
885                                 continue;
886
887                         reg = nct6775_read_value(data, data->REG_FAN[i]);
888                         data->rpm[i] = data->fan_from_reg(reg,
889                                                           data->fan_div[i]);
890
891                         if (data->has_fan_min & (1 << i))
892                                 data->fan_min[i] = nct6775_read_value(data,
893                                            data->REG_FAN_MIN[i]);
894
895                         /*
896                          * If we failed to measure the fan speed and clock
897                          * divider can be increased, let's try that for next
898                          * time
899                          */
900                         if (data->has_fan_div
901                             && (reg >= 0xff || (data->kind == nct6775
902                                                 && reg == 0x00))
903                             && data->fan_div[i] < 0x07) {
904                                 dev_dbg(dev, "Increasing fan%d "
905                                         "clock divider from %u to %u\n",
906                                         i + 1, div_from_reg(data->fan_div[i]),
907                                         div_from_reg(data->fan_div[i] + 1));
908                                 data->fan_div[i]++;
909                                 nct6775_write_fan_div_common(dev, data, i);
910                                 /* Preserve min limit if possible */
911                                 if ((data->has_fan_min & (1 << i))
912                                  && data->fan_min[i] >= 2
913                                  && data->fan_min[i] != 255)
914                                         nct6775_write_value(data,
915                                                 data->REG_FAN_MIN[i],
916                                                 (data->fan_min[i] /= 2));
917                         }
918                 }
919
920                 nct6775_update_pwm(dev);
921                 nct6775_update_pwm_limits(dev);
922
923                 /* Measured temperatures and limits */
924                 for (i = 0; i < NUM_REG_TEMP; i++) {
925                         if (!(data->have_temp & (1 << i)))
926                                 continue;
927                         for (j = 0; j < 3; j++) {
928                                 if (data->reg_temp[j][i])
929                                         data->temp[j][i]
930                                           = nct6775_read_temp(data,
931                                                 data->reg_temp[j][i]);
932                         }
933                         if (i > data->temp_num_fixed)
934                                 continue;
935                         data->temp_offset[i]
936                           = nct6775_read_value(data, data->REG_TEMP_OFFSET[i]);
937                 }
938
939                 data->alarms = 0;
940                 for (i = 0; i < 6; i++) {
941                         u8 alarm;
942                         if (!data->REG_ALARM[i])
943                                 continue;
944                         alarm = nct6775_read_value(data, data->REG_ALARM[i]);
945                         data->alarms |= (((u64)alarm) << (i << 3));
946                 }
947
948                 data->caseopen = nct6775_read_value(data, data->REG_CASEOPEN);
949
950                 data->last_updated = jiffies;
951                 data->valid = 1;
952         }
953
954         mutex_unlock(&data->update_lock);
955         return data;
956 }
957
958 /*
959  * Sysfs callback functions
960  */
961 static ssize_t
962 show_in_reg(struct device *dev, struct device_attribute *attr, char *buf)
963 {
964         struct nct6775_data *data = nct6775_update_device(dev);
965         struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
966         int nr = sattr->nr;
967         int index = sattr->index;
968         return sprintf(buf, "%ld\n", in_from_reg(data->in[nr][index], nr));
969 }
970
971 static ssize_t
972 store_in_reg(struct device *dev, struct device_attribute *attr, const char *buf,
973              size_t count)
974 {
975         struct nct6775_data *data = dev_get_drvdata(dev);
976         struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
977         int nr = sattr->nr;
978         int index = sattr->index;
979         unsigned long val;
980         int err = kstrtoul(buf, 10, &val);
981         if (err < 0)
982                 return err;
983         mutex_lock(&data->update_lock);
984         data->in[nr][index] = in_to_reg(val, nr);
985         nct6775_write_value(data, data->REG_IN_MINMAX[index-1][nr],
986                             data->in[nr][index]);
987         mutex_unlock(&data->update_lock);
988         return count;
989 }
990
991 static ssize_t
992 show_alarm(struct device *dev, struct device_attribute *attr, char *buf)
993 {
994         struct nct6775_data *data = nct6775_update_device(dev);
995         struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr);
996         int nr = sattr->index;
997         return sprintf(buf, "%u\n",
998                        (unsigned int)((data->alarms >> nr) & 0x01));
999 }
1000
1001 static SENSOR_DEVICE_ATTR_2(in0_input, S_IRUGO, show_in_reg, NULL, 0, 0);
1002 static SENSOR_DEVICE_ATTR_2(in1_input, S_IRUGO, show_in_reg, NULL, 1, 0);
1003 static SENSOR_DEVICE_ATTR_2(in2_input, S_IRUGO, show_in_reg, NULL, 2, 0);
1004 static SENSOR_DEVICE_ATTR_2(in3_input, S_IRUGO, show_in_reg, NULL, 3, 0);
1005 static SENSOR_DEVICE_ATTR_2(in4_input, S_IRUGO, show_in_reg, NULL, 4, 0);
1006 static SENSOR_DEVICE_ATTR_2(in5_input, S_IRUGO, show_in_reg, NULL, 5, 0);
1007 static SENSOR_DEVICE_ATTR_2(in6_input, S_IRUGO, show_in_reg, NULL, 6, 0);
1008 static SENSOR_DEVICE_ATTR_2(in7_input, S_IRUGO, show_in_reg, NULL, 7, 0);
1009 static SENSOR_DEVICE_ATTR_2(in8_input, S_IRUGO, show_in_reg, NULL, 8, 0);
1010 static SENSOR_DEVICE_ATTR_2(in9_input, S_IRUGO, show_in_reg, NULL, 9, 0);
1011 static SENSOR_DEVICE_ATTR_2(in10_input, S_IRUGO, show_in_reg, NULL, 10, 0);
1012 static SENSOR_DEVICE_ATTR_2(in11_input, S_IRUGO, show_in_reg, NULL, 11, 0);
1013 static SENSOR_DEVICE_ATTR_2(in12_input, S_IRUGO, show_in_reg, NULL, 12, 0);
1014 static SENSOR_DEVICE_ATTR_2(in13_input, S_IRUGO, show_in_reg, NULL, 13, 0);
1015 static SENSOR_DEVICE_ATTR_2(in14_input, S_IRUGO, show_in_reg, NULL, 14, 0);
1016
1017 static SENSOR_DEVICE_ATTR(in0_alarm, S_IRUGO, show_alarm, NULL, 0);
1018 static SENSOR_DEVICE_ATTR(in1_alarm, S_IRUGO, show_alarm, NULL, 1);
1019 static SENSOR_DEVICE_ATTR(in2_alarm, S_IRUGO, show_alarm, NULL, 2);
1020 static SENSOR_DEVICE_ATTR(in3_alarm, S_IRUGO, show_alarm, NULL, 3);
1021 static SENSOR_DEVICE_ATTR(in4_alarm, S_IRUGO, show_alarm, NULL, 8);
1022 static SENSOR_DEVICE_ATTR(in5_alarm, S_IRUGO, show_alarm, NULL, 21);
1023 static SENSOR_DEVICE_ATTR(in6_alarm, S_IRUGO, show_alarm, NULL, 20);
1024 static SENSOR_DEVICE_ATTR(in7_alarm, S_IRUGO, show_alarm, NULL, 16);
1025 static SENSOR_DEVICE_ATTR(in8_alarm, S_IRUGO, show_alarm, NULL, 17);
1026 static SENSOR_DEVICE_ATTR(in9_alarm, S_IRUGO, show_alarm, NULL, 24);
1027 static SENSOR_DEVICE_ATTR(in10_alarm, S_IRUGO, show_alarm, NULL, 25);
1028 static SENSOR_DEVICE_ATTR(in11_alarm, S_IRUGO, show_alarm, NULL, 26);
1029 static SENSOR_DEVICE_ATTR(in12_alarm, S_IRUGO, show_alarm, NULL, 27);
1030 static SENSOR_DEVICE_ATTR(in13_alarm, S_IRUGO, show_alarm, NULL, 28);
1031 static SENSOR_DEVICE_ATTR(in14_alarm, S_IRUGO, show_alarm, NULL, 29);
1032
1033 static SENSOR_DEVICE_ATTR_2(in0_min, S_IWUSR | S_IRUGO, show_in_reg,
1034                             store_in_reg, 0, 1);
1035 static SENSOR_DEVICE_ATTR_2(in1_min, S_IWUSR | S_IRUGO, show_in_reg,
1036                             store_in_reg, 1, 1);
1037 static SENSOR_DEVICE_ATTR_2(in2_min, S_IWUSR | S_IRUGO, show_in_reg,
1038                             store_in_reg, 2, 1);
1039 static SENSOR_DEVICE_ATTR_2(in3_min, S_IWUSR | S_IRUGO, show_in_reg,
1040                             store_in_reg, 3, 1);
1041 static SENSOR_DEVICE_ATTR_2(in4_min, S_IWUSR | S_IRUGO, show_in_reg,
1042                             store_in_reg, 4, 1);
1043 static SENSOR_DEVICE_ATTR_2(in5_min, S_IWUSR | S_IRUGO, show_in_reg,
1044                             store_in_reg, 5, 1);
1045 static SENSOR_DEVICE_ATTR_2(in6_min, S_IWUSR | S_IRUGO, show_in_reg,
1046                             store_in_reg, 6, 1);
1047 static SENSOR_DEVICE_ATTR_2(in7_min, S_IWUSR | S_IRUGO, show_in_reg,
1048                             store_in_reg, 7, 1);
1049 static SENSOR_DEVICE_ATTR_2(in8_min, S_IWUSR | S_IRUGO, show_in_reg,
1050                             store_in_reg, 8, 1);
1051 static SENSOR_DEVICE_ATTR_2(in9_min, S_IWUSR | S_IRUGO, show_in_reg,
1052                             store_in_reg, 9, 1);
1053 static SENSOR_DEVICE_ATTR_2(in10_min, S_IWUSR | S_IRUGO, show_in_reg,
1054                             store_in_reg, 10, 1);
1055 static SENSOR_DEVICE_ATTR_2(in11_min, S_IWUSR | S_IRUGO, show_in_reg,
1056                             store_in_reg, 11, 1);
1057 static SENSOR_DEVICE_ATTR_2(in12_min, S_IWUSR | S_IRUGO, show_in_reg,
1058                             store_in_reg, 12, 1);
1059 static SENSOR_DEVICE_ATTR_2(in13_min, S_IWUSR | S_IRUGO, show_in_reg,
1060                             store_in_reg, 13, 1);
1061 static SENSOR_DEVICE_ATTR_2(in14_min, S_IWUSR | S_IRUGO, show_in_reg,
1062                             store_in_reg, 14, 1);
1063
1064 static SENSOR_DEVICE_ATTR_2(in0_max, S_IWUSR | S_IRUGO, show_in_reg,
1065                           store_in_reg, 0, 2);
1066 static SENSOR_DEVICE_ATTR_2(in1_max, S_IWUSR | S_IRUGO, show_in_reg,
1067                           store_in_reg, 1, 2);
1068 static SENSOR_DEVICE_ATTR_2(in2_max, S_IWUSR | S_IRUGO, show_in_reg,
1069                           store_in_reg, 2, 2);
1070 static SENSOR_DEVICE_ATTR_2(in3_max, S_IWUSR | S_IRUGO, show_in_reg,
1071                           store_in_reg, 3, 2);
1072 static SENSOR_DEVICE_ATTR_2(in4_max, S_IWUSR | S_IRUGO, show_in_reg,
1073                           store_in_reg, 4, 2);
1074 static SENSOR_DEVICE_ATTR_2(in5_max, S_IWUSR | S_IRUGO, show_in_reg,
1075                           store_in_reg, 5, 2);
1076 static SENSOR_DEVICE_ATTR_2(in6_max, S_IWUSR | S_IRUGO, show_in_reg,
1077                           store_in_reg, 6, 2);
1078 static SENSOR_DEVICE_ATTR_2(in7_max, S_IWUSR | S_IRUGO, show_in_reg,
1079                           store_in_reg, 7, 2);
1080 static SENSOR_DEVICE_ATTR_2(in8_max, S_IWUSR | S_IRUGO, show_in_reg,
1081                           store_in_reg, 8, 2);
1082 static SENSOR_DEVICE_ATTR_2(in9_max, S_IWUSR | S_IRUGO, show_in_reg,
1083                           store_in_reg, 9, 2);
1084 static SENSOR_DEVICE_ATTR_2(in10_max, S_IWUSR | S_IRUGO, show_in_reg,
1085                           store_in_reg, 10, 2);
1086 static SENSOR_DEVICE_ATTR_2(in11_max, S_IWUSR | S_IRUGO, show_in_reg,
1087                           store_in_reg, 11, 2);
1088 static SENSOR_DEVICE_ATTR_2(in12_max, S_IWUSR | S_IRUGO, show_in_reg,
1089                           store_in_reg, 12, 2);
1090 static SENSOR_DEVICE_ATTR_2(in13_max, S_IWUSR | S_IRUGO, show_in_reg,
1091                           store_in_reg, 13, 2);
1092 static SENSOR_DEVICE_ATTR_2(in14_max, S_IWUSR | S_IRUGO, show_in_reg,
1093                           store_in_reg, 14, 2);
1094
1095 static struct attribute *nct6775_attributes_in[15][5] = {
1096         {
1097                 &sensor_dev_attr_in0_input.dev_attr.attr,
1098                 &sensor_dev_attr_in0_min.dev_attr.attr,
1099                 &sensor_dev_attr_in0_max.dev_attr.attr,
1100                 &sensor_dev_attr_in0_alarm.dev_attr.attr,
1101                 NULL
1102         },
1103         {
1104                 &sensor_dev_attr_in1_input.dev_attr.attr,
1105                 &sensor_dev_attr_in1_min.dev_attr.attr,
1106                 &sensor_dev_attr_in1_max.dev_attr.attr,
1107                 &sensor_dev_attr_in1_alarm.dev_attr.attr,
1108                 NULL
1109         },
1110         {
1111                 &sensor_dev_attr_in2_input.dev_attr.attr,
1112                 &sensor_dev_attr_in2_min.dev_attr.attr,
1113                 &sensor_dev_attr_in2_max.dev_attr.attr,
1114                 &sensor_dev_attr_in2_alarm.dev_attr.attr,
1115                 NULL
1116         },
1117         {
1118                 &sensor_dev_attr_in3_input.dev_attr.attr,
1119                 &sensor_dev_attr_in3_min.dev_attr.attr,
1120                 &sensor_dev_attr_in3_max.dev_attr.attr,
1121                 &sensor_dev_attr_in3_alarm.dev_attr.attr,
1122                 NULL
1123         },
1124         {
1125                 &sensor_dev_attr_in4_input.dev_attr.attr,
1126                 &sensor_dev_attr_in4_min.dev_attr.attr,
1127                 &sensor_dev_attr_in4_max.dev_attr.attr,
1128                 &sensor_dev_attr_in4_alarm.dev_attr.attr,
1129                 NULL
1130         },
1131         {
1132                 &sensor_dev_attr_in5_input.dev_attr.attr,
1133                 &sensor_dev_attr_in5_min.dev_attr.attr,
1134                 &sensor_dev_attr_in5_max.dev_attr.attr,
1135                 &sensor_dev_attr_in5_alarm.dev_attr.attr,
1136                 NULL
1137         },
1138         {
1139                 &sensor_dev_attr_in6_input.dev_attr.attr,
1140                 &sensor_dev_attr_in6_min.dev_attr.attr,
1141                 &sensor_dev_attr_in6_max.dev_attr.attr,
1142                 &sensor_dev_attr_in6_alarm.dev_attr.attr,
1143                 NULL
1144         },
1145         {
1146                 &sensor_dev_attr_in7_input.dev_attr.attr,
1147                 &sensor_dev_attr_in7_min.dev_attr.attr,
1148                 &sensor_dev_attr_in7_max.dev_attr.attr,
1149                 &sensor_dev_attr_in7_alarm.dev_attr.attr,
1150                 NULL
1151         },
1152         {
1153                 &sensor_dev_attr_in8_input.dev_attr.attr,
1154                 &sensor_dev_attr_in8_min.dev_attr.attr,
1155                 &sensor_dev_attr_in8_max.dev_attr.attr,
1156                 &sensor_dev_attr_in8_alarm.dev_attr.attr,
1157                 NULL
1158         },
1159         {
1160                 &sensor_dev_attr_in9_input.dev_attr.attr,
1161                 &sensor_dev_attr_in9_min.dev_attr.attr,
1162                 &sensor_dev_attr_in9_max.dev_attr.attr,
1163                 &sensor_dev_attr_in9_alarm.dev_attr.attr,
1164                 NULL
1165         },
1166         {
1167                 &sensor_dev_attr_in10_input.dev_attr.attr,
1168                 &sensor_dev_attr_in10_min.dev_attr.attr,
1169                 &sensor_dev_attr_in10_max.dev_attr.attr,
1170                 &sensor_dev_attr_in10_alarm.dev_attr.attr,
1171                 NULL
1172         },
1173         {
1174                 &sensor_dev_attr_in11_input.dev_attr.attr,
1175                 &sensor_dev_attr_in11_min.dev_attr.attr,
1176                 &sensor_dev_attr_in11_max.dev_attr.attr,
1177                 &sensor_dev_attr_in11_alarm.dev_attr.attr,
1178                 NULL
1179         },
1180         {
1181                 &sensor_dev_attr_in12_input.dev_attr.attr,
1182                 &sensor_dev_attr_in12_min.dev_attr.attr,
1183                 &sensor_dev_attr_in12_max.dev_attr.attr,
1184                 &sensor_dev_attr_in12_alarm.dev_attr.attr,
1185                 NULL
1186         },
1187         {
1188                 &sensor_dev_attr_in13_input.dev_attr.attr,
1189                 &sensor_dev_attr_in13_min.dev_attr.attr,
1190                 &sensor_dev_attr_in13_max.dev_attr.attr,
1191                 &sensor_dev_attr_in13_alarm.dev_attr.attr,
1192                 NULL
1193         },
1194         {
1195                 &sensor_dev_attr_in14_input.dev_attr.attr,
1196                 &sensor_dev_attr_in14_min.dev_attr.attr,
1197                 &sensor_dev_attr_in14_max.dev_attr.attr,
1198                 &sensor_dev_attr_in14_alarm.dev_attr.attr,
1199                 NULL
1200         },
1201 };
1202
1203 static const struct attribute_group nct6775_group_in[15] = {
1204         { .attrs = nct6775_attributes_in[0] },
1205         { .attrs = nct6775_attributes_in[1] },
1206         { .attrs = nct6775_attributes_in[2] },
1207         { .attrs = nct6775_attributes_in[3] },
1208         { .attrs = nct6775_attributes_in[4] },
1209         { .attrs = nct6775_attributes_in[5] },
1210         { .attrs = nct6775_attributes_in[6] },
1211         { .attrs = nct6775_attributes_in[7] },
1212         { .attrs = nct6775_attributes_in[8] },
1213         { .attrs = nct6775_attributes_in[9] },
1214         { .attrs = nct6775_attributes_in[10] },
1215         { .attrs = nct6775_attributes_in[11] },
1216         { .attrs = nct6775_attributes_in[12] },
1217         { .attrs = nct6775_attributes_in[13] },
1218         { .attrs = nct6775_attributes_in[14] },
1219 };
1220
1221 static ssize_t
1222 show_fan(struct device *dev, struct device_attribute *attr, char *buf)
1223 {
1224         struct nct6775_data *data = nct6775_update_device(dev);
1225         struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr);
1226         int nr = sattr->index;
1227         return sprintf(buf, "%d\n", data->rpm[nr]);
1228 }
1229
1230 static ssize_t
1231 show_fan_min(struct device *dev, struct device_attribute *attr, char *buf)
1232 {
1233         struct nct6775_data *data = nct6775_update_device(dev);
1234         struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr);
1235         int nr = sattr->index;
1236         return sprintf(buf, "%d\n",
1237                        data->fan_from_reg_min(data->fan_min[nr],
1238                                               data->fan_div[nr]));
1239 }
1240
1241 static ssize_t
1242 show_fan_div(struct device *dev, struct device_attribute *attr, char *buf)
1243 {
1244         struct nct6775_data *data = nct6775_update_device(dev);
1245         struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr);
1246         int nr = sattr->index;
1247         return sprintf(buf, "%u\n", div_from_reg(data->fan_div[nr]));
1248 }
1249
1250 static ssize_t
1251 store_fan_min(struct device *dev, struct device_attribute *attr,
1252               const char *buf, size_t count)
1253 {
1254         struct nct6775_data *data = dev_get_drvdata(dev);
1255         struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr);
1256         int nr = sattr->index;
1257         unsigned long val;
1258         int err;
1259         unsigned int reg;
1260         u8 new_div;
1261
1262         err = kstrtoul(buf, 10, &val);
1263         if (err < 0)
1264                 return err;
1265
1266         mutex_lock(&data->update_lock);
1267         if (!data->has_fan_div) {
1268                 /*
1269                  * Only NCT6776F for now, so we know that this is a 13 bit
1270                  * register
1271                  */
1272                 if (!val) {
1273                         val = 0xff1f;
1274                 } else {
1275                         if (val > 1350000U)
1276                                 val = 135000U;
1277                         val = 1350000U / val;
1278                         val = (val & 0x1f) | ((val << 3) & 0xff00);
1279                 }
1280                 data->fan_min[nr] = val;
1281                 goto write_min; /* Leave fan divider alone */
1282         }
1283         if (!val) {
1284                 /* No min limit, alarm disabled */
1285                 data->fan_min[nr] = 255;
1286                 new_div = data->fan_div[nr]; /* No change */
1287                 dev_info(dev, "fan%u low limit and alarm disabled\n", nr + 1);
1288                 goto write_div;
1289         }
1290         reg = 1350000U / val;
1291         if (reg >= 128 * 255) {
1292                 /*
1293                  * Speed below this value cannot possibly be represented,
1294                  * even with the highest divider (128)
1295                  */
1296                 data->fan_min[nr] = 254;
1297                 new_div = 7; /* 128 == (1 << 7) */
1298                 dev_warn(dev, "fan%u low limit %lu below minimum %u, set to "
1299                          "minimum\n", nr + 1, val,
1300                          data->fan_from_reg_min(254, 7));
1301         } else if (!reg) {
1302                 /*
1303                  * Speed above this value cannot possibly be represented,
1304                  * even with the lowest divider (1)
1305                  */
1306                 data->fan_min[nr] = 1;
1307                 new_div = 0; /* 1 == (1 << 0) */
1308                 dev_warn(dev, "fan%u low limit %lu above maximum %u, set to "
1309                          "maximum\n", nr + 1, val,
1310                          data->fan_from_reg_min(1, 0));
1311         } else {
1312                 /*
1313                  * Automatically pick the best divider, i.e. the one such
1314                  * that the min limit will correspond to a register value
1315                  * in the 96..192 range
1316                  */
1317                 new_div = 0;
1318                 while (reg > 192 && new_div < 7) {
1319                         reg >>= 1;
1320                         new_div++;
1321                 }
1322                 data->fan_min[nr] = reg;
1323         }
1324
1325 write_div:
1326         /*
1327          * Write both the fan clock divider (if it changed) and the new
1328          * fan min (unconditionally)
1329          */
1330         if (new_div != data->fan_div[nr]) {
1331                 dev_dbg(dev, "fan%u clock divider changed from %u to %u\n",
1332                         nr + 1, div_from_reg(data->fan_div[nr]),
1333                         div_from_reg(new_div));
1334                 data->fan_div[nr] = new_div;
1335                 nct6775_write_fan_div_common(dev, data, nr);
1336                 /* Give the chip time to sample a new speed value */
1337                 data->last_updated = jiffies;
1338         }
1339
1340 write_min:
1341         nct6775_write_value(data, data->REG_FAN_MIN[nr],
1342                               data->fan_min[nr]);
1343         mutex_unlock(&data->update_lock);
1344
1345         return count;
1346 }
1347
1348 static struct sensor_device_attribute sda_fan_input[] = {
1349         SENSOR_ATTR(fan1_input, S_IRUGO, show_fan, NULL, 0),
1350         SENSOR_ATTR(fan2_input, S_IRUGO, show_fan, NULL, 1),
1351         SENSOR_ATTR(fan3_input, S_IRUGO, show_fan, NULL, 2),
1352         SENSOR_ATTR(fan4_input, S_IRUGO, show_fan, NULL, 3),
1353         SENSOR_ATTR(fan5_input, S_IRUGO, show_fan, NULL, 4),
1354 };
1355
1356 static struct sensor_device_attribute sda_fan_alarm[] = {
1357         SENSOR_ATTR(fan1_alarm, S_IRUGO, show_alarm, NULL, 6),
1358         SENSOR_ATTR(fan2_alarm, S_IRUGO, show_alarm, NULL, 7),
1359         SENSOR_ATTR(fan3_alarm, S_IRUGO, show_alarm, NULL, 11),
1360         SENSOR_ATTR(fan4_alarm, S_IRUGO, show_alarm, NULL, 10),
1361         SENSOR_ATTR(fan5_alarm, S_IRUGO, show_alarm, NULL, 23),
1362 };
1363
1364 static struct sensor_device_attribute sda_fan_min[] = {
1365         SENSOR_ATTR(fan1_min, S_IWUSR | S_IRUGO, show_fan_min,
1366                     store_fan_min, 0),
1367         SENSOR_ATTR(fan2_min, S_IWUSR | S_IRUGO, show_fan_min,
1368                     store_fan_min, 1),
1369         SENSOR_ATTR(fan3_min, S_IWUSR | S_IRUGO, show_fan_min,
1370                     store_fan_min, 2),
1371         SENSOR_ATTR(fan4_min, S_IWUSR | S_IRUGO, show_fan_min,
1372                     store_fan_min, 3),
1373         SENSOR_ATTR(fan5_min, S_IWUSR | S_IRUGO, show_fan_min,
1374                     store_fan_min, 4),
1375 };
1376
1377 static struct sensor_device_attribute sda_fan_div[] = {
1378         SENSOR_ATTR(fan1_div, S_IRUGO, show_fan_div, NULL, 0),
1379         SENSOR_ATTR(fan2_div, S_IRUGO, show_fan_div, NULL, 1),
1380         SENSOR_ATTR(fan3_div, S_IRUGO, show_fan_div, NULL, 2),
1381         SENSOR_ATTR(fan4_div, S_IRUGO, show_fan_div, NULL, 3),
1382         SENSOR_ATTR(fan5_div, S_IRUGO, show_fan_div, NULL, 4),
1383 };
1384
1385 static ssize_t
1386 show_temp_label(struct device *dev, struct device_attribute *attr, char *buf)
1387 {
1388         struct nct6775_data *data = nct6775_update_device(dev);
1389         struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr);
1390         int nr = sattr->index;
1391         return sprintf(buf, "%s\n", data->temp_label[data->temp_src[nr]]);
1392 }
1393
1394 static ssize_t
1395 show_temp(struct device *dev, struct device_attribute *attr, char *buf)
1396 {
1397         struct nct6775_data *data = nct6775_update_device(dev);
1398         struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
1399         int nr = sattr->nr;
1400         int index = sattr->index;
1401
1402         return sprintf(buf, "%d\n", LM75_TEMP_FROM_REG(data->temp[index][nr]));
1403 }
1404
1405 static ssize_t
1406 store_temp(struct device *dev, struct device_attribute *attr, const char *buf,
1407            size_t count)
1408 {
1409         struct nct6775_data *data = dev_get_drvdata(dev);
1410         struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
1411         int nr = sattr->nr;
1412         int index = sattr->index;
1413         int err;
1414         long val;
1415
1416         err = kstrtol(buf, 10, &val);
1417         if (err < 0)
1418                 return err;
1419
1420         mutex_lock(&data->update_lock);
1421         data->temp[index][nr] = LM75_TEMP_TO_REG(val);
1422         nct6775_write_temp(data, data->reg_temp[index][nr],
1423                            data->temp[index][nr]);
1424         mutex_unlock(&data->update_lock);
1425         return count;
1426 }
1427
1428 static ssize_t
1429 show_temp_offset(struct device *dev, struct device_attribute *attr, char *buf)
1430 {
1431         struct nct6775_data *data = nct6775_update_device(dev);
1432         struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr);
1433
1434         return sprintf(buf, "%d\n", data->temp_offset[sattr->index] * 1000);
1435 }
1436
1437 static ssize_t
1438 store_temp_offset(struct device *dev, struct device_attribute *attr,
1439                   const char *buf, size_t count)
1440 {
1441         struct nct6775_data *data = dev_get_drvdata(dev);
1442         struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr);
1443         int nr = sattr->index;
1444         long val;
1445         int err;
1446
1447         err = kstrtol(buf, 10, &val);
1448         if (err < 0)
1449                 return err;
1450
1451         val = SENSORS_LIMIT(DIV_ROUND_CLOSEST(val, 1000), -128, 127);
1452
1453         mutex_lock(&data->update_lock);
1454         data->temp_offset[nr] = val;
1455         nct6775_write_value(data, data->REG_TEMP_OFFSET[nr], val);
1456         mutex_unlock(&data->update_lock);
1457
1458         return count;
1459 }
1460
1461 static ssize_t
1462 show_temp_type(struct device *dev, struct device_attribute *attr, char *buf)
1463 {
1464         struct nct6775_data *data = nct6775_update_device(dev);
1465         struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr);
1466         int nr = sattr->index;
1467         return sprintf(buf, "%d\n", (int)data->temp_type[nr]);
1468 }
1469
1470 static ssize_t
1471 store_temp_type(struct device *dev, struct device_attribute *attr,
1472                 const char *buf, size_t count)
1473 {
1474         struct nct6775_data *data = nct6775_update_device(dev);
1475         struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr);
1476         int nr = sattr->index;
1477         unsigned long val;
1478         int err;
1479         u8 vbat, diode, bit;
1480
1481         err = kstrtoul(buf, 10, &val);
1482         if (err < 0)
1483                 return err;
1484
1485         if (val != 1 && val != 3 && val != 4)
1486                 return -EINVAL;
1487
1488         mutex_lock(&data->update_lock);
1489
1490         data->temp_type[nr] = val;
1491         vbat = nct6775_read_value(data, data->REG_VBAT) & ~(0x02 << nr);
1492         diode = nct6775_read_value(data, data->REG_DIODE) & ~(0x02 << nr);
1493         bit = 0x02 << nr;
1494         switch(val) {
1495         case 1: /* CPU diode (diode, current mode) */
1496                 vbat |= bit;
1497                 diode |= bit;
1498                 break;
1499         case 3: /* diode, voltage mode */
1500                 vbat |= bit;
1501                 break;
1502         case 4: /* thermistor */
1503                 break;
1504         };
1505         nct6775_write_value(data, data->REG_VBAT, vbat);
1506         nct6775_write_value(data, data->REG_DIODE, diode);
1507
1508         mutex_unlock(&data->update_lock);
1509         return count;
1510 }
1511
1512 static struct sensor_device_attribute_2 sda_temp_input[] = {
1513         SENSOR_ATTR_2(temp1_input, S_IRUGO, show_temp, NULL, 0, 0),
1514         SENSOR_ATTR_2(temp2_input, S_IRUGO, show_temp, NULL, 1, 0),
1515         SENSOR_ATTR_2(temp3_input, S_IRUGO, show_temp, NULL, 2, 0),
1516         SENSOR_ATTR_2(temp4_input, S_IRUGO, show_temp, NULL, 3, 0),
1517         SENSOR_ATTR_2(temp5_input, S_IRUGO, show_temp, NULL, 4, 0),
1518         SENSOR_ATTR_2(temp6_input, S_IRUGO, show_temp, NULL, 5, 0),
1519 };
1520
1521 static struct sensor_device_attribute sda_temp_label[] = {
1522         SENSOR_ATTR(temp1_label, S_IRUGO, show_temp_label, NULL, 0),
1523         SENSOR_ATTR(temp2_label, S_IRUGO, show_temp_label, NULL, 1),
1524         SENSOR_ATTR(temp3_label, S_IRUGO, show_temp_label, NULL, 2),
1525         SENSOR_ATTR(temp4_label, S_IRUGO, show_temp_label, NULL, 3),
1526         SENSOR_ATTR(temp5_label, S_IRUGO, show_temp_label, NULL, 4),
1527         SENSOR_ATTR(temp6_label, S_IRUGO, show_temp_label, NULL, 5),
1528 };
1529
1530 static struct sensor_device_attribute_2 sda_temp_max[] = {
1531         SENSOR_ATTR_2(temp1_max, S_IRUGO | S_IWUSR, show_temp, store_temp,
1532                       0, 1),
1533         SENSOR_ATTR_2(temp2_max, S_IRUGO | S_IWUSR, show_temp, store_temp,
1534                       1, 1),
1535         SENSOR_ATTR_2(temp3_max, S_IRUGO | S_IWUSR, show_temp, store_temp,
1536                       2, 1),
1537         SENSOR_ATTR_2(temp4_max, S_IRUGO | S_IWUSR, show_temp, store_temp,
1538                       3, 1),
1539         SENSOR_ATTR_2(temp5_max, S_IRUGO | S_IWUSR, show_temp, store_temp,
1540                       4, 1),
1541         SENSOR_ATTR_2(temp6_max, S_IRUGO | S_IWUSR, show_temp, store_temp,
1542                       5, 1),
1543 };
1544
1545 static struct sensor_device_attribute_2 sda_temp_max_hyst[] = {
1546         SENSOR_ATTR_2(temp1_max_hyst, S_IRUGO | S_IWUSR, show_temp, store_temp,
1547                       0, 2),
1548         SENSOR_ATTR_2(temp2_max_hyst, S_IRUGO | S_IWUSR, show_temp, store_temp,
1549                       1, 2),
1550         SENSOR_ATTR_2(temp3_max_hyst, S_IRUGO | S_IWUSR, show_temp, store_temp,
1551                       2, 2),
1552         SENSOR_ATTR_2(temp4_max_hyst, S_IRUGO | S_IWUSR, show_temp, store_temp,
1553                       3, 2),
1554         SENSOR_ATTR_2(temp5_max_hyst, S_IRUGO | S_IWUSR, show_temp, store_temp,
1555                       4, 2),
1556         SENSOR_ATTR_2(temp6_max_hyst, S_IRUGO | S_IWUSR, show_temp, store_temp,
1557                       5, 2),
1558 };
1559
1560 static struct sensor_device_attribute sda_temp_offset[] = {
1561         SENSOR_ATTR(temp1_offset, S_IRUGO | S_IWUSR, show_temp_offset,
1562                     store_temp_offset, 0),
1563         SENSOR_ATTR(temp2_offset, S_IRUGO | S_IWUSR, show_temp_offset,
1564                     store_temp_offset, 1),
1565         SENSOR_ATTR(temp3_offset, S_IRUGO | S_IWUSR, show_temp_offset,
1566                     store_temp_offset, 2),
1567         SENSOR_ATTR(temp4_offset, S_IRUGO | S_IWUSR, show_temp_offset,
1568                     store_temp_offset, 3),
1569         SENSOR_ATTR(temp5_offset, S_IRUGO | S_IWUSR, show_temp_offset,
1570                     store_temp_offset, 4),
1571         SENSOR_ATTR(temp6_offset, S_IRUGO | S_IWUSR, show_temp_offset,
1572                     store_temp_offset, 5),
1573 };
1574
1575 static struct sensor_device_attribute sda_temp_alarm[] = {
1576         SENSOR_ATTR(temp1_alarm, S_IRUGO, show_alarm, NULL, 4),
1577         SENSOR_ATTR(temp2_alarm, S_IRUGO, show_alarm, NULL, 5),
1578         SENSOR_ATTR(temp3_alarm, S_IRUGO, show_alarm, NULL, 13),
1579 };
1580
1581 static struct sensor_device_attribute sda_temp_type[] = {
1582         SENSOR_ATTR(temp1_type, S_IRUGO | S_IWUSR, show_temp_type,
1583                     store_temp_type, 0),
1584         SENSOR_ATTR(temp2_type, S_IRUGO | S_IWUSR, show_temp_type,
1585                     store_temp_type, 1),
1586         SENSOR_ATTR(temp3_type, S_IRUGO | S_IWUSR, show_temp_type,
1587                     store_temp_type, 2),
1588         SENSOR_ATTR(temp4_type, S_IRUGO | S_IWUSR, show_temp_type,
1589                     store_temp_type, 3),
1590         SENSOR_ATTR(temp5_type, S_IRUGO | S_IWUSR, show_temp_type,
1591                     store_temp_type, 4),
1592         SENSOR_ATTR(temp6_type, S_IRUGO | S_IWUSR, show_temp_type,
1593                     store_temp_type, 5),
1594 };
1595
1596 static ssize_t
1597 show_pwm_mode(struct device *dev, struct device_attribute *attr, char *buf)
1598 {
1599         struct nct6775_data *data = nct6775_update_device(dev);
1600         struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr);
1601
1602         return sprintf(buf, "%d\n", !data->pwm_mode[sattr->index]);
1603 }
1604
1605 static ssize_t
1606 store_pwm_mode(struct device *dev, struct device_attribute *attr,
1607                         const char *buf, size_t count)
1608 {
1609         struct nct6775_data *data = dev_get_drvdata(dev);
1610         struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr);
1611         int nr = sattr->index;
1612         unsigned long val;
1613         int err;
1614         u8 reg;
1615
1616         err = kstrtoul(buf, 10, &val);
1617         if (err < 0)
1618                 return err;
1619
1620         if (val > 1)
1621                 return -EINVAL;
1622
1623         /* Setting DC mode is not supported for all chips/channels */
1624         if (data->REG_PWM_MODE[nr] == 0) {
1625                 if (val)
1626                         return -EINVAL;
1627                 return count;
1628         }
1629
1630         mutex_lock(&data->update_lock);
1631         data->pwm_mode[nr] = val;
1632         reg = nct6775_read_value(data, data->REG_PWM_MODE[nr]);
1633         reg &= ~data->PWM_MODE_MASK[nr];
1634         if (val)
1635                 reg |= data->PWM_MODE_MASK[nr];
1636         nct6775_write_value(data, data->REG_PWM_MODE[nr], reg);
1637         mutex_unlock(&data->update_lock);
1638         return count;
1639 }
1640
1641 static ssize_t
1642 show_pwm_enable(struct device *dev, struct device_attribute *attr, char *buf)
1643 {
1644         struct nct6775_data *data = nct6775_update_device(dev);
1645         struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr);
1646
1647         return sprintf(buf, "%d\n", data->pwm_enable[sattr->index]);
1648 }
1649
1650 static ssize_t
1651 show_pwm(struct device *dev, struct device_attribute *attr, char *buf)
1652 {
1653         struct nct6775_data *data = nct6775_update_device(dev);
1654         struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
1655         int nr = sattr->nr;
1656         int index = sattr->index;
1657         int pwm;
1658
1659         /*
1660          * For automatic fan control modes, show current pwm readings.
1661          * Otherwise, show the configured value.
1662          */
1663         if (index == 0 && data->pwm_enable[nr] > 1)
1664                 pwm = nct6775_read_value(data, data->REG_PWM_READ[nr]);
1665         else
1666                 pwm = data->pwm[index][nr];
1667
1668         return sprintf(buf, "%d\n", pwm);
1669 }
1670
1671 static ssize_t
1672 store_pwm(struct device *dev, struct device_attribute *attr, const char *buf,
1673           size_t count)
1674 {
1675         struct nct6775_data *data = dev_get_drvdata(dev);
1676         struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
1677         int nr = sattr->nr;
1678         int index = sattr->index;
1679         unsigned long val;
1680         int err;
1681
1682         err = kstrtoul(buf, 10, &val);
1683         if (err < 0)
1684                 return err;
1685         if (val > 255 || (index && val == 0))
1686                 return -EINVAL;
1687
1688         mutex_lock(&data->update_lock);
1689         data->pwm[index][nr] = val;
1690         nct6775_write_value(data, data->REG_PWM[index][nr], val);
1691         mutex_unlock(&data->update_lock);
1692         return count;
1693 }
1694
1695 /* Returns 0 if OK, -EINVAL otherwise */
1696 static int check_trip_points(struct nct6775_data *data, int nr)
1697 {
1698         int i;
1699
1700         for (i = 0; i < data->auto_pwm_num - 1; i++) {
1701                 if (data->auto_temp[nr][i] > data->auto_temp[nr][i + 1])
1702                         return -EINVAL;
1703         }
1704         for (i = 0; i < data->auto_pwm_num - 1; i++) {
1705                 if (data->auto_pwm[nr][i] > data->auto_pwm[nr][i + 1])
1706                         return -EINVAL;
1707         }
1708         /* validate critical temperature and pwm if enabled (pwm > 0) */
1709         if (data->auto_pwm[nr][data->auto_pwm_num]) {
1710                 if (data->auto_temp[nr][data->auto_pwm_num - 1] >
1711                                 data->auto_temp[nr][data->auto_pwm_num] ||
1712                     data->auto_pwm[nr][data->auto_pwm_num - 1] >
1713                                 data->auto_pwm[nr][data->auto_pwm_num])
1714                         return -EINVAL;
1715         }
1716         return 0;
1717 }
1718
1719 static ssize_t
1720 store_pwm_enable(struct device *dev, struct device_attribute *attr,
1721                  const char *buf, size_t count)
1722 {
1723         struct nct6775_data *data = dev_get_drvdata(dev);
1724         struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr);
1725         int nr = sattr->index;
1726         unsigned long val;
1727         int err;
1728         u16 reg;
1729
1730         err = kstrtoul(buf, 10, &val);
1731         if (err < 0)
1732                 return err;
1733
1734         if (val > 4)
1735                 return -EINVAL;
1736
1737         if (val == 4 && check_trip_points(data, nr)) {
1738                 dev_err(dev, "Inconsistent trip points, not switching to SmartFan IV mode\n");
1739                 dev_err(dev, "Adjust trip points and try again\n");
1740                 return -EINVAL;
1741         }
1742
1743         mutex_lock(&data->update_lock);
1744         data->pwm_enable[nr] = val;
1745         if (!val) {
1746                 /*
1747                  * turn off pwm control: select manual mode, set pwm to maximum
1748                  */
1749                 data->pwm[0][nr] = 255;
1750                 nct6775_write_value(data, data->REG_PWM[0][nr], 255);
1751         }
1752         reg = nct6775_read_value(data, data->REG_FAN_MODE[nr]);
1753         reg &= 0x0f;
1754         reg |= (pwm_enable_to_reg(val) << 4);
1755         nct6775_write_value(data, data->REG_FAN_MODE[nr], reg);
1756         mutex_unlock(&data->update_lock);
1757         return count;
1758 }
1759
1760 static ssize_t
1761 show_pwm_temp(struct device *dev, struct device_attribute *attr, char *buf)
1762 {
1763         struct nct6775_data *data = nct6775_update_device(dev);
1764         struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr);
1765         int nr = sattr->index;
1766
1767         return sprintf(buf, "%d\n", LM75_TEMP_FROM_REG(data->pwm_temp[nr]));
1768 }
1769
1770 static ssize_t
1771 show_pwm_temp_sel(struct device *dev, struct device_attribute *attr, char *buf)
1772 {
1773         struct nct6775_data *data = nct6775_update_device(dev);
1774         struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
1775
1776         return sprintf(buf, "%d\n",
1777                        data->pwm_temp_sel[sattr->index][sattr->nr]);
1778 }
1779
1780 static ssize_t
1781 store_pwm_temp_sel(struct device *dev, struct device_attribute *attr,
1782                    const char *buf, size_t count)
1783 {
1784         struct nct6775_data *data = nct6775_update_device(dev);
1785         struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
1786         int nr = sattr->nr;
1787         int index = sattr->index;
1788         unsigned long val;
1789         int err;
1790         int reg;
1791         static const int max_src[] = {
1792             ARRAY_SIZE(nct6775_temp_label),
1793             ARRAY_SIZE(nct6776_temp_label),
1794             ARRAY_SIZE(nct6779_temp_label)
1795         };
1796
1797         err = kstrtoul(buf, 10, &val);
1798         if (err < 0)
1799                 return err;
1800         if (val == 0 || val > 0x1f)
1801                 return -EINVAL;
1802
1803         val = SENSORS_LIMIT(val, 1, max_src[data->kind]);
1804
1805         if (!strlen(data->temp_label[val]))
1806                 return -EINVAL;
1807
1808         mutex_lock(&data->update_lock);
1809         data->pwm_temp_sel[index][nr] = val;
1810         reg = nct6775_read_value(data, data->REG_TEMP_SEL[index][nr]);
1811         reg &= 0xe0;
1812         reg |= val;
1813         nct6775_write_value(data, data->REG_TEMP_SEL[index][nr], reg);
1814         mutex_unlock(&data->update_lock);
1815
1816         return count;
1817 }
1818
1819 static ssize_t
1820 show_target_temp(struct device *dev, struct device_attribute *attr, char *buf)
1821 {
1822         struct nct6775_data *data = nct6775_update_device(dev);
1823         struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr);
1824
1825         return sprintf(buf, "%d\n",
1826                        data->target_temp[sattr->index] * 1000);
1827 }
1828
1829 static ssize_t
1830 store_target_temp(struct device *dev, struct device_attribute *attr,
1831                   const char *buf, size_t count)
1832 {
1833         struct nct6775_data *data = dev_get_drvdata(dev);
1834         struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr);
1835         int nr = sattr->index;
1836         long val;
1837         int err;
1838
1839         err = kstrtol(buf, 10, &val);
1840         if (err < 0)
1841                 return err;
1842
1843         val = SENSORS_LIMIT(DIV_ROUND_CLOSEST(val, 1000), 0, 127);
1844
1845         mutex_lock(&data->update_lock);
1846         data->target_temp[nr] = val;
1847         nct6775_write_value(data, data->REG_TARGET[nr], val);
1848         mutex_unlock(&data->update_lock);
1849         return count;
1850 }
1851
1852 static ssize_t
1853 show_auto_temp_hyst(struct device *dev, struct device_attribute *attr,
1854                     char *buf)
1855 {
1856         struct nct6775_data *data = nct6775_update_device(dev);
1857         struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
1858         int nr = sattr->nr;
1859         int point = sattr->index >= data->auto_pwm_num ? 1 : 0;
1860         int tolerance = data->tolerance[nr][point];
1861         int temp = data->auto_temp[nr][sattr->index];
1862
1863         return sprintf(buf, "%d\n", (temp - tolerance) * 1000);
1864 }
1865
1866 static ssize_t
1867 store_auto_temp_hyst(struct device *dev, struct device_attribute *attr,
1868                      const char *buf, size_t count)
1869 {
1870         struct nct6775_data *data = dev_get_drvdata(dev);
1871         struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
1872         int nr = sattr->nr;
1873         int point = sattr->index >= data->auto_pwm_num ? 1 : 0;
1874         u16 reg;
1875         long val;
1876         int err;
1877         int maxlimit[2][3] = { { 15, 7, 63 }, { 15, 7, 7 } };
1878         int mask[] = { 0x0f, 0x07, 0x07 };
1879         int temp;
1880
1881         err = kstrtol(buf, 10, &val);
1882         if (err < 0)
1883                 return err;
1884
1885         temp = data->auto_temp[nr][sattr->index];
1886         val = temp - DIV_ROUND_CLOSEST(val, 1000);
1887
1888         /* Limit tolerance as needed */
1889         val = SENSORS_LIMIT(val, 0, maxlimit[point][data->kind]);
1890
1891         mutex_lock(&data->update_lock);
1892         if (point) {
1893                 nct6775_write_value(data,
1894                                     data->REG_CRITICAL_TEMP_TOLERANCE[nr],
1895                                     val);
1896         } else {
1897                 reg = nct6775_read_value(data, data->REG_FAN_MODE[nr]);
1898                 reg = (reg & ~mask[nr]) | (val & mask[nr]);
1899                 nct6775_write_value(data, data->REG_FAN_MODE[nr], reg);
1900                 if (data->kind == nct6779) {
1901                         reg = nct6775_read_value(data,
1902                                                  NCT6779_REG_TOLERANCE_H[nr]);
1903                         reg = (reg & 0x70) | ((val & 0x38) << 1);
1904                         nct6775_write_value(data,
1905                                             NCT6779_REG_TOLERANCE_H[nr], reg);
1906                 }
1907         }
1908
1909         data->tolerance[nr][point] = val;
1910         mutex_unlock(&data->update_lock);
1911         return count;
1912 }
1913
1914 static SENSOR_DEVICE_ATTR_2(pwm1, S_IWUSR | S_IRUGO, show_pwm, store_pwm, 0, 0);
1915 static SENSOR_DEVICE_ATTR_2(pwm2, S_IWUSR | S_IRUGO, show_pwm, store_pwm, 1, 0);
1916 static SENSOR_DEVICE_ATTR_2(pwm3, S_IWUSR | S_IRUGO, show_pwm, store_pwm, 2, 0);
1917 static SENSOR_DEVICE_ATTR_2(pwm4, S_IWUSR | S_IRUGO, show_pwm, store_pwm, 3, 0);
1918 static SENSOR_DEVICE_ATTR_2(pwm5, S_IWUSR | S_IRUGO, show_pwm, store_pwm, 4, 0);
1919
1920 static SENSOR_DEVICE_ATTR(pwm1_mode, S_IWUSR | S_IRUGO, show_pwm_mode,
1921                           store_pwm_mode, 0);
1922 static SENSOR_DEVICE_ATTR(pwm2_mode, S_IWUSR | S_IRUGO, show_pwm_mode,
1923                           store_pwm_mode, 1);
1924 static SENSOR_DEVICE_ATTR(pwm3_mode, S_IWUSR | S_IRUGO, show_pwm_mode,
1925                           store_pwm_mode, 2);
1926 static SENSOR_DEVICE_ATTR(pwm4_mode, S_IWUSR | S_IRUGO, show_pwm_mode,
1927                           store_pwm_mode, 3);
1928 static SENSOR_DEVICE_ATTR(pwm5_mode, S_IWUSR | S_IRUGO, show_pwm_mode,
1929                           store_pwm_mode, 4);
1930
1931 static SENSOR_DEVICE_ATTR(pwm1_enable, S_IWUSR | S_IRUGO, show_pwm_enable,
1932                           store_pwm_enable, 0);
1933 static SENSOR_DEVICE_ATTR(pwm2_enable, S_IWUSR | S_IRUGO, show_pwm_enable,
1934                           store_pwm_enable, 1);
1935 static SENSOR_DEVICE_ATTR(pwm3_enable, S_IWUSR | S_IRUGO, show_pwm_enable,
1936                           store_pwm_enable, 2);
1937 static SENSOR_DEVICE_ATTR(pwm4_enable, S_IWUSR | S_IRUGO, show_pwm_enable,
1938                           store_pwm_enable, 3);
1939 static SENSOR_DEVICE_ATTR(pwm5_enable, S_IWUSR | S_IRUGO, show_pwm_enable,
1940                           store_pwm_enable, 4);
1941
1942 static SENSOR_DEVICE_ATTR_2(pwm1_temp_sel, S_IWUSR | S_IRUGO,
1943                             show_pwm_temp_sel, store_pwm_temp_sel, 0, 0);
1944 static SENSOR_DEVICE_ATTR_2(pwm2_temp_sel, S_IWUSR | S_IRUGO,
1945                             show_pwm_temp_sel, store_pwm_temp_sel, 1, 0);
1946 static SENSOR_DEVICE_ATTR_2(pwm3_temp_sel, S_IWUSR | S_IRUGO,
1947                             show_pwm_temp_sel, store_pwm_temp_sel, 2, 0);
1948 static SENSOR_DEVICE_ATTR_2(pwm4_temp_sel, S_IWUSR | S_IRUGO,
1949                             show_pwm_temp_sel, store_pwm_temp_sel, 3, 0);
1950 static SENSOR_DEVICE_ATTR_2(pwm5_temp_sel, S_IWUSR | S_IRUGO,
1951                             show_pwm_temp_sel, store_pwm_temp_sel, 4, 0);
1952
1953 static SENSOR_DEVICE_ATTR(pwm1_target, S_IWUSR | S_IRUGO, show_target_temp,
1954                           store_target_temp, 0);
1955 static SENSOR_DEVICE_ATTR(pwm2_target, S_IWUSR | S_IRUGO, show_target_temp,
1956                           store_target_temp, 1);
1957 static SENSOR_DEVICE_ATTR(pwm3_target, S_IWUSR | S_IRUGO, show_target_temp,
1958                           store_target_temp, 2);
1959 static SENSOR_DEVICE_ATTR(pwm4_target, S_IWUSR | S_IRUGO, show_target_temp,
1960                           store_target_temp, 3);
1961 static SENSOR_DEVICE_ATTR(pwm5_target, S_IWUSR | S_IRUGO, show_target_temp,
1962                           store_target_temp, 4);
1963
1964 /* Monitored pwm temperatures */
1965 static SENSOR_DEVICE_ATTR(temp7_input, S_IRUGO, show_pwm_temp, NULL, 0);
1966 static SENSOR_DEVICE_ATTR(temp8_input, S_IRUGO, show_pwm_temp, NULL, 1);
1967 static SENSOR_DEVICE_ATTR(temp9_input, S_IRUGO, show_pwm_temp, NULL, 2);
1968 static SENSOR_DEVICE_ATTR(temp10_input, S_IRUGO, show_pwm_temp, NULL, 3);
1969 static SENSOR_DEVICE_ATTR(temp11_input, S_IRUGO, show_pwm_temp, NULL, 4);
1970
1971 /* Smart Fan registers */
1972
1973 static ssize_t
1974 show_pwm_weight_enable(struct device *dev, struct device_attribute *attr,
1975                        char *buf)
1976 {
1977         struct nct6775_data *data = nct6775_update_device(dev);
1978         struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr);
1979
1980         return sprintf(buf, "%d\n", data->pwm_weight_enable[sattr->index]);
1981 }
1982
1983 static ssize_t
1984 store_pwm_weight_enable(struct device *dev, struct device_attribute *attr,
1985                         const char *buf, size_t count)
1986 {
1987         struct nct6775_data *data = dev_get_drvdata(dev);
1988         struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr);
1989         int nr = sattr->index;
1990         unsigned long val;
1991         int err;
1992         u8 reg;
1993
1994         err = kstrtoul(buf, 10, &val);
1995         if (err < 0)
1996                 return err;
1997
1998         if (val > 1)
1999                 return -EINVAL;
2000
2001         mutex_lock(&data->update_lock);
2002         data->pwm_weight_enable[nr] = val;
2003         reg = nct6775_read_value(data, data->REG_TEMP_SEL[1][nr]);
2004         reg &= 0x7f;
2005         if (val)
2006                 reg |= 0x80;
2007         nct6775_write_value(data, data->REG_TEMP_SEL[1][nr], reg);
2008         mutex_unlock(&data->update_lock);
2009         return count;
2010 }
2011
2012 static ssize_t
2013 show_weight_temp(struct device *dev, struct device_attribute *attr, char *buf)
2014 {
2015         struct nct6775_data *data = nct6775_update_device(dev);
2016         struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
2017         int nr = sattr->nr;
2018         int index = sattr->index;
2019
2020         return sprintf(buf, "%d\n", data->weight_temp[index][nr] * 1000);
2021 }
2022
2023 static ssize_t
2024 store_weight_temp(struct device *dev, struct device_attribute *attr,
2025                   const char *buf, size_t count)
2026 {
2027         struct nct6775_data *data = dev_get_drvdata(dev);
2028         struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
2029         int nr = sattr->nr;
2030         int index = sattr->index;
2031         unsigned long val;
2032         int err;
2033
2034         err = kstrtoul(buf, 10, &val);
2035         if (err < 0)
2036                 return err;
2037
2038         val = SENSORS_LIMIT(DIV_ROUND_CLOSEST(val, 1000), 0, 255);
2039
2040         mutex_lock(&data->update_lock);
2041         data->weight_temp[index][nr] = val;
2042         nct6775_write_value(data, data->REG_WEIGHT_TEMP[index][nr], val);
2043         mutex_unlock(&data->update_lock);
2044         return count;
2045 }
2046
2047 static ssize_t
2048 show_weight_duty(struct device *dev, struct device_attribute *attr, char *buf)
2049 {
2050         struct nct6775_data *data = nct6775_update_device(dev);
2051         struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
2052         int nr = sattr->nr;
2053         int index = sattr->index;
2054
2055         return sprintf(buf, "%d\n", data->weight_duty[index][nr]);
2056 }
2057
2058 static ssize_t
2059 store_weight_duty(struct device *dev, struct device_attribute *attr,
2060                   const char *buf, size_t count)
2061 {
2062         struct nct6775_data *data = dev_get_drvdata(dev);
2063         struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
2064         int nr = sattr->nr;
2065         int index = sattr->index;
2066         unsigned long val;
2067         int err;
2068
2069         err = kstrtoul(buf, 10, &val);
2070         if (err < 0)
2071                 return err;
2072
2073         val = SENSORS_LIMIT(val, 0, 255);
2074
2075         mutex_lock(&data->update_lock);
2076         data->weight_duty[index][nr] = val;
2077         nct6775_write_value(data, data->REG_WEIGHT_DUTY[index][nr], val);
2078         mutex_unlock(&data->update_lock);
2079         return count;
2080 }
2081
2082 static SENSOR_DEVICE_ATTR(pwm1_weight_enable, S_IWUSR | S_IRUGO,
2083                           show_pwm_weight_enable, store_pwm_weight_enable, 0);
2084 static SENSOR_DEVICE_ATTR(pwm2_weight_enable, S_IWUSR | S_IRUGO,
2085                           show_pwm_weight_enable, store_pwm_weight_enable, 1);
2086 static SENSOR_DEVICE_ATTR(pwm3_weight_enable, S_IWUSR | S_IRUGO,
2087                           show_pwm_weight_enable, store_pwm_weight_enable, 2);
2088 static SENSOR_DEVICE_ATTR(pwm4_weight_enable, S_IWUSR | S_IRUGO,
2089                           show_pwm_weight_enable, store_pwm_weight_enable, 3);
2090 static SENSOR_DEVICE_ATTR(pwm5_weight_enable, S_IWUSR | S_IRUGO,
2091                           show_pwm_weight_enable, store_pwm_weight_enable, 4);
2092
2093 static SENSOR_DEVICE_ATTR_2(pwm1_weight_temp_sel, S_IWUSR | S_IRUGO,
2094                             show_pwm_temp_sel, store_pwm_temp_sel, 0, 1);
2095 static SENSOR_DEVICE_ATTR_2(pwm2_weight_temp_sel, S_IWUSR | S_IRUGO,
2096                             show_pwm_temp_sel, store_pwm_temp_sel, 1, 1);
2097 static SENSOR_DEVICE_ATTR_2(pwm3_weight_temp_sel, S_IWUSR | S_IRUGO,
2098                             show_pwm_temp_sel, store_pwm_temp_sel, 2, 1);
2099 static SENSOR_DEVICE_ATTR_2(pwm4_weight_temp_sel, S_IWUSR | S_IRUGO,
2100                             show_pwm_temp_sel, store_pwm_temp_sel, 3, 1);
2101 static SENSOR_DEVICE_ATTR_2(pwm5_weight_temp_sel, S_IWUSR | S_IRUGO,
2102                             show_pwm_temp_sel, store_pwm_temp_sel, 4, 1);
2103
2104 static SENSOR_DEVICE_ATTR_2(pwm1_weight_temp_step, S_IWUSR | S_IRUGO,
2105                             show_weight_temp, store_weight_temp, 0, 0);
2106 static SENSOR_DEVICE_ATTR_2(pwm2_weight_temp_step, S_IWUSR | S_IRUGO,
2107                             show_weight_temp, store_weight_temp, 1, 0);
2108 static SENSOR_DEVICE_ATTR_2(pwm3_weight_temp_step, S_IWUSR | S_IRUGO,
2109                             show_weight_temp, store_weight_temp, 2, 0);
2110 static SENSOR_DEVICE_ATTR_2(pwm4_weight_temp_step, S_IWUSR | S_IRUGO,
2111                             show_weight_temp, store_weight_temp, 3, 0);
2112 static SENSOR_DEVICE_ATTR_2(pwm5_weight_temp_step, S_IWUSR | S_IRUGO,
2113                             show_weight_temp, store_weight_temp, 4, 0);
2114
2115 static SENSOR_DEVICE_ATTR_2(pwm1_weight_temp_step_tol, S_IWUSR | S_IRUGO,
2116                             show_weight_temp, store_weight_temp, 0, 1);
2117 static SENSOR_DEVICE_ATTR_2(pwm2_weight_temp_step_tol, S_IWUSR | S_IRUGO,
2118                             show_weight_temp, store_weight_temp, 1, 1);
2119 static SENSOR_DEVICE_ATTR_2(pwm3_weight_temp_step_tol, S_IWUSR | S_IRUGO,
2120                             show_weight_temp, store_weight_temp, 2, 1);
2121 static SENSOR_DEVICE_ATTR_2(pwm4_weight_temp_step_tol, S_IWUSR | S_IRUGO,
2122                             show_weight_temp, store_weight_temp, 3, 1);
2123 static SENSOR_DEVICE_ATTR_2(pwm5_weight_temp_step_tol, S_IWUSR | S_IRUGO,
2124                             show_weight_temp, store_weight_temp, 4, 1);
2125
2126 static SENSOR_DEVICE_ATTR_2(pwm1_weight_temp_step_base, S_IWUSR | S_IRUGO,
2127                             show_weight_temp, store_weight_temp, 0, 2);
2128 static SENSOR_DEVICE_ATTR_2(pwm2_weight_temp_step_base, S_IWUSR | S_IRUGO,
2129                             show_weight_temp, store_weight_temp, 1, 2);
2130 static SENSOR_DEVICE_ATTR_2(pwm3_weight_temp_step_base, S_IWUSR | S_IRUGO,
2131                             show_weight_temp, store_weight_temp, 2, 2);
2132 static SENSOR_DEVICE_ATTR_2(pwm4_weight_temp_step_base, S_IWUSR | S_IRUGO,
2133                             show_weight_temp, store_weight_temp, 3, 2);
2134 static SENSOR_DEVICE_ATTR_2(pwm5_weight_temp_step_base, S_IWUSR | S_IRUGO,
2135                             show_weight_temp, store_weight_temp, 4, 2);
2136
2137 static SENSOR_DEVICE_ATTR_2(pwm1_weight_duty_step, S_IWUSR | S_IRUGO,
2138                             show_weight_duty, store_weight_duty, 0, 0);
2139 static SENSOR_DEVICE_ATTR_2(pwm2_weight_duty_step, S_IWUSR | S_IRUGO,
2140                             show_weight_duty, store_weight_duty, 1, 0);
2141 static SENSOR_DEVICE_ATTR_2(pwm3_weight_duty_step, S_IWUSR | S_IRUGO,
2142                             show_weight_duty, store_weight_duty, 2, 0);
2143 static SENSOR_DEVICE_ATTR_2(pwm4_weight_duty_step, S_IWUSR | S_IRUGO,
2144                             show_weight_duty, store_weight_duty, 3, 0);
2145 static SENSOR_DEVICE_ATTR_2(pwm5_weight_duty_step, S_IWUSR | S_IRUGO,
2146                             show_weight_duty, store_weight_duty, 4, 0);
2147
2148 /* duty_base is not supported on all chips */
2149 static struct sensor_device_attribute_2 sda_weight_duty_base[] = {
2150         SENSOR_ATTR_2(pwm1_weight_duty_base, S_IWUSR | S_IRUGO,
2151                       show_weight_duty, store_weight_duty, 0, 1),
2152         SENSOR_ATTR_2(pwm2_weight_duty_base, S_IWUSR | S_IRUGO,
2153                       show_weight_duty, store_weight_duty, 1, 1),
2154         SENSOR_ATTR_2(pwm3_weight_duty_base, S_IWUSR | S_IRUGO,
2155                       show_weight_duty, store_weight_duty, 2, 1),
2156         SENSOR_ATTR_2(pwm4_weight_duty_base, S_IWUSR | S_IRUGO,
2157                       show_weight_duty, store_weight_duty, 3, 1),
2158         SENSOR_ATTR_2(pwm5_weight_duty_base, S_IWUSR | S_IRUGO,
2159                       show_weight_duty, store_weight_duty, 4, 1),
2160 };
2161
2162 static ssize_t
2163 show_fan_time(struct device *dev, struct device_attribute *attr, char *buf)
2164 {
2165         struct nct6775_data *data = nct6775_update_device(dev);
2166         struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
2167         int nr = sattr->nr;
2168         int index = sattr->index;
2169
2170         return sprintf(buf, "%d\n",
2171                        step_time_from_reg(data->fan_time[index][nr],
2172                                           data->pwm_mode[nr]));
2173 }
2174
2175 static ssize_t
2176 store_fan_time(struct device *dev, struct device_attribute *attr,
2177                const char *buf, size_t count)
2178 {
2179         struct nct6775_data *data = dev_get_drvdata(dev);
2180         struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
2181         int nr = sattr->nr;
2182         int index = sattr->index;
2183         unsigned long val;
2184         int err;
2185
2186         err = kstrtoul(buf, 10, &val);
2187         if (err < 0)
2188                 return err;
2189
2190         val = step_time_to_reg(val, data->pwm_mode[nr]);
2191         mutex_lock(&data->update_lock);
2192         data->fan_time[index][nr] = val;
2193         nct6775_write_value(data, data->REG_FAN_TIME[index][nr], val);
2194         mutex_unlock(&data->update_lock);
2195         return count;
2196 }
2197
2198 static ssize_t
2199 show_name(struct device *dev, struct device_attribute *attr, char *buf)
2200 {
2201         struct nct6775_data *data = dev_get_drvdata(dev);
2202
2203         return sprintf(buf, "%s\n", data->name);
2204 }
2205
2206 static DEVICE_ATTR(name, S_IRUGO, show_name, NULL);
2207
2208 static SENSOR_DEVICE_ATTR_2(pwm1_stop_time, S_IWUSR | S_IRUGO, show_fan_time,
2209                             store_fan_time, 0, 0);
2210 static SENSOR_DEVICE_ATTR_2(pwm2_stop_time, S_IWUSR | S_IRUGO, show_fan_time,
2211                             store_fan_time, 1, 0);
2212 static SENSOR_DEVICE_ATTR_2(pwm3_stop_time, S_IWUSR | S_IRUGO, show_fan_time,
2213                             store_fan_time, 2, 0);
2214 static SENSOR_DEVICE_ATTR_2(pwm4_stop_time, S_IWUSR | S_IRUGO, show_fan_time,
2215                             store_fan_time, 3, 0);
2216 static SENSOR_DEVICE_ATTR_2(pwm5_stop_time, S_IWUSR | S_IRUGO, show_fan_time,
2217                             store_fan_time, 4, 0);
2218
2219 static SENSOR_DEVICE_ATTR_2(pwm1_step_up_time, S_IWUSR | S_IRUGO, show_fan_time,
2220                             store_fan_time, 0, 1);
2221 static SENSOR_DEVICE_ATTR_2(pwm2_step_up_time, S_IWUSR | S_IRUGO, show_fan_time,
2222                             store_fan_time, 1, 1);
2223 static SENSOR_DEVICE_ATTR_2(pwm3_step_up_time, S_IWUSR | S_IRUGO, show_fan_time,
2224                             store_fan_time, 2, 1);
2225 static SENSOR_DEVICE_ATTR_2(pwm4_step_up_time, S_IWUSR | S_IRUGO, show_fan_time,
2226                             store_fan_time, 3, 1);
2227 static SENSOR_DEVICE_ATTR_2(pwm5_step_up_time, S_IWUSR | S_IRUGO, show_fan_time,
2228                             store_fan_time, 4, 1);
2229
2230 static SENSOR_DEVICE_ATTR_2(pwm1_step_down_time, S_IWUSR | S_IRUGO,
2231                             show_fan_time, store_fan_time, 0, 2);
2232 static SENSOR_DEVICE_ATTR_2(pwm2_step_down_time, S_IWUSR | S_IRUGO,
2233                             show_fan_time, store_fan_time, 1, 2);
2234 static SENSOR_DEVICE_ATTR_2(pwm3_step_down_time, S_IWUSR | S_IRUGO,
2235                             show_fan_time, store_fan_time, 2, 2);
2236 static SENSOR_DEVICE_ATTR_2(pwm4_step_down_time, S_IWUSR | S_IRUGO,
2237                             show_fan_time, store_fan_time, 3, 2);
2238 static SENSOR_DEVICE_ATTR_2(pwm5_step_down_time, S_IWUSR | S_IRUGO,
2239                             show_fan_time, store_fan_time, 4, 2);
2240
2241 static SENSOR_DEVICE_ATTR_2(pwm1_start_output, S_IWUSR | S_IRUGO, show_pwm,
2242                             store_pwm, 0, 1);
2243 static SENSOR_DEVICE_ATTR_2(pwm2_start_output, S_IWUSR | S_IRUGO, show_pwm,
2244                             store_pwm, 1, 1);
2245 static SENSOR_DEVICE_ATTR_2(pwm3_start_output, S_IWUSR | S_IRUGO, show_pwm,
2246                             store_pwm, 2, 1);
2247 static SENSOR_DEVICE_ATTR_2(pwm4_start_output, S_IWUSR | S_IRUGO, show_pwm,
2248                             store_pwm, 3, 1);
2249 static SENSOR_DEVICE_ATTR_2(pwm5_start_output, S_IWUSR | S_IRUGO, show_pwm,
2250                             store_pwm, 4, 1);
2251
2252 static SENSOR_DEVICE_ATTR_2(pwm1_stop_output, S_IWUSR | S_IRUGO, show_pwm,
2253                             store_pwm, 0, 2);
2254 static SENSOR_DEVICE_ATTR_2(pwm2_stop_output, S_IWUSR | S_IRUGO, show_pwm,
2255                             store_pwm, 1, 2);
2256 static SENSOR_DEVICE_ATTR_2(pwm3_stop_output, S_IWUSR | S_IRUGO, show_pwm,
2257                             store_pwm, 2, 2);
2258 static SENSOR_DEVICE_ATTR_2(pwm4_stop_output, S_IWUSR | S_IRUGO, show_pwm,
2259                             store_pwm, 3, 2);
2260 static SENSOR_DEVICE_ATTR_2(pwm5_stop_output, S_IWUSR | S_IRUGO, show_pwm,
2261                             store_pwm, 4, 2);
2262
2263 static struct attribute *nct6775_attributes_pwm[5][19] = {
2264         {
2265                 &sensor_dev_attr_temp7_input.dev_attr.attr,
2266                 &sensor_dev_attr_pwm1.dev_attr.attr,
2267                 &sensor_dev_attr_pwm1_mode.dev_attr.attr,
2268                 &sensor_dev_attr_pwm1_enable.dev_attr.attr,
2269                 &sensor_dev_attr_pwm1_weight_enable.dev_attr.attr,
2270                 &sensor_dev_attr_pwm1_temp_sel.dev_attr.attr,
2271                 &sensor_dev_attr_pwm1_target.dev_attr.attr,
2272                 &sensor_dev_attr_pwm1_stop_time.dev_attr.attr,
2273                 &sensor_dev_attr_pwm1_step_up_time.dev_attr.attr,
2274                 &sensor_dev_attr_pwm1_step_down_time.dev_attr.attr,
2275                 &sensor_dev_attr_pwm1_start_output.dev_attr.attr,
2276                 &sensor_dev_attr_pwm1_stop_output.dev_attr.attr,
2277                 &sensor_dev_attr_pwm1_weight_temp_sel.dev_attr.attr,
2278                 &sensor_dev_attr_pwm1_weight_temp_step.dev_attr.attr,
2279                 &sensor_dev_attr_pwm1_weight_temp_step_tol.dev_attr.attr,
2280                 &sensor_dev_attr_pwm1_weight_temp_step_base.dev_attr.attr,
2281                 &sensor_dev_attr_pwm1_weight_duty_step.dev_attr.attr,
2282                 NULL
2283         },
2284         {
2285                 &sensor_dev_attr_temp8_input.dev_attr.attr,
2286                 &sensor_dev_attr_pwm2.dev_attr.attr,
2287                 &sensor_dev_attr_pwm2_mode.dev_attr.attr,
2288                 &sensor_dev_attr_pwm2_enable.dev_attr.attr,
2289                 &sensor_dev_attr_pwm2_weight_enable.dev_attr.attr,
2290                 &sensor_dev_attr_pwm2_temp_sel.dev_attr.attr,
2291                 &sensor_dev_attr_pwm2_target.dev_attr.attr,
2292                 &sensor_dev_attr_pwm2_stop_time.dev_attr.attr,
2293                 &sensor_dev_attr_pwm2_step_up_time.dev_attr.attr,
2294                 &sensor_dev_attr_pwm2_step_down_time.dev_attr.attr,
2295                 &sensor_dev_attr_pwm2_start_output.dev_attr.attr,
2296                 &sensor_dev_attr_pwm2_stop_output.dev_attr.attr,
2297                 &sensor_dev_attr_pwm2_weight_temp_sel.dev_attr.attr,
2298                 &sensor_dev_attr_pwm2_weight_temp_step.dev_attr.attr,
2299                 &sensor_dev_attr_pwm2_weight_temp_step_tol.dev_attr.attr,
2300                 &sensor_dev_attr_pwm2_weight_temp_step_base.dev_attr.attr,
2301                 &sensor_dev_attr_pwm2_weight_duty_step.dev_attr.attr,
2302                 NULL
2303         },
2304         {
2305                 &sensor_dev_attr_temp9_input.dev_attr.attr,
2306                 &sensor_dev_attr_pwm3.dev_attr.attr,
2307                 &sensor_dev_attr_pwm3_mode.dev_attr.attr,
2308                 &sensor_dev_attr_pwm3_enable.dev_attr.attr,
2309                 &sensor_dev_attr_pwm3_weight_enable.dev_attr.attr,
2310                 &sensor_dev_attr_pwm3_temp_sel.dev_attr.attr,
2311                 &sensor_dev_attr_pwm3_target.dev_attr.attr,
2312                 &sensor_dev_attr_pwm3_stop_time.dev_attr.attr,
2313                 &sensor_dev_attr_pwm3_step_up_time.dev_attr.attr,
2314                 &sensor_dev_attr_pwm3_step_down_time.dev_attr.attr,
2315                 &sensor_dev_attr_pwm3_start_output.dev_attr.attr,
2316                 &sensor_dev_attr_pwm3_stop_output.dev_attr.attr,
2317                 &sensor_dev_attr_pwm3_weight_temp_sel.dev_attr.attr,
2318                 &sensor_dev_attr_pwm3_weight_temp_step.dev_attr.attr,
2319                 &sensor_dev_attr_pwm3_weight_temp_step_tol.dev_attr.attr,
2320                 &sensor_dev_attr_pwm3_weight_temp_step_base.dev_attr.attr,
2321                 &sensor_dev_attr_pwm3_weight_duty_step.dev_attr.attr,
2322                 NULL
2323         },
2324         {
2325                 &sensor_dev_attr_temp10_input.dev_attr.attr,
2326                 &sensor_dev_attr_pwm4.dev_attr.attr,
2327                 &sensor_dev_attr_pwm4_mode.dev_attr.attr,
2328                 &sensor_dev_attr_pwm4_enable.dev_attr.attr,
2329                 &sensor_dev_attr_pwm4_weight_enable.dev_attr.attr,
2330                 &sensor_dev_attr_pwm4_temp_sel.dev_attr.attr,
2331                 &sensor_dev_attr_pwm4_target.dev_attr.attr,
2332                 &sensor_dev_attr_pwm4_stop_time.dev_attr.attr,
2333                 &sensor_dev_attr_pwm4_step_up_time.dev_attr.attr,
2334                 &sensor_dev_attr_pwm4_step_down_time.dev_attr.attr,
2335                 &sensor_dev_attr_pwm4_start_output.dev_attr.attr,
2336                 &sensor_dev_attr_pwm4_stop_output.dev_attr.attr,
2337                 &sensor_dev_attr_pwm4_weight_temp_sel.dev_attr.attr,
2338                 &sensor_dev_attr_pwm4_weight_temp_step.dev_attr.attr,
2339                 &sensor_dev_attr_pwm4_weight_temp_step_tol.dev_attr.attr,
2340                 &sensor_dev_attr_pwm4_weight_temp_step_base.dev_attr.attr,
2341                 &sensor_dev_attr_pwm4_weight_duty_step.dev_attr.attr,
2342                 NULL
2343         },
2344         {
2345                 &sensor_dev_attr_temp11_input.dev_attr.attr,
2346                 &sensor_dev_attr_pwm5.dev_attr.attr,
2347                 &sensor_dev_attr_pwm5_mode.dev_attr.attr,
2348                 &sensor_dev_attr_pwm5_enable.dev_attr.attr,
2349                 &sensor_dev_attr_pwm5_weight_enable.dev_attr.attr,
2350                 &sensor_dev_attr_pwm5_temp_sel.dev_attr.attr,
2351                 &sensor_dev_attr_pwm5_target.dev_attr.attr,
2352                 &sensor_dev_attr_pwm5_stop_time.dev_attr.attr,
2353                 &sensor_dev_attr_pwm5_step_up_time.dev_attr.attr,
2354                 &sensor_dev_attr_pwm5_step_down_time.dev_attr.attr,
2355                 &sensor_dev_attr_pwm5_start_output.dev_attr.attr,
2356                 &sensor_dev_attr_pwm5_stop_output.dev_attr.attr,
2357                 &sensor_dev_attr_pwm5_weight_temp_sel.dev_attr.attr,
2358                 &sensor_dev_attr_pwm5_weight_temp_step.dev_attr.attr,
2359                 &sensor_dev_attr_pwm5_weight_temp_step_tol.dev_attr.attr,
2360                 &sensor_dev_attr_pwm5_weight_temp_step_base.dev_attr.attr,
2361                 &sensor_dev_attr_pwm5_weight_duty_step.dev_attr.attr,
2362                 NULL
2363         },
2364 };
2365
2366 static const struct attribute_group nct6775_group_pwm[5] = {
2367         { .attrs = nct6775_attributes_pwm[0] },
2368         { .attrs = nct6775_attributes_pwm[1] },
2369         { .attrs = nct6775_attributes_pwm[2] },
2370         { .attrs = nct6775_attributes_pwm[3] },
2371         { .attrs = nct6775_attributes_pwm[4] },
2372 };
2373
2374 static ssize_t
2375 show_auto_pwm(struct device *dev, struct device_attribute *attr, char *buf)
2376 {
2377         struct nct6775_data *data = nct6775_update_device(dev);
2378         struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
2379
2380         return sprintf(buf, "%d\n", data->auto_pwm[sattr->nr][sattr->index]);
2381 }
2382
2383 static ssize_t
2384 store_auto_pwm(struct device *dev, struct device_attribute *attr,
2385                const char *buf, size_t count)
2386 {
2387         struct nct6775_data *data = dev_get_drvdata(dev);
2388         struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
2389         int nr = sattr->nr;
2390         int point = sattr->index;
2391         unsigned long val;
2392         int err;
2393         u8 reg;
2394
2395         err = kstrtoul(buf, 10, &val);
2396         if (err < 0)
2397                 return err;
2398         if (val > 255)
2399                 return -EINVAL;
2400
2401         if (point == data->auto_pwm_num) {
2402                 if (data->kind != nct6775 && !val)
2403                         return -EINVAL;
2404                 if (data->kind != nct6779 && val)
2405                         val = 0xff;
2406         }
2407
2408         mutex_lock(&data->update_lock);
2409         data->auto_pwm[nr][point] = val;
2410         if (point < data->auto_pwm_num) {
2411                 nct6775_write_value(data,
2412                                     NCT6775_AUTO_PWM(data, nr, point),
2413                                     data->auto_pwm[nr][point]);
2414         } else {
2415                 switch (data->kind) {
2416                 case nct6775:
2417                         /* disable if needed (pwm == 0) */
2418                         reg = nct6775_read_value(data,
2419                                                  NCT6775_REG_CRITICAL_ENAB[nr]);
2420                         if (val)
2421                                 reg |= 0x02;
2422                         else
2423                                 reg &= ~0x02;
2424                         nct6775_write_value(data, NCT6775_REG_CRITICAL_ENAB[nr],
2425                                             reg);
2426                         break;
2427                 case nct6776:
2428                         break; /* always enabled, nothing to do */
2429                 case nct6779:
2430                         nct6775_write_value(data, NCT6779_REG_CRITICAL_PWM[nr],
2431                                             val);
2432                         reg = nct6775_read_value(data,
2433                                         NCT6779_REG_CRITICAL_PWM_ENABLE[nr]);
2434                         if (val == 255)
2435                                 reg &= ~0x01;
2436                         else
2437                                 reg |= 0x01;
2438                         nct6775_write_value(data,
2439                                             NCT6779_REG_CRITICAL_PWM_ENABLE[nr],
2440                                             reg);
2441                         break;
2442                 }
2443         }
2444         mutex_unlock(&data->update_lock);
2445         return count;
2446 }
2447
2448 static ssize_t
2449 show_auto_temp(struct device *dev, struct device_attribute *attr, char *buf)
2450 {
2451         struct nct6775_data *data = nct6775_update_device(dev);
2452         struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
2453         int nr = sattr->nr;
2454         int point = sattr->index;
2455
2456         /*
2457          * We don't know for sure if the temperature is signed or unsigned.
2458          * Assume it is unsigned.
2459          */
2460         return sprintf(buf, "%d\n", data->auto_temp[nr][point] * 1000);
2461 }
2462
2463 static ssize_t
2464 store_auto_temp(struct device *dev, struct device_attribute *attr,
2465                 const char *buf, size_t count)
2466 {
2467         struct nct6775_data *data = dev_get_drvdata(dev);
2468         struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
2469         int nr = sattr->nr;
2470         int point = sattr->index;
2471         unsigned long val;
2472         int err;
2473
2474         err = kstrtoul(buf, 10, &val);
2475         if (err)
2476                 return err;
2477         if (val > 255000)
2478                 return -EINVAL;
2479
2480         mutex_lock(&data->update_lock);
2481         data->auto_temp[nr][point] = DIV_ROUND_CLOSEST(val, 1000);
2482         if (point < data->auto_pwm_num) {
2483                 nct6775_write_value(data,
2484                                     NCT6775_AUTO_TEMP(data, nr, point),
2485                                     data->auto_temp[nr][point]);
2486         } else {
2487                 nct6775_write_value(data, data->REG_CRITICAL_TEMP[nr],
2488                                     data->auto_temp[nr][point]);
2489         }
2490         mutex_unlock(&data->update_lock);
2491         return count;
2492 }
2493
2494 /*
2495  * The number of auto-point trip points is chip dependent.
2496  * Need to check support while generating/removing attribute files.
2497  */
2498 static struct sensor_device_attribute_2 sda_auto_pwm_arrays[] = {
2499         SENSOR_ATTR_2(pwm1_auto_point1_pwm, S_IWUSR | S_IRUGO,
2500                           show_auto_pwm, store_auto_pwm, 0, 0),
2501         SENSOR_ATTR_2(pwm1_auto_point1_temp, S_IWUSR | S_IRUGO,
2502                           show_auto_temp, store_auto_temp, 0, 0),
2503         SENSOR_ATTR_2(pwm1_auto_point1_temp_hyst, S_IWUSR | S_IRUGO,
2504                           show_auto_temp_hyst, store_auto_temp_hyst, 0, 0),
2505         SENSOR_ATTR_2(pwm1_auto_point2_pwm, S_IWUSR | S_IRUGO,
2506                           show_auto_pwm, store_auto_pwm, 0, 1),
2507         SENSOR_ATTR_2(pwm1_auto_point2_temp, S_IWUSR | S_IRUGO,
2508                           show_auto_temp, store_auto_temp, 0, 1),
2509         SENSOR_ATTR_2(pwm1_auto_point2_temp_hyst, S_IWUSR | S_IRUGO,
2510                           show_auto_temp_hyst, store_auto_temp_hyst, 0, 1),
2511         SENSOR_ATTR_2(pwm1_auto_point3_pwm, S_IWUSR | S_IRUGO,
2512                           show_auto_pwm, store_auto_pwm, 0, 2),
2513         SENSOR_ATTR_2(pwm1_auto_point3_temp, S_IWUSR | S_IRUGO,
2514                           show_auto_temp, store_auto_temp, 0, 2),
2515         SENSOR_ATTR_2(pwm1_auto_point3_temp_hyst, S_IWUSR | S_IRUGO,
2516                           show_auto_temp_hyst, store_auto_temp_hyst, 0, 2),
2517         SENSOR_ATTR_2(pwm1_auto_point4_pwm, S_IWUSR | S_IRUGO,
2518                           show_auto_pwm, store_auto_pwm, 0, 3),
2519         SENSOR_ATTR_2(pwm1_auto_point4_temp, S_IWUSR | S_IRUGO,
2520                           show_auto_temp, store_auto_temp, 0, 3),
2521         SENSOR_ATTR_2(pwm1_auto_point4_temp_hyst, S_IWUSR | S_IRUGO,
2522                           show_auto_temp_hyst, store_auto_temp_hyst, 0, 3),
2523         SENSOR_ATTR_2(pwm1_auto_point5_pwm, S_IWUSR | S_IRUGO,
2524                           show_auto_pwm, store_auto_pwm, 0, 4),
2525         SENSOR_ATTR_2(pwm1_auto_point5_temp, S_IWUSR | S_IRUGO,
2526                           show_auto_temp, store_auto_temp, 0, 4),
2527         SENSOR_ATTR_2(pwm1_auto_point5_temp_hyst, S_IWUSR | S_IRUGO,
2528                           show_auto_temp_hyst, store_auto_temp_hyst, 0, 4),
2529         SENSOR_ATTR_2(pwm1_auto_point6_pwm, S_IWUSR | S_IRUGO,
2530                           show_auto_pwm, store_auto_pwm, 0, 5),
2531         SENSOR_ATTR_2(pwm1_auto_point6_temp, S_IWUSR | S_IRUGO,
2532                           show_auto_temp, store_auto_temp, 0, 5),
2533         SENSOR_ATTR_2(pwm1_auto_point6_temp_hyst, S_IWUSR | S_IRUGO,
2534                           show_auto_temp_hyst, store_auto_temp_hyst, 0, 5),
2535         SENSOR_ATTR_2(pwm1_auto_point7_pwm, S_IWUSR | S_IRUGO,
2536                           show_auto_pwm, store_auto_pwm, 0, 6),
2537         SENSOR_ATTR_2(pwm1_auto_point7_temp, S_IWUSR | S_IRUGO,
2538                           show_auto_temp, store_auto_temp, 0, 6),
2539         SENSOR_ATTR_2(pwm1_auto_point7_temp_hyst, S_IWUSR | S_IRUGO,
2540                           show_auto_temp_hyst, store_auto_temp_hyst, 0, 6),
2541
2542         SENSOR_ATTR_2(pwm2_auto_point1_pwm, S_IWUSR | S_IRUGO,
2543                           show_auto_pwm, store_auto_pwm, 1, 0),
2544         SENSOR_ATTR_2(pwm2_auto_point1_temp, S_IWUSR | S_IRUGO,
2545                           show_auto_temp, store_auto_temp, 1, 0),
2546         SENSOR_ATTR_2(pwm2_auto_point1_temp_hyst, S_IWUSR | S_IRUGO,
2547                           show_auto_temp_hyst, store_auto_temp_hyst, 1, 0),
2548         SENSOR_ATTR_2(pwm2_auto_point2_pwm, S_IWUSR | S_IRUGO,
2549                           show_auto_pwm, store_auto_pwm, 1, 1),
2550         SENSOR_ATTR_2(pwm2_auto_point2_temp, S_IWUSR | S_IRUGO,
2551                           show_auto_temp, store_auto_temp, 1, 1),
2552         SENSOR_ATTR_2(pwm2_auto_point2_temp_hyst, S_IWUSR | S_IRUGO,
2553                           show_auto_temp_hyst, store_auto_temp_hyst, 1, 1),
2554         SENSOR_ATTR_2(pwm2_auto_point3_pwm, S_IWUSR | S_IRUGO,
2555                           show_auto_pwm, store_auto_pwm, 1, 2),
2556         SENSOR_ATTR_2(pwm2_auto_point3_temp, S_IWUSR | S_IRUGO,
2557                           show_auto_temp, store_auto_temp, 1, 2),
2558         SENSOR_ATTR_2(pwm2_auto_point3_temp_hyst, S_IWUSR | S_IRUGO,
2559                           show_auto_temp_hyst, store_auto_temp_hyst, 1, 2),
2560         SENSOR_ATTR_2(pwm2_auto_point4_pwm, S_IWUSR | S_IRUGO,
2561                           show_auto_pwm, store_auto_pwm, 1, 3),
2562         SENSOR_ATTR_2(pwm2_auto_point4_temp, S_IWUSR | S_IRUGO,
2563                           show_auto_temp, store_auto_temp, 1, 3),
2564         SENSOR_ATTR_2(pwm2_auto_point4_temp_hyst, S_IWUSR | S_IRUGO,
2565                           show_auto_temp_hyst, store_auto_temp_hyst, 1, 3),
2566         SENSOR_ATTR_2(pwm2_auto_point5_pwm, S_IWUSR | S_IRUGO,
2567                           show_auto_pwm, store_auto_pwm, 1, 4),
2568         SENSOR_ATTR_2(pwm2_auto_point5_temp, S_IWUSR | S_IRUGO,
2569                           show_auto_temp, store_auto_temp, 1, 4),
2570         SENSOR_ATTR_2(pwm2_auto_point5_temp_hyst, S_IWUSR | S_IRUGO,
2571                           show_auto_temp_hyst, store_auto_temp_hyst, 1, 4),
2572         SENSOR_ATTR_2(pwm2_auto_point6_pwm, S_IWUSR | S_IRUGO,
2573                           show_auto_pwm, store_auto_pwm, 1, 5),
2574         SENSOR_ATTR_2(pwm2_auto_point6_temp, S_IWUSR | S_IRUGO,
2575                           show_auto_temp, store_auto_temp, 1, 5),
2576         SENSOR_ATTR_2(pwm2_auto_point6_temp_hyst, S_IWUSR | S_IRUGO,
2577                           show_auto_temp_hyst, store_auto_temp_hyst, 1, 5),
2578         SENSOR_ATTR_2(pwm2_auto_point7_pwm, S_IWUSR | S_IRUGO,
2579                           show_auto_pwm, store_auto_pwm, 1, 6),
2580         SENSOR_ATTR_2(pwm2_auto_point7_temp, S_IWUSR | S_IRUGO,
2581                           show_auto_temp, store_auto_temp, 1, 6),
2582         SENSOR_ATTR_2(pwm2_auto_point7_temp_hyst, S_IWUSR | S_IRUGO,
2583                           show_auto_temp_hyst, store_auto_temp_hyst, 1, 6),
2584
2585         SENSOR_ATTR_2(pwm3_auto_point1_pwm, S_IWUSR | S_IRUGO,
2586                           show_auto_pwm, store_auto_pwm, 2, 0),
2587         SENSOR_ATTR_2(pwm3_auto_point1_temp, S_IWUSR | S_IRUGO,
2588                           show_auto_temp, store_auto_temp, 2, 0),
2589         SENSOR_ATTR_2(pwm3_auto_point1_temp_hyst, S_IWUSR | S_IRUGO,
2590                           show_auto_temp_hyst, store_auto_temp_hyst, 2, 0),
2591         SENSOR_ATTR_2(pwm3_auto_point2_pwm, S_IWUSR | S_IRUGO,
2592                           show_auto_pwm, store_auto_pwm, 2, 1),
2593         SENSOR_ATTR_2(pwm3_auto_point2_temp, S_IWUSR | S_IRUGO,
2594                           show_auto_temp, store_auto_temp, 2, 1),
2595         SENSOR_ATTR_2(pwm3_auto_point2_temp_hyst, S_IWUSR | S_IRUGO,
2596                           show_auto_temp_hyst, store_auto_temp_hyst, 2, 1),
2597         SENSOR_ATTR_2(pwm3_auto_point3_pwm, S_IWUSR | S_IRUGO,
2598                           show_auto_pwm, store_auto_pwm, 2, 2),
2599         SENSOR_ATTR_2(pwm3_auto_point3_temp, S_IWUSR | S_IRUGO,
2600                           show_auto_temp, store_auto_temp, 2, 2),
2601         SENSOR_ATTR_2(pwm3_auto_point3_temp_hyst, S_IWUSR | S_IRUGO,
2602                           show_auto_temp_hyst, store_auto_temp_hyst, 2, 2),
2603         SENSOR_ATTR_2(pwm3_auto_point4_pwm, S_IWUSR | S_IRUGO,
2604                           show_auto_pwm, store_auto_pwm, 2, 3),
2605         SENSOR_ATTR_2(pwm3_auto_point4_temp, S_IWUSR | S_IRUGO,
2606                           show_auto_temp, store_auto_temp, 2, 3),
2607         SENSOR_ATTR_2(pwm3_auto_point4_temp_hyst, S_IWUSR | S_IRUGO,
2608                           show_auto_temp_hyst, store_auto_temp_hyst, 2, 3),
2609         SENSOR_ATTR_2(pwm3_auto_point5_pwm, S_IWUSR | S_IRUGO,
2610                           show_auto_pwm, store_auto_pwm, 2, 4),
2611         SENSOR_ATTR_2(pwm3_auto_point5_temp, S_IWUSR | S_IRUGO,
2612                           show_auto_temp, store_auto_temp, 2, 4),
2613         SENSOR_ATTR_2(pwm3_auto_point5_temp_hyst, S_IWUSR | S_IRUGO,
2614                           show_auto_temp_hyst, store_auto_temp_hyst, 2, 4),
2615         SENSOR_ATTR_2(pwm3_auto_point6_pwm, S_IWUSR | S_IRUGO,
2616                           show_auto_pwm, store_auto_pwm, 2, 5),
2617         SENSOR_ATTR_2(pwm3_auto_point6_temp, S_IWUSR | S_IRUGO,
2618                           show_auto_temp, store_auto_temp, 2, 5),
2619         SENSOR_ATTR_2(pwm3_auto_point6_temp_hyst, S_IWUSR | S_IRUGO,
2620                           show_auto_temp_hyst, store_auto_temp_hyst, 2, 5),
2621         SENSOR_ATTR_2(pwm3_auto_point7_pwm, S_IWUSR | S_IRUGO,
2622                           show_auto_pwm, store_auto_pwm, 2, 6),
2623         SENSOR_ATTR_2(pwm3_auto_point7_temp, S_IWUSR | S_IRUGO,
2624                           show_auto_temp, store_auto_temp, 2, 6),
2625         SENSOR_ATTR_2(pwm3_auto_point7_temp_hyst, S_IWUSR | S_IRUGO,
2626                           show_auto_temp_hyst, store_auto_temp_hyst, 2, 6),
2627
2628         SENSOR_ATTR_2(pwm4_auto_point1_pwm, S_IWUSR | S_IRUGO,
2629                           show_auto_pwm, store_auto_pwm, 3, 0),
2630         SENSOR_ATTR_2(pwm4_auto_point1_temp, S_IWUSR | S_IRUGO,
2631                           show_auto_temp, store_auto_temp, 3, 0),
2632         SENSOR_ATTR_2(pwm4_auto_point1_temp_hyst, S_IWUSR | S_IRUGO,
2633                           show_auto_temp_hyst, store_auto_temp_hyst, 3, 0),
2634         SENSOR_ATTR_2(pwm4_auto_point2_pwm, S_IWUSR | S_IRUGO,
2635                           show_auto_pwm, store_auto_pwm, 3, 1),
2636         SENSOR_ATTR_2(pwm4_auto_point2_temp, S_IWUSR | S_IRUGO,
2637                           show_auto_temp, store_auto_temp, 3, 1),
2638         SENSOR_ATTR_2(pwm4_auto_point2_temp_hyst, S_IWUSR | S_IRUGO,
2639                           show_auto_temp_hyst, store_auto_temp_hyst, 3, 1),
2640         SENSOR_ATTR_2(pwm4_auto_point3_pwm, S_IWUSR | S_IRUGO,
2641                           show_auto_pwm, store_auto_pwm, 3, 2),
2642         SENSOR_ATTR_2(pwm4_auto_point3_temp, S_IWUSR | S_IRUGO,
2643                           show_auto_temp, store_auto_temp, 3, 2),
2644         SENSOR_ATTR_2(pwm4_auto_point3_temp_hyst, S_IWUSR | S_IRUGO,
2645                           show_auto_temp_hyst, store_auto_temp_hyst, 3, 2),
2646         SENSOR_ATTR_2(pwm4_auto_point4_pwm, S_IWUSR | S_IRUGO,
2647                           show_auto_pwm, store_auto_pwm, 3, 3),
2648         SENSOR_ATTR_2(pwm4_auto_point4_temp, S_IWUSR | S_IRUGO,
2649                           show_auto_temp, store_auto_temp, 3, 3),
2650         SENSOR_ATTR_2(pwm4_auto_point4_temp_hyst, S_IWUSR | S_IRUGO,
2651                           show_auto_temp_hyst, store_auto_temp_hyst, 3, 3),
2652         SENSOR_ATTR_2(pwm4_auto_point5_pwm, S_IWUSR | S_IRUGO,
2653                           show_auto_pwm, store_auto_pwm, 3, 4),
2654         SENSOR_ATTR_2(pwm4_auto_point5_temp, S_IWUSR | S_IRUGO,
2655                           show_auto_temp, store_auto_temp, 3, 4),
2656         SENSOR_ATTR_2(pwm4_auto_point5_temp_hyst, S_IWUSR | S_IRUGO,
2657                           show_auto_temp_hyst, store_auto_temp_hyst, 3, 4),
2658         SENSOR_ATTR_2(pwm4_auto_point6_pwm, S_IWUSR | S_IRUGO,
2659                           show_auto_pwm, store_auto_pwm, 3, 5),
2660         SENSOR_ATTR_2(pwm4_auto_point6_temp, S_IWUSR | S_IRUGO,
2661                           show_auto_temp, store_auto_temp, 3, 5),
2662         SENSOR_ATTR_2(pwm4_auto_point6_temp_hyst, S_IWUSR | S_IRUGO,
2663                           show_auto_temp_hyst, store_auto_temp_hyst, 3, 5),
2664         SENSOR_ATTR_2(pwm4_auto_point7_pwm, S_IWUSR | S_IRUGO,
2665                           show_auto_pwm, store_auto_pwm, 3, 6),
2666         SENSOR_ATTR_2(pwm4_auto_point7_temp, S_IWUSR | S_IRUGO,
2667                           show_auto_temp, store_auto_temp, 3, 6),
2668         SENSOR_ATTR_2(pwm4_auto_point7_temp_hyst, S_IWUSR | S_IRUGO,
2669                           show_auto_temp_hyst, store_auto_temp_hyst, 3, 6),
2670
2671         SENSOR_ATTR_2(pwm5_auto_point1_pwm, S_IWUSR | S_IRUGO,
2672                           show_auto_pwm, store_auto_pwm, 4, 0),
2673         SENSOR_ATTR_2(pwm5_auto_point1_temp, S_IWUSR | S_IRUGO,
2674                           show_auto_temp, store_auto_temp, 4, 0),
2675         SENSOR_ATTR_2(pwm5_auto_point1_temp_hyst, S_IWUSR | S_IRUGO,
2676                           show_auto_temp_hyst, store_auto_temp_hyst, 4, 0),
2677         SENSOR_ATTR_2(pwm5_auto_point2_pwm, S_IWUSR | S_IRUGO,
2678                           show_auto_pwm, store_auto_pwm, 4, 1),
2679         SENSOR_ATTR_2(pwm5_auto_point2_temp, S_IWUSR | S_IRUGO,
2680                           show_auto_temp, store_auto_temp, 4, 1),
2681         SENSOR_ATTR_2(pwm5_auto_point2_temp_hyst, S_IWUSR | S_IRUGO,
2682                           show_auto_temp_hyst, store_auto_temp_hyst, 4, 1),
2683         SENSOR_ATTR_2(pwm5_auto_point3_pwm, S_IWUSR | S_IRUGO,
2684                           show_auto_pwm, store_auto_pwm, 4, 2),
2685         SENSOR_ATTR_2(pwm5_auto_point3_temp, S_IWUSR | S_IRUGO,
2686                           show_auto_temp, store_auto_temp, 4, 2),
2687         SENSOR_ATTR_2(pwm5_auto_point3_temp_hyst, S_IWUSR | S_IRUGO,
2688                           show_auto_temp_hyst, store_auto_temp_hyst, 4, 2),
2689         SENSOR_ATTR_2(pwm5_auto_point4_pwm, S_IWUSR | S_IRUGO,
2690                           show_auto_pwm, store_auto_pwm, 4, 3),
2691         SENSOR_ATTR_2(pwm5_auto_point4_temp, S_IWUSR | S_IRUGO,
2692                           show_auto_temp, store_auto_temp, 4, 3),
2693         SENSOR_ATTR_2(pwm5_auto_point4_temp_hyst, S_IWUSR | S_IRUGO,
2694                           show_auto_temp_hyst, store_auto_temp_hyst, 4, 3),
2695         SENSOR_ATTR_2(pwm5_auto_point5_pwm, S_IWUSR | S_IRUGO,
2696                           show_auto_pwm, store_auto_pwm, 4, 4),
2697         SENSOR_ATTR_2(pwm5_auto_point5_temp, S_IWUSR | S_IRUGO,
2698                           show_auto_temp, store_auto_temp, 4, 4),
2699         SENSOR_ATTR_2(pwm5_auto_point5_temp_hyst, S_IWUSR | S_IRUGO,
2700                           show_auto_temp_hyst, store_auto_temp_hyst, 4, 4),
2701         SENSOR_ATTR_2(pwm5_auto_point6_pwm, S_IWUSR | S_IRUGO,
2702                           show_auto_pwm, store_auto_pwm, 4, 5),
2703         SENSOR_ATTR_2(pwm5_auto_point6_temp, S_IWUSR | S_IRUGO,
2704                           show_auto_temp, store_auto_temp, 4, 5),
2705         SENSOR_ATTR_2(pwm5_auto_point6_temp_hyst, S_IWUSR | S_IRUGO,
2706                           show_auto_temp_hyst, store_auto_temp_hyst, 4, 5),
2707         SENSOR_ATTR_2(pwm5_auto_point7_pwm, S_IWUSR | S_IRUGO,
2708                           show_auto_pwm, store_auto_pwm, 4, 6),
2709         SENSOR_ATTR_2(pwm5_auto_point7_temp, S_IWUSR | S_IRUGO,
2710                           show_auto_temp, store_auto_temp, 4, 6),
2711         SENSOR_ATTR_2(pwm5_auto_point7_temp_hyst, S_IWUSR | S_IRUGO,
2712                           show_auto_temp_hyst, store_auto_temp_hyst, 4, 6),
2713 };
2714
2715 static ssize_t
2716 show_vid(struct device *dev, struct device_attribute *attr, char *buf)
2717 {
2718         struct nct6775_data *data = dev_get_drvdata(dev);
2719         return sprintf(buf, "%d\n", vid_from_reg(data->vid, data->vrm));
2720 }
2721
2722 static DEVICE_ATTR(cpu0_vid, S_IRUGO, show_vid, NULL);
2723
2724 /* Case open detection */
2725
2726 static ssize_t
2727 show_caseopen(struct device *dev, struct device_attribute *attr, char *buf)
2728 {
2729         struct nct6775_data *data = nct6775_update_device(dev);
2730
2731         return sprintf(buf, "%d\n",
2732                 !!(data->caseopen & to_sensor_dev_attr_2(attr)->index));
2733 }
2734
2735 static ssize_t
2736 clear_caseopen(struct device *dev, struct device_attribute *attr,
2737                const char *buf, size_t count)
2738 {
2739         struct nct6775_data *data = dev_get_drvdata(dev);
2740         struct nct6775_sio_data *sio_data = dev->platform_data;
2741         int nr = to_sensor_dev_attr(attr)->index;
2742         unsigned long val;
2743         u8 reg;
2744
2745         if (kstrtoul(buf, 10, &val) || val != 0)
2746                 return -EINVAL;
2747
2748         mutex_lock(&data->update_lock);
2749
2750         /*
2751          * Use CR registers to clear caseopen status.
2752          * The CR registers are the same for all chips, and not all chips
2753          * support clearing the caseopen status through "regular" registers.
2754          */
2755         superio_enter(sio_data->sioreg);
2756         superio_select(sio_data->sioreg, NCT6775_LD_ACPI);
2757         reg = superio_inb(sio_data->sioreg, NCT6775_REG_CR_CASEOPEN_CLR[nr]);
2758         reg |= NCT6775_CR_CASEOPEN_CLR_MASK[nr];
2759         superio_outb(sio_data->sioreg, NCT6775_REG_CR_CASEOPEN_CLR[nr], reg);
2760         superio_exit(sio_data->sioreg);
2761
2762         data->valid = 0;        /* Force cache refresh */
2763
2764         mutex_unlock(&data->update_lock);
2765
2766         return count;
2767 }
2768
2769 static struct sensor_device_attribute sda_caseopen[] = {
2770         SENSOR_ATTR(intrusion0_alarm, S_IWUSR | S_IRUGO, show_caseopen,
2771                     clear_caseopen, 0),
2772         SENSOR_ATTR(intrusion1_alarm, S_IWUSR | S_IRUGO, show_caseopen,
2773                     clear_caseopen, 1),
2774 };
2775
2776 /*
2777  * Driver and device management
2778  */
2779
2780 static void nct6775_device_remove_files(struct device *dev)
2781 {
2782         /*
2783          * some entries in the following arrays may not have been used in
2784          * device_create_file(), but device_remove_file() will ignore them
2785          */
2786         int i;
2787         struct nct6775_data *data = dev_get_drvdata(dev);
2788
2789         for (i = 0; i < data->pwm_num; i++)
2790                 sysfs_remove_group(&dev->kobj, &nct6775_group_pwm[i]);
2791
2792         for (i = 0; i < ARRAY_SIZE(sda_weight_duty_base); i++)
2793                 device_remove_file(dev, &sda_weight_duty_base[i].dev_attr);
2794
2795         for (i = 0; i < ARRAY_SIZE(sda_auto_pwm_arrays); i++)
2796                 device_remove_file(dev, &sda_auto_pwm_arrays[i].dev_attr);
2797
2798         for (i = 0; i < data->in_num; i++)
2799                 sysfs_remove_group(&dev->kobj, &nct6775_group_in[i]);
2800
2801         for (i = 0; i < 5; i++) {
2802                 device_remove_file(dev, &sda_fan_input[i].dev_attr);
2803                 device_remove_file(dev, &sda_fan_alarm[i].dev_attr);
2804                 device_remove_file(dev, &sda_fan_div[i].dev_attr);
2805                 device_remove_file(dev, &sda_fan_min[i].dev_attr);
2806         }
2807         for (i = 0; i < NUM_REG_TEMP; i++) {
2808                 if (!(data->have_temp & (1 << i)))
2809                         continue;
2810                 device_remove_file(dev, &sda_temp_input[i].dev_attr);
2811                 device_remove_file(dev, &sda_temp_label[i].dev_attr);
2812                 device_remove_file(dev, &sda_temp_max[i].dev_attr);
2813                 device_remove_file(dev, &sda_temp_max_hyst[i].dev_attr);
2814                 if (i > data->temp_num_fixed)
2815                         continue;
2816                 device_remove_file(dev, &sda_temp_type[i].dev_attr);
2817                 device_remove_file(dev, &sda_temp_offset[i].dev_attr);
2818                 if (i > 2)
2819                         continue;
2820                 device_remove_file(dev, &sda_temp_alarm[i].dev_attr);
2821         }
2822
2823         device_remove_file(dev, &sda_caseopen[0].dev_attr);
2824         device_remove_file(dev, &sda_caseopen[1].dev_attr);
2825
2826         device_remove_file(dev, &dev_attr_name);
2827         device_remove_file(dev, &dev_attr_cpu0_vid);
2828 }
2829
2830 /* Get the monitoring functions started */
2831 static inline void __devinit nct6775_init_device(struct nct6775_data *data)
2832 {
2833         int i;
2834         u8 tmp, diode;
2835
2836         /* Start monitoring if needed */
2837         if (data->REG_CONFIG) {
2838                 tmp = nct6775_read_value(data, data->REG_CONFIG);
2839                 if (!(tmp & 0x01))
2840                         nct6775_write_value(data, data->REG_CONFIG, tmp | 0x01);
2841         }
2842
2843         /* Enable temperature sensors if needed */
2844         for (i = 0; i < NUM_REG_TEMP; i++) {
2845                 if (!(data->have_temp & (1 << i)))
2846                         continue;
2847                 if (!data->reg_temp_config[i])
2848                         continue;
2849                 tmp = nct6775_read_value(data, data->reg_temp_config[i]);
2850                 if (tmp & 0x01)
2851                         nct6775_write_value(data, data->reg_temp_config[i],
2852                                             tmp & 0xfe);
2853         }
2854
2855         /* Enable VBAT monitoring if needed */
2856         tmp = nct6775_read_value(data, data->REG_VBAT);
2857         if (!(tmp & 0x01))
2858                 nct6775_write_value(data, data->REG_VBAT, tmp | 0x01);
2859
2860         diode = nct6775_read_value(data, data->REG_DIODE);
2861
2862         for (i = 0; i < data->num_temp_fixed; i++) {
2863                 if ((tmp & (0x02 << i)))        /* diode */
2864                         data->temp_type[i] = 3 - ((diode >> i) & 0x02);
2865                 else                            /* thermistor */
2866                         data->temp_type[i] = 4;
2867         }
2868 }
2869
2870 static void __devinit
2871 nct6775_check_fan_inputs(const struct nct6775_sio_data *sio_data,
2872                          struct nct6775_data *data)
2873 {
2874         int regval;
2875         bool fan3pin, fan3min, fan4pin, fan4min, fan5pin;
2876         bool pwm3pin, pwm4pin, pwm5pin;
2877
2878         superio_enter(sio_data->sioreg);
2879
2880         /* fan4 and fan5 share some pins with the GPIO and serial flash */
2881         if (data->kind == nct6775) {
2882                 regval = superio_inb(sio_data->sioreg, 0x2c);
2883
2884                 fan3pin = regval & (1 << 6);
2885                 fan3min = fan3pin;
2886                 pwm3pin = regval & (1 << 7);
2887
2888                 /* On NCT6775, fan4 shares pins with the fdc interface */
2889                 fan4pin = !(superio_inb(sio_data->sioreg, 0x2A) & 0x80);
2890                 fan4min = 0;
2891                 fan5pin = 0;
2892                 pwm4pin = 0;
2893                 pwm5pin = 0;
2894         } else if (data->kind == nct6776) {
2895                 bool gpok = superio_inb(sio_data->sioreg, 0x27) & 0x80;
2896
2897                 superio_select(sio_data->sioreg, NCT6775_LD_HWM);
2898                 regval = superio_inb(sio_data->sioreg, SIO_REG_ENABLE);
2899
2900                 if (regval & 0x80)
2901                         fan3pin = gpok;
2902                 else
2903                         fan3pin = !(superio_inb(sio_data->sioreg, 0x24) & 0x40);
2904
2905                 if (regval & 0x40)
2906                         fan4pin = gpok;
2907                 else
2908                         fan4pin = superio_inb(sio_data->sioreg, 0x1C) & 0x01;
2909
2910                 if (regval & 0x20)
2911                         fan5pin = gpok;
2912                 else
2913                         fan5pin = superio_inb(sio_data->sioreg, 0x1C) & 0x02;
2914
2915                 fan4min = fan4pin;
2916                 fan3min = fan3pin;
2917                 pwm3pin = fan3pin;
2918                 pwm4pin = 0;
2919                 pwm5pin = 0;
2920         } else {        /* NCT6779D */
2921                 regval = superio_inb(sio_data->sioreg, 0x1c);
2922
2923                 fan3pin = !(regval & (1 << 5));
2924                 fan4pin = !(regval & (1 << 6));
2925                 fan5pin = !(regval & (1 << 7));
2926
2927                 pwm3pin = !(regval & (1 << 0));
2928                 pwm4pin = !(regval & (1 << 1));
2929                 pwm5pin = !(regval & (1 << 2));
2930
2931                 fan3min = 0;
2932                 fan4min = 0;
2933         }
2934
2935         superio_exit(sio_data->sioreg);
2936
2937         data->has_fan = data->has_fan_min = 0x03; /* fan1 and fan2 */
2938         data->has_fan |= (fan3pin << 2);
2939         data->has_fan_min |= (fan3min << 2);
2940
2941         data->has_fan |= (fan4pin << 3) | (fan5pin << 4);
2942         data->has_fan_min |= (fan4min << 3) | (fan5pin << 4);
2943
2944         data->has_pwm = 0x03 | (pwm3pin << 2) | (pwm4pin << 3) | (pwm5pin << 4);
2945 }
2946
2947 static int __devinit nct6775_probe(struct platform_device *pdev)
2948 {
2949         struct device *dev = &pdev->dev;
2950         struct nct6775_sio_data *sio_data = dev->platform_data;
2951         struct nct6775_data *data;
2952         struct resource *res;
2953         int i, s, err = 0;
2954         int src, mask = 0;
2955         const u16 *reg_temp, *reg_temp_over, *reg_temp_hyst, *reg_temp_config;
2956
2957         res = platform_get_resource(pdev, IORESOURCE_IO, 0);
2958         if (!request_region(res->start, IOREGION_LENGTH, DRVNAME)) {
2959                 err = -EBUSY;
2960                 dev_err(dev, "Failed to request region 0x%lx-0x%lx\n",
2961                         (unsigned long)res->start,
2962                         (unsigned long)res->start + IOREGION_LENGTH - 1);
2963                 goto exit;
2964         }
2965
2966         data = devm_kzalloc(&pdev->dev, sizeof(struct nct6775_data),
2967                             GFP_KERNEL);
2968         if (!data) {
2969                 err = -ENOMEM;
2970                 goto exit_release;
2971         }
2972
2973         data->kind = sio_data->kind;
2974         data->addr = res->start;
2975         mutex_init(&data->lock);
2976         mutex_init(&data->update_lock);
2977         data->name = nct6775_device_names[data->kind];
2978         platform_set_drvdata(pdev, data);
2979
2980         switch (data->kind) {
2981         case nct6775:
2982                 data->in_num = 9;
2983                 data->have_in = 0x1ff;
2984                 data->pwm_num = 3;
2985                 data->auto_pwm_num = 6;
2986                 data->has_fan_div = true;
2987                 data->num_temp_fixed = 3;
2988
2989                 data->fan_from_reg = fan_from_reg16;
2990                 data->fan_from_reg_min = fan_from_reg8;
2991
2992                 data->REG_CONFIG = NCT6775_REG_CONFIG;
2993                 data->REG_VBAT = NCT6775_REG_VBAT;
2994                 data->REG_DIODE = NCT6775_REG_DIODE;
2995                 data->REG_VIN = NCT6775_REG_IN;
2996                 data->REG_IN_MINMAX[0] = NCT6775_REG_IN_MIN;
2997                 data->REG_IN_MINMAX[1] = NCT6775_REG_IN_MAX;
2998                 data->REG_TARGET = NCT6775_REG_TARGET;
2999                 data->REG_FAN = NCT6775_REG_FAN;
3000                 data->REG_FAN_MODE = NCT6775_REG_FAN_MODE;
3001                 data->REG_FAN_MIN = NCT6775_REG_FAN_MIN;
3002                 data->REG_FAN_TIME[0] = NCT6775_REG_FAN_STOP_TIME;
3003                 data->REG_FAN_TIME[1] = NCT6775_REG_FAN_STEP_UP_TIME;
3004                 data->REG_FAN_TIME[2] = NCT6775_REG_FAN_STEP_DOWN_TIME;
3005                 data->REG_PWM[0] = NCT6775_REG_PWM;
3006                 data->REG_PWM[1] = NCT6775_REG_FAN_START_OUTPUT;
3007                 data->REG_PWM[2] = NCT6775_REG_FAN_STOP_OUTPUT;
3008                 data->REG_PWM_READ = NCT6775_REG_PWM_READ;
3009                 data->REG_PWM_MODE = NCT6775_REG_PWM_MODE;
3010                 data->PWM_MODE_MASK = NCT6775_PWM_MODE_MASK;
3011                 data->REG_TEMP_MON = NCT6775_REG_TEMP_MON;
3012                 data->REG_AUTO_TEMP = NCT6775_REG_AUTO_TEMP;
3013                 data->REG_AUTO_PWM = NCT6775_REG_AUTO_PWM;
3014                 data->REG_CRITICAL_TEMP = NCT6775_REG_CRITICAL_TEMP;
3015                 data->REG_CRITICAL_TEMP_TOLERANCE
3016                   = NCT6775_REG_CRITICAL_TEMP_TOLERANCE;
3017                 data->REG_TEMP_OFFSET = NCT6775_REG_TEMP_OFFSET;
3018                 data->REG_TEMP_SOURCE = NCT6775_REG_TEMP_SOURCE;
3019                 data->REG_TEMP_SEL[0] = NCT6775_REG_TEMP_SEL;
3020                 data->REG_TEMP_SEL[1] = NCT6775_REG_WEIGHT_TEMP_SEL;
3021                 data->REG_WEIGHT_TEMP[0] = NCT6775_REG_WEIGHT_TEMP_STEP;
3022                 data->REG_WEIGHT_TEMP[1] = NCT6775_REG_WEIGHT_TEMP_STEP_TOL;
3023                 data->REG_WEIGHT_TEMP[2] = NCT6775_REG_WEIGHT_TEMP_BASE;
3024                 data->REG_WEIGHT_DUTY[0] = NCT6775_REG_WEIGHT_DUTY_STEP;
3025                 data->REG_ALARM = NCT6775_REG_ALARM;
3026                 data->REG_CASEOPEN = NCT6775_REG_CASEOPEN;
3027                 data->CASEOPEN_MASK = NCT6775_CASEOPEN_MASK;
3028
3029                 reg_temp = NCT6775_REG_TEMP;
3030                 reg_temp_over = NCT6775_REG_TEMP_OVER;
3031                 reg_temp_hyst = NCT6775_REG_TEMP_HYST;
3032                 reg_temp_config = NCT6775_REG_TEMP_CONFIG;
3033
3034                 break;
3035         case nct6776:
3036                 data->in_num = 9;
3037                 data->have_in = 0x1ff;
3038                 data->pwm_num = 3;
3039                 data->auto_pwm_num = 4;
3040                 data->has_fan_div = false;
3041                 data->num_temp_fixed = 3;
3042
3043                 data->fan_from_reg = fan_from_reg13;
3044                 data->fan_from_reg_min = fan_from_reg13;
3045
3046                 data->REG_CONFIG = NCT6775_REG_CONFIG;
3047                 data->REG_VBAT = NCT6775_REG_VBAT;
3048                 data->REG_DIODE = NCT6775_REG_DIODE;
3049                 data->REG_VIN = NCT6775_REG_IN;
3050                 data->REG_IN_MINMAX[0] = NCT6775_REG_IN_MIN;
3051                 data->REG_IN_MINMAX[1] = NCT6775_REG_IN_MAX;
3052                 data->REG_TARGET = NCT6775_REG_TARGET;
3053                 data->REG_FAN = NCT6775_REG_FAN;
3054                 data->REG_FAN_MODE = NCT6775_REG_FAN_MODE;
3055                 data->REG_FAN_MIN = NCT6776_REG_FAN_MIN;
3056                 data->REG_FAN_TIME[0] = NCT6775_REG_FAN_STOP_TIME;
3057                 data->REG_FAN_TIME[1] = NCT6775_REG_FAN_STEP_UP_TIME;
3058                 data->REG_FAN_TIME[2] = NCT6775_REG_FAN_STEP_DOWN_TIME;
3059                 data->REG_PWM[0] = NCT6775_REG_PWM;
3060                 data->REG_PWM[1] = NCT6775_REG_FAN_START_OUTPUT;
3061                 data->REG_PWM[2] = NCT6775_REG_FAN_STOP_OUTPUT;
3062                 data->REG_PWM_READ = NCT6775_REG_PWM_READ;
3063                 data->REG_PWM_MODE = NCT6776_REG_PWM_MODE;
3064                 data->PWM_MODE_MASK = NCT6776_PWM_MODE_MASK;
3065                 data->REG_TEMP_MON = NCT6775_REG_TEMP_MON;
3066                 data->REG_AUTO_TEMP = NCT6775_REG_AUTO_TEMP;
3067                 data->REG_AUTO_PWM = NCT6775_REG_AUTO_PWM;
3068                 data->REG_CRITICAL_TEMP = NCT6775_REG_CRITICAL_TEMP;
3069                 data->REG_CRITICAL_TEMP_TOLERANCE
3070                   = NCT6775_REG_CRITICAL_TEMP_TOLERANCE;
3071                 data->REG_TEMP_OFFSET = NCT6775_REG_TEMP_OFFSET;
3072                 data->REG_TEMP_SOURCE = NCT6775_REG_TEMP_SOURCE;
3073                 data->REG_TEMP_SEL[0] = NCT6775_REG_TEMP_SEL;
3074                 data->REG_TEMP_SEL[1] = NCT6775_REG_WEIGHT_TEMP_SEL;
3075                 data->REG_WEIGHT_TEMP[0] = NCT6775_REG_WEIGHT_TEMP_STEP;
3076                 data->REG_WEIGHT_TEMP[1] = NCT6775_REG_WEIGHT_TEMP_STEP_TOL;
3077                 data->REG_WEIGHT_TEMP[2] = NCT6775_REG_WEIGHT_TEMP_BASE;
3078                 data->REG_WEIGHT_DUTY[0] = NCT6775_REG_WEIGHT_DUTY_STEP;
3079                 data->REG_WEIGHT_DUTY[1] = NCT6776_REG_WEIGHT_DUTY_BASE;
3080                 data->REG_ALARM = NCT6775_REG_ALARM;
3081                 data->REG_CASEOPEN = NCT6775_REG_CASEOPEN;
3082                 data->CASEOPEN_MASK = NCT6776_CASEOPEN_MASK;
3083
3084                 reg_temp = NCT6775_REG_TEMP;
3085                 reg_temp_over = NCT6775_REG_TEMP_OVER;
3086                 reg_temp_hyst = NCT6775_REG_TEMP_HYST;
3087                 reg_temp_config = NCT6776_REG_TEMP_CONFIG;
3088
3089                 break;
3090         case nct6779:
3091                 data->in_num = 15;
3092                 data->have_in = 0x7fff;
3093                 data->pwm_num = 5;
3094                 data->auto_pwm_num = 4;
3095                 data->has_fan_div = false;
3096                 data->num_temp_fixed = 6;
3097
3098                 data->fan_from_reg = fan_from_reg13;
3099                 data->fan_from_reg_min = fan_from_reg13;
3100
3101                 data->REG_CONFIG = NCT6775_REG_CONFIG;
3102                 data->REG_VBAT = NCT6775_REG_VBAT;
3103                 data->REG_DIODE = NCT6775_REG_DIODE;
3104                 data->REG_VIN = NCT6779_REG_IN;
3105                 data->REG_IN_MINMAX[0] = NCT6775_REG_IN_MIN;
3106                 data->REG_IN_MINMAX[1] = NCT6775_REG_IN_MAX;
3107                 data->REG_TARGET = NCT6775_REG_TARGET;
3108                 data->REG_FAN = NCT6779_REG_FAN;
3109                 data->REG_FAN_MODE = NCT6775_REG_FAN_MODE;
3110                 data->REG_FAN_MIN = NCT6776_REG_FAN_MIN;
3111                 data->REG_FAN_TIME[0] = NCT6775_REG_FAN_STOP_TIME;
3112                 data->REG_FAN_TIME[1] = NCT6775_REG_FAN_STEP_UP_TIME;
3113                 data->REG_FAN_TIME[2] = NCT6775_REG_FAN_STEP_DOWN_TIME;
3114                 data->REG_PWM[0] = NCT6775_REG_PWM;
3115                 data->REG_PWM[1] = NCT6775_REG_FAN_START_OUTPUT;
3116                 data->REG_PWM[2] = NCT6775_REG_FAN_STOP_OUTPUT;
3117                 data->REG_PWM_READ = NCT6775_REG_PWM_READ;
3118                 data->REG_PWM_MODE = NCT6776_REG_PWM_MODE;
3119                 data->PWM_MODE_MASK = NCT6776_PWM_MODE_MASK;
3120                 data->REG_TEMP_MON = NCT6775_REG_TEMP_MON;
3121                 data->REG_AUTO_TEMP = NCT6775_REG_AUTO_TEMP;
3122                 data->REG_AUTO_PWM = NCT6775_REG_AUTO_PWM;
3123                 data->REG_CRITICAL_TEMP = NCT6775_REG_CRITICAL_TEMP;
3124                 data->REG_CRITICAL_TEMP_TOLERANCE
3125                   = NCT6775_REG_CRITICAL_TEMP_TOLERANCE;
3126                 data->REG_TEMP_OFFSET = NCT6779_REG_TEMP_OFFSET;
3127                 data->REG_TEMP_SOURCE = NCT6775_REG_TEMP_SOURCE;
3128                 data->REG_TEMP_SEL[0] = NCT6775_REG_TEMP_SEL;
3129                 data->REG_TEMP_SEL[1] = NCT6775_REG_WEIGHT_TEMP_SEL;
3130                 data->REG_WEIGHT_TEMP[0] = NCT6775_REG_WEIGHT_TEMP_STEP;
3131                 data->REG_WEIGHT_TEMP[1] = NCT6775_REG_WEIGHT_TEMP_STEP_TOL;
3132                 data->REG_WEIGHT_TEMP[2] = NCT6775_REG_WEIGHT_TEMP_BASE;
3133                 data->REG_WEIGHT_DUTY[0] = NCT6775_REG_WEIGHT_DUTY_STEP;
3134                 data->REG_WEIGHT_DUTY[1] = NCT6776_REG_WEIGHT_DUTY_BASE;
3135                 data->REG_ALARM = NCT6779_REG_ALARM;
3136                 data->REG_CASEOPEN = NCT6775_REG_CASEOPEN;
3137                 data->CASEOPEN_MASK = NCT6776_CASEOPEN_MASK;
3138
3139                 reg_temp = NCT6779_REG_TEMP;
3140                 reg_temp_over = NCT6775_REG_TEMP_OVER;
3141                 reg_temp_hyst = NCT6775_REG_TEMP_HYST;
3142                 reg_temp_config = NCT6776_REG_TEMP_CONFIG;
3143
3144                 break;
3145         default:
3146                 err = -ENODEV;
3147                 goto exit_release;
3148         }
3149
3150         /* Default to no temperature inputs, code below will adjust as needed */
3151         data->have_temp = 0;
3152
3153         s = 3;  /* Index for first non-standard temperature */
3154         for (i = 0; i < NUM_REG_TEMP; i++) {
3155                 if (reg_temp[i] == 0)
3156                         continue;
3157
3158                 src = nct6775_read_value(data, data->REG_TEMP_SOURCE[i]);
3159                 src &= 0x1f;
3160
3161                 if (!src || (mask & (1 << src)))
3162                         continue;
3163
3164                 /* Use fixed index for SYSTIN(1), CPUTIN(2), AUXTIN(3) */
3165                 if (src <= data->num_temp_fixed) {
3166                         data->have_temp |= (1 << (src - 1));
3167                         mask |= 1 << src;
3168                         data->reg_temp[0][src - 1] = reg_temp[i];
3169                         data->reg_temp[1][src - 1] = reg_temp_over[i];
3170                         data->reg_temp[2][src - 1] = reg_temp_hyst[i];
3171                         data->reg_temp_config[src - 1] = reg_temp_config[i];
3172                         data->temp_src[src - 1] = src;
3173                         continue;
3174                 }
3175
3176                 /* Use dynamic index starting with <s> for other sources */
3177                 data->have_temp |= 1 << s;
3178                 mask |= 1 << src;
3179
3180                 data->reg_temp[0][s] = reg_temp[i];
3181                 data->reg_temp[1][s] = reg_temp_over[i];
3182                 data->reg_temp[2][s] = reg_temp_hyst[i];
3183                 data->reg_temp_config[s] = reg_temp_config[i];
3184
3185
3186                 data->temp_src[s] = src;
3187                 s++;
3188         }
3189
3190         switch (data->kind) {
3191         case nct6775:
3192                 data->temp_label = nct6775_temp_label;
3193                 break;
3194         case nct6776:
3195                 /*
3196                  * On NCT6776, AUXTIN and VIN3 pins are shared.
3197                  * Only way to detect it is to check if AUXTIN is used
3198                  * as a temperature source, and if that source is
3199                  * enabled.
3200                  *
3201                  * If that is the case, disable in6, which reports VIN3.
3202                  * Otherwise disable temp3.
3203                  */
3204                 if (data->temp_src[2] == 3) {
3205                         u8 reg;
3206
3207                         if (data->reg_temp_config[2])
3208                                 reg = nct6775_read_value(data,
3209                                         data->reg_temp_config[2]);
3210                         else
3211                                 reg = 0; /* Assume AUXTIN is used */
3212
3213                         if (reg & 0x01)
3214                                 data->have_temp &= ~(1 << 2);
3215                         else
3216                                 data->have_in &= ~(1 << 6);
3217                 }
3218                 data->temp_label = nct6776_temp_label;
3219                 break;
3220         case nct6779:
3221                 /*
3222                  * Shared pins:
3223                  *      VIN4 / AUXTIN0
3224                  *      VIN5 / AUXTIN1
3225                  *      VIN6 / AUXTIN2
3226                  *      VIN7 / AUXTIN3
3227                  * Assume voltage is disabled if the respective temperature is
3228                  * used as temperature source.
3229                  */
3230                 for (i = 0; i < ARRAY_SIZE(NCT6779_REG_TEMP); i++) {
3231                         if (!(data->have_temp & (1 << i)))
3232                                 continue;
3233                         if (data->temp_src[i] == 3)             /* AUXTIN0 */
3234                                 data->have_in &= ~(1 << 6);     /* no VIN4 */
3235                         if (data->temp_src[i] == 4)             /* AUXTIN1 */
3236                                 data->have_in &= ~(1 << 10);    /* no VIN5 */
3237                         if (data->temp_src[i] == 5)             /* AUXTIN2 */
3238                                 data->have_in &= ~(1 << 11);    /* no VIN6 */
3239                         if (data->temp_src[i] == 6)             /* AUXTIN0 */
3240                                 data->have_in &= ~(1 << 14);    /* no VIN7 */
3241                 }
3242                 data->temp_label = nct6779_temp_label;
3243                 break;
3244         }
3245
3246         /* Initialize the chip */
3247         nct6775_init_device(data);
3248
3249         data->vrm = vid_which_vrm();
3250         superio_enter(sio_data->sioreg);
3251         /*
3252          * Read VID value
3253          * We can get the VID input values directly at logical device D 0xe3.
3254          */
3255         superio_select(sio_data->sioreg, NCT6775_LD_VID);
3256         data->vid = superio_inb(sio_data->sioreg, 0xe3);
3257         err = device_create_file(dev, &dev_attr_cpu0_vid);
3258         if (err)
3259                 goto exit_release;
3260
3261         if (fan_debounce) {
3262                 u8 tmp;
3263
3264                 superio_select(sio_data->sioreg, NCT6775_LD_HWM);
3265                 tmp = superio_inb(sio_data->sioreg,
3266                                   NCT6775_REG_CR_FAN_DEBOUNCE);
3267                 switch (data->kind) {
3268                 case nct6775:
3269                         tmp |= 0x1e;
3270                         break;
3271                 case nct6776:
3272                 case nct6779:
3273                         tmp |= 0x3e;
3274                         break;
3275                 }
3276                 superio_outb(sio_data->sioreg, NCT6775_REG_CR_FAN_DEBOUNCE,
3277                              tmp);
3278                 pr_info("Enabled fan debounce for chip %s\n", data->name);
3279         }
3280
3281         superio_exit(sio_data->sioreg);
3282
3283         nct6775_check_fan_inputs(sio_data, data);
3284
3285         /* Read fan clock dividers immediately */
3286         nct6775_update_fan_div_common(dev, data);
3287
3288         /* Register sysfs hooks */
3289         for (i = 0; i < data->pwm_num; i++) {
3290                 if (!(data->has_pwm & (1 << i)))
3291                         continue;
3292
3293                 err = sysfs_create_group(&dev->kobj, &nct6775_group_pwm[i]);
3294                 if (err)
3295                         goto exit_remove;
3296
3297                 if (data->REG_WEIGHT_DUTY[1]) {
3298                         err = device_create_file(dev,
3299                                         &sda_weight_duty_base[i].dev_attr);
3300                         if (err)
3301                                 goto exit_remove;
3302                 }
3303         }
3304         for (i = 0; i < ARRAY_SIZE(sda_auto_pwm_arrays); i++) {
3305                 struct sensor_device_attribute_2 *attr =
3306                         &sda_auto_pwm_arrays[i];
3307
3308                 if (!(data->has_pwm & (1 << attr->nr)))
3309                         continue;
3310                 if (attr->index > data->auto_pwm_num)
3311                         continue;
3312                 err = device_create_file(dev, &attr->dev_attr);
3313                 if (err)
3314                         goto exit_remove;
3315         }
3316
3317         for (i = 0; i < data->in_num; i++) {
3318                 if (!(data->have_in & (1 << i)))
3319                         continue;
3320                 err = sysfs_create_group(&dev->kobj, &nct6775_group_in[i]);
3321                 if (err)
3322                         goto exit_remove;
3323         }
3324
3325         for (i = 0; i < 5; i++) {
3326                 if (data->has_fan & (1 << i)) {
3327                         err = device_create_file(dev,
3328                                                  &sda_fan_input[i].dev_attr);
3329                         if (err)
3330                                 goto exit_remove;
3331                         err = device_create_file(dev,
3332                                                  &sda_fan_alarm[i].dev_attr);
3333                         if (err)
3334                                 goto exit_remove;
3335                         if (data->kind != nct6776 &&
3336                             data->kind != nct6779) {
3337                                 err = device_create_file(dev,
3338                                                 &sda_fan_div[i].dev_attr);
3339                                 if (err)
3340                                         goto exit_remove;
3341                         }
3342                         if (data->has_fan_min & (1 << i)) {
3343                                 err = device_create_file(dev,
3344                                                 &sda_fan_min[i].dev_attr);
3345                                 if (err)
3346                                         goto exit_remove;
3347                         }
3348                 }
3349         }
3350
3351         for (i = 0; i < NUM_REG_TEMP; i++) {
3352                 if (!(data->have_temp & (1 << i)))
3353                         continue;
3354                 err = device_create_file(dev, &sda_temp_input[i].dev_attr);
3355                 if (err)
3356                         goto exit_remove;
3357                 if (data->temp_label) {
3358                         err = device_create_file(dev,
3359                                                  &sda_temp_label[i].dev_attr);
3360                         if (err)
3361                                 goto exit_remove;
3362                 }
3363                 if (data->reg_temp[1][i]) {
3364                         err = device_create_file(dev,
3365                                                  &sda_temp_max[i].dev_attr);
3366                         if (err)
3367                                 goto exit_remove;
3368                 }
3369                 if (data->reg_temp[2][i]) {
3370                         err = device_create_file(dev,
3371                                         &sda_temp_max_hyst[i].dev_attr);
3372                         if (err)
3373                                 goto exit_remove;
3374                 }
3375                 if (i > data->num_temp_fixed)
3376                         continue;
3377                 err = device_create_file(dev, &sda_temp_type[i].dev_attr);
3378                 if (err)
3379                         goto exit_remove;
3380                 err = device_create_file(dev,
3381                                          &sda_temp_offset[i].dev_attr);
3382                 if (err)
3383                         goto exit_remove;
3384                 if (i > 2)
3385                         continue;
3386                 err = device_create_file(dev, &sda_temp_alarm[i].dev_attr);
3387                 if (err)
3388                         goto exit_remove;
3389         }
3390
3391         for (i = 0; i < ARRAY_SIZE(sda_caseopen); i++) {
3392                 if (!data->CASEOPEN_MASK[i])
3393                         continue;
3394                 err = device_create_file(dev, &sda_caseopen[i].dev_attr);
3395                 if (err)
3396                         goto exit_remove;
3397         }
3398
3399         err = device_create_file(dev, &dev_attr_name);
3400         if (err)
3401                 goto exit_remove;
3402
3403         data->hwmon_dev = hwmon_device_register(dev);
3404         if (IS_ERR(data->hwmon_dev)) {
3405                 err = PTR_ERR(data->hwmon_dev);
3406                 goto exit_remove;
3407         }
3408
3409         return 0;
3410
3411 exit_remove:
3412         nct6775_device_remove_files(dev);
3413 exit_release:
3414         release_region(res->start, IOREGION_LENGTH);
3415 exit:
3416         return err;
3417 }
3418
3419 static int __devexit nct6775_remove(struct platform_device *pdev)
3420 {
3421         struct nct6775_data *data = platform_get_drvdata(pdev);
3422
3423         hwmon_device_unregister(data->hwmon_dev);
3424         nct6775_device_remove_files(&pdev->dev);
3425         release_region(data->addr, IOREGION_LENGTH);
3426
3427         return 0;
3428 }
3429
3430 static struct platform_driver nct6775_driver = {
3431         .driver = {
3432                 .owner  = THIS_MODULE,
3433                 .name   = DRVNAME,
3434         },
3435         .probe          = nct6775_probe,
3436         .remove         = __devexit_p(nct6775_remove),
3437 };
3438
3439 /* nct6775_find() looks for a '627 in the Super-I/O config space */
3440 static int __init nct6775_find(int sioaddr, unsigned short *addr,
3441                                struct nct6775_sio_data *sio_data)
3442 {
3443         static const char __initdata sio_name_NCT6775[] = "NCT6775F";
3444         static const char __initdata sio_name_NCT6776[] = "NCT6776F";
3445         static const char __initdata sio_name_NCT6779[] = "NCT6779D";
3446
3447         u16 val;
3448         const char *sio_name;
3449
3450         superio_enter(sioaddr);
3451
3452         if (force_id)
3453                 val = force_id;
3454         else
3455                 val = (superio_inb(sioaddr, SIO_REG_DEVID) << 8)
3456                     | superio_inb(sioaddr, SIO_REG_DEVID + 1);
3457         switch (val & SIO_ID_MASK) {
3458         case SIO_NCT6775_ID:
3459                 sio_data->kind = nct6775;
3460                 sio_name = sio_name_NCT6775;
3461                 break;
3462         case SIO_NCT6776_ID:
3463                 sio_data->kind = nct6776;
3464                 sio_name = sio_name_NCT6776;
3465                 break;
3466         case SIO_NCT6779_ID:
3467                 sio_data->kind = nct6779;
3468                 sio_name = sio_name_NCT6779;
3469                 break;
3470         default:
3471                 if (val != 0xffff)
3472                         pr_debug("unsupported chip ID: 0x%04x\n", val);
3473                 superio_exit(sioaddr);
3474                 return -ENODEV;
3475         }
3476
3477         /* We have a known chip, find the HWM I/O address */
3478         superio_select(sioaddr, NCT6775_LD_HWM);
3479         val = (superio_inb(sioaddr, SIO_REG_ADDR) << 8)
3480             | superio_inb(sioaddr, SIO_REG_ADDR + 1);
3481         *addr = val & IOREGION_ALIGNMENT;
3482         if (*addr == 0) {
3483                 pr_err("Refusing to enable a Super-I/O device with a base I/O port 0\n");
3484                 superio_exit(sioaddr);
3485                 return -ENODEV;
3486         }
3487
3488         /* Activate logical device if needed */
3489         val = superio_inb(sioaddr, SIO_REG_ENABLE);
3490         if (!(val & 0x01)) {
3491                 pr_warn("Forcibly enabling Super-I/O. "
3492                         "Sensor is probably unusable.\n");
3493                 superio_outb(sioaddr, SIO_REG_ENABLE, val | 0x01);
3494         }
3495
3496         superio_exit(sioaddr);
3497         pr_info("Found %s chip at %#x\n", sio_name, *addr);
3498         sio_data->sioreg = sioaddr;
3499
3500         return 0;
3501 }
3502
3503 /*
3504  * when Super-I/O functions move to a separate file, the Super-I/O
3505  * bus will manage the lifetime of the device and this module will only keep
3506  * track of the nct6775 driver. But since we platform_device_alloc(), we
3507  * must keep track of the device
3508  */
3509 static struct platform_device *pdev;
3510
3511 static int __init sensors_nct6775_init(void)
3512 {
3513         int err;
3514         unsigned short address;
3515         struct resource res;
3516         struct nct6775_sio_data sio_data;
3517
3518         /*
3519          * initialize sio_data->kind and sio_data->sioreg.
3520          *
3521          * when Super-I/O functions move to a separate file, the Super-I/O
3522          * driver will probe 0x2e and 0x4e and auto-detect the presence of a
3523          * nct6775 hardware monitor, and call probe()
3524          */
3525         if (nct6775_find(0x2e, &address, &sio_data) &&
3526             nct6775_find(0x4e, &address, &sio_data))
3527                 return -ENODEV;
3528
3529         err = platform_driver_register(&nct6775_driver);
3530         if (err)
3531                 goto exit;
3532
3533         pdev = platform_device_alloc(DRVNAME, address);
3534         if (!pdev) {
3535                 err = -ENOMEM;
3536                 pr_err("Device allocation failed\n");
3537                 goto exit_unregister;
3538         }
3539
3540         err = platform_device_add_data(pdev, &sio_data,
3541                                        sizeof(struct nct6775_sio_data));
3542         if (err) {
3543                 pr_err("Platform data allocation failed\n");
3544                 goto exit_device_put;
3545         }
3546
3547         memset(&res, 0, sizeof(res));
3548         res.name = DRVNAME;
3549         res.start = address + IOREGION_OFFSET;
3550         res.end = address + IOREGION_OFFSET + IOREGION_LENGTH - 1;
3551         res.flags = IORESOURCE_IO;
3552
3553         err = acpi_check_resource_conflict(&res);
3554         if (err)
3555                 goto exit_device_put;
3556
3557         err = platform_device_add_resources(pdev, &res, 1);
3558         if (err) {
3559                 pr_err("Device resource addition failed (%d)\n", err);
3560                 goto exit_device_put;
3561         }
3562
3563         /* platform_device_add calls probe() */
3564         err = platform_device_add(pdev);
3565         if (err) {
3566                 pr_err("Device addition failed (%d)\n", err);
3567                 goto exit_device_put;
3568         }
3569
3570         return 0;
3571
3572 exit_device_put:
3573         platform_device_put(pdev);
3574 exit_unregister:
3575         platform_driver_unregister(&nct6775_driver);
3576 exit:
3577         return err;
3578 }
3579
3580 static void __exit sensors_nct6775_exit(void)
3581 {
3582         platform_device_unregister(pdev);
3583         platform_driver_unregister(&nct6775_driver);
3584 }
3585
3586 MODULE_AUTHOR("Guenter Roeck <linux@roeck-us.net>");
3587 MODULE_DESCRIPTION("NCT6775F/NCT6776F/NCT6779D driver");
3588 MODULE_LICENSE("GPL");
3589
3590 module_init(sensors_nct6775_init);
3591 module_exit(sensors_nct6775_exit);