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>
51 #define WATCHDOG_TIMEOUT 2 /* 2 minute default timeout */
53 /* Addresses to scan */
54 static DEFINE_MUTEX(watchdog_mutex);
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 int nowayout = WATCHDOG_NOWAYOUT;
76 module_param(nowayout, int, 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
114 /* only crit and crit_hyst affect real-time alarm status
115 current crit crit_hyst warn warn_hyst */
116 static u16 W83793_REG_TEMP[][5] = {
117 {0x1c, 0x78, 0x79, 0x7a, 0x7b},
118 {0x1d, 0x7c, 0x7d, 0x7e, 0x7f},
119 {0x1e, 0x80, 0x81, 0x82, 0x83},
120 {0x1f, 0x84, 0x85, 0x86, 0x87},
121 {0x20, 0x88, 0x89, 0x8a, 0x8b},
122 {0x21, 0x8c, 0x8d, 0x8e, 0x8f},
125 #define W83793_REG_TEMP_LOW_BITS 0x22
127 #define W83793_REG_BEEP(index) (0x53 + (index))
128 #define W83793_REG_ALARM(index) (0x4b + (index))
130 #define W83793_REG_CLR_CHASSIS 0x4a /* SMI MASK4 */
131 #define W83793_REG_IRQ_CTRL 0x50
132 #define W83793_REG_OVT_CTRL 0x51
133 #define W83793_REG_OVT_BEEP 0x52
138 static const u16 W83793_REG_IN[][3] = {
139 /* Current, High, Low */
140 {0x10, 0x60, 0x61}, /* Vcore A */
141 {0x11, 0x62, 0x63}, /* Vcore B */
142 {0x12, 0x64, 0x65}, /* Vtt */
143 {0x14, 0x6a, 0x6b}, /* VSEN1 */
144 {0x15, 0x6c, 0x6d}, /* VSEN2 */
145 {0x16, 0x6e, 0x6f}, /* +3VSEN */
146 {0x17, 0x70, 0x71}, /* +12VSEN */
147 {0x18, 0x72, 0x73}, /* 5VDD */
148 {0x19, 0x74, 0x75}, /* 5VSB */
149 {0x1a, 0x76, 0x77}, /* VBAT */
152 /* Low Bits of Vcore A/B Vtt Read/High/Low */
153 static const u16 W83793_REG_IN_LOW_BITS[] = { 0x1b, 0x68, 0x69 };
154 static u8 scale_in[] = { 2, 2, 2, 16, 16, 16, 8, 24, 24, 16 };
155 static u8 scale_in_add[] = { 0, 0, 0, 0, 0, 0, 0, 150, 150, 0 };
157 #define W83793_REG_FAN(index) (0x23 + 2 * (index)) /* High byte */
158 #define W83793_REG_FAN_MIN(index) (0x90 + 2 * (index)) /* High byte */
160 #define W83793_REG_PWM_DEFAULT 0xb2
161 #define W83793_REG_PWM_ENABLE 0x207
162 #define W83793_REG_PWM_UPTIME 0xc3 /* Unit in 0.1 second */
163 #define W83793_REG_PWM_DOWNTIME 0xc4 /* Unit in 0.1 second */
164 #define W83793_REG_TEMP_CRITICAL 0xc5
168 #define PWM_NONSTOP 2
169 #define PWM_STOP_TIME 3
170 #define W83793_REG_PWM(index, nr) (((nr) == 0 ? 0xb3 : \
171 (nr) == 1 ? 0x220 : 0x218) + (index))
173 /* bit field, fan1 is bit0, fan2 is bit1 ... */
174 #define W83793_REG_TEMP_FAN_MAP(index) (0x201 + (index))
175 #define W83793_REG_TEMP_TOL(index) (0x208 + (index))
176 #define W83793_REG_TEMP_CRUISE(index) (0x210 + (index))
177 #define W83793_REG_PWM_STOP_TIME(index) (0x228 + (index))
178 #define W83793_REG_SF2_TEMP(index, nr) (0x230 + ((index) << 4) + (nr))
179 #define W83793_REG_SF2_PWM(index, nr) (0x238 + ((index) << 4) + (nr))
181 static inline unsigned long FAN_FROM_REG(u16 val)
183 if ((val >= 0xfff) || (val == 0))
185 return (1350000UL / val);
188 static inline u16 FAN_TO_REG(long rpm)
192 return SENSORS_LIMIT((1350000 + (rpm >> 1)) / rpm, 1, 0xffe);
195 static inline unsigned long TIME_FROM_REG(u8 reg)
200 static inline u8 TIME_TO_REG(unsigned long val)
202 return SENSORS_LIMIT((val + 50) / 100, 0, 0xff);
205 static inline long TEMP_FROM_REG(s8 reg)
210 static inline s8 TEMP_TO_REG(long val, s8 min, s8 max)
212 return SENSORS_LIMIT((val + (val < 0 ? -500 : 500)) / 1000, min, max);
216 struct i2c_client *lm75[2];
217 struct device *hwmon_dev;
218 struct mutex update_lock;
219 char valid; /* !=0 if following fields are valid */
220 unsigned long last_updated; /* In jiffies */
221 unsigned long last_nonvolatile; /* In jiffies, last time we update the
222 nonvolatile registers */
227 u8 in[10][3]; /* Register value, read/high/low */
228 u8 in_low_bits[3]; /* Additional resolution for VCore A/B Vtt */
230 u16 has_fan; /* Only fan1- fan5 has own pins */
231 u16 fan[12]; /* Register value combine */
232 u16 fan_min[12]; /* Register value combine */
234 s8 temp[6][5]; /* current, crit, crit_hyst,warn, warn_hyst */
235 u8 temp_low_bits; /* Additional resolution TD1-TD4 */
236 u8 temp_mode[2]; /* byte 0: Temp D1-D4 mode each has 2 bits
237 byte 1: Temp R1,R2 mode, each has 1 bit */
238 u8 temp_critical; /* If reached all fan will be at full speed */
239 u8 temp_fan_map[6]; /* Temp controls which pwm fan, bit field */
244 u8 pwm_enable; /* Register value, each Temp has 1 bit */
245 u8 pwm_uptime; /* Register value */
246 u8 pwm_downtime; /* Register value */
247 u8 pwm_default; /* All fan default pwm, next poweron valid */
248 u8 pwm[8][3]; /* Register value */
252 u8 alarms[5]; /* realtime status registers */
255 u8 tolerance[3]; /* Temp tolerance(Smart Fan I/II) */
256 u8 sf2_pwm[6][7]; /* Smart FanII: Fan duty cycle */
257 u8 sf2_temp[6][7]; /* Smart FanII: Temp level point */
260 struct i2c_client *client;
261 struct mutex watchdog_lock;
262 struct list_head list; /* member of the watchdog_data_list */
264 struct miscdevice watchdog_miscdev;
265 unsigned long watchdog_is_open;
266 char watchdog_expect_close;
267 char watchdog_name[10]; /* must be unique to avoid sysfs conflict */
268 unsigned int watchdog_caused_reboot;
269 int watchdog_timeout; /* watchdog timeout in minutes */
272 /* Somewhat ugly :( global data pointer list with all devices, so that
273 we can find our device data as when using misc_register. There is no
274 other method to get to one's device data from the open file-op and
275 for usage in the reboot notifier callback. */
276 static LIST_HEAD(watchdog_data_list);
278 /* Note this lock not only protect list access, but also data.kref access */
279 static DEFINE_MUTEX(watchdog_data_mutex);
281 /* Release our data struct when we're detached from the i2c client *and* all
282 references to our watchdog device are released */
283 static void w83793_release_resources(struct kref *ref)
285 struct w83793_data *data = container_of(ref, struct w83793_data, kref);
289 static u8 w83793_read_value(struct i2c_client *client, u16 reg);
290 static int w83793_write_value(struct i2c_client *client, u16 reg, u8 value);
291 static int w83793_probe(struct i2c_client *client,
292 const struct i2c_device_id *id);
293 static int w83793_detect(struct i2c_client *client,
294 struct i2c_board_info *info);
295 static int w83793_remove(struct i2c_client *client);
296 static void w83793_init_client(struct i2c_client *client);
297 static void w83793_update_nonvolatile(struct device *dev);
298 static struct w83793_data *w83793_update_device(struct device *dev);
300 static const struct i2c_device_id w83793_id[] = {
304 MODULE_DEVICE_TABLE(i2c, w83793_id);
306 static struct i2c_driver w83793_driver = {
307 .class = I2C_CLASS_HWMON,
311 .probe = w83793_probe,
312 .remove = w83793_remove,
313 .id_table = w83793_id,
314 .detect = w83793_detect,
315 .address_list = normal_i2c,
319 show_vrm(struct device *dev, struct device_attribute *attr, char *buf)
321 struct w83793_data *data = dev_get_drvdata(dev);
322 return sprintf(buf, "%d\n", data->vrm);
326 show_vid(struct device *dev, struct device_attribute *attr, char *buf)
328 struct w83793_data *data = w83793_update_device(dev);
329 struct sensor_device_attribute_2 *sensor_attr =
330 to_sensor_dev_attr_2(attr);
331 int index = sensor_attr->index;
333 return sprintf(buf, "%d\n", vid_from_reg(data->vid[index], data->vrm));
337 store_vrm(struct device *dev, struct device_attribute *attr,
338 const char *buf, size_t count)
340 struct w83793_data *data = dev_get_drvdata(dev);
341 data->vrm = simple_strtoul(buf, NULL, 10);
345 #define ALARM_STATUS 0
346 #define BEEP_ENABLE 1
348 show_alarm_beep(struct device *dev, struct device_attribute *attr, char *buf)
350 struct w83793_data *data = w83793_update_device(dev);
351 struct sensor_device_attribute_2 *sensor_attr =
352 to_sensor_dev_attr_2(attr);
353 int nr = sensor_attr->nr;
354 int index = sensor_attr->index >> 3;
355 int bit = sensor_attr->index & 0x07;
358 if (ALARM_STATUS == nr) {
359 val = (data->alarms[index] >> (bit)) & 1;
360 } else { /* BEEP_ENABLE */
361 val = (data->beeps[index] >> (bit)) & 1;
364 return sprintf(buf, "%u\n", val);
368 store_beep(struct device *dev, struct device_attribute *attr,
369 const char *buf, size_t count)
371 struct i2c_client *client = to_i2c_client(dev);
372 struct w83793_data *data = i2c_get_clientdata(client);
373 struct sensor_device_attribute_2 *sensor_attr =
374 to_sensor_dev_attr_2(attr);
375 int index = sensor_attr->index >> 3;
376 int shift = sensor_attr->index & 0x07;
377 u8 beep_bit = 1 << shift;
380 val = simple_strtoul(buf, NULL, 10);
381 if (val != 0 && val != 1)
384 mutex_lock(&data->update_lock);
385 data->beeps[index] = w83793_read_value(client, W83793_REG_BEEP(index));
386 data->beeps[index] &= ~beep_bit;
387 data->beeps[index] |= val << shift;
388 w83793_write_value(client, W83793_REG_BEEP(index), data->beeps[index]);
389 mutex_unlock(&data->update_lock);
395 show_beep_enable(struct device *dev, struct device_attribute *attr, char *buf)
397 struct w83793_data *data = w83793_update_device(dev);
398 return sprintf(buf, "%u\n", (data->beep_enable >> 1) & 0x01);
402 store_beep_enable(struct device *dev, struct device_attribute *attr,
403 const char *buf, size_t count)
405 struct i2c_client *client = to_i2c_client(dev);
406 struct w83793_data *data = i2c_get_clientdata(client);
407 u8 val = simple_strtoul(buf, NULL, 10);
409 if (val != 0 && val != 1)
412 mutex_lock(&data->update_lock);
413 data->beep_enable = w83793_read_value(client, W83793_REG_OVT_BEEP)
415 data->beep_enable |= val << 1;
416 w83793_write_value(client, W83793_REG_OVT_BEEP, data->beep_enable);
417 mutex_unlock(&data->update_lock);
422 /* Write any value to clear chassis alarm */
424 store_chassis_clear_legacy(struct device *dev,
425 struct device_attribute *attr, const char *buf,
428 struct i2c_client *client = to_i2c_client(dev);
429 struct w83793_data *data = i2c_get_clientdata(client);
432 dev_warn(dev, "Attribute chassis is deprecated, "
433 "use intrusion0_alarm instead\n");
435 mutex_lock(&data->update_lock);
436 val = w83793_read_value(client, W83793_REG_CLR_CHASSIS);
438 w83793_write_value(client, W83793_REG_CLR_CHASSIS, val);
439 mutex_unlock(&data->update_lock);
443 /* Write 0 to clear chassis alarm */
445 store_chassis_clear(struct device *dev,
446 struct device_attribute *attr, const char *buf,
449 struct i2c_client *client = to_i2c_client(dev);
450 struct w83793_data *data = i2c_get_clientdata(client);
454 if (strict_strtoul(buf, 10, &val) || val != 0)
457 mutex_lock(&data->update_lock);
458 reg = w83793_read_value(client, W83793_REG_CLR_CHASSIS);
459 w83793_write_value(client, W83793_REG_CLR_CHASSIS, reg | 0x80);
460 data->valid = 0; /* Force cache refresh */
461 mutex_unlock(&data->update_lock);
468 show_fan(struct device *dev, struct device_attribute *attr, char *buf)
470 struct sensor_device_attribute_2 *sensor_attr =
471 to_sensor_dev_attr_2(attr);
472 int nr = sensor_attr->nr;
473 int index = sensor_attr->index;
474 struct w83793_data *data = w83793_update_device(dev);
477 if (FAN_INPUT == nr) {
478 val = data->fan[index] & 0x0fff;
480 val = data->fan_min[index] & 0x0fff;
483 return sprintf(buf, "%lu\n", FAN_FROM_REG(val));
487 store_fan_min(struct device *dev, struct device_attribute *attr,
488 const char *buf, size_t count)
490 struct sensor_device_attribute_2 *sensor_attr =
491 to_sensor_dev_attr_2(attr);
492 int index = sensor_attr->index;
493 struct i2c_client *client = to_i2c_client(dev);
494 struct w83793_data *data = i2c_get_clientdata(client);
495 u16 val = FAN_TO_REG(simple_strtoul(buf, NULL, 10));
497 mutex_lock(&data->update_lock);
498 data->fan_min[index] = val;
499 w83793_write_value(client, W83793_REG_FAN_MIN(index),
501 w83793_write_value(client, W83793_REG_FAN_MIN(index) + 1, val & 0xff);
502 mutex_unlock(&data->update_lock);
508 show_pwm(struct device *dev, struct device_attribute *attr, char *buf)
510 struct sensor_device_attribute_2 *sensor_attr =
511 to_sensor_dev_attr_2(attr);
512 struct w83793_data *data = w83793_update_device(dev);
514 int nr = sensor_attr->nr;
515 int index = sensor_attr->index;
517 if (PWM_STOP_TIME == nr)
518 val = TIME_FROM_REG(data->pwm_stop_time[index]);
520 val = (data->pwm[index][nr] & 0x3f) << 2;
522 return sprintf(buf, "%d\n", val);
526 store_pwm(struct device *dev, struct device_attribute *attr,
527 const char *buf, size_t count)
529 struct i2c_client *client = to_i2c_client(dev);
530 struct w83793_data *data = i2c_get_clientdata(client);
531 struct sensor_device_attribute_2 *sensor_attr =
532 to_sensor_dev_attr_2(attr);
533 int nr = sensor_attr->nr;
534 int index = sensor_attr->index;
537 mutex_lock(&data->update_lock);
538 if (PWM_STOP_TIME == nr) {
539 val = TIME_TO_REG(simple_strtoul(buf, NULL, 10));
540 data->pwm_stop_time[index] = val;
541 w83793_write_value(client, W83793_REG_PWM_STOP_TIME(index),
544 val = SENSORS_LIMIT(simple_strtoul(buf, NULL, 10), 0, 0xff)
546 data->pwm[index][nr] =
547 w83793_read_value(client, W83793_REG_PWM(index, nr)) & 0xc0;
548 data->pwm[index][nr] |= val;
549 w83793_write_value(client, W83793_REG_PWM(index, nr),
550 data->pwm[index][nr]);
553 mutex_unlock(&data->update_lock);
558 show_temp(struct device *dev, struct device_attribute *attr, char *buf)
560 struct sensor_device_attribute_2 *sensor_attr =
561 to_sensor_dev_attr_2(attr);
562 int nr = sensor_attr->nr;
563 int index = sensor_attr->index;
564 struct w83793_data *data = w83793_update_device(dev);
565 long temp = TEMP_FROM_REG(data->temp[index][nr]);
567 if (TEMP_READ == nr && index < 4) { /* Only TD1-TD4 have low bits */
568 int low = ((data->temp_low_bits >> (index * 2)) & 0x03) * 250;
569 temp += temp > 0 ? low : -low;
571 return sprintf(buf, "%ld\n", temp);
575 store_temp(struct device *dev, struct device_attribute *attr,
576 const char *buf, size_t count)
578 struct sensor_device_attribute_2 *sensor_attr =
579 to_sensor_dev_attr_2(attr);
580 int nr = sensor_attr->nr;
581 int index = sensor_attr->index;
582 struct i2c_client *client = to_i2c_client(dev);
583 struct w83793_data *data = i2c_get_clientdata(client);
584 long tmp = simple_strtol(buf, NULL, 10);
586 mutex_lock(&data->update_lock);
587 data->temp[index][nr] = TEMP_TO_REG(tmp, -128, 127);
588 w83793_write_value(client, W83793_REG_TEMP[index][nr],
589 data->temp[index][nr]);
590 mutex_unlock(&data->update_lock);
596 each has 4 mode:(2 bits)
598 1: Use internal temp sensor(default)
600 3: Use sensor in Intel CPU and get result by PECI
603 each has 2 mode:(1 bit)
604 0: Disable temp sensor monitor
605 1: To enable temp sensors monitor
608 /* 0 disable, 6 PECI */
609 static u8 TO_TEMP_MODE[] = { 0, 0, 0, 6 };
612 show_temp_mode(struct device *dev, struct device_attribute *attr, char *buf)
614 struct w83793_data *data = w83793_update_device(dev);
615 struct sensor_device_attribute_2 *sensor_attr =
616 to_sensor_dev_attr_2(attr);
617 int index = sensor_attr->index;
618 u8 mask = (index < 4) ? 0x03 : 0x01;
619 u8 shift = (index < 4) ? (2 * index) : (index - 4);
621 index = (index < 4) ? 0 : 1;
623 tmp = (data->temp_mode[index] >> shift) & mask;
625 /* for the internal sensor, found out if diode or thermistor */
627 tmp = index == 0 ? 3 : 4;
629 tmp = TO_TEMP_MODE[tmp];
632 return sprintf(buf, "%d\n", tmp);
636 store_temp_mode(struct device *dev, struct device_attribute *attr,
637 const char *buf, size_t count)
639 struct i2c_client *client = to_i2c_client(dev);
640 struct w83793_data *data = i2c_get_clientdata(client);
641 struct sensor_device_attribute_2 *sensor_attr =
642 to_sensor_dev_attr_2(attr);
643 int index = sensor_attr->index;
644 u8 mask = (index < 4) ? 0x03 : 0x01;
645 u8 shift = (index < 4) ? (2 * index) : (index - 4);
646 u8 val = simple_strtoul(buf, NULL, 10);
648 /* transform the sysfs interface values into table above */
649 if ((val == 6) && (index < 4)) {
651 } else if ((val == 3 && index < 4)
652 || (val == 4 && index >= 4)) {
653 /* transform diode or thermistor into internal enable */
659 index = (index < 4) ? 0 : 1;
660 mutex_lock(&data->update_lock);
661 data->temp_mode[index] =
662 w83793_read_value(client, W83793_REG_TEMP_MODE[index]);
663 data->temp_mode[index] &= ~(mask << shift);
664 data->temp_mode[index] |= val << shift;
665 w83793_write_value(client, W83793_REG_TEMP_MODE[index],
666 data->temp_mode[index]);
667 mutex_unlock(&data->update_lock);
672 #define SETUP_PWM_DEFAULT 0
673 #define SETUP_PWM_UPTIME 1 /* Unit in 0.1s */
674 #define SETUP_PWM_DOWNTIME 2 /* Unit in 0.1s */
675 #define SETUP_TEMP_CRITICAL 3
677 show_sf_setup(struct device *dev, struct device_attribute *attr, char *buf)
679 struct sensor_device_attribute_2 *sensor_attr =
680 to_sensor_dev_attr_2(attr);
681 int nr = sensor_attr->nr;
682 struct w83793_data *data = w83793_update_device(dev);
685 if (SETUP_PWM_DEFAULT == nr) {
686 val = (data->pwm_default & 0x3f) << 2;
687 } else if (SETUP_PWM_UPTIME == nr) {
688 val = TIME_FROM_REG(data->pwm_uptime);
689 } else if (SETUP_PWM_DOWNTIME == nr) {
690 val = TIME_FROM_REG(data->pwm_downtime);
691 } else if (SETUP_TEMP_CRITICAL == nr) {
692 val = TEMP_FROM_REG(data->temp_critical & 0x7f);
695 return sprintf(buf, "%d\n", val);
699 store_sf_setup(struct device *dev, struct device_attribute *attr,
700 const char *buf, size_t count)
702 struct sensor_device_attribute_2 *sensor_attr =
703 to_sensor_dev_attr_2(attr);
704 int nr = sensor_attr->nr;
705 struct i2c_client *client = to_i2c_client(dev);
706 struct w83793_data *data = i2c_get_clientdata(client);
708 mutex_lock(&data->update_lock);
709 if (SETUP_PWM_DEFAULT == nr) {
711 w83793_read_value(client, W83793_REG_PWM_DEFAULT) & 0xc0;
712 data->pwm_default |= SENSORS_LIMIT(simple_strtoul(buf, NULL,
715 w83793_write_value(client, W83793_REG_PWM_DEFAULT,
717 } else if (SETUP_PWM_UPTIME == nr) {
718 data->pwm_uptime = TIME_TO_REG(simple_strtoul(buf, NULL, 10));
719 data->pwm_uptime += data->pwm_uptime == 0 ? 1 : 0;
720 w83793_write_value(client, W83793_REG_PWM_UPTIME,
722 } else if (SETUP_PWM_DOWNTIME == nr) {
723 data->pwm_downtime = TIME_TO_REG(simple_strtoul(buf, NULL, 10));
724 data->pwm_downtime += data->pwm_downtime == 0 ? 1 : 0;
725 w83793_write_value(client, W83793_REG_PWM_DOWNTIME,
727 } else { /* SETUP_TEMP_CRITICAL */
728 data->temp_critical =
729 w83793_read_value(client, W83793_REG_TEMP_CRITICAL) & 0x80;
730 data->temp_critical |= TEMP_TO_REG(simple_strtol(buf, NULL, 10),
732 w83793_write_value(client, W83793_REG_TEMP_CRITICAL,
733 data->temp_critical);
736 mutex_unlock(&data->update_lock);
741 Temp SmartFan control
743 Temp channel control which pwm fan, bitfield, bit 0 indicate pwm1...
744 It's possible two or more temp channels control the same fan, w83793
745 always prefers to pick the most critical request and applies it to
747 It's possible one fan is not in any mapping of 6 temp channels, this
748 means the fan is manual mode
751 Each temp channel has its own SmartFan mode, and temp channel
752 control fans that are set by TEMP_FAN_MAP
754 1: Thermal Cruise Mode
757 Target temperature in thermal cruise mode, w83793 will try to turn
758 fan speed to keep the temperature of target device around this
762 If Temp higher or lower than target with this tolerance, w83793
763 will take actions to speed up or slow down the fan to keep the
764 temperature within the tolerance range.
767 #define TEMP_FAN_MAP 0
768 #define TEMP_PWM_ENABLE 1
769 #define TEMP_CRUISE 2
770 #define TEMP_TOLERANCE 3
772 show_sf_ctrl(struct device *dev, struct device_attribute *attr, char *buf)
774 struct sensor_device_attribute_2 *sensor_attr =
775 to_sensor_dev_attr_2(attr);
776 int nr = sensor_attr->nr;
777 int index = sensor_attr->index;
778 struct w83793_data *data = w83793_update_device(dev);
781 if (TEMP_FAN_MAP == nr) {
782 val = data->temp_fan_map[index];
783 } else if (TEMP_PWM_ENABLE == nr) {
784 /* +2 to transfrom into 2 and 3 to conform with sysfs intf */
785 val = ((data->pwm_enable >> index) & 0x01) + 2;
786 } else if (TEMP_CRUISE == nr) {
787 val = TEMP_FROM_REG(data->temp_cruise[index] & 0x7f);
788 } else { /* TEMP_TOLERANCE */
789 val = data->tolerance[index >> 1] >> ((index & 0x01) ? 4 : 0);
790 val = TEMP_FROM_REG(val & 0x0f);
792 return sprintf(buf, "%d\n", val);
796 store_sf_ctrl(struct device *dev, struct device_attribute *attr,
797 const char *buf, size_t count)
799 struct sensor_device_attribute_2 *sensor_attr =
800 to_sensor_dev_attr_2(attr);
801 int nr = sensor_attr->nr;
802 int index = sensor_attr->index;
803 struct i2c_client *client = to_i2c_client(dev);
804 struct w83793_data *data = i2c_get_clientdata(client);
807 mutex_lock(&data->update_lock);
808 if (TEMP_FAN_MAP == nr) {
809 val = simple_strtoul(buf, NULL, 10) & 0xff;
810 w83793_write_value(client, W83793_REG_TEMP_FAN_MAP(index), val);
811 data->temp_fan_map[index] = val;
812 } else if (TEMP_PWM_ENABLE == nr) {
813 val = simple_strtoul(buf, NULL, 10);
814 if (2 == val || 3 == val) {
816 w83793_read_value(client, W83793_REG_PWM_ENABLE);
818 data->pwm_enable |= 1 << index;
820 data->pwm_enable &= ~(1 << index);
821 w83793_write_value(client, W83793_REG_PWM_ENABLE,
824 mutex_unlock(&data->update_lock);
827 } else if (TEMP_CRUISE == nr) {
828 data->temp_cruise[index] =
829 w83793_read_value(client, W83793_REG_TEMP_CRUISE(index));
830 val = TEMP_TO_REG(simple_strtol(buf, NULL, 10), 0, 0x7f);
831 data->temp_cruise[index] &= 0x80;
832 data->temp_cruise[index] |= val;
834 w83793_write_value(client, W83793_REG_TEMP_CRUISE(index),
835 data->temp_cruise[index]);
836 } else { /* TEMP_TOLERANCE */
838 u8 shift = (index & 0x01) ? 4 : 0;
840 w83793_read_value(client, W83793_REG_TEMP_TOL(i));
842 val = TEMP_TO_REG(simple_strtol(buf, NULL, 10), 0, 0x0f);
843 data->tolerance[i] &= ~(0x0f << shift);
844 data->tolerance[i] |= val << shift;
845 w83793_write_value(client, W83793_REG_TEMP_TOL(i),
849 mutex_unlock(&data->update_lock);
854 show_sf2_pwm(struct device *dev, struct device_attribute *attr, char *buf)
856 struct sensor_device_attribute_2 *sensor_attr =
857 to_sensor_dev_attr_2(attr);
858 int nr = sensor_attr->nr;
859 int index = sensor_attr->index;
860 struct w83793_data *data = w83793_update_device(dev);
862 return sprintf(buf, "%d\n", (data->sf2_pwm[index][nr] & 0x3f) << 2);
866 store_sf2_pwm(struct device *dev, struct device_attribute *attr,
867 const char *buf, size_t count)
869 struct i2c_client *client = to_i2c_client(dev);
870 struct w83793_data *data = i2c_get_clientdata(client);
871 struct sensor_device_attribute_2 *sensor_attr =
872 to_sensor_dev_attr_2(attr);
873 int nr = sensor_attr->nr;
874 int index = sensor_attr->index;
875 u8 val = SENSORS_LIMIT(simple_strtoul(buf, NULL, 10), 0, 0xff) >> 2;
877 mutex_lock(&data->update_lock);
878 data->sf2_pwm[index][nr] =
879 w83793_read_value(client, W83793_REG_SF2_PWM(index, nr)) & 0xc0;
880 data->sf2_pwm[index][nr] |= val;
881 w83793_write_value(client, W83793_REG_SF2_PWM(index, nr),
882 data->sf2_pwm[index][nr]);
883 mutex_unlock(&data->update_lock);
888 show_sf2_temp(struct device *dev, struct device_attribute *attr, char *buf)
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;
894 struct w83793_data *data = w83793_update_device(dev);
896 return sprintf(buf, "%ld\n",
897 TEMP_FROM_REG(data->sf2_temp[index][nr] & 0x7f));
901 store_sf2_temp(struct device *dev, struct device_attribute *attr,
902 const char *buf, size_t count)
904 struct i2c_client *client = to_i2c_client(dev);
905 struct w83793_data *data = i2c_get_clientdata(client);
906 struct sensor_device_attribute_2 *sensor_attr =
907 to_sensor_dev_attr_2(attr);
908 int nr = sensor_attr->nr;
909 int index = sensor_attr->index;
910 u8 val = TEMP_TO_REG(simple_strtol(buf, NULL, 10), 0, 0x7f);
912 mutex_lock(&data->update_lock);
913 data->sf2_temp[index][nr] =
914 w83793_read_value(client, W83793_REG_SF2_TEMP(index, nr)) & 0x80;
915 data->sf2_temp[index][nr] |= val;
916 w83793_write_value(client, W83793_REG_SF2_TEMP(index, nr),
917 data->sf2_temp[index][nr]);
918 mutex_unlock(&data->update_lock);
922 /* only Vcore A/B and Vtt have additional 2 bits precision */
924 show_in(struct device *dev, struct device_attribute *attr, char *buf)
926 struct sensor_device_attribute_2 *sensor_attr =
927 to_sensor_dev_attr_2(attr);
928 int nr = sensor_attr->nr;
929 int index = sensor_attr->index;
930 struct w83793_data *data = w83793_update_device(dev);
931 u16 val = data->in[index][nr];
935 val += (data->in_low_bits[nr] >> (index * 2)) & 0x3;
937 /* voltage inputs 5VDD and 5VSB needs 150mV offset */
938 val = val * scale_in[index] + scale_in_add[index];
939 return sprintf(buf, "%d\n", val);
943 store_in(struct device *dev, struct device_attribute *attr,
944 const char *buf, size_t count)
946 struct sensor_device_attribute_2 *sensor_attr =
947 to_sensor_dev_attr_2(attr);
948 int nr = sensor_attr->nr;
949 int index = sensor_attr->index;
950 struct i2c_client *client = to_i2c_client(dev);
951 struct w83793_data *data = i2c_get_clientdata(client);
955 (simple_strtoul(buf, NULL, 10) +
956 scale_in[index] / 2) / scale_in[index];
957 mutex_lock(&data->update_lock);
959 /* fix the limit values of 5VDD and 5VSB to ALARM mechanism */
960 if (1 == nr || 2 == nr) {
961 val -= scale_in_add[index] / scale_in[index];
963 val = SENSORS_LIMIT(val, 0, 255);
965 val = SENSORS_LIMIT(val, 0, 0x3FF);
966 data->in_low_bits[nr] =
967 w83793_read_value(client, W83793_REG_IN_LOW_BITS[nr]);
968 data->in_low_bits[nr] &= ~(0x03 << (2 * index));
969 data->in_low_bits[nr] |= (val & 0x03) << (2 * index);
970 w83793_write_value(client, W83793_REG_IN_LOW_BITS[nr],
971 data->in_low_bits[nr]);
974 data->in[index][nr] = val;
975 w83793_write_value(client, W83793_REG_IN[index][nr],
976 data->in[index][nr]);
977 mutex_unlock(&data->update_lock);
983 #define SENSOR_ATTR_IN(index) \
984 SENSOR_ATTR_2(in##index##_input, S_IRUGO, show_in, NULL, \
986 SENSOR_ATTR_2(in##index##_max, S_IRUGO | S_IWUSR, show_in, \
987 store_in, IN_MAX, index), \
988 SENSOR_ATTR_2(in##index##_min, S_IRUGO | S_IWUSR, show_in, \
989 store_in, IN_LOW, index), \
990 SENSOR_ATTR_2(in##index##_alarm, S_IRUGO, show_alarm_beep, \
991 NULL, ALARM_STATUS, index + ((index > 2) ? 1 : 0)), \
992 SENSOR_ATTR_2(in##index##_beep, S_IWUSR | S_IRUGO, \
993 show_alarm_beep, store_beep, BEEP_ENABLE, \
994 index + ((index > 2) ? 1 : 0))
996 #define SENSOR_ATTR_FAN(index) \
997 SENSOR_ATTR_2(fan##index##_alarm, S_IRUGO, show_alarm_beep, \
998 NULL, ALARM_STATUS, index + 17), \
999 SENSOR_ATTR_2(fan##index##_beep, S_IWUSR | S_IRUGO, \
1000 show_alarm_beep, store_beep, BEEP_ENABLE, index + 17), \
1001 SENSOR_ATTR_2(fan##index##_input, S_IRUGO, show_fan, \
1002 NULL, FAN_INPUT, index - 1), \
1003 SENSOR_ATTR_2(fan##index##_min, S_IWUSR | S_IRUGO, \
1004 show_fan, store_fan_min, FAN_MIN, index - 1)
1006 #define SENSOR_ATTR_PWM(index) \
1007 SENSOR_ATTR_2(pwm##index, S_IWUSR | S_IRUGO, show_pwm, \
1008 store_pwm, PWM_DUTY, index - 1), \
1009 SENSOR_ATTR_2(pwm##index##_nonstop, S_IWUSR | S_IRUGO, \
1010 show_pwm, store_pwm, PWM_NONSTOP, index - 1), \
1011 SENSOR_ATTR_2(pwm##index##_start, S_IWUSR | S_IRUGO, \
1012 show_pwm, store_pwm, PWM_START, index - 1), \
1013 SENSOR_ATTR_2(pwm##index##_stop_time, S_IWUSR | S_IRUGO, \
1014 show_pwm, store_pwm, PWM_STOP_TIME, index - 1)
1016 #define SENSOR_ATTR_TEMP(index) \
1017 SENSOR_ATTR_2(temp##index##_type, S_IRUGO | S_IWUSR, \
1018 show_temp_mode, store_temp_mode, NOT_USED, index - 1), \
1019 SENSOR_ATTR_2(temp##index##_input, S_IRUGO, show_temp, \
1020 NULL, TEMP_READ, index - 1), \
1021 SENSOR_ATTR_2(temp##index##_max, S_IRUGO | S_IWUSR, show_temp, \
1022 store_temp, TEMP_CRIT, index - 1), \
1023 SENSOR_ATTR_2(temp##index##_max_hyst, S_IRUGO | S_IWUSR, \
1024 show_temp, store_temp, TEMP_CRIT_HYST, index - 1), \
1025 SENSOR_ATTR_2(temp##index##_warn, S_IRUGO | S_IWUSR, show_temp, \
1026 store_temp, TEMP_WARN, index - 1), \
1027 SENSOR_ATTR_2(temp##index##_warn_hyst, S_IRUGO | S_IWUSR, \
1028 show_temp, store_temp, TEMP_WARN_HYST, index - 1), \
1029 SENSOR_ATTR_2(temp##index##_alarm, S_IRUGO, \
1030 show_alarm_beep, NULL, ALARM_STATUS, index + 11), \
1031 SENSOR_ATTR_2(temp##index##_beep, S_IWUSR | S_IRUGO, \
1032 show_alarm_beep, store_beep, BEEP_ENABLE, index + 11), \
1033 SENSOR_ATTR_2(temp##index##_auto_channels_pwm, \
1034 S_IRUGO | S_IWUSR, show_sf_ctrl, store_sf_ctrl, \
1035 TEMP_FAN_MAP, index - 1), \
1036 SENSOR_ATTR_2(temp##index##_pwm_enable, S_IWUSR | S_IRUGO, \
1037 show_sf_ctrl, store_sf_ctrl, TEMP_PWM_ENABLE, \
1039 SENSOR_ATTR_2(thermal_cruise##index, S_IRUGO | S_IWUSR, \
1040 show_sf_ctrl, store_sf_ctrl, TEMP_CRUISE, index - 1), \
1041 SENSOR_ATTR_2(tolerance##index, S_IRUGO | S_IWUSR, show_sf_ctrl,\
1042 store_sf_ctrl, TEMP_TOLERANCE, index - 1), \
1043 SENSOR_ATTR_2(temp##index##_auto_point1_pwm, S_IRUGO | S_IWUSR, \
1044 show_sf2_pwm, store_sf2_pwm, 0, index - 1), \
1045 SENSOR_ATTR_2(temp##index##_auto_point2_pwm, S_IRUGO | S_IWUSR, \
1046 show_sf2_pwm, store_sf2_pwm, 1, index - 1), \
1047 SENSOR_ATTR_2(temp##index##_auto_point3_pwm, S_IRUGO | S_IWUSR, \
1048 show_sf2_pwm, store_sf2_pwm, 2, index - 1), \
1049 SENSOR_ATTR_2(temp##index##_auto_point4_pwm, S_IRUGO | S_IWUSR, \
1050 show_sf2_pwm, store_sf2_pwm, 3, index - 1), \
1051 SENSOR_ATTR_2(temp##index##_auto_point5_pwm, S_IRUGO | S_IWUSR, \
1052 show_sf2_pwm, store_sf2_pwm, 4, index - 1), \
1053 SENSOR_ATTR_2(temp##index##_auto_point6_pwm, S_IRUGO | S_IWUSR, \
1054 show_sf2_pwm, store_sf2_pwm, 5, index - 1), \
1055 SENSOR_ATTR_2(temp##index##_auto_point7_pwm, S_IRUGO | S_IWUSR, \
1056 show_sf2_pwm, store_sf2_pwm, 6, index - 1), \
1057 SENSOR_ATTR_2(temp##index##_auto_point1_temp, S_IRUGO | S_IWUSR,\
1058 show_sf2_temp, store_sf2_temp, 0, index - 1), \
1059 SENSOR_ATTR_2(temp##index##_auto_point2_temp, S_IRUGO | S_IWUSR,\
1060 show_sf2_temp, store_sf2_temp, 1, index - 1), \
1061 SENSOR_ATTR_2(temp##index##_auto_point3_temp, S_IRUGO | S_IWUSR,\
1062 show_sf2_temp, store_sf2_temp, 2, index - 1), \
1063 SENSOR_ATTR_2(temp##index##_auto_point4_temp, S_IRUGO | S_IWUSR,\
1064 show_sf2_temp, store_sf2_temp, 3, index - 1), \
1065 SENSOR_ATTR_2(temp##index##_auto_point5_temp, S_IRUGO | S_IWUSR,\
1066 show_sf2_temp, store_sf2_temp, 4, index - 1), \
1067 SENSOR_ATTR_2(temp##index##_auto_point6_temp, S_IRUGO | S_IWUSR,\
1068 show_sf2_temp, store_sf2_temp, 5, index - 1), \
1069 SENSOR_ATTR_2(temp##index##_auto_point7_temp, S_IRUGO | S_IWUSR,\
1070 show_sf2_temp, store_sf2_temp, 6, index - 1)
1072 static struct sensor_device_attribute_2 w83793_sensor_attr_2[] = {
1093 static struct sensor_device_attribute_2 w83793_temp[] = {
1094 SENSOR_ATTR_TEMP(1),
1095 SENSOR_ATTR_TEMP(2),
1096 SENSOR_ATTR_TEMP(3),
1097 SENSOR_ATTR_TEMP(4),
1098 SENSOR_ATTR_TEMP(5),
1099 SENSOR_ATTR_TEMP(6),
1103 static struct sensor_device_attribute_2 w83793_left_fan[] = {
1108 SENSOR_ATTR_FAN(10),
1109 SENSOR_ATTR_FAN(11),
1110 SENSOR_ATTR_FAN(12),
1114 static struct sensor_device_attribute_2 w83793_left_pwm[] = {
1122 static struct sensor_device_attribute_2 w83793_vid[] = {
1123 SENSOR_ATTR_2(cpu0_vid, S_IRUGO, show_vid, NULL, NOT_USED, 0),
1124 SENSOR_ATTR_2(cpu1_vid, S_IRUGO, show_vid, NULL, NOT_USED, 1),
1126 static DEVICE_ATTR(vrm, S_IWUSR | S_IRUGO, show_vrm, store_vrm);
1128 static struct sensor_device_attribute_2 sda_single_files[] = {
1129 SENSOR_ATTR_2(chassis, S_IWUSR | S_IRUGO, show_alarm_beep,
1130 store_chassis_clear_legacy, ALARM_STATUS, 30),
1131 SENSOR_ATTR_2(intrusion0_alarm, S_IWUSR | S_IRUGO, show_alarm_beep,
1132 store_chassis_clear, ALARM_STATUS, 30),
1133 SENSOR_ATTR_2(beep_enable, S_IWUSR | S_IRUGO, show_beep_enable,
1134 store_beep_enable, NOT_USED, NOT_USED),
1135 SENSOR_ATTR_2(pwm_default, S_IWUSR | S_IRUGO, show_sf_setup,
1136 store_sf_setup, SETUP_PWM_DEFAULT, NOT_USED),
1137 SENSOR_ATTR_2(pwm_uptime, S_IWUSR | S_IRUGO, show_sf_setup,
1138 store_sf_setup, SETUP_PWM_UPTIME, NOT_USED),
1139 SENSOR_ATTR_2(pwm_downtime, S_IWUSR | S_IRUGO, show_sf_setup,
1140 store_sf_setup, SETUP_PWM_DOWNTIME, NOT_USED),
1141 SENSOR_ATTR_2(temp_critical, S_IWUSR | S_IRUGO, show_sf_setup,
1142 store_sf_setup, SETUP_TEMP_CRITICAL, NOT_USED),
1145 static void w83793_init_client(struct i2c_client *client)
1148 w83793_write_value(client, W83793_REG_CONFIG, 0x80);
1151 /* Start monitoring */
1152 w83793_write_value(client, W83793_REG_CONFIG,
1153 w83793_read_value(client, W83793_REG_CONFIG) | 0x01);
1160 static int watchdog_set_timeout(struct w83793_data *data, int timeout)
1164 mtimeout = DIV_ROUND_UP(timeout, 60);
1169 mutex_lock(&data->watchdog_lock);
1170 if (!data->client) {
1175 data->watchdog_timeout = mtimeout;
1177 /* Set Timeout value (in Minutes) */
1178 w83793_write_value(data->client, W83793_REG_WDT_TIMEOUT,
1179 data->watchdog_timeout);
1181 ret = mtimeout * 60;
1184 mutex_unlock(&data->watchdog_lock);
1188 static int watchdog_get_timeout(struct w83793_data *data)
1192 mutex_lock(&data->watchdog_lock);
1193 timeout = data->watchdog_timeout * 60;
1194 mutex_unlock(&data->watchdog_lock);
1199 static int watchdog_trigger(struct w83793_data *data)
1203 mutex_lock(&data->watchdog_lock);
1204 if (!data->client) {
1209 /* Set Timeout value (in Minutes) */
1210 w83793_write_value(data->client, W83793_REG_WDT_TIMEOUT,
1211 data->watchdog_timeout);
1214 mutex_unlock(&data->watchdog_lock);
1218 static int watchdog_enable(struct w83793_data *data)
1222 mutex_lock(&data->watchdog_lock);
1223 if (!data->client) {
1228 /* Set initial timeout */
1229 w83793_write_value(data->client, W83793_REG_WDT_TIMEOUT,
1230 data->watchdog_timeout);
1232 /* Enable Soft Watchdog */
1233 w83793_write_value(data->client, W83793_REG_WDT_LOCK, 0x55);
1236 mutex_unlock(&data->watchdog_lock);
1240 static int watchdog_disable(struct w83793_data *data)
1244 mutex_lock(&data->watchdog_lock);
1245 if (!data->client) {
1250 /* Disable Soft Watchdog */
1251 w83793_write_value(data->client, W83793_REG_WDT_LOCK, 0xAA);
1254 mutex_unlock(&data->watchdog_lock);
1258 static int watchdog_open(struct inode *inode, struct file *filp)
1260 struct w83793_data *pos, *data = NULL;
1261 int watchdog_is_open;
1263 /* We get called from drivers/char/misc.c with misc_mtx hold, and we
1264 call misc_register() from w83793_probe() with watchdog_data_mutex
1265 hold, as misc_register() takes the misc_mtx lock, this is a possible
1266 deadlock, so we use mutex_trylock here. */
1267 if (!mutex_trylock(&watchdog_data_mutex))
1268 return -ERESTARTSYS;
1269 list_for_each_entry(pos, &watchdog_data_list, list) {
1270 if (pos->watchdog_miscdev.minor == iminor(inode)) {
1276 /* Check, if device is already open */
1277 watchdog_is_open = test_and_set_bit(0, &data->watchdog_is_open);
1279 /* Increase data reference counter (if not already done).
1280 Note we can never not have found data, so we don't check for this */
1281 if (!watchdog_is_open)
1282 kref_get(&data->kref);
1284 mutex_unlock(&watchdog_data_mutex);
1286 /* Check, if device is already open and possibly issue error */
1287 if (watchdog_is_open)
1290 /* Enable Soft Watchdog */
1291 watchdog_enable(data);
1293 /* Store pointer to data into filp's private data */
1294 filp->private_data = data;
1296 return nonseekable_open(inode, filp);
1299 static int watchdog_close(struct inode *inode, struct file *filp)
1301 struct w83793_data *data = filp->private_data;
1303 if (data->watchdog_expect_close) {
1304 watchdog_disable(data);
1305 data->watchdog_expect_close = 0;
1307 watchdog_trigger(data);
1308 dev_crit(&data->client->dev,
1309 "unexpected close, not stopping watchdog!\n");
1312 clear_bit(0, &data->watchdog_is_open);
1314 /* Decrease data reference counter */
1315 mutex_lock(&watchdog_data_mutex);
1316 kref_put(&data->kref, w83793_release_resources);
1317 mutex_unlock(&watchdog_data_mutex);
1322 static ssize_t watchdog_write(struct file *filp, const char __user *buf,
1323 size_t count, loff_t *offset)
1326 struct w83793_data *data = filp->private_data;
1332 /* Clear it in case it was set with a previous write */
1333 data->watchdog_expect_close = 0;
1335 for (i = 0; i != count; i++) {
1337 if (get_user(c, buf + i))
1340 data->watchdog_expect_close = 1;
1343 ret = watchdog_trigger(data);
1350 static long watchdog_ioctl(struct file *filp, unsigned int cmd,
1353 static struct watchdog_info ident = {
1354 .options = WDIOF_KEEPALIVEPING |
1357 .identity = "w83793 watchdog"
1361 struct w83793_data *data = filp->private_data;
1363 mutex_lock(&watchdog_mutex);
1365 case WDIOC_GETSUPPORT:
1367 ident.options |= WDIOF_MAGICCLOSE;
1368 if (copy_to_user((void __user *)arg, &ident, sizeof(ident)))
1372 case WDIOC_GETSTATUS:
1373 val = data->watchdog_caused_reboot ? WDIOF_CARDRESET : 0;
1374 ret = put_user(val, (int __user *)arg);
1377 case WDIOC_GETBOOTSTATUS:
1378 ret = put_user(0, (int __user *)arg);
1381 case WDIOC_KEEPALIVE:
1382 ret = watchdog_trigger(data);
1385 case WDIOC_GETTIMEOUT:
1386 val = watchdog_get_timeout(data);
1387 ret = put_user(val, (int __user *)arg);
1390 case WDIOC_SETTIMEOUT:
1391 if (get_user(val, (int __user *)arg)) {
1395 ret = watchdog_set_timeout(data, val);
1397 ret = put_user(ret, (int __user *)arg);
1400 case WDIOC_SETOPTIONS:
1401 if (get_user(val, (int __user *)arg)) {
1406 if (val & WDIOS_DISABLECARD)
1407 ret = watchdog_disable(data);
1408 else if (val & WDIOS_ENABLECARD)
1409 ret = watchdog_enable(data);
1417 mutex_unlock(&watchdog_mutex);
1421 static const struct file_operations watchdog_fops = {
1422 .owner = THIS_MODULE,
1423 .llseek = no_llseek,
1424 .open = watchdog_open,
1425 .release = watchdog_close,
1426 .write = watchdog_write,
1427 .unlocked_ioctl = watchdog_ioctl,
1431 * Notifier for system down
1434 static int watchdog_notify_sys(struct notifier_block *this, unsigned long code,
1437 struct w83793_data *data = NULL;
1439 if (code == SYS_DOWN || code == SYS_HALT) {
1441 /* Disable each registered watchdog */
1442 mutex_lock(&watchdog_data_mutex);
1443 list_for_each_entry(data, &watchdog_data_list, list) {
1444 if (data->watchdog_miscdev.minor)
1445 watchdog_disable(data);
1447 mutex_unlock(&watchdog_data_mutex);
1454 * The WDT needs to learn about soft shutdowns in order to
1455 * turn the timebomb registers off.
1458 static struct notifier_block watchdog_notifier = {
1459 .notifier_call = watchdog_notify_sys,
1463 * Init / remove routines
1466 static int w83793_remove(struct i2c_client *client)
1468 struct w83793_data *data = i2c_get_clientdata(client);
1469 struct device *dev = &client->dev;
1472 /* Unregister the watchdog (if registered) */
1473 if (data->watchdog_miscdev.minor) {
1474 misc_deregister(&data->watchdog_miscdev);
1476 if (data->watchdog_is_open) {
1477 dev_warn(&client->dev,
1478 "i2c client detached with watchdog open! "
1479 "Stopping watchdog.\n");
1480 watchdog_disable(data);
1483 mutex_lock(&watchdog_data_mutex);
1484 list_del(&data->list);
1485 mutex_unlock(&watchdog_data_mutex);
1487 /* Tell the watchdog code the client is gone */
1488 mutex_lock(&data->watchdog_lock);
1489 data->client = NULL;
1490 mutex_unlock(&data->watchdog_lock);
1493 /* Reset Configuration Register to Disable Watch Dog Registers */
1494 tmp = w83793_read_value(client, W83793_REG_CONFIG);
1495 w83793_write_value(client, W83793_REG_CONFIG, tmp & ~0x04);
1497 unregister_reboot_notifier(&watchdog_notifier);
1499 hwmon_device_unregister(data->hwmon_dev);
1501 for (i = 0; i < ARRAY_SIZE(w83793_sensor_attr_2); i++)
1502 device_remove_file(dev,
1503 &w83793_sensor_attr_2[i].dev_attr);
1505 for (i = 0; i < ARRAY_SIZE(sda_single_files); i++)
1506 device_remove_file(dev, &sda_single_files[i].dev_attr);
1508 for (i = 0; i < ARRAY_SIZE(w83793_vid); i++)
1509 device_remove_file(dev, &w83793_vid[i].dev_attr);
1510 device_remove_file(dev, &dev_attr_vrm);
1512 for (i = 0; i < ARRAY_SIZE(w83793_left_fan); i++)
1513 device_remove_file(dev, &w83793_left_fan[i].dev_attr);
1515 for (i = 0; i < ARRAY_SIZE(w83793_left_pwm); i++)
1516 device_remove_file(dev, &w83793_left_pwm[i].dev_attr);
1518 for (i = 0; i < ARRAY_SIZE(w83793_temp); i++)
1519 device_remove_file(dev, &w83793_temp[i].dev_attr);
1521 if (data->lm75[0] != NULL)
1522 i2c_unregister_device(data->lm75[0]);
1523 if (data->lm75[1] != NULL)
1524 i2c_unregister_device(data->lm75[1]);
1526 /* Decrease data reference counter */
1527 mutex_lock(&watchdog_data_mutex);
1528 kref_put(&data->kref, w83793_release_resources);
1529 mutex_unlock(&watchdog_data_mutex);
1535 w83793_detect_subclients(struct i2c_client *client)
1538 int address = client->addr;
1540 struct i2c_adapter *adapter = client->adapter;
1541 struct w83793_data *data = i2c_get_clientdata(client);
1543 id = i2c_adapter_id(adapter);
1544 if (force_subclients[0] == id && force_subclients[1] == address) {
1545 for (i = 2; i <= 3; i++) {
1546 if (force_subclients[i] < 0x48
1547 || force_subclients[i] > 0x4f) {
1548 dev_err(&client->dev,
1549 "invalid subclient "
1550 "address %d; must be 0x48-0x4f\n",
1551 force_subclients[i]);
1556 w83793_write_value(client, W83793_REG_I2C_SUBADDR,
1557 (force_subclients[2] & 0x07) |
1558 ((force_subclients[3] & 0x07) << 4));
1561 tmp = w83793_read_value(client, W83793_REG_I2C_SUBADDR);
1562 if (!(tmp & 0x08)) {
1563 data->lm75[0] = i2c_new_dummy(adapter, 0x48 + (tmp & 0x7));
1565 if (!(tmp & 0x80)) {
1566 if ((data->lm75[0] != NULL)
1567 && ((tmp & 0x7) == ((tmp >> 4) & 0x7))) {
1568 dev_err(&client->dev,
1569 "duplicate addresses 0x%x, "
1570 "use force_subclients\n", data->lm75[0]->addr);
1574 data->lm75[1] = i2c_new_dummy(adapter,
1575 0x48 + ((tmp >> 4) & 0x7));
1580 /* Undo inits in case of errors */
1583 if (data->lm75[0] != NULL)
1584 i2c_unregister_device(data->lm75[0]);
1589 /* Return 0 if detection is successful, -ENODEV otherwise */
1590 static int w83793_detect(struct i2c_client *client,
1591 struct i2c_board_info *info)
1593 u8 tmp, bank, chip_id;
1594 struct i2c_adapter *adapter = client->adapter;
1595 unsigned short address = client->addr;
1597 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA)) {
1601 bank = i2c_smbus_read_byte_data(client, W83793_REG_BANKSEL);
1603 tmp = bank & 0x80 ? 0x5c : 0xa3;
1604 /* Check Winbond vendor ID */
1605 if (tmp != i2c_smbus_read_byte_data(client, W83793_REG_VENDORID)) {
1606 pr_debug("w83793: Detection failed at check vendor id\n");
1610 /* If Winbond chip, address of chip and W83793_REG_I2C_ADDR
1612 if ((bank & 0x07) == 0
1613 && i2c_smbus_read_byte_data(client, W83793_REG_I2C_ADDR) !=
1615 pr_debug("w83793: Detection failed at check i2c addr\n");
1619 /* Determine the chip type now */
1620 chip_id = i2c_smbus_read_byte_data(client, W83793_REG_CHIPID);
1621 if (chip_id != 0x7b)
1624 strlcpy(info->type, "w83793", I2C_NAME_SIZE);
1629 static int w83793_probe(struct i2c_client *client,
1630 const struct i2c_device_id *id)
1632 struct device *dev = &client->dev;
1633 const int watchdog_minors[] = { WATCHDOG_MINOR, 212, 213, 214, 215 };
1634 struct w83793_data *data;
1635 int i, tmp, val, err;
1636 int files_fan = ARRAY_SIZE(w83793_left_fan) / 7;
1637 int files_pwm = ARRAY_SIZE(w83793_left_pwm) / 5;
1638 int files_temp = ARRAY_SIZE(w83793_temp) / 6;
1640 data = kzalloc(sizeof(struct w83793_data), GFP_KERNEL);
1646 i2c_set_clientdata(client, data);
1647 data->bank = i2c_smbus_read_byte_data(client, W83793_REG_BANKSEL);
1648 mutex_init(&data->update_lock);
1649 mutex_init(&data->watchdog_lock);
1650 INIT_LIST_HEAD(&data->list);
1651 kref_init(&data->kref);
1653 /* Store client pointer in our data struct for watchdog usage
1654 (where the client is found through a data ptr instead of the
1656 data->client = client;
1658 err = w83793_detect_subclients(client);
1662 /* Initialize the chip */
1663 w83793_init_client(client);
1666 Only fan 1-5 has their own input pins,
1667 Pwm 1-3 has their own pins
1669 data->has_fan = 0x1f;
1670 data->has_pwm = 0x07;
1671 tmp = w83793_read_value(client, W83793_REG_MFC);
1672 val = w83793_read_value(client, W83793_REG_FANIN_CTRL);
1674 /* check the function of pins 49-56 */
1676 data->has_vid |= 0x2; /* has VIDB */
1678 data->has_pwm |= 0x18; /* pwm 4,5 */
1679 if (val & 0x01) { /* fan 6 */
1680 data->has_fan |= 0x20;
1681 data->has_pwm |= 0x20;
1683 if (val & 0x02) { /* fan 7 */
1684 data->has_fan |= 0x40;
1685 data->has_pwm |= 0x40;
1687 if (!(tmp & 0x40) && (val & 0x04)) { /* fan 8 */
1688 data->has_fan |= 0x80;
1689 data->has_pwm |= 0x80;
1693 /* check the function of pins 37-40 */
1695 data->has_vid |= 0x1; /* has VIDA */
1696 if (0x08 == (tmp & 0x0c)) {
1697 if (val & 0x08) /* fan 9 */
1698 data->has_fan |= 0x100;
1699 if (val & 0x10) /* fan 10 */
1700 data->has_fan |= 0x200;
1702 if (0x20 == (tmp & 0x30)) {
1703 if (val & 0x20) /* fan 11 */
1704 data->has_fan |= 0x400;
1705 if (val & 0x40) /* fan 12 */
1706 data->has_fan |= 0x800;
1709 if ((tmp & 0x01) && (val & 0x04)) { /* fan 8, second location */
1710 data->has_fan |= 0x80;
1711 data->has_pwm |= 0x80;
1714 tmp = w83793_read_value(client, W83793_REG_FANIN_SEL);
1715 if ((tmp & 0x01) && (val & 0x08)) { /* fan 9, second location */
1716 data->has_fan |= 0x100;
1718 if ((tmp & 0x02) && (val & 0x10)) { /* fan 10, second location */
1719 data->has_fan |= 0x200;
1721 if ((tmp & 0x04) && (val & 0x20)) { /* fan 11, second location */
1722 data->has_fan |= 0x400;
1724 if ((tmp & 0x08) && (val & 0x40)) { /* fan 12, second location */
1725 data->has_fan |= 0x800;
1728 /* check the temp1-6 mode, ignore former AMDSI selected inputs */
1729 tmp = w83793_read_value(client,W83793_REG_TEMP_MODE[0]);
1731 data->has_temp |= 0x01;
1733 data->has_temp |= 0x02;
1735 data->has_temp |= 0x04;
1737 data->has_temp |= 0x08;
1739 tmp = w83793_read_value(client,W83793_REG_TEMP_MODE[1]);
1741 data->has_temp |= 0x10;
1743 data->has_temp |= 0x20;
1745 /* Register sysfs hooks */
1746 for (i = 0; i < ARRAY_SIZE(w83793_sensor_attr_2); i++) {
1747 err = device_create_file(dev,
1748 &w83793_sensor_attr_2[i].dev_attr);
1753 for (i = 0; i < ARRAY_SIZE(w83793_vid); i++) {
1754 if (!(data->has_vid & (1 << i)))
1756 err = device_create_file(dev, &w83793_vid[i].dev_attr);
1760 if (data->has_vid) {
1761 data->vrm = vid_which_vrm();
1762 err = device_create_file(dev, &dev_attr_vrm);
1767 for (i = 0; i < ARRAY_SIZE(sda_single_files); i++) {
1768 err = device_create_file(dev, &sda_single_files[i].dev_attr);
1774 for (i = 0; i < 6; i++) {
1776 if (!(data->has_temp & (1 << i)))
1778 for (j = 0; j < files_temp; j++) {
1779 err = device_create_file(dev,
1780 &w83793_temp[(i) * files_temp
1787 for (i = 5; i < 12; i++) {
1789 if (!(data->has_fan & (1 << i)))
1791 for (j = 0; j < files_fan; j++) {
1792 err = device_create_file(dev,
1793 &w83793_left_fan[(i - 5) * files_fan
1800 for (i = 3; i < 8; i++) {
1802 if (!(data->has_pwm & (1 << i)))
1804 for (j = 0; j < files_pwm; j++) {
1805 err = device_create_file(dev,
1806 &w83793_left_pwm[(i - 3) * files_pwm
1813 data->hwmon_dev = hwmon_device_register(dev);
1814 if (IS_ERR(data->hwmon_dev)) {
1815 err = PTR_ERR(data->hwmon_dev);
1819 /* Watchdog initialization */
1821 /* Register boot notifier */
1822 err = register_reboot_notifier(&watchdog_notifier);
1824 dev_err(&client->dev,
1825 "cannot register reboot notifier (err=%d)\n", err);
1829 /* Enable Watchdog registers.
1830 Set Configuration Register to Enable Watch Dog Registers
1831 (Bit 2) = XXXX, X1XX. */
1832 tmp = w83793_read_value(client, W83793_REG_CONFIG);
1833 w83793_write_value(client, W83793_REG_CONFIG, tmp | 0x04);
1835 /* Set the default watchdog timeout */
1836 data->watchdog_timeout = timeout;
1838 /* Check, if last reboot was caused by watchdog */
1839 data->watchdog_caused_reboot =
1840 w83793_read_value(data->client, W83793_REG_WDT_STATUS) & 0x01;
1842 /* Disable Soft Watchdog during initialiation */
1843 watchdog_disable(data);
1845 /* We take the data_mutex lock early so that watchdog_open() cannot
1846 run when misc_register() has completed, but we've not yet added
1847 our data to the watchdog_data_list (and set the default timeout) */
1848 mutex_lock(&watchdog_data_mutex);
1849 for (i = 0; i < ARRAY_SIZE(watchdog_minors); i++) {
1850 /* Register our watchdog part */
1851 snprintf(data->watchdog_name, sizeof(data->watchdog_name),
1852 "watchdog%c", (i == 0) ? '\0' : ('0' + i));
1853 data->watchdog_miscdev.name = data->watchdog_name;
1854 data->watchdog_miscdev.fops = &watchdog_fops;
1855 data->watchdog_miscdev.minor = watchdog_minors[i];
1857 err = misc_register(&data->watchdog_miscdev);
1861 data->watchdog_miscdev.minor = 0;
1862 dev_err(&client->dev,
1863 "Registering watchdog chardev: %d\n", err);
1867 list_add(&data->list, &watchdog_data_list);
1869 dev_info(&client->dev,
1870 "Registered watchdog chardev major 10, minor: %d\n",
1871 watchdog_minors[i]);
1874 if (i == ARRAY_SIZE(watchdog_minors)) {
1875 data->watchdog_miscdev.minor = 0;
1876 dev_warn(&client->dev, "Couldn't register watchdog chardev "
1877 "(due to no free minor)\n");
1880 mutex_unlock(&watchdog_data_mutex);
1884 /* Unregister hwmon device */
1888 hwmon_device_unregister(data->hwmon_dev);
1890 /* Unregister sysfs hooks */
1893 for (i = 0; i < ARRAY_SIZE(w83793_sensor_attr_2); i++)
1894 device_remove_file(dev, &w83793_sensor_attr_2[i].dev_attr);
1896 for (i = 0; i < ARRAY_SIZE(sda_single_files); i++)
1897 device_remove_file(dev, &sda_single_files[i].dev_attr);
1899 for (i = 0; i < ARRAY_SIZE(w83793_vid); i++)
1900 device_remove_file(dev, &w83793_vid[i].dev_attr);
1902 for (i = 0; i < ARRAY_SIZE(w83793_left_fan); i++)
1903 device_remove_file(dev, &w83793_left_fan[i].dev_attr);
1905 for (i = 0; i < ARRAY_SIZE(w83793_left_pwm); i++)
1906 device_remove_file(dev, &w83793_left_pwm[i].dev_attr);
1908 for (i = 0; i < ARRAY_SIZE(w83793_temp); i++)
1909 device_remove_file(dev, &w83793_temp[i].dev_attr);
1911 if (data->lm75[0] != NULL)
1912 i2c_unregister_device(data->lm75[0]);
1913 if (data->lm75[1] != NULL)
1914 i2c_unregister_device(data->lm75[1]);
1921 static void w83793_update_nonvolatile(struct device *dev)
1923 struct i2c_client *client = to_i2c_client(dev);
1924 struct w83793_data *data = i2c_get_clientdata(client);
1927 They are somewhat "stable" registers, and to update them everytime
1928 takes so much time, it's just not worthy. Update them in a long
1929 interval to avoid exception.
1931 if (!(time_after(jiffies, data->last_nonvolatile + HZ * 300)
1934 /* update voltage limits */
1935 for (i = 1; i < 3; i++) {
1936 for (j = 0; j < ARRAY_SIZE(data->in); j++) {
1938 w83793_read_value(client, W83793_REG_IN[j][i]);
1940 data->in_low_bits[i] =
1941 w83793_read_value(client, W83793_REG_IN_LOW_BITS[i]);
1944 for (i = 0; i < ARRAY_SIZE(data->fan_min); i++) {
1945 /* Update the Fan measured value and limits */
1946 if (!(data->has_fan & (1 << i))) {
1950 w83793_read_value(client, W83793_REG_FAN_MIN(i)) << 8;
1952 w83793_read_value(client, W83793_REG_FAN_MIN(i) + 1);
1955 for (i = 0; i < ARRAY_SIZE(data->temp_fan_map); i++) {
1956 if (!(data->has_temp & (1 << i)))
1958 data->temp_fan_map[i] =
1959 w83793_read_value(client, W83793_REG_TEMP_FAN_MAP(i));
1960 for (j = 1; j < 5; j++) {
1962 w83793_read_value(client, W83793_REG_TEMP[i][j]);
1964 data->temp_cruise[i] =
1965 w83793_read_value(client, W83793_REG_TEMP_CRUISE(i));
1966 for (j = 0; j < 7; j++) {
1967 data->sf2_pwm[i][j] =
1968 w83793_read_value(client, W83793_REG_SF2_PWM(i, j));
1969 data->sf2_temp[i][j] =
1970 w83793_read_value(client,
1971 W83793_REG_SF2_TEMP(i, j));
1975 for (i = 0; i < ARRAY_SIZE(data->temp_mode); i++)
1976 data->temp_mode[i] =
1977 w83793_read_value(client, W83793_REG_TEMP_MODE[i]);
1979 for (i = 0; i < ARRAY_SIZE(data->tolerance); i++) {
1980 data->tolerance[i] =
1981 w83793_read_value(client, W83793_REG_TEMP_TOL(i));
1984 for (i = 0; i < ARRAY_SIZE(data->pwm); i++) {
1985 if (!(data->has_pwm & (1 << i)))
1987 data->pwm[i][PWM_NONSTOP] =
1988 w83793_read_value(client, W83793_REG_PWM(i, PWM_NONSTOP));
1989 data->pwm[i][PWM_START] =
1990 w83793_read_value(client, W83793_REG_PWM(i, PWM_START));
1991 data->pwm_stop_time[i] =
1992 w83793_read_value(client, W83793_REG_PWM_STOP_TIME(i));
1995 data->pwm_default = w83793_read_value(client, W83793_REG_PWM_DEFAULT);
1996 data->pwm_enable = w83793_read_value(client, W83793_REG_PWM_ENABLE);
1997 data->pwm_uptime = w83793_read_value(client, W83793_REG_PWM_UPTIME);
1998 data->pwm_downtime = w83793_read_value(client, W83793_REG_PWM_DOWNTIME);
1999 data->temp_critical =
2000 w83793_read_value(client, W83793_REG_TEMP_CRITICAL);
2001 data->beep_enable = w83793_read_value(client, W83793_REG_OVT_BEEP);
2003 for (i = 0; i < ARRAY_SIZE(data->beeps); i++) {
2004 data->beeps[i] = w83793_read_value(client, W83793_REG_BEEP(i));
2007 data->last_nonvolatile = jiffies;
2010 static struct w83793_data *w83793_update_device(struct device *dev)
2012 struct i2c_client *client = to_i2c_client(dev);
2013 struct w83793_data *data = i2c_get_clientdata(client);
2016 mutex_lock(&data->update_lock);
2018 if (!(time_after(jiffies, data->last_updated + HZ * 2)
2022 /* Update the voltages measured value and limits */
2023 for (i = 0; i < ARRAY_SIZE(data->in); i++)
2024 data->in[i][IN_READ] =
2025 w83793_read_value(client, W83793_REG_IN[i][IN_READ]);
2027 data->in_low_bits[IN_READ] =
2028 w83793_read_value(client, W83793_REG_IN_LOW_BITS[IN_READ]);
2030 for (i = 0; i < ARRAY_SIZE(data->fan); i++) {
2031 if (!(data->has_fan & (1 << i))) {
2035 w83793_read_value(client, W83793_REG_FAN(i)) << 8;
2037 w83793_read_value(client, W83793_REG_FAN(i) + 1);
2040 for (i = 0; i < ARRAY_SIZE(data->temp); i++) {
2041 if (!(data->has_temp & (1 << i)))
2043 data->temp[i][TEMP_READ] =
2044 w83793_read_value(client, W83793_REG_TEMP[i][TEMP_READ]);
2047 data->temp_low_bits =
2048 w83793_read_value(client, W83793_REG_TEMP_LOW_BITS);
2050 for (i = 0; i < ARRAY_SIZE(data->pwm); i++) {
2051 if (data->has_pwm & (1 << i))
2052 data->pwm[i][PWM_DUTY] =
2053 w83793_read_value(client,
2054 W83793_REG_PWM(i, PWM_DUTY));
2057 for (i = 0; i < ARRAY_SIZE(data->alarms); i++)
2059 w83793_read_value(client, W83793_REG_ALARM(i));
2060 if (data->has_vid & 0x01)
2061 data->vid[0] = w83793_read_value(client, W83793_REG_VID_INA);
2062 if (data->has_vid & 0x02)
2063 data->vid[1] = w83793_read_value(client, W83793_REG_VID_INB);
2064 w83793_update_nonvolatile(dev);
2065 data->last_updated = jiffies;
2069 mutex_unlock(&data->update_lock);
2073 /* Ignore the possibility that somebody change bank outside the driver
2074 Must be called with data->update_lock held, except during initialization */
2075 static u8 w83793_read_value(struct i2c_client *client, u16 reg)
2077 struct w83793_data *data = i2c_get_clientdata(client);
2079 u8 new_bank = reg >> 8;
2081 new_bank |= data->bank & 0xfc;
2082 if (data->bank != new_bank) {
2083 if (i2c_smbus_write_byte_data
2084 (client, W83793_REG_BANKSEL, new_bank) >= 0)
2085 data->bank = new_bank;
2087 dev_err(&client->dev,
2088 "set bank to %d failed, fall back "
2089 "to bank %d, read reg 0x%x error\n",
2090 new_bank, data->bank, reg);
2091 res = 0x0; /* read 0x0 from the chip */
2095 res = i2c_smbus_read_byte_data(client, reg & 0xff);
2100 /* Must be called with data->update_lock held, except during initialization */
2101 static int w83793_write_value(struct i2c_client *client, u16 reg, u8 value)
2103 struct w83793_data *data = i2c_get_clientdata(client);
2105 u8 new_bank = reg >> 8;
2107 new_bank |= data->bank & 0xfc;
2108 if (data->bank != new_bank) {
2109 if ((res = i2c_smbus_write_byte_data
2110 (client, W83793_REG_BANKSEL, new_bank)) >= 0)
2111 data->bank = new_bank;
2113 dev_err(&client->dev,
2114 "set bank to %d failed, fall back "
2115 "to bank %d, write reg 0x%x error\n",
2116 new_bank, data->bank, reg);
2121 res = i2c_smbus_write_byte_data(client, reg & 0xff, value);
2126 static int __init sensors_w83793_init(void)
2128 return i2c_add_driver(&w83793_driver);
2131 static void __exit sensors_w83793_exit(void)
2133 i2c_del_driver(&w83793_driver);
2136 MODULE_AUTHOR("Yuan Mu, Sven Anders");
2137 MODULE_DESCRIPTION("w83793 driver");
2138 MODULE_LICENSE("GPL");
2140 module_init(sensors_w83793_init);
2141 module_exit(sensors_w83793_exit);