blob: 5ce54a297249784b97f17bb6ae7d7b4f57f7d310 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Guenter Roeck27b9de32012-01-15 11:07:26 -08002 * w83627hf.c - Part of lm_sensors, Linux kernel modules for hardware
3 * monitoring
4 * Copyright (c) 1998 - 2003 Frodo Looijaard <frodol@dds.nl>,
5 * Philip Edelbrock <phil@netroedge.com>,
6 * and Mark Studebaker <mdsxyz123@yahoo.com>
7 * Ported to 2.6 by Bernhard C. Schrenk <clemy@clemy.org>
8 * Copyright (c) 2007 Jean Delvare <khali@linux-fr.org>
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070024
25/*
Guenter Roeck27b9de32012-01-15 11:07:26 -080026 * Supports following chips:
27 *
28 * Chip #vin #fanin #pwm #temp wchipid vendid i2c ISA
29 * w83627hf 9 3 2 3 0x20 0x5ca3 no yes(LPC)
30 * w83627thf 7 3 3 3 0x90 0x5ca3 no yes(LPC)
31 * w83637hf 7 3 3 3 0x80 0x5ca3 no yes(LPC)
32 * w83687thf 7 3 3 3 0x90 0x5ca3 no yes(LPC)
33 * w83697hf 8 2 2 2 0x60 0x5ca3 no yes(LPC)
34 *
35 * For other winbond chips, and for i2c support in the above chips,
36 * use w83781d.c.
37 *
38 * Note: automatic ("cruise") fan control for 697, 637 & 627thf not
39 * supported yet.
40 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070041
Joe Perches18de0302010-10-20 06:51:55 +000042#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
43
Linus Torvalds1da177e2005-04-16 15:20:36 -070044#include <linux/module.h>
45#include <linux/init.h>
46#include <linux/slab.h>
47#include <linux/jiffies.h>
Jean Delvare787c72b2007-05-08 17:22:00 +020048#include <linux/platform_device.h>
Mark M. Hoffman943b0832005-07-15 21:39:18 -040049#include <linux/hwmon.h>
Jim Cromie07584c72007-10-12 21:08:00 +020050#include <linux/hwmon-sysfs.h>
Jean Delvare303760b2005-07-31 21:52:01 +020051#include <linux/hwmon-vid.h>
Mark M. Hoffman943b0832005-07-15 21:39:18 -040052#include <linux/err.h>
Ingo Molnar9a61bf62006-01-18 23:19:26 +010053#include <linux/mutex.h>
Jean Delvared27c37c2007-05-08 17:21:59 +020054#include <linux/ioport.h>
Jean Delvareb9acb642009-01-07 16:37:35 +010055#include <linux/acpi.h>
H Hartley Sweeten6055fae2009-09-15 17:18:13 +020056#include <linux/io.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070057#include "lm75.h"
58
Jean Delvare787c72b2007-05-08 17:22:00 +020059static struct platform_device *pdev;
Jean Delvared27c37c2007-05-08 17:21:59 +020060
61#define DRVNAME "w83627hf"
62enum chips { w83627hf, w83627thf, w83697hf, w83637hf, w83687thf };
63
Jean Delvareb72656d2009-12-09 20:35:49 +010064struct w83627hf_sio_data {
65 enum chips type;
66 int sioaddr;
67};
68
Linus Torvalds1da177e2005-04-16 15:20:36 -070069static u8 force_i2c = 0x1f;
70module_param(force_i2c, byte, 0);
71MODULE_PARM_DESC(force_i2c,
72 "Initialize the i2c address of the sensors");
73
Rusty Russell90ab5ee2012-01-13 09:32:20 +103074static bool init = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -070075module_param(init, bool, 0);
76MODULE_PARM_DESC(init, "Set to zero to bypass chip initialization");
77
Jean Delvare67b671b2007-12-06 23:13:42 +010078static unsigned short force_id;
79module_param(force_id, ushort, 0);
80MODULE_PARM_DESC(force_id, "Override the detected device ID");
81
Linus Torvalds1da177e2005-04-16 15:20:36 -070082/* modified from kernel/include/traps.c */
Guenter Roeck27b9de32012-01-15 11:07:26 -080083#define DEV 0x07 /* Register: Logical device select */
Linus Torvalds1da177e2005-04-16 15:20:36 -070084
85/* logical device numbers for superio_select (below) */
86#define W83627HF_LD_FDC 0x00
87#define W83627HF_LD_PRT 0x01
88#define W83627HF_LD_UART1 0x02
89#define W83627HF_LD_UART2 0x03
90#define W83627HF_LD_KBC 0x05
91#define W83627HF_LD_CIR 0x06 /* w83627hf only */
92#define W83627HF_LD_GAME 0x07
93#define W83627HF_LD_MIDI 0x07
94#define W83627HF_LD_GPIO1 0x07
95#define W83627HF_LD_GPIO5 0x07 /* w83627thf only */
96#define W83627HF_LD_GPIO2 0x08
97#define W83627HF_LD_GPIO3 0x09
98#define W83627HF_LD_GPIO4 0x09 /* w83627thf only */
99#define W83627HF_LD_ACPI 0x0a
100#define W83627HF_LD_HWM 0x0b
101
Guenter Roeck27b9de32012-01-15 11:07:26 -0800102#define DEVID 0x20 /* Register: Device ID */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103
104#define W83627THF_GPIO5_EN 0x30 /* w83627thf only */
105#define W83627THF_GPIO5_IOSR 0xf3 /* w83627thf only */
106#define W83627THF_GPIO5_DR 0xf4 /* w83627thf only */
107
Jean Delvarec2db6ce2006-01-18 23:22:12 +0100108#define W83687THF_VID_EN 0x29 /* w83687thf only */
109#define W83687THF_VID_CFG 0xF0 /* w83687thf only */
110#define W83687THF_VID_DATA 0xF1 /* w83687thf only */
111
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112static inline void
Jean Delvareb72656d2009-12-09 20:35:49 +0100113superio_outb(struct w83627hf_sio_data *sio, int reg, int val)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114{
Jean Delvareb72656d2009-12-09 20:35:49 +0100115 outb(reg, sio->sioaddr);
116 outb(val, sio->sioaddr + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117}
118
119static inline int
Jean Delvareb72656d2009-12-09 20:35:49 +0100120superio_inb(struct w83627hf_sio_data *sio, int reg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121{
Jean Delvareb72656d2009-12-09 20:35:49 +0100122 outb(reg, sio->sioaddr);
123 return inb(sio->sioaddr + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124}
125
126static inline void
Jean Delvareb72656d2009-12-09 20:35:49 +0100127superio_select(struct w83627hf_sio_data *sio, int ld)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128{
Jean Delvareb72656d2009-12-09 20:35:49 +0100129 outb(DEV, sio->sioaddr);
130 outb(ld, sio->sioaddr + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131}
132
133static inline void
Jean Delvareb72656d2009-12-09 20:35:49 +0100134superio_enter(struct w83627hf_sio_data *sio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135{
Jean Delvareb72656d2009-12-09 20:35:49 +0100136 outb(0x87, sio->sioaddr);
137 outb(0x87, sio->sioaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138}
139
140static inline void
Jean Delvareb72656d2009-12-09 20:35:49 +0100141superio_exit(struct w83627hf_sio_data *sio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142{
Jean Delvareb72656d2009-12-09 20:35:49 +0100143 outb(0xAA, sio->sioaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144}
145
146#define W627_DEVID 0x52
147#define W627THF_DEVID 0x82
148#define W697_DEVID 0x60
149#define W637_DEVID 0x70
Jean Delvarec2db6ce2006-01-18 23:22:12 +0100150#define W687THF_DEVID 0x85
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151#define WINB_ACT_REG 0x30
152#define WINB_BASE_REG 0x60
153/* Constants specified below */
154
Petr Vandrovecada0c2f2005-10-07 23:11:03 +0200155/* Alignment of the base address */
156#define WINB_ALIGNMENT ~7
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157
Petr Vandrovecada0c2f2005-10-07 23:11:03 +0200158/* Offset & size of I/O region we are interested in */
159#define WINB_REGION_OFFSET 5
160#define WINB_REGION_SIZE 2
161
Jean Delvare787c72b2007-05-08 17:22:00 +0200162/* Where are the sensors address/data registers relative to the region offset */
163#define W83781D_ADDR_REG_OFFSET 0
164#define W83781D_DATA_REG_OFFSET 1
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165
166/* The W83781D registers */
167/* The W83782D registers for nr=7,8 are in bank 5 */
168#define W83781D_REG_IN_MAX(nr) ((nr < 7) ? (0x2b + (nr) * 2) : \
169 (0x554 + (((nr) - 7) * 2)))
170#define W83781D_REG_IN_MIN(nr) ((nr < 7) ? (0x2c + (nr) * 2) : \
171 (0x555 + (((nr) - 7) * 2)))
172#define W83781D_REG_IN(nr) ((nr < 7) ? (0x20 + (nr)) : \
173 (0x550 + (nr) - 7))
174
Jim Cromie2ca2fcd2007-10-14 17:20:50 -0600175/* nr:0-2 for fans:1-3 */
176#define W83627HF_REG_FAN_MIN(nr) (0x3b + (nr))
177#define W83627HF_REG_FAN(nr) (0x28 + (nr))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178
Jim Cromiedf48ed82007-10-14 17:10:52 -0600179#define W83627HF_REG_TEMP2_CONFIG 0x152
180#define W83627HF_REG_TEMP3_CONFIG 0x252
181/* these are zero-based, unlike config constants above */
182static const u16 w83627hf_reg_temp[] = { 0x27, 0x150, 0x250 };
183static const u16 w83627hf_reg_temp_hyst[] = { 0x3A, 0x153, 0x253 };
184static const u16 w83627hf_reg_temp_over[] = { 0x39, 0x155, 0x255 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185
186#define W83781D_REG_BANK 0x4E
187
188#define W83781D_REG_CONFIG 0x40
Yuan Mu4a1c44472005-11-07 22:19:04 +0100189#define W83781D_REG_ALARM1 0x459
190#define W83781D_REG_ALARM2 0x45A
191#define W83781D_REG_ALARM3 0x45B
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193#define W83781D_REG_BEEP_CONFIG 0x4D
194#define W83781D_REG_BEEP_INTS1 0x56
195#define W83781D_REG_BEEP_INTS2 0x57
196#define W83781D_REG_BEEP_INTS3 0x453
197
198#define W83781D_REG_VID_FANDIV 0x47
199
200#define W83781D_REG_CHIPID 0x49
201#define W83781D_REG_WCHIPID 0x58
202#define W83781D_REG_CHIPMAN 0x4F
203#define W83781D_REG_PIN 0x4B
204
205#define W83781D_REG_VBAT 0x5D
206
207#define W83627HF_REG_PWM1 0x5A
208#define W83627HF_REG_PWM2 0x5B
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209
Dominik Geyera95a5ed2008-08-06 22:41:04 +0200210static const u8 W83627THF_REG_PWM_ENABLE[] = {
211 0x04, /* FAN 1 mode */
212 0x04, /* FAN 2 mode */
213 0x12, /* FAN AUX mode */
214};
215static const u8 W83627THF_PWM_ENABLE_SHIFT[] = { 2, 4, 1 };
216
Jean Delvarec2db6ce2006-01-18 23:22:12 +0100217#define W83627THF_REG_PWM1 0x01 /* 697HF/637HF/687THF too */
218#define W83627THF_REG_PWM2 0x03 /* 697HF/637HF/687THF too */
219#define W83627THF_REG_PWM3 0x11 /* 637HF/687THF too */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220
Jean Delvarec2db6ce2006-01-18 23:22:12 +0100221#define W83627THF_REG_VRM_OVT_CFG 0x18 /* 637HF/687THF too */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222
223static const u8 regpwm_627hf[] = { W83627HF_REG_PWM1, W83627HF_REG_PWM2 };
224static const u8 regpwm[] = { W83627THF_REG_PWM1, W83627THF_REG_PWM2,
225 W83627THF_REG_PWM3 };
226#define W836X7HF_REG_PWM(type, nr) (((type) == w83627hf) ? \
Jim Cromie07584c72007-10-12 21:08:00 +0200227 regpwm_627hf[nr] : regpwm[nr])
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228
Carlos Olalla Martinez1550cb62007-06-09 10:11:16 -0400229#define W83627HF_REG_PWM_FREQ 0x5C /* Only for the 627HF */
230
231#define W83637HF_REG_PWM_FREQ1 0x00 /* 697HF/687THF too */
232#define W83637HF_REG_PWM_FREQ2 0x02 /* 697HF/687THF too */
233#define W83637HF_REG_PWM_FREQ3 0x10 /* 687THF too */
234
235static const u8 W83637HF_REG_PWM_FREQ[] = { W83637HF_REG_PWM_FREQ1,
236 W83637HF_REG_PWM_FREQ2,
237 W83637HF_REG_PWM_FREQ3 };
238
239#define W83627HF_BASE_PWM_FREQ 46870
240
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241#define W83781D_REG_I2C_ADDR 0x48
242#define W83781D_REG_I2C_SUBADDR 0x4A
243
244/* Sensor selection */
245#define W83781D_REG_SCFG1 0x5D
246static const u8 BIT_SCFG1[] = { 0x02, 0x04, 0x08 };
247#define W83781D_REG_SCFG2 0x59
248static const u8 BIT_SCFG2[] = { 0x10, 0x20, 0x40 };
249#define W83781D_DEFAULT_BETA 3435
250
Guenter Roeck27b9de32012-01-15 11:07:26 -0800251/*
252 * Conversions. Limit checking is only done on the TO_REG
253 * variants. Note that you should be a bit careful with which arguments
254 * these macros are called: arguments may be evaluated more than once.
255 * Fixing this is just not worth it.
256 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257#define IN_TO_REG(val) (SENSORS_LIMIT((((val) + 8)/16),0,255))
258#define IN_FROM_REG(val) ((val) * 16)
259
260static inline u8 FAN_TO_REG(long rpm, int div)
261{
262 if (rpm == 0)
263 return 255;
264 rpm = SENSORS_LIMIT(rpm, 1, 1000000);
265 return SENSORS_LIMIT((1350000 + rpm * div / 2) / (rpm * div), 1,
266 254);
267}
268
269#define TEMP_MIN (-128000)
270#define TEMP_MAX ( 127000)
271
Guenter Roeck27b9de32012-01-15 11:07:26 -0800272/*
273 * TEMP: 0.001C/bit (-128C to +127C)
274 * REG: 1C/bit, two's complement
275 */
Christian Hohnstaedt5bfedac2007-08-16 11:40:10 +0200276static u8 TEMP_TO_REG(long temp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277{
278 int ntemp = SENSORS_LIMIT(temp, TEMP_MIN, TEMP_MAX);
279 ntemp += (ntemp<0 ? -500 : 500);
280 return (u8)(ntemp / 1000);
281}
282
283static int TEMP_FROM_REG(u8 reg)
284{
285 return (s8)reg * 1000;
286}
287
288#define FAN_FROM_REG(val,div) ((val)==0?-1:(val)==255?0:1350000/((val)*(div)))
289
290#define PWM_TO_REG(val) (SENSORS_LIMIT((val),0,255))
291
Carlos Olalla Martinez1550cb62007-06-09 10:11:16 -0400292static inline unsigned long pwm_freq_from_reg_627hf(u8 reg)
293{
294 unsigned long freq;
295 freq = W83627HF_BASE_PWM_FREQ >> reg;
296 return freq;
297}
298static inline u8 pwm_freq_to_reg_627hf(unsigned long val)
299{
300 u8 i;
Guenter Roeck27b9de32012-01-15 11:07:26 -0800301 /*
302 * Only 5 dividers (1 2 4 8 16)
303 * Search for the nearest available frequency
304 */
Carlos Olalla Martinez1550cb62007-06-09 10:11:16 -0400305 for (i = 0; i < 4; i++) {
306 if (val > (((W83627HF_BASE_PWM_FREQ >> i) +
307 (W83627HF_BASE_PWM_FREQ >> (i+1))) / 2))
308 break;
309 }
310 return i;
311}
312
313static inline unsigned long pwm_freq_from_reg(u8 reg)
314{
315 /* Clock bit 8 -> 180 kHz or 24 MHz */
316 unsigned long clock = (reg & 0x80) ? 180000UL : 24000000UL;
317
318 reg &= 0x7f;
319 /* This should not happen but anyway... */
320 if (reg == 0)
321 reg++;
Frans Meulenbroeks7fe83ad2012-01-05 19:50:18 +0100322 return clock / (reg << 8);
Carlos Olalla Martinez1550cb62007-06-09 10:11:16 -0400323}
324static inline u8 pwm_freq_to_reg(unsigned long val)
325{
326 /* Minimum divider value is 0x01 and maximum is 0x7F */
327 if (val >= 93750) /* The highest we can do */
328 return 0x01;
329 if (val >= 720) /* Use 24 MHz clock */
Frans Meulenbroeks7fe83ad2012-01-05 19:50:18 +0100330 return 24000000UL / (val << 8);
Carlos Olalla Martinez1550cb62007-06-09 10:11:16 -0400331 if (val < 6) /* The lowest we can do */
332 return 0xFF;
333 else /* Use 180 kHz clock */
Frans Meulenbroeks7fe83ad2012-01-05 19:50:18 +0100334 return 0x80 | (180000UL / (val << 8));
Carlos Olalla Martinez1550cb62007-06-09 10:11:16 -0400335}
336
Jean Delvare1c138102008-01-03 23:04:55 +0100337#define BEEP_MASK_FROM_REG(val) ((val) & 0xff7fff)
338#define BEEP_MASK_TO_REG(val) ((val) & 0xff7fff)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339
340#define DIV_FROM_REG(val) (1 << (val))
341
342static inline u8 DIV_TO_REG(long val)
343{
344 int i;
345 val = SENSORS_LIMIT(val, 1, 128) >> 1;
Grant Coadyabc01922005-05-12 13:41:51 +1000346 for (i = 0; i < 7; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347 if (val == 0)
348 break;
349 val >>= 1;
350 }
Frans Meulenbroeks7fe83ad2012-01-05 19:50:18 +0100351 return (u8)i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352}
353
Guenter Roeck27b9de32012-01-15 11:07:26 -0800354/*
355 * For each registered chip, we need to keep some data in memory.
356 * The structure is dynamically allocated.
357 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358struct w83627hf_data {
Jean Delvare787c72b2007-05-08 17:22:00 +0200359 unsigned short addr;
360 const char *name;
Tony Jones1beeffe2007-08-20 13:46:20 -0700361 struct device *hwmon_dev;
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100362 struct mutex lock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363 enum chips type;
364
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100365 struct mutex update_lock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366 char valid; /* !=0 if following fields are valid */
367 unsigned long last_updated; /* In jiffies */
368
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369 u8 in[9]; /* Register value */
370 u8 in_max[9]; /* Register value */
371 u8 in_min[9]; /* Register value */
372 u8 fan[3]; /* Register value */
373 u8 fan_min[3]; /* Register value */
Jim Cromiedf48ed82007-10-14 17:10:52 -0600374 u16 temp[3]; /* Register value */
375 u16 temp_max[3]; /* Register value */
376 u16 temp_max_hyst[3]; /* Register value */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377 u8 fan_div[3]; /* Register encoding, shifted right */
378 u8 vid; /* Register encoding, combined */
379 u32 alarms; /* Register encoding, combined */
380 u32 beep_mask; /* Register encoding, combined */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381 u8 pwm[3]; /* Register value */
Dominik Geyera95a5ed2008-08-06 22:41:04 +0200382 u8 pwm_enable[3]; /* 1 = manual
Guenter Roeck27b9de32012-01-15 11:07:26 -0800383 * 2 = thermal cruise (also called SmartFan I)
384 * 3 = fan speed cruise
385 */
Carlos Olalla Martinez1550cb62007-06-09 10:11:16 -0400386 u8 pwm_freq[3]; /* Register value */
Jean Delvareb26f9332007-08-16 14:30:01 +0200387 u16 sens[3]; /* 1 = pentium diode; 2 = 3904 diode;
Guenter Roeck27b9de32012-01-15 11:07:26 -0800388 * 4 = thermistor
389 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390 u8 vrm;
Jean Delvarec2db6ce2006-01-18 23:22:12 +0100391 u8 vrm_ovt; /* Register value, 627THF/637HF/687THF only */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392};
393
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394
Jean Delvare787c72b2007-05-08 17:22:00 +0200395static int w83627hf_probe(struct platform_device *pdev);
Jean Delvared0546122007-07-22 12:09:48 +0200396static int __devexit w83627hf_remove(struct platform_device *pdev);
Jean Delvare787c72b2007-05-08 17:22:00 +0200397
398static int w83627hf_read_value(struct w83627hf_data *data, u16 reg);
399static int w83627hf_write_value(struct w83627hf_data *data, u16 reg, u16 value);
Jean Delvarec09c5182007-10-12 21:53:07 +0200400static void w83627hf_update_fan_div(struct w83627hf_data *data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401static struct w83627hf_data *w83627hf_update_device(struct device *dev);
Jean Delvare787c72b2007-05-08 17:22:00 +0200402static void w83627hf_init_device(struct platform_device *pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403
Jean Delvare787c72b2007-05-08 17:22:00 +0200404static struct platform_driver w83627hf_driver = {
Laurent Riffardcdaf7932005-11-26 20:37:41 +0100405 .driver = {
Jean Delvare87218842006-09-03 22:36:14 +0200406 .owner = THIS_MODULE,
Jean Delvared27c37c2007-05-08 17:21:59 +0200407 .name = DRVNAME,
Laurent Riffardcdaf7932005-11-26 20:37:41 +0100408 },
Jean Delvare787c72b2007-05-08 17:22:00 +0200409 .probe = w83627hf_probe,
410 .remove = __devexit_p(w83627hf_remove),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411};
412
Jim Cromie07584c72007-10-12 21:08:00 +0200413static ssize_t
414show_in_input(struct device *dev, struct device_attribute *devattr, char *buf)
415{
416 int nr = to_sensor_dev_attr(devattr)->index;
417 struct w83627hf_data *data = w83627hf_update_device(dev);
418 return sprintf(buf, "%ld\n", (long)IN_FROM_REG(data->in[nr]));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419}
Jim Cromie07584c72007-10-12 21:08:00 +0200420static ssize_t
421show_in_min(struct device *dev, struct device_attribute *devattr, char *buf)
422{
423 int nr = to_sensor_dev_attr(devattr)->index;
424 struct w83627hf_data *data = w83627hf_update_device(dev);
425 return sprintf(buf, "%ld\n", (long)IN_FROM_REG(data->in_min[nr]));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426}
Jim Cromie07584c72007-10-12 21:08:00 +0200427static ssize_t
428show_in_max(struct device *dev, struct device_attribute *devattr, char *buf)
429{
430 int nr = to_sensor_dev_attr(devattr)->index;
431 struct w83627hf_data *data = w83627hf_update_device(dev);
432 return sprintf(buf, "%ld\n", (long)IN_FROM_REG(data->in_max[nr]));
433}
434static ssize_t
435store_in_min(struct device *dev, struct device_attribute *devattr,
436 const char *buf, size_t count)
437{
438 int nr = to_sensor_dev_attr(devattr)->index;
439 struct w83627hf_data *data = dev_get_drvdata(dev);
Guenter Roeck27b9de32012-01-15 11:07:26 -0800440 long val;
441 int err;
442
443 err = kstrtol(buf, 10, &val);
444 if (err)
445 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446
Jim Cromie07584c72007-10-12 21:08:00 +0200447 mutex_lock(&data->update_lock);
448 data->in_min[nr] = IN_TO_REG(val);
449 w83627hf_write_value(data, W83781D_REG_IN_MIN(nr), data->in_min[nr]);
450 mutex_unlock(&data->update_lock);
451 return count;
452}
453static ssize_t
454store_in_max(struct device *dev, struct device_attribute *devattr,
455 const char *buf, size_t count)
456{
457 int nr = to_sensor_dev_attr(devattr)->index;
458 struct w83627hf_data *data = dev_get_drvdata(dev);
Guenter Roeck27b9de32012-01-15 11:07:26 -0800459 long val;
460 int err;
461
462 err = kstrtol(buf, 10, &val);
463 if (err)
464 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465
Jim Cromie07584c72007-10-12 21:08:00 +0200466 mutex_lock(&data->update_lock);
467 data->in_max[nr] = IN_TO_REG(val);
468 w83627hf_write_value(data, W83781D_REG_IN_MAX(nr), data->in_max[nr]);
469 mutex_unlock(&data->update_lock);
470 return count;
471}
472#define sysfs_vin_decl(offset) \
473static SENSOR_DEVICE_ATTR(in##offset##_input, S_IRUGO, \
474 show_in_input, NULL, offset); \
475static SENSOR_DEVICE_ATTR(in##offset##_min, S_IRUGO|S_IWUSR, \
476 show_in_min, store_in_min, offset); \
477static SENSOR_DEVICE_ATTR(in##offset##_max, S_IRUGO|S_IWUSR, \
478 show_in_max, store_in_max, offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479
Jim Cromie07584c72007-10-12 21:08:00 +0200480sysfs_vin_decl(1);
481sysfs_vin_decl(2);
482sysfs_vin_decl(3);
483sysfs_vin_decl(4);
484sysfs_vin_decl(5);
485sysfs_vin_decl(6);
486sysfs_vin_decl(7);
487sysfs_vin_decl(8);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488
489/* use a different set of functions for in0 */
490static ssize_t show_in_0(struct w83627hf_data *data, char *buf, u8 reg)
491{
492 long in0;
493
494 if ((data->vrm_ovt & 0x01) &&
Jean Delvarec2db6ce2006-01-18 23:22:12 +0100495 (w83627thf == data->type || w83637hf == data->type
496 || w83687thf == data->type))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497
498 /* use VRM9 calculation */
499 in0 = (long)((reg * 488 + 70000 + 50) / 100);
500 else
501 /* use VRM8 (standard) calculation */
502 in0 = (long)IN_FROM_REG(reg);
503
504 return sprintf(buf,"%ld\n", in0);
505}
506
Yani Ioannoua5099cf2005-05-17 06:42:25 -0400507static ssize_t show_regs_in_0(struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508{
509 struct w83627hf_data *data = w83627hf_update_device(dev);
510 return show_in_0(data, buf, data->in[0]);
511}
512
Yani Ioannoua5099cf2005-05-17 06:42:25 -0400513static ssize_t show_regs_in_min0(struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514{
515 struct w83627hf_data *data = w83627hf_update_device(dev);
516 return show_in_0(data, buf, data->in_min[0]);
517}
518
Yani Ioannoua5099cf2005-05-17 06:42:25 -0400519static ssize_t show_regs_in_max0(struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520{
521 struct w83627hf_data *data = w83627hf_update_device(dev);
522 return show_in_0(data, buf, data->in_max[0]);
523}
524
Yani Ioannoua5099cf2005-05-17 06:42:25 -0400525static ssize_t store_regs_in_min0(struct device *dev, struct device_attribute *attr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526 const char *buf, size_t count)
527{
Jean Delvare787c72b2007-05-08 17:22:00 +0200528 struct w83627hf_data *data = dev_get_drvdata(dev);
Guenter Roeck27b9de32012-01-15 11:07:26 -0800529 unsigned long val;
530 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531
Guenter Roeck27b9de32012-01-15 11:07:26 -0800532 err = kstrtoul(buf, 10, &val);
533 if (err)
534 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100536 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537
538 if ((data->vrm_ovt & 0x01) &&
Jean Delvarec2db6ce2006-01-18 23:22:12 +0100539 (w83627thf == data->type || w83637hf == data->type
540 || w83687thf == data->type))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541
542 /* use VRM9 calculation */
Yuan Mu2723ab92005-11-23 15:44:21 -0800543 data->in_min[0] =
544 SENSORS_LIMIT(((val * 100) - 70000 + 244) / 488, 0,
545 255);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546 else
547 /* use VRM8 (standard) calculation */
548 data->in_min[0] = IN_TO_REG(val);
549
Jean Delvare787c72b2007-05-08 17:22:00 +0200550 w83627hf_write_value(data, W83781D_REG_IN_MIN(0), data->in_min[0]);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100551 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552 return count;
553}
554
Yani Ioannoua5099cf2005-05-17 06:42:25 -0400555static ssize_t store_regs_in_max0(struct device *dev, struct device_attribute *attr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556 const char *buf, size_t count)
557{
Jean Delvare787c72b2007-05-08 17:22:00 +0200558 struct w83627hf_data *data = dev_get_drvdata(dev);
Guenter Roeck27b9de32012-01-15 11:07:26 -0800559 unsigned long val;
560 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561
Guenter Roeck27b9de32012-01-15 11:07:26 -0800562 err = kstrtoul(buf, 10, &val);
563 if (err)
564 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100566 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567
568 if ((data->vrm_ovt & 0x01) &&
Jean Delvarec2db6ce2006-01-18 23:22:12 +0100569 (w83627thf == data->type || w83637hf == data->type
570 || w83687thf == data->type))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571
572 /* use VRM9 calculation */
Yuan Mu2723ab92005-11-23 15:44:21 -0800573 data->in_max[0] =
574 SENSORS_LIMIT(((val * 100) - 70000 + 244) / 488, 0,
575 255);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576 else
577 /* use VRM8 (standard) calculation */
578 data->in_max[0] = IN_TO_REG(val);
579
Jean Delvare787c72b2007-05-08 17:22:00 +0200580 w83627hf_write_value(data, W83781D_REG_IN_MAX(0), data->in_max[0]);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100581 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582 return count;
583}
584
585static DEVICE_ATTR(in0_input, S_IRUGO, show_regs_in_0, NULL);
586static DEVICE_ATTR(in0_min, S_IRUGO | S_IWUSR,
587 show_regs_in_min0, store_regs_in_min0);
588static DEVICE_ATTR(in0_max, S_IRUGO | S_IWUSR,
589 show_regs_in_max0, store_regs_in_max0);
590
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591static ssize_t
Jim Cromie07584c72007-10-12 21:08:00 +0200592show_fan_input(struct device *dev, struct device_attribute *devattr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593{
Jim Cromie07584c72007-10-12 21:08:00 +0200594 int nr = to_sensor_dev_attr(devattr)->index;
595 struct w83627hf_data *data = w83627hf_update_device(dev);
596 return sprintf(buf, "%ld\n", FAN_FROM_REG(data->fan[nr],
597 (long)DIV_FROM_REG(data->fan_div[nr])));
598}
599static ssize_t
600show_fan_min(struct device *dev, struct device_attribute *devattr, char *buf)
601{
602 int nr = to_sensor_dev_attr(devattr)->index;
603 struct w83627hf_data *data = w83627hf_update_device(dev);
604 return sprintf(buf, "%ld\n", FAN_FROM_REG(data->fan_min[nr],
605 (long)DIV_FROM_REG(data->fan_div[nr])));
606}
607static ssize_t
608store_fan_min(struct device *dev, struct device_attribute *devattr,
609 const char *buf, size_t count)
610{
611 int nr = to_sensor_dev_attr(devattr)->index;
Jean Delvare787c72b2007-05-08 17:22:00 +0200612 struct w83627hf_data *data = dev_get_drvdata(dev);
Guenter Roeck27b9de32012-01-15 11:07:26 -0800613 unsigned long val;
614 int err;
615
616 err = kstrtoul(buf, 10, &val);
617 if (err)
618 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100620 mutex_lock(&data->update_lock);
Jim Cromie07584c72007-10-12 21:08:00 +0200621 data->fan_min[nr] = FAN_TO_REG(val, DIV_FROM_REG(data->fan_div[nr]));
Jim Cromie2ca2fcd2007-10-14 17:20:50 -0600622 w83627hf_write_value(data, W83627HF_REG_FAN_MIN(nr),
Jim Cromie07584c72007-10-12 21:08:00 +0200623 data->fan_min[nr]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100625 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626 return count;
627}
Jim Cromie07584c72007-10-12 21:08:00 +0200628#define sysfs_fan_decl(offset) \
629static SENSOR_DEVICE_ATTR(fan##offset##_input, S_IRUGO, \
630 show_fan_input, NULL, offset - 1); \
631static SENSOR_DEVICE_ATTR(fan##offset##_min, S_IRUGO | S_IWUSR, \
632 show_fan_min, store_fan_min, offset - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633
Jim Cromie07584c72007-10-12 21:08:00 +0200634sysfs_fan_decl(1);
635sysfs_fan_decl(2);
636sysfs_fan_decl(3);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637
Jim Cromie07584c72007-10-12 21:08:00 +0200638static ssize_t
639show_temp(struct device *dev, struct device_attribute *devattr, char *buf)
640{
641 int nr = to_sensor_dev_attr(devattr)->index;
642 struct w83627hf_data *data = w83627hf_update_device(dev);
Jim Cromiedf48ed82007-10-14 17:10:52 -0600643
644 u16 tmp = data->temp[nr];
645 return sprintf(buf, "%ld\n", (nr) ? (long) LM75_TEMP_FROM_REG(tmp)
646 : (long) TEMP_FROM_REG(tmp));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648
Jim Cromie07584c72007-10-12 21:08:00 +0200649static ssize_t
650show_temp_max(struct device *dev, struct device_attribute *devattr,
651 char *buf)
652{
653 int nr = to_sensor_dev_attr(devattr)->index;
654 struct w83627hf_data *data = w83627hf_update_device(dev);
Jim Cromiedf48ed82007-10-14 17:10:52 -0600655
656 u16 tmp = data->temp_max[nr];
657 return sprintf(buf, "%ld\n", (nr) ? (long) LM75_TEMP_FROM_REG(tmp)
658 : (long) TEMP_FROM_REG(tmp));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660
Jim Cromie07584c72007-10-12 21:08:00 +0200661static ssize_t
662show_temp_max_hyst(struct device *dev, struct device_attribute *devattr,
663 char *buf)
664{
665 int nr = to_sensor_dev_attr(devattr)->index;
666 struct w83627hf_data *data = w83627hf_update_device(dev);
Jim Cromiedf48ed82007-10-14 17:10:52 -0600667
668 u16 tmp = data->temp_max_hyst[nr];
669 return sprintf(buf, "%ld\n", (nr) ? (long) LM75_TEMP_FROM_REG(tmp)
670 : (long) TEMP_FROM_REG(tmp));
Jim Cromie07584c72007-10-12 21:08:00 +0200671}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672
Jim Cromie07584c72007-10-12 21:08:00 +0200673static ssize_t
674store_temp_max(struct device *dev, struct device_attribute *devattr,
675 const char *buf, size_t count)
676{
677 int nr = to_sensor_dev_attr(devattr)->index;
678 struct w83627hf_data *data = dev_get_drvdata(dev);
Guenter Roeck27b9de32012-01-15 11:07:26 -0800679 u16 tmp;
680 long val;
681 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682
Guenter Roeck27b9de32012-01-15 11:07:26 -0800683 err = kstrtol(buf, 10, &val);
684 if (err)
685 return err;
686
687 tmp = (nr) ? LM75_TEMP_TO_REG(val) : TEMP_TO_REG(val);
Jim Cromie07584c72007-10-12 21:08:00 +0200688 mutex_lock(&data->update_lock);
Jim Cromiedf48ed82007-10-14 17:10:52 -0600689 data->temp_max[nr] = tmp;
690 w83627hf_write_value(data, w83627hf_reg_temp_over[nr], tmp);
Jim Cromie07584c72007-10-12 21:08:00 +0200691 mutex_unlock(&data->update_lock);
692 return count;
693}
694
695static ssize_t
696store_temp_max_hyst(struct device *dev, struct device_attribute *devattr,
697 const char *buf, size_t count)
698{
699 int nr = to_sensor_dev_attr(devattr)->index;
700 struct w83627hf_data *data = dev_get_drvdata(dev);
Guenter Roeck27b9de32012-01-15 11:07:26 -0800701 u16 tmp;
702 long val;
703 int err;
Jim Cromie07584c72007-10-12 21:08:00 +0200704
Guenter Roeck27b9de32012-01-15 11:07:26 -0800705 err = kstrtol(buf, 10, &val);
706 if (err)
707 return err;
708
709 tmp = (nr) ? LM75_TEMP_TO_REG(val) : TEMP_TO_REG(val);
Jim Cromie07584c72007-10-12 21:08:00 +0200710 mutex_lock(&data->update_lock);
Jim Cromiedf48ed82007-10-14 17:10:52 -0600711 data->temp_max_hyst[nr] = tmp;
712 w83627hf_write_value(data, w83627hf_reg_temp_hyst[nr], tmp);
Jim Cromie07584c72007-10-12 21:08:00 +0200713 mutex_unlock(&data->update_lock);
714 return count;
715}
716
717#define sysfs_temp_decl(offset) \
718static SENSOR_DEVICE_ATTR(temp##offset##_input, S_IRUGO, \
Jim Cromiedf48ed82007-10-14 17:10:52 -0600719 show_temp, NULL, offset - 1); \
Jim Cromie07584c72007-10-12 21:08:00 +0200720static SENSOR_DEVICE_ATTR(temp##offset##_max, S_IRUGO|S_IWUSR, \
Jim Cromiedf48ed82007-10-14 17:10:52 -0600721 show_temp_max, store_temp_max, offset - 1); \
Jim Cromie07584c72007-10-12 21:08:00 +0200722static SENSOR_DEVICE_ATTR(temp##offset##_max_hyst, S_IRUGO|S_IWUSR, \
Jim Cromiedf48ed82007-10-14 17:10:52 -0600723 show_temp_max_hyst, store_temp_max_hyst, offset - 1);
Jim Cromie07584c72007-10-12 21:08:00 +0200724
725sysfs_temp_decl(1);
726sysfs_temp_decl(2);
727sysfs_temp_decl(3);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729static ssize_t
Yani Ioannoua5099cf2005-05-17 06:42:25 -0400730show_vid_reg(struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731{
732 struct w83627hf_data *data = w83627hf_update_device(dev);
733 return sprintf(buf, "%ld\n", (long) vid_from_reg(data->vid, data->vrm));
734}
735static DEVICE_ATTR(cpu0_vid, S_IRUGO, show_vid_reg, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736
737static ssize_t
Yani Ioannoua5099cf2005-05-17 06:42:25 -0400738show_vrm_reg(struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739{
Jean Delvare90d66192007-10-08 18:24:35 +0200740 struct w83627hf_data *data = dev_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741 return sprintf(buf, "%ld\n", (long) data->vrm);
742}
743static ssize_t
Yani Ioannoua5099cf2005-05-17 06:42:25 -0400744store_vrm_reg(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745{
Jean Delvare787c72b2007-05-08 17:22:00 +0200746 struct w83627hf_data *data = dev_get_drvdata(dev);
Guenter Roeck27b9de32012-01-15 11:07:26 -0800747 unsigned long val;
748 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749
Guenter Roeck27b9de32012-01-15 11:07:26 -0800750 err = kstrtoul(buf, 10, &val);
751 if (err)
752 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753 data->vrm = val;
754
755 return count;
756}
757static DEVICE_ATTR(vrm, S_IRUGO | S_IWUSR, show_vrm_reg, store_vrm_reg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758
759static ssize_t
Yani Ioannoua5099cf2005-05-17 06:42:25 -0400760show_alarms_reg(struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761{
762 struct w83627hf_data *data = w83627hf_update_device(dev);
763 return sprintf(buf, "%ld\n", (long) data->alarms);
764}
765static DEVICE_ATTR(alarms, S_IRUGO, show_alarms_reg, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766
Jean Delvaree3604c62008-01-03 23:00:30 +0100767static ssize_t
768show_alarm(struct device *dev, struct device_attribute *attr, char *buf)
769{
770 struct w83627hf_data *data = w83627hf_update_device(dev);
771 int bitnr = to_sensor_dev_attr(attr)->index;
772 return sprintf(buf, "%u\n", (data->alarms >> bitnr) & 1);
773}
774static SENSOR_DEVICE_ATTR(in0_alarm, S_IRUGO, show_alarm, NULL, 0);
775static SENSOR_DEVICE_ATTR(in1_alarm, S_IRUGO, show_alarm, NULL, 1);
776static SENSOR_DEVICE_ATTR(in2_alarm, S_IRUGO, show_alarm, NULL, 2);
777static SENSOR_DEVICE_ATTR(in3_alarm, S_IRUGO, show_alarm, NULL, 3);
778static SENSOR_DEVICE_ATTR(in4_alarm, S_IRUGO, show_alarm, NULL, 8);
779static SENSOR_DEVICE_ATTR(in5_alarm, S_IRUGO, show_alarm, NULL, 9);
780static SENSOR_DEVICE_ATTR(in6_alarm, S_IRUGO, show_alarm, NULL, 10);
781static SENSOR_DEVICE_ATTR(in7_alarm, S_IRUGO, show_alarm, NULL, 16);
782static SENSOR_DEVICE_ATTR(in8_alarm, S_IRUGO, show_alarm, NULL, 17);
783static SENSOR_DEVICE_ATTR(fan1_alarm, S_IRUGO, show_alarm, NULL, 6);
784static SENSOR_DEVICE_ATTR(fan2_alarm, S_IRUGO, show_alarm, NULL, 7);
785static SENSOR_DEVICE_ATTR(fan3_alarm, S_IRUGO, show_alarm, NULL, 11);
786static SENSOR_DEVICE_ATTR(temp1_alarm, S_IRUGO, show_alarm, NULL, 4);
787static SENSOR_DEVICE_ATTR(temp2_alarm, S_IRUGO, show_alarm, NULL, 5);
788static SENSOR_DEVICE_ATTR(temp3_alarm, S_IRUGO, show_alarm, NULL, 13);
789
Jean Delvare1c138102008-01-03 23:04:55 +0100790static ssize_t
791show_beep_mask(struct device *dev, struct device_attribute *attr, char *buf)
792{
793 struct w83627hf_data *data = w83627hf_update_device(dev);
794 return sprintf(buf, "%ld\n",
795 (long)BEEP_MASK_FROM_REG(data->beep_mask));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797
798static ssize_t
Jean Delvare1c138102008-01-03 23:04:55 +0100799store_beep_mask(struct device *dev, struct device_attribute *attr,
800 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801{
Jean Delvare787c72b2007-05-08 17:22:00 +0200802 struct w83627hf_data *data = dev_get_drvdata(dev);
Jean Delvare1c138102008-01-03 23:04:55 +0100803 unsigned long val;
Guenter Roeck27b9de32012-01-15 11:07:26 -0800804 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805
Guenter Roeck27b9de32012-01-15 11:07:26 -0800806 err = kstrtoul(buf, 10, &val);
807 if (err)
808 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100810 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811
Jean Delvare1c138102008-01-03 23:04:55 +0100812 /* preserve beep enable */
813 data->beep_mask = (data->beep_mask & 0x8000)
814 | BEEP_MASK_TO_REG(val);
815 w83627hf_write_value(data, W83781D_REG_BEEP_INTS1,
816 data->beep_mask & 0xff);
817 w83627hf_write_value(data, W83781D_REG_BEEP_INTS3,
818 ((data->beep_mask) >> 16) & 0xff);
Jean Delvare787c72b2007-05-08 17:22:00 +0200819 w83627hf_write_value(data, W83781D_REG_BEEP_INTS2,
Jean Delvare1c138102008-01-03 23:04:55 +0100820 (data->beep_mask >> 8) & 0xff);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100822 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823 return count;
824}
825
Jean Delvare1c138102008-01-03 23:04:55 +0100826static DEVICE_ATTR(beep_mask, S_IRUGO | S_IWUSR,
827 show_beep_mask, store_beep_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829static ssize_t
Jean Delvaree3604c62008-01-03 23:00:30 +0100830show_beep(struct device *dev, struct device_attribute *attr, char *buf)
831{
832 struct w83627hf_data *data = w83627hf_update_device(dev);
833 int bitnr = to_sensor_dev_attr(attr)->index;
834 return sprintf(buf, "%u\n", (data->beep_mask >> bitnr) & 1);
835}
836
837static ssize_t
838store_beep(struct device *dev, struct device_attribute *attr,
839 const char *buf, size_t count)
840{
841 struct w83627hf_data *data = dev_get_drvdata(dev);
842 int bitnr = to_sensor_dev_attr(attr)->index;
Jean Delvaree3604c62008-01-03 23:00:30 +0100843 u8 reg;
Guenter Roeck27b9de32012-01-15 11:07:26 -0800844 unsigned long bit;
845 int err;
Jean Delvaree3604c62008-01-03 23:00:30 +0100846
Guenter Roeck27b9de32012-01-15 11:07:26 -0800847 err = kstrtoul(buf, 10, &bit);
848 if (err)
849 return err;
850
Jean Delvaree3604c62008-01-03 23:00:30 +0100851 if (bit & ~1)
852 return -EINVAL;
853
854 mutex_lock(&data->update_lock);
855 if (bit)
856 data->beep_mask |= (1 << bitnr);
857 else
858 data->beep_mask &= ~(1 << bitnr);
859
860 if (bitnr < 8) {
861 reg = w83627hf_read_value(data, W83781D_REG_BEEP_INTS1);
862 if (bit)
863 reg |= (1 << bitnr);
864 else
865 reg &= ~(1 << bitnr);
866 w83627hf_write_value(data, W83781D_REG_BEEP_INTS1, reg);
867 } else if (bitnr < 16) {
868 reg = w83627hf_read_value(data, W83781D_REG_BEEP_INTS2);
869 if (bit)
870 reg |= (1 << (bitnr - 8));
871 else
872 reg &= ~(1 << (bitnr - 8));
873 w83627hf_write_value(data, W83781D_REG_BEEP_INTS2, reg);
874 } else {
875 reg = w83627hf_read_value(data, W83781D_REG_BEEP_INTS3);
876 if (bit)
877 reg |= (1 << (bitnr - 16));
878 else
879 reg &= ~(1 << (bitnr - 16));
880 w83627hf_write_value(data, W83781D_REG_BEEP_INTS3, reg);
881 }
882 mutex_unlock(&data->update_lock);
883
884 return count;
885}
886
887static SENSOR_DEVICE_ATTR(in0_beep, S_IRUGO | S_IWUSR,
888 show_beep, store_beep, 0);
889static SENSOR_DEVICE_ATTR(in1_beep, S_IRUGO | S_IWUSR,
890 show_beep, store_beep, 1);
891static SENSOR_DEVICE_ATTR(in2_beep, S_IRUGO | S_IWUSR,
892 show_beep, store_beep, 2);
893static SENSOR_DEVICE_ATTR(in3_beep, S_IRUGO | S_IWUSR,
894 show_beep, store_beep, 3);
895static SENSOR_DEVICE_ATTR(in4_beep, S_IRUGO | S_IWUSR,
896 show_beep, store_beep, 8);
897static SENSOR_DEVICE_ATTR(in5_beep, S_IRUGO | S_IWUSR,
898 show_beep, store_beep, 9);
899static SENSOR_DEVICE_ATTR(in6_beep, S_IRUGO | S_IWUSR,
900 show_beep, store_beep, 10);
901static SENSOR_DEVICE_ATTR(in7_beep, S_IRUGO | S_IWUSR,
902 show_beep, store_beep, 16);
903static SENSOR_DEVICE_ATTR(in8_beep, S_IRUGO | S_IWUSR,
904 show_beep, store_beep, 17);
905static SENSOR_DEVICE_ATTR(fan1_beep, S_IRUGO | S_IWUSR,
906 show_beep, store_beep, 6);
907static SENSOR_DEVICE_ATTR(fan2_beep, S_IRUGO | S_IWUSR,
908 show_beep, store_beep, 7);
909static SENSOR_DEVICE_ATTR(fan3_beep, S_IRUGO | S_IWUSR,
910 show_beep, store_beep, 11);
911static SENSOR_DEVICE_ATTR(temp1_beep, S_IRUGO | S_IWUSR,
912 show_beep, store_beep, 4);
913static SENSOR_DEVICE_ATTR(temp2_beep, S_IRUGO | S_IWUSR,
914 show_beep, store_beep, 5);
915static SENSOR_DEVICE_ATTR(temp3_beep, S_IRUGO | S_IWUSR,
916 show_beep, store_beep, 13);
Jean Delvare1c138102008-01-03 23:04:55 +0100917static SENSOR_DEVICE_ATTR(beep_enable, S_IRUGO | S_IWUSR,
918 show_beep, store_beep, 15);
Jean Delvaree3604c62008-01-03 23:00:30 +0100919
920static ssize_t
Jim Cromie07584c72007-10-12 21:08:00 +0200921show_fan_div(struct device *dev, struct device_attribute *devattr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922{
Jim Cromie07584c72007-10-12 21:08:00 +0200923 int nr = to_sensor_dev_attr(devattr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700924 struct w83627hf_data *data = w83627hf_update_device(dev);
925 return sprintf(buf, "%ld\n",
Jim Cromie07584c72007-10-12 21:08:00 +0200926 (long) DIV_FROM_REG(data->fan_div[nr]));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927}
Guenter Roeck27b9de32012-01-15 11:07:26 -0800928/*
929 * Note: we save and restore the fan minimum here, because its value is
930 * determined in part by the fan divisor. This follows the principle of
931 * least surprise; the user doesn't expect the fan minimum to change just
932 * because the divisor changed.
933 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934static ssize_t
Jim Cromie07584c72007-10-12 21:08:00 +0200935store_fan_div(struct device *dev, struct device_attribute *devattr,
936 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937{
Jim Cromie07584c72007-10-12 21:08:00 +0200938 int nr = to_sensor_dev_attr(devattr)->index;
Jean Delvare787c72b2007-05-08 17:22:00 +0200939 struct w83627hf_data *data = dev_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940 unsigned long min;
941 u8 reg;
Guenter Roeck27b9de32012-01-15 11:07:26 -0800942 unsigned long val;
943 int err;
944
945 err = kstrtoul(buf, 10, &val);
946 if (err)
947 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100949 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950
951 /* Save fan_min */
952 min = FAN_FROM_REG(data->fan_min[nr],
953 DIV_FROM_REG(data->fan_div[nr]));
954
955 data->fan_div[nr] = DIV_TO_REG(val);
956
Jean Delvare787c72b2007-05-08 17:22:00 +0200957 reg = (w83627hf_read_value(data, nr==2 ? W83781D_REG_PIN : W83781D_REG_VID_FANDIV)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958 & (nr==0 ? 0xcf : 0x3f))
959 | ((data->fan_div[nr] & 0x03) << (nr==0 ? 4 : 6));
Jean Delvare787c72b2007-05-08 17:22:00 +0200960 w83627hf_write_value(data, nr==2 ? W83781D_REG_PIN : W83781D_REG_VID_FANDIV, reg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961
Jean Delvare787c72b2007-05-08 17:22:00 +0200962 reg = (w83627hf_read_value(data, W83781D_REG_VBAT)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700963 & ~(1 << (5 + nr)))
964 | ((data->fan_div[nr] & 0x04) << (3 + nr));
Jean Delvare787c72b2007-05-08 17:22:00 +0200965 w83627hf_write_value(data, W83781D_REG_VBAT, reg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966
967 /* Restore fan_min */
968 data->fan_min[nr] = FAN_TO_REG(min, DIV_FROM_REG(data->fan_div[nr]));
Jim Cromie2ca2fcd2007-10-14 17:20:50 -0600969 w83627hf_write_value(data, W83627HF_REG_FAN_MIN(nr), data->fan_min[nr]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100971 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972 return count;
973}
974
Jim Cromie07584c72007-10-12 21:08:00 +0200975static SENSOR_DEVICE_ATTR(fan1_div, S_IRUGO|S_IWUSR,
976 show_fan_div, store_fan_div, 0);
977static SENSOR_DEVICE_ATTR(fan2_div, S_IRUGO|S_IWUSR,
978 show_fan_div, store_fan_div, 1);
979static SENSOR_DEVICE_ATTR(fan3_div, S_IRUGO|S_IWUSR,
980 show_fan_div, store_fan_div, 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981
Linus Torvalds1da177e2005-04-16 15:20:36 -0700982static ssize_t
Jim Cromie07584c72007-10-12 21:08:00 +0200983show_pwm(struct device *dev, struct device_attribute *devattr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984{
Jim Cromie07584c72007-10-12 21:08:00 +0200985 int nr = to_sensor_dev_attr(devattr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700986 struct w83627hf_data *data = w83627hf_update_device(dev);
Jim Cromie07584c72007-10-12 21:08:00 +0200987 return sprintf(buf, "%ld\n", (long) data->pwm[nr]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988}
989
990static ssize_t
Jim Cromie07584c72007-10-12 21:08:00 +0200991store_pwm(struct device *dev, struct device_attribute *devattr,
992 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700993{
Jim Cromie07584c72007-10-12 21:08:00 +0200994 int nr = to_sensor_dev_attr(devattr)->index;
Jean Delvare787c72b2007-05-08 17:22:00 +0200995 struct w83627hf_data *data = dev_get_drvdata(dev);
Guenter Roeck27b9de32012-01-15 11:07:26 -0800996 unsigned long val;
997 int err;
998
999 err = kstrtoul(buf, 10, &val);
1000 if (err)
1001 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002
Ingo Molnar9a61bf62006-01-18 23:19:26 +01001003 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004
1005 if (data->type == w83627thf) {
1006 /* bits 0-3 are reserved in 627THF */
Jim Cromie07584c72007-10-12 21:08:00 +02001007 data->pwm[nr] = PWM_TO_REG(val) & 0xf0;
Jean Delvare787c72b2007-05-08 17:22:00 +02001008 w83627hf_write_value(data,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001009 W836X7HF_REG_PWM(data->type, nr),
Jim Cromie07584c72007-10-12 21:08:00 +02001010 data->pwm[nr] |
Jean Delvare787c72b2007-05-08 17:22:00 +02001011 (w83627hf_read_value(data,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001012 W836X7HF_REG_PWM(data->type, nr)) & 0x0f));
1013 } else {
Jim Cromie07584c72007-10-12 21:08:00 +02001014 data->pwm[nr] = PWM_TO_REG(val);
Jean Delvare787c72b2007-05-08 17:22:00 +02001015 w83627hf_write_value(data,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001016 W836X7HF_REG_PWM(data->type, nr),
Jim Cromie07584c72007-10-12 21:08:00 +02001017 data->pwm[nr]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001018 }
1019
Ingo Molnar9a61bf62006-01-18 23:19:26 +01001020 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021 return count;
1022}
1023
Jim Cromie07584c72007-10-12 21:08:00 +02001024static SENSOR_DEVICE_ATTR(pwm1, S_IRUGO|S_IWUSR, show_pwm, store_pwm, 0);
1025static SENSOR_DEVICE_ATTR(pwm2, S_IRUGO|S_IWUSR, show_pwm, store_pwm, 1);
1026static SENSOR_DEVICE_ATTR(pwm3, S_IRUGO|S_IWUSR, show_pwm, store_pwm, 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027
Linus Torvalds1da177e2005-04-16 15:20:36 -07001028static ssize_t
Dominik Geyera95a5ed2008-08-06 22:41:04 +02001029show_pwm_enable(struct device *dev, struct device_attribute *devattr, char *buf)
1030{
1031 int nr = to_sensor_dev_attr(devattr)->index;
1032 struct w83627hf_data *data = w83627hf_update_device(dev);
1033 return sprintf(buf, "%d\n", data->pwm_enable[nr]);
1034}
1035
1036static ssize_t
1037store_pwm_enable(struct device *dev, struct device_attribute *devattr,
1038 const char *buf, size_t count)
1039{
1040 int nr = to_sensor_dev_attr(devattr)->index;
1041 struct w83627hf_data *data = dev_get_drvdata(dev);
Dominik Geyera95a5ed2008-08-06 22:41:04 +02001042 u8 reg;
Guenter Roeck27b9de32012-01-15 11:07:26 -08001043 unsigned long val;
1044 int err;
Dominik Geyera95a5ed2008-08-06 22:41:04 +02001045
Guenter Roeck27b9de32012-01-15 11:07:26 -08001046 err = kstrtoul(buf, 10, &val);
1047 if (err)
1048 return err;
1049
1050 if (!val || val > 3) /* modes 1, 2 and 3 are supported */
Dominik Geyera95a5ed2008-08-06 22:41:04 +02001051 return -EINVAL;
1052 mutex_lock(&data->update_lock);
1053 data->pwm_enable[nr] = val;
1054 reg = w83627hf_read_value(data, W83627THF_REG_PWM_ENABLE[nr]);
1055 reg &= ~(0x03 << W83627THF_PWM_ENABLE_SHIFT[nr]);
1056 reg |= (val - 1) << W83627THF_PWM_ENABLE_SHIFT[nr];
1057 w83627hf_write_value(data, W83627THF_REG_PWM_ENABLE[nr], reg);
1058 mutex_unlock(&data->update_lock);
1059 return count;
1060}
1061
1062static SENSOR_DEVICE_ATTR(pwm1_enable, S_IRUGO|S_IWUSR, show_pwm_enable,
1063 store_pwm_enable, 0);
1064static SENSOR_DEVICE_ATTR(pwm2_enable, S_IRUGO|S_IWUSR, show_pwm_enable,
1065 store_pwm_enable, 1);
1066static SENSOR_DEVICE_ATTR(pwm3_enable, S_IRUGO|S_IWUSR, show_pwm_enable,
1067 store_pwm_enable, 2);
1068
1069static ssize_t
Jim Cromie07584c72007-10-12 21:08:00 +02001070show_pwm_freq(struct device *dev, struct device_attribute *devattr, char *buf)
Carlos Olalla Martinez1550cb62007-06-09 10:11:16 -04001071{
Jim Cromie07584c72007-10-12 21:08:00 +02001072 int nr = to_sensor_dev_attr(devattr)->index;
Carlos Olalla Martinez1550cb62007-06-09 10:11:16 -04001073 struct w83627hf_data *data = w83627hf_update_device(dev);
1074 if (data->type == w83627hf)
1075 return sprintf(buf, "%ld\n",
Jim Cromie07584c72007-10-12 21:08:00 +02001076 pwm_freq_from_reg_627hf(data->pwm_freq[nr]));
Carlos Olalla Martinez1550cb62007-06-09 10:11:16 -04001077 else
1078 return sprintf(buf, "%ld\n",
Jim Cromie07584c72007-10-12 21:08:00 +02001079 pwm_freq_from_reg(data->pwm_freq[nr]));
Carlos Olalla Martinez1550cb62007-06-09 10:11:16 -04001080}
1081
1082static ssize_t
Jim Cromie07584c72007-10-12 21:08:00 +02001083store_pwm_freq(struct device *dev, struct device_attribute *devattr,
1084 const char *buf, size_t count)
Carlos Olalla Martinez1550cb62007-06-09 10:11:16 -04001085{
Jim Cromie07584c72007-10-12 21:08:00 +02001086 int nr = to_sensor_dev_attr(devattr)->index;
Carlos Olalla Martinez1550cb62007-06-09 10:11:16 -04001087 struct w83627hf_data *data = dev_get_drvdata(dev);
1088 static const u8 mask[]={0xF8, 0x8F};
Guenter Roeck27b9de32012-01-15 11:07:26 -08001089 unsigned long val;
1090 int err;
Carlos Olalla Martinez1550cb62007-06-09 10:11:16 -04001091
Guenter Roeck27b9de32012-01-15 11:07:26 -08001092 err = kstrtoul(buf, 10, &val);
1093 if (err)
1094 return err;
Carlos Olalla Martinez1550cb62007-06-09 10:11:16 -04001095
1096 mutex_lock(&data->update_lock);
1097
1098 if (data->type == w83627hf) {
Jim Cromie07584c72007-10-12 21:08:00 +02001099 data->pwm_freq[nr] = pwm_freq_to_reg_627hf(val);
Carlos Olalla Martinez1550cb62007-06-09 10:11:16 -04001100 w83627hf_write_value(data, W83627HF_REG_PWM_FREQ,
Jim Cromie07584c72007-10-12 21:08:00 +02001101 (data->pwm_freq[nr] << (nr*4)) |
Carlos Olalla Martinez1550cb62007-06-09 10:11:16 -04001102 (w83627hf_read_value(data,
Jim Cromie07584c72007-10-12 21:08:00 +02001103 W83627HF_REG_PWM_FREQ) & mask[nr]));
Carlos Olalla Martinez1550cb62007-06-09 10:11:16 -04001104 } else {
Jim Cromie07584c72007-10-12 21:08:00 +02001105 data->pwm_freq[nr] = pwm_freq_to_reg(val);
1106 w83627hf_write_value(data, W83637HF_REG_PWM_FREQ[nr],
1107 data->pwm_freq[nr]);
Carlos Olalla Martinez1550cb62007-06-09 10:11:16 -04001108 }
1109
1110 mutex_unlock(&data->update_lock);
1111 return count;
1112}
1113
Jim Cromie07584c72007-10-12 21:08:00 +02001114static SENSOR_DEVICE_ATTR(pwm1_freq, S_IRUGO|S_IWUSR,
1115 show_pwm_freq, store_pwm_freq, 0);
1116static SENSOR_DEVICE_ATTR(pwm2_freq, S_IRUGO|S_IWUSR,
1117 show_pwm_freq, store_pwm_freq, 1);
1118static SENSOR_DEVICE_ATTR(pwm3_freq, S_IRUGO|S_IWUSR,
1119 show_pwm_freq, store_pwm_freq, 2);
Carlos Olalla Martinez1550cb62007-06-09 10:11:16 -04001120
1121static ssize_t
Jim Cromie07584c72007-10-12 21:08:00 +02001122show_temp_type(struct device *dev, struct device_attribute *devattr,
1123 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001124{
Jim Cromie07584c72007-10-12 21:08:00 +02001125 int nr = to_sensor_dev_attr(devattr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001126 struct w83627hf_data *data = w83627hf_update_device(dev);
Jim Cromie07584c72007-10-12 21:08:00 +02001127 return sprintf(buf, "%ld\n", (long) data->sens[nr]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001128}
1129
1130static ssize_t
Jim Cromie07584c72007-10-12 21:08:00 +02001131store_temp_type(struct device *dev, struct device_attribute *devattr,
1132 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001133{
Jim Cromie07584c72007-10-12 21:08:00 +02001134 int nr = to_sensor_dev_attr(devattr)->index;
Jean Delvare787c72b2007-05-08 17:22:00 +02001135 struct w83627hf_data *data = dev_get_drvdata(dev);
Guenter Roeck27b9de32012-01-15 11:07:26 -08001136 unsigned long val;
1137 u32 tmp;
1138 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001139
Guenter Roeck27b9de32012-01-15 11:07:26 -08001140 err = kstrtoul(buf, 10, &val);
1141 if (err)
1142 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001143
Ingo Molnar9a61bf62006-01-18 23:19:26 +01001144 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001145
1146 switch (val) {
1147 case 1: /* PII/Celeron diode */
Jean Delvare787c72b2007-05-08 17:22:00 +02001148 tmp = w83627hf_read_value(data, W83781D_REG_SCFG1);
1149 w83627hf_write_value(data, W83781D_REG_SCFG1,
Jim Cromie07584c72007-10-12 21:08:00 +02001150 tmp | BIT_SCFG1[nr]);
Jean Delvare787c72b2007-05-08 17:22:00 +02001151 tmp = w83627hf_read_value(data, W83781D_REG_SCFG2);
1152 w83627hf_write_value(data, W83781D_REG_SCFG2,
Jim Cromie07584c72007-10-12 21:08:00 +02001153 tmp | BIT_SCFG2[nr]);
1154 data->sens[nr] = val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001155 break;
1156 case 2: /* 3904 */
Jean Delvare787c72b2007-05-08 17:22:00 +02001157 tmp = w83627hf_read_value(data, W83781D_REG_SCFG1);
1158 w83627hf_write_value(data, W83781D_REG_SCFG1,
Jim Cromie07584c72007-10-12 21:08:00 +02001159 tmp | BIT_SCFG1[nr]);
Jean Delvare787c72b2007-05-08 17:22:00 +02001160 tmp = w83627hf_read_value(data, W83781D_REG_SCFG2);
1161 w83627hf_write_value(data, W83781D_REG_SCFG2,
Jim Cromie07584c72007-10-12 21:08:00 +02001162 tmp & ~BIT_SCFG2[nr]);
1163 data->sens[nr] = val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001164 break;
Jean Delvareb26f9332007-08-16 14:30:01 +02001165 case W83781D_DEFAULT_BETA:
1166 dev_warn(dev, "Sensor type %d is deprecated, please use 4 "
1167 "instead\n", W83781D_DEFAULT_BETA);
1168 /* fall through */
1169 case 4: /* thermistor */
Jean Delvare787c72b2007-05-08 17:22:00 +02001170 tmp = w83627hf_read_value(data, W83781D_REG_SCFG1);
1171 w83627hf_write_value(data, W83781D_REG_SCFG1,
Jim Cromie07584c72007-10-12 21:08:00 +02001172 tmp & ~BIT_SCFG1[nr]);
1173 data->sens[nr] = val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001174 break;
1175 default:
Jean Delvare787c72b2007-05-08 17:22:00 +02001176 dev_err(dev,
Jean Delvareb26f9332007-08-16 14:30:01 +02001177 "Invalid sensor type %ld; must be 1, 2, or 4\n",
1178 (long) val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001179 break;
1180 }
1181
Ingo Molnar9a61bf62006-01-18 23:19:26 +01001182 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001183 return count;
1184}
1185
Jim Cromie07584c72007-10-12 21:08:00 +02001186#define sysfs_temp_type(offset) \
1187static SENSOR_DEVICE_ATTR(temp##offset##_type, S_IRUGO | S_IWUSR, \
1188 show_temp_type, store_temp_type, offset - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001189
Jim Cromie07584c72007-10-12 21:08:00 +02001190sysfs_temp_type(1);
1191sysfs_temp_type(2);
1192sysfs_temp_type(3);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001193
Jim Cromie07584c72007-10-12 21:08:00 +02001194static ssize_t
1195show_name(struct device *dev, struct device_attribute *devattr, char *buf)
Jean Delvare787c72b2007-05-08 17:22:00 +02001196{
1197 struct w83627hf_data *data = dev_get_drvdata(dev);
1198
1199 return sprintf(buf, "%s\n", data->name);
1200}
1201static DEVICE_ATTR(name, S_IRUGO, show_name, NULL);
1202
1203static int __init w83627hf_find(int sioaddr, unsigned short *addr,
1204 struct w83627hf_sio_data *sio_data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001205{
Jean Delvared27c37c2007-05-08 17:21:59 +02001206 int err = -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001207 u16 val;
1208
Jean Delvare787c72b2007-05-08 17:22:00 +02001209 static const __initdata char *names[] = {
1210 "W83627HF",
1211 "W83627THF",
1212 "W83697HF",
1213 "W83637HF",
1214 "W83687THF",
1215 };
1216
Christian Schultec46c0e92009-12-16 21:38:29 +01001217 sio_data->sioaddr = sioaddr;
Jean Delvareb72656d2009-12-09 20:35:49 +01001218 superio_enter(sio_data);
1219 val = force_id ? force_id : superio_inb(sio_data, DEVID);
Jean Delvare787c72b2007-05-08 17:22:00 +02001220 switch (val) {
1221 case W627_DEVID:
1222 sio_data->type = w83627hf;
1223 break;
1224 case W627THF_DEVID:
1225 sio_data->type = w83627thf;
1226 break;
1227 case W697_DEVID:
1228 sio_data->type = w83697hf;
1229 break;
1230 case W637_DEVID:
1231 sio_data->type = w83637hf;
1232 break;
1233 case W687THF_DEVID:
1234 sio_data->type = w83687thf;
1235 break;
Jean Delvaree142e2a2007-05-27 22:17:43 +02001236 case 0xff: /* No device at all */
1237 goto exit;
Jean Delvare787c72b2007-05-08 17:22:00 +02001238 default:
Jean Delvaree142e2a2007-05-27 22:17:43 +02001239 pr_debug(DRVNAME ": Unsupported chip (DEVID=0x%02x)\n", val);
Jean Delvared27c37c2007-05-08 17:21:59 +02001240 goto exit;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001241 }
1242
Jean Delvareb72656d2009-12-09 20:35:49 +01001243 superio_select(sio_data, W83627HF_LD_HWM);
1244 val = (superio_inb(sio_data, WINB_BASE_REG) << 8) |
1245 superio_inb(sio_data, WINB_BASE_REG + 1);
Petr Vandrovecada0c2f2005-10-07 23:11:03 +02001246 *addr = val & WINB_ALIGNMENT;
Jean Delvared27c37c2007-05-08 17:21:59 +02001247 if (*addr == 0) {
Joe Perches18de0302010-10-20 06:51:55 +00001248 pr_warn("Base address not set, skipping\n");
Jean Delvared27c37c2007-05-08 17:21:59 +02001249 goto exit;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001250 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001251
Jean Delvareb72656d2009-12-09 20:35:49 +01001252 val = superio_inb(sio_data, WINB_ACT_REG);
Jean Delvared27c37c2007-05-08 17:21:59 +02001253 if (!(val & 0x01)) {
Joe Perches18de0302010-10-20 06:51:55 +00001254 pr_warn("Enabling HWM logical device\n");
Jean Delvareb72656d2009-12-09 20:35:49 +01001255 superio_outb(sio_data, WINB_ACT_REG, val | 0x01);
Jean Delvared27c37c2007-05-08 17:21:59 +02001256 }
1257
1258 err = 0;
Jean Delvare787c72b2007-05-08 17:22:00 +02001259 pr_info(DRVNAME ": Found %s chip at %#x\n",
1260 names[sio_data->type], *addr);
Jean Delvared27c37c2007-05-08 17:21:59 +02001261
1262 exit:
Jean Delvareb72656d2009-12-09 20:35:49 +01001263 superio_exit(sio_data);
Jean Delvared27c37c2007-05-08 17:21:59 +02001264 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001265}
1266
Jim Cromie07584c72007-10-12 21:08:00 +02001267#define VIN_UNIT_ATTRS(_X_) \
1268 &sensor_dev_attr_in##_X_##_input.dev_attr.attr, \
1269 &sensor_dev_attr_in##_X_##_min.dev_attr.attr, \
Jean Delvaree3604c62008-01-03 23:00:30 +01001270 &sensor_dev_attr_in##_X_##_max.dev_attr.attr, \
1271 &sensor_dev_attr_in##_X_##_alarm.dev_attr.attr, \
1272 &sensor_dev_attr_in##_X_##_beep.dev_attr.attr
Jim Cromie07584c72007-10-12 21:08:00 +02001273
1274#define FAN_UNIT_ATTRS(_X_) \
1275 &sensor_dev_attr_fan##_X_##_input.dev_attr.attr, \
1276 &sensor_dev_attr_fan##_X_##_min.dev_attr.attr, \
Jean Delvaree3604c62008-01-03 23:00:30 +01001277 &sensor_dev_attr_fan##_X_##_div.dev_attr.attr, \
1278 &sensor_dev_attr_fan##_X_##_alarm.dev_attr.attr, \
1279 &sensor_dev_attr_fan##_X_##_beep.dev_attr.attr
Jim Cromie07584c72007-10-12 21:08:00 +02001280
1281#define TEMP_UNIT_ATTRS(_X_) \
1282 &sensor_dev_attr_temp##_X_##_input.dev_attr.attr, \
1283 &sensor_dev_attr_temp##_X_##_max.dev_attr.attr, \
1284 &sensor_dev_attr_temp##_X_##_max_hyst.dev_attr.attr, \
Jean Delvaree3604c62008-01-03 23:00:30 +01001285 &sensor_dev_attr_temp##_X_##_type.dev_attr.attr, \
1286 &sensor_dev_attr_temp##_X_##_alarm.dev_attr.attr, \
1287 &sensor_dev_attr_temp##_X_##_beep.dev_attr.attr
Jim Cromie07584c72007-10-12 21:08:00 +02001288
Mark M. Hoffmanc1685f62006-09-24 20:59:49 +02001289static struct attribute *w83627hf_attributes[] = {
1290 &dev_attr_in0_input.attr,
1291 &dev_attr_in0_min.attr,
1292 &dev_attr_in0_max.attr,
Jean Delvaree3604c62008-01-03 23:00:30 +01001293 &sensor_dev_attr_in0_alarm.dev_attr.attr,
1294 &sensor_dev_attr_in0_beep.dev_attr.attr,
Jim Cromie07584c72007-10-12 21:08:00 +02001295 VIN_UNIT_ATTRS(2),
1296 VIN_UNIT_ATTRS(3),
1297 VIN_UNIT_ATTRS(4),
1298 VIN_UNIT_ATTRS(7),
1299 VIN_UNIT_ATTRS(8),
Mark M. Hoffmanc1685f62006-09-24 20:59:49 +02001300
Jim Cromie07584c72007-10-12 21:08:00 +02001301 FAN_UNIT_ATTRS(1),
1302 FAN_UNIT_ATTRS(2),
Mark M. Hoffmanc1685f62006-09-24 20:59:49 +02001303
Jim Cromie07584c72007-10-12 21:08:00 +02001304 TEMP_UNIT_ATTRS(1),
1305 TEMP_UNIT_ATTRS(2),
Mark M. Hoffmanc1685f62006-09-24 20:59:49 +02001306
1307 &dev_attr_alarms.attr,
Jean Delvare1c138102008-01-03 23:04:55 +01001308 &sensor_dev_attr_beep_enable.dev_attr.attr,
Mark M. Hoffmanc1685f62006-09-24 20:59:49 +02001309 &dev_attr_beep_mask.attr,
1310
Jim Cromie07584c72007-10-12 21:08:00 +02001311 &sensor_dev_attr_pwm1.dev_attr.attr,
1312 &sensor_dev_attr_pwm2.dev_attr.attr,
Jean Delvare787c72b2007-05-08 17:22:00 +02001313 &dev_attr_name.attr,
Mark M. Hoffmanc1685f62006-09-24 20:59:49 +02001314 NULL
1315};
1316
1317static const struct attribute_group w83627hf_group = {
1318 .attrs = w83627hf_attributes,
1319};
1320
1321static struct attribute *w83627hf_attributes_opt[] = {
Jim Cromie07584c72007-10-12 21:08:00 +02001322 VIN_UNIT_ATTRS(1),
1323 VIN_UNIT_ATTRS(5),
1324 VIN_UNIT_ATTRS(6),
Mark M. Hoffmanc1685f62006-09-24 20:59:49 +02001325
Jim Cromie07584c72007-10-12 21:08:00 +02001326 FAN_UNIT_ATTRS(3),
1327 TEMP_UNIT_ATTRS(3),
1328 &sensor_dev_attr_pwm3.dev_attr.attr,
Mark M. Hoffmanc1685f62006-09-24 20:59:49 +02001329
Jim Cromie07584c72007-10-12 21:08:00 +02001330 &sensor_dev_attr_pwm1_freq.dev_attr.attr,
1331 &sensor_dev_attr_pwm2_freq.dev_attr.attr,
1332 &sensor_dev_attr_pwm3_freq.dev_attr.attr,
Dominik Geyera95a5ed2008-08-06 22:41:04 +02001333
1334 &sensor_dev_attr_pwm1_enable.dev_attr.attr,
1335 &sensor_dev_attr_pwm2_enable.dev_attr.attr,
1336 &sensor_dev_attr_pwm3_enable.dev_attr.attr,
1337
Mark M. Hoffmanc1685f62006-09-24 20:59:49 +02001338 NULL
1339};
1340
1341static const struct attribute_group w83627hf_group_opt = {
1342 .attrs = w83627hf_attributes_opt,
1343};
1344
Jean Delvare787c72b2007-05-08 17:22:00 +02001345static int __devinit w83627hf_probe(struct platform_device *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001346{
Jean Delvare787c72b2007-05-08 17:22:00 +02001347 struct device *dev = &pdev->dev;
1348 struct w83627hf_sio_data *sio_data = dev->platform_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001349 struct w83627hf_data *data;
Jean Delvare787c72b2007-05-08 17:22:00 +02001350 struct resource *res;
Jim Cromie2ca2fcd2007-10-14 17:20:50 -06001351 int err, i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001352
Jean Delvare787c72b2007-05-08 17:22:00 +02001353 static const char *names[] = {
1354 "w83627hf",
1355 "w83627thf",
1356 "w83697hf",
1357 "w83637hf",
1358 "w83687thf",
1359 };
1360
1361 res = platform_get_resource(pdev, IORESOURCE_IO, 0);
1362 if (!request_region(res->start, WINB_REGION_SIZE, DRVNAME)) {
1363 dev_err(dev, "Failed to request region 0x%lx-0x%lx\n",
1364 (unsigned long)res->start,
1365 (unsigned long)(res->start + WINB_REGION_SIZE - 1));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001366 err = -EBUSY;
1367 goto ERROR0;
1368 }
1369
Guenter Roeck27b9de32012-01-15 11:07:26 -08001370 data = kzalloc(sizeof(struct w83627hf_data), GFP_KERNEL);
1371 if (!data) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001372 err = -ENOMEM;
1373 goto ERROR1;
1374 }
Jean Delvare787c72b2007-05-08 17:22:00 +02001375 data->addr = res->start;
1376 data->type = sio_data->type;
1377 data->name = names[sio_data->type];
Ingo Molnar9a61bf62006-01-18 23:19:26 +01001378 mutex_init(&data->lock);
Ingo Molnar9a61bf62006-01-18 23:19:26 +01001379 mutex_init(&data->update_lock);
Jean Delvare787c72b2007-05-08 17:22:00 +02001380 platform_set_drvdata(pdev, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001381
Linus Torvalds1da177e2005-04-16 15:20:36 -07001382 /* Initialize the chip */
Jean Delvare787c72b2007-05-08 17:22:00 +02001383 w83627hf_init_device(pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001384
1385 /* A few vars need to be filled upon startup */
Jim Cromie2ca2fcd2007-10-14 17:20:50 -06001386 for (i = 0; i <= 2; i++)
1387 data->fan_min[i] = w83627hf_read_value(
1388 data, W83627HF_REG_FAN_MIN(i));
Jean Delvarec09c5182007-10-12 21:53:07 +02001389 w83627hf_update_fan_div(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001390
Mark M. Hoffmanc1685f62006-09-24 20:59:49 +02001391 /* Register common device attributes */
Guenter Roeck27b9de32012-01-15 11:07:26 -08001392 err = sysfs_create_group(&dev->kobj, &w83627hf_group);
1393 if (err)
Mark M. Hoffmanc1685f62006-09-24 20:59:49 +02001394 goto ERROR3;
1395
1396 /* Register chip-specific device attributes */
Jean Delvare787c72b2007-05-08 17:22:00 +02001397 if (data->type == w83627hf || data->type == w83697hf)
Jim Cromie07584c72007-10-12 21:08:00 +02001398 if ((err = device_create_file(dev,
1399 &sensor_dev_attr_in5_input.dev_attr))
1400 || (err = device_create_file(dev,
1401 &sensor_dev_attr_in5_min.dev_attr))
1402 || (err = device_create_file(dev,
1403 &sensor_dev_attr_in5_max.dev_attr))
1404 || (err = device_create_file(dev,
Jean Delvaree3604c62008-01-03 23:00:30 +01001405 &sensor_dev_attr_in5_alarm.dev_attr))
1406 || (err = device_create_file(dev,
1407 &sensor_dev_attr_in5_beep.dev_attr))
1408 || (err = device_create_file(dev,
Jim Cromie07584c72007-10-12 21:08:00 +02001409 &sensor_dev_attr_in6_input.dev_attr))
1410 || (err = device_create_file(dev,
1411 &sensor_dev_attr_in6_min.dev_attr))
1412 || (err = device_create_file(dev,
1413 &sensor_dev_attr_in6_max.dev_attr))
1414 || (err = device_create_file(dev,
Jean Delvaree3604c62008-01-03 23:00:30 +01001415 &sensor_dev_attr_in6_alarm.dev_attr))
1416 || (err = device_create_file(dev,
1417 &sensor_dev_attr_in6_beep.dev_attr))
1418 || (err = device_create_file(dev,
Jim Cromie07584c72007-10-12 21:08:00 +02001419 &sensor_dev_attr_pwm1_freq.dev_attr))
1420 || (err = device_create_file(dev,
1421 &sensor_dev_attr_pwm2_freq.dev_attr)))
Mark M. Hoffmanc1685f62006-09-24 20:59:49 +02001422 goto ERROR4;
1423
Jean Delvare787c72b2007-05-08 17:22:00 +02001424 if (data->type != w83697hf)
Jim Cromie07584c72007-10-12 21:08:00 +02001425 if ((err = device_create_file(dev,
1426 &sensor_dev_attr_in1_input.dev_attr))
1427 || (err = device_create_file(dev,
1428 &sensor_dev_attr_in1_min.dev_attr))
1429 || (err = device_create_file(dev,
1430 &sensor_dev_attr_in1_max.dev_attr))
1431 || (err = device_create_file(dev,
Jean Delvaree3604c62008-01-03 23:00:30 +01001432 &sensor_dev_attr_in1_alarm.dev_attr))
1433 || (err = device_create_file(dev,
1434 &sensor_dev_attr_in1_beep.dev_attr))
1435 || (err = device_create_file(dev,
Jim Cromie07584c72007-10-12 21:08:00 +02001436 &sensor_dev_attr_fan3_input.dev_attr))
1437 || (err = device_create_file(dev,
1438 &sensor_dev_attr_fan3_min.dev_attr))
1439 || (err = device_create_file(dev,
1440 &sensor_dev_attr_fan3_div.dev_attr))
1441 || (err = device_create_file(dev,
Jean Delvaree3604c62008-01-03 23:00:30 +01001442 &sensor_dev_attr_fan3_alarm.dev_attr))
1443 || (err = device_create_file(dev,
1444 &sensor_dev_attr_fan3_beep.dev_attr))
1445 || (err = device_create_file(dev,
Jim Cromie07584c72007-10-12 21:08:00 +02001446 &sensor_dev_attr_temp3_input.dev_attr))
1447 || (err = device_create_file(dev,
1448 &sensor_dev_attr_temp3_max.dev_attr))
1449 || (err = device_create_file(dev,
1450 &sensor_dev_attr_temp3_max_hyst.dev_attr))
1451 || (err = device_create_file(dev,
Jean Delvaree3604c62008-01-03 23:00:30 +01001452 &sensor_dev_attr_temp3_alarm.dev_attr))
1453 || (err = device_create_file(dev,
1454 &sensor_dev_attr_temp3_beep.dev_attr))
1455 || (err = device_create_file(dev,
Jim Cromie07584c72007-10-12 21:08:00 +02001456 &sensor_dev_attr_temp3_type.dev_attr)))
Mark M. Hoffmanc1685f62006-09-24 20:59:49 +02001457 goto ERROR4;
1458
Jean Delvare787c72b2007-05-08 17:22:00 +02001459 if (data->type != w83697hf && data->vid != 0xff) {
Jean Delvare8a665a02007-05-08 17:21:59 +02001460 /* Convert VID to voltage based on VRM */
1461 data->vrm = vid_which_vrm();
1462
Jean Delvare787c72b2007-05-08 17:22:00 +02001463 if ((err = device_create_file(dev, &dev_attr_cpu0_vid))
1464 || (err = device_create_file(dev, &dev_attr_vrm)))
Mark M. Hoffmanc1685f62006-09-24 20:59:49 +02001465 goto ERROR4;
Jean Delvare8a665a02007-05-08 17:21:59 +02001466 }
Mark M. Hoffmanc1685f62006-09-24 20:59:49 +02001467
Jean Delvare787c72b2007-05-08 17:22:00 +02001468 if (data->type == w83627thf || data->type == w83637hf
Guenter Roeck27b9de32012-01-15 11:07:26 -08001469 || data->type == w83687thf) {
1470 err = device_create_file(dev, &sensor_dev_attr_pwm3.dev_attr);
1471 if (err)
Mark M. Hoffmanc1685f62006-09-24 20:59:49 +02001472 goto ERROR4;
Guenter Roeck27b9de32012-01-15 11:07:26 -08001473 }
Mark M. Hoffmanc1685f62006-09-24 20:59:49 +02001474
Carlos Olalla Martinez1550cb62007-06-09 10:11:16 -04001475 if (data->type == w83637hf || data->type == w83687thf)
Jim Cromie07584c72007-10-12 21:08:00 +02001476 if ((err = device_create_file(dev,
1477 &sensor_dev_attr_pwm1_freq.dev_attr))
1478 || (err = device_create_file(dev,
1479 &sensor_dev_attr_pwm2_freq.dev_attr))
1480 || (err = device_create_file(dev,
1481 &sensor_dev_attr_pwm3_freq.dev_attr)))
Carlos Olalla Martinez1550cb62007-06-09 10:11:16 -04001482 goto ERROR4;
1483
Dominik Geyera95a5ed2008-08-06 22:41:04 +02001484 if (data->type != w83627hf)
1485 if ((err = device_create_file(dev,
1486 &sensor_dev_attr_pwm1_enable.dev_attr))
1487 || (err = device_create_file(dev,
1488 &sensor_dev_attr_pwm2_enable.dev_attr)))
1489 goto ERROR4;
1490
1491 if (data->type == w83627thf || data->type == w83637hf
Guenter Roeck27b9de32012-01-15 11:07:26 -08001492 || data->type == w83687thf) {
1493 err = device_create_file(dev,
1494 &sensor_dev_attr_pwm3_enable.dev_attr);
1495 if (err)
Dominik Geyera95a5ed2008-08-06 22:41:04 +02001496 goto ERROR4;
Guenter Roeck27b9de32012-01-15 11:07:26 -08001497 }
Dominik Geyera95a5ed2008-08-06 22:41:04 +02001498
Tony Jones1beeffe2007-08-20 13:46:20 -07001499 data->hwmon_dev = hwmon_device_register(dev);
1500 if (IS_ERR(data->hwmon_dev)) {
1501 err = PTR_ERR(data->hwmon_dev);
Mark M. Hoffmanc1685f62006-09-24 20:59:49 +02001502 goto ERROR4;
Mark M. Hoffman943b0832005-07-15 21:39:18 -04001503 }
1504
Linus Torvalds1da177e2005-04-16 15:20:36 -07001505 return 0;
1506
Mark M. Hoffmanc1685f62006-09-24 20:59:49 +02001507 ERROR4:
Jean Delvare787c72b2007-05-08 17:22:00 +02001508 sysfs_remove_group(&dev->kobj, &w83627hf_group);
1509 sysfs_remove_group(&dev->kobj, &w83627hf_group_opt);
Mark M. Hoffman943b0832005-07-15 21:39:18 -04001510 ERROR3:
Jean Delvare04a62172007-06-12 13:57:19 +02001511 platform_set_drvdata(pdev, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001512 kfree(data);
1513 ERROR1:
Jean Delvare787c72b2007-05-08 17:22:00 +02001514 release_region(res->start, WINB_REGION_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001515 ERROR0:
1516 return err;
1517}
1518
Jean Delvare787c72b2007-05-08 17:22:00 +02001519static int __devexit w83627hf_remove(struct platform_device *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001520{
Jean Delvare787c72b2007-05-08 17:22:00 +02001521 struct w83627hf_data *data = platform_get_drvdata(pdev);
1522 struct resource *res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001523
Tony Jones1beeffe2007-08-20 13:46:20 -07001524 hwmon_device_unregister(data->hwmon_dev);
Mark M. Hoffman943b0832005-07-15 21:39:18 -04001525
Jean Delvare787c72b2007-05-08 17:22:00 +02001526 sysfs_remove_group(&pdev->dev.kobj, &w83627hf_group);
1527 sysfs_remove_group(&pdev->dev.kobj, &w83627hf_group_opt);
Jean Delvare04a62172007-06-12 13:57:19 +02001528 platform_set_drvdata(pdev, NULL);
Mark M. Hoffman943b0832005-07-15 21:39:18 -04001529 kfree(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001530
Jean Delvare787c72b2007-05-08 17:22:00 +02001531 res = platform_get_resource(pdev, IORESOURCE_IO, 0);
1532 release_region(res->start, WINB_REGION_SIZE);
1533
Linus Torvalds1da177e2005-04-16 15:20:36 -07001534 return 0;
1535}
1536
1537
Jean Delvared58df9c2007-10-10 16:30:23 +02001538/* Registers 0x50-0x5f are banked */
1539static inline void w83627hf_set_bank(struct w83627hf_data *data, u16 reg)
1540{
1541 if ((reg & 0x00f0) == 0x50) {
1542 outb_p(W83781D_REG_BANK, data->addr + W83781D_ADDR_REG_OFFSET);
1543 outb_p(reg >> 8, data->addr + W83781D_DATA_REG_OFFSET);
1544 }
1545}
1546
1547/* Not strictly necessary, but play it safe for now */
1548static inline void w83627hf_reset_bank(struct w83627hf_data *data, u16 reg)
1549{
1550 if (reg & 0xff00) {
1551 outb_p(W83781D_REG_BANK, data->addr + W83781D_ADDR_REG_OFFSET);
1552 outb_p(0, data->addr + W83781D_DATA_REG_OFFSET);
1553 }
1554}
1555
Jean Delvare787c72b2007-05-08 17:22:00 +02001556static int w83627hf_read_value(struct w83627hf_data *data, u16 reg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001557{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001558 int res, word_sized;
1559
Ingo Molnar9a61bf62006-01-18 23:19:26 +01001560 mutex_lock(&data->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001561 word_sized = (((reg & 0xff00) == 0x100)
1562 || ((reg & 0xff00) == 0x200))
1563 && (((reg & 0x00ff) == 0x50)
1564 || ((reg & 0x00ff) == 0x53)
1565 || ((reg & 0x00ff) == 0x55));
Jean Delvared58df9c2007-10-10 16:30:23 +02001566 w83627hf_set_bank(data, reg);
Jean Delvare787c72b2007-05-08 17:22:00 +02001567 outb_p(reg & 0xff, data->addr + W83781D_ADDR_REG_OFFSET);
1568 res = inb_p(data->addr + W83781D_DATA_REG_OFFSET);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001569 if (word_sized) {
1570 outb_p((reg & 0xff) + 1,
Jean Delvare787c72b2007-05-08 17:22:00 +02001571 data->addr + W83781D_ADDR_REG_OFFSET);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001572 res =
Jean Delvare787c72b2007-05-08 17:22:00 +02001573 (res << 8) + inb_p(data->addr +
Linus Torvalds1da177e2005-04-16 15:20:36 -07001574 W83781D_DATA_REG_OFFSET);
1575 }
Jean Delvared58df9c2007-10-10 16:30:23 +02001576 w83627hf_reset_bank(data, reg);
Ingo Molnar9a61bf62006-01-18 23:19:26 +01001577 mutex_unlock(&data->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001578 return res;
1579}
1580
Jean Delvare787c72b2007-05-08 17:22:00 +02001581static int __devinit w83627thf_read_gpio5(struct platform_device *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001582{
Jean Delvareb72656d2009-12-09 20:35:49 +01001583 struct w83627hf_sio_data *sio_data = pdev->dev.platform_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001584 int res = 0xff, sel;
1585
Jean Delvareb72656d2009-12-09 20:35:49 +01001586 superio_enter(sio_data);
1587 superio_select(sio_data, W83627HF_LD_GPIO5);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001588
1589 /* Make sure these GPIO pins are enabled */
Jean Delvareb72656d2009-12-09 20:35:49 +01001590 if (!(superio_inb(sio_data, W83627THF_GPIO5_EN) & (1<<3))) {
Jean Delvare787c72b2007-05-08 17:22:00 +02001591 dev_dbg(&pdev->dev, "GPIO5 disabled, no VID function\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001592 goto exit;
1593 }
1594
Guenter Roeck27b9de32012-01-15 11:07:26 -08001595 /*
1596 * Make sure the pins are configured for input
1597 * There must be at least five (VRM 9), and possibly 6 (VRM 10)
1598 */
Jean Delvareb72656d2009-12-09 20:35:49 +01001599 sel = superio_inb(sio_data, W83627THF_GPIO5_IOSR) & 0x3f;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001600 if ((sel & 0x1f) != 0x1f) {
Jean Delvare787c72b2007-05-08 17:22:00 +02001601 dev_dbg(&pdev->dev, "GPIO5 not configured for VID "
Linus Torvalds1da177e2005-04-16 15:20:36 -07001602 "function\n");
1603 goto exit;
1604 }
1605
Jean Delvare787c72b2007-05-08 17:22:00 +02001606 dev_info(&pdev->dev, "Reading VID from GPIO5\n");
Jean Delvareb72656d2009-12-09 20:35:49 +01001607 res = superio_inb(sio_data, W83627THF_GPIO5_DR) & sel;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001608
1609exit:
Jean Delvareb72656d2009-12-09 20:35:49 +01001610 superio_exit(sio_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001611 return res;
1612}
1613
Jean Delvare787c72b2007-05-08 17:22:00 +02001614static int __devinit w83687thf_read_vid(struct platform_device *pdev)
Jean Delvarec2db6ce2006-01-18 23:22:12 +01001615{
Jean Delvareb72656d2009-12-09 20:35:49 +01001616 struct w83627hf_sio_data *sio_data = pdev->dev.platform_data;
Jean Delvarec2db6ce2006-01-18 23:22:12 +01001617 int res = 0xff;
1618
Jean Delvareb72656d2009-12-09 20:35:49 +01001619 superio_enter(sio_data);
1620 superio_select(sio_data, W83627HF_LD_HWM);
Jean Delvarec2db6ce2006-01-18 23:22:12 +01001621
1622 /* Make sure these GPIO pins are enabled */
Jean Delvareb72656d2009-12-09 20:35:49 +01001623 if (!(superio_inb(sio_data, W83687THF_VID_EN) & (1 << 2))) {
Jean Delvare787c72b2007-05-08 17:22:00 +02001624 dev_dbg(&pdev->dev, "VID disabled, no VID function\n");
Jean Delvarec2db6ce2006-01-18 23:22:12 +01001625 goto exit;
1626 }
1627
1628 /* Make sure the pins are configured for input */
Jean Delvareb72656d2009-12-09 20:35:49 +01001629 if (!(superio_inb(sio_data, W83687THF_VID_CFG) & (1 << 4))) {
Jean Delvare787c72b2007-05-08 17:22:00 +02001630 dev_dbg(&pdev->dev, "VID configured as output, "
Jean Delvarec2db6ce2006-01-18 23:22:12 +01001631 "no VID function\n");
1632 goto exit;
1633 }
1634
Jean Delvareb72656d2009-12-09 20:35:49 +01001635 res = superio_inb(sio_data, W83687THF_VID_DATA) & 0x3f;
Jean Delvarec2db6ce2006-01-18 23:22:12 +01001636
1637exit:
Jean Delvareb72656d2009-12-09 20:35:49 +01001638 superio_exit(sio_data);
Jean Delvarec2db6ce2006-01-18 23:22:12 +01001639 return res;
1640}
1641
Jean Delvare787c72b2007-05-08 17:22:00 +02001642static int w83627hf_write_value(struct w83627hf_data *data, u16 reg, u16 value)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001643{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001644 int word_sized;
1645
Ingo Molnar9a61bf62006-01-18 23:19:26 +01001646 mutex_lock(&data->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001647 word_sized = (((reg & 0xff00) == 0x100)
1648 || ((reg & 0xff00) == 0x200))
1649 && (((reg & 0x00ff) == 0x53)
1650 || ((reg & 0x00ff) == 0x55));
Jean Delvared58df9c2007-10-10 16:30:23 +02001651 w83627hf_set_bank(data, reg);
Jean Delvare787c72b2007-05-08 17:22:00 +02001652 outb_p(reg & 0xff, data->addr + W83781D_ADDR_REG_OFFSET);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001653 if (word_sized) {
1654 outb_p(value >> 8,
Jean Delvare787c72b2007-05-08 17:22:00 +02001655 data->addr + W83781D_DATA_REG_OFFSET);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001656 outb_p((reg & 0xff) + 1,
Jean Delvare787c72b2007-05-08 17:22:00 +02001657 data->addr + W83781D_ADDR_REG_OFFSET);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001658 }
1659 outb_p(value & 0xff,
Jean Delvare787c72b2007-05-08 17:22:00 +02001660 data->addr + W83781D_DATA_REG_OFFSET);
Jean Delvared58df9c2007-10-10 16:30:23 +02001661 w83627hf_reset_bank(data, reg);
Ingo Molnar9a61bf62006-01-18 23:19:26 +01001662 mutex_unlock(&data->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001663 return 0;
1664}
1665
Jean Delvare787c72b2007-05-08 17:22:00 +02001666static void __devinit w83627hf_init_device(struct platform_device *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001667{
Jean Delvare787c72b2007-05-08 17:22:00 +02001668 struct w83627hf_data *data = platform_get_drvdata(pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001669 int i;
Jean Delvared27c37c2007-05-08 17:21:59 +02001670 enum chips type = data->type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001671 u8 tmp;
1672
Linus Torvalds1da177e2005-04-16 15:20:36 -07001673 /* Minimize conflicts with other winbond i2c-only clients... */
1674 /* disable i2c subclients... how to disable main i2c client?? */
1675 /* force i2c address to relatively uncommon address */
Jean Delvare787c72b2007-05-08 17:22:00 +02001676 w83627hf_write_value(data, W83781D_REG_I2C_SUBADDR, 0x89);
1677 w83627hf_write_value(data, W83781D_REG_I2C_ADDR, force_i2c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001678
1679 /* Read VID only once */
Jean Delvared27c37c2007-05-08 17:21:59 +02001680 if (type == w83627hf || type == w83637hf) {
Jean Delvare787c72b2007-05-08 17:22:00 +02001681 int lo = w83627hf_read_value(data, W83781D_REG_VID_FANDIV);
1682 int hi = w83627hf_read_value(data, W83781D_REG_CHIPID);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001683 data->vid = (lo & 0x0f) | ((hi & 0x01) << 4);
Jean Delvared27c37c2007-05-08 17:21:59 +02001684 } else if (type == w83627thf) {
Jean Delvare787c72b2007-05-08 17:22:00 +02001685 data->vid = w83627thf_read_gpio5(pdev);
Jean Delvared27c37c2007-05-08 17:21:59 +02001686 } else if (type == w83687thf) {
Jean Delvare787c72b2007-05-08 17:22:00 +02001687 data->vid = w83687thf_read_vid(pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001688 }
1689
1690 /* Read VRM & OVT Config only once */
Jean Delvared27c37c2007-05-08 17:21:59 +02001691 if (type == w83627thf || type == w83637hf || type == w83687thf) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001692 data->vrm_ovt =
Jean Delvare787c72b2007-05-08 17:22:00 +02001693 w83627hf_read_value(data, W83627THF_REG_VRM_OVT_CFG);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001694 }
1695
Jean Delvare787c72b2007-05-08 17:22:00 +02001696 tmp = w83627hf_read_value(data, W83781D_REG_SCFG1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001697 for (i = 1; i <= 3; i++) {
1698 if (!(tmp & BIT_SCFG1[i - 1])) {
Jean Delvareb26f9332007-08-16 14:30:01 +02001699 data->sens[i - 1] = 4;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001700 } else {
1701 if (w83627hf_read_value
Jean Delvare787c72b2007-05-08 17:22:00 +02001702 (data,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001703 W83781D_REG_SCFG2) & BIT_SCFG2[i - 1])
1704 data->sens[i - 1] = 1;
1705 else
1706 data->sens[i - 1] = 2;
1707 }
1708 if ((type == w83697hf) && (i == 2))
1709 break;
1710 }
1711
1712 if(init) {
1713 /* Enable temp2 */
Jim Cromiedf48ed82007-10-14 17:10:52 -06001714 tmp = w83627hf_read_value(data, W83627HF_REG_TEMP2_CONFIG);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001715 if (tmp & 0x01) {
Jean Delvare787c72b2007-05-08 17:22:00 +02001716 dev_warn(&pdev->dev, "Enabling temp2, readings "
Linus Torvalds1da177e2005-04-16 15:20:36 -07001717 "might not make sense\n");
Jim Cromiedf48ed82007-10-14 17:10:52 -06001718 w83627hf_write_value(data, W83627HF_REG_TEMP2_CONFIG,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001719 tmp & 0xfe);
1720 }
1721
1722 /* Enable temp3 */
1723 if (type != w83697hf) {
Jean Delvare787c72b2007-05-08 17:22:00 +02001724 tmp = w83627hf_read_value(data,
Jim Cromiedf48ed82007-10-14 17:10:52 -06001725 W83627HF_REG_TEMP3_CONFIG);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001726 if (tmp & 0x01) {
Jean Delvare787c72b2007-05-08 17:22:00 +02001727 dev_warn(&pdev->dev, "Enabling temp3, "
Linus Torvalds1da177e2005-04-16 15:20:36 -07001728 "readings might not make sense\n");
Jean Delvare787c72b2007-05-08 17:22:00 +02001729 w83627hf_write_value(data,
Jim Cromiedf48ed82007-10-14 17:10:52 -06001730 W83627HF_REG_TEMP3_CONFIG, tmp & 0xfe);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001731 }
1732 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001733 }
1734
1735 /* Start monitoring */
Jean Delvare787c72b2007-05-08 17:22:00 +02001736 w83627hf_write_value(data, W83781D_REG_CONFIG,
1737 (w83627hf_read_value(data,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001738 W83781D_REG_CONFIG) & 0xf7)
1739 | 0x01);
Jean Delvareef878b12008-01-03 22:54:13 +01001740
1741 /* Enable VBAT monitoring if needed */
1742 tmp = w83627hf_read_value(data, W83781D_REG_VBAT);
1743 if (!(tmp & 0x01))
1744 w83627hf_write_value(data, W83781D_REG_VBAT, tmp | 0x01);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001745}
1746
Jean Delvarec09c5182007-10-12 21:53:07 +02001747static void w83627hf_update_fan_div(struct w83627hf_data *data)
1748{
1749 int reg;
1750
1751 reg = w83627hf_read_value(data, W83781D_REG_VID_FANDIV);
1752 data->fan_div[0] = (reg >> 4) & 0x03;
1753 data->fan_div[1] = (reg >> 6) & 0x03;
1754 if (data->type != w83697hf) {
1755 data->fan_div[2] = (w83627hf_read_value(data,
1756 W83781D_REG_PIN) >> 6) & 0x03;
1757 }
1758 reg = w83627hf_read_value(data, W83781D_REG_VBAT);
1759 data->fan_div[0] |= (reg >> 3) & 0x04;
1760 data->fan_div[1] |= (reg >> 4) & 0x04;
1761 if (data->type != w83697hf)
1762 data->fan_div[2] |= (reg >> 5) & 0x04;
1763}
1764
Linus Torvalds1da177e2005-04-16 15:20:36 -07001765static struct w83627hf_data *w83627hf_update_device(struct device *dev)
1766{
Jean Delvare787c72b2007-05-08 17:22:00 +02001767 struct w83627hf_data *data = dev_get_drvdata(dev);
Jim Cromiedf48ed82007-10-14 17:10:52 -06001768 int i, num_temps = (data->type == w83697hf) ? 2 : 3;
Dominik Geyera95a5ed2008-08-06 22:41:04 +02001769 int num_pwms = (data->type == w83697hf) ? 2 : 3;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001770
Ingo Molnar9a61bf62006-01-18 23:19:26 +01001771 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001772
1773 if (time_after(jiffies, data->last_updated + HZ + HZ / 2)
1774 || !data->valid) {
1775 for (i = 0; i <= 8; i++) {
1776 /* skip missing sensors */
1777 if (((data->type == w83697hf) && (i == 1)) ||
Jean Delvarec2db6ce2006-01-18 23:22:12 +01001778 ((data->type != w83627hf && data->type != w83697hf)
Yuan Mu4a1c44472005-11-07 22:19:04 +01001779 && (i == 5 || i == 6)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001780 continue;
1781 data->in[i] =
Jean Delvare787c72b2007-05-08 17:22:00 +02001782 w83627hf_read_value(data, W83781D_REG_IN(i));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001783 data->in_min[i] =
Jean Delvare787c72b2007-05-08 17:22:00 +02001784 w83627hf_read_value(data,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001785 W83781D_REG_IN_MIN(i));
1786 data->in_max[i] =
Jean Delvare787c72b2007-05-08 17:22:00 +02001787 w83627hf_read_value(data,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001788 W83781D_REG_IN_MAX(i));
1789 }
Jim Cromie2ca2fcd2007-10-14 17:20:50 -06001790 for (i = 0; i <= 2; i++) {
1791 data->fan[i] =
1792 w83627hf_read_value(data, W83627HF_REG_FAN(i));
1793 data->fan_min[i] =
Jean Delvare787c72b2007-05-08 17:22:00 +02001794 w83627hf_read_value(data,
Jim Cromie2ca2fcd2007-10-14 17:20:50 -06001795 W83627HF_REG_FAN_MIN(i));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001796 }
Jim Cromie07584c72007-10-12 21:08:00 +02001797 for (i = 0; i <= 2; i++) {
Jean Delvare787c72b2007-05-08 17:22:00 +02001798 u8 tmp = w83627hf_read_value(data,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001799 W836X7HF_REG_PWM(data->type, i));
1800 /* bits 0-3 are reserved in 627THF */
1801 if (data->type == w83627thf)
1802 tmp &= 0xf0;
Jim Cromie07584c72007-10-12 21:08:00 +02001803 data->pwm[i] = tmp;
1804 if (i == 1 &&
1805 (data->type == w83627hf || data->type == w83697hf))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001806 break;
1807 }
Carlos Olalla Martinez1550cb62007-06-09 10:11:16 -04001808 if (data->type == w83627hf) {
1809 u8 tmp = w83627hf_read_value(data,
1810 W83627HF_REG_PWM_FREQ);
1811 data->pwm_freq[0] = tmp & 0x07;
1812 data->pwm_freq[1] = (tmp >> 4) & 0x07;
1813 } else if (data->type != w83627thf) {
1814 for (i = 1; i <= 3; i++) {
1815 data->pwm_freq[i - 1] =
1816 w83627hf_read_value(data,
1817 W83637HF_REG_PWM_FREQ[i - 1]);
1818 if (i == 2 && (data->type == w83697hf))
1819 break;
1820 }
1821 }
Dominik Geyera95a5ed2008-08-06 22:41:04 +02001822 if (data->type != w83627hf) {
1823 for (i = 0; i < num_pwms; i++) {
1824 u8 tmp = w83627hf_read_value(data,
1825 W83627THF_REG_PWM_ENABLE[i]);
1826 data->pwm_enable[i] =
1827 ((tmp >> W83627THF_PWM_ENABLE_SHIFT[i])
1828 & 0x03) + 1;
1829 }
1830 }
Jim Cromiedf48ed82007-10-14 17:10:52 -06001831 for (i = 0; i < num_temps; i++) {
1832 data->temp[i] = w83627hf_read_value(
1833 data, w83627hf_reg_temp[i]);
1834 data->temp_max[i] = w83627hf_read_value(
1835 data, w83627hf_reg_temp_over[i]);
1836 data->temp_max_hyst[i] = w83627hf_read_value(
1837 data, w83627hf_reg_temp_hyst[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001838 }
1839
Jean Delvarec09c5182007-10-12 21:53:07 +02001840 w83627hf_update_fan_div(data);
1841
Linus Torvalds1da177e2005-04-16 15:20:36 -07001842 data->alarms =
Jean Delvare787c72b2007-05-08 17:22:00 +02001843 w83627hf_read_value(data, W83781D_REG_ALARM1) |
1844 (w83627hf_read_value(data, W83781D_REG_ALARM2) << 8) |
1845 (w83627hf_read_value(data, W83781D_REG_ALARM3) << 16);
1846 i = w83627hf_read_value(data, W83781D_REG_BEEP_INTS2);
Jean Delvare1c138102008-01-03 23:04:55 +01001847 data->beep_mask = (i << 8) |
Jean Delvare787c72b2007-05-08 17:22:00 +02001848 w83627hf_read_value(data, W83781D_REG_BEEP_INTS1) |
1849 w83627hf_read_value(data, W83781D_REG_BEEP_INTS3) << 16;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001850 data->last_updated = jiffies;
1851 data->valid = 1;
1852 }
1853
Ingo Molnar9a61bf62006-01-18 23:19:26 +01001854 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001855
1856 return data;
1857}
1858
Jean Delvare787c72b2007-05-08 17:22:00 +02001859static int __init w83627hf_device_add(unsigned short address,
1860 const struct w83627hf_sio_data *sio_data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001861{
Jean Delvare787c72b2007-05-08 17:22:00 +02001862 struct resource res = {
1863 .start = address + WINB_REGION_OFFSET,
1864 .end = address + WINB_REGION_OFFSET + WINB_REGION_SIZE - 1,
1865 .name = DRVNAME,
1866 .flags = IORESOURCE_IO,
1867 };
1868 int err;
1869
Jean Delvareb9acb642009-01-07 16:37:35 +01001870 err = acpi_check_resource_conflict(&res);
1871 if (err)
1872 goto exit;
1873
Jean Delvare787c72b2007-05-08 17:22:00 +02001874 pdev = platform_device_alloc(DRVNAME, address);
1875 if (!pdev) {
1876 err = -ENOMEM;
Joe Perches18de0302010-10-20 06:51:55 +00001877 pr_err("Device allocation failed\n");
Jean Delvare787c72b2007-05-08 17:22:00 +02001878 goto exit;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001879 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001880
Jean Delvare787c72b2007-05-08 17:22:00 +02001881 err = platform_device_add_resources(pdev, &res, 1);
1882 if (err) {
Joe Perches18de0302010-10-20 06:51:55 +00001883 pr_err("Device resource addition failed (%d)\n", err);
Jean Delvare787c72b2007-05-08 17:22:00 +02001884 goto exit_device_put;
1885 }
1886
Jean Delvare2df6d812007-06-09 10:11:16 -04001887 err = platform_device_add_data(pdev, sio_data,
1888 sizeof(struct w83627hf_sio_data));
1889 if (err) {
Joe Perches18de0302010-10-20 06:51:55 +00001890 pr_err("Platform data allocation failed\n");
Jean Delvare787c72b2007-05-08 17:22:00 +02001891 goto exit_device_put;
1892 }
Jean Delvare787c72b2007-05-08 17:22:00 +02001893
1894 err = platform_device_add(pdev);
1895 if (err) {
Joe Perches18de0302010-10-20 06:51:55 +00001896 pr_err("Device addition failed (%d)\n", err);
Jean Delvare787c72b2007-05-08 17:22:00 +02001897 goto exit_device_put;
1898 }
1899
1900 return 0;
1901
1902exit_device_put:
1903 platform_device_put(pdev);
1904exit:
1905 return err;
1906}
1907
1908static int __init sensors_w83627hf_init(void)
1909{
1910 int err;
1911 unsigned short address;
1912 struct w83627hf_sio_data sio_data;
1913
1914 if (w83627hf_find(0x2e, &address, &sio_data)
1915 && w83627hf_find(0x4e, &address, &sio_data))
1916 return -ENODEV;
1917
1918 err = platform_driver_register(&w83627hf_driver);
1919 if (err)
1920 goto exit;
1921
1922 /* Sets global pdev as a side effect */
1923 err = w83627hf_device_add(address, &sio_data);
1924 if (err)
1925 goto exit_driver;
1926
1927 return 0;
1928
1929exit_driver:
1930 platform_driver_unregister(&w83627hf_driver);
1931exit:
1932 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001933}
1934
1935static void __exit sensors_w83627hf_exit(void)
1936{
Jean Delvare787c72b2007-05-08 17:22:00 +02001937 platform_device_unregister(pdev);
1938 platform_driver_unregister(&w83627hf_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001939}
1940
1941MODULE_AUTHOR("Frodo Looijaard <frodol@dds.nl>, "
1942 "Philip Edelbrock <phil@netroedge.com>, "
1943 "and Mark Studebaker <mdsxyz123@yahoo.com>");
1944MODULE_DESCRIPTION("W83627HF driver");
1945MODULE_LICENSE("GPL");
1946
1947module_init(sensors_w83627hf_init);
1948module_exit(sensors_w83627hf_exit);