blob: e64b42058b219772d0c28ecf0a8780f8ef7ea1ab [file] [log] [blame]
Jean Delvare08e7e272005-04-25 22:43:25 +02001/*
2 w83627ehf - Driver for the hardware monitoring functionality of
3 the Winbond W83627EHF Super-I/O chip
4 Copyright (C) 2005 Jean Delvare <khali@linux-fr.org>
Jean Delvare3379cee2006-09-24 21:25:52 +02005 Copyright (C) 2006 Yuan Mu (Winbond),
Jean Delvare7188cc62006-12-12 18:18:30 +01006 Rudolf Marek <r.marek@assembler.cz>
David Hubbardc18beb52006-09-24 21:04:38 +02007 David Hubbard <david.c.hubbard@gmail.com>
Jean Delvare08e7e272005-04-25 22:43:25 +02008
9 Shamelessly ripped from the w83627hf driver
10 Copyright (C) 2003 Mark Studebaker
11
12 Thanks to Leon Moonen, Steve Cliffe and Grant Coady for their help
13 in testing and debugging this driver.
14
Jean Delvare8dd2d2c2005-07-27 21:33:15 +020015 This driver also supports the W83627EHG, which is the lead-free
16 version of the W83627EHF.
17
Jean Delvare08e7e272005-04-25 22:43:25 +020018 This program is free software; you can redistribute it and/or modify
19 it under the terms of the GNU General Public License as published by
20 the Free Software Foundation; either version 2 of the License, or
21 (at your option) any later version.
22
23 This program is distributed in the hope that it will be useful,
24 but WITHOUT ANY WARRANTY; without even the implied warranty of
25 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
26 GNU General Public License for more details.
27
28 You should have received a copy of the GNU General Public License
29 along with this program; if not, write to the Free Software
30 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
31
32
33 Supports the following chips:
34
David Hubbard657c93b2007-02-14 21:15:04 +010035 Chip #vin #fan #pwm #temp chip IDs man ID
36 w83627ehf 10 5 4 3 0x8850 0x88 0x5ca3
37 0x8860 0xa1
38 w83627dhg 9 5 4 3 0xa020 0xc1 0x5ca3
Gong Jun237c8d22009-03-30 21:46:42 +020039 w83667hg 9 5 3 3 0xa510 0xc1 0x5ca3
Jean Delvare08e7e272005-04-25 22:43:25 +020040*/
41
42#include <linux/module.h>
43#include <linux/init.h>
44#include <linux/slab.h>
David Hubbard1ea6dd32007-06-24 11:16:15 +020045#include <linux/jiffies.h>
46#include <linux/platform_device.h>
Mark M. Hoffman943b0832005-07-15 21:39:18 -040047#include <linux/hwmon.h>
Yuan Mu412fec82006-02-05 23:24:16 +010048#include <linux/hwmon-sysfs.h>
Jean Delvarefc18d6c2007-06-24 11:19:42 +020049#include <linux/hwmon-vid.h>
Mark M. Hoffman943b0832005-07-15 21:39:18 -040050#include <linux/err.h>
Ingo Molnar9a61bf62006-01-18 23:19:26 +010051#include <linux/mutex.h>
Jean Delvareb9acb642009-01-07 16:37:35 +010052#include <linux/acpi.h>
Jean Delvare08e7e272005-04-25 22:43:25 +020053#include <asm/io.h>
54#include "lm75.h"
55
Gong Jun237c8d22009-03-30 21:46:42 +020056enum kinds { w83627ehf, w83627dhg, w83667hg };
David Hubbard1ea6dd32007-06-24 11:16:15 +020057
58/* used to set data->name = w83627ehf_device_names[data->sio_kind] */
59static const char * w83627ehf_device_names[] = {
60 "w83627ehf",
61 "w83627dhg",
Gong Jun237c8d22009-03-30 21:46:42 +020062 "w83667hg",
David Hubbard1ea6dd32007-06-24 11:16:15 +020063};
64
Jean Delvare67b671b2007-12-06 23:13:42 +010065static unsigned short force_id;
66module_param(force_id, ushort, 0);
67MODULE_PARM_DESC(force_id, "Override the detected device ID");
68
David Hubbard1ea6dd32007-06-24 11:16:15 +020069#define DRVNAME "w83627ehf"
Jean Delvare08e7e272005-04-25 22:43:25 +020070
71/*
72 * Super-I/O constants and functions
73 */
74
Jean Delvare08e7e272005-04-25 22:43:25 +020075#define W83627EHF_LD_HWM 0x0b
Gong Jun237c8d22009-03-30 21:46:42 +020076#define W83667HG_LD_VID 0x0d
Jean Delvare08e7e272005-04-25 22:43:25 +020077
78#define SIO_REG_LDSEL 0x07 /* Logical device select */
79#define SIO_REG_DEVID 0x20 /* Device ID (2 bytes) */
Jean Delvarefc18d6c2007-06-24 11:19:42 +020080#define SIO_REG_EN_VRM10 0x2C /* GPIO3, GPIO4 selection */
Jean Delvare08e7e272005-04-25 22:43:25 +020081#define SIO_REG_ENABLE 0x30 /* Logical device enable */
82#define SIO_REG_ADDR 0x60 /* Logical device address (2 bytes) */
Jean Delvarefc18d6c2007-06-24 11:19:42 +020083#define SIO_REG_VID_CTRL 0xF0 /* VID control */
84#define SIO_REG_VID_DATA 0xF1 /* VID data */
Jean Delvare08e7e272005-04-25 22:43:25 +020085
David Hubbard657c93b2007-02-14 21:15:04 +010086#define SIO_W83627EHF_ID 0x8850
87#define SIO_W83627EHG_ID 0x8860
88#define SIO_W83627DHG_ID 0xa020
Gong Jun237c8d22009-03-30 21:46:42 +020089#define SIO_W83667HG_ID 0xa510
David Hubbard657c93b2007-02-14 21:15:04 +010090#define SIO_ID_MASK 0xFFF0
Jean Delvare08e7e272005-04-25 22:43:25 +020091
92static inline void
David Hubbard1ea6dd32007-06-24 11:16:15 +020093superio_outb(int ioreg, int reg, int val)
Jean Delvare08e7e272005-04-25 22:43:25 +020094{
David Hubbard1ea6dd32007-06-24 11:16:15 +020095 outb(reg, ioreg);
96 outb(val, ioreg + 1);
Jean Delvare08e7e272005-04-25 22:43:25 +020097}
98
99static inline int
David Hubbard1ea6dd32007-06-24 11:16:15 +0200100superio_inb(int ioreg, int reg)
Jean Delvare08e7e272005-04-25 22:43:25 +0200101{
David Hubbard1ea6dd32007-06-24 11:16:15 +0200102 outb(reg, ioreg);
103 return inb(ioreg + 1);
Jean Delvare08e7e272005-04-25 22:43:25 +0200104}
105
106static inline void
David Hubbard1ea6dd32007-06-24 11:16:15 +0200107superio_select(int ioreg, int ld)
Jean Delvare08e7e272005-04-25 22:43:25 +0200108{
David Hubbard1ea6dd32007-06-24 11:16:15 +0200109 outb(SIO_REG_LDSEL, ioreg);
110 outb(ld, ioreg + 1);
Jean Delvare08e7e272005-04-25 22:43:25 +0200111}
112
113static inline void
David Hubbard1ea6dd32007-06-24 11:16:15 +0200114superio_enter(int ioreg)
Jean Delvare08e7e272005-04-25 22:43:25 +0200115{
David Hubbard1ea6dd32007-06-24 11:16:15 +0200116 outb(0x87, ioreg);
117 outb(0x87, ioreg);
Jean Delvare08e7e272005-04-25 22:43:25 +0200118}
119
120static inline void
David Hubbard1ea6dd32007-06-24 11:16:15 +0200121superio_exit(int ioreg)
Jean Delvare08e7e272005-04-25 22:43:25 +0200122{
David Hubbard1ea6dd32007-06-24 11:16:15 +0200123 outb(0x02, ioreg);
124 outb(0x02, ioreg + 1);
Jean Delvare08e7e272005-04-25 22:43:25 +0200125}
126
127/*
128 * ISA constants
129 */
130
Jean Delvare1a641fc2007-04-23 14:41:16 -0700131#define IOREGION_ALIGNMENT ~7
132#define IOREGION_OFFSET 5
133#define IOREGION_LENGTH 2
David Hubbard1ea6dd32007-06-24 11:16:15 +0200134#define ADDR_REG_OFFSET 0
135#define DATA_REG_OFFSET 1
Jean Delvare08e7e272005-04-25 22:43:25 +0200136
137#define W83627EHF_REG_BANK 0x4E
138#define W83627EHF_REG_CONFIG 0x40
David Hubbard657c93b2007-02-14 21:15:04 +0100139
140/* Not currently used:
141 * REG_MAN_ID has the value 0x5ca3 for all supported chips.
142 * REG_CHIP_ID == 0x88/0xa1/0xc1 depending on chip model.
143 * REG_MAN_ID is at port 0x4f
144 * REG_CHIP_ID is at port 0x58 */
Jean Delvare08e7e272005-04-25 22:43:25 +0200145
146static const u16 W83627EHF_REG_FAN[] = { 0x28, 0x29, 0x2a, 0x3f, 0x553 };
147static const u16 W83627EHF_REG_FAN_MIN[] = { 0x3b, 0x3c, 0x3d, 0x3e, 0x55c };
148
Rudolf Marekcf0676f2006-03-23 16:25:22 +0100149/* The W83627EHF registers for nr=7,8,9 are in bank 5 */
150#define W83627EHF_REG_IN_MAX(nr) ((nr < 7) ? (0x2b + (nr) * 2) : \
151 (0x554 + (((nr) - 7) * 2)))
152#define W83627EHF_REG_IN_MIN(nr) ((nr < 7) ? (0x2c + (nr) * 2) : \
153 (0x555 + (((nr) - 7) * 2)))
154#define W83627EHF_REG_IN(nr) ((nr < 7) ? (0x20 + (nr)) : \
155 (0x550 + (nr) - 7))
156
Jean Delvare08e7e272005-04-25 22:43:25 +0200157#define W83627EHF_REG_TEMP1 0x27
158#define W83627EHF_REG_TEMP1_HYST 0x3a
159#define W83627EHF_REG_TEMP1_OVER 0x39
160static const u16 W83627EHF_REG_TEMP[] = { 0x150, 0x250 };
161static const u16 W83627EHF_REG_TEMP_HYST[] = { 0x153, 0x253 };
162static const u16 W83627EHF_REG_TEMP_OVER[] = { 0x155, 0x255 };
163static const u16 W83627EHF_REG_TEMP_CONFIG[] = { 0x152, 0x252 };
164
165/* Fan clock dividers are spread over the following five registers */
166#define W83627EHF_REG_FANDIV1 0x47
167#define W83627EHF_REG_FANDIV2 0x4B
168#define W83627EHF_REG_VBAT 0x5D
169#define W83627EHF_REG_DIODE 0x59
170#define W83627EHF_REG_SMI_OVT 0x4C
171
Jean Delvarea4589db2006-03-23 16:30:29 +0100172#define W83627EHF_REG_ALARM1 0x459
173#define W83627EHF_REG_ALARM2 0x45A
174#define W83627EHF_REG_ALARM3 0x45B
175
Rudolf Marek08c79952006-07-05 18:14:31 +0200176/* SmartFan registers */
177/* DC or PWM output fan configuration */
178static const u8 W83627EHF_REG_PWM_ENABLE[] = {
179 0x04, /* SYS FAN0 output mode and PWM mode */
180 0x04, /* CPU FAN0 output mode and PWM mode */
181 0x12, /* AUX FAN mode */
182 0x62, /* CPU fan1 mode */
183};
184
185static const u8 W83627EHF_PWM_MODE_SHIFT[] = { 0, 1, 0, 6 };
186static const u8 W83627EHF_PWM_ENABLE_SHIFT[] = { 2, 4, 1, 4 };
187
188/* FAN Duty Cycle, be used to control */
189static const u8 W83627EHF_REG_PWM[] = { 0x01, 0x03, 0x11, 0x61 };
190static const u8 W83627EHF_REG_TARGET[] = { 0x05, 0x06, 0x13, 0x63 };
191static const u8 W83627EHF_REG_TOLERANCE[] = { 0x07, 0x07, 0x14, 0x62 };
192
193
194/* Advanced Fan control, some values are common for all fans */
195static const u8 W83627EHF_REG_FAN_MIN_OUTPUT[] = { 0x08, 0x09, 0x15, 0x64 };
196static const u8 W83627EHF_REG_FAN_STOP_TIME[] = { 0x0C, 0x0D, 0x17, 0x66 };
197
Jean Delvare08e7e272005-04-25 22:43:25 +0200198/*
199 * Conversions
200 */
201
Rudolf Marek08c79952006-07-05 18:14:31 +0200202/* 1 is PWM mode, output in ms */
203static inline unsigned int step_time_from_reg(u8 reg, u8 mode)
204{
205 return mode ? 100 * reg : 400 * reg;
206}
207
208static inline u8 step_time_to_reg(unsigned int msec, u8 mode)
209{
210 return SENSORS_LIMIT((mode ? (msec + 50) / 100 :
211 (msec + 200) / 400), 1, 255);
212}
213
Jean Delvare08e7e272005-04-25 22:43:25 +0200214static inline unsigned int
215fan_from_reg(u8 reg, unsigned int div)
216{
217 if (reg == 0 || reg == 255)
218 return 0;
219 return 1350000U / (reg * div);
220}
221
222static inline unsigned int
223div_from_reg(u8 reg)
224{
225 return 1 << reg;
226}
227
228static inline int
229temp1_from_reg(s8 reg)
230{
231 return reg * 1000;
232}
233
234static inline s8
Christian Hohnstaedt5bfedac2007-08-16 11:40:10 +0200235temp1_to_reg(long temp, int min, int max)
Jean Delvare08e7e272005-04-25 22:43:25 +0200236{
Rudolf Marek08c79952006-07-05 18:14:31 +0200237 if (temp <= min)
238 return min / 1000;
239 if (temp >= max)
240 return max / 1000;
Jean Delvare08e7e272005-04-25 22:43:25 +0200241 if (temp < 0)
242 return (temp - 500) / 1000;
243 return (temp + 500) / 1000;
244}
245
Rudolf Marekcf0676f2006-03-23 16:25:22 +0100246/* Some of analog inputs have internal scaling (2x), 8mV is ADC LSB */
247
248static u8 scale_in[10] = { 8, 8, 16, 16, 8, 8, 8, 16, 16, 8 };
249
250static inline long in_from_reg(u8 reg, u8 nr)
251{
252 return reg * scale_in[nr];
253}
254
255static inline u8 in_to_reg(u32 val, u8 nr)
256{
257 return SENSORS_LIMIT(((val + (scale_in[nr] / 2)) / scale_in[nr]), 0, 255);
258}
259
Jean Delvare08e7e272005-04-25 22:43:25 +0200260/*
261 * Data structures and manipulation thereof
262 */
263
264struct w83627ehf_data {
David Hubbard1ea6dd32007-06-24 11:16:15 +0200265 int addr; /* IO base of hw monitor block */
266 const char *name;
267
Tony Jones1beeffe2007-08-20 13:46:20 -0700268 struct device *hwmon_dev;
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100269 struct mutex lock;
Jean Delvare08e7e272005-04-25 22:43:25 +0200270
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100271 struct mutex update_lock;
Jean Delvare08e7e272005-04-25 22:43:25 +0200272 char valid; /* !=0 if following fields are valid */
273 unsigned long last_updated; /* In jiffies */
274
275 /* Register values */
David Hubbard1ea6dd32007-06-24 11:16:15 +0200276 u8 in_num; /* number of in inputs we have */
Rudolf Marekcf0676f2006-03-23 16:25:22 +0100277 u8 in[10]; /* Register value */
278 u8 in_max[10]; /* Register value */
279 u8 in_min[10]; /* Register value */
Jean Delvare08e7e272005-04-25 22:43:25 +0200280 u8 fan[5];
281 u8 fan_min[5];
282 u8 fan_div[5];
283 u8 has_fan; /* some fan inputs can be disabled */
Jean Delvareda667362007-06-24 11:21:02 +0200284 u8 temp_type[3];
Jean Delvare08e7e272005-04-25 22:43:25 +0200285 s8 temp1;
286 s8 temp1_max;
287 s8 temp1_max_hyst;
288 s16 temp[2];
289 s16 temp_max[2];
290 s16 temp_max_hyst[2];
Jean Delvarea4589db2006-03-23 16:30:29 +0100291 u32 alarms;
Rudolf Marek08c79952006-07-05 18:14:31 +0200292
293 u8 pwm_mode[4]; /* 0->DC variable voltage, 1->PWM variable duty cycle */
294 u8 pwm_enable[4]; /* 1->manual
295 2->thermal cruise (also called SmartFan I) */
Gong Jun237c8d22009-03-30 21:46:42 +0200296 u8 pwm_num; /* number of pwm */
Rudolf Marek08c79952006-07-05 18:14:31 +0200297 u8 pwm[4];
298 u8 target_temp[4];
299 u8 tolerance[4];
300
301 u8 fan_min_output[4]; /* minimum fan speed */
302 u8 fan_stop_time[4];
Jean Delvarefc18d6c2007-06-24 11:19:42 +0200303
304 u8 vid;
305 u8 vrm;
Gong Juna157d062009-03-30 21:46:43 +0200306
307 u8 temp3_disable;
308 u8 in6_skip;
Jean Delvare08e7e272005-04-25 22:43:25 +0200309};
310
David Hubbard1ea6dd32007-06-24 11:16:15 +0200311struct w83627ehf_sio_data {
312 int sioreg;
313 enum kinds kind;
314};
315
Jean Delvare08e7e272005-04-25 22:43:25 +0200316static inline int is_word_sized(u16 reg)
317{
318 return (((reg & 0xff00) == 0x100
319 || (reg & 0xff00) == 0x200)
320 && ((reg & 0x00ff) == 0x50
321 || (reg & 0x00ff) == 0x53
322 || (reg & 0x00ff) == 0x55));
323}
324
Jean Delvare09568952007-08-11 13:57:05 +0200325/* Registers 0x50-0x5f are banked */
David Hubbard1ea6dd32007-06-24 11:16:15 +0200326static inline void w83627ehf_set_bank(struct w83627ehf_data *data, u16 reg)
Jean Delvare08e7e272005-04-25 22:43:25 +0200327{
Jean Delvare09568952007-08-11 13:57:05 +0200328 if ((reg & 0x00f0) == 0x50) {
David Hubbard1ea6dd32007-06-24 11:16:15 +0200329 outb_p(W83627EHF_REG_BANK, data->addr + ADDR_REG_OFFSET);
330 outb_p(reg >> 8, data->addr + DATA_REG_OFFSET);
Jean Delvare08e7e272005-04-25 22:43:25 +0200331 }
332}
333
Jean Delvare09568952007-08-11 13:57:05 +0200334/* Not strictly necessary, but play it safe for now */
David Hubbard1ea6dd32007-06-24 11:16:15 +0200335static inline void w83627ehf_reset_bank(struct w83627ehf_data *data, u16 reg)
Jean Delvare08e7e272005-04-25 22:43:25 +0200336{
337 if (reg & 0xff00) {
David Hubbard1ea6dd32007-06-24 11:16:15 +0200338 outb_p(W83627EHF_REG_BANK, data->addr + ADDR_REG_OFFSET);
339 outb_p(0, data->addr + DATA_REG_OFFSET);
Jean Delvare08e7e272005-04-25 22:43:25 +0200340 }
341}
342
David Hubbard1ea6dd32007-06-24 11:16:15 +0200343static u16 w83627ehf_read_value(struct w83627ehf_data *data, u16 reg)
Jean Delvare08e7e272005-04-25 22:43:25 +0200344{
Jean Delvare08e7e272005-04-25 22:43:25 +0200345 int res, word_sized = is_word_sized(reg);
346
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100347 mutex_lock(&data->lock);
Jean Delvare08e7e272005-04-25 22:43:25 +0200348
David Hubbard1ea6dd32007-06-24 11:16:15 +0200349 w83627ehf_set_bank(data, reg);
350 outb_p(reg & 0xff, data->addr + ADDR_REG_OFFSET);
351 res = inb_p(data->addr + DATA_REG_OFFSET);
Jean Delvare08e7e272005-04-25 22:43:25 +0200352 if (word_sized) {
353 outb_p((reg & 0xff) + 1,
David Hubbard1ea6dd32007-06-24 11:16:15 +0200354 data->addr + ADDR_REG_OFFSET);
355 res = (res << 8) + inb_p(data->addr + DATA_REG_OFFSET);
Jean Delvare08e7e272005-04-25 22:43:25 +0200356 }
David Hubbard1ea6dd32007-06-24 11:16:15 +0200357 w83627ehf_reset_bank(data, reg);
Jean Delvare08e7e272005-04-25 22:43:25 +0200358
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100359 mutex_unlock(&data->lock);
Jean Delvare08e7e272005-04-25 22:43:25 +0200360
361 return res;
362}
363
David Hubbard1ea6dd32007-06-24 11:16:15 +0200364static int w83627ehf_write_value(struct w83627ehf_data *data, u16 reg, u16 value)
Jean Delvare08e7e272005-04-25 22:43:25 +0200365{
Jean Delvare08e7e272005-04-25 22:43:25 +0200366 int word_sized = is_word_sized(reg);
367
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100368 mutex_lock(&data->lock);
Jean Delvare08e7e272005-04-25 22:43:25 +0200369
David Hubbard1ea6dd32007-06-24 11:16:15 +0200370 w83627ehf_set_bank(data, reg);
371 outb_p(reg & 0xff, data->addr + ADDR_REG_OFFSET);
Jean Delvare08e7e272005-04-25 22:43:25 +0200372 if (word_sized) {
David Hubbard1ea6dd32007-06-24 11:16:15 +0200373 outb_p(value >> 8, data->addr + DATA_REG_OFFSET);
Jean Delvare08e7e272005-04-25 22:43:25 +0200374 outb_p((reg & 0xff) + 1,
David Hubbard1ea6dd32007-06-24 11:16:15 +0200375 data->addr + ADDR_REG_OFFSET);
Jean Delvare08e7e272005-04-25 22:43:25 +0200376 }
David Hubbard1ea6dd32007-06-24 11:16:15 +0200377 outb_p(value & 0xff, data->addr + DATA_REG_OFFSET);
378 w83627ehf_reset_bank(data, reg);
Jean Delvare08e7e272005-04-25 22:43:25 +0200379
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100380 mutex_unlock(&data->lock);
Jean Delvare08e7e272005-04-25 22:43:25 +0200381 return 0;
382}
383
384/* This function assumes that the caller holds data->update_lock */
David Hubbard1ea6dd32007-06-24 11:16:15 +0200385static void w83627ehf_write_fan_div(struct w83627ehf_data *data, int nr)
Jean Delvare08e7e272005-04-25 22:43:25 +0200386{
Jean Delvare08e7e272005-04-25 22:43:25 +0200387 u8 reg;
388
389 switch (nr) {
390 case 0:
David Hubbard1ea6dd32007-06-24 11:16:15 +0200391 reg = (w83627ehf_read_value(data, W83627EHF_REG_FANDIV1) & 0xcf)
Jean Delvare08e7e272005-04-25 22:43:25 +0200392 | ((data->fan_div[0] & 0x03) << 4);
Rudolf Marek14992c72006-10-08 22:02:09 +0200393 /* fan5 input control bit is write only, compute the value */
394 reg |= (data->has_fan & (1 << 4)) ? 1 : 0;
David Hubbard1ea6dd32007-06-24 11:16:15 +0200395 w83627ehf_write_value(data, W83627EHF_REG_FANDIV1, reg);
396 reg = (w83627ehf_read_value(data, W83627EHF_REG_VBAT) & 0xdf)
Jean Delvare08e7e272005-04-25 22:43:25 +0200397 | ((data->fan_div[0] & 0x04) << 3);
David Hubbard1ea6dd32007-06-24 11:16:15 +0200398 w83627ehf_write_value(data, W83627EHF_REG_VBAT, reg);
Jean Delvare08e7e272005-04-25 22:43:25 +0200399 break;
400 case 1:
David Hubbard1ea6dd32007-06-24 11:16:15 +0200401 reg = (w83627ehf_read_value(data, W83627EHF_REG_FANDIV1) & 0x3f)
Jean Delvare08e7e272005-04-25 22:43:25 +0200402 | ((data->fan_div[1] & 0x03) << 6);
Rudolf Marek14992c72006-10-08 22:02:09 +0200403 /* fan5 input control bit is write only, compute the value */
404 reg |= (data->has_fan & (1 << 4)) ? 1 : 0;
David Hubbard1ea6dd32007-06-24 11:16:15 +0200405 w83627ehf_write_value(data, W83627EHF_REG_FANDIV1, reg);
406 reg = (w83627ehf_read_value(data, W83627EHF_REG_VBAT) & 0xbf)
Jean Delvare08e7e272005-04-25 22:43:25 +0200407 | ((data->fan_div[1] & 0x04) << 4);
David Hubbard1ea6dd32007-06-24 11:16:15 +0200408 w83627ehf_write_value(data, W83627EHF_REG_VBAT, reg);
Jean Delvare08e7e272005-04-25 22:43:25 +0200409 break;
410 case 2:
David Hubbard1ea6dd32007-06-24 11:16:15 +0200411 reg = (w83627ehf_read_value(data, W83627EHF_REG_FANDIV2) & 0x3f)
Jean Delvare08e7e272005-04-25 22:43:25 +0200412 | ((data->fan_div[2] & 0x03) << 6);
David Hubbard1ea6dd32007-06-24 11:16:15 +0200413 w83627ehf_write_value(data, W83627EHF_REG_FANDIV2, reg);
414 reg = (w83627ehf_read_value(data, W83627EHF_REG_VBAT) & 0x7f)
Jean Delvare08e7e272005-04-25 22:43:25 +0200415 | ((data->fan_div[2] & 0x04) << 5);
David Hubbard1ea6dd32007-06-24 11:16:15 +0200416 w83627ehf_write_value(data, W83627EHF_REG_VBAT, reg);
Jean Delvare08e7e272005-04-25 22:43:25 +0200417 break;
418 case 3:
David Hubbard1ea6dd32007-06-24 11:16:15 +0200419 reg = (w83627ehf_read_value(data, W83627EHF_REG_DIODE) & 0xfc)
Jean Delvare08e7e272005-04-25 22:43:25 +0200420 | (data->fan_div[3] & 0x03);
David Hubbard1ea6dd32007-06-24 11:16:15 +0200421 w83627ehf_write_value(data, W83627EHF_REG_DIODE, reg);
422 reg = (w83627ehf_read_value(data, W83627EHF_REG_SMI_OVT) & 0x7f)
Jean Delvare08e7e272005-04-25 22:43:25 +0200423 | ((data->fan_div[3] & 0x04) << 5);
David Hubbard1ea6dd32007-06-24 11:16:15 +0200424 w83627ehf_write_value(data, W83627EHF_REG_SMI_OVT, reg);
Jean Delvare08e7e272005-04-25 22:43:25 +0200425 break;
426 case 4:
David Hubbard1ea6dd32007-06-24 11:16:15 +0200427 reg = (w83627ehf_read_value(data, W83627EHF_REG_DIODE) & 0x73)
Jean Delvare33725ad2007-04-17 00:32:27 -0700428 | ((data->fan_div[4] & 0x03) << 2)
Jean Delvare08e7e272005-04-25 22:43:25 +0200429 | ((data->fan_div[4] & 0x04) << 5);
David Hubbard1ea6dd32007-06-24 11:16:15 +0200430 w83627ehf_write_value(data, W83627EHF_REG_DIODE, reg);
Jean Delvare08e7e272005-04-25 22:43:25 +0200431 break;
432 }
433}
434
Mark M. Hoffmanea7be662007-08-05 12:19:01 -0400435static void w83627ehf_update_fan_div(struct w83627ehf_data *data)
436{
437 int i;
438
439 i = w83627ehf_read_value(data, W83627EHF_REG_FANDIV1);
440 data->fan_div[0] = (i >> 4) & 0x03;
441 data->fan_div[1] = (i >> 6) & 0x03;
442 i = w83627ehf_read_value(data, W83627EHF_REG_FANDIV2);
443 data->fan_div[2] = (i >> 6) & 0x03;
444 i = w83627ehf_read_value(data, W83627EHF_REG_VBAT);
445 data->fan_div[0] |= (i >> 3) & 0x04;
446 data->fan_div[1] |= (i >> 4) & 0x04;
447 data->fan_div[2] |= (i >> 5) & 0x04;
448 if (data->has_fan & ((1 << 3) | (1 << 4))) {
449 i = w83627ehf_read_value(data, W83627EHF_REG_DIODE);
450 data->fan_div[3] = i & 0x03;
451 data->fan_div[4] = ((i >> 2) & 0x03)
452 | ((i >> 5) & 0x04);
453 }
454 if (data->has_fan & (1 << 3)) {
455 i = w83627ehf_read_value(data, W83627EHF_REG_SMI_OVT);
456 data->fan_div[3] |= (i >> 5) & 0x04;
457 }
458}
459
Jean Delvare08e7e272005-04-25 22:43:25 +0200460static struct w83627ehf_data *w83627ehf_update_device(struct device *dev)
461{
David Hubbard1ea6dd32007-06-24 11:16:15 +0200462 struct w83627ehf_data *data = dev_get_drvdata(dev);
Rudolf Marek08c79952006-07-05 18:14:31 +0200463 int pwmcfg = 0, tolerance = 0; /* shut up the compiler */
Jean Delvare08e7e272005-04-25 22:43:25 +0200464 int i;
465
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100466 mutex_lock(&data->update_lock);
Jean Delvare08e7e272005-04-25 22:43:25 +0200467
Jean Delvare6b3e4642007-06-24 11:19:01 +0200468 if (time_after(jiffies, data->last_updated + HZ + HZ/2)
Jean Delvare08e7e272005-04-25 22:43:25 +0200469 || !data->valid) {
470 /* Fan clock dividers */
Mark M. Hoffmanea7be662007-08-05 12:19:01 -0400471 w83627ehf_update_fan_div(data);
Jean Delvare08e7e272005-04-25 22:43:25 +0200472
Rudolf Marekcf0676f2006-03-23 16:25:22 +0100473 /* Measured voltages and limits */
David Hubbard1ea6dd32007-06-24 11:16:15 +0200474 for (i = 0; i < data->in_num; i++) {
475 data->in[i] = w83627ehf_read_value(data,
Rudolf Marekcf0676f2006-03-23 16:25:22 +0100476 W83627EHF_REG_IN(i));
David Hubbard1ea6dd32007-06-24 11:16:15 +0200477 data->in_min[i] = w83627ehf_read_value(data,
Rudolf Marekcf0676f2006-03-23 16:25:22 +0100478 W83627EHF_REG_IN_MIN(i));
David Hubbard1ea6dd32007-06-24 11:16:15 +0200479 data->in_max[i] = w83627ehf_read_value(data,
Rudolf Marekcf0676f2006-03-23 16:25:22 +0100480 W83627EHF_REG_IN_MAX(i));
481 }
482
Jean Delvare08e7e272005-04-25 22:43:25 +0200483 /* Measured fan speeds and limits */
484 for (i = 0; i < 5; i++) {
485 if (!(data->has_fan & (1 << i)))
486 continue;
487
David Hubbard1ea6dd32007-06-24 11:16:15 +0200488 data->fan[i] = w83627ehf_read_value(data,
Jean Delvare08e7e272005-04-25 22:43:25 +0200489 W83627EHF_REG_FAN[i]);
David Hubbard1ea6dd32007-06-24 11:16:15 +0200490 data->fan_min[i] = w83627ehf_read_value(data,
Jean Delvare08e7e272005-04-25 22:43:25 +0200491 W83627EHF_REG_FAN_MIN[i]);
492
493 /* If we failed to measure the fan speed and clock
494 divider can be increased, let's try that for next
495 time */
496 if (data->fan[i] == 0xff
497 && data->fan_div[i] < 0x07) {
David Hubbard1ea6dd32007-06-24 11:16:15 +0200498 dev_dbg(dev, "Increasing fan%d "
Jean Delvare08e7e272005-04-25 22:43:25 +0200499 "clock divider from %u to %u\n",
Jean Delvare33725ad2007-04-17 00:32:27 -0700500 i + 1, div_from_reg(data->fan_div[i]),
Jean Delvare08e7e272005-04-25 22:43:25 +0200501 div_from_reg(data->fan_div[i] + 1));
502 data->fan_div[i]++;
David Hubbard1ea6dd32007-06-24 11:16:15 +0200503 w83627ehf_write_fan_div(data, i);
Jean Delvare08e7e272005-04-25 22:43:25 +0200504 /* Preserve min limit if possible */
505 if (data->fan_min[i] >= 2
506 && data->fan_min[i] != 255)
David Hubbard1ea6dd32007-06-24 11:16:15 +0200507 w83627ehf_write_value(data,
Jean Delvare08e7e272005-04-25 22:43:25 +0200508 W83627EHF_REG_FAN_MIN[i],
509 (data->fan_min[i] /= 2));
510 }
511 }
512
Rudolf Marek08c79952006-07-05 18:14:31 +0200513 for (i = 0; i < 4; i++) {
Jean Delvare77fa49d2009-01-07 16:37:35 +0100514 /* pwmcfg, tolerance mapped for i=0, i=1 to same reg */
Rudolf Marek08c79952006-07-05 18:14:31 +0200515 if (i != 1) {
David Hubbard1ea6dd32007-06-24 11:16:15 +0200516 pwmcfg = w83627ehf_read_value(data,
Rudolf Marek08c79952006-07-05 18:14:31 +0200517 W83627EHF_REG_PWM_ENABLE[i]);
David Hubbard1ea6dd32007-06-24 11:16:15 +0200518 tolerance = w83627ehf_read_value(data,
Rudolf Marek08c79952006-07-05 18:14:31 +0200519 W83627EHF_REG_TOLERANCE[i]);
520 }
521 data->pwm_mode[i] =
522 ((pwmcfg >> W83627EHF_PWM_MODE_SHIFT[i]) & 1)
523 ? 0 : 1;
524 data->pwm_enable[i] =
525 ((pwmcfg >> W83627EHF_PWM_ENABLE_SHIFT[i])
526 & 3) + 1;
David Hubbard1ea6dd32007-06-24 11:16:15 +0200527 data->pwm[i] = w83627ehf_read_value(data,
Rudolf Marek08c79952006-07-05 18:14:31 +0200528 W83627EHF_REG_PWM[i]);
David Hubbard1ea6dd32007-06-24 11:16:15 +0200529 data->fan_min_output[i] = w83627ehf_read_value(data,
Rudolf Marek08c79952006-07-05 18:14:31 +0200530 W83627EHF_REG_FAN_MIN_OUTPUT[i]);
David Hubbard1ea6dd32007-06-24 11:16:15 +0200531 data->fan_stop_time[i] = w83627ehf_read_value(data,
Rudolf Marek08c79952006-07-05 18:14:31 +0200532 W83627EHF_REG_FAN_STOP_TIME[i]);
533 data->target_temp[i] =
David Hubbard1ea6dd32007-06-24 11:16:15 +0200534 w83627ehf_read_value(data,
Rudolf Marek08c79952006-07-05 18:14:31 +0200535 W83627EHF_REG_TARGET[i]) &
536 (data->pwm_mode[i] == 1 ? 0x7f : 0xff);
537 data->tolerance[i] = (tolerance >> (i == 1 ? 4 : 0))
538 & 0x0f;
539 }
540
Jean Delvare08e7e272005-04-25 22:43:25 +0200541 /* Measured temperatures and limits */
David Hubbard1ea6dd32007-06-24 11:16:15 +0200542 data->temp1 = w83627ehf_read_value(data,
Jean Delvare08e7e272005-04-25 22:43:25 +0200543 W83627EHF_REG_TEMP1);
David Hubbard1ea6dd32007-06-24 11:16:15 +0200544 data->temp1_max = w83627ehf_read_value(data,
Jean Delvare08e7e272005-04-25 22:43:25 +0200545 W83627EHF_REG_TEMP1_OVER);
David Hubbard1ea6dd32007-06-24 11:16:15 +0200546 data->temp1_max_hyst = w83627ehf_read_value(data,
Jean Delvare08e7e272005-04-25 22:43:25 +0200547 W83627EHF_REG_TEMP1_HYST);
548 for (i = 0; i < 2; i++) {
David Hubbard1ea6dd32007-06-24 11:16:15 +0200549 data->temp[i] = w83627ehf_read_value(data,
Jean Delvare08e7e272005-04-25 22:43:25 +0200550 W83627EHF_REG_TEMP[i]);
David Hubbard1ea6dd32007-06-24 11:16:15 +0200551 data->temp_max[i] = w83627ehf_read_value(data,
Jean Delvare08e7e272005-04-25 22:43:25 +0200552 W83627EHF_REG_TEMP_OVER[i]);
David Hubbard1ea6dd32007-06-24 11:16:15 +0200553 data->temp_max_hyst[i] = w83627ehf_read_value(data,
Jean Delvare08e7e272005-04-25 22:43:25 +0200554 W83627EHF_REG_TEMP_HYST[i]);
555 }
556
David Hubbard1ea6dd32007-06-24 11:16:15 +0200557 data->alarms = w83627ehf_read_value(data,
Jean Delvarea4589db2006-03-23 16:30:29 +0100558 W83627EHF_REG_ALARM1) |
David Hubbard1ea6dd32007-06-24 11:16:15 +0200559 (w83627ehf_read_value(data,
Jean Delvarea4589db2006-03-23 16:30:29 +0100560 W83627EHF_REG_ALARM2) << 8) |
David Hubbard1ea6dd32007-06-24 11:16:15 +0200561 (w83627ehf_read_value(data,
Jean Delvarea4589db2006-03-23 16:30:29 +0100562 W83627EHF_REG_ALARM3) << 16);
563
Jean Delvare08e7e272005-04-25 22:43:25 +0200564 data->last_updated = jiffies;
565 data->valid = 1;
566 }
567
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100568 mutex_unlock(&data->update_lock);
Jean Delvare08e7e272005-04-25 22:43:25 +0200569 return data;
570}
571
572/*
573 * Sysfs callback functions
574 */
Rudolf Marekcf0676f2006-03-23 16:25:22 +0100575#define show_in_reg(reg) \
576static ssize_t \
577show_##reg(struct device *dev, struct device_attribute *attr, \
578 char *buf) \
579{ \
580 struct w83627ehf_data *data = w83627ehf_update_device(dev); \
581 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr); \
582 int nr = sensor_attr->index; \
583 return sprintf(buf, "%ld\n", in_from_reg(data->reg[nr], nr)); \
584}
585show_in_reg(in)
586show_in_reg(in_min)
587show_in_reg(in_max)
588
589#define store_in_reg(REG, reg) \
590static ssize_t \
591store_in_##reg (struct device *dev, struct device_attribute *attr, \
592 const char *buf, size_t count) \
593{ \
David Hubbard1ea6dd32007-06-24 11:16:15 +0200594 struct w83627ehf_data *data = dev_get_drvdata(dev); \
Rudolf Marekcf0676f2006-03-23 16:25:22 +0100595 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr); \
596 int nr = sensor_attr->index; \
597 u32 val = simple_strtoul(buf, NULL, 10); \
598 \
599 mutex_lock(&data->update_lock); \
600 data->in_##reg[nr] = in_to_reg(val, nr); \
David Hubbard1ea6dd32007-06-24 11:16:15 +0200601 w83627ehf_write_value(data, W83627EHF_REG_IN_##REG(nr), \
Rudolf Marekcf0676f2006-03-23 16:25:22 +0100602 data->in_##reg[nr]); \
603 mutex_unlock(&data->update_lock); \
604 return count; \
605}
606
607store_in_reg(MIN, min)
608store_in_reg(MAX, max)
609
Jean Delvarea4589db2006-03-23 16:30:29 +0100610static ssize_t show_alarm(struct device *dev, struct device_attribute *attr, char *buf)
611{
612 struct w83627ehf_data *data = w83627ehf_update_device(dev);
613 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
614 int nr = sensor_attr->index;
615 return sprintf(buf, "%u\n", (data->alarms >> nr) & 0x01);
616}
617
Rudolf Marekcf0676f2006-03-23 16:25:22 +0100618static struct sensor_device_attribute sda_in_input[] = {
619 SENSOR_ATTR(in0_input, S_IRUGO, show_in, NULL, 0),
620 SENSOR_ATTR(in1_input, S_IRUGO, show_in, NULL, 1),
621 SENSOR_ATTR(in2_input, S_IRUGO, show_in, NULL, 2),
622 SENSOR_ATTR(in3_input, S_IRUGO, show_in, NULL, 3),
623 SENSOR_ATTR(in4_input, S_IRUGO, show_in, NULL, 4),
624 SENSOR_ATTR(in5_input, S_IRUGO, show_in, NULL, 5),
625 SENSOR_ATTR(in6_input, S_IRUGO, show_in, NULL, 6),
626 SENSOR_ATTR(in7_input, S_IRUGO, show_in, NULL, 7),
627 SENSOR_ATTR(in8_input, S_IRUGO, show_in, NULL, 8),
628 SENSOR_ATTR(in9_input, S_IRUGO, show_in, NULL, 9),
629};
630
Jean Delvarea4589db2006-03-23 16:30:29 +0100631static struct sensor_device_attribute sda_in_alarm[] = {
632 SENSOR_ATTR(in0_alarm, S_IRUGO, show_alarm, NULL, 0),
633 SENSOR_ATTR(in1_alarm, S_IRUGO, show_alarm, NULL, 1),
634 SENSOR_ATTR(in2_alarm, S_IRUGO, show_alarm, NULL, 2),
635 SENSOR_ATTR(in3_alarm, S_IRUGO, show_alarm, NULL, 3),
636 SENSOR_ATTR(in4_alarm, S_IRUGO, show_alarm, NULL, 8),
637 SENSOR_ATTR(in5_alarm, S_IRUGO, show_alarm, NULL, 21),
638 SENSOR_ATTR(in6_alarm, S_IRUGO, show_alarm, NULL, 20),
639 SENSOR_ATTR(in7_alarm, S_IRUGO, show_alarm, NULL, 16),
640 SENSOR_ATTR(in8_alarm, S_IRUGO, show_alarm, NULL, 17),
641 SENSOR_ATTR(in9_alarm, S_IRUGO, show_alarm, NULL, 19),
642};
643
Rudolf Marekcf0676f2006-03-23 16:25:22 +0100644static struct sensor_device_attribute sda_in_min[] = {
645 SENSOR_ATTR(in0_min, S_IWUSR | S_IRUGO, show_in_min, store_in_min, 0),
646 SENSOR_ATTR(in1_min, S_IWUSR | S_IRUGO, show_in_min, store_in_min, 1),
647 SENSOR_ATTR(in2_min, S_IWUSR | S_IRUGO, show_in_min, store_in_min, 2),
648 SENSOR_ATTR(in3_min, S_IWUSR | S_IRUGO, show_in_min, store_in_min, 3),
649 SENSOR_ATTR(in4_min, S_IWUSR | S_IRUGO, show_in_min, store_in_min, 4),
650 SENSOR_ATTR(in5_min, S_IWUSR | S_IRUGO, show_in_min, store_in_min, 5),
651 SENSOR_ATTR(in6_min, S_IWUSR | S_IRUGO, show_in_min, store_in_min, 6),
652 SENSOR_ATTR(in7_min, S_IWUSR | S_IRUGO, show_in_min, store_in_min, 7),
653 SENSOR_ATTR(in8_min, S_IWUSR | S_IRUGO, show_in_min, store_in_min, 8),
654 SENSOR_ATTR(in9_min, S_IWUSR | S_IRUGO, show_in_min, store_in_min, 9),
655};
656
657static struct sensor_device_attribute sda_in_max[] = {
658 SENSOR_ATTR(in0_max, S_IWUSR | S_IRUGO, show_in_max, store_in_max, 0),
659 SENSOR_ATTR(in1_max, S_IWUSR | S_IRUGO, show_in_max, store_in_max, 1),
660 SENSOR_ATTR(in2_max, S_IWUSR | S_IRUGO, show_in_max, store_in_max, 2),
661 SENSOR_ATTR(in3_max, S_IWUSR | S_IRUGO, show_in_max, store_in_max, 3),
662 SENSOR_ATTR(in4_max, S_IWUSR | S_IRUGO, show_in_max, store_in_max, 4),
663 SENSOR_ATTR(in5_max, S_IWUSR | S_IRUGO, show_in_max, store_in_max, 5),
664 SENSOR_ATTR(in6_max, S_IWUSR | S_IRUGO, show_in_max, store_in_max, 6),
665 SENSOR_ATTR(in7_max, S_IWUSR | S_IRUGO, show_in_max, store_in_max, 7),
666 SENSOR_ATTR(in8_max, S_IWUSR | S_IRUGO, show_in_max, store_in_max, 8),
667 SENSOR_ATTR(in9_max, S_IWUSR | S_IRUGO, show_in_max, store_in_max, 9),
668};
669
Jean Delvare08e7e272005-04-25 22:43:25 +0200670#define show_fan_reg(reg) \
671static ssize_t \
Yuan Mu412fec82006-02-05 23:24:16 +0100672show_##reg(struct device *dev, struct device_attribute *attr, \
673 char *buf) \
Jean Delvare08e7e272005-04-25 22:43:25 +0200674{ \
675 struct w83627ehf_data *data = w83627ehf_update_device(dev); \
Yuan Mu412fec82006-02-05 23:24:16 +0100676 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr); \
677 int nr = sensor_attr->index; \
Jean Delvare08e7e272005-04-25 22:43:25 +0200678 return sprintf(buf, "%d\n", \
679 fan_from_reg(data->reg[nr], \
680 div_from_reg(data->fan_div[nr]))); \
681}
682show_fan_reg(fan);
683show_fan_reg(fan_min);
684
685static ssize_t
Yuan Mu412fec82006-02-05 23:24:16 +0100686show_fan_div(struct device *dev, struct device_attribute *attr,
687 char *buf)
Jean Delvare08e7e272005-04-25 22:43:25 +0200688{
689 struct w83627ehf_data *data = w83627ehf_update_device(dev);
Yuan Mu412fec82006-02-05 23:24:16 +0100690 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
691 int nr = sensor_attr->index;
692 return sprintf(buf, "%u\n", div_from_reg(data->fan_div[nr]));
Jean Delvare08e7e272005-04-25 22:43:25 +0200693}
694
695static ssize_t
Yuan Mu412fec82006-02-05 23:24:16 +0100696store_fan_min(struct device *dev, struct device_attribute *attr,
697 const char *buf, size_t count)
Jean Delvare08e7e272005-04-25 22:43:25 +0200698{
David Hubbard1ea6dd32007-06-24 11:16:15 +0200699 struct w83627ehf_data *data = dev_get_drvdata(dev);
Yuan Mu412fec82006-02-05 23:24:16 +0100700 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
701 int nr = sensor_attr->index;
Jean Delvare08e7e272005-04-25 22:43:25 +0200702 unsigned int val = simple_strtoul(buf, NULL, 10);
703 unsigned int reg;
704 u8 new_div;
705
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100706 mutex_lock(&data->update_lock);
Jean Delvare08e7e272005-04-25 22:43:25 +0200707 if (!val) {
708 /* No min limit, alarm disabled */
709 data->fan_min[nr] = 255;
710 new_div = data->fan_div[nr]; /* No change */
711 dev_info(dev, "fan%u low limit and alarm disabled\n", nr + 1);
712 } else if ((reg = 1350000U / val) >= 128 * 255) {
713 /* Speed below this value cannot possibly be represented,
714 even with the highest divider (128) */
715 data->fan_min[nr] = 254;
716 new_div = 7; /* 128 == (1 << 7) */
717 dev_warn(dev, "fan%u low limit %u below minimum %u, set to "
718 "minimum\n", nr + 1, val, fan_from_reg(254, 128));
719 } else if (!reg) {
720 /* Speed above this value cannot possibly be represented,
721 even with the lowest divider (1) */
722 data->fan_min[nr] = 1;
723 new_div = 0; /* 1 == (1 << 0) */
724 dev_warn(dev, "fan%u low limit %u above maximum %u, set to "
Jean Delvareb9110b12005-05-02 23:08:22 +0200725 "maximum\n", nr + 1, val, fan_from_reg(1, 1));
Jean Delvare08e7e272005-04-25 22:43:25 +0200726 } else {
727 /* Automatically pick the best divider, i.e. the one such
728 that the min limit will correspond to a register value
729 in the 96..192 range */
730 new_div = 0;
731 while (reg > 192 && new_div < 7) {
732 reg >>= 1;
733 new_div++;
734 }
735 data->fan_min[nr] = reg;
736 }
737
738 /* Write both the fan clock divider (if it changed) and the new
739 fan min (unconditionally) */
740 if (new_div != data->fan_div[nr]) {
Jean Delvare158ce072007-06-17 16:09:12 +0200741 /* Preserve the fan speed reading */
742 if (data->fan[nr] != 0xff) {
743 if (new_div > data->fan_div[nr])
744 data->fan[nr] >>= new_div - data->fan_div[nr];
745 else if (data->fan[nr] & 0x80)
746 data->fan[nr] = 0xff;
747 else
748 data->fan[nr] <<= data->fan_div[nr] - new_div;
749 }
Jean Delvare08e7e272005-04-25 22:43:25 +0200750
751 dev_dbg(dev, "fan%u clock divider changed from %u to %u\n",
752 nr + 1, div_from_reg(data->fan_div[nr]),
753 div_from_reg(new_div));
754 data->fan_div[nr] = new_div;
David Hubbard1ea6dd32007-06-24 11:16:15 +0200755 w83627ehf_write_fan_div(data, nr);
Jean Delvare6b3e4642007-06-24 11:19:01 +0200756 /* Give the chip time to sample a new speed value */
757 data->last_updated = jiffies;
Jean Delvare08e7e272005-04-25 22:43:25 +0200758 }
David Hubbard1ea6dd32007-06-24 11:16:15 +0200759 w83627ehf_write_value(data, W83627EHF_REG_FAN_MIN[nr],
Jean Delvare08e7e272005-04-25 22:43:25 +0200760 data->fan_min[nr]);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100761 mutex_unlock(&data->update_lock);
Jean Delvare08e7e272005-04-25 22:43:25 +0200762
763 return count;
764}
765
Yuan Mu412fec82006-02-05 23:24:16 +0100766static struct sensor_device_attribute sda_fan_input[] = {
767 SENSOR_ATTR(fan1_input, S_IRUGO, show_fan, NULL, 0),
768 SENSOR_ATTR(fan2_input, S_IRUGO, show_fan, NULL, 1),
769 SENSOR_ATTR(fan3_input, S_IRUGO, show_fan, NULL, 2),
770 SENSOR_ATTR(fan4_input, S_IRUGO, show_fan, NULL, 3),
771 SENSOR_ATTR(fan5_input, S_IRUGO, show_fan, NULL, 4),
772};
Jean Delvare08e7e272005-04-25 22:43:25 +0200773
Jean Delvarea4589db2006-03-23 16:30:29 +0100774static struct sensor_device_attribute sda_fan_alarm[] = {
775 SENSOR_ATTR(fan1_alarm, S_IRUGO, show_alarm, NULL, 6),
776 SENSOR_ATTR(fan2_alarm, S_IRUGO, show_alarm, NULL, 7),
777 SENSOR_ATTR(fan3_alarm, S_IRUGO, show_alarm, NULL, 11),
778 SENSOR_ATTR(fan4_alarm, S_IRUGO, show_alarm, NULL, 10),
779 SENSOR_ATTR(fan5_alarm, S_IRUGO, show_alarm, NULL, 23),
780};
781
Yuan Mu412fec82006-02-05 23:24:16 +0100782static struct sensor_device_attribute sda_fan_min[] = {
783 SENSOR_ATTR(fan1_min, S_IWUSR | S_IRUGO, show_fan_min,
784 store_fan_min, 0),
785 SENSOR_ATTR(fan2_min, S_IWUSR | S_IRUGO, show_fan_min,
786 store_fan_min, 1),
787 SENSOR_ATTR(fan3_min, S_IWUSR | S_IRUGO, show_fan_min,
788 store_fan_min, 2),
789 SENSOR_ATTR(fan4_min, S_IWUSR | S_IRUGO, show_fan_min,
790 store_fan_min, 3),
791 SENSOR_ATTR(fan5_min, S_IWUSR | S_IRUGO, show_fan_min,
792 store_fan_min, 4),
793};
Jean Delvare08e7e272005-04-25 22:43:25 +0200794
Yuan Mu412fec82006-02-05 23:24:16 +0100795static struct sensor_device_attribute sda_fan_div[] = {
796 SENSOR_ATTR(fan1_div, S_IRUGO, show_fan_div, NULL, 0),
797 SENSOR_ATTR(fan2_div, S_IRUGO, show_fan_div, NULL, 1),
798 SENSOR_ATTR(fan3_div, S_IRUGO, show_fan_div, NULL, 2),
799 SENSOR_ATTR(fan4_div, S_IRUGO, show_fan_div, NULL, 3),
800 SENSOR_ATTR(fan5_div, S_IRUGO, show_fan_div, NULL, 4),
801};
Jean Delvare08e7e272005-04-25 22:43:25 +0200802
Jean Delvare08e7e272005-04-25 22:43:25 +0200803#define show_temp1_reg(reg) \
804static ssize_t \
Greg Kroah-Hartman6f637a62005-06-21 21:01:59 -0700805show_##reg(struct device *dev, struct device_attribute *attr, \
806 char *buf) \
Jean Delvare08e7e272005-04-25 22:43:25 +0200807{ \
808 struct w83627ehf_data *data = w83627ehf_update_device(dev); \
809 return sprintf(buf, "%d\n", temp1_from_reg(data->reg)); \
810}
811show_temp1_reg(temp1);
812show_temp1_reg(temp1_max);
813show_temp1_reg(temp1_max_hyst);
814
815#define store_temp1_reg(REG, reg) \
816static ssize_t \
Greg Kroah-Hartman6f637a62005-06-21 21:01:59 -0700817store_temp1_##reg(struct device *dev, struct device_attribute *attr, \
818 const char *buf, size_t count) \
Jean Delvare08e7e272005-04-25 22:43:25 +0200819{ \
David Hubbard1ea6dd32007-06-24 11:16:15 +0200820 struct w83627ehf_data *data = dev_get_drvdata(dev); \
Christian Hohnstaedt5bfedac2007-08-16 11:40:10 +0200821 long val = simple_strtol(buf, NULL, 10); \
Jean Delvare08e7e272005-04-25 22:43:25 +0200822 \
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100823 mutex_lock(&data->update_lock); \
Rudolf Marek08c79952006-07-05 18:14:31 +0200824 data->temp1_##reg = temp1_to_reg(val, -128000, 127000); \
David Hubbard1ea6dd32007-06-24 11:16:15 +0200825 w83627ehf_write_value(data, W83627EHF_REG_TEMP1_##REG, \
Jean Delvare08e7e272005-04-25 22:43:25 +0200826 data->temp1_##reg); \
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100827 mutex_unlock(&data->update_lock); \
Jean Delvare08e7e272005-04-25 22:43:25 +0200828 return count; \
829}
830store_temp1_reg(OVER, max);
831store_temp1_reg(HYST, max_hyst);
832
Jean Delvare08e7e272005-04-25 22:43:25 +0200833#define show_temp_reg(reg) \
834static ssize_t \
Yuan Mu412fec82006-02-05 23:24:16 +0100835show_##reg(struct device *dev, struct device_attribute *attr, \
836 char *buf) \
Jean Delvare08e7e272005-04-25 22:43:25 +0200837{ \
838 struct w83627ehf_data *data = w83627ehf_update_device(dev); \
Yuan Mu412fec82006-02-05 23:24:16 +0100839 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr); \
840 int nr = sensor_attr->index; \
Jean Delvare08e7e272005-04-25 22:43:25 +0200841 return sprintf(buf, "%d\n", \
842 LM75_TEMP_FROM_REG(data->reg[nr])); \
843}
844show_temp_reg(temp);
845show_temp_reg(temp_max);
846show_temp_reg(temp_max_hyst);
847
848#define store_temp_reg(REG, reg) \
849static ssize_t \
Yuan Mu412fec82006-02-05 23:24:16 +0100850store_##reg(struct device *dev, struct device_attribute *attr, \
851 const char *buf, size_t count) \
Jean Delvare08e7e272005-04-25 22:43:25 +0200852{ \
David Hubbard1ea6dd32007-06-24 11:16:15 +0200853 struct w83627ehf_data *data = dev_get_drvdata(dev); \
Yuan Mu412fec82006-02-05 23:24:16 +0100854 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr); \
855 int nr = sensor_attr->index; \
Christian Hohnstaedt5bfedac2007-08-16 11:40:10 +0200856 long val = simple_strtol(buf, NULL, 10); \
Jean Delvare08e7e272005-04-25 22:43:25 +0200857 \
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100858 mutex_lock(&data->update_lock); \
Jean Delvare08e7e272005-04-25 22:43:25 +0200859 data->reg[nr] = LM75_TEMP_TO_REG(val); \
David Hubbard1ea6dd32007-06-24 11:16:15 +0200860 w83627ehf_write_value(data, W83627EHF_REG_TEMP_##REG[nr], \
Jean Delvare08e7e272005-04-25 22:43:25 +0200861 data->reg[nr]); \
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100862 mutex_unlock(&data->update_lock); \
Jean Delvare08e7e272005-04-25 22:43:25 +0200863 return count; \
864}
865store_temp_reg(OVER, temp_max);
866store_temp_reg(HYST, temp_max_hyst);
867
Jean Delvareda667362007-06-24 11:21:02 +0200868static ssize_t
869show_temp_type(struct device *dev, struct device_attribute *attr, char *buf)
870{
871 struct w83627ehf_data *data = w83627ehf_update_device(dev);
872 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
873 int nr = sensor_attr->index;
874 return sprintf(buf, "%d\n", (int)data->temp_type[nr]);
875}
876
Gong Juna157d062009-03-30 21:46:43 +0200877static struct sensor_device_attribute sda_temp_input[] = {
Yuan Mu412fec82006-02-05 23:24:16 +0100878 SENSOR_ATTR(temp1_input, S_IRUGO, show_temp1, NULL, 0),
879 SENSOR_ATTR(temp2_input, S_IRUGO, show_temp, NULL, 0),
880 SENSOR_ATTR(temp3_input, S_IRUGO, show_temp, NULL, 1),
Gong Juna157d062009-03-30 21:46:43 +0200881};
882
883static struct sensor_device_attribute sda_temp_max[] = {
Yuan Mu412fec82006-02-05 23:24:16 +0100884 SENSOR_ATTR(temp1_max, S_IRUGO | S_IWUSR, show_temp1_max,
885 store_temp1_max, 0),
886 SENSOR_ATTR(temp2_max, S_IRUGO | S_IWUSR, show_temp_max,
887 store_temp_max, 0),
888 SENSOR_ATTR(temp3_max, S_IRUGO | S_IWUSR, show_temp_max,
889 store_temp_max, 1),
Gong Juna157d062009-03-30 21:46:43 +0200890};
891
892static struct sensor_device_attribute sda_temp_max_hyst[] = {
Yuan Mu412fec82006-02-05 23:24:16 +0100893 SENSOR_ATTR(temp1_max_hyst, S_IRUGO | S_IWUSR, show_temp1_max_hyst,
894 store_temp1_max_hyst, 0),
895 SENSOR_ATTR(temp2_max_hyst, S_IRUGO | S_IWUSR, show_temp_max_hyst,
896 store_temp_max_hyst, 0),
897 SENSOR_ATTR(temp3_max_hyst, S_IRUGO | S_IWUSR, show_temp_max_hyst,
898 store_temp_max_hyst, 1),
Gong Juna157d062009-03-30 21:46:43 +0200899};
900
901static struct sensor_device_attribute sda_temp_alarm[] = {
Jean Delvarea4589db2006-03-23 16:30:29 +0100902 SENSOR_ATTR(temp1_alarm, S_IRUGO, show_alarm, NULL, 4),
903 SENSOR_ATTR(temp2_alarm, S_IRUGO, show_alarm, NULL, 5),
904 SENSOR_ATTR(temp3_alarm, S_IRUGO, show_alarm, NULL, 13),
Gong Juna157d062009-03-30 21:46:43 +0200905};
906
907static struct sensor_device_attribute sda_temp_type[] = {
Jean Delvareda667362007-06-24 11:21:02 +0200908 SENSOR_ATTR(temp1_type, S_IRUGO, show_temp_type, NULL, 0),
909 SENSOR_ATTR(temp2_type, S_IRUGO, show_temp_type, NULL, 1),
910 SENSOR_ATTR(temp3_type, S_IRUGO, show_temp_type, NULL, 2),
Yuan Mu412fec82006-02-05 23:24:16 +0100911};
Jean Delvare08e7e272005-04-25 22:43:25 +0200912
Rudolf Marek08c79952006-07-05 18:14:31 +0200913#define show_pwm_reg(reg) \
914static ssize_t show_##reg (struct device *dev, struct device_attribute *attr, \
915 char *buf) \
916{ \
917 struct w83627ehf_data *data = w83627ehf_update_device(dev); \
918 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr); \
919 int nr = sensor_attr->index; \
920 return sprintf(buf, "%d\n", data->reg[nr]); \
921}
922
923show_pwm_reg(pwm_mode)
924show_pwm_reg(pwm_enable)
925show_pwm_reg(pwm)
926
927static ssize_t
928store_pwm_mode(struct device *dev, struct device_attribute *attr,
929 const char *buf, size_t count)
930{
David Hubbard1ea6dd32007-06-24 11:16:15 +0200931 struct w83627ehf_data *data = dev_get_drvdata(dev);
Rudolf Marek08c79952006-07-05 18:14:31 +0200932 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
933 int nr = sensor_attr->index;
934 u32 val = simple_strtoul(buf, NULL, 10);
935 u16 reg;
936
937 if (val > 1)
938 return -EINVAL;
939 mutex_lock(&data->update_lock);
David Hubbard1ea6dd32007-06-24 11:16:15 +0200940 reg = w83627ehf_read_value(data, W83627EHF_REG_PWM_ENABLE[nr]);
Rudolf Marek08c79952006-07-05 18:14:31 +0200941 data->pwm_mode[nr] = val;
942 reg &= ~(1 << W83627EHF_PWM_MODE_SHIFT[nr]);
943 if (!val)
944 reg |= 1 << W83627EHF_PWM_MODE_SHIFT[nr];
David Hubbard1ea6dd32007-06-24 11:16:15 +0200945 w83627ehf_write_value(data, W83627EHF_REG_PWM_ENABLE[nr], reg);
Rudolf Marek08c79952006-07-05 18:14:31 +0200946 mutex_unlock(&data->update_lock);
947 return count;
948}
949
950static ssize_t
951store_pwm(struct device *dev, struct device_attribute *attr,
952 const char *buf, size_t count)
953{
David Hubbard1ea6dd32007-06-24 11:16:15 +0200954 struct w83627ehf_data *data = dev_get_drvdata(dev);
Rudolf Marek08c79952006-07-05 18:14:31 +0200955 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
956 int nr = sensor_attr->index;
957 u32 val = SENSORS_LIMIT(simple_strtoul(buf, NULL, 10), 0, 255);
958
959 mutex_lock(&data->update_lock);
960 data->pwm[nr] = val;
David Hubbard1ea6dd32007-06-24 11:16:15 +0200961 w83627ehf_write_value(data, W83627EHF_REG_PWM[nr], val);
Rudolf Marek08c79952006-07-05 18:14:31 +0200962 mutex_unlock(&data->update_lock);
963 return count;
964}
965
966static ssize_t
967store_pwm_enable(struct device *dev, struct device_attribute *attr,
968 const char *buf, size_t count)
969{
David Hubbard1ea6dd32007-06-24 11:16:15 +0200970 struct w83627ehf_data *data = dev_get_drvdata(dev);
Rudolf Marek08c79952006-07-05 18:14:31 +0200971 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
972 int nr = sensor_attr->index;
973 u32 val = simple_strtoul(buf, NULL, 10);
974 u16 reg;
975
976 if (!val || (val > 2)) /* only modes 1 and 2 are supported */
977 return -EINVAL;
978 mutex_lock(&data->update_lock);
David Hubbard1ea6dd32007-06-24 11:16:15 +0200979 reg = w83627ehf_read_value(data, W83627EHF_REG_PWM_ENABLE[nr]);
Rudolf Marek08c79952006-07-05 18:14:31 +0200980 data->pwm_enable[nr] = val;
981 reg &= ~(0x03 << W83627EHF_PWM_ENABLE_SHIFT[nr]);
982 reg |= (val - 1) << W83627EHF_PWM_ENABLE_SHIFT[nr];
David Hubbard1ea6dd32007-06-24 11:16:15 +0200983 w83627ehf_write_value(data, W83627EHF_REG_PWM_ENABLE[nr], reg);
Rudolf Marek08c79952006-07-05 18:14:31 +0200984 mutex_unlock(&data->update_lock);
985 return count;
986}
987
988
989#define show_tol_temp(reg) \
990static ssize_t show_##reg(struct device *dev, struct device_attribute *attr, \
991 char *buf) \
992{ \
993 struct w83627ehf_data *data = w83627ehf_update_device(dev); \
994 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr); \
995 int nr = sensor_attr->index; \
996 return sprintf(buf, "%d\n", temp1_from_reg(data->reg[nr])); \
997}
998
999show_tol_temp(tolerance)
1000show_tol_temp(target_temp)
1001
1002static ssize_t
1003store_target_temp(struct device *dev, struct device_attribute *attr,
1004 const char *buf, size_t count)
1005{
David Hubbard1ea6dd32007-06-24 11:16:15 +02001006 struct w83627ehf_data *data = dev_get_drvdata(dev);
Rudolf Marek08c79952006-07-05 18:14:31 +02001007 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
1008 int nr = sensor_attr->index;
1009 u8 val = temp1_to_reg(simple_strtoul(buf, NULL, 10), 0, 127000);
1010
1011 mutex_lock(&data->update_lock);
1012 data->target_temp[nr] = val;
David Hubbard1ea6dd32007-06-24 11:16:15 +02001013 w83627ehf_write_value(data, W83627EHF_REG_TARGET[nr], val);
Rudolf Marek08c79952006-07-05 18:14:31 +02001014 mutex_unlock(&data->update_lock);
1015 return count;
1016}
1017
1018static ssize_t
1019store_tolerance(struct device *dev, struct device_attribute *attr,
1020 const char *buf, size_t count)
1021{
David Hubbard1ea6dd32007-06-24 11:16:15 +02001022 struct w83627ehf_data *data = dev_get_drvdata(dev);
Rudolf Marek08c79952006-07-05 18:14:31 +02001023 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
1024 int nr = sensor_attr->index;
1025 u16 reg;
1026 /* Limit the temp to 0C - 15C */
1027 u8 val = temp1_to_reg(simple_strtoul(buf, NULL, 10), 0, 15000);
1028
1029 mutex_lock(&data->update_lock);
David Hubbard1ea6dd32007-06-24 11:16:15 +02001030 reg = w83627ehf_read_value(data, W83627EHF_REG_TOLERANCE[nr]);
Rudolf Marek08c79952006-07-05 18:14:31 +02001031 data->tolerance[nr] = val;
1032 if (nr == 1)
1033 reg = (reg & 0x0f) | (val << 4);
1034 else
1035 reg = (reg & 0xf0) | val;
David Hubbard1ea6dd32007-06-24 11:16:15 +02001036 w83627ehf_write_value(data, W83627EHF_REG_TOLERANCE[nr], reg);
Rudolf Marek08c79952006-07-05 18:14:31 +02001037 mutex_unlock(&data->update_lock);
1038 return count;
1039}
1040
1041static struct sensor_device_attribute sda_pwm[] = {
1042 SENSOR_ATTR(pwm1, S_IWUSR | S_IRUGO, show_pwm, store_pwm, 0),
1043 SENSOR_ATTR(pwm2, S_IWUSR | S_IRUGO, show_pwm, store_pwm, 1),
1044 SENSOR_ATTR(pwm3, S_IWUSR | S_IRUGO, show_pwm, store_pwm, 2),
1045 SENSOR_ATTR(pwm4, S_IWUSR | S_IRUGO, show_pwm, store_pwm, 3),
1046};
1047
1048static struct sensor_device_attribute sda_pwm_mode[] = {
1049 SENSOR_ATTR(pwm1_mode, S_IWUSR | S_IRUGO, show_pwm_mode,
1050 store_pwm_mode, 0),
1051 SENSOR_ATTR(pwm2_mode, S_IWUSR | S_IRUGO, show_pwm_mode,
1052 store_pwm_mode, 1),
1053 SENSOR_ATTR(pwm3_mode, S_IWUSR | S_IRUGO, show_pwm_mode,
1054 store_pwm_mode, 2),
1055 SENSOR_ATTR(pwm4_mode, S_IWUSR | S_IRUGO, show_pwm_mode,
1056 store_pwm_mode, 3),
1057};
1058
1059static struct sensor_device_attribute sda_pwm_enable[] = {
1060 SENSOR_ATTR(pwm1_enable, S_IWUSR | S_IRUGO, show_pwm_enable,
1061 store_pwm_enable, 0),
1062 SENSOR_ATTR(pwm2_enable, S_IWUSR | S_IRUGO, show_pwm_enable,
1063 store_pwm_enable, 1),
1064 SENSOR_ATTR(pwm3_enable, S_IWUSR | S_IRUGO, show_pwm_enable,
1065 store_pwm_enable, 2),
1066 SENSOR_ATTR(pwm4_enable, S_IWUSR | S_IRUGO, show_pwm_enable,
1067 store_pwm_enable, 3),
1068};
1069
1070static struct sensor_device_attribute sda_target_temp[] = {
1071 SENSOR_ATTR(pwm1_target, S_IWUSR | S_IRUGO, show_target_temp,
1072 store_target_temp, 0),
1073 SENSOR_ATTR(pwm2_target, S_IWUSR | S_IRUGO, show_target_temp,
1074 store_target_temp, 1),
1075 SENSOR_ATTR(pwm3_target, S_IWUSR | S_IRUGO, show_target_temp,
1076 store_target_temp, 2),
1077 SENSOR_ATTR(pwm4_target, S_IWUSR | S_IRUGO, show_target_temp,
1078 store_target_temp, 3),
1079};
1080
1081static struct sensor_device_attribute sda_tolerance[] = {
1082 SENSOR_ATTR(pwm1_tolerance, S_IWUSR | S_IRUGO, show_tolerance,
1083 store_tolerance, 0),
1084 SENSOR_ATTR(pwm2_tolerance, S_IWUSR | S_IRUGO, show_tolerance,
1085 store_tolerance, 1),
1086 SENSOR_ATTR(pwm3_tolerance, S_IWUSR | S_IRUGO, show_tolerance,
1087 store_tolerance, 2),
1088 SENSOR_ATTR(pwm4_tolerance, S_IWUSR | S_IRUGO, show_tolerance,
1089 store_tolerance, 3),
1090};
1091
Rudolf Marek08c79952006-07-05 18:14:31 +02001092/* Smart Fan registers */
1093
1094#define fan_functions(reg, REG) \
1095static ssize_t show_##reg(struct device *dev, struct device_attribute *attr, \
1096 char *buf) \
1097{ \
1098 struct w83627ehf_data *data = w83627ehf_update_device(dev); \
1099 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr); \
1100 int nr = sensor_attr->index; \
1101 return sprintf(buf, "%d\n", data->reg[nr]); \
1102}\
1103static ssize_t \
1104store_##reg(struct device *dev, struct device_attribute *attr, \
1105 const char *buf, size_t count) \
1106{\
David Hubbard1ea6dd32007-06-24 11:16:15 +02001107 struct w83627ehf_data *data = dev_get_drvdata(dev); \
Rudolf Marek08c79952006-07-05 18:14:31 +02001108 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr); \
1109 int nr = sensor_attr->index; \
1110 u32 val = SENSORS_LIMIT(simple_strtoul(buf, NULL, 10), 1, 255); \
1111 mutex_lock(&data->update_lock); \
1112 data->reg[nr] = val; \
David Hubbard1ea6dd32007-06-24 11:16:15 +02001113 w83627ehf_write_value(data, W83627EHF_REG_##REG[nr], val); \
Rudolf Marek08c79952006-07-05 18:14:31 +02001114 mutex_unlock(&data->update_lock); \
1115 return count; \
1116}
1117
1118fan_functions(fan_min_output, FAN_MIN_OUTPUT)
1119
1120#define fan_time_functions(reg, REG) \
1121static ssize_t show_##reg(struct device *dev, struct device_attribute *attr, \
1122 char *buf) \
1123{ \
1124 struct w83627ehf_data *data = w83627ehf_update_device(dev); \
1125 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr); \
1126 int nr = sensor_attr->index; \
1127 return sprintf(buf, "%d\n", \
1128 step_time_from_reg(data->reg[nr], data->pwm_mode[nr])); \
1129} \
1130\
1131static ssize_t \
1132store_##reg(struct device *dev, struct device_attribute *attr, \
1133 const char *buf, size_t count) \
1134{ \
David Hubbard1ea6dd32007-06-24 11:16:15 +02001135 struct w83627ehf_data *data = dev_get_drvdata(dev); \
Rudolf Marek08c79952006-07-05 18:14:31 +02001136 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr); \
1137 int nr = sensor_attr->index; \
1138 u8 val = step_time_to_reg(simple_strtoul(buf, NULL, 10), \
1139 data->pwm_mode[nr]); \
1140 mutex_lock(&data->update_lock); \
1141 data->reg[nr] = val; \
David Hubbard1ea6dd32007-06-24 11:16:15 +02001142 w83627ehf_write_value(data, W83627EHF_REG_##REG[nr], val); \
Rudolf Marek08c79952006-07-05 18:14:31 +02001143 mutex_unlock(&data->update_lock); \
1144 return count; \
1145} \
1146
1147fan_time_functions(fan_stop_time, FAN_STOP_TIME)
1148
David Hubbard1ea6dd32007-06-24 11:16:15 +02001149static ssize_t show_name(struct device *dev, struct device_attribute *attr,
1150 char *buf)
1151{
1152 struct w83627ehf_data *data = dev_get_drvdata(dev);
1153
1154 return sprintf(buf, "%s\n", data->name);
1155}
1156static DEVICE_ATTR(name, S_IRUGO, show_name, NULL);
Rudolf Marek08c79952006-07-05 18:14:31 +02001157
1158static struct sensor_device_attribute sda_sf3_arrays_fan4[] = {
1159 SENSOR_ATTR(pwm4_stop_time, S_IWUSR | S_IRUGO, show_fan_stop_time,
1160 store_fan_stop_time, 3),
1161 SENSOR_ATTR(pwm4_min_output, S_IWUSR | S_IRUGO, show_fan_min_output,
1162 store_fan_min_output, 3),
1163};
1164
1165static struct sensor_device_attribute sda_sf3_arrays[] = {
1166 SENSOR_ATTR(pwm1_stop_time, S_IWUSR | S_IRUGO, show_fan_stop_time,
1167 store_fan_stop_time, 0),
1168 SENSOR_ATTR(pwm2_stop_time, S_IWUSR | S_IRUGO, show_fan_stop_time,
1169 store_fan_stop_time, 1),
1170 SENSOR_ATTR(pwm3_stop_time, S_IWUSR | S_IRUGO, show_fan_stop_time,
1171 store_fan_stop_time, 2),
1172 SENSOR_ATTR(pwm1_min_output, S_IWUSR | S_IRUGO, show_fan_min_output,
1173 store_fan_min_output, 0),
1174 SENSOR_ATTR(pwm2_min_output, S_IWUSR | S_IRUGO, show_fan_min_output,
1175 store_fan_min_output, 1),
1176 SENSOR_ATTR(pwm3_min_output, S_IWUSR | S_IRUGO, show_fan_min_output,
1177 store_fan_min_output, 2),
1178};
1179
Jean Delvarefc18d6c2007-06-24 11:19:42 +02001180static ssize_t
1181show_vid(struct device *dev, struct device_attribute *attr, char *buf)
1182{
1183 struct w83627ehf_data *data = dev_get_drvdata(dev);
1184 return sprintf(buf, "%d\n", vid_from_reg(data->vid, data->vrm));
1185}
1186static DEVICE_ATTR(cpu0_vid, S_IRUGO, show_vid, NULL);
1187
Jean Delvare08e7e272005-04-25 22:43:25 +02001188/*
David Hubbard1ea6dd32007-06-24 11:16:15 +02001189 * Driver and device management
Jean Delvare08e7e272005-04-25 22:43:25 +02001190 */
1191
David Hubbardc18beb52006-09-24 21:04:38 +02001192static void w83627ehf_device_remove_files(struct device *dev)
1193{
1194 /* some entries in the following arrays may not have been used in
1195 * device_create_file(), but device_remove_file() will ignore them */
1196 int i;
David Hubbard1ea6dd32007-06-24 11:16:15 +02001197 struct w83627ehf_data *data = dev_get_drvdata(dev);
David Hubbardc18beb52006-09-24 21:04:38 +02001198
1199 for (i = 0; i < ARRAY_SIZE(sda_sf3_arrays); i++)
1200 device_remove_file(dev, &sda_sf3_arrays[i].dev_attr);
1201 for (i = 0; i < ARRAY_SIZE(sda_sf3_arrays_fan4); i++)
1202 device_remove_file(dev, &sda_sf3_arrays_fan4[i].dev_attr);
David Hubbard1ea6dd32007-06-24 11:16:15 +02001203 for (i = 0; i < data->in_num; i++) {
Gong Juna157d062009-03-30 21:46:43 +02001204 if ((i == 6) && data->in6_skip)
1205 continue;
David Hubbardc18beb52006-09-24 21:04:38 +02001206 device_remove_file(dev, &sda_in_input[i].dev_attr);
1207 device_remove_file(dev, &sda_in_alarm[i].dev_attr);
1208 device_remove_file(dev, &sda_in_min[i].dev_attr);
1209 device_remove_file(dev, &sda_in_max[i].dev_attr);
1210 }
1211 for (i = 0; i < 5; i++) {
1212 device_remove_file(dev, &sda_fan_input[i].dev_attr);
1213 device_remove_file(dev, &sda_fan_alarm[i].dev_attr);
1214 device_remove_file(dev, &sda_fan_div[i].dev_attr);
1215 device_remove_file(dev, &sda_fan_min[i].dev_attr);
1216 }
Gong Jun237c8d22009-03-30 21:46:42 +02001217 for (i = 0; i < data->pwm_num; i++) {
David Hubbardc18beb52006-09-24 21:04:38 +02001218 device_remove_file(dev, &sda_pwm[i].dev_attr);
1219 device_remove_file(dev, &sda_pwm_mode[i].dev_attr);
1220 device_remove_file(dev, &sda_pwm_enable[i].dev_attr);
1221 device_remove_file(dev, &sda_target_temp[i].dev_attr);
1222 device_remove_file(dev, &sda_tolerance[i].dev_attr);
1223 }
Gong Juna157d062009-03-30 21:46:43 +02001224 for (i = 0; i < 3; i++) {
1225 if ((i == 2) && data->temp3_disable)
1226 continue;
1227 device_remove_file(dev, &sda_temp_input[i].dev_attr);
1228 device_remove_file(dev, &sda_temp_max[i].dev_attr);
1229 device_remove_file(dev, &sda_temp_max_hyst[i].dev_attr);
1230 device_remove_file(dev, &sda_temp_alarm[i].dev_attr);
1231 device_remove_file(dev, &sda_temp_type[i].dev_attr);
1232 }
David Hubbard1ea6dd32007-06-24 11:16:15 +02001233
1234 device_remove_file(dev, &dev_attr_name);
Jean Delvarecbe311f2008-01-03 21:22:44 +01001235 device_remove_file(dev, &dev_attr_cpu0_vid);
David Hubbardc18beb52006-09-24 21:04:38 +02001236}
1237
David Hubbard1ea6dd32007-06-24 11:16:15 +02001238/* Get the monitoring functions started */
1239static inline void __devinit w83627ehf_init_device(struct w83627ehf_data *data)
Jean Delvare08e7e272005-04-25 22:43:25 +02001240{
1241 int i;
Jean Delvareda667362007-06-24 11:21:02 +02001242 u8 tmp, diode;
Jean Delvare08e7e272005-04-25 22:43:25 +02001243
1244 /* Start monitoring is needed */
David Hubbard1ea6dd32007-06-24 11:16:15 +02001245 tmp = w83627ehf_read_value(data, W83627EHF_REG_CONFIG);
Jean Delvare08e7e272005-04-25 22:43:25 +02001246 if (!(tmp & 0x01))
David Hubbard1ea6dd32007-06-24 11:16:15 +02001247 w83627ehf_write_value(data, W83627EHF_REG_CONFIG,
Jean Delvare08e7e272005-04-25 22:43:25 +02001248 tmp | 0x01);
1249
1250 /* Enable temp2 and temp3 if needed */
1251 for (i = 0; i < 2; i++) {
David Hubbard1ea6dd32007-06-24 11:16:15 +02001252 tmp = w83627ehf_read_value(data,
Jean Delvare08e7e272005-04-25 22:43:25 +02001253 W83627EHF_REG_TEMP_CONFIG[i]);
Gong Juna157d062009-03-30 21:46:43 +02001254 if ((i == 1) && data->temp3_disable)
1255 continue;
Jean Delvare08e7e272005-04-25 22:43:25 +02001256 if (tmp & 0x01)
David Hubbard1ea6dd32007-06-24 11:16:15 +02001257 w83627ehf_write_value(data,
Jean Delvare08e7e272005-04-25 22:43:25 +02001258 W83627EHF_REG_TEMP_CONFIG[i],
1259 tmp & 0xfe);
1260 }
Jean Delvared3130f02007-06-24 11:20:13 +02001261
1262 /* Enable VBAT monitoring if needed */
1263 tmp = w83627ehf_read_value(data, W83627EHF_REG_VBAT);
1264 if (!(tmp & 0x01))
1265 w83627ehf_write_value(data, W83627EHF_REG_VBAT, tmp | 0x01);
Jean Delvareda667362007-06-24 11:21:02 +02001266
1267 /* Get thermal sensor types */
1268 diode = w83627ehf_read_value(data, W83627EHF_REG_DIODE);
1269 for (i = 0; i < 3; i++) {
1270 if ((tmp & (0x02 << i)))
1271 data->temp_type[i] = (diode & (0x10 << i)) ? 1 : 2;
1272 else
1273 data->temp_type[i] = 4; /* thermistor */
1274 }
Jean Delvare08e7e272005-04-25 22:43:25 +02001275}
1276
David Hubbard1ea6dd32007-06-24 11:16:15 +02001277static int __devinit w83627ehf_probe(struct platform_device *pdev)
Jean Delvare08e7e272005-04-25 22:43:25 +02001278{
David Hubbard1ea6dd32007-06-24 11:16:15 +02001279 struct device *dev = &pdev->dev;
1280 struct w83627ehf_sio_data *sio_data = dev->platform_data;
Jean Delvare08e7e272005-04-25 22:43:25 +02001281 struct w83627ehf_data *data;
David Hubbard1ea6dd32007-06-24 11:16:15 +02001282 struct resource *res;
Jean Delvarefc18d6c2007-06-24 11:19:42 +02001283 u8 fan4pin, fan5pin, en_vrm10;
Jean Delvare08e7e272005-04-25 22:43:25 +02001284 int i, err = 0;
1285
David Hubbard1ea6dd32007-06-24 11:16:15 +02001286 res = platform_get_resource(pdev, IORESOURCE_IO, 0);
1287 if (!request_region(res->start, IOREGION_LENGTH, DRVNAME)) {
Jean Delvare08e7e272005-04-25 22:43:25 +02001288 err = -EBUSY;
David Hubbard1ea6dd32007-06-24 11:16:15 +02001289 dev_err(dev, "Failed to request region 0x%lx-0x%lx\n",
1290 (unsigned long)res->start,
1291 (unsigned long)res->start + IOREGION_LENGTH - 1);
Jean Delvare08e7e272005-04-25 22:43:25 +02001292 goto exit;
1293 }
1294
Deepak Saxenaba9c2e82005-10-17 23:08:32 +02001295 if (!(data = kzalloc(sizeof(struct w83627ehf_data), GFP_KERNEL))) {
Jean Delvare08e7e272005-04-25 22:43:25 +02001296 err = -ENOMEM;
1297 goto exit_release;
1298 }
Jean Delvare08e7e272005-04-25 22:43:25 +02001299
David Hubbard1ea6dd32007-06-24 11:16:15 +02001300 data->addr = res->start;
Ingo Molnar9a61bf62006-01-18 23:19:26 +01001301 mutex_init(&data->lock);
Ingo Molnar9a61bf62006-01-18 23:19:26 +01001302 mutex_init(&data->update_lock);
David Hubbard1ea6dd32007-06-24 11:16:15 +02001303 data->name = w83627ehf_device_names[sio_data->kind];
1304 platform_set_drvdata(pdev, data);
Jean Delvare08e7e272005-04-25 22:43:25 +02001305
Gong Jun237c8d22009-03-30 21:46:42 +02001306 /* 627EHG and 627EHF have 10 voltage inputs; 627DHG and 667HG have 9 */
1307 data->in_num = (sio_data->kind == w83627ehf) ? 10 : 9;
1308 /* 667HG has 3 pwms */
1309 data->pwm_num = (sio_data->kind == w83667hg) ? 3 : 4;
Jean Delvare08e7e272005-04-25 22:43:25 +02001310
Gong Juna157d062009-03-30 21:46:43 +02001311 /* Check temp3 configuration bit for 667HG */
1312 if (sio_data->kind == w83667hg) {
1313 data->temp3_disable = w83627ehf_read_value(data,
1314 W83627EHF_REG_TEMP_CONFIG[1]) & 0x01;
1315 data->in6_skip = !data->temp3_disable;
1316 }
1317
Jean Delvare08e7e272005-04-25 22:43:25 +02001318 /* Initialize the chip */
David Hubbard1ea6dd32007-06-24 11:16:15 +02001319 w83627ehf_init_device(data);
Jean Delvare08e7e272005-04-25 22:43:25 +02001320
Jean Delvarefc18d6c2007-06-24 11:19:42 +02001321 data->vrm = vid_which_vrm();
1322 superio_enter(sio_data->sioreg);
Jean Delvarefc18d6c2007-06-24 11:19:42 +02001323 /* Read VID value */
Gong Jun237c8d22009-03-30 21:46:42 +02001324 if (sio_data->kind == w83667hg) {
1325 /* W83667HG has different pins for VID input and output, so
1326 we can get the VID input values directly at logical device D
1327 0xe3. */
1328 superio_select(sio_data->sioreg, W83667HG_LD_VID);
1329 data->vid = superio_inb(sio_data->sioreg, 0xe3);
Jean Delvarecbe311f2008-01-03 21:22:44 +01001330 err = device_create_file(dev, &dev_attr_cpu0_vid);
1331 if (err)
1332 goto exit_release;
Jean Delvare58e6e782008-01-03 07:33:31 -05001333 } else {
Gong Jun237c8d22009-03-30 21:46:42 +02001334 superio_select(sio_data->sioreg, W83627EHF_LD_HWM);
1335 if (superio_inb(sio_data->sioreg, SIO_REG_VID_CTRL) & 0x80) {
1336 /* Set VID input sensibility if needed. In theory the
1337 BIOS should have set it, but in practice it's not
1338 always the case. We only do it for the W83627EHF/EHG
1339 because the W83627DHG is more complex in this
1340 respect. */
1341 if (sio_data->kind == w83627ehf) {
1342 en_vrm10 = superio_inb(sio_data->sioreg,
1343 SIO_REG_EN_VRM10);
1344 if ((en_vrm10 & 0x08) && data->vrm == 90) {
1345 dev_warn(dev, "Setting VID input "
1346 "voltage to TTL\n");
1347 superio_outb(sio_data->sioreg,
1348 SIO_REG_EN_VRM10,
1349 en_vrm10 & ~0x08);
1350 } else if (!(en_vrm10 & 0x08)
1351 && data->vrm == 100) {
1352 dev_warn(dev, "Setting VID input "
1353 "voltage to VRM10\n");
1354 superio_outb(sio_data->sioreg,
1355 SIO_REG_EN_VRM10,
1356 en_vrm10 | 0x08);
1357 }
1358 }
1359
1360 data->vid = superio_inb(sio_data->sioreg,
1361 SIO_REG_VID_DATA);
1362 if (sio_data->kind == w83627ehf) /* 6 VID pins only */
1363 data->vid &= 0x3f;
1364
1365 err = device_create_file(dev, &dev_attr_cpu0_vid);
1366 if (err)
1367 goto exit_release;
1368 } else {
1369 dev_info(dev, "VID pins in output mode, CPU VID not "
1370 "available\n");
1371 }
Jean Delvarefc18d6c2007-06-24 11:19:42 +02001372 }
1373
Rudolf Marek08c79952006-07-05 18:14:31 +02001374 /* fan4 and fan5 share some pins with the GPIO and serial flash */
Gong Jun237c8d22009-03-30 21:46:42 +02001375 if (sio_data->kind == w83667hg) {
1376 fan5pin = superio_inb(sio_data->sioreg, 0x27) & 0x20;
1377 fan4pin = superio_inb(sio_data->sioreg, 0x27) & 0x40;
1378 } else {
1379 fan5pin = !(superio_inb(sio_data->sioreg, 0x24) & 0x02);
1380 fan4pin = !(superio_inb(sio_data->sioreg, 0x29) & 0x06);
1381 }
David Hubbard1ea6dd32007-06-24 11:16:15 +02001382 superio_exit(sio_data->sioreg);
Rudolf Marek08c79952006-07-05 18:14:31 +02001383
Jean Delvare08e7e272005-04-25 22:43:25 +02001384 /* It looks like fan4 and fan5 pins can be alternatively used
Rudolf Marek14992c72006-10-08 22:02:09 +02001385 as fan on/off switches, but fan5 control is write only :/
1386 We assume that if the serial interface is disabled, designers
1387 connected fan5 as input unless they are emitting log 1, which
1388 is not the default. */
Rudolf Marek08c79952006-07-05 18:14:31 +02001389
Jean Delvare08e7e272005-04-25 22:43:25 +02001390 data->has_fan = 0x07; /* fan1, fan2 and fan3 */
David Hubbard1ea6dd32007-06-24 11:16:15 +02001391 i = w83627ehf_read_value(data, W83627EHF_REG_FANDIV1);
Jean Delvare1704b262009-03-30 21:46:42 +02001392 if ((i & (1 << 2)) && fan4pin)
Jean Delvare08e7e272005-04-25 22:43:25 +02001393 data->has_fan |= (1 << 3);
Jean Delvare1704b262009-03-30 21:46:42 +02001394 if (!(i & (1 << 1)) && fan5pin)
Jean Delvare08e7e272005-04-25 22:43:25 +02001395 data->has_fan |= (1 << 4);
1396
Mark M. Hoffmanea7be662007-08-05 12:19:01 -04001397 /* Read fan clock dividers immediately */
1398 w83627ehf_update_fan_div(data);
1399
Jean Delvare08e7e272005-04-25 22:43:25 +02001400 /* Register sysfs hooks */
Rudolf Marek08c79952006-07-05 18:14:31 +02001401 for (i = 0; i < ARRAY_SIZE(sda_sf3_arrays); i++)
David Hubbardc18beb52006-09-24 21:04:38 +02001402 if ((err = device_create_file(dev,
1403 &sda_sf3_arrays[i].dev_attr)))
1404 goto exit_remove;
Rudolf Marek08c79952006-07-05 18:14:31 +02001405
1406 /* if fan4 is enabled create the sf3 files for it */
Gong Jun237c8d22009-03-30 21:46:42 +02001407 if ((data->has_fan & (1 << 3)) && data->pwm_num >= 4)
David Hubbardc18beb52006-09-24 21:04:38 +02001408 for (i = 0; i < ARRAY_SIZE(sda_sf3_arrays_fan4); i++) {
1409 if ((err = device_create_file(dev,
1410 &sda_sf3_arrays_fan4[i].dev_attr)))
1411 goto exit_remove;
1412 }
Rudolf Marek08c79952006-07-05 18:14:31 +02001413
Gong Juna157d062009-03-30 21:46:43 +02001414 for (i = 0; i < data->in_num; i++) {
1415 if ((i == 6) && data->in6_skip)
1416 continue;
David Hubbardc18beb52006-09-24 21:04:38 +02001417 if ((err = device_create_file(dev, &sda_in_input[i].dev_attr))
1418 || (err = device_create_file(dev,
1419 &sda_in_alarm[i].dev_attr))
1420 || (err = device_create_file(dev,
1421 &sda_in_min[i].dev_attr))
1422 || (err = device_create_file(dev,
1423 &sda_in_max[i].dev_attr)))
1424 goto exit_remove;
Gong Juna157d062009-03-30 21:46:43 +02001425 }
Rudolf Marekcf0676f2006-03-23 16:25:22 +01001426
Yuan Mu412fec82006-02-05 23:24:16 +01001427 for (i = 0; i < 5; i++) {
Rudolf Marek08c79952006-07-05 18:14:31 +02001428 if (data->has_fan & (1 << i)) {
David Hubbardc18beb52006-09-24 21:04:38 +02001429 if ((err = device_create_file(dev,
1430 &sda_fan_input[i].dev_attr))
1431 || (err = device_create_file(dev,
1432 &sda_fan_alarm[i].dev_attr))
1433 || (err = device_create_file(dev,
1434 &sda_fan_div[i].dev_attr))
1435 || (err = device_create_file(dev,
1436 &sda_fan_min[i].dev_attr)))
1437 goto exit_remove;
Gong Jun237c8d22009-03-30 21:46:42 +02001438 if (i < data->pwm_num &&
David Hubbardc18beb52006-09-24 21:04:38 +02001439 ((err = device_create_file(dev,
1440 &sda_pwm[i].dev_attr))
1441 || (err = device_create_file(dev,
1442 &sda_pwm_mode[i].dev_attr))
1443 || (err = device_create_file(dev,
1444 &sda_pwm_enable[i].dev_attr))
1445 || (err = device_create_file(dev,
1446 &sda_target_temp[i].dev_attr))
1447 || (err = device_create_file(dev,
1448 &sda_tolerance[i].dev_attr))))
1449 goto exit_remove;
Rudolf Marek08c79952006-07-05 18:14:31 +02001450 }
Jean Delvare08e7e272005-04-25 22:43:25 +02001451 }
Rudolf Marek08c79952006-07-05 18:14:31 +02001452
Gong Juna157d062009-03-30 21:46:43 +02001453 for (i = 0; i < 3; i++) {
1454 if ((i == 2) && data->temp3_disable)
1455 continue;
1456 if ((err = device_create_file(dev,
1457 &sda_temp_input[i].dev_attr))
1458 || (err = device_create_file(dev,
1459 &sda_temp_max[i].dev_attr))
1460 || (err = device_create_file(dev,
1461 &sda_temp_max_hyst[i].dev_attr))
1462 || (err = device_create_file(dev,
1463 &sda_temp_alarm[i].dev_attr))
1464 || (err = device_create_file(dev,
1465 &sda_temp_type[i].dev_attr)))
David Hubbardc18beb52006-09-24 21:04:38 +02001466 goto exit_remove;
Gong Juna157d062009-03-30 21:46:43 +02001467 }
David Hubbardc18beb52006-09-24 21:04:38 +02001468
David Hubbard1ea6dd32007-06-24 11:16:15 +02001469 err = device_create_file(dev, &dev_attr_name);
1470 if (err)
1471 goto exit_remove;
1472
Tony Jones1beeffe2007-08-20 13:46:20 -07001473 data->hwmon_dev = hwmon_device_register(dev);
1474 if (IS_ERR(data->hwmon_dev)) {
1475 err = PTR_ERR(data->hwmon_dev);
David Hubbardc18beb52006-09-24 21:04:38 +02001476 goto exit_remove;
1477 }
Jean Delvare08e7e272005-04-25 22:43:25 +02001478
1479 return 0;
1480
David Hubbardc18beb52006-09-24 21:04:38 +02001481exit_remove:
1482 w83627ehf_device_remove_files(dev);
Jean Delvare08e7e272005-04-25 22:43:25 +02001483 kfree(data);
David Hubbard1ea6dd32007-06-24 11:16:15 +02001484 platform_set_drvdata(pdev, NULL);
Jean Delvare08e7e272005-04-25 22:43:25 +02001485exit_release:
David Hubbard1ea6dd32007-06-24 11:16:15 +02001486 release_region(res->start, IOREGION_LENGTH);
Jean Delvare08e7e272005-04-25 22:43:25 +02001487exit:
1488 return err;
1489}
1490
David Hubbard1ea6dd32007-06-24 11:16:15 +02001491static int __devexit w83627ehf_remove(struct platform_device *pdev)
Jean Delvare08e7e272005-04-25 22:43:25 +02001492{
David Hubbard1ea6dd32007-06-24 11:16:15 +02001493 struct w83627ehf_data *data = platform_get_drvdata(pdev);
Jean Delvare08e7e272005-04-25 22:43:25 +02001494
Tony Jones1beeffe2007-08-20 13:46:20 -07001495 hwmon_device_unregister(data->hwmon_dev);
David Hubbard1ea6dd32007-06-24 11:16:15 +02001496 w83627ehf_device_remove_files(&pdev->dev);
1497 release_region(data->addr, IOREGION_LENGTH);
1498 platform_set_drvdata(pdev, NULL);
Mark M. Hoffman943b0832005-07-15 21:39:18 -04001499 kfree(data);
Jean Delvare08e7e272005-04-25 22:43:25 +02001500
1501 return 0;
1502}
1503
David Hubbard1ea6dd32007-06-24 11:16:15 +02001504static struct platform_driver w83627ehf_driver = {
Laurent Riffardcdaf7932005-11-26 20:37:41 +01001505 .driver = {
Jean Delvare87218842006-09-03 22:36:14 +02001506 .owner = THIS_MODULE,
David Hubbard1ea6dd32007-06-24 11:16:15 +02001507 .name = DRVNAME,
Laurent Riffardcdaf7932005-11-26 20:37:41 +01001508 },
David Hubbard1ea6dd32007-06-24 11:16:15 +02001509 .probe = w83627ehf_probe,
1510 .remove = __devexit_p(w83627ehf_remove),
Jean Delvare08e7e272005-04-25 22:43:25 +02001511};
1512
David Hubbard1ea6dd32007-06-24 11:16:15 +02001513/* w83627ehf_find() looks for a '627 in the Super-I/O config space */
1514static int __init w83627ehf_find(int sioaddr, unsigned short *addr,
1515 struct w83627ehf_sio_data *sio_data)
Jean Delvare08e7e272005-04-25 22:43:25 +02001516{
David Hubbard1ea6dd32007-06-24 11:16:15 +02001517 static const char __initdata sio_name_W83627EHF[] = "W83627EHF";
1518 static const char __initdata sio_name_W83627EHG[] = "W83627EHG";
1519 static const char __initdata sio_name_W83627DHG[] = "W83627DHG";
Gong Jun237c8d22009-03-30 21:46:42 +02001520 static const char __initdata sio_name_W83667HG[] = "W83667HG";
David Hubbard1ea6dd32007-06-24 11:16:15 +02001521
Jean Delvare08e7e272005-04-25 22:43:25 +02001522 u16 val;
David Hubbard1ea6dd32007-06-24 11:16:15 +02001523 const char *sio_name;
Jean Delvare08e7e272005-04-25 22:43:25 +02001524
David Hubbard1ea6dd32007-06-24 11:16:15 +02001525 superio_enter(sioaddr);
Jean Delvare08e7e272005-04-25 22:43:25 +02001526
Jean Delvare67b671b2007-12-06 23:13:42 +01001527 if (force_id)
1528 val = force_id;
1529 else
1530 val = (superio_inb(sioaddr, SIO_REG_DEVID) << 8)
1531 | superio_inb(sioaddr, SIO_REG_DEVID + 1);
David Hubbard657c93b2007-02-14 21:15:04 +01001532 switch (val & SIO_ID_MASK) {
David Hubbard657c93b2007-02-14 21:15:04 +01001533 case SIO_W83627EHF_ID:
David Hubbard1ea6dd32007-06-24 11:16:15 +02001534 sio_data->kind = w83627ehf;
1535 sio_name = sio_name_W83627EHF;
1536 break;
David Hubbard657c93b2007-02-14 21:15:04 +01001537 case SIO_W83627EHG_ID:
David Hubbard1ea6dd32007-06-24 11:16:15 +02001538 sio_data->kind = w83627ehf;
1539 sio_name = sio_name_W83627EHG;
1540 break;
1541 case SIO_W83627DHG_ID:
1542 sio_data->kind = w83627dhg;
1543 sio_name = sio_name_W83627DHG;
David Hubbard657c93b2007-02-14 21:15:04 +01001544 break;
Gong Jun237c8d22009-03-30 21:46:42 +02001545 case SIO_W83667HG_ID:
1546 sio_data->kind = w83667hg;
1547 sio_name = sio_name_W83667HG;
1548 break;
David Hubbard657c93b2007-02-14 21:15:04 +01001549 default:
Jean Delvare9f660362007-06-24 11:23:41 +02001550 if (val != 0xffff)
1551 pr_debug(DRVNAME ": unsupported chip ID: 0x%04x\n",
1552 val);
David Hubbard1ea6dd32007-06-24 11:16:15 +02001553 superio_exit(sioaddr);
Jean Delvare08e7e272005-04-25 22:43:25 +02001554 return -ENODEV;
1555 }
1556
David Hubbard1ea6dd32007-06-24 11:16:15 +02001557 /* We have a known chip, find the HWM I/O address */
1558 superio_select(sioaddr, W83627EHF_LD_HWM);
1559 val = (superio_inb(sioaddr, SIO_REG_ADDR) << 8)
1560 | superio_inb(sioaddr, SIO_REG_ADDR + 1);
Jean Delvare1a641fc2007-04-23 14:41:16 -07001561 *addr = val & IOREGION_ALIGNMENT;
Jean Delvare2d8672c2005-07-19 23:56:35 +02001562 if (*addr == 0) {
David Hubbard475ef852007-06-24 11:17:09 +02001563 printk(KERN_ERR DRVNAME ": Refusing to enable a Super-I/O "
1564 "device with a base I/O port 0.\n");
David Hubbard1ea6dd32007-06-24 11:16:15 +02001565 superio_exit(sioaddr);
Jean Delvare08e7e272005-04-25 22:43:25 +02001566 return -ENODEV;
1567 }
1568
1569 /* Activate logical device if needed */
David Hubbard1ea6dd32007-06-24 11:16:15 +02001570 val = superio_inb(sioaddr, SIO_REG_ENABLE);
David Hubbard475ef852007-06-24 11:17:09 +02001571 if (!(val & 0x01)) {
1572 printk(KERN_WARNING DRVNAME ": Forcibly enabling Super-I/O. "
1573 "Sensor is probably unusable.\n");
David Hubbard1ea6dd32007-06-24 11:16:15 +02001574 superio_outb(sioaddr, SIO_REG_ENABLE, val | 0x01);
David Hubbard475ef852007-06-24 11:17:09 +02001575 }
Jean Delvare08e7e272005-04-25 22:43:25 +02001576
David Hubbard1ea6dd32007-06-24 11:16:15 +02001577 superio_exit(sioaddr);
1578 pr_info(DRVNAME ": Found %s chip at %#x\n", sio_name, *addr);
1579 sio_data->sioreg = sioaddr;
1580
Jean Delvare08e7e272005-04-25 22:43:25 +02001581 return 0;
1582}
1583
David Hubbard1ea6dd32007-06-24 11:16:15 +02001584/* when Super-I/O functions move to a separate file, the Super-I/O
1585 * bus will manage the lifetime of the device and this module will only keep
1586 * track of the w83627ehf driver. But since we platform_device_alloc(), we
1587 * must keep track of the device */
1588static struct platform_device *pdev;
1589
Jean Delvare08e7e272005-04-25 22:43:25 +02001590static int __init sensors_w83627ehf_init(void)
1591{
David Hubbard1ea6dd32007-06-24 11:16:15 +02001592 int err;
1593 unsigned short address;
1594 struct resource res;
1595 struct w83627ehf_sio_data sio_data;
1596
1597 /* initialize sio_data->kind and sio_data->sioreg.
1598 *
1599 * when Super-I/O functions move to a separate file, the Super-I/O
1600 * driver will probe 0x2e and 0x4e and auto-detect the presence of a
1601 * w83627ehf hardware monitor, and call probe() */
1602 if (w83627ehf_find(0x2e, &address, &sio_data) &&
1603 w83627ehf_find(0x4e, &address, &sio_data))
Jean Delvare08e7e272005-04-25 22:43:25 +02001604 return -ENODEV;
1605
David Hubbard1ea6dd32007-06-24 11:16:15 +02001606 err = platform_driver_register(&w83627ehf_driver);
1607 if (err)
1608 goto exit;
1609
1610 if (!(pdev = platform_device_alloc(DRVNAME, address))) {
1611 err = -ENOMEM;
1612 printk(KERN_ERR DRVNAME ": Device allocation failed\n");
1613 goto exit_unregister;
1614 }
1615
1616 err = platform_device_add_data(pdev, &sio_data,
1617 sizeof(struct w83627ehf_sio_data));
1618 if (err) {
1619 printk(KERN_ERR DRVNAME ": Platform data allocation failed\n");
1620 goto exit_device_put;
1621 }
1622
1623 memset(&res, 0, sizeof(res));
1624 res.name = DRVNAME;
1625 res.start = address + IOREGION_OFFSET;
1626 res.end = address + IOREGION_OFFSET + IOREGION_LENGTH - 1;
1627 res.flags = IORESOURCE_IO;
Jean Delvareb9acb642009-01-07 16:37:35 +01001628
1629 err = acpi_check_resource_conflict(&res);
1630 if (err)
Hans de Goede18632f82009-02-17 19:59:54 +01001631 goto exit_device_put;
Jean Delvareb9acb642009-01-07 16:37:35 +01001632
David Hubbard1ea6dd32007-06-24 11:16:15 +02001633 err = platform_device_add_resources(pdev, &res, 1);
1634 if (err) {
1635 printk(KERN_ERR DRVNAME ": Device resource addition failed "
1636 "(%d)\n", err);
1637 goto exit_device_put;
1638 }
1639
1640 /* platform_device_add calls probe() */
1641 err = platform_device_add(pdev);
1642 if (err) {
1643 printk(KERN_ERR DRVNAME ": Device addition failed (%d)\n",
1644 err);
1645 goto exit_device_put;
1646 }
1647
1648 return 0;
1649
1650exit_device_put:
1651 platform_device_put(pdev);
1652exit_unregister:
1653 platform_driver_unregister(&w83627ehf_driver);
1654exit:
1655 return err;
Jean Delvare08e7e272005-04-25 22:43:25 +02001656}
1657
1658static void __exit sensors_w83627ehf_exit(void)
1659{
David Hubbard1ea6dd32007-06-24 11:16:15 +02001660 platform_device_unregister(pdev);
1661 platform_driver_unregister(&w83627ehf_driver);
Jean Delvare08e7e272005-04-25 22:43:25 +02001662}
1663
1664MODULE_AUTHOR("Jean Delvare <khali@linux-fr.org>");
1665MODULE_DESCRIPTION("W83627EHF driver");
1666MODULE_LICENSE("GPL");
1667
1668module_init(sensors_w83627ehf_init);
1669module_exit(sensors_w83627ehf_exit);