2 w83627hf.c - Part of lm_sensors, Linux kernel modules for hardware
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>
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.
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.
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.
25 Supports following chips:
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)
33 For other winbond chips, and for i2c support in the above chips,
36 Note: automatic ("cruise") fan control for 697, 637 & 627thf not
40 #include <linux/module.h>
41 #include <linux/init.h>
42 #include <linux/slab.h>
43 #include <linux/i2c.h>
44 #include <linux/i2c-sensor.h>
45 #include <linux/i2c-vid.h>
49 static int force_addr;
50 MODULE_PARM(force_addr, "i");
51 MODULE_PARM_DESC(force_addr,
52 "Initialize the base address of the sensors");
53 static int force_i2c = 0x1f;
54 MODULE_PARM(force_i2c, "i");
55 MODULE_PARM_DESC(force_i2c,
56 "Initialize the i2c address of the sensors");
58 /* Addresses to scan */
59 static unsigned short normal_i2c[] = { I2C_CLIENT_END };
60 static unsigned short normal_i2c_range[] = { I2C_CLIENT_END };
61 static unsigned int normal_isa[] = { 0, I2C_CLIENT_ISA_END };
62 static unsigned int normal_isa_range[] = { I2C_CLIENT_ISA_END };
64 /* Insmod parameters */
65 SENSORS_INSMOD_4(w83627hf, w83627thf, w83697hf, w83637hf);
68 MODULE_PARM(init, "i");
69 MODULE_PARM_DESC(init, "Set to zero to bypass chip initialization");
71 /* modified from kernel/include/traps.c */
72 #define REG 0x2e /* The register to read/write */
73 #define DEV 0x07 /* Register: Logical device select */
74 #define VAL 0x2f /* The value to read/write */
76 /* logical device numbers for superio_select (below) */
77 #define W83627HF_LD_FDC 0x00
78 #define W83627HF_LD_PRT 0x01
79 #define W83627HF_LD_UART1 0x02
80 #define W83627HF_LD_UART2 0x03
81 #define W83627HF_LD_KBC 0x05
82 #define W83627HF_LD_CIR 0x06 /* w83627hf only */
83 #define W83627HF_LD_GAME 0x07
84 #define W83627HF_LD_MIDI 0x07
85 #define W83627HF_LD_GPIO1 0x07
86 #define W83627HF_LD_GPIO5 0x07 /* w83627thf only */
87 #define W83627HF_LD_GPIO2 0x08
88 #define W83627HF_LD_GPIO3 0x09
89 #define W83627HF_LD_GPIO4 0x09 /* w83627thf only */
90 #define W83627HF_LD_ACPI 0x0a
91 #define W83627HF_LD_HWM 0x0b
93 #define DEVID 0x20 /* Register: Device ID */
95 #define W83627THF_GPIO5_IOSR 0xf3 /* w83627thf only */
96 #define W83627THF_GPIO5_DR 0xf4 /* w83627thf only */
97 #define W83627THF_GPIO5_INVR 0xf5 /* w83627thf only */
100 superio_outb(int reg, int val)
114 superio_select(int ld)
133 #define W627_DEVID 0x52
134 #define W627THF_DEVID 0x82
135 #define W697_DEVID 0x60
136 #define W637_DEVID 0x70
137 #define WINB_ACT_REG 0x30
138 #define WINB_BASE_REG 0x60
139 /* Constants specified below */
141 /* Length of ISA address segment */
142 #define WINB_EXTENT 8
144 /* Where are the ISA address/data registers relative to the base address */
145 #define W83781D_ADDR_REG_OFFSET 5
146 #define W83781D_DATA_REG_OFFSET 6
148 /* The W83781D registers */
149 /* The W83782D registers for nr=7,8 are in bank 5 */
150 #define W83781D_REG_IN_MAX(nr) ((nr < 7) ? (0x2b + (nr) * 2) : \
151 (0x554 + (((nr) - 7) * 2)))
152 #define W83781D_REG_IN_MIN(nr) ((nr < 7) ? (0x2c + (nr) * 2) : \
153 (0x555 + (((nr) - 7) * 2)))
154 #define W83781D_REG_IN(nr) ((nr < 7) ? (0x20 + (nr)) : \
157 #define W83781D_REG_FAN_MIN(nr) (0x3a + (nr))
158 #define W83781D_REG_FAN(nr) (0x27 + (nr))
160 #define W83781D_REG_TEMP2_CONFIG 0x152
161 #define W83781D_REG_TEMP3_CONFIG 0x252
162 #define W83781D_REG_TEMP(nr) ((nr == 3) ? (0x0250) : \
163 ((nr == 2) ? (0x0150) : \
165 #define W83781D_REG_TEMP_HYST(nr) ((nr == 3) ? (0x253) : \
166 ((nr == 2) ? (0x153) : \
168 #define W83781D_REG_TEMP_OVER(nr) ((nr == 3) ? (0x255) : \
169 ((nr == 2) ? (0x155) : \
172 #define W83781D_REG_BANK 0x4E
174 #define W83781D_REG_CONFIG 0x40
175 #define W83781D_REG_ALARM1 0x41
176 #define W83781D_REG_ALARM2 0x42
177 #define W83781D_REG_ALARM3 0x450
179 #define W83781D_REG_IRQ 0x4C
180 #define W83781D_REG_BEEP_CONFIG 0x4D
181 #define W83781D_REG_BEEP_INTS1 0x56
182 #define W83781D_REG_BEEP_INTS2 0x57
183 #define W83781D_REG_BEEP_INTS3 0x453
185 #define W83781D_REG_VID_FANDIV 0x47
187 #define W83781D_REG_CHIPID 0x49
188 #define W83781D_REG_WCHIPID 0x58
189 #define W83781D_REG_CHIPMAN 0x4F
190 #define W83781D_REG_PIN 0x4B
192 #define W83781D_REG_VBAT 0x5D
194 #define W83627HF_REG_PWM1 0x5A
195 #define W83627HF_REG_PWM2 0x5B
196 #define W83627HF_REG_PWMCLK12 0x5C
198 #define W83627THF_REG_PWM1 0x01 /* 697HF and 637HF too */
199 #define W83627THF_REG_PWM2 0x03 /* 697HF and 637HF too */
200 #define W83627THF_REG_PWM3 0x11 /* 637HF too */
202 #define W83627THF_REG_VRM_OVT_CFG 0x18 /* 637HF too, unused yet */
204 static const u8 regpwm_627hf[] = { W83627HF_REG_PWM1, W83627HF_REG_PWM2 };
205 static const u8 regpwm[] = { W83627THF_REG_PWM1, W83627THF_REG_PWM2,
206 W83627THF_REG_PWM3 };
207 #define W836X7HF_REG_PWM(type, nr) (((type) == w83627hf) ? \
208 regpwm_627hf[(nr) - 1] : regpwm[(nr) - 1])
210 #define W83781D_REG_I2C_ADDR 0x48
211 #define W83781D_REG_I2C_SUBADDR 0x4A
213 /* Sensor selection */
214 #define W83781D_REG_SCFG1 0x5D
215 static const u8 BIT_SCFG1[] = { 0x02, 0x04, 0x08 };
216 #define W83781D_REG_SCFG2 0x59
217 static const u8 BIT_SCFG2[] = { 0x10, 0x20, 0x40 };
218 #define W83781D_DEFAULT_BETA 3435
220 /* Conversions. Limit checking is only done on the TO_REG
221 variants. Note that you should be a bit careful with which arguments
222 these macros are called: arguments may be evaluated more than once.
223 Fixing this is just not worth it. */
224 #define IN_TO_REG(val) (SENSORS_LIMIT((((val) + 8)/16),0,255))
225 #define IN_FROM_REG(val) ((val) * 16 + 5)
227 static inline u8 FAN_TO_REG(long rpm, int div)
231 rpm = SENSORS_LIMIT(rpm, 1, 1000000);
232 return SENSORS_LIMIT((1350000 + rpm * div / 2) / (rpm * div), 1,
236 #define TEMP_MIN (-128000)
237 #define TEMP_MAX ( 127000)
239 /* TEMP: 0.001C/bit (-128C to +127C)
240 REG: 1C/bit, two's complement */
241 static u8 TEMP_TO_REG(int temp)
243 int ntemp = SENSORS_LIMIT(temp, TEMP_MIN, TEMP_MAX);
244 ntemp += (ntemp<0 ? -500 : 500);
245 return (u8)(ntemp / 1000);
248 static int TEMP_FROM_REG(u8 reg)
250 return (s8)reg * 1000;
253 #define FAN_FROM_REG(val,div) ((val)==0?-1:(val)==255?0:1350000/((val)*(div)))
255 #define PWM_TO_REG(val) (SENSORS_LIMIT((val),0,255))
257 #define BEEP_MASK_FROM_REG(val) (val)
258 #define BEEP_MASK_TO_REG(val) ((val) & 0xffffff)
259 #define BEEP_ENABLE_TO_REG(val) ((val)?1:0)
260 #define BEEP_ENABLE_FROM_REG(val) ((val)?1:0)
262 #define DIV_FROM_REG(val) (1 << (val))
264 static inline u8 DIV_TO_REG(long val)
267 val = SENSORS_LIMIT(val, 1, 128) >> 1;
268 for (i = 0; i < 6; i++) {
276 /* For each registered chip, we need to keep some data in memory. That
277 data is pointed to by w83627hf_list[NR]->data. The structure itself is
278 dynamically allocated, at the same time when a new client is allocated. */
279 struct w83627hf_data {
280 struct i2c_client client;
281 struct semaphore lock;
284 struct semaphore update_lock;
285 char valid; /* !=0 if following fields are valid */
286 unsigned long last_updated; /* In jiffies */
288 struct i2c_client *lm75; /* for secondary I2C addresses */
289 /* pointer to array of 2 subclients */
291 u8 in[9]; /* Register value */
292 u8 in_max[9]; /* Register value */
293 u8 in_min[9]; /* Register value */
294 u8 fan[3]; /* Register value */
295 u8 fan_min[3]; /* Register value */
297 u8 temp_max; /* Register value */
298 u8 temp_max_hyst; /* Register value */
299 u16 temp_add[2]; /* Register value */
300 u16 temp_max_add[2]; /* Register value */
301 u16 temp_max_hyst_add[2]; /* Register value */
302 u8 fan_div[3]; /* Register encoding, shifted right */
303 u8 vid; /* Register encoding, combined */
304 u32 alarms; /* Register encoding, combined */
305 u32 beep_mask; /* Register encoding, combined */
306 u8 beep_enable; /* Boolean */
307 u8 pwm[3]; /* Register value */
308 u8 pwmenable[3]; /* bool */
309 u16 sens[3]; /* 782D/783S only.
310 1 = pentium diode; 2 = 3904 diode;
311 3000-5000 = thermistor beta.
313 Other Betas unimplemented */
318 static int w83627hf_attach_adapter(struct i2c_adapter *adapter);
319 static int w83627hf_detect(struct i2c_adapter *adapter, int address,
321 static int w83627hf_detach_client(struct i2c_client *client);
323 static int w83627hf_read_value(struct i2c_client *client, u16 register);
324 static int w83627hf_write_value(struct i2c_client *client, u16 register,
326 static struct w83627hf_data *w83627hf_update_device(struct device *dev);
327 static void w83627hf_init_client(struct i2c_client *client);
329 static struct i2c_driver w83627hf_driver = {
330 .owner = THIS_MODULE,
332 .id = I2C_DRIVERID_W83627HF,
333 .flags = I2C_DF_NOTIFY,
334 .attach_adapter = w83627hf_attach_adapter,
335 .detach_client = w83627hf_detach_client,
338 /* following are the sysfs callback functions */
339 #define show_in_reg(reg) \
340 static ssize_t show_##reg (struct device *dev, char *buf, int nr) \
342 struct w83627hf_data *data = w83627hf_update_device(dev); \
343 return sprintf(buf,"%ld\n", (long)IN_FROM_REG(data->reg[nr])); \
349 #define store_in_reg(REG, reg) \
351 store_in_##reg (struct device *dev, const char *buf, size_t count, int nr) \
353 struct i2c_client *client = to_i2c_client(dev); \
354 struct w83627hf_data *data = i2c_get_clientdata(client); \
357 val = simple_strtoul(buf, NULL, 10); \
358 data->in_##reg[nr] = IN_TO_REG(val); \
359 w83627hf_write_value(client, W83781D_REG_IN_##REG(nr), \
360 data->in_##reg[nr]); \
364 store_in_reg(MIN, min)
365 store_in_reg(MAX, max)
367 #define sysfs_in_offset(offset) \
369 show_regs_in_##offset (struct device *dev, char *buf) \
371 return show_in(dev, buf, 0x##offset); \
373 static DEVICE_ATTR(in##offset##_input, S_IRUGO, show_regs_in_##offset, NULL)
375 #define sysfs_in_reg_offset(reg, offset) \
376 static ssize_t show_regs_in_##reg##offset (struct device *dev, char *buf) \
378 return show_in_##reg (dev, buf, 0x##offset); \
381 store_regs_in_##reg##offset (struct device *dev, \
382 const char *buf, size_t count) \
384 return store_in_##reg (dev, buf, count, 0x##offset); \
386 static DEVICE_ATTR(in##offset##_##reg, S_IRUGO| S_IWUSR, \
387 show_regs_in_##reg##offset, store_regs_in_##reg##offset)
389 #define sysfs_in_offsets(offset) \
390 sysfs_in_offset(offset) \
391 sysfs_in_reg_offset(min, offset) \
392 sysfs_in_reg_offset(max, offset)
404 #define device_create_file_in(client, offset) \
406 device_create_file(&client->dev, &dev_attr_in##offset##_input); \
407 device_create_file(&client->dev, &dev_attr_in##offset##_min); \
408 device_create_file(&client->dev, &dev_attr_in##offset##_max); \
411 #define show_fan_reg(reg) \
412 static ssize_t show_##reg (struct device *dev, char *buf, int nr) \
414 struct w83627hf_data *data = w83627hf_update_device(dev); \
415 return sprintf(buf,"%ld\n", \
416 FAN_FROM_REG(data->reg[nr-1], \
417 (long)DIV_FROM_REG(data->fan_div[nr-1]))); \
420 show_fan_reg(fan_min)
423 store_fan_min(struct device *dev, const char *buf, size_t count, int nr)
425 struct i2c_client *client = to_i2c_client(dev);
426 struct w83627hf_data *data = i2c_get_clientdata(client);
429 val = simple_strtoul(buf, NULL, 10);
430 data->fan_min[nr - 1] =
431 FAN_TO_REG(val, DIV_FROM_REG(data->fan_div[nr - 1]));
432 w83627hf_write_value(client, W83781D_REG_FAN_MIN(nr),
433 data->fan_min[nr - 1]);
438 #define sysfs_fan_offset(offset) \
439 static ssize_t show_regs_fan_##offset (struct device *dev, char *buf) \
441 return show_fan(dev, buf, 0x##offset); \
443 static DEVICE_ATTR(fan##offset##_input, S_IRUGO, show_regs_fan_##offset, NULL)
445 #define sysfs_fan_min_offset(offset) \
446 static ssize_t show_regs_fan_min##offset (struct device *dev, char *buf) \
448 return show_fan_min(dev, buf, 0x##offset); \
451 store_regs_fan_min##offset (struct device *dev, const char *buf, size_t count) \
453 return store_fan_min(dev, buf, count, 0x##offset); \
455 static DEVICE_ATTR(fan##offset##_min, S_IRUGO | S_IWUSR, \
456 show_regs_fan_min##offset, store_regs_fan_min##offset)
459 sysfs_fan_min_offset(1)
461 sysfs_fan_min_offset(2)
463 sysfs_fan_min_offset(3)
465 #define device_create_file_fan(client, offset) \
467 device_create_file(&client->dev, &dev_attr_fan##offset##_input); \
468 device_create_file(&client->dev, &dev_attr_fan##offset##_min); \
471 #define show_temp_reg(reg) \
472 static ssize_t show_##reg (struct device *dev, char *buf, int nr) \
474 struct w83627hf_data *data = w83627hf_update_device(dev); \
475 if (nr >= 2) { /* TEMP2 and TEMP3 */ \
476 return sprintf(buf,"%ld\n", \
477 (long)LM75_TEMP_FROM_REG(data->reg##_add[nr-2])); \
478 } else { /* TEMP1 */ \
479 return sprintf(buf,"%ld\n", (long)TEMP_FROM_REG(data->reg)); \
483 show_temp_reg(temp_max)
484 show_temp_reg(temp_max_hyst)
486 #define store_temp_reg(REG, reg) \
488 store_temp_##reg (struct device *dev, const char *buf, size_t count, int nr) \
490 struct i2c_client *client = to_i2c_client(dev); \
491 struct w83627hf_data *data = i2c_get_clientdata(client); \
494 val = simple_strtoul(buf, NULL, 10); \
496 if (nr >= 2) { /* TEMP2 and TEMP3 */ \
497 data->temp_##reg##_add[nr-2] = LM75_TEMP_TO_REG(val); \
498 w83627hf_write_value(client, W83781D_REG_TEMP_##REG(nr), \
499 data->temp_##reg##_add[nr-2]); \
500 } else { /* TEMP1 */ \
501 data->temp_##reg = TEMP_TO_REG(val); \
502 w83627hf_write_value(client, W83781D_REG_TEMP_##REG(nr), \
508 store_temp_reg(OVER, max)
509 store_temp_reg(HYST, max_hyst)
511 #define sysfs_temp_offset(offset) \
513 show_regs_temp_##offset (struct device *dev, char *buf) \
515 return show_temp(dev, buf, 0x##offset); \
517 static DEVICE_ATTR(temp##offset##_input, S_IRUGO, show_regs_temp_##offset, NULL)
519 #define sysfs_temp_reg_offset(reg, offset) \
520 static ssize_t show_regs_temp_##reg##offset (struct device *dev, char *buf) \
522 return show_temp_##reg (dev, buf, 0x##offset); \
525 store_regs_temp_##reg##offset (struct device *dev, \
526 const char *buf, size_t count) \
528 return store_temp_##reg (dev, buf, count, 0x##offset); \
530 static DEVICE_ATTR(temp##offset##_##reg, S_IRUGO| S_IWUSR, \
531 show_regs_temp_##reg##offset, store_regs_temp_##reg##offset)
533 #define sysfs_temp_offsets(offset) \
534 sysfs_temp_offset(offset) \
535 sysfs_temp_reg_offset(max, offset) \
536 sysfs_temp_reg_offset(max_hyst, offset)
538 sysfs_temp_offsets(1)
539 sysfs_temp_offsets(2)
540 sysfs_temp_offsets(3)
542 #define device_create_file_temp(client, offset) \
544 device_create_file(&client->dev, &dev_attr_temp##offset##_input); \
545 device_create_file(&client->dev, &dev_attr_temp##offset##_max); \
546 device_create_file(&client->dev, &dev_attr_temp##offset##_max_hyst); \
550 show_vid_reg(struct device *dev, char *buf)
552 struct w83627hf_data *data = w83627hf_update_device(dev);
553 return sprintf(buf, "%ld\n", (long) vid_from_reg(data->vid, data->vrm));
555 static DEVICE_ATTR(in0_ref, S_IRUGO, show_vid_reg, NULL)
556 #define device_create_file_vid(client) \
557 device_create_file(&client->dev, &dev_attr_in0_ref)
560 show_vrm_reg(struct device *dev, char *buf)
562 struct w83627hf_data *data = w83627hf_update_device(dev);
563 return sprintf(buf, "%ld\n", (long) data->vrm);
566 store_vrm_reg(struct device *dev, const char *buf, size_t count)
568 struct i2c_client *client = to_i2c_client(dev);
569 struct w83627hf_data *data = i2c_get_clientdata(client);
572 val = simple_strtoul(buf, NULL, 10);
577 static DEVICE_ATTR(vrm, S_IRUGO | S_IWUSR, show_vrm_reg, store_vrm_reg)
578 #define device_create_file_vrm(client) \
579 device_create_file(&client->dev, &dev_attr_vrm)
582 show_alarms_reg(struct device *dev, char *buf)
584 struct w83627hf_data *data = w83627hf_update_device(dev);
585 return sprintf(buf, "%ld\n", (long) data->alarms);
587 static DEVICE_ATTR(alarms, S_IRUGO, show_alarms_reg, NULL)
588 #define device_create_file_alarms(client) \
589 device_create_file(&client->dev, &dev_attr_alarms)
591 #define show_beep_reg(REG, reg) \
592 static ssize_t show_beep_##reg (struct device *dev, char *buf) \
594 struct w83627hf_data *data = w83627hf_update_device(dev); \
595 return sprintf(buf,"%ld\n", \
596 (long)BEEP_##REG##_FROM_REG(data->beep_##reg)); \
598 show_beep_reg(ENABLE, enable)
599 show_beep_reg(MASK, mask)
601 #define BEEP_ENABLE 0 /* Store beep_enable */
602 #define BEEP_MASK 1 /* Store beep_mask */
605 store_beep_reg(struct device *dev, const char *buf, size_t count,
608 struct i2c_client *client = to_i2c_client(dev);
609 struct w83627hf_data *data = i2c_get_clientdata(client);
612 val = simple_strtoul(buf, NULL, 10);
614 if (update_mask == BEEP_MASK) { /* We are storing beep_mask */
615 data->beep_mask = BEEP_MASK_TO_REG(val);
616 w83627hf_write_value(client, W83781D_REG_BEEP_INTS1,
617 data->beep_mask & 0xff);
618 w83627hf_write_value(client, W83781D_REG_BEEP_INTS3,
619 ((data->beep_mask) >> 16) & 0xff);
620 val2 = (data->beep_mask >> 8) & 0x7f;
621 } else { /* We are storing beep_enable */
623 w83627hf_read_value(client, W83781D_REG_BEEP_INTS2) & 0x7f;
624 data->beep_enable = BEEP_ENABLE_TO_REG(val);
627 w83627hf_write_value(client, W83781D_REG_BEEP_INTS2,
628 val2 | data->beep_enable << 7);
633 #define sysfs_beep(REG, reg) \
634 static ssize_t show_regs_beep_##reg (struct device *dev, char *buf) \
636 return show_beep_##reg(dev, buf); \
639 store_regs_beep_##reg (struct device *dev, const char *buf, size_t count) \
641 return store_beep_reg(dev, buf, count, BEEP_##REG); \
643 static DEVICE_ATTR(beep_##reg, S_IRUGO | S_IWUSR, \
644 show_regs_beep_##reg, store_regs_beep_##reg)
646 sysfs_beep(ENABLE, enable)
647 sysfs_beep(MASK, mask)
649 #define device_create_file_beep(client) \
651 device_create_file(&client->dev, &dev_attr_beep_enable); \
652 device_create_file(&client->dev, &dev_attr_beep_mask); \
656 show_fan_div_reg(struct device *dev, char *buf, int nr)
658 struct w83627hf_data *data = w83627hf_update_device(dev);
659 return sprintf(buf, "%ld\n",
660 (long) DIV_FROM_REG(data->fan_div[nr - 1]));
663 /* Note: we save and restore the fan minimum here, because its value is
664 determined in part by the fan divisor. This follows the principle of
665 least suprise; the user doesn't expect the fan minimum to change just
666 because the divisor changed. */
668 store_fan_div_reg(struct device *dev, const char *buf, size_t count, int nr)
670 struct i2c_client *client = to_i2c_client(dev);
671 struct w83627hf_data *data = i2c_get_clientdata(client);
676 min = FAN_FROM_REG(data->fan_min[nr],
677 DIV_FROM_REG(data->fan_div[nr]));
679 data->fan_div[nr] = DIV_TO_REG(simple_strtoul(buf, NULL, 10));
681 reg = (w83627hf_read_value(client, nr==2 ? W83781D_REG_PIN : W83781D_REG_VID_FANDIV)
682 & (nr==0 ? 0xcf : 0x3f))
683 | ((data->fan_div[nr] & 0x03) << (nr==0 ? 4 : 6));
684 w83627hf_write_value(client, nr==2 ? W83781D_REG_PIN : W83781D_REG_VID_FANDIV, reg);
686 reg = (w83627hf_read_value(client, W83781D_REG_VBAT)
688 | ((data->fan_div[nr] & 0x04) << (3 + nr));
689 w83627hf_write_value(client, W83781D_REG_VBAT, reg);
691 /* Restore fan_min */
692 data->fan_min[nr] = FAN_TO_REG(min, DIV_FROM_REG(data->fan_div[nr]));
693 w83627hf_write_value(client, W83781D_REG_FAN_MIN(nr+1), data->fan_min[nr]);
698 #define sysfs_fan_div(offset) \
699 static ssize_t show_regs_fan_div_##offset (struct device *dev, char *buf) \
701 return show_fan_div_reg(dev, buf, offset); \
704 store_regs_fan_div_##offset (struct device *dev, \
705 const char *buf, size_t count) \
707 return store_fan_div_reg(dev, buf, count, offset - 1); \
709 static DEVICE_ATTR(fan##offset##_div, S_IRUGO | S_IWUSR, \
710 show_regs_fan_div_##offset, store_regs_fan_div_##offset)
716 #define device_create_file_fan_div(client, offset) \
718 device_create_file(&client->dev, &dev_attr_fan##offset##_div); \
722 show_pwm_reg(struct device *dev, char *buf, int nr)
724 struct w83627hf_data *data = w83627hf_update_device(dev);
725 return sprintf(buf, "%ld\n", (long) data->pwm[nr - 1]);
729 store_pwm_reg(struct device *dev, const char *buf, size_t count, int nr)
731 struct i2c_client *client = to_i2c_client(dev);
732 struct w83627hf_data *data = i2c_get_clientdata(client);
735 val = simple_strtoul(buf, NULL, 10);
737 if (data->type == w83627thf) {
738 /* bits 0-3 are reserved in 627THF */
739 data->pwm[nr - 1] = PWM_TO_REG(val) & 0xf0;
740 w83627hf_write_value(client,
741 W836X7HF_REG_PWM(data->type, nr),
743 (w83627hf_read_value(client,
744 W836X7HF_REG_PWM(data->type, nr)) & 0x0f));
746 data->pwm[nr - 1] = PWM_TO_REG(val);
747 w83627hf_write_value(client,
748 W836X7HF_REG_PWM(data->type, nr),
755 #define sysfs_pwm(offset) \
756 static ssize_t show_regs_pwm_##offset (struct device *dev, char *buf) \
758 return show_pwm_reg(dev, buf, offset); \
761 store_regs_pwm_##offset (struct device *dev, const char *buf, size_t count) \
763 return store_pwm_reg(dev, buf, count, offset); \
765 static DEVICE_ATTR(fan##offset##_pwm, S_IRUGO | S_IWUSR, \
766 show_regs_pwm_##offset, store_regs_pwm_##offset)
772 #define device_create_file_pwm(client, offset) \
774 device_create_file(&client->dev, &dev_attr_fan##offset##_pwm); \
778 show_sensor_reg(struct device *dev, char *buf, int nr)
780 struct w83627hf_data *data = w83627hf_update_device(dev);
781 return sprintf(buf, "%ld\n", (long) data->sens[nr - 1]);
785 store_sensor_reg(struct device *dev, const char *buf, size_t count, int nr)
787 struct i2c_client *client = to_i2c_client(dev);
788 struct w83627hf_data *data = i2c_get_clientdata(client);
791 val = simple_strtoul(buf, NULL, 10);
794 case 1: /* PII/Celeron diode */
795 tmp = w83627hf_read_value(client, W83781D_REG_SCFG1);
796 w83627hf_write_value(client, W83781D_REG_SCFG1,
797 tmp | BIT_SCFG1[nr - 1]);
798 tmp = w83627hf_read_value(client, W83781D_REG_SCFG2);
799 w83627hf_write_value(client, W83781D_REG_SCFG2,
800 tmp | BIT_SCFG2[nr - 1]);
801 data->sens[nr - 1] = val;
804 tmp = w83627hf_read_value(client, W83781D_REG_SCFG1);
805 w83627hf_write_value(client, W83781D_REG_SCFG1,
806 tmp | BIT_SCFG1[nr - 1]);
807 tmp = w83627hf_read_value(client, W83781D_REG_SCFG2);
808 w83627hf_write_value(client, W83781D_REG_SCFG2,
809 tmp & ~BIT_SCFG2[nr - 1]);
810 data->sens[nr - 1] = val;
812 case W83781D_DEFAULT_BETA: /* thermistor */
813 tmp = w83627hf_read_value(client, W83781D_REG_SCFG1);
814 w83627hf_write_value(client, W83781D_REG_SCFG1,
815 tmp & ~BIT_SCFG1[nr - 1]);
816 data->sens[nr - 1] = val;
819 dev_err(&client->dev,
820 "Invalid sensor type %ld; must be 1, 2, or %d\n",
821 (long) val, W83781D_DEFAULT_BETA);
828 #define sysfs_sensor(offset) \
829 static ssize_t show_regs_sensor_##offset (struct device *dev, char *buf) \
831 return show_sensor_reg(dev, buf, offset); \
834 store_regs_sensor_##offset (struct device *dev, const char *buf, size_t count) \
836 return store_sensor_reg(dev, buf, count, offset); \
838 static DEVICE_ATTR(temp##offset##_type, S_IRUGO | S_IWUSR, \
839 show_regs_sensor_##offset, store_regs_sensor_##offset)
845 #define device_create_file_sensor(client, offset) \
847 device_create_file(&client->dev, &dev_attr_temp##offset##_type); \
851 /* This function is called when:
852 * w83627hf_driver is inserted (when this module is loaded), for each
854 * when a new adapter is inserted (and w83627hf_driver is still present) */
855 static int w83627hf_attach_adapter(struct i2c_adapter *adapter)
857 return i2c_detect(adapter, &addr_data, w83627hf_detect);
860 static int w83627hf_find(int *address)
865 val= superio_inb(DEVID);
866 if(val != W627_DEVID &&
867 val != W627THF_DEVID &&
874 superio_select(W83627HF_LD_HWM);
875 val = (superio_inb(WINB_BASE_REG) << 8) |
876 superio_inb(WINB_BASE_REG + 1);
877 *address = val & ~(WINB_EXTENT - 1);
878 if (*address == 0 && force_addr == 0) {
883 *address = force_addr; /* so detect will get called */
889 int w83627hf_detect(struct i2c_adapter *adapter, int address,
893 struct i2c_client *new_client;
894 struct w83627hf_data *data;
896 const char *client_name = "";
898 if (!i2c_is_isa_adapter(adapter)) {
904 address = force_addr & ~(WINB_EXTENT - 1);
906 if (!request_region(address, WINB_EXTENT, "w83627hf")) {
912 printk("w83627hf.o: forcing ISA address 0x%04X\n", address);
914 superio_select(W83627HF_LD_HWM);
915 superio_outb(WINB_BASE_REG, address >> 8);
916 superio_outb(WINB_BASE_REG+1, address & 0xff);
921 val= superio_inb(DEVID);
922 if(val == W627_DEVID)
924 else if(val == W697_DEVID)
926 else if(val == W627THF_DEVID)
928 else if(val == W637_DEVID)
931 dev_info(&adapter->dev,
932 "Unsupported chip (dev_id=0x%02X).\n", val);
936 superio_select(W83627HF_LD_HWM);
937 if((val = 0x01 & superio_inb(WINB_ACT_REG)) == 0)
938 superio_outb(WINB_ACT_REG, 1);
941 /* OK. For now, we presume we have a valid client. We now create the
942 client structure, even though we cannot fill it completely yet.
943 But it allows us to access w83627hf_{read,write}_value. */
945 if (!(data = kmalloc(sizeof(struct w83627hf_data), GFP_KERNEL))) {
949 memset(data, 0, sizeof(struct w83627hf_data));
951 new_client = &data->client;
952 i2c_set_clientdata(new_client, data);
953 new_client->addr = address;
954 init_MUTEX(&data->lock);
955 new_client->adapter = adapter;
956 new_client->driver = &w83627hf_driver;
957 new_client->flags = 0;
960 if (kind == w83627hf) {
961 client_name = "w83627hf";
962 } else if (kind == w83627thf) {
963 client_name = "w83627thf";
964 } else if (kind == w83697hf) {
965 client_name = "w83697hf";
966 } else if (kind == w83637hf) {
967 client_name = "w83637hf";
970 /* Fill in the remaining client fields and put into the global list */
971 strlcpy(new_client->name, client_name, I2C_NAME_SIZE);
974 init_MUTEX(&data->update_lock);
976 /* Tell the I2C layer a new client has arrived */
977 if ((err = i2c_attach_client(new_client)))
982 /* Initialize the chip */
983 w83627hf_init_client(new_client);
985 /* A few vars need to be filled upon startup */
986 data->fan_min[0] = w83627hf_read_value(new_client, W83781D_REG_FAN_MIN(1));
987 data->fan_min[1] = w83627hf_read_value(new_client, W83781D_REG_FAN_MIN(2));
988 data->fan_min[2] = w83627hf_read_value(new_client, W83781D_REG_FAN_MIN(3));
990 /* Register sysfs hooks */
991 device_create_file_in(new_client, 0);
992 if (kind != w83697hf)
993 device_create_file_in(new_client, 1);
994 device_create_file_in(new_client, 2);
995 device_create_file_in(new_client, 3);
996 device_create_file_in(new_client, 4);
997 if (kind != w83627thf && kind != w83637hf) {
998 device_create_file_in(new_client, 5);
999 device_create_file_in(new_client, 6);
1001 device_create_file_in(new_client, 7);
1002 device_create_file_in(new_client, 8);
1004 device_create_file_fan(new_client, 1);
1005 device_create_file_fan(new_client, 2);
1006 if (kind != w83697hf)
1007 device_create_file_fan(new_client, 3);
1009 device_create_file_temp(new_client, 1);
1010 device_create_file_temp(new_client, 2);
1011 if (kind != w83697hf)
1012 device_create_file_temp(new_client, 3);
1014 if (kind != w83697hf)
1015 device_create_file_vid(new_client);
1017 if (kind != w83697hf)
1018 device_create_file_vrm(new_client);
1020 device_create_file_fan_div(new_client, 1);
1021 device_create_file_fan_div(new_client, 2);
1022 if (kind != w83697hf)
1023 device_create_file_fan_div(new_client, 3);
1025 device_create_file_alarms(new_client);
1027 device_create_file_beep(new_client);
1029 device_create_file_pwm(new_client, 1);
1030 device_create_file_pwm(new_client, 2);
1031 if (kind == w83627thf || kind == w83637hf)
1032 device_create_file_pwm(new_client, 3);
1034 device_create_file_sensor(new_client, 1);
1035 device_create_file_sensor(new_client, 2);
1036 if (kind != w83697hf)
1037 device_create_file_sensor(new_client, 3);
1044 release_region(address, WINB_EXTENT);
1049 static int w83627hf_detach_client(struct i2c_client *client)
1053 if ((err = i2c_detach_client(client))) {
1054 dev_err(&client->dev,
1055 "Client deregistration failed, client not detached.\n");
1059 release_region(client->addr, WINB_EXTENT);
1060 kfree(i2c_get_clientdata(client));
1067 ISA access must always be locked explicitly!
1068 We ignore the W83781D BUSY flag at this moment - it could lead to deadlocks,
1069 would slow down the W83781D access and should not be necessary.
1070 There are some ugly typecasts here, but the good news is - they should
1071 nowhere else be necessary! */
1072 static int w83627hf_read_value(struct i2c_client *client, u16 reg)
1074 struct w83627hf_data *data = i2c_get_clientdata(client);
1075 int res, word_sized;
1078 word_sized = (((reg & 0xff00) == 0x100)
1079 || ((reg & 0xff00) == 0x200))
1080 && (((reg & 0x00ff) == 0x50)
1081 || ((reg & 0x00ff) == 0x53)
1082 || ((reg & 0x00ff) == 0x55));
1084 outb_p(W83781D_REG_BANK,
1085 client->addr + W83781D_ADDR_REG_OFFSET);
1087 client->addr + W83781D_DATA_REG_OFFSET);
1089 outb_p(reg & 0xff, client->addr + W83781D_ADDR_REG_OFFSET);
1090 res = inb_p(client->addr + W83781D_DATA_REG_OFFSET);
1092 outb_p((reg & 0xff) + 1,
1093 client->addr + W83781D_ADDR_REG_OFFSET);
1095 (res << 8) + inb_p(client->addr +
1096 W83781D_DATA_REG_OFFSET);
1099 outb_p(W83781D_REG_BANK,
1100 client->addr + W83781D_ADDR_REG_OFFSET);
1101 outb_p(0, client->addr + W83781D_DATA_REG_OFFSET);
1107 static int w83627thf_read_gpio5(struct i2c_client *client)
1109 struct w83627hf_data *data = i2c_get_clientdata(client);
1114 superio_select(W83627HF_LD_GPIO5);
1115 res = superio_inb(W83627THF_GPIO5_DR);
1116 inv = superio_inb(W83627THF_GPIO5_INVR);
1122 static int w83627hf_write_value(struct i2c_client *client, u16 reg, u16 value)
1124 struct w83627hf_data *data = i2c_get_clientdata(client);
1128 word_sized = (((reg & 0xff00) == 0x100)
1129 || ((reg & 0xff00) == 0x200))
1130 && (((reg & 0x00ff) == 0x53)
1131 || ((reg & 0x00ff) == 0x55));
1133 outb_p(W83781D_REG_BANK,
1134 client->addr + W83781D_ADDR_REG_OFFSET);
1136 client->addr + W83781D_DATA_REG_OFFSET);
1138 outb_p(reg & 0xff, client->addr + W83781D_ADDR_REG_OFFSET);
1141 client->addr + W83781D_DATA_REG_OFFSET);
1142 outb_p((reg & 0xff) + 1,
1143 client->addr + W83781D_ADDR_REG_OFFSET);
1145 outb_p(value & 0xff,
1146 client->addr + W83781D_DATA_REG_OFFSET);
1148 outb_p(W83781D_REG_BANK,
1149 client->addr + W83781D_ADDR_REG_OFFSET);
1150 outb_p(0, client->addr + W83781D_DATA_REG_OFFSET);
1156 /* Called when we have found a new W83781D. It should set limits, etc. */
1157 static void w83627hf_init_client(struct i2c_client *client)
1159 struct w83627hf_data *data = i2c_get_clientdata(client);
1161 int type = data->type;
1165 /* save this register */
1166 i = w83627hf_read_value(client, W83781D_REG_BEEP_CONFIG);
1167 /* Reset all except Watchdog values and last conversion values
1168 This sets fan-divs to 2, among others */
1169 w83627hf_write_value(client, W83781D_REG_CONFIG, 0x80);
1170 /* Restore the register and disable power-on abnormal beep.
1171 This saves FAN 1/2/3 input/output values set by BIOS. */
1172 w83627hf_write_value(client, W83781D_REG_BEEP_CONFIG, i | 0x80);
1173 /* Disable master beep-enable (reset turns it on).
1174 Individual beeps should be reset to off but for some reason
1175 disabling this bit helps some people not get beeped */
1176 w83627hf_write_value(client, W83781D_REG_BEEP_INTS2, 0);
1179 /* Minimize conflicts with other winbond i2c-only clients... */
1180 /* disable i2c subclients... how to disable main i2c client?? */
1181 /* force i2c address to relatively uncommon address */
1182 w83627hf_write_value(client, W83781D_REG_I2C_SUBADDR, 0x89);
1183 w83627hf_write_value(client, W83781D_REG_I2C_ADDR, force_i2c);
1185 /* Read VID only once */
1186 if (w83627hf == data->type || w83637hf == data->type) {
1187 int lo = w83627hf_read_value(client, W83781D_REG_VID_FANDIV);
1188 int hi = w83627hf_read_value(client, W83781D_REG_CHIPID);
1189 data->vid = (lo & 0x0f) | ((hi & 0x01) << 4);
1190 } else if (w83627thf == data->type) {
1191 data->vid = w83627thf_read_gpio5(client) & 0x1f;
1194 /* Convert VID to voltage based on default VRM */
1195 data->vrm = DEFAULT_VRM;
1196 if (type != w83697hf)
1197 vid = vid_from_reg(vid, data->vrm);
1199 tmp = w83627hf_read_value(client, W83781D_REG_SCFG1);
1200 for (i = 1; i <= 3; i++) {
1201 if (!(tmp & BIT_SCFG1[i - 1])) {
1202 data->sens[i - 1] = W83781D_DEFAULT_BETA;
1204 if (w83627hf_read_value
1206 W83781D_REG_SCFG2) & BIT_SCFG2[i - 1])
1207 data->sens[i - 1] = 1;
1209 data->sens[i - 1] = 2;
1211 if ((type == w83697hf) && (i == 2))
1215 data->pwmenable[0] = 1;
1216 data->pwmenable[1] = 1;
1217 data->pwmenable[2] = 1;
1220 if (type == w83627hf) {
1221 /* enable PWM2 control (can't hurt since PWM reg
1222 should have been reset to 0xff) */
1223 w83627hf_write_value(client, W83627HF_REG_PWMCLK12,
1226 /* enable comparator mode for temp2 and temp3 so
1227 alarm indication will work correctly */
1228 i = w83627hf_read_value(client, W83781D_REG_IRQ);
1230 w83627hf_write_value(client, W83781D_REG_IRQ,
1234 /* Start monitoring */
1235 w83627hf_write_value(client, W83781D_REG_CONFIG,
1236 (w83627hf_read_value(client,
1237 W83781D_REG_CONFIG) & 0xf7)
1241 static struct w83627hf_data *w83627hf_update_device(struct device *dev)
1243 struct i2c_client *client = to_i2c_client(dev);
1244 struct w83627hf_data *data = i2c_get_clientdata(client);
1247 down(&data->update_lock);
1249 if ((jiffies - data->last_updated > HZ + HZ / 2) ||
1250 (jiffies < data->last_updated) || !data->valid) {
1251 for (i = 0; i <= 8; i++) {
1252 /* skip missing sensors */
1253 if (((data->type == w83697hf) && (i == 1)) ||
1254 ((data->type == w83627thf || data->type == w83637hf)
1255 && (i == 4 || i == 5)))
1258 w83627hf_read_value(client, W83781D_REG_IN(i));
1260 w83627hf_read_value(client,
1261 W83781D_REG_IN_MIN(i));
1263 w83627hf_read_value(client,
1264 W83781D_REG_IN_MAX(i));
1266 for (i = 1; i <= 3; i++) {
1268 w83627hf_read_value(client, W83781D_REG_FAN(i));
1269 data->fan_min[i - 1] =
1270 w83627hf_read_value(client,
1271 W83781D_REG_FAN_MIN(i));
1273 for (i = 1; i <= 3; i++) {
1274 u8 tmp = w83627hf_read_value(client,
1275 W836X7HF_REG_PWM(data->type, i));
1276 /* bits 0-3 are reserved in 627THF */
1277 if (data->type == w83627thf)
1279 data->pwm[i - 1] = tmp;
1281 (data->type == w83627hf || data->type == w83697hf))
1285 data->temp = w83627hf_read_value(client, W83781D_REG_TEMP(1));
1287 w83627hf_read_value(client, W83781D_REG_TEMP_OVER(1));
1288 data->temp_max_hyst =
1289 w83627hf_read_value(client, W83781D_REG_TEMP_HYST(1));
1291 w83627hf_read_value(client, W83781D_REG_TEMP(2));
1292 data->temp_max_add[0] =
1293 w83627hf_read_value(client, W83781D_REG_TEMP_OVER(2));
1294 data->temp_max_hyst_add[0] =
1295 w83627hf_read_value(client, W83781D_REG_TEMP_HYST(2));
1296 if (data->type != w83697hf) {
1298 w83627hf_read_value(client, W83781D_REG_TEMP(3));
1299 data->temp_max_add[1] =
1300 w83627hf_read_value(client, W83781D_REG_TEMP_OVER(3));
1301 data->temp_max_hyst_add[1] =
1302 w83627hf_read_value(client, W83781D_REG_TEMP_HYST(3));
1305 i = w83627hf_read_value(client, W83781D_REG_VID_FANDIV);
1306 data->fan_div[0] = (i >> 4) & 0x03;
1307 data->fan_div[1] = (i >> 6) & 0x03;
1308 if (data->type != w83697hf) {
1309 data->fan_div[2] = (w83627hf_read_value(client,
1310 W83781D_REG_PIN) >> 6) & 0x03;
1312 i = w83627hf_read_value(client, W83781D_REG_VBAT);
1313 data->fan_div[0] |= (i >> 3) & 0x04;
1314 data->fan_div[1] |= (i >> 4) & 0x04;
1315 if (data->type != w83697hf)
1316 data->fan_div[2] |= (i >> 5) & 0x04;
1318 w83627hf_read_value(client, W83781D_REG_ALARM1) |
1319 (w83627hf_read_value(client, W83781D_REG_ALARM2) << 8) |
1320 (w83627hf_read_value(client, W83781D_REG_ALARM3) << 16);
1321 i = w83627hf_read_value(client, W83781D_REG_BEEP_INTS2);
1322 data->beep_enable = i >> 7;
1323 data->beep_mask = ((i & 0x7f) << 8) |
1324 w83627hf_read_value(client, W83781D_REG_BEEP_INTS1) |
1325 w83627hf_read_value(client, W83781D_REG_BEEP_INTS3) << 16;
1326 data->last_updated = jiffies;
1330 up(&data->update_lock);
1335 static int __init sensors_w83627hf_init(void)
1339 if (w83627hf_find(&addr)) {
1342 normal_isa[0] = addr;
1344 return i2c_add_driver(&w83627hf_driver);
1347 static void __exit sensors_w83627hf_exit(void)
1349 i2c_del_driver(&w83627hf_driver);
1352 MODULE_AUTHOR("Frodo Looijaard <frodol@dds.nl>, "
1353 "Philip Edelbrock <phil@netroedge.com>, "
1354 "and Mark Studebaker <mdsxyz123@yahoo.com>");
1355 MODULE_DESCRIPTION("W83627HF driver");
1356 MODULE_LICENSE("GPL");
1358 module_init(sensors_w83627hf_init);
1359 module_exit(sensors_w83627hf_exit);