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