2 * w83793.c - Linux kernel driver for hardware monitoring
3 * Copyright (C) 2006 Winbond Electronics Corp.
5 * Rudolf Marek <r.marek@assembler.cz>
6 * Copyright (C) 2009-2010 Sven Anders <anders@anduras.de>, ANDURAS AG.
8 * (Based partially on fschmd driver,
9 * Copyright 2007-2008 by Hans de Goede)
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation - version 2.
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., 51 Franklin Street, Fifth Floor, Boston, MA
27 * Supports following chips:
29 * Chip #vin #fanin #pwm #temp wchipid vendid i2c ISA
30 * w83793 10 12 8 6 0x7b 0x5ca3 yes no
33 #include <linux/module.h>
34 #include <linux/init.h>
35 #include <linux/slab.h>
36 #include <linux/i2c.h>
37 #include <linux/hwmon.h>
38 #include <linux/hwmon-vid.h>
39 #include <linux/hwmon-sysfs.h>
40 #include <linux/err.h>
41 #include <linux/mutex.h>
43 #include <linux/watchdog.h>
44 #include <linux/miscdevice.h>
45 #include <linux/uaccess.h>
46 #include <linux/kref.h>
47 #include <linux/notifier.h>
48 #include <linux/reboot.h>
49 #include <linux/jiffies.h>
52 #define WATCHDOG_TIMEOUT 2 /* 2 minute default timeout */
54 /* Addresses to scan */
55 static const unsigned short normal_i2c
[] = { 0x2c, 0x2d, 0x2e, 0x2f,
58 /* Insmod parameters */
60 static unsigned short force_subclients
[4];
61 module_param_array(force_subclients
, short, NULL
, 0);
62 MODULE_PARM_DESC(force_subclients
, "List of subclient addresses: "
63 "{bus, clientaddr, subclientaddr1, subclientaddr2}");
66 module_param(reset
, bool, 0);
67 MODULE_PARM_DESC(reset
, "Set to 1 to reset chip, not recommended");
69 static int timeout
= WATCHDOG_TIMEOUT
; /* default timeout in minutes */
70 module_param(timeout
, int, 0);
71 MODULE_PARM_DESC(timeout
,
72 "Watchdog timeout in minutes. 2<= timeout <=255 (default="
73 __MODULE_STRING(WATCHDOG_TIMEOUT
) ")");
75 static bool nowayout
= WATCHDOG_NOWAYOUT
;
76 module_param(nowayout
, bool, 0);
77 MODULE_PARM_DESC(nowayout
,
78 "Watchdog cannot be stopped once started (default="
79 __MODULE_STRING(WATCHDOG_NOWAYOUT
) ")");
82 * Address 0x00, 0x0d, 0x0e, 0x0f in all three banks are reserved
83 * as ID, Bank Select registers
85 #define W83793_REG_BANKSEL 0x00
86 #define W83793_REG_VENDORID 0x0d
87 #define W83793_REG_CHIPID 0x0e
88 #define W83793_REG_DEVICEID 0x0f
90 #define W83793_REG_CONFIG 0x40
91 #define W83793_REG_MFC 0x58
92 #define W83793_REG_FANIN_CTRL 0x5c
93 #define W83793_REG_FANIN_SEL 0x5d
94 #define W83793_REG_I2C_ADDR 0x0b
95 #define W83793_REG_I2C_SUBADDR 0x0c
96 #define W83793_REG_VID_INA 0x05
97 #define W83793_REG_VID_INB 0x06
98 #define W83793_REG_VID_LATCHA 0x07
99 #define W83793_REG_VID_LATCHB 0x08
100 #define W83793_REG_VID_CTRL 0x59
102 #define W83793_REG_WDT_LOCK 0x01
103 #define W83793_REG_WDT_ENABLE 0x02
104 #define W83793_REG_WDT_STATUS 0x03
105 #define W83793_REG_WDT_TIMEOUT 0x04
107 static u16 W83793_REG_TEMP_MODE
[2] = { 0x5e, 0x5f };
111 #define TEMP_CRIT_HYST 2
113 #define TEMP_WARN_HYST 4
115 * only crit and crit_hyst affect real-time alarm status
116 * current crit crit_hyst warn warn_hyst
118 static u16 W83793_REG_TEMP
[][5] = {
119 {0x1c, 0x78, 0x79, 0x7a, 0x7b},
120 {0x1d, 0x7c, 0x7d, 0x7e, 0x7f},
121 {0x1e, 0x80, 0x81, 0x82, 0x83},
122 {0x1f, 0x84, 0x85, 0x86, 0x87},
123 {0x20, 0x88, 0x89, 0x8a, 0x8b},
124 {0x21, 0x8c, 0x8d, 0x8e, 0x8f},
127 #define W83793_REG_TEMP_LOW_BITS 0x22
129 #define W83793_REG_BEEP(index) (0x53 + (index))
130 #define W83793_REG_ALARM(index) (0x4b + (index))
132 #define W83793_REG_CLR_CHASSIS 0x4a /* SMI MASK4 */
133 #define W83793_REG_IRQ_CTRL 0x50
134 #define W83793_REG_OVT_CTRL 0x51
135 #define W83793_REG_OVT_BEEP 0x52
140 static const u16 W83793_REG_IN
[][3] = {
141 /* Current, High, Low */
142 {0x10, 0x60, 0x61}, /* Vcore A */
143 {0x11, 0x62, 0x63}, /* Vcore B */
144 {0x12, 0x64, 0x65}, /* Vtt */
145 {0x14, 0x6a, 0x6b}, /* VSEN1 */
146 {0x15, 0x6c, 0x6d}, /* VSEN2 */
147 {0x16, 0x6e, 0x6f}, /* +3VSEN */
148 {0x17, 0x70, 0x71}, /* +12VSEN */
149 {0x18, 0x72, 0x73}, /* 5VDD */
150 {0x19, 0x74, 0x75}, /* 5VSB */
151 {0x1a, 0x76, 0x77}, /* VBAT */
154 /* Low Bits of Vcore A/B Vtt Read/High/Low */
155 static const u16 W83793_REG_IN_LOW_BITS
[] = { 0x1b, 0x68, 0x69 };
156 static u8 scale_in
[] = { 2, 2, 2, 16, 16, 16, 8, 24, 24, 16 };
157 static u8 scale_in_add
[] = { 0, 0, 0, 0, 0, 0, 0, 150, 150, 0 };
159 #define W83793_REG_FAN(index) (0x23 + 2 * (index)) /* High byte */
160 #define W83793_REG_FAN_MIN(index) (0x90 + 2 * (index)) /* High byte */
162 #define W83793_REG_PWM_DEFAULT 0xb2
163 #define W83793_REG_PWM_ENABLE 0x207
164 #define W83793_REG_PWM_UPTIME 0xc3 /* Unit in 0.1 second */
165 #define W83793_REG_PWM_DOWNTIME 0xc4 /* Unit in 0.1 second */
166 #define W83793_REG_TEMP_CRITICAL 0xc5
170 #define PWM_NONSTOP 2
171 #define PWM_STOP_TIME 3
172 #define W83793_REG_PWM(index, nr) (((nr) == 0 ? 0xb3 : \
173 (nr) == 1 ? 0x220 : 0x218) + (index))
175 /* bit field, fan1 is bit0, fan2 is bit1 ... */
176 #define W83793_REG_TEMP_FAN_MAP(index) (0x201 + (index))
177 #define W83793_REG_TEMP_TOL(index) (0x208 + (index))
178 #define W83793_REG_TEMP_CRUISE(index) (0x210 + (index))
179 #define W83793_REG_PWM_STOP_TIME(index) (0x228 + (index))
180 #define W83793_REG_SF2_TEMP(index, nr) (0x230 + ((index) << 4) + (nr))
181 #define W83793_REG_SF2_PWM(index, nr) (0x238 + ((index) << 4) + (nr))
183 static inline unsigned long FAN_FROM_REG(u16 val
)
185 if ((val
>= 0xfff) || (val
== 0))
187 return 1350000UL / val
;
190 static inline u16
FAN_TO_REG(long rpm
)
194 return SENSORS_LIMIT((1350000 + (rpm
>> 1)) / rpm
, 1, 0xffe);
197 static inline unsigned long TIME_FROM_REG(u8 reg
)
202 static inline u8
TIME_TO_REG(unsigned long val
)
204 return SENSORS_LIMIT((val
+ 50) / 100, 0, 0xff);
207 static inline long TEMP_FROM_REG(s8 reg
)
212 static inline s8
TEMP_TO_REG(long val
, s8 min
, s8 max
)
214 return SENSORS_LIMIT((val
+ (val
< 0 ? -500 : 500)) / 1000, min
, max
);
218 struct i2c_client
*lm75
[2];
219 struct device
*hwmon_dev
;
220 struct mutex update_lock
;
221 char valid
; /* !=0 if following fields are valid */
222 unsigned long last_updated
; /* In jiffies */
223 unsigned long last_nonvolatile
; /* In jiffies, last time we update the
224 * nonvolatile registers
230 u8 in
[10][3]; /* Register value, read/high/low */
231 u8 in_low_bits
[3]; /* Additional resolution for VCore A/B Vtt */
233 u16 has_fan
; /* Only fan1- fan5 has own pins */
234 u16 fan
[12]; /* Register value combine */
235 u16 fan_min
[12]; /* Register value combine */
237 s8 temp
[6][5]; /* current, crit, crit_hyst,warn, warn_hyst */
238 u8 temp_low_bits
; /* Additional resolution TD1-TD4 */
239 u8 temp_mode
[2]; /* byte 0: Temp D1-D4 mode each has 2 bits
240 * byte 1: Temp R1,R2 mode, each has 1 bit
242 u8 temp_critical
; /* If reached all fan will be at full speed */
243 u8 temp_fan_map
[6]; /* Temp controls which pwm fan, bit field */
248 u8 pwm_enable
; /* Register value, each Temp has 1 bit */
249 u8 pwm_uptime
; /* Register value */
250 u8 pwm_downtime
; /* Register value */
251 u8 pwm_default
; /* All fan default pwm, next poweron valid */
252 u8 pwm
[8][3]; /* Register value */
256 u8 alarms
[5]; /* realtime status registers */
259 u8 tolerance
[3]; /* Temp tolerance(Smart Fan I/II) */
260 u8 sf2_pwm
[6][7]; /* Smart FanII: Fan duty cycle */
261 u8 sf2_temp
[6][7]; /* Smart FanII: Temp level point */
264 struct i2c_client
*client
;
265 struct mutex watchdog_lock
;
266 struct list_head list
; /* member of the watchdog_data_list */
268 struct miscdevice watchdog_miscdev
;
269 unsigned long watchdog_is_open
;
270 char watchdog_expect_close
;
271 char watchdog_name
[10]; /* must be unique to avoid sysfs conflict */
272 unsigned int watchdog_caused_reboot
;
273 int watchdog_timeout
; /* watchdog timeout in minutes */
277 * Somewhat ugly :( global data pointer list with all devices, so that
278 * we can find our device data as when using misc_register. There is no
279 * other method to get to one's device data from the open file-op and
280 * for usage in the reboot notifier callback.
282 static LIST_HEAD(watchdog_data_list
);
284 /* Note this lock not only protect list access, but also data.kref access */
285 static DEFINE_MUTEX(watchdog_data_mutex
);
288 * Release our data struct when we're detached from the i2c client *and* all
289 * references to our watchdog device are released
291 static void w83793_release_resources(struct kref
*ref
)
293 struct w83793_data
*data
= container_of(ref
, struct w83793_data
, kref
);
297 static u8
w83793_read_value(struct i2c_client
*client
, u16 reg
);
298 static int w83793_write_value(struct i2c_client
*client
, u16 reg
, u8 value
);
299 static int w83793_probe(struct i2c_client
*client
,
300 const struct i2c_device_id
*id
);
301 static int w83793_detect(struct i2c_client
*client
,
302 struct i2c_board_info
*info
);
303 static int w83793_remove(struct i2c_client
*client
);
304 static void w83793_init_client(struct i2c_client
*client
);
305 static void w83793_update_nonvolatile(struct device
*dev
);
306 static struct w83793_data
*w83793_update_device(struct device
*dev
);
308 static const struct i2c_device_id w83793_id
[] = {
312 MODULE_DEVICE_TABLE(i2c
, w83793_id
);
314 static struct i2c_driver w83793_driver
= {
315 .class = I2C_CLASS_HWMON
,
319 .probe
= w83793_probe
,
320 .remove
= w83793_remove
,
321 .id_table
= w83793_id
,
322 .detect
= w83793_detect
,
323 .address_list
= normal_i2c
,
327 show_vrm(struct device
*dev
, struct device_attribute
*attr
, char *buf
)
329 struct w83793_data
*data
= dev_get_drvdata(dev
);
330 return sprintf(buf
, "%d\n", data
->vrm
);
334 show_vid(struct device
*dev
, struct device_attribute
*attr
, char *buf
)
336 struct w83793_data
*data
= w83793_update_device(dev
);
337 struct sensor_device_attribute_2
*sensor_attr
=
338 to_sensor_dev_attr_2(attr
);
339 int index
= sensor_attr
->index
;
341 return sprintf(buf
, "%d\n", vid_from_reg(data
->vid
[index
], data
->vrm
));
345 store_vrm(struct device
*dev
, struct device_attribute
*attr
,
346 const char *buf
, size_t count
)
348 struct w83793_data
*data
= dev_get_drvdata(dev
);
352 err
= kstrtoul(buf
, 10, &val
);
360 #define ALARM_STATUS 0
361 #define BEEP_ENABLE 1
363 show_alarm_beep(struct device
*dev
, struct device_attribute
*attr
, char *buf
)
365 struct w83793_data
*data
= w83793_update_device(dev
);
366 struct sensor_device_attribute_2
*sensor_attr
=
367 to_sensor_dev_attr_2(attr
);
368 int nr
= sensor_attr
->nr
;
369 int index
= sensor_attr
->index
>> 3;
370 int bit
= sensor_attr
->index
& 0x07;
373 if (nr
== ALARM_STATUS
) {
374 val
= (data
->alarms
[index
] >> (bit
)) & 1;
375 } else { /* BEEP_ENABLE */
376 val
= (data
->beeps
[index
] >> (bit
)) & 1;
379 return sprintf(buf
, "%u\n", val
);
383 store_beep(struct device
*dev
, struct device_attribute
*attr
,
384 const char *buf
, size_t count
)
386 struct i2c_client
*client
= to_i2c_client(dev
);
387 struct w83793_data
*data
= i2c_get_clientdata(client
);
388 struct sensor_device_attribute_2
*sensor_attr
=
389 to_sensor_dev_attr_2(attr
);
390 int index
= sensor_attr
->index
>> 3;
391 int shift
= sensor_attr
->index
& 0x07;
392 u8 beep_bit
= 1 << shift
;
396 err
= kstrtoul(buf
, 10, &val
);
403 mutex_lock(&data
->update_lock
);
404 data
->beeps
[index
] = w83793_read_value(client
, W83793_REG_BEEP(index
));
405 data
->beeps
[index
] &= ~beep_bit
;
406 data
->beeps
[index
] |= val
<< shift
;
407 w83793_write_value(client
, W83793_REG_BEEP(index
), data
->beeps
[index
]);
408 mutex_unlock(&data
->update_lock
);
414 show_beep_enable(struct device
*dev
, struct device_attribute
*attr
, char *buf
)
416 struct w83793_data
*data
= w83793_update_device(dev
);
417 return sprintf(buf
, "%u\n", (data
->beep_enable
>> 1) & 0x01);
421 store_beep_enable(struct device
*dev
, struct device_attribute
*attr
,
422 const char *buf
, size_t count
)
424 struct i2c_client
*client
= to_i2c_client(dev
);
425 struct w83793_data
*data
= i2c_get_clientdata(client
);
429 err
= kstrtoul(buf
, 10, &val
);
436 mutex_lock(&data
->update_lock
);
437 data
->beep_enable
= w83793_read_value(client
, W83793_REG_OVT_BEEP
)
439 data
->beep_enable
|= val
<< 1;
440 w83793_write_value(client
, W83793_REG_OVT_BEEP
, data
->beep_enable
);
441 mutex_unlock(&data
->update_lock
);
446 /* Write 0 to clear chassis alarm */
448 store_chassis_clear(struct device
*dev
,
449 struct device_attribute
*attr
, const char *buf
,
452 struct i2c_client
*client
= to_i2c_client(dev
);
453 struct w83793_data
*data
= i2c_get_clientdata(client
);
458 err
= kstrtoul(buf
, 10, &val
);
464 mutex_lock(&data
->update_lock
);
465 reg
= w83793_read_value(client
, W83793_REG_CLR_CHASSIS
);
466 w83793_write_value(client
, W83793_REG_CLR_CHASSIS
, reg
| 0x80);
467 data
->valid
= 0; /* Force cache refresh */
468 mutex_unlock(&data
->update_lock
);
475 show_fan(struct device
*dev
, struct device_attribute
*attr
, char *buf
)
477 struct sensor_device_attribute_2
*sensor_attr
=
478 to_sensor_dev_attr_2(attr
);
479 int nr
= sensor_attr
->nr
;
480 int index
= sensor_attr
->index
;
481 struct w83793_data
*data
= w83793_update_device(dev
);
485 val
= data
->fan
[index
] & 0x0fff;
487 val
= data
->fan_min
[index
] & 0x0fff;
489 return sprintf(buf
, "%lu\n", FAN_FROM_REG(val
));
493 store_fan_min(struct device
*dev
, struct device_attribute
*attr
,
494 const char *buf
, size_t count
)
496 struct sensor_device_attribute_2
*sensor_attr
=
497 to_sensor_dev_attr_2(attr
);
498 int index
= sensor_attr
->index
;
499 struct i2c_client
*client
= to_i2c_client(dev
);
500 struct w83793_data
*data
= i2c_get_clientdata(client
);
504 err
= kstrtoul(buf
, 10, &val
);
507 val
= FAN_TO_REG(val
);
509 mutex_lock(&data
->update_lock
);
510 data
->fan_min
[index
] = val
;
511 w83793_write_value(client
, W83793_REG_FAN_MIN(index
),
513 w83793_write_value(client
, W83793_REG_FAN_MIN(index
) + 1, val
& 0xff);
514 mutex_unlock(&data
->update_lock
);
520 show_pwm(struct device
*dev
, struct device_attribute
*attr
, char *buf
)
522 struct sensor_device_attribute_2
*sensor_attr
=
523 to_sensor_dev_attr_2(attr
);
524 struct w83793_data
*data
= w83793_update_device(dev
);
526 int nr
= sensor_attr
->nr
;
527 int index
= sensor_attr
->index
;
529 if (nr
== PWM_STOP_TIME
)
530 val
= TIME_FROM_REG(data
->pwm_stop_time
[index
]);
532 val
= (data
->pwm
[index
][nr
] & 0x3f) << 2;
534 return sprintf(buf
, "%d\n", val
);
538 store_pwm(struct device
*dev
, struct device_attribute
*attr
,
539 const char *buf
, size_t count
)
541 struct i2c_client
*client
= to_i2c_client(dev
);
542 struct w83793_data
*data
= i2c_get_clientdata(client
);
543 struct sensor_device_attribute_2
*sensor_attr
=
544 to_sensor_dev_attr_2(attr
);
545 int nr
= sensor_attr
->nr
;
546 int index
= sensor_attr
->index
;
550 err
= kstrtoul(buf
, 10, &val
);
554 mutex_lock(&data
->update_lock
);
555 if (nr
== PWM_STOP_TIME
) {
556 val
= TIME_TO_REG(val
);
557 data
->pwm_stop_time
[index
] = val
;
558 w83793_write_value(client
, W83793_REG_PWM_STOP_TIME(index
),
561 val
= SENSORS_LIMIT(val
, 0, 0xff) >> 2;
562 data
->pwm
[index
][nr
] =
563 w83793_read_value(client
, W83793_REG_PWM(index
, nr
)) & 0xc0;
564 data
->pwm
[index
][nr
] |= val
;
565 w83793_write_value(client
, W83793_REG_PWM(index
, nr
),
566 data
->pwm
[index
][nr
]);
569 mutex_unlock(&data
->update_lock
);
574 show_temp(struct device
*dev
, struct device_attribute
*attr
, char *buf
)
576 struct sensor_device_attribute_2
*sensor_attr
=
577 to_sensor_dev_attr_2(attr
);
578 int nr
= sensor_attr
->nr
;
579 int index
= sensor_attr
->index
;
580 struct w83793_data
*data
= w83793_update_device(dev
);
581 long temp
= TEMP_FROM_REG(data
->temp
[index
][nr
]);
583 if (nr
== TEMP_READ
&& index
< 4) { /* Only TD1-TD4 have low bits */
584 int low
= ((data
->temp_low_bits
>> (index
* 2)) & 0x03) * 250;
585 temp
+= temp
> 0 ? low
: -low
;
587 return sprintf(buf
, "%ld\n", temp
);
591 store_temp(struct device
*dev
, struct device_attribute
*attr
,
592 const char *buf
, size_t count
)
594 struct sensor_device_attribute_2
*sensor_attr
=
595 to_sensor_dev_attr_2(attr
);
596 int nr
= sensor_attr
->nr
;
597 int index
= sensor_attr
->index
;
598 struct i2c_client
*client
= to_i2c_client(dev
);
599 struct w83793_data
*data
= i2c_get_clientdata(client
);
603 err
= kstrtol(buf
, 10, &tmp
);
607 mutex_lock(&data
->update_lock
);
608 data
->temp
[index
][nr
] = TEMP_TO_REG(tmp
, -128, 127);
609 w83793_write_value(client
, W83793_REG_TEMP
[index
][nr
],
610 data
->temp
[index
][nr
]);
611 mutex_unlock(&data
->update_lock
);
617 * each has 4 mode:(2 bits)
619 * 1: Use internal temp sensor(default)
621 * 3: Use sensor in Intel CPU and get result by PECI
624 * each has 2 mode:(1 bit)
625 * 0: Disable temp sensor monitor
626 * 1: To enable temp sensors monitor
629 /* 0 disable, 6 PECI */
630 static u8 TO_TEMP_MODE
[] = { 0, 0, 0, 6 };
633 show_temp_mode(struct device
*dev
, struct device_attribute
*attr
, char *buf
)
635 struct w83793_data
*data
= w83793_update_device(dev
);
636 struct sensor_device_attribute_2
*sensor_attr
=
637 to_sensor_dev_attr_2(attr
);
638 int index
= sensor_attr
->index
;
639 u8 mask
= (index
< 4) ? 0x03 : 0x01;
640 u8 shift
= (index
< 4) ? (2 * index
) : (index
- 4);
642 index
= (index
< 4) ? 0 : 1;
644 tmp
= (data
->temp_mode
[index
] >> shift
) & mask
;
646 /* for the internal sensor, found out if diode or thermistor */
648 tmp
= index
== 0 ? 3 : 4;
650 tmp
= TO_TEMP_MODE
[tmp
];
652 return sprintf(buf
, "%d\n", tmp
);
656 store_temp_mode(struct device
*dev
, struct device_attribute
*attr
,
657 const char *buf
, size_t count
)
659 struct i2c_client
*client
= to_i2c_client(dev
);
660 struct w83793_data
*data
= i2c_get_clientdata(client
);
661 struct sensor_device_attribute_2
*sensor_attr
=
662 to_sensor_dev_attr_2(attr
);
663 int index
= sensor_attr
->index
;
664 u8 mask
= (index
< 4) ? 0x03 : 0x01;
665 u8 shift
= (index
< 4) ? (2 * index
) : (index
- 4);
669 err
= kstrtoul(buf
, 10, &val
);
673 /* transform the sysfs interface values into table above */
674 if ((val
== 6) && (index
< 4)) {
676 } else if ((val
== 3 && index
< 4)
677 || (val
== 4 && index
>= 4)) {
678 /* transform diode or thermistor into internal enable */
684 index
= (index
< 4) ? 0 : 1;
685 mutex_lock(&data
->update_lock
);
686 data
->temp_mode
[index
] =
687 w83793_read_value(client
, W83793_REG_TEMP_MODE
[index
]);
688 data
->temp_mode
[index
] &= ~(mask
<< shift
);
689 data
->temp_mode
[index
] |= val
<< shift
;
690 w83793_write_value(client
, W83793_REG_TEMP_MODE
[index
],
691 data
->temp_mode
[index
]);
692 mutex_unlock(&data
->update_lock
);
697 #define SETUP_PWM_DEFAULT 0
698 #define SETUP_PWM_UPTIME 1 /* Unit in 0.1s */
699 #define SETUP_PWM_DOWNTIME 2 /* Unit in 0.1s */
700 #define SETUP_TEMP_CRITICAL 3
702 show_sf_setup(struct device
*dev
, struct device_attribute
*attr
, char *buf
)
704 struct sensor_device_attribute_2
*sensor_attr
=
705 to_sensor_dev_attr_2(attr
);
706 int nr
= sensor_attr
->nr
;
707 struct w83793_data
*data
= w83793_update_device(dev
);
710 if (nr
== SETUP_PWM_DEFAULT
)
711 val
= (data
->pwm_default
& 0x3f) << 2;
712 else if (nr
== SETUP_PWM_UPTIME
)
713 val
= TIME_FROM_REG(data
->pwm_uptime
);
714 else if (nr
== SETUP_PWM_DOWNTIME
)
715 val
= TIME_FROM_REG(data
->pwm_downtime
);
716 else if (nr
== SETUP_TEMP_CRITICAL
)
717 val
= TEMP_FROM_REG(data
->temp_critical
& 0x7f);
719 return sprintf(buf
, "%d\n", val
);
723 store_sf_setup(struct device
*dev
, struct device_attribute
*attr
,
724 const char *buf
, size_t count
)
726 struct sensor_device_attribute_2
*sensor_attr
=
727 to_sensor_dev_attr_2(attr
);
728 int nr
= sensor_attr
->nr
;
729 struct i2c_client
*client
= to_i2c_client(dev
);
730 struct w83793_data
*data
= i2c_get_clientdata(client
);
734 err
= kstrtol(buf
, 10, &val
);
738 mutex_lock(&data
->update_lock
);
739 if (nr
== SETUP_PWM_DEFAULT
) {
741 w83793_read_value(client
, W83793_REG_PWM_DEFAULT
) & 0xc0;
742 data
->pwm_default
|= SENSORS_LIMIT(val
, 0, 0xff) >> 2;
743 w83793_write_value(client
, W83793_REG_PWM_DEFAULT
,
745 } else if (nr
== SETUP_PWM_UPTIME
) {
746 data
->pwm_uptime
= TIME_TO_REG(val
);
747 data
->pwm_uptime
+= data
->pwm_uptime
== 0 ? 1 : 0;
748 w83793_write_value(client
, W83793_REG_PWM_UPTIME
,
750 } else if (nr
== SETUP_PWM_DOWNTIME
) {
751 data
->pwm_downtime
= TIME_TO_REG(val
);
752 data
->pwm_downtime
+= data
->pwm_downtime
== 0 ? 1 : 0;
753 w83793_write_value(client
, W83793_REG_PWM_DOWNTIME
,
755 } else { /* SETUP_TEMP_CRITICAL */
756 data
->temp_critical
=
757 w83793_read_value(client
, W83793_REG_TEMP_CRITICAL
) & 0x80;
758 data
->temp_critical
|= TEMP_TO_REG(val
, 0, 0x7f);
759 w83793_write_value(client
, W83793_REG_TEMP_CRITICAL
,
760 data
->temp_critical
);
763 mutex_unlock(&data
->update_lock
);
768 * Temp SmartFan control
770 * Temp channel control which pwm fan, bitfield, bit 0 indicate pwm1...
771 * It's possible two or more temp channels control the same fan, w83793
772 * always prefers to pick the most critical request and applies it to
774 * It's possible one fan is not in any mapping of 6 temp channels, this
775 * means the fan is manual mode
778 * Each temp channel has its own SmartFan mode, and temp channel
779 * control fans that are set by TEMP_FAN_MAP
781 * 1: Thermal Cruise Mode
784 * Target temperature in thermal cruise mode, w83793 will try to turn
785 * fan speed to keep the temperature of target device around this
789 * If Temp higher or lower than target with this tolerance, w83793
790 * will take actions to speed up or slow down the fan to keep the
791 * temperature within the tolerance range.
794 #define TEMP_FAN_MAP 0
795 #define TEMP_PWM_ENABLE 1
796 #define TEMP_CRUISE 2
797 #define TEMP_TOLERANCE 3
799 show_sf_ctrl(struct device
*dev
, struct device_attribute
*attr
, char *buf
)
801 struct sensor_device_attribute_2
*sensor_attr
=
802 to_sensor_dev_attr_2(attr
);
803 int nr
= sensor_attr
->nr
;
804 int index
= sensor_attr
->index
;
805 struct w83793_data
*data
= w83793_update_device(dev
);
808 if (nr
== TEMP_FAN_MAP
) {
809 val
= data
->temp_fan_map
[index
];
810 } else if (nr
== TEMP_PWM_ENABLE
) {
811 /* +2 to transfrom into 2 and 3 to conform with sysfs intf */
812 val
= ((data
->pwm_enable
>> index
) & 0x01) + 2;
813 } else if (nr
== TEMP_CRUISE
) {
814 val
= TEMP_FROM_REG(data
->temp_cruise
[index
] & 0x7f);
815 } else { /* TEMP_TOLERANCE */
816 val
= data
->tolerance
[index
>> 1] >> ((index
& 0x01) ? 4 : 0);
817 val
= TEMP_FROM_REG(val
& 0x0f);
819 return sprintf(buf
, "%d\n", val
);
823 store_sf_ctrl(struct device
*dev
, struct device_attribute
*attr
,
824 const char *buf
, size_t count
)
826 struct sensor_device_attribute_2
*sensor_attr
=
827 to_sensor_dev_attr_2(attr
);
828 int nr
= sensor_attr
->nr
;
829 int index
= sensor_attr
->index
;
830 struct i2c_client
*client
= to_i2c_client(dev
);
831 struct w83793_data
*data
= i2c_get_clientdata(client
);
835 err
= kstrtol(buf
, 10, &val
);
839 mutex_lock(&data
->update_lock
);
840 if (nr
== TEMP_FAN_MAP
) {
841 val
= SENSORS_LIMIT(val
, 0, 255);
842 w83793_write_value(client
, W83793_REG_TEMP_FAN_MAP(index
), val
);
843 data
->temp_fan_map
[index
] = val
;
844 } else if (nr
== TEMP_PWM_ENABLE
) {
845 if (val
== 2 || val
== 3) {
847 w83793_read_value(client
, W83793_REG_PWM_ENABLE
);
849 data
->pwm_enable
|= 1 << index
;
851 data
->pwm_enable
&= ~(1 << index
);
852 w83793_write_value(client
, W83793_REG_PWM_ENABLE
,
855 mutex_unlock(&data
->update_lock
);
858 } else if (nr
== TEMP_CRUISE
) {
859 data
->temp_cruise
[index
] =
860 w83793_read_value(client
, W83793_REG_TEMP_CRUISE(index
));
861 data
->temp_cruise
[index
] &= 0x80;
862 data
->temp_cruise
[index
] |= TEMP_TO_REG(val
, 0, 0x7f);
864 w83793_write_value(client
, W83793_REG_TEMP_CRUISE(index
),
865 data
->temp_cruise
[index
]);
866 } else { /* TEMP_TOLERANCE */
868 u8 shift
= (index
& 0x01) ? 4 : 0;
870 w83793_read_value(client
, W83793_REG_TEMP_TOL(i
));
872 data
->tolerance
[i
] &= ~(0x0f << shift
);
873 data
->tolerance
[i
] |= TEMP_TO_REG(val
, 0, 0x0f) << shift
;
874 w83793_write_value(client
, W83793_REG_TEMP_TOL(i
),
878 mutex_unlock(&data
->update_lock
);
883 show_sf2_pwm(struct device
*dev
, struct device_attribute
*attr
, char *buf
)
885 struct sensor_device_attribute_2
*sensor_attr
=
886 to_sensor_dev_attr_2(attr
);
887 int nr
= sensor_attr
->nr
;
888 int index
= sensor_attr
->index
;
889 struct w83793_data
*data
= w83793_update_device(dev
);
891 return sprintf(buf
, "%d\n", (data
->sf2_pwm
[index
][nr
] & 0x3f) << 2);
895 store_sf2_pwm(struct device
*dev
, struct device_attribute
*attr
,
896 const char *buf
, size_t count
)
898 struct i2c_client
*client
= to_i2c_client(dev
);
899 struct w83793_data
*data
= i2c_get_clientdata(client
);
900 struct sensor_device_attribute_2
*sensor_attr
=
901 to_sensor_dev_attr_2(attr
);
902 int nr
= sensor_attr
->nr
;
903 int index
= sensor_attr
->index
;
907 err
= kstrtoul(buf
, 10, &val
);
910 val
= SENSORS_LIMIT(val
, 0, 0xff) >> 2;
912 mutex_lock(&data
->update_lock
);
913 data
->sf2_pwm
[index
][nr
] =
914 w83793_read_value(client
, W83793_REG_SF2_PWM(index
, nr
)) & 0xc0;
915 data
->sf2_pwm
[index
][nr
] |= val
;
916 w83793_write_value(client
, W83793_REG_SF2_PWM(index
, nr
),
917 data
->sf2_pwm
[index
][nr
]);
918 mutex_unlock(&data
->update_lock
);
923 show_sf2_temp(struct device
*dev
, struct device_attribute
*attr
, char *buf
)
925 struct sensor_device_attribute_2
*sensor_attr
=
926 to_sensor_dev_attr_2(attr
);
927 int nr
= sensor_attr
->nr
;
928 int index
= sensor_attr
->index
;
929 struct w83793_data
*data
= w83793_update_device(dev
);
931 return sprintf(buf
, "%ld\n",
932 TEMP_FROM_REG(data
->sf2_temp
[index
][nr
] & 0x7f));
936 store_sf2_temp(struct device
*dev
, struct device_attribute
*attr
,
937 const char *buf
, size_t count
)
939 struct i2c_client
*client
= to_i2c_client(dev
);
940 struct w83793_data
*data
= i2c_get_clientdata(client
);
941 struct sensor_device_attribute_2
*sensor_attr
=
942 to_sensor_dev_attr_2(attr
);
943 int nr
= sensor_attr
->nr
;
944 int index
= sensor_attr
->index
;
948 err
= kstrtol(buf
, 10, &val
);
951 val
= TEMP_TO_REG(val
, 0, 0x7f);
953 mutex_lock(&data
->update_lock
);
954 data
->sf2_temp
[index
][nr
] =
955 w83793_read_value(client
, W83793_REG_SF2_TEMP(index
, nr
)) & 0x80;
956 data
->sf2_temp
[index
][nr
] |= val
;
957 w83793_write_value(client
, W83793_REG_SF2_TEMP(index
, nr
),
958 data
->sf2_temp
[index
][nr
]);
959 mutex_unlock(&data
->update_lock
);
963 /* only Vcore A/B and Vtt have additional 2 bits precision */
965 show_in(struct device
*dev
, struct device_attribute
*attr
, char *buf
)
967 struct sensor_device_attribute_2
*sensor_attr
=
968 to_sensor_dev_attr_2(attr
);
969 int nr
= sensor_attr
->nr
;
970 int index
= sensor_attr
->index
;
971 struct w83793_data
*data
= w83793_update_device(dev
);
972 u16 val
= data
->in
[index
][nr
];
976 val
+= (data
->in_low_bits
[nr
] >> (index
* 2)) & 0x3;
978 /* voltage inputs 5VDD and 5VSB needs 150mV offset */
979 val
= val
* scale_in
[index
] + scale_in_add
[index
];
980 return sprintf(buf
, "%d\n", val
);
984 store_in(struct device
*dev
, struct device_attribute
*attr
,
985 const char *buf
, size_t count
)
987 struct sensor_device_attribute_2
*sensor_attr
=
988 to_sensor_dev_attr_2(attr
);
989 int nr
= sensor_attr
->nr
;
990 int index
= sensor_attr
->index
;
991 struct i2c_client
*client
= to_i2c_client(dev
);
992 struct w83793_data
*data
= i2c_get_clientdata(client
);
996 err
= kstrtoul(buf
, 10, &val
);
999 val
= (val
+ scale_in
[index
] / 2) / scale_in
[index
];
1001 mutex_lock(&data
->update_lock
);
1003 /* fix the limit values of 5VDD and 5VSB to ALARM mechanism */
1004 if (nr
== 1 || nr
== 2)
1005 val
-= scale_in_add
[index
] / scale_in
[index
];
1006 val
= SENSORS_LIMIT(val
, 0, 255);
1008 val
= SENSORS_LIMIT(val
, 0, 0x3FF);
1009 data
->in_low_bits
[nr
] =
1010 w83793_read_value(client
, W83793_REG_IN_LOW_BITS
[nr
]);
1011 data
->in_low_bits
[nr
] &= ~(0x03 << (2 * index
));
1012 data
->in_low_bits
[nr
] |= (val
& 0x03) << (2 * index
);
1013 w83793_write_value(client
, W83793_REG_IN_LOW_BITS
[nr
],
1014 data
->in_low_bits
[nr
]);
1017 data
->in
[index
][nr
] = val
;
1018 w83793_write_value(client
, W83793_REG_IN
[index
][nr
],
1019 data
->in
[index
][nr
]);
1020 mutex_unlock(&data
->update_lock
);
1026 #define SENSOR_ATTR_IN(index) \
1027 SENSOR_ATTR_2(in##index##_input, S_IRUGO, show_in, NULL, \
1029 SENSOR_ATTR_2(in##index##_max, S_IRUGO | S_IWUSR, show_in, \
1030 store_in, IN_MAX, index), \
1031 SENSOR_ATTR_2(in##index##_min, S_IRUGO | S_IWUSR, show_in, \
1032 store_in, IN_LOW, index), \
1033 SENSOR_ATTR_2(in##index##_alarm, S_IRUGO, show_alarm_beep, \
1034 NULL, ALARM_STATUS, index + ((index > 2) ? 1 : 0)), \
1035 SENSOR_ATTR_2(in##index##_beep, S_IWUSR | S_IRUGO, \
1036 show_alarm_beep, store_beep, BEEP_ENABLE, \
1037 index + ((index > 2) ? 1 : 0))
1039 #define SENSOR_ATTR_FAN(index) \
1040 SENSOR_ATTR_2(fan##index##_alarm, S_IRUGO, show_alarm_beep, \
1041 NULL, ALARM_STATUS, index + 17), \
1042 SENSOR_ATTR_2(fan##index##_beep, S_IWUSR | S_IRUGO, \
1043 show_alarm_beep, store_beep, BEEP_ENABLE, index + 17), \
1044 SENSOR_ATTR_2(fan##index##_input, S_IRUGO, show_fan, \
1045 NULL, FAN_INPUT, index - 1), \
1046 SENSOR_ATTR_2(fan##index##_min, S_IWUSR | S_IRUGO, \
1047 show_fan, store_fan_min, FAN_MIN, index - 1)
1049 #define SENSOR_ATTR_PWM(index) \
1050 SENSOR_ATTR_2(pwm##index, S_IWUSR | S_IRUGO, show_pwm, \
1051 store_pwm, PWM_DUTY, index - 1), \
1052 SENSOR_ATTR_2(pwm##index##_nonstop, S_IWUSR | S_IRUGO, \
1053 show_pwm, store_pwm, PWM_NONSTOP, index - 1), \
1054 SENSOR_ATTR_2(pwm##index##_start, S_IWUSR | S_IRUGO, \
1055 show_pwm, store_pwm, PWM_START, index - 1), \
1056 SENSOR_ATTR_2(pwm##index##_stop_time, S_IWUSR | S_IRUGO, \
1057 show_pwm, store_pwm, PWM_STOP_TIME, index - 1)
1059 #define SENSOR_ATTR_TEMP(index) \
1060 SENSOR_ATTR_2(temp##index##_type, S_IRUGO | S_IWUSR, \
1061 show_temp_mode, store_temp_mode, NOT_USED, index - 1), \
1062 SENSOR_ATTR_2(temp##index##_input, S_IRUGO, show_temp, \
1063 NULL, TEMP_READ, index - 1), \
1064 SENSOR_ATTR_2(temp##index##_max, S_IRUGO | S_IWUSR, show_temp, \
1065 store_temp, TEMP_CRIT, index - 1), \
1066 SENSOR_ATTR_2(temp##index##_max_hyst, S_IRUGO | S_IWUSR, \
1067 show_temp, store_temp, TEMP_CRIT_HYST, index - 1), \
1068 SENSOR_ATTR_2(temp##index##_warn, S_IRUGO | S_IWUSR, show_temp, \
1069 store_temp, TEMP_WARN, index - 1), \
1070 SENSOR_ATTR_2(temp##index##_warn_hyst, S_IRUGO | S_IWUSR, \
1071 show_temp, store_temp, TEMP_WARN_HYST, index - 1), \
1072 SENSOR_ATTR_2(temp##index##_alarm, S_IRUGO, \
1073 show_alarm_beep, NULL, ALARM_STATUS, index + 11), \
1074 SENSOR_ATTR_2(temp##index##_beep, S_IWUSR | S_IRUGO, \
1075 show_alarm_beep, store_beep, BEEP_ENABLE, index + 11), \
1076 SENSOR_ATTR_2(temp##index##_auto_channels_pwm, \
1077 S_IRUGO | S_IWUSR, show_sf_ctrl, store_sf_ctrl, \
1078 TEMP_FAN_MAP, index - 1), \
1079 SENSOR_ATTR_2(temp##index##_pwm_enable, S_IWUSR | S_IRUGO, \
1080 show_sf_ctrl, store_sf_ctrl, TEMP_PWM_ENABLE, \
1082 SENSOR_ATTR_2(thermal_cruise##index, S_IRUGO | S_IWUSR, \
1083 show_sf_ctrl, store_sf_ctrl, TEMP_CRUISE, index - 1), \
1084 SENSOR_ATTR_2(tolerance##index, S_IRUGO | S_IWUSR, show_sf_ctrl,\
1085 store_sf_ctrl, TEMP_TOLERANCE, index - 1), \
1086 SENSOR_ATTR_2(temp##index##_auto_point1_pwm, S_IRUGO | S_IWUSR, \
1087 show_sf2_pwm, store_sf2_pwm, 0, index - 1), \
1088 SENSOR_ATTR_2(temp##index##_auto_point2_pwm, S_IRUGO | S_IWUSR, \
1089 show_sf2_pwm, store_sf2_pwm, 1, index - 1), \
1090 SENSOR_ATTR_2(temp##index##_auto_point3_pwm, S_IRUGO | S_IWUSR, \
1091 show_sf2_pwm, store_sf2_pwm, 2, index - 1), \
1092 SENSOR_ATTR_2(temp##index##_auto_point4_pwm, S_IRUGO | S_IWUSR, \
1093 show_sf2_pwm, store_sf2_pwm, 3, index - 1), \
1094 SENSOR_ATTR_2(temp##index##_auto_point5_pwm, S_IRUGO | S_IWUSR, \
1095 show_sf2_pwm, store_sf2_pwm, 4, index - 1), \
1096 SENSOR_ATTR_2(temp##index##_auto_point6_pwm, S_IRUGO | S_IWUSR, \
1097 show_sf2_pwm, store_sf2_pwm, 5, index - 1), \
1098 SENSOR_ATTR_2(temp##index##_auto_point7_pwm, S_IRUGO | S_IWUSR, \
1099 show_sf2_pwm, store_sf2_pwm, 6, index - 1), \
1100 SENSOR_ATTR_2(temp##index##_auto_point1_temp, S_IRUGO | S_IWUSR,\
1101 show_sf2_temp, store_sf2_temp, 0, index - 1), \
1102 SENSOR_ATTR_2(temp##index##_auto_point2_temp, S_IRUGO | S_IWUSR,\
1103 show_sf2_temp, store_sf2_temp, 1, index - 1), \
1104 SENSOR_ATTR_2(temp##index##_auto_point3_temp, S_IRUGO | S_IWUSR,\
1105 show_sf2_temp, store_sf2_temp, 2, index - 1), \
1106 SENSOR_ATTR_2(temp##index##_auto_point4_temp, S_IRUGO | S_IWUSR,\
1107 show_sf2_temp, store_sf2_temp, 3, index - 1), \
1108 SENSOR_ATTR_2(temp##index##_auto_point5_temp, S_IRUGO | S_IWUSR,\
1109 show_sf2_temp, store_sf2_temp, 4, index - 1), \
1110 SENSOR_ATTR_2(temp##index##_auto_point6_temp, S_IRUGO | S_IWUSR,\
1111 show_sf2_temp, store_sf2_temp, 5, index - 1), \
1112 SENSOR_ATTR_2(temp##index##_auto_point7_temp, S_IRUGO | S_IWUSR,\
1113 show_sf2_temp, store_sf2_temp, 6, index - 1)
1115 static struct sensor_device_attribute_2 w83793_sensor_attr_2
[] = {
1136 static struct sensor_device_attribute_2 w83793_temp
[] = {
1137 SENSOR_ATTR_TEMP(1),
1138 SENSOR_ATTR_TEMP(2),
1139 SENSOR_ATTR_TEMP(3),
1140 SENSOR_ATTR_TEMP(4),
1141 SENSOR_ATTR_TEMP(5),
1142 SENSOR_ATTR_TEMP(6),
1146 static struct sensor_device_attribute_2 w83793_left_fan
[] = {
1151 SENSOR_ATTR_FAN(10),
1152 SENSOR_ATTR_FAN(11),
1153 SENSOR_ATTR_FAN(12),
1157 static struct sensor_device_attribute_2 w83793_left_pwm
[] = {
1165 static struct sensor_device_attribute_2 w83793_vid
[] = {
1166 SENSOR_ATTR_2(cpu0_vid
, S_IRUGO
, show_vid
, NULL
, NOT_USED
, 0),
1167 SENSOR_ATTR_2(cpu1_vid
, S_IRUGO
, show_vid
, NULL
, NOT_USED
, 1),
1169 static DEVICE_ATTR(vrm
, S_IWUSR
| S_IRUGO
, show_vrm
, store_vrm
);
1171 static struct sensor_device_attribute_2 sda_single_files
[] = {
1172 SENSOR_ATTR_2(intrusion0_alarm
, S_IWUSR
| S_IRUGO
, show_alarm_beep
,
1173 store_chassis_clear
, ALARM_STATUS
, 30),
1174 SENSOR_ATTR_2(beep_enable
, S_IWUSR
| S_IRUGO
, show_beep_enable
,
1175 store_beep_enable
, NOT_USED
, NOT_USED
),
1176 SENSOR_ATTR_2(pwm_default
, S_IWUSR
| S_IRUGO
, show_sf_setup
,
1177 store_sf_setup
, SETUP_PWM_DEFAULT
, NOT_USED
),
1178 SENSOR_ATTR_2(pwm_uptime
, S_IWUSR
| S_IRUGO
, show_sf_setup
,
1179 store_sf_setup
, SETUP_PWM_UPTIME
, NOT_USED
),
1180 SENSOR_ATTR_2(pwm_downtime
, S_IWUSR
| S_IRUGO
, show_sf_setup
,
1181 store_sf_setup
, SETUP_PWM_DOWNTIME
, NOT_USED
),
1182 SENSOR_ATTR_2(temp_critical
, S_IWUSR
| S_IRUGO
, show_sf_setup
,
1183 store_sf_setup
, SETUP_TEMP_CRITICAL
, NOT_USED
),
1186 static void w83793_init_client(struct i2c_client
*client
)
1189 w83793_write_value(client
, W83793_REG_CONFIG
, 0x80);
1191 /* Start monitoring */
1192 w83793_write_value(client
, W83793_REG_CONFIG
,
1193 w83793_read_value(client
, W83793_REG_CONFIG
) | 0x01);
1200 static int watchdog_set_timeout(struct w83793_data
*data
, int timeout
)
1204 mtimeout
= DIV_ROUND_UP(timeout
, 60);
1209 mutex_lock(&data
->watchdog_lock
);
1210 if (!data
->client
) {
1215 data
->watchdog_timeout
= mtimeout
;
1217 /* Set Timeout value (in Minutes) */
1218 w83793_write_value(data
->client
, W83793_REG_WDT_TIMEOUT
,
1219 data
->watchdog_timeout
);
1221 ret
= mtimeout
* 60;
1224 mutex_unlock(&data
->watchdog_lock
);
1228 static int watchdog_get_timeout(struct w83793_data
*data
)
1232 mutex_lock(&data
->watchdog_lock
);
1233 timeout
= data
->watchdog_timeout
* 60;
1234 mutex_unlock(&data
->watchdog_lock
);
1239 static int watchdog_trigger(struct w83793_data
*data
)
1243 mutex_lock(&data
->watchdog_lock
);
1244 if (!data
->client
) {
1249 /* Set Timeout value (in Minutes) */
1250 w83793_write_value(data
->client
, W83793_REG_WDT_TIMEOUT
,
1251 data
->watchdog_timeout
);
1254 mutex_unlock(&data
->watchdog_lock
);
1258 static int watchdog_enable(struct w83793_data
*data
)
1262 mutex_lock(&data
->watchdog_lock
);
1263 if (!data
->client
) {
1268 /* Set initial timeout */
1269 w83793_write_value(data
->client
, W83793_REG_WDT_TIMEOUT
,
1270 data
->watchdog_timeout
);
1272 /* Enable Soft Watchdog */
1273 w83793_write_value(data
->client
, W83793_REG_WDT_LOCK
, 0x55);
1276 mutex_unlock(&data
->watchdog_lock
);
1280 static int watchdog_disable(struct w83793_data
*data
)
1284 mutex_lock(&data
->watchdog_lock
);
1285 if (!data
->client
) {
1290 /* Disable Soft Watchdog */
1291 w83793_write_value(data
->client
, W83793_REG_WDT_LOCK
, 0xAA);
1294 mutex_unlock(&data
->watchdog_lock
);
1298 static int watchdog_open(struct inode
*inode
, struct file
*filp
)
1300 struct w83793_data
*pos
, *data
= NULL
;
1301 int watchdog_is_open
;
1304 * We get called from drivers/char/misc.c with misc_mtx hold, and we
1305 * call misc_register() from w83793_probe() with watchdog_data_mutex
1306 * hold, as misc_register() takes the misc_mtx lock, this is a possible
1307 * deadlock, so we use mutex_trylock here.
1309 if (!mutex_trylock(&watchdog_data_mutex
))
1310 return -ERESTARTSYS
;
1311 list_for_each_entry(pos
, &watchdog_data_list
, list
) {
1312 if (pos
->watchdog_miscdev
.minor
== iminor(inode
)) {
1318 /* Check, if device is already open */
1319 watchdog_is_open
= test_and_set_bit(0, &data
->watchdog_is_open
);
1322 * Increase data reference counter (if not already done).
1323 * Note we can never not have found data, so we don't check for this
1325 if (!watchdog_is_open
)
1326 kref_get(&data
->kref
);
1328 mutex_unlock(&watchdog_data_mutex
);
1330 /* Check, if device is already open and possibly issue error */
1331 if (watchdog_is_open
)
1334 /* Enable Soft Watchdog */
1335 watchdog_enable(data
);
1337 /* Store pointer to data into filp's private data */
1338 filp
->private_data
= data
;
1340 return nonseekable_open(inode
, filp
);
1343 static int watchdog_close(struct inode
*inode
, struct file
*filp
)
1345 struct w83793_data
*data
= filp
->private_data
;
1347 if (data
->watchdog_expect_close
) {
1348 watchdog_disable(data
);
1349 data
->watchdog_expect_close
= 0;
1351 watchdog_trigger(data
);
1352 dev_crit(&data
->client
->dev
,
1353 "unexpected close, not stopping watchdog!\n");
1356 clear_bit(0, &data
->watchdog_is_open
);
1358 /* Decrease data reference counter */
1359 mutex_lock(&watchdog_data_mutex
);
1360 kref_put(&data
->kref
, w83793_release_resources
);
1361 mutex_unlock(&watchdog_data_mutex
);
1366 static ssize_t
watchdog_write(struct file
*filp
, const char __user
*buf
,
1367 size_t count
, loff_t
*offset
)
1370 struct w83793_data
*data
= filp
->private_data
;
1376 /* Clear it in case it was set with a previous write */
1377 data
->watchdog_expect_close
= 0;
1379 for (i
= 0; i
!= count
; i
++) {
1381 if (get_user(c
, buf
+ i
))
1384 data
->watchdog_expect_close
= 1;
1387 ret
= watchdog_trigger(data
);
1394 static long watchdog_ioctl(struct file
*filp
, unsigned int cmd
,
1397 struct watchdog_info ident
= {
1398 .options
= WDIOF_KEEPALIVEPING
|
1401 .identity
= "w83793 watchdog"
1405 struct w83793_data
*data
= filp
->private_data
;
1408 case WDIOC_GETSUPPORT
:
1410 ident
.options
|= WDIOF_MAGICCLOSE
;
1411 if (copy_to_user((void __user
*)arg
, &ident
, sizeof(ident
)))
1415 case WDIOC_GETSTATUS
:
1416 val
= data
->watchdog_caused_reboot
? WDIOF_CARDRESET
: 0;
1417 ret
= put_user(val
, (int __user
*)arg
);
1420 case WDIOC_GETBOOTSTATUS
:
1421 ret
= put_user(0, (int __user
*)arg
);
1424 case WDIOC_KEEPALIVE
:
1425 ret
= watchdog_trigger(data
);
1428 case WDIOC_GETTIMEOUT
:
1429 val
= watchdog_get_timeout(data
);
1430 ret
= put_user(val
, (int __user
*)arg
);
1433 case WDIOC_SETTIMEOUT
:
1434 if (get_user(val
, (int __user
*)arg
)) {
1438 ret
= watchdog_set_timeout(data
, val
);
1440 ret
= put_user(ret
, (int __user
*)arg
);
1443 case WDIOC_SETOPTIONS
:
1444 if (get_user(val
, (int __user
*)arg
)) {
1449 if (val
& WDIOS_DISABLECARD
)
1450 ret
= watchdog_disable(data
);
1451 else if (val
& WDIOS_ENABLECARD
)
1452 ret
= watchdog_enable(data
);
1463 static const struct file_operations watchdog_fops
= {
1464 .owner
= THIS_MODULE
,
1465 .llseek
= no_llseek
,
1466 .open
= watchdog_open
,
1467 .release
= watchdog_close
,
1468 .write
= watchdog_write
,
1469 .unlocked_ioctl
= watchdog_ioctl
,
1473 * Notifier for system down
1476 static int watchdog_notify_sys(struct notifier_block
*this, unsigned long code
,
1479 struct w83793_data
*data
= NULL
;
1481 if (code
== SYS_DOWN
|| code
== SYS_HALT
) {
1483 /* Disable each registered watchdog */
1484 mutex_lock(&watchdog_data_mutex
);
1485 list_for_each_entry(data
, &watchdog_data_list
, list
) {
1486 if (data
->watchdog_miscdev
.minor
)
1487 watchdog_disable(data
);
1489 mutex_unlock(&watchdog_data_mutex
);
1496 * The WDT needs to learn about soft shutdowns in order to
1497 * turn the timebomb registers off.
1500 static struct notifier_block watchdog_notifier
= {
1501 .notifier_call
= watchdog_notify_sys
,
1505 * Init / remove routines
1508 static int w83793_remove(struct i2c_client
*client
)
1510 struct w83793_data
*data
= i2c_get_clientdata(client
);
1511 struct device
*dev
= &client
->dev
;
1514 /* Unregister the watchdog (if registered) */
1515 if (data
->watchdog_miscdev
.minor
) {
1516 misc_deregister(&data
->watchdog_miscdev
);
1518 if (data
->watchdog_is_open
) {
1519 dev_warn(&client
->dev
,
1520 "i2c client detached with watchdog open! "
1521 "Stopping watchdog.\n");
1522 watchdog_disable(data
);
1525 mutex_lock(&watchdog_data_mutex
);
1526 list_del(&data
->list
);
1527 mutex_unlock(&watchdog_data_mutex
);
1529 /* Tell the watchdog code the client is gone */
1530 mutex_lock(&data
->watchdog_lock
);
1531 data
->client
= NULL
;
1532 mutex_unlock(&data
->watchdog_lock
);
1535 /* Reset Configuration Register to Disable Watch Dog Registers */
1536 tmp
= w83793_read_value(client
, W83793_REG_CONFIG
);
1537 w83793_write_value(client
, W83793_REG_CONFIG
, tmp
& ~0x04);
1539 unregister_reboot_notifier(&watchdog_notifier
);
1541 hwmon_device_unregister(data
->hwmon_dev
);
1543 for (i
= 0; i
< ARRAY_SIZE(w83793_sensor_attr_2
); i
++)
1544 device_remove_file(dev
,
1545 &w83793_sensor_attr_2
[i
].dev_attr
);
1547 for (i
= 0; i
< ARRAY_SIZE(sda_single_files
); i
++)
1548 device_remove_file(dev
, &sda_single_files
[i
].dev_attr
);
1550 for (i
= 0; i
< ARRAY_SIZE(w83793_vid
); i
++)
1551 device_remove_file(dev
, &w83793_vid
[i
].dev_attr
);
1552 device_remove_file(dev
, &dev_attr_vrm
);
1554 for (i
= 0; i
< ARRAY_SIZE(w83793_left_fan
); i
++)
1555 device_remove_file(dev
, &w83793_left_fan
[i
].dev_attr
);
1557 for (i
= 0; i
< ARRAY_SIZE(w83793_left_pwm
); i
++)
1558 device_remove_file(dev
, &w83793_left_pwm
[i
].dev_attr
);
1560 for (i
= 0; i
< ARRAY_SIZE(w83793_temp
); i
++)
1561 device_remove_file(dev
, &w83793_temp
[i
].dev_attr
);
1563 if (data
->lm75
[0] != NULL
)
1564 i2c_unregister_device(data
->lm75
[0]);
1565 if (data
->lm75
[1] != NULL
)
1566 i2c_unregister_device(data
->lm75
[1]);
1568 /* Decrease data reference counter */
1569 mutex_lock(&watchdog_data_mutex
);
1570 kref_put(&data
->kref
, w83793_release_resources
);
1571 mutex_unlock(&watchdog_data_mutex
);
1577 w83793_detect_subclients(struct i2c_client
*client
)
1580 int address
= client
->addr
;
1582 struct i2c_adapter
*adapter
= client
->adapter
;
1583 struct w83793_data
*data
= i2c_get_clientdata(client
);
1585 id
= i2c_adapter_id(adapter
);
1586 if (force_subclients
[0] == id
&& force_subclients
[1] == address
) {
1587 for (i
= 2; i
<= 3; i
++) {
1588 if (force_subclients
[i
] < 0x48
1589 || force_subclients
[i
] > 0x4f) {
1590 dev_err(&client
->dev
,
1591 "invalid subclient "
1592 "address %d; must be 0x48-0x4f\n",
1593 force_subclients
[i
]);
1598 w83793_write_value(client
, W83793_REG_I2C_SUBADDR
,
1599 (force_subclients
[2] & 0x07) |
1600 ((force_subclients
[3] & 0x07) << 4));
1603 tmp
= w83793_read_value(client
, W83793_REG_I2C_SUBADDR
);
1605 data
->lm75
[0] = i2c_new_dummy(adapter
, 0x48 + (tmp
& 0x7));
1606 if (!(tmp
& 0x80)) {
1607 if ((data
->lm75
[0] != NULL
)
1608 && ((tmp
& 0x7) == ((tmp
>> 4) & 0x7))) {
1609 dev_err(&client
->dev
,
1610 "duplicate addresses 0x%x, "
1611 "use force_subclients\n", data
->lm75
[0]->addr
);
1615 data
->lm75
[1] = i2c_new_dummy(adapter
,
1616 0x48 + ((tmp
>> 4) & 0x7));
1621 /* Undo inits in case of errors */
1624 if (data
->lm75
[0] != NULL
)
1625 i2c_unregister_device(data
->lm75
[0]);
1630 /* Return 0 if detection is successful, -ENODEV otherwise */
1631 static int w83793_detect(struct i2c_client
*client
,
1632 struct i2c_board_info
*info
)
1634 u8 tmp
, bank
, chip_id
;
1635 struct i2c_adapter
*adapter
= client
->adapter
;
1636 unsigned short address
= client
->addr
;
1638 if (!i2c_check_functionality(adapter
, I2C_FUNC_SMBUS_BYTE_DATA
))
1641 bank
= i2c_smbus_read_byte_data(client
, W83793_REG_BANKSEL
);
1643 tmp
= bank
& 0x80 ? 0x5c : 0xa3;
1644 /* Check Winbond vendor ID */
1645 if (tmp
!= i2c_smbus_read_byte_data(client
, W83793_REG_VENDORID
)) {
1646 pr_debug("w83793: Detection failed at check vendor id\n");
1651 * If Winbond chip, address of chip and W83793_REG_I2C_ADDR
1654 if ((bank
& 0x07) == 0
1655 && i2c_smbus_read_byte_data(client
, W83793_REG_I2C_ADDR
) !=
1657 pr_debug("w83793: Detection failed at check i2c addr\n");
1661 /* Determine the chip type now */
1662 chip_id
= i2c_smbus_read_byte_data(client
, W83793_REG_CHIPID
);
1663 if (chip_id
!= 0x7b)
1666 strlcpy(info
->type
, "w83793", I2C_NAME_SIZE
);
1671 static int w83793_probe(struct i2c_client
*client
,
1672 const struct i2c_device_id
*id
)
1674 struct device
*dev
= &client
->dev
;
1675 const int watchdog_minors
[] = { WATCHDOG_MINOR
, 212, 213, 214, 215 };
1676 struct w83793_data
*data
;
1677 int i
, tmp
, val
, err
;
1678 int files_fan
= ARRAY_SIZE(w83793_left_fan
) / 7;
1679 int files_pwm
= ARRAY_SIZE(w83793_left_pwm
) / 5;
1680 int files_temp
= ARRAY_SIZE(w83793_temp
) / 6;
1682 data
= kzalloc(sizeof(struct w83793_data
), GFP_KERNEL
);
1688 i2c_set_clientdata(client
, data
);
1689 data
->bank
= i2c_smbus_read_byte_data(client
, W83793_REG_BANKSEL
);
1690 mutex_init(&data
->update_lock
);
1691 mutex_init(&data
->watchdog_lock
);
1692 INIT_LIST_HEAD(&data
->list
);
1693 kref_init(&data
->kref
);
1696 * Store client pointer in our data struct for watchdog usage
1697 * (where the client is found through a data ptr instead of the
1700 data
->client
= client
;
1702 err
= w83793_detect_subclients(client
);
1706 /* Initialize the chip */
1707 w83793_init_client(client
);
1710 * Only fan 1-5 has their own input pins,
1711 * Pwm 1-3 has their own pins
1713 data
->has_fan
= 0x1f;
1714 data
->has_pwm
= 0x07;
1715 tmp
= w83793_read_value(client
, W83793_REG_MFC
);
1716 val
= w83793_read_value(client
, W83793_REG_FANIN_CTRL
);
1718 /* check the function of pins 49-56 */
1720 data
->has_vid
|= 0x2; /* has VIDB */
1722 data
->has_pwm
|= 0x18; /* pwm 4,5 */
1723 if (val
& 0x01) { /* fan 6 */
1724 data
->has_fan
|= 0x20;
1725 data
->has_pwm
|= 0x20;
1727 if (val
& 0x02) { /* fan 7 */
1728 data
->has_fan
|= 0x40;
1729 data
->has_pwm
|= 0x40;
1731 if (!(tmp
& 0x40) && (val
& 0x04)) { /* fan 8 */
1732 data
->has_fan
|= 0x80;
1733 data
->has_pwm
|= 0x80;
1737 /* check the function of pins 37-40 */
1739 data
->has_vid
|= 0x1; /* has VIDA */
1740 if (0x08 == (tmp
& 0x0c)) {
1741 if (val
& 0x08) /* fan 9 */
1742 data
->has_fan
|= 0x100;
1743 if (val
& 0x10) /* fan 10 */
1744 data
->has_fan
|= 0x200;
1746 if (0x20 == (tmp
& 0x30)) {
1747 if (val
& 0x20) /* fan 11 */
1748 data
->has_fan
|= 0x400;
1749 if (val
& 0x40) /* fan 12 */
1750 data
->has_fan
|= 0x800;
1753 if ((tmp
& 0x01) && (val
& 0x04)) { /* fan 8, second location */
1754 data
->has_fan
|= 0x80;
1755 data
->has_pwm
|= 0x80;
1758 tmp
= w83793_read_value(client
, W83793_REG_FANIN_SEL
);
1759 if ((tmp
& 0x01) && (val
& 0x08)) { /* fan 9, second location */
1760 data
->has_fan
|= 0x100;
1762 if ((tmp
& 0x02) && (val
& 0x10)) { /* fan 10, second location */
1763 data
->has_fan
|= 0x200;
1765 if ((tmp
& 0x04) && (val
& 0x20)) { /* fan 11, second location */
1766 data
->has_fan
|= 0x400;
1768 if ((tmp
& 0x08) && (val
& 0x40)) { /* fan 12, second location */
1769 data
->has_fan
|= 0x800;
1772 /* check the temp1-6 mode, ignore former AMDSI selected inputs */
1773 tmp
= w83793_read_value(client
, W83793_REG_TEMP_MODE
[0]);
1775 data
->has_temp
|= 0x01;
1777 data
->has_temp
|= 0x02;
1779 data
->has_temp
|= 0x04;
1781 data
->has_temp
|= 0x08;
1783 tmp
= w83793_read_value(client
, W83793_REG_TEMP_MODE
[1]);
1785 data
->has_temp
|= 0x10;
1787 data
->has_temp
|= 0x20;
1789 /* Register sysfs hooks */
1790 for (i
= 0; i
< ARRAY_SIZE(w83793_sensor_attr_2
); i
++) {
1791 err
= device_create_file(dev
,
1792 &w83793_sensor_attr_2
[i
].dev_attr
);
1797 for (i
= 0; i
< ARRAY_SIZE(w83793_vid
); i
++) {
1798 if (!(data
->has_vid
& (1 << i
)))
1800 err
= device_create_file(dev
, &w83793_vid
[i
].dev_attr
);
1804 if (data
->has_vid
) {
1805 data
->vrm
= vid_which_vrm();
1806 err
= device_create_file(dev
, &dev_attr_vrm
);
1811 for (i
= 0; i
< ARRAY_SIZE(sda_single_files
); i
++) {
1812 err
= device_create_file(dev
, &sda_single_files
[i
].dev_attr
);
1818 for (i
= 0; i
< 6; i
++) {
1820 if (!(data
->has_temp
& (1 << i
)))
1822 for (j
= 0; j
< files_temp
; j
++) {
1823 err
= device_create_file(dev
,
1824 &w83793_temp
[(i
) * files_temp
1831 for (i
= 5; i
< 12; i
++) {
1833 if (!(data
->has_fan
& (1 << i
)))
1835 for (j
= 0; j
< files_fan
; j
++) {
1836 err
= device_create_file(dev
,
1837 &w83793_left_fan
[(i
- 5) * files_fan
1844 for (i
= 3; i
< 8; i
++) {
1846 if (!(data
->has_pwm
& (1 << i
)))
1848 for (j
= 0; j
< files_pwm
; j
++) {
1849 err
= device_create_file(dev
,
1850 &w83793_left_pwm
[(i
- 3) * files_pwm
1857 data
->hwmon_dev
= hwmon_device_register(dev
);
1858 if (IS_ERR(data
->hwmon_dev
)) {
1859 err
= PTR_ERR(data
->hwmon_dev
);
1863 /* Watchdog initialization */
1865 /* Register boot notifier */
1866 err
= register_reboot_notifier(&watchdog_notifier
);
1868 dev_err(&client
->dev
,
1869 "cannot register reboot notifier (err=%d)\n", err
);
1874 * Enable Watchdog registers.
1875 * Set Configuration Register to Enable Watch Dog Registers
1876 * (Bit 2) = XXXX, X1XX.
1878 tmp
= w83793_read_value(client
, W83793_REG_CONFIG
);
1879 w83793_write_value(client
, W83793_REG_CONFIG
, tmp
| 0x04);
1881 /* Set the default watchdog timeout */
1882 data
->watchdog_timeout
= timeout
;
1884 /* Check, if last reboot was caused by watchdog */
1885 data
->watchdog_caused_reboot
=
1886 w83793_read_value(data
->client
, W83793_REG_WDT_STATUS
) & 0x01;
1888 /* Disable Soft Watchdog during initialiation */
1889 watchdog_disable(data
);
1892 * We take the data_mutex lock early so that watchdog_open() cannot
1893 * run when misc_register() has completed, but we've not yet added
1894 * our data to the watchdog_data_list (and set the default timeout)
1896 mutex_lock(&watchdog_data_mutex
);
1897 for (i
= 0; i
< ARRAY_SIZE(watchdog_minors
); i
++) {
1898 /* Register our watchdog part */
1899 snprintf(data
->watchdog_name
, sizeof(data
->watchdog_name
),
1900 "watchdog%c", (i
== 0) ? '\0' : ('0' + i
));
1901 data
->watchdog_miscdev
.name
= data
->watchdog_name
;
1902 data
->watchdog_miscdev
.fops
= &watchdog_fops
;
1903 data
->watchdog_miscdev
.minor
= watchdog_minors
[i
];
1905 err
= misc_register(&data
->watchdog_miscdev
);
1909 data
->watchdog_miscdev
.minor
= 0;
1910 dev_err(&client
->dev
,
1911 "Registering watchdog chardev: %d\n", err
);
1915 list_add(&data
->list
, &watchdog_data_list
);
1917 dev_info(&client
->dev
,
1918 "Registered watchdog chardev major 10, minor: %d\n",
1919 watchdog_minors
[i
]);
1922 if (i
== ARRAY_SIZE(watchdog_minors
)) {
1923 data
->watchdog_miscdev
.minor
= 0;
1924 dev_warn(&client
->dev
, "Couldn't register watchdog chardev "
1925 "(due to no free minor)\n");
1928 mutex_unlock(&watchdog_data_mutex
);
1932 /* Unregister hwmon device */
1936 hwmon_device_unregister(data
->hwmon_dev
);
1938 /* Unregister sysfs hooks */
1941 for (i
= 0; i
< ARRAY_SIZE(w83793_sensor_attr_2
); i
++)
1942 device_remove_file(dev
, &w83793_sensor_attr_2
[i
].dev_attr
);
1944 for (i
= 0; i
< ARRAY_SIZE(sda_single_files
); i
++)
1945 device_remove_file(dev
, &sda_single_files
[i
].dev_attr
);
1947 for (i
= 0; i
< ARRAY_SIZE(w83793_vid
); i
++)
1948 device_remove_file(dev
, &w83793_vid
[i
].dev_attr
);
1950 for (i
= 0; i
< ARRAY_SIZE(w83793_left_fan
); i
++)
1951 device_remove_file(dev
, &w83793_left_fan
[i
].dev_attr
);
1953 for (i
= 0; i
< ARRAY_SIZE(w83793_left_pwm
); i
++)
1954 device_remove_file(dev
, &w83793_left_pwm
[i
].dev_attr
);
1956 for (i
= 0; i
< ARRAY_SIZE(w83793_temp
); i
++)
1957 device_remove_file(dev
, &w83793_temp
[i
].dev_attr
);
1959 if (data
->lm75
[0] != NULL
)
1960 i2c_unregister_device(data
->lm75
[0]);
1961 if (data
->lm75
[1] != NULL
)
1962 i2c_unregister_device(data
->lm75
[1]);
1969 static void w83793_update_nonvolatile(struct device
*dev
)
1971 struct i2c_client
*client
= to_i2c_client(dev
);
1972 struct w83793_data
*data
= i2c_get_clientdata(client
);
1975 * They are somewhat "stable" registers, and to update them every time
1976 * takes so much time, it's just not worthy. Update them in a long
1977 * interval to avoid exception.
1979 if (!(time_after(jiffies
, data
->last_nonvolatile
+ HZ
* 300)
1982 /* update voltage limits */
1983 for (i
= 1; i
< 3; i
++) {
1984 for (j
= 0; j
< ARRAY_SIZE(data
->in
); j
++) {
1986 w83793_read_value(client
, W83793_REG_IN
[j
][i
]);
1988 data
->in_low_bits
[i
] =
1989 w83793_read_value(client
, W83793_REG_IN_LOW_BITS
[i
]);
1992 for (i
= 0; i
< ARRAY_SIZE(data
->fan_min
); i
++) {
1993 /* Update the Fan measured value and limits */
1994 if (!(data
->has_fan
& (1 << i
)))
1997 w83793_read_value(client
, W83793_REG_FAN_MIN(i
)) << 8;
1999 w83793_read_value(client
, W83793_REG_FAN_MIN(i
) + 1);
2002 for (i
= 0; i
< ARRAY_SIZE(data
->temp_fan_map
); i
++) {
2003 if (!(data
->has_temp
& (1 << i
)))
2005 data
->temp_fan_map
[i
] =
2006 w83793_read_value(client
, W83793_REG_TEMP_FAN_MAP(i
));
2007 for (j
= 1; j
< 5; j
++) {
2009 w83793_read_value(client
, W83793_REG_TEMP
[i
][j
]);
2011 data
->temp_cruise
[i
] =
2012 w83793_read_value(client
, W83793_REG_TEMP_CRUISE(i
));
2013 for (j
= 0; j
< 7; j
++) {
2014 data
->sf2_pwm
[i
][j
] =
2015 w83793_read_value(client
, W83793_REG_SF2_PWM(i
, j
));
2016 data
->sf2_temp
[i
][j
] =
2017 w83793_read_value(client
,
2018 W83793_REG_SF2_TEMP(i
, j
));
2022 for (i
= 0; i
< ARRAY_SIZE(data
->temp_mode
); i
++)
2023 data
->temp_mode
[i
] =
2024 w83793_read_value(client
, W83793_REG_TEMP_MODE
[i
]);
2026 for (i
= 0; i
< ARRAY_SIZE(data
->tolerance
); i
++) {
2027 data
->tolerance
[i
] =
2028 w83793_read_value(client
, W83793_REG_TEMP_TOL(i
));
2031 for (i
= 0; i
< ARRAY_SIZE(data
->pwm
); i
++) {
2032 if (!(data
->has_pwm
& (1 << i
)))
2034 data
->pwm
[i
][PWM_NONSTOP
] =
2035 w83793_read_value(client
, W83793_REG_PWM(i
, PWM_NONSTOP
));
2036 data
->pwm
[i
][PWM_START
] =
2037 w83793_read_value(client
, W83793_REG_PWM(i
, PWM_START
));
2038 data
->pwm_stop_time
[i
] =
2039 w83793_read_value(client
, W83793_REG_PWM_STOP_TIME(i
));
2042 data
->pwm_default
= w83793_read_value(client
, W83793_REG_PWM_DEFAULT
);
2043 data
->pwm_enable
= w83793_read_value(client
, W83793_REG_PWM_ENABLE
);
2044 data
->pwm_uptime
= w83793_read_value(client
, W83793_REG_PWM_UPTIME
);
2045 data
->pwm_downtime
= w83793_read_value(client
, W83793_REG_PWM_DOWNTIME
);
2046 data
->temp_critical
=
2047 w83793_read_value(client
, W83793_REG_TEMP_CRITICAL
);
2048 data
->beep_enable
= w83793_read_value(client
, W83793_REG_OVT_BEEP
);
2050 for (i
= 0; i
< ARRAY_SIZE(data
->beeps
); i
++)
2051 data
->beeps
[i
] = w83793_read_value(client
, W83793_REG_BEEP(i
));
2053 data
->last_nonvolatile
= jiffies
;
2056 static struct w83793_data
*w83793_update_device(struct device
*dev
)
2058 struct i2c_client
*client
= to_i2c_client(dev
);
2059 struct w83793_data
*data
= i2c_get_clientdata(client
);
2062 mutex_lock(&data
->update_lock
);
2064 if (!(time_after(jiffies
, data
->last_updated
+ HZ
* 2)
2068 /* Update the voltages measured value and limits */
2069 for (i
= 0; i
< ARRAY_SIZE(data
->in
); i
++)
2070 data
->in
[i
][IN_READ
] =
2071 w83793_read_value(client
, W83793_REG_IN
[i
][IN_READ
]);
2073 data
->in_low_bits
[IN_READ
] =
2074 w83793_read_value(client
, W83793_REG_IN_LOW_BITS
[IN_READ
]);
2076 for (i
= 0; i
< ARRAY_SIZE(data
->fan
); i
++) {
2077 if (!(data
->has_fan
& (1 << i
)))
2080 w83793_read_value(client
, W83793_REG_FAN(i
)) << 8;
2082 w83793_read_value(client
, W83793_REG_FAN(i
) + 1);
2085 for (i
= 0; i
< ARRAY_SIZE(data
->temp
); i
++) {
2086 if (!(data
->has_temp
& (1 << i
)))
2088 data
->temp
[i
][TEMP_READ
] =
2089 w83793_read_value(client
, W83793_REG_TEMP
[i
][TEMP_READ
]);
2092 data
->temp_low_bits
=
2093 w83793_read_value(client
, W83793_REG_TEMP_LOW_BITS
);
2095 for (i
= 0; i
< ARRAY_SIZE(data
->pwm
); i
++) {
2096 if (data
->has_pwm
& (1 << i
))
2097 data
->pwm
[i
][PWM_DUTY
] =
2098 w83793_read_value(client
,
2099 W83793_REG_PWM(i
, PWM_DUTY
));
2102 for (i
= 0; i
< ARRAY_SIZE(data
->alarms
); i
++)
2104 w83793_read_value(client
, W83793_REG_ALARM(i
));
2105 if (data
->has_vid
& 0x01)
2106 data
->vid
[0] = w83793_read_value(client
, W83793_REG_VID_INA
);
2107 if (data
->has_vid
& 0x02)
2108 data
->vid
[1] = w83793_read_value(client
, W83793_REG_VID_INB
);
2109 w83793_update_nonvolatile(dev
);
2110 data
->last_updated
= jiffies
;
2114 mutex_unlock(&data
->update_lock
);
2119 * Ignore the possibility that somebody change bank outside the driver
2120 * Must be called with data->update_lock held, except during initialization
2122 static u8
w83793_read_value(struct i2c_client
*client
, u16 reg
)
2124 struct w83793_data
*data
= i2c_get_clientdata(client
);
2126 u8 new_bank
= reg
>> 8;
2128 new_bank
|= data
->bank
& 0xfc;
2129 if (data
->bank
!= new_bank
) {
2130 if (i2c_smbus_write_byte_data
2131 (client
, W83793_REG_BANKSEL
, new_bank
) >= 0)
2132 data
->bank
= new_bank
;
2134 dev_err(&client
->dev
,
2135 "set bank to %d failed, fall back "
2136 "to bank %d, read reg 0x%x error\n",
2137 new_bank
, data
->bank
, reg
);
2138 res
= 0x0; /* read 0x0 from the chip */
2142 res
= i2c_smbus_read_byte_data(client
, reg
& 0xff);
2147 /* Must be called with data->update_lock held, except during initialization */
2148 static int w83793_write_value(struct i2c_client
*client
, u16 reg
, u8 value
)
2150 struct w83793_data
*data
= i2c_get_clientdata(client
);
2152 u8 new_bank
= reg
>> 8;
2154 new_bank
|= data
->bank
& 0xfc;
2155 if (data
->bank
!= new_bank
) {
2156 res
= i2c_smbus_write_byte_data(client
, W83793_REG_BANKSEL
,
2159 dev_err(&client
->dev
,
2160 "set bank to %d failed, fall back "
2161 "to bank %d, write reg 0x%x error\n",
2162 new_bank
, data
->bank
, reg
);
2165 data
->bank
= new_bank
;
2168 res
= i2c_smbus_write_byte_data(client
, reg
& 0xff, value
);
2173 module_i2c_driver(w83793_driver
);
2175 MODULE_AUTHOR("Yuan Mu, Sven Anders");
2176 MODULE_DESCRIPTION("w83793 driver");
2177 MODULE_LICENSE("GPL");