blob: df6e50260e014007ba11bcc499de7886e767cd70 [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 Jun237c8d2f2009-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 Jun237c8d2f2009-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
David Hubbard1ea6dd32007-06-24 11:16:15 +020082#define DRVNAME "w83627ehf"
Jean Delvare08e7e272005-04-25 22:43:25 +020083
84/*
85 * Super-I/O constants and functions
86 */
87
Jean Delvare08e7e272005-04-25 22:43:25 +020088#define W83627EHF_LD_HWM 0x0b
Guenter Roecke7e1ca62011-02-04 13:24:30 -080089#define W83667HG_LD_VID 0x0d
Jean Delvare08e7e272005-04-25 22:43:25 +020090
91#define SIO_REG_LDSEL 0x07 /* Logical device select */
92#define SIO_REG_DEVID 0x20 /* Device ID (2 bytes) */
Jean Delvarefc18d6c2007-06-24 11:19:42 +020093#define SIO_REG_EN_VRM10 0x2C /* GPIO3, GPIO4 selection */
Jean Delvare08e7e272005-04-25 22:43:25 +020094#define SIO_REG_ENABLE 0x30 /* Logical device enable */
95#define SIO_REG_ADDR 0x60 /* Logical device address (2 bytes) */
Jean Delvarefc18d6c2007-06-24 11:19:42 +020096#define SIO_REG_VID_CTRL 0xF0 /* VID control */
97#define SIO_REG_VID_DATA 0xF1 /* VID data */
Jean Delvare08e7e272005-04-25 22:43:25 +020098
David Hubbard657c93b2007-02-14 21:15:04 +010099#define SIO_W83627EHF_ID 0x8850
100#define SIO_W83627EHG_ID 0x8860
101#define SIO_W83627DHG_ID 0xa020
Jean Delvarec1e48dc2009-06-15 18:39:50 +0200102#define SIO_W83627DHG_P_ID 0xb070
Guenter Roecke7e1ca62011-02-04 13:24:30 -0800103#define SIO_W83667HG_ID 0xa510
Guenter Roeckc39aeda2010-08-14 21:08:55 +0200104#define SIO_W83667HG_B_ID 0xb350
Guenter Roeckec3e5a12011-02-02 08:46:49 -0800105#define SIO_NCT6775_ID 0xb470
106#define SIO_NCT6776_ID 0xc330
David Hubbard657c93b2007-02-14 21:15:04 +0100107#define SIO_ID_MASK 0xFFF0
Jean Delvare08e7e272005-04-25 22:43:25 +0200108
109static inline void
David Hubbard1ea6dd32007-06-24 11:16:15 +0200110superio_outb(int ioreg, int reg, int val)
Jean Delvare08e7e272005-04-25 22:43:25 +0200111{
David Hubbard1ea6dd32007-06-24 11:16:15 +0200112 outb(reg, ioreg);
113 outb(val, ioreg + 1);
Jean Delvare08e7e272005-04-25 22:43:25 +0200114}
115
116static inline int
David Hubbard1ea6dd32007-06-24 11:16:15 +0200117superio_inb(int ioreg, int reg)
Jean Delvare08e7e272005-04-25 22:43:25 +0200118{
David Hubbard1ea6dd32007-06-24 11:16:15 +0200119 outb(reg, ioreg);
120 return inb(ioreg + 1);
Jean Delvare08e7e272005-04-25 22:43:25 +0200121}
122
123static inline void
David Hubbard1ea6dd32007-06-24 11:16:15 +0200124superio_select(int ioreg, int ld)
Jean Delvare08e7e272005-04-25 22:43:25 +0200125{
David Hubbard1ea6dd32007-06-24 11:16:15 +0200126 outb(SIO_REG_LDSEL, ioreg);
127 outb(ld, ioreg + 1);
Jean Delvare08e7e272005-04-25 22:43:25 +0200128}
129
130static inline void
David Hubbard1ea6dd32007-06-24 11:16:15 +0200131superio_enter(int ioreg)
Jean Delvare08e7e272005-04-25 22:43:25 +0200132{
David Hubbard1ea6dd32007-06-24 11:16:15 +0200133 outb(0x87, ioreg);
134 outb(0x87, ioreg);
Jean Delvare08e7e272005-04-25 22:43:25 +0200135}
136
137static inline void
David Hubbard1ea6dd32007-06-24 11:16:15 +0200138superio_exit(int ioreg)
Jean Delvare08e7e272005-04-25 22:43:25 +0200139{
Jonas Jonsson022b75a2010-09-17 17:24:13 +0200140 outb(0xaa, ioreg);
David Hubbard1ea6dd32007-06-24 11:16:15 +0200141 outb(0x02, ioreg);
142 outb(0x02, ioreg + 1);
Jean Delvare08e7e272005-04-25 22:43:25 +0200143}
144
145/*
146 * ISA constants
147 */
148
Guenter Roecke7e1ca62011-02-04 13:24:30 -0800149#define IOREGION_ALIGNMENT (~7)
Jean Delvare1a641fc2007-04-23 14:41:16 -0700150#define IOREGION_OFFSET 5
151#define IOREGION_LENGTH 2
David Hubbard1ea6dd32007-06-24 11:16:15 +0200152#define ADDR_REG_OFFSET 0
153#define DATA_REG_OFFSET 1
Jean Delvare08e7e272005-04-25 22:43:25 +0200154
155#define W83627EHF_REG_BANK 0x4E
156#define W83627EHF_REG_CONFIG 0x40
David Hubbard657c93b2007-02-14 21:15:04 +0100157
158/* Not currently used:
159 * REG_MAN_ID has the value 0x5ca3 for all supported chips.
160 * REG_CHIP_ID == 0x88/0xa1/0xc1 depending on chip model.
161 * REG_MAN_ID is at port 0x4f
162 * REG_CHIP_ID is at port 0x58 */
Jean Delvare08e7e272005-04-25 22:43:25 +0200163
164static const u16 W83627EHF_REG_FAN[] = { 0x28, 0x29, 0x2a, 0x3f, 0x553 };
165static const u16 W83627EHF_REG_FAN_MIN[] = { 0x3b, 0x3c, 0x3d, 0x3e, 0x55c };
166
Rudolf Marekcf0676f2006-03-23 16:25:22 +0100167/* The W83627EHF registers for nr=7,8,9 are in bank 5 */
168#define W83627EHF_REG_IN_MAX(nr) ((nr < 7) ? (0x2b + (nr) * 2) : \
169 (0x554 + (((nr) - 7) * 2)))
170#define W83627EHF_REG_IN_MIN(nr) ((nr < 7) ? (0x2c + (nr) * 2) : \
171 (0x555 + (((nr) - 7) * 2)))
172#define W83627EHF_REG_IN(nr) ((nr < 7) ? (0x20 + (nr)) : \
173 (0x550 + (nr) - 7))
174
Guenter Roeckd36cf322011-02-07 15:08:54 -0800175static const u16 W83627EHF_REG_TEMP[] = { 0x27, 0x150, 0x250, 0x7e };
176static const u16 W83627EHF_REG_TEMP_HYST[] = { 0x3a, 0x153, 0x253, 0 };
177static const u16 W83627EHF_REG_TEMP_OVER[] = { 0x39, 0x155, 0x255, 0 };
178static const u16 W83627EHF_REG_TEMP_CONFIG[] = { 0, 0x152, 0x252, 0 };
Jean Delvare08e7e272005-04-25 22:43:25 +0200179
180/* Fan clock dividers are spread over the following five registers */
181#define W83627EHF_REG_FANDIV1 0x47
182#define W83627EHF_REG_FANDIV2 0x4B
183#define W83627EHF_REG_VBAT 0x5D
184#define W83627EHF_REG_DIODE 0x59
185#define W83627EHF_REG_SMI_OVT 0x4C
186
Guenter Roeckec3e5a12011-02-02 08:46:49 -0800187/* NCT6775F has its own fan divider registers */
188#define NCT6775_REG_FANDIV1 0x506
189#define NCT6775_REG_FANDIV2 0x507
190
Jean Delvarea4589db2006-03-23 16:30:29 +0100191#define W83627EHF_REG_ALARM1 0x459
192#define W83627EHF_REG_ALARM2 0x45A
193#define W83627EHF_REG_ALARM3 0x45B
194
Rudolf Marek08c79952006-07-05 18:14:31 +0200195/* SmartFan registers */
Daniel J Blueman41e9a062009-12-14 18:01:37 -0800196#define W83627EHF_REG_FAN_STEPUP_TIME 0x0f
197#define W83627EHF_REG_FAN_STEPDOWN_TIME 0x0e
198
Rudolf Marek08c79952006-07-05 18:14:31 +0200199/* DC or PWM output fan configuration */
200static const u8 W83627EHF_REG_PWM_ENABLE[] = {
201 0x04, /* SYS FAN0 output mode and PWM mode */
202 0x04, /* CPU FAN0 output mode and PWM mode */
203 0x12, /* AUX FAN mode */
Daniel J Blueman41e9a062009-12-14 18:01:37 -0800204 0x62, /* CPU FAN1 mode */
Rudolf Marek08c79952006-07-05 18:14:31 +0200205};
206
207static const u8 W83627EHF_PWM_MODE_SHIFT[] = { 0, 1, 0, 6 };
208static const u8 W83627EHF_PWM_ENABLE_SHIFT[] = { 2, 4, 1, 4 };
209
210/* FAN Duty Cycle, be used to control */
Guenter Roeck279af1a2011-02-13 22:34:47 -0800211static const u16 W83627EHF_REG_PWM[] = { 0x01, 0x03, 0x11, 0x61 };
212static const u16 W83627EHF_REG_TARGET[] = { 0x05, 0x06, 0x13, 0x63 };
Rudolf Marek08c79952006-07-05 18:14:31 +0200213static const u8 W83627EHF_REG_TOLERANCE[] = { 0x07, 0x07, 0x14, 0x62 };
214
Rudolf Marek08c79952006-07-05 18:14:31 +0200215/* Advanced Fan control, some values are common for all fans */
Guenter Roeck279af1a2011-02-13 22:34:47 -0800216static const u16 W83627EHF_REG_FAN_START_OUTPUT[] = { 0x0a, 0x0b, 0x16, 0x65 };
217static const u16 W83627EHF_REG_FAN_STOP_OUTPUT[] = { 0x08, 0x09, 0x15, 0x64 };
218static const u16 W83627EHF_REG_FAN_STOP_TIME[] = { 0x0c, 0x0d, 0x17, 0x66 };
Guenter Roeckc39aeda2010-08-14 21:08:55 +0200219
Guenter Roeck279af1a2011-02-13 22:34:47 -0800220static const u16 W83627EHF_REG_FAN_MAX_OUTPUT_COMMON[]
Guenter Roeckc39aeda2010-08-14 21:08:55 +0200221 = { 0xff, 0x67, 0xff, 0x69 };
Guenter Roeck279af1a2011-02-13 22:34:47 -0800222static const u16 W83627EHF_REG_FAN_STEP_OUTPUT_COMMON[]
Guenter Roeckc39aeda2010-08-14 21:08:55 +0200223 = { 0xff, 0x68, 0xff, 0x6a };
224
Guenter Roeck279af1a2011-02-13 22:34:47 -0800225static const u16 W83627EHF_REG_FAN_MAX_OUTPUT_W83667_B[] = { 0x67, 0x69, 0x6b };
226static const u16 W83627EHF_REG_FAN_STEP_OUTPUT_W83667_B[]
227 = { 0x68, 0x6a, 0x6c };
Rudolf Marek08c79952006-07-05 18:14:31 +0200228
Guenter Roeckec3e5a12011-02-02 08:46:49 -0800229static const u16 NCT6775_REG_TARGET[] = { 0x101, 0x201, 0x301 };
230static const u16 NCT6775_REG_FAN_MODE[] = { 0x102, 0x202, 0x302 };
231static const u16 NCT6775_REG_FAN_STOP_OUTPUT[] = { 0x105, 0x205, 0x305 };
232static const u16 NCT6775_REG_FAN_START_OUTPUT[] = { 0x106, 0x206, 0x306 };
233static const u16 NCT6775_REG_FAN_STOP_TIME[] = { 0x107, 0x207, 0x307 };
234static const u16 NCT6775_REG_PWM[] = { 0x109, 0x209, 0x309 };
235static const u16 NCT6775_REG_FAN_MAX_OUTPUT[] = { 0x10a, 0x20a, 0x30a };
236static const u16 NCT6775_REG_FAN_STEP_OUTPUT[] = { 0x10b, 0x20b, 0x30b };
Guenter Roeck26bc4402011-02-11 08:00:58 -0800237static const u16 NCT6775_REG_FAN[] = { 0x630, 0x632, 0x634, 0x636, 0x638 };
Guenter Roeckec3e5a12011-02-02 08:46:49 -0800238static const u16 NCT6776_REG_FAN_MIN[] = { 0x63a, 0x63c, 0x63e, 0x640, 0x642};
239
240static const u16 NCT6775_REG_TEMP[]
241 = { 0x27, 0x150, 0x250, 0x73, 0x75, 0x77, 0x62b, 0x62c, 0x62d };
242static const u16 NCT6775_REG_TEMP_CONFIG[]
243 = { 0, 0x152, 0x252, 0, 0, 0, 0x628, 0x629, 0x62A };
244static const u16 NCT6775_REG_TEMP_HYST[]
245 = { 0x3a, 0x153, 0x253, 0, 0, 0, 0x673, 0x678, 0x67D };
246static const u16 NCT6775_REG_TEMP_OVER[]
247 = { 0x39, 0x155, 0x255, 0, 0, 0, 0x672, 0x677, 0x67C };
248static const u16 NCT6775_REG_TEMP_SOURCE[]
249 = { 0x621, 0x622, 0x623, 0x100, 0x200, 0x300, 0x624, 0x625, 0x626 };
250
Guenter Roeckd36cf322011-02-07 15:08:54 -0800251static const char *const w83667hg_b_temp_label[] = {
252 "SYSTIN",
253 "CPUTIN",
254 "AUXTIN",
255 "AMDTSI",
256 "PECI Agent 1",
257 "PECI Agent 2",
258 "PECI Agent 3",
259 "PECI Agent 4"
260};
261
Guenter Roeckec3e5a12011-02-02 08:46:49 -0800262static const char *const nct6775_temp_label[] = {
263 "",
264 "SYSTIN",
265 "CPUTIN",
266 "AUXTIN",
267 "AMD SB-TSI",
268 "PECI Agent 0",
269 "PECI Agent 1",
270 "PECI Agent 2",
271 "PECI Agent 3",
272 "PECI Agent 4",
273 "PECI Agent 5",
274 "PECI Agent 6",
275 "PECI Agent 7",
276 "PCH_CHIP_CPU_MAX_TEMP",
277 "PCH_CHIP_TEMP",
278 "PCH_CPU_TEMP",
279 "PCH_MCH_TEMP",
280 "PCH_DIM0_TEMP",
281 "PCH_DIM1_TEMP",
282 "PCH_DIM2_TEMP",
283 "PCH_DIM3_TEMP"
284};
285
286static const char *const nct6776_temp_label[] = {
287 "",
288 "SYSTIN",
289 "CPUTIN",
290 "AUXTIN",
291 "SMBUSMASTER 0",
292 "SMBUSMASTER 1",
293 "SMBUSMASTER 2",
294 "SMBUSMASTER 3",
295 "SMBUSMASTER 4",
296 "SMBUSMASTER 5",
297 "SMBUSMASTER 6",
298 "SMBUSMASTER 7",
299 "PECI Agent 0",
300 "PECI Agent 1",
301 "PCH_CHIP_CPU_MAX_TEMP",
302 "PCH_CHIP_TEMP",
303 "PCH_CPU_TEMP",
304 "PCH_MCH_TEMP",
305 "PCH_DIM0_TEMP",
306 "PCH_DIM1_TEMP",
307 "PCH_DIM2_TEMP",
308 "PCH_DIM3_TEMP",
309 "BYTE_TEMP"
310};
311
312#define NUM_REG_TEMP ARRAY_SIZE(NCT6775_REG_TEMP)
Guenter Roeckd36cf322011-02-07 15:08:54 -0800313
Guenter Roeckbce26c52011-02-04 12:54:14 -0800314static inline int is_word_sized(u16 reg)
315{
Guenter Roeckec3e5a12011-02-02 08:46:49 -0800316 return ((((reg & 0xff00) == 0x100
Guenter Roeckbce26c52011-02-04 12:54:14 -0800317 || (reg & 0xff00) == 0x200)
318 && ((reg & 0x00ff) == 0x50
319 || (reg & 0x00ff) == 0x53
Guenter Roeckec3e5a12011-02-02 08:46:49 -0800320 || (reg & 0x00ff) == 0x55))
321 || (reg & 0xfff0) == 0x630
322 || reg == 0x640 || reg == 0x642
323 || ((reg & 0xfff0) == 0x650
324 && (reg & 0x000f) >= 0x06)
325 || reg == 0x73 || reg == 0x75 || reg == 0x77
326 );
Guenter Roeckbce26c52011-02-04 12:54:14 -0800327}
328
Jean Delvare08e7e272005-04-25 22:43:25 +0200329/*
330 * Conversions
331 */
332
Rudolf Marek08c79952006-07-05 18:14:31 +0200333/* 1 is PWM mode, output in ms */
334static inline unsigned int step_time_from_reg(u8 reg, u8 mode)
335{
336 return mode ? 100 * reg : 400 * reg;
337}
338
339static inline u8 step_time_to_reg(unsigned int msec, u8 mode)
340{
341 return SENSORS_LIMIT((mode ? (msec + 50) / 100 :
342 (msec + 200) / 400), 1, 255);
343}
344
Guenter Roeck26bc4402011-02-11 08:00:58 -0800345static unsigned int fan_from_reg8(u16 reg, unsigned int divreg)
Jean Delvare08e7e272005-04-25 22:43:25 +0200346{
Guenter Roeck26bc4402011-02-11 08:00:58 -0800347 if (reg == 0 || reg == 255)
Jean Delvare08e7e272005-04-25 22:43:25 +0200348 return 0;
Guenter Roeck26bc4402011-02-11 08:00:58 -0800349 return 1350000U / (reg << divreg);
350}
351
352static unsigned int fan_from_reg13(u16 reg, unsigned int divreg)
353{
354 if ((reg & 0xff1f) == 0xff1f)
355 return 0;
356
357 reg = (reg & 0x1f) | ((reg & 0xff00) >> 3);
358
359 if (reg == 0)
360 return 0;
361
362 return 1350000U / reg;
363}
364
365static unsigned int fan_from_reg16(u16 reg, unsigned int divreg)
366{
367 if (reg == 0 || reg == 0xffff)
368 return 0;
369
370 /*
371 * Even though the registers are 16 bit wide, the fan divisor
372 * still applies.
373 */
374 return 1350000U / (reg << divreg);
Jean Delvare08e7e272005-04-25 22:43:25 +0200375}
376
377static inline unsigned int
378div_from_reg(u8 reg)
379{
380 return 1 << reg;
381}
382
383static inline int
Guenter Roeckbce26c52011-02-04 12:54:14 -0800384temp_from_reg(u16 reg, s16 regval)
Jean Delvare08e7e272005-04-25 22:43:25 +0200385{
Guenter Roeckbce26c52011-02-04 12:54:14 -0800386 if (is_word_sized(reg))
387 return LM75_TEMP_FROM_REG(regval);
388 return regval * 1000;
Jean Delvare08e7e272005-04-25 22:43:25 +0200389}
390
Guenter Roeckec3e5a12011-02-02 08:46:49 -0800391static inline u16
Guenter Roeckbce26c52011-02-04 12:54:14 -0800392temp_to_reg(u16 reg, long temp)
Jean Delvare08e7e272005-04-25 22:43:25 +0200393{
Guenter Roeckbce26c52011-02-04 12:54:14 -0800394 if (is_word_sized(reg))
395 return LM75_TEMP_TO_REG(temp);
396 return DIV_ROUND_CLOSEST(SENSORS_LIMIT(temp, -127000, 128000), 1000);
Jean Delvare08e7e272005-04-25 22:43:25 +0200397}
398
Rudolf Marekcf0676f2006-03-23 16:25:22 +0100399/* Some of analog inputs have internal scaling (2x), 8mV is ADC LSB */
400
401static u8 scale_in[10] = { 8, 8, 16, 16, 8, 8, 8, 16, 16, 8 };
402
403static inline long in_from_reg(u8 reg, u8 nr)
404{
405 return reg * scale_in[nr];
406}
407
408static inline u8 in_to_reg(u32 val, u8 nr)
409{
Guenter Roecke7e1ca62011-02-04 13:24:30 -0800410 return SENSORS_LIMIT(((val + (scale_in[nr] / 2)) / scale_in[nr]), 0,
411 255);
Rudolf Marekcf0676f2006-03-23 16:25:22 +0100412}
413
Jean Delvare08e7e272005-04-25 22:43:25 +0200414/*
415 * Data structures and manipulation thereof
416 */
417
418struct w83627ehf_data {
David Hubbard1ea6dd32007-06-24 11:16:15 +0200419 int addr; /* IO base of hw monitor block */
420 const char *name;
421
Tony Jones1beeffe2007-08-20 13:46:20 -0700422 struct device *hwmon_dev;
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100423 struct mutex lock;
Jean Delvare08e7e272005-04-25 22:43:25 +0200424
Guenter Roeckec3e5a12011-02-02 08:46:49 -0800425 u16 reg_temp[NUM_REG_TEMP];
426 u16 reg_temp_over[NUM_REG_TEMP];
427 u16 reg_temp_hyst[NUM_REG_TEMP];
428 u16 reg_temp_config[NUM_REG_TEMP];
Guenter Roeckd36cf322011-02-07 15:08:54 -0800429 u8 temp_src[NUM_REG_TEMP];
430 const char * const *temp_label;
431
Guenter Roeck279af1a2011-02-13 22:34:47 -0800432 const u16 *REG_PWM;
433 const u16 *REG_TARGET;
434 const u16 *REG_FAN;
435 const u16 *REG_FAN_MIN;
436 const u16 *REG_FAN_START_OUTPUT;
437 const u16 *REG_FAN_STOP_OUTPUT;
438 const u16 *REG_FAN_STOP_TIME;
439 const u16 *REG_FAN_MAX_OUTPUT;
440 const u16 *REG_FAN_STEP_OUTPUT;
Guenter Roeckda2e0252010-08-14 21:08:55 +0200441
Guenter Roeck26bc4402011-02-11 08:00:58 -0800442 unsigned int (*fan_from_reg)(u16 reg, unsigned int divreg);
443 unsigned int (*fan_from_reg_min)(u16 reg, unsigned int divreg);
444
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100445 struct mutex update_lock;
Jean Delvare08e7e272005-04-25 22:43:25 +0200446 char valid; /* !=0 if following fields are valid */
447 unsigned long last_updated; /* In jiffies */
448
449 /* Register values */
Guenter Roeck83cc8982011-02-06 08:10:15 -0800450 u8 bank; /* current register bank */
David Hubbard1ea6dd32007-06-24 11:16:15 +0200451 u8 in_num; /* number of in inputs we have */
Rudolf Marekcf0676f2006-03-23 16:25:22 +0100452 u8 in[10]; /* Register value */
453 u8 in_max[10]; /* Register value */
454 u8 in_min[10]; /* Register value */
Guenter Roeckec3e5a12011-02-02 08:46:49 -0800455 u16 fan[5];
456 u16 fan_min[5];
Jean Delvare08e7e272005-04-25 22:43:25 +0200457 u8 fan_div[5];
458 u8 has_fan; /* some fan inputs can be disabled */
Guenter Roeckec3e5a12011-02-02 08:46:49 -0800459 u8 has_fan_min; /* some fans don't have min register */
Guenter Roeck26bc4402011-02-11 08:00:58 -0800460 bool has_fan_div;
Jean Delvareda667362007-06-24 11:21:02 +0200461 u8 temp_type[3];
Guenter Roeckec3e5a12011-02-02 08:46:49 -0800462 s16 temp[9];
463 s16 temp_max[9];
464 s16 temp_max_hyst[9];
Jean Delvarea4589db2006-03-23 16:30:29 +0100465 u32 alarms;
Rudolf Marek08c79952006-07-05 18:14:31 +0200466
467 u8 pwm_mode[4]; /* 0->DC variable voltage, 1->PWM variable duty cycle */
468 u8 pwm_enable[4]; /* 1->manual
Daniel J Blueman41e9a062009-12-14 18:01:37 -0800469 2->thermal cruise mode (also called SmartFan I)
470 3->fan speed cruise mode
Guenter Roecke7e1ca62011-02-04 13:24:30 -0800471 4->variable thermal cruise (also called
Guenter Roeckb84bb512011-02-13 23:01:25 -0800472 SmartFan III)
473 5->enhanced variable thermal cruise (also called
474 SmartFan IV) */
475 u8 pwm_enable_orig[4]; /* original value of pwm_enable */
Gong Jun237c8d2f2009-03-30 21:46:42 +0200476 u8 pwm_num; /* number of pwm */
Rudolf Marek08c79952006-07-05 18:14:31 +0200477 u8 pwm[4];
478 u8 target_temp[4];
479 u8 tolerance[4];
480
Daniel J Blueman41e9a062009-12-14 18:01:37 -0800481 u8 fan_start_output[4]; /* minimum fan speed when spinning up */
482 u8 fan_stop_output[4]; /* minimum fan speed when spinning down */
483 u8 fan_stop_time[4]; /* time at minimum before disabling fan */
484 u8 fan_max_output[4]; /* maximum fan speed */
485 u8 fan_step_output[4]; /* rate of change output value */
Jean Delvarefc18d6c2007-06-24 11:19:42 +0200486
487 u8 vid;
488 u8 vrm;
Gong Juna157d062009-03-30 21:46:43 +0200489
Guenter Roeckec3e5a12011-02-02 08:46:49 -0800490 u16 have_temp;
Gong Juna157d062009-03-30 21:46:43 +0200491 u8 in6_skip;
Jean Delvare08e7e272005-04-25 22:43:25 +0200492};
493
David Hubbard1ea6dd32007-06-24 11:16:15 +0200494struct w83627ehf_sio_data {
495 int sioreg;
496 enum kinds kind;
497};
498
Guenter Roeck83cc8982011-02-06 08:10:15 -0800499/*
500 * On older chips, only registers 0x50-0x5f are banked.
501 * On more recent chips, all registers are banked.
502 * Assume that is the case and set the bank number for each access.
503 * Cache the bank number so it only needs to be set if it changes.
504 */
David Hubbard1ea6dd32007-06-24 11:16:15 +0200505static inline void w83627ehf_set_bank(struct w83627ehf_data *data, u16 reg)
Jean Delvare08e7e272005-04-25 22:43:25 +0200506{
Guenter Roeck83cc8982011-02-06 08:10:15 -0800507 u8 bank = reg >> 8;
508 if (data->bank != bank) {
David Hubbard1ea6dd32007-06-24 11:16:15 +0200509 outb_p(W83627EHF_REG_BANK, data->addr + ADDR_REG_OFFSET);
Guenter Roeck83cc8982011-02-06 08:10:15 -0800510 outb_p(bank, data->addr + DATA_REG_OFFSET);
511 data->bank = bank;
Jean Delvare08e7e272005-04-25 22:43:25 +0200512 }
513}
514
David Hubbard1ea6dd32007-06-24 11:16:15 +0200515static u16 w83627ehf_read_value(struct w83627ehf_data *data, u16 reg)
Jean Delvare08e7e272005-04-25 22:43:25 +0200516{
Jean Delvare08e7e272005-04-25 22:43:25 +0200517 int res, word_sized = is_word_sized(reg);
518
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100519 mutex_lock(&data->lock);
Jean Delvare08e7e272005-04-25 22:43:25 +0200520
David Hubbard1ea6dd32007-06-24 11:16:15 +0200521 w83627ehf_set_bank(data, reg);
522 outb_p(reg & 0xff, data->addr + ADDR_REG_OFFSET);
523 res = inb_p(data->addr + DATA_REG_OFFSET);
Jean Delvare08e7e272005-04-25 22:43:25 +0200524 if (word_sized) {
525 outb_p((reg & 0xff) + 1,
David Hubbard1ea6dd32007-06-24 11:16:15 +0200526 data->addr + ADDR_REG_OFFSET);
527 res = (res << 8) + inb_p(data->addr + DATA_REG_OFFSET);
Jean Delvare08e7e272005-04-25 22:43:25 +0200528 }
Jean Delvare08e7e272005-04-25 22:43:25 +0200529
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100530 mutex_unlock(&data->lock);
Jean Delvare08e7e272005-04-25 22:43:25 +0200531 return res;
532}
533
Guenter Roecke7e1ca62011-02-04 13:24:30 -0800534static int w83627ehf_write_value(struct w83627ehf_data *data, u16 reg,
535 u16 value)
Jean Delvare08e7e272005-04-25 22:43:25 +0200536{
Jean Delvare08e7e272005-04-25 22:43:25 +0200537 int word_sized = is_word_sized(reg);
538
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100539 mutex_lock(&data->lock);
Jean Delvare08e7e272005-04-25 22:43:25 +0200540
David Hubbard1ea6dd32007-06-24 11:16:15 +0200541 w83627ehf_set_bank(data, reg);
542 outb_p(reg & 0xff, data->addr + ADDR_REG_OFFSET);
Jean Delvare08e7e272005-04-25 22:43:25 +0200543 if (word_sized) {
David Hubbard1ea6dd32007-06-24 11:16:15 +0200544 outb_p(value >> 8, data->addr + DATA_REG_OFFSET);
Jean Delvare08e7e272005-04-25 22:43:25 +0200545 outb_p((reg & 0xff) + 1,
David Hubbard1ea6dd32007-06-24 11:16:15 +0200546 data->addr + ADDR_REG_OFFSET);
Jean Delvare08e7e272005-04-25 22:43:25 +0200547 }
David Hubbard1ea6dd32007-06-24 11:16:15 +0200548 outb_p(value & 0xff, data->addr + DATA_REG_OFFSET);
Jean Delvare08e7e272005-04-25 22:43:25 +0200549
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100550 mutex_unlock(&data->lock);
Jean Delvare08e7e272005-04-25 22:43:25 +0200551 return 0;
552}
553
554/* This function assumes that the caller holds data->update_lock */
Guenter Roeckec3e5a12011-02-02 08:46:49 -0800555static void nct6775_write_fan_div(struct w83627ehf_data *data, int nr)
556{
557 u8 reg;
558
559 switch (nr) {
560 case 0:
561 reg = (w83627ehf_read_value(data, NCT6775_REG_FANDIV1) & 0x70)
562 | (data->fan_div[0] & 0x7);
563 w83627ehf_write_value(data, NCT6775_REG_FANDIV1, reg);
564 break;
565 case 1:
566 reg = (w83627ehf_read_value(data, NCT6775_REG_FANDIV1) & 0x7)
567 | ((data->fan_div[1] << 4) & 0x70);
568 w83627ehf_write_value(data, NCT6775_REG_FANDIV1, reg);
569 case 2:
570 reg = (w83627ehf_read_value(data, NCT6775_REG_FANDIV2) & 0x70)
571 | (data->fan_div[2] & 0x7);
572 w83627ehf_write_value(data, NCT6775_REG_FANDIV2, reg);
573 break;
574 case 3:
575 reg = (w83627ehf_read_value(data, NCT6775_REG_FANDIV2) & 0x7)
576 | ((data->fan_div[3] << 4) & 0x70);
577 w83627ehf_write_value(data, NCT6775_REG_FANDIV2, reg);
578 break;
579 }
580}
581
582/* This function assumes that the caller holds data->update_lock */
David Hubbard1ea6dd32007-06-24 11:16:15 +0200583static void w83627ehf_write_fan_div(struct w83627ehf_data *data, int nr)
Jean Delvare08e7e272005-04-25 22:43:25 +0200584{
Jean Delvare08e7e272005-04-25 22:43:25 +0200585 u8 reg;
586
587 switch (nr) {
588 case 0:
David Hubbard1ea6dd32007-06-24 11:16:15 +0200589 reg = (w83627ehf_read_value(data, W83627EHF_REG_FANDIV1) & 0xcf)
Jean Delvare08e7e272005-04-25 22:43:25 +0200590 | ((data->fan_div[0] & 0x03) << 4);
Rudolf Marek14992c72006-10-08 22:02:09 +0200591 /* fan5 input control bit is write only, compute the value */
592 reg |= (data->has_fan & (1 << 4)) ? 1 : 0;
David Hubbard1ea6dd32007-06-24 11:16:15 +0200593 w83627ehf_write_value(data, W83627EHF_REG_FANDIV1, reg);
594 reg = (w83627ehf_read_value(data, W83627EHF_REG_VBAT) & 0xdf)
Jean Delvare08e7e272005-04-25 22:43:25 +0200595 | ((data->fan_div[0] & 0x04) << 3);
David Hubbard1ea6dd32007-06-24 11:16:15 +0200596 w83627ehf_write_value(data, W83627EHF_REG_VBAT, reg);
Jean Delvare08e7e272005-04-25 22:43:25 +0200597 break;
598 case 1:
David Hubbard1ea6dd32007-06-24 11:16:15 +0200599 reg = (w83627ehf_read_value(data, W83627EHF_REG_FANDIV1) & 0x3f)
Jean Delvare08e7e272005-04-25 22:43:25 +0200600 | ((data->fan_div[1] & 0x03) << 6);
Rudolf Marek14992c72006-10-08 22:02:09 +0200601 /* fan5 input control bit is write only, compute the value */
602 reg |= (data->has_fan & (1 << 4)) ? 1 : 0;
David Hubbard1ea6dd32007-06-24 11:16:15 +0200603 w83627ehf_write_value(data, W83627EHF_REG_FANDIV1, reg);
604 reg = (w83627ehf_read_value(data, W83627EHF_REG_VBAT) & 0xbf)
Jean Delvare08e7e272005-04-25 22:43:25 +0200605 | ((data->fan_div[1] & 0x04) << 4);
David Hubbard1ea6dd32007-06-24 11:16:15 +0200606 w83627ehf_write_value(data, W83627EHF_REG_VBAT, reg);
Jean Delvare08e7e272005-04-25 22:43:25 +0200607 break;
608 case 2:
David Hubbard1ea6dd32007-06-24 11:16:15 +0200609 reg = (w83627ehf_read_value(data, W83627EHF_REG_FANDIV2) & 0x3f)
Jean Delvare08e7e272005-04-25 22:43:25 +0200610 | ((data->fan_div[2] & 0x03) << 6);
David Hubbard1ea6dd32007-06-24 11:16:15 +0200611 w83627ehf_write_value(data, W83627EHF_REG_FANDIV2, reg);
612 reg = (w83627ehf_read_value(data, W83627EHF_REG_VBAT) & 0x7f)
Jean Delvare08e7e272005-04-25 22:43:25 +0200613 | ((data->fan_div[2] & 0x04) << 5);
David Hubbard1ea6dd32007-06-24 11:16:15 +0200614 w83627ehf_write_value(data, W83627EHF_REG_VBAT, reg);
Jean Delvare08e7e272005-04-25 22:43:25 +0200615 break;
616 case 3:
David Hubbard1ea6dd32007-06-24 11:16:15 +0200617 reg = (w83627ehf_read_value(data, W83627EHF_REG_DIODE) & 0xfc)
Jean Delvare08e7e272005-04-25 22:43:25 +0200618 | (data->fan_div[3] & 0x03);
David Hubbard1ea6dd32007-06-24 11:16:15 +0200619 w83627ehf_write_value(data, W83627EHF_REG_DIODE, reg);
620 reg = (w83627ehf_read_value(data, W83627EHF_REG_SMI_OVT) & 0x7f)
Jean Delvare08e7e272005-04-25 22:43:25 +0200621 | ((data->fan_div[3] & 0x04) << 5);
David Hubbard1ea6dd32007-06-24 11:16:15 +0200622 w83627ehf_write_value(data, W83627EHF_REG_SMI_OVT, reg);
Jean Delvare08e7e272005-04-25 22:43:25 +0200623 break;
624 case 4:
David Hubbard1ea6dd32007-06-24 11:16:15 +0200625 reg = (w83627ehf_read_value(data, W83627EHF_REG_DIODE) & 0x73)
Jean Delvare33725ad2007-04-17 00:32:27 -0700626 | ((data->fan_div[4] & 0x03) << 2)
Jean Delvare08e7e272005-04-25 22:43:25 +0200627 | ((data->fan_div[4] & 0x04) << 5);
David Hubbard1ea6dd32007-06-24 11:16:15 +0200628 w83627ehf_write_value(data, W83627EHF_REG_DIODE, reg);
Jean Delvare08e7e272005-04-25 22:43:25 +0200629 break;
630 }
631}
632
Guenter Roeckec3e5a12011-02-02 08:46:49 -0800633static void w83627ehf_write_fan_div_common(struct device *dev,
634 struct w83627ehf_data *data, int nr)
635{
636 struct w83627ehf_sio_data *sio_data = dev->platform_data;
637
638 if (sio_data->kind == nct6776)
639 ; /* no dividers, do nothing */
640 else if (sio_data->kind == nct6775)
641 nct6775_write_fan_div(data, nr);
642 else
643 w83627ehf_write_fan_div(data, nr);
644}
645
646static void nct6775_update_fan_div(struct w83627ehf_data *data)
647{
648 u8 i;
649
650 i = w83627ehf_read_value(data, NCT6775_REG_FANDIV1);
651 data->fan_div[0] = i & 0x7;
652 data->fan_div[1] = (i & 0x70) >> 4;
653 i = w83627ehf_read_value(data, NCT6775_REG_FANDIV2);
654 data->fan_div[2] = i & 0x7;
655 if (data->has_fan & (1<<3))
656 data->fan_div[3] = (i & 0x70) >> 4;
657}
658
Mark M. Hoffmanea7be662007-08-05 12:19:01 -0400659static void w83627ehf_update_fan_div(struct w83627ehf_data *data)
660{
661 int i;
662
663 i = w83627ehf_read_value(data, W83627EHF_REG_FANDIV1);
664 data->fan_div[0] = (i >> 4) & 0x03;
665 data->fan_div[1] = (i >> 6) & 0x03;
666 i = w83627ehf_read_value(data, W83627EHF_REG_FANDIV2);
667 data->fan_div[2] = (i >> 6) & 0x03;
668 i = w83627ehf_read_value(data, W83627EHF_REG_VBAT);
669 data->fan_div[0] |= (i >> 3) & 0x04;
670 data->fan_div[1] |= (i >> 4) & 0x04;
671 data->fan_div[2] |= (i >> 5) & 0x04;
672 if (data->has_fan & ((1 << 3) | (1 << 4))) {
673 i = w83627ehf_read_value(data, W83627EHF_REG_DIODE);
674 data->fan_div[3] = i & 0x03;
675 data->fan_div[4] = ((i >> 2) & 0x03)
676 | ((i >> 5) & 0x04);
677 }
678 if (data->has_fan & (1 << 3)) {
679 i = w83627ehf_read_value(data, W83627EHF_REG_SMI_OVT);
680 data->fan_div[3] |= (i >> 5) & 0x04;
681 }
682}
683
Guenter Roeckec3e5a12011-02-02 08:46:49 -0800684static void w83627ehf_update_fan_div_common(struct device *dev,
685 struct w83627ehf_data *data)
686{
687 struct w83627ehf_sio_data *sio_data = dev->platform_data;
688
689 if (sio_data->kind == nct6776)
690 ; /* no dividers, do nothing */
691 else if (sio_data->kind == nct6775)
692 nct6775_update_fan_div(data);
693 else
694 w83627ehf_update_fan_div(data);
695}
696
697static void nct6775_update_pwm(struct w83627ehf_data *data)
698{
699 int i;
700 int pwmcfg, fanmodecfg;
701
702 for (i = 0; i < data->pwm_num; i++) {
703 pwmcfg = w83627ehf_read_value(data,
704 W83627EHF_REG_PWM_ENABLE[i]);
705 fanmodecfg = w83627ehf_read_value(data,
706 NCT6775_REG_FAN_MODE[i]);
707 data->pwm_mode[i] =
708 ((pwmcfg >> W83627EHF_PWM_MODE_SHIFT[i]) & 1) ? 0 : 1;
709 data->pwm_enable[i] = ((fanmodecfg >> 4) & 7) + 1;
710 data->tolerance[i] = fanmodecfg & 0x0f;
711 data->pwm[i] = w83627ehf_read_value(data, data->REG_PWM[i]);
712 }
713}
714
715static void w83627ehf_update_pwm(struct w83627ehf_data *data)
716{
717 int i;
718 int pwmcfg = 0, tolerance = 0; /* shut up the compiler */
719
720 for (i = 0; i < data->pwm_num; i++) {
721 if (!(data->has_fan & (1 << i)))
722 continue;
723
724 /* pwmcfg, tolerance mapped for i=0, i=1 to same reg */
725 if (i != 1) {
726 pwmcfg = w83627ehf_read_value(data,
727 W83627EHF_REG_PWM_ENABLE[i]);
728 tolerance = w83627ehf_read_value(data,
729 W83627EHF_REG_TOLERANCE[i]);
730 }
731 data->pwm_mode[i] =
732 ((pwmcfg >> W83627EHF_PWM_MODE_SHIFT[i]) & 1) ? 0 : 1;
733 data->pwm_enable[i] = ((pwmcfg >> W83627EHF_PWM_ENABLE_SHIFT[i])
734 & 3) + 1;
735 data->pwm[i] = w83627ehf_read_value(data, data->REG_PWM[i]);
736
737 data->tolerance[i] = (tolerance >> (i == 1 ? 4 : 0)) & 0x0f;
738 }
739}
740
741static void w83627ehf_update_pwm_common(struct device *dev,
742 struct w83627ehf_data *data)
743{
744 struct w83627ehf_sio_data *sio_data = dev->platform_data;
745
746 if (sio_data->kind == nct6775 || sio_data->kind == nct6776)
747 nct6775_update_pwm(data);
748 else
749 w83627ehf_update_pwm(data);
750}
751
Jean Delvare08e7e272005-04-25 22:43:25 +0200752static struct w83627ehf_data *w83627ehf_update_device(struct device *dev)
753{
David Hubbard1ea6dd32007-06-24 11:16:15 +0200754 struct w83627ehf_data *data = dev_get_drvdata(dev);
Guenter Roeckec3e5a12011-02-02 08:46:49 -0800755 struct w83627ehf_sio_data *sio_data = dev->platform_data;
756
Jean Delvare08e7e272005-04-25 22:43:25 +0200757 int i;
758
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100759 mutex_lock(&data->update_lock);
Jean Delvare08e7e272005-04-25 22:43:25 +0200760
Jean Delvare6b3e4642007-06-24 11:19:01 +0200761 if (time_after(jiffies, data->last_updated + HZ + HZ/2)
Jean Delvare08e7e272005-04-25 22:43:25 +0200762 || !data->valid) {
763 /* Fan clock dividers */
Guenter Roeckec3e5a12011-02-02 08:46:49 -0800764 w83627ehf_update_fan_div_common(dev, data);
Jean Delvare08e7e272005-04-25 22:43:25 +0200765
Rudolf Marekcf0676f2006-03-23 16:25:22 +0100766 /* Measured voltages and limits */
David Hubbard1ea6dd32007-06-24 11:16:15 +0200767 for (i = 0; i < data->in_num; i++) {
768 data->in[i] = w83627ehf_read_value(data,
Rudolf Marekcf0676f2006-03-23 16:25:22 +0100769 W83627EHF_REG_IN(i));
David Hubbard1ea6dd32007-06-24 11:16:15 +0200770 data->in_min[i] = w83627ehf_read_value(data,
Rudolf Marekcf0676f2006-03-23 16:25:22 +0100771 W83627EHF_REG_IN_MIN(i));
David Hubbard1ea6dd32007-06-24 11:16:15 +0200772 data->in_max[i] = w83627ehf_read_value(data,
Rudolf Marekcf0676f2006-03-23 16:25:22 +0100773 W83627EHF_REG_IN_MAX(i));
774 }
775
Jean Delvare08e7e272005-04-25 22:43:25 +0200776 /* Measured fan speeds and limits */
777 for (i = 0; i < 5; i++) {
778 if (!(data->has_fan & (1 << i)))
779 continue;
780
David Hubbard1ea6dd32007-06-24 11:16:15 +0200781 data->fan[i] = w83627ehf_read_value(data,
Guenter Roeckec3e5a12011-02-02 08:46:49 -0800782 data->REG_FAN[i]);
783
784 if (data->has_fan_min & (1 << i))
785 data->fan_min[i] = w83627ehf_read_value(data,
Guenter Roeck279af1a2011-02-13 22:34:47 -0800786 data->REG_FAN_MIN[i]);
Jean Delvare08e7e272005-04-25 22:43:25 +0200787
788 /* If we failed to measure the fan speed and clock
789 divider can be increased, let's try that for next
790 time */
Guenter Roeck26bc4402011-02-11 08:00:58 -0800791 if (data->has_fan_div
792 && (data->fan[i] >= 0xff
Guenter Roeckec3e5a12011-02-02 08:46:49 -0800793 || (sio_data->kind == nct6775
794 && data->fan[i] == 0x00))
795 && data->fan_div[i] < 0x07) {
Guenter Roecke7e1ca62011-02-04 13:24:30 -0800796 dev_dbg(dev, "Increasing fan%d "
Jean Delvare08e7e272005-04-25 22:43:25 +0200797 "clock divider from %u to %u\n",
Jean Delvare33725ad2007-04-17 00:32:27 -0700798 i + 1, div_from_reg(data->fan_div[i]),
Jean Delvare08e7e272005-04-25 22:43:25 +0200799 div_from_reg(data->fan_div[i] + 1));
800 data->fan_div[i]++;
Guenter Roeckec3e5a12011-02-02 08:46:49 -0800801 w83627ehf_write_fan_div_common(dev, data, i);
Jean Delvare08e7e272005-04-25 22:43:25 +0200802 /* Preserve min limit if possible */
Guenter Roeckec3e5a12011-02-02 08:46:49 -0800803 if ((data->has_fan_min & (1 << i))
804 && data->fan_min[i] >= 2
Jean Delvare08e7e272005-04-25 22:43:25 +0200805 && data->fan_min[i] != 255)
David Hubbard1ea6dd32007-06-24 11:16:15 +0200806 w83627ehf_write_value(data,
Guenter Roeck279af1a2011-02-13 22:34:47 -0800807 data->REG_FAN_MIN[i],
Jean Delvare08e7e272005-04-25 22:43:25 +0200808 (data->fan_min[i] /= 2));
809 }
810 }
811
Guenter Roeckec3e5a12011-02-02 08:46:49 -0800812 w83627ehf_update_pwm_common(dev, data);
813
Guenter Roeckda2e0252010-08-14 21:08:55 +0200814 for (i = 0; i < data->pwm_num; i++) {
815 if (!(data->has_fan & (1 << i)))
816 continue;
817
Guenter Roeckec3e5a12011-02-02 08:46:49 -0800818 data->fan_start_output[i] =
819 w83627ehf_read_value(data,
820 data->REG_FAN_START_OUTPUT[i]);
821 data->fan_stop_output[i] =
822 w83627ehf_read_value(data,
823 data->REG_FAN_STOP_OUTPUT[i]);
824 data->fan_stop_time[i] =
825 w83627ehf_read_value(data,
826 data->REG_FAN_STOP_TIME[i]);
Guenter Roeckda2e0252010-08-14 21:08:55 +0200827
Guenter Roeckec3e5a12011-02-02 08:46:49 -0800828 if (data->REG_FAN_MAX_OUTPUT &&
829 data->REG_FAN_MAX_OUTPUT[i] != 0xff)
Guenter Roeckda2e0252010-08-14 21:08:55 +0200830 data->fan_max_output[i] =
831 w83627ehf_read_value(data,
Guenter Roeckec3e5a12011-02-02 08:46:49 -0800832 data->REG_FAN_MAX_OUTPUT[i]);
Guenter Roeckda2e0252010-08-14 21:08:55 +0200833
Guenter Roeckec3e5a12011-02-02 08:46:49 -0800834 if (data->REG_FAN_STEP_OUTPUT &&
835 data->REG_FAN_STEP_OUTPUT[i] != 0xff)
Guenter Roeckda2e0252010-08-14 21:08:55 +0200836 data->fan_step_output[i] =
837 w83627ehf_read_value(data,
Guenter Roeckec3e5a12011-02-02 08:46:49 -0800838 data->REG_FAN_STEP_OUTPUT[i]);
Guenter Roeckda2e0252010-08-14 21:08:55 +0200839
Rudolf Marek08c79952006-07-05 18:14:31 +0200840 data->target_temp[i] =
David Hubbard1ea6dd32007-06-24 11:16:15 +0200841 w83627ehf_read_value(data,
Guenter Roeck279af1a2011-02-13 22:34:47 -0800842 data->REG_TARGET[i]) &
Rudolf Marek08c79952006-07-05 18:14:31 +0200843 (data->pwm_mode[i] == 1 ? 0x7f : 0xff);
Rudolf Marek08c79952006-07-05 18:14:31 +0200844 }
845
Jean Delvare08e7e272005-04-25 22:43:25 +0200846 /* Measured temperatures and limits */
Guenter Roeckd36cf322011-02-07 15:08:54 -0800847 for (i = 0; i < NUM_REG_TEMP; i++) {
848 if (!(data->have_temp & (1 << i)))
849 continue;
Guenter Roeckec3e5a12011-02-02 08:46:49 -0800850 data->temp[i] = w83627ehf_read_value(data,
851 data->reg_temp[i]);
852 if (data->reg_temp_over[i])
853 data->temp_max[i]
854 = w83627ehf_read_value(data,
855 data->reg_temp_over[i]);
856 if (data->reg_temp_hyst[i])
857 data->temp_max_hyst[i]
858 = w83627ehf_read_value(data,
859 data->reg_temp_hyst[i]);
Jean Delvare08e7e272005-04-25 22:43:25 +0200860 }
861
David Hubbard1ea6dd32007-06-24 11:16:15 +0200862 data->alarms = w83627ehf_read_value(data,
Jean Delvarea4589db2006-03-23 16:30:29 +0100863 W83627EHF_REG_ALARM1) |
David Hubbard1ea6dd32007-06-24 11:16:15 +0200864 (w83627ehf_read_value(data,
Jean Delvarea4589db2006-03-23 16:30:29 +0100865 W83627EHF_REG_ALARM2) << 8) |
David Hubbard1ea6dd32007-06-24 11:16:15 +0200866 (w83627ehf_read_value(data,
Jean Delvarea4589db2006-03-23 16:30:29 +0100867 W83627EHF_REG_ALARM3) << 16);
868
Jean Delvare08e7e272005-04-25 22:43:25 +0200869 data->last_updated = jiffies;
870 data->valid = 1;
871 }
872
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100873 mutex_unlock(&data->update_lock);
Jean Delvare08e7e272005-04-25 22:43:25 +0200874 return data;
875}
876
877/*
878 * Sysfs callback functions
879 */
Rudolf Marekcf0676f2006-03-23 16:25:22 +0100880#define show_in_reg(reg) \
881static ssize_t \
882show_##reg(struct device *dev, struct device_attribute *attr, \
883 char *buf) \
884{ \
885 struct w83627ehf_data *data = w83627ehf_update_device(dev); \
Guenter Roecke7e1ca62011-02-04 13:24:30 -0800886 struct sensor_device_attribute *sensor_attr = \
887 to_sensor_dev_attr(attr); \
Rudolf Marekcf0676f2006-03-23 16:25:22 +0100888 int nr = sensor_attr->index; \
889 return sprintf(buf, "%ld\n", in_from_reg(data->reg[nr], nr)); \
890}
891show_in_reg(in)
892show_in_reg(in_min)
893show_in_reg(in_max)
894
895#define store_in_reg(REG, reg) \
896static ssize_t \
Guenter Roecke7e1ca62011-02-04 13:24:30 -0800897store_in_##reg(struct device *dev, struct device_attribute *attr, \
898 const char *buf, size_t count) \
Rudolf Marekcf0676f2006-03-23 16:25:22 +0100899{ \
David Hubbard1ea6dd32007-06-24 11:16:15 +0200900 struct w83627ehf_data *data = dev_get_drvdata(dev); \
Guenter Roecke7e1ca62011-02-04 13:24:30 -0800901 struct sensor_device_attribute *sensor_attr = \
902 to_sensor_dev_attr(attr); \
Rudolf Marekcf0676f2006-03-23 16:25:22 +0100903 int nr = sensor_attr->index; \
Guenter Roeckbce26c52011-02-04 12:54:14 -0800904 unsigned long val; \
905 int err; \
906 err = strict_strtoul(buf, 10, &val); \
907 if (err < 0) \
908 return err; \
Rudolf Marekcf0676f2006-03-23 16:25:22 +0100909 mutex_lock(&data->update_lock); \
910 data->in_##reg[nr] = in_to_reg(val, nr); \
David Hubbard1ea6dd32007-06-24 11:16:15 +0200911 w83627ehf_write_value(data, W83627EHF_REG_IN_##REG(nr), \
Rudolf Marekcf0676f2006-03-23 16:25:22 +0100912 data->in_##reg[nr]); \
913 mutex_unlock(&data->update_lock); \
914 return count; \
915}
916
917store_in_reg(MIN, min)
918store_in_reg(MAX, max)
919
Guenter Roecke7e1ca62011-02-04 13:24:30 -0800920static ssize_t show_alarm(struct device *dev, struct device_attribute *attr,
921 char *buf)
Jean Delvarea4589db2006-03-23 16:30:29 +0100922{
923 struct w83627ehf_data *data = w83627ehf_update_device(dev);
924 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
925 int nr = sensor_attr->index;
926 return sprintf(buf, "%u\n", (data->alarms >> nr) & 0x01);
927}
928
Rudolf Marekcf0676f2006-03-23 16:25:22 +0100929static struct sensor_device_attribute sda_in_input[] = {
930 SENSOR_ATTR(in0_input, S_IRUGO, show_in, NULL, 0),
931 SENSOR_ATTR(in1_input, S_IRUGO, show_in, NULL, 1),
932 SENSOR_ATTR(in2_input, S_IRUGO, show_in, NULL, 2),
933 SENSOR_ATTR(in3_input, S_IRUGO, show_in, NULL, 3),
934 SENSOR_ATTR(in4_input, S_IRUGO, show_in, NULL, 4),
935 SENSOR_ATTR(in5_input, S_IRUGO, show_in, NULL, 5),
936 SENSOR_ATTR(in6_input, S_IRUGO, show_in, NULL, 6),
937 SENSOR_ATTR(in7_input, S_IRUGO, show_in, NULL, 7),
938 SENSOR_ATTR(in8_input, S_IRUGO, show_in, NULL, 8),
939 SENSOR_ATTR(in9_input, S_IRUGO, show_in, NULL, 9),
940};
941
Jean Delvarea4589db2006-03-23 16:30:29 +0100942static struct sensor_device_attribute sda_in_alarm[] = {
943 SENSOR_ATTR(in0_alarm, S_IRUGO, show_alarm, NULL, 0),
944 SENSOR_ATTR(in1_alarm, S_IRUGO, show_alarm, NULL, 1),
945 SENSOR_ATTR(in2_alarm, S_IRUGO, show_alarm, NULL, 2),
946 SENSOR_ATTR(in3_alarm, S_IRUGO, show_alarm, NULL, 3),
947 SENSOR_ATTR(in4_alarm, S_IRUGO, show_alarm, NULL, 8),
948 SENSOR_ATTR(in5_alarm, S_IRUGO, show_alarm, NULL, 21),
949 SENSOR_ATTR(in6_alarm, S_IRUGO, show_alarm, NULL, 20),
950 SENSOR_ATTR(in7_alarm, S_IRUGO, show_alarm, NULL, 16),
951 SENSOR_ATTR(in8_alarm, S_IRUGO, show_alarm, NULL, 17),
952 SENSOR_ATTR(in9_alarm, S_IRUGO, show_alarm, NULL, 19),
953};
954
Rudolf Marekcf0676f2006-03-23 16:25:22 +0100955static struct sensor_device_attribute sda_in_min[] = {
Guenter Roecke7e1ca62011-02-04 13:24:30 -0800956 SENSOR_ATTR(in0_min, S_IWUSR | S_IRUGO, show_in_min, store_in_min, 0),
957 SENSOR_ATTR(in1_min, S_IWUSR | S_IRUGO, show_in_min, store_in_min, 1),
958 SENSOR_ATTR(in2_min, S_IWUSR | S_IRUGO, show_in_min, store_in_min, 2),
959 SENSOR_ATTR(in3_min, S_IWUSR | S_IRUGO, show_in_min, store_in_min, 3),
960 SENSOR_ATTR(in4_min, S_IWUSR | S_IRUGO, show_in_min, store_in_min, 4),
961 SENSOR_ATTR(in5_min, S_IWUSR | S_IRUGO, show_in_min, store_in_min, 5),
962 SENSOR_ATTR(in6_min, S_IWUSR | S_IRUGO, show_in_min, store_in_min, 6),
963 SENSOR_ATTR(in7_min, S_IWUSR | S_IRUGO, show_in_min, store_in_min, 7),
964 SENSOR_ATTR(in8_min, S_IWUSR | S_IRUGO, show_in_min, store_in_min, 8),
965 SENSOR_ATTR(in9_min, S_IWUSR | S_IRUGO, show_in_min, store_in_min, 9),
Rudolf Marekcf0676f2006-03-23 16:25:22 +0100966};
967
968static struct sensor_device_attribute sda_in_max[] = {
Guenter Roecke7e1ca62011-02-04 13:24:30 -0800969 SENSOR_ATTR(in0_max, S_IWUSR | S_IRUGO, show_in_max, store_in_max, 0),
970 SENSOR_ATTR(in1_max, S_IWUSR | S_IRUGO, show_in_max, store_in_max, 1),
971 SENSOR_ATTR(in2_max, S_IWUSR | S_IRUGO, show_in_max, store_in_max, 2),
972 SENSOR_ATTR(in3_max, S_IWUSR | S_IRUGO, show_in_max, store_in_max, 3),
973 SENSOR_ATTR(in4_max, S_IWUSR | S_IRUGO, show_in_max, store_in_max, 4),
974 SENSOR_ATTR(in5_max, S_IWUSR | S_IRUGO, show_in_max, store_in_max, 5),
975 SENSOR_ATTR(in6_max, S_IWUSR | S_IRUGO, show_in_max, store_in_max, 6),
976 SENSOR_ATTR(in7_max, S_IWUSR | S_IRUGO, show_in_max, store_in_max, 7),
977 SENSOR_ATTR(in8_max, S_IWUSR | S_IRUGO, show_in_max, store_in_max, 8),
978 SENSOR_ATTR(in9_max, S_IWUSR | S_IRUGO, show_in_max, store_in_max, 9),
Rudolf Marekcf0676f2006-03-23 16:25:22 +0100979};
980
Guenter Roeckec3e5a12011-02-02 08:46:49 -0800981static ssize_t
982show_fan(struct device *dev, struct device_attribute *attr, char *buf)
983{
984 struct w83627ehf_data *data = w83627ehf_update_device(dev);
985 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
986 int nr = sensor_attr->index;
987 return sprintf(buf, "%d\n",
Guenter Roeck26bc4402011-02-11 08:00:58 -0800988 data->fan_from_reg(data->fan[nr], data->fan_div[nr]));
Jean Delvare08e7e272005-04-25 22:43:25 +0200989}
Guenter Roeckec3e5a12011-02-02 08:46:49 -0800990
991static ssize_t
992show_fan_min(struct device *dev, struct device_attribute *attr, char *buf)
993{
994 struct w83627ehf_data *data = w83627ehf_update_device(dev);
995 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
996 int nr = sensor_attr->index;
997 return sprintf(buf, "%d\n",
Guenter Roeck26bc4402011-02-11 08:00:58 -0800998 data->fan_from_reg_min(data->fan_min[nr],
999 data->fan_div[nr]));
Guenter Roeckec3e5a12011-02-02 08:46:49 -08001000}
Jean Delvare08e7e272005-04-25 22:43:25 +02001001
1002static ssize_t
Yuan Mu412fec82006-02-05 23:24:16 +01001003show_fan_div(struct device *dev, struct device_attribute *attr,
1004 char *buf)
Jean Delvare08e7e272005-04-25 22:43:25 +02001005{
1006 struct w83627ehf_data *data = w83627ehf_update_device(dev);
Yuan Mu412fec82006-02-05 23:24:16 +01001007 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
1008 int nr = sensor_attr->index;
1009 return sprintf(buf, "%u\n", div_from_reg(data->fan_div[nr]));
Jean Delvare08e7e272005-04-25 22:43:25 +02001010}
1011
1012static ssize_t
Yuan Mu412fec82006-02-05 23:24:16 +01001013store_fan_min(struct device *dev, struct device_attribute *attr,
1014 const char *buf, size_t count)
Jean Delvare08e7e272005-04-25 22:43:25 +02001015{
David Hubbard1ea6dd32007-06-24 11:16:15 +02001016 struct w83627ehf_data *data = dev_get_drvdata(dev);
Yuan Mu412fec82006-02-05 23:24:16 +01001017 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
1018 int nr = sensor_attr->index;
Guenter Roeckbce26c52011-02-04 12:54:14 -08001019 unsigned long val;
1020 int err;
Jean Delvare08e7e272005-04-25 22:43:25 +02001021 unsigned int reg;
1022 u8 new_div;
1023
Guenter Roeckbce26c52011-02-04 12:54:14 -08001024 err = strict_strtoul(buf, 10, &val);
1025 if (err < 0)
1026 return err;
1027
Ingo Molnar9a61bf62006-01-18 23:19:26 +01001028 mutex_lock(&data->update_lock);
Guenter Roeck26bc4402011-02-11 08:00:58 -08001029 if (!data->has_fan_div) {
1030 /*
1031 * Only NCT6776F for now, so we know that this is a 13 bit
1032 * register
1033 */
Guenter Roeckec3e5a12011-02-02 08:46:49 -08001034 if (!val) {
1035 val = 0xff1f;
1036 } else {
1037 if (val > 1350000U)
1038 val = 135000U;
1039 val = 1350000U / val;
1040 val = (val & 0x1f) | ((val << 3) & 0xff00);
1041 }
1042 data->fan_min[nr] = val;
1043 goto done; /* Leave fan divider alone */
1044 }
Jean Delvare08e7e272005-04-25 22:43:25 +02001045 if (!val) {
1046 /* No min limit, alarm disabled */
1047 data->fan_min[nr] = 255;
1048 new_div = data->fan_div[nr]; /* No change */
1049 dev_info(dev, "fan%u low limit and alarm disabled\n", nr + 1);
1050 } else if ((reg = 1350000U / val) >= 128 * 255) {
1051 /* Speed below this value cannot possibly be represented,
1052 even with the highest divider (128) */
1053 data->fan_min[nr] = 254;
1054 new_div = 7; /* 128 == (1 << 7) */
Guenter Roeckbce26c52011-02-04 12:54:14 -08001055 dev_warn(dev, "fan%u low limit %lu below minimum %u, set to "
Guenter Roeckec3e5a12011-02-02 08:46:49 -08001056 "minimum\n", nr + 1, val,
Guenter Roeck26bc4402011-02-11 08:00:58 -08001057 data->fan_from_reg_min(254, 7));
Jean Delvare08e7e272005-04-25 22:43:25 +02001058 } else if (!reg) {
1059 /* Speed above this value cannot possibly be represented,
1060 even with the lowest divider (1) */
1061 data->fan_min[nr] = 1;
1062 new_div = 0; /* 1 == (1 << 0) */
Guenter Roeckbce26c52011-02-04 12:54:14 -08001063 dev_warn(dev, "fan%u low limit %lu above maximum %u, set to "
Guenter Roeckec3e5a12011-02-02 08:46:49 -08001064 "maximum\n", nr + 1, val,
Guenter Roeck26bc4402011-02-11 08:00:58 -08001065 data->fan_from_reg_min(1, 0));
Jean Delvare08e7e272005-04-25 22:43:25 +02001066 } else {
1067 /* Automatically pick the best divider, i.e. the one such
1068 that the min limit will correspond to a register value
1069 in the 96..192 range */
1070 new_div = 0;
1071 while (reg > 192 && new_div < 7) {
1072 reg >>= 1;
1073 new_div++;
1074 }
1075 data->fan_min[nr] = reg;
1076 }
1077
1078 /* Write both the fan clock divider (if it changed) and the new
1079 fan min (unconditionally) */
1080 if (new_div != data->fan_div[nr]) {
Jean Delvare158ce072007-06-17 16:09:12 +02001081 /* Preserve the fan speed reading */
1082 if (data->fan[nr] != 0xff) {
1083 if (new_div > data->fan_div[nr])
1084 data->fan[nr] >>= new_div - data->fan_div[nr];
1085 else if (data->fan[nr] & 0x80)
1086 data->fan[nr] = 0xff;
1087 else
1088 data->fan[nr] <<= data->fan_div[nr] - new_div;
1089 }
Jean Delvare08e7e272005-04-25 22:43:25 +02001090
1091 dev_dbg(dev, "fan%u clock divider changed from %u to %u\n",
1092 nr + 1, div_from_reg(data->fan_div[nr]),
1093 div_from_reg(new_div));
1094 data->fan_div[nr] = new_div;
Guenter Roeckec3e5a12011-02-02 08:46:49 -08001095 w83627ehf_write_fan_div_common(dev, data, nr);
Jean Delvare6b3e4642007-06-24 11:19:01 +02001096 /* Give the chip time to sample a new speed value */
1097 data->last_updated = jiffies;
Jean Delvare08e7e272005-04-25 22:43:25 +02001098 }
Guenter Roeckec3e5a12011-02-02 08:46:49 -08001099done:
Guenter Roeck279af1a2011-02-13 22:34:47 -08001100 w83627ehf_write_value(data, data->REG_FAN_MIN[nr],
Jean Delvare08e7e272005-04-25 22:43:25 +02001101 data->fan_min[nr]);
Ingo Molnar9a61bf62006-01-18 23:19:26 +01001102 mutex_unlock(&data->update_lock);
Jean Delvare08e7e272005-04-25 22:43:25 +02001103
1104 return count;
1105}
1106
Yuan Mu412fec82006-02-05 23:24:16 +01001107static struct sensor_device_attribute sda_fan_input[] = {
1108 SENSOR_ATTR(fan1_input, S_IRUGO, show_fan, NULL, 0),
1109 SENSOR_ATTR(fan2_input, S_IRUGO, show_fan, NULL, 1),
1110 SENSOR_ATTR(fan3_input, S_IRUGO, show_fan, NULL, 2),
1111 SENSOR_ATTR(fan4_input, S_IRUGO, show_fan, NULL, 3),
1112 SENSOR_ATTR(fan5_input, S_IRUGO, show_fan, NULL, 4),
1113};
Jean Delvare08e7e272005-04-25 22:43:25 +02001114
Jean Delvarea4589db2006-03-23 16:30:29 +01001115static struct sensor_device_attribute sda_fan_alarm[] = {
1116 SENSOR_ATTR(fan1_alarm, S_IRUGO, show_alarm, NULL, 6),
1117 SENSOR_ATTR(fan2_alarm, S_IRUGO, show_alarm, NULL, 7),
1118 SENSOR_ATTR(fan3_alarm, S_IRUGO, show_alarm, NULL, 11),
1119 SENSOR_ATTR(fan4_alarm, S_IRUGO, show_alarm, NULL, 10),
1120 SENSOR_ATTR(fan5_alarm, S_IRUGO, show_alarm, NULL, 23),
1121};
1122
Yuan Mu412fec82006-02-05 23:24:16 +01001123static struct sensor_device_attribute sda_fan_min[] = {
1124 SENSOR_ATTR(fan1_min, S_IWUSR | S_IRUGO, show_fan_min,
1125 store_fan_min, 0),
1126 SENSOR_ATTR(fan2_min, S_IWUSR | S_IRUGO, show_fan_min,
1127 store_fan_min, 1),
1128 SENSOR_ATTR(fan3_min, S_IWUSR | S_IRUGO, show_fan_min,
1129 store_fan_min, 2),
1130 SENSOR_ATTR(fan4_min, S_IWUSR | S_IRUGO, show_fan_min,
1131 store_fan_min, 3),
1132 SENSOR_ATTR(fan5_min, S_IWUSR | S_IRUGO, show_fan_min,
1133 store_fan_min, 4),
1134};
Jean Delvare08e7e272005-04-25 22:43:25 +02001135
Yuan Mu412fec82006-02-05 23:24:16 +01001136static struct sensor_device_attribute sda_fan_div[] = {
1137 SENSOR_ATTR(fan1_div, S_IRUGO, show_fan_div, NULL, 0),
1138 SENSOR_ATTR(fan2_div, S_IRUGO, show_fan_div, NULL, 1),
1139 SENSOR_ATTR(fan3_div, S_IRUGO, show_fan_div, NULL, 2),
1140 SENSOR_ATTR(fan4_div, S_IRUGO, show_fan_div, NULL, 3),
1141 SENSOR_ATTR(fan5_div, S_IRUGO, show_fan_div, NULL, 4),
1142};
Jean Delvare08e7e272005-04-25 22:43:25 +02001143
Guenter Roeckd36cf322011-02-07 15:08:54 -08001144static ssize_t
1145show_temp_label(struct device *dev, struct device_attribute *attr, char *buf)
1146{
1147 struct w83627ehf_data *data = w83627ehf_update_device(dev);
1148 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
1149 int nr = sensor_attr->index;
1150 return sprintf(buf, "%s\n", data->temp_label[data->temp_src[nr]]);
1151}
1152
Guenter Roeckec3e5a12011-02-02 08:46:49 -08001153#define show_temp_reg(addr, reg) \
Jean Delvare08e7e272005-04-25 22:43:25 +02001154static ssize_t \
Yuan Mu412fec82006-02-05 23:24:16 +01001155show_##reg(struct device *dev, struct device_attribute *attr, \
1156 char *buf) \
Jean Delvare08e7e272005-04-25 22:43:25 +02001157{ \
1158 struct w83627ehf_data *data = w83627ehf_update_device(dev); \
Guenter Roecke7e1ca62011-02-04 13:24:30 -08001159 struct sensor_device_attribute *sensor_attr = \
1160 to_sensor_dev_attr(attr); \
Yuan Mu412fec82006-02-05 23:24:16 +01001161 int nr = sensor_attr->index; \
Jean Delvare08e7e272005-04-25 22:43:25 +02001162 return sprintf(buf, "%d\n", \
Guenter Roeckec3e5a12011-02-02 08:46:49 -08001163 temp_from_reg(data->addr[nr], data->reg[nr])); \
Jean Delvare08e7e272005-04-25 22:43:25 +02001164}
Guenter Roeckec3e5a12011-02-02 08:46:49 -08001165show_temp_reg(reg_temp, temp);
1166show_temp_reg(reg_temp_over, temp_max);
1167show_temp_reg(reg_temp_hyst, temp_max_hyst);
Jean Delvare08e7e272005-04-25 22:43:25 +02001168
Guenter Roeckec3e5a12011-02-02 08:46:49 -08001169#define store_temp_reg(addr, reg) \
Jean Delvare08e7e272005-04-25 22:43:25 +02001170static ssize_t \
Yuan Mu412fec82006-02-05 23:24:16 +01001171store_##reg(struct device *dev, struct device_attribute *attr, \
1172 const char *buf, size_t count) \
Jean Delvare08e7e272005-04-25 22:43:25 +02001173{ \
David Hubbard1ea6dd32007-06-24 11:16:15 +02001174 struct w83627ehf_data *data = dev_get_drvdata(dev); \
Guenter Roecke7e1ca62011-02-04 13:24:30 -08001175 struct sensor_device_attribute *sensor_attr = \
1176 to_sensor_dev_attr(attr); \
Yuan Mu412fec82006-02-05 23:24:16 +01001177 int nr = sensor_attr->index; \
Guenter Roeckbce26c52011-02-04 12:54:14 -08001178 int err; \
1179 long val; \
1180 err = strict_strtol(buf, 10, &val); \
1181 if (err < 0) \
1182 return err; \
Ingo Molnar9a61bf62006-01-18 23:19:26 +01001183 mutex_lock(&data->update_lock); \
Guenter Roeckec3e5a12011-02-02 08:46:49 -08001184 data->reg[nr] = temp_to_reg(data->addr[nr], val); \
1185 w83627ehf_write_value(data, data->addr[nr], \
Jean Delvare08e7e272005-04-25 22:43:25 +02001186 data->reg[nr]); \
Ingo Molnar9a61bf62006-01-18 23:19:26 +01001187 mutex_unlock(&data->update_lock); \
Jean Delvare08e7e272005-04-25 22:43:25 +02001188 return count; \
1189}
Guenter Roeckec3e5a12011-02-02 08:46:49 -08001190store_temp_reg(reg_temp_over, temp_max);
1191store_temp_reg(reg_temp_hyst, temp_max_hyst);
Jean Delvare08e7e272005-04-25 22:43:25 +02001192
Jean Delvareda667362007-06-24 11:21:02 +02001193static ssize_t
1194show_temp_type(struct device *dev, struct device_attribute *attr, char *buf)
1195{
1196 struct w83627ehf_data *data = w83627ehf_update_device(dev);
1197 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
1198 int nr = sensor_attr->index;
1199 return sprintf(buf, "%d\n", (int)data->temp_type[nr]);
1200}
1201
Gong Juna157d062009-03-30 21:46:43 +02001202static struct sensor_device_attribute sda_temp_input[] = {
Guenter Roeckbce26c52011-02-04 12:54:14 -08001203 SENSOR_ATTR(temp1_input, S_IRUGO, show_temp, NULL, 0),
1204 SENSOR_ATTR(temp2_input, S_IRUGO, show_temp, NULL, 1),
1205 SENSOR_ATTR(temp3_input, S_IRUGO, show_temp, NULL, 2),
Guenter Roeckd36cf322011-02-07 15:08:54 -08001206 SENSOR_ATTR(temp4_input, S_IRUGO, show_temp, NULL, 3),
Guenter Roeckec3e5a12011-02-02 08:46:49 -08001207 SENSOR_ATTR(temp5_input, S_IRUGO, show_temp, NULL, 4),
1208 SENSOR_ATTR(temp6_input, S_IRUGO, show_temp, NULL, 5),
1209 SENSOR_ATTR(temp7_input, S_IRUGO, show_temp, NULL, 6),
1210 SENSOR_ATTR(temp8_input, S_IRUGO, show_temp, NULL, 7),
1211 SENSOR_ATTR(temp9_input, S_IRUGO, show_temp, NULL, 8),
Guenter Roeckd36cf322011-02-07 15:08:54 -08001212};
1213
1214static struct sensor_device_attribute sda_temp_label[] = {
1215 SENSOR_ATTR(temp1_label, S_IRUGO, show_temp_label, NULL, 0),
1216 SENSOR_ATTR(temp2_label, S_IRUGO, show_temp_label, NULL, 1),
1217 SENSOR_ATTR(temp3_label, S_IRUGO, show_temp_label, NULL, 2),
1218 SENSOR_ATTR(temp4_label, S_IRUGO, show_temp_label, NULL, 3),
Guenter Roeckec3e5a12011-02-02 08:46:49 -08001219 SENSOR_ATTR(temp5_label, S_IRUGO, show_temp_label, NULL, 4),
1220 SENSOR_ATTR(temp6_label, S_IRUGO, show_temp_label, NULL, 5),
1221 SENSOR_ATTR(temp7_label, S_IRUGO, show_temp_label, NULL, 6),
1222 SENSOR_ATTR(temp8_label, S_IRUGO, show_temp_label, NULL, 7),
1223 SENSOR_ATTR(temp9_label, S_IRUGO, show_temp_label, NULL, 8),
Gong Juna157d062009-03-30 21:46:43 +02001224};
1225
1226static struct sensor_device_attribute sda_temp_max[] = {
Guenter Roeckbce26c52011-02-04 12:54:14 -08001227 SENSOR_ATTR(temp1_max, S_IRUGO | S_IWUSR, show_temp_max,
Yuan Mu412fec82006-02-05 23:24:16 +01001228 store_temp_max, 0),
Guenter Roeckbce26c52011-02-04 12:54:14 -08001229 SENSOR_ATTR(temp2_max, S_IRUGO | S_IWUSR, show_temp_max,
Yuan Mu412fec82006-02-05 23:24:16 +01001230 store_temp_max, 1),
Guenter Roeckbce26c52011-02-04 12:54:14 -08001231 SENSOR_ATTR(temp3_max, S_IRUGO | S_IWUSR, show_temp_max,
1232 store_temp_max, 2),
Guenter Roeckec3e5a12011-02-02 08:46:49 -08001233 SENSOR_ATTR(temp4_max, S_IRUGO | S_IWUSR, show_temp_max,
1234 store_temp_max, 3),
1235 SENSOR_ATTR(temp5_max, S_IRUGO | S_IWUSR, show_temp_max,
1236 store_temp_max, 4),
1237 SENSOR_ATTR(temp6_max, S_IRUGO | S_IWUSR, show_temp_max,
1238 store_temp_max, 5),
1239 SENSOR_ATTR(temp7_max, S_IRUGO | S_IWUSR, show_temp_max,
1240 store_temp_max, 6),
1241 SENSOR_ATTR(temp8_max, S_IRUGO | S_IWUSR, show_temp_max,
1242 store_temp_max, 7),
1243 SENSOR_ATTR(temp9_max, S_IRUGO | S_IWUSR, show_temp_max,
1244 store_temp_max, 8),
Gong Juna157d062009-03-30 21:46:43 +02001245};
1246
1247static struct sensor_device_attribute sda_temp_max_hyst[] = {
Guenter Roeckbce26c52011-02-04 12:54:14 -08001248 SENSOR_ATTR(temp1_max_hyst, S_IRUGO | S_IWUSR, show_temp_max_hyst,
Yuan Mu412fec82006-02-05 23:24:16 +01001249 store_temp_max_hyst, 0),
Guenter Roeckbce26c52011-02-04 12:54:14 -08001250 SENSOR_ATTR(temp2_max_hyst, S_IRUGO | S_IWUSR, show_temp_max_hyst,
Yuan Mu412fec82006-02-05 23:24:16 +01001251 store_temp_max_hyst, 1),
Guenter Roeckbce26c52011-02-04 12:54:14 -08001252 SENSOR_ATTR(temp3_max_hyst, S_IRUGO | S_IWUSR, show_temp_max_hyst,
1253 store_temp_max_hyst, 2),
Guenter Roeckec3e5a12011-02-02 08:46:49 -08001254 SENSOR_ATTR(temp4_max_hyst, S_IRUGO | S_IWUSR, show_temp_max_hyst,
1255 store_temp_max_hyst, 3),
1256 SENSOR_ATTR(temp5_max_hyst, S_IRUGO | S_IWUSR, show_temp_max_hyst,
1257 store_temp_max_hyst, 4),
1258 SENSOR_ATTR(temp6_max_hyst, S_IRUGO | S_IWUSR, show_temp_max_hyst,
1259 store_temp_max_hyst, 5),
1260 SENSOR_ATTR(temp7_max_hyst, S_IRUGO | S_IWUSR, show_temp_max_hyst,
1261 store_temp_max_hyst, 6),
1262 SENSOR_ATTR(temp8_max_hyst, S_IRUGO | S_IWUSR, show_temp_max_hyst,
1263 store_temp_max_hyst, 7),
1264 SENSOR_ATTR(temp9_max_hyst, S_IRUGO | S_IWUSR, show_temp_max_hyst,
1265 store_temp_max_hyst, 8),
Gong Juna157d062009-03-30 21:46:43 +02001266};
1267
1268static struct sensor_device_attribute sda_temp_alarm[] = {
Jean Delvarea4589db2006-03-23 16:30:29 +01001269 SENSOR_ATTR(temp1_alarm, S_IRUGO, show_alarm, NULL, 4),
1270 SENSOR_ATTR(temp2_alarm, S_IRUGO, show_alarm, NULL, 5),
1271 SENSOR_ATTR(temp3_alarm, S_IRUGO, show_alarm, NULL, 13),
Gong Juna157d062009-03-30 21:46:43 +02001272};
1273
1274static struct sensor_device_attribute sda_temp_type[] = {
Jean Delvareda667362007-06-24 11:21:02 +02001275 SENSOR_ATTR(temp1_type, S_IRUGO, show_temp_type, NULL, 0),
1276 SENSOR_ATTR(temp2_type, S_IRUGO, show_temp_type, NULL, 1),
1277 SENSOR_ATTR(temp3_type, S_IRUGO, show_temp_type, NULL, 2),
Yuan Mu412fec82006-02-05 23:24:16 +01001278};
Jean Delvare08e7e272005-04-25 22:43:25 +02001279
Rudolf Marek08c79952006-07-05 18:14:31 +02001280#define show_pwm_reg(reg) \
Guenter Roecke7e1ca62011-02-04 13:24:30 -08001281static ssize_t show_##reg(struct device *dev, struct device_attribute *attr, \
1282 char *buf) \
Rudolf Marek08c79952006-07-05 18:14:31 +02001283{ \
1284 struct w83627ehf_data *data = w83627ehf_update_device(dev); \
Guenter Roecke7e1ca62011-02-04 13:24:30 -08001285 struct sensor_device_attribute *sensor_attr = \
1286 to_sensor_dev_attr(attr); \
Rudolf Marek08c79952006-07-05 18:14:31 +02001287 int nr = sensor_attr->index; \
1288 return sprintf(buf, "%d\n", data->reg[nr]); \
1289}
1290
1291show_pwm_reg(pwm_mode)
1292show_pwm_reg(pwm_enable)
1293show_pwm_reg(pwm)
1294
1295static ssize_t
1296store_pwm_mode(struct device *dev, struct device_attribute *attr,
1297 const char *buf, size_t count)
1298{
David Hubbard1ea6dd32007-06-24 11:16:15 +02001299 struct w83627ehf_data *data = dev_get_drvdata(dev);
Rudolf Marek08c79952006-07-05 18:14:31 +02001300 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
1301 int nr = sensor_attr->index;
Guenter Roeckbce26c52011-02-04 12:54:14 -08001302 unsigned long val;
1303 int err;
Rudolf Marek08c79952006-07-05 18:14:31 +02001304 u16 reg;
1305
Guenter Roeckbce26c52011-02-04 12:54:14 -08001306 err = strict_strtoul(buf, 10, &val);
1307 if (err < 0)
1308 return err;
1309
Rudolf Marek08c79952006-07-05 18:14:31 +02001310 if (val > 1)
1311 return -EINVAL;
1312 mutex_lock(&data->update_lock);
David Hubbard1ea6dd32007-06-24 11:16:15 +02001313 reg = w83627ehf_read_value(data, W83627EHF_REG_PWM_ENABLE[nr]);
Rudolf Marek08c79952006-07-05 18:14:31 +02001314 data->pwm_mode[nr] = val;
1315 reg &= ~(1 << W83627EHF_PWM_MODE_SHIFT[nr]);
1316 if (!val)
1317 reg |= 1 << W83627EHF_PWM_MODE_SHIFT[nr];
David Hubbard1ea6dd32007-06-24 11:16:15 +02001318 w83627ehf_write_value(data, W83627EHF_REG_PWM_ENABLE[nr], reg);
Rudolf Marek08c79952006-07-05 18:14:31 +02001319 mutex_unlock(&data->update_lock);
1320 return count;
1321}
1322
1323static ssize_t
1324store_pwm(struct device *dev, struct device_attribute *attr,
1325 const char *buf, size_t count)
1326{
David Hubbard1ea6dd32007-06-24 11:16:15 +02001327 struct w83627ehf_data *data = dev_get_drvdata(dev);
Rudolf Marek08c79952006-07-05 18:14:31 +02001328 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
1329 int nr = sensor_attr->index;
Guenter Roeckbce26c52011-02-04 12:54:14 -08001330 unsigned long val;
1331 int err;
1332
1333 err = strict_strtoul(buf, 10, &val);
1334 if (err < 0)
1335 return err;
1336
1337 val = SENSORS_LIMIT(val, 0, 255);
Rudolf Marek08c79952006-07-05 18:14:31 +02001338
1339 mutex_lock(&data->update_lock);
1340 data->pwm[nr] = val;
Guenter Roeck279af1a2011-02-13 22:34:47 -08001341 w83627ehf_write_value(data, data->REG_PWM[nr], val);
Rudolf Marek08c79952006-07-05 18:14:31 +02001342 mutex_unlock(&data->update_lock);
1343 return count;
1344}
1345
1346static ssize_t
1347store_pwm_enable(struct device *dev, struct device_attribute *attr,
1348 const char *buf, size_t count)
1349{
David Hubbard1ea6dd32007-06-24 11:16:15 +02001350 struct w83627ehf_data *data = dev_get_drvdata(dev);
Guenter Roeckec3e5a12011-02-02 08:46:49 -08001351 struct w83627ehf_sio_data *sio_data = dev->platform_data;
Rudolf Marek08c79952006-07-05 18:14:31 +02001352 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
1353 int nr = sensor_attr->index;
Guenter Roeckbce26c52011-02-04 12:54:14 -08001354 unsigned long val;
1355 int err;
Rudolf Marek08c79952006-07-05 18:14:31 +02001356 u16 reg;
1357
Guenter Roeckbce26c52011-02-04 12:54:14 -08001358 err = strict_strtoul(buf, 10, &val);
1359 if (err < 0)
1360 return err;
1361
Guenter Roeckb84bb512011-02-13 23:01:25 -08001362 if (!val || (val > 4 && val != data->pwm_enable_orig[nr]))
Rudolf Marek08c79952006-07-05 18:14:31 +02001363 return -EINVAL;
Guenter Roeckec3e5a12011-02-02 08:46:49 -08001364 /* SmartFan III mode is not supported on NCT6776F */
1365 if (sio_data->kind == nct6776 && val == 4)
1366 return -EINVAL;
1367
Rudolf Marek08c79952006-07-05 18:14:31 +02001368 mutex_lock(&data->update_lock);
Rudolf Marek08c79952006-07-05 18:14:31 +02001369 data->pwm_enable[nr] = val;
Guenter Roeckec3e5a12011-02-02 08:46:49 -08001370 if (sio_data->kind == nct6775 || sio_data->kind == nct6776) {
1371 reg = w83627ehf_read_value(data,
1372 NCT6775_REG_FAN_MODE[nr]);
1373 reg &= 0x0f;
1374 reg |= (val - 1) << 4;
1375 w83627ehf_write_value(data,
1376 NCT6775_REG_FAN_MODE[nr], reg);
1377 } else {
1378 reg = w83627ehf_read_value(data, W83627EHF_REG_PWM_ENABLE[nr]);
1379 reg &= ~(0x03 << W83627EHF_PWM_ENABLE_SHIFT[nr]);
1380 reg |= (val - 1) << W83627EHF_PWM_ENABLE_SHIFT[nr];
1381 w83627ehf_write_value(data, W83627EHF_REG_PWM_ENABLE[nr], reg);
1382 }
Rudolf Marek08c79952006-07-05 18:14:31 +02001383 mutex_unlock(&data->update_lock);
1384 return count;
1385}
1386
1387
1388#define show_tol_temp(reg) \
1389static ssize_t show_##reg(struct device *dev, struct device_attribute *attr, \
1390 char *buf) \
1391{ \
1392 struct w83627ehf_data *data = w83627ehf_update_device(dev); \
Guenter Roecke7e1ca62011-02-04 13:24:30 -08001393 struct sensor_device_attribute *sensor_attr = \
1394 to_sensor_dev_attr(attr); \
Rudolf Marek08c79952006-07-05 18:14:31 +02001395 int nr = sensor_attr->index; \
Guenter Roeckbce26c52011-02-04 12:54:14 -08001396 return sprintf(buf, "%d\n", data->reg[nr] * 1000); \
Rudolf Marek08c79952006-07-05 18:14:31 +02001397}
1398
1399show_tol_temp(tolerance)
1400show_tol_temp(target_temp)
1401
1402static ssize_t
1403store_target_temp(struct device *dev, struct device_attribute *attr,
1404 const char *buf, size_t count)
1405{
David Hubbard1ea6dd32007-06-24 11:16:15 +02001406 struct w83627ehf_data *data = dev_get_drvdata(dev);
Rudolf Marek08c79952006-07-05 18:14:31 +02001407 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
1408 int nr = sensor_attr->index;
Guenter Roeckbce26c52011-02-04 12:54:14 -08001409 long val;
1410 int err;
1411
1412 err = strict_strtol(buf, 10, &val);
1413 if (err < 0)
1414 return err;
1415
1416 val = SENSORS_LIMIT(DIV_ROUND_CLOSEST(val, 1000), 0, 127);
Rudolf Marek08c79952006-07-05 18:14:31 +02001417
1418 mutex_lock(&data->update_lock);
1419 data->target_temp[nr] = val;
Guenter Roeck279af1a2011-02-13 22:34:47 -08001420 w83627ehf_write_value(data, data->REG_TARGET[nr], val);
Rudolf Marek08c79952006-07-05 18:14:31 +02001421 mutex_unlock(&data->update_lock);
1422 return count;
1423}
1424
1425static ssize_t
1426store_tolerance(struct device *dev, struct device_attribute *attr,
1427 const char *buf, size_t count)
1428{
David Hubbard1ea6dd32007-06-24 11:16:15 +02001429 struct w83627ehf_data *data = dev_get_drvdata(dev);
Guenter Roeckec3e5a12011-02-02 08:46:49 -08001430 struct w83627ehf_sio_data *sio_data = dev->platform_data;
Rudolf Marek08c79952006-07-05 18:14:31 +02001431 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
1432 int nr = sensor_attr->index;
1433 u16 reg;
Guenter Roeckbce26c52011-02-04 12:54:14 -08001434 long val;
1435 int err;
1436
1437 err = strict_strtol(buf, 10, &val);
1438 if (err < 0)
1439 return err;
1440
Rudolf Marek08c79952006-07-05 18:14:31 +02001441 /* Limit the temp to 0C - 15C */
Guenter Roeckbce26c52011-02-04 12:54:14 -08001442 val = SENSORS_LIMIT(DIV_ROUND_CLOSEST(val, 1000), 0, 15);
Rudolf Marek08c79952006-07-05 18:14:31 +02001443
1444 mutex_lock(&data->update_lock);
Guenter Roeckec3e5a12011-02-02 08:46:49 -08001445 if (sio_data->kind == nct6775 || sio_data->kind == nct6776) {
1446 /* Limit tolerance further for NCT6776F */
1447 if (sio_data->kind == nct6776 && val > 7)
1448 val = 7;
1449 reg = w83627ehf_read_value(data, NCT6775_REG_FAN_MODE[nr]);
Rudolf Marek08c79952006-07-05 18:14:31 +02001450 reg = (reg & 0xf0) | val;
Guenter Roeckec3e5a12011-02-02 08:46:49 -08001451 w83627ehf_write_value(data, NCT6775_REG_FAN_MODE[nr], reg);
1452 } else {
1453 reg = w83627ehf_read_value(data, W83627EHF_REG_TOLERANCE[nr]);
1454 if (nr == 1)
1455 reg = (reg & 0x0f) | (val << 4);
1456 else
1457 reg = (reg & 0xf0) | val;
1458 w83627ehf_write_value(data, W83627EHF_REG_TOLERANCE[nr], reg);
1459 }
1460 data->tolerance[nr] = val;
Rudolf Marek08c79952006-07-05 18:14:31 +02001461 mutex_unlock(&data->update_lock);
1462 return count;
1463}
1464
1465static struct sensor_device_attribute sda_pwm[] = {
1466 SENSOR_ATTR(pwm1, S_IWUSR | S_IRUGO, show_pwm, store_pwm, 0),
1467 SENSOR_ATTR(pwm2, S_IWUSR | S_IRUGO, show_pwm, store_pwm, 1),
1468 SENSOR_ATTR(pwm3, S_IWUSR | S_IRUGO, show_pwm, store_pwm, 2),
1469 SENSOR_ATTR(pwm4, S_IWUSR | S_IRUGO, show_pwm, store_pwm, 3),
1470};
1471
1472static struct sensor_device_attribute sda_pwm_mode[] = {
1473 SENSOR_ATTR(pwm1_mode, S_IWUSR | S_IRUGO, show_pwm_mode,
1474 store_pwm_mode, 0),
1475 SENSOR_ATTR(pwm2_mode, S_IWUSR | S_IRUGO, show_pwm_mode,
1476 store_pwm_mode, 1),
1477 SENSOR_ATTR(pwm3_mode, S_IWUSR | S_IRUGO, show_pwm_mode,
1478 store_pwm_mode, 2),
1479 SENSOR_ATTR(pwm4_mode, S_IWUSR | S_IRUGO, show_pwm_mode,
1480 store_pwm_mode, 3),
1481};
1482
1483static struct sensor_device_attribute sda_pwm_enable[] = {
1484 SENSOR_ATTR(pwm1_enable, S_IWUSR | S_IRUGO, show_pwm_enable,
1485 store_pwm_enable, 0),
1486 SENSOR_ATTR(pwm2_enable, S_IWUSR | S_IRUGO, show_pwm_enable,
1487 store_pwm_enable, 1),
1488 SENSOR_ATTR(pwm3_enable, S_IWUSR | S_IRUGO, show_pwm_enable,
1489 store_pwm_enable, 2),
1490 SENSOR_ATTR(pwm4_enable, S_IWUSR | S_IRUGO, show_pwm_enable,
1491 store_pwm_enable, 3),
1492};
1493
1494static struct sensor_device_attribute sda_target_temp[] = {
1495 SENSOR_ATTR(pwm1_target, S_IWUSR | S_IRUGO, show_target_temp,
1496 store_target_temp, 0),
1497 SENSOR_ATTR(pwm2_target, S_IWUSR | S_IRUGO, show_target_temp,
1498 store_target_temp, 1),
1499 SENSOR_ATTR(pwm3_target, S_IWUSR | S_IRUGO, show_target_temp,
1500 store_target_temp, 2),
1501 SENSOR_ATTR(pwm4_target, S_IWUSR | S_IRUGO, show_target_temp,
1502 store_target_temp, 3),
1503};
1504
1505static struct sensor_device_attribute sda_tolerance[] = {
1506 SENSOR_ATTR(pwm1_tolerance, S_IWUSR | S_IRUGO, show_tolerance,
1507 store_tolerance, 0),
1508 SENSOR_ATTR(pwm2_tolerance, S_IWUSR | S_IRUGO, show_tolerance,
1509 store_tolerance, 1),
1510 SENSOR_ATTR(pwm3_tolerance, S_IWUSR | S_IRUGO, show_tolerance,
1511 store_tolerance, 2),
1512 SENSOR_ATTR(pwm4_tolerance, S_IWUSR | S_IRUGO, show_tolerance,
1513 store_tolerance, 3),
1514};
1515
Rudolf Marek08c79952006-07-05 18:14:31 +02001516/* Smart Fan registers */
1517
1518#define fan_functions(reg, REG) \
1519static ssize_t show_##reg(struct device *dev, struct device_attribute *attr, \
1520 char *buf) \
1521{ \
1522 struct w83627ehf_data *data = w83627ehf_update_device(dev); \
Guenter Roecke7e1ca62011-02-04 13:24:30 -08001523 struct sensor_device_attribute *sensor_attr = \
1524 to_sensor_dev_attr(attr); \
Rudolf Marek08c79952006-07-05 18:14:31 +02001525 int nr = sensor_attr->index; \
1526 return sprintf(buf, "%d\n", data->reg[nr]); \
Guenter Roecke7e1ca62011-02-04 13:24:30 -08001527} \
Rudolf Marek08c79952006-07-05 18:14:31 +02001528static ssize_t \
1529store_##reg(struct device *dev, struct device_attribute *attr, \
1530 const char *buf, size_t count) \
Guenter Roecke7e1ca62011-02-04 13:24:30 -08001531{ \
David Hubbard1ea6dd32007-06-24 11:16:15 +02001532 struct w83627ehf_data *data = dev_get_drvdata(dev); \
Guenter Roecke7e1ca62011-02-04 13:24:30 -08001533 struct sensor_device_attribute *sensor_attr = \
1534 to_sensor_dev_attr(attr); \
Rudolf Marek08c79952006-07-05 18:14:31 +02001535 int nr = sensor_attr->index; \
Guenter Roeckbce26c52011-02-04 12:54:14 -08001536 unsigned long val; \
1537 int err; \
1538 err = strict_strtoul(buf, 10, &val); \
1539 if (err < 0) \
1540 return err; \
1541 val = SENSORS_LIMIT(val, 1, 255); \
Rudolf Marek08c79952006-07-05 18:14:31 +02001542 mutex_lock(&data->update_lock); \
1543 data->reg[nr] = val; \
Guenter Roeckda2e0252010-08-14 21:08:55 +02001544 w83627ehf_write_value(data, data->REG_##REG[nr], val); \
Rudolf Marek08c79952006-07-05 18:14:31 +02001545 mutex_unlock(&data->update_lock); \
1546 return count; \
1547}
1548
Daniel J Blueman41e9a062009-12-14 18:01:37 -08001549fan_functions(fan_start_output, FAN_START_OUTPUT)
1550fan_functions(fan_stop_output, FAN_STOP_OUTPUT)
1551fan_functions(fan_max_output, FAN_MAX_OUTPUT)
1552fan_functions(fan_step_output, FAN_STEP_OUTPUT)
Rudolf Marek08c79952006-07-05 18:14:31 +02001553
1554#define fan_time_functions(reg, REG) \
1555static ssize_t show_##reg(struct device *dev, struct device_attribute *attr, \
1556 char *buf) \
1557{ \
1558 struct w83627ehf_data *data = w83627ehf_update_device(dev); \
Guenter Roecke7e1ca62011-02-04 13:24:30 -08001559 struct sensor_device_attribute *sensor_attr = \
1560 to_sensor_dev_attr(attr); \
Rudolf Marek08c79952006-07-05 18:14:31 +02001561 int nr = sensor_attr->index; \
1562 return sprintf(buf, "%d\n", \
Guenter Roecke7e1ca62011-02-04 13:24:30 -08001563 step_time_from_reg(data->reg[nr], \
1564 data->pwm_mode[nr])); \
Rudolf Marek08c79952006-07-05 18:14:31 +02001565} \
1566\
1567static ssize_t \
1568store_##reg(struct device *dev, struct device_attribute *attr, \
1569 const char *buf, size_t count) \
1570{ \
David Hubbard1ea6dd32007-06-24 11:16:15 +02001571 struct w83627ehf_data *data = dev_get_drvdata(dev); \
Guenter Roecke7e1ca62011-02-04 13:24:30 -08001572 struct sensor_device_attribute *sensor_attr = \
1573 to_sensor_dev_attr(attr); \
Rudolf Marek08c79952006-07-05 18:14:31 +02001574 int nr = sensor_attr->index; \
Guenter Roeckbce26c52011-02-04 12:54:14 -08001575 unsigned long val; \
1576 int err; \
1577 err = strict_strtoul(buf, 10, &val); \
1578 if (err < 0) \
1579 return err; \
1580 val = step_time_to_reg(val, data->pwm_mode[nr]); \
Rudolf Marek08c79952006-07-05 18:14:31 +02001581 mutex_lock(&data->update_lock); \
1582 data->reg[nr] = val; \
David Hubbard1ea6dd32007-06-24 11:16:15 +02001583 w83627ehf_write_value(data, W83627EHF_REG_##REG[nr], val); \
Rudolf Marek08c79952006-07-05 18:14:31 +02001584 mutex_unlock(&data->update_lock); \
1585 return count; \
1586} \
1587
1588fan_time_functions(fan_stop_time, FAN_STOP_TIME)
1589
David Hubbard1ea6dd32007-06-24 11:16:15 +02001590static ssize_t show_name(struct device *dev, struct device_attribute *attr,
1591 char *buf)
1592{
1593 struct w83627ehf_data *data = dev_get_drvdata(dev);
1594
1595 return sprintf(buf, "%s\n", data->name);
1596}
1597static DEVICE_ATTR(name, S_IRUGO, show_name, NULL);
Rudolf Marek08c79952006-07-05 18:14:31 +02001598
1599static struct sensor_device_attribute sda_sf3_arrays_fan4[] = {
1600 SENSOR_ATTR(pwm4_stop_time, S_IWUSR | S_IRUGO, show_fan_stop_time,
1601 store_fan_stop_time, 3),
Daniel J Blueman41e9a062009-12-14 18:01:37 -08001602 SENSOR_ATTR(pwm4_start_output, S_IWUSR | S_IRUGO, show_fan_start_output,
1603 store_fan_start_output, 3),
1604 SENSOR_ATTR(pwm4_stop_output, S_IWUSR | S_IRUGO, show_fan_stop_output,
1605 store_fan_stop_output, 3),
1606 SENSOR_ATTR(pwm4_max_output, S_IWUSR | S_IRUGO, show_fan_max_output,
1607 store_fan_max_output, 3),
1608 SENSOR_ATTR(pwm4_step_output, S_IWUSR | S_IRUGO, show_fan_step_output,
1609 store_fan_step_output, 3),
Rudolf Marek08c79952006-07-05 18:14:31 +02001610};
1611
1612static struct sensor_device_attribute sda_sf3_arrays[] = {
1613 SENSOR_ATTR(pwm1_stop_time, S_IWUSR | S_IRUGO, show_fan_stop_time,
1614 store_fan_stop_time, 0),
1615 SENSOR_ATTR(pwm2_stop_time, S_IWUSR | S_IRUGO, show_fan_stop_time,
1616 store_fan_stop_time, 1),
1617 SENSOR_ATTR(pwm3_stop_time, S_IWUSR | S_IRUGO, show_fan_stop_time,
1618 store_fan_stop_time, 2),
Daniel J Blueman41e9a062009-12-14 18:01:37 -08001619 SENSOR_ATTR(pwm1_start_output, S_IWUSR | S_IRUGO, show_fan_start_output,
1620 store_fan_start_output, 0),
1621 SENSOR_ATTR(pwm2_start_output, S_IWUSR | S_IRUGO, show_fan_start_output,
1622 store_fan_start_output, 1),
1623 SENSOR_ATTR(pwm3_start_output, S_IWUSR | S_IRUGO, show_fan_start_output,
1624 store_fan_start_output, 2),
1625 SENSOR_ATTR(pwm1_stop_output, S_IWUSR | S_IRUGO, show_fan_stop_output,
1626 store_fan_stop_output, 0),
1627 SENSOR_ATTR(pwm2_stop_output, S_IWUSR | S_IRUGO, show_fan_stop_output,
1628 store_fan_stop_output, 1),
1629 SENSOR_ATTR(pwm3_stop_output, S_IWUSR | S_IRUGO, show_fan_stop_output,
1630 store_fan_stop_output, 2),
Guenter Roeckda2e0252010-08-14 21:08:55 +02001631};
Daniel J Blueman41e9a062009-12-14 18:01:37 -08001632
Guenter Roeckda2e0252010-08-14 21:08:55 +02001633
1634/*
1635 * pwm1 and pwm3 don't support max and step settings on all chips.
1636 * Need to check support while generating/removing attribute files.
1637 */
1638static struct sensor_device_attribute sda_sf3_max_step_arrays[] = {
1639 SENSOR_ATTR(pwm1_max_output, S_IWUSR | S_IRUGO, show_fan_max_output,
1640 store_fan_max_output, 0),
1641 SENSOR_ATTR(pwm1_step_output, S_IWUSR | S_IRUGO, show_fan_step_output,
1642 store_fan_step_output, 0),
Daniel J Blueman41e9a062009-12-14 18:01:37 -08001643 SENSOR_ATTR(pwm2_max_output, S_IWUSR | S_IRUGO, show_fan_max_output,
1644 store_fan_max_output, 1),
1645 SENSOR_ATTR(pwm2_step_output, S_IWUSR | S_IRUGO, show_fan_step_output,
1646 store_fan_step_output, 1),
Guenter Roeckda2e0252010-08-14 21:08:55 +02001647 SENSOR_ATTR(pwm3_max_output, S_IWUSR | S_IRUGO, show_fan_max_output,
1648 store_fan_max_output, 2),
1649 SENSOR_ATTR(pwm3_step_output, S_IWUSR | S_IRUGO, show_fan_step_output,
1650 store_fan_step_output, 2),
Rudolf Marek08c79952006-07-05 18:14:31 +02001651};
1652
Jean Delvarefc18d6c2007-06-24 11:19:42 +02001653static ssize_t
1654show_vid(struct device *dev, struct device_attribute *attr, char *buf)
1655{
1656 struct w83627ehf_data *data = dev_get_drvdata(dev);
1657 return sprintf(buf, "%d\n", vid_from_reg(data->vid, data->vrm));
1658}
1659static DEVICE_ATTR(cpu0_vid, S_IRUGO, show_vid, NULL);
1660
Jean Delvare08e7e272005-04-25 22:43:25 +02001661/*
David Hubbard1ea6dd32007-06-24 11:16:15 +02001662 * Driver and device management
Jean Delvare08e7e272005-04-25 22:43:25 +02001663 */
1664
David Hubbardc18beb52006-09-24 21:04:38 +02001665static void w83627ehf_device_remove_files(struct device *dev)
1666{
1667 /* some entries in the following arrays may not have been used in
1668 * device_create_file(), but device_remove_file() will ignore them */
1669 int i;
David Hubbard1ea6dd32007-06-24 11:16:15 +02001670 struct w83627ehf_data *data = dev_get_drvdata(dev);
David Hubbardc18beb52006-09-24 21:04:38 +02001671
1672 for (i = 0; i < ARRAY_SIZE(sda_sf3_arrays); i++)
1673 device_remove_file(dev, &sda_sf3_arrays[i].dev_attr);
Guenter Roeckda2e0252010-08-14 21:08:55 +02001674 for (i = 0; i < ARRAY_SIZE(sda_sf3_max_step_arrays); i++) {
1675 struct sensor_device_attribute *attr =
1676 &sda_sf3_max_step_arrays[i];
Guenter Roeckec3e5a12011-02-02 08:46:49 -08001677 if (data->REG_FAN_STEP_OUTPUT &&
1678 data->REG_FAN_STEP_OUTPUT[attr->index] != 0xff)
Guenter Roeckda2e0252010-08-14 21:08:55 +02001679 device_remove_file(dev, &attr->dev_attr);
1680 }
David Hubbardc18beb52006-09-24 21:04:38 +02001681 for (i = 0; i < ARRAY_SIZE(sda_sf3_arrays_fan4); i++)
1682 device_remove_file(dev, &sda_sf3_arrays_fan4[i].dev_attr);
David Hubbard1ea6dd32007-06-24 11:16:15 +02001683 for (i = 0; i < data->in_num; i++) {
Gong Juna157d062009-03-30 21:46:43 +02001684 if ((i == 6) && data->in6_skip)
1685 continue;
David Hubbardc18beb52006-09-24 21:04:38 +02001686 device_remove_file(dev, &sda_in_input[i].dev_attr);
1687 device_remove_file(dev, &sda_in_alarm[i].dev_attr);
1688 device_remove_file(dev, &sda_in_min[i].dev_attr);
1689 device_remove_file(dev, &sda_in_max[i].dev_attr);
1690 }
1691 for (i = 0; i < 5; i++) {
1692 device_remove_file(dev, &sda_fan_input[i].dev_attr);
1693 device_remove_file(dev, &sda_fan_alarm[i].dev_attr);
1694 device_remove_file(dev, &sda_fan_div[i].dev_attr);
1695 device_remove_file(dev, &sda_fan_min[i].dev_attr);
1696 }
Gong Jun237c8d2f2009-03-30 21:46:42 +02001697 for (i = 0; i < data->pwm_num; i++) {
David Hubbardc18beb52006-09-24 21:04:38 +02001698 device_remove_file(dev, &sda_pwm[i].dev_attr);
1699 device_remove_file(dev, &sda_pwm_mode[i].dev_attr);
1700 device_remove_file(dev, &sda_pwm_enable[i].dev_attr);
1701 device_remove_file(dev, &sda_target_temp[i].dev_attr);
1702 device_remove_file(dev, &sda_tolerance[i].dev_attr);
1703 }
Guenter Roeckd36cf322011-02-07 15:08:54 -08001704 for (i = 0; i < NUM_REG_TEMP; i++) {
1705 if (!(data->have_temp & (1 << i)))
Gong Juna157d062009-03-30 21:46:43 +02001706 continue;
1707 device_remove_file(dev, &sda_temp_input[i].dev_attr);
Guenter Roeckd36cf322011-02-07 15:08:54 -08001708 device_remove_file(dev, &sda_temp_label[i].dev_attr);
Gong Juna157d062009-03-30 21:46:43 +02001709 device_remove_file(dev, &sda_temp_max[i].dev_attr);
1710 device_remove_file(dev, &sda_temp_max_hyst[i].dev_attr);
Guenter Roeckec3e5a12011-02-02 08:46:49 -08001711 if (i > 2)
1712 continue;
Gong Juna157d062009-03-30 21:46:43 +02001713 device_remove_file(dev, &sda_temp_alarm[i].dev_attr);
1714 device_remove_file(dev, &sda_temp_type[i].dev_attr);
1715 }
David Hubbard1ea6dd32007-06-24 11:16:15 +02001716
1717 device_remove_file(dev, &dev_attr_name);
Jean Delvarecbe311f2008-01-03 21:22:44 +01001718 device_remove_file(dev, &dev_attr_cpu0_vid);
David Hubbardc18beb52006-09-24 21:04:38 +02001719}
1720
David Hubbard1ea6dd32007-06-24 11:16:15 +02001721/* Get the monitoring functions started */
1722static inline void __devinit w83627ehf_init_device(struct w83627ehf_data *data)
Jean Delvare08e7e272005-04-25 22:43:25 +02001723{
1724 int i;
Jean Delvareda667362007-06-24 11:21:02 +02001725 u8 tmp, diode;
Jean Delvare08e7e272005-04-25 22:43:25 +02001726
1727 /* Start monitoring is needed */
David Hubbard1ea6dd32007-06-24 11:16:15 +02001728 tmp = w83627ehf_read_value(data, W83627EHF_REG_CONFIG);
Jean Delvare08e7e272005-04-25 22:43:25 +02001729 if (!(tmp & 0x01))
David Hubbard1ea6dd32007-06-24 11:16:15 +02001730 w83627ehf_write_value(data, W83627EHF_REG_CONFIG,
Jean Delvare08e7e272005-04-25 22:43:25 +02001731 tmp | 0x01);
1732
Guenter Roeckd36cf322011-02-07 15:08:54 -08001733 /* Enable temperature sensors if needed */
1734 for (i = 0; i < NUM_REG_TEMP; i++) {
1735 if (!(data->have_temp & (1 << i)))
1736 continue;
Guenter Roeckec3e5a12011-02-02 08:46:49 -08001737 if (!data->reg_temp_config[i])
Guenter Roeckd36cf322011-02-07 15:08:54 -08001738 continue;
David Hubbard1ea6dd32007-06-24 11:16:15 +02001739 tmp = w83627ehf_read_value(data,
Guenter Roeckec3e5a12011-02-02 08:46:49 -08001740 data->reg_temp_config[i]);
Jean Delvare08e7e272005-04-25 22:43:25 +02001741 if (tmp & 0x01)
David Hubbard1ea6dd32007-06-24 11:16:15 +02001742 w83627ehf_write_value(data,
Guenter Roeckec3e5a12011-02-02 08:46:49 -08001743 data->reg_temp_config[i],
Jean Delvare08e7e272005-04-25 22:43:25 +02001744 tmp & 0xfe);
1745 }
Jean Delvared3130f02007-06-24 11:20:13 +02001746
1747 /* Enable VBAT monitoring if needed */
1748 tmp = w83627ehf_read_value(data, W83627EHF_REG_VBAT);
1749 if (!(tmp & 0x01))
1750 w83627ehf_write_value(data, W83627EHF_REG_VBAT, tmp | 0x01);
Jean Delvareda667362007-06-24 11:21:02 +02001751
1752 /* Get thermal sensor types */
1753 diode = w83627ehf_read_value(data, W83627EHF_REG_DIODE);
1754 for (i = 0; i < 3; i++) {
1755 if ((tmp & (0x02 << i)))
1756 data->temp_type[i] = (diode & (0x10 << i)) ? 1 : 2;
1757 else
1758 data->temp_type[i] = 4; /* thermistor */
1759 }
Jean Delvare08e7e272005-04-25 22:43:25 +02001760}
1761
Guenter Roeckec3e5a12011-02-02 08:46:49 -08001762static void w82627ehf_swap_tempreg(struct w83627ehf_data *data,
1763 int r1, int r2)
1764{
1765 u16 tmp;
1766
1767 tmp = data->temp_src[r1];
1768 data->temp_src[r1] = data->temp_src[r2];
1769 data->temp_src[r2] = tmp;
1770
1771 tmp = data->reg_temp[r1];
1772 data->reg_temp[r1] = data->reg_temp[r2];
1773 data->reg_temp[r2] = tmp;
1774
1775 tmp = data->reg_temp_over[r1];
1776 data->reg_temp_over[r1] = data->reg_temp_over[r2];
1777 data->reg_temp_over[r2] = tmp;
1778
1779 tmp = data->reg_temp_hyst[r1];
1780 data->reg_temp_hyst[r1] = data->reg_temp_hyst[r2];
1781 data->reg_temp_hyst[r2] = tmp;
1782
1783 tmp = data->reg_temp_config[r1];
1784 data->reg_temp_config[r1] = data->reg_temp_config[r2];
1785 data->reg_temp_config[r2] = tmp;
1786}
1787
David Hubbard1ea6dd32007-06-24 11:16:15 +02001788static int __devinit w83627ehf_probe(struct platform_device *pdev)
Jean Delvare08e7e272005-04-25 22:43:25 +02001789{
David Hubbard1ea6dd32007-06-24 11:16:15 +02001790 struct device *dev = &pdev->dev;
1791 struct w83627ehf_sio_data *sio_data = dev->platform_data;
Jean Delvare08e7e272005-04-25 22:43:25 +02001792 struct w83627ehf_data *data;
David Hubbard1ea6dd32007-06-24 11:16:15 +02001793 struct resource *res;
Guenter Roeckec3e5a12011-02-02 08:46:49 -08001794 u8 fan3pin, fan4pin, fan4min, fan5pin, en_vrm10;
Jean Delvare08e7e272005-04-25 22:43:25 +02001795 int i, err = 0;
1796
David Hubbard1ea6dd32007-06-24 11:16:15 +02001797 res = platform_get_resource(pdev, IORESOURCE_IO, 0);
1798 if (!request_region(res->start, IOREGION_LENGTH, DRVNAME)) {
Jean Delvare08e7e272005-04-25 22:43:25 +02001799 err = -EBUSY;
David Hubbard1ea6dd32007-06-24 11:16:15 +02001800 dev_err(dev, "Failed to request region 0x%lx-0x%lx\n",
1801 (unsigned long)res->start,
1802 (unsigned long)res->start + IOREGION_LENGTH - 1);
Jean Delvare08e7e272005-04-25 22:43:25 +02001803 goto exit;
1804 }
1805
Guenter Roecke7e1ca62011-02-04 13:24:30 -08001806 data = kzalloc(sizeof(struct w83627ehf_data), GFP_KERNEL);
1807 if (!data) {
Jean Delvare08e7e272005-04-25 22:43:25 +02001808 err = -ENOMEM;
1809 goto exit_release;
1810 }
Jean Delvare08e7e272005-04-25 22:43:25 +02001811
David Hubbard1ea6dd32007-06-24 11:16:15 +02001812 data->addr = res->start;
Ingo Molnar9a61bf62006-01-18 23:19:26 +01001813 mutex_init(&data->lock);
Ingo Molnar9a61bf62006-01-18 23:19:26 +01001814 mutex_init(&data->update_lock);
David Hubbard1ea6dd32007-06-24 11:16:15 +02001815 data->name = w83627ehf_device_names[sio_data->kind];
1816 platform_set_drvdata(pdev, data);
Jean Delvare08e7e272005-04-25 22:43:25 +02001817
Gong Jun237c8d2f2009-03-30 21:46:42 +02001818 /* 627EHG and 627EHF have 10 voltage inputs; 627DHG and 667HG have 9 */
1819 data->in_num = (sio_data->kind == w83627ehf) ? 10 : 9;
Guenter Roeckec3e5a12011-02-02 08:46:49 -08001820 /* 667HG, NCT6775F, and NCT6776F have 3 pwms */
Guenter Roeckc39aeda2010-08-14 21:08:55 +02001821 data->pwm_num = (sio_data->kind == w83667hg
Guenter Roeckec3e5a12011-02-02 08:46:49 -08001822 || sio_data->kind == w83667hg_b
1823 || sio_data->kind == nct6775
1824 || sio_data->kind == nct6776) ? 3 : 4;
Jean Delvare08e7e272005-04-25 22:43:25 +02001825
Guenter Roeckd36cf322011-02-07 15:08:54 -08001826 data->have_temp = 0x07;
Gong Juna157d062009-03-30 21:46:43 +02001827 /* Check temp3 configuration bit for 667HG */
Guenter Roeckd36cf322011-02-07 15:08:54 -08001828 if (sio_data->kind == w83667hg) {
1829 u8 reg;
1830
1831 reg = w83627ehf_read_value(data, W83627EHF_REG_TEMP_CONFIG[2]);
1832 if (reg & 0x01)
1833 data->have_temp &= ~(1 << 2);
1834 else
Guenter Roeckec3e5a12011-02-02 08:46:49 -08001835 data->in6_skip = 1; /* either temp3 or in6 */
1836 }
1837
1838 /* Deal with temperature register setup first. */
1839 if (sio_data->kind == nct6775 || sio_data->kind == nct6776) {
1840 int mask = 0;
1841
1842 /*
1843 * Display temperature sensor output only if it monitors
1844 * a source other than one already reported. Always display
1845 * first three temperature registers, though.
1846 */
1847 for (i = 0; i < NUM_REG_TEMP; i++) {
1848 u8 src;
1849
1850 data->reg_temp[i] = NCT6775_REG_TEMP[i];
1851 data->reg_temp_over[i] = NCT6775_REG_TEMP_OVER[i];
1852 data->reg_temp_hyst[i] = NCT6775_REG_TEMP_HYST[i];
1853 data->reg_temp_config[i] = NCT6775_REG_TEMP_CONFIG[i];
1854
1855 src = w83627ehf_read_value(data,
1856 NCT6775_REG_TEMP_SOURCE[i]);
1857 src &= 0x1f;
1858 if (src && !(mask & (1 << src))) {
1859 data->have_temp |= 1 << i;
1860 mask |= 1 << src;
1861 }
1862
1863 data->temp_src[i] = src;
1864
1865 /*
1866 * Now do some register swapping if index 0..2 don't
1867 * point to SYSTIN(1), CPUIN(2), and AUXIN(3).
1868 * Idea is to have the first three attributes
1869 * report SYSTIN, CPUIN, and AUXIN if possible
1870 * without overriding the basic system configuration.
1871 */
1872 if (i > 0 && data->temp_src[0] != 1
1873 && data->temp_src[i] == 1)
1874 w82627ehf_swap_tempreg(data, 0, i);
1875 if (i > 1 && data->temp_src[1] != 2
1876 && data->temp_src[i] == 2)
1877 w82627ehf_swap_tempreg(data, 1, i);
1878 if (i > 2 && data->temp_src[2] != 3
1879 && data->temp_src[i] == 3)
1880 w82627ehf_swap_tempreg(data, 2, i);
1881 }
1882 if (sio_data->kind == nct6776) {
1883 /*
1884 * On NCT6776, AUXTIN and VIN3 pins are shared.
1885 * Only way to detect it is to check if AUXTIN is used
1886 * as a temperature source, and if that source is
1887 * enabled.
1888 *
1889 * If that is the case, disable in6, which reports VIN3.
1890 * Otherwise disable temp3.
1891 */
1892 if (data->temp_src[2] == 3) {
1893 u8 reg;
1894
1895 if (data->reg_temp_config[2])
1896 reg = w83627ehf_read_value(data,
1897 data->reg_temp_config[2]);
1898 else
1899 reg = 0; /* Assume AUXTIN is used */
1900
1901 if (reg & 0x01)
1902 data->have_temp &= ~(1 << 2);
1903 else
1904 data->in6_skip = 1;
1905 }
1906 }
1907
1908 data->temp_label = nct6776_temp_label;
Guenter Roeckd36cf322011-02-07 15:08:54 -08001909 } else if (sio_data->kind == w83667hg_b) {
1910 u8 reg;
1911
Guenter Roeckec3e5a12011-02-02 08:46:49 -08001912 /*
1913 * Temperature sources are selected with bank 0, registers 0x49
1914 * and 0x4a.
1915 */
1916 for (i = 0; i < ARRAY_SIZE(W83627EHF_REG_TEMP); i++) {
1917 data->reg_temp[i] = W83627EHF_REG_TEMP[i];
1918 data->reg_temp_over[i] = W83627EHF_REG_TEMP_OVER[i];
1919 data->reg_temp_hyst[i] = W83627EHF_REG_TEMP_HYST[i];
1920 data->reg_temp_config[i] = W83627EHF_REG_TEMP_CONFIG[i];
1921 }
Guenter Roeckd36cf322011-02-07 15:08:54 -08001922 reg = w83627ehf_read_value(data, 0x4a);
1923 data->temp_src[0] = reg >> 5;
1924 reg = w83627ehf_read_value(data, 0x49);
1925 data->temp_src[1] = reg & 0x07;
Guenter Roeckec3e5a12011-02-02 08:46:49 -08001926 data->temp_src[2] = (reg >> 4) & 0x07;
Guenter Roeckd36cf322011-02-07 15:08:54 -08001927
1928 /*
1929 * W83667HG-B has another temperature register at 0x7e.
1930 * The temperature source is selected with register 0x7d.
1931 * Support it if the source differs from already reported
1932 * sources.
1933 */
1934 reg = w83627ehf_read_value(data, 0x7d);
1935 reg &= 0x07;
1936 if (reg != data->temp_src[0] && reg != data->temp_src[1]
1937 && reg != data->temp_src[2]) {
1938 data->temp_src[3] = reg;
1939 data->have_temp |= 1 << 3;
1940 }
1941
1942 /*
1943 * Chip supports either AUXTIN or VIN3. Try to find out which
1944 * one.
1945 */
1946 reg = w83627ehf_read_value(data, W83627EHF_REG_TEMP_CONFIG[2]);
1947 if (data->temp_src[2] == 2 && (reg & 0x01))
1948 data->have_temp &= ~(1 << 2);
1949
1950 if ((data->temp_src[2] == 2 && (data->have_temp & (1 << 2)))
1951 || (data->temp_src[3] == 2 && (data->have_temp & (1 << 3))))
1952 data->in6_skip = 1;
1953
1954 data->temp_label = w83667hg_b_temp_label;
Guenter Roeckec3e5a12011-02-02 08:46:49 -08001955 } else {
1956 /* Temperature sources are fixed */
1957 for (i = 0; i < 3; i++) {
1958 data->reg_temp[i] = W83627EHF_REG_TEMP[i];
1959 data->reg_temp_over[i] = W83627EHF_REG_TEMP_OVER[i];
1960 data->reg_temp_hyst[i] = W83627EHF_REG_TEMP_HYST[i];
1961 data->reg_temp_config[i] = W83627EHF_REG_TEMP_CONFIG[i];
1962 }
Gong Juna157d062009-03-30 21:46:43 +02001963 }
1964
Guenter Roeckec3e5a12011-02-02 08:46:49 -08001965 if (sio_data->kind == nct6775) {
Guenter Roeck26bc4402011-02-11 08:00:58 -08001966 data->has_fan_div = true;
1967 data->fan_from_reg = fan_from_reg16;
1968 data->fan_from_reg_min = fan_from_reg8;
Guenter Roeckec3e5a12011-02-02 08:46:49 -08001969 data->REG_PWM = NCT6775_REG_PWM;
1970 data->REG_TARGET = NCT6775_REG_TARGET;
Guenter Roeck26bc4402011-02-11 08:00:58 -08001971 data->REG_FAN = NCT6775_REG_FAN;
Guenter Roeckec3e5a12011-02-02 08:46:49 -08001972 data->REG_FAN_MIN = W83627EHF_REG_FAN_MIN;
1973 data->REG_FAN_START_OUTPUT = NCT6775_REG_FAN_START_OUTPUT;
1974 data->REG_FAN_STOP_OUTPUT = NCT6775_REG_FAN_STOP_OUTPUT;
1975 data->REG_FAN_STOP_TIME = NCT6775_REG_FAN_STOP_TIME;
1976 data->REG_FAN_MAX_OUTPUT = NCT6775_REG_FAN_MAX_OUTPUT;
1977 data->REG_FAN_STEP_OUTPUT = NCT6775_REG_FAN_STEP_OUTPUT;
1978 } else if (sio_data->kind == nct6776) {
Guenter Roeck26bc4402011-02-11 08:00:58 -08001979 data->has_fan_div = false;
1980 data->fan_from_reg = fan_from_reg13;
1981 data->fan_from_reg_min = fan_from_reg13;
Guenter Roeckec3e5a12011-02-02 08:46:49 -08001982 data->REG_PWM = NCT6775_REG_PWM;
1983 data->REG_TARGET = NCT6775_REG_TARGET;
Guenter Roeck26bc4402011-02-11 08:00:58 -08001984 data->REG_FAN = NCT6775_REG_FAN;
Guenter Roeckec3e5a12011-02-02 08:46:49 -08001985 data->REG_FAN_MIN = NCT6776_REG_FAN_MIN;
1986 data->REG_FAN_START_OUTPUT = NCT6775_REG_FAN_START_OUTPUT;
1987 data->REG_FAN_STOP_OUTPUT = NCT6775_REG_FAN_STOP_OUTPUT;
1988 data->REG_FAN_STOP_TIME = NCT6775_REG_FAN_STOP_TIME;
1989 } else if (sio_data->kind == w83667hg_b) {
Guenter Roeck26bc4402011-02-11 08:00:58 -08001990 data->has_fan_div = true;
1991 data->fan_from_reg = fan_from_reg8;
1992 data->fan_from_reg_min = fan_from_reg8;
Guenter Roeckec3e5a12011-02-02 08:46:49 -08001993 data->REG_PWM = W83627EHF_REG_PWM;
1994 data->REG_TARGET = W83627EHF_REG_TARGET;
1995 data->REG_FAN = W83627EHF_REG_FAN;
1996 data->REG_FAN_MIN = W83627EHF_REG_FAN_MIN;
1997 data->REG_FAN_START_OUTPUT = W83627EHF_REG_FAN_START_OUTPUT;
1998 data->REG_FAN_STOP_OUTPUT = W83627EHF_REG_FAN_STOP_OUTPUT;
1999 data->REG_FAN_STOP_TIME = W83627EHF_REG_FAN_STOP_TIME;
Guenter Roeckc39aeda2010-08-14 21:08:55 +02002000 data->REG_FAN_MAX_OUTPUT =
2001 W83627EHF_REG_FAN_MAX_OUTPUT_W83667_B;
2002 data->REG_FAN_STEP_OUTPUT =
2003 W83627EHF_REG_FAN_STEP_OUTPUT_W83667_B;
2004 } else {
Guenter Roeck26bc4402011-02-11 08:00:58 -08002005 data->has_fan_div = true;
2006 data->fan_from_reg = fan_from_reg8;
2007 data->fan_from_reg_min = fan_from_reg8;
Guenter Roeckec3e5a12011-02-02 08:46:49 -08002008 data->REG_PWM = W83627EHF_REG_PWM;
2009 data->REG_TARGET = W83627EHF_REG_TARGET;
2010 data->REG_FAN = W83627EHF_REG_FAN;
2011 data->REG_FAN_MIN = W83627EHF_REG_FAN_MIN;
2012 data->REG_FAN_START_OUTPUT = W83627EHF_REG_FAN_START_OUTPUT;
2013 data->REG_FAN_STOP_OUTPUT = W83627EHF_REG_FAN_STOP_OUTPUT;
2014 data->REG_FAN_STOP_TIME = W83627EHF_REG_FAN_STOP_TIME;
Guenter Roeckc39aeda2010-08-14 21:08:55 +02002015 data->REG_FAN_MAX_OUTPUT =
2016 W83627EHF_REG_FAN_MAX_OUTPUT_COMMON;
2017 data->REG_FAN_STEP_OUTPUT =
2018 W83627EHF_REG_FAN_STEP_OUTPUT_COMMON;
2019 }
Guenter Roeckda2e0252010-08-14 21:08:55 +02002020
Jean Delvare08e7e272005-04-25 22:43:25 +02002021 /* Initialize the chip */
David Hubbard1ea6dd32007-06-24 11:16:15 +02002022 w83627ehf_init_device(data);
Jean Delvare08e7e272005-04-25 22:43:25 +02002023
Jean Delvarefc18d6c2007-06-24 11:19:42 +02002024 data->vrm = vid_which_vrm();
2025 superio_enter(sio_data->sioreg);
Jean Delvarefc18d6c2007-06-24 11:19:42 +02002026 /* Read VID value */
Guenter Roeckec3e5a12011-02-02 08:46:49 -08002027 if (sio_data->kind == w83667hg || sio_data->kind == w83667hg_b ||
2028 sio_data->kind == nct6775 || sio_data->kind == nct6776) {
Gong Jun237c8d2f2009-03-30 21:46:42 +02002029 /* W83667HG has different pins for VID input and output, so
2030 we can get the VID input values directly at logical device D
2031 0xe3. */
2032 superio_select(sio_data->sioreg, W83667HG_LD_VID);
2033 data->vid = superio_inb(sio_data->sioreg, 0xe3);
Jean Delvarecbe311f2008-01-03 21:22:44 +01002034 err = device_create_file(dev, &dev_attr_cpu0_vid);
2035 if (err)
2036 goto exit_release;
Jean Delvare58e6e782008-01-03 07:33:31 -05002037 } else {
Gong Jun237c8d2f2009-03-30 21:46:42 +02002038 superio_select(sio_data->sioreg, W83627EHF_LD_HWM);
2039 if (superio_inb(sio_data->sioreg, SIO_REG_VID_CTRL) & 0x80) {
2040 /* Set VID input sensibility if needed. In theory the
2041 BIOS should have set it, but in practice it's not
2042 always the case. We only do it for the W83627EHF/EHG
2043 because the W83627DHG is more complex in this
2044 respect. */
2045 if (sio_data->kind == w83627ehf) {
2046 en_vrm10 = superio_inb(sio_data->sioreg,
2047 SIO_REG_EN_VRM10);
2048 if ((en_vrm10 & 0x08) && data->vrm == 90) {
2049 dev_warn(dev, "Setting VID input "
2050 "voltage to TTL\n");
2051 superio_outb(sio_data->sioreg,
2052 SIO_REG_EN_VRM10,
2053 en_vrm10 & ~0x08);
2054 } else if (!(en_vrm10 & 0x08)
2055 && data->vrm == 100) {
2056 dev_warn(dev, "Setting VID input "
2057 "voltage to VRM10\n");
2058 superio_outb(sio_data->sioreg,
2059 SIO_REG_EN_VRM10,
2060 en_vrm10 | 0x08);
2061 }
2062 }
2063
2064 data->vid = superio_inb(sio_data->sioreg,
2065 SIO_REG_VID_DATA);
2066 if (sio_data->kind == w83627ehf) /* 6 VID pins only */
2067 data->vid &= 0x3f;
2068
2069 err = device_create_file(dev, &dev_attr_cpu0_vid);
2070 if (err)
2071 goto exit_release;
2072 } else {
2073 dev_info(dev, "VID pins in output mode, CPU VID not "
2074 "available\n");
2075 }
Jean Delvarefc18d6c2007-06-24 11:19:42 +02002076 }
2077
Rudolf Marek08c79952006-07-05 18:14:31 +02002078 /* fan4 and fan5 share some pins with the GPIO and serial flash */
Guenter Roeckec3e5a12011-02-02 08:46:49 -08002079 if (sio_data->kind == nct6775) {
2080 /* On NCT6775, fan4 shares pins with the fdc interface */
2081 fan3pin = 1;
2082 fan4pin = !(superio_inb(sio_data->sioreg, 0x2A) & 0x80);
2083 fan4min = 0;
2084 fan5pin = 0;
2085 } else if (sio_data->kind == nct6776) {
2086 fan3pin = !(superio_inb(sio_data->sioreg, 0x24) & 0x40);
2087 fan4pin = !!(superio_inb(sio_data->sioreg, 0x1C) & 0x01);
2088 fan5pin = !!(superio_inb(sio_data->sioreg, 0x1C) & 0x02);
2089 fan4min = fan4pin;
2090 } else if (sio_data->kind == w83667hg || sio_data->kind == w83667hg_b) {
2091 fan3pin = 1;
Gong Jun237c8d2f2009-03-30 21:46:42 +02002092 fan4pin = superio_inb(sio_data->sioreg, 0x27) & 0x40;
Guenter Roeckec3e5a12011-02-02 08:46:49 -08002093 fan5pin = superio_inb(sio_data->sioreg, 0x27) & 0x20;
2094 fan4min = fan4pin;
Gong Jun237c8d2f2009-03-30 21:46:42 +02002095 } else {
Guenter Roeckec3e5a12011-02-02 08:46:49 -08002096 fan3pin = 1;
Gong Jun237c8d2f2009-03-30 21:46:42 +02002097 fan4pin = !(superio_inb(sio_data->sioreg, 0x29) & 0x06);
Guenter Roeckec3e5a12011-02-02 08:46:49 -08002098 fan5pin = !(superio_inb(sio_data->sioreg, 0x24) & 0x02);
2099 fan4min = fan4pin;
Gong Jun237c8d2f2009-03-30 21:46:42 +02002100 }
David Hubbard1ea6dd32007-06-24 11:16:15 +02002101 superio_exit(sio_data->sioreg);
Rudolf Marek08c79952006-07-05 18:14:31 +02002102
Jean Delvare08e7e272005-04-25 22:43:25 +02002103 /* It looks like fan4 and fan5 pins can be alternatively used
Rudolf Marek14992c72006-10-08 22:02:09 +02002104 as fan on/off switches, but fan5 control is write only :/
2105 We assume that if the serial interface is disabled, designers
2106 connected fan5 as input unless they are emitting log 1, which
2107 is not the default. */
Rudolf Marek08c79952006-07-05 18:14:31 +02002108
Guenter Roeckec3e5a12011-02-02 08:46:49 -08002109 data->has_fan = data->has_fan_min = 0x03; /* fan1 and fan2 */
2110
2111 data->has_fan |= (fan3pin << 2);
2112 data->has_fan_min |= (fan3pin << 2);
2113
2114 /*
2115 * NCT6775F and NCT6776F don't have the W83627EHF_REG_FANDIV1 register
2116 */
2117 if (sio_data->kind == nct6775 || sio_data->kind == nct6776) {
2118 data->has_fan |= (fan4pin << 3) | (fan5pin << 4);
2119 data->has_fan_min |= (fan4min << 3) | (fan5pin << 4);
2120 } else {
2121 i = w83627ehf_read_value(data, W83627EHF_REG_FANDIV1);
2122 if ((i & (1 << 2)) && fan4pin) {
2123 data->has_fan |= (1 << 3);
2124 data->has_fan_min |= (1 << 3);
2125 }
2126 if (!(i & (1 << 1)) && fan5pin) {
2127 data->has_fan |= (1 << 4);
2128 data->has_fan_min |= (1 << 4);
2129 }
2130 }
Jean Delvare08e7e272005-04-25 22:43:25 +02002131
Mark M. Hoffmanea7be662007-08-05 12:19:01 -04002132 /* Read fan clock dividers immediately */
Guenter Roeckec3e5a12011-02-02 08:46:49 -08002133 w83627ehf_update_fan_div_common(dev, data);
2134
2135 /* Read pwm data to save original values */
2136 w83627ehf_update_pwm_common(dev, data);
2137 for (i = 0; i < data->pwm_num; i++)
2138 data->pwm_enable_orig[i] = data->pwm_enable[i];
Mark M. Hoffmanea7be662007-08-05 12:19:01 -04002139
Guenter Roeckb84bb512011-02-13 23:01:25 -08002140 /* Read pwm data to save original values */
2141 w83627ehf_update_pwm_common(dev, data);
2142 for (i = 0; i < data->pwm_num; i++)
2143 data->pwm_enable_orig[i] = data->pwm_enable[i];
2144
Jean Delvare08e7e272005-04-25 22:43:25 +02002145 /* Register sysfs hooks */
Guenter Roecke7e1ca62011-02-04 13:24:30 -08002146 for (i = 0; i < ARRAY_SIZE(sda_sf3_arrays); i++) {
2147 err = device_create_file(dev, &sda_sf3_arrays[i].dev_attr);
2148 if (err)
David Hubbardc18beb52006-09-24 21:04:38 +02002149 goto exit_remove;
Guenter Roecke7e1ca62011-02-04 13:24:30 -08002150 }
Rudolf Marek08c79952006-07-05 18:14:31 +02002151
Guenter Roeckda2e0252010-08-14 21:08:55 +02002152 for (i = 0; i < ARRAY_SIZE(sda_sf3_max_step_arrays); i++) {
2153 struct sensor_device_attribute *attr =
2154 &sda_sf3_max_step_arrays[i];
Guenter Roeckec3e5a12011-02-02 08:46:49 -08002155 if (data->REG_FAN_STEP_OUTPUT &&
2156 data->REG_FAN_STEP_OUTPUT[attr->index] != 0xff) {
Guenter Roeckda2e0252010-08-14 21:08:55 +02002157 err = device_create_file(dev, &attr->dev_attr);
2158 if (err)
2159 goto exit_remove;
2160 }
2161 }
Rudolf Marek08c79952006-07-05 18:14:31 +02002162 /* if fan4 is enabled create the sf3 files for it */
Gong Jun237c8d2f2009-03-30 21:46:42 +02002163 if ((data->has_fan & (1 << 3)) && data->pwm_num >= 4)
David Hubbardc18beb52006-09-24 21:04:38 +02002164 for (i = 0; i < ARRAY_SIZE(sda_sf3_arrays_fan4); i++) {
Guenter Roecke7e1ca62011-02-04 13:24:30 -08002165 err = device_create_file(dev,
2166 &sda_sf3_arrays_fan4[i].dev_attr);
2167 if (err)
David Hubbardc18beb52006-09-24 21:04:38 +02002168 goto exit_remove;
2169 }
Rudolf Marek08c79952006-07-05 18:14:31 +02002170
Gong Juna157d062009-03-30 21:46:43 +02002171 for (i = 0; i < data->in_num; i++) {
2172 if ((i == 6) && data->in6_skip)
2173 continue;
David Hubbardc18beb52006-09-24 21:04:38 +02002174 if ((err = device_create_file(dev, &sda_in_input[i].dev_attr))
2175 || (err = device_create_file(dev,
2176 &sda_in_alarm[i].dev_attr))
2177 || (err = device_create_file(dev,
2178 &sda_in_min[i].dev_attr))
2179 || (err = device_create_file(dev,
2180 &sda_in_max[i].dev_attr)))
2181 goto exit_remove;
Gong Juna157d062009-03-30 21:46:43 +02002182 }
Rudolf Marekcf0676f2006-03-23 16:25:22 +01002183
Yuan Mu412fec82006-02-05 23:24:16 +01002184 for (i = 0; i < 5; i++) {
Rudolf Marek08c79952006-07-05 18:14:31 +02002185 if (data->has_fan & (1 << i)) {
David Hubbardc18beb52006-09-24 21:04:38 +02002186 if ((err = device_create_file(dev,
2187 &sda_fan_input[i].dev_attr))
2188 || (err = device_create_file(dev,
Guenter Roeckec3e5a12011-02-02 08:46:49 -08002189 &sda_fan_alarm[i].dev_attr)))
David Hubbardc18beb52006-09-24 21:04:38 +02002190 goto exit_remove;
Guenter Roeckec3e5a12011-02-02 08:46:49 -08002191 if (sio_data->kind != nct6776) {
2192 err = device_create_file(dev,
2193 &sda_fan_div[i].dev_attr);
2194 if (err)
2195 goto exit_remove;
2196 }
2197 if (data->has_fan_min & (1 << i)) {
2198 err = device_create_file(dev,
2199 &sda_fan_min[i].dev_attr);
2200 if (err)
2201 goto exit_remove;
2202 }
Gong Jun237c8d2f2009-03-30 21:46:42 +02002203 if (i < data->pwm_num &&
David Hubbardc18beb52006-09-24 21:04:38 +02002204 ((err = device_create_file(dev,
2205 &sda_pwm[i].dev_attr))
2206 || (err = device_create_file(dev,
2207 &sda_pwm_mode[i].dev_attr))
2208 || (err = device_create_file(dev,
2209 &sda_pwm_enable[i].dev_attr))
2210 || (err = device_create_file(dev,
2211 &sda_target_temp[i].dev_attr))
2212 || (err = device_create_file(dev,
2213 &sda_tolerance[i].dev_attr))))
2214 goto exit_remove;
Rudolf Marek08c79952006-07-05 18:14:31 +02002215 }
Jean Delvare08e7e272005-04-25 22:43:25 +02002216 }
Rudolf Marek08c79952006-07-05 18:14:31 +02002217
Guenter Roeckd36cf322011-02-07 15:08:54 -08002218 for (i = 0; i < NUM_REG_TEMP; i++) {
2219 if (!(data->have_temp & (1 << i)))
Gong Juna157d062009-03-30 21:46:43 +02002220 continue;
Guenter Roeckd36cf322011-02-07 15:08:54 -08002221 err = device_create_file(dev, &sda_temp_input[i].dev_attr);
2222 if (err)
2223 goto exit_remove;
2224 if (data->temp_label) {
2225 err = device_create_file(dev,
2226 &sda_temp_label[i].dev_attr);
2227 if (err)
2228 goto exit_remove;
2229 }
Guenter Roeckec3e5a12011-02-02 08:46:49 -08002230 if (data->reg_temp_over[i]) {
2231 err = device_create_file(dev,
2232 &sda_temp_max[i].dev_attr);
2233 if (err)
2234 goto exit_remove;
2235 }
2236 if (data->reg_temp_hyst[i]) {
2237 err = device_create_file(dev,
2238 &sda_temp_max_hyst[i].dev_attr);
2239 if (err)
2240 goto exit_remove;
2241 }
Guenter Roeckd36cf322011-02-07 15:08:54 -08002242 if (i > 2)
Guenter Roeckec3e5a12011-02-02 08:46:49 -08002243 continue;
2244 if ((err = device_create_file(dev,
Gong Juna157d062009-03-30 21:46:43 +02002245 &sda_temp_alarm[i].dev_attr))
2246 || (err = device_create_file(dev,
2247 &sda_temp_type[i].dev_attr)))
David Hubbardc18beb52006-09-24 21:04:38 +02002248 goto exit_remove;
Gong Juna157d062009-03-30 21:46:43 +02002249 }
David Hubbardc18beb52006-09-24 21:04:38 +02002250
David Hubbard1ea6dd32007-06-24 11:16:15 +02002251 err = device_create_file(dev, &dev_attr_name);
2252 if (err)
2253 goto exit_remove;
2254
Tony Jones1beeffe2007-08-20 13:46:20 -07002255 data->hwmon_dev = hwmon_device_register(dev);
2256 if (IS_ERR(data->hwmon_dev)) {
2257 err = PTR_ERR(data->hwmon_dev);
David Hubbardc18beb52006-09-24 21:04:38 +02002258 goto exit_remove;
2259 }
Jean Delvare08e7e272005-04-25 22:43:25 +02002260
2261 return 0;
2262
David Hubbardc18beb52006-09-24 21:04:38 +02002263exit_remove:
2264 w83627ehf_device_remove_files(dev);
Jean Delvare08e7e272005-04-25 22:43:25 +02002265 kfree(data);
David Hubbard1ea6dd32007-06-24 11:16:15 +02002266 platform_set_drvdata(pdev, NULL);
Jean Delvare08e7e272005-04-25 22:43:25 +02002267exit_release:
David Hubbard1ea6dd32007-06-24 11:16:15 +02002268 release_region(res->start, IOREGION_LENGTH);
Jean Delvare08e7e272005-04-25 22:43:25 +02002269exit:
2270 return err;
2271}
2272
David Hubbard1ea6dd32007-06-24 11:16:15 +02002273static int __devexit w83627ehf_remove(struct platform_device *pdev)
Jean Delvare08e7e272005-04-25 22:43:25 +02002274{
David Hubbard1ea6dd32007-06-24 11:16:15 +02002275 struct w83627ehf_data *data = platform_get_drvdata(pdev);
Jean Delvare08e7e272005-04-25 22:43:25 +02002276
Tony Jones1beeffe2007-08-20 13:46:20 -07002277 hwmon_device_unregister(data->hwmon_dev);
David Hubbard1ea6dd32007-06-24 11:16:15 +02002278 w83627ehf_device_remove_files(&pdev->dev);
2279 release_region(data->addr, IOREGION_LENGTH);
2280 platform_set_drvdata(pdev, NULL);
Mark M. Hoffman943b0832005-07-15 21:39:18 -04002281 kfree(data);
Jean Delvare08e7e272005-04-25 22:43:25 +02002282
2283 return 0;
2284}
2285
David Hubbard1ea6dd32007-06-24 11:16:15 +02002286static struct platform_driver w83627ehf_driver = {
Laurent Riffardcdaf7932005-11-26 20:37:41 +01002287 .driver = {
Jean Delvare87218842006-09-03 22:36:14 +02002288 .owner = THIS_MODULE,
David Hubbard1ea6dd32007-06-24 11:16:15 +02002289 .name = DRVNAME,
Laurent Riffardcdaf7932005-11-26 20:37:41 +01002290 },
David Hubbard1ea6dd32007-06-24 11:16:15 +02002291 .probe = w83627ehf_probe,
2292 .remove = __devexit_p(w83627ehf_remove),
Jean Delvare08e7e272005-04-25 22:43:25 +02002293};
2294
David Hubbard1ea6dd32007-06-24 11:16:15 +02002295/* w83627ehf_find() looks for a '627 in the Super-I/O config space */
2296static int __init w83627ehf_find(int sioaddr, unsigned short *addr,
2297 struct w83627ehf_sio_data *sio_data)
Jean Delvare08e7e272005-04-25 22:43:25 +02002298{
David Hubbard1ea6dd32007-06-24 11:16:15 +02002299 static const char __initdata sio_name_W83627EHF[] = "W83627EHF";
2300 static const char __initdata sio_name_W83627EHG[] = "W83627EHG";
2301 static const char __initdata sio_name_W83627DHG[] = "W83627DHG";
Jean Delvarec1e48dc2009-06-15 18:39:50 +02002302 static const char __initdata sio_name_W83627DHG_P[] = "W83627DHG-P";
Gong Jun237c8d2f2009-03-30 21:46:42 +02002303 static const char __initdata sio_name_W83667HG[] = "W83667HG";
Guenter Roeckc39aeda2010-08-14 21:08:55 +02002304 static const char __initdata sio_name_W83667HG_B[] = "W83667HG-B";
Guenter Roeckec3e5a12011-02-02 08:46:49 -08002305 static const char __initdata sio_name_NCT6775[] = "NCT6775F";
2306 static const char __initdata sio_name_NCT6776[] = "NCT6776F";
David Hubbard1ea6dd32007-06-24 11:16:15 +02002307
Jean Delvare08e7e272005-04-25 22:43:25 +02002308 u16 val;
David Hubbard1ea6dd32007-06-24 11:16:15 +02002309 const char *sio_name;
Jean Delvare08e7e272005-04-25 22:43:25 +02002310
David Hubbard1ea6dd32007-06-24 11:16:15 +02002311 superio_enter(sioaddr);
Jean Delvare08e7e272005-04-25 22:43:25 +02002312
Jean Delvare67b671b2007-12-06 23:13:42 +01002313 if (force_id)
2314 val = force_id;
2315 else
2316 val = (superio_inb(sioaddr, SIO_REG_DEVID) << 8)
2317 | superio_inb(sioaddr, SIO_REG_DEVID + 1);
David Hubbard657c93b2007-02-14 21:15:04 +01002318 switch (val & SIO_ID_MASK) {
David Hubbard657c93b2007-02-14 21:15:04 +01002319 case SIO_W83627EHF_ID:
David Hubbard1ea6dd32007-06-24 11:16:15 +02002320 sio_data->kind = w83627ehf;
2321 sio_name = sio_name_W83627EHF;
2322 break;
David Hubbard657c93b2007-02-14 21:15:04 +01002323 case SIO_W83627EHG_ID:
David Hubbard1ea6dd32007-06-24 11:16:15 +02002324 sio_data->kind = w83627ehf;
2325 sio_name = sio_name_W83627EHG;
2326 break;
2327 case SIO_W83627DHG_ID:
2328 sio_data->kind = w83627dhg;
2329 sio_name = sio_name_W83627DHG;
David Hubbard657c93b2007-02-14 21:15:04 +01002330 break;
Jean Delvarec1e48dc2009-06-15 18:39:50 +02002331 case SIO_W83627DHG_P_ID:
2332 sio_data->kind = w83627dhg_p;
2333 sio_name = sio_name_W83627DHG_P;
2334 break;
Gong Jun237c8d2f2009-03-30 21:46:42 +02002335 case SIO_W83667HG_ID:
2336 sio_data->kind = w83667hg;
2337 sio_name = sio_name_W83667HG;
2338 break;
Guenter Roeckc39aeda2010-08-14 21:08:55 +02002339 case SIO_W83667HG_B_ID:
2340 sio_data->kind = w83667hg_b;
2341 sio_name = sio_name_W83667HG_B;
2342 break;
Guenter Roeckec3e5a12011-02-02 08:46:49 -08002343 case SIO_NCT6775_ID:
2344 sio_data->kind = nct6775;
2345 sio_name = sio_name_NCT6775;
2346 break;
2347 case SIO_NCT6776_ID:
2348 sio_data->kind = nct6776;
2349 sio_name = sio_name_NCT6776;
2350 break;
David Hubbard657c93b2007-02-14 21:15:04 +01002351 default:
Jean Delvare9f660362007-06-24 11:23:41 +02002352 if (val != 0xffff)
Joe Perchesabdc6fd2010-10-20 06:51:54 +00002353 pr_debug("unsupported chip ID: 0x%04x\n", val);
David Hubbard1ea6dd32007-06-24 11:16:15 +02002354 superio_exit(sioaddr);
Jean Delvare08e7e272005-04-25 22:43:25 +02002355 return -ENODEV;
2356 }
2357
David Hubbard1ea6dd32007-06-24 11:16:15 +02002358 /* We have a known chip, find the HWM I/O address */
2359 superio_select(sioaddr, W83627EHF_LD_HWM);
2360 val = (superio_inb(sioaddr, SIO_REG_ADDR) << 8)
2361 | superio_inb(sioaddr, SIO_REG_ADDR + 1);
Jean Delvare1a641fc2007-04-23 14:41:16 -07002362 *addr = val & IOREGION_ALIGNMENT;
Jean Delvare2d8672c2005-07-19 23:56:35 +02002363 if (*addr == 0) {
Joe Perchesabdc6fd2010-10-20 06:51:54 +00002364 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 +02002365 superio_exit(sioaddr);
Jean Delvare08e7e272005-04-25 22:43:25 +02002366 return -ENODEV;
2367 }
2368
2369 /* Activate logical device if needed */
David Hubbard1ea6dd32007-06-24 11:16:15 +02002370 val = superio_inb(sioaddr, SIO_REG_ENABLE);
David Hubbard475ef852007-06-24 11:17:09 +02002371 if (!(val & 0x01)) {
Guenter Roecke7e1ca62011-02-04 13:24:30 -08002372 pr_warn("Forcibly enabling Super-I/O. "
2373 "Sensor is probably unusable.\n");
David Hubbard1ea6dd32007-06-24 11:16:15 +02002374 superio_outb(sioaddr, SIO_REG_ENABLE, val | 0x01);
David Hubbard475ef852007-06-24 11:17:09 +02002375 }
Jean Delvare08e7e272005-04-25 22:43:25 +02002376
David Hubbard1ea6dd32007-06-24 11:16:15 +02002377 superio_exit(sioaddr);
Joe Perchesabdc6fd2010-10-20 06:51:54 +00002378 pr_info("Found %s chip at %#x\n", sio_name, *addr);
David Hubbard1ea6dd32007-06-24 11:16:15 +02002379 sio_data->sioreg = sioaddr;
2380
Jean Delvare08e7e272005-04-25 22:43:25 +02002381 return 0;
2382}
2383
David Hubbard1ea6dd32007-06-24 11:16:15 +02002384/* when Super-I/O functions move to a separate file, the Super-I/O
2385 * bus will manage the lifetime of the device and this module will only keep
2386 * track of the w83627ehf driver. But since we platform_device_alloc(), we
2387 * must keep track of the device */
2388static struct platform_device *pdev;
2389
Jean Delvare08e7e272005-04-25 22:43:25 +02002390static int __init sensors_w83627ehf_init(void)
2391{
David Hubbard1ea6dd32007-06-24 11:16:15 +02002392 int err;
2393 unsigned short address;
2394 struct resource res;
2395 struct w83627ehf_sio_data sio_data;
2396
2397 /* initialize sio_data->kind and sio_data->sioreg.
2398 *
2399 * when Super-I/O functions move to a separate file, the Super-I/O
2400 * driver will probe 0x2e and 0x4e and auto-detect the presence of a
2401 * w83627ehf hardware monitor, and call probe() */
2402 if (w83627ehf_find(0x2e, &address, &sio_data) &&
2403 w83627ehf_find(0x4e, &address, &sio_data))
Jean Delvare08e7e272005-04-25 22:43:25 +02002404 return -ENODEV;
2405
David Hubbard1ea6dd32007-06-24 11:16:15 +02002406 err = platform_driver_register(&w83627ehf_driver);
2407 if (err)
2408 goto exit;
2409
Guenter Roecke7e1ca62011-02-04 13:24:30 -08002410 pdev = platform_device_alloc(DRVNAME, address);
2411 if (!pdev) {
David Hubbard1ea6dd32007-06-24 11:16:15 +02002412 err = -ENOMEM;
Joe Perchesabdc6fd2010-10-20 06:51:54 +00002413 pr_err("Device allocation failed\n");
David Hubbard1ea6dd32007-06-24 11:16:15 +02002414 goto exit_unregister;
2415 }
2416
2417 err = platform_device_add_data(pdev, &sio_data,
2418 sizeof(struct w83627ehf_sio_data));
2419 if (err) {
Joe Perchesabdc6fd2010-10-20 06:51:54 +00002420 pr_err("Platform data allocation failed\n");
David Hubbard1ea6dd32007-06-24 11:16:15 +02002421 goto exit_device_put;
2422 }
2423
2424 memset(&res, 0, sizeof(res));
2425 res.name = DRVNAME;
2426 res.start = address + IOREGION_OFFSET;
2427 res.end = address + IOREGION_OFFSET + IOREGION_LENGTH - 1;
2428 res.flags = IORESOURCE_IO;
Jean Delvareb9acb642009-01-07 16:37:35 +01002429
2430 err = acpi_check_resource_conflict(&res);
2431 if (err)
Hans de Goede18632f82009-02-17 19:59:54 +01002432 goto exit_device_put;
Jean Delvareb9acb642009-01-07 16:37:35 +01002433
David Hubbard1ea6dd32007-06-24 11:16:15 +02002434 err = platform_device_add_resources(pdev, &res, 1);
2435 if (err) {
Joe Perchesabdc6fd2010-10-20 06:51:54 +00002436 pr_err("Device resource addition failed (%d)\n", err);
David Hubbard1ea6dd32007-06-24 11:16:15 +02002437 goto exit_device_put;
2438 }
2439
2440 /* platform_device_add calls probe() */
2441 err = platform_device_add(pdev);
2442 if (err) {
Joe Perchesabdc6fd2010-10-20 06:51:54 +00002443 pr_err("Device addition failed (%d)\n", err);
David Hubbard1ea6dd32007-06-24 11:16:15 +02002444 goto exit_device_put;
2445 }
2446
2447 return 0;
2448
2449exit_device_put:
2450 platform_device_put(pdev);
2451exit_unregister:
2452 platform_driver_unregister(&w83627ehf_driver);
2453exit:
2454 return err;
Jean Delvare08e7e272005-04-25 22:43:25 +02002455}
2456
2457static void __exit sensors_w83627ehf_exit(void)
2458{
David Hubbard1ea6dd32007-06-24 11:16:15 +02002459 platform_device_unregister(pdev);
2460 platform_driver_unregister(&w83627ehf_driver);
Jean Delvare08e7e272005-04-25 22:43:25 +02002461}
2462
2463MODULE_AUTHOR("Jean Delvare <khali@linux-fr.org>");
2464MODULE_DESCRIPTION("W83627EHF driver");
2465MODULE_LICENSE("GPL");
2466
2467module_init(sensors_w83627ehf_init);
2468module_exit(sensors_w83627ehf_exit);