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