blob: ec595c606ed4608b5c6a604e867eeb05adcfe5f6 [file] [log] [blame]
Jean Delvare08e7e272005-04-25 22:43:25 +02001/*
Guenter Roeck8969e842012-01-19 11:02:27 -08002 * w83627ehf - Driver for the hardware monitoring functionality of
3 * the Winbond W83627EHF Super-I/O chip
4 * Copyright (C) 2005-2011 Jean Delvare <khali@linux-fr.org>
5 * Copyright (C) 2006 Yuan Mu (Winbond),
6 * Rudolf Marek <r.marek@assembler.cz>
7 * David Hubbard <david.c.hubbard@gmail.com>
8 * Daniel J Blueman <daniel.blueman@gmail.com>
9 * Copyright (C) 2010 Sheng-Yuan Huang (Nuvoton) (PS00)
10 *
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 *
17 * This driver also supports the W83627EHG, which is the lead-free
18 * version of the W83627EHF.
19 *
20 * 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 * Supports the following chips:
35 *
36 * Chip #vin #fan #pwm #temp chip IDs man ID
37 * w83627ehf 10 5 4 3 0x8850 0x88 0x5ca3
38 * 0x8860 0xa1
39 * w83627dhg 9 5 4 3 0xa020 0xc1 0x5ca3
40 * w83627dhg-p 9 5 4 3 0xb070 0xc1 0x5ca3
41 * w83627uhg 8 2 2 3 0xa230 0xc1 0x5ca3
42 * w83667hg 9 5 3 3 0xa510 0xc1 0x5ca3
43 * w83667hg-b 9 5 3 4 0xb350 0xc1 0x5ca3
44 * nct6775f 9 4 3 9 0xb470 0xc1 0x5ca3
45 * nct6776f 9 5 3 9 0xC330 0xc1 0x5ca3
46 */
Jean Delvare08e7e272005-04-25 22:43:25 +020047
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
Jean Delvareeff76872011-11-04 12:00:48 +010064enum kinds {
65 w83627ehf, w83627dhg, w83627dhg_p, w83627uhg,
66 w83667hg, w83667hg_b, nct6775, nct6776,
67};
David Hubbard1ea6dd32007-06-24 11:16:15 +020068
69/* used to set data->name = w83627ehf_device_names[data->sio_kind] */
Guenter Roecke7e1ca62011-02-04 13:24:30 -080070static const char * const w83627ehf_device_names[] = {
David Hubbard1ea6dd32007-06-24 11:16:15 +020071 "w83627ehf",
72 "w83627dhg",
Jean Delvarec1e48dc2009-06-15 18:39:50 +020073 "w83627dhg",
Jean Delvareeff76872011-11-04 12:00:48 +010074 "w83627uhg",
Gong Jun237c8d22009-03-30 21:46:42 +020075 "w83667hg",
Guenter Roeckc39aeda2010-08-14 21:08:55 +020076 "w83667hg",
Guenter Roeckec3e5a12011-02-02 08:46:49 -080077 "nct6775",
78 "nct6776",
David Hubbard1ea6dd32007-06-24 11:16:15 +020079};
80
Jean Delvare67b671b2007-12-06 23:13:42 +010081static unsigned short force_id;
82module_param(force_id, ushort, 0);
83MODULE_PARM_DESC(force_id, "Override the detected device ID");
84
Ian Dobsond42e8692011-03-07 14:21:12 -080085static unsigned short fan_debounce;
86module_param(fan_debounce, ushort, 0);
87MODULE_PARM_DESC(fan_debounce, "Enable debouncing for fan RPM signal");
88
David Hubbard1ea6dd32007-06-24 11:16:15 +020089#define DRVNAME "w83627ehf"
Jean Delvare08e7e272005-04-25 22:43:25 +020090
91/*
92 * Super-I/O constants and functions
93 */
94
Jean Delvare08e7e272005-04-25 22:43:25 +020095#define W83627EHF_LD_HWM 0x0b
Guenter Roecke7e1ca62011-02-04 13:24:30 -080096#define W83667HG_LD_VID 0x0d
Jean Delvare08e7e272005-04-25 22:43:25 +020097
98#define SIO_REG_LDSEL 0x07 /* Logical device select */
99#define SIO_REG_DEVID 0x20 /* Device ID (2 bytes) */
Jean Delvarefc18d6c2007-06-24 11:19:42 +0200100#define SIO_REG_EN_VRM10 0x2C /* GPIO3, GPIO4 selection */
Jean Delvare08e7e272005-04-25 22:43:25 +0200101#define SIO_REG_ENABLE 0x30 /* Logical device enable */
102#define SIO_REG_ADDR 0x60 /* Logical device address (2 bytes) */
Jean Delvarefc18d6c2007-06-24 11:19:42 +0200103#define SIO_REG_VID_CTRL 0xF0 /* VID control */
104#define SIO_REG_VID_DATA 0xF1 /* VID data */
Jean Delvare08e7e272005-04-25 22:43:25 +0200105
David Hubbard657c93b2007-02-14 21:15:04 +0100106#define SIO_W83627EHF_ID 0x8850
107#define SIO_W83627EHG_ID 0x8860
108#define SIO_W83627DHG_ID 0xa020
Jean Delvarec1e48dc2009-06-15 18:39:50 +0200109#define SIO_W83627DHG_P_ID 0xb070
Jean Delvareeff76872011-11-04 12:00:48 +0100110#define SIO_W83627UHG_ID 0xa230
Guenter Roecke7e1ca62011-02-04 13:24:30 -0800111#define SIO_W83667HG_ID 0xa510
Guenter Roeckc39aeda2010-08-14 21:08:55 +0200112#define SIO_W83667HG_B_ID 0xb350
Guenter Roeckec3e5a12011-02-02 08:46:49 -0800113#define SIO_NCT6775_ID 0xb470
114#define SIO_NCT6776_ID 0xc330
David Hubbard657c93b2007-02-14 21:15:04 +0100115#define SIO_ID_MASK 0xFFF0
Jean Delvare08e7e272005-04-25 22:43:25 +0200116
117static inline void
David Hubbard1ea6dd32007-06-24 11:16:15 +0200118superio_outb(int ioreg, int reg, int val)
Jean Delvare08e7e272005-04-25 22:43:25 +0200119{
David Hubbard1ea6dd32007-06-24 11:16:15 +0200120 outb(reg, ioreg);
121 outb(val, ioreg + 1);
Jean Delvare08e7e272005-04-25 22:43:25 +0200122}
123
124static inline int
David Hubbard1ea6dd32007-06-24 11:16:15 +0200125superio_inb(int ioreg, int reg)
Jean Delvare08e7e272005-04-25 22:43:25 +0200126{
David Hubbard1ea6dd32007-06-24 11:16:15 +0200127 outb(reg, ioreg);
128 return inb(ioreg + 1);
Jean Delvare08e7e272005-04-25 22:43:25 +0200129}
130
131static inline void
David Hubbard1ea6dd32007-06-24 11:16:15 +0200132superio_select(int ioreg, int ld)
Jean Delvare08e7e272005-04-25 22:43:25 +0200133{
David Hubbard1ea6dd32007-06-24 11:16:15 +0200134 outb(SIO_REG_LDSEL, ioreg);
135 outb(ld, ioreg + 1);
Jean Delvare08e7e272005-04-25 22:43:25 +0200136}
137
138static inline void
David Hubbard1ea6dd32007-06-24 11:16:15 +0200139superio_enter(int ioreg)
Jean Delvare08e7e272005-04-25 22:43:25 +0200140{
David Hubbard1ea6dd32007-06-24 11:16:15 +0200141 outb(0x87, ioreg);
142 outb(0x87, ioreg);
Jean Delvare08e7e272005-04-25 22:43:25 +0200143}
144
145static inline void
David Hubbard1ea6dd32007-06-24 11:16:15 +0200146superio_exit(int ioreg)
Jean Delvare08e7e272005-04-25 22:43:25 +0200147{
Jonas Jonsson022b75a2010-09-17 17:24:13 +0200148 outb(0xaa, ioreg);
David Hubbard1ea6dd32007-06-24 11:16:15 +0200149 outb(0x02, ioreg);
150 outb(0x02, ioreg + 1);
Jean Delvare08e7e272005-04-25 22:43:25 +0200151}
152
153/*
154 * ISA constants
155 */
156
Guenter Roecke7e1ca62011-02-04 13:24:30 -0800157#define IOREGION_ALIGNMENT (~7)
Jean Delvare1a641fc2007-04-23 14:41:16 -0700158#define IOREGION_OFFSET 5
159#define IOREGION_LENGTH 2
David Hubbard1ea6dd32007-06-24 11:16:15 +0200160#define ADDR_REG_OFFSET 0
161#define DATA_REG_OFFSET 1
Jean Delvare08e7e272005-04-25 22:43:25 +0200162
163#define W83627EHF_REG_BANK 0x4E
164#define W83627EHF_REG_CONFIG 0x40
David Hubbard657c93b2007-02-14 21:15:04 +0100165
Guenter Roeck8969e842012-01-19 11:02:27 -0800166/*
167 * Not currently used:
David Hubbard657c93b2007-02-14 21:15:04 +0100168 * REG_MAN_ID has the value 0x5ca3 for all supported chips.
169 * REG_CHIP_ID == 0x88/0xa1/0xc1 depending on chip model.
170 * REG_MAN_ID is at port 0x4f
Guenter Roeck8969e842012-01-19 11:02:27 -0800171 * REG_CHIP_ID is at port 0x58
172 */
Jean Delvare08e7e272005-04-25 22:43:25 +0200173
174static const u16 W83627EHF_REG_FAN[] = { 0x28, 0x29, 0x2a, 0x3f, 0x553 };
175static const u16 W83627EHF_REG_FAN_MIN[] = { 0x3b, 0x3c, 0x3d, 0x3e, 0x55c };
176
Rudolf Marekcf0676f2006-03-23 16:25:22 +0100177/* The W83627EHF registers for nr=7,8,9 are in bank 5 */
178#define W83627EHF_REG_IN_MAX(nr) ((nr < 7) ? (0x2b + (nr) * 2) : \
179 (0x554 + (((nr) - 7) * 2)))
180#define W83627EHF_REG_IN_MIN(nr) ((nr < 7) ? (0x2c + (nr) * 2) : \
181 (0x555 + (((nr) - 7) * 2)))
182#define W83627EHF_REG_IN(nr) ((nr < 7) ? (0x20 + (nr)) : \
183 (0x550 + (nr) - 7))
184
Guenter Roeckd36cf322011-02-07 15:08:54 -0800185static const u16 W83627EHF_REG_TEMP[] = { 0x27, 0x150, 0x250, 0x7e };
186static const u16 W83627EHF_REG_TEMP_HYST[] = { 0x3a, 0x153, 0x253, 0 };
187static const u16 W83627EHF_REG_TEMP_OVER[] = { 0x39, 0x155, 0x255, 0 };
188static const u16 W83627EHF_REG_TEMP_CONFIG[] = { 0, 0x152, 0x252, 0 };
Jean Delvare08e7e272005-04-25 22:43:25 +0200189
190/* Fan clock dividers are spread over the following five registers */
191#define W83627EHF_REG_FANDIV1 0x47
192#define W83627EHF_REG_FANDIV2 0x4B
193#define W83627EHF_REG_VBAT 0x5D
194#define W83627EHF_REG_DIODE 0x59
195#define W83627EHF_REG_SMI_OVT 0x4C
196
Guenter Roeckec3e5a12011-02-02 08:46:49 -0800197/* NCT6775F has its own fan divider registers */
198#define NCT6775_REG_FANDIV1 0x506
199#define NCT6775_REG_FANDIV2 0x507
Ian Dobsond42e8692011-03-07 14:21:12 -0800200#define NCT6775_REG_FAN_DEBOUNCE 0xf0
Guenter Roeckec3e5a12011-02-02 08:46:49 -0800201
Jean Delvarea4589db2006-03-23 16:30:29 +0100202#define W83627EHF_REG_ALARM1 0x459
203#define W83627EHF_REG_ALARM2 0x45A
204#define W83627EHF_REG_ALARM3 0x45B
205
Dmitry Artamonow363a12a2011-08-12 16:41:11 -0400206#define W83627EHF_REG_CASEOPEN_DET 0x42 /* SMI STATUS #2 */
207#define W83627EHF_REG_CASEOPEN_CLR 0x46 /* SMI MASK #3 */
208
Rudolf Marek08c79952006-07-05 18:14:31 +0200209/* SmartFan registers */
Daniel J Blueman41e9a062009-12-14 18:01:37 -0800210#define W83627EHF_REG_FAN_STEPUP_TIME 0x0f
211#define W83627EHF_REG_FAN_STEPDOWN_TIME 0x0e
212
Rudolf Marek08c79952006-07-05 18:14:31 +0200213/* DC or PWM output fan configuration */
214static const u8 W83627EHF_REG_PWM_ENABLE[] = {
215 0x04, /* SYS FAN0 output mode and PWM mode */
216 0x04, /* CPU FAN0 output mode and PWM mode */
217 0x12, /* AUX FAN mode */
Daniel J Blueman41e9a062009-12-14 18:01:37 -0800218 0x62, /* CPU FAN1 mode */
Rudolf Marek08c79952006-07-05 18:14:31 +0200219};
220
221static const u8 W83627EHF_PWM_MODE_SHIFT[] = { 0, 1, 0, 6 };
222static const u8 W83627EHF_PWM_ENABLE_SHIFT[] = { 2, 4, 1, 4 };
223
224/* FAN Duty Cycle, be used to control */
Guenter Roeck279af1a2011-02-13 22:34:47 -0800225static const u16 W83627EHF_REG_PWM[] = { 0x01, 0x03, 0x11, 0x61 };
226static const u16 W83627EHF_REG_TARGET[] = { 0x05, 0x06, 0x13, 0x63 };
Rudolf Marek08c79952006-07-05 18:14:31 +0200227static const u8 W83627EHF_REG_TOLERANCE[] = { 0x07, 0x07, 0x14, 0x62 };
228
Rudolf Marek08c79952006-07-05 18:14:31 +0200229/* Advanced Fan control, some values are common for all fans */
Guenter Roeck279af1a2011-02-13 22:34:47 -0800230static const u16 W83627EHF_REG_FAN_START_OUTPUT[] = { 0x0a, 0x0b, 0x16, 0x65 };
231static const u16 W83627EHF_REG_FAN_STOP_OUTPUT[] = { 0x08, 0x09, 0x15, 0x64 };
232static const u16 W83627EHF_REG_FAN_STOP_TIME[] = { 0x0c, 0x0d, 0x17, 0x66 };
Guenter Roeckc39aeda2010-08-14 21:08:55 +0200233
Guenter Roeck279af1a2011-02-13 22:34:47 -0800234static const u16 W83627EHF_REG_FAN_MAX_OUTPUT_COMMON[]
Guenter Roeckc39aeda2010-08-14 21:08:55 +0200235 = { 0xff, 0x67, 0xff, 0x69 };
Guenter Roeck279af1a2011-02-13 22:34:47 -0800236static const u16 W83627EHF_REG_FAN_STEP_OUTPUT_COMMON[]
Guenter Roeckc39aeda2010-08-14 21:08:55 +0200237 = { 0xff, 0x68, 0xff, 0x6a };
238
Guenter Roeck279af1a2011-02-13 22:34:47 -0800239static const u16 W83627EHF_REG_FAN_MAX_OUTPUT_W83667_B[] = { 0x67, 0x69, 0x6b };
240static const u16 W83627EHF_REG_FAN_STEP_OUTPUT_W83667_B[]
241 = { 0x68, 0x6a, 0x6c };
Rudolf Marek08c79952006-07-05 18:14:31 +0200242
Guenter Roeckec3e5a12011-02-02 08:46:49 -0800243static const u16 NCT6775_REG_TARGET[] = { 0x101, 0x201, 0x301 };
244static const u16 NCT6775_REG_FAN_MODE[] = { 0x102, 0x202, 0x302 };
245static const u16 NCT6775_REG_FAN_STOP_OUTPUT[] = { 0x105, 0x205, 0x305 };
246static const u16 NCT6775_REG_FAN_START_OUTPUT[] = { 0x106, 0x206, 0x306 };
247static const u16 NCT6775_REG_FAN_STOP_TIME[] = { 0x107, 0x207, 0x307 };
248static const u16 NCT6775_REG_PWM[] = { 0x109, 0x209, 0x309 };
249static const u16 NCT6775_REG_FAN_MAX_OUTPUT[] = { 0x10a, 0x20a, 0x30a };
250static const u16 NCT6775_REG_FAN_STEP_OUTPUT[] = { 0x10b, 0x20b, 0x30b };
Guenter Roeck26bc4402011-02-11 08:00:58 -0800251static const u16 NCT6775_REG_FAN[] = { 0x630, 0x632, 0x634, 0x636, 0x638 };
Guenter Roeckec3e5a12011-02-02 08:46:49 -0800252static const u16 NCT6776_REG_FAN_MIN[] = { 0x63a, 0x63c, 0x63e, 0x640, 0x642};
253
254static const u16 NCT6775_REG_TEMP[]
255 = { 0x27, 0x150, 0x250, 0x73, 0x75, 0x77, 0x62b, 0x62c, 0x62d };
256static const u16 NCT6775_REG_TEMP_CONFIG[]
257 = { 0, 0x152, 0x252, 0, 0, 0, 0x628, 0x629, 0x62A };
258static const u16 NCT6775_REG_TEMP_HYST[]
259 = { 0x3a, 0x153, 0x253, 0, 0, 0, 0x673, 0x678, 0x67D };
260static const u16 NCT6775_REG_TEMP_OVER[]
261 = { 0x39, 0x155, 0x255, 0, 0, 0, 0x672, 0x677, 0x67C };
262static const u16 NCT6775_REG_TEMP_SOURCE[]
263 = { 0x621, 0x622, 0x623, 0x100, 0x200, 0x300, 0x624, 0x625, 0x626 };
264
Guenter Roeckd36cf322011-02-07 15:08:54 -0800265static const char *const w83667hg_b_temp_label[] = {
266 "SYSTIN",
267 "CPUTIN",
268 "AUXTIN",
269 "AMDTSI",
270 "PECI Agent 1",
271 "PECI Agent 2",
272 "PECI Agent 3",
273 "PECI Agent 4"
274};
275
Guenter Roeckec3e5a12011-02-02 08:46:49 -0800276static const char *const nct6775_temp_label[] = {
277 "",
278 "SYSTIN",
279 "CPUTIN",
280 "AUXTIN",
281 "AMD SB-TSI",
282 "PECI Agent 0",
283 "PECI Agent 1",
284 "PECI Agent 2",
285 "PECI Agent 3",
286 "PECI Agent 4",
287 "PECI Agent 5",
288 "PECI Agent 6",
289 "PECI Agent 7",
290 "PCH_CHIP_CPU_MAX_TEMP",
291 "PCH_CHIP_TEMP",
292 "PCH_CPU_TEMP",
293 "PCH_MCH_TEMP",
294 "PCH_DIM0_TEMP",
295 "PCH_DIM1_TEMP",
296 "PCH_DIM2_TEMP",
297 "PCH_DIM3_TEMP"
298};
299
300static const char *const nct6776_temp_label[] = {
301 "",
302 "SYSTIN",
303 "CPUTIN",
304 "AUXTIN",
305 "SMBUSMASTER 0",
306 "SMBUSMASTER 1",
307 "SMBUSMASTER 2",
308 "SMBUSMASTER 3",
309 "SMBUSMASTER 4",
310 "SMBUSMASTER 5",
311 "SMBUSMASTER 6",
312 "SMBUSMASTER 7",
313 "PECI Agent 0",
314 "PECI Agent 1",
315 "PCH_CHIP_CPU_MAX_TEMP",
316 "PCH_CHIP_TEMP",
317 "PCH_CPU_TEMP",
318 "PCH_MCH_TEMP",
319 "PCH_DIM0_TEMP",
320 "PCH_DIM1_TEMP",
321 "PCH_DIM2_TEMP",
322 "PCH_DIM3_TEMP",
323 "BYTE_TEMP"
324};
325
326#define NUM_REG_TEMP ARRAY_SIZE(NCT6775_REG_TEMP)
Guenter Roeckd36cf322011-02-07 15:08:54 -0800327
Jean Delvare17296fe2011-10-20 03:08:27 -0400328static int is_word_sized(u16 reg)
Guenter Roeckbce26c52011-02-04 12:54:14 -0800329{
Guenter Roeckec3e5a12011-02-02 08:46:49 -0800330 return ((((reg & 0xff00) == 0x100
Guenter Roeckbce26c52011-02-04 12:54:14 -0800331 || (reg & 0xff00) == 0x200)
332 && ((reg & 0x00ff) == 0x50
333 || (reg & 0x00ff) == 0x53
Guenter Roeckec3e5a12011-02-02 08:46:49 -0800334 || (reg & 0x00ff) == 0x55))
335 || (reg & 0xfff0) == 0x630
336 || reg == 0x640 || reg == 0x642
337 || ((reg & 0xfff0) == 0x650
338 && (reg & 0x000f) >= 0x06)
339 || reg == 0x73 || reg == 0x75 || reg == 0x77
340 );
Guenter Roeckbce26c52011-02-04 12:54:14 -0800341}
342
Jean Delvare08e7e272005-04-25 22:43:25 +0200343/*
344 * Conversions
345 */
346
Rudolf Marek08c79952006-07-05 18:14:31 +0200347/* 1 is PWM mode, output in ms */
348static inline unsigned int step_time_from_reg(u8 reg, u8 mode)
349{
350 return mode ? 100 * reg : 400 * reg;
351}
352
353static inline u8 step_time_to_reg(unsigned int msec, u8 mode)
354{
355 return SENSORS_LIMIT((mode ? (msec + 50) / 100 :
356 (msec + 200) / 400), 1, 255);
357}
358
Guenter Roeck26bc4402011-02-11 08:00:58 -0800359static unsigned int fan_from_reg8(u16 reg, unsigned int divreg)
Jean Delvare08e7e272005-04-25 22:43:25 +0200360{
Guenter Roeck26bc4402011-02-11 08:00:58 -0800361 if (reg == 0 || reg == 255)
Jean Delvare08e7e272005-04-25 22:43:25 +0200362 return 0;
Guenter Roeck26bc4402011-02-11 08:00:58 -0800363 return 1350000U / (reg << divreg);
364}
365
366static unsigned int fan_from_reg13(u16 reg, unsigned int divreg)
367{
368 if ((reg & 0xff1f) == 0xff1f)
369 return 0;
370
371 reg = (reg & 0x1f) | ((reg & 0xff00) >> 3);
372
373 if (reg == 0)
374 return 0;
375
376 return 1350000U / reg;
377}
378
379static unsigned int fan_from_reg16(u16 reg, unsigned int divreg)
380{
381 if (reg == 0 || reg == 0xffff)
382 return 0;
383
384 /*
385 * Even though the registers are 16 bit wide, the fan divisor
386 * still applies.
387 */
388 return 1350000U / (reg << divreg);
Jean Delvare08e7e272005-04-25 22:43:25 +0200389}
390
391static inline unsigned int
392div_from_reg(u8 reg)
393{
394 return 1 << reg;
395}
396
Guenter Roeck8969e842012-01-19 11:02:27 -0800397/*
398 * Some of the voltage inputs have internal scaling, the tables below
399 * contain 8 (the ADC LSB in mV) * scaling factor * 100
400 */
Jean Delvareeff76872011-11-04 12:00:48 +0100401static const u16 scale_in_common[10] = {
402 800, 800, 1600, 1600, 800, 800, 800, 1600, 1600, 800
403};
404static const u16 scale_in_w83627uhg[9] = {
405 800, 800, 3328, 3424, 800, 800, 0, 3328, 3400
406};
Rudolf Marekcf0676f2006-03-23 16:25:22 +0100407
Jean Delvareeff76872011-11-04 12:00:48 +0100408static inline long in_from_reg(u8 reg, u8 nr, const u16 *scale_in)
Rudolf Marekcf0676f2006-03-23 16:25:22 +0100409{
Jean Delvareeff76872011-11-04 12:00:48 +0100410 return DIV_ROUND_CLOSEST(reg * scale_in[nr], 100);
Rudolf Marekcf0676f2006-03-23 16:25:22 +0100411}
412
Jean Delvareeff76872011-11-04 12:00:48 +0100413static inline u8 in_to_reg(u32 val, u8 nr, const u16 *scale_in)
Rudolf Marekcf0676f2006-03-23 16:25:22 +0100414{
Jean Delvareeff76872011-11-04 12:00:48 +0100415 return SENSORS_LIMIT(DIV_ROUND_CLOSEST(val * 100, scale_in[nr]), 0,
Guenter Roecke7e1ca62011-02-04 13:24:30 -0800416 255);
Rudolf Marekcf0676f2006-03-23 16:25:22 +0100417}
418
Jean Delvare08e7e272005-04-25 22:43:25 +0200419/*
420 * Data structures and manipulation thereof
421 */
422
423struct w83627ehf_data {
David Hubbard1ea6dd32007-06-24 11:16:15 +0200424 int addr; /* IO base of hw monitor block */
425 const char *name;
426
Tony Jones1beeffe2007-08-20 13:46:20 -0700427 struct device *hwmon_dev;
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100428 struct mutex lock;
Jean Delvare08e7e272005-04-25 22:43:25 +0200429
Guenter Roeckec3e5a12011-02-02 08:46:49 -0800430 u16 reg_temp[NUM_REG_TEMP];
431 u16 reg_temp_over[NUM_REG_TEMP];
432 u16 reg_temp_hyst[NUM_REG_TEMP];
433 u16 reg_temp_config[NUM_REG_TEMP];
Guenter Roeckd36cf322011-02-07 15:08:54 -0800434 u8 temp_src[NUM_REG_TEMP];
435 const char * const *temp_label;
436
Guenter Roeck279af1a2011-02-13 22:34:47 -0800437 const u16 *REG_PWM;
438 const u16 *REG_TARGET;
439 const u16 *REG_FAN;
440 const u16 *REG_FAN_MIN;
441 const u16 *REG_FAN_START_OUTPUT;
442 const u16 *REG_FAN_STOP_OUTPUT;
443 const u16 *REG_FAN_STOP_TIME;
444 const u16 *REG_FAN_MAX_OUTPUT;
445 const u16 *REG_FAN_STEP_OUTPUT;
Jean Delvareeff76872011-11-04 12:00:48 +0100446 const u16 *scale_in;
Guenter Roeckda2e0252010-08-14 21:08:55 +0200447
Guenter Roeck26bc4402011-02-11 08:00:58 -0800448 unsigned int (*fan_from_reg)(u16 reg, unsigned int divreg);
449 unsigned int (*fan_from_reg_min)(u16 reg, unsigned int divreg);
450
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100451 struct mutex update_lock;
Jean Delvare08e7e272005-04-25 22:43:25 +0200452 char valid; /* !=0 if following fields are valid */
453 unsigned long last_updated; /* In jiffies */
454
455 /* Register values */
Guenter Roeck83cc8982011-02-06 08:10:15 -0800456 u8 bank; /* current register bank */
David Hubbard1ea6dd32007-06-24 11:16:15 +0200457 u8 in_num; /* number of in inputs we have */
Rudolf Marekcf0676f2006-03-23 16:25:22 +0100458 u8 in[10]; /* Register value */
459 u8 in_max[10]; /* Register value */
460 u8 in_min[10]; /* Register value */
Guenter Roeck3382a912011-02-13 13:08:23 -0800461 unsigned int rpm[5];
Guenter Roeckec3e5a12011-02-02 08:46:49 -0800462 u16 fan_min[5];
Jean Delvare08e7e272005-04-25 22:43:25 +0200463 u8 fan_div[5];
464 u8 has_fan; /* some fan inputs can be disabled */
Guenter Roeckec3e5a12011-02-02 08:46:49 -0800465 u8 has_fan_min; /* some fans don't have min register */
Guenter Roeck26bc4402011-02-11 08:00:58 -0800466 bool has_fan_div;
Jean Delvareda667362007-06-24 11:21:02 +0200467 u8 temp_type[3];
Guenter Roeckec3e5a12011-02-02 08:46:49 -0800468 s16 temp[9];
469 s16 temp_max[9];
470 s16 temp_max_hyst[9];
Jean Delvarea4589db2006-03-23 16:30:29 +0100471 u32 alarms;
Dmitry Artamonow363a12a2011-08-12 16:41:11 -0400472 u8 caseopen;
Rudolf Marek08c79952006-07-05 18:14:31 +0200473
474 u8 pwm_mode[4]; /* 0->DC variable voltage, 1->PWM variable duty cycle */
475 u8 pwm_enable[4]; /* 1->manual
Guenter Roeck8969e842012-01-19 11:02:27 -0800476 * 2->thermal cruise mode (also called SmartFan I)
477 * 3->fan speed cruise mode
478 * 4->variable thermal cruise (also called
479 * SmartFan III)
480 * 5->enhanced variable thermal cruise (also called
481 * SmartFan IV)
482 */
Guenter Roeckb84bb512011-02-13 23:01:25 -0800483 u8 pwm_enable_orig[4]; /* original value of pwm_enable */
Gong Jun237c8d22009-03-30 21:46:42 +0200484 u8 pwm_num; /* number of pwm */
Rudolf Marek08c79952006-07-05 18:14:31 +0200485 u8 pwm[4];
486 u8 target_temp[4];
487 u8 tolerance[4];
488
Daniel J Blueman41e9a062009-12-14 18:01:37 -0800489 u8 fan_start_output[4]; /* minimum fan speed when spinning up */
490 u8 fan_stop_output[4]; /* minimum fan speed when spinning down */
491 u8 fan_stop_time[4]; /* time at minimum before disabling fan */
492 u8 fan_max_output[4]; /* maximum fan speed */
493 u8 fan_step_output[4]; /* rate of change output value */
Jean Delvarefc18d6c2007-06-24 11:19:42 +0200494
495 u8 vid;
496 u8 vrm;
Gong Juna157d062009-03-30 21:46:43 +0200497
Guenter Roeckec3e5a12011-02-02 08:46:49 -0800498 u16 have_temp;
Jean Delvareeff76872011-11-04 12:00:48 +0100499 u8 in6_skip:1;
500 u8 temp3_val_only:1;
Jean Delvare08e7e272005-04-25 22:43:25 +0200501};
502
David Hubbard1ea6dd32007-06-24 11:16:15 +0200503struct w83627ehf_sio_data {
504 int sioreg;
505 enum kinds kind;
506};
507
Guenter Roeck83cc8982011-02-06 08:10:15 -0800508/*
509 * On older chips, only registers 0x50-0x5f are banked.
510 * On more recent chips, all registers are banked.
511 * Assume that is the case and set the bank number for each access.
512 * Cache the bank number so it only needs to be set if it changes.
513 */
David Hubbard1ea6dd32007-06-24 11:16:15 +0200514static inline void w83627ehf_set_bank(struct w83627ehf_data *data, u16 reg)
Jean Delvare08e7e272005-04-25 22:43:25 +0200515{
Guenter Roeck83cc8982011-02-06 08:10:15 -0800516 u8 bank = reg >> 8;
517 if (data->bank != bank) {
David Hubbard1ea6dd32007-06-24 11:16:15 +0200518 outb_p(W83627EHF_REG_BANK, data->addr + ADDR_REG_OFFSET);
Guenter Roeck83cc8982011-02-06 08:10:15 -0800519 outb_p(bank, data->addr + DATA_REG_OFFSET);
520 data->bank = bank;
Jean Delvare08e7e272005-04-25 22:43:25 +0200521 }
522}
523
David Hubbard1ea6dd32007-06-24 11:16:15 +0200524static u16 w83627ehf_read_value(struct w83627ehf_data *data, u16 reg)
Jean Delvare08e7e272005-04-25 22:43:25 +0200525{
Jean Delvare08e7e272005-04-25 22:43:25 +0200526 int res, word_sized = is_word_sized(reg);
527
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100528 mutex_lock(&data->lock);
Jean Delvare08e7e272005-04-25 22:43:25 +0200529
David Hubbard1ea6dd32007-06-24 11:16:15 +0200530 w83627ehf_set_bank(data, reg);
531 outb_p(reg & 0xff, data->addr + ADDR_REG_OFFSET);
532 res = inb_p(data->addr + DATA_REG_OFFSET);
Jean Delvare08e7e272005-04-25 22:43:25 +0200533 if (word_sized) {
534 outb_p((reg & 0xff) + 1,
David Hubbard1ea6dd32007-06-24 11:16:15 +0200535 data->addr + ADDR_REG_OFFSET);
536 res = (res << 8) + inb_p(data->addr + DATA_REG_OFFSET);
Jean Delvare08e7e272005-04-25 22:43:25 +0200537 }
Jean Delvare08e7e272005-04-25 22:43:25 +0200538
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100539 mutex_unlock(&data->lock);
Jean Delvare08e7e272005-04-25 22:43:25 +0200540 return res;
541}
542
Guenter Roecke7e1ca62011-02-04 13:24:30 -0800543static int w83627ehf_write_value(struct w83627ehf_data *data, u16 reg,
544 u16 value)
Jean Delvare08e7e272005-04-25 22:43:25 +0200545{
Jean Delvare08e7e272005-04-25 22:43:25 +0200546 int word_sized = is_word_sized(reg);
547
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100548 mutex_lock(&data->lock);
Jean Delvare08e7e272005-04-25 22:43:25 +0200549
David Hubbard1ea6dd32007-06-24 11:16:15 +0200550 w83627ehf_set_bank(data, reg);
551 outb_p(reg & 0xff, data->addr + ADDR_REG_OFFSET);
Jean Delvare08e7e272005-04-25 22:43:25 +0200552 if (word_sized) {
David Hubbard1ea6dd32007-06-24 11:16:15 +0200553 outb_p(value >> 8, data->addr + DATA_REG_OFFSET);
Jean Delvare08e7e272005-04-25 22:43:25 +0200554 outb_p((reg & 0xff) + 1,
David Hubbard1ea6dd32007-06-24 11:16:15 +0200555 data->addr + ADDR_REG_OFFSET);
Jean Delvare08e7e272005-04-25 22:43:25 +0200556 }
David Hubbard1ea6dd32007-06-24 11:16:15 +0200557 outb_p(value & 0xff, data->addr + DATA_REG_OFFSET);
Jean Delvare08e7e272005-04-25 22:43:25 +0200558
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100559 mutex_unlock(&data->lock);
Jean Delvare08e7e272005-04-25 22:43:25 +0200560 return 0;
561}
562
Jean Delvarec5794cf2011-10-20 03:13:31 -0400563/* We left-align 8-bit temperature values to make the code simpler */
564static u16 w83627ehf_read_temp(struct w83627ehf_data *data, u16 reg)
565{
566 u16 res;
567
568 res = w83627ehf_read_value(data, reg);
569 if (!is_word_sized(reg))
570 res <<= 8;
571
572 return res;
573}
574
575static int w83627ehf_write_temp(struct w83627ehf_data *data, u16 reg,
576 u16 value)
577{
578 if (!is_word_sized(reg))
579 value >>= 8;
580 return w83627ehf_write_value(data, reg, value);
581}
582
Jean Delvare08e7e272005-04-25 22:43:25 +0200583/* This function assumes that the caller holds data->update_lock */
Guenter Roeckec3e5a12011-02-02 08:46:49 -0800584static void nct6775_write_fan_div(struct w83627ehf_data *data, int nr)
585{
586 u8 reg;
587
588 switch (nr) {
589 case 0:
590 reg = (w83627ehf_read_value(data, NCT6775_REG_FANDIV1) & 0x70)
591 | (data->fan_div[0] & 0x7);
592 w83627ehf_write_value(data, NCT6775_REG_FANDIV1, reg);
593 break;
594 case 1:
595 reg = (w83627ehf_read_value(data, NCT6775_REG_FANDIV1) & 0x7)
596 | ((data->fan_div[1] << 4) & 0x70);
597 w83627ehf_write_value(data, NCT6775_REG_FANDIV1, reg);
598 case 2:
599 reg = (w83627ehf_read_value(data, NCT6775_REG_FANDIV2) & 0x70)
600 | (data->fan_div[2] & 0x7);
601 w83627ehf_write_value(data, NCT6775_REG_FANDIV2, reg);
602 break;
603 case 3:
604 reg = (w83627ehf_read_value(data, NCT6775_REG_FANDIV2) & 0x7)
605 | ((data->fan_div[3] << 4) & 0x70);
606 w83627ehf_write_value(data, NCT6775_REG_FANDIV2, reg);
607 break;
608 }
609}
610
611/* This function assumes that the caller holds data->update_lock */
David Hubbard1ea6dd32007-06-24 11:16:15 +0200612static void w83627ehf_write_fan_div(struct w83627ehf_data *data, int nr)
Jean Delvare08e7e272005-04-25 22:43:25 +0200613{
Jean Delvare08e7e272005-04-25 22:43:25 +0200614 u8 reg;
615
616 switch (nr) {
617 case 0:
David Hubbard1ea6dd32007-06-24 11:16:15 +0200618 reg = (w83627ehf_read_value(data, W83627EHF_REG_FANDIV1) & 0xcf)
Jean Delvare08e7e272005-04-25 22:43:25 +0200619 | ((data->fan_div[0] & 0x03) << 4);
Rudolf Marek14992c72006-10-08 22:02:09 +0200620 /* fan5 input control bit is write only, compute the value */
621 reg |= (data->has_fan & (1 << 4)) ? 1 : 0;
David Hubbard1ea6dd32007-06-24 11:16:15 +0200622 w83627ehf_write_value(data, W83627EHF_REG_FANDIV1, reg);
623 reg = (w83627ehf_read_value(data, W83627EHF_REG_VBAT) & 0xdf)
Jean Delvare08e7e272005-04-25 22:43:25 +0200624 | ((data->fan_div[0] & 0x04) << 3);
David Hubbard1ea6dd32007-06-24 11:16:15 +0200625 w83627ehf_write_value(data, W83627EHF_REG_VBAT, reg);
Jean Delvare08e7e272005-04-25 22:43:25 +0200626 break;
627 case 1:
David Hubbard1ea6dd32007-06-24 11:16:15 +0200628 reg = (w83627ehf_read_value(data, W83627EHF_REG_FANDIV1) & 0x3f)
Jean Delvare08e7e272005-04-25 22:43:25 +0200629 | ((data->fan_div[1] & 0x03) << 6);
Rudolf Marek14992c72006-10-08 22:02:09 +0200630 /* fan5 input control bit is write only, compute the value */
631 reg |= (data->has_fan & (1 << 4)) ? 1 : 0;
David Hubbard1ea6dd32007-06-24 11:16:15 +0200632 w83627ehf_write_value(data, W83627EHF_REG_FANDIV1, reg);
633 reg = (w83627ehf_read_value(data, W83627EHF_REG_VBAT) & 0xbf)
Jean Delvare08e7e272005-04-25 22:43:25 +0200634 | ((data->fan_div[1] & 0x04) << 4);
David Hubbard1ea6dd32007-06-24 11:16:15 +0200635 w83627ehf_write_value(data, W83627EHF_REG_VBAT, reg);
Jean Delvare08e7e272005-04-25 22:43:25 +0200636 break;
637 case 2:
David Hubbard1ea6dd32007-06-24 11:16:15 +0200638 reg = (w83627ehf_read_value(data, W83627EHF_REG_FANDIV2) & 0x3f)
Jean Delvare08e7e272005-04-25 22:43:25 +0200639 | ((data->fan_div[2] & 0x03) << 6);
David Hubbard1ea6dd32007-06-24 11:16:15 +0200640 w83627ehf_write_value(data, W83627EHF_REG_FANDIV2, reg);
641 reg = (w83627ehf_read_value(data, W83627EHF_REG_VBAT) & 0x7f)
Jean Delvare08e7e272005-04-25 22:43:25 +0200642 | ((data->fan_div[2] & 0x04) << 5);
David Hubbard1ea6dd32007-06-24 11:16:15 +0200643 w83627ehf_write_value(data, W83627EHF_REG_VBAT, reg);
Jean Delvare08e7e272005-04-25 22:43:25 +0200644 break;
645 case 3:
David Hubbard1ea6dd32007-06-24 11:16:15 +0200646 reg = (w83627ehf_read_value(data, W83627EHF_REG_DIODE) & 0xfc)
Jean Delvare08e7e272005-04-25 22:43:25 +0200647 | (data->fan_div[3] & 0x03);
David Hubbard1ea6dd32007-06-24 11:16:15 +0200648 w83627ehf_write_value(data, W83627EHF_REG_DIODE, reg);
649 reg = (w83627ehf_read_value(data, W83627EHF_REG_SMI_OVT) & 0x7f)
Jean Delvare08e7e272005-04-25 22:43:25 +0200650 | ((data->fan_div[3] & 0x04) << 5);
David Hubbard1ea6dd32007-06-24 11:16:15 +0200651 w83627ehf_write_value(data, W83627EHF_REG_SMI_OVT, reg);
Jean Delvare08e7e272005-04-25 22:43:25 +0200652 break;
653 case 4:
David Hubbard1ea6dd32007-06-24 11:16:15 +0200654 reg = (w83627ehf_read_value(data, W83627EHF_REG_DIODE) & 0x73)
Jean Delvare33725ad2007-04-17 00:32:27 -0700655 | ((data->fan_div[4] & 0x03) << 2)
Jean Delvare08e7e272005-04-25 22:43:25 +0200656 | ((data->fan_div[4] & 0x04) << 5);
David Hubbard1ea6dd32007-06-24 11:16:15 +0200657 w83627ehf_write_value(data, W83627EHF_REG_DIODE, reg);
Jean Delvare08e7e272005-04-25 22:43:25 +0200658 break;
659 }
660}
661
Guenter Roeckec3e5a12011-02-02 08:46:49 -0800662static void w83627ehf_write_fan_div_common(struct device *dev,
663 struct w83627ehf_data *data, int nr)
664{
665 struct w83627ehf_sio_data *sio_data = dev->platform_data;
666
667 if (sio_data->kind == nct6776)
668 ; /* no dividers, do nothing */
669 else if (sio_data->kind == nct6775)
670 nct6775_write_fan_div(data, nr);
671 else
672 w83627ehf_write_fan_div(data, nr);
673}
674
675static void nct6775_update_fan_div(struct w83627ehf_data *data)
676{
677 u8 i;
678
679 i = w83627ehf_read_value(data, NCT6775_REG_FANDIV1);
680 data->fan_div[0] = i & 0x7;
681 data->fan_div[1] = (i & 0x70) >> 4;
682 i = w83627ehf_read_value(data, NCT6775_REG_FANDIV2);
683 data->fan_div[2] = i & 0x7;
684 if (data->has_fan & (1<<3))
685 data->fan_div[3] = (i & 0x70) >> 4;
686}
687
Mark M. Hoffmanea7be662007-08-05 12:19:01 -0400688static void w83627ehf_update_fan_div(struct w83627ehf_data *data)
689{
690 int i;
691
692 i = w83627ehf_read_value(data, W83627EHF_REG_FANDIV1);
693 data->fan_div[0] = (i >> 4) & 0x03;
694 data->fan_div[1] = (i >> 6) & 0x03;
695 i = w83627ehf_read_value(data, W83627EHF_REG_FANDIV2);
696 data->fan_div[2] = (i >> 6) & 0x03;
697 i = w83627ehf_read_value(data, W83627EHF_REG_VBAT);
698 data->fan_div[0] |= (i >> 3) & 0x04;
699 data->fan_div[1] |= (i >> 4) & 0x04;
700 data->fan_div[2] |= (i >> 5) & 0x04;
701 if (data->has_fan & ((1 << 3) | (1 << 4))) {
702 i = w83627ehf_read_value(data, W83627EHF_REG_DIODE);
703 data->fan_div[3] = i & 0x03;
704 data->fan_div[4] = ((i >> 2) & 0x03)
705 | ((i >> 5) & 0x04);
706 }
707 if (data->has_fan & (1 << 3)) {
708 i = w83627ehf_read_value(data, W83627EHF_REG_SMI_OVT);
709 data->fan_div[3] |= (i >> 5) & 0x04;
710 }
711}
712
Guenter Roeckec3e5a12011-02-02 08:46:49 -0800713static void w83627ehf_update_fan_div_common(struct device *dev,
714 struct w83627ehf_data *data)
715{
716 struct w83627ehf_sio_data *sio_data = dev->platform_data;
717
718 if (sio_data->kind == nct6776)
719 ; /* no dividers, do nothing */
720 else if (sio_data->kind == nct6775)
721 nct6775_update_fan_div(data);
722 else
723 w83627ehf_update_fan_div(data);
724}
725
726static void nct6775_update_pwm(struct w83627ehf_data *data)
727{
728 int i;
729 int pwmcfg, fanmodecfg;
730
731 for (i = 0; i < data->pwm_num; i++) {
732 pwmcfg = w83627ehf_read_value(data,
733 W83627EHF_REG_PWM_ENABLE[i]);
734 fanmodecfg = w83627ehf_read_value(data,
735 NCT6775_REG_FAN_MODE[i]);
736 data->pwm_mode[i] =
737 ((pwmcfg >> W83627EHF_PWM_MODE_SHIFT[i]) & 1) ? 0 : 1;
738 data->pwm_enable[i] = ((fanmodecfg >> 4) & 7) + 1;
739 data->tolerance[i] = fanmodecfg & 0x0f;
740 data->pwm[i] = w83627ehf_read_value(data, data->REG_PWM[i]);
741 }
742}
743
744static void w83627ehf_update_pwm(struct w83627ehf_data *data)
745{
746 int i;
747 int pwmcfg = 0, tolerance = 0; /* shut up the compiler */
748
749 for (i = 0; i < data->pwm_num; i++) {
750 if (!(data->has_fan & (1 << i)))
751 continue;
752
753 /* pwmcfg, tolerance mapped for i=0, i=1 to same reg */
754 if (i != 1) {
755 pwmcfg = w83627ehf_read_value(data,
756 W83627EHF_REG_PWM_ENABLE[i]);
757 tolerance = w83627ehf_read_value(data,
758 W83627EHF_REG_TOLERANCE[i]);
759 }
760 data->pwm_mode[i] =
761 ((pwmcfg >> W83627EHF_PWM_MODE_SHIFT[i]) & 1) ? 0 : 1;
762 data->pwm_enable[i] = ((pwmcfg >> W83627EHF_PWM_ENABLE_SHIFT[i])
763 & 3) + 1;
764 data->pwm[i] = w83627ehf_read_value(data, data->REG_PWM[i]);
765
766 data->tolerance[i] = (tolerance >> (i == 1 ? 4 : 0)) & 0x0f;
767 }
768}
769
770static void w83627ehf_update_pwm_common(struct device *dev,
771 struct w83627ehf_data *data)
772{
773 struct w83627ehf_sio_data *sio_data = dev->platform_data;
774
775 if (sio_data->kind == nct6775 || sio_data->kind == nct6776)
776 nct6775_update_pwm(data);
777 else
778 w83627ehf_update_pwm(data);
779}
780
Jean Delvare08e7e272005-04-25 22:43:25 +0200781static struct w83627ehf_data *w83627ehf_update_device(struct device *dev)
782{
David Hubbard1ea6dd32007-06-24 11:16:15 +0200783 struct w83627ehf_data *data = dev_get_drvdata(dev);
Guenter Roeckec3e5a12011-02-02 08:46:49 -0800784 struct w83627ehf_sio_data *sio_data = dev->platform_data;
785
Jean Delvare08e7e272005-04-25 22:43:25 +0200786 int i;
787
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100788 mutex_lock(&data->update_lock);
Jean Delvare08e7e272005-04-25 22:43:25 +0200789
Jean Delvare6b3e4642007-06-24 11:19:01 +0200790 if (time_after(jiffies, data->last_updated + HZ + HZ/2)
Jean Delvare08e7e272005-04-25 22:43:25 +0200791 || !data->valid) {
792 /* Fan clock dividers */
Guenter Roeckec3e5a12011-02-02 08:46:49 -0800793 w83627ehf_update_fan_div_common(dev, data);
Jean Delvare08e7e272005-04-25 22:43:25 +0200794
Rudolf Marekcf0676f2006-03-23 16:25:22 +0100795 /* Measured voltages and limits */
David Hubbard1ea6dd32007-06-24 11:16:15 +0200796 for (i = 0; i < data->in_num; i++) {
Jean Delvare389ef652011-10-13 10:40:53 -0400797 if ((i == 6) && data->in6_skip)
798 continue;
799
David Hubbard1ea6dd32007-06-24 11:16:15 +0200800 data->in[i] = w83627ehf_read_value(data,
Rudolf Marekcf0676f2006-03-23 16:25:22 +0100801 W83627EHF_REG_IN(i));
David Hubbard1ea6dd32007-06-24 11:16:15 +0200802 data->in_min[i] = w83627ehf_read_value(data,
Rudolf Marekcf0676f2006-03-23 16:25:22 +0100803 W83627EHF_REG_IN_MIN(i));
David Hubbard1ea6dd32007-06-24 11:16:15 +0200804 data->in_max[i] = w83627ehf_read_value(data,
Rudolf Marekcf0676f2006-03-23 16:25:22 +0100805 W83627EHF_REG_IN_MAX(i));
806 }
807
Jean Delvare08e7e272005-04-25 22:43:25 +0200808 /* Measured fan speeds and limits */
809 for (i = 0; i < 5; i++) {
Guenter Roeck3382a912011-02-13 13:08:23 -0800810 u16 reg;
811
Jean Delvare08e7e272005-04-25 22:43:25 +0200812 if (!(data->has_fan & (1 << i)))
813 continue;
814
Guenter Roeck3382a912011-02-13 13:08:23 -0800815 reg = w83627ehf_read_value(data, data->REG_FAN[i]);
816 data->rpm[i] = data->fan_from_reg(reg,
817 data->fan_div[i]);
Guenter Roeckec3e5a12011-02-02 08:46:49 -0800818
819 if (data->has_fan_min & (1 << i))
820 data->fan_min[i] = w83627ehf_read_value(data,
Guenter Roeck279af1a2011-02-13 22:34:47 -0800821 data->REG_FAN_MIN[i]);
Jean Delvare08e7e272005-04-25 22:43:25 +0200822
Guenter Roeck8969e842012-01-19 11:02:27 -0800823 /*
824 * If we failed to measure the fan speed and clock
825 * divider can be increased, let's try that for next
826 * time
827 */
Guenter Roeck26bc4402011-02-11 08:00:58 -0800828 if (data->has_fan_div
Guenter Roeck3382a912011-02-13 13:08:23 -0800829 && (reg >= 0xff || (sio_data->kind == nct6775
830 && reg == 0x00))
Guenter Roeckec3e5a12011-02-02 08:46:49 -0800831 && data->fan_div[i] < 0x07) {
Guenter Roecke7e1ca62011-02-04 13:24:30 -0800832 dev_dbg(dev, "Increasing fan%d "
Jean Delvare08e7e272005-04-25 22:43:25 +0200833 "clock divider from %u to %u\n",
Jean Delvare33725ad2007-04-17 00:32:27 -0700834 i + 1, div_from_reg(data->fan_div[i]),
Jean Delvare08e7e272005-04-25 22:43:25 +0200835 div_from_reg(data->fan_div[i] + 1));
836 data->fan_div[i]++;
Guenter Roeckec3e5a12011-02-02 08:46:49 -0800837 w83627ehf_write_fan_div_common(dev, data, i);
Jean Delvare08e7e272005-04-25 22:43:25 +0200838 /* Preserve min limit if possible */
Guenter Roeckec3e5a12011-02-02 08:46:49 -0800839 if ((data->has_fan_min & (1 << i))
840 && data->fan_min[i] >= 2
Jean Delvare08e7e272005-04-25 22:43:25 +0200841 && data->fan_min[i] != 255)
David Hubbard1ea6dd32007-06-24 11:16:15 +0200842 w83627ehf_write_value(data,
Guenter Roeck279af1a2011-02-13 22:34:47 -0800843 data->REG_FAN_MIN[i],
Jean Delvare08e7e272005-04-25 22:43:25 +0200844 (data->fan_min[i] /= 2));
845 }
846 }
847
Guenter Roeckec3e5a12011-02-02 08:46:49 -0800848 w83627ehf_update_pwm_common(dev, data);
849
Guenter Roeckda2e0252010-08-14 21:08:55 +0200850 for (i = 0; i < data->pwm_num; i++) {
851 if (!(data->has_fan & (1 << i)))
852 continue;
853
Guenter Roeckec3e5a12011-02-02 08:46:49 -0800854 data->fan_start_output[i] =
855 w83627ehf_read_value(data,
856 data->REG_FAN_START_OUTPUT[i]);
857 data->fan_stop_output[i] =
858 w83627ehf_read_value(data,
859 data->REG_FAN_STOP_OUTPUT[i]);
860 data->fan_stop_time[i] =
861 w83627ehf_read_value(data,
862 data->REG_FAN_STOP_TIME[i]);
Guenter Roeckda2e0252010-08-14 21:08:55 +0200863
Guenter Roeckec3e5a12011-02-02 08:46:49 -0800864 if (data->REG_FAN_MAX_OUTPUT &&
865 data->REG_FAN_MAX_OUTPUT[i] != 0xff)
Guenter Roeckda2e0252010-08-14 21:08:55 +0200866 data->fan_max_output[i] =
867 w83627ehf_read_value(data,
Guenter Roeckec3e5a12011-02-02 08:46:49 -0800868 data->REG_FAN_MAX_OUTPUT[i]);
Guenter Roeckda2e0252010-08-14 21:08:55 +0200869
Guenter Roeckec3e5a12011-02-02 08:46:49 -0800870 if (data->REG_FAN_STEP_OUTPUT &&
871 data->REG_FAN_STEP_OUTPUT[i] != 0xff)
Guenter Roeckda2e0252010-08-14 21:08:55 +0200872 data->fan_step_output[i] =
873 w83627ehf_read_value(data,
Guenter Roeckec3e5a12011-02-02 08:46:49 -0800874 data->REG_FAN_STEP_OUTPUT[i]);
Guenter Roeckda2e0252010-08-14 21:08:55 +0200875
Rudolf Marek08c79952006-07-05 18:14:31 +0200876 data->target_temp[i] =
David Hubbard1ea6dd32007-06-24 11:16:15 +0200877 w83627ehf_read_value(data,
Guenter Roeck279af1a2011-02-13 22:34:47 -0800878 data->REG_TARGET[i]) &
Rudolf Marek08c79952006-07-05 18:14:31 +0200879 (data->pwm_mode[i] == 1 ? 0x7f : 0xff);
Rudolf Marek08c79952006-07-05 18:14:31 +0200880 }
881
Jean Delvare08e7e272005-04-25 22:43:25 +0200882 /* Measured temperatures and limits */
Guenter Roeckd36cf322011-02-07 15:08:54 -0800883 for (i = 0; i < NUM_REG_TEMP; i++) {
884 if (!(data->have_temp & (1 << i)))
885 continue;
Jean Delvarec5794cf2011-10-20 03:13:31 -0400886 data->temp[i] = w83627ehf_read_temp(data,
Guenter Roeckec3e5a12011-02-02 08:46:49 -0800887 data->reg_temp[i]);
888 if (data->reg_temp_over[i])
889 data->temp_max[i]
Jean Delvarec5794cf2011-10-20 03:13:31 -0400890 = w83627ehf_read_temp(data,
Guenter Roeckec3e5a12011-02-02 08:46:49 -0800891 data->reg_temp_over[i]);
892 if (data->reg_temp_hyst[i])
893 data->temp_max_hyst[i]
Jean Delvarec5794cf2011-10-20 03:13:31 -0400894 = w83627ehf_read_temp(data,
Guenter Roeckec3e5a12011-02-02 08:46:49 -0800895 data->reg_temp_hyst[i]);
Jean Delvare08e7e272005-04-25 22:43:25 +0200896 }
897
David Hubbard1ea6dd32007-06-24 11:16:15 +0200898 data->alarms = w83627ehf_read_value(data,
Jean Delvarea4589db2006-03-23 16:30:29 +0100899 W83627EHF_REG_ALARM1) |
David Hubbard1ea6dd32007-06-24 11:16:15 +0200900 (w83627ehf_read_value(data,
Jean Delvarea4589db2006-03-23 16:30:29 +0100901 W83627EHF_REG_ALARM2) << 8) |
David Hubbard1ea6dd32007-06-24 11:16:15 +0200902 (w83627ehf_read_value(data,
Jean Delvarea4589db2006-03-23 16:30:29 +0100903 W83627EHF_REG_ALARM3) << 16);
904
Dmitry Artamonow363a12a2011-08-12 16:41:11 -0400905 data->caseopen = w83627ehf_read_value(data,
906 W83627EHF_REG_CASEOPEN_DET);
907
Jean Delvare08e7e272005-04-25 22:43:25 +0200908 data->last_updated = jiffies;
909 data->valid = 1;
910 }
911
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100912 mutex_unlock(&data->update_lock);
Jean Delvare08e7e272005-04-25 22:43:25 +0200913 return data;
914}
915
916/*
917 * Sysfs callback functions
918 */
Rudolf Marekcf0676f2006-03-23 16:25:22 +0100919#define show_in_reg(reg) \
920static ssize_t \
921show_##reg(struct device *dev, struct device_attribute *attr, \
922 char *buf) \
923{ \
924 struct w83627ehf_data *data = w83627ehf_update_device(dev); \
Guenter Roecke7e1ca62011-02-04 13:24:30 -0800925 struct sensor_device_attribute *sensor_attr = \
926 to_sensor_dev_attr(attr); \
Rudolf Marekcf0676f2006-03-23 16:25:22 +0100927 int nr = sensor_attr->index; \
Jean Delvareeff76872011-11-04 12:00:48 +0100928 return sprintf(buf, "%ld\n", in_from_reg(data->reg[nr], nr, \
929 data->scale_in)); \
Rudolf Marekcf0676f2006-03-23 16:25:22 +0100930}
931show_in_reg(in)
932show_in_reg(in_min)
933show_in_reg(in_max)
934
935#define store_in_reg(REG, reg) \
936static ssize_t \
Guenter Roecke7e1ca62011-02-04 13:24:30 -0800937store_in_##reg(struct device *dev, struct device_attribute *attr, \
938 const char *buf, size_t count) \
Rudolf Marekcf0676f2006-03-23 16:25:22 +0100939{ \
David Hubbard1ea6dd32007-06-24 11:16:15 +0200940 struct w83627ehf_data *data = dev_get_drvdata(dev); \
Guenter Roecke7e1ca62011-02-04 13:24:30 -0800941 struct sensor_device_attribute *sensor_attr = \
942 to_sensor_dev_attr(attr); \
Rudolf Marekcf0676f2006-03-23 16:25:22 +0100943 int nr = sensor_attr->index; \
Guenter Roeckbce26c52011-02-04 12:54:14 -0800944 unsigned long val; \
945 int err; \
Frans Meulenbroeks179c4fd2012-01-04 20:58:52 +0100946 err = kstrtoul(buf, 10, &val); \
Guenter Roeckbce26c52011-02-04 12:54:14 -0800947 if (err < 0) \
948 return err; \
Rudolf Marekcf0676f2006-03-23 16:25:22 +0100949 mutex_lock(&data->update_lock); \
Jean Delvareeff76872011-11-04 12:00:48 +0100950 data->in_##reg[nr] = in_to_reg(val, nr, data->scale_in); \
David Hubbard1ea6dd32007-06-24 11:16:15 +0200951 w83627ehf_write_value(data, W83627EHF_REG_IN_##REG(nr), \
Rudolf Marekcf0676f2006-03-23 16:25:22 +0100952 data->in_##reg[nr]); \
953 mutex_unlock(&data->update_lock); \
954 return count; \
955}
956
957store_in_reg(MIN, min)
958store_in_reg(MAX, max)
959
Guenter Roecke7e1ca62011-02-04 13:24:30 -0800960static ssize_t show_alarm(struct device *dev, struct device_attribute *attr,
961 char *buf)
Jean Delvarea4589db2006-03-23 16:30:29 +0100962{
963 struct w83627ehf_data *data = w83627ehf_update_device(dev);
964 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
965 int nr = sensor_attr->index;
966 return sprintf(buf, "%u\n", (data->alarms >> nr) & 0x01);
967}
968
Rudolf Marekcf0676f2006-03-23 16:25:22 +0100969static struct sensor_device_attribute sda_in_input[] = {
970 SENSOR_ATTR(in0_input, S_IRUGO, show_in, NULL, 0),
971 SENSOR_ATTR(in1_input, S_IRUGO, show_in, NULL, 1),
972 SENSOR_ATTR(in2_input, S_IRUGO, show_in, NULL, 2),
973 SENSOR_ATTR(in3_input, S_IRUGO, show_in, NULL, 3),
974 SENSOR_ATTR(in4_input, S_IRUGO, show_in, NULL, 4),
975 SENSOR_ATTR(in5_input, S_IRUGO, show_in, NULL, 5),
976 SENSOR_ATTR(in6_input, S_IRUGO, show_in, NULL, 6),
977 SENSOR_ATTR(in7_input, S_IRUGO, show_in, NULL, 7),
978 SENSOR_ATTR(in8_input, S_IRUGO, show_in, NULL, 8),
979 SENSOR_ATTR(in9_input, S_IRUGO, show_in, NULL, 9),
980};
981
Jean Delvarea4589db2006-03-23 16:30:29 +0100982static struct sensor_device_attribute sda_in_alarm[] = {
983 SENSOR_ATTR(in0_alarm, S_IRUGO, show_alarm, NULL, 0),
984 SENSOR_ATTR(in1_alarm, S_IRUGO, show_alarm, NULL, 1),
985 SENSOR_ATTR(in2_alarm, S_IRUGO, show_alarm, NULL, 2),
986 SENSOR_ATTR(in3_alarm, S_IRUGO, show_alarm, NULL, 3),
987 SENSOR_ATTR(in4_alarm, S_IRUGO, show_alarm, NULL, 8),
988 SENSOR_ATTR(in5_alarm, S_IRUGO, show_alarm, NULL, 21),
989 SENSOR_ATTR(in6_alarm, S_IRUGO, show_alarm, NULL, 20),
990 SENSOR_ATTR(in7_alarm, S_IRUGO, show_alarm, NULL, 16),
991 SENSOR_ATTR(in8_alarm, S_IRUGO, show_alarm, NULL, 17),
992 SENSOR_ATTR(in9_alarm, S_IRUGO, show_alarm, NULL, 19),
993};
994
Rudolf Marekcf0676f2006-03-23 16:25:22 +0100995static struct sensor_device_attribute sda_in_min[] = {
Guenter Roecke7e1ca62011-02-04 13:24:30 -0800996 SENSOR_ATTR(in0_min, S_IWUSR | S_IRUGO, show_in_min, store_in_min, 0),
997 SENSOR_ATTR(in1_min, S_IWUSR | S_IRUGO, show_in_min, store_in_min, 1),
998 SENSOR_ATTR(in2_min, S_IWUSR | S_IRUGO, show_in_min, store_in_min, 2),
999 SENSOR_ATTR(in3_min, S_IWUSR | S_IRUGO, show_in_min, store_in_min, 3),
1000 SENSOR_ATTR(in4_min, S_IWUSR | S_IRUGO, show_in_min, store_in_min, 4),
1001 SENSOR_ATTR(in5_min, S_IWUSR | S_IRUGO, show_in_min, store_in_min, 5),
1002 SENSOR_ATTR(in6_min, S_IWUSR | S_IRUGO, show_in_min, store_in_min, 6),
1003 SENSOR_ATTR(in7_min, S_IWUSR | S_IRUGO, show_in_min, store_in_min, 7),
1004 SENSOR_ATTR(in8_min, S_IWUSR | S_IRUGO, show_in_min, store_in_min, 8),
1005 SENSOR_ATTR(in9_min, S_IWUSR | S_IRUGO, show_in_min, store_in_min, 9),
Rudolf Marekcf0676f2006-03-23 16:25:22 +01001006};
1007
1008static struct sensor_device_attribute sda_in_max[] = {
Guenter Roecke7e1ca62011-02-04 13:24:30 -08001009 SENSOR_ATTR(in0_max, S_IWUSR | S_IRUGO, show_in_max, store_in_max, 0),
1010 SENSOR_ATTR(in1_max, S_IWUSR | S_IRUGO, show_in_max, store_in_max, 1),
1011 SENSOR_ATTR(in2_max, S_IWUSR | S_IRUGO, show_in_max, store_in_max, 2),
1012 SENSOR_ATTR(in3_max, S_IWUSR | S_IRUGO, show_in_max, store_in_max, 3),
1013 SENSOR_ATTR(in4_max, S_IWUSR | S_IRUGO, show_in_max, store_in_max, 4),
1014 SENSOR_ATTR(in5_max, S_IWUSR | S_IRUGO, show_in_max, store_in_max, 5),
1015 SENSOR_ATTR(in6_max, S_IWUSR | S_IRUGO, show_in_max, store_in_max, 6),
1016 SENSOR_ATTR(in7_max, S_IWUSR | S_IRUGO, show_in_max, store_in_max, 7),
1017 SENSOR_ATTR(in8_max, S_IWUSR | S_IRUGO, show_in_max, store_in_max, 8),
1018 SENSOR_ATTR(in9_max, S_IWUSR | S_IRUGO, show_in_max, store_in_max, 9),
Rudolf Marekcf0676f2006-03-23 16:25:22 +01001019};
1020
Guenter Roeckec3e5a12011-02-02 08:46:49 -08001021static ssize_t
1022show_fan(struct device *dev, struct device_attribute *attr, char *buf)
1023{
1024 struct w83627ehf_data *data = w83627ehf_update_device(dev);
1025 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
1026 int nr = sensor_attr->index;
Guenter Roeck3382a912011-02-13 13:08:23 -08001027 return sprintf(buf, "%d\n", data->rpm[nr]);
Jean Delvare08e7e272005-04-25 22:43:25 +02001028}
Guenter Roeckec3e5a12011-02-02 08:46:49 -08001029
1030static ssize_t
1031show_fan_min(struct device *dev, struct device_attribute *attr, char *buf)
1032{
1033 struct w83627ehf_data *data = w83627ehf_update_device(dev);
1034 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
1035 int nr = sensor_attr->index;
1036 return sprintf(buf, "%d\n",
Guenter Roeck26bc4402011-02-11 08:00:58 -08001037 data->fan_from_reg_min(data->fan_min[nr],
1038 data->fan_div[nr]));
Guenter Roeckec3e5a12011-02-02 08:46:49 -08001039}
Jean Delvare08e7e272005-04-25 22:43:25 +02001040
1041static ssize_t
Yuan Mu412fec82006-02-05 23:24:16 +01001042show_fan_div(struct device *dev, struct device_attribute *attr,
1043 char *buf)
Jean Delvare08e7e272005-04-25 22:43:25 +02001044{
1045 struct w83627ehf_data *data = w83627ehf_update_device(dev);
Yuan Mu412fec82006-02-05 23:24:16 +01001046 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
1047 int nr = sensor_attr->index;
1048 return sprintf(buf, "%u\n", div_from_reg(data->fan_div[nr]));
Jean Delvare08e7e272005-04-25 22:43:25 +02001049}
1050
1051static ssize_t
Yuan Mu412fec82006-02-05 23:24:16 +01001052store_fan_min(struct device *dev, struct device_attribute *attr,
1053 const char *buf, size_t count)
Jean Delvare08e7e272005-04-25 22:43:25 +02001054{
David Hubbard1ea6dd32007-06-24 11:16:15 +02001055 struct w83627ehf_data *data = dev_get_drvdata(dev);
Yuan Mu412fec82006-02-05 23:24:16 +01001056 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
1057 int nr = sensor_attr->index;
Guenter Roeckbce26c52011-02-04 12:54:14 -08001058 unsigned long val;
1059 int err;
Jean Delvare08e7e272005-04-25 22:43:25 +02001060 unsigned int reg;
1061 u8 new_div;
1062
Frans Meulenbroeks179c4fd2012-01-04 20:58:52 +01001063 err = kstrtoul(buf, 10, &val);
Guenter Roeckbce26c52011-02-04 12:54:14 -08001064 if (err < 0)
1065 return err;
1066
Ingo Molnar9a61bf62006-01-18 23:19:26 +01001067 mutex_lock(&data->update_lock);
Guenter Roeck26bc4402011-02-11 08:00:58 -08001068 if (!data->has_fan_div) {
1069 /*
1070 * Only NCT6776F for now, so we know that this is a 13 bit
1071 * register
1072 */
Guenter Roeckec3e5a12011-02-02 08:46:49 -08001073 if (!val) {
1074 val = 0xff1f;
1075 } else {
1076 if (val > 1350000U)
1077 val = 135000U;
1078 val = 1350000U / val;
1079 val = (val & 0x1f) | ((val << 3) & 0xff00);
1080 }
1081 data->fan_min[nr] = val;
1082 goto done; /* Leave fan divider alone */
1083 }
Jean Delvare08e7e272005-04-25 22:43:25 +02001084 if (!val) {
1085 /* No min limit, alarm disabled */
1086 data->fan_min[nr] = 255;
1087 new_div = data->fan_div[nr]; /* No change */
1088 dev_info(dev, "fan%u low limit and alarm disabled\n", nr + 1);
1089 } else if ((reg = 1350000U / val) >= 128 * 255) {
Guenter Roeck8969e842012-01-19 11:02:27 -08001090 /*
1091 * Speed below this value cannot possibly be represented,
1092 * even with the highest divider (128)
1093 */
Jean Delvare08e7e272005-04-25 22:43:25 +02001094 data->fan_min[nr] = 254;
1095 new_div = 7; /* 128 == (1 << 7) */
Guenter Roeckbce26c52011-02-04 12:54:14 -08001096 dev_warn(dev, "fan%u low limit %lu below minimum %u, set to "
Guenter Roeckec3e5a12011-02-02 08:46:49 -08001097 "minimum\n", nr + 1, val,
Guenter Roeck26bc4402011-02-11 08:00:58 -08001098 data->fan_from_reg_min(254, 7));
Jean Delvare08e7e272005-04-25 22:43:25 +02001099 } else if (!reg) {
Guenter Roeck8969e842012-01-19 11:02:27 -08001100 /*
1101 * Speed above this value cannot possibly be represented,
1102 * even with the lowest divider (1)
1103 */
Jean Delvare08e7e272005-04-25 22:43:25 +02001104 data->fan_min[nr] = 1;
1105 new_div = 0; /* 1 == (1 << 0) */
Guenter Roeckbce26c52011-02-04 12:54:14 -08001106 dev_warn(dev, "fan%u low limit %lu above maximum %u, set to "
Guenter Roeckec3e5a12011-02-02 08:46:49 -08001107 "maximum\n", nr + 1, val,
Guenter Roeck26bc4402011-02-11 08:00:58 -08001108 data->fan_from_reg_min(1, 0));
Jean Delvare08e7e272005-04-25 22:43:25 +02001109 } else {
Guenter Roeck8969e842012-01-19 11:02:27 -08001110 /*
1111 * Automatically pick the best divider, i.e. the one such
1112 * that the min limit will correspond to a register value
1113 * in the 96..192 range
1114 */
Jean Delvare08e7e272005-04-25 22:43:25 +02001115 new_div = 0;
1116 while (reg > 192 && new_div < 7) {
1117 reg >>= 1;
1118 new_div++;
1119 }
1120 data->fan_min[nr] = reg;
1121 }
1122
Guenter Roeck8969e842012-01-19 11:02:27 -08001123 /*
1124 * Write both the fan clock divider (if it changed) and the new
1125 * fan min (unconditionally)
1126 */
Jean Delvare08e7e272005-04-25 22:43:25 +02001127 if (new_div != data->fan_div[nr]) {
Jean Delvare08e7e272005-04-25 22:43:25 +02001128 dev_dbg(dev, "fan%u clock divider changed from %u to %u\n",
1129 nr + 1, div_from_reg(data->fan_div[nr]),
1130 div_from_reg(new_div));
1131 data->fan_div[nr] = new_div;
Guenter Roeckec3e5a12011-02-02 08:46:49 -08001132 w83627ehf_write_fan_div_common(dev, data, nr);
Jean Delvare6b3e4642007-06-24 11:19:01 +02001133 /* Give the chip time to sample a new speed value */
1134 data->last_updated = jiffies;
Jean Delvare08e7e272005-04-25 22:43:25 +02001135 }
Guenter Roeckec3e5a12011-02-02 08:46:49 -08001136done:
Guenter Roeck279af1a2011-02-13 22:34:47 -08001137 w83627ehf_write_value(data, data->REG_FAN_MIN[nr],
Jean Delvare08e7e272005-04-25 22:43:25 +02001138 data->fan_min[nr]);
Ingo Molnar9a61bf62006-01-18 23:19:26 +01001139 mutex_unlock(&data->update_lock);
Jean Delvare08e7e272005-04-25 22:43:25 +02001140
1141 return count;
1142}
1143
Yuan Mu412fec82006-02-05 23:24:16 +01001144static struct sensor_device_attribute sda_fan_input[] = {
1145 SENSOR_ATTR(fan1_input, S_IRUGO, show_fan, NULL, 0),
1146 SENSOR_ATTR(fan2_input, S_IRUGO, show_fan, NULL, 1),
1147 SENSOR_ATTR(fan3_input, S_IRUGO, show_fan, NULL, 2),
1148 SENSOR_ATTR(fan4_input, S_IRUGO, show_fan, NULL, 3),
1149 SENSOR_ATTR(fan5_input, S_IRUGO, show_fan, NULL, 4),
1150};
Jean Delvare08e7e272005-04-25 22:43:25 +02001151
Jean Delvarea4589db2006-03-23 16:30:29 +01001152static struct sensor_device_attribute sda_fan_alarm[] = {
1153 SENSOR_ATTR(fan1_alarm, S_IRUGO, show_alarm, NULL, 6),
1154 SENSOR_ATTR(fan2_alarm, S_IRUGO, show_alarm, NULL, 7),
1155 SENSOR_ATTR(fan3_alarm, S_IRUGO, show_alarm, NULL, 11),
1156 SENSOR_ATTR(fan4_alarm, S_IRUGO, show_alarm, NULL, 10),
1157 SENSOR_ATTR(fan5_alarm, S_IRUGO, show_alarm, NULL, 23),
1158};
1159
Yuan Mu412fec82006-02-05 23:24:16 +01001160static struct sensor_device_attribute sda_fan_min[] = {
1161 SENSOR_ATTR(fan1_min, S_IWUSR | S_IRUGO, show_fan_min,
1162 store_fan_min, 0),
1163 SENSOR_ATTR(fan2_min, S_IWUSR | S_IRUGO, show_fan_min,
1164 store_fan_min, 1),
1165 SENSOR_ATTR(fan3_min, S_IWUSR | S_IRUGO, show_fan_min,
1166 store_fan_min, 2),
1167 SENSOR_ATTR(fan4_min, S_IWUSR | S_IRUGO, show_fan_min,
1168 store_fan_min, 3),
1169 SENSOR_ATTR(fan5_min, S_IWUSR | S_IRUGO, show_fan_min,
1170 store_fan_min, 4),
1171};
Jean Delvare08e7e272005-04-25 22:43:25 +02001172
Yuan Mu412fec82006-02-05 23:24:16 +01001173static struct sensor_device_attribute sda_fan_div[] = {
1174 SENSOR_ATTR(fan1_div, S_IRUGO, show_fan_div, NULL, 0),
1175 SENSOR_ATTR(fan2_div, S_IRUGO, show_fan_div, NULL, 1),
1176 SENSOR_ATTR(fan3_div, S_IRUGO, show_fan_div, NULL, 2),
1177 SENSOR_ATTR(fan4_div, S_IRUGO, show_fan_div, NULL, 3),
1178 SENSOR_ATTR(fan5_div, S_IRUGO, show_fan_div, NULL, 4),
1179};
Jean Delvare08e7e272005-04-25 22:43:25 +02001180
Guenter Roeckd36cf322011-02-07 15:08:54 -08001181static ssize_t
1182show_temp_label(struct device *dev, struct device_attribute *attr, char *buf)
1183{
1184 struct w83627ehf_data *data = w83627ehf_update_device(dev);
1185 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
1186 int nr = sensor_attr->index;
1187 return sprintf(buf, "%s\n", data->temp_label[data->temp_src[nr]]);
1188}
1189
Guenter Roeckec3e5a12011-02-02 08:46:49 -08001190#define show_temp_reg(addr, reg) \
Jean Delvare08e7e272005-04-25 22:43:25 +02001191static ssize_t \
Yuan Mu412fec82006-02-05 23:24:16 +01001192show_##reg(struct device *dev, struct device_attribute *attr, \
1193 char *buf) \
Jean Delvare08e7e272005-04-25 22:43:25 +02001194{ \
1195 struct w83627ehf_data *data = w83627ehf_update_device(dev); \
Guenter Roecke7e1ca62011-02-04 13:24:30 -08001196 struct sensor_device_attribute *sensor_attr = \
1197 to_sensor_dev_attr(attr); \
Yuan Mu412fec82006-02-05 23:24:16 +01001198 int nr = sensor_attr->index; \
Jean Delvarec5794cf2011-10-20 03:13:31 -04001199 return sprintf(buf, "%d\n", LM75_TEMP_FROM_REG(data->reg[nr])); \
Jean Delvare08e7e272005-04-25 22:43:25 +02001200}
Guenter Roeckec3e5a12011-02-02 08:46:49 -08001201show_temp_reg(reg_temp, temp);
1202show_temp_reg(reg_temp_over, temp_max);
1203show_temp_reg(reg_temp_hyst, temp_max_hyst);
Jean Delvare08e7e272005-04-25 22:43:25 +02001204
Guenter Roeckec3e5a12011-02-02 08:46:49 -08001205#define store_temp_reg(addr, reg) \
Jean Delvare08e7e272005-04-25 22:43:25 +02001206static ssize_t \
Yuan Mu412fec82006-02-05 23:24:16 +01001207store_##reg(struct device *dev, struct device_attribute *attr, \
1208 const char *buf, size_t count) \
Jean Delvare08e7e272005-04-25 22:43:25 +02001209{ \
David Hubbard1ea6dd32007-06-24 11:16:15 +02001210 struct w83627ehf_data *data = dev_get_drvdata(dev); \
Guenter Roecke7e1ca62011-02-04 13:24:30 -08001211 struct sensor_device_attribute *sensor_attr = \
1212 to_sensor_dev_attr(attr); \
Yuan Mu412fec82006-02-05 23:24:16 +01001213 int nr = sensor_attr->index; \
Guenter Roeckbce26c52011-02-04 12:54:14 -08001214 int err; \
1215 long val; \
Frans Meulenbroeks179c4fd2012-01-04 20:58:52 +01001216 err = kstrtol(buf, 10, &val); \
Guenter Roeckbce26c52011-02-04 12:54:14 -08001217 if (err < 0) \
1218 return err; \
Ingo Molnar9a61bf62006-01-18 23:19:26 +01001219 mutex_lock(&data->update_lock); \
Jean Delvarec5794cf2011-10-20 03:13:31 -04001220 data->reg[nr] = LM75_TEMP_TO_REG(val); \
1221 w83627ehf_write_temp(data, data->addr[nr], data->reg[nr]); \
Ingo Molnar9a61bf62006-01-18 23:19:26 +01001222 mutex_unlock(&data->update_lock); \
Jean Delvare08e7e272005-04-25 22:43:25 +02001223 return count; \
1224}
Guenter Roeckec3e5a12011-02-02 08:46:49 -08001225store_temp_reg(reg_temp_over, temp_max);
1226store_temp_reg(reg_temp_hyst, temp_max_hyst);
Jean Delvare08e7e272005-04-25 22:43:25 +02001227
Jean Delvareda667362007-06-24 11:21:02 +02001228static ssize_t
1229show_temp_type(struct device *dev, struct device_attribute *attr, char *buf)
1230{
1231 struct w83627ehf_data *data = w83627ehf_update_device(dev);
1232 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
1233 int nr = sensor_attr->index;
1234 return sprintf(buf, "%d\n", (int)data->temp_type[nr]);
1235}
1236
Gong Juna157d062009-03-30 21:46:43 +02001237static struct sensor_device_attribute sda_temp_input[] = {
Guenter Roeckbce26c52011-02-04 12:54:14 -08001238 SENSOR_ATTR(temp1_input, S_IRUGO, show_temp, NULL, 0),
1239 SENSOR_ATTR(temp2_input, S_IRUGO, show_temp, NULL, 1),
1240 SENSOR_ATTR(temp3_input, S_IRUGO, show_temp, NULL, 2),
Guenter Roeckd36cf322011-02-07 15:08:54 -08001241 SENSOR_ATTR(temp4_input, S_IRUGO, show_temp, NULL, 3),
Guenter Roeckec3e5a12011-02-02 08:46:49 -08001242 SENSOR_ATTR(temp5_input, S_IRUGO, show_temp, NULL, 4),
1243 SENSOR_ATTR(temp6_input, S_IRUGO, show_temp, NULL, 5),
1244 SENSOR_ATTR(temp7_input, S_IRUGO, show_temp, NULL, 6),
1245 SENSOR_ATTR(temp8_input, S_IRUGO, show_temp, NULL, 7),
1246 SENSOR_ATTR(temp9_input, S_IRUGO, show_temp, NULL, 8),
Guenter Roeckd36cf322011-02-07 15:08:54 -08001247};
1248
1249static struct sensor_device_attribute sda_temp_label[] = {
1250 SENSOR_ATTR(temp1_label, S_IRUGO, show_temp_label, NULL, 0),
1251 SENSOR_ATTR(temp2_label, S_IRUGO, show_temp_label, NULL, 1),
1252 SENSOR_ATTR(temp3_label, S_IRUGO, show_temp_label, NULL, 2),
1253 SENSOR_ATTR(temp4_label, S_IRUGO, show_temp_label, NULL, 3),
Guenter Roeckec3e5a12011-02-02 08:46:49 -08001254 SENSOR_ATTR(temp5_label, S_IRUGO, show_temp_label, NULL, 4),
1255 SENSOR_ATTR(temp6_label, S_IRUGO, show_temp_label, NULL, 5),
1256 SENSOR_ATTR(temp7_label, S_IRUGO, show_temp_label, NULL, 6),
1257 SENSOR_ATTR(temp8_label, S_IRUGO, show_temp_label, NULL, 7),
1258 SENSOR_ATTR(temp9_label, S_IRUGO, show_temp_label, NULL, 8),
Gong Juna157d062009-03-30 21:46:43 +02001259};
1260
1261static struct sensor_device_attribute sda_temp_max[] = {
Guenter Roeckbce26c52011-02-04 12:54:14 -08001262 SENSOR_ATTR(temp1_max, S_IRUGO | S_IWUSR, show_temp_max,
Yuan Mu412fec82006-02-05 23:24:16 +01001263 store_temp_max, 0),
Guenter Roeckbce26c52011-02-04 12:54:14 -08001264 SENSOR_ATTR(temp2_max, S_IRUGO | S_IWUSR, show_temp_max,
Yuan Mu412fec82006-02-05 23:24:16 +01001265 store_temp_max, 1),
Guenter Roeckbce26c52011-02-04 12:54:14 -08001266 SENSOR_ATTR(temp3_max, S_IRUGO | S_IWUSR, show_temp_max,
1267 store_temp_max, 2),
Guenter Roeckec3e5a12011-02-02 08:46:49 -08001268 SENSOR_ATTR(temp4_max, S_IRUGO | S_IWUSR, show_temp_max,
1269 store_temp_max, 3),
1270 SENSOR_ATTR(temp5_max, S_IRUGO | S_IWUSR, show_temp_max,
1271 store_temp_max, 4),
1272 SENSOR_ATTR(temp6_max, S_IRUGO | S_IWUSR, show_temp_max,
1273 store_temp_max, 5),
1274 SENSOR_ATTR(temp7_max, S_IRUGO | S_IWUSR, show_temp_max,
1275 store_temp_max, 6),
1276 SENSOR_ATTR(temp8_max, S_IRUGO | S_IWUSR, show_temp_max,
1277 store_temp_max, 7),
1278 SENSOR_ATTR(temp9_max, S_IRUGO | S_IWUSR, show_temp_max,
1279 store_temp_max, 8),
Gong Juna157d062009-03-30 21:46:43 +02001280};
1281
1282static struct sensor_device_attribute sda_temp_max_hyst[] = {
Guenter Roeckbce26c52011-02-04 12:54:14 -08001283 SENSOR_ATTR(temp1_max_hyst, S_IRUGO | S_IWUSR, show_temp_max_hyst,
Yuan Mu412fec82006-02-05 23:24:16 +01001284 store_temp_max_hyst, 0),
Guenter Roeckbce26c52011-02-04 12:54:14 -08001285 SENSOR_ATTR(temp2_max_hyst, S_IRUGO | S_IWUSR, show_temp_max_hyst,
Yuan Mu412fec82006-02-05 23:24:16 +01001286 store_temp_max_hyst, 1),
Guenter Roeckbce26c52011-02-04 12:54:14 -08001287 SENSOR_ATTR(temp3_max_hyst, S_IRUGO | S_IWUSR, show_temp_max_hyst,
1288 store_temp_max_hyst, 2),
Guenter Roeckec3e5a12011-02-02 08:46:49 -08001289 SENSOR_ATTR(temp4_max_hyst, S_IRUGO | S_IWUSR, show_temp_max_hyst,
1290 store_temp_max_hyst, 3),
1291 SENSOR_ATTR(temp5_max_hyst, S_IRUGO | S_IWUSR, show_temp_max_hyst,
1292 store_temp_max_hyst, 4),
1293 SENSOR_ATTR(temp6_max_hyst, S_IRUGO | S_IWUSR, show_temp_max_hyst,
1294 store_temp_max_hyst, 5),
1295 SENSOR_ATTR(temp7_max_hyst, S_IRUGO | S_IWUSR, show_temp_max_hyst,
1296 store_temp_max_hyst, 6),
1297 SENSOR_ATTR(temp8_max_hyst, S_IRUGO | S_IWUSR, show_temp_max_hyst,
1298 store_temp_max_hyst, 7),
1299 SENSOR_ATTR(temp9_max_hyst, S_IRUGO | S_IWUSR, show_temp_max_hyst,
1300 store_temp_max_hyst, 8),
Gong Juna157d062009-03-30 21:46:43 +02001301};
1302
1303static struct sensor_device_attribute sda_temp_alarm[] = {
Jean Delvarea4589db2006-03-23 16:30:29 +01001304 SENSOR_ATTR(temp1_alarm, S_IRUGO, show_alarm, NULL, 4),
1305 SENSOR_ATTR(temp2_alarm, S_IRUGO, show_alarm, NULL, 5),
1306 SENSOR_ATTR(temp3_alarm, S_IRUGO, show_alarm, NULL, 13),
Gong Juna157d062009-03-30 21:46:43 +02001307};
1308
1309static struct sensor_device_attribute sda_temp_type[] = {
Jean Delvareda667362007-06-24 11:21:02 +02001310 SENSOR_ATTR(temp1_type, S_IRUGO, show_temp_type, NULL, 0),
1311 SENSOR_ATTR(temp2_type, S_IRUGO, show_temp_type, NULL, 1),
1312 SENSOR_ATTR(temp3_type, S_IRUGO, show_temp_type, NULL, 2),
Yuan Mu412fec82006-02-05 23:24:16 +01001313};
Jean Delvare08e7e272005-04-25 22:43:25 +02001314
Rudolf Marek08c79952006-07-05 18:14:31 +02001315#define show_pwm_reg(reg) \
Guenter Roecke7e1ca62011-02-04 13:24:30 -08001316static ssize_t show_##reg(struct device *dev, struct device_attribute *attr, \
1317 char *buf) \
Rudolf Marek08c79952006-07-05 18:14:31 +02001318{ \
1319 struct w83627ehf_data *data = w83627ehf_update_device(dev); \
Guenter Roecke7e1ca62011-02-04 13:24:30 -08001320 struct sensor_device_attribute *sensor_attr = \
1321 to_sensor_dev_attr(attr); \
Rudolf Marek08c79952006-07-05 18:14:31 +02001322 int nr = sensor_attr->index; \
1323 return sprintf(buf, "%d\n", data->reg[nr]); \
1324}
1325
1326show_pwm_reg(pwm_mode)
1327show_pwm_reg(pwm_enable)
1328show_pwm_reg(pwm)
1329
1330static ssize_t
1331store_pwm_mode(struct device *dev, struct device_attribute *attr,
1332 const char *buf, size_t count)
1333{
David Hubbard1ea6dd32007-06-24 11:16:15 +02001334 struct w83627ehf_data *data = dev_get_drvdata(dev);
Rudolf Marek08c79952006-07-05 18:14:31 +02001335 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
Guenter Roeckad77c3e2012-01-27 17:56:06 -08001336 struct w83627ehf_sio_data *sio_data = dev->platform_data;
Rudolf Marek08c79952006-07-05 18:14:31 +02001337 int nr = sensor_attr->index;
Guenter Roeckbce26c52011-02-04 12:54:14 -08001338 unsigned long val;
1339 int err;
Rudolf Marek08c79952006-07-05 18:14:31 +02001340 u16 reg;
1341
Frans Meulenbroeks179c4fd2012-01-04 20:58:52 +01001342 err = kstrtoul(buf, 10, &val);
Guenter Roeckbce26c52011-02-04 12:54:14 -08001343 if (err < 0)
1344 return err;
1345
Rudolf Marek08c79952006-07-05 18:14:31 +02001346 if (val > 1)
1347 return -EINVAL;
Guenter Roeckad77c3e2012-01-27 17:56:06 -08001348
1349 /* On NCT67766F, DC mode is only supported for pwm1 */
1350 if (sio_data->kind == nct6776 && nr && val != 1)
1351 return -EINVAL;
1352
Rudolf Marek08c79952006-07-05 18:14:31 +02001353 mutex_lock(&data->update_lock);
David Hubbard1ea6dd32007-06-24 11:16:15 +02001354 reg = w83627ehf_read_value(data, W83627EHF_REG_PWM_ENABLE[nr]);
Rudolf Marek08c79952006-07-05 18:14:31 +02001355 data->pwm_mode[nr] = val;
1356 reg &= ~(1 << W83627EHF_PWM_MODE_SHIFT[nr]);
1357 if (!val)
1358 reg |= 1 << W83627EHF_PWM_MODE_SHIFT[nr];
David Hubbard1ea6dd32007-06-24 11:16:15 +02001359 w83627ehf_write_value(data, W83627EHF_REG_PWM_ENABLE[nr], reg);
Rudolf Marek08c79952006-07-05 18:14:31 +02001360 mutex_unlock(&data->update_lock);
1361 return count;
1362}
1363
1364static ssize_t
1365store_pwm(struct device *dev, struct device_attribute *attr,
1366 const char *buf, size_t count)
1367{
David Hubbard1ea6dd32007-06-24 11:16:15 +02001368 struct w83627ehf_data *data = dev_get_drvdata(dev);
Rudolf Marek08c79952006-07-05 18:14:31 +02001369 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
1370 int nr = sensor_attr->index;
Guenter Roeckbce26c52011-02-04 12:54:14 -08001371 unsigned long val;
1372 int err;
1373
Frans Meulenbroeks179c4fd2012-01-04 20:58:52 +01001374 err = kstrtoul(buf, 10, &val);
Guenter Roeckbce26c52011-02-04 12:54:14 -08001375 if (err < 0)
1376 return err;
1377
1378 val = SENSORS_LIMIT(val, 0, 255);
Rudolf Marek08c79952006-07-05 18:14:31 +02001379
1380 mutex_lock(&data->update_lock);
1381 data->pwm[nr] = val;
Guenter Roeck279af1a2011-02-13 22:34:47 -08001382 w83627ehf_write_value(data, data->REG_PWM[nr], val);
Rudolf Marek08c79952006-07-05 18:14:31 +02001383 mutex_unlock(&data->update_lock);
1384 return count;
1385}
1386
1387static ssize_t
1388store_pwm_enable(struct device *dev, struct device_attribute *attr,
1389 const char *buf, size_t count)
1390{
David Hubbard1ea6dd32007-06-24 11:16:15 +02001391 struct w83627ehf_data *data = dev_get_drvdata(dev);
Guenter Roeckec3e5a12011-02-02 08:46:49 -08001392 struct w83627ehf_sio_data *sio_data = dev->platform_data;
Rudolf Marek08c79952006-07-05 18:14:31 +02001393 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
1394 int nr = sensor_attr->index;
Guenter Roeckbce26c52011-02-04 12:54:14 -08001395 unsigned long val;
1396 int err;
Rudolf Marek08c79952006-07-05 18:14:31 +02001397 u16 reg;
1398
Frans Meulenbroeks179c4fd2012-01-04 20:58:52 +01001399 err = kstrtoul(buf, 10, &val);
Guenter Roeckbce26c52011-02-04 12:54:14 -08001400 if (err < 0)
1401 return err;
1402
Guenter Roeckb84bb512011-02-13 23:01:25 -08001403 if (!val || (val > 4 && val != data->pwm_enable_orig[nr]))
Rudolf Marek08c79952006-07-05 18:14:31 +02001404 return -EINVAL;
Guenter Roeckec3e5a12011-02-02 08:46:49 -08001405 /* SmartFan III mode is not supported on NCT6776F */
1406 if (sio_data->kind == nct6776 && val == 4)
1407 return -EINVAL;
1408
Rudolf Marek08c79952006-07-05 18:14:31 +02001409 mutex_lock(&data->update_lock);
Rudolf Marek08c79952006-07-05 18:14:31 +02001410 data->pwm_enable[nr] = val;
Guenter Roeckec3e5a12011-02-02 08:46:49 -08001411 if (sio_data->kind == nct6775 || sio_data->kind == nct6776) {
1412 reg = w83627ehf_read_value(data,
1413 NCT6775_REG_FAN_MODE[nr]);
1414 reg &= 0x0f;
1415 reg |= (val - 1) << 4;
1416 w83627ehf_write_value(data,
1417 NCT6775_REG_FAN_MODE[nr], reg);
1418 } else {
1419 reg = w83627ehf_read_value(data, W83627EHF_REG_PWM_ENABLE[nr]);
1420 reg &= ~(0x03 << W83627EHF_PWM_ENABLE_SHIFT[nr]);
1421 reg |= (val - 1) << W83627EHF_PWM_ENABLE_SHIFT[nr];
1422 w83627ehf_write_value(data, W83627EHF_REG_PWM_ENABLE[nr], reg);
1423 }
Rudolf Marek08c79952006-07-05 18:14:31 +02001424 mutex_unlock(&data->update_lock);
1425 return count;
1426}
1427
1428
1429#define show_tol_temp(reg) \
1430static ssize_t show_##reg(struct device *dev, struct device_attribute *attr, \
1431 char *buf) \
1432{ \
1433 struct w83627ehf_data *data = w83627ehf_update_device(dev); \
Guenter Roecke7e1ca62011-02-04 13:24:30 -08001434 struct sensor_device_attribute *sensor_attr = \
1435 to_sensor_dev_attr(attr); \
Rudolf Marek08c79952006-07-05 18:14:31 +02001436 int nr = sensor_attr->index; \
Guenter Roeckbce26c52011-02-04 12:54:14 -08001437 return sprintf(buf, "%d\n", data->reg[nr] * 1000); \
Rudolf Marek08c79952006-07-05 18:14:31 +02001438}
1439
1440show_tol_temp(tolerance)
1441show_tol_temp(target_temp)
1442
1443static ssize_t
1444store_target_temp(struct device *dev, struct device_attribute *attr,
1445 const char *buf, size_t count)
1446{
David Hubbard1ea6dd32007-06-24 11:16:15 +02001447 struct w83627ehf_data *data = dev_get_drvdata(dev);
Rudolf Marek08c79952006-07-05 18:14:31 +02001448 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
1449 int nr = sensor_attr->index;
Guenter Roeckbce26c52011-02-04 12:54:14 -08001450 long val;
1451 int err;
1452
Frans Meulenbroeks179c4fd2012-01-04 20:58:52 +01001453 err = kstrtol(buf, 10, &val);
Guenter Roeckbce26c52011-02-04 12:54:14 -08001454 if (err < 0)
1455 return err;
1456
1457 val = SENSORS_LIMIT(DIV_ROUND_CLOSEST(val, 1000), 0, 127);
Rudolf Marek08c79952006-07-05 18:14:31 +02001458
1459 mutex_lock(&data->update_lock);
1460 data->target_temp[nr] = val;
Guenter Roeck279af1a2011-02-13 22:34:47 -08001461 w83627ehf_write_value(data, data->REG_TARGET[nr], val);
Rudolf Marek08c79952006-07-05 18:14:31 +02001462 mutex_unlock(&data->update_lock);
1463 return count;
1464}
1465
1466static ssize_t
1467store_tolerance(struct device *dev, struct device_attribute *attr,
1468 const char *buf, size_t count)
1469{
David Hubbard1ea6dd32007-06-24 11:16:15 +02001470 struct w83627ehf_data *data = dev_get_drvdata(dev);
Guenter Roeckec3e5a12011-02-02 08:46:49 -08001471 struct w83627ehf_sio_data *sio_data = dev->platform_data;
Rudolf Marek08c79952006-07-05 18:14:31 +02001472 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
1473 int nr = sensor_attr->index;
1474 u16 reg;
Guenter Roeckbce26c52011-02-04 12:54:14 -08001475 long val;
1476 int err;
1477
Frans Meulenbroeks179c4fd2012-01-04 20:58:52 +01001478 err = kstrtol(buf, 10, &val);
Guenter Roeckbce26c52011-02-04 12:54:14 -08001479 if (err < 0)
1480 return err;
1481
Rudolf Marek08c79952006-07-05 18:14:31 +02001482 /* Limit the temp to 0C - 15C */
Guenter Roeckbce26c52011-02-04 12:54:14 -08001483 val = SENSORS_LIMIT(DIV_ROUND_CLOSEST(val, 1000), 0, 15);
Rudolf Marek08c79952006-07-05 18:14:31 +02001484
1485 mutex_lock(&data->update_lock);
Guenter Roeckec3e5a12011-02-02 08:46:49 -08001486 if (sio_data->kind == nct6775 || sio_data->kind == nct6776) {
1487 /* Limit tolerance further for NCT6776F */
1488 if (sio_data->kind == nct6776 && val > 7)
1489 val = 7;
1490 reg = w83627ehf_read_value(data, NCT6775_REG_FAN_MODE[nr]);
Rudolf Marek08c79952006-07-05 18:14:31 +02001491 reg = (reg & 0xf0) | val;
Guenter Roeckec3e5a12011-02-02 08:46:49 -08001492 w83627ehf_write_value(data, NCT6775_REG_FAN_MODE[nr], reg);
1493 } else {
1494 reg = w83627ehf_read_value(data, W83627EHF_REG_TOLERANCE[nr]);
1495 if (nr == 1)
1496 reg = (reg & 0x0f) | (val << 4);
1497 else
1498 reg = (reg & 0xf0) | val;
1499 w83627ehf_write_value(data, W83627EHF_REG_TOLERANCE[nr], reg);
1500 }
1501 data->tolerance[nr] = val;
Rudolf Marek08c79952006-07-05 18:14:31 +02001502 mutex_unlock(&data->update_lock);
1503 return count;
1504}
1505
1506static struct sensor_device_attribute sda_pwm[] = {
1507 SENSOR_ATTR(pwm1, S_IWUSR | S_IRUGO, show_pwm, store_pwm, 0),
1508 SENSOR_ATTR(pwm2, S_IWUSR | S_IRUGO, show_pwm, store_pwm, 1),
1509 SENSOR_ATTR(pwm3, S_IWUSR | S_IRUGO, show_pwm, store_pwm, 2),
1510 SENSOR_ATTR(pwm4, S_IWUSR | S_IRUGO, show_pwm, store_pwm, 3),
1511};
1512
1513static struct sensor_device_attribute sda_pwm_mode[] = {
1514 SENSOR_ATTR(pwm1_mode, S_IWUSR | S_IRUGO, show_pwm_mode,
1515 store_pwm_mode, 0),
1516 SENSOR_ATTR(pwm2_mode, S_IWUSR | S_IRUGO, show_pwm_mode,
1517 store_pwm_mode, 1),
1518 SENSOR_ATTR(pwm3_mode, S_IWUSR | S_IRUGO, show_pwm_mode,
1519 store_pwm_mode, 2),
1520 SENSOR_ATTR(pwm4_mode, S_IWUSR | S_IRUGO, show_pwm_mode,
1521 store_pwm_mode, 3),
1522};
1523
1524static struct sensor_device_attribute sda_pwm_enable[] = {
1525 SENSOR_ATTR(pwm1_enable, S_IWUSR | S_IRUGO, show_pwm_enable,
1526 store_pwm_enable, 0),
1527 SENSOR_ATTR(pwm2_enable, S_IWUSR | S_IRUGO, show_pwm_enable,
1528 store_pwm_enable, 1),
1529 SENSOR_ATTR(pwm3_enable, S_IWUSR | S_IRUGO, show_pwm_enable,
1530 store_pwm_enable, 2),
1531 SENSOR_ATTR(pwm4_enable, S_IWUSR | S_IRUGO, show_pwm_enable,
1532 store_pwm_enable, 3),
1533};
1534
1535static struct sensor_device_attribute sda_target_temp[] = {
1536 SENSOR_ATTR(pwm1_target, S_IWUSR | S_IRUGO, show_target_temp,
1537 store_target_temp, 0),
1538 SENSOR_ATTR(pwm2_target, S_IWUSR | S_IRUGO, show_target_temp,
1539 store_target_temp, 1),
1540 SENSOR_ATTR(pwm3_target, S_IWUSR | S_IRUGO, show_target_temp,
1541 store_target_temp, 2),
1542 SENSOR_ATTR(pwm4_target, S_IWUSR | S_IRUGO, show_target_temp,
1543 store_target_temp, 3),
1544};
1545
1546static struct sensor_device_attribute sda_tolerance[] = {
1547 SENSOR_ATTR(pwm1_tolerance, S_IWUSR | S_IRUGO, show_tolerance,
1548 store_tolerance, 0),
1549 SENSOR_ATTR(pwm2_tolerance, S_IWUSR | S_IRUGO, show_tolerance,
1550 store_tolerance, 1),
1551 SENSOR_ATTR(pwm3_tolerance, S_IWUSR | S_IRUGO, show_tolerance,
1552 store_tolerance, 2),
1553 SENSOR_ATTR(pwm4_tolerance, S_IWUSR | S_IRUGO, show_tolerance,
1554 store_tolerance, 3),
1555};
1556
Rudolf Marek08c79952006-07-05 18:14:31 +02001557/* Smart Fan registers */
1558
1559#define fan_functions(reg, REG) \
1560static ssize_t show_##reg(struct device *dev, struct device_attribute *attr, \
1561 char *buf) \
1562{ \
1563 struct w83627ehf_data *data = w83627ehf_update_device(dev); \
Guenter Roecke7e1ca62011-02-04 13:24:30 -08001564 struct sensor_device_attribute *sensor_attr = \
1565 to_sensor_dev_attr(attr); \
Rudolf Marek08c79952006-07-05 18:14:31 +02001566 int nr = sensor_attr->index; \
1567 return sprintf(buf, "%d\n", data->reg[nr]); \
Guenter Roecke7e1ca62011-02-04 13:24:30 -08001568} \
Rudolf Marek08c79952006-07-05 18:14:31 +02001569static ssize_t \
1570store_##reg(struct device *dev, struct device_attribute *attr, \
1571 const char *buf, size_t count) \
Guenter Roecke7e1ca62011-02-04 13:24:30 -08001572{ \
David Hubbard1ea6dd32007-06-24 11:16:15 +02001573 struct w83627ehf_data *data = dev_get_drvdata(dev); \
Guenter Roecke7e1ca62011-02-04 13:24:30 -08001574 struct sensor_device_attribute *sensor_attr = \
1575 to_sensor_dev_attr(attr); \
Rudolf Marek08c79952006-07-05 18:14:31 +02001576 int nr = sensor_attr->index; \
Guenter Roeckbce26c52011-02-04 12:54:14 -08001577 unsigned long val; \
1578 int err; \
Frans Meulenbroeks179c4fd2012-01-04 20:58:52 +01001579 err = kstrtoul(buf, 10, &val); \
Guenter Roeckbce26c52011-02-04 12:54:14 -08001580 if (err < 0) \
1581 return err; \
1582 val = SENSORS_LIMIT(val, 1, 255); \
Rudolf Marek08c79952006-07-05 18:14:31 +02001583 mutex_lock(&data->update_lock); \
1584 data->reg[nr] = val; \
Guenter Roeckda2e0252010-08-14 21:08:55 +02001585 w83627ehf_write_value(data, data->REG_##REG[nr], val); \
Rudolf Marek08c79952006-07-05 18:14:31 +02001586 mutex_unlock(&data->update_lock); \
1587 return count; \
1588}
1589
Daniel J Blueman41e9a062009-12-14 18:01:37 -08001590fan_functions(fan_start_output, FAN_START_OUTPUT)
1591fan_functions(fan_stop_output, FAN_STOP_OUTPUT)
1592fan_functions(fan_max_output, FAN_MAX_OUTPUT)
1593fan_functions(fan_step_output, FAN_STEP_OUTPUT)
Rudolf Marek08c79952006-07-05 18:14:31 +02001594
1595#define fan_time_functions(reg, REG) \
1596static ssize_t show_##reg(struct device *dev, struct device_attribute *attr, \
1597 char *buf) \
1598{ \
1599 struct w83627ehf_data *data = w83627ehf_update_device(dev); \
Guenter Roecke7e1ca62011-02-04 13:24:30 -08001600 struct sensor_device_attribute *sensor_attr = \
1601 to_sensor_dev_attr(attr); \
Rudolf Marek08c79952006-07-05 18:14:31 +02001602 int nr = sensor_attr->index; \
1603 return sprintf(buf, "%d\n", \
Guenter Roecke7e1ca62011-02-04 13:24:30 -08001604 step_time_from_reg(data->reg[nr], \
1605 data->pwm_mode[nr])); \
Rudolf Marek08c79952006-07-05 18:14:31 +02001606} \
1607\
1608static ssize_t \
1609store_##reg(struct device *dev, struct device_attribute *attr, \
1610 const char *buf, size_t count) \
1611{ \
David Hubbard1ea6dd32007-06-24 11:16:15 +02001612 struct w83627ehf_data *data = dev_get_drvdata(dev); \
Guenter Roecke7e1ca62011-02-04 13:24:30 -08001613 struct sensor_device_attribute *sensor_attr = \
1614 to_sensor_dev_attr(attr); \
Rudolf Marek08c79952006-07-05 18:14:31 +02001615 int nr = sensor_attr->index; \
Guenter Roeckbce26c52011-02-04 12:54:14 -08001616 unsigned long val; \
1617 int err; \
Frans Meulenbroeks179c4fd2012-01-04 20:58:52 +01001618 err = kstrtoul(buf, 10, &val); \
Guenter Roeckbce26c52011-02-04 12:54:14 -08001619 if (err < 0) \
1620 return err; \
1621 val = step_time_to_reg(val, data->pwm_mode[nr]); \
Rudolf Marek08c79952006-07-05 18:14:31 +02001622 mutex_lock(&data->update_lock); \
1623 data->reg[nr] = val; \
Guenter Roeck33fa9b62012-03-12 08:21:16 -07001624 w83627ehf_write_value(data, data->REG_##REG[nr], val); \
Rudolf Marek08c79952006-07-05 18:14:31 +02001625 mutex_unlock(&data->update_lock); \
1626 return count; \
1627} \
1628
1629fan_time_functions(fan_stop_time, FAN_STOP_TIME)
1630
David Hubbard1ea6dd32007-06-24 11:16:15 +02001631static ssize_t show_name(struct device *dev, struct device_attribute *attr,
1632 char *buf)
1633{
1634 struct w83627ehf_data *data = dev_get_drvdata(dev);
1635
1636 return sprintf(buf, "%s\n", data->name);
1637}
1638static DEVICE_ATTR(name, S_IRUGO, show_name, NULL);
Rudolf Marek08c79952006-07-05 18:14:31 +02001639
1640static struct sensor_device_attribute sda_sf3_arrays_fan4[] = {
1641 SENSOR_ATTR(pwm4_stop_time, S_IWUSR | S_IRUGO, show_fan_stop_time,
1642 store_fan_stop_time, 3),
Daniel J Blueman41e9a062009-12-14 18:01:37 -08001643 SENSOR_ATTR(pwm4_start_output, S_IWUSR | S_IRUGO, show_fan_start_output,
1644 store_fan_start_output, 3),
1645 SENSOR_ATTR(pwm4_stop_output, S_IWUSR | S_IRUGO, show_fan_stop_output,
1646 store_fan_stop_output, 3),
1647 SENSOR_ATTR(pwm4_max_output, S_IWUSR | S_IRUGO, show_fan_max_output,
1648 store_fan_max_output, 3),
1649 SENSOR_ATTR(pwm4_step_output, S_IWUSR | S_IRUGO, show_fan_step_output,
1650 store_fan_step_output, 3),
Rudolf Marek08c79952006-07-05 18:14:31 +02001651};
1652
Jean Delvareeff76872011-11-04 12:00:48 +01001653static struct sensor_device_attribute sda_sf3_arrays_fan3[] = {
1654 SENSOR_ATTR(pwm3_stop_time, S_IWUSR | S_IRUGO, show_fan_stop_time,
1655 store_fan_stop_time, 2),
1656 SENSOR_ATTR(pwm3_start_output, S_IWUSR | S_IRUGO, show_fan_start_output,
1657 store_fan_start_output, 2),
1658 SENSOR_ATTR(pwm3_stop_output, S_IWUSR | S_IRUGO, show_fan_stop_output,
1659 store_fan_stop_output, 2),
1660};
1661
Rudolf Marek08c79952006-07-05 18:14:31 +02001662static struct sensor_device_attribute sda_sf3_arrays[] = {
1663 SENSOR_ATTR(pwm1_stop_time, S_IWUSR | S_IRUGO, show_fan_stop_time,
1664 store_fan_stop_time, 0),
1665 SENSOR_ATTR(pwm2_stop_time, S_IWUSR | S_IRUGO, show_fan_stop_time,
1666 store_fan_stop_time, 1),
Daniel J Blueman41e9a062009-12-14 18:01:37 -08001667 SENSOR_ATTR(pwm1_start_output, S_IWUSR | S_IRUGO, show_fan_start_output,
1668 store_fan_start_output, 0),
1669 SENSOR_ATTR(pwm2_start_output, S_IWUSR | S_IRUGO, show_fan_start_output,
1670 store_fan_start_output, 1),
Daniel J Blueman41e9a062009-12-14 18:01:37 -08001671 SENSOR_ATTR(pwm1_stop_output, S_IWUSR | S_IRUGO, show_fan_stop_output,
1672 store_fan_stop_output, 0),
1673 SENSOR_ATTR(pwm2_stop_output, S_IWUSR | S_IRUGO, show_fan_stop_output,
1674 store_fan_stop_output, 1),
Guenter Roeckda2e0252010-08-14 21:08:55 +02001675};
Daniel J Blueman41e9a062009-12-14 18:01:37 -08001676
Guenter Roeckda2e0252010-08-14 21:08:55 +02001677
1678/*
1679 * pwm1 and pwm3 don't support max and step settings on all chips.
1680 * Need to check support while generating/removing attribute files.
1681 */
1682static struct sensor_device_attribute sda_sf3_max_step_arrays[] = {
1683 SENSOR_ATTR(pwm1_max_output, S_IWUSR | S_IRUGO, show_fan_max_output,
1684 store_fan_max_output, 0),
1685 SENSOR_ATTR(pwm1_step_output, S_IWUSR | S_IRUGO, show_fan_step_output,
1686 store_fan_step_output, 0),
Daniel J Blueman41e9a062009-12-14 18:01:37 -08001687 SENSOR_ATTR(pwm2_max_output, S_IWUSR | S_IRUGO, show_fan_max_output,
1688 store_fan_max_output, 1),
1689 SENSOR_ATTR(pwm2_step_output, S_IWUSR | S_IRUGO, show_fan_step_output,
1690 store_fan_step_output, 1),
Guenter Roeckda2e0252010-08-14 21:08:55 +02001691 SENSOR_ATTR(pwm3_max_output, S_IWUSR | S_IRUGO, show_fan_max_output,
1692 store_fan_max_output, 2),
1693 SENSOR_ATTR(pwm3_step_output, S_IWUSR | S_IRUGO, show_fan_step_output,
1694 store_fan_step_output, 2),
Rudolf Marek08c79952006-07-05 18:14:31 +02001695};
1696
Jean Delvarefc18d6c2007-06-24 11:19:42 +02001697static ssize_t
1698show_vid(struct device *dev, struct device_attribute *attr, char *buf)
1699{
1700 struct w83627ehf_data *data = dev_get_drvdata(dev);
1701 return sprintf(buf, "%d\n", vid_from_reg(data->vid, data->vrm));
1702}
1703static DEVICE_ATTR(cpu0_vid, S_IRUGO, show_vid, NULL);
1704
Dmitry Artamonow363a12a2011-08-12 16:41:11 -04001705
1706/* Case open detection */
1707
1708static ssize_t
1709show_caseopen(struct device *dev, struct device_attribute *attr, char *buf)
1710{
1711 struct w83627ehf_data *data = w83627ehf_update_device(dev);
1712
1713 return sprintf(buf, "%d\n",
1714 !!(data->caseopen & to_sensor_dev_attr_2(attr)->index));
1715}
1716
1717static ssize_t
1718clear_caseopen(struct device *dev, struct device_attribute *attr,
1719 const char *buf, size_t count)
1720{
1721 struct w83627ehf_data *data = dev_get_drvdata(dev);
1722 unsigned long val;
1723 u16 reg, mask;
1724
Frans Meulenbroeks179c4fd2012-01-04 20:58:52 +01001725 if (kstrtoul(buf, 10, &val) || val != 0)
Dmitry Artamonow363a12a2011-08-12 16:41:11 -04001726 return -EINVAL;
1727
1728 mask = to_sensor_dev_attr_2(attr)->nr;
1729
1730 mutex_lock(&data->update_lock);
1731 reg = w83627ehf_read_value(data, W83627EHF_REG_CASEOPEN_CLR);
1732 w83627ehf_write_value(data, W83627EHF_REG_CASEOPEN_CLR, reg | mask);
1733 w83627ehf_write_value(data, W83627EHF_REG_CASEOPEN_CLR, reg & ~mask);
1734 data->valid = 0; /* Force cache refresh */
1735 mutex_unlock(&data->update_lock);
1736
1737 return count;
1738}
1739
1740static struct sensor_device_attribute_2 sda_caseopen[] = {
1741 SENSOR_ATTR_2(intrusion0_alarm, S_IWUSR | S_IRUGO, show_caseopen,
1742 clear_caseopen, 0x80, 0x10),
1743 SENSOR_ATTR_2(intrusion1_alarm, S_IWUSR | S_IRUGO, show_caseopen,
1744 clear_caseopen, 0x40, 0x40),
1745};
1746
Jean Delvare08e7e272005-04-25 22:43:25 +02001747/*
David Hubbard1ea6dd32007-06-24 11:16:15 +02001748 * Driver and device management
Jean Delvare08e7e272005-04-25 22:43:25 +02001749 */
1750
David Hubbardc18beb52006-09-24 21:04:38 +02001751static void w83627ehf_device_remove_files(struct device *dev)
1752{
Guenter Roeck8969e842012-01-19 11:02:27 -08001753 /*
1754 * some entries in the following arrays may not have been used in
1755 * device_create_file(), but device_remove_file() will ignore them
1756 */
David Hubbardc18beb52006-09-24 21:04:38 +02001757 int i;
David Hubbard1ea6dd32007-06-24 11:16:15 +02001758 struct w83627ehf_data *data = dev_get_drvdata(dev);
David Hubbardc18beb52006-09-24 21:04:38 +02001759
1760 for (i = 0; i < ARRAY_SIZE(sda_sf3_arrays); i++)
1761 device_remove_file(dev, &sda_sf3_arrays[i].dev_attr);
Guenter Roeckda2e0252010-08-14 21:08:55 +02001762 for (i = 0; i < ARRAY_SIZE(sda_sf3_max_step_arrays); i++) {
1763 struct sensor_device_attribute *attr =
1764 &sda_sf3_max_step_arrays[i];
Guenter Roeckec3e5a12011-02-02 08:46:49 -08001765 if (data->REG_FAN_STEP_OUTPUT &&
1766 data->REG_FAN_STEP_OUTPUT[attr->index] != 0xff)
Guenter Roeckda2e0252010-08-14 21:08:55 +02001767 device_remove_file(dev, &attr->dev_attr);
1768 }
Jean Delvareeff76872011-11-04 12:00:48 +01001769 for (i = 0; i < ARRAY_SIZE(sda_sf3_arrays_fan3); i++)
1770 device_remove_file(dev, &sda_sf3_arrays_fan3[i].dev_attr);
David Hubbardc18beb52006-09-24 21:04:38 +02001771 for (i = 0; i < ARRAY_SIZE(sda_sf3_arrays_fan4); i++)
1772 device_remove_file(dev, &sda_sf3_arrays_fan4[i].dev_attr);
David Hubbard1ea6dd32007-06-24 11:16:15 +02001773 for (i = 0; i < data->in_num; i++) {
Gong Juna157d062009-03-30 21:46:43 +02001774 if ((i == 6) && data->in6_skip)
1775 continue;
David Hubbardc18beb52006-09-24 21:04:38 +02001776 device_remove_file(dev, &sda_in_input[i].dev_attr);
1777 device_remove_file(dev, &sda_in_alarm[i].dev_attr);
1778 device_remove_file(dev, &sda_in_min[i].dev_attr);
1779 device_remove_file(dev, &sda_in_max[i].dev_attr);
1780 }
1781 for (i = 0; i < 5; i++) {
1782 device_remove_file(dev, &sda_fan_input[i].dev_attr);
1783 device_remove_file(dev, &sda_fan_alarm[i].dev_attr);
1784 device_remove_file(dev, &sda_fan_div[i].dev_attr);
1785 device_remove_file(dev, &sda_fan_min[i].dev_attr);
1786 }
Gong Jun237c8d22009-03-30 21:46:42 +02001787 for (i = 0; i < data->pwm_num; i++) {
David Hubbardc18beb52006-09-24 21:04:38 +02001788 device_remove_file(dev, &sda_pwm[i].dev_attr);
1789 device_remove_file(dev, &sda_pwm_mode[i].dev_attr);
1790 device_remove_file(dev, &sda_pwm_enable[i].dev_attr);
1791 device_remove_file(dev, &sda_target_temp[i].dev_attr);
1792 device_remove_file(dev, &sda_tolerance[i].dev_attr);
1793 }
Guenter Roeckd36cf322011-02-07 15:08:54 -08001794 for (i = 0; i < NUM_REG_TEMP; i++) {
1795 if (!(data->have_temp & (1 << i)))
Gong Juna157d062009-03-30 21:46:43 +02001796 continue;
1797 device_remove_file(dev, &sda_temp_input[i].dev_attr);
Guenter Roeckd36cf322011-02-07 15:08:54 -08001798 device_remove_file(dev, &sda_temp_label[i].dev_attr);
Jean Delvareeff76872011-11-04 12:00:48 +01001799 if (i == 2 && data->temp3_val_only)
1800 continue;
Gong Juna157d062009-03-30 21:46:43 +02001801 device_remove_file(dev, &sda_temp_max[i].dev_attr);
1802 device_remove_file(dev, &sda_temp_max_hyst[i].dev_attr);
Guenter Roeckec3e5a12011-02-02 08:46:49 -08001803 if (i > 2)
1804 continue;
Gong Juna157d062009-03-30 21:46:43 +02001805 device_remove_file(dev, &sda_temp_alarm[i].dev_attr);
1806 device_remove_file(dev, &sda_temp_type[i].dev_attr);
1807 }
David Hubbard1ea6dd32007-06-24 11:16:15 +02001808
Dmitry Artamonow363a12a2011-08-12 16:41:11 -04001809 device_remove_file(dev, &sda_caseopen[0].dev_attr);
1810 device_remove_file(dev, &sda_caseopen[1].dev_attr);
1811
David Hubbard1ea6dd32007-06-24 11:16:15 +02001812 device_remove_file(dev, &dev_attr_name);
Jean Delvarecbe311f2008-01-03 21:22:44 +01001813 device_remove_file(dev, &dev_attr_cpu0_vid);
David Hubbardc18beb52006-09-24 21:04:38 +02001814}
1815
David Hubbard1ea6dd32007-06-24 11:16:15 +02001816/* Get the monitoring functions started */
Jean Delvarebf164c52011-10-13 15:49:08 -04001817static inline void __devinit w83627ehf_init_device(struct w83627ehf_data *data,
1818 enum kinds kind)
Jean Delvare08e7e272005-04-25 22:43:25 +02001819{
1820 int i;
Jean Delvareda667362007-06-24 11:21:02 +02001821 u8 tmp, diode;
Jean Delvare08e7e272005-04-25 22:43:25 +02001822
1823 /* Start monitoring is needed */
David Hubbard1ea6dd32007-06-24 11:16:15 +02001824 tmp = w83627ehf_read_value(data, W83627EHF_REG_CONFIG);
Jean Delvare08e7e272005-04-25 22:43:25 +02001825 if (!(tmp & 0x01))
David Hubbard1ea6dd32007-06-24 11:16:15 +02001826 w83627ehf_write_value(data, W83627EHF_REG_CONFIG,
Jean Delvare08e7e272005-04-25 22:43:25 +02001827 tmp | 0x01);
1828
Guenter Roeckd36cf322011-02-07 15:08:54 -08001829 /* Enable temperature sensors if needed */
1830 for (i = 0; i < NUM_REG_TEMP; i++) {
1831 if (!(data->have_temp & (1 << i)))
1832 continue;
Guenter Roeckec3e5a12011-02-02 08:46:49 -08001833 if (!data->reg_temp_config[i])
Guenter Roeckd36cf322011-02-07 15:08:54 -08001834 continue;
David Hubbard1ea6dd32007-06-24 11:16:15 +02001835 tmp = w83627ehf_read_value(data,
Guenter Roeckec3e5a12011-02-02 08:46:49 -08001836 data->reg_temp_config[i]);
Jean Delvare08e7e272005-04-25 22:43:25 +02001837 if (tmp & 0x01)
David Hubbard1ea6dd32007-06-24 11:16:15 +02001838 w83627ehf_write_value(data,
Guenter Roeckec3e5a12011-02-02 08:46:49 -08001839 data->reg_temp_config[i],
Jean Delvare08e7e272005-04-25 22:43:25 +02001840 tmp & 0xfe);
1841 }
Jean Delvared3130f02007-06-24 11:20:13 +02001842
1843 /* Enable VBAT monitoring if needed */
1844 tmp = w83627ehf_read_value(data, W83627EHF_REG_VBAT);
1845 if (!(tmp & 0x01))
1846 w83627ehf_write_value(data, W83627EHF_REG_VBAT, tmp | 0x01);
Jean Delvareda667362007-06-24 11:21:02 +02001847
1848 /* Get thermal sensor types */
Jean Delvarebf164c52011-10-13 15:49:08 -04001849 switch (kind) {
1850 case w83627ehf:
1851 diode = w83627ehf_read_value(data, W83627EHF_REG_DIODE);
1852 break;
Jean Delvareeff76872011-11-04 12:00:48 +01001853 case w83627uhg:
1854 diode = 0x00;
1855 break;
Jean Delvarebf164c52011-10-13 15:49:08 -04001856 default:
1857 diode = 0x70;
1858 }
Jean Delvareda667362007-06-24 11:21:02 +02001859 for (i = 0; i < 3; i++) {
Guenter Roeckbfa02b02011-11-06 20:25:18 +01001860 const char *label = NULL;
1861
1862 if (data->temp_label)
1863 label = data->temp_label[data->temp_src[i]];
Jean Delvare2265cef2011-11-04 12:00:47 +01001864
1865 /* Digital source overrides analog type */
Guenter Roeckbfa02b02011-11-06 20:25:18 +01001866 if (label && strncmp(label, "PECI", 4) == 0)
Jean Delvare2265cef2011-11-04 12:00:47 +01001867 data->temp_type[i] = 6;
Guenter Roeckbfa02b02011-11-06 20:25:18 +01001868 else if (label && strncmp(label, "AMD", 3) == 0)
Jean Delvare2265cef2011-11-04 12:00:47 +01001869 data->temp_type[i] = 5;
1870 else if ((tmp & (0x02 << i)))
Jean Delvarebf164c52011-10-13 15:49:08 -04001871 data->temp_type[i] = (diode & (0x10 << i)) ? 1 : 3;
Jean Delvareda667362007-06-24 11:21:02 +02001872 else
1873 data->temp_type[i] = 4; /* thermistor */
1874 }
Jean Delvare08e7e272005-04-25 22:43:25 +02001875}
1876
Guenter Roeckec3e5a12011-02-02 08:46:49 -08001877static void w82627ehf_swap_tempreg(struct w83627ehf_data *data,
1878 int r1, int r2)
1879{
1880 u16 tmp;
1881
1882 tmp = data->temp_src[r1];
1883 data->temp_src[r1] = data->temp_src[r2];
1884 data->temp_src[r2] = tmp;
1885
1886 tmp = data->reg_temp[r1];
1887 data->reg_temp[r1] = data->reg_temp[r2];
1888 data->reg_temp[r2] = tmp;
1889
1890 tmp = data->reg_temp_over[r1];
1891 data->reg_temp_over[r1] = data->reg_temp_over[r2];
1892 data->reg_temp_over[r2] = tmp;
1893
1894 tmp = data->reg_temp_hyst[r1];
1895 data->reg_temp_hyst[r1] = data->reg_temp_hyst[r2];
1896 data->reg_temp_hyst[r2] = tmp;
1897
1898 tmp = data->reg_temp_config[r1];
1899 data->reg_temp_config[r1] = data->reg_temp_config[r2];
1900 data->reg_temp_config[r2] = tmp;
1901}
1902
Jean Delvare03f5de22011-10-13 10:43:14 -04001903static void __devinit
Jean Delvare6ba71de2011-11-04 12:00:47 +01001904w83627ehf_set_temp_reg_ehf(struct w83627ehf_data *data, int n_temp)
1905{
1906 int i;
1907
1908 for (i = 0; i < n_temp; i++) {
1909 data->reg_temp[i] = W83627EHF_REG_TEMP[i];
1910 data->reg_temp_over[i] = W83627EHF_REG_TEMP_OVER[i];
1911 data->reg_temp_hyst[i] = W83627EHF_REG_TEMP_HYST[i];
1912 data->reg_temp_config[i] = W83627EHF_REG_TEMP_CONFIG[i];
1913 }
1914}
1915
1916static void __devinit
Jean Delvare03f5de22011-10-13 10:43:14 -04001917w83627ehf_check_fan_inputs(const struct w83627ehf_sio_data *sio_data,
1918 struct w83627ehf_data *data)
1919{
1920 int fan3pin, fan4pin, fan4min, fan5pin, regval;
1921
Jean Delvareeff76872011-11-04 12:00:48 +01001922 /* The W83627UHG is simple, only two fan inputs, no config */
1923 if (sio_data->kind == w83627uhg) {
1924 data->has_fan = 0x03; /* fan1 and fan2 */
1925 data->has_fan_min = 0x03;
1926 return;
1927 }
1928
Jean Delvare03f5de22011-10-13 10:43:14 -04001929 superio_enter(sio_data->sioreg);
1930
1931 /* fan4 and fan5 share some pins with the GPIO and serial flash */
1932 if (sio_data->kind == nct6775) {
1933 /* On NCT6775, fan4 shares pins with the fdc interface */
1934 fan3pin = 1;
1935 fan4pin = !(superio_inb(sio_data->sioreg, 0x2A) & 0x80);
1936 fan4min = 0;
1937 fan5pin = 0;
1938 } else if (sio_data->kind == nct6776) {
Guenter Roeck585c0fd2012-01-27 05:43:59 -08001939 bool gpok = superio_inb(sio_data->sioreg, 0x27) & 0x80;
1940
1941 superio_select(sio_data->sioreg, W83627EHF_LD_HWM);
1942 regval = superio_inb(sio_data->sioreg, SIO_REG_ENABLE);
1943
1944 if (regval & 0x80)
1945 fan3pin = gpok;
1946 else
1947 fan3pin = !(superio_inb(sio_data->sioreg, 0x24) & 0x40);
1948
1949 if (regval & 0x40)
1950 fan4pin = gpok;
1951 else
1952 fan4pin = !!(superio_inb(sio_data->sioreg, 0x1C) & 0x01);
1953
1954 if (regval & 0x20)
1955 fan5pin = gpok;
1956 else
1957 fan5pin = !!(superio_inb(sio_data->sioreg, 0x1C) & 0x02);
1958
Jean Delvare03f5de22011-10-13 10:43:14 -04001959 fan4min = fan4pin;
1960 } else if (sio_data->kind == w83667hg || sio_data->kind == w83667hg_b) {
1961 fan3pin = 1;
1962 fan4pin = superio_inb(sio_data->sioreg, 0x27) & 0x40;
1963 fan5pin = superio_inb(sio_data->sioreg, 0x27) & 0x20;
1964 fan4min = fan4pin;
1965 } else {
1966 fan3pin = 1;
1967 fan4pin = !(superio_inb(sio_data->sioreg, 0x29) & 0x06);
1968 fan5pin = !(superio_inb(sio_data->sioreg, 0x24) & 0x02);
1969 fan4min = fan4pin;
1970 }
1971
1972 superio_exit(sio_data->sioreg);
1973
1974 data->has_fan = data->has_fan_min = 0x03; /* fan1 and fan2 */
1975 data->has_fan |= (fan3pin << 2);
1976 data->has_fan_min |= (fan3pin << 2);
1977
1978 if (sio_data->kind == nct6775 || sio_data->kind == nct6776) {
1979 /*
1980 * NCT6775F and NCT6776F don't have the W83627EHF_REG_FANDIV1
1981 * register
1982 */
1983 data->has_fan |= (fan4pin << 3) | (fan5pin << 4);
1984 data->has_fan_min |= (fan4min << 3) | (fan5pin << 4);
1985 } else {
1986 /*
1987 * It looks like fan4 and fan5 pins can be alternatively used
1988 * as fan on/off switches, but fan5 control is write only :/
1989 * We assume that if the serial interface is disabled, designers
1990 * connected fan5 as input unless they are emitting log 1, which
1991 * is not the default.
1992 */
1993 regval = w83627ehf_read_value(data, W83627EHF_REG_FANDIV1);
1994 if ((regval & (1 << 2)) && fan4pin) {
1995 data->has_fan |= (1 << 3);
1996 data->has_fan_min |= (1 << 3);
1997 }
1998 if (!(regval & (1 << 1)) && fan5pin) {
1999 data->has_fan |= (1 << 4);
2000 data->has_fan_min |= (1 << 4);
2001 }
2002 }
2003}
2004
David Hubbard1ea6dd32007-06-24 11:16:15 +02002005static int __devinit w83627ehf_probe(struct platform_device *pdev)
Jean Delvare08e7e272005-04-25 22:43:25 +02002006{
David Hubbard1ea6dd32007-06-24 11:16:15 +02002007 struct device *dev = &pdev->dev;
2008 struct w83627ehf_sio_data *sio_data = dev->platform_data;
Jean Delvare08e7e272005-04-25 22:43:25 +02002009 struct w83627ehf_data *data;
David Hubbard1ea6dd32007-06-24 11:16:15 +02002010 struct resource *res;
Jean Delvare03f5de22011-10-13 10:43:14 -04002011 u8 en_vrm10;
Jean Delvare08e7e272005-04-25 22:43:25 +02002012 int i, err = 0;
2013
David Hubbard1ea6dd32007-06-24 11:16:15 +02002014 res = platform_get_resource(pdev, IORESOURCE_IO, 0);
2015 if (!request_region(res->start, IOREGION_LENGTH, DRVNAME)) {
Jean Delvare08e7e272005-04-25 22:43:25 +02002016 err = -EBUSY;
David Hubbard1ea6dd32007-06-24 11:16:15 +02002017 dev_err(dev, "Failed to request region 0x%lx-0x%lx\n",
2018 (unsigned long)res->start,
2019 (unsigned long)res->start + IOREGION_LENGTH - 1);
Jean Delvare08e7e272005-04-25 22:43:25 +02002020 goto exit;
2021 }
2022
Guenter Roeck32260d92012-03-12 08:33:10 -07002023 data = devm_kzalloc(&pdev->dev, sizeof(struct w83627ehf_data),
2024 GFP_KERNEL);
Guenter Roecke7e1ca62011-02-04 13:24:30 -08002025 if (!data) {
Jean Delvare08e7e272005-04-25 22:43:25 +02002026 err = -ENOMEM;
2027 goto exit_release;
2028 }
Jean Delvare08e7e272005-04-25 22:43:25 +02002029
David Hubbard1ea6dd32007-06-24 11:16:15 +02002030 data->addr = res->start;
Ingo Molnar9a61bf62006-01-18 23:19:26 +01002031 mutex_init(&data->lock);
Ingo Molnar9a61bf62006-01-18 23:19:26 +01002032 mutex_init(&data->update_lock);
David Hubbard1ea6dd32007-06-24 11:16:15 +02002033 data->name = w83627ehf_device_names[sio_data->kind];
2034 platform_set_drvdata(pdev, data);
Jean Delvare08e7e272005-04-25 22:43:25 +02002035
Gong Jun237c8d22009-03-30 21:46:42 +02002036 /* 627EHG and 627EHF have 10 voltage inputs; 627DHG and 667HG have 9 */
2037 data->in_num = (sio_data->kind == w83627ehf) ? 10 : 9;
Jean Delvareeff76872011-11-04 12:00:48 +01002038 /* 667HG, NCT6775F, and NCT6776F have 3 pwms, and 627UHG has only 2 */
2039 switch (sio_data->kind) {
2040 default:
2041 data->pwm_num = 4;
2042 break;
2043 case w83667hg:
2044 case w83667hg_b:
2045 case nct6775:
2046 case nct6776:
2047 data->pwm_num = 3;
2048 break;
2049 case w83627uhg:
2050 data->pwm_num = 2;
2051 break;
2052 }
Jean Delvare08e7e272005-04-25 22:43:25 +02002053
Jean Delvare6ba71de2011-11-04 12:00:47 +01002054 /* Default to 3 temperature inputs, code below will adjust as needed */
Guenter Roeckd36cf322011-02-07 15:08:54 -08002055 data->have_temp = 0x07;
Guenter Roeckec3e5a12011-02-02 08:46:49 -08002056
2057 /* Deal with temperature register setup first. */
2058 if (sio_data->kind == nct6775 || sio_data->kind == nct6776) {
2059 int mask = 0;
2060
2061 /*
2062 * Display temperature sensor output only if it monitors
2063 * a source other than one already reported. Always display
2064 * first three temperature registers, though.
2065 */
2066 for (i = 0; i < NUM_REG_TEMP; i++) {
2067 u8 src;
2068
2069 data->reg_temp[i] = NCT6775_REG_TEMP[i];
2070 data->reg_temp_over[i] = NCT6775_REG_TEMP_OVER[i];
2071 data->reg_temp_hyst[i] = NCT6775_REG_TEMP_HYST[i];
2072 data->reg_temp_config[i] = NCT6775_REG_TEMP_CONFIG[i];
2073
2074 src = w83627ehf_read_value(data,
2075 NCT6775_REG_TEMP_SOURCE[i]);
2076 src &= 0x1f;
2077 if (src && !(mask & (1 << src))) {
2078 data->have_temp |= 1 << i;
2079 mask |= 1 << src;
2080 }
2081
2082 data->temp_src[i] = src;
2083
2084 /*
2085 * Now do some register swapping if index 0..2 don't
2086 * point to SYSTIN(1), CPUIN(2), and AUXIN(3).
2087 * Idea is to have the first three attributes
2088 * report SYSTIN, CPUIN, and AUXIN if possible
2089 * without overriding the basic system configuration.
2090 */
2091 if (i > 0 && data->temp_src[0] != 1
2092 && data->temp_src[i] == 1)
2093 w82627ehf_swap_tempreg(data, 0, i);
2094 if (i > 1 && data->temp_src[1] != 2
2095 && data->temp_src[i] == 2)
2096 w82627ehf_swap_tempreg(data, 1, i);
2097 if (i > 2 && data->temp_src[2] != 3
2098 && data->temp_src[i] == 3)
2099 w82627ehf_swap_tempreg(data, 2, i);
2100 }
2101 if (sio_data->kind == nct6776) {
2102 /*
2103 * On NCT6776, AUXTIN and VIN3 pins are shared.
2104 * Only way to detect it is to check if AUXTIN is used
2105 * as a temperature source, and if that source is
2106 * enabled.
2107 *
2108 * If that is the case, disable in6, which reports VIN3.
2109 * Otherwise disable temp3.
2110 */
2111 if (data->temp_src[2] == 3) {
2112 u8 reg;
2113
2114 if (data->reg_temp_config[2])
2115 reg = w83627ehf_read_value(data,
2116 data->reg_temp_config[2]);
2117 else
2118 reg = 0; /* Assume AUXTIN is used */
2119
2120 if (reg & 0x01)
2121 data->have_temp &= ~(1 << 2);
2122 else
2123 data->in6_skip = 1;
2124 }
Guenter Roeck02309ad2011-03-10 15:47:14 -08002125 data->temp_label = nct6776_temp_label;
2126 } else {
2127 data->temp_label = nct6775_temp_label;
Guenter Roeckec3e5a12011-02-02 08:46:49 -08002128 }
Guenter Roeckd36cf322011-02-07 15:08:54 -08002129 } else if (sio_data->kind == w83667hg_b) {
2130 u8 reg;
2131
Jean Delvare6ba71de2011-11-04 12:00:47 +01002132 w83627ehf_set_temp_reg_ehf(data, 4);
2133
Guenter Roeckec3e5a12011-02-02 08:46:49 -08002134 /*
2135 * Temperature sources are selected with bank 0, registers 0x49
2136 * and 0x4a.
2137 */
Guenter Roeckd36cf322011-02-07 15:08:54 -08002138 reg = w83627ehf_read_value(data, 0x4a);
2139 data->temp_src[0] = reg >> 5;
2140 reg = w83627ehf_read_value(data, 0x49);
2141 data->temp_src[1] = reg & 0x07;
Guenter Roeckec3e5a12011-02-02 08:46:49 -08002142 data->temp_src[2] = (reg >> 4) & 0x07;
Guenter Roeckd36cf322011-02-07 15:08:54 -08002143
2144 /*
2145 * W83667HG-B has another temperature register at 0x7e.
2146 * The temperature source is selected with register 0x7d.
2147 * Support it if the source differs from already reported
2148 * sources.
2149 */
2150 reg = w83627ehf_read_value(data, 0x7d);
2151 reg &= 0x07;
2152 if (reg != data->temp_src[0] && reg != data->temp_src[1]
2153 && reg != data->temp_src[2]) {
2154 data->temp_src[3] = reg;
2155 data->have_temp |= 1 << 3;
2156 }
2157
2158 /*
2159 * Chip supports either AUXTIN or VIN3. Try to find out which
2160 * one.
2161 */
2162 reg = w83627ehf_read_value(data, W83627EHF_REG_TEMP_CONFIG[2]);
2163 if (data->temp_src[2] == 2 && (reg & 0x01))
2164 data->have_temp &= ~(1 << 2);
2165
2166 if ((data->temp_src[2] == 2 && (data->have_temp & (1 << 2)))
2167 || (data->temp_src[3] == 2 && (data->have_temp & (1 << 3))))
2168 data->in6_skip = 1;
2169
2170 data->temp_label = w83667hg_b_temp_label;
Jean Delvareeff76872011-11-04 12:00:48 +01002171 } else if (sio_data->kind == w83627uhg) {
2172 u8 reg;
2173
2174 w83627ehf_set_temp_reg_ehf(data, 3);
2175
2176 /*
Jean Delvareaacb6b02012-03-13 04:03:27 -04002177 * Temperature sources for temp2 and temp3 are selected with
Jean Delvareeff76872011-11-04 12:00:48 +01002178 * bank 0, registers 0x49 and 0x4a.
2179 */
2180 data->temp_src[0] = 0; /* SYSTIN */
2181 reg = w83627ehf_read_value(data, 0x49) & 0x07;
2182 /* Adjust to have the same mapping as other source registers */
2183 if (reg == 0)
Jean Delvareaacb6b02012-03-13 04:03:27 -04002184 data->temp_src[1] = 1;
Jean Delvareeff76872011-11-04 12:00:48 +01002185 else if (reg >= 2 && reg <= 5)
Jean Delvareaacb6b02012-03-13 04:03:27 -04002186 data->temp_src[1] = reg + 2;
Jean Delvareeff76872011-11-04 12:00:48 +01002187 else /* should never happen */
2188 data->have_temp &= ~(1 << 1);
2189 reg = w83627ehf_read_value(data, 0x4a);
2190 data->temp_src[2] = reg >> 5;
2191
2192 /*
2193 * Skip temp3 if source is invalid or the same as temp1
2194 * or temp2.
2195 */
2196 if (data->temp_src[2] == 2 || data->temp_src[2] == 3 ||
2197 data->temp_src[2] == data->temp_src[0] ||
2198 ((data->have_temp & (1 << 1)) &&
2199 data->temp_src[2] == data->temp_src[1]))
2200 data->have_temp &= ~(1 << 2);
2201 else
2202 data->temp3_val_only = 1; /* No limit regs */
2203
2204 data->in6_skip = 1; /* No VIN3 */
2205
2206 data->temp_label = w83667hg_b_temp_label;
Guenter Roeckec3e5a12011-02-02 08:46:49 -08002207 } else {
Jean Delvare6ba71de2011-11-04 12:00:47 +01002208 w83627ehf_set_temp_reg_ehf(data, 3);
2209
Guenter Roeckec3e5a12011-02-02 08:46:49 -08002210 /* Temperature sources are fixed */
Jean Delvare6ba71de2011-11-04 12:00:47 +01002211
2212 if (sio_data->kind == w83667hg) {
2213 u8 reg;
2214
2215 /*
2216 * Chip supports either AUXTIN or VIN3. Try to find
2217 * out which one.
2218 */
2219 reg = w83627ehf_read_value(data,
2220 W83627EHF_REG_TEMP_CONFIG[2]);
2221 if (reg & 0x01)
2222 data->have_temp &= ~(1 << 2);
2223 else
2224 data->in6_skip = 1;
Guenter Roeckec3e5a12011-02-02 08:46:49 -08002225 }
Gong Juna157d062009-03-30 21:46:43 +02002226 }
2227
Guenter Roeckec3e5a12011-02-02 08:46:49 -08002228 if (sio_data->kind == nct6775) {
Guenter Roeck26bc4402011-02-11 08:00:58 -08002229 data->has_fan_div = true;
2230 data->fan_from_reg = fan_from_reg16;
2231 data->fan_from_reg_min = fan_from_reg8;
Guenter Roeckec3e5a12011-02-02 08:46:49 -08002232 data->REG_PWM = NCT6775_REG_PWM;
2233 data->REG_TARGET = NCT6775_REG_TARGET;
Guenter Roeck26bc4402011-02-11 08:00:58 -08002234 data->REG_FAN = NCT6775_REG_FAN;
Guenter Roeckec3e5a12011-02-02 08:46:49 -08002235 data->REG_FAN_MIN = W83627EHF_REG_FAN_MIN;
2236 data->REG_FAN_START_OUTPUT = NCT6775_REG_FAN_START_OUTPUT;
2237 data->REG_FAN_STOP_OUTPUT = NCT6775_REG_FAN_STOP_OUTPUT;
2238 data->REG_FAN_STOP_TIME = NCT6775_REG_FAN_STOP_TIME;
2239 data->REG_FAN_MAX_OUTPUT = NCT6775_REG_FAN_MAX_OUTPUT;
2240 data->REG_FAN_STEP_OUTPUT = NCT6775_REG_FAN_STEP_OUTPUT;
2241 } else if (sio_data->kind == nct6776) {
Guenter Roeck26bc4402011-02-11 08:00:58 -08002242 data->has_fan_div = false;
2243 data->fan_from_reg = fan_from_reg13;
2244 data->fan_from_reg_min = fan_from_reg13;
Guenter Roeckec3e5a12011-02-02 08:46:49 -08002245 data->REG_PWM = NCT6775_REG_PWM;
2246 data->REG_TARGET = NCT6775_REG_TARGET;
Guenter Roeck26bc4402011-02-11 08:00:58 -08002247 data->REG_FAN = NCT6775_REG_FAN;
Guenter Roeckec3e5a12011-02-02 08:46:49 -08002248 data->REG_FAN_MIN = NCT6776_REG_FAN_MIN;
2249 data->REG_FAN_START_OUTPUT = NCT6775_REG_FAN_START_OUTPUT;
2250 data->REG_FAN_STOP_OUTPUT = NCT6775_REG_FAN_STOP_OUTPUT;
2251 data->REG_FAN_STOP_TIME = NCT6775_REG_FAN_STOP_TIME;
2252 } else if (sio_data->kind == w83667hg_b) {
Guenter Roeck26bc4402011-02-11 08:00:58 -08002253 data->has_fan_div = true;
2254 data->fan_from_reg = fan_from_reg8;
2255 data->fan_from_reg_min = fan_from_reg8;
Guenter Roeckec3e5a12011-02-02 08:46:49 -08002256 data->REG_PWM = W83627EHF_REG_PWM;
2257 data->REG_TARGET = W83627EHF_REG_TARGET;
2258 data->REG_FAN = W83627EHF_REG_FAN;
2259 data->REG_FAN_MIN = W83627EHF_REG_FAN_MIN;
2260 data->REG_FAN_START_OUTPUT = W83627EHF_REG_FAN_START_OUTPUT;
2261 data->REG_FAN_STOP_OUTPUT = W83627EHF_REG_FAN_STOP_OUTPUT;
2262 data->REG_FAN_STOP_TIME = W83627EHF_REG_FAN_STOP_TIME;
Guenter Roeckc39aeda2010-08-14 21:08:55 +02002263 data->REG_FAN_MAX_OUTPUT =
2264 W83627EHF_REG_FAN_MAX_OUTPUT_W83667_B;
2265 data->REG_FAN_STEP_OUTPUT =
2266 W83627EHF_REG_FAN_STEP_OUTPUT_W83667_B;
2267 } else {
Guenter Roeck26bc4402011-02-11 08:00:58 -08002268 data->has_fan_div = true;
2269 data->fan_from_reg = fan_from_reg8;
2270 data->fan_from_reg_min = fan_from_reg8;
Guenter Roeckec3e5a12011-02-02 08:46:49 -08002271 data->REG_PWM = W83627EHF_REG_PWM;
2272 data->REG_TARGET = W83627EHF_REG_TARGET;
2273 data->REG_FAN = W83627EHF_REG_FAN;
2274 data->REG_FAN_MIN = W83627EHF_REG_FAN_MIN;
2275 data->REG_FAN_START_OUTPUT = W83627EHF_REG_FAN_START_OUTPUT;
2276 data->REG_FAN_STOP_OUTPUT = W83627EHF_REG_FAN_STOP_OUTPUT;
2277 data->REG_FAN_STOP_TIME = W83627EHF_REG_FAN_STOP_TIME;
Guenter Roeckc39aeda2010-08-14 21:08:55 +02002278 data->REG_FAN_MAX_OUTPUT =
2279 W83627EHF_REG_FAN_MAX_OUTPUT_COMMON;
2280 data->REG_FAN_STEP_OUTPUT =
2281 W83627EHF_REG_FAN_STEP_OUTPUT_COMMON;
2282 }
Guenter Roeckda2e0252010-08-14 21:08:55 +02002283
Jean Delvareeff76872011-11-04 12:00:48 +01002284 /* Setup input voltage scaling factors */
2285 if (sio_data->kind == w83627uhg)
2286 data->scale_in = scale_in_w83627uhg;
2287 else
2288 data->scale_in = scale_in_common;
2289
Jean Delvare08e7e272005-04-25 22:43:25 +02002290 /* Initialize the chip */
Jean Delvarebf164c52011-10-13 15:49:08 -04002291 w83627ehf_init_device(data, sio_data->kind);
Jean Delvare08e7e272005-04-25 22:43:25 +02002292
Jean Delvarefc18d6c2007-06-24 11:19:42 +02002293 data->vrm = vid_which_vrm();
2294 superio_enter(sio_data->sioreg);
Jean Delvarefc18d6c2007-06-24 11:19:42 +02002295 /* Read VID value */
Guenter Roeckec3e5a12011-02-02 08:46:49 -08002296 if (sio_data->kind == w83667hg || sio_data->kind == w83667hg_b ||
2297 sio_data->kind == nct6775 || sio_data->kind == nct6776) {
Guenter Roeck8969e842012-01-19 11:02:27 -08002298 /*
2299 * W83667HG has different pins for VID input and output, so
2300 * we can get the VID input values directly at logical device D
2301 * 0xe3.
2302 */
Gong Jun237c8d22009-03-30 21:46:42 +02002303 superio_select(sio_data->sioreg, W83667HG_LD_VID);
2304 data->vid = superio_inb(sio_data->sioreg, 0xe3);
Jean Delvarecbe311f2008-01-03 21:22:44 +01002305 err = device_create_file(dev, &dev_attr_cpu0_vid);
2306 if (err)
2307 goto exit_release;
Jean Delvareeff76872011-11-04 12:00:48 +01002308 } else if (sio_data->kind != w83627uhg) {
Gong Jun237c8d22009-03-30 21:46:42 +02002309 superio_select(sio_data->sioreg, W83627EHF_LD_HWM);
2310 if (superio_inb(sio_data->sioreg, SIO_REG_VID_CTRL) & 0x80) {
Guenter Roeck8969e842012-01-19 11:02:27 -08002311 /*
2312 * Set VID input sensibility if needed. In theory the
2313 * BIOS should have set it, but in practice it's not
2314 * always the case. We only do it for the W83627EHF/EHG
2315 * because the W83627DHG is more complex in this
2316 * respect.
2317 */
Gong Jun237c8d22009-03-30 21:46:42 +02002318 if (sio_data->kind == w83627ehf) {
2319 en_vrm10 = superio_inb(sio_data->sioreg,
2320 SIO_REG_EN_VRM10);
2321 if ((en_vrm10 & 0x08) && data->vrm == 90) {
2322 dev_warn(dev, "Setting VID input "
2323 "voltage to TTL\n");
2324 superio_outb(sio_data->sioreg,
2325 SIO_REG_EN_VRM10,
2326 en_vrm10 & ~0x08);
2327 } else if (!(en_vrm10 & 0x08)
2328 && data->vrm == 100) {
2329 dev_warn(dev, "Setting VID input "
2330 "voltage to VRM10\n");
2331 superio_outb(sio_data->sioreg,
2332 SIO_REG_EN_VRM10,
2333 en_vrm10 | 0x08);
2334 }
2335 }
2336
2337 data->vid = superio_inb(sio_data->sioreg,
2338 SIO_REG_VID_DATA);
2339 if (sio_data->kind == w83627ehf) /* 6 VID pins only */
2340 data->vid &= 0x3f;
2341
2342 err = device_create_file(dev, &dev_attr_cpu0_vid);
2343 if (err)
2344 goto exit_release;
2345 } else {
2346 dev_info(dev, "VID pins in output mode, CPU VID not "
2347 "available\n");
2348 }
Jean Delvarefc18d6c2007-06-24 11:19:42 +02002349 }
2350
Ian Dobsond42e8692011-03-07 14:21:12 -08002351 if (fan_debounce &&
2352 (sio_data->kind == nct6775 || sio_data->kind == nct6776)) {
2353 u8 tmp;
2354
2355 superio_select(sio_data->sioreg, W83627EHF_LD_HWM);
2356 tmp = superio_inb(sio_data->sioreg, NCT6775_REG_FAN_DEBOUNCE);
2357 if (sio_data->kind == nct6776)
2358 superio_outb(sio_data->sioreg, NCT6775_REG_FAN_DEBOUNCE,
2359 0x3e | tmp);
2360 else
2361 superio_outb(sio_data->sioreg, NCT6775_REG_FAN_DEBOUNCE,
2362 0x1e | tmp);
2363 pr_info("Enabled fan debounce for chip %s\n", data->name);
2364 }
2365
David Hubbard1ea6dd32007-06-24 11:16:15 +02002366 superio_exit(sio_data->sioreg);
Rudolf Marek08c79952006-07-05 18:14:31 +02002367
Jean Delvare03f5de22011-10-13 10:43:14 -04002368 w83627ehf_check_fan_inputs(sio_data, data);
Jean Delvare08e7e272005-04-25 22:43:25 +02002369
Mark M. Hoffmanea7be662007-08-05 12:19:01 -04002370 /* Read fan clock dividers immediately */
Guenter Roeckec3e5a12011-02-02 08:46:49 -08002371 w83627ehf_update_fan_div_common(dev, data);
2372
2373 /* Read pwm data to save original values */
2374 w83627ehf_update_pwm_common(dev, data);
2375 for (i = 0; i < data->pwm_num; i++)
2376 data->pwm_enable_orig[i] = data->pwm_enable[i];
Mark M. Hoffmanea7be662007-08-05 12:19:01 -04002377
Jean Delvare08e7e272005-04-25 22:43:25 +02002378 /* Register sysfs hooks */
Guenter Roecke7e1ca62011-02-04 13:24:30 -08002379 for (i = 0; i < ARRAY_SIZE(sda_sf3_arrays); i++) {
2380 err = device_create_file(dev, &sda_sf3_arrays[i].dev_attr);
2381 if (err)
David Hubbardc18beb52006-09-24 21:04:38 +02002382 goto exit_remove;
Guenter Roecke7e1ca62011-02-04 13:24:30 -08002383 }
Rudolf Marek08c79952006-07-05 18:14:31 +02002384
Guenter Roeckda2e0252010-08-14 21:08:55 +02002385 for (i = 0; i < ARRAY_SIZE(sda_sf3_max_step_arrays); i++) {
2386 struct sensor_device_attribute *attr =
2387 &sda_sf3_max_step_arrays[i];
Guenter Roeckec3e5a12011-02-02 08:46:49 -08002388 if (data->REG_FAN_STEP_OUTPUT &&
2389 data->REG_FAN_STEP_OUTPUT[attr->index] != 0xff) {
Guenter Roeckda2e0252010-08-14 21:08:55 +02002390 err = device_create_file(dev, &attr->dev_attr);
2391 if (err)
2392 goto exit_remove;
2393 }
2394 }
Jean Delvareeff76872011-11-04 12:00:48 +01002395 /* if fan3 and fan4 are enabled create the sf3 files for them */
2396 if ((data->has_fan & (1 << 2)) && data->pwm_num >= 3)
2397 for (i = 0; i < ARRAY_SIZE(sda_sf3_arrays_fan3); i++) {
2398 err = device_create_file(dev,
2399 &sda_sf3_arrays_fan3[i].dev_attr);
2400 if (err)
2401 goto exit_remove;
2402 }
Gong Jun237c8d22009-03-30 21:46:42 +02002403 if ((data->has_fan & (1 << 3)) && data->pwm_num >= 4)
David Hubbardc18beb52006-09-24 21:04:38 +02002404 for (i = 0; i < ARRAY_SIZE(sda_sf3_arrays_fan4); i++) {
Guenter Roecke7e1ca62011-02-04 13:24:30 -08002405 err = device_create_file(dev,
2406 &sda_sf3_arrays_fan4[i].dev_attr);
2407 if (err)
David Hubbardc18beb52006-09-24 21:04:38 +02002408 goto exit_remove;
2409 }
Rudolf Marek08c79952006-07-05 18:14:31 +02002410
Gong Juna157d062009-03-30 21:46:43 +02002411 for (i = 0; i < data->in_num; i++) {
2412 if ((i == 6) && data->in6_skip)
2413 continue;
David Hubbardc18beb52006-09-24 21:04:38 +02002414 if ((err = device_create_file(dev, &sda_in_input[i].dev_attr))
2415 || (err = device_create_file(dev,
2416 &sda_in_alarm[i].dev_attr))
2417 || (err = device_create_file(dev,
2418 &sda_in_min[i].dev_attr))
2419 || (err = device_create_file(dev,
2420 &sda_in_max[i].dev_attr)))
2421 goto exit_remove;
Gong Juna157d062009-03-30 21:46:43 +02002422 }
Rudolf Marekcf0676f2006-03-23 16:25:22 +01002423
Yuan Mu412fec82006-02-05 23:24:16 +01002424 for (i = 0; i < 5; i++) {
Rudolf Marek08c79952006-07-05 18:14:31 +02002425 if (data->has_fan & (1 << i)) {
David Hubbardc18beb52006-09-24 21:04:38 +02002426 if ((err = device_create_file(dev,
2427 &sda_fan_input[i].dev_attr))
2428 || (err = device_create_file(dev,
Guenter Roeckec3e5a12011-02-02 08:46:49 -08002429 &sda_fan_alarm[i].dev_attr)))
David Hubbardc18beb52006-09-24 21:04:38 +02002430 goto exit_remove;
Guenter Roeckec3e5a12011-02-02 08:46:49 -08002431 if (sio_data->kind != nct6776) {
2432 err = device_create_file(dev,
2433 &sda_fan_div[i].dev_attr);
2434 if (err)
2435 goto exit_remove;
2436 }
2437 if (data->has_fan_min & (1 << i)) {
2438 err = device_create_file(dev,
2439 &sda_fan_min[i].dev_attr);
2440 if (err)
2441 goto exit_remove;
2442 }
Gong Jun237c8d22009-03-30 21:46:42 +02002443 if (i < data->pwm_num &&
David Hubbardc18beb52006-09-24 21:04:38 +02002444 ((err = device_create_file(dev,
2445 &sda_pwm[i].dev_attr))
2446 || (err = device_create_file(dev,
2447 &sda_pwm_mode[i].dev_attr))
2448 || (err = device_create_file(dev,
2449 &sda_pwm_enable[i].dev_attr))
2450 || (err = device_create_file(dev,
2451 &sda_target_temp[i].dev_attr))
2452 || (err = device_create_file(dev,
2453 &sda_tolerance[i].dev_attr))))
2454 goto exit_remove;
Rudolf Marek08c79952006-07-05 18:14:31 +02002455 }
Jean Delvare08e7e272005-04-25 22:43:25 +02002456 }
Rudolf Marek08c79952006-07-05 18:14:31 +02002457
Guenter Roeckd36cf322011-02-07 15:08:54 -08002458 for (i = 0; i < NUM_REG_TEMP; i++) {
2459 if (!(data->have_temp & (1 << i)))
Gong Juna157d062009-03-30 21:46:43 +02002460 continue;
Guenter Roeckd36cf322011-02-07 15:08:54 -08002461 err = device_create_file(dev, &sda_temp_input[i].dev_attr);
2462 if (err)
2463 goto exit_remove;
2464 if (data->temp_label) {
2465 err = device_create_file(dev,
2466 &sda_temp_label[i].dev_attr);
2467 if (err)
2468 goto exit_remove;
2469 }
Jean Delvareeff76872011-11-04 12:00:48 +01002470 if (i == 2 && data->temp3_val_only)
2471 continue;
Guenter Roeckec3e5a12011-02-02 08:46:49 -08002472 if (data->reg_temp_over[i]) {
2473 err = device_create_file(dev,
2474 &sda_temp_max[i].dev_attr);
2475 if (err)
2476 goto exit_remove;
2477 }
2478 if (data->reg_temp_hyst[i]) {
2479 err = device_create_file(dev,
2480 &sda_temp_max_hyst[i].dev_attr);
2481 if (err)
2482 goto exit_remove;
2483 }
Guenter Roeckd36cf322011-02-07 15:08:54 -08002484 if (i > 2)
Guenter Roeckec3e5a12011-02-02 08:46:49 -08002485 continue;
2486 if ((err = device_create_file(dev,
Gong Juna157d062009-03-30 21:46:43 +02002487 &sda_temp_alarm[i].dev_attr))
2488 || (err = device_create_file(dev,
2489 &sda_temp_type[i].dev_attr)))
David Hubbardc18beb52006-09-24 21:04:38 +02002490 goto exit_remove;
Gong Juna157d062009-03-30 21:46:43 +02002491 }
David Hubbardc18beb52006-09-24 21:04:38 +02002492
Dmitry Artamonow363a12a2011-08-12 16:41:11 -04002493 err = device_create_file(dev, &sda_caseopen[0].dev_attr);
2494 if (err)
2495 goto exit_remove;
2496
2497 if (sio_data->kind == nct6776) {
2498 err = device_create_file(dev, &sda_caseopen[1].dev_attr);
2499 if (err)
2500 goto exit_remove;
2501 }
2502
David Hubbard1ea6dd32007-06-24 11:16:15 +02002503 err = device_create_file(dev, &dev_attr_name);
2504 if (err)
2505 goto exit_remove;
2506
Tony Jones1beeffe2007-08-20 13:46:20 -07002507 data->hwmon_dev = hwmon_device_register(dev);
2508 if (IS_ERR(data->hwmon_dev)) {
2509 err = PTR_ERR(data->hwmon_dev);
David Hubbardc18beb52006-09-24 21:04:38 +02002510 goto exit_remove;
2511 }
Jean Delvare08e7e272005-04-25 22:43:25 +02002512
2513 return 0;
2514
David Hubbardc18beb52006-09-24 21:04:38 +02002515exit_remove:
2516 w83627ehf_device_remove_files(dev);
Jean Delvare08e7e272005-04-25 22:43:25 +02002517exit_release:
Guenter Roeck32260d92012-03-12 08:33:10 -07002518 platform_set_drvdata(pdev, NULL);
David Hubbard1ea6dd32007-06-24 11:16:15 +02002519 release_region(res->start, IOREGION_LENGTH);
Jean Delvare08e7e272005-04-25 22:43:25 +02002520exit:
2521 return err;
2522}
2523
David Hubbard1ea6dd32007-06-24 11:16:15 +02002524static int __devexit w83627ehf_remove(struct platform_device *pdev)
Jean Delvare08e7e272005-04-25 22:43:25 +02002525{
David Hubbard1ea6dd32007-06-24 11:16:15 +02002526 struct w83627ehf_data *data = platform_get_drvdata(pdev);
Jean Delvare08e7e272005-04-25 22:43:25 +02002527
Tony Jones1beeffe2007-08-20 13:46:20 -07002528 hwmon_device_unregister(data->hwmon_dev);
David Hubbard1ea6dd32007-06-24 11:16:15 +02002529 w83627ehf_device_remove_files(&pdev->dev);
2530 release_region(data->addr, IOREGION_LENGTH);
2531 platform_set_drvdata(pdev, NULL);
Jean Delvare08e7e272005-04-25 22:43:25 +02002532
2533 return 0;
2534}
2535
David Hubbard1ea6dd32007-06-24 11:16:15 +02002536static struct platform_driver w83627ehf_driver = {
Laurent Riffardcdaf7932005-11-26 20:37:41 +01002537 .driver = {
Jean Delvare87218842006-09-03 22:36:14 +02002538 .owner = THIS_MODULE,
David Hubbard1ea6dd32007-06-24 11:16:15 +02002539 .name = DRVNAME,
Laurent Riffardcdaf7932005-11-26 20:37:41 +01002540 },
David Hubbard1ea6dd32007-06-24 11:16:15 +02002541 .probe = w83627ehf_probe,
2542 .remove = __devexit_p(w83627ehf_remove),
Jean Delvare08e7e272005-04-25 22:43:25 +02002543};
2544
David Hubbard1ea6dd32007-06-24 11:16:15 +02002545/* w83627ehf_find() looks for a '627 in the Super-I/O config space */
2546static int __init w83627ehf_find(int sioaddr, unsigned short *addr,
2547 struct w83627ehf_sio_data *sio_data)
Jean Delvare08e7e272005-04-25 22:43:25 +02002548{
David Hubbard1ea6dd32007-06-24 11:16:15 +02002549 static const char __initdata sio_name_W83627EHF[] = "W83627EHF";
2550 static const char __initdata sio_name_W83627EHG[] = "W83627EHG";
2551 static const char __initdata sio_name_W83627DHG[] = "W83627DHG";
Jean Delvarec1e48dc2009-06-15 18:39:50 +02002552 static const char __initdata sio_name_W83627DHG_P[] = "W83627DHG-P";
Jean Delvareeff76872011-11-04 12:00:48 +01002553 static const char __initdata sio_name_W83627UHG[] = "W83627UHG";
Gong Jun237c8d22009-03-30 21:46:42 +02002554 static const char __initdata sio_name_W83667HG[] = "W83667HG";
Guenter Roeckc39aeda2010-08-14 21:08:55 +02002555 static const char __initdata sio_name_W83667HG_B[] = "W83667HG-B";
Guenter Roeckec3e5a12011-02-02 08:46:49 -08002556 static const char __initdata sio_name_NCT6775[] = "NCT6775F";
2557 static const char __initdata sio_name_NCT6776[] = "NCT6776F";
David Hubbard1ea6dd32007-06-24 11:16:15 +02002558
Jean Delvare08e7e272005-04-25 22:43:25 +02002559 u16 val;
David Hubbard1ea6dd32007-06-24 11:16:15 +02002560 const char *sio_name;
Jean Delvare08e7e272005-04-25 22:43:25 +02002561
David Hubbard1ea6dd32007-06-24 11:16:15 +02002562 superio_enter(sioaddr);
Jean Delvare08e7e272005-04-25 22:43:25 +02002563
Jean Delvare67b671b2007-12-06 23:13:42 +01002564 if (force_id)
2565 val = force_id;
2566 else
2567 val = (superio_inb(sioaddr, SIO_REG_DEVID) << 8)
2568 | superio_inb(sioaddr, SIO_REG_DEVID + 1);
David Hubbard657c93b2007-02-14 21:15:04 +01002569 switch (val & SIO_ID_MASK) {
David Hubbard657c93b2007-02-14 21:15:04 +01002570 case SIO_W83627EHF_ID:
David Hubbard1ea6dd32007-06-24 11:16:15 +02002571 sio_data->kind = w83627ehf;
2572 sio_name = sio_name_W83627EHF;
2573 break;
David Hubbard657c93b2007-02-14 21:15:04 +01002574 case SIO_W83627EHG_ID:
David Hubbard1ea6dd32007-06-24 11:16:15 +02002575 sio_data->kind = w83627ehf;
2576 sio_name = sio_name_W83627EHG;
2577 break;
2578 case SIO_W83627DHG_ID:
2579 sio_data->kind = w83627dhg;
2580 sio_name = sio_name_W83627DHG;
David Hubbard657c93b2007-02-14 21:15:04 +01002581 break;
Jean Delvarec1e48dc2009-06-15 18:39:50 +02002582 case SIO_W83627DHG_P_ID:
2583 sio_data->kind = w83627dhg_p;
2584 sio_name = sio_name_W83627DHG_P;
2585 break;
Jean Delvareeff76872011-11-04 12:00:48 +01002586 case SIO_W83627UHG_ID:
2587 sio_data->kind = w83627uhg;
2588 sio_name = sio_name_W83627UHG;
2589 break;
Gong Jun237c8d22009-03-30 21:46:42 +02002590 case SIO_W83667HG_ID:
2591 sio_data->kind = w83667hg;
2592 sio_name = sio_name_W83667HG;
2593 break;
Guenter Roeckc39aeda2010-08-14 21:08:55 +02002594 case SIO_W83667HG_B_ID:
2595 sio_data->kind = w83667hg_b;
2596 sio_name = sio_name_W83667HG_B;
2597 break;
Guenter Roeckec3e5a12011-02-02 08:46:49 -08002598 case SIO_NCT6775_ID:
2599 sio_data->kind = nct6775;
2600 sio_name = sio_name_NCT6775;
2601 break;
2602 case SIO_NCT6776_ID:
2603 sio_data->kind = nct6776;
2604 sio_name = sio_name_NCT6776;
2605 break;
David Hubbard657c93b2007-02-14 21:15:04 +01002606 default:
Jean Delvare9f660362007-06-24 11:23:41 +02002607 if (val != 0xffff)
Joe Perchesabdc6fd2010-10-20 06:51:54 +00002608 pr_debug("unsupported chip ID: 0x%04x\n", val);
David Hubbard1ea6dd32007-06-24 11:16:15 +02002609 superio_exit(sioaddr);
Jean Delvare08e7e272005-04-25 22:43:25 +02002610 return -ENODEV;
2611 }
2612
David Hubbard1ea6dd32007-06-24 11:16:15 +02002613 /* We have a known chip, find the HWM I/O address */
2614 superio_select(sioaddr, W83627EHF_LD_HWM);
2615 val = (superio_inb(sioaddr, SIO_REG_ADDR) << 8)
2616 | superio_inb(sioaddr, SIO_REG_ADDR + 1);
Jean Delvare1a641fc2007-04-23 14:41:16 -07002617 *addr = val & IOREGION_ALIGNMENT;
Jean Delvare2d8672c2005-07-19 23:56:35 +02002618 if (*addr == 0) {
Joe Perchesabdc6fd2010-10-20 06:51:54 +00002619 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 +02002620 superio_exit(sioaddr);
Jean Delvare08e7e272005-04-25 22:43:25 +02002621 return -ENODEV;
2622 }
2623
2624 /* Activate logical device if needed */
David Hubbard1ea6dd32007-06-24 11:16:15 +02002625 val = superio_inb(sioaddr, SIO_REG_ENABLE);
David Hubbard475ef852007-06-24 11:17:09 +02002626 if (!(val & 0x01)) {
Guenter Roecke7e1ca62011-02-04 13:24:30 -08002627 pr_warn("Forcibly enabling Super-I/O. "
2628 "Sensor is probably unusable.\n");
David Hubbard1ea6dd32007-06-24 11:16:15 +02002629 superio_outb(sioaddr, SIO_REG_ENABLE, val | 0x01);
David Hubbard475ef852007-06-24 11:17:09 +02002630 }
Jean Delvare08e7e272005-04-25 22:43:25 +02002631
David Hubbard1ea6dd32007-06-24 11:16:15 +02002632 superio_exit(sioaddr);
Joe Perchesabdc6fd2010-10-20 06:51:54 +00002633 pr_info("Found %s chip at %#x\n", sio_name, *addr);
David Hubbard1ea6dd32007-06-24 11:16:15 +02002634 sio_data->sioreg = sioaddr;
2635
Jean Delvare08e7e272005-04-25 22:43:25 +02002636 return 0;
2637}
2638
Guenter Roeck8969e842012-01-19 11:02:27 -08002639/*
2640 * when Super-I/O functions move to a separate file, the Super-I/O
David Hubbard1ea6dd32007-06-24 11:16:15 +02002641 * bus will manage the lifetime of the device and this module will only keep
2642 * track of the w83627ehf driver. But since we platform_device_alloc(), we
Guenter Roeck8969e842012-01-19 11:02:27 -08002643 * must keep track of the device
2644 */
David Hubbard1ea6dd32007-06-24 11:16:15 +02002645static struct platform_device *pdev;
2646
Jean Delvare08e7e272005-04-25 22:43:25 +02002647static int __init sensors_w83627ehf_init(void)
2648{
David Hubbard1ea6dd32007-06-24 11:16:15 +02002649 int err;
2650 unsigned short address;
2651 struct resource res;
2652 struct w83627ehf_sio_data sio_data;
2653
Guenter Roeck8969e842012-01-19 11:02:27 -08002654 /*
2655 * initialize sio_data->kind and sio_data->sioreg.
David Hubbard1ea6dd32007-06-24 11:16:15 +02002656 *
2657 * when Super-I/O functions move to a separate file, the Super-I/O
2658 * driver will probe 0x2e and 0x4e and auto-detect the presence of a
Guenter Roeck8969e842012-01-19 11:02:27 -08002659 * w83627ehf hardware monitor, and call probe()
2660 */
David Hubbard1ea6dd32007-06-24 11:16:15 +02002661 if (w83627ehf_find(0x2e, &address, &sio_data) &&
2662 w83627ehf_find(0x4e, &address, &sio_data))
Jean Delvare08e7e272005-04-25 22:43:25 +02002663 return -ENODEV;
2664
David Hubbard1ea6dd32007-06-24 11:16:15 +02002665 err = platform_driver_register(&w83627ehf_driver);
2666 if (err)
2667 goto exit;
2668
Guenter Roecke7e1ca62011-02-04 13:24:30 -08002669 pdev = platform_device_alloc(DRVNAME, address);
2670 if (!pdev) {
David Hubbard1ea6dd32007-06-24 11:16:15 +02002671 err = -ENOMEM;
Joe Perchesabdc6fd2010-10-20 06:51:54 +00002672 pr_err("Device allocation failed\n");
David Hubbard1ea6dd32007-06-24 11:16:15 +02002673 goto exit_unregister;
2674 }
2675
2676 err = platform_device_add_data(pdev, &sio_data,
2677 sizeof(struct w83627ehf_sio_data));
2678 if (err) {
Joe Perchesabdc6fd2010-10-20 06:51:54 +00002679 pr_err("Platform data allocation failed\n");
David Hubbard1ea6dd32007-06-24 11:16:15 +02002680 goto exit_device_put;
2681 }
2682
2683 memset(&res, 0, sizeof(res));
2684 res.name = DRVNAME;
2685 res.start = address + IOREGION_OFFSET;
2686 res.end = address + IOREGION_OFFSET + IOREGION_LENGTH - 1;
2687 res.flags = IORESOURCE_IO;
Jean Delvareb9acb642009-01-07 16:37:35 +01002688
2689 err = acpi_check_resource_conflict(&res);
2690 if (err)
Hans de Goede18632f82009-02-17 19:59:54 +01002691 goto exit_device_put;
Jean Delvareb9acb642009-01-07 16:37:35 +01002692
David Hubbard1ea6dd32007-06-24 11:16:15 +02002693 err = platform_device_add_resources(pdev, &res, 1);
2694 if (err) {
Joe Perchesabdc6fd2010-10-20 06:51:54 +00002695 pr_err("Device resource addition failed (%d)\n", err);
David Hubbard1ea6dd32007-06-24 11:16:15 +02002696 goto exit_device_put;
2697 }
2698
2699 /* platform_device_add calls probe() */
2700 err = platform_device_add(pdev);
2701 if (err) {
Joe Perchesabdc6fd2010-10-20 06:51:54 +00002702 pr_err("Device addition failed (%d)\n", err);
David Hubbard1ea6dd32007-06-24 11:16:15 +02002703 goto exit_device_put;
2704 }
2705
2706 return 0;
2707
2708exit_device_put:
2709 platform_device_put(pdev);
2710exit_unregister:
2711 platform_driver_unregister(&w83627ehf_driver);
2712exit:
2713 return err;
Jean Delvare08e7e272005-04-25 22:43:25 +02002714}
2715
2716static void __exit sensors_w83627ehf_exit(void)
2717{
David Hubbard1ea6dd32007-06-24 11:16:15 +02002718 platform_device_unregister(pdev);
2719 platform_driver_unregister(&w83627ehf_driver);
Jean Delvare08e7e272005-04-25 22:43:25 +02002720}
2721
2722MODULE_AUTHOR("Jean Delvare <khali@linux-fr.org>");
2723MODULE_DESCRIPTION("W83627EHF driver");
2724MODULE_LICENSE("GPL");
2725
2726module_init(sensors_w83627ehf_init);
2727module_exit(sensors_w83627ehf_exit);