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