blob: 2f17f99e0ae15c95053cafd5d014a66228613242 [file] [log] [blame]
Jean Delvare08e7e272005-04-25 22:43:25 +02001/*
2 w83627ehf - Driver for the hardware monitoring functionality of
Guenter Roecke7e1ca62011-02-04 13:24:30 -08003 the Winbond W83627EHF Super-I/O chip
Jean Delvare08e7e272005-04-25 22:43:25 +02004 Copyright (C) 2005 Jean Delvare <khali@linux-fr.org>
Jean Delvare3379cee2006-09-24 21:25:52 +02005 Copyright (C) 2006 Yuan Mu (Winbond),
Guenter Roecke7e1ca62011-02-04 13:24:30 -08006 Rudolf Marek <r.marek@assembler.cz>
7 David Hubbard <david.c.hubbard@gmail.com>
Daniel J Blueman41e9a062009-12-14 18:01:37 -08008 Daniel J Blueman <daniel.blueman@gmail.com>
Jean Delvare08e7e272005-04-25 22:43:25 +02009
10 Shamelessly ripped from the w83627hf driver
11 Copyright (C) 2003 Mark Studebaker
12
13 Thanks to Leon Moonen, Steve Cliffe and Grant Coady for their help
14 in testing and debugging this driver.
15
Jean Delvare8dd2d2c2005-07-27 21:33:15 +020016 This driver also supports the W83627EHG, which is the lead-free
17 version of the W83627EHF.
18
Jean Delvare08e7e272005-04-25 22:43:25 +020019 This program is free software; you can redistribute it and/or modify
20 it under the terms of the GNU General Public License as published by
21 the Free Software Foundation; either version 2 of the License, or
22 (at your option) any later version.
23
24 This program is distributed in the hope that it will be useful,
25 but WITHOUT ANY WARRANTY; without even the implied warranty of
26 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
27 GNU General Public License for more details.
28
29 You should have received a copy of the GNU General Public License
30 along with this program; if not, write to the Free Software
31 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
32
33
34 Supports the following chips:
35
David Hubbard657c93b2007-02-14 21:15:04 +010036 Chip #vin #fan #pwm #temp chip IDs man ID
37 w83627ehf 10 5 4 3 0x8850 0x88 0x5ca3
Guenter Roecke7e1ca62011-02-04 13:24:30 -080038 0x8860 0xa1
David Hubbard657c93b2007-02-14 21:15:04 +010039 w83627dhg 9 5 4 3 0xa020 0xc1 0x5ca3
Jean Delvarec1e48dc2009-06-15 18:39:50 +020040 w83627dhg-p 9 5 4 3 0xb070 0xc1 0x5ca3
Gong Jun237c8d2f2009-03-30 21:46:42 +020041 w83667hg 9 5 3 3 0xa510 0xc1 0x5ca3
Guenter Roeckd36cf322011-02-07 15:08:54 -080042 w83667hg-b 9 5 3 4 0xb350 0xc1 0x5ca3
Jean Delvare08e7e272005-04-25 22:43:25 +020043*/
44
Joe Perchesabdc6fd2010-10-20 06:51:54 +000045#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
46
Jean Delvare08e7e272005-04-25 22:43:25 +020047#include <linux/module.h>
48#include <linux/init.h>
49#include <linux/slab.h>
David Hubbard1ea6dd32007-06-24 11:16:15 +020050#include <linux/jiffies.h>
51#include <linux/platform_device.h>
Mark M. Hoffman943b0832005-07-15 21:39:18 -040052#include <linux/hwmon.h>
Yuan Mu412fec82006-02-05 23:24:16 +010053#include <linux/hwmon-sysfs.h>
Jean Delvarefc18d6c2007-06-24 11:19:42 +020054#include <linux/hwmon-vid.h>
Mark M. Hoffman943b0832005-07-15 21:39:18 -040055#include <linux/err.h>
Ingo Molnar9a61bf62006-01-18 23:19:26 +010056#include <linux/mutex.h>
Jean Delvareb9acb642009-01-07 16:37:35 +010057#include <linux/acpi.h>
H Hartley Sweeten6055fae2009-09-15 17:18:13 +020058#include <linux/io.h>
Jean Delvare08e7e272005-04-25 22:43:25 +020059#include "lm75.h"
60
Guenter Roeckc39aeda2010-08-14 21:08:55 +020061enum kinds { w83627ehf, w83627dhg, w83627dhg_p, w83667hg, w83667hg_b };
David Hubbard1ea6dd32007-06-24 11:16:15 +020062
63/* used to set data->name = w83627ehf_device_names[data->sio_kind] */
Guenter Roecke7e1ca62011-02-04 13:24:30 -080064static const char * const w83627ehf_device_names[] = {
David Hubbard1ea6dd32007-06-24 11:16:15 +020065 "w83627ehf",
66 "w83627dhg",
Jean Delvarec1e48dc2009-06-15 18:39:50 +020067 "w83627dhg",
Gong Jun237c8d2f2009-03-30 21:46:42 +020068 "w83667hg",
Guenter Roeckc39aeda2010-08-14 21:08:55 +020069 "w83667hg",
David Hubbard1ea6dd32007-06-24 11:16:15 +020070};
71
Jean Delvare67b671b2007-12-06 23:13:42 +010072static unsigned short force_id;
73module_param(force_id, ushort, 0);
74MODULE_PARM_DESC(force_id, "Override the detected device ID");
75
David Hubbard1ea6dd32007-06-24 11:16:15 +020076#define DRVNAME "w83627ehf"
Jean Delvare08e7e272005-04-25 22:43:25 +020077
78/*
79 * Super-I/O constants and functions
80 */
81
Jean Delvare08e7e272005-04-25 22:43:25 +020082#define W83627EHF_LD_HWM 0x0b
Guenter Roecke7e1ca62011-02-04 13:24:30 -080083#define W83667HG_LD_VID 0x0d
Jean Delvare08e7e272005-04-25 22:43:25 +020084
85#define SIO_REG_LDSEL 0x07 /* Logical device select */
86#define SIO_REG_DEVID 0x20 /* Device ID (2 bytes) */
Jean Delvarefc18d6c2007-06-24 11:19:42 +020087#define SIO_REG_EN_VRM10 0x2C /* GPIO3, GPIO4 selection */
Jean Delvare08e7e272005-04-25 22:43:25 +020088#define SIO_REG_ENABLE 0x30 /* Logical device enable */
89#define SIO_REG_ADDR 0x60 /* Logical device address (2 bytes) */
Jean Delvarefc18d6c2007-06-24 11:19:42 +020090#define SIO_REG_VID_CTRL 0xF0 /* VID control */
91#define SIO_REG_VID_DATA 0xF1 /* VID data */
Jean Delvare08e7e272005-04-25 22:43:25 +020092
David Hubbard657c93b2007-02-14 21:15:04 +010093#define SIO_W83627EHF_ID 0x8850
94#define SIO_W83627EHG_ID 0x8860
95#define SIO_W83627DHG_ID 0xa020
Jean Delvarec1e48dc2009-06-15 18:39:50 +020096#define SIO_W83627DHG_P_ID 0xb070
Guenter Roecke7e1ca62011-02-04 13:24:30 -080097#define SIO_W83667HG_ID 0xa510
Guenter Roeckc39aeda2010-08-14 21:08:55 +020098#define SIO_W83667HG_B_ID 0xb350
David Hubbard657c93b2007-02-14 21:15:04 +010099#define SIO_ID_MASK 0xFFF0
Jean Delvare08e7e272005-04-25 22:43:25 +0200100
101static inline void
David Hubbard1ea6dd32007-06-24 11:16:15 +0200102superio_outb(int ioreg, int reg, int val)
Jean Delvare08e7e272005-04-25 22:43:25 +0200103{
David Hubbard1ea6dd32007-06-24 11:16:15 +0200104 outb(reg, ioreg);
105 outb(val, ioreg + 1);
Jean Delvare08e7e272005-04-25 22:43:25 +0200106}
107
108static inline int
David Hubbard1ea6dd32007-06-24 11:16:15 +0200109superio_inb(int ioreg, int reg)
Jean Delvare08e7e272005-04-25 22:43:25 +0200110{
David Hubbard1ea6dd32007-06-24 11:16:15 +0200111 outb(reg, ioreg);
112 return inb(ioreg + 1);
Jean Delvare08e7e272005-04-25 22:43:25 +0200113}
114
115static inline void
David Hubbard1ea6dd32007-06-24 11:16:15 +0200116superio_select(int ioreg, int ld)
Jean Delvare08e7e272005-04-25 22:43:25 +0200117{
David Hubbard1ea6dd32007-06-24 11:16:15 +0200118 outb(SIO_REG_LDSEL, ioreg);
119 outb(ld, ioreg + 1);
Jean Delvare08e7e272005-04-25 22:43:25 +0200120}
121
122static inline void
David Hubbard1ea6dd32007-06-24 11:16:15 +0200123superio_enter(int ioreg)
Jean Delvare08e7e272005-04-25 22:43:25 +0200124{
David Hubbard1ea6dd32007-06-24 11:16:15 +0200125 outb(0x87, ioreg);
126 outb(0x87, ioreg);
Jean Delvare08e7e272005-04-25 22:43:25 +0200127}
128
129static inline void
David Hubbard1ea6dd32007-06-24 11:16:15 +0200130superio_exit(int ioreg)
Jean Delvare08e7e272005-04-25 22:43:25 +0200131{
Jonas Jonsson022b75a2010-09-17 17:24:13 +0200132 outb(0xaa, ioreg);
David Hubbard1ea6dd32007-06-24 11:16:15 +0200133 outb(0x02, ioreg);
134 outb(0x02, ioreg + 1);
Jean Delvare08e7e272005-04-25 22:43:25 +0200135}
136
137/*
138 * ISA constants
139 */
140
Guenter Roecke7e1ca62011-02-04 13:24:30 -0800141#define IOREGION_ALIGNMENT (~7)
Jean Delvare1a641fc2007-04-23 14:41:16 -0700142#define IOREGION_OFFSET 5
143#define IOREGION_LENGTH 2
David Hubbard1ea6dd32007-06-24 11:16:15 +0200144#define ADDR_REG_OFFSET 0
145#define DATA_REG_OFFSET 1
Jean Delvare08e7e272005-04-25 22:43:25 +0200146
147#define W83627EHF_REG_BANK 0x4E
148#define W83627EHF_REG_CONFIG 0x40
David Hubbard657c93b2007-02-14 21:15:04 +0100149
150/* Not currently used:
151 * REG_MAN_ID has the value 0x5ca3 for all supported chips.
152 * REG_CHIP_ID == 0x88/0xa1/0xc1 depending on chip model.
153 * REG_MAN_ID is at port 0x4f
154 * REG_CHIP_ID is at port 0x58 */
Jean Delvare08e7e272005-04-25 22:43:25 +0200155
156static const u16 W83627EHF_REG_FAN[] = { 0x28, 0x29, 0x2a, 0x3f, 0x553 };
157static const u16 W83627EHF_REG_FAN_MIN[] = { 0x3b, 0x3c, 0x3d, 0x3e, 0x55c };
158
Rudolf Marekcf0676f2006-03-23 16:25:22 +0100159/* The W83627EHF registers for nr=7,8,9 are in bank 5 */
160#define W83627EHF_REG_IN_MAX(nr) ((nr < 7) ? (0x2b + (nr) * 2) : \
161 (0x554 + (((nr) - 7) * 2)))
162#define W83627EHF_REG_IN_MIN(nr) ((nr < 7) ? (0x2c + (nr) * 2) : \
163 (0x555 + (((nr) - 7) * 2)))
164#define W83627EHF_REG_IN(nr) ((nr < 7) ? (0x20 + (nr)) : \
165 (0x550 + (nr) - 7))
166
Guenter Roeckd36cf322011-02-07 15:08:54 -0800167static const u16 W83627EHF_REG_TEMP[] = { 0x27, 0x150, 0x250, 0x7e };
168static const u16 W83627EHF_REG_TEMP_HYST[] = { 0x3a, 0x153, 0x253, 0 };
169static const u16 W83627EHF_REG_TEMP_OVER[] = { 0x39, 0x155, 0x255, 0 };
170static const u16 W83627EHF_REG_TEMP_CONFIG[] = { 0, 0x152, 0x252, 0 };
Jean Delvare08e7e272005-04-25 22:43:25 +0200171
172/* Fan clock dividers are spread over the following five registers */
173#define W83627EHF_REG_FANDIV1 0x47
174#define W83627EHF_REG_FANDIV2 0x4B
175#define W83627EHF_REG_VBAT 0x5D
176#define W83627EHF_REG_DIODE 0x59
177#define W83627EHF_REG_SMI_OVT 0x4C
178
Jean Delvarea4589db2006-03-23 16:30:29 +0100179#define W83627EHF_REG_ALARM1 0x459
180#define W83627EHF_REG_ALARM2 0x45A
181#define W83627EHF_REG_ALARM3 0x45B
182
Rudolf Marek08c79952006-07-05 18:14:31 +0200183/* SmartFan registers */
Daniel J Blueman41e9a062009-12-14 18:01:37 -0800184#define W83627EHF_REG_FAN_STEPUP_TIME 0x0f
185#define W83627EHF_REG_FAN_STEPDOWN_TIME 0x0e
186
Rudolf Marek08c79952006-07-05 18:14:31 +0200187/* DC or PWM output fan configuration */
188static const u8 W83627EHF_REG_PWM_ENABLE[] = {
189 0x04, /* SYS FAN0 output mode and PWM mode */
190 0x04, /* CPU FAN0 output mode and PWM mode */
191 0x12, /* AUX FAN mode */
Daniel J Blueman41e9a062009-12-14 18:01:37 -0800192 0x62, /* CPU FAN1 mode */
Rudolf Marek08c79952006-07-05 18:14:31 +0200193};
194
195static const u8 W83627EHF_PWM_MODE_SHIFT[] = { 0, 1, 0, 6 };
196static const u8 W83627EHF_PWM_ENABLE_SHIFT[] = { 2, 4, 1, 4 };
197
198/* FAN Duty Cycle, be used to control */
Guenter Roeck279af1a2011-02-13 22:34:47 -0800199static const u16 W83627EHF_REG_PWM[] = { 0x01, 0x03, 0x11, 0x61 };
200static const u16 W83627EHF_REG_TARGET[] = { 0x05, 0x06, 0x13, 0x63 };
Rudolf Marek08c79952006-07-05 18:14:31 +0200201static const u8 W83627EHF_REG_TOLERANCE[] = { 0x07, 0x07, 0x14, 0x62 };
202
Rudolf Marek08c79952006-07-05 18:14:31 +0200203/* Advanced Fan control, some values are common for all fans */
Guenter Roeck279af1a2011-02-13 22:34:47 -0800204static const u16 W83627EHF_REG_FAN_START_OUTPUT[] = { 0x0a, 0x0b, 0x16, 0x65 };
205static const u16 W83627EHF_REG_FAN_STOP_OUTPUT[] = { 0x08, 0x09, 0x15, 0x64 };
206static const u16 W83627EHF_REG_FAN_STOP_TIME[] = { 0x0c, 0x0d, 0x17, 0x66 };
Guenter Roeckc39aeda2010-08-14 21:08:55 +0200207
Guenter Roeck279af1a2011-02-13 22:34:47 -0800208static const u16 W83627EHF_REG_FAN_MAX_OUTPUT_COMMON[]
Guenter Roeckc39aeda2010-08-14 21:08:55 +0200209 = { 0xff, 0x67, 0xff, 0x69 };
Guenter Roeck279af1a2011-02-13 22:34:47 -0800210static const u16 W83627EHF_REG_FAN_STEP_OUTPUT_COMMON[]
Guenter Roeckc39aeda2010-08-14 21:08:55 +0200211 = { 0xff, 0x68, 0xff, 0x6a };
212
Guenter Roeck279af1a2011-02-13 22:34:47 -0800213static const u16 W83627EHF_REG_FAN_MAX_OUTPUT_W83667_B[] = { 0x67, 0x69, 0x6b };
214static const u16 W83627EHF_REG_FAN_STEP_OUTPUT_W83667_B[]
215 = { 0x68, 0x6a, 0x6c };
Rudolf Marek08c79952006-07-05 18:14:31 +0200216
Guenter Roeckd36cf322011-02-07 15:08:54 -0800217static const char *const w83667hg_b_temp_label[] = {
218 "SYSTIN",
219 "CPUTIN",
220 "AUXTIN",
221 "AMDTSI",
222 "PECI Agent 1",
223 "PECI Agent 2",
224 "PECI Agent 3",
225 "PECI Agent 4"
226};
227
228#define NUM_REG_TEMP 4
229
Guenter Roeckbce26c52011-02-04 12:54:14 -0800230static inline int is_word_sized(u16 reg)
231{
232 return (((reg & 0xff00) == 0x100
233 || (reg & 0xff00) == 0x200)
234 && ((reg & 0x00ff) == 0x50
235 || (reg & 0x00ff) == 0x53
236 || (reg & 0x00ff) == 0x55));
237}
238
Jean Delvare08e7e272005-04-25 22:43:25 +0200239/*
240 * Conversions
241 */
242
Rudolf Marek08c79952006-07-05 18:14:31 +0200243/* 1 is PWM mode, output in ms */
244static inline unsigned int step_time_from_reg(u8 reg, u8 mode)
245{
246 return mode ? 100 * reg : 400 * reg;
247}
248
249static inline u8 step_time_to_reg(unsigned int msec, u8 mode)
250{
251 return SENSORS_LIMIT((mode ? (msec + 50) / 100 :
252 (msec + 200) / 400), 1, 255);
253}
254
Jean Delvare08e7e272005-04-25 22:43:25 +0200255static inline unsigned int
256fan_from_reg(u8 reg, unsigned int div)
257{
258 if (reg == 0 || reg == 255)
259 return 0;
260 return 1350000U / (reg * div);
261}
262
263static inline unsigned int
264div_from_reg(u8 reg)
265{
266 return 1 << reg;
267}
268
269static inline int
Guenter Roeckbce26c52011-02-04 12:54:14 -0800270temp_from_reg(u16 reg, s16 regval)
Jean Delvare08e7e272005-04-25 22:43:25 +0200271{
Guenter Roeckbce26c52011-02-04 12:54:14 -0800272 if (is_word_sized(reg))
273 return LM75_TEMP_FROM_REG(regval);
274 return regval * 1000;
Jean Delvare08e7e272005-04-25 22:43:25 +0200275}
276
Guenter Roeckbce26c52011-02-04 12:54:14 -0800277static inline s16
278temp_to_reg(u16 reg, long temp)
Jean Delvare08e7e272005-04-25 22:43:25 +0200279{
Guenter Roeckbce26c52011-02-04 12:54:14 -0800280 if (is_word_sized(reg))
281 return LM75_TEMP_TO_REG(temp);
282 return DIV_ROUND_CLOSEST(SENSORS_LIMIT(temp, -127000, 128000), 1000);
Jean Delvare08e7e272005-04-25 22:43:25 +0200283}
284
Rudolf Marekcf0676f2006-03-23 16:25:22 +0100285/* Some of analog inputs have internal scaling (2x), 8mV is ADC LSB */
286
287static u8 scale_in[10] = { 8, 8, 16, 16, 8, 8, 8, 16, 16, 8 };
288
289static inline long in_from_reg(u8 reg, u8 nr)
290{
291 return reg * scale_in[nr];
292}
293
294static inline u8 in_to_reg(u32 val, u8 nr)
295{
Guenter Roecke7e1ca62011-02-04 13:24:30 -0800296 return SENSORS_LIMIT(((val + (scale_in[nr] / 2)) / scale_in[nr]), 0,
297 255);
Rudolf Marekcf0676f2006-03-23 16:25:22 +0100298}
299
Jean Delvare08e7e272005-04-25 22:43:25 +0200300/*
301 * Data structures and manipulation thereof
302 */
303
304struct w83627ehf_data {
David Hubbard1ea6dd32007-06-24 11:16:15 +0200305 int addr; /* IO base of hw monitor block */
306 const char *name;
307
Tony Jones1beeffe2007-08-20 13:46:20 -0700308 struct device *hwmon_dev;
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100309 struct mutex lock;
Jean Delvare08e7e272005-04-25 22:43:25 +0200310
Guenter Roeckd36cf322011-02-07 15:08:54 -0800311 u8 temp_src[NUM_REG_TEMP];
312 const char * const *temp_label;
313
Guenter Roeck279af1a2011-02-13 22:34:47 -0800314 const u16 *REG_PWM;
315 const u16 *REG_TARGET;
316 const u16 *REG_FAN;
317 const u16 *REG_FAN_MIN;
318 const u16 *REG_FAN_START_OUTPUT;
319 const u16 *REG_FAN_STOP_OUTPUT;
320 const u16 *REG_FAN_STOP_TIME;
321 const u16 *REG_FAN_MAX_OUTPUT;
322 const u16 *REG_FAN_STEP_OUTPUT;
Guenter Roeckda2e0252010-08-14 21:08:55 +0200323
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100324 struct mutex update_lock;
Jean Delvare08e7e272005-04-25 22:43:25 +0200325 char valid; /* !=0 if following fields are valid */
326 unsigned long last_updated; /* In jiffies */
327
328 /* Register values */
Guenter Roeck83cc8982011-02-06 08:10:15 -0800329 u8 bank; /* current register bank */
David Hubbard1ea6dd32007-06-24 11:16:15 +0200330 u8 in_num; /* number of in inputs we have */
Rudolf Marekcf0676f2006-03-23 16:25:22 +0100331 u8 in[10]; /* Register value */
332 u8 in_max[10]; /* Register value */
333 u8 in_min[10]; /* Register value */
Jean Delvare08e7e272005-04-25 22:43:25 +0200334 u8 fan[5];
335 u8 fan_min[5];
336 u8 fan_div[5];
337 u8 has_fan; /* some fan inputs can be disabled */
Jean Delvareda667362007-06-24 11:21:02 +0200338 u8 temp_type[3];
Guenter Roeckd36cf322011-02-07 15:08:54 -0800339 s16 temp[4];
340 s16 temp_max[4];
341 s16 temp_max_hyst[4];
Jean Delvarea4589db2006-03-23 16:30:29 +0100342 u32 alarms;
Rudolf Marek08c79952006-07-05 18:14:31 +0200343
344 u8 pwm_mode[4]; /* 0->DC variable voltage, 1->PWM variable duty cycle */
345 u8 pwm_enable[4]; /* 1->manual
Daniel J Blueman41e9a062009-12-14 18:01:37 -0800346 2->thermal cruise mode (also called SmartFan I)
347 3->fan speed cruise mode
Guenter Roecke7e1ca62011-02-04 13:24:30 -0800348 4->variable thermal cruise (also called
Guenter Roeckb84bb512011-02-13 23:01:25 -0800349 SmartFan III)
350 5->enhanced variable thermal cruise (also called
351 SmartFan IV) */
352 u8 pwm_enable_orig[4]; /* original value of pwm_enable */
Gong Jun237c8d2f2009-03-30 21:46:42 +0200353 u8 pwm_num; /* number of pwm */
Rudolf Marek08c79952006-07-05 18:14:31 +0200354 u8 pwm[4];
355 u8 target_temp[4];
356 u8 tolerance[4];
357
Daniel J Blueman41e9a062009-12-14 18:01:37 -0800358 u8 fan_start_output[4]; /* minimum fan speed when spinning up */
359 u8 fan_stop_output[4]; /* minimum fan speed when spinning down */
360 u8 fan_stop_time[4]; /* time at minimum before disabling fan */
361 u8 fan_max_output[4]; /* maximum fan speed */
362 u8 fan_step_output[4]; /* rate of change output value */
Jean Delvarefc18d6c2007-06-24 11:19:42 +0200363
364 u8 vid;
365 u8 vrm;
Gong Juna157d062009-03-30 21:46:43 +0200366
Guenter Roeckd36cf322011-02-07 15:08:54 -0800367 u8 have_temp;
Gong Juna157d062009-03-30 21:46:43 +0200368 u8 in6_skip;
Jean Delvare08e7e272005-04-25 22:43:25 +0200369};
370
David Hubbard1ea6dd32007-06-24 11:16:15 +0200371struct w83627ehf_sio_data {
372 int sioreg;
373 enum kinds kind;
374};
375
Guenter Roeck83cc8982011-02-06 08:10:15 -0800376/*
377 * On older chips, only registers 0x50-0x5f are banked.
378 * On more recent chips, all registers are banked.
379 * Assume that is the case and set the bank number for each access.
380 * Cache the bank number so it only needs to be set if it changes.
381 */
David Hubbard1ea6dd32007-06-24 11:16:15 +0200382static inline void w83627ehf_set_bank(struct w83627ehf_data *data, u16 reg)
Jean Delvare08e7e272005-04-25 22:43:25 +0200383{
Guenter Roeck83cc8982011-02-06 08:10:15 -0800384 u8 bank = reg >> 8;
385 if (data->bank != bank) {
David Hubbard1ea6dd32007-06-24 11:16:15 +0200386 outb_p(W83627EHF_REG_BANK, data->addr + ADDR_REG_OFFSET);
Guenter Roeck83cc8982011-02-06 08:10:15 -0800387 outb_p(bank, data->addr + DATA_REG_OFFSET);
388 data->bank = bank;
Jean Delvare08e7e272005-04-25 22:43:25 +0200389 }
390}
391
David Hubbard1ea6dd32007-06-24 11:16:15 +0200392static u16 w83627ehf_read_value(struct w83627ehf_data *data, u16 reg)
Jean Delvare08e7e272005-04-25 22:43:25 +0200393{
Jean Delvare08e7e272005-04-25 22:43:25 +0200394 int res, word_sized = is_word_sized(reg);
395
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100396 mutex_lock(&data->lock);
Jean Delvare08e7e272005-04-25 22:43:25 +0200397
David Hubbard1ea6dd32007-06-24 11:16:15 +0200398 w83627ehf_set_bank(data, reg);
399 outb_p(reg & 0xff, data->addr + ADDR_REG_OFFSET);
400 res = inb_p(data->addr + DATA_REG_OFFSET);
Jean Delvare08e7e272005-04-25 22:43:25 +0200401 if (word_sized) {
402 outb_p((reg & 0xff) + 1,
David Hubbard1ea6dd32007-06-24 11:16:15 +0200403 data->addr + ADDR_REG_OFFSET);
404 res = (res << 8) + inb_p(data->addr + DATA_REG_OFFSET);
Jean Delvare08e7e272005-04-25 22:43:25 +0200405 }
Jean Delvare08e7e272005-04-25 22:43:25 +0200406
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100407 mutex_unlock(&data->lock);
Jean Delvare08e7e272005-04-25 22:43:25 +0200408 return res;
409}
410
Guenter Roecke7e1ca62011-02-04 13:24:30 -0800411static int w83627ehf_write_value(struct w83627ehf_data *data, u16 reg,
412 u16 value)
Jean Delvare08e7e272005-04-25 22:43:25 +0200413{
Jean Delvare08e7e272005-04-25 22:43:25 +0200414 int word_sized = is_word_sized(reg);
415
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100416 mutex_lock(&data->lock);
Jean Delvare08e7e272005-04-25 22:43:25 +0200417
David Hubbard1ea6dd32007-06-24 11:16:15 +0200418 w83627ehf_set_bank(data, reg);
419 outb_p(reg & 0xff, data->addr + ADDR_REG_OFFSET);
Jean Delvare08e7e272005-04-25 22:43:25 +0200420 if (word_sized) {
David Hubbard1ea6dd32007-06-24 11:16:15 +0200421 outb_p(value >> 8, data->addr + DATA_REG_OFFSET);
Jean Delvare08e7e272005-04-25 22:43:25 +0200422 outb_p((reg & 0xff) + 1,
David Hubbard1ea6dd32007-06-24 11:16:15 +0200423 data->addr + ADDR_REG_OFFSET);
Jean Delvare08e7e272005-04-25 22:43:25 +0200424 }
David Hubbard1ea6dd32007-06-24 11:16:15 +0200425 outb_p(value & 0xff, data->addr + DATA_REG_OFFSET);
Jean Delvare08e7e272005-04-25 22:43:25 +0200426
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100427 mutex_unlock(&data->lock);
Jean Delvare08e7e272005-04-25 22:43:25 +0200428 return 0;
429}
430
431/* This function assumes that the caller holds data->update_lock */
David Hubbard1ea6dd32007-06-24 11:16:15 +0200432static void w83627ehf_write_fan_div(struct w83627ehf_data *data, int nr)
Jean Delvare08e7e272005-04-25 22:43:25 +0200433{
Jean Delvare08e7e272005-04-25 22:43:25 +0200434 u8 reg;
435
436 switch (nr) {
437 case 0:
David Hubbard1ea6dd32007-06-24 11:16:15 +0200438 reg = (w83627ehf_read_value(data, W83627EHF_REG_FANDIV1) & 0xcf)
Jean Delvare08e7e272005-04-25 22:43:25 +0200439 | ((data->fan_div[0] & 0x03) << 4);
Rudolf Marek14992c72006-10-08 22:02:09 +0200440 /* fan5 input control bit is write only, compute the value */
441 reg |= (data->has_fan & (1 << 4)) ? 1 : 0;
David Hubbard1ea6dd32007-06-24 11:16:15 +0200442 w83627ehf_write_value(data, W83627EHF_REG_FANDIV1, reg);
443 reg = (w83627ehf_read_value(data, W83627EHF_REG_VBAT) & 0xdf)
Jean Delvare08e7e272005-04-25 22:43:25 +0200444 | ((data->fan_div[0] & 0x04) << 3);
David Hubbard1ea6dd32007-06-24 11:16:15 +0200445 w83627ehf_write_value(data, W83627EHF_REG_VBAT, reg);
Jean Delvare08e7e272005-04-25 22:43:25 +0200446 break;
447 case 1:
David Hubbard1ea6dd32007-06-24 11:16:15 +0200448 reg = (w83627ehf_read_value(data, W83627EHF_REG_FANDIV1) & 0x3f)
Jean Delvare08e7e272005-04-25 22:43:25 +0200449 | ((data->fan_div[1] & 0x03) << 6);
Rudolf Marek14992c72006-10-08 22:02:09 +0200450 /* fan5 input control bit is write only, compute the value */
451 reg |= (data->has_fan & (1 << 4)) ? 1 : 0;
David Hubbard1ea6dd32007-06-24 11:16:15 +0200452 w83627ehf_write_value(data, W83627EHF_REG_FANDIV1, reg);
453 reg = (w83627ehf_read_value(data, W83627EHF_REG_VBAT) & 0xbf)
Jean Delvare08e7e272005-04-25 22:43:25 +0200454 | ((data->fan_div[1] & 0x04) << 4);
David Hubbard1ea6dd32007-06-24 11:16:15 +0200455 w83627ehf_write_value(data, W83627EHF_REG_VBAT, reg);
Jean Delvare08e7e272005-04-25 22:43:25 +0200456 break;
457 case 2:
David Hubbard1ea6dd32007-06-24 11:16:15 +0200458 reg = (w83627ehf_read_value(data, W83627EHF_REG_FANDIV2) & 0x3f)
Jean Delvare08e7e272005-04-25 22:43:25 +0200459 | ((data->fan_div[2] & 0x03) << 6);
David Hubbard1ea6dd32007-06-24 11:16:15 +0200460 w83627ehf_write_value(data, W83627EHF_REG_FANDIV2, reg);
461 reg = (w83627ehf_read_value(data, W83627EHF_REG_VBAT) & 0x7f)
Jean Delvare08e7e272005-04-25 22:43:25 +0200462 | ((data->fan_div[2] & 0x04) << 5);
David Hubbard1ea6dd32007-06-24 11:16:15 +0200463 w83627ehf_write_value(data, W83627EHF_REG_VBAT, reg);
Jean Delvare08e7e272005-04-25 22:43:25 +0200464 break;
465 case 3:
David Hubbard1ea6dd32007-06-24 11:16:15 +0200466 reg = (w83627ehf_read_value(data, W83627EHF_REG_DIODE) & 0xfc)
Jean Delvare08e7e272005-04-25 22:43:25 +0200467 | (data->fan_div[3] & 0x03);
David Hubbard1ea6dd32007-06-24 11:16:15 +0200468 w83627ehf_write_value(data, W83627EHF_REG_DIODE, reg);
469 reg = (w83627ehf_read_value(data, W83627EHF_REG_SMI_OVT) & 0x7f)
Jean Delvare08e7e272005-04-25 22:43:25 +0200470 | ((data->fan_div[3] & 0x04) << 5);
David Hubbard1ea6dd32007-06-24 11:16:15 +0200471 w83627ehf_write_value(data, W83627EHF_REG_SMI_OVT, reg);
Jean Delvare08e7e272005-04-25 22:43:25 +0200472 break;
473 case 4:
David Hubbard1ea6dd32007-06-24 11:16:15 +0200474 reg = (w83627ehf_read_value(data, W83627EHF_REG_DIODE) & 0x73)
Jean Delvare33725ad2007-04-17 00:32:27 -0700475 | ((data->fan_div[4] & 0x03) << 2)
Jean Delvare08e7e272005-04-25 22:43:25 +0200476 | ((data->fan_div[4] & 0x04) << 5);
David Hubbard1ea6dd32007-06-24 11:16:15 +0200477 w83627ehf_write_value(data, W83627EHF_REG_DIODE, reg);
Jean Delvare08e7e272005-04-25 22:43:25 +0200478 break;
479 }
480}
481
Mark M. Hoffmanea7be662007-08-05 12:19:01 -0400482static void w83627ehf_update_fan_div(struct w83627ehf_data *data)
483{
484 int i;
485
486 i = w83627ehf_read_value(data, W83627EHF_REG_FANDIV1);
487 data->fan_div[0] = (i >> 4) & 0x03;
488 data->fan_div[1] = (i >> 6) & 0x03;
489 i = w83627ehf_read_value(data, W83627EHF_REG_FANDIV2);
490 data->fan_div[2] = (i >> 6) & 0x03;
491 i = w83627ehf_read_value(data, W83627EHF_REG_VBAT);
492 data->fan_div[0] |= (i >> 3) & 0x04;
493 data->fan_div[1] |= (i >> 4) & 0x04;
494 data->fan_div[2] |= (i >> 5) & 0x04;
495 if (data->has_fan & ((1 << 3) | (1 << 4))) {
496 i = w83627ehf_read_value(data, W83627EHF_REG_DIODE);
497 data->fan_div[3] = i & 0x03;
498 data->fan_div[4] = ((i >> 2) & 0x03)
499 | ((i >> 5) & 0x04);
500 }
501 if (data->has_fan & (1 << 3)) {
502 i = w83627ehf_read_value(data, W83627EHF_REG_SMI_OVT);
503 data->fan_div[3] |= (i >> 5) & 0x04;
504 }
505}
506
Jean Delvare08e7e272005-04-25 22:43:25 +0200507static struct w83627ehf_data *w83627ehf_update_device(struct device *dev)
508{
David Hubbard1ea6dd32007-06-24 11:16:15 +0200509 struct w83627ehf_data *data = dev_get_drvdata(dev);
Rudolf Marek08c79952006-07-05 18:14:31 +0200510 int pwmcfg = 0, tolerance = 0; /* shut up the compiler */
Jean Delvare08e7e272005-04-25 22:43:25 +0200511 int i;
512
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100513 mutex_lock(&data->update_lock);
Jean Delvare08e7e272005-04-25 22:43:25 +0200514
Jean Delvare6b3e4642007-06-24 11:19:01 +0200515 if (time_after(jiffies, data->last_updated + HZ + HZ/2)
Jean Delvare08e7e272005-04-25 22:43:25 +0200516 || !data->valid) {
517 /* Fan clock dividers */
Mark M. Hoffmanea7be662007-08-05 12:19:01 -0400518 w83627ehf_update_fan_div(data);
Jean Delvare08e7e272005-04-25 22:43:25 +0200519
Rudolf Marekcf0676f2006-03-23 16:25:22 +0100520 /* Measured voltages and limits */
David Hubbard1ea6dd32007-06-24 11:16:15 +0200521 for (i = 0; i < data->in_num; i++) {
522 data->in[i] = w83627ehf_read_value(data,
Rudolf Marekcf0676f2006-03-23 16:25:22 +0100523 W83627EHF_REG_IN(i));
David Hubbard1ea6dd32007-06-24 11:16:15 +0200524 data->in_min[i] = w83627ehf_read_value(data,
Rudolf Marekcf0676f2006-03-23 16:25:22 +0100525 W83627EHF_REG_IN_MIN(i));
David Hubbard1ea6dd32007-06-24 11:16:15 +0200526 data->in_max[i] = w83627ehf_read_value(data,
Rudolf Marekcf0676f2006-03-23 16:25:22 +0100527 W83627EHF_REG_IN_MAX(i));
528 }
529
Jean Delvare08e7e272005-04-25 22:43:25 +0200530 /* Measured fan speeds and limits */
531 for (i = 0; i < 5; i++) {
532 if (!(data->has_fan & (1 << i)))
533 continue;
534
David Hubbard1ea6dd32007-06-24 11:16:15 +0200535 data->fan[i] = w83627ehf_read_value(data,
Guenter Roeck279af1a2011-02-13 22:34:47 -0800536 data->REG_FAN[i]);
David Hubbard1ea6dd32007-06-24 11:16:15 +0200537 data->fan_min[i] = w83627ehf_read_value(data,
Guenter Roeck279af1a2011-02-13 22:34:47 -0800538 data->REG_FAN_MIN[i]);
Jean Delvare08e7e272005-04-25 22:43:25 +0200539
540 /* If we failed to measure the fan speed and clock
541 divider can be increased, let's try that for next
542 time */
543 if (data->fan[i] == 0xff
544 && data->fan_div[i] < 0x07) {
Guenter Roecke7e1ca62011-02-04 13:24:30 -0800545 dev_dbg(dev, "Increasing fan%d "
Jean Delvare08e7e272005-04-25 22:43:25 +0200546 "clock divider from %u to %u\n",
Jean Delvare33725ad2007-04-17 00:32:27 -0700547 i + 1, div_from_reg(data->fan_div[i]),
Jean Delvare08e7e272005-04-25 22:43:25 +0200548 div_from_reg(data->fan_div[i] + 1));
549 data->fan_div[i]++;
David Hubbard1ea6dd32007-06-24 11:16:15 +0200550 w83627ehf_write_fan_div(data, i);
Jean Delvare08e7e272005-04-25 22:43:25 +0200551 /* Preserve min limit if possible */
552 if (data->fan_min[i] >= 2
553 && data->fan_min[i] != 255)
David Hubbard1ea6dd32007-06-24 11:16:15 +0200554 w83627ehf_write_value(data,
Guenter Roeck279af1a2011-02-13 22:34:47 -0800555 data->REG_FAN_MIN[i],
Jean Delvare08e7e272005-04-25 22:43:25 +0200556 (data->fan_min[i] /= 2));
557 }
558 }
559
Guenter Roeckda2e0252010-08-14 21:08:55 +0200560 for (i = 0; i < data->pwm_num; i++) {
561 if (!(data->has_fan & (1 << i)))
562 continue;
563
Jean Delvare77fa49d2009-01-07 16:37:35 +0100564 /* pwmcfg, tolerance mapped for i=0, i=1 to same reg */
Rudolf Marek08c79952006-07-05 18:14:31 +0200565 if (i != 1) {
David Hubbard1ea6dd32007-06-24 11:16:15 +0200566 pwmcfg = w83627ehf_read_value(data,
Rudolf Marek08c79952006-07-05 18:14:31 +0200567 W83627EHF_REG_PWM_ENABLE[i]);
David Hubbard1ea6dd32007-06-24 11:16:15 +0200568 tolerance = w83627ehf_read_value(data,
Rudolf Marek08c79952006-07-05 18:14:31 +0200569 W83627EHF_REG_TOLERANCE[i]);
570 }
571 data->pwm_mode[i] =
572 ((pwmcfg >> W83627EHF_PWM_MODE_SHIFT[i]) & 1)
573 ? 0 : 1;
574 data->pwm_enable[i] =
Guenter Roecke7e1ca62011-02-04 13:24:30 -0800575 ((pwmcfg >> W83627EHF_PWM_ENABLE_SHIFT[i])
576 & 3) + 1;
David Hubbard1ea6dd32007-06-24 11:16:15 +0200577 data->pwm[i] = w83627ehf_read_value(data,
Guenter Roeck279af1a2011-02-13 22:34:47 -0800578 data->REG_PWM[i]);
Daniel J Blueman41e9a062009-12-14 18:01:37 -0800579 data->fan_start_output[i] = w83627ehf_read_value(data,
Guenter Roeck279af1a2011-02-13 22:34:47 -0800580 data->REG_FAN_START_OUTPUT[i]);
Daniel J Blueman41e9a062009-12-14 18:01:37 -0800581 data->fan_stop_output[i] = w83627ehf_read_value(data,
Guenter Roeck279af1a2011-02-13 22:34:47 -0800582 data->REG_FAN_STOP_OUTPUT[i]);
David Hubbard1ea6dd32007-06-24 11:16:15 +0200583 data->fan_stop_time[i] = w83627ehf_read_value(data,
Guenter Roeck279af1a2011-02-13 22:34:47 -0800584 data->REG_FAN_STOP_TIME[i]);
Guenter Roeckda2e0252010-08-14 21:08:55 +0200585
586 if (data->REG_FAN_MAX_OUTPUT[i] != 0xff)
587 data->fan_max_output[i] =
588 w83627ehf_read_value(data,
589 data->REG_FAN_MAX_OUTPUT[i]);
590
591 if (data->REG_FAN_STEP_OUTPUT[i] != 0xff)
592 data->fan_step_output[i] =
593 w83627ehf_read_value(data,
594 data->REG_FAN_STEP_OUTPUT[i]);
595
Rudolf Marek08c79952006-07-05 18:14:31 +0200596 data->target_temp[i] =
David Hubbard1ea6dd32007-06-24 11:16:15 +0200597 w83627ehf_read_value(data,
Guenter Roeck279af1a2011-02-13 22:34:47 -0800598 data->REG_TARGET[i]) &
Rudolf Marek08c79952006-07-05 18:14:31 +0200599 (data->pwm_mode[i] == 1 ? 0x7f : 0xff);
600 data->tolerance[i] = (tolerance >> (i == 1 ? 4 : 0))
601 & 0x0f;
602 }
603
Jean Delvare08e7e272005-04-25 22:43:25 +0200604 /* Measured temperatures and limits */
Guenter Roeckd36cf322011-02-07 15:08:54 -0800605 for (i = 0; i < NUM_REG_TEMP; i++) {
606 if (!(data->have_temp & (1 << i)))
607 continue;
608 data->temp[i]
609 = w83627ehf_read_value(data, W83627EHF_REG_TEMP[i]);
610 if (i > 2)
611 break;
612 data->temp_max[i]
613 = w83627ehf_read_value(data,
614 W83627EHF_REG_TEMP_OVER[i]);
615 data->temp_max_hyst[i]
616 = w83627ehf_read_value(data,
Jean Delvare08e7e272005-04-25 22:43:25 +0200617 W83627EHF_REG_TEMP_HYST[i]);
618 }
619
David Hubbard1ea6dd32007-06-24 11:16:15 +0200620 data->alarms = w83627ehf_read_value(data,
Jean Delvarea4589db2006-03-23 16:30:29 +0100621 W83627EHF_REG_ALARM1) |
David Hubbard1ea6dd32007-06-24 11:16:15 +0200622 (w83627ehf_read_value(data,
Jean Delvarea4589db2006-03-23 16:30:29 +0100623 W83627EHF_REG_ALARM2) << 8) |
David Hubbard1ea6dd32007-06-24 11:16:15 +0200624 (w83627ehf_read_value(data,
Jean Delvarea4589db2006-03-23 16:30:29 +0100625 W83627EHF_REG_ALARM3) << 16);
626
Jean Delvare08e7e272005-04-25 22:43:25 +0200627 data->last_updated = jiffies;
628 data->valid = 1;
629 }
630
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100631 mutex_unlock(&data->update_lock);
Jean Delvare08e7e272005-04-25 22:43:25 +0200632 return data;
633}
634
635/*
636 * Sysfs callback functions
637 */
Rudolf Marekcf0676f2006-03-23 16:25:22 +0100638#define show_in_reg(reg) \
639static ssize_t \
640show_##reg(struct device *dev, struct device_attribute *attr, \
641 char *buf) \
642{ \
643 struct w83627ehf_data *data = w83627ehf_update_device(dev); \
Guenter Roecke7e1ca62011-02-04 13:24:30 -0800644 struct sensor_device_attribute *sensor_attr = \
645 to_sensor_dev_attr(attr); \
Rudolf Marekcf0676f2006-03-23 16:25:22 +0100646 int nr = sensor_attr->index; \
647 return sprintf(buf, "%ld\n", in_from_reg(data->reg[nr], nr)); \
648}
649show_in_reg(in)
650show_in_reg(in_min)
651show_in_reg(in_max)
652
653#define store_in_reg(REG, reg) \
654static ssize_t \
Guenter Roecke7e1ca62011-02-04 13:24:30 -0800655store_in_##reg(struct device *dev, struct device_attribute *attr, \
656 const char *buf, size_t count) \
Rudolf Marekcf0676f2006-03-23 16:25:22 +0100657{ \
David Hubbard1ea6dd32007-06-24 11:16:15 +0200658 struct w83627ehf_data *data = dev_get_drvdata(dev); \
Guenter Roecke7e1ca62011-02-04 13:24:30 -0800659 struct sensor_device_attribute *sensor_attr = \
660 to_sensor_dev_attr(attr); \
Rudolf Marekcf0676f2006-03-23 16:25:22 +0100661 int nr = sensor_attr->index; \
Guenter Roeckbce26c52011-02-04 12:54:14 -0800662 unsigned long val; \
663 int err; \
664 err = strict_strtoul(buf, 10, &val); \
665 if (err < 0) \
666 return err; \
Rudolf Marekcf0676f2006-03-23 16:25:22 +0100667 mutex_lock(&data->update_lock); \
668 data->in_##reg[nr] = in_to_reg(val, nr); \
David Hubbard1ea6dd32007-06-24 11:16:15 +0200669 w83627ehf_write_value(data, W83627EHF_REG_IN_##REG(nr), \
Rudolf Marekcf0676f2006-03-23 16:25:22 +0100670 data->in_##reg[nr]); \
671 mutex_unlock(&data->update_lock); \
672 return count; \
673}
674
675store_in_reg(MIN, min)
676store_in_reg(MAX, max)
677
Guenter Roecke7e1ca62011-02-04 13:24:30 -0800678static ssize_t show_alarm(struct device *dev, struct device_attribute *attr,
679 char *buf)
Jean Delvarea4589db2006-03-23 16:30:29 +0100680{
681 struct w83627ehf_data *data = w83627ehf_update_device(dev);
682 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
683 int nr = sensor_attr->index;
684 return sprintf(buf, "%u\n", (data->alarms >> nr) & 0x01);
685}
686
Rudolf Marekcf0676f2006-03-23 16:25:22 +0100687static struct sensor_device_attribute sda_in_input[] = {
688 SENSOR_ATTR(in0_input, S_IRUGO, show_in, NULL, 0),
689 SENSOR_ATTR(in1_input, S_IRUGO, show_in, NULL, 1),
690 SENSOR_ATTR(in2_input, S_IRUGO, show_in, NULL, 2),
691 SENSOR_ATTR(in3_input, S_IRUGO, show_in, NULL, 3),
692 SENSOR_ATTR(in4_input, S_IRUGO, show_in, NULL, 4),
693 SENSOR_ATTR(in5_input, S_IRUGO, show_in, NULL, 5),
694 SENSOR_ATTR(in6_input, S_IRUGO, show_in, NULL, 6),
695 SENSOR_ATTR(in7_input, S_IRUGO, show_in, NULL, 7),
696 SENSOR_ATTR(in8_input, S_IRUGO, show_in, NULL, 8),
697 SENSOR_ATTR(in9_input, S_IRUGO, show_in, NULL, 9),
698};
699
Jean Delvarea4589db2006-03-23 16:30:29 +0100700static struct sensor_device_attribute sda_in_alarm[] = {
701 SENSOR_ATTR(in0_alarm, S_IRUGO, show_alarm, NULL, 0),
702 SENSOR_ATTR(in1_alarm, S_IRUGO, show_alarm, NULL, 1),
703 SENSOR_ATTR(in2_alarm, S_IRUGO, show_alarm, NULL, 2),
704 SENSOR_ATTR(in3_alarm, S_IRUGO, show_alarm, NULL, 3),
705 SENSOR_ATTR(in4_alarm, S_IRUGO, show_alarm, NULL, 8),
706 SENSOR_ATTR(in5_alarm, S_IRUGO, show_alarm, NULL, 21),
707 SENSOR_ATTR(in6_alarm, S_IRUGO, show_alarm, NULL, 20),
708 SENSOR_ATTR(in7_alarm, S_IRUGO, show_alarm, NULL, 16),
709 SENSOR_ATTR(in8_alarm, S_IRUGO, show_alarm, NULL, 17),
710 SENSOR_ATTR(in9_alarm, S_IRUGO, show_alarm, NULL, 19),
711};
712
Rudolf Marekcf0676f2006-03-23 16:25:22 +0100713static struct sensor_device_attribute sda_in_min[] = {
Guenter Roecke7e1ca62011-02-04 13:24:30 -0800714 SENSOR_ATTR(in0_min, S_IWUSR | S_IRUGO, show_in_min, store_in_min, 0),
715 SENSOR_ATTR(in1_min, S_IWUSR | S_IRUGO, show_in_min, store_in_min, 1),
716 SENSOR_ATTR(in2_min, S_IWUSR | S_IRUGO, show_in_min, store_in_min, 2),
717 SENSOR_ATTR(in3_min, S_IWUSR | S_IRUGO, show_in_min, store_in_min, 3),
718 SENSOR_ATTR(in4_min, S_IWUSR | S_IRUGO, show_in_min, store_in_min, 4),
719 SENSOR_ATTR(in5_min, S_IWUSR | S_IRUGO, show_in_min, store_in_min, 5),
720 SENSOR_ATTR(in6_min, S_IWUSR | S_IRUGO, show_in_min, store_in_min, 6),
721 SENSOR_ATTR(in7_min, S_IWUSR | S_IRUGO, show_in_min, store_in_min, 7),
722 SENSOR_ATTR(in8_min, S_IWUSR | S_IRUGO, show_in_min, store_in_min, 8),
723 SENSOR_ATTR(in9_min, S_IWUSR | S_IRUGO, show_in_min, store_in_min, 9),
Rudolf Marekcf0676f2006-03-23 16:25:22 +0100724};
725
726static struct sensor_device_attribute sda_in_max[] = {
Guenter Roecke7e1ca62011-02-04 13:24:30 -0800727 SENSOR_ATTR(in0_max, S_IWUSR | S_IRUGO, show_in_max, store_in_max, 0),
728 SENSOR_ATTR(in1_max, S_IWUSR | S_IRUGO, show_in_max, store_in_max, 1),
729 SENSOR_ATTR(in2_max, S_IWUSR | S_IRUGO, show_in_max, store_in_max, 2),
730 SENSOR_ATTR(in3_max, S_IWUSR | S_IRUGO, show_in_max, store_in_max, 3),
731 SENSOR_ATTR(in4_max, S_IWUSR | S_IRUGO, show_in_max, store_in_max, 4),
732 SENSOR_ATTR(in5_max, S_IWUSR | S_IRUGO, show_in_max, store_in_max, 5),
733 SENSOR_ATTR(in6_max, S_IWUSR | S_IRUGO, show_in_max, store_in_max, 6),
734 SENSOR_ATTR(in7_max, S_IWUSR | S_IRUGO, show_in_max, store_in_max, 7),
735 SENSOR_ATTR(in8_max, S_IWUSR | S_IRUGO, show_in_max, store_in_max, 8),
736 SENSOR_ATTR(in9_max, S_IWUSR | S_IRUGO, show_in_max, store_in_max, 9),
Rudolf Marekcf0676f2006-03-23 16:25:22 +0100737};
738
Jean Delvare08e7e272005-04-25 22:43:25 +0200739#define show_fan_reg(reg) \
740static ssize_t \
Yuan Mu412fec82006-02-05 23:24:16 +0100741show_##reg(struct device *dev, struct device_attribute *attr, \
742 char *buf) \
Jean Delvare08e7e272005-04-25 22:43:25 +0200743{ \
744 struct w83627ehf_data *data = w83627ehf_update_device(dev); \
Guenter Roecke7e1ca62011-02-04 13:24:30 -0800745 struct sensor_device_attribute *sensor_attr = \
746 to_sensor_dev_attr(attr); \
Yuan Mu412fec82006-02-05 23:24:16 +0100747 int nr = sensor_attr->index; \
Jean Delvare08e7e272005-04-25 22:43:25 +0200748 return sprintf(buf, "%d\n", \
749 fan_from_reg(data->reg[nr], \
750 div_from_reg(data->fan_div[nr]))); \
751}
752show_fan_reg(fan);
753show_fan_reg(fan_min);
754
755static ssize_t
Yuan Mu412fec82006-02-05 23:24:16 +0100756show_fan_div(struct device *dev, struct device_attribute *attr,
757 char *buf)
Jean Delvare08e7e272005-04-25 22:43:25 +0200758{
759 struct w83627ehf_data *data = w83627ehf_update_device(dev);
Yuan Mu412fec82006-02-05 23:24:16 +0100760 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
761 int nr = sensor_attr->index;
762 return sprintf(buf, "%u\n", div_from_reg(data->fan_div[nr]));
Jean Delvare08e7e272005-04-25 22:43:25 +0200763}
764
765static ssize_t
Yuan Mu412fec82006-02-05 23:24:16 +0100766store_fan_min(struct device *dev, struct device_attribute *attr,
767 const char *buf, size_t count)
Jean Delvare08e7e272005-04-25 22:43:25 +0200768{
David Hubbard1ea6dd32007-06-24 11:16:15 +0200769 struct w83627ehf_data *data = dev_get_drvdata(dev);
Yuan Mu412fec82006-02-05 23:24:16 +0100770 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
771 int nr = sensor_attr->index;
Guenter Roeckbce26c52011-02-04 12:54:14 -0800772 unsigned long val;
773 int err;
Jean Delvare08e7e272005-04-25 22:43:25 +0200774 unsigned int reg;
775 u8 new_div;
776
Guenter Roeckbce26c52011-02-04 12:54:14 -0800777 err = strict_strtoul(buf, 10, &val);
778 if (err < 0)
779 return err;
780
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100781 mutex_lock(&data->update_lock);
Jean Delvare08e7e272005-04-25 22:43:25 +0200782 if (!val) {
783 /* No min limit, alarm disabled */
784 data->fan_min[nr] = 255;
785 new_div = data->fan_div[nr]; /* No change */
786 dev_info(dev, "fan%u low limit and alarm disabled\n", nr + 1);
787 } else if ((reg = 1350000U / val) >= 128 * 255) {
788 /* Speed below this value cannot possibly be represented,
789 even with the highest divider (128) */
790 data->fan_min[nr] = 254;
791 new_div = 7; /* 128 == (1 << 7) */
Guenter Roeckbce26c52011-02-04 12:54:14 -0800792 dev_warn(dev, "fan%u low limit %lu below minimum %u, set to "
Jean Delvare08e7e272005-04-25 22:43:25 +0200793 "minimum\n", nr + 1, val, fan_from_reg(254, 128));
794 } else if (!reg) {
795 /* Speed above this value cannot possibly be represented,
796 even with the lowest divider (1) */
797 data->fan_min[nr] = 1;
798 new_div = 0; /* 1 == (1 << 0) */
Guenter Roeckbce26c52011-02-04 12:54:14 -0800799 dev_warn(dev, "fan%u low limit %lu above maximum %u, set to "
Jean Delvareb9110b12005-05-02 23:08:22 +0200800 "maximum\n", nr + 1, val, fan_from_reg(1, 1));
Jean Delvare08e7e272005-04-25 22:43:25 +0200801 } else {
802 /* Automatically pick the best divider, i.e. the one such
803 that the min limit will correspond to a register value
804 in the 96..192 range */
805 new_div = 0;
806 while (reg > 192 && new_div < 7) {
807 reg >>= 1;
808 new_div++;
809 }
810 data->fan_min[nr] = reg;
811 }
812
813 /* Write both the fan clock divider (if it changed) and the new
814 fan min (unconditionally) */
815 if (new_div != data->fan_div[nr]) {
Jean Delvare158ce072007-06-17 16:09:12 +0200816 /* Preserve the fan speed reading */
817 if (data->fan[nr] != 0xff) {
818 if (new_div > data->fan_div[nr])
819 data->fan[nr] >>= new_div - data->fan_div[nr];
820 else if (data->fan[nr] & 0x80)
821 data->fan[nr] = 0xff;
822 else
823 data->fan[nr] <<= data->fan_div[nr] - new_div;
824 }
Jean Delvare08e7e272005-04-25 22:43:25 +0200825
826 dev_dbg(dev, "fan%u clock divider changed from %u to %u\n",
827 nr + 1, div_from_reg(data->fan_div[nr]),
828 div_from_reg(new_div));
829 data->fan_div[nr] = new_div;
David Hubbard1ea6dd32007-06-24 11:16:15 +0200830 w83627ehf_write_fan_div(data, nr);
Jean Delvare6b3e4642007-06-24 11:19:01 +0200831 /* Give the chip time to sample a new speed value */
832 data->last_updated = jiffies;
Jean Delvare08e7e272005-04-25 22:43:25 +0200833 }
Guenter Roeck279af1a2011-02-13 22:34:47 -0800834 w83627ehf_write_value(data, data->REG_FAN_MIN[nr],
Jean Delvare08e7e272005-04-25 22:43:25 +0200835 data->fan_min[nr]);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100836 mutex_unlock(&data->update_lock);
Jean Delvare08e7e272005-04-25 22:43:25 +0200837
838 return count;
839}
840
Yuan Mu412fec82006-02-05 23:24:16 +0100841static struct sensor_device_attribute sda_fan_input[] = {
842 SENSOR_ATTR(fan1_input, S_IRUGO, show_fan, NULL, 0),
843 SENSOR_ATTR(fan2_input, S_IRUGO, show_fan, NULL, 1),
844 SENSOR_ATTR(fan3_input, S_IRUGO, show_fan, NULL, 2),
845 SENSOR_ATTR(fan4_input, S_IRUGO, show_fan, NULL, 3),
846 SENSOR_ATTR(fan5_input, S_IRUGO, show_fan, NULL, 4),
847};
Jean Delvare08e7e272005-04-25 22:43:25 +0200848
Jean Delvarea4589db2006-03-23 16:30:29 +0100849static struct sensor_device_attribute sda_fan_alarm[] = {
850 SENSOR_ATTR(fan1_alarm, S_IRUGO, show_alarm, NULL, 6),
851 SENSOR_ATTR(fan2_alarm, S_IRUGO, show_alarm, NULL, 7),
852 SENSOR_ATTR(fan3_alarm, S_IRUGO, show_alarm, NULL, 11),
853 SENSOR_ATTR(fan4_alarm, S_IRUGO, show_alarm, NULL, 10),
854 SENSOR_ATTR(fan5_alarm, S_IRUGO, show_alarm, NULL, 23),
855};
856
Yuan Mu412fec82006-02-05 23:24:16 +0100857static struct sensor_device_attribute sda_fan_min[] = {
858 SENSOR_ATTR(fan1_min, S_IWUSR | S_IRUGO, show_fan_min,
859 store_fan_min, 0),
860 SENSOR_ATTR(fan2_min, S_IWUSR | S_IRUGO, show_fan_min,
861 store_fan_min, 1),
862 SENSOR_ATTR(fan3_min, S_IWUSR | S_IRUGO, show_fan_min,
863 store_fan_min, 2),
864 SENSOR_ATTR(fan4_min, S_IWUSR | S_IRUGO, show_fan_min,
865 store_fan_min, 3),
866 SENSOR_ATTR(fan5_min, S_IWUSR | S_IRUGO, show_fan_min,
867 store_fan_min, 4),
868};
Jean Delvare08e7e272005-04-25 22:43:25 +0200869
Yuan Mu412fec82006-02-05 23:24:16 +0100870static struct sensor_device_attribute sda_fan_div[] = {
871 SENSOR_ATTR(fan1_div, S_IRUGO, show_fan_div, NULL, 0),
872 SENSOR_ATTR(fan2_div, S_IRUGO, show_fan_div, NULL, 1),
873 SENSOR_ATTR(fan3_div, S_IRUGO, show_fan_div, NULL, 2),
874 SENSOR_ATTR(fan4_div, S_IRUGO, show_fan_div, NULL, 3),
875 SENSOR_ATTR(fan5_div, S_IRUGO, show_fan_div, NULL, 4),
876};
Jean Delvare08e7e272005-04-25 22:43:25 +0200877
Guenter Roeckd36cf322011-02-07 15:08:54 -0800878static ssize_t
879show_temp_label(struct device *dev, struct device_attribute *attr, char *buf)
880{
881 struct w83627ehf_data *data = w83627ehf_update_device(dev);
882 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
883 int nr = sensor_attr->index;
884 return sprintf(buf, "%s\n", data->temp_label[data->temp_src[nr]]);
885}
886
Guenter Roeckbce26c52011-02-04 12:54:14 -0800887#define show_temp_reg(REG, reg) \
Jean Delvare08e7e272005-04-25 22:43:25 +0200888static ssize_t \
Yuan Mu412fec82006-02-05 23:24:16 +0100889show_##reg(struct device *dev, struct device_attribute *attr, \
890 char *buf) \
Jean Delvare08e7e272005-04-25 22:43:25 +0200891{ \
892 struct w83627ehf_data *data = w83627ehf_update_device(dev); \
Guenter Roecke7e1ca62011-02-04 13:24:30 -0800893 struct sensor_device_attribute *sensor_attr = \
894 to_sensor_dev_attr(attr); \
Yuan Mu412fec82006-02-05 23:24:16 +0100895 int nr = sensor_attr->index; \
Jean Delvare08e7e272005-04-25 22:43:25 +0200896 return sprintf(buf, "%d\n", \
Guenter Roeckbce26c52011-02-04 12:54:14 -0800897 temp_from_reg(W83627EHF_REG_##REG[nr], data->reg[nr])); \
Jean Delvare08e7e272005-04-25 22:43:25 +0200898}
Guenter Roeckbce26c52011-02-04 12:54:14 -0800899show_temp_reg(TEMP, temp);
900show_temp_reg(TEMP_OVER, temp_max);
901show_temp_reg(TEMP_HYST, temp_max_hyst);
Jean Delvare08e7e272005-04-25 22:43:25 +0200902
903#define store_temp_reg(REG, reg) \
904static ssize_t \
Yuan Mu412fec82006-02-05 23:24:16 +0100905store_##reg(struct device *dev, struct device_attribute *attr, \
906 const char *buf, size_t count) \
Jean Delvare08e7e272005-04-25 22:43:25 +0200907{ \
David Hubbard1ea6dd32007-06-24 11:16:15 +0200908 struct w83627ehf_data *data = dev_get_drvdata(dev); \
Guenter Roecke7e1ca62011-02-04 13:24:30 -0800909 struct sensor_device_attribute *sensor_attr = \
910 to_sensor_dev_attr(attr); \
Yuan Mu412fec82006-02-05 23:24:16 +0100911 int nr = sensor_attr->index; \
Guenter Roeckbce26c52011-02-04 12:54:14 -0800912 int err; \
913 long val; \
914 err = strict_strtol(buf, 10, &val); \
915 if (err < 0) \
916 return err; \
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100917 mutex_lock(&data->update_lock); \
Guenter Roeckbce26c52011-02-04 12:54:14 -0800918 data->reg[nr] = temp_to_reg(W83627EHF_REG_TEMP_##REG[nr], val); \
David Hubbard1ea6dd32007-06-24 11:16:15 +0200919 w83627ehf_write_value(data, W83627EHF_REG_TEMP_##REG[nr], \
Jean Delvare08e7e272005-04-25 22:43:25 +0200920 data->reg[nr]); \
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100921 mutex_unlock(&data->update_lock); \
Jean Delvare08e7e272005-04-25 22:43:25 +0200922 return count; \
923}
924store_temp_reg(OVER, temp_max);
925store_temp_reg(HYST, temp_max_hyst);
926
Jean Delvareda667362007-06-24 11:21:02 +0200927static ssize_t
928show_temp_type(struct device *dev, struct device_attribute *attr, char *buf)
929{
930 struct w83627ehf_data *data = w83627ehf_update_device(dev);
931 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
932 int nr = sensor_attr->index;
933 return sprintf(buf, "%d\n", (int)data->temp_type[nr]);
934}
935
Gong Juna157d062009-03-30 21:46:43 +0200936static struct sensor_device_attribute sda_temp_input[] = {
Guenter Roeckbce26c52011-02-04 12:54:14 -0800937 SENSOR_ATTR(temp1_input, S_IRUGO, show_temp, NULL, 0),
938 SENSOR_ATTR(temp2_input, S_IRUGO, show_temp, NULL, 1),
939 SENSOR_ATTR(temp3_input, S_IRUGO, show_temp, NULL, 2),
Guenter Roeckd36cf322011-02-07 15:08:54 -0800940 SENSOR_ATTR(temp4_input, S_IRUGO, show_temp, NULL, 3),
941};
942
943static struct sensor_device_attribute sda_temp_label[] = {
944 SENSOR_ATTR(temp1_label, S_IRUGO, show_temp_label, NULL, 0),
945 SENSOR_ATTR(temp2_label, S_IRUGO, show_temp_label, NULL, 1),
946 SENSOR_ATTR(temp3_label, S_IRUGO, show_temp_label, NULL, 2),
947 SENSOR_ATTR(temp4_label, S_IRUGO, show_temp_label, NULL, 3),
Gong Juna157d062009-03-30 21:46:43 +0200948};
949
950static struct sensor_device_attribute sda_temp_max[] = {
Guenter Roeckbce26c52011-02-04 12:54:14 -0800951 SENSOR_ATTR(temp1_max, S_IRUGO | S_IWUSR, show_temp_max,
Yuan Mu412fec82006-02-05 23:24:16 +0100952 store_temp_max, 0),
Guenter Roeckbce26c52011-02-04 12:54:14 -0800953 SENSOR_ATTR(temp2_max, S_IRUGO | S_IWUSR, show_temp_max,
Yuan Mu412fec82006-02-05 23:24:16 +0100954 store_temp_max, 1),
Guenter Roeckbce26c52011-02-04 12:54:14 -0800955 SENSOR_ATTR(temp3_max, S_IRUGO | S_IWUSR, show_temp_max,
956 store_temp_max, 2),
Gong Juna157d062009-03-30 21:46:43 +0200957};
958
959static struct sensor_device_attribute sda_temp_max_hyst[] = {
Guenter Roeckbce26c52011-02-04 12:54:14 -0800960 SENSOR_ATTR(temp1_max_hyst, S_IRUGO | S_IWUSR, show_temp_max_hyst,
Yuan Mu412fec82006-02-05 23:24:16 +0100961 store_temp_max_hyst, 0),
Guenter Roeckbce26c52011-02-04 12:54:14 -0800962 SENSOR_ATTR(temp2_max_hyst, S_IRUGO | S_IWUSR, show_temp_max_hyst,
Yuan Mu412fec82006-02-05 23:24:16 +0100963 store_temp_max_hyst, 1),
Guenter Roeckbce26c52011-02-04 12:54:14 -0800964 SENSOR_ATTR(temp3_max_hyst, S_IRUGO | S_IWUSR, show_temp_max_hyst,
965 store_temp_max_hyst, 2),
Gong Juna157d062009-03-30 21:46:43 +0200966};
967
968static struct sensor_device_attribute sda_temp_alarm[] = {
Jean Delvarea4589db2006-03-23 16:30:29 +0100969 SENSOR_ATTR(temp1_alarm, S_IRUGO, show_alarm, NULL, 4),
970 SENSOR_ATTR(temp2_alarm, S_IRUGO, show_alarm, NULL, 5),
971 SENSOR_ATTR(temp3_alarm, S_IRUGO, show_alarm, NULL, 13),
Gong Juna157d062009-03-30 21:46:43 +0200972};
973
974static struct sensor_device_attribute sda_temp_type[] = {
Jean Delvareda667362007-06-24 11:21:02 +0200975 SENSOR_ATTR(temp1_type, S_IRUGO, show_temp_type, NULL, 0),
976 SENSOR_ATTR(temp2_type, S_IRUGO, show_temp_type, NULL, 1),
977 SENSOR_ATTR(temp3_type, S_IRUGO, show_temp_type, NULL, 2),
Yuan Mu412fec82006-02-05 23:24:16 +0100978};
Jean Delvare08e7e272005-04-25 22:43:25 +0200979
Rudolf Marek08c79952006-07-05 18:14:31 +0200980#define show_pwm_reg(reg) \
Guenter Roecke7e1ca62011-02-04 13:24:30 -0800981static ssize_t show_##reg(struct device *dev, struct device_attribute *attr, \
982 char *buf) \
Rudolf Marek08c79952006-07-05 18:14:31 +0200983{ \
984 struct w83627ehf_data *data = w83627ehf_update_device(dev); \
Guenter Roecke7e1ca62011-02-04 13:24:30 -0800985 struct sensor_device_attribute *sensor_attr = \
986 to_sensor_dev_attr(attr); \
Rudolf Marek08c79952006-07-05 18:14:31 +0200987 int nr = sensor_attr->index; \
988 return sprintf(buf, "%d\n", data->reg[nr]); \
989}
990
991show_pwm_reg(pwm_mode)
992show_pwm_reg(pwm_enable)
993show_pwm_reg(pwm)
994
995static ssize_t
996store_pwm_mode(struct device *dev, struct device_attribute *attr,
997 const char *buf, size_t count)
998{
David Hubbard1ea6dd32007-06-24 11:16:15 +0200999 struct w83627ehf_data *data = dev_get_drvdata(dev);
Rudolf Marek08c79952006-07-05 18:14:31 +02001000 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
1001 int nr = sensor_attr->index;
Guenter Roeckbce26c52011-02-04 12:54:14 -08001002 unsigned long val;
1003 int err;
Rudolf Marek08c79952006-07-05 18:14:31 +02001004 u16 reg;
1005
Guenter Roeckbce26c52011-02-04 12:54:14 -08001006 err = strict_strtoul(buf, 10, &val);
1007 if (err < 0)
1008 return err;
1009
Rudolf Marek08c79952006-07-05 18:14:31 +02001010 if (val > 1)
1011 return -EINVAL;
1012 mutex_lock(&data->update_lock);
David Hubbard1ea6dd32007-06-24 11:16:15 +02001013 reg = w83627ehf_read_value(data, W83627EHF_REG_PWM_ENABLE[nr]);
Rudolf Marek08c79952006-07-05 18:14:31 +02001014 data->pwm_mode[nr] = val;
1015 reg &= ~(1 << W83627EHF_PWM_MODE_SHIFT[nr]);
1016 if (!val)
1017 reg |= 1 << W83627EHF_PWM_MODE_SHIFT[nr];
David Hubbard1ea6dd32007-06-24 11:16:15 +02001018 w83627ehf_write_value(data, W83627EHF_REG_PWM_ENABLE[nr], reg);
Rudolf Marek08c79952006-07-05 18:14:31 +02001019 mutex_unlock(&data->update_lock);
1020 return count;
1021}
1022
1023static ssize_t
1024store_pwm(struct device *dev, struct device_attribute *attr,
1025 const char *buf, size_t count)
1026{
David Hubbard1ea6dd32007-06-24 11:16:15 +02001027 struct w83627ehf_data *data = dev_get_drvdata(dev);
Rudolf Marek08c79952006-07-05 18:14:31 +02001028 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
1029 int nr = sensor_attr->index;
Guenter Roeckbce26c52011-02-04 12:54:14 -08001030 unsigned long val;
1031 int err;
1032
1033 err = strict_strtoul(buf, 10, &val);
1034 if (err < 0)
1035 return err;
1036
1037 val = SENSORS_LIMIT(val, 0, 255);
Rudolf Marek08c79952006-07-05 18:14:31 +02001038
1039 mutex_lock(&data->update_lock);
1040 data->pwm[nr] = val;
Guenter Roeck279af1a2011-02-13 22:34:47 -08001041 w83627ehf_write_value(data, data->REG_PWM[nr], val);
Rudolf Marek08c79952006-07-05 18:14:31 +02001042 mutex_unlock(&data->update_lock);
1043 return count;
1044}
1045
1046static ssize_t
1047store_pwm_enable(struct device *dev, struct device_attribute *attr,
1048 const char *buf, size_t count)
1049{
David Hubbard1ea6dd32007-06-24 11:16:15 +02001050 struct w83627ehf_data *data = dev_get_drvdata(dev);
Rudolf Marek08c79952006-07-05 18:14:31 +02001051 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
1052 int nr = sensor_attr->index;
Guenter Roeckbce26c52011-02-04 12:54:14 -08001053 unsigned long val;
1054 int err;
Rudolf Marek08c79952006-07-05 18:14:31 +02001055 u16 reg;
1056
Guenter Roeckbce26c52011-02-04 12:54:14 -08001057 err = strict_strtoul(buf, 10, &val);
1058 if (err < 0)
1059 return err;
1060
Guenter Roeckb84bb512011-02-13 23:01:25 -08001061 if (!val || (val > 4 && val != data->pwm_enable_orig[nr]))
Rudolf Marek08c79952006-07-05 18:14:31 +02001062 return -EINVAL;
1063 mutex_lock(&data->update_lock);
David Hubbard1ea6dd32007-06-24 11:16:15 +02001064 reg = w83627ehf_read_value(data, W83627EHF_REG_PWM_ENABLE[nr]);
Rudolf Marek08c79952006-07-05 18:14:31 +02001065 data->pwm_enable[nr] = val;
1066 reg &= ~(0x03 << W83627EHF_PWM_ENABLE_SHIFT[nr]);
1067 reg |= (val - 1) << W83627EHF_PWM_ENABLE_SHIFT[nr];
David Hubbard1ea6dd32007-06-24 11:16:15 +02001068 w83627ehf_write_value(data, W83627EHF_REG_PWM_ENABLE[nr], reg);
Rudolf Marek08c79952006-07-05 18:14:31 +02001069 mutex_unlock(&data->update_lock);
1070 return count;
1071}
1072
1073
1074#define show_tol_temp(reg) \
1075static ssize_t show_##reg(struct device *dev, struct device_attribute *attr, \
1076 char *buf) \
1077{ \
1078 struct w83627ehf_data *data = w83627ehf_update_device(dev); \
Guenter Roecke7e1ca62011-02-04 13:24:30 -08001079 struct sensor_device_attribute *sensor_attr = \
1080 to_sensor_dev_attr(attr); \
Rudolf Marek08c79952006-07-05 18:14:31 +02001081 int nr = sensor_attr->index; \
Guenter Roeckbce26c52011-02-04 12:54:14 -08001082 return sprintf(buf, "%d\n", data->reg[nr] * 1000); \
Rudolf Marek08c79952006-07-05 18:14:31 +02001083}
1084
1085show_tol_temp(tolerance)
1086show_tol_temp(target_temp)
1087
1088static ssize_t
1089store_target_temp(struct device *dev, struct device_attribute *attr,
1090 const char *buf, size_t count)
1091{
David Hubbard1ea6dd32007-06-24 11:16:15 +02001092 struct w83627ehf_data *data = dev_get_drvdata(dev);
Rudolf Marek08c79952006-07-05 18:14:31 +02001093 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
1094 int nr = sensor_attr->index;
Guenter Roeckbce26c52011-02-04 12:54:14 -08001095 long val;
1096 int err;
1097
1098 err = strict_strtol(buf, 10, &val);
1099 if (err < 0)
1100 return err;
1101
1102 val = SENSORS_LIMIT(DIV_ROUND_CLOSEST(val, 1000), 0, 127);
Rudolf Marek08c79952006-07-05 18:14:31 +02001103
1104 mutex_lock(&data->update_lock);
1105 data->target_temp[nr] = val;
Guenter Roeck279af1a2011-02-13 22:34:47 -08001106 w83627ehf_write_value(data, data->REG_TARGET[nr], val);
Rudolf Marek08c79952006-07-05 18:14:31 +02001107 mutex_unlock(&data->update_lock);
1108 return count;
1109}
1110
1111static ssize_t
1112store_tolerance(struct device *dev, struct device_attribute *attr,
1113 const char *buf, size_t count)
1114{
David Hubbard1ea6dd32007-06-24 11:16:15 +02001115 struct w83627ehf_data *data = dev_get_drvdata(dev);
Rudolf Marek08c79952006-07-05 18:14:31 +02001116 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
1117 int nr = sensor_attr->index;
1118 u16 reg;
Guenter Roeckbce26c52011-02-04 12:54:14 -08001119 long val;
1120 int err;
1121
1122 err = strict_strtol(buf, 10, &val);
1123 if (err < 0)
1124 return err;
1125
Rudolf Marek08c79952006-07-05 18:14:31 +02001126 /* Limit the temp to 0C - 15C */
Guenter Roeckbce26c52011-02-04 12:54:14 -08001127 val = SENSORS_LIMIT(DIV_ROUND_CLOSEST(val, 1000), 0, 15);
Rudolf Marek08c79952006-07-05 18:14:31 +02001128
1129 mutex_lock(&data->update_lock);
David Hubbard1ea6dd32007-06-24 11:16:15 +02001130 reg = w83627ehf_read_value(data, W83627EHF_REG_TOLERANCE[nr]);
Rudolf Marek08c79952006-07-05 18:14:31 +02001131 data->tolerance[nr] = val;
1132 if (nr == 1)
1133 reg = (reg & 0x0f) | (val << 4);
1134 else
1135 reg = (reg & 0xf0) | val;
David Hubbard1ea6dd32007-06-24 11:16:15 +02001136 w83627ehf_write_value(data, W83627EHF_REG_TOLERANCE[nr], reg);
Rudolf Marek08c79952006-07-05 18:14:31 +02001137 mutex_unlock(&data->update_lock);
1138 return count;
1139}
1140
1141static struct sensor_device_attribute sda_pwm[] = {
1142 SENSOR_ATTR(pwm1, S_IWUSR | S_IRUGO, show_pwm, store_pwm, 0),
1143 SENSOR_ATTR(pwm2, S_IWUSR | S_IRUGO, show_pwm, store_pwm, 1),
1144 SENSOR_ATTR(pwm3, S_IWUSR | S_IRUGO, show_pwm, store_pwm, 2),
1145 SENSOR_ATTR(pwm4, S_IWUSR | S_IRUGO, show_pwm, store_pwm, 3),
1146};
1147
1148static struct sensor_device_attribute sda_pwm_mode[] = {
1149 SENSOR_ATTR(pwm1_mode, S_IWUSR | S_IRUGO, show_pwm_mode,
1150 store_pwm_mode, 0),
1151 SENSOR_ATTR(pwm2_mode, S_IWUSR | S_IRUGO, show_pwm_mode,
1152 store_pwm_mode, 1),
1153 SENSOR_ATTR(pwm3_mode, S_IWUSR | S_IRUGO, show_pwm_mode,
1154 store_pwm_mode, 2),
1155 SENSOR_ATTR(pwm4_mode, S_IWUSR | S_IRUGO, show_pwm_mode,
1156 store_pwm_mode, 3),
1157};
1158
1159static struct sensor_device_attribute sda_pwm_enable[] = {
1160 SENSOR_ATTR(pwm1_enable, S_IWUSR | S_IRUGO, show_pwm_enable,
1161 store_pwm_enable, 0),
1162 SENSOR_ATTR(pwm2_enable, S_IWUSR | S_IRUGO, show_pwm_enable,
1163 store_pwm_enable, 1),
1164 SENSOR_ATTR(pwm3_enable, S_IWUSR | S_IRUGO, show_pwm_enable,
1165 store_pwm_enable, 2),
1166 SENSOR_ATTR(pwm4_enable, S_IWUSR | S_IRUGO, show_pwm_enable,
1167 store_pwm_enable, 3),
1168};
1169
1170static struct sensor_device_attribute sda_target_temp[] = {
1171 SENSOR_ATTR(pwm1_target, S_IWUSR | S_IRUGO, show_target_temp,
1172 store_target_temp, 0),
1173 SENSOR_ATTR(pwm2_target, S_IWUSR | S_IRUGO, show_target_temp,
1174 store_target_temp, 1),
1175 SENSOR_ATTR(pwm3_target, S_IWUSR | S_IRUGO, show_target_temp,
1176 store_target_temp, 2),
1177 SENSOR_ATTR(pwm4_target, S_IWUSR | S_IRUGO, show_target_temp,
1178 store_target_temp, 3),
1179};
1180
1181static struct sensor_device_attribute sda_tolerance[] = {
1182 SENSOR_ATTR(pwm1_tolerance, S_IWUSR | S_IRUGO, show_tolerance,
1183 store_tolerance, 0),
1184 SENSOR_ATTR(pwm2_tolerance, S_IWUSR | S_IRUGO, show_tolerance,
1185 store_tolerance, 1),
1186 SENSOR_ATTR(pwm3_tolerance, S_IWUSR | S_IRUGO, show_tolerance,
1187 store_tolerance, 2),
1188 SENSOR_ATTR(pwm4_tolerance, S_IWUSR | S_IRUGO, show_tolerance,
1189 store_tolerance, 3),
1190};
1191
Rudolf Marek08c79952006-07-05 18:14:31 +02001192/* Smart Fan registers */
1193
1194#define fan_functions(reg, REG) \
1195static ssize_t show_##reg(struct device *dev, struct device_attribute *attr, \
1196 char *buf) \
1197{ \
1198 struct w83627ehf_data *data = w83627ehf_update_device(dev); \
Guenter Roecke7e1ca62011-02-04 13:24:30 -08001199 struct sensor_device_attribute *sensor_attr = \
1200 to_sensor_dev_attr(attr); \
Rudolf Marek08c79952006-07-05 18:14:31 +02001201 int nr = sensor_attr->index; \
1202 return sprintf(buf, "%d\n", data->reg[nr]); \
Guenter Roecke7e1ca62011-02-04 13:24:30 -08001203} \
Rudolf Marek08c79952006-07-05 18:14:31 +02001204static ssize_t \
1205store_##reg(struct device *dev, struct device_attribute *attr, \
1206 const char *buf, size_t count) \
Guenter Roecke7e1ca62011-02-04 13:24:30 -08001207{ \
David Hubbard1ea6dd32007-06-24 11:16:15 +02001208 struct w83627ehf_data *data = dev_get_drvdata(dev); \
Guenter Roecke7e1ca62011-02-04 13:24:30 -08001209 struct sensor_device_attribute *sensor_attr = \
1210 to_sensor_dev_attr(attr); \
Rudolf Marek08c79952006-07-05 18:14:31 +02001211 int nr = sensor_attr->index; \
Guenter Roeckbce26c52011-02-04 12:54:14 -08001212 unsigned long val; \
1213 int err; \
1214 err = strict_strtoul(buf, 10, &val); \
1215 if (err < 0) \
1216 return err; \
1217 val = SENSORS_LIMIT(val, 1, 255); \
Rudolf Marek08c79952006-07-05 18:14:31 +02001218 mutex_lock(&data->update_lock); \
1219 data->reg[nr] = val; \
Guenter Roeckda2e0252010-08-14 21:08:55 +02001220 w83627ehf_write_value(data, data->REG_##REG[nr], val); \
Rudolf Marek08c79952006-07-05 18:14:31 +02001221 mutex_unlock(&data->update_lock); \
1222 return count; \
1223}
1224
Daniel J Blueman41e9a062009-12-14 18:01:37 -08001225fan_functions(fan_start_output, FAN_START_OUTPUT)
1226fan_functions(fan_stop_output, FAN_STOP_OUTPUT)
1227fan_functions(fan_max_output, FAN_MAX_OUTPUT)
1228fan_functions(fan_step_output, FAN_STEP_OUTPUT)
Rudolf Marek08c79952006-07-05 18:14:31 +02001229
1230#define fan_time_functions(reg, REG) \
1231static ssize_t show_##reg(struct device *dev, struct device_attribute *attr, \
1232 char *buf) \
1233{ \
1234 struct w83627ehf_data *data = w83627ehf_update_device(dev); \
Guenter Roecke7e1ca62011-02-04 13:24:30 -08001235 struct sensor_device_attribute *sensor_attr = \
1236 to_sensor_dev_attr(attr); \
Rudolf Marek08c79952006-07-05 18:14:31 +02001237 int nr = sensor_attr->index; \
1238 return sprintf(buf, "%d\n", \
Guenter Roecke7e1ca62011-02-04 13:24:30 -08001239 step_time_from_reg(data->reg[nr], \
1240 data->pwm_mode[nr])); \
Rudolf Marek08c79952006-07-05 18:14:31 +02001241} \
1242\
1243static ssize_t \
1244store_##reg(struct device *dev, struct device_attribute *attr, \
1245 const char *buf, size_t count) \
1246{ \
David Hubbard1ea6dd32007-06-24 11:16:15 +02001247 struct w83627ehf_data *data = dev_get_drvdata(dev); \
Guenter Roecke7e1ca62011-02-04 13:24:30 -08001248 struct sensor_device_attribute *sensor_attr = \
1249 to_sensor_dev_attr(attr); \
Rudolf Marek08c79952006-07-05 18:14:31 +02001250 int nr = sensor_attr->index; \
Guenter Roeckbce26c52011-02-04 12:54:14 -08001251 unsigned long val; \
1252 int err; \
1253 err = strict_strtoul(buf, 10, &val); \
1254 if (err < 0) \
1255 return err; \
1256 val = step_time_to_reg(val, data->pwm_mode[nr]); \
Rudolf Marek08c79952006-07-05 18:14:31 +02001257 mutex_lock(&data->update_lock); \
1258 data->reg[nr] = val; \
David Hubbard1ea6dd32007-06-24 11:16:15 +02001259 w83627ehf_write_value(data, W83627EHF_REG_##REG[nr], val); \
Rudolf Marek08c79952006-07-05 18:14:31 +02001260 mutex_unlock(&data->update_lock); \
1261 return count; \
1262} \
1263
1264fan_time_functions(fan_stop_time, FAN_STOP_TIME)
1265
David Hubbard1ea6dd32007-06-24 11:16:15 +02001266static ssize_t show_name(struct device *dev, struct device_attribute *attr,
1267 char *buf)
1268{
1269 struct w83627ehf_data *data = dev_get_drvdata(dev);
1270
1271 return sprintf(buf, "%s\n", data->name);
1272}
1273static DEVICE_ATTR(name, S_IRUGO, show_name, NULL);
Rudolf Marek08c79952006-07-05 18:14:31 +02001274
1275static struct sensor_device_attribute sda_sf3_arrays_fan4[] = {
1276 SENSOR_ATTR(pwm4_stop_time, S_IWUSR | S_IRUGO, show_fan_stop_time,
1277 store_fan_stop_time, 3),
Daniel J Blueman41e9a062009-12-14 18:01:37 -08001278 SENSOR_ATTR(pwm4_start_output, S_IWUSR | S_IRUGO, show_fan_start_output,
1279 store_fan_start_output, 3),
1280 SENSOR_ATTR(pwm4_stop_output, S_IWUSR | S_IRUGO, show_fan_stop_output,
1281 store_fan_stop_output, 3),
1282 SENSOR_ATTR(pwm4_max_output, S_IWUSR | S_IRUGO, show_fan_max_output,
1283 store_fan_max_output, 3),
1284 SENSOR_ATTR(pwm4_step_output, S_IWUSR | S_IRUGO, show_fan_step_output,
1285 store_fan_step_output, 3),
Rudolf Marek08c79952006-07-05 18:14:31 +02001286};
1287
1288static struct sensor_device_attribute sda_sf3_arrays[] = {
1289 SENSOR_ATTR(pwm1_stop_time, S_IWUSR | S_IRUGO, show_fan_stop_time,
1290 store_fan_stop_time, 0),
1291 SENSOR_ATTR(pwm2_stop_time, S_IWUSR | S_IRUGO, show_fan_stop_time,
1292 store_fan_stop_time, 1),
1293 SENSOR_ATTR(pwm3_stop_time, S_IWUSR | S_IRUGO, show_fan_stop_time,
1294 store_fan_stop_time, 2),
Daniel J Blueman41e9a062009-12-14 18:01:37 -08001295 SENSOR_ATTR(pwm1_start_output, S_IWUSR | S_IRUGO, show_fan_start_output,
1296 store_fan_start_output, 0),
1297 SENSOR_ATTR(pwm2_start_output, S_IWUSR | S_IRUGO, show_fan_start_output,
1298 store_fan_start_output, 1),
1299 SENSOR_ATTR(pwm3_start_output, S_IWUSR | S_IRUGO, show_fan_start_output,
1300 store_fan_start_output, 2),
1301 SENSOR_ATTR(pwm1_stop_output, S_IWUSR | S_IRUGO, show_fan_stop_output,
1302 store_fan_stop_output, 0),
1303 SENSOR_ATTR(pwm2_stop_output, S_IWUSR | S_IRUGO, show_fan_stop_output,
1304 store_fan_stop_output, 1),
1305 SENSOR_ATTR(pwm3_stop_output, S_IWUSR | S_IRUGO, show_fan_stop_output,
1306 store_fan_stop_output, 2),
Guenter Roeckda2e0252010-08-14 21:08:55 +02001307};
Daniel J Blueman41e9a062009-12-14 18:01:37 -08001308
Guenter Roeckda2e0252010-08-14 21:08:55 +02001309
1310/*
1311 * pwm1 and pwm3 don't support max and step settings on all chips.
1312 * Need to check support while generating/removing attribute files.
1313 */
1314static struct sensor_device_attribute sda_sf3_max_step_arrays[] = {
1315 SENSOR_ATTR(pwm1_max_output, S_IWUSR | S_IRUGO, show_fan_max_output,
1316 store_fan_max_output, 0),
1317 SENSOR_ATTR(pwm1_step_output, S_IWUSR | S_IRUGO, show_fan_step_output,
1318 store_fan_step_output, 0),
Daniel J Blueman41e9a062009-12-14 18:01:37 -08001319 SENSOR_ATTR(pwm2_max_output, S_IWUSR | S_IRUGO, show_fan_max_output,
1320 store_fan_max_output, 1),
1321 SENSOR_ATTR(pwm2_step_output, S_IWUSR | S_IRUGO, show_fan_step_output,
1322 store_fan_step_output, 1),
Guenter Roeckda2e0252010-08-14 21:08:55 +02001323 SENSOR_ATTR(pwm3_max_output, S_IWUSR | S_IRUGO, show_fan_max_output,
1324 store_fan_max_output, 2),
1325 SENSOR_ATTR(pwm3_step_output, S_IWUSR | S_IRUGO, show_fan_step_output,
1326 store_fan_step_output, 2),
Rudolf Marek08c79952006-07-05 18:14:31 +02001327};
1328
Jean Delvarefc18d6c2007-06-24 11:19:42 +02001329static ssize_t
1330show_vid(struct device *dev, struct device_attribute *attr, char *buf)
1331{
1332 struct w83627ehf_data *data = dev_get_drvdata(dev);
1333 return sprintf(buf, "%d\n", vid_from_reg(data->vid, data->vrm));
1334}
1335static DEVICE_ATTR(cpu0_vid, S_IRUGO, show_vid, NULL);
1336
Jean Delvare08e7e272005-04-25 22:43:25 +02001337/*
David Hubbard1ea6dd32007-06-24 11:16:15 +02001338 * Driver and device management
Jean Delvare08e7e272005-04-25 22:43:25 +02001339 */
1340
David Hubbardc18beb52006-09-24 21:04:38 +02001341static void w83627ehf_device_remove_files(struct device *dev)
1342{
1343 /* some entries in the following arrays may not have been used in
1344 * device_create_file(), but device_remove_file() will ignore them */
1345 int i;
David Hubbard1ea6dd32007-06-24 11:16:15 +02001346 struct w83627ehf_data *data = dev_get_drvdata(dev);
David Hubbardc18beb52006-09-24 21:04:38 +02001347
1348 for (i = 0; i < ARRAY_SIZE(sda_sf3_arrays); i++)
1349 device_remove_file(dev, &sda_sf3_arrays[i].dev_attr);
Guenter Roeckda2e0252010-08-14 21:08:55 +02001350 for (i = 0; i < ARRAY_SIZE(sda_sf3_max_step_arrays); i++) {
1351 struct sensor_device_attribute *attr =
1352 &sda_sf3_max_step_arrays[i];
1353 if (data->REG_FAN_STEP_OUTPUT[attr->index] != 0xff)
1354 device_remove_file(dev, &attr->dev_attr);
1355 }
David Hubbardc18beb52006-09-24 21:04:38 +02001356 for (i = 0; i < ARRAY_SIZE(sda_sf3_arrays_fan4); i++)
1357 device_remove_file(dev, &sda_sf3_arrays_fan4[i].dev_attr);
David Hubbard1ea6dd32007-06-24 11:16:15 +02001358 for (i = 0; i < data->in_num; i++) {
Gong Juna157d062009-03-30 21:46:43 +02001359 if ((i == 6) && data->in6_skip)
1360 continue;
David Hubbardc18beb52006-09-24 21:04:38 +02001361 device_remove_file(dev, &sda_in_input[i].dev_attr);
1362 device_remove_file(dev, &sda_in_alarm[i].dev_attr);
1363 device_remove_file(dev, &sda_in_min[i].dev_attr);
1364 device_remove_file(dev, &sda_in_max[i].dev_attr);
1365 }
1366 for (i = 0; i < 5; i++) {
1367 device_remove_file(dev, &sda_fan_input[i].dev_attr);
1368 device_remove_file(dev, &sda_fan_alarm[i].dev_attr);
1369 device_remove_file(dev, &sda_fan_div[i].dev_attr);
1370 device_remove_file(dev, &sda_fan_min[i].dev_attr);
1371 }
Gong Jun237c8d2f2009-03-30 21:46:42 +02001372 for (i = 0; i < data->pwm_num; i++) {
David Hubbardc18beb52006-09-24 21:04:38 +02001373 device_remove_file(dev, &sda_pwm[i].dev_attr);
1374 device_remove_file(dev, &sda_pwm_mode[i].dev_attr);
1375 device_remove_file(dev, &sda_pwm_enable[i].dev_attr);
1376 device_remove_file(dev, &sda_target_temp[i].dev_attr);
1377 device_remove_file(dev, &sda_tolerance[i].dev_attr);
1378 }
Guenter Roeckd36cf322011-02-07 15:08:54 -08001379 for (i = 0; i < NUM_REG_TEMP; i++) {
1380 if (!(data->have_temp & (1 << i)))
Gong Juna157d062009-03-30 21:46:43 +02001381 continue;
1382 device_remove_file(dev, &sda_temp_input[i].dev_attr);
Guenter Roeckd36cf322011-02-07 15:08:54 -08001383 device_remove_file(dev, &sda_temp_label[i].dev_attr);
1384 if (i > 2)
1385 break;
Gong Juna157d062009-03-30 21:46:43 +02001386 device_remove_file(dev, &sda_temp_max[i].dev_attr);
1387 device_remove_file(dev, &sda_temp_max_hyst[i].dev_attr);
1388 device_remove_file(dev, &sda_temp_alarm[i].dev_attr);
1389 device_remove_file(dev, &sda_temp_type[i].dev_attr);
1390 }
David Hubbard1ea6dd32007-06-24 11:16:15 +02001391
1392 device_remove_file(dev, &dev_attr_name);
Jean Delvarecbe311f2008-01-03 21:22:44 +01001393 device_remove_file(dev, &dev_attr_cpu0_vid);
David Hubbardc18beb52006-09-24 21:04:38 +02001394}
1395
David Hubbard1ea6dd32007-06-24 11:16:15 +02001396/* Get the monitoring functions started */
1397static inline void __devinit w83627ehf_init_device(struct w83627ehf_data *data)
Jean Delvare08e7e272005-04-25 22:43:25 +02001398{
1399 int i;
Jean Delvareda667362007-06-24 11:21:02 +02001400 u8 tmp, diode;
Jean Delvare08e7e272005-04-25 22:43:25 +02001401
1402 /* Start monitoring is needed */
David Hubbard1ea6dd32007-06-24 11:16:15 +02001403 tmp = w83627ehf_read_value(data, W83627EHF_REG_CONFIG);
Jean Delvare08e7e272005-04-25 22:43:25 +02001404 if (!(tmp & 0x01))
David Hubbard1ea6dd32007-06-24 11:16:15 +02001405 w83627ehf_write_value(data, W83627EHF_REG_CONFIG,
Jean Delvare08e7e272005-04-25 22:43:25 +02001406 tmp | 0x01);
1407
Guenter Roeckd36cf322011-02-07 15:08:54 -08001408 /* Enable temperature sensors if needed */
1409 for (i = 0; i < NUM_REG_TEMP; i++) {
1410 if (!(data->have_temp & (1 << i)))
1411 continue;
1412 if (!W83627EHF_REG_TEMP_CONFIG[i])
1413 continue;
David Hubbard1ea6dd32007-06-24 11:16:15 +02001414 tmp = w83627ehf_read_value(data,
Jean Delvare08e7e272005-04-25 22:43:25 +02001415 W83627EHF_REG_TEMP_CONFIG[i]);
1416 if (tmp & 0x01)
David Hubbard1ea6dd32007-06-24 11:16:15 +02001417 w83627ehf_write_value(data,
Jean Delvare08e7e272005-04-25 22:43:25 +02001418 W83627EHF_REG_TEMP_CONFIG[i],
1419 tmp & 0xfe);
1420 }
Jean Delvared3130f02007-06-24 11:20:13 +02001421
1422 /* Enable VBAT monitoring if needed */
1423 tmp = w83627ehf_read_value(data, W83627EHF_REG_VBAT);
1424 if (!(tmp & 0x01))
1425 w83627ehf_write_value(data, W83627EHF_REG_VBAT, tmp | 0x01);
Jean Delvareda667362007-06-24 11:21:02 +02001426
1427 /* Get thermal sensor types */
1428 diode = w83627ehf_read_value(data, W83627EHF_REG_DIODE);
1429 for (i = 0; i < 3; i++) {
1430 if ((tmp & (0x02 << i)))
1431 data->temp_type[i] = (diode & (0x10 << i)) ? 1 : 2;
1432 else
1433 data->temp_type[i] = 4; /* thermistor */
1434 }
Jean Delvare08e7e272005-04-25 22:43:25 +02001435}
1436
David Hubbard1ea6dd32007-06-24 11:16:15 +02001437static int __devinit w83627ehf_probe(struct platform_device *pdev)
Jean Delvare08e7e272005-04-25 22:43:25 +02001438{
David Hubbard1ea6dd32007-06-24 11:16:15 +02001439 struct device *dev = &pdev->dev;
1440 struct w83627ehf_sio_data *sio_data = dev->platform_data;
Jean Delvare08e7e272005-04-25 22:43:25 +02001441 struct w83627ehf_data *data;
David Hubbard1ea6dd32007-06-24 11:16:15 +02001442 struct resource *res;
Jean Delvarefc18d6c2007-06-24 11:19:42 +02001443 u8 fan4pin, fan5pin, en_vrm10;
Jean Delvare08e7e272005-04-25 22:43:25 +02001444 int i, err = 0;
1445
David Hubbard1ea6dd32007-06-24 11:16:15 +02001446 res = platform_get_resource(pdev, IORESOURCE_IO, 0);
1447 if (!request_region(res->start, IOREGION_LENGTH, DRVNAME)) {
Jean Delvare08e7e272005-04-25 22:43:25 +02001448 err = -EBUSY;
David Hubbard1ea6dd32007-06-24 11:16:15 +02001449 dev_err(dev, "Failed to request region 0x%lx-0x%lx\n",
1450 (unsigned long)res->start,
1451 (unsigned long)res->start + IOREGION_LENGTH - 1);
Jean Delvare08e7e272005-04-25 22:43:25 +02001452 goto exit;
1453 }
1454
Guenter Roecke7e1ca62011-02-04 13:24:30 -08001455 data = kzalloc(sizeof(struct w83627ehf_data), GFP_KERNEL);
1456 if (!data) {
Jean Delvare08e7e272005-04-25 22:43:25 +02001457 err = -ENOMEM;
1458 goto exit_release;
1459 }
Jean Delvare08e7e272005-04-25 22:43:25 +02001460
David Hubbard1ea6dd32007-06-24 11:16:15 +02001461 data->addr = res->start;
Ingo Molnar9a61bf62006-01-18 23:19:26 +01001462 mutex_init(&data->lock);
Ingo Molnar9a61bf62006-01-18 23:19:26 +01001463 mutex_init(&data->update_lock);
David Hubbard1ea6dd32007-06-24 11:16:15 +02001464 data->name = w83627ehf_device_names[sio_data->kind];
1465 platform_set_drvdata(pdev, data);
Jean Delvare08e7e272005-04-25 22:43:25 +02001466
Gong Jun237c8d2f2009-03-30 21:46:42 +02001467 /* 627EHG and 627EHF have 10 voltage inputs; 627DHG and 667HG have 9 */
1468 data->in_num = (sio_data->kind == w83627ehf) ? 10 : 9;
1469 /* 667HG has 3 pwms */
Guenter Roeckc39aeda2010-08-14 21:08:55 +02001470 data->pwm_num = (sio_data->kind == w83667hg
1471 || sio_data->kind == w83667hg_b) ? 3 : 4;
Jean Delvare08e7e272005-04-25 22:43:25 +02001472
Guenter Roeckd36cf322011-02-07 15:08:54 -08001473 data->have_temp = 0x07;
Gong Juna157d062009-03-30 21:46:43 +02001474 /* Check temp3 configuration bit for 667HG */
Guenter Roeckd36cf322011-02-07 15:08:54 -08001475 if (sio_data->kind == w83667hg) {
1476 u8 reg;
1477
1478 reg = w83627ehf_read_value(data, W83627EHF_REG_TEMP_CONFIG[2]);
1479 if (reg & 0x01)
1480 data->have_temp &= ~(1 << 2);
1481 else
1482 data->in6_skip = 1; /* Either temp3 or in6 */
1483 } else if (sio_data->kind == w83667hg_b) {
1484 u8 reg;
1485
1486 reg = w83627ehf_read_value(data, 0x4a);
1487 data->temp_src[0] = reg >> 5;
1488 reg = w83627ehf_read_value(data, 0x49);
1489 data->temp_src[1] = reg & 0x07;
1490 data->temp_src[2] = (reg >> 4) & 0x07;
1491
1492 /*
1493 * W83667HG-B has another temperature register at 0x7e.
1494 * The temperature source is selected with register 0x7d.
1495 * Support it if the source differs from already reported
1496 * sources.
1497 */
1498 reg = w83627ehf_read_value(data, 0x7d);
1499 reg &= 0x07;
1500 if (reg != data->temp_src[0] && reg != data->temp_src[1]
1501 && reg != data->temp_src[2]) {
1502 data->temp_src[3] = reg;
1503 data->have_temp |= 1 << 3;
1504 }
1505
1506 /*
1507 * Chip supports either AUXTIN or VIN3. Try to find out which
1508 * one.
1509 */
1510 reg = w83627ehf_read_value(data, W83627EHF_REG_TEMP_CONFIG[2]);
1511 if (data->temp_src[2] == 2 && (reg & 0x01))
1512 data->have_temp &= ~(1 << 2);
1513
1514 if ((data->temp_src[2] == 2 && (data->have_temp & (1 << 2)))
1515 || (data->temp_src[3] == 2 && (data->have_temp & (1 << 3))))
1516 data->in6_skip = 1;
1517
1518 data->temp_label = w83667hg_b_temp_label;
Gong Juna157d062009-03-30 21:46:43 +02001519 }
1520
Guenter Roeck279af1a2011-02-13 22:34:47 -08001521 data->REG_PWM = W83627EHF_REG_PWM;
1522 data->REG_TARGET = W83627EHF_REG_TARGET;
1523 data->REG_FAN = W83627EHF_REG_FAN;
1524 data->REG_FAN_MIN = W83627EHF_REG_FAN_MIN;
Guenter Roeckda2e0252010-08-14 21:08:55 +02001525 data->REG_FAN_START_OUTPUT = W83627EHF_REG_FAN_START_OUTPUT;
1526 data->REG_FAN_STOP_OUTPUT = W83627EHF_REG_FAN_STOP_OUTPUT;
Guenter Roeck279af1a2011-02-13 22:34:47 -08001527 data->REG_FAN_STOP_TIME = W83627EHF_REG_FAN_STOP_TIME;
1528 data->REG_FAN_START_OUTPUT = W83627EHF_REG_FAN_START_OUTPUT;
Guenter Roeckc39aeda2010-08-14 21:08:55 +02001529 if (sio_data->kind == w83667hg_b) {
1530 data->REG_FAN_MAX_OUTPUT =
1531 W83627EHF_REG_FAN_MAX_OUTPUT_W83667_B;
1532 data->REG_FAN_STEP_OUTPUT =
1533 W83627EHF_REG_FAN_STEP_OUTPUT_W83667_B;
1534 } else {
1535 data->REG_FAN_MAX_OUTPUT =
1536 W83627EHF_REG_FAN_MAX_OUTPUT_COMMON;
1537 data->REG_FAN_STEP_OUTPUT =
1538 W83627EHF_REG_FAN_STEP_OUTPUT_COMMON;
1539 }
Guenter Roeckda2e0252010-08-14 21:08:55 +02001540
Jean Delvare08e7e272005-04-25 22:43:25 +02001541 /* Initialize the chip */
David Hubbard1ea6dd32007-06-24 11:16:15 +02001542 w83627ehf_init_device(data);
Jean Delvare08e7e272005-04-25 22:43:25 +02001543
Jean Delvarefc18d6c2007-06-24 11:19:42 +02001544 data->vrm = vid_which_vrm();
1545 superio_enter(sio_data->sioreg);
Jean Delvarefc18d6c2007-06-24 11:19:42 +02001546 /* Read VID value */
Guenter Roeckc39aeda2010-08-14 21:08:55 +02001547 if (sio_data->kind == w83667hg || sio_data->kind == w83667hg_b) {
Gong Jun237c8d2f2009-03-30 21:46:42 +02001548 /* W83667HG has different pins for VID input and output, so
1549 we can get the VID input values directly at logical device D
1550 0xe3. */
1551 superio_select(sio_data->sioreg, W83667HG_LD_VID);
1552 data->vid = superio_inb(sio_data->sioreg, 0xe3);
Jean Delvarecbe311f2008-01-03 21:22:44 +01001553 err = device_create_file(dev, &dev_attr_cpu0_vid);
1554 if (err)
1555 goto exit_release;
Jean Delvare58e6e782008-01-03 07:33:31 -05001556 } else {
Gong Jun237c8d2f2009-03-30 21:46:42 +02001557 superio_select(sio_data->sioreg, W83627EHF_LD_HWM);
1558 if (superio_inb(sio_data->sioreg, SIO_REG_VID_CTRL) & 0x80) {
1559 /* Set VID input sensibility if needed. In theory the
1560 BIOS should have set it, but in practice it's not
1561 always the case. We only do it for the W83627EHF/EHG
1562 because the W83627DHG is more complex in this
1563 respect. */
1564 if (sio_data->kind == w83627ehf) {
1565 en_vrm10 = superio_inb(sio_data->sioreg,
1566 SIO_REG_EN_VRM10);
1567 if ((en_vrm10 & 0x08) && data->vrm == 90) {
1568 dev_warn(dev, "Setting VID input "
1569 "voltage to TTL\n");
1570 superio_outb(sio_data->sioreg,
1571 SIO_REG_EN_VRM10,
1572 en_vrm10 & ~0x08);
1573 } else if (!(en_vrm10 & 0x08)
1574 && data->vrm == 100) {
1575 dev_warn(dev, "Setting VID input "
1576 "voltage to VRM10\n");
1577 superio_outb(sio_data->sioreg,
1578 SIO_REG_EN_VRM10,
1579 en_vrm10 | 0x08);
1580 }
1581 }
1582
1583 data->vid = superio_inb(sio_data->sioreg,
1584 SIO_REG_VID_DATA);
1585 if (sio_data->kind == w83627ehf) /* 6 VID pins only */
1586 data->vid &= 0x3f;
1587
1588 err = device_create_file(dev, &dev_attr_cpu0_vid);
1589 if (err)
1590 goto exit_release;
1591 } else {
1592 dev_info(dev, "VID pins in output mode, CPU VID not "
1593 "available\n");
1594 }
Jean Delvarefc18d6c2007-06-24 11:19:42 +02001595 }
1596
Rudolf Marek08c79952006-07-05 18:14:31 +02001597 /* fan4 and fan5 share some pins with the GPIO and serial flash */
Guenter Roeckc39aeda2010-08-14 21:08:55 +02001598 if (sio_data->kind == w83667hg || sio_data->kind == w83667hg_b) {
Gong Jun237c8d2f2009-03-30 21:46:42 +02001599 fan5pin = superio_inb(sio_data->sioreg, 0x27) & 0x20;
1600 fan4pin = superio_inb(sio_data->sioreg, 0x27) & 0x40;
1601 } else {
1602 fan5pin = !(superio_inb(sio_data->sioreg, 0x24) & 0x02);
1603 fan4pin = !(superio_inb(sio_data->sioreg, 0x29) & 0x06);
1604 }
David Hubbard1ea6dd32007-06-24 11:16:15 +02001605 superio_exit(sio_data->sioreg);
Rudolf Marek08c79952006-07-05 18:14:31 +02001606
Jean Delvare08e7e272005-04-25 22:43:25 +02001607 /* It looks like fan4 and fan5 pins can be alternatively used
Rudolf Marek14992c72006-10-08 22:02:09 +02001608 as fan on/off switches, but fan5 control is write only :/
1609 We assume that if the serial interface is disabled, designers
1610 connected fan5 as input unless they are emitting log 1, which
1611 is not the default. */
Rudolf Marek08c79952006-07-05 18:14:31 +02001612
Jean Delvare08e7e272005-04-25 22:43:25 +02001613 data->has_fan = 0x07; /* fan1, fan2 and fan3 */
David Hubbard1ea6dd32007-06-24 11:16:15 +02001614 i = w83627ehf_read_value(data, W83627EHF_REG_FANDIV1);
Jean Delvare1704b262009-03-30 21:46:42 +02001615 if ((i & (1 << 2)) && fan4pin)
Jean Delvare08e7e272005-04-25 22:43:25 +02001616 data->has_fan |= (1 << 3);
Jean Delvare1704b262009-03-30 21:46:42 +02001617 if (!(i & (1 << 1)) && fan5pin)
Jean Delvare08e7e272005-04-25 22:43:25 +02001618 data->has_fan |= (1 << 4);
1619
Mark M. Hoffmanea7be662007-08-05 12:19:01 -04001620 /* Read fan clock dividers immediately */
1621 w83627ehf_update_fan_div(data);
1622
Guenter Roeckb84bb512011-02-13 23:01:25 -08001623 /* Read pwm data to save original values */
1624 w83627ehf_update_pwm_common(dev, data);
1625 for (i = 0; i < data->pwm_num; i++)
1626 data->pwm_enable_orig[i] = data->pwm_enable[i];
1627
Jean Delvare08e7e272005-04-25 22:43:25 +02001628 /* Register sysfs hooks */
Guenter Roecke7e1ca62011-02-04 13:24:30 -08001629 for (i = 0; i < ARRAY_SIZE(sda_sf3_arrays); i++) {
1630 err = device_create_file(dev, &sda_sf3_arrays[i].dev_attr);
1631 if (err)
David Hubbardc18beb52006-09-24 21:04:38 +02001632 goto exit_remove;
Guenter Roecke7e1ca62011-02-04 13:24:30 -08001633 }
Rudolf Marek08c79952006-07-05 18:14:31 +02001634
Guenter Roeckda2e0252010-08-14 21:08:55 +02001635 for (i = 0; i < ARRAY_SIZE(sda_sf3_max_step_arrays); i++) {
1636 struct sensor_device_attribute *attr =
1637 &sda_sf3_max_step_arrays[i];
1638 if (data->REG_FAN_STEP_OUTPUT[attr->index] != 0xff) {
1639 err = device_create_file(dev, &attr->dev_attr);
1640 if (err)
1641 goto exit_remove;
1642 }
1643 }
Rudolf Marek08c79952006-07-05 18:14:31 +02001644 /* if fan4 is enabled create the sf3 files for it */
Gong Jun237c8d2f2009-03-30 21:46:42 +02001645 if ((data->has_fan & (1 << 3)) && data->pwm_num >= 4)
David Hubbardc18beb52006-09-24 21:04:38 +02001646 for (i = 0; i < ARRAY_SIZE(sda_sf3_arrays_fan4); i++) {
Guenter Roecke7e1ca62011-02-04 13:24:30 -08001647 err = device_create_file(dev,
1648 &sda_sf3_arrays_fan4[i].dev_attr);
1649 if (err)
David Hubbardc18beb52006-09-24 21:04:38 +02001650 goto exit_remove;
1651 }
Rudolf Marek08c79952006-07-05 18:14:31 +02001652
Gong Juna157d062009-03-30 21:46:43 +02001653 for (i = 0; i < data->in_num; i++) {
1654 if ((i == 6) && data->in6_skip)
1655 continue;
David Hubbardc18beb52006-09-24 21:04:38 +02001656 if ((err = device_create_file(dev, &sda_in_input[i].dev_attr))
1657 || (err = device_create_file(dev,
1658 &sda_in_alarm[i].dev_attr))
1659 || (err = device_create_file(dev,
1660 &sda_in_min[i].dev_attr))
1661 || (err = device_create_file(dev,
1662 &sda_in_max[i].dev_attr)))
1663 goto exit_remove;
Gong Juna157d062009-03-30 21:46:43 +02001664 }
Rudolf Marekcf0676f2006-03-23 16:25:22 +01001665
Yuan Mu412fec82006-02-05 23:24:16 +01001666 for (i = 0; i < 5; i++) {
Rudolf Marek08c79952006-07-05 18:14:31 +02001667 if (data->has_fan & (1 << i)) {
David Hubbardc18beb52006-09-24 21:04:38 +02001668 if ((err = device_create_file(dev,
1669 &sda_fan_input[i].dev_attr))
1670 || (err = device_create_file(dev,
1671 &sda_fan_alarm[i].dev_attr))
1672 || (err = device_create_file(dev,
1673 &sda_fan_div[i].dev_attr))
1674 || (err = device_create_file(dev,
1675 &sda_fan_min[i].dev_attr)))
1676 goto exit_remove;
Gong Jun237c8d2f2009-03-30 21:46:42 +02001677 if (i < data->pwm_num &&
David Hubbardc18beb52006-09-24 21:04:38 +02001678 ((err = device_create_file(dev,
1679 &sda_pwm[i].dev_attr))
1680 || (err = device_create_file(dev,
1681 &sda_pwm_mode[i].dev_attr))
1682 || (err = device_create_file(dev,
1683 &sda_pwm_enable[i].dev_attr))
1684 || (err = device_create_file(dev,
1685 &sda_target_temp[i].dev_attr))
1686 || (err = device_create_file(dev,
1687 &sda_tolerance[i].dev_attr))))
1688 goto exit_remove;
Rudolf Marek08c79952006-07-05 18:14:31 +02001689 }
Jean Delvare08e7e272005-04-25 22:43:25 +02001690 }
Rudolf Marek08c79952006-07-05 18:14:31 +02001691
Guenter Roeckd36cf322011-02-07 15:08:54 -08001692 for (i = 0; i < NUM_REG_TEMP; i++) {
1693 if (!(data->have_temp & (1 << i)))
Gong Juna157d062009-03-30 21:46:43 +02001694 continue;
Guenter Roeckd36cf322011-02-07 15:08:54 -08001695 err = device_create_file(dev, &sda_temp_input[i].dev_attr);
1696 if (err)
1697 goto exit_remove;
1698 if (data->temp_label) {
1699 err = device_create_file(dev,
1700 &sda_temp_label[i].dev_attr);
1701 if (err)
1702 goto exit_remove;
1703 }
1704 if (i > 2)
1705 break;
1706 if ((err = device_create_file(dev, &sda_temp_max[i].dev_attr))
Gong Juna157d062009-03-30 21:46:43 +02001707 || (err = device_create_file(dev,
1708 &sda_temp_max_hyst[i].dev_attr))
1709 || (err = device_create_file(dev,
1710 &sda_temp_alarm[i].dev_attr))
1711 || (err = device_create_file(dev,
1712 &sda_temp_type[i].dev_attr)))
David Hubbardc18beb52006-09-24 21:04:38 +02001713 goto exit_remove;
Gong Juna157d062009-03-30 21:46:43 +02001714 }
David Hubbardc18beb52006-09-24 21:04:38 +02001715
David Hubbard1ea6dd32007-06-24 11:16:15 +02001716 err = device_create_file(dev, &dev_attr_name);
1717 if (err)
1718 goto exit_remove;
1719
Tony Jones1beeffe2007-08-20 13:46:20 -07001720 data->hwmon_dev = hwmon_device_register(dev);
1721 if (IS_ERR(data->hwmon_dev)) {
1722 err = PTR_ERR(data->hwmon_dev);
David Hubbardc18beb52006-09-24 21:04:38 +02001723 goto exit_remove;
1724 }
Jean Delvare08e7e272005-04-25 22:43:25 +02001725
1726 return 0;
1727
David Hubbardc18beb52006-09-24 21:04:38 +02001728exit_remove:
1729 w83627ehf_device_remove_files(dev);
Jean Delvare08e7e272005-04-25 22:43:25 +02001730 kfree(data);
David Hubbard1ea6dd32007-06-24 11:16:15 +02001731 platform_set_drvdata(pdev, NULL);
Jean Delvare08e7e272005-04-25 22:43:25 +02001732exit_release:
David Hubbard1ea6dd32007-06-24 11:16:15 +02001733 release_region(res->start, IOREGION_LENGTH);
Jean Delvare08e7e272005-04-25 22:43:25 +02001734exit:
1735 return err;
1736}
1737
David Hubbard1ea6dd32007-06-24 11:16:15 +02001738static int __devexit w83627ehf_remove(struct platform_device *pdev)
Jean Delvare08e7e272005-04-25 22:43:25 +02001739{
David Hubbard1ea6dd32007-06-24 11:16:15 +02001740 struct w83627ehf_data *data = platform_get_drvdata(pdev);
Jean Delvare08e7e272005-04-25 22:43:25 +02001741
Tony Jones1beeffe2007-08-20 13:46:20 -07001742 hwmon_device_unregister(data->hwmon_dev);
David Hubbard1ea6dd32007-06-24 11:16:15 +02001743 w83627ehf_device_remove_files(&pdev->dev);
1744 release_region(data->addr, IOREGION_LENGTH);
1745 platform_set_drvdata(pdev, NULL);
Mark M. Hoffman943b0832005-07-15 21:39:18 -04001746 kfree(data);
Jean Delvare08e7e272005-04-25 22:43:25 +02001747
1748 return 0;
1749}
1750
David Hubbard1ea6dd32007-06-24 11:16:15 +02001751static struct platform_driver w83627ehf_driver = {
Laurent Riffardcdaf7932005-11-26 20:37:41 +01001752 .driver = {
Jean Delvare87218842006-09-03 22:36:14 +02001753 .owner = THIS_MODULE,
David Hubbard1ea6dd32007-06-24 11:16:15 +02001754 .name = DRVNAME,
Laurent Riffardcdaf7932005-11-26 20:37:41 +01001755 },
David Hubbard1ea6dd32007-06-24 11:16:15 +02001756 .probe = w83627ehf_probe,
1757 .remove = __devexit_p(w83627ehf_remove),
Jean Delvare08e7e272005-04-25 22:43:25 +02001758};
1759
David Hubbard1ea6dd32007-06-24 11:16:15 +02001760/* w83627ehf_find() looks for a '627 in the Super-I/O config space */
1761static int __init w83627ehf_find(int sioaddr, unsigned short *addr,
1762 struct w83627ehf_sio_data *sio_data)
Jean Delvare08e7e272005-04-25 22:43:25 +02001763{
David Hubbard1ea6dd32007-06-24 11:16:15 +02001764 static const char __initdata sio_name_W83627EHF[] = "W83627EHF";
1765 static const char __initdata sio_name_W83627EHG[] = "W83627EHG";
1766 static const char __initdata sio_name_W83627DHG[] = "W83627DHG";
Jean Delvarec1e48dc2009-06-15 18:39:50 +02001767 static const char __initdata sio_name_W83627DHG_P[] = "W83627DHG-P";
Gong Jun237c8d2f2009-03-30 21:46:42 +02001768 static const char __initdata sio_name_W83667HG[] = "W83667HG";
Guenter Roeckc39aeda2010-08-14 21:08:55 +02001769 static const char __initdata sio_name_W83667HG_B[] = "W83667HG-B";
David Hubbard1ea6dd32007-06-24 11:16:15 +02001770
Jean Delvare08e7e272005-04-25 22:43:25 +02001771 u16 val;
David Hubbard1ea6dd32007-06-24 11:16:15 +02001772 const char *sio_name;
Jean Delvare08e7e272005-04-25 22:43:25 +02001773
David Hubbard1ea6dd32007-06-24 11:16:15 +02001774 superio_enter(sioaddr);
Jean Delvare08e7e272005-04-25 22:43:25 +02001775
Jean Delvare67b671b2007-12-06 23:13:42 +01001776 if (force_id)
1777 val = force_id;
1778 else
1779 val = (superio_inb(sioaddr, SIO_REG_DEVID) << 8)
1780 | superio_inb(sioaddr, SIO_REG_DEVID + 1);
David Hubbard657c93b2007-02-14 21:15:04 +01001781 switch (val & SIO_ID_MASK) {
David Hubbard657c93b2007-02-14 21:15:04 +01001782 case SIO_W83627EHF_ID:
David Hubbard1ea6dd32007-06-24 11:16:15 +02001783 sio_data->kind = w83627ehf;
1784 sio_name = sio_name_W83627EHF;
1785 break;
David Hubbard657c93b2007-02-14 21:15:04 +01001786 case SIO_W83627EHG_ID:
David Hubbard1ea6dd32007-06-24 11:16:15 +02001787 sio_data->kind = w83627ehf;
1788 sio_name = sio_name_W83627EHG;
1789 break;
1790 case SIO_W83627DHG_ID:
1791 sio_data->kind = w83627dhg;
1792 sio_name = sio_name_W83627DHG;
David Hubbard657c93b2007-02-14 21:15:04 +01001793 break;
Jean Delvarec1e48dc2009-06-15 18:39:50 +02001794 case SIO_W83627DHG_P_ID:
1795 sio_data->kind = w83627dhg_p;
1796 sio_name = sio_name_W83627DHG_P;
1797 break;
Gong Jun237c8d2f2009-03-30 21:46:42 +02001798 case SIO_W83667HG_ID:
1799 sio_data->kind = w83667hg;
1800 sio_name = sio_name_W83667HG;
1801 break;
Guenter Roeckc39aeda2010-08-14 21:08:55 +02001802 case SIO_W83667HG_B_ID:
1803 sio_data->kind = w83667hg_b;
1804 sio_name = sio_name_W83667HG_B;
1805 break;
David Hubbard657c93b2007-02-14 21:15:04 +01001806 default:
Jean Delvare9f660362007-06-24 11:23:41 +02001807 if (val != 0xffff)
Joe Perchesabdc6fd2010-10-20 06:51:54 +00001808 pr_debug("unsupported chip ID: 0x%04x\n", val);
David Hubbard1ea6dd32007-06-24 11:16:15 +02001809 superio_exit(sioaddr);
Jean Delvare08e7e272005-04-25 22:43:25 +02001810 return -ENODEV;
1811 }
1812
David Hubbard1ea6dd32007-06-24 11:16:15 +02001813 /* We have a known chip, find the HWM I/O address */
1814 superio_select(sioaddr, W83627EHF_LD_HWM);
1815 val = (superio_inb(sioaddr, SIO_REG_ADDR) << 8)
1816 | superio_inb(sioaddr, SIO_REG_ADDR + 1);
Jean Delvare1a641fc2007-04-23 14:41:16 -07001817 *addr = val & IOREGION_ALIGNMENT;
Jean Delvare2d8672c2005-07-19 23:56:35 +02001818 if (*addr == 0) {
Joe Perchesabdc6fd2010-10-20 06:51:54 +00001819 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 +02001820 superio_exit(sioaddr);
Jean Delvare08e7e272005-04-25 22:43:25 +02001821 return -ENODEV;
1822 }
1823
1824 /* Activate logical device if needed */
David Hubbard1ea6dd32007-06-24 11:16:15 +02001825 val = superio_inb(sioaddr, SIO_REG_ENABLE);
David Hubbard475ef852007-06-24 11:17:09 +02001826 if (!(val & 0x01)) {
Guenter Roecke7e1ca62011-02-04 13:24:30 -08001827 pr_warn("Forcibly enabling Super-I/O. "
1828 "Sensor is probably unusable.\n");
David Hubbard1ea6dd32007-06-24 11:16:15 +02001829 superio_outb(sioaddr, SIO_REG_ENABLE, val | 0x01);
David Hubbard475ef852007-06-24 11:17:09 +02001830 }
Jean Delvare08e7e272005-04-25 22:43:25 +02001831
David Hubbard1ea6dd32007-06-24 11:16:15 +02001832 superio_exit(sioaddr);
Joe Perchesabdc6fd2010-10-20 06:51:54 +00001833 pr_info("Found %s chip at %#x\n", sio_name, *addr);
David Hubbard1ea6dd32007-06-24 11:16:15 +02001834 sio_data->sioreg = sioaddr;
1835
Jean Delvare08e7e272005-04-25 22:43:25 +02001836 return 0;
1837}
1838
David Hubbard1ea6dd32007-06-24 11:16:15 +02001839/* when Super-I/O functions move to a separate file, the Super-I/O
1840 * bus will manage the lifetime of the device and this module will only keep
1841 * track of the w83627ehf driver. But since we platform_device_alloc(), we
1842 * must keep track of the device */
1843static struct platform_device *pdev;
1844
Jean Delvare08e7e272005-04-25 22:43:25 +02001845static int __init sensors_w83627ehf_init(void)
1846{
David Hubbard1ea6dd32007-06-24 11:16:15 +02001847 int err;
1848 unsigned short address;
1849 struct resource res;
1850 struct w83627ehf_sio_data sio_data;
1851
1852 /* initialize sio_data->kind and sio_data->sioreg.
1853 *
1854 * when Super-I/O functions move to a separate file, the Super-I/O
1855 * driver will probe 0x2e and 0x4e and auto-detect the presence of a
1856 * w83627ehf hardware monitor, and call probe() */
1857 if (w83627ehf_find(0x2e, &address, &sio_data) &&
1858 w83627ehf_find(0x4e, &address, &sio_data))
Jean Delvare08e7e272005-04-25 22:43:25 +02001859 return -ENODEV;
1860
David Hubbard1ea6dd32007-06-24 11:16:15 +02001861 err = platform_driver_register(&w83627ehf_driver);
1862 if (err)
1863 goto exit;
1864
Guenter Roecke7e1ca62011-02-04 13:24:30 -08001865 pdev = platform_device_alloc(DRVNAME, address);
1866 if (!pdev) {
David Hubbard1ea6dd32007-06-24 11:16:15 +02001867 err = -ENOMEM;
Joe Perchesabdc6fd2010-10-20 06:51:54 +00001868 pr_err("Device allocation failed\n");
David Hubbard1ea6dd32007-06-24 11:16:15 +02001869 goto exit_unregister;
1870 }
1871
1872 err = platform_device_add_data(pdev, &sio_data,
1873 sizeof(struct w83627ehf_sio_data));
1874 if (err) {
Joe Perchesabdc6fd2010-10-20 06:51:54 +00001875 pr_err("Platform data allocation failed\n");
David Hubbard1ea6dd32007-06-24 11:16:15 +02001876 goto exit_device_put;
1877 }
1878
1879 memset(&res, 0, sizeof(res));
1880 res.name = DRVNAME;
1881 res.start = address + IOREGION_OFFSET;
1882 res.end = address + IOREGION_OFFSET + IOREGION_LENGTH - 1;
1883 res.flags = IORESOURCE_IO;
Jean Delvareb9acb642009-01-07 16:37:35 +01001884
1885 err = acpi_check_resource_conflict(&res);
1886 if (err)
Hans de Goede18632f82009-02-17 19:59:54 +01001887 goto exit_device_put;
Jean Delvareb9acb642009-01-07 16:37:35 +01001888
David Hubbard1ea6dd32007-06-24 11:16:15 +02001889 err = platform_device_add_resources(pdev, &res, 1);
1890 if (err) {
Joe Perchesabdc6fd2010-10-20 06:51:54 +00001891 pr_err("Device resource addition failed (%d)\n", err);
David Hubbard1ea6dd32007-06-24 11:16:15 +02001892 goto exit_device_put;
1893 }
1894
1895 /* platform_device_add calls probe() */
1896 err = platform_device_add(pdev);
1897 if (err) {
Joe Perchesabdc6fd2010-10-20 06:51:54 +00001898 pr_err("Device addition failed (%d)\n", err);
David Hubbard1ea6dd32007-06-24 11:16:15 +02001899 goto exit_device_put;
1900 }
1901
1902 return 0;
1903
1904exit_device_put:
1905 platform_device_put(pdev);
1906exit_unregister:
1907 platform_driver_unregister(&w83627ehf_driver);
1908exit:
1909 return err;
Jean Delvare08e7e272005-04-25 22:43:25 +02001910}
1911
1912static void __exit sensors_w83627ehf_exit(void)
1913{
David Hubbard1ea6dd32007-06-24 11:16:15 +02001914 platform_device_unregister(pdev);
1915 platform_driver_unregister(&w83627ehf_driver);
Jean Delvare08e7e272005-04-25 22:43:25 +02001916}
1917
1918MODULE_AUTHOR("Jean Delvare <khali@linux-fr.org>");
1919MODULE_DESCRIPTION("W83627EHF driver");
1920MODULE_LICENSE("GPL");
1921
1922module_init(sensors_w83627ehf_init);
1923module_exit(sensors_w83627ehf_exit);