blob: 7b0260dc76fbac2df46ed55f6a0e8e78848db131 [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);
393 return 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);
401 return DIV_ROUND_CLOSEST(SENSORS_LIMIT(temp, -127000, 128000), 1000);
Jean Delvare08e7e272005-04-25 22:43:25 +0200402}
403
Rudolf Marekcf0676f2006-03-23 16:25:22 +0100404/* Some of analog inputs have internal scaling (2x), 8mV is ADC LSB */
405
406static u8 scale_in[10] = { 8, 8, 16, 16, 8, 8, 8, 16, 16, 8 };
407
408static inline long in_from_reg(u8 reg, u8 nr)
409{
410 return reg * scale_in[nr];
411}
412
413static inline u8 in_to_reg(u32 val, u8 nr)
414{
Guenter Roecke7e1ca62011-02-04 13:24:30 -0800415 return SENSORS_LIMIT(((val + (scale_in[nr] / 2)) / scale_in[nr]), 0,
416 255);
Rudolf Marekcf0676f2006-03-23 16:25:22 +0100417}
418
Jean Delvare08e7e272005-04-25 22:43:25 +0200419/*
420 * Data structures and manipulation thereof
421 */
422
423struct w83627ehf_data {
David Hubbard1ea6dd32007-06-24 11:16:15 +0200424 int addr; /* IO base of hw monitor block */
425 const char *name;
426
Tony Jones1beeffe2007-08-20 13:46:20 -0700427 struct device *hwmon_dev;
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100428 struct mutex lock;
Jean Delvare08e7e272005-04-25 22:43:25 +0200429
Guenter Roeckec3e5a12011-02-02 08:46:49 -0800430 u16 reg_temp[NUM_REG_TEMP];
431 u16 reg_temp_over[NUM_REG_TEMP];
432 u16 reg_temp_hyst[NUM_REG_TEMP];
433 u16 reg_temp_config[NUM_REG_TEMP];
Guenter Roeckd36cf322011-02-07 15:08:54 -0800434 u8 temp_src[NUM_REG_TEMP];
435 const char * const *temp_label;
436
Guenter Roeck279af1a2011-02-13 22:34:47 -0800437 const u16 *REG_PWM;
438 const u16 *REG_TARGET;
439 const u16 *REG_FAN;
440 const u16 *REG_FAN_MIN;
441 const u16 *REG_FAN_START_OUTPUT;
442 const u16 *REG_FAN_STOP_OUTPUT;
443 const u16 *REG_FAN_STOP_TIME;
444 const u16 *REG_FAN_MAX_OUTPUT;
445 const u16 *REG_FAN_STEP_OUTPUT;
Guenter Roeckda2e0252010-08-14 21:08:55 +0200446
Guenter Roeck26bc4402011-02-11 08:00:58 -0800447 unsigned int (*fan_from_reg)(u16 reg, unsigned int divreg);
448 unsigned int (*fan_from_reg_min)(u16 reg, unsigned int divreg);
449
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100450 struct mutex update_lock;
Jean Delvare08e7e272005-04-25 22:43:25 +0200451 char valid; /* !=0 if following fields are valid */
452 unsigned long last_updated; /* In jiffies */
453
454 /* Register values */
Guenter Roeck83cc8982011-02-06 08:10:15 -0800455 u8 bank; /* current register bank */
David Hubbard1ea6dd32007-06-24 11:16:15 +0200456 u8 in_num; /* number of in inputs we have */
Rudolf Marekcf0676f2006-03-23 16:25:22 +0100457 u8 in[10]; /* Register value */
458 u8 in_max[10]; /* Register value */
459 u8 in_min[10]; /* Register value */
Guenter Roeck3382a912011-02-13 13:08:23 -0800460 unsigned int rpm[5];
Guenter Roeckec3e5a12011-02-02 08:46:49 -0800461 u16 fan_min[5];
Jean Delvare08e7e272005-04-25 22:43:25 +0200462 u8 fan_div[5];
463 u8 has_fan; /* some fan inputs can be disabled */
Guenter Roeckec3e5a12011-02-02 08:46:49 -0800464 u8 has_fan_min; /* some fans don't have min register */
Guenter Roeck26bc4402011-02-11 08:00:58 -0800465 bool has_fan_div;
Jean Delvareda667362007-06-24 11:21:02 +0200466 u8 temp_type[3];
Guenter Roeckec3e5a12011-02-02 08:46:49 -0800467 s16 temp[9];
468 s16 temp_max[9];
469 s16 temp_max_hyst[9];
Jean Delvarea4589db2006-03-23 16:30:29 +0100470 u32 alarms;
Rudolf Marek08c79952006-07-05 18:14:31 +0200471
472 u8 pwm_mode[4]; /* 0->DC variable voltage, 1->PWM variable duty cycle */
473 u8 pwm_enable[4]; /* 1->manual
Daniel J Blueman41e9a062009-12-14 18:01:37 -0800474 2->thermal cruise mode (also called SmartFan I)
475 3->fan speed cruise mode
Guenter Roecke7e1ca62011-02-04 13:24:30 -0800476 4->variable thermal cruise (also called
Guenter Roeckb84bb512011-02-13 23:01:25 -0800477 SmartFan III)
478 5->enhanced variable thermal cruise (also called
479 SmartFan IV) */
480 u8 pwm_enable_orig[4]; /* original value of pwm_enable */
Gong Jun237c8d22009-03-30 21:46:42 +0200481 u8 pwm_num; /* number of pwm */
Rudolf Marek08c79952006-07-05 18:14:31 +0200482 u8 pwm[4];
483 u8 target_temp[4];
484 u8 tolerance[4];
485
Daniel J Blueman41e9a062009-12-14 18:01:37 -0800486 u8 fan_start_output[4]; /* minimum fan speed when spinning up */
487 u8 fan_stop_output[4]; /* minimum fan speed when spinning down */
488 u8 fan_stop_time[4]; /* time at minimum before disabling fan */
489 u8 fan_max_output[4]; /* maximum fan speed */
490 u8 fan_step_output[4]; /* rate of change output value */
Jean Delvarefc18d6c2007-06-24 11:19:42 +0200491
492 u8 vid;
493 u8 vrm;
Gong Juna157d062009-03-30 21:46:43 +0200494
Guenter Roeckec3e5a12011-02-02 08:46:49 -0800495 u16 have_temp;
Gong Juna157d062009-03-30 21:46:43 +0200496 u8 in6_skip;
Jean Delvare08e7e272005-04-25 22:43:25 +0200497};
498
David Hubbard1ea6dd32007-06-24 11:16:15 +0200499struct w83627ehf_sio_data {
500 int sioreg;
501 enum kinds kind;
502};
503
Guenter Roeck83cc8982011-02-06 08:10:15 -0800504/*
505 * On older chips, only registers 0x50-0x5f are banked.
506 * On more recent chips, all registers are banked.
507 * Assume that is the case and set the bank number for each access.
508 * Cache the bank number so it only needs to be set if it changes.
509 */
David Hubbard1ea6dd32007-06-24 11:16:15 +0200510static inline void w83627ehf_set_bank(struct w83627ehf_data *data, u16 reg)
Jean Delvare08e7e272005-04-25 22:43:25 +0200511{
Guenter Roeck83cc8982011-02-06 08:10:15 -0800512 u8 bank = reg >> 8;
513 if (data->bank != bank) {
David Hubbard1ea6dd32007-06-24 11:16:15 +0200514 outb_p(W83627EHF_REG_BANK, data->addr + ADDR_REG_OFFSET);
Guenter Roeck83cc8982011-02-06 08:10:15 -0800515 outb_p(bank, data->addr + DATA_REG_OFFSET);
516 data->bank = bank;
Jean Delvare08e7e272005-04-25 22:43:25 +0200517 }
518}
519
David Hubbard1ea6dd32007-06-24 11:16:15 +0200520static u16 w83627ehf_read_value(struct w83627ehf_data *data, u16 reg)
Jean Delvare08e7e272005-04-25 22:43:25 +0200521{
Jean Delvare08e7e272005-04-25 22:43:25 +0200522 int res, word_sized = is_word_sized(reg);
523
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100524 mutex_lock(&data->lock);
Jean Delvare08e7e272005-04-25 22:43:25 +0200525
David Hubbard1ea6dd32007-06-24 11:16:15 +0200526 w83627ehf_set_bank(data, reg);
527 outb_p(reg & 0xff, data->addr + ADDR_REG_OFFSET);
528 res = inb_p(data->addr + DATA_REG_OFFSET);
Jean Delvare08e7e272005-04-25 22:43:25 +0200529 if (word_sized) {
530 outb_p((reg & 0xff) + 1,
David Hubbard1ea6dd32007-06-24 11:16:15 +0200531 data->addr + ADDR_REG_OFFSET);
532 res = (res << 8) + inb_p(data->addr + DATA_REG_OFFSET);
Jean Delvare08e7e272005-04-25 22:43:25 +0200533 }
Jean Delvare08e7e272005-04-25 22:43:25 +0200534
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100535 mutex_unlock(&data->lock);
Jean Delvare08e7e272005-04-25 22:43:25 +0200536 return res;
537}
538
Guenter Roecke7e1ca62011-02-04 13:24:30 -0800539static int w83627ehf_write_value(struct w83627ehf_data *data, u16 reg,
540 u16 value)
Jean Delvare08e7e272005-04-25 22:43:25 +0200541{
Jean Delvare08e7e272005-04-25 22:43:25 +0200542 int word_sized = is_word_sized(reg);
543
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100544 mutex_lock(&data->lock);
Jean Delvare08e7e272005-04-25 22:43:25 +0200545
David Hubbard1ea6dd32007-06-24 11:16:15 +0200546 w83627ehf_set_bank(data, reg);
547 outb_p(reg & 0xff, data->addr + ADDR_REG_OFFSET);
Jean Delvare08e7e272005-04-25 22:43:25 +0200548 if (word_sized) {
David Hubbard1ea6dd32007-06-24 11:16:15 +0200549 outb_p(value >> 8, data->addr + DATA_REG_OFFSET);
Jean Delvare08e7e272005-04-25 22:43:25 +0200550 outb_p((reg & 0xff) + 1,
David Hubbard1ea6dd32007-06-24 11:16:15 +0200551 data->addr + ADDR_REG_OFFSET);
Jean Delvare08e7e272005-04-25 22:43:25 +0200552 }
David Hubbard1ea6dd32007-06-24 11:16:15 +0200553 outb_p(value & 0xff, data->addr + DATA_REG_OFFSET);
Jean Delvare08e7e272005-04-25 22:43:25 +0200554
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100555 mutex_unlock(&data->lock);
Jean Delvare08e7e272005-04-25 22:43:25 +0200556 return 0;
557}
558
559/* This function assumes that the caller holds data->update_lock */
Guenter Roeckec3e5a12011-02-02 08:46:49 -0800560static void nct6775_write_fan_div(struct w83627ehf_data *data, int nr)
561{
562 u8 reg;
563
564 switch (nr) {
565 case 0:
566 reg = (w83627ehf_read_value(data, NCT6775_REG_FANDIV1) & 0x70)
567 | (data->fan_div[0] & 0x7);
568 w83627ehf_write_value(data, NCT6775_REG_FANDIV1, reg);
569 break;
570 case 1:
571 reg = (w83627ehf_read_value(data, NCT6775_REG_FANDIV1) & 0x7)
572 | ((data->fan_div[1] << 4) & 0x70);
573 w83627ehf_write_value(data, NCT6775_REG_FANDIV1, reg);
574 case 2:
575 reg = (w83627ehf_read_value(data, NCT6775_REG_FANDIV2) & 0x70)
576 | (data->fan_div[2] & 0x7);
577 w83627ehf_write_value(data, NCT6775_REG_FANDIV2, reg);
578 break;
579 case 3:
580 reg = (w83627ehf_read_value(data, NCT6775_REG_FANDIV2) & 0x7)
581 | ((data->fan_div[3] << 4) & 0x70);
582 w83627ehf_write_value(data, NCT6775_REG_FANDIV2, reg);
583 break;
584 }
585}
586
587/* This function assumes that the caller holds data->update_lock */
David Hubbard1ea6dd32007-06-24 11:16:15 +0200588static void w83627ehf_write_fan_div(struct w83627ehf_data *data, int nr)
Jean Delvare08e7e272005-04-25 22:43:25 +0200589{
Jean Delvare08e7e272005-04-25 22:43:25 +0200590 u8 reg;
591
592 switch (nr) {
593 case 0:
David Hubbard1ea6dd32007-06-24 11:16:15 +0200594 reg = (w83627ehf_read_value(data, W83627EHF_REG_FANDIV1) & 0xcf)
Jean Delvare08e7e272005-04-25 22:43:25 +0200595 | ((data->fan_div[0] & 0x03) << 4);
Rudolf Marek14992c72006-10-08 22:02:09 +0200596 /* fan5 input control bit is write only, compute the value */
597 reg |= (data->has_fan & (1 << 4)) ? 1 : 0;
David Hubbard1ea6dd32007-06-24 11:16:15 +0200598 w83627ehf_write_value(data, W83627EHF_REG_FANDIV1, reg);
599 reg = (w83627ehf_read_value(data, W83627EHF_REG_VBAT) & 0xdf)
Jean Delvare08e7e272005-04-25 22:43:25 +0200600 | ((data->fan_div[0] & 0x04) << 3);
David Hubbard1ea6dd32007-06-24 11:16:15 +0200601 w83627ehf_write_value(data, W83627EHF_REG_VBAT, reg);
Jean Delvare08e7e272005-04-25 22:43:25 +0200602 break;
603 case 1:
David Hubbard1ea6dd32007-06-24 11:16:15 +0200604 reg = (w83627ehf_read_value(data, W83627EHF_REG_FANDIV1) & 0x3f)
Jean Delvare08e7e272005-04-25 22:43:25 +0200605 | ((data->fan_div[1] & 0x03) << 6);
Rudolf Marek14992c72006-10-08 22:02:09 +0200606 /* fan5 input control bit is write only, compute the value */
607 reg |= (data->has_fan & (1 << 4)) ? 1 : 0;
David Hubbard1ea6dd32007-06-24 11:16:15 +0200608 w83627ehf_write_value(data, W83627EHF_REG_FANDIV1, reg);
609 reg = (w83627ehf_read_value(data, W83627EHF_REG_VBAT) & 0xbf)
Jean Delvare08e7e272005-04-25 22:43:25 +0200610 | ((data->fan_div[1] & 0x04) << 4);
David Hubbard1ea6dd32007-06-24 11:16:15 +0200611 w83627ehf_write_value(data, W83627EHF_REG_VBAT, reg);
Jean Delvare08e7e272005-04-25 22:43:25 +0200612 break;
613 case 2:
David Hubbard1ea6dd32007-06-24 11:16:15 +0200614 reg = (w83627ehf_read_value(data, W83627EHF_REG_FANDIV2) & 0x3f)
Jean Delvare08e7e272005-04-25 22:43:25 +0200615 | ((data->fan_div[2] & 0x03) << 6);
David Hubbard1ea6dd32007-06-24 11:16:15 +0200616 w83627ehf_write_value(data, W83627EHF_REG_FANDIV2, reg);
617 reg = (w83627ehf_read_value(data, W83627EHF_REG_VBAT) & 0x7f)
Jean Delvare08e7e272005-04-25 22:43:25 +0200618 | ((data->fan_div[2] & 0x04) << 5);
David Hubbard1ea6dd32007-06-24 11:16:15 +0200619 w83627ehf_write_value(data, W83627EHF_REG_VBAT, reg);
Jean Delvare08e7e272005-04-25 22:43:25 +0200620 break;
621 case 3:
David Hubbard1ea6dd32007-06-24 11:16:15 +0200622 reg = (w83627ehf_read_value(data, W83627EHF_REG_DIODE) & 0xfc)
Jean Delvare08e7e272005-04-25 22:43:25 +0200623 | (data->fan_div[3] & 0x03);
David Hubbard1ea6dd32007-06-24 11:16:15 +0200624 w83627ehf_write_value(data, W83627EHF_REG_DIODE, reg);
625 reg = (w83627ehf_read_value(data, W83627EHF_REG_SMI_OVT) & 0x7f)
Jean Delvare08e7e272005-04-25 22:43:25 +0200626 | ((data->fan_div[3] & 0x04) << 5);
David Hubbard1ea6dd32007-06-24 11:16:15 +0200627 w83627ehf_write_value(data, W83627EHF_REG_SMI_OVT, reg);
Jean Delvare08e7e272005-04-25 22:43:25 +0200628 break;
629 case 4:
David Hubbard1ea6dd32007-06-24 11:16:15 +0200630 reg = (w83627ehf_read_value(data, W83627EHF_REG_DIODE) & 0x73)
Jean Delvare33725ad2007-04-17 00:32:27 -0700631 | ((data->fan_div[4] & 0x03) << 2)
Jean Delvare08e7e272005-04-25 22:43:25 +0200632 | ((data->fan_div[4] & 0x04) << 5);
David Hubbard1ea6dd32007-06-24 11:16:15 +0200633 w83627ehf_write_value(data, W83627EHF_REG_DIODE, reg);
Jean Delvare08e7e272005-04-25 22:43:25 +0200634 break;
635 }
636}
637
Guenter Roeckec3e5a12011-02-02 08:46:49 -0800638static void w83627ehf_write_fan_div_common(struct device *dev,
639 struct w83627ehf_data *data, int nr)
640{
641 struct w83627ehf_sio_data *sio_data = dev->platform_data;
642
643 if (sio_data->kind == nct6776)
644 ; /* no dividers, do nothing */
645 else if (sio_data->kind == nct6775)
646 nct6775_write_fan_div(data, nr);
647 else
648 w83627ehf_write_fan_div(data, nr);
649}
650
651static void nct6775_update_fan_div(struct w83627ehf_data *data)
652{
653 u8 i;
654
655 i = w83627ehf_read_value(data, NCT6775_REG_FANDIV1);
656 data->fan_div[0] = i & 0x7;
657 data->fan_div[1] = (i & 0x70) >> 4;
658 i = w83627ehf_read_value(data, NCT6775_REG_FANDIV2);
659 data->fan_div[2] = i & 0x7;
660 if (data->has_fan & (1<<3))
661 data->fan_div[3] = (i & 0x70) >> 4;
662}
663
Mark M. Hoffmanea7be662007-08-05 12:19:01 -0400664static void w83627ehf_update_fan_div(struct w83627ehf_data *data)
665{
666 int i;
667
668 i = w83627ehf_read_value(data, W83627EHF_REG_FANDIV1);
669 data->fan_div[0] = (i >> 4) & 0x03;
670 data->fan_div[1] = (i >> 6) & 0x03;
671 i = w83627ehf_read_value(data, W83627EHF_REG_FANDIV2);
672 data->fan_div[2] = (i >> 6) & 0x03;
673 i = w83627ehf_read_value(data, W83627EHF_REG_VBAT);
674 data->fan_div[0] |= (i >> 3) & 0x04;
675 data->fan_div[1] |= (i >> 4) & 0x04;
676 data->fan_div[2] |= (i >> 5) & 0x04;
677 if (data->has_fan & ((1 << 3) | (1 << 4))) {
678 i = w83627ehf_read_value(data, W83627EHF_REG_DIODE);
679 data->fan_div[3] = i & 0x03;
680 data->fan_div[4] = ((i >> 2) & 0x03)
681 | ((i >> 5) & 0x04);
682 }
683 if (data->has_fan & (1 << 3)) {
684 i = w83627ehf_read_value(data, W83627EHF_REG_SMI_OVT);
685 data->fan_div[3] |= (i >> 5) & 0x04;
686 }
687}
688
Guenter Roeckec3e5a12011-02-02 08:46:49 -0800689static void w83627ehf_update_fan_div_common(struct device *dev,
690 struct w83627ehf_data *data)
691{
692 struct w83627ehf_sio_data *sio_data = dev->platform_data;
693
694 if (sio_data->kind == nct6776)
695 ; /* no dividers, do nothing */
696 else if (sio_data->kind == nct6775)
697 nct6775_update_fan_div(data);
698 else
699 w83627ehf_update_fan_div(data);
700}
701
702static void nct6775_update_pwm(struct w83627ehf_data *data)
703{
704 int i;
705 int pwmcfg, fanmodecfg;
706
707 for (i = 0; i < data->pwm_num; i++) {
708 pwmcfg = w83627ehf_read_value(data,
709 W83627EHF_REG_PWM_ENABLE[i]);
710 fanmodecfg = w83627ehf_read_value(data,
711 NCT6775_REG_FAN_MODE[i]);
712 data->pwm_mode[i] =
713 ((pwmcfg >> W83627EHF_PWM_MODE_SHIFT[i]) & 1) ? 0 : 1;
714 data->pwm_enable[i] = ((fanmodecfg >> 4) & 7) + 1;
715 data->tolerance[i] = fanmodecfg & 0x0f;
716 data->pwm[i] = w83627ehf_read_value(data, data->REG_PWM[i]);
717 }
718}
719
720static void w83627ehf_update_pwm(struct w83627ehf_data *data)
721{
722 int i;
723 int pwmcfg = 0, tolerance = 0; /* shut up the compiler */
724
725 for (i = 0; i < data->pwm_num; i++) {
726 if (!(data->has_fan & (1 << i)))
727 continue;
728
729 /* pwmcfg, tolerance mapped for i=0, i=1 to same reg */
730 if (i != 1) {
731 pwmcfg = w83627ehf_read_value(data,
732 W83627EHF_REG_PWM_ENABLE[i]);
733 tolerance = w83627ehf_read_value(data,
734 W83627EHF_REG_TOLERANCE[i]);
735 }
736 data->pwm_mode[i] =
737 ((pwmcfg >> W83627EHF_PWM_MODE_SHIFT[i]) & 1) ? 0 : 1;
738 data->pwm_enable[i] = ((pwmcfg >> W83627EHF_PWM_ENABLE_SHIFT[i])
739 & 3) + 1;
740 data->pwm[i] = w83627ehf_read_value(data, data->REG_PWM[i]);
741
742 data->tolerance[i] = (tolerance >> (i == 1 ? 4 : 0)) & 0x0f;
743 }
744}
745
746static void w83627ehf_update_pwm_common(struct device *dev,
747 struct w83627ehf_data *data)
748{
749 struct w83627ehf_sio_data *sio_data = dev->platform_data;
750
751 if (sio_data->kind == nct6775 || sio_data->kind == nct6776)
752 nct6775_update_pwm(data);
753 else
754 w83627ehf_update_pwm(data);
755}
756
Jean Delvare08e7e272005-04-25 22:43:25 +0200757static struct w83627ehf_data *w83627ehf_update_device(struct device *dev)
758{
David Hubbard1ea6dd32007-06-24 11:16:15 +0200759 struct w83627ehf_data *data = dev_get_drvdata(dev);
Guenter Roeckec3e5a12011-02-02 08:46:49 -0800760 struct w83627ehf_sio_data *sio_data = dev->platform_data;
761
Jean Delvare08e7e272005-04-25 22:43:25 +0200762 int i;
763
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100764 mutex_lock(&data->update_lock);
Jean Delvare08e7e272005-04-25 22:43:25 +0200765
Jean Delvare6b3e4642007-06-24 11:19:01 +0200766 if (time_after(jiffies, data->last_updated + HZ + HZ/2)
Jean Delvare08e7e272005-04-25 22:43:25 +0200767 || !data->valid) {
768 /* Fan clock dividers */
Guenter Roeckec3e5a12011-02-02 08:46:49 -0800769 w83627ehf_update_fan_div_common(dev, data);
Jean Delvare08e7e272005-04-25 22:43:25 +0200770
Rudolf Marekcf0676f2006-03-23 16:25:22 +0100771 /* Measured voltages and limits */
David Hubbard1ea6dd32007-06-24 11:16:15 +0200772 for (i = 0; i < data->in_num; i++) {
773 data->in[i] = w83627ehf_read_value(data,
Rudolf Marekcf0676f2006-03-23 16:25:22 +0100774 W83627EHF_REG_IN(i));
David Hubbard1ea6dd32007-06-24 11:16:15 +0200775 data->in_min[i] = w83627ehf_read_value(data,
Rudolf Marekcf0676f2006-03-23 16:25:22 +0100776 W83627EHF_REG_IN_MIN(i));
David Hubbard1ea6dd32007-06-24 11:16:15 +0200777 data->in_max[i] = w83627ehf_read_value(data,
Rudolf Marekcf0676f2006-03-23 16:25:22 +0100778 W83627EHF_REG_IN_MAX(i));
779 }
780
Jean Delvare08e7e272005-04-25 22:43:25 +0200781 /* Measured fan speeds and limits */
782 for (i = 0; i < 5; i++) {
Guenter Roeck3382a912011-02-13 13:08:23 -0800783 u16 reg;
784
Jean Delvare08e7e272005-04-25 22:43:25 +0200785 if (!(data->has_fan & (1 << i)))
786 continue;
787
Guenter Roeck3382a912011-02-13 13:08:23 -0800788 reg = w83627ehf_read_value(data, data->REG_FAN[i]);
789 data->rpm[i] = data->fan_from_reg(reg,
790 data->fan_div[i]);
Guenter Roeckec3e5a12011-02-02 08:46:49 -0800791
792 if (data->has_fan_min & (1 << i))
793 data->fan_min[i] = w83627ehf_read_value(data,
Guenter Roeck279af1a2011-02-13 22:34:47 -0800794 data->REG_FAN_MIN[i]);
Jean Delvare08e7e272005-04-25 22:43:25 +0200795
796 /* If we failed to measure the fan speed and clock
797 divider can be increased, let's try that for next
798 time */
Guenter Roeck26bc4402011-02-11 08:00:58 -0800799 if (data->has_fan_div
Guenter Roeck3382a912011-02-13 13:08:23 -0800800 && (reg >= 0xff || (sio_data->kind == nct6775
801 && reg == 0x00))
Guenter Roeckec3e5a12011-02-02 08:46:49 -0800802 && data->fan_div[i] < 0x07) {
Guenter Roecke7e1ca62011-02-04 13:24:30 -0800803 dev_dbg(dev, "Increasing fan%d "
Jean Delvare08e7e272005-04-25 22:43:25 +0200804 "clock divider from %u to %u\n",
Jean Delvare33725ad2007-04-17 00:32:27 -0700805 i + 1, div_from_reg(data->fan_div[i]),
Jean Delvare08e7e272005-04-25 22:43:25 +0200806 div_from_reg(data->fan_div[i] + 1));
807 data->fan_div[i]++;
Guenter Roeckec3e5a12011-02-02 08:46:49 -0800808 w83627ehf_write_fan_div_common(dev, data, i);
Jean Delvare08e7e272005-04-25 22:43:25 +0200809 /* Preserve min limit if possible */
Guenter Roeckec3e5a12011-02-02 08:46:49 -0800810 if ((data->has_fan_min & (1 << i))
811 && data->fan_min[i] >= 2
Jean Delvare08e7e272005-04-25 22:43:25 +0200812 && data->fan_min[i] != 255)
David Hubbard1ea6dd32007-06-24 11:16:15 +0200813 w83627ehf_write_value(data,
Guenter Roeck279af1a2011-02-13 22:34:47 -0800814 data->REG_FAN_MIN[i],
Jean Delvare08e7e272005-04-25 22:43:25 +0200815 (data->fan_min[i] /= 2));
816 }
817 }
818
Guenter Roeckec3e5a12011-02-02 08:46:49 -0800819 w83627ehf_update_pwm_common(dev, data);
820
Guenter Roeckda2e0252010-08-14 21:08:55 +0200821 for (i = 0; i < data->pwm_num; i++) {
822 if (!(data->has_fan & (1 << i)))
823 continue;
824
Guenter Roeckec3e5a12011-02-02 08:46:49 -0800825 data->fan_start_output[i] =
826 w83627ehf_read_value(data,
827 data->REG_FAN_START_OUTPUT[i]);
828 data->fan_stop_output[i] =
829 w83627ehf_read_value(data,
830 data->REG_FAN_STOP_OUTPUT[i]);
831 data->fan_stop_time[i] =
832 w83627ehf_read_value(data,
833 data->REG_FAN_STOP_TIME[i]);
Guenter Roeckda2e0252010-08-14 21:08:55 +0200834
Guenter Roeckec3e5a12011-02-02 08:46:49 -0800835 if (data->REG_FAN_MAX_OUTPUT &&
836 data->REG_FAN_MAX_OUTPUT[i] != 0xff)
Guenter Roeckda2e0252010-08-14 21:08:55 +0200837 data->fan_max_output[i] =
838 w83627ehf_read_value(data,
Guenter Roeckec3e5a12011-02-02 08:46:49 -0800839 data->REG_FAN_MAX_OUTPUT[i]);
Guenter Roeckda2e0252010-08-14 21:08:55 +0200840
Guenter Roeckec3e5a12011-02-02 08:46:49 -0800841 if (data->REG_FAN_STEP_OUTPUT &&
842 data->REG_FAN_STEP_OUTPUT[i] != 0xff)
Guenter Roeckda2e0252010-08-14 21:08:55 +0200843 data->fan_step_output[i] =
844 w83627ehf_read_value(data,
Guenter Roeckec3e5a12011-02-02 08:46:49 -0800845 data->REG_FAN_STEP_OUTPUT[i]);
Guenter Roeckda2e0252010-08-14 21:08:55 +0200846
Rudolf Marek08c79952006-07-05 18:14:31 +0200847 data->target_temp[i] =
David Hubbard1ea6dd32007-06-24 11:16:15 +0200848 w83627ehf_read_value(data,
Guenter Roeck279af1a2011-02-13 22:34:47 -0800849 data->REG_TARGET[i]) &
Rudolf Marek08c79952006-07-05 18:14:31 +0200850 (data->pwm_mode[i] == 1 ? 0x7f : 0xff);
Rudolf Marek08c79952006-07-05 18:14:31 +0200851 }
852
Jean Delvare08e7e272005-04-25 22:43:25 +0200853 /* Measured temperatures and limits */
Guenter Roeckd36cf322011-02-07 15:08:54 -0800854 for (i = 0; i < NUM_REG_TEMP; i++) {
855 if (!(data->have_temp & (1 << i)))
856 continue;
Guenter Roeckec3e5a12011-02-02 08:46:49 -0800857 data->temp[i] = w83627ehf_read_value(data,
858 data->reg_temp[i]);
859 if (data->reg_temp_over[i])
860 data->temp_max[i]
861 = w83627ehf_read_value(data,
862 data->reg_temp_over[i]);
863 if (data->reg_temp_hyst[i])
864 data->temp_max_hyst[i]
865 = w83627ehf_read_value(data,
866 data->reg_temp_hyst[i]);
Jean Delvare08e7e272005-04-25 22:43:25 +0200867 }
868
David Hubbard1ea6dd32007-06-24 11:16:15 +0200869 data->alarms = w83627ehf_read_value(data,
Jean Delvarea4589db2006-03-23 16:30:29 +0100870 W83627EHF_REG_ALARM1) |
David Hubbard1ea6dd32007-06-24 11:16:15 +0200871 (w83627ehf_read_value(data,
Jean Delvarea4589db2006-03-23 16:30:29 +0100872 W83627EHF_REG_ALARM2) << 8) |
David Hubbard1ea6dd32007-06-24 11:16:15 +0200873 (w83627ehf_read_value(data,
Jean Delvarea4589db2006-03-23 16:30:29 +0100874 W83627EHF_REG_ALARM3) << 16);
875
Jean Delvare08e7e272005-04-25 22:43:25 +0200876 data->last_updated = jiffies;
877 data->valid = 1;
878 }
879
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100880 mutex_unlock(&data->update_lock);
Jean Delvare08e7e272005-04-25 22:43:25 +0200881 return data;
882}
883
884/*
885 * Sysfs callback functions
886 */
Rudolf Marekcf0676f2006-03-23 16:25:22 +0100887#define show_in_reg(reg) \
888static ssize_t \
889show_##reg(struct device *dev, struct device_attribute *attr, \
890 char *buf) \
891{ \
892 struct w83627ehf_data *data = w83627ehf_update_device(dev); \
Guenter Roecke7e1ca62011-02-04 13:24:30 -0800893 struct sensor_device_attribute *sensor_attr = \
894 to_sensor_dev_attr(attr); \
Rudolf Marekcf0676f2006-03-23 16:25:22 +0100895 int nr = sensor_attr->index; \
896 return sprintf(buf, "%ld\n", in_from_reg(data->reg[nr], nr)); \
897}
898show_in_reg(in)
899show_in_reg(in_min)
900show_in_reg(in_max)
901
902#define store_in_reg(REG, reg) \
903static ssize_t \
Guenter Roecke7e1ca62011-02-04 13:24:30 -0800904store_in_##reg(struct device *dev, struct device_attribute *attr, \
905 const char *buf, size_t count) \
Rudolf Marekcf0676f2006-03-23 16:25:22 +0100906{ \
David Hubbard1ea6dd32007-06-24 11:16:15 +0200907 struct w83627ehf_data *data = dev_get_drvdata(dev); \
Guenter Roecke7e1ca62011-02-04 13:24:30 -0800908 struct sensor_device_attribute *sensor_attr = \
909 to_sensor_dev_attr(attr); \
Rudolf Marekcf0676f2006-03-23 16:25:22 +0100910 int nr = sensor_attr->index; \
Guenter Roeckbce26c52011-02-04 12:54:14 -0800911 unsigned long val; \
912 int err; \
913 err = strict_strtoul(buf, 10, &val); \
914 if (err < 0) \
915 return err; \
Rudolf Marekcf0676f2006-03-23 16:25:22 +0100916 mutex_lock(&data->update_lock); \
917 data->in_##reg[nr] = in_to_reg(val, nr); \
David Hubbard1ea6dd32007-06-24 11:16:15 +0200918 w83627ehf_write_value(data, W83627EHF_REG_IN_##REG(nr), \
Rudolf Marekcf0676f2006-03-23 16:25:22 +0100919 data->in_##reg[nr]); \
920 mutex_unlock(&data->update_lock); \
921 return count; \
922}
923
924store_in_reg(MIN, min)
925store_in_reg(MAX, max)
926
Guenter Roecke7e1ca62011-02-04 13:24:30 -0800927static ssize_t show_alarm(struct device *dev, struct device_attribute *attr,
928 char *buf)
Jean Delvarea4589db2006-03-23 16:30:29 +0100929{
930 struct w83627ehf_data *data = w83627ehf_update_device(dev);
931 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
932 int nr = sensor_attr->index;
933 return sprintf(buf, "%u\n", (data->alarms >> nr) & 0x01);
934}
935
Rudolf Marekcf0676f2006-03-23 16:25:22 +0100936static struct sensor_device_attribute sda_in_input[] = {
937 SENSOR_ATTR(in0_input, S_IRUGO, show_in, NULL, 0),
938 SENSOR_ATTR(in1_input, S_IRUGO, show_in, NULL, 1),
939 SENSOR_ATTR(in2_input, S_IRUGO, show_in, NULL, 2),
940 SENSOR_ATTR(in3_input, S_IRUGO, show_in, NULL, 3),
941 SENSOR_ATTR(in4_input, S_IRUGO, show_in, NULL, 4),
942 SENSOR_ATTR(in5_input, S_IRUGO, show_in, NULL, 5),
943 SENSOR_ATTR(in6_input, S_IRUGO, show_in, NULL, 6),
944 SENSOR_ATTR(in7_input, S_IRUGO, show_in, NULL, 7),
945 SENSOR_ATTR(in8_input, S_IRUGO, show_in, NULL, 8),
946 SENSOR_ATTR(in9_input, S_IRUGO, show_in, NULL, 9),
947};
948
Jean Delvarea4589db2006-03-23 16:30:29 +0100949static struct sensor_device_attribute sda_in_alarm[] = {
950 SENSOR_ATTR(in0_alarm, S_IRUGO, show_alarm, NULL, 0),
951 SENSOR_ATTR(in1_alarm, S_IRUGO, show_alarm, NULL, 1),
952 SENSOR_ATTR(in2_alarm, S_IRUGO, show_alarm, NULL, 2),
953 SENSOR_ATTR(in3_alarm, S_IRUGO, show_alarm, NULL, 3),
954 SENSOR_ATTR(in4_alarm, S_IRUGO, show_alarm, NULL, 8),
955 SENSOR_ATTR(in5_alarm, S_IRUGO, show_alarm, NULL, 21),
956 SENSOR_ATTR(in6_alarm, S_IRUGO, show_alarm, NULL, 20),
957 SENSOR_ATTR(in7_alarm, S_IRUGO, show_alarm, NULL, 16),
958 SENSOR_ATTR(in8_alarm, S_IRUGO, show_alarm, NULL, 17),
959 SENSOR_ATTR(in9_alarm, S_IRUGO, show_alarm, NULL, 19),
960};
961
Rudolf Marekcf0676f2006-03-23 16:25:22 +0100962static struct sensor_device_attribute sda_in_min[] = {
Guenter Roecke7e1ca62011-02-04 13:24:30 -0800963 SENSOR_ATTR(in0_min, S_IWUSR | S_IRUGO, show_in_min, store_in_min, 0),
964 SENSOR_ATTR(in1_min, S_IWUSR | S_IRUGO, show_in_min, store_in_min, 1),
965 SENSOR_ATTR(in2_min, S_IWUSR | S_IRUGO, show_in_min, store_in_min, 2),
966 SENSOR_ATTR(in3_min, S_IWUSR | S_IRUGO, show_in_min, store_in_min, 3),
967 SENSOR_ATTR(in4_min, S_IWUSR | S_IRUGO, show_in_min, store_in_min, 4),
968 SENSOR_ATTR(in5_min, S_IWUSR | S_IRUGO, show_in_min, store_in_min, 5),
969 SENSOR_ATTR(in6_min, S_IWUSR | S_IRUGO, show_in_min, store_in_min, 6),
970 SENSOR_ATTR(in7_min, S_IWUSR | S_IRUGO, show_in_min, store_in_min, 7),
971 SENSOR_ATTR(in8_min, S_IWUSR | S_IRUGO, show_in_min, store_in_min, 8),
972 SENSOR_ATTR(in9_min, S_IWUSR | S_IRUGO, show_in_min, store_in_min, 9),
Rudolf Marekcf0676f2006-03-23 16:25:22 +0100973};
974
975static struct sensor_device_attribute sda_in_max[] = {
Guenter Roecke7e1ca62011-02-04 13:24:30 -0800976 SENSOR_ATTR(in0_max, S_IWUSR | S_IRUGO, show_in_max, store_in_max, 0),
977 SENSOR_ATTR(in1_max, S_IWUSR | S_IRUGO, show_in_max, store_in_max, 1),
978 SENSOR_ATTR(in2_max, S_IWUSR | S_IRUGO, show_in_max, store_in_max, 2),
979 SENSOR_ATTR(in3_max, S_IWUSR | S_IRUGO, show_in_max, store_in_max, 3),
980 SENSOR_ATTR(in4_max, S_IWUSR | S_IRUGO, show_in_max, store_in_max, 4),
981 SENSOR_ATTR(in5_max, S_IWUSR | S_IRUGO, show_in_max, store_in_max, 5),
982 SENSOR_ATTR(in6_max, S_IWUSR | S_IRUGO, show_in_max, store_in_max, 6),
983 SENSOR_ATTR(in7_max, S_IWUSR | S_IRUGO, show_in_max, store_in_max, 7),
984 SENSOR_ATTR(in8_max, S_IWUSR | S_IRUGO, show_in_max, store_in_max, 8),
985 SENSOR_ATTR(in9_max, S_IWUSR | S_IRUGO, show_in_max, store_in_max, 9),
Rudolf Marekcf0676f2006-03-23 16:25:22 +0100986};
987
Guenter Roeckec3e5a12011-02-02 08:46:49 -0800988static ssize_t
989show_fan(struct device *dev, struct device_attribute *attr, char *buf)
990{
991 struct w83627ehf_data *data = w83627ehf_update_device(dev);
992 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
993 int nr = sensor_attr->index;
Guenter Roeck3382a912011-02-13 13:08:23 -0800994 return sprintf(buf, "%d\n", data->rpm[nr]);
Jean Delvare08e7e272005-04-25 22:43:25 +0200995}
Guenter Roeckec3e5a12011-02-02 08:46:49 -0800996
997static ssize_t
998show_fan_min(struct device *dev, struct device_attribute *attr, char *buf)
999{
1000 struct w83627ehf_data *data = w83627ehf_update_device(dev);
1001 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
1002 int nr = sensor_attr->index;
1003 return sprintf(buf, "%d\n",
Guenter Roeck26bc4402011-02-11 08:00:58 -08001004 data->fan_from_reg_min(data->fan_min[nr],
1005 data->fan_div[nr]));
Guenter Roeckec3e5a12011-02-02 08:46:49 -08001006}
Jean Delvare08e7e272005-04-25 22:43:25 +02001007
1008static ssize_t
Yuan Mu412fec82006-02-05 23:24:16 +01001009show_fan_div(struct device *dev, struct device_attribute *attr,
1010 char *buf)
Jean Delvare08e7e272005-04-25 22:43:25 +02001011{
1012 struct w83627ehf_data *data = w83627ehf_update_device(dev);
Yuan Mu412fec82006-02-05 23:24:16 +01001013 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
1014 int nr = sensor_attr->index;
1015 return sprintf(buf, "%u\n", div_from_reg(data->fan_div[nr]));
Jean Delvare08e7e272005-04-25 22:43:25 +02001016}
1017
1018static ssize_t
Yuan Mu412fec82006-02-05 23:24:16 +01001019store_fan_min(struct device *dev, struct device_attribute *attr,
1020 const char *buf, size_t count)
Jean Delvare08e7e272005-04-25 22:43:25 +02001021{
David Hubbard1ea6dd32007-06-24 11:16:15 +02001022 struct w83627ehf_data *data = dev_get_drvdata(dev);
Yuan Mu412fec82006-02-05 23:24:16 +01001023 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
1024 int nr = sensor_attr->index;
Guenter Roeckbce26c52011-02-04 12:54:14 -08001025 unsigned long val;
1026 int err;
Jean Delvare08e7e272005-04-25 22:43:25 +02001027 unsigned int reg;
1028 u8 new_div;
1029
Guenter Roeckbce26c52011-02-04 12:54:14 -08001030 err = strict_strtoul(buf, 10, &val);
1031 if (err < 0)
1032 return err;
1033
Ingo Molnar9a61bf62006-01-18 23:19:26 +01001034 mutex_lock(&data->update_lock);
Guenter Roeck26bc4402011-02-11 08:00:58 -08001035 if (!data->has_fan_div) {
1036 /*
1037 * Only NCT6776F for now, so we know that this is a 13 bit
1038 * register
1039 */
Guenter Roeckec3e5a12011-02-02 08:46:49 -08001040 if (!val) {
1041 val = 0xff1f;
1042 } else {
1043 if (val > 1350000U)
1044 val = 135000U;
1045 val = 1350000U / val;
1046 val = (val & 0x1f) | ((val << 3) & 0xff00);
1047 }
1048 data->fan_min[nr] = val;
1049 goto done; /* Leave fan divider alone */
1050 }
Jean Delvare08e7e272005-04-25 22:43:25 +02001051 if (!val) {
1052 /* No min limit, alarm disabled */
1053 data->fan_min[nr] = 255;
1054 new_div = data->fan_div[nr]; /* No change */
1055 dev_info(dev, "fan%u low limit and alarm disabled\n", nr + 1);
1056 } else if ((reg = 1350000U / val) >= 128 * 255) {
1057 /* Speed below this value cannot possibly be represented,
1058 even with the highest divider (128) */
1059 data->fan_min[nr] = 254;
1060 new_div = 7; /* 128 == (1 << 7) */
Guenter Roeckbce26c52011-02-04 12:54:14 -08001061 dev_warn(dev, "fan%u low limit %lu below minimum %u, set to "
Guenter Roeckec3e5a12011-02-02 08:46:49 -08001062 "minimum\n", nr + 1, val,
Guenter Roeck26bc4402011-02-11 08:00:58 -08001063 data->fan_from_reg_min(254, 7));
Jean Delvare08e7e272005-04-25 22:43:25 +02001064 } else if (!reg) {
1065 /* Speed above this value cannot possibly be represented,
1066 even with the lowest divider (1) */
1067 data->fan_min[nr] = 1;
1068 new_div = 0; /* 1 == (1 << 0) */
Guenter Roeckbce26c52011-02-04 12:54:14 -08001069 dev_warn(dev, "fan%u low limit %lu above maximum %u, set to "
Guenter Roeckec3e5a12011-02-02 08:46:49 -08001070 "maximum\n", nr + 1, val,
Guenter Roeck26bc4402011-02-11 08:00:58 -08001071 data->fan_from_reg_min(1, 0));
Jean Delvare08e7e272005-04-25 22:43:25 +02001072 } else {
1073 /* Automatically pick the best divider, i.e. the one such
1074 that the min limit will correspond to a register value
1075 in the 96..192 range */
1076 new_div = 0;
1077 while (reg > 192 && new_div < 7) {
1078 reg >>= 1;
1079 new_div++;
1080 }
1081 data->fan_min[nr] = reg;
1082 }
1083
1084 /* Write both the fan clock divider (if it changed) and the new
1085 fan min (unconditionally) */
1086 if (new_div != data->fan_div[nr]) {
Jean Delvare08e7e272005-04-25 22:43:25 +02001087 dev_dbg(dev, "fan%u clock divider changed from %u to %u\n",
1088 nr + 1, div_from_reg(data->fan_div[nr]),
1089 div_from_reg(new_div));
1090 data->fan_div[nr] = new_div;
Guenter Roeckec3e5a12011-02-02 08:46:49 -08001091 w83627ehf_write_fan_div_common(dev, data, nr);
Jean Delvare6b3e4642007-06-24 11:19:01 +02001092 /* Give the chip time to sample a new speed value */
1093 data->last_updated = jiffies;
Jean Delvare08e7e272005-04-25 22:43:25 +02001094 }
Guenter Roeckec3e5a12011-02-02 08:46:49 -08001095done:
Guenter Roeck279af1a2011-02-13 22:34:47 -08001096 w83627ehf_write_value(data, data->REG_FAN_MIN[nr],
Jean Delvare08e7e272005-04-25 22:43:25 +02001097 data->fan_min[nr]);
Ingo Molnar9a61bf62006-01-18 23:19:26 +01001098 mutex_unlock(&data->update_lock);
Jean Delvare08e7e272005-04-25 22:43:25 +02001099
1100 return count;
1101}
1102
Yuan Mu412fec82006-02-05 23:24:16 +01001103static struct sensor_device_attribute sda_fan_input[] = {
1104 SENSOR_ATTR(fan1_input, S_IRUGO, show_fan, NULL, 0),
1105 SENSOR_ATTR(fan2_input, S_IRUGO, show_fan, NULL, 1),
1106 SENSOR_ATTR(fan3_input, S_IRUGO, show_fan, NULL, 2),
1107 SENSOR_ATTR(fan4_input, S_IRUGO, show_fan, NULL, 3),
1108 SENSOR_ATTR(fan5_input, S_IRUGO, show_fan, NULL, 4),
1109};
Jean Delvare08e7e272005-04-25 22:43:25 +02001110
Jean Delvarea4589db2006-03-23 16:30:29 +01001111static struct sensor_device_attribute sda_fan_alarm[] = {
1112 SENSOR_ATTR(fan1_alarm, S_IRUGO, show_alarm, NULL, 6),
1113 SENSOR_ATTR(fan2_alarm, S_IRUGO, show_alarm, NULL, 7),
1114 SENSOR_ATTR(fan3_alarm, S_IRUGO, show_alarm, NULL, 11),
1115 SENSOR_ATTR(fan4_alarm, S_IRUGO, show_alarm, NULL, 10),
1116 SENSOR_ATTR(fan5_alarm, S_IRUGO, show_alarm, NULL, 23),
1117};
1118
Yuan Mu412fec82006-02-05 23:24:16 +01001119static struct sensor_device_attribute sda_fan_min[] = {
1120 SENSOR_ATTR(fan1_min, S_IWUSR | S_IRUGO, show_fan_min,
1121 store_fan_min, 0),
1122 SENSOR_ATTR(fan2_min, S_IWUSR | S_IRUGO, show_fan_min,
1123 store_fan_min, 1),
1124 SENSOR_ATTR(fan3_min, S_IWUSR | S_IRUGO, show_fan_min,
1125 store_fan_min, 2),
1126 SENSOR_ATTR(fan4_min, S_IWUSR | S_IRUGO, show_fan_min,
1127 store_fan_min, 3),
1128 SENSOR_ATTR(fan5_min, S_IWUSR | S_IRUGO, show_fan_min,
1129 store_fan_min, 4),
1130};
Jean Delvare08e7e272005-04-25 22:43:25 +02001131
Yuan Mu412fec82006-02-05 23:24:16 +01001132static struct sensor_device_attribute sda_fan_div[] = {
1133 SENSOR_ATTR(fan1_div, S_IRUGO, show_fan_div, NULL, 0),
1134 SENSOR_ATTR(fan2_div, S_IRUGO, show_fan_div, NULL, 1),
1135 SENSOR_ATTR(fan3_div, S_IRUGO, show_fan_div, NULL, 2),
1136 SENSOR_ATTR(fan4_div, S_IRUGO, show_fan_div, NULL, 3),
1137 SENSOR_ATTR(fan5_div, S_IRUGO, show_fan_div, NULL, 4),
1138};
Jean Delvare08e7e272005-04-25 22:43:25 +02001139
Guenter Roeckd36cf322011-02-07 15:08:54 -08001140static ssize_t
1141show_temp_label(struct device *dev, struct device_attribute *attr, char *buf)
1142{
1143 struct w83627ehf_data *data = w83627ehf_update_device(dev);
1144 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
1145 int nr = sensor_attr->index;
1146 return sprintf(buf, "%s\n", data->temp_label[data->temp_src[nr]]);
1147}
1148
Guenter Roeckec3e5a12011-02-02 08:46:49 -08001149#define show_temp_reg(addr, reg) \
Jean Delvare08e7e272005-04-25 22:43:25 +02001150static ssize_t \
Yuan Mu412fec82006-02-05 23:24:16 +01001151show_##reg(struct device *dev, struct device_attribute *attr, \
1152 char *buf) \
Jean Delvare08e7e272005-04-25 22:43:25 +02001153{ \
1154 struct w83627ehf_data *data = w83627ehf_update_device(dev); \
Guenter Roecke7e1ca62011-02-04 13:24:30 -08001155 struct sensor_device_attribute *sensor_attr = \
1156 to_sensor_dev_attr(attr); \
Yuan Mu412fec82006-02-05 23:24:16 +01001157 int nr = sensor_attr->index; \
Jean Delvare08e7e272005-04-25 22:43:25 +02001158 return sprintf(buf, "%d\n", \
Guenter Roeckec3e5a12011-02-02 08:46:49 -08001159 temp_from_reg(data->addr[nr], data->reg[nr])); \
Jean Delvare08e7e272005-04-25 22:43:25 +02001160}
Guenter Roeckec3e5a12011-02-02 08:46:49 -08001161show_temp_reg(reg_temp, temp);
1162show_temp_reg(reg_temp_over, temp_max);
1163show_temp_reg(reg_temp_hyst, temp_max_hyst);
Jean Delvare08e7e272005-04-25 22:43:25 +02001164
Guenter Roeckec3e5a12011-02-02 08:46:49 -08001165#define store_temp_reg(addr, reg) \
Jean Delvare08e7e272005-04-25 22:43:25 +02001166static ssize_t \
Yuan Mu412fec82006-02-05 23:24:16 +01001167store_##reg(struct device *dev, struct device_attribute *attr, \
1168 const char *buf, size_t count) \
Jean Delvare08e7e272005-04-25 22:43:25 +02001169{ \
David Hubbard1ea6dd32007-06-24 11:16:15 +02001170 struct w83627ehf_data *data = dev_get_drvdata(dev); \
Guenter Roecke7e1ca62011-02-04 13:24:30 -08001171 struct sensor_device_attribute *sensor_attr = \
1172 to_sensor_dev_attr(attr); \
Yuan Mu412fec82006-02-05 23:24:16 +01001173 int nr = sensor_attr->index; \
Guenter Roeckbce26c52011-02-04 12:54:14 -08001174 int err; \
1175 long val; \
1176 err = strict_strtol(buf, 10, &val); \
1177 if (err < 0) \
1178 return err; \
Ingo Molnar9a61bf62006-01-18 23:19:26 +01001179 mutex_lock(&data->update_lock); \
Guenter Roeckec3e5a12011-02-02 08:46:49 -08001180 data->reg[nr] = temp_to_reg(data->addr[nr], val); \
1181 w83627ehf_write_value(data, data->addr[nr], \
Jean Delvare08e7e272005-04-25 22:43:25 +02001182 data->reg[nr]); \
Ingo Molnar9a61bf62006-01-18 23:19:26 +01001183 mutex_unlock(&data->update_lock); \
Jean Delvare08e7e272005-04-25 22:43:25 +02001184 return count; \
1185}
Guenter Roeckec3e5a12011-02-02 08:46:49 -08001186store_temp_reg(reg_temp_over, temp_max);
1187store_temp_reg(reg_temp_hyst, temp_max_hyst);
Jean Delvare08e7e272005-04-25 22:43:25 +02001188
Jean Delvareda667362007-06-24 11:21:02 +02001189static ssize_t
1190show_temp_type(struct device *dev, struct device_attribute *attr, char *buf)
1191{
1192 struct w83627ehf_data *data = w83627ehf_update_device(dev);
1193 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
1194 int nr = sensor_attr->index;
1195 return sprintf(buf, "%d\n", (int)data->temp_type[nr]);
1196}
1197
Gong Juna157d062009-03-30 21:46:43 +02001198static struct sensor_device_attribute sda_temp_input[] = {
Guenter Roeckbce26c52011-02-04 12:54:14 -08001199 SENSOR_ATTR(temp1_input, S_IRUGO, show_temp, NULL, 0),
1200 SENSOR_ATTR(temp2_input, S_IRUGO, show_temp, NULL, 1),
1201 SENSOR_ATTR(temp3_input, S_IRUGO, show_temp, NULL, 2),
Guenter Roeckd36cf322011-02-07 15:08:54 -08001202 SENSOR_ATTR(temp4_input, S_IRUGO, show_temp, NULL, 3),
Guenter Roeckec3e5a12011-02-02 08:46:49 -08001203 SENSOR_ATTR(temp5_input, S_IRUGO, show_temp, NULL, 4),
1204 SENSOR_ATTR(temp6_input, S_IRUGO, show_temp, NULL, 5),
1205 SENSOR_ATTR(temp7_input, S_IRUGO, show_temp, NULL, 6),
1206 SENSOR_ATTR(temp8_input, S_IRUGO, show_temp, NULL, 7),
1207 SENSOR_ATTR(temp9_input, S_IRUGO, show_temp, NULL, 8),
Guenter Roeckd36cf322011-02-07 15:08:54 -08001208};
1209
1210static struct sensor_device_attribute sda_temp_label[] = {
1211 SENSOR_ATTR(temp1_label, S_IRUGO, show_temp_label, NULL, 0),
1212 SENSOR_ATTR(temp2_label, S_IRUGO, show_temp_label, NULL, 1),
1213 SENSOR_ATTR(temp3_label, S_IRUGO, show_temp_label, NULL, 2),
1214 SENSOR_ATTR(temp4_label, S_IRUGO, show_temp_label, NULL, 3),
Guenter Roeckec3e5a12011-02-02 08:46:49 -08001215 SENSOR_ATTR(temp5_label, S_IRUGO, show_temp_label, NULL, 4),
1216 SENSOR_ATTR(temp6_label, S_IRUGO, show_temp_label, NULL, 5),
1217 SENSOR_ATTR(temp7_label, S_IRUGO, show_temp_label, NULL, 6),
1218 SENSOR_ATTR(temp8_label, S_IRUGO, show_temp_label, NULL, 7),
1219 SENSOR_ATTR(temp9_label, S_IRUGO, show_temp_label, NULL, 8),
Gong Juna157d062009-03-30 21:46:43 +02001220};
1221
1222static struct sensor_device_attribute sda_temp_max[] = {
Guenter Roeckbce26c52011-02-04 12:54:14 -08001223 SENSOR_ATTR(temp1_max, S_IRUGO | S_IWUSR, show_temp_max,
Yuan Mu412fec82006-02-05 23:24:16 +01001224 store_temp_max, 0),
Guenter Roeckbce26c52011-02-04 12:54:14 -08001225 SENSOR_ATTR(temp2_max, S_IRUGO | S_IWUSR, show_temp_max,
Yuan Mu412fec82006-02-05 23:24:16 +01001226 store_temp_max, 1),
Guenter Roeckbce26c52011-02-04 12:54:14 -08001227 SENSOR_ATTR(temp3_max, S_IRUGO | S_IWUSR, show_temp_max,
1228 store_temp_max, 2),
Guenter Roeckec3e5a12011-02-02 08:46:49 -08001229 SENSOR_ATTR(temp4_max, S_IRUGO | S_IWUSR, show_temp_max,
1230 store_temp_max, 3),
1231 SENSOR_ATTR(temp5_max, S_IRUGO | S_IWUSR, show_temp_max,
1232 store_temp_max, 4),
1233 SENSOR_ATTR(temp6_max, S_IRUGO | S_IWUSR, show_temp_max,
1234 store_temp_max, 5),
1235 SENSOR_ATTR(temp7_max, S_IRUGO | S_IWUSR, show_temp_max,
1236 store_temp_max, 6),
1237 SENSOR_ATTR(temp8_max, S_IRUGO | S_IWUSR, show_temp_max,
1238 store_temp_max, 7),
1239 SENSOR_ATTR(temp9_max, S_IRUGO | S_IWUSR, show_temp_max,
1240 store_temp_max, 8),
Gong Juna157d062009-03-30 21:46:43 +02001241};
1242
1243static struct sensor_device_attribute sda_temp_max_hyst[] = {
Guenter Roeckbce26c52011-02-04 12:54:14 -08001244 SENSOR_ATTR(temp1_max_hyst, S_IRUGO | S_IWUSR, show_temp_max_hyst,
Yuan Mu412fec82006-02-05 23:24:16 +01001245 store_temp_max_hyst, 0),
Guenter Roeckbce26c52011-02-04 12:54:14 -08001246 SENSOR_ATTR(temp2_max_hyst, S_IRUGO | S_IWUSR, show_temp_max_hyst,
Yuan Mu412fec82006-02-05 23:24:16 +01001247 store_temp_max_hyst, 1),
Guenter Roeckbce26c52011-02-04 12:54:14 -08001248 SENSOR_ATTR(temp3_max_hyst, S_IRUGO | S_IWUSR, show_temp_max_hyst,
1249 store_temp_max_hyst, 2),
Guenter Roeckec3e5a12011-02-02 08:46:49 -08001250 SENSOR_ATTR(temp4_max_hyst, S_IRUGO | S_IWUSR, show_temp_max_hyst,
1251 store_temp_max_hyst, 3),
1252 SENSOR_ATTR(temp5_max_hyst, S_IRUGO | S_IWUSR, show_temp_max_hyst,
1253 store_temp_max_hyst, 4),
1254 SENSOR_ATTR(temp6_max_hyst, S_IRUGO | S_IWUSR, show_temp_max_hyst,
1255 store_temp_max_hyst, 5),
1256 SENSOR_ATTR(temp7_max_hyst, S_IRUGO | S_IWUSR, show_temp_max_hyst,
1257 store_temp_max_hyst, 6),
1258 SENSOR_ATTR(temp8_max_hyst, S_IRUGO | S_IWUSR, show_temp_max_hyst,
1259 store_temp_max_hyst, 7),
1260 SENSOR_ATTR(temp9_max_hyst, S_IRUGO | S_IWUSR, show_temp_max_hyst,
1261 store_temp_max_hyst, 8),
Gong Juna157d062009-03-30 21:46:43 +02001262};
1263
1264static struct sensor_device_attribute sda_temp_alarm[] = {
Jean Delvarea4589db2006-03-23 16:30:29 +01001265 SENSOR_ATTR(temp1_alarm, S_IRUGO, show_alarm, NULL, 4),
1266 SENSOR_ATTR(temp2_alarm, S_IRUGO, show_alarm, NULL, 5),
1267 SENSOR_ATTR(temp3_alarm, S_IRUGO, show_alarm, NULL, 13),
Gong Juna157d062009-03-30 21:46:43 +02001268};
1269
1270static struct sensor_device_attribute sda_temp_type[] = {
Jean Delvareda667362007-06-24 11:21:02 +02001271 SENSOR_ATTR(temp1_type, S_IRUGO, show_temp_type, NULL, 0),
1272 SENSOR_ATTR(temp2_type, S_IRUGO, show_temp_type, NULL, 1),
1273 SENSOR_ATTR(temp3_type, S_IRUGO, show_temp_type, NULL, 2),
Yuan Mu412fec82006-02-05 23:24:16 +01001274};
Jean Delvare08e7e272005-04-25 22:43:25 +02001275
Rudolf Marek08c79952006-07-05 18:14:31 +02001276#define show_pwm_reg(reg) \
Guenter Roecke7e1ca62011-02-04 13:24:30 -08001277static ssize_t show_##reg(struct device *dev, struct device_attribute *attr, \
1278 char *buf) \
Rudolf Marek08c79952006-07-05 18:14:31 +02001279{ \
1280 struct w83627ehf_data *data = w83627ehf_update_device(dev); \
Guenter Roecke7e1ca62011-02-04 13:24:30 -08001281 struct sensor_device_attribute *sensor_attr = \
1282 to_sensor_dev_attr(attr); \
Rudolf Marek08c79952006-07-05 18:14:31 +02001283 int nr = sensor_attr->index; \
1284 return sprintf(buf, "%d\n", data->reg[nr]); \
1285}
1286
1287show_pwm_reg(pwm_mode)
1288show_pwm_reg(pwm_enable)
1289show_pwm_reg(pwm)
1290
1291static ssize_t
1292store_pwm_mode(struct device *dev, struct device_attribute *attr,
1293 const char *buf, size_t count)
1294{
David Hubbard1ea6dd32007-06-24 11:16:15 +02001295 struct w83627ehf_data *data = dev_get_drvdata(dev);
Rudolf Marek08c79952006-07-05 18:14:31 +02001296 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
1297 int nr = sensor_attr->index;
Guenter Roeckbce26c52011-02-04 12:54:14 -08001298 unsigned long val;
1299 int err;
Rudolf Marek08c79952006-07-05 18:14:31 +02001300 u16 reg;
1301
Guenter Roeckbce26c52011-02-04 12:54:14 -08001302 err = strict_strtoul(buf, 10, &val);
1303 if (err < 0)
1304 return err;
1305
Rudolf Marek08c79952006-07-05 18:14:31 +02001306 if (val > 1)
1307 return -EINVAL;
1308 mutex_lock(&data->update_lock);
David Hubbard1ea6dd32007-06-24 11:16:15 +02001309 reg = w83627ehf_read_value(data, W83627EHF_REG_PWM_ENABLE[nr]);
Rudolf Marek08c79952006-07-05 18:14:31 +02001310 data->pwm_mode[nr] = val;
1311 reg &= ~(1 << W83627EHF_PWM_MODE_SHIFT[nr]);
1312 if (!val)
1313 reg |= 1 << W83627EHF_PWM_MODE_SHIFT[nr];
David Hubbard1ea6dd32007-06-24 11:16:15 +02001314 w83627ehf_write_value(data, W83627EHF_REG_PWM_ENABLE[nr], reg);
Rudolf Marek08c79952006-07-05 18:14:31 +02001315 mutex_unlock(&data->update_lock);
1316 return count;
1317}
1318
1319static ssize_t
1320store_pwm(struct device *dev, struct device_attribute *attr,
1321 const char *buf, size_t count)
1322{
David Hubbard1ea6dd32007-06-24 11:16:15 +02001323 struct w83627ehf_data *data = dev_get_drvdata(dev);
Rudolf Marek08c79952006-07-05 18:14:31 +02001324 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
1325 int nr = sensor_attr->index;
Guenter Roeckbce26c52011-02-04 12:54:14 -08001326 unsigned long val;
1327 int err;
1328
1329 err = strict_strtoul(buf, 10, &val);
1330 if (err < 0)
1331 return err;
1332
1333 val = SENSORS_LIMIT(val, 0, 255);
Rudolf Marek08c79952006-07-05 18:14:31 +02001334
1335 mutex_lock(&data->update_lock);
1336 data->pwm[nr] = val;
Guenter Roeck279af1a2011-02-13 22:34:47 -08001337 w83627ehf_write_value(data, data->REG_PWM[nr], val);
Rudolf Marek08c79952006-07-05 18:14:31 +02001338 mutex_unlock(&data->update_lock);
1339 return count;
1340}
1341
1342static ssize_t
1343store_pwm_enable(struct device *dev, struct device_attribute *attr,
1344 const char *buf, size_t count)
1345{
David Hubbard1ea6dd32007-06-24 11:16:15 +02001346 struct w83627ehf_data *data = dev_get_drvdata(dev);
Guenter Roeckec3e5a12011-02-02 08:46:49 -08001347 struct w83627ehf_sio_data *sio_data = dev->platform_data;
Rudolf Marek08c79952006-07-05 18:14:31 +02001348 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
1349 int nr = sensor_attr->index;
Guenter Roeckbce26c52011-02-04 12:54:14 -08001350 unsigned long val;
1351 int err;
Rudolf Marek08c79952006-07-05 18:14:31 +02001352 u16 reg;
1353
Guenter Roeckbce26c52011-02-04 12:54:14 -08001354 err = strict_strtoul(buf, 10, &val);
1355 if (err < 0)
1356 return err;
1357
Guenter Roeckb84bb512011-02-13 23:01:25 -08001358 if (!val || (val > 4 && val != data->pwm_enable_orig[nr]))
Rudolf Marek08c79952006-07-05 18:14:31 +02001359 return -EINVAL;
Guenter Roeckec3e5a12011-02-02 08:46:49 -08001360 /* SmartFan III mode is not supported on NCT6776F */
1361 if (sio_data->kind == nct6776 && val == 4)
1362 return -EINVAL;
1363
Rudolf Marek08c79952006-07-05 18:14:31 +02001364 mutex_lock(&data->update_lock);
Rudolf Marek08c79952006-07-05 18:14:31 +02001365 data->pwm_enable[nr] = val;
Guenter Roeckec3e5a12011-02-02 08:46:49 -08001366 if (sio_data->kind == nct6775 || sio_data->kind == nct6776) {
1367 reg = w83627ehf_read_value(data,
1368 NCT6775_REG_FAN_MODE[nr]);
1369 reg &= 0x0f;
1370 reg |= (val - 1) << 4;
1371 w83627ehf_write_value(data,
1372 NCT6775_REG_FAN_MODE[nr], reg);
1373 } else {
1374 reg = w83627ehf_read_value(data, W83627EHF_REG_PWM_ENABLE[nr]);
1375 reg &= ~(0x03 << W83627EHF_PWM_ENABLE_SHIFT[nr]);
1376 reg |= (val - 1) << W83627EHF_PWM_ENABLE_SHIFT[nr];
1377 w83627ehf_write_value(data, W83627EHF_REG_PWM_ENABLE[nr], reg);
1378 }
Rudolf Marek08c79952006-07-05 18:14:31 +02001379 mutex_unlock(&data->update_lock);
1380 return count;
1381}
1382
1383
1384#define show_tol_temp(reg) \
1385static ssize_t show_##reg(struct device *dev, struct device_attribute *attr, \
1386 char *buf) \
1387{ \
1388 struct w83627ehf_data *data = w83627ehf_update_device(dev); \
Guenter Roecke7e1ca62011-02-04 13:24:30 -08001389 struct sensor_device_attribute *sensor_attr = \
1390 to_sensor_dev_attr(attr); \
Rudolf Marek08c79952006-07-05 18:14:31 +02001391 int nr = sensor_attr->index; \
Guenter Roeckbce26c52011-02-04 12:54:14 -08001392 return sprintf(buf, "%d\n", data->reg[nr] * 1000); \
Rudolf Marek08c79952006-07-05 18:14:31 +02001393}
1394
1395show_tol_temp(tolerance)
1396show_tol_temp(target_temp)
1397
1398static ssize_t
1399store_target_temp(struct device *dev, struct device_attribute *attr,
1400 const char *buf, size_t count)
1401{
David Hubbard1ea6dd32007-06-24 11:16:15 +02001402 struct w83627ehf_data *data = dev_get_drvdata(dev);
Rudolf Marek08c79952006-07-05 18:14:31 +02001403 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
1404 int nr = sensor_attr->index;
Guenter Roeckbce26c52011-02-04 12:54:14 -08001405 long val;
1406 int err;
1407
1408 err = strict_strtol(buf, 10, &val);
1409 if (err < 0)
1410 return err;
1411
1412 val = SENSORS_LIMIT(DIV_ROUND_CLOSEST(val, 1000), 0, 127);
Rudolf Marek08c79952006-07-05 18:14:31 +02001413
1414 mutex_lock(&data->update_lock);
1415 data->target_temp[nr] = val;
Guenter Roeck279af1a2011-02-13 22:34:47 -08001416 w83627ehf_write_value(data, data->REG_TARGET[nr], val);
Rudolf Marek08c79952006-07-05 18:14:31 +02001417 mutex_unlock(&data->update_lock);
1418 return count;
1419}
1420
1421static ssize_t
1422store_tolerance(struct device *dev, struct device_attribute *attr,
1423 const char *buf, size_t count)
1424{
David Hubbard1ea6dd32007-06-24 11:16:15 +02001425 struct w83627ehf_data *data = dev_get_drvdata(dev);
Guenter Roeckec3e5a12011-02-02 08:46:49 -08001426 struct w83627ehf_sio_data *sio_data = dev->platform_data;
Rudolf Marek08c79952006-07-05 18:14:31 +02001427 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
1428 int nr = sensor_attr->index;
1429 u16 reg;
Guenter Roeckbce26c52011-02-04 12:54:14 -08001430 long val;
1431 int err;
1432
1433 err = strict_strtol(buf, 10, &val);
1434 if (err < 0)
1435 return err;
1436
Rudolf Marek08c79952006-07-05 18:14:31 +02001437 /* Limit the temp to 0C - 15C */
Guenter Roeckbce26c52011-02-04 12:54:14 -08001438 val = SENSORS_LIMIT(DIV_ROUND_CLOSEST(val, 1000), 0, 15);
Rudolf Marek08c79952006-07-05 18:14:31 +02001439
1440 mutex_lock(&data->update_lock);
Guenter Roeckec3e5a12011-02-02 08:46:49 -08001441 if (sio_data->kind == nct6775 || sio_data->kind == nct6776) {
1442 /* Limit tolerance further for NCT6776F */
1443 if (sio_data->kind == nct6776 && val > 7)
1444 val = 7;
1445 reg = w83627ehf_read_value(data, NCT6775_REG_FAN_MODE[nr]);
Rudolf Marek08c79952006-07-05 18:14:31 +02001446 reg = (reg & 0xf0) | val;
Guenter Roeckec3e5a12011-02-02 08:46:49 -08001447 w83627ehf_write_value(data, NCT6775_REG_FAN_MODE[nr], reg);
1448 } else {
1449 reg = w83627ehf_read_value(data, W83627EHF_REG_TOLERANCE[nr]);
1450 if (nr == 1)
1451 reg = (reg & 0x0f) | (val << 4);
1452 else
1453 reg = (reg & 0xf0) | val;
1454 w83627ehf_write_value(data, W83627EHF_REG_TOLERANCE[nr], reg);
1455 }
1456 data->tolerance[nr] = val;
Rudolf Marek08c79952006-07-05 18:14:31 +02001457 mutex_unlock(&data->update_lock);
1458 return count;
1459}
1460
1461static struct sensor_device_attribute sda_pwm[] = {
1462 SENSOR_ATTR(pwm1, S_IWUSR | S_IRUGO, show_pwm, store_pwm, 0),
1463 SENSOR_ATTR(pwm2, S_IWUSR | S_IRUGO, show_pwm, store_pwm, 1),
1464 SENSOR_ATTR(pwm3, S_IWUSR | S_IRUGO, show_pwm, store_pwm, 2),
1465 SENSOR_ATTR(pwm4, S_IWUSR | S_IRUGO, show_pwm, store_pwm, 3),
1466};
1467
1468static struct sensor_device_attribute sda_pwm_mode[] = {
1469 SENSOR_ATTR(pwm1_mode, S_IWUSR | S_IRUGO, show_pwm_mode,
1470 store_pwm_mode, 0),
1471 SENSOR_ATTR(pwm2_mode, S_IWUSR | S_IRUGO, show_pwm_mode,
1472 store_pwm_mode, 1),
1473 SENSOR_ATTR(pwm3_mode, S_IWUSR | S_IRUGO, show_pwm_mode,
1474 store_pwm_mode, 2),
1475 SENSOR_ATTR(pwm4_mode, S_IWUSR | S_IRUGO, show_pwm_mode,
1476 store_pwm_mode, 3),
1477};
1478
1479static struct sensor_device_attribute sda_pwm_enable[] = {
1480 SENSOR_ATTR(pwm1_enable, S_IWUSR | S_IRUGO, show_pwm_enable,
1481 store_pwm_enable, 0),
1482 SENSOR_ATTR(pwm2_enable, S_IWUSR | S_IRUGO, show_pwm_enable,
1483 store_pwm_enable, 1),
1484 SENSOR_ATTR(pwm3_enable, S_IWUSR | S_IRUGO, show_pwm_enable,
1485 store_pwm_enable, 2),
1486 SENSOR_ATTR(pwm4_enable, S_IWUSR | S_IRUGO, show_pwm_enable,
1487 store_pwm_enable, 3),
1488};
1489
1490static struct sensor_device_attribute sda_target_temp[] = {
1491 SENSOR_ATTR(pwm1_target, S_IWUSR | S_IRUGO, show_target_temp,
1492 store_target_temp, 0),
1493 SENSOR_ATTR(pwm2_target, S_IWUSR | S_IRUGO, show_target_temp,
1494 store_target_temp, 1),
1495 SENSOR_ATTR(pwm3_target, S_IWUSR | S_IRUGO, show_target_temp,
1496 store_target_temp, 2),
1497 SENSOR_ATTR(pwm4_target, S_IWUSR | S_IRUGO, show_target_temp,
1498 store_target_temp, 3),
1499};
1500
1501static struct sensor_device_attribute sda_tolerance[] = {
1502 SENSOR_ATTR(pwm1_tolerance, S_IWUSR | S_IRUGO, show_tolerance,
1503 store_tolerance, 0),
1504 SENSOR_ATTR(pwm2_tolerance, S_IWUSR | S_IRUGO, show_tolerance,
1505 store_tolerance, 1),
1506 SENSOR_ATTR(pwm3_tolerance, S_IWUSR | S_IRUGO, show_tolerance,
1507 store_tolerance, 2),
1508 SENSOR_ATTR(pwm4_tolerance, S_IWUSR | S_IRUGO, show_tolerance,
1509 store_tolerance, 3),
1510};
1511
Rudolf Marek08c79952006-07-05 18:14:31 +02001512/* Smart Fan registers */
1513
1514#define fan_functions(reg, REG) \
1515static ssize_t show_##reg(struct device *dev, struct device_attribute *attr, \
1516 char *buf) \
1517{ \
1518 struct w83627ehf_data *data = w83627ehf_update_device(dev); \
Guenter Roecke7e1ca62011-02-04 13:24:30 -08001519 struct sensor_device_attribute *sensor_attr = \
1520 to_sensor_dev_attr(attr); \
Rudolf Marek08c79952006-07-05 18:14:31 +02001521 int nr = sensor_attr->index; \
1522 return sprintf(buf, "%d\n", data->reg[nr]); \
Guenter Roecke7e1ca62011-02-04 13:24:30 -08001523} \
Rudolf Marek08c79952006-07-05 18:14:31 +02001524static ssize_t \
1525store_##reg(struct device *dev, struct device_attribute *attr, \
1526 const char *buf, size_t count) \
Guenter Roecke7e1ca62011-02-04 13:24:30 -08001527{ \
David Hubbard1ea6dd32007-06-24 11:16:15 +02001528 struct w83627ehf_data *data = dev_get_drvdata(dev); \
Guenter Roecke7e1ca62011-02-04 13:24:30 -08001529 struct sensor_device_attribute *sensor_attr = \
1530 to_sensor_dev_attr(attr); \
Rudolf Marek08c79952006-07-05 18:14:31 +02001531 int nr = sensor_attr->index; \
Guenter Roeckbce26c52011-02-04 12:54:14 -08001532 unsigned long val; \
1533 int err; \
1534 err = strict_strtoul(buf, 10, &val); \
1535 if (err < 0) \
1536 return err; \
1537 val = SENSORS_LIMIT(val, 1, 255); \
Rudolf Marek08c79952006-07-05 18:14:31 +02001538 mutex_lock(&data->update_lock); \
1539 data->reg[nr] = val; \
Guenter Roeckda2e0252010-08-14 21:08:55 +02001540 w83627ehf_write_value(data, data->REG_##REG[nr], val); \
Rudolf Marek08c79952006-07-05 18:14:31 +02001541 mutex_unlock(&data->update_lock); \
1542 return count; \
1543}
1544
Daniel J Blueman41e9a062009-12-14 18:01:37 -08001545fan_functions(fan_start_output, FAN_START_OUTPUT)
1546fan_functions(fan_stop_output, FAN_STOP_OUTPUT)
1547fan_functions(fan_max_output, FAN_MAX_OUTPUT)
1548fan_functions(fan_step_output, FAN_STEP_OUTPUT)
Rudolf Marek08c79952006-07-05 18:14:31 +02001549
1550#define fan_time_functions(reg, REG) \
1551static ssize_t show_##reg(struct device *dev, struct device_attribute *attr, \
1552 char *buf) \
1553{ \
1554 struct w83627ehf_data *data = w83627ehf_update_device(dev); \
Guenter Roecke7e1ca62011-02-04 13:24:30 -08001555 struct sensor_device_attribute *sensor_attr = \
1556 to_sensor_dev_attr(attr); \
Rudolf Marek08c79952006-07-05 18:14:31 +02001557 int nr = sensor_attr->index; \
1558 return sprintf(buf, "%d\n", \
Guenter Roecke7e1ca62011-02-04 13:24:30 -08001559 step_time_from_reg(data->reg[nr], \
1560 data->pwm_mode[nr])); \
Rudolf Marek08c79952006-07-05 18:14:31 +02001561} \
1562\
1563static ssize_t \
1564store_##reg(struct device *dev, struct device_attribute *attr, \
1565 const char *buf, size_t count) \
1566{ \
David Hubbard1ea6dd32007-06-24 11:16:15 +02001567 struct w83627ehf_data *data = dev_get_drvdata(dev); \
Guenter Roecke7e1ca62011-02-04 13:24:30 -08001568 struct sensor_device_attribute *sensor_attr = \
1569 to_sensor_dev_attr(attr); \
Rudolf Marek08c79952006-07-05 18:14:31 +02001570 int nr = sensor_attr->index; \
Guenter Roeckbce26c52011-02-04 12:54:14 -08001571 unsigned long val; \
1572 int err; \
1573 err = strict_strtoul(buf, 10, &val); \
1574 if (err < 0) \
1575 return err; \
1576 val = step_time_to_reg(val, data->pwm_mode[nr]); \
Rudolf Marek08c79952006-07-05 18:14:31 +02001577 mutex_lock(&data->update_lock); \
1578 data->reg[nr] = val; \
David Hubbard1ea6dd32007-06-24 11:16:15 +02001579 w83627ehf_write_value(data, W83627EHF_REG_##REG[nr], val); \
Rudolf Marek08c79952006-07-05 18:14:31 +02001580 mutex_unlock(&data->update_lock); \
1581 return count; \
1582} \
1583
1584fan_time_functions(fan_stop_time, FAN_STOP_TIME)
1585
David Hubbard1ea6dd32007-06-24 11:16:15 +02001586static ssize_t show_name(struct device *dev, struct device_attribute *attr,
1587 char *buf)
1588{
1589 struct w83627ehf_data *data = dev_get_drvdata(dev);
1590
1591 return sprintf(buf, "%s\n", data->name);
1592}
1593static DEVICE_ATTR(name, S_IRUGO, show_name, NULL);
Rudolf Marek08c79952006-07-05 18:14:31 +02001594
1595static struct sensor_device_attribute sda_sf3_arrays_fan4[] = {
1596 SENSOR_ATTR(pwm4_stop_time, S_IWUSR | S_IRUGO, show_fan_stop_time,
1597 store_fan_stop_time, 3),
Daniel J Blueman41e9a062009-12-14 18:01:37 -08001598 SENSOR_ATTR(pwm4_start_output, S_IWUSR | S_IRUGO, show_fan_start_output,
1599 store_fan_start_output, 3),
1600 SENSOR_ATTR(pwm4_stop_output, S_IWUSR | S_IRUGO, show_fan_stop_output,
1601 store_fan_stop_output, 3),
1602 SENSOR_ATTR(pwm4_max_output, S_IWUSR | S_IRUGO, show_fan_max_output,
1603 store_fan_max_output, 3),
1604 SENSOR_ATTR(pwm4_step_output, S_IWUSR | S_IRUGO, show_fan_step_output,
1605 store_fan_step_output, 3),
Rudolf Marek08c79952006-07-05 18:14:31 +02001606};
1607
1608static struct sensor_device_attribute sda_sf3_arrays[] = {
1609 SENSOR_ATTR(pwm1_stop_time, S_IWUSR | S_IRUGO, show_fan_stop_time,
1610 store_fan_stop_time, 0),
1611 SENSOR_ATTR(pwm2_stop_time, S_IWUSR | S_IRUGO, show_fan_stop_time,
1612 store_fan_stop_time, 1),
1613 SENSOR_ATTR(pwm3_stop_time, S_IWUSR | S_IRUGO, show_fan_stop_time,
1614 store_fan_stop_time, 2),
Daniel J Blueman41e9a062009-12-14 18:01:37 -08001615 SENSOR_ATTR(pwm1_start_output, S_IWUSR | S_IRUGO, show_fan_start_output,
1616 store_fan_start_output, 0),
1617 SENSOR_ATTR(pwm2_start_output, S_IWUSR | S_IRUGO, show_fan_start_output,
1618 store_fan_start_output, 1),
1619 SENSOR_ATTR(pwm3_start_output, S_IWUSR | S_IRUGO, show_fan_start_output,
1620 store_fan_start_output, 2),
1621 SENSOR_ATTR(pwm1_stop_output, S_IWUSR | S_IRUGO, show_fan_stop_output,
1622 store_fan_stop_output, 0),
1623 SENSOR_ATTR(pwm2_stop_output, S_IWUSR | S_IRUGO, show_fan_stop_output,
1624 store_fan_stop_output, 1),
1625 SENSOR_ATTR(pwm3_stop_output, S_IWUSR | S_IRUGO, show_fan_stop_output,
1626 store_fan_stop_output, 2),
Guenter Roeckda2e0252010-08-14 21:08:55 +02001627};
Daniel J Blueman41e9a062009-12-14 18:01:37 -08001628
Guenter Roeckda2e0252010-08-14 21:08:55 +02001629
1630/*
1631 * pwm1 and pwm3 don't support max and step settings on all chips.
1632 * Need to check support while generating/removing attribute files.
1633 */
1634static struct sensor_device_attribute sda_sf3_max_step_arrays[] = {
1635 SENSOR_ATTR(pwm1_max_output, S_IWUSR | S_IRUGO, show_fan_max_output,
1636 store_fan_max_output, 0),
1637 SENSOR_ATTR(pwm1_step_output, S_IWUSR | S_IRUGO, show_fan_step_output,
1638 store_fan_step_output, 0),
Daniel J Blueman41e9a062009-12-14 18:01:37 -08001639 SENSOR_ATTR(pwm2_max_output, S_IWUSR | S_IRUGO, show_fan_max_output,
1640 store_fan_max_output, 1),
1641 SENSOR_ATTR(pwm2_step_output, S_IWUSR | S_IRUGO, show_fan_step_output,
1642 store_fan_step_output, 1),
Guenter Roeckda2e0252010-08-14 21:08:55 +02001643 SENSOR_ATTR(pwm3_max_output, S_IWUSR | S_IRUGO, show_fan_max_output,
1644 store_fan_max_output, 2),
1645 SENSOR_ATTR(pwm3_step_output, S_IWUSR | S_IRUGO, show_fan_step_output,
1646 store_fan_step_output, 2),
Rudolf Marek08c79952006-07-05 18:14:31 +02001647};
1648
Jean Delvarefc18d6c2007-06-24 11:19:42 +02001649static ssize_t
1650show_vid(struct device *dev, struct device_attribute *attr, char *buf)
1651{
1652 struct w83627ehf_data *data = dev_get_drvdata(dev);
1653 return sprintf(buf, "%d\n", vid_from_reg(data->vid, data->vrm));
1654}
1655static DEVICE_ATTR(cpu0_vid, S_IRUGO, show_vid, NULL);
1656
Jean Delvare08e7e272005-04-25 22:43:25 +02001657/*
David Hubbard1ea6dd32007-06-24 11:16:15 +02001658 * Driver and device management
Jean Delvare08e7e272005-04-25 22:43:25 +02001659 */
1660
David Hubbardc18beb52006-09-24 21:04:38 +02001661static void w83627ehf_device_remove_files(struct device *dev)
1662{
1663 /* some entries in the following arrays may not have been used in
1664 * device_create_file(), but device_remove_file() will ignore them */
1665 int i;
David Hubbard1ea6dd32007-06-24 11:16:15 +02001666 struct w83627ehf_data *data = dev_get_drvdata(dev);
David Hubbardc18beb52006-09-24 21:04:38 +02001667
1668 for (i = 0; i < ARRAY_SIZE(sda_sf3_arrays); i++)
1669 device_remove_file(dev, &sda_sf3_arrays[i].dev_attr);
Guenter Roeckda2e0252010-08-14 21:08:55 +02001670 for (i = 0; i < ARRAY_SIZE(sda_sf3_max_step_arrays); i++) {
1671 struct sensor_device_attribute *attr =
1672 &sda_sf3_max_step_arrays[i];
Guenter Roeckec3e5a12011-02-02 08:46:49 -08001673 if (data->REG_FAN_STEP_OUTPUT &&
1674 data->REG_FAN_STEP_OUTPUT[attr->index] != 0xff)
Guenter Roeckda2e0252010-08-14 21:08:55 +02001675 device_remove_file(dev, &attr->dev_attr);
1676 }
David Hubbardc18beb52006-09-24 21:04:38 +02001677 for (i = 0; i < ARRAY_SIZE(sda_sf3_arrays_fan4); i++)
1678 device_remove_file(dev, &sda_sf3_arrays_fan4[i].dev_attr);
David Hubbard1ea6dd32007-06-24 11:16:15 +02001679 for (i = 0; i < data->in_num; i++) {
Gong Juna157d062009-03-30 21:46:43 +02001680 if ((i == 6) && data->in6_skip)
1681 continue;
David Hubbardc18beb52006-09-24 21:04:38 +02001682 device_remove_file(dev, &sda_in_input[i].dev_attr);
1683 device_remove_file(dev, &sda_in_alarm[i].dev_attr);
1684 device_remove_file(dev, &sda_in_min[i].dev_attr);
1685 device_remove_file(dev, &sda_in_max[i].dev_attr);
1686 }
1687 for (i = 0; i < 5; i++) {
1688 device_remove_file(dev, &sda_fan_input[i].dev_attr);
1689 device_remove_file(dev, &sda_fan_alarm[i].dev_attr);
1690 device_remove_file(dev, &sda_fan_div[i].dev_attr);
1691 device_remove_file(dev, &sda_fan_min[i].dev_attr);
1692 }
Gong Jun237c8d22009-03-30 21:46:42 +02001693 for (i = 0; i < data->pwm_num; i++) {
David Hubbardc18beb52006-09-24 21:04:38 +02001694 device_remove_file(dev, &sda_pwm[i].dev_attr);
1695 device_remove_file(dev, &sda_pwm_mode[i].dev_attr);
1696 device_remove_file(dev, &sda_pwm_enable[i].dev_attr);
1697 device_remove_file(dev, &sda_target_temp[i].dev_attr);
1698 device_remove_file(dev, &sda_tolerance[i].dev_attr);
1699 }
Guenter Roeckd36cf322011-02-07 15:08:54 -08001700 for (i = 0; i < NUM_REG_TEMP; i++) {
1701 if (!(data->have_temp & (1 << i)))
Gong Juna157d062009-03-30 21:46:43 +02001702 continue;
1703 device_remove_file(dev, &sda_temp_input[i].dev_attr);
Guenter Roeckd36cf322011-02-07 15:08:54 -08001704 device_remove_file(dev, &sda_temp_label[i].dev_attr);
Gong Juna157d062009-03-30 21:46:43 +02001705 device_remove_file(dev, &sda_temp_max[i].dev_attr);
1706 device_remove_file(dev, &sda_temp_max_hyst[i].dev_attr);
Guenter Roeckec3e5a12011-02-02 08:46:49 -08001707 if (i > 2)
1708 continue;
Gong Juna157d062009-03-30 21:46:43 +02001709 device_remove_file(dev, &sda_temp_alarm[i].dev_attr);
1710 device_remove_file(dev, &sda_temp_type[i].dev_attr);
1711 }
David Hubbard1ea6dd32007-06-24 11:16:15 +02001712
1713 device_remove_file(dev, &dev_attr_name);
Jean Delvarecbe311f2008-01-03 21:22:44 +01001714 device_remove_file(dev, &dev_attr_cpu0_vid);
David Hubbardc18beb52006-09-24 21:04:38 +02001715}
1716
David Hubbard1ea6dd32007-06-24 11:16:15 +02001717/* Get the monitoring functions started */
Jean Delvarebf164c52011-10-13 15:49:08 -04001718static inline void __devinit w83627ehf_init_device(struct w83627ehf_data *data,
1719 enum kinds kind)
Jean Delvare08e7e272005-04-25 22:43:25 +02001720{
1721 int i;
Jean Delvareda667362007-06-24 11:21:02 +02001722 u8 tmp, diode;
Jean Delvare08e7e272005-04-25 22:43:25 +02001723
1724 /* Start monitoring is needed */
David Hubbard1ea6dd32007-06-24 11:16:15 +02001725 tmp = w83627ehf_read_value(data, W83627EHF_REG_CONFIG);
Jean Delvare08e7e272005-04-25 22:43:25 +02001726 if (!(tmp & 0x01))
David Hubbard1ea6dd32007-06-24 11:16:15 +02001727 w83627ehf_write_value(data, W83627EHF_REG_CONFIG,
Jean Delvare08e7e272005-04-25 22:43:25 +02001728 tmp | 0x01);
1729
Guenter Roeckd36cf322011-02-07 15:08:54 -08001730 /* Enable temperature sensors if needed */
1731 for (i = 0; i < NUM_REG_TEMP; i++) {
1732 if (!(data->have_temp & (1 << i)))
1733 continue;
Guenter Roeckec3e5a12011-02-02 08:46:49 -08001734 if (!data->reg_temp_config[i])
Guenter Roeckd36cf322011-02-07 15:08:54 -08001735 continue;
David Hubbard1ea6dd32007-06-24 11:16:15 +02001736 tmp = w83627ehf_read_value(data,
Guenter Roeckec3e5a12011-02-02 08:46:49 -08001737 data->reg_temp_config[i]);
Jean Delvare08e7e272005-04-25 22:43:25 +02001738 if (tmp & 0x01)
David Hubbard1ea6dd32007-06-24 11:16:15 +02001739 w83627ehf_write_value(data,
Guenter Roeckec3e5a12011-02-02 08:46:49 -08001740 data->reg_temp_config[i],
Jean Delvare08e7e272005-04-25 22:43:25 +02001741 tmp & 0xfe);
1742 }
Jean Delvared3130f02007-06-24 11:20:13 +02001743
1744 /* Enable VBAT monitoring if needed */
1745 tmp = w83627ehf_read_value(data, W83627EHF_REG_VBAT);
1746 if (!(tmp & 0x01))
1747 w83627ehf_write_value(data, W83627EHF_REG_VBAT, tmp | 0x01);
Jean Delvareda667362007-06-24 11:21:02 +02001748
1749 /* Get thermal sensor types */
Jean Delvarebf164c52011-10-13 15:49:08 -04001750 switch (kind) {
1751 case w83627ehf:
1752 diode = w83627ehf_read_value(data, W83627EHF_REG_DIODE);
1753 break;
1754 default:
1755 diode = 0x70;
1756 }
Jean Delvareda667362007-06-24 11:21:02 +02001757 for (i = 0; i < 3; i++) {
1758 if ((tmp & (0x02 << i)))
Jean Delvarebf164c52011-10-13 15:49:08 -04001759 data->temp_type[i] = (diode & (0x10 << i)) ? 1 : 3;
Jean Delvareda667362007-06-24 11:21:02 +02001760 else
1761 data->temp_type[i] = 4; /* thermistor */
1762 }
Jean Delvare08e7e272005-04-25 22:43:25 +02001763}
1764
Guenter Roeckec3e5a12011-02-02 08:46:49 -08001765static void w82627ehf_swap_tempreg(struct w83627ehf_data *data,
1766 int r1, int r2)
1767{
1768 u16 tmp;
1769
1770 tmp = data->temp_src[r1];
1771 data->temp_src[r1] = data->temp_src[r2];
1772 data->temp_src[r2] = tmp;
1773
1774 tmp = data->reg_temp[r1];
1775 data->reg_temp[r1] = data->reg_temp[r2];
1776 data->reg_temp[r2] = tmp;
1777
1778 tmp = data->reg_temp_over[r1];
1779 data->reg_temp_over[r1] = data->reg_temp_over[r2];
1780 data->reg_temp_over[r2] = tmp;
1781
1782 tmp = data->reg_temp_hyst[r1];
1783 data->reg_temp_hyst[r1] = data->reg_temp_hyst[r2];
1784 data->reg_temp_hyst[r2] = tmp;
1785
1786 tmp = data->reg_temp_config[r1];
1787 data->reg_temp_config[r1] = data->reg_temp_config[r2];
1788 data->reg_temp_config[r2] = tmp;
1789}
1790
David Hubbard1ea6dd32007-06-24 11:16:15 +02001791static int __devinit w83627ehf_probe(struct platform_device *pdev)
Jean Delvare08e7e272005-04-25 22:43:25 +02001792{
David Hubbard1ea6dd32007-06-24 11:16:15 +02001793 struct device *dev = &pdev->dev;
1794 struct w83627ehf_sio_data *sio_data = dev->platform_data;
Jean Delvare08e7e272005-04-25 22:43:25 +02001795 struct w83627ehf_data *data;
David Hubbard1ea6dd32007-06-24 11:16:15 +02001796 struct resource *res;
Guenter Roeckec3e5a12011-02-02 08:46:49 -08001797 u8 fan3pin, fan4pin, fan4min, fan5pin, en_vrm10;
Jean Delvare08e7e272005-04-25 22:43:25 +02001798 int i, err = 0;
1799
David Hubbard1ea6dd32007-06-24 11:16:15 +02001800 res = platform_get_resource(pdev, IORESOURCE_IO, 0);
1801 if (!request_region(res->start, IOREGION_LENGTH, DRVNAME)) {
Jean Delvare08e7e272005-04-25 22:43:25 +02001802 err = -EBUSY;
David Hubbard1ea6dd32007-06-24 11:16:15 +02001803 dev_err(dev, "Failed to request region 0x%lx-0x%lx\n",
1804 (unsigned long)res->start,
1805 (unsigned long)res->start + IOREGION_LENGTH - 1);
Jean Delvare08e7e272005-04-25 22:43:25 +02001806 goto exit;
1807 }
1808
Guenter Roecke7e1ca62011-02-04 13:24:30 -08001809 data = kzalloc(sizeof(struct w83627ehf_data), GFP_KERNEL);
1810 if (!data) {
Jean Delvare08e7e272005-04-25 22:43:25 +02001811 err = -ENOMEM;
1812 goto exit_release;
1813 }
Jean Delvare08e7e272005-04-25 22:43:25 +02001814
David Hubbard1ea6dd32007-06-24 11:16:15 +02001815 data->addr = res->start;
Ingo Molnar9a61bf62006-01-18 23:19:26 +01001816 mutex_init(&data->lock);
Ingo Molnar9a61bf62006-01-18 23:19:26 +01001817 mutex_init(&data->update_lock);
David Hubbard1ea6dd32007-06-24 11:16:15 +02001818 data->name = w83627ehf_device_names[sio_data->kind];
1819 platform_set_drvdata(pdev, data);
Jean Delvare08e7e272005-04-25 22:43:25 +02001820
Gong Jun237c8d22009-03-30 21:46:42 +02001821 /* 627EHG and 627EHF have 10 voltage inputs; 627DHG and 667HG have 9 */
1822 data->in_num = (sio_data->kind == w83627ehf) ? 10 : 9;
Guenter Roeckec3e5a12011-02-02 08:46:49 -08001823 /* 667HG, NCT6775F, and NCT6776F have 3 pwms */
Guenter Roeckc39aeda2010-08-14 21:08:55 +02001824 data->pwm_num = (sio_data->kind == w83667hg
Guenter Roeckec3e5a12011-02-02 08:46:49 -08001825 || sio_data->kind == w83667hg_b
1826 || sio_data->kind == nct6775
1827 || sio_data->kind == nct6776) ? 3 : 4;
Jean Delvare08e7e272005-04-25 22:43:25 +02001828
Guenter Roeckd36cf322011-02-07 15:08:54 -08001829 data->have_temp = 0x07;
Gong Juna157d062009-03-30 21:46:43 +02001830 /* Check temp3 configuration bit for 667HG */
Guenter Roeckd36cf322011-02-07 15:08:54 -08001831 if (sio_data->kind == w83667hg) {
1832 u8 reg;
1833
1834 reg = w83627ehf_read_value(data, W83627EHF_REG_TEMP_CONFIG[2]);
1835 if (reg & 0x01)
1836 data->have_temp &= ~(1 << 2);
1837 else
Guenter Roeckec3e5a12011-02-02 08:46:49 -08001838 data->in6_skip = 1; /* either temp3 or in6 */
1839 }
1840
1841 /* Deal with temperature register setup first. */
1842 if (sio_data->kind == nct6775 || sio_data->kind == nct6776) {
1843 int mask = 0;
1844
1845 /*
1846 * Display temperature sensor output only if it monitors
1847 * a source other than one already reported. Always display
1848 * first three temperature registers, though.
1849 */
1850 for (i = 0; i < NUM_REG_TEMP; i++) {
1851 u8 src;
1852
1853 data->reg_temp[i] = NCT6775_REG_TEMP[i];
1854 data->reg_temp_over[i] = NCT6775_REG_TEMP_OVER[i];
1855 data->reg_temp_hyst[i] = NCT6775_REG_TEMP_HYST[i];
1856 data->reg_temp_config[i] = NCT6775_REG_TEMP_CONFIG[i];
1857
1858 src = w83627ehf_read_value(data,
1859 NCT6775_REG_TEMP_SOURCE[i]);
1860 src &= 0x1f;
1861 if (src && !(mask & (1 << src))) {
1862 data->have_temp |= 1 << i;
1863 mask |= 1 << src;
1864 }
1865
1866 data->temp_src[i] = src;
1867
1868 /*
1869 * Now do some register swapping if index 0..2 don't
1870 * point to SYSTIN(1), CPUIN(2), and AUXIN(3).
1871 * Idea is to have the first three attributes
1872 * report SYSTIN, CPUIN, and AUXIN if possible
1873 * without overriding the basic system configuration.
1874 */
1875 if (i > 0 && data->temp_src[0] != 1
1876 && data->temp_src[i] == 1)
1877 w82627ehf_swap_tempreg(data, 0, i);
1878 if (i > 1 && data->temp_src[1] != 2
1879 && data->temp_src[i] == 2)
1880 w82627ehf_swap_tempreg(data, 1, i);
1881 if (i > 2 && data->temp_src[2] != 3
1882 && data->temp_src[i] == 3)
1883 w82627ehf_swap_tempreg(data, 2, i);
1884 }
1885 if (sio_data->kind == nct6776) {
1886 /*
1887 * On NCT6776, AUXTIN and VIN3 pins are shared.
1888 * Only way to detect it is to check if AUXTIN is used
1889 * as a temperature source, and if that source is
1890 * enabled.
1891 *
1892 * If that is the case, disable in6, which reports VIN3.
1893 * Otherwise disable temp3.
1894 */
1895 if (data->temp_src[2] == 3) {
1896 u8 reg;
1897
1898 if (data->reg_temp_config[2])
1899 reg = w83627ehf_read_value(data,
1900 data->reg_temp_config[2]);
1901 else
1902 reg = 0; /* Assume AUXTIN is used */
1903
1904 if (reg & 0x01)
1905 data->have_temp &= ~(1 << 2);
1906 else
1907 data->in6_skip = 1;
1908 }
Guenter Roeck02309ad2011-03-10 15:47:14 -08001909 data->temp_label = nct6776_temp_label;
1910 } else {
1911 data->temp_label = nct6775_temp_label;
Guenter Roeckec3e5a12011-02-02 08:46:49 -08001912 }
Guenter Roeckd36cf322011-02-07 15:08:54 -08001913 } else if (sio_data->kind == w83667hg_b) {
1914 u8 reg;
1915
Guenter Roeckec3e5a12011-02-02 08:46:49 -08001916 /*
1917 * Temperature sources are selected with bank 0, registers 0x49
1918 * and 0x4a.
1919 */
1920 for (i = 0; i < ARRAY_SIZE(W83627EHF_REG_TEMP); i++) {
1921 data->reg_temp[i] = W83627EHF_REG_TEMP[i];
1922 data->reg_temp_over[i] = W83627EHF_REG_TEMP_OVER[i];
1923 data->reg_temp_hyst[i] = W83627EHF_REG_TEMP_HYST[i];
1924 data->reg_temp_config[i] = W83627EHF_REG_TEMP_CONFIG[i];
1925 }
Guenter Roeckd36cf322011-02-07 15:08:54 -08001926 reg = w83627ehf_read_value(data, 0x4a);
1927 data->temp_src[0] = reg >> 5;
1928 reg = w83627ehf_read_value(data, 0x49);
1929 data->temp_src[1] = reg & 0x07;
Guenter Roeckec3e5a12011-02-02 08:46:49 -08001930 data->temp_src[2] = (reg >> 4) & 0x07;
Guenter Roeckd36cf322011-02-07 15:08:54 -08001931
1932 /*
1933 * W83667HG-B has another temperature register at 0x7e.
1934 * The temperature source is selected with register 0x7d.
1935 * Support it if the source differs from already reported
1936 * sources.
1937 */
1938 reg = w83627ehf_read_value(data, 0x7d);
1939 reg &= 0x07;
1940 if (reg != data->temp_src[0] && reg != data->temp_src[1]
1941 && reg != data->temp_src[2]) {
1942 data->temp_src[3] = reg;
1943 data->have_temp |= 1 << 3;
1944 }
1945
1946 /*
1947 * Chip supports either AUXTIN or VIN3. Try to find out which
1948 * one.
1949 */
1950 reg = w83627ehf_read_value(data, W83627EHF_REG_TEMP_CONFIG[2]);
1951 if (data->temp_src[2] == 2 && (reg & 0x01))
1952 data->have_temp &= ~(1 << 2);
1953
1954 if ((data->temp_src[2] == 2 && (data->have_temp & (1 << 2)))
1955 || (data->temp_src[3] == 2 && (data->have_temp & (1 << 3))))
1956 data->in6_skip = 1;
1957
1958 data->temp_label = w83667hg_b_temp_label;
Guenter Roeckec3e5a12011-02-02 08:46:49 -08001959 } else {
1960 /* Temperature sources are fixed */
1961 for (i = 0; i < 3; i++) {
1962 data->reg_temp[i] = W83627EHF_REG_TEMP[i];
1963 data->reg_temp_over[i] = W83627EHF_REG_TEMP_OVER[i];
1964 data->reg_temp_hyst[i] = W83627EHF_REG_TEMP_HYST[i];
1965 data->reg_temp_config[i] = W83627EHF_REG_TEMP_CONFIG[i];
1966 }
Gong Juna157d062009-03-30 21:46:43 +02001967 }
1968
Guenter Roeckec3e5a12011-02-02 08:46:49 -08001969 if (sio_data->kind == nct6775) {
Guenter Roeck26bc4402011-02-11 08:00:58 -08001970 data->has_fan_div = true;
1971 data->fan_from_reg = fan_from_reg16;
1972 data->fan_from_reg_min = fan_from_reg8;
Guenter Roeckec3e5a12011-02-02 08:46:49 -08001973 data->REG_PWM = NCT6775_REG_PWM;
1974 data->REG_TARGET = NCT6775_REG_TARGET;
Guenter Roeck26bc4402011-02-11 08:00:58 -08001975 data->REG_FAN = NCT6775_REG_FAN;
Guenter Roeckec3e5a12011-02-02 08:46:49 -08001976 data->REG_FAN_MIN = W83627EHF_REG_FAN_MIN;
1977 data->REG_FAN_START_OUTPUT = NCT6775_REG_FAN_START_OUTPUT;
1978 data->REG_FAN_STOP_OUTPUT = NCT6775_REG_FAN_STOP_OUTPUT;
1979 data->REG_FAN_STOP_TIME = NCT6775_REG_FAN_STOP_TIME;
1980 data->REG_FAN_MAX_OUTPUT = NCT6775_REG_FAN_MAX_OUTPUT;
1981 data->REG_FAN_STEP_OUTPUT = NCT6775_REG_FAN_STEP_OUTPUT;
1982 } else if (sio_data->kind == nct6776) {
Guenter Roeck26bc4402011-02-11 08:00:58 -08001983 data->has_fan_div = false;
1984 data->fan_from_reg = fan_from_reg13;
1985 data->fan_from_reg_min = fan_from_reg13;
Guenter Roeckec3e5a12011-02-02 08:46:49 -08001986 data->REG_PWM = NCT6775_REG_PWM;
1987 data->REG_TARGET = NCT6775_REG_TARGET;
Guenter Roeck26bc4402011-02-11 08:00:58 -08001988 data->REG_FAN = NCT6775_REG_FAN;
Guenter Roeckec3e5a12011-02-02 08:46:49 -08001989 data->REG_FAN_MIN = NCT6776_REG_FAN_MIN;
1990 data->REG_FAN_START_OUTPUT = NCT6775_REG_FAN_START_OUTPUT;
1991 data->REG_FAN_STOP_OUTPUT = NCT6775_REG_FAN_STOP_OUTPUT;
1992 data->REG_FAN_STOP_TIME = NCT6775_REG_FAN_STOP_TIME;
1993 } else if (sio_data->kind == w83667hg_b) {
Guenter Roeck26bc4402011-02-11 08:00:58 -08001994 data->has_fan_div = true;
1995 data->fan_from_reg = fan_from_reg8;
1996 data->fan_from_reg_min = fan_from_reg8;
Guenter Roeckec3e5a12011-02-02 08:46:49 -08001997 data->REG_PWM = W83627EHF_REG_PWM;
1998 data->REG_TARGET = W83627EHF_REG_TARGET;
1999 data->REG_FAN = W83627EHF_REG_FAN;
2000 data->REG_FAN_MIN = W83627EHF_REG_FAN_MIN;
2001 data->REG_FAN_START_OUTPUT = W83627EHF_REG_FAN_START_OUTPUT;
2002 data->REG_FAN_STOP_OUTPUT = W83627EHF_REG_FAN_STOP_OUTPUT;
2003 data->REG_FAN_STOP_TIME = W83627EHF_REG_FAN_STOP_TIME;
Guenter Roeckc39aeda2010-08-14 21:08:55 +02002004 data->REG_FAN_MAX_OUTPUT =
2005 W83627EHF_REG_FAN_MAX_OUTPUT_W83667_B;
2006 data->REG_FAN_STEP_OUTPUT =
2007 W83627EHF_REG_FAN_STEP_OUTPUT_W83667_B;
2008 } else {
Guenter Roeck26bc4402011-02-11 08:00:58 -08002009 data->has_fan_div = true;
2010 data->fan_from_reg = fan_from_reg8;
2011 data->fan_from_reg_min = fan_from_reg8;
Guenter Roeckec3e5a12011-02-02 08:46:49 -08002012 data->REG_PWM = W83627EHF_REG_PWM;
2013 data->REG_TARGET = W83627EHF_REG_TARGET;
2014 data->REG_FAN = W83627EHF_REG_FAN;
2015 data->REG_FAN_MIN = W83627EHF_REG_FAN_MIN;
2016 data->REG_FAN_START_OUTPUT = W83627EHF_REG_FAN_START_OUTPUT;
2017 data->REG_FAN_STOP_OUTPUT = W83627EHF_REG_FAN_STOP_OUTPUT;
2018 data->REG_FAN_STOP_TIME = W83627EHF_REG_FAN_STOP_TIME;
Guenter Roeckc39aeda2010-08-14 21:08:55 +02002019 data->REG_FAN_MAX_OUTPUT =
2020 W83627EHF_REG_FAN_MAX_OUTPUT_COMMON;
2021 data->REG_FAN_STEP_OUTPUT =
2022 W83627EHF_REG_FAN_STEP_OUTPUT_COMMON;
2023 }
Guenter Roeckda2e0252010-08-14 21:08:55 +02002024
Jean Delvare08e7e272005-04-25 22:43:25 +02002025 /* Initialize the chip */
Jean Delvarebf164c52011-10-13 15:49:08 -04002026 w83627ehf_init_device(data, sio_data->kind);
Jean Delvare08e7e272005-04-25 22:43:25 +02002027
Jean Delvarefc18d6c2007-06-24 11:19:42 +02002028 data->vrm = vid_which_vrm();
2029 superio_enter(sio_data->sioreg);
Jean Delvarefc18d6c2007-06-24 11:19:42 +02002030 /* Read VID value */
Guenter Roeckec3e5a12011-02-02 08:46:49 -08002031 if (sio_data->kind == w83667hg || sio_data->kind == w83667hg_b ||
2032 sio_data->kind == nct6775 || sio_data->kind == nct6776) {
Gong Jun237c8d22009-03-30 21:46:42 +02002033 /* W83667HG has different pins for VID input and output, so
2034 we can get the VID input values directly at logical device D
2035 0xe3. */
2036 superio_select(sio_data->sioreg, W83667HG_LD_VID);
2037 data->vid = superio_inb(sio_data->sioreg, 0xe3);
Jean Delvarecbe311f2008-01-03 21:22:44 +01002038 err = device_create_file(dev, &dev_attr_cpu0_vid);
2039 if (err)
2040 goto exit_release;
Jean Delvare58e6e782008-01-03 07:33:31 -05002041 } else {
Gong Jun237c8d22009-03-30 21:46:42 +02002042 superio_select(sio_data->sioreg, W83627EHF_LD_HWM);
2043 if (superio_inb(sio_data->sioreg, SIO_REG_VID_CTRL) & 0x80) {
2044 /* Set VID input sensibility if needed. In theory the
2045 BIOS should have set it, but in practice it's not
2046 always the case. We only do it for the W83627EHF/EHG
2047 because the W83627DHG is more complex in this
2048 respect. */
2049 if (sio_data->kind == w83627ehf) {
2050 en_vrm10 = superio_inb(sio_data->sioreg,
2051 SIO_REG_EN_VRM10);
2052 if ((en_vrm10 & 0x08) && data->vrm == 90) {
2053 dev_warn(dev, "Setting VID input "
2054 "voltage to TTL\n");
2055 superio_outb(sio_data->sioreg,
2056 SIO_REG_EN_VRM10,
2057 en_vrm10 & ~0x08);
2058 } else if (!(en_vrm10 & 0x08)
2059 && data->vrm == 100) {
2060 dev_warn(dev, "Setting VID input "
2061 "voltage to VRM10\n");
2062 superio_outb(sio_data->sioreg,
2063 SIO_REG_EN_VRM10,
2064 en_vrm10 | 0x08);
2065 }
2066 }
2067
2068 data->vid = superio_inb(sio_data->sioreg,
2069 SIO_REG_VID_DATA);
2070 if (sio_data->kind == w83627ehf) /* 6 VID pins only */
2071 data->vid &= 0x3f;
2072
2073 err = device_create_file(dev, &dev_attr_cpu0_vid);
2074 if (err)
2075 goto exit_release;
2076 } else {
2077 dev_info(dev, "VID pins in output mode, CPU VID not "
2078 "available\n");
2079 }
Jean Delvarefc18d6c2007-06-24 11:19:42 +02002080 }
2081
Rudolf Marek08c79952006-07-05 18:14:31 +02002082 /* fan4 and fan5 share some pins with the GPIO and serial flash */
Guenter Roeckec3e5a12011-02-02 08:46:49 -08002083 if (sio_data->kind == nct6775) {
2084 /* On NCT6775, fan4 shares pins with the fdc interface */
2085 fan3pin = 1;
2086 fan4pin = !(superio_inb(sio_data->sioreg, 0x2A) & 0x80);
2087 fan4min = 0;
2088 fan5pin = 0;
2089 } else if (sio_data->kind == nct6776) {
2090 fan3pin = !(superio_inb(sio_data->sioreg, 0x24) & 0x40);
2091 fan4pin = !!(superio_inb(sio_data->sioreg, 0x1C) & 0x01);
2092 fan5pin = !!(superio_inb(sio_data->sioreg, 0x1C) & 0x02);
2093 fan4min = fan4pin;
2094 } else if (sio_data->kind == w83667hg || sio_data->kind == w83667hg_b) {
2095 fan3pin = 1;
Gong Jun237c8d22009-03-30 21:46:42 +02002096 fan4pin = superio_inb(sio_data->sioreg, 0x27) & 0x40;
Guenter Roeckec3e5a12011-02-02 08:46:49 -08002097 fan5pin = superio_inb(sio_data->sioreg, 0x27) & 0x20;
2098 fan4min = fan4pin;
Gong Jun237c8d22009-03-30 21:46:42 +02002099 } else {
Guenter Roeckec3e5a12011-02-02 08:46:49 -08002100 fan3pin = 1;
Gong Jun237c8d22009-03-30 21:46:42 +02002101 fan4pin = !(superio_inb(sio_data->sioreg, 0x29) & 0x06);
Guenter Roeckec3e5a12011-02-02 08:46:49 -08002102 fan5pin = !(superio_inb(sio_data->sioreg, 0x24) & 0x02);
2103 fan4min = fan4pin;
Gong Jun237c8d22009-03-30 21:46:42 +02002104 }
Ian Dobsond42e8692011-03-07 14:21:12 -08002105
2106 if (fan_debounce &&
2107 (sio_data->kind == nct6775 || sio_data->kind == nct6776)) {
2108 u8 tmp;
2109
2110 superio_select(sio_data->sioreg, W83627EHF_LD_HWM);
2111 tmp = superio_inb(sio_data->sioreg, NCT6775_REG_FAN_DEBOUNCE);
2112 if (sio_data->kind == nct6776)
2113 superio_outb(sio_data->sioreg, NCT6775_REG_FAN_DEBOUNCE,
2114 0x3e | tmp);
2115 else
2116 superio_outb(sio_data->sioreg, NCT6775_REG_FAN_DEBOUNCE,
2117 0x1e | tmp);
2118 pr_info("Enabled fan debounce for chip %s\n", data->name);
2119 }
2120
David Hubbard1ea6dd32007-06-24 11:16:15 +02002121 superio_exit(sio_data->sioreg);
Rudolf Marek08c79952006-07-05 18:14:31 +02002122
Jean Delvare08e7e272005-04-25 22:43:25 +02002123 /* It looks like fan4 and fan5 pins can be alternatively used
Rudolf Marek14992c72006-10-08 22:02:09 +02002124 as fan on/off switches, but fan5 control is write only :/
2125 We assume that if the serial interface is disabled, designers
2126 connected fan5 as input unless they are emitting log 1, which
2127 is not the default. */
Rudolf Marek08c79952006-07-05 18:14:31 +02002128
Guenter Roeckec3e5a12011-02-02 08:46:49 -08002129 data->has_fan = data->has_fan_min = 0x03; /* fan1 and fan2 */
2130
2131 data->has_fan |= (fan3pin << 2);
2132 data->has_fan_min |= (fan3pin << 2);
2133
2134 /*
2135 * NCT6775F and NCT6776F don't have the W83627EHF_REG_FANDIV1 register
2136 */
2137 if (sio_data->kind == nct6775 || sio_data->kind == nct6776) {
2138 data->has_fan |= (fan4pin << 3) | (fan5pin << 4);
2139 data->has_fan_min |= (fan4min << 3) | (fan5pin << 4);
2140 } else {
2141 i = w83627ehf_read_value(data, W83627EHF_REG_FANDIV1);
2142 if ((i & (1 << 2)) && fan4pin) {
2143 data->has_fan |= (1 << 3);
2144 data->has_fan_min |= (1 << 3);
2145 }
2146 if (!(i & (1 << 1)) && fan5pin) {
2147 data->has_fan |= (1 << 4);
2148 data->has_fan_min |= (1 << 4);
2149 }
2150 }
Jean Delvare08e7e272005-04-25 22:43:25 +02002151
Mark M. Hoffmanea7be662007-08-05 12:19:01 -04002152 /* Read fan clock dividers immediately */
Guenter Roeckec3e5a12011-02-02 08:46:49 -08002153 w83627ehf_update_fan_div_common(dev, data);
2154
2155 /* Read pwm data to save original values */
2156 w83627ehf_update_pwm_common(dev, data);
2157 for (i = 0; i < data->pwm_num; i++)
2158 data->pwm_enable_orig[i] = data->pwm_enable[i];
Mark M. Hoffmanea7be662007-08-05 12:19:01 -04002159
Guenter Roeckb84bb512011-02-13 23:01:25 -08002160 /* Read pwm data to save original values */
2161 w83627ehf_update_pwm_common(dev, data);
2162 for (i = 0; i < data->pwm_num; i++)
2163 data->pwm_enable_orig[i] = data->pwm_enable[i];
2164
Jean Delvare08e7e272005-04-25 22:43:25 +02002165 /* Register sysfs hooks */
Guenter Roecke7e1ca62011-02-04 13:24:30 -08002166 for (i = 0; i < ARRAY_SIZE(sda_sf3_arrays); i++) {
2167 err = device_create_file(dev, &sda_sf3_arrays[i].dev_attr);
2168 if (err)
David Hubbardc18beb52006-09-24 21:04:38 +02002169 goto exit_remove;
Guenter Roecke7e1ca62011-02-04 13:24:30 -08002170 }
Rudolf Marek08c79952006-07-05 18:14:31 +02002171
Guenter Roeckda2e0252010-08-14 21:08:55 +02002172 for (i = 0; i < ARRAY_SIZE(sda_sf3_max_step_arrays); i++) {
2173 struct sensor_device_attribute *attr =
2174 &sda_sf3_max_step_arrays[i];
Guenter Roeckec3e5a12011-02-02 08:46:49 -08002175 if (data->REG_FAN_STEP_OUTPUT &&
2176 data->REG_FAN_STEP_OUTPUT[attr->index] != 0xff) {
Guenter Roeckda2e0252010-08-14 21:08:55 +02002177 err = device_create_file(dev, &attr->dev_attr);
2178 if (err)
2179 goto exit_remove;
2180 }
2181 }
Rudolf Marek08c79952006-07-05 18:14:31 +02002182 /* if fan4 is enabled create the sf3 files for it */
Gong Jun237c8d22009-03-30 21:46:42 +02002183 if ((data->has_fan & (1 << 3)) && data->pwm_num >= 4)
David Hubbardc18beb52006-09-24 21:04:38 +02002184 for (i = 0; i < ARRAY_SIZE(sda_sf3_arrays_fan4); i++) {
Guenter Roecke7e1ca62011-02-04 13:24:30 -08002185 err = device_create_file(dev,
2186 &sda_sf3_arrays_fan4[i].dev_attr);
2187 if (err)
David Hubbardc18beb52006-09-24 21:04:38 +02002188 goto exit_remove;
2189 }
Rudolf Marek08c79952006-07-05 18:14:31 +02002190
Gong Juna157d062009-03-30 21:46:43 +02002191 for (i = 0; i < data->in_num; i++) {
2192 if ((i == 6) && data->in6_skip)
2193 continue;
David Hubbardc18beb52006-09-24 21:04:38 +02002194 if ((err = device_create_file(dev, &sda_in_input[i].dev_attr))
2195 || (err = device_create_file(dev,
2196 &sda_in_alarm[i].dev_attr))
2197 || (err = device_create_file(dev,
2198 &sda_in_min[i].dev_attr))
2199 || (err = device_create_file(dev,
2200 &sda_in_max[i].dev_attr)))
2201 goto exit_remove;
Gong Juna157d062009-03-30 21:46:43 +02002202 }
Rudolf Marekcf0676f2006-03-23 16:25:22 +01002203
Yuan Mu412fec82006-02-05 23:24:16 +01002204 for (i = 0; i < 5; i++) {
Rudolf Marek08c79952006-07-05 18:14:31 +02002205 if (data->has_fan & (1 << i)) {
David Hubbardc18beb52006-09-24 21:04:38 +02002206 if ((err = device_create_file(dev,
2207 &sda_fan_input[i].dev_attr))
2208 || (err = device_create_file(dev,
Guenter Roeckec3e5a12011-02-02 08:46:49 -08002209 &sda_fan_alarm[i].dev_attr)))
David Hubbardc18beb52006-09-24 21:04:38 +02002210 goto exit_remove;
Guenter Roeckec3e5a12011-02-02 08:46:49 -08002211 if (sio_data->kind != nct6776) {
2212 err = device_create_file(dev,
2213 &sda_fan_div[i].dev_attr);
2214 if (err)
2215 goto exit_remove;
2216 }
2217 if (data->has_fan_min & (1 << i)) {
2218 err = device_create_file(dev,
2219 &sda_fan_min[i].dev_attr);
2220 if (err)
2221 goto exit_remove;
2222 }
Gong Jun237c8d22009-03-30 21:46:42 +02002223 if (i < data->pwm_num &&
David Hubbardc18beb52006-09-24 21:04:38 +02002224 ((err = device_create_file(dev,
2225 &sda_pwm[i].dev_attr))
2226 || (err = device_create_file(dev,
2227 &sda_pwm_mode[i].dev_attr))
2228 || (err = device_create_file(dev,
2229 &sda_pwm_enable[i].dev_attr))
2230 || (err = device_create_file(dev,
2231 &sda_target_temp[i].dev_attr))
2232 || (err = device_create_file(dev,
2233 &sda_tolerance[i].dev_attr))))
2234 goto exit_remove;
Rudolf Marek08c79952006-07-05 18:14:31 +02002235 }
Jean Delvare08e7e272005-04-25 22:43:25 +02002236 }
Rudolf Marek08c79952006-07-05 18:14:31 +02002237
Guenter Roeckd36cf322011-02-07 15:08:54 -08002238 for (i = 0; i < NUM_REG_TEMP; i++) {
2239 if (!(data->have_temp & (1 << i)))
Gong Juna157d062009-03-30 21:46:43 +02002240 continue;
Guenter Roeckd36cf322011-02-07 15:08:54 -08002241 err = device_create_file(dev, &sda_temp_input[i].dev_attr);
2242 if (err)
2243 goto exit_remove;
2244 if (data->temp_label) {
2245 err = device_create_file(dev,
2246 &sda_temp_label[i].dev_attr);
2247 if (err)
2248 goto exit_remove;
2249 }
Guenter Roeckec3e5a12011-02-02 08:46:49 -08002250 if (data->reg_temp_over[i]) {
2251 err = device_create_file(dev,
2252 &sda_temp_max[i].dev_attr);
2253 if (err)
2254 goto exit_remove;
2255 }
2256 if (data->reg_temp_hyst[i]) {
2257 err = device_create_file(dev,
2258 &sda_temp_max_hyst[i].dev_attr);
2259 if (err)
2260 goto exit_remove;
2261 }
Guenter Roeckd36cf322011-02-07 15:08:54 -08002262 if (i > 2)
Guenter Roeckec3e5a12011-02-02 08:46:49 -08002263 continue;
2264 if ((err = device_create_file(dev,
Gong Juna157d062009-03-30 21:46:43 +02002265 &sda_temp_alarm[i].dev_attr))
2266 || (err = device_create_file(dev,
2267 &sda_temp_type[i].dev_attr)))
David Hubbardc18beb52006-09-24 21:04:38 +02002268 goto exit_remove;
Gong Juna157d062009-03-30 21:46:43 +02002269 }
David Hubbardc18beb52006-09-24 21:04:38 +02002270
David Hubbard1ea6dd32007-06-24 11:16:15 +02002271 err = device_create_file(dev, &dev_attr_name);
2272 if (err)
2273 goto exit_remove;
2274
Tony Jones1beeffe2007-08-20 13:46:20 -07002275 data->hwmon_dev = hwmon_device_register(dev);
2276 if (IS_ERR(data->hwmon_dev)) {
2277 err = PTR_ERR(data->hwmon_dev);
David Hubbardc18beb52006-09-24 21:04:38 +02002278 goto exit_remove;
2279 }
Jean Delvare08e7e272005-04-25 22:43:25 +02002280
2281 return 0;
2282
David Hubbardc18beb52006-09-24 21:04:38 +02002283exit_remove:
2284 w83627ehf_device_remove_files(dev);
Jean Delvare08e7e272005-04-25 22:43:25 +02002285 kfree(data);
David Hubbard1ea6dd32007-06-24 11:16:15 +02002286 platform_set_drvdata(pdev, NULL);
Jean Delvare08e7e272005-04-25 22:43:25 +02002287exit_release:
David Hubbard1ea6dd32007-06-24 11:16:15 +02002288 release_region(res->start, IOREGION_LENGTH);
Jean Delvare08e7e272005-04-25 22:43:25 +02002289exit:
2290 return err;
2291}
2292
David Hubbard1ea6dd32007-06-24 11:16:15 +02002293static int __devexit w83627ehf_remove(struct platform_device *pdev)
Jean Delvare08e7e272005-04-25 22:43:25 +02002294{
David Hubbard1ea6dd32007-06-24 11:16:15 +02002295 struct w83627ehf_data *data = platform_get_drvdata(pdev);
Jean Delvare08e7e272005-04-25 22:43:25 +02002296
Tony Jones1beeffe2007-08-20 13:46:20 -07002297 hwmon_device_unregister(data->hwmon_dev);
David Hubbard1ea6dd32007-06-24 11:16:15 +02002298 w83627ehf_device_remove_files(&pdev->dev);
2299 release_region(data->addr, IOREGION_LENGTH);
2300 platform_set_drvdata(pdev, NULL);
Mark M. Hoffman943b0832005-07-15 21:39:18 -04002301 kfree(data);
Jean Delvare08e7e272005-04-25 22:43:25 +02002302
2303 return 0;
2304}
2305
David Hubbard1ea6dd32007-06-24 11:16:15 +02002306static struct platform_driver w83627ehf_driver = {
Laurent Riffardcdaf7932005-11-26 20:37:41 +01002307 .driver = {
Jean Delvare87218842006-09-03 22:36:14 +02002308 .owner = THIS_MODULE,
David Hubbard1ea6dd32007-06-24 11:16:15 +02002309 .name = DRVNAME,
Laurent Riffardcdaf7932005-11-26 20:37:41 +01002310 },
David Hubbard1ea6dd32007-06-24 11:16:15 +02002311 .probe = w83627ehf_probe,
2312 .remove = __devexit_p(w83627ehf_remove),
Jean Delvare08e7e272005-04-25 22:43:25 +02002313};
2314
David Hubbard1ea6dd32007-06-24 11:16:15 +02002315/* w83627ehf_find() looks for a '627 in the Super-I/O config space */
2316static int __init w83627ehf_find(int sioaddr, unsigned short *addr,
2317 struct w83627ehf_sio_data *sio_data)
Jean Delvare08e7e272005-04-25 22:43:25 +02002318{
David Hubbard1ea6dd32007-06-24 11:16:15 +02002319 static const char __initdata sio_name_W83627EHF[] = "W83627EHF";
2320 static const char __initdata sio_name_W83627EHG[] = "W83627EHG";
2321 static const char __initdata sio_name_W83627DHG[] = "W83627DHG";
Jean Delvarec1e48dc2009-06-15 18:39:50 +02002322 static const char __initdata sio_name_W83627DHG_P[] = "W83627DHG-P";
Gong Jun237c8d22009-03-30 21:46:42 +02002323 static const char __initdata sio_name_W83667HG[] = "W83667HG";
Guenter Roeckc39aeda2010-08-14 21:08:55 +02002324 static const char __initdata sio_name_W83667HG_B[] = "W83667HG-B";
Guenter Roeckec3e5a12011-02-02 08:46:49 -08002325 static const char __initdata sio_name_NCT6775[] = "NCT6775F";
2326 static const char __initdata sio_name_NCT6776[] = "NCT6776F";
David Hubbard1ea6dd32007-06-24 11:16:15 +02002327
Jean Delvare08e7e272005-04-25 22:43:25 +02002328 u16 val;
David Hubbard1ea6dd32007-06-24 11:16:15 +02002329 const char *sio_name;
Jean Delvare08e7e272005-04-25 22:43:25 +02002330
David Hubbard1ea6dd32007-06-24 11:16:15 +02002331 superio_enter(sioaddr);
Jean Delvare08e7e272005-04-25 22:43:25 +02002332
Jean Delvare67b671b2007-12-06 23:13:42 +01002333 if (force_id)
2334 val = force_id;
2335 else
2336 val = (superio_inb(sioaddr, SIO_REG_DEVID) << 8)
2337 | superio_inb(sioaddr, SIO_REG_DEVID + 1);
David Hubbard657c93b2007-02-14 21:15:04 +01002338 switch (val & SIO_ID_MASK) {
David Hubbard657c93b2007-02-14 21:15:04 +01002339 case SIO_W83627EHF_ID:
David Hubbard1ea6dd32007-06-24 11:16:15 +02002340 sio_data->kind = w83627ehf;
2341 sio_name = sio_name_W83627EHF;
2342 break;
David Hubbard657c93b2007-02-14 21:15:04 +01002343 case SIO_W83627EHG_ID:
David Hubbard1ea6dd32007-06-24 11:16:15 +02002344 sio_data->kind = w83627ehf;
2345 sio_name = sio_name_W83627EHG;
2346 break;
2347 case SIO_W83627DHG_ID:
2348 sio_data->kind = w83627dhg;
2349 sio_name = sio_name_W83627DHG;
David Hubbard657c93b2007-02-14 21:15:04 +01002350 break;
Jean Delvarec1e48dc2009-06-15 18:39:50 +02002351 case SIO_W83627DHG_P_ID:
2352 sio_data->kind = w83627dhg_p;
2353 sio_name = sio_name_W83627DHG_P;
2354 break;
Gong Jun237c8d22009-03-30 21:46:42 +02002355 case SIO_W83667HG_ID:
2356 sio_data->kind = w83667hg;
2357 sio_name = sio_name_W83667HG;
2358 break;
Guenter Roeckc39aeda2010-08-14 21:08:55 +02002359 case SIO_W83667HG_B_ID:
2360 sio_data->kind = w83667hg_b;
2361 sio_name = sio_name_W83667HG_B;
2362 break;
Guenter Roeckec3e5a12011-02-02 08:46:49 -08002363 case SIO_NCT6775_ID:
2364 sio_data->kind = nct6775;
2365 sio_name = sio_name_NCT6775;
2366 break;
2367 case SIO_NCT6776_ID:
2368 sio_data->kind = nct6776;
2369 sio_name = sio_name_NCT6776;
2370 break;
David Hubbard657c93b2007-02-14 21:15:04 +01002371 default:
Jean Delvare9f660362007-06-24 11:23:41 +02002372 if (val != 0xffff)
Joe Perchesabdc6fd2010-10-20 06:51:54 +00002373 pr_debug("unsupported chip ID: 0x%04x\n", val);
David Hubbard1ea6dd32007-06-24 11:16:15 +02002374 superio_exit(sioaddr);
Jean Delvare08e7e272005-04-25 22:43:25 +02002375 return -ENODEV;
2376 }
2377
David Hubbard1ea6dd32007-06-24 11:16:15 +02002378 /* We have a known chip, find the HWM I/O address */
2379 superio_select(sioaddr, W83627EHF_LD_HWM);
2380 val = (superio_inb(sioaddr, SIO_REG_ADDR) << 8)
2381 | superio_inb(sioaddr, SIO_REG_ADDR + 1);
Jean Delvare1a641fc2007-04-23 14:41:16 -07002382 *addr = val & IOREGION_ALIGNMENT;
Jean Delvare2d8672c2005-07-19 23:56:35 +02002383 if (*addr == 0) {
Joe Perchesabdc6fd2010-10-20 06:51:54 +00002384 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 +02002385 superio_exit(sioaddr);
Jean Delvare08e7e272005-04-25 22:43:25 +02002386 return -ENODEV;
2387 }
2388
2389 /* Activate logical device if needed */
David Hubbard1ea6dd32007-06-24 11:16:15 +02002390 val = superio_inb(sioaddr, SIO_REG_ENABLE);
David Hubbard475ef852007-06-24 11:17:09 +02002391 if (!(val & 0x01)) {
Guenter Roecke7e1ca62011-02-04 13:24:30 -08002392 pr_warn("Forcibly enabling Super-I/O. "
2393 "Sensor is probably unusable.\n");
David Hubbard1ea6dd32007-06-24 11:16:15 +02002394 superio_outb(sioaddr, SIO_REG_ENABLE, val | 0x01);
David Hubbard475ef852007-06-24 11:17:09 +02002395 }
Jean Delvare08e7e272005-04-25 22:43:25 +02002396
David Hubbard1ea6dd32007-06-24 11:16:15 +02002397 superio_exit(sioaddr);
Joe Perchesabdc6fd2010-10-20 06:51:54 +00002398 pr_info("Found %s chip at %#x\n", sio_name, *addr);
David Hubbard1ea6dd32007-06-24 11:16:15 +02002399 sio_data->sioreg = sioaddr;
2400
Jean Delvare08e7e272005-04-25 22:43:25 +02002401 return 0;
2402}
2403
David Hubbard1ea6dd32007-06-24 11:16:15 +02002404/* when Super-I/O functions move to a separate file, the Super-I/O
2405 * bus will manage the lifetime of the device and this module will only keep
2406 * track of the w83627ehf driver. But since we platform_device_alloc(), we
2407 * must keep track of the device */
2408static struct platform_device *pdev;
2409
Jean Delvare08e7e272005-04-25 22:43:25 +02002410static int __init sensors_w83627ehf_init(void)
2411{
David Hubbard1ea6dd32007-06-24 11:16:15 +02002412 int err;
2413 unsigned short address;
2414 struct resource res;
2415 struct w83627ehf_sio_data sio_data;
2416
2417 /* initialize sio_data->kind and sio_data->sioreg.
2418 *
2419 * when Super-I/O functions move to a separate file, the Super-I/O
2420 * driver will probe 0x2e and 0x4e and auto-detect the presence of a
2421 * w83627ehf hardware monitor, and call probe() */
2422 if (w83627ehf_find(0x2e, &address, &sio_data) &&
2423 w83627ehf_find(0x4e, &address, &sio_data))
Jean Delvare08e7e272005-04-25 22:43:25 +02002424 return -ENODEV;
2425
David Hubbard1ea6dd32007-06-24 11:16:15 +02002426 err = platform_driver_register(&w83627ehf_driver);
2427 if (err)
2428 goto exit;
2429
Guenter Roecke7e1ca62011-02-04 13:24:30 -08002430 pdev = platform_device_alloc(DRVNAME, address);
2431 if (!pdev) {
David Hubbard1ea6dd32007-06-24 11:16:15 +02002432 err = -ENOMEM;
Joe Perchesabdc6fd2010-10-20 06:51:54 +00002433 pr_err("Device allocation failed\n");
David Hubbard1ea6dd32007-06-24 11:16:15 +02002434 goto exit_unregister;
2435 }
2436
2437 err = platform_device_add_data(pdev, &sio_data,
2438 sizeof(struct w83627ehf_sio_data));
2439 if (err) {
Joe Perchesabdc6fd2010-10-20 06:51:54 +00002440 pr_err("Platform data allocation failed\n");
David Hubbard1ea6dd32007-06-24 11:16:15 +02002441 goto exit_device_put;
2442 }
2443
2444 memset(&res, 0, sizeof(res));
2445 res.name = DRVNAME;
2446 res.start = address + IOREGION_OFFSET;
2447 res.end = address + IOREGION_OFFSET + IOREGION_LENGTH - 1;
2448 res.flags = IORESOURCE_IO;
Jean Delvareb9acb642009-01-07 16:37:35 +01002449
2450 err = acpi_check_resource_conflict(&res);
2451 if (err)
Hans de Goede18632f82009-02-17 19:59:54 +01002452 goto exit_device_put;
Jean Delvareb9acb642009-01-07 16:37:35 +01002453
David Hubbard1ea6dd32007-06-24 11:16:15 +02002454 err = platform_device_add_resources(pdev, &res, 1);
2455 if (err) {
Joe Perchesabdc6fd2010-10-20 06:51:54 +00002456 pr_err("Device resource addition failed (%d)\n", err);
David Hubbard1ea6dd32007-06-24 11:16:15 +02002457 goto exit_device_put;
2458 }
2459
2460 /* platform_device_add calls probe() */
2461 err = platform_device_add(pdev);
2462 if (err) {
Joe Perchesabdc6fd2010-10-20 06:51:54 +00002463 pr_err("Device addition failed (%d)\n", err);
David Hubbard1ea6dd32007-06-24 11:16:15 +02002464 goto exit_device_put;
2465 }
2466
2467 return 0;
2468
2469exit_device_put:
2470 platform_device_put(pdev);
2471exit_unregister:
2472 platform_driver_unregister(&w83627ehf_driver);
2473exit:
2474 return err;
Jean Delvare08e7e272005-04-25 22:43:25 +02002475}
2476
2477static void __exit sensors_w83627ehf_exit(void)
2478{
David Hubbard1ea6dd32007-06-24 11:16:15 +02002479 platform_device_unregister(pdev);
2480 platform_driver_unregister(&w83627ehf_driver);
Jean Delvare08e7e272005-04-25 22:43:25 +02002481}
2482
2483MODULE_AUTHOR("Jean Delvare <khali@linux-fr.org>");
2484MODULE_DESCRIPTION("W83627EHF driver");
2485MODULE_LICENSE("GPL");
2486
2487module_init(sensors_w83627ehf_init);
2488module_exit(sensors_w83627ehf_exit);