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>
8 Copyright (c) 2007 Jean Delvare <khali@linux-fr.org>
10 This program is free software; you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation; either version 2 of the License, or
13 (at your option) any later version.
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
20 You should have received a copy of the GNU General Public License
21 along with this program; if not, write to the Free Software
22 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
26 Supports following chips:
28 Chip #vin #fanin #pwm #temp wchipid vendid i2c ISA
29 w83627hf 9 3 2 3 0x20 0x5ca3 no yes(LPC)
30 w83627thf 7 3 3 3 0x90 0x5ca3 no yes(LPC)
31 w83637hf 7 3 3 3 0x80 0x5ca3 no yes(LPC)
32 w83687thf 7 3 3 3 0x90 0x5ca3 no yes(LPC)
33 w83697hf 8 2 2 2 0x60 0x5ca3 no yes(LPC)
35 For other winbond chips, and for i2c support in the above chips,
38 Note: automatic ("cruise") fan control for 697, 637 & 627thf not
42 #include <linux/module.h>
43 #include <linux/init.h>
44 #include <linux/slab.h>
45 #include <linux/jiffies.h>
46 #include <linux/platform_device.h>
47 #include <linux/hwmon.h>
48 #include <linux/hwmon-vid.h>
49 #include <linux/err.h>
50 #include <linux/mutex.h>
51 #include <linux/ioport.h>
55 static struct platform_device *pdev;
57 #define DRVNAME "w83627hf"
58 enum chips { w83627hf, w83627thf, w83697hf, w83637hf, w83687thf };
60 static u16 force_addr;
61 module_param(force_addr, ushort, 0);
62 MODULE_PARM_DESC(force_addr,
63 "Initialize the base address of the sensors");
64 static u8 force_i2c = 0x1f;
65 module_param(force_i2c, byte, 0);
66 MODULE_PARM_DESC(force_i2c,
67 "Initialize the i2c address of the sensors");
70 module_param(reset, bool, 0);
71 MODULE_PARM_DESC(reset, "Set to one to reset chip on load");
74 module_param(init, bool, 0);
75 MODULE_PARM_DESC(init, "Set to zero to bypass chip initialization");
77 /* modified from kernel/include/traps.c */
78 static int REG; /* The register to read/write */
79 #define DEV 0x07 /* Register: Logical device select */
80 static int VAL; /* The value to read/write */
82 /* logical device numbers for superio_select (below) */
83 #define W83627HF_LD_FDC 0x00
84 #define W83627HF_LD_PRT 0x01
85 #define W83627HF_LD_UART1 0x02
86 #define W83627HF_LD_UART2 0x03
87 #define W83627HF_LD_KBC 0x05
88 #define W83627HF_LD_CIR 0x06 /* w83627hf only */
89 #define W83627HF_LD_GAME 0x07
90 #define W83627HF_LD_MIDI 0x07
91 #define W83627HF_LD_GPIO1 0x07
92 #define W83627HF_LD_GPIO5 0x07 /* w83627thf only */
93 #define W83627HF_LD_GPIO2 0x08
94 #define W83627HF_LD_GPIO3 0x09
95 #define W83627HF_LD_GPIO4 0x09 /* w83627thf only */
96 #define W83627HF_LD_ACPI 0x0a
97 #define W83627HF_LD_HWM 0x0b
99 #define DEVID 0x20 /* Register: Device ID */
101 #define W83627THF_GPIO5_EN 0x30 /* w83627thf only */
102 #define W83627THF_GPIO5_IOSR 0xf3 /* w83627thf only */
103 #define W83627THF_GPIO5_DR 0xf4 /* w83627thf only */
105 #define W83687THF_VID_EN 0x29 /* w83687thf only */
106 #define W83687THF_VID_CFG 0xF0 /* w83687thf only */
107 #define W83687THF_VID_DATA 0xF1 /* w83687thf only */
110 superio_outb(int reg, int val)
124 superio_select(int ld)
143 #define W627_DEVID 0x52
144 #define W627THF_DEVID 0x82
145 #define W697_DEVID 0x60
146 #define W637_DEVID 0x70
147 #define W687THF_DEVID 0x85
148 #define WINB_ACT_REG 0x30
149 #define WINB_BASE_REG 0x60
150 /* Constants specified below */
152 /* Alignment of the base address */
153 #define WINB_ALIGNMENT ~7
155 /* Offset & size of I/O region we are interested in */
156 #define WINB_REGION_OFFSET 5
157 #define WINB_REGION_SIZE 2
159 /* Where are the sensors address/data registers relative to the region offset */
160 #define W83781D_ADDR_REG_OFFSET 0
161 #define W83781D_DATA_REG_OFFSET 1
163 /* The W83781D registers */
164 /* The W83782D registers for nr=7,8 are in bank 5 */
165 #define W83781D_REG_IN_MAX(nr) ((nr < 7) ? (0x2b + (nr) * 2) : \
166 (0x554 + (((nr) - 7) * 2)))
167 #define W83781D_REG_IN_MIN(nr) ((nr < 7) ? (0x2c + (nr) * 2) : \
168 (0x555 + (((nr) - 7) * 2)))
169 #define W83781D_REG_IN(nr) ((nr < 7) ? (0x20 + (nr)) : \
172 #define W83781D_REG_FAN_MIN(nr) (0x3a + (nr))
173 #define W83781D_REG_FAN(nr) (0x27 + (nr))
175 #define W83781D_REG_TEMP2_CONFIG 0x152
176 #define W83781D_REG_TEMP3_CONFIG 0x252
177 #define W83781D_REG_TEMP(nr) ((nr == 3) ? (0x0250) : \
178 ((nr == 2) ? (0x0150) : \
180 #define W83781D_REG_TEMP_HYST(nr) ((nr == 3) ? (0x253) : \
181 ((nr == 2) ? (0x153) : \
183 #define W83781D_REG_TEMP_OVER(nr) ((nr == 3) ? (0x255) : \
184 ((nr == 2) ? (0x155) : \
187 #define W83781D_REG_BANK 0x4E
189 #define W83781D_REG_CONFIG 0x40
190 #define W83781D_REG_ALARM1 0x459
191 #define W83781D_REG_ALARM2 0x45A
192 #define W83781D_REG_ALARM3 0x45B
194 #define W83781D_REG_BEEP_CONFIG 0x4D
195 #define W83781D_REG_BEEP_INTS1 0x56
196 #define W83781D_REG_BEEP_INTS2 0x57
197 #define W83781D_REG_BEEP_INTS3 0x453
199 #define W83781D_REG_VID_FANDIV 0x47
201 #define W83781D_REG_CHIPID 0x49
202 #define W83781D_REG_WCHIPID 0x58
203 #define W83781D_REG_CHIPMAN 0x4F
204 #define W83781D_REG_PIN 0x4B
206 #define W83781D_REG_VBAT 0x5D
208 #define W83627HF_REG_PWM1 0x5A
209 #define W83627HF_REG_PWM2 0x5B
211 #define W83627THF_REG_PWM1 0x01 /* 697HF/637HF/687THF too */
212 #define W83627THF_REG_PWM2 0x03 /* 697HF/637HF/687THF too */
213 #define W83627THF_REG_PWM3 0x11 /* 637HF/687THF too */
215 #define W83627THF_REG_VRM_OVT_CFG 0x18 /* 637HF/687THF too */
217 static const u8 regpwm_627hf[] = { W83627HF_REG_PWM1, W83627HF_REG_PWM2 };
218 static const u8 regpwm[] = { W83627THF_REG_PWM1, W83627THF_REG_PWM2,
219 W83627THF_REG_PWM3 };
220 #define W836X7HF_REG_PWM(type, nr) (((type) == w83627hf) ? \
221 regpwm_627hf[(nr) - 1] : regpwm[(nr) - 1])
223 #define W83627HF_REG_PWM_FREQ 0x5C /* Only for the 627HF */
225 #define W83637HF_REG_PWM_FREQ1 0x00 /* 697HF/687THF too */
226 #define W83637HF_REG_PWM_FREQ2 0x02 /* 697HF/687THF too */
227 #define W83637HF_REG_PWM_FREQ3 0x10 /* 687THF too */
229 static const u8 W83637HF_REG_PWM_FREQ[] = { W83637HF_REG_PWM_FREQ1,
230 W83637HF_REG_PWM_FREQ2,
231 W83637HF_REG_PWM_FREQ3 };
233 #define W83627HF_BASE_PWM_FREQ 46870
235 #define W83781D_REG_I2C_ADDR 0x48
236 #define W83781D_REG_I2C_SUBADDR 0x4A
238 /* Sensor selection */
239 #define W83781D_REG_SCFG1 0x5D
240 static const u8 BIT_SCFG1[] = { 0x02, 0x04, 0x08 };
241 #define W83781D_REG_SCFG2 0x59
242 static const u8 BIT_SCFG2[] = { 0x10, 0x20, 0x40 };
243 #define W83781D_DEFAULT_BETA 3435
245 /* Conversions. Limit checking is only done on the TO_REG
246 variants. Note that you should be a bit careful with which arguments
247 these macros are called: arguments may be evaluated more than once.
248 Fixing this is just not worth it. */
249 #define IN_TO_REG(val) (SENSORS_LIMIT((((val) + 8)/16),0,255))
250 #define IN_FROM_REG(val) ((val) * 16)
252 static inline u8 FAN_TO_REG(long rpm, int div)
256 rpm = SENSORS_LIMIT(rpm, 1, 1000000);
257 return SENSORS_LIMIT((1350000 + rpm * div / 2) / (rpm * div), 1,
261 #define TEMP_MIN (-128000)
262 #define TEMP_MAX ( 127000)
264 /* TEMP: 0.001C/bit (-128C to +127C)
265 REG: 1C/bit, two's complement */
266 static u8 TEMP_TO_REG(int temp)
268 int ntemp = SENSORS_LIMIT(temp, TEMP_MIN, TEMP_MAX);
269 ntemp += (ntemp<0 ? -500 : 500);
270 return (u8)(ntemp / 1000);
273 static int TEMP_FROM_REG(u8 reg)
275 return (s8)reg * 1000;
278 #define FAN_FROM_REG(val,div) ((val)==0?-1:(val)==255?0:1350000/((val)*(div)))
280 #define PWM_TO_REG(val) (SENSORS_LIMIT((val),0,255))
282 static inline unsigned long pwm_freq_from_reg_627hf(u8 reg)
285 freq = W83627HF_BASE_PWM_FREQ >> reg;
288 static inline u8 pwm_freq_to_reg_627hf(unsigned long val)
291 /* Only 5 dividers (1 2 4 8 16)
292 Search for the nearest available frequency */
293 for (i = 0; i < 4; i++) {
294 if (val > (((W83627HF_BASE_PWM_FREQ >> i) +
295 (W83627HF_BASE_PWM_FREQ >> (i+1))) / 2))
301 static inline unsigned long pwm_freq_from_reg(u8 reg)
303 /* Clock bit 8 -> 180 kHz or 24 MHz */
304 unsigned long clock = (reg & 0x80) ? 180000UL : 24000000UL;
307 /* This should not happen but anyway... */
310 return (clock / (reg << 8));
312 static inline u8 pwm_freq_to_reg(unsigned long val)
314 /* Minimum divider value is 0x01 and maximum is 0x7F */
315 if (val >= 93750) /* The highest we can do */
317 if (val >= 720) /* Use 24 MHz clock */
318 return (24000000UL / (val << 8));
319 if (val < 6) /* The lowest we can do */
321 else /* Use 180 kHz clock */
322 return (0x80 | (180000UL / (val << 8)));
325 #define BEEP_MASK_FROM_REG(val) (val)
326 #define BEEP_MASK_TO_REG(val) ((val) & 0xffffff)
327 #define BEEP_ENABLE_TO_REG(val) ((val)?1:0)
328 #define BEEP_ENABLE_FROM_REG(val) ((val)?1:0)
330 #define DIV_FROM_REG(val) (1 << (val))
332 static inline u8 DIV_TO_REG(long val)
335 val = SENSORS_LIMIT(val, 1, 128) >> 1;
336 for (i = 0; i < 7; i++) {
344 /* For each registered chip, we need to keep some data in memory.
345 The structure is dynamically allocated. */
346 struct w83627hf_data {
349 struct class_device *class_dev;
353 struct mutex update_lock;
354 char valid; /* !=0 if following fields are valid */
355 unsigned long last_updated; /* In jiffies */
357 u8 in[9]; /* Register value */
358 u8 in_max[9]; /* Register value */
359 u8 in_min[9]; /* Register value */
360 u8 fan[3]; /* Register value */
361 u8 fan_min[3]; /* Register value */
363 u8 temp_max; /* Register value */
364 u8 temp_max_hyst; /* Register value */
365 u16 temp_add[2]; /* Register value */
366 u16 temp_max_add[2]; /* Register value */
367 u16 temp_max_hyst_add[2]; /* Register value */
368 u8 fan_div[3]; /* Register encoding, shifted right */
369 u8 vid; /* Register encoding, combined */
370 u32 alarms; /* Register encoding, combined */
371 u32 beep_mask; /* Register encoding, combined */
372 u8 beep_enable; /* Boolean */
373 u8 pwm[3]; /* Register value */
374 u8 pwm_freq[3]; /* Register value */
375 u16 sens[3]; /* 782D/783S only.
376 1 = pentium diode; 2 = 3904 diode;
377 3000-5000 = thermistor beta.
379 Other Betas unimplemented */
381 u8 vrm_ovt; /* Register value, 627THF/637HF/687THF only */
384 struct w83627hf_sio_data {
389 static int w83627hf_probe(struct platform_device *pdev);
390 static int w83627hf_remove(struct platform_device *pdev);
392 static int w83627hf_read_value(struct w83627hf_data *data, u16 reg);
393 static int w83627hf_write_value(struct w83627hf_data *data, u16 reg, u16 value);
394 static struct w83627hf_data *w83627hf_update_device(struct device *dev);
395 static void w83627hf_init_device(struct platform_device *pdev);
397 static struct platform_driver w83627hf_driver = {
399 .owner = THIS_MODULE,
402 .probe = w83627hf_probe,
403 .remove = __devexit_p(w83627hf_remove),
406 /* following are the sysfs callback functions */
407 #define show_in_reg(reg) \
408 static ssize_t show_##reg (struct device *dev, char *buf, int nr) \
410 struct w83627hf_data *data = w83627hf_update_device(dev); \
411 return sprintf(buf,"%ld\n", (long)IN_FROM_REG(data->reg[nr])); \
417 #define store_in_reg(REG, reg) \
419 store_in_##reg (struct device *dev, const char *buf, size_t count, int nr) \
421 struct w83627hf_data *data = dev_get_drvdata(dev); \
424 val = simple_strtoul(buf, NULL, 10); \
426 mutex_lock(&data->update_lock); \
427 data->in_##reg[nr] = IN_TO_REG(val); \
428 w83627hf_write_value(data, W83781D_REG_IN_##REG(nr), \
429 data->in_##reg[nr]); \
431 mutex_unlock(&data->update_lock); \
434 store_in_reg(MIN, min)
435 store_in_reg(MAX, max)
437 #define sysfs_in_offset(offset) \
439 show_regs_in_##offset (struct device *dev, struct device_attribute *attr, char *buf) \
441 return show_in(dev, buf, offset); \
443 static DEVICE_ATTR(in##offset##_input, S_IRUGO, show_regs_in_##offset, NULL);
445 #define sysfs_in_reg_offset(reg, offset) \
446 static ssize_t show_regs_in_##reg##offset (struct device *dev, struct device_attribute *attr, char *buf) \
448 return show_in_##reg (dev, buf, offset); \
451 store_regs_in_##reg##offset (struct device *dev, struct device_attribute *attr, \
452 const char *buf, size_t count) \
454 return store_in_##reg (dev, buf, count, offset); \
456 static DEVICE_ATTR(in##offset##_##reg, S_IRUGO| S_IWUSR, \
457 show_regs_in_##reg##offset, store_regs_in_##reg##offset);
459 #define sysfs_in_offsets(offset) \
460 sysfs_in_offset(offset) \
461 sysfs_in_reg_offset(min, offset) \
462 sysfs_in_reg_offset(max, offset)
473 /* use a different set of functions for in0 */
474 static ssize_t show_in_0(struct w83627hf_data *data, char *buf, u8 reg)
478 if ((data->vrm_ovt & 0x01) &&
479 (w83627thf == data->type || w83637hf == data->type
480 || w83687thf == data->type))
482 /* use VRM9 calculation */
483 in0 = (long)((reg * 488 + 70000 + 50) / 100);
485 /* use VRM8 (standard) calculation */
486 in0 = (long)IN_FROM_REG(reg);
488 return sprintf(buf,"%ld\n", in0);
491 static ssize_t show_regs_in_0(struct device *dev, struct device_attribute *attr, char *buf)
493 struct w83627hf_data *data = w83627hf_update_device(dev);
494 return show_in_0(data, buf, data->in[0]);
497 static ssize_t show_regs_in_min0(struct device *dev, struct device_attribute *attr, char *buf)
499 struct w83627hf_data *data = w83627hf_update_device(dev);
500 return show_in_0(data, buf, data->in_min[0]);
503 static ssize_t show_regs_in_max0(struct device *dev, struct device_attribute *attr, char *buf)
505 struct w83627hf_data *data = w83627hf_update_device(dev);
506 return show_in_0(data, buf, data->in_max[0]);
509 static ssize_t store_regs_in_min0(struct device *dev, struct device_attribute *attr,
510 const char *buf, size_t count)
512 struct w83627hf_data *data = dev_get_drvdata(dev);
515 val = simple_strtoul(buf, NULL, 10);
517 mutex_lock(&data->update_lock);
519 if ((data->vrm_ovt & 0x01) &&
520 (w83627thf == data->type || w83637hf == data->type
521 || w83687thf == data->type))
523 /* use VRM9 calculation */
525 SENSORS_LIMIT(((val * 100) - 70000 + 244) / 488, 0,
528 /* use VRM8 (standard) calculation */
529 data->in_min[0] = IN_TO_REG(val);
531 w83627hf_write_value(data, W83781D_REG_IN_MIN(0), data->in_min[0]);
532 mutex_unlock(&data->update_lock);
536 static ssize_t store_regs_in_max0(struct device *dev, struct device_attribute *attr,
537 const char *buf, size_t count)
539 struct w83627hf_data *data = dev_get_drvdata(dev);
542 val = simple_strtoul(buf, NULL, 10);
544 mutex_lock(&data->update_lock);
546 if ((data->vrm_ovt & 0x01) &&
547 (w83627thf == data->type || w83637hf == data->type
548 || w83687thf == data->type))
550 /* use VRM9 calculation */
552 SENSORS_LIMIT(((val * 100) - 70000 + 244) / 488, 0,
555 /* use VRM8 (standard) calculation */
556 data->in_max[0] = IN_TO_REG(val);
558 w83627hf_write_value(data, W83781D_REG_IN_MAX(0), data->in_max[0]);
559 mutex_unlock(&data->update_lock);
563 static DEVICE_ATTR(in0_input, S_IRUGO, show_regs_in_0, NULL);
564 static DEVICE_ATTR(in0_min, S_IRUGO | S_IWUSR,
565 show_regs_in_min0, store_regs_in_min0);
566 static DEVICE_ATTR(in0_max, S_IRUGO | S_IWUSR,
567 show_regs_in_max0, store_regs_in_max0);
569 #define show_fan_reg(reg) \
570 static ssize_t show_##reg (struct device *dev, char *buf, int nr) \
572 struct w83627hf_data *data = w83627hf_update_device(dev); \
573 return sprintf(buf,"%ld\n", \
574 FAN_FROM_REG(data->reg[nr-1], \
575 (long)DIV_FROM_REG(data->fan_div[nr-1]))); \
578 show_fan_reg(fan_min);
581 store_fan_min(struct device *dev, const char *buf, size_t count, int nr)
583 struct w83627hf_data *data = dev_get_drvdata(dev);
586 val = simple_strtoul(buf, NULL, 10);
588 mutex_lock(&data->update_lock);
589 data->fan_min[nr - 1] =
590 FAN_TO_REG(val, DIV_FROM_REG(data->fan_div[nr - 1]));
591 w83627hf_write_value(data, W83781D_REG_FAN_MIN(nr),
592 data->fan_min[nr - 1]);
594 mutex_unlock(&data->update_lock);
598 #define sysfs_fan_offset(offset) \
599 static ssize_t show_regs_fan_##offset (struct device *dev, struct device_attribute *attr, char *buf) \
601 return show_fan(dev, buf, offset); \
603 static DEVICE_ATTR(fan##offset##_input, S_IRUGO, show_regs_fan_##offset, NULL);
605 #define sysfs_fan_min_offset(offset) \
606 static ssize_t show_regs_fan_min##offset (struct device *dev, struct device_attribute *attr, char *buf) \
608 return show_fan_min(dev, buf, offset); \
611 store_regs_fan_min##offset (struct device *dev, struct device_attribute *attr, const char *buf, size_t count) \
613 return store_fan_min(dev, buf, count, offset); \
615 static DEVICE_ATTR(fan##offset##_min, S_IRUGO | S_IWUSR, \
616 show_regs_fan_min##offset, store_regs_fan_min##offset);
619 sysfs_fan_min_offset(1);
621 sysfs_fan_min_offset(2);
623 sysfs_fan_min_offset(3);
625 #define show_temp_reg(reg) \
626 static ssize_t show_##reg (struct device *dev, char *buf, int nr) \
628 struct w83627hf_data *data = w83627hf_update_device(dev); \
629 if (nr >= 2) { /* TEMP2 and TEMP3 */ \
630 return sprintf(buf,"%ld\n", \
631 (long)LM75_TEMP_FROM_REG(data->reg##_add[nr-2])); \
632 } else { /* TEMP1 */ \
633 return sprintf(buf,"%ld\n", (long)TEMP_FROM_REG(data->reg)); \
637 show_temp_reg(temp_max);
638 show_temp_reg(temp_max_hyst);
640 #define store_temp_reg(REG, reg) \
642 store_temp_##reg (struct device *dev, const char *buf, size_t count, int nr) \
644 struct w83627hf_data *data = dev_get_drvdata(dev); \
647 val = simple_strtoul(buf, NULL, 10); \
649 mutex_lock(&data->update_lock); \
651 if (nr >= 2) { /* TEMP2 and TEMP3 */ \
652 data->temp_##reg##_add[nr-2] = LM75_TEMP_TO_REG(val); \
653 w83627hf_write_value(data, W83781D_REG_TEMP_##REG(nr), \
654 data->temp_##reg##_add[nr-2]); \
655 } else { /* TEMP1 */ \
656 data->temp_##reg = TEMP_TO_REG(val); \
657 w83627hf_write_value(data, W83781D_REG_TEMP_##REG(nr), \
661 mutex_unlock(&data->update_lock); \
664 store_temp_reg(OVER, max);
665 store_temp_reg(HYST, max_hyst);
667 #define sysfs_temp_offset(offset) \
669 show_regs_temp_##offset (struct device *dev, struct device_attribute *attr, char *buf) \
671 return show_temp(dev, buf, offset); \
673 static DEVICE_ATTR(temp##offset##_input, S_IRUGO, show_regs_temp_##offset, NULL);
675 #define sysfs_temp_reg_offset(reg, offset) \
676 static ssize_t show_regs_temp_##reg##offset (struct device *dev, struct device_attribute *attr, char *buf) \
678 return show_temp_##reg (dev, buf, offset); \
681 store_regs_temp_##reg##offset (struct device *dev, struct device_attribute *attr, \
682 const char *buf, size_t count) \
684 return store_temp_##reg (dev, buf, count, offset); \
686 static DEVICE_ATTR(temp##offset##_##reg, S_IRUGO| S_IWUSR, \
687 show_regs_temp_##reg##offset, store_regs_temp_##reg##offset);
689 #define sysfs_temp_offsets(offset) \
690 sysfs_temp_offset(offset) \
691 sysfs_temp_reg_offset(max, offset) \
692 sysfs_temp_reg_offset(max_hyst, offset)
694 sysfs_temp_offsets(1);
695 sysfs_temp_offsets(2);
696 sysfs_temp_offsets(3);
699 show_vid_reg(struct device *dev, struct device_attribute *attr, char *buf)
701 struct w83627hf_data *data = w83627hf_update_device(dev);
702 return sprintf(buf, "%ld\n", (long) vid_from_reg(data->vid, data->vrm));
704 static DEVICE_ATTR(cpu0_vid, S_IRUGO, show_vid_reg, NULL);
707 show_vrm_reg(struct device *dev, struct device_attribute *attr, char *buf)
709 struct w83627hf_data *data = w83627hf_update_device(dev);
710 return sprintf(buf, "%ld\n", (long) data->vrm);
713 store_vrm_reg(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
715 struct w83627hf_data *data = dev_get_drvdata(dev);
718 val = simple_strtoul(buf, NULL, 10);
723 static DEVICE_ATTR(vrm, S_IRUGO | S_IWUSR, show_vrm_reg, store_vrm_reg);
726 show_alarms_reg(struct device *dev, struct device_attribute *attr, char *buf)
728 struct w83627hf_data *data = w83627hf_update_device(dev);
729 return sprintf(buf, "%ld\n", (long) data->alarms);
731 static DEVICE_ATTR(alarms, S_IRUGO, show_alarms_reg, NULL);
733 #define show_beep_reg(REG, reg) \
734 static ssize_t show_beep_##reg (struct device *dev, struct device_attribute *attr, char *buf) \
736 struct w83627hf_data *data = w83627hf_update_device(dev); \
737 return sprintf(buf,"%ld\n", \
738 (long)BEEP_##REG##_FROM_REG(data->beep_##reg)); \
740 show_beep_reg(ENABLE, enable)
741 show_beep_reg(MASK, mask)
743 #define BEEP_ENABLE 0 /* Store beep_enable */
744 #define BEEP_MASK 1 /* Store beep_mask */
747 store_beep_reg(struct device *dev, const char *buf, size_t count,
750 struct w83627hf_data *data = dev_get_drvdata(dev);
753 val = simple_strtoul(buf, NULL, 10);
755 mutex_lock(&data->update_lock);
757 if (update_mask == BEEP_MASK) { /* We are storing beep_mask */
758 data->beep_mask = BEEP_MASK_TO_REG(val);
759 w83627hf_write_value(data, W83781D_REG_BEEP_INTS1,
760 data->beep_mask & 0xff);
761 w83627hf_write_value(data, W83781D_REG_BEEP_INTS3,
762 ((data->beep_mask) >> 16) & 0xff);
763 val2 = (data->beep_mask >> 8) & 0x7f;
764 } else { /* We are storing beep_enable */
766 w83627hf_read_value(data, W83781D_REG_BEEP_INTS2) & 0x7f;
767 data->beep_enable = BEEP_ENABLE_TO_REG(val);
770 w83627hf_write_value(data, W83781D_REG_BEEP_INTS2,
771 val2 | data->beep_enable << 7);
773 mutex_unlock(&data->update_lock);
777 #define sysfs_beep(REG, reg) \
778 static ssize_t show_regs_beep_##reg (struct device *dev, struct device_attribute *attr, char *buf) \
780 return show_beep_##reg(dev, attr, buf); \
783 store_regs_beep_##reg (struct device *dev, struct device_attribute *attr, const char *buf, size_t count) \
785 return store_beep_reg(dev, buf, count, BEEP_##REG); \
787 static DEVICE_ATTR(beep_##reg, S_IRUGO | S_IWUSR, \
788 show_regs_beep_##reg, store_regs_beep_##reg);
790 sysfs_beep(ENABLE, enable);
791 sysfs_beep(MASK, mask);
794 show_fan_div_reg(struct device *dev, char *buf, int nr)
796 struct w83627hf_data *data = w83627hf_update_device(dev);
797 return sprintf(buf, "%ld\n",
798 (long) DIV_FROM_REG(data->fan_div[nr - 1]));
801 /* Note: we save and restore the fan minimum here, because its value is
802 determined in part by the fan divisor. This follows the principle of
803 least surprise; the user doesn't expect the fan minimum to change just
804 because the divisor changed. */
806 store_fan_div_reg(struct device *dev, const char *buf, size_t count, int nr)
808 struct w83627hf_data *data = dev_get_drvdata(dev);
811 unsigned long val = simple_strtoul(buf, NULL, 10);
813 mutex_lock(&data->update_lock);
816 min = FAN_FROM_REG(data->fan_min[nr],
817 DIV_FROM_REG(data->fan_div[nr]));
819 data->fan_div[nr] = DIV_TO_REG(val);
821 reg = (w83627hf_read_value(data, nr==2 ? W83781D_REG_PIN : W83781D_REG_VID_FANDIV)
822 & (nr==0 ? 0xcf : 0x3f))
823 | ((data->fan_div[nr] & 0x03) << (nr==0 ? 4 : 6));
824 w83627hf_write_value(data, nr==2 ? W83781D_REG_PIN : W83781D_REG_VID_FANDIV, reg);
826 reg = (w83627hf_read_value(data, W83781D_REG_VBAT)
828 | ((data->fan_div[nr] & 0x04) << (3 + nr));
829 w83627hf_write_value(data, W83781D_REG_VBAT, reg);
831 /* Restore fan_min */
832 data->fan_min[nr] = FAN_TO_REG(min, DIV_FROM_REG(data->fan_div[nr]));
833 w83627hf_write_value(data, W83781D_REG_FAN_MIN(nr+1), data->fan_min[nr]);
835 mutex_unlock(&data->update_lock);
839 #define sysfs_fan_div(offset) \
840 static ssize_t show_regs_fan_div_##offset (struct device *dev, struct device_attribute *attr, char *buf) \
842 return show_fan_div_reg(dev, buf, offset); \
845 store_regs_fan_div_##offset (struct device *dev, struct device_attribute *attr, \
846 const char *buf, size_t count) \
848 return store_fan_div_reg(dev, buf, count, offset - 1); \
850 static DEVICE_ATTR(fan##offset##_div, S_IRUGO | S_IWUSR, \
851 show_regs_fan_div_##offset, store_regs_fan_div_##offset);
858 show_pwm_reg(struct device *dev, char *buf, int nr)
860 struct w83627hf_data *data = w83627hf_update_device(dev);
861 return sprintf(buf, "%ld\n", (long) data->pwm[nr - 1]);
865 store_pwm_reg(struct device *dev, const char *buf, size_t count, int nr)
867 struct w83627hf_data *data = dev_get_drvdata(dev);
870 val = simple_strtoul(buf, NULL, 10);
872 mutex_lock(&data->update_lock);
874 if (data->type == w83627thf) {
875 /* bits 0-3 are reserved in 627THF */
876 data->pwm[nr - 1] = PWM_TO_REG(val) & 0xf0;
877 w83627hf_write_value(data,
878 W836X7HF_REG_PWM(data->type, nr),
880 (w83627hf_read_value(data,
881 W836X7HF_REG_PWM(data->type, nr)) & 0x0f));
883 data->pwm[nr - 1] = PWM_TO_REG(val);
884 w83627hf_write_value(data,
885 W836X7HF_REG_PWM(data->type, nr),
889 mutex_unlock(&data->update_lock);
893 #define sysfs_pwm(offset) \
894 static ssize_t show_regs_pwm_##offset (struct device *dev, struct device_attribute *attr, char *buf) \
896 return show_pwm_reg(dev, buf, offset); \
899 store_regs_pwm_##offset (struct device *dev, struct device_attribute *attr, const char *buf, size_t count) \
901 return store_pwm_reg(dev, buf, count, offset); \
903 static DEVICE_ATTR(pwm##offset, S_IRUGO | S_IWUSR, \
904 show_regs_pwm_##offset, store_regs_pwm_##offset);
911 show_pwm_freq_reg(struct device *dev, char *buf, int nr)
913 struct w83627hf_data *data = w83627hf_update_device(dev);
914 if (data->type == w83627hf)
915 return sprintf(buf, "%ld\n",
916 pwm_freq_from_reg_627hf(data->pwm_freq[nr - 1]));
918 return sprintf(buf, "%ld\n",
919 pwm_freq_from_reg(data->pwm_freq[nr - 1]));
923 store_pwm_freq_reg(struct device *dev, const char *buf, size_t count, int nr)
925 struct w83627hf_data *data = dev_get_drvdata(dev);
926 static const u8 mask[]={0xF8, 0x8F};
929 val = simple_strtoul(buf, NULL, 10);
931 mutex_lock(&data->update_lock);
933 if (data->type == w83627hf) {
934 data->pwm_freq[nr - 1] = pwm_freq_to_reg_627hf(val);
935 w83627hf_write_value(data, W83627HF_REG_PWM_FREQ,
936 (data->pwm_freq[nr - 1] << ((nr - 1)*4)) |
937 (w83627hf_read_value(data,
938 W83627HF_REG_PWM_FREQ) & mask[nr - 1]));
940 data->pwm_freq[nr - 1] = pwm_freq_to_reg(val);
941 w83627hf_write_value(data, W83637HF_REG_PWM_FREQ[nr - 1],
942 data->pwm_freq[nr - 1]);
945 mutex_unlock(&data->update_lock);
949 #define sysfs_pwm_freq(offset) \
950 static ssize_t show_regs_pwm_freq_##offset(struct device *dev, \
951 struct device_attribute *attr, char *buf) \
953 return show_pwm_freq_reg(dev, buf, offset); \
956 store_regs_pwm_freq_##offset(struct device *dev, \
957 struct device_attribute *attr, const char *buf, size_t count) \
959 return store_pwm_freq_reg(dev, buf, count, offset); \
961 static DEVICE_ATTR(pwm##offset##_freq, S_IRUGO | S_IWUSR, \
962 show_regs_pwm_freq_##offset, store_regs_pwm_freq_##offset);
969 show_sensor_reg(struct device *dev, char *buf, int nr)
971 struct w83627hf_data *data = w83627hf_update_device(dev);
972 return sprintf(buf, "%ld\n", (long) data->sens[nr - 1]);
976 store_sensor_reg(struct device *dev, const char *buf, size_t count, int nr)
978 struct w83627hf_data *data = dev_get_drvdata(dev);
981 val = simple_strtoul(buf, NULL, 10);
983 mutex_lock(&data->update_lock);
986 case 1: /* PII/Celeron diode */
987 tmp = w83627hf_read_value(data, W83781D_REG_SCFG1);
988 w83627hf_write_value(data, W83781D_REG_SCFG1,
989 tmp | BIT_SCFG1[nr - 1]);
990 tmp = w83627hf_read_value(data, W83781D_REG_SCFG2);
991 w83627hf_write_value(data, W83781D_REG_SCFG2,
992 tmp | BIT_SCFG2[nr - 1]);
993 data->sens[nr - 1] = val;
996 tmp = w83627hf_read_value(data, W83781D_REG_SCFG1);
997 w83627hf_write_value(data, W83781D_REG_SCFG1,
998 tmp | BIT_SCFG1[nr - 1]);
999 tmp = w83627hf_read_value(data, W83781D_REG_SCFG2);
1000 w83627hf_write_value(data, W83781D_REG_SCFG2,
1001 tmp & ~BIT_SCFG2[nr - 1]);
1002 data->sens[nr - 1] = val;
1004 case W83781D_DEFAULT_BETA: /* thermistor */
1005 tmp = w83627hf_read_value(data, W83781D_REG_SCFG1);
1006 w83627hf_write_value(data, W83781D_REG_SCFG1,
1007 tmp & ~BIT_SCFG1[nr - 1]);
1008 data->sens[nr - 1] = val;
1012 "Invalid sensor type %ld; must be 1, 2, or %d\n",
1013 (long) val, W83781D_DEFAULT_BETA);
1017 mutex_unlock(&data->update_lock);
1021 #define sysfs_sensor(offset) \
1022 static ssize_t show_regs_sensor_##offset (struct device *dev, struct device_attribute *attr, char *buf) \
1024 return show_sensor_reg(dev, buf, offset); \
1027 store_regs_sensor_##offset (struct device *dev, struct device_attribute *attr, const char *buf, size_t count) \
1029 return store_sensor_reg(dev, buf, count, offset); \
1031 static DEVICE_ATTR(temp##offset##_type, S_IRUGO | S_IWUSR, \
1032 show_regs_sensor_##offset, store_regs_sensor_##offset);
1038 static ssize_t show_name(struct device *dev, struct device_attribute
1039 *devattr, char *buf)
1041 struct w83627hf_data *data = dev_get_drvdata(dev);
1043 return sprintf(buf, "%s\n", data->name);
1045 static DEVICE_ATTR(name, S_IRUGO, show_name, NULL);
1047 static int __init w83627hf_find(int sioaddr, unsigned short *addr,
1048 struct w83627hf_sio_data *sio_data)
1053 static const __initdata char *names[] = {
1065 val= superio_inb(DEVID);
1068 sio_data->type = w83627hf;
1071 sio_data->type = w83627thf;
1074 sio_data->type = w83697hf;
1077 sio_data->type = w83637hf;
1080 sio_data->type = w83687thf;
1082 case 0xff: /* No device at all */
1085 pr_debug(DRVNAME ": Unsupported chip (DEVID=0x%02x)\n", val);
1089 superio_select(W83627HF_LD_HWM);
1090 force_addr &= WINB_ALIGNMENT;
1092 printk(KERN_WARNING DRVNAME ": Forcing address 0x%x\n",
1094 superio_outb(WINB_BASE_REG, force_addr >> 8);
1095 superio_outb(WINB_BASE_REG + 1, force_addr & 0xff);
1097 val = (superio_inb(WINB_BASE_REG) << 8) |
1098 superio_inb(WINB_BASE_REG + 1);
1099 *addr = val & WINB_ALIGNMENT;
1101 printk(KERN_WARNING DRVNAME ": Base address not set, "
1106 val = superio_inb(WINB_ACT_REG);
1107 if (!(val & 0x01)) {
1108 printk(KERN_WARNING DRVNAME ": Enabling HWM logical device\n");
1109 superio_outb(WINB_ACT_REG, val | 0x01);
1113 pr_info(DRVNAME ": Found %s chip at %#x\n",
1114 names[sio_data->type], *addr);
1121 static struct attribute *w83627hf_attributes[] = {
1122 &dev_attr_in0_input.attr,
1123 &dev_attr_in0_min.attr,
1124 &dev_attr_in0_max.attr,
1125 &dev_attr_in2_input.attr,
1126 &dev_attr_in2_min.attr,
1127 &dev_attr_in2_max.attr,
1128 &dev_attr_in3_input.attr,
1129 &dev_attr_in3_min.attr,
1130 &dev_attr_in3_max.attr,
1131 &dev_attr_in4_input.attr,
1132 &dev_attr_in4_min.attr,
1133 &dev_attr_in4_max.attr,
1134 &dev_attr_in7_input.attr,
1135 &dev_attr_in7_min.attr,
1136 &dev_attr_in7_max.attr,
1137 &dev_attr_in8_input.attr,
1138 &dev_attr_in8_min.attr,
1139 &dev_attr_in8_max.attr,
1141 &dev_attr_fan1_input.attr,
1142 &dev_attr_fan1_min.attr,
1143 &dev_attr_fan1_div.attr,
1144 &dev_attr_fan2_input.attr,
1145 &dev_attr_fan2_min.attr,
1146 &dev_attr_fan2_div.attr,
1148 &dev_attr_temp1_input.attr,
1149 &dev_attr_temp1_max.attr,
1150 &dev_attr_temp1_max_hyst.attr,
1151 &dev_attr_temp1_type.attr,
1152 &dev_attr_temp2_input.attr,
1153 &dev_attr_temp2_max.attr,
1154 &dev_attr_temp2_max_hyst.attr,
1155 &dev_attr_temp2_type.attr,
1157 &dev_attr_alarms.attr,
1158 &dev_attr_beep_enable.attr,
1159 &dev_attr_beep_mask.attr,
1161 &dev_attr_pwm1.attr,
1162 &dev_attr_pwm2.attr,
1164 &dev_attr_name.attr,
1168 static const struct attribute_group w83627hf_group = {
1169 .attrs = w83627hf_attributes,
1172 static struct attribute *w83627hf_attributes_opt[] = {
1173 &dev_attr_in1_input.attr,
1174 &dev_attr_in1_min.attr,
1175 &dev_attr_in1_max.attr,
1176 &dev_attr_in5_input.attr,
1177 &dev_attr_in5_min.attr,
1178 &dev_attr_in5_max.attr,
1179 &dev_attr_in6_input.attr,
1180 &dev_attr_in6_min.attr,
1181 &dev_attr_in6_max.attr,
1183 &dev_attr_fan3_input.attr,
1184 &dev_attr_fan3_min.attr,
1185 &dev_attr_fan3_div.attr,
1187 &dev_attr_temp3_input.attr,
1188 &dev_attr_temp3_max.attr,
1189 &dev_attr_temp3_max_hyst.attr,
1190 &dev_attr_temp3_type.attr,
1192 &dev_attr_pwm3.attr,
1194 &dev_attr_pwm1_freq.attr,
1195 &dev_attr_pwm2_freq.attr,
1196 &dev_attr_pwm3_freq.attr,
1200 static const struct attribute_group w83627hf_group_opt = {
1201 .attrs = w83627hf_attributes_opt,
1204 static int __devinit w83627hf_probe(struct platform_device *pdev)
1206 struct device *dev = &pdev->dev;
1207 struct w83627hf_sio_data *sio_data = dev->platform_data;
1208 struct w83627hf_data *data;
1209 struct resource *res;
1212 static const char *names[] = {
1220 res = platform_get_resource(pdev, IORESOURCE_IO, 0);
1221 if (!request_region(res->start, WINB_REGION_SIZE, DRVNAME)) {
1222 dev_err(dev, "Failed to request region 0x%lx-0x%lx\n",
1223 (unsigned long)res->start,
1224 (unsigned long)(res->start + WINB_REGION_SIZE - 1));
1229 if (!(data = kzalloc(sizeof(struct w83627hf_data), GFP_KERNEL))) {
1233 data->addr = res->start;
1234 data->type = sio_data->type;
1235 data->name = names[sio_data->type];
1236 mutex_init(&data->lock);
1237 mutex_init(&data->update_lock);
1238 platform_set_drvdata(pdev, data);
1240 /* Initialize the chip */
1241 w83627hf_init_device(pdev);
1243 /* A few vars need to be filled upon startup */
1244 data->fan_min[0] = w83627hf_read_value(data, W83781D_REG_FAN_MIN(1));
1245 data->fan_min[1] = w83627hf_read_value(data, W83781D_REG_FAN_MIN(2));
1246 data->fan_min[2] = w83627hf_read_value(data, W83781D_REG_FAN_MIN(3));
1248 /* Register common device attributes */
1249 if ((err = sysfs_create_group(&dev->kobj, &w83627hf_group)))
1252 /* Register chip-specific device attributes */
1253 if (data->type == w83627hf || data->type == w83697hf)
1254 if ((err = device_create_file(dev, &dev_attr_in5_input))
1255 || (err = device_create_file(dev, &dev_attr_in5_min))
1256 || (err = device_create_file(dev, &dev_attr_in5_max))
1257 || (err = device_create_file(dev, &dev_attr_in6_input))
1258 || (err = device_create_file(dev, &dev_attr_in6_min))
1259 || (err = device_create_file(dev, &dev_attr_in6_max))
1260 || (err = device_create_file(dev, &dev_attr_pwm1_freq))
1261 || (err = device_create_file(dev, &dev_attr_pwm2_freq)))
1264 if (data->type != w83697hf)
1265 if ((err = device_create_file(dev, &dev_attr_in1_input))
1266 || (err = device_create_file(dev, &dev_attr_in1_min))
1267 || (err = device_create_file(dev, &dev_attr_in1_max))
1268 || (err = device_create_file(dev, &dev_attr_fan3_input))
1269 || (err = device_create_file(dev, &dev_attr_fan3_min))
1270 || (err = device_create_file(dev, &dev_attr_fan3_div))
1271 || (err = device_create_file(dev, &dev_attr_temp3_input))
1272 || (err = device_create_file(dev, &dev_attr_temp3_max))
1273 || (err = device_create_file(dev, &dev_attr_temp3_max_hyst))
1274 || (err = device_create_file(dev, &dev_attr_temp3_type)))
1277 if (data->type != w83697hf && data->vid != 0xff) {
1278 /* Convert VID to voltage based on VRM */
1279 data->vrm = vid_which_vrm();
1281 if ((err = device_create_file(dev, &dev_attr_cpu0_vid))
1282 || (err = device_create_file(dev, &dev_attr_vrm)))
1286 if (data->type == w83627thf || data->type == w83637hf
1287 || data->type == w83687thf)
1288 if ((err = device_create_file(dev, &dev_attr_pwm3)))
1291 if (data->type == w83637hf || data->type == w83687thf)
1292 if ((err = device_create_file(dev, &dev_attr_pwm1_freq))
1293 || (err = device_create_file(dev, &dev_attr_pwm2_freq))
1294 || (err = device_create_file(dev, &dev_attr_pwm3_freq)))
1297 data->class_dev = hwmon_device_register(dev);
1298 if (IS_ERR(data->class_dev)) {
1299 err = PTR_ERR(data->class_dev);
1306 sysfs_remove_group(&dev->kobj, &w83627hf_group);
1307 sysfs_remove_group(&dev->kobj, &w83627hf_group_opt);
1309 platform_set_drvdata(pdev, NULL);
1312 release_region(res->start, WINB_REGION_SIZE);
1317 static int __devexit w83627hf_remove(struct platform_device *pdev)
1319 struct w83627hf_data *data = platform_get_drvdata(pdev);
1320 struct resource *res;
1322 hwmon_device_unregister(data->class_dev);
1324 sysfs_remove_group(&pdev->dev.kobj, &w83627hf_group);
1325 sysfs_remove_group(&pdev->dev.kobj, &w83627hf_group_opt);
1326 platform_set_drvdata(pdev, NULL);
1329 res = platform_get_resource(pdev, IORESOURCE_IO, 0);
1330 release_region(res->start, WINB_REGION_SIZE);
1336 static int w83627hf_read_value(struct w83627hf_data *data, u16 reg)
1338 int res, word_sized;
1340 mutex_lock(&data->lock);
1341 word_sized = (((reg & 0xff00) == 0x100)
1342 || ((reg & 0xff00) == 0x200))
1343 && (((reg & 0x00ff) == 0x50)
1344 || ((reg & 0x00ff) == 0x53)
1345 || ((reg & 0x00ff) == 0x55));
1347 outb_p(W83781D_REG_BANK,
1348 data->addr + W83781D_ADDR_REG_OFFSET);
1350 data->addr + W83781D_DATA_REG_OFFSET);
1352 outb_p(reg & 0xff, data->addr + W83781D_ADDR_REG_OFFSET);
1353 res = inb_p(data->addr + W83781D_DATA_REG_OFFSET);
1355 outb_p((reg & 0xff) + 1,
1356 data->addr + W83781D_ADDR_REG_OFFSET);
1358 (res << 8) + inb_p(data->addr +
1359 W83781D_DATA_REG_OFFSET);
1362 outb_p(W83781D_REG_BANK,
1363 data->addr + W83781D_ADDR_REG_OFFSET);
1364 outb_p(0, data->addr + W83781D_DATA_REG_OFFSET);
1366 mutex_unlock(&data->lock);
1370 static int __devinit w83627thf_read_gpio5(struct platform_device *pdev)
1372 int res = 0xff, sel;
1375 superio_select(W83627HF_LD_GPIO5);
1377 /* Make sure these GPIO pins are enabled */
1378 if (!(superio_inb(W83627THF_GPIO5_EN) & (1<<3))) {
1379 dev_dbg(&pdev->dev, "GPIO5 disabled, no VID function\n");
1383 /* Make sure the pins are configured for input
1384 There must be at least five (VRM 9), and possibly 6 (VRM 10) */
1385 sel = superio_inb(W83627THF_GPIO5_IOSR) & 0x3f;
1386 if ((sel & 0x1f) != 0x1f) {
1387 dev_dbg(&pdev->dev, "GPIO5 not configured for VID "
1392 dev_info(&pdev->dev, "Reading VID from GPIO5\n");
1393 res = superio_inb(W83627THF_GPIO5_DR) & sel;
1400 static int __devinit w83687thf_read_vid(struct platform_device *pdev)
1405 superio_select(W83627HF_LD_HWM);
1407 /* Make sure these GPIO pins are enabled */
1408 if (!(superio_inb(W83687THF_VID_EN) & (1 << 2))) {
1409 dev_dbg(&pdev->dev, "VID disabled, no VID function\n");
1413 /* Make sure the pins are configured for input */
1414 if (!(superio_inb(W83687THF_VID_CFG) & (1 << 4))) {
1415 dev_dbg(&pdev->dev, "VID configured as output, "
1416 "no VID function\n");
1420 res = superio_inb(W83687THF_VID_DATA) & 0x3f;
1427 static int w83627hf_write_value(struct w83627hf_data *data, u16 reg, u16 value)
1431 mutex_lock(&data->lock);
1432 word_sized = (((reg & 0xff00) == 0x100)
1433 || ((reg & 0xff00) == 0x200))
1434 && (((reg & 0x00ff) == 0x53)
1435 || ((reg & 0x00ff) == 0x55));
1437 outb_p(W83781D_REG_BANK,
1438 data->addr + W83781D_ADDR_REG_OFFSET);
1440 data->addr + W83781D_DATA_REG_OFFSET);
1442 outb_p(reg & 0xff, data->addr + W83781D_ADDR_REG_OFFSET);
1445 data->addr + W83781D_DATA_REG_OFFSET);
1446 outb_p((reg & 0xff) + 1,
1447 data->addr + W83781D_ADDR_REG_OFFSET);
1449 outb_p(value & 0xff,
1450 data->addr + W83781D_DATA_REG_OFFSET);
1452 outb_p(W83781D_REG_BANK,
1453 data->addr + W83781D_ADDR_REG_OFFSET);
1454 outb_p(0, data->addr + W83781D_DATA_REG_OFFSET);
1456 mutex_unlock(&data->lock);
1460 static void __devinit w83627hf_init_device(struct platform_device *pdev)
1462 struct w83627hf_data *data = platform_get_drvdata(pdev);
1464 enum chips type = data->type;
1468 /* Resetting the chip has been the default for a long time,
1469 but repeatedly caused problems (fans going to full
1470 speed...) so it is now optional. It might even go away if
1471 nobody reports it as being useful, as I see very little
1472 reason why this would be needed at all. */
1473 dev_info(&pdev->dev, "If reset=1 solved a problem you were "
1474 "having, please report!\n");
1476 /* save this register */
1477 i = w83627hf_read_value(data, W83781D_REG_BEEP_CONFIG);
1478 /* Reset all except Watchdog values and last conversion values
1479 This sets fan-divs to 2, among others */
1480 w83627hf_write_value(data, W83781D_REG_CONFIG, 0x80);
1481 /* Restore the register and disable power-on abnormal beep.
1482 This saves FAN 1/2/3 input/output values set by BIOS. */
1483 w83627hf_write_value(data, W83781D_REG_BEEP_CONFIG, i | 0x80);
1484 /* Disable master beep-enable (reset turns it on).
1485 Individual beeps should be reset to off but for some reason
1486 disabling this bit helps some people not get beeped */
1487 w83627hf_write_value(data, W83781D_REG_BEEP_INTS2, 0);
1490 /* Minimize conflicts with other winbond i2c-only clients... */
1491 /* disable i2c subclients... how to disable main i2c client?? */
1492 /* force i2c address to relatively uncommon address */
1493 w83627hf_write_value(data, W83781D_REG_I2C_SUBADDR, 0x89);
1494 w83627hf_write_value(data, W83781D_REG_I2C_ADDR, force_i2c);
1496 /* Read VID only once */
1497 if (type == w83627hf || type == w83637hf) {
1498 int lo = w83627hf_read_value(data, W83781D_REG_VID_FANDIV);
1499 int hi = w83627hf_read_value(data, W83781D_REG_CHIPID);
1500 data->vid = (lo & 0x0f) | ((hi & 0x01) << 4);
1501 } else if (type == w83627thf) {
1502 data->vid = w83627thf_read_gpio5(pdev);
1503 } else if (type == w83687thf) {
1504 data->vid = w83687thf_read_vid(pdev);
1507 /* Read VRM & OVT Config only once */
1508 if (type == w83627thf || type == w83637hf || type == w83687thf) {
1510 w83627hf_read_value(data, W83627THF_REG_VRM_OVT_CFG);
1513 tmp = w83627hf_read_value(data, W83781D_REG_SCFG1);
1514 for (i = 1; i <= 3; i++) {
1515 if (!(tmp & BIT_SCFG1[i - 1])) {
1516 data->sens[i - 1] = W83781D_DEFAULT_BETA;
1518 if (w83627hf_read_value
1520 W83781D_REG_SCFG2) & BIT_SCFG2[i - 1])
1521 data->sens[i - 1] = 1;
1523 data->sens[i - 1] = 2;
1525 if ((type == w83697hf) && (i == 2))
1531 tmp = w83627hf_read_value(data, W83781D_REG_TEMP2_CONFIG);
1533 dev_warn(&pdev->dev, "Enabling temp2, readings "
1534 "might not make sense\n");
1535 w83627hf_write_value(data, W83781D_REG_TEMP2_CONFIG,
1540 if (type != w83697hf) {
1541 tmp = w83627hf_read_value(data,
1542 W83781D_REG_TEMP3_CONFIG);
1544 dev_warn(&pdev->dev, "Enabling temp3, "
1545 "readings might not make sense\n");
1546 w83627hf_write_value(data,
1547 W83781D_REG_TEMP3_CONFIG, tmp & 0xfe);
1552 /* Start monitoring */
1553 w83627hf_write_value(data, W83781D_REG_CONFIG,
1554 (w83627hf_read_value(data,
1555 W83781D_REG_CONFIG) & 0xf7)
1559 static struct w83627hf_data *w83627hf_update_device(struct device *dev)
1561 struct w83627hf_data *data = dev_get_drvdata(dev);
1564 mutex_lock(&data->update_lock);
1566 if (time_after(jiffies, data->last_updated + HZ + HZ / 2)
1568 for (i = 0; i <= 8; i++) {
1569 /* skip missing sensors */
1570 if (((data->type == w83697hf) && (i == 1)) ||
1571 ((data->type != w83627hf && data->type != w83697hf)
1572 && (i == 5 || i == 6)))
1575 w83627hf_read_value(data, W83781D_REG_IN(i));
1577 w83627hf_read_value(data,
1578 W83781D_REG_IN_MIN(i));
1580 w83627hf_read_value(data,
1581 W83781D_REG_IN_MAX(i));
1583 for (i = 1; i <= 3; i++) {
1585 w83627hf_read_value(data, W83781D_REG_FAN(i));
1586 data->fan_min[i - 1] =
1587 w83627hf_read_value(data,
1588 W83781D_REG_FAN_MIN(i));
1590 for (i = 1; i <= 3; i++) {
1591 u8 tmp = w83627hf_read_value(data,
1592 W836X7HF_REG_PWM(data->type, i));
1593 /* bits 0-3 are reserved in 627THF */
1594 if (data->type == w83627thf)
1596 data->pwm[i - 1] = tmp;
1598 (data->type == w83627hf || data->type == w83697hf))
1601 if (data->type == w83627hf) {
1602 u8 tmp = w83627hf_read_value(data,
1603 W83627HF_REG_PWM_FREQ);
1604 data->pwm_freq[0] = tmp & 0x07;
1605 data->pwm_freq[1] = (tmp >> 4) & 0x07;
1606 } else if (data->type != w83627thf) {
1607 for (i = 1; i <= 3; i++) {
1608 data->pwm_freq[i - 1] =
1609 w83627hf_read_value(data,
1610 W83637HF_REG_PWM_FREQ[i - 1]);
1611 if (i == 2 && (data->type == w83697hf))
1616 data->temp = w83627hf_read_value(data, W83781D_REG_TEMP(1));
1618 w83627hf_read_value(data, W83781D_REG_TEMP_OVER(1));
1619 data->temp_max_hyst =
1620 w83627hf_read_value(data, W83781D_REG_TEMP_HYST(1));
1622 w83627hf_read_value(data, W83781D_REG_TEMP(2));
1623 data->temp_max_add[0] =
1624 w83627hf_read_value(data, W83781D_REG_TEMP_OVER(2));
1625 data->temp_max_hyst_add[0] =
1626 w83627hf_read_value(data, W83781D_REG_TEMP_HYST(2));
1627 if (data->type != w83697hf) {
1629 w83627hf_read_value(data, W83781D_REG_TEMP(3));
1630 data->temp_max_add[1] =
1631 w83627hf_read_value(data, W83781D_REG_TEMP_OVER(3));
1632 data->temp_max_hyst_add[1] =
1633 w83627hf_read_value(data, W83781D_REG_TEMP_HYST(3));
1636 i = w83627hf_read_value(data, W83781D_REG_VID_FANDIV);
1637 data->fan_div[0] = (i >> 4) & 0x03;
1638 data->fan_div[1] = (i >> 6) & 0x03;
1639 if (data->type != w83697hf) {
1640 data->fan_div[2] = (w83627hf_read_value(data,
1641 W83781D_REG_PIN) >> 6) & 0x03;
1643 i = w83627hf_read_value(data, W83781D_REG_VBAT);
1644 data->fan_div[0] |= (i >> 3) & 0x04;
1645 data->fan_div[1] |= (i >> 4) & 0x04;
1646 if (data->type != w83697hf)
1647 data->fan_div[2] |= (i >> 5) & 0x04;
1649 w83627hf_read_value(data, W83781D_REG_ALARM1) |
1650 (w83627hf_read_value(data, W83781D_REG_ALARM2) << 8) |
1651 (w83627hf_read_value(data, W83781D_REG_ALARM3) << 16);
1652 i = w83627hf_read_value(data, W83781D_REG_BEEP_INTS2);
1653 data->beep_enable = i >> 7;
1654 data->beep_mask = ((i & 0x7f) << 8) |
1655 w83627hf_read_value(data, W83781D_REG_BEEP_INTS1) |
1656 w83627hf_read_value(data, W83781D_REG_BEEP_INTS3) << 16;
1657 data->last_updated = jiffies;
1661 mutex_unlock(&data->update_lock);
1666 static int __init w83627hf_device_add(unsigned short address,
1667 const struct w83627hf_sio_data *sio_data)
1669 struct resource res = {
1670 .start = address + WINB_REGION_OFFSET,
1671 .end = address + WINB_REGION_OFFSET + WINB_REGION_SIZE - 1,
1673 .flags = IORESOURCE_IO,
1677 pdev = platform_device_alloc(DRVNAME, address);
1680 printk(KERN_ERR DRVNAME ": Device allocation failed\n");
1684 err = platform_device_add_resources(pdev, &res, 1);
1686 printk(KERN_ERR DRVNAME ": Device resource addition failed "
1688 goto exit_device_put;
1691 err = platform_device_add_data(pdev, sio_data,
1692 sizeof(struct w83627hf_sio_data));
1694 printk(KERN_ERR DRVNAME ": Platform data allocation failed\n");
1695 goto exit_device_put;
1698 err = platform_device_add(pdev);
1700 printk(KERN_ERR DRVNAME ": Device addition failed (%d)\n",
1702 goto exit_device_put;
1708 platform_device_put(pdev);
1713 static int __init sensors_w83627hf_init(void)
1716 unsigned short address;
1717 struct w83627hf_sio_data sio_data;
1719 if (w83627hf_find(0x2e, &address, &sio_data)
1720 && w83627hf_find(0x4e, &address, &sio_data))
1723 err = platform_driver_register(&w83627hf_driver);
1727 /* Sets global pdev as a side effect */
1728 err = w83627hf_device_add(address, &sio_data);
1735 platform_driver_unregister(&w83627hf_driver);
1740 static void __exit sensors_w83627hf_exit(void)
1742 platform_device_unregister(pdev);
1743 platform_driver_unregister(&w83627hf_driver);
1746 MODULE_AUTHOR("Frodo Looijaard <frodol@dds.nl>, "
1747 "Philip Edelbrock <phil@netroedge.com>, "
1748 "and Mark Studebaker <mdsxyz123@yahoo.com>");
1749 MODULE_DESCRIPTION("W83627HF driver");
1750 MODULE_LICENSE("GPL");
1752 module_init(sensors_w83627hf_init);
1753 module_exit(sensors_w83627hf_exit);