blob: 359bb1e23ac5f27cda0d3dba27e23c942c45cb8f [file] [log] [blame]
Jean Delvare08e7e272005-04-25 22:43:25 +02001/*
2 w83627ehf - Driver for the hardware monitoring functionality of
Guenter Roecke7e1ca62011-02-04 13:24:30 -08003 the Winbond W83627EHF Super-I/O chip
Jean Delvare08e7e272005-04-25 22:43:25 +02004 Copyright (C) 2005 Jean Delvare <khali@linux-fr.org>
Jean Delvare3379cee2006-09-24 21:25:52 +02005 Copyright (C) 2006 Yuan Mu (Winbond),
Guenter Roecke7e1ca62011-02-04 13:24:30 -08006 Rudolf Marek <r.marek@assembler.cz>
7 David Hubbard <david.c.hubbard@gmail.com>
Daniel J Blueman41e9a062009-12-14 18:01:37 -08008 Daniel J Blueman <daniel.blueman@gmail.com>
Guenter Roeckec3e5a12011-02-02 08:46:49 -08009 Copyright (C) 2010 Sheng-Yuan Huang (Nuvoton) (PS00)
Jean Delvare08e7e272005-04-25 22:43:25 +020010
11 Shamelessly ripped from the w83627hf driver
12 Copyright (C) 2003 Mark Studebaker
13
14 Thanks to Leon Moonen, Steve Cliffe and Grant Coady for their help
15 in testing and debugging this driver.
16
Jean Delvare8dd2d2c2005-07-27 21:33:15 +020017 This driver also supports the W83627EHG, which is the lead-free
18 version of the W83627EHF.
19
Jean Delvare08e7e272005-04-25 22:43:25 +020020 This program is free software; you can redistribute it and/or modify
21 it under the terms of the GNU General Public License as published by
22 the Free Software Foundation; either version 2 of the License, or
23 (at your option) any later version.
24
25 This program is distributed in the hope that it will be useful,
26 but WITHOUT ANY WARRANTY; without even the implied warranty of
27 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
28 GNU General Public License for more details.
29
30 You should have received a copy of the GNU General Public License
31 along with this program; if not, write to the Free Software
32 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
33
34
35 Supports the following chips:
36
David Hubbard657c93b2007-02-14 21:15:04 +010037 Chip #vin #fan #pwm #temp chip IDs man ID
38 w83627ehf 10 5 4 3 0x8850 0x88 0x5ca3
Guenter Roecke7e1ca62011-02-04 13:24:30 -080039 0x8860 0xa1
David Hubbard657c93b2007-02-14 21:15:04 +010040 w83627dhg 9 5 4 3 0xa020 0xc1 0x5ca3
Jean Delvarec1e48dc2009-06-15 18:39:50 +020041 w83627dhg-p 9 5 4 3 0xb070 0xc1 0x5ca3
Gong Jun237c8d22009-03-30 21:46:42 +020042 w83667hg 9 5 3 3 0xa510 0xc1 0x5ca3
Guenter Roeckd36cf322011-02-07 15:08:54 -080043 w83667hg-b 9 5 3 4 0xb350 0xc1 0x5ca3
Guenter Roeckec3e5a12011-02-02 08:46:49 -080044 nct6775f 9 4 3 9 0xb470 0xc1 0x5ca3
45 nct6776f 9 5 3 9 0xC330 0xc1 0x5ca3
Jean Delvare08e7e272005-04-25 22:43:25 +020046*/
47
Joe Perchesabdc6fd2010-10-20 06:51:54 +000048#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
49
Jean Delvare08e7e272005-04-25 22:43:25 +020050#include <linux/module.h>
51#include <linux/init.h>
52#include <linux/slab.h>
David Hubbard1ea6dd32007-06-24 11:16:15 +020053#include <linux/jiffies.h>
54#include <linux/platform_device.h>
Mark M. Hoffman943b0832005-07-15 21:39:18 -040055#include <linux/hwmon.h>
Yuan Mu412fec82006-02-05 23:24:16 +010056#include <linux/hwmon-sysfs.h>
Jean Delvarefc18d6c2007-06-24 11:19:42 +020057#include <linux/hwmon-vid.h>
Mark M. Hoffman943b0832005-07-15 21:39:18 -040058#include <linux/err.h>
Ingo Molnar9a61bf62006-01-18 23:19:26 +010059#include <linux/mutex.h>
Jean Delvareb9acb642009-01-07 16:37:35 +010060#include <linux/acpi.h>
H Hartley Sweeten6055fae2009-09-15 17:18:13 +020061#include <linux/io.h>
Jean Delvare08e7e272005-04-25 22:43:25 +020062#include "lm75.h"
63
Guenter Roeckec3e5a12011-02-02 08:46:49 -080064enum kinds { w83627ehf, w83627dhg, w83627dhg_p, w83667hg, w83667hg_b, nct6775,
65 nct6776 };
David Hubbard1ea6dd32007-06-24 11:16:15 +020066
67/* used to set data->name = w83627ehf_device_names[data->sio_kind] */
Guenter Roecke7e1ca62011-02-04 13:24:30 -080068static const char * const w83627ehf_device_names[] = {
David Hubbard1ea6dd32007-06-24 11:16:15 +020069 "w83627ehf",
70 "w83627dhg",
Jean Delvarec1e48dc2009-06-15 18:39:50 +020071 "w83627dhg",
Gong Jun237c8d22009-03-30 21:46:42 +020072 "w83667hg",
Guenter Roeckc39aeda2010-08-14 21:08:55 +020073 "w83667hg",
Guenter Roeckec3e5a12011-02-02 08:46:49 -080074 "nct6775",
75 "nct6776",
David Hubbard1ea6dd32007-06-24 11:16:15 +020076};
77
Jean Delvare67b671b2007-12-06 23:13:42 +010078static unsigned short force_id;
79module_param(force_id, ushort, 0);
80MODULE_PARM_DESC(force_id, "Override the detected device ID");
81
Ian Dobsond42e8692011-03-07 14:21:12 -080082static unsigned short fan_debounce;
83module_param(fan_debounce, ushort, 0);
84MODULE_PARM_DESC(fan_debounce, "Enable debouncing for fan RPM signal");
85
David Hubbard1ea6dd32007-06-24 11:16:15 +020086#define DRVNAME "w83627ehf"
Jean Delvare08e7e272005-04-25 22:43:25 +020087
88/*
89 * Super-I/O constants and functions
90 */
91
Jean Delvare08e7e272005-04-25 22:43:25 +020092#define W83627EHF_LD_HWM 0x0b
Guenter Roecke7e1ca62011-02-04 13:24:30 -080093#define W83667HG_LD_VID 0x0d
Jean Delvare08e7e272005-04-25 22:43:25 +020094
95#define SIO_REG_LDSEL 0x07 /* Logical device select */
96#define SIO_REG_DEVID 0x20 /* Device ID (2 bytes) */
Jean Delvarefc18d6c2007-06-24 11:19:42 +020097#define SIO_REG_EN_VRM10 0x2C /* GPIO3, GPIO4 selection */
Jean Delvare08e7e272005-04-25 22:43:25 +020098#define SIO_REG_ENABLE 0x30 /* Logical device enable */
99#define SIO_REG_ADDR 0x60 /* Logical device address (2 bytes) */
Jean Delvarefc18d6c2007-06-24 11:19:42 +0200100#define SIO_REG_VID_CTRL 0xF0 /* VID control */
101#define SIO_REG_VID_DATA 0xF1 /* VID data */
Jean Delvare08e7e272005-04-25 22:43:25 +0200102
David Hubbard657c93b2007-02-14 21:15:04 +0100103#define SIO_W83627EHF_ID 0x8850
104#define SIO_W83627EHG_ID 0x8860
105#define SIO_W83627DHG_ID 0xa020
Jean Delvarec1e48dc2009-06-15 18:39:50 +0200106#define SIO_W83627DHG_P_ID 0xb070
Guenter Roecke7e1ca62011-02-04 13:24:30 -0800107#define SIO_W83667HG_ID 0xa510
Guenter Roeckc39aeda2010-08-14 21:08:55 +0200108#define SIO_W83667HG_B_ID 0xb350
Guenter Roeckec3e5a12011-02-02 08:46:49 -0800109#define SIO_NCT6775_ID 0xb470
110#define SIO_NCT6776_ID 0xc330
David Hubbard657c93b2007-02-14 21:15:04 +0100111#define SIO_ID_MASK 0xFFF0
Jean Delvare08e7e272005-04-25 22:43:25 +0200112
113static inline void
David Hubbard1ea6dd32007-06-24 11:16:15 +0200114superio_outb(int ioreg, int reg, int val)
Jean Delvare08e7e272005-04-25 22:43:25 +0200115{
David Hubbard1ea6dd32007-06-24 11:16:15 +0200116 outb(reg, ioreg);
117 outb(val, ioreg + 1);
Jean Delvare08e7e272005-04-25 22:43:25 +0200118}
119
120static inline int
David Hubbard1ea6dd32007-06-24 11:16:15 +0200121superio_inb(int ioreg, int reg)
Jean Delvare08e7e272005-04-25 22:43:25 +0200122{
David Hubbard1ea6dd32007-06-24 11:16:15 +0200123 outb(reg, ioreg);
124 return inb(ioreg + 1);
Jean Delvare08e7e272005-04-25 22:43:25 +0200125}
126
127static inline void
David Hubbard1ea6dd32007-06-24 11:16:15 +0200128superio_select(int ioreg, int ld)
Jean Delvare08e7e272005-04-25 22:43:25 +0200129{
David Hubbard1ea6dd32007-06-24 11:16:15 +0200130 outb(SIO_REG_LDSEL, ioreg);
131 outb(ld, ioreg + 1);
Jean Delvare08e7e272005-04-25 22:43:25 +0200132}
133
134static inline void
David Hubbard1ea6dd32007-06-24 11:16:15 +0200135superio_enter(int ioreg)
Jean Delvare08e7e272005-04-25 22:43:25 +0200136{
David Hubbard1ea6dd32007-06-24 11:16:15 +0200137 outb(0x87, ioreg);
138 outb(0x87, ioreg);
Jean Delvare08e7e272005-04-25 22:43:25 +0200139}
140
141static inline void
David Hubbard1ea6dd32007-06-24 11:16:15 +0200142superio_exit(int ioreg)
Jean Delvare08e7e272005-04-25 22:43:25 +0200143{
Jonas Jonsson022b75a2010-09-17 17:24:13 +0200144 outb(0xaa, ioreg);
David Hubbard1ea6dd32007-06-24 11:16:15 +0200145 outb(0x02, ioreg);
146 outb(0x02, ioreg + 1);
Jean Delvare08e7e272005-04-25 22:43:25 +0200147}
148
149/*
150 * ISA constants
151 */
152
Guenter Roecke7e1ca62011-02-04 13:24:30 -0800153#define IOREGION_ALIGNMENT (~7)
Jean Delvare1a641fc2007-04-23 14:41:16 -0700154#define IOREGION_OFFSET 5
155#define IOREGION_LENGTH 2
David Hubbard1ea6dd32007-06-24 11:16:15 +0200156#define ADDR_REG_OFFSET 0
157#define DATA_REG_OFFSET 1
Jean Delvare08e7e272005-04-25 22:43:25 +0200158
159#define W83627EHF_REG_BANK 0x4E
160#define W83627EHF_REG_CONFIG 0x40
David Hubbard657c93b2007-02-14 21:15:04 +0100161
162/* Not currently used:
163 * REG_MAN_ID has the value 0x5ca3 for all supported chips.
164 * REG_CHIP_ID == 0x88/0xa1/0xc1 depending on chip model.
165 * REG_MAN_ID is at port 0x4f
166 * REG_CHIP_ID is at port 0x58 */
Jean Delvare08e7e272005-04-25 22:43:25 +0200167
168static const u16 W83627EHF_REG_FAN[] = { 0x28, 0x29, 0x2a, 0x3f, 0x553 };
169static const u16 W83627EHF_REG_FAN_MIN[] = { 0x3b, 0x3c, 0x3d, 0x3e, 0x55c };
170
Rudolf Marekcf0676f2006-03-23 16:25:22 +0100171/* The W83627EHF registers for nr=7,8,9 are in bank 5 */
172#define W83627EHF_REG_IN_MAX(nr) ((nr < 7) ? (0x2b + (nr) * 2) : \
173 (0x554 + (((nr) - 7) * 2)))
174#define W83627EHF_REG_IN_MIN(nr) ((nr < 7) ? (0x2c + (nr) * 2) : \
175 (0x555 + (((nr) - 7) * 2)))
176#define W83627EHF_REG_IN(nr) ((nr < 7) ? (0x20 + (nr)) : \
177 (0x550 + (nr) - 7))
178
Guenter Roeckd36cf322011-02-07 15:08:54 -0800179static const u16 W83627EHF_REG_TEMP[] = { 0x27, 0x150, 0x250, 0x7e };
180static const u16 W83627EHF_REG_TEMP_HYST[] = { 0x3a, 0x153, 0x253, 0 };
181static const u16 W83627EHF_REG_TEMP_OVER[] = { 0x39, 0x155, 0x255, 0 };
182static const u16 W83627EHF_REG_TEMP_CONFIG[] = { 0, 0x152, 0x252, 0 };
Jean Delvare08e7e272005-04-25 22:43:25 +0200183
184/* Fan clock dividers are spread over the following five registers */
185#define W83627EHF_REG_FANDIV1 0x47
186#define W83627EHF_REG_FANDIV2 0x4B
187#define W83627EHF_REG_VBAT 0x5D
188#define W83627EHF_REG_DIODE 0x59
189#define W83627EHF_REG_SMI_OVT 0x4C
190
Guenter Roeckec3e5a12011-02-02 08:46:49 -0800191/* NCT6775F has its own fan divider registers */
192#define NCT6775_REG_FANDIV1 0x506
193#define NCT6775_REG_FANDIV2 0x507
Ian Dobsond42e8692011-03-07 14:21:12 -0800194#define NCT6775_REG_FAN_DEBOUNCE 0xf0
Guenter Roeckec3e5a12011-02-02 08:46:49 -0800195
Jean Delvarea4589db2006-03-23 16:30:29 +0100196#define W83627EHF_REG_ALARM1 0x459
197#define W83627EHF_REG_ALARM2 0x45A
198#define W83627EHF_REG_ALARM3 0x45B
199
Rudolf Marek08c79952006-07-05 18:14:31 +0200200/* SmartFan registers */
Daniel J Blueman41e9a062009-12-14 18:01:37 -0800201#define W83627EHF_REG_FAN_STEPUP_TIME 0x0f
202#define W83627EHF_REG_FAN_STEPDOWN_TIME 0x0e
203
Rudolf Marek08c79952006-07-05 18:14:31 +0200204/* DC or PWM output fan configuration */
205static const u8 W83627EHF_REG_PWM_ENABLE[] = {
206 0x04, /* SYS FAN0 output mode and PWM mode */
207 0x04, /* CPU FAN0 output mode and PWM mode */
208 0x12, /* AUX FAN mode */
Daniel J Blueman41e9a062009-12-14 18:01:37 -0800209 0x62, /* CPU FAN1 mode */
Rudolf Marek08c79952006-07-05 18:14:31 +0200210};
211
212static const u8 W83627EHF_PWM_MODE_SHIFT[] = { 0, 1, 0, 6 };
213static const u8 W83627EHF_PWM_ENABLE_SHIFT[] = { 2, 4, 1, 4 };
214
215/* FAN Duty Cycle, be used to control */
Guenter Roeck279af1a2011-02-13 22:34:47 -0800216static const u16 W83627EHF_REG_PWM[] = { 0x01, 0x03, 0x11, 0x61 };
217static const u16 W83627EHF_REG_TARGET[] = { 0x05, 0x06, 0x13, 0x63 };
Rudolf Marek08c79952006-07-05 18:14:31 +0200218static const u8 W83627EHF_REG_TOLERANCE[] = { 0x07, 0x07, 0x14, 0x62 };
219
Rudolf Marek08c79952006-07-05 18:14:31 +0200220/* Advanced Fan control, some values are common for all fans */
Guenter Roeck279af1a2011-02-13 22:34:47 -0800221static const u16 W83627EHF_REG_FAN_START_OUTPUT[] = { 0x0a, 0x0b, 0x16, 0x65 };
222static const u16 W83627EHF_REG_FAN_STOP_OUTPUT[] = { 0x08, 0x09, 0x15, 0x64 };
223static const u16 W83627EHF_REG_FAN_STOP_TIME[] = { 0x0c, 0x0d, 0x17, 0x66 };
Guenter Roeckc39aeda2010-08-14 21:08:55 +0200224
Guenter Roeck279af1a2011-02-13 22:34:47 -0800225static const u16 W83627EHF_REG_FAN_MAX_OUTPUT_COMMON[]
Guenter Roeckc39aeda2010-08-14 21:08:55 +0200226 = { 0xff, 0x67, 0xff, 0x69 };
Guenter Roeck279af1a2011-02-13 22:34:47 -0800227static const u16 W83627EHF_REG_FAN_STEP_OUTPUT_COMMON[]
Guenter Roeckc39aeda2010-08-14 21:08:55 +0200228 = { 0xff, 0x68, 0xff, 0x6a };
229
Guenter Roeck279af1a2011-02-13 22:34:47 -0800230static const u16 W83627EHF_REG_FAN_MAX_OUTPUT_W83667_B[] = { 0x67, 0x69, 0x6b };
231static const u16 W83627EHF_REG_FAN_STEP_OUTPUT_W83667_B[]
232 = { 0x68, 0x6a, 0x6c };
Rudolf Marek08c79952006-07-05 18:14:31 +0200233
Guenter Roeckec3e5a12011-02-02 08:46:49 -0800234static const u16 NCT6775_REG_TARGET[] = { 0x101, 0x201, 0x301 };
235static const u16 NCT6775_REG_FAN_MODE[] = { 0x102, 0x202, 0x302 };
236static const u16 NCT6775_REG_FAN_STOP_OUTPUT[] = { 0x105, 0x205, 0x305 };
237static const u16 NCT6775_REG_FAN_START_OUTPUT[] = { 0x106, 0x206, 0x306 };
238static const u16 NCT6775_REG_FAN_STOP_TIME[] = { 0x107, 0x207, 0x307 };
239static const u16 NCT6775_REG_PWM[] = { 0x109, 0x209, 0x309 };
240static const u16 NCT6775_REG_FAN_MAX_OUTPUT[] = { 0x10a, 0x20a, 0x30a };
241static const u16 NCT6775_REG_FAN_STEP_OUTPUT[] = { 0x10b, 0x20b, 0x30b };
Guenter Roeck26bc4402011-02-11 08:00:58 -0800242static const u16 NCT6775_REG_FAN[] = { 0x630, 0x632, 0x634, 0x636, 0x638 };
Guenter Roeckec3e5a12011-02-02 08:46:49 -0800243static const u16 NCT6776_REG_FAN_MIN[] = { 0x63a, 0x63c, 0x63e, 0x640, 0x642};
244
245static const u16 NCT6775_REG_TEMP[]
246 = { 0x27, 0x150, 0x250, 0x73, 0x75, 0x77, 0x62b, 0x62c, 0x62d };
247static const u16 NCT6775_REG_TEMP_CONFIG[]
248 = { 0, 0x152, 0x252, 0, 0, 0, 0x628, 0x629, 0x62A };
249static const u16 NCT6775_REG_TEMP_HYST[]
250 = { 0x3a, 0x153, 0x253, 0, 0, 0, 0x673, 0x678, 0x67D };
251static const u16 NCT6775_REG_TEMP_OVER[]
252 = { 0x39, 0x155, 0x255, 0, 0, 0, 0x672, 0x677, 0x67C };
253static const u16 NCT6775_REG_TEMP_SOURCE[]
254 = { 0x621, 0x622, 0x623, 0x100, 0x200, 0x300, 0x624, 0x625, 0x626 };
255
Guenter Roeckd36cf322011-02-07 15:08:54 -0800256static const char *const w83667hg_b_temp_label[] = {
257 "SYSTIN",
258 "CPUTIN",
259 "AUXTIN",
260 "AMDTSI",
261 "PECI Agent 1",
262 "PECI Agent 2",
263 "PECI Agent 3",
264 "PECI Agent 4"
265};
266
Guenter Roeckec3e5a12011-02-02 08:46:49 -0800267static const char *const nct6775_temp_label[] = {
268 "",
269 "SYSTIN",
270 "CPUTIN",
271 "AUXTIN",
272 "AMD SB-TSI",
273 "PECI Agent 0",
274 "PECI Agent 1",
275 "PECI Agent 2",
276 "PECI Agent 3",
277 "PECI Agent 4",
278 "PECI Agent 5",
279 "PECI Agent 6",
280 "PECI Agent 7",
281 "PCH_CHIP_CPU_MAX_TEMP",
282 "PCH_CHIP_TEMP",
283 "PCH_CPU_TEMP",
284 "PCH_MCH_TEMP",
285 "PCH_DIM0_TEMP",
286 "PCH_DIM1_TEMP",
287 "PCH_DIM2_TEMP",
288 "PCH_DIM3_TEMP"
289};
290
291static const char *const nct6776_temp_label[] = {
292 "",
293 "SYSTIN",
294 "CPUTIN",
295 "AUXTIN",
296 "SMBUSMASTER 0",
297 "SMBUSMASTER 1",
298 "SMBUSMASTER 2",
299 "SMBUSMASTER 3",
300 "SMBUSMASTER 4",
301 "SMBUSMASTER 5",
302 "SMBUSMASTER 6",
303 "SMBUSMASTER 7",
304 "PECI Agent 0",
305 "PECI Agent 1",
306 "PCH_CHIP_CPU_MAX_TEMP",
307 "PCH_CHIP_TEMP",
308 "PCH_CPU_TEMP",
309 "PCH_MCH_TEMP",
310 "PCH_DIM0_TEMP",
311 "PCH_DIM1_TEMP",
312 "PCH_DIM2_TEMP",
313 "PCH_DIM3_TEMP",
314 "BYTE_TEMP"
315};
316
317#define NUM_REG_TEMP ARRAY_SIZE(NCT6775_REG_TEMP)
Guenter Roeckd36cf322011-02-07 15:08:54 -0800318
Guenter Roeckbce26c52011-02-04 12:54:14 -0800319static inline int is_word_sized(u16 reg)
320{
Guenter Roeckec3e5a12011-02-02 08:46:49 -0800321 return ((((reg & 0xff00) == 0x100
Guenter Roeckbce26c52011-02-04 12:54:14 -0800322 || (reg & 0xff00) == 0x200)
323 && ((reg & 0x00ff) == 0x50
324 || (reg & 0x00ff) == 0x53
Guenter Roeckec3e5a12011-02-02 08:46:49 -0800325 || (reg & 0x00ff) == 0x55))
326 || (reg & 0xfff0) == 0x630
327 || reg == 0x640 || reg == 0x642
328 || ((reg & 0xfff0) == 0x650
329 && (reg & 0x000f) >= 0x06)
330 || reg == 0x73 || reg == 0x75 || reg == 0x77
331 );
Guenter Roeckbce26c52011-02-04 12:54:14 -0800332}
333
Jean Delvare08e7e272005-04-25 22:43:25 +0200334/*
335 * Conversions
336 */
337
Rudolf Marek08c79952006-07-05 18:14:31 +0200338/* 1 is PWM mode, output in ms */
339static inline unsigned int step_time_from_reg(u8 reg, u8 mode)
340{
341 return mode ? 100 * reg : 400 * reg;
342}
343
344static inline u8 step_time_to_reg(unsigned int msec, u8 mode)
345{
346 return SENSORS_LIMIT((mode ? (msec + 50) / 100 :
347 (msec + 200) / 400), 1, 255);
348}
349
Guenter Roeck26bc4402011-02-11 08:00:58 -0800350static unsigned int fan_from_reg8(u16 reg, unsigned int divreg)
Jean Delvare08e7e272005-04-25 22:43:25 +0200351{
Guenter Roeck26bc4402011-02-11 08:00:58 -0800352 if (reg == 0 || reg == 255)
Jean Delvare08e7e272005-04-25 22:43:25 +0200353 return 0;
Guenter Roeck26bc4402011-02-11 08:00:58 -0800354 return 1350000U / (reg << divreg);
355}
356
357static unsigned int fan_from_reg13(u16 reg, unsigned int divreg)
358{
359 if ((reg & 0xff1f) == 0xff1f)
360 return 0;
361
362 reg = (reg & 0x1f) | ((reg & 0xff00) >> 3);
363
364 if (reg == 0)
365 return 0;
366
367 return 1350000U / reg;
368}
369
370static unsigned int fan_from_reg16(u16 reg, unsigned int divreg)
371{
372 if (reg == 0 || reg == 0xffff)
373 return 0;
374
375 /*
376 * Even though the registers are 16 bit wide, the fan divisor
377 * still applies.
378 */
379 return 1350000U / (reg << divreg);
Jean Delvare08e7e272005-04-25 22:43:25 +0200380}
381
382static inline unsigned int
383div_from_reg(u8 reg)
384{
385 return 1 << reg;
386}
387
388static inline int
Guenter Roeckbce26c52011-02-04 12:54:14 -0800389temp_from_reg(u16 reg, s16 regval)
Jean Delvare08e7e272005-04-25 22:43:25 +0200390{
Guenter Roeckbce26c52011-02-04 12:54:14 -0800391 if (is_word_sized(reg))
392 return LM75_TEMP_FROM_REG(regval);
Jean Delvare63e01012011-10-20 03:06:45 -0400393 return ((s8)regval) * 1000;
Jean Delvare08e7e272005-04-25 22:43:25 +0200394}
395
Guenter Roeckec3e5a12011-02-02 08:46:49 -0800396static inline u16
Guenter Roeckbce26c52011-02-04 12:54:14 -0800397temp_to_reg(u16 reg, long temp)
Jean Delvare08e7e272005-04-25 22:43:25 +0200398{
Guenter Roeckbce26c52011-02-04 12:54:14 -0800399 if (is_word_sized(reg))
400 return LM75_TEMP_TO_REG(temp);
Jean Delvare63e01012011-10-20 03:06:45 -0400401 return (s8)DIV_ROUND_CLOSEST(SENSORS_LIMIT(temp, -127000, 128000),
402 1000);
Jean Delvare08e7e272005-04-25 22:43:25 +0200403}
404
Rudolf Marekcf0676f2006-03-23 16:25:22 +0100405/* Some of analog inputs have internal scaling (2x), 8mV is ADC LSB */
406
407static u8 scale_in[10] = { 8, 8, 16, 16, 8, 8, 8, 16, 16, 8 };
408
409static inline long in_from_reg(u8 reg, u8 nr)
410{
411 return reg * scale_in[nr];
412}
413
414static inline u8 in_to_reg(u32 val, u8 nr)
415{
Guenter Roecke7e1ca62011-02-04 13:24:30 -0800416 return SENSORS_LIMIT(((val + (scale_in[nr] / 2)) / scale_in[nr]), 0,
417 255);
Rudolf Marekcf0676f2006-03-23 16:25:22 +0100418}
419
Jean Delvare08e7e272005-04-25 22:43:25 +0200420/*
421 * Data structures and manipulation thereof
422 */
423
424struct w83627ehf_data {
David Hubbard1ea6dd32007-06-24 11:16:15 +0200425 int addr; /* IO base of hw monitor block */
426 const char *name;
427
Tony Jones1beeffe2007-08-20 13:46:20 -0700428 struct device *hwmon_dev;
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100429 struct mutex lock;
Jean Delvare08e7e272005-04-25 22:43:25 +0200430
Guenter Roeckec3e5a12011-02-02 08:46:49 -0800431 u16 reg_temp[NUM_REG_TEMP];
432 u16 reg_temp_over[NUM_REG_TEMP];
433 u16 reg_temp_hyst[NUM_REG_TEMP];
434 u16 reg_temp_config[NUM_REG_TEMP];
Guenter Roeckd36cf322011-02-07 15:08:54 -0800435 u8 temp_src[NUM_REG_TEMP];
436 const char * const *temp_label;
437
Guenter Roeck279af1a2011-02-13 22:34:47 -0800438 const u16 *REG_PWM;
439 const u16 *REG_TARGET;
440 const u16 *REG_FAN;
441 const u16 *REG_FAN_MIN;
442 const u16 *REG_FAN_START_OUTPUT;
443 const u16 *REG_FAN_STOP_OUTPUT;
444 const u16 *REG_FAN_STOP_TIME;
445 const u16 *REG_FAN_MAX_OUTPUT;
446 const u16 *REG_FAN_STEP_OUTPUT;
Guenter Roeckda2e0252010-08-14 21:08:55 +0200447
Guenter Roeck26bc4402011-02-11 08:00:58 -0800448 unsigned int (*fan_from_reg)(u16 reg, unsigned int divreg);
449 unsigned int (*fan_from_reg_min)(u16 reg, unsigned int divreg);
450
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100451 struct mutex update_lock;
Jean Delvare08e7e272005-04-25 22:43:25 +0200452 char valid; /* !=0 if following fields are valid */
453 unsigned long last_updated; /* In jiffies */
454
455 /* Register values */
Guenter Roeck83cc8982011-02-06 08:10:15 -0800456 u8 bank; /* current register bank */
David Hubbard1ea6dd32007-06-24 11:16:15 +0200457 u8 in_num; /* number of in inputs we have */
Rudolf Marekcf0676f2006-03-23 16:25:22 +0100458 u8 in[10]; /* Register value */
459 u8 in_max[10]; /* Register value */
460 u8 in_min[10]; /* Register value */
Guenter Roeck3382a912011-02-13 13:08:23 -0800461 unsigned int rpm[5];
Guenter Roeckec3e5a12011-02-02 08:46:49 -0800462 u16 fan_min[5];
Jean Delvare08e7e272005-04-25 22:43:25 +0200463 u8 fan_div[5];
464 u8 has_fan; /* some fan inputs can be disabled */
Guenter Roeckec3e5a12011-02-02 08:46:49 -0800465 u8 has_fan_min; /* some fans don't have min register */
Guenter Roeck26bc4402011-02-11 08:00:58 -0800466 bool has_fan_div;
Jean Delvareda667362007-06-24 11:21:02 +0200467 u8 temp_type[3];
Guenter Roeckec3e5a12011-02-02 08:46:49 -0800468 s16 temp[9];
469 s16 temp_max[9];
470 s16 temp_max_hyst[9];
Jean Delvarea4589db2006-03-23 16:30:29 +0100471 u32 alarms;
Rudolf Marek08c79952006-07-05 18:14:31 +0200472
473 u8 pwm_mode[4]; /* 0->DC variable voltage, 1->PWM variable duty cycle */
474 u8 pwm_enable[4]; /* 1->manual
Daniel J Blueman41e9a062009-12-14 18:01:37 -0800475 2->thermal cruise mode (also called SmartFan I)
476 3->fan speed cruise mode
Guenter Roecke7e1ca62011-02-04 13:24:30 -0800477 4->variable thermal cruise (also called
Guenter Roeckb84bb512011-02-13 23:01:25 -0800478 SmartFan III)
479 5->enhanced variable thermal cruise (also called
480 SmartFan IV) */
481 u8 pwm_enable_orig[4]; /* original value of pwm_enable */
Gong Jun237c8d22009-03-30 21:46:42 +0200482 u8 pwm_num; /* number of pwm */
Rudolf Marek08c79952006-07-05 18:14:31 +0200483 u8 pwm[4];
484 u8 target_temp[4];
485 u8 tolerance[4];
486
Daniel J Blueman41e9a062009-12-14 18:01:37 -0800487 u8 fan_start_output[4]; /* minimum fan speed when spinning up */
488 u8 fan_stop_output[4]; /* minimum fan speed when spinning down */
489 u8 fan_stop_time[4]; /* time at minimum before disabling fan */
490 u8 fan_max_output[4]; /* maximum fan speed */
491 u8 fan_step_output[4]; /* rate of change output value */
Jean Delvarefc18d6c2007-06-24 11:19:42 +0200492
493 u8 vid;
494 u8 vrm;
Gong Juna157d062009-03-30 21:46:43 +0200495
Guenter Roeckec3e5a12011-02-02 08:46:49 -0800496 u16 have_temp;
Gong Juna157d062009-03-30 21:46:43 +0200497 u8 in6_skip;
Jean Delvare08e7e272005-04-25 22:43:25 +0200498};
499
David Hubbard1ea6dd32007-06-24 11:16:15 +0200500struct w83627ehf_sio_data {
501 int sioreg;
502 enum kinds kind;
503};
504
Guenter Roeck83cc8982011-02-06 08:10:15 -0800505/*
506 * On older chips, only registers 0x50-0x5f are banked.
507 * On more recent chips, all registers are banked.
508 * Assume that is the case and set the bank number for each access.
509 * Cache the bank number so it only needs to be set if it changes.
510 */
David Hubbard1ea6dd32007-06-24 11:16:15 +0200511static inline void w83627ehf_set_bank(struct w83627ehf_data *data, u16 reg)
Jean Delvare08e7e272005-04-25 22:43:25 +0200512{
Guenter Roeck83cc8982011-02-06 08:10:15 -0800513 u8 bank = reg >> 8;
514 if (data->bank != bank) {
David Hubbard1ea6dd32007-06-24 11:16:15 +0200515 outb_p(W83627EHF_REG_BANK, data->addr + ADDR_REG_OFFSET);
Guenter Roeck83cc8982011-02-06 08:10:15 -0800516 outb_p(bank, data->addr + DATA_REG_OFFSET);
517 data->bank = bank;
Jean Delvare08e7e272005-04-25 22:43:25 +0200518 }
519}
520
David Hubbard1ea6dd32007-06-24 11:16:15 +0200521static u16 w83627ehf_read_value(struct w83627ehf_data *data, u16 reg)
Jean Delvare08e7e272005-04-25 22:43:25 +0200522{
Jean Delvare08e7e272005-04-25 22:43:25 +0200523 int res, word_sized = is_word_sized(reg);
524
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100525 mutex_lock(&data->lock);
Jean Delvare08e7e272005-04-25 22:43:25 +0200526
David Hubbard1ea6dd32007-06-24 11:16:15 +0200527 w83627ehf_set_bank(data, reg);
528 outb_p(reg & 0xff, data->addr + ADDR_REG_OFFSET);
529 res = inb_p(data->addr + DATA_REG_OFFSET);
Jean Delvare08e7e272005-04-25 22:43:25 +0200530 if (word_sized) {
531 outb_p((reg & 0xff) + 1,
David Hubbard1ea6dd32007-06-24 11:16:15 +0200532 data->addr + ADDR_REG_OFFSET);
533 res = (res << 8) + inb_p(data->addr + DATA_REG_OFFSET);
Jean Delvare08e7e272005-04-25 22:43:25 +0200534 }
Jean Delvare08e7e272005-04-25 22:43:25 +0200535
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100536 mutex_unlock(&data->lock);
Jean Delvare08e7e272005-04-25 22:43:25 +0200537 return res;
538}
539
Guenter Roecke7e1ca62011-02-04 13:24:30 -0800540static int w83627ehf_write_value(struct w83627ehf_data *data, u16 reg,
541 u16 value)
Jean Delvare08e7e272005-04-25 22:43:25 +0200542{
Jean Delvare08e7e272005-04-25 22:43:25 +0200543 int word_sized = is_word_sized(reg);
544
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100545 mutex_lock(&data->lock);
Jean Delvare08e7e272005-04-25 22:43:25 +0200546
David Hubbard1ea6dd32007-06-24 11:16:15 +0200547 w83627ehf_set_bank(data, reg);
548 outb_p(reg & 0xff, data->addr + ADDR_REG_OFFSET);
Jean Delvare08e7e272005-04-25 22:43:25 +0200549 if (word_sized) {
David Hubbard1ea6dd32007-06-24 11:16:15 +0200550 outb_p(value >> 8, data->addr + DATA_REG_OFFSET);
Jean Delvare08e7e272005-04-25 22:43:25 +0200551 outb_p((reg & 0xff) + 1,
David Hubbard1ea6dd32007-06-24 11:16:15 +0200552 data->addr + ADDR_REG_OFFSET);
Jean Delvare08e7e272005-04-25 22:43:25 +0200553 }
David Hubbard1ea6dd32007-06-24 11:16:15 +0200554 outb_p(value & 0xff, data->addr + DATA_REG_OFFSET);
Jean Delvare08e7e272005-04-25 22:43:25 +0200555
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100556 mutex_unlock(&data->lock);
Jean Delvare08e7e272005-04-25 22:43:25 +0200557 return 0;
558}
559
560/* This function assumes that the caller holds data->update_lock */
Guenter Roeckec3e5a12011-02-02 08:46:49 -0800561static void nct6775_write_fan_div(struct w83627ehf_data *data, int nr)
562{
563 u8 reg;
564
565 switch (nr) {
566 case 0:
567 reg = (w83627ehf_read_value(data, NCT6775_REG_FANDIV1) & 0x70)
568 | (data->fan_div[0] & 0x7);
569 w83627ehf_write_value(data, NCT6775_REG_FANDIV1, reg);
570 break;
571 case 1:
572 reg = (w83627ehf_read_value(data, NCT6775_REG_FANDIV1) & 0x7)
573 | ((data->fan_div[1] << 4) & 0x70);
574 w83627ehf_write_value(data, NCT6775_REG_FANDIV1, reg);
575 case 2:
576 reg = (w83627ehf_read_value(data, NCT6775_REG_FANDIV2) & 0x70)
577 | (data->fan_div[2] & 0x7);
578 w83627ehf_write_value(data, NCT6775_REG_FANDIV2, reg);
579 break;
580 case 3:
581 reg = (w83627ehf_read_value(data, NCT6775_REG_FANDIV2) & 0x7)
582 | ((data->fan_div[3] << 4) & 0x70);
583 w83627ehf_write_value(data, NCT6775_REG_FANDIV2, reg);
584 break;
585 }
586}
587
588/* This function assumes that the caller holds data->update_lock */
David Hubbard1ea6dd32007-06-24 11:16:15 +0200589static void w83627ehf_write_fan_div(struct w83627ehf_data *data, int nr)
Jean Delvare08e7e272005-04-25 22:43:25 +0200590{
Jean Delvare08e7e272005-04-25 22:43:25 +0200591 u8 reg;
592
593 switch (nr) {
594 case 0:
David Hubbard1ea6dd32007-06-24 11:16:15 +0200595 reg = (w83627ehf_read_value(data, W83627EHF_REG_FANDIV1) & 0xcf)
Jean Delvare08e7e272005-04-25 22:43:25 +0200596 | ((data->fan_div[0] & 0x03) << 4);
Rudolf Marek14992c72006-10-08 22:02:09 +0200597 /* fan5 input control bit is write only, compute the value */
598 reg |= (data->has_fan & (1 << 4)) ? 1 : 0;
David Hubbard1ea6dd32007-06-24 11:16:15 +0200599 w83627ehf_write_value(data, W83627EHF_REG_FANDIV1, reg);
600 reg = (w83627ehf_read_value(data, W83627EHF_REG_VBAT) & 0xdf)
Jean Delvare08e7e272005-04-25 22:43:25 +0200601 | ((data->fan_div[0] & 0x04) << 3);
David Hubbard1ea6dd32007-06-24 11:16:15 +0200602 w83627ehf_write_value(data, W83627EHF_REG_VBAT, reg);
Jean Delvare08e7e272005-04-25 22:43:25 +0200603 break;
604 case 1:
David Hubbard1ea6dd32007-06-24 11:16:15 +0200605 reg = (w83627ehf_read_value(data, W83627EHF_REG_FANDIV1) & 0x3f)
Jean Delvare08e7e272005-04-25 22:43:25 +0200606 | ((data->fan_div[1] & 0x03) << 6);
Rudolf Marek14992c72006-10-08 22:02:09 +0200607 /* fan5 input control bit is write only, compute the value */
608 reg |= (data->has_fan & (1 << 4)) ? 1 : 0;
David Hubbard1ea6dd32007-06-24 11:16:15 +0200609 w83627ehf_write_value(data, W83627EHF_REG_FANDIV1, reg);
610 reg = (w83627ehf_read_value(data, W83627EHF_REG_VBAT) & 0xbf)
Jean Delvare08e7e272005-04-25 22:43:25 +0200611 | ((data->fan_div[1] & 0x04) << 4);
David Hubbard1ea6dd32007-06-24 11:16:15 +0200612 w83627ehf_write_value(data, W83627EHF_REG_VBAT, reg);
Jean Delvare08e7e272005-04-25 22:43:25 +0200613 break;
614 case 2:
David Hubbard1ea6dd32007-06-24 11:16:15 +0200615 reg = (w83627ehf_read_value(data, W83627EHF_REG_FANDIV2) & 0x3f)
Jean Delvare08e7e272005-04-25 22:43:25 +0200616 | ((data->fan_div[2] & 0x03) << 6);
David Hubbard1ea6dd32007-06-24 11:16:15 +0200617 w83627ehf_write_value(data, W83627EHF_REG_FANDIV2, reg);
618 reg = (w83627ehf_read_value(data, W83627EHF_REG_VBAT) & 0x7f)
Jean Delvare08e7e272005-04-25 22:43:25 +0200619 | ((data->fan_div[2] & 0x04) << 5);
David Hubbard1ea6dd32007-06-24 11:16:15 +0200620 w83627ehf_write_value(data, W83627EHF_REG_VBAT, reg);
Jean Delvare08e7e272005-04-25 22:43:25 +0200621 break;
622 case 3:
David Hubbard1ea6dd32007-06-24 11:16:15 +0200623 reg = (w83627ehf_read_value(data, W83627EHF_REG_DIODE) & 0xfc)
Jean Delvare08e7e272005-04-25 22:43:25 +0200624 | (data->fan_div[3] & 0x03);
David Hubbard1ea6dd32007-06-24 11:16:15 +0200625 w83627ehf_write_value(data, W83627EHF_REG_DIODE, reg);
626 reg = (w83627ehf_read_value(data, W83627EHF_REG_SMI_OVT) & 0x7f)
Jean Delvare08e7e272005-04-25 22:43:25 +0200627 | ((data->fan_div[3] & 0x04) << 5);
David Hubbard1ea6dd32007-06-24 11:16:15 +0200628 w83627ehf_write_value(data, W83627EHF_REG_SMI_OVT, reg);
Jean Delvare08e7e272005-04-25 22:43:25 +0200629 break;
630 case 4:
David Hubbard1ea6dd32007-06-24 11:16:15 +0200631 reg = (w83627ehf_read_value(data, W83627EHF_REG_DIODE) & 0x73)
Jean Delvare33725ad2007-04-17 00:32:27 -0700632 | ((data->fan_div[4] & 0x03) << 2)
Jean Delvare08e7e272005-04-25 22:43:25 +0200633 | ((data->fan_div[4] & 0x04) << 5);
David Hubbard1ea6dd32007-06-24 11:16:15 +0200634 w83627ehf_write_value(data, W83627EHF_REG_DIODE, reg);
Jean Delvare08e7e272005-04-25 22:43:25 +0200635 break;
636 }
637}
638
Guenter Roeckec3e5a12011-02-02 08:46:49 -0800639static void w83627ehf_write_fan_div_common(struct device *dev,
640 struct w83627ehf_data *data, int nr)
641{
642 struct w83627ehf_sio_data *sio_data = dev->platform_data;
643
644 if (sio_data->kind == nct6776)
645 ; /* no dividers, do nothing */
646 else if (sio_data->kind == nct6775)
647 nct6775_write_fan_div(data, nr);
648 else
649 w83627ehf_write_fan_div(data, nr);
650}
651
652static void nct6775_update_fan_div(struct w83627ehf_data *data)
653{
654 u8 i;
655
656 i = w83627ehf_read_value(data, NCT6775_REG_FANDIV1);
657 data->fan_div[0] = i & 0x7;
658 data->fan_div[1] = (i & 0x70) >> 4;
659 i = w83627ehf_read_value(data, NCT6775_REG_FANDIV2);
660 data->fan_div[2] = i & 0x7;
661 if (data->has_fan & (1<<3))
662 data->fan_div[3] = (i & 0x70) >> 4;
663}
664
Mark M. Hoffmanea7be662007-08-05 12:19:01 -0400665static void w83627ehf_update_fan_div(struct w83627ehf_data *data)
666{
667 int i;
668
669 i = w83627ehf_read_value(data, W83627EHF_REG_FANDIV1);
670 data->fan_div[0] = (i >> 4) & 0x03;
671 data->fan_div[1] = (i >> 6) & 0x03;
672 i = w83627ehf_read_value(data, W83627EHF_REG_FANDIV2);
673 data->fan_div[2] = (i >> 6) & 0x03;
674 i = w83627ehf_read_value(data, W83627EHF_REG_VBAT);
675 data->fan_div[0] |= (i >> 3) & 0x04;
676 data->fan_div[1] |= (i >> 4) & 0x04;
677 data->fan_div[2] |= (i >> 5) & 0x04;
678 if (data->has_fan & ((1 << 3) | (1 << 4))) {
679 i = w83627ehf_read_value(data, W83627EHF_REG_DIODE);
680 data->fan_div[3] = i & 0x03;
681 data->fan_div[4] = ((i >> 2) & 0x03)
682 | ((i >> 5) & 0x04);
683 }
684 if (data->has_fan & (1 << 3)) {
685 i = w83627ehf_read_value(data, W83627EHF_REG_SMI_OVT);
686 data->fan_div[3] |= (i >> 5) & 0x04;
687 }
688}
689
Guenter Roeckec3e5a12011-02-02 08:46:49 -0800690static void w83627ehf_update_fan_div_common(struct device *dev,
691 struct w83627ehf_data *data)
692{
693 struct w83627ehf_sio_data *sio_data = dev->platform_data;
694
695 if (sio_data->kind == nct6776)
696 ; /* no dividers, do nothing */
697 else if (sio_data->kind == nct6775)
698 nct6775_update_fan_div(data);
699 else
700 w83627ehf_update_fan_div(data);
701}
702
703static void nct6775_update_pwm(struct w83627ehf_data *data)
704{
705 int i;
706 int pwmcfg, fanmodecfg;
707
708 for (i = 0; i < data->pwm_num; i++) {
709 pwmcfg = w83627ehf_read_value(data,
710 W83627EHF_REG_PWM_ENABLE[i]);
711 fanmodecfg = w83627ehf_read_value(data,
712 NCT6775_REG_FAN_MODE[i]);
713 data->pwm_mode[i] =
714 ((pwmcfg >> W83627EHF_PWM_MODE_SHIFT[i]) & 1) ? 0 : 1;
715 data->pwm_enable[i] = ((fanmodecfg >> 4) & 7) + 1;
716 data->tolerance[i] = fanmodecfg & 0x0f;
717 data->pwm[i] = w83627ehf_read_value(data, data->REG_PWM[i]);
718 }
719}
720
721static void w83627ehf_update_pwm(struct w83627ehf_data *data)
722{
723 int i;
724 int pwmcfg = 0, tolerance = 0; /* shut up the compiler */
725
726 for (i = 0; i < data->pwm_num; i++) {
727 if (!(data->has_fan & (1 << i)))
728 continue;
729
730 /* pwmcfg, tolerance mapped for i=0, i=1 to same reg */
731 if (i != 1) {
732 pwmcfg = w83627ehf_read_value(data,
733 W83627EHF_REG_PWM_ENABLE[i]);
734 tolerance = w83627ehf_read_value(data,
735 W83627EHF_REG_TOLERANCE[i]);
736 }
737 data->pwm_mode[i] =
738 ((pwmcfg >> W83627EHF_PWM_MODE_SHIFT[i]) & 1) ? 0 : 1;
739 data->pwm_enable[i] = ((pwmcfg >> W83627EHF_PWM_ENABLE_SHIFT[i])
740 & 3) + 1;
741 data->pwm[i] = w83627ehf_read_value(data, data->REG_PWM[i]);
742
743 data->tolerance[i] = (tolerance >> (i == 1 ? 4 : 0)) & 0x0f;
744 }
745}
746
747static void w83627ehf_update_pwm_common(struct device *dev,
748 struct w83627ehf_data *data)
749{
750 struct w83627ehf_sio_data *sio_data = dev->platform_data;
751
752 if (sio_data->kind == nct6775 || sio_data->kind == nct6776)
753 nct6775_update_pwm(data);
754 else
755 w83627ehf_update_pwm(data);
756}
757
Jean Delvare08e7e272005-04-25 22:43:25 +0200758static struct w83627ehf_data *w83627ehf_update_device(struct device *dev)
759{
David Hubbard1ea6dd32007-06-24 11:16:15 +0200760 struct w83627ehf_data *data = dev_get_drvdata(dev);
Guenter Roeckec3e5a12011-02-02 08:46:49 -0800761 struct w83627ehf_sio_data *sio_data = dev->platform_data;
762
Jean Delvare08e7e272005-04-25 22:43:25 +0200763 int i;
764
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100765 mutex_lock(&data->update_lock);
Jean Delvare08e7e272005-04-25 22:43:25 +0200766
Jean Delvare6b3e4642007-06-24 11:19:01 +0200767 if (time_after(jiffies, data->last_updated + HZ + HZ/2)
Jean Delvare08e7e272005-04-25 22:43:25 +0200768 || !data->valid) {
769 /* Fan clock dividers */
Guenter Roeckec3e5a12011-02-02 08:46:49 -0800770 w83627ehf_update_fan_div_common(dev, data);
Jean Delvare08e7e272005-04-25 22:43:25 +0200771
Rudolf Marekcf0676f2006-03-23 16:25:22 +0100772 /* Measured voltages and limits */
David Hubbard1ea6dd32007-06-24 11:16:15 +0200773 for (i = 0; i < data->in_num; i++) {
774 data->in[i] = w83627ehf_read_value(data,
Rudolf Marekcf0676f2006-03-23 16:25:22 +0100775 W83627EHF_REG_IN(i));
David Hubbard1ea6dd32007-06-24 11:16:15 +0200776 data->in_min[i] = w83627ehf_read_value(data,
Rudolf Marekcf0676f2006-03-23 16:25:22 +0100777 W83627EHF_REG_IN_MIN(i));
David Hubbard1ea6dd32007-06-24 11:16:15 +0200778 data->in_max[i] = w83627ehf_read_value(data,
Rudolf Marekcf0676f2006-03-23 16:25:22 +0100779 W83627EHF_REG_IN_MAX(i));
780 }
781
Jean Delvare08e7e272005-04-25 22:43:25 +0200782 /* Measured fan speeds and limits */
783 for (i = 0; i < 5; i++) {
Guenter Roeck3382a912011-02-13 13:08:23 -0800784 u16 reg;
785
Jean Delvare08e7e272005-04-25 22:43:25 +0200786 if (!(data->has_fan & (1 << i)))
787 continue;
788
Guenter Roeck3382a912011-02-13 13:08:23 -0800789 reg = w83627ehf_read_value(data, data->REG_FAN[i]);
790 data->rpm[i] = data->fan_from_reg(reg,
791 data->fan_div[i]);
Guenter Roeckec3e5a12011-02-02 08:46:49 -0800792
793 if (data->has_fan_min & (1 << i))
794 data->fan_min[i] = w83627ehf_read_value(data,
Guenter Roeck279af1a2011-02-13 22:34:47 -0800795 data->REG_FAN_MIN[i]);
Jean Delvare08e7e272005-04-25 22:43:25 +0200796
797 /* If we failed to measure the fan speed and clock
798 divider can be increased, let's try that for next
799 time */
Guenter Roeck26bc4402011-02-11 08:00:58 -0800800 if (data->has_fan_div
Guenter Roeck3382a912011-02-13 13:08:23 -0800801 && (reg >= 0xff || (sio_data->kind == nct6775
802 && reg == 0x00))
Guenter Roeckec3e5a12011-02-02 08:46:49 -0800803 && data->fan_div[i] < 0x07) {
Guenter Roecke7e1ca62011-02-04 13:24:30 -0800804 dev_dbg(dev, "Increasing fan%d "
Jean Delvare08e7e272005-04-25 22:43:25 +0200805 "clock divider from %u to %u\n",
Jean Delvare33725ad2007-04-17 00:32:27 -0700806 i + 1, div_from_reg(data->fan_div[i]),
Jean Delvare08e7e272005-04-25 22:43:25 +0200807 div_from_reg(data->fan_div[i] + 1));
808 data->fan_div[i]++;
Guenter Roeckec3e5a12011-02-02 08:46:49 -0800809 w83627ehf_write_fan_div_common(dev, data, i);
Jean Delvare08e7e272005-04-25 22:43:25 +0200810 /* Preserve min limit if possible */
Guenter Roeckec3e5a12011-02-02 08:46:49 -0800811 if ((data->has_fan_min & (1 << i))
812 && data->fan_min[i] >= 2
Jean Delvare08e7e272005-04-25 22:43:25 +0200813 && data->fan_min[i] != 255)
David Hubbard1ea6dd32007-06-24 11:16:15 +0200814 w83627ehf_write_value(data,
Guenter Roeck279af1a2011-02-13 22:34:47 -0800815 data->REG_FAN_MIN[i],
Jean Delvare08e7e272005-04-25 22:43:25 +0200816 (data->fan_min[i] /= 2));
817 }
818 }
819
Guenter Roeckec3e5a12011-02-02 08:46:49 -0800820 w83627ehf_update_pwm_common(dev, data);
821
Guenter Roeckda2e0252010-08-14 21:08:55 +0200822 for (i = 0; i < data->pwm_num; i++) {
823 if (!(data->has_fan & (1 << i)))
824 continue;
825
Guenter Roeckec3e5a12011-02-02 08:46:49 -0800826 data->fan_start_output[i] =
827 w83627ehf_read_value(data,
828 data->REG_FAN_START_OUTPUT[i]);
829 data->fan_stop_output[i] =
830 w83627ehf_read_value(data,
831 data->REG_FAN_STOP_OUTPUT[i]);
832 data->fan_stop_time[i] =
833 w83627ehf_read_value(data,
834 data->REG_FAN_STOP_TIME[i]);
Guenter Roeckda2e0252010-08-14 21:08:55 +0200835
Guenter Roeckec3e5a12011-02-02 08:46:49 -0800836 if (data->REG_FAN_MAX_OUTPUT &&
837 data->REG_FAN_MAX_OUTPUT[i] != 0xff)
Guenter Roeckda2e0252010-08-14 21:08:55 +0200838 data->fan_max_output[i] =
839 w83627ehf_read_value(data,
Guenter Roeckec3e5a12011-02-02 08:46:49 -0800840 data->REG_FAN_MAX_OUTPUT[i]);
Guenter Roeckda2e0252010-08-14 21:08:55 +0200841
Guenter Roeckec3e5a12011-02-02 08:46:49 -0800842 if (data->REG_FAN_STEP_OUTPUT &&
843 data->REG_FAN_STEP_OUTPUT[i] != 0xff)
Guenter Roeckda2e0252010-08-14 21:08:55 +0200844 data->fan_step_output[i] =
845 w83627ehf_read_value(data,
Guenter Roeckec3e5a12011-02-02 08:46:49 -0800846 data->REG_FAN_STEP_OUTPUT[i]);
Guenter Roeckda2e0252010-08-14 21:08:55 +0200847
Rudolf Marek08c79952006-07-05 18:14:31 +0200848 data->target_temp[i] =
David Hubbard1ea6dd32007-06-24 11:16:15 +0200849 w83627ehf_read_value(data,
Guenter Roeck279af1a2011-02-13 22:34:47 -0800850 data->REG_TARGET[i]) &
Rudolf Marek08c79952006-07-05 18:14:31 +0200851 (data->pwm_mode[i] == 1 ? 0x7f : 0xff);
Rudolf Marek08c79952006-07-05 18:14:31 +0200852 }
853
Jean Delvare08e7e272005-04-25 22:43:25 +0200854 /* Measured temperatures and limits */
Guenter Roeckd36cf322011-02-07 15:08:54 -0800855 for (i = 0; i < NUM_REG_TEMP; i++) {
856 if (!(data->have_temp & (1 << i)))
857 continue;
Guenter Roeckec3e5a12011-02-02 08:46:49 -0800858 data->temp[i] = w83627ehf_read_value(data,
859 data->reg_temp[i]);
860 if (data->reg_temp_over[i])
861 data->temp_max[i]
862 = w83627ehf_read_value(data,
863 data->reg_temp_over[i]);
864 if (data->reg_temp_hyst[i])
865 data->temp_max_hyst[i]
866 = w83627ehf_read_value(data,
867 data->reg_temp_hyst[i]);
Jean Delvare08e7e272005-04-25 22:43:25 +0200868 }
869
David Hubbard1ea6dd32007-06-24 11:16:15 +0200870 data->alarms = w83627ehf_read_value(data,
Jean Delvarea4589db2006-03-23 16:30:29 +0100871 W83627EHF_REG_ALARM1) |
David Hubbard1ea6dd32007-06-24 11:16:15 +0200872 (w83627ehf_read_value(data,
Jean Delvarea4589db2006-03-23 16:30:29 +0100873 W83627EHF_REG_ALARM2) << 8) |
David Hubbard1ea6dd32007-06-24 11:16:15 +0200874 (w83627ehf_read_value(data,
Jean Delvarea4589db2006-03-23 16:30:29 +0100875 W83627EHF_REG_ALARM3) << 16);
876
Jean Delvare08e7e272005-04-25 22:43:25 +0200877 data->last_updated = jiffies;
878 data->valid = 1;
879 }
880
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100881 mutex_unlock(&data->update_lock);
Jean Delvare08e7e272005-04-25 22:43:25 +0200882 return data;
883}
884
885/*
886 * Sysfs callback functions
887 */
Rudolf Marekcf0676f2006-03-23 16:25:22 +0100888#define show_in_reg(reg) \
889static ssize_t \
890show_##reg(struct device *dev, struct device_attribute *attr, \
891 char *buf) \
892{ \
893 struct w83627ehf_data *data = w83627ehf_update_device(dev); \
Guenter Roecke7e1ca62011-02-04 13:24:30 -0800894 struct sensor_device_attribute *sensor_attr = \
895 to_sensor_dev_attr(attr); \
Rudolf Marekcf0676f2006-03-23 16:25:22 +0100896 int nr = sensor_attr->index; \
897 return sprintf(buf, "%ld\n", in_from_reg(data->reg[nr], nr)); \
898}
899show_in_reg(in)
900show_in_reg(in_min)
901show_in_reg(in_max)
902
903#define store_in_reg(REG, reg) \
904static ssize_t \
Guenter Roecke7e1ca62011-02-04 13:24:30 -0800905store_in_##reg(struct device *dev, struct device_attribute *attr, \
906 const char *buf, size_t count) \
Rudolf Marekcf0676f2006-03-23 16:25:22 +0100907{ \
David Hubbard1ea6dd32007-06-24 11:16:15 +0200908 struct w83627ehf_data *data = dev_get_drvdata(dev); \
Guenter Roecke7e1ca62011-02-04 13:24:30 -0800909 struct sensor_device_attribute *sensor_attr = \
910 to_sensor_dev_attr(attr); \
Rudolf Marekcf0676f2006-03-23 16:25:22 +0100911 int nr = sensor_attr->index; \
Guenter Roeckbce26c52011-02-04 12:54:14 -0800912 unsigned long val; \
913 int err; \
914 err = strict_strtoul(buf, 10, &val); \
915 if (err < 0) \
916 return err; \
Rudolf Marekcf0676f2006-03-23 16:25:22 +0100917 mutex_lock(&data->update_lock); \
918 data->in_##reg[nr] = in_to_reg(val, nr); \
David Hubbard1ea6dd32007-06-24 11:16:15 +0200919 w83627ehf_write_value(data, W83627EHF_REG_IN_##REG(nr), \
Rudolf Marekcf0676f2006-03-23 16:25:22 +0100920 data->in_##reg[nr]); \
921 mutex_unlock(&data->update_lock); \
922 return count; \
923}
924
925store_in_reg(MIN, min)
926store_in_reg(MAX, max)
927
Guenter Roecke7e1ca62011-02-04 13:24:30 -0800928static ssize_t show_alarm(struct device *dev, struct device_attribute *attr,
929 char *buf)
Jean Delvarea4589db2006-03-23 16:30:29 +0100930{
931 struct w83627ehf_data *data = w83627ehf_update_device(dev);
932 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
933 int nr = sensor_attr->index;
934 return sprintf(buf, "%u\n", (data->alarms >> nr) & 0x01);
935}
936
Rudolf Marekcf0676f2006-03-23 16:25:22 +0100937static struct sensor_device_attribute sda_in_input[] = {
938 SENSOR_ATTR(in0_input, S_IRUGO, show_in, NULL, 0),
939 SENSOR_ATTR(in1_input, S_IRUGO, show_in, NULL, 1),
940 SENSOR_ATTR(in2_input, S_IRUGO, show_in, NULL, 2),
941 SENSOR_ATTR(in3_input, S_IRUGO, show_in, NULL, 3),
942 SENSOR_ATTR(in4_input, S_IRUGO, show_in, NULL, 4),
943 SENSOR_ATTR(in5_input, S_IRUGO, show_in, NULL, 5),
944 SENSOR_ATTR(in6_input, S_IRUGO, show_in, NULL, 6),
945 SENSOR_ATTR(in7_input, S_IRUGO, show_in, NULL, 7),
946 SENSOR_ATTR(in8_input, S_IRUGO, show_in, NULL, 8),
947 SENSOR_ATTR(in9_input, S_IRUGO, show_in, NULL, 9),
948};
949
Jean Delvarea4589db2006-03-23 16:30:29 +0100950static struct sensor_device_attribute sda_in_alarm[] = {
951 SENSOR_ATTR(in0_alarm, S_IRUGO, show_alarm, NULL, 0),
952 SENSOR_ATTR(in1_alarm, S_IRUGO, show_alarm, NULL, 1),
953 SENSOR_ATTR(in2_alarm, S_IRUGO, show_alarm, NULL, 2),
954 SENSOR_ATTR(in3_alarm, S_IRUGO, show_alarm, NULL, 3),
955 SENSOR_ATTR(in4_alarm, S_IRUGO, show_alarm, NULL, 8),
956 SENSOR_ATTR(in5_alarm, S_IRUGO, show_alarm, NULL, 21),
957 SENSOR_ATTR(in6_alarm, S_IRUGO, show_alarm, NULL, 20),
958 SENSOR_ATTR(in7_alarm, S_IRUGO, show_alarm, NULL, 16),
959 SENSOR_ATTR(in8_alarm, S_IRUGO, show_alarm, NULL, 17),
960 SENSOR_ATTR(in9_alarm, S_IRUGO, show_alarm, NULL, 19),
961};
962
Rudolf Marekcf0676f2006-03-23 16:25:22 +0100963static struct sensor_device_attribute sda_in_min[] = {
Guenter Roecke7e1ca62011-02-04 13:24:30 -0800964 SENSOR_ATTR(in0_min, S_IWUSR | S_IRUGO, show_in_min, store_in_min, 0),
965 SENSOR_ATTR(in1_min, S_IWUSR | S_IRUGO, show_in_min, store_in_min, 1),
966 SENSOR_ATTR(in2_min, S_IWUSR | S_IRUGO, show_in_min, store_in_min, 2),
967 SENSOR_ATTR(in3_min, S_IWUSR | S_IRUGO, show_in_min, store_in_min, 3),
968 SENSOR_ATTR(in4_min, S_IWUSR | S_IRUGO, show_in_min, store_in_min, 4),
969 SENSOR_ATTR(in5_min, S_IWUSR | S_IRUGO, show_in_min, store_in_min, 5),
970 SENSOR_ATTR(in6_min, S_IWUSR | S_IRUGO, show_in_min, store_in_min, 6),
971 SENSOR_ATTR(in7_min, S_IWUSR | S_IRUGO, show_in_min, store_in_min, 7),
972 SENSOR_ATTR(in8_min, S_IWUSR | S_IRUGO, show_in_min, store_in_min, 8),
973 SENSOR_ATTR(in9_min, S_IWUSR | S_IRUGO, show_in_min, store_in_min, 9),
Rudolf Marekcf0676f2006-03-23 16:25:22 +0100974};
975
976static struct sensor_device_attribute sda_in_max[] = {
Guenter Roecke7e1ca62011-02-04 13:24:30 -0800977 SENSOR_ATTR(in0_max, S_IWUSR | S_IRUGO, show_in_max, store_in_max, 0),
978 SENSOR_ATTR(in1_max, S_IWUSR | S_IRUGO, show_in_max, store_in_max, 1),
979 SENSOR_ATTR(in2_max, S_IWUSR | S_IRUGO, show_in_max, store_in_max, 2),
980 SENSOR_ATTR(in3_max, S_IWUSR | S_IRUGO, show_in_max, store_in_max, 3),
981 SENSOR_ATTR(in4_max, S_IWUSR | S_IRUGO, show_in_max, store_in_max, 4),
982 SENSOR_ATTR(in5_max, S_IWUSR | S_IRUGO, show_in_max, store_in_max, 5),
983 SENSOR_ATTR(in6_max, S_IWUSR | S_IRUGO, show_in_max, store_in_max, 6),
984 SENSOR_ATTR(in7_max, S_IWUSR | S_IRUGO, show_in_max, store_in_max, 7),
985 SENSOR_ATTR(in8_max, S_IWUSR | S_IRUGO, show_in_max, store_in_max, 8),
986 SENSOR_ATTR(in9_max, S_IWUSR | S_IRUGO, show_in_max, store_in_max, 9),
Rudolf Marekcf0676f2006-03-23 16:25:22 +0100987};
988
Guenter Roeckec3e5a12011-02-02 08:46:49 -0800989static ssize_t
990show_fan(struct device *dev, struct device_attribute *attr, char *buf)
991{
992 struct w83627ehf_data *data = w83627ehf_update_device(dev);
993 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
994 int nr = sensor_attr->index;
Guenter Roeck3382a912011-02-13 13:08:23 -0800995 return sprintf(buf, "%d\n", data->rpm[nr]);
Jean Delvare08e7e272005-04-25 22:43:25 +0200996}
Guenter Roeckec3e5a12011-02-02 08:46:49 -0800997
998static ssize_t
999show_fan_min(struct device *dev, struct device_attribute *attr, char *buf)
1000{
1001 struct w83627ehf_data *data = w83627ehf_update_device(dev);
1002 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
1003 int nr = sensor_attr->index;
1004 return sprintf(buf, "%d\n",
Guenter Roeck26bc4402011-02-11 08:00:58 -08001005 data->fan_from_reg_min(data->fan_min[nr],
1006 data->fan_div[nr]));
Guenter Roeckec3e5a12011-02-02 08:46:49 -08001007}
Jean Delvare08e7e272005-04-25 22:43:25 +02001008
1009static ssize_t
Yuan Mu412fec82006-02-05 23:24:16 +01001010show_fan_div(struct device *dev, struct device_attribute *attr,
1011 char *buf)
Jean Delvare08e7e272005-04-25 22:43:25 +02001012{
1013 struct w83627ehf_data *data = w83627ehf_update_device(dev);
Yuan Mu412fec82006-02-05 23:24:16 +01001014 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
1015 int nr = sensor_attr->index;
1016 return sprintf(buf, "%u\n", div_from_reg(data->fan_div[nr]));
Jean Delvare08e7e272005-04-25 22:43:25 +02001017}
1018
1019static ssize_t
Yuan Mu412fec82006-02-05 23:24:16 +01001020store_fan_min(struct device *dev, struct device_attribute *attr,
1021 const char *buf, size_t count)
Jean Delvare08e7e272005-04-25 22:43:25 +02001022{
David Hubbard1ea6dd32007-06-24 11:16:15 +02001023 struct w83627ehf_data *data = dev_get_drvdata(dev);
Yuan Mu412fec82006-02-05 23:24:16 +01001024 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
1025 int nr = sensor_attr->index;
Guenter Roeckbce26c52011-02-04 12:54:14 -08001026 unsigned long val;
1027 int err;
Jean Delvare08e7e272005-04-25 22:43:25 +02001028 unsigned int reg;
1029 u8 new_div;
1030
Guenter Roeckbce26c52011-02-04 12:54:14 -08001031 err = strict_strtoul(buf, 10, &val);
1032 if (err < 0)
1033 return err;
1034
Ingo Molnar9a61bf62006-01-18 23:19:26 +01001035 mutex_lock(&data->update_lock);
Guenter Roeck26bc4402011-02-11 08:00:58 -08001036 if (!data->has_fan_div) {
1037 /*
1038 * Only NCT6776F for now, so we know that this is a 13 bit
1039 * register
1040 */
Guenter Roeckec3e5a12011-02-02 08:46:49 -08001041 if (!val) {
1042 val = 0xff1f;
1043 } else {
1044 if (val > 1350000U)
1045 val = 135000U;
1046 val = 1350000U / val;
1047 val = (val & 0x1f) | ((val << 3) & 0xff00);
1048 }
1049 data->fan_min[nr] = val;
1050 goto done; /* Leave fan divider alone */
1051 }
Jean Delvare08e7e272005-04-25 22:43:25 +02001052 if (!val) {
1053 /* No min limit, alarm disabled */
1054 data->fan_min[nr] = 255;
1055 new_div = data->fan_div[nr]; /* No change */
1056 dev_info(dev, "fan%u low limit and alarm disabled\n", nr + 1);
1057 } else if ((reg = 1350000U / val) >= 128 * 255) {
1058 /* Speed below this value cannot possibly be represented,
1059 even with the highest divider (128) */
1060 data->fan_min[nr] = 254;
1061 new_div = 7; /* 128 == (1 << 7) */
Guenter Roeckbce26c52011-02-04 12:54:14 -08001062 dev_warn(dev, "fan%u low limit %lu below minimum %u, set to "
Guenter Roeckec3e5a12011-02-02 08:46:49 -08001063 "minimum\n", nr + 1, val,
Guenter Roeck26bc4402011-02-11 08:00:58 -08001064 data->fan_from_reg_min(254, 7));
Jean Delvare08e7e272005-04-25 22:43:25 +02001065 } else if (!reg) {
1066 /* Speed above this value cannot possibly be represented,
1067 even with the lowest divider (1) */
1068 data->fan_min[nr] = 1;
1069 new_div = 0; /* 1 == (1 << 0) */
Guenter Roeckbce26c52011-02-04 12:54:14 -08001070 dev_warn(dev, "fan%u low limit %lu above maximum %u, set to "
Guenter Roeckec3e5a12011-02-02 08:46:49 -08001071 "maximum\n", nr + 1, val,
Guenter Roeck26bc4402011-02-11 08:00:58 -08001072 data->fan_from_reg_min(1, 0));
Jean Delvare08e7e272005-04-25 22:43:25 +02001073 } else {
1074 /* Automatically pick the best divider, i.e. the one such
1075 that the min limit will correspond to a register value
1076 in the 96..192 range */
1077 new_div = 0;
1078 while (reg > 192 && new_div < 7) {
1079 reg >>= 1;
1080 new_div++;
1081 }
1082 data->fan_min[nr] = reg;
1083 }
1084
1085 /* Write both the fan clock divider (if it changed) and the new
1086 fan min (unconditionally) */
1087 if (new_div != data->fan_div[nr]) {
Jean Delvare08e7e272005-04-25 22:43:25 +02001088 dev_dbg(dev, "fan%u clock divider changed from %u to %u\n",
1089 nr + 1, div_from_reg(data->fan_div[nr]),
1090 div_from_reg(new_div));
1091 data->fan_div[nr] = new_div;
Guenter Roeckec3e5a12011-02-02 08:46:49 -08001092 w83627ehf_write_fan_div_common(dev, data, nr);
Jean Delvare6b3e4642007-06-24 11:19:01 +02001093 /* Give the chip time to sample a new speed value */
1094 data->last_updated = jiffies;
Jean Delvare08e7e272005-04-25 22:43:25 +02001095 }
Guenter Roeckec3e5a12011-02-02 08:46:49 -08001096done:
Guenter Roeck279af1a2011-02-13 22:34:47 -08001097 w83627ehf_write_value(data, data->REG_FAN_MIN[nr],
Jean Delvare08e7e272005-04-25 22:43:25 +02001098 data->fan_min[nr]);
Ingo Molnar9a61bf62006-01-18 23:19:26 +01001099 mutex_unlock(&data->update_lock);
Jean Delvare08e7e272005-04-25 22:43:25 +02001100
1101 return count;
1102}
1103
Yuan Mu412fec82006-02-05 23:24:16 +01001104static struct sensor_device_attribute sda_fan_input[] = {
1105 SENSOR_ATTR(fan1_input, S_IRUGO, show_fan, NULL, 0),
1106 SENSOR_ATTR(fan2_input, S_IRUGO, show_fan, NULL, 1),
1107 SENSOR_ATTR(fan3_input, S_IRUGO, show_fan, NULL, 2),
1108 SENSOR_ATTR(fan4_input, S_IRUGO, show_fan, NULL, 3),
1109 SENSOR_ATTR(fan5_input, S_IRUGO, show_fan, NULL, 4),
1110};
Jean Delvare08e7e272005-04-25 22:43:25 +02001111
Jean Delvarea4589db2006-03-23 16:30:29 +01001112static struct sensor_device_attribute sda_fan_alarm[] = {
1113 SENSOR_ATTR(fan1_alarm, S_IRUGO, show_alarm, NULL, 6),
1114 SENSOR_ATTR(fan2_alarm, S_IRUGO, show_alarm, NULL, 7),
1115 SENSOR_ATTR(fan3_alarm, S_IRUGO, show_alarm, NULL, 11),
1116 SENSOR_ATTR(fan4_alarm, S_IRUGO, show_alarm, NULL, 10),
1117 SENSOR_ATTR(fan5_alarm, S_IRUGO, show_alarm, NULL, 23),
1118};
1119
Yuan Mu412fec82006-02-05 23:24:16 +01001120static struct sensor_device_attribute sda_fan_min[] = {
1121 SENSOR_ATTR(fan1_min, S_IWUSR | S_IRUGO, show_fan_min,
1122 store_fan_min, 0),
1123 SENSOR_ATTR(fan2_min, S_IWUSR | S_IRUGO, show_fan_min,
1124 store_fan_min, 1),
1125 SENSOR_ATTR(fan3_min, S_IWUSR | S_IRUGO, show_fan_min,
1126 store_fan_min, 2),
1127 SENSOR_ATTR(fan4_min, S_IWUSR | S_IRUGO, show_fan_min,
1128 store_fan_min, 3),
1129 SENSOR_ATTR(fan5_min, S_IWUSR | S_IRUGO, show_fan_min,
1130 store_fan_min, 4),
1131};
Jean Delvare08e7e272005-04-25 22:43:25 +02001132
Yuan Mu412fec82006-02-05 23:24:16 +01001133static struct sensor_device_attribute sda_fan_div[] = {
1134 SENSOR_ATTR(fan1_div, S_IRUGO, show_fan_div, NULL, 0),
1135 SENSOR_ATTR(fan2_div, S_IRUGO, show_fan_div, NULL, 1),
1136 SENSOR_ATTR(fan3_div, S_IRUGO, show_fan_div, NULL, 2),
1137 SENSOR_ATTR(fan4_div, S_IRUGO, show_fan_div, NULL, 3),
1138 SENSOR_ATTR(fan5_div, S_IRUGO, show_fan_div, NULL, 4),
1139};
Jean Delvare08e7e272005-04-25 22:43:25 +02001140
Guenter Roeckd36cf322011-02-07 15:08:54 -08001141static ssize_t
1142show_temp_label(struct device *dev, struct device_attribute *attr, char *buf)
1143{
1144 struct w83627ehf_data *data = w83627ehf_update_device(dev);
1145 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
1146 int nr = sensor_attr->index;
1147 return sprintf(buf, "%s\n", data->temp_label[data->temp_src[nr]]);
1148}
1149
Guenter Roeckec3e5a12011-02-02 08:46:49 -08001150#define show_temp_reg(addr, reg) \
Jean Delvare08e7e272005-04-25 22:43:25 +02001151static ssize_t \
Yuan Mu412fec82006-02-05 23:24:16 +01001152show_##reg(struct device *dev, struct device_attribute *attr, \
1153 char *buf) \
Jean Delvare08e7e272005-04-25 22:43:25 +02001154{ \
1155 struct w83627ehf_data *data = w83627ehf_update_device(dev); \
Guenter Roecke7e1ca62011-02-04 13:24:30 -08001156 struct sensor_device_attribute *sensor_attr = \
1157 to_sensor_dev_attr(attr); \
Yuan Mu412fec82006-02-05 23:24:16 +01001158 int nr = sensor_attr->index; \
Jean Delvare08e7e272005-04-25 22:43:25 +02001159 return sprintf(buf, "%d\n", \
Guenter Roeckec3e5a12011-02-02 08:46:49 -08001160 temp_from_reg(data->addr[nr], data->reg[nr])); \
Jean Delvare08e7e272005-04-25 22:43:25 +02001161}
Guenter Roeckec3e5a12011-02-02 08:46:49 -08001162show_temp_reg(reg_temp, temp);
1163show_temp_reg(reg_temp_over, temp_max);
1164show_temp_reg(reg_temp_hyst, temp_max_hyst);
Jean Delvare08e7e272005-04-25 22:43:25 +02001165
Guenter Roeckec3e5a12011-02-02 08:46:49 -08001166#define store_temp_reg(addr, reg) \
Jean Delvare08e7e272005-04-25 22:43:25 +02001167static ssize_t \
Yuan Mu412fec82006-02-05 23:24:16 +01001168store_##reg(struct device *dev, struct device_attribute *attr, \
1169 const char *buf, size_t count) \
Jean Delvare08e7e272005-04-25 22:43:25 +02001170{ \
David Hubbard1ea6dd32007-06-24 11:16:15 +02001171 struct w83627ehf_data *data = dev_get_drvdata(dev); \
Guenter Roecke7e1ca62011-02-04 13:24:30 -08001172 struct sensor_device_attribute *sensor_attr = \
1173 to_sensor_dev_attr(attr); \
Yuan Mu412fec82006-02-05 23:24:16 +01001174 int nr = sensor_attr->index; \
Guenter Roeckbce26c52011-02-04 12:54:14 -08001175 int err; \
1176 long val; \
1177 err = strict_strtol(buf, 10, &val); \
1178 if (err < 0) \
1179 return err; \
Ingo Molnar9a61bf62006-01-18 23:19:26 +01001180 mutex_lock(&data->update_lock); \
Guenter Roeckec3e5a12011-02-02 08:46:49 -08001181 data->reg[nr] = temp_to_reg(data->addr[nr], val); \
1182 w83627ehf_write_value(data, data->addr[nr], \
Jean Delvare08e7e272005-04-25 22:43:25 +02001183 data->reg[nr]); \
Ingo Molnar9a61bf62006-01-18 23:19:26 +01001184 mutex_unlock(&data->update_lock); \
Jean Delvare08e7e272005-04-25 22:43:25 +02001185 return count; \
1186}
Guenter Roeckec3e5a12011-02-02 08:46:49 -08001187store_temp_reg(reg_temp_over, temp_max);
1188store_temp_reg(reg_temp_hyst, temp_max_hyst);
Jean Delvare08e7e272005-04-25 22:43:25 +02001189
Jean Delvareda667362007-06-24 11:21:02 +02001190static ssize_t
1191show_temp_type(struct device *dev, struct device_attribute *attr, char *buf)
1192{
1193 struct w83627ehf_data *data = w83627ehf_update_device(dev);
1194 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
1195 int nr = sensor_attr->index;
1196 return sprintf(buf, "%d\n", (int)data->temp_type[nr]);
1197}
1198
Gong Juna157d062009-03-30 21:46:43 +02001199static struct sensor_device_attribute sda_temp_input[] = {
Guenter Roeckbce26c52011-02-04 12:54:14 -08001200 SENSOR_ATTR(temp1_input, S_IRUGO, show_temp, NULL, 0),
1201 SENSOR_ATTR(temp2_input, S_IRUGO, show_temp, NULL, 1),
1202 SENSOR_ATTR(temp3_input, S_IRUGO, show_temp, NULL, 2),
Guenter Roeckd36cf322011-02-07 15:08:54 -08001203 SENSOR_ATTR(temp4_input, S_IRUGO, show_temp, NULL, 3),
Guenter Roeckec3e5a12011-02-02 08:46:49 -08001204 SENSOR_ATTR(temp5_input, S_IRUGO, show_temp, NULL, 4),
1205 SENSOR_ATTR(temp6_input, S_IRUGO, show_temp, NULL, 5),
1206 SENSOR_ATTR(temp7_input, S_IRUGO, show_temp, NULL, 6),
1207 SENSOR_ATTR(temp8_input, S_IRUGO, show_temp, NULL, 7),
1208 SENSOR_ATTR(temp9_input, S_IRUGO, show_temp, NULL, 8),
Guenter Roeckd36cf322011-02-07 15:08:54 -08001209};
1210
1211static struct sensor_device_attribute sda_temp_label[] = {
1212 SENSOR_ATTR(temp1_label, S_IRUGO, show_temp_label, NULL, 0),
1213 SENSOR_ATTR(temp2_label, S_IRUGO, show_temp_label, NULL, 1),
1214 SENSOR_ATTR(temp3_label, S_IRUGO, show_temp_label, NULL, 2),
1215 SENSOR_ATTR(temp4_label, S_IRUGO, show_temp_label, NULL, 3),
Guenter Roeckec3e5a12011-02-02 08:46:49 -08001216 SENSOR_ATTR(temp5_label, S_IRUGO, show_temp_label, NULL, 4),
1217 SENSOR_ATTR(temp6_label, S_IRUGO, show_temp_label, NULL, 5),
1218 SENSOR_ATTR(temp7_label, S_IRUGO, show_temp_label, NULL, 6),
1219 SENSOR_ATTR(temp8_label, S_IRUGO, show_temp_label, NULL, 7),
1220 SENSOR_ATTR(temp9_label, S_IRUGO, show_temp_label, NULL, 8),
Gong Juna157d062009-03-30 21:46:43 +02001221};
1222
1223static struct sensor_device_attribute sda_temp_max[] = {
Guenter Roeckbce26c52011-02-04 12:54:14 -08001224 SENSOR_ATTR(temp1_max, S_IRUGO | S_IWUSR, show_temp_max,
Yuan Mu412fec82006-02-05 23:24:16 +01001225 store_temp_max, 0),
Guenter Roeckbce26c52011-02-04 12:54:14 -08001226 SENSOR_ATTR(temp2_max, S_IRUGO | S_IWUSR, show_temp_max,
Yuan Mu412fec82006-02-05 23:24:16 +01001227 store_temp_max, 1),
Guenter Roeckbce26c52011-02-04 12:54:14 -08001228 SENSOR_ATTR(temp3_max, S_IRUGO | S_IWUSR, show_temp_max,
1229 store_temp_max, 2),
Guenter Roeckec3e5a12011-02-02 08:46:49 -08001230 SENSOR_ATTR(temp4_max, S_IRUGO | S_IWUSR, show_temp_max,
1231 store_temp_max, 3),
1232 SENSOR_ATTR(temp5_max, S_IRUGO | S_IWUSR, show_temp_max,
1233 store_temp_max, 4),
1234 SENSOR_ATTR(temp6_max, S_IRUGO | S_IWUSR, show_temp_max,
1235 store_temp_max, 5),
1236 SENSOR_ATTR(temp7_max, S_IRUGO | S_IWUSR, show_temp_max,
1237 store_temp_max, 6),
1238 SENSOR_ATTR(temp8_max, S_IRUGO | S_IWUSR, show_temp_max,
1239 store_temp_max, 7),
1240 SENSOR_ATTR(temp9_max, S_IRUGO | S_IWUSR, show_temp_max,
1241 store_temp_max, 8),
Gong Juna157d062009-03-30 21:46:43 +02001242};
1243
1244static struct sensor_device_attribute sda_temp_max_hyst[] = {
Guenter Roeckbce26c52011-02-04 12:54:14 -08001245 SENSOR_ATTR(temp1_max_hyst, S_IRUGO | S_IWUSR, show_temp_max_hyst,
Yuan Mu412fec82006-02-05 23:24:16 +01001246 store_temp_max_hyst, 0),
Guenter Roeckbce26c52011-02-04 12:54:14 -08001247 SENSOR_ATTR(temp2_max_hyst, S_IRUGO | S_IWUSR, show_temp_max_hyst,
Yuan Mu412fec82006-02-05 23:24:16 +01001248 store_temp_max_hyst, 1),
Guenter Roeckbce26c52011-02-04 12:54:14 -08001249 SENSOR_ATTR(temp3_max_hyst, S_IRUGO | S_IWUSR, show_temp_max_hyst,
1250 store_temp_max_hyst, 2),
Guenter Roeckec3e5a12011-02-02 08:46:49 -08001251 SENSOR_ATTR(temp4_max_hyst, S_IRUGO | S_IWUSR, show_temp_max_hyst,
1252 store_temp_max_hyst, 3),
1253 SENSOR_ATTR(temp5_max_hyst, S_IRUGO | S_IWUSR, show_temp_max_hyst,
1254 store_temp_max_hyst, 4),
1255 SENSOR_ATTR(temp6_max_hyst, S_IRUGO | S_IWUSR, show_temp_max_hyst,
1256 store_temp_max_hyst, 5),
1257 SENSOR_ATTR(temp7_max_hyst, S_IRUGO | S_IWUSR, show_temp_max_hyst,
1258 store_temp_max_hyst, 6),
1259 SENSOR_ATTR(temp8_max_hyst, S_IRUGO | S_IWUSR, show_temp_max_hyst,
1260 store_temp_max_hyst, 7),
1261 SENSOR_ATTR(temp9_max_hyst, S_IRUGO | S_IWUSR, show_temp_max_hyst,
1262 store_temp_max_hyst, 8),
Gong Juna157d062009-03-30 21:46:43 +02001263};
1264
1265static struct sensor_device_attribute sda_temp_alarm[] = {
Jean Delvarea4589db2006-03-23 16:30:29 +01001266 SENSOR_ATTR(temp1_alarm, S_IRUGO, show_alarm, NULL, 4),
1267 SENSOR_ATTR(temp2_alarm, S_IRUGO, show_alarm, NULL, 5),
1268 SENSOR_ATTR(temp3_alarm, S_IRUGO, show_alarm, NULL, 13),
Gong Juna157d062009-03-30 21:46:43 +02001269};
1270
1271static struct sensor_device_attribute sda_temp_type[] = {
Jean Delvareda667362007-06-24 11:21:02 +02001272 SENSOR_ATTR(temp1_type, S_IRUGO, show_temp_type, NULL, 0),
1273 SENSOR_ATTR(temp2_type, S_IRUGO, show_temp_type, NULL, 1),
1274 SENSOR_ATTR(temp3_type, S_IRUGO, show_temp_type, NULL, 2),
Yuan Mu412fec82006-02-05 23:24:16 +01001275};
Jean Delvare08e7e272005-04-25 22:43:25 +02001276
Rudolf Marek08c79952006-07-05 18:14:31 +02001277#define show_pwm_reg(reg) \
Guenter Roecke7e1ca62011-02-04 13:24:30 -08001278static ssize_t show_##reg(struct device *dev, struct device_attribute *attr, \
1279 char *buf) \
Rudolf Marek08c79952006-07-05 18:14:31 +02001280{ \
1281 struct w83627ehf_data *data = w83627ehf_update_device(dev); \
Guenter Roecke7e1ca62011-02-04 13:24:30 -08001282 struct sensor_device_attribute *sensor_attr = \
1283 to_sensor_dev_attr(attr); \
Rudolf Marek08c79952006-07-05 18:14:31 +02001284 int nr = sensor_attr->index; \
1285 return sprintf(buf, "%d\n", data->reg[nr]); \
1286}
1287
1288show_pwm_reg(pwm_mode)
1289show_pwm_reg(pwm_enable)
1290show_pwm_reg(pwm)
1291
1292static ssize_t
1293store_pwm_mode(struct device *dev, struct device_attribute *attr,
1294 const char *buf, size_t count)
1295{
David Hubbard1ea6dd32007-06-24 11:16:15 +02001296 struct w83627ehf_data *data = dev_get_drvdata(dev);
Rudolf Marek08c79952006-07-05 18:14:31 +02001297 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
Guenter Roeck70c3e9e2012-01-27 17:56:06 -08001298 struct w83627ehf_sio_data *sio_data = dev->platform_data;
Rudolf Marek08c79952006-07-05 18:14:31 +02001299 int nr = sensor_attr->index;
Guenter Roeckbce26c52011-02-04 12:54:14 -08001300 unsigned long val;
1301 int err;
Rudolf Marek08c79952006-07-05 18:14:31 +02001302 u16 reg;
1303
Guenter Roeckbce26c52011-02-04 12:54:14 -08001304 err = strict_strtoul(buf, 10, &val);
1305 if (err < 0)
1306 return err;
1307
Rudolf Marek08c79952006-07-05 18:14:31 +02001308 if (val > 1)
1309 return -EINVAL;
Guenter Roeck70c3e9e2012-01-27 17:56:06 -08001310
1311 /* On NCT67766F, DC mode is only supported for pwm1 */
1312 if (sio_data->kind == nct6776 && nr && val != 1)
1313 return -EINVAL;
1314
Rudolf Marek08c79952006-07-05 18:14:31 +02001315 mutex_lock(&data->update_lock);
David Hubbard1ea6dd32007-06-24 11:16:15 +02001316 reg = w83627ehf_read_value(data, W83627EHF_REG_PWM_ENABLE[nr]);
Rudolf Marek08c79952006-07-05 18:14:31 +02001317 data->pwm_mode[nr] = val;
1318 reg &= ~(1 << W83627EHF_PWM_MODE_SHIFT[nr]);
1319 if (!val)
1320 reg |= 1 << W83627EHF_PWM_MODE_SHIFT[nr];
David Hubbard1ea6dd32007-06-24 11:16:15 +02001321 w83627ehf_write_value(data, W83627EHF_REG_PWM_ENABLE[nr], reg);
Rudolf Marek08c79952006-07-05 18:14:31 +02001322 mutex_unlock(&data->update_lock);
1323 return count;
1324}
1325
1326static ssize_t
1327store_pwm(struct device *dev, struct device_attribute *attr,
1328 const char *buf, size_t count)
1329{
David Hubbard1ea6dd32007-06-24 11:16:15 +02001330 struct w83627ehf_data *data = dev_get_drvdata(dev);
Rudolf Marek08c79952006-07-05 18:14:31 +02001331 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
1332 int nr = sensor_attr->index;
Guenter Roeckbce26c52011-02-04 12:54:14 -08001333 unsigned long val;
1334 int err;
1335
1336 err = strict_strtoul(buf, 10, &val);
1337 if (err < 0)
1338 return err;
1339
1340 val = SENSORS_LIMIT(val, 0, 255);
Rudolf Marek08c79952006-07-05 18:14:31 +02001341
1342 mutex_lock(&data->update_lock);
1343 data->pwm[nr] = val;
Guenter Roeck279af1a2011-02-13 22:34:47 -08001344 w83627ehf_write_value(data, data->REG_PWM[nr], val);
Rudolf Marek08c79952006-07-05 18:14:31 +02001345 mutex_unlock(&data->update_lock);
1346 return count;
1347}
1348
1349static ssize_t
1350store_pwm_enable(struct device *dev, struct device_attribute *attr,
1351 const char *buf, size_t count)
1352{
David Hubbard1ea6dd32007-06-24 11:16:15 +02001353 struct w83627ehf_data *data = dev_get_drvdata(dev);
Guenter Roeckec3e5a12011-02-02 08:46:49 -08001354 struct w83627ehf_sio_data *sio_data = dev->platform_data;
Rudolf Marek08c79952006-07-05 18:14:31 +02001355 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
1356 int nr = sensor_attr->index;
Guenter Roeckbce26c52011-02-04 12:54:14 -08001357 unsigned long val;
1358 int err;
Rudolf Marek08c79952006-07-05 18:14:31 +02001359 u16 reg;
1360
Guenter Roeckbce26c52011-02-04 12:54:14 -08001361 err = strict_strtoul(buf, 10, &val);
1362 if (err < 0)
1363 return err;
1364
Guenter Roeckb84bb512011-02-13 23:01:25 -08001365 if (!val || (val > 4 && val != data->pwm_enable_orig[nr]))
Rudolf Marek08c79952006-07-05 18:14:31 +02001366 return -EINVAL;
Guenter Roeckec3e5a12011-02-02 08:46:49 -08001367 /* SmartFan III mode is not supported on NCT6776F */
1368 if (sio_data->kind == nct6776 && val == 4)
1369 return -EINVAL;
1370
Rudolf Marek08c79952006-07-05 18:14:31 +02001371 mutex_lock(&data->update_lock);
Rudolf Marek08c79952006-07-05 18:14:31 +02001372 data->pwm_enable[nr] = val;
Guenter Roeckec3e5a12011-02-02 08:46:49 -08001373 if (sio_data->kind == nct6775 || sio_data->kind == nct6776) {
1374 reg = w83627ehf_read_value(data,
1375 NCT6775_REG_FAN_MODE[nr]);
1376 reg &= 0x0f;
1377 reg |= (val - 1) << 4;
1378 w83627ehf_write_value(data,
1379 NCT6775_REG_FAN_MODE[nr], reg);
1380 } else {
1381 reg = w83627ehf_read_value(data, W83627EHF_REG_PWM_ENABLE[nr]);
1382 reg &= ~(0x03 << W83627EHF_PWM_ENABLE_SHIFT[nr]);
1383 reg |= (val - 1) << W83627EHF_PWM_ENABLE_SHIFT[nr];
1384 w83627ehf_write_value(data, W83627EHF_REG_PWM_ENABLE[nr], reg);
1385 }
Rudolf Marek08c79952006-07-05 18:14:31 +02001386 mutex_unlock(&data->update_lock);
1387 return count;
1388}
1389
1390
1391#define show_tol_temp(reg) \
1392static ssize_t show_##reg(struct device *dev, struct device_attribute *attr, \
1393 char *buf) \
1394{ \
1395 struct w83627ehf_data *data = w83627ehf_update_device(dev); \
Guenter Roecke7e1ca62011-02-04 13:24:30 -08001396 struct sensor_device_attribute *sensor_attr = \
1397 to_sensor_dev_attr(attr); \
Rudolf Marek08c79952006-07-05 18:14:31 +02001398 int nr = sensor_attr->index; \
Guenter Roeckbce26c52011-02-04 12:54:14 -08001399 return sprintf(buf, "%d\n", data->reg[nr] * 1000); \
Rudolf Marek08c79952006-07-05 18:14:31 +02001400}
1401
1402show_tol_temp(tolerance)
1403show_tol_temp(target_temp)
1404
1405static ssize_t
1406store_target_temp(struct device *dev, struct device_attribute *attr,
1407 const char *buf, size_t count)
1408{
David Hubbard1ea6dd32007-06-24 11:16:15 +02001409 struct w83627ehf_data *data = dev_get_drvdata(dev);
Rudolf Marek08c79952006-07-05 18:14:31 +02001410 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
1411 int nr = sensor_attr->index;
Guenter Roeckbce26c52011-02-04 12:54:14 -08001412 long val;
1413 int err;
1414
1415 err = strict_strtol(buf, 10, &val);
1416 if (err < 0)
1417 return err;
1418
1419 val = SENSORS_LIMIT(DIV_ROUND_CLOSEST(val, 1000), 0, 127);
Rudolf Marek08c79952006-07-05 18:14:31 +02001420
1421 mutex_lock(&data->update_lock);
1422 data->target_temp[nr] = val;
Guenter Roeck279af1a2011-02-13 22:34:47 -08001423 w83627ehf_write_value(data, data->REG_TARGET[nr], val);
Rudolf Marek08c79952006-07-05 18:14:31 +02001424 mutex_unlock(&data->update_lock);
1425 return count;
1426}
1427
1428static ssize_t
1429store_tolerance(struct device *dev, struct device_attribute *attr,
1430 const char *buf, size_t count)
1431{
David Hubbard1ea6dd32007-06-24 11:16:15 +02001432 struct w83627ehf_data *data = dev_get_drvdata(dev);
Guenter Roeckec3e5a12011-02-02 08:46:49 -08001433 struct w83627ehf_sio_data *sio_data = dev->platform_data;
Rudolf Marek08c79952006-07-05 18:14:31 +02001434 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
1435 int nr = sensor_attr->index;
1436 u16 reg;
Guenter Roeckbce26c52011-02-04 12:54:14 -08001437 long val;
1438 int err;
1439
1440 err = strict_strtol(buf, 10, &val);
1441 if (err < 0)
1442 return err;
1443
Rudolf Marek08c79952006-07-05 18:14:31 +02001444 /* Limit the temp to 0C - 15C */
Guenter Roeckbce26c52011-02-04 12:54:14 -08001445 val = SENSORS_LIMIT(DIV_ROUND_CLOSEST(val, 1000), 0, 15);
Rudolf Marek08c79952006-07-05 18:14:31 +02001446
1447 mutex_lock(&data->update_lock);
Guenter Roeckec3e5a12011-02-02 08:46:49 -08001448 if (sio_data->kind == nct6775 || sio_data->kind == nct6776) {
1449 /* Limit tolerance further for NCT6776F */
1450 if (sio_data->kind == nct6776 && val > 7)
1451 val = 7;
1452 reg = w83627ehf_read_value(data, NCT6775_REG_FAN_MODE[nr]);
Rudolf Marek08c79952006-07-05 18:14:31 +02001453 reg = (reg & 0xf0) | val;
Guenter Roeckec3e5a12011-02-02 08:46:49 -08001454 w83627ehf_write_value(data, NCT6775_REG_FAN_MODE[nr], reg);
1455 } else {
1456 reg = w83627ehf_read_value(data, W83627EHF_REG_TOLERANCE[nr]);
1457 if (nr == 1)
1458 reg = (reg & 0x0f) | (val << 4);
1459 else
1460 reg = (reg & 0xf0) | val;
1461 w83627ehf_write_value(data, W83627EHF_REG_TOLERANCE[nr], reg);
1462 }
1463 data->tolerance[nr] = val;
Rudolf Marek08c79952006-07-05 18:14:31 +02001464 mutex_unlock(&data->update_lock);
1465 return count;
1466}
1467
1468static struct sensor_device_attribute sda_pwm[] = {
1469 SENSOR_ATTR(pwm1, S_IWUSR | S_IRUGO, show_pwm, store_pwm, 0),
1470 SENSOR_ATTR(pwm2, S_IWUSR | S_IRUGO, show_pwm, store_pwm, 1),
1471 SENSOR_ATTR(pwm3, S_IWUSR | S_IRUGO, show_pwm, store_pwm, 2),
1472 SENSOR_ATTR(pwm4, S_IWUSR | S_IRUGO, show_pwm, store_pwm, 3),
1473};
1474
1475static struct sensor_device_attribute sda_pwm_mode[] = {
1476 SENSOR_ATTR(pwm1_mode, S_IWUSR | S_IRUGO, show_pwm_mode,
1477 store_pwm_mode, 0),
1478 SENSOR_ATTR(pwm2_mode, S_IWUSR | S_IRUGO, show_pwm_mode,
1479 store_pwm_mode, 1),
1480 SENSOR_ATTR(pwm3_mode, S_IWUSR | S_IRUGO, show_pwm_mode,
1481 store_pwm_mode, 2),
1482 SENSOR_ATTR(pwm4_mode, S_IWUSR | S_IRUGO, show_pwm_mode,
1483 store_pwm_mode, 3),
1484};
1485
1486static struct sensor_device_attribute sda_pwm_enable[] = {
1487 SENSOR_ATTR(pwm1_enable, S_IWUSR | S_IRUGO, show_pwm_enable,
1488 store_pwm_enable, 0),
1489 SENSOR_ATTR(pwm2_enable, S_IWUSR | S_IRUGO, show_pwm_enable,
1490 store_pwm_enable, 1),
1491 SENSOR_ATTR(pwm3_enable, S_IWUSR | S_IRUGO, show_pwm_enable,
1492 store_pwm_enable, 2),
1493 SENSOR_ATTR(pwm4_enable, S_IWUSR | S_IRUGO, show_pwm_enable,
1494 store_pwm_enable, 3),
1495};
1496
1497static struct sensor_device_attribute sda_target_temp[] = {
1498 SENSOR_ATTR(pwm1_target, S_IWUSR | S_IRUGO, show_target_temp,
1499 store_target_temp, 0),
1500 SENSOR_ATTR(pwm2_target, S_IWUSR | S_IRUGO, show_target_temp,
1501 store_target_temp, 1),
1502 SENSOR_ATTR(pwm3_target, S_IWUSR | S_IRUGO, show_target_temp,
1503 store_target_temp, 2),
1504 SENSOR_ATTR(pwm4_target, S_IWUSR | S_IRUGO, show_target_temp,
1505 store_target_temp, 3),
1506};
1507
1508static struct sensor_device_attribute sda_tolerance[] = {
1509 SENSOR_ATTR(pwm1_tolerance, S_IWUSR | S_IRUGO, show_tolerance,
1510 store_tolerance, 0),
1511 SENSOR_ATTR(pwm2_tolerance, S_IWUSR | S_IRUGO, show_tolerance,
1512 store_tolerance, 1),
1513 SENSOR_ATTR(pwm3_tolerance, S_IWUSR | S_IRUGO, show_tolerance,
1514 store_tolerance, 2),
1515 SENSOR_ATTR(pwm4_tolerance, S_IWUSR | S_IRUGO, show_tolerance,
1516 store_tolerance, 3),
1517};
1518
Rudolf Marek08c79952006-07-05 18:14:31 +02001519/* Smart Fan registers */
1520
1521#define fan_functions(reg, REG) \
1522static ssize_t show_##reg(struct device *dev, struct device_attribute *attr, \
1523 char *buf) \
1524{ \
1525 struct w83627ehf_data *data = w83627ehf_update_device(dev); \
Guenter Roecke7e1ca62011-02-04 13:24:30 -08001526 struct sensor_device_attribute *sensor_attr = \
1527 to_sensor_dev_attr(attr); \
Rudolf Marek08c79952006-07-05 18:14:31 +02001528 int nr = sensor_attr->index; \
1529 return sprintf(buf, "%d\n", data->reg[nr]); \
Guenter Roecke7e1ca62011-02-04 13:24:30 -08001530} \
Rudolf Marek08c79952006-07-05 18:14:31 +02001531static ssize_t \
1532store_##reg(struct device *dev, struct device_attribute *attr, \
1533 const char *buf, size_t count) \
Guenter Roecke7e1ca62011-02-04 13:24:30 -08001534{ \
David Hubbard1ea6dd32007-06-24 11:16:15 +02001535 struct w83627ehf_data *data = dev_get_drvdata(dev); \
Guenter Roecke7e1ca62011-02-04 13:24:30 -08001536 struct sensor_device_attribute *sensor_attr = \
1537 to_sensor_dev_attr(attr); \
Rudolf Marek08c79952006-07-05 18:14:31 +02001538 int nr = sensor_attr->index; \
Guenter Roeckbce26c52011-02-04 12:54:14 -08001539 unsigned long val; \
1540 int err; \
1541 err = strict_strtoul(buf, 10, &val); \
1542 if (err < 0) \
1543 return err; \
1544 val = SENSORS_LIMIT(val, 1, 255); \
Rudolf Marek08c79952006-07-05 18:14:31 +02001545 mutex_lock(&data->update_lock); \
1546 data->reg[nr] = val; \
Guenter Roeckda2e0252010-08-14 21:08:55 +02001547 w83627ehf_write_value(data, data->REG_##REG[nr], val); \
Rudolf Marek08c79952006-07-05 18:14:31 +02001548 mutex_unlock(&data->update_lock); \
1549 return count; \
1550}
1551
Daniel J Blueman41e9a062009-12-14 18:01:37 -08001552fan_functions(fan_start_output, FAN_START_OUTPUT)
1553fan_functions(fan_stop_output, FAN_STOP_OUTPUT)
1554fan_functions(fan_max_output, FAN_MAX_OUTPUT)
1555fan_functions(fan_step_output, FAN_STEP_OUTPUT)
Rudolf Marek08c79952006-07-05 18:14:31 +02001556
1557#define fan_time_functions(reg, REG) \
1558static ssize_t show_##reg(struct device *dev, struct device_attribute *attr, \
1559 char *buf) \
1560{ \
1561 struct w83627ehf_data *data = w83627ehf_update_device(dev); \
Guenter Roecke7e1ca62011-02-04 13:24:30 -08001562 struct sensor_device_attribute *sensor_attr = \
1563 to_sensor_dev_attr(attr); \
Rudolf Marek08c79952006-07-05 18:14:31 +02001564 int nr = sensor_attr->index; \
1565 return sprintf(buf, "%d\n", \
Guenter Roecke7e1ca62011-02-04 13:24:30 -08001566 step_time_from_reg(data->reg[nr], \
1567 data->pwm_mode[nr])); \
Rudolf Marek08c79952006-07-05 18:14:31 +02001568} \
1569\
1570static ssize_t \
1571store_##reg(struct device *dev, struct device_attribute *attr, \
1572 const char *buf, size_t count) \
1573{ \
David Hubbard1ea6dd32007-06-24 11:16:15 +02001574 struct w83627ehf_data *data = dev_get_drvdata(dev); \
Guenter Roecke7e1ca62011-02-04 13:24:30 -08001575 struct sensor_device_attribute *sensor_attr = \
1576 to_sensor_dev_attr(attr); \
Rudolf Marek08c79952006-07-05 18:14:31 +02001577 int nr = sensor_attr->index; \
Guenter Roeckbce26c52011-02-04 12:54:14 -08001578 unsigned long val; \
1579 int err; \
1580 err = strict_strtoul(buf, 10, &val); \
1581 if (err < 0) \
1582 return err; \
1583 val = step_time_to_reg(val, data->pwm_mode[nr]); \
Rudolf Marek08c79952006-07-05 18:14:31 +02001584 mutex_lock(&data->update_lock); \
1585 data->reg[nr] = val; \
David Hubbard1ea6dd32007-06-24 11:16:15 +02001586 w83627ehf_write_value(data, W83627EHF_REG_##REG[nr], val); \
Rudolf Marek08c79952006-07-05 18:14:31 +02001587 mutex_unlock(&data->update_lock); \
1588 return count; \
1589} \
1590
1591fan_time_functions(fan_stop_time, FAN_STOP_TIME)
1592
David Hubbard1ea6dd32007-06-24 11:16:15 +02001593static ssize_t show_name(struct device *dev, struct device_attribute *attr,
1594 char *buf)
1595{
1596 struct w83627ehf_data *data = dev_get_drvdata(dev);
1597
1598 return sprintf(buf, "%s\n", data->name);
1599}
1600static DEVICE_ATTR(name, S_IRUGO, show_name, NULL);
Rudolf Marek08c79952006-07-05 18:14:31 +02001601
1602static struct sensor_device_attribute sda_sf3_arrays_fan4[] = {
1603 SENSOR_ATTR(pwm4_stop_time, S_IWUSR | S_IRUGO, show_fan_stop_time,
1604 store_fan_stop_time, 3),
Daniel J Blueman41e9a062009-12-14 18:01:37 -08001605 SENSOR_ATTR(pwm4_start_output, S_IWUSR | S_IRUGO, show_fan_start_output,
1606 store_fan_start_output, 3),
1607 SENSOR_ATTR(pwm4_stop_output, S_IWUSR | S_IRUGO, show_fan_stop_output,
1608 store_fan_stop_output, 3),
1609 SENSOR_ATTR(pwm4_max_output, S_IWUSR | S_IRUGO, show_fan_max_output,
1610 store_fan_max_output, 3),
1611 SENSOR_ATTR(pwm4_step_output, S_IWUSR | S_IRUGO, show_fan_step_output,
1612 store_fan_step_output, 3),
Rudolf Marek08c79952006-07-05 18:14:31 +02001613};
1614
1615static struct sensor_device_attribute sda_sf3_arrays[] = {
1616 SENSOR_ATTR(pwm1_stop_time, S_IWUSR | S_IRUGO, show_fan_stop_time,
1617 store_fan_stop_time, 0),
1618 SENSOR_ATTR(pwm2_stop_time, S_IWUSR | S_IRUGO, show_fan_stop_time,
1619 store_fan_stop_time, 1),
1620 SENSOR_ATTR(pwm3_stop_time, S_IWUSR | S_IRUGO, show_fan_stop_time,
1621 store_fan_stop_time, 2),
Daniel J Blueman41e9a062009-12-14 18:01:37 -08001622 SENSOR_ATTR(pwm1_start_output, S_IWUSR | S_IRUGO, show_fan_start_output,
1623 store_fan_start_output, 0),
1624 SENSOR_ATTR(pwm2_start_output, S_IWUSR | S_IRUGO, show_fan_start_output,
1625 store_fan_start_output, 1),
1626 SENSOR_ATTR(pwm3_start_output, S_IWUSR | S_IRUGO, show_fan_start_output,
1627 store_fan_start_output, 2),
1628 SENSOR_ATTR(pwm1_stop_output, S_IWUSR | S_IRUGO, show_fan_stop_output,
1629 store_fan_stop_output, 0),
1630 SENSOR_ATTR(pwm2_stop_output, S_IWUSR | S_IRUGO, show_fan_stop_output,
1631 store_fan_stop_output, 1),
1632 SENSOR_ATTR(pwm3_stop_output, S_IWUSR | S_IRUGO, show_fan_stop_output,
1633 store_fan_stop_output, 2),
Guenter Roeckda2e0252010-08-14 21:08:55 +02001634};
Daniel J Blueman41e9a062009-12-14 18:01:37 -08001635
Guenter Roeckda2e0252010-08-14 21:08:55 +02001636
1637/*
1638 * pwm1 and pwm3 don't support max and step settings on all chips.
1639 * Need to check support while generating/removing attribute files.
1640 */
1641static struct sensor_device_attribute sda_sf3_max_step_arrays[] = {
1642 SENSOR_ATTR(pwm1_max_output, S_IWUSR | S_IRUGO, show_fan_max_output,
1643 store_fan_max_output, 0),
1644 SENSOR_ATTR(pwm1_step_output, S_IWUSR | S_IRUGO, show_fan_step_output,
1645 store_fan_step_output, 0),
Daniel J Blueman41e9a062009-12-14 18:01:37 -08001646 SENSOR_ATTR(pwm2_max_output, S_IWUSR | S_IRUGO, show_fan_max_output,
1647 store_fan_max_output, 1),
1648 SENSOR_ATTR(pwm2_step_output, S_IWUSR | S_IRUGO, show_fan_step_output,
1649 store_fan_step_output, 1),
Guenter Roeckda2e0252010-08-14 21:08:55 +02001650 SENSOR_ATTR(pwm3_max_output, S_IWUSR | S_IRUGO, show_fan_max_output,
1651 store_fan_max_output, 2),
1652 SENSOR_ATTR(pwm3_step_output, S_IWUSR | S_IRUGO, show_fan_step_output,
1653 store_fan_step_output, 2),
Rudolf Marek08c79952006-07-05 18:14:31 +02001654};
1655
Jean Delvarefc18d6c2007-06-24 11:19:42 +02001656static ssize_t
1657show_vid(struct device *dev, struct device_attribute *attr, char *buf)
1658{
1659 struct w83627ehf_data *data = dev_get_drvdata(dev);
1660 return sprintf(buf, "%d\n", vid_from_reg(data->vid, data->vrm));
1661}
1662static DEVICE_ATTR(cpu0_vid, S_IRUGO, show_vid, NULL);
1663
Jean Delvare08e7e272005-04-25 22:43:25 +02001664/*
David Hubbard1ea6dd32007-06-24 11:16:15 +02001665 * Driver and device management
Jean Delvare08e7e272005-04-25 22:43:25 +02001666 */
1667
David Hubbardc18beb52006-09-24 21:04:38 +02001668static void w83627ehf_device_remove_files(struct device *dev)
1669{
1670 /* some entries in the following arrays may not have been used in
1671 * device_create_file(), but device_remove_file() will ignore them */
1672 int i;
David Hubbard1ea6dd32007-06-24 11:16:15 +02001673 struct w83627ehf_data *data = dev_get_drvdata(dev);
David Hubbardc18beb52006-09-24 21:04:38 +02001674
1675 for (i = 0; i < ARRAY_SIZE(sda_sf3_arrays); i++)
1676 device_remove_file(dev, &sda_sf3_arrays[i].dev_attr);
Guenter Roeckda2e0252010-08-14 21:08:55 +02001677 for (i = 0; i < ARRAY_SIZE(sda_sf3_max_step_arrays); i++) {
1678 struct sensor_device_attribute *attr =
1679 &sda_sf3_max_step_arrays[i];
Guenter Roeckec3e5a12011-02-02 08:46:49 -08001680 if (data->REG_FAN_STEP_OUTPUT &&
1681 data->REG_FAN_STEP_OUTPUT[attr->index] != 0xff)
Guenter Roeckda2e0252010-08-14 21:08:55 +02001682 device_remove_file(dev, &attr->dev_attr);
1683 }
David Hubbardc18beb52006-09-24 21:04:38 +02001684 for (i = 0; i < ARRAY_SIZE(sda_sf3_arrays_fan4); i++)
1685 device_remove_file(dev, &sda_sf3_arrays_fan4[i].dev_attr);
David Hubbard1ea6dd32007-06-24 11:16:15 +02001686 for (i = 0; i < data->in_num; i++) {
Gong Juna157d062009-03-30 21:46:43 +02001687 if ((i == 6) && data->in6_skip)
1688 continue;
David Hubbardc18beb52006-09-24 21:04:38 +02001689 device_remove_file(dev, &sda_in_input[i].dev_attr);
1690 device_remove_file(dev, &sda_in_alarm[i].dev_attr);
1691 device_remove_file(dev, &sda_in_min[i].dev_attr);
1692 device_remove_file(dev, &sda_in_max[i].dev_attr);
1693 }
1694 for (i = 0; i < 5; i++) {
1695 device_remove_file(dev, &sda_fan_input[i].dev_attr);
1696 device_remove_file(dev, &sda_fan_alarm[i].dev_attr);
1697 device_remove_file(dev, &sda_fan_div[i].dev_attr);
1698 device_remove_file(dev, &sda_fan_min[i].dev_attr);
1699 }
Gong Jun237c8d22009-03-30 21:46:42 +02001700 for (i = 0; i < data->pwm_num; i++) {
David Hubbardc18beb52006-09-24 21:04:38 +02001701 device_remove_file(dev, &sda_pwm[i].dev_attr);
1702 device_remove_file(dev, &sda_pwm_mode[i].dev_attr);
1703 device_remove_file(dev, &sda_pwm_enable[i].dev_attr);
1704 device_remove_file(dev, &sda_target_temp[i].dev_attr);
1705 device_remove_file(dev, &sda_tolerance[i].dev_attr);
1706 }
Guenter Roeckd36cf322011-02-07 15:08:54 -08001707 for (i = 0; i < NUM_REG_TEMP; i++) {
1708 if (!(data->have_temp & (1 << i)))
Gong Juna157d062009-03-30 21:46:43 +02001709 continue;
1710 device_remove_file(dev, &sda_temp_input[i].dev_attr);
Guenter Roeckd36cf322011-02-07 15:08:54 -08001711 device_remove_file(dev, &sda_temp_label[i].dev_attr);
Gong Juna157d062009-03-30 21:46:43 +02001712 device_remove_file(dev, &sda_temp_max[i].dev_attr);
1713 device_remove_file(dev, &sda_temp_max_hyst[i].dev_attr);
Guenter Roeckec3e5a12011-02-02 08:46:49 -08001714 if (i > 2)
1715 continue;
Gong Juna157d062009-03-30 21:46:43 +02001716 device_remove_file(dev, &sda_temp_alarm[i].dev_attr);
1717 device_remove_file(dev, &sda_temp_type[i].dev_attr);
1718 }
David Hubbard1ea6dd32007-06-24 11:16:15 +02001719
1720 device_remove_file(dev, &dev_attr_name);
Jean Delvarecbe311f2008-01-03 21:22:44 +01001721 device_remove_file(dev, &dev_attr_cpu0_vid);
David Hubbardc18beb52006-09-24 21:04:38 +02001722}
1723
David Hubbard1ea6dd32007-06-24 11:16:15 +02001724/* Get the monitoring functions started */
Jean Delvare05ffb672011-10-13 15:49:08 -04001725static inline void __devinit w83627ehf_init_device(struct w83627ehf_data *data,
1726 enum kinds kind)
Jean Delvare08e7e272005-04-25 22:43:25 +02001727{
1728 int i;
Jean Delvareda667362007-06-24 11:21:02 +02001729 u8 tmp, diode;
Jean Delvare08e7e272005-04-25 22:43:25 +02001730
1731 /* Start monitoring is needed */
David Hubbard1ea6dd32007-06-24 11:16:15 +02001732 tmp = w83627ehf_read_value(data, W83627EHF_REG_CONFIG);
Jean Delvare08e7e272005-04-25 22:43:25 +02001733 if (!(tmp & 0x01))
David Hubbard1ea6dd32007-06-24 11:16:15 +02001734 w83627ehf_write_value(data, W83627EHF_REG_CONFIG,
Jean Delvare08e7e272005-04-25 22:43:25 +02001735 tmp | 0x01);
1736
Guenter Roeckd36cf322011-02-07 15:08:54 -08001737 /* Enable temperature sensors if needed */
1738 for (i = 0; i < NUM_REG_TEMP; i++) {
1739 if (!(data->have_temp & (1 << i)))
1740 continue;
Guenter Roeckec3e5a12011-02-02 08:46:49 -08001741 if (!data->reg_temp_config[i])
Guenter Roeckd36cf322011-02-07 15:08:54 -08001742 continue;
David Hubbard1ea6dd32007-06-24 11:16:15 +02001743 tmp = w83627ehf_read_value(data,
Guenter Roeckec3e5a12011-02-02 08:46:49 -08001744 data->reg_temp_config[i]);
Jean Delvare08e7e272005-04-25 22:43:25 +02001745 if (tmp & 0x01)
David Hubbard1ea6dd32007-06-24 11:16:15 +02001746 w83627ehf_write_value(data,
Guenter Roeckec3e5a12011-02-02 08:46:49 -08001747 data->reg_temp_config[i],
Jean Delvare08e7e272005-04-25 22:43:25 +02001748 tmp & 0xfe);
1749 }
Jean Delvared3130f02007-06-24 11:20:13 +02001750
1751 /* Enable VBAT monitoring if needed */
1752 tmp = w83627ehf_read_value(data, W83627EHF_REG_VBAT);
1753 if (!(tmp & 0x01))
1754 w83627ehf_write_value(data, W83627EHF_REG_VBAT, tmp | 0x01);
Jean Delvareda667362007-06-24 11:21:02 +02001755
1756 /* Get thermal sensor types */
Jean Delvare05ffb672011-10-13 15:49:08 -04001757 switch (kind) {
1758 case w83627ehf:
1759 diode = w83627ehf_read_value(data, W83627EHF_REG_DIODE);
1760 break;
1761 default:
1762 diode = 0x70;
1763 }
Jean Delvareda667362007-06-24 11:21:02 +02001764 for (i = 0; i < 3; i++) {
Guenter Roeck24b79132011-11-06 20:25:18 +01001765 const char *label = NULL;
1766
1767 if (data->temp_label)
1768 label = data->temp_label[data->temp_src[i]];
Jean Delvaree3c29812011-11-04 12:00:47 +01001769
1770 /* Digital source overrides analog type */
Guenter Roeck24b79132011-11-06 20:25:18 +01001771 if (label && strncmp(label, "PECI", 4) == 0)
Jean Delvaree3c29812011-11-04 12:00:47 +01001772 data->temp_type[i] = 6;
Guenter Roeck24b79132011-11-06 20:25:18 +01001773 else if (label && strncmp(label, "AMD", 3) == 0)
Jean Delvaree3c29812011-11-04 12:00:47 +01001774 data->temp_type[i] = 5;
1775 else if ((tmp & (0x02 << i)))
Jean Delvare05ffb672011-10-13 15:49:08 -04001776 data->temp_type[i] = (diode & (0x10 << i)) ? 1 : 3;
Jean Delvareda667362007-06-24 11:21:02 +02001777 else
1778 data->temp_type[i] = 4; /* thermistor */
1779 }
Jean Delvare08e7e272005-04-25 22:43:25 +02001780}
1781
Guenter Roeckec3e5a12011-02-02 08:46:49 -08001782static void w82627ehf_swap_tempreg(struct w83627ehf_data *data,
1783 int r1, int r2)
1784{
1785 u16 tmp;
1786
1787 tmp = data->temp_src[r1];
1788 data->temp_src[r1] = data->temp_src[r2];
1789 data->temp_src[r2] = tmp;
1790
1791 tmp = data->reg_temp[r1];
1792 data->reg_temp[r1] = data->reg_temp[r2];
1793 data->reg_temp[r2] = tmp;
1794
1795 tmp = data->reg_temp_over[r1];
1796 data->reg_temp_over[r1] = data->reg_temp_over[r2];
1797 data->reg_temp_over[r2] = tmp;
1798
1799 tmp = data->reg_temp_hyst[r1];
1800 data->reg_temp_hyst[r1] = data->reg_temp_hyst[r2];
1801 data->reg_temp_hyst[r2] = tmp;
1802
1803 tmp = data->reg_temp_config[r1];
1804 data->reg_temp_config[r1] = data->reg_temp_config[r2];
1805 data->reg_temp_config[r2] = tmp;
1806}
1807
David Hubbard1ea6dd32007-06-24 11:16:15 +02001808static int __devinit w83627ehf_probe(struct platform_device *pdev)
Jean Delvare08e7e272005-04-25 22:43:25 +02001809{
David Hubbard1ea6dd32007-06-24 11:16:15 +02001810 struct device *dev = &pdev->dev;
1811 struct w83627ehf_sio_data *sio_data = dev->platform_data;
Jean Delvare08e7e272005-04-25 22:43:25 +02001812 struct w83627ehf_data *data;
David Hubbard1ea6dd32007-06-24 11:16:15 +02001813 struct resource *res;
Guenter Roeckec3e5a12011-02-02 08:46:49 -08001814 u8 fan3pin, fan4pin, fan4min, fan5pin, en_vrm10;
Jean Delvare08e7e272005-04-25 22:43:25 +02001815 int i, err = 0;
1816
David Hubbard1ea6dd32007-06-24 11:16:15 +02001817 res = platform_get_resource(pdev, IORESOURCE_IO, 0);
1818 if (!request_region(res->start, IOREGION_LENGTH, DRVNAME)) {
Jean Delvare08e7e272005-04-25 22:43:25 +02001819 err = -EBUSY;
David Hubbard1ea6dd32007-06-24 11:16:15 +02001820 dev_err(dev, "Failed to request region 0x%lx-0x%lx\n",
1821 (unsigned long)res->start,
1822 (unsigned long)res->start + IOREGION_LENGTH - 1);
Jean Delvare08e7e272005-04-25 22:43:25 +02001823 goto exit;
1824 }
1825
Guenter Roecke7e1ca62011-02-04 13:24:30 -08001826 data = kzalloc(sizeof(struct w83627ehf_data), GFP_KERNEL);
1827 if (!data) {
Jean Delvare08e7e272005-04-25 22:43:25 +02001828 err = -ENOMEM;
1829 goto exit_release;
1830 }
Jean Delvare08e7e272005-04-25 22:43:25 +02001831
David Hubbard1ea6dd32007-06-24 11:16:15 +02001832 data->addr = res->start;
Ingo Molnar9a61bf62006-01-18 23:19:26 +01001833 mutex_init(&data->lock);
Ingo Molnar9a61bf62006-01-18 23:19:26 +01001834 mutex_init(&data->update_lock);
David Hubbard1ea6dd32007-06-24 11:16:15 +02001835 data->name = w83627ehf_device_names[sio_data->kind];
1836 platform_set_drvdata(pdev, data);
Jean Delvare08e7e272005-04-25 22:43:25 +02001837
Gong Jun237c8d22009-03-30 21:46:42 +02001838 /* 627EHG and 627EHF have 10 voltage inputs; 627DHG and 667HG have 9 */
1839 data->in_num = (sio_data->kind == w83627ehf) ? 10 : 9;
Guenter Roeckec3e5a12011-02-02 08:46:49 -08001840 /* 667HG, NCT6775F, and NCT6776F have 3 pwms */
Guenter Roeckc39aeda2010-08-14 21:08:55 +02001841 data->pwm_num = (sio_data->kind == w83667hg
Guenter Roeckec3e5a12011-02-02 08:46:49 -08001842 || sio_data->kind == w83667hg_b
1843 || sio_data->kind == nct6775
1844 || sio_data->kind == nct6776) ? 3 : 4;
Jean Delvare08e7e272005-04-25 22:43:25 +02001845
Guenter Roeckd36cf322011-02-07 15:08:54 -08001846 data->have_temp = 0x07;
Gong Juna157d062009-03-30 21:46:43 +02001847 /* Check temp3 configuration bit for 667HG */
Guenter Roeckd36cf322011-02-07 15:08:54 -08001848 if (sio_data->kind == w83667hg) {
1849 u8 reg;
1850
1851 reg = w83627ehf_read_value(data, W83627EHF_REG_TEMP_CONFIG[2]);
1852 if (reg & 0x01)
1853 data->have_temp &= ~(1 << 2);
1854 else
Guenter Roeckec3e5a12011-02-02 08:46:49 -08001855 data->in6_skip = 1; /* either temp3 or in6 */
1856 }
1857
1858 /* Deal with temperature register setup first. */
1859 if (sio_data->kind == nct6775 || sio_data->kind == nct6776) {
1860 int mask = 0;
1861
1862 /*
1863 * Display temperature sensor output only if it monitors
1864 * a source other than one already reported. Always display
1865 * first three temperature registers, though.
1866 */
1867 for (i = 0; i < NUM_REG_TEMP; i++) {
1868 u8 src;
1869
1870 data->reg_temp[i] = NCT6775_REG_TEMP[i];
1871 data->reg_temp_over[i] = NCT6775_REG_TEMP_OVER[i];
1872 data->reg_temp_hyst[i] = NCT6775_REG_TEMP_HYST[i];
1873 data->reg_temp_config[i] = NCT6775_REG_TEMP_CONFIG[i];
1874
1875 src = w83627ehf_read_value(data,
1876 NCT6775_REG_TEMP_SOURCE[i]);
1877 src &= 0x1f;
1878 if (src && !(mask & (1 << src))) {
1879 data->have_temp |= 1 << i;
1880 mask |= 1 << src;
1881 }
1882
1883 data->temp_src[i] = src;
1884
1885 /*
1886 * Now do some register swapping if index 0..2 don't
1887 * point to SYSTIN(1), CPUIN(2), and AUXIN(3).
1888 * Idea is to have the first three attributes
1889 * report SYSTIN, CPUIN, and AUXIN if possible
1890 * without overriding the basic system configuration.
1891 */
1892 if (i > 0 && data->temp_src[0] != 1
1893 && data->temp_src[i] == 1)
1894 w82627ehf_swap_tempreg(data, 0, i);
1895 if (i > 1 && data->temp_src[1] != 2
1896 && data->temp_src[i] == 2)
1897 w82627ehf_swap_tempreg(data, 1, i);
1898 if (i > 2 && data->temp_src[2] != 3
1899 && data->temp_src[i] == 3)
1900 w82627ehf_swap_tempreg(data, 2, i);
1901 }
1902 if (sio_data->kind == nct6776) {
1903 /*
1904 * On NCT6776, AUXTIN and VIN3 pins are shared.
1905 * Only way to detect it is to check if AUXTIN is used
1906 * as a temperature source, and if that source is
1907 * enabled.
1908 *
1909 * If that is the case, disable in6, which reports VIN3.
1910 * Otherwise disable temp3.
1911 */
1912 if (data->temp_src[2] == 3) {
1913 u8 reg;
1914
1915 if (data->reg_temp_config[2])
1916 reg = w83627ehf_read_value(data,
1917 data->reg_temp_config[2]);
1918 else
1919 reg = 0; /* Assume AUXTIN is used */
1920
1921 if (reg & 0x01)
1922 data->have_temp &= ~(1 << 2);
1923 else
1924 data->in6_skip = 1;
1925 }
Guenter Roeck02309ad2011-03-10 15:47:14 -08001926 data->temp_label = nct6776_temp_label;
1927 } else {
1928 data->temp_label = nct6775_temp_label;
Guenter Roeckec3e5a12011-02-02 08:46:49 -08001929 }
Guenter Roeckd36cf322011-02-07 15:08:54 -08001930 } else if (sio_data->kind == w83667hg_b) {
1931 u8 reg;
1932
Guenter Roeckec3e5a12011-02-02 08:46:49 -08001933 /*
1934 * Temperature sources are selected with bank 0, registers 0x49
1935 * and 0x4a.
1936 */
1937 for (i = 0; i < ARRAY_SIZE(W83627EHF_REG_TEMP); i++) {
1938 data->reg_temp[i] = W83627EHF_REG_TEMP[i];
1939 data->reg_temp_over[i] = W83627EHF_REG_TEMP_OVER[i];
1940 data->reg_temp_hyst[i] = W83627EHF_REG_TEMP_HYST[i];
1941 data->reg_temp_config[i] = W83627EHF_REG_TEMP_CONFIG[i];
1942 }
Guenter Roeckd36cf322011-02-07 15:08:54 -08001943 reg = w83627ehf_read_value(data, 0x4a);
1944 data->temp_src[0] = reg >> 5;
1945 reg = w83627ehf_read_value(data, 0x49);
1946 data->temp_src[1] = reg & 0x07;
Guenter Roeckec3e5a12011-02-02 08:46:49 -08001947 data->temp_src[2] = (reg >> 4) & 0x07;
Guenter Roeckd36cf322011-02-07 15:08:54 -08001948
1949 /*
1950 * W83667HG-B has another temperature register at 0x7e.
1951 * The temperature source is selected with register 0x7d.
1952 * Support it if the source differs from already reported
1953 * sources.
1954 */
1955 reg = w83627ehf_read_value(data, 0x7d);
1956 reg &= 0x07;
1957 if (reg != data->temp_src[0] && reg != data->temp_src[1]
1958 && reg != data->temp_src[2]) {
1959 data->temp_src[3] = reg;
1960 data->have_temp |= 1 << 3;
1961 }
1962
1963 /*
1964 * Chip supports either AUXTIN or VIN3. Try to find out which
1965 * one.
1966 */
1967 reg = w83627ehf_read_value(data, W83627EHF_REG_TEMP_CONFIG[2]);
1968 if (data->temp_src[2] == 2 && (reg & 0x01))
1969 data->have_temp &= ~(1 << 2);
1970
1971 if ((data->temp_src[2] == 2 && (data->have_temp & (1 << 2)))
1972 || (data->temp_src[3] == 2 && (data->have_temp & (1 << 3))))
1973 data->in6_skip = 1;
1974
1975 data->temp_label = w83667hg_b_temp_label;
Guenter Roeckec3e5a12011-02-02 08:46:49 -08001976 } else {
1977 /* Temperature sources are fixed */
1978 for (i = 0; i < 3; i++) {
1979 data->reg_temp[i] = W83627EHF_REG_TEMP[i];
1980 data->reg_temp_over[i] = W83627EHF_REG_TEMP_OVER[i];
1981 data->reg_temp_hyst[i] = W83627EHF_REG_TEMP_HYST[i];
1982 data->reg_temp_config[i] = W83627EHF_REG_TEMP_CONFIG[i];
1983 }
Gong Juna157d062009-03-30 21:46:43 +02001984 }
1985
Guenter Roeckec3e5a12011-02-02 08:46:49 -08001986 if (sio_data->kind == nct6775) {
Guenter Roeck26bc4402011-02-11 08:00:58 -08001987 data->has_fan_div = true;
1988 data->fan_from_reg = fan_from_reg16;
1989 data->fan_from_reg_min = fan_from_reg8;
Guenter Roeckec3e5a12011-02-02 08:46:49 -08001990 data->REG_PWM = NCT6775_REG_PWM;
1991 data->REG_TARGET = NCT6775_REG_TARGET;
Guenter Roeck26bc4402011-02-11 08:00:58 -08001992 data->REG_FAN = NCT6775_REG_FAN;
Guenter Roeckec3e5a12011-02-02 08:46:49 -08001993 data->REG_FAN_MIN = W83627EHF_REG_FAN_MIN;
1994 data->REG_FAN_START_OUTPUT = NCT6775_REG_FAN_START_OUTPUT;
1995 data->REG_FAN_STOP_OUTPUT = NCT6775_REG_FAN_STOP_OUTPUT;
1996 data->REG_FAN_STOP_TIME = NCT6775_REG_FAN_STOP_TIME;
1997 data->REG_FAN_MAX_OUTPUT = NCT6775_REG_FAN_MAX_OUTPUT;
1998 data->REG_FAN_STEP_OUTPUT = NCT6775_REG_FAN_STEP_OUTPUT;
1999 } else if (sio_data->kind == nct6776) {
Guenter Roeck26bc4402011-02-11 08:00:58 -08002000 data->has_fan_div = false;
2001 data->fan_from_reg = fan_from_reg13;
2002 data->fan_from_reg_min = fan_from_reg13;
Guenter Roeckec3e5a12011-02-02 08:46:49 -08002003 data->REG_PWM = NCT6775_REG_PWM;
2004 data->REG_TARGET = NCT6775_REG_TARGET;
Guenter Roeck26bc4402011-02-11 08:00:58 -08002005 data->REG_FAN = NCT6775_REG_FAN;
Guenter Roeckec3e5a12011-02-02 08:46:49 -08002006 data->REG_FAN_MIN = NCT6776_REG_FAN_MIN;
2007 data->REG_FAN_START_OUTPUT = NCT6775_REG_FAN_START_OUTPUT;
2008 data->REG_FAN_STOP_OUTPUT = NCT6775_REG_FAN_STOP_OUTPUT;
2009 data->REG_FAN_STOP_TIME = NCT6775_REG_FAN_STOP_TIME;
2010 } else if (sio_data->kind == w83667hg_b) {
Guenter Roeck26bc4402011-02-11 08:00:58 -08002011 data->has_fan_div = true;
2012 data->fan_from_reg = fan_from_reg8;
2013 data->fan_from_reg_min = fan_from_reg8;
Guenter Roeckec3e5a12011-02-02 08:46:49 -08002014 data->REG_PWM = W83627EHF_REG_PWM;
2015 data->REG_TARGET = W83627EHF_REG_TARGET;
2016 data->REG_FAN = W83627EHF_REG_FAN;
2017 data->REG_FAN_MIN = W83627EHF_REG_FAN_MIN;
2018 data->REG_FAN_START_OUTPUT = W83627EHF_REG_FAN_START_OUTPUT;
2019 data->REG_FAN_STOP_OUTPUT = W83627EHF_REG_FAN_STOP_OUTPUT;
2020 data->REG_FAN_STOP_TIME = W83627EHF_REG_FAN_STOP_TIME;
Guenter Roeckc39aeda2010-08-14 21:08:55 +02002021 data->REG_FAN_MAX_OUTPUT =
2022 W83627EHF_REG_FAN_MAX_OUTPUT_W83667_B;
2023 data->REG_FAN_STEP_OUTPUT =
2024 W83627EHF_REG_FAN_STEP_OUTPUT_W83667_B;
2025 } else {
Guenter Roeck26bc4402011-02-11 08:00:58 -08002026 data->has_fan_div = true;
2027 data->fan_from_reg = fan_from_reg8;
2028 data->fan_from_reg_min = fan_from_reg8;
Guenter Roeckec3e5a12011-02-02 08:46:49 -08002029 data->REG_PWM = W83627EHF_REG_PWM;
2030 data->REG_TARGET = W83627EHF_REG_TARGET;
2031 data->REG_FAN = W83627EHF_REG_FAN;
2032 data->REG_FAN_MIN = W83627EHF_REG_FAN_MIN;
2033 data->REG_FAN_START_OUTPUT = W83627EHF_REG_FAN_START_OUTPUT;
2034 data->REG_FAN_STOP_OUTPUT = W83627EHF_REG_FAN_STOP_OUTPUT;
2035 data->REG_FAN_STOP_TIME = W83627EHF_REG_FAN_STOP_TIME;
Guenter Roeckc39aeda2010-08-14 21:08:55 +02002036 data->REG_FAN_MAX_OUTPUT =
2037 W83627EHF_REG_FAN_MAX_OUTPUT_COMMON;
2038 data->REG_FAN_STEP_OUTPUT =
2039 W83627EHF_REG_FAN_STEP_OUTPUT_COMMON;
2040 }
Guenter Roeckda2e0252010-08-14 21:08:55 +02002041
Jean Delvare08e7e272005-04-25 22:43:25 +02002042 /* Initialize the chip */
Jean Delvare05ffb672011-10-13 15:49:08 -04002043 w83627ehf_init_device(data, sio_data->kind);
Jean Delvare08e7e272005-04-25 22:43:25 +02002044
Jean Delvarefc18d6c2007-06-24 11:19:42 +02002045 data->vrm = vid_which_vrm();
2046 superio_enter(sio_data->sioreg);
Jean Delvarefc18d6c2007-06-24 11:19:42 +02002047 /* Read VID value */
Guenter Roeckec3e5a12011-02-02 08:46:49 -08002048 if (sio_data->kind == w83667hg || sio_data->kind == w83667hg_b ||
2049 sio_data->kind == nct6775 || sio_data->kind == nct6776) {
Gong Jun237c8d22009-03-30 21:46:42 +02002050 /* W83667HG has different pins for VID input and output, so
2051 we can get the VID input values directly at logical device D
2052 0xe3. */
2053 superio_select(sio_data->sioreg, W83667HG_LD_VID);
2054 data->vid = superio_inb(sio_data->sioreg, 0xe3);
Jean Delvarecbe311f2008-01-03 21:22:44 +01002055 err = device_create_file(dev, &dev_attr_cpu0_vid);
2056 if (err)
2057 goto exit_release;
Jean Delvare58e6e782008-01-03 07:33:31 -05002058 } else {
Gong Jun237c8d22009-03-30 21:46:42 +02002059 superio_select(sio_data->sioreg, W83627EHF_LD_HWM);
2060 if (superio_inb(sio_data->sioreg, SIO_REG_VID_CTRL) & 0x80) {
2061 /* Set VID input sensibility if needed. In theory the
2062 BIOS should have set it, but in practice it's not
2063 always the case. We only do it for the W83627EHF/EHG
2064 because the W83627DHG is more complex in this
2065 respect. */
2066 if (sio_data->kind == w83627ehf) {
2067 en_vrm10 = superio_inb(sio_data->sioreg,
2068 SIO_REG_EN_VRM10);
2069 if ((en_vrm10 & 0x08) && data->vrm == 90) {
2070 dev_warn(dev, "Setting VID input "
2071 "voltage to TTL\n");
2072 superio_outb(sio_data->sioreg,
2073 SIO_REG_EN_VRM10,
2074 en_vrm10 & ~0x08);
2075 } else if (!(en_vrm10 & 0x08)
2076 && data->vrm == 100) {
2077 dev_warn(dev, "Setting VID input "
2078 "voltage to VRM10\n");
2079 superio_outb(sio_data->sioreg,
2080 SIO_REG_EN_VRM10,
2081 en_vrm10 | 0x08);
2082 }
2083 }
2084
2085 data->vid = superio_inb(sio_data->sioreg,
2086 SIO_REG_VID_DATA);
2087 if (sio_data->kind == w83627ehf) /* 6 VID pins only */
2088 data->vid &= 0x3f;
2089
2090 err = device_create_file(dev, &dev_attr_cpu0_vid);
2091 if (err)
2092 goto exit_release;
2093 } else {
2094 dev_info(dev, "VID pins in output mode, CPU VID not "
2095 "available\n");
2096 }
Jean Delvarefc18d6c2007-06-24 11:19:42 +02002097 }
2098
Rudolf Marek08c79952006-07-05 18:14:31 +02002099 /* fan4 and fan5 share some pins with the GPIO and serial flash */
Guenter Roeckec3e5a12011-02-02 08:46:49 -08002100 if (sio_data->kind == nct6775) {
2101 /* On NCT6775, fan4 shares pins with the fdc interface */
2102 fan3pin = 1;
2103 fan4pin = !(superio_inb(sio_data->sioreg, 0x2A) & 0x80);
2104 fan4min = 0;
2105 fan5pin = 0;
2106 } else if (sio_data->kind == nct6776) {
Guenter Roeckeb521fb2012-01-27 05:43:59 -08002107 bool gpok = superio_inb(sio_data->sioreg, 0x27) & 0x80;
2108 u8 regval;
2109
2110 superio_select(sio_data->sioreg, W83627EHF_LD_HWM);
2111 regval = superio_inb(sio_data->sioreg, SIO_REG_ENABLE);
2112
2113 if (regval & 0x80)
2114 fan3pin = gpok;
2115 else
2116 fan3pin = !(superio_inb(sio_data->sioreg, 0x24) & 0x40);
2117
2118 if (regval & 0x40)
2119 fan4pin = gpok;
2120 else
2121 fan4pin = !!(superio_inb(sio_data->sioreg, 0x1C)
2122 & 0x01);
2123
2124 if (regval & 0x20)
2125 fan5pin = gpok;
2126 else
2127 fan5pin = !!(superio_inb(sio_data->sioreg, 0x1C)
2128 & 0x02);
2129
Guenter Roeckec3e5a12011-02-02 08:46:49 -08002130 fan4min = fan4pin;
2131 } else if (sio_data->kind == w83667hg || sio_data->kind == w83667hg_b) {
2132 fan3pin = 1;
Gong Jun237c8d22009-03-30 21:46:42 +02002133 fan4pin = superio_inb(sio_data->sioreg, 0x27) & 0x40;
Guenter Roeckec3e5a12011-02-02 08:46:49 -08002134 fan5pin = superio_inb(sio_data->sioreg, 0x27) & 0x20;
2135 fan4min = fan4pin;
Gong Jun237c8d22009-03-30 21:46:42 +02002136 } else {
Guenter Roeckec3e5a12011-02-02 08:46:49 -08002137 fan3pin = 1;
Gong Jun237c8d22009-03-30 21:46:42 +02002138 fan4pin = !(superio_inb(sio_data->sioreg, 0x29) & 0x06);
Guenter Roeckec3e5a12011-02-02 08:46:49 -08002139 fan5pin = !(superio_inb(sio_data->sioreg, 0x24) & 0x02);
2140 fan4min = fan4pin;
Gong Jun237c8d22009-03-30 21:46:42 +02002141 }
Ian Dobsond42e8692011-03-07 14:21:12 -08002142
2143 if (fan_debounce &&
2144 (sio_data->kind == nct6775 || sio_data->kind == nct6776)) {
2145 u8 tmp;
2146
2147 superio_select(sio_data->sioreg, W83627EHF_LD_HWM);
2148 tmp = superio_inb(sio_data->sioreg, NCT6775_REG_FAN_DEBOUNCE);
2149 if (sio_data->kind == nct6776)
2150 superio_outb(sio_data->sioreg, NCT6775_REG_FAN_DEBOUNCE,
2151 0x3e | tmp);
2152 else
2153 superio_outb(sio_data->sioreg, NCT6775_REG_FAN_DEBOUNCE,
2154 0x1e | tmp);
2155 pr_info("Enabled fan debounce for chip %s\n", data->name);
2156 }
2157
David Hubbard1ea6dd32007-06-24 11:16:15 +02002158 superio_exit(sio_data->sioreg);
Rudolf Marek08c79952006-07-05 18:14:31 +02002159
Jean Delvare08e7e272005-04-25 22:43:25 +02002160 /* It looks like fan4 and fan5 pins can be alternatively used
Rudolf Marek14992c72006-10-08 22:02:09 +02002161 as fan on/off switches, but fan5 control is write only :/
2162 We assume that if the serial interface is disabled, designers
2163 connected fan5 as input unless they are emitting log 1, which
2164 is not the default. */
Rudolf Marek08c79952006-07-05 18:14:31 +02002165
Guenter Roeckec3e5a12011-02-02 08:46:49 -08002166 data->has_fan = data->has_fan_min = 0x03; /* fan1 and fan2 */
2167
2168 data->has_fan |= (fan3pin << 2);
2169 data->has_fan_min |= (fan3pin << 2);
2170
2171 /*
2172 * NCT6775F and NCT6776F don't have the W83627EHF_REG_FANDIV1 register
2173 */
2174 if (sio_data->kind == nct6775 || sio_data->kind == nct6776) {
2175 data->has_fan |= (fan4pin << 3) | (fan5pin << 4);
2176 data->has_fan_min |= (fan4min << 3) | (fan5pin << 4);
2177 } else {
2178 i = w83627ehf_read_value(data, W83627EHF_REG_FANDIV1);
2179 if ((i & (1 << 2)) && fan4pin) {
2180 data->has_fan |= (1 << 3);
2181 data->has_fan_min |= (1 << 3);
2182 }
2183 if (!(i & (1 << 1)) && fan5pin) {
2184 data->has_fan |= (1 << 4);
2185 data->has_fan_min |= (1 << 4);
2186 }
2187 }
Jean Delvare08e7e272005-04-25 22:43:25 +02002188
Mark M. Hoffmanea7be662007-08-05 12:19:01 -04002189 /* Read fan clock dividers immediately */
Guenter Roeckec3e5a12011-02-02 08:46:49 -08002190 w83627ehf_update_fan_div_common(dev, data);
2191
2192 /* Read pwm data to save original values */
2193 w83627ehf_update_pwm_common(dev, data);
2194 for (i = 0; i < data->pwm_num; i++)
2195 data->pwm_enable_orig[i] = data->pwm_enable[i];
Mark M. Hoffmanea7be662007-08-05 12:19:01 -04002196
Guenter Roeckb84bb512011-02-13 23:01:25 -08002197 /* Read pwm data to save original values */
2198 w83627ehf_update_pwm_common(dev, data);
2199 for (i = 0; i < data->pwm_num; i++)
2200 data->pwm_enable_orig[i] = data->pwm_enable[i];
2201
Jean Delvare08e7e272005-04-25 22:43:25 +02002202 /* Register sysfs hooks */
Guenter Roecke7e1ca62011-02-04 13:24:30 -08002203 for (i = 0; i < ARRAY_SIZE(sda_sf3_arrays); i++) {
2204 err = device_create_file(dev, &sda_sf3_arrays[i].dev_attr);
2205 if (err)
David Hubbardc18beb52006-09-24 21:04:38 +02002206 goto exit_remove;
Guenter Roecke7e1ca62011-02-04 13:24:30 -08002207 }
Rudolf Marek08c79952006-07-05 18:14:31 +02002208
Guenter Roeckda2e0252010-08-14 21:08:55 +02002209 for (i = 0; i < ARRAY_SIZE(sda_sf3_max_step_arrays); i++) {
2210 struct sensor_device_attribute *attr =
2211 &sda_sf3_max_step_arrays[i];
Guenter Roeckec3e5a12011-02-02 08:46:49 -08002212 if (data->REG_FAN_STEP_OUTPUT &&
2213 data->REG_FAN_STEP_OUTPUT[attr->index] != 0xff) {
Guenter Roeckda2e0252010-08-14 21:08:55 +02002214 err = device_create_file(dev, &attr->dev_attr);
2215 if (err)
2216 goto exit_remove;
2217 }
2218 }
Rudolf Marek08c79952006-07-05 18:14:31 +02002219 /* if fan4 is enabled create the sf3 files for it */
Gong Jun237c8d22009-03-30 21:46:42 +02002220 if ((data->has_fan & (1 << 3)) && data->pwm_num >= 4)
David Hubbardc18beb52006-09-24 21:04:38 +02002221 for (i = 0; i < ARRAY_SIZE(sda_sf3_arrays_fan4); i++) {
Guenter Roecke7e1ca62011-02-04 13:24:30 -08002222 err = device_create_file(dev,
2223 &sda_sf3_arrays_fan4[i].dev_attr);
2224 if (err)
David Hubbardc18beb52006-09-24 21:04:38 +02002225 goto exit_remove;
2226 }
Rudolf Marek08c79952006-07-05 18:14:31 +02002227
Gong Juna157d062009-03-30 21:46:43 +02002228 for (i = 0; i < data->in_num; i++) {
2229 if ((i == 6) && data->in6_skip)
2230 continue;
David Hubbardc18beb52006-09-24 21:04:38 +02002231 if ((err = device_create_file(dev, &sda_in_input[i].dev_attr))
2232 || (err = device_create_file(dev,
2233 &sda_in_alarm[i].dev_attr))
2234 || (err = device_create_file(dev,
2235 &sda_in_min[i].dev_attr))
2236 || (err = device_create_file(dev,
2237 &sda_in_max[i].dev_attr)))
2238 goto exit_remove;
Gong Juna157d062009-03-30 21:46:43 +02002239 }
Rudolf Marekcf0676f2006-03-23 16:25:22 +01002240
Yuan Mu412fec82006-02-05 23:24:16 +01002241 for (i = 0; i < 5; i++) {
Rudolf Marek08c79952006-07-05 18:14:31 +02002242 if (data->has_fan & (1 << i)) {
David Hubbardc18beb52006-09-24 21:04:38 +02002243 if ((err = device_create_file(dev,
2244 &sda_fan_input[i].dev_attr))
2245 || (err = device_create_file(dev,
Guenter Roeckec3e5a12011-02-02 08:46:49 -08002246 &sda_fan_alarm[i].dev_attr)))
David Hubbardc18beb52006-09-24 21:04:38 +02002247 goto exit_remove;
Guenter Roeckec3e5a12011-02-02 08:46:49 -08002248 if (sio_data->kind != nct6776) {
2249 err = device_create_file(dev,
2250 &sda_fan_div[i].dev_attr);
2251 if (err)
2252 goto exit_remove;
2253 }
2254 if (data->has_fan_min & (1 << i)) {
2255 err = device_create_file(dev,
2256 &sda_fan_min[i].dev_attr);
2257 if (err)
2258 goto exit_remove;
2259 }
Gong Jun237c8d22009-03-30 21:46:42 +02002260 if (i < data->pwm_num &&
David Hubbardc18beb52006-09-24 21:04:38 +02002261 ((err = device_create_file(dev,
2262 &sda_pwm[i].dev_attr))
2263 || (err = device_create_file(dev,
2264 &sda_pwm_mode[i].dev_attr))
2265 || (err = device_create_file(dev,
2266 &sda_pwm_enable[i].dev_attr))
2267 || (err = device_create_file(dev,
2268 &sda_target_temp[i].dev_attr))
2269 || (err = device_create_file(dev,
2270 &sda_tolerance[i].dev_attr))))
2271 goto exit_remove;
Rudolf Marek08c79952006-07-05 18:14:31 +02002272 }
Jean Delvare08e7e272005-04-25 22:43:25 +02002273 }
Rudolf Marek08c79952006-07-05 18:14:31 +02002274
Guenter Roeckd36cf322011-02-07 15:08:54 -08002275 for (i = 0; i < NUM_REG_TEMP; i++) {
2276 if (!(data->have_temp & (1 << i)))
Gong Juna157d062009-03-30 21:46:43 +02002277 continue;
Guenter Roeckd36cf322011-02-07 15:08:54 -08002278 err = device_create_file(dev, &sda_temp_input[i].dev_attr);
2279 if (err)
2280 goto exit_remove;
2281 if (data->temp_label) {
2282 err = device_create_file(dev,
2283 &sda_temp_label[i].dev_attr);
2284 if (err)
2285 goto exit_remove;
2286 }
Guenter Roeckec3e5a12011-02-02 08:46:49 -08002287 if (data->reg_temp_over[i]) {
2288 err = device_create_file(dev,
2289 &sda_temp_max[i].dev_attr);
2290 if (err)
2291 goto exit_remove;
2292 }
2293 if (data->reg_temp_hyst[i]) {
2294 err = device_create_file(dev,
2295 &sda_temp_max_hyst[i].dev_attr);
2296 if (err)
2297 goto exit_remove;
2298 }
Guenter Roeckd36cf322011-02-07 15:08:54 -08002299 if (i > 2)
Guenter Roeckec3e5a12011-02-02 08:46:49 -08002300 continue;
2301 if ((err = device_create_file(dev,
Gong Juna157d062009-03-30 21:46:43 +02002302 &sda_temp_alarm[i].dev_attr))
2303 || (err = device_create_file(dev,
2304 &sda_temp_type[i].dev_attr)))
David Hubbardc18beb52006-09-24 21:04:38 +02002305 goto exit_remove;
Gong Juna157d062009-03-30 21:46:43 +02002306 }
David Hubbardc18beb52006-09-24 21:04:38 +02002307
David Hubbard1ea6dd32007-06-24 11:16:15 +02002308 err = device_create_file(dev, &dev_attr_name);
2309 if (err)
2310 goto exit_remove;
2311
Tony Jones1beeffe2007-08-20 13:46:20 -07002312 data->hwmon_dev = hwmon_device_register(dev);
2313 if (IS_ERR(data->hwmon_dev)) {
2314 err = PTR_ERR(data->hwmon_dev);
David Hubbardc18beb52006-09-24 21:04:38 +02002315 goto exit_remove;
2316 }
Jean Delvare08e7e272005-04-25 22:43:25 +02002317
2318 return 0;
2319
David Hubbardc18beb52006-09-24 21:04:38 +02002320exit_remove:
2321 w83627ehf_device_remove_files(dev);
Jean Delvare08e7e272005-04-25 22:43:25 +02002322 kfree(data);
David Hubbard1ea6dd32007-06-24 11:16:15 +02002323 platform_set_drvdata(pdev, NULL);
Jean Delvare08e7e272005-04-25 22:43:25 +02002324exit_release:
David Hubbard1ea6dd32007-06-24 11:16:15 +02002325 release_region(res->start, IOREGION_LENGTH);
Jean Delvare08e7e272005-04-25 22:43:25 +02002326exit:
2327 return err;
2328}
2329
David Hubbard1ea6dd32007-06-24 11:16:15 +02002330static int __devexit w83627ehf_remove(struct platform_device *pdev)
Jean Delvare08e7e272005-04-25 22:43:25 +02002331{
David Hubbard1ea6dd32007-06-24 11:16:15 +02002332 struct w83627ehf_data *data = platform_get_drvdata(pdev);
Jean Delvare08e7e272005-04-25 22:43:25 +02002333
Tony Jones1beeffe2007-08-20 13:46:20 -07002334 hwmon_device_unregister(data->hwmon_dev);
David Hubbard1ea6dd32007-06-24 11:16:15 +02002335 w83627ehf_device_remove_files(&pdev->dev);
2336 release_region(data->addr, IOREGION_LENGTH);
2337 platform_set_drvdata(pdev, NULL);
Mark M. Hoffman943b0832005-07-15 21:39:18 -04002338 kfree(data);
Jean Delvare08e7e272005-04-25 22:43:25 +02002339
2340 return 0;
2341}
2342
David Hubbard1ea6dd32007-06-24 11:16:15 +02002343static struct platform_driver w83627ehf_driver = {
Laurent Riffardcdaf7932005-11-26 20:37:41 +01002344 .driver = {
Jean Delvare87218842006-09-03 22:36:14 +02002345 .owner = THIS_MODULE,
David Hubbard1ea6dd32007-06-24 11:16:15 +02002346 .name = DRVNAME,
Laurent Riffardcdaf7932005-11-26 20:37:41 +01002347 },
David Hubbard1ea6dd32007-06-24 11:16:15 +02002348 .probe = w83627ehf_probe,
2349 .remove = __devexit_p(w83627ehf_remove),
Jean Delvare08e7e272005-04-25 22:43:25 +02002350};
2351
David Hubbard1ea6dd32007-06-24 11:16:15 +02002352/* w83627ehf_find() looks for a '627 in the Super-I/O config space */
2353static int __init w83627ehf_find(int sioaddr, unsigned short *addr,
2354 struct w83627ehf_sio_data *sio_data)
Jean Delvare08e7e272005-04-25 22:43:25 +02002355{
David Hubbard1ea6dd32007-06-24 11:16:15 +02002356 static const char __initdata sio_name_W83627EHF[] = "W83627EHF";
2357 static const char __initdata sio_name_W83627EHG[] = "W83627EHG";
2358 static const char __initdata sio_name_W83627DHG[] = "W83627DHG";
Jean Delvarec1e48dc2009-06-15 18:39:50 +02002359 static const char __initdata sio_name_W83627DHG_P[] = "W83627DHG-P";
Gong Jun237c8d22009-03-30 21:46:42 +02002360 static const char __initdata sio_name_W83667HG[] = "W83667HG";
Guenter Roeckc39aeda2010-08-14 21:08:55 +02002361 static const char __initdata sio_name_W83667HG_B[] = "W83667HG-B";
Guenter Roeckec3e5a12011-02-02 08:46:49 -08002362 static const char __initdata sio_name_NCT6775[] = "NCT6775F";
2363 static const char __initdata sio_name_NCT6776[] = "NCT6776F";
David Hubbard1ea6dd32007-06-24 11:16:15 +02002364
Jean Delvare08e7e272005-04-25 22:43:25 +02002365 u16 val;
David Hubbard1ea6dd32007-06-24 11:16:15 +02002366 const char *sio_name;
Jean Delvare08e7e272005-04-25 22:43:25 +02002367
David Hubbard1ea6dd32007-06-24 11:16:15 +02002368 superio_enter(sioaddr);
Jean Delvare08e7e272005-04-25 22:43:25 +02002369
Jean Delvare67b671b2007-12-06 23:13:42 +01002370 if (force_id)
2371 val = force_id;
2372 else
2373 val = (superio_inb(sioaddr, SIO_REG_DEVID) << 8)
2374 | superio_inb(sioaddr, SIO_REG_DEVID + 1);
David Hubbard657c93b2007-02-14 21:15:04 +01002375 switch (val & SIO_ID_MASK) {
David Hubbard657c93b2007-02-14 21:15:04 +01002376 case SIO_W83627EHF_ID:
David Hubbard1ea6dd32007-06-24 11:16:15 +02002377 sio_data->kind = w83627ehf;
2378 sio_name = sio_name_W83627EHF;
2379 break;
David Hubbard657c93b2007-02-14 21:15:04 +01002380 case SIO_W83627EHG_ID:
David Hubbard1ea6dd32007-06-24 11:16:15 +02002381 sio_data->kind = w83627ehf;
2382 sio_name = sio_name_W83627EHG;
2383 break;
2384 case SIO_W83627DHG_ID:
2385 sio_data->kind = w83627dhg;
2386 sio_name = sio_name_W83627DHG;
David Hubbard657c93b2007-02-14 21:15:04 +01002387 break;
Jean Delvarec1e48dc2009-06-15 18:39:50 +02002388 case SIO_W83627DHG_P_ID:
2389 sio_data->kind = w83627dhg_p;
2390 sio_name = sio_name_W83627DHG_P;
2391 break;
Gong Jun237c8d22009-03-30 21:46:42 +02002392 case SIO_W83667HG_ID:
2393 sio_data->kind = w83667hg;
2394 sio_name = sio_name_W83667HG;
2395 break;
Guenter Roeckc39aeda2010-08-14 21:08:55 +02002396 case SIO_W83667HG_B_ID:
2397 sio_data->kind = w83667hg_b;
2398 sio_name = sio_name_W83667HG_B;
2399 break;
Guenter Roeckec3e5a12011-02-02 08:46:49 -08002400 case SIO_NCT6775_ID:
2401 sio_data->kind = nct6775;
2402 sio_name = sio_name_NCT6775;
2403 break;
2404 case SIO_NCT6776_ID:
2405 sio_data->kind = nct6776;
2406 sio_name = sio_name_NCT6776;
2407 break;
David Hubbard657c93b2007-02-14 21:15:04 +01002408 default:
Jean Delvare9f660362007-06-24 11:23:41 +02002409 if (val != 0xffff)
Joe Perchesabdc6fd2010-10-20 06:51:54 +00002410 pr_debug("unsupported chip ID: 0x%04x\n", val);
David Hubbard1ea6dd32007-06-24 11:16:15 +02002411 superio_exit(sioaddr);
Jean Delvare08e7e272005-04-25 22:43:25 +02002412 return -ENODEV;
2413 }
2414
David Hubbard1ea6dd32007-06-24 11:16:15 +02002415 /* We have a known chip, find the HWM I/O address */
2416 superio_select(sioaddr, W83627EHF_LD_HWM);
2417 val = (superio_inb(sioaddr, SIO_REG_ADDR) << 8)
2418 | superio_inb(sioaddr, SIO_REG_ADDR + 1);
Jean Delvare1a641fc2007-04-23 14:41:16 -07002419 *addr = val & IOREGION_ALIGNMENT;
Jean Delvare2d8672c2005-07-19 23:56:35 +02002420 if (*addr == 0) {
Joe Perchesabdc6fd2010-10-20 06:51:54 +00002421 pr_err("Refusing to enable a Super-I/O device with a base I/O port 0\n");
David Hubbard1ea6dd32007-06-24 11:16:15 +02002422 superio_exit(sioaddr);
Jean Delvare08e7e272005-04-25 22:43:25 +02002423 return -ENODEV;
2424 }
2425
2426 /* Activate logical device if needed */
David Hubbard1ea6dd32007-06-24 11:16:15 +02002427 val = superio_inb(sioaddr, SIO_REG_ENABLE);
David Hubbard475ef852007-06-24 11:17:09 +02002428 if (!(val & 0x01)) {
Guenter Roecke7e1ca62011-02-04 13:24:30 -08002429 pr_warn("Forcibly enabling Super-I/O. "
2430 "Sensor is probably unusable.\n");
David Hubbard1ea6dd32007-06-24 11:16:15 +02002431 superio_outb(sioaddr, SIO_REG_ENABLE, val | 0x01);
David Hubbard475ef852007-06-24 11:17:09 +02002432 }
Jean Delvare08e7e272005-04-25 22:43:25 +02002433
David Hubbard1ea6dd32007-06-24 11:16:15 +02002434 superio_exit(sioaddr);
Joe Perchesabdc6fd2010-10-20 06:51:54 +00002435 pr_info("Found %s chip at %#x\n", sio_name, *addr);
David Hubbard1ea6dd32007-06-24 11:16:15 +02002436 sio_data->sioreg = sioaddr;
2437
Jean Delvare08e7e272005-04-25 22:43:25 +02002438 return 0;
2439}
2440
David Hubbard1ea6dd32007-06-24 11:16:15 +02002441/* when Super-I/O functions move to a separate file, the Super-I/O
2442 * bus will manage the lifetime of the device and this module will only keep
2443 * track of the w83627ehf driver. But since we platform_device_alloc(), we
2444 * must keep track of the device */
2445static struct platform_device *pdev;
2446
Jean Delvare08e7e272005-04-25 22:43:25 +02002447static int __init sensors_w83627ehf_init(void)
2448{
David Hubbard1ea6dd32007-06-24 11:16:15 +02002449 int err;
2450 unsigned short address;
2451 struct resource res;
2452 struct w83627ehf_sio_data sio_data;
2453
2454 /* initialize sio_data->kind and sio_data->sioreg.
2455 *
2456 * when Super-I/O functions move to a separate file, the Super-I/O
2457 * driver will probe 0x2e and 0x4e and auto-detect the presence of a
2458 * w83627ehf hardware monitor, and call probe() */
2459 if (w83627ehf_find(0x2e, &address, &sio_data) &&
2460 w83627ehf_find(0x4e, &address, &sio_data))
Jean Delvare08e7e272005-04-25 22:43:25 +02002461 return -ENODEV;
2462
David Hubbard1ea6dd32007-06-24 11:16:15 +02002463 err = platform_driver_register(&w83627ehf_driver);
2464 if (err)
2465 goto exit;
2466
Guenter Roecke7e1ca62011-02-04 13:24:30 -08002467 pdev = platform_device_alloc(DRVNAME, address);
2468 if (!pdev) {
David Hubbard1ea6dd32007-06-24 11:16:15 +02002469 err = -ENOMEM;
Joe Perchesabdc6fd2010-10-20 06:51:54 +00002470 pr_err("Device allocation failed\n");
David Hubbard1ea6dd32007-06-24 11:16:15 +02002471 goto exit_unregister;
2472 }
2473
2474 err = platform_device_add_data(pdev, &sio_data,
2475 sizeof(struct w83627ehf_sio_data));
2476 if (err) {
Joe Perchesabdc6fd2010-10-20 06:51:54 +00002477 pr_err("Platform data allocation failed\n");
David Hubbard1ea6dd32007-06-24 11:16:15 +02002478 goto exit_device_put;
2479 }
2480
2481 memset(&res, 0, sizeof(res));
2482 res.name = DRVNAME;
2483 res.start = address + IOREGION_OFFSET;
2484 res.end = address + IOREGION_OFFSET + IOREGION_LENGTH - 1;
2485 res.flags = IORESOURCE_IO;
Jean Delvareb9acb642009-01-07 16:37:35 +01002486
2487 err = acpi_check_resource_conflict(&res);
2488 if (err)
Hans de Goede18632f82009-02-17 19:59:54 +01002489 goto exit_device_put;
Jean Delvareb9acb642009-01-07 16:37:35 +01002490
David Hubbard1ea6dd32007-06-24 11:16:15 +02002491 err = platform_device_add_resources(pdev, &res, 1);
2492 if (err) {
Joe Perchesabdc6fd2010-10-20 06:51:54 +00002493 pr_err("Device resource addition failed (%d)\n", err);
David Hubbard1ea6dd32007-06-24 11:16:15 +02002494 goto exit_device_put;
2495 }
2496
2497 /* platform_device_add calls probe() */
2498 err = platform_device_add(pdev);
2499 if (err) {
Joe Perchesabdc6fd2010-10-20 06:51:54 +00002500 pr_err("Device addition failed (%d)\n", err);
David Hubbard1ea6dd32007-06-24 11:16:15 +02002501 goto exit_device_put;
2502 }
2503
2504 return 0;
2505
2506exit_device_put:
2507 platform_device_put(pdev);
2508exit_unregister:
2509 platform_driver_unregister(&w83627ehf_driver);
2510exit:
2511 return err;
Jean Delvare08e7e272005-04-25 22:43:25 +02002512}
2513
2514static void __exit sensors_w83627ehf_exit(void)
2515{
David Hubbard1ea6dd32007-06-24 11:16:15 +02002516 platform_device_unregister(pdev);
2517 platform_driver_unregister(&w83627ehf_driver);
Jean Delvare08e7e272005-04-25 22:43:25 +02002518}
2519
2520MODULE_AUTHOR("Jean Delvare <khali@linux-fr.org>");
2521MODULE_DESCRIPTION("W83627EHF driver");
2522MODULE_LICENSE("GPL");
2523
2524module_init(sensors_w83627ehf_init);
2525module_exit(sensors_w83627ehf_exit);