blob: 721295b9a05176704b156e7dd799ad17da5edd86 [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 Delvared27c37c2007-05-08 17:21:59 +0200477 .name = DRVNAME,
Jean Delvare275b7d62012-12-19 22:16:59 +0100478 .pm = W83627HF_DEV_PM_OPS,
Laurent Riffardcdaf7932005-11-26 20:37:41 +0100479 },
Jean Delvare787c72b2007-05-08 17:22:00 +0200480 .probe = w83627hf_probe,
Bill Pemberton9e5e9b72012-11-19 13:21:20 -0500481 .remove = w83627hf_remove,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482};
483
Jim Cromie07584c72007-10-12 21:08:00 +0200484static ssize_t
485show_in_input(struct device *dev, struct device_attribute *devattr, char *buf)
486{
487 int nr = to_sensor_dev_attr(devattr)->index;
488 struct w83627hf_data *data = w83627hf_update_device(dev);
489 return sprintf(buf, "%ld\n", (long)IN_FROM_REG(data->in[nr]));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490}
Jim Cromie07584c72007-10-12 21:08:00 +0200491static ssize_t
492show_in_min(struct device *dev, struct device_attribute *devattr, char *buf)
493{
494 int nr = to_sensor_dev_attr(devattr)->index;
495 struct w83627hf_data *data = w83627hf_update_device(dev);
496 return sprintf(buf, "%ld\n", (long)IN_FROM_REG(data->in_min[nr]));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497}
Jim Cromie07584c72007-10-12 21:08:00 +0200498static ssize_t
499show_in_max(struct device *dev, struct device_attribute *devattr, char *buf)
500{
501 int nr = to_sensor_dev_attr(devattr)->index;
502 struct w83627hf_data *data = w83627hf_update_device(dev);
503 return sprintf(buf, "%ld\n", (long)IN_FROM_REG(data->in_max[nr]));
504}
505static ssize_t
506store_in_min(struct device *dev, struct device_attribute *devattr,
507 const char *buf, size_t count)
508{
509 int nr = to_sensor_dev_attr(devattr)->index;
510 struct w83627hf_data *data = dev_get_drvdata(dev);
Guenter Roeck27b9de32012-01-15 11:07:26 -0800511 long val;
512 int err;
513
514 err = kstrtol(buf, 10, &val);
515 if (err)
516 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517
Jim Cromie07584c72007-10-12 21:08:00 +0200518 mutex_lock(&data->update_lock);
519 data->in_min[nr] = IN_TO_REG(val);
520 w83627hf_write_value(data, W83781D_REG_IN_MIN(nr), data->in_min[nr]);
521 mutex_unlock(&data->update_lock);
522 return count;
523}
524static ssize_t
525store_in_max(struct device *dev, struct device_attribute *devattr,
526 const char *buf, size_t count)
527{
528 int nr = to_sensor_dev_attr(devattr)->index;
529 struct w83627hf_data *data = dev_get_drvdata(dev);
Guenter Roeck27b9de32012-01-15 11:07:26 -0800530 long val;
531 int err;
532
533 err = kstrtol(buf, 10, &val);
534 if (err)
535 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536
Jim Cromie07584c72007-10-12 21:08:00 +0200537 mutex_lock(&data->update_lock);
538 data->in_max[nr] = IN_TO_REG(val);
539 w83627hf_write_value(data, W83781D_REG_IN_MAX(nr), data->in_max[nr]);
540 mutex_unlock(&data->update_lock);
541 return count;
542}
543#define sysfs_vin_decl(offset) \
544static SENSOR_DEVICE_ATTR(in##offset##_input, S_IRUGO, \
545 show_in_input, NULL, offset); \
546static SENSOR_DEVICE_ATTR(in##offset##_min, S_IRUGO|S_IWUSR, \
547 show_in_min, store_in_min, offset); \
548static SENSOR_DEVICE_ATTR(in##offset##_max, S_IRUGO|S_IWUSR, \
549 show_in_max, store_in_max, offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550
Jim Cromie07584c72007-10-12 21:08:00 +0200551sysfs_vin_decl(1);
552sysfs_vin_decl(2);
553sysfs_vin_decl(3);
554sysfs_vin_decl(4);
555sysfs_vin_decl(5);
556sysfs_vin_decl(6);
557sysfs_vin_decl(7);
558sysfs_vin_decl(8);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559
560/* use a different set of functions for in0 */
561static ssize_t show_in_0(struct w83627hf_data *data, char *buf, u8 reg)
562{
563 long in0;
564
565 if ((data->vrm_ovt & 0x01) &&
Jean Delvarec2db6ce2006-01-18 23:22:12 +0100566 (w83627thf == data->type || w83637hf == data->type
567 || w83687thf == data->type))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568
569 /* use VRM9 calculation */
570 in0 = (long)((reg * 488 + 70000 + 50) / 100);
571 else
572 /* use VRM8 (standard) calculation */
573 in0 = (long)IN_FROM_REG(reg);
574
575 return sprintf(buf,"%ld\n", in0);
576}
577
Yani Ioannoua5099cf2005-05-17 06:42:25 -0400578static ssize_t show_regs_in_0(struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579{
580 struct w83627hf_data *data = w83627hf_update_device(dev);
581 return show_in_0(data, buf, data->in[0]);
582}
583
Yani Ioannoua5099cf2005-05-17 06:42:25 -0400584static ssize_t show_regs_in_min0(struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585{
586 struct w83627hf_data *data = w83627hf_update_device(dev);
587 return show_in_0(data, buf, data->in_min[0]);
588}
589
Yani Ioannoua5099cf2005-05-17 06:42:25 -0400590static ssize_t show_regs_in_max0(struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591{
592 struct w83627hf_data *data = w83627hf_update_device(dev);
593 return show_in_0(data, buf, data->in_max[0]);
594}
595
Yani Ioannoua5099cf2005-05-17 06:42:25 -0400596static ssize_t store_regs_in_min0(struct device *dev, struct device_attribute *attr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597 const char *buf, size_t count)
598{
Jean Delvare787c72b2007-05-08 17:22:00 +0200599 struct w83627hf_data *data = dev_get_drvdata(dev);
Guenter Roeck27b9de32012-01-15 11:07:26 -0800600 unsigned long val;
601 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602
Guenter Roeck27b9de32012-01-15 11:07:26 -0800603 err = kstrtoul(buf, 10, &val);
604 if (err)
605 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100607 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608
609 if ((data->vrm_ovt & 0x01) &&
Jean Delvarec2db6ce2006-01-18 23:22:12 +0100610 (w83627thf == data->type || w83637hf == data->type
611 || w83687thf == data->type))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612
613 /* use VRM9 calculation */
Yuan Mu2723ab92005-11-23 15:44:21 -0800614 data->in_min[0] =
Guenter Roeck2a844c12013-01-09 08:09:34 -0800615 clamp_val(((val * 100) - 70000 + 244) / 488, 0, 255);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616 else
617 /* use VRM8 (standard) calculation */
618 data->in_min[0] = IN_TO_REG(val);
619
Jean Delvare787c72b2007-05-08 17:22:00 +0200620 w83627hf_write_value(data, W83781D_REG_IN_MIN(0), data->in_min[0]);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100621 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622 return count;
623}
624
Yani Ioannoua5099cf2005-05-17 06:42:25 -0400625static ssize_t store_regs_in_max0(struct device *dev, struct device_attribute *attr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626 const char *buf, size_t count)
627{
Jean Delvare787c72b2007-05-08 17:22:00 +0200628 struct w83627hf_data *data = dev_get_drvdata(dev);
Guenter Roeck27b9de32012-01-15 11:07:26 -0800629 unsigned long val;
630 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631
Guenter Roeck27b9de32012-01-15 11:07:26 -0800632 err = kstrtoul(buf, 10, &val);
633 if (err)
634 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100636 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637
638 if ((data->vrm_ovt & 0x01) &&
Jean Delvarec2db6ce2006-01-18 23:22:12 +0100639 (w83627thf == data->type || w83637hf == data->type
640 || w83687thf == data->type))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641
642 /* use VRM9 calculation */
Yuan Mu2723ab92005-11-23 15:44:21 -0800643 data->in_max[0] =
Guenter Roeck2a844c12013-01-09 08:09:34 -0800644 clamp_val(((val * 100) - 70000 + 244) / 488, 0, 255);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645 else
646 /* use VRM8 (standard) calculation */
647 data->in_max[0] = IN_TO_REG(val);
648
Jean Delvare787c72b2007-05-08 17:22:00 +0200649 w83627hf_write_value(data, W83781D_REG_IN_MAX(0), data->in_max[0]);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100650 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651 return count;
652}
653
654static DEVICE_ATTR(in0_input, S_IRUGO, show_regs_in_0, NULL);
655static DEVICE_ATTR(in0_min, S_IRUGO | S_IWUSR,
656 show_regs_in_min0, store_regs_in_min0);
657static DEVICE_ATTR(in0_max, S_IRUGO | S_IWUSR,
658 show_regs_in_max0, store_regs_in_max0);
659
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660static ssize_t
Jim Cromie07584c72007-10-12 21:08:00 +0200661show_fan_input(struct device *dev, struct device_attribute *devattr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662{
Jim Cromie07584c72007-10-12 21:08:00 +0200663 int nr = to_sensor_dev_attr(devattr)->index;
664 struct w83627hf_data *data = w83627hf_update_device(dev);
665 return sprintf(buf, "%ld\n", FAN_FROM_REG(data->fan[nr],
666 (long)DIV_FROM_REG(data->fan_div[nr])));
667}
668static ssize_t
669show_fan_min(struct device *dev, struct device_attribute *devattr, char *buf)
670{
671 int nr = to_sensor_dev_attr(devattr)->index;
672 struct w83627hf_data *data = w83627hf_update_device(dev);
673 return sprintf(buf, "%ld\n", FAN_FROM_REG(data->fan_min[nr],
674 (long)DIV_FROM_REG(data->fan_div[nr])));
675}
676static ssize_t
677store_fan_min(struct device *dev, struct device_attribute *devattr,
678 const char *buf, size_t count)
679{
680 int nr = to_sensor_dev_attr(devattr)->index;
Jean Delvare787c72b2007-05-08 17:22:00 +0200681 struct w83627hf_data *data = dev_get_drvdata(dev);
Guenter Roeck27b9de32012-01-15 11:07:26 -0800682 unsigned long val;
683 int err;
684
685 err = kstrtoul(buf, 10, &val);
686 if (err)
687 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100689 mutex_lock(&data->update_lock);
Jim Cromie07584c72007-10-12 21:08:00 +0200690 data->fan_min[nr] = FAN_TO_REG(val, DIV_FROM_REG(data->fan_div[nr]));
Jim Cromie2ca2fcd2007-10-14 17:20:50 -0600691 w83627hf_write_value(data, W83627HF_REG_FAN_MIN(nr),
Jim Cromie07584c72007-10-12 21:08:00 +0200692 data->fan_min[nr]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100694 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695 return count;
696}
Jim Cromie07584c72007-10-12 21:08:00 +0200697#define sysfs_fan_decl(offset) \
698static SENSOR_DEVICE_ATTR(fan##offset##_input, S_IRUGO, \
699 show_fan_input, NULL, offset - 1); \
700static SENSOR_DEVICE_ATTR(fan##offset##_min, S_IRUGO | S_IWUSR, \
701 show_fan_min, store_fan_min, offset - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702
Jim Cromie07584c72007-10-12 21:08:00 +0200703sysfs_fan_decl(1);
704sysfs_fan_decl(2);
705sysfs_fan_decl(3);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706
Jim Cromie07584c72007-10-12 21:08:00 +0200707static ssize_t
708show_temp(struct device *dev, struct device_attribute *devattr, char *buf)
709{
710 int nr = to_sensor_dev_attr(devattr)->index;
711 struct w83627hf_data *data = w83627hf_update_device(dev);
Jim Cromiedf48ed82007-10-14 17:10:52 -0600712
713 u16 tmp = data->temp[nr];
714 return sprintf(buf, "%ld\n", (nr) ? (long) LM75_TEMP_FROM_REG(tmp)
715 : (long) TEMP_FROM_REG(tmp));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717
Jim Cromie07584c72007-10-12 21:08:00 +0200718static ssize_t
719show_temp_max(struct device *dev, struct device_attribute *devattr,
720 char *buf)
721{
722 int nr = to_sensor_dev_attr(devattr)->index;
723 struct w83627hf_data *data = w83627hf_update_device(dev);
Jim Cromiedf48ed82007-10-14 17:10:52 -0600724
725 u16 tmp = data->temp_max[nr];
726 return sprintf(buf, "%ld\n", (nr) ? (long) LM75_TEMP_FROM_REG(tmp)
727 : (long) TEMP_FROM_REG(tmp));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729
Jim Cromie07584c72007-10-12 21:08:00 +0200730static ssize_t
731show_temp_max_hyst(struct device *dev, struct device_attribute *devattr,
732 char *buf)
733{
734 int nr = to_sensor_dev_attr(devattr)->index;
735 struct w83627hf_data *data = w83627hf_update_device(dev);
Jim Cromiedf48ed82007-10-14 17:10:52 -0600736
737 u16 tmp = data->temp_max_hyst[nr];
738 return sprintf(buf, "%ld\n", (nr) ? (long) LM75_TEMP_FROM_REG(tmp)
739 : (long) TEMP_FROM_REG(tmp));
Jim Cromie07584c72007-10-12 21:08:00 +0200740}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741
Jim Cromie07584c72007-10-12 21:08:00 +0200742static ssize_t
743store_temp_max(struct device *dev, struct device_attribute *devattr,
744 const char *buf, size_t count)
745{
746 int nr = to_sensor_dev_attr(devattr)->index;
747 struct w83627hf_data *data = dev_get_drvdata(dev);
Guenter Roeck27b9de32012-01-15 11:07:26 -0800748 u16 tmp;
749 long val;
750 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751
Guenter Roeck27b9de32012-01-15 11:07:26 -0800752 err = kstrtol(buf, 10, &val);
753 if (err)
754 return err;
755
756 tmp = (nr) ? LM75_TEMP_TO_REG(val) : TEMP_TO_REG(val);
Jim Cromie07584c72007-10-12 21:08:00 +0200757 mutex_lock(&data->update_lock);
Jim Cromiedf48ed82007-10-14 17:10:52 -0600758 data->temp_max[nr] = tmp;
759 w83627hf_write_value(data, w83627hf_reg_temp_over[nr], tmp);
Jim Cromie07584c72007-10-12 21:08:00 +0200760 mutex_unlock(&data->update_lock);
761 return count;
762}
763
764static ssize_t
765store_temp_max_hyst(struct device *dev, struct device_attribute *devattr,
766 const char *buf, size_t count)
767{
768 int nr = to_sensor_dev_attr(devattr)->index;
769 struct w83627hf_data *data = dev_get_drvdata(dev);
Guenter Roeck27b9de32012-01-15 11:07:26 -0800770 u16 tmp;
771 long val;
772 int err;
Jim Cromie07584c72007-10-12 21:08:00 +0200773
Guenter Roeck27b9de32012-01-15 11:07:26 -0800774 err = kstrtol(buf, 10, &val);
775 if (err)
776 return err;
777
778 tmp = (nr) ? LM75_TEMP_TO_REG(val) : TEMP_TO_REG(val);
Jim Cromie07584c72007-10-12 21:08:00 +0200779 mutex_lock(&data->update_lock);
Jim Cromiedf48ed82007-10-14 17:10:52 -0600780 data->temp_max_hyst[nr] = tmp;
781 w83627hf_write_value(data, w83627hf_reg_temp_hyst[nr], tmp);
Jim Cromie07584c72007-10-12 21:08:00 +0200782 mutex_unlock(&data->update_lock);
783 return count;
784}
785
786#define sysfs_temp_decl(offset) \
787static SENSOR_DEVICE_ATTR(temp##offset##_input, S_IRUGO, \
Jim Cromiedf48ed82007-10-14 17:10:52 -0600788 show_temp, NULL, offset - 1); \
Jim Cromie07584c72007-10-12 21:08:00 +0200789static SENSOR_DEVICE_ATTR(temp##offset##_max, S_IRUGO|S_IWUSR, \
Jim Cromiedf48ed82007-10-14 17:10:52 -0600790 show_temp_max, store_temp_max, offset - 1); \
Jim Cromie07584c72007-10-12 21:08:00 +0200791static SENSOR_DEVICE_ATTR(temp##offset##_max_hyst, S_IRUGO|S_IWUSR, \
Jim Cromiedf48ed82007-10-14 17:10:52 -0600792 show_temp_max_hyst, store_temp_max_hyst, offset - 1);
Jim Cromie07584c72007-10-12 21:08:00 +0200793
794sysfs_temp_decl(1);
795sysfs_temp_decl(2);
796sysfs_temp_decl(3);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798static ssize_t
Yani Ioannoua5099cf2005-05-17 06:42:25 -0400799show_vid_reg(struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800{
801 struct w83627hf_data *data = w83627hf_update_device(dev);
802 return sprintf(buf, "%ld\n", (long) vid_from_reg(data->vid, data->vrm));
803}
804static DEVICE_ATTR(cpu0_vid, S_IRUGO, show_vid_reg, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805
806static ssize_t
Yani Ioannoua5099cf2005-05-17 06:42:25 -0400807show_vrm_reg(struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808{
Jean Delvare90d66192007-10-08 18:24:35 +0200809 struct w83627hf_data *data = dev_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810 return sprintf(buf, "%ld\n", (long) data->vrm);
811}
812static ssize_t
Yani Ioannoua5099cf2005-05-17 06:42:25 -0400813store_vrm_reg(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814{
Jean Delvare787c72b2007-05-08 17:22:00 +0200815 struct w83627hf_data *data = dev_get_drvdata(dev);
Guenter Roeck27b9de32012-01-15 11:07:26 -0800816 unsigned long val;
817 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818
Guenter Roeck27b9de32012-01-15 11:07:26 -0800819 err = kstrtoul(buf, 10, &val);
820 if (err)
821 return err;
Axel Lin970255b2014-08-06 08:27:11 +0800822
823 if (val > 255)
824 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825 data->vrm = val;
826
827 return count;
828}
829static DEVICE_ATTR(vrm, S_IRUGO | S_IWUSR, show_vrm_reg, store_vrm_reg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830
831static ssize_t
Yani Ioannoua5099cf2005-05-17 06:42:25 -0400832show_alarms_reg(struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833{
834 struct w83627hf_data *data = w83627hf_update_device(dev);
835 return sprintf(buf, "%ld\n", (long) data->alarms);
836}
837static DEVICE_ATTR(alarms, S_IRUGO, show_alarms_reg, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838
Jean Delvaree3604c62008-01-03 23:00:30 +0100839static ssize_t
840show_alarm(struct device *dev, struct device_attribute *attr, char *buf)
841{
842 struct w83627hf_data *data = w83627hf_update_device(dev);
843 int bitnr = to_sensor_dev_attr(attr)->index;
844 return sprintf(buf, "%u\n", (data->alarms >> bitnr) & 1);
845}
846static SENSOR_DEVICE_ATTR(in0_alarm, S_IRUGO, show_alarm, NULL, 0);
847static SENSOR_DEVICE_ATTR(in1_alarm, S_IRUGO, show_alarm, NULL, 1);
848static SENSOR_DEVICE_ATTR(in2_alarm, S_IRUGO, show_alarm, NULL, 2);
849static SENSOR_DEVICE_ATTR(in3_alarm, S_IRUGO, show_alarm, NULL, 3);
850static SENSOR_DEVICE_ATTR(in4_alarm, S_IRUGO, show_alarm, NULL, 8);
851static SENSOR_DEVICE_ATTR(in5_alarm, S_IRUGO, show_alarm, NULL, 9);
852static SENSOR_DEVICE_ATTR(in6_alarm, S_IRUGO, show_alarm, NULL, 10);
853static SENSOR_DEVICE_ATTR(in7_alarm, S_IRUGO, show_alarm, NULL, 16);
854static SENSOR_DEVICE_ATTR(in8_alarm, S_IRUGO, show_alarm, NULL, 17);
855static SENSOR_DEVICE_ATTR(fan1_alarm, S_IRUGO, show_alarm, NULL, 6);
856static SENSOR_DEVICE_ATTR(fan2_alarm, S_IRUGO, show_alarm, NULL, 7);
857static SENSOR_DEVICE_ATTR(fan3_alarm, S_IRUGO, show_alarm, NULL, 11);
858static SENSOR_DEVICE_ATTR(temp1_alarm, S_IRUGO, show_alarm, NULL, 4);
859static SENSOR_DEVICE_ATTR(temp2_alarm, S_IRUGO, show_alarm, NULL, 5);
860static SENSOR_DEVICE_ATTR(temp3_alarm, S_IRUGO, show_alarm, NULL, 13);
861
Jean Delvare1c138102008-01-03 23:04:55 +0100862static ssize_t
863show_beep_mask(struct device *dev, struct device_attribute *attr, char *buf)
864{
865 struct w83627hf_data *data = w83627hf_update_device(dev);
866 return sprintf(buf, "%ld\n",
867 (long)BEEP_MASK_FROM_REG(data->beep_mask));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869
870static ssize_t
Jean Delvare1c138102008-01-03 23:04:55 +0100871store_beep_mask(struct device *dev, struct device_attribute *attr,
872 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873{
Jean Delvare787c72b2007-05-08 17:22:00 +0200874 struct w83627hf_data *data = dev_get_drvdata(dev);
Jean Delvare1c138102008-01-03 23:04:55 +0100875 unsigned long val;
Guenter Roeck27b9de32012-01-15 11:07:26 -0800876 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877
Guenter Roeck27b9de32012-01-15 11:07:26 -0800878 err = kstrtoul(buf, 10, &val);
879 if (err)
880 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100882 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883
Jean Delvare1c138102008-01-03 23:04:55 +0100884 /* preserve beep enable */
885 data->beep_mask = (data->beep_mask & 0x8000)
886 | BEEP_MASK_TO_REG(val);
887 w83627hf_write_value(data, W83781D_REG_BEEP_INTS1,
888 data->beep_mask & 0xff);
889 w83627hf_write_value(data, W83781D_REG_BEEP_INTS3,
890 ((data->beep_mask) >> 16) & 0xff);
Jean Delvare787c72b2007-05-08 17:22:00 +0200891 w83627hf_write_value(data, W83781D_REG_BEEP_INTS2,
Jean Delvare1c138102008-01-03 23:04:55 +0100892 (data->beep_mask >> 8) & 0xff);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100894 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895 return count;
896}
897
Jean Delvare1c138102008-01-03 23:04:55 +0100898static DEVICE_ATTR(beep_mask, S_IRUGO | S_IWUSR,
899 show_beep_mask, store_beep_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901static ssize_t
Jean Delvaree3604c62008-01-03 23:00:30 +0100902show_beep(struct device *dev, struct device_attribute *attr, char *buf)
903{
904 struct w83627hf_data *data = w83627hf_update_device(dev);
905 int bitnr = to_sensor_dev_attr(attr)->index;
906 return sprintf(buf, "%u\n", (data->beep_mask >> bitnr) & 1);
907}
908
909static ssize_t
910store_beep(struct device *dev, struct device_attribute *attr,
911 const char *buf, size_t count)
912{
913 struct w83627hf_data *data = dev_get_drvdata(dev);
914 int bitnr = to_sensor_dev_attr(attr)->index;
Jean Delvaree3604c62008-01-03 23:00:30 +0100915 u8 reg;
Guenter Roeck27b9de32012-01-15 11:07:26 -0800916 unsigned long bit;
917 int err;
Jean Delvaree3604c62008-01-03 23:00:30 +0100918
Guenter Roeck27b9de32012-01-15 11:07:26 -0800919 err = kstrtoul(buf, 10, &bit);
920 if (err)
921 return err;
922
Jean Delvaree3604c62008-01-03 23:00:30 +0100923 if (bit & ~1)
924 return -EINVAL;
925
926 mutex_lock(&data->update_lock);
927 if (bit)
928 data->beep_mask |= (1 << bitnr);
929 else
930 data->beep_mask &= ~(1 << bitnr);
931
932 if (bitnr < 8) {
933 reg = w83627hf_read_value(data, W83781D_REG_BEEP_INTS1);
934 if (bit)
935 reg |= (1 << bitnr);
936 else
937 reg &= ~(1 << bitnr);
938 w83627hf_write_value(data, W83781D_REG_BEEP_INTS1, reg);
939 } else if (bitnr < 16) {
940 reg = w83627hf_read_value(data, W83781D_REG_BEEP_INTS2);
941 if (bit)
942 reg |= (1 << (bitnr - 8));
943 else
944 reg &= ~(1 << (bitnr - 8));
945 w83627hf_write_value(data, W83781D_REG_BEEP_INTS2, reg);
946 } else {
947 reg = w83627hf_read_value(data, W83781D_REG_BEEP_INTS3);
948 if (bit)
949 reg |= (1 << (bitnr - 16));
950 else
951 reg &= ~(1 << (bitnr - 16));
952 w83627hf_write_value(data, W83781D_REG_BEEP_INTS3, reg);
953 }
954 mutex_unlock(&data->update_lock);
955
956 return count;
957}
958
959static SENSOR_DEVICE_ATTR(in0_beep, S_IRUGO | S_IWUSR,
960 show_beep, store_beep, 0);
961static SENSOR_DEVICE_ATTR(in1_beep, S_IRUGO | S_IWUSR,
962 show_beep, store_beep, 1);
963static SENSOR_DEVICE_ATTR(in2_beep, S_IRUGO | S_IWUSR,
964 show_beep, store_beep, 2);
965static SENSOR_DEVICE_ATTR(in3_beep, S_IRUGO | S_IWUSR,
966 show_beep, store_beep, 3);
967static SENSOR_DEVICE_ATTR(in4_beep, S_IRUGO | S_IWUSR,
968 show_beep, store_beep, 8);
969static SENSOR_DEVICE_ATTR(in5_beep, S_IRUGO | S_IWUSR,
970 show_beep, store_beep, 9);
971static SENSOR_DEVICE_ATTR(in6_beep, S_IRUGO | S_IWUSR,
972 show_beep, store_beep, 10);
973static SENSOR_DEVICE_ATTR(in7_beep, S_IRUGO | S_IWUSR,
974 show_beep, store_beep, 16);
975static SENSOR_DEVICE_ATTR(in8_beep, S_IRUGO | S_IWUSR,
976 show_beep, store_beep, 17);
977static SENSOR_DEVICE_ATTR(fan1_beep, S_IRUGO | S_IWUSR,
978 show_beep, store_beep, 6);
979static SENSOR_DEVICE_ATTR(fan2_beep, S_IRUGO | S_IWUSR,
980 show_beep, store_beep, 7);
981static SENSOR_DEVICE_ATTR(fan3_beep, S_IRUGO | S_IWUSR,
982 show_beep, store_beep, 11);
983static SENSOR_DEVICE_ATTR(temp1_beep, S_IRUGO | S_IWUSR,
984 show_beep, store_beep, 4);
985static SENSOR_DEVICE_ATTR(temp2_beep, S_IRUGO | S_IWUSR,
986 show_beep, store_beep, 5);
987static SENSOR_DEVICE_ATTR(temp3_beep, S_IRUGO | S_IWUSR,
988 show_beep, store_beep, 13);
Jean Delvare1c138102008-01-03 23:04:55 +0100989static SENSOR_DEVICE_ATTR(beep_enable, S_IRUGO | S_IWUSR,
990 show_beep, store_beep, 15);
Jean Delvaree3604c62008-01-03 23:00:30 +0100991
992static ssize_t
Jim Cromie07584c72007-10-12 21:08:00 +0200993show_fan_div(struct device *dev, struct device_attribute *devattr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994{
Jim Cromie07584c72007-10-12 21:08:00 +0200995 int nr = to_sensor_dev_attr(devattr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700996 struct w83627hf_data *data = w83627hf_update_device(dev);
997 return sprintf(buf, "%ld\n",
Jim Cromie07584c72007-10-12 21:08:00 +0200998 (long) DIV_FROM_REG(data->fan_div[nr]));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700999}
Guenter Roeck27b9de32012-01-15 11:07:26 -08001000/*
1001 * Note: we save and restore the fan minimum here, because its value is
1002 * determined in part by the fan divisor. This follows the principle of
1003 * least surprise; the user doesn't expect the fan minimum to change just
1004 * because the divisor changed.
1005 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001006static ssize_t
Jim Cromie07584c72007-10-12 21:08:00 +02001007store_fan_div(struct device *dev, struct device_attribute *devattr,
1008 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001009{
Jim Cromie07584c72007-10-12 21:08:00 +02001010 int nr = to_sensor_dev_attr(devattr)->index;
Jean Delvare787c72b2007-05-08 17:22:00 +02001011 struct w83627hf_data *data = dev_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001012 unsigned long min;
1013 u8 reg;
Guenter Roeck27b9de32012-01-15 11:07:26 -08001014 unsigned long val;
1015 int err;
1016
1017 err = kstrtoul(buf, 10, &val);
1018 if (err)
1019 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020
Ingo Molnar9a61bf62006-01-18 23:19:26 +01001021 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001022
1023 /* Save fan_min */
1024 min = FAN_FROM_REG(data->fan_min[nr],
1025 DIV_FROM_REG(data->fan_div[nr]));
1026
1027 data->fan_div[nr] = DIV_TO_REG(val);
1028
Jean Delvare787c72b2007-05-08 17:22:00 +02001029 reg = (w83627hf_read_value(data, nr==2 ? W83781D_REG_PIN : W83781D_REG_VID_FANDIV)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030 & (nr==0 ? 0xcf : 0x3f))
1031 | ((data->fan_div[nr] & 0x03) << (nr==0 ? 4 : 6));
Jean Delvare787c72b2007-05-08 17:22:00 +02001032 w83627hf_write_value(data, nr==2 ? W83781D_REG_PIN : W83781D_REG_VID_FANDIV, reg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033
Jean Delvare787c72b2007-05-08 17:22:00 +02001034 reg = (w83627hf_read_value(data, W83781D_REG_VBAT)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001035 & ~(1 << (5 + nr)))
1036 | ((data->fan_div[nr] & 0x04) << (3 + nr));
Jean Delvare787c72b2007-05-08 17:22:00 +02001037 w83627hf_write_value(data, W83781D_REG_VBAT, reg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001038
1039 /* Restore fan_min */
1040 data->fan_min[nr] = FAN_TO_REG(min, DIV_FROM_REG(data->fan_div[nr]));
Jim Cromie2ca2fcd2007-10-14 17:20:50 -06001041 w83627hf_write_value(data, W83627HF_REG_FAN_MIN(nr), data->fan_min[nr]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042
Ingo Molnar9a61bf62006-01-18 23:19:26 +01001043 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001044 return count;
1045}
1046
Jim Cromie07584c72007-10-12 21:08:00 +02001047static SENSOR_DEVICE_ATTR(fan1_div, S_IRUGO|S_IWUSR,
1048 show_fan_div, store_fan_div, 0);
1049static SENSOR_DEVICE_ATTR(fan2_div, S_IRUGO|S_IWUSR,
1050 show_fan_div, store_fan_div, 1);
1051static SENSOR_DEVICE_ATTR(fan3_div, S_IRUGO|S_IWUSR,
1052 show_fan_div, store_fan_div, 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001053
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054static ssize_t
Jim Cromie07584c72007-10-12 21:08:00 +02001055show_pwm(struct device *dev, struct device_attribute *devattr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056{
Jim Cromie07584c72007-10-12 21:08:00 +02001057 int nr = to_sensor_dev_attr(devattr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001058 struct w83627hf_data *data = w83627hf_update_device(dev);
Jim Cromie07584c72007-10-12 21:08:00 +02001059 return sprintf(buf, "%ld\n", (long) data->pwm[nr]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001060}
1061
1062static ssize_t
Jim Cromie07584c72007-10-12 21:08:00 +02001063store_pwm(struct device *dev, struct device_attribute *devattr,
1064 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001065{
Jim Cromie07584c72007-10-12 21:08:00 +02001066 int nr = to_sensor_dev_attr(devattr)->index;
Jean Delvare787c72b2007-05-08 17:22:00 +02001067 struct w83627hf_data *data = dev_get_drvdata(dev);
Guenter Roeck27b9de32012-01-15 11:07:26 -08001068 unsigned long val;
1069 int err;
1070
1071 err = kstrtoul(buf, 10, &val);
1072 if (err)
1073 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001074
Ingo Molnar9a61bf62006-01-18 23:19:26 +01001075 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001076
1077 if (data->type == w83627thf) {
1078 /* bits 0-3 are reserved in 627THF */
Jim Cromie07584c72007-10-12 21:08:00 +02001079 data->pwm[nr] = PWM_TO_REG(val) & 0xf0;
Jean Delvare787c72b2007-05-08 17:22:00 +02001080 w83627hf_write_value(data,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001081 W836X7HF_REG_PWM(data->type, nr),
Jim Cromie07584c72007-10-12 21:08:00 +02001082 data->pwm[nr] |
Jean Delvare787c72b2007-05-08 17:22:00 +02001083 (w83627hf_read_value(data,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001084 W836X7HF_REG_PWM(data->type, nr)) & 0x0f));
1085 } else {
Jim Cromie07584c72007-10-12 21:08:00 +02001086 data->pwm[nr] = PWM_TO_REG(val);
Jean Delvare787c72b2007-05-08 17:22:00 +02001087 w83627hf_write_value(data,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001088 W836X7HF_REG_PWM(data->type, nr),
Jim Cromie07584c72007-10-12 21:08:00 +02001089 data->pwm[nr]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001090 }
1091
Ingo Molnar9a61bf62006-01-18 23:19:26 +01001092 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001093 return count;
1094}
1095
Jim Cromie07584c72007-10-12 21:08:00 +02001096static SENSOR_DEVICE_ATTR(pwm1, S_IRUGO|S_IWUSR, show_pwm, store_pwm, 0);
1097static SENSOR_DEVICE_ATTR(pwm2, S_IRUGO|S_IWUSR, show_pwm, store_pwm, 1);
1098static SENSOR_DEVICE_ATTR(pwm3, S_IRUGO|S_IWUSR, show_pwm, store_pwm, 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001099
Linus Torvalds1da177e2005-04-16 15:20:36 -07001100static ssize_t
Dominik Geyera95a5ed2008-08-06 22:41:04 +02001101show_pwm_enable(struct device *dev, struct device_attribute *devattr, char *buf)
1102{
1103 int nr = to_sensor_dev_attr(devattr)->index;
1104 struct w83627hf_data *data = w83627hf_update_device(dev);
1105 return sprintf(buf, "%d\n", data->pwm_enable[nr]);
1106}
1107
1108static ssize_t
1109store_pwm_enable(struct device *dev, struct device_attribute *devattr,
1110 const char *buf, size_t count)
1111{
1112 int nr = to_sensor_dev_attr(devattr)->index;
1113 struct w83627hf_data *data = dev_get_drvdata(dev);
Dominik Geyera95a5ed2008-08-06 22:41:04 +02001114 u8 reg;
Guenter Roeck27b9de32012-01-15 11:07:26 -08001115 unsigned long val;
1116 int err;
Dominik Geyera95a5ed2008-08-06 22:41:04 +02001117
Guenter Roeck27b9de32012-01-15 11:07:26 -08001118 err = kstrtoul(buf, 10, &val);
1119 if (err)
1120 return err;
1121
1122 if (!val || val > 3) /* modes 1, 2 and 3 are supported */
Dominik Geyera95a5ed2008-08-06 22:41:04 +02001123 return -EINVAL;
1124 mutex_lock(&data->update_lock);
1125 data->pwm_enable[nr] = val;
1126 reg = w83627hf_read_value(data, W83627THF_REG_PWM_ENABLE[nr]);
1127 reg &= ~(0x03 << W83627THF_PWM_ENABLE_SHIFT[nr]);
1128 reg |= (val - 1) << W83627THF_PWM_ENABLE_SHIFT[nr];
1129 w83627hf_write_value(data, W83627THF_REG_PWM_ENABLE[nr], reg);
1130 mutex_unlock(&data->update_lock);
1131 return count;
1132}
1133
1134static SENSOR_DEVICE_ATTR(pwm1_enable, S_IRUGO|S_IWUSR, show_pwm_enable,
1135 store_pwm_enable, 0);
1136static SENSOR_DEVICE_ATTR(pwm2_enable, S_IRUGO|S_IWUSR, show_pwm_enable,
1137 store_pwm_enable, 1);
1138static SENSOR_DEVICE_ATTR(pwm3_enable, S_IRUGO|S_IWUSR, show_pwm_enable,
1139 store_pwm_enable, 2);
1140
1141static ssize_t
Jim Cromie07584c72007-10-12 21:08:00 +02001142show_pwm_freq(struct device *dev, struct device_attribute *devattr, char *buf)
Carlos Olalla Martinez1550cb62007-06-09 10:11:16 -04001143{
Jim Cromie07584c72007-10-12 21:08:00 +02001144 int nr = to_sensor_dev_attr(devattr)->index;
Carlos Olalla Martinez1550cb62007-06-09 10:11:16 -04001145 struct w83627hf_data *data = w83627hf_update_device(dev);
1146 if (data->type == w83627hf)
1147 return sprintf(buf, "%ld\n",
Jim Cromie07584c72007-10-12 21:08:00 +02001148 pwm_freq_from_reg_627hf(data->pwm_freq[nr]));
Carlos Olalla Martinez1550cb62007-06-09 10:11:16 -04001149 else
1150 return sprintf(buf, "%ld\n",
Jim Cromie07584c72007-10-12 21:08:00 +02001151 pwm_freq_from_reg(data->pwm_freq[nr]));
Carlos Olalla Martinez1550cb62007-06-09 10:11:16 -04001152}
1153
1154static ssize_t
Jim Cromie07584c72007-10-12 21:08:00 +02001155store_pwm_freq(struct device *dev, struct device_attribute *devattr,
1156 const char *buf, size_t count)
Carlos Olalla Martinez1550cb62007-06-09 10:11:16 -04001157{
Jim Cromie07584c72007-10-12 21:08:00 +02001158 int nr = to_sensor_dev_attr(devattr)->index;
Carlos Olalla Martinez1550cb62007-06-09 10:11:16 -04001159 struct w83627hf_data *data = dev_get_drvdata(dev);
1160 static const u8 mask[]={0xF8, 0x8F};
Guenter Roeck27b9de32012-01-15 11:07:26 -08001161 unsigned long val;
1162 int err;
Carlos Olalla Martinez1550cb62007-06-09 10:11:16 -04001163
Guenter Roeck27b9de32012-01-15 11:07:26 -08001164 err = kstrtoul(buf, 10, &val);
1165 if (err)
1166 return err;
Carlos Olalla Martinez1550cb62007-06-09 10:11:16 -04001167
1168 mutex_lock(&data->update_lock);
1169
1170 if (data->type == w83627hf) {
Jim Cromie07584c72007-10-12 21:08:00 +02001171 data->pwm_freq[nr] = pwm_freq_to_reg_627hf(val);
Carlos Olalla Martinez1550cb62007-06-09 10:11:16 -04001172 w83627hf_write_value(data, W83627HF_REG_PWM_FREQ,
Jim Cromie07584c72007-10-12 21:08:00 +02001173 (data->pwm_freq[nr] << (nr*4)) |
Carlos Olalla Martinez1550cb62007-06-09 10:11:16 -04001174 (w83627hf_read_value(data,
Jim Cromie07584c72007-10-12 21:08:00 +02001175 W83627HF_REG_PWM_FREQ) & mask[nr]));
Carlos Olalla Martinez1550cb62007-06-09 10:11:16 -04001176 } else {
Jim Cromie07584c72007-10-12 21:08:00 +02001177 data->pwm_freq[nr] = pwm_freq_to_reg(val);
1178 w83627hf_write_value(data, W83637HF_REG_PWM_FREQ[nr],
1179 data->pwm_freq[nr]);
Carlos Olalla Martinez1550cb62007-06-09 10:11:16 -04001180 }
1181
1182 mutex_unlock(&data->update_lock);
1183 return count;
1184}
1185
Jim Cromie07584c72007-10-12 21:08:00 +02001186static SENSOR_DEVICE_ATTR(pwm1_freq, S_IRUGO|S_IWUSR,
1187 show_pwm_freq, store_pwm_freq, 0);
1188static SENSOR_DEVICE_ATTR(pwm2_freq, S_IRUGO|S_IWUSR,
1189 show_pwm_freq, store_pwm_freq, 1);
1190static SENSOR_DEVICE_ATTR(pwm3_freq, S_IRUGO|S_IWUSR,
1191 show_pwm_freq, store_pwm_freq, 2);
Carlos Olalla Martinez1550cb62007-06-09 10:11:16 -04001192
1193static ssize_t
Jim Cromie07584c72007-10-12 21:08:00 +02001194show_temp_type(struct device *dev, struct device_attribute *devattr,
1195 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001196{
Jim Cromie07584c72007-10-12 21:08:00 +02001197 int nr = to_sensor_dev_attr(devattr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001198 struct w83627hf_data *data = w83627hf_update_device(dev);
Jim Cromie07584c72007-10-12 21:08:00 +02001199 return sprintf(buf, "%ld\n", (long) data->sens[nr]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001200}
1201
1202static ssize_t
Jim Cromie07584c72007-10-12 21:08:00 +02001203store_temp_type(struct device *dev, struct device_attribute *devattr,
1204 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001205{
Jim Cromie07584c72007-10-12 21:08:00 +02001206 int nr = to_sensor_dev_attr(devattr)->index;
Jean Delvare787c72b2007-05-08 17:22:00 +02001207 struct w83627hf_data *data = dev_get_drvdata(dev);
Guenter Roeck27b9de32012-01-15 11:07:26 -08001208 unsigned long val;
1209 u32 tmp;
1210 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001211
Guenter Roeck27b9de32012-01-15 11:07:26 -08001212 err = kstrtoul(buf, 10, &val);
1213 if (err)
1214 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001215
Ingo Molnar9a61bf62006-01-18 23:19:26 +01001216 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001217
1218 switch (val) {
1219 case 1: /* PII/Celeron diode */
Jean Delvare787c72b2007-05-08 17:22:00 +02001220 tmp = w83627hf_read_value(data, W83781D_REG_SCFG1);
1221 w83627hf_write_value(data, W83781D_REG_SCFG1,
Jim Cromie07584c72007-10-12 21:08:00 +02001222 tmp | BIT_SCFG1[nr]);
Jean Delvare787c72b2007-05-08 17:22:00 +02001223 tmp = w83627hf_read_value(data, W83781D_REG_SCFG2);
1224 w83627hf_write_value(data, W83781D_REG_SCFG2,
Jim Cromie07584c72007-10-12 21:08:00 +02001225 tmp | BIT_SCFG2[nr]);
1226 data->sens[nr] = val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001227 break;
1228 case 2: /* 3904 */
Jean Delvare787c72b2007-05-08 17:22:00 +02001229 tmp = w83627hf_read_value(data, W83781D_REG_SCFG1);
1230 w83627hf_write_value(data, W83781D_REG_SCFG1,
Jim Cromie07584c72007-10-12 21:08:00 +02001231 tmp | BIT_SCFG1[nr]);
Jean Delvare787c72b2007-05-08 17:22:00 +02001232 tmp = w83627hf_read_value(data, W83781D_REG_SCFG2);
1233 w83627hf_write_value(data, W83781D_REG_SCFG2,
Jim Cromie07584c72007-10-12 21:08:00 +02001234 tmp & ~BIT_SCFG2[nr]);
1235 data->sens[nr] = val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001236 break;
Jean Delvareb26f9332007-08-16 14:30:01 +02001237 case W83781D_DEFAULT_BETA:
1238 dev_warn(dev, "Sensor type %d is deprecated, please use 4 "
1239 "instead\n", W83781D_DEFAULT_BETA);
1240 /* fall through */
1241 case 4: /* thermistor */
Jean Delvare787c72b2007-05-08 17:22:00 +02001242 tmp = w83627hf_read_value(data, W83781D_REG_SCFG1);
1243 w83627hf_write_value(data, W83781D_REG_SCFG1,
Jim Cromie07584c72007-10-12 21:08:00 +02001244 tmp & ~BIT_SCFG1[nr]);
1245 data->sens[nr] = val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001246 break;
1247 default:
Jean Delvare787c72b2007-05-08 17:22:00 +02001248 dev_err(dev,
Jean Delvareb26f9332007-08-16 14:30:01 +02001249 "Invalid sensor type %ld; must be 1, 2, or 4\n",
1250 (long) val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001251 break;
1252 }
1253
Ingo Molnar9a61bf62006-01-18 23:19:26 +01001254 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001255 return count;
1256}
1257
Jim Cromie07584c72007-10-12 21:08:00 +02001258#define sysfs_temp_type(offset) \
1259static SENSOR_DEVICE_ATTR(temp##offset##_type, S_IRUGO | S_IWUSR, \
1260 show_temp_type, store_temp_type, offset - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001261
Jim Cromie07584c72007-10-12 21:08:00 +02001262sysfs_temp_type(1);
1263sysfs_temp_type(2);
1264sysfs_temp_type(3);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001265
Jim Cromie07584c72007-10-12 21:08:00 +02001266static ssize_t
1267show_name(struct device *dev, struct device_attribute *devattr, char *buf)
Jean Delvare787c72b2007-05-08 17:22:00 +02001268{
1269 struct w83627hf_data *data = dev_get_drvdata(dev);
1270
1271 return sprintf(buf, "%s\n", data->name);
1272}
1273static DEVICE_ATTR(name, S_IRUGO, show_name, NULL);
1274
1275static int __init w83627hf_find(int sioaddr, unsigned short *addr,
1276 struct w83627hf_sio_data *sio_data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001277{
Jean Delvared27c37c2007-05-08 17:21:59 +02001278 int err = -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001279 u16 val;
1280
Andi Kleen64f50302012-08-18 10:30:05 -07001281 static __initconst char *const names[] = {
Jean Delvare787c72b2007-05-08 17:22:00 +02001282 "W83627HF",
1283 "W83627THF",
1284 "W83697HF",
1285 "W83637HF",
1286 "W83687THF",
1287 };
1288
Christian Schultec46c0e92009-12-16 21:38:29 +01001289 sio_data->sioaddr = sioaddr;
Jean Delvareb72656d2009-12-09 20:35:49 +01001290 superio_enter(sio_data);
1291 val = force_id ? force_id : superio_inb(sio_data, DEVID);
Jean Delvare787c72b2007-05-08 17:22:00 +02001292 switch (val) {
1293 case W627_DEVID:
1294 sio_data->type = w83627hf;
1295 break;
1296 case W627THF_DEVID:
1297 sio_data->type = w83627thf;
1298 break;
1299 case W697_DEVID:
1300 sio_data->type = w83697hf;
1301 break;
1302 case W637_DEVID:
1303 sio_data->type = w83637hf;
1304 break;
1305 case W687THF_DEVID:
1306 sio_data->type = w83687thf;
1307 break;
Jean Delvaree142e2a2007-05-27 22:17:43 +02001308 case 0xff: /* No device at all */
1309 goto exit;
Jean Delvare787c72b2007-05-08 17:22:00 +02001310 default:
Jean Delvaree142e2a2007-05-27 22:17:43 +02001311 pr_debug(DRVNAME ": Unsupported chip (DEVID=0x%02x)\n", val);
Jean Delvared27c37c2007-05-08 17:21:59 +02001312 goto exit;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001313 }
1314
Jean Delvareb72656d2009-12-09 20:35:49 +01001315 superio_select(sio_data, W83627HF_LD_HWM);
1316 val = (superio_inb(sio_data, WINB_BASE_REG) << 8) |
1317 superio_inb(sio_data, WINB_BASE_REG + 1);
Petr Vandrovecada0c2f2005-10-07 23:11:03 +02001318 *addr = val & WINB_ALIGNMENT;
Jean Delvared27c37c2007-05-08 17:21:59 +02001319 if (*addr == 0) {
Joe Perches18de0302010-10-20 06:51:55 +00001320 pr_warn("Base address not set, skipping\n");
Jean Delvared27c37c2007-05-08 17:21:59 +02001321 goto exit;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001322 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001323
Jean Delvareb72656d2009-12-09 20:35:49 +01001324 val = superio_inb(sio_data, WINB_ACT_REG);
Jean Delvared27c37c2007-05-08 17:21:59 +02001325 if (!(val & 0x01)) {
Joe Perches18de0302010-10-20 06:51:55 +00001326 pr_warn("Enabling HWM logical device\n");
Jean Delvareb72656d2009-12-09 20:35:49 +01001327 superio_outb(sio_data, WINB_ACT_REG, val | 0x01);
Jean Delvared27c37c2007-05-08 17:21:59 +02001328 }
1329
1330 err = 0;
Jean Delvare787c72b2007-05-08 17:22:00 +02001331 pr_info(DRVNAME ": Found %s chip at %#x\n",
1332 names[sio_data->type], *addr);
Jean Delvared27c37c2007-05-08 17:21:59 +02001333
1334 exit:
Jean Delvareb72656d2009-12-09 20:35:49 +01001335 superio_exit(sio_data);
Jean Delvared27c37c2007-05-08 17:21:59 +02001336 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001337}
1338
Jim Cromie07584c72007-10-12 21:08:00 +02001339#define VIN_UNIT_ATTRS(_X_) \
1340 &sensor_dev_attr_in##_X_##_input.dev_attr.attr, \
1341 &sensor_dev_attr_in##_X_##_min.dev_attr.attr, \
Jean Delvaree3604c62008-01-03 23:00:30 +01001342 &sensor_dev_attr_in##_X_##_max.dev_attr.attr, \
1343 &sensor_dev_attr_in##_X_##_alarm.dev_attr.attr, \
1344 &sensor_dev_attr_in##_X_##_beep.dev_attr.attr
Jim Cromie07584c72007-10-12 21:08:00 +02001345
1346#define FAN_UNIT_ATTRS(_X_) \
1347 &sensor_dev_attr_fan##_X_##_input.dev_attr.attr, \
1348 &sensor_dev_attr_fan##_X_##_min.dev_attr.attr, \
Jean Delvaree3604c62008-01-03 23:00:30 +01001349 &sensor_dev_attr_fan##_X_##_div.dev_attr.attr, \
1350 &sensor_dev_attr_fan##_X_##_alarm.dev_attr.attr, \
1351 &sensor_dev_attr_fan##_X_##_beep.dev_attr.attr
Jim Cromie07584c72007-10-12 21:08:00 +02001352
1353#define TEMP_UNIT_ATTRS(_X_) \
1354 &sensor_dev_attr_temp##_X_##_input.dev_attr.attr, \
1355 &sensor_dev_attr_temp##_X_##_max.dev_attr.attr, \
1356 &sensor_dev_attr_temp##_X_##_max_hyst.dev_attr.attr, \
Jean Delvaree3604c62008-01-03 23:00:30 +01001357 &sensor_dev_attr_temp##_X_##_type.dev_attr.attr, \
1358 &sensor_dev_attr_temp##_X_##_alarm.dev_attr.attr, \
1359 &sensor_dev_attr_temp##_X_##_beep.dev_attr.attr
Jim Cromie07584c72007-10-12 21:08:00 +02001360
Mark M. Hoffmanc1685f62006-09-24 20:59:49 +02001361static struct attribute *w83627hf_attributes[] = {
1362 &dev_attr_in0_input.attr,
1363 &dev_attr_in0_min.attr,
1364 &dev_attr_in0_max.attr,
Jean Delvaree3604c62008-01-03 23:00:30 +01001365 &sensor_dev_attr_in0_alarm.dev_attr.attr,
1366 &sensor_dev_attr_in0_beep.dev_attr.attr,
Jim Cromie07584c72007-10-12 21:08:00 +02001367 VIN_UNIT_ATTRS(2),
1368 VIN_UNIT_ATTRS(3),
1369 VIN_UNIT_ATTRS(4),
1370 VIN_UNIT_ATTRS(7),
1371 VIN_UNIT_ATTRS(8),
Mark M. Hoffmanc1685f62006-09-24 20:59:49 +02001372
Jim Cromie07584c72007-10-12 21:08:00 +02001373 FAN_UNIT_ATTRS(1),
1374 FAN_UNIT_ATTRS(2),
Mark M. Hoffmanc1685f62006-09-24 20:59:49 +02001375
Jim Cromie07584c72007-10-12 21:08:00 +02001376 TEMP_UNIT_ATTRS(1),
1377 TEMP_UNIT_ATTRS(2),
Mark M. Hoffmanc1685f62006-09-24 20:59:49 +02001378
1379 &dev_attr_alarms.attr,
Jean Delvare1c138102008-01-03 23:04:55 +01001380 &sensor_dev_attr_beep_enable.dev_attr.attr,
Mark M. Hoffmanc1685f62006-09-24 20:59:49 +02001381 &dev_attr_beep_mask.attr,
1382
Jim Cromie07584c72007-10-12 21:08:00 +02001383 &sensor_dev_attr_pwm1.dev_attr.attr,
1384 &sensor_dev_attr_pwm2.dev_attr.attr,
Jean Delvare787c72b2007-05-08 17:22:00 +02001385 &dev_attr_name.attr,
Mark M. Hoffmanc1685f62006-09-24 20:59:49 +02001386 NULL
1387};
1388
1389static const struct attribute_group w83627hf_group = {
1390 .attrs = w83627hf_attributes,
1391};
1392
1393static struct attribute *w83627hf_attributes_opt[] = {
Jim Cromie07584c72007-10-12 21:08:00 +02001394 VIN_UNIT_ATTRS(1),
1395 VIN_UNIT_ATTRS(5),
1396 VIN_UNIT_ATTRS(6),
Mark M. Hoffmanc1685f62006-09-24 20:59:49 +02001397
Jim Cromie07584c72007-10-12 21:08:00 +02001398 FAN_UNIT_ATTRS(3),
1399 TEMP_UNIT_ATTRS(3),
1400 &sensor_dev_attr_pwm3.dev_attr.attr,
Mark M. Hoffmanc1685f62006-09-24 20:59:49 +02001401
Jim Cromie07584c72007-10-12 21:08:00 +02001402 &sensor_dev_attr_pwm1_freq.dev_attr.attr,
1403 &sensor_dev_attr_pwm2_freq.dev_attr.attr,
1404 &sensor_dev_attr_pwm3_freq.dev_attr.attr,
Dominik Geyera95a5ed2008-08-06 22:41:04 +02001405
1406 &sensor_dev_attr_pwm1_enable.dev_attr.attr,
1407 &sensor_dev_attr_pwm2_enable.dev_attr.attr,
1408 &sensor_dev_attr_pwm3_enable.dev_attr.attr,
1409
Mark M. Hoffmanc1685f62006-09-24 20:59:49 +02001410 NULL
1411};
1412
1413static const struct attribute_group w83627hf_group_opt = {
1414 .attrs = w83627hf_attributes_opt,
1415};
1416
Bill Pemberton6c931ae2012-11-19 13:22:35 -05001417static int w83627hf_probe(struct platform_device *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001418{
Jean Delvare787c72b2007-05-08 17:22:00 +02001419 struct device *dev = &pdev->dev;
Jingoo Hana8b3a3a2013-07-30 17:13:06 +09001420 struct w83627hf_sio_data *sio_data = dev_get_platdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001421 struct w83627hf_data *data;
Jean Delvare787c72b2007-05-08 17:22:00 +02001422 struct resource *res;
Jim Cromie2ca2fcd2007-10-14 17:20:50 -06001423 int err, i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001424
Jean Delvare787c72b2007-05-08 17:22:00 +02001425 static const char *names[] = {
1426 "w83627hf",
1427 "w83627thf",
1428 "w83697hf",
1429 "w83637hf",
1430 "w83687thf",
1431 };
1432
1433 res = platform_get_resource(pdev, IORESOURCE_IO, 0);
Guenter Roeck0cf46992012-06-02 11:47:59 -07001434 if (!devm_request_region(dev, res->start, WINB_REGION_SIZE, DRVNAME)) {
Jean Delvare787c72b2007-05-08 17:22:00 +02001435 dev_err(dev, "Failed to request region 0x%lx-0x%lx\n",
1436 (unsigned long)res->start,
1437 (unsigned long)(res->start + WINB_REGION_SIZE - 1));
Guenter Roeck0cf46992012-06-02 11:47:59 -07001438 return -EBUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001439 }
1440
Guenter Roeck0cf46992012-06-02 11:47:59 -07001441 data = devm_kzalloc(dev, sizeof(struct w83627hf_data), GFP_KERNEL);
1442 if (!data)
1443 return -ENOMEM;
1444
Jean Delvare787c72b2007-05-08 17:22:00 +02001445 data->addr = res->start;
1446 data->type = sio_data->type;
1447 data->name = names[sio_data->type];
Ingo Molnar9a61bf62006-01-18 23:19:26 +01001448 mutex_init(&data->lock);
Ingo Molnar9a61bf62006-01-18 23:19:26 +01001449 mutex_init(&data->update_lock);
Jean Delvare787c72b2007-05-08 17:22:00 +02001450 platform_set_drvdata(pdev, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001451
Linus Torvalds1da177e2005-04-16 15:20:36 -07001452 /* Initialize the chip */
Jean Delvare787c72b2007-05-08 17:22:00 +02001453 w83627hf_init_device(pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001454
1455 /* A few vars need to be filled upon startup */
Jim Cromie2ca2fcd2007-10-14 17:20:50 -06001456 for (i = 0; i <= 2; i++)
1457 data->fan_min[i] = w83627hf_read_value(
1458 data, W83627HF_REG_FAN_MIN(i));
Jean Delvarec09c5182007-10-12 21:53:07 +02001459 w83627hf_update_fan_div(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001460
Mark M. Hoffmanc1685f62006-09-24 20:59:49 +02001461 /* Register common device attributes */
Guenter Roeck27b9de32012-01-15 11:07:26 -08001462 err = sysfs_create_group(&dev->kobj, &w83627hf_group);
1463 if (err)
Guenter Roeck0cf46992012-06-02 11:47:59 -07001464 return err;
Mark M. Hoffmanc1685f62006-09-24 20:59:49 +02001465
1466 /* Register chip-specific device attributes */
Jean Delvare787c72b2007-05-08 17:22:00 +02001467 if (data->type == w83627hf || data->type == w83697hf)
Jim Cromie07584c72007-10-12 21:08:00 +02001468 if ((err = device_create_file(dev,
1469 &sensor_dev_attr_in5_input.dev_attr))
1470 || (err = device_create_file(dev,
1471 &sensor_dev_attr_in5_min.dev_attr))
1472 || (err = device_create_file(dev,
1473 &sensor_dev_attr_in5_max.dev_attr))
1474 || (err = device_create_file(dev,
Jean Delvaree3604c62008-01-03 23:00:30 +01001475 &sensor_dev_attr_in5_alarm.dev_attr))
1476 || (err = device_create_file(dev,
1477 &sensor_dev_attr_in5_beep.dev_attr))
1478 || (err = device_create_file(dev,
Jim Cromie07584c72007-10-12 21:08:00 +02001479 &sensor_dev_attr_in6_input.dev_attr))
1480 || (err = device_create_file(dev,
1481 &sensor_dev_attr_in6_min.dev_attr))
1482 || (err = device_create_file(dev,
1483 &sensor_dev_attr_in6_max.dev_attr))
1484 || (err = device_create_file(dev,
Jean Delvaree3604c62008-01-03 23:00:30 +01001485 &sensor_dev_attr_in6_alarm.dev_attr))
1486 || (err = device_create_file(dev,
1487 &sensor_dev_attr_in6_beep.dev_attr))
1488 || (err = device_create_file(dev,
Jim Cromie07584c72007-10-12 21:08:00 +02001489 &sensor_dev_attr_pwm1_freq.dev_attr))
1490 || (err = device_create_file(dev,
1491 &sensor_dev_attr_pwm2_freq.dev_attr)))
Guenter Roeck0cf46992012-06-02 11:47:59 -07001492 goto error;
Mark M. Hoffmanc1685f62006-09-24 20:59:49 +02001493
Jean Delvare787c72b2007-05-08 17:22:00 +02001494 if (data->type != w83697hf)
Jim Cromie07584c72007-10-12 21:08:00 +02001495 if ((err = device_create_file(dev,
1496 &sensor_dev_attr_in1_input.dev_attr))
1497 || (err = device_create_file(dev,
1498 &sensor_dev_attr_in1_min.dev_attr))
1499 || (err = device_create_file(dev,
1500 &sensor_dev_attr_in1_max.dev_attr))
1501 || (err = device_create_file(dev,
Jean Delvaree3604c62008-01-03 23:00:30 +01001502 &sensor_dev_attr_in1_alarm.dev_attr))
1503 || (err = device_create_file(dev,
1504 &sensor_dev_attr_in1_beep.dev_attr))
1505 || (err = device_create_file(dev,
Jim Cromie07584c72007-10-12 21:08:00 +02001506 &sensor_dev_attr_fan3_input.dev_attr))
1507 || (err = device_create_file(dev,
1508 &sensor_dev_attr_fan3_min.dev_attr))
1509 || (err = device_create_file(dev,
1510 &sensor_dev_attr_fan3_div.dev_attr))
1511 || (err = device_create_file(dev,
Jean Delvaree3604c62008-01-03 23:00:30 +01001512 &sensor_dev_attr_fan3_alarm.dev_attr))
1513 || (err = device_create_file(dev,
1514 &sensor_dev_attr_fan3_beep.dev_attr))
1515 || (err = device_create_file(dev,
Jim Cromie07584c72007-10-12 21:08:00 +02001516 &sensor_dev_attr_temp3_input.dev_attr))
1517 || (err = device_create_file(dev,
1518 &sensor_dev_attr_temp3_max.dev_attr))
1519 || (err = device_create_file(dev,
1520 &sensor_dev_attr_temp3_max_hyst.dev_attr))
1521 || (err = device_create_file(dev,
Jean Delvaree3604c62008-01-03 23:00:30 +01001522 &sensor_dev_attr_temp3_alarm.dev_attr))
1523 || (err = device_create_file(dev,
1524 &sensor_dev_attr_temp3_beep.dev_attr))
1525 || (err = device_create_file(dev,
Jim Cromie07584c72007-10-12 21:08:00 +02001526 &sensor_dev_attr_temp3_type.dev_attr)))
Guenter Roeck0cf46992012-06-02 11:47:59 -07001527 goto error;
Mark M. Hoffmanc1685f62006-09-24 20:59:49 +02001528
Jean Delvare787c72b2007-05-08 17:22:00 +02001529 if (data->type != w83697hf && data->vid != 0xff) {
Jean Delvare8a665a02007-05-08 17:21:59 +02001530 /* Convert VID to voltage based on VRM */
1531 data->vrm = vid_which_vrm();
1532
Jean Delvare787c72b2007-05-08 17:22:00 +02001533 if ((err = device_create_file(dev, &dev_attr_cpu0_vid))
1534 || (err = device_create_file(dev, &dev_attr_vrm)))
Guenter Roeck0cf46992012-06-02 11:47:59 -07001535 goto error;
Jean Delvare8a665a02007-05-08 17:21:59 +02001536 }
Mark M. Hoffmanc1685f62006-09-24 20:59:49 +02001537
Jean Delvare787c72b2007-05-08 17:22:00 +02001538 if (data->type == w83627thf || data->type == w83637hf
Guenter Roeck27b9de32012-01-15 11:07:26 -08001539 || data->type == w83687thf) {
1540 err = device_create_file(dev, &sensor_dev_attr_pwm3.dev_attr);
1541 if (err)
Guenter Roeck0cf46992012-06-02 11:47:59 -07001542 goto error;
Guenter Roeck27b9de32012-01-15 11:07:26 -08001543 }
Mark M. Hoffmanc1685f62006-09-24 20:59:49 +02001544
Carlos Olalla Martinez1550cb62007-06-09 10:11:16 -04001545 if (data->type == w83637hf || data->type == w83687thf)
Jim Cromie07584c72007-10-12 21:08:00 +02001546 if ((err = device_create_file(dev,
1547 &sensor_dev_attr_pwm1_freq.dev_attr))
1548 || (err = device_create_file(dev,
1549 &sensor_dev_attr_pwm2_freq.dev_attr))
1550 || (err = device_create_file(dev,
1551 &sensor_dev_attr_pwm3_freq.dev_attr)))
Guenter Roeck0cf46992012-06-02 11:47:59 -07001552 goto error;
Carlos Olalla Martinez1550cb62007-06-09 10:11:16 -04001553
Dominik Geyera95a5ed2008-08-06 22:41:04 +02001554 if (data->type != w83627hf)
1555 if ((err = device_create_file(dev,
1556 &sensor_dev_attr_pwm1_enable.dev_attr))
1557 || (err = device_create_file(dev,
1558 &sensor_dev_attr_pwm2_enable.dev_attr)))
Guenter Roeck0cf46992012-06-02 11:47:59 -07001559 goto error;
Dominik Geyera95a5ed2008-08-06 22:41:04 +02001560
1561 if (data->type == w83627thf || data->type == w83637hf
Guenter Roeck27b9de32012-01-15 11:07:26 -08001562 || data->type == w83687thf) {
1563 err = device_create_file(dev,
1564 &sensor_dev_attr_pwm3_enable.dev_attr);
1565 if (err)
Guenter Roeck0cf46992012-06-02 11:47:59 -07001566 goto error;
Guenter Roeck27b9de32012-01-15 11:07:26 -08001567 }
Dominik Geyera95a5ed2008-08-06 22:41:04 +02001568
Tony Jones1beeffe2007-08-20 13:46:20 -07001569 data->hwmon_dev = hwmon_device_register(dev);
1570 if (IS_ERR(data->hwmon_dev)) {
1571 err = PTR_ERR(data->hwmon_dev);
Guenter Roeck0cf46992012-06-02 11:47:59 -07001572 goto error;
Mark M. Hoffman943b0832005-07-15 21:39:18 -04001573 }
1574
Linus Torvalds1da177e2005-04-16 15:20:36 -07001575 return 0;
1576
Guenter Roeck0cf46992012-06-02 11:47:59 -07001577 error:
Jean Delvare787c72b2007-05-08 17:22:00 +02001578 sysfs_remove_group(&dev->kobj, &w83627hf_group);
1579 sysfs_remove_group(&dev->kobj, &w83627hf_group_opt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001580 return err;
1581}
1582
Bill Pemberton281dfd02012-11-19 13:25:51 -05001583static int w83627hf_remove(struct platform_device *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001584{
Jean Delvare787c72b2007-05-08 17:22:00 +02001585 struct w83627hf_data *data = platform_get_drvdata(pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001586
Tony Jones1beeffe2007-08-20 13:46:20 -07001587 hwmon_device_unregister(data->hwmon_dev);
Mark M. Hoffman943b0832005-07-15 21:39:18 -04001588
Jean Delvare787c72b2007-05-08 17:22:00 +02001589 sysfs_remove_group(&pdev->dev.kobj, &w83627hf_group);
1590 sysfs_remove_group(&pdev->dev.kobj, &w83627hf_group_opt);
Jean Delvare787c72b2007-05-08 17:22:00 +02001591
Linus Torvalds1da177e2005-04-16 15:20:36 -07001592 return 0;
1593}
1594
1595
Jean Delvared58df9c2007-10-10 16:30:23 +02001596/* Registers 0x50-0x5f are banked */
1597static inline void w83627hf_set_bank(struct w83627hf_data *data, u16 reg)
1598{
1599 if ((reg & 0x00f0) == 0x50) {
1600 outb_p(W83781D_REG_BANK, data->addr + W83781D_ADDR_REG_OFFSET);
1601 outb_p(reg >> 8, data->addr + W83781D_DATA_REG_OFFSET);
1602 }
1603}
1604
1605/* Not strictly necessary, but play it safe for now */
1606static inline void w83627hf_reset_bank(struct w83627hf_data *data, u16 reg)
1607{
1608 if (reg & 0xff00) {
1609 outb_p(W83781D_REG_BANK, data->addr + W83781D_ADDR_REG_OFFSET);
1610 outb_p(0, data->addr + W83781D_DATA_REG_OFFSET);
1611 }
1612}
1613
Jean Delvare787c72b2007-05-08 17:22:00 +02001614static int w83627hf_read_value(struct w83627hf_data *data, u16 reg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001615{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001616 int res, word_sized;
1617
Ingo Molnar9a61bf62006-01-18 23:19:26 +01001618 mutex_lock(&data->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001619 word_sized = (((reg & 0xff00) == 0x100)
1620 || ((reg & 0xff00) == 0x200))
1621 && (((reg & 0x00ff) == 0x50)
1622 || ((reg & 0x00ff) == 0x53)
1623 || ((reg & 0x00ff) == 0x55));
Jean Delvared58df9c2007-10-10 16:30:23 +02001624 w83627hf_set_bank(data, reg);
Jean Delvare787c72b2007-05-08 17:22:00 +02001625 outb_p(reg & 0xff, data->addr + W83781D_ADDR_REG_OFFSET);
1626 res = inb_p(data->addr + W83781D_DATA_REG_OFFSET);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001627 if (word_sized) {
1628 outb_p((reg & 0xff) + 1,
Jean Delvare787c72b2007-05-08 17:22:00 +02001629 data->addr + W83781D_ADDR_REG_OFFSET);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001630 res =
Jean Delvare787c72b2007-05-08 17:22:00 +02001631 (res << 8) + inb_p(data->addr +
Linus Torvalds1da177e2005-04-16 15:20:36 -07001632 W83781D_DATA_REG_OFFSET);
1633 }
Jean Delvared58df9c2007-10-10 16:30:23 +02001634 w83627hf_reset_bank(data, reg);
Ingo Molnar9a61bf62006-01-18 23:19:26 +01001635 mutex_unlock(&data->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001636 return res;
1637}
1638
Bill Pemberton6c931ae2012-11-19 13:22:35 -05001639static int w83627thf_read_gpio5(struct platform_device *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001640{
Jingoo Hana8b3a3a2013-07-30 17:13:06 +09001641 struct w83627hf_sio_data *sio_data = dev_get_platdata(&pdev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001642 int res = 0xff, sel;
1643
Jean Delvareb72656d2009-12-09 20:35:49 +01001644 superio_enter(sio_data);
1645 superio_select(sio_data, W83627HF_LD_GPIO5);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001646
1647 /* Make sure these GPIO pins are enabled */
Jean Delvareb72656d2009-12-09 20:35:49 +01001648 if (!(superio_inb(sio_data, W83627THF_GPIO5_EN) & (1<<3))) {
Jean Delvare787c72b2007-05-08 17:22:00 +02001649 dev_dbg(&pdev->dev, "GPIO5 disabled, no VID function\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001650 goto exit;
1651 }
1652
Guenter Roeck27b9de32012-01-15 11:07:26 -08001653 /*
1654 * Make sure the pins are configured for input
1655 * There must be at least five (VRM 9), and possibly 6 (VRM 10)
1656 */
Jean Delvareb72656d2009-12-09 20:35:49 +01001657 sel = superio_inb(sio_data, W83627THF_GPIO5_IOSR) & 0x3f;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001658 if ((sel & 0x1f) != 0x1f) {
Jean Delvare787c72b2007-05-08 17:22:00 +02001659 dev_dbg(&pdev->dev, "GPIO5 not configured for VID "
Linus Torvalds1da177e2005-04-16 15:20:36 -07001660 "function\n");
1661 goto exit;
1662 }
1663
Jean Delvare787c72b2007-05-08 17:22:00 +02001664 dev_info(&pdev->dev, "Reading VID from GPIO5\n");
Jean Delvareb72656d2009-12-09 20:35:49 +01001665 res = superio_inb(sio_data, W83627THF_GPIO5_DR) & sel;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001666
1667exit:
Jean Delvareb72656d2009-12-09 20:35:49 +01001668 superio_exit(sio_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001669 return res;
1670}
1671
Bill Pemberton6c931ae2012-11-19 13:22:35 -05001672static int w83687thf_read_vid(struct platform_device *pdev)
Jean Delvarec2db6ce2006-01-18 23:22:12 +01001673{
Jingoo Hana8b3a3a2013-07-30 17:13:06 +09001674 struct w83627hf_sio_data *sio_data = dev_get_platdata(&pdev->dev);
Jean Delvarec2db6ce2006-01-18 23:22:12 +01001675 int res = 0xff;
1676
Jean Delvareb72656d2009-12-09 20:35:49 +01001677 superio_enter(sio_data);
1678 superio_select(sio_data, W83627HF_LD_HWM);
Jean Delvarec2db6ce2006-01-18 23:22:12 +01001679
1680 /* Make sure these GPIO pins are enabled */
Jean Delvareb72656d2009-12-09 20:35:49 +01001681 if (!(superio_inb(sio_data, W83687THF_VID_EN) & (1 << 2))) {
Jean Delvare787c72b2007-05-08 17:22:00 +02001682 dev_dbg(&pdev->dev, "VID disabled, no VID function\n");
Jean Delvarec2db6ce2006-01-18 23:22:12 +01001683 goto exit;
1684 }
1685
1686 /* Make sure the pins are configured for input */
Jean Delvareb72656d2009-12-09 20:35:49 +01001687 if (!(superio_inb(sio_data, W83687THF_VID_CFG) & (1 << 4))) {
Jean Delvare787c72b2007-05-08 17:22:00 +02001688 dev_dbg(&pdev->dev, "VID configured as output, "
Jean Delvarec2db6ce2006-01-18 23:22:12 +01001689 "no VID function\n");
1690 goto exit;
1691 }
1692
Jean Delvareb72656d2009-12-09 20:35:49 +01001693 res = superio_inb(sio_data, W83687THF_VID_DATA) & 0x3f;
Jean Delvarec2db6ce2006-01-18 23:22:12 +01001694
1695exit:
Jean Delvareb72656d2009-12-09 20:35:49 +01001696 superio_exit(sio_data);
Jean Delvarec2db6ce2006-01-18 23:22:12 +01001697 return res;
1698}
1699
Jean Delvare787c72b2007-05-08 17:22:00 +02001700static int w83627hf_write_value(struct w83627hf_data *data, u16 reg, u16 value)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001701{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001702 int word_sized;
1703
Ingo Molnar9a61bf62006-01-18 23:19:26 +01001704 mutex_lock(&data->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001705 word_sized = (((reg & 0xff00) == 0x100)
1706 || ((reg & 0xff00) == 0x200))
1707 && (((reg & 0x00ff) == 0x53)
1708 || ((reg & 0x00ff) == 0x55));
Jean Delvared58df9c2007-10-10 16:30:23 +02001709 w83627hf_set_bank(data, reg);
Jean Delvare787c72b2007-05-08 17:22:00 +02001710 outb_p(reg & 0xff, data->addr + W83781D_ADDR_REG_OFFSET);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001711 if (word_sized) {
1712 outb_p(value >> 8,
Jean Delvare787c72b2007-05-08 17:22:00 +02001713 data->addr + W83781D_DATA_REG_OFFSET);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001714 outb_p((reg & 0xff) + 1,
Jean Delvare787c72b2007-05-08 17:22:00 +02001715 data->addr + W83781D_ADDR_REG_OFFSET);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001716 }
1717 outb_p(value & 0xff,
Jean Delvare787c72b2007-05-08 17:22:00 +02001718 data->addr + W83781D_DATA_REG_OFFSET);
Jean Delvared58df9c2007-10-10 16:30:23 +02001719 w83627hf_reset_bank(data, reg);
Ingo Molnar9a61bf62006-01-18 23:19:26 +01001720 mutex_unlock(&data->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001721 return 0;
1722}
1723
Bill Pemberton6c931ae2012-11-19 13:22:35 -05001724static void w83627hf_init_device(struct platform_device *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001725{
Jean Delvare787c72b2007-05-08 17:22:00 +02001726 struct w83627hf_data *data = platform_get_drvdata(pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001727 int i;
Jean Delvared27c37c2007-05-08 17:21:59 +02001728 enum chips type = data->type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001729 u8 tmp;
1730
Linus Torvalds1da177e2005-04-16 15:20:36 -07001731 /* Minimize conflicts with other winbond i2c-only clients... */
1732 /* disable i2c subclients... how to disable main i2c client?? */
1733 /* force i2c address to relatively uncommon address */
Jean Delvare8f3c7c52012-12-19 22:17:00 +01001734 if (type == w83627hf) {
1735 w83627hf_write_value(data, W83781D_REG_I2C_SUBADDR, 0x89);
1736 w83627hf_write_value(data, W83781D_REG_I2C_ADDR, force_i2c);
1737 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001738
1739 /* Read VID only once */
Jean Delvared27c37c2007-05-08 17:21:59 +02001740 if (type == w83627hf || type == w83637hf) {
Jean Delvare787c72b2007-05-08 17:22:00 +02001741 int lo = w83627hf_read_value(data, W83781D_REG_VID_FANDIV);
1742 int hi = w83627hf_read_value(data, W83781D_REG_CHIPID);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001743 data->vid = (lo & 0x0f) | ((hi & 0x01) << 4);
Jean Delvared27c37c2007-05-08 17:21:59 +02001744 } else if (type == w83627thf) {
Jean Delvare787c72b2007-05-08 17:22:00 +02001745 data->vid = w83627thf_read_gpio5(pdev);
Jean Delvared27c37c2007-05-08 17:21:59 +02001746 } else if (type == w83687thf) {
Jean Delvare787c72b2007-05-08 17:22:00 +02001747 data->vid = w83687thf_read_vid(pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001748 }
1749
1750 /* Read VRM & OVT Config only once */
Jean Delvared27c37c2007-05-08 17:21:59 +02001751 if (type == w83627thf || type == w83637hf || type == w83687thf) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001752 data->vrm_ovt =
Jean Delvare787c72b2007-05-08 17:22:00 +02001753 w83627hf_read_value(data, W83627THF_REG_VRM_OVT_CFG);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001754 }
1755
Jean Delvare787c72b2007-05-08 17:22:00 +02001756 tmp = w83627hf_read_value(data, W83781D_REG_SCFG1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001757 for (i = 1; i <= 3; i++) {
1758 if (!(tmp & BIT_SCFG1[i - 1])) {
Jean Delvareb26f9332007-08-16 14:30:01 +02001759 data->sens[i - 1] = 4;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001760 } else {
1761 if (w83627hf_read_value
Jean Delvare787c72b2007-05-08 17:22:00 +02001762 (data,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001763 W83781D_REG_SCFG2) & BIT_SCFG2[i - 1])
1764 data->sens[i - 1] = 1;
1765 else
1766 data->sens[i - 1] = 2;
1767 }
1768 if ((type == w83697hf) && (i == 2))
1769 break;
1770 }
1771
1772 if(init) {
1773 /* Enable temp2 */
Jim Cromiedf48ed82007-10-14 17:10:52 -06001774 tmp = w83627hf_read_value(data, W83627HF_REG_TEMP2_CONFIG);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001775 if (tmp & 0x01) {
Jean Delvare787c72b2007-05-08 17:22:00 +02001776 dev_warn(&pdev->dev, "Enabling temp2, readings "
Linus Torvalds1da177e2005-04-16 15:20:36 -07001777 "might not make sense\n");
Jim Cromiedf48ed82007-10-14 17:10:52 -06001778 w83627hf_write_value(data, W83627HF_REG_TEMP2_CONFIG,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001779 tmp & 0xfe);
1780 }
1781
1782 /* Enable temp3 */
1783 if (type != w83697hf) {
Jean Delvare787c72b2007-05-08 17:22:00 +02001784 tmp = w83627hf_read_value(data,
Jim Cromiedf48ed82007-10-14 17:10:52 -06001785 W83627HF_REG_TEMP3_CONFIG);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001786 if (tmp & 0x01) {
Jean Delvare787c72b2007-05-08 17:22:00 +02001787 dev_warn(&pdev->dev, "Enabling temp3, "
Linus Torvalds1da177e2005-04-16 15:20:36 -07001788 "readings might not make sense\n");
Jean Delvare787c72b2007-05-08 17:22:00 +02001789 w83627hf_write_value(data,
Jim Cromiedf48ed82007-10-14 17:10:52 -06001790 W83627HF_REG_TEMP3_CONFIG, tmp & 0xfe);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001791 }
1792 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001793 }
1794
1795 /* Start monitoring */
Jean Delvare787c72b2007-05-08 17:22:00 +02001796 w83627hf_write_value(data, W83781D_REG_CONFIG,
1797 (w83627hf_read_value(data,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001798 W83781D_REG_CONFIG) & 0xf7)
1799 | 0x01);
Jean Delvareef878b12008-01-03 22:54:13 +01001800
1801 /* Enable VBAT monitoring if needed */
1802 tmp = w83627hf_read_value(data, W83781D_REG_VBAT);
1803 if (!(tmp & 0x01))
1804 w83627hf_write_value(data, W83781D_REG_VBAT, tmp | 0x01);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001805}
1806
Jean Delvarec09c5182007-10-12 21:53:07 +02001807static void w83627hf_update_fan_div(struct w83627hf_data *data)
1808{
1809 int reg;
1810
1811 reg = w83627hf_read_value(data, W83781D_REG_VID_FANDIV);
1812 data->fan_div[0] = (reg >> 4) & 0x03;
1813 data->fan_div[1] = (reg >> 6) & 0x03;
1814 if (data->type != w83697hf) {
1815 data->fan_div[2] = (w83627hf_read_value(data,
1816 W83781D_REG_PIN) >> 6) & 0x03;
1817 }
1818 reg = w83627hf_read_value(data, W83781D_REG_VBAT);
1819 data->fan_div[0] |= (reg >> 3) & 0x04;
1820 data->fan_div[1] |= (reg >> 4) & 0x04;
1821 if (data->type != w83697hf)
1822 data->fan_div[2] |= (reg >> 5) & 0x04;
1823}
1824
Linus Torvalds1da177e2005-04-16 15:20:36 -07001825static struct w83627hf_data *w83627hf_update_device(struct device *dev)
1826{
Jean Delvare787c72b2007-05-08 17:22:00 +02001827 struct w83627hf_data *data = dev_get_drvdata(dev);
Jim Cromiedf48ed82007-10-14 17:10:52 -06001828 int i, num_temps = (data->type == w83697hf) ? 2 : 3;
Dominik Geyera95a5ed2008-08-06 22:41:04 +02001829 int num_pwms = (data->type == w83697hf) ? 2 : 3;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001830
Ingo Molnar9a61bf62006-01-18 23:19:26 +01001831 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001832
1833 if (time_after(jiffies, data->last_updated + HZ + HZ / 2)
1834 || !data->valid) {
1835 for (i = 0; i <= 8; i++) {
1836 /* skip missing sensors */
1837 if (((data->type == w83697hf) && (i == 1)) ||
Jean Delvarec2db6ce2006-01-18 23:22:12 +01001838 ((data->type != w83627hf && data->type != w83697hf)
Yuan Mu4a1c44472005-11-07 22:19:04 +01001839 && (i == 5 || i == 6)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001840 continue;
1841 data->in[i] =
Jean Delvare787c72b2007-05-08 17:22:00 +02001842 w83627hf_read_value(data, W83781D_REG_IN(i));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001843 data->in_min[i] =
Jean Delvare787c72b2007-05-08 17:22:00 +02001844 w83627hf_read_value(data,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001845 W83781D_REG_IN_MIN(i));
1846 data->in_max[i] =
Jean Delvare787c72b2007-05-08 17:22:00 +02001847 w83627hf_read_value(data,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001848 W83781D_REG_IN_MAX(i));
1849 }
Jim Cromie2ca2fcd2007-10-14 17:20:50 -06001850 for (i = 0; i <= 2; i++) {
1851 data->fan[i] =
1852 w83627hf_read_value(data, W83627HF_REG_FAN(i));
1853 data->fan_min[i] =
Jean Delvare787c72b2007-05-08 17:22:00 +02001854 w83627hf_read_value(data,
Jim Cromie2ca2fcd2007-10-14 17:20:50 -06001855 W83627HF_REG_FAN_MIN(i));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001856 }
Jim Cromie07584c72007-10-12 21:08:00 +02001857 for (i = 0; i <= 2; i++) {
Jean Delvare787c72b2007-05-08 17:22:00 +02001858 u8 tmp = w83627hf_read_value(data,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001859 W836X7HF_REG_PWM(data->type, i));
1860 /* bits 0-3 are reserved in 627THF */
1861 if (data->type == w83627thf)
1862 tmp &= 0xf0;
Jim Cromie07584c72007-10-12 21:08:00 +02001863 data->pwm[i] = tmp;
1864 if (i == 1 &&
1865 (data->type == w83627hf || data->type == w83697hf))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001866 break;
1867 }
Carlos Olalla Martinez1550cb62007-06-09 10:11:16 -04001868 if (data->type == w83627hf) {
1869 u8 tmp = w83627hf_read_value(data,
1870 W83627HF_REG_PWM_FREQ);
1871 data->pwm_freq[0] = tmp & 0x07;
1872 data->pwm_freq[1] = (tmp >> 4) & 0x07;
1873 } else if (data->type != w83627thf) {
1874 for (i = 1; i <= 3; i++) {
1875 data->pwm_freq[i - 1] =
1876 w83627hf_read_value(data,
1877 W83637HF_REG_PWM_FREQ[i - 1]);
1878 if (i == 2 && (data->type == w83697hf))
1879 break;
1880 }
1881 }
Dominik Geyera95a5ed2008-08-06 22:41:04 +02001882 if (data->type != w83627hf) {
1883 for (i = 0; i < num_pwms; i++) {
1884 u8 tmp = w83627hf_read_value(data,
1885 W83627THF_REG_PWM_ENABLE[i]);
1886 data->pwm_enable[i] =
1887 ((tmp >> W83627THF_PWM_ENABLE_SHIFT[i])
1888 & 0x03) + 1;
1889 }
1890 }
Jim Cromiedf48ed82007-10-14 17:10:52 -06001891 for (i = 0; i < num_temps; i++) {
1892 data->temp[i] = w83627hf_read_value(
1893 data, w83627hf_reg_temp[i]);
1894 data->temp_max[i] = w83627hf_read_value(
1895 data, w83627hf_reg_temp_over[i]);
1896 data->temp_max_hyst[i] = w83627hf_read_value(
1897 data, w83627hf_reg_temp_hyst[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001898 }
1899
Jean Delvarec09c5182007-10-12 21:53:07 +02001900 w83627hf_update_fan_div(data);
1901
Linus Torvalds1da177e2005-04-16 15:20:36 -07001902 data->alarms =
Jean Delvare787c72b2007-05-08 17:22:00 +02001903 w83627hf_read_value(data, W83781D_REG_ALARM1) |
1904 (w83627hf_read_value(data, W83781D_REG_ALARM2) << 8) |
1905 (w83627hf_read_value(data, W83781D_REG_ALARM3) << 16);
1906 i = w83627hf_read_value(data, W83781D_REG_BEEP_INTS2);
Jean Delvare1c138102008-01-03 23:04:55 +01001907 data->beep_mask = (i << 8) |
Jean Delvare787c72b2007-05-08 17:22:00 +02001908 w83627hf_read_value(data, W83781D_REG_BEEP_INTS1) |
1909 w83627hf_read_value(data, W83781D_REG_BEEP_INTS3) << 16;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001910 data->last_updated = jiffies;
1911 data->valid = 1;
1912 }
1913
Ingo Molnar9a61bf62006-01-18 23:19:26 +01001914 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001915
1916 return data;
1917}
1918
Jean Delvare787c72b2007-05-08 17:22:00 +02001919static int __init w83627hf_device_add(unsigned short address,
1920 const struct w83627hf_sio_data *sio_data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001921{
Jean Delvare787c72b2007-05-08 17:22:00 +02001922 struct resource res = {
1923 .start = address + WINB_REGION_OFFSET,
1924 .end = address + WINB_REGION_OFFSET + WINB_REGION_SIZE - 1,
1925 .name = DRVNAME,
1926 .flags = IORESOURCE_IO,
1927 };
1928 int err;
1929
Jean Delvareb9acb642009-01-07 16:37:35 +01001930 err = acpi_check_resource_conflict(&res);
1931 if (err)
1932 goto exit;
1933
Jean Delvare787c72b2007-05-08 17:22:00 +02001934 pdev = platform_device_alloc(DRVNAME, address);
1935 if (!pdev) {
1936 err = -ENOMEM;
Joe Perches18de0302010-10-20 06:51:55 +00001937 pr_err("Device allocation failed\n");
Jean Delvare787c72b2007-05-08 17:22:00 +02001938 goto exit;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001939 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001940
Jean Delvare787c72b2007-05-08 17:22:00 +02001941 err = platform_device_add_resources(pdev, &res, 1);
1942 if (err) {
Joe Perches18de0302010-10-20 06:51:55 +00001943 pr_err("Device resource addition failed (%d)\n", err);
Jean Delvare787c72b2007-05-08 17:22:00 +02001944 goto exit_device_put;
1945 }
1946
Jean Delvare2df6d812007-06-09 10:11:16 -04001947 err = platform_device_add_data(pdev, sio_data,
1948 sizeof(struct w83627hf_sio_data));
1949 if (err) {
Joe Perches18de0302010-10-20 06:51:55 +00001950 pr_err("Platform data allocation failed\n");
Jean Delvare787c72b2007-05-08 17:22:00 +02001951 goto exit_device_put;
1952 }
Jean Delvare787c72b2007-05-08 17:22:00 +02001953
1954 err = platform_device_add(pdev);
1955 if (err) {
Joe Perches18de0302010-10-20 06:51:55 +00001956 pr_err("Device addition failed (%d)\n", err);
Jean Delvare787c72b2007-05-08 17:22:00 +02001957 goto exit_device_put;
1958 }
1959
1960 return 0;
1961
1962exit_device_put:
1963 platform_device_put(pdev);
1964exit:
1965 return err;
1966}
1967
1968static int __init sensors_w83627hf_init(void)
1969{
1970 int err;
1971 unsigned short address;
1972 struct w83627hf_sio_data sio_data;
1973
1974 if (w83627hf_find(0x2e, &address, &sio_data)
1975 && w83627hf_find(0x4e, &address, &sio_data))
1976 return -ENODEV;
1977
1978 err = platform_driver_register(&w83627hf_driver);
1979 if (err)
1980 goto exit;
1981
1982 /* Sets global pdev as a side effect */
1983 err = w83627hf_device_add(address, &sio_data);
1984 if (err)
1985 goto exit_driver;
1986
1987 return 0;
1988
1989exit_driver:
1990 platform_driver_unregister(&w83627hf_driver);
1991exit:
1992 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001993}
1994
1995static void __exit sensors_w83627hf_exit(void)
1996{
Jean Delvare787c72b2007-05-08 17:22:00 +02001997 platform_device_unregister(pdev);
1998 platform_driver_unregister(&w83627hf_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001999}
2000
2001MODULE_AUTHOR("Frodo Looijaard <frodol@dds.nl>, "
2002 "Philip Edelbrock <phil@netroedge.com>, "
2003 "and Mark Studebaker <mdsxyz123@yahoo.com>");
2004MODULE_DESCRIPTION("W83627HF driver");
2005MODULE_LICENSE("GPL");
2006
2007module_init(sensors_w83627hf_init);
2008module_exit(sensors_w83627hf_exit);