blob: ad4ecc04e2395a645715c5ff6c4936801b25bab8 [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
Guenter Roeck47ece962012-12-04 07:59:32 -080075static unsigned short fan_debounce;
76module_param(fan_debounce, ushort, 0);
77MODULE_PARM_DESC(fan_debounce, "Enable debouncing for fan RPM signal");
78
Guenter Roeck9de2e2e2012-05-20 19:29:48 -070079#define DRVNAME "nct6775"
80
81/*
82 * Super-I/O constants and functions
83 */
84
Guenter Roecka6bd5872012-12-04 03:13:34 -080085#define NCT6775_LD_ACPI 0x0a
Guenter Roeck9de2e2e2012-05-20 19:29:48 -070086#define NCT6775_LD_HWM 0x0b
87#define NCT6775_LD_VID 0x0d
88
89#define SIO_REG_LDSEL 0x07 /* Logical device select */
90#define SIO_REG_DEVID 0x20 /* Device ID (2 bytes) */
91#define SIO_REG_ENABLE 0x30 /* Logical device enable */
92#define SIO_REG_ADDR 0x60 /* Logical device address (2 bytes) */
93
94#define SIO_NCT6775_ID 0xb470
95#define SIO_NCT6776_ID 0xc330
96#define SIO_NCT6779_ID 0xc560
97#define SIO_ID_MASK 0xFFF0
98
Guenter Roeck77eb5b32012-12-04 08:30:54 -080099enum pwm_enable { off, manual, thermal_cruise, speed_cruise, sf3, sf4 };
100
Guenter Roeck9de2e2e2012-05-20 19:29:48 -0700101static inline void
102superio_outb(int ioreg, int reg, int val)
103{
104 outb(reg, ioreg);
105 outb(val, ioreg + 1);
106}
107
108static inline int
109superio_inb(int ioreg, int reg)
110{
111 outb(reg, ioreg);
112 return inb(ioreg + 1);
113}
114
115static inline void
116superio_select(int ioreg, int ld)
117{
118 outb(SIO_REG_LDSEL, ioreg);
119 outb(ld, ioreg + 1);
120}
121
122static inline int
123superio_enter(int ioreg)
124{
125 /*
126 * Try to reserve <ioreg> and <ioreg + 1> for exclusive access.
127 */
128 if (!request_muxed_region(ioreg, 2, DRVNAME))
129 return -EBUSY;
130
131 outb(0x87, ioreg);
132 outb(0x87, ioreg);
133
134 return 0;
135}
136
137static inline void
138superio_exit(int ioreg)
139{
140 outb(0xaa, ioreg);
141 outb(0x02, ioreg);
142 outb(0x02, ioreg + 1);
143 release_region(ioreg, 2);
144}
145
146/*
147 * ISA constants
148 */
149
150#define IOREGION_ALIGNMENT (~7)
151#define IOREGION_OFFSET 5
152#define IOREGION_LENGTH 2
153#define ADDR_REG_OFFSET 0
154#define DATA_REG_OFFSET 1
155
156#define NCT6775_REG_BANK 0x4E
157#define NCT6775_REG_CONFIG 0x40
158
159/*
160 * Not currently used:
161 * REG_MAN_ID has the value 0x5ca3 for all supported chips.
162 * REG_CHIP_ID == 0x88/0xa1/0xc1 depending on chip model.
163 * REG_MAN_ID is at port 0x4f
164 * REG_CHIP_ID is at port 0x58
165 */
166
Guenter Roeckaa136e52012-12-04 03:26:05 -0800167#define NUM_TEMP 10 /* Max number of temp attribute sets w/ limits*/
168#define NUM_TEMP_FIXED 6 /* Max number of fixed temp attribute sets */
169
Guenter Roeck9de2e2e2012-05-20 19:29:48 -0700170#define NUM_REG_ALARM 4 /* Max number of alarm registers */
171
172/* Common and NCT6775 specific data */
173
174/* Voltage min/max registers for nr=7..14 are in bank 5 */
175
176static const u16 NCT6775_REG_IN_MAX[] = {
177 0x2b, 0x2d, 0x2f, 0x31, 0x33, 0x35, 0x37, 0x554, 0x556, 0x558, 0x55a,
178 0x55c, 0x55e, 0x560, 0x562 };
179static const u16 NCT6775_REG_IN_MIN[] = {
180 0x2c, 0x2e, 0x30, 0x32, 0x34, 0x36, 0x38, 0x555, 0x557, 0x559, 0x55b,
181 0x55d, 0x55f, 0x561, 0x563 };
182static const u16 NCT6775_REG_IN[] = {
183 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x550, 0x551, 0x552
184};
185
186#define NCT6775_REG_VBAT 0x5D
Guenter Roeckaa136e52012-12-04 03:26:05 -0800187#define NCT6775_REG_DIODE 0x5E
Guenter Roeck9de2e2e2012-05-20 19:29:48 -0700188
Guenter Roeck1c65dc32012-12-04 07:56:24 -0800189#define NCT6775_REG_FANDIV1 0x506
190#define NCT6775_REG_FANDIV2 0x507
191
Guenter Roeck47ece962012-12-04 07:59:32 -0800192#define NCT6775_REG_CR_FAN_DEBOUNCE 0xf0
193
Guenter Roeck9de2e2e2012-05-20 19:29:48 -0700194static const u16 NCT6775_REG_ALARM[NUM_REG_ALARM] = { 0x459, 0x45A, 0x45B };
195
196/* 0..15 voltages, 16..23 fans, 24..31 temperatures */
197
198static const s8 NCT6775_ALARM_BITS[] = {
199 0, 1, 2, 3, 8, 21, 20, 16, /* in0.. in7 */
200 17, -1, -1, -1, -1, -1, -1, /* in8..in14 */
201 -1, /* unused */
202 6, 7, 11, 10, 23, /* fan1..fan5 */
203 -1, -1, -1, /* unused */
204 4, 5, 13, -1, -1, -1, /* temp1..temp6 */
205 12, -1 }; /* intrusion0, intrusion1 */
206
Guenter Roeck1c65dc32012-12-04 07:56:24 -0800207#define FAN_ALARM_BASE 16
Guenter Roeckaa136e52012-12-04 03:26:05 -0800208#define TEMP_ALARM_BASE 24
Guenter Roecka6bd5872012-12-04 03:13:34 -0800209#define INTRUSION_ALARM_BASE 30
210
211static const u8 NCT6775_REG_CR_CASEOPEN_CLR[] = { 0xe6, 0xee };
212static const u8 NCT6775_CR_CASEOPEN_CLR_MASK[] = { 0x20, 0x01 };
213
Guenter Roeck77eb5b32012-12-04 08:30:54 -0800214/* DC or PWM output fan configuration */
215static const u8 NCT6775_REG_PWM_MODE[] = { 0x04, 0x04, 0x12 };
216static const u8 NCT6775_PWM_MODE_MASK[] = { 0x01, 0x02, 0x01 };
217
218static const u16 NCT6775_REG_FAN_MODE[] = { 0x102, 0x202, 0x302, 0x802, 0x902 };
219
220static const u16 NCT6775_REG_PWM[] = { 0x109, 0x209, 0x309, 0x809, 0x909 };
221static const u16 NCT6775_REG_PWM_READ[] = { 0x01, 0x03, 0x11, 0x13, 0x15 };
222
Guenter Roeck1c65dc32012-12-04 07:56:24 -0800223static const u16 NCT6775_REG_FAN[] = { 0x630, 0x632, 0x634, 0x636, 0x638 };
224static const u16 NCT6775_REG_FAN_MIN[] = { 0x3b, 0x3c, 0x3d };
Guenter Roeck5c25d952012-12-11 07:29:06 -0800225static const u16 NCT6775_REG_FAN_PULSES[] = { 0x641, 0x642, 0x643, 0x644, 0 };
Guenter Roeck1c65dc32012-12-04 07:56:24 -0800226
Guenter Roeckaa136e52012-12-04 03:26:05 -0800227static const u16 NCT6775_REG_TEMP[] = {
228 0x27, 0x150, 0x250, 0x62b, 0x62c, 0x62d };
229
230static const u16 NCT6775_REG_TEMP_CONFIG[ARRAY_SIZE(NCT6775_REG_TEMP)] = {
231 0, 0x152, 0x252, 0x628, 0x629, 0x62A };
232static const u16 NCT6775_REG_TEMP_HYST[ARRAY_SIZE(NCT6775_REG_TEMP)] = {
233 0x3a, 0x153, 0x253, 0x673, 0x678, 0x67D };
234static const u16 NCT6775_REG_TEMP_OVER[ARRAY_SIZE(NCT6775_REG_TEMP)] = {
235 0x39, 0x155, 0x255, 0x672, 0x677, 0x67C };
236
237static const u16 NCT6775_REG_TEMP_SOURCE[ARRAY_SIZE(NCT6775_REG_TEMP)] = {
238 0x621, 0x622, 0x623, 0x624, 0x625, 0x626 };
239
240static const u16 NCT6775_REG_TEMP_OFFSET[] = { 0x454, 0x455, 0x456 };
241
242static const char *const nct6775_temp_label[] = {
243 "",
244 "SYSTIN",
245 "CPUTIN",
246 "AUXTIN",
247 "AMD SB-TSI",
248 "PECI Agent 0",
249 "PECI Agent 1",
250 "PECI Agent 2",
251 "PECI Agent 3",
252 "PECI Agent 4",
253 "PECI Agent 5",
254 "PECI Agent 6",
255 "PECI Agent 7",
256 "PCH_CHIP_CPU_MAX_TEMP",
257 "PCH_CHIP_TEMP",
258 "PCH_CPU_TEMP",
259 "PCH_MCH_TEMP",
260 "PCH_DIM0_TEMP",
261 "PCH_DIM1_TEMP",
262 "PCH_DIM2_TEMP",
263 "PCH_DIM3_TEMP"
264};
265
266static const u16 NCT6775_REG_TEMP_ALTERNATE[ARRAY_SIZE(nct6775_temp_label) - 1]
267 = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0x661, 0x662, 0x664 };
268
269static const u16 NCT6775_REG_TEMP_CRIT[ARRAY_SIZE(nct6775_temp_label) - 1]
270 = { 0, 0, 0, 0, 0xa00, 0xa01, 0xa02, 0xa03, 0xa04, 0xa05, 0xa06,
271 0xa07 };
272
Guenter Roeck9de2e2e2012-05-20 19:29:48 -0700273/* NCT6776 specific data */
274
275static const s8 NCT6776_ALARM_BITS[] = {
276 0, 1, 2, 3, 8, 21, 20, 16, /* in0.. in7 */
277 17, -1, -1, -1, -1, -1, -1, /* in8..in14 */
278 -1, /* unused */
279 6, 7, 11, 10, 23, /* fan1..fan5 */
280 -1, -1, -1, /* unused */
281 4, 5, 13, -1, -1, -1, /* temp1..temp6 */
282 12, 9 }; /* intrusion0, intrusion1 */
283
Guenter Roeck77eb5b32012-12-04 08:30:54 -0800284static const u8 NCT6776_REG_PWM_MODE[] = { 0x04, 0, 0 };
285static const u8 NCT6776_PWM_MODE_MASK[] = { 0x01, 0, 0 };
286
Guenter Roeck1c65dc32012-12-04 07:56:24 -0800287static const u16 NCT6776_REG_FAN_MIN[] = { 0x63a, 0x63c, 0x63e, 0x640, 0x642 };
Guenter Roeck5c25d952012-12-11 07:29:06 -0800288static const u16 NCT6776_REG_FAN_PULSES[] = { 0x644, 0x645, 0x646, 0, 0 };
Guenter Roeck1c65dc32012-12-04 07:56:24 -0800289
Guenter Roeckaa136e52012-12-04 03:26:05 -0800290static const u16 NCT6776_REG_TEMP_CONFIG[ARRAY_SIZE(NCT6775_REG_TEMP)] = {
291 0x18, 0x152, 0x252, 0x628, 0x629, 0x62A };
292
293static const char *const nct6776_temp_label[] = {
294 "",
295 "SYSTIN",
296 "CPUTIN",
297 "AUXTIN",
298 "SMBUSMASTER 0",
299 "SMBUSMASTER 1",
300 "SMBUSMASTER 2",
301 "SMBUSMASTER 3",
302 "SMBUSMASTER 4",
303 "SMBUSMASTER 5",
304 "SMBUSMASTER 6",
305 "SMBUSMASTER 7",
306 "PECI Agent 0",
307 "PECI Agent 1",
308 "PCH_CHIP_CPU_MAX_TEMP",
309 "PCH_CHIP_TEMP",
310 "PCH_CPU_TEMP",
311 "PCH_MCH_TEMP",
312 "PCH_DIM0_TEMP",
313 "PCH_DIM1_TEMP",
314 "PCH_DIM2_TEMP",
315 "PCH_DIM3_TEMP",
316 "BYTE_TEMP"
317};
318
319static const u16 NCT6776_REG_TEMP_ALTERNATE[ARRAY_SIZE(nct6776_temp_label) - 1]
320 = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0x401, 0x402, 0x404 };
321
322static const u16 NCT6776_REG_TEMP_CRIT[ARRAY_SIZE(nct6776_temp_label) - 1]
323 = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0x709, 0x70a };
324
Guenter Roeck9de2e2e2012-05-20 19:29:48 -0700325/* NCT6779 specific data */
326
327static const u16 NCT6779_REG_IN[] = {
328 0x480, 0x481, 0x482, 0x483, 0x484, 0x485, 0x486, 0x487,
329 0x488, 0x489, 0x48a, 0x48b, 0x48c, 0x48d, 0x48e };
330
331static const u16 NCT6779_REG_ALARM[NUM_REG_ALARM] = {
332 0x459, 0x45A, 0x45B, 0x568 };
333
334static const s8 NCT6779_ALARM_BITS[] = {
335 0, 1, 2, 3, 8, 21, 20, 16, /* in0.. in7 */
336 17, 24, 25, 26, 27, 28, 29, /* in8..in14 */
337 -1, /* unused */
338 6, 7, 11, 10, 23, /* fan1..fan5 */
339 -1, -1, -1, /* unused */
340 4, 5, 13, -1, -1, -1, /* temp1..temp6 */
341 12, 9 }; /* intrusion0, intrusion1 */
342
Guenter Roeck1c65dc32012-12-04 07:56:24 -0800343static const u16 NCT6779_REG_FAN[] = { 0x4b0, 0x4b2, 0x4b4, 0x4b6, 0x4b8 };
Guenter Roeck5c25d952012-12-11 07:29:06 -0800344static const u16 NCT6779_REG_FAN_PULSES[] = {
345 0x644, 0x645, 0x646, 0x647, 0x648 };
Guenter Roeck1c65dc32012-12-04 07:56:24 -0800346
Guenter Roeckaa136e52012-12-04 03:26:05 -0800347static const u16 NCT6779_REG_TEMP[] = { 0x27, 0x150 };
348static const u16 NCT6779_REG_TEMP_CONFIG[ARRAY_SIZE(NCT6779_REG_TEMP)] = {
349 0x18, 0x152 };
350static const u16 NCT6779_REG_TEMP_HYST[ARRAY_SIZE(NCT6779_REG_TEMP)] = {
351 0x3a, 0x153 };
352static const u16 NCT6779_REG_TEMP_OVER[ARRAY_SIZE(NCT6779_REG_TEMP)] = {
353 0x39, 0x155 };
354
355static const u16 NCT6779_REG_TEMP_OFFSET[] = {
356 0x454, 0x455, 0x456, 0x44a, 0x44b, 0x44c };
357
358static const char *const nct6779_temp_label[] = {
359 "",
360 "SYSTIN",
361 "CPUTIN",
362 "AUXTIN0",
363 "AUXTIN1",
364 "AUXTIN2",
365 "AUXTIN3",
366 "",
367 "SMBUSMASTER 0",
368 "SMBUSMASTER 1",
369 "SMBUSMASTER 2",
370 "SMBUSMASTER 3",
371 "SMBUSMASTER 4",
372 "SMBUSMASTER 5",
373 "SMBUSMASTER 6",
374 "SMBUSMASTER 7",
375 "PECI Agent 0",
376 "PECI Agent 1",
377 "PCH_CHIP_CPU_MAX_TEMP",
378 "PCH_CHIP_TEMP",
379 "PCH_CPU_TEMP",
380 "PCH_MCH_TEMP",
381 "PCH_DIM0_TEMP",
382 "PCH_DIM1_TEMP",
383 "PCH_DIM2_TEMP",
384 "PCH_DIM3_TEMP",
385 "BYTE_TEMP"
386};
387
388static const u16 NCT6779_REG_TEMP_ALTERNATE[ARRAY_SIZE(nct6779_temp_label) - 1]
389 = { 0x490, 0x491, 0x492, 0x493, 0x494, 0x495, 0, 0,
390 0, 0, 0, 0, 0, 0, 0, 0,
391 0, 0x400, 0x401, 0x402, 0x404, 0x405, 0x406, 0x407,
392 0x408, 0 };
393
394static const u16 NCT6779_REG_TEMP_CRIT[ARRAY_SIZE(nct6779_temp_label) - 1]
395 = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0x709, 0x70a };
396
Guenter Roeck77eb5b32012-12-04 08:30:54 -0800397static enum pwm_enable reg_to_pwm_enable(int pwm, int mode)
398{
399 if (mode == 0 && pwm == 255)
400 return off;
401 return mode + 1;
402}
403
404static int pwm_enable_to_reg(enum pwm_enable mode)
405{
406 if (mode == off)
407 return 0;
408 return mode - 1;
409}
410
Guenter Roeck9de2e2e2012-05-20 19:29:48 -0700411/*
412 * Conversions
413 */
414
Guenter Roeck1c65dc32012-12-04 07:56:24 -0800415static unsigned int fan_from_reg8(u16 reg, unsigned int divreg)
416{
417 if (reg == 0 || reg == 255)
418 return 0;
419 return 1350000U / (reg << divreg);
420}
421
422static unsigned int fan_from_reg13(u16 reg, unsigned int divreg)
423{
424 if ((reg & 0xff1f) == 0xff1f)
425 return 0;
426
427 reg = (reg & 0x1f) | ((reg & 0xff00) >> 3);
428
429 if (reg == 0)
430 return 0;
431
432 return 1350000U / reg;
433}
434
435static unsigned int fan_from_reg16(u16 reg, unsigned int divreg)
436{
437 if (reg == 0 || reg == 0xffff)
438 return 0;
439
440 /*
441 * Even though the registers are 16 bit wide, the fan divisor
442 * still applies.
443 */
444 return 1350000U / (reg << divreg);
445}
446
447static inline unsigned int
448div_from_reg(u8 reg)
449{
450 return 1 << reg;
451}
452
Guenter Roeck9de2e2e2012-05-20 19:29:48 -0700453/*
454 * Some of the voltage inputs have internal scaling, the tables below
455 * contain 8 (the ADC LSB in mV) * scaling factor * 100
456 */
457static const u16 scale_in[15] = {
458 800, 800, 1600, 1600, 800, 800, 800, 1600, 1600, 800, 800, 800, 800,
459 800, 800
460};
461
462static inline long in_from_reg(u8 reg, u8 nr)
463{
464 return DIV_ROUND_CLOSEST(reg * scale_in[nr], 100);
465}
466
467static inline u8 in_to_reg(u32 val, u8 nr)
468{
469 return clamp_val(DIV_ROUND_CLOSEST(val * 100, scale_in[nr]), 0, 255);
470}
471
472/*
473 * Data structures and manipulation thereof
474 */
475
476struct nct6775_data {
477 int addr; /* IO base of hw monitor block */
478 enum kinds kind;
479 const char *name;
480
481 struct device *hwmon_dev;
482 struct mutex lock;
483
Guenter Roeckaa136e52012-12-04 03:26:05 -0800484 u16 reg_temp[4][NUM_TEMP]; /* 0=temp, 1=temp_over, 2=temp_hyst,
485 * 3=temp_crit
486 */
487 u8 temp_src[NUM_TEMP];
488 u16 reg_temp_config[NUM_TEMP];
489 const char * const *temp_label;
490 int temp_label_num;
491
Guenter Roeck9de2e2e2012-05-20 19:29:48 -0700492 u16 REG_CONFIG;
493 u16 REG_VBAT;
Guenter Roeckaa136e52012-12-04 03:26:05 -0800494 u16 REG_DIODE;
Guenter Roeck9de2e2e2012-05-20 19:29:48 -0700495
496 const s8 *ALARM_BITS;
497
498 const u16 *REG_VIN;
499 const u16 *REG_IN_MINMAX[2];
500
Guenter Roeck1c65dc32012-12-04 07:56:24 -0800501 const u16 *REG_FAN;
Guenter Roeck77eb5b32012-12-04 08:30:54 -0800502 const u16 *REG_FAN_MODE;
Guenter Roeck1c65dc32012-12-04 07:56:24 -0800503 const u16 *REG_FAN_MIN;
Guenter Roeck5c25d952012-12-11 07:29:06 -0800504 const u16 *REG_FAN_PULSES;
Guenter Roeckaa136e52012-12-04 03:26:05 -0800505
Guenter Roeck77eb5b32012-12-04 08:30:54 -0800506 const u8 *REG_PWM_MODE;
507 const u8 *PWM_MODE_MASK;
508
509 const u16 *REG_PWM[1]; /* [0]=pwm */
510 const u16 *REG_PWM_READ;
511
Guenter Roeck1c65dc32012-12-04 07:56:24 -0800512 const u16 *REG_TEMP_SOURCE; /* temp register sources */
Guenter Roeckaa136e52012-12-04 03:26:05 -0800513 const u16 *REG_TEMP_OFFSET;
514
Guenter Roeck9de2e2e2012-05-20 19:29:48 -0700515 const u16 *REG_ALARM;
516
Guenter Roeck1c65dc32012-12-04 07:56:24 -0800517 unsigned int (*fan_from_reg)(u16 reg, unsigned int divreg);
518 unsigned int (*fan_from_reg_min)(u16 reg, unsigned int divreg);
519
Guenter Roeck9de2e2e2012-05-20 19:29:48 -0700520 struct mutex update_lock;
521 bool valid; /* true if following fields are valid */
522 unsigned long last_updated; /* In jiffies */
523
524 /* Register values */
525 u8 bank; /* current register bank */
526 u8 in_num; /* number of in inputs we have */
527 u8 in[15][3]; /* [0]=in, [1]=in_max, [2]=in_min */
Guenter Roeck1c65dc32012-12-04 07:56:24 -0800528 unsigned int rpm[5];
529 u16 fan_min[5];
Guenter Roeck5c25d952012-12-11 07:29:06 -0800530 u8 fan_pulses[5];
Guenter Roeck1c65dc32012-12-04 07:56:24 -0800531 u8 fan_div[5];
Guenter Roeck77eb5b32012-12-04 08:30:54 -0800532 u8 has_pwm;
Guenter Roeck1c65dc32012-12-04 07:56:24 -0800533 u8 has_fan; /* some fan inputs can be disabled */
534 u8 has_fan_min; /* some fans don't have min register */
535 bool has_fan_div;
Guenter Roeck9de2e2e2012-05-20 19:29:48 -0700536
Guenter Roeckaa136e52012-12-04 03:26:05 -0800537 u8 temp_fixed_num; /* 3 or 6 */
538 u8 temp_type[NUM_TEMP_FIXED];
539 s8 temp_offset[NUM_TEMP_FIXED];
540 s16 temp[4][NUM_TEMP]; /* 0=temp, 1=temp_over, 2=temp_hyst,
541 * 3=temp_crit */
Guenter Roeck9de2e2e2012-05-20 19:29:48 -0700542 u64 alarms;
543
Guenter Roeck77eb5b32012-12-04 08:30:54 -0800544 u8 pwm_num; /* number of pwm */
545 u8 pwm_mode[5]; /* 1->DC variable voltage, 0->PWM variable duty cycle */
546 enum pwm_enable pwm_enable[5];
547 /* 0->off
548 * 1->manual
549 * 2->thermal cruise mode (also called SmartFan I)
550 * 3->fan speed cruise mode
551 * 4->SmartFan III
552 * 5->enhanced variable thermal cruise (SmartFan IV)
553 */
554 u8 pwm[1][5]; /* [0]=pwm */
555
Guenter Roeck9de2e2e2012-05-20 19:29:48 -0700556 u8 vid;
557 u8 vrm;
558
Guenter Roeckaa136e52012-12-04 03:26:05 -0800559 u16 have_temp;
560 u16 have_temp_fixed;
Guenter Roeck9de2e2e2012-05-20 19:29:48 -0700561 u16 have_in;
Guenter Roeck84d19d92012-12-04 08:01:39 -0800562#ifdef CONFIG_PM
563 /* Remember extra register values over suspend/resume */
564 u8 vbat;
565 u8 fandiv1;
566 u8 fandiv2;
567#endif
Guenter Roeck9de2e2e2012-05-20 19:29:48 -0700568};
569
570struct nct6775_sio_data {
571 int sioreg;
572 enum kinds kind;
573};
574
575static bool is_word_sized(struct nct6775_data *data, u16 reg)
576{
577 switch (data->kind) {
578 case nct6775:
579 return (((reg & 0xff00) == 0x100 ||
580 (reg & 0xff00) == 0x200) &&
581 ((reg & 0x00ff) == 0x50 ||
582 (reg & 0x00ff) == 0x53 ||
583 (reg & 0x00ff) == 0x55)) ||
584 (reg & 0xfff0) == 0x630 ||
585 reg == 0x640 || reg == 0x642 ||
586 reg == 0x662 ||
587 ((reg & 0xfff0) == 0x650 && (reg & 0x000f) >= 0x06) ||
588 reg == 0x73 || reg == 0x75 || reg == 0x77;
589 case nct6776:
590 return (((reg & 0xff00) == 0x100 ||
591 (reg & 0xff00) == 0x200) &&
592 ((reg & 0x00ff) == 0x50 ||
593 (reg & 0x00ff) == 0x53 ||
594 (reg & 0x00ff) == 0x55)) ||
595 (reg & 0xfff0) == 0x630 ||
596 reg == 0x402 ||
597 reg == 0x640 || reg == 0x642 ||
598 ((reg & 0xfff0) == 0x650 && (reg & 0x000f) >= 0x06) ||
599 reg == 0x73 || reg == 0x75 || reg == 0x77;
600 case nct6779:
601 return reg == 0x150 || reg == 0x153 || reg == 0x155 ||
602 ((reg & 0xfff0) == 0x4b0 && (reg & 0x000f) < 0x09) ||
603 reg == 0x402 ||
604 reg == 0x63a || reg == 0x63c || reg == 0x63e ||
605 reg == 0x640 || reg == 0x642 ||
606 reg == 0x73 || reg == 0x75 || reg == 0x77 || reg == 0x79 ||
607 reg == 0x7b;
608 }
609 return false;
610}
611
612/*
613 * On older chips, only registers 0x50-0x5f are banked.
614 * On more recent chips, all registers are banked.
615 * Assume that is the case and set the bank number for each access.
616 * Cache the bank number so it only needs to be set if it changes.
617 */
618static inline void nct6775_set_bank(struct nct6775_data *data, u16 reg)
619{
620 u8 bank = reg >> 8;
621 if (data->bank != bank) {
622 outb_p(NCT6775_REG_BANK, data->addr + ADDR_REG_OFFSET);
623 outb_p(bank, data->addr + DATA_REG_OFFSET);
624 data->bank = bank;
625 }
626}
627
628static u16 nct6775_read_value(struct nct6775_data *data, u16 reg)
629{
630 int res, word_sized = is_word_sized(data, reg);
631
632 mutex_lock(&data->lock);
633
634 nct6775_set_bank(data, reg);
635 outb_p(reg & 0xff, data->addr + ADDR_REG_OFFSET);
636 res = inb_p(data->addr + DATA_REG_OFFSET);
637 if (word_sized) {
638 outb_p((reg & 0xff) + 1,
639 data->addr + ADDR_REG_OFFSET);
640 res = (res << 8) + inb_p(data->addr + DATA_REG_OFFSET);
641 }
642
643 mutex_unlock(&data->lock);
644 return res;
645}
646
647static int nct6775_write_value(struct nct6775_data *data, u16 reg, u16 value)
648{
649 int word_sized = is_word_sized(data, reg);
650
651 mutex_lock(&data->lock);
652
653 nct6775_set_bank(data, reg);
654 outb_p(reg & 0xff, data->addr + ADDR_REG_OFFSET);
655 if (word_sized) {
656 outb_p(value >> 8, data->addr + DATA_REG_OFFSET);
657 outb_p((reg & 0xff) + 1,
658 data->addr + ADDR_REG_OFFSET);
659 }
660 outb_p(value & 0xff, data->addr + DATA_REG_OFFSET);
661
662 mutex_unlock(&data->lock);
663 return 0;
664}
665
Guenter Roeckaa136e52012-12-04 03:26:05 -0800666/* We left-align 8-bit temperature values to make the code simpler */
667static u16 nct6775_read_temp(struct nct6775_data *data, u16 reg)
668{
669 u16 res;
670
671 res = nct6775_read_value(data, reg);
672 if (!is_word_sized(data, reg))
673 res <<= 8;
674
675 return res;
676}
677
678static int nct6775_write_temp(struct nct6775_data *data, u16 reg, u16 value)
679{
680 if (!is_word_sized(data, reg))
681 value >>= 8;
682 return nct6775_write_value(data, reg, value);
683}
684
Guenter Roeck1c65dc32012-12-04 07:56:24 -0800685/* This function assumes that the caller holds data->update_lock */
686static void nct6775_write_fan_div(struct nct6775_data *data, int nr)
687{
688 u8 reg;
689
690 switch (nr) {
691 case 0:
692 reg = (nct6775_read_value(data, NCT6775_REG_FANDIV1) & 0x70)
693 | (data->fan_div[0] & 0x7);
694 nct6775_write_value(data, NCT6775_REG_FANDIV1, reg);
695 break;
696 case 1:
697 reg = (nct6775_read_value(data, NCT6775_REG_FANDIV1) & 0x7)
698 | ((data->fan_div[1] << 4) & 0x70);
699 nct6775_write_value(data, NCT6775_REG_FANDIV1, reg);
700 break;
701 case 2:
702 reg = (nct6775_read_value(data, NCT6775_REG_FANDIV2) & 0x70)
703 | (data->fan_div[2] & 0x7);
704 nct6775_write_value(data, NCT6775_REG_FANDIV2, reg);
705 break;
706 case 3:
707 reg = (nct6775_read_value(data, NCT6775_REG_FANDIV2) & 0x7)
708 | ((data->fan_div[3] << 4) & 0x70);
709 nct6775_write_value(data, NCT6775_REG_FANDIV2, reg);
710 break;
711 }
712}
713
714static void nct6775_write_fan_div_common(struct nct6775_data *data, int nr)
715{
716 if (data->kind == nct6775)
717 nct6775_write_fan_div(data, nr);
718}
719
720static void nct6775_update_fan_div(struct nct6775_data *data)
721{
722 u8 i;
723
724 i = nct6775_read_value(data, NCT6775_REG_FANDIV1);
725 data->fan_div[0] = i & 0x7;
726 data->fan_div[1] = (i & 0x70) >> 4;
727 i = nct6775_read_value(data, NCT6775_REG_FANDIV2);
728 data->fan_div[2] = i & 0x7;
729 if (data->has_fan & (1<<3))
730 data->fan_div[3] = (i & 0x70) >> 4;
731}
732
733static void nct6775_update_fan_div_common(struct nct6775_data *data)
734{
735 if (data->kind == nct6775)
736 nct6775_update_fan_div(data);
737}
738
739static void nct6775_init_fan_div(struct nct6775_data *data)
740{
741 int i;
742
743 nct6775_update_fan_div_common(data);
744 /*
745 * For all fans, start with highest divider value if the divider
746 * register is not initialized. This ensures that we get a
747 * reading from the fan count register, even if it is not optimal.
748 * We'll compute a better divider later on.
749 */
750 for (i = 0; i < 3; i++) {
751 if (!(data->has_fan & (1 << i)))
752 continue;
753 if (data->fan_div[i] == 0) {
754 data->fan_div[i] = 7;
755 nct6775_write_fan_div_common(data, i);
756 }
757 }
758}
759
760static void nct6775_init_fan_common(struct device *dev,
761 struct nct6775_data *data)
762{
763 int i;
764 u8 reg;
765
766 if (data->has_fan_div)
767 nct6775_init_fan_div(data);
768
769 /*
770 * If fan_min is not set (0), set it to 0xff to disable it. This
771 * prevents the unnecessary warning when fanX_min is reported as 0.
772 */
773 for (i = 0; i < 5; i++) {
774 if (data->has_fan_min & (1 << i)) {
775 reg = nct6775_read_value(data, data->REG_FAN_MIN[i]);
776 if (!reg)
777 nct6775_write_value(data, data->REG_FAN_MIN[i],
778 data->has_fan_div ? 0xff
779 : 0xff1f);
780 }
781 }
782}
783
784static void nct6775_select_fan_div(struct device *dev,
785 struct nct6775_data *data, int nr, u16 reg)
786{
787 u8 fan_div = data->fan_div[nr];
788 u16 fan_min;
789
790 if (!data->has_fan_div)
791 return;
792
793 /*
794 * If we failed to measure the fan speed, or the reported value is not
795 * in the optimal range, and the clock divider can be modified,
796 * let's try that for next time.
797 */
798 if (reg == 0x00 && fan_div < 0x07)
799 fan_div++;
800 else if (reg != 0x00 && reg < 0x30 && fan_div > 0)
801 fan_div--;
802
803 if (fan_div != data->fan_div[nr]) {
804 dev_dbg(dev, "Modifying fan%d clock divider from %u to %u\n",
805 nr + 1, div_from_reg(data->fan_div[nr]),
806 div_from_reg(fan_div));
807
808 /* Preserve min limit if possible */
809 if (data->has_fan_min & (1 << nr)) {
810 fan_min = data->fan_min[nr];
811 if (fan_div > data->fan_div[nr]) {
812 if (fan_min != 255 && fan_min > 1)
813 fan_min >>= 1;
814 } else {
815 if (fan_min != 255) {
816 fan_min <<= 1;
817 if (fan_min > 254)
818 fan_min = 254;
819 }
820 }
821 if (fan_min != data->fan_min[nr]) {
822 data->fan_min[nr] = fan_min;
823 nct6775_write_value(data, data->REG_FAN_MIN[nr],
824 fan_min);
825 }
826 }
827 data->fan_div[nr] = fan_div;
828 nct6775_write_fan_div_common(data, nr);
829 }
830}
831
Guenter Roeck77eb5b32012-12-04 08:30:54 -0800832static void nct6775_update_pwm(struct device *dev)
833{
834 struct nct6775_data *data = dev_get_drvdata(dev);
835 int i, j;
836 int fanmodecfg;
837 bool duty_is_dc;
838
839 for (i = 0; i < data->pwm_num; i++) {
840 if (!(data->has_pwm & (1 << i)))
841 continue;
842
843 duty_is_dc = data->REG_PWM_MODE[i] &&
844 (nct6775_read_value(data, data->REG_PWM_MODE[i])
845 & data->PWM_MODE_MASK[i]);
846 data->pwm_mode[i] = duty_is_dc;
847
848 fanmodecfg = nct6775_read_value(data, data->REG_FAN_MODE[i]);
849 for (j = 0; j < ARRAY_SIZE(data->REG_PWM); j++) {
850 if (data->REG_PWM[j] && data->REG_PWM[j][i]) {
851 data->pwm[j][i]
852 = nct6775_read_value(data,
853 data->REG_PWM[j][i]);
854 }
855 }
856
857 data->pwm_enable[i] = reg_to_pwm_enable(data->pwm[0][i],
858 (fanmodecfg >> 4) & 7);
859 }
860}
861
Guenter Roeck9de2e2e2012-05-20 19:29:48 -0700862static struct nct6775_data *nct6775_update_device(struct device *dev)
863{
864 struct nct6775_data *data = dev_get_drvdata(dev);
Guenter Roeckaa136e52012-12-04 03:26:05 -0800865 int i, j;
Guenter Roeck9de2e2e2012-05-20 19:29:48 -0700866
867 mutex_lock(&data->update_lock);
868
869 if (time_after(jiffies, data->last_updated + HZ + HZ/2)
870 || !data->valid) {
Guenter Roeck1c65dc32012-12-04 07:56:24 -0800871 /* Fan clock dividers */
872 nct6775_update_fan_div_common(data);
873
Guenter Roeck9de2e2e2012-05-20 19:29:48 -0700874 /* Measured voltages and limits */
875 for (i = 0; i < data->in_num; i++) {
876 if (!(data->have_in & (1 << i)))
877 continue;
878
879 data->in[i][0] = nct6775_read_value(data,
880 data->REG_VIN[i]);
881 data->in[i][1] = nct6775_read_value(data,
882 data->REG_IN_MINMAX[0][i]);
883 data->in[i][2] = nct6775_read_value(data,
884 data->REG_IN_MINMAX[1][i]);
885 }
886
Guenter Roeck1c65dc32012-12-04 07:56:24 -0800887 /* Measured fan speeds and limits */
888 for (i = 0; i < 5; i++) {
889 u16 reg;
890
891 if (!(data->has_fan & (1 << i)))
892 continue;
893
894 reg = nct6775_read_value(data, data->REG_FAN[i]);
895 data->rpm[i] = data->fan_from_reg(reg,
896 data->fan_div[i]);
897
898 if (data->has_fan_min & (1 << i))
899 data->fan_min[i] = nct6775_read_value(data,
900 data->REG_FAN_MIN[i]);
Guenter Roeck5c25d952012-12-11 07:29:06 -0800901 data->fan_pulses[i] =
902 nct6775_read_value(data, data->REG_FAN_PULSES[i]);
Guenter Roeck1c65dc32012-12-04 07:56:24 -0800903
904 nct6775_select_fan_div(dev, data, i, reg);
905 }
906
Guenter Roeck77eb5b32012-12-04 08:30:54 -0800907 nct6775_update_pwm(dev);
908
Guenter Roeckaa136e52012-12-04 03:26:05 -0800909 /* Measured temperatures and limits */
910 for (i = 0; i < NUM_TEMP; i++) {
911 if (!(data->have_temp & (1 << i)))
912 continue;
913 for (j = 0; j < 4; j++) {
914 if (data->reg_temp[j][i])
915 data->temp[j][i]
916 = nct6775_read_temp(data,
917 data->reg_temp[j][i]);
918 }
919 if (!(data->have_temp_fixed & (1 << i)))
920 continue;
921 data->temp_offset[i]
922 = nct6775_read_value(data, data->REG_TEMP_OFFSET[i]);
923 }
924
Guenter Roeck9de2e2e2012-05-20 19:29:48 -0700925 data->alarms = 0;
926 for (i = 0; i < NUM_REG_ALARM; i++) {
927 u8 alarm;
928 if (!data->REG_ALARM[i])
929 continue;
930 alarm = nct6775_read_value(data, data->REG_ALARM[i]);
931 data->alarms |= ((u64)alarm) << (i << 3);
932 }
933
934 data->last_updated = jiffies;
935 data->valid = true;
936 }
937
938 mutex_unlock(&data->update_lock);
939 return data;
940}
941
942/*
943 * Sysfs callback functions
944 */
945static ssize_t
946show_in_reg(struct device *dev, struct device_attribute *attr, char *buf)
947{
948 struct nct6775_data *data = nct6775_update_device(dev);
949 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
950 int nr = sattr->nr;
951 int index = sattr->index;
952 return sprintf(buf, "%ld\n", in_from_reg(data->in[nr][index], nr));
953}
954
955static ssize_t
956store_in_reg(struct device *dev, struct device_attribute *attr, const char *buf,
957 size_t count)
958{
959 struct nct6775_data *data = dev_get_drvdata(dev);
960 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
961 int nr = sattr->nr;
962 int index = sattr->index;
963 unsigned long val;
964 int err = kstrtoul(buf, 10, &val);
965 if (err < 0)
966 return err;
967 mutex_lock(&data->update_lock);
968 data->in[nr][index] = in_to_reg(val, nr);
969 nct6775_write_value(data, data->REG_IN_MINMAX[index-1][nr],
970 data->in[nr][index]);
971 mutex_unlock(&data->update_lock);
972 return count;
973}
974
975static ssize_t
976show_alarm(struct device *dev, struct device_attribute *attr, char *buf)
977{
978 struct nct6775_data *data = nct6775_update_device(dev);
979 struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr);
980 int nr = data->ALARM_BITS[sattr->index];
981 return sprintf(buf, "%u\n",
982 (unsigned int)((data->alarms >> nr) & 0x01));
983}
984
985static SENSOR_DEVICE_ATTR_2(in0_input, S_IRUGO, show_in_reg, NULL, 0, 0);
986static SENSOR_DEVICE_ATTR_2(in1_input, S_IRUGO, show_in_reg, NULL, 1, 0);
987static SENSOR_DEVICE_ATTR_2(in2_input, S_IRUGO, show_in_reg, NULL, 2, 0);
988static SENSOR_DEVICE_ATTR_2(in3_input, S_IRUGO, show_in_reg, NULL, 3, 0);
989static SENSOR_DEVICE_ATTR_2(in4_input, S_IRUGO, show_in_reg, NULL, 4, 0);
990static SENSOR_DEVICE_ATTR_2(in5_input, S_IRUGO, show_in_reg, NULL, 5, 0);
991static SENSOR_DEVICE_ATTR_2(in6_input, S_IRUGO, show_in_reg, NULL, 6, 0);
992static SENSOR_DEVICE_ATTR_2(in7_input, S_IRUGO, show_in_reg, NULL, 7, 0);
993static SENSOR_DEVICE_ATTR_2(in8_input, S_IRUGO, show_in_reg, NULL, 8, 0);
994static SENSOR_DEVICE_ATTR_2(in9_input, S_IRUGO, show_in_reg, NULL, 9, 0);
995static SENSOR_DEVICE_ATTR_2(in10_input, S_IRUGO, show_in_reg, NULL, 10, 0);
996static SENSOR_DEVICE_ATTR_2(in11_input, S_IRUGO, show_in_reg, NULL, 11, 0);
997static SENSOR_DEVICE_ATTR_2(in12_input, S_IRUGO, show_in_reg, NULL, 12, 0);
998static SENSOR_DEVICE_ATTR_2(in13_input, S_IRUGO, show_in_reg, NULL, 13, 0);
999static SENSOR_DEVICE_ATTR_2(in14_input, S_IRUGO, show_in_reg, NULL, 14, 0);
1000
1001static SENSOR_DEVICE_ATTR(in0_alarm, S_IRUGO, show_alarm, NULL, 0);
1002static SENSOR_DEVICE_ATTR(in1_alarm, S_IRUGO, show_alarm, NULL, 1);
1003static SENSOR_DEVICE_ATTR(in2_alarm, S_IRUGO, show_alarm, NULL, 2);
1004static SENSOR_DEVICE_ATTR(in3_alarm, S_IRUGO, show_alarm, NULL, 3);
1005static SENSOR_DEVICE_ATTR(in4_alarm, S_IRUGO, show_alarm, NULL, 4);
1006static SENSOR_DEVICE_ATTR(in5_alarm, S_IRUGO, show_alarm, NULL, 5);
1007static SENSOR_DEVICE_ATTR(in6_alarm, S_IRUGO, show_alarm, NULL, 6);
1008static SENSOR_DEVICE_ATTR(in7_alarm, S_IRUGO, show_alarm, NULL, 7);
1009static SENSOR_DEVICE_ATTR(in8_alarm, S_IRUGO, show_alarm, NULL, 8);
1010static SENSOR_DEVICE_ATTR(in9_alarm, S_IRUGO, show_alarm, NULL, 9);
1011static SENSOR_DEVICE_ATTR(in10_alarm, S_IRUGO, show_alarm, NULL, 10);
1012static SENSOR_DEVICE_ATTR(in11_alarm, S_IRUGO, show_alarm, NULL, 11);
1013static SENSOR_DEVICE_ATTR(in12_alarm, S_IRUGO, show_alarm, NULL, 12);
1014static SENSOR_DEVICE_ATTR(in13_alarm, S_IRUGO, show_alarm, NULL, 13);
1015static SENSOR_DEVICE_ATTR(in14_alarm, S_IRUGO, show_alarm, NULL, 14);
1016
1017static SENSOR_DEVICE_ATTR_2(in0_min, S_IWUSR | S_IRUGO, show_in_reg,
1018 store_in_reg, 0, 1);
1019static SENSOR_DEVICE_ATTR_2(in1_min, S_IWUSR | S_IRUGO, show_in_reg,
1020 store_in_reg, 1, 1);
1021static SENSOR_DEVICE_ATTR_2(in2_min, S_IWUSR | S_IRUGO, show_in_reg,
1022 store_in_reg, 2, 1);
1023static SENSOR_DEVICE_ATTR_2(in3_min, S_IWUSR | S_IRUGO, show_in_reg,
1024 store_in_reg, 3, 1);
1025static SENSOR_DEVICE_ATTR_2(in4_min, S_IWUSR | S_IRUGO, show_in_reg,
1026 store_in_reg, 4, 1);
1027static SENSOR_DEVICE_ATTR_2(in5_min, S_IWUSR | S_IRUGO, show_in_reg,
1028 store_in_reg, 5, 1);
1029static SENSOR_DEVICE_ATTR_2(in6_min, S_IWUSR | S_IRUGO, show_in_reg,
1030 store_in_reg, 6, 1);
1031static SENSOR_DEVICE_ATTR_2(in7_min, S_IWUSR | S_IRUGO, show_in_reg,
1032 store_in_reg, 7, 1);
1033static SENSOR_DEVICE_ATTR_2(in8_min, S_IWUSR | S_IRUGO, show_in_reg,
1034 store_in_reg, 8, 1);
1035static SENSOR_DEVICE_ATTR_2(in9_min, S_IWUSR | S_IRUGO, show_in_reg,
1036 store_in_reg, 9, 1);
1037static SENSOR_DEVICE_ATTR_2(in10_min, S_IWUSR | S_IRUGO, show_in_reg,
1038 store_in_reg, 10, 1);
1039static SENSOR_DEVICE_ATTR_2(in11_min, S_IWUSR | S_IRUGO, show_in_reg,
1040 store_in_reg, 11, 1);
1041static SENSOR_DEVICE_ATTR_2(in12_min, S_IWUSR | S_IRUGO, show_in_reg,
1042 store_in_reg, 12, 1);
1043static SENSOR_DEVICE_ATTR_2(in13_min, S_IWUSR | S_IRUGO, show_in_reg,
1044 store_in_reg, 13, 1);
1045static SENSOR_DEVICE_ATTR_2(in14_min, S_IWUSR | S_IRUGO, show_in_reg,
1046 store_in_reg, 14, 1);
1047
1048static SENSOR_DEVICE_ATTR_2(in0_max, S_IWUSR | S_IRUGO, show_in_reg,
1049 store_in_reg, 0, 2);
1050static SENSOR_DEVICE_ATTR_2(in1_max, S_IWUSR | S_IRUGO, show_in_reg,
1051 store_in_reg, 1, 2);
1052static SENSOR_DEVICE_ATTR_2(in2_max, S_IWUSR | S_IRUGO, show_in_reg,
1053 store_in_reg, 2, 2);
1054static SENSOR_DEVICE_ATTR_2(in3_max, S_IWUSR | S_IRUGO, show_in_reg,
1055 store_in_reg, 3, 2);
1056static SENSOR_DEVICE_ATTR_2(in4_max, S_IWUSR | S_IRUGO, show_in_reg,
1057 store_in_reg, 4, 2);
1058static SENSOR_DEVICE_ATTR_2(in5_max, S_IWUSR | S_IRUGO, show_in_reg,
1059 store_in_reg, 5, 2);
1060static SENSOR_DEVICE_ATTR_2(in6_max, S_IWUSR | S_IRUGO, show_in_reg,
1061 store_in_reg, 6, 2);
1062static SENSOR_DEVICE_ATTR_2(in7_max, S_IWUSR | S_IRUGO, show_in_reg,
1063 store_in_reg, 7, 2);
1064static SENSOR_DEVICE_ATTR_2(in8_max, S_IWUSR | S_IRUGO, show_in_reg,
1065 store_in_reg, 8, 2);
1066static SENSOR_DEVICE_ATTR_2(in9_max, S_IWUSR | S_IRUGO, show_in_reg,
1067 store_in_reg, 9, 2);
1068static SENSOR_DEVICE_ATTR_2(in10_max, S_IWUSR | S_IRUGO, show_in_reg,
1069 store_in_reg, 10, 2);
1070static SENSOR_DEVICE_ATTR_2(in11_max, S_IWUSR | S_IRUGO, show_in_reg,
1071 store_in_reg, 11, 2);
1072static SENSOR_DEVICE_ATTR_2(in12_max, S_IWUSR | S_IRUGO, show_in_reg,
1073 store_in_reg, 12, 2);
1074static SENSOR_DEVICE_ATTR_2(in13_max, S_IWUSR | S_IRUGO, show_in_reg,
1075 store_in_reg, 13, 2);
1076static SENSOR_DEVICE_ATTR_2(in14_max, S_IWUSR | S_IRUGO, show_in_reg,
1077 store_in_reg, 14, 2);
1078
1079static struct attribute *nct6775_attributes_in[15][5] = {
1080 {
1081 &sensor_dev_attr_in0_input.dev_attr.attr,
1082 &sensor_dev_attr_in0_min.dev_attr.attr,
1083 &sensor_dev_attr_in0_max.dev_attr.attr,
1084 &sensor_dev_attr_in0_alarm.dev_attr.attr,
1085 NULL
1086 },
1087 {
1088 &sensor_dev_attr_in1_input.dev_attr.attr,
1089 &sensor_dev_attr_in1_min.dev_attr.attr,
1090 &sensor_dev_attr_in1_max.dev_attr.attr,
1091 &sensor_dev_attr_in1_alarm.dev_attr.attr,
1092 NULL
1093 },
1094 {
1095 &sensor_dev_attr_in2_input.dev_attr.attr,
1096 &sensor_dev_attr_in2_min.dev_attr.attr,
1097 &sensor_dev_attr_in2_max.dev_attr.attr,
1098 &sensor_dev_attr_in2_alarm.dev_attr.attr,
1099 NULL
1100 },
1101 {
1102 &sensor_dev_attr_in3_input.dev_attr.attr,
1103 &sensor_dev_attr_in3_min.dev_attr.attr,
1104 &sensor_dev_attr_in3_max.dev_attr.attr,
1105 &sensor_dev_attr_in3_alarm.dev_attr.attr,
1106 NULL
1107 },
1108 {
1109 &sensor_dev_attr_in4_input.dev_attr.attr,
1110 &sensor_dev_attr_in4_min.dev_attr.attr,
1111 &sensor_dev_attr_in4_max.dev_attr.attr,
1112 &sensor_dev_attr_in4_alarm.dev_attr.attr,
1113 NULL
1114 },
1115 {
1116 &sensor_dev_attr_in5_input.dev_attr.attr,
1117 &sensor_dev_attr_in5_min.dev_attr.attr,
1118 &sensor_dev_attr_in5_max.dev_attr.attr,
1119 &sensor_dev_attr_in5_alarm.dev_attr.attr,
1120 NULL
1121 },
1122 {
1123 &sensor_dev_attr_in6_input.dev_attr.attr,
1124 &sensor_dev_attr_in6_min.dev_attr.attr,
1125 &sensor_dev_attr_in6_max.dev_attr.attr,
1126 &sensor_dev_attr_in6_alarm.dev_attr.attr,
1127 NULL
1128 },
1129 {
1130 &sensor_dev_attr_in7_input.dev_attr.attr,
1131 &sensor_dev_attr_in7_min.dev_attr.attr,
1132 &sensor_dev_attr_in7_max.dev_attr.attr,
1133 &sensor_dev_attr_in7_alarm.dev_attr.attr,
1134 NULL
1135 },
1136 {
1137 &sensor_dev_attr_in8_input.dev_attr.attr,
1138 &sensor_dev_attr_in8_min.dev_attr.attr,
1139 &sensor_dev_attr_in8_max.dev_attr.attr,
1140 &sensor_dev_attr_in8_alarm.dev_attr.attr,
1141 NULL
1142 },
1143 {
1144 &sensor_dev_attr_in9_input.dev_attr.attr,
1145 &sensor_dev_attr_in9_min.dev_attr.attr,
1146 &sensor_dev_attr_in9_max.dev_attr.attr,
1147 &sensor_dev_attr_in9_alarm.dev_attr.attr,
1148 NULL
1149 },
1150 {
1151 &sensor_dev_attr_in10_input.dev_attr.attr,
1152 &sensor_dev_attr_in10_min.dev_attr.attr,
1153 &sensor_dev_attr_in10_max.dev_attr.attr,
1154 &sensor_dev_attr_in10_alarm.dev_attr.attr,
1155 NULL
1156 },
1157 {
1158 &sensor_dev_attr_in11_input.dev_attr.attr,
1159 &sensor_dev_attr_in11_min.dev_attr.attr,
1160 &sensor_dev_attr_in11_max.dev_attr.attr,
1161 &sensor_dev_attr_in11_alarm.dev_attr.attr,
1162 NULL
1163 },
1164 {
1165 &sensor_dev_attr_in12_input.dev_attr.attr,
1166 &sensor_dev_attr_in12_min.dev_attr.attr,
1167 &sensor_dev_attr_in12_max.dev_attr.attr,
1168 &sensor_dev_attr_in12_alarm.dev_attr.attr,
1169 NULL
1170 },
1171 {
1172 &sensor_dev_attr_in13_input.dev_attr.attr,
1173 &sensor_dev_attr_in13_min.dev_attr.attr,
1174 &sensor_dev_attr_in13_max.dev_attr.attr,
1175 &sensor_dev_attr_in13_alarm.dev_attr.attr,
1176 NULL
1177 },
1178 {
1179 &sensor_dev_attr_in14_input.dev_attr.attr,
1180 &sensor_dev_attr_in14_min.dev_attr.attr,
1181 &sensor_dev_attr_in14_max.dev_attr.attr,
1182 &sensor_dev_attr_in14_alarm.dev_attr.attr,
1183 NULL
1184 },
1185};
1186
1187static const struct attribute_group nct6775_group_in[15] = {
1188 { .attrs = nct6775_attributes_in[0] },
1189 { .attrs = nct6775_attributes_in[1] },
1190 { .attrs = nct6775_attributes_in[2] },
1191 { .attrs = nct6775_attributes_in[3] },
1192 { .attrs = nct6775_attributes_in[4] },
1193 { .attrs = nct6775_attributes_in[5] },
1194 { .attrs = nct6775_attributes_in[6] },
1195 { .attrs = nct6775_attributes_in[7] },
1196 { .attrs = nct6775_attributes_in[8] },
1197 { .attrs = nct6775_attributes_in[9] },
1198 { .attrs = nct6775_attributes_in[10] },
1199 { .attrs = nct6775_attributes_in[11] },
1200 { .attrs = nct6775_attributes_in[12] },
1201 { .attrs = nct6775_attributes_in[13] },
1202 { .attrs = nct6775_attributes_in[14] },
1203};
1204
1205static ssize_t
Guenter Roeck1c65dc32012-12-04 07:56:24 -08001206show_fan(struct device *dev, struct device_attribute *attr, char *buf)
1207{
1208 struct nct6775_data *data = nct6775_update_device(dev);
1209 struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr);
1210 int nr = sattr->index;
1211 return sprintf(buf, "%d\n", data->rpm[nr]);
1212}
1213
1214static ssize_t
1215show_fan_min(struct device *dev, struct device_attribute *attr, char *buf)
1216{
1217 struct nct6775_data *data = nct6775_update_device(dev);
1218 struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr);
1219 int nr = sattr->index;
1220 return sprintf(buf, "%d\n",
1221 data->fan_from_reg_min(data->fan_min[nr],
1222 data->fan_div[nr]));
1223}
1224
1225static ssize_t
1226show_fan_div(struct device *dev, struct device_attribute *attr, char *buf)
1227{
1228 struct nct6775_data *data = nct6775_update_device(dev);
1229 struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr);
1230 int nr = sattr->index;
1231 return sprintf(buf, "%u\n", div_from_reg(data->fan_div[nr]));
1232}
1233
1234static ssize_t
1235store_fan_min(struct device *dev, struct device_attribute *attr,
1236 const char *buf, size_t count)
1237{
1238 struct nct6775_data *data = dev_get_drvdata(dev);
1239 struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr);
1240 int nr = sattr->index;
1241 unsigned long val;
1242 int err;
1243 unsigned int reg;
1244 u8 new_div;
1245
1246 err = kstrtoul(buf, 10, &val);
1247 if (err < 0)
1248 return err;
1249
1250 mutex_lock(&data->update_lock);
1251 if (!data->has_fan_div) {
1252 /* NCT6776F or NCT6779D; we know this is a 13 bit register */
1253 if (!val) {
1254 val = 0xff1f;
1255 } else {
1256 if (val > 1350000U)
1257 val = 135000U;
1258 val = 1350000U / val;
1259 val = (val & 0x1f) | ((val << 3) & 0xff00);
1260 }
1261 data->fan_min[nr] = val;
1262 goto write_min; /* Leave fan divider alone */
1263 }
1264 if (!val) {
1265 /* No min limit, alarm disabled */
1266 data->fan_min[nr] = 255;
1267 new_div = data->fan_div[nr]; /* No change */
1268 dev_info(dev, "fan%u low limit and alarm disabled\n", nr + 1);
1269 goto write_div;
1270 }
1271 reg = 1350000U / val;
1272 if (reg >= 128 * 255) {
1273 /*
1274 * Speed below this value cannot possibly be represented,
1275 * even with the highest divider (128)
1276 */
1277 data->fan_min[nr] = 254;
1278 new_div = 7; /* 128 == (1 << 7) */
1279 dev_warn(dev,
1280 "fan%u low limit %lu below minimum %u, set to minimum\n",
1281 nr + 1, val, data->fan_from_reg_min(254, 7));
1282 } else if (!reg) {
1283 /*
1284 * Speed above this value cannot possibly be represented,
1285 * even with the lowest divider (1)
1286 */
1287 data->fan_min[nr] = 1;
1288 new_div = 0; /* 1 == (1 << 0) */
1289 dev_warn(dev,
1290 "fan%u low limit %lu above maximum %u, set to maximum\n",
1291 nr + 1, val, data->fan_from_reg_min(1, 0));
1292 } else {
1293 /*
1294 * Automatically pick the best divider, i.e. the one such
1295 * that the min limit will correspond to a register value
1296 * in the 96..192 range
1297 */
1298 new_div = 0;
1299 while (reg > 192 && new_div < 7) {
1300 reg >>= 1;
1301 new_div++;
1302 }
1303 data->fan_min[nr] = reg;
1304 }
1305
1306write_div:
1307 /*
1308 * Write both the fan clock divider (if it changed) and the new
1309 * fan min (unconditionally)
1310 */
1311 if (new_div != data->fan_div[nr]) {
1312 dev_dbg(dev, "fan%u clock divider changed from %u to %u\n",
1313 nr + 1, div_from_reg(data->fan_div[nr]),
1314 div_from_reg(new_div));
1315 data->fan_div[nr] = new_div;
1316 nct6775_write_fan_div_common(data, nr);
1317 /* Give the chip time to sample a new speed value */
1318 data->last_updated = jiffies;
1319 }
1320
1321write_min:
1322 nct6775_write_value(data, data->REG_FAN_MIN[nr], data->fan_min[nr]);
1323 mutex_unlock(&data->update_lock);
1324
1325 return count;
1326}
1327
Guenter Roeck5c25d952012-12-11 07:29:06 -08001328static ssize_t
1329show_fan_pulses(struct device *dev, struct device_attribute *attr, char *buf)
1330{
1331 struct nct6775_data *data = nct6775_update_device(dev);
1332 struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr);
1333 int p = data->fan_pulses[sattr->index];
1334
1335 return sprintf(buf, "%d\n", p ? : 4);
1336}
1337
1338static ssize_t
1339store_fan_pulses(struct device *dev, struct device_attribute *attr,
1340 const char *buf, size_t count)
1341{
1342 struct nct6775_data *data = dev_get_drvdata(dev);
1343 struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr);
1344 int nr = sattr->index;
1345 unsigned long val;
1346 int err;
1347
1348 err = kstrtoul(buf, 10, &val);
1349 if (err < 0)
1350 return err;
1351
1352 if (val > 4)
1353 return -EINVAL;
1354
1355 mutex_lock(&data->update_lock);
1356 data->fan_pulses[nr] = val & 3;
1357 nct6775_write_value(data, data->REG_FAN_PULSES[nr], val & 3);
1358 mutex_unlock(&data->update_lock);
1359
1360 return count;
1361}
1362
Guenter Roeck1c65dc32012-12-04 07:56:24 -08001363static struct sensor_device_attribute sda_fan_input[] = {
1364 SENSOR_ATTR(fan1_input, S_IRUGO, show_fan, NULL, 0),
1365 SENSOR_ATTR(fan2_input, S_IRUGO, show_fan, NULL, 1),
1366 SENSOR_ATTR(fan3_input, S_IRUGO, show_fan, NULL, 2),
1367 SENSOR_ATTR(fan4_input, S_IRUGO, show_fan, NULL, 3),
1368 SENSOR_ATTR(fan5_input, S_IRUGO, show_fan, NULL, 4),
1369};
1370
1371static struct sensor_device_attribute sda_fan_alarm[] = {
1372 SENSOR_ATTR(fan1_alarm, S_IRUGO, show_alarm, NULL, FAN_ALARM_BASE),
1373 SENSOR_ATTR(fan2_alarm, S_IRUGO, show_alarm, NULL, FAN_ALARM_BASE + 1),
1374 SENSOR_ATTR(fan3_alarm, S_IRUGO, show_alarm, NULL, FAN_ALARM_BASE + 2),
1375 SENSOR_ATTR(fan4_alarm, S_IRUGO, show_alarm, NULL, FAN_ALARM_BASE + 3),
1376 SENSOR_ATTR(fan5_alarm, S_IRUGO, show_alarm, NULL, FAN_ALARM_BASE + 4),
1377};
1378
1379static struct sensor_device_attribute sda_fan_min[] = {
1380 SENSOR_ATTR(fan1_min, S_IWUSR | S_IRUGO, show_fan_min,
1381 store_fan_min, 0),
1382 SENSOR_ATTR(fan2_min, S_IWUSR | S_IRUGO, show_fan_min,
1383 store_fan_min, 1),
1384 SENSOR_ATTR(fan3_min, S_IWUSR | S_IRUGO, show_fan_min,
1385 store_fan_min, 2),
1386 SENSOR_ATTR(fan4_min, S_IWUSR | S_IRUGO, show_fan_min,
1387 store_fan_min, 3),
1388 SENSOR_ATTR(fan5_min, S_IWUSR | S_IRUGO, show_fan_min,
1389 store_fan_min, 4),
1390};
1391
Guenter Roeck5c25d952012-12-11 07:29:06 -08001392static struct sensor_device_attribute sda_fan_pulses[] = {
1393 SENSOR_ATTR(fan1_pulses, S_IWUSR | S_IRUGO, show_fan_pulses,
1394 store_fan_pulses, 0),
1395 SENSOR_ATTR(fan2_pulses, S_IWUSR | S_IRUGO, show_fan_pulses,
1396 store_fan_pulses, 1),
1397 SENSOR_ATTR(fan3_pulses, S_IWUSR | S_IRUGO, show_fan_pulses,
1398 store_fan_pulses, 2),
1399 SENSOR_ATTR(fan4_pulses, S_IWUSR | S_IRUGO, show_fan_pulses,
1400 store_fan_pulses, 3),
1401 SENSOR_ATTR(fan5_pulses, S_IWUSR | S_IRUGO, show_fan_pulses,
1402 store_fan_pulses, 4),
1403};
1404
Guenter Roeck1c65dc32012-12-04 07:56:24 -08001405static struct sensor_device_attribute sda_fan_div[] = {
1406 SENSOR_ATTR(fan1_div, S_IRUGO, show_fan_div, NULL, 0),
1407 SENSOR_ATTR(fan2_div, S_IRUGO, show_fan_div, NULL, 1),
1408 SENSOR_ATTR(fan3_div, S_IRUGO, show_fan_div, NULL, 2),
1409 SENSOR_ATTR(fan4_div, S_IRUGO, show_fan_div, NULL, 3),
1410 SENSOR_ATTR(fan5_div, S_IRUGO, show_fan_div, NULL, 4),
1411};
1412
1413static ssize_t
Guenter Roeckaa136e52012-12-04 03:26:05 -08001414show_temp_label(struct device *dev, struct device_attribute *attr, char *buf)
1415{
1416 struct nct6775_data *data = nct6775_update_device(dev);
1417 struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr);
1418 int nr = sattr->index;
1419 return sprintf(buf, "%s\n", data->temp_label[data->temp_src[nr]]);
1420}
1421
1422static ssize_t
1423show_temp(struct device *dev, struct device_attribute *attr, char *buf)
1424{
1425 struct nct6775_data *data = nct6775_update_device(dev);
1426 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
1427 int nr = sattr->nr;
1428 int index = sattr->index;
1429
1430 return sprintf(buf, "%d\n", LM75_TEMP_FROM_REG(data->temp[index][nr]));
1431}
1432
1433static ssize_t
1434store_temp(struct device *dev, struct device_attribute *attr, const char *buf,
1435 size_t count)
1436{
1437 struct nct6775_data *data = dev_get_drvdata(dev);
1438 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
1439 int nr = sattr->nr;
1440 int index = sattr->index;
1441 int err;
1442 long val;
1443
1444 err = kstrtol(buf, 10, &val);
1445 if (err < 0)
1446 return err;
1447
1448 mutex_lock(&data->update_lock);
1449 data->temp[index][nr] = LM75_TEMP_TO_REG(val);
1450 nct6775_write_temp(data, data->reg_temp[index][nr],
1451 data->temp[index][nr]);
1452 mutex_unlock(&data->update_lock);
1453 return count;
1454}
1455
1456static ssize_t
1457show_temp_offset(struct device *dev, struct device_attribute *attr, char *buf)
1458{
1459 struct nct6775_data *data = nct6775_update_device(dev);
1460 struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr);
1461
1462 return sprintf(buf, "%d\n", data->temp_offset[sattr->index] * 1000);
1463}
1464
1465static ssize_t
1466store_temp_offset(struct device *dev, struct device_attribute *attr,
1467 const char *buf, size_t count)
1468{
1469 struct nct6775_data *data = dev_get_drvdata(dev);
1470 struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr);
1471 int nr = sattr->index;
1472 long val;
1473 int err;
1474
1475 err = kstrtol(buf, 10, &val);
1476 if (err < 0)
1477 return err;
1478
1479 val = clamp_val(DIV_ROUND_CLOSEST(val, 1000), -128, 127);
1480
1481 mutex_lock(&data->update_lock);
1482 data->temp_offset[nr] = val;
1483 nct6775_write_value(data, data->REG_TEMP_OFFSET[nr], val);
1484 mutex_unlock(&data->update_lock);
1485
1486 return count;
1487}
1488
1489static ssize_t
1490show_temp_type(struct device *dev, struct device_attribute *attr, char *buf)
1491{
1492 struct nct6775_data *data = nct6775_update_device(dev);
1493 struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr);
1494 int nr = sattr->index;
1495 return sprintf(buf, "%d\n", (int)data->temp_type[nr]);
1496}
1497
1498static ssize_t
1499store_temp_type(struct device *dev, struct device_attribute *attr,
1500 const char *buf, size_t count)
1501{
1502 struct nct6775_data *data = nct6775_update_device(dev);
1503 struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr);
1504 int nr = sattr->index;
1505 unsigned long val;
1506 int err;
1507 u8 vbat, diode, bit;
1508
1509 err = kstrtoul(buf, 10, &val);
1510 if (err < 0)
1511 return err;
1512
1513 if (val != 1 && val != 3 && val != 4)
1514 return -EINVAL;
1515
1516 mutex_lock(&data->update_lock);
1517
1518 data->temp_type[nr] = val;
1519 vbat = nct6775_read_value(data, data->REG_VBAT) & ~(0x02 << nr);
1520 diode = nct6775_read_value(data, data->REG_DIODE) & ~(0x02 << nr);
1521 bit = 0x02 << nr;
1522 switch (val) {
1523 case 1: /* CPU diode (diode, current mode) */
1524 vbat |= bit;
1525 diode |= bit;
1526 break;
1527 case 3: /* diode, voltage mode */
1528 vbat |= bit;
1529 break;
1530 case 4: /* thermistor */
1531 break;
1532 }
1533 nct6775_write_value(data, data->REG_VBAT, vbat);
1534 nct6775_write_value(data, data->REG_DIODE, diode);
1535
1536 mutex_unlock(&data->update_lock);
1537 return count;
1538}
1539
1540static struct sensor_device_attribute_2 sda_temp_input[] = {
1541 SENSOR_ATTR_2(temp1_input, S_IRUGO, show_temp, NULL, 0, 0),
1542 SENSOR_ATTR_2(temp2_input, S_IRUGO, show_temp, NULL, 1, 0),
1543 SENSOR_ATTR_2(temp3_input, S_IRUGO, show_temp, NULL, 2, 0),
1544 SENSOR_ATTR_2(temp4_input, S_IRUGO, show_temp, NULL, 3, 0),
1545 SENSOR_ATTR_2(temp5_input, S_IRUGO, show_temp, NULL, 4, 0),
1546 SENSOR_ATTR_2(temp6_input, S_IRUGO, show_temp, NULL, 5, 0),
1547 SENSOR_ATTR_2(temp7_input, S_IRUGO, show_temp, NULL, 6, 0),
1548 SENSOR_ATTR_2(temp8_input, S_IRUGO, show_temp, NULL, 7, 0),
1549 SENSOR_ATTR_2(temp9_input, S_IRUGO, show_temp, NULL, 8, 0),
1550 SENSOR_ATTR_2(temp10_input, S_IRUGO, show_temp, NULL, 9, 0),
1551};
1552
1553static struct sensor_device_attribute sda_temp_label[] = {
1554 SENSOR_ATTR(temp1_label, S_IRUGO, show_temp_label, NULL, 0),
1555 SENSOR_ATTR(temp2_label, S_IRUGO, show_temp_label, NULL, 1),
1556 SENSOR_ATTR(temp3_label, S_IRUGO, show_temp_label, NULL, 2),
1557 SENSOR_ATTR(temp4_label, S_IRUGO, show_temp_label, NULL, 3),
1558 SENSOR_ATTR(temp5_label, S_IRUGO, show_temp_label, NULL, 4),
1559 SENSOR_ATTR(temp6_label, S_IRUGO, show_temp_label, NULL, 5),
1560 SENSOR_ATTR(temp7_label, S_IRUGO, show_temp_label, NULL, 6),
1561 SENSOR_ATTR(temp8_label, S_IRUGO, show_temp_label, NULL, 7),
1562 SENSOR_ATTR(temp9_label, S_IRUGO, show_temp_label, NULL, 8),
1563 SENSOR_ATTR(temp10_label, S_IRUGO, show_temp_label, NULL, 9),
1564};
1565
1566static struct sensor_device_attribute_2 sda_temp_max[] = {
1567 SENSOR_ATTR_2(temp1_max, S_IRUGO | S_IWUSR, show_temp, store_temp,
1568 0, 1),
1569 SENSOR_ATTR_2(temp2_max, S_IRUGO | S_IWUSR, show_temp, store_temp,
1570 1, 1),
1571 SENSOR_ATTR_2(temp3_max, S_IRUGO | S_IWUSR, show_temp, store_temp,
1572 2, 1),
1573 SENSOR_ATTR_2(temp4_max, S_IRUGO | S_IWUSR, show_temp, store_temp,
1574 3, 1),
1575 SENSOR_ATTR_2(temp5_max, S_IRUGO | S_IWUSR, show_temp, store_temp,
1576 4, 1),
1577 SENSOR_ATTR_2(temp6_max, S_IRUGO | S_IWUSR, show_temp, store_temp,
1578 5, 1),
1579 SENSOR_ATTR_2(temp7_max, S_IRUGO | S_IWUSR, show_temp, store_temp,
1580 6, 1),
1581 SENSOR_ATTR_2(temp8_max, S_IRUGO | S_IWUSR, show_temp, store_temp,
1582 7, 1),
1583 SENSOR_ATTR_2(temp9_max, S_IRUGO | S_IWUSR, show_temp, store_temp,
1584 8, 1),
1585 SENSOR_ATTR_2(temp10_max, S_IRUGO | S_IWUSR, show_temp, store_temp,
1586 9, 1),
1587};
1588
1589static struct sensor_device_attribute_2 sda_temp_max_hyst[] = {
1590 SENSOR_ATTR_2(temp1_max_hyst, S_IRUGO | S_IWUSR, show_temp, store_temp,
1591 0, 2),
1592 SENSOR_ATTR_2(temp2_max_hyst, S_IRUGO | S_IWUSR, show_temp, store_temp,
1593 1, 2),
1594 SENSOR_ATTR_2(temp3_max_hyst, S_IRUGO | S_IWUSR, show_temp, store_temp,
1595 2, 2),
1596 SENSOR_ATTR_2(temp4_max_hyst, S_IRUGO | S_IWUSR, show_temp, store_temp,
1597 3, 2),
1598 SENSOR_ATTR_2(temp5_max_hyst, S_IRUGO | S_IWUSR, show_temp, store_temp,
1599 4, 2),
1600 SENSOR_ATTR_2(temp6_max_hyst, S_IRUGO | S_IWUSR, show_temp, store_temp,
1601 5, 2),
1602 SENSOR_ATTR_2(temp7_max_hyst, S_IRUGO | S_IWUSR, show_temp, store_temp,
1603 6, 2),
1604 SENSOR_ATTR_2(temp8_max_hyst, S_IRUGO | S_IWUSR, show_temp, store_temp,
1605 7, 2),
1606 SENSOR_ATTR_2(temp9_max_hyst, S_IRUGO | S_IWUSR, show_temp, store_temp,
1607 8, 2),
1608 SENSOR_ATTR_2(temp10_max_hyst, S_IRUGO | S_IWUSR, show_temp, store_temp,
1609 9, 2),
1610};
1611
1612static struct sensor_device_attribute_2 sda_temp_crit[] = {
1613 SENSOR_ATTR_2(temp1_crit, S_IRUGO | S_IWUSR, show_temp, store_temp,
1614 0, 3),
1615 SENSOR_ATTR_2(temp2_crit, S_IRUGO | S_IWUSR, show_temp, store_temp,
1616 1, 3),
1617 SENSOR_ATTR_2(temp3_crit, S_IRUGO | S_IWUSR, show_temp, store_temp,
1618 2, 3),
1619 SENSOR_ATTR_2(temp4_crit, S_IRUGO | S_IWUSR, show_temp, store_temp,
1620 3, 3),
1621 SENSOR_ATTR_2(temp5_crit, S_IRUGO | S_IWUSR, show_temp, store_temp,
1622 4, 3),
1623 SENSOR_ATTR_2(temp6_crit, S_IRUGO | S_IWUSR, show_temp, store_temp,
1624 5, 3),
1625 SENSOR_ATTR_2(temp7_crit, S_IRUGO | S_IWUSR, show_temp, store_temp,
1626 6, 3),
1627 SENSOR_ATTR_2(temp8_crit, S_IRUGO | S_IWUSR, show_temp, store_temp,
1628 7, 3),
1629 SENSOR_ATTR_2(temp9_crit, S_IRUGO | S_IWUSR, show_temp, store_temp,
1630 8, 3),
1631 SENSOR_ATTR_2(temp10_crit, S_IRUGO | S_IWUSR, show_temp, store_temp,
1632 9, 3),
1633};
1634
1635static struct sensor_device_attribute sda_temp_offset[] = {
1636 SENSOR_ATTR(temp1_offset, S_IRUGO | S_IWUSR, show_temp_offset,
1637 store_temp_offset, 0),
1638 SENSOR_ATTR(temp2_offset, S_IRUGO | S_IWUSR, show_temp_offset,
1639 store_temp_offset, 1),
1640 SENSOR_ATTR(temp3_offset, S_IRUGO | S_IWUSR, show_temp_offset,
1641 store_temp_offset, 2),
1642 SENSOR_ATTR(temp4_offset, S_IRUGO | S_IWUSR, show_temp_offset,
1643 store_temp_offset, 3),
1644 SENSOR_ATTR(temp5_offset, S_IRUGO | S_IWUSR, show_temp_offset,
1645 store_temp_offset, 4),
1646 SENSOR_ATTR(temp6_offset, S_IRUGO | S_IWUSR, show_temp_offset,
1647 store_temp_offset, 5),
1648};
1649
1650static struct sensor_device_attribute sda_temp_type[] = {
1651 SENSOR_ATTR(temp1_type, S_IRUGO | S_IWUSR, show_temp_type,
1652 store_temp_type, 0),
1653 SENSOR_ATTR(temp2_type, S_IRUGO | S_IWUSR, show_temp_type,
1654 store_temp_type, 1),
1655 SENSOR_ATTR(temp3_type, S_IRUGO | S_IWUSR, show_temp_type,
1656 store_temp_type, 2),
1657 SENSOR_ATTR(temp4_type, S_IRUGO | S_IWUSR, show_temp_type,
1658 store_temp_type, 3),
1659 SENSOR_ATTR(temp5_type, S_IRUGO | S_IWUSR, show_temp_type,
1660 store_temp_type, 4),
1661 SENSOR_ATTR(temp6_type, S_IRUGO | S_IWUSR, show_temp_type,
1662 store_temp_type, 5),
1663};
1664
1665static struct sensor_device_attribute sda_temp_alarm[] = {
1666 SENSOR_ATTR(temp1_alarm, S_IRUGO, show_alarm, NULL,
1667 TEMP_ALARM_BASE),
1668 SENSOR_ATTR(temp2_alarm, S_IRUGO, show_alarm, NULL,
1669 TEMP_ALARM_BASE + 1),
1670 SENSOR_ATTR(temp3_alarm, S_IRUGO, show_alarm, NULL,
1671 TEMP_ALARM_BASE + 2),
1672 SENSOR_ATTR(temp4_alarm, S_IRUGO, show_alarm, NULL,
1673 TEMP_ALARM_BASE + 3),
1674 SENSOR_ATTR(temp5_alarm, S_IRUGO, show_alarm, NULL,
1675 TEMP_ALARM_BASE + 4),
1676 SENSOR_ATTR(temp6_alarm, S_IRUGO, show_alarm, NULL,
1677 TEMP_ALARM_BASE + 5),
1678};
1679
1680#define NUM_TEMP_ALARM ARRAY_SIZE(sda_temp_alarm)
1681
1682static ssize_t
Guenter Roeck77eb5b32012-12-04 08:30:54 -08001683show_pwm_mode(struct device *dev, struct device_attribute *attr, char *buf)
1684{
1685 struct nct6775_data *data = nct6775_update_device(dev);
1686 struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr);
1687
1688 return sprintf(buf, "%d\n", !data->pwm_mode[sattr->index]);
1689}
1690
1691static ssize_t
1692store_pwm_mode(struct device *dev, struct device_attribute *attr,
1693 const char *buf, size_t count)
1694{
1695 struct nct6775_data *data = dev_get_drvdata(dev);
1696 struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr);
1697 int nr = sattr->index;
1698 unsigned long val;
1699 int err;
1700 u8 reg;
1701
1702 err = kstrtoul(buf, 10, &val);
1703 if (err < 0)
1704 return err;
1705
1706 if (val > 1)
1707 return -EINVAL;
1708
1709 /* Setting DC mode is not supported for all chips/channels */
1710 if (data->REG_PWM_MODE[nr] == 0) {
1711 if (val)
1712 return -EINVAL;
1713 return count;
1714 }
1715
1716 mutex_lock(&data->update_lock);
1717 data->pwm_mode[nr] = val;
1718 reg = nct6775_read_value(data, data->REG_PWM_MODE[nr]);
1719 reg &= ~data->PWM_MODE_MASK[nr];
1720 if (val)
1721 reg |= data->PWM_MODE_MASK[nr];
1722 nct6775_write_value(data, data->REG_PWM_MODE[nr], reg);
1723 mutex_unlock(&data->update_lock);
1724 return count;
1725}
1726
1727static ssize_t
1728show_pwm(struct device *dev, struct device_attribute *attr, char *buf)
1729{
1730 struct nct6775_data *data = nct6775_update_device(dev);
1731 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
1732 int nr = sattr->nr;
1733 int index = sattr->index;
1734 int pwm;
1735
1736 /*
1737 * For automatic fan control modes, show current pwm readings.
1738 * Otherwise, show the configured value.
1739 */
1740 if (index == 0 && data->pwm_enable[nr] > manual)
1741 pwm = nct6775_read_value(data, data->REG_PWM_READ[nr]);
1742 else
1743 pwm = data->pwm[index][nr];
1744
1745 return sprintf(buf, "%d\n", pwm);
1746}
1747
1748static ssize_t
1749store_pwm(struct device *dev, struct device_attribute *attr, const char *buf,
1750 size_t count)
1751{
1752 struct nct6775_data *data = dev_get_drvdata(dev);
1753 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
1754 int nr = sattr->nr;
1755 int index = sattr->index;
1756 unsigned long val;
1757 int err;
1758
1759 err = kstrtoul(buf, 10, &val);
1760 if (err < 0)
1761 return err;
1762 val = clamp_val(val, 0, 255);
1763
1764 mutex_lock(&data->update_lock);
1765 data->pwm[index][nr] = val;
1766 nct6775_write_value(data, data->REG_PWM[index][nr], val);
1767 mutex_unlock(&data->update_lock);
1768 return count;
1769}
1770
1771static ssize_t
1772show_pwm_enable(struct device *dev, struct device_attribute *attr, char *buf)
1773{
1774 struct nct6775_data *data = nct6775_update_device(dev);
1775 struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr);
1776
1777 return sprintf(buf, "%d\n", data->pwm_enable[sattr->index]);
1778}
1779
1780static ssize_t
1781store_pwm_enable(struct device *dev, struct device_attribute *attr,
1782 const char *buf, size_t count)
1783{
1784 struct nct6775_data *data = dev_get_drvdata(dev);
1785 struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr);
1786 int nr = sattr->index;
1787 unsigned long val;
1788 int err;
1789 u16 reg;
1790
1791 err = kstrtoul(buf, 10, &val);
1792 if (err < 0)
1793 return err;
1794
1795 if (val > sf4)
1796 return -EINVAL;
1797
1798 if (val == sf3 && data->kind != nct6775)
1799 return -EINVAL;
1800
1801 mutex_lock(&data->update_lock);
1802 data->pwm_enable[nr] = val;
1803 if (val == off) {
1804 /*
1805 * turn off pwm control: select manual mode, set pwm to maximum
1806 */
1807 data->pwm[0][nr] = 255;
1808 nct6775_write_value(data, data->REG_PWM[0][nr], 255);
1809 }
1810 reg = nct6775_read_value(data, data->REG_FAN_MODE[nr]);
1811 reg &= 0x0f;
1812 reg |= pwm_enable_to_reg(val) << 4;
1813 nct6775_write_value(data, data->REG_FAN_MODE[nr], reg);
1814 mutex_unlock(&data->update_lock);
1815 return count;
1816}
1817
1818static SENSOR_DEVICE_ATTR_2(pwm1, S_IWUSR | S_IRUGO, show_pwm, store_pwm, 0, 0);
1819static SENSOR_DEVICE_ATTR_2(pwm2, S_IWUSR | S_IRUGO, show_pwm, store_pwm, 1, 0);
1820static SENSOR_DEVICE_ATTR_2(pwm3, S_IWUSR | S_IRUGO, show_pwm, store_pwm, 2, 0);
1821static SENSOR_DEVICE_ATTR_2(pwm4, S_IWUSR | S_IRUGO, show_pwm, store_pwm, 3, 0);
1822static SENSOR_DEVICE_ATTR_2(pwm5, S_IWUSR | S_IRUGO, show_pwm, store_pwm, 4, 0);
1823
1824static SENSOR_DEVICE_ATTR(pwm1_mode, S_IWUSR | S_IRUGO, show_pwm_mode,
1825 store_pwm_mode, 0);
1826static SENSOR_DEVICE_ATTR(pwm2_mode, S_IWUSR | S_IRUGO, show_pwm_mode,
1827 store_pwm_mode, 1);
1828static SENSOR_DEVICE_ATTR(pwm3_mode, S_IWUSR | S_IRUGO, show_pwm_mode,
1829 store_pwm_mode, 2);
1830static SENSOR_DEVICE_ATTR(pwm4_mode, S_IWUSR | S_IRUGO, show_pwm_mode,
1831 store_pwm_mode, 3);
1832static SENSOR_DEVICE_ATTR(pwm5_mode, S_IWUSR | S_IRUGO, show_pwm_mode,
1833 store_pwm_mode, 4);
1834
1835static SENSOR_DEVICE_ATTR(pwm1_enable, S_IWUSR | S_IRUGO, show_pwm_enable,
1836 store_pwm_enable, 0);
1837static SENSOR_DEVICE_ATTR(pwm2_enable, S_IWUSR | S_IRUGO, show_pwm_enable,
1838 store_pwm_enable, 1);
1839static SENSOR_DEVICE_ATTR(pwm3_enable, S_IWUSR | S_IRUGO, show_pwm_enable,
1840 store_pwm_enable, 2);
1841static SENSOR_DEVICE_ATTR(pwm4_enable, S_IWUSR | S_IRUGO, show_pwm_enable,
1842 store_pwm_enable, 3);
1843static SENSOR_DEVICE_ATTR(pwm5_enable, S_IWUSR | S_IRUGO, show_pwm_enable,
1844 store_pwm_enable, 4);
1845
1846static ssize_t
Guenter Roeck9de2e2e2012-05-20 19:29:48 -07001847show_name(struct device *dev, struct device_attribute *attr, char *buf)
1848{
1849 struct nct6775_data *data = dev_get_drvdata(dev);
1850
1851 return sprintf(buf, "%s\n", data->name);
1852}
1853
1854static DEVICE_ATTR(name, S_IRUGO, show_name, NULL);
1855
Guenter Roeck77eb5b32012-12-04 08:30:54 -08001856static struct attribute *nct6775_attributes_pwm[5][4] = {
1857 {
1858 &sensor_dev_attr_pwm1.dev_attr.attr,
1859 &sensor_dev_attr_pwm1_mode.dev_attr.attr,
1860 &sensor_dev_attr_pwm1_enable.dev_attr.attr,
1861 NULL
1862 },
1863 {
1864 &sensor_dev_attr_pwm2.dev_attr.attr,
1865 &sensor_dev_attr_pwm2_mode.dev_attr.attr,
1866 &sensor_dev_attr_pwm2_enable.dev_attr.attr,
1867 NULL
1868 },
1869 {
1870 &sensor_dev_attr_pwm3.dev_attr.attr,
1871 &sensor_dev_attr_pwm3_mode.dev_attr.attr,
1872 &sensor_dev_attr_pwm3_enable.dev_attr.attr,
1873 NULL
1874 },
1875 {
1876 &sensor_dev_attr_pwm4.dev_attr.attr,
1877 &sensor_dev_attr_pwm4_mode.dev_attr.attr,
1878 &sensor_dev_attr_pwm4_enable.dev_attr.attr,
1879 NULL
1880 },
1881 {
1882 &sensor_dev_attr_pwm5.dev_attr.attr,
1883 &sensor_dev_attr_pwm5_mode.dev_attr.attr,
1884 &sensor_dev_attr_pwm5_enable.dev_attr.attr,
1885 NULL
1886 },
1887};
1888
1889static const struct attribute_group nct6775_group_pwm[5] = {
1890 { .attrs = nct6775_attributes_pwm[0] },
1891 { .attrs = nct6775_attributes_pwm[1] },
1892 { .attrs = nct6775_attributes_pwm[2] },
1893 { .attrs = nct6775_attributes_pwm[3] },
1894 { .attrs = nct6775_attributes_pwm[4] },
1895};
1896
Guenter Roeck9de2e2e2012-05-20 19:29:48 -07001897static ssize_t
1898show_vid(struct device *dev, struct device_attribute *attr, char *buf)
1899{
1900 struct nct6775_data *data = dev_get_drvdata(dev);
1901 return sprintf(buf, "%d\n", vid_from_reg(data->vid, data->vrm));
1902}
1903
1904static DEVICE_ATTR(cpu0_vid, S_IRUGO, show_vid, NULL);
1905
Guenter Roecka6bd5872012-12-04 03:13:34 -08001906/* Case open detection */
1907
1908static ssize_t
1909clear_caseopen(struct device *dev, struct device_attribute *attr,
1910 const char *buf, size_t count)
1911{
1912 struct nct6775_data *data = dev_get_drvdata(dev);
1913 struct nct6775_sio_data *sio_data = dev->platform_data;
1914 int nr = to_sensor_dev_attr(attr)->index - INTRUSION_ALARM_BASE;
1915 unsigned long val;
1916 u8 reg;
1917 int ret;
1918
1919 if (kstrtoul(buf, 10, &val) || val != 0)
1920 return -EINVAL;
1921
1922 mutex_lock(&data->update_lock);
1923
1924 /*
1925 * Use CR registers to clear caseopen status.
1926 * The CR registers are the same for all chips, and not all chips
1927 * support clearing the caseopen status through "regular" registers.
1928 */
1929 ret = superio_enter(sio_data->sioreg);
1930 if (ret) {
1931 count = ret;
1932 goto error;
1933 }
1934
1935 superio_select(sio_data->sioreg, NCT6775_LD_ACPI);
1936 reg = superio_inb(sio_data->sioreg, NCT6775_REG_CR_CASEOPEN_CLR[nr]);
1937 reg |= NCT6775_CR_CASEOPEN_CLR_MASK[nr];
1938 superio_outb(sio_data->sioreg, NCT6775_REG_CR_CASEOPEN_CLR[nr], reg);
1939 reg &= ~NCT6775_CR_CASEOPEN_CLR_MASK[nr];
1940 superio_outb(sio_data->sioreg, NCT6775_REG_CR_CASEOPEN_CLR[nr], reg);
1941 superio_exit(sio_data->sioreg);
1942
1943 data->valid = false; /* Force cache refresh */
1944error:
1945 mutex_unlock(&data->update_lock);
1946 return count;
1947}
1948
1949static struct sensor_device_attribute sda_caseopen[] = {
1950 SENSOR_ATTR(intrusion0_alarm, S_IWUSR | S_IRUGO, show_alarm,
1951 clear_caseopen, INTRUSION_ALARM_BASE),
1952 SENSOR_ATTR(intrusion1_alarm, S_IWUSR | S_IRUGO, show_alarm,
1953 clear_caseopen, INTRUSION_ALARM_BASE + 1),
1954};
1955
Guenter Roeck9de2e2e2012-05-20 19:29:48 -07001956/*
1957 * Driver and device management
1958 */
1959
1960static void nct6775_device_remove_files(struct device *dev)
1961{
1962 /*
1963 * some entries in the following arrays may not have been used in
1964 * device_create_file(), but device_remove_file() will ignore them
1965 */
1966 int i;
1967 struct nct6775_data *data = dev_get_drvdata(dev);
1968
Guenter Roeck77eb5b32012-12-04 08:30:54 -08001969 for (i = 0; i < data->pwm_num; i++)
1970 sysfs_remove_group(&dev->kobj, &nct6775_group_pwm[i]);
1971
Guenter Roeck9de2e2e2012-05-20 19:29:48 -07001972 for (i = 0; i < data->in_num; i++)
1973 sysfs_remove_group(&dev->kobj, &nct6775_group_in[i]);
1974
Guenter Roeck1c65dc32012-12-04 07:56:24 -08001975 for (i = 0; i < 5; i++) {
1976 device_remove_file(dev, &sda_fan_input[i].dev_attr);
1977 device_remove_file(dev, &sda_fan_alarm[i].dev_attr);
1978 device_remove_file(dev, &sda_fan_div[i].dev_attr);
1979 device_remove_file(dev, &sda_fan_min[i].dev_attr);
Guenter Roeck5c25d952012-12-11 07:29:06 -08001980 device_remove_file(dev, &sda_fan_pulses[i].dev_attr);
Guenter Roeck1c65dc32012-12-04 07:56:24 -08001981 }
Guenter Roeckaa136e52012-12-04 03:26:05 -08001982 for (i = 0; i < NUM_TEMP; i++) {
1983 if (!(data->have_temp & (1 << i)))
1984 continue;
1985 device_remove_file(dev, &sda_temp_input[i].dev_attr);
1986 device_remove_file(dev, &sda_temp_label[i].dev_attr);
1987 device_remove_file(dev, &sda_temp_max[i].dev_attr);
1988 device_remove_file(dev, &sda_temp_max_hyst[i].dev_attr);
1989 device_remove_file(dev, &sda_temp_crit[i].dev_attr);
1990 if (!(data->have_temp_fixed & (1 << i)))
1991 continue;
1992 device_remove_file(dev, &sda_temp_type[i].dev_attr);
1993 device_remove_file(dev, &sda_temp_offset[i].dev_attr);
1994 if (i >= NUM_TEMP_ALARM)
1995 continue;
1996 device_remove_file(dev, &sda_temp_alarm[i].dev_attr);
1997 }
1998
Guenter Roecka6bd5872012-12-04 03:13:34 -08001999 device_remove_file(dev, &sda_caseopen[0].dev_attr);
2000 device_remove_file(dev, &sda_caseopen[1].dev_attr);
2001
Guenter Roeck9de2e2e2012-05-20 19:29:48 -07002002 device_remove_file(dev, &dev_attr_name);
2003 device_remove_file(dev, &dev_attr_cpu0_vid);
2004}
2005
2006/* Get the monitoring functions started */
2007static inline void nct6775_init_device(struct nct6775_data *data)
2008{
Guenter Roeckaa136e52012-12-04 03:26:05 -08002009 int i;
2010 u8 tmp, diode;
Guenter Roeck9de2e2e2012-05-20 19:29:48 -07002011
2012 /* Start monitoring if needed */
2013 if (data->REG_CONFIG) {
2014 tmp = nct6775_read_value(data, data->REG_CONFIG);
2015 if (!(tmp & 0x01))
2016 nct6775_write_value(data, data->REG_CONFIG, tmp | 0x01);
2017 }
2018
Guenter Roeckaa136e52012-12-04 03:26:05 -08002019 /* Enable temperature sensors if needed */
2020 for (i = 0; i < NUM_TEMP; i++) {
2021 if (!(data->have_temp & (1 << i)))
2022 continue;
2023 if (!data->reg_temp_config[i])
2024 continue;
2025 tmp = nct6775_read_value(data, data->reg_temp_config[i]);
2026 if (tmp & 0x01)
2027 nct6775_write_value(data, data->reg_temp_config[i],
2028 tmp & 0xfe);
2029 }
2030
Guenter Roeck9de2e2e2012-05-20 19:29:48 -07002031 /* Enable VBAT monitoring if needed */
2032 tmp = nct6775_read_value(data, data->REG_VBAT);
2033 if (!(tmp & 0x01))
2034 nct6775_write_value(data, data->REG_VBAT, tmp | 0x01);
Guenter Roeckaa136e52012-12-04 03:26:05 -08002035
2036 diode = nct6775_read_value(data, data->REG_DIODE);
2037
2038 for (i = 0; i < data->temp_fixed_num; i++) {
2039 if (!(data->have_temp_fixed & (1 << i)))
2040 continue;
2041 if ((tmp & (0x02 << i))) /* diode */
2042 data->temp_type[i] = 3 - ((diode >> i) & 0x02);
2043 else /* thermistor */
2044 data->temp_type[i] = 4;
2045 }
Guenter Roeck9de2e2e2012-05-20 19:29:48 -07002046}
2047
Guenter Roeck1c65dc32012-12-04 07:56:24 -08002048static int
2049nct6775_check_fan_inputs(const struct nct6775_sio_data *sio_data,
2050 struct nct6775_data *data)
2051{
2052 int regval;
2053 bool fan3pin, fan3min, fan4pin, fan4min, fan5pin;
Guenter Roeck77eb5b32012-12-04 08:30:54 -08002054 bool pwm3pin, pwm4pin, pwm5pin;
Guenter Roeck1c65dc32012-12-04 07:56:24 -08002055 int ret;
2056
2057 ret = superio_enter(sio_data->sioreg);
2058 if (ret)
2059 return ret;
2060
2061 /* fan4 and fan5 share some pins with the GPIO and serial flash */
2062 if (data->kind == nct6775) {
2063 regval = superio_inb(sio_data->sioreg, 0x2c);
2064
2065 fan3pin = regval & (1 << 6);
2066 fan3min = fan3pin;
Guenter Roeck77eb5b32012-12-04 08:30:54 -08002067 pwm3pin = regval & (1 << 7);
Guenter Roeck1c65dc32012-12-04 07:56:24 -08002068
2069 /* On NCT6775, fan4 shares pins with the fdc interface */
2070 fan4pin = !(superio_inb(sio_data->sioreg, 0x2A) & 0x80);
2071 fan4min = 0;
2072 fan5pin = 0;
Guenter Roeck77eb5b32012-12-04 08:30:54 -08002073 pwm4pin = 0;
2074 pwm5pin = 0;
Guenter Roeck1c65dc32012-12-04 07:56:24 -08002075 } else if (data->kind == nct6776) {
2076 bool gpok = superio_inb(sio_data->sioreg, 0x27) & 0x80;
2077
2078 superio_select(sio_data->sioreg, NCT6775_LD_HWM);
2079 regval = superio_inb(sio_data->sioreg, SIO_REG_ENABLE);
2080
2081 if (regval & 0x80)
2082 fan3pin = gpok;
2083 else
2084 fan3pin = !(superio_inb(sio_data->sioreg, 0x24) & 0x40);
2085
2086 if (regval & 0x40)
2087 fan4pin = gpok;
2088 else
2089 fan4pin = superio_inb(sio_data->sioreg, 0x1C) & 0x01;
2090
2091 if (regval & 0x20)
2092 fan5pin = gpok;
2093 else
2094 fan5pin = superio_inb(sio_data->sioreg, 0x1C) & 0x02;
2095
2096 fan4min = fan4pin;
2097 fan3min = fan3pin;
Guenter Roeck77eb5b32012-12-04 08:30:54 -08002098 pwm3pin = fan3pin;
2099 pwm4pin = 0;
2100 pwm5pin = 0;
Guenter Roeck1c65dc32012-12-04 07:56:24 -08002101 } else { /* NCT6779D */
2102 regval = superio_inb(sio_data->sioreg, 0x1c);
2103
2104 fan3pin = !(regval & (1 << 5));
2105 fan4pin = !(regval & (1 << 6));
2106 fan5pin = !(regval & (1 << 7));
2107
Guenter Roeck77eb5b32012-12-04 08:30:54 -08002108 pwm3pin = !(regval & (1 << 0));
2109 pwm4pin = !(regval & (1 << 1));
2110 pwm5pin = !(regval & (1 << 2));
2111
Guenter Roeck1c65dc32012-12-04 07:56:24 -08002112 fan3min = fan3pin;
2113 fan4min = fan4pin;
2114 }
2115
2116 superio_exit(sio_data->sioreg);
2117
2118 data->has_fan = data->has_fan_min = 0x03; /* fan1 and fan2 */
2119 data->has_fan |= fan3pin << 2;
2120 data->has_fan_min |= fan3min << 2;
2121
2122 data->has_fan |= (fan4pin << 3) | (fan5pin << 4);
2123 data->has_fan_min |= (fan4min << 3) | (fan5pin << 4);
2124
Guenter Roeck77eb5b32012-12-04 08:30:54 -08002125 data->has_pwm = 0x03 | (pwm3pin << 2) | (pwm4pin << 3) | (pwm5pin << 4);
2126
Guenter Roeck1c65dc32012-12-04 07:56:24 -08002127 return 0;
2128}
2129
Guenter Roeck9de2e2e2012-05-20 19:29:48 -07002130static int nct6775_probe(struct platform_device *pdev)
2131{
2132 struct device *dev = &pdev->dev;
2133 struct nct6775_sio_data *sio_data = dev->platform_data;
2134 struct nct6775_data *data;
2135 struct resource *res;
Guenter Roeckaa136e52012-12-04 03:26:05 -08002136 int i, s, err = 0;
2137 int src, mask, available;
2138 const u16 *reg_temp, *reg_temp_over, *reg_temp_hyst, *reg_temp_config;
2139 const u16 *reg_temp_alternate, *reg_temp_crit;
2140 int num_reg_temp;
Guenter Roeck9de2e2e2012-05-20 19:29:48 -07002141
2142 res = platform_get_resource(pdev, IORESOURCE_IO, 0);
2143 if (!devm_request_region(&pdev->dev, res->start, IOREGION_LENGTH,
2144 DRVNAME))
2145 return -EBUSY;
2146
2147 data = devm_kzalloc(&pdev->dev, sizeof(struct nct6775_data),
2148 GFP_KERNEL);
2149 if (!data)
2150 return -ENOMEM;
2151
2152 data->kind = sio_data->kind;
2153 data->addr = res->start;
2154 mutex_init(&data->lock);
2155 mutex_init(&data->update_lock);
2156 data->name = nct6775_device_names[data->kind];
2157 data->bank = 0xff; /* Force initial bank selection */
2158 platform_set_drvdata(pdev, data);
2159
2160 switch (data->kind) {
2161 case nct6775:
2162 data->in_num = 9;
Guenter Roeck77eb5b32012-12-04 08:30:54 -08002163 data->pwm_num = 3;
Guenter Roeck1c65dc32012-12-04 07:56:24 -08002164 data->has_fan_div = true;
Guenter Roeckaa136e52012-12-04 03:26:05 -08002165 data->temp_fixed_num = 3;
Guenter Roeck9de2e2e2012-05-20 19:29:48 -07002166
2167 data->ALARM_BITS = NCT6775_ALARM_BITS;
2168
Guenter Roeck1c65dc32012-12-04 07:56:24 -08002169 data->fan_from_reg = fan_from_reg16;
2170 data->fan_from_reg_min = fan_from_reg8;
2171
Guenter Roeckaa136e52012-12-04 03:26:05 -08002172 data->temp_label = nct6775_temp_label;
2173 data->temp_label_num = ARRAY_SIZE(nct6775_temp_label);
2174
Guenter Roeck9de2e2e2012-05-20 19:29:48 -07002175 data->REG_CONFIG = NCT6775_REG_CONFIG;
2176 data->REG_VBAT = NCT6775_REG_VBAT;
Guenter Roeckaa136e52012-12-04 03:26:05 -08002177 data->REG_DIODE = NCT6775_REG_DIODE;
Guenter Roeck9de2e2e2012-05-20 19:29:48 -07002178 data->REG_VIN = NCT6775_REG_IN;
2179 data->REG_IN_MINMAX[0] = NCT6775_REG_IN_MIN;
2180 data->REG_IN_MINMAX[1] = NCT6775_REG_IN_MAX;
Guenter Roeck1c65dc32012-12-04 07:56:24 -08002181 data->REG_FAN = NCT6775_REG_FAN;
Guenter Roeck77eb5b32012-12-04 08:30:54 -08002182 data->REG_FAN_MODE = NCT6775_REG_FAN_MODE;
Guenter Roeck1c65dc32012-12-04 07:56:24 -08002183 data->REG_FAN_MIN = NCT6775_REG_FAN_MIN;
Guenter Roeck5c25d952012-12-11 07:29:06 -08002184 data->REG_FAN_PULSES = NCT6775_REG_FAN_PULSES;
Guenter Roeck77eb5b32012-12-04 08:30:54 -08002185 data->REG_PWM[0] = NCT6775_REG_PWM;
2186 data->REG_PWM_READ = NCT6775_REG_PWM_READ;
2187 data->REG_PWM_MODE = NCT6775_REG_PWM_MODE;
2188 data->PWM_MODE_MASK = NCT6775_PWM_MODE_MASK;
Guenter Roeckaa136e52012-12-04 03:26:05 -08002189 data->REG_TEMP_OFFSET = NCT6775_REG_TEMP_OFFSET;
2190 data->REG_TEMP_SOURCE = NCT6775_REG_TEMP_SOURCE;
Guenter Roeck9de2e2e2012-05-20 19:29:48 -07002191 data->REG_ALARM = NCT6775_REG_ALARM;
Guenter Roeckaa136e52012-12-04 03:26:05 -08002192
2193 reg_temp = NCT6775_REG_TEMP;
2194 num_reg_temp = ARRAY_SIZE(NCT6775_REG_TEMP);
2195 reg_temp_over = NCT6775_REG_TEMP_OVER;
2196 reg_temp_hyst = NCT6775_REG_TEMP_HYST;
2197 reg_temp_config = NCT6775_REG_TEMP_CONFIG;
2198 reg_temp_alternate = NCT6775_REG_TEMP_ALTERNATE;
2199 reg_temp_crit = NCT6775_REG_TEMP_CRIT;
2200
Guenter Roeck9de2e2e2012-05-20 19:29:48 -07002201 break;
2202 case nct6776:
2203 data->in_num = 9;
Guenter Roeck77eb5b32012-12-04 08:30:54 -08002204 data->pwm_num = 3;
Guenter Roeck1c65dc32012-12-04 07:56:24 -08002205 data->has_fan_div = false;
Guenter Roeckaa136e52012-12-04 03:26:05 -08002206 data->temp_fixed_num = 3;
Guenter Roeck9de2e2e2012-05-20 19:29:48 -07002207
2208 data->ALARM_BITS = NCT6776_ALARM_BITS;
2209
Guenter Roeck1c65dc32012-12-04 07:56:24 -08002210 data->fan_from_reg = fan_from_reg13;
2211 data->fan_from_reg_min = fan_from_reg13;
2212
Guenter Roeckaa136e52012-12-04 03:26:05 -08002213 data->temp_label = nct6776_temp_label;
2214 data->temp_label_num = ARRAY_SIZE(nct6776_temp_label);
2215
Guenter Roeck9de2e2e2012-05-20 19:29:48 -07002216 data->REG_CONFIG = NCT6775_REG_CONFIG;
2217 data->REG_VBAT = NCT6775_REG_VBAT;
Guenter Roeckaa136e52012-12-04 03:26:05 -08002218 data->REG_DIODE = NCT6775_REG_DIODE;
Guenter Roeck9de2e2e2012-05-20 19:29:48 -07002219 data->REG_VIN = NCT6775_REG_IN;
2220 data->REG_IN_MINMAX[0] = NCT6775_REG_IN_MIN;
2221 data->REG_IN_MINMAX[1] = NCT6775_REG_IN_MAX;
Guenter Roeck1c65dc32012-12-04 07:56:24 -08002222 data->REG_FAN = NCT6775_REG_FAN;
Guenter Roeck77eb5b32012-12-04 08:30:54 -08002223 data->REG_FAN_MODE = NCT6775_REG_FAN_MODE;
Guenter Roeck1c65dc32012-12-04 07:56:24 -08002224 data->REG_FAN_MIN = NCT6776_REG_FAN_MIN;
Guenter Roeck5c25d952012-12-11 07:29:06 -08002225 data->REG_FAN_PULSES = NCT6776_REG_FAN_PULSES;
Guenter Roeck77eb5b32012-12-04 08:30:54 -08002226 data->REG_PWM[0] = NCT6775_REG_PWM;
2227 data->REG_PWM_READ = NCT6775_REG_PWM_READ;
2228 data->REG_PWM_MODE = NCT6776_REG_PWM_MODE;
2229 data->PWM_MODE_MASK = NCT6776_PWM_MODE_MASK;
Guenter Roeckaa136e52012-12-04 03:26:05 -08002230 data->REG_TEMP_OFFSET = NCT6775_REG_TEMP_OFFSET;
2231 data->REG_TEMP_SOURCE = NCT6775_REG_TEMP_SOURCE;
Guenter Roeck9de2e2e2012-05-20 19:29:48 -07002232 data->REG_ALARM = NCT6775_REG_ALARM;
Guenter Roeckaa136e52012-12-04 03:26:05 -08002233
2234 reg_temp = NCT6775_REG_TEMP;
2235 num_reg_temp = ARRAY_SIZE(NCT6775_REG_TEMP);
2236 reg_temp_over = NCT6775_REG_TEMP_OVER;
2237 reg_temp_hyst = NCT6775_REG_TEMP_HYST;
2238 reg_temp_config = NCT6776_REG_TEMP_CONFIG;
2239 reg_temp_alternate = NCT6776_REG_TEMP_ALTERNATE;
2240 reg_temp_crit = NCT6776_REG_TEMP_CRIT;
2241
Guenter Roeck9de2e2e2012-05-20 19:29:48 -07002242 break;
2243 case nct6779:
2244 data->in_num = 15;
Guenter Roeck77eb5b32012-12-04 08:30:54 -08002245 data->pwm_num = 5;
Guenter Roeck1c65dc32012-12-04 07:56:24 -08002246 data->has_fan_div = false;
Guenter Roeckaa136e52012-12-04 03:26:05 -08002247 data->temp_fixed_num = 6;
Guenter Roeck9de2e2e2012-05-20 19:29:48 -07002248
2249 data->ALARM_BITS = NCT6779_ALARM_BITS;
2250
Guenter Roeck1c65dc32012-12-04 07:56:24 -08002251 data->fan_from_reg = fan_from_reg13;
2252 data->fan_from_reg_min = fan_from_reg13;
2253
Guenter Roeckaa136e52012-12-04 03:26:05 -08002254 data->temp_label = nct6779_temp_label;
2255 data->temp_label_num = ARRAY_SIZE(nct6779_temp_label);
2256
Guenter Roeck9de2e2e2012-05-20 19:29:48 -07002257 data->REG_CONFIG = NCT6775_REG_CONFIG;
2258 data->REG_VBAT = NCT6775_REG_VBAT;
Guenter Roeckaa136e52012-12-04 03:26:05 -08002259 data->REG_DIODE = NCT6775_REG_DIODE;
Guenter Roeck9de2e2e2012-05-20 19:29:48 -07002260 data->REG_VIN = NCT6779_REG_IN;
2261 data->REG_IN_MINMAX[0] = NCT6775_REG_IN_MIN;
2262 data->REG_IN_MINMAX[1] = NCT6775_REG_IN_MAX;
Guenter Roeck1c65dc32012-12-04 07:56:24 -08002263 data->REG_FAN = NCT6779_REG_FAN;
Guenter Roeck77eb5b32012-12-04 08:30:54 -08002264 data->REG_FAN_MODE = NCT6775_REG_FAN_MODE;
Guenter Roeck1c65dc32012-12-04 07:56:24 -08002265 data->REG_FAN_MIN = NCT6776_REG_FAN_MIN;
Guenter Roeck5c25d952012-12-11 07:29:06 -08002266 data->REG_FAN_PULSES = NCT6779_REG_FAN_PULSES;
Guenter Roeck77eb5b32012-12-04 08:30:54 -08002267 data->REG_PWM[0] = NCT6775_REG_PWM;
2268 data->REG_PWM_READ = NCT6775_REG_PWM_READ;
2269 data->REG_PWM_MODE = NCT6776_REG_PWM_MODE;
2270 data->PWM_MODE_MASK = NCT6776_PWM_MODE_MASK;
Guenter Roeckaa136e52012-12-04 03:26:05 -08002271 data->REG_TEMP_OFFSET = NCT6779_REG_TEMP_OFFSET;
2272 data->REG_TEMP_SOURCE = NCT6775_REG_TEMP_SOURCE;
Guenter Roeck9de2e2e2012-05-20 19:29:48 -07002273 data->REG_ALARM = NCT6779_REG_ALARM;
Guenter Roeckaa136e52012-12-04 03:26:05 -08002274
2275 reg_temp = NCT6779_REG_TEMP;
2276 num_reg_temp = ARRAY_SIZE(NCT6779_REG_TEMP);
2277 reg_temp_over = NCT6779_REG_TEMP_OVER;
2278 reg_temp_hyst = NCT6779_REG_TEMP_HYST;
2279 reg_temp_config = NCT6779_REG_TEMP_CONFIG;
2280 reg_temp_alternate = NCT6779_REG_TEMP_ALTERNATE;
2281 reg_temp_crit = NCT6779_REG_TEMP_CRIT;
2282
Guenter Roeck9de2e2e2012-05-20 19:29:48 -07002283 break;
2284 default:
2285 return -ENODEV;
2286 }
2287 data->have_in = (1 << data->in_num) - 1;
Guenter Roeckaa136e52012-12-04 03:26:05 -08002288 data->have_temp = 0;
2289
2290 /*
2291 * On some boards, not all available temperature sources are monitored,
2292 * even though some of the monitoring registers are unused.
2293 * Get list of unused monitoring registers, then detect if any fan
2294 * controls are configured to use unmonitored temperature sources.
2295 * If so, assign the unmonitored temperature sources to available
2296 * monitoring registers.
2297 */
2298 mask = 0;
2299 available = 0;
2300 for (i = 0; i < num_reg_temp; i++) {
2301 if (reg_temp[i] == 0)
2302 continue;
2303
2304 src = nct6775_read_value(data, data->REG_TEMP_SOURCE[i]) & 0x1f;
2305 if (!src || (mask & (1 << src)))
2306 available |= 1 << i;
2307
2308 mask |= 1 << src;
2309 }
2310
2311 mask = 0;
2312 s = NUM_TEMP_FIXED; /* First dynamic temperature attribute */
2313 for (i = 0; i < num_reg_temp; i++) {
2314 if (reg_temp[i] == 0)
2315 continue;
2316
2317 src = nct6775_read_value(data, data->REG_TEMP_SOURCE[i]) & 0x1f;
2318 if (!src || (mask & (1 << src)))
2319 continue;
2320
2321 if (src >= data->temp_label_num ||
2322 !strlen(data->temp_label[src])) {
2323 dev_info(dev,
2324 "Invalid temperature source %d at index %d, source register 0x%x, temp register 0x%x\n",
2325 src, i, data->REG_TEMP_SOURCE[i], reg_temp[i]);
2326 continue;
2327 }
2328
2329 mask |= 1 << src;
2330
2331 /* Use fixed index for SYSTIN(1), CPUTIN(2), AUXTIN(3) */
2332 if (src <= data->temp_fixed_num) {
2333 data->have_temp |= 1 << (src - 1);
2334 data->have_temp_fixed |= 1 << (src - 1);
2335 data->reg_temp[0][src - 1] = reg_temp[i];
2336 data->reg_temp[1][src - 1] = reg_temp_over[i];
2337 data->reg_temp[2][src - 1] = reg_temp_hyst[i];
2338 data->reg_temp_config[src - 1] = reg_temp_config[i];
2339 data->temp_src[src - 1] = src;
2340 continue;
2341 }
2342
2343 if (s >= NUM_TEMP)
2344 continue;
2345
2346 /* Use dynamic index for other sources */
2347 data->have_temp |= 1 << s;
2348 data->reg_temp[0][s] = reg_temp[i];
2349 data->reg_temp[1][s] = reg_temp_over[i];
2350 data->reg_temp[2][s] = reg_temp_hyst[i];
2351 data->reg_temp_config[s] = reg_temp_config[i];
2352 if (reg_temp_crit[src - 1])
2353 data->reg_temp[3][s] = reg_temp_crit[src - 1];
2354
2355 data->temp_src[s] = src;
2356 s++;
2357 }
2358
2359#ifdef USE_ALTERNATE
2360 /*
2361 * Go through the list of alternate temp registers and enable
2362 * if possible.
2363 * The temperature is already monitored if the respective bit in <mask>
2364 * is set.
2365 */
2366 for (i = 0; i < data->temp_label_num - 1; i++) {
2367 if (!reg_temp_alternate[i])
2368 continue;
2369 if (mask & (1 << (i + 1)))
2370 continue;
2371 if (i < data->temp_fixed_num) {
2372 if (data->have_temp & (1 << i))
2373 continue;
2374 data->have_temp |= 1 << i;
2375 data->have_temp_fixed |= 1 << i;
2376 data->reg_temp[0][i] = reg_temp_alternate[i];
2377 data->reg_temp[1][i] = reg_temp_over[i];
2378 data->reg_temp[2][i] = reg_temp_hyst[i];
2379 data->temp_src[i] = i + 1;
2380 continue;
2381 }
2382
2383 if (s >= NUM_TEMP) /* Abort if no more space */
2384 break;
2385
2386 data->have_temp |= 1 << s;
2387 data->reg_temp[0][s] = reg_temp_alternate[i];
2388 data->temp_src[s] = i + 1;
2389 s++;
2390 }
2391#endif /* USE_ALTERNATE */
2392
2393 switch (data->kind) {
2394 case nct6775:
2395 break;
2396 case nct6776:
2397 /*
2398 * On NCT6776, AUXTIN and VIN3 pins are shared.
2399 * Only way to detect it is to check if AUXTIN is used
2400 * as a temperature source, and if that source is
2401 * enabled.
2402 *
2403 * If that is the case, disable in6, which reports VIN3.
2404 * Otherwise disable temp3.
2405 */
2406 if (data->have_temp & (1 << 2)) {
2407 u8 reg = nct6775_read_value(data,
2408 data->reg_temp_config[2]);
2409 if (reg & 0x01)
2410 data->have_temp &= ~(1 << 2);
2411 else
2412 data->have_in &= ~(1 << 6);
2413 }
2414 break;
2415 case nct6779:
2416 /*
2417 * Shared pins:
2418 * VIN4 / AUXTIN0
2419 * VIN5 / AUXTIN1
2420 * VIN6 / AUXTIN2
2421 * VIN7 / AUXTIN3
2422 *
2423 * There does not seem to be a clean way to detect if VINx or
2424 * AUXTINx is active, so for keep both sensor types enabled
2425 * for now.
2426 */
2427 break;
2428 }
Guenter Roeck9de2e2e2012-05-20 19:29:48 -07002429
2430 /* Initialize the chip */
2431 nct6775_init_device(data);
2432
2433 data->vrm = vid_which_vrm();
2434 err = superio_enter(sio_data->sioreg);
2435 if (err)
2436 return err;
2437
2438 /*
2439 * Read VID value
2440 * We can get the VID input values directly at logical device D 0xe3.
2441 */
2442 superio_select(sio_data->sioreg, NCT6775_LD_VID);
2443 data->vid = superio_inb(sio_data->sioreg, 0xe3);
Guenter Roeck47ece962012-12-04 07:59:32 -08002444
2445 if (fan_debounce) {
2446 u8 tmp;
2447
2448 superio_select(sio_data->sioreg, NCT6775_LD_HWM);
2449 tmp = superio_inb(sio_data->sioreg,
2450 NCT6775_REG_CR_FAN_DEBOUNCE);
2451 switch (data->kind) {
2452 case nct6775:
2453 tmp |= 0x1e;
2454 break;
2455 case nct6776:
2456 case nct6779:
2457 tmp |= 0x3e;
2458 break;
2459 }
2460 superio_outb(sio_data->sioreg, NCT6775_REG_CR_FAN_DEBOUNCE,
2461 tmp);
2462 dev_info(&pdev->dev, "Enabled fan debounce for chip %s\n",
2463 data->name);
2464 }
2465
Guenter Roeck9de2e2e2012-05-20 19:29:48 -07002466 superio_exit(sio_data->sioreg);
2467
2468 err = device_create_file(dev, &dev_attr_cpu0_vid);
2469 if (err)
2470 return err;
2471
Guenter Roeck1c65dc32012-12-04 07:56:24 -08002472 err = nct6775_check_fan_inputs(sio_data, data);
2473 if (err)
2474 goto exit_remove;
2475
2476 /* Read fan clock dividers immediately */
2477 nct6775_init_fan_common(dev, data);
2478
Guenter Roeck77eb5b32012-12-04 08:30:54 -08002479 /* Register sysfs hooks */
2480 for (i = 0; i < data->pwm_num; i++) {
2481 if (!(data->has_pwm & (1 << i)))
2482 continue;
2483
2484 err = sysfs_create_group(&dev->kobj, &nct6775_group_pwm[i]);
2485 if (err)
2486 goto exit_remove;
2487 }
2488
Guenter Roeck9de2e2e2012-05-20 19:29:48 -07002489 for (i = 0; i < data->in_num; i++) {
2490 if (!(data->have_in & (1 << i)))
2491 continue;
2492 err = sysfs_create_group(&dev->kobj, &nct6775_group_in[i]);
2493 if (err)
2494 goto exit_remove;
2495 }
2496
Guenter Roeck1c65dc32012-12-04 07:56:24 -08002497 for (i = 0; i < 5; i++) {
2498 if (data->has_fan & (1 << i)) {
2499 err = device_create_file(dev,
2500 &sda_fan_input[i].dev_attr);
2501 if (err)
2502 goto exit_remove;
2503 err = device_create_file(dev,
2504 &sda_fan_alarm[i].dev_attr);
2505 if (err)
2506 goto exit_remove;
2507 if (data->kind != nct6776 &&
2508 data->kind != nct6779) {
2509 err = device_create_file(dev,
2510 &sda_fan_div[i].dev_attr);
2511 if (err)
2512 goto exit_remove;
2513 }
2514 if (data->has_fan_min & (1 << i)) {
2515 err = device_create_file(dev,
2516 &sda_fan_min[i].dev_attr);
2517 if (err)
2518 goto exit_remove;
2519 }
Guenter Roeck5c25d952012-12-11 07:29:06 -08002520 err = device_create_file(dev,
2521 &sda_fan_pulses[i].dev_attr);
2522 if (err)
2523 goto exit_remove;
Guenter Roeck1c65dc32012-12-04 07:56:24 -08002524 }
2525 }
2526
Guenter Roeckaa136e52012-12-04 03:26:05 -08002527 for (i = 0; i < NUM_TEMP; i++) {
2528 if (!(data->have_temp & (1 << i)))
2529 continue;
2530 err = device_create_file(dev, &sda_temp_input[i].dev_attr);
2531 if (err)
2532 goto exit_remove;
2533 if (data->temp_label) {
2534 err = device_create_file(dev,
2535 &sda_temp_label[i].dev_attr);
2536 if (err)
2537 goto exit_remove;
2538 }
2539 if (data->reg_temp[1][i]) {
2540 err = device_create_file(dev,
2541 &sda_temp_max[i].dev_attr);
2542 if (err)
2543 goto exit_remove;
2544 }
2545 if (data->reg_temp[2][i]) {
2546 err = device_create_file(dev,
2547 &sda_temp_max_hyst[i].dev_attr);
2548 if (err)
2549 goto exit_remove;
2550 }
2551 if (data->reg_temp[3][i]) {
2552 err = device_create_file(dev,
2553 &sda_temp_crit[i].dev_attr);
2554 if (err)
2555 goto exit_remove;
2556 }
2557 if (!(data->have_temp_fixed & (1 << i)))
2558 continue;
2559 err = device_create_file(dev, &sda_temp_type[i].dev_attr);
2560 if (err)
2561 goto exit_remove;
2562 err = device_create_file(dev, &sda_temp_offset[i].dev_attr);
2563 if (err)
2564 goto exit_remove;
2565 if (i >= NUM_TEMP_ALARM ||
2566 data->ALARM_BITS[TEMP_ALARM_BASE + i] < 0)
2567 continue;
2568 err = device_create_file(dev, &sda_temp_alarm[i].dev_attr);
2569 if (err)
2570 goto exit_remove;
2571 }
2572
Guenter Roecka6bd5872012-12-04 03:13:34 -08002573 for (i = 0; i < ARRAY_SIZE(sda_caseopen); i++) {
2574 if (data->ALARM_BITS[INTRUSION_ALARM_BASE + i] < 0)
2575 continue;
2576 err = device_create_file(dev, &sda_caseopen[i].dev_attr);
2577 if (err)
2578 goto exit_remove;
2579 }
2580
Guenter Roeck9de2e2e2012-05-20 19:29:48 -07002581 err = device_create_file(dev, &dev_attr_name);
2582 if (err)
2583 goto exit_remove;
2584
2585 data->hwmon_dev = hwmon_device_register(dev);
2586 if (IS_ERR(data->hwmon_dev)) {
2587 err = PTR_ERR(data->hwmon_dev);
2588 goto exit_remove;
2589 }
2590
2591 return 0;
2592
2593exit_remove:
2594 nct6775_device_remove_files(dev);
2595 return err;
2596}
2597
2598static int nct6775_remove(struct platform_device *pdev)
2599{
2600 struct nct6775_data *data = platform_get_drvdata(pdev);
2601
2602 hwmon_device_unregister(data->hwmon_dev);
2603 nct6775_device_remove_files(&pdev->dev);
2604
2605 return 0;
2606}
2607
Guenter Roeck84d19d92012-12-04 08:01:39 -08002608#ifdef CONFIG_PM
2609static int nct6775_suspend(struct device *dev)
2610{
2611 struct nct6775_data *data = nct6775_update_device(dev);
2612 struct nct6775_sio_data *sio_data = dev->platform_data;
2613
2614 mutex_lock(&data->update_lock);
2615 data->vbat = nct6775_read_value(data, data->REG_VBAT);
2616 if (sio_data->kind == nct6775) {
2617 data->fandiv1 = nct6775_read_value(data, NCT6775_REG_FANDIV1);
2618 data->fandiv2 = nct6775_read_value(data, NCT6775_REG_FANDIV2);
2619 }
2620 mutex_unlock(&data->update_lock);
2621
2622 return 0;
2623}
2624
2625static int nct6775_resume(struct device *dev)
2626{
2627 struct nct6775_data *data = dev_get_drvdata(dev);
2628 struct nct6775_sio_data *sio_data = dev->platform_data;
2629 int i, j;
2630
2631 mutex_lock(&data->update_lock);
2632 data->bank = 0xff; /* Force initial bank selection */
2633
2634 /* Restore limits */
2635 for (i = 0; i < data->in_num; i++) {
2636 if (!(data->have_in & (1 << i)))
2637 continue;
2638
2639 nct6775_write_value(data, data->REG_IN_MINMAX[0][i],
2640 data->in[i][1]);
2641 nct6775_write_value(data, data->REG_IN_MINMAX[1][i],
2642 data->in[i][2]);
2643 }
2644
2645 for (i = 0; i < 5; i++) {
2646 if (!(data->has_fan_min & (1 << i)))
2647 continue;
2648
2649 nct6775_write_value(data, data->REG_FAN_MIN[i],
2650 data->fan_min[i]);
2651 }
2652
2653 for (i = 0; i < NUM_TEMP; i++) {
2654 if (!(data->have_temp & (1 << i)))
2655 continue;
2656
2657 for (j = 1; j < 4; j++)
2658 if (data->reg_temp[j][i])
2659 nct6775_write_temp(data, data->reg_temp[j][i],
2660 data->temp[j][i]);
2661 }
2662
2663 /* Restore other settings */
2664 nct6775_write_value(data, data->REG_VBAT, data->vbat);
2665 if (sio_data->kind == nct6775) {
2666 nct6775_write_value(data, NCT6775_REG_FANDIV1, data->fandiv1);
2667 nct6775_write_value(data, NCT6775_REG_FANDIV2, data->fandiv2);
2668 }
2669
2670 /* Force re-reading all values */
2671 data->valid = false;
2672 mutex_unlock(&data->update_lock);
2673
2674 return 0;
2675}
2676
2677static const struct dev_pm_ops nct6775_dev_pm_ops = {
2678 .suspend = nct6775_suspend,
2679 .resume = nct6775_resume,
2680};
2681
2682#define NCT6775_DEV_PM_OPS (&nct6775_dev_pm_ops)
2683#else
2684#define NCT6775_DEV_PM_OPS NULL
2685#endif /* CONFIG_PM */
2686
Guenter Roeck9de2e2e2012-05-20 19:29:48 -07002687static struct platform_driver nct6775_driver = {
2688 .driver = {
2689 .owner = THIS_MODULE,
2690 .name = DRVNAME,
Guenter Roeck84d19d92012-12-04 08:01:39 -08002691 .pm = NCT6775_DEV_PM_OPS,
Guenter Roeck9de2e2e2012-05-20 19:29:48 -07002692 },
2693 .probe = nct6775_probe,
2694 .remove = nct6775_remove,
2695};
2696
2697/* nct6775_find() looks for a '627 in the Super-I/O config space */
2698static int __init nct6775_find(int sioaddr, unsigned short *addr,
2699 struct nct6775_sio_data *sio_data)
2700{
2701 static const char sio_name_NCT6775[] __initconst = "NCT6775F";
2702 static const char sio_name_NCT6776[] __initconst = "NCT6776F";
2703 static const char sio_name_NCT6779[] __initconst = "NCT6779D";
2704
2705 u16 val;
2706 const char *sio_name;
2707 int err;
2708
2709 err = superio_enter(sioaddr);
2710 if (err)
2711 return err;
2712
2713 if (force_id)
2714 val = force_id;
2715 else
2716 val = (superio_inb(sioaddr, SIO_REG_DEVID) << 8)
2717 | superio_inb(sioaddr, SIO_REG_DEVID + 1);
2718 switch (val & SIO_ID_MASK) {
2719 case SIO_NCT6775_ID:
2720 sio_data->kind = nct6775;
2721 sio_name = sio_name_NCT6775;
2722 break;
2723 case SIO_NCT6776_ID:
2724 sio_data->kind = nct6776;
2725 sio_name = sio_name_NCT6776;
2726 break;
2727 case SIO_NCT6779_ID:
2728 sio_data->kind = nct6779;
2729 sio_name = sio_name_NCT6779;
2730 break;
2731 default:
2732 if (val != 0xffff)
2733 pr_debug("unsupported chip ID: 0x%04x\n", val);
2734 superio_exit(sioaddr);
2735 return -ENODEV;
2736 }
2737
2738 /* We have a known chip, find the HWM I/O address */
2739 superio_select(sioaddr, NCT6775_LD_HWM);
2740 val = (superio_inb(sioaddr, SIO_REG_ADDR) << 8)
2741 | superio_inb(sioaddr, SIO_REG_ADDR + 1);
2742 *addr = val & IOREGION_ALIGNMENT;
2743 if (*addr == 0) {
2744 pr_err("Refusing to enable a Super-I/O device with a base I/O port 0\n");
2745 superio_exit(sioaddr);
2746 return -ENODEV;
2747 }
2748
2749 /* Activate logical device if needed */
2750 val = superio_inb(sioaddr, SIO_REG_ENABLE);
2751 if (!(val & 0x01)) {
2752 pr_warn("Forcibly enabling Super-I/O. Sensor is probably unusable.\n");
2753 superio_outb(sioaddr, SIO_REG_ENABLE, val | 0x01);
2754 }
2755
2756 superio_exit(sioaddr);
2757 pr_info("Found %s chip at %#x\n", sio_name, *addr);
2758 sio_data->sioreg = sioaddr;
2759
2760 return 0;
2761}
2762
2763/*
2764 * when Super-I/O functions move to a separate file, the Super-I/O
2765 * bus will manage the lifetime of the device and this module will only keep
2766 * track of the nct6775 driver. But since we platform_device_alloc(), we
2767 * must keep track of the device
2768 */
2769static struct platform_device *pdev;
2770
2771static int __init sensors_nct6775_init(void)
2772{
2773 int err;
2774 unsigned short address;
2775 struct resource res;
2776 struct nct6775_sio_data sio_data;
2777
2778 /*
2779 * initialize sio_data->kind and sio_data->sioreg.
2780 *
2781 * when Super-I/O functions move to a separate file, the Super-I/O
2782 * driver will probe 0x2e and 0x4e and auto-detect the presence of a
2783 * nct6775 hardware monitor, and call probe()
2784 */
2785 if (nct6775_find(0x2e, &address, &sio_data) &&
2786 nct6775_find(0x4e, &address, &sio_data))
2787 return -ENODEV;
2788
2789 err = platform_driver_register(&nct6775_driver);
2790 if (err)
2791 goto exit;
2792
2793 pdev = platform_device_alloc(DRVNAME, address);
2794 if (!pdev) {
2795 err = -ENOMEM;
2796 pr_err("Device allocation failed\n");
2797 goto exit_unregister;
2798 }
2799
2800 err = platform_device_add_data(pdev, &sio_data,
2801 sizeof(struct nct6775_sio_data));
2802 if (err) {
2803 pr_err("Platform data allocation failed\n");
2804 goto exit_device_put;
2805 }
2806
2807 memset(&res, 0, sizeof(res));
2808 res.name = DRVNAME;
2809 res.start = address + IOREGION_OFFSET;
2810 res.end = address + IOREGION_OFFSET + IOREGION_LENGTH - 1;
2811 res.flags = IORESOURCE_IO;
2812
2813 err = acpi_check_resource_conflict(&res);
2814 if (err)
2815 goto exit_device_put;
2816
2817 err = platform_device_add_resources(pdev, &res, 1);
2818 if (err) {
2819 pr_err("Device resource addition failed (%d)\n", err);
2820 goto exit_device_put;
2821 }
2822
2823 /* platform_device_add calls probe() */
2824 err = platform_device_add(pdev);
2825 if (err) {
2826 pr_err("Device addition failed (%d)\n", err);
2827 goto exit_device_put;
2828 }
2829
2830 return 0;
2831
2832exit_device_put:
2833 platform_device_put(pdev);
2834exit_unregister:
2835 platform_driver_unregister(&nct6775_driver);
2836exit:
2837 return err;
2838}
2839
2840static void __exit sensors_nct6775_exit(void)
2841{
2842 platform_device_unregister(pdev);
2843 platform_driver_unregister(&nct6775_driver);
2844}
2845
2846MODULE_AUTHOR("Guenter Roeck <linux@roeck-us.net>");
2847MODULE_DESCRIPTION("NCT6775F/NCT6776F/NCT6779D driver");
2848MODULE_LICENSE("GPL");
2849
2850module_init(sensors_nct6775_init);
2851module_exit(sensors_nct6775_exit);