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