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