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