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