blob: 97bbf1af11e806b400c905fc6b6fd1b2b11b51ce [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 Delvare7c81c60f2014-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
Jean Delvare787c72b2007-05-08 17:22:00 +0200399static int w83627hf_probe(struct platform_device *pdev);
Bill Pemberton281dfd02012-11-19 13:25:51 -0500400static int w83627hf_remove(struct platform_device *pdev);
Jean Delvare787c72b2007-05-08 17:22:00 +0200401
402static int w83627hf_read_value(struct w83627hf_data *data, u16 reg);
403static int w83627hf_write_value(struct w83627hf_data *data, u16 reg, u16 value);
Jean Delvarec09c5182007-10-12 21:53:07 +0200404static void w83627hf_update_fan_div(struct w83627hf_data *data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405static struct w83627hf_data *w83627hf_update_device(struct device *dev);
Jean Delvare787c72b2007-05-08 17:22:00 +0200406static void w83627hf_init_device(struct platform_device *pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407
Jean Delvare275b7d62012-12-19 22:16:59 +0100408#ifdef CONFIG_PM
409static int w83627hf_suspend(struct device *dev)
410{
411 struct w83627hf_data *data = w83627hf_update_device(dev);
412
413 mutex_lock(&data->update_lock);
414 data->scfg1 = w83627hf_read_value(data, W83781D_REG_SCFG1);
415 data->scfg2 = w83627hf_read_value(data, W83781D_REG_SCFG2);
416 mutex_unlock(&data->update_lock);
417
418 return 0;
419}
420
421static int w83627hf_resume(struct device *dev)
422{
423 struct w83627hf_data *data = dev_get_drvdata(dev);
424 int i, num_temps = (data->type == w83697hf) ? 2 : 3;
425
426 /* Restore limits */
427 mutex_lock(&data->update_lock);
428 for (i = 0; i <= 8; i++) {
429 /* skip missing sensors */
430 if (((data->type == w83697hf) && (i == 1)) ||
431 ((data->type != w83627hf && data->type != w83697hf)
432 && (i == 5 || i == 6)))
433 continue;
434 w83627hf_write_value(data, W83781D_REG_IN_MAX(i),
435 data->in_max[i]);
436 w83627hf_write_value(data, W83781D_REG_IN_MIN(i),
437 data->in_min[i]);
438 }
439 for (i = 0; i <= 2; i++)
440 w83627hf_write_value(data, W83627HF_REG_FAN_MIN(i),
441 data->fan_min[i]);
442 for (i = 0; i < num_temps; i++) {
443 w83627hf_write_value(data, w83627hf_reg_temp_over[i],
444 data->temp_max[i]);
445 w83627hf_write_value(data, w83627hf_reg_temp_hyst[i],
446 data->temp_max_hyst[i]);
447 }
448
449 /* Fixup BIOS bugs */
450 if (data->type == w83627thf || data->type == w83637hf ||
451 data->type == w83687thf)
452 w83627hf_write_value(data, W83627THF_REG_VRM_OVT_CFG,
453 data->vrm_ovt);
454 w83627hf_write_value(data, W83781D_REG_SCFG1, data->scfg1);
455 w83627hf_write_value(data, W83781D_REG_SCFG2, data->scfg2);
456
457 /* Force re-reading all values */
458 data->valid = 0;
459 mutex_unlock(&data->update_lock);
460
461 return 0;
462}
463
464static const struct dev_pm_ops w83627hf_dev_pm_ops = {
465 .suspend = w83627hf_suspend,
466 .resume = w83627hf_resume,
467};
468
469#define W83627HF_DEV_PM_OPS (&w83627hf_dev_pm_ops)
470#else
471#define W83627HF_DEV_PM_OPS NULL
472#endif /* CONFIG_PM */
473
Jean Delvare787c72b2007-05-08 17:22:00 +0200474static struct platform_driver w83627hf_driver = {
Laurent Riffardcdaf7932005-11-26 20:37:41 +0100475 .driver = {
Jean Delvared27c37c2007-05-08 17:21:59 +0200476 .name = DRVNAME,
Jean Delvare275b7d62012-12-19 22:16:59 +0100477 .pm = W83627HF_DEV_PM_OPS,
Laurent Riffardcdaf7932005-11-26 20:37:41 +0100478 },
Jean Delvare787c72b2007-05-08 17:22:00 +0200479 .probe = w83627hf_probe,
Bill Pemberton9e5e9b72012-11-19 13:21:20 -0500480 .remove = w83627hf_remove,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481};
482
Jim Cromie07584c72007-10-12 21:08:00 +0200483static ssize_t
Guenter Roeck502a92f2019-01-22 15:25:47 -0800484in_input_show(struct device *dev, struct device_attribute *devattr, char *buf)
Jim Cromie07584c72007-10-12 21:08:00 +0200485{
486 int nr = to_sensor_dev_attr(devattr)->index;
487 struct w83627hf_data *data = w83627hf_update_device(dev);
488 return sprintf(buf, "%ld\n", (long)IN_FROM_REG(data->in[nr]));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489}
Jim Cromie07584c72007-10-12 21:08:00 +0200490static ssize_t
Guenter Roeck502a92f2019-01-22 15:25:47 -0800491in_min_show(struct device *dev, struct device_attribute *devattr, char *buf)
Jim Cromie07584c72007-10-12 21:08:00 +0200492{
493 int nr = to_sensor_dev_attr(devattr)->index;
494 struct w83627hf_data *data = w83627hf_update_device(dev);
495 return sprintf(buf, "%ld\n", (long)IN_FROM_REG(data->in_min[nr]));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496}
Jim Cromie07584c72007-10-12 21:08:00 +0200497static ssize_t
Guenter Roeck502a92f2019-01-22 15:25:47 -0800498in_max_show(struct device *dev, struct device_attribute *devattr, char *buf)
Jim Cromie07584c72007-10-12 21:08:00 +0200499{
500 int nr = to_sensor_dev_attr(devattr)->index;
501 struct w83627hf_data *data = w83627hf_update_device(dev);
502 return sprintf(buf, "%ld\n", (long)IN_FROM_REG(data->in_max[nr]));
503}
504static ssize_t
Guenter Roeck502a92f2019-01-22 15:25:47 -0800505in_min_store(struct device *dev, struct device_attribute *devattr,
Jim Cromie07584c72007-10-12 21:08:00 +0200506 const char *buf, size_t count)
507{
508 int nr = to_sensor_dev_attr(devattr)->index;
509 struct w83627hf_data *data = dev_get_drvdata(dev);
Guenter Roeck27b9de32012-01-15 11:07:26 -0800510 long val;
511 int err;
512
513 err = kstrtol(buf, 10, &val);
514 if (err)
515 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516
Jim Cromie07584c72007-10-12 21:08:00 +0200517 mutex_lock(&data->update_lock);
518 data->in_min[nr] = IN_TO_REG(val);
519 w83627hf_write_value(data, W83781D_REG_IN_MIN(nr), data->in_min[nr]);
520 mutex_unlock(&data->update_lock);
521 return count;
522}
523static ssize_t
Guenter Roeck502a92f2019-01-22 15:25:47 -0800524in_max_store(struct device *dev, struct device_attribute *devattr,
Jim Cromie07584c72007-10-12 21:08:00 +0200525 const char *buf, size_t count)
526{
527 int nr = to_sensor_dev_attr(devattr)->index;
528 struct w83627hf_data *data = dev_get_drvdata(dev);
Guenter Roeck27b9de32012-01-15 11:07:26 -0800529 long val;
530 int err;
531
532 err = kstrtol(buf, 10, &val);
533 if (err)
534 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535
Jim Cromie07584c72007-10-12 21:08:00 +0200536 mutex_lock(&data->update_lock);
537 data->in_max[nr] = IN_TO_REG(val);
538 w83627hf_write_value(data, W83781D_REG_IN_MAX(nr), data->in_max[nr]);
539 mutex_unlock(&data->update_lock);
540 return count;
541}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542
Guenter Roeck502a92f2019-01-22 15:25:47 -0800543static SENSOR_DEVICE_ATTR_RO(in1_input, in_input, 1);
544static SENSOR_DEVICE_ATTR_RW(in1_min, in_min, 1);
545static SENSOR_DEVICE_ATTR_RW(in1_max, in_max, 1);
546static SENSOR_DEVICE_ATTR_RO(in2_input, in_input, 2);
547static SENSOR_DEVICE_ATTR_RW(in2_min, in_min, 2);
548static SENSOR_DEVICE_ATTR_RW(in2_max, in_max, 2);
549static SENSOR_DEVICE_ATTR_RO(in3_input, in_input, 3);
550static SENSOR_DEVICE_ATTR_RW(in3_min, in_min, 3);
551static SENSOR_DEVICE_ATTR_RW(in3_max, in_max, 3);
552static SENSOR_DEVICE_ATTR_RO(in4_input, in_input, 4);
553static SENSOR_DEVICE_ATTR_RW(in4_min, in_min, 4);
554static SENSOR_DEVICE_ATTR_RW(in4_max, in_max, 4);
555static SENSOR_DEVICE_ATTR_RO(in5_input, in_input, 5);
556static SENSOR_DEVICE_ATTR_RW(in5_min, in_min, 5);
557static SENSOR_DEVICE_ATTR_RW(in5_max, in_max, 5);
558static SENSOR_DEVICE_ATTR_RO(in6_input, in_input, 6);
559static SENSOR_DEVICE_ATTR_RW(in6_min, in_min, 6);
560static SENSOR_DEVICE_ATTR_RW(in6_max, in_max, 6);
561static SENSOR_DEVICE_ATTR_RO(in7_input, in_input, 7);
562static SENSOR_DEVICE_ATTR_RW(in7_min, in_min, 7);
563static SENSOR_DEVICE_ATTR_RW(in7_max, in_max, 7);
564static SENSOR_DEVICE_ATTR_RO(in8_input, in_input, 8);
565static SENSOR_DEVICE_ATTR_RW(in8_min, in_min, 8);
566static SENSOR_DEVICE_ATTR_RW(in8_max, in_max, 8);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567
568/* use a different set of functions for in0 */
569static ssize_t show_in_0(struct w83627hf_data *data, char *buf, u8 reg)
570{
571 long in0;
572
573 if ((data->vrm_ovt & 0x01) &&
Jean Delvarec2db6ce2006-01-18 23:22:12 +0100574 (w83627thf == data->type || w83637hf == data->type
575 || w83687thf == data->type))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576
577 /* use VRM9 calculation */
578 in0 = (long)((reg * 488 + 70000 + 50) / 100);
579 else
580 /* use VRM8 (standard) calculation */
581 in0 = (long)IN_FROM_REG(reg);
582
583 return sprintf(buf,"%ld\n", in0);
584}
585
Julia Lawall8dfcdfc2016-12-22 13:05:11 +0100586static ssize_t in0_input_show(struct device *dev,
587 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588{
589 struct w83627hf_data *data = w83627hf_update_device(dev);
590 return show_in_0(data, buf, data->in[0]);
591}
592
Julia Lawall8dfcdfc2016-12-22 13:05:11 +0100593static ssize_t in0_min_show(struct device *dev, struct device_attribute *attr,
594 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595{
596 struct w83627hf_data *data = w83627hf_update_device(dev);
597 return show_in_0(data, buf, data->in_min[0]);
598}
599
Julia Lawall8dfcdfc2016-12-22 13:05:11 +0100600static ssize_t in0_max_show(struct device *dev, struct device_attribute *attr,
601 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602{
603 struct w83627hf_data *data = w83627hf_update_device(dev);
604 return show_in_0(data, buf, data->in_max[0]);
605}
606
Julia Lawall8dfcdfc2016-12-22 13:05:11 +0100607static ssize_t in0_min_store(struct device *dev,
608 struct device_attribute *attr, const char *buf,
609 size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610{
Jean Delvare787c72b2007-05-08 17:22:00 +0200611 struct w83627hf_data *data = dev_get_drvdata(dev);
Guenter Roeck27b9de32012-01-15 11:07:26 -0800612 unsigned long val;
613 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614
Guenter Roeck27b9de32012-01-15 11:07:26 -0800615 err = kstrtoul(buf, 10, &val);
616 if (err)
617 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100619 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620
621 if ((data->vrm_ovt & 0x01) &&
Jean Delvarec2db6ce2006-01-18 23:22:12 +0100622 (w83627thf == data->type || w83637hf == data->type
623 || w83687thf == data->type))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624
625 /* use VRM9 calculation */
Yuan Mu2723ab92005-11-23 15:44:21 -0800626 data->in_min[0] =
Guenter Roeck2a844c12013-01-09 08:09:34 -0800627 clamp_val(((val * 100) - 70000 + 244) / 488, 0, 255);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628 else
629 /* use VRM8 (standard) calculation */
630 data->in_min[0] = IN_TO_REG(val);
631
Jean Delvare787c72b2007-05-08 17:22:00 +0200632 w83627hf_write_value(data, W83781D_REG_IN_MIN(0), data->in_min[0]);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100633 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634 return count;
635}
636
Julia Lawall8dfcdfc2016-12-22 13:05:11 +0100637static ssize_t in0_max_store(struct device *dev,
638 struct device_attribute *attr, const char *buf,
639 size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640{
Jean Delvare787c72b2007-05-08 17:22:00 +0200641 struct w83627hf_data *data = dev_get_drvdata(dev);
Guenter Roeck27b9de32012-01-15 11:07:26 -0800642 unsigned long val;
643 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644
Guenter Roeck27b9de32012-01-15 11:07:26 -0800645 err = kstrtoul(buf, 10, &val);
646 if (err)
647 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100649 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650
651 if ((data->vrm_ovt & 0x01) &&
Jean Delvarec2db6ce2006-01-18 23:22:12 +0100652 (w83627thf == data->type || w83637hf == data->type
653 || w83687thf == data->type))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654
655 /* use VRM9 calculation */
Yuan Mu2723ab92005-11-23 15:44:21 -0800656 data->in_max[0] =
Guenter Roeck2a844c12013-01-09 08:09:34 -0800657 clamp_val(((val * 100) - 70000 + 244) / 488, 0, 255);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658 else
659 /* use VRM8 (standard) calculation */
660 data->in_max[0] = IN_TO_REG(val);
661
Jean Delvare787c72b2007-05-08 17:22:00 +0200662 w83627hf_write_value(data, W83781D_REG_IN_MAX(0), data->in_max[0]);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100663 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664 return count;
665}
666
Julia Lawall8dfcdfc2016-12-22 13:05:11 +0100667static DEVICE_ATTR_RO(in0_input);
668static DEVICE_ATTR_RW(in0_min);
669static DEVICE_ATTR_RW(in0_max);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671static ssize_t
Guenter Roeck502a92f2019-01-22 15:25:47 -0800672fan_input_show(struct device *dev, struct device_attribute *devattr,
673 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674{
Jim Cromie07584c72007-10-12 21:08:00 +0200675 int nr = to_sensor_dev_attr(devattr)->index;
676 struct w83627hf_data *data = w83627hf_update_device(dev);
677 return sprintf(buf, "%ld\n", FAN_FROM_REG(data->fan[nr],
678 (long)DIV_FROM_REG(data->fan_div[nr])));
679}
680static ssize_t
Guenter Roeck502a92f2019-01-22 15:25:47 -0800681fan_min_show(struct device *dev, struct device_attribute *devattr, char *buf)
Jim Cromie07584c72007-10-12 21:08:00 +0200682{
683 int nr = to_sensor_dev_attr(devattr)->index;
684 struct w83627hf_data *data = w83627hf_update_device(dev);
685 return sprintf(buf, "%ld\n", FAN_FROM_REG(data->fan_min[nr],
686 (long)DIV_FROM_REG(data->fan_div[nr])));
687}
688static ssize_t
Guenter Roeck502a92f2019-01-22 15:25:47 -0800689fan_min_store(struct device *dev, struct device_attribute *devattr,
Jim Cromie07584c72007-10-12 21:08:00 +0200690 const char *buf, size_t count)
691{
692 int nr = to_sensor_dev_attr(devattr)->index;
Jean Delvare787c72b2007-05-08 17:22:00 +0200693 struct w83627hf_data *data = dev_get_drvdata(dev);
Guenter Roeck27b9de32012-01-15 11:07:26 -0800694 unsigned long val;
695 int err;
696
697 err = kstrtoul(buf, 10, &val);
698 if (err)
699 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100701 mutex_lock(&data->update_lock);
Jim Cromie07584c72007-10-12 21:08:00 +0200702 data->fan_min[nr] = FAN_TO_REG(val, DIV_FROM_REG(data->fan_div[nr]));
Jim Cromie2ca2fcd2007-10-14 17:20:50 -0600703 w83627hf_write_value(data, W83627HF_REG_FAN_MIN(nr),
Jim Cromie07584c72007-10-12 21:08:00 +0200704 data->fan_min[nr]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100706 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707 return count;
708}
709
Guenter Roeck502a92f2019-01-22 15:25:47 -0800710static SENSOR_DEVICE_ATTR_RO(fan1_input, fan_input, 0);
711static SENSOR_DEVICE_ATTR_RW(fan1_min, fan_min, 0);
712static SENSOR_DEVICE_ATTR_RO(fan2_input, fan_input, 1);
713static SENSOR_DEVICE_ATTR_RW(fan2_min, fan_min, 1);
714static SENSOR_DEVICE_ATTR_RO(fan3_input, fan_input, 2);
715static SENSOR_DEVICE_ATTR_RW(fan3_min, fan_min, 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716
Jim Cromie07584c72007-10-12 21:08:00 +0200717static ssize_t
Guenter Roeck502a92f2019-01-22 15:25:47 -0800718temp_show(struct device *dev, struct device_attribute *devattr, char *buf)
Jim Cromie07584c72007-10-12 21:08:00 +0200719{
720 int nr = to_sensor_dev_attr(devattr)->index;
721 struct w83627hf_data *data = w83627hf_update_device(dev);
Jim Cromiedf48ed82007-10-14 17:10:52 -0600722
723 u16 tmp = data->temp[nr];
724 return sprintf(buf, "%ld\n", (nr) ? (long) LM75_TEMP_FROM_REG(tmp)
725 : (long) TEMP_FROM_REG(tmp));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727
Jim Cromie07584c72007-10-12 21:08:00 +0200728static ssize_t
Guenter Roeck502a92f2019-01-22 15:25:47 -0800729temp_max_show(struct device *dev, struct device_attribute *devattr, char *buf)
Jim Cromie07584c72007-10-12 21:08:00 +0200730{
731 int nr = to_sensor_dev_attr(devattr)->index;
732 struct w83627hf_data *data = w83627hf_update_device(dev);
Jim Cromiedf48ed82007-10-14 17:10:52 -0600733
734 u16 tmp = data->temp_max[nr];
735 return sprintf(buf, "%ld\n", (nr) ? (long) LM75_TEMP_FROM_REG(tmp)
736 : (long) TEMP_FROM_REG(tmp));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738
Jim Cromie07584c72007-10-12 21:08:00 +0200739static ssize_t
Guenter Roeck502a92f2019-01-22 15:25:47 -0800740temp_max_hyst_show(struct device *dev, struct device_attribute *devattr,
Jim Cromie07584c72007-10-12 21:08:00 +0200741 char *buf)
742{
743 int nr = to_sensor_dev_attr(devattr)->index;
744 struct w83627hf_data *data = w83627hf_update_device(dev);
Jim Cromiedf48ed82007-10-14 17:10:52 -0600745
746 u16 tmp = data->temp_max_hyst[nr];
747 return sprintf(buf, "%ld\n", (nr) ? (long) LM75_TEMP_FROM_REG(tmp)
748 : (long) TEMP_FROM_REG(tmp));
Jim Cromie07584c72007-10-12 21:08:00 +0200749}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750
Jim Cromie07584c72007-10-12 21:08:00 +0200751static ssize_t
Guenter Roeck502a92f2019-01-22 15:25:47 -0800752temp_max_store(struct device *dev, struct device_attribute *devattr,
Jim Cromie07584c72007-10-12 21:08:00 +0200753 const char *buf, size_t count)
754{
755 int nr = to_sensor_dev_attr(devattr)->index;
756 struct w83627hf_data *data = dev_get_drvdata(dev);
Guenter Roeck27b9de32012-01-15 11:07:26 -0800757 u16 tmp;
758 long val;
759 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760
Guenter Roeck27b9de32012-01-15 11:07:26 -0800761 err = kstrtol(buf, 10, &val);
762 if (err)
763 return err;
764
765 tmp = (nr) ? LM75_TEMP_TO_REG(val) : TEMP_TO_REG(val);
Jim Cromie07584c72007-10-12 21:08:00 +0200766 mutex_lock(&data->update_lock);
Jim Cromiedf48ed82007-10-14 17:10:52 -0600767 data->temp_max[nr] = tmp;
768 w83627hf_write_value(data, w83627hf_reg_temp_over[nr], tmp);
Jim Cromie07584c72007-10-12 21:08:00 +0200769 mutex_unlock(&data->update_lock);
770 return count;
771}
772
773static ssize_t
Guenter Roeck502a92f2019-01-22 15:25:47 -0800774temp_max_hyst_store(struct device *dev, struct device_attribute *devattr,
Jim Cromie07584c72007-10-12 21:08:00 +0200775 const char *buf, size_t count)
776{
777 int nr = to_sensor_dev_attr(devattr)->index;
778 struct w83627hf_data *data = dev_get_drvdata(dev);
Guenter Roeck27b9de32012-01-15 11:07:26 -0800779 u16 tmp;
780 long val;
781 int err;
Jim Cromie07584c72007-10-12 21:08:00 +0200782
Guenter Roeck27b9de32012-01-15 11:07:26 -0800783 err = kstrtol(buf, 10, &val);
784 if (err)
785 return err;
786
787 tmp = (nr) ? LM75_TEMP_TO_REG(val) : TEMP_TO_REG(val);
Jim Cromie07584c72007-10-12 21:08:00 +0200788 mutex_lock(&data->update_lock);
Jim Cromiedf48ed82007-10-14 17:10:52 -0600789 data->temp_max_hyst[nr] = tmp;
790 w83627hf_write_value(data, w83627hf_reg_temp_hyst[nr], tmp);
Jim Cromie07584c72007-10-12 21:08:00 +0200791 mutex_unlock(&data->update_lock);
792 return count;
793}
794
Guenter Roeck502a92f2019-01-22 15:25:47 -0800795static SENSOR_DEVICE_ATTR_RO(temp1_input, temp, 0);
796static SENSOR_DEVICE_ATTR_RW(temp1_max, temp_max, 0);
797static SENSOR_DEVICE_ATTR_RW(temp1_max_hyst, temp_max_hyst, 0);
798static SENSOR_DEVICE_ATTR_RO(temp2_input, temp, 1);
799static SENSOR_DEVICE_ATTR_RW(temp2_max, temp_max, 1);
800static SENSOR_DEVICE_ATTR_RW(temp2_max_hyst, temp_max_hyst, 1);
801static SENSOR_DEVICE_ATTR_RO(temp3_input, temp, 2);
802static SENSOR_DEVICE_ATTR_RW(temp3_max, temp_max, 2);
803static SENSOR_DEVICE_ATTR_RW(temp3_max_hyst, temp_max_hyst, 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805static ssize_t
Julia Lawall8dfcdfc2016-12-22 13:05:11 +0100806cpu0_vid_show(struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807{
808 struct w83627hf_data *data = w83627hf_update_device(dev);
809 return sprintf(buf, "%ld\n", (long) vid_from_reg(data->vid, data->vrm));
810}
Julia Lawall8dfcdfc2016-12-22 13:05:11 +0100811static DEVICE_ATTR_RO(cpu0_vid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812
813static ssize_t
Julia Lawall8dfcdfc2016-12-22 13:05:11 +0100814vrm_show(struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815{
Jean Delvare90d66192007-10-08 18:24:35 +0200816 struct w83627hf_data *data = dev_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817 return sprintf(buf, "%ld\n", (long) data->vrm);
818}
819static ssize_t
Julia Lawall8dfcdfc2016-12-22 13:05:11 +0100820vrm_store(struct device *dev, struct device_attribute *attr, const char *buf,
821 size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822{
Jean Delvare787c72b2007-05-08 17:22:00 +0200823 struct w83627hf_data *data = dev_get_drvdata(dev);
Guenter Roeck27b9de32012-01-15 11:07:26 -0800824 unsigned long val;
825 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826
Guenter Roeck27b9de32012-01-15 11:07:26 -0800827 err = kstrtoul(buf, 10, &val);
828 if (err)
829 return err;
Axel Lin970255b2014-08-06 08:27:11 +0800830
831 if (val > 255)
832 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833 data->vrm = val;
834
835 return count;
836}
Julia Lawall8dfcdfc2016-12-22 13:05:11 +0100837static DEVICE_ATTR_RW(vrm);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838
839static ssize_t
Julia Lawall8dfcdfc2016-12-22 13:05:11 +0100840alarms_show(struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841{
842 struct w83627hf_data *data = w83627hf_update_device(dev);
843 return sprintf(buf, "%ld\n", (long) data->alarms);
844}
Julia Lawall8dfcdfc2016-12-22 13:05:11 +0100845static DEVICE_ATTR_RO(alarms);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846
Jean Delvaree3604c62008-01-03 23:00:30 +0100847static ssize_t
Guenter Roeck502a92f2019-01-22 15:25:47 -0800848alarm_show(struct device *dev, struct device_attribute *attr, char *buf)
Jean Delvaree3604c62008-01-03 23:00:30 +0100849{
850 struct w83627hf_data *data = w83627hf_update_device(dev);
851 int bitnr = to_sensor_dev_attr(attr)->index;
852 return sprintf(buf, "%u\n", (data->alarms >> bitnr) & 1);
853}
Guenter Roeck502a92f2019-01-22 15:25:47 -0800854static SENSOR_DEVICE_ATTR_RO(in0_alarm, alarm, 0);
855static SENSOR_DEVICE_ATTR_RO(in1_alarm, alarm, 1);
856static SENSOR_DEVICE_ATTR_RO(in2_alarm, alarm, 2);
857static SENSOR_DEVICE_ATTR_RO(in3_alarm, alarm, 3);
858static SENSOR_DEVICE_ATTR_RO(in4_alarm, alarm, 8);
859static SENSOR_DEVICE_ATTR_RO(in5_alarm, alarm, 9);
860static SENSOR_DEVICE_ATTR_RO(in6_alarm, alarm, 10);
861static SENSOR_DEVICE_ATTR_RO(in7_alarm, alarm, 16);
862static SENSOR_DEVICE_ATTR_RO(in8_alarm, alarm, 17);
863static SENSOR_DEVICE_ATTR_RO(fan1_alarm, alarm, 6);
864static SENSOR_DEVICE_ATTR_RO(fan2_alarm, alarm, 7);
865static SENSOR_DEVICE_ATTR_RO(fan3_alarm, alarm, 11);
866static SENSOR_DEVICE_ATTR_RO(temp1_alarm, alarm, 4);
867static SENSOR_DEVICE_ATTR_RO(temp2_alarm, alarm, 5);
868static SENSOR_DEVICE_ATTR_RO(temp3_alarm, alarm, 13);
Jean Delvaree3604c62008-01-03 23:00:30 +0100869
Jean Delvare1c138102008-01-03 23:04:55 +0100870static ssize_t
Julia Lawall8dfcdfc2016-12-22 13:05:11 +0100871beep_mask_show(struct device *dev, struct device_attribute *attr, char *buf)
Jean Delvare1c138102008-01-03 23:04:55 +0100872{
873 struct w83627hf_data *data = w83627hf_update_device(dev);
874 return sprintf(buf, "%ld\n",
875 (long)BEEP_MASK_FROM_REG(data->beep_mask));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877
878static ssize_t
Julia Lawall8dfcdfc2016-12-22 13:05:11 +0100879beep_mask_store(struct device *dev, struct device_attribute *attr,
Jean Delvare1c138102008-01-03 23:04:55 +0100880 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881{
Jean Delvare787c72b2007-05-08 17:22:00 +0200882 struct w83627hf_data *data = dev_get_drvdata(dev);
Jean Delvare1c138102008-01-03 23:04:55 +0100883 unsigned long val;
Guenter Roeck27b9de32012-01-15 11:07:26 -0800884 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885
Guenter Roeck27b9de32012-01-15 11:07:26 -0800886 err = kstrtoul(buf, 10, &val);
887 if (err)
888 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100890 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891
Jean Delvare1c138102008-01-03 23:04:55 +0100892 /* preserve beep enable */
893 data->beep_mask = (data->beep_mask & 0x8000)
894 | BEEP_MASK_TO_REG(val);
895 w83627hf_write_value(data, W83781D_REG_BEEP_INTS1,
896 data->beep_mask & 0xff);
897 w83627hf_write_value(data, W83781D_REG_BEEP_INTS3,
898 ((data->beep_mask) >> 16) & 0xff);
Jean Delvare787c72b2007-05-08 17:22:00 +0200899 w83627hf_write_value(data, W83781D_REG_BEEP_INTS2,
Jean Delvare1c138102008-01-03 23:04:55 +0100900 (data->beep_mask >> 8) & 0xff);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100902 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903 return count;
904}
905
Julia Lawall8dfcdfc2016-12-22 13:05:11 +0100906static DEVICE_ATTR_RW(beep_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908static ssize_t
Guenter Roeck502a92f2019-01-22 15:25:47 -0800909beep_show(struct device *dev, struct device_attribute *attr, char *buf)
Jean Delvaree3604c62008-01-03 23:00:30 +0100910{
911 struct w83627hf_data *data = w83627hf_update_device(dev);
912 int bitnr = to_sensor_dev_attr(attr)->index;
913 return sprintf(buf, "%u\n", (data->beep_mask >> bitnr) & 1);
914}
915
916static ssize_t
Guenter Roeck502a92f2019-01-22 15:25:47 -0800917beep_store(struct device *dev, struct device_attribute *attr, const char *buf,
918 size_t count)
Jean Delvaree3604c62008-01-03 23:00:30 +0100919{
920 struct w83627hf_data *data = dev_get_drvdata(dev);
921 int bitnr = to_sensor_dev_attr(attr)->index;
Jean Delvaree3604c62008-01-03 23:00:30 +0100922 u8 reg;
Guenter Roeck27b9de32012-01-15 11:07:26 -0800923 unsigned long bit;
924 int err;
Jean Delvaree3604c62008-01-03 23:00:30 +0100925
Guenter Roeck27b9de32012-01-15 11:07:26 -0800926 err = kstrtoul(buf, 10, &bit);
927 if (err)
928 return err;
929
Jean Delvaree3604c62008-01-03 23:00:30 +0100930 if (bit & ~1)
931 return -EINVAL;
932
933 mutex_lock(&data->update_lock);
934 if (bit)
935 data->beep_mask |= (1 << bitnr);
936 else
937 data->beep_mask &= ~(1 << bitnr);
938
939 if (bitnr < 8) {
940 reg = w83627hf_read_value(data, W83781D_REG_BEEP_INTS1);
941 if (bit)
942 reg |= (1 << bitnr);
943 else
944 reg &= ~(1 << bitnr);
945 w83627hf_write_value(data, W83781D_REG_BEEP_INTS1, reg);
946 } else if (bitnr < 16) {
947 reg = w83627hf_read_value(data, W83781D_REG_BEEP_INTS2);
948 if (bit)
949 reg |= (1 << (bitnr - 8));
950 else
951 reg &= ~(1 << (bitnr - 8));
952 w83627hf_write_value(data, W83781D_REG_BEEP_INTS2, reg);
953 } else {
954 reg = w83627hf_read_value(data, W83781D_REG_BEEP_INTS3);
955 if (bit)
956 reg |= (1 << (bitnr - 16));
957 else
958 reg &= ~(1 << (bitnr - 16));
959 w83627hf_write_value(data, W83781D_REG_BEEP_INTS3, reg);
960 }
961 mutex_unlock(&data->update_lock);
962
963 return count;
964}
965
Guenter Roeck502a92f2019-01-22 15:25:47 -0800966static SENSOR_DEVICE_ATTR_RW(in0_beep, beep, 0);
967static SENSOR_DEVICE_ATTR_RW(in1_beep, beep, 1);
968static SENSOR_DEVICE_ATTR_RW(in2_beep, beep, 2);
969static SENSOR_DEVICE_ATTR_RW(in3_beep, beep, 3);
970static SENSOR_DEVICE_ATTR_RW(in4_beep, beep, 8);
971static SENSOR_DEVICE_ATTR_RW(in5_beep, beep, 9);
972static SENSOR_DEVICE_ATTR_RW(in6_beep, beep, 10);
973static SENSOR_DEVICE_ATTR_RW(in7_beep, beep, 16);
974static SENSOR_DEVICE_ATTR_RW(in8_beep, beep, 17);
975static SENSOR_DEVICE_ATTR_RW(fan1_beep, beep, 6);
976static SENSOR_DEVICE_ATTR_RW(fan2_beep, beep, 7);
977static SENSOR_DEVICE_ATTR_RW(fan3_beep, beep, 11);
978static SENSOR_DEVICE_ATTR_RW(temp1_beep, beep, 4);
979static SENSOR_DEVICE_ATTR_RW(temp2_beep, beep, 5);
980static SENSOR_DEVICE_ATTR_RW(temp3_beep, beep, 13);
981static SENSOR_DEVICE_ATTR_RW(beep_enable, beep, 15);
Jean Delvaree3604c62008-01-03 23:00:30 +0100982
983static ssize_t
Guenter Roeck502a92f2019-01-22 15:25:47 -0800984fan_div_show(struct device *dev, struct device_attribute *devattr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985{
Jim Cromie07584c72007-10-12 21:08:00 +0200986 int nr = to_sensor_dev_attr(devattr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700987 struct w83627hf_data *data = w83627hf_update_device(dev);
988 return sprintf(buf, "%ld\n",
Jim Cromie07584c72007-10-12 21:08:00 +0200989 (long) DIV_FROM_REG(data->fan_div[nr]));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990}
Guenter Roeck27b9de32012-01-15 11:07:26 -0800991/*
992 * Note: we save and restore the fan minimum here, because its value is
993 * determined in part by the fan divisor. This follows the principle of
994 * least surprise; the user doesn't expect the fan minimum to change just
995 * because the divisor changed.
996 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997static ssize_t
Guenter Roeck502a92f2019-01-22 15:25:47 -0800998fan_div_store(struct device *dev, struct device_attribute *devattr,
Jim Cromie07584c72007-10-12 21:08:00 +0200999 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000{
Jim Cromie07584c72007-10-12 21:08:00 +02001001 int nr = to_sensor_dev_attr(devattr)->index;
Jean Delvare787c72b2007-05-08 17:22:00 +02001002 struct w83627hf_data *data = dev_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001003 unsigned long min;
1004 u8 reg;
Guenter Roeck27b9de32012-01-15 11:07:26 -08001005 unsigned long val;
1006 int err;
1007
1008 err = kstrtoul(buf, 10, &val);
1009 if (err)
1010 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001011
Ingo Molnar9a61bf62006-01-18 23:19:26 +01001012 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013
1014 /* Save fan_min */
1015 min = FAN_FROM_REG(data->fan_min[nr],
1016 DIV_FROM_REG(data->fan_div[nr]));
1017
1018 data->fan_div[nr] = DIV_TO_REG(val);
1019
Jean Delvare787c72b2007-05-08 17:22:00 +02001020 reg = (w83627hf_read_value(data, nr==2 ? W83781D_REG_PIN : W83781D_REG_VID_FANDIV)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021 & (nr==0 ? 0xcf : 0x3f))
1022 | ((data->fan_div[nr] & 0x03) << (nr==0 ? 4 : 6));
Jean Delvare787c72b2007-05-08 17:22:00 +02001023 w83627hf_write_value(data, nr==2 ? W83781D_REG_PIN : W83781D_REG_VID_FANDIV, reg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001024
Jean Delvare787c72b2007-05-08 17:22:00 +02001025 reg = (w83627hf_read_value(data, W83781D_REG_VBAT)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001026 & ~(1 << (5 + nr)))
1027 | ((data->fan_div[nr] & 0x04) << (3 + nr));
Jean Delvare787c72b2007-05-08 17:22:00 +02001028 w83627hf_write_value(data, W83781D_REG_VBAT, reg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001029
1030 /* Restore fan_min */
1031 data->fan_min[nr] = FAN_TO_REG(min, DIV_FROM_REG(data->fan_div[nr]));
Jim Cromie2ca2fcd2007-10-14 17:20:50 -06001032 w83627hf_write_value(data, W83627HF_REG_FAN_MIN(nr), data->fan_min[nr]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033
Ingo Molnar9a61bf62006-01-18 23:19:26 +01001034 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001035 return count;
1036}
1037
Guenter Roeck502a92f2019-01-22 15:25:47 -08001038static SENSOR_DEVICE_ATTR_RW(fan1_div, fan_div, 0);
1039static SENSOR_DEVICE_ATTR_RW(fan2_div, fan_div, 1);
1040static SENSOR_DEVICE_ATTR_RW(fan3_div, fan_div, 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001041
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042static ssize_t
Guenter Roeck502a92f2019-01-22 15:25:47 -08001043pwm_show(struct device *dev, struct device_attribute *devattr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001044{
Jim Cromie07584c72007-10-12 21:08:00 +02001045 int nr = to_sensor_dev_attr(devattr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046 struct w83627hf_data *data = w83627hf_update_device(dev);
Jim Cromie07584c72007-10-12 21:08:00 +02001047 return sprintf(buf, "%ld\n", (long) data->pwm[nr]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001048}
1049
1050static ssize_t
Guenter Roeck502a92f2019-01-22 15:25:47 -08001051pwm_store(struct device *dev, struct device_attribute *devattr,
Jim Cromie07584c72007-10-12 21:08:00 +02001052 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001053{
Jim Cromie07584c72007-10-12 21:08:00 +02001054 int nr = to_sensor_dev_attr(devattr)->index;
Jean Delvare787c72b2007-05-08 17:22:00 +02001055 struct w83627hf_data *data = dev_get_drvdata(dev);
Guenter Roeck27b9de32012-01-15 11:07:26 -08001056 unsigned long val;
1057 int err;
1058
1059 err = kstrtoul(buf, 10, &val);
1060 if (err)
1061 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062
Ingo Molnar9a61bf62006-01-18 23:19:26 +01001063 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064
1065 if (data->type == w83627thf) {
1066 /* bits 0-3 are reserved in 627THF */
Jim Cromie07584c72007-10-12 21:08:00 +02001067 data->pwm[nr] = PWM_TO_REG(val) & 0xf0;
Jean Delvare787c72b2007-05-08 17:22:00 +02001068 w83627hf_write_value(data,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001069 W836X7HF_REG_PWM(data->type, nr),
Jim Cromie07584c72007-10-12 21:08:00 +02001070 data->pwm[nr] |
Jean Delvare787c72b2007-05-08 17:22:00 +02001071 (w83627hf_read_value(data,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001072 W836X7HF_REG_PWM(data->type, nr)) & 0x0f));
1073 } else {
Jim Cromie07584c72007-10-12 21:08:00 +02001074 data->pwm[nr] = PWM_TO_REG(val);
Jean Delvare787c72b2007-05-08 17:22:00 +02001075 w83627hf_write_value(data,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001076 W836X7HF_REG_PWM(data->type, nr),
Jim Cromie07584c72007-10-12 21:08:00 +02001077 data->pwm[nr]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001078 }
1079
Ingo Molnar9a61bf62006-01-18 23:19:26 +01001080 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001081 return count;
1082}
1083
Guenter Roeck502a92f2019-01-22 15:25:47 -08001084static SENSOR_DEVICE_ATTR_RW(pwm1, pwm, 0);
1085static SENSOR_DEVICE_ATTR_RW(pwm2, pwm, 1);
1086static SENSOR_DEVICE_ATTR_RW(pwm3, pwm, 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001087
Linus Torvalds1da177e2005-04-16 15:20:36 -07001088static ssize_t
Guenter Roeck502a92f2019-01-22 15:25:47 -08001089pwm_enable_show(struct device *dev, struct device_attribute *devattr,
1090 char *buf)
Dominik Geyera95a5ed2008-08-06 22:41:04 +02001091{
1092 int nr = to_sensor_dev_attr(devattr)->index;
1093 struct w83627hf_data *data = w83627hf_update_device(dev);
1094 return sprintf(buf, "%d\n", data->pwm_enable[nr]);
1095}
1096
1097static ssize_t
Guenter Roeck502a92f2019-01-22 15:25:47 -08001098pwm_enable_store(struct device *dev, struct device_attribute *devattr,
1099 const char *buf, size_t count)
Dominik Geyera95a5ed2008-08-06 22:41:04 +02001100{
1101 int nr = to_sensor_dev_attr(devattr)->index;
1102 struct w83627hf_data *data = dev_get_drvdata(dev);
Dominik Geyera95a5ed2008-08-06 22:41:04 +02001103 u8 reg;
Guenter Roeck27b9de32012-01-15 11:07:26 -08001104 unsigned long val;
1105 int err;
Dominik Geyera95a5ed2008-08-06 22:41:04 +02001106
Guenter Roeck27b9de32012-01-15 11:07:26 -08001107 err = kstrtoul(buf, 10, &val);
1108 if (err)
1109 return err;
1110
1111 if (!val || val > 3) /* modes 1, 2 and 3 are supported */
Dominik Geyera95a5ed2008-08-06 22:41:04 +02001112 return -EINVAL;
1113 mutex_lock(&data->update_lock);
1114 data->pwm_enable[nr] = val;
1115 reg = w83627hf_read_value(data, W83627THF_REG_PWM_ENABLE[nr]);
1116 reg &= ~(0x03 << W83627THF_PWM_ENABLE_SHIFT[nr]);
1117 reg |= (val - 1) << W83627THF_PWM_ENABLE_SHIFT[nr];
1118 w83627hf_write_value(data, W83627THF_REG_PWM_ENABLE[nr], reg);
1119 mutex_unlock(&data->update_lock);
1120 return count;
1121}
1122
Guenter Roeck502a92f2019-01-22 15:25:47 -08001123static SENSOR_DEVICE_ATTR_RW(pwm1_enable, pwm_enable, 0);
1124static SENSOR_DEVICE_ATTR_RW(pwm2_enable, pwm_enable, 1);
1125static SENSOR_DEVICE_ATTR_RW(pwm3_enable, pwm_enable, 2);
Dominik Geyera95a5ed2008-08-06 22:41:04 +02001126
1127static ssize_t
Guenter Roeck502a92f2019-01-22 15:25:47 -08001128pwm_freq_show(struct device *dev, struct device_attribute *devattr, char *buf)
Carlos Olalla Martinez1550cb62007-06-09 10:11:16 -04001129{
Jim Cromie07584c72007-10-12 21:08:00 +02001130 int nr = to_sensor_dev_attr(devattr)->index;
Carlos Olalla Martinez1550cb62007-06-09 10:11:16 -04001131 struct w83627hf_data *data = w83627hf_update_device(dev);
1132 if (data->type == w83627hf)
1133 return sprintf(buf, "%ld\n",
Jim Cromie07584c72007-10-12 21:08:00 +02001134 pwm_freq_from_reg_627hf(data->pwm_freq[nr]));
Carlos Olalla Martinez1550cb62007-06-09 10:11:16 -04001135 else
1136 return sprintf(buf, "%ld\n",
Jim Cromie07584c72007-10-12 21:08:00 +02001137 pwm_freq_from_reg(data->pwm_freq[nr]));
Carlos Olalla Martinez1550cb62007-06-09 10:11:16 -04001138}
1139
1140static ssize_t
Guenter Roeck502a92f2019-01-22 15:25:47 -08001141pwm_freq_store(struct device *dev, struct device_attribute *devattr,
Jim Cromie07584c72007-10-12 21:08:00 +02001142 const char *buf, size_t count)
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 = dev_get_drvdata(dev);
1146 static const u8 mask[]={0xF8, 0x8F};
Guenter Roeck27b9de32012-01-15 11:07:26 -08001147 unsigned long val;
1148 int err;
Carlos Olalla Martinez1550cb62007-06-09 10:11:16 -04001149
Guenter Roeck27b9de32012-01-15 11:07:26 -08001150 err = kstrtoul(buf, 10, &val);
1151 if (err)
1152 return err;
Carlos Olalla Martinez1550cb62007-06-09 10:11:16 -04001153
1154 mutex_lock(&data->update_lock);
1155
1156 if (data->type == w83627hf) {
Jim Cromie07584c72007-10-12 21:08:00 +02001157 data->pwm_freq[nr] = pwm_freq_to_reg_627hf(val);
Carlos Olalla Martinez1550cb62007-06-09 10:11:16 -04001158 w83627hf_write_value(data, W83627HF_REG_PWM_FREQ,
Jim Cromie07584c72007-10-12 21:08:00 +02001159 (data->pwm_freq[nr] << (nr*4)) |
Carlos Olalla Martinez1550cb62007-06-09 10:11:16 -04001160 (w83627hf_read_value(data,
Jim Cromie07584c72007-10-12 21:08:00 +02001161 W83627HF_REG_PWM_FREQ) & mask[nr]));
Carlos Olalla Martinez1550cb62007-06-09 10:11:16 -04001162 } else {
Jim Cromie07584c72007-10-12 21:08:00 +02001163 data->pwm_freq[nr] = pwm_freq_to_reg(val);
1164 w83627hf_write_value(data, W83637HF_REG_PWM_FREQ[nr],
1165 data->pwm_freq[nr]);
Carlos Olalla Martinez1550cb62007-06-09 10:11:16 -04001166 }
1167
1168 mutex_unlock(&data->update_lock);
1169 return count;
1170}
1171
Guenter Roeck502a92f2019-01-22 15:25:47 -08001172static SENSOR_DEVICE_ATTR_RW(pwm1_freq, pwm_freq, 0);
1173static SENSOR_DEVICE_ATTR_RW(pwm2_freq, pwm_freq, 1);
1174static SENSOR_DEVICE_ATTR_RW(pwm3_freq, pwm_freq, 2);
Carlos Olalla Martinez1550cb62007-06-09 10:11:16 -04001175
1176static ssize_t
Guenter Roeck502a92f2019-01-22 15:25:47 -08001177temp_type_show(struct device *dev, struct device_attribute *devattr,
Jim Cromie07584c72007-10-12 21:08:00 +02001178 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001179{
Jim Cromie07584c72007-10-12 21:08:00 +02001180 int nr = to_sensor_dev_attr(devattr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001181 struct w83627hf_data *data = w83627hf_update_device(dev);
Jim Cromie07584c72007-10-12 21:08:00 +02001182 return sprintf(buf, "%ld\n", (long) data->sens[nr]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001183}
1184
1185static ssize_t
Guenter Roeck502a92f2019-01-22 15:25:47 -08001186temp_type_store(struct device *dev, struct device_attribute *devattr,
Jim Cromie07584c72007-10-12 21:08:00 +02001187 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001188{
Jim Cromie07584c72007-10-12 21:08:00 +02001189 int nr = to_sensor_dev_attr(devattr)->index;
Jean Delvare787c72b2007-05-08 17:22:00 +02001190 struct w83627hf_data *data = dev_get_drvdata(dev);
Guenter Roeck27b9de32012-01-15 11:07:26 -08001191 unsigned long val;
1192 u32 tmp;
1193 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001194
Guenter Roeck27b9de32012-01-15 11:07:26 -08001195 err = kstrtoul(buf, 10, &val);
1196 if (err)
1197 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001198
Ingo Molnar9a61bf62006-01-18 23:19:26 +01001199 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001200
1201 switch (val) {
1202 case 1: /* PII/Celeron diode */
Jean Delvare787c72b2007-05-08 17:22:00 +02001203 tmp = w83627hf_read_value(data, W83781D_REG_SCFG1);
1204 w83627hf_write_value(data, W83781D_REG_SCFG1,
Jim Cromie07584c72007-10-12 21:08:00 +02001205 tmp | BIT_SCFG1[nr]);
Jean Delvare787c72b2007-05-08 17:22:00 +02001206 tmp = w83627hf_read_value(data, W83781D_REG_SCFG2);
1207 w83627hf_write_value(data, W83781D_REG_SCFG2,
Jim Cromie07584c72007-10-12 21:08:00 +02001208 tmp | BIT_SCFG2[nr]);
1209 data->sens[nr] = val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001210 break;
1211 case 2: /* 3904 */
Jean Delvare787c72b2007-05-08 17:22:00 +02001212 tmp = w83627hf_read_value(data, W83781D_REG_SCFG1);
1213 w83627hf_write_value(data, W83781D_REG_SCFG1,
Jim Cromie07584c72007-10-12 21:08:00 +02001214 tmp | BIT_SCFG1[nr]);
Jean Delvare787c72b2007-05-08 17:22:00 +02001215 tmp = w83627hf_read_value(data, W83781D_REG_SCFG2);
1216 w83627hf_write_value(data, W83781D_REG_SCFG2,
Jim Cromie07584c72007-10-12 21:08:00 +02001217 tmp & ~BIT_SCFG2[nr]);
1218 data->sens[nr] = val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001219 break;
Jean Delvareb26f9332007-08-16 14:30:01 +02001220 case W83781D_DEFAULT_BETA:
1221 dev_warn(dev, "Sensor type %d is deprecated, please use 4 "
1222 "instead\n", W83781D_DEFAULT_BETA);
1223 /* fall through */
1224 case 4: /* thermistor */
Jean Delvare787c72b2007-05-08 17:22:00 +02001225 tmp = w83627hf_read_value(data, W83781D_REG_SCFG1);
1226 w83627hf_write_value(data, W83781D_REG_SCFG1,
Jim Cromie07584c72007-10-12 21:08:00 +02001227 tmp & ~BIT_SCFG1[nr]);
1228 data->sens[nr] = val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001229 break;
1230 default:
Jean Delvare787c72b2007-05-08 17:22:00 +02001231 dev_err(dev,
Jean Delvareb26f9332007-08-16 14:30:01 +02001232 "Invalid sensor type %ld; must be 1, 2, or 4\n",
1233 (long) val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001234 break;
1235 }
1236
Ingo Molnar9a61bf62006-01-18 23:19:26 +01001237 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001238 return count;
1239}
1240
Guenter Roeck502a92f2019-01-22 15:25:47 -08001241static SENSOR_DEVICE_ATTR_RW(temp1_type, temp_type, 0);
1242static SENSOR_DEVICE_ATTR_RW(temp2_type, temp_type, 1);
1243static SENSOR_DEVICE_ATTR_RW(temp3_type, temp_type, 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001244
Jim Cromie07584c72007-10-12 21:08:00 +02001245static ssize_t
Julia Lawall8dfcdfc2016-12-22 13:05:11 +01001246name_show(struct device *dev, struct device_attribute *devattr, char *buf)
Jean Delvare787c72b2007-05-08 17:22:00 +02001247{
1248 struct w83627hf_data *data = dev_get_drvdata(dev);
1249
1250 return sprintf(buf, "%s\n", data->name);
1251}
Julia Lawall8dfcdfc2016-12-22 13:05:11 +01001252static DEVICE_ATTR_RO(name);
Jean Delvare787c72b2007-05-08 17:22:00 +02001253
1254static int __init w83627hf_find(int sioaddr, unsigned short *addr,
1255 struct w83627hf_sio_data *sio_data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001256{
Jean Delvared27c37c2007-05-08 17:21:59 +02001257 int err = -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001258 u16 val;
1259
Andi Kleen64f50302012-08-18 10:30:05 -07001260 static __initconst char *const names[] = {
Jean Delvare787c72b2007-05-08 17:22:00 +02001261 "W83627HF",
1262 "W83627THF",
1263 "W83697HF",
1264 "W83637HF",
1265 "W83687THF",
1266 };
1267
Christian Schultec46c0e92009-12-16 21:38:29 +01001268 sio_data->sioaddr = sioaddr;
Jean Delvareb72656d2009-12-09 20:35:49 +01001269 superio_enter(sio_data);
1270 val = force_id ? force_id : superio_inb(sio_data, DEVID);
Jean Delvare787c72b2007-05-08 17:22:00 +02001271 switch (val) {
1272 case W627_DEVID:
1273 sio_data->type = w83627hf;
1274 break;
1275 case W627THF_DEVID:
1276 sio_data->type = w83627thf;
1277 break;
1278 case W697_DEVID:
1279 sio_data->type = w83697hf;
1280 break;
1281 case W637_DEVID:
1282 sio_data->type = w83637hf;
1283 break;
1284 case W687THF_DEVID:
1285 sio_data->type = w83687thf;
1286 break;
Jean Delvaree142e2a2007-05-27 22:17:43 +02001287 case 0xff: /* No device at all */
1288 goto exit;
Jean Delvare787c72b2007-05-08 17:22:00 +02001289 default:
Jean Delvaree142e2a2007-05-27 22:17:43 +02001290 pr_debug(DRVNAME ": Unsupported chip (DEVID=0x%02x)\n", val);
Jean Delvared27c37c2007-05-08 17:21:59 +02001291 goto exit;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001292 }
1293
Jean Delvareb72656d2009-12-09 20:35:49 +01001294 superio_select(sio_data, W83627HF_LD_HWM);
1295 val = (superio_inb(sio_data, WINB_BASE_REG) << 8) |
1296 superio_inb(sio_data, WINB_BASE_REG + 1);
Petr Vandrovecada0c2f2005-10-07 23:11:03 +02001297 *addr = val & WINB_ALIGNMENT;
Jean Delvared27c37c2007-05-08 17:21:59 +02001298 if (*addr == 0) {
Joe Perches18de0302010-10-20 06:51:55 +00001299 pr_warn("Base address not set, skipping\n");
Jean Delvared27c37c2007-05-08 17:21:59 +02001300 goto exit;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001301 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001302
Jean Delvareb72656d2009-12-09 20:35:49 +01001303 val = superio_inb(sio_data, WINB_ACT_REG);
Jean Delvared27c37c2007-05-08 17:21:59 +02001304 if (!(val & 0x01)) {
Joe Perches18de0302010-10-20 06:51:55 +00001305 pr_warn("Enabling HWM logical device\n");
Jean Delvareb72656d2009-12-09 20:35:49 +01001306 superio_outb(sio_data, WINB_ACT_REG, val | 0x01);
Jean Delvared27c37c2007-05-08 17:21:59 +02001307 }
1308
1309 err = 0;
Jean Delvare787c72b2007-05-08 17:22:00 +02001310 pr_info(DRVNAME ": Found %s chip at %#x\n",
1311 names[sio_data->type], *addr);
Jean Delvared27c37c2007-05-08 17:21:59 +02001312
1313 exit:
Jean Delvareb72656d2009-12-09 20:35:49 +01001314 superio_exit(sio_data);
Jean Delvared27c37c2007-05-08 17:21:59 +02001315 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001316}
1317
Jim Cromie07584c72007-10-12 21:08:00 +02001318#define VIN_UNIT_ATTRS(_X_) \
1319 &sensor_dev_attr_in##_X_##_input.dev_attr.attr, \
1320 &sensor_dev_attr_in##_X_##_min.dev_attr.attr, \
Jean Delvaree3604c62008-01-03 23:00:30 +01001321 &sensor_dev_attr_in##_X_##_max.dev_attr.attr, \
1322 &sensor_dev_attr_in##_X_##_alarm.dev_attr.attr, \
1323 &sensor_dev_attr_in##_X_##_beep.dev_attr.attr
Jim Cromie07584c72007-10-12 21:08:00 +02001324
1325#define FAN_UNIT_ATTRS(_X_) \
1326 &sensor_dev_attr_fan##_X_##_input.dev_attr.attr, \
1327 &sensor_dev_attr_fan##_X_##_min.dev_attr.attr, \
Jean Delvaree3604c62008-01-03 23:00:30 +01001328 &sensor_dev_attr_fan##_X_##_div.dev_attr.attr, \
1329 &sensor_dev_attr_fan##_X_##_alarm.dev_attr.attr, \
1330 &sensor_dev_attr_fan##_X_##_beep.dev_attr.attr
Jim Cromie07584c72007-10-12 21:08:00 +02001331
1332#define TEMP_UNIT_ATTRS(_X_) \
1333 &sensor_dev_attr_temp##_X_##_input.dev_attr.attr, \
1334 &sensor_dev_attr_temp##_X_##_max.dev_attr.attr, \
1335 &sensor_dev_attr_temp##_X_##_max_hyst.dev_attr.attr, \
Jean Delvaree3604c62008-01-03 23:00:30 +01001336 &sensor_dev_attr_temp##_X_##_type.dev_attr.attr, \
1337 &sensor_dev_attr_temp##_X_##_alarm.dev_attr.attr, \
1338 &sensor_dev_attr_temp##_X_##_beep.dev_attr.attr
Jim Cromie07584c72007-10-12 21:08:00 +02001339
Mark M. Hoffmanc1685f62006-09-24 20:59:49 +02001340static struct attribute *w83627hf_attributes[] = {
1341 &dev_attr_in0_input.attr,
1342 &dev_attr_in0_min.attr,
1343 &dev_attr_in0_max.attr,
Jean Delvaree3604c62008-01-03 23:00:30 +01001344 &sensor_dev_attr_in0_alarm.dev_attr.attr,
1345 &sensor_dev_attr_in0_beep.dev_attr.attr,
Jim Cromie07584c72007-10-12 21:08:00 +02001346 VIN_UNIT_ATTRS(2),
1347 VIN_UNIT_ATTRS(3),
1348 VIN_UNIT_ATTRS(4),
1349 VIN_UNIT_ATTRS(7),
1350 VIN_UNIT_ATTRS(8),
Mark M. Hoffmanc1685f62006-09-24 20:59:49 +02001351
Jim Cromie07584c72007-10-12 21:08:00 +02001352 FAN_UNIT_ATTRS(1),
1353 FAN_UNIT_ATTRS(2),
Mark M. Hoffmanc1685f62006-09-24 20:59:49 +02001354
Jim Cromie07584c72007-10-12 21:08:00 +02001355 TEMP_UNIT_ATTRS(1),
1356 TEMP_UNIT_ATTRS(2),
Mark M. Hoffmanc1685f62006-09-24 20:59:49 +02001357
1358 &dev_attr_alarms.attr,
Jean Delvare1c138102008-01-03 23:04:55 +01001359 &sensor_dev_attr_beep_enable.dev_attr.attr,
Mark M. Hoffmanc1685f62006-09-24 20:59:49 +02001360 &dev_attr_beep_mask.attr,
1361
Jim Cromie07584c72007-10-12 21:08:00 +02001362 &sensor_dev_attr_pwm1.dev_attr.attr,
1363 &sensor_dev_attr_pwm2.dev_attr.attr,
Jean Delvare787c72b2007-05-08 17:22:00 +02001364 &dev_attr_name.attr,
Mark M. Hoffmanc1685f62006-09-24 20:59:49 +02001365 NULL
1366};
1367
1368static const struct attribute_group w83627hf_group = {
1369 .attrs = w83627hf_attributes,
1370};
1371
1372static struct attribute *w83627hf_attributes_opt[] = {
Jim Cromie07584c72007-10-12 21:08:00 +02001373 VIN_UNIT_ATTRS(1),
1374 VIN_UNIT_ATTRS(5),
1375 VIN_UNIT_ATTRS(6),
Mark M. Hoffmanc1685f62006-09-24 20:59:49 +02001376
Jim Cromie07584c72007-10-12 21:08:00 +02001377 FAN_UNIT_ATTRS(3),
1378 TEMP_UNIT_ATTRS(3),
1379 &sensor_dev_attr_pwm3.dev_attr.attr,
Mark M. Hoffmanc1685f62006-09-24 20:59:49 +02001380
Jim Cromie07584c72007-10-12 21:08:00 +02001381 &sensor_dev_attr_pwm1_freq.dev_attr.attr,
1382 &sensor_dev_attr_pwm2_freq.dev_attr.attr,
1383 &sensor_dev_attr_pwm3_freq.dev_attr.attr,
Dominik Geyera95a5ed2008-08-06 22:41:04 +02001384
1385 &sensor_dev_attr_pwm1_enable.dev_attr.attr,
1386 &sensor_dev_attr_pwm2_enable.dev_attr.attr,
1387 &sensor_dev_attr_pwm3_enable.dev_attr.attr,
1388
Mark M. Hoffmanc1685f62006-09-24 20:59:49 +02001389 NULL
1390};
1391
1392static const struct attribute_group w83627hf_group_opt = {
1393 .attrs = w83627hf_attributes_opt,
1394};
1395
Bill Pemberton6c931ae2012-11-19 13:22:35 -05001396static int w83627hf_probe(struct platform_device *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001397{
Jean Delvare787c72b2007-05-08 17:22:00 +02001398 struct device *dev = &pdev->dev;
Jingoo Hana8b3a3a2013-07-30 17:13:06 +09001399 struct w83627hf_sio_data *sio_data = dev_get_platdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001400 struct w83627hf_data *data;
Jean Delvare787c72b2007-05-08 17:22:00 +02001401 struct resource *res;
Jim Cromie2ca2fcd2007-10-14 17:20:50 -06001402 int err, i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001403
Jean Delvare787c72b2007-05-08 17:22:00 +02001404 static const char *names[] = {
1405 "w83627hf",
1406 "w83627thf",
1407 "w83697hf",
1408 "w83637hf",
1409 "w83687thf",
1410 };
1411
1412 res = platform_get_resource(pdev, IORESOURCE_IO, 0);
Guenter Roeck0cf46992012-06-02 11:47:59 -07001413 if (!devm_request_region(dev, res->start, WINB_REGION_SIZE, DRVNAME)) {
Jean Delvare787c72b2007-05-08 17:22:00 +02001414 dev_err(dev, "Failed to request region 0x%lx-0x%lx\n",
1415 (unsigned long)res->start,
1416 (unsigned long)(res->start + WINB_REGION_SIZE - 1));
Guenter Roeck0cf46992012-06-02 11:47:59 -07001417 return -EBUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001418 }
1419
Guenter Roeck0cf46992012-06-02 11:47:59 -07001420 data = devm_kzalloc(dev, sizeof(struct w83627hf_data), GFP_KERNEL);
1421 if (!data)
1422 return -ENOMEM;
1423
Jean Delvare787c72b2007-05-08 17:22:00 +02001424 data->addr = res->start;
1425 data->type = sio_data->type;
1426 data->name = names[sio_data->type];
Ingo Molnar9a61bf62006-01-18 23:19:26 +01001427 mutex_init(&data->lock);
Ingo Molnar9a61bf62006-01-18 23:19:26 +01001428 mutex_init(&data->update_lock);
Jean Delvare787c72b2007-05-08 17:22:00 +02001429 platform_set_drvdata(pdev, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001430
Linus Torvalds1da177e2005-04-16 15:20:36 -07001431 /* Initialize the chip */
Jean Delvare787c72b2007-05-08 17:22:00 +02001432 w83627hf_init_device(pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001433
1434 /* A few vars need to be filled upon startup */
Jim Cromie2ca2fcd2007-10-14 17:20:50 -06001435 for (i = 0; i <= 2; i++)
1436 data->fan_min[i] = w83627hf_read_value(
1437 data, W83627HF_REG_FAN_MIN(i));
Jean Delvarec09c5182007-10-12 21:53:07 +02001438 w83627hf_update_fan_div(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001439
Mark M. Hoffmanc1685f62006-09-24 20:59:49 +02001440 /* Register common device attributes */
Guenter Roeck27b9de32012-01-15 11:07:26 -08001441 err = sysfs_create_group(&dev->kobj, &w83627hf_group);
1442 if (err)
Guenter Roeck0cf46992012-06-02 11:47:59 -07001443 return err;
Mark M. Hoffmanc1685f62006-09-24 20:59:49 +02001444
1445 /* Register chip-specific device attributes */
Jean Delvare787c72b2007-05-08 17:22:00 +02001446 if (data->type == w83627hf || data->type == w83697hf)
Jim Cromie07584c72007-10-12 21:08:00 +02001447 if ((err = device_create_file(dev,
1448 &sensor_dev_attr_in5_input.dev_attr))
1449 || (err = device_create_file(dev,
1450 &sensor_dev_attr_in5_min.dev_attr))
1451 || (err = device_create_file(dev,
1452 &sensor_dev_attr_in5_max.dev_attr))
1453 || (err = device_create_file(dev,
Jean Delvaree3604c62008-01-03 23:00:30 +01001454 &sensor_dev_attr_in5_alarm.dev_attr))
1455 || (err = device_create_file(dev,
1456 &sensor_dev_attr_in5_beep.dev_attr))
1457 || (err = device_create_file(dev,
Jim Cromie07584c72007-10-12 21:08:00 +02001458 &sensor_dev_attr_in6_input.dev_attr))
1459 || (err = device_create_file(dev,
1460 &sensor_dev_attr_in6_min.dev_attr))
1461 || (err = device_create_file(dev,
1462 &sensor_dev_attr_in6_max.dev_attr))
1463 || (err = device_create_file(dev,
Jean Delvaree3604c62008-01-03 23:00:30 +01001464 &sensor_dev_attr_in6_alarm.dev_attr))
1465 || (err = device_create_file(dev,
1466 &sensor_dev_attr_in6_beep.dev_attr))
1467 || (err = device_create_file(dev,
Jim Cromie07584c72007-10-12 21:08:00 +02001468 &sensor_dev_attr_pwm1_freq.dev_attr))
1469 || (err = device_create_file(dev,
1470 &sensor_dev_attr_pwm2_freq.dev_attr)))
Guenter Roeck0cf46992012-06-02 11:47:59 -07001471 goto error;
Mark M. Hoffmanc1685f62006-09-24 20:59:49 +02001472
Jean Delvare787c72b2007-05-08 17:22:00 +02001473 if (data->type != w83697hf)
Jim Cromie07584c72007-10-12 21:08:00 +02001474 if ((err = device_create_file(dev,
1475 &sensor_dev_attr_in1_input.dev_attr))
1476 || (err = device_create_file(dev,
1477 &sensor_dev_attr_in1_min.dev_attr))
1478 || (err = device_create_file(dev,
1479 &sensor_dev_attr_in1_max.dev_attr))
1480 || (err = device_create_file(dev,
Jean Delvaree3604c62008-01-03 23:00:30 +01001481 &sensor_dev_attr_in1_alarm.dev_attr))
1482 || (err = device_create_file(dev,
1483 &sensor_dev_attr_in1_beep.dev_attr))
1484 || (err = device_create_file(dev,
Jim Cromie07584c72007-10-12 21:08:00 +02001485 &sensor_dev_attr_fan3_input.dev_attr))
1486 || (err = device_create_file(dev,
1487 &sensor_dev_attr_fan3_min.dev_attr))
1488 || (err = device_create_file(dev,
1489 &sensor_dev_attr_fan3_div.dev_attr))
1490 || (err = device_create_file(dev,
Jean Delvaree3604c62008-01-03 23:00:30 +01001491 &sensor_dev_attr_fan3_alarm.dev_attr))
1492 || (err = device_create_file(dev,
1493 &sensor_dev_attr_fan3_beep.dev_attr))
1494 || (err = device_create_file(dev,
Jim Cromie07584c72007-10-12 21:08:00 +02001495 &sensor_dev_attr_temp3_input.dev_attr))
1496 || (err = device_create_file(dev,
1497 &sensor_dev_attr_temp3_max.dev_attr))
1498 || (err = device_create_file(dev,
1499 &sensor_dev_attr_temp3_max_hyst.dev_attr))
1500 || (err = device_create_file(dev,
Jean Delvaree3604c62008-01-03 23:00:30 +01001501 &sensor_dev_attr_temp3_alarm.dev_attr))
1502 || (err = device_create_file(dev,
1503 &sensor_dev_attr_temp3_beep.dev_attr))
1504 || (err = device_create_file(dev,
Jim Cromie07584c72007-10-12 21:08:00 +02001505 &sensor_dev_attr_temp3_type.dev_attr)))
Guenter Roeck0cf46992012-06-02 11:47:59 -07001506 goto error;
Mark M. Hoffmanc1685f62006-09-24 20:59:49 +02001507
Jean Delvare787c72b2007-05-08 17:22:00 +02001508 if (data->type != w83697hf && data->vid != 0xff) {
Jean Delvare8a665a02007-05-08 17:21:59 +02001509 /* Convert VID to voltage based on VRM */
1510 data->vrm = vid_which_vrm();
1511
Jean Delvare787c72b2007-05-08 17:22:00 +02001512 if ((err = device_create_file(dev, &dev_attr_cpu0_vid))
1513 || (err = device_create_file(dev, &dev_attr_vrm)))
Guenter Roeck0cf46992012-06-02 11:47:59 -07001514 goto error;
Jean Delvare8a665a02007-05-08 17:21:59 +02001515 }
Mark M. Hoffmanc1685f62006-09-24 20:59:49 +02001516
Jean Delvare787c72b2007-05-08 17:22:00 +02001517 if (data->type == w83627thf || data->type == w83637hf
Guenter Roeck27b9de32012-01-15 11:07:26 -08001518 || data->type == w83687thf) {
1519 err = device_create_file(dev, &sensor_dev_attr_pwm3.dev_attr);
1520 if (err)
Guenter Roeck0cf46992012-06-02 11:47:59 -07001521 goto error;
Guenter Roeck27b9de32012-01-15 11:07:26 -08001522 }
Mark M. Hoffmanc1685f62006-09-24 20:59:49 +02001523
Carlos Olalla Martinez1550cb62007-06-09 10:11:16 -04001524 if (data->type == w83637hf || data->type == w83687thf)
Jim Cromie07584c72007-10-12 21:08:00 +02001525 if ((err = device_create_file(dev,
1526 &sensor_dev_attr_pwm1_freq.dev_attr))
1527 || (err = device_create_file(dev,
1528 &sensor_dev_attr_pwm2_freq.dev_attr))
1529 || (err = device_create_file(dev,
1530 &sensor_dev_attr_pwm3_freq.dev_attr)))
Guenter Roeck0cf46992012-06-02 11:47:59 -07001531 goto error;
Carlos Olalla Martinez1550cb62007-06-09 10:11:16 -04001532
Dominik Geyera95a5ed2008-08-06 22:41:04 +02001533 if (data->type != w83627hf)
1534 if ((err = device_create_file(dev,
1535 &sensor_dev_attr_pwm1_enable.dev_attr))
1536 || (err = device_create_file(dev,
1537 &sensor_dev_attr_pwm2_enable.dev_attr)))
Guenter Roeck0cf46992012-06-02 11:47:59 -07001538 goto error;
Dominik Geyera95a5ed2008-08-06 22:41:04 +02001539
1540 if (data->type == w83627thf || data->type == w83637hf
Guenter Roeck27b9de32012-01-15 11:07:26 -08001541 || data->type == w83687thf) {
1542 err = device_create_file(dev,
1543 &sensor_dev_attr_pwm3_enable.dev_attr);
1544 if (err)
Guenter Roeck0cf46992012-06-02 11:47:59 -07001545 goto error;
Guenter Roeck27b9de32012-01-15 11:07:26 -08001546 }
Dominik Geyera95a5ed2008-08-06 22:41:04 +02001547
Tony Jones1beeffe2007-08-20 13:46:20 -07001548 data->hwmon_dev = hwmon_device_register(dev);
1549 if (IS_ERR(data->hwmon_dev)) {
1550 err = PTR_ERR(data->hwmon_dev);
Guenter Roeck0cf46992012-06-02 11:47:59 -07001551 goto error;
Mark M. Hoffman943b0832005-07-15 21:39:18 -04001552 }
1553
Linus Torvalds1da177e2005-04-16 15:20:36 -07001554 return 0;
1555
Guenter Roeck0cf46992012-06-02 11:47:59 -07001556 error:
Jean Delvare787c72b2007-05-08 17:22:00 +02001557 sysfs_remove_group(&dev->kobj, &w83627hf_group);
1558 sysfs_remove_group(&dev->kobj, &w83627hf_group_opt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001559 return err;
1560}
1561
Bill Pemberton281dfd02012-11-19 13:25:51 -05001562static int w83627hf_remove(struct platform_device *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001563{
Jean Delvare787c72b2007-05-08 17:22:00 +02001564 struct w83627hf_data *data = platform_get_drvdata(pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001565
Tony Jones1beeffe2007-08-20 13:46:20 -07001566 hwmon_device_unregister(data->hwmon_dev);
Mark M. Hoffman943b0832005-07-15 21:39:18 -04001567
Jean Delvare787c72b2007-05-08 17:22:00 +02001568 sysfs_remove_group(&pdev->dev.kobj, &w83627hf_group);
1569 sysfs_remove_group(&pdev->dev.kobj, &w83627hf_group_opt);
Jean Delvare787c72b2007-05-08 17:22:00 +02001570
Linus Torvalds1da177e2005-04-16 15:20:36 -07001571 return 0;
1572}
1573
Jean Delvared58df9c2007-10-10 16:30:23 +02001574/* Registers 0x50-0x5f are banked */
1575static inline void w83627hf_set_bank(struct w83627hf_data *data, u16 reg)
1576{
1577 if ((reg & 0x00f0) == 0x50) {
1578 outb_p(W83781D_REG_BANK, data->addr + W83781D_ADDR_REG_OFFSET);
1579 outb_p(reg >> 8, data->addr + W83781D_DATA_REG_OFFSET);
1580 }
1581}
1582
1583/* Not strictly necessary, but play it safe for now */
1584static inline void w83627hf_reset_bank(struct w83627hf_data *data, u16 reg)
1585{
1586 if (reg & 0xff00) {
1587 outb_p(W83781D_REG_BANK, data->addr + W83781D_ADDR_REG_OFFSET);
1588 outb_p(0, data->addr + W83781D_DATA_REG_OFFSET);
1589 }
1590}
1591
Jean Delvare787c72b2007-05-08 17:22:00 +02001592static int w83627hf_read_value(struct w83627hf_data *data, u16 reg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001593{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001594 int res, word_sized;
1595
Ingo Molnar9a61bf62006-01-18 23:19:26 +01001596 mutex_lock(&data->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001597 word_sized = (((reg & 0xff00) == 0x100)
1598 || ((reg & 0xff00) == 0x200))
1599 && (((reg & 0x00ff) == 0x50)
1600 || ((reg & 0x00ff) == 0x53)
1601 || ((reg & 0x00ff) == 0x55));
Jean Delvared58df9c2007-10-10 16:30:23 +02001602 w83627hf_set_bank(data, reg);
Jean Delvare787c72b2007-05-08 17:22:00 +02001603 outb_p(reg & 0xff, data->addr + W83781D_ADDR_REG_OFFSET);
1604 res = inb_p(data->addr + W83781D_DATA_REG_OFFSET);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001605 if (word_sized) {
1606 outb_p((reg & 0xff) + 1,
Jean Delvare787c72b2007-05-08 17:22:00 +02001607 data->addr + W83781D_ADDR_REG_OFFSET);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001608 res =
Jean Delvare787c72b2007-05-08 17:22:00 +02001609 (res << 8) + inb_p(data->addr +
Linus Torvalds1da177e2005-04-16 15:20:36 -07001610 W83781D_DATA_REG_OFFSET);
1611 }
Jean Delvared58df9c2007-10-10 16:30:23 +02001612 w83627hf_reset_bank(data, reg);
Ingo Molnar9a61bf62006-01-18 23:19:26 +01001613 mutex_unlock(&data->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001614 return res;
1615}
1616
Bill Pemberton6c931ae2012-11-19 13:22:35 -05001617static int w83627thf_read_gpio5(struct platform_device *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001618{
Jingoo Hana8b3a3a2013-07-30 17:13:06 +09001619 struct w83627hf_sio_data *sio_data = dev_get_platdata(&pdev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001620 int res = 0xff, sel;
1621
Jean Delvareb72656d2009-12-09 20:35:49 +01001622 superio_enter(sio_data);
1623 superio_select(sio_data, W83627HF_LD_GPIO5);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001624
1625 /* Make sure these GPIO pins are enabled */
Jean Delvareb72656d2009-12-09 20:35:49 +01001626 if (!(superio_inb(sio_data, W83627THF_GPIO5_EN) & (1<<3))) {
Jean Delvare787c72b2007-05-08 17:22:00 +02001627 dev_dbg(&pdev->dev, "GPIO5 disabled, no VID function\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001628 goto exit;
1629 }
1630
Guenter Roeck27b9de32012-01-15 11:07:26 -08001631 /*
1632 * Make sure the pins are configured for input
1633 * There must be at least five (VRM 9), and possibly 6 (VRM 10)
1634 */
Jean Delvareb72656d2009-12-09 20:35:49 +01001635 sel = superio_inb(sio_data, W83627THF_GPIO5_IOSR) & 0x3f;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001636 if ((sel & 0x1f) != 0x1f) {
Jean Delvare787c72b2007-05-08 17:22:00 +02001637 dev_dbg(&pdev->dev, "GPIO5 not configured for VID "
Linus Torvalds1da177e2005-04-16 15:20:36 -07001638 "function\n");
1639 goto exit;
1640 }
1641
Jean Delvare787c72b2007-05-08 17:22:00 +02001642 dev_info(&pdev->dev, "Reading VID from GPIO5\n");
Jean Delvareb72656d2009-12-09 20:35:49 +01001643 res = superio_inb(sio_data, W83627THF_GPIO5_DR) & sel;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001644
1645exit:
Jean Delvareb72656d2009-12-09 20:35:49 +01001646 superio_exit(sio_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001647 return res;
1648}
1649
Bill Pemberton6c931ae2012-11-19 13:22:35 -05001650static int w83687thf_read_vid(struct platform_device *pdev)
Jean Delvarec2db6ce2006-01-18 23:22:12 +01001651{
Jingoo Hana8b3a3a2013-07-30 17:13:06 +09001652 struct w83627hf_sio_data *sio_data = dev_get_platdata(&pdev->dev);
Jean Delvarec2db6ce2006-01-18 23:22:12 +01001653 int res = 0xff;
1654
Jean Delvareb72656d2009-12-09 20:35:49 +01001655 superio_enter(sio_data);
1656 superio_select(sio_data, W83627HF_LD_HWM);
Jean Delvarec2db6ce2006-01-18 23:22:12 +01001657
1658 /* Make sure these GPIO pins are enabled */
Jean Delvareb72656d2009-12-09 20:35:49 +01001659 if (!(superio_inb(sio_data, W83687THF_VID_EN) & (1 << 2))) {
Jean Delvare787c72b2007-05-08 17:22:00 +02001660 dev_dbg(&pdev->dev, "VID disabled, no VID function\n");
Jean Delvarec2db6ce2006-01-18 23:22:12 +01001661 goto exit;
1662 }
1663
1664 /* Make sure the pins are configured for input */
Jean Delvareb72656d2009-12-09 20:35:49 +01001665 if (!(superio_inb(sio_data, W83687THF_VID_CFG) & (1 << 4))) {
Jean Delvare787c72b2007-05-08 17:22:00 +02001666 dev_dbg(&pdev->dev, "VID configured as output, "
Jean Delvarec2db6ce2006-01-18 23:22:12 +01001667 "no VID function\n");
1668 goto exit;
1669 }
1670
Jean Delvareb72656d2009-12-09 20:35:49 +01001671 res = superio_inb(sio_data, W83687THF_VID_DATA) & 0x3f;
Jean Delvarec2db6ce2006-01-18 23:22:12 +01001672
1673exit:
Jean Delvareb72656d2009-12-09 20:35:49 +01001674 superio_exit(sio_data);
Jean Delvarec2db6ce2006-01-18 23:22:12 +01001675 return res;
1676}
1677
Jean Delvare787c72b2007-05-08 17:22:00 +02001678static int w83627hf_write_value(struct w83627hf_data *data, u16 reg, u16 value)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001679{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001680 int word_sized;
1681
Ingo Molnar9a61bf62006-01-18 23:19:26 +01001682 mutex_lock(&data->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001683 word_sized = (((reg & 0xff00) == 0x100)
1684 || ((reg & 0xff00) == 0x200))
1685 && (((reg & 0x00ff) == 0x53)
1686 || ((reg & 0x00ff) == 0x55));
Jean Delvared58df9c2007-10-10 16:30:23 +02001687 w83627hf_set_bank(data, reg);
Jean Delvare787c72b2007-05-08 17:22:00 +02001688 outb_p(reg & 0xff, data->addr + W83781D_ADDR_REG_OFFSET);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001689 if (word_sized) {
1690 outb_p(value >> 8,
Jean Delvare787c72b2007-05-08 17:22:00 +02001691 data->addr + W83781D_DATA_REG_OFFSET);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001692 outb_p((reg & 0xff) + 1,
Jean Delvare787c72b2007-05-08 17:22:00 +02001693 data->addr + W83781D_ADDR_REG_OFFSET);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001694 }
1695 outb_p(value & 0xff,
Jean Delvare787c72b2007-05-08 17:22:00 +02001696 data->addr + W83781D_DATA_REG_OFFSET);
Jean Delvared58df9c2007-10-10 16:30:23 +02001697 w83627hf_reset_bank(data, reg);
Ingo Molnar9a61bf62006-01-18 23:19:26 +01001698 mutex_unlock(&data->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001699 return 0;
1700}
1701
Bill Pemberton6c931ae2012-11-19 13:22:35 -05001702static void w83627hf_init_device(struct platform_device *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001703{
Jean Delvare787c72b2007-05-08 17:22:00 +02001704 struct w83627hf_data *data = platform_get_drvdata(pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001705 int i;
Jean Delvared27c37c2007-05-08 17:21:59 +02001706 enum chips type = data->type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001707 u8 tmp;
1708
Linus Torvalds1da177e2005-04-16 15:20:36 -07001709 /* Minimize conflicts with other winbond i2c-only clients... */
1710 /* disable i2c subclients... how to disable main i2c client?? */
1711 /* force i2c address to relatively uncommon address */
Jean Delvare8f3c7c52012-12-19 22:17:00 +01001712 if (type == w83627hf) {
1713 w83627hf_write_value(data, W83781D_REG_I2C_SUBADDR, 0x89);
1714 w83627hf_write_value(data, W83781D_REG_I2C_ADDR, force_i2c);
1715 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001716
1717 /* Read VID only once */
Jean Delvared27c37c2007-05-08 17:21:59 +02001718 if (type == w83627hf || type == w83637hf) {
Jean Delvare787c72b2007-05-08 17:22:00 +02001719 int lo = w83627hf_read_value(data, W83781D_REG_VID_FANDIV);
1720 int hi = w83627hf_read_value(data, W83781D_REG_CHIPID);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001721 data->vid = (lo & 0x0f) | ((hi & 0x01) << 4);
Jean Delvared27c37c2007-05-08 17:21:59 +02001722 } else if (type == w83627thf) {
Jean Delvare787c72b2007-05-08 17:22:00 +02001723 data->vid = w83627thf_read_gpio5(pdev);
Jean Delvared27c37c2007-05-08 17:21:59 +02001724 } else if (type == w83687thf) {
Jean Delvare787c72b2007-05-08 17:22:00 +02001725 data->vid = w83687thf_read_vid(pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001726 }
1727
1728 /* Read VRM & OVT Config only once */
Jean Delvared27c37c2007-05-08 17:21:59 +02001729 if (type == w83627thf || type == w83637hf || type == w83687thf) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001730 data->vrm_ovt =
Jean Delvare787c72b2007-05-08 17:22:00 +02001731 w83627hf_read_value(data, W83627THF_REG_VRM_OVT_CFG);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001732 }
1733
Jean Delvare787c72b2007-05-08 17:22:00 +02001734 tmp = w83627hf_read_value(data, W83781D_REG_SCFG1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001735 for (i = 1; i <= 3; i++) {
1736 if (!(tmp & BIT_SCFG1[i - 1])) {
Jean Delvareb26f9332007-08-16 14:30:01 +02001737 data->sens[i - 1] = 4;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001738 } else {
1739 if (w83627hf_read_value
Jean Delvare787c72b2007-05-08 17:22:00 +02001740 (data,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001741 W83781D_REG_SCFG2) & BIT_SCFG2[i - 1])
1742 data->sens[i - 1] = 1;
1743 else
1744 data->sens[i - 1] = 2;
1745 }
1746 if ((type == w83697hf) && (i == 2))
1747 break;
1748 }
1749
1750 if(init) {
1751 /* Enable temp2 */
Jim Cromiedf48ed82007-10-14 17:10:52 -06001752 tmp = w83627hf_read_value(data, W83627HF_REG_TEMP2_CONFIG);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001753 if (tmp & 0x01) {
Jean Delvare787c72b2007-05-08 17:22:00 +02001754 dev_warn(&pdev->dev, "Enabling temp2, readings "
Linus Torvalds1da177e2005-04-16 15:20:36 -07001755 "might not make sense\n");
Jim Cromiedf48ed82007-10-14 17:10:52 -06001756 w83627hf_write_value(data, W83627HF_REG_TEMP2_CONFIG,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001757 tmp & 0xfe);
1758 }
1759
1760 /* Enable temp3 */
1761 if (type != w83697hf) {
Jean Delvare787c72b2007-05-08 17:22:00 +02001762 tmp = w83627hf_read_value(data,
Jim Cromiedf48ed82007-10-14 17:10:52 -06001763 W83627HF_REG_TEMP3_CONFIG);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001764 if (tmp & 0x01) {
Jean Delvare787c72b2007-05-08 17:22:00 +02001765 dev_warn(&pdev->dev, "Enabling temp3, "
Linus Torvalds1da177e2005-04-16 15:20:36 -07001766 "readings might not make sense\n");
Jean Delvare787c72b2007-05-08 17:22:00 +02001767 w83627hf_write_value(data,
Jim Cromiedf48ed82007-10-14 17:10:52 -06001768 W83627HF_REG_TEMP3_CONFIG, tmp & 0xfe);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001769 }
1770 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001771 }
1772
1773 /* Start monitoring */
Jean Delvare787c72b2007-05-08 17:22:00 +02001774 w83627hf_write_value(data, W83781D_REG_CONFIG,
1775 (w83627hf_read_value(data,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001776 W83781D_REG_CONFIG) & 0xf7)
1777 | 0x01);
Jean Delvareef878b12008-01-03 22:54:13 +01001778
1779 /* Enable VBAT monitoring if needed */
1780 tmp = w83627hf_read_value(data, W83781D_REG_VBAT);
1781 if (!(tmp & 0x01))
1782 w83627hf_write_value(data, W83781D_REG_VBAT, tmp | 0x01);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001783}
1784
Jean Delvarec09c5182007-10-12 21:53:07 +02001785static void w83627hf_update_fan_div(struct w83627hf_data *data)
1786{
1787 int reg;
1788
1789 reg = w83627hf_read_value(data, W83781D_REG_VID_FANDIV);
1790 data->fan_div[0] = (reg >> 4) & 0x03;
1791 data->fan_div[1] = (reg >> 6) & 0x03;
1792 if (data->type != w83697hf) {
1793 data->fan_div[2] = (w83627hf_read_value(data,
1794 W83781D_REG_PIN) >> 6) & 0x03;
1795 }
1796 reg = w83627hf_read_value(data, W83781D_REG_VBAT);
1797 data->fan_div[0] |= (reg >> 3) & 0x04;
1798 data->fan_div[1] |= (reg >> 4) & 0x04;
1799 if (data->type != w83697hf)
1800 data->fan_div[2] |= (reg >> 5) & 0x04;
1801}
1802
Linus Torvalds1da177e2005-04-16 15:20:36 -07001803static struct w83627hf_data *w83627hf_update_device(struct device *dev)
1804{
Jean Delvare787c72b2007-05-08 17:22:00 +02001805 struct w83627hf_data *data = dev_get_drvdata(dev);
Jim Cromiedf48ed82007-10-14 17:10:52 -06001806 int i, num_temps = (data->type == w83697hf) ? 2 : 3;
Dominik Geyera95a5ed2008-08-06 22:41:04 +02001807 int num_pwms = (data->type == w83697hf) ? 2 : 3;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001808
Ingo Molnar9a61bf62006-01-18 23:19:26 +01001809 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001810
1811 if (time_after(jiffies, data->last_updated + HZ + HZ / 2)
1812 || !data->valid) {
1813 for (i = 0; i <= 8; i++) {
1814 /* skip missing sensors */
1815 if (((data->type == w83697hf) && (i == 1)) ||
Jean Delvarec2db6ce2006-01-18 23:22:12 +01001816 ((data->type != w83627hf && data->type != w83697hf)
Yuan Mu4a1c44472005-11-07 22:19:04 +01001817 && (i == 5 || i == 6)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001818 continue;
1819 data->in[i] =
Jean Delvare787c72b2007-05-08 17:22:00 +02001820 w83627hf_read_value(data, W83781D_REG_IN(i));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001821 data->in_min[i] =
Jean Delvare787c72b2007-05-08 17:22:00 +02001822 w83627hf_read_value(data,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001823 W83781D_REG_IN_MIN(i));
1824 data->in_max[i] =
Jean Delvare787c72b2007-05-08 17:22:00 +02001825 w83627hf_read_value(data,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001826 W83781D_REG_IN_MAX(i));
1827 }
Jim Cromie2ca2fcd2007-10-14 17:20:50 -06001828 for (i = 0; i <= 2; i++) {
1829 data->fan[i] =
1830 w83627hf_read_value(data, W83627HF_REG_FAN(i));
1831 data->fan_min[i] =
Jean Delvare787c72b2007-05-08 17:22:00 +02001832 w83627hf_read_value(data,
Jim Cromie2ca2fcd2007-10-14 17:20:50 -06001833 W83627HF_REG_FAN_MIN(i));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001834 }
Jim Cromie07584c72007-10-12 21:08:00 +02001835 for (i = 0; i <= 2; i++) {
Jean Delvare787c72b2007-05-08 17:22:00 +02001836 u8 tmp = w83627hf_read_value(data,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001837 W836X7HF_REG_PWM(data->type, i));
1838 /* bits 0-3 are reserved in 627THF */
1839 if (data->type == w83627thf)
1840 tmp &= 0xf0;
Jim Cromie07584c72007-10-12 21:08:00 +02001841 data->pwm[i] = tmp;
1842 if (i == 1 &&
1843 (data->type == w83627hf || data->type == w83697hf))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001844 break;
1845 }
Carlos Olalla Martinez1550cb62007-06-09 10:11:16 -04001846 if (data->type == w83627hf) {
1847 u8 tmp = w83627hf_read_value(data,
1848 W83627HF_REG_PWM_FREQ);
1849 data->pwm_freq[0] = tmp & 0x07;
1850 data->pwm_freq[1] = (tmp >> 4) & 0x07;
1851 } else if (data->type != w83627thf) {
1852 for (i = 1; i <= 3; i++) {
1853 data->pwm_freq[i - 1] =
1854 w83627hf_read_value(data,
1855 W83637HF_REG_PWM_FREQ[i - 1]);
1856 if (i == 2 && (data->type == w83697hf))
1857 break;
1858 }
1859 }
Dominik Geyera95a5ed2008-08-06 22:41:04 +02001860 if (data->type != w83627hf) {
1861 for (i = 0; i < num_pwms; i++) {
1862 u8 tmp = w83627hf_read_value(data,
1863 W83627THF_REG_PWM_ENABLE[i]);
1864 data->pwm_enable[i] =
1865 ((tmp >> W83627THF_PWM_ENABLE_SHIFT[i])
1866 & 0x03) + 1;
1867 }
1868 }
Jim Cromiedf48ed82007-10-14 17:10:52 -06001869 for (i = 0; i < num_temps; i++) {
1870 data->temp[i] = w83627hf_read_value(
1871 data, w83627hf_reg_temp[i]);
1872 data->temp_max[i] = w83627hf_read_value(
1873 data, w83627hf_reg_temp_over[i]);
1874 data->temp_max_hyst[i] = w83627hf_read_value(
1875 data, w83627hf_reg_temp_hyst[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001876 }
1877
Jean Delvarec09c5182007-10-12 21:53:07 +02001878 w83627hf_update_fan_div(data);
1879
Linus Torvalds1da177e2005-04-16 15:20:36 -07001880 data->alarms =
Jean Delvare787c72b2007-05-08 17:22:00 +02001881 w83627hf_read_value(data, W83781D_REG_ALARM1) |
1882 (w83627hf_read_value(data, W83781D_REG_ALARM2) << 8) |
1883 (w83627hf_read_value(data, W83781D_REG_ALARM3) << 16);
1884 i = w83627hf_read_value(data, W83781D_REG_BEEP_INTS2);
Jean Delvare1c138102008-01-03 23:04:55 +01001885 data->beep_mask = (i << 8) |
Jean Delvare787c72b2007-05-08 17:22:00 +02001886 w83627hf_read_value(data, W83781D_REG_BEEP_INTS1) |
1887 w83627hf_read_value(data, W83781D_REG_BEEP_INTS3) << 16;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001888 data->last_updated = jiffies;
1889 data->valid = 1;
1890 }
1891
Ingo Molnar9a61bf62006-01-18 23:19:26 +01001892 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001893
1894 return data;
1895}
1896
Jean Delvare787c72b2007-05-08 17:22:00 +02001897static int __init w83627hf_device_add(unsigned short address,
1898 const struct w83627hf_sio_data *sio_data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001899{
Jean Delvare787c72b2007-05-08 17:22:00 +02001900 struct resource res = {
1901 .start = address + WINB_REGION_OFFSET,
1902 .end = address + WINB_REGION_OFFSET + WINB_REGION_SIZE - 1,
1903 .name = DRVNAME,
1904 .flags = IORESOURCE_IO,
1905 };
1906 int err;
1907
Jean Delvareb9acb642009-01-07 16:37:35 +01001908 err = acpi_check_resource_conflict(&res);
1909 if (err)
1910 goto exit;
1911
Jean Delvare787c72b2007-05-08 17:22:00 +02001912 pdev = platform_device_alloc(DRVNAME, address);
1913 if (!pdev) {
1914 err = -ENOMEM;
Joe Perches18de0302010-10-20 06:51:55 +00001915 pr_err("Device allocation failed\n");
Jean Delvare787c72b2007-05-08 17:22:00 +02001916 goto exit;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001917 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001918
Jean Delvare787c72b2007-05-08 17:22:00 +02001919 err = platform_device_add_resources(pdev, &res, 1);
1920 if (err) {
Joe Perches18de0302010-10-20 06:51:55 +00001921 pr_err("Device resource addition failed (%d)\n", err);
Jean Delvare787c72b2007-05-08 17:22:00 +02001922 goto exit_device_put;
1923 }
1924
Jean Delvare2df6d812007-06-09 10:11:16 -04001925 err = platform_device_add_data(pdev, sio_data,
1926 sizeof(struct w83627hf_sio_data));
1927 if (err) {
Joe Perches18de0302010-10-20 06:51:55 +00001928 pr_err("Platform data allocation failed\n");
Jean Delvare787c72b2007-05-08 17:22:00 +02001929 goto exit_device_put;
1930 }
Jean Delvare787c72b2007-05-08 17:22:00 +02001931
1932 err = platform_device_add(pdev);
1933 if (err) {
Joe Perches18de0302010-10-20 06:51:55 +00001934 pr_err("Device addition failed (%d)\n", err);
Jean Delvare787c72b2007-05-08 17:22:00 +02001935 goto exit_device_put;
1936 }
1937
1938 return 0;
1939
1940exit_device_put:
1941 platform_device_put(pdev);
1942exit:
1943 return err;
1944}
1945
1946static int __init sensors_w83627hf_init(void)
1947{
1948 int err;
1949 unsigned short address;
1950 struct w83627hf_sio_data sio_data;
1951
1952 if (w83627hf_find(0x2e, &address, &sio_data)
1953 && w83627hf_find(0x4e, &address, &sio_data))
1954 return -ENODEV;
1955
1956 err = platform_driver_register(&w83627hf_driver);
1957 if (err)
1958 goto exit;
1959
1960 /* Sets global pdev as a side effect */
1961 err = w83627hf_device_add(address, &sio_data);
1962 if (err)
1963 goto exit_driver;
1964
1965 return 0;
1966
1967exit_driver:
1968 platform_driver_unregister(&w83627hf_driver);
1969exit:
1970 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001971}
1972
1973static void __exit sensors_w83627hf_exit(void)
1974{
Jean Delvare787c72b2007-05-08 17:22:00 +02001975 platform_device_unregister(pdev);
1976 platform_driver_unregister(&w83627hf_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001977}
1978
1979MODULE_AUTHOR("Frodo Looijaard <frodol@dds.nl>, "
1980 "Philip Edelbrock <phil@netroedge.com>, "
1981 "and Mark Studebaker <mdsxyz123@yahoo.com>");
1982MODULE_DESCRIPTION("W83627HF driver");
1983MODULE_LICENSE("GPL");
1984
1985module_init(sensors_w83627hf_init);
1986module_exit(sensors_w83627hf_exit);