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