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