]> git.sur5r.net Git - groeck-nct6775/blob - nct6775.c
Add support for more temperatures
[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       6+3    0xb470 0xc1    0x5ca3
37  * nct6776f     9      5       3       6+3    0xc330 0xc1    0x5ca3
38  * nct6779d    15      5       5       2+5    0xc560 0xc1    0x5ca3
39  */
40
41 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
42
43 #include <linux/module.h>
44 #include <linux/init.h>
45 #include <linux/slab.h>
46 #include <linux/jiffies.h>
47 #include <linux/platform_device.h>
48 #include <linux/hwmon.h>
49 #include <linux/hwmon-sysfs.h>
50 #include <linux/hwmon-vid.h>
51 #include <linux/err.h>
52 #include <linux/mutex.h>
53 #include <linux/acpi.h>
54 #include <linux/io.h>
55 #include "lm75.h"
56
57 enum kinds { nct6775, nct6776, nct6779 };
58
59 /* used to set data->name = nct6775_device_names[data->sio_kind] */
60 static const char * const nct6775_device_names[] = {
61         "nct6775",
62         "nct6776",
63         "nct6779",
64 };
65
66 static unsigned short force_id;
67 module_param(force_id, ushort, 0);
68 MODULE_PARM_DESC(force_id, "Override the detected device ID");
69
70 static unsigned short fan_debounce;
71 module_param(fan_debounce, ushort, 0);
72 MODULE_PARM_DESC(fan_debounce, "Enable debouncing for fan RPM signal");
73
74 #define DRVNAME "nct6775"
75
76 /*
77  * Super-I/O constants and functions
78  */
79
80 #define NCT6775_LD_ACPI         0x0a
81 #define NCT6775_LD_HWM          0x0b
82 #define NCT6775_LD_VID          0x0d
83
84 #define SIO_REG_LDSEL           0x07    /* Logical device select */
85 #define SIO_REG_DEVID           0x20    /* Device ID (2 bytes) */
86 #define SIO_REG_EN_VRM10        0x2C    /* GPIO3, GPIO4 selection */
87 #define SIO_REG_ENABLE          0x30    /* Logical device enable */
88 #define SIO_REG_ADDR            0x60    /* Logical device address (2 bytes) */
89 #define SIO_REG_VID_CTRL        0xF0    /* VID control */
90 #define SIO_REG_VID_DATA        0xF1    /* VID data */
91
92 #define SIO_NCT6775_ID          0xb470
93 #define SIO_NCT6776_ID          0xc330
94 #define SIO_NCT6779_ID          0xc560
95 #define SIO_ID_MASK             0xFFF0
96
97 static inline void
98 superio_outb(int ioreg, int reg, int val)
99 {
100         outb(reg, ioreg);
101         outb(val, ioreg + 1);
102 }
103
104 static inline int
105 superio_inb(int ioreg, int reg)
106 {
107         outb(reg, ioreg);
108         return inb(ioreg + 1);
109 }
110
111 static inline void
112 superio_select(int ioreg, int ld)
113 {
114         outb(SIO_REG_LDSEL, ioreg);
115         outb(ld, ioreg + 1);
116 }
117
118 static inline void
119 superio_enter(int ioreg)
120 {
121         outb(0x87, ioreg);
122         outb(0x87, ioreg);
123 }
124
125 static inline void
126 superio_exit(int ioreg)
127 {
128         outb(0xaa, ioreg);
129         outb(0x02, ioreg);
130         outb(0x02, ioreg + 1);
131 }
132
133 /*
134  * ISA constants
135  */
136
137 #define IOREGION_ALIGNMENT      (~7)
138 #define IOREGION_OFFSET         5
139 #define IOREGION_LENGTH         2
140 #define ADDR_REG_OFFSET         0
141 #define DATA_REG_OFFSET         1
142
143 #define NCT6775_REG_BANK        0x4E
144 #define NCT6775_REG_CONFIG      0x40
145
146 /*
147  * Not currently used:
148  * REG_MAN_ID has the value 0x5ca3 for all supported chips.
149  * REG_CHIP_ID == 0x88/0xa1/0xc1 depending on chip model.
150  * REG_MAN_ID is at port 0x4f
151  * REG_CHIP_ID is at port 0x58
152  */
153
154 static const u16 NCT6775_REG_FAN_MIN[] = { 0x3b, 0x3c, 0x3d };
155
156 /* Voltage min/max registers for nr=7..14 are in bank 5 */
157
158 static const u16 NCT6775_REG_IN_MAX[] = {
159         0x2b, 0x2d, 0x2f, 0x31, 0x33, 0x35, 0x37, 0x554, 0x556 };
160 static const u16 NCT6775_REG_IN_MIN[] = {
161         0x2c, 0x2e, 0x30, 0x32, 0x34, 0x36, 0x38, 0x555, 0x557 };
162 static const u16 NCT6775_REG_IN[] = {
163         0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x550, 0x551, 0x552
164 };
165
166 static const u16 NCT6779_REG_IN[] = {
167         0x480, 0x481, 0x482, 0x483, 0x484, 0x485, 0x486, 0x487,
168         0x488, 0x489, 0x48a, 0x48b, 0x48c, 0x48d, 0x48e
169 };
170
171 #define NCT6775_REG_VBAT                0x5D
172 #define NCT6775_REG_DIODE               0x5E
173
174 #define NCT6775_REG_FANDIV1             0x506
175 #define NCT6775_REG_FANDIV2             0x507
176
177 #define NCT6775_REG_CR_FAN_DEBOUNCE     0xf0
178
179 static const u16 NCT6775_REG_ALARM[6] = { 0x459, 0x45A, 0x45B };
180 static const u16 NCT6779_REG_ALARM[6] = { 0x459, 0x45A, 0x45B, 0x568 };
181
182 #define NCT6775_REG_CASEOPEN            0x42
183
184 static const u8 NCT6775_CASEOPEN_MASK[] = { 0x10, 0x00 };
185 static const u8 NCT6776_CASEOPEN_MASK[] = { 0x10, 0x40 };
186
187 static const u8 NCT6775_REG_CR_CASEOPEN_CLR[] = { 0xe6, 0xee };
188 static const u8 NCT6775_CR_CASEOPEN_CLR_MASK[] = { 0x20, 0x01 };
189
190 /* DC or PWM output fan configuration */
191 static const u8 NCT6775_REG_PWM_MODE[] = { 0x04, 0x04, 0x12 };
192 static const u8 NCT6775_PWM_MODE_MASK[] = { 0x01, 0x02, 0x01 };
193
194 static const u8 NCT6776_REG_PWM_MODE[] = { 0x04, 0, 0 };
195 static const u8 NCT6776_PWM_MODE_MASK[] = { 0x01, 0, 0 };
196
197 /* Advanced Fan control, some values are common for all fans */
198
199 static const u16 NCT6775_REG_TARGET[] = { 0x101, 0x201, 0x301, 0x801, 0x901 };
200 static const u16 NCT6775_REG_FAN_MODE[] = { 0x102, 0x202, 0x302, 0x802, 0x902 };
201 static const u16 NCT6775_REG_FAN_STEP_DOWN_TIME[] = {
202         0x103, 0x203, 0x303, 0x803, 0x903 };
203 static const u16 NCT6775_REG_FAN_STEP_UP_TIME[] = {
204         0x104, 0x204, 0x304, 0x804, 0x904 };
205 static const u16 NCT6775_REG_FAN_STOP_OUTPUT[] = {
206         0x105, 0x205, 0x305, 0x805, 0x905 };
207 static const u16 NCT6775_REG_FAN_START_OUTPUT[]
208         = { 0x106, 0x206, 0x306, 0x806, 0x906 };
209 static const u16 NCT6775_REG_FAN_MAX_OUTPUT[] = { 0x10a, 0x20a, 0x30a };
210 static const u16 NCT6775_REG_FAN_STEP_OUTPUT[] = { 0x10b, 0x20b, 0x30b };
211
212 static const u16 NCT6775_REG_FAN_STOP_TIME[]
213         = { 0x107, 0x207, 0x307, 0x807, 0x907 };
214 static const u16 NCT6775_REG_PWM[] = { 0x109, 0x209, 0x309, 0x809, 0x909 };
215 static const u16 NCT6775_REG_PWM_READ[] = { 0x01, 0x03, 0x11, 0x13, 0x15 };
216
217 static const u16 NCT6775_REG_FAN[] = { 0x630, 0x632, 0x634, 0x636, 0x638 };
218
219 static const u16 NCT6776_REG_FAN_MIN[] = { 0x63a, 0x63c, 0x63e, 0x640, 0x642};
220
221 static const u16 NCT6779_REG_TOLERANCE_H[]
222         = { 0x10c, 0x20c, 0x30c, 0x40c, 0x50c };
223
224 static const u16 NCT6779_REG_FAN[] = { 0x4c0, 0x4c2, 0x4c4, 0x4c6, 0x4c8 };
225
226 static const u16 NCT6775_REG_TEMP[]
227         = { 0x27, 0x150, 0x250, 0x62b, 0x62c, 0x62d };
228 static const u16 NCT6779_REG_TEMP[] = { 0x27, 0x150 };
229
230 static const u16 NCT6775_REG_TEMP_CONFIG[]
231         = { 0, 0x152, 0x252, 0x628, 0x629, 0x62A };
232 static const u16 NCT6775_REG_TEMP_HYST[]
233         = { 0x3a, 0x153, 0x253, 0x673, 0x678, 0x67D };
234 static const u16 NCT6775_REG_TEMP_OVER[]
235         = { 0x39, 0x155, 0x255, 0x672, 0x677, 0x67C };
236
237 static const u16 NCT6775_REG_TEMP_SOURCE[]
238         = { 0x621, 0x622, 0x623, 0x624, 0x625, 0x626 };
239
240 static const u16 NCT6775_REG_TEMP_SEL[]
241         = { 0x100, 0x200, 0x300, 0x800, 0x900 };
242
243 static const u16 NCT6775_REG_WEIGHT_TEMP_SEL[]
244         = { 0x139, 0x239, 0x339, 0x839, 0x939 };
245 static const u16 NCT6775_REG_WEIGHT_TEMP_STEP[]
246         = { 0x13a, 0x23a, 0x33a, 0x83a, 0x93a };
247 static const u16 NCT6775_REG_WEIGHT_TEMP_STEP_TOL[]
248         = { 0x13b, 0x23b, 0x33b, 0x83b, 0x93b };
249 static const u16 NCT6775_REG_WEIGHT_DUTY_STEP[]
250         = { 0x13c, 0x23c, 0x33c, 0x83c, 0x93c };
251 static const u16 NCT6775_REG_WEIGHT_TEMP_BASE[]
252         = { 0x13d, 0x23d, 0x33d, 0x83d, 0x93d };
253
254 static const u16 NCT6776_REG_WEIGHT_DUTY_BASE[]
255         = { 0x13e, 0x23e, 0x33e, 0x83e, 0x93e };
256
257 static const u16 NCT6775_REG_TEMP_OFFSET[] = { 0x454, 0x455, 0x456 };
258
259 static const u16 NCT6779_REG_TEMP_OFFSET[]
260         = { 0x454, 0x455, 0x456, 0x44a, 0x44b, 0x44c };
261
262 static const u16 NCT6776_REG_TEMP_CONFIG[11]
263         = { 0x18, 0x152, 0x252, 0x628, 0x629, 0x62A };
264
265 static const u16 NCT6775_REG_TEMP_MON[] = { 0x73, 0x75, 0x77, 0x79, 0x7b };
266
267 static const u16 NCT6775_REG_AUTO_TEMP[]
268         = { 0x121, 0x221, 0x321, 0x821, 0x921 };
269 static const u16 NCT6775_REG_AUTO_PWM[]
270         = { 0x127, 0x227, 0x327, 0x827, 0x927 };
271
272 #define NCT6775_AUTO_TEMP(data, nr, p)  ((data)->REG_AUTO_TEMP[nr] + (p))
273 #define NCT6775_AUTO_PWM(data, nr, p)   ((data)->REG_AUTO_PWM[nr] + (p))
274
275 static const u16 NCT6775_REG_CRITICAL_ENAB[] = { 0x134, 0x234, 0x334 };
276
277 static const u16 NCT6775_REG_CRITICAL_TEMP[] = {
278         0x135, 0x235, 0x335, 0x835, 0x935 };
279 static const u16 NCT6775_REG_CRITICAL_TEMP_TOLERANCE[] = {
280         0x138, 0x238, 0x338, 0x838, 0x938 };
281
282 static const u16 NCT6779_REG_CRITICAL_PWM_ENABLE[] = {
283         0x136, 0x236, 0x336, 0x836, 0x936 };
284 static const u16 NCT6779_REG_CRITICAL_PWM[] = {
285         0x137, 0x237, 0x337, 0x837, 0x937 };
286
287 static const char *const nct6775_temp_label[] = {
288         "",
289         "SYSTIN",
290         "CPUTIN",
291         "AUXTIN",
292         "AMD SB-TSI",
293         "PECI Agent 0",
294         "PECI Agent 1",
295         "PECI Agent 2",
296         "PECI Agent 3",
297         "PECI Agent 4",
298         "PECI Agent 5",
299         "PECI Agent 6",
300         "PECI Agent 7",
301         "PCH_CHIP_CPU_MAX_TEMP",
302         "PCH_CHIP_TEMP",
303         "PCH_CPU_TEMP",
304         "PCH_MCH_TEMP",
305         "PCH_DIM0_TEMP",
306         "PCH_DIM1_TEMP",
307         "PCH_DIM2_TEMP",
308         "PCH_DIM3_TEMP"
309 };
310
311 static const char *const nct6776_temp_label[] = {
312         "",
313         "SYSTIN",
314         "CPUTIN",
315         "AUXTIN",
316         "SMBUSMASTER 0",
317         "SMBUSMASTER 1",
318         "SMBUSMASTER 2",
319         "SMBUSMASTER 3",
320         "SMBUSMASTER 4",
321         "SMBUSMASTER 5",
322         "SMBUSMASTER 6",
323         "SMBUSMASTER 7",
324         "PECI Agent 0",
325         "PECI Agent 1",
326         "PCH_CHIP_CPU_MAX_TEMP",
327         "PCH_CHIP_TEMP",
328         "PCH_CPU_TEMP",
329         "PCH_MCH_TEMP",
330         "PCH_DIM0_TEMP",
331         "PCH_DIM1_TEMP",
332         "PCH_DIM2_TEMP",
333         "PCH_DIM3_TEMP",
334         "BYTE_TEMP"
335 };
336
337 static const char *const nct6779_temp_label[] = {
338         "",
339         "SYSTIN",
340         "CPUTIN",
341         "AUXTIN0",
342         "AUXTIN1",
343         "AUXTIN2",
344         "AUXTIN3",
345         "",
346         "SMBUSMASTER 0",
347         "SMBUSMASTER 1",
348         "SMBUSMASTER 2",
349         "SMBUSMASTER 3",
350         "SMBUSMASTER 4",
351         "SMBUSMASTER 5",
352         "SMBUSMASTER 6",
353         "SMBUSMASTER 7",
354         "PECI Agent 0",
355         "PECI Agent 1",
356         "PCH_CHIP_CPU_MAX_TEMP",
357         "PCH_CHIP_TEMP",
358         "PCH_CPU_TEMP",
359         "PCH_MCH_TEMP",
360         "PCH_DIM0_TEMP",
361         "PCH_DIM1_TEMP",
362         "PCH_DIM2_TEMP",
363         "PCH_DIM3_TEMP",
364         "BYTE_TEMP"
365 };
366
367 static const u16 NCT6775_REG_TEMP_ALTERNATE[ARRAY_SIZE(nct6779_temp_label)]
368         = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0x661, 0x662, 0x664 };
369
370 static const u16 NCT6776_REG_TEMP_ALTERNATE[ARRAY_SIZE(nct6776_temp_label)]
371         = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0x401, 0x402, 0x404 };
372
373 static const u16 NCT6779_REG_TEMP_ALTERNATE[ARRAY_SIZE(nct6779_temp_label)]
374         = { 0x490, 0x491, 0x492, 0x493, 0x494, 0x495, 0, 0,
375             0, 0, 0, 0, 0, 0, 0, 0,
376             0, 0, 0x401, 0x402, 0x404 };
377
378 #define NUM_TEMP        10      /* Max number of temp attribute sets w/ limits*/
379 #define NUM_TEMP_FIXED  6       /* Max number of fixed temp attribute sets */
380
381 #define NUM_REG_TEMP    ARRAY_SIZE(NCT6775_REG_TEMP)
382
383 static inline int reg_to_pwm_enable(int pwm, int mode)
384 {
385         if (mode == 0 && pwm == 255)
386                 return 0;       /* off  */
387         return mode + 1;
388 }
389
390 static inline int pwm_enable_to_reg(int mode)
391 {
392         if (mode == 0)
393                 return 0;
394         return mode - 1;
395 }
396
397 /*
398  * Conversions
399  */
400
401 /* 1 is DC mode, output in ms */
402 static inline unsigned int step_time_from_reg(u8 reg, u8 mode)
403 {
404         return mode ? 400 * reg : 100 * reg;
405 }
406
407 static inline u8 step_time_to_reg(unsigned int msec, u8 mode)
408 {
409         return SENSORS_LIMIT((mode ? (msec + 200) / 400 :
410                                         (msec + 50) / 100), 1, 255);
411 }
412
413 static unsigned int fan_from_reg8(u16 reg, unsigned int divreg)
414 {
415         if (reg == 0 || reg == 255)
416                 return 0;
417         return 1350000U / (reg << divreg);
418 }
419
420 static unsigned int fan_from_reg13(u16 reg, unsigned int divreg)
421 {
422         if ((reg & 0xff1f) == 0xff1f)
423                 return 0;
424
425         reg = (reg & 0x1f) | ((reg & 0xff00) >> 3);
426
427         if (reg == 0)
428                 return 0;
429
430         return 1350000U / reg;
431 }
432
433 static unsigned int fan_from_reg16(u16 reg, unsigned int divreg)
434 {
435         if (reg == 0 || reg == 0xffff)
436                 return 0;
437
438         /*
439          * Even though the registers are 16 bit wide, the fan divisor
440          * still applies.
441          */
442         return 1350000U / (reg << divreg);
443 }
444
445 static inline unsigned int
446 div_from_reg(u8 reg)
447 {
448         return 1 << reg;
449 }
450
451 /*
452  * Some of the voltage inputs have internal scaling, the tables below
453  * contain 8 (the ADC LSB in mV) * scaling factor * 100
454  */
455 static const u16 scale_in[15] = {
456         800, 800, 1600, 1600, 800, 800, 800, 1600, 1600, 800, 800, 800, 800,
457         800, 800
458 };
459
460 static inline long in_from_reg(u8 reg, u8 nr)
461 {
462         return DIV_ROUND_CLOSEST(reg * scale_in[nr], 100);
463 }
464
465 static inline u8 in_to_reg(u32 val, u8 nr)
466 {
467         return SENSORS_LIMIT(DIV_ROUND_CLOSEST(val * 100, scale_in[nr]), 0,
468                              255);
469 }
470
471 /*
472  * Data structures and manipulation thereof
473  */
474
475 struct nct6775_data {
476         int addr;       /* IO base of hw monitor block */
477         enum kinds kind;
478         const char *name;
479
480         struct device *hwmon_dev;
481         struct mutex lock;
482
483         u16 reg_temp[3][NUM_TEMP]; /* 0=temp, 1=temp_over, 2=temp_hyst */
484         u8 temp_src[NUM_TEMP];
485         u16 reg_temp_config[NUM_TEMP];
486         const char * const *temp_label;
487         int temp_label_num;
488
489         u16 REG_CONFIG;
490         u16 REG_VBAT;
491         u16 REG_DIODE;
492
493         const u16 *REG_VIN;
494         const u16 *REG_IN_MINMAX[2];
495
496         const u16 *REG_TARGET;
497         const u16 *REG_FAN;
498         const u16 *REG_FAN_MODE;
499         const u16 *REG_FAN_MIN;
500         const u16 *REG_FAN_TIME[3];
501
502         const u8 *REG_PWM_MODE;
503         const u8 *PWM_MODE_MASK;
504
505         const u16 *REG_PWM[7];  /* [0]=pwm, [1]=start_output, [2]=stop_output,
506                                  * [3]=max_output, [4]=step_output,
507                                  * [5]=weight_duty_step, [6]=weight_duty_base
508                                  */
509         const u16 *REG_PWM_READ;
510
511         const u16 *REG_TEMP_ALTERNATE;
512         const u16 *REG_TEMP_MON;
513         const u16 *REG_AUTO_TEMP;
514         const u16 *REG_AUTO_PWM;
515
516         const u16 *REG_CRITICAL_TEMP;
517         const u16 *REG_CRITICAL_TEMP_TOLERANCE;
518
519         const u16 *REG_TEMP_SOURCE;     /* temp register sources */
520         const u16 *REG_TEMP_SEL[2];     /* pwm temp, 0=base, 1=weight */
521
522         const u16 *REG_WEIGHT_TEMP[3];  /* 0=base, 1=hyst, 2=step */
523
524         const u16 *REG_TEMP_OFFSET;
525
526         const u16 *REG_ALARM;
527
528         u8 REG_CASEOPEN;
529         const u8 *CASEOPEN_MASK;
530
531         unsigned int (*fan_from_reg)(u16 reg, unsigned int divreg);
532         unsigned int (*fan_from_reg_min)(u16 reg, unsigned int divreg);
533
534         struct mutex update_lock;
535         char valid;             /* !=0 if following fields are valid */
536         unsigned long last_updated;     /* In jiffies */
537
538         /* Register values */
539         u8 bank;                /* current register bank */
540         u8 in_num;              /* number of in inputs we have */
541         u8 in[15][3];           /* [0]=in, [1]=in_max, [2]=in_min */
542         unsigned int rpm[5];
543         u16 fan_min[5];
544         u8 fan_div[5];
545         u8 has_pwm;
546         u8 has_fan;             /* some fan inputs can be disabled */
547         u8 has_fan_min;         /* some fans don't have min register */
548         bool has_fan_div;
549
550         u8 temp_fixed_num;      /* 3 or 6 */
551         u8 temp_type[NUM_TEMP_FIXED];
552         s8 temp_offset[NUM_TEMP_FIXED];
553         s16 temp[3][NUM_TEMP]; /* 0=temp, 1=temp_over, 2=temp_hyst */
554         u64 alarms;
555         u8 caseopen;
556
557         u8 pwm_num;     /* number of pwm */
558
559         u8 pwm_mode[5]; /* 1->DC variable voltage, 0->PWM variable duty cycle */
560         u8 pwm_enable[5]; /* 0->off
561                            * 1->manual
562                            * 2->thermal cruise mode (also called SmartFan I)
563                            * 3->fan speed cruise mode
564                            * 4->SmartFan III
565                            * 5->enhanced variable thermal cruise (SmartFan IV)
566                            */
567         u8 pwm[7][5];   /* [0]=pwm, [1]=start_output, [2]=stop_output,
568                          * [3]=max_output, [4]=step_output,
569                          * [5]=weight_duty_step, [6]=weight_duty_base
570                          */
571         u8 target_temp[5];
572         s16 pwm_temp[5];
573         u8 tolerance[5][2];
574
575         u8 fan_time[3][5]; /* 0 = stop_time, 1 = step_up, 2 = step_down */
576
577         /* Automatic fan speed control registers */
578         int auto_pwm_num;
579         u8 auto_pwm[5][7];
580         u8 auto_temp[5][7];
581
582         u8 pwm_temp_sel[2][5];
583
584         bool pwm_sel_enable[2][5];/* 0->stop_val, 1->weight;
585                                    * false->off, true->on
586                                    */
587         u8 weight_temp[3][5];   /* 0->temp_step, 1->temp_step_tol,
588                                  * 2->temp_base
589                                  */
590
591         u8 vid;
592         u8 vrm;
593
594         u16 have_temp;
595         u16 have_temp_fixed;
596         u16 have_in;
597 };
598
599 struct nct6775_sio_data {
600         int sioreg;
601         enum kinds kind;
602 };
603
604 static bool is_word_sized(struct nct6775_data *data, u16 reg)
605 {
606         switch (data->kind) {
607         case nct6775:
608                 return (((reg & 0xff00) == 0x100 ||
609                     (reg & 0xff00) == 0x200) &&
610                    ((reg & 0x00ff) == 0x50 ||
611                     (reg & 0x00ff) == 0x53 ||
612                     (reg & 0x00ff) == 0x55)) ||
613                   (reg & 0xfff0) == 0x630 ||
614                   reg == 0x640 || reg == 0x642 ||
615                   reg == 0x662 ||
616                   ((reg & 0xfff0) == 0x650 && (reg & 0x000f) >= 0x06) ||
617                   reg == 0x73 || reg == 0x75 || reg == 0x77;
618         case nct6776:
619                 return (((reg & 0xff00) == 0x100 ||
620                     (reg & 0xff00) == 0x200) &&
621                    ((reg & 0x00ff) == 0x50 ||
622                     (reg & 0x00ff) == 0x53 ||
623                     (reg & 0x00ff) == 0x55)) ||
624                   (reg & 0xfff0) == 0x630 ||
625                   reg == 0x402 ||
626                   reg == 0x640 || reg == 0x642 ||
627                   ((reg & 0xfff0) == 0x650 && (reg & 0x000f) >= 0x06) ||
628                   reg == 0x73 || reg == 0x75 || reg == 0x77;
629         case nct6779:
630                 return reg == 0x150 || reg == 0x153 || reg == 0x155 ||
631                   ((reg & 0xfff0) == 0x4c0 && (reg & 0x000f) < 0x09) ||
632                   reg == 0x402 ||
633                   reg == 0x63a || reg == 0x63c || reg == 0x63e ||
634                   reg == 0x640 || reg == 0x642 ||
635                   reg == 0x73 || reg == 0x75 || reg == 0x77 || reg == 0x79 ||
636                   reg == 0x7b;
637         }
638         return false;
639 }
640
641 /*
642  * On older chips, only registers 0x50-0x5f are banked.
643  * On more recent chips, all registers are banked.
644  * Assume that is the case and set the bank number for each access.
645  * Cache the bank number so it only needs to be set if it changes.
646  */
647 static inline void nct6775_set_bank(struct nct6775_data *data, u16 reg)
648 {
649         u8 bank = reg >> 8;
650         if (data->bank != bank) {
651                 outb_p(NCT6775_REG_BANK, data->addr + ADDR_REG_OFFSET);
652                 outb_p(bank, data->addr + DATA_REG_OFFSET);
653                 data->bank = bank;
654         }
655 }
656
657 static u16 nct6775_read_value(struct nct6775_data *data, u16 reg)
658 {
659         int res, word_sized = is_word_sized(data, reg);
660
661         mutex_lock(&data->lock);
662
663         nct6775_set_bank(data, reg);
664         outb_p(reg & 0xff, data->addr + ADDR_REG_OFFSET);
665         res = inb_p(data->addr + DATA_REG_OFFSET);
666         if (word_sized) {
667                 outb_p((reg & 0xff) + 1,
668                        data->addr + ADDR_REG_OFFSET);
669                 res = (res << 8) + inb_p(data->addr + DATA_REG_OFFSET);
670         }
671
672         mutex_unlock(&data->lock);
673         return res;
674 }
675
676 static int nct6775_write_value(struct nct6775_data *data, u16 reg, u16 value)
677 {
678         int word_sized = is_word_sized(data, reg);
679
680         mutex_lock(&data->lock);
681
682         nct6775_set_bank(data, reg);
683         outb_p(reg & 0xff, data->addr + ADDR_REG_OFFSET);
684         if (word_sized) {
685                 outb_p(value >> 8, data->addr + DATA_REG_OFFSET);
686                 outb_p((reg & 0xff) + 1,
687                        data->addr + ADDR_REG_OFFSET);
688         }
689         outb_p(value & 0xff, data->addr + DATA_REG_OFFSET);
690
691         mutex_unlock(&data->lock);
692         return 0;
693 }
694
695 /* We left-align 8-bit temperature values to make the code simpler */
696 static u16 nct6775_read_temp(struct nct6775_data *data, u16 reg)
697 {
698         u16 res;
699
700         res = nct6775_read_value(data, reg);
701         if (!is_word_sized(data, reg))
702                 res <<= 8;
703
704         return res;
705 }
706
707 static int nct6775_write_temp(struct nct6775_data *data, u16 reg, u16 value)
708 {
709         if (!is_word_sized(data, reg))
710                 value >>= 8;
711         return nct6775_write_value(data, reg, value);
712 }
713
714 /* This function assumes that the caller holds data->update_lock */
715 static void nct6775_write_fan_div(struct nct6775_data *data, int nr)
716 {
717         u8 reg;
718
719         switch (nr) {
720         case 0:
721                 reg = (nct6775_read_value(data, NCT6775_REG_FANDIV1) & 0x70)
722                     | (data->fan_div[0] & 0x7);
723                 nct6775_write_value(data, NCT6775_REG_FANDIV1, reg);
724                 break;
725         case 1:
726                 reg = (nct6775_read_value(data, NCT6775_REG_FANDIV1) & 0x7)
727                     | ((data->fan_div[1] << 4) & 0x70);
728                 nct6775_write_value(data, NCT6775_REG_FANDIV1, reg);
729         case 2:
730                 reg = (nct6775_read_value(data, NCT6775_REG_FANDIV2) & 0x70)
731                     | (data->fan_div[2] & 0x7);
732                 nct6775_write_value(data, NCT6775_REG_FANDIV2, reg);
733                 break;
734         case 3:
735                 reg = (nct6775_read_value(data, NCT6775_REG_FANDIV2) & 0x7)
736                     | ((data->fan_div[3] << 4) & 0x70);
737                 nct6775_write_value(data, NCT6775_REG_FANDIV2, reg);
738                 break;
739         }
740 }
741
742 static void nct6775_write_fan_div_common(struct device *dev,
743                                          struct nct6775_data *data, int nr)
744 {
745         if (data->kind == nct6775)
746                 nct6775_write_fan_div(data, nr);
747 }
748
749 static void nct6775_update_fan_div(struct nct6775_data *data)
750 {
751         u8 i;
752
753         i = nct6775_read_value(data, NCT6775_REG_FANDIV1);
754         data->fan_div[0] = i & 0x7;
755         data->fan_div[1] = (i & 0x70) >> 4;
756         i = nct6775_read_value(data, NCT6775_REG_FANDIV2);
757         data->fan_div[2] = i & 0x7;
758         if (data->has_fan & (1<<3))
759                 data->fan_div[3] = (i & 0x70) >> 4;
760 }
761
762 static void nct6775_update_fan_div_common(struct device *dev,
763                                           struct nct6775_data *data)
764 {
765         if (data->kind == nct6775)
766                 nct6775_update_fan_div(data);
767 }
768
769 static void nct6775_update_pwm(struct device *dev)
770 {
771         struct nct6775_data *data = dev_get_drvdata(dev);
772         int i, j;
773         int fanmodecfg, reg;
774         bool duty_is_dc;
775
776         for (i = 0; i < data->pwm_num; i++) {
777                 if (!(data->has_pwm & (1 << i)))
778                         continue;
779
780                 duty_is_dc = data->REG_PWM_MODE[i] &&
781                   (nct6775_read_value(data, data->REG_PWM_MODE[i])
782                    & data->PWM_MODE_MASK[i]);
783                 data->pwm_mode[i] = duty_is_dc;
784
785                 fanmodecfg = nct6775_read_value(data, data->REG_FAN_MODE[i]);
786                 for (j = 0; j < ARRAY_SIZE(data->REG_PWM); j++) {
787                         if (data->REG_PWM[j] && data->REG_PWM[j][i]) {
788                                 data->pwm[j][i]
789                                   = nct6775_read_value(data,
790                                                        data->REG_PWM[j][i]);
791                         }
792                 }
793
794                 data->pwm_enable[i] = reg_to_pwm_enable(data->pwm[0][i],
795                                                         (fanmodecfg >> 4) & 7);
796
797                 data->tolerance[i][0] = fanmodecfg & 0x0f;
798                 if (data->kind == nct6779) {
799                         reg = nct6775_read_value(data,
800                                                  NCT6779_REG_TOLERANCE_H[i]);
801                         data->tolerance[i][0] |= (reg & 0x70) >> 1;
802                 }
803                 data->tolerance[i][1] =
804                         nct6775_read_value(data,
805                                         data->REG_CRITICAL_TEMP_TOLERANCE[i]);
806
807                 data->pwm_sel_enable[0][i] =
808                         nct6775_read_value(data, data->REG_TEMP_SEL[0][i])
809                                 & 0x80;
810                 data->pwm_sel_enable[1][i] =
811                         nct6775_read_value(data, data->REG_TEMP_SEL[1][i])
812                                 & 0x80;
813
814                 /* Weight data */
815                 for (j = 0; j < 2; j++) {
816                         reg = nct6775_read_value(data,
817                                                  data->REG_TEMP_SEL[j][i]);
818                         data->pwm_temp_sel[j][i] = reg & 0x1f;
819                 }
820                 /* Weight temp data */
821                 for (j = 0; j < 3; j++) {
822                         data->weight_temp[j][i]
823                           = nct6775_read_value(data,
824                                                data->REG_WEIGHT_TEMP[j][i]);
825                 }
826         }
827 }
828
829 static void nct6775_update_pwm_limits(struct device *dev)
830 {
831         struct nct6775_data *data = dev_get_drvdata(dev);
832         int i, j;
833         u8 reg;
834
835         for (i = 0; i < data->pwm_num; i++) {
836                 if (!(data->has_pwm & (1 << i)))
837                         continue;
838
839                 for (j = 0; j < 3; j++) {
840                         data->fan_time[j][i] =
841                           nct6775_read_value(data, data->REG_FAN_TIME[j][i]);
842                 }
843
844                 data->target_temp[i] =
845                         nct6775_read_value(data, data->REG_TARGET[i]) &
846                                         (data->pwm_mode[i] ? 0xff : 0x7f);
847                 data->pwm_temp[i] =
848                         nct6775_read_value(data, data->REG_TEMP_MON[i]);
849
850                 for (j = 0; j < data->auto_pwm_num; j++) {
851                         data->auto_pwm[i][j] =
852                           nct6775_read_value(data,
853                                              NCT6775_AUTO_PWM(data, i, j));
854                         data->auto_temp[i][j] =
855                           nct6775_read_value(data,
856                                              NCT6775_AUTO_TEMP(data, i, j));
857                 }
858
859                 /* critical auto_pwm temperature data */
860                 data->auto_temp[i][data->auto_pwm_num] =
861                         nct6775_read_value(data, data->REG_CRITICAL_TEMP[i]);
862
863                 switch (data->kind) {
864                 case nct6775:
865                         reg = nct6775_read_value(data,
866                                                  NCT6775_REG_CRITICAL_ENAB[i]);
867                         data->auto_pwm[i][data->auto_pwm_num] =
868                                                 (reg & 0x02) ? 0xff : 0x00;
869                         break;
870                 case nct6776:
871                         data->auto_pwm[i][data->auto_pwm_num] = 0xff;
872                         break;
873                 case nct6779:
874                         reg = nct6775_read_value(data,
875                                         NCT6779_REG_CRITICAL_PWM_ENABLE[i]);
876                         if (reg & 1)
877                                 data->auto_pwm[i][data->auto_pwm_num] =
878                                   nct6775_read_value(data,
879                                         NCT6779_REG_CRITICAL_PWM[i]);
880                         else
881                                 data->auto_pwm[i][data->auto_pwm_num] = 0xff;
882                         break;
883                 }
884         }
885 }
886
887 static struct nct6775_data *nct6775_update_device(struct device *dev)
888 {
889         struct nct6775_data *data = dev_get_drvdata(dev);
890         int i, j;
891
892         mutex_lock(&data->update_lock);
893
894         if (time_after(jiffies, data->last_updated + HZ + HZ/2)
895          || !data->valid) {
896                 /* Fan clock dividers */
897                 nct6775_update_fan_div_common(dev, data);
898
899                 /* Measured voltages and limits */
900                 for (i = 0; i < data->in_num; i++) {
901                         if (!(data->have_in & (1 << i)))
902                                 continue;
903
904                         data->in[i][0] = nct6775_read_value(data,
905                                                             data->REG_VIN[i]);
906                         data->in[i][1] = nct6775_read_value(data,
907                                           data->REG_IN_MINMAX[0][i]);
908                         data->in[i][2] = nct6775_read_value(data,
909                                           data->REG_IN_MINMAX[1][i]);
910                 }
911
912                 /* Measured fan speeds and limits */
913                 for (i = 0; i < 5; i++) {
914                         u16 reg;
915
916                         if (!(data->has_fan & (1 << i)))
917                                 continue;
918
919                         reg = nct6775_read_value(data, data->REG_FAN[i]);
920                         data->rpm[i] = data->fan_from_reg(reg,
921                                                           data->fan_div[i]);
922
923                         if (data->has_fan_min & (1 << i))
924                                 data->fan_min[i] = nct6775_read_value(data,
925                                            data->REG_FAN_MIN[i]);
926
927                         /*
928                          * If we failed to measure the fan speed and clock
929                          * divider can be increased, let's try that for next
930                          * time
931                          */
932                         if (data->has_fan_div
933                             && (reg >= 0xff || (data->kind == nct6775
934                                                 && reg == 0x00))
935                             && data->fan_div[i] < 0x07) {
936                                 dev_dbg(dev, "Increasing fan%d "
937                                         "clock divider from %u to %u\n",
938                                         i + 1, div_from_reg(data->fan_div[i]),
939                                         div_from_reg(data->fan_div[i] + 1));
940                                 data->fan_div[i]++;
941                                 nct6775_write_fan_div_common(dev, data, i);
942                                 /* Preserve min limit if possible */
943                                 if ((data->has_fan_min & (1 << i))
944                                  && data->fan_min[i] >= 2
945                                  && data->fan_min[i] != 255)
946                                         nct6775_write_value(data,
947                                                 data->REG_FAN_MIN[i],
948                                                 (data->fan_min[i] /= 2));
949                         }
950                 }
951
952                 nct6775_update_pwm(dev);
953                 nct6775_update_pwm_limits(dev);
954
955                 /* Measured temperatures and limits */
956                 for (i = 0; i < NUM_TEMP; i++) {
957                         if (!(data->have_temp & (1 << i)))
958                                 continue;
959                         for (j = 0; j < 3; j++) {
960                                 if (data->reg_temp[j][i])
961                                         data->temp[j][i]
962                                           = nct6775_read_temp(data,
963                                                 data->reg_temp[j][i]);
964                         }
965                         if (!(data->have_temp_fixed & (1 << i)))
966                                 continue;
967                         data->temp_offset[i]
968                           = nct6775_read_value(data, data->REG_TEMP_OFFSET[i]);
969                 }
970
971                 data->alarms = 0;
972                 for (i = 0; i < 6; i++) {
973                         u8 alarm;
974                         if (!data->REG_ALARM[i])
975                                 continue;
976                         alarm = nct6775_read_value(data, data->REG_ALARM[i]);
977                         data->alarms |= ((u64)alarm) << (i << 3);
978                 }
979
980                 data->caseopen = nct6775_read_value(data, data->REG_CASEOPEN);
981
982                 data->last_updated = jiffies;
983                 data->valid = 1;
984         }
985
986         mutex_unlock(&data->update_lock);
987         return data;
988 }
989
990 /*
991  * Sysfs callback functions
992  */
993 static ssize_t
994 show_in_reg(struct device *dev, struct device_attribute *attr, char *buf)
995 {
996         struct nct6775_data *data = nct6775_update_device(dev);
997         struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
998         int nr = sattr->nr;
999         int index = sattr->index;
1000         return sprintf(buf, "%ld\n", in_from_reg(data->in[nr][index], nr));
1001 }
1002
1003 static ssize_t
1004 store_in_reg(struct device *dev, struct device_attribute *attr, const char *buf,
1005              size_t count)
1006 {
1007         struct nct6775_data *data = dev_get_drvdata(dev);
1008         struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
1009         int nr = sattr->nr;
1010         int index = sattr->index;
1011         unsigned long val;
1012         int err = kstrtoul(buf, 10, &val);
1013         if (err < 0)
1014                 return err;
1015         mutex_lock(&data->update_lock);
1016         data->in[nr][index] = in_to_reg(val, nr);
1017         nct6775_write_value(data, data->REG_IN_MINMAX[index-1][nr],
1018                             data->in[nr][index]);
1019         mutex_unlock(&data->update_lock);
1020         return count;
1021 }
1022
1023 static ssize_t
1024 show_alarm(struct device *dev, struct device_attribute *attr, char *buf)
1025 {
1026         struct nct6775_data *data = nct6775_update_device(dev);
1027         struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr);
1028         int nr = sattr->index;
1029         return sprintf(buf, "%u\n",
1030                        (unsigned int)((data->alarms >> nr) & 0x01));
1031 }
1032
1033 static SENSOR_DEVICE_ATTR_2(in0_input, S_IRUGO, show_in_reg, NULL, 0, 0);
1034 static SENSOR_DEVICE_ATTR_2(in1_input, S_IRUGO, show_in_reg, NULL, 1, 0);
1035 static SENSOR_DEVICE_ATTR_2(in2_input, S_IRUGO, show_in_reg, NULL, 2, 0);
1036 static SENSOR_DEVICE_ATTR_2(in3_input, S_IRUGO, show_in_reg, NULL, 3, 0);
1037 static SENSOR_DEVICE_ATTR_2(in4_input, S_IRUGO, show_in_reg, NULL, 4, 0);
1038 static SENSOR_DEVICE_ATTR_2(in5_input, S_IRUGO, show_in_reg, NULL, 5, 0);
1039 static SENSOR_DEVICE_ATTR_2(in6_input, S_IRUGO, show_in_reg, NULL, 6, 0);
1040 static SENSOR_DEVICE_ATTR_2(in7_input, S_IRUGO, show_in_reg, NULL, 7, 0);
1041 static SENSOR_DEVICE_ATTR_2(in8_input, S_IRUGO, show_in_reg, NULL, 8, 0);
1042 static SENSOR_DEVICE_ATTR_2(in9_input, S_IRUGO, show_in_reg, NULL, 9, 0);
1043 static SENSOR_DEVICE_ATTR_2(in10_input, S_IRUGO, show_in_reg, NULL, 10, 0);
1044 static SENSOR_DEVICE_ATTR_2(in11_input, S_IRUGO, show_in_reg, NULL, 11, 0);
1045 static SENSOR_DEVICE_ATTR_2(in12_input, S_IRUGO, show_in_reg, NULL, 12, 0);
1046 static SENSOR_DEVICE_ATTR_2(in13_input, S_IRUGO, show_in_reg, NULL, 13, 0);
1047 static SENSOR_DEVICE_ATTR_2(in14_input, S_IRUGO, show_in_reg, NULL, 14, 0);
1048
1049 static SENSOR_DEVICE_ATTR(in0_alarm, S_IRUGO, show_alarm, NULL, 0);
1050 static SENSOR_DEVICE_ATTR(in1_alarm, S_IRUGO, show_alarm, NULL, 1);
1051 static SENSOR_DEVICE_ATTR(in2_alarm, S_IRUGO, show_alarm, NULL, 2);
1052 static SENSOR_DEVICE_ATTR(in3_alarm, S_IRUGO, show_alarm, NULL, 3);
1053 static SENSOR_DEVICE_ATTR(in4_alarm, S_IRUGO, show_alarm, NULL, 8);
1054 static SENSOR_DEVICE_ATTR(in5_alarm, S_IRUGO, show_alarm, NULL, 21);
1055 static SENSOR_DEVICE_ATTR(in6_alarm, S_IRUGO, show_alarm, NULL, 20);
1056 static SENSOR_DEVICE_ATTR(in7_alarm, S_IRUGO, show_alarm, NULL, 16);
1057 static SENSOR_DEVICE_ATTR(in8_alarm, S_IRUGO, show_alarm, NULL, 17);
1058 static SENSOR_DEVICE_ATTR(in9_alarm, S_IRUGO, show_alarm, NULL, 24);
1059 static SENSOR_DEVICE_ATTR(in10_alarm, S_IRUGO, show_alarm, NULL, 25);
1060 static SENSOR_DEVICE_ATTR(in11_alarm, S_IRUGO, show_alarm, NULL, 26);
1061 static SENSOR_DEVICE_ATTR(in12_alarm, S_IRUGO, show_alarm, NULL, 27);
1062 static SENSOR_DEVICE_ATTR(in13_alarm, S_IRUGO, show_alarm, NULL, 28);
1063 static SENSOR_DEVICE_ATTR(in14_alarm, S_IRUGO, show_alarm, NULL, 29);
1064
1065 static SENSOR_DEVICE_ATTR_2(in0_min, S_IWUSR | S_IRUGO, show_in_reg,
1066                             store_in_reg, 0, 1);
1067 static SENSOR_DEVICE_ATTR_2(in1_min, S_IWUSR | S_IRUGO, show_in_reg,
1068                             store_in_reg, 1, 1);
1069 static SENSOR_DEVICE_ATTR_2(in2_min, S_IWUSR | S_IRUGO, show_in_reg,
1070                             store_in_reg, 2, 1);
1071 static SENSOR_DEVICE_ATTR_2(in3_min, S_IWUSR | S_IRUGO, show_in_reg,
1072                             store_in_reg, 3, 1);
1073 static SENSOR_DEVICE_ATTR_2(in4_min, S_IWUSR | S_IRUGO, show_in_reg,
1074                             store_in_reg, 4, 1);
1075 static SENSOR_DEVICE_ATTR_2(in5_min, S_IWUSR | S_IRUGO, show_in_reg,
1076                             store_in_reg, 5, 1);
1077 static SENSOR_DEVICE_ATTR_2(in6_min, S_IWUSR | S_IRUGO, show_in_reg,
1078                             store_in_reg, 6, 1);
1079 static SENSOR_DEVICE_ATTR_2(in7_min, S_IWUSR | S_IRUGO, show_in_reg,
1080                             store_in_reg, 7, 1);
1081 static SENSOR_DEVICE_ATTR_2(in8_min, S_IWUSR | S_IRUGO, show_in_reg,
1082                             store_in_reg, 8, 1);
1083 static SENSOR_DEVICE_ATTR_2(in9_min, S_IWUSR | S_IRUGO, show_in_reg,
1084                             store_in_reg, 9, 1);
1085 static SENSOR_DEVICE_ATTR_2(in10_min, S_IWUSR | S_IRUGO, show_in_reg,
1086                             store_in_reg, 10, 1);
1087 static SENSOR_DEVICE_ATTR_2(in11_min, S_IWUSR | S_IRUGO, show_in_reg,
1088                             store_in_reg, 11, 1);
1089 static SENSOR_DEVICE_ATTR_2(in12_min, S_IWUSR | S_IRUGO, show_in_reg,
1090                             store_in_reg, 12, 1);
1091 static SENSOR_DEVICE_ATTR_2(in13_min, S_IWUSR | S_IRUGO, show_in_reg,
1092                             store_in_reg, 13, 1);
1093 static SENSOR_DEVICE_ATTR_2(in14_min, S_IWUSR | S_IRUGO, show_in_reg,
1094                             store_in_reg, 14, 1);
1095
1096 static SENSOR_DEVICE_ATTR_2(in0_max, S_IWUSR | S_IRUGO, show_in_reg,
1097                           store_in_reg, 0, 2);
1098 static SENSOR_DEVICE_ATTR_2(in1_max, S_IWUSR | S_IRUGO, show_in_reg,
1099                           store_in_reg, 1, 2);
1100 static SENSOR_DEVICE_ATTR_2(in2_max, S_IWUSR | S_IRUGO, show_in_reg,
1101                           store_in_reg, 2, 2);
1102 static SENSOR_DEVICE_ATTR_2(in3_max, S_IWUSR | S_IRUGO, show_in_reg,
1103                           store_in_reg, 3, 2);
1104 static SENSOR_DEVICE_ATTR_2(in4_max, S_IWUSR | S_IRUGO, show_in_reg,
1105                           store_in_reg, 4, 2);
1106 static SENSOR_DEVICE_ATTR_2(in5_max, S_IWUSR | S_IRUGO, show_in_reg,
1107                           store_in_reg, 5, 2);
1108 static SENSOR_DEVICE_ATTR_2(in6_max, S_IWUSR | S_IRUGO, show_in_reg,
1109                           store_in_reg, 6, 2);
1110 static SENSOR_DEVICE_ATTR_2(in7_max, S_IWUSR | S_IRUGO, show_in_reg,
1111                           store_in_reg, 7, 2);
1112 static SENSOR_DEVICE_ATTR_2(in8_max, S_IWUSR | S_IRUGO, show_in_reg,
1113                           store_in_reg, 8, 2);
1114 static SENSOR_DEVICE_ATTR_2(in9_max, S_IWUSR | S_IRUGO, show_in_reg,
1115                           store_in_reg, 9, 2);
1116 static SENSOR_DEVICE_ATTR_2(in10_max, S_IWUSR | S_IRUGO, show_in_reg,
1117                           store_in_reg, 10, 2);
1118 static SENSOR_DEVICE_ATTR_2(in11_max, S_IWUSR | S_IRUGO, show_in_reg,
1119                           store_in_reg, 11, 2);
1120 static SENSOR_DEVICE_ATTR_2(in12_max, S_IWUSR | S_IRUGO, show_in_reg,
1121                           store_in_reg, 12, 2);
1122 static SENSOR_DEVICE_ATTR_2(in13_max, S_IWUSR | S_IRUGO, show_in_reg,
1123                           store_in_reg, 13, 2);
1124 static SENSOR_DEVICE_ATTR_2(in14_max, S_IWUSR | S_IRUGO, show_in_reg,
1125                           store_in_reg, 14, 2);
1126
1127 static struct attribute *nct6775_attributes_in[15][5] = {
1128         {
1129                 &sensor_dev_attr_in0_input.dev_attr.attr,
1130                 &sensor_dev_attr_in0_min.dev_attr.attr,
1131                 &sensor_dev_attr_in0_max.dev_attr.attr,
1132                 &sensor_dev_attr_in0_alarm.dev_attr.attr,
1133                 NULL
1134         },
1135         {
1136                 &sensor_dev_attr_in1_input.dev_attr.attr,
1137                 &sensor_dev_attr_in1_min.dev_attr.attr,
1138                 &sensor_dev_attr_in1_max.dev_attr.attr,
1139                 &sensor_dev_attr_in1_alarm.dev_attr.attr,
1140                 NULL
1141         },
1142         {
1143                 &sensor_dev_attr_in2_input.dev_attr.attr,
1144                 &sensor_dev_attr_in2_min.dev_attr.attr,
1145                 &sensor_dev_attr_in2_max.dev_attr.attr,
1146                 &sensor_dev_attr_in2_alarm.dev_attr.attr,
1147                 NULL
1148         },
1149         {
1150                 &sensor_dev_attr_in3_input.dev_attr.attr,
1151                 &sensor_dev_attr_in3_min.dev_attr.attr,
1152                 &sensor_dev_attr_in3_max.dev_attr.attr,
1153                 &sensor_dev_attr_in3_alarm.dev_attr.attr,
1154                 NULL
1155         },
1156         {
1157                 &sensor_dev_attr_in4_input.dev_attr.attr,
1158                 &sensor_dev_attr_in4_min.dev_attr.attr,
1159                 &sensor_dev_attr_in4_max.dev_attr.attr,
1160                 &sensor_dev_attr_in4_alarm.dev_attr.attr,
1161                 NULL
1162         },
1163         {
1164                 &sensor_dev_attr_in5_input.dev_attr.attr,
1165                 &sensor_dev_attr_in5_min.dev_attr.attr,
1166                 &sensor_dev_attr_in5_max.dev_attr.attr,
1167                 &sensor_dev_attr_in5_alarm.dev_attr.attr,
1168                 NULL
1169         },
1170         {
1171                 &sensor_dev_attr_in6_input.dev_attr.attr,
1172                 &sensor_dev_attr_in6_min.dev_attr.attr,
1173                 &sensor_dev_attr_in6_max.dev_attr.attr,
1174                 &sensor_dev_attr_in6_alarm.dev_attr.attr,
1175                 NULL
1176         },
1177         {
1178                 &sensor_dev_attr_in7_input.dev_attr.attr,
1179                 &sensor_dev_attr_in7_min.dev_attr.attr,
1180                 &sensor_dev_attr_in7_max.dev_attr.attr,
1181                 &sensor_dev_attr_in7_alarm.dev_attr.attr,
1182                 NULL
1183         },
1184         {
1185                 &sensor_dev_attr_in8_input.dev_attr.attr,
1186                 &sensor_dev_attr_in8_min.dev_attr.attr,
1187                 &sensor_dev_attr_in8_max.dev_attr.attr,
1188                 &sensor_dev_attr_in8_alarm.dev_attr.attr,
1189                 NULL
1190         },
1191         {
1192                 &sensor_dev_attr_in9_input.dev_attr.attr,
1193                 &sensor_dev_attr_in9_min.dev_attr.attr,
1194                 &sensor_dev_attr_in9_max.dev_attr.attr,
1195                 &sensor_dev_attr_in9_alarm.dev_attr.attr,
1196                 NULL
1197         },
1198         {
1199                 &sensor_dev_attr_in10_input.dev_attr.attr,
1200                 &sensor_dev_attr_in10_min.dev_attr.attr,
1201                 &sensor_dev_attr_in10_max.dev_attr.attr,
1202                 &sensor_dev_attr_in10_alarm.dev_attr.attr,
1203                 NULL
1204         },
1205         {
1206                 &sensor_dev_attr_in11_input.dev_attr.attr,
1207                 &sensor_dev_attr_in11_min.dev_attr.attr,
1208                 &sensor_dev_attr_in11_max.dev_attr.attr,
1209                 &sensor_dev_attr_in11_alarm.dev_attr.attr,
1210                 NULL
1211         },
1212         {
1213                 &sensor_dev_attr_in12_input.dev_attr.attr,
1214                 &sensor_dev_attr_in12_min.dev_attr.attr,
1215                 &sensor_dev_attr_in12_max.dev_attr.attr,
1216                 &sensor_dev_attr_in12_alarm.dev_attr.attr,
1217                 NULL
1218         },
1219         {
1220                 &sensor_dev_attr_in13_input.dev_attr.attr,
1221                 &sensor_dev_attr_in13_min.dev_attr.attr,
1222                 &sensor_dev_attr_in13_max.dev_attr.attr,
1223                 &sensor_dev_attr_in13_alarm.dev_attr.attr,
1224                 NULL
1225         },
1226         {
1227                 &sensor_dev_attr_in14_input.dev_attr.attr,
1228                 &sensor_dev_attr_in14_min.dev_attr.attr,
1229                 &sensor_dev_attr_in14_max.dev_attr.attr,
1230                 &sensor_dev_attr_in14_alarm.dev_attr.attr,
1231                 NULL
1232         },
1233 };
1234
1235 static const struct attribute_group nct6775_group_in[15] = {
1236         { .attrs = nct6775_attributes_in[0] },
1237         { .attrs = nct6775_attributes_in[1] },
1238         { .attrs = nct6775_attributes_in[2] },
1239         { .attrs = nct6775_attributes_in[3] },
1240         { .attrs = nct6775_attributes_in[4] },
1241         { .attrs = nct6775_attributes_in[5] },
1242         { .attrs = nct6775_attributes_in[6] },
1243         { .attrs = nct6775_attributes_in[7] },
1244         { .attrs = nct6775_attributes_in[8] },
1245         { .attrs = nct6775_attributes_in[9] },
1246         { .attrs = nct6775_attributes_in[10] },
1247         { .attrs = nct6775_attributes_in[11] },
1248         { .attrs = nct6775_attributes_in[12] },
1249         { .attrs = nct6775_attributes_in[13] },
1250         { .attrs = nct6775_attributes_in[14] },
1251 };
1252
1253 static ssize_t
1254 show_fan(struct device *dev, struct device_attribute *attr, char *buf)
1255 {
1256         struct nct6775_data *data = nct6775_update_device(dev);
1257         struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr);
1258         int nr = sattr->index;
1259         return sprintf(buf, "%d\n", data->rpm[nr]);
1260 }
1261
1262 static ssize_t
1263 show_fan_min(struct device *dev, struct device_attribute *attr, char *buf)
1264 {
1265         struct nct6775_data *data = nct6775_update_device(dev);
1266         struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr);
1267         int nr = sattr->index;
1268         return sprintf(buf, "%d\n",
1269                        data->fan_from_reg_min(data->fan_min[nr],
1270                                               data->fan_div[nr]));
1271 }
1272
1273 static ssize_t
1274 show_fan_div(struct device *dev, struct device_attribute *attr, char *buf)
1275 {
1276         struct nct6775_data *data = nct6775_update_device(dev);
1277         struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr);
1278         int nr = sattr->index;
1279         return sprintf(buf, "%u\n", div_from_reg(data->fan_div[nr]));
1280 }
1281
1282 static ssize_t
1283 store_fan_min(struct device *dev, struct device_attribute *attr,
1284               const char *buf, size_t count)
1285 {
1286         struct nct6775_data *data = dev_get_drvdata(dev);
1287         struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr);
1288         int nr = sattr->index;
1289         unsigned long val;
1290         int err;
1291         unsigned int reg;
1292         u8 new_div;
1293
1294         err = kstrtoul(buf, 10, &val);
1295         if (err < 0)
1296                 return err;
1297
1298         mutex_lock(&data->update_lock);
1299         if (!data->has_fan_div) {
1300                 /*
1301                  * Only NCT6776F for now, so we know that this is a 13 bit
1302                  * register
1303                  */
1304                 if (!val) {
1305                         val = 0xff1f;
1306                 } else {
1307                         if (val > 1350000U)
1308                                 val = 135000U;
1309                         val = 1350000U / val;
1310                         val = (val & 0x1f) | ((val << 3) & 0xff00);
1311                 }
1312                 data->fan_min[nr] = val;
1313                 goto write_min; /* Leave fan divider alone */
1314         }
1315         if (!val) {
1316                 /* No min limit, alarm disabled */
1317                 data->fan_min[nr] = 255;
1318                 new_div = data->fan_div[nr]; /* No change */
1319                 dev_info(dev, "fan%u low limit and alarm disabled\n", nr + 1);
1320                 goto write_div;
1321         }
1322         reg = 1350000U / val;
1323         if (reg >= 128 * 255) {
1324                 /*
1325                  * Speed below this value cannot possibly be represented,
1326                  * even with the highest divider (128)
1327                  */
1328                 data->fan_min[nr] = 254;
1329                 new_div = 7; /* 128 == (1 << 7) */
1330                 dev_warn(dev, "fan%u low limit %lu below minimum %u, set to "
1331                          "minimum\n", nr + 1, val,
1332                          data->fan_from_reg_min(254, 7));
1333         } else if (!reg) {
1334                 /*
1335                  * Speed above this value cannot possibly be represented,
1336                  * even with the lowest divider (1)
1337                  */
1338                 data->fan_min[nr] = 1;
1339                 new_div = 0; /* 1 == (1 << 0) */
1340                 dev_warn(dev, "fan%u low limit %lu above maximum %u, set to "
1341                          "maximum\n", nr + 1, val,
1342                          data->fan_from_reg_min(1, 0));
1343         } else {
1344                 /*
1345                  * Automatically pick the best divider, i.e. the one such
1346                  * that the min limit will correspond to a register value
1347                  * in the 96..192 range
1348                  */
1349                 new_div = 0;
1350                 while (reg > 192 && new_div < 7) {
1351                         reg >>= 1;
1352                         new_div++;
1353                 }
1354                 data->fan_min[nr] = reg;
1355         }
1356
1357 write_div:
1358         /*
1359          * Write both the fan clock divider (if it changed) and the new
1360          * fan min (unconditionally)
1361          */
1362         if (new_div != data->fan_div[nr]) {
1363                 dev_dbg(dev, "fan%u clock divider changed from %u to %u\n",
1364                         nr + 1, div_from_reg(data->fan_div[nr]),
1365                         div_from_reg(new_div));
1366                 data->fan_div[nr] = new_div;
1367                 nct6775_write_fan_div_common(dev, data, nr);
1368                 /* Give the chip time to sample a new speed value */
1369                 data->last_updated = jiffies;
1370         }
1371
1372 write_min:
1373         nct6775_write_value(data, data->REG_FAN_MIN[nr],
1374                               data->fan_min[nr]);
1375         mutex_unlock(&data->update_lock);
1376
1377         return count;
1378 }
1379
1380 static struct sensor_device_attribute sda_fan_input[] = {
1381         SENSOR_ATTR(fan1_input, S_IRUGO, show_fan, NULL, 0),
1382         SENSOR_ATTR(fan2_input, S_IRUGO, show_fan, NULL, 1),
1383         SENSOR_ATTR(fan3_input, S_IRUGO, show_fan, NULL, 2),
1384         SENSOR_ATTR(fan4_input, S_IRUGO, show_fan, NULL, 3),
1385         SENSOR_ATTR(fan5_input, S_IRUGO, show_fan, NULL, 4),
1386 };
1387
1388 static struct sensor_device_attribute sda_fan_alarm[] = {
1389         SENSOR_ATTR(fan1_alarm, S_IRUGO, show_alarm, NULL, 6),
1390         SENSOR_ATTR(fan2_alarm, S_IRUGO, show_alarm, NULL, 7),
1391         SENSOR_ATTR(fan3_alarm, S_IRUGO, show_alarm, NULL, 11),
1392         SENSOR_ATTR(fan4_alarm, S_IRUGO, show_alarm, NULL, 10),
1393         SENSOR_ATTR(fan5_alarm, S_IRUGO, show_alarm, NULL, 23),
1394 };
1395
1396 static struct sensor_device_attribute sda_fan_min[] = {
1397         SENSOR_ATTR(fan1_min, S_IWUSR | S_IRUGO, show_fan_min,
1398                     store_fan_min, 0),
1399         SENSOR_ATTR(fan2_min, S_IWUSR | S_IRUGO, show_fan_min,
1400                     store_fan_min, 1),
1401         SENSOR_ATTR(fan3_min, S_IWUSR | S_IRUGO, show_fan_min,
1402                     store_fan_min, 2),
1403         SENSOR_ATTR(fan4_min, S_IWUSR | S_IRUGO, show_fan_min,
1404                     store_fan_min, 3),
1405         SENSOR_ATTR(fan5_min, S_IWUSR | S_IRUGO, show_fan_min,
1406                     store_fan_min, 4),
1407 };
1408
1409 static struct sensor_device_attribute sda_fan_div[] = {
1410         SENSOR_ATTR(fan1_div, S_IRUGO, show_fan_div, NULL, 0),
1411         SENSOR_ATTR(fan2_div, S_IRUGO, show_fan_div, NULL, 1),
1412         SENSOR_ATTR(fan3_div, S_IRUGO, show_fan_div, NULL, 2),
1413         SENSOR_ATTR(fan4_div, S_IRUGO, show_fan_div, NULL, 3),
1414         SENSOR_ATTR(fan5_div, S_IRUGO, show_fan_div, NULL, 4),
1415 };
1416
1417 static ssize_t
1418 show_temp_label(struct device *dev, struct device_attribute *attr, char *buf)
1419 {
1420         struct nct6775_data *data = nct6775_update_device(dev);
1421         struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr);
1422         int nr = sattr->index;
1423         return sprintf(buf, "%s\n", data->temp_label[data->temp_src[nr]]);
1424 }
1425
1426 static ssize_t
1427 show_temp(struct device *dev, struct device_attribute *attr, char *buf)
1428 {
1429         struct nct6775_data *data = nct6775_update_device(dev);
1430         struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
1431         int nr = sattr->nr;
1432         int index = sattr->index;
1433
1434         return sprintf(buf, "%d\n", LM75_TEMP_FROM_REG(data->temp[index][nr]));
1435 }
1436
1437 static ssize_t
1438 store_temp(struct device *dev, struct device_attribute *attr, const char *buf,
1439            size_t count)
1440 {
1441         struct nct6775_data *data = dev_get_drvdata(dev);
1442         struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
1443         int nr = sattr->nr;
1444         int index = sattr->index;
1445         int err;
1446         long val;
1447
1448         err = kstrtol(buf, 10, &val);
1449         if (err < 0)
1450                 return err;
1451
1452         mutex_lock(&data->update_lock);
1453         data->temp[index][nr] = LM75_TEMP_TO_REG(val);
1454         nct6775_write_temp(data, data->reg_temp[index][nr],
1455                            data->temp[index][nr]);
1456         mutex_unlock(&data->update_lock);
1457         return count;
1458 }
1459
1460 static ssize_t
1461 show_temp_offset(struct device *dev, struct device_attribute *attr, char *buf)
1462 {
1463         struct nct6775_data *data = nct6775_update_device(dev);
1464         struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr);
1465
1466         return sprintf(buf, "%d\n", data->temp_offset[sattr->index] * 1000);
1467 }
1468
1469 static ssize_t
1470 store_temp_offset(struct device *dev, struct device_attribute *attr,
1471                   const char *buf, size_t count)
1472 {
1473         struct nct6775_data *data = dev_get_drvdata(dev);
1474         struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr);
1475         int nr = sattr->index;
1476         long val;
1477         int err;
1478
1479         err = kstrtol(buf, 10, &val);
1480         if (err < 0)
1481                 return err;
1482
1483         val = SENSORS_LIMIT(DIV_ROUND_CLOSEST(val, 1000), -128, 127);
1484
1485         mutex_lock(&data->update_lock);
1486         data->temp_offset[nr] = val;
1487         nct6775_write_value(data, data->REG_TEMP_OFFSET[nr], val);
1488         mutex_unlock(&data->update_lock);
1489
1490         return count;
1491 }
1492
1493 static ssize_t
1494 show_temp_type(struct device *dev, struct device_attribute *attr, char *buf)
1495 {
1496         struct nct6775_data *data = nct6775_update_device(dev);
1497         struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr);
1498         int nr = sattr->index;
1499         return sprintf(buf, "%d\n", (int)data->temp_type[nr]);
1500 }
1501
1502 static ssize_t
1503 store_temp_type(struct device *dev, struct device_attribute *attr,
1504                 const char *buf, size_t count)
1505 {
1506         struct nct6775_data *data = nct6775_update_device(dev);
1507         struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr);
1508         int nr = sattr->index;
1509         unsigned long val;
1510         int err;
1511         u8 vbat, diode, bit;
1512
1513         err = kstrtoul(buf, 10, &val);
1514         if (err < 0)
1515                 return err;
1516
1517         if (val != 1 && val != 3 && val != 4)
1518                 return -EINVAL;
1519
1520         mutex_lock(&data->update_lock);
1521
1522         data->temp_type[nr] = val;
1523         vbat = nct6775_read_value(data, data->REG_VBAT) & ~(0x02 << nr);
1524         diode = nct6775_read_value(data, data->REG_DIODE) & ~(0x02 << nr);
1525         bit = 0x02 << nr;
1526         switch (val) {
1527         case 1: /* CPU diode (diode, current mode) */
1528                 vbat |= bit;
1529                 diode |= bit;
1530                 break;
1531         case 3: /* diode, voltage mode */
1532                 vbat |= bit;
1533                 break;
1534         case 4: /* thermistor */
1535                 break;
1536         };
1537         nct6775_write_value(data, data->REG_VBAT, vbat);
1538         nct6775_write_value(data, data->REG_DIODE, diode);
1539
1540         mutex_unlock(&data->update_lock);
1541         return count;
1542 }
1543
1544 static struct sensor_device_attribute_2 sda_temp_input[] = {
1545         SENSOR_ATTR_2(temp1_input, S_IRUGO, show_temp, NULL, 0, 0),
1546         SENSOR_ATTR_2(temp2_input, S_IRUGO, show_temp, NULL, 1, 0),
1547         SENSOR_ATTR_2(temp3_input, S_IRUGO, show_temp, NULL, 2, 0),
1548         SENSOR_ATTR_2(temp4_input, S_IRUGO, show_temp, NULL, 3, 0),
1549         SENSOR_ATTR_2(temp5_input, S_IRUGO, show_temp, NULL, 4, 0),
1550         SENSOR_ATTR_2(temp6_input, S_IRUGO, show_temp, NULL, 5, 0),
1551         SENSOR_ATTR_2(temp7_input, S_IRUGO, show_temp, NULL, 6, 0),
1552         SENSOR_ATTR_2(temp8_input, S_IRUGO, show_temp, NULL, 7, 0),
1553         SENSOR_ATTR_2(temp9_input, S_IRUGO, show_temp, NULL, 8, 0),
1554         SENSOR_ATTR_2(temp10_input, S_IRUGO, show_temp, NULL, 9, 0),
1555 };
1556
1557 static struct sensor_device_attribute sda_temp_label[] = {
1558         SENSOR_ATTR(temp1_label, S_IRUGO, show_temp_label, NULL, 0),
1559         SENSOR_ATTR(temp2_label, S_IRUGO, show_temp_label, NULL, 1),
1560         SENSOR_ATTR(temp3_label, S_IRUGO, show_temp_label, NULL, 2),
1561         SENSOR_ATTR(temp4_label, S_IRUGO, show_temp_label, NULL, 3),
1562         SENSOR_ATTR(temp5_label, S_IRUGO, show_temp_label, NULL, 4),
1563         SENSOR_ATTR(temp6_label, S_IRUGO, show_temp_label, NULL, 5),
1564         SENSOR_ATTR(temp7_label, S_IRUGO, show_temp_label, NULL, 6),
1565         SENSOR_ATTR(temp8_label, S_IRUGO, show_temp_label, NULL, 7),
1566         SENSOR_ATTR(temp9_label, S_IRUGO, show_temp_label, NULL, 8),
1567         SENSOR_ATTR(temp10_label, S_IRUGO, show_temp_label, NULL, 9),
1568 };
1569
1570 static struct sensor_device_attribute_2 sda_temp_max[] = {
1571         SENSOR_ATTR_2(temp1_max, S_IRUGO | S_IWUSR, show_temp, store_temp,
1572                       0, 1),
1573         SENSOR_ATTR_2(temp2_max, S_IRUGO | S_IWUSR, show_temp, store_temp,
1574                       1, 1),
1575         SENSOR_ATTR_2(temp3_max, S_IRUGO | S_IWUSR, show_temp, store_temp,
1576                       2, 1),
1577         SENSOR_ATTR_2(temp4_max, S_IRUGO | S_IWUSR, show_temp, store_temp,
1578                       3, 1),
1579         SENSOR_ATTR_2(temp5_max, S_IRUGO | S_IWUSR, show_temp, store_temp,
1580                       4, 1),
1581         SENSOR_ATTR_2(temp6_max, S_IRUGO | S_IWUSR, show_temp, store_temp,
1582                       5, 1),
1583         SENSOR_ATTR_2(temp7_max, S_IRUGO | S_IWUSR, show_temp, store_temp,
1584                       6, 1),
1585         SENSOR_ATTR_2(temp8_max, S_IRUGO | S_IWUSR, show_temp, store_temp,
1586                       7, 1),
1587         SENSOR_ATTR_2(temp9_max, S_IRUGO | S_IWUSR, show_temp, store_temp,
1588                       8, 1),
1589         SENSOR_ATTR_2(temp10_max, S_IRUGO | S_IWUSR, show_temp, store_temp,
1590                       9, 1),
1591 };
1592
1593 static struct sensor_device_attribute_2 sda_temp_max_hyst[] = {
1594         SENSOR_ATTR_2(temp1_max_hyst, S_IRUGO | S_IWUSR, show_temp, store_temp,
1595                       0, 2),
1596         SENSOR_ATTR_2(temp2_max_hyst, S_IRUGO | S_IWUSR, show_temp, store_temp,
1597                       1, 2),
1598         SENSOR_ATTR_2(temp3_max_hyst, S_IRUGO | S_IWUSR, show_temp, store_temp,
1599                       2, 2),
1600         SENSOR_ATTR_2(temp4_max_hyst, S_IRUGO | S_IWUSR, show_temp, store_temp,
1601                       3, 2),
1602         SENSOR_ATTR_2(temp5_max_hyst, S_IRUGO | S_IWUSR, show_temp, store_temp,
1603                       4, 2),
1604         SENSOR_ATTR_2(temp6_max_hyst, S_IRUGO | S_IWUSR, show_temp, store_temp,
1605                       5, 2),
1606         SENSOR_ATTR_2(temp7_max_hyst, S_IRUGO | S_IWUSR, show_temp, store_temp,
1607                       6, 2),
1608         SENSOR_ATTR_2(temp8_max_hyst, S_IRUGO | S_IWUSR, show_temp, store_temp,
1609                       7, 2),
1610         SENSOR_ATTR_2(temp9_max_hyst, S_IRUGO | S_IWUSR, show_temp, store_temp,
1611                       8, 2),
1612         SENSOR_ATTR_2(temp9_max_hyst, S_IRUGO | S_IWUSR, show_temp, store_temp,
1613                       9, 2),
1614 };
1615
1616 static struct sensor_device_attribute sda_temp_offset[] = {
1617         SENSOR_ATTR(temp1_offset, S_IRUGO | S_IWUSR, show_temp_offset,
1618                     store_temp_offset, 0),
1619         SENSOR_ATTR(temp2_offset, S_IRUGO | S_IWUSR, show_temp_offset,
1620                     store_temp_offset, 1),
1621         SENSOR_ATTR(temp3_offset, S_IRUGO | S_IWUSR, show_temp_offset,
1622                     store_temp_offset, 2),
1623         SENSOR_ATTR(temp4_offset, S_IRUGO | S_IWUSR, show_temp_offset,
1624                     store_temp_offset, 3),
1625         SENSOR_ATTR(temp5_offset, S_IRUGO | S_IWUSR, show_temp_offset,
1626                     store_temp_offset, 4),
1627         SENSOR_ATTR(temp6_offset, S_IRUGO | S_IWUSR, show_temp_offset,
1628                     store_temp_offset, 5),
1629 };
1630
1631 static struct sensor_device_attribute sda_temp_type[] = {
1632         SENSOR_ATTR(temp1_type, S_IRUGO | S_IWUSR, show_temp_type,
1633                     store_temp_type, 0),
1634         SENSOR_ATTR(temp2_type, S_IRUGO | S_IWUSR, show_temp_type,
1635                     store_temp_type, 1),
1636         SENSOR_ATTR(temp3_type, S_IRUGO | S_IWUSR, show_temp_type,
1637                     store_temp_type, 2),
1638         SENSOR_ATTR(temp4_type, S_IRUGO | S_IWUSR, show_temp_type,
1639                     store_temp_type, 3),
1640         SENSOR_ATTR(temp5_type, S_IRUGO | S_IWUSR, show_temp_type,
1641                     store_temp_type, 4),
1642         SENSOR_ATTR(temp6_type, S_IRUGO | S_IWUSR, show_temp_type,
1643                     store_temp_type, 5),
1644 };
1645
1646 static struct sensor_device_attribute sda_temp_alarm[] = {
1647         SENSOR_ATTR(temp1_alarm, S_IRUGO, show_alarm, NULL, 4),
1648         SENSOR_ATTR(temp2_alarm, S_IRUGO, show_alarm, NULL, 5),
1649         SENSOR_ATTR(temp3_alarm, S_IRUGO, show_alarm, NULL, 13),
1650 };
1651
1652 #define NUM_TEMP_ALARM  ARRAY_SIZE(sda_temp_alarm)
1653
1654 static ssize_t
1655 show_pwm_mode(struct device *dev, struct device_attribute *attr, char *buf)
1656 {
1657         struct nct6775_data *data = nct6775_update_device(dev);
1658         struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr);
1659
1660         return sprintf(buf, "%d\n", !data->pwm_mode[sattr->index]);
1661 }
1662
1663 static ssize_t
1664 store_pwm_mode(struct device *dev, struct device_attribute *attr,
1665                         const char *buf, size_t count)
1666 {
1667         struct nct6775_data *data = dev_get_drvdata(dev);
1668         struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr);
1669         int nr = sattr->index;
1670         unsigned long val;
1671         int err;
1672         u8 reg;
1673
1674         err = kstrtoul(buf, 10, &val);
1675         if (err < 0)
1676                 return err;
1677
1678         if (val > 1)
1679                 return -EINVAL;
1680
1681         /* Setting DC mode is not supported for all chips/channels */
1682         if (data->REG_PWM_MODE[nr] == 0) {
1683                 if (val)
1684                         return -EINVAL;
1685                 return count;
1686         }
1687
1688         mutex_lock(&data->update_lock);
1689         data->pwm_mode[nr] = val;
1690         reg = nct6775_read_value(data, data->REG_PWM_MODE[nr]);
1691         reg &= ~data->PWM_MODE_MASK[nr];
1692         if (val)
1693                 reg |= data->PWM_MODE_MASK[nr];
1694         nct6775_write_value(data, data->REG_PWM_MODE[nr], reg);
1695         mutex_unlock(&data->update_lock);
1696         return count;
1697 }
1698
1699 static ssize_t
1700 show_pwm_enable(struct device *dev, struct device_attribute *attr, char *buf)
1701 {
1702         struct nct6775_data *data = nct6775_update_device(dev);
1703         struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr);
1704
1705         return sprintf(buf, "%d\n", data->pwm_enable[sattr->index]);
1706 }
1707
1708 static ssize_t
1709 show_pwm(struct device *dev, struct device_attribute *attr, char *buf)
1710 {
1711         struct nct6775_data *data = nct6775_update_device(dev);
1712         struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
1713         int nr = sattr->nr;
1714         int index = sattr->index;
1715         int pwm;
1716
1717         /*
1718          * For automatic fan control modes, show current pwm readings.
1719          * Otherwise, show the configured value.
1720          */
1721         if (index == 0 && data->pwm_enable[nr] > 1)
1722                 pwm = nct6775_read_value(data, data->REG_PWM_READ[nr]);
1723         else
1724                 pwm = data->pwm[index][nr];
1725
1726         return sprintf(buf, "%d\n", pwm);
1727 }
1728
1729 static ssize_t
1730 store_pwm(struct device *dev, struct device_attribute *attr, const char *buf,
1731           size_t count)
1732 {
1733         struct nct6775_data *data = dev_get_drvdata(dev);
1734         struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
1735         int nr = sattr->nr;
1736         int index = sattr->index;
1737         unsigned long val;
1738         int minval[7] = { 0, 1, 1, data->pwm[2][nr], 0, 0, 0 };
1739         int maxval[7]
1740           = { 255, 255, data->pwm[3][nr] ? : 255, 255, 255, 255, 255 };
1741         int err;
1742
1743         err = kstrtoul(buf, 10, &val);
1744         if (err < 0)
1745                 return err;
1746         val = SENSORS_LIMIT(val, minval[index], maxval[index]);
1747
1748         mutex_lock(&data->update_lock);
1749         data->pwm[index][nr] = val;
1750         nct6775_write_value(data, data->REG_PWM[index][nr], val);
1751         mutex_unlock(&data->update_lock);
1752         return count;
1753 }
1754
1755 /* Returns 0 if OK, -EINVAL otherwise */
1756 static int check_trip_points(struct nct6775_data *data, int nr)
1757 {
1758         int i;
1759
1760         for (i = 0; i < data->auto_pwm_num - 1; i++) {
1761                 if (data->auto_temp[nr][i] > data->auto_temp[nr][i + 1])
1762                         return -EINVAL;
1763         }
1764         for (i = 0; i < data->auto_pwm_num - 1; i++) {
1765                 if (data->auto_pwm[nr][i] > data->auto_pwm[nr][i + 1])
1766                         return -EINVAL;
1767         }
1768         /* validate critical temperature and pwm if enabled (pwm > 0) */
1769         if (data->auto_pwm[nr][data->auto_pwm_num]) {
1770                 if (data->auto_temp[nr][data->auto_pwm_num - 1] >
1771                                 data->auto_temp[nr][data->auto_pwm_num] ||
1772                     data->auto_pwm[nr][data->auto_pwm_num - 1] >
1773                                 data->auto_pwm[nr][data->auto_pwm_num])
1774                         return -EINVAL;
1775         }
1776         return 0;
1777 }
1778
1779 static ssize_t
1780 store_pwm_enable(struct device *dev, struct device_attribute *attr,
1781                  const char *buf, size_t count)
1782 {
1783         struct nct6775_data *data = dev_get_drvdata(dev);
1784         struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr);
1785         int nr = sattr->index;
1786         unsigned long val;
1787         int err;
1788         u16 reg;
1789
1790         err = kstrtoul(buf, 10, &val);
1791         if (err < 0)
1792                 return err;
1793
1794         if (val > 5)
1795                 return -EINVAL;
1796
1797         if (val == 4 && data->kind != nct6775)
1798                 return -EINVAL;
1799
1800         if (val == 5 && check_trip_points(data, nr)) {
1801                 dev_err(dev, "Inconsistent trip points, not switching to SmartFan IV mode\n");
1802                 dev_err(dev, "Adjust trip points and try again\n");
1803                 return -EINVAL;
1804         }
1805
1806         mutex_lock(&data->update_lock);
1807         data->pwm_enable[nr] = val;
1808         if (!val) {
1809                 /*
1810                  * turn off pwm control: select manual mode, set pwm to maximum
1811                  */
1812                 data->pwm[0][nr] = 255;
1813                 nct6775_write_value(data, data->REG_PWM[0][nr], 255);
1814         }
1815         reg = nct6775_read_value(data, data->REG_FAN_MODE[nr]);
1816         reg &= 0x0f;
1817         reg |= pwm_enable_to_reg(val) << 4;
1818         nct6775_write_value(data, data->REG_FAN_MODE[nr], reg);
1819         mutex_unlock(&data->update_lock);
1820         return count;
1821 }
1822
1823 static ssize_t
1824 show_pwm_temp(struct device *dev, struct device_attribute *attr, char *buf)
1825 {
1826         struct nct6775_data *data = nct6775_update_device(dev);
1827         struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr);
1828         int nr = sattr->index;
1829
1830         return sprintf(buf, "%d\n", LM75_TEMP_FROM_REG(data->pwm_temp[nr]));
1831 }
1832
1833 static ssize_t
1834 show_pwm_temp_sel(struct device *dev, struct device_attribute *attr, char *buf)
1835 {
1836         struct nct6775_data *data = nct6775_update_device(dev);
1837         struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
1838
1839         return sprintf(buf, "%d\n",
1840                        data->pwm_temp_sel[sattr->index][sattr->nr]);
1841 }
1842
1843 static ssize_t
1844 store_pwm_temp_sel(struct device *dev, struct device_attribute *attr,
1845                    const char *buf, size_t count)
1846 {
1847         struct nct6775_data *data = nct6775_update_device(dev);
1848         struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
1849         int nr = sattr->nr;
1850         int index = sattr->index;
1851         unsigned long val;
1852         int err;
1853         int reg;
1854
1855         err = kstrtoul(buf, 10, &val);
1856         if (err < 0)
1857                 return err;
1858         if (val == 0 || val > 0x1f)
1859                 return -EINVAL;
1860
1861         val = SENSORS_LIMIT(val, 1, data->temp_label_num - 1);
1862
1863         if (!strlen(data->temp_label[val]))
1864                 return -EINVAL;
1865
1866         mutex_lock(&data->update_lock);
1867         data->pwm_temp_sel[index][nr] = val;
1868         reg = nct6775_read_value(data, data->REG_TEMP_SEL[index][nr]);
1869         reg &= 0xe0;
1870         reg |= val;
1871         nct6775_write_value(data, data->REG_TEMP_SEL[index][nr], reg);
1872         mutex_unlock(&data->update_lock);
1873
1874         return count;
1875 }
1876
1877 static ssize_t
1878 show_target_temp(struct device *dev, struct device_attribute *attr, char *buf)
1879 {
1880         struct nct6775_data *data = nct6775_update_device(dev);
1881         struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr);
1882
1883         return sprintf(buf, "%d\n",
1884                        data->target_temp[sattr->index] * 1000);
1885 }
1886
1887 static ssize_t
1888 store_target_temp(struct device *dev, struct device_attribute *attr,
1889                   const char *buf, size_t count)
1890 {
1891         struct nct6775_data *data = dev_get_drvdata(dev);
1892         struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr);
1893         int nr = sattr->index;
1894         long val;
1895         int err;
1896
1897         err = kstrtol(buf, 10, &val);
1898         if (err < 0)
1899                 return err;
1900
1901         val = SENSORS_LIMIT(DIV_ROUND_CLOSEST(val, 1000), 0, 127);
1902
1903         mutex_lock(&data->update_lock);
1904         data->target_temp[nr] = val;
1905         nct6775_write_value(data, data->REG_TARGET[nr], val);
1906         mutex_unlock(&data->update_lock);
1907         return count;
1908 }
1909
1910 static ssize_t
1911 show_auto_temp_hyst(struct device *dev, struct device_attribute *attr,
1912                     char *buf)
1913 {
1914         struct nct6775_data *data = nct6775_update_device(dev);
1915         struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
1916         int nr = sattr->nr;
1917         int point = sattr->index >= data->auto_pwm_num ? 1 : 0;
1918         int tolerance = data->tolerance[nr][point];
1919         int temp = data->auto_temp[nr][sattr->index];
1920
1921         return sprintf(buf, "%d\n", (temp - tolerance) * 1000);
1922 }
1923
1924 static ssize_t
1925 store_auto_temp_hyst(struct device *dev, struct device_attribute *attr,
1926                      const char *buf, size_t count)
1927 {
1928         struct nct6775_data *data = dev_get_drvdata(dev);
1929         struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
1930         int nr = sattr->nr;
1931         int point = sattr->index >= data->auto_pwm_num ? 1 : 0;
1932         u16 reg;
1933         long val;
1934         int err;
1935         int maxlimit[2][3] = { { 15, 7, 63 }, { 15, 7, 7 } };
1936         int mask[] = { 0x0f, 0x07, 0x07 };
1937         int temp;
1938
1939         err = kstrtol(buf, 10, &val);
1940         if (err < 0)
1941                 return err;
1942
1943         temp = data->auto_temp[nr][sattr->index];
1944         val = temp - DIV_ROUND_CLOSEST(val, 1000);
1945
1946         /* Limit tolerance as needed */
1947         val = SENSORS_LIMIT(val, 0, maxlimit[point][data->kind]);
1948
1949         mutex_lock(&data->update_lock);
1950         if (point) {
1951                 nct6775_write_value(data,
1952                                     data->REG_CRITICAL_TEMP_TOLERANCE[nr],
1953                                     val);
1954         } else {
1955                 reg = nct6775_read_value(data, data->REG_FAN_MODE[nr]);
1956                 reg = (reg & ~mask[nr]) | (val & mask[nr]);
1957                 nct6775_write_value(data, data->REG_FAN_MODE[nr], reg);
1958                 if (data->kind == nct6779) {
1959                         reg = nct6775_read_value(data,
1960                                                  NCT6779_REG_TOLERANCE_H[nr]);
1961                         reg = (reg & 0x70) | ((val & 0x38) << 1);
1962                         nct6775_write_value(data,
1963                                             NCT6779_REG_TOLERANCE_H[nr], reg);
1964                 }
1965         }
1966
1967         data->tolerance[nr][point] = val;
1968         mutex_unlock(&data->update_lock);
1969         return count;
1970 }
1971
1972 static SENSOR_DEVICE_ATTR_2(pwm1, S_IWUSR | S_IRUGO, show_pwm, store_pwm, 0, 0);
1973 static SENSOR_DEVICE_ATTR_2(pwm2, S_IWUSR | S_IRUGO, show_pwm, store_pwm, 1, 0);
1974 static SENSOR_DEVICE_ATTR_2(pwm3, S_IWUSR | S_IRUGO, show_pwm, store_pwm, 2, 0);
1975 static SENSOR_DEVICE_ATTR_2(pwm4, S_IWUSR | S_IRUGO, show_pwm, store_pwm, 3, 0);
1976 static SENSOR_DEVICE_ATTR_2(pwm5, S_IWUSR | S_IRUGO, show_pwm, store_pwm, 4, 0);
1977
1978 static SENSOR_DEVICE_ATTR(pwm1_mode, S_IWUSR | S_IRUGO, show_pwm_mode,
1979                           store_pwm_mode, 0);
1980 static SENSOR_DEVICE_ATTR(pwm2_mode, S_IWUSR | S_IRUGO, show_pwm_mode,
1981                           store_pwm_mode, 1);
1982 static SENSOR_DEVICE_ATTR(pwm3_mode, S_IWUSR | S_IRUGO, show_pwm_mode,
1983                           store_pwm_mode, 2);
1984 static SENSOR_DEVICE_ATTR(pwm4_mode, S_IWUSR | S_IRUGO, show_pwm_mode,
1985                           store_pwm_mode, 3);
1986 static SENSOR_DEVICE_ATTR(pwm5_mode, S_IWUSR | S_IRUGO, show_pwm_mode,
1987                           store_pwm_mode, 4);
1988
1989 static SENSOR_DEVICE_ATTR(pwm1_enable, S_IWUSR | S_IRUGO, show_pwm_enable,
1990                           store_pwm_enable, 0);
1991 static SENSOR_DEVICE_ATTR(pwm2_enable, S_IWUSR | S_IRUGO, show_pwm_enable,
1992                           store_pwm_enable, 1);
1993 static SENSOR_DEVICE_ATTR(pwm3_enable, S_IWUSR | S_IRUGO, show_pwm_enable,
1994                           store_pwm_enable, 2);
1995 static SENSOR_DEVICE_ATTR(pwm4_enable, S_IWUSR | S_IRUGO, show_pwm_enable,
1996                           store_pwm_enable, 3);
1997 static SENSOR_DEVICE_ATTR(pwm5_enable, S_IWUSR | S_IRUGO, show_pwm_enable,
1998                           store_pwm_enable, 4);
1999
2000 static SENSOR_DEVICE_ATTR_2(pwm1_temp_sel, S_IWUSR | S_IRUGO,
2001                             show_pwm_temp_sel, store_pwm_temp_sel, 0, 0);
2002 static SENSOR_DEVICE_ATTR_2(pwm2_temp_sel, S_IWUSR | S_IRUGO,
2003                             show_pwm_temp_sel, store_pwm_temp_sel, 1, 0);
2004 static SENSOR_DEVICE_ATTR_2(pwm3_temp_sel, S_IWUSR | S_IRUGO,
2005                             show_pwm_temp_sel, store_pwm_temp_sel, 2, 0);
2006 static SENSOR_DEVICE_ATTR_2(pwm4_temp_sel, S_IWUSR | S_IRUGO,
2007                             show_pwm_temp_sel, store_pwm_temp_sel, 3, 0);
2008 static SENSOR_DEVICE_ATTR_2(pwm5_temp_sel, S_IWUSR | S_IRUGO,
2009                             show_pwm_temp_sel, store_pwm_temp_sel, 4, 0);
2010
2011 static SENSOR_DEVICE_ATTR(pwm1_target, S_IWUSR | S_IRUGO, show_target_temp,
2012                           store_target_temp, 0);
2013 static SENSOR_DEVICE_ATTR(pwm2_target, S_IWUSR | S_IRUGO, show_target_temp,
2014                           store_target_temp, 1);
2015 static SENSOR_DEVICE_ATTR(pwm3_target, S_IWUSR | S_IRUGO, show_target_temp,
2016                           store_target_temp, 2);
2017 static SENSOR_DEVICE_ATTR(pwm4_target, S_IWUSR | S_IRUGO, show_target_temp,
2018                           store_target_temp, 3);
2019 static SENSOR_DEVICE_ATTR(pwm5_target, S_IWUSR | S_IRUGO, show_target_temp,
2020                           store_target_temp, 4);
2021
2022 /* Monitored pwm temperatures */
2023 static SENSOR_DEVICE_ATTR(temp11_input, S_IRUGO, show_pwm_temp, NULL, 0);
2024 static SENSOR_DEVICE_ATTR(temp12_input, S_IRUGO, show_pwm_temp, NULL, 1);
2025 static SENSOR_DEVICE_ATTR(temp13_input, S_IRUGO, show_pwm_temp, NULL, 2);
2026 static SENSOR_DEVICE_ATTR(temp14_input, S_IRUGO, show_pwm_temp, NULL, 3);
2027 static SENSOR_DEVICE_ATTR(temp15_input, S_IRUGO, show_pwm_temp, NULL, 4);
2028
2029 /* Smart Fan registers */
2030
2031 static ssize_t
2032 show_pwm_sel_enable(struct device *dev, struct device_attribute *attr,
2033                     char *buf)
2034 {
2035         struct nct6775_data *data = nct6775_update_device(dev);
2036         struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
2037
2038         return sprintf(buf, "%d\n",
2039                        data->pwm_sel_enable[sattr->index][sattr->nr]);
2040 }
2041
2042 static ssize_t
2043 store_pwm_sel_enable(struct device *dev, struct device_attribute *attr,
2044                      const char *buf, size_t count)
2045 {
2046         struct nct6775_data *data = dev_get_drvdata(dev);
2047         struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
2048         int nr = sattr->nr;
2049         int index = sattr->index;
2050         unsigned long val;
2051         int err;
2052         u8 reg;
2053
2054         err = kstrtoul(buf, 10, &val);
2055         if (err < 0)
2056                 return err;
2057
2058         if (val > 1)
2059                 return -EINVAL;
2060
2061         mutex_lock(&data->update_lock);
2062         data->pwm_sel_enable[index][nr] = val;
2063         reg = nct6775_read_value(data, data->REG_TEMP_SEL[index][nr]);
2064         reg &= 0x7f;
2065         if (val)
2066                 reg |= 0x80;
2067         nct6775_write_value(data, data->REG_TEMP_SEL[index][nr], reg);
2068         mutex_unlock(&data->update_lock);
2069         return count;
2070 }
2071
2072 static ssize_t
2073 show_weight_temp(struct device *dev, struct device_attribute *attr, char *buf)
2074 {
2075         struct nct6775_data *data = nct6775_update_device(dev);
2076         struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
2077         int nr = sattr->nr;
2078         int index = sattr->index;
2079
2080         return sprintf(buf, "%d\n", data->weight_temp[index][nr] * 1000);
2081 }
2082
2083 static ssize_t
2084 store_weight_temp(struct device *dev, struct device_attribute *attr,
2085                   const char *buf, size_t count)
2086 {
2087         struct nct6775_data *data = dev_get_drvdata(dev);
2088         struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
2089         int nr = sattr->nr;
2090         int index = sattr->index;
2091         unsigned long val;
2092         int err;
2093
2094         err = kstrtoul(buf, 10, &val);
2095         if (err < 0)
2096                 return err;
2097
2098         val = SENSORS_LIMIT(DIV_ROUND_CLOSEST(val, 1000), 0, 255);
2099
2100         mutex_lock(&data->update_lock);
2101         data->weight_temp[index][nr] = val;
2102         nct6775_write_value(data, data->REG_WEIGHT_TEMP[index][nr], val);
2103         mutex_unlock(&data->update_lock);
2104         return count;
2105 }
2106
2107 static SENSOR_DEVICE_ATTR_2(pwm1_stop_output_enable, S_IWUSR | S_IRUGO,
2108                             show_pwm_sel_enable, store_pwm_sel_enable, 0, 0);
2109 static SENSOR_DEVICE_ATTR_2(pwm2_stop_output_enable, S_IWUSR | S_IRUGO,
2110                             show_pwm_sel_enable, store_pwm_sel_enable, 1, 0);
2111 static SENSOR_DEVICE_ATTR_2(pwm3_stop_output_enable, S_IWUSR | S_IRUGO,
2112                             show_pwm_sel_enable, store_pwm_sel_enable, 2, 0);
2113 static SENSOR_DEVICE_ATTR_2(pwm4_stop_output_enable, S_IWUSR | S_IRUGO,
2114                             show_pwm_sel_enable, store_pwm_sel_enable, 3, 0);
2115 static SENSOR_DEVICE_ATTR_2(pwm5_stop_output_enable, S_IWUSR | S_IRUGO,
2116                             show_pwm_sel_enable, store_pwm_sel_enable, 4, 0);
2117
2118 static SENSOR_DEVICE_ATTR_2(pwm1_weight_enable, S_IWUSR | S_IRUGO,
2119                             show_pwm_sel_enable, store_pwm_sel_enable, 0, 1);
2120 static SENSOR_DEVICE_ATTR_2(pwm2_weight_enable, S_IWUSR | S_IRUGO,
2121                             show_pwm_sel_enable, store_pwm_sel_enable, 1, 1);
2122 static SENSOR_DEVICE_ATTR_2(pwm3_weight_enable, S_IWUSR | S_IRUGO,
2123                             show_pwm_sel_enable, store_pwm_sel_enable, 2, 1);
2124 static SENSOR_DEVICE_ATTR_2(pwm4_weight_enable, S_IWUSR | S_IRUGO,
2125                             show_pwm_sel_enable, store_pwm_sel_enable, 3, 1);
2126 static SENSOR_DEVICE_ATTR_2(pwm5_weight_enable, S_IWUSR | S_IRUGO,
2127                             show_pwm_sel_enable, store_pwm_sel_enable, 4, 1);
2128
2129 static SENSOR_DEVICE_ATTR_2(pwm1_weight_temp_sel, S_IWUSR | S_IRUGO,
2130                             show_pwm_temp_sel, store_pwm_temp_sel, 0, 1);
2131 static SENSOR_DEVICE_ATTR_2(pwm2_weight_temp_sel, S_IWUSR | S_IRUGO,
2132                             show_pwm_temp_sel, store_pwm_temp_sel, 1, 1);
2133 static SENSOR_DEVICE_ATTR_2(pwm3_weight_temp_sel, S_IWUSR | S_IRUGO,
2134                             show_pwm_temp_sel, store_pwm_temp_sel, 2, 1);
2135 static SENSOR_DEVICE_ATTR_2(pwm4_weight_temp_sel, S_IWUSR | S_IRUGO,
2136                             show_pwm_temp_sel, store_pwm_temp_sel, 3, 1);
2137 static SENSOR_DEVICE_ATTR_2(pwm5_weight_temp_sel, S_IWUSR | S_IRUGO,
2138                             show_pwm_temp_sel, store_pwm_temp_sel, 4, 1);
2139
2140 static SENSOR_DEVICE_ATTR_2(pwm1_weight_temp_step, S_IWUSR | S_IRUGO,
2141                             show_weight_temp, store_weight_temp, 0, 0);
2142 static SENSOR_DEVICE_ATTR_2(pwm2_weight_temp_step, S_IWUSR | S_IRUGO,
2143                             show_weight_temp, store_weight_temp, 1, 0);
2144 static SENSOR_DEVICE_ATTR_2(pwm3_weight_temp_step, S_IWUSR | S_IRUGO,
2145                             show_weight_temp, store_weight_temp, 2, 0);
2146 static SENSOR_DEVICE_ATTR_2(pwm4_weight_temp_step, S_IWUSR | S_IRUGO,
2147                             show_weight_temp, store_weight_temp, 3, 0);
2148 static SENSOR_DEVICE_ATTR_2(pwm5_weight_temp_step, S_IWUSR | S_IRUGO,
2149                             show_weight_temp, store_weight_temp, 4, 0);
2150
2151 static SENSOR_DEVICE_ATTR_2(pwm1_weight_temp_step_tol, S_IWUSR | S_IRUGO,
2152                             show_weight_temp, store_weight_temp, 0, 1);
2153 static SENSOR_DEVICE_ATTR_2(pwm2_weight_temp_step_tol, S_IWUSR | S_IRUGO,
2154                             show_weight_temp, store_weight_temp, 1, 1);
2155 static SENSOR_DEVICE_ATTR_2(pwm3_weight_temp_step_tol, S_IWUSR | S_IRUGO,
2156                             show_weight_temp, store_weight_temp, 2, 1);
2157 static SENSOR_DEVICE_ATTR_2(pwm4_weight_temp_step_tol, S_IWUSR | S_IRUGO,
2158                             show_weight_temp, store_weight_temp, 3, 1);
2159 static SENSOR_DEVICE_ATTR_2(pwm5_weight_temp_step_tol, S_IWUSR | S_IRUGO,
2160                             show_weight_temp, store_weight_temp, 4, 1);
2161
2162 static SENSOR_DEVICE_ATTR_2(pwm1_weight_temp_step_base, S_IWUSR | S_IRUGO,
2163                             show_weight_temp, store_weight_temp, 0, 2);
2164 static SENSOR_DEVICE_ATTR_2(pwm2_weight_temp_step_base, S_IWUSR | S_IRUGO,
2165                             show_weight_temp, store_weight_temp, 1, 2);
2166 static SENSOR_DEVICE_ATTR_2(pwm3_weight_temp_step_base, S_IWUSR | S_IRUGO,
2167                             show_weight_temp, store_weight_temp, 2, 2);
2168 static SENSOR_DEVICE_ATTR_2(pwm4_weight_temp_step_base, S_IWUSR | S_IRUGO,
2169                             show_weight_temp, store_weight_temp, 3, 2);
2170 static SENSOR_DEVICE_ATTR_2(pwm5_weight_temp_step_base, S_IWUSR | S_IRUGO,
2171                             show_weight_temp, store_weight_temp, 4, 2);
2172
2173 static SENSOR_DEVICE_ATTR_2(pwm1_weight_duty_step, S_IWUSR | S_IRUGO,
2174                             show_pwm, store_pwm, 0, 5);
2175 static SENSOR_DEVICE_ATTR_2(pwm2_weight_duty_step, S_IWUSR | S_IRUGO,
2176                             show_pwm, store_pwm, 1, 5);
2177 static SENSOR_DEVICE_ATTR_2(pwm3_weight_duty_step, S_IWUSR | S_IRUGO,
2178                             show_pwm, store_pwm, 2, 5);
2179 static SENSOR_DEVICE_ATTR_2(pwm4_weight_duty_step, S_IWUSR | S_IRUGO,
2180                             show_pwm, store_pwm, 3, 5);
2181 static SENSOR_DEVICE_ATTR_2(pwm5_weight_duty_step, S_IWUSR | S_IRUGO,
2182                             show_pwm, store_pwm, 4, 5);
2183
2184 /* duty_base is not supported on all chips */
2185 static struct sensor_device_attribute_2 sda_weight_duty_base[] = {
2186         SENSOR_ATTR_2(pwm1_weight_duty_base, S_IWUSR | S_IRUGO,
2187                       show_pwm, store_pwm, 0, 6),
2188         SENSOR_ATTR_2(pwm2_weight_duty_base, S_IWUSR | S_IRUGO,
2189                       show_pwm, store_pwm, 1, 6),
2190         SENSOR_ATTR_2(pwm3_weight_duty_base, S_IWUSR | S_IRUGO,
2191                       show_pwm, store_pwm, 2, 6),
2192         SENSOR_ATTR_2(pwm4_weight_duty_base, S_IWUSR | S_IRUGO,
2193                       show_pwm, store_pwm, 3, 6),
2194         SENSOR_ATTR_2(pwm5_weight_duty_base, S_IWUSR | S_IRUGO,
2195                       show_pwm, store_pwm, 4, 6),
2196 };
2197
2198 static ssize_t
2199 show_fan_time(struct device *dev, struct device_attribute *attr, char *buf)
2200 {
2201         struct nct6775_data *data = nct6775_update_device(dev);
2202         struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
2203         int nr = sattr->nr;
2204         int index = sattr->index;
2205
2206         return sprintf(buf, "%d\n",
2207                        step_time_from_reg(data->fan_time[index][nr],
2208                                           data->pwm_mode[nr]));
2209 }
2210
2211 static ssize_t
2212 store_fan_time(struct device *dev, struct device_attribute *attr,
2213                const char *buf, size_t count)
2214 {
2215         struct nct6775_data *data = dev_get_drvdata(dev);
2216         struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
2217         int nr = sattr->nr;
2218         int index = sattr->index;
2219         unsigned long val;
2220         int err;
2221
2222         err = kstrtoul(buf, 10, &val);
2223         if (err < 0)
2224                 return err;
2225
2226         val = step_time_to_reg(val, data->pwm_mode[nr]);
2227         mutex_lock(&data->update_lock);
2228         data->fan_time[index][nr] = val;
2229         nct6775_write_value(data, data->REG_FAN_TIME[index][nr], val);
2230         mutex_unlock(&data->update_lock);
2231         return count;
2232 }
2233
2234 static ssize_t
2235 show_name(struct device *dev, struct device_attribute *attr, char *buf)
2236 {
2237         struct nct6775_data *data = dev_get_drvdata(dev);
2238
2239         return sprintf(buf, "%s\n", data->name);
2240 }
2241
2242 static DEVICE_ATTR(name, S_IRUGO, show_name, NULL);
2243
2244 static SENSOR_DEVICE_ATTR_2(pwm1_stop_time, S_IWUSR | S_IRUGO, show_fan_time,
2245                             store_fan_time, 0, 0);
2246 static SENSOR_DEVICE_ATTR_2(pwm2_stop_time, S_IWUSR | S_IRUGO, show_fan_time,
2247                             store_fan_time, 1, 0);
2248 static SENSOR_DEVICE_ATTR_2(pwm3_stop_time, S_IWUSR | S_IRUGO, show_fan_time,
2249                             store_fan_time, 2, 0);
2250 static SENSOR_DEVICE_ATTR_2(pwm4_stop_time, S_IWUSR | S_IRUGO, show_fan_time,
2251                             store_fan_time, 3, 0);
2252 static SENSOR_DEVICE_ATTR_2(pwm5_stop_time, S_IWUSR | S_IRUGO, show_fan_time,
2253                             store_fan_time, 4, 0);
2254
2255 static SENSOR_DEVICE_ATTR_2(pwm1_step_up_time, S_IWUSR | S_IRUGO, show_fan_time,
2256                             store_fan_time, 0, 1);
2257 static SENSOR_DEVICE_ATTR_2(pwm2_step_up_time, S_IWUSR | S_IRUGO, show_fan_time,
2258                             store_fan_time, 1, 1);
2259 static SENSOR_DEVICE_ATTR_2(pwm3_step_up_time, S_IWUSR | S_IRUGO, show_fan_time,
2260                             store_fan_time, 2, 1);
2261 static SENSOR_DEVICE_ATTR_2(pwm4_step_up_time, S_IWUSR | S_IRUGO, show_fan_time,
2262                             store_fan_time, 3, 1);
2263 static SENSOR_DEVICE_ATTR_2(pwm5_step_up_time, S_IWUSR | S_IRUGO, show_fan_time,
2264                             store_fan_time, 4, 1);
2265
2266 static SENSOR_DEVICE_ATTR_2(pwm1_step_down_time, S_IWUSR | S_IRUGO,
2267                             show_fan_time, store_fan_time, 0, 2);
2268 static SENSOR_DEVICE_ATTR_2(pwm2_step_down_time, S_IWUSR | S_IRUGO,
2269                             show_fan_time, store_fan_time, 1, 2);
2270 static SENSOR_DEVICE_ATTR_2(pwm3_step_down_time, S_IWUSR | S_IRUGO,
2271                             show_fan_time, store_fan_time, 2, 2);
2272 static SENSOR_DEVICE_ATTR_2(pwm4_step_down_time, S_IWUSR | S_IRUGO,
2273                             show_fan_time, store_fan_time, 3, 2);
2274 static SENSOR_DEVICE_ATTR_2(pwm5_step_down_time, S_IWUSR | S_IRUGO,
2275                             show_fan_time, store_fan_time, 4, 2);
2276
2277 static SENSOR_DEVICE_ATTR_2(pwm1_start_output, S_IWUSR | S_IRUGO, show_pwm,
2278                             store_pwm, 0, 1);
2279 static SENSOR_DEVICE_ATTR_2(pwm2_start_output, S_IWUSR | S_IRUGO, show_pwm,
2280                             store_pwm, 1, 1);
2281 static SENSOR_DEVICE_ATTR_2(pwm3_start_output, S_IWUSR | S_IRUGO, show_pwm,
2282                             store_pwm, 2, 1);
2283 static SENSOR_DEVICE_ATTR_2(pwm4_start_output, S_IWUSR | S_IRUGO, show_pwm,
2284                             store_pwm, 3, 1);
2285 static SENSOR_DEVICE_ATTR_2(pwm5_start_output, S_IWUSR | S_IRUGO, show_pwm,
2286                             store_pwm, 4, 1);
2287
2288 static SENSOR_DEVICE_ATTR_2(pwm1_stop_output, S_IWUSR | S_IRUGO, show_pwm,
2289                             store_pwm, 0, 2);
2290 static SENSOR_DEVICE_ATTR_2(pwm2_stop_output, S_IWUSR | S_IRUGO, show_pwm,
2291                             store_pwm, 1, 2);
2292 static SENSOR_DEVICE_ATTR_2(pwm3_stop_output, S_IWUSR | S_IRUGO, show_pwm,
2293                             store_pwm, 2, 2);
2294 static SENSOR_DEVICE_ATTR_2(pwm4_stop_output, S_IWUSR | S_IRUGO, show_pwm,
2295                             store_pwm, 3, 2);
2296 static SENSOR_DEVICE_ATTR_2(pwm5_stop_output, S_IWUSR | S_IRUGO, show_pwm,
2297                             store_pwm, 4, 2);
2298
2299 /* max_output is not supported on all chips */
2300 static struct sensor_device_attribute_2 sda_max_output[] = {
2301         SENSOR_ATTR_2(pwm1_max_output, S_IWUSR | S_IRUGO, show_pwm, store_pwm,
2302                       0, 3),
2303         SENSOR_ATTR_2(pwm2_max_output, S_IWUSR | S_IRUGO, show_pwm, store_pwm,
2304                       1, 3),
2305         SENSOR_ATTR_2(pwm3_max_output, S_IWUSR | S_IRUGO, show_pwm, store_pwm,
2306                       2, 3),
2307         SENSOR_ATTR_2(pwm4_max_output, S_IWUSR | S_IRUGO, show_pwm, store_pwm,
2308                       3, 3),
2309         SENSOR_ATTR_2(pwm5_max_output, S_IWUSR | S_IRUGO, show_pwm, store_pwm,
2310                       4, 3),
2311 };
2312
2313 /* step_output is not supported on all chips */
2314 static struct sensor_device_attribute_2 sda_step_output[] = {
2315         SENSOR_ATTR_2(pwm1_step_output, S_IWUSR | S_IRUGO, show_pwm, store_pwm,
2316                       0, 4),
2317         SENSOR_ATTR_2(pwm2_step_output, S_IWUSR | S_IRUGO, show_pwm, store_pwm,
2318                       1, 4),
2319         SENSOR_ATTR_2(pwm3_step_output, S_IWUSR | S_IRUGO, show_pwm, store_pwm,
2320                       2, 4),
2321         SENSOR_ATTR_2(pwm4_step_output, S_IWUSR | S_IRUGO, show_pwm, store_pwm,
2322                       3, 4),
2323         SENSOR_ATTR_2(pwm5_step_output, S_IWUSR | S_IRUGO, show_pwm, store_pwm,
2324                       4, 4),
2325 };
2326
2327 static struct attribute *nct6775_attributes_pwm[5][20] = {
2328         {
2329                 &sensor_dev_attr_temp11_input.dev_attr.attr,
2330                 &sensor_dev_attr_pwm1.dev_attr.attr,
2331                 &sensor_dev_attr_pwm1_mode.dev_attr.attr,
2332                 &sensor_dev_attr_pwm1_enable.dev_attr.attr,
2333                 &sensor_dev_attr_pwm1_stop_output_enable.dev_attr.attr,
2334                 &sensor_dev_attr_pwm1_weight_enable.dev_attr.attr,
2335                 &sensor_dev_attr_pwm1_temp_sel.dev_attr.attr,
2336                 &sensor_dev_attr_pwm1_target.dev_attr.attr,
2337                 &sensor_dev_attr_pwm1_stop_time.dev_attr.attr,
2338                 &sensor_dev_attr_pwm1_step_up_time.dev_attr.attr,
2339                 &sensor_dev_attr_pwm1_step_down_time.dev_attr.attr,
2340                 &sensor_dev_attr_pwm1_start_output.dev_attr.attr,
2341                 &sensor_dev_attr_pwm1_stop_output.dev_attr.attr,
2342                 &sensor_dev_attr_pwm1_weight_temp_sel.dev_attr.attr,
2343                 &sensor_dev_attr_pwm1_weight_temp_step.dev_attr.attr,
2344                 &sensor_dev_attr_pwm1_weight_temp_step_tol.dev_attr.attr,
2345                 &sensor_dev_attr_pwm1_weight_temp_step_base.dev_attr.attr,
2346                 &sensor_dev_attr_pwm1_weight_duty_step.dev_attr.attr,
2347                 NULL
2348         },
2349         {
2350                 &sensor_dev_attr_temp12_input.dev_attr.attr,
2351                 &sensor_dev_attr_pwm2.dev_attr.attr,
2352                 &sensor_dev_attr_pwm2_mode.dev_attr.attr,
2353                 &sensor_dev_attr_pwm2_enable.dev_attr.attr,
2354                 &sensor_dev_attr_pwm2_stop_output_enable.dev_attr.attr,
2355                 &sensor_dev_attr_pwm2_weight_enable.dev_attr.attr,
2356                 &sensor_dev_attr_pwm2_temp_sel.dev_attr.attr,
2357                 &sensor_dev_attr_pwm2_target.dev_attr.attr,
2358                 &sensor_dev_attr_pwm2_stop_time.dev_attr.attr,
2359                 &sensor_dev_attr_pwm2_step_up_time.dev_attr.attr,
2360                 &sensor_dev_attr_pwm2_step_down_time.dev_attr.attr,
2361                 &sensor_dev_attr_pwm2_start_output.dev_attr.attr,
2362                 &sensor_dev_attr_pwm2_stop_output.dev_attr.attr,
2363                 &sensor_dev_attr_pwm2_weight_temp_sel.dev_attr.attr,
2364                 &sensor_dev_attr_pwm2_weight_temp_step.dev_attr.attr,
2365                 &sensor_dev_attr_pwm2_weight_temp_step_tol.dev_attr.attr,
2366                 &sensor_dev_attr_pwm2_weight_temp_step_base.dev_attr.attr,
2367                 &sensor_dev_attr_pwm2_weight_duty_step.dev_attr.attr,
2368                 NULL
2369         },
2370         {
2371                 &sensor_dev_attr_temp13_input.dev_attr.attr,
2372                 &sensor_dev_attr_pwm3.dev_attr.attr,
2373                 &sensor_dev_attr_pwm3_mode.dev_attr.attr,
2374                 &sensor_dev_attr_pwm3_enable.dev_attr.attr,
2375                 &sensor_dev_attr_pwm3_stop_output_enable.dev_attr.attr,
2376                 &sensor_dev_attr_pwm3_weight_enable.dev_attr.attr,
2377                 &sensor_dev_attr_pwm3_temp_sel.dev_attr.attr,
2378                 &sensor_dev_attr_pwm3_target.dev_attr.attr,
2379                 &sensor_dev_attr_pwm3_stop_time.dev_attr.attr,
2380                 &sensor_dev_attr_pwm3_step_up_time.dev_attr.attr,
2381                 &sensor_dev_attr_pwm3_step_down_time.dev_attr.attr,
2382                 &sensor_dev_attr_pwm3_start_output.dev_attr.attr,
2383                 &sensor_dev_attr_pwm3_stop_output.dev_attr.attr,
2384                 &sensor_dev_attr_pwm3_weight_temp_sel.dev_attr.attr,
2385                 &sensor_dev_attr_pwm3_weight_temp_step.dev_attr.attr,
2386                 &sensor_dev_attr_pwm3_weight_temp_step_tol.dev_attr.attr,
2387                 &sensor_dev_attr_pwm3_weight_temp_step_base.dev_attr.attr,
2388                 &sensor_dev_attr_pwm3_weight_duty_step.dev_attr.attr,
2389                 NULL
2390         },
2391         {
2392                 &sensor_dev_attr_temp14_input.dev_attr.attr,
2393                 &sensor_dev_attr_pwm4.dev_attr.attr,
2394                 &sensor_dev_attr_pwm4_mode.dev_attr.attr,
2395                 &sensor_dev_attr_pwm4_enable.dev_attr.attr,
2396                 &sensor_dev_attr_pwm4_stop_output_enable.dev_attr.attr,
2397                 &sensor_dev_attr_pwm4_weight_enable.dev_attr.attr,
2398                 &sensor_dev_attr_pwm4_temp_sel.dev_attr.attr,
2399                 &sensor_dev_attr_pwm4_target.dev_attr.attr,
2400                 &sensor_dev_attr_pwm4_stop_time.dev_attr.attr,
2401                 &sensor_dev_attr_pwm4_step_up_time.dev_attr.attr,
2402                 &sensor_dev_attr_pwm4_step_down_time.dev_attr.attr,
2403                 &sensor_dev_attr_pwm4_start_output.dev_attr.attr,
2404                 &sensor_dev_attr_pwm4_stop_output.dev_attr.attr,
2405                 &sensor_dev_attr_pwm4_weight_temp_sel.dev_attr.attr,
2406                 &sensor_dev_attr_pwm4_weight_temp_step.dev_attr.attr,
2407                 &sensor_dev_attr_pwm4_weight_temp_step_tol.dev_attr.attr,
2408                 &sensor_dev_attr_pwm4_weight_temp_step_base.dev_attr.attr,
2409                 &sensor_dev_attr_pwm4_weight_duty_step.dev_attr.attr,
2410                 NULL
2411         },
2412         {
2413                 &sensor_dev_attr_temp15_input.dev_attr.attr,
2414                 &sensor_dev_attr_pwm5.dev_attr.attr,
2415                 &sensor_dev_attr_pwm5_mode.dev_attr.attr,
2416                 &sensor_dev_attr_pwm5_enable.dev_attr.attr,
2417                 &sensor_dev_attr_pwm5_stop_output_enable.dev_attr.attr,
2418                 &sensor_dev_attr_pwm5_weight_enable.dev_attr.attr,
2419                 &sensor_dev_attr_pwm5_temp_sel.dev_attr.attr,
2420                 &sensor_dev_attr_pwm5_target.dev_attr.attr,
2421                 &sensor_dev_attr_pwm5_stop_time.dev_attr.attr,
2422                 &sensor_dev_attr_pwm5_step_up_time.dev_attr.attr,
2423                 &sensor_dev_attr_pwm5_step_down_time.dev_attr.attr,
2424                 &sensor_dev_attr_pwm5_start_output.dev_attr.attr,
2425                 &sensor_dev_attr_pwm5_stop_output.dev_attr.attr,
2426                 &sensor_dev_attr_pwm5_weight_temp_sel.dev_attr.attr,
2427                 &sensor_dev_attr_pwm5_weight_temp_step.dev_attr.attr,
2428                 &sensor_dev_attr_pwm5_weight_temp_step_tol.dev_attr.attr,
2429                 &sensor_dev_attr_pwm5_weight_temp_step_base.dev_attr.attr,
2430                 &sensor_dev_attr_pwm5_weight_duty_step.dev_attr.attr,
2431                 NULL
2432         },
2433 };
2434
2435 static const struct attribute_group nct6775_group_pwm[5] = {
2436         { .attrs = nct6775_attributes_pwm[0] },
2437         { .attrs = nct6775_attributes_pwm[1] },
2438         { .attrs = nct6775_attributes_pwm[2] },
2439         { .attrs = nct6775_attributes_pwm[3] },
2440         { .attrs = nct6775_attributes_pwm[4] },
2441 };
2442
2443 static ssize_t
2444 show_auto_pwm(struct device *dev, struct device_attribute *attr, char *buf)
2445 {
2446         struct nct6775_data *data = nct6775_update_device(dev);
2447         struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
2448
2449         return sprintf(buf, "%d\n", data->auto_pwm[sattr->nr][sattr->index]);
2450 }
2451
2452 static ssize_t
2453 store_auto_pwm(struct device *dev, struct device_attribute *attr,
2454                const char *buf, size_t count)
2455 {
2456         struct nct6775_data *data = dev_get_drvdata(dev);
2457         struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
2458         int nr = sattr->nr;
2459         int point = sattr->index;
2460         unsigned long val;
2461         int err;
2462         u8 reg;
2463
2464         err = kstrtoul(buf, 10, &val);
2465         if (err < 0)
2466                 return err;
2467         if (val > 255)
2468                 return -EINVAL;
2469
2470         if (point == data->auto_pwm_num) {
2471                 if (data->kind != nct6775 && !val)
2472                         return -EINVAL;
2473                 if (data->kind != nct6779 && val)
2474                         val = 0xff;
2475         }
2476
2477         mutex_lock(&data->update_lock);
2478         data->auto_pwm[nr][point] = val;
2479         if (point < data->auto_pwm_num) {
2480                 nct6775_write_value(data,
2481                                     NCT6775_AUTO_PWM(data, nr, point),
2482                                     data->auto_pwm[nr][point]);
2483         } else {
2484                 switch (data->kind) {
2485                 case nct6775:
2486                         /* disable if needed (pwm == 0) */
2487                         reg = nct6775_read_value(data,
2488                                                  NCT6775_REG_CRITICAL_ENAB[nr]);
2489                         if (val)
2490                                 reg |= 0x02;
2491                         else
2492                                 reg &= ~0x02;
2493                         nct6775_write_value(data, NCT6775_REG_CRITICAL_ENAB[nr],
2494                                             reg);
2495                         break;
2496                 case nct6776:
2497                         break; /* always enabled, nothing to do */
2498                 case nct6779:
2499                         nct6775_write_value(data, NCT6779_REG_CRITICAL_PWM[nr],
2500                                             val);
2501                         reg = nct6775_read_value(data,
2502                                         NCT6779_REG_CRITICAL_PWM_ENABLE[nr]);
2503                         if (val == 255)
2504                                 reg &= ~0x01;
2505                         else
2506                                 reg |= 0x01;
2507                         nct6775_write_value(data,
2508                                             NCT6779_REG_CRITICAL_PWM_ENABLE[nr],
2509                                             reg);
2510                         break;
2511                 }
2512         }
2513         mutex_unlock(&data->update_lock);
2514         return count;
2515 }
2516
2517 static ssize_t
2518 show_auto_temp(struct device *dev, struct device_attribute *attr, char *buf)
2519 {
2520         struct nct6775_data *data = nct6775_update_device(dev);
2521         struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
2522         int nr = sattr->nr;
2523         int point = sattr->index;
2524
2525         /*
2526          * We don't know for sure if the temperature is signed or unsigned.
2527          * Assume it is unsigned.
2528          */
2529         return sprintf(buf, "%d\n", data->auto_temp[nr][point] * 1000);
2530 }
2531
2532 static ssize_t
2533 store_auto_temp(struct device *dev, struct device_attribute *attr,
2534                 const char *buf, size_t count)
2535 {
2536         struct nct6775_data *data = dev_get_drvdata(dev);
2537         struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
2538         int nr = sattr->nr;
2539         int point = sattr->index;
2540         unsigned long val;
2541         int err;
2542
2543         err = kstrtoul(buf, 10, &val);
2544         if (err)
2545                 return err;
2546         if (val > 255000)
2547                 return -EINVAL;
2548
2549         mutex_lock(&data->update_lock);
2550         data->auto_temp[nr][point] = DIV_ROUND_CLOSEST(val, 1000);
2551         if (point < data->auto_pwm_num) {
2552                 nct6775_write_value(data,
2553                                     NCT6775_AUTO_TEMP(data, nr, point),
2554                                     data->auto_temp[nr][point]);
2555         } else {
2556                 nct6775_write_value(data, data->REG_CRITICAL_TEMP[nr],
2557                                     data->auto_temp[nr][point]);
2558         }
2559         mutex_unlock(&data->update_lock);
2560         return count;
2561 }
2562
2563 /*
2564  * The number of auto-point trip points is chip dependent.
2565  * Need to check support while generating/removing attribute files.
2566  */
2567 static struct sensor_device_attribute_2 sda_auto_pwm_arrays[] = {
2568         SENSOR_ATTR_2(pwm1_auto_point1_pwm, S_IWUSR | S_IRUGO,
2569                           show_auto_pwm, store_auto_pwm, 0, 0),
2570         SENSOR_ATTR_2(pwm1_auto_point1_temp, S_IWUSR | S_IRUGO,
2571                           show_auto_temp, store_auto_temp, 0, 0),
2572         SENSOR_ATTR_2(pwm1_auto_point1_temp_hyst, S_IWUSR | S_IRUGO,
2573                           show_auto_temp_hyst, store_auto_temp_hyst, 0, 0),
2574         SENSOR_ATTR_2(pwm1_auto_point2_pwm, S_IWUSR | S_IRUGO,
2575                           show_auto_pwm, store_auto_pwm, 0, 1),
2576         SENSOR_ATTR_2(pwm1_auto_point2_temp, S_IWUSR | S_IRUGO,
2577                           show_auto_temp, store_auto_temp, 0, 1),
2578         SENSOR_ATTR_2(pwm1_auto_point2_temp_hyst, S_IWUSR | S_IRUGO,
2579                           show_auto_temp_hyst, store_auto_temp_hyst, 0, 1),
2580         SENSOR_ATTR_2(pwm1_auto_point3_pwm, S_IWUSR | S_IRUGO,
2581                           show_auto_pwm, store_auto_pwm, 0, 2),
2582         SENSOR_ATTR_2(pwm1_auto_point3_temp, S_IWUSR | S_IRUGO,
2583                           show_auto_temp, store_auto_temp, 0, 2),
2584         SENSOR_ATTR_2(pwm1_auto_point3_temp_hyst, S_IWUSR | S_IRUGO,
2585                           show_auto_temp_hyst, store_auto_temp_hyst, 0, 2),
2586         SENSOR_ATTR_2(pwm1_auto_point4_pwm, S_IWUSR | S_IRUGO,
2587                           show_auto_pwm, store_auto_pwm, 0, 3),
2588         SENSOR_ATTR_2(pwm1_auto_point4_temp, S_IWUSR | S_IRUGO,
2589                           show_auto_temp, store_auto_temp, 0, 3),
2590         SENSOR_ATTR_2(pwm1_auto_point4_temp_hyst, S_IWUSR | S_IRUGO,
2591                           show_auto_temp_hyst, store_auto_temp_hyst, 0, 3),
2592         SENSOR_ATTR_2(pwm1_auto_point5_pwm, S_IWUSR | S_IRUGO,
2593                           show_auto_pwm, store_auto_pwm, 0, 4),
2594         SENSOR_ATTR_2(pwm1_auto_point5_temp, S_IWUSR | S_IRUGO,
2595                           show_auto_temp, store_auto_temp, 0, 4),
2596         SENSOR_ATTR_2(pwm1_auto_point5_temp_hyst, S_IWUSR | S_IRUGO,
2597                           show_auto_temp_hyst, store_auto_temp_hyst, 0, 4),
2598         SENSOR_ATTR_2(pwm1_auto_point6_pwm, S_IWUSR | S_IRUGO,
2599                           show_auto_pwm, store_auto_pwm, 0, 5),
2600         SENSOR_ATTR_2(pwm1_auto_point6_temp, S_IWUSR | S_IRUGO,
2601                           show_auto_temp, store_auto_temp, 0, 5),
2602         SENSOR_ATTR_2(pwm1_auto_point6_temp_hyst, S_IWUSR | S_IRUGO,
2603                           show_auto_temp_hyst, store_auto_temp_hyst, 0, 5),
2604         SENSOR_ATTR_2(pwm1_auto_point7_pwm, S_IWUSR | S_IRUGO,
2605                           show_auto_pwm, store_auto_pwm, 0, 6),
2606         SENSOR_ATTR_2(pwm1_auto_point7_temp, S_IWUSR | S_IRUGO,
2607                           show_auto_temp, store_auto_temp, 0, 6),
2608         SENSOR_ATTR_2(pwm1_auto_point7_temp_hyst, S_IWUSR | S_IRUGO,
2609                           show_auto_temp_hyst, store_auto_temp_hyst, 0, 6),
2610
2611         SENSOR_ATTR_2(pwm2_auto_point1_pwm, S_IWUSR | S_IRUGO,
2612                           show_auto_pwm, store_auto_pwm, 1, 0),
2613         SENSOR_ATTR_2(pwm2_auto_point1_temp, S_IWUSR | S_IRUGO,
2614                           show_auto_temp, store_auto_temp, 1, 0),
2615         SENSOR_ATTR_2(pwm2_auto_point1_temp_hyst, S_IWUSR | S_IRUGO,
2616                           show_auto_temp_hyst, store_auto_temp_hyst, 1, 0),
2617         SENSOR_ATTR_2(pwm2_auto_point2_pwm, S_IWUSR | S_IRUGO,
2618                           show_auto_pwm, store_auto_pwm, 1, 1),
2619         SENSOR_ATTR_2(pwm2_auto_point2_temp, S_IWUSR | S_IRUGO,
2620                           show_auto_temp, store_auto_temp, 1, 1),
2621         SENSOR_ATTR_2(pwm2_auto_point2_temp_hyst, S_IWUSR | S_IRUGO,
2622                           show_auto_temp_hyst, store_auto_temp_hyst, 1, 1),
2623         SENSOR_ATTR_2(pwm2_auto_point3_pwm, S_IWUSR | S_IRUGO,
2624                           show_auto_pwm, store_auto_pwm, 1, 2),
2625         SENSOR_ATTR_2(pwm2_auto_point3_temp, S_IWUSR | S_IRUGO,
2626                           show_auto_temp, store_auto_temp, 1, 2),
2627         SENSOR_ATTR_2(pwm2_auto_point3_temp_hyst, S_IWUSR | S_IRUGO,
2628                           show_auto_temp_hyst, store_auto_temp_hyst, 1, 2),
2629         SENSOR_ATTR_2(pwm2_auto_point4_pwm, S_IWUSR | S_IRUGO,
2630                           show_auto_pwm, store_auto_pwm, 1, 3),
2631         SENSOR_ATTR_2(pwm2_auto_point4_temp, S_IWUSR | S_IRUGO,
2632                           show_auto_temp, store_auto_temp, 1, 3),
2633         SENSOR_ATTR_2(pwm2_auto_point4_temp_hyst, S_IWUSR | S_IRUGO,
2634                           show_auto_temp_hyst, store_auto_temp_hyst, 1, 3),
2635         SENSOR_ATTR_2(pwm2_auto_point5_pwm, S_IWUSR | S_IRUGO,
2636                           show_auto_pwm, store_auto_pwm, 1, 4),
2637         SENSOR_ATTR_2(pwm2_auto_point5_temp, S_IWUSR | S_IRUGO,
2638                           show_auto_temp, store_auto_temp, 1, 4),
2639         SENSOR_ATTR_2(pwm2_auto_point5_temp_hyst, S_IWUSR | S_IRUGO,
2640                           show_auto_temp_hyst, store_auto_temp_hyst, 1, 4),
2641         SENSOR_ATTR_2(pwm2_auto_point6_pwm, S_IWUSR | S_IRUGO,
2642                           show_auto_pwm, store_auto_pwm, 1, 5),
2643         SENSOR_ATTR_2(pwm2_auto_point6_temp, S_IWUSR | S_IRUGO,
2644                           show_auto_temp, store_auto_temp, 1, 5),
2645         SENSOR_ATTR_2(pwm2_auto_point6_temp_hyst, S_IWUSR | S_IRUGO,
2646                           show_auto_temp_hyst, store_auto_temp_hyst, 1, 5),
2647         SENSOR_ATTR_2(pwm2_auto_point7_pwm, S_IWUSR | S_IRUGO,
2648                           show_auto_pwm, store_auto_pwm, 1, 6),
2649         SENSOR_ATTR_2(pwm2_auto_point7_temp, S_IWUSR | S_IRUGO,
2650                           show_auto_temp, store_auto_temp, 1, 6),
2651         SENSOR_ATTR_2(pwm2_auto_point7_temp_hyst, S_IWUSR | S_IRUGO,
2652                           show_auto_temp_hyst, store_auto_temp_hyst, 1, 6),
2653
2654         SENSOR_ATTR_2(pwm3_auto_point1_pwm, S_IWUSR | S_IRUGO,
2655                           show_auto_pwm, store_auto_pwm, 2, 0),
2656         SENSOR_ATTR_2(pwm3_auto_point1_temp, S_IWUSR | S_IRUGO,
2657                           show_auto_temp, store_auto_temp, 2, 0),
2658         SENSOR_ATTR_2(pwm3_auto_point1_temp_hyst, S_IWUSR | S_IRUGO,
2659                           show_auto_temp_hyst, store_auto_temp_hyst, 2, 0),
2660         SENSOR_ATTR_2(pwm3_auto_point2_pwm, S_IWUSR | S_IRUGO,
2661                           show_auto_pwm, store_auto_pwm, 2, 1),
2662         SENSOR_ATTR_2(pwm3_auto_point2_temp, S_IWUSR | S_IRUGO,
2663                           show_auto_temp, store_auto_temp, 2, 1),
2664         SENSOR_ATTR_2(pwm3_auto_point2_temp_hyst, S_IWUSR | S_IRUGO,
2665                           show_auto_temp_hyst, store_auto_temp_hyst, 2, 1),
2666         SENSOR_ATTR_2(pwm3_auto_point3_pwm, S_IWUSR | S_IRUGO,
2667                           show_auto_pwm, store_auto_pwm, 2, 2),
2668         SENSOR_ATTR_2(pwm3_auto_point3_temp, S_IWUSR | S_IRUGO,
2669                           show_auto_temp, store_auto_temp, 2, 2),
2670         SENSOR_ATTR_2(pwm3_auto_point3_temp_hyst, S_IWUSR | S_IRUGO,
2671                           show_auto_temp_hyst, store_auto_temp_hyst, 2, 2),
2672         SENSOR_ATTR_2(pwm3_auto_point4_pwm, S_IWUSR | S_IRUGO,
2673                           show_auto_pwm, store_auto_pwm, 2, 3),
2674         SENSOR_ATTR_2(pwm3_auto_point4_temp, S_IWUSR | S_IRUGO,
2675                           show_auto_temp, store_auto_temp, 2, 3),
2676         SENSOR_ATTR_2(pwm3_auto_point4_temp_hyst, S_IWUSR | S_IRUGO,
2677                           show_auto_temp_hyst, store_auto_temp_hyst, 2, 3),
2678         SENSOR_ATTR_2(pwm3_auto_point5_pwm, S_IWUSR | S_IRUGO,
2679                           show_auto_pwm, store_auto_pwm, 2, 4),
2680         SENSOR_ATTR_2(pwm3_auto_point5_temp, S_IWUSR | S_IRUGO,
2681                           show_auto_temp, store_auto_temp, 2, 4),
2682         SENSOR_ATTR_2(pwm3_auto_point5_temp_hyst, S_IWUSR | S_IRUGO,
2683                           show_auto_temp_hyst, store_auto_temp_hyst, 2, 4),
2684         SENSOR_ATTR_2(pwm3_auto_point6_pwm, S_IWUSR | S_IRUGO,
2685                           show_auto_pwm, store_auto_pwm, 2, 5),
2686         SENSOR_ATTR_2(pwm3_auto_point6_temp, S_IWUSR | S_IRUGO,
2687                           show_auto_temp, store_auto_temp, 2, 5),
2688         SENSOR_ATTR_2(pwm3_auto_point6_temp_hyst, S_IWUSR | S_IRUGO,
2689                           show_auto_temp_hyst, store_auto_temp_hyst, 2, 5),
2690         SENSOR_ATTR_2(pwm3_auto_point7_pwm, S_IWUSR | S_IRUGO,
2691                           show_auto_pwm, store_auto_pwm, 2, 6),
2692         SENSOR_ATTR_2(pwm3_auto_point7_temp, S_IWUSR | S_IRUGO,
2693                           show_auto_temp, store_auto_temp, 2, 6),
2694         SENSOR_ATTR_2(pwm3_auto_point7_temp_hyst, S_IWUSR | S_IRUGO,
2695                           show_auto_temp_hyst, store_auto_temp_hyst, 2, 6),
2696
2697         SENSOR_ATTR_2(pwm4_auto_point1_pwm, S_IWUSR | S_IRUGO,
2698                           show_auto_pwm, store_auto_pwm, 3, 0),
2699         SENSOR_ATTR_2(pwm4_auto_point1_temp, S_IWUSR | S_IRUGO,
2700                           show_auto_temp, store_auto_temp, 3, 0),
2701         SENSOR_ATTR_2(pwm4_auto_point1_temp_hyst, S_IWUSR | S_IRUGO,
2702                           show_auto_temp_hyst, store_auto_temp_hyst, 3, 0),
2703         SENSOR_ATTR_2(pwm4_auto_point2_pwm, S_IWUSR | S_IRUGO,
2704                           show_auto_pwm, store_auto_pwm, 3, 1),
2705         SENSOR_ATTR_2(pwm4_auto_point2_temp, S_IWUSR | S_IRUGO,
2706                           show_auto_temp, store_auto_temp, 3, 1),
2707         SENSOR_ATTR_2(pwm4_auto_point2_temp_hyst, S_IWUSR | S_IRUGO,
2708                           show_auto_temp_hyst, store_auto_temp_hyst, 3, 1),
2709         SENSOR_ATTR_2(pwm4_auto_point3_pwm, S_IWUSR | S_IRUGO,
2710                           show_auto_pwm, store_auto_pwm, 3, 2),
2711         SENSOR_ATTR_2(pwm4_auto_point3_temp, S_IWUSR | S_IRUGO,
2712                           show_auto_temp, store_auto_temp, 3, 2),
2713         SENSOR_ATTR_2(pwm4_auto_point3_temp_hyst, S_IWUSR | S_IRUGO,
2714                           show_auto_temp_hyst, store_auto_temp_hyst, 3, 2),
2715         SENSOR_ATTR_2(pwm4_auto_point4_pwm, S_IWUSR | S_IRUGO,
2716                           show_auto_pwm, store_auto_pwm, 3, 3),
2717         SENSOR_ATTR_2(pwm4_auto_point4_temp, S_IWUSR | S_IRUGO,
2718                           show_auto_temp, store_auto_temp, 3, 3),
2719         SENSOR_ATTR_2(pwm4_auto_point4_temp_hyst, S_IWUSR | S_IRUGO,
2720                           show_auto_temp_hyst, store_auto_temp_hyst, 3, 3),
2721         SENSOR_ATTR_2(pwm4_auto_point5_pwm, S_IWUSR | S_IRUGO,
2722                           show_auto_pwm, store_auto_pwm, 3, 4),
2723         SENSOR_ATTR_2(pwm4_auto_point5_temp, S_IWUSR | S_IRUGO,
2724                           show_auto_temp, store_auto_temp, 3, 4),
2725         SENSOR_ATTR_2(pwm4_auto_point5_temp_hyst, S_IWUSR | S_IRUGO,
2726                           show_auto_temp_hyst, store_auto_temp_hyst, 3, 4),
2727         SENSOR_ATTR_2(pwm4_auto_point6_pwm, S_IWUSR | S_IRUGO,
2728                           show_auto_pwm, store_auto_pwm, 3, 5),
2729         SENSOR_ATTR_2(pwm4_auto_point6_temp, S_IWUSR | S_IRUGO,
2730                           show_auto_temp, store_auto_temp, 3, 5),
2731         SENSOR_ATTR_2(pwm4_auto_point6_temp_hyst, S_IWUSR | S_IRUGO,
2732                           show_auto_temp_hyst, store_auto_temp_hyst, 3, 5),
2733         SENSOR_ATTR_2(pwm4_auto_point7_pwm, S_IWUSR | S_IRUGO,
2734                           show_auto_pwm, store_auto_pwm, 3, 6),
2735         SENSOR_ATTR_2(pwm4_auto_point7_temp, S_IWUSR | S_IRUGO,
2736                           show_auto_temp, store_auto_temp, 3, 6),
2737         SENSOR_ATTR_2(pwm4_auto_point7_temp_hyst, S_IWUSR | S_IRUGO,
2738                           show_auto_temp_hyst, store_auto_temp_hyst, 3, 6),
2739
2740         SENSOR_ATTR_2(pwm5_auto_point1_pwm, S_IWUSR | S_IRUGO,
2741                           show_auto_pwm, store_auto_pwm, 4, 0),
2742         SENSOR_ATTR_2(pwm5_auto_point1_temp, S_IWUSR | S_IRUGO,
2743                           show_auto_temp, store_auto_temp, 4, 0),
2744         SENSOR_ATTR_2(pwm5_auto_point1_temp_hyst, S_IWUSR | S_IRUGO,
2745                           show_auto_temp_hyst, store_auto_temp_hyst, 4, 0),
2746         SENSOR_ATTR_2(pwm5_auto_point2_pwm, S_IWUSR | S_IRUGO,
2747                           show_auto_pwm, store_auto_pwm, 4, 1),
2748         SENSOR_ATTR_2(pwm5_auto_point2_temp, S_IWUSR | S_IRUGO,
2749                           show_auto_temp, store_auto_temp, 4, 1),
2750         SENSOR_ATTR_2(pwm5_auto_point2_temp_hyst, S_IWUSR | S_IRUGO,
2751                           show_auto_temp_hyst, store_auto_temp_hyst, 4, 1),
2752         SENSOR_ATTR_2(pwm5_auto_point3_pwm, S_IWUSR | S_IRUGO,
2753                           show_auto_pwm, store_auto_pwm, 4, 2),
2754         SENSOR_ATTR_2(pwm5_auto_point3_temp, S_IWUSR | S_IRUGO,
2755                           show_auto_temp, store_auto_temp, 4, 2),
2756         SENSOR_ATTR_2(pwm5_auto_point3_temp_hyst, S_IWUSR | S_IRUGO,
2757                           show_auto_temp_hyst, store_auto_temp_hyst, 4, 2),
2758         SENSOR_ATTR_2(pwm5_auto_point4_pwm, S_IWUSR | S_IRUGO,
2759                           show_auto_pwm, store_auto_pwm, 4, 3),
2760         SENSOR_ATTR_2(pwm5_auto_point4_temp, S_IWUSR | S_IRUGO,
2761                           show_auto_temp, store_auto_temp, 4, 3),
2762         SENSOR_ATTR_2(pwm5_auto_point4_temp_hyst, S_IWUSR | S_IRUGO,
2763                           show_auto_temp_hyst, store_auto_temp_hyst, 4, 3),
2764         SENSOR_ATTR_2(pwm5_auto_point5_pwm, S_IWUSR | S_IRUGO,
2765                           show_auto_pwm, store_auto_pwm, 4, 4),
2766         SENSOR_ATTR_2(pwm5_auto_point5_temp, S_IWUSR | S_IRUGO,
2767                           show_auto_temp, store_auto_temp, 4, 4),
2768         SENSOR_ATTR_2(pwm5_auto_point5_temp_hyst, S_IWUSR | S_IRUGO,
2769                           show_auto_temp_hyst, store_auto_temp_hyst, 4, 4),
2770         SENSOR_ATTR_2(pwm5_auto_point6_pwm, S_IWUSR | S_IRUGO,
2771                           show_auto_pwm, store_auto_pwm, 4, 5),
2772         SENSOR_ATTR_2(pwm5_auto_point6_temp, S_IWUSR | S_IRUGO,
2773                           show_auto_temp, store_auto_temp, 4, 5),
2774         SENSOR_ATTR_2(pwm5_auto_point6_temp_hyst, S_IWUSR | S_IRUGO,
2775                           show_auto_temp_hyst, store_auto_temp_hyst, 4, 5),
2776         SENSOR_ATTR_2(pwm5_auto_point7_pwm, S_IWUSR | S_IRUGO,
2777                           show_auto_pwm, store_auto_pwm, 4, 6),
2778         SENSOR_ATTR_2(pwm5_auto_point7_temp, S_IWUSR | S_IRUGO,
2779                           show_auto_temp, store_auto_temp, 4, 6),
2780         SENSOR_ATTR_2(pwm5_auto_point7_temp_hyst, S_IWUSR | S_IRUGO,
2781                           show_auto_temp_hyst, store_auto_temp_hyst, 4, 6),
2782 };
2783
2784 static ssize_t
2785 show_vid(struct device *dev, struct device_attribute *attr, char *buf)
2786 {
2787         struct nct6775_data *data = dev_get_drvdata(dev);
2788         return sprintf(buf, "%d\n", vid_from_reg(data->vid, data->vrm));
2789 }
2790
2791 static DEVICE_ATTR(cpu0_vid, S_IRUGO, show_vid, NULL);
2792
2793 /* Case open detection */
2794
2795 static ssize_t
2796 show_caseopen(struct device *dev, struct device_attribute *attr, char *buf)
2797 {
2798         struct nct6775_data *data = nct6775_update_device(dev);
2799
2800         return sprintf(buf, "%d\n",
2801                 !!(data->caseopen & to_sensor_dev_attr_2(attr)->index));
2802 }
2803
2804 static ssize_t
2805 clear_caseopen(struct device *dev, struct device_attribute *attr,
2806                const char *buf, size_t count)
2807 {
2808         struct nct6775_data *data = dev_get_drvdata(dev);
2809         struct nct6775_sio_data *sio_data = dev->platform_data;
2810         int nr = to_sensor_dev_attr(attr)->index;
2811         unsigned long val;
2812         u8 reg;
2813
2814         if (kstrtoul(buf, 10, &val) || val != 0)
2815                 return -EINVAL;
2816
2817         mutex_lock(&data->update_lock);
2818
2819         /*
2820          * Use CR registers to clear caseopen status.
2821          * The CR registers are the same for all chips, and not all chips
2822          * support clearing the caseopen status through "regular" registers.
2823          */
2824         superio_enter(sio_data->sioreg);
2825         superio_select(sio_data->sioreg, NCT6775_LD_ACPI);
2826         reg = superio_inb(sio_data->sioreg, NCT6775_REG_CR_CASEOPEN_CLR[nr]);
2827         reg |= NCT6775_CR_CASEOPEN_CLR_MASK[nr];
2828         superio_outb(sio_data->sioreg, NCT6775_REG_CR_CASEOPEN_CLR[nr], reg);
2829         superio_exit(sio_data->sioreg);
2830
2831         data->valid = 0;        /* Force cache refresh */
2832
2833         mutex_unlock(&data->update_lock);
2834
2835         return count;
2836 }
2837
2838 static struct sensor_device_attribute sda_caseopen[] = {
2839         SENSOR_ATTR(intrusion0_alarm, S_IWUSR | S_IRUGO, show_caseopen,
2840                     clear_caseopen, 0),
2841         SENSOR_ATTR(intrusion1_alarm, S_IWUSR | S_IRUGO, show_caseopen,
2842                     clear_caseopen, 1),
2843 };
2844
2845 /*
2846  * Driver and device management
2847  */
2848
2849 static void nct6775_device_remove_files(struct device *dev)
2850 {
2851         /*
2852          * some entries in the following arrays may not have been used in
2853          * device_create_file(), but device_remove_file() will ignore them
2854          */
2855         int i;
2856         struct nct6775_data *data = dev_get_drvdata(dev);
2857
2858         for (i = 0; i < data->pwm_num; i++)
2859                 sysfs_remove_group(&dev->kobj, &nct6775_group_pwm[i]);
2860
2861         for (i = 0; i < ARRAY_SIZE(sda_max_output); i++)
2862                 device_remove_file(dev, &sda_max_output[i].dev_attr);
2863
2864         for (i = 0; i < ARRAY_SIZE(sda_step_output); i++)
2865                 device_remove_file(dev, &sda_max_output[i].dev_attr);
2866
2867         for (i = 0; i < ARRAY_SIZE(sda_weight_duty_base); i++)
2868                 device_remove_file(dev, &sda_weight_duty_base[i].dev_attr);
2869
2870         for (i = 0; i < ARRAY_SIZE(sda_auto_pwm_arrays); i++)
2871                 device_remove_file(dev, &sda_auto_pwm_arrays[i].dev_attr);
2872
2873         for (i = 0; i < data->in_num; i++)
2874                 sysfs_remove_group(&dev->kobj, &nct6775_group_in[i]);
2875
2876         for (i = 0; i < 5; i++) {
2877                 device_remove_file(dev, &sda_fan_input[i].dev_attr);
2878                 device_remove_file(dev, &sda_fan_alarm[i].dev_attr);
2879                 device_remove_file(dev, &sda_fan_div[i].dev_attr);
2880                 device_remove_file(dev, &sda_fan_min[i].dev_attr);
2881         }
2882         for (i = 0; i < NUM_TEMP; i++) {
2883                 if (!(data->have_temp & (1 << i)))
2884                         continue;
2885                 device_remove_file(dev, &sda_temp_input[i].dev_attr);
2886                 device_remove_file(dev, &sda_temp_label[i].dev_attr);
2887                 device_remove_file(dev, &sda_temp_max[i].dev_attr);
2888                 device_remove_file(dev, &sda_temp_max_hyst[i].dev_attr);
2889                 if (!(data->have_temp_fixed & (1 << i)))
2890                         continue;
2891                 device_remove_file(dev, &sda_temp_type[i].dev_attr);
2892                 device_remove_file(dev, &sda_temp_offset[i].dev_attr);
2893                 if (i >= NUM_TEMP_ALARM)
2894                         continue;
2895                 device_remove_file(dev, &sda_temp_alarm[i].dev_attr);
2896         }
2897
2898         device_remove_file(dev, &sda_caseopen[0].dev_attr);
2899         device_remove_file(dev, &sda_caseopen[1].dev_attr);
2900
2901         device_remove_file(dev, &dev_attr_name);
2902         device_remove_file(dev, &dev_attr_cpu0_vid);
2903 }
2904
2905 /* Get the monitoring functions started */
2906 static inline void __devinit nct6775_init_device(struct nct6775_data *data)
2907 {
2908         int i;
2909         u8 tmp, diode;
2910
2911         /* Start monitoring if needed */
2912         if (data->REG_CONFIG) {
2913                 tmp = nct6775_read_value(data, data->REG_CONFIG);
2914                 if (!(tmp & 0x01))
2915                         nct6775_write_value(data, data->REG_CONFIG, tmp | 0x01);
2916         }
2917
2918         /* Enable temperature sensors if needed */
2919         for (i = 0; i < NUM_TEMP; i++) {
2920                 if (!(data->have_temp & (1 << i)))
2921                         continue;
2922                 if (!data->reg_temp_config[i])
2923                         continue;
2924                 tmp = nct6775_read_value(data, data->reg_temp_config[i]);
2925                 if (tmp & 0x01)
2926                         nct6775_write_value(data, data->reg_temp_config[i],
2927                                             tmp & 0xfe);
2928         }
2929
2930         /* Enable VBAT monitoring if needed */
2931         tmp = nct6775_read_value(data, data->REG_VBAT);
2932         if (!(tmp & 0x01))
2933                 nct6775_write_value(data, data->REG_VBAT, tmp | 0x01);
2934
2935         diode = nct6775_read_value(data, data->REG_DIODE);
2936
2937         for (i = 0; i < data->temp_fixed_num; i++) {
2938                 if (!(data->have_temp_fixed & (1 << i)))
2939                         continue;
2940                 if ((tmp & (0x02 << i)))        /* diode */
2941                         data->temp_type[i] = 3 - ((diode >> i) & 0x02);
2942                 else                            /* thermistor */
2943                         data->temp_type[i] = 4;
2944         }
2945 }
2946
2947 static void __devinit
2948 nct6775_check_fan_inputs(const struct nct6775_sio_data *sio_data,
2949                          struct nct6775_data *data)
2950 {
2951         int regval;
2952         bool fan3pin, fan3min, fan4pin, fan4min, fan5pin;
2953         bool pwm3pin, pwm4pin, pwm5pin;
2954
2955         superio_enter(sio_data->sioreg);
2956
2957         /* fan4 and fan5 share some pins with the GPIO and serial flash */
2958         if (data->kind == nct6775) {
2959                 regval = superio_inb(sio_data->sioreg, 0x2c);
2960
2961                 fan3pin = regval & (1 << 6);
2962                 fan3min = fan3pin;
2963                 pwm3pin = regval & (1 << 7);
2964
2965                 /* On NCT6775, fan4 shares pins with the fdc interface */
2966                 fan4pin = !(superio_inb(sio_data->sioreg, 0x2A) & 0x80);
2967                 fan4min = 0;
2968                 fan5pin = 0;
2969                 pwm4pin = 0;
2970                 pwm5pin = 0;
2971         } else if (data->kind == nct6776) {
2972                 bool gpok = superio_inb(sio_data->sioreg, 0x27) & 0x80;
2973
2974                 superio_select(sio_data->sioreg, NCT6775_LD_HWM);
2975                 regval = superio_inb(sio_data->sioreg, SIO_REG_ENABLE);
2976
2977                 if (regval & 0x80)
2978                         fan3pin = gpok;
2979                 else
2980                         fan3pin = !(superio_inb(sio_data->sioreg, 0x24) & 0x40);
2981
2982                 if (regval & 0x40)
2983                         fan4pin = gpok;
2984                 else
2985                         fan4pin = superio_inb(sio_data->sioreg, 0x1C) & 0x01;
2986
2987                 if (regval & 0x20)
2988                         fan5pin = gpok;
2989                 else
2990                         fan5pin = superio_inb(sio_data->sioreg, 0x1C) & 0x02;
2991
2992                 fan4min = fan4pin;
2993                 fan3min = fan3pin;
2994                 pwm3pin = fan3pin;
2995                 pwm4pin = 0;
2996                 pwm5pin = 0;
2997         } else {        /* NCT6779D */
2998                 regval = superio_inb(sio_data->sioreg, 0x1c);
2999
3000                 fan3pin = !(regval & (1 << 5));
3001                 fan4pin = !(regval & (1 << 6));
3002                 fan5pin = !(regval & (1 << 7));
3003
3004                 pwm3pin = !(regval & (1 << 0));
3005                 pwm4pin = !(regval & (1 << 1));
3006                 pwm5pin = !(regval & (1 << 2));
3007
3008                 fan3min = 0;
3009                 fan4min = 0;
3010         }
3011
3012         superio_exit(sio_data->sioreg);
3013
3014         data->has_fan = data->has_fan_min = 0x03; /* fan1 and fan2 */
3015         data->has_fan |= fan3pin << 2;
3016         data->has_fan_min |= fan3min << 2;
3017
3018         data->has_fan |= (fan4pin << 3) | (fan5pin << 4);
3019         data->has_fan_min |= (fan4min << 3) | (fan5pin << 4);
3020
3021         data->has_pwm = 0x03 | (pwm3pin << 2) | (pwm4pin << 3) | (pwm5pin << 4);
3022 }
3023
3024 static int __devinit nct6775_probe(struct platform_device *pdev)
3025 {
3026         struct device *dev = &pdev->dev;
3027         struct nct6775_sio_data *sio_data = dev->platform_data;
3028         struct nct6775_data *data;
3029         struct resource *res;
3030         int i, s, err = 0;
3031         int src, mask, available;
3032         const u16 *reg_temp, *reg_temp_over, *reg_temp_hyst, *reg_temp_config;
3033
3034         res = platform_get_resource(pdev, IORESOURCE_IO, 0);
3035         if (!request_region(res->start, IOREGION_LENGTH, DRVNAME)) {
3036                 err = -EBUSY;
3037                 dev_err(dev, "Failed to request region 0x%lx-0x%lx\n",
3038                         (unsigned long)res->start,
3039                         (unsigned long)res->start + IOREGION_LENGTH - 1);
3040                 goto exit;
3041         }
3042
3043         data = devm_kzalloc(&pdev->dev, sizeof(struct nct6775_data),
3044                             GFP_KERNEL);
3045         if (!data) {
3046                 err = -ENOMEM;
3047                 goto exit_release;
3048         }
3049
3050         data->kind = sio_data->kind;
3051         data->addr = res->start;
3052         mutex_init(&data->lock);
3053         mutex_init(&data->update_lock);
3054         data->name = nct6775_device_names[data->kind];
3055         platform_set_drvdata(pdev, data);
3056
3057         switch (data->kind) {
3058         case nct6775:
3059                 data->in_num = 9;
3060                 data->have_in = 0x1ff;
3061                 data->pwm_num = 3;
3062                 data->auto_pwm_num = 6;
3063                 data->has_fan_div = true;
3064                 data->temp_fixed_num = 3;
3065
3066                 data->fan_from_reg = fan_from_reg16;
3067                 data->fan_from_reg_min = fan_from_reg8;
3068
3069                 data->temp_label = nct6775_temp_label;
3070                 data->temp_label_num = ARRAY_SIZE(nct6775_temp_label);
3071
3072                 data->REG_CONFIG = NCT6775_REG_CONFIG;
3073                 data->REG_VBAT = NCT6775_REG_VBAT;
3074                 data->REG_DIODE = NCT6775_REG_DIODE;
3075                 data->REG_VIN = NCT6775_REG_IN;
3076                 data->REG_IN_MINMAX[0] = NCT6775_REG_IN_MIN;
3077                 data->REG_IN_MINMAX[1] = NCT6775_REG_IN_MAX;
3078                 data->REG_TARGET = NCT6775_REG_TARGET;
3079                 data->REG_FAN = NCT6775_REG_FAN;
3080                 data->REG_FAN_MODE = NCT6775_REG_FAN_MODE;
3081                 data->REG_FAN_MIN = NCT6775_REG_FAN_MIN;
3082                 data->REG_FAN_TIME[0] = NCT6775_REG_FAN_STOP_TIME;
3083                 data->REG_FAN_TIME[1] = NCT6775_REG_FAN_STEP_UP_TIME;
3084                 data->REG_FAN_TIME[2] = NCT6775_REG_FAN_STEP_DOWN_TIME;
3085                 data->REG_PWM[0] = NCT6775_REG_PWM;
3086                 data->REG_PWM[1] = NCT6775_REG_FAN_START_OUTPUT;
3087                 data->REG_PWM[2] = NCT6775_REG_FAN_STOP_OUTPUT;
3088                 data->REG_PWM[3] = NCT6775_REG_FAN_MAX_OUTPUT;
3089                 data->REG_PWM[4] = NCT6775_REG_FAN_STEP_OUTPUT;
3090                 data->REG_PWM[5] = NCT6775_REG_WEIGHT_DUTY_STEP;
3091                 data->REG_PWM_READ = NCT6775_REG_PWM_READ;
3092                 data->REG_PWM_MODE = NCT6775_REG_PWM_MODE;
3093                 data->PWM_MODE_MASK = NCT6775_PWM_MODE_MASK;
3094                 data->REG_TEMP_ALTERNATE = NCT6775_REG_TEMP_ALTERNATE;
3095                 data->REG_TEMP_MON = NCT6775_REG_TEMP_MON;
3096                 data->REG_AUTO_TEMP = NCT6775_REG_AUTO_TEMP;
3097                 data->REG_AUTO_PWM = NCT6775_REG_AUTO_PWM;
3098                 data->REG_CRITICAL_TEMP = NCT6775_REG_CRITICAL_TEMP;
3099                 data->REG_CRITICAL_TEMP_TOLERANCE
3100                   = NCT6775_REG_CRITICAL_TEMP_TOLERANCE;
3101                 data->REG_TEMP_OFFSET = NCT6775_REG_TEMP_OFFSET;
3102                 data->REG_TEMP_SOURCE = NCT6775_REG_TEMP_SOURCE;
3103                 data->REG_TEMP_SEL[0] = NCT6775_REG_TEMP_SEL;
3104                 data->REG_TEMP_SEL[1] = NCT6775_REG_WEIGHT_TEMP_SEL;
3105                 data->REG_WEIGHT_TEMP[0] = NCT6775_REG_WEIGHT_TEMP_STEP;
3106                 data->REG_WEIGHT_TEMP[1] = NCT6775_REG_WEIGHT_TEMP_STEP_TOL;
3107                 data->REG_WEIGHT_TEMP[2] = NCT6775_REG_WEIGHT_TEMP_BASE;
3108                 data->REG_ALARM = NCT6775_REG_ALARM;
3109                 data->REG_CASEOPEN = NCT6775_REG_CASEOPEN;
3110                 data->CASEOPEN_MASK = NCT6775_CASEOPEN_MASK;
3111
3112                 reg_temp = NCT6775_REG_TEMP;
3113                 reg_temp_over = NCT6775_REG_TEMP_OVER;
3114                 reg_temp_hyst = NCT6775_REG_TEMP_HYST;
3115                 reg_temp_config = NCT6775_REG_TEMP_CONFIG;
3116
3117                 break;
3118         case nct6776:
3119                 data->in_num = 9;
3120                 data->have_in = 0x1ff;
3121                 data->pwm_num = 3;
3122                 data->auto_pwm_num = 4;
3123                 data->has_fan_div = false;
3124                 data->temp_fixed_num = 3;
3125
3126                 data->fan_from_reg = fan_from_reg13;
3127                 data->fan_from_reg_min = fan_from_reg13;
3128
3129                 data->temp_label = nct6776_temp_label;
3130                 data->temp_label_num = ARRAY_SIZE(nct6776_temp_label);
3131
3132                 data->REG_CONFIG = NCT6775_REG_CONFIG;
3133                 data->REG_VBAT = NCT6775_REG_VBAT;
3134                 data->REG_DIODE = NCT6775_REG_DIODE;
3135                 data->REG_VIN = NCT6775_REG_IN;
3136                 data->REG_IN_MINMAX[0] = NCT6775_REG_IN_MIN;
3137                 data->REG_IN_MINMAX[1] = NCT6775_REG_IN_MAX;
3138                 data->REG_TARGET = NCT6775_REG_TARGET;
3139                 data->REG_FAN = NCT6775_REG_FAN;
3140                 data->REG_FAN_MODE = NCT6775_REG_FAN_MODE;
3141                 data->REG_FAN_MIN = NCT6776_REG_FAN_MIN;
3142                 data->REG_FAN_TIME[0] = NCT6775_REG_FAN_STOP_TIME;
3143                 data->REG_FAN_TIME[1] = NCT6775_REG_FAN_STEP_UP_TIME;
3144                 data->REG_FAN_TIME[2] = NCT6775_REG_FAN_STEP_DOWN_TIME;
3145                 data->REG_PWM[0] = NCT6775_REG_PWM;
3146                 data->REG_PWM[1] = NCT6775_REG_FAN_START_OUTPUT;
3147                 data->REG_PWM[2] = NCT6775_REG_FAN_STOP_OUTPUT;
3148                 data->REG_PWM[5] = NCT6775_REG_WEIGHT_DUTY_STEP;
3149                 data->REG_PWM[6] = NCT6776_REG_WEIGHT_DUTY_BASE;
3150                 data->REG_PWM_READ = NCT6775_REG_PWM_READ;
3151                 data->REG_PWM_MODE = NCT6776_REG_PWM_MODE;
3152                 data->PWM_MODE_MASK = NCT6776_PWM_MODE_MASK;
3153                 data->REG_TEMP_ALTERNATE = NCT6776_REG_TEMP_ALTERNATE;
3154                 data->REG_TEMP_MON = NCT6775_REG_TEMP_MON;
3155                 data->REG_AUTO_TEMP = NCT6775_REG_AUTO_TEMP;
3156                 data->REG_AUTO_PWM = NCT6775_REG_AUTO_PWM;
3157                 data->REG_CRITICAL_TEMP = NCT6775_REG_CRITICAL_TEMP;
3158                 data->REG_CRITICAL_TEMP_TOLERANCE
3159                   = NCT6775_REG_CRITICAL_TEMP_TOLERANCE;
3160                 data->REG_TEMP_OFFSET = NCT6775_REG_TEMP_OFFSET;
3161                 data->REG_TEMP_SOURCE = NCT6775_REG_TEMP_SOURCE;
3162                 data->REG_TEMP_SEL[0] = NCT6775_REG_TEMP_SEL;
3163                 data->REG_TEMP_SEL[1] = NCT6775_REG_WEIGHT_TEMP_SEL;
3164                 data->REG_WEIGHT_TEMP[0] = NCT6775_REG_WEIGHT_TEMP_STEP;
3165                 data->REG_WEIGHT_TEMP[1] = NCT6775_REG_WEIGHT_TEMP_STEP_TOL;
3166                 data->REG_WEIGHT_TEMP[2] = NCT6775_REG_WEIGHT_TEMP_BASE;
3167                 data->REG_ALARM = NCT6775_REG_ALARM;
3168                 data->REG_CASEOPEN = NCT6775_REG_CASEOPEN;
3169                 data->CASEOPEN_MASK = NCT6776_CASEOPEN_MASK;
3170
3171                 reg_temp = NCT6775_REG_TEMP;
3172                 reg_temp_over = NCT6775_REG_TEMP_OVER;
3173                 reg_temp_hyst = NCT6775_REG_TEMP_HYST;
3174                 reg_temp_config = NCT6776_REG_TEMP_CONFIG;
3175
3176                 break;
3177         case nct6779:
3178                 data->in_num = 15;
3179                 data->have_in = 0x7fff;
3180                 data->pwm_num = 5;
3181                 data->auto_pwm_num = 4;
3182                 data->has_fan_div = false;
3183                 data->temp_fixed_num = 6;
3184
3185                 data->fan_from_reg = fan_from_reg13;
3186                 data->fan_from_reg_min = fan_from_reg13;
3187
3188                 data->temp_label = nct6779_temp_label;
3189                 data->temp_label_num = ARRAY_SIZE(nct6779_temp_label);
3190
3191                 data->REG_CONFIG = NCT6775_REG_CONFIG;
3192                 data->REG_VBAT = NCT6775_REG_VBAT;
3193                 data->REG_DIODE = NCT6775_REG_DIODE;
3194                 data->REG_VIN = NCT6779_REG_IN;
3195                 data->REG_IN_MINMAX[0] = NCT6775_REG_IN_MIN;
3196                 data->REG_IN_MINMAX[1] = NCT6775_REG_IN_MAX;
3197                 data->REG_TARGET = NCT6775_REG_TARGET;
3198                 data->REG_FAN = NCT6779_REG_FAN;
3199                 data->REG_FAN_MODE = NCT6775_REG_FAN_MODE;
3200                 data->REG_FAN_MIN = NCT6776_REG_FAN_MIN;
3201                 data->REG_FAN_TIME[0] = NCT6775_REG_FAN_STOP_TIME;
3202                 data->REG_FAN_TIME[1] = NCT6775_REG_FAN_STEP_UP_TIME;
3203                 data->REG_FAN_TIME[2] = NCT6775_REG_FAN_STEP_DOWN_TIME;
3204                 data->REG_PWM[0] = NCT6775_REG_PWM;
3205                 data->REG_PWM[1] = NCT6775_REG_FAN_START_OUTPUT;
3206                 data->REG_PWM[2] = NCT6775_REG_FAN_STOP_OUTPUT;
3207                 data->REG_PWM[5] = NCT6775_REG_WEIGHT_DUTY_STEP;
3208                 data->REG_PWM[6] = NCT6776_REG_WEIGHT_DUTY_BASE;
3209                 data->REG_PWM_READ = NCT6775_REG_PWM_READ;
3210                 data->REG_PWM_MODE = NCT6776_REG_PWM_MODE;
3211                 data->PWM_MODE_MASK = NCT6776_PWM_MODE_MASK;
3212                 data->REG_TEMP_ALTERNATE = NCT6779_REG_TEMP_ALTERNATE;
3213                 data->REG_TEMP_MON = NCT6775_REG_TEMP_MON;
3214                 data->REG_AUTO_TEMP = NCT6775_REG_AUTO_TEMP;
3215                 data->REG_AUTO_PWM = NCT6775_REG_AUTO_PWM;
3216                 data->REG_CRITICAL_TEMP = NCT6775_REG_CRITICAL_TEMP;
3217                 data->REG_CRITICAL_TEMP_TOLERANCE
3218                   = NCT6775_REG_CRITICAL_TEMP_TOLERANCE;
3219                 data->REG_TEMP_OFFSET = NCT6779_REG_TEMP_OFFSET;
3220                 data->REG_TEMP_SOURCE = NCT6775_REG_TEMP_SOURCE;
3221                 data->REG_TEMP_SEL[0] = NCT6775_REG_TEMP_SEL;
3222                 data->REG_TEMP_SEL[1] = NCT6775_REG_WEIGHT_TEMP_SEL;
3223                 data->REG_WEIGHT_TEMP[0] = NCT6775_REG_WEIGHT_TEMP_STEP;
3224                 data->REG_WEIGHT_TEMP[1] = NCT6775_REG_WEIGHT_TEMP_STEP_TOL;
3225                 data->REG_WEIGHT_TEMP[2] = NCT6775_REG_WEIGHT_TEMP_BASE;
3226                 data->REG_ALARM = NCT6779_REG_ALARM;
3227                 data->REG_CASEOPEN = NCT6775_REG_CASEOPEN;
3228                 data->CASEOPEN_MASK = NCT6776_CASEOPEN_MASK;
3229
3230                 reg_temp = NCT6779_REG_TEMP;
3231                 reg_temp_over = NCT6775_REG_TEMP_OVER;
3232                 reg_temp_hyst = NCT6775_REG_TEMP_HYST;
3233                 reg_temp_config = NCT6776_REG_TEMP_CONFIG;
3234
3235                 break;
3236         default:
3237                 err = -ENODEV;
3238                 goto exit_release;
3239         }
3240
3241         data->have_temp = 0;
3242
3243         /*
3244          * On some boards, not all available temperature sources are monitored,
3245          * even though some of the monitoring registers are unused.
3246          * Get list of unused monitoring registers, then detect if any fan
3247          * controls are configured to use unmonitored temperature sources.
3248          * If so, assign the unmonitored temperature sources to available
3249          * monitoring registers.
3250          */
3251         mask = 0;
3252         available = 0;
3253         for (i = 0; i < NUM_REG_TEMP; i++) {
3254                 if (reg_temp[i] == 0)
3255                         continue;
3256
3257                 src = nct6775_read_value(data, data->REG_TEMP_SOURCE[i]) & 0x1f;
3258                 if (!src || (mask & (1 << src)))
3259                         available |= 1 << i;
3260
3261                 mask |= 1 << src;
3262         }
3263
3264         /*
3265          * Now find unmonitored temperature registers and enable monitoring
3266          * if additional monitoring registers are available.
3267          */
3268         for (i = 0; i < ARRAY_SIZE(data->REG_TEMP_SEL) && available; i++) {
3269                 int j;
3270
3271                 if (!data->REG_TEMP_SEL[i])
3272                         continue;
3273                 for (j = 0; j < data->pwm_num && available; j++) {
3274                         int index;
3275
3276                         if (!data->REG_TEMP_SEL[i][j])
3277                                 continue;
3278                         src = nct6775_read_value(data,
3279                                                  data->REG_TEMP_SEL[i][j]);
3280                         src &= 0x1f;
3281                         if (!src || (mask & (1 << src)))
3282                                 continue;
3283
3284                         index = __ffs(available);
3285                         nct6775_write_value(data,
3286                                             data->REG_TEMP_SOURCE[index],
3287                                             src);
3288                         available &= ~(1 << index);
3289                         mask |= 1 << src;
3290                 }
3291         }
3292
3293         mask = 0;
3294         s = NUM_TEMP_FIXED;     /* First dynamic temperature attribute */
3295         for (i = 0; i < NUM_REG_TEMP; i++) {
3296                 if (reg_temp[i] == 0)
3297                         continue;
3298
3299                 src = nct6775_read_value(data, data->REG_TEMP_SOURCE[i]) & 0x1f;
3300                 if (!src || (mask & (1 << src)))
3301                         continue;
3302
3303                 mask |= 1 << src;
3304
3305                 /* Use fixed index for SYSTIN(1), CPUTIN(2), AUXTIN(3) */
3306                 if (src <= data->temp_fixed_num) {
3307                         data->have_temp |= 1 << (src - 1);
3308                         data->have_temp_fixed |= 1 << (src - 1);
3309                         data->reg_temp[0][src - 1] = reg_temp[i];
3310                         data->reg_temp[1][src - 1] = reg_temp_over[i];
3311                         data->reg_temp[2][src - 1] = reg_temp_hyst[i];
3312                         data->reg_temp_config[src - 1] = reg_temp_config[i];
3313                         data->temp_src[src - 1] = src;
3314                         continue;
3315                 }
3316
3317                 if (s >= NUM_TEMP)
3318                         continue;
3319
3320                 /* Use dynamic index for other sources */
3321                 data->have_temp |= 1 << s;
3322                 data->reg_temp[0][s] = reg_temp[i];
3323                 data->reg_temp[1][s] = reg_temp_over[i];
3324                 data->reg_temp[2][s] = reg_temp_hyst[i];
3325                 data->reg_temp_config[s] = reg_temp_config[i];
3326
3327                 data->temp_src[s] = src;
3328                 s++;
3329         }
3330
3331         /*
3332          * We may have alternate registers for some sensors.
3333          * Go through the list and enable if possible.
3334          * The temperature is already monitored if the respective bit in <mask>
3335          * is set.
3336          */
3337         if (data->REG_TEMP_ALTERNATE) {
3338                 for (i = 0; i < data->temp_label_num; i++) {
3339                         if (!data->REG_TEMP_ALTERNATE[i])
3340                                 continue;
3341                         pr_info("Alternate index %d reg 0x%x source %s mask 0x%x have 0x%x s %d\n",
3342                                 i, data->REG_TEMP_ALTERNATE[i],
3343                                 data->temp_label[i + 1], mask,
3344                                 data->have_temp, s);
3345                         if (mask & (1 << (i + 1)))
3346                                 continue;
3347                         if (i < data->temp_fixed_num) {
3348                                 if (data->have_temp & (1 << i))
3349                                         continue;
3350                                 data->have_temp |= 1 << i;
3351                                 data->reg_temp[0][i] = data->REG_TEMP_ALTERNATE[i];
3352                                 data->temp_src[i] = i + 1;
3353                                 continue;
3354                         }
3355
3356                         if (s >= NUM_TEMP)      /* Abort if no more space */
3357                                 break;
3358
3359                         data->have_temp |= 1 << s;
3360                         data->reg_temp[0][s] = data->REG_TEMP_ALTERNATE[i];
3361                         data->temp_src[s] = i + 1;
3362                         s++;
3363                 }
3364         }
3365
3366         switch (data->kind) {
3367         case nct6775:
3368                 break;
3369         case nct6776:
3370                 /*
3371                  * On NCT6776, AUXTIN and VIN3 pins are shared.
3372                  * Only way to detect it is to check if AUXTIN is used
3373                  * as a temperature source, and if that source is
3374                  * enabled.
3375                  *
3376                  * If that is the case, disable in6, which reports VIN3.
3377                  * Otherwise disable temp3.
3378                  */
3379                 if (data->have_temp & (1 << 2)) {
3380                         u8 reg = nct6775_read_value(data,
3381                                                     data->reg_temp_config[2]);
3382                         if (reg & 0x01)
3383                                 data->have_temp &= ~(1 << 2);
3384                         else
3385                                 data->have_in &= ~(1 << 6);
3386                 }
3387                 break;
3388         case nct6779:
3389                 /*
3390                  * Shared pins:
3391                  *      VIN4 / AUXTIN0
3392                  *      VIN5 / AUXTIN1
3393                  *      VIN6 / AUXTIN2
3394                  *      VIN7 / AUXTIN3
3395                  * Assume voltage is disabled if the respective temperature is
3396                  * used as temperature source.
3397                  */
3398                 for (i = 0; i < ARRAY_SIZE(NCT6779_REG_TEMP); i++) {
3399                         if (!(data->have_temp & (1 << i)))
3400                                 continue;
3401                         if (i == 2)                             /* AUXTIN0 */
3402                                 data->have_in &= ~(1 << 6);     /* no VIN4 */
3403                         if (i == 3)                             /* AUXTIN1 */
3404                                 data->have_in &= ~(1 << 10);    /* no VIN5 */
3405                         if (i == 4)                             /* AUXTIN2 */
3406                                 data->have_in &= ~(1 << 11);    /* no VIN6 */
3407                         if (i == 5)                             /* AUXTIN3 */
3408                                 data->have_in &= ~(1 << 14);    /* no VIN7 */
3409                 }
3410                 break;
3411         }
3412
3413         /* Initialize the chip */
3414         nct6775_init_device(data);
3415
3416         data->vrm = vid_which_vrm();
3417         superio_enter(sio_data->sioreg);
3418         /*
3419          * Read VID value
3420          * We can get the VID input values directly at logical device D 0xe3.
3421          */
3422         superio_select(sio_data->sioreg, NCT6775_LD_VID);
3423         data->vid = superio_inb(sio_data->sioreg, 0xe3);
3424         err = device_create_file(dev, &dev_attr_cpu0_vid);
3425         if (err)
3426                 goto exit_release;
3427
3428         if (fan_debounce) {
3429                 u8 tmp;
3430
3431                 superio_select(sio_data->sioreg, NCT6775_LD_HWM);
3432                 tmp = superio_inb(sio_data->sioreg,
3433                                   NCT6775_REG_CR_FAN_DEBOUNCE);
3434                 switch (data->kind) {
3435                 case nct6775:
3436                         tmp |= 0x1e;
3437                         break;
3438                 case nct6776:
3439                 case nct6779:
3440                         tmp |= 0x3e;
3441                         break;
3442                 }
3443                 superio_outb(sio_data->sioreg, NCT6775_REG_CR_FAN_DEBOUNCE,
3444                              tmp);
3445                 pr_info("Enabled fan debounce for chip %s\n", data->name);
3446         }
3447
3448         superio_exit(sio_data->sioreg);
3449
3450         nct6775_check_fan_inputs(sio_data, data);
3451
3452         /* Read fan clock dividers immediately */
3453         nct6775_update_fan_div_common(dev, data);
3454
3455         /* Register sysfs hooks */
3456         for (i = 0; i < data->pwm_num; i++) {
3457                 if (!(data->has_pwm & (1 << i)))
3458                         continue;
3459
3460                 err = sysfs_create_group(&dev->kobj, &nct6775_group_pwm[i]);
3461                 if (err)
3462                         goto exit_remove;
3463
3464                 if (data->REG_PWM[3]) {
3465                         err = device_create_file(dev,
3466                                         &sda_max_output[i].dev_attr);
3467                         if (err)
3468                                 goto exit_remove;
3469                 }
3470                 if (data->REG_PWM[4]) {
3471                         err = device_create_file(dev,
3472                                         &sda_step_output[i].dev_attr);
3473                         if (err)
3474                                 goto exit_remove;
3475                 }
3476                 if (data->REG_PWM[6]) {
3477                         err = device_create_file(dev,
3478                                         &sda_weight_duty_base[i].dev_attr);
3479                         if (err)
3480                                 goto exit_remove;
3481                 }
3482         }
3483         for (i = 0; i < ARRAY_SIZE(sda_auto_pwm_arrays); i++) {
3484                 struct sensor_device_attribute_2 *attr =
3485                         &sda_auto_pwm_arrays[i];
3486
3487                 if (!(data->has_pwm & (1 << attr->nr)))
3488                         continue;
3489                 if (attr->index > data->auto_pwm_num)
3490                         continue;
3491                 err = device_create_file(dev, &attr->dev_attr);
3492                 if (err)
3493                         goto exit_remove;
3494         }
3495
3496         for (i = 0; i < data->in_num; i++) {
3497                 if (!(data->have_in & (1 << i)))
3498                         continue;
3499                 err = sysfs_create_group(&dev->kobj, &nct6775_group_in[i]);
3500                 if (err)
3501                         goto exit_remove;
3502         }
3503
3504         for (i = 0; i < 5; i++) {
3505                 if (data->has_fan & (1 << i)) {
3506                         err = device_create_file(dev,
3507                                                  &sda_fan_input[i].dev_attr);
3508                         if (err)
3509                                 goto exit_remove;
3510                         err = device_create_file(dev,
3511                                                  &sda_fan_alarm[i].dev_attr);
3512                         if (err)
3513                                 goto exit_remove;
3514                         if (data->kind != nct6776 &&
3515                             data->kind != nct6779) {
3516                                 err = device_create_file(dev,
3517                                                 &sda_fan_div[i].dev_attr);
3518                                 if (err)
3519                                         goto exit_remove;
3520                         }
3521                         if (data->has_fan_min & (1 << i)) {
3522                                 err = device_create_file(dev,
3523                                                 &sda_fan_min[i].dev_attr);
3524                                 if (err)
3525                                         goto exit_remove;
3526                         }
3527                 }
3528         }
3529
3530         for (i = 0; i < NUM_TEMP; i++) {
3531                 if (!(data->have_temp & (1 << i)))
3532                         continue;
3533                 err = device_create_file(dev, &sda_temp_input[i].dev_attr);
3534                 if (err)
3535                         goto exit_remove;
3536                 if (data->temp_label) {
3537                         err = device_create_file(dev,
3538                                                  &sda_temp_label[i].dev_attr);
3539                         if (err)
3540                                 goto exit_remove;
3541                 }
3542                 if (data->reg_temp[1][i]) {
3543                         err = device_create_file(dev,
3544                                                  &sda_temp_max[i].dev_attr);
3545                         if (err)
3546                                 goto exit_remove;
3547                 }
3548                 if (data->reg_temp[2][i]) {
3549                         err = device_create_file(dev,
3550                                         &sda_temp_max_hyst[i].dev_attr);
3551                         if (err)
3552                                 goto exit_remove;
3553                 }
3554                 if (!(data->have_temp_fixed & (1 << i)))
3555                         continue;
3556                 err = device_create_file(dev, &sda_temp_type[i].dev_attr);
3557                 if (err)
3558                         goto exit_remove;
3559                 err = device_create_file(dev,
3560                                          &sda_temp_offset[i].dev_attr);
3561                 if (err)
3562                         goto exit_remove;
3563                 if (i >= NUM_TEMP_ALARM)
3564                         continue;
3565                 err = device_create_file(dev, &sda_temp_alarm[i].dev_attr);
3566                 if (err)
3567                         goto exit_remove;
3568         }
3569
3570         for (i = 0; i < ARRAY_SIZE(sda_caseopen); i++) {
3571                 if (!data->CASEOPEN_MASK[i])
3572                         continue;
3573                 err = device_create_file(dev, &sda_caseopen[i].dev_attr);
3574                 if (err)
3575                         goto exit_remove;
3576         }
3577
3578         err = device_create_file(dev, &dev_attr_name);
3579         if (err)
3580                 goto exit_remove;
3581
3582         data->hwmon_dev = hwmon_device_register(dev);
3583         if (IS_ERR(data->hwmon_dev)) {
3584                 err = PTR_ERR(data->hwmon_dev);
3585                 goto exit_remove;
3586         }
3587
3588         return 0;
3589
3590 exit_remove:
3591         nct6775_device_remove_files(dev);
3592 exit_release:
3593         release_region(res->start, IOREGION_LENGTH);
3594 exit:
3595         return err;
3596 }
3597
3598 static int __devexit nct6775_remove(struct platform_device *pdev)
3599 {
3600         struct nct6775_data *data = platform_get_drvdata(pdev);
3601
3602         hwmon_device_unregister(data->hwmon_dev);
3603         nct6775_device_remove_files(&pdev->dev);
3604         release_region(data->addr, IOREGION_LENGTH);
3605
3606         return 0;
3607 }
3608
3609 static struct platform_driver nct6775_driver = {
3610         .driver = {
3611                 .owner  = THIS_MODULE,
3612                 .name   = DRVNAME,
3613         },
3614         .probe          = nct6775_probe,
3615         .remove         = __devexit_p(nct6775_remove),
3616 };
3617
3618 /* nct6775_find() looks for a '627 in the Super-I/O config space */
3619 static int __init nct6775_find(int sioaddr, unsigned short *addr,
3620                                struct nct6775_sio_data *sio_data)
3621 {
3622         static const char __initdata sio_name_NCT6775[] = "NCT6775F";
3623         static const char __initdata sio_name_NCT6776[] = "NCT6776F";
3624         static const char __initdata sio_name_NCT6779[] = "NCT6779D";
3625
3626         u16 val;
3627         const char *sio_name;
3628
3629         superio_enter(sioaddr);
3630
3631         if (force_id)
3632                 val = force_id;
3633         else
3634                 val = (superio_inb(sioaddr, SIO_REG_DEVID) << 8)
3635                     | superio_inb(sioaddr, SIO_REG_DEVID + 1);
3636         switch (val & SIO_ID_MASK) {
3637         case SIO_NCT6775_ID:
3638                 sio_data->kind = nct6775;
3639                 sio_name = sio_name_NCT6775;
3640                 break;
3641         case SIO_NCT6776_ID:
3642                 sio_data->kind = nct6776;
3643                 sio_name = sio_name_NCT6776;
3644                 break;
3645         case SIO_NCT6779_ID:
3646                 sio_data->kind = nct6779;
3647                 sio_name = sio_name_NCT6779;
3648                 break;
3649         default:
3650                 if (val != 0xffff)
3651                         pr_debug("unsupported chip ID: 0x%04x\n", val);
3652                 superio_exit(sioaddr);
3653                 return -ENODEV;
3654         }
3655
3656         /* We have a known chip, find the HWM I/O address */
3657         superio_select(sioaddr, NCT6775_LD_HWM);
3658         val = (superio_inb(sioaddr, SIO_REG_ADDR) << 8)
3659             | superio_inb(sioaddr, SIO_REG_ADDR + 1);
3660         *addr = val & IOREGION_ALIGNMENT;
3661         if (*addr == 0) {
3662                 pr_err("Refusing to enable a Super-I/O device with a base I/O port 0\n");
3663                 superio_exit(sioaddr);
3664                 return -ENODEV;
3665         }
3666
3667         /* Activate logical device if needed */
3668         val = superio_inb(sioaddr, SIO_REG_ENABLE);
3669         if (!(val & 0x01)) {
3670                 pr_warn("Forcibly enabling Super-I/O. "
3671                         "Sensor is probably unusable.\n");
3672                 superio_outb(sioaddr, SIO_REG_ENABLE, val | 0x01);
3673         }
3674
3675         superio_exit(sioaddr);
3676         pr_info("Found %s chip at %#x\n", sio_name, *addr);
3677         sio_data->sioreg = sioaddr;
3678
3679         return 0;
3680 }
3681
3682 /*
3683  * when Super-I/O functions move to a separate file, the Super-I/O
3684  * bus will manage the lifetime of the device and this module will only keep
3685  * track of the nct6775 driver. But since we platform_device_alloc(), we
3686  * must keep track of the device
3687  */
3688 static struct platform_device *pdev;
3689
3690 static int __init sensors_nct6775_init(void)
3691 {
3692         int err;
3693         unsigned short address;
3694         struct resource res;
3695         struct nct6775_sio_data sio_data;
3696
3697         /*
3698          * initialize sio_data->kind and sio_data->sioreg.
3699          *
3700          * when Super-I/O functions move to a separate file, the Super-I/O
3701          * driver will probe 0x2e and 0x4e and auto-detect the presence of a
3702          * nct6775 hardware monitor, and call probe()
3703          */
3704         if (nct6775_find(0x2e, &address, &sio_data) &&
3705             nct6775_find(0x4e, &address, &sio_data))
3706                 return -ENODEV;
3707
3708         err = platform_driver_register(&nct6775_driver);
3709         if (err)
3710                 goto exit;
3711
3712         pdev = platform_device_alloc(DRVNAME, address);
3713         if (!pdev) {
3714                 err = -ENOMEM;
3715                 pr_err("Device allocation failed\n");
3716                 goto exit_unregister;
3717         }
3718
3719         err = platform_device_add_data(pdev, &sio_data,
3720                                        sizeof(struct nct6775_sio_data));
3721         if (err) {
3722                 pr_err("Platform data allocation failed\n");
3723                 goto exit_device_put;
3724         }
3725
3726         memset(&res, 0, sizeof(res));
3727         res.name = DRVNAME;
3728         res.start = address + IOREGION_OFFSET;
3729         res.end = address + IOREGION_OFFSET + IOREGION_LENGTH - 1;
3730         res.flags = IORESOURCE_IO;
3731
3732         err = acpi_check_resource_conflict(&res);
3733         if (err)
3734                 goto exit_device_put;
3735
3736         err = platform_device_add_resources(pdev, &res, 1);
3737         if (err) {
3738                 pr_err("Device resource addition failed (%d)\n", err);
3739                 goto exit_device_put;
3740         }
3741
3742         /* platform_device_add calls probe() */
3743         err = platform_device_add(pdev);
3744         if (err) {
3745                 pr_err("Device addition failed (%d)\n", err);
3746                 goto exit_device_put;
3747         }
3748
3749         return 0;
3750
3751 exit_device_put:
3752         platform_device_put(pdev);
3753 exit_unregister:
3754         platform_driver_unregister(&nct6775_driver);
3755 exit:
3756         return err;
3757 }
3758
3759 static void __exit sensors_nct6775_exit(void)
3760 {
3761         platform_device_unregister(pdev);
3762         platform_driver_unregister(&nct6775_driver);
3763 }
3764
3765 MODULE_AUTHOR("Guenter Roeck <linux@roeck-us.net>");
3766 MODULE_DESCRIPTION("NCT6775F/NCT6776F/NCT6779D driver");
3767 MODULE_LICENSE("GPL");
3768
3769 module_init(sensors_nct6775_init);
3770 module_exit(sensors_nct6775_exit);