2 w83627ehf - Driver for the hardware monitoring functionality of
3 the Winbond W83627EHF Super-I/O chip
4 Copyright (C) 2005 Jean Delvare <khali@linux-fr.org>
6 Shamelessly ripped from the w83627hf driver
7 Copyright (C) 2003 Mark Studebaker
9 Thanks to Leon Moonen, Steve Cliffe and Grant Coady for their help
10 in testing and debugging this driver.
12 This driver also supports the W83627EHG, which is the lead-free
13 version of the W83627EHF.
15 This program is free software; you can redistribute it and/or modify
16 it under the terms of the GNU General Public License as published by
17 the Free Software Foundation; either version 2 of the License, or
18 (at your option) any later version.
20 This program is distributed in the hope that it will be useful,
21 but WITHOUT ANY WARRANTY; without even the implied warranty of
22 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 GNU General Public License for more details.
25 You should have received a copy of the GNU General Public License
26 along with this program; if not, write to the Free Software
27 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
30 Supports the following chips:
32 Chip #vin #fan #pwm #temp chip_id man_id
33 w83627ehf 10 5 - 3 0x88 0x5ca3
36 #include <linux/module.h>
37 #include <linux/init.h>
38 #include <linux/slab.h>
39 #include <linux/i2c.h>
40 #include <linux/i2c-isa.h>
41 #include <linux/hwmon.h>
42 #include <linux/hwmon-sysfs.h>
43 #include <linux/err.h>
44 #include <linux/mutex.h>
48 /* The actual ISA address is read from Super-I/O configuration space */
49 static unsigned short address
;
52 * Super-I/O constants and functions
55 static int REG
; /* The register to read/write */
56 static int VAL
; /* The value to read/write */
58 #define W83627EHF_LD_HWM 0x0b
60 #define SIO_REG_LDSEL 0x07 /* Logical device select */
61 #define SIO_REG_DEVID 0x20 /* Device ID (2 bytes) */
62 #define SIO_REG_ENABLE 0x30 /* Logical device enable */
63 #define SIO_REG_ADDR 0x60 /* Logical device address (2 bytes) */
65 #define SIO_W83627EHF_ID 0x8840
66 #define SIO_ID_MASK 0xFFC0
69 superio_outb(int reg
, int val
)
83 superio_select(int ld
)
85 outb(SIO_REG_LDSEL
, REG
);
107 #define REGION_ALIGNMENT ~7
108 #define REGION_OFFSET 5
109 #define REGION_LENGTH 2
110 #define ADDR_REG_OFFSET 5
111 #define DATA_REG_OFFSET 6
113 #define W83627EHF_REG_BANK 0x4E
114 #define W83627EHF_REG_CONFIG 0x40
115 #define W83627EHF_REG_CHIP_ID 0x49
116 #define W83627EHF_REG_MAN_ID 0x4F
118 static const u16 W83627EHF_REG_FAN
[] = { 0x28, 0x29, 0x2a, 0x3f, 0x553 };
119 static const u16 W83627EHF_REG_FAN_MIN
[] = { 0x3b, 0x3c, 0x3d, 0x3e, 0x55c };
121 /* The W83627EHF registers for nr=7,8,9 are in bank 5 */
122 #define W83627EHF_REG_IN_MAX(nr) ((nr < 7) ? (0x2b + (nr) * 2) : \
123 (0x554 + (((nr) - 7) * 2)))
124 #define W83627EHF_REG_IN_MIN(nr) ((nr < 7) ? (0x2c + (nr) * 2) : \
125 (0x555 + (((nr) - 7) * 2)))
126 #define W83627EHF_REG_IN(nr) ((nr < 7) ? (0x20 + (nr)) : \
129 #define W83627EHF_REG_TEMP1 0x27
130 #define W83627EHF_REG_TEMP1_HYST 0x3a
131 #define W83627EHF_REG_TEMP1_OVER 0x39
132 static const u16 W83627EHF_REG_TEMP
[] = { 0x150, 0x250 };
133 static const u16 W83627EHF_REG_TEMP_HYST
[] = { 0x153, 0x253 };
134 static const u16 W83627EHF_REG_TEMP_OVER
[] = { 0x155, 0x255 };
135 static const u16 W83627EHF_REG_TEMP_CONFIG
[] = { 0x152, 0x252 };
137 /* Fan clock dividers are spread over the following five registers */
138 #define W83627EHF_REG_FANDIV1 0x47
139 #define W83627EHF_REG_FANDIV2 0x4B
140 #define W83627EHF_REG_VBAT 0x5D
141 #define W83627EHF_REG_DIODE 0x59
142 #define W83627EHF_REG_SMI_OVT 0x4C
144 #define W83627EHF_REG_ALARM1 0x459
145 #define W83627EHF_REG_ALARM2 0x45A
146 #define W83627EHF_REG_ALARM3 0x45B
152 static inline unsigned int
153 fan_from_reg(u8 reg
, unsigned int div
)
155 if (reg
== 0 || reg
== 255)
157 return 1350000U / (reg
* div
);
160 static inline unsigned int
167 temp1_from_reg(s8 reg
)
173 temp1_to_reg(int temp
)
180 return (temp
- 500) / 1000;
181 return (temp
+ 500) / 1000;
184 /* Some of analog inputs have internal scaling (2x), 8mV is ADC LSB */
186 static u8 scale_in
[10] = { 8, 8, 16, 16, 8, 8, 8, 16, 16, 8 };
188 static inline long in_from_reg(u8 reg
, u8 nr
)
190 return reg
* scale_in
[nr
];
193 static inline u8
in_to_reg(u32 val
, u8 nr
)
195 return SENSORS_LIMIT(((val
+ (scale_in
[nr
] / 2)) / scale_in
[nr
]), 0, 255);
199 * Data structures and manipulation thereof
202 struct w83627ehf_data
{
203 struct i2c_client client
;
204 struct class_device
*class_dev
;
207 struct mutex update_lock
;
208 char valid
; /* !=0 if following fields are valid */
209 unsigned long last_updated
; /* In jiffies */
211 /* Register values */
212 u8 in
[10]; /* Register value */
213 u8 in_max
[10]; /* Register value */
214 u8 in_min
[10]; /* Register value */
218 u8 has_fan
; /* some fan inputs can be disabled */
224 s16 temp_max_hyst
[2];
228 static inline int is_word_sized(u16 reg
)
230 return (((reg
& 0xff00) == 0x100
231 || (reg
& 0xff00) == 0x200)
232 && ((reg
& 0x00ff) == 0x50
233 || (reg
& 0x00ff) == 0x53
234 || (reg
& 0x00ff) == 0x55));
237 /* We assume that the default bank is 0, thus the following two functions do
238 nothing for registers which live in bank 0. For others, they respectively
239 set the bank register to the correct value (before the register is
240 accessed), and back to 0 (afterwards). */
241 static inline void w83627ehf_set_bank(struct i2c_client
*client
, u16 reg
)
244 outb_p(W83627EHF_REG_BANK
, client
->addr
+ ADDR_REG_OFFSET
);
245 outb_p(reg
>> 8, client
->addr
+ DATA_REG_OFFSET
);
249 static inline void w83627ehf_reset_bank(struct i2c_client
*client
, u16 reg
)
252 outb_p(W83627EHF_REG_BANK
, client
->addr
+ ADDR_REG_OFFSET
);
253 outb_p(0, client
->addr
+ DATA_REG_OFFSET
);
257 static u16
w83627ehf_read_value(struct i2c_client
*client
, u16 reg
)
259 struct w83627ehf_data
*data
= i2c_get_clientdata(client
);
260 int res
, word_sized
= is_word_sized(reg
);
262 mutex_lock(&data
->lock
);
264 w83627ehf_set_bank(client
, reg
);
265 outb_p(reg
& 0xff, client
->addr
+ ADDR_REG_OFFSET
);
266 res
= inb_p(client
->addr
+ DATA_REG_OFFSET
);
268 outb_p((reg
& 0xff) + 1,
269 client
->addr
+ ADDR_REG_OFFSET
);
270 res
= (res
<< 8) + inb_p(client
->addr
+ DATA_REG_OFFSET
);
272 w83627ehf_reset_bank(client
, reg
);
274 mutex_unlock(&data
->lock
);
279 static int w83627ehf_write_value(struct i2c_client
*client
, u16 reg
, u16 value
)
281 struct w83627ehf_data
*data
= i2c_get_clientdata(client
);
282 int word_sized
= is_word_sized(reg
);
284 mutex_lock(&data
->lock
);
286 w83627ehf_set_bank(client
, reg
);
287 outb_p(reg
& 0xff, client
->addr
+ ADDR_REG_OFFSET
);
289 outb_p(value
>> 8, client
->addr
+ DATA_REG_OFFSET
);
290 outb_p((reg
& 0xff) + 1,
291 client
->addr
+ ADDR_REG_OFFSET
);
293 outb_p(value
& 0xff, client
->addr
+ DATA_REG_OFFSET
);
294 w83627ehf_reset_bank(client
, reg
);
296 mutex_unlock(&data
->lock
);
300 /* This function assumes that the caller holds data->update_lock */
301 static void w83627ehf_write_fan_div(struct i2c_client
*client
, int nr
)
303 struct w83627ehf_data
*data
= i2c_get_clientdata(client
);
308 reg
= (w83627ehf_read_value(client
, W83627EHF_REG_FANDIV1
) & 0xcf)
309 | ((data
->fan_div
[0] & 0x03) << 4);
310 w83627ehf_write_value(client
, W83627EHF_REG_FANDIV1
, reg
);
311 reg
= (w83627ehf_read_value(client
, W83627EHF_REG_VBAT
) & 0xdf)
312 | ((data
->fan_div
[0] & 0x04) << 3);
313 w83627ehf_write_value(client
, W83627EHF_REG_VBAT
, reg
);
316 reg
= (w83627ehf_read_value(client
, W83627EHF_REG_FANDIV1
) & 0x3f)
317 | ((data
->fan_div
[1] & 0x03) << 6);
318 w83627ehf_write_value(client
, W83627EHF_REG_FANDIV1
, reg
);
319 reg
= (w83627ehf_read_value(client
, W83627EHF_REG_VBAT
) & 0xbf)
320 | ((data
->fan_div
[1] & 0x04) << 4);
321 w83627ehf_write_value(client
, W83627EHF_REG_VBAT
, reg
);
324 reg
= (w83627ehf_read_value(client
, W83627EHF_REG_FANDIV2
) & 0x3f)
325 | ((data
->fan_div
[2] & 0x03) << 6);
326 w83627ehf_write_value(client
, W83627EHF_REG_FANDIV2
, reg
);
327 reg
= (w83627ehf_read_value(client
, W83627EHF_REG_VBAT
) & 0x7f)
328 | ((data
->fan_div
[2] & 0x04) << 5);
329 w83627ehf_write_value(client
, W83627EHF_REG_VBAT
, reg
);
332 reg
= (w83627ehf_read_value(client
, W83627EHF_REG_DIODE
) & 0xfc)
333 | (data
->fan_div
[3] & 0x03);
334 w83627ehf_write_value(client
, W83627EHF_REG_DIODE
, reg
);
335 reg
= (w83627ehf_read_value(client
, W83627EHF_REG_SMI_OVT
) & 0x7f)
336 | ((data
->fan_div
[3] & 0x04) << 5);
337 w83627ehf_write_value(client
, W83627EHF_REG_SMI_OVT
, reg
);
340 reg
= (w83627ehf_read_value(client
, W83627EHF_REG_DIODE
) & 0x73)
341 | ((data
->fan_div
[4] & 0x03) << 3)
342 | ((data
->fan_div
[4] & 0x04) << 5);
343 w83627ehf_write_value(client
, W83627EHF_REG_DIODE
, reg
);
348 static struct w83627ehf_data
*w83627ehf_update_device(struct device
*dev
)
350 struct i2c_client
*client
= to_i2c_client(dev
);
351 struct w83627ehf_data
*data
= i2c_get_clientdata(client
);
354 mutex_lock(&data
->update_lock
);
356 if (time_after(jiffies
, data
->last_updated
+ HZ
)
358 /* Fan clock dividers */
359 i
= w83627ehf_read_value(client
, W83627EHF_REG_FANDIV1
);
360 data
->fan_div
[0] = (i
>> 4) & 0x03;
361 data
->fan_div
[1] = (i
>> 6) & 0x03;
362 i
= w83627ehf_read_value(client
, W83627EHF_REG_FANDIV2
);
363 data
->fan_div
[2] = (i
>> 6) & 0x03;
364 i
= w83627ehf_read_value(client
, W83627EHF_REG_VBAT
);
365 data
->fan_div
[0] |= (i
>> 3) & 0x04;
366 data
->fan_div
[1] |= (i
>> 4) & 0x04;
367 data
->fan_div
[2] |= (i
>> 5) & 0x04;
368 if (data
->has_fan
& ((1 << 3) | (1 << 4))) {
369 i
= w83627ehf_read_value(client
, W83627EHF_REG_DIODE
);
370 data
->fan_div
[3] = i
& 0x03;
371 data
->fan_div
[4] = ((i
>> 2) & 0x03)
374 if (data
->has_fan
& (1 << 3)) {
375 i
= w83627ehf_read_value(client
, W83627EHF_REG_SMI_OVT
);
376 data
->fan_div
[3] |= (i
>> 5) & 0x04;
379 /* Measured voltages and limits */
380 for (i
= 0; i
< 10; i
++) {
381 data
->in
[i
] = w83627ehf_read_value(client
,
382 W83627EHF_REG_IN(i
));
383 data
->in_min
[i
] = w83627ehf_read_value(client
,
384 W83627EHF_REG_IN_MIN(i
));
385 data
->in_max
[i
] = w83627ehf_read_value(client
,
386 W83627EHF_REG_IN_MAX(i
));
389 /* Measured fan speeds and limits */
390 for (i
= 0; i
< 5; i
++) {
391 if (!(data
->has_fan
& (1 << i
)))
394 data
->fan
[i
] = w83627ehf_read_value(client
,
395 W83627EHF_REG_FAN
[i
]);
396 data
->fan_min
[i
] = w83627ehf_read_value(client
,
397 W83627EHF_REG_FAN_MIN
[i
]);
399 /* If we failed to measure the fan speed and clock
400 divider can be increased, let's try that for next
402 if (data
->fan
[i
] == 0xff
403 && data
->fan_div
[i
] < 0x07) {
404 dev_dbg(&client
->dev
, "Increasing fan %d "
405 "clock divider from %u to %u\n",
406 i
, div_from_reg(data
->fan_div
[i
]),
407 div_from_reg(data
->fan_div
[i
] + 1));
409 w83627ehf_write_fan_div(client
, i
);
410 /* Preserve min limit if possible */
411 if (data
->fan_min
[i
] >= 2
412 && data
->fan_min
[i
] != 255)
413 w83627ehf_write_value(client
,
414 W83627EHF_REG_FAN_MIN
[i
],
415 (data
->fan_min
[i
] /= 2));
419 /* Measured temperatures and limits */
420 data
->temp1
= w83627ehf_read_value(client
,
421 W83627EHF_REG_TEMP1
);
422 data
->temp1_max
= w83627ehf_read_value(client
,
423 W83627EHF_REG_TEMP1_OVER
);
424 data
->temp1_max_hyst
= w83627ehf_read_value(client
,
425 W83627EHF_REG_TEMP1_HYST
);
426 for (i
= 0; i
< 2; i
++) {
427 data
->temp
[i
] = w83627ehf_read_value(client
,
428 W83627EHF_REG_TEMP
[i
]);
429 data
->temp_max
[i
] = w83627ehf_read_value(client
,
430 W83627EHF_REG_TEMP_OVER
[i
]);
431 data
->temp_max_hyst
[i
] = w83627ehf_read_value(client
,
432 W83627EHF_REG_TEMP_HYST
[i
]);
435 data
->alarms
= w83627ehf_read_value(client
,
436 W83627EHF_REG_ALARM1
) |
437 (w83627ehf_read_value(client
,
438 W83627EHF_REG_ALARM2
) << 8) |
439 (w83627ehf_read_value(client
,
440 W83627EHF_REG_ALARM3
) << 16);
442 data
->last_updated
= jiffies
;
446 mutex_unlock(&data
->update_lock
);
451 * Sysfs callback functions
453 #define show_in_reg(reg) \
455 show_##reg(struct device *dev, struct device_attribute *attr, \
458 struct w83627ehf_data *data = w83627ehf_update_device(dev); \
459 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr); \
460 int nr = sensor_attr->index; \
461 return sprintf(buf, "%ld\n", in_from_reg(data->reg[nr], nr)); \
467 #define store_in_reg(REG, reg) \
469 store_in_##reg (struct device *dev, struct device_attribute *attr, \
470 const char *buf, size_t count) \
472 struct i2c_client *client = to_i2c_client(dev); \
473 struct w83627ehf_data *data = i2c_get_clientdata(client); \
474 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr); \
475 int nr = sensor_attr->index; \
476 u32 val = simple_strtoul(buf, NULL, 10); \
478 mutex_lock(&data->update_lock); \
479 data->in_##reg[nr] = in_to_reg(val, nr); \
480 w83627ehf_write_value(client, W83627EHF_REG_IN_##REG(nr), \
481 data->in_##reg[nr]); \
482 mutex_unlock(&data->update_lock); \
486 store_in_reg(MIN
, min
)
487 store_in_reg(MAX
, max
)
489 static ssize_t
show_alarm(struct device
*dev
, struct device_attribute
*attr
, char *buf
)
491 struct w83627ehf_data
*data
= w83627ehf_update_device(dev
);
492 struct sensor_device_attribute
*sensor_attr
= to_sensor_dev_attr(attr
);
493 int nr
= sensor_attr
->index
;
494 return sprintf(buf
, "%u\n", (data
->alarms
>> nr
) & 0x01);
497 static struct sensor_device_attribute sda_in_input
[] = {
498 SENSOR_ATTR(in0_input
, S_IRUGO
, show_in
, NULL
, 0),
499 SENSOR_ATTR(in1_input
, S_IRUGO
, show_in
, NULL
, 1),
500 SENSOR_ATTR(in2_input
, S_IRUGO
, show_in
, NULL
, 2),
501 SENSOR_ATTR(in3_input
, S_IRUGO
, show_in
, NULL
, 3),
502 SENSOR_ATTR(in4_input
, S_IRUGO
, show_in
, NULL
, 4),
503 SENSOR_ATTR(in5_input
, S_IRUGO
, show_in
, NULL
, 5),
504 SENSOR_ATTR(in6_input
, S_IRUGO
, show_in
, NULL
, 6),
505 SENSOR_ATTR(in7_input
, S_IRUGO
, show_in
, NULL
, 7),
506 SENSOR_ATTR(in8_input
, S_IRUGO
, show_in
, NULL
, 8),
507 SENSOR_ATTR(in9_input
, S_IRUGO
, show_in
, NULL
, 9),
510 static struct sensor_device_attribute sda_in_alarm
[] = {
511 SENSOR_ATTR(in0_alarm
, S_IRUGO
, show_alarm
, NULL
, 0),
512 SENSOR_ATTR(in1_alarm
, S_IRUGO
, show_alarm
, NULL
, 1),
513 SENSOR_ATTR(in2_alarm
, S_IRUGO
, show_alarm
, NULL
, 2),
514 SENSOR_ATTR(in3_alarm
, S_IRUGO
, show_alarm
, NULL
, 3),
515 SENSOR_ATTR(in4_alarm
, S_IRUGO
, show_alarm
, NULL
, 8),
516 SENSOR_ATTR(in5_alarm
, S_IRUGO
, show_alarm
, NULL
, 21),
517 SENSOR_ATTR(in6_alarm
, S_IRUGO
, show_alarm
, NULL
, 20),
518 SENSOR_ATTR(in7_alarm
, S_IRUGO
, show_alarm
, NULL
, 16),
519 SENSOR_ATTR(in8_alarm
, S_IRUGO
, show_alarm
, NULL
, 17),
520 SENSOR_ATTR(in9_alarm
, S_IRUGO
, show_alarm
, NULL
, 19),
523 static struct sensor_device_attribute sda_in_min
[] = {
524 SENSOR_ATTR(in0_min
, S_IWUSR
| S_IRUGO
, show_in_min
, store_in_min
, 0),
525 SENSOR_ATTR(in1_min
, S_IWUSR
| S_IRUGO
, show_in_min
, store_in_min
, 1),
526 SENSOR_ATTR(in2_min
, S_IWUSR
| S_IRUGO
, show_in_min
, store_in_min
, 2),
527 SENSOR_ATTR(in3_min
, S_IWUSR
| S_IRUGO
, show_in_min
, store_in_min
, 3),
528 SENSOR_ATTR(in4_min
, S_IWUSR
| S_IRUGO
, show_in_min
, store_in_min
, 4),
529 SENSOR_ATTR(in5_min
, S_IWUSR
| S_IRUGO
, show_in_min
, store_in_min
, 5),
530 SENSOR_ATTR(in6_min
, S_IWUSR
| S_IRUGO
, show_in_min
, store_in_min
, 6),
531 SENSOR_ATTR(in7_min
, S_IWUSR
| S_IRUGO
, show_in_min
, store_in_min
, 7),
532 SENSOR_ATTR(in8_min
, S_IWUSR
| S_IRUGO
, show_in_min
, store_in_min
, 8),
533 SENSOR_ATTR(in9_min
, S_IWUSR
| S_IRUGO
, show_in_min
, store_in_min
, 9),
536 static struct sensor_device_attribute sda_in_max
[] = {
537 SENSOR_ATTR(in0_max
, S_IWUSR
| S_IRUGO
, show_in_max
, store_in_max
, 0),
538 SENSOR_ATTR(in1_max
, S_IWUSR
| S_IRUGO
, show_in_max
, store_in_max
, 1),
539 SENSOR_ATTR(in2_max
, S_IWUSR
| S_IRUGO
, show_in_max
, store_in_max
, 2),
540 SENSOR_ATTR(in3_max
, S_IWUSR
| S_IRUGO
, show_in_max
, store_in_max
, 3),
541 SENSOR_ATTR(in4_max
, S_IWUSR
| S_IRUGO
, show_in_max
, store_in_max
, 4),
542 SENSOR_ATTR(in5_max
, S_IWUSR
| S_IRUGO
, show_in_max
, store_in_max
, 5),
543 SENSOR_ATTR(in6_max
, S_IWUSR
| S_IRUGO
, show_in_max
, store_in_max
, 6),
544 SENSOR_ATTR(in7_max
, S_IWUSR
| S_IRUGO
, show_in_max
, store_in_max
, 7),
545 SENSOR_ATTR(in8_max
, S_IWUSR
| S_IRUGO
, show_in_max
, store_in_max
, 8),
546 SENSOR_ATTR(in9_max
, S_IWUSR
| S_IRUGO
, show_in_max
, store_in_max
, 9),
549 static void device_create_file_in(struct device
*dev
, int i
)
551 device_create_file(dev
, &sda_in_input
[i
].dev_attr
);
552 device_create_file(dev
, &sda_in_alarm
[i
].dev_attr
);
553 device_create_file(dev
, &sda_in_min
[i
].dev_attr
);
554 device_create_file(dev
, &sda_in_max
[i
].dev_attr
);
557 #define show_fan_reg(reg) \
559 show_##reg(struct device *dev, struct device_attribute *attr, \
562 struct w83627ehf_data *data = w83627ehf_update_device(dev); \
563 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr); \
564 int nr = sensor_attr->index; \
565 return sprintf(buf, "%d\n", \
566 fan_from_reg(data->reg[nr], \
567 div_from_reg(data->fan_div[nr]))); \
570 show_fan_reg(fan_min
);
573 show_fan_div(struct device
*dev
, struct device_attribute
*attr
,
576 struct w83627ehf_data
*data
= w83627ehf_update_device(dev
);
577 struct sensor_device_attribute
*sensor_attr
= to_sensor_dev_attr(attr
);
578 int nr
= sensor_attr
->index
;
579 return sprintf(buf
, "%u\n", div_from_reg(data
->fan_div
[nr
]));
583 store_fan_min(struct device
*dev
, struct device_attribute
*attr
,
584 const char *buf
, size_t count
)
586 struct i2c_client
*client
= to_i2c_client(dev
);
587 struct w83627ehf_data
*data
= i2c_get_clientdata(client
);
588 struct sensor_device_attribute
*sensor_attr
= to_sensor_dev_attr(attr
);
589 int nr
= sensor_attr
->index
;
590 unsigned int val
= simple_strtoul(buf
, NULL
, 10);
594 mutex_lock(&data
->update_lock
);
596 /* No min limit, alarm disabled */
597 data
->fan_min
[nr
] = 255;
598 new_div
= data
->fan_div
[nr
]; /* No change */
599 dev_info(dev
, "fan%u low limit and alarm disabled\n", nr
+ 1);
600 } else if ((reg
= 1350000U / val
) >= 128 * 255) {
601 /* Speed below this value cannot possibly be represented,
602 even with the highest divider (128) */
603 data
->fan_min
[nr
] = 254;
604 new_div
= 7; /* 128 == (1 << 7) */
605 dev_warn(dev
, "fan%u low limit %u below minimum %u, set to "
606 "minimum\n", nr
+ 1, val
, fan_from_reg(254, 128));
608 /* Speed above this value cannot possibly be represented,
609 even with the lowest divider (1) */
610 data
->fan_min
[nr
] = 1;
611 new_div
= 0; /* 1 == (1 << 0) */
612 dev_warn(dev
, "fan%u low limit %u above maximum %u, set to "
613 "maximum\n", nr
+ 1, val
, fan_from_reg(1, 1));
615 /* Automatically pick the best divider, i.e. the one such
616 that the min limit will correspond to a register value
617 in the 96..192 range */
619 while (reg
> 192 && new_div
< 7) {
623 data
->fan_min
[nr
] = reg
;
626 /* Write both the fan clock divider (if it changed) and the new
627 fan min (unconditionally) */
628 if (new_div
!= data
->fan_div
[nr
]) {
629 if (new_div
> data
->fan_div
[nr
])
630 data
->fan
[nr
] >>= (data
->fan_div
[nr
] - new_div
);
632 data
->fan
[nr
] <<= (new_div
- data
->fan_div
[nr
]);
634 dev_dbg(dev
, "fan%u clock divider changed from %u to %u\n",
635 nr
+ 1, div_from_reg(data
->fan_div
[nr
]),
636 div_from_reg(new_div
));
637 data
->fan_div
[nr
] = new_div
;
638 w83627ehf_write_fan_div(client
, nr
);
640 w83627ehf_write_value(client
, W83627EHF_REG_FAN_MIN
[nr
],
642 mutex_unlock(&data
->update_lock
);
647 static struct sensor_device_attribute sda_fan_input
[] = {
648 SENSOR_ATTR(fan1_input
, S_IRUGO
, show_fan
, NULL
, 0),
649 SENSOR_ATTR(fan2_input
, S_IRUGO
, show_fan
, NULL
, 1),
650 SENSOR_ATTR(fan3_input
, S_IRUGO
, show_fan
, NULL
, 2),
651 SENSOR_ATTR(fan4_input
, S_IRUGO
, show_fan
, NULL
, 3),
652 SENSOR_ATTR(fan5_input
, S_IRUGO
, show_fan
, NULL
, 4),
655 static struct sensor_device_attribute sda_fan_alarm
[] = {
656 SENSOR_ATTR(fan1_alarm
, S_IRUGO
, show_alarm
, NULL
, 6),
657 SENSOR_ATTR(fan2_alarm
, S_IRUGO
, show_alarm
, NULL
, 7),
658 SENSOR_ATTR(fan3_alarm
, S_IRUGO
, show_alarm
, NULL
, 11),
659 SENSOR_ATTR(fan4_alarm
, S_IRUGO
, show_alarm
, NULL
, 10),
660 SENSOR_ATTR(fan5_alarm
, S_IRUGO
, show_alarm
, NULL
, 23),
663 static struct sensor_device_attribute sda_fan_min
[] = {
664 SENSOR_ATTR(fan1_min
, S_IWUSR
| S_IRUGO
, show_fan_min
,
666 SENSOR_ATTR(fan2_min
, S_IWUSR
| S_IRUGO
, show_fan_min
,
668 SENSOR_ATTR(fan3_min
, S_IWUSR
| S_IRUGO
, show_fan_min
,
670 SENSOR_ATTR(fan4_min
, S_IWUSR
| S_IRUGO
, show_fan_min
,
672 SENSOR_ATTR(fan5_min
, S_IWUSR
| S_IRUGO
, show_fan_min
,
676 static struct sensor_device_attribute sda_fan_div
[] = {
677 SENSOR_ATTR(fan1_div
, S_IRUGO
, show_fan_div
, NULL
, 0),
678 SENSOR_ATTR(fan2_div
, S_IRUGO
, show_fan_div
, NULL
, 1),
679 SENSOR_ATTR(fan3_div
, S_IRUGO
, show_fan_div
, NULL
, 2),
680 SENSOR_ATTR(fan4_div
, S_IRUGO
, show_fan_div
, NULL
, 3),
681 SENSOR_ATTR(fan5_div
, S_IRUGO
, show_fan_div
, NULL
, 4),
684 static void device_create_file_fan(struct device
*dev
, int i
)
686 device_create_file(dev
, &sda_fan_input
[i
].dev_attr
);
687 device_create_file(dev
, &sda_fan_alarm
[i
].dev_attr
);
688 device_create_file(dev
, &sda_fan_div
[i
].dev_attr
);
689 device_create_file(dev
, &sda_fan_min
[i
].dev_attr
);
692 #define show_temp1_reg(reg) \
694 show_##reg(struct device *dev, struct device_attribute *attr, \
697 struct w83627ehf_data *data = w83627ehf_update_device(dev); \
698 return sprintf(buf, "%d\n", temp1_from_reg(data->reg)); \
700 show_temp1_reg(temp1
);
701 show_temp1_reg(temp1_max
);
702 show_temp1_reg(temp1_max_hyst
);
704 #define store_temp1_reg(REG, reg) \
706 store_temp1_##reg(struct device *dev, struct device_attribute *attr, \
707 const char *buf, size_t count) \
709 struct i2c_client *client = to_i2c_client(dev); \
710 struct w83627ehf_data *data = i2c_get_clientdata(client); \
711 u32 val = simple_strtoul(buf, NULL, 10); \
713 mutex_lock(&data->update_lock); \
714 data->temp1_##reg = temp1_to_reg(val); \
715 w83627ehf_write_value(client, W83627EHF_REG_TEMP1_##REG, \
716 data->temp1_##reg); \
717 mutex_unlock(&data->update_lock); \
720 store_temp1_reg(OVER
, max
);
721 store_temp1_reg(HYST
, max_hyst
);
723 #define show_temp_reg(reg) \
725 show_##reg(struct device *dev, struct device_attribute *attr, \
728 struct w83627ehf_data *data = w83627ehf_update_device(dev); \
729 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr); \
730 int nr = sensor_attr->index; \
731 return sprintf(buf, "%d\n", \
732 LM75_TEMP_FROM_REG(data->reg[nr])); \
735 show_temp_reg(temp_max
);
736 show_temp_reg(temp_max_hyst
);
738 #define store_temp_reg(REG, reg) \
740 store_##reg(struct device *dev, struct device_attribute *attr, \
741 const char *buf, size_t count) \
743 struct i2c_client *client = to_i2c_client(dev); \
744 struct w83627ehf_data *data = i2c_get_clientdata(client); \
745 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr); \
746 int nr = sensor_attr->index; \
747 u32 val = simple_strtoul(buf, NULL, 10); \
749 mutex_lock(&data->update_lock); \
750 data->reg[nr] = LM75_TEMP_TO_REG(val); \
751 w83627ehf_write_value(client, W83627EHF_REG_TEMP_##REG[nr], \
753 mutex_unlock(&data->update_lock); \
756 store_temp_reg(OVER
, temp_max
);
757 store_temp_reg(HYST
, temp_max_hyst
);
759 static struct sensor_device_attribute sda_temp
[] = {
760 SENSOR_ATTR(temp1_input
, S_IRUGO
, show_temp1
, NULL
, 0),
761 SENSOR_ATTR(temp2_input
, S_IRUGO
, show_temp
, NULL
, 0),
762 SENSOR_ATTR(temp3_input
, S_IRUGO
, show_temp
, NULL
, 1),
763 SENSOR_ATTR(temp1_max
, S_IRUGO
| S_IWUSR
, show_temp1_max
,
765 SENSOR_ATTR(temp2_max
, S_IRUGO
| S_IWUSR
, show_temp_max
,
767 SENSOR_ATTR(temp3_max
, S_IRUGO
| S_IWUSR
, show_temp_max
,
769 SENSOR_ATTR(temp1_max_hyst
, S_IRUGO
| S_IWUSR
, show_temp1_max_hyst
,
770 store_temp1_max_hyst
, 0),
771 SENSOR_ATTR(temp2_max_hyst
, S_IRUGO
| S_IWUSR
, show_temp_max_hyst
,
772 store_temp_max_hyst
, 0),
773 SENSOR_ATTR(temp3_max_hyst
, S_IRUGO
| S_IWUSR
, show_temp_max_hyst
,
774 store_temp_max_hyst
, 1),
775 SENSOR_ATTR(temp1_alarm
, S_IRUGO
, show_alarm
, NULL
, 4),
776 SENSOR_ATTR(temp2_alarm
, S_IRUGO
, show_alarm
, NULL
, 5),
777 SENSOR_ATTR(temp3_alarm
, S_IRUGO
, show_alarm
, NULL
, 13),
781 * Driver and client management
784 static struct i2c_driver w83627ehf_driver
;
786 static void w83627ehf_init_client(struct i2c_client
*client
)
791 /* Start monitoring is needed */
792 tmp
= w83627ehf_read_value(client
, W83627EHF_REG_CONFIG
);
794 w83627ehf_write_value(client
, W83627EHF_REG_CONFIG
,
797 /* Enable temp2 and temp3 if needed */
798 for (i
= 0; i
< 2; i
++) {
799 tmp
= w83627ehf_read_value(client
,
800 W83627EHF_REG_TEMP_CONFIG
[i
]);
802 w83627ehf_write_value(client
,
803 W83627EHF_REG_TEMP_CONFIG
[i
],
808 static int w83627ehf_detect(struct i2c_adapter
*adapter
)
810 struct i2c_client
*client
;
811 struct w83627ehf_data
*data
;
815 if (!request_region(address
+ REGION_OFFSET
, REGION_LENGTH
,
816 w83627ehf_driver
.driver
.name
)) {
821 if (!(data
= kzalloc(sizeof(struct w83627ehf_data
), GFP_KERNEL
))) {
826 client
= &data
->client
;
827 i2c_set_clientdata(client
, data
);
828 client
->addr
= address
;
829 mutex_init(&data
->lock
);
830 client
->adapter
= adapter
;
831 client
->driver
= &w83627ehf_driver
;
835 strlcpy(client
->name
, "w83627ehf", I2C_NAME_SIZE
);
837 mutex_init(&data
->update_lock
);
839 /* Tell the i2c layer a new client has arrived */
840 if ((err
= i2c_attach_client(client
)))
843 /* Initialize the chip */
844 w83627ehf_init_client(client
);
846 /* A few vars need to be filled upon startup */
847 for (i
= 0; i
< 5; i
++)
848 data
->fan_min
[i
] = w83627ehf_read_value(client
,
849 W83627EHF_REG_FAN_MIN
[i
]);
851 /* It looks like fan4 and fan5 pins can be alternatively used
852 as fan on/off switches */
853 data
->has_fan
= 0x07; /* fan1, fan2 and fan3 */
854 i
= w83627ehf_read_value(client
, W83627EHF_REG_FANDIV1
);
856 data
->has_fan
|= (1 << 3);
858 data
->has_fan
|= (1 << 4);
860 /* Register sysfs hooks */
861 data
->class_dev
= hwmon_device_register(dev
);
862 if (IS_ERR(data
->class_dev
)) {
863 err
= PTR_ERR(data
->class_dev
);
867 for (i
= 0; i
< 10; i
++)
868 device_create_file_in(dev
, i
);
870 for (i
= 0; i
< 5; i
++) {
871 if (data
->has_fan
& (1 << i
))
872 device_create_file_fan(dev
, i
);
874 for (i
= 0; i
< ARRAY_SIZE(sda_temp
); i
++)
875 device_create_file(dev
, &sda_temp
[i
].dev_attr
);
880 i2c_detach_client(client
);
884 release_region(address
+ REGION_OFFSET
, REGION_LENGTH
);
889 static int w83627ehf_detach_client(struct i2c_client
*client
)
891 struct w83627ehf_data
*data
= i2c_get_clientdata(client
);
894 hwmon_device_unregister(data
->class_dev
);
896 if ((err
= i2c_detach_client(client
)))
898 release_region(client
->addr
+ REGION_OFFSET
, REGION_LENGTH
);
904 static struct i2c_driver w83627ehf_driver
= {
908 .attach_adapter
= w83627ehf_detect
,
909 .detach_client
= w83627ehf_detach_client
,
912 static int __init
w83627ehf_find(int sioaddr
, unsigned short *addr
)
920 val
= (superio_inb(SIO_REG_DEVID
) << 8)
921 | superio_inb(SIO_REG_DEVID
+ 1);
922 if ((val
& SIO_ID_MASK
) != SIO_W83627EHF_ID
) {
927 superio_select(W83627EHF_LD_HWM
);
928 val
= (superio_inb(SIO_REG_ADDR
) << 8)
929 | superio_inb(SIO_REG_ADDR
+ 1);
930 *addr
= val
& REGION_ALIGNMENT
;
936 /* Activate logical device if needed */
937 val
= superio_inb(SIO_REG_ENABLE
);
939 superio_outb(SIO_REG_ENABLE
, val
| 0x01);
945 static int __init
sensors_w83627ehf_init(void)
947 if (w83627ehf_find(0x2e, &address
)
948 && w83627ehf_find(0x4e, &address
))
951 return i2c_isa_add_driver(&w83627ehf_driver
);
954 static void __exit
sensors_w83627ehf_exit(void)
956 i2c_isa_del_driver(&w83627ehf_driver
);
959 MODULE_AUTHOR("Jean Delvare <khali@linux-fr.org>");
960 MODULE_DESCRIPTION("W83627EHF driver");
961 MODULE_LICENSE("GPL");
963 module_init(sensors_w83627ehf_init
);
964 module_exit(sensors_w83627ehf_exit
);