[PATCH] hwmon: f71805f semaphore to mutex conversions
[linux-2.6-block.git] / drivers / hwmon / w83627hf.c
CommitLineData
1da177e4
LT
1/*
2 w83627hf.c - Part of lm_sensors, Linux kernel modules for hardware
3 monitoring
4 Copyright (c) 1998 - 2003 Frodo Looijaard <frodol@dds.nl>,
5 Philip Edelbrock <phil@netroedge.com>,
6 and Mark Studebaker <mdsxyz123@yahoo.com>
7 Ported to 2.6 by Bernhard C. Schrenk <clemy@clemy.org>
8
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 2 of the License, or
12 (at your option) any later version.
13
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
18
19 You should have received a copy of the GNU General Public License
20 along with this program; if not, write to the Free Software
21 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22*/
23
24/*
25 Supports following chips:
26
27 Chip #vin #fanin #pwm #temp wchipid vendid i2c ISA
28 w83627hf 9 3 2 3 0x20 0x5ca3 no yes(LPC)
29 w83627thf 7 3 3 3 0x90 0x5ca3 no yes(LPC)
30 w83637hf 7 3 3 3 0x80 0x5ca3 no yes(LPC)
31 w83697hf 8 2 2 2 0x60 0x5ca3 no yes(LPC)
32
33 For other winbond chips, and for i2c support in the above chips,
34 use w83781d.c.
35
36 Note: automatic ("cruise") fan control for 697, 637 & 627thf not
37 supported yet.
38*/
39
40#include <linux/module.h>
41#include <linux/init.h>
42#include <linux/slab.h>
43#include <linux/jiffies.h>
44#include <linux/i2c.h>
fde09509 45#include <linux/i2c-isa.h>
943b0830 46#include <linux/hwmon.h>
303760b4 47#include <linux/hwmon-vid.h>
943b0830 48#include <linux/err.h>
9a61bf63 49#include <linux/mutex.h>
1da177e4
LT
50#include <asm/io.h>
51#include "lm75.h"
52
53static u16 force_addr;
54module_param(force_addr, ushort, 0);
55MODULE_PARM_DESC(force_addr,
56 "Initialize the base address of the sensors");
57static u8 force_i2c = 0x1f;
58module_param(force_i2c, byte, 0);
59MODULE_PARM_DESC(force_i2c,
60 "Initialize the i2c address of the sensors");
61
2d8672c5
JD
62/* The actual ISA address is read from Super-I/O configuration space */
63static unsigned short address;
1da177e4
LT
64
65/* Insmod parameters */
2d8672c5 66enum chips { any_chip, w83627hf, w83627thf, w83697hf, w83637hf };
1da177e4 67
2251cf1a
JD
68static int reset;
69module_param(reset, bool, 0);
70MODULE_PARM_DESC(reset, "Set to one to reset chip on load");
71
1da177e4
LT
72static int init = 1;
73module_param(init, bool, 0);
74MODULE_PARM_DESC(init, "Set to zero to bypass chip initialization");
75
76/* modified from kernel/include/traps.c */
77static int REG; /* The register to read/write */
78#define DEV 0x07 /* Register: Logical device select */
79static int VAL; /* The value to read/write */
80
81/* logical device numbers for superio_select (below) */
82#define W83627HF_LD_FDC 0x00
83#define W83627HF_LD_PRT 0x01
84#define W83627HF_LD_UART1 0x02
85#define W83627HF_LD_UART2 0x03
86#define W83627HF_LD_KBC 0x05
87#define W83627HF_LD_CIR 0x06 /* w83627hf only */
88#define W83627HF_LD_GAME 0x07
89#define W83627HF_LD_MIDI 0x07
90#define W83627HF_LD_GPIO1 0x07
91#define W83627HF_LD_GPIO5 0x07 /* w83627thf only */
92#define W83627HF_LD_GPIO2 0x08
93#define W83627HF_LD_GPIO3 0x09
94#define W83627HF_LD_GPIO4 0x09 /* w83627thf only */
95#define W83627HF_LD_ACPI 0x0a
96#define W83627HF_LD_HWM 0x0b
97
98#define DEVID 0x20 /* Register: Device ID */
99
100#define W83627THF_GPIO5_EN 0x30 /* w83627thf only */
101#define W83627THF_GPIO5_IOSR 0xf3 /* w83627thf only */
102#define W83627THF_GPIO5_DR 0xf4 /* w83627thf only */
103
104static inline void
105superio_outb(int reg, int val)
106{
107 outb(reg, REG);
108 outb(val, VAL);
109}
110
111static inline int
112superio_inb(int reg)
113{
114 outb(reg, REG);
115 return inb(VAL);
116}
117
118static inline void
119superio_select(int ld)
120{
121 outb(DEV, REG);
122 outb(ld, VAL);
123}
124
125static inline void
126superio_enter(void)
127{
128 outb(0x87, REG);
129 outb(0x87, REG);
130}
131
132static inline void
133superio_exit(void)
134{
135 outb(0xAA, REG);
136}
137
138#define W627_DEVID 0x52
139#define W627THF_DEVID 0x82
140#define W697_DEVID 0x60
141#define W637_DEVID 0x70
142#define WINB_ACT_REG 0x30
143#define WINB_BASE_REG 0x60
144/* Constants specified below */
145
ada0c2f8
PV
146/* Alignment of the base address */
147#define WINB_ALIGNMENT ~7
1da177e4 148
ada0c2f8
PV
149/* Offset & size of I/O region we are interested in */
150#define WINB_REGION_OFFSET 5
151#define WINB_REGION_SIZE 2
152
153/* Where are the sensors address/data registers relative to the base address */
1da177e4
LT
154#define W83781D_ADDR_REG_OFFSET 5
155#define W83781D_DATA_REG_OFFSET 6
156
157/* The W83781D registers */
158/* The W83782D registers for nr=7,8 are in bank 5 */
159#define W83781D_REG_IN_MAX(nr) ((nr < 7) ? (0x2b + (nr) * 2) : \
160 (0x554 + (((nr) - 7) * 2)))
161#define W83781D_REG_IN_MIN(nr) ((nr < 7) ? (0x2c + (nr) * 2) : \
162 (0x555 + (((nr) - 7) * 2)))
163#define W83781D_REG_IN(nr) ((nr < 7) ? (0x20 + (nr)) : \
164 (0x550 + (nr) - 7))
165
166#define W83781D_REG_FAN_MIN(nr) (0x3a + (nr))
167#define W83781D_REG_FAN(nr) (0x27 + (nr))
168
169#define W83781D_REG_TEMP2_CONFIG 0x152
170#define W83781D_REG_TEMP3_CONFIG 0x252
171#define W83781D_REG_TEMP(nr) ((nr == 3) ? (0x0250) : \
172 ((nr == 2) ? (0x0150) : \
173 (0x27)))
174#define W83781D_REG_TEMP_HYST(nr) ((nr == 3) ? (0x253) : \
175 ((nr == 2) ? (0x153) : \
176 (0x3A)))
177#define W83781D_REG_TEMP_OVER(nr) ((nr == 3) ? (0x255) : \
178 ((nr == 2) ? (0x155) : \
179 (0x39)))
180
181#define W83781D_REG_BANK 0x4E
182
183#define W83781D_REG_CONFIG 0x40
4a1c4447
YM
184#define W83781D_REG_ALARM1 0x459
185#define W83781D_REG_ALARM2 0x45A
186#define W83781D_REG_ALARM3 0x45B
1da177e4 187
1da177e4
LT
188#define W83781D_REG_BEEP_CONFIG 0x4D
189#define W83781D_REG_BEEP_INTS1 0x56
190#define W83781D_REG_BEEP_INTS2 0x57
191#define W83781D_REG_BEEP_INTS3 0x453
192
193#define W83781D_REG_VID_FANDIV 0x47
194
195#define W83781D_REG_CHIPID 0x49
196#define W83781D_REG_WCHIPID 0x58
197#define W83781D_REG_CHIPMAN 0x4F
198#define W83781D_REG_PIN 0x4B
199
200#define W83781D_REG_VBAT 0x5D
201
202#define W83627HF_REG_PWM1 0x5A
203#define W83627HF_REG_PWM2 0x5B
1da177e4
LT
204
205#define W83627THF_REG_PWM1 0x01 /* 697HF and 637HF too */
206#define W83627THF_REG_PWM2 0x03 /* 697HF and 637HF too */
207#define W83627THF_REG_PWM3 0x11 /* 637HF too */
208
209#define W83627THF_REG_VRM_OVT_CFG 0x18 /* 637HF too */
210
211static const u8 regpwm_627hf[] = { W83627HF_REG_PWM1, W83627HF_REG_PWM2 };
212static const u8 regpwm[] = { W83627THF_REG_PWM1, W83627THF_REG_PWM2,
213 W83627THF_REG_PWM3 };
214#define W836X7HF_REG_PWM(type, nr) (((type) == w83627hf) ? \
215 regpwm_627hf[(nr) - 1] : regpwm[(nr) - 1])
216
217#define W83781D_REG_I2C_ADDR 0x48
218#define W83781D_REG_I2C_SUBADDR 0x4A
219
220/* Sensor selection */
221#define W83781D_REG_SCFG1 0x5D
222static const u8 BIT_SCFG1[] = { 0x02, 0x04, 0x08 };
223#define W83781D_REG_SCFG2 0x59
224static const u8 BIT_SCFG2[] = { 0x10, 0x20, 0x40 };
225#define W83781D_DEFAULT_BETA 3435
226
227/* Conversions. Limit checking is only done on the TO_REG
228 variants. Note that you should be a bit careful with which arguments
229 these macros are called: arguments may be evaluated more than once.
230 Fixing this is just not worth it. */
231#define IN_TO_REG(val) (SENSORS_LIMIT((((val) + 8)/16),0,255))
232#define IN_FROM_REG(val) ((val) * 16)
233
234static inline u8 FAN_TO_REG(long rpm, int div)
235{
236 if (rpm == 0)
237 return 255;
238 rpm = SENSORS_LIMIT(rpm, 1, 1000000);
239 return SENSORS_LIMIT((1350000 + rpm * div / 2) / (rpm * div), 1,
240 254);
241}
242
243#define TEMP_MIN (-128000)
244#define TEMP_MAX ( 127000)
245
246/* TEMP: 0.001C/bit (-128C to +127C)
247 REG: 1C/bit, two's complement */
248static u8 TEMP_TO_REG(int temp)
249{
250 int ntemp = SENSORS_LIMIT(temp, TEMP_MIN, TEMP_MAX);
251 ntemp += (ntemp<0 ? -500 : 500);
252 return (u8)(ntemp / 1000);
253}
254
255static int TEMP_FROM_REG(u8 reg)
256{
257 return (s8)reg * 1000;
258}
259
260#define FAN_FROM_REG(val,div) ((val)==0?-1:(val)==255?0:1350000/((val)*(div)))
261
262#define PWM_TO_REG(val) (SENSORS_LIMIT((val),0,255))
263
264#define BEEP_MASK_FROM_REG(val) (val)
265#define BEEP_MASK_TO_REG(val) ((val) & 0xffffff)
266#define BEEP_ENABLE_TO_REG(val) ((val)?1:0)
267#define BEEP_ENABLE_FROM_REG(val) ((val)?1:0)
268
269#define DIV_FROM_REG(val) (1 << (val))
270
271static inline u8 DIV_TO_REG(long val)
272{
273 int i;
274 val = SENSORS_LIMIT(val, 1, 128) >> 1;
abc01922 275 for (i = 0; i < 7; i++) {
1da177e4
LT
276 if (val == 0)
277 break;
278 val >>= 1;
279 }
280 return ((u8) i);
281}
282
283/* For each registered chip, we need to keep some data in memory. That
284 data is pointed to by w83627hf_list[NR]->data. The structure itself is
285 dynamically allocated, at the same time when a new client is allocated. */
286struct w83627hf_data {
287 struct i2c_client client;
943b0830 288 struct class_device *class_dev;
9a61bf63 289 struct mutex lock;
1da177e4
LT
290 enum chips type;
291
9a61bf63 292 struct mutex update_lock;
1da177e4
LT
293 char valid; /* !=0 if following fields are valid */
294 unsigned long last_updated; /* In jiffies */
295
296 struct i2c_client *lm75; /* for secondary I2C addresses */
297 /* pointer to array of 2 subclients */
298
299 u8 in[9]; /* Register value */
300 u8 in_max[9]; /* Register value */
301 u8 in_min[9]; /* Register value */
302 u8 fan[3]; /* Register value */
303 u8 fan_min[3]; /* Register value */
304 u8 temp;
305 u8 temp_max; /* Register value */
306 u8 temp_max_hyst; /* Register value */
307 u16 temp_add[2]; /* Register value */
308 u16 temp_max_add[2]; /* Register value */
309 u16 temp_max_hyst_add[2]; /* Register value */
310 u8 fan_div[3]; /* Register encoding, shifted right */
311 u8 vid; /* Register encoding, combined */
312 u32 alarms; /* Register encoding, combined */
313 u32 beep_mask; /* Register encoding, combined */
314 u8 beep_enable; /* Boolean */
315 u8 pwm[3]; /* Register value */
316 u16 sens[3]; /* 782D/783S only.
317 1 = pentium diode; 2 = 3904 diode;
318 3000-5000 = thermistor beta.
319 Default = 3435.
320 Other Betas unimplemented */
321 u8 vrm;
322 u8 vrm_ovt; /* Register value, 627thf & 637hf only */
323};
324
325
2d8672c5 326static int w83627hf_detect(struct i2c_adapter *adapter);
1da177e4
LT
327static int w83627hf_detach_client(struct i2c_client *client);
328
329static int w83627hf_read_value(struct i2c_client *client, u16 register);
330static int w83627hf_write_value(struct i2c_client *client, u16 register,
331 u16 value);
332static struct w83627hf_data *w83627hf_update_device(struct device *dev);
333static void w83627hf_init_client(struct i2c_client *client);
334
335static struct i2c_driver w83627hf_driver = {
cdaf7934 336 .driver = {
cdaf7934
LR
337 .name = "w83627hf",
338 },
2d8672c5 339 .attach_adapter = w83627hf_detect,
1da177e4
LT
340 .detach_client = w83627hf_detach_client,
341};
342
343/* following are the sysfs callback functions */
344#define show_in_reg(reg) \
345static ssize_t show_##reg (struct device *dev, char *buf, int nr) \
346{ \
347 struct w83627hf_data *data = w83627hf_update_device(dev); \
348 return sprintf(buf,"%ld\n", (long)IN_FROM_REG(data->reg[nr])); \
349}
350show_in_reg(in)
351show_in_reg(in_min)
352show_in_reg(in_max)
353
354#define store_in_reg(REG, reg) \
355static ssize_t \
356store_in_##reg (struct device *dev, const char *buf, size_t count, int nr) \
357{ \
358 struct i2c_client *client = to_i2c_client(dev); \
359 struct w83627hf_data *data = i2c_get_clientdata(client); \
360 u32 val; \
361 \
362 val = simple_strtoul(buf, NULL, 10); \
363 \
9a61bf63 364 mutex_lock(&data->update_lock); \
1da177e4
LT
365 data->in_##reg[nr] = IN_TO_REG(val); \
366 w83627hf_write_value(client, W83781D_REG_IN_##REG(nr), \
367 data->in_##reg[nr]); \
368 \
9a61bf63 369 mutex_unlock(&data->update_lock); \
1da177e4
LT
370 return count; \
371}
372store_in_reg(MIN, min)
373store_in_reg(MAX, max)
374
375#define sysfs_in_offset(offset) \
376static ssize_t \
a5099cfc 377show_regs_in_##offset (struct device *dev, struct device_attribute *attr, char *buf) \
1da177e4
LT
378{ \
379 return show_in(dev, buf, offset); \
380} \
381static DEVICE_ATTR(in##offset##_input, S_IRUGO, show_regs_in_##offset, NULL);
382
383#define sysfs_in_reg_offset(reg, offset) \
a5099cfc 384static ssize_t show_regs_in_##reg##offset (struct device *dev, struct device_attribute *attr, char *buf) \
1da177e4
LT
385{ \
386 return show_in_##reg (dev, buf, offset); \
387} \
388static ssize_t \
a5099cfc 389store_regs_in_##reg##offset (struct device *dev, struct device_attribute *attr, \
1da177e4
LT
390 const char *buf, size_t count) \
391{ \
392 return store_in_##reg (dev, buf, count, offset); \
393} \
394static DEVICE_ATTR(in##offset##_##reg, S_IRUGO| S_IWUSR, \
395 show_regs_in_##reg##offset, store_regs_in_##reg##offset);
396
397#define sysfs_in_offsets(offset) \
398sysfs_in_offset(offset) \
399sysfs_in_reg_offset(min, offset) \
400sysfs_in_reg_offset(max, offset)
401
402sysfs_in_offsets(1);
403sysfs_in_offsets(2);
404sysfs_in_offsets(3);
405sysfs_in_offsets(4);
406sysfs_in_offsets(5);
407sysfs_in_offsets(6);
408sysfs_in_offsets(7);
409sysfs_in_offsets(8);
410
411/* use a different set of functions for in0 */
412static ssize_t show_in_0(struct w83627hf_data *data, char *buf, u8 reg)
413{
414 long in0;
415
416 if ((data->vrm_ovt & 0x01) &&
417 (w83627thf == data->type || w83637hf == data->type))
418
419 /* use VRM9 calculation */
420 in0 = (long)((reg * 488 + 70000 + 50) / 100);
421 else
422 /* use VRM8 (standard) calculation */
423 in0 = (long)IN_FROM_REG(reg);
424
425 return sprintf(buf,"%ld\n", in0);
426}
427
a5099cfc 428static ssize_t show_regs_in_0(struct device *dev, struct device_attribute *attr, char *buf)
1da177e4
LT
429{
430 struct w83627hf_data *data = w83627hf_update_device(dev);
431 return show_in_0(data, buf, data->in[0]);
432}
433
a5099cfc 434static ssize_t show_regs_in_min0(struct device *dev, struct device_attribute *attr, char *buf)
1da177e4
LT
435{
436 struct w83627hf_data *data = w83627hf_update_device(dev);
437 return show_in_0(data, buf, data->in_min[0]);
438}
439
a5099cfc 440static ssize_t show_regs_in_max0(struct device *dev, struct device_attribute *attr, char *buf)
1da177e4
LT
441{
442 struct w83627hf_data *data = w83627hf_update_device(dev);
443 return show_in_0(data, buf, data->in_max[0]);
444}
445
a5099cfc 446static ssize_t store_regs_in_min0(struct device *dev, struct device_attribute *attr,
1da177e4
LT
447 const char *buf, size_t count)
448{
449 struct i2c_client *client = to_i2c_client(dev);
450 struct w83627hf_data *data = i2c_get_clientdata(client);
451 u32 val;
452
453 val = simple_strtoul(buf, NULL, 10);
454
9a61bf63 455 mutex_lock(&data->update_lock);
1da177e4
LT
456
457 if ((data->vrm_ovt & 0x01) &&
458 (w83627thf == data->type || w83637hf == data->type))
459
460 /* use VRM9 calculation */
2723ab91
YM
461 data->in_min[0] =
462 SENSORS_LIMIT(((val * 100) - 70000 + 244) / 488, 0,
463 255);
1da177e4
LT
464 else
465 /* use VRM8 (standard) calculation */
466 data->in_min[0] = IN_TO_REG(val);
467
468 w83627hf_write_value(client, W83781D_REG_IN_MIN(0), data->in_min[0]);
9a61bf63 469 mutex_unlock(&data->update_lock);
1da177e4
LT
470 return count;
471}
472
a5099cfc 473static ssize_t store_regs_in_max0(struct device *dev, struct device_attribute *attr,
1da177e4
LT
474 const char *buf, size_t count)
475{
476 struct i2c_client *client = to_i2c_client(dev);
477 struct w83627hf_data *data = i2c_get_clientdata(client);
478 u32 val;
479
480 val = simple_strtoul(buf, NULL, 10);
481
9a61bf63 482 mutex_lock(&data->update_lock);
1da177e4
LT
483
484 if ((data->vrm_ovt & 0x01) &&
485 (w83627thf == data->type || w83637hf == data->type))
486
487 /* use VRM9 calculation */
2723ab91
YM
488 data->in_max[0] =
489 SENSORS_LIMIT(((val * 100) - 70000 + 244) / 488, 0,
490 255);
1da177e4
LT
491 else
492 /* use VRM8 (standard) calculation */
493 data->in_max[0] = IN_TO_REG(val);
494
495 w83627hf_write_value(client, W83781D_REG_IN_MAX(0), data->in_max[0]);
9a61bf63 496 mutex_unlock(&data->update_lock);
1da177e4
LT
497 return count;
498}
499
500static DEVICE_ATTR(in0_input, S_IRUGO, show_regs_in_0, NULL);
501static DEVICE_ATTR(in0_min, S_IRUGO | S_IWUSR,
502 show_regs_in_min0, store_regs_in_min0);
503static DEVICE_ATTR(in0_max, S_IRUGO | S_IWUSR,
504 show_regs_in_max0, store_regs_in_max0);
505
506#define device_create_file_in(client, offset) \
507do { \
508device_create_file(&client->dev, &dev_attr_in##offset##_input); \
509device_create_file(&client->dev, &dev_attr_in##offset##_min); \
510device_create_file(&client->dev, &dev_attr_in##offset##_max); \
511} while (0)
512
513#define show_fan_reg(reg) \
514static ssize_t show_##reg (struct device *dev, char *buf, int nr) \
515{ \
516 struct w83627hf_data *data = w83627hf_update_device(dev); \
517 return sprintf(buf,"%ld\n", \
518 FAN_FROM_REG(data->reg[nr-1], \
519 (long)DIV_FROM_REG(data->fan_div[nr-1]))); \
520}
521show_fan_reg(fan);
522show_fan_reg(fan_min);
523
524static ssize_t
525store_fan_min(struct device *dev, const char *buf, size_t count, int nr)
526{
527 struct i2c_client *client = to_i2c_client(dev);
528 struct w83627hf_data *data = i2c_get_clientdata(client);
529 u32 val;
530
531 val = simple_strtoul(buf, NULL, 10);
532
9a61bf63 533 mutex_lock(&data->update_lock);
1da177e4
LT
534 data->fan_min[nr - 1] =
535 FAN_TO_REG(val, DIV_FROM_REG(data->fan_div[nr - 1]));
536 w83627hf_write_value(client, W83781D_REG_FAN_MIN(nr),
537 data->fan_min[nr - 1]);
538
9a61bf63 539 mutex_unlock(&data->update_lock);
1da177e4
LT
540 return count;
541}
542
543#define sysfs_fan_offset(offset) \
a5099cfc 544static ssize_t show_regs_fan_##offset (struct device *dev, struct device_attribute *attr, char *buf) \
1da177e4
LT
545{ \
546 return show_fan(dev, buf, offset); \
547} \
548static DEVICE_ATTR(fan##offset##_input, S_IRUGO, show_regs_fan_##offset, NULL);
549
550#define sysfs_fan_min_offset(offset) \
a5099cfc 551static ssize_t show_regs_fan_min##offset (struct device *dev, struct device_attribute *attr, char *buf) \
1da177e4
LT
552{ \
553 return show_fan_min(dev, buf, offset); \
554} \
555static ssize_t \
a5099cfc 556store_regs_fan_min##offset (struct device *dev, struct device_attribute *attr, const char *buf, size_t count) \
1da177e4
LT
557{ \
558 return store_fan_min(dev, buf, count, offset); \
559} \
560static DEVICE_ATTR(fan##offset##_min, S_IRUGO | S_IWUSR, \
561 show_regs_fan_min##offset, store_regs_fan_min##offset);
562
563sysfs_fan_offset(1);
564sysfs_fan_min_offset(1);
565sysfs_fan_offset(2);
566sysfs_fan_min_offset(2);
567sysfs_fan_offset(3);
568sysfs_fan_min_offset(3);
569
570#define device_create_file_fan(client, offset) \
571do { \
572device_create_file(&client->dev, &dev_attr_fan##offset##_input); \
573device_create_file(&client->dev, &dev_attr_fan##offset##_min); \
574} while (0)
575
576#define show_temp_reg(reg) \
577static ssize_t show_##reg (struct device *dev, char *buf, int nr) \
578{ \
579 struct w83627hf_data *data = w83627hf_update_device(dev); \
580 if (nr >= 2) { /* TEMP2 and TEMP3 */ \
581 return sprintf(buf,"%ld\n", \
582 (long)LM75_TEMP_FROM_REG(data->reg##_add[nr-2])); \
583 } else { /* TEMP1 */ \
584 return sprintf(buf,"%ld\n", (long)TEMP_FROM_REG(data->reg)); \
585 } \
586}
587show_temp_reg(temp);
588show_temp_reg(temp_max);
589show_temp_reg(temp_max_hyst);
590
591#define store_temp_reg(REG, reg) \
592static ssize_t \
593store_temp_##reg (struct device *dev, const char *buf, size_t count, int nr) \
594{ \
595 struct i2c_client *client = to_i2c_client(dev); \
596 struct w83627hf_data *data = i2c_get_clientdata(client); \
597 u32 val; \
598 \
599 val = simple_strtoul(buf, NULL, 10); \
600 \
9a61bf63 601 mutex_lock(&data->update_lock); \
1da177e4
LT
602 \
603 if (nr >= 2) { /* TEMP2 and TEMP3 */ \
604 data->temp_##reg##_add[nr-2] = LM75_TEMP_TO_REG(val); \
605 w83627hf_write_value(client, W83781D_REG_TEMP_##REG(nr), \
606 data->temp_##reg##_add[nr-2]); \
607 } else { /* TEMP1 */ \
608 data->temp_##reg = TEMP_TO_REG(val); \
609 w83627hf_write_value(client, W83781D_REG_TEMP_##REG(nr), \
610 data->temp_##reg); \
611 } \
612 \
9a61bf63 613 mutex_unlock(&data->update_lock); \
1da177e4
LT
614 return count; \
615}
616store_temp_reg(OVER, max);
617store_temp_reg(HYST, max_hyst);
618
619#define sysfs_temp_offset(offset) \
620static ssize_t \
a5099cfc 621show_regs_temp_##offset (struct device *dev, struct device_attribute *attr, char *buf) \
1da177e4
LT
622{ \
623 return show_temp(dev, buf, offset); \
624} \
625static DEVICE_ATTR(temp##offset##_input, S_IRUGO, show_regs_temp_##offset, NULL);
626
627#define sysfs_temp_reg_offset(reg, offset) \
a5099cfc 628static ssize_t show_regs_temp_##reg##offset (struct device *dev, struct device_attribute *attr, char *buf) \
1da177e4
LT
629{ \
630 return show_temp_##reg (dev, buf, offset); \
631} \
632static ssize_t \
a5099cfc 633store_regs_temp_##reg##offset (struct device *dev, struct device_attribute *attr, \
1da177e4
LT
634 const char *buf, size_t count) \
635{ \
636 return store_temp_##reg (dev, buf, count, offset); \
637} \
638static DEVICE_ATTR(temp##offset##_##reg, S_IRUGO| S_IWUSR, \
639 show_regs_temp_##reg##offset, store_regs_temp_##reg##offset);
640
641#define sysfs_temp_offsets(offset) \
642sysfs_temp_offset(offset) \
643sysfs_temp_reg_offset(max, offset) \
644sysfs_temp_reg_offset(max_hyst, offset)
645
646sysfs_temp_offsets(1);
647sysfs_temp_offsets(2);
648sysfs_temp_offsets(3);
649
650#define device_create_file_temp(client, offset) \
651do { \
652device_create_file(&client->dev, &dev_attr_temp##offset##_input); \
653device_create_file(&client->dev, &dev_attr_temp##offset##_max); \
654device_create_file(&client->dev, &dev_attr_temp##offset##_max_hyst); \
655} while (0)
656
657static ssize_t
a5099cfc 658show_vid_reg(struct device *dev, struct device_attribute *attr, char *buf)
1da177e4
LT
659{
660 struct w83627hf_data *data = w83627hf_update_device(dev);
661 return sprintf(buf, "%ld\n", (long) vid_from_reg(data->vid, data->vrm));
662}
663static DEVICE_ATTR(cpu0_vid, S_IRUGO, show_vid_reg, NULL);
664#define device_create_file_vid(client) \
665device_create_file(&client->dev, &dev_attr_cpu0_vid)
666
667static ssize_t
a5099cfc 668show_vrm_reg(struct device *dev, struct device_attribute *attr, char *buf)
1da177e4
LT
669{
670 struct w83627hf_data *data = w83627hf_update_device(dev);
671 return sprintf(buf, "%ld\n", (long) data->vrm);
672}
673static ssize_t
a5099cfc 674store_vrm_reg(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
1da177e4
LT
675{
676 struct i2c_client *client = to_i2c_client(dev);
677 struct w83627hf_data *data = i2c_get_clientdata(client);
678 u32 val;
679
680 val = simple_strtoul(buf, NULL, 10);
681 data->vrm = val;
682
683 return count;
684}
685static DEVICE_ATTR(vrm, S_IRUGO | S_IWUSR, show_vrm_reg, store_vrm_reg);
686#define device_create_file_vrm(client) \
687device_create_file(&client->dev, &dev_attr_vrm)
688
689static ssize_t
a5099cfc 690show_alarms_reg(struct device *dev, struct device_attribute *attr, char *buf)
1da177e4
LT
691{
692 struct w83627hf_data *data = w83627hf_update_device(dev);
693 return sprintf(buf, "%ld\n", (long) data->alarms);
694}
695static DEVICE_ATTR(alarms, S_IRUGO, show_alarms_reg, NULL);
696#define device_create_file_alarms(client) \
697device_create_file(&client->dev, &dev_attr_alarms)
698
699#define show_beep_reg(REG, reg) \
a5099cfc 700static ssize_t show_beep_##reg (struct device *dev, struct device_attribute *attr, char *buf) \
1da177e4
LT
701{ \
702 struct w83627hf_data *data = w83627hf_update_device(dev); \
703 return sprintf(buf,"%ld\n", \
704 (long)BEEP_##REG##_FROM_REG(data->beep_##reg)); \
705}
706show_beep_reg(ENABLE, enable)
707show_beep_reg(MASK, mask)
708
709#define BEEP_ENABLE 0 /* Store beep_enable */
710#define BEEP_MASK 1 /* Store beep_mask */
711
712static ssize_t
713store_beep_reg(struct device *dev, const char *buf, size_t count,
714 int update_mask)
715{
716 struct i2c_client *client = to_i2c_client(dev);
717 struct w83627hf_data *data = i2c_get_clientdata(client);
718 u32 val, val2;
719
720 val = simple_strtoul(buf, NULL, 10);
721
9a61bf63 722 mutex_lock(&data->update_lock);
1da177e4
LT
723
724 if (update_mask == BEEP_MASK) { /* We are storing beep_mask */
725 data->beep_mask = BEEP_MASK_TO_REG(val);
726 w83627hf_write_value(client, W83781D_REG_BEEP_INTS1,
727 data->beep_mask & 0xff);
728 w83627hf_write_value(client, W83781D_REG_BEEP_INTS3,
729 ((data->beep_mask) >> 16) & 0xff);
730 val2 = (data->beep_mask >> 8) & 0x7f;
731 } else { /* We are storing beep_enable */
732 val2 =
733 w83627hf_read_value(client, W83781D_REG_BEEP_INTS2) & 0x7f;
734 data->beep_enable = BEEP_ENABLE_TO_REG(val);
735 }
736
737 w83627hf_write_value(client, W83781D_REG_BEEP_INTS2,
738 val2 | data->beep_enable << 7);
739
9a61bf63 740 mutex_unlock(&data->update_lock);
1da177e4
LT
741 return count;
742}
743
744#define sysfs_beep(REG, reg) \
a5099cfc 745static ssize_t show_regs_beep_##reg (struct device *dev, struct device_attribute *attr, char *buf) \
1da177e4 746{ \
a5099cfc 747 return show_beep_##reg(dev, attr, buf); \
1da177e4
LT
748} \
749static ssize_t \
a5099cfc 750store_regs_beep_##reg (struct device *dev, struct device_attribute *attr, const char *buf, size_t count) \
1da177e4
LT
751{ \
752 return store_beep_reg(dev, buf, count, BEEP_##REG); \
753} \
754static DEVICE_ATTR(beep_##reg, S_IRUGO | S_IWUSR, \
755 show_regs_beep_##reg, store_regs_beep_##reg);
756
757sysfs_beep(ENABLE, enable);
758sysfs_beep(MASK, mask);
759
760#define device_create_file_beep(client) \
761do { \
762device_create_file(&client->dev, &dev_attr_beep_enable); \
763device_create_file(&client->dev, &dev_attr_beep_mask); \
764} while (0)
765
766static ssize_t
767show_fan_div_reg(struct device *dev, char *buf, int nr)
768{
769 struct w83627hf_data *data = w83627hf_update_device(dev);
770 return sprintf(buf, "%ld\n",
771 (long) DIV_FROM_REG(data->fan_div[nr - 1]));
772}
773
774/* Note: we save and restore the fan minimum here, because its value is
775 determined in part by the fan divisor. This follows the principle of
776 least suprise; the user doesn't expect the fan minimum to change just
777 because the divisor changed. */
778static ssize_t
779store_fan_div_reg(struct device *dev, const char *buf, size_t count, int nr)
780{
781 struct i2c_client *client = to_i2c_client(dev);
782 struct w83627hf_data *data = i2c_get_clientdata(client);
783 unsigned long min;
784 u8 reg;
785 unsigned long val = simple_strtoul(buf, NULL, 10);
786
9a61bf63 787 mutex_lock(&data->update_lock);
1da177e4
LT
788
789 /* Save fan_min */
790 min = FAN_FROM_REG(data->fan_min[nr],
791 DIV_FROM_REG(data->fan_div[nr]));
792
793 data->fan_div[nr] = DIV_TO_REG(val);
794
795 reg = (w83627hf_read_value(client, nr==2 ? W83781D_REG_PIN : W83781D_REG_VID_FANDIV)
796 & (nr==0 ? 0xcf : 0x3f))
797 | ((data->fan_div[nr] & 0x03) << (nr==0 ? 4 : 6));
798 w83627hf_write_value(client, nr==2 ? W83781D_REG_PIN : W83781D_REG_VID_FANDIV, reg);
799
800 reg = (w83627hf_read_value(client, W83781D_REG_VBAT)
801 & ~(1 << (5 + nr)))
802 | ((data->fan_div[nr] & 0x04) << (3 + nr));
803 w83627hf_write_value(client, W83781D_REG_VBAT, reg);
804
805 /* Restore fan_min */
806 data->fan_min[nr] = FAN_TO_REG(min, DIV_FROM_REG(data->fan_div[nr]));
807 w83627hf_write_value(client, W83781D_REG_FAN_MIN(nr+1), data->fan_min[nr]);
808
9a61bf63 809 mutex_unlock(&data->update_lock);
1da177e4
LT
810 return count;
811}
812
813#define sysfs_fan_div(offset) \
a5099cfc 814static ssize_t show_regs_fan_div_##offset (struct device *dev, struct device_attribute *attr, char *buf) \
1da177e4
LT
815{ \
816 return show_fan_div_reg(dev, buf, offset); \
817} \
818static ssize_t \
a5099cfc 819store_regs_fan_div_##offset (struct device *dev, struct device_attribute *attr, \
1da177e4
LT
820 const char *buf, size_t count) \
821{ \
822 return store_fan_div_reg(dev, buf, count, offset - 1); \
823} \
824static DEVICE_ATTR(fan##offset##_div, S_IRUGO | S_IWUSR, \
825 show_regs_fan_div_##offset, store_regs_fan_div_##offset);
826
827sysfs_fan_div(1);
828sysfs_fan_div(2);
829sysfs_fan_div(3);
830
831#define device_create_file_fan_div(client, offset) \
832do { \
833device_create_file(&client->dev, &dev_attr_fan##offset##_div); \
834} while (0)
835
836static ssize_t
837show_pwm_reg(struct device *dev, char *buf, int nr)
838{
839 struct w83627hf_data *data = w83627hf_update_device(dev);
840 return sprintf(buf, "%ld\n", (long) data->pwm[nr - 1]);
841}
842
843static ssize_t
844store_pwm_reg(struct device *dev, const char *buf, size_t count, int nr)
845{
846 struct i2c_client *client = to_i2c_client(dev);
847 struct w83627hf_data *data = i2c_get_clientdata(client);
848 u32 val;
849
850 val = simple_strtoul(buf, NULL, 10);
851
9a61bf63 852 mutex_lock(&data->update_lock);
1da177e4
LT
853
854 if (data->type == w83627thf) {
855 /* bits 0-3 are reserved in 627THF */
856 data->pwm[nr - 1] = PWM_TO_REG(val) & 0xf0;
857 w83627hf_write_value(client,
858 W836X7HF_REG_PWM(data->type, nr),
859 data->pwm[nr - 1] |
860 (w83627hf_read_value(client,
861 W836X7HF_REG_PWM(data->type, nr)) & 0x0f));
862 } else {
863 data->pwm[nr - 1] = PWM_TO_REG(val);
864 w83627hf_write_value(client,
865 W836X7HF_REG_PWM(data->type, nr),
866 data->pwm[nr - 1]);
867 }
868
9a61bf63 869 mutex_unlock(&data->update_lock);
1da177e4
LT
870 return count;
871}
872
873#define sysfs_pwm(offset) \
a5099cfc 874static ssize_t show_regs_pwm_##offset (struct device *dev, struct device_attribute *attr, char *buf) \
1da177e4
LT
875{ \
876 return show_pwm_reg(dev, buf, offset); \
877} \
878static ssize_t \
a5099cfc 879store_regs_pwm_##offset (struct device *dev, struct device_attribute *attr, const char *buf, size_t count) \
1da177e4
LT
880{ \
881 return store_pwm_reg(dev, buf, count, offset); \
882} \
883static DEVICE_ATTR(pwm##offset, S_IRUGO | S_IWUSR, \
884 show_regs_pwm_##offset, store_regs_pwm_##offset);
885
886sysfs_pwm(1);
887sysfs_pwm(2);
888sysfs_pwm(3);
889
890#define device_create_file_pwm(client, offset) \
891do { \
892device_create_file(&client->dev, &dev_attr_pwm##offset); \
893} while (0)
894
895static ssize_t
896show_sensor_reg(struct device *dev, char *buf, int nr)
897{
898 struct w83627hf_data *data = w83627hf_update_device(dev);
899 return sprintf(buf, "%ld\n", (long) data->sens[nr - 1]);
900}
901
902static ssize_t
903store_sensor_reg(struct device *dev, const char *buf, size_t count, int nr)
904{
905 struct i2c_client *client = to_i2c_client(dev);
906 struct w83627hf_data *data = i2c_get_clientdata(client);
907 u32 val, tmp;
908
909 val = simple_strtoul(buf, NULL, 10);
910
9a61bf63 911 mutex_lock(&data->update_lock);
1da177e4
LT
912
913 switch (val) {
914 case 1: /* PII/Celeron diode */
915 tmp = w83627hf_read_value(client, W83781D_REG_SCFG1);
916 w83627hf_write_value(client, W83781D_REG_SCFG1,
917 tmp | BIT_SCFG1[nr - 1]);
918 tmp = w83627hf_read_value(client, W83781D_REG_SCFG2);
919 w83627hf_write_value(client, W83781D_REG_SCFG2,
920 tmp | BIT_SCFG2[nr - 1]);
921 data->sens[nr - 1] = val;
922 break;
923 case 2: /* 3904 */
924 tmp = w83627hf_read_value(client, W83781D_REG_SCFG1);
925 w83627hf_write_value(client, W83781D_REG_SCFG1,
926 tmp | BIT_SCFG1[nr - 1]);
927 tmp = w83627hf_read_value(client, W83781D_REG_SCFG2);
928 w83627hf_write_value(client, W83781D_REG_SCFG2,
929 tmp & ~BIT_SCFG2[nr - 1]);
930 data->sens[nr - 1] = val;
931 break;
932 case W83781D_DEFAULT_BETA: /* thermistor */
933 tmp = w83627hf_read_value(client, W83781D_REG_SCFG1);
934 w83627hf_write_value(client, W83781D_REG_SCFG1,
935 tmp & ~BIT_SCFG1[nr - 1]);
936 data->sens[nr - 1] = val;
937 break;
938 default:
939 dev_err(&client->dev,
940 "Invalid sensor type %ld; must be 1, 2, or %d\n",
941 (long) val, W83781D_DEFAULT_BETA);
942 break;
943 }
944
9a61bf63 945 mutex_unlock(&data->update_lock);
1da177e4
LT
946 return count;
947}
948
949#define sysfs_sensor(offset) \
a5099cfc 950static ssize_t show_regs_sensor_##offset (struct device *dev, struct device_attribute *attr, char *buf) \
1da177e4
LT
951{ \
952 return show_sensor_reg(dev, buf, offset); \
953} \
954static ssize_t \
a5099cfc 955store_regs_sensor_##offset (struct device *dev, struct device_attribute *attr, const char *buf, size_t count) \
1da177e4
LT
956{ \
957 return store_sensor_reg(dev, buf, count, offset); \
958} \
959static DEVICE_ATTR(temp##offset##_type, S_IRUGO | S_IWUSR, \
960 show_regs_sensor_##offset, store_regs_sensor_##offset);
961
962sysfs_sensor(1);
963sysfs_sensor(2);
964sysfs_sensor(3);
965
966#define device_create_file_sensor(client, offset) \
967do { \
968device_create_file(&client->dev, &dev_attr_temp##offset##_type); \
969} while (0)
970
971
e6cfb3ad 972static int __init w83627hf_find(int sioaddr, unsigned short *addr)
1da177e4
LT
973{
974 u16 val;
975
976 REG = sioaddr;
977 VAL = sioaddr + 1;
978
979 superio_enter();
980 val= superio_inb(DEVID);
981 if(val != W627_DEVID &&
982 val != W627THF_DEVID &&
983 val != W697_DEVID &&
984 val != W637_DEVID) {
985 superio_exit();
986 return -ENODEV;
987 }
988
989 superio_select(W83627HF_LD_HWM);
990 val = (superio_inb(WINB_BASE_REG) << 8) |
991 superio_inb(WINB_BASE_REG + 1);
ada0c2f8 992 *addr = val & WINB_ALIGNMENT;
2d8672c5 993 if (*addr == 0 && force_addr == 0) {
1da177e4
LT
994 superio_exit();
995 return -ENODEV;
996 }
1da177e4
LT
997
998 superio_exit();
999 return 0;
1000}
1001
2d8672c5 1002static int w83627hf_detect(struct i2c_adapter *adapter)
1da177e4 1003{
2d8672c5 1004 int val, kind;
1da177e4
LT
1005 struct i2c_client *new_client;
1006 struct w83627hf_data *data;
1007 int err = 0;
1008 const char *client_name = "";
1009
1da177e4 1010 if(force_addr)
ada0c2f8 1011 address = force_addr & WINB_ALIGNMENT;
1da177e4 1012
ada0c2f8 1013 if (!request_region(address + WINB_REGION_OFFSET, WINB_REGION_SIZE,
cdaf7934 1014 w83627hf_driver.driver.name)) {
1da177e4
LT
1015 err = -EBUSY;
1016 goto ERROR0;
1017 }
1018
1019 if(force_addr) {
1020 printk("w83627hf.o: forcing ISA address 0x%04X\n", address);
1021 superio_enter();
1022 superio_select(W83627HF_LD_HWM);
1023 superio_outb(WINB_BASE_REG, address >> 8);
1024 superio_outb(WINB_BASE_REG+1, address & 0xff);
1025 superio_exit();
1026 }
1027
1028 superio_enter();
1029 val= superio_inb(DEVID);
1030 if(val == W627_DEVID)
1031 kind = w83627hf;
1032 else if(val == W697_DEVID)
1033 kind = w83697hf;
1034 else if(val == W627THF_DEVID)
1035 kind = w83627thf;
1036 else if(val == W637_DEVID)
1037 kind = w83637hf;
1038 else {
1039 dev_info(&adapter->dev,
1040 "Unsupported chip (dev_id=0x%02X).\n", val);
1041 goto ERROR1;
1042 }
1043
1044 superio_select(W83627HF_LD_HWM);
1045 if((val = 0x01 & superio_inb(WINB_ACT_REG)) == 0)
1046 superio_outb(WINB_ACT_REG, 1);
1047 superio_exit();
1048
1049 /* OK. For now, we presume we have a valid client. We now create the
1050 client structure, even though we cannot fill it completely yet.
1051 But it allows us to access w83627hf_{read,write}_value. */
1052
ba9c2e8d 1053 if (!(data = kzalloc(sizeof(struct w83627hf_data), GFP_KERNEL))) {
1da177e4
LT
1054 err = -ENOMEM;
1055 goto ERROR1;
1056 }
1da177e4
LT
1057
1058 new_client = &data->client;
1059 i2c_set_clientdata(new_client, data);
1060 new_client->addr = address;
9a61bf63 1061 mutex_init(&data->lock);
1da177e4
LT
1062 new_client->adapter = adapter;
1063 new_client->driver = &w83627hf_driver;
1064 new_client->flags = 0;
1065
1066
1067 if (kind == w83627hf) {
1068 client_name = "w83627hf";
1069 } else if (kind == w83627thf) {
1070 client_name = "w83627thf";
1071 } else if (kind == w83697hf) {
1072 client_name = "w83697hf";
1073 } else if (kind == w83637hf) {
1074 client_name = "w83637hf";
1075 }
1076
1077 /* Fill in the remaining client fields and put into the global list */
1078 strlcpy(new_client->name, client_name, I2C_NAME_SIZE);
1079 data->type = kind;
1080 data->valid = 0;
9a61bf63 1081 mutex_init(&data->update_lock);
1da177e4
LT
1082
1083 /* Tell the I2C layer a new client has arrived */
1084 if ((err = i2c_attach_client(new_client)))
1085 goto ERROR2;
1086
1087 data->lm75 = NULL;
1088
1089 /* Initialize the chip */
1090 w83627hf_init_client(new_client);
1091
1092 /* A few vars need to be filled upon startup */
1093 data->fan_min[0] = w83627hf_read_value(new_client, W83781D_REG_FAN_MIN(1));
1094 data->fan_min[1] = w83627hf_read_value(new_client, W83781D_REG_FAN_MIN(2));
1095 data->fan_min[2] = w83627hf_read_value(new_client, W83781D_REG_FAN_MIN(3));
1096
1097 /* Register sysfs hooks */
943b0830
MH
1098 data->class_dev = hwmon_device_register(&new_client->dev);
1099 if (IS_ERR(data->class_dev)) {
1100 err = PTR_ERR(data->class_dev);
1101 goto ERROR3;
1102 }
1103
1da177e4
LT
1104 device_create_file_in(new_client, 0);
1105 if (kind != w83697hf)
1106 device_create_file_in(new_client, 1);
1107 device_create_file_in(new_client, 2);
1108 device_create_file_in(new_client, 3);
1109 device_create_file_in(new_client, 4);
1110 if (kind != w83627thf && kind != w83637hf) {
1111 device_create_file_in(new_client, 5);
1112 device_create_file_in(new_client, 6);
1113 }
1114 device_create_file_in(new_client, 7);
1115 device_create_file_in(new_client, 8);
1116
1117 device_create_file_fan(new_client, 1);
1118 device_create_file_fan(new_client, 2);
1119 if (kind != w83697hf)
1120 device_create_file_fan(new_client, 3);
1121
1122 device_create_file_temp(new_client, 1);
1123 device_create_file_temp(new_client, 2);
1124 if (kind != w83697hf)
1125 device_create_file_temp(new_client, 3);
1126
dd149c52 1127 if (kind != w83697hf && data->vid != 0xff) {
1da177e4 1128 device_create_file_vid(new_client);
1da177e4 1129 device_create_file_vrm(new_client);
dd149c52 1130 }
1da177e4
LT
1131
1132 device_create_file_fan_div(new_client, 1);
1133 device_create_file_fan_div(new_client, 2);
1134 if (kind != w83697hf)
1135 device_create_file_fan_div(new_client, 3);
1136
1137 device_create_file_alarms(new_client);
1138
1139 device_create_file_beep(new_client);
1140
1141 device_create_file_pwm(new_client, 1);
1142 device_create_file_pwm(new_client, 2);
1143 if (kind == w83627thf || kind == w83637hf)
1144 device_create_file_pwm(new_client, 3);
1145
1146 device_create_file_sensor(new_client, 1);
1147 device_create_file_sensor(new_client, 2);
1148 if (kind != w83697hf)
1149 device_create_file_sensor(new_client, 3);
1150
1151 return 0;
1152
943b0830
MH
1153 ERROR3:
1154 i2c_detach_client(new_client);
1da177e4
LT
1155 ERROR2:
1156 kfree(data);
1157 ERROR1:
ada0c2f8 1158 release_region(address + WINB_REGION_OFFSET, WINB_REGION_SIZE);
1da177e4
LT
1159 ERROR0:
1160 return err;
1161}
1162
1163static int w83627hf_detach_client(struct i2c_client *client)
1164{
943b0830 1165 struct w83627hf_data *data = i2c_get_clientdata(client);
1da177e4
LT
1166 int err;
1167
943b0830
MH
1168 hwmon_device_unregister(data->class_dev);
1169
7bef5594 1170 if ((err = i2c_detach_client(client)))
1da177e4 1171 return err;
1da177e4 1172
ada0c2f8 1173 release_region(client->addr + WINB_REGION_OFFSET, WINB_REGION_SIZE);
943b0830 1174 kfree(data);
1da177e4
LT
1175
1176 return 0;
1177}
1178
1179
1180/*
1181 ISA access must always be locked explicitly!
1182 We ignore the W83781D BUSY flag at this moment - it could lead to deadlocks,
1183 would slow down the W83781D access and should not be necessary.
1184 There are some ugly typecasts here, but the good news is - they should
1185 nowhere else be necessary! */
1186static int w83627hf_read_value(struct i2c_client *client, u16 reg)
1187{
1188 struct w83627hf_data *data = i2c_get_clientdata(client);
1189 int res, word_sized;
1190
9a61bf63 1191 mutex_lock(&data->lock);
1da177e4
LT
1192 word_sized = (((reg & 0xff00) == 0x100)
1193 || ((reg & 0xff00) == 0x200))
1194 && (((reg & 0x00ff) == 0x50)
1195 || ((reg & 0x00ff) == 0x53)
1196 || ((reg & 0x00ff) == 0x55));
1197 if (reg & 0xff00) {
1198 outb_p(W83781D_REG_BANK,
1199 client->addr + W83781D_ADDR_REG_OFFSET);
1200 outb_p(reg >> 8,
1201 client->addr + W83781D_DATA_REG_OFFSET);
1202 }
1203 outb_p(reg & 0xff, client->addr + W83781D_ADDR_REG_OFFSET);
1204 res = inb_p(client->addr + W83781D_DATA_REG_OFFSET);
1205 if (word_sized) {
1206 outb_p((reg & 0xff) + 1,
1207 client->addr + W83781D_ADDR_REG_OFFSET);
1208 res =
1209 (res << 8) + inb_p(client->addr +
1210 W83781D_DATA_REG_OFFSET);
1211 }
1212 if (reg & 0xff00) {
1213 outb_p(W83781D_REG_BANK,
1214 client->addr + W83781D_ADDR_REG_OFFSET);
1215 outb_p(0, client->addr + W83781D_DATA_REG_OFFSET);
1216 }
9a61bf63 1217 mutex_unlock(&data->lock);
1da177e4
LT
1218 return res;
1219}
1220
1221static int w83627thf_read_gpio5(struct i2c_client *client)
1222{
1223 int res = 0xff, sel;
1224
1225 superio_enter();
1226 superio_select(W83627HF_LD_GPIO5);
1227
1228 /* Make sure these GPIO pins are enabled */
1229 if (!(superio_inb(W83627THF_GPIO5_EN) & (1<<3))) {
1230 dev_dbg(&client->dev, "GPIO5 disabled, no VID function\n");
1231 goto exit;
1232 }
1233
1234 /* Make sure the pins are configured for input
1235 There must be at least five (VRM 9), and possibly 6 (VRM 10) */
dd149c52 1236 sel = superio_inb(W83627THF_GPIO5_IOSR) & 0x3f;
1da177e4
LT
1237 if ((sel & 0x1f) != 0x1f) {
1238 dev_dbg(&client->dev, "GPIO5 not configured for VID "
1239 "function\n");
1240 goto exit;
1241 }
1242
1243 dev_info(&client->dev, "Reading VID from GPIO5\n");
1244 res = superio_inb(W83627THF_GPIO5_DR) & sel;
1245
1246exit:
1247 superio_exit();
1248 return res;
1249}
1250
1251static int w83627hf_write_value(struct i2c_client *client, u16 reg, u16 value)
1252{
1253 struct w83627hf_data *data = i2c_get_clientdata(client);
1254 int word_sized;
1255
9a61bf63 1256 mutex_lock(&data->lock);
1da177e4
LT
1257 word_sized = (((reg & 0xff00) == 0x100)
1258 || ((reg & 0xff00) == 0x200))
1259 && (((reg & 0x00ff) == 0x53)
1260 || ((reg & 0x00ff) == 0x55));
1261 if (reg & 0xff00) {
1262 outb_p(W83781D_REG_BANK,
1263 client->addr + W83781D_ADDR_REG_OFFSET);
1264 outb_p(reg >> 8,
1265 client->addr + W83781D_DATA_REG_OFFSET);
1266 }
1267 outb_p(reg & 0xff, client->addr + W83781D_ADDR_REG_OFFSET);
1268 if (word_sized) {
1269 outb_p(value >> 8,
1270 client->addr + W83781D_DATA_REG_OFFSET);
1271 outb_p((reg & 0xff) + 1,
1272 client->addr + W83781D_ADDR_REG_OFFSET);
1273 }
1274 outb_p(value & 0xff,
1275 client->addr + W83781D_DATA_REG_OFFSET);
1276 if (reg & 0xff00) {
1277 outb_p(W83781D_REG_BANK,
1278 client->addr + W83781D_ADDR_REG_OFFSET);
1279 outb_p(0, client->addr + W83781D_DATA_REG_OFFSET);
1280 }
9a61bf63 1281 mutex_unlock(&data->lock);
1da177e4
LT
1282 return 0;
1283}
1284
1da177e4
LT
1285static void w83627hf_init_client(struct i2c_client *client)
1286{
1287 struct w83627hf_data *data = i2c_get_clientdata(client);
1288 int i;
1289 int type = data->type;
1290 u8 tmp;
1291
2251cf1a
JD
1292 if (reset) {
1293 /* Resetting the chip has been the default for a long time,
1294 but repeatedly caused problems (fans going to full
1295 speed...) so it is now optional. It might even go away if
1296 nobody reports it as being useful, as I see very little
1297 reason why this would be needed at all. */
1298 dev_info(&client->dev, "If reset=1 solved a problem you were "
1299 "having, please report!\n");
1300
1da177e4
LT
1301 /* save this register */
1302 i = w83627hf_read_value(client, W83781D_REG_BEEP_CONFIG);
1303 /* Reset all except Watchdog values and last conversion values
1304 This sets fan-divs to 2, among others */
1305 w83627hf_write_value(client, W83781D_REG_CONFIG, 0x80);
1306 /* Restore the register and disable power-on abnormal beep.
1307 This saves FAN 1/2/3 input/output values set by BIOS. */
1308 w83627hf_write_value(client, W83781D_REG_BEEP_CONFIG, i | 0x80);
1309 /* Disable master beep-enable (reset turns it on).
1310 Individual beeps should be reset to off but for some reason
1311 disabling this bit helps some people not get beeped */
1312 w83627hf_write_value(client, W83781D_REG_BEEP_INTS2, 0);
1313 }
1314
1315 /* Minimize conflicts with other winbond i2c-only clients... */
1316 /* disable i2c subclients... how to disable main i2c client?? */
1317 /* force i2c address to relatively uncommon address */
1318 w83627hf_write_value(client, W83781D_REG_I2C_SUBADDR, 0x89);
1319 w83627hf_write_value(client, W83781D_REG_I2C_ADDR, force_i2c);
1320
1321 /* Read VID only once */
1322 if (w83627hf == data->type || w83637hf == data->type) {
1323 int lo = w83627hf_read_value(client, W83781D_REG_VID_FANDIV);
1324 int hi = w83627hf_read_value(client, W83781D_REG_CHIPID);
1325 data->vid = (lo & 0x0f) | ((hi & 0x01) << 4);
1326 } else if (w83627thf == data->type) {
dd149c52 1327 data->vid = w83627thf_read_gpio5(client);
1da177e4
LT
1328 }
1329
1330 /* Read VRM & OVT Config only once */
1331 if (w83627thf == data->type || w83637hf == data->type) {
1332 data->vrm_ovt =
1333 w83627hf_read_value(client, W83627THF_REG_VRM_OVT_CFG);
1da177e4
LT
1334 }
1335
dd149c52
YM
1336 /* Convert VID to voltage based on VRM */
1337 data->vrm = vid_which_vrm();
1338
1da177e4
LT
1339 tmp = w83627hf_read_value(client, W83781D_REG_SCFG1);
1340 for (i = 1; i <= 3; i++) {
1341 if (!(tmp & BIT_SCFG1[i - 1])) {
1342 data->sens[i - 1] = W83781D_DEFAULT_BETA;
1343 } else {
1344 if (w83627hf_read_value
1345 (client,
1346 W83781D_REG_SCFG2) & BIT_SCFG2[i - 1])
1347 data->sens[i - 1] = 1;
1348 else
1349 data->sens[i - 1] = 2;
1350 }
1351 if ((type == w83697hf) && (i == 2))
1352 break;
1353 }
1354
1355 if(init) {
1356 /* Enable temp2 */
1357 tmp = w83627hf_read_value(client, W83781D_REG_TEMP2_CONFIG);
1358 if (tmp & 0x01) {
1359 dev_warn(&client->dev, "Enabling temp2, readings "
1360 "might not make sense\n");
1361 w83627hf_write_value(client, W83781D_REG_TEMP2_CONFIG,
1362 tmp & 0xfe);
1363 }
1364
1365 /* Enable temp3 */
1366 if (type != w83697hf) {
1367 tmp = w83627hf_read_value(client,
1368 W83781D_REG_TEMP3_CONFIG);
1369 if (tmp & 0x01) {
1370 dev_warn(&client->dev, "Enabling temp3, "
1371 "readings might not make sense\n");
1372 w83627hf_write_value(client,
1373 W83781D_REG_TEMP3_CONFIG, tmp & 0xfe);
1374 }
1375 }
1da177e4
LT
1376 }
1377
1378 /* Start monitoring */
1379 w83627hf_write_value(client, W83781D_REG_CONFIG,
1380 (w83627hf_read_value(client,
1381 W83781D_REG_CONFIG) & 0xf7)
1382 | 0x01);
1383}
1384
1385static struct w83627hf_data *w83627hf_update_device(struct device *dev)
1386{
1387 struct i2c_client *client = to_i2c_client(dev);
1388 struct w83627hf_data *data = i2c_get_clientdata(client);
1389 int i;
1390
9a61bf63 1391 mutex_lock(&data->update_lock);
1da177e4
LT
1392
1393 if (time_after(jiffies, data->last_updated + HZ + HZ / 2)
1394 || !data->valid) {
1395 for (i = 0; i <= 8; i++) {
1396 /* skip missing sensors */
1397 if (((data->type == w83697hf) && (i == 1)) ||
1398 ((data->type == w83627thf || data->type == w83637hf)
4a1c4447 1399 && (i == 5 || i == 6)))
1da177e4
LT
1400 continue;
1401 data->in[i] =
1402 w83627hf_read_value(client, W83781D_REG_IN(i));
1403 data->in_min[i] =
1404 w83627hf_read_value(client,
1405 W83781D_REG_IN_MIN(i));
1406 data->in_max[i] =
1407 w83627hf_read_value(client,
1408 W83781D_REG_IN_MAX(i));
1409 }
1410 for (i = 1; i <= 3; i++) {
1411 data->fan[i - 1] =
1412 w83627hf_read_value(client, W83781D_REG_FAN(i));
1413 data->fan_min[i - 1] =
1414 w83627hf_read_value(client,
1415 W83781D_REG_FAN_MIN(i));
1416 }
1417 for (i = 1; i <= 3; i++) {
1418 u8 tmp = w83627hf_read_value(client,
1419 W836X7HF_REG_PWM(data->type, i));
1420 /* bits 0-3 are reserved in 627THF */
1421 if (data->type == w83627thf)
1422 tmp &= 0xf0;
1423 data->pwm[i - 1] = tmp;
1424 if(i == 2 &&
1425 (data->type == w83627hf || data->type == w83697hf))
1426 break;
1427 }
1428
1429 data->temp = w83627hf_read_value(client, W83781D_REG_TEMP(1));
1430 data->temp_max =
1431 w83627hf_read_value(client, W83781D_REG_TEMP_OVER(1));
1432 data->temp_max_hyst =
1433 w83627hf_read_value(client, W83781D_REG_TEMP_HYST(1));
1434 data->temp_add[0] =
1435 w83627hf_read_value(client, W83781D_REG_TEMP(2));
1436 data->temp_max_add[0] =
1437 w83627hf_read_value(client, W83781D_REG_TEMP_OVER(2));
1438 data->temp_max_hyst_add[0] =
1439 w83627hf_read_value(client, W83781D_REG_TEMP_HYST(2));
1440 if (data->type != w83697hf) {
1441 data->temp_add[1] =
1442 w83627hf_read_value(client, W83781D_REG_TEMP(3));
1443 data->temp_max_add[1] =
1444 w83627hf_read_value(client, W83781D_REG_TEMP_OVER(3));
1445 data->temp_max_hyst_add[1] =
1446 w83627hf_read_value(client, W83781D_REG_TEMP_HYST(3));
1447 }
1448
1449 i = w83627hf_read_value(client, W83781D_REG_VID_FANDIV);
1450 data->fan_div[0] = (i >> 4) & 0x03;
1451 data->fan_div[1] = (i >> 6) & 0x03;
1452 if (data->type != w83697hf) {
1453 data->fan_div[2] = (w83627hf_read_value(client,
1454 W83781D_REG_PIN) >> 6) & 0x03;
1455 }
1456 i = w83627hf_read_value(client, W83781D_REG_VBAT);
1457 data->fan_div[0] |= (i >> 3) & 0x04;
1458 data->fan_div[1] |= (i >> 4) & 0x04;
1459 if (data->type != w83697hf)
1460 data->fan_div[2] |= (i >> 5) & 0x04;
1461 data->alarms =
1462 w83627hf_read_value(client, W83781D_REG_ALARM1) |
1463 (w83627hf_read_value(client, W83781D_REG_ALARM2) << 8) |
1464 (w83627hf_read_value(client, W83781D_REG_ALARM3) << 16);
1465 i = w83627hf_read_value(client, W83781D_REG_BEEP_INTS2);
1466 data->beep_enable = i >> 7;
1467 data->beep_mask = ((i & 0x7f) << 8) |
1468 w83627hf_read_value(client, W83781D_REG_BEEP_INTS1) |
1469 w83627hf_read_value(client, W83781D_REG_BEEP_INTS3) << 16;
1470 data->last_updated = jiffies;
1471 data->valid = 1;
1472 }
1473
9a61bf63 1474 mutex_unlock(&data->update_lock);
1da177e4
LT
1475
1476 return data;
1477}
1478
1479static int __init sensors_w83627hf_init(void)
1480{
2d8672c5
JD
1481 if (w83627hf_find(0x2e, &address)
1482 && w83627hf_find(0x4e, &address)) {
1da177e4
LT
1483 return -ENODEV;
1484 }
1da177e4 1485
fde09509 1486 return i2c_isa_add_driver(&w83627hf_driver);
1da177e4
LT
1487}
1488
1489static void __exit sensors_w83627hf_exit(void)
1490{
fde09509 1491 i2c_isa_del_driver(&w83627hf_driver);
1da177e4
LT
1492}
1493
1494MODULE_AUTHOR("Frodo Looijaard <frodol@dds.nl>, "
1495 "Philip Edelbrock <phil@netroedge.com>, "
1496 "and Mark Studebaker <mdsxyz123@yahoo.com>");
1497MODULE_DESCRIPTION("W83627HF driver");
1498MODULE_LICENSE("GPL");
1499
1500module_init(sensors_w83627hf_init);
1501module_exit(sensors_w83627hf_exit);