1 // SPDX-License-Identifier: GPL-2.0-only
3 * w83793.c - Linux kernel driver for hardware monitoring
4 * Copyright (C) 2006 Winbond Electronics Corp.
6 * Rudolf Marek <r.marek@assembler.cz>
7 * Copyright (C) 2009-2010 Sven Anders <anders@anduras.de>, ANDURAS AG.
9 * (Based partially on fschmd driver,
10 * Copyright 2007-2008 by Hans de Goede)
14 * Supports following chips:
16 * Chip #vin #fanin #pwm #temp wchipid vendid i2c ISA
17 * w83793 10 12 8 6 0x7b 0x5ca3 yes no
20 #include <linux/module.h>
21 #include <linux/init.h>
22 #include <linux/slab.h>
23 #include <linux/i2c.h>
24 #include <linux/hwmon.h>
25 #include <linux/hwmon-vid.h>
26 #include <linux/hwmon-sysfs.h>
27 #include <linux/err.h>
28 #include <linux/mutex.h>
30 #include <linux/watchdog.h>
31 #include <linux/miscdevice.h>
32 #include <linux/uaccess.h>
33 #include <linux/kref.h>
34 #include <linux/notifier.h>
35 #include <linux/reboot.h>
36 #include <linux/jiffies.h>
39 #define WATCHDOG_TIMEOUT 2 /* 2 minute default timeout */
41 /* Addresses to scan */
42 static const unsigned short normal_i2c[] = { 0x2c, 0x2d, 0x2e, 0x2f,
45 /* Insmod parameters */
47 static unsigned short force_subclients[4];
48 module_param_array(force_subclients, short, NULL, 0);
49 MODULE_PARM_DESC(force_subclients,
50 "List of subclient addresses: {bus, clientaddr, subclientaddr1, subclientaddr2}");
53 module_param(reset, bool, 0);
54 MODULE_PARM_DESC(reset, "Set to 1 to reset chip, not recommended");
56 static int timeout = WATCHDOG_TIMEOUT; /* default timeout in minutes */
57 module_param(timeout, int, 0);
58 MODULE_PARM_DESC(timeout,
59 "Watchdog timeout in minutes. 2<= timeout <=255 (default="
60 __MODULE_STRING(WATCHDOG_TIMEOUT) ")");
62 static bool nowayout = WATCHDOG_NOWAYOUT;
63 module_param(nowayout, bool, 0);
64 MODULE_PARM_DESC(nowayout,
65 "Watchdog cannot be stopped once started (default="
66 __MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
69 * Address 0x00, 0x0d, 0x0e, 0x0f in all three banks are reserved
70 * as ID, Bank Select registers
72 #define W83793_REG_BANKSEL 0x00
73 #define W83793_REG_VENDORID 0x0d
74 #define W83793_REG_CHIPID 0x0e
75 #define W83793_REG_DEVICEID 0x0f
77 #define W83793_REG_CONFIG 0x40
78 #define W83793_REG_MFC 0x58
79 #define W83793_REG_FANIN_CTRL 0x5c
80 #define W83793_REG_FANIN_SEL 0x5d
81 #define W83793_REG_I2C_ADDR 0x0b
82 #define W83793_REG_I2C_SUBADDR 0x0c
83 #define W83793_REG_VID_INA 0x05
84 #define W83793_REG_VID_INB 0x06
85 #define W83793_REG_VID_LATCHA 0x07
86 #define W83793_REG_VID_LATCHB 0x08
87 #define W83793_REG_VID_CTRL 0x59
89 #define W83793_REG_WDT_LOCK 0x01
90 #define W83793_REG_WDT_ENABLE 0x02
91 #define W83793_REG_WDT_STATUS 0x03
92 #define W83793_REG_WDT_TIMEOUT 0x04
94 static u16 W83793_REG_TEMP_MODE[2] = { 0x5e, 0x5f };
98 #define TEMP_CRIT_HYST 2
100 #define TEMP_WARN_HYST 4
102 * only crit and crit_hyst affect real-time alarm status
103 * current crit crit_hyst warn warn_hyst
105 static u16 W83793_REG_TEMP[][5] = {
106 {0x1c, 0x78, 0x79, 0x7a, 0x7b},
107 {0x1d, 0x7c, 0x7d, 0x7e, 0x7f},
108 {0x1e, 0x80, 0x81, 0x82, 0x83},
109 {0x1f, 0x84, 0x85, 0x86, 0x87},
110 {0x20, 0x88, 0x89, 0x8a, 0x8b},
111 {0x21, 0x8c, 0x8d, 0x8e, 0x8f},
114 #define W83793_REG_TEMP_LOW_BITS 0x22
116 #define W83793_REG_BEEP(index) (0x53 + (index))
117 #define W83793_REG_ALARM(index) (0x4b + (index))
119 #define W83793_REG_CLR_CHASSIS 0x4a /* SMI MASK4 */
120 #define W83793_REG_IRQ_CTRL 0x50
121 #define W83793_REG_OVT_CTRL 0x51
122 #define W83793_REG_OVT_BEEP 0x52
127 static const u16 W83793_REG_IN[][3] = {
128 /* Current, High, Low */
129 {0x10, 0x60, 0x61}, /* Vcore A */
130 {0x11, 0x62, 0x63}, /* Vcore B */
131 {0x12, 0x64, 0x65}, /* Vtt */
132 {0x14, 0x6a, 0x6b}, /* VSEN1 */
133 {0x15, 0x6c, 0x6d}, /* VSEN2 */
134 {0x16, 0x6e, 0x6f}, /* +3VSEN */
135 {0x17, 0x70, 0x71}, /* +12VSEN */
136 {0x18, 0x72, 0x73}, /* 5VDD */
137 {0x19, 0x74, 0x75}, /* 5VSB */
138 {0x1a, 0x76, 0x77}, /* VBAT */
141 /* Low Bits of Vcore A/B Vtt Read/High/Low */
142 static const u16 W83793_REG_IN_LOW_BITS[] = { 0x1b, 0x68, 0x69 };
143 static u8 scale_in[] = { 2, 2, 2, 16, 16, 16, 8, 24, 24, 16 };
144 static u8 scale_in_add[] = { 0, 0, 0, 0, 0, 0, 0, 150, 150, 0 };
146 #define W83793_REG_FAN(index) (0x23 + 2 * (index)) /* High byte */
147 #define W83793_REG_FAN_MIN(index) (0x90 + 2 * (index)) /* High byte */
149 #define W83793_REG_PWM_DEFAULT 0xb2
150 #define W83793_REG_PWM_ENABLE 0x207
151 #define W83793_REG_PWM_UPTIME 0xc3 /* Unit in 0.1 second */
152 #define W83793_REG_PWM_DOWNTIME 0xc4 /* Unit in 0.1 second */
153 #define W83793_REG_TEMP_CRITICAL 0xc5
157 #define PWM_NONSTOP 2
158 #define PWM_STOP_TIME 3
159 #define W83793_REG_PWM(index, nr) (((nr) == 0 ? 0xb3 : \
160 (nr) == 1 ? 0x220 : 0x218) + (index))
162 /* bit field, fan1 is bit0, fan2 is bit1 ... */
163 #define W83793_REG_TEMP_FAN_MAP(index) (0x201 + (index))
164 #define W83793_REG_TEMP_TOL(index) (0x208 + (index))
165 #define W83793_REG_TEMP_CRUISE(index) (0x210 + (index))
166 #define W83793_REG_PWM_STOP_TIME(index) (0x228 + (index))
167 #define W83793_REG_SF2_TEMP(index, nr) (0x230 + ((index) << 4) + (nr))
168 #define W83793_REG_SF2_PWM(index, nr) (0x238 + ((index) << 4) + (nr))
170 static inline unsigned long FAN_FROM_REG(u16 val)
172 if ((val >= 0xfff) || (val == 0))
174 return 1350000UL / val;
177 static inline u16 FAN_TO_REG(long rpm)
181 return clamp_val((1350000 + (rpm >> 1)) / rpm, 1, 0xffe);
184 static inline unsigned long TIME_FROM_REG(u8 reg)
189 static inline u8 TIME_TO_REG(unsigned long val)
191 return clamp_val((val + 50) / 100, 0, 0xff);
194 static inline long TEMP_FROM_REG(s8 reg)
199 static inline s8 TEMP_TO_REG(long val, s8 min, s8 max)
201 return clamp_val((val + (val < 0 ? -500 : 500)) / 1000, min, max);
205 struct i2c_client *lm75[2];
206 struct device *hwmon_dev;
207 struct mutex update_lock;
208 char valid; /* !=0 if following fields are valid */
209 unsigned long last_updated; /* In jiffies */
210 unsigned long last_nonvolatile; /* In jiffies, last time we update the
211 * nonvolatile registers
217 u8 in[10][3]; /* Register value, read/high/low */
218 u8 in_low_bits[3]; /* Additional resolution for VCore A/B Vtt */
220 u16 has_fan; /* Only fan1- fan5 has own pins */
221 u16 fan[12]; /* Register value combine */
222 u16 fan_min[12]; /* Register value combine */
224 s8 temp[6][5]; /* current, crit, crit_hyst,warn, warn_hyst */
225 u8 temp_low_bits; /* Additional resolution TD1-TD4 */
226 u8 temp_mode[2]; /* byte 0: Temp D1-D4 mode each has 2 bits
227 * byte 1: Temp R1,R2 mode, each has 1 bit
229 u8 temp_critical; /* If reached all fan will be at full speed */
230 u8 temp_fan_map[6]; /* Temp controls which pwm fan, bit field */
235 u8 pwm_enable; /* Register value, each Temp has 1 bit */
236 u8 pwm_uptime; /* Register value */
237 u8 pwm_downtime; /* Register value */
238 u8 pwm_default; /* All fan default pwm, next poweron valid */
239 u8 pwm[8][3]; /* Register value */
243 u8 alarms[5]; /* realtime status registers */
246 u8 tolerance[3]; /* Temp tolerance(Smart Fan I/II) */
247 u8 sf2_pwm[6][7]; /* Smart FanII: Fan duty cycle */
248 u8 sf2_temp[6][7]; /* Smart FanII: Temp level point */
251 struct i2c_client *client;
252 struct mutex watchdog_lock;
253 struct list_head list; /* member of the watchdog_data_list */
255 struct miscdevice watchdog_miscdev;
256 unsigned long watchdog_is_open;
257 char watchdog_expect_close;
258 char watchdog_name[10]; /* must be unique to avoid sysfs conflict */
259 unsigned int watchdog_caused_reboot;
260 int watchdog_timeout; /* watchdog timeout in minutes */
264 * Somewhat ugly :( global data pointer list with all devices, so that
265 * we can find our device data as when using misc_register. There is no
266 * other method to get to one's device data from the open file-op and
267 * for usage in the reboot notifier callback.
269 static LIST_HEAD(watchdog_data_list);
271 /* Note this lock not only protect list access, but also data.kref access */
272 static DEFINE_MUTEX(watchdog_data_mutex);
275 * Release our data struct when we're detached from the i2c client *and* all
276 * references to our watchdog device are released
278 static void w83793_release_resources(struct kref *ref)
280 struct w83793_data *data = container_of(ref, struct w83793_data, kref);
284 static u8 w83793_read_value(struct i2c_client *client, u16 reg);
285 static int w83793_write_value(struct i2c_client *client, u16 reg, u8 value);
286 static int w83793_probe(struct i2c_client *client,
287 const struct i2c_device_id *id);
288 static int w83793_detect(struct i2c_client *client,
289 struct i2c_board_info *info);
290 static int w83793_remove(struct i2c_client *client);
291 static void w83793_init_client(struct i2c_client *client);
292 static void w83793_update_nonvolatile(struct device *dev);
293 static struct w83793_data *w83793_update_device(struct device *dev);
295 static const struct i2c_device_id w83793_id[] = {
299 MODULE_DEVICE_TABLE(i2c, w83793_id);
301 static struct i2c_driver w83793_driver = {
302 .class = I2C_CLASS_HWMON,
306 .probe = w83793_probe,
307 .remove = w83793_remove,
308 .id_table = w83793_id,
309 .detect = w83793_detect,
310 .address_list = normal_i2c,
314 vrm_show(struct device *dev, struct device_attribute *attr, char *buf)
316 struct w83793_data *data = dev_get_drvdata(dev);
317 return sprintf(buf, "%d\n", data->vrm);
321 show_vid(struct device *dev, struct device_attribute *attr, char *buf)
323 struct w83793_data *data = w83793_update_device(dev);
324 struct sensor_device_attribute_2 *sensor_attr =
325 to_sensor_dev_attr_2(attr);
326 int index = sensor_attr->index;
328 return sprintf(buf, "%d\n", vid_from_reg(data->vid[index], data->vrm));
332 vrm_store(struct device *dev, struct device_attribute *attr,
333 const char *buf, size_t count)
335 struct w83793_data *data = dev_get_drvdata(dev);
339 err = kstrtoul(buf, 10, &val);
350 #define ALARM_STATUS 0
351 #define BEEP_ENABLE 1
353 show_alarm_beep(struct device *dev, struct device_attribute *attr, char *buf)
355 struct w83793_data *data = w83793_update_device(dev);
356 struct sensor_device_attribute_2 *sensor_attr =
357 to_sensor_dev_attr_2(attr);
358 int nr = sensor_attr->nr;
359 int index = sensor_attr->index >> 3;
360 int bit = sensor_attr->index & 0x07;
363 if (nr == ALARM_STATUS) {
364 val = (data->alarms[index] >> (bit)) & 1;
365 } else { /* BEEP_ENABLE */
366 val = (data->beeps[index] >> (bit)) & 1;
369 return sprintf(buf, "%u\n", val);
373 store_beep(struct device *dev, struct device_attribute *attr,
374 const char *buf, size_t count)
376 struct i2c_client *client = to_i2c_client(dev);
377 struct w83793_data *data = i2c_get_clientdata(client);
378 struct sensor_device_attribute_2 *sensor_attr =
379 to_sensor_dev_attr_2(attr);
380 int index = sensor_attr->index >> 3;
381 int shift = sensor_attr->index & 0x07;
382 u8 beep_bit = 1 << shift;
386 err = kstrtoul(buf, 10, &val);
393 mutex_lock(&data->update_lock);
394 data->beeps[index] = w83793_read_value(client, W83793_REG_BEEP(index));
395 data->beeps[index] &= ~beep_bit;
396 data->beeps[index] |= val << shift;
397 w83793_write_value(client, W83793_REG_BEEP(index), data->beeps[index]);
398 mutex_unlock(&data->update_lock);
404 show_beep_enable(struct device *dev, struct device_attribute *attr, char *buf)
406 struct w83793_data *data = w83793_update_device(dev);
407 return sprintf(buf, "%u\n", (data->beep_enable >> 1) & 0x01);
411 store_beep_enable(struct device *dev, struct device_attribute *attr,
412 const char *buf, size_t count)
414 struct i2c_client *client = to_i2c_client(dev);
415 struct w83793_data *data = i2c_get_clientdata(client);
419 err = kstrtoul(buf, 10, &val);
426 mutex_lock(&data->update_lock);
427 data->beep_enable = w83793_read_value(client, W83793_REG_OVT_BEEP)
429 data->beep_enable |= val << 1;
430 w83793_write_value(client, W83793_REG_OVT_BEEP, data->beep_enable);
431 mutex_unlock(&data->update_lock);
436 /* Write 0 to clear chassis alarm */
438 store_chassis_clear(struct device *dev,
439 struct device_attribute *attr, const char *buf,
442 struct i2c_client *client = to_i2c_client(dev);
443 struct w83793_data *data = i2c_get_clientdata(client);
448 err = kstrtoul(buf, 10, &val);
454 mutex_lock(&data->update_lock);
455 reg = w83793_read_value(client, W83793_REG_CLR_CHASSIS);
456 w83793_write_value(client, W83793_REG_CLR_CHASSIS, reg | 0x80);
457 data->valid = 0; /* Force cache refresh */
458 mutex_unlock(&data->update_lock);
465 show_fan(struct device *dev, struct device_attribute *attr, char *buf)
467 struct sensor_device_attribute_2 *sensor_attr =
468 to_sensor_dev_attr_2(attr);
469 int nr = sensor_attr->nr;
470 int index = sensor_attr->index;
471 struct w83793_data *data = w83793_update_device(dev);
475 val = data->fan[index] & 0x0fff;
477 val = data->fan_min[index] & 0x0fff;
479 return sprintf(buf, "%lu\n", FAN_FROM_REG(val));
483 store_fan_min(struct device *dev, struct device_attribute *attr,
484 const char *buf, size_t count)
486 struct sensor_device_attribute_2 *sensor_attr =
487 to_sensor_dev_attr_2(attr);
488 int index = sensor_attr->index;
489 struct i2c_client *client = to_i2c_client(dev);
490 struct w83793_data *data = i2c_get_clientdata(client);
494 err = kstrtoul(buf, 10, &val);
497 val = FAN_TO_REG(val);
499 mutex_lock(&data->update_lock);
500 data->fan_min[index] = val;
501 w83793_write_value(client, W83793_REG_FAN_MIN(index),
503 w83793_write_value(client, W83793_REG_FAN_MIN(index) + 1, val & 0xff);
504 mutex_unlock(&data->update_lock);
510 show_pwm(struct device *dev, struct device_attribute *attr, char *buf)
512 struct sensor_device_attribute_2 *sensor_attr =
513 to_sensor_dev_attr_2(attr);
514 struct w83793_data *data = w83793_update_device(dev);
516 int nr = sensor_attr->nr;
517 int index = sensor_attr->index;
519 if (nr == PWM_STOP_TIME)
520 val = TIME_FROM_REG(data->pwm_stop_time[index]);
522 val = (data->pwm[index][nr] & 0x3f) << 2;
524 return sprintf(buf, "%d\n", val);
528 store_pwm(struct device *dev, struct device_attribute *attr,
529 const char *buf, size_t count)
531 struct i2c_client *client = to_i2c_client(dev);
532 struct w83793_data *data = i2c_get_clientdata(client);
533 struct sensor_device_attribute_2 *sensor_attr =
534 to_sensor_dev_attr_2(attr);
535 int nr = sensor_attr->nr;
536 int index = sensor_attr->index;
540 err = kstrtoul(buf, 10, &val);
544 mutex_lock(&data->update_lock);
545 if (nr == PWM_STOP_TIME) {
546 val = TIME_TO_REG(val);
547 data->pwm_stop_time[index] = val;
548 w83793_write_value(client, W83793_REG_PWM_STOP_TIME(index),
551 val = clamp_val(val, 0, 0xff) >> 2;
552 data->pwm[index][nr] =
553 w83793_read_value(client, W83793_REG_PWM(index, nr)) & 0xc0;
554 data->pwm[index][nr] |= val;
555 w83793_write_value(client, W83793_REG_PWM(index, nr),
556 data->pwm[index][nr]);
559 mutex_unlock(&data->update_lock);
564 show_temp(struct device *dev, struct device_attribute *attr, char *buf)
566 struct sensor_device_attribute_2 *sensor_attr =
567 to_sensor_dev_attr_2(attr);
568 int nr = sensor_attr->nr;
569 int index = sensor_attr->index;
570 struct w83793_data *data = w83793_update_device(dev);
571 long temp = TEMP_FROM_REG(data->temp[index][nr]);
573 if (nr == TEMP_READ && index < 4) { /* Only TD1-TD4 have low bits */
574 int low = ((data->temp_low_bits >> (index * 2)) & 0x03) * 250;
575 temp += temp > 0 ? low : -low;
577 return sprintf(buf, "%ld\n", temp);
581 store_temp(struct device *dev, struct device_attribute *attr,
582 const char *buf, size_t count)
584 struct sensor_device_attribute_2 *sensor_attr =
585 to_sensor_dev_attr_2(attr);
586 int nr = sensor_attr->nr;
587 int index = sensor_attr->index;
588 struct i2c_client *client = to_i2c_client(dev);
589 struct w83793_data *data = i2c_get_clientdata(client);
593 err = kstrtol(buf, 10, &tmp);
597 mutex_lock(&data->update_lock);
598 data->temp[index][nr] = TEMP_TO_REG(tmp, -128, 127);
599 w83793_write_value(client, W83793_REG_TEMP[index][nr],
600 data->temp[index][nr]);
601 mutex_unlock(&data->update_lock);
607 * each has 4 mode:(2 bits)
609 * 1: Use internal temp sensor(default)
611 * 3: Use sensor in Intel CPU and get result by PECI
614 * each has 2 mode:(1 bit)
615 * 0: Disable temp sensor monitor
616 * 1: To enable temp sensors monitor
619 /* 0 disable, 6 PECI */
620 static u8 TO_TEMP_MODE[] = { 0, 0, 0, 6 };
623 show_temp_mode(struct device *dev, struct device_attribute *attr, char *buf)
625 struct w83793_data *data = w83793_update_device(dev);
626 struct sensor_device_attribute_2 *sensor_attr =
627 to_sensor_dev_attr_2(attr);
628 int index = sensor_attr->index;
629 u8 mask = (index < 4) ? 0x03 : 0x01;
630 u8 shift = (index < 4) ? (2 * index) : (index - 4);
632 index = (index < 4) ? 0 : 1;
634 tmp = (data->temp_mode[index] >> shift) & mask;
636 /* for the internal sensor, found out if diode or thermistor */
638 tmp = index == 0 ? 3 : 4;
640 tmp = TO_TEMP_MODE[tmp];
642 return sprintf(buf, "%d\n", tmp);
646 store_temp_mode(struct device *dev, struct device_attribute *attr,
647 const char *buf, size_t count)
649 struct i2c_client *client = to_i2c_client(dev);
650 struct w83793_data *data = i2c_get_clientdata(client);
651 struct sensor_device_attribute_2 *sensor_attr =
652 to_sensor_dev_attr_2(attr);
653 int index = sensor_attr->index;
654 u8 mask = (index < 4) ? 0x03 : 0x01;
655 u8 shift = (index < 4) ? (2 * index) : (index - 4);
659 err = kstrtoul(buf, 10, &val);
663 /* transform the sysfs interface values into table above */
664 if ((val == 6) && (index < 4)) {
666 } else if ((val == 3 && index < 4)
667 || (val == 4 && index >= 4)) {
668 /* transform diode or thermistor into internal enable */
674 index = (index < 4) ? 0 : 1;
675 mutex_lock(&data->update_lock);
676 data->temp_mode[index] =
677 w83793_read_value(client, W83793_REG_TEMP_MODE[index]);
678 data->temp_mode[index] &= ~(mask << shift);
679 data->temp_mode[index] |= val << shift;
680 w83793_write_value(client, W83793_REG_TEMP_MODE[index],
681 data->temp_mode[index]);
682 mutex_unlock(&data->update_lock);
687 #define SETUP_PWM_DEFAULT 0
688 #define SETUP_PWM_UPTIME 1 /* Unit in 0.1s */
689 #define SETUP_PWM_DOWNTIME 2 /* Unit in 0.1s */
690 #define SETUP_TEMP_CRITICAL 3
692 show_sf_setup(struct device *dev, struct device_attribute *attr, char *buf)
694 struct sensor_device_attribute_2 *sensor_attr =
695 to_sensor_dev_attr_2(attr);
696 int nr = sensor_attr->nr;
697 struct w83793_data *data = w83793_update_device(dev);
700 if (nr == SETUP_PWM_DEFAULT)
701 val = (data->pwm_default & 0x3f) << 2;
702 else if (nr == SETUP_PWM_UPTIME)
703 val = TIME_FROM_REG(data->pwm_uptime);
704 else if (nr == SETUP_PWM_DOWNTIME)
705 val = TIME_FROM_REG(data->pwm_downtime);
706 else if (nr == SETUP_TEMP_CRITICAL)
707 val = TEMP_FROM_REG(data->temp_critical & 0x7f);
709 return sprintf(buf, "%d\n", val);
713 store_sf_setup(struct device *dev, struct device_attribute *attr,
714 const char *buf, size_t count)
716 struct sensor_device_attribute_2 *sensor_attr =
717 to_sensor_dev_attr_2(attr);
718 int nr = sensor_attr->nr;
719 struct i2c_client *client = to_i2c_client(dev);
720 struct w83793_data *data = i2c_get_clientdata(client);
724 err = kstrtol(buf, 10, &val);
728 mutex_lock(&data->update_lock);
729 if (nr == SETUP_PWM_DEFAULT) {
731 w83793_read_value(client, W83793_REG_PWM_DEFAULT) & 0xc0;
732 data->pwm_default |= clamp_val(val, 0, 0xff) >> 2;
733 w83793_write_value(client, W83793_REG_PWM_DEFAULT,
735 } else if (nr == SETUP_PWM_UPTIME) {
736 data->pwm_uptime = TIME_TO_REG(val);
737 data->pwm_uptime += data->pwm_uptime == 0 ? 1 : 0;
738 w83793_write_value(client, W83793_REG_PWM_UPTIME,
740 } else if (nr == SETUP_PWM_DOWNTIME) {
741 data->pwm_downtime = TIME_TO_REG(val);
742 data->pwm_downtime += data->pwm_downtime == 0 ? 1 : 0;
743 w83793_write_value(client, W83793_REG_PWM_DOWNTIME,
745 } else { /* SETUP_TEMP_CRITICAL */
746 data->temp_critical =
747 w83793_read_value(client, W83793_REG_TEMP_CRITICAL) & 0x80;
748 data->temp_critical |= TEMP_TO_REG(val, 0, 0x7f);
749 w83793_write_value(client, W83793_REG_TEMP_CRITICAL,
750 data->temp_critical);
753 mutex_unlock(&data->update_lock);
758 * Temp SmartFan control
760 * Temp channel control which pwm fan, bitfield, bit 0 indicate pwm1...
761 * It's possible two or more temp channels control the same fan, w83793
762 * always prefers to pick the most critical request and applies it to
764 * It's possible one fan is not in any mapping of 6 temp channels, this
765 * means the fan is manual mode
768 * Each temp channel has its own SmartFan mode, and temp channel
769 * control fans that are set by TEMP_FAN_MAP
771 * 1: Thermal Cruise Mode
774 * Target temperature in thermal cruise mode, w83793 will try to turn
775 * fan speed to keep the temperature of target device around this
779 * If Temp higher or lower than target with this tolerance, w83793
780 * will take actions to speed up or slow down the fan to keep the
781 * temperature within the tolerance range.
784 #define TEMP_FAN_MAP 0
785 #define TEMP_PWM_ENABLE 1
786 #define TEMP_CRUISE 2
787 #define TEMP_TOLERANCE 3
789 show_sf_ctrl(struct device *dev, struct device_attribute *attr, char *buf)
791 struct sensor_device_attribute_2 *sensor_attr =
792 to_sensor_dev_attr_2(attr);
793 int nr = sensor_attr->nr;
794 int index = sensor_attr->index;
795 struct w83793_data *data = w83793_update_device(dev);
798 if (nr == TEMP_FAN_MAP) {
799 val = data->temp_fan_map[index];
800 } else if (nr == TEMP_PWM_ENABLE) {
801 /* +2 to transform into 2 and 3 to conform with sysfs intf */
802 val = ((data->pwm_enable >> index) & 0x01) + 2;
803 } else if (nr == TEMP_CRUISE) {
804 val = TEMP_FROM_REG(data->temp_cruise[index] & 0x7f);
805 } else { /* TEMP_TOLERANCE */
806 val = data->tolerance[index >> 1] >> ((index & 0x01) ? 4 : 0);
807 val = TEMP_FROM_REG(val & 0x0f);
809 return sprintf(buf, "%d\n", val);
813 store_sf_ctrl(struct device *dev, struct device_attribute *attr,
814 const char *buf, size_t count)
816 struct sensor_device_attribute_2 *sensor_attr =
817 to_sensor_dev_attr_2(attr);
818 int nr = sensor_attr->nr;
819 int index = sensor_attr->index;
820 struct i2c_client *client = to_i2c_client(dev);
821 struct w83793_data *data = i2c_get_clientdata(client);
825 err = kstrtol(buf, 10, &val);
829 mutex_lock(&data->update_lock);
830 if (nr == TEMP_FAN_MAP) {
831 val = clamp_val(val, 0, 255);
832 w83793_write_value(client, W83793_REG_TEMP_FAN_MAP(index), val);
833 data->temp_fan_map[index] = val;
834 } else if (nr == TEMP_PWM_ENABLE) {
835 if (val == 2 || val == 3) {
837 w83793_read_value(client, W83793_REG_PWM_ENABLE);
839 data->pwm_enable |= 1 << index;
841 data->pwm_enable &= ~(1 << index);
842 w83793_write_value(client, W83793_REG_PWM_ENABLE,
845 mutex_unlock(&data->update_lock);
848 } else if (nr == TEMP_CRUISE) {
849 data->temp_cruise[index] =
850 w83793_read_value(client, W83793_REG_TEMP_CRUISE(index));
851 data->temp_cruise[index] &= 0x80;
852 data->temp_cruise[index] |= TEMP_TO_REG(val, 0, 0x7f);
854 w83793_write_value(client, W83793_REG_TEMP_CRUISE(index),
855 data->temp_cruise[index]);
856 } else { /* TEMP_TOLERANCE */
858 u8 shift = (index & 0x01) ? 4 : 0;
860 w83793_read_value(client, W83793_REG_TEMP_TOL(i));
862 data->tolerance[i] &= ~(0x0f << shift);
863 data->tolerance[i] |= TEMP_TO_REG(val, 0, 0x0f) << shift;
864 w83793_write_value(client, W83793_REG_TEMP_TOL(i),
868 mutex_unlock(&data->update_lock);
873 show_sf2_pwm(struct device *dev, struct device_attribute *attr, char *buf)
875 struct sensor_device_attribute_2 *sensor_attr =
876 to_sensor_dev_attr_2(attr);
877 int nr = sensor_attr->nr;
878 int index = sensor_attr->index;
879 struct w83793_data *data = w83793_update_device(dev);
881 return sprintf(buf, "%d\n", (data->sf2_pwm[index][nr] & 0x3f) << 2);
885 store_sf2_pwm(struct device *dev, struct device_attribute *attr,
886 const char *buf, size_t count)
888 struct i2c_client *client = to_i2c_client(dev);
889 struct w83793_data *data = i2c_get_clientdata(client);
890 struct sensor_device_attribute_2 *sensor_attr =
891 to_sensor_dev_attr_2(attr);
892 int nr = sensor_attr->nr;
893 int index = sensor_attr->index;
897 err = kstrtoul(buf, 10, &val);
900 val = clamp_val(val, 0, 0xff) >> 2;
902 mutex_lock(&data->update_lock);
903 data->sf2_pwm[index][nr] =
904 w83793_read_value(client, W83793_REG_SF2_PWM(index, nr)) & 0xc0;
905 data->sf2_pwm[index][nr] |= val;
906 w83793_write_value(client, W83793_REG_SF2_PWM(index, nr),
907 data->sf2_pwm[index][nr]);
908 mutex_unlock(&data->update_lock);
913 show_sf2_temp(struct device *dev, struct device_attribute *attr, char *buf)
915 struct sensor_device_attribute_2 *sensor_attr =
916 to_sensor_dev_attr_2(attr);
917 int nr = sensor_attr->nr;
918 int index = sensor_attr->index;
919 struct w83793_data *data = w83793_update_device(dev);
921 return sprintf(buf, "%ld\n",
922 TEMP_FROM_REG(data->sf2_temp[index][nr] & 0x7f));
926 store_sf2_temp(struct device *dev, struct device_attribute *attr,
927 const char *buf, size_t count)
929 struct i2c_client *client = to_i2c_client(dev);
930 struct w83793_data *data = i2c_get_clientdata(client);
931 struct sensor_device_attribute_2 *sensor_attr =
932 to_sensor_dev_attr_2(attr);
933 int nr = sensor_attr->nr;
934 int index = sensor_attr->index;
938 err = kstrtol(buf, 10, &val);
941 val = TEMP_TO_REG(val, 0, 0x7f);
943 mutex_lock(&data->update_lock);
944 data->sf2_temp[index][nr] =
945 w83793_read_value(client, W83793_REG_SF2_TEMP(index, nr)) & 0x80;
946 data->sf2_temp[index][nr] |= val;
947 w83793_write_value(client, W83793_REG_SF2_TEMP(index, nr),
948 data->sf2_temp[index][nr]);
949 mutex_unlock(&data->update_lock);
953 /* only Vcore A/B and Vtt have additional 2 bits precision */
955 show_in(struct device *dev, struct device_attribute *attr, char *buf)
957 struct sensor_device_attribute_2 *sensor_attr =
958 to_sensor_dev_attr_2(attr);
959 int nr = sensor_attr->nr;
960 int index = sensor_attr->index;
961 struct w83793_data *data = w83793_update_device(dev);
962 u16 val = data->in[index][nr];
966 val += (data->in_low_bits[nr] >> (index * 2)) & 0x3;
968 /* voltage inputs 5VDD and 5VSB needs 150mV offset */
969 val = val * scale_in[index] + scale_in_add[index];
970 return sprintf(buf, "%d\n", val);
974 store_in(struct device *dev, struct device_attribute *attr,
975 const char *buf, size_t count)
977 struct sensor_device_attribute_2 *sensor_attr =
978 to_sensor_dev_attr_2(attr);
979 int nr = sensor_attr->nr;
980 int index = sensor_attr->index;
981 struct i2c_client *client = to_i2c_client(dev);
982 struct w83793_data *data = i2c_get_clientdata(client);
986 err = kstrtoul(buf, 10, &val);
989 val = (val + scale_in[index] / 2) / scale_in[index];
991 mutex_lock(&data->update_lock);
993 /* fix the limit values of 5VDD and 5VSB to ALARM mechanism */
994 if (nr == 1 || nr == 2)
995 val -= scale_in_add[index] / scale_in[index];
996 val = clamp_val(val, 0, 255);
998 val = clamp_val(val, 0, 0x3FF);
999 data->in_low_bits[nr] =
1000 w83793_read_value(client, W83793_REG_IN_LOW_BITS[nr]);
1001 data->in_low_bits[nr] &= ~(0x03 << (2 * index));
1002 data->in_low_bits[nr] |= (val & 0x03) << (2 * index);
1003 w83793_write_value(client, W83793_REG_IN_LOW_BITS[nr],
1004 data->in_low_bits[nr]);
1007 data->in[index][nr] = val;
1008 w83793_write_value(client, W83793_REG_IN[index][nr],
1009 data->in[index][nr]);
1010 mutex_unlock(&data->update_lock);
1016 #define SENSOR_ATTR_IN(index) \
1017 SENSOR_ATTR_2(in##index##_input, S_IRUGO, show_in, NULL, \
1019 SENSOR_ATTR_2(in##index##_max, S_IRUGO | S_IWUSR, show_in, \
1020 store_in, IN_MAX, index), \
1021 SENSOR_ATTR_2(in##index##_min, S_IRUGO | S_IWUSR, show_in, \
1022 store_in, IN_LOW, index), \
1023 SENSOR_ATTR_2(in##index##_alarm, S_IRUGO, show_alarm_beep, \
1024 NULL, ALARM_STATUS, index + ((index > 2) ? 1 : 0)), \
1025 SENSOR_ATTR_2(in##index##_beep, S_IWUSR | S_IRUGO, \
1026 show_alarm_beep, store_beep, BEEP_ENABLE, \
1027 index + ((index > 2) ? 1 : 0))
1029 #define SENSOR_ATTR_FAN(index) \
1030 SENSOR_ATTR_2(fan##index##_alarm, S_IRUGO, show_alarm_beep, \
1031 NULL, ALARM_STATUS, index + 17), \
1032 SENSOR_ATTR_2(fan##index##_beep, S_IWUSR | S_IRUGO, \
1033 show_alarm_beep, store_beep, BEEP_ENABLE, index + 17), \
1034 SENSOR_ATTR_2(fan##index##_input, S_IRUGO, show_fan, \
1035 NULL, FAN_INPUT, index - 1), \
1036 SENSOR_ATTR_2(fan##index##_min, S_IWUSR | S_IRUGO, \
1037 show_fan, store_fan_min, FAN_MIN, index - 1)
1039 #define SENSOR_ATTR_PWM(index) \
1040 SENSOR_ATTR_2(pwm##index, S_IWUSR | S_IRUGO, show_pwm, \
1041 store_pwm, PWM_DUTY, index - 1), \
1042 SENSOR_ATTR_2(pwm##index##_nonstop, S_IWUSR | S_IRUGO, \
1043 show_pwm, store_pwm, PWM_NONSTOP, index - 1), \
1044 SENSOR_ATTR_2(pwm##index##_start, S_IWUSR | S_IRUGO, \
1045 show_pwm, store_pwm, PWM_START, index - 1), \
1046 SENSOR_ATTR_2(pwm##index##_stop_time, S_IWUSR | S_IRUGO, \
1047 show_pwm, store_pwm, PWM_STOP_TIME, index - 1)
1049 #define SENSOR_ATTR_TEMP(index) \
1050 SENSOR_ATTR_2(temp##index##_type, S_IRUGO | S_IWUSR, \
1051 show_temp_mode, store_temp_mode, NOT_USED, index - 1), \
1052 SENSOR_ATTR_2(temp##index##_input, S_IRUGO, show_temp, \
1053 NULL, TEMP_READ, index - 1), \
1054 SENSOR_ATTR_2(temp##index##_max, S_IRUGO | S_IWUSR, show_temp, \
1055 store_temp, TEMP_CRIT, index - 1), \
1056 SENSOR_ATTR_2(temp##index##_max_hyst, S_IRUGO | S_IWUSR, \
1057 show_temp, store_temp, TEMP_CRIT_HYST, index - 1), \
1058 SENSOR_ATTR_2(temp##index##_warn, S_IRUGO | S_IWUSR, show_temp, \
1059 store_temp, TEMP_WARN, index - 1), \
1060 SENSOR_ATTR_2(temp##index##_warn_hyst, S_IRUGO | S_IWUSR, \
1061 show_temp, store_temp, TEMP_WARN_HYST, index - 1), \
1062 SENSOR_ATTR_2(temp##index##_alarm, S_IRUGO, \
1063 show_alarm_beep, NULL, ALARM_STATUS, index + 11), \
1064 SENSOR_ATTR_2(temp##index##_beep, S_IWUSR | S_IRUGO, \
1065 show_alarm_beep, store_beep, BEEP_ENABLE, index + 11), \
1066 SENSOR_ATTR_2(temp##index##_auto_channels_pwm, \
1067 S_IRUGO | S_IWUSR, show_sf_ctrl, store_sf_ctrl, \
1068 TEMP_FAN_MAP, index - 1), \
1069 SENSOR_ATTR_2(temp##index##_pwm_enable, S_IWUSR | S_IRUGO, \
1070 show_sf_ctrl, store_sf_ctrl, TEMP_PWM_ENABLE, \
1072 SENSOR_ATTR_2(thermal_cruise##index, S_IRUGO | S_IWUSR, \
1073 show_sf_ctrl, store_sf_ctrl, TEMP_CRUISE, index - 1), \
1074 SENSOR_ATTR_2(tolerance##index, S_IRUGO | S_IWUSR, show_sf_ctrl,\
1075 store_sf_ctrl, TEMP_TOLERANCE, index - 1), \
1076 SENSOR_ATTR_2(temp##index##_auto_point1_pwm, S_IRUGO | S_IWUSR, \
1077 show_sf2_pwm, store_sf2_pwm, 0, index - 1), \
1078 SENSOR_ATTR_2(temp##index##_auto_point2_pwm, S_IRUGO | S_IWUSR, \
1079 show_sf2_pwm, store_sf2_pwm, 1, index - 1), \
1080 SENSOR_ATTR_2(temp##index##_auto_point3_pwm, S_IRUGO | S_IWUSR, \
1081 show_sf2_pwm, store_sf2_pwm, 2, index - 1), \
1082 SENSOR_ATTR_2(temp##index##_auto_point4_pwm, S_IRUGO | S_IWUSR, \
1083 show_sf2_pwm, store_sf2_pwm, 3, index - 1), \
1084 SENSOR_ATTR_2(temp##index##_auto_point5_pwm, S_IRUGO | S_IWUSR, \
1085 show_sf2_pwm, store_sf2_pwm, 4, index - 1), \
1086 SENSOR_ATTR_2(temp##index##_auto_point6_pwm, S_IRUGO | S_IWUSR, \
1087 show_sf2_pwm, store_sf2_pwm, 5, index - 1), \
1088 SENSOR_ATTR_2(temp##index##_auto_point7_pwm, S_IRUGO | S_IWUSR, \
1089 show_sf2_pwm, store_sf2_pwm, 6, index - 1), \
1090 SENSOR_ATTR_2(temp##index##_auto_point1_temp, S_IRUGO | S_IWUSR,\
1091 show_sf2_temp, store_sf2_temp, 0, index - 1), \
1092 SENSOR_ATTR_2(temp##index##_auto_point2_temp, S_IRUGO | S_IWUSR,\
1093 show_sf2_temp, store_sf2_temp, 1, index - 1), \
1094 SENSOR_ATTR_2(temp##index##_auto_point3_temp, S_IRUGO | S_IWUSR,\
1095 show_sf2_temp, store_sf2_temp, 2, index - 1), \
1096 SENSOR_ATTR_2(temp##index##_auto_point4_temp, S_IRUGO | S_IWUSR,\
1097 show_sf2_temp, store_sf2_temp, 3, index - 1), \
1098 SENSOR_ATTR_2(temp##index##_auto_point5_temp, S_IRUGO | S_IWUSR,\
1099 show_sf2_temp, store_sf2_temp, 4, index - 1), \
1100 SENSOR_ATTR_2(temp##index##_auto_point6_temp, S_IRUGO | S_IWUSR,\
1101 show_sf2_temp, store_sf2_temp, 5, index - 1), \
1102 SENSOR_ATTR_2(temp##index##_auto_point7_temp, S_IRUGO | S_IWUSR,\
1103 show_sf2_temp, store_sf2_temp, 6, index - 1)
1105 static struct sensor_device_attribute_2 w83793_sensor_attr_2[] = {
1126 static struct sensor_device_attribute_2 w83793_temp[] = {
1127 SENSOR_ATTR_TEMP(1),
1128 SENSOR_ATTR_TEMP(2),
1129 SENSOR_ATTR_TEMP(3),
1130 SENSOR_ATTR_TEMP(4),
1131 SENSOR_ATTR_TEMP(5),
1132 SENSOR_ATTR_TEMP(6),
1136 static struct sensor_device_attribute_2 w83793_left_fan[] = {
1141 SENSOR_ATTR_FAN(10),
1142 SENSOR_ATTR_FAN(11),
1143 SENSOR_ATTR_FAN(12),
1147 static struct sensor_device_attribute_2 w83793_left_pwm[] = {
1155 static struct sensor_device_attribute_2 w83793_vid[] = {
1156 SENSOR_ATTR_2(cpu0_vid, S_IRUGO, show_vid, NULL, NOT_USED, 0),
1157 SENSOR_ATTR_2(cpu1_vid, S_IRUGO, show_vid, NULL, NOT_USED, 1),
1159 static DEVICE_ATTR_RW(vrm);
1161 static struct sensor_device_attribute_2 sda_single_files[] = {
1162 SENSOR_ATTR_2(intrusion0_alarm, S_IWUSR | S_IRUGO, show_alarm_beep,
1163 store_chassis_clear, ALARM_STATUS, 30),
1164 SENSOR_ATTR_2(beep_enable, S_IWUSR | S_IRUGO, show_beep_enable,
1165 store_beep_enable, NOT_USED, NOT_USED),
1166 SENSOR_ATTR_2(pwm_default, S_IWUSR | S_IRUGO, show_sf_setup,
1167 store_sf_setup, SETUP_PWM_DEFAULT, NOT_USED),
1168 SENSOR_ATTR_2(pwm_uptime, S_IWUSR | S_IRUGO, show_sf_setup,
1169 store_sf_setup, SETUP_PWM_UPTIME, NOT_USED),
1170 SENSOR_ATTR_2(pwm_downtime, S_IWUSR | S_IRUGO, show_sf_setup,
1171 store_sf_setup, SETUP_PWM_DOWNTIME, NOT_USED),
1172 SENSOR_ATTR_2(temp_critical, S_IWUSR | S_IRUGO, show_sf_setup,
1173 store_sf_setup, SETUP_TEMP_CRITICAL, NOT_USED),
1176 static void w83793_init_client(struct i2c_client *client)
1179 w83793_write_value(client, W83793_REG_CONFIG, 0x80);
1181 /* Start monitoring */
1182 w83793_write_value(client, W83793_REG_CONFIG,
1183 w83793_read_value(client, W83793_REG_CONFIG) | 0x01);
1190 static int watchdog_set_timeout(struct w83793_data *data, int timeout)
1192 unsigned int mtimeout;
1195 mtimeout = DIV_ROUND_UP(timeout, 60);
1200 mutex_lock(&data->watchdog_lock);
1201 if (!data->client) {
1206 data->watchdog_timeout = mtimeout;
1208 /* Set Timeout value (in Minutes) */
1209 w83793_write_value(data->client, W83793_REG_WDT_TIMEOUT,
1210 data->watchdog_timeout);
1212 ret = mtimeout * 60;
1215 mutex_unlock(&data->watchdog_lock);
1219 static int watchdog_get_timeout(struct w83793_data *data)
1223 mutex_lock(&data->watchdog_lock);
1224 timeout = data->watchdog_timeout * 60;
1225 mutex_unlock(&data->watchdog_lock);
1230 static int watchdog_trigger(struct w83793_data *data)
1234 mutex_lock(&data->watchdog_lock);
1235 if (!data->client) {
1240 /* Set Timeout value (in Minutes) */
1241 w83793_write_value(data->client, W83793_REG_WDT_TIMEOUT,
1242 data->watchdog_timeout);
1245 mutex_unlock(&data->watchdog_lock);
1249 static int watchdog_enable(struct w83793_data *data)
1253 mutex_lock(&data->watchdog_lock);
1254 if (!data->client) {
1259 /* Set initial timeout */
1260 w83793_write_value(data->client, W83793_REG_WDT_TIMEOUT,
1261 data->watchdog_timeout);
1263 /* Enable Soft Watchdog */
1264 w83793_write_value(data->client, W83793_REG_WDT_LOCK, 0x55);
1267 mutex_unlock(&data->watchdog_lock);
1271 static int watchdog_disable(struct w83793_data *data)
1275 mutex_lock(&data->watchdog_lock);
1276 if (!data->client) {
1281 /* Disable Soft Watchdog */
1282 w83793_write_value(data->client, W83793_REG_WDT_LOCK, 0xAA);
1285 mutex_unlock(&data->watchdog_lock);
1289 static int watchdog_open(struct inode *inode, struct file *filp)
1291 struct w83793_data *pos, *data = NULL;
1292 int watchdog_is_open;
1295 * We get called from drivers/char/misc.c with misc_mtx hold, and we
1296 * call misc_register() from w83793_probe() with watchdog_data_mutex
1297 * hold, as misc_register() takes the misc_mtx lock, this is a possible
1298 * deadlock, so we use mutex_trylock here.
1300 if (!mutex_trylock(&watchdog_data_mutex))
1301 return -ERESTARTSYS;
1302 list_for_each_entry(pos, &watchdog_data_list, list) {
1303 if (pos->watchdog_miscdev.minor == iminor(inode)) {
1309 /* Check, if device is already open */
1310 watchdog_is_open = test_and_set_bit(0, &data->watchdog_is_open);
1313 * Increase data reference counter (if not already done).
1314 * Note we can never not have found data, so we don't check for this
1316 if (!watchdog_is_open)
1317 kref_get(&data->kref);
1319 mutex_unlock(&watchdog_data_mutex);
1321 /* Check, if device is already open and possibly issue error */
1322 if (watchdog_is_open)
1325 /* Enable Soft Watchdog */
1326 watchdog_enable(data);
1328 /* Store pointer to data into filp's private data */
1329 filp->private_data = data;
1331 return stream_open(inode, filp);
1334 static int watchdog_close(struct inode *inode, struct file *filp)
1336 struct w83793_data *data = filp->private_data;
1338 if (data->watchdog_expect_close) {
1339 watchdog_disable(data);
1340 data->watchdog_expect_close = 0;
1342 watchdog_trigger(data);
1343 dev_crit(&data->client->dev,
1344 "unexpected close, not stopping watchdog!\n");
1347 clear_bit(0, &data->watchdog_is_open);
1349 /* Decrease data reference counter */
1350 mutex_lock(&watchdog_data_mutex);
1351 kref_put(&data->kref, w83793_release_resources);
1352 mutex_unlock(&watchdog_data_mutex);
1357 static ssize_t watchdog_write(struct file *filp, const char __user *buf,
1358 size_t count, loff_t *offset)
1361 struct w83793_data *data = filp->private_data;
1367 /* Clear it in case it was set with a previous write */
1368 data->watchdog_expect_close = 0;
1370 for (i = 0; i != count; i++) {
1372 if (get_user(c, buf + i))
1375 data->watchdog_expect_close = 1;
1378 ret = watchdog_trigger(data);
1385 static long watchdog_ioctl(struct file *filp, unsigned int cmd,
1388 struct watchdog_info ident = {
1389 .options = WDIOF_KEEPALIVEPING |
1392 .identity = "w83793 watchdog"
1396 struct w83793_data *data = filp->private_data;
1399 case WDIOC_GETSUPPORT:
1401 ident.options |= WDIOF_MAGICCLOSE;
1402 if (copy_to_user((void __user *)arg, &ident, sizeof(ident)))
1406 case WDIOC_GETSTATUS:
1407 val = data->watchdog_caused_reboot ? WDIOF_CARDRESET : 0;
1408 ret = put_user(val, (int __user *)arg);
1411 case WDIOC_GETBOOTSTATUS:
1412 ret = put_user(0, (int __user *)arg);
1415 case WDIOC_KEEPALIVE:
1416 ret = watchdog_trigger(data);
1419 case WDIOC_GETTIMEOUT:
1420 val = watchdog_get_timeout(data);
1421 ret = put_user(val, (int __user *)arg);
1424 case WDIOC_SETTIMEOUT:
1425 if (get_user(val, (int __user *)arg)) {
1429 ret = watchdog_set_timeout(data, val);
1431 ret = put_user(ret, (int __user *)arg);
1434 case WDIOC_SETOPTIONS:
1435 if (get_user(val, (int __user *)arg)) {
1440 if (val & WDIOS_DISABLECARD)
1441 ret = watchdog_disable(data);
1442 else if (val & WDIOS_ENABLECARD)
1443 ret = watchdog_enable(data);
1454 static const struct file_operations watchdog_fops = {
1455 .owner = THIS_MODULE,
1456 .llseek = no_llseek,
1457 .open = watchdog_open,
1458 .release = watchdog_close,
1459 .write = watchdog_write,
1460 .unlocked_ioctl = watchdog_ioctl,
1464 * Notifier for system down
1467 static int watchdog_notify_sys(struct notifier_block *this, unsigned long code,
1470 struct w83793_data *data = NULL;
1472 if (code == SYS_DOWN || code == SYS_HALT) {
1474 /* Disable each registered watchdog */
1475 mutex_lock(&watchdog_data_mutex);
1476 list_for_each_entry(data, &watchdog_data_list, list) {
1477 if (data->watchdog_miscdev.minor)
1478 watchdog_disable(data);
1480 mutex_unlock(&watchdog_data_mutex);
1487 * The WDT needs to learn about soft shutdowns in order to
1488 * turn the timebomb registers off.
1491 static struct notifier_block watchdog_notifier = {
1492 .notifier_call = watchdog_notify_sys,
1496 * Init / remove routines
1499 static int w83793_remove(struct i2c_client *client)
1501 struct w83793_data *data = i2c_get_clientdata(client);
1502 struct device *dev = &client->dev;
1505 /* Unregister the watchdog (if registered) */
1506 if (data->watchdog_miscdev.minor) {
1507 misc_deregister(&data->watchdog_miscdev);
1509 if (data->watchdog_is_open) {
1510 dev_warn(&client->dev,
1511 "i2c client detached with watchdog open! "
1512 "Stopping watchdog.\n");
1513 watchdog_disable(data);
1516 mutex_lock(&watchdog_data_mutex);
1517 list_del(&data->list);
1518 mutex_unlock(&watchdog_data_mutex);
1520 /* Tell the watchdog code the client is gone */
1521 mutex_lock(&data->watchdog_lock);
1522 data->client = NULL;
1523 mutex_unlock(&data->watchdog_lock);
1526 /* Reset Configuration Register to Disable Watch Dog Registers */
1527 tmp = w83793_read_value(client, W83793_REG_CONFIG);
1528 w83793_write_value(client, W83793_REG_CONFIG, tmp & ~0x04);
1530 unregister_reboot_notifier(&watchdog_notifier);
1532 hwmon_device_unregister(data->hwmon_dev);
1534 for (i = 0; i < ARRAY_SIZE(w83793_sensor_attr_2); i++)
1535 device_remove_file(dev,
1536 &w83793_sensor_attr_2[i].dev_attr);
1538 for (i = 0; i < ARRAY_SIZE(sda_single_files); i++)
1539 device_remove_file(dev, &sda_single_files[i].dev_attr);
1541 for (i = 0; i < ARRAY_SIZE(w83793_vid); i++)
1542 device_remove_file(dev, &w83793_vid[i].dev_attr);
1543 device_remove_file(dev, &dev_attr_vrm);
1545 for (i = 0; i < ARRAY_SIZE(w83793_left_fan); i++)
1546 device_remove_file(dev, &w83793_left_fan[i].dev_attr);
1548 for (i = 0; i < ARRAY_SIZE(w83793_left_pwm); i++)
1549 device_remove_file(dev, &w83793_left_pwm[i].dev_attr);
1551 for (i = 0; i < ARRAY_SIZE(w83793_temp); i++)
1552 device_remove_file(dev, &w83793_temp[i].dev_attr);
1554 i2c_unregister_device(data->lm75[0]);
1555 i2c_unregister_device(data->lm75[1]);
1557 /* Decrease data reference counter */
1558 mutex_lock(&watchdog_data_mutex);
1559 kref_put(&data->kref, w83793_release_resources);
1560 mutex_unlock(&watchdog_data_mutex);
1566 w83793_detect_subclients(struct i2c_client *client)
1569 int address = client->addr;
1571 struct i2c_adapter *adapter = client->adapter;
1572 struct w83793_data *data = i2c_get_clientdata(client);
1574 id = i2c_adapter_id(adapter);
1575 if (force_subclients[0] == id && force_subclients[1] == address) {
1576 for (i = 2; i <= 3; i++) {
1577 if (force_subclients[i] < 0x48
1578 || force_subclients[i] > 0x4f) {
1579 dev_err(&client->dev,
1580 "invalid subclient "
1581 "address %d; must be 0x48-0x4f\n",
1582 force_subclients[i]);
1587 w83793_write_value(client, W83793_REG_I2C_SUBADDR,
1588 (force_subclients[2] & 0x07) |
1589 ((force_subclients[3] & 0x07) << 4));
1592 tmp = w83793_read_value(client, W83793_REG_I2C_SUBADDR);
1594 data->lm75[0] = i2c_new_dummy(adapter, 0x48 + (tmp & 0x7));
1595 if (!(tmp & 0x80)) {
1596 if ((data->lm75[0] != NULL)
1597 && ((tmp & 0x7) == ((tmp >> 4) & 0x7))) {
1598 dev_err(&client->dev,
1599 "duplicate addresses 0x%x, "
1600 "use force_subclients\n", data->lm75[0]->addr);
1604 data->lm75[1] = i2c_new_dummy(adapter,
1605 0x48 + ((tmp >> 4) & 0x7));
1610 /* Undo inits in case of errors */
1613 i2c_unregister_device(data->lm75[0]);
1618 /* Return 0 if detection is successful, -ENODEV otherwise */
1619 static int w83793_detect(struct i2c_client *client,
1620 struct i2c_board_info *info)
1622 u8 tmp, bank, chip_id;
1623 struct i2c_adapter *adapter = client->adapter;
1624 unsigned short address = client->addr;
1626 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
1629 bank = i2c_smbus_read_byte_data(client, W83793_REG_BANKSEL);
1631 tmp = bank & 0x80 ? 0x5c : 0xa3;
1632 /* Check Winbond vendor ID */
1633 if (tmp != i2c_smbus_read_byte_data(client, W83793_REG_VENDORID)) {
1634 pr_debug("w83793: Detection failed at check vendor id\n");
1639 * If Winbond chip, address of chip and W83793_REG_I2C_ADDR
1642 if ((bank & 0x07) == 0
1643 && i2c_smbus_read_byte_data(client, W83793_REG_I2C_ADDR) !=
1645 pr_debug("w83793: Detection failed at check i2c addr\n");
1649 /* Determine the chip type now */
1650 chip_id = i2c_smbus_read_byte_data(client, W83793_REG_CHIPID);
1651 if (chip_id != 0x7b)
1654 strlcpy(info->type, "w83793", I2C_NAME_SIZE);
1659 static int w83793_probe(struct i2c_client *client,
1660 const struct i2c_device_id *id)
1662 struct device *dev = &client->dev;
1663 static const int watchdog_minors[] = {
1664 WATCHDOG_MINOR, 212, 213, 214, 215
1666 struct w83793_data *data;
1667 int i, tmp, val, err;
1668 int files_fan = ARRAY_SIZE(w83793_left_fan) / 7;
1669 int files_pwm = ARRAY_SIZE(w83793_left_pwm) / 5;
1670 int files_temp = ARRAY_SIZE(w83793_temp) / 6;
1672 data = kzalloc(sizeof(struct w83793_data), GFP_KERNEL);
1678 i2c_set_clientdata(client, data);
1679 data->bank = i2c_smbus_read_byte_data(client, W83793_REG_BANKSEL);
1680 mutex_init(&data->update_lock);
1681 mutex_init(&data->watchdog_lock);
1682 INIT_LIST_HEAD(&data->list);
1683 kref_init(&data->kref);
1686 * Store client pointer in our data struct for watchdog usage
1687 * (where the client is found through a data ptr instead of the
1690 data->client = client;
1692 err = w83793_detect_subclients(client);
1696 /* Initialize the chip */
1697 w83793_init_client(client);
1700 * Only fan 1-5 has their own input pins,
1701 * Pwm 1-3 has their own pins
1703 data->has_fan = 0x1f;
1704 data->has_pwm = 0x07;
1705 tmp = w83793_read_value(client, W83793_REG_MFC);
1706 val = w83793_read_value(client, W83793_REG_FANIN_CTRL);
1708 /* check the function of pins 49-56 */
1710 data->has_vid |= 0x2; /* has VIDB */
1712 data->has_pwm |= 0x18; /* pwm 4,5 */
1713 if (val & 0x01) { /* fan 6 */
1714 data->has_fan |= 0x20;
1715 data->has_pwm |= 0x20;
1717 if (val & 0x02) { /* fan 7 */
1718 data->has_fan |= 0x40;
1719 data->has_pwm |= 0x40;
1721 if (!(tmp & 0x40) && (val & 0x04)) { /* fan 8 */
1722 data->has_fan |= 0x80;
1723 data->has_pwm |= 0x80;
1727 /* check the function of pins 37-40 */
1729 data->has_vid |= 0x1; /* has VIDA */
1730 if (0x08 == (tmp & 0x0c)) {
1731 if (val & 0x08) /* fan 9 */
1732 data->has_fan |= 0x100;
1733 if (val & 0x10) /* fan 10 */
1734 data->has_fan |= 0x200;
1736 if (0x20 == (tmp & 0x30)) {
1737 if (val & 0x20) /* fan 11 */
1738 data->has_fan |= 0x400;
1739 if (val & 0x40) /* fan 12 */
1740 data->has_fan |= 0x800;
1743 if ((tmp & 0x01) && (val & 0x04)) { /* fan 8, second location */
1744 data->has_fan |= 0x80;
1745 data->has_pwm |= 0x80;
1748 tmp = w83793_read_value(client, W83793_REG_FANIN_SEL);
1749 if ((tmp & 0x01) && (val & 0x08)) { /* fan 9, second location */
1750 data->has_fan |= 0x100;
1752 if ((tmp & 0x02) && (val & 0x10)) { /* fan 10, second location */
1753 data->has_fan |= 0x200;
1755 if ((tmp & 0x04) && (val & 0x20)) { /* fan 11, second location */
1756 data->has_fan |= 0x400;
1758 if ((tmp & 0x08) && (val & 0x40)) { /* fan 12, second location */
1759 data->has_fan |= 0x800;
1762 /* check the temp1-6 mode, ignore former AMDSI selected inputs */
1763 tmp = w83793_read_value(client, W83793_REG_TEMP_MODE[0]);
1765 data->has_temp |= 0x01;
1767 data->has_temp |= 0x02;
1769 data->has_temp |= 0x04;
1771 data->has_temp |= 0x08;
1773 tmp = w83793_read_value(client, W83793_REG_TEMP_MODE[1]);
1775 data->has_temp |= 0x10;
1777 data->has_temp |= 0x20;
1779 /* Register sysfs hooks */
1780 for (i = 0; i < ARRAY_SIZE(w83793_sensor_attr_2); i++) {
1781 err = device_create_file(dev,
1782 &w83793_sensor_attr_2[i].dev_attr);
1787 for (i = 0; i < ARRAY_SIZE(w83793_vid); i++) {
1788 if (!(data->has_vid & (1 << i)))
1790 err = device_create_file(dev, &w83793_vid[i].dev_attr);
1794 if (data->has_vid) {
1795 data->vrm = vid_which_vrm();
1796 err = device_create_file(dev, &dev_attr_vrm);
1801 for (i = 0; i < ARRAY_SIZE(sda_single_files); i++) {
1802 err = device_create_file(dev, &sda_single_files[i].dev_attr);
1808 for (i = 0; i < 6; i++) {
1810 if (!(data->has_temp & (1 << i)))
1812 for (j = 0; j < files_temp; j++) {
1813 err = device_create_file(dev,
1814 &w83793_temp[(i) * files_temp
1821 for (i = 5; i < 12; i++) {
1823 if (!(data->has_fan & (1 << i)))
1825 for (j = 0; j < files_fan; j++) {
1826 err = device_create_file(dev,
1827 &w83793_left_fan[(i - 5) * files_fan
1834 for (i = 3; i < 8; i++) {
1836 if (!(data->has_pwm & (1 << i)))
1838 for (j = 0; j < files_pwm; j++) {
1839 err = device_create_file(dev,
1840 &w83793_left_pwm[(i - 3) * files_pwm
1847 data->hwmon_dev = hwmon_device_register(dev);
1848 if (IS_ERR(data->hwmon_dev)) {
1849 err = PTR_ERR(data->hwmon_dev);
1853 /* Watchdog initialization */
1855 /* Register boot notifier */
1856 err = register_reboot_notifier(&watchdog_notifier);
1858 dev_err(&client->dev,
1859 "cannot register reboot notifier (err=%d)\n", err);
1864 * Enable Watchdog registers.
1865 * Set Configuration Register to Enable Watch Dog Registers
1866 * (Bit 2) = XXXX, X1XX.
1868 tmp = w83793_read_value(client, W83793_REG_CONFIG);
1869 w83793_write_value(client, W83793_REG_CONFIG, tmp | 0x04);
1871 /* Set the default watchdog timeout */
1872 data->watchdog_timeout = timeout;
1874 /* Check, if last reboot was caused by watchdog */
1875 data->watchdog_caused_reboot =
1876 w83793_read_value(data->client, W83793_REG_WDT_STATUS) & 0x01;
1878 /* Disable Soft Watchdog during initialiation */
1879 watchdog_disable(data);
1882 * We take the data_mutex lock early so that watchdog_open() cannot
1883 * run when misc_register() has completed, but we've not yet added
1884 * our data to the watchdog_data_list (and set the default timeout)
1886 mutex_lock(&watchdog_data_mutex);
1887 for (i = 0; i < ARRAY_SIZE(watchdog_minors); i++) {
1888 /* Register our watchdog part */
1889 snprintf(data->watchdog_name, sizeof(data->watchdog_name),
1890 "watchdog%c", (i == 0) ? '\0' : ('0' + i));
1891 data->watchdog_miscdev.name = data->watchdog_name;
1892 data->watchdog_miscdev.fops = &watchdog_fops;
1893 data->watchdog_miscdev.minor = watchdog_minors[i];
1895 err = misc_register(&data->watchdog_miscdev);
1899 data->watchdog_miscdev.minor = 0;
1900 dev_err(&client->dev,
1901 "Registering watchdog chardev: %d\n", err);
1905 list_add(&data->list, &watchdog_data_list);
1907 dev_info(&client->dev,
1908 "Registered watchdog chardev major 10, minor: %d\n",
1909 watchdog_minors[i]);
1912 if (i == ARRAY_SIZE(watchdog_minors)) {
1913 data->watchdog_miscdev.minor = 0;
1914 dev_warn(&client->dev,
1915 "Couldn't register watchdog chardev (due to no free minor)\n");
1918 mutex_unlock(&watchdog_data_mutex);
1922 /* Unregister hwmon device */
1926 hwmon_device_unregister(data->hwmon_dev);
1928 /* Unregister sysfs hooks */
1931 for (i = 0; i < ARRAY_SIZE(w83793_sensor_attr_2); i++)
1932 device_remove_file(dev, &w83793_sensor_attr_2[i].dev_attr);
1934 for (i = 0; i < ARRAY_SIZE(sda_single_files); i++)
1935 device_remove_file(dev, &sda_single_files[i].dev_attr);
1937 for (i = 0; i < ARRAY_SIZE(w83793_vid); i++)
1938 device_remove_file(dev, &w83793_vid[i].dev_attr);
1940 for (i = 0; i < ARRAY_SIZE(w83793_left_fan); i++)
1941 device_remove_file(dev, &w83793_left_fan[i].dev_attr);
1943 for (i = 0; i < ARRAY_SIZE(w83793_left_pwm); i++)
1944 device_remove_file(dev, &w83793_left_pwm[i].dev_attr);
1946 for (i = 0; i < ARRAY_SIZE(w83793_temp); i++)
1947 device_remove_file(dev, &w83793_temp[i].dev_attr);
1949 i2c_unregister_device(data->lm75[0]);
1950 i2c_unregister_device(data->lm75[1]);
1957 static void w83793_update_nonvolatile(struct device *dev)
1959 struct i2c_client *client = to_i2c_client(dev);
1960 struct w83793_data *data = i2c_get_clientdata(client);
1963 * They are somewhat "stable" registers, and to update them every time
1964 * takes so much time, it's just not worthy. Update them in a long
1965 * interval to avoid exception.
1967 if (!(time_after(jiffies, data->last_nonvolatile + HZ * 300)
1970 /* update voltage limits */
1971 for (i = 1; i < 3; i++) {
1972 for (j = 0; j < ARRAY_SIZE(data->in); j++) {
1974 w83793_read_value(client, W83793_REG_IN[j][i]);
1976 data->in_low_bits[i] =
1977 w83793_read_value(client, W83793_REG_IN_LOW_BITS[i]);
1980 for (i = 0; i < ARRAY_SIZE(data->fan_min); i++) {
1981 /* Update the Fan measured value and limits */
1982 if (!(data->has_fan & (1 << i)))
1985 w83793_read_value(client, W83793_REG_FAN_MIN(i)) << 8;
1987 w83793_read_value(client, W83793_REG_FAN_MIN(i) + 1);
1990 for (i = 0; i < ARRAY_SIZE(data->temp_fan_map); i++) {
1991 if (!(data->has_temp & (1 << i)))
1993 data->temp_fan_map[i] =
1994 w83793_read_value(client, W83793_REG_TEMP_FAN_MAP(i));
1995 for (j = 1; j < 5; j++) {
1997 w83793_read_value(client, W83793_REG_TEMP[i][j]);
1999 data->temp_cruise[i] =
2000 w83793_read_value(client, W83793_REG_TEMP_CRUISE(i));
2001 for (j = 0; j < 7; j++) {
2002 data->sf2_pwm[i][j] =
2003 w83793_read_value(client, W83793_REG_SF2_PWM(i, j));
2004 data->sf2_temp[i][j] =
2005 w83793_read_value(client,
2006 W83793_REG_SF2_TEMP(i, j));
2010 for (i = 0; i < ARRAY_SIZE(data->temp_mode); i++)
2011 data->temp_mode[i] =
2012 w83793_read_value(client, W83793_REG_TEMP_MODE[i]);
2014 for (i = 0; i < ARRAY_SIZE(data->tolerance); i++) {
2015 data->tolerance[i] =
2016 w83793_read_value(client, W83793_REG_TEMP_TOL(i));
2019 for (i = 0; i < ARRAY_SIZE(data->pwm); i++) {
2020 if (!(data->has_pwm & (1 << i)))
2022 data->pwm[i][PWM_NONSTOP] =
2023 w83793_read_value(client, W83793_REG_PWM(i, PWM_NONSTOP));
2024 data->pwm[i][PWM_START] =
2025 w83793_read_value(client, W83793_REG_PWM(i, PWM_START));
2026 data->pwm_stop_time[i] =
2027 w83793_read_value(client, W83793_REG_PWM_STOP_TIME(i));
2030 data->pwm_default = w83793_read_value(client, W83793_REG_PWM_DEFAULT);
2031 data->pwm_enable = w83793_read_value(client, W83793_REG_PWM_ENABLE);
2032 data->pwm_uptime = w83793_read_value(client, W83793_REG_PWM_UPTIME);
2033 data->pwm_downtime = w83793_read_value(client, W83793_REG_PWM_DOWNTIME);
2034 data->temp_critical =
2035 w83793_read_value(client, W83793_REG_TEMP_CRITICAL);
2036 data->beep_enable = w83793_read_value(client, W83793_REG_OVT_BEEP);
2038 for (i = 0; i < ARRAY_SIZE(data->beeps); i++)
2039 data->beeps[i] = w83793_read_value(client, W83793_REG_BEEP(i));
2041 data->last_nonvolatile = jiffies;
2044 static struct w83793_data *w83793_update_device(struct device *dev)
2046 struct i2c_client *client = to_i2c_client(dev);
2047 struct w83793_data *data = i2c_get_clientdata(client);
2050 mutex_lock(&data->update_lock);
2052 if (!(time_after(jiffies, data->last_updated + HZ * 2)
2056 /* Update the voltages measured value and limits */
2057 for (i = 0; i < ARRAY_SIZE(data->in); i++)
2058 data->in[i][IN_READ] =
2059 w83793_read_value(client, W83793_REG_IN[i][IN_READ]);
2061 data->in_low_bits[IN_READ] =
2062 w83793_read_value(client, W83793_REG_IN_LOW_BITS[IN_READ]);
2064 for (i = 0; i < ARRAY_SIZE(data->fan); i++) {
2065 if (!(data->has_fan & (1 << i)))
2068 w83793_read_value(client, W83793_REG_FAN(i)) << 8;
2070 w83793_read_value(client, W83793_REG_FAN(i) + 1);
2073 for (i = 0; i < ARRAY_SIZE(data->temp); i++) {
2074 if (!(data->has_temp & (1 << i)))
2076 data->temp[i][TEMP_READ] =
2077 w83793_read_value(client, W83793_REG_TEMP[i][TEMP_READ]);
2080 data->temp_low_bits =
2081 w83793_read_value(client, W83793_REG_TEMP_LOW_BITS);
2083 for (i = 0; i < ARRAY_SIZE(data->pwm); i++) {
2084 if (data->has_pwm & (1 << i))
2085 data->pwm[i][PWM_DUTY] =
2086 w83793_read_value(client,
2087 W83793_REG_PWM(i, PWM_DUTY));
2090 for (i = 0; i < ARRAY_SIZE(data->alarms); i++)
2092 w83793_read_value(client, W83793_REG_ALARM(i));
2093 if (data->has_vid & 0x01)
2094 data->vid[0] = w83793_read_value(client, W83793_REG_VID_INA);
2095 if (data->has_vid & 0x02)
2096 data->vid[1] = w83793_read_value(client, W83793_REG_VID_INB);
2097 w83793_update_nonvolatile(dev);
2098 data->last_updated = jiffies;
2102 mutex_unlock(&data->update_lock);
2107 * Ignore the possibility that somebody change bank outside the driver
2108 * Must be called with data->update_lock held, except during initialization
2110 static u8 w83793_read_value(struct i2c_client *client, u16 reg)
2112 struct w83793_data *data = i2c_get_clientdata(client);
2114 u8 new_bank = reg >> 8;
2116 new_bank |= data->bank & 0xfc;
2117 if (data->bank != new_bank) {
2118 if (i2c_smbus_write_byte_data
2119 (client, W83793_REG_BANKSEL, new_bank) >= 0)
2120 data->bank = new_bank;
2122 dev_err(&client->dev,
2123 "set bank to %d failed, fall back "
2124 "to bank %d, read reg 0x%x error\n",
2125 new_bank, data->bank, reg);
2126 res = 0x0; /* read 0x0 from the chip */
2130 res = i2c_smbus_read_byte_data(client, reg & 0xff);
2135 /* Must be called with data->update_lock held, except during initialization */
2136 static int w83793_write_value(struct i2c_client *client, u16 reg, u8 value)
2138 struct w83793_data *data = i2c_get_clientdata(client);
2140 u8 new_bank = reg >> 8;
2142 new_bank |= data->bank & 0xfc;
2143 if (data->bank != new_bank) {
2144 res = i2c_smbus_write_byte_data(client, W83793_REG_BANKSEL,
2147 dev_err(&client->dev,
2148 "set bank to %d failed, fall back "
2149 "to bank %d, write reg 0x%x error\n",
2150 new_bank, data->bank, reg);
2153 data->bank = new_bank;
2156 res = i2c_smbus_write_byte_data(client, reg & 0xff, value);
2161 module_i2c_driver(w83793_driver);
2163 MODULE_AUTHOR("Yuan Mu, Sven Anders");
2164 MODULE_DESCRIPTION("w83793 driver");
2165 MODULE_LICENSE("GPL");