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