blob: 2f55973a8c4c37989750905d1fd2b209b0bbd902 [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>
Jean Delvare7c81c602014-01-29 20:40:08 +01008 * Copyright (c) 2007 - 1012 Jean Delvare <jdelvare@suse.de>
Guenter Roeck27b9de32012-01-15 11:07:26 -08009 *
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 *
Jean Delvare4101ece2012-11-05 21:54:40 +010028 * Chip #vin #fanin #pwm #temp wchipid vendid i2c ISA
Guenter Roeck27b9de32012-01-15 11:07:26 -080029 * 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 */
Guenter Roeck2a844c12013-01-09 08:09:34 -0800257#define IN_TO_REG(val) (clamp_val((((val) + 8) / 16), 0, 255))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258#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;
Guenter Roeck2a844c12013-01-09 08:09:34 -0800264 rpm = clamp_val(rpm, 1, 1000000);
265 return clamp_val((1350000 + rpm * div / 2) / (rpm * div), 1, 254);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266}
267
268#define TEMP_MIN (-128000)
269#define TEMP_MAX ( 127000)
270
Guenter Roeck27b9de32012-01-15 11:07:26 -0800271/*
272 * TEMP: 0.001C/bit (-128C to +127C)
273 * REG: 1C/bit, two's complement
274 */
Christian Hohnstaedt5bfedac2007-08-16 11:40:10 +0200275static u8 TEMP_TO_REG(long temp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276{
Guenter Roeck2a844c12013-01-09 08:09:34 -0800277 int ntemp = clamp_val(temp, TEMP_MIN, TEMP_MAX);
278 ntemp += (ntemp < 0 ? -500 : 500);
279 return (u8)(ntemp / 1000);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280}
281
282static int TEMP_FROM_REG(u8 reg)
283{
284 return (s8)reg * 1000;
285}
286
287#define FAN_FROM_REG(val,div) ((val)==0?-1:(val)==255?0:1350000/((val)*(div)))
288
Guenter Roeck2a844c12013-01-09 08:09:34 -0800289#define PWM_TO_REG(val) (clamp_val((val), 0, 255))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290
Carlos Olalla Martinez1550cb62007-06-09 10:11:16 -0400291static inline unsigned long pwm_freq_from_reg_627hf(u8 reg)
292{
293 unsigned long freq;
294 freq = W83627HF_BASE_PWM_FREQ >> reg;
295 return freq;
296}
297static inline u8 pwm_freq_to_reg_627hf(unsigned long val)
298{
299 u8 i;
Guenter Roeck27b9de32012-01-15 11:07:26 -0800300 /*
301 * Only 5 dividers (1 2 4 8 16)
302 * Search for the nearest available frequency
303 */
Carlos Olalla Martinez1550cb62007-06-09 10:11:16 -0400304 for (i = 0; i < 4; i++) {
305 if (val > (((W83627HF_BASE_PWM_FREQ >> i) +
306 (W83627HF_BASE_PWM_FREQ >> (i+1))) / 2))
307 break;
308 }
309 return i;
310}
311
312static inline unsigned long pwm_freq_from_reg(u8 reg)
313{
314 /* Clock bit 8 -> 180 kHz or 24 MHz */
315 unsigned long clock = (reg & 0x80) ? 180000UL : 24000000UL;
316
317 reg &= 0x7f;
318 /* This should not happen but anyway... */
319 if (reg == 0)
320 reg++;
Frans Meulenbroeks7fe83ad2012-01-05 19:50:18 +0100321 return clock / (reg << 8);
Carlos Olalla Martinez1550cb62007-06-09 10:11:16 -0400322}
323static inline u8 pwm_freq_to_reg(unsigned long val)
324{
325 /* Minimum divider value is 0x01 and maximum is 0x7F */
326 if (val >= 93750) /* The highest we can do */
327 return 0x01;
328 if (val >= 720) /* Use 24 MHz clock */
Frans Meulenbroeks7fe83ad2012-01-05 19:50:18 +0100329 return 24000000UL / (val << 8);
Carlos Olalla Martinez1550cb62007-06-09 10:11:16 -0400330 if (val < 6) /* The lowest we can do */
331 return 0xFF;
332 else /* Use 180 kHz clock */
Frans Meulenbroeks7fe83ad2012-01-05 19:50:18 +0100333 return 0x80 | (180000UL / (val << 8));
Carlos Olalla Martinez1550cb62007-06-09 10:11:16 -0400334}
335
Jean Delvare1c138102008-01-03 23:04:55 +0100336#define BEEP_MASK_FROM_REG(val) ((val) & 0xff7fff)
337#define BEEP_MASK_TO_REG(val) ((val) & 0xff7fff)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338
339#define DIV_FROM_REG(val) (1 << (val))
340
341static inline u8 DIV_TO_REG(long val)
342{
343 int i;
Guenter Roeck2a844c12013-01-09 08:09:34 -0800344 val = clamp_val(val, 1, 128) >> 1;
Grant Coadyabc01922005-05-12 13:41:51 +1000345 for (i = 0; i < 7; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346 if (val == 0)
347 break;
348 val >>= 1;
349 }
Frans Meulenbroeks7fe83ad2012-01-05 19:50:18 +0100350 return (u8)i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351}
352
Guenter Roeck27b9de32012-01-15 11:07:26 -0800353/*
354 * For each registered chip, we need to keep some data in memory.
355 * The structure is dynamically allocated.
356 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357struct w83627hf_data {
Jean Delvare787c72b2007-05-08 17:22:00 +0200358 unsigned short addr;
359 const char *name;
Tony Jones1beeffe2007-08-20 13:46:20 -0700360 struct device *hwmon_dev;
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100361 struct mutex lock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362 enum chips type;
363
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100364 struct mutex update_lock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365 char valid; /* !=0 if following fields are valid */
366 unsigned long last_updated; /* In jiffies */
367
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368 u8 in[9]; /* Register value */
369 u8 in_max[9]; /* Register value */
370 u8 in_min[9]; /* Register value */
371 u8 fan[3]; /* Register value */
372 u8 fan_min[3]; /* Register value */
Jim Cromiedf48ed82007-10-14 17:10:52 -0600373 u16 temp[3]; /* Register value */
374 u16 temp_max[3]; /* Register value */
375 u16 temp_max_hyst[3]; /* Register value */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376 u8 fan_div[3]; /* Register encoding, shifted right */
377 u8 vid; /* Register encoding, combined */
378 u32 alarms; /* Register encoding, combined */
379 u32 beep_mask; /* Register encoding, combined */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380 u8 pwm[3]; /* Register value */
Dominik Geyera95a5ed2008-08-06 22:41:04 +0200381 u8 pwm_enable[3]; /* 1 = manual
Guenter Roeck27b9de32012-01-15 11:07:26 -0800382 * 2 = thermal cruise (also called SmartFan I)
383 * 3 = fan speed cruise
384 */
Carlos Olalla Martinez1550cb62007-06-09 10:11:16 -0400385 u8 pwm_freq[3]; /* Register value */
Jean Delvareb26f9332007-08-16 14:30:01 +0200386 u16 sens[3]; /* 1 = pentium diode; 2 = 3904 diode;
Guenter Roeck27b9de32012-01-15 11:07:26 -0800387 * 4 = thermistor
388 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389 u8 vrm;
Jean Delvarec2db6ce2006-01-18 23:22:12 +0100390 u8 vrm_ovt; /* Register value, 627THF/637HF/687THF only */
Jean Delvare275b7d62012-12-19 22:16:59 +0100391
392#ifdef CONFIG_PM
393 /* Remember extra register values over suspend/resume */
394 u8 scfg1;
395 u8 scfg2;
396#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397};
398
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399
Jean Delvare787c72b2007-05-08 17:22:00 +0200400static int w83627hf_probe(struct platform_device *pdev);
Bill Pemberton281dfd02012-11-19 13:25:51 -0500401static int w83627hf_remove(struct platform_device *pdev);
Jean Delvare787c72b2007-05-08 17:22:00 +0200402
403static int w83627hf_read_value(struct w83627hf_data *data, u16 reg);
404static int w83627hf_write_value(struct w83627hf_data *data, u16 reg, u16 value);
Jean Delvarec09c5182007-10-12 21:53:07 +0200405static void w83627hf_update_fan_div(struct w83627hf_data *data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406static struct w83627hf_data *w83627hf_update_device(struct device *dev);
Jean Delvare787c72b2007-05-08 17:22:00 +0200407static void w83627hf_init_device(struct platform_device *pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408
Jean Delvare275b7d62012-12-19 22:16:59 +0100409#ifdef CONFIG_PM
410static int w83627hf_suspend(struct device *dev)
411{
412 struct w83627hf_data *data = w83627hf_update_device(dev);
413
414 mutex_lock(&data->update_lock);
415 data->scfg1 = w83627hf_read_value(data, W83781D_REG_SCFG1);
416 data->scfg2 = w83627hf_read_value(data, W83781D_REG_SCFG2);
417 mutex_unlock(&data->update_lock);
418
419 return 0;
420}
421
422static int w83627hf_resume(struct device *dev)
423{
424 struct w83627hf_data *data = dev_get_drvdata(dev);
425 int i, num_temps = (data->type == w83697hf) ? 2 : 3;
426
427 /* Restore limits */
428 mutex_lock(&data->update_lock);
429 for (i = 0; i <= 8; i++) {
430 /* skip missing sensors */
431 if (((data->type == w83697hf) && (i == 1)) ||
432 ((data->type != w83627hf && data->type != w83697hf)
433 && (i == 5 || i == 6)))
434 continue;
435 w83627hf_write_value(data, W83781D_REG_IN_MAX(i),
436 data->in_max[i]);
437 w83627hf_write_value(data, W83781D_REG_IN_MIN(i),
438 data->in_min[i]);
439 }
440 for (i = 0; i <= 2; i++)
441 w83627hf_write_value(data, W83627HF_REG_FAN_MIN(i),
442 data->fan_min[i]);
443 for (i = 0; i < num_temps; i++) {
444 w83627hf_write_value(data, w83627hf_reg_temp_over[i],
445 data->temp_max[i]);
446 w83627hf_write_value(data, w83627hf_reg_temp_hyst[i],
447 data->temp_max_hyst[i]);
448 }
449
450 /* Fixup BIOS bugs */
451 if (data->type == w83627thf || data->type == w83637hf ||
452 data->type == w83687thf)
453 w83627hf_write_value(data, W83627THF_REG_VRM_OVT_CFG,
454 data->vrm_ovt);
455 w83627hf_write_value(data, W83781D_REG_SCFG1, data->scfg1);
456 w83627hf_write_value(data, W83781D_REG_SCFG2, data->scfg2);
457
458 /* Force re-reading all values */
459 data->valid = 0;
460 mutex_unlock(&data->update_lock);
461
462 return 0;
463}
464
465static const struct dev_pm_ops w83627hf_dev_pm_ops = {
466 .suspend = w83627hf_suspend,
467 .resume = w83627hf_resume,
468};
469
470#define W83627HF_DEV_PM_OPS (&w83627hf_dev_pm_ops)
471#else
472#define W83627HF_DEV_PM_OPS NULL
473#endif /* CONFIG_PM */
474
Jean Delvare787c72b2007-05-08 17:22:00 +0200475static struct platform_driver w83627hf_driver = {
Laurent Riffardcdaf7932005-11-26 20:37:41 +0100476 .driver = {
Jean Delvare87218842006-09-03 22:36:14 +0200477 .owner = THIS_MODULE,
Jean Delvared27c37c2007-05-08 17:21:59 +0200478 .name = DRVNAME,
Jean Delvare275b7d62012-12-19 22:16:59 +0100479 .pm = W83627HF_DEV_PM_OPS,
Laurent Riffardcdaf7932005-11-26 20:37:41 +0100480 },
Jean Delvare787c72b2007-05-08 17:22:00 +0200481 .probe = w83627hf_probe,
Bill Pemberton9e5e9b72012-11-19 13:21:20 -0500482 .remove = w83627hf_remove,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483};
484
Jim Cromie07584c72007-10-12 21:08:00 +0200485static ssize_t
486show_in_input(struct device *dev, struct device_attribute *devattr, char *buf)
487{
488 int nr = to_sensor_dev_attr(devattr)->index;
489 struct w83627hf_data *data = w83627hf_update_device(dev);
490 return sprintf(buf, "%ld\n", (long)IN_FROM_REG(data->in[nr]));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491}
Jim Cromie07584c72007-10-12 21:08:00 +0200492static ssize_t
493show_in_min(struct device *dev, struct device_attribute *devattr, char *buf)
494{
495 int nr = to_sensor_dev_attr(devattr)->index;
496 struct w83627hf_data *data = w83627hf_update_device(dev);
497 return sprintf(buf, "%ld\n", (long)IN_FROM_REG(data->in_min[nr]));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498}
Jim Cromie07584c72007-10-12 21:08:00 +0200499static ssize_t
500show_in_max(struct device *dev, struct device_attribute *devattr, char *buf)
501{
502 int nr = to_sensor_dev_attr(devattr)->index;
503 struct w83627hf_data *data = w83627hf_update_device(dev);
504 return sprintf(buf, "%ld\n", (long)IN_FROM_REG(data->in_max[nr]));
505}
506static ssize_t
507store_in_min(struct device *dev, struct device_attribute *devattr,
508 const char *buf, size_t count)
509{
510 int nr = to_sensor_dev_attr(devattr)->index;
511 struct w83627hf_data *data = dev_get_drvdata(dev);
Guenter Roeck27b9de32012-01-15 11:07:26 -0800512 long val;
513 int err;
514
515 err = kstrtol(buf, 10, &val);
516 if (err)
517 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518
Jim Cromie07584c72007-10-12 21:08:00 +0200519 mutex_lock(&data->update_lock);
520 data->in_min[nr] = IN_TO_REG(val);
521 w83627hf_write_value(data, W83781D_REG_IN_MIN(nr), data->in_min[nr]);
522 mutex_unlock(&data->update_lock);
523 return count;
524}
525static ssize_t
526store_in_max(struct device *dev, struct device_attribute *devattr,
527 const char *buf, size_t count)
528{
529 int nr = to_sensor_dev_attr(devattr)->index;
530 struct w83627hf_data *data = dev_get_drvdata(dev);
Guenter Roeck27b9de32012-01-15 11:07:26 -0800531 long val;
532 int err;
533
534 err = kstrtol(buf, 10, &val);
535 if (err)
536 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537
Jim Cromie07584c72007-10-12 21:08:00 +0200538 mutex_lock(&data->update_lock);
539 data->in_max[nr] = IN_TO_REG(val);
540 w83627hf_write_value(data, W83781D_REG_IN_MAX(nr), data->in_max[nr]);
541 mutex_unlock(&data->update_lock);
542 return count;
543}
544#define sysfs_vin_decl(offset) \
545static SENSOR_DEVICE_ATTR(in##offset##_input, S_IRUGO, \
546 show_in_input, NULL, offset); \
547static SENSOR_DEVICE_ATTR(in##offset##_min, S_IRUGO|S_IWUSR, \
548 show_in_min, store_in_min, offset); \
549static SENSOR_DEVICE_ATTR(in##offset##_max, S_IRUGO|S_IWUSR, \
550 show_in_max, store_in_max, offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551
Jim Cromie07584c72007-10-12 21:08:00 +0200552sysfs_vin_decl(1);
553sysfs_vin_decl(2);
554sysfs_vin_decl(3);
555sysfs_vin_decl(4);
556sysfs_vin_decl(5);
557sysfs_vin_decl(6);
558sysfs_vin_decl(7);
559sysfs_vin_decl(8);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560
561/* use a different set of functions for in0 */
562static ssize_t show_in_0(struct w83627hf_data *data, char *buf, u8 reg)
563{
564 long in0;
565
566 if ((data->vrm_ovt & 0x01) &&
Jean Delvarec2db6ce2006-01-18 23:22:12 +0100567 (w83627thf == data->type || w83637hf == data->type
568 || w83687thf == data->type))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569
570 /* use VRM9 calculation */
571 in0 = (long)((reg * 488 + 70000 + 50) / 100);
572 else
573 /* use VRM8 (standard) calculation */
574 in0 = (long)IN_FROM_REG(reg);
575
576 return sprintf(buf,"%ld\n", in0);
577}
578
Yani Ioannoua5099cf2005-05-17 06:42:25 -0400579static ssize_t show_regs_in_0(struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580{
581 struct w83627hf_data *data = w83627hf_update_device(dev);
582 return show_in_0(data, buf, data->in[0]);
583}
584
Yani Ioannoua5099cf2005-05-17 06:42:25 -0400585static ssize_t show_regs_in_min0(struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586{
587 struct w83627hf_data *data = w83627hf_update_device(dev);
588 return show_in_0(data, buf, data->in_min[0]);
589}
590
Yani Ioannoua5099cf2005-05-17 06:42:25 -0400591static ssize_t show_regs_in_max0(struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592{
593 struct w83627hf_data *data = w83627hf_update_device(dev);
594 return show_in_0(data, buf, data->in_max[0]);
595}
596
Yani Ioannoua5099cf2005-05-17 06:42:25 -0400597static ssize_t store_regs_in_min0(struct device *dev, struct device_attribute *attr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598 const char *buf, size_t count)
599{
Jean Delvare787c72b2007-05-08 17:22:00 +0200600 struct w83627hf_data *data = dev_get_drvdata(dev);
Guenter Roeck27b9de32012-01-15 11:07:26 -0800601 unsigned long val;
602 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603
Guenter Roeck27b9de32012-01-15 11:07:26 -0800604 err = kstrtoul(buf, 10, &val);
605 if (err)
606 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100608 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609
610 if ((data->vrm_ovt & 0x01) &&
Jean Delvarec2db6ce2006-01-18 23:22:12 +0100611 (w83627thf == data->type || w83637hf == data->type
612 || w83687thf == data->type))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613
614 /* use VRM9 calculation */
Yuan Mu2723ab92005-11-23 15:44:21 -0800615 data->in_min[0] =
Guenter Roeck2a844c12013-01-09 08:09:34 -0800616 clamp_val(((val * 100) - 70000 + 244) / 488, 0, 255);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617 else
618 /* use VRM8 (standard) calculation */
619 data->in_min[0] = IN_TO_REG(val);
620
Jean Delvare787c72b2007-05-08 17:22:00 +0200621 w83627hf_write_value(data, W83781D_REG_IN_MIN(0), data->in_min[0]);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100622 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623 return count;
624}
625
Yani Ioannoua5099cf2005-05-17 06:42:25 -0400626static ssize_t store_regs_in_max0(struct device *dev, struct device_attribute *attr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627 const char *buf, size_t count)
628{
Jean Delvare787c72b2007-05-08 17:22:00 +0200629 struct w83627hf_data *data = dev_get_drvdata(dev);
Guenter Roeck27b9de32012-01-15 11:07:26 -0800630 unsigned long val;
631 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632
Guenter Roeck27b9de32012-01-15 11:07:26 -0800633 err = kstrtoul(buf, 10, &val);
634 if (err)
635 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100637 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638
639 if ((data->vrm_ovt & 0x01) &&
Jean Delvarec2db6ce2006-01-18 23:22:12 +0100640 (w83627thf == data->type || w83637hf == data->type
641 || w83687thf == data->type))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642
643 /* use VRM9 calculation */
Yuan Mu2723ab92005-11-23 15:44:21 -0800644 data->in_max[0] =
Guenter Roeck2a844c12013-01-09 08:09:34 -0800645 clamp_val(((val * 100) - 70000 + 244) / 488, 0, 255);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646 else
647 /* use VRM8 (standard) calculation */
648 data->in_max[0] = IN_TO_REG(val);
649
Jean Delvare787c72b2007-05-08 17:22:00 +0200650 w83627hf_write_value(data, W83781D_REG_IN_MAX(0), data->in_max[0]);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100651 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652 return count;
653}
654
655static DEVICE_ATTR(in0_input, S_IRUGO, show_regs_in_0, NULL);
656static DEVICE_ATTR(in0_min, S_IRUGO | S_IWUSR,
657 show_regs_in_min0, store_regs_in_min0);
658static DEVICE_ATTR(in0_max, S_IRUGO | S_IWUSR,
659 show_regs_in_max0, store_regs_in_max0);
660
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661static ssize_t
Jim Cromie07584c72007-10-12 21:08:00 +0200662show_fan_input(struct device *dev, struct device_attribute *devattr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663{
Jim Cromie07584c72007-10-12 21:08:00 +0200664 int nr = to_sensor_dev_attr(devattr)->index;
665 struct w83627hf_data *data = w83627hf_update_device(dev);
666 return sprintf(buf, "%ld\n", FAN_FROM_REG(data->fan[nr],
667 (long)DIV_FROM_REG(data->fan_div[nr])));
668}
669static ssize_t
670show_fan_min(struct device *dev, struct device_attribute *devattr, char *buf)
671{
672 int nr = to_sensor_dev_attr(devattr)->index;
673 struct w83627hf_data *data = w83627hf_update_device(dev);
674 return sprintf(buf, "%ld\n", FAN_FROM_REG(data->fan_min[nr],
675 (long)DIV_FROM_REG(data->fan_div[nr])));
676}
677static ssize_t
678store_fan_min(struct device *dev, struct device_attribute *devattr,
679 const char *buf, size_t count)
680{
681 int nr = to_sensor_dev_attr(devattr)->index;
Jean Delvare787c72b2007-05-08 17:22:00 +0200682 struct w83627hf_data *data = dev_get_drvdata(dev);
Guenter Roeck27b9de32012-01-15 11:07:26 -0800683 unsigned long val;
684 int err;
685
686 err = kstrtoul(buf, 10, &val);
687 if (err)
688 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100690 mutex_lock(&data->update_lock);
Jim Cromie07584c72007-10-12 21:08:00 +0200691 data->fan_min[nr] = FAN_TO_REG(val, DIV_FROM_REG(data->fan_div[nr]));
Jim Cromie2ca2fcd2007-10-14 17:20:50 -0600692 w83627hf_write_value(data, W83627HF_REG_FAN_MIN(nr),
Jim Cromie07584c72007-10-12 21:08:00 +0200693 data->fan_min[nr]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100695 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696 return count;
697}
Jim Cromie07584c72007-10-12 21:08:00 +0200698#define sysfs_fan_decl(offset) \
699static SENSOR_DEVICE_ATTR(fan##offset##_input, S_IRUGO, \
700 show_fan_input, NULL, offset - 1); \
701static SENSOR_DEVICE_ATTR(fan##offset##_min, S_IRUGO | S_IWUSR, \
702 show_fan_min, store_fan_min, offset - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703
Jim Cromie07584c72007-10-12 21:08:00 +0200704sysfs_fan_decl(1);
705sysfs_fan_decl(2);
706sysfs_fan_decl(3);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707
Jim Cromie07584c72007-10-12 21:08:00 +0200708static ssize_t
709show_temp(struct device *dev, struct device_attribute *devattr, char *buf)
710{
711 int nr = to_sensor_dev_attr(devattr)->index;
712 struct w83627hf_data *data = w83627hf_update_device(dev);
Jim Cromiedf48ed82007-10-14 17:10:52 -0600713
714 u16 tmp = data->temp[nr];
715 return sprintf(buf, "%ld\n", (nr) ? (long) LM75_TEMP_FROM_REG(tmp)
716 : (long) TEMP_FROM_REG(tmp));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718
Jim Cromie07584c72007-10-12 21:08:00 +0200719static ssize_t
720show_temp_max(struct device *dev, struct device_attribute *devattr,
721 char *buf)
722{
723 int nr = to_sensor_dev_attr(devattr)->index;
724 struct w83627hf_data *data = w83627hf_update_device(dev);
Jim Cromiedf48ed82007-10-14 17:10:52 -0600725
726 u16 tmp = data->temp_max[nr];
727 return sprintf(buf, "%ld\n", (nr) ? (long) LM75_TEMP_FROM_REG(tmp)
728 : (long) TEMP_FROM_REG(tmp));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730
Jim Cromie07584c72007-10-12 21:08:00 +0200731static ssize_t
732show_temp_max_hyst(struct device *dev, struct device_attribute *devattr,
733 char *buf)
734{
735 int nr = to_sensor_dev_attr(devattr)->index;
736 struct w83627hf_data *data = w83627hf_update_device(dev);
Jim Cromiedf48ed82007-10-14 17:10:52 -0600737
738 u16 tmp = data->temp_max_hyst[nr];
739 return sprintf(buf, "%ld\n", (nr) ? (long) LM75_TEMP_FROM_REG(tmp)
740 : (long) TEMP_FROM_REG(tmp));
Jim Cromie07584c72007-10-12 21:08:00 +0200741}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742
Jim Cromie07584c72007-10-12 21:08:00 +0200743static ssize_t
744store_temp_max(struct device *dev, struct device_attribute *devattr,
745 const char *buf, size_t count)
746{
747 int nr = to_sensor_dev_attr(devattr)->index;
748 struct w83627hf_data *data = dev_get_drvdata(dev);
Guenter Roeck27b9de32012-01-15 11:07:26 -0800749 u16 tmp;
750 long val;
751 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752
Guenter Roeck27b9de32012-01-15 11:07:26 -0800753 err = kstrtol(buf, 10, &val);
754 if (err)
755 return err;
756
757 tmp = (nr) ? LM75_TEMP_TO_REG(val) : TEMP_TO_REG(val);
Jim Cromie07584c72007-10-12 21:08:00 +0200758 mutex_lock(&data->update_lock);
Jim Cromiedf48ed82007-10-14 17:10:52 -0600759 data->temp_max[nr] = tmp;
760 w83627hf_write_value(data, w83627hf_reg_temp_over[nr], tmp);
Jim Cromie07584c72007-10-12 21:08:00 +0200761 mutex_unlock(&data->update_lock);
762 return count;
763}
764
765static ssize_t
766store_temp_max_hyst(struct device *dev, struct device_attribute *devattr,
767 const char *buf, size_t count)
768{
769 int nr = to_sensor_dev_attr(devattr)->index;
770 struct w83627hf_data *data = dev_get_drvdata(dev);
Guenter Roeck27b9de32012-01-15 11:07:26 -0800771 u16 tmp;
772 long val;
773 int err;
Jim Cromie07584c72007-10-12 21:08:00 +0200774
Guenter Roeck27b9de32012-01-15 11:07:26 -0800775 err = kstrtol(buf, 10, &val);
776 if (err)
777 return err;
778
779 tmp = (nr) ? LM75_TEMP_TO_REG(val) : TEMP_TO_REG(val);
Jim Cromie07584c72007-10-12 21:08:00 +0200780 mutex_lock(&data->update_lock);
Jim Cromiedf48ed82007-10-14 17:10:52 -0600781 data->temp_max_hyst[nr] = tmp;
782 w83627hf_write_value(data, w83627hf_reg_temp_hyst[nr], tmp);
Jim Cromie07584c72007-10-12 21:08:00 +0200783 mutex_unlock(&data->update_lock);
784 return count;
785}
786
787#define sysfs_temp_decl(offset) \
788static SENSOR_DEVICE_ATTR(temp##offset##_input, S_IRUGO, \
Jim Cromiedf48ed82007-10-14 17:10:52 -0600789 show_temp, NULL, offset - 1); \
Jim Cromie07584c72007-10-12 21:08:00 +0200790static SENSOR_DEVICE_ATTR(temp##offset##_max, S_IRUGO|S_IWUSR, \
Jim Cromiedf48ed82007-10-14 17:10:52 -0600791 show_temp_max, store_temp_max, offset - 1); \
Jim Cromie07584c72007-10-12 21:08:00 +0200792static SENSOR_DEVICE_ATTR(temp##offset##_max_hyst, S_IRUGO|S_IWUSR, \
Jim Cromiedf48ed82007-10-14 17:10:52 -0600793 show_temp_max_hyst, store_temp_max_hyst, offset - 1);
Jim Cromie07584c72007-10-12 21:08:00 +0200794
795sysfs_temp_decl(1);
796sysfs_temp_decl(2);
797sysfs_temp_decl(3);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799static ssize_t
Yani Ioannoua5099cf2005-05-17 06:42:25 -0400800show_vid_reg(struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801{
802 struct w83627hf_data *data = w83627hf_update_device(dev);
803 return sprintf(buf, "%ld\n", (long) vid_from_reg(data->vid, data->vrm));
804}
805static DEVICE_ATTR(cpu0_vid, S_IRUGO, show_vid_reg, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806
807static ssize_t
Yani Ioannoua5099cf2005-05-17 06:42:25 -0400808show_vrm_reg(struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809{
Jean Delvare90d66192007-10-08 18:24:35 +0200810 struct w83627hf_data *data = dev_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811 return sprintf(buf, "%ld\n", (long) data->vrm);
812}
813static ssize_t
Yani Ioannoua5099cf2005-05-17 06:42:25 -0400814store_vrm_reg(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815{
Jean Delvare787c72b2007-05-08 17:22:00 +0200816 struct w83627hf_data *data = dev_get_drvdata(dev);
Guenter Roeck27b9de32012-01-15 11:07:26 -0800817 unsigned long val;
818 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819
Guenter Roeck27b9de32012-01-15 11:07:26 -0800820 err = kstrtoul(buf, 10, &val);
821 if (err)
822 return err;
Axel Lin970255b2014-08-06 08:27:11 +0800823
824 if (val > 255)
825 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826 data->vrm = val;
827
828 return count;
829}
830static DEVICE_ATTR(vrm, S_IRUGO | S_IWUSR, show_vrm_reg, store_vrm_reg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831
832static ssize_t
Yani Ioannoua5099cf2005-05-17 06:42:25 -0400833show_alarms_reg(struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834{
835 struct w83627hf_data *data = w83627hf_update_device(dev);
836 return sprintf(buf, "%ld\n", (long) data->alarms);
837}
838static DEVICE_ATTR(alarms, S_IRUGO, show_alarms_reg, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839
Jean Delvaree3604c62008-01-03 23:00:30 +0100840static ssize_t
841show_alarm(struct device *dev, struct device_attribute *attr, char *buf)
842{
843 struct w83627hf_data *data = w83627hf_update_device(dev);
844 int bitnr = to_sensor_dev_attr(attr)->index;
845 return sprintf(buf, "%u\n", (data->alarms >> bitnr) & 1);
846}
847static SENSOR_DEVICE_ATTR(in0_alarm, S_IRUGO, show_alarm, NULL, 0);
848static SENSOR_DEVICE_ATTR(in1_alarm, S_IRUGO, show_alarm, NULL, 1);
849static SENSOR_DEVICE_ATTR(in2_alarm, S_IRUGO, show_alarm, NULL, 2);
850static SENSOR_DEVICE_ATTR(in3_alarm, S_IRUGO, show_alarm, NULL, 3);
851static SENSOR_DEVICE_ATTR(in4_alarm, S_IRUGO, show_alarm, NULL, 8);
852static SENSOR_DEVICE_ATTR(in5_alarm, S_IRUGO, show_alarm, NULL, 9);
853static SENSOR_DEVICE_ATTR(in6_alarm, S_IRUGO, show_alarm, NULL, 10);
854static SENSOR_DEVICE_ATTR(in7_alarm, S_IRUGO, show_alarm, NULL, 16);
855static SENSOR_DEVICE_ATTR(in8_alarm, S_IRUGO, show_alarm, NULL, 17);
856static SENSOR_DEVICE_ATTR(fan1_alarm, S_IRUGO, show_alarm, NULL, 6);
857static SENSOR_DEVICE_ATTR(fan2_alarm, S_IRUGO, show_alarm, NULL, 7);
858static SENSOR_DEVICE_ATTR(fan3_alarm, S_IRUGO, show_alarm, NULL, 11);
859static SENSOR_DEVICE_ATTR(temp1_alarm, S_IRUGO, show_alarm, NULL, 4);
860static SENSOR_DEVICE_ATTR(temp2_alarm, S_IRUGO, show_alarm, NULL, 5);
861static SENSOR_DEVICE_ATTR(temp3_alarm, S_IRUGO, show_alarm, NULL, 13);
862
Jean Delvare1c138102008-01-03 23:04:55 +0100863static ssize_t
864show_beep_mask(struct device *dev, struct device_attribute *attr, char *buf)
865{
866 struct w83627hf_data *data = w83627hf_update_device(dev);
867 return sprintf(buf, "%ld\n",
868 (long)BEEP_MASK_FROM_REG(data->beep_mask));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870
871static ssize_t
Jean Delvare1c138102008-01-03 23:04:55 +0100872store_beep_mask(struct device *dev, struct device_attribute *attr,
873 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874{
Jean Delvare787c72b2007-05-08 17:22:00 +0200875 struct w83627hf_data *data = dev_get_drvdata(dev);
Jean Delvare1c138102008-01-03 23:04:55 +0100876 unsigned long val;
Guenter Roeck27b9de32012-01-15 11:07:26 -0800877 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878
Guenter Roeck27b9de32012-01-15 11:07:26 -0800879 err = kstrtoul(buf, 10, &val);
880 if (err)
881 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100883 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884
Jean Delvare1c138102008-01-03 23:04:55 +0100885 /* preserve beep enable */
886 data->beep_mask = (data->beep_mask & 0x8000)
887 | BEEP_MASK_TO_REG(val);
888 w83627hf_write_value(data, W83781D_REG_BEEP_INTS1,
889 data->beep_mask & 0xff);
890 w83627hf_write_value(data, W83781D_REG_BEEP_INTS3,
891 ((data->beep_mask) >> 16) & 0xff);
Jean Delvare787c72b2007-05-08 17:22:00 +0200892 w83627hf_write_value(data, W83781D_REG_BEEP_INTS2,
Jean Delvare1c138102008-01-03 23:04:55 +0100893 (data->beep_mask >> 8) & 0xff);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100895 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896 return count;
897}
898
Jean Delvare1c138102008-01-03 23:04:55 +0100899static DEVICE_ATTR(beep_mask, S_IRUGO | S_IWUSR,
900 show_beep_mask, store_beep_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902static ssize_t
Jean Delvaree3604c62008-01-03 23:00:30 +0100903show_beep(struct device *dev, struct device_attribute *attr, char *buf)
904{
905 struct w83627hf_data *data = w83627hf_update_device(dev);
906 int bitnr = to_sensor_dev_attr(attr)->index;
907 return sprintf(buf, "%u\n", (data->beep_mask >> bitnr) & 1);
908}
909
910static ssize_t
911store_beep(struct device *dev, struct device_attribute *attr,
912 const char *buf, size_t count)
913{
914 struct w83627hf_data *data = dev_get_drvdata(dev);
915 int bitnr = to_sensor_dev_attr(attr)->index;
Jean Delvaree3604c62008-01-03 23:00:30 +0100916 u8 reg;
Guenter Roeck27b9de32012-01-15 11:07:26 -0800917 unsigned long bit;
918 int err;
Jean Delvaree3604c62008-01-03 23:00:30 +0100919
Guenter Roeck27b9de32012-01-15 11:07:26 -0800920 err = kstrtoul(buf, 10, &bit);
921 if (err)
922 return err;
923
Jean Delvaree3604c62008-01-03 23:00:30 +0100924 if (bit & ~1)
925 return -EINVAL;
926
927 mutex_lock(&data->update_lock);
928 if (bit)
929 data->beep_mask |= (1 << bitnr);
930 else
931 data->beep_mask &= ~(1 << bitnr);
932
933 if (bitnr < 8) {
934 reg = w83627hf_read_value(data, W83781D_REG_BEEP_INTS1);
935 if (bit)
936 reg |= (1 << bitnr);
937 else
938 reg &= ~(1 << bitnr);
939 w83627hf_write_value(data, W83781D_REG_BEEP_INTS1, reg);
940 } else if (bitnr < 16) {
941 reg = w83627hf_read_value(data, W83781D_REG_BEEP_INTS2);
942 if (bit)
943 reg |= (1 << (bitnr - 8));
944 else
945 reg &= ~(1 << (bitnr - 8));
946 w83627hf_write_value(data, W83781D_REG_BEEP_INTS2, reg);
947 } else {
948 reg = w83627hf_read_value(data, W83781D_REG_BEEP_INTS3);
949 if (bit)
950 reg |= (1 << (bitnr - 16));
951 else
952 reg &= ~(1 << (bitnr - 16));
953 w83627hf_write_value(data, W83781D_REG_BEEP_INTS3, reg);
954 }
955 mutex_unlock(&data->update_lock);
956
957 return count;
958}
959
960static SENSOR_DEVICE_ATTR(in0_beep, S_IRUGO | S_IWUSR,
961 show_beep, store_beep, 0);
962static SENSOR_DEVICE_ATTR(in1_beep, S_IRUGO | S_IWUSR,
963 show_beep, store_beep, 1);
964static SENSOR_DEVICE_ATTR(in2_beep, S_IRUGO | S_IWUSR,
965 show_beep, store_beep, 2);
966static SENSOR_DEVICE_ATTR(in3_beep, S_IRUGO | S_IWUSR,
967 show_beep, store_beep, 3);
968static SENSOR_DEVICE_ATTR(in4_beep, S_IRUGO | S_IWUSR,
969 show_beep, store_beep, 8);
970static SENSOR_DEVICE_ATTR(in5_beep, S_IRUGO | S_IWUSR,
971 show_beep, store_beep, 9);
972static SENSOR_DEVICE_ATTR(in6_beep, S_IRUGO | S_IWUSR,
973 show_beep, store_beep, 10);
974static SENSOR_DEVICE_ATTR(in7_beep, S_IRUGO | S_IWUSR,
975 show_beep, store_beep, 16);
976static SENSOR_DEVICE_ATTR(in8_beep, S_IRUGO | S_IWUSR,
977 show_beep, store_beep, 17);
978static SENSOR_DEVICE_ATTR(fan1_beep, S_IRUGO | S_IWUSR,
979 show_beep, store_beep, 6);
980static SENSOR_DEVICE_ATTR(fan2_beep, S_IRUGO | S_IWUSR,
981 show_beep, store_beep, 7);
982static SENSOR_DEVICE_ATTR(fan3_beep, S_IRUGO | S_IWUSR,
983 show_beep, store_beep, 11);
984static SENSOR_DEVICE_ATTR(temp1_beep, S_IRUGO | S_IWUSR,
985 show_beep, store_beep, 4);
986static SENSOR_DEVICE_ATTR(temp2_beep, S_IRUGO | S_IWUSR,
987 show_beep, store_beep, 5);
988static SENSOR_DEVICE_ATTR(temp3_beep, S_IRUGO | S_IWUSR,
989 show_beep, store_beep, 13);
Jean Delvare1c138102008-01-03 23:04:55 +0100990static SENSOR_DEVICE_ATTR(beep_enable, S_IRUGO | S_IWUSR,
991 show_beep, store_beep, 15);
Jean Delvaree3604c62008-01-03 23:00:30 +0100992
993static ssize_t
Jim Cromie07584c72007-10-12 21:08:00 +0200994show_fan_div(struct device *dev, struct device_attribute *devattr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700995{
Jim Cromie07584c72007-10-12 21:08:00 +0200996 int nr = to_sensor_dev_attr(devattr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997 struct w83627hf_data *data = w83627hf_update_device(dev);
998 return sprintf(buf, "%ld\n",
Jim Cromie07584c72007-10-12 21:08:00 +0200999 (long) DIV_FROM_REG(data->fan_div[nr]));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000}
Guenter Roeck27b9de32012-01-15 11:07:26 -08001001/*
1002 * Note: we save and restore the fan minimum here, because its value is
1003 * determined in part by the fan divisor. This follows the principle of
1004 * least surprise; the user doesn't expect the fan minimum to change just
1005 * because the divisor changed.
1006 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007static ssize_t
Jim Cromie07584c72007-10-12 21:08:00 +02001008store_fan_div(struct device *dev, struct device_attribute *devattr,
1009 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001010{
Jim Cromie07584c72007-10-12 21:08:00 +02001011 int nr = to_sensor_dev_attr(devattr)->index;
Jean Delvare787c72b2007-05-08 17:22:00 +02001012 struct w83627hf_data *data = dev_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013 unsigned long min;
1014 u8 reg;
Guenter Roeck27b9de32012-01-15 11:07:26 -08001015 unsigned long val;
1016 int err;
1017
1018 err = kstrtoul(buf, 10, &val);
1019 if (err)
1020 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021
Ingo Molnar9a61bf62006-01-18 23:19:26 +01001022 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001023
1024 /* Save fan_min */
1025 min = FAN_FROM_REG(data->fan_min[nr],
1026 DIV_FROM_REG(data->fan_div[nr]));
1027
1028 data->fan_div[nr] = DIV_TO_REG(val);
1029
Jean Delvare787c72b2007-05-08 17:22:00 +02001030 reg = (w83627hf_read_value(data, nr==2 ? W83781D_REG_PIN : W83781D_REG_VID_FANDIV)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031 & (nr==0 ? 0xcf : 0x3f))
1032 | ((data->fan_div[nr] & 0x03) << (nr==0 ? 4 : 6));
Jean Delvare787c72b2007-05-08 17:22:00 +02001033 w83627hf_write_value(data, nr==2 ? W83781D_REG_PIN : W83781D_REG_VID_FANDIV, reg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001034
Jean Delvare787c72b2007-05-08 17:22:00 +02001035 reg = (w83627hf_read_value(data, W83781D_REG_VBAT)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036 & ~(1 << (5 + nr)))
1037 | ((data->fan_div[nr] & 0x04) << (3 + nr));
Jean Delvare787c72b2007-05-08 17:22:00 +02001038 w83627hf_write_value(data, W83781D_REG_VBAT, reg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001039
1040 /* Restore fan_min */
1041 data->fan_min[nr] = FAN_TO_REG(min, DIV_FROM_REG(data->fan_div[nr]));
Jim Cromie2ca2fcd2007-10-14 17:20:50 -06001042 w83627hf_write_value(data, W83627HF_REG_FAN_MIN(nr), data->fan_min[nr]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043
Ingo Molnar9a61bf62006-01-18 23:19:26 +01001044 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001045 return count;
1046}
1047
Jim Cromie07584c72007-10-12 21:08:00 +02001048static SENSOR_DEVICE_ATTR(fan1_div, S_IRUGO|S_IWUSR,
1049 show_fan_div, store_fan_div, 0);
1050static SENSOR_DEVICE_ATTR(fan2_div, S_IRUGO|S_IWUSR,
1051 show_fan_div, store_fan_div, 1);
1052static SENSOR_DEVICE_ATTR(fan3_div, S_IRUGO|S_IWUSR,
1053 show_fan_div, store_fan_div, 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054
Linus Torvalds1da177e2005-04-16 15:20:36 -07001055static ssize_t
Jim Cromie07584c72007-10-12 21:08:00 +02001056show_pwm(struct device *dev, struct device_attribute *devattr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057{
Jim Cromie07584c72007-10-12 21:08:00 +02001058 int nr = to_sensor_dev_attr(devattr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001059 struct w83627hf_data *data = w83627hf_update_device(dev);
Jim Cromie07584c72007-10-12 21:08:00 +02001060 return sprintf(buf, "%ld\n", (long) data->pwm[nr]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001061}
1062
1063static ssize_t
Jim Cromie07584c72007-10-12 21:08:00 +02001064store_pwm(struct device *dev, struct device_attribute *devattr,
1065 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066{
Jim Cromie07584c72007-10-12 21:08:00 +02001067 int nr = to_sensor_dev_attr(devattr)->index;
Jean Delvare787c72b2007-05-08 17:22:00 +02001068 struct w83627hf_data *data = dev_get_drvdata(dev);
Guenter Roeck27b9de32012-01-15 11:07:26 -08001069 unsigned long val;
1070 int err;
1071
1072 err = kstrtoul(buf, 10, &val);
1073 if (err)
1074 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001075
Ingo Molnar9a61bf62006-01-18 23:19:26 +01001076 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077
1078 if (data->type == w83627thf) {
1079 /* bits 0-3 are reserved in 627THF */
Jim Cromie07584c72007-10-12 21:08:00 +02001080 data->pwm[nr] = PWM_TO_REG(val) & 0xf0;
Jean Delvare787c72b2007-05-08 17:22:00 +02001081 w83627hf_write_value(data,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001082 W836X7HF_REG_PWM(data->type, nr),
Jim Cromie07584c72007-10-12 21:08:00 +02001083 data->pwm[nr] |
Jean Delvare787c72b2007-05-08 17:22:00 +02001084 (w83627hf_read_value(data,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001085 W836X7HF_REG_PWM(data->type, nr)) & 0x0f));
1086 } else {
Jim Cromie07584c72007-10-12 21:08:00 +02001087 data->pwm[nr] = PWM_TO_REG(val);
Jean Delvare787c72b2007-05-08 17:22:00 +02001088 w83627hf_write_value(data,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001089 W836X7HF_REG_PWM(data->type, nr),
Jim Cromie07584c72007-10-12 21:08:00 +02001090 data->pwm[nr]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001091 }
1092
Ingo Molnar9a61bf62006-01-18 23:19:26 +01001093 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001094 return count;
1095}
1096
Jim Cromie07584c72007-10-12 21:08:00 +02001097static SENSOR_DEVICE_ATTR(pwm1, S_IRUGO|S_IWUSR, show_pwm, store_pwm, 0);
1098static SENSOR_DEVICE_ATTR(pwm2, S_IRUGO|S_IWUSR, show_pwm, store_pwm, 1);
1099static SENSOR_DEVICE_ATTR(pwm3, S_IRUGO|S_IWUSR, show_pwm, store_pwm, 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001100
Linus Torvalds1da177e2005-04-16 15:20:36 -07001101static ssize_t
Dominik Geyera95a5ed2008-08-06 22:41:04 +02001102show_pwm_enable(struct device *dev, struct device_attribute *devattr, char *buf)
1103{
1104 int nr = to_sensor_dev_attr(devattr)->index;
1105 struct w83627hf_data *data = w83627hf_update_device(dev);
1106 return sprintf(buf, "%d\n", data->pwm_enable[nr]);
1107}
1108
1109static ssize_t
1110store_pwm_enable(struct device *dev, struct device_attribute *devattr,
1111 const char *buf, size_t count)
1112{
1113 int nr = to_sensor_dev_attr(devattr)->index;
1114 struct w83627hf_data *data = dev_get_drvdata(dev);
Dominik Geyera95a5ed2008-08-06 22:41:04 +02001115 u8 reg;
Guenter Roeck27b9de32012-01-15 11:07:26 -08001116 unsigned long val;
1117 int err;
Dominik Geyera95a5ed2008-08-06 22:41:04 +02001118
Guenter Roeck27b9de32012-01-15 11:07:26 -08001119 err = kstrtoul(buf, 10, &val);
1120 if (err)
1121 return err;
1122
1123 if (!val || val > 3) /* modes 1, 2 and 3 are supported */
Dominik Geyera95a5ed2008-08-06 22:41:04 +02001124 return -EINVAL;
1125 mutex_lock(&data->update_lock);
1126 data->pwm_enable[nr] = val;
1127 reg = w83627hf_read_value(data, W83627THF_REG_PWM_ENABLE[nr]);
1128 reg &= ~(0x03 << W83627THF_PWM_ENABLE_SHIFT[nr]);
1129 reg |= (val - 1) << W83627THF_PWM_ENABLE_SHIFT[nr];
1130 w83627hf_write_value(data, W83627THF_REG_PWM_ENABLE[nr], reg);
1131 mutex_unlock(&data->update_lock);
1132 return count;
1133}
1134
1135static SENSOR_DEVICE_ATTR(pwm1_enable, S_IRUGO|S_IWUSR, show_pwm_enable,
1136 store_pwm_enable, 0);
1137static SENSOR_DEVICE_ATTR(pwm2_enable, S_IRUGO|S_IWUSR, show_pwm_enable,
1138 store_pwm_enable, 1);
1139static SENSOR_DEVICE_ATTR(pwm3_enable, S_IRUGO|S_IWUSR, show_pwm_enable,
1140 store_pwm_enable, 2);
1141
1142static ssize_t
Jim Cromie07584c72007-10-12 21:08:00 +02001143show_pwm_freq(struct device *dev, struct device_attribute *devattr, char *buf)
Carlos Olalla Martinez1550cb62007-06-09 10:11:16 -04001144{
Jim Cromie07584c72007-10-12 21:08:00 +02001145 int nr = to_sensor_dev_attr(devattr)->index;
Carlos Olalla Martinez1550cb62007-06-09 10:11:16 -04001146 struct w83627hf_data *data = w83627hf_update_device(dev);
1147 if (data->type == w83627hf)
1148 return sprintf(buf, "%ld\n",
Jim Cromie07584c72007-10-12 21:08:00 +02001149 pwm_freq_from_reg_627hf(data->pwm_freq[nr]));
Carlos Olalla Martinez1550cb62007-06-09 10:11:16 -04001150 else
1151 return sprintf(buf, "%ld\n",
Jim Cromie07584c72007-10-12 21:08:00 +02001152 pwm_freq_from_reg(data->pwm_freq[nr]));
Carlos Olalla Martinez1550cb62007-06-09 10:11:16 -04001153}
1154
1155static ssize_t
Jim Cromie07584c72007-10-12 21:08:00 +02001156store_pwm_freq(struct device *dev, struct device_attribute *devattr,
1157 const char *buf, size_t count)
Carlos Olalla Martinez1550cb62007-06-09 10:11:16 -04001158{
Jim Cromie07584c72007-10-12 21:08:00 +02001159 int nr = to_sensor_dev_attr(devattr)->index;
Carlos Olalla Martinez1550cb62007-06-09 10:11:16 -04001160 struct w83627hf_data *data = dev_get_drvdata(dev);
1161 static const u8 mask[]={0xF8, 0x8F};
Guenter Roeck27b9de32012-01-15 11:07:26 -08001162 unsigned long val;
1163 int err;
Carlos Olalla Martinez1550cb62007-06-09 10:11:16 -04001164
Guenter Roeck27b9de32012-01-15 11:07:26 -08001165 err = kstrtoul(buf, 10, &val);
1166 if (err)
1167 return err;
Carlos Olalla Martinez1550cb62007-06-09 10:11:16 -04001168
1169 mutex_lock(&data->update_lock);
1170
1171 if (data->type == w83627hf) {
Jim Cromie07584c72007-10-12 21:08:00 +02001172 data->pwm_freq[nr] = pwm_freq_to_reg_627hf(val);
Carlos Olalla Martinez1550cb62007-06-09 10:11:16 -04001173 w83627hf_write_value(data, W83627HF_REG_PWM_FREQ,
Jim Cromie07584c72007-10-12 21:08:00 +02001174 (data->pwm_freq[nr] << (nr*4)) |
Carlos Olalla Martinez1550cb62007-06-09 10:11:16 -04001175 (w83627hf_read_value(data,
Jim Cromie07584c72007-10-12 21:08:00 +02001176 W83627HF_REG_PWM_FREQ) & mask[nr]));
Carlos Olalla Martinez1550cb62007-06-09 10:11:16 -04001177 } else {
Jim Cromie07584c72007-10-12 21:08:00 +02001178 data->pwm_freq[nr] = pwm_freq_to_reg(val);
1179 w83627hf_write_value(data, W83637HF_REG_PWM_FREQ[nr],
1180 data->pwm_freq[nr]);
Carlos Olalla Martinez1550cb62007-06-09 10:11:16 -04001181 }
1182
1183 mutex_unlock(&data->update_lock);
1184 return count;
1185}
1186
Jim Cromie07584c72007-10-12 21:08:00 +02001187static SENSOR_DEVICE_ATTR(pwm1_freq, S_IRUGO|S_IWUSR,
1188 show_pwm_freq, store_pwm_freq, 0);
1189static SENSOR_DEVICE_ATTR(pwm2_freq, S_IRUGO|S_IWUSR,
1190 show_pwm_freq, store_pwm_freq, 1);
1191static SENSOR_DEVICE_ATTR(pwm3_freq, S_IRUGO|S_IWUSR,
1192 show_pwm_freq, store_pwm_freq, 2);
Carlos Olalla Martinez1550cb62007-06-09 10:11:16 -04001193
1194static ssize_t
Jim Cromie07584c72007-10-12 21:08:00 +02001195show_temp_type(struct device *dev, struct device_attribute *devattr,
1196 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001197{
Jim Cromie07584c72007-10-12 21:08:00 +02001198 int nr = to_sensor_dev_attr(devattr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001199 struct w83627hf_data *data = w83627hf_update_device(dev);
Jim Cromie07584c72007-10-12 21:08:00 +02001200 return sprintf(buf, "%ld\n", (long) data->sens[nr]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001201}
1202
1203static ssize_t
Jim Cromie07584c72007-10-12 21:08:00 +02001204store_temp_type(struct device *dev, struct device_attribute *devattr,
1205 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001206{
Jim Cromie07584c72007-10-12 21:08:00 +02001207 int nr = to_sensor_dev_attr(devattr)->index;
Jean Delvare787c72b2007-05-08 17:22:00 +02001208 struct w83627hf_data *data = dev_get_drvdata(dev);
Guenter Roeck27b9de32012-01-15 11:07:26 -08001209 unsigned long val;
1210 u32 tmp;
1211 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001212
Guenter Roeck27b9de32012-01-15 11:07:26 -08001213 err = kstrtoul(buf, 10, &val);
1214 if (err)
1215 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001216
Ingo Molnar9a61bf62006-01-18 23:19:26 +01001217 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001218
1219 switch (val) {
1220 case 1: /* PII/Celeron diode */
Jean Delvare787c72b2007-05-08 17:22:00 +02001221 tmp = w83627hf_read_value(data, W83781D_REG_SCFG1);
1222 w83627hf_write_value(data, W83781D_REG_SCFG1,
Jim Cromie07584c72007-10-12 21:08:00 +02001223 tmp | BIT_SCFG1[nr]);
Jean Delvare787c72b2007-05-08 17:22:00 +02001224 tmp = w83627hf_read_value(data, W83781D_REG_SCFG2);
1225 w83627hf_write_value(data, W83781D_REG_SCFG2,
Jim Cromie07584c72007-10-12 21:08:00 +02001226 tmp | BIT_SCFG2[nr]);
1227 data->sens[nr] = val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001228 break;
1229 case 2: /* 3904 */
Jean Delvare787c72b2007-05-08 17:22:00 +02001230 tmp = w83627hf_read_value(data, W83781D_REG_SCFG1);
1231 w83627hf_write_value(data, W83781D_REG_SCFG1,
Jim Cromie07584c72007-10-12 21:08:00 +02001232 tmp | BIT_SCFG1[nr]);
Jean Delvare787c72b2007-05-08 17:22:00 +02001233 tmp = w83627hf_read_value(data, W83781D_REG_SCFG2);
1234 w83627hf_write_value(data, W83781D_REG_SCFG2,
Jim Cromie07584c72007-10-12 21:08:00 +02001235 tmp & ~BIT_SCFG2[nr]);
1236 data->sens[nr] = val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001237 break;
Jean Delvareb26f9332007-08-16 14:30:01 +02001238 case W83781D_DEFAULT_BETA:
1239 dev_warn(dev, "Sensor type %d is deprecated, please use 4 "
1240 "instead\n", W83781D_DEFAULT_BETA);
1241 /* fall through */
1242 case 4: /* thermistor */
Jean Delvare787c72b2007-05-08 17:22:00 +02001243 tmp = w83627hf_read_value(data, W83781D_REG_SCFG1);
1244 w83627hf_write_value(data, W83781D_REG_SCFG1,
Jim Cromie07584c72007-10-12 21:08:00 +02001245 tmp & ~BIT_SCFG1[nr]);
1246 data->sens[nr] = val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001247 break;
1248 default:
Jean Delvare787c72b2007-05-08 17:22:00 +02001249 dev_err(dev,
Jean Delvareb26f9332007-08-16 14:30:01 +02001250 "Invalid sensor type %ld; must be 1, 2, or 4\n",
1251 (long) val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001252 break;
1253 }
1254
Ingo Molnar9a61bf62006-01-18 23:19:26 +01001255 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001256 return count;
1257}
1258
Jim Cromie07584c72007-10-12 21:08:00 +02001259#define sysfs_temp_type(offset) \
1260static SENSOR_DEVICE_ATTR(temp##offset##_type, S_IRUGO | S_IWUSR, \
1261 show_temp_type, store_temp_type, offset - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001262
Jim Cromie07584c72007-10-12 21:08:00 +02001263sysfs_temp_type(1);
1264sysfs_temp_type(2);
1265sysfs_temp_type(3);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001266
Jim Cromie07584c72007-10-12 21:08:00 +02001267static ssize_t
1268show_name(struct device *dev, struct device_attribute *devattr, char *buf)
Jean Delvare787c72b2007-05-08 17:22:00 +02001269{
1270 struct w83627hf_data *data = dev_get_drvdata(dev);
1271
1272 return sprintf(buf, "%s\n", data->name);
1273}
1274static DEVICE_ATTR(name, S_IRUGO, show_name, NULL);
1275
1276static int __init w83627hf_find(int sioaddr, unsigned short *addr,
1277 struct w83627hf_sio_data *sio_data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001278{
Jean Delvared27c37c2007-05-08 17:21:59 +02001279 int err = -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001280 u16 val;
1281
Andi Kleen64f50302012-08-18 10:30:05 -07001282 static __initconst char *const names[] = {
Jean Delvare787c72b2007-05-08 17:22:00 +02001283 "W83627HF",
1284 "W83627THF",
1285 "W83697HF",
1286 "W83637HF",
1287 "W83687THF",
1288 };
1289
Christian Schultec46c0e92009-12-16 21:38:29 +01001290 sio_data->sioaddr = sioaddr;
Jean Delvareb72656d2009-12-09 20:35:49 +01001291 superio_enter(sio_data);
1292 val = force_id ? force_id : superio_inb(sio_data, DEVID);
Jean Delvare787c72b2007-05-08 17:22:00 +02001293 switch (val) {
1294 case W627_DEVID:
1295 sio_data->type = w83627hf;
1296 break;
1297 case W627THF_DEVID:
1298 sio_data->type = w83627thf;
1299 break;
1300 case W697_DEVID:
1301 sio_data->type = w83697hf;
1302 break;
1303 case W637_DEVID:
1304 sio_data->type = w83637hf;
1305 break;
1306 case W687THF_DEVID:
1307 sio_data->type = w83687thf;
1308 break;
Jean Delvaree142e2a2007-05-27 22:17:43 +02001309 case 0xff: /* No device at all */
1310 goto exit;
Jean Delvare787c72b2007-05-08 17:22:00 +02001311 default:
Jean Delvaree142e2a2007-05-27 22:17:43 +02001312 pr_debug(DRVNAME ": Unsupported chip (DEVID=0x%02x)\n", val);
Jean Delvared27c37c2007-05-08 17:21:59 +02001313 goto exit;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001314 }
1315
Jean Delvareb72656d2009-12-09 20:35:49 +01001316 superio_select(sio_data, W83627HF_LD_HWM);
1317 val = (superio_inb(sio_data, WINB_BASE_REG) << 8) |
1318 superio_inb(sio_data, WINB_BASE_REG + 1);
Petr Vandrovecada0c2f2005-10-07 23:11:03 +02001319 *addr = val & WINB_ALIGNMENT;
Jean Delvared27c37c2007-05-08 17:21:59 +02001320 if (*addr == 0) {
Joe Perches18de0302010-10-20 06:51:55 +00001321 pr_warn("Base address not set, skipping\n");
Jean Delvared27c37c2007-05-08 17:21:59 +02001322 goto exit;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001323 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001324
Jean Delvareb72656d2009-12-09 20:35:49 +01001325 val = superio_inb(sio_data, WINB_ACT_REG);
Jean Delvared27c37c2007-05-08 17:21:59 +02001326 if (!(val & 0x01)) {
Joe Perches18de0302010-10-20 06:51:55 +00001327 pr_warn("Enabling HWM logical device\n");
Jean Delvareb72656d2009-12-09 20:35:49 +01001328 superio_outb(sio_data, WINB_ACT_REG, val | 0x01);
Jean Delvared27c37c2007-05-08 17:21:59 +02001329 }
1330
1331 err = 0;
Jean Delvare787c72b2007-05-08 17:22:00 +02001332 pr_info(DRVNAME ": Found %s chip at %#x\n",
1333 names[sio_data->type], *addr);
Jean Delvared27c37c2007-05-08 17:21:59 +02001334
1335 exit:
Jean Delvareb72656d2009-12-09 20:35:49 +01001336 superio_exit(sio_data);
Jean Delvared27c37c2007-05-08 17:21:59 +02001337 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001338}
1339
Jim Cromie07584c72007-10-12 21:08:00 +02001340#define VIN_UNIT_ATTRS(_X_) \
1341 &sensor_dev_attr_in##_X_##_input.dev_attr.attr, \
1342 &sensor_dev_attr_in##_X_##_min.dev_attr.attr, \
Jean Delvaree3604c62008-01-03 23:00:30 +01001343 &sensor_dev_attr_in##_X_##_max.dev_attr.attr, \
1344 &sensor_dev_attr_in##_X_##_alarm.dev_attr.attr, \
1345 &sensor_dev_attr_in##_X_##_beep.dev_attr.attr
Jim Cromie07584c72007-10-12 21:08:00 +02001346
1347#define FAN_UNIT_ATTRS(_X_) \
1348 &sensor_dev_attr_fan##_X_##_input.dev_attr.attr, \
1349 &sensor_dev_attr_fan##_X_##_min.dev_attr.attr, \
Jean Delvaree3604c62008-01-03 23:00:30 +01001350 &sensor_dev_attr_fan##_X_##_div.dev_attr.attr, \
1351 &sensor_dev_attr_fan##_X_##_alarm.dev_attr.attr, \
1352 &sensor_dev_attr_fan##_X_##_beep.dev_attr.attr
Jim Cromie07584c72007-10-12 21:08:00 +02001353
1354#define TEMP_UNIT_ATTRS(_X_) \
1355 &sensor_dev_attr_temp##_X_##_input.dev_attr.attr, \
1356 &sensor_dev_attr_temp##_X_##_max.dev_attr.attr, \
1357 &sensor_dev_attr_temp##_X_##_max_hyst.dev_attr.attr, \
Jean Delvaree3604c62008-01-03 23:00:30 +01001358 &sensor_dev_attr_temp##_X_##_type.dev_attr.attr, \
1359 &sensor_dev_attr_temp##_X_##_alarm.dev_attr.attr, \
1360 &sensor_dev_attr_temp##_X_##_beep.dev_attr.attr
Jim Cromie07584c72007-10-12 21:08:00 +02001361
Mark M. Hoffmanc1685f62006-09-24 20:59:49 +02001362static struct attribute *w83627hf_attributes[] = {
1363 &dev_attr_in0_input.attr,
1364 &dev_attr_in0_min.attr,
1365 &dev_attr_in0_max.attr,
Jean Delvaree3604c62008-01-03 23:00:30 +01001366 &sensor_dev_attr_in0_alarm.dev_attr.attr,
1367 &sensor_dev_attr_in0_beep.dev_attr.attr,
Jim Cromie07584c72007-10-12 21:08:00 +02001368 VIN_UNIT_ATTRS(2),
1369 VIN_UNIT_ATTRS(3),
1370 VIN_UNIT_ATTRS(4),
1371 VIN_UNIT_ATTRS(7),
1372 VIN_UNIT_ATTRS(8),
Mark M. Hoffmanc1685f62006-09-24 20:59:49 +02001373
Jim Cromie07584c72007-10-12 21:08:00 +02001374 FAN_UNIT_ATTRS(1),
1375 FAN_UNIT_ATTRS(2),
Mark M. Hoffmanc1685f62006-09-24 20:59:49 +02001376
Jim Cromie07584c72007-10-12 21:08:00 +02001377 TEMP_UNIT_ATTRS(1),
1378 TEMP_UNIT_ATTRS(2),
Mark M. Hoffmanc1685f62006-09-24 20:59:49 +02001379
1380 &dev_attr_alarms.attr,
Jean Delvare1c138102008-01-03 23:04:55 +01001381 &sensor_dev_attr_beep_enable.dev_attr.attr,
Mark M. Hoffmanc1685f62006-09-24 20:59:49 +02001382 &dev_attr_beep_mask.attr,
1383
Jim Cromie07584c72007-10-12 21:08:00 +02001384 &sensor_dev_attr_pwm1.dev_attr.attr,
1385 &sensor_dev_attr_pwm2.dev_attr.attr,
Jean Delvare787c72b2007-05-08 17:22:00 +02001386 &dev_attr_name.attr,
Mark M. Hoffmanc1685f62006-09-24 20:59:49 +02001387 NULL
1388};
1389
1390static const struct attribute_group w83627hf_group = {
1391 .attrs = w83627hf_attributes,
1392};
1393
1394static struct attribute *w83627hf_attributes_opt[] = {
Jim Cromie07584c72007-10-12 21:08:00 +02001395 VIN_UNIT_ATTRS(1),
1396 VIN_UNIT_ATTRS(5),
1397 VIN_UNIT_ATTRS(6),
Mark M. Hoffmanc1685f62006-09-24 20:59:49 +02001398
Jim Cromie07584c72007-10-12 21:08:00 +02001399 FAN_UNIT_ATTRS(3),
1400 TEMP_UNIT_ATTRS(3),
1401 &sensor_dev_attr_pwm3.dev_attr.attr,
Mark M. Hoffmanc1685f62006-09-24 20:59:49 +02001402
Jim Cromie07584c72007-10-12 21:08:00 +02001403 &sensor_dev_attr_pwm1_freq.dev_attr.attr,
1404 &sensor_dev_attr_pwm2_freq.dev_attr.attr,
1405 &sensor_dev_attr_pwm3_freq.dev_attr.attr,
Dominik Geyera95a5ed2008-08-06 22:41:04 +02001406
1407 &sensor_dev_attr_pwm1_enable.dev_attr.attr,
1408 &sensor_dev_attr_pwm2_enable.dev_attr.attr,
1409 &sensor_dev_attr_pwm3_enable.dev_attr.attr,
1410
Mark M. Hoffmanc1685f62006-09-24 20:59:49 +02001411 NULL
1412};
1413
1414static const struct attribute_group w83627hf_group_opt = {
1415 .attrs = w83627hf_attributes_opt,
1416};
1417
Bill Pemberton6c931ae2012-11-19 13:22:35 -05001418static int w83627hf_probe(struct platform_device *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001419{
Jean Delvare787c72b2007-05-08 17:22:00 +02001420 struct device *dev = &pdev->dev;
Jingoo Hana8b3a3a2013-07-30 17:13:06 +09001421 struct w83627hf_sio_data *sio_data = dev_get_platdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001422 struct w83627hf_data *data;
Jean Delvare787c72b2007-05-08 17:22:00 +02001423 struct resource *res;
Jim Cromie2ca2fcd2007-10-14 17:20:50 -06001424 int err, i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001425
Jean Delvare787c72b2007-05-08 17:22:00 +02001426 static const char *names[] = {
1427 "w83627hf",
1428 "w83627thf",
1429 "w83697hf",
1430 "w83637hf",
1431 "w83687thf",
1432 };
1433
1434 res = platform_get_resource(pdev, IORESOURCE_IO, 0);
Guenter Roeck0cf46992012-06-02 11:47:59 -07001435 if (!devm_request_region(dev, res->start, WINB_REGION_SIZE, DRVNAME)) {
Jean Delvare787c72b2007-05-08 17:22:00 +02001436 dev_err(dev, "Failed to request region 0x%lx-0x%lx\n",
1437 (unsigned long)res->start,
1438 (unsigned long)(res->start + WINB_REGION_SIZE - 1));
Guenter Roeck0cf46992012-06-02 11:47:59 -07001439 return -EBUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001440 }
1441
Guenter Roeck0cf46992012-06-02 11:47:59 -07001442 data = devm_kzalloc(dev, sizeof(struct w83627hf_data), GFP_KERNEL);
1443 if (!data)
1444 return -ENOMEM;
1445
Jean Delvare787c72b2007-05-08 17:22:00 +02001446 data->addr = res->start;
1447 data->type = sio_data->type;
1448 data->name = names[sio_data->type];
Ingo Molnar9a61bf62006-01-18 23:19:26 +01001449 mutex_init(&data->lock);
Ingo Molnar9a61bf62006-01-18 23:19:26 +01001450 mutex_init(&data->update_lock);
Jean Delvare787c72b2007-05-08 17:22:00 +02001451 platform_set_drvdata(pdev, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001452
Linus Torvalds1da177e2005-04-16 15:20:36 -07001453 /* Initialize the chip */
Jean Delvare787c72b2007-05-08 17:22:00 +02001454 w83627hf_init_device(pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001455
1456 /* A few vars need to be filled upon startup */
Jim Cromie2ca2fcd2007-10-14 17:20:50 -06001457 for (i = 0; i <= 2; i++)
1458 data->fan_min[i] = w83627hf_read_value(
1459 data, W83627HF_REG_FAN_MIN(i));
Jean Delvarec09c5182007-10-12 21:53:07 +02001460 w83627hf_update_fan_div(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001461
Mark M. Hoffmanc1685f62006-09-24 20:59:49 +02001462 /* Register common device attributes */
Guenter Roeck27b9de32012-01-15 11:07:26 -08001463 err = sysfs_create_group(&dev->kobj, &w83627hf_group);
1464 if (err)
Guenter Roeck0cf46992012-06-02 11:47:59 -07001465 return err;
Mark M. Hoffmanc1685f62006-09-24 20:59:49 +02001466
1467 /* Register chip-specific device attributes */
Jean Delvare787c72b2007-05-08 17:22:00 +02001468 if (data->type == w83627hf || data->type == w83697hf)
Jim Cromie07584c72007-10-12 21:08:00 +02001469 if ((err = device_create_file(dev,
1470 &sensor_dev_attr_in5_input.dev_attr))
1471 || (err = device_create_file(dev,
1472 &sensor_dev_attr_in5_min.dev_attr))
1473 || (err = device_create_file(dev,
1474 &sensor_dev_attr_in5_max.dev_attr))
1475 || (err = device_create_file(dev,
Jean Delvaree3604c62008-01-03 23:00:30 +01001476 &sensor_dev_attr_in5_alarm.dev_attr))
1477 || (err = device_create_file(dev,
1478 &sensor_dev_attr_in5_beep.dev_attr))
1479 || (err = device_create_file(dev,
Jim Cromie07584c72007-10-12 21:08:00 +02001480 &sensor_dev_attr_in6_input.dev_attr))
1481 || (err = device_create_file(dev,
1482 &sensor_dev_attr_in6_min.dev_attr))
1483 || (err = device_create_file(dev,
1484 &sensor_dev_attr_in6_max.dev_attr))
1485 || (err = device_create_file(dev,
Jean Delvaree3604c62008-01-03 23:00:30 +01001486 &sensor_dev_attr_in6_alarm.dev_attr))
1487 || (err = device_create_file(dev,
1488 &sensor_dev_attr_in6_beep.dev_attr))
1489 || (err = device_create_file(dev,
Jim Cromie07584c72007-10-12 21:08:00 +02001490 &sensor_dev_attr_pwm1_freq.dev_attr))
1491 || (err = device_create_file(dev,
1492 &sensor_dev_attr_pwm2_freq.dev_attr)))
Guenter Roeck0cf46992012-06-02 11:47:59 -07001493 goto error;
Mark M. Hoffmanc1685f62006-09-24 20:59:49 +02001494
Jean Delvare787c72b2007-05-08 17:22:00 +02001495 if (data->type != w83697hf)
Jim Cromie07584c72007-10-12 21:08:00 +02001496 if ((err = device_create_file(dev,
1497 &sensor_dev_attr_in1_input.dev_attr))
1498 || (err = device_create_file(dev,
1499 &sensor_dev_attr_in1_min.dev_attr))
1500 || (err = device_create_file(dev,
1501 &sensor_dev_attr_in1_max.dev_attr))
1502 || (err = device_create_file(dev,
Jean Delvaree3604c62008-01-03 23:00:30 +01001503 &sensor_dev_attr_in1_alarm.dev_attr))
1504 || (err = device_create_file(dev,
1505 &sensor_dev_attr_in1_beep.dev_attr))
1506 || (err = device_create_file(dev,
Jim Cromie07584c72007-10-12 21:08:00 +02001507 &sensor_dev_attr_fan3_input.dev_attr))
1508 || (err = device_create_file(dev,
1509 &sensor_dev_attr_fan3_min.dev_attr))
1510 || (err = device_create_file(dev,
1511 &sensor_dev_attr_fan3_div.dev_attr))
1512 || (err = device_create_file(dev,
Jean Delvaree3604c62008-01-03 23:00:30 +01001513 &sensor_dev_attr_fan3_alarm.dev_attr))
1514 || (err = device_create_file(dev,
1515 &sensor_dev_attr_fan3_beep.dev_attr))
1516 || (err = device_create_file(dev,
Jim Cromie07584c72007-10-12 21:08:00 +02001517 &sensor_dev_attr_temp3_input.dev_attr))
1518 || (err = device_create_file(dev,
1519 &sensor_dev_attr_temp3_max.dev_attr))
1520 || (err = device_create_file(dev,
1521 &sensor_dev_attr_temp3_max_hyst.dev_attr))
1522 || (err = device_create_file(dev,
Jean Delvaree3604c62008-01-03 23:00:30 +01001523 &sensor_dev_attr_temp3_alarm.dev_attr))
1524 || (err = device_create_file(dev,
1525 &sensor_dev_attr_temp3_beep.dev_attr))
1526 || (err = device_create_file(dev,
Jim Cromie07584c72007-10-12 21:08:00 +02001527 &sensor_dev_attr_temp3_type.dev_attr)))
Guenter Roeck0cf46992012-06-02 11:47:59 -07001528 goto error;
Mark M. Hoffmanc1685f62006-09-24 20:59:49 +02001529
Jean Delvare787c72b2007-05-08 17:22:00 +02001530 if (data->type != w83697hf && data->vid != 0xff) {
Jean Delvare8a665a02007-05-08 17:21:59 +02001531 /* Convert VID to voltage based on VRM */
1532 data->vrm = vid_which_vrm();
1533
Jean Delvare787c72b2007-05-08 17:22:00 +02001534 if ((err = device_create_file(dev, &dev_attr_cpu0_vid))
1535 || (err = device_create_file(dev, &dev_attr_vrm)))
Guenter Roeck0cf46992012-06-02 11:47:59 -07001536 goto error;
Jean Delvare8a665a02007-05-08 17:21:59 +02001537 }
Mark M. Hoffmanc1685f62006-09-24 20:59:49 +02001538
Jean Delvare787c72b2007-05-08 17:22:00 +02001539 if (data->type == w83627thf || data->type == w83637hf
Guenter Roeck27b9de32012-01-15 11:07:26 -08001540 || data->type == w83687thf) {
1541 err = device_create_file(dev, &sensor_dev_attr_pwm3.dev_attr);
1542 if (err)
Guenter Roeck0cf46992012-06-02 11:47:59 -07001543 goto error;
Guenter Roeck27b9de32012-01-15 11:07:26 -08001544 }
Mark M. Hoffmanc1685f62006-09-24 20:59:49 +02001545
Carlos Olalla Martinez1550cb62007-06-09 10:11:16 -04001546 if (data->type == w83637hf || data->type == w83687thf)
Jim Cromie07584c72007-10-12 21:08:00 +02001547 if ((err = device_create_file(dev,
1548 &sensor_dev_attr_pwm1_freq.dev_attr))
1549 || (err = device_create_file(dev,
1550 &sensor_dev_attr_pwm2_freq.dev_attr))
1551 || (err = device_create_file(dev,
1552 &sensor_dev_attr_pwm3_freq.dev_attr)))
Guenter Roeck0cf46992012-06-02 11:47:59 -07001553 goto error;
Carlos Olalla Martinez1550cb62007-06-09 10:11:16 -04001554
Dominik Geyera95a5ed2008-08-06 22:41:04 +02001555 if (data->type != w83627hf)
1556 if ((err = device_create_file(dev,
1557 &sensor_dev_attr_pwm1_enable.dev_attr))
1558 || (err = device_create_file(dev,
1559 &sensor_dev_attr_pwm2_enable.dev_attr)))
Guenter Roeck0cf46992012-06-02 11:47:59 -07001560 goto error;
Dominik Geyera95a5ed2008-08-06 22:41:04 +02001561
1562 if (data->type == w83627thf || data->type == w83637hf
Guenter Roeck27b9de32012-01-15 11:07:26 -08001563 || data->type == w83687thf) {
1564 err = device_create_file(dev,
1565 &sensor_dev_attr_pwm3_enable.dev_attr);
1566 if (err)
Guenter Roeck0cf46992012-06-02 11:47:59 -07001567 goto error;
Guenter Roeck27b9de32012-01-15 11:07:26 -08001568 }
Dominik Geyera95a5ed2008-08-06 22:41:04 +02001569
Tony Jones1beeffe2007-08-20 13:46:20 -07001570 data->hwmon_dev = hwmon_device_register(dev);
1571 if (IS_ERR(data->hwmon_dev)) {
1572 err = PTR_ERR(data->hwmon_dev);
Guenter Roeck0cf46992012-06-02 11:47:59 -07001573 goto error;
Mark M. Hoffman943b0832005-07-15 21:39:18 -04001574 }
1575
Linus Torvalds1da177e2005-04-16 15:20:36 -07001576 return 0;
1577
Guenter Roeck0cf46992012-06-02 11:47:59 -07001578 error:
Jean Delvare787c72b2007-05-08 17:22:00 +02001579 sysfs_remove_group(&dev->kobj, &w83627hf_group);
1580 sysfs_remove_group(&dev->kobj, &w83627hf_group_opt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001581 return err;
1582}
1583
Bill Pemberton281dfd02012-11-19 13:25:51 -05001584static int w83627hf_remove(struct platform_device *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001585{
Jean Delvare787c72b2007-05-08 17:22:00 +02001586 struct w83627hf_data *data = platform_get_drvdata(pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001587
Tony Jones1beeffe2007-08-20 13:46:20 -07001588 hwmon_device_unregister(data->hwmon_dev);
Mark M. Hoffman943b0832005-07-15 21:39:18 -04001589
Jean Delvare787c72b2007-05-08 17:22:00 +02001590 sysfs_remove_group(&pdev->dev.kobj, &w83627hf_group);
1591 sysfs_remove_group(&pdev->dev.kobj, &w83627hf_group_opt);
Jean Delvare787c72b2007-05-08 17:22:00 +02001592
Linus Torvalds1da177e2005-04-16 15:20:36 -07001593 return 0;
1594}
1595
1596
Jean Delvared58df9c2007-10-10 16:30:23 +02001597/* Registers 0x50-0x5f are banked */
1598static inline void w83627hf_set_bank(struct w83627hf_data *data, u16 reg)
1599{
1600 if ((reg & 0x00f0) == 0x50) {
1601 outb_p(W83781D_REG_BANK, data->addr + W83781D_ADDR_REG_OFFSET);
1602 outb_p(reg >> 8, data->addr + W83781D_DATA_REG_OFFSET);
1603 }
1604}
1605
1606/* Not strictly necessary, but play it safe for now */
1607static inline void w83627hf_reset_bank(struct w83627hf_data *data, u16 reg)
1608{
1609 if (reg & 0xff00) {
1610 outb_p(W83781D_REG_BANK, data->addr + W83781D_ADDR_REG_OFFSET);
1611 outb_p(0, data->addr + W83781D_DATA_REG_OFFSET);
1612 }
1613}
1614
Jean Delvare787c72b2007-05-08 17:22:00 +02001615static int w83627hf_read_value(struct w83627hf_data *data, u16 reg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001616{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001617 int res, word_sized;
1618
Ingo Molnar9a61bf62006-01-18 23:19:26 +01001619 mutex_lock(&data->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001620 word_sized = (((reg & 0xff00) == 0x100)
1621 || ((reg & 0xff00) == 0x200))
1622 && (((reg & 0x00ff) == 0x50)
1623 || ((reg & 0x00ff) == 0x53)
1624 || ((reg & 0x00ff) == 0x55));
Jean Delvared58df9c2007-10-10 16:30:23 +02001625 w83627hf_set_bank(data, reg);
Jean Delvare787c72b2007-05-08 17:22:00 +02001626 outb_p(reg & 0xff, data->addr + W83781D_ADDR_REG_OFFSET);
1627 res = inb_p(data->addr + W83781D_DATA_REG_OFFSET);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001628 if (word_sized) {
1629 outb_p((reg & 0xff) + 1,
Jean Delvare787c72b2007-05-08 17:22:00 +02001630 data->addr + W83781D_ADDR_REG_OFFSET);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001631 res =
Jean Delvare787c72b2007-05-08 17:22:00 +02001632 (res << 8) + inb_p(data->addr +
Linus Torvalds1da177e2005-04-16 15:20:36 -07001633 W83781D_DATA_REG_OFFSET);
1634 }
Jean Delvared58df9c2007-10-10 16:30:23 +02001635 w83627hf_reset_bank(data, reg);
Ingo Molnar9a61bf62006-01-18 23:19:26 +01001636 mutex_unlock(&data->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001637 return res;
1638}
1639
Bill Pemberton6c931ae2012-11-19 13:22:35 -05001640static int w83627thf_read_gpio5(struct platform_device *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001641{
Jingoo Hana8b3a3a2013-07-30 17:13:06 +09001642 struct w83627hf_sio_data *sio_data = dev_get_platdata(&pdev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001643 int res = 0xff, sel;
1644
Jean Delvareb72656d2009-12-09 20:35:49 +01001645 superio_enter(sio_data);
1646 superio_select(sio_data, W83627HF_LD_GPIO5);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001647
1648 /* Make sure these GPIO pins are enabled */
Jean Delvareb72656d2009-12-09 20:35:49 +01001649 if (!(superio_inb(sio_data, W83627THF_GPIO5_EN) & (1<<3))) {
Jean Delvare787c72b2007-05-08 17:22:00 +02001650 dev_dbg(&pdev->dev, "GPIO5 disabled, no VID function\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001651 goto exit;
1652 }
1653
Guenter Roeck27b9de32012-01-15 11:07:26 -08001654 /*
1655 * Make sure the pins are configured for input
1656 * There must be at least five (VRM 9), and possibly 6 (VRM 10)
1657 */
Jean Delvareb72656d2009-12-09 20:35:49 +01001658 sel = superio_inb(sio_data, W83627THF_GPIO5_IOSR) & 0x3f;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001659 if ((sel & 0x1f) != 0x1f) {
Jean Delvare787c72b2007-05-08 17:22:00 +02001660 dev_dbg(&pdev->dev, "GPIO5 not configured for VID "
Linus Torvalds1da177e2005-04-16 15:20:36 -07001661 "function\n");
1662 goto exit;
1663 }
1664
Jean Delvare787c72b2007-05-08 17:22:00 +02001665 dev_info(&pdev->dev, "Reading VID from GPIO5\n");
Jean Delvareb72656d2009-12-09 20:35:49 +01001666 res = superio_inb(sio_data, W83627THF_GPIO5_DR) & sel;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001667
1668exit:
Jean Delvareb72656d2009-12-09 20:35:49 +01001669 superio_exit(sio_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001670 return res;
1671}
1672
Bill Pemberton6c931ae2012-11-19 13:22:35 -05001673static int w83687thf_read_vid(struct platform_device *pdev)
Jean Delvarec2db6ce2006-01-18 23:22:12 +01001674{
Jingoo Hana8b3a3a2013-07-30 17:13:06 +09001675 struct w83627hf_sio_data *sio_data = dev_get_platdata(&pdev->dev);
Jean Delvarec2db6ce2006-01-18 23:22:12 +01001676 int res = 0xff;
1677
Jean Delvareb72656d2009-12-09 20:35:49 +01001678 superio_enter(sio_data);
1679 superio_select(sio_data, W83627HF_LD_HWM);
Jean Delvarec2db6ce2006-01-18 23:22:12 +01001680
1681 /* Make sure these GPIO pins are enabled */
Jean Delvareb72656d2009-12-09 20:35:49 +01001682 if (!(superio_inb(sio_data, W83687THF_VID_EN) & (1 << 2))) {
Jean Delvare787c72b2007-05-08 17:22:00 +02001683 dev_dbg(&pdev->dev, "VID disabled, no VID function\n");
Jean Delvarec2db6ce2006-01-18 23:22:12 +01001684 goto exit;
1685 }
1686
1687 /* Make sure the pins are configured for input */
Jean Delvareb72656d2009-12-09 20:35:49 +01001688 if (!(superio_inb(sio_data, W83687THF_VID_CFG) & (1 << 4))) {
Jean Delvare787c72b2007-05-08 17:22:00 +02001689 dev_dbg(&pdev->dev, "VID configured as output, "
Jean Delvarec2db6ce2006-01-18 23:22:12 +01001690 "no VID function\n");
1691 goto exit;
1692 }
1693
Jean Delvareb72656d2009-12-09 20:35:49 +01001694 res = superio_inb(sio_data, W83687THF_VID_DATA) & 0x3f;
Jean Delvarec2db6ce2006-01-18 23:22:12 +01001695
1696exit:
Jean Delvareb72656d2009-12-09 20:35:49 +01001697 superio_exit(sio_data);
Jean Delvarec2db6ce2006-01-18 23:22:12 +01001698 return res;
1699}
1700
Jean Delvare787c72b2007-05-08 17:22:00 +02001701static int w83627hf_write_value(struct w83627hf_data *data, u16 reg, u16 value)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001702{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001703 int word_sized;
1704
Ingo Molnar9a61bf62006-01-18 23:19:26 +01001705 mutex_lock(&data->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001706 word_sized = (((reg & 0xff00) == 0x100)
1707 || ((reg & 0xff00) == 0x200))
1708 && (((reg & 0x00ff) == 0x53)
1709 || ((reg & 0x00ff) == 0x55));
Jean Delvared58df9c2007-10-10 16:30:23 +02001710 w83627hf_set_bank(data, reg);
Jean Delvare787c72b2007-05-08 17:22:00 +02001711 outb_p(reg & 0xff, data->addr + W83781D_ADDR_REG_OFFSET);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001712 if (word_sized) {
1713 outb_p(value >> 8,
Jean Delvare787c72b2007-05-08 17:22:00 +02001714 data->addr + W83781D_DATA_REG_OFFSET);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001715 outb_p((reg & 0xff) + 1,
Jean Delvare787c72b2007-05-08 17:22:00 +02001716 data->addr + W83781D_ADDR_REG_OFFSET);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001717 }
1718 outb_p(value & 0xff,
Jean Delvare787c72b2007-05-08 17:22:00 +02001719 data->addr + W83781D_DATA_REG_OFFSET);
Jean Delvared58df9c2007-10-10 16:30:23 +02001720 w83627hf_reset_bank(data, reg);
Ingo Molnar9a61bf62006-01-18 23:19:26 +01001721 mutex_unlock(&data->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001722 return 0;
1723}
1724
Bill Pemberton6c931ae2012-11-19 13:22:35 -05001725static void w83627hf_init_device(struct platform_device *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001726{
Jean Delvare787c72b2007-05-08 17:22:00 +02001727 struct w83627hf_data *data = platform_get_drvdata(pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001728 int i;
Jean Delvared27c37c2007-05-08 17:21:59 +02001729 enum chips type = data->type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001730 u8 tmp;
1731
Linus Torvalds1da177e2005-04-16 15:20:36 -07001732 /* Minimize conflicts with other winbond i2c-only clients... */
1733 /* disable i2c subclients... how to disable main i2c client?? */
1734 /* force i2c address to relatively uncommon address */
Jean Delvare8f3c7c52012-12-19 22:17:00 +01001735 if (type == w83627hf) {
1736 w83627hf_write_value(data, W83781D_REG_I2C_SUBADDR, 0x89);
1737 w83627hf_write_value(data, W83781D_REG_I2C_ADDR, force_i2c);
1738 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001739
1740 /* Read VID only once */
Jean Delvared27c37c2007-05-08 17:21:59 +02001741 if (type == w83627hf || type == w83637hf) {
Jean Delvare787c72b2007-05-08 17:22:00 +02001742 int lo = w83627hf_read_value(data, W83781D_REG_VID_FANDIV);
1743 int hi = w83627hf_read_value(data, W83781D_REG_CHIPID);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001744 data->vid = (lo & 0x0f) | ((hi & 0x01) << 4);
Jean Delvared27c37c2007-05-08 17:21:59 +02001745 } else if (type == w83627thf) {
Jean Delvare787c72b2007-05-08 17:22:00 +02001746 data->vid = w83627thf_read_gpio5(pdev);
Jean Delvared27c37c2007-05-08 17:21:59 +02001747 } else if (type == w83687thf) {
Jean Delvare787c72b2007-05-08 17:22:00 +02001748 data->vid = w83687thf_read_vid(pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001749 }
1750
1751 /* Read VRM & OVT Config only once */
Jean Delvared27c37c2007-05-08 17:21:59 +02001752 if (type == w83627thf || type == w83637hf || type == w83687thf) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001753 data->vrm_ovt =
Jean Delvare787c72b2007-05-08 17:22:00 +02001754 w83627hf_read_value(data, W83627THF_REG_VRM_OVT_CFG);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001755 }
1756
Jean Delvare787c72b2007-05-08 17:22:00 +02001757 tmp = w83627hf_read_value(data, W83781D_REG_SCFG1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001758 for (i = 1; i <= 3; i++) {
1759 if (!(tmp & BIT_SCFG1[i - 1])) {
Jean Delvareb26f9332007-08-16 14:30:01 +02001760 data->sens[i - 1] = 4;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001761 } else {
1762 if (w83627hf_read_value
Jean Delvare787c72b2007-05-08 17:22:00 +02001763 (data,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001764 W83781D_REG_SCFG2) & BIT_SCFG2[i - 1])
1765 data->sens[i - 1] = 1;
1766 else
1767 data->sens[i - 1] = 2;
1768 }
1769 if ((type == w83697hf) && (i == 2))
1770 break;
1771 }
1772
1773 if(init) {
1774 /* Enable temp2 */
Jim Cromiedf48ed82007-10-14 17:10:52 -06001775 tmp = w83627hf_read_value(data, W83627HF_REG_TEMP2_CONFIG);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001776 if (tmp & 0x01) {
Jean Delvare787c72b2007-05-08 17:22:00 +02001777 dev_warn(&pdev->dev, "Enabling temp2, readings "
Linus Torvalds1da177e2005-04-16 15:20:36 -07001778 "might not make sense\n");
Jim Cromiedf48ed82007-10-14 17:10:52 -06001779 w83627hf_write_value(data, W83627HF_REG_TEMP2_CONFIG,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001780 tmp & 0xfe);
1781 }
1782
1783 /* Enable temp3 */
1784 if (type != w83697hf) {
Jean Delvare787c72b2007-05-08 17:22:00 +02001785 tmp = w83627hf_read_value(data,
Jim Cromiedf48ed82007-10-14 17:10:52 -06001786 W83627HF_REG_TEMP3_CONFIG);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001787 if (tmp & 0x01) {
Jean Delvare787c72b2007-05-08 17:22:00 +02001788 dev_warn(&pdev->dev, "Enabling temp3, "
Linus Torvalds1da177e2005-04-16 15:20:36 -07001789 "readings might not make sense\n");
Jean Delvare787c72b2007-05-08 17:22:00 +02001790 w83627hf_write_value(data,
Jim Cromiedf48ed82007-10-14 17:10:52 -06001791 W83627HF_REG_TEMP3_CONFIG, tmp & 0xfe);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001792 }
1793 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001794 }
1795
1796 /* Start monitoring */
Jean Delvare787c72b2007-05-08 17:22:00 +02001797 w83627hf_write_value(data, W83781D_REG_CONFIG,
1798 (w83627hf_read_value(data,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001799 W83781D_REG_CONFIG) & 0xf7)
1800 | 0x01);
Jean Delvareef878b12008-01-03 22:54:13 +01001801
1802 /* Enable VBAT monitoring if needed */
1803 tmp = w83627hf_read_value(data, W83781D_REG_VBAT);
1804 if (!(tmp & 0x01))
1805 w83627hf_write_value(data, W83781D_REG_VBAT, tmp | 0x01);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001806}
1807
Jean Delvarec09c5182007-10-12 21:53:07 +02001808static void w83627hf_update_fan_div(struct w83627hf_data *data)
1809{
1810 int reg;
1811
1812 reg = w83627hf_read_value(data, W83781D_REG_VID_FANDIV);
1813 data->fan_div[0] = (reg >> 4) & 0x03;
1814 data->fan_div[1] = (reg >> 6) & 0x03;
1815 if (data->type != w83697hf) {
1816 data->fan_div[2] = (w83627hf_read_value(data,
1817 W83781D_REG_PIN) >> 6) & 0x03;
1818 }
1819 reg = w83627hf_read_value(data, W83781D_REG_VBAT);
1820 data->fan_div[0] |= (reg >> 3) & 0x04;
1821 data->fan_div[1] |= (reg >> 4) & 0x04;
1822 if (data->type != w83697hf)
1823 data->fan_div[2] |= (reg >> 5) & 0x04;
1824}
1825
Linus Torvalds1da177e2005-04-16 15:20:36 -07001826static struct w83627hf_data *w83627hf_update_device(struct device *dev)
1827{
Jean Delvare787c72b2007-05-08 17:22:00 +02001828 struct w83627hf_data *data = dev_get_drvdata(dev);
Jim Cromiedf48ed82007-10-14 17:10:52 -06001829 int i, num_temps = (data->type == w83697hf) ? 2 : 3;
Dominik Geyera95a5ed2008-08-06 22:41:04 +02001830 int num_pwms = (data->type == w83697hf) ? 2 : 3;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001831
Ingo Molnar9a61bf62006-01-18 23:19:26 +01001832 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001833
1834 if (time_after(jiffies, data->last_updated + HZ + HZ / 2)
1835 || !data->valid) {
1836 for (i = 0; i <= 8; i++) {
1837 /* skip missing sensors */
1838 if (((data->type == w83697hf) && (i == 1)) ||
Jean Delvarec2db6ce2006-01-18 23:22:12 +01001839 ((data->type != w83627hf && data->type != w83697hf)
Yuan Mu4a1c44472005-11-07 22:19:04 +01001840 && (i == 5 || i == 6)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001841 continue;
1842 data->in[i] =
Jean Delvare787c72b2007-05-08 17:22:00 +02001843 w83627hf_read_value(data, W83781D_REG_IN(i));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001844 data->in_min[i] =
Jean Delvare787c72b2007-05-08 17:22:00 +02001845 w83627hf_read_value(data,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001846 W83781D_REG_IN_MIN(i));
1847 data->in_max[i] =
Jean Delvare787c72b2007-05-08 17:22:00 +02001848 w83627hf_read_value(data,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001849 W83781D_REG_IN_MAX(i));
1850 }
Jim Cromie2ca2fcd2007-10-14 17:20:50 -06001851 for (i = 0; i <= 2; i++) {
1852 data->fan[i] =
1853 w83627hf_read_value(data, W83627HF_REG_FAN(i));
1854 data->fan_min[i] =
Jean Delvare787c72b2007-05-08 17:22:00 +02001855 w83627hf_read_value(data,
Jim Cromie2ca2fcd2007-10-14 17:20:50 -06001856 W83627HF_REG_FAN_MIN(i));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001857 }
Jim Cromie07584c72007-10-12 21:08:00 +02001858 for (i = 0; i <= 2; i++) {
Jean Delvare787c72b2007-05-08 17:22:00 +02001859 u8 tmp = w83627hf_read_value(data,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001860 W836X7HF_REG_PWM(data->type, i));
1861 /* bits 0-3 are reserved in 627THF */
1862 if (data->type == w83627thf)
1863 tmp &= 0xf0;
Jim Cromie07584c72007-10-12 21:08:00 +02001864 data->pwm[i] = tmp;
1865 if (i == 1 &&
1866 (data->type == w83627hf || data->type == w83697hf))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001867 break;
1868 }
Carlos Olalla Martinez1550cb62007-06-09 10:11:16 -04001869 if (data->type == w83627hf) {
1870 u8 tmp = w83627hf_read_value(data,
1871 W83627HF_REG_PWM_FREQ);
1872 data->pwm_freq[0] = tmp & 0x07;
1873 data->pwm_freq[1] = (tmp >> 4) & 0x07;
1874 } else if (data->type != w83627thf) {
1875 for (i = 1; i <= 3; i++) {
1876 data->pwm_freq[i - 1] =
1877 w83627hf_read_value(data,
1878 W83637HF_REG_PWM_FREQ[i - 1]);
1879 if (i == 2 && (data->type == w83697hf))
1880 break;
1881 }
1882 }
Dominik Geyera95a5ed2008-08-06 22:41:04 +02001883 if (data->type != w83627hf) {
1884 for (i = 0; i < num_pwms; i++) {
1885 u8 tmp = w83627hf_read_value(data,
1886 W83627THF_REG_PWM_ENABLE[i]);
1887 data->pwm_enable[i] =
1888 ((tmp >> W83627THF_PWM_ENABLE_SHIFT[i])
1889 & 0x03) + 1;
1890 }
1891 }
Jim Cromiedf48ed82007-10-14 17:10:52 -06001892 for (i = 0; i < num_temps; i++) {
1893 data->temp[i] = w83627hf_read_value(
1894 data, w83627hf_reg_temp[i]);
1895 data->temp_max[i] = w83627hf_read_value(
1896 data, w83627hf_reg_temp_over[i]);
1897 data->temp_max_hyst[i] = w83627hf_read_value(
1898 data, w83627hf_reg_temp_hyst[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001899 }
1900
Jean Delvarec09c5182007-10-12 21:53:07 +02001901 w83627hf_update_fan_div(data);
1902
Linus Torvalds1da177e2005-04-16 15:20:36 -07001903 data->alarms =
Jean Delvare787c72b2007-05-08 17:22:00 +02001904 w83627hf_read_value(data, W83781D_REG_ALARM1) |
1905 (w83627hf_read_value(data, W83781D_REG_ALARM2) << 8) |
1906 (w83627hf_read_value(data, W83781D_REG_ALARM3) << 16);
1907 i = w83627hf_read_value(data, W83781D_REG_BEEP_INTS2);
Jean Delvare1c138102008-01-03 23:04:55 +01001908 data->beep_mask = (i << 8) |
Jean Delvare787c72b2007-05-08 17:22:00 +02001909 w83627hf_read_value(data, W83781D_REG_BEEP_INTS1) |
1910 w83627hf_read_value(data, W83781D_REG_BEEP_INTS3) << 16;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001911 data->last_updated = jiffies;
1912 data->valid = 1;
1913 }
1914
Ingo Molnar9a61bf62006-01-18 23:19:26 +01001915 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001916
1917 return data;
1918}
1919
Jean Delvare787c72b2007-05-08 17:22:00 +02001920static int __init w83627hf_device_add(unsigned short address,
1921 const struct w83627hf_sio_data *sio_data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001922{
Jean Delvare787c72b2007-05-08 17:22:00 +02001923 struct resource res = {
1924 .start = address + WINB_REGION_OFFSET,
1925 .end = address + WINB_REGION_OFFSET + WINB_REGION_SIZE - 1,
1926 .name = DRVNAME,
1927 .flags = IORESOURCE_IO,
1928 };
1929 int err;
1930
Jean Delvareb9acb642009-01-07 16:37:35 +01001931 err = acpi_check_resource_conflict(&res);
1932 if (err)
1933 goto exit;
1934
Jean Delvare787c72b2007-05-08 17:22:00 +02001935 pdev = platform_device_alloc(DRVNAME, address);
1936 if (!pdev) {
1937 err = -ENOMEM;
Joe Perches18de0302010-10-20 06:51:55 +00001938 pr_err("Device allocation failed\n");
Jean Delvare787c72b2007-05-08 17:22:00 +02001939 goto exit;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001940 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001941
Jean Delvare787c72b2007-05-08 17:22:00 +02001942 err = platform_device_add_resources(pdev, &res, 1);
1943 if (err) {
Joe Perches18de0302010-10-20 06:51:55 +00001944 pr_err("Device resource addition failed (%d)\n", err);
Jean Delvare787c72b2007-05-08 17:22:00 +02001945 goto exit_device_put;
1946 }
1947
Jean Delvare2df6d812007-06-09 10:11:16 -04001948 err = platform_device_add_data(pdev, sio_data,
1949 sizeof(struct w83627hf_sio_data));
1950 if (err) {
Joe Perches18de0302010-10-20 06:51:55 +00001951 pr_err("Platform data allocation failed\n");
Jean Delvare787c72b2007-05-08 17:22:00 +02001952 goto exit_device_put;
1953 }
Jean Delvare787c72b2007-05-08 17:22:00 +02001954
1955 err = platform_device_add(pdev);
1956 if (err) {
Joe Perches18de0302010-10-20 06:51:55 +00001957 pr_err("Device addition failed (%d)\n", err);
Jean Delvare787c72b2007-05-08 17:22:00 +02001958 goto exit_device_put;
1959 }
1960
1961 return 0;
1962
1963exit_device_put:
1964 platform_device_put(pdev);
1965exit:
1966 return err;
1967}
1968
1969static int __init sensors_w83627hf_init(void)
1970{
1971 int err;
1972 unsigned short address;
1973 struct w83627hf_sio_data sio_data;
1974
1975 if (w83627hf_find(0x2e, &address, &sio_data)
1976 && w83627hf_find(0x4e, &address, &sio_data))
1977 return -ENODEV;
1978
1979 err = platform_driver_register(&w83627hf_driver);
1980 if (err)
1981 goto exit;
1982
1983 /* Sets global pdev as a side effect */
1984 err = w83627hf_device_add(address, &sio_data);
1985 if (err)
1986 goto exit_driver;
1987
1988 return 0;
1989
1990exit_driver:
1991 platform_driver_unregister(&w83627hf_driver);
1992exit:
1993 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001994}
1995
1996static void __exit sensors_w83627hf_exit(void)
1997{
Jean Delvare787c72b2007-05-08 17:22:00 +02001998 platform_device_unregister(pdev);
1999 platform_driver_unregister(&w83627hf_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002000}
2001
2002MODULE_AUTHOR("Frodo Looijaard <frodol@dds.nl>, "
2003 "Philip Edelbrock <phil@netroedge.com>, "
2004 "and Mark Studebaker <mdsxyz123@yahoo.com>");
2005MODULE_DESCRIPTION("W83627HF driver");
2006MODULE_LICENSE("GPL");
2007
2008module_init(sensors_w83627hf_init);
2009module_exit(sensors_w83627hf_exit);