blob: bafcae55e2557067851c3aef9dab9e2305ece659 [file] [log] [blame]
Guenter Roeck9de2e2e2012-05-20 19:29:48 -07001/*
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-2012 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+6 0xc560 0xc1 0x5ca3
39 *
40 * #temp lists the number of monitored temperature sources (first value) plus
41 * the number of directly connectable temperature sensors (second value).
42 */
43
44#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
45
46#include <linux/module.h>
47#include <linux/init.h>
48#include <linux/slab.h>
49#include <linux/jiffies.h>
50#include <linux/platform_device.h>
51#include <linux/hwmon.h>
52#include <linux/hwmon-sysfs.h>
53#include <linux/hwmon-vid.h>
54#include <linux/err.h>
55#include <linux/mutex.h>
56#include <linux/acpi.h>
57#include <linux/io.h>
58#include "lm75.h"
59
Guenter Roeckaa136e52012-12-04 03:26:05 -080060#define USE_ALTERNATE
61
Guenter Roeck9de2e2e2012-05-20 19:29:48 -070062enum kinds { nct6775, nct6776, nct6779 };
63
64/* used to set data->name = nct6775_device_names[data->sio_kind] */
65static const char * const nct6775_device_names[] = {
66 "nct6775",
67 "nct6776",
68 "nct6779",
69};
70
71static unsigned short force_id;
72module_param(force_id, ushort, 0);
73MODULE_PARM_DESC(force_id, "Override the detected device ID");
74
75#define DRVNAME "nct6775"
76
77/*
78 * Super-I/O constants and functions
79 */
80
Guenter Roecka6bd5872012-12-04 03:13:34 -080081#define NCT6775_LD_ACPI 0x0a
Guenter Roeck9de2e2e2012-05-20 19:29:48 -070082#define NCT6775_LD_HWM 0x0b
83#define NCT6775_LD_VID 0x0d
84
85#define SIO_REG_LDSEL 0x07 /* Logical device select */
86#define SIO_REG_DEVID 0x20 /* Device ID (2 bytes) */
87#define SIO_REG_ENABLE 0x30 /* Logical device enable */
88#define SIO_REG_ADDR 0x60 /* Logical device address (2 bytes) */
89
90#define SIO_NCT6775_ID 0xb470
91#define SIO_NCT6776_ID 0xc330
92#define SIO_NCT6779_ID 0xc560
93#define SIO_ID_MASK 0xFFF0
94
95static inline void
96superio_outb(int ioreg, int reg, int val)
97{
98 outb(reg, ioreg);
99 outb(val, ioreg + 1);
100}
101
102static inline int
103superio_inb(int ioreg, int reg)
104{
105 outb(reg, ioreg);
106 return inb(ioreg + 1);
107}
108
109static inline void
110superio_select(int ioreg, int ld)
111{
112 outb(SIO_REG_LDSEL, ioreg);
113 outb(ld, ioreg + 1);
114}
115
116static inline int
117superio_enter(int ioreg)
118{
119 /*
120 * Try to reserve <ioreg> and <ioreg + 1> for exclusive access.
121 */
122 if (!request_muxed_region(ioreg, 2, DRVNAME))
123 return -EBUSY;
124
125 outb(0x87, ioreg);
126 outb(0x87, ioreg);
127
128 return 0;
129}
130
131static inline void
132superio_exit(int ioreg)
133{
134 outb(0xaa, ioreg);
135 outb(0x02, ioreg);
136 outb(0x02, ioreg + 1);
137 release_region(ioreg, 2);
138}
139
140/*
141 * ISA constants
142 */
143
144#define IOREGION_ALIGNMENT (~7)
145#define IOREGION_OFFSET 5
146#define IOREGION_LENGTH 2
147#define ADDR_REG_OFFSET 0
148#define DATA_REG_OFFSET 1
149
150#define NCT6775_REG_BANK 0x4E
151#define NCT6775_REG_CONFIG 0x40
152
153/*
154 * Not currently used:
155 * REG_MAN_ID has the value 0x5ca3 for all supported chips.
156 * REG_CHIP_ID == 0x88/0xa1/0xc1 depending on chip model.
157 * REG_MAN_ID is at port 0x4f
158 * REG_CHIP_ID is at port 0x58
159 */
160
Guenter Roeckaa136e52012-12-04 03:26:05 -0800161#define NUM_TEMP 10 /* Max number of temp attribute sets w/ limits*/
162#define NUM_TEMP_FIXED 6 /* Max number of fixed temp attribute sets */
163
Guenter Roeck9de2e2e2012-05-20 19:29:48 -0700164#define NUM_REG_ALARM 4 /* Max number of alarm registers */
165
166/* Common and NCT6775 specific data */
167
168/* Voltage min/max registers for nr=7..14 are in bank 5 */
169
170static const u16 NCT6775_REG_IN_MAX[] = {
171 0x2b, 0x2d, 0x2f, 0x31, 0x33, 0x35, 0x37, 0x554, 0x556, 0x558, 0x55a,
172 0x55c, 0x55e, 0x560, 0x562 };
173static const u16 NCT6775_REG_IN_MIN[] = {
174 0x2c, 0x2e, 0x30, 0x32, 0x34, 0x36, 0x38, 0x555, 0x557, 0x559, 0x55b,
175 0x55d, 0x55f, 0x561, 0x563 };
176static const u16 NCT6775_REG_IN[] = {
177 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x550, 0x551, 0x552
178};
179
180#define NCT6775_REG_VBAT 0x5D
Guenter Roeckaa136e52012-12-04 03:26:05 -0800181#define NCT6775_REG_DIODE 0x5E
Guenter Roeck9de2e2e2012-05-20 19:29:48 -0700182
Guenter Roeck1c65dc32012-12-04 07:56:24 -0800183#define NCT6775_REG_FANDIV1 0x506
184#define NCT6775_REG_FANDIV2 0x507
185
Guenter Roeck9de2e2e2012-05-20 19:29:48 -0700186static const u16 NCT6775_REG_ALARM[NUM_REG_ALARM] = { 0x459, 0x45A, 0x45B };
187
188/* 0..15 voltages, 16..23 fans, 24..31 temperatures */
189
190static const s8 NCT6775_ALARM_BITS[] = {
191 0, 1, 2, 3, 8, 21, 20, 16, /* in0.. in7 */
192 17, -1, -1, -1, -1, -1, -1, /* in8..in14 */
193 -1, /* unused */
194 6, 7, 11, 10, 23, /* fan1..fan5 */
195 -1, -1, -1, /* unused */
196 4, 5, 13, -1, -1, -1, /* temp1..temp6 */
197 12, -1 }; /* intrusion0, intrusion1 */
198
Guenter Roeck1c65dc32012-12-04 07:56:24 -0800199#define FAN_ALARM_BASE 16
Guenter Roeckaa136e52012-12-04 03:26:05 -0800200#define TEMP_ALARM_BASE 24
Guenter Roecka6bd5872012-12-04 03:13:34 -0800201#define INTRUSION_ALARM_BASE 30
202
203static const u8 NCT6775_REG_CR_CASEOPEN_CLR[] = { 0xe6, 0xee };
204static const u8 NCT6775_CR_CASEOPEN_CLR_MASK[] = { 0x20, 0x01 };
205
Guenter Roeck1c65dc32012-12-04 07:56:24 -0800206static const u16 NCT6775_REG_FAN[] = { 0x630, 0x632, 0x634, 0x636, 0x638 };
207static const u16 NCT6775_REG_FAN_MIN[] = { 0x3b, 0x3c, 0x3d };
208
Guenter Roeckaa136e52012-12-04 03:26:05 -0800209static const u16 NCT6775_REG_TEMP[] = {
210 0x27, 0x150, 0x250, 0x62b, 0x62c, 0x62d };
211
212static const u16 NCT6775_REG_TEMP_CONFIG[ARRAY_SIZE(NCT6775_REG_TEMP)] = {
213 0, 0x152, 0x252, 0x628, 0x629, 0x62A };
214static const u16 NCT6775_REG_TEMP_HYST[ARRAY_SIZE(NCT6775_REG_TEMP)] = {
215 0x3a, 0x153, 0x253, 0x673, 0x678, 0x67D };
216static const u16 NCT6775_REG_TEMP_OVER[ARRAY_SIZE(NCT6775_REG_TEMP)] = {
217 0x39, 0x155, 0x255, 0x672, 0x677, 0x67C };
218
219static const u16 NCT6775_REG_TEMP_SOURCE[ARRAY_SIZE(NCT6775_REG_TEMP)] = {
220 0x621, 0x622, 0x623, 0x624, 0x625, 0x626 };
221
222static const u16 NCT6775_REG_TEMP_OFFSET[] = { 0x454, 0x455, 0x456 };
223
224static const char *const nct6775_temp_label[] = {
225 "",
226 "SYSTIN",
227 "CPUTIN",
228 "AUXTIN",
229 "AMD SB-TSI",
230 "PECI Agent 0",
231 "PECI Agent 1",
232 "PECI Agent 2",
233 "PECI Agent 3",
234 "PECI Agent 4",
235 "PECI Agent 5",
236 "PECI Agent 6",
237 "PECI Agent 7",
238 "PCH_CHIP_CPU_MAX_TEMP",
239 "PCH_CHIP_TEMP",
240 "PCH_CPU_TEMP",
241 "PCH_MCH_TEMP",
242 "PCH_DIM0_TEMP",
243 "PCH_DIM1_TEMP",
244 "PCH_DIM2_TEMP",
245 "PCH_DIM3_TEMP"
246};
247
248static const u16 NCT6775_REG_TEMP_ALTERNATE[ARRAY_SIZE(nct6775_temp_label) - 1]
249 = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0x661, 0x662, 0x664 };
250
251static const u16 NCT6775_REG_TEMP_CRIT[ARRAY_SIZE(nct6775_temp_label) - 1]
252 = { 0, 0, 0, 0, 0xa00, 0xa01, 0xa02, 0xa03, 0xa04, 0xa05, 0xa06,
253 0xa07 };
254
Guenter Roeck9de2e2e2012-05-20 19:29:48 -0700255/* NCT6776 specific data */
256
257static const s8 NCT6776_ALARM_BITS[] = {
258 0, 1, 2, 3, 8, 21, 20, 16, /* in0.. in7 */
259 17, -1, -1, -1, -1, -1, -1, /* in8..in14 */
260 -1, /* unused */
261 6, 7, 11, 10, 23, /* fan1..fan5 */
262 -1, -1, -1, /* unused */
263 4, 5, 13, -1, -1, -1, /* temp1..temp6 */
264 12, 9 }; /* intrusion0, intrusion1 */
265
Guenter Roeck1c65dc32012-12-04 07:56:24 -0800266static const u16 NCT6776_REG_FAN_MIN[] = { 0x63a, 0x63c, 0x63e, 0x640, 0x642 };
267
Guenter Roeckaa136e52012-12-04 03:26:05 -0800268static const u16 NCT6776_REG_TEMP_CONFIG[ARRAY_SIZE(NCT6775_REG_TEMP)] = {
269 0x18, 0x152, 0x252, 0x628, 0x629, 0x62A };
270
271static const char *const nct6776_temp_label[] = {
272 "",
273 "SYSTIN",
274 "CPUTIN",
275 "AUXTIN",
276 "SMBUSMASTER 0",
277 "SMBUSMASTER 1",
278 "SMBUSMASTER 2",
279 "SMBUSMASTER 3",
280 "SMBUSMASTER 4",
281 "SMBUSMASTER 5",
282 "SMBUSMASTER 6",
283 "SMBUSMASTER 7",
284 "PECI Agent 0",
285 "PECI Agent 1",
286 "PCH_CHIP_CPU_MAX_TEMP",
287 "PCH_CHIP_TEMP",
288 "PCH_CPU_TEMP",
289 "PCH_MCH_TEMP",
290 "PCH_DIM0_TEMP",
291 "PCH_DIM1_TEMP",
292 "PCH_DIM2_TEMP",
293 "PCH_DIM3_TEMP",
294 "BYTE_TEMP"
295};
296
297static const u16 NCT6776_REG_TEMP_ALTERNATE[ARRAY_SIZE(nct6776_temp_label) - 1]
298 = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0x401, 0x402, 0x404 };
299
300static const u16 NCT6776_REG_TEMP_CRIT[ARRAY_SIZE(nct6776_temp_label) - 1]
301 = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0x709, 0x70a };
302
Guenter Roeck9de2e2e2012-05-20 19:29:48 -0700303/* NCT6779 specific data */
304
305static const u16 NCT6779_REG_IN[] = {
306 0x480, 0x481, 0x482, 0x483, 0x484, 0x485, 0x486, 0x487,
307 0x488, 0x489, 0x48a, 0x48b, 0x48c, 0x48d, 0x48e };
308
309static const u16 NCT6779_REG_ALARM[NUM_REG_ALARM] = {
310 0x459, 0x45A, 0x45B, 0x568 };
311
312static const s8 NCT6779_ALARM_BITS[] = {
313 0, 1, 2, 3, 8, 21, 20, 16, /* in0.. in7 */
314 17, 24, 25, 26, 27, 28, 29, /* in8..in14 */
315 -1, /* unused */
316 6, 7, 11, 10, 23, /* fan1..fan5 */
317 -1, -1, -1, /* unused */
318 4, 5, 13, -1, -1, -1, /* temp1..temp6 */
319 12, 9 }; /* intrusion0, intrusion1 */
320
Guenter Roeck1c65dc32012-12-04 07:56:24 -0800321static const u16 NCT6779_REG_FAN[] = { 0x4b0, 0x4b2, 0x4b4, 0x4b6, 0x4b8 };
322
Guenter Roeckaa136e52012-12-04 03:26:05 -0800323static const u16 NCT6779_REG_TEMP[] = { 0x27, 0x150 };
324static const u16 NCT6779_REG_TEMP_CONFIG[ARRAY_SIZE(NCT6779_REG_TEMP)] = {
325 0x18, 0x152 };
326static const u16 NCT6779_REG_TEMP_HYST[ARRAY_SIZE(NCT6779_REG_TEMP)] = {
327 0x3a, 0x153 };
328static const u16 NCT6779_REG_TEMP_OVER[ARRAY_SIZE(NCT6779_REG_TEMP)] = {
329 0x39, 0x155 };
330
331static const u16 NCT6779_REG_TEMP_OFFSET[] = {
332 0x454, 0x455, 0x456, 0x44a, 0x44b, 0x44c };
333
334static const char *const nct6779_temp_label[] = {
335 "",
336 "SYSTIN",
337 "CPUTIN",
338 "AUXTIN0",
339 "AUXTIN1",
340 "AUXTIN2",
341 "AUXTIN3",
342 "",
343 "SMBUSMASTER 0",
344 "SMBUSMASTER 1",
345 "SMBUSMASTER 2",
346 "SMBUSMASTER 3",
347 "SMBUSMASTER 4",
348 "SMBUSMASTER 5",
349 "SMBUSMASTER 6",
350 "SMBUSMASTER 7",
351 "PECI Agent 0",
352 "PECI Agent 1",
353 "PCH_CHIP_CPU_MAX_TEMP",
354 "PCH_CHIP_TEMP",
355 "PCH_CPU_TEMP",
356 "PCH_MCH_TEMP",
357 "PCH_DIM0_TEMP",
358 "PCH_DIM1_TEMP",
359 "PCH_DIM2_TEMP",
360 "PCH_DIM3_TEMP",
361 "BYTE_TEMP"
362};
363
364static const u16 NCT6779_REG_TEMP_ALTERNATE[ARRAY_SIZE(nct6779_temp_label) - 1]
365 = { 0x490, 0x491, 0x492, 0x493, 0x494, 0x495, 0, 0,
366 0, 0, 0, 0, 0, 0, 0, 0,
367 0, 0x400, 0x401, 0x402, 0x404, 0x405, 0x406, 0x407,
368 0x408, 0 };
369
370static const u16 NCT6779_REG_TEMP_CRIT[ARRAY_SIZE(nct6779_temp_label) - 1]
371 = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0x709, 0x70a };
372
Guenter Roeck9de2e2e2012-05-20 19:29:48 -0700373/*
374 * Conversions
375 */
376
Guenter Roeck1c65dc32012-12-04 07:56:24 -0800377static unsigned int fan_from_reg8(u16 reg, unsigned int divreg)
378{
379 if (reg == 0 || reg == 255)
380 return 0;
381 return 1350000U / (reg << divreg);
382}
383
384static unsigned int fan_from_reg13(u16 reg, unsigned int divreg)
385{
386 if ((reg & 0xff1f) == 0xff1f)
387 return 0;
388
389 reg = (reg & 0x1f) | ((reg & 0xff00) >> 3);
390
391 if (reg == 0)
392 return 0;
393
394 return 1350000U / reg;
395}
396
397static unsigned int fan_from_reg16(u16 reg, unsigned int divreg)
398{
399 if (reg == 0 || reg == 0xffff)
400 return 0;
401
402 /*
403 * Even though the registers are 16 bit wide, the fan divisor
404 * still applies.
405 */
406 return 1350000U / (reg << divreg);
407}
408
409static inline unsigned int
410div_from_reg(u8 reg)
411{
412 return 1 << reg;
413}
414
Guenter Roeck9de2e2e2012-05-20 19:29:48 -0700415/*
416 * Some of the voltage inputs have internal scaling, the tables below
417 * contain 8 (the ADC LSB in mV) * scaling factor * 100
418 */
419static const u16 scale_in[15] = {
420 800, 800, 1600, 1600, 800, 800, 800, 1600, 1600, 800, 800, 800, 800,
421 800, 800
422};
423
424static inline long in_from_reg(u8 reg, u8 nr)
425{
426 return DIV_ROUND_CLOSEST(reg * scale_in[nr], 100);
427}
428
429static inline u8 in_to_reg(u32 val, u8 nr)
430{
431 return clamp_val(DIV_ROUND_CLOSEST(val * 100, scale_in[nr]), 0, 255);
432}
433
434/*
435 * Data structures and manipulation thereof
436 */
437
438struct nct6775_data {
439 int addr; /* IO base of hw monitor block */
440 enum kinds kind;
441 const char *name;
442
443 struct device *hwmon_dev;
444 struct mutex lock;
445
Guenter Roeckaa136e52012-12-04 03:26:05 -0800446 u16 reg_temp[4][NUM_TEMP]; /* 0=temp, 1=temp_over, 2=temp_hyst,
447 * 3=temp_crit
448 */
449 u8 temp_src[NUM_TEMP];
450 u16 reg_temp_config[NUM_TEMP];
451 const char * const *temp_label;
452 int temp_label_num;
453
Guenter Roeck9de2e2e2012-05-20 19:29:48 -0700454 u16 REG_CONFIG;
455 u16 REG_VBAT;
Guenter Roeckaa136e52012-12-04 03:26:05 -0800456 u16 REG_DIODE;
Guenter Roeck9de2e2e2012-05-20 19:29:48 -0700457
458 const s8 *ALARM_BITS;
459
460 const u16 *REG_VIN;
461 const u16 *REG_IN_MINMAX[2];
462
Guenter Roeck1c65dc32012-12-04 07:56:24 -0800463 const u16 *REG_FAN;
464 const u16 *REG_FAN_MIN;
Guenter Roeckaa136e52012-12-04 03:26:05 -0800465
Guenter Roeck1c65dc32012-12-04 07:56:24 -0800466 const u16 *REG_TEMP_SOURCE; /* temp register sources */
Guenter Roeckaa136e52012-12-04 03:26:05 -0800467 const u16 *REG_TEMP_OFFSET;
468
Guenter Roeck9de2e2e2012-05-20 19:29:48 -0700469 const u16 *REG_ALARM;
470
Guenter Roeck1c65dc32012-12-04 07:56:24 -0800471 unsigned int (*fan_from_reg)(u16 reg, unsigned int divreg);
472 unsigned int (*fan_from_reg_min)(u16 reg, unsigned int divreg);
473
Guenter Roeck9de2e2e2012-05-20 19:29:48 -0700474 struct mutex update_lock;
475 bool valid; /* true if following fields are valid */
476 unsigned long last_updated; /* In jiffies */
477
478 /* Register values */
479 u8 bank; /* current register bank */
480 u8 in_num; /* number of in inputs we have */
481 u8 in[15][3]; /* [0]=in, [1]=in_max, [2]=in_min */
Guenter Roeck1c65dc32012-12-04 07:56:24 -0800482 unsigned int rpm[5];
483 u16 fan_min[5];
484 u8 fan_div[5];
485 u8 has_fan; /* some fan inputs can be disabled */
486 u8 has_fan_min; /* some fans don't have min register */
487 bool has_fan_div;
Guenter Roeck9de2e2e2012-05-20 19:29:48 -0700488
Guenter Roeckaa136e52012-12-04 03:26:05 -0800489 u8 temp_fixed_num; /* 3 or 6 */
490 u8 temp_type[NUM_TEMP_FIXED];
491 s8 temp_offset[NUM_TEMP_FIXED];
492 s16 temp[4][NUM_TEMP]; /* 0=temp, 1=temp_over, 2=temp_hyst,
493 * 3=temp_crit */
Guenter Roeck9de2e2e2012-05-20 19:29:48 -0700494 u64 alarms;
495
496 u8 vid;
497 u8 vrm;
498
Guenter Roeckaa136e52012-12-04 03:26:05 -0800499 u16 have_temp;
500 u16 have_temp_fixed;
Guenter Roeck9de2e2e2012-05-20 19:29:48 -0700501 u16 have_in;
502};
503
504struct nct6775_sio_data {
505 int sioreg;
506 enum kinds kind;
507};
508
509static bool is_word_sized(struct nct6775_data *data, u16 reg)
510{
511 switch (data->kind) {
512 case nct6775:
513 return (((reg & 0xff00) == 0x100 ||
514 (reg & 0xff00) == 0x200) &&
515 ((reg & 0x00ff) == 0x50 ||
516 (reg & 0x00ff) == 0x53 ||
517 (reg & 0x00ff) == 0x55)) ||
518 (reg & 0xfff0) == 0x630 ||
519 reg == 0x640 || reg == 0x642 ||
520 reg == 0x662 ||
521 ((reg & 0xfff0) == 0x650 && (reg & 0x000f) >= 0x06) ||
522 reg == 0x73 || reg == 0x75 || reg == 0x77;
523 case nct6776:
524 return (((reg & 0xff00) == 0x100 ||
525 (reg & 0xff00) == 0x200) &&
526 ((reg & 0x00ff) == 0x50 ||
527 (reg & 0x00ff) == 0x53 ||
528 (reg & 0x00ff) == 0x55)) ||
529 (reg & 0xfff0) == 0x630 ||
530 reg == 0x402 ||
531 reg == 0x640 || reg == 0x642 ||
532 ((reg & 0xfff0) == 0x650 && (reg & 0x000f) >= 0x06) ||
533 reg == 0x73 || reg == 0x75 || reg == 0x77;
534 case nct6779:
535 return reg == 0x150 || reg == 0x153 || reg == 0x155 ||
536 ((reg & 0xfff0) == 0x4b0 && (reg & 0x000f) < 0x09) ||
537 reg == 0x402 ||
538 reg == 0x63a || reg == 0x63c || reg == 0x63e ||
539 reg == 0x640 || reg == 0x642 ||
540 reg == 0x73 || reg == 0x75 || reg == 0x77 || reg == 0x79 ||
541 reg == 0x7b;
542 }
543 return false;
544}
545
546/*
547 * On older chips, only registers 0x50-0x5f are banked.
548 * On more recent chips, all registers are banked.
549 * Assume that is the case and set the bank number for each access.
550 * Cache the bank number so it only needs to be set if it changes.
551 */
552static inline void nct6775_set_bank(struct nct6775_data *data, u16 reg)
553{
554 u8 bank = reg >> 8;
555 if (data->bank != bank) {
556 outb_p(NCT6775_REG_BANK, data->addr + ADDR_REG_OFFSET);
557 outb_p(bank, data->addr + DATA_REG_OFFSET);
558 data->bank = bank;
559 }
560}
561
562static u16 nct6775_read_value(struct nct6775_data *data, u16 reg)
563{
564 int res, word_sized = is_word_sized(data, reg);
565
566 mutex_lock(&data->lock);
567
568 nct6775_set_bank(data, reg);
569 outb_p(reg & 0xff, data->addr + ADDR_REG_OFFSET);
570 res = inb_p(data->addr + DATA_REG_OFFSET);
571 if (word_sized) {
572 outb_p((reg & 0xff) + 1,
573 data->addr + ADDR_REG_OFFSET);
574 res = (res << 8) + inb_p(data->addr + DATA_REG_OFFSET);
575 }
576
577 mutex_unlock(&data->lock);
578 return res;
579}
580
581static int nct6775_write_value(struct nct6775_data *data, u16 reg, u16 value)
582{
583 int word_sized = is_word_sized(data, reg);
584
585 mutex_lock(&data->lock);
586
587 nct6775_set_bank(data, reg);
588 outb_p(reg & 0xff, data->addr + ADDR_REG_OFFSET);
589 if (word_sized) {
590 outb_p(value >> 8, data->addr + DATA_REG_OFFSET);
591 outb_p((reg & 0xff) + 1,
592 data->addr + ADDR_REG_OFFSET);
593 }
594 outb_p(value & 0xff, data->addr + DATA_REG_OFFSET);
595
596 mutex_unlock(&data->lock);
597 return 0;
598}
599
Guenter Roeckaa136e52012-12-04 03:26:05 -0800600/* We left-align 8-bit temperature values to make the code simpler */
601static u16 nct6775_read_temp(struct nct6775_data *data, u16 reg)
602{
603 u16 res;
604
605 res = nct6775_read_value(data, reg);
606 if (!is_word_sized(data, reg))
607 res <<= 8;
608
609 return res;
610}
611
612static int nct6775_write_temp(struct nct6775_data *data, u16 reg, u16 value)
613{
614 if (!is_word_sized(data, reg))
615 value >>= 8;
616 return nct6775_write_value(data, reg, value);
617}
618
Guenter Roeck1c65dc32012-12-04 07:56:24 -0800619/* This function assumes that the caller holds data->update_lock */
620static void nct6775_write_fan_div(struct nct6775_data *data, int nr)
621{
622 u8 reg;
623
624 switch (nr) {
625 case 0:
626 reg = (nct6775_read_value(data, NCT6775_REG_FANDIV1) & 0x70)
627 | (data->fan_div[0] & 0x7);
628 nct6775_write_value(data, NCT6775_REG_FANDIV1, reg);
629 break;
630 case 1:
631 reg = (nct6775_read_value(data, NCT6775_REG_FANDIV1) & 0x7)
632 | ((data->fan_div[1] << 4) & 0x70);
633 nct6775_write_value(data, NCT6775_REG_FANDIV1, reg);
634 break;
635 case 2:
636 reg = (nct6775_read_value(data, NCT6775_REG_FANDIV2) & 0x70)
637 | (data->fan_div[2] & 0x7);
638 nct6775_write_value(data, NCT6775_REG_FANDIV2, reg);
639 break;
640 case 3:
641 reg = (nct6775_read_value(data, NCT6775_REG_FANDIV2) & 0x7)
642 | ((data->fan_div[3] << 4) & 0x70);
643 nct6775_write_value(data, NCT6775_REG_FANDIV2, reg);
644 break;
645 }
646}
647
648static void nct6775_write_fan_div_common(struct nct6775_data *data, int nr)
649{
650 if (data->kind == nct6775)
651 nct6775_write_fan_div(data, nr);
652}
653
654static void nct6775_update_fan_div(struct nct6775_data *data)
655{
656 u8 i;
657
658 i = nct6775_read_value(data, NCT6775_REG_FANDIV1);
659 data->fan_div[0] = i & 0x7;
660 data->fan_div[1] = (i & 0x70) >> 4;
661 i = nct6775_read_value(data, NCT6775_REG_FANDIV2);
662 data->fan_div[2] = i & 0x7;
663 if (data->has_fan & (1<<3))
664 data->fan_div[3] = (i & 0x70) >> 4;
665}
666
667static void nct6775_update_fan_div_common(struct nct6775_data *data)
668{
669 if (data->kind == nct6775)
670 nct6775_update_fan_div(data);
671}
672
673static void nct6775_init_fan_div(struct nct6775_data *data)
674{
675 int i;
676
677 nct6775_update_fan_div_common(data);
678 /*
679 * For all fans, start with highest divider value if the divider
680 * register is not initialized. This ensures that we get a
681 * reading from the fan count register, even if it is not optimal.
682 * We'll compute a better divider later on.
683 */
684 for (i = 0; i < 3; i++) {
685 if (!(data->has_fan & (1 << i)))
686 continue;
687 if (data->fan_div[i] == 0) {
688 data->fan_div[i] = 7;
689 nct6775_write_fan_div_common(data, i);
690 }
691 }
692}
693
694static void nct6775_init_fan_common(struct device *dev,
695 struct nct6775_data *data)
696{
697 int i;
698 u8 reg;
699
700 if (data->has_fan_div)
701 nct6775_init_fan_div(data);
702
703 /*
704 * If fan_min is not set (0), set it to 0xff to disable it. This
705 * prevents the unnecessary warning when fanX_min is reported as 0.
706 */
707 for (i = 0; i < 5; i++) {
708 if (data->has_fan_min & (1 << i)) {
709 reg = nct6775_read_value(data, data->REG_FAN_MIN[i]);
710 if (!reg)
711 nct6775_write_value(data, data->REG_FAN_MIN[i],
712 data->has_fan_div ? 0xff
713 : 0xff1f);
714 }
715 }
716}
717
718static void nct6775_select_fan_div(struct device *dev,
719 struct nct6775_data *data, int nr, u16 reg)
720{
721 u8 fan_div = data->fan_div[nr];
722 u16 fan_min;
723
724 if (!data->has_fan_div)
725 return;
726
727 /*
728 * If we failed to measure the fan speed, or the reported value is not
729 * in the optimal range, and the clock divider can be modified,
730 * let's try that for next time.
731 */
732 if (reg == 0x00 && fan_div < 0x07)
733 fan_div++;
734 else if (reg != 0x00 && reg < 0x30 && fan_div > 0)
735 fan_div--;
736
737 if (fan_div != data->fan_div[nr]) {
738 dev_dbg(dev, "Modifying fan%d clock divider from %u to %u\n",
739 nr + 1, div_from_reg(data->fan_div[nr]),
740 div_from_reg(fan_div));
741
742 /* Preserve min limit if possible */
743 if (data->has_fan_min & (1 << nr)) {
744 fan_min = data->fan_min[nr];
745 if (fan_div > data->fan_div[nr]) {
746 if (fan_min != 255 && fan_min > 1)
747 fan_min >>= 1;
748 } else {
749 if (fan_min != 255) {
750 fan_min <<= 1;
751 if (fan_min > 254)
752 fan_min = 254;
753 }
754 }
755 if (fan_min != data->fan_min[nr]) {
756 data->fan_min[nr] = fan_min;
757 nct6775_write_value(data, data->REG_FAN_MIN[nr],
758 fan_min);
759 }
760 }
761 data->fan_div[nr] = fan_div;
762 nct6775_write_fan_div_common(data, nr);
763 }
764}
765
Guenter Roeck9de2e2e2012-05-20 19:29:48 -0700766static struct nct6775_data *nct6775_update_device(struct device *dev)
767{
768 struct nct6775_data *data = dev_get_drvdata(dev);
Guenter Roeckaa136e52012-12-04 03:26:05 -0800769 int i, j;
Guenter Roeck9de2e2e2012-05-20 19:29:48 -0700770
771 mutex_lock(&data->update_lock);
772
773 if (time_after(jiffies, data->last_updated + HZ + HZ/2)
774 || !data->valid) {
Guenter Roeck1c65dc32012-12-04 07:56:24 -0800775 /* Fan clock dividers */
776 nct6775_update_fan_div_common(data);
777
Guenter Roeck9de2e2e2012-05-20 19:29:48 -0700778 /* Measured voltages and limits */
779 for (i = 0; i < data->in_num; i++) {
780 if (!(data->have_in & (1 << i)))
781 continue;
782
783 data->in[i][0] = nct6775_read_value(data,
784 data->REG_VIN[i]);
785 data->in[i][1] = nct6775_read_value(data,
786 data->REG_IN_MINMAX[0][i]);
787 data->in[i][2] = nct6775_read_value(data,
788 data->REG_IN_MINMAX[1][i]);
789 }
790
Guenter Roeck1c65dc32012-12-04 07:56:24 -0800791 /* Measured fan speeds and limits */
792 for (i = 0; i < 5; i++) {
793 u16 reg;
794
795 if (!(data->has_fan & (1 << i)))
796 continue;
797
798 reg = nct6775_read_value(data, data->REG_FAN[i]);
799 data->rpm[i] = data->fan_from_reg(reg,
800 data->fan_div[i]);
801
802 if (data->has_fan_min & (1 << i))
803 data->fan_min[i] = nct6775_read_value(data,
804 data->REG_FAN_MIN[i]);
805
806 nct6775_select_fan_div(dev, data, i, reg);
807 }
808
Guenter Roeckaa136e52012-12-04 03:26:05 -0800809 /* Measured temperatures and limits */
810 for (i = 0; i < NUM_TEMP; i++) {
811 if (!(data->have_temp & (1 << i)))
812 continue;
813 for (j = 0; j < 4; j++) {
814 if (data->reg_temp[j][i])
815 data->temp[j][i]
816 = nct6775_read_temp(data,
817 data->reg_temp[j][i]);
818 }
819 if (!(data->have_temp_fixed & (1 << i)))
820 continue;
821 data->temp_offset[i]
822 = nct6775_read_value(data, data->REG_TEMP_OFFSET[i]);
823 }
824
Guenter Roeck9de2e2e2012-05-20 19:29:48 -0700825 data->alarms = 0;
826 for (i = 0; i < NUM_REG_ALARM; i++) {
827 u8 alarm;
828 if (!data->REG_ALARM[i])
829 continue;
830 alarm = nct6775_read_value(data, data->REG_ALARM[i]);
831 data->alarms |= ((u64)alarm) << (i << 3);
832 }
833
834 data->last_updated = jiffies;
835 data->valid = true;
836 }
837
838 mutex_unlock(&data->update_lock);
839 return data;
840}
841
842/*
843 * Sysfs callback functions
844 */
845static ssize_t
846show_in_reg(struct device *dev, struct device_attribute *attr, char *buf)
847{
848 struct nct6775_data *data = nct6775_update_device(dev);
849 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
850 int nr = sattr->nr;
851 int index = sattr->index;
852 return sprintf(buf, "%ld\n", in_from_reg(data->in[nr][index], nr));
853}
854
855static ssize_t
856store_in_reg(struct device *dev, struct device_attribute *attr, const char *buf,
857 size_t count)
858{
859 struct nct6775_data *data = dev_get_drvdata(dev);
860 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
861 int nr = sattr->nr;
862 int index = sattr->index;
863 unsigned long val;
864 int err = kstrtoul(buf, 10, &val);
865 if (err < 0)
866 return err;
867 mutex_lock(&data->update_lock);
868 data->in[nr][index] = in_to_reg(val, nr);
869 nct6775_write_value(data, data->REG_IN_MINMAX[index-1][nr],
870 data->in[nr][index]);
871 mutex_unlock(&data->update_lock);
872 return count;
873}
874
875static ssize_t
876show_alarm(struct device *dev, struct device_attribute *attr, char *buf)
877{
878 struct nct6775_data *data = nct6775_update_device(dev);
879 struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr);
880 int nr = data->ALARM_BITS[sattr->index];
881 return sprintf(buf, "%u\n",
882 (unsigned int)((data->alarms >> nr) & 0x01));
883}
884
885static SENSOR_DEVICE_ATTR_2(in0_input, S_IRUGO, show_in_reg, NULL, 0, 0);
886static SENSOR_DEVICE_ATTR_2(in1_input, S_IRUGO, show_in_reg, NULL, 1, 0);
887static SENSOR_DEVICE_ATTR_2(in2_input, S_IRUGO, show_in_reg, NULL, 2, 0);
888static SENSOR_DEVICE_ATTR_2(in3_input, S_IRUGO, show_in_reg, NULL, 3, 0);
889static SENSOR_DEVICE_ATTR_2(in4_input, S_IRUGO, show_in_reg, NULL, 4, 0);
890static SENSOR_DEVICE_ATTR_2(in5_input, S_IRUGO, show_in_reg, NULL, 5, 0);
891static SENSOR_DEVICE_ATTR_2(in6_input, S_IRUGO, show_in_reg, NULL, 6, 0);
892static SENSOR_DEVICE_ATTR_2(in7_input, S_IRUGO, show_in_reg, NULL, 7, 0);
893static SENSOR_DEVICE_ATTR_2(in8_input, S_IRUGO, show_in_reg, NULL, 8, 0);
894static SENSOR_DEVICE_ATTR_2(in9_input, S_IRUGO, show_in_reg, NULL, 9, 0);
895static SENSOR_DEVICE_ATTR_2(in10_input, S_IRUGO, show_in_reg, NULL, 10, 0);
896static SENSOR_DEVICE_ATTR_2(in11_input, S_IRUGO, show_in_reg, NULL, 11, 0);
897static SENSOR_DEVICE_ATTR_2(in12_input, S_IRUGO, show_in_reg, NULL, 12, 0);
898static SENSOR_DEVICE_ATTR_2(in13_input, S_IRUGO, show_in_reg, NULL, 13, 0);
899static SENSOR_DEVICE_ATTR_2(in14_input, S_IRUGO, show_in_reg, NULL, 14, 0);
900
901static SENSOR_DEVICE_ATTR(in0_alarm, S_IRUGO, show_alarm, NULL, 0);
902static SENSOR_DEVICE_ATTR(in1_alarm, S_IRUGO, show_alarm, NULL, 1);
903static SENSOR_DEVICE_ATTR(in2_alarm, S_IRUGO, show_alarm, NULL, 2);
904static SENSOR_DEVICE_ATTR(in3_alarm, S_IRUGO, show_alarm, NULL, 3);
905static SENSOR_DEVICE_ATTR(in4_alarm, S_IRUGO, show_alarm, NULL, 4);
906static SENSOR_DEVICE_ATTR(in5_alarm, S_IRUGO, show_alarm, NULL, 5);
907static SENSOR_DEVICE_ATTR(in6_alarm, S_IRUGO, show_alarm, NULL, 6);
908static SENSOR_DEVICE_ATTR(in7_alarm, S_IRUGO, show_alarm, NULL, 7);
909static SENSOR_DEVICE_ATTR(in8_alarm, S_IRUGO, show_alarm, NULL, 8);
910static SENSOR_DEVICE_ATTR(in9_alarm, S_IRUGO, show_alarm, NULL, 9);
911static SENSOR_DEVICE_ATTR(in10_alarm, S_IRUGO, show_alarm, NULL, 10);
912static SENSOR_DEVICE_ATTR(in11_alarm, S_IRUGO, show_alarm, NULL, 11);
913static SENSOR_DEVICE_ATTR(in12_alarm, S_IRUGO, show_alarm, NULL, 12);
914static SENSOR_DEVICE_ATTR(in13_alarm, S_IRUGO, show_alarm, NULL, 13);
915static SENSOR_DEVICE_ATTR(in14_alarm, S_IRUGO, show_alarm, NULL, 14);
916
917static SENSOR_DEVICE_ATTR_2(in0_min, S_IWUSR | S_IRUGO, show_in_reg,
918 store_in_reg, 0, 1);
919static SENSOR_DEVICE_ATTR_2(in1_min, S_IWUSR | S_IRUGO, show_in_reg,
920 store_in_reg, 1, 1);
921static SENSOR_DEVICE_ATTR_2(in2_min, S_IWUSR | S_IRUGO, show_in_reg,
922 store_in_reg, 2, 1);
923static SENSOR_DEVICE_ATTR_2(in3_min, S_IWUSR | S_IRUGO, show_in_reg,
924 store_in_reg, 3, 1);
925static SENSOR_DEVICE_ATTR_2(in4_min, S_IWUSR | S_IRUGO, show_in_reg,
926 store_in_reg, 4, 1);
927static SENSOR_DEVICE_ATTR_2(in5_min, S_IWUSR | S_IRUGO, show_in_reg,
928 store_in_reg, 5, 1);
929static SENSOR_DEVICE_ATTR_2(in6_min, S_IWUSR | S_IRUGO, show_in_reg,
930 store_in_reg, 6, 1);
931static SENSOR_DEVICE_ATTR_2(in7_min, S_IWUSR | S_IRUGO, show_in_reg,
932 store_in_reg, 7, 1);
933static SENSOR_DEVICE_ATTR_2(in8_min, S_IWUSR | S_IRUGO, show_in_reg,
934 store_in_reg, 8, 1);
935static SENSOR_DEVICE_ATTR_2(in9_min, S_IWUSR | S_IRUGO, show_in_reg,
936 store_in_reg, 9, 1);
937static SENSOR_DEVICE_ATTR_2(in10_min, S_IWUSR | S_IRUGO, show_in_reg,
938 store_in_reg, 10, 1);
939static SENSOR_DEVICE_ATTR_2(in11_min, S_IWUSR | S_IRUGO, show_in_reg,
940 store_in_reg, 11, 1);
941static SENSOR_DEVICE_ATTR_2(in12_min, S_IWUSR | S_IRUGO, show_in_reg,
942 store_in_reg, 12, 1);
943static SENSOR_DEVICE_ATTR_2(in13_min, S_IWUSR | S_IRUGO, show_in_reg,
944 store_in_reg, 13, 1);
945static SENSOR_DEVICE_ATTR_2(in14_min, S_IWUSR | S_IRUGO, show_in_reg,
946 store_in_reg, 14, 1);
947
948static SENSOR_DEVICE_ATTR_2(in0_max, S_IWUSR | S_IRUGO, show_in_reg,
949 store_in_reg, 0, 2);
950static SENSOR_DEVICE_ATTR_2(in1_max, S_IWUSR | S_IRUGO, show_in_reg,
951 store_in_reg, 1, 2);
952static SENSOR_DEVICE_ATTR_2(in2_max, S_IWUSR | S_IRUGO, show_in_reg,
953 store_in_reg, 2, 2);
954static SENSOR_DEVICE_ATTR_2(in3_max, S_IWUSR | S_IRUGO, show_in_reg,
955 store_in_reg, 3, 2);
956static SENSOR_DEVICE_ATTR_2(in4_max, S_IWUSR | S_IRUGO, show_in_reg,
957 store_in_reg, 4, 2);
958static SENSOR_DEVICE_ATTR_2(in5_max, S_IWUSR | S_IRUGO, show_in_reg,
959 store_in_reg, 5, 2);
960static SENSOR_DEVICE_ATTR_2(in6_max, S_IWUSR | S_IRUGO, show_in_reg,
961 store_in_reg, 6, 2);
962static SENSOR_DEVICE_ATTR_2(in7_max, S_IWUSR | S_IRUGO, show_in_reg,
963 store_in_reg, 7, 2);
964static SENSOR_DEVICE_ATTR_2(in8_max, S_IWUSR | S_IRUGO, show_in_reg,
965 store_in_reg, 8, 2);
966static SENSOR_DEVICE_ATTR_2(in9_max, S_IWUSR | S_IRUGO, show_in_reg,
967 store_in_reg, 9, 2);
968static SENSOR_DEVICE_ATTR_2(in10_max, S_IWUSR | S_IRUGO, show_in_reg,
969 store_in_reg, 10, 2);
970static SENSOR_DEVICE_ATTR_2(in11_max, S_IWUSR | S_IRUGO, show_in_reg,
971 store_in_reg, 11, 2);
972static SENSOR_DEVICE_ATTR_2(in12_max, S_IWUSR | S_IRUGO, show_in_reg,
973 store_in_reg, 12, 2);
974static SENSOR_DEVICE_ATTR_2(in13_max, S_IWUSR | S_IRUGO, show_in_reg,
975 store_in_reg, 13, 2);
976static SENSOR_DEVICE_ATTR_2(in14_max, S_IWUSR | S_IRUGO, show_in_reg,
977 store_in_reg, 14, 2);
978
979static struct attribute *nct6775_attributes_in[15][5] = {
980 {
981 &sensor_dev_attr_in0_input.dev_attr.attr,
982 &sensor_dev_attr_in0_min.dev_attr.attr,
983 &sensor_dev_attr_in0_max.dev_attr.attr,
984 &sensor_dev_attr_in0_alarm.dev_attr.attr,
985 NULL
986 },
987 {
988 &sensor_dev_attr_in1_input.dev_attr.attr,
989 &sensor_dev_attr_in1_min.dev_attr.attr,
990 &sensor_dev_attr_in1_max.dev_attr.attr,
991 &sensor_dev_attr_in1_alarm.dev_attr.attr,
992 NULL
993 },
994 {
995 &sensor_dev_attr_in2_input.dev_attr.attr,
996 &sensor_dev_attr_in2_min.dev_attr.attr,
997 &sensor_dev_attr_in2_max.dev_attr.attr,
998 &sensor_dev_attr_in2_alarm.dev_attr.attr,
999 NULL
1000 },
1001 {
1002 &sensor_dev_attr_in3_input.dev_attr.attr,
1003 &sensor_dev_attr_in3_min.dev_attr.attr,
1004 &sensor_dev_attr_in3_max.dev_attr.attr,
1005 &sensor_dev_attr_in3_alarm.dev_attr.attr,
1006 NULL
1007 },
1008 {
1009 &sensor_dev_attr_in4_input.dev_attr.attr,
1010 &sensor_dev_attr_in4_min.dev_attr.attr,
1011 &sensor_dev_attr_in4_max.dev_attr.attr,
1012 &sensor_dev_attr_in4_alarm.dev_attr.attr,
1013 NULL
1014 },
1015 {
1016 &sensor_dev_attr_in5_input.dev_attr.attr,
1017 &sensor_dev_attr_in5_min.dev_attr.attr,
1018 &sensor_dev_attr_in5_max.dev_attr.attr,
1019 &sensor_dev_attr_in5_alarm.dev_attr.attr,
1020 NULL
1021 },
1022 {
1023 &sensor_dev_attr_in6_input.dev_attr.attr,
1024 &sensor_dev_attr_in6_min.dev_attr.attr,
1025 &sensor_dev_attr_in6_max.dev_attr.attr,
1026 &sensor_dev_attr_in6_alarm.dev_attr.attr,
1027 NULL
1028 },
1029 {
1030 &sensor_dev_attr_in7_input.dev_attr.attr,
1031 &sensor_dev_attr_in7_min.dev_attr.attr,
1032 &sensor_dev_attr_in7_max.dev_attr.attr,
1033 &sensor_dev_attr_in7_alarm.dev_attr.attr,
1034 NULL
1035 },
1036 {
1037 &sensor_dev_attr_in8_input.dev_attr.attr,
1038 &sensor_dev_attr_in8_min.dev_attr.attr,
1039 &sensor_dev_attr_in8_max.dev_attr.attr,
1040 &sensor_dev_attr_in8_alarm.dev_attr.attr,
1041 NULL
1042 },
1043 {
1044 &sensor_dev_attr_in9_input.dev_attr.attr,
1045 &sensor_dev_attr_in9_min.dev_attr.attr,
1046 &sensor_dev_attr_in9_max.dev_attr.attr,
1047 &sensor_dev_attr_in9_alarm.dev_attr.attr,
1048 NULL
1049 },
1050 {
1051 &sensor_dev_attr_in10_input.dev_attr.attr,
1052 &sensor_dev_attr_in10_min.dev_attr.attr,
1053 &sensor_dev_attr_in10_max.dev_attr.attr,
1054 &sensor_dev_attr_in10_alarm.dev_attr.attr,
1055 NULL
1056 },
1057 {
1058 &sensor_dev_attr_in11_input.dev_attr.attr,
1059 &sensor_dev_attr_in11_min.dev_attr.attr,
1060 &sensor_dev_attr_in11_max.dev_attr.attr,
1061 &sensor_dev_attr_in11_alarm.dev_attr.attr,
1062 NULL
1063 },
1064 {
1065 &sensor_dev_attr_in12_input.dev_attr.attr,
1066 &sensor_dev_attr_in12_min.dev_attr.attr,
1067 &sensor_dev_attr_in12_max.dev_attr.attr,
1068 &sensor_dev_attr_in12_alarm.dev_attr.attr,
1069 NULL
1070 },
1071 {
1072 &sensor_dev_attr_in13_input.dev_attr.attr,
1073 &sensor_dev_attr_in13_min.dev_attr.attr,
1074 &sensor_dev_attr_in13_max.dev_attr.attr,
1075 &sensor_dev_attr_in13_alarm.dev_attr.attr,
1076 NULL
1077 },
1078 {
1079 &sensor_dev_attr_in14_input.dev_attr.attr,
1080 &sensor_dev_attr_in14_min.dev_attr.attr,
1081 &sensor_dev_attr_in14_max.dev_attr.attr,
1082 &sensor_dev_attr_in14_alarm.dev_attr.attr,
1083 NULL
1084 },
1085};
1086
1087static const struct attribute_group nct6775_group_in[15] = {
1088 { .attrs = nct6775_attributes_in[0] },
1089 { .attrs = nct6775_attributes_in[1] },
1090 { .attrs = nct6775_attributes_in[2] },
1091 { .attrs = nct6775_attributes_in[3] },
1092 { .attrs = nct6775_attributes_in[4] },
1093 { .attrs = nct6775_attributes_in[5] },
1094 { .attrs = nct6775_attributes_in[6] },
1095 { .attrs = nct6775_attributes_in[7] },
1096 { .attrs = nct6775_attributes_in[8] },
1097 { .attrs = nct6775_attributes_in[9] },
1098 { .attrs = nct6775_attributes_in[10] },
1099 { .attrs = nct6775_attributes_in[11] },
1100 { .attrs = nct6775_attributes_in[12] },
1101 { .attrs = nct6775_attributes_in[13] },
1102 { .attrs = nct6775_attributes_in[14] },
1103};
1104
1105static ssize_t
Guenter Roeck1c65dc32012-12-04 07:56:24 -08001106show_fan(struct device *dev, struct device_attribute *attr, char *buf)
1107{
1108 struct nct6775_data *data = nct6775_update_device(dev);
1109 struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr);
1110 int nr = sattr->index;
1111 return sprintf(buf, "%d\n", data->rpm[nr]);
1112}
1113
1114static ssize_t
1115show_fan_min(struct device *dev, struct device_attribute *attr, char *buf)
1116{
1117 struct nct6775_data *data = nct6775_update_device(dev);
1118 struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr);
1119 int nr = sattr->index;
1120 return sprintf(buf, "%d\n",
1121 data->fan_from_reg_min(data->fan_min[nr],
1122 data->fan_div[nr]));
1123}
1124
1125static ssize_t
1126show_fan_div(struct device *dev, struct device_attribute *attr, char *buf)
1127{
1128 struct nct6775_data *data = nct6775_update_device(dev);
1129 struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr);
1130 int nr = sattr->index;
1131 return sprintf(buf, "%u\n", div_from_reg(data->fan_div[nr]));
1132}
1133
1134static ssize_t
1135store_fan_min(struct device *dev, struct device_attribute *attr,
1136 const char *buf, size_t count)
1137{
1138 struct nct6775_data *data = dev_get_drvdata(dev);
1139 struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr);
1140 int nr = sattr->index;
1141 unsigned long val;
1142 int err;
1143 unsigned int reg;
1144 u8 new_div;
1145
1146 err = kstrtoul(buf, 10, &val);
1147 if (err < 0)
1148 return err;
1149
1150 mutex_lock(&data->update_lock);
1151 if (!data->has_fan_div) {
1152 /* NCT6776F or NCT6779D; we know this is a 13 bit register */
1153 if (!val) {
1154 val = 0xff1f;
1155 } else {
1156 if (val > 1350000U)
1157 val = 135000U;
1158 val = 1350000U / val;
1159 val = (val & 0x1f) | ((val << 3) & 0xff00);
1160 }
1161 data->fan_min[nr] = val;
1162 goto write_min; /* Leave fan divider alone */
1163 }
1164 if (!val) {
1165 /* No min limit, alarm disabled */
1166 data->fan_min[nr] = 255;
1167 new_div = data->fan_div[nr]; /* No change */
1168 dev_info(dev, "fan%u low limit and alarm disabled\n", nr + 1);
1169 goto write_div;
1170 }
1171 reg = 1350000U / val;
1172 if (reg >= 128 * 255) {
1173 /*
1174 * Speed below this value cannot possibly be represented,
1175 * even with the highest divider (128)
1176 */
1177 data->fan_min[nr] = 254;
1178 new_div = 7; /* 128 == (1 << 7) */
1179 dev_warn(dev,
1180 "fan%u low limit %lu below minimum %u, set to minimum\n",
1181 nr + 1, val, data->fan_from_reg_min(254, 7));
1182 } else if (!reg) {
1183 /*
1184 * Speed above this value cannot possibly be represented,
1185 * even with the lowest divider (1)
1186 */
1187 data->fan_min[nr] = 1;
1188 new_div = 0; /* 1 == (1 << 0) */
1189 dev_warn(dev,
1190 "fan%u low limit %lu above maximum %u, set to maximum\n",
1191 nr + 1, val, data->fan_from_reg_min(1, 0));
1192 } else {
1193 /*
1194 * Automatically pick the best divider, i.e. the one such
1195 * that the min limit will correspond to a register value
1196 * in the 96..192 range
1197 */
1198 new_div = 0;
1199 while (reg > 192 && new_div < 7) {
1200 reg >>= 1;
1201 new_div++;
1202 }
1203 data->fan_min[nr] = reg;
1204 }
1205
1206write_div:
1207 /*
1208 * Write both the fan clock divider (if it changed) and the new
1209 * fan min (unconditionally)
1210 */
1211 if (new_div != data->fan_div[nr]) {
1212 dev_dbg(dev, "fan%u clock divider changed from %u to %u\n",
1213 nr + 1, div_from_reg(data->fan_div[nr]),
1214 div_from_reg(new_div));
1215 data->fan_div[nr] = new_div;
1216 nct6775_write_fan_div_common(data, nr);
1217 /* Give the chip time to sample a new speed value */
1218 data->last_updated = jiffies;
1219 }
1220
1221write_min:
1222 nct6775_write_value(data, data->REG_FAN_MIN[nr], data->fan_min[nr]);
1223 mutex_unlock(&data->update_lock);
1224
1225 return count;
1226}
1227
1228static struct sensor_device_attribute sda_fan_input[] = {
1229 SENSOR_ATTR(fan1_input, S_IRUGO, show_fan, NULL, 0),
1230 SENSOR_ATTR(fan2_input, S_IRUGO, show_fan, NULL, 1),
1231 SENSOR_ATTR(fan3_input, S_IRUGO, show_fan, NULL, 2),
1232 SENSOR_ATTR(fan4_input, S_IRUGO, show_fan, NULL, 3),
1233 SENSOR_ATTR(fan5_input, S_IRUGO, show_fan, NULL, 4),
1234};
1235
1236static struct sensor_device_attribute sda_fan_alarm[] = {
1237 SENSOR_ATTR(fan1_alarm, S_IRUGO, show_alarm, NULL, FAN_ALARM_BASE),
1238 SENSOR_ATTR(fan2_alarm, S_IRUGO, show_alarm, NULL, FAN_ALARM_BASE + 1),
1239 SENSOR_ATTR(fan3_alarm, S_IRUGO, show_alarm, NULL, FAN_ALARM_BASE + 2),
1240 SENSOR_ATTR(fan4_alarm, S_IRUGO, show_alarm, NULL, FAN_ALARM_BASE + 3),
1241 SENSOR_ATTR(fan5_alarm, S_IRUGO, show_alarm, NULL, FAN_ALARM_BASE + 4),
1242};
1243
1244static struct sensor_device_attribute sda_fan_min[] = {
1245 SENSOR_ATTR(fan1_min, S_IWUSR | S_IRUGO, show_fan_min,
1246 store_fan_min, 0),
1247 SENSOR_ATTR(fan2_min, S_IWUSR | S_IRUGO, show_fan_min,
1248 store_fan_min, 1),
1249 SENSOR_ATTR(fan3_min, S_IWUSR | S_IRUGO, show_fan_min,
1250 store_fan_min, 2),
1251 SENSOR_ATTR(fan4_min, S_IWUSR | S_IRUGO, show_fan_min,
1252 store_fan_min, 3),
1253 SENSOR_ATTR(fan5_min, S_IWUSR | S_IRUGO, show_fan_min,
1254 store_fan_min, 4),
1255};
1256
1257static struct sensor_device_attribute sda_fan_div[] = {
1258 SENSOR_ATTR(fan1_div, S_IRUGO, show_fan_div, NULL, 0),
1259 SENSOR_ATTR(fan2_div, S_IRUGO, show_fan_div, NULL, 1),
1260 SENSOR_ATTR(fan3_div, S_IRUGO, show_fan_div, NULL, 2),
1261 SENSOR_ATTR(fan4_div, S_IRUGO, show_fan_div, NULL, 3),
1262 SENSOR_ATTR(fan5_div, S_IRUGO, show_fan_div, NULL, 4),
1263};
1264
1265static ssize_t
Guenter Roeckaa136e52012-12-04 03:26:05 -08001266show_temp_label(struct device *dev, struct device_attribute *attr, char *buf)
1267{
1268 struct nct6775_data *data = nct6775_update_device(dev);
1269 struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr);
1270 int nr = sattr->index;
1271 return sprintf(buf, "%s\n", data->temp_label[data->temp_src[nr]]);
1272}
1273
1274static ssize_t
1275show_temp(struct device *dev, struct device_attribute *attr, char *buf)
1276{
1277 struct nct6775_data *data = nct6775_update_device(dev);
1278 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
1279 int nr = sattr->nr;
1280 int index = sattr->index;
1281
1282 return sprintf(buf, "%d\n", LM75_TEMP_FROM_REG(data->temp[index][nr]));
1283}
1284
1285static ssize_t
1286store_temp(struct device *dev, struct device_attribute *attr, const char *buf,
1287 size_t count)
1288{
1289 struct nct6775_data *data = dev_get_drvdata(dev);
1290 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
1291 int nr = sattr->nr;
1292 int index = sattr->index;
1293 int err;
1294 long val;
1295
1296 err = kstrtol(buf, 10, &val);
1297 if (err < 0)
1298 return err;
1299
1300 mutex_lock(&data->update_lock);
1301 data->temp[index][nr] = LM75_TEMP_TO_REG(val);
1302 nct6775_write_temp(data, data->reg_temp[index][nr],
1303 data->temp[index][nr]);
1304 mutex_unlock(&data->update_lock);
1305 return count;
1306}
1307
1308static ssize_t
1309show_temp_offset(struct device *dev, struct device_attribute *attr, char *buf)
1310{
1311 struct nct6775_data *data = nct6775_update_device(dev);
1312 struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr);
1313
1314 return sprintf(buf, "%d\n", data->temp_offset[sattr->index] * 1000);
1315}
1316
1317static ssize_t
1318store_temp_offset(struct device *dev, struct device_attribute *attr,
1319 const char *buf, size_t count)
1320{
1321 struct nct6775_data *data = dev_get_drvdata(dev);
1322 struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr);
1323 int nr = sattr->index;
1324 long val;
1325 int err;
1326
1327 err = kstrtol(buf, 10, &val);
1328 if (err < 0)
1329 return err;
1330
1331 val = clamp_val(DIV_ROUND_CLOSEST(val, 1000), -128, 127);
1332
1333 mutex_lock(&data->update_lock);
1334 data->temp_offset[nr] = val;
1335 nct6775_write_value(data, data->REG_TEMP_OFFSET[nr], val);
1336 mutex_unlock(&data->update_lock);
1337
1338 return count;
1339}
1340
1341static ssize_t
1342show_temp_type(struct device *dev, struct device_attribute *attr, char *buf)
1343{
1344 struct nct6775_data *data = nct6775_update_device(dev);
1345 struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr);
1346 int nr = sattr->index;
1347 return sprintf(buf, "%d\n", (int)data->temp_type[nr]);
1348}
1349
1350static ssize_t
1351store_temp_type(struct device *dev, struct device_attribute *attr,
1352 const char *buf, size_t count)
1353{
1354 struct nct6775_data *data = nct6775_update_device(dev);
1355 struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr);
1356 int nr = sattr->index;
1357 unsigned long val;
1358 int err;
1359 u8 vbat, diode, bit;
1360
1361 err = kstrtoul(buf, 10, &val);
1362 if (err < 0)
1363 return err;
1364
1365 if (val != 1 && val != 3 && val != 4)
1366 return -EINVAL;
1367
1368 mutex_lock(&data->update_lock);
1369
1370 data->temp_type[nr] = val;
1371 vbat = nct6775_read_value(data, data->REG_VBAT) & ~(0x02 << nr);
1372 diode = nct6775_read_value(data, data->REG_DIODE) & ~(0x02 << nr);
1373 bit = 0x02 << nr;
1374 switch (val) {
1375 case 1: /* CPU diode (diode, current mode) */
1376 vbat |= bit;
1377 diode |= bit;
1378 break;
1379 case 3: /* diode, voltage mode */
1380 vbat |= bit;
1381 break;
1382 case 4: /* thermistor */
1383 break;
1384 }
1385 nct6775_write_value(data, data->REG_VBAT, vbat);
1386 nct6775_write_value(data, data->REG_DIODE, diode);
1387
1388 mutex_unlock(&data->update_lock);
1389 return count;
1390}
1391
1392static struct sensor_device_attribute_2 sda_temp_input[] = {
1393 SENSOR_ATTR_2(temp1_input, S_IRUGO, show_temp, NULL, 0, 0),
1394 SENSOR_ATTR_2(temp2_input, S_IRUGO, show_temp, NULL, 1, 0),
1395 SENSOR_ATTR_2(temp3_input, S_IRUGO, show_temp, NULL, 2, 0),
1396 SENSOR_ATTR_2(temp4_input, S_IRUGO, show_temp, NULL, 3, 0),
1397 SENSOR_ATTR_2(temp5_input, S_IRUGO, show_temp, NULL, 4, 0),
1398 SENSOR_ATTR_2(temp6_input, S_IRUGO, show_temp, NULL, 5, 0),
1399 SENSOR_ATTR_2(temp7_input, S_IRUGO, show_temp, NULL, 6, 0),
1400 SENSOR_ATTR_2(temp8_input, S_IRUGO, show_temp, NULL, 7, 0),
1401 SENSOR_ATTR_2(temp9_input, S_IRUGO, show_temp, NULL, 8, 0),
1402 SENSOR_ATTR_2(temp10_input, S_IRUGO, show_temp, NULL, 9, 0),
1403};
1404
1405static struct sensor_device_attribute sda_temp_label[] = {
1406 SENSOR_ATTR(temp1_label, S_IRUGO, show_temp_label, NULL, 0),
1407 SENSOR_ATTR(temp2_label, S_IRUGO, show_temp_label, NULL, 1),
1408 SENSOR_ATTR(temp3_label, S_IRUGO, show_temp_label, NULL, 2),
1409 SENSOR_ATTR(temp4_label, S_IRUGO, show_temp_label, NULL, 3),
1410 SENSOR_ATTR(temp5_label, S_IRUGO, show_temp_label, NULL, 4),
1411 SENSOR_ATTR(temp6_label, S_IRUGO, show_temp_label, NULL, 5),
1412 SENSOR_ATTR(temp7_label, S_IRUGO, show_temp_label, NULL, 6),
1413 SENSOR_ATTR(temp8_label, S_IRUGO, show_temp_label, NULL, 7),
1414 SENSOR_ATTR(temp9_label, S_IRUGO, show_temp_label, NULL, 8),
1415 SENSOR_ATTR(temp10_label, S_IRUGO, show_temp_label, NULL, 9),
1416};
1417
1418static struct sensor_device_attribute_2 sda_temp_max[] = {
1419 SENSOR_ATTR_2(temp1_max, S_IRUGO | S_IWUSR, show_temp, store_temp,
1420 0, 1),
1421 SENSOR_ATTR_2(temp2_max, S_IRUGO | S_IWUSR, show_temp, store_temp,
1422 1, 1),
1423 SENSOR_ATTR_2(temp3_max, S_IRUGO | S_IWUSR, show_temp, store_temp,
1424 2, 1),
1425 SENSOR_ATTR_2(temp4_max, S_IRUGO | S_IWUSR, show_temp, store_temp,
1426 3, 1),
1427 SENSOR_ATTR_2(temp5_max, S_IRUGO | S_IWUSR, show_temp, store_temp,
1428 4, 1),
1429 SENSOR_ATTR_2(temp6_max, S_IRUGO | S_IWUSR, show_temp, store_temp,
1430 5, 1),
1431 SENSOR_ATTR_2(temp7_max, S_IRUGO | S_IWUSR, show_temp, store_temp,
1432 6, 1),
1433 SENSOR_ATTR_2(temp8_max, S_IRUGO | S_IWUSR, show_temp, store_temp,
1434 7, 1),
1435 SENSOR_ATTR_2(temp9_max, S_IRUGO | S_IWUSR, show_temp, store_temp,
1436 8, 1),
1437 SENSOR_ATTR_2(temp10_max, S_IRUGO | S_IWUSR, show_temp, store_temp,
1438 9, 1),
1439};
1440
1441static struct sensor_device_attribute_2 sda_temp_max_hyst[] = {
1442 SENSOR_ATTR_2(temp1_max_hyst, S_IRUGO | S_IWUSR, show_temp, store_temp,
1443 0, 2),
1444 SENSOR_ATTR_2(temp2_max_hyst, S_IRUGO | S_IWUSR, show_temp, store_temp,
1445 1, 2),
1446 SENSOR_ATTR_2(temp3_max_hyst, S_IRUGO | S_IWUSR, show_temp, store_temp,
1447 2, 2),
1448 SENSOR_ATTR_2(temp4_max_hyst, S_IRUGO | S_IWUSR, show_temp, store_temp,
1449 3, 2),
1450 SENSOR_ATTR_2(temp5_max_hyst, S_IRUGO | S_IWUSR, show_temp, store_temp,
1451 4, 2),
1452 SENSOR_ATTR_2(temp6_max_hyst, S_IRUGO | S_IWUSR, show_temp, store_temp,
1453 5, 2),
1454 SENSOR_ATTR_2(temp7_max_hyst, S_IRUGO | S_IWUSR, show_temp, store_temp,
1455 6, 2),
1456 SENSOR_ATTR_2(temp8_max_hyst, S_IRUGO | S_IWUSR, show_temp, store_temp,
1457 7, 2),
1458 SENSOR_ATTR_2(temp9_max_hyst, S_IRUGO | S_IWUSR, show_temp, store_temp,
1459 8, 2),
1460 SENSOR_ATTR_2(temp10_max_hyst, S_IRUGO | S_IWUSR, show_temp, store_temp,
1461 9, 2),
1462};
1463
1464static struct sensor_device_attribute_2 sda_temp_crit[] = {
1465 SENSOR_ATTR_2(temp1_crit, S_IRUGO | S_IWUSR, show_temp, store_temp,
1466 0, 3),
1467 SENSOR_ATTR_2(temp2_crit, S_IRUGO | S_IWUSR, show_temp, store_temp,
1468 1, 3),
1469 SENSOR_ATTR_2(temp3_crit, S_IRUGO | S_IWUSR, show_temp, store_temp,
1470 2, 3),
1471 SENSOR_ATTR_2(temp4_crit, S_IRUGO | S_IWUSR, show_temp, store_temp,
1472 3, 3),
1473 SENSOR_ATTR_2(temp5_crit, S_IRUGO | S_IWUSR, show_temp, store_temp,
1474 4, 3),
1475 SENSOR_ATTR_2(temp6_crit, S_IRUGO | S_IWUSR, show_temp, store_temp,
1476 5, 3),
1477 SENSOR_ATTR_2(temp7_crit, S_IRUGO | S_IWUSR, show_temp, store_temp,
1478 6, 3),
1479 SENSOR_ATTR_2(temp8_crit, S_IRUGO | S_IWUSR, show_temp, store_temp,
1480 7, 3),
1481 SENSOR_ATTR_2(temp9_crit, S_IRUGO | S_IWUSR, show_temp, store_temp,
1482 8, 3),
1483 SENSOR_ATTR_2(temp10_crit, S_IRUGO | S_IWUSR, show_temp, store_temp,
1484 9, 3),
1485};
1486
1487static struct sensor_device_attribute sda_temp_offset[] = {
1488 SENSOR_ATTR(temp1_offset, S_IRUGO | S_IWUSR, show_temp_offset,
1489 store_temp_offset, 0),
1490 SENSOR_ATTR(temp2_offset, S_IRUGO | S_IWUSR, show_temp_offset,
1491 store_temp_offset, 1),
1492 SENSOR_ATTR(temp3_offset, S_IRUGO | S_IWUSR, show_temp_offset,
1493 store_temp_offset, 2),
1494 SENSOR_ATTR(temp4_offset, S_IRUGO | S_IWUSR, show_temp_offset,
1495 store_temp_offset, 3),
1496 SENSOR_ATTR(temp5_offset, S_IRUGO | S_IWUSR, show_temp_offset,
1497 store_temp_offset, 4),
1498 SENSOR_ATTR(temp6_offset, S_IRUGO | S_IWUSR, show_temp_offset,
1499 store_temp_offset, 5),
1500};
1501
1502static struct sensor_device_attribute sda_temp_type[] = {
1503 SENSOR_ATTR(temp1_type, S_IRUGO | S_IWUSR, show_temp_type,
1504 store_temp_type, 0),
1505 SENSOR_ATTR(temp2_type, S_IRUGO | S_IWUSR, show_temp_type,
1506 store_temp_type, 1),
1507 SENSOR_ATTR(temp3_type, S_IRUGO | S_IWUSR, show_temp_type,
1508 store_temp_type, 2),
1509 SENSOR_ATTR(temp4_type, S_IRUGO | S_IWUSR, show_temp_type,
1510 store_temp_type, 3),
1511 SENSOR_ATTR(temp5_type, S_IRUGO | S_IWUSR, show_temp_type,
1512 store_temp_type, 4),
1513 SENSOR_ATTR(temp6_type, S_IRUGO | S_IWUSR, show_temp_type,
1514 store_temp_type, 5),
1515};
1516
1517static struct sensor_device_attribute sda_temp_alarm[] = {
1518 SENSOR_ATTR(temp1_alarm, S_IRUGO, show_alarm, NULL,
1519 TEMP_ALARM_BASE),
1520 SENSOR_ATTR(temp2_alarm, S_IRUGO, show_alarm, NULL,
1521 TEMP_ALARM_BASE + 1),
1522 SENSOR_ATTR(temp3_alarm, S_IRUGO, show_alarm, NULL,
1523 TEMP_ALARM_BASE + 2),
1524 SENSOR_ATTR(temp4_alarm, S_IRUGO, show_alarm, NULL,
1525 TEMP_ALARM_BASE + 3),
1526 SENSOR_ATTR(temp5_alarm, S_IRUGO, show_alarm, NULL,
1527 TEMP_ALARM_BASE + 4),
1528 SENSOR_ATTR(temp6_alarm, S_IRUGO, show_alarm, NULL,
1529 TEMP_ALARM_BASE + 5),
1530};
1531
1532#define NUM_TEMP_ALARM ARRAY_SIZE(sda_temp_alarm)
1533
1534static ssize_t
Guenter Roeck9de2e2e2012-05-20 19:29:48 -07001535show_name(struct device *dev, struct device_attribute *attr, char *buf)
1536{
1537 struct nct6775_data *data = dev_get_drvdata(dev);
1538
1539 return sprintf(buf, "%s\n", data->name);
1540}
1541
1542static DEVICE_ATTR(name, S_IRUGO, show_name, NULL);
1543
1544static ssize_t
1545show_vid(struct device *dev, struct device_attribute *attr, char *buf)
1546{
1547 struct nct6775_data *data = dev_get_drvdata(dev);
1548 return sprintf(buf, "%d\n", vid_from_reg(data->vid, data->vrm));
1549}
1550
1551static DEVICE_ATTR(cpu0_vid, S_IRUGO, show_vid, NULL);
1552
Guenter Roecka6bd5872012-12-04 03:13:34 -08001553/* Case open detection */
1554
1555static ssize_t
1556clear_caseopen(struct device *dev, struct device_attribute *attr,
1557 const char *buf, size_t count)
1558{
1559 struct nct6775_data *data = dev_get_drvdata(dev);
1560 struct nct6775_sio_data *sio_data = dev->platform_data;
1561 int nr = to_sensor_dev_attr(attr)->index - INTRUSION_ALARM_BASE;
1562 unsigned long val;
1563 u8 reg;
1564 int ret;
1565
1566 if (kstrtoul(buf, 10, &val) || val != 0)
1567 return -EINVAL;
1568
1569 mutex_lock(&data->update_lock);
1570
1571 /*
1572 * Use CR registers to clear caseopen status.
1573 * The CR registers are the same for all chips, and not all chips
1574 * support clearing the caseopen status through "regular" registers.
1575 */
1576 ret = superio_enter(sio_data->sioreg);
1577 if (ret) {
1578 count = ret;
1579 goto error;
1580 }
1581
1582 superio_select(sio_data->sioreg, NCT6775_LD_ACPI);
1583 reg = superio_inb(sio_data->sioreg, NCT6775_REG_CR_CASEOPEN_CLR[nr]);
1584 reg |= NCT6775_CR_CASEOPEN_CLR_MASK[nr];
1585 superio_outb(sio_data->sioreg, NCT6775_REG_CR_CASEOPEN_CLR[nr], reg);
1586 reg &= ~NCT6775_CR_CASEOPEN_CLR_MASK[nr];
1587 superio_outb(sio_data->sioreg, NCT6775_REG_CR_CASEOPEN_CLR[nr], reg);
1588 superio_exit(sio_data->sioreg);
1589
1590 data->valid = false; /* Force cache refresh */
1591error:
1592 mutex_unlock(&data->update_lock);
1593 return count;
1594}
1595
1596static struct sensor_device_attribute sda_caseopen[] = {
1597 SENSOR_ATTR(intrusion0_alarm, S_IWUSR | S_IRUGO, show_alarm,
1598 clear_caseopen, INTRUSION_ALARM_BASE),
1599 SENSOR_ATTR(intrusion1_alarm, S_IWUSR | S_IRUGO, show_alarm,
1600 clear_caseopen, INTRUSION_ALARM_BASE + 1),
1601};
1602
Guenter Roeck9de2e2e2012-05-20 19:29:48 -07001603/*
1604 * Driver and device management
1605 */
1606
1607static void nct6775_device_remove_files(struct device *dev)
1608{
1609 /*
1610 * some entries in the following arrays may not have been used in
1611 * device_create_file(), but device_remove_file() will ignore them
1612 */
1613 int i;
1614 struct nct6775_data *data = dev_get_drvdata(dev);
1615
1616 for (i = 0; i < data->in_num; i++)
1617 sysfs_remove_group(&dev->kobj, &nct6775_group_in[i]);
1618
Guenter Roeck1c65dc32012-12-04 07:56:24 -08001619 for (i = 0; i < 5; i++) {
1620 device_remove_file(dev, &sda_fan_input[i].dev_attr);
1621 device_remove_file(dev, &sda_fan_alarm[i].dev_attr);
1622 device_remove_file(dev, &sda_fan_div[i].dev_attr);
1623 device_remove_file(dev, &sda_fan_min[i].dev_attr);
1624 }
Guenter Roeckaa136e52012-12-04 03:26:05 -08001625 for (i = 0; i < NUM_TEMP; i++) {
1626 if (!(data->have_temp & (1 << i)))
1627 continue;
1628 device_remove_file(dev, &sda_temp_input[i].dev_attr);
1629 device_remove_file(dev, &sda_temp_label[i].dev_attr);
1630 device_remove_file(dev, &sda_temp_max[i].dev_attr);
1631 device_remove_file(dev, &sda_temp_max_hyst[i].dev_attr);
1632 device_remove_file(dev, &sda_temp_crit[i].dev_attr);
1633 if (!(data->have_temp_fixed & (1 << i)))
1634 continue;
1635 device_remove_file(dev, &sda_temp_type[i].dev_attr);
1636 device_remove_file(dev, &sda_temp_offset[i].dev_attr);
1637 if (i >= NUM_TEMP_ALARM)
1638 continue;
1639 device_remove_file(dev, &sda_temp_alarm[i].dev_attr);
1640 }
1641
Guenter Roecka6bd5872012-12-04 03:13:34 -08001642 device_remove_file(dev, &sda_caseopen[0].dev_attr);
1643 device_remove_file(dev, &sda_caseopen[1].dev_attr);
1644
Guenter Roeck9de2e2e2012-05-20 19:29:48 -07001645 device_remove_file(dev, &dev_attr_name);
1646 device_remove_file(dev, &dev_attr_cpu0_vid);
1647}
1648
1649/* Get the monitoring functions started */
1650static inline void nct6775_init_device(struct nct6775_data *data)
1651{
Guenter Roeckaa136e52012-12-04 03:26:05 -08001652 int i;
1653 u8 tmp, diode;
Guenter Roeck9de2e2e2012-05-20 19:29:48 -07001654
1655 /* Start monitoring if needed */
1656 if (data->REG_CONFIG) {
1657 tmp = nct6775_read_value(data, data->REG_CONFIG);
1658 if (!(tmp & 0x01))
1659 nct6775_write_value(data, data->REG_CONFIG, tmp | 0x01);
1660 }
1661
Guenter Roeckaa136e52012-12-04 03:26:05 -08001662 /* Enable temperature sensors if needed */
1663 for (i = 0; i < NUM_TEMP; i++) {
1664 if (!(data->have_temp & (1 << i)))
1665 continue;
1666 if (!data->reg_temp_config[i])
1667 continue;
1668 tmp = nct6775_read_value(data, data->reg_temp_config[i]);
1669 if (tmp & 0x01)
1670 nct6775_write_value(data, data->reg_temp_config[i],
1671 tmp & 0xfe);
1672 }
1673
Guenter Roeck9de2e2e2012-05-20 19:29:48 -07001674 /* Enable VBAT monitoring if needed */
1675 tmp = nct6775_read_value(data, data->REG_VBAT);
1676 if (!(tmp & 0x01))
1677 nct6775_write_value(data, data->REG_VBAT, tmp | 0x01);
Guenter Roeckaa136e52012-12-04 03:26:05 -08001678
1679 diode = nct6775_read_value(data, data->REG_DIODE);
1680
1681 for (i = 0; i < data->temp_fixed_num; i++) {
1682 if (!(data->have_temp_fixed & (1 << i)))
1683 continue;
1684 if ((tmp & (0x02 << i))) /* diode */
1685 data->temp_type[i] = 3 - ((diode >> i) & 0x02);
1686 else /* thermistor */
1687 data->temp_type[i] = 4;
1688 }
Guenter Roeck9de2e2e2012-05-20 19:29:48 -07001689}
1690
Guenter Roeck1c65dc32012-12-04 07:56:24 -08001691static int
1692nct6775_check_fan_inputs(const struct nct6775_sio_data *sio_data,
1693 struct nct6775_data *data)
1694{
1695 int regval;
1696 bool fan3pin, fan3min, fan4pin, fan4min, fan5pin;
1697 int ret;
1698
1699 ret = superio_enter(sio_data->sioreg);
1700 if (ret)
1701 return ret;
1702
1703 /* fan4 and fan5 share some pins with the GPIO and serial flash */
1704 if (data->kind == nct6775) {
1705 regval = superio_inb(sio_data->sioreg, 0x2c);
1706
1707 fan3pin = regval & (1 << 6);
1708 fan3min = fan3pin;
1709
1710 /* On NCT6775, fan4 shares pins with the fdc interface */
1711 fan4pin = !(superio_inb(sio_data->sioreg, 0x2A) & 0x80);
1712 fan4min = 0;
1713 fan5pin = 0;
1714 } else if (data->kind == nct6776) {
1715 bool gpok = superio_inb(sio_data->sioreg, 0x27) & 0x80;
1716
1717 superio_select(sio_data->sioreg, NCT6775_LD_HWM);
1718 regval = superio_inb(sio_data->sioreg, SIO_REG_ENABLE);
1719
1720 if (regval & 0x80)
1721 fan3pin = gpok;
1722 else
1723 fan3pin = !(superio_inb(sio_data->sioreg, 0x24) & 0x40);
1724
1725 if (regval & 0x40)
1726 fan4pin = gpok;
1727 else
1728 fan4pin = superio_inb(sio_data->sioreg, 0x1C) & 0x01;
1729
1730 if (regval & 0x20)
1731 fan5pin = gpok;
1732 else
1733 fan5pin = superio_inb(sio_data->sioreg, 0x1C) & 0x02;
1734
1735 fan4min = fan4pin;
1736 fan3min = fan3pin;
1737 } else { /* NCT6779D */
1738 regval = superio_inb(sio_data->sioreg, 0x1c);
1739
1740 fan3pin = !(regval & (1 << 5));
1741 fan4pin = !(regval & (1 << 6));
1742 fan5pin = !(regval & (1 << 7));
1743
1744 fan3min = fan3pin;
1745 fan4min = fan4pin;
1746 }
1747
1748 superio_exit(sio_data->sioreg);
1749
1750 data->has_fan = data->has_fan_min = 0x03; /* fan1 and fan2 */
1751 data->has_fan |= fan3pin << 2;
1752 data->has_fan_min |= fan3min << 2;
1753
1754 data->has_fan |= (fan4pin << 3) | (fan5pin << 4);
1755 data->has_fan_min |= (fan4min << 3) | (fan5pin << 4);
1756
1757 return 0;
1758}
1759
Guenter Roeck9de2e2e2012-05-20 19:29:48 -07001760static int nct6775_probe(struct platform_device *pdev)
1761{
1762 struct device *dev = &pdev->dev;
1763 struct nct6775_sio_data *sio_data = dev->platform_data;
1764 struct nct6775_data *data;
1765 struct resource *res;
Guenter Roeckaa136e52012-12-04 03:26:05 -08001766 int i, s, err = 0;
1767 int src, mask, available;
1768 const u16 *reg_temp, *reg_temp_over, *reg_temp_hyst, *reg_temp_config;
1769 const u16 *reg_temp_alternate, *reg_temp_crit;
1770 int num_reg_temp;
Guenter Roeck9de2e2e2012-05-20 19:29:48 -07001771
1772 res = platform_get_resource(pdev, IORESOURCE_IO, 0);
1773 if (!devm_request_region(&pdev->dev, res->start, IOREGION_LENGTH,
1774 DRVNAME))
1775 return -EBUSY;
1776
1777 data = devm_kzalloc(&pdev->dev, sizeof(struct nct6775_data),
1778 GFP_KERNEL);
1779 if (!data)
1780 return -ENOMEM;
1781
1782 data->kind = sio_data->kind;
1783 data->addr = res->start;
1784 mutex_init(&data->lock);
1785 mutex_init(&data->update_lock);
1786 data->name = nct6775_device_names[data->kind];
1787 data->bank = 0xff; /* Force initial bank selection */
1788 platform_set_drvdata(pdev, data);
1789
1790 switch (data->kind) {
1791 case nct6775:
1792 data->in_num = 9;
Guenter Roeck1c65dc32012-12-04 07:56:24 -08001793 data->has_fan_div = true;
Guenter Roeckaa136e52012-12-04 03:26:05 -08001794 data->temp_fixed_num = 3;
Guenter Roeck9de2e2e2012-05-20 19:29:48 -07001795
1796 data->ALARM_BITS = NCT6775_ALARM_BITS;
1797
Guenter Roeck1c65dc32012-12-04 07:56:24 -08001798 data->fan_from_reg = fan_from_reg16;
1799 data->fan_from_reg_min = fan_from_reg8;
1800
Guenter Roeckaa136e52012-12-04 03:26:05 -08001801 data->temp_label = nct6775_temp_label;
1802 data->temp_label_num = ARRAY_SIZE(nct6775_temp_label);
1803
Guenter Roeck9de2e2e2012-05-20 19:29:48 -07001804 data->REG_CONFIG = NCT6775_REG_CONFIG;
1805 data->REG_VBAT = NCT6775_REG_VBAT;
Guenter Roeckaa136e52012-12-04 03:26:05 -08001806 data->REG_DIODE = NCT6775_REG_DIODE;
Guenter Roeck9de2e2e2012-05-20 19:29:48 -07001807 data->REG_VIN = NCT6775_REG_IN;
1808 data->REG_IN_MINMAX[0] = NCT6775_REG_IN_MIN;
1809 data->REG_IN_MINMAX[1] = NCT6775_REG_IN_MAX;
Guenter Roeck1c65dc32012-12-04 07:56:24 -08001810 data->REG_FAN = NCT6775_REG_FAN;
1811 data->REG_FAN_MIN = NCT6775_REG_FAN_MIN;
Guenter Roeckaa136e52012-12-04 03:26:05 -08001812 data->REG_TEMP_OFFSET = NCT6775_REG_TEMP_OFFSET;
1813 data->REG_TEMP_SOURCE = NCT6775_REG_TEMP_SOURCE;
Guenter Roeck9de2e2e2012-05-20 19:29:48 -07001814 data->REG_ALARM = NCT6775_REG_ALARM;
Guenter Roeckaa136e52012-12-04 03:26:05 -08001815
1816 reg_temp = NCT6775_REG_TEMP;
1817 num_reg_temp = ARRAY_SIZE(NCT6775_REG_TEMP);
1818 reg_temp_over = NCT6775_REG_TEMP_OVER;
1819 reg_temp_hyst = NCT6775_REG_TEMP_HYST;
1820 reg_temp_config = NCT6775_REG_TEMP_CONFIG;
1821 reg_temp_alternate = NCT6775_REG_TEMP_ALTERNATE;
1822 reg_temp_crit = NCT6775_REG_TEMP_CRIT;
1823
Guenter Roeck9de2e2e2012-05-20 19:29:48 -07001824 break;
1825 case nct6776:
1826 data->in_num = 9;
Guenter Roeck1c65dc32012-12-04 07:56:24 -08001827 data->has_fan_div = false;
Guenter Roeckaa136e52012-12-04 03:26:05 -08001828 data->temp_fixed_num = 3;
Guenter Roeck9de2e2e2012-05-20 19:29:48 -07001829
1830 data->ALARM_BITS = NCT6776_ALARM_BITS;
1831
Guenter Roeck1c65dc32012-12-04 07:56:24 -08001832 data->fan_from_reg = fan_from_reg13;
1833 data->fan_from_reg_min = fan_from_reg13;
1834
Guenter Roeckaa136e52012-12-04 03:26:05 -08001835 data->temp_label = nct6776_temp_label;
1836 data->temp_label_num = ARRAY_SIZE(nct6776_temp_label);
1837
Guenter Roeck9de2e2e2012-05-20 19:29:48 -07001838 data->REG_CONFIG = NCT6775_REG_CONFIG;
1839 data->REG_VBAT = NCT6775_REG_VBAT;
Guenter Roeckaa136e52012-12-04 03:26:05 -08001840 data->REG_DIODE = NCT6775_REG_DIODE;
Guenter Roeck9de2e2e2012-05-20 19:29:48 -07001841 data->REG_VIN = NCT6775_REG_IN;
1842 data->REG_IN_MINMAX[0] = NCT6775_REG_IN_MIN;
1843 data->REG_IN_MINMAX[1] = NCT6775_REG_IN_MAX;
Guenter Roeck1c65dc32012-12-04 07:56:24 -08001844 data->REG_FAN = NCT6775_REG_FAN;
1845 data->REG_FAN_MIN = NCT6776_REG_FAN_MIN;
Guenter Roeckaa136e52012-12-04 03:26:05 -08001846 data->REG_TEMP_OFFSET = NCT6775_REG_TEMP_OFFSET;
1847 data->REG_TEMP_SOURCE = NCT6775_REG_TEMP_SOURCE;
Guenter Roeck9de2e2e2012-05-20 19:29:48 -07001848 data->REG_ALARM = NCT6775_REG_ALARM;
Guenter Roeckaa136e52012-12-04 03:26:05 -08001849
1850 reg_temp = NCT6775_REG_TEMP;
1851 num_reg_temp = ARRAY_SIZE(NCT6775_REG_TEMP);
1852 reg_temp_over = NCT6775_REG_TEMP_OVER;
1853 reg_temp_hyst = NCT6775_REG_TEMP_HYST;
1854 reg_temp_config = NCT6776_REG_TEMP_CONFIG;
1855 reg_temp_alternate = NCT6776_REG_TEMP_ALTERNATE;
1856 reg_temp_crit = NCT6776_REG_TEMP_CRIT;
1857
Guenter Roeck9de2e2e2012-05-20 19:29:48 -07001858 break;
1859 case nct6779:
1860 data->in_num = 15;
Guenter Roeck1c65dc32012-12-04 07:56:24 -08001861 data->has_fan_div = false;
Guenter Roeckaa136e52012-12-04 03:26:05 -08001862 data->temp_fixed_num = 6;
Guenter Roeck9de2e2e2012-05-20 19:29:48 -07001863
1864 data->ALARM_BITS = NCT6779_ALARM_BITS;
1865
Guenter Roeck1c65dc32012-12-04 07:56:24 -08001866 data->fan_from_reg = fan_from_reg13;
1867 data->fan_from_reg_min = fan_from_reg13;
1868
Guenter Roeckaa136e52012-12-04 03:26:05 -08001869 data->temp_label = nct6779_temp_label;
1870 data->temp_label_num = ARRAY_SIZE(nct6779_temp_label);
1871
Guenter Roeck9de2e2e2012-05-20 19:29:48 -07001872 data->REG_CONFIG = NCT6775_REG_CONFIG;
1873 data->REG_VBAT = NCT6775_REG_VBAT;
Guenter Roeckaa136e52012-12-04 03:26:05 -08001874 data->REG_DIODE = NCT6775_REG_DIODE;
Guenter Roeck9de2e2e2012-05-20 19:29:48 -07001875 data->REG_VIN = NCT6779_REG_IN;
1876 data->REG_IN_MINMAX[0] = NCT6775_REG_IN_MIN;
1877 data->REG_IN_MINMAX[1] = NCT6775_REG_IN_MAX;
Guenter Roeck1c65dc32012-12-04 07:56:24 -08001878 data->REG_FAN = NCT6779_REG_FAN;
1879 data->REG_FAN_MIN = NCT6776_REG_FAN_MIN;
Guenter Roeckaa136e52012-12-04 03:26:05 -08001880 data->REG_TEMP_OFFSET = NCT6779_REG_TEMP_OFFSET;
1881 data->REG_TEMP_SOURCE = NCT6775_REG_TEMP_SOURCE;
Guenter Roeck9de2e2e2012-05-20 19:29:48 -07001882 data->REG_ALARM = NCT6779_REG_ALARM;
Guenter Roeckaa136e52012-12-04 03:26:05 -08001883
1884 reg_temp = NCT6779_REG_TEMP;
1885 num_reg_temp = ARRAY_SIZE(NCT6779_REG_TEMP);
1886 reg_temp_over = NCT6779_REG_TEMP_OVER;
1887 reg_temp_hyst = NCT6779_REG_TEMP_HYST;
1888 reg_temp_config = NCT6779_REG_TEMP_CONFIG;
1889 reg_temp_alternate = NCT6779_REG_TEMP_ALTERNATE;
1890 reg_temp_crit = NCT6779_REG_TEMP_CRIT;
1891
Guenter Roeck9de2e2e2012-05-20 19:29:48 -07001892 break;
1893 default:
1894 return -ENODEV;
1895 }
1896 data->have_in = (1 << data->in_num) - 1;
Guenter Roeckaa136e52012-12-04 03:26:05 -08001897 data->have_temp = 0;
1898
1899 /*
1900 * On some boards, not all available temperature sources are monitored,
1901 * even though some of the monitoring registers are unused.
1902 * Get list of unused monitoring registers, then detect if any fan
1903 * controls are configured to use unmonitored temperature sources.
1904 * If so, assign the unmonitored temperature sources to available
1905 * monitoring registers.
1906 */
1907 mask = 0;
1908 available = 0;
1909 for (i = 0; i < num_reg_temp; i++) {
1910 if (reg_temp[i] == 0)
1911 continue;
1912
1913 src = nct6775_read_value(data, data->REG_TEMP_SOURCE[i]) & 0x1f;
1914 if (!src || (mask & (1 << src)))
1915 available |= 1 << i;
1916
1917 mask |= 1 << src;
1918 }
1919
1920 mask = 0;
1921 s = NUM_TEMP_FIXED; /* First dynamic temperature attribute */
1922 for (i = 0; i < num_reg_temp; i++) {
1923 if (reg_temp[i] == 0)
1924 continue;
1925
1926 src = nct6775_read_value(data, data->REG_TEMP_SOURCE[i]) & 0x1f;
1927 if (!src || (mask & (1 << src)))
1928 continue;
1929
1930 if (src >= data->temp_label_num ||
1931 !strlen(data->temp_label[src])) {
1932 dev_info(dev,
1933 "Invalid temperature source %d at index %d, source register 0x%x, temp register 0x%x\n",
1934 src, i, data->REG_TEMP_SOURCE[i], reg_temp[i]);
1935 continue;
1936 }
1937
1938 mask |= 1 << src;
1939
1940 /* Use fixed index for SYSTIN(1), CPUTIN(2), AUXTIN(3) */
1941 if (src <= data->temp_fixed_num) {
1942 data->have_temp |= 1 << (src - 1);
1943 data->have_temp_fixed |= 1 << (src - 1);
1944 data->reg_temp[0][src - 1] = reg_temp[i];
1945 data->reg_temp[1][src - 1] = reg_temp_over[i];
1946 data->reg_temp[2][src - 1] = reg_temp_hyst[i];
1947 data->reg_temp_config[src - 1] = reg_temp_config[i];
1948 data->temp_src[src - 1] = src;
1949 continue;
1950 }
1951
1952 if (s >= NUM_TEMP)
1953 continue;
1954
1955 /* Use dynamic index for other sources */
1956 data->have_temp |= 1 << s;
1957 data->reg_temp[0][s] = reg_temp[i];
1958 data->reg_temp[1][s] = reg_temp_over[i];
1959 data->reg_temp[2][s] = reg_temp_hyst[i];
1960 data->reg_temp_config[s] = reg_temp_config[i];
1961 if (reg_temp_crit[src - 1])
1962 data->reg_temp[3][s] = reg_temp_crit[src - 1];
1963
1964 data->temp_src[s] = src;
1965 s++;
1966 }
1967
1968#ifdef USE_ALTERNATE
1969 /*
1970 * Go through the list of alternate temp registers and enable
1971 * if possible.
1972 * The temperature is already monitored if the respective bit in <mask>
1973 * is set.
1974 */
1975 for (i = 0; i < data->temp_label_num - 1; i++) {
1976 if (!reg_temp_alternate[i])
1977 continue;
1978 if (mask & (1 << (i + 1)))
1979 continue;
1980 if (i < data->temp_fixed_num) {
1981 if (data->have_temp & (1 << i))
1982 continue;
1983 data->have_temp |= 1 << i;
1984 data->have_temp_fixed |= 1 << i;
1985 data->reg_temp[0][i] = reg_temp_alternate[i];
1986 data->reg_temp[1][i] = reg_temp_over[i];
1987 data->reg_temp[2][i] = reg_temp_hyst[i];
1988 data->temp_src[i] = i + 1;
1989 continue;
1990 }
1991
1992 if (s >= NUM_TEMP) /* Abort if no more space */
1993 break;
1994
1995 data->have_temp |= 1 << s;
1996 data->reg_temp[0][s] = reg_temp_alternate[i];
1997 data->temp_src[s] = i + 1;
1998 s++;
1999 }
2000#endif /* USE_ALTERNATE */
2001
2002 switch (data->kind) {
2003 case nct6775:
2004 break;
2005 case nct6776:
2006 /*
2007 * On NCT6776, AUXTIN and VIN3 pins are shared.
2008 * Only way to detect it is to check if AUXTIN is used
2009 * as a temperature source, and if that source is
2010 * enabled.
2011 *
2012 * If that is the case, disable in6, which reports VIN3.
2013 * Otherwise disable temp3.
2014 */
2015 if (data->have_temp & (1 << 2)) {
2016 u8 reg = nct6775_read_value(data,
2017 data->reg_temp_config[2]);
2018 if (reg & 0x01)
2019 data->have_temp &= ~(1 << 2);
2020 else
2021 data->have_in &= ~(1 << 6);
2022 }
2023 break;
2024 case nct6779:
2025 /*
2026 * Shared pins:
2027 * VIN4 / AUXTIN0
2028 * VIN5 / AUXTIN1
2029 * VIN6 / AUXTIN2
2030 * VIN7 / AUXTIN3
2031 *
2032 * There does not seem to be a clean way to detect if VINx or
2033 * AUXTINx is active, so for keep both sensor types enabled
2034 * for now.
2035 */
2036 break;
2037 }
Guenter Roeck9de2e2e2012-05-20 19:29:48 -07002038
2039 /* Initialize the chip */
2040 nct6775_init_device(data);
2041
2042 data->vrm = vid_which_vrm();
2043 err = superio_enter(sio_data->sioreg);
2044 if (err)
2045 return err;
2046
2047 /*
2048 * Read VID value
2049 * We can get the VID input values directly at logical device D 0xe3.
2050 */
2051 superio_select(sio_data->sioreg, NCT6775_LD_VID);
2052 data->vid = superio_inb(sio_data->sioreg, 0xe3);
2053 superio_exit(sio_data->sioreg);
2054
2055 err = device_create_file(dev, &dev_attr_cpu0_vid);
2056 if (err)
2057 return err;
2058
Guenter Roeck1c65dc32012-12-04 07:56:24 -08002059 err = nct6775_check_fan_inputs(sio_data, data);
2060 if (err)
2061 goto exit_remove;
2062
2063 /* Read fan clock dividers immediately */
2064 nct6775_init_fan_common(dev, data);
2065
Guenter Roeck9de2e2e2012-05-20 19:29:48 -07002066 for (i = 0; i < data->in_num; i++) {
2067 if (!(data->have_in & (1 << i)))
2068 continue;
2069 err = sysfs_create_group(&dev->kobj, &nct6775_group_in[i]);
2070 if (err)
2071 goto exit_remove;
2072 }
2073
Guenter Roeck1c65dc32012-12-04 07:56:24 -08002074 for (i = 0; i < 5; i++) {
2075 if (data->has_fan & (1 << i)) {
2076 err = device_create_file(dev,
2077 &sda_fan_input[i].dev_attr);
2078 if (err)
2079 goto exit_remove;
2080 err = device_create_file(dev,
2081 &sda_fan_alarm[i].dev_attr);
2082 if (err)
2083 goto exit_remove;
2084 if (data->kind != nct6776 &&
2085 data->kind != nct6779) {
2086 err = device_create_file(dev,
2087 &sda_fan_div[i].dev_attr);
2088 if (err)
2089 goto exit_remove;
2090 }
2091 if (data->has_fan_min & (1 << i)) {
2092 err = device_create_file(dev,
2093 &sda_fan_min[i].dev_attr);
2094 if (err)
2095 goto exit_remove;
2096 }
2097 }
2098 }
2099
Guenter Roeckaa136e52012-12-04 03:26:05 -08002100 for (i = 0; i < NUM_TEMP; i++) {
2101 if (!(data->have_temp & (1 << i)))
2102 continue;
2103 err = device_create_file(dev, &sda_temp_input[i].dev_attr);
2104 if (err)
2105 goto exit_remove;
2106 if (data->temp_label) {
2107 err = device_create_file(dev,
2108 &sda_temp_label[i].dev_attr);
2109 if (err)
2110 goto exit_remove;
2111 }
2112 if (data->reg_temp[1][i]) {
2113 err = device_create_file(dev,
2114 &sda_temp_max[i].dev_attr);
2115 if (err)
2116 goto exit_remove;
2117 }
2118 if (data->reg_temp[2][i]) {
2119 err = device_create_file(dev,
2120 &sda_temp_max_hyst[i].dev_attr);
2121 if (err)
2122 goto exit_remove;
2123 }
2124 if (data->reg_temp[3][i]) {
2125 err = device_create_file(dev,
2126 &sda_temp_crit[i].dev_attr);
2127 if (err)
2128 goto exit_remove;
2129 }
2130 if (!(data->have_temp_fixed & (1 << i)))
2131 continue;
2132 err = device_create_file(dev, &sda_temp_type[i].dev_attr);
2133 if (err)
2134 goto exit_remove;
2135 err = device_create_file(dev, &sda_temp_offset[i].dev_attr);
2136 if (err)
2137 goto exit_remove;
2138 if (i >= NUM_TEMP_ALARM ||
2139 data->ALARM_BITS[TEMP_ALARM_BASE + i] < 0)
2140 continue;
2141 err = device_create_file(dev, &sda_temp_alarm[i].dev_attr);
2142 if (err)
2143 goto exit_remove;
2144 }
2145
Guenter Roecka6bd5872012-12-04 03:13:34 -08002146 for (i = 0; i < ARRAY_SIZE(sda_caseopen); i++) {
2147 if (data->ALARM_BITS[INTRUSION_ALARM_BASE + i] < 0)
2148 continue;
2149 err = device_create_file(dev, &sda_caseopen[i].dev_attr);
2150 if (err)
2151 goto exit_remove;
2152 }
2153
Guenter Roeck9de2e2e2012-05-20 19:29:48 -07002154 err = device_create_file(dev, &dev_attr_name);
2155 if (err)
2156 goto exit_remove;
2157
2158 data->hwmon_dev = hwmon_device_register(dev);
2159 if (IS_ERR(data->hwmon_dev)) {
2160 err = PTR_ERR(data->hwmon_dev);
2161 goto exit_remove;
2162 }
2163
2164 return 0;
2165
2166exit_remove:
2167 nct6775_device_remove_files(dev);
2168 return err;
2169}
2170
2171static int nct6775_remove(struct platform_device *pdev)
2172{
2173 struct nct6775_data *data = platform_get_drvdata(pdev);
2174
2175 hwmon_device_unregister(data->hwmon_dev);
2176 nct6775_device_remove_files(&pdev->dev);
2177
2178 return 0;
2179}
2180
2181static struct platform_driver nct6775_driver = {
2182 .driver = {
2183 .owner = THIS_MODULE,
2184 .name = DRVNAME,
2185 },
2186 .probe = nct6775_probe,
2187 .remove = nct6775_remove,
2188};
2189
2190/* nct6775_find() looks for a '627 in the Super-I/O config space */
2191static int __init nct6775_find(int sioaddr, unsigned short *addr,
2192 struct nct6775_sio_data *sio_data)
2193{
2194 static const char sio_name_NCT6775[] __initconst = "NCT6775F";
2195 static const char sio_name_NCT6776[] __initconst = "NCT6776F";
2196 static const char sio_name_NCT6779[] __initconst = "NCT6779D";
2197
2198 u16 val;
2199 const char *sio_name;
2200 int err;
2201
2202 err = superio_enter(sioaddr);
2203 if (err)
2204 return err;
2205
2206 if (force_id)
2207 val = force_id;
2208 else
2209 val = (superio_inb(sioaddr, SIO_REG_DEVID) << 8)
2210 | superio_inb(sioaddr, SIO_REG_DEVID + 1);
2211 switch (val & SIO_ID_MASK) {
2212 case SIO_NCT6775_ID:
2213 sio_data->kind = nct6775;
2214 sio_name = sio_name_NCT6775;
2215 break;
2216 case SIO_NCT6776_ID:
2217 sio_data->kind = nct6776;
2218 sio_name = sio_name_NCT6776;
2219 break;
2220 case SIO_NCT6779_ID:
2221 sio_data->kind = nct6779;
2222 sio_name = sio_name_NCT6779;
2223 break;
2224 default:
2225 if (val != 0xffff)
2226 pr_debug("unsupported chip ID: 0x%04x\n", val);
2227 superio_exit(sioaddr);
2228 return -ENODEV;
2229 }
2230
2231 /* We have a known chip, find the HWM I/O address */
2232 superio_select(sioaddr, NCT6775_LD_HWM);
2233 val = (superio_inb(sioaddr, SIO_REG_ADDR) << 8)
2234 | superio_inb(sioaddr, SIO_REG_ADDR + 1);
2235 *addr = val & IOREGION_ALIGNMENT;
2236 if (*addr == 0) {
2237 pr_err("Refusing to enable a Super-I/O device with a base I/O port 0\n");
2238 superio_exit(sioaddr);
2239 return -ENODEV;
2240 }
2241
2242 /* Activate logical device if needed */
2243 val = superio_inb(sioaddr, SIO_REG_ENABLE);
2244 if (!(val & 0x01)) {
2245 pr_warn("Forcibly enabling Super-I/O. Sensor is probably unusable.\n");
2246 superio_outb(sioaddr, SIO_REG_ENABLE, val | 0x01);
2247 }
2248
2249 superio_exit(sioaddr);
2250 pr_info("Found %s chip at %#x\n", sio_name, *addr);
2251 sio_data->sioreg = sioaddr;
2252
2253 return 0;
2254}
2255
2256/*
2257 * when Super-I/O functions move to a separate file, the Super-I/O
2258 * bus will manage the lifetime of the device and this module will only keep
2259 * track of the nct6775 driver. But since we platform_device_alloc(), we
2260 * must keep track of the device
2261 */
2262static struct platform_device *pdev;
2263
2264static int __init sensors_nct6775_init(void)
2265{
2266 int err;
2267 unsigned short address;
2268 struct resource res;
2269 struct nct6775_sio_data sio_data;
2270
2271 /*
2272 * initialize sio_data->kind and sio_data->sioreg.
2273 *
2274 * when Super-I/O functions move to a separate file, the Super-I/O
2275 * driver will probe 0x2e and 0x4e and auto-detect the presence of a
2276 * nct6775 hardware monitor, and call probe()
2277 */
2278 if (nct6775_find(0x2e, &address, &sio_data) &&
2279 nct6775_find(0x4e, &address, &sio_data))
2280 return -ENODEV;
2281
2282 err = platform_driver_register(&nct6775_driver);
2283 if (err)
2284 goto exit;
2285
2286 pdev = platform_device_alloc(DRVNAME, address);
2287 if (!pdev) {
2288 err = -ENOMEM;
2289 pr_err("Device allocation failed\n");
2290 goto exit_unregister;
2291 }
2292
2293 err = platform_device_add_data(pdev, &sio_data,
2294 sizeof(struct nct6775_sio_data));
2295 if (err) {
2296 pr_err("Platform data allocation failed\n");
2297 goto exit_device_put;
2298 }
2299
2300 memset(&res, 0, sizeof(res));
2301 res.name = DRVNAME;
2302 res.start = address + IOREGION_OFFSET;
2303 res.end = address + IOREGION_OFFSET + IOREGION_LENGTH - 1;
2304 res.flags = IORESOURCE_IO;
2305
2306 err = acpi_check_resource_conflict(&res);
2307 if (err)
2308 goto exit_device_put;
2309
2310 err = platform_device_add_resources(pdev, &res, 1);
2311 if (err) {
2312 pr_err("Device resource addition failed (%d)\n", err);
2313 goto exit_device_put;
2314 }
2315
2316 /* platform_device_add calls probe() */
2317 err = platform_device_add(pdev);
2318 if (err) {
2319 pr_err("Device addition failed (%d)\n", err);
2320 goto exit_device_put;
2321 }
2322
2323 return 0;
2324
2325exit_device_put:
2326 platform_device_put(pdev);
2327exit_unregister:
2328 platform_driver_unregister(&nct6775_driver);
2329exit:
2330 return err;
2331}
2332
2333static void __exit sensors_nct6775_exit(void)
2334{
2335 platform_device_unregister(pdev);
2336 platform_driver_unregister(&nct6775_driver);
2337}
2338
2339MODULE_AUTHOR("Guenter Roeck <linux@roeck-us.net>");
2340MODULE_DESCRIPTION("NCT6775F/NCT6776F/NCT6779D driver");
2341MODULE_LICENSE("GPL");
2342
2343module_init(sensors_nct6775_init);
2344module_exit(sensors_nct6775_exit);