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