blob: 43c0f89cefdf0d1395d011c8f9a287b9d7a8a795 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Guenter Roeck27b9de32012-01-15 11:07:26 -08002 * w83627hf.c - Part of lm_sensors, Linux kernel modules for hardware
3 * monitoring
4 * Copyright (c) 1998 - 2003 Frodo Looijaard <frodol@dds.nl>,
5 * Philip Edelbrock <phil@netroedge.com>,
6 * and Mark Studebaker <mdsxyz123@yahoo.com>
7 * Ported to 2.6 by Bernhard C. Schrenk <clemy@clemy.org>
Jean Delvare7c81c602014-01-29 20:40:08 +01008 * Copyright (c) 2007 - 1012 Jean Delvare <jdelvare@suse.de>
Guenter Roeck27b9de32012-01-15 11:07:26 -08009 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070024
25/*
Guenter Roeck27b9de32012-01-15 11:07:26 -080026 * Supports following chips:
27 *
Jean Delvare4101ece2012-11-05 21:54:40 +010028 * Chip #vin #fanin #pwm #temp wchipid vendid i2c ISA
Guenter Roeck27b9de32012-01-15 11:07:26 -080029 * w83627hf 9 3 2 3 0x20 0x5ca3 no yes(LPC)
30 * w83627thf 7 3 3 3 0x90 0x5ca3 no yes(LPC)
31 * w83637hf 7 3 3 3 0x80 0x5ca3 no yes(LPC)
32 * w83687thf 7 3 3 3 0x90 0x5ca3 no yes(LPC)
33 * w83697hf 8 2 2 2 0x60 0x5ca3 no yes(LPC)
34 *
35 * For other winbond chips, and for i2c support in the above chips,
36 * use w83781d.c.
37 *
38 * Note: automatic ("cruise") fan control for 697, 637 & 627thf not
39 * supported yet.
40 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070041
Joe Perches18de0302010-10-20 06:51:55 +000042#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
43
Linus Torvalds1da177e2005-04-16 15:20:36 -070044#include <linux/module.h>
45#include <linux/init.h>
46#include <linux/slab.h>
47#include <linux/jiffies.h>
Jean Delvare787c72b2007-05-08 17:22:00 +020048#include <linux/platform_device.h>
Mark M. Hoffman943b0832005-07-15 21:39:18 -040049#include <linux/hwmon.h>
Jim Cromie07584c72007-10-12 21:08:00 +020050#include <linux/hwmon-sysfs.h>
Jean Delvare303760b2005-07-31 21:52:01 +020051#include <linux/hwmon-vid.h>
Mark M. Hoffman943b0832005-07-15 21:39:18 -040052#include <linux/err.h>
Ingo Molnar9a61bf62006-01-18 23:19:26 +010053#include <linux/mutex.h>
Jean Delvared27c37c2007-05-08 17:21:59 +020054#include <linux/ioport.h>
Jean Delvareb9acb642009-01-07 16:37:35 +010055#include <linux/acpi.h>
H Hartley Sweeten6055fae2009-09-15 17:18:13 +020056#include <linux/io.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070057#include "lm75.h"
58
Jean Delvare787c72b2007-05-08 17:22:00 +020059static struct platform_device *pdev;
Jean Delvared27c37c2007-05-08 17:21:59 +020060
61#define DRVNAME "w83627hf"
62enum chips { w83627hf, w83627thf, w83697hf, w83637hf, w83687thf };
63
Jean Delvareb72656d2009-12-09 20:35:49 +010064struct w83627hf_sio_data {
65 enum chips type;
66 int sioaddr;
67};
68
Linus Torvalds1da177e2005-04-16 15:20:36 -070069static u8 force_i2c = 0x1f;
70module_param(force_i2c, byte, 0);
71MODULE_PARM_DESC(force_i2c,
72 "Initialize the i2c address of the sensors");
73
Rusty Russell90ab5ee2012-01-13 09:32:20 +103074static bool init = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -070075module_param(init, bool, 0);
76MODULE_PARM_DESC(init, "Set to zero to bypass chip initialization");
77
Jean Delvare67b671b2007-12-06 23:13:42 +010078static unsigned short force_id;
79module_param(force_id, ushort, 0);
80MODULE_PARM_DESC(force_id, "Override the detected device ID");
81
Linus Torvalds1da177e2005-04-16 15:20:36 -070082/* modified from kernel/include/traps.c */
Guenter Roeck27b9de32012-01-15 11:07:26 -080083#define DEV 0x07 /* Register: Logical device select */
Linus Torvalds1da177e2005-04-16 15:20:36 -070084
85/* logical device numbers for superio_select (below) */
86#define W83627HF_LD_FDC 0x00
87#define W83627HF_LD_PRT 0x01
88#define W83627HF_LD_UART1 0x02
89#define W83627HF_LD_UART2 0x03
90#define W83627HF_LD_KBC 0x05
91#define W83627HF_LD_CIR 0x06 /* w83627hf only */
92#define W83627HF_LD_GAME 0x07
93#define W83627HF_LD_MIDI 0x07
94#define W83627HF_LD_GPIO1 0x07
95#define W83627HF_LD_GPIO5 0x07 /* w83627thf only */
96#define W83627HF_LD_GPIO2 0x08
97#define W83627HF_LD_GPIO3 0x09
98#define W83627HF_LD_GPIO4 0x09 /* w83627thf only */
99#define W83627HF_LD_ACPI 0x0a
100#define W83627HF_LD_HWM 0x0b
101
Guenter Roeck27b9de32012-01-15 11:07:26 -0800102#define DEVID 0x20 /* Register: Device ID */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103
104#define W83627THF_GPIO5_EN 0x30 /* w83627thf only */
105#define W83627THF_GPIO5_IOSR 0xf3 /* w83627thf only */
106#define W83627THF_GPIO5_DR 0xf4 /* w83627thf only */
107
Jean Delvarec2db6ce2006-01-18 23:22:12 +0100108#define W83687THF_VID_EN 0x29 /* w83687thf only */
109#define W83687THF_VID_CFG 0xF0 /* w83687thf only */
110#define W83687THF_VID_DATA 0xF1 /* w83687thf only */
111
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112static inline void
Jean Delvareb72656d2009-12-09 20:35:49 +0100113superio_outb(struct w83627hf_sio_data *sio, int reg, int val)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114{
Jean Delvareb72656d2009-12-09 20:35:49 +0100115 outb(reg, sio->sioaddr);
116 outb(val, sio->sioaddr + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117}
118
119static inline int
Jean Delvareb72656d2009-12-09 20:35:49 +0100120superio_inb(struct w83627hf_sio_data *sio, int reg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121{
Jean Delvareb72656d2009-12-09 20:35:49 +0100122 outb(reg, sio->sioaddr);
123 return inb(sio->sioaddr + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124}
125
126static inline void
Jean Delvareb72656d2009-12-09 20:35:49 +0100127superio_select(struct w83627hf_sio_data *sio, int ld)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128{
Jean Delvareb72656d2009-12-09 20:35:49 +0100129 outb(DEV, sio->sioaddr);
130 outb(ld, sio->sioaddr + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131}
132
Guenter Roeck99184172019-04-05 08:44:41 -0700133static inline int
Jean Delvareb72656d2009-12-09 20:35:49 +0100134superio_enter(struct w83627hf_sio_data *sio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135{
Guenter Roeck99184172019-04-05 08:44:41 -0700136 if (!request_muxed_region(sio->sioaddr, 2, DRVNAME))
137 return -EBUSY;
138
Jean Delvareb72656d2009-12-09 20:35:49 +0100139 outb(0x87, sio->sioaddr);
140 outb(0x87, sio->sioaddr);
Guenter Roeck99184172019-04-05 08:44:41 -0700141
142 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143}
144
145static inline void
Jean Delvareb72656d2009-12-09 20:35:49 +0100146superio_exit(struct w83627hf_sio_data *sio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147{
Jean Delvareb72656d2009-12-09 20:35:49 +0100148 outb(0xAA, sio->sioaddr);
Guenter Roeck99184172019-04-05 08:44:41 -0700149 release_region(sio->sioaddr, 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150}
151
152#define W627_DEVID 0x52
153#define W627THF_DEVID 0x82
154#define W697_DEVID 0x60
155#define W637_DEVID 0x70
Jean Delvarec2db6ce2006-01-18 23:22:12 +0100156#define W687THF_DEVID 0x85
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157#define WINB_ACT_REG 0x30
158#define WINB_BASE_REG 0x60
159/* Constants specified below */
160
Petr Vandrovecada0c2f2005-10-07 23:11:03 +0200161/* Alignment of the base address */
162#define WINB_ALIGNMENT ~7
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163
Petr Vandrovecada0c2f2005-10-07 23:11:03 +0200164/* Offset & size of I/O region we are interested in */
165#define WINB_REGION_OFFSET 5
166#define WINB_REGION_SIZE 2
167
Jean Delvare787c72b2007-05-08 17:22:00 +0200168/* Where are the sensors address/data registers relative to the region offset */
169#define W83781D_ADDR_REG_OFFSET 0
170#define W83781D_DATA_REG_OFFSET 1
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171
172/* The W83781D registers */
173/* The W83782D registers for nr=7,8 are in bank 5 */
174#define W83781D_REG_IN_MAX(nr) ((nr < 7) ? (0x2b + (nr) * 2) : \
175 (0x554 + (((nr) - 7) * 2)))
176#define W83781D_REG_IN_MIN(nr) ((nr < 7) ? (0x2c + (nr) * 2) : \
177 (0x555 + (((nr) - 7) * 2)))
178#define W83781D_REG_IN(nr) ((nr < 7) ? (0x20 + (nr)) : \
179 (0x550 + (nr) - 7))
180
Jim Cromie2ca2fcd2007-10-14 17:20:50 -0600181/* nr:0-2 for fans:1-3 */
182#define W83627HF_REG_FAN_MIN(nr) (0x3b + (nr))
183#define W83627HF_REG_FAN(nr) (0x28 + (nr))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184
Jim Cromiedf48ed82007-10-14 17:10:52 -0600185#define W83627HF_REG_TEMP2_CONFIG 0x152
186#define W83627HF_REG_TEMP3_CONFIG 0x252
187/* these are zero-based, unlike config constants above */
188static const u16 w83627hf_reg_temp[] = { 0x27, 0x150, 0x250 };
189static const u16 w83627hf_reg_temp_hyst[] = { 0x3A, 0x153, 0x253 };
190static const u16 w83627hf_reg_temp_over[] = { 0x39, 0x155, 0x255 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191
192#define W83781D_REG_BANK 0x4E
193
194#define W83781D_REG_CONFIG 0x40
Yuan Mu4a1c44472005-11-07 22:19:04 +0100195#define W83781D_REG_ALARM1 0x459
196#define W83781D_REG_ALARM2 0x45A
197#define W83781D_REG_ALARM3 0x45B
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199#define W83781D_REG_BEEP_CONFIG 0x4D
200#define W83781D_REG_BEEP_INTS1 0x56
201#define W83781D_REG_BEEP_INTS2 0x57
202#define W83781D_REG_BEEP_INTS3 0x453
203
204#define W83781D_REG_VID_FANDIV 0x47
205
206#define W83781D_REG_CHIPID 0x49
207#define W83781D_REG_WCHIPID 0x58
208#define W83781D_REG_CHIPMAN 0x4F
209#define W83781D_REG_PIN 0x4B
210
211#define W83781D_REG_VBAT 0x5D
212
213#define W83627HF_REG_PWM1 0x5A
214#define W83627HF_REG_PWM2 0x5B
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215
Dominik Geyera95a5ed2008-08-06 22:41:04 +0200216static const u8 W83627THF_REG_PWM_ENABLE[] = {
217 0x04, /* FAN 1 mode */
218 0x04, /* FAN 2 mode */
219 0x12, /* FAN AUX mode */
220};
221static const u8 W83627THF_PWM_ENABLE_SHIFT[] = { 2, 4, 1 };
222
Jean Delvarec2db6ce2006-01-18 23:22:12 +0100223#define W83627THF_REG_PWM1 0x01 /* 697HF/637HF/687THF too */
224#define W83627THF_REG_PWM2 0x03 /* 697HF/637HF/687THF too */
225#define W83627THF_REG_PWM3 0x11 /* 637HF/687THF too */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226
Jean Delvarec2db6ce2006-01-18 23:22:12 +0100227#define W83627THF_REG_VRM_OVT_CFG 0x18 /* 637HF/687THF too */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228
229static const u8 regpwm_627hf[] = { W83627HF_REG_PWM1, W83627HF_REG_PWM2 };
230static const u8 regpwm[] = { W83627THF_REG_PWM1, W83627THF_REG_PWM2,
231 W83627THF_REG_PWM3 };
232#define W836X7HF_REG_PWM(type, nr) (((type) == w83627hf) ? \
Jim Cromie07584c72007-10-12 21:08:00 +0200233 regpwm_627hf[nr] : regpwm[nr])
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234
Carlos Olalla Martinez1550cb62007-06-09 10:11:16 -0400235#define W83627HF_REG_PWM_FREQ 0x5C /* Only for the 627HF */
236
237#define W83637HF_REG_PWM_FREQ1 0x00 /* 697HF/687THF too */
238#define W83637HF_REG_PWM_FREQ2 0x02 /* 697HF/687THF too */
239#define W83637HF_REG_PWM_FREQ3 0x10 /* 687THF too */
240
241static const u8 W83637HF_REG_PWM_FREQ[] = { W83637HF_REG_PWM_FREQ1,
242 W83637HF_REG_PWM_FREQ2,
243 W83637HF_REG_PWM_FREQ3 };
244
245#define W83627HF_BASE_PWM_FREQ 46870
246
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247#define W83781D_REG_I2C_ADDR 0x48
248#define W83781D_REG_I2C_SUBADDR 0x4A
249
250/* Sensor selection */
251#define W83781D_REG_SCFG1 0x5D
252static const u8 BIT_SCFG1[] = { 0x02, 0x04, 0x08 };
253#define W83781D_REG_SCFG2 0x59
254static const u8 BIT_SCFG2[] = { 0x10, 0x20, 0x40 };
255#define W83781D_DEFAULT_BETA 3435
256
Guenter Roeck27b9de32012-01-15 11:07:26 -0800257/*
258 * Conversions. Limit checking is only done on the TO_REG
259 * variants. Note that you should be a bit careful with which arguments
260 * these macros are called: arguments may be evaluated more than once.
261 * Fixing this is just not worth it.
262 */
Guenter Roeck2a844c12013-01-09 08:09:34 -0800263#define IN_TO_REG(val) (clamp_val((((val) + 8) / 16), 0, 255))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264#define IN_FROM_REG(val) ((val) * 16)
265
266static inline u8 FAN_TO_REG(long rpm, int div)
267{
268 if (rpm == 0)
269 return 255;
Guenter Roeck2a844c12013-01-09 08:09:34 -0800270 rpm = clamp_val(rpm, 1, 1000000);
271 return clamp_val((1350000 + rpm * div / 2) / (rpm * div), 1, 254);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272}
273
274#define TEMP_MIN (-128000)
275#define TEMP_MAX ( 127000)
276
Guenter Roeck27b9de32012-01-15 11:07:26 -0800277/*
278 * TEMP: 0.001C/bit (-128C to +127C)
279 * REG: 1C/bit, two's complement
280 */
Christian Hohnstaedt5bfedac2007-08-16 11:40:10 +0200281static u8 TEMP_TO_REG(long temp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282{
Guenter Roeck2a844c12013-01-09 08:09:34 -0800283 int ntemp = clamp_val(temp, TEMP_MIN, TEMP_MAX);
284 ntemp += (ntemp < 0 ? -500 : 500);
285 return (u8)(ntemp / 1000);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286}
287
288static int TEMP_FROM_REG(u8 reg)
289{
290 return (s8)reg * 1000;
291}
292
293#define FAN_FROM_REG(val,div) ((val)==0?-1:(val)==255?0:1350000/((val)*(div)))
294
Guenter Roeck2a844c12013-01-09 08:09:34 -0800295#define PWM_TO_REG(val) (clamp_val((val), 0, 255))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296
Carlos Olalla Martinez1550cb62007-06-09 10:11:16 -0400297static inline unsigned long pwm_freq_from_reg_627hf(u8 reg)
298{
299 unsigned long freq;
300 freq = W83627HF_BASE_PWM_FREQ >> reg;
301 return freq;
302}
303static inline u8 pwm_freq_to_reg_627hf(unsigned long val)
304{
305 u8 i;
Guenter Roeck27b9de32012-01-15 11:07:26 -0800306 /*
307 * Only 5 dividers (1 2 4 8 16)
308 * Search for the nearest available frequency
309 */
Carlos Olalla Martinez1550cb62007-06-09 10:11:16 -0400310 for (i = 0; i < 4; i++) {
311 if (val > (((W83627HF_BASE_PWM_FREQ >> i) +
312 (W83627HF_BASE_PWM_FREQ >> (i+1))) / 2))
313 break;
314 }
315 return i;
316}
317
318static inline unsigned long pwm_freq_from_reg(u8 reg)
319{
320 /* Clock bit 8 -> 180 kHz or 24 MHz */
321 unsigned long clock = (reg & 0x80) ? 180000UL : 24000000UL;
322
323 reg &= 0x7f;
324 /* This should not happen but anyway... */
325 if (reg == 0)
326 reg++;
Frans Meulenbroeks7fe83ad2012-01-05 19:50:18 +0100327 return clock / (reg << 8);
Carlos Olalla Martinez1550cb62007-06-09 10:11:16 -0400328}
329static inline u8 pwm_freq_to_reg(unsigned long val)
330{
331 /* Minimum divider value is 0x01 and maximum is 0x7F */
332 if (val >= 93750) /* The highest we can do */
333 return 0x01;
334 if (val >= 720) /* Use 24 MHz clock */
Frans Meulenbroeks7fe83ad2012-01-05 19:50:18 +0100335 return 24000000UL / (val << 8);
Carlos Olalla Martinez1550cb62007-06-09 10:11:16 -0400336 if (val < 6) /* The lowest we can do */
337 return 0xFF;
338 else /* Use 180 kHz clock */
Frans Meulenbroeks7fe83ad2012-01-05 19:50:18 +0100339 return 0x80 | (180000UL / (val << 8));
Carlos Olalla Martinez1550cb62007-06-09 10:11:16 -0400340}
341
Jean Delvare1c138102008-01-03 23:04:55 +0100342#define BEEP_MASK_FROM_REG(val) ((val) & 0xff7fff)
343#define BEEP_MASK_TO_REG(val) ((val) & 0xff7fff)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344
345#define DIV_FROM_REG(val) (1 << (val))
346
347static inline u8 DIV_TO_REG(long val)
348{
349 int i;
Guenter Roeck2a844c12013-01-09 08:09:34 -0800350 val = clamp_val(val, 1, 128) >> 1;
Grant Coadyabc01922005-05-12 13:41:51 +1000351 for (i = 0; i < 7; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352 if (val == 0)
353 break;
354 val >>= 1;
355 }
Frans Meulenbroeks7fe83ad2012-01-05 19:50:18 +0100356 return (u8)i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357}
358
Guenter Roeck27b9de32012-01-15 11:07:26 -0800359/*
360 * For each registered chip, we need to keep some data in memory.
361 * The structure is dynamically allocated.
362 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363struct w83627hf_data {
Jean Delvare787c72b2007-05-08 17:22:00 +0200364 unsigned short addr;
365 const char *name;
Tony Jones1beeffe2007-08-20 13:46:20 -0700366 struct device *hwmon_dev;
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100367 struct mutex lock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368 enum chips type;
369
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100370 struct mutex update_lock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371 char valid; /* !=0 if following fields are valid */
372 unsigned long last_updated; /* In jiffies */
373
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374 u8 in[9]; /* Register value */
375 u8 in_max[9]; /* Register value */
376 u8 in_min[9]; /* Register value */
377 u8 fan[3]; /* Register value */
378 u8 fan_min[3]; /* Register value */
Jim Cromiedf48ed82007-10-14 17:10:52 -0600379 u16 temp[3]; /* Register value */
380 u16 temp_max[3]; /* Register value */
381 u16 temp_max_hyst[3]; /* Register value */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382 u8 fan_div[3]; /* Register encoding, shifted right */
383 u8 vid; /* Register encoding, combined */
384 u32 alarms; /* Register encoding, combined */
385 u32 beep_mask; /* Register encoding, combined */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386 u8 pwm[3]; /* Register value */
Dominik Geyera95a5ed2008-08-06 22:41:04 +0200387 u8 pwm_enable[3]; /* 1 = manual
Guenter Roeck27b9de32012-01-15 11:07:26 -0800388 * 2 = thermal cruise (also called SmartFan I)
389 * 3 = fan speed cruise
390 */
Carlos Olalla Martinez1550cb62007-06-09 10:11:16 -0400391 u8 pwm_freq[3]; /* Register value */
Jean Delvareb26f9332007-08-16 14:30:01 +0200392 u16 sens[3]; /* 1 = pentium diode; 2 = 3904 diode;
Guenter Roeck27b9de32012-01-15 11:07:26 -0800393 * 4 = thermistor
394 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395 u8 vrm;
Jean Delvarec2db6ce2006-01-18 23:22:12 +0100396 u8 vrm_ovt; /* Register value, 627THF/637HF/687THF only */
Jean Delvare275b7d62012-12-19 22:16:59 +0100397
398#ifdef CONFIG_PM
399 /* Remember extra register values over suspend/resume */
400 u8 scfg1;
401 u8 scfg2;
402#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403};
404
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405
Jean Delvare787c72b2007-05-08 17:22:00 +0200406static int w83627hf_probe(struct platform_device *pdev);
Bill Pemberton281dfd02012-11-19 13:25:51 -0500407static int w83627hf_remove(struct platform_device *pdev);
Jean Delvare787c72b2007-05-08 17:22:00 +0200408
409static int w83627hf_read_value(struct w83627hf_data *data, u16 reg);
410static int w83627hf_write_value(struct w83627hf_data *data, u16 reg, u16 value);
Jean Delvarec09c5182007-10-12 21:53:07 +0200411static void w83627hf_update_fan_div(struct w83627hf_data *data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412static struct w83627hf_data *w83627hf_update_device(struct device *dev);
Jean Delvare787c72b2007-05-08 17:22:00 +0200413static void w83627hf_init_device(struct platform_device *pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414
Jean Delvare275b7d62012-12-19 22:16:59 +0100415#ifdef CONFIG_PM
416static int w83627hf_suspend(struct device *dev)
417{
418 struct w83627hf_data *data = w83627hf_update_device(dev);
419
420 mutex_lock(&data->update_lock);
421 data->scfg1 = w83627hf_read_value(data, W83781D_REG_SCFG1);
422 data->scfg2 = w83627hf_read_value(data, W83781D_REG_SCFG2);
423 mutex_unlock(&data->update_lock);
424
425 return 0;
426}
427
428static int w83627hf_resume(struct device *dev)
429{
430 struct w83627hf_data *data = dev_get_drvdata(dev);
431 int i, num_temps = (data->type == w83697hf) ? 2 : 3;
432
433 /* Restore limits */
434 mutex_lock(&data->update_lock);
435 for (i = 0; i <= 8; i++) {
436 /* skip missing sensors */
437 if (((data->type == w83697hf) && (i == 1)) ||
438 ((data->type != w83627hf && data->type != w83697hf)
439 && (i == 5 || i == 6)))
440 continue;
441 w83627hf_write_value(data, W83781D_REG_IN_MAX(i),
442 data->in_max[i]);
443 w83627hf_write_value(data, W83781D_REG_IN_MIN(i),
444 data->in_min[i]);
445 }
446 for (i = 0; i <= 2; i++)
447 w83627hf_write_value(data, W83627HF_REG_FAN_MIN(i),
448 data->fan_min[i]);
449 for (i = 0; i < num_temps; i++) {
450 w83627hf_write_value(data, w83627hf_reg_temp_over[i],
451 data->temp_max[i]);
452 w83627hf_write_value(data, w83627hf_reg_temp_hyst[i],
453 data->temp_max_hyst[i]);
454 }
455
456 /* Fixup BIOS bugs */
457 if (data->type == w83627thf || data->type == w83637hf ||
458 data->type == w83687thf)
459 w83627hf_write_value(data, W83627THF_REG_VRM_OVT_CFG,
460 data->vrm_ovt);
461 w83627hf_write_value(data, W83781D_REG_SCFG1, data->scfg1);
462 w83627hf_write_value(data, W83781D_REG_SCFG2, data->scfg2);
463
464 /* Force re-reading all values */
465 data->valid = 0;
466 mutex_unlock(&data->update_lock);
467
468 return 0;
469}
470
471static const struct dev_pm_ops w83627hf_dev_pm_ops = {
472 .suspend = w83627hf_suspend,
473 .resume = w83627hf_resume,
474};
475
476#define W83627HF_DEV_PM_OPS (&w83627hf_dev_pm_ops)
477#else
478#define W83627HF_DEV_PM_OPS NULL
479#endif /* CONFIG_PM */
480
Jean Delvare787c72b2007-05-08 17:22:00 +0200481static struct platform_driver w83627hf_driver = {
Laurent Riffardcdaf7932005-11-26 20:37:41 +0100482 .driver = {
Jean Delvared27c37c2007-05-08 17:21:59 +0200483 .name = DRVNAME,
Jean Delvare275b7d62012-12-19 22:16:59 +0100484 .pm = W83627HF_DEV_PM_OPS,
Laurent Riffardcdaf7932005-11-26 20:37:41 +0100485 },
Jean Delvare787c72b2007-05-08 17:22:00 +0200486 .probe = w83627hf_probe,
Bill Pemberton9e5e9b72012-11-19 13:21:20 -0500487 .remove = w83627hf_remove,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488};
489
Jim Cromie07584c72007-10-12 21:08:00 +0200490static ssize_t
491show_in_input(struct device *dev, struct device_attribute *devattr, char *buf)
492{
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[nr]));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496}
Jim Cromie07584c72007-10-12 21:08:00 +0200497static ssize_t
498show_in_min(struct device *dev, struct device_attribute *devattr, char *buf)
499{
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_min[nr]));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503}
Jim Cromie07584c72007-10-12 21:08:00 +0200504static ssize_t
505show_in_max(struct device *dev, struct device_attribute *devattr, char *buf)
506{
507 int nr = to_sensor_dev_attr(devattr)->index;
508 struct w83627hf_data *data = w83627hf_update_device(dev);
509 return sprintf(buf, "%ld\n", (long)IN_FROM_REG(data->in_max[nr]));
510}
511static ssize_t
512store_in_min(struct device *dev, struct device_attribute *devattr,
513 const char *buf, size_t count)
514{
515 int nr = to_sensor_dev_attr(devattr)->index;
516 struct w83627hf_data *data = dev_get_drvdata(dev);
Guenter Roeck27b9de32012-01-15 11:07:26 -0800517 long val;
518 int err;
519
520 err = kstrtol(buf, 10, &val);
521 if (err)
522 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523
Jim Cromie07584c72007-10-12 21:08:00 +0200524 mutex_lock(&data->update_lock);
525 data->in_min[nr] = IN_TO_REG(val);
526 w83627hf_write_value(data, W83781D_REG_IN_MIN(nr), data->in_min[nr]);
527 mutex_unlock(&data->update_lock);
528 return count;
529}
530static ssize_t
531store_in_max(struct device *dev, struct device_attribute *devattr,
532 const char *buf, size_t count)
533{
534 int nr = to_sensor_dev_attr(devattr)->index;
535 struct w83627hf_data *data = dev_get_drvdata(dev);
Guenter Roeck27b9de32012-01-15 11:07:26 -0800536 long val;
537 int err;
538
539 err = kstrtol(buf, 10, &val);
540 if (err)
541 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542
Jim Cromie07584c72007-10-12 21:08:00 +0200543 mutex_lock(&data->update_lock);
544 data->in_max[nr] = IN_TO_REG(val);
545 w83627hf_write_value(data, W83781D_REG_IN_MAX(nr), data->in_max[nr]);
546 mutex_unlock(&data->update_lock);
547 return count;
548}
549#define sysfs_vin_decl(offset) \
550static SENSOR_DEVICE_ATTR(in##offset##_input, S_IRUGO, \
551 show_in_input, NULL, offset); \
552static SENSOR_DEVICE_ATTR(in##offset##_min, S_IRUGO|S_IWUSR, \
553 show_in_min, store_in_min, offset); \
554static SENSOR_DEVICE_ATTR(in##offset##_max, S_IRUGO|S_IWUSR, \
555 show_in_max, store_in_max, offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556
Jim Cromie07584c72007-10-12 21:08:00 +0200557sysfs_vin_decl(1);
558sysfs_vin_decl(2);
559sysfs_vin_decl(3);
560sysfs_vin_decl(4);
561sysfs_vin_decl(5);
562sysfs_vin_decl(6);
563sysfs_vin_decl(7);
564sysfs_vin_decl(8);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565
566/* use a different set of functions for in0 */
567static ssize_t show_in_0(struct w83627hf_data *data, char *buf, u8 reg)
568{
569 long in0;
570
571 if ((data->vrm_ovt & 0x01) &&
Jean Delvarec2db6ce2006-01-18 23:22:12 +0100572 (w83627thf == data->type || w83637hf == data->type
573 || w83687thf == data->type))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574
575 /* use VRM9 calculation */
576 in0 = (long)((reg * 488 + 70000 + 50) / 100);
577 else
578 /* use VRM8 (standard) calculation */
579 in0 = (long)IN_FROM_REG(reg);
580
581 return sprintf(buf,"%ld\n", in0);
582}
583
Yani Ioannoua5099cf2005-05-17 06:42:25 -0400584static ssize_t show_regs_in_0(struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585{
586 struct w83627hf_data *data = w83627hf_update_device(dev);
587 return show_in_0(data, buf, data->in[0]);
588}
589
Yani Ioannoua5099cf2005-05-17 06:42:25 -0400590static ssize_t show_regs_in_min0(struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591{
592 struct w83627hf_data *data = w83627hf_update_device(dev);
593 return show_in_0(data, buf, data->in_min[0]);
594}
595
Yani Ioannoua5099cf2005-05-17 06:42:25 -0400596static ssize_t show_regs_in_max0(struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597{
598 struct w83627hf_data *data = w83627hf_update_device(dev);
599 return show_in_0(data, buf, data->in_max[0]);
600}
601
Yani Ioannoua5099cf2005-05-17 06:42:25 -0400602static ssize_t store_regs_in_min0(struct device *dev, struct device_attribute *attr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603 const char *buf, size_t count)
604{
Jean Delvare787c72b2007-05-08 17:22:00 +0200605 struct w83627hf_data *data = dev_get_drvdata(dev);
Guenter Roeck27b9de32012-01-15 11:07:26 -0800606 unsigned long val;
607 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608
Guenter Roeck27b9de32012-01-15 11:07:26 -0800609 err = kstrtoul(buf, 10, &val);
610 if (err)
611 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100613 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614
615 if ((data->vrm_ovt & 0x01) &&
Jean Delvarec2db6ce2006-01-18 23:22:12 +0100616 (w83627thf == data->type || w83637hf == data->type
617 || w83687thf == data->type))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618
619 /* use VRM9 calculation */
Yuan Mu2723ab92005-11-23 15:44:21 -0800620 data->in_min[0] =
Guenter Roeck2a844c12013-01-09 08:09:34 -0800621 clamp_val(((val * 100) - 70000 + 244) / 488, 0, 255);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622 else
623 /* use VRM8 (standard) calculation */
624 data->in_min[0] = IN_TO_REG(val);
625
Jean Delvare787c72b2007-05-08 17:22:00 +0200626 w83627hf_write_value(data, W83781D_REG_IN_MIN(0), data->in_min[0]);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100627 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628 return count;
629}
630
Yani Ioannoua5099cf2005-05-17 06:42:25 -0400631static ssize_t store_regs_in_max0(struct device *dev, struct device_attribute *attr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632 const char *buf, size_t count)
633{
Jean Delvare787c72b2007-05-08 17:22:00 +0200634 struct w83627hf_data *data = dev_get_drvdata(dev);
Guenter Roeck27b9de32012-01-15 11:07:26 -0800635 unsigned long val;
636 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637
Guenter Roeck27b9de32012-01-15 11:07:26 -0800638 err = kstrtoul(buf, 10, &val);
639 if (err)
640 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100642 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643
644 if ((data->vrm_ovt & 0x01) &&
Jean Delvarec2db6ce2006-01-18 23:22:12 +0100645 (w83627thf == data->type || w83637hf == data->type
646 || w83687thf == data->type))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647
648 /* use VRM9 calculation */
Yuan Mu2723ab92005-11-23 15:44:21 -0800649 data->in_max[0] =
Guenter Roeck2a844c12013-01-09 08:09:34 -0800650 clamp_val(((val * 100) - 70000 + 244) / 488, 0, 255);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651 else
652 /* use VRM8 (standard) calculation */
653 data->in_max[0] = IN_TO_REG(val);
654
Jean Delvare787c72b2007-05-08 17:22:00 +0200655 w83627hf_write_value(data, W83781D_REG_IN_MAX(0), data->in_max[0]);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100656 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657 return count;
658}
659
660static DEVICE_ATTR(in0_input, S_IRUGO, show_regs_in_0, NULL);
661static DEVICE_ATTR(in0_min, S_IRUGO | S_IWUSR,
662 show_regs_in_min0, store_regs_in_min0);
663static DEVICE_ATTR(in0_max, S_IRUGO | S_IWUSR,
664 show_regs_in_max0, store_regs_in_max0);
665
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666static ssize_t
Jim Cromie07584c72007-10-12 21:08:00 +0200667show_fan_input(struct device *dev, struct device_attribute *devattr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668{
Jim Cromie07584c72007-10-12 21:08:00 +0200669 int nr = to_sensor_dev_attr(devattr)->index;
670 struct w83627hf_data *data = w83627hf_update_device(dev);
671 return sprintf(buf, "%ld\n", FAN_FROM_REG(data->fan[nr],
672 (long)DIV_FROM_REG(data->fan_div[nr])));
673}
674static ssize_t
675show_fan_min(struct device *dev, struct device_attribute *devattr, char *buf)
676{
677 int nr = to_sensor_dev_attr(devattr)->index;
678 struct w83627hf_data *data = w83627hf_update_device(dev);
679 return sprintf(buf, "%ld\n", FAN_FROM_REG(data->fan_min[nr],
680 (long)DIV_FROM_REG(data->fan_div[nr])));
681}
682static ssize_t
683store_fan_min(struct device *dev, struct device_attribute *devattr,
684 const char *buf, size_t count)
685{
686 int nr = to_sensor_dev_attr(devattr)->index;
Jean Delvare787c72b2007-05-08 17:22:00 +0200687 struct w83627hf_data *data = dev_get_drvdata(dev);
Guenter Roeck27b9de32012-01-15 11:07:26 -0800688 unsigned long val;
689 int err;
690
691 err = kstrtoul(buf, 10, &val);
692 if (err)
693 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100695 mutex_lock(&data->update_lock);
Jim Cromie07584c72007-10-12 21:08:00 +0200696 data->fan_min[nr] = FAN_TO_REG(val, DIV_FROM_REG(data->fan_div[nr]));
Jim Cromie2ca2fcd2007-10-14 17:20:50 -0600697 w83627hf_write_value(data, W83627HF_REG_FAN_MIN(nr),
Jim Cromie07584c72007-10-12 21:08:00 +0200698 data->fan_min[nr]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100700 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701 return count;
702}
Jim Cromie07584c72007-10-12 21:08:00 +0200703#define sysfs_fan_decl(offset) \
704static SENSOR_DEVICE_ATTR(fan##offset##_input, S_IRUGO, \
705 show_fan_input, NULL, offset - 1); \
706static SENSOR_DEVICE_ATTR(fan##offset##_min, S_IRUGO | S_IWUSR, \
707 show_fan_min, store_fan_min, offset - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708
Jim Cromie07584c72007-10-12 21:08:00 +0200709sysfs_fan_decl(1);
710sysfs_fan_decl(2);
711sysfs_fan_decl(3);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712
Jim Cromie07584c72007-10-12 21:08:00 +0200713static ssize_t
714show_temp(struct device *dev, struct device_attribute *devattr, char *buf)
715{
716 int nr = to_sensor_dev_attr(devattr)->index;
717 struct w83627hf_data *data = w83627hf_update_device(dev);
Jim Cromiedf48ed82007-10-14 17:10:52 -0600718
719 u16 tmp = data->temp[nr];
720 return sprintf(buf, "%ld\n", (nr) ? (long) LM75_TEMP_FROM_REG(tmp)
721 : (long) TEMP_FROM_REG(tmp));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723
Jim Cromie07584c72007-10-12 21:08:00 +0200724static ssize_t
725show_temp_max(struct device *dev, struct device_attribute *devattr,
726 char *buf)
727{
728 int nr = to_sensor_dev_attr(devattr)->index;
729 struct w83627hf_data *data = w83627hf_update_device(dev);
Jim Cromiedf48ed82007-10-14 17:10:52 -0600730
731 u16 tmp = data->temp_max[nr];
732 return sprintf(buf, "%ld\n", (nr) ? (long) LM75_TEMP_FROM_REG(tmp)
733 : (long) TEMP_FROM_REG(tmp));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735
Jim Cromie07584c72007-10-12 21:08:00 +0200736static ssize_t
737show_temp_max_hyst(struct device *dev, struct device_attribute *devattr,
738 char *buf)
739{
740 int nr = to_sensor_dev_attr(devattr)->index;
741 struct w83627hf_data *data = w83627hf_update_device(dev);
Jim Cromiedf48ed82007-10-14 17:10:52 -0600742
743 u16 tmp = data->temp_max_hyst[nr];
744 return sprintf(buf, "%ld\n", (nr) ? (long) LM75_TEMP_FROM_REG(tmp)
745 : (long) TEMP_FROM_REG(tmp));
Jim Cromie07584c72007-10-12 21:08:00 +0200746}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747
Jim Cromie07584c72007-10-12 21:08:00 +0200748static ssize_t
749store_temp_max(struct device *dev, struct device_attribute *devattr,
750 const char *buf, size_t count)
751{
752 int nr = to_sensor_dev_attr(devattr)->index;
753 struct w83627hf_data *data = dev_get_drvdata(dev);
Guenter Roeck27b9de32012-01-15 11:07:26 -0800754 u16 tmp;
755 long val;
756 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757
Guenter Roeck27b9de32012-01-15 11:07:26 -0800758 err = kstrtol(buf, 10, &val);
759 if (err)
760 return err;
761
762 tmp = (nr) ? LM75_TEMP_TO_REG(val) : TEMP_TO_REG(val);
Jim Cromie07584c72007-10-12 21:08:00 +0200763 mutex_lock(&data->update_lock);
Jim Cromiedf48ed82007-10-14 17:10:52 -0600764 data->temp_max[nr] = tmp;
765 w83627hf_write_value(data, w83627hf_reg_temp_over[nr], tmp);
Jim Cromie07584c72007-10-12 21:08:00 +0200766 mutex_unlock(&data->update_lock);
767 return count;
768}
769
770static ssize_t
771store_temp_max_hyst(struct device *dev, struct device_attribute *devattr,
772 const char *buf, size_t count)
773{
774 int nr = to_sensor_dev_attr(devattr)->index;
775 struct w83627hf_data *data = dev_get_drvdata(dev);
Guenter Roeck27b9de32012-01-15 11:07:26 -0800776 u16 tmp;
777 long val;
778 int err;
Jim Cromie07584c72007-10-12 21:08:00 +0200779
Guenter Roeck27b9de32012-01-15 11:07:26 -0800780 err = kstrtol(buf, 10, &val);
781 if (err)
782 return err;
783
784 tmp = (nr) ? LM75_TEMP_TO_REG(val) : TEMP_TO_REG(val);
Jim Cromie07584c72007-10-12 21:08:00 +0200785 mutex_lock(&data->update_lock);
Jim Cromiedf48ed82007-10-14 17:10:52 -0600786 data->temp_max_hyst[nr] = tmp;
787 w83627hf_write_value(data, w83627hf_reg_temp_hyst[nr], tmp);
Jim Cromie07584c72007-10-12 21:08:00 +0200788 mutex_unlock(&data->update_lock);
789 return count;
790}
791
792#define sysfs_temp_decl(offset) \
793static SENSOR_DEVICE_ATTR(temp##offset##_input, S_IRUGO, \
Jim Cromiedf48ed82007-10-14 17:10:52 -0600794 show_temp, NULL, offset - 1); \
Jim Cromie07584c72007-10-12 21:08:00 +0200795static SENSOR_DEVICE_ATTR(temp##offset##_max, S_IRUGO|S_IWUSR, \
Jim Cromiedf48ed82007-10-14 17:10:52 -0600796 show_temp_max, store_temp_max, offset - 1); \
Jim Cromie07584c72007-10-12 21:08:00 +0200797static SENSOR_DEVICE_ATTR(temp##offset##_max_hyst, S_IRUGO|S_IWUSR, \
Jim Cromiedf48ed82007-10-14 17:10:52 -0600798 show_temp_max_hyst, store_temp_max_hyst, offset - 1);
Jim Cromie07584c72007-10-12 21:08:00 +0200799
800sysfs_temp_decl(1);
801sysfs_temp_decl(2);
802sysfs_temp_decl(3);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804static ssize_t
Yani Ioannoua5099cf2005-05-17 06:42:25 -0400805show_vid_reg(struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806{
807 struct w83627hf_data *data = w83627hf_update_device(dev);
808 return sprintf(buf, "%ld\n", (long) vid_from_reg(data->vid, data->vrm));
809}
810static DEVICE_ATTR(cpu0_vid, S_IRUGO, show_vid_reg, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811
812static ssize_t
Yani Ioannoua5099cf2005-05-17 06:42:25 -0400813show_vrm_reg(struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814{
Jean Delvare90d66192007-10-08 18:24:35 +0200815 struct w83627hf_data *data = dev_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816 return sprintf(buf, "%ld\n", (long) data->vrm);
817}
818static ssize_t
Yani Ioannoua5099cf2005-05-17 06:42:25 -0400819store_vrm_reg(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820{
Jean Delvare787c72b2007-05-08 17:22:00 +0200821 struct w83627hf_data *data = dev_get_drvdata(dev);
Guenter Roeck27b9de32012-01-15 11:07:26 -0800822 unsigned long val;
823 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824
Guenter Roeck27b9de32012-01-15 11:07:26 -0800825 err = kstrtoul(buf, 10, &val);
826 if (err)
827 return err;
Axel Lin970255b2014-08-06 08:27:11 +0800828
829 if (val > 255)
830 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831 data->vrm = val;
832
833 return count;
834}
835static DEVICE_ATTR(vrm, S_IRUGO | S_IWUSR, show_vrm_reg, store_vrm_reg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836
837static ssize_t
Yani Ioannoua5099cf2005-05-17 06:42:25 -0400838show_alarms_reg(struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839{
840 struct w83627hf_data *data = w83627hf_update_device(dev);
841 return sprintf(buf, "%ld\n", (long) data->alarms);
842}
843static DEVICE_ATTR(alarms, S_IRUGO, show_alarms_reg, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844
Jean Delvaree3604c62008-01-03 23:00:30 +0100845static ssize_t
846show_alarm(struct device *dev, struct device_attribute *attr, char *buf)
847{
848 struct w83627hf_data *data = w83627hf_update_device(dev);
849 int bitnr = to_sensor_dev_attr(attr)->index;
850 return sprintf(buf, "%u\n", (data->alarms >> bitnr) & 1);
851}
852static SENSOR_DEVICE_ATTR(in0_alarm, S_IRUGO, show_alarm, NULL, 0);
853static SENSOR_DEVICE_ATTR(in1_alarm, S_IRUGO, show_alarm, NULL, 1);
854static SENSOR_DEVICE_ATTR(in2_alarm, S_IRUGO, show_alarm, NULL, 2);
855static SENSOR_DEVICE_ATTR(in3_alarm, S_IRUGO, show_alarm, NULL, 3);
856static SENSOR_DEVICE_ATTR(in4_alarm, S_IRUGO, show_alarm, NULL, 8);
857static SENSOR_DEVICE_ATTR(in5_alarm, S_IRUGO, show_alarm, NULL, 9);
858static SENSOR_DEVICE_ATTR(in6_alarm, S_IRUGO, show_alarm, NULL, 10);
859static SENSOR_DEVICE_ATTR(in7_alarm, S_IRUGO, show_alarm, NULL, 16);
860static SENSOR_DEVICE_ATTR(in8_alarm, S_IRUGO, show_alarm, NULL, 17);
861static SENSOR_DEVICE_ATTR(fan1_alarm, S_IRUGO, show_alarm, NULL, 6);
862static SENSOR_DEVICE_ATTR(fan2_alarm, S_IRUGO, show_alarm, NULL, 7);
863static SENSOR_DEVICE_ATTR(fan3_alarm, S_IRUGO, show_alarm, NULL, 11);
864static SENSOR_DEVICE_ATTR(temp1_alarm, S_IRUGO, show_alarm, NULL, 4);
865static SENSOR_DEVICE_ATTR(temp2_alarm, S_IRUGO, show_alarm, NULL, 5);
866static SENSOR_DEVICE_ATTR(temp3_alarm, S_IRUGO, show_alarm, NULL, 13);
867
Jean Delvare1c138102008-01-03 23:04:55 +0100868static ssize_t
869show_beep_mask(struct device *dev, struct device_attribute *attr, char *buf)
870{
871 struct w83627hf_data *data = w83627hf_update_device(dev);
872 return sprintf(buf, "%ld\n",
873 (long)BEEP_MASK_FROM_REG(data->beep_mask));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875
876static ssize_t
Jean Delvare1c138102008-01-03 23:04:55 +0100877store_beep_mask(struct device *dev, struct device_attribute *attr,
878 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879{
Jean Delvare787c72b2007-05-08 17:22:00 +0200880 struct w83627hf_data *data = dev_get_drvdata(dev);
Jean Delvare1c138102008-01-03 23:04:55 +0100881 unsigned long val;
Guenter Roeck27b9de32012-01-15 11:07:26 -0800882 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883
Guenter Roeck27b9de32012-01-15 11:07:26 -0800884 err = kstrtoul(buf, 10, &val);
885 if (err)
886 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100888 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889
Jean Delvare1c138102008-01-03 23:04:55 +0100890 /* preserve beep enable */
891 data->beep_mask = (data->beep_mask & 0x8000)
892 | BEEP_MASK_TO_REG(val);
893 w83627hf_write_value(data, W83781D_REG_BEEP_INTS1,
894 data->beep_mask & 0xff);
895 w83627hf_write_value(data, W83781D_REG_BEEP_INTS3,
896 ((data->beep_mask) >> 16) & 0xff);
Jean Delvare787c72b2007-05-08 17:22:00 +0200897 w83627hf_write_value(data, W83781D_REG_BEEP_INTS2,
Jean Delvare1c138102008-01-03 23:04:55 +0100898 (data->beep_mask >> 8) & 0xff);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100900 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901 return count;
902}
903
Jean Delvare1c138102008-01-03 23:04:55 +0100904static DEVICE_ATTR(beep_mask, S_IRUGO | S_IWUSR,
905 show_beep_mask, store_beep_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907static ssize_t
Jean Delvaree3604c62008-01-03 23:00:30 +0100908show_beep(struct device *dev, struct device_attribute *attr, char *buf)
909{
910 struct w83627hf_data *data = w83627hf_update_device(dev);
911 int bitnr = to_sensor_dev_attr(attr)->index;
912 return sprintf(buf, "%u\n", (data->beep_mask >> bitnr) & 1);
913}
914
915static ssize_t
916store_beep(struct device *dev, struct device_attribute *attr,
917 const char *buf, size_t count)
918{
919 struct w83627hf_data *data = dev_get_drvdata(dev);
920 int bitnr = to_sensor_dev_attr(attr)->index;
Jean Delvaree3604c62008-01-03 23:00:30 +0100921 u8 reg;
Guenter Roeck27b9de32012-01-15 11:07:26 -0800922 unsigned long bit;
923 int err;
Jean Delvaree3604c62008-01-03 23:00:30 +0100924
Guenter Roeck27b9de32012-01-15 11:07:26 -0800925 err = kstrtoul(buf, 10, &bit);
926 if (err)
927 return err;
928
Jean Delvaree3604c62008-01-03 23:00:30 +0100929 if (bit & ~1)
930 return -EINVAL;
931
932 mutex_lock(&data->update_lock);
933 if (bit)
934 data->beep_mask |= (1 << bitnr);
935 else
936 data->beep_mask &= ~(1 << bitnr);
937
938 if (bitnr < 8) {
939 reg = w83627hf_read_value(data, W83781D_REG_BEEP_INTS1);
940 if (bit)
941 reg |= (1 << bitnr);
942 else
943 reg &= ~(1 << bitnr);
944 w83627hf_write_value(data, W83781D_REG_BEEP_INTS1, reg);
945 } else if (bitnr < 16) {
946 reg = w83627hf_read_value(data, W83781D_REG_BEEP_INTS2);
947 if (bit)
948 reg |= (1 << (bitnr - 8));
949 else
950 reg &= ~(1 << (bitnr - 8));
951 w83627hf_write_value(data, W83781D_REG_BEEP_INTS2, reg);
952 } else {
953 reg = w83627hf_read_value(data, W83781D_REG_BEEP_INTS3);
954 if (bit)
955 reg |= (1 << (bitnr - 16));
956 else
957 reg &= ~(1 << (bitnr - 16));
958 w83627hf_write_value(data, W83781D_REG_BEEP_INTS3, reg);
959 }
960 mutex_unlock(&data->update_lock);
961
962 return count;
963}
964
965static SENSOR_DEVICE_ATTR(in0_beep, S_IRUGO | S_IWUSR,
966 show_beep, store_beep, 0);
967static SENSOR_DEVICE_ATTR(in1_beep, S_IRUGO | S_IWUSR,
968 show_beep, store_beep, 1);
969static SENSOR_DEVICE_ATTR(in2_beep, S_IRUGO | S_IWUSR,
970 show_beep, store_beep, 2);
971static SENSOR_DEVICE_ATTR(in3_beep, S_IRUGO | S_IWUSR,
972 show_beep, store_beep, 3);
973static SENSOR_DEVICE_ATTR(in4_beep, S_IRUGO | S_IWUSR,
974 show_beep, store_beep, 8);
975static SENSOR_DEVICE_ATTR(in5_beep, S_IRUGO | S_IWUSR,
976 show_beep, store_beep, 9);
977static SENSOR_DEVICE_ATTR(in6_beep, S_IRUGO | S_IWUSR,
978 show_beep, store_beep, 10);
979static SENSOR_DEVICE_ATTR(in7_beep, S_IRUGO | S_IWUSR,
980 show_beep, store_beep, 16);
981static SENSOR_DEVICE_ATTR(in8_beep, S_IRUGO | S_IWUSR,
982 show_beep, store_beep, 17);
983static SENSOR_DEVICE_ATTR(fan1_beep, S_IRUGO | S_IWUSR,
984 show_beep, store_beep, 6);
985static SENSOR_DEVICE_ATTR(fan2_beep, S_IRUGO | S_IWUSR,
986 show_beep, store_beep, 7);
987static SENSOR_DEVICE_ATTR(fan3_beep, S_IRUGO | S_IWUSR,
988 show_beep, store_beep, 11);
989static SENSOR_DEVICE_ATTR(temp1_beep, S_IRUGO | S_IWUSR,
990 show_beep, store_beep, 4);
991static SENSOR_DEVICE_ATTR(temp2_beep, S_IRUGO | S_IWUSR,
992 show_beep, store_beep, 5);
993static SENSOR_DEVICE_ATTR(temp3_beep, S_IRUGO | S_IWUSR,
994 show_beep, store_beep, 13);
Jean Delvare1c138102008-01-03 23:04:55 +0100995static SENSOR_DEVICE_ATTR(beep_enable, S_IRUGO | S_IWUSR,
996 show_beep, store_beep, 15);
Jean Delvaree3604c62008-01-03 23:00:30 +0100997
998static ssize_t
Jim Cromie07584c72007-10-12 21:08:00 +0200999show_fan_div(struct device *dev, struct device_attribute *devattr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000{
Jim Cromie07584c72007-10-12 21:08:00 +02001001 int nr = to_sensor_dev_attr(devattr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002 struct w83627hf_data *data = w83627hf_update_device(dev);
1003 return sprintf(buf, "%ld\n",
Jim Cromie07584c72007-10-12 21:08:00 +02001004 (long) DIV_FROM_REG(data->fan_div[nr]));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001005}
Guenter Roeck27b9de32012-01-15 11:07:26 -08001006/*
1007 * Note: we save and restore the fan minimum here, because its value is
1008 * determined in part by the fan divisor. This follows the principle of
1009 * least surprise; the user doesn't expect the fan minimum to change just
1010 * because the divisor changed.
1011 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001012static ssize_t
Jim Cromie07584c72007-10-12 21:08:00 +02001013store_fan_div(struct device *dev, struct device_attribute *devattr,
1014 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015{
Jim Cromie07584c72007-10-12 21:08:00 +02001016 int nr = to_sensor_dev_attr(devattr)->index;
Jean Delvare787c72b2007-05-08 17:22:00 +02001017 struct w83627hf_data *data = dev_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001018 unsigned long min;
1019 u8 reg;
Guenter Roeck27b9de32012-01-15 11:07:26 -08001020 unsigned long val;
1021 int err;
1022
1023 err = kstrtoul(buf, 10, &val);
1024 if (err)
1025 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001026
Ingo Molnar9a61bf62006-01-18 23:19:26 +01001027 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001028
1029 /* Save fan_min */
1030 min = FAN_FROM_REG(data->fan_min[nr],
1031 DIV_FROM_REG(data->fan_div[nr]));
1032
1033 data->fan_div[nr] = DIV_TO_REG(val);
1034
Jean Delvare787c72b2007-05-08 17:22:00 +02001035 reg = (w83627hf_read_value(data, nr==2 ? W83781D_REG_PIN : W83781D_REG_VID_FANDIV)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036 & (nr==0 ? 0xcf : 0x3f))
1037 | ((data->fan_div[nr] & 0x03) << (nr==0 ? 4 : 6));
Jean Delvare787c72b2007-05-08 17:22:00 +02001038 w83627hf_write_value(data, nr==2 ? W83781D_REG_PIN : W83781D_REG_VID_FANDIV, reg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001039
Jean Delvare787c72b2007-05-08 17:22:00 +02001040 reg = (w83627hf_read_value(data, W83781D_REG_VBAT)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001041 & ~(1 << (5 + nr)))
1042 | ((data->fan_div[nr] & 0x04) << (3 + nr));
Jean Delvare787c72b2007-05-08 17:22:00 +02001043 w83627hf_write_value(data, W83781D_REG_VBAT, reg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001044
1045 /* Restore fan_min */
1046 data->fan_min[nr] = FAN_TO_REG(min, DIV_FROM_REG(data->fan_div[nr]));
Jim Cromie2ca2fcd2007-10-14 17:20:50 -06001047 w83627hf_write_value(data, W83627HF_REG_FAN_MIN(nr), data->fan_min[nr]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001048
Ingo Molnar9a61bf62006-01-18 23:19:26 +01001049 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001050 return count;
1051}
1052
Jim Cromie07584c72007-10-12 21:08:00 +02001053static SENSOR_DEVICE_ATTR(fan1_div, S_IRUGO|S_IWUSR,
1054 show_fan_div, store_fan_div, 0);
1055static SENSOR_DEVICE_ATTR(fan2_div, S_IRUGO|S_IWUSR,
1056 show_fan_div, store_fan_div, 1);
1057static SENSOR_DEVICE_ATTR(fan3_div, S_IRUGO|S_IWUSR,
1058 show_fan_div, store_fan_div, 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001059
Linus Torvalds1da177e2005-04-16 15:20:36 -07001060static ssize_t
Jim Cromie07584c72007-10-12 21:08:00 +02001061show_pwm(struct device *dev, struct device_attribute *devattr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062{
Jim Cromie07584c72007-10-12 21:08:00 +02001063 int nr = to_sensor_dev_attr(devattr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064 struct w83627hf_data *data = w83627hf_update_device(dev);
Jim Cromie07584c72007-10-12 21:08:00 +02001065 return sprintf(buf, "%ld\n", (long) data->pwm[nr]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066}
1067
1068static ssize_t
Jim Cromie07584c72007-10-12 21:08:00 +02001069store_pwm(struct device *dev, struct device_attribute *devattr,
1070 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001071{
Jim Cromie07584c72007-10-12 21:08:00 +02001072 int nr = to_sensor_dev_attr(devattr)->index;
Jean Delvare787c72b2007-05-08 17:22:00 +02001073 struct w83627hf_data *data = dev_get_drvdata(dev);
Guenter Roeck27b9de32012-01-15 11:07:26 -08001074 unsigned long val;
1075 int err;
1076
1077 err = kstrtoul(buf, 10, &val);
1078 if (err)
1079 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001080
Ingo Molnar9a61bf62006-01-18 23:19:26 +01001081 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001082
1083 if (data->type == w83627thf) {
1084 /* bits 0-3 are reserved in 627THF */
Jim Cromie07584c72007-10-12 21:08:00 +02001085 data->pwm[nr] = PWM_TO_REG(val) & 0xf0;
Jean Delvare787c72b2007-05-08 17:22:00 +02001086 w83627hf_write_value(data,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001087 W836X7HF_REG_PWM(data->type, nr),
Jim Cromie07584c72007-10-12 21:08:00 +02001088 data->pwm[nr] |
Jean Delvare787c72b2007-05-08 17:22:00 +02001089 (w83627hf_read_value(data,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001090 W836X7HF_REG_PWM(data->type, nr)) & 0x0f));
1091 } else {
Jim Cromie07584c72007-10-12 21:08:00 +02001092 data->pwm[nr] = PWM_TO_REG(val);
Jean Delvare787c72b2007-05-08 17:22:00 +02001093 w83627hf_write_value(data,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001094 W836X7HF_REG_PWM(data->type, nr),
Jim Cromie07584c72007-10-12 21:08:00 +02001095 data->pwm[nr]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001096 }
1097
Ingo Molnar9a61bf62006-01-18 23:19:26 +01001098 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001099 return count;
1100}
1101
Jim Cromie07584c72007-10-12 21:08:00 +02001102static SENSOR_DEVICE_ATTR(pwm1, S_IRUGO|S_IWUSR, show_pwm, store_pwm, 0);
1103static SENSOR_DEVICE_ATTR(pwm2, S_IRUGO|S_IWUSR, show_pwm, store_pwm, 1);
1104static SENSOR_DEVICE_ATTR(pwm3, S_IRUGO|S_IWUSR, show_pwm, store_pwm, 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001105
Linus Torvalds1da177e2005-04-16 15:20:36 -07001106static ssize_t
Dominik Geyera95a5ed2008-08-06 22:41:04 +02001107show_pwm_enable(struct device *dev, struct device_attribute *devattr, char *buf)
1108{
1109 int nr = to_sensor_dev_attr(devattr)->index;
1110 struct w83627hf_data *data = w83627hf_update_device(dev);
1111 return sprintf(buf, "%d\n", data->pwm_enable[nr]);
1112}
1113
1114static ssize_t
1115store_pwm_enable(struct device *dev, struct device_attribute *devattr,
1116 const char *buf, size_t count)
1117{
1118 int nr = to_sensor_dev_attr(devattr)->index;
1119 struct w83627hf_data *data = dev_get_drvdata(dev);
Dominik Geyera95a5ed2008-08-06 22:41:04 +02001120 u8 reg;
Guenter Roeck27b9de32012-01-15 11:07:26 -08001121 unsigned long val;
1122 int err;
Dominik Geyera95a5ed2008-08-06 22:41:04 +02001123
Guenter Roeck27b9de32012-01-15 11:07:26 -08001124 err = kstrtoul(buf, 10, &val);
1125 if (err)
1126 return err;
1127
1128 if (!val || val > 3) /* modes 1, 2 and 3 are supported */
Dominik Geyera95a5ed2008-08-06 22:41:04 +02001129 return -EINVAL;
1130 mutex_lock(&data->update_lock);
1131 data->pwm_enable[nr] = val;
1132 reg = w83627hf_read_value(data, W83627THF_REG_PWM_ENABLE[nr]);
1133 reg &= ~(0x03 << W83627THF_PWM_ENABLE_SHIFT[nr]);
1134 reg |= (val - 1) << W83627THF_PWM_ENABLE_SHIFT[nr];
1135 w83627hf_write_value(data, W83627THF_REG_PWM_ENABLE[nr], reg);
1136 mutex_unlock(&data->update_lock);
1137 return count;
1138}
1139
1140static SENSOR_DEVICE_ATTR(pwm1_enable, S_IRUGO|S_IWUSR, show_pwm_enable,
1141 store_pwm_enable, 0);
1142static SENSOR_DEVICE_ATTR(pwm2_enable, S_IRUGO|S_IWUSR, show_pwm_enable,
1143 store_pwm_enable, 1);
1144static SENSOR_DEVICE_ATTR(pwm3_enable, S_IRUGO|S_IWUSR, show_pwm_enable,
1145 store_pwm_enable, 2);
1146
1147static ssize_t
Jim Cromie07584c72007-10-12 21:08:00 +02001148show_pwm_freq(struct device *dev, struct device_attribute *devattr, char *buf)
Carlos Olalla Martinez1550cb62007-06-09 10:11:16 -04001149{
Jim Cromie07584c72007-10-12 21:08:00 +02001150 int nr = to_sensor_dev_attr(devattr)->index;
Carlos Olalla Martinez1550cb62007-06-09 10:11:16 -04001151 struct w83627hf_data *data = w83627hf_update_device(dev);
1152 if (data->type == w83627hf)
1153 return sprintf(buf, "%ld\n",
Jim Cromie07584c72007-10-12 21:08:00 +02001154 pwm_freq_from_reg_627hf(data->pwm_freq[nr]));
Carlos Olalla Martinez1550cb62007-06-09 10:11:16 -04001155 else
1156 return sprintf(buf, "%ld\n",
Jim Cromie07584c72007-10-12 21:08:00 +02001157 pwm_freq_from_reg(data->pwm_freq[nr]));
Carlos Olalla Martinez1550cb62007-06-09 10:11:16 -04001158}
1159
1160static ssize_t
Jim Cromie07584c72007-10-12 21:08:00 +02001161store_pwm_freq(struct device *dev, struct device_attribute *devattr,
1162 const char *buf, size_t count)
Carlos Olalla Martinez1550cb62007-06-09 10:11:16 -04001163{
Jim Cromie07584c72007-10-12 21:08:00 +02001164 int nr = to_sensor_dev_attr(devattr)->index;
Carlos Olalla Martinez1550cb62007-06-09 10:11:16 -04001165 struct w83627hf_data *data = dev_get_drvdata(dev);
1166 static const u8 mask[]={0xF8, 0x8F};
Guenter Roeck27b9de32012-01-15 11:07:26 -08001167 unsigned long val;
1168 int err;
Carlos Olalla Martinez1550cb62007-06-09 10:11:16 -04001169
Guenter Roeck27b9de32012-01-15 11:07:26 -08001170 err = kstrtoul(buf, 10, &val);
1171 if (err)
1172 return err;
Carlos Olalla Martinez1550cb62007-06-09 10:11:16 -04001173
1174 mutex_lock(&data->update_lock);
1175
1176 if (data->type == w83627hf) {
Jim Cromie07584c72007-10-12 21:08:00 +02001177 data->pwm_freq[nr] = pwm_freq_to_reg_627hf(val);
Carlos Olalla Martinez1550cb62007-06-09 10:11:16 -04001178 w83627hf_write_value(data, W83627HF_REG_PWM_FREQ,
Jim Cromie07584c72007-10-12 21:08:00 +02001179 (data->pwm_freq[nr] << (nr*4)) |
Carlos Olalla Martinez1550cb62007-06-09 10:11:16 -04001180 (w83627hf_read_value(data,
Jim Cromie07584c72007-10-12 21:08:00 +02001181 W83627HF_REG_PWM_FREQ) & mask[nr]));
Carlos Olalla Martinez1550cb62007-06-09 10:11:16 -04001182 } else {
Jim Cromie07584c72007-10-12 21:08:00 +02001183 data->pwm_freq[nr] = pwm_freq_to_reg(val);
1184 w83627hf_write_value(data, W83637HF_REG_PWM_FREQ[nr],
1185 data->pwm_freq[nr]);
Carlos Olalla Martinez1550cb62007-06-09 10:11:16 -04001186 }
1187
1188 mutex_unlock(&data->update_lock);
1189 return count;
1190}
1191
Jim Cromie07584c72007-10-12 21:08:00 +02001192static SENSOR_DEVICE_ATTR(pwm1_freq, S_IRUGO|S_IWUSR,
1193 show_pwm_freq, store_pwm_freq, 0);
1194static SENSOR_DEVICE_ATTR(pwm2_freq, S_IRUGO|S_IWUSR,
1195 show_pwm_freq, store_pwm_freq, 1);
1196static SENSOR_DEVICE_ATTR(pwm3_freq, S_IRUGO|S_IWUSR,
1197 show_pwm_freq, store_pwm_freq, 2);
Carlos Olalla Martinez1550cb62007-06-09 10:11:16 -04001198
1199static ssize_t
Jim Cromie07584c72007-10-12 21:08:00 +02001200show_temp_type(struct device *dev, struct device_attribute *devattr,
1201 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001202{
Jim Cromie07584c72007-10-12 21:08:00 +02001203 int nr = to_sensor_dev_attr(devattr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001204 struct w83627hf_data *data = w83627hf_update_device(dev);
Jim Cromie07584c72007-10-12 21:08:00 +02001205 return sprintf(buf, "%ld\n", (long) data->sens[nr]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001206}
1207
1208static ssize_t
Jim Cromie07584c72007-10-12 21:08:00 +02001209store_temp_type(struct device *dev, struct device_attribute *devattr,
1210 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001211{
Jim Cromie07584c72007-10-12 21:08:00 +02001212 int nr = to_sensor_dev_attr(devattr)->index;
Jean Delvare787c72b2007-05-08 17:22:00 +02001213 struct w83627hf_data *data = dev_get_drvdata(dev);
Guenter Roeck27b9de32012-01-15 11:07:26 -08001214 unsigned long val;
1215 u32 tmp;
1216 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001217
Guenter Roeck27b9de32012-01-15 11:07:26 -08001218 err = kstrtoul(buf, 10, &val);
1219 if (err)
1220 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001221
Ingo Molnar9a61bf62006-01-18 23:19:26 +01001222 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001223
1224 switch (val) {
1225 case 1: /* PII/Celeron diode */
Jean Delvare787c72b2007-05-08 17:22:00 +02001226 tmp = w83627hf_read_value(data, W83781D_REG_SCFG1);
1227 w83627hf_write_value(data, W83781D_REG_SCFG1,
Jim Cromie07584c72007-10-12 21:08:00 +02001228 tmp | BIT_SCFG1[nr]);
Jean Delvare787c72b2007-05-08 17:22:00 +02001229 tmp = w83627hf_read_value(data, W83781D_REG_SCFG2);
1230 w83627hf_write_value(data, W83781D_REG_SCFG2,
Jim Cromie07584c72007-10-12 21:08:00 +02001231 tmp | BIT_SCFG2[nr]);
1232 data->sens[nr] = val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001233 break;
1234 case 2: /* 3904 */
Jean Delvare787c72b2007-05-08 17:22:00 +02001235 tmp = w83627hf_read_value(data, W83781D_REG_SCFG1);
1236 w83627hf_write_value(data, W83781D_REG_SCFG1,
Jim Cromie07584c72007-10-12 21:08:00 +02001237 tmp | BIT_SCFG1[nr]);
Jean Delvare787c72b2007-05-08 17:22:00 +02001238 tmp = w83627hf_read_value(data, W83781D_REG_SCFG2);
1239 w83627hf_write_value(data, W83781D_REG_SCFG2,
Jim Cromie07584c72007-10-12 21:08:00 +02001240 tmp & ~BIT_SCFG2[nr]);
1241 data->sens[nr] = val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001242 break;
Jean Delvareb26f9332007-08-16 14:30:01 +02001243 case W83781D_DEFAULT_BETA:
1244 dev_warn(dev, "Sensor type %d is deprecated, please use 4 "
1245 "instead\n", W83781D_DEFAULT_BETA);
1246 /* fall through */
1247 case 4: /* thermistor */
Jean Delvare787c72b2007-05-08 17:22:00 +02001248 tmp = w83627hf_read_value(data, W83781D_REG_SCFG1);
1249 w83627hf_write_value(data, W83781D_REG_SCFG1,
Jim Cromie07584c72007-10-12 21:08:00 +02001250 tmp & ~BIT_SCFG1[nr]);
1251 data->sens[nr] = val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001252 break;
1253 default:
Jean Delvare787c72b2007-05-08 17:22:00 +02001254 dev_err(dev,
Jean Delvareb26f9332007-08-16 14:30:01 +02001255 "Invalid sensor type %ld; must be 1, 2, or 4\n",
1256 (long) val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001257 break;
1258 }
1259
Ingo Molnar9a61bf62006-01-18 23:19:26 +01001260 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001261 return count;
1262}
1263
Jim Cromie07584c72007-10-12 21:08:00 +02001264#define sysfs_temp_type(offset) \
1265static SENSOR_DEVICE_ATTR(temp##offset##_type, S_IRUGO | S_IWUSR, \
1266 show_temp_type, store_temp_type, offset - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001267
Jim Cromie07584c72007-10-12 21:08:00 +02001268sysfs_temp_type(1);
1269sysfs_temp_type(2);
1270sysfs_temp_type(3);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001271
Jim Cromie07584c72007-10-12 21:08:00 +02001272static ssize_t
1273show_name(struct device *dev, struct device_attribute *devattr, char *buf)
Jean Delvare787c72b2007-05-08 17:22:00 +02001274{
1275 struct w83627hf_data *data = dev_get_drvdata(dev);
1276
1277 return sprintf(buf, "%s\n", data->name);
1278}
1279static DEVICE_ATTR(name, S_IRUGO, show_name, NULL);
1280
1281static int __init w83627hf_find(int sioaddr, unsigned short *addr,
1282 struct w83627hf_sio_data *sio_data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001283{
Guenter Roeck99184172019-04-05 08:44:41 -07001284 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001285 u16 val;
1286
Andi Kleen64f50302012-08-18 10:30:05 -07001287 static __initconst char *const names[] = {
Jean Delvare787c72b2007-05-08 17:22:00 +02001288 "W83627HF",
1289 "W83627THF",
1290 "W83697HF",
1291 "W83637HF",
1292 "W83687THF",
1293 };
1294
Christian Schultec46c0e92009-12-16 21:38:29 +01001295 sio_data->sioaddr = sioaddr;
Guenter Roeck99184172019-04-05 08:44:41 -07001296 err = superio_enter(sio_data);
1297 if (err)
1298 return err;
1299
1300 err = -ENODEV;
Jean Delvareb72656d2009-12-09 20:35:49 +01001301 val = force_id ? force_id : superio_inb(sio_data, DEVID);
Jean Delvare787c72b2007-05-08 17:22:00 +02001302 switch (val) {
1303 case W627_DEVID:
1304 sio_data->type = w83627hf;
1305 break;
1306 case W627THF_DEVID:
1307 sio_data->type = w83627thf;
1308 break;
1309 case W697_DEVID:
1310 sio_data->type = w83697hf;
1311 break;
1312 case W637_DEVID:
1313 sio_data->type = w83637hf;
1314 break;
1315 case W687THF_DEVID:
1316 sio_data->type = w83687thf;
1317 break;
Jean Delvaree142e2a2007-05-27 22:17:43 +02001318 case 0xff: /* No device at all */
1319 goto exit;
Jean Delvare787c72b2007-05-08 17:22:00 +02001320 default:
Jean Delvaree142e2a2007-05-27 22:17:43 +02001321 pr_debug(DRVNAME ": Unsupported chip (DEVID=0x%02x)\n", val);
Jean Delvared27c37c2007-05-08 17:21:59 +02001322 goto exit;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001323 }
1324
Jean Delvareb72656d2009-12-09 20:35:49 +01001325 superio_select(sio_data, W83627HF_LD_HWM);
1326 val = (superio_inb(sio_data, WINB_BASE_REG) << 8) |
1327 superio_inb(sio_data, WINB_BASE_REG + 1);
Petr Vandrovecada0c2f2005-10-07 23:11:03 +02001328 *addr = val & WINB_ALIGNMENT;
Jean Delvared27c37c2007-05-08 17:21:59 +02001329 if (*addr == 0) {
Joe Perches18de0302010-10-20 06:51:55 +00001330 pr_warn("Base address not set, skipping\n");
Jean Delvared27c37c2007-05-08 17:21:59 +02001331 goto exit;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001332 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001333
Jean Delvareb72656d2009-12-09 20:35:49 +01001334 val = superio_inb(sio_data, WINB_ACT_REG);
Jean Delvared27c37c2007-05-08 17:21:59 +02001335 if (!(val & 0x01)) {
Joe Perches18de0302010-10-20 06:51:55 +00001336 pr_warn("Enabling HWM logical device\n");
Jean Delvareb72656d2009-12-09 20:35:49 +01001337 superio_outb(sio_data, WINB_ACT_REG, val | 0x01);
Jean Delvared27c37c2007-05-08 17:21:59 +02001338 }
1339
1340 err = 0;
Jean Delvare787c72b2007-05-08 17:22:00 +02001341 pr_info(DRVNAME ": Found %s chip at %#x\n",
1342 names[sio_data->type], *addr);
Jean Delvared27c37c2007-05-08 17:21:59 +02001343
1344 exit:
Jean Delvareb72656d2009-12-09 20:35:49 +01001345 superio_exit(sio_data);
Jean Delvared27c37c2007-05-08 17:21:59 +02001346 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001347}
1348
Jim Cromie07584c72007-10-12 21:08:00 +02001349#define VIN_UNIT_ATTRS(_X_) \
1350 &sensor_dev_attr_in##_X_##_input.dev_attr.attr, \
1351 &sensor_dev_attr_in##_X_##_min.dev_attr.attr, \
Jean Delvaree3604c62008-01-03 23:00:30 +01001352 &sensor_dev_attr_in##_X_##_max.dev_attr.attr, \
1353 &sensor_dev_attr_in##_X_##_alarm.dev_attr.attr, \
1354 &sensor_dev_attr_in##_X_##_beep.dev_attr.attr
Jim Cromie07584c72007-10-12 21:08:00 +02001355
1356#define FAN_UNIT_ATTRS(_X_) \
1357 &sensor_dev_attr_fan##_X_##_input.dev_attr.attr, \
1358 &sensor_dev_attr_fan##_X_##_min.dev_attr.attr, \
Jean Delvaree3604c62008-01-03 23:00:30 +01001359 &sensor_dev_attr_fan##_X_##_div.dev_attr.attr, \
1360 &sensor_dev_attr_fan##_X_##_alarm.dev_attr.attr, \
1361 &sensor_dev_attr_fan##_X_##_beep.dev_attr.attr
Jim Cromie07584c72007-10-12 21:08:00 +02001362
1363#define TEMP_UNIT_ATTRS(_X_) \
1364 &sensor_dev_attr_temp##_X_##_input.dev_attr.attr, \
1365 &sensor_dev_attr_temp##_X_##_max.dev_attr.attr, \
1366 &sensor_dev_attr_temp##_X_##_max_hyst.dev_attr.attr, \
Jean Delvaree3604c62008-01-03 23:00:30 +01001367 &sensor_dev_attr_temp##_X_##_type.dev_attr.attr, \
1368 &sensor_dev_attr_temp##_X_##_alarm.dev_attr.attr, \
1369 &sensor_dev_attr_temp##_X_##_beep.dev_attr.attr
Jim Cromie07584c72007-10-12 21:08:00 +02001370
Mark M. Hoffmanc1685f62006-09-24 20:59:49 +02001371static struct attribute *w83627hf_attributes[] = {
1372 &dev_attr_in0_input.attr,
1373 &dev_attr_in0_min.attr,
1374 &dev_attr_in0_max.attr,
Jean Delvaree3604c62008-01-03 23:00:30 +01001375 &sensor_dev_attr_in0_alarm.dev_attr.attr,
1376 &sensor_dev_attr_in0_beep.dev_attr.attr,
Jim Cromie07584c72007-10-12 21:08:00 +02001377 VIN_UNIT_ATTRS(2),
1378 VIN_UNIT_ATTRS(3),
1379 VIN_UNIT_ATTRS(4),
1380 VIN_UNIT_ATTRS(7),
1381 VIN_UNIT_ATTRS(8),
Mark M. Hoffmanc1685f62006-09-24 20:59:49 +02001382
Jim Cromie07584c72007-10-12 21:08:00 +02001383 FAN_UNIT_ATTRS(1),
1384 FAN_UNIT_ATTRS(2),
Mark M. Hoffmanc1685f62006-09-24 20:59:49 +02001385
Jim Cromie07584c72007-10-12 21:08:00 +02001386 TEMP_UNIT_ATTRS(1),
1387 TEMP_UNIT_ATTRS(2),
Mark M. Hoffmanc1685f62006-09-24 20:59:49 +02001388
1389 &dev_attr_alarms.attr,
Jean Delvare1c138102008-01-03 23:04:55 +01001390 &sensor_dev_attr_beep_enable.dev_attr.attr,
Mark M. Hoffmanc1685f62006-09-24 20:59:49 +02001391 &dev_attr_beep_mask.attr,
1392
Jim Cromie07584c72007-10-12 21:08:00 +02001393 &sensor_dev_attr_pwm1.dev_attr.attr,
1394 &sensor_dev_attr_pwm2.dev_attr.attr,
Jean Delvare787c72b2007-05-08 17:22:00 +02001395 &dev_attr_name.attr,
Mark M. Hoffmanc1685f62006-09-24 20:59:49 +02001396 NULL
1397};
1398
1399static const struct attribute_group w83627hf_group = {
1400 .attrs = w83627hf_attributes,
1401};
1402
1403static struct attribute *w83627hf_attributes_opt[] = {
Jim Cromie07584c72007-10-12 21:08:00 +02001404 VIN_UNIT_ATTRS(1),
1405 VIN_UNIT_ATTRS(5),
1406 VIN_UNIT_ATTRS(6),
Mark M. Hoffmanc1685f62006-09-24 20:59:49 +02001407
Jim Cromie07584c72007-10-12 21:08:00 +02001408 FAN_UNIT_ATTRS(3),
1409 TEMP_UNIT_ATTRS(3),
1410 &sensor_dev_attr_pwm3.dev_attr.attr,
Mark M. Hoffmanc1685f62006-09-24 20:59:49 +02001411
Jim Cromie07584c72007-10-12 21:08:00 +02001412 &sensor_dev_attr_pwm1_freq.dev_attr.attr,
1413 &sensor_dev_attr_pwm2_freq.dev_attr.attr,
1414 &sensor_dev_attr_pwm3_freq.dev_attr.attr,
Dominik Geyera95a5ed2008-08-06 22:41:04 +02001415
1416 &sensor_dev_attr_pwm1_enable.dev_attr.attr,
1417 &sensor_dev_attr_pwm2_enable.dev_attr.attr,
1418 &sensor_dev_attr_pwm3_enable.dev_attr.attr,
1419
Mark M. Hoffmanc1685f62006-09-24 20:59:49 +02001420 NULL
1421};
1422
1423static const struct attribute_group w83627hf_group_opt = {
1424 .attrs = w83627hf_attributes_opt,
1425};
1426
Bill Pemberton6c931ae2012-11-19 13:22:35 -05001427static int w83627hf_probe(struct platform_device *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001428{
Jean Delvare787c72b2007-05-08 17:22:00 +02001429 struct device *dev = &pdev->dev;
Jingoo Hana8b3a3a2013-07-30 17:13:06 +09001430 struct w83627hf_sio_data *sio_data = dev_get_platdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001431 struct w83627hf_data *data;
Jean Delvare787c72b2007-05-08 17:22:00 +02001432 struct resource *res;
Jim Cromie2ca2fcd2007-10-14 17:20:50 -06001433 int err, i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001434
Jean Delvare787c72b2007-05-08 17:22:00 +02001435 static const char *names[] = {
1436 "w83627hf",
1437 "w83627thf",
1438 "w83697hf",
1439 "w83637hf",
1440 "w83687thf",
1441 };
1442
1443 res = platform_get_resource(pdev, IORESOURCE_IO, 0);
Guenter Roeck0cf46992012-06-02 11:47:59 -07001444 if (!devm_request_region(dev, res->start, WINB_REGION_SIZE, DRVNAME)) {
Jean Delvare787c72b2007-05-08 17:22:00 +02001445 dev_err(dev, "Failed to request region 0x%lx-0x%lx\n",
1446 (unsigned long)res->start,
1447 (unsigned long)(res->start + WINB_REGION_SIZE - 1));
Guenter Roeck0cf46992012-06-02 11:47:59 -07001448 return -EBUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001449 }
1450
Guenter Roeck0cf46992012-06-02 11:47:59 -07001451 data = devm_kzalloc(dev, sizeof(struct w83627hf_data), GFP_KERNEL);
1452 if (!data)
1453 return -ENOMEM;
1454
Jean Delvare787c72b2007-05-08 17:22:00 +02001455 data->addr = res->start;
1456 data->type = sio_data->type;
1457 data->name = names[sio_data->type];
Ingo Molnar9a61bf62006-01-18 23:19:26 +01001458 mutex_init(&data->lock);
Ingo Molnar9a61bf62006-01-18 23:19:26 +01001459 mutex_init(&data->update_lock);
Jean Delvare787c72b2007-05-08 17:22:00 +02001460 platform_set_drvdata(pdev, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001461
Linus Torvalds1da177e2005-04-16 15:20:36 -07001462 /* Initialize the chip */
Jean Delvare787c72b2007-05-08 17:22:00 +02001463 w83627hf_init_device(pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001464
1465 /* A few vars need to be filled upon startup */
Jim Cromie2ca2fcd2007-10-14 17:20:50 -06001466 for (i = 0; i <= 2; i++)
1467 data->fan_min[i] = w83627hf_read_value(
1468 data, W83627HF_REG_FAN_MIN(i));
Jean Delvarec09c5182007-10-12 21:53:07 +02001469 w83627hf_update_fan_div(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001470
Mark M. Hoffmanc1685f62006-09-24 20:59:49 +02001471 /* Register common device attributes */
Guenter Roeck27b9de32012-01-15 11:07:26 -08001472 err = sysfs_create_group(&dev->kobj, &w83627hf_group);
1473 if (err)
Guenter Roeck0cf46992012-06-02 11:47:59 -07001474 return err;
Mark M. Hoffmanc1685f62006-09-24 20:59:49 +02001475
1476 /* Register chip-specific device attributes */
Jean Delvare787c72b2007-05-08 17:22:00 +02001477 if (data->type == w83627hf || data->type == w83697hf)
Jim Cromie07584c72007-10-12 21:08:00 +02001478 if ((err = device_create_file(dev,
1479 &sensor_dev_attr_in5_input.dev_attr))
1480 || (err = device_create_file(dev,
1481 &sensor_dev_attr_in5_min.dev_attr))
1482 || (err = device_create_file(dev,
1483 &sensor_dev_attr_in5_max.dev_attr))
1484 || (err = device_create_file(dev,
Jean Delvaree3604c62008-01-03 23:00:30 +01001485 &sensor_dev_attr_in5_alarm.dev_attr))
1486 || (err = device_create_file(dev,
1487 &sensor_dev_attr_in5_beep.dev_attr))
1488 || (err = device_create_file(dev,
Jim Cromie07584c72007-10-12 21:08:00 +02001489 &sensor_dev_attr_in6_input.dev_attr))
1490 || (err = device_create_file(dev,
1491 &sensor_dev_attr_in6_min.dev_attr))
1492 || (err = device_create_file(dev,
1493 &sensor_dev_attr_in6_max.dev_attr))
1494 || (err = device_create_file(dev,
Jean Delvaree3604c62008-01-03 23:00:30 +01001495 &sensor_dev_attr_in6_alarm.dev_attr))
1496 || (err = device_create_file(dev,
1497 &sensor_dev_attr_in6_beep.dev_attr))
1498 || (err = device_create_file(dev,
Jim Cromie07584c72007-10-12 21:08:00 +02001499 &sensor_dev_attr_pwm1_freq.dev_attr))
1500 || (err = device_create_file(dev,
1501 &sensor_dev_attr_pwm2_freq.dev_attr)))
Guenter Roeck0cf46992012-06-02 11:47:59 -07001502 goto error;
Mark M. Hoffmanc1685f62006-09-24 20:59:49 +02001503
Jean Delvare787c72b2007-05-08 17:22:00 +02001504 if (data->type != w83697hf)
Jim Cromie07584c72007-10-12 21:08:00 +02001505 if ((err = device_create_file(dev,
1506 &sensor_dev_attr_in1_input.dev_attr))
1507 || (err = device_create_file(dev,
1508 &sensor_dev_attr_in1_min.dev_attr))
1509 || (err = device_create_file(dev,
1510 &sensor_dev_attr_in1_max.dev_attr))
1511 || (err = device_create_file(dev,
Jean Delvaree3604c62008-01-03 23:00:30 +01001512 &sensor_dev_attr_in1_alarm.dev_attr))
1513 || (err = device_create_file(dev,
1514 &sensor_dev_attr_in1_beep.dev_attr))
1515 || (err = device_create_file(dev,
Jim Cromie07584c72007-10-12 21:08:00 +02001516 &sensor_dev_attr_fan3_input.dev_attr))
1517 || (err = device_create_file(dev,
1518 &sensor_dev_attr_fan3_min.dev_attr))
1519 || (err = device_create_file(dev,
1520 &sensor_dev_attr_fan3_div.dev_attr))
1521 || (err = device_create_file(dev,
Jean Delvaree3604c62008-01-03 23:00:30 +01001522 &sensor_dev_attr_fan3_alarm.dev_attr))
1523 || (err = device_create_file(dev,
1524 &sensor_dev_attr_fan3_beep.dev_attr))
1525 || (err = device_create_file(dev,
Jim Cromie07584c72007-10-12 21:08:00 +02001526 &sensor_dev_attr_temp3_input.dev_attr))
1527 || (err = device_create_file(dev,
1528 &sensor_dev_attr_temp3_max.dev_attr))
1529 || (err = device_create_file(dev,
1530 &sensor_dev_attr_temp3_max_hyst.dev_attr))
1531 || (err = device_create_file(dev,
Jean Delvaree3604c62008-01-03 23:00:30 +01001532 &sensor_dev_attr_temp3_alarm.dev_attr))
1533 || (err = device_create_file(dev,
1534 &sensor_dev_attr_temp3_beep.dev_attr))
1535 || (err = device_create_file(dev,
Jim Cromie07584c72007-10-12 21:08:00 +02001536 &sensor_dev_attr_temp3_type.dev_attr)))
Guenter Roeck0cf46992012-06-02 11:47:59 -07001537 goto error;
Mark M. Hoffmanc1685f62006-09-24 20:59:49 +02001538
Jean Delvare787c72b2007-05-08 17:22:00 +02001539 if (data->type != w83697hf && data->vid != 0xff) {
Jean Delvare8a665a02007-05-08 17:21:59 +02001540 /* Convert VID to voltage based on VRM */
1541 data->vrm = vid_which_vrm();
1542
Jean Delvare787c72b2007-05-08 17:22:00 +02001543 if ((err = device_create_file(dev, &dev_attr_cpu0_vid))
1544 || (err = device_create_file(dev, &dev_attr_vrm)))
Guenter Roeck0cf46992012-06-02 11:47:59 -07001545 goto error;
Jean Delvare8a665a02007-05-08 17:21:59 +02001546 }
Mark M. Hoffmanc1685f62006-09-24 20:59:49 +02001547
Jean Delvare787c72b2007-05-08 17:22:00 +02001548 if (data->type == w83627thf || data->type == w83637hf
Guenter Roeck27b9de32012-01-15 11:07:26 -08001549 || data->type == w83687thf) {
1550 err = device_create_file(dev, &sensor_dev_attr_pwm3.dev_attr);
1551 if (err)
Guenter Roeck0cf46992012-06-02 11:47:59 -07001552 goto error;
Guenter Roeck27b9de32012-01-15 11:07:26 -08001553 }
Mark M. Hoffmanc1685f62006-09-24 20:59:49 +02001554
Carlos Olalla Martinez1550cb62007-06-09 10:11:16 -04001555 if (data->type == w83637hf || data->type == w83687thf)
Jim Cromie07584c72007-10-12 21:08:00 +02001556 if ((err = device_create_file(dev,
1557 &sensor_dev_attr_pwm1_freq.dev_attr))
1558 || (err = device_create_file(dev,
1559 &sensor_dev_attr_pwm2_freq.dev_attr))
1560 || (err = device_create_file(dev,
1561 &sensor_dev_attr_pwm3_freq.dev_attr)))
Guenter Roeck0cf46992012-06-02 11:47:59 -07001562 goto error;
Carlos Olalla Martinez1550cb62007-06-09 10:11:16 -04001563
Dominik Geyera95a5ed2008-08-06 22:41:04 +02001564 if (data->type != w83627hf)
1565 if ((err = device_create_file(dev,
1566 &sensor_dev_attr_pwm1_enable.dev_attr))
1567 || (err = device_create_file(dev,
1568 &sensor_dev_attr_pwm2_enable.dev_attr)))
Guenter Roeck0cf46992012-06-02 11:47:59 -07001569 goto error;
Dominik Geyera95a5ed2008-08-06 22:41:04 +02001570
1571 if (data->type == w83627thf || data->type == w83637hf
Guenter Roeck27b9de32012-01-15 11:07:26 -08001572 || data->type == w83687thf) {
1573 err = device_create_file(dev,
1574 &sensor_dev_attr_pwm3_enable.dev_attr);
1575 if (err)
Guenter Roeck0cf46992012-06-02 11:47:59 -07001576 goto error;
Guenter Roeck27b9de32012-01-15 11:07:26 -08001577 }
Dominik Geyera95a5ed2008-08-06 22:41:04 +02001578
Tony Jones1beeffe2007-08-20 13:46:20 -07001579 data->hwmon_dev = hwmon_device_register(dev);
1580 if (IS_ERR(data->hwmon_dev)) {
1581 err = PTR_ERR(data->hwmon_dev);
Guenter Roeck0cf46992012-06-02 11:47:59 -07001582 goto error;
Mark M. Hoffman943b0832005-07-15 21:39:18 -04001583 }
1584
Linus Torvalds1da177e2005-04-16 15:20:36 -07001585 return 0;
1586
Guenter Roeck0cf46992012-06-02 11:47:59 -07001587 error:
Jean Delvare787c72b2007-05-08 17:22:00 +02001588 sysfs_remove_group(&dev->kobj, &w83627hf_group);
1589 sysfs_remove_group(&dev->kobj, &w83627hf_group_opt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001590 return err;
1591}
1592
Bill Pemberton281dfd02012-11-19 13:25:51 -05001593static int w83627hf_remove(struct platform_device *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001594{
Jean Delvare787c72b2007-05-08 17:22:00 +02001595 struct w83627hf_data *data = platform_get_drvdata(pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001596
Tony Jones1beeffe2007-08-20 13:46:20 -07001597 hwmon_device_unregister(data->hwmon_dev);
Mark M. Hoffman943b0832005-07-15 21:39:18 -04001598
Jean Delvare787c72b2007-05-08 17:22:00 +02001599 sysfs_remove_group(&pdev->dev.kobj, &w83627hf_group);
1600 sysfs_remove_group(&pdev->dev.kobj, &w83627hf_group_opt);
Jean Delvare787c72b2007-05-08 17:22:00 +02001601
Linus Torvalds1da177e2005-04-16 15:20:36 -07001602 return 0;
1603}
1604
1605
Jean Delvared58df9c2007-10-10 16:30:23 +02001606/* Registers 0x50-0x5f are banked */
1607static inline void w83627hf_set_bank(struct w83627hf_data *data, u16 reg)
1608{
1609 if ((reg & 0x00f0) == 0x50) {
1610 outb_p(W83781D_REG_BANK, data->addr + W83781D_ADDR_REG_OFFSET);
1611 outb_p(reg >> 8, data->addr + W83781D_DATA_REG_OFFSET);
1612 }
1613}
1614
1615/* Not strictly necessary, but play it safe for now */
1616static inline void w83627hf_reset_bank(struct w83627hf_data *data, u16 reg)
1617{
1618 if (reg & 0xff00) {
1619 outb_p(W83781D_REG_BANK, data->addr + W83781D_ADDR_REG_OFFSET);
1620 outb_p(0, data->addr + W83781D_DATA_REG_OFFSET);
1621 }
1622}
1623
Jean Delvare787c72b2007-05-08 17:22:00 +02001624static int w83627hf_read_value(struct w83627hf_data *data, u16 reg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001625{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001626 int res, word_sized;
1627
Ingo Molnar9a61bf62006-01-18 23:19:26 +01001628 mutex_lock(&data->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001629 word_sized = (((reg & 0xff00) == 0x100)
1630 || ((reg & 0xff00) == 0x200))
1631 && (((reg & 0x00ff) == 0x50)
1632 || ((reg & 0x00ff) == 0x53)
1633 || ((reg & 0x00ff) == 0x55));
Jean Delvared58df9c2007-10-10 16:30:23 +02001634 w83627hf_set_bank(data, reg);
Jean Delvare787c72b2007-05-08 17:22:00 +02001635 outb_p(reg & 0xff, data->addr + W83781D_ADDR_REG_OFFSET);
1636 res = inb_p(data->addr + W83781D_DATA_REG_OFFSET);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001637 if (word_sized) {
1638 outb_p((reg & 0xff) + 1,
Jean Delvare787c72b2007-05-08 17:22:00 +02001639 data->addr + W83781D_ADDR_REG_OFFSET);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001640 res =
Jean Delvare787c72b2007-05-08 17:22:00 +02001641 (res << 8) + inb_p(data->addr +
Linus Torvalds1da177e2005-04-16 15:20:36 -07001642 W83781D_DATA_REG_OFFSET);
1643 }
Jean Delvared58df9c2007-10-10 16:30:23 +02001644 w83627hf_reset_bank(data, reg);
Ingo Molnar9a61bf62006-01-18 23:19:26 +01001645 mutex_unlock(&data->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001646 return res;
1647}
1648
Bill Pemberton6c931ae2012-11-19 13:22:35 -05001649static int w83627thf_read_gpio5(struct platform_device *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001650{
Jingoo Hana8b3a3a2013-07-30 17:13:06 +09001651 struct w83627hf_sio_data *sio_data = dev_get_platdata(&pdev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001652 int res = 0xff, sel;
1653
Guenter Roeck99184172019-04-05 08:44:41 -07001654 if (superio_enter(sio_data)) {
1655 /*
1656 * Some other driver reserved the address space for itself.
1657 * We don't want to fail driver instantiation because of that,
1658 * so display a warning and keep going.
1659 */
1660 dev_warn(&pdev->dev,
1661 "Can not read VID data: Failed to enable SuperIO access\n");
1662 return res;
1663 }
1664
Jean Delvareb72656d2009-12-09 20:35:49 +01001665 superio_select(sio_data, W83627HF_LD_GPIO5);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001666
Guenter Roeck99184172019-04-05 08:44:41 -07001667 res = 0xff;
1668
Linus Torvalds1da177e2005-04-16 15:20:36 -07001669 /* Make sure these GPIO pins are enabled */
Jean Delvareb72656d2009-12-09 20:35:49 +01001670 if (!(superio_inb(sio_data, W83627THF_GPIO5_EN) & (1<<3))) {
Jean Delvare787c72b2007-05-08 17:22:00 +02001671 dev_dbg(&pdev->dev, "GPIO5 disabled, no VID function\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001672 goto exit;
1673 }
1674
Guenter Roeck27b9de32012-01-15 11:07:26 -08001675 /*
1676 * Make sure the pins are configured for input
1677 * There must be at least five (VRM 9), and possibly 6 (VRM 10)
1678 */
Jean Delvareb72656d2009-12-09 20:35:49 +01001679 sel = superio_inb(sio_data, W83627THF_GPIO5_IOSR) & 0x3f;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001680 if ((sel & 0x1f) != 0x1f) {
Jean Delvare787c72b2007-05-08 17:22:00 +02001681 dev_dbg(&pdev->dev, "GPIO5 not configured for VID "
Linus Torvalds1da177e2005-04-16 15:20:36 -07001682 "function\n");
1683 goto exit;
1684 }
1685
Jean Delvare787c72b2007-05-08 17:22:00 +02001686 dev_info(&pdev->dev, "Reading VID from GPIO5\n");
Jean Delvareb72656d2009-12-09 20:35:49 +01001687 res = superio_inb(sio_data, W83627THF_GPIO5_DR) & sel;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001688
1689exit:
Jean Delvareb72656d2009-12-09 20:35:49 +01001690 superio_exit(sio_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001691 return res;
1692}
1693
Bill Pemberton6c931ae2012-11-19 13:22:35 -05001694static int w83687thf_read_vid(struct platform_device *pdev)
Jean Delvarec2db6ce2006-01-18 23:22:12 +01001695{
Jingoo Hana8b3a3a2013-07-30 17:13:06 +09001696 struct w83627hf_sio_data *sio_data = dev_get_platdata(&pdev->dev);
Jean Delvarec2db6ce2006-01-18 23:22:12 +01001697 int res = 0xff;
1698
Guenter Roeck99184172019-04-05 08:44:41 -07001699 if (superio_enter(sio_data)) {
1700 /*
1701 * Some other driver reserved the address space for itself.
1702 * We don't want to fail driver instantiation because of that,
1703 * so display a warning and keep going.
1704 */
1705 dev_warn(&pdev->dev,
1706 "Can not read VID data: Failed to enable SuperIO access\n");
1707 return res;
1708 }
1709
Jean Delvareb72656d2009-12-09 20:35:49 +01001710 superio_select(sio_data, W83627HF_LD_HWM);
Jean Delvarec2db6ce2006-01-18 23:22:12 +01001711
1712 /* Make sure these GPIO pins are enabled */
Jean Delvareb72656d2009-12-09 20:35:49 +01001713 if (!(superio_inb(sio_data, W83687THF_VID_EN) & (1 << 2))) {
Jean Delvare787c72b2007-05-08 17:22:00 +02001714 dev_dbg(&pdev->dev, "VID disabled, no VID function\n");
Jean Delvarec2db6ce2006-01-18 23:22:12 +01001715 goto exit;
1716 }
1717
1718 /* Make sure the pins are configured for input */
Jean Delvareb72656d2009-12-09 20:35:49 +01001719 if (!(superio_inb(sio_data, W83687THF_VID_CFG) & (1 << 4))) {
Jean Delvare787c72b2007-05-08 17:22:00 +02001720 dev_dbg(&pdev->dev, "VID configured as output, "
Jean Delvarec2db6ce2006-01-18 23:22:12 +01001721 "no VID function\n");
1722 goto exit;
1723 }
1724
Jean Delvareb72656d2009-12-09 20:35:49 +01001725 res = superio_inb(sio_data, W83687THF_VID_DATA) & 0x3f;
Jean Delvarec2db6ce2006-01-18 23:22:12 +01001726
1727exit:
Jean Delvareb72656d2009-12-09 20:35:49 +01001728 superio_exit(sio_data);
Jean Delvarec2db6ce2006-01-18 23:22:12 +01001729 return res;
1730}
1731
Jean Delvare787c72b2007-05-08 17:22:00 +02001732static int w83627hf_write_value(struct w83627hf_data *data, u16 reg, u16 value)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001733{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001734 int word_sized;
1735
Ingo Molnar9a61bf62006-01-18 23:19:26 +01001736 mutex_lock(&data->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001737 word_sized = (((reg & 0xff00) == 0x100)
1738 || ((reg & 0xff00) == 0x200))
1739 && (((reg & 0x00ff) == 0x53)
1740 || ((reg & 0x00ff) == 0x55));
Jean Delvared58df9c2007-10-10 16:30:23 +02001741 w83627hf_set_bank(data, reg);
Jean Delvare787c72b2007-05-08 17:22:00 +02001742 outb_p(reg & 0xff, data->addr + W83781D_ADDR_REG_OFFSET);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001743 if (word_sized) {
1744 outb_p(value >> 8,
Jean Delvare787c72b2007-05-08 17:22:00 +02001745 data->addr + W83781D_DATA_REG_OFFSET);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001746 outb_p((reg & 0xff) + 1,
Jean Delvare787c72b2007-05-08 17:22:00 +02001747 data->addr + W83781D_ADDR_REG_OFFSET);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001748 }
1749 outb_p(value & 0xff,
Jean Delvare787c72b2007-05-08 17:22:00 +02001750 data->addr + W83781D_DATA_REG_OFFSET);
Jean Delvared58df9c2007-10-10 16:30:23 +02001751 w83627hf_reset_bank(data, reg);
Ingo Molnar9a61bf62006-01-18 23:19:26 +01001752 mutex_unlock(&data->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001753 return 0;
1754}
1755
Bill Pemberton6c931ae2012-11-19 13:22:35 -05001756static void w83627hf_init_device(struct platform_device *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001757{
Jean Delvare787c72b2007-05-08 17:22:00 +02001758 struct w83627hf_data *data = platform_get_drvdata(pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001759 int i;
Jean Delvared27c37c2007-05-08 17:21:59 +02001760 enum chips type = data->type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001761 u8 tmp;
1762
Linus Torvalds1da177e2005-04-16 15:20:36 -07001763 /* Minimize conflicts with other winbond i2c-only clients... */
1764 /* disable i2c subclients... how to disable main i2c client?? */
1765 /* force i2c address to relatively uncommon address */
Jean Delvare8f3c7c52012-12-19 22:17:00 +01001766 if (type == w83627hf) {
1767 w83627hf_write_value(data, W83781D_REG_I2C_SUBADDR, 0x89);
1768 w83627hf_write_value(data, W83781D_REG_I2C_ADDR, force_i2c);
1769 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001770
1771 /* Read VID only once */
Jean Delvared27c37c2007-05-08 17:21:59 +02001772 if (type == w83627hf || type == w83637hf) {
Jean Delvare787c72b2007-05-08 17:22:00 +02001773 int lo = w83627hf_read_value(data, W83781D_REG_VID_FANDIV);
1774 int hi = w83627hf_read_value(data, W83781D_REG_CHIPID);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001775 data->vid = (lo & 0x0f) | ((hi & 0x01) << 4);
Jean Delvared27c37c2007-05-08 17:21:59 +02001776 } else if (type == w83627thf) {
Jean Delvare787c72b2007-05-08 17:22:00 +02001777 data->vid = w83627thf_read_gpio5(pdev);
Jean Delvared27c37c2007-05-08 17:21:59 +02001778 } else if (type == w83687thf) {
Jean Delvare787c72b2007-05-08 17:22:00 +02001779 data->vid = w83687thf_read_vid(pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001780 }
1781
1782 /* Read VRM & OVT Config only once */
Jean Delvared27c37c2007-05-08 17:21:59 +02001783 if (type == w83627thf || type == w83637hf || type == w83687thf) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001784 data->vrm_ovt =
Jean Delvare787c72b2007-05-08 17:22:00 +02001785 w83627hf_read_value(data, W83627THF_REG_VRM_OVT_CFG);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001786 }
1787
Jean Delvare787c72b2007-05-08 17:22:00 +02001788 tmp = w83627hf_read_value(data, W83781D_REG_SCFG1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001789 for (i = 1; i <= 3; i++) {
1790 if (!(tmp & BIT_SCFG1[i - 1])) {
Jean Delvareb26f9332007-08-16 14:30:01 +02001791 data->sens[i - 1] = 4;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001792 } else {
1793 if (w83627hf_read_value
Jean Delvare787c72b2007-05-08 17:22:00 +02001794 (data,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001795 W83781D_REG_SCFG2) & BIT_SCFG2[i - 1])
1796 data->sens[i - 1] = 1;
1797 else
1798 data->sens[i - 1] = 2;
1799 }
1800 if ((type == w83697hf) && (i == 2))
1801 break;
1802 }
1803
1804 if(init) {
1805 /* Enable temp2 */
Jim Cromiedf48ed82007-10-14 17:10:52 -06001806 tmp = w83627hf_read_value(data, W83627HF_REG_TEMP2_CONFIG);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001807 if (tmp & 0x01) {
Jean Delvare787c72b2007-05-08 17:22:00 +02001808 dev_warn(&pdev->dev, "Enabling temp2, readings "
Linus Torvalds1da177e2005-04-16 15:20:36 -07001809 "might not make sense\n");
Jim Cromiedf48ed82007-10-14 17:10:52 -06001810 w83627hf_write_value(data, W83627HF_REG_TEMP2_CONFIG,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001811 tmp & 0xfe);
1812 }
1813
1814 /* Enable temp3 */
1815 if (type != w83697hf) {
Jean Delvare787c72b2007-05-08 17:22:00 +02001816 tmp = w83627hf_read_value(data,
Jim Cromiedf48ed82007-10-14 17:10:52 -06001817 W83627HF_REG_TEMP3_CONFIG);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001818 if (tmp & 0x01) {
Jean Delvare787c72b2007-05-08 17:22:00 +02001819 dev_warn(&pdev->dev, "Enabling temp3, "
Linus Torvalds1da177e2005-04-16 15:20:36 -07001820 "readings might not make sense\n");
Jean Delvare787c72b2007-05-08 17:22:00 +02001821 w83627hf_write_value(data,
Jim Cromiedf48ed82007-10-14 17:10:52 -06001822 W83627HF_REG_TEMP3_CONFIG, tmp & 0xfe);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001823 }
1824 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001825 }
1826
1827 /* Start monitoring */
Jean Delvare787c72b2007-05-08 17:22:00 +02001828 w83627hf_write_value(data, W83781D_REG_CONFIG,
1829 (w83627hf_read_value(data,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001830 W83781D_REG_CONFIG) & 0xf7)
1831 | 0x01);
Jean Delvareef878b12008-01-03 22:54:13 +01001832
1833 /* Enable VBAT monitoring if needed */
1834 tmp = w83627hf_read_value(data, W83781D_REG_VBAT);
1835 if (!(tmp & 0x01))
1836 w83627hf_write_value(data, W83781D_REG_VBAT, tmp | 0x01);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001837}
1838
Jean Delvarec09c5182007-10-12 21:53:07 +02001839static void w83627hf_update_fan_div(struct w83627hf_data *data)
1840{
1841 int reg;
1842
1843 reg = w83627hf_read_value(data, W83781D_REG_VID_FANDIV);
1844 data->fan_div[0] = (reg >> 4) & 0x03;
1845 data->fan_div[1] = (reg >> 6) & 0x03;
1846 if (data->type != w83697hf) {
1847 data->fan_div[2] = (w83627hf_read_value(data,
1848 W83781D_REG_PIN) >> 6) & 0x03;
1849 }
1850 reg = w83627hf_read_value(data, W83781D_REG_VBAT);
1851 data->fan_div[0] |= (reg >> 3) & 0x04;
1852 data->fan_div[1] |= (reg >> 4) & 0x04;
1853 if (data->type != w83697hf)
1854 data->fan_div[2] |= (reg >> 5) & 0x04;
1855}
1856
Linus Torvalds1da177e2005-04-16 15:20:36 -07001857static struct w83627hf_data *w83627hf_update_device(struct device *dev)
1858{
Jean Delvare787c72b2007-05-08 17:22:00 +02001859 struct w83627hf_data *data = dev_get_drvdata(dev);
Jim Cromiedf48ed82007-10-14 17:10:52 -06001860 int i, num_temps = (data->type == w83697hf) ? 2 : 3;
Dominik Geyera95a5ed2008-08-06 22:41:04 +02001861 int num_pwms = (data->type == w83697hf) ? 2 : 3;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001862
Ingo Molnar9a61bf62006-01-18 23:19:26 +01001863 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001864
1865 if (time_after(jiffies, data->last_updated + HZ + HZ / 2)
1866 || !data->valid) {
1867 for (i = 0; i <= 8; i++) {
1868 /* skip missing sensors */
1869 if (((data->type == w83697hf) && (i == 1)) ||
Jean Delvarec2db6ce2006-01-18 23:22:12 +01001870 ((data->type != w83627hf && data->type != w83697hf)
Yuan Mu4a1c44472005-11-07 22:19:04 +01001871 && (i == 5 || i == 6)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001872 continue;
1873 data->in[i] =
Jean Delvare787c72b2007-05-08 17:22:00 +02001874 w83627hf_read_value(data, W83781D_REG_IN(i));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001875 data->in_min[i] =
Jean Delvare787c72b2007-05-08 17:22:00 +02001876 w83627hf_read_value(data,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001877 W83781D_REG_IN_MIN(i));
1878 data->in_max[i] =
Jean Delvare787c72b2007-05-08 17:22:00 +02001879 w83627hf_read_value(data,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001880 W83781D_REG_IN_MAX(i));
1881 }
Jim Cromie2ca2fcd2007-10-14 17:20:50 -06001882 for (i = 0; i <= 2; i++) {
1883 data->fan[i] =
1884 w83627hf_read_value(data, W83627HF_REG_FAN(i));
1885 data->fan_min[i] =
Jean Delvare787c72b2007-05-08 17:22:00 +02001886 w83627hf_read_value(data,
Jim Cromie2ca2fcd2007-10-14 17:20:50 -06001887 W83627HF_REG_FAN_MIN(i));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001888 }
Jim Cromie07584c72007-10-12 21:08:00 +02001889 for (i = 0; i <= 2; i++) {
Jean Delvare787c72b2007-05-08 17:22:00 +02001890 u8 tmp = w83627hf_read_value(data,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001891 W836X7HF_REG_PWM(data->type, i));
1892 /* bits 0-3 are reserved in 627THF */
1893 if (data->type == w83627thf)
1894 tmp &= 0xf0;
Jim Cromie07584c72007-10-12 21:08:00 +02001895 data->pwm[i] = tmp;
1896 if (i == 1 &&
1897 (data->type == w83627hf || data->type == w83697hf))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001898 break;
1899 }
Carlos Olalla Martinez1550cb62007-06-09 10:11:16 -04001900 if (data->type == w83627hf) {
1901 u8 tmp = w83627hf_read_value(data,
1902 W83627HF_REG_PWM_FREQ);
1903 data->pwm_freq[0] = tmp & 0x07;
1904 data->pwm_freq[1] = (tmp >> 4) & 0x07;
1905 } else if (data->type != w83627thf) {
1906 for (i = 1; i <= 3; i++) {
1907 data->pwm_freq[i - 1] =
1908 w83627hf_read_value(data,
1909 W83637HF_REG_PWM_FREQ[i - 1]);
1910 if (i == 2 && (data->type == w83697hf))
1911 break;
1912 }
1913 }
Dominik Geyera95a5ed2008-08-06 22:41:04 +02001914 if (data->type != w83627hf) {
1915 for (i = 0; i < num_pwms; i++) {
1916 u8 tmp = w83627hf_read_value(data,
1917 W83627THF_REG_PWM_ENABLE[i]);
1918 data->pwm_enable[i] =
1919 ((tmp >> W83627THF_PWM_ENABLE_SHIFT[i])
1920 & 0x03) + 1;
1921 }
1922 }
Jim Cromiedf48ed82007-10-14 17:10:52 -06001923 for (i = 0; i < num_temps; i++) {
1924 data->temp[i] = w83627hf_read_value(
1925 data, w83627hf_reg_temp[i]);
1926 data->temp_max[i] = w83627hf_read_value(
1927 data, w83627hf_reg_temp_over[i]);
1928 data->temp_max_hyst[i] = w83627hf_read_value(
1929 data, w83627hf_reg_temp_hyst[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001930 }
1931
Jean Delvarec09c5182007-10-12 21:53:07 +02001932 w83627hf_update_fan_div(data);
1933
Linus Torvalds1da177e2005-04-16 15:20:36 -07001934 data->alarms =
Jean Delvare787c72b2007-05-08 17:22:00 +02001935 w83627hf_read_value(data, W83781D_REG_ALARM1) |
1936 (w83627hf_read_value(data, W83781D_REG_ALARM2) << 8) |
1937 (w83627hf_read_value(data, W83781D_REG_ALARM3) << 16);
1938 i = w83627hf_read_value(data, W83781D_REG_BEEP_INTS2);
Jean Delvare1c138102008-01-03 23:04:55 +01001939 data->beep_mask = (i << 8) |
Jean Delvare787c72b2007-05-08 17:22:00 +02001940 w83627hf_read_value(data, W83781D_REG_BEEP_INTS1) |
1941 w83627hf_read_value(data, W83781D_REG_BEEP_INTS3) << 16;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001942 data->last_updated = jiffies;
1943 data->valid = 1;
1944 }
1945
Ingo Molnar9a61bf62006-01-18 23:19:26 +01001946 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001947
1948 return data;
1949}
1950
Jean Delvare787c72b2007-05-08 17:22:00 +02001951static int __init w83627hf_device_add(unsigned short address,
1952 const struct w83627hf_sio_data *sio_data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001953{
Jean Delvare787c72b2007-05-08 17:22:00 +02001954 struct resource res = {
1955 .start = address + WINB_REGION_OFFSET,
1956 .end = address + WINB_REGION_OFFSET + WINB_REGION_SIZE - 1,
1957 .name = DRVNAME,
1958 .flags = IORESOURCE_IO,
1959 };
1960 int err;
1961
Jean Delvareb9acb642009-01-07 16:37:35 +01001962 err = acpi_check_resource_conflict(&res);
1963 if (err)
1964 goto exit;
1965
Jean Delvare787c72b2007-05-08 17:22:00 +02001966 pdev = platform_device_alloc(DRVNAME, address);
1967 if (!pdev) {
1968 err = -ENOMEM;
Joe Perches18de0302010-10-20 06:51:55 +00001969 pr_err("Device allocation failed\n");
Jean Delvare787c72b2007-05-08 17:22:00 +02001970 goto exit;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001971 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001972
Jean Delvare787c72b2007-05-08 17:22:00 +02001973 err = platform_device_add_resources(pdev, &res, 1);
1974 if (err) {
Joe Perches18de0302010-10-20 06:51:55 +00001975 pr_err("Device resource addition failed (%d)\n", err);
Jean Delvare787c72b2007-05-08 17:22:00 +02001976 goto exit_device_put;
1977 }
1978
Jean Delvare2df6d812007-06-09 10:11:16 -04001979 err = platform_device_add_data(pdev, sio_data,
1980 sizeof(struct w83627hf_sio_data));
1981 if (err) {
Joe Perches18de0302010-10-20 06:51:55 +00001982 pr_err("Platform data allocation failed\n");
Jean Delvare787c72b2007-05-08 17:22:00 +02001983 goto exit_device_put;
1984 }
Jean Delvare787c72b2007-05-08 17:22:00 +02001985
1986 err = platform_device_add(pdev);
1987 if (err) {
Joe Perches18de0302010-10-20 06:51:55 +00001988 pr_err("Device addition failed (%d)\n", err);
Jean Delvare787c72b2007-05-08 17:22:00 +02001989 goto exit_device_put;
1990 }
1991
1992 return 0;
1993
1994exit_device_put:
1995 platform_device_put(pdev);
1996exit:
1997 return err;
1998}
1999
2000static int __init sensors_w83627hf_init(void)
2001{
2002 int err;
2003 unsigned short address;
2004 struct w83627hf_sio_data sio_data;
2005
2006 if (w83627hf_find(0x2e, &address, &sio_data)
2007 && w83627hf_find(0x4e, &address, &sio_data))
2008 return -ENODEV;
2009
2010 err = platform_driver_register(&w83627hf_driver);
2011 if (err)
2012 goto exit;
2013
2014 /* Sets global pdev as a side effect */
2015 err = w83627hf_device_add(address, &sio_data);
2016 if (err)
2017 goto exit_driver;
2018
2019 return 0;
2020
2021exit_driver:
2022 platform_driver_unregister(&w83627hf_driver);
2023exit:
2024 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002025}
2026
2027static void __exit sensors_w83627hf_exit(void)
2028{
Jean Delvare787c72b2007-05-08 17:22:00 +02002029 platform_device_unregister(pdev);
2030 platform_driver_unregister(&w83627hf_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002031}
2032
2033MODULE_AUTHOR("Frodo Looijaard <frodol@dds.nl>, "
2034 "Philip Edelbrock <phil@netroedge.com>, "
2035 "and Mark Studebaker <mdsxyz123@yahoo.com>");
2036MODULE_DESCRIPTION("W83627HF driver");
2037MODULE_LICENSE("GPL");
2038
2039module_init(sensors_w83627hf_init);
2040module_exit(sensors_w83627hf_exit);