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