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