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