blob: 9be277156ed272fdb2abd8b85da30f7d6368c6a6 [file] [log] [blame]
Thomas Gleixner74ba9202019-05-20 09:19:02 +02001// SPDX-License-Identifier: GPL-2.0-or-later
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
Guenter Roeck27b9de32012-01-15 11:07:26 -08003 * w83627hf.c - Part of lm_sensors, Linux kernel modules for hardware
4 * monitoring
5 * Copyright (c) 1998 - 2003 Frodo Looijaard <frodol@dds.nl>,
6 * Philip Edelbrock <phil@netroedge.com>,
7 * and Mark Studebaker <mdsxyz123@yahoo.com>
8 * Ported to 2.6 by Bernhard C. Schrenk <clemy@clemy.org>
Jean Delvare7c81c60f2014-01-29 20:40:08 +01009 * Copyright (c) 2007 - 1012 Jean Delvare <jdelvare@suse.de>
Guenter Roeck27b9de32012-01-15 11:07:26 -080010 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070011
12/*
Guenter Roeck27b9de32012-01-15 11:07:26 -080013 * Supports following chips:
14 *
Jean Delvare4101ece2012-11-05 21:54:40 +010015 * Chip #vin #fanin #pwm #temp wchipid vendid i2c ISA
Guenter Roeck27b9de32012-01-15 11:07:26 -080016 * w83627hf 9 3 2 3 0x20 0x5ca3 no yes(LPC)
17 * w83627thf 7 3 3 3 0x90 0x5ca3 no yes(LPC)
18 * w83637hf 7 3 3 3 0x80 0x5ca3 no yes(LPC)
19 * w83687thf 7 3 3 3 0x90 0x5ca3 no yes(LPC)
20 * w83697hf 8 2 2 2 0x60 0x5ca3 no yes(LPC)
21 *
22 * For other winbond chips, and for i2c support in the above chips,
23 * use w83781d.c.
24 *
25 * Note: automatic ("cruise") fan control for 697, 637 & 627thf not
26 * supported yet.
27 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070028
Joe Perches18de0302010-10-20 06:51:55 +000029#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
30
Linus Torvalds1da177e2005-04-16 15:20:36 -070031#include <linux/module.h>
32#include <linux/init.h>
33#include <linux/slab.h>
34#include <linux/jiffies.h>
Jean Delvare787c72b2007-05-08 17:22:00 +020035#include <linux/platform_device.h>
Mark M. Hoffman943b0832005-07-15 21:39:18 -040036#include <linux/hwmon.h>
Jim Cromie07584c72007-10-12 21:08:00 +020037#include <linux/hwmon-sysfs.h>
Jean Delvare303760b2005-07-31 21:52:01 +020038#include <linux/hwmon-vid.h>
Mark M. Hoffman943b0832005-07-15 21:39:18 -040039#include <linux/err.h>
Ingo Molnar9a61bf62006-01-18 23:19:26 +010040#include <linux/mutex.h>
Jean Delvared27c37c2007-05-08 17:21:59 +020041#include <linux/ioport.h>
Jean Delvareb9acb642009-01-07 16:37:35 +010042#include <linux/acpi.h>
H Hartley Sweeten6055fae2009-09-15 17:18:13 +020043#include <linux/io.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070044#include "lm75.h"
45
Jean Delvare787c72b2007-05-08 17:22:00 +020046static struct platform_device *pdev;
Jean Delvared27c37c2007-05-08 17:21:59 +020047
48#define DRVNAME "w83627hf"
49enum chips { w83627hf, w83627thf, w83697hf, w83637hf, w83687thf };
50
Jean Delvareb72656d2009-12-09 20:35:49 +010051struct w83627hf_sio_data {
52 enum chips type;
53 int sioaddr;
54};
55
Linus Torvalds1da177e2005-04-16 15:20:36 -070056static u8 force_i2c = 0x1f;
57module_param(force_i2c, byte, 0);
58MODULE_PARM_DESC(force_i2c,
59 "Initialize the i2c address of the sensors");
60
Rusty Russell90ab5ee2012-01-13 09:32:20 +103061static bool init = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -070062module_param(init, bool, 0);
63MODULE_PARM_DESC(init, "Set to zero to bypass chip initialization");
64
Jean Delvare67b671b2007-12-06 23:13:42 +010065static unsigned short force_id;
66module_param(force_id, ushort, 0);
67MODULE_PARM_DESC(force_id, "Override the detected device ID");
68
Linus Torvalds1da177e2005-04-16 15:20:36 -070069/* modified from kernel/include/traps.c */
Guenter Roeck27b9de32012-01-15 11:07:26 -080070#define DEV 0x07 /* Register: Logical device select */
Linus Torvalds1da177e2005-04-16 15:20:36 -070071
72/* logical device numbers for superio_select (below) */
73#define W83627HF_LD_FDC 0x00
74#define W83627HF_LD_PRT 0x01
75#define W83627HF_LD_UART1 0x02
76#define W83627HF_LD_UART2 0x03
77#define W83627HF_LD_KBC 0x05
78#define W83627HF_LD_CIR 0x06 /* w83627hf only */
79#define W83627HF_LD_GAME 0x07
80#define W83627HF_LD_MIDI 0x07
81#define W83627HF_LD_GPIO1 0x07
82#define W83627HF_LD_GPIO5 0x07 /* w83627thf only */
83#define W83627HF_LD_GPIO2 0x08
84#define W83627HF_LD_GPIO3 0x09
85#define W83627HF_LD_GPIO4 0x09 /* w83627thf only */
86#define W83627HF_LD_ACPI 0x0a
87#define W83627HF_LD_HWM 0x0b
88
Guenter Roeck27b9de32012-01-15 11:07:26 -080089#define DEVID 0x20 /* Register: Device ID */
Linus Torvalds1da177e2005-04-16 15:20:36 -070090
91#define W83627THF_GPIO5_EN 0x30 /* w83627thf only */
92#define W83627THF_GPIO5_IOSR 0xf3 /* w83627thf only */
93#define W83627THF_GPIO5_DR 0xf4 /* w83627thf only */
94
Jean Delvarec2db6ce2006-01-18 23:22:12 +010095#define W83687THF_VID_EN 0x29 /* w83687thf only */
96#define W83687THF_VID_CFG 0xF0 /* w83687thf only */
97#define W83687THF_VID_DATA 0xF1 /* w83687thf only */
98
Linus Torvalds1da177e2005-04-16 15:20:36 -070099static inline void
Jean Delvareb72656d2009-12-09 20:35:49 +0100100superio_outb(struct w83627hf_sio_data *sio, int reg, int val)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101{
Jean Delvareb72656d2009-12-09 20:35:49 +0100102 outb(reg, sio->sioaddr);
103 outb(val, sio->sioaddr + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104}
105
106static inline int
Jean Delvareb72656d2009-12-09 20:35:49 +0100107superio_inb(struct w83627hf_sio_data *sio, int reg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108{
Jean Delvareb72656d2009-12-09 20:35:49 +0100109 outb(reg, sio->sioaddr);
110 return inb(sio->sioaddr + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111}
112
113static inline void
Jean Delvareb72656d2009-12-09 20:35:49 +0100114superio_select(struct w83627hf_sio_data *sio, int ld)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115{
Jean Delvareb72656d2009-12-09 20:35:49 +0100116 outb(DEV, sio->sioaddr);
117 outb(ld, sio->sioaddr + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118}
119
Guenter Roecke95fd512019-04-05 08:44:41 -0700120static inline int
Jean Delvareb72656d2009-12-09 20:35:49 +0100121superio_enter(struct w83627hf_sio_data *sio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122{
Guenter Roecke95fd512019-04-05 08:44:41 -0700123 if (!request_muxed_region(sio->sioaddr, 2, DRVNAME))
124 return -EBUSY;
125
Jean Delvareb72656d2009-12-09 20:35:49 +0100126 outb(0x87, sio->sioaddr);
127 outb(0x87, sio->sioaddr);
Guenter Roecke95fd512019-04-05 08:44:41 -0700128
129 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130}
131
132static inline void
Jean Delvareb72656d2009-12-09 20:35:49 +0100133superio_exit(struct w83627hf_sio_data *sio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134{
Jean Delvareb72656d2009-12-09 20:35:49 +0100135 outb(0xAA, sio->sioaddr);
Guenter Roecke95fd512019-04-05 08:44:41 -0700136 release_region(sio->sioaddr, 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137}
138
139#define W627_DEVID 0x52
140#define W627THF_DEVID 0x82
141#define W697_DEVID 0x60
142#define W637_DEVID 0x70
Jean Delvarec2db6ce2006-01-18 23:22:12 +0100143#define W687THF_DEVID 0x85
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144#define WINB_ACT_REG 0x30
145#define WINB_BASE_REG 0x60
146/* Constants specified below */
147
Petr Vandrovecada0c2f2005-10-07 23:11:03 +0200148/* Alignment of the base address */
149#define WINB_ALIGNMENT ~7
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150
Petr Vandrovecada0c2f2005-10-07 23:11:03 +0200151/* Offset & size of I/O region we are interested in */
152#define WINB_REGION_OFFSET 5
153#define WINB_REGION_SIZE 2
154
Jean Delvare787c72b2007-05-08 17:22:00 +0200155/* Where are the sensors address/data registers relative to the region offset */
156#define W83781D_ADDR_REG_OFFSET 0
157#define W83781D_DATA_REG_OFFSET 1
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158
159/* The W83781D registers */
160/* The W83782D registers for nr=7,8 are in bank 5 */
161#define W83781D_REG_IN_MAX(nr) ((nr < 7) ? (0x2b + (nr) * 2) : \
162 (0x554 + (((nr) - 7) * 2)))
163#define W83781D_REG_IN_MIN(nr) ((nr < 7) ? (0x2c + (nr) * 2) : \
164 (0x555 + (((nr) - 7) * 2)))
165#define W83781D_REG_IN(nr) ((nr < 7) ? (0x20 + (nr)) : \
166 (0x550 + (nr) - 7))
167
Jim Cromie2ca2fcd2007-10-14 17:20:50 -0600168/* nr:0-2 for fans:1-3 */
169#define W83627HF_REG_FAN_MIN(nr) (0x3b + (nr))
170#define W83627HF_REG_FAN(nr) (0x28 + (nr))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171
Jim Cromiedf48ed82007-10-14 17:10:52 -0600172#define W83627HF_REG_TEMP2_CONFIG 0x152
173#define W83627HF_REG_TEMP3_CONFIG 0x252
174/* these are zero-based, unlike config constants above */
175static const u16 w83627hf_reg_temp[] = { 0x27, 0x150, 0x250 };
176static const u16 w83627hf_reg_temp_hyst[] = { 0x3A, 0x153, 0x253 };
177static const u16 w83627hf_reg_temp_over[] = { 0x39, 0x155, 0x255 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178
179#define W83781D_REG_BANK 0x4E
180
181#define W83781D_REG_CONFIG 0x40
Yuan Mu4a1c44472005-11-07 22:19:04 +0100182#define W83781D_REG_ALARM1 0x459
183#define W83781D_REG_ALARM2 0x45A
184#define W83781D_REG_ALARM3 0x45B
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186#define W83781D_REG_BEEP_CONFIG 0x4D
187#define W83781D_REG_BEEP_INTS1 0x56
188#define W83781D_REG_BEEP_INTS2 0x57
189#define W83781D_REG_BEEP_INTS3 0x453
190
191#define W83781D_REG_VID_FANDIV 0x47
192
193#define W83781D_REG_CHIPID 0x49
194#define W83781D_REG_WCHIPID 0x58
195#define W83781D_REG_CHIPMAN 0x4F
196#define W83781D_REG_PIN 0x4B
197
198#define W83781D_REG_VBAT 0x5D
199
200#define W83627HF_REG_PWM1 0x5A
201#define W83627HF_REG_PWM2 0x5B
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202
Dominik Geyera95a5ed2008-08-06 22:41:04 +0200203static const u8 W83627THF_REG_PWM_ENABLE[] = {
204 0x04, /* FAN 1 mode */
205 0x04, /* FAN 2 mode */
206 0x12, /* FAN AUX mode */
207};
208static const u8 W83627THF_PWM_ENABLE_SHIFT[] = { 2, 4, 1 };
209
Jean Delvarec2db6ce2006-01-18 23:22:12 +0100210#define W83627THF_REG_PWM1 0x01 /* 697HF/637HF/687THF too */
211#define W83627THF_REG_PWM2 0x03 /* 697HF/637HF/687THF too */
212#define W83627THF_REG_PWM3 0x11 /* 637HF/687THF too */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213
Jean Delvarec2db6ce2006-01-18 23:22:12 +0100214#define W83627THF_REG_VRM_OVT_CFG 0x18 /* 637HF/687THF too */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215
216static const u8 regpwm_627hf[] = { W83627HF_REG_PWM1, W83627HF_REG_PWM2 };
217static const u8 regpwm[] = { W83627THF_REG_PWM1, W83627THF_REG_PWM2,
218 W83627THF_REG_PWM3 };
219#define W836X7HF_REG_PWM(type, nr) (((type) == w83627hf) ? \
Jim Cromie07584c72007-10-12 21:08:00 +0200220 regpwm_627hf[nr] : regpwm[nr])
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221
Carlos Olalla Martinez1550cb62007-06-09 10:11:16 -0400222#define W83627HF_REG_PWM_FREQ 0x5C /* Only for the 627HF */
223
224#define W83637HF_REG_PWM_FREQ1 0x00 /* 697HF/687THF too */
225#define W83637HF_REG_PWM_FREQ2 0x02 /* 697HF/687THF too */
226#define W83637HF_REG_PWM_FREQ3 0x10 /* 687THF too */
227
228static const u8 W83637HF_REG_PWM_FREQ[] = { W83637HF_REG_PWM_FREQ1,
229 W83637HF_REG_PWM_FREQ2,
230 W83637HF_REG_PWM_FREQ3 };
231
232#define W83627HF_BASE_PWM_FREQ 46870
233
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234#define W83781D_REG_I2C_ADDR 0x48
235#define W83781D_REG_I2C_SUBADDR 0x4A
236
237/* Sensor selection */
238#define W83781D_REG_SCFG1 0x5D
239static const u8 BIT_SCFG1[] = { 0x02, 0x04, 0x08 };
240#define W83781D_REG_SCFG2 0x59
241static const u8 BIT_SCFG2[] = { 0x10, 0x20, 0x40 };
242#define W83781D_DEFAULT_BETA 3435
243
Guenter Roeck27b9de32012-01-15 11:07:26 -0800244/*
245 * Conversions. Limit checking is only done on the TO_REG
246 * variants. Note that you should be a bit careful with which arguments
247 * these macros are called: arguments may be evaluated more than once.
248 * Fixing this is just not worth it.
249 */
Guenter Roeck2a844c12013-01-09 08:09:34 -0800250#define IN_TO_REG(val) (clamp_val((((val) + 8) / 16), 0, 255))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251#define IN_FROM_REG(val) ((val) * 16)
252
253static inline u8 FAN_TO_REG(long rpm, int div)
254{
255 if (rpm == 0)
256 return 255;
Guenter Roeck2a844c12013-01-09 08:09:34 -0800257 rpm = clamp_val(rpm, 1, 1000000);
258 return clamp_val((1350000 + rpm * div / 2) / (rpm * div), 1, 254);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259}
260
261#define TEMP_MIN (-128000)
262#define TEMP_MAX ( 127000)
263
Guenter Roeck27b9de32012-01-15 11:07:26 -0800264/*
265 * TEMP: 0.001C/bit (-128C to +127C)
266 * REG: 1C/bit, two's complement
267 */
Christian Hohnstaedt5bfedac2007-08-16 11:40:10 +0200268static u8 TEMP_TO_REG(long temp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269{
Guenter Roeck2a844c12013-01-09 08:09:34 -0800270 int ntemp = clamp_val(temp, TEMP_MIN, TEMP_MAX);
271 ntemp += (ntemp < 0 ? -500 : 500);
272 return (u8)(ntemp / 1000);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273}
274
275static int TEMP_FROM_REG(u8 reg)
276{
277 return (s8)reg * 1000;
278}
279
280#define FAN_FROM_REG(val,div) ((val)==0?-1:(val)==255?0:1350000/((val)*(div)))
281
Guenter Roeck2a844c12013-01-09 08:09:34 -0800282#define PWM_TO_REG(val) (clamp_val((val), 0, 255))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283
Carlos Olalla Martinez1550cb62007-06-09 10:11:16 -0400284static inline unsigned long pwm_freq_from_reg_627hf(u8 reg)
285{
286 unsigned long freq;
287 freq = W83627HF_BASE_PWM_FREQ >> reg;
288 return freq;
289}
290static inline u8 pwm_freq_to_reg_627hf(unsigned long val)
291{
292 u8 i;
Guenter Roeck27b9de32012-01-15 11:07:26 -0800293 /*
294 * Only 5 dividers (1 2 4 8 16)
295 * Search for the nearest available frequency
296 */
Carlos Olalla Martinez1550cb62007-06-09 10:11:16 -0400297 for (i = 0; i < 4; i++) {
298 if (val > (((W83627HF_BASE_PWM_FREQ >> i) +
299 (W83627HF_BASE_PWM_FREQ >> (i+1))) / 2))
300 break;
301 }
302 return i;
303}
304
305static inline unsigned long pwm_freq_from_reg(u8 reg)
306{
307 /* Clock bit 8 -> 180 kHz or 24 MHz */
308 unsigned long clock = (reg & 0x80) ? 180000UL : 24000000UL;
309
310 reg &= 0x7f;
311 /* This should not happen but anyway... */
312 if (reg == 0)
313 reg++;
Frans Meulenbroeks7fe83ad2012-01-05 19:50:18 +0100314 return clock / (reg << 8);
Carlos Olalla Martinez1550cb62007-06-09 10:11:16 -0400315}
316static inline u8 pwm_freq_to_reg(unsigned long val)
317{
318 /* Minimum divider value is 0x01 and maximum is 0x7F */
319 if (val >= 93750) /* The highest we can do */
320 return 0x01;
321 if (val >= 720) /* Use 24 MHz clock */
Frans Meulenbroeks7fe83ad2012-01-05 19:50:18 +0100322 return 24000000UL / (val << 8);
Carlos Olalla Martinez1550cb62007-06-09 10:11:16 -0400323 if (val < 6) /* The lowest we can do */
324 return 0xFF;
325 else /* Use 180 kHz clock */
Frans Meulenbroeks7fe83ad2012-01-05 19:50:18 +0100326 return 0x80 | (180000UL / (val << 8));
Carlos Olalla Martinez1550cb62007-06-09 10:11:16 -0400327}
328
Jean Delvare1c138102008-01-03 23:04:55 +0100329#define BEEP_MASK_FROM_REG(val) ((val) & 0xff7fff)
330#define BEEP_MASK_TO_REG(val) ((val) & 0xff7fff)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331
332#define DIV_FROM_REG(val) (1 << (val))
333
334static inline u8 DIV_TO_REG(long val)
335{
336 int i;
Guenter Roeck2a844c12013-01-09 08:09:34 -0800337 val = clamp_val(val, 1, 128) >> 1;
Grant Coadyabc01922005-05-12 13:41:51 +1000338 for (i = 0; i < 7; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339 if (val == 0)
340 break;
341 val >>= 1;
342 }
Frans Meulenbroeks7fe83ad2012-01-05 19:50:18 +0100343 return (u8)i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344}
345
Guenter Roeck27b9de32012-01-15 11:07:26 -0800346/*
347 * For each registered chip, we need to keep some data in memory.
348 * The structure is dynamically allocated.
349 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350struct w83627hf_data {
Jean Delvare787c72b2007-05-08 17:22:00 +0200351 unsigned short addr;
352 const char *name;
Tony Jones1beeffe2007-08-20 13:46:20 -0700353 struct device *hwmon_dev;
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100354 struct mutex lock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355 enum chips type;
356
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100357 struct mutex update_lock;
Paul Fertser952a11c2021-09-24 22:52:02 +0300358 bool valid; /* true if following fields are valid */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359 unsigned long last_updated; /* In jiffies */
360
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361 u8 in[9]; /* Register value */
362 u8 in_max[9]; /* Register value */
363 u8 in_min[9]; /* Register value */
364 u8 fan[3]; /* Register value */
365 u8 fan_min[3]; /* Register value */
Jim Cromiedf48ed82007-10-14 17:10:52 -0600366 u16 temp[3]; /* Register value */
367 u16 temp_max[3]; /* Register value */
368 u16 temp_max_hyst[3]; /* Register value */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369 u8 fan_div[3]; /* Register encoding, shifted right */
370 u8 vid; /* Register encoding, combined */
371 u32 alarms; /* Register encoding, combined */
372 u32 beep_mask; /* Register encoding, combined */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373 u8 pwm[3]; /* Register value */
Dominik Geyera95a5ed2008-08-06 22:41:04 +0200374 u8 pwm_enable[3]; /* 1 = manual
Guenter Roeck27b9de32012-01-15 11:07:26 -0800375 * 2 = thermal cruise (also called SmartFan I)
376 * 3 = fan speed cruise
377 */
Carlos Olalla Martinez1550cb62007-06-09 10:11:16 -0400378 u8 pwm_freq[3]; /* Register value */
Jean Delvareb26f9332007-08-16 14:30:01 +0200379 u16 sens[3]; /* 1 = pentium diode; 2 = 3904 diode;
Guenter Roeck27b9de32012-01-15 11:07:26 -0800380 * 4 = thermistor
381 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382 u8 vrm;
Jean Delvarec2db6ce2006-01-18 23:22:12 +0100383 u8 vrm_ovt; /* Register value, 627THF/637HF/687THF only */
Jean Delvare275b7d62012-12-19 22:16:59 +0100384
385#ifdef CONFIG_PM
386 /* Remember extra register values over suspend/resume */
387 u8 scfg1;
388 u8 scfg2;
389#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390};
391
Jean Delvare787c72b2007-05-08 17:22:00 +0200392static int w83627hf_probe(struct platform_device *pdev);
Bill Pemberton281dfd02012-11-19 13:25:51 -0500393static int w83627hf_remove(struct platform_device *pdev);
Jean Delvare787c72b2007-05-08 17:22:00 +0200394
395static int w83627hf_read_value(struct w83627hf_data *data, u16 reg);
396static int w83627hf_write_value(struct w83627hf_data *data, u16 reg, u16 value);
Jean Delvarec09c5182007-10-12 21:53:07 +0200397static void w83627hf_update_fan_div(struct w83627hf_data *data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398static struct w83627hf_data *w83627hf_update_device(struct device *dev);
Jean Delvare787c72b2007-05-08 17:22:00 +0200399static void w83627hf_init_device(struct platform_device *pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400
Jean Delvare275b7d62012-12-19 22:16:59 +0100401#ifdef CONFIG_PM
402static int w83627hf_suspend(struct device *dev)
403{
404 struct w83627hf_data *data = w83627hf_update_device(dev);
405
406 mutex_lock(&data->update_lock);
407 data->scfg1 = w83627hf_read_value(data, W83781D_REG_SCFG1);
408 data->scfg2 = w83627hf_read_value(data, W83781D_REG_SCFG2);
409 mutex_unlock(&data->update_lock);
410
411 return 0;
412}
413
414static int w83627hf_resume(struct device *dev)
415{
416 struct w83627hf_data *data = dev_get_drvdata(dev);
417 int i, num_temps = (data->type == w83697hf) ? 2 : 3;
418
419 /* Restore limits */
420 mutex_lock(&data->update_lock);
421 for (i = 0; i <= 8; i++) {
422 /* skip missing sensors */
423 if (((data->type == w83697hf) && (i == 1)) ||
424 ((data->type != w83627hf && data->type != w83697hf)
425 && (i == 5 || i == 6)))
426 continue;
427 w83627hf_write_value(data, W83781D_REG_IN_MAX(i),
428 data->in_max[i]);
429 w83627hf_write_value(data, W83781D_REG_IN_MIN(i),
430 data->in_min[i]);
431 }
432 for (i = 0; i <= 2; i++)
433 w83627hf_write_value(data, W83627HF_REG_FAN_MIN(i),
434 data->fan_min[i]);
435 for (i = 0; i < num_temps; i++) {
436 w83627hf_write_value(data, w83627hf_reg_temp_over[i],
437 data->temp_max[i]);
438 w83627hf_write_value(data, w83627hf_reg_temp_hyst[i],
439 data->temp_max_hyst[i]);
440 }
441
442 /* Fixup BIOS bugs */
443 if (data->type == w83627thf || data->type == w83637hf ||
444 data->type == w83687thf)
445 w83627hf_write_value(data, W83627THF_REG_VRM_OVT_CFG,
446 data->vrm_ovt);
447 w83627hf_write_value(data, W83781D_REG_SCFG1, data->scfg1);
448 w83627hf_write_value(data, W83781D_REG_SCFG2, data->scfg2);
449
450 /* Force re-reading all values */
Paul Fertser952a11c2021-09-24 22:52:02 +0300451 data->valid = false;
Jean Delvare275b7d62012-12-19 22:16:59 +0100452 mutex_unlock(&data->update_lock);
453
454 return 0;
455}
456
457static const struct dev_pm_ops w83627hf_dev_pm_ops = {
458 .suspend = w83627hf_suspend,
459 .resume = w83627hf_resume,
460};
461
462#define W83627HF_DEV_PM_OPS (&w83627hf_dev_pm_ops)
463#else
464#define W83627HF_DEV_PM_OPS NULL
465#endif /* CONFIG_PM */
466
Jean Delvare787c72b2007-05-08 17:22:00 +0200467static struct platform_driver w83627hf_driver = {
Laurent Riffardcdaf7932005-11-26 20:37:41 +0100468 .driver = {
Jean Delvared27c37c2007-05-08 17:21:59 +0200469 .name = DRVNAME,
Jean Delvare275b7d62012-12-19 22:16:59 +0100470 .pm = W83627HF_DEV_PM_OPS,
Laurent Riffardcdaf7932005-11-26 20:37:41 +0100471 },
Jean Delvare787c72b2007-05-08 17:22:00 +0200472 .probe = w83627hf_probe,
Bill Pemberton9e5e9b72012-11-19 13:21:20 -0500473 .remove = w83627hf_remove,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474};
475
Jim Cromie07584c72007-10-12 21:08:00 +0200476static ssize_t
Guenter Roeck502a92f2019-01-22 15:25:47 -0800477in_input_show(struct device *dev, struct device_attribute *devattr, char *buf)
Jim Cromie07584c72007-10-12 21:08:00 +0200478{
479 int nr = to_sensor_dev_attr(devattr)->index;
480 struct w83627hf_data *data = w83627hf_update_device(dev);
481 return sprintf(buf, "%ld\n", (long)IN_FROM_REG(data->in[nr]));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482}
Jim Cromie07584c72007-10-12 21:08:00 +0200483static ssize_t
Guenter Roeck502a92f2019-01-22 15:25:47 -0800484in_min_show(struct device *dev, struct device_attribute *devattr, char *buf)
Jim Cromie07584c72007-10-12 21:08:00 +0200485{
486 int nr = to_sensor_dev_attr(devattr)->index;
487 struct w83627hf_data *data = w83627hf_update_device(dev);
488 return sprintf(buf, "%ld\n", (long)IN_FROM_REG(data->in_min[nr]));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489}
Jim Cromie07584c72007-10-12 21:08:00 +0200490static ssize_t
Guenter Roeck502a92f2019-01-22 15:25:47 -0800491in_max_show(struct device *dev, struct device_attribute *devattr, char *buf)
Jim Cromie07584c72007-10-12 21:08:00 +0200492{
493 int nr = to_sensor_dev_attr(devattr)->index;
494 struct w83627hf_data *data = w83627hf_update_device(dev);
495 return sprintf(buf, "%ld\n", (long)IN_FROM_REG(data->in_max[nr]));
496}
497static ssize_t
Guenter Roeck502a92f2019-01-22 15:25:47 -0800498in_min_store(struct device *dev, struct device_attribute *devattr,
Jim Cromie07584c72007-10-12 21:08:00 +0200499 const char *buf, size_t count)
500{
501 int nr = to_sensor_dev_attr(devattr)->index;
502 struct w83627hf_data *data = dev_get_drvdata(dev);
Guenter Roeck27b9de32012-01-15 11:07:26 -0800503 long val;
504 int err;
505
506 err = kstrtol(buf, 10, &val);
507 if (err)
508 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509
Jim Cromie07584c72007-10-12 21:08:00 +0200510 mutex_lock(&data->update_lock);
511 data->in_min[nr] = IN_TO_REG(val);
512 w83627hf_write_value(data, W83781D_REG_IN_MIN(nr), data->in_min[nr]);
513 mutex_unlock(&data->update_lock);
514 return count;
515}
516static ssize_t
Guenter Roeck502a92f2019-01-22 15:25:47 -0800517in_max_store(struct device *dev, struct device_attribute *devattr,
Jim Cromie07584c72007-10-12 21:08:00 +0200518 const char *buf, size_t count)
519{
520 int nr = to_sensor_dev_attr(devattr)->index;
521 struct w83627hf_data *data = dev_get_drvdata(dev);
Guenter Roeck27b9de32012-01-15 11:07:26 -0800522 long val;
523 int err;
524
525 err = kstrtol(buf, 10, &val);
526 if (err)
527 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528
Jim Cromie07584c72007-10-12 21:08:00 +0200529 mutex_lock(&data->update_lock);
530 data->in_max[nr] = IN_TO_REG(val);
531 w83627hf_write_value(data, W83781D_REG_IN_MAX(nr), data->in_max[nr]);
532 mutex_unlock(&data->update_lock);
533 return count;
534}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535
Guenter Roeck502a92f2019-01-22 15:25:47 -0800536static SENSOR_DEVICE_ATTR_RO(in1_input, in_input, 1);
537static SENSOR_DEVICE_ATTR_RW(in1_min, in_min, 1);
538static SENSOR_DEVICE_ATTR_RW(in1_max, in_max, 1);
539static SENSOR_DEVICE_ATTR_RO(in2_input, in_input, 2);
540static SENSOR_DEVICE_ATTR_RW(in2_min, in_min, 2);
541static SENSOR_DEVICE_ATTR_RW(in2_max, in_max, 2);
542static SENSOR_DEVICE_ATTR_RO(in3_input, in_input, 3);
543static SENSOR_DEVICE_ATTR_RW(in3_min, in_min, 3);
544static SENSOR_DEVICE_ATTR_RW(in3_max, in_max, 3);
545static SENSOR_DEVICE_ATTR_RO(in4_input, in_input, 4);
546static SENSOR_DEVICE_ATTR_RW(in4_min, in_min, 4);
547static SENSOR_DEVICE_ATTR_RW(in4_max, in_max, 4);
548static SENSOR_DEVICE_ATTR_RO(in5_input, in_input, 5);
549static SENSOR_DEVICE_ATTR_RW(in5_min, in_min, 5);
550static SENSOR_DEVICE_ATTR_RW(in5_max, in_max, 5);
551static SENSOR_DEVICE_ATTR_RO(in6_input, in_input, 6);
552static SENSOR_DEVICE_ATTR_RW(in6_min, in_min, 6);
553static SENSOR_DEVICE_ATTR_RW(in6_max, in_max, 6);
554static SENSOR_DEVICE_ATTR_RO(in7_input, in_input, 7);
555static SENSOR_DEVICE_ATTR_RW(in7_min, in_min, 7);
556static SENSOR_DEVICE_ATTR_RW(in7_max, in_max, 7);
557static SENSOR_DEVICE_ATTR_RO(in8_input, in_input, 8);
558static SENSOR_DEVICE_ATTR_RW(in8_min, in_min, 8);
559static SENSOR_DEVICE_ATTR_RW(in8_max, in_max, 8);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560
561/* use a different set of functions for in0 */
562static ssize_t show_in_0(struct w83627hf_data *data, char *buf, u8 reg)
563{
564 long in0;
565
566 if ((data->vrm_ovt & 0x01) &&
Jean Delvarec2db6ce2006-01-18 23:22:12 +0100567 (w83627thf == data->type || w83637hf == data->type
568 || w83687thf == data->type))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569
570 /* use VRM9 calculation */
571 in0 = (long)((reg * 488 + 70000 + 50) / 100);
572 else
573 /* use VRM8 (standard) calculation */
574 in0 = (long)IN_FROM_REG(reg);
575
576 return sprintf(buf,"%ld\n", in0);
577}
578
Julia Lawall8dfcdfc2016-12-22 13:05:11 +0100579static ssize_t in0_input_show(struct device *dev,
580 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581{
582 struct w83627hf_data *data = w83627hf_update_device(dev);
583 return show_in_0(data, buf, data->in[0]);
584}
585
Julia Lawall8dfcdfc2016-12-22 13:05:11 +0100586static ssize_t in0_min_show(struct device *dev, struct device_attribute *attr,
587 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588{
589 struct w83627hf_data *data = w83627hf_update_device(dev);
590 return show_in_0(data, buf, data->in_min[0]);
591}
592
Julia Lawall8dfcdfc2016-12-22 13:05:11 +0100593static ssize_t in0_max_show(struct device *dev, struct device_attribute *attr,
594 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595{
596 struct w83627hf_data *data = w83627hf_update_device(dev);
597 return show_in_0(data, buf, data->in_max[0]);
598}
599
Julia Lawall8dfcdfc2016-12-22 13:05:11 +0100600static ssize_t in0_min_store(struct device *dev,
601 struct device_attribute *attr, const char *buf,
602 size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603{
Jean Delvare787c72b2007-05-08 17:22:00 +0200604 struct w83627hf_data *data = dev_get_drvdata(dev);
Guenter Roeck27b9de32012-01-15 11:07:26 -0800605 unsigned long val;
606 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607
Guenter Roeck27b9de32012-01-15 11:07:26 -0800608 err = kstrtoul(buf, 10, &val);
609 if (err)
610 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100612 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613
614 if ((data->vrm_ovt & 0x01) &&
Jean Delvarec2db6ce2006-01-18 23:22:12 +0100615 (w83627thf == data->type || w83637hf == data->type
616 || w83687thf == data->type))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617
618 /* use VRM9 calculation */
Yuan Mu2723ab92005-11-23 15:44:21 -0800619 data->in_min[0] =
Guenter Roeck2a844c12013-01-09 08:09:34 -0800620 clamp_val(((val * 100) - 70000 + 244) / 488, 0, 255);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621 else
622 /* use VRM8 (standard) calculation */
623 data->in_min[0] = IN_TO_REG(val);
624
Jean Delvare787c72b2007-05-08 17:22:00 +0200625 w83627hf_write_value(data, W83781D_REG_IN_MIN(0), data->in_min[0]);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100626 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627 return count;
628}
629
Julia Lawall8dfcdfc2016-12-22 13:05:11 +0100630static ssize_t in0_max_store(struct device *dev,
631 struct device_attribute *attr, const char *buf,
632 size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633{
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
Julia Lawall8dfcdfc2016-12-22 13:05:11 +0100660static DEVICE_ATTR_RO(in0_input);
661static DEVICE_ATTR_RW(in0_min);
662static DEVICE_ATTR_RW(in0_max);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664static ssize_t
Guenter Roeck502a92f2019-01-22 15:25:47 -0800665fan_input_show(struct device *dev, struct device_attribute *devattr,
666 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667{
Jim Cromie07584c72007-10-12 21:08:00 +0200668 int nr = to_sensor_dev_attr(devattr)->index;
669 struct w83627hf_data *data = w83627hf_update_device(dev);
670 return sprintf(buf, "%ld\n", FAN_FROM_REG(data->fan[nr],
671 (long)DIV_FROM_REG(data->fan_div[nr])));
672}
673static ssize_t
Guenter Roeck502a92f2019-01-22 15:25:47 -0800674fan_min_show(struct device *dev, struct device_attribute *devattr, char *buf)
Jim Cromie07584c72007-10-12 21:08:00 +0200675{
676 int nr = to_sensor_dev_attr(devattr)->index;
677 struct w83627hf_data *data = w83627hf_update_device(dev);
678 return sprintf(buf, "%ld\n", FAN_FROM_REG(data->fan_min[nr],
679 (long)DIV_FROM_REG(data->fan_div[nr])));
680}
681static ssize_t
Guenter Roeck502a92f2019-01-22 15:25:47 -0800682fan_min_store(struct device *dev, struct device_attribute *devattr,
Jim Cromie07584c72007-10-12 21:08:00 +0200683 const char *buf, size_t count)
684{
685 int nr = to_sensor_dev_attr(devattr)->index;
Jean Delvare787c72b2007-05-08 17:22:00 +0200686 struct w83627hf_data *data = dev_get_drvdata(dev);
Guenter Roeck27b9de32012-01-15 11:07:26 -0800687 unsigned long val;
688 int err;
689
690 err = kstrtoul(buf, 10, &val);
691 if (err)
692 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100694 mutex_lock(&data->update_lock);
Jim Cromie07584c72007-10-12 21:08:00 +0200695 data->fan_min[nr] = FAN_TO_REG(val, DIV_FROM_REG(data->fan_div[nr]));
Jim Cromie2ca2fcd2007-10-14 17:20:50 -0600696 w83627hf_write_value(data, W83627HF_REG_FAN_MIN(nr),
Jim Cromie07584c72007-10-12 21:08:00 +0200697 data->fan_min[nr]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100699 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700 return count;
701}
702
Guenter Roeck502a92f2019-01-22 15:25:47 -0800703static SENSOR_DEVICE_ATTR_RO(fan1_input, fan_input, 0);
704static SENSOR_DEVICE_ATTR_RW(fan1_min, fan_min, 0);
705static SENSOR_DEVICE_ATTR_RO(fan2_input, fan_input, 1);
706static SENSOR_DEVICE_ATTR_RW(fan2_min, fan_min, 1);
707static SENSOR_DEVICE_ATTR_RO(fan3_input, fan_input, 2);
708static SENSOR_DEVICE_ATTR_RW(fan3_min, fan_min, 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709
Jim Cromie07584c72007-10-12 21:08:00 +0200710static ssize_t
Guenter Roeck502a92f2019-01-22 15:25:47 -0800711temp_show(struct device *dev, struct device_attribute *devattr, char *buf)
Jim Cromie07584c72007-10-12 21:08:00 +0200712{
713 int nr = to_sensor_dev_attr(devattr)->index;
714 struct w83627hf_data *data = w83627hf_update_device(dev);
Jim Cromiedf48ed82007-10-14 17:10:52 -0600715
716 u16 tmp = data->temp[nr];
717 return sprintf(buf, "%ld\n", (nr) ? (long) LM75_TEMP_FROM_REG(tmp)
718 : (long) TEMP_FROM_REG(tmp));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720
Jim Cromie07584c72007-10-12 21:08:00 +0200721static ssize_t
Guenter Roeck502a92f2019-01-22 15:25:47 -0800722temp_max_show(struct device *dev, struct device_attribute *devattr, char *buf)
Jim Cromie07584c72007-10-12 21:08:00 +0200723{
724 int nr = to_sensor_dev_attr(devattr)->index;
725 struct w83627hf_data *data = w83627hf_update_device(dev);
Jim Cromiedf48ed82007-10-14 17:10:52 -0600726
727 u16 tmp = data->temp_max[nr];
728 return sprintf(buf, "%ld\n", (nr) ? (long) LM75_TEMP_FROM_REG(tmp)
729 : (long) TEMP_FROM_REG(tmp));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731
Jim Cromie07584c72007-10-12 21:08:00 +0200732static ssize_t
Guenter Roeck502a92f2019-01-22 15:25:47 -0800733temp_max_hyst_show(struct device *dev, struct device_attribute *devattr,
Jim Cromie07584c72007-10-12 21:08:00 +0200734 char *buf)
735{
736 int nr = to_sensor_dev_attr(devattr)->index;
737 struct w83627hf_data *data = w83627hf_update_device(dev);
Jim Cromiedf48ed82007-10-14 17:10:52 -0600738
739 u16 tmp = data->temp_max_hyst[nr];
740 return sprintf(buf, "%ld\n", (nr) ? (long) LM75_TEMP_FROM_REG(tmp)
741 : (long) TEMP_FROM_REG(tmp));
Jim Cromie07584c72007-10-12 21:08:00 +0200742}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743
Jim Cromie07584c72007-10-12 21:08:00 +0200744static ssize_t
Guenter Roeck502a92f2019-01-22 15:25:47 -0800745temp_max_store(struct device *dev, struct device_attribute *devattr,
Jim Cromie07584c72007-10-12 21:08:00 +0200746 const char *buf, size_t count)
747{
748 int nr = to_sensor_dev_attr(devattr)->index;
749 struct w83627hf_data *data = dev_get_drvdata(dev);
Guenter Roeck27b9de32012-01-15 11:07:26 -0800750 u16 tmp;
751 long val;
752 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753
Guenter Roeck27b9de32012-01-15 11:07:26 -0800754 err = kstrtol(buf, 10, &val);
755 if (err)
756 return err;
757
758 tmp = (nr) ? LM75_TEMP_TO_REG(val) : TEMP_TO_REG(val);
Jim Cromie07584c72007-10-12 21:08:00 +0200759 mutex_lock(&data->update_lock);
Jim Cromiedf48ed82007-10-14 17:10:52 -0600760 data->temp_max[nr] = tmp;
761 w83627hf_write_value(data, w83627hf_reg_temp_over[nr], tmp);
Jim Cromie07584c72007-10-12 21:08:00 +0200762 mutex_unlock(&data->update_lock);
763 return count;
764}
765
766static ssize_t
Guenter Roeck502a92f2019-01-22 15:25:47 -0800767temp_max_hyst_store(struct device *dev, struct device_attribute *devattr,
Jim Cromie07584c72007-10-12 21:08:00 +0200768 const char *buf, size_t count)
769{
770 int nr = to_sensor_dev_attr(devattr)->index;
771 struct w83627hf_data *data = dev_get_drvdata(dev);
Guenter Roeck27b9de32012-01-15 11:07:26 -0800772 u16 tmp;
773 long val;
774 int err;
Jim Cromie07584c72007-10-12 21:08:00 +0200775
Guenter Roeck27b9de32012-01-15 11:07:26 -0800776 err = kstrtol(buf, 10, &val);
777 if (err)
778 return err;
779
780 tmp = (nr) ? LM75_TEMP_TO_REG(val) : TEMP_TO_REG(val);
Jim Cromie07584c72007-10-12 21:08:00 +0200781 mutex_lock(&data->update_lock);
Jim Cromiedf48ed82007-10-14 17:10:52 -0600782 data->temp_max_hyst[nr] = tmp;
783 w83627hf_write_value(data, w83627hf_reg_temp_hyst[nr], tmp);
Jim Cromie07584c72007-10-12 21:08:00 +0200784 mutex_unlock(&data->update_lock);
785 return count;
786}
787
Guenter Roeck502a92f2019-01-22 15:25:47 -0800788static SENSOR_DEVICE_ATTR_RO(temp1_input, temp, 0);
789static SENSOR_DEVICE_ATTR_RW(temp1_max, temp_max, 0);
790static SENSOR_DEVICE_ATTR_RW(temp1_max_hyst, temp_max_hyst, 0);
791static SENSOR_DEVICE_ATTR_RO(temp2_input, temp, 1);
792static SENSOR_DEVICE_ATTR_RW(temp2_max, temp_max, 1);
793static SENSOR_DEVICE_ATTR_RW(temp2_max_hyst, temp_max_hyst, 1);
794static SENSOR_DEVICE_ATTR_RO(temp3_input, temp, 2);
795static SENSOR_DEVICE_ATTR_RW(temp3_max, temp_max, 2);
796static SENSOR_DEVICE_ATTR_RW(temp3_max_hyst, temp_max_hyst, 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798static ssize_t
Julia Lawall8dfcdfc2016-12-22 13:05:11 +0100799cpu0_vid_show(struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800{
801 struct w83627hf_data *data = w83627hf_update_device(dev);
802 return sprintf(buf, "%ld\n", (long) vid_from_reg(data->vid, data->vrm));
803}
Julia Lawall8dfcdfc2016-12-22 13:05:11 +0100804static DEVICE_ATTR_RO(cpu0_vid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805
806static ssize_t
Julia Lawall8dfcdfc2016-12-22 13:05:11 +0100807vrm_show(struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808{
Jean Delvare90d66192007-10-08 18:24:35 +0200809 struct w83627hf_data *data = dev_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810 return sprintf(buf, "%ld\n", (long) data->vrm);
811}
812static ssize_t
Julia Lawall8dfcdfc2016-12-22 13:05:11 +0100813vrm_store(struct device *dev, struct device_attribute *attr, const char *buf,
814 size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815{
Jean Delvare787c72b2007-05-08 17:22:00 +0200816 struct w83627hf_data *data = dev_get_drvdata(dev);
Guenter Roeck27b9de32012-01-15 11:07:26 -0800817 unsigned long val;
818 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819
Guenter Roeck27b9de32012-01-15 11:07:26 -0800820 err = kstrtoul(buf, 10, &val);
821 if (err)
822 return err;
Axel Lin970255b2014-08-06 08:27:11 +0800823
824 if (val > 255)
825 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826 data->vrm = val;
827
828 return count;
829}
Julia Lawall8dfcdfc2016-12-22 13:05:11 +0100830static DEVICE_ATTR_RW(vrm);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831
832static ssize_t
Julia Lawall8dfcdfc2016-12-22 13:05:11 +0100833alarms_show(struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834{
835 struct w83627hf_data *data = w83627hf_update_device(dev);
836 return sprintf(buf, "%ld\n", (long) data->alarms);
837}
Julia Lawall8dfcdfc2016-12-22 13:05:11 +0100838static DEVICE_ATTR_RO(alarms);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839
Jean Delvaree3604c62008-01-03 23:00:30 +0100840static ssize_t
Guenter Roeck502a92f2019-01-22 15:25:47 -0800841alarm_show(struct device *dev, struct device_attribute *attr, char *buf)
Jean Delvaree3604c62008-01-03 23:00:30 +0100842{
843 struct w83627hf_data *data = w83627hf_update_device(dev);
844 int bitnr = to_sensor_dev_attr(attr)->index;
845 return sprintf(buf, "%u\n", (data->alarms >> bitnr) & 1);
846}
Guenter Roeck502a92f2019-01-22 15:25:47 -0800847static SENSOR_DEVICE_ATTR_RO(in0_alarm, alarm, 0);
848static SENSOR_DEVICE_ATTR_RO(in1_alarm, alarm, 1);
849static SENSOR_DEVICE_ATTR_RO(in2_alarm, alarm, 2);
850static SENSOR_DEVICE_ATTR_RO(in3_alarm, alarm, 3);
851static SENSOR_DEVICE_ATTR_RO(in4_alarm, alarm, 8);
852static SENSOR_DEVICE_ATTR_RO(in5_alarm, alarm, 9);
853static SENSOR_DEVICE_ATTR_RO(in6_alarm, alarm, 10);
854static SENSOR_DEVICE_ATTR_RO(in7_alarm, alarm, 16);
855static SENSOR_DEVICE_ATTR_RO(in8_alarm, alarm, 17);
856static SENSOR_DEVICE_ATTR_RO(fan1_alarm, alarm, 6);
857static SENSOR_DEVICE_ATTR_RO(fan2_alarm, alarm, 7);
858static SENSOR_DEVICE_ATTR_RO(fan3_alarm, alarm, 11);
859static SENSOR_DEVICE_ATTR_RO(temp1_alarm, alarm, 4);
860static SENSOR_DEVICE_ATTR_RO(temp2_alarm, alarm, 5);
861static SENSOR_DEVICE_ATTR_RO(temp3_alarm, alarm, 13);
Jean Delvaree3604c62008-01-03 23:00:30 +0100862
Jean Delvare1c138102008-01-03 23:04:55 +0100863static ssize_t
Julia Lawall8dfcdfc2016-12-22 13:05:11 +0100864beep_mask_show(struct device *dev, struct device_attribute *attr, char *buf)
Jean Delvare1c138102008-01-03 23:04:55 +0100865{
866 struct w83627hf_data *data = w83627hf_update_device(dev);
867 return sprintf(buf, "%ld\n",
868 (long)BEEP_MASK_FROM_REG(data->beep_mask));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870
871static ssize_t
Julia Lawall8dfcdfc2016-12-22 13:05:11 +0100872beep_mask_store(struct device *dev, struct device_attribute *attr,
Jean Delvare1c138102008-01-03 23:04:55 +0100873 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874{
Jean Delvare787c72b2007-05-08 17:22:00 +0200875 struct w83627hf_data *data = dev_get_drvdata(dev);
Jean Delvare1c138102008-01-03 23:04:55 +0100876 unsigned long val;
Guenter Roeck27b9de32012-01-15 11:07:26 -0800877 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878
Guenter Roeck27b9de32012-01-15 11:07:26 -0800879 err = kstrtoul(buf, 10, &val);
880 if (err)
881 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100883 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884
Jean Delvare1c138102008-01-03 23:04:55 +0100885 /* preserve beep enable */
886 data->beep_mask = (data->beep_mask & 0x8000)
887 | BEEP_MASK_TO_REG(val);
888 w83627hf_write_value(data, W83781D_REG_BEEP_INTS1,
889 data->beep_mask & 0xff);
890 w83627hf_write_value(data, W83781D_REG_BEEP_INTS3,
891 ((data->beep_mask) >> 16) & 0xff);
Jean Delvare787c72b2007-05-08 17:22:00 +0200892 w83627hf_write_value(data, W83781D_REG_BEEP_INTS2,
Jean Delvare1c138102008-01-03 23:04:55 +0100893 (data->beep_mask >> 8) & 0xff);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100895 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896 return count;
897}
898
Julia Lawall8dfcdfc2016-12-22 13:05:11 +0100899static DEVICE_ATTR_RW(beep_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901static ssize_t
Guenter Roeck502a92f2019-01-22 15:25:47 -0800902beep_show(struct device *dev, struct device_attribute *attr, char *buf)
Jean Delvaree3604c62008-01-03 23:00:30 +0100903{
904 struct w83627hf_data *data = w83627hf_update_device(dev);
905 int bitnr = to_sensor_dev_attr(attr)->index;
906 return sprintf(buf, "%u\n", (data->beep_mask >> bitnr) & 1);
907}
908
909static ssize_t
Guenter Roeck502a92f2019-01-22 15:25:47 -0800910beep_store(struct device *dev, struct device_attribute *attr, const char *buf,
911 size_t count)
Jean Delvaree3604c62008-01-03 23:00:30 +0100912{
913 struct w83627hf_data *data = dev_get_drvdata(dev);
914 int bitnr = to_sensor_dev_attr(attr)->index;
Jean Delvaree3604c62008-01-03 23:00:30 +0100915 u8 reg;
Guenter Roeck27b9de32012-01-15 11:07:26 -0800916 unsigned long bit;
917 int err;
Jean Delvaree3604c62008-01-03 23:00:30 +0100918
Guenter Roeck27b9de32012-01-15 11:07:26 -0800919 err = kstrtoul(buf, 10, &bit);
920 if (err)
921 return err;
922
Jean Delvaree3604c62008-01-03 23:00:30 +0100923 if (bit & ~1)
924 return -EINVAL;
925
926 mutex_lock(&data->update_lock);
927 if (bit)
928 data->beep_mask |= (1 << bitnr);
929 else
930 data->beep_mask &= ~(1 << bitnr);
931
932 if (bitnr < 8) {
933 reg = w83627hf_read_value(data, W83781D_REG_BEEP_INTS1);
934 if (bit)
935 reg |= (1 << bitnr);
936 else
937 reg &= ~(1 << bitnr);
938 w83627hf_write_value(data, W83781D_REG_BEEP_INTS1, reg);
939 } else if (bitnr < 16) {
940 reg = w83627hf_read_value(data, W83781D_REG_BEEP_INTS2);
941 if (bit)
942 reg |= (1 << (bitnr - 8));
943 else
944 reg &= ~(1 << (bitnr - 8));
945 w83627hf_write_value(data, W83781D_REG_BEEP_INTS2, reg);
946 } else {
947 reg = w83627hf_read_value(data, W83781D_REG_BEEP_INTS3);
948 if (bit)
949 reg |= (1 << (bitnr - 16));
950 else
951 reg &= ~(1 << (bitnr - 16));
952 w83627hf_write_value(data, W83781D_REG_BEEP_INTS3, reg);
953 }
954 mutex_unlock(&data->update_lock);
955
956 return count;
957}
958
Guenter Roeck502a92f2019-01-22 15:25:47 -0800959static SENSOR_DEVICE_ATTR_RW(in0_beep, beep, 0);
960static SENSOR_DEVICE_ATTR_RW(in1_beep, beep, 1);
961static SENSOR_DEVICE_ATTR_RW(in2_beep, beep, 2);
962static SENSOR_DEVICE_ATTR_RW(in3_beep, beep, 3);
963static SENSOR_DEVICE_ATTR_RW(in4_beep, beep, 8);
964static SENSOR_DEVICE_ATTR_RW(in5_beep, beep, 9);
965static SENSOR_DEVICE_ATTR_RW(in6_beep, beep, 10);
966static SENSOR_DEVICE_ATTR_RW(in7_beep, beep, 16);
967static SENSOR_DEVICE_ATTR_RW(in8_beep, beep, 17);
968static SENSOR_DEVICE_ATTR_RW(fan1_beep, beep, 6);
969static SENSOR_DEVICE_ATTR_RW(fan2_beep, beep, 7);
970static SENSOR_DEVICE_ATTR_RW(fan3_beep, beep, 11);
971static SENSOR_DEVICE_ATTR_RW(temp1_beep, beep, 4);
972static SENSOR_DEVICE_ATTR_RW(temp2_beep, beep, 5);
973static SENSOR_DEVICE_ATTR_RW(temp3_beep, beep, 13);
974static SENSOR_DEVICE_ATTR_RW(beep_enable, beep, 15);
Jean Delvaree3604c62008-01-03 23:00:30 +0100975
976static ssize_t
Guenter Roeck502a92f2019-01-22 15:25:47 -0800977fan_div_show(struct device *dev, struct device_attribute *devattr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700978{
Jim Cromie07584c72007-10-12 21:08:00 +0200979 int nr = to_sensor_dev_attr(devattr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980 struct w83627hf_data *data = w83627hf_update_device(dev);
981 return sprintf(buf, "%ld\n",
Jim Cromie07584c72007-10-12 21:08:00 +0200982 (long) DIV_FROM_REG(data->fan_div[nr]));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983}
Guenter Roeck27b9de32012-01-15 11:07:26 -0800984/*
985 * Note: we save and restore the fan minimum here, because its value is
986 * determined in part by the fan divisor. This follows the principle of
987 * least surprise; the user doesn't expect the fan minimum to change just
988 * because the divisor changed.
989 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990static ssize_t
Guenter Roeck502a92f2019-01-22 15:25:47 -0800991fan_div_store(struct device *dev, struct device_attribute *devattr,
Jim Cromie07584c72007-10-12 21:08:00 +0200992 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700993{
Jim Cromie07584c72007-10-12 21:08:00 +0200994 int nr = to_sensor_dev_attr(devattr)->index;
Jean Delvare787c72b2007-05-08 17:22:00 +0200995 struct w83627hf_data *data = dev_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700996 unsigned long min;
997 u8 reg;
Guenter Roeck27b9de32012-01-15 11:07:26 -0800998 unsigned long val;
999 int err;
1000
1001 err = kstrtoul(buf, 10, &val);
1002 if (err)
1003 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004
Ingo Molnar9a61bf62006-01-18 23:19:26 +01001005 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001006
1007 /* Save fan_min */
1008 min = FAN_FROM_REG(data->fan_min[nr],
1009 DIV_FROM_REG(data->fan_div[nr]));
1010
1011 data->fan_div[nr] = DIV_TO_REG(val);
1012
Jean Delvare787c72b2007-05-08 17:22:00 +02001013 reg = (w83627hf_read_value(data, nr==2 ? W83781D_REG_PIN : W83781D_REG_VID_FANDIV)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001014 & (nr==0 ? 0xcf : 0x3f))
1015 | ((data->fan_div[nr] & 0x03) << (nr==0 ? 4 : 6));
Jean Delvare787c72b2007-05-08 17:22:00 +02001016 w83627hf_write_value(data, nr==2 ? W83781D_REG_PIN : W83781D_REG_VID_FANDIV, reg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001017
Jean Delvare787c72b2007-05-08 17:22:00 +02001018 reg = (w83627hf_read_value(data, W83781D_REG_VBAT)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019 & ~(1 << (5 + nr)))
1020 | ((data->fan_div[nr] & 0x04) << (3 + nr));
Jean Delvare787c72b2007-05-08 17:22:00 +02001021 w83627hf_write_value(data, W83781D_REG_VBAT, reg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001022
1023 /* Restore fan_min */
1024 data->fan_min[nr] = FAN_TO_REG(min, DIV_FROM_REG(data->fan_div[nr]));
Jim Cromie2ca2fcd2007-10-14 17:20:50 -06001025 w83627hf_write_value(data, W83627HF_REG_FAN_MIN(nr), data->fan_min[nr]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001026
Ingo Molnar9a61bf62006-01-18 23:19:26 +01001027 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001028 return count;
1029}
1030
Guenter Roeck502a92f2019-01-22 15:25:47 -08001031static SENSOR_DEVICE_ATTR_RW(fan1_div, fan_div, 0);
1032static SENSOR_DEVICE_ATTR_RW(fan2_div, fan_div, 1);
1033static SENSOR_DEVICE_ATTR_RW(fan3_div, fan_div, 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001034
Linus Torvalds1da177e2005-04-16 15:20:36 -07001035static ssize_t
Guenter Roeck502a92f2019-01-22 15:25:47 -08001036pwm_show(struct device *dev, struct device_attribute *devattr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001037{
Jim Cromie07584c72007-10-12 21:08:00 +02001038 int nr = to_sensor_dev_attr(devattr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001039 struct w83627hf_data *data = w83627hf_update_device(dev);
Jim Cromie07584c72007-10-12 21:08:00 +02001040 return sprintf(buf, "%ld\n", (long) data->pwm[nr]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001041}
1042
1043static ssize_t
Guenter Roeck502a92f2019-01-22 15:25:47 -08001044pwm_store(struct device *dev, struct device_attribute *devattr,
Jim Cromie07584c72007-10-12 21:08:00 +02001045 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046{
Jim Cromie07584c72007-10-12 21:08:00 +02001047 int nr = to_sensor_dev_attr(devattr)->index;
Jean Delvare787c72b2007-05-08 17:22:00 +02001048 struct w83627hf_data *data = dev_get_drvdata(dev);
Guenter Roeck27b9de32012-01-15 11:07:26 -08001049 unsigned long val;
1050 int err;
1051
1052 err = kstrtoul(buf, 10, &val);
1053 if (err)
1054 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001055
Ingo Molnar9a61bf62006-01-18 23:19:26 +01001056 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057
1058 if (data->type == w83627thf) {
1059 /* bits 0-3 are reserved in 627THF */
Jim Cromie07584c72007-10-12 21:08:00 +02001060 data->pwm[nr] = PWM_TO_REG(val) & 0xf0;
Jean Delvare787c72b2007-05-08 17:22:00 +02001061 w83627hf_write_value(data,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062 W836X7HF_REG_PWM(data->type, nr),
Jim Cromie07584c72007-10-12 21:08:00 +02001063 data->pwm[nr] |
Jean Delvare787c72b2007-05-08 17:22:00 +02001064 (w83627hf_read_value(data,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001065 W836X7HF_REG_PWM(data->type, nr)) & 0x0f));
1066 } else {
Jim Cromie07584c72007-10-12 21:08:00 +02001067 data->pwm[nr] = PWM_TO_REG(val);
Jean Delvare787c72b2007-05-08 17:22:00 +02001068 w83627hf_write_value(data,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001069 W836X7HF_REG_PWM(data->type, nr),
Jim Cromie07584c72007-10-12 21:08:00 +02001070 data->pwm[nr]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001071 }
1072
Ingo Molnar9a61bf62006-01-18 23:19:26 +01001073 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001074 return count;
1075}
1076
Guenter Roeck502a92f2019-01-22 15:25:47 -08001077static SENSOR_DEVICE_ATTR_RW(pwm1, pwm, 0);
1078static SENSOR_DEVICE_ATTR_RW(pwm2, pwm, 1);
1079static SENSOR_DEVICE_ATTR_RW(pwm3, pwm, 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001080
Linus Torvalds1da177e2005-04-16 15:20:36 -07001081static ssize_t
Guenter Roeck502a92f2019-01-22 15:25:47 -08001082pwm_enable_show(struct device *dev, struct device_attribute *devattr,
1083 char *buf)
Dominik Geyera95a5ed2008-08-06 22:41:04 +02001084{
1085 int nr = to_sensor_dev_attr(devattr)->index;
1086 struct w83627hf_data *data = w83627hf_update_device(dev);
1087 return sprintf(buf, "%d\n", data->pwm_enable[nr]);
1088}
1089
1090static ssize_t
Guenter Roeck502a92f2019-01-22 15:25:47 -08001091pwm_enable_store(struct device *dev, struct device_attribute *devattr,
1092 const char *buf, size_t count)
Dominik Geyera95a5ed2008-08-06 22:41:04 +02001093{
1094 int nr = to_sensor_dev_attr(devattr)->index;
1095 struct w83627hf_data *data = dev_get_drvdata(dev);
Dominik Geyera95a5ed2008-08-06 22:41:04 +02001096 u8 reg;
Guenter Roeck27b9de32012-01-15 11:07:26 -08001097 unsigned long val;
1098 int err;
Dominik Geyera95a5ed2008-08-06 22:41:04 +02001099
Guenter Roeck27b9de32012-01-15 11:07:26 -08001100 err = kstrtoul(buf, 10, &val);
1101 if (err)
1102 return err;
1103
1104 if (!val || val > 3) /* modes 1, 2 and 3 are supported */
Dominik Geyera95a5ed2008-08-06 22:41:04 +02001105 return -EINVAL;
1106 mutex_lock(&data->update_lock);
1107 data->pwm_enable[nr] = val;
1108 reg = w83627hf_read_value(data, W83627THF_REG_PWM_ENABLE[nr]);
1109 reg &= ~(0x03 << W83627THF_PWM_ENABLE_SHIFT[nr]);
1110 reg |= (val - 1) << W83627THF_PWM_ENABLE_SHIFT[nr];
1111 w83627hf_write_value(data, W83627THF_REG_PWM_ENABLE[nr], reg);
1112 mutex_unlock(&data->update_lock);
1113 return count;
1114}
1115
Guenter Roeck502a92f2019-01-22 15:25:47 -08001116static SENSOR_DEVICE_ATTR_RW(pwm1_enable, pwm_enable, 0);
1117static SENSOR_DEVICE_ATTR_RW(pwm2_enable, pwm_enable, 1);
1118static SENSOR_DEVICE_ATTR_RW(pwm3_enable, pwm_enable, 2);
Dominik Geyera95a5ed2008-08-06 22:41:04 +02001119
1120static ssize_t
Guenter Roeck502a92f2019-01-22 15:25:47 -08001121pwm_freq_show(struct device *dev, struct device_attribute *devattr, char *buf)
Carlos Olalla Martinez1550cb62007-06-09 10:11:16 -04001122{
Jim Cromie07584c72007-10-12 21:08:00 +02001123 int nr = to_sensor_dev_attr(devattr)->index;
Carlos Olalla Martinez1550cb62007-06-09 10:11:16 -04001124 struct w83627hf_data *data = w83627hf_update_device(dev);
1125 if (data->type == w83627hf)
1126 return sprintf(buf, "%ld\n",
Jim Cromie07584c72007-10-12 21:08:00 +02001127 pwm_freq_from_reg_627hf(data->pwm_freq[nr]));
Carlos Olalla Martinez1550cb62007-06-09 10:11:16 -04001128 else
1129 return sprintf(buf, "%ld\n",
Jim Cromie07584c72007-10-12 21:08:00 +02001130 pwm_freq_from_reg(data->pwm_freq[nr]));
Carlos Olalla Martinez1550cb62007-06-09 10:11:16 -04001131}
1132
1133static ssize_t
Guenter Roeck502a92f2019-01-22 15:25:47 -08001134pwm_freq_store(struct device *dev, struct device_attribute *devattr,
Jim Cromie07584c72007-10-12 21:08:00 +02001135 const char *buf, size_t count)
Carlos Olalla Martinez1550cb62007-06-09 10:11:16 -04001136{
Jim Cromie07584c72007-10-12 21:08:00 +02001137 int nr = to_sensor_dev_attr(devattr)->index;
Carlos Olalla Martinez1550cb62007-06-09 10:11:16 -04001138 struct w83627hf_data *data = dev_get_drvdata(dev);
1139 static const u8 mask[]={0xF8, 0x8F};
Guenter Roeck27b9de32012-01-15 11:07:26 -08001140 unsigned long val;
1141 int err;
Carlos Olalla Martinez1550cb62007-06-09 10:11:16 -04001142
Guenter Roeck27b9de32012-01-15 11:07:26 -08001143 err = kstrtoul(buf, 10, &val);
1144 if (err)
1145 return err;
Carlos Olalla Martinez1550cb62007-06-09 10:11:16 -04001146
1147 mutex_lock(&data->update_lock);
1148
1149 if (data->type == w83627hf) {
Jim Cromie07584c72007-10-12 21:08:00 +02001150 data->pwm_freq[nr] = pwm_freq_to_reg_627hf(val);
Carlos Olalla Martinez1550cb62007-06-09 10:11:16 -04001151 w83627hf_write_value(data, W83627HF_REG_PWM_FREQ,
Jim Cromie07584c72007-10-12 21:08:00 +02001152 (data->pwm_freq[nr] << (nr*4)) |
Carlos Olalla Martinez1550cb62007-06-09 10:11:16 -04001153 (w83627hf_read_value(data,
Jim Cromie07584c72007-10-12 21:08:00 +02001154 W83627HF_REG_PWM_FREQ) & mask[nr]));
Carlos Olalla Martinez1550cb62007-06-09 10:11:16 -04001155 } else {
Jim Cromie07584c72007-10-12 21:08:00 +02001156 data->pwm_freq[nr] = pwm_freq_to_reg(val);
1157 w83627hf_write_value(data, W83637HF_REG_PWM_FREQ[nr],
1158 data->pwm_freq[nr]);
Carlos Olalla Martinez1550cb62007-06-09 10:11:16 -04001159 }
1160
1161 mutex_unlock(&data->update_lock);
1162 return count;
1163}
1164
Guenter Roeck502a92f2019-01-22 15:25:47 -08001165static SENSOR_DEVICE_ATTR_RW(pwm1_freq, pwm_freq, 0);
1166static SENSOR_DEVICE_ATTR_RW(pwm2_freq, pwm_freq, 1);
1167static SENSOR_DEVICE_ATTR_RW(pwm3_freq, pwm_freq, 2);
Carlos Olalla Martinez1550cb62007-06-09 10:11:16 -04001168
1169static ssize_t
Guenter Roeck502a92f2019-01-22 15:25:47 -08001170temp_type_show(struct device *dev, struct device_attribute *devattr,
Jim Cromie07584c72007-10-12 21:08:00 +02001171 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001172{
Jim Cromie07584c72007-10-12 21:08:00 +02001173 int nr = to_sensor_dev_attr(devattr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001174 struct w83627hf_data *data = w83627hf_update_device(dev);
Jim Cromie07584c72007-10-12 21:08:00 +02001175 return sprintf(buf, "%ld\n", (long) data->sens[nr]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001176}
1177
1178static ssize_t
Guenter Roeck502a92f2019-01-22 15:25:47 -08001179temp_type_store(struct device *dev, struct device_attribute *devattr,
Jim Cromie07584c72007-10-12 21:08:00 +02001180 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001181{
Jim Cromie07584c72007-10-12 21:08:00 +02001182 int nr = to_sensor_dev_attr(devattr)->index;
Jean Delvare787c72b2007-05-08 17:22:00 +02001183 struct w83627hf_data *data = dev_get_drvdata(dev);
Guenter Roeck27b9de32012-01-15 11:07:26 -08001184 unsigned long val;
1185 u32 tmp;
1186 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001187
Guenter Roeck27b9de32012-01-15 11:07:26 -08001188 err = kstrtoul(buf, 10, &val);
1189 if (err)
1190 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001191
Ingo Molnar9a61bf62006-01-18 23:19:26 +01001192 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001193
1194 switch (val) {
1195 case 1: /* PII/Celeron diode */
Jean Delvare787c72b2007-05-08 17:22:00 +02001196 tmp = w83627hf_read_value(data, W83781D_REG_SCFG1);
1197 w83627hf_write_value(data, W83781D_REG_SCFG1,
Jim Cromie07584c72007-10-12 21:08:00 +02001198 tmp | BIT_SCFG1[nr]);
Jean Delvare787c72b2007-05-08 17:22:00 +02001199 tmp = w83627hf_read_value(data, W83781D_REG_SCFG2);
1200 w83627hf_write_value(data, W83781D_REG_SCFG2,
Jim Cromie07584c72007-10-12 21:08:00 +02001201 tmp | BIT_SCFG2[nr]);
1202 data->sens[nr] = val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001203 break;
1204 case 2: /* 3904 */
Jean Delvare787c72b2007-05-08 17:22:00 +02001205 tmp = w83627hf_read_value(data, W83781D_REG_SCFG1);
1206 w83627hf_write_value(data, W83781D_REG_SCFG1,
Jim Cromie07584c72007-10-12 21:08:00 +02001207 tmp | BIT_SCFG1[nr]);
Jean Delvare787c72b2007-05-08 17:22:00 +02001208 tmp = w83627hf_read_value(data, W83781D_REG_SCFG2);
1209 w83627hf_write_value(data, W83781D_REG_SCFG2,
Jim Cromie07584c72007-10-12 21:08:00 +02001210 tmp & ~BIT_SCFG2[nr]);
1211 data->sens[nr] = val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001212 break;
Jean Delvareb26f9332007-08-16 14:30:01 +02001213 case W83781D_DEFAULT_BETA:
1214 dev_warn(dev, "Sensor type %d is deprecated, please use 4 "
1215 "instead\n", W83781D_DEFAULT_BETA);
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -05001216 fallthrough;
Jean Delvareb26f9332007-08-16 14:30:01 +02001217 case 4: /* thermistor */
Jean Delvare787c72b2007-05-08 17:22:00 +02001218 tmp = w83627hf_read_value(data, W83781D_REG_SCFG1);
1219 w83627hf_write_value(data, W83781D_REG_SCFG1,
Jim Cromie07584c72007-10-12 21:08:00 +02001220 tmp & ~BIT_SCFG1[nr]);
1221 data->sens[nr] = val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001222 break;
1223 default:
Jean Delvare787c72b2007-05-08 17:22:00 +02001224 dev_err(dev,
Jean Delvareb26f9332007-08-16 14:30:01 +02001225 "Invalid sensor type %ld; must be 1, 2, or 4\n",
1226 (long) val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001227 break;
1228 }
1229
Ingo Molnar9a61bf62006-01-18 23:19:26 +01001230 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001231 return count;
1232}
1233
Guenter Roeck502a92f2019-01-22 15:25:47 -08001234static SENSOR_DEVICE_ATTR_RW(temp1_type, temp_type, 0);
1235static SENSOR_DEVICE_ATTR_RW(temp2_type, temp_type, 1);
1236static SENSOR_DEVICE_ATTR_RW(temp3_type, temp_type, 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001237
Jim Cromie07584c72007-10-12 21:08:00 +02001238static ssize_t
Julia Lawall8dfcdfc2016-12-22 13:05:11 +01001239name_show(struct device *dev, struct device_attribute *devattr, char *buf)
Jean Delvare787c72b2007-05-08 17:22:00 +02001240{
1241 struct w83627hf_data *data = dev_get_drvdata(dev);
1242
1243 return sprintf(buf, "%s\n", data->name);
1244}
Julia Lawall8dfcdfc2016-12-22 13:05:11 +01001245static DEVICE_ATTR_RO(name);
Jean Delvare787c72b2007-05-08 17:22:00 +02001246
1247static int __init w83627hf_find(int sioaddr, unsigned short *addr,
1248 struct w83627hf_sio_data *sio_data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001249{
Guenter Roecke95fd512019-04-05 08:44:41 -07001250 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001251 u16 val;
1252
Andi Kleen64f50302012-08-18 10:30:05 -07001253 static __initconst char *const names[] = {
Jean Delvare787c72b2007-05-08 17:22:00 +02001254 "W83627HF",
1255 "W83627THF",
1256 "W83697HF",
1257 "W83637HF",
1258 "W83687THF",
1259 };
1260
Christian Schultec46c0e92009-12-16 21:38:29 +01001261 sio_data->sioaddr = sioaddr;
Guenter Roecke95fd512019-04-05 08:44:41 -07001262 err = superio_enter(sio_data);
1263 if (err)
1264 return err;
1265
1266 err = -ENODEV;
Jean Delvareb72656d2009-12-09 20:35:49 +01001267 val = force_id ? force_id : superio_inb(sio_data, DEVID);
Jean Delvare787c72b2007-05-08 17:22:00 +02001268 switch (val) {
1269 case W627_DEVID:
1270 sio_data->type = w83627hf;
1271 break;
1272 case W627THF_DEVID:
1273 sio_data->type = w83627thf;
1274 break;
1275 case W697_DEVID:
1276 sio_data->type = w83697hf;
1277 break;
1278 case W637_DEVID:
1279 sio_data->type = w83637hf;
1280 break;
1281 case W687THF_DEVID:
1282 sio_data->type = w83687thf;
1283 break;
Jean Delvaree142e2a2007-05-27 22:17:43 +02001284 case 0xff: /* No device at all */
1285 goto exit;
Jean Delvare787c72b2007-05-08 17:22:00 +02001286 default:
Jean Delvaree142e2a2007-05-27 22:17:43 +02001287 pr_debug(DRVNAME ": Unsupported chip (DEVID=0x%02x)\n", val);
Jean Delvared27c37c2007-05-08 17:21:59 +02001288 goto exit;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001289 }
1290
Jean Delvareb72656d2009-12-09 20:35:49 +01001291 superio_select(sio_data, W83627HF_LD_HWM);
1292 val = (superio_inb(sio_data, WINB_BASE_REG) << 8) |
1293 superio_inb(sio_data, WINB_BASE_REG + 1);
Petr Vandrovecada0c2f2005-10-07 23:11:03 +02001294 *addr = val & WINB_ALIGNMENT;
Jean Delvared27c37c2007-05-08 17:21:59 +02001295 if (*addr == 0) {
Joe Perches18de0302010-10-20 06:51:55 +00001296 pr_warn("Base address not set, skipping\n");
Jean Delvared27c37c2007-05-08 17:21:59 +02001297 goto exit;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001298 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001299
Jean Delvareb72656d2009-12-09 20:35:49 +01001300 val = superio_inb(sio_data, WINB_ACT_REG);
Jean Delvared27c37c2007-05-08 17:21:59 +02001301 if (!(val & 0x01)) {
Joe Perches18de0302010-10-20 06:51:55 +00001302 pr_warn("Enabling HWM logical device\n");
Jean Delvareb72656d2009-12-09 20:35:49 +01001303 superio_outb(sio_data, WINB_ACT_REG, val | 0x01);
Jean Delvared27c37c2007-05-08 17:21:59 +02001304 }
1305
1306 err = 0;
Jean Delvare787c72b2007-05-08 17:22:00 +02001307 pr_info(DRVNAME ": Found %s chip at %#x\n",
1308 names[sio_data->type], *addr);
Jean Delvared27c37c2007-05-08 17:21:59 +02001309
1310 exit:
Jean Delvareb72656d2009-12-09 20:35:49 +01001311 superio_exit(sio_data);
Jean Delvared27c37c2007-05-08 17:21:59 +02001312 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001313}
1314
Jim Cromie07584c72007-10-12 21:08:00 +02001315#define VIN_UNIT_ATTRS(_X_) \
1316 &sensor_dev_attr_in##_X_##_input.dev_attr.attr, \
1317 &sensor_dev_attr_in##_X_##_min.dev_attr.attr, \
Jean Delvaree3604c62008-01-03 23:00:30 +01001318 &sensor_dev_attr_in##_X_##_max.dev_attr.attr, \
1319 &sensor_dev_attr_in##_X_##_alarm.dev_attr.attr, \
1320 &sensor_dev_attr_in##_X_##_beep.dev_attr.attr
Jim Cromie07584c72007-10-12 21:08:00 +02001321
1322#define FAN_UNIT_ATTRS(_X_) \
1323 &sensor_dev_attr_fan##_X_##_input.dev_attr.attr, \
1324 &sensor_dev_attr_fan##_X_##_min.dev_attr.attr, \
Jean Delvaree3604c62008-01-03 23:00:30 +01001325 &sensor_dev_attr_fan##_X_##_div.dev_attr.attr, \
1326 &sensor_dev_attr_fan##_X_##_alarm.dev_attr.attr, \
1327 &sensor_dev_attr_fan##_X_##_beep.dev_attr.attr
Jim Cromie07584c72007-10-12 21:08:00 +02001328
1329#define TEMP_UNIT_ATTRS(_X_) \
1330 &sensor_dev_attr_temp##_X_##_input.dev_attr.attr, \
1331 &sensor_dev_attr_temp##_X_##_max.dev_attr.attr, \
1332 &sensor_dev_attr_temp##_X_##_max_hyst.dev_attr.attr, \
Jean Delvaree3604c62008-01-03 23:00:30 +01001333 &sensor_dev_attr_temp##_X_##_type.dev_attr.attr, \
1334 &sensor_dev_attr_temp##_X_##_alarm.dev_attr.attr, \
1335 &sensor_dev_attr_temp##_X_##_beep.dev_attr.attr
Jim Cromie07584c72007-10-12 21:08:00 +02001336
Mark M. Hoffmanc1685f62006-09-24 20:59:49 +02001337static struct attribute *w83627hf_attributes[] = {
1338 &dev_attr_in0_input.attr,
1339 &dev_attr_in0_min.attr,
1340 &dev_attr_in0_max.attr,
Jean Delvaree3604c62008-01-03 23:00:30 +01001341 &sensor_dev_attr_in0_alarm.dev_attr.attr,
1342 &sensor_dev_attr_in0_beep.dev_attr.attr,
Jim Cromie07584c72007-10-12 21:08:00 +02001343 VIN_UNIT_ATTRS(2),
1344 VIN_UNIT_ATTRS(3),
1345 VIN_UNIT_ATTRS(4),
1346 VIN_UNIT_ATTRS(7),
1347 VIN_UNIT_ATTRS(8),
Mark M. Hoffmanc1685f62006-09-24 20:59:49 +02001348
Jim Cromie07584c72007-10-12 21:08:00 +02001349 FAN_UNIT_ATTRS(1),
1350 FAN_UNIT_ATTRS(2),
Mark M. Hoffmanc1685f62006-09-24 20:59:49 +02001351
Jim Cromie07584c72007-10-12 21:08:00 +02001352 TEMP_UNIT_ATTRS(1),
1353 TEMP_UNIT_ATTRS(2),
Mark M. Hoffmanc1685f62006-09-24 20:59:49 +02001354
1355 &dev_attr_alarms.attr,
Jean Delvare1c138102008-01-03 23:04:55 +01001356 &sensor_dev_attr_beep_enable.dev_attr.attr,
Mark M. Hoffmanc1685f62006-09-24 20:59:49 +02001357 &dev_attr_beep_mask.attr,
1358
Jim Cromie07584c72007-10-12 21:08:00 +02001359 &sensor_dev_attr_pwm1.dev_attr.attr,
1360 &sensor_dev_attr_pwm2.dev_attr.attr,
Jean Delvare787c72b2007-05-08 17:22:00 +02001361 &dev_attr_name.attr,
Mark M. Hoffmanc1685f62006-09-24 20:59:49 +02001362 NULL
1363};
1364
1365static const struct attribute_group w83627hf_group = {
1366 .attrs = w83627hf_attributes,
1367};
1368
1369static struct attribute *w83627hf_attributes_opt[] = {
Jim Cromie07584c72007-10-12 21:08:00 +02001370 VIN_UNIT_ATTRS(1),
1371 VIN_UNIT_ATTRS(5),
1372 VIN_UNIT_ATTRS(6),
Mark M. Hoffmanc1685f62006-09-24 20:59:49 +02001373
Jim Cromie07584c72007-10-12 21:08:00 +02001374 FAN_UNIT_ATTRS(3),
1375 TEMP_UNIT_ATTRS(3),
1376 &sensor_dev_attr_pwm3.dev_attr.attr,
Mark M. Hoffmanc1685f62006-09-24 20:59:49 +02001377
Jim Cromie07584c72007-10-12 21:08:00 +02001378 &sensor_dev_attr_pwm1_freq.dev_attr.attr,
1379 &sensor_dev_attr_pwm2_freq.dev_attr.attr,
1380 &sensor_dev_attr_pwm3_freq.dev_attr.attr,
Dominik Geyera95a5ed2008-08-06 22:41:04 +02001381
1382 &sensor_dev_attr_pwm1_enable.dev_attr.attr,
1383 &sensor_dev_attr_pwm2_enable.dev_attr.attr,
1384 &sensor_dev_attr_pwm3_enable.dev_attr.attr,
1385
Mark M. Hoffmanc1685f62006-09-24 20:59:49 +02001386 NULL
1387};
1388
1389static const struct attribute_group w83627hf_group_opt = {
1390 .attrs = w83627hf_attributes_opt,
1391};
1392
Bill Pemberton6c931ae2012-11-19 13:22:35 -05001393static int w83627hf_probe(struct platform_device *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001394{
Jean Delvare787c72b2007-05-08 17:22:00 +02001395 struct device *dev = &pdev->dev;
Jingoo Hana8b3a3a2013-07-30 17:13:06 +09001396 struct w83627hf_sio_data *sio_data = dev_get_platdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001397 struct w83627hf_data *data;
Jean Delvare787c72b2007-05-08 17:22:00 +02001398 struct resource *res;
Jim Cromie2ca2fcd2007-10-14 17:20:50 -06001399 int err, i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001400
Jean Delvare787c72b2007-05-08 17:22:00 +02001401 static const char *names[] = {
1402 "w83627hf",
1403 "w83627thf",
1404 "w83697hf",
1405 "w83637hf",
1406 "w83687thf",
1407 };
1408
1409 res = platform_get_resource(pdev, IORESOURCE_IO, 0);
Guenter Roeck0cf46992012-06-02 11:47:59 -07001410 if (!devm_request_region(dev, res->start, WINB_REGION_SIZE, DRVNAME)) {
Jean Delvare787c72b2007-05-08 17:22:00 +02001411 dev_err(dev, "Failed to request region 0x%lx-0x%lx\n",
1412 (unsigned long)res->start,
1413 (unsigned long)(res->start + WINB_REGION_SIZE - 1));
Guenter Roeck0cf46992012-06-02 11:47:59 -07001414 return -EBUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001415 }
1416
Guenter Roeck0cf46992012-06-02 11:47:59 -07001417 data = devm_kzalloc(dev, sizeof(struct w83627hf_data), GFP_KERNEL);
1418 if (!data)
1419 return -ENOMEM;
1420
Jean Delvare787c72b2007-05-08 17:22:00 +02001421 data->addr = res->start;
1422 data->type = sio_data->type;
1423 data->name = names[sio_data->type];
Ingo Molnar9a61bf62006-01-18 23:19:26 +01001424 mutex_init(&data->lock);
Ingo Molnar9a61bf62006-01-18 23:19:26 +01001425 mutex_init(&data->update_lock);
Jean Delvare787c72b2007-05-08 17:22:00 +02001426 platform_set_drvdata(pdev, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001427
Linus Torvalds1da177e2005-04-16 15:20:36 -07001428 /* Initialize the chip */
Jean Delvare787c72b2007-05-08 17:22:00 +02001429 w83627hf_init_device(pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001430
1431 /* A few vars need to be filled upon startup */
Jim Cromie2ca2fcd2007-10-14 17:20:50 -06001432 for (i = 0; i <= 2; i++)
1433 data->fan_min[i] = w83627hf_read_value(
1434 data, W83627HF_REG_FAN_MIN(i));
Jean Delvarec09c5182007-10-12 21:53:07 +02001435 w83627hf_update_fan_div(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001436
Mark M. Hoffmanc1685f62006-09-24 20:59:49 +02001437 /* Register common device attributes */
Guenter Roeck27b9de32012-01-15 11:07:26 -08001438 err = sysfs_create_group(&dev->kobj, &w83627hf_group);
1439 if (err)
Guenter Roeck0cf46992012-06-02 11:47:59 -07001440 return err;
Mark M. Hoffmanc1685f62006-09-24 20:59:49 +02001441
1442 /* Register chip-specific device attributes */
Jean Delvare787c72b2007-05-08 17:22:00 +02001443 if (data->type == w83627hf || data->type == w83697hf)
Jim Cromie07584c72007-10-12 21:08:00 +02001444 if ((err = device_create_file(dev,
1445 &sensor_dev_attr_in5_input.dev_attr))
1446 || (err = device_create_file(dev,
1447 &sensor_dev_attr_in5_min.dev_attr))
1448 || (err = device_create_file(dev,
1449 &sensor_dev_attr_in5_max.dev_attr))
1450 || (err = device_create_file(dev,
Jean Delvaree3604c62008-01-03 23:00:30 +01001451 &sensor_dev_attr_in5_alarm.dev_attr))
1452 || (err = device_create_file(dev,
1453 &sensor_dev_attr_in5_beep.dev_attr))
1454 || (err = device_create_file(dev,
Jim Cromie07584c72007-10-12 21:08:00 +02001455 &sensor_dev_attr_in6_input.dev_attr))
1456 || (err = device_create_file(dev,
1457 &sensor_dev_attr_in6_min.dev_attr))
1458 || (err = device_create_file(dev,
1459 &sensor_dev_attr_in6_max.dev_attr))
1460 || (err = device_create_file(dev,
Jean Delvaree3604c62008-01-03 23:00:30 +01001461 &sensor_dev_attr_in6_alarm.dev_attr))
1462 || (err = device_create_file(dev,
1463 &sensor_dev_attr_in6_beep.dev_attr))
1464 || (err = device_create_file(dev,
Jim Cromie07584c72007-10-12 21:08:00 +02001465 &sensor_dev_attr_pwm1_freq.dev_attr))
1466 || (err = device_create_file(dev,
1467 &sensor_dev_attr_pwm2_freq.dev_attr)))
Guenter Roeck0cf46992012-06-02 11:47:59 -07001468 goto error;
Mark M. Hoffmanc1685f62006-09-24 20:59:49 +02001469
Jean Delvare787c72b2007-05-08 17:22:00 +02001470 if (data->type != w83697hf)
Jim Cromie07584c72007-10-12 21:08:00 +02001471 if ((err = device_create_file(dev,
1472 &sensor_dev_attr_in1_input.dev_attr))
1473 || (err = device_create_file(dev,
1474 &sensor_dev_attr_in1_min.dev_attr))
1475 || (err = device_create_file(dev,
1476 &sensor_dev_attr_in1_max.dev_attr))
1477 || (err = device_create_file(dev,
Jean Delvaree3604c62008-01-03 23:00:30 +01001478 &sensor_dev_attr_in1_alarm.dev_attr))
1479 || (err = device_create_file(dev,
1480 &sensor_dev_attr_in1_beep.dev_attr))
1481 || (err = device_create_file(dev,
Jim Cromie07584c72007-10-12 21:08:00 +02001482 &sensor_dev_attr_fan3_input.dev_attr))
1483 || (err = device_create_file(dev,
1484 &sensor_dev_attr_fan3_min.dev_attr))
1485 || (err = device_create_file(dev,
1486 &sensor_dev_attr_fan3_div.dev_attr))
1487 || (err = device_create_file(dev,
Jean Delvaree3604c62008-01-03 23:00:30 +01001488 &sensor_dev_attr_fan3_alarm.dev_attr))
1489 || (err = device_create_file(dev,
1490 &sensor_dev_attr_fan3_beep.dev_attr))
1491 || (err = device_create_file(dev,
Jim Cromie07584c72007-10-12 21:08:00 +02001492 &sensor_dev_attr_temp3_input.dev_attr))
1493 || (err = device_create_file(dev,
1494 &sensor_dev_attr_temp3_max.dev_attr))
1495 || (err = device_create_file(dev,
1496 &sensor_dev_attr_temp3_max_hyst.dev_attr))
1497 || (err = device_create_file(dev,
Jean Delvaree3604c62008-01-03 23:00:30 +01001498 &sensor_dev_attr_temp3_alarm.dev_attr))
1499 || (err = device_create_file(dev,
1500 &sensor_dev_attr_temp3_beep.dev_attr))
1501 || (err = device_create_file(dev,
Jim Cromie07584c72007-10-12 21:08:00 +02001502 &sensor_dev_attr_temp3_type.dev_attr)))
Guenter Roeck0cf46992012-06-02 11:47:59 -07001503 goto error;
Mark M. Hoffmanc1685f62006-09-24 20:59:49 +02001504
Jean Delvare787c72b2007-05-08 17:22:00 +02001505 if (data->type != w83697hf && data->vid != 0xff) {
Jean Delvare8a665a02007-05-08 17:21:59 +02001506 /* Convert VID to voltage based on VRM */
1507 data->vrm = vid_which_vrm();
1508
Jean Delvare787c72b2007-05-08 17:22:00 +02001509 if ((err = device_create_file(dev, &dev_attr_cpu0_vid))
1510 || (err = device_create_file(dev, &dev_attr_vrm)))
Guenter Roeck0cf46992012-06-02 11:47:59 -07001511 goto error;
Jean Delvare8a665a02007-05-08 17:21:59 +02001512 }
Mark M. Hoffmanc1685f62006-09-24 20:59:49 +02001513
Jean Delvare787c72b2007-05-08 17:22:00 +02001514 if (data->type == w83627thf || data->type == w83637hf
Guenter Roeck27b9de32012-01-15 11:07:26 -08001515 || data->type == w83687thf) {
1516 err = device_create_file(dev, &sensor_dev_attr_pwm3.dev_attr);
1517 if (err)
Guenter Roeck0cf46992012-06-02 11:47:59 -07001518 goto error;
Guenter Roeck27b9de32012-01-15 11:07:26 -08001519 }
Mark M. Hoffmanc1685f62006-09-24 20:59:49 +02001520
Carlos Olalla Martinez1550cb62007-06-09 10:11:16 -04001521 if (data->type == w83637hf || data->type == w83687thf)
Jim Cromie07584c72007-10-12 21:08:00 +02001522 if ((err = device_create_file(dev,
1523 &sensor_dev_attr_pwm1_freq.dev_attr))
1524 || (err = device_create_file(dev,
1525 &sensor_dev_attr_pwm2_freq.dev_attr))
1526 || (err = device_create_file(dev,
1527 &sensor_dev_attr_pwm3_freq.dev_attr)))
Guenter Roeck0cf46992012-06-02 11:47:59 -07001528 goto error;
Carlos Olalla Martinez1550cb62007-06-09 10:11:16 -04001529
Dominik Geyera95a5ed2008-08-06 22:41:04 +02001530 if (data->type != w83627hf)
1531 if ((err = device_create_file(dev,
1532 &sensor_dev_attr_pwm1_enable.dev_attr))
1533 || (err = device_create_file(dev,
1534 &sensor_dev_attr_pwm2_enable.dev_attr)))
Guenter Roeck0cf46992012-06-02 11:47:59 -07001535 goto error;
Dominik Geyera95a5ed2008-08-06 22:41:04 +02001536
1537 if (data->type == w83627thf || data->type == w83637hf
Guenter Roeck27b9de32012-01-15 11:07:26 -08001538 || data->type == w83687thf) {
1539 err = device_create_file(dev,
1540 &sensor_dev_attr_pwm3_enable.dev_attr);
1541 if (err)
Guenter Roeck0cf46992012-06-02 11:47:59 -07001542 goto error;
Guenter Roeck27b9de32012-01-15 11:07:26 -08001543 }
Dominik Geyera95a5ed2008-08-06 22:41:04 +02001544
Tony Jones1beeffe2007-08-20 13:46:20 -07001545 data->hwmon_dev = hwmon_device_register(dev);
1546 if (IS_ERR(data->hwmon_dev)) {
1547 err = PTR_ERR(data->hwmon_dev);
Guenter Roeck0cf46992012-06-02 11:47:59 -07001548 goto error;
Mark M. Hoffman943b0832005-07-15 21:39:18 -04001549 }
1550
Linus Torvalds1da177e2005-04-16 15:20:36 -07001551 return 0;
1552
Guenter Roeck0cf46992012-06-02 11:47:59 -07001553 error:
Jean Delvare787c72b2007-05-08 17:22:00 +02001554 sysfs_remove_group(&dev->kobj, &w83627hf_group);
1555 sysfs_remove_group(&dev->kobj, &w83627hf_group_opt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001556 return err;
1557}
1558
Bill Pemberton281dfd02012-11-19 13:25:51 -05001559static int w83627hf_remove(struct platform_device *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001560{
Jean Delvare787c72b2007-05-08 17:22:00 +02001561 struct w83627hf_data *data = platform_get_drvdata(pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001562
Tony Jones1beeffe2007-08-20 13:46:20 -07001563 hwmon_device_unregister(data->hwmon_dev);
Mark M. Hoffman943b0832005-07-15 21:39:18 -04001564
Jean Delvare787c72b2007-05-08 17:22:00 +02001565 sysfs_remove_group(&pdev->dev.kobj, &w83627hf_group);
1566 sysfs_remove_group(&pdev->dev.kobj, &w83627hf_group_opt);
Jean Delvare787c72b2007-05-08 17:22:00 +02001567
Linus Torvalds1da177e2005-04-16 15:20:36 -07001568 return 0;
1569}
1570
Jean Delvared58df9c2007-10-10 16:30:23 +02001571/* Registers 0x50-0x5f are banked */
1572static inline void w83627hf_set_bank(struct w83627hf_data *data, u16 reg)
1573{
1574 if ((reg & 0x00f0) == 0x50) {
1575 outb_p(W83781D_REG_BANK, data->addr + W83781D_ADDR_REG_OFFSET);
1576 outb_p(reg >> 8, data->addr + W83781D_DATA_REG_OFFSET);
1577 }
1578}
1579
1580/* Not strictly necessary, but play it safe for now */
1581static inline void w83627hf_reset_bank(struct w83627hf_data *data, u16 reg)
1582{
1583 if (reg & 0xff00) {
1584 outb_p(W83781D_REG_BANK, data->addr + W83781D_ADDR_REG_OFFSET);
1585 outb_p(0, data->addr + W83781D_DATA_REG_OFFSET);
1586 }
1587}
1588
Jean Delvare787c72b2007-05-08 17:22:00 +02001589static int w83627hf_read_value(struct w83627hf_data *data, u16 reg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001590{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001591 int res, word_sized;
1592
Ingo Molnar9a61bf62006-01-18 23:19:26 +01001593 mutex_lock(&data->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001594 word_sized = (((reg & 0xff00) == 0x100)
1595 || ((reg & 0xff00) == 0x200))
1596 && (((reg & 0x00ff) == 0x50)
1597 || ((reg & 0x00ff) == 0x53)
1598 || ((reg & 0x00ff) == 0x55));
Jean Delvared58df9c2007-10-10 16:30:23 +02001599 w83627hf_set_bank(data, reg);
Jean Delvare787c72b2007-05-08 17:22:00 +02001600 outb_p(reg & 0xff, data->addr + W83781D_ADDR_REG_OFFSET);
1601 res = inb_p(data->addr + W83781D_DATA_REG_OFFSET);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001602 if (word_sized) {
1603 outb_p((reg & 0xff) + 1,
Jean Delvare787c72b2007-05-08 17:22:00 +02001604 data->addr + W83781D_ADDR_REG_OFFSET);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001605 res =
Jean Delvare787c72b2007-05-08 17:22:00 +02001606 (res << 8) + inb_p(data->addr +
Linus Torvalds1da177e2005-04-16 15:20:36 -07001607 W83781D_DATA_REG_OFFSET);
1608 }
Jean Delvared58df9c2007-10-10 16:30:23 +02001609 w83627hf_reset_bank(data, reg);
Ingo Molnar9a61bf62006-01-18 23:19:26 +01001610 mutex_unlock(&data->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001611 return res;
1612}
1613
Bill Pemberton6c931ae2012-11-19 13:22:35 -05001614static int w83627thf_read_gpio5(struct platform_device *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001615{
Jingoo Hana8b3a3a2013-07-30 17:13:06 +09001616 struct w83627hf_sio_data *sio_data = dev_get_platdata(&pdev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001617 int res = 0xff, sel;
1618
Guenter Roecke95fd512019-04-05 08:44:41 -07001619 if (superio_enter(sio_data)) {
1620 /*
1621 * Some other driver reserved the address space for itself.
1622 * We don't want to fail driver instantiation because of that,
1623 * so display a warning and keep going.
1624 */
1625 dev_warn(&pdev->dev,
1626 "Can not read VID data: Failed to enable SuperIO access\n");
1627 return res;
1628 }
1629
Jean Delvareb72656d2009-12-09 20:35:49 +01001630 superio_select(sio_data, W83627HF_LD_GPIO5);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001631
Guenter Roecke95fd512019-04-05 08:44:41 -07001632 res = 0xff;
1633
Linus Torvalds1da177e2005-04-16 15:20:36 -07001634 /* Make sure these GPIO pins are enabled */
Jean Delvareb72656d2009-12-09 20:35:49 +01001635 if (!(superio_inb(sio_data, W83627THF_GPIO5_EN) & (1<<3))) {
Jean Delvare787c72b2007-05-08 17:22:00 +02001636 dev_dbg(&pdev->dev, "GPIO5 disabled, no VID function\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001637 goto exit;
1638 }
1639
Guenter Roeck27b9de32012-01-15 11:07:26 -08001640 /*
1641 * Make sure the pins are configured for input
1642 * There must be at least five (VRM 9), and possibly 6 (VRM 10)
1643 */
Jean Delvareb72656d2009-12-09 20:35:49 +01001644 sel = superio_inb(sio_data, W83627THF_GPIO5_IOSR) & 0x3f;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001645 if ((sel & 0x1f) != 0x1f) {
Jean Delvare787c72b2007-05-08 17:22:00 +02001646 dev_dbg(&pdev->dev, "GPIO5 not configured for VID "
Linus Torvalds1da177e2005-04-16 15:20:36 -07001647 "function\n");
1648 goto exit;
1649 }
1650
Jean Delvare787c72b2007-05-08 17:22:00 +02001651 dev_info(&pdev->dev, "Reading VID from GPIO5\n");
Jean Delvareb72656d2009-12-09 20:35:49 +01001652 res = superio_inb(sio_data, W83627THF_GPIO5_DR) & sel;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001653
1654exit:
Jean Delvareb72656d2009-12-09 20:35:49 +01001655 superio_exit(sio_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001656 return res;
1657}
1658
Bill Pemberton6c931ae2012-11-19 13:22:35 -05001659static int w83687thf_read_vid(struct platform_device *pdev)
Jean Delvarec2db6ce2006-01-18 23:22:12 +01001660{
Jingoo Hana8b3a3a2013-07-30 17:13:06 +09001661 struct w83627hf_sio_data *sio_data = dev_get_platdata(&pdev->dev);
Jean Delvarec2db6ce2006-01-18 23:22:12 +01001662 int res = 0xff;
1663
Guenter Roecke95fd512019-04-05 08:44:41 -07001664 if (superio_enter(sio_data)) {
1665 /*
1666 * Some other driver reserved the address space for itself.
1667 * We don't want to fail driver instantiation because of that,
1668 * so display a warning and keep going.
1669 */
1670 dev_warn(&pdev->dev,
1671 "Can not read VID data: Failed to enable SuperIO access\n");
1672 return res;
1673 }
1674
Jean Delvareb72656d2009-12-09 20:35:49 +01001675 superio_select(sio_data, W83627HF_LD_HWM);
Jean Delvarec2db6ce2006-01-18 23:22:12 +01001676
1677 /* Make sure these GPIO pins are enabled */
Jean Delvareb72656d2009-12-09 20:35:49 +01001678 if (!(superio_inb(sio_data, W83687THF_VID_EN) & (1 << 2))) {
Jean Delvare787c72b2007-05-08 17:22:00 +02001679 dev_dbg(&pdev->dev, "VID disabled, no VID function\n");
Jean Delvarec2db6ce2006-01-18 23:22:12 +01001680 goto exit;
1681 }
1682
1683 /* Make sure the pins are configured for input */
Jean Delvareb72656d2009-12-09 20:35:49 +01001684 if (!(superio_inb(sio_data, W83687THF_VID_CFG) & (1 << 4))) {
Jean Delvare787c72b2007-05-08 17:22:00 +02001685 dev_dbg(&pdev->dev, "VID configured as output, "
Jean Delvarec2db6ce2006-01-18 23:22:12 +01001686 "no VID function\n");
1687 goto exit;
1688 }
1689
Jean Delvareb72656d2009-12-09 20:35:49 +01001690 res = superio_inb(sio_data, W83687THF_VID_DATA) & 0x3f;
Jean Delvarec2db6ce2006-01-18 23:22:12 +01001691
1692exit:
Jean Delvareb72656d2009-12-09 20:35:49 +01001693 superio_exit(sio_data);
Jean Delvarec2db6ce2006-01-18 23:22:12 +01001694 return res;
1695}
1696
Jean Delvare787c72b2007-05-08 17:22:00 +02001697static int w83627hf_write_value(struct w83627hf_data *data, u16 reg, u16 value)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001698{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001699 int word_sized;
1700
Ingo Molnar9a61bf62006-01-18 23:19:26 +01001701 mutex_lock(&data->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001702 word_sized = (((reg & 0xff00) == 0x100)
1703 || ((reg & 0xff00) == 0x200))
1704 && (((reg & 0x00ff) == 0x53)
1705 || ((reg & 0x00ff) == 0x55));
Jean Delvared58df9c2007-10-10 16:30:23 +02001706 w83627hf_set_bank(data, reg);
Jean Delvare787c72b2007-05-08 17:22:00 +02001707 outb_p(reg & 0xff, data->addr + W83781D_ADDR_REG_OFFSET);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001708 if (word_sized) {
1709 outb_p(value >> 8,
Jean Delvare787c72b2007-05-08 17:22:00 +02001710 data->addr + W83781D_DATA_REG_OFFSET);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001711 outb_p((reg & 0xff) + 1,
Jean Delvare787c72b2007-05-08 17:22:00 +02001712 data->addr + W83781D_ADDR_REG_OFFSET);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001713 }
1714 outb_p(value & 0xff,
Jean Delvare787c72b2007-05-08 17:22:00 +02001715 data->addr + W83781D_DATA_REG_OFFSET);
Jean Delvared58df9c2007-10-10 16:30:23 +02001716 w83627hf_reset_bank(data, reg);
Ingo Molnar9a61bf62006-01-18 23:19:26 +01001717 mutex_unlock(&data->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001718 return 0;
1719}
1720
Bill Pemberton6c931ae2012-11-19 13:22:35 -05001721static void w83627hf_init_device(struct platform_device *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001722{
Jean Delvare787c72b2007-05-08 17:22:00 +02001723 struct w83627hf_data *data = platform_get_drvdata(pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001724 int i;
Jean Delvared27c37c2007-05-08 17:21:59 +02001725 enum chips type = data->type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001726 u8 tmp;
1727
Linus Torvalds1da177e2005-04-16 15:20:36 -07001728 /* Minimize conflicts with other winbond i2c-only clients... */
1729 /* disable i2c subclients... how to disable main i2c client?? */
1730 /* force i2c address to relatively uncommon address */
Jean Delvare8f3c7c52012-12-19 22:17:00 +01001731 if (type == w83627hf) {
1732 w83627hf_write_value(data, W83781D_REG_I2C_SUBADDR, 0x89);
1733 w83627hf_write_value(data, W83781D_REG_I2C_ADDR, force_i2c);
1734 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001735
1736 /* Read VID only once */
Jean Delvared27c37c2007-05-08 17:21:59 +02001737 if (type == w83627hf || type == w83637hf) {
Jean Delvare787c72b2007-05-08 17:22:00 +02001738 int lo = w83627hf_read_value(data, W83781D_REG_VID_FANDIV);
1739 int hi = w83627hf_read_value(data, W83781D_REG_CHIPID);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001740 data->vid = (lo & 0x0f) | ((hi & 0x01) << 4);
Jean Delvared27c37c2007-05-08 17:21:59 +02001741 } else if (type == w83627thf) {
Jean Delvare787c72b2007-05-08 17:22:00 +02001742 data->vid = w83627thf_read_gpio5(pdev);
Jean Delvared27c37c2007-05-08 17:21:59 +02001743 } else if (type == w83687thf) {
Jean Delvare787c72b2007-05-08 17:22:00 +02001744 data->vid = w83687thf_read_vid(pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001745 }
1746
1747 /* Read VRM & OVT Config only once */
Jean Delvared27c37c2007-05-08 17:21:59 +02001748 if (type == w83627thf || type == w83637hf || type == w83687thf) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001749 data->vrm_ovt =
Jean Delvare787c72b2007-05-08 17:22:00 +02001750 w83627hf_read_value(data, W83627THF_REG_VRM_OVT_CFG);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001751 }
1752
Jean Delvare787c72b2007-05-08 17:22:00 +02001753 tmp = w83627hf_read_value(data, W83781D_REG_SCFG1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001754 for (i = 1; i <= 3; i++) {
1755 if (!(tmp & BIT_SCFG1[i - 1])) {
Jean Delvareb26f9332007-08-16 14:30:01 +02001756 data->sens[i - 1] = 4;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001757 } else {
1758 if (w83627hf_read_value
Jean Delvare787c72b2007-05-08 17:22:00 +02001759 (data,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001760 W83781D_REG_SCFG2) & BIT_SCFG2[i - 1])
1761 data->sens[i - 1] = 1;
1762 else
1763 data->sens[i - 1] = 2;
1764 }
1765 if ((type == w83697hf) && (i == 2))
1766 break;
1767 }
1768
1769 if(init) {
1770 /* Enable temp2 */
Jim Cromiedf48ed82007-10-14 17:10:52 -06001771 tmp = w83627hf_read_value(data, W83627HF_REG_TEMP2_CONFIG);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001772 if (tmp & 0x01) {
Jean Delvare787c72b2007-05-08 17:22:00 +02001773 dev_warn(&pdev->dev, "Enabling temp2, readings "
Linus Torvalds1da177e2005-04-16 15:20:36 -07001774 "might not make sense\n");
Jim Cromiedf48ed82007-10-14 17:10:52 -06001775 w83627hf_write_value(data, W83627HF_REG_TEMP2_CONFIG,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001776 tmp & 0xfe);
1777 }
1778
1779 /* Enable temp3 */
1780 if (type != w83697hf) {
Jean Delvare787c72b2007-05-08 17:22:00 +02001781 tmp = w83627hf_read_value(data,
Jim Cromiedf48ed82007-10-14 17:10:52 -06001782 W83627HF_REG_TEMP3_CONFIG);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001783 if (tmp & 0x01) {
Jean Delvare787c72b2007-05-08 17:22:00 +02001784 dev_warn(&pdev->dev, "Enabling temp3, "
Linus Torvalds1da177e2005-04-16 15:20:36 -07001785 "readings might not make sense\n");
Jean Delvare787c72b2007-05-08 17:22:00 +02001786 w83627hf_write_value(data,
Jim Cromiedf48ed82007-10-14 17:10:52 -06001787 W83627HF_REG_TEMP3_CONFIG, tmp & 0xfe);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001788 }
1789 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001790 }
1791
1792 /* Start monitoring */
Jean Delvare787c72b2007-05-08 17:22:00 +02001793 w83627hf_write_value(data, W83781D_REG_CONFIG,
1794 (w83627hf_read_value(data,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001795 W83781D_REG_CONFIG) & 0xf7)
1796 | 0x01);
Jean Delvareef878b12008-01-03 22:54:13 +01001797
1798 /* Enable VBAT monitoring if needed */
1799 tmp = w83627hf_read_value(data, W83781D_REG_VBAT);
1800 if (!(tmp & 0x01))
1801 w83627hf_write_value(data, W83781D_REG_VBAT, tmp | 0x01);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001802}
1803
Jean Delvarec09c5182007-10-12 21:53:07 +02001804static void w83627hf_update_fan_div(struct w83627hf_data *data)
1805{
1806 int reg;
1807
1808 reg = w83627hf_read_value(data, W83781D_REG_VID_FANDIV);
1809 data->fan_div[0] = (reg >> 4) & 0x03;
1810 data->fan_div[1] = (reg >> 6) & 0x03;
1811 if (data->type != w83697hf) {
1812 data->fan_div[2] = (w83627hf_read_value(data,
1813 W83781D_REG_PIN) >> 6) & 0x03;
1814 }
1815 reg = w83627hf_read_value(data, W83781D_REG_VBAT);
1816 data->fan_div[0] |= (reg >> 3) & 0x04;
1817 data->fan_div[1] |= (reg >> 4) & 0x04;
1818 if (data->type != w83697hf)
1819 data->fan_div[2] |= (reg >> 5) & 0x04;
1820}
1821
Linus Torvalds1da177e2005-04-16 15:20:36 -07001822static struct w83627hf_data *w83627hf_update_device(struct device *dev)
1823{
Jean Delvare787c72b2007-05-08 17:22:00 +02001824 struct w83627hf_data *data = dev_get_drvdata(dev);
Jim Cromiedf48ed82007-10-14 17:10:52 -06001825 int i, num_temps = (data->type == w83697hf) ? 2 : 3;
Dominik Geyera95a5ed2008-08-06 22:41:04 +02001826 int num_pwms = (data->type == w83697hf) ? 2 : 3;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001827
Ingo Molnar9a61bf62006-01-18 23:19:26 +01001828 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001829
1830 if (time_after(jiffies, data->last_updated + HZ + HZ / 2)
1831 || !data->valid) {
1832 for (i = 0; i <= 8; i++) {
1833 /* skip missing sensors */
1834 if (((data->type == w83697hf) && (i == 1)) ||
Jean Delvarec2db6ce2006-01-18 23:22:12 +01001835 ((data->type != w83627hf && data->type != w83697hf)
Yuan Mu4a1c44472005-11-07 22:19:04 +01001836 && (i == 5 || i == 6)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001837 continue;
1838 data->in[i] =
Jean Delvare787c72b2007-05-08 17:22:00 +02001839 w83627hf_read_value(data, W83781D_REG_IN(i));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001840 data->in_min[i] =
Jean Delvare787c72b2007-05-08 17:22:00 +02001841 w83627hf_read_value(data,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001842 W83781D_REG_IN_MIN(i));
1843 data->in_max[i] =
Jean Delvare787c72b2007-05-08 17:22:00 +02001844 w83627hf_read_value(data,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001845 W83781D_REG_IN_MAX(i));
1846 }
Jim Cromie2ca2fcd2007-10-14 17:20:50 -06001847 for (i = 0; i <= 2; i++) {
1848 data->fan[i] =
1849 w83627hf_read_value(data, W83627HF_REG_FAN(i));
1850 data->fan_min[i] =
Jean Delvare787c72b2007-05-08 17:22:00 +02001851 w83627hf_read_value(data,
Jim Cromie2ca2fcd2007-10-14 17:20:50 -06001852 W83627HF_REG_FAN_MIN(i));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001853 }
Jim Cromie07584c72007-10-12 21:08:00 +02001854 for (i = 0; i <= 2; i++) {
Jean Delvare787c72b2007-05-08 17:22:00 +02001855 u8 tmp = w83627hf_read_value(data,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001856 W836X7HF_REG_PWM(data->type, i));
1857 /* bits 0-3 are reserved in 627THF */
1858 if (data->type == w83627thf)
1859 tmp &= 0xf0;
Jim Cromie07584c72007-10-12 21:08:00 +02001860 data->pwm[i] = tmp;
1861 if (i == 1 &&
1862 (data->type == w83627hf || data->type == w83697hf))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001863 break;
1864 }
Carlos Olalla Martinez1550cb62007-06-09 10:11:16 -04001865 if (data->type == w83627hf) {
1866 u8 tmp = w83627hf_read_value(data,
1867 W83627HF_REG_PWM_FREQ);
1868 data->pwm_freq[0] = tmp & 0x07;
1869 data->pwm_freq[1] = (tmp >> 4) & 0x07;
1870 } else if (data->type != w83627thf) {
1871 for (i = 1; i <= 3; i++) {
1872 data->pwm_freq[i - 1] =
1873 w83627hf_read_value(data,
1874 W83637HF_REG_PWM_FREQ[i - 1]);
1875 if (i == 2 && (data->type == w83697hf))
1876 break;
1877 }
1878 }
Dominik Geyera95a5ed2008-08-06 22:41:04 +02001879 if (data->type != w83627hf) {
1880 for (i = 0; i < num_pwms; i++) {
1881 u8 tmp = w83627hf_read_value(data,
1882 W83627THF_REG_PWM_ENABLE[i]);
1883 data->pwm_enable[i] =
1884 ((tmp >> W83627THF_PWM_ENABLE_SHIFT[i])
1885 & 0x03) + 1;
1886 }
1887 }
Jim Cromiedf48ed82007-10-14 17:10:52 -06001888 for (i = 0; i < num_temps; i++) {
1889 data->temp[i] = w83627hf_read_value(
1890 data, w83627hf_reg_temp[i]);
1891 data->temp_max[i] = w83627hf_read_value(
1892 data, w83627hf_reg_temp_over[i]);
1893 data->temp_max_hyst[i] = w83627hf_read_value(
1894 data, w83627hf_reg_temp_hyst[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001895 }
1896
Jean Delvarec09c5182007-10-12 21:53:07 +02001897 w83627hf_update_fan_div(data);
1898
Linus Torvalds1da177e2005-04-16 15:20:36 -07001899 data->alarms =
Jean Delvare787c72b2007-05-08 17:22:00 +02001900 w83627hf_read_value(data, W83781D_REG_ALARM1) |
1901 (w83627hf_read_value(data, W83781D_REG_ALARM2) << 8) |
1902 (w83627hf_read_value(data, W83781D_REG_ALARM3) << 16);
1903 i = w83627hf_read_value(data, W83781D_REG_BEEP_INTS2);
Jean Delvare1c138102008-01-03 23:04:55 +01001904 data->beep_mask = (i << 8) |
Jean Delvare787c72b2007-05-08 17:22:00 +02001905 w83627hf_read_value(data, W83781D_REG_BEEP_INTS1) |
1906 w83627hf_read_value(data, W83781D_REG_BEEP_INTS3) << 16;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001907 data->last_updated = jiffies;
Paul Fertser952a11c2021-09-24 22:52:02 +03001908 data->valid = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001909 }
1910
Ingo Molnar9a61bf62006-01-18 23:19:26 +01001911 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001912
1913 return data;
1914}
1915
Jean Delvare787c72b2007-05-08 17:22:00 +02001916static int __init w83627hf_device_add(unsigned short address,
1917 const struct w83627hf_sio_data *sio_data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001918{
Jean Delvare787c72b2007-05-08 17:22:00 +02001919 struct resource res = {
1920 .start = address + WINB_REGION_OFFSET,
1921 .end = address + WINB_REGION_OFFSET + WINB_REGION_SIZE - 1,
1922 .name = DRVNAME,
1923 .flags = IORESOURCE_IO,
1924 };
1925 int err;
1926
Jean Delvareb9acb642009-01-07 16:37:35 +01001927 err = acpi_check_resource_conflict(&res);
1928 if (err)
1929 goto exit;
1930
Jean Delvare787c72b2007-05-08 17:22:00 +02001931 pdev = platform_device_alloc(DRVNAME, address);
1932 if (!pdev) {
1933 err = -ENOMEM;
Joe Perches18de0302010-10-20 06:51:55 +00001934 pr_err("Device allocation failed\n");
Jean Delvare787c72b2007-05-08 17:22:00 +02001935 goto exit;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001936 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001937
Jean Delvare787c72b2007-05-08 17:22:00 +02001938 err = platform_device_add_resources(pdev, &res, 1);
1939 if (err) {
Joe Perches18de0302010-10-20 06:51:55 +00001940 pr_err("Device resource addition failed (%d)\n", err);
Jean Delvare787c72b2007-05-08 17:22:00 +02001941 goto exit_device_put;
1942 }
1943
Jean Delvare2df6d812007-06-09 10:11:16 -04001944 err = platform_device_add_data(pdev, sio_data,
1945 sizeof(struct w83627hf_sio_data));
1946 if (err) {
Joe Perches18de0302010-10-20 06:51:55 +00001947 pr_err("Platform data allocation failed\n");
Jean Delvare787c72b2007-05-08 17:22:00 +02001948 goto exit_device_put;
1949 }
Jean Delvare787c72b2007-05-08 17:22:00 +02001950
1951 err = platform_device_add(pdev);
1952 if (err) {
Joe Perches18de0302010-10-20 06:51:55 +00001953 pr_err("Device addition failed (%d)\n", err);
Jean Delvare787c72b2007-05-08 17:22:00 +02001954 goto exit_device_put;
1955 }
1956
1957 return 0;
1958
1959exit_device_put:
1960 platform_device_put(pdev);
1961exit:
1962 return err;
1963}
1964
1965static int __init sensors_w83627hf_init(void)
1966{
1967 int err;
1968 unsigned short address;
1969 struct w83627hf_sio_data sio_data;
1970
1971 if (w83627hf_find(0x2e, &address, &sio_data)
1972 && w83627hf_find(0x4e, &address, &sio_data))
1973 return -ENODEV;
1974
1975 err = platform_driver_register(&w83627hf_driver);
1976 if (err)
1977 goto exit;
1978
1979 /* Sets global pdev as a side effect */
1980 err = w83627hf_device_add(address, &sio_data);
1981 if (err)
1982 goto exit_driver;
1983
1984 return 0;
1985
1986exit_driver:
1987 platform_driver_unregister(&w83627hf_driver);
1988exit:
1989 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001990}
1991
1992static void __exit sensors_w83627hf_exit(void)
1993{
Jean Delvare787c72b2007-05-08 17:22:00 +02001994 platform_device_unregister(pdev);
1995 platform_driver_unregister(&w83627hf_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001996}
1997
1998MODULE_AUTHOR("Frodo Looijaard <frodol@dds.nl>, "
1999 "Philip Edelbrock <phil@netroedge.com>, "
2000 "and Mark Studebaker <mdsxyz123@yahoo.com>");
2001MODULE_DESCRIPTION("W83627HF driver");
2002MODULE_LICENSE("GPL");
2003
2004module_init(sensors_w83627hf_init);
2005module_exit(sensors_w83627hf_exit);