Staging: strip: delete the driver
[linux/fpc-iii.git] / drivers / hwmon / w83793.c
blob612807d9715533b3f03afcdf6738fb79767ee55e
1 /*
2 w83793.c - Linux kernel driver for hardware monitoring
3 Copyright (C) 2006 Winbond Electronics Corp.
4 Yuan Mu
5 Rudolf Marek <r.marek@assembler.cz>
6 Copyright (C) 2009-2010 Sven Anders <anders@anduras.de>, ANDURAS AG.
7 Watchdog driver part
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
23 02110-1301 USA.
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>
42 #include <linux/fs.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>
50 /* Default values */
51 #define WATCHDOG_TIMEOUT 2 /* 2 minute default timeout */
53 /* Addresses to scan */
54 static const unsigned short normal_i2c[] = { 0x2c, 0x2d, 0x2e, 0x2f,
55 I2C_CLIENT_END };
57 /* Insmod parameters */
59 static unsigned short force_subclients[4];
60 module_param_array(force_subclients, short, NULL, 0);
61 MODULE_PARM_DESC(force_subclients, "List of subclient addresses: "
62 "{bus, clientaddr, subclientaddr1, subclientaddr2}");
64 static int reset;
65 module_param(reset, bool, 0);
66 MODULE_PARM_DESC(reset, "Set to 1 to reset chip, not recommended");
68 static int timeout = WATCHDOG_TIMEOUT; /* default timeout in minutes */
69 module_param(timeout, int, 0);
70 MODULE_PARM_DESC(timeout,
71 "Watchdog timeout in minutes. 2<= timeout <=255 (default="
72 __MODULE_STRING(WATCHDOG_TIMEOUT) ")");
74 static int nowayout = WATCHDOG_NOWAYOUT;
75 module_param(nowayout, int, 0);
76 MODULE_PARM_DESC(nowayout,
77 "Watchdog cannot be stopped once started (default="
78 __MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
81 Address 0x00, 0x0d, 0x0e, 0x0f in all three banks are reserved
82 as ID, Bank Select registers
84 #define W83793_REG_BANKSEL 0x00
85 #define W83793_REG_VENDORID 0x0d
86 #define W83793_REG_CHIPID 0x0e
87 #define W83793_REG_DEVICEID 0x0f
89 #define W83793_REG_CONFIG 0x40
90 #define W83793_REG_MFC 0x58
91 #define W83793_REG_FANIN_CTRL 0x5c
92 #define W83793_REG_FANIN_SEL 0x5d
93 #define W83793_REG_I2C_ADDR 0x0b
94 #define W83793_REG_I2C_SUBADDR 0x0c
95 #define W83793_REG_VID_INA 0x05
96 #define W83793_REG_VID_INB 0x06
97 #define W83793_REG_VID_LATCHA 0x07
98 #define W83793_REG_VID_LATCHB 0x08
99 #define W83793_REG_VID_CTRL 0x59
101 #define W83793_REG_WDT_LOCK 0x01
102 #define W83793_REG_WDT_ENABLE 0x02
103 #define W83793_REG_WDT_STATUS 0x03
104 #define W83793_REG_WDT_TIMEOUT 0x04
106 static u16 W83793_REG_TEMP_MODE[2] = { 0x5e, 0x5f };
108 #define TEMP_READ 0
109 #define TEMP_CRIT 1
110 #define TEMP_CRIT_HYST 2
111 #define TEMP_WARN 3
112 #define TEMP_WARN_HYST 4
113 /* only crit and crit_hyst affect real-time alarm status
114 current crit crit_hyst warn warn_hyst */
115 static u16 W83793_REG_TEMP[][5] = {
116 {0x1c, 0x78, 0x79, 0x7a, 0x7b},
117 {0x1d, 0x7c, 0x7d, 0x7e, 0x7f},
118 {0x1e, 0x80, 0x81, 0x82, 0x83},
119 {0x1f, 0x84, 0x85, 0x86, 0x87},
120 {0x20, 0x88, 0x89, 0x8a, 0x8b},
121 {0x21, 0x8c, 0x8d, 0x8e, 0x8f},
124 #define W83793_REG_TEMP_LOW_BITS 0x22
126 #define W83793_REG_BEEP(index) (0x53 + (index))
127 #define W83793_REG_ALARM(index) (0x4b + (index))
129 #define W83793_REG_CLR_CHASSIS 0x4a /* SMI MASK4 */
130 #define W83793_REG_IRQ_CTRL 0x50
131 #define W83793_REG_OVT_CTRL 0x51
132 #define W83793_REG_OVT_BEEP 0x52
134 #define IN_READ 0
135 #define IN_MAX 1
136 #define IN_LOW 2
137 static const u16 W83793_REG_IN[][3] = {
138 /* Current, High, Low */
139 {0x10, 0x60, 0x61}, /* Vcore A */
140 {0x11, 0x62, 0x63}, /* Vcore B */
141 {0x12, 0x64, 0x65}, /* Vtt */
142 {0x14, 0x6a, 0x6b}, /* VSEN1 */
143 {0x15, 0x6c, 0x6d}, /* VSEN2 */
144 {0x16, 0x6e, 0x6f}, /* +3VSEN */
145 {0x17, 0x70, 0x71}, /* +12VSEN */
146 {0x18, 0x72, 0x73}, /* 5VDD */
147 {0x19, 0x74, 0x75}, /* 5VSB */
148 {0x1a, 0x76, 0x77}, /* VBAT */
151 /* Low Bits of Vcore A/B Vtt Read/High/Low */
152 static const u16 W83793_REG_IN_LOW_BITS[] = { 0x1b, 0x68, 0x69 };
153 static u8 scale_in[] = { 2, 2, 2, 16, 16, 16, 8, 24, 24, 16 };
154 static u8 scale_in_add[] = { 0, 0, 0, 0, 0, 0, 0, 150, 150, 0 };
156 #define W83793_REG_FAN(index) (0x23 + 2 * (index)) /* High byte */
157 #define W83793_REG_FAN_MIN(index) (0x90 + 2 * (index)) /* High byte */
159 #define W83793_REG_PWM_DEFAULT 0xb2
160 #define W83793_REG_PWM_ENABLE 0x207
161 #define W83793_REG_PWM_UPTIME 0xc3 /* Unit in 0.1 second */
162 #define W83793_REG_PWM_DOWNTIME 0xc4 /* Unit in 0.1 second */
163 #define W83793_REG_TEMP_CRITICAL 0xc5
165 #define PWM_DUTY 0
166 #define PWM_START 1
167 #define PWM_NONSTOP 2
168 #define PWM_STOP_TIME 3
169 #define W83793_REG_PWM(index, nr) (((nr) == 0 ? 0xb3 : \
170 (nr) == 1 ? 0x220 : 0x218) + (index))
172 /* bit field, fan1 is bit0, fan2 is bit1 ... */
173 #define W83793_REG_TEMP_FAN_MAP(index) (0x201 + (index))
174 #define W83793_REG_TEMP_TOL(index) (0x208 + (index))
175 #define W83793_REG_TEMP_CRUISE(index) (0x210 + (index))
176 #define W83793_REG_PWM_STOP_TIME(index) (0x228 + (index))
177 #define W83793_REG_SF2_TEMP(index, nr) (0x230 + ((index) << 4) + (nr))
178 #define W83793_REG_SF2_PWM(index, nr) (0x238 + ((index) << 4) + (nr))
180 static inline unsigned long FAN_FROM_REG(u16 val)
182 if ((val >= 0xfff) || (val == 0))
183 return 0;
184 return (1350000UL / val);
187 static inline u16 FAN_TO_REG(long rpm)
189 if (rpm <= 0)
190 return 0x0fff;
191 return SENSORS_LIMIT((1350000 + (rpm >> 1)) / rpm, 1, 0xffe);
194 static inline unsigned long TIME_FROM_REG(u8 reg)
196 return (reg * 100);
199 static inline u8 TIME_TO_REG(unsigned long val)
201 return SENSORS_LIMIT((val + 50) / 100, 0, 0xff);
204 static inline long TEMP_FROM_REG(s8 reg)
206 return (reg * 1000);
209 static inline s8 TEMP_TO_REG(long val, s8 min, s8 max)
211 return SENSORS_LIMIT((val + (val < 0 ? -500 : 500)) / 1000, min, max);
214 struct w83793_data {
215 struct i2c_client *lm75[2];
216 struct device *hwmon_dev;
217 struct mutex update_lock;
218 char valid; /* !=0 if following fields are valid */
219 unsigned long last_updated; /* In jiffies */
220 unsigned long last_nonvolatile; /* In jiffies, last time we update the
221 nonvolatile registers */
223 u8 bank;
224 u8 vrm;
225 u8 vid[2];
226 u8 in[10][3]; /* Register value, read/high/low */
227 u8 in_low_bits[3]; /* Additional resolution for VCore A/B Vtt */
229 u16 has_fan; /* Only fan1- fan5 has own pins */
230 u16 fan[12]; /* Register value combine */
231 u16 fan_min[12]; /* Register value combine */
233 s8 temp[6][5]; /* current, crit, crit_hyst,warn, warn_hyst */
234 u8 temp_low_bits; /* Additional resolution TD1-TD4 */
235 u8 temp_mode[2]; /* byte 0: Temp D1-D4 mode each has 2 bits
236 byte 1: Temp R1,R2 mode, each has 1 bit */
237 u8 temp_critical; /* If reached all fan will be at full speed */
238 u8 temp_fan_map[6]; /* Temp controls which pwm fan, bit field */
240 u8 has_pwm;
241 u8 has_temp;
242 u8 has_vid;
243 u8 pwm_enable; /* Register value, each Temp has 1 bit */
244 u8 pwm_uptime; /* Register value */
245 u8 pwm_downtime; /* Register value */
246 u8 pwm_default; /* All fan default pwm, next poweron valid */
247 u8 pwm[8][3]; /* Register value */
248 u8 pwm_stop_time[8];
249 u8 temp_cruise[6];
251 u8 alarms[5]; /* realtime status registers */
252 u8 beeps[5];
253 u8 beep_enable;
254 u8 tolerance[3]; /* Temp tolerance(Smart Fan I/II) */
255 u8 sf2_pwm[6][7]; /* Smart FanII: Fan duty cycle */
256 u8 sf2_temp[6][7]; /* Smart FanII: Temp level point */
258 /* watchdog */
259 struct i2c_client *client;
260 struct mutex watchdog_lock;
261 struct list_head list; /* member of the watchdog_data_list */
262 struct kref kref;
263 struct miscdevice watchdog_miscdev;
264 unsigned long watchdog_is_open;
265 char watchdog_expect_close;
266 char watchdog_name[10]; /* must be unique to avoid sysfs conflict */
267 unsigned int watchdog_caused_reboot;
268 int watchdog_timeout; /* watchdog timeout in minutes */
271 /* Somewhat ugly :( global data pointer list with all devices, so that
272 we can find our device data as when using misc_register. There is no
273 other method to get to one's device data from the open file-op and
274 for usage in the reboot notifier callback. */
275 static LIST_HEAD(watchdog_data_list);
277 /* Note this lock not only protect list access, but also data.kref access */
278 static DEFINE_MUTEX(watchdog_data_mutex);
280 /* Release our data struct when we're detached from the i2c client *and* all
281 references to our watchdog device are released */
282 static void w83793_release_resources(struct kref *ref)
284 struct w83793_data *data = container_of(ref, struct w83793_data, kref);
285 kfree(data);
288 static u8 w83793_read_value(struct i2c_client *client, u16 reg);
289 static int w83793_write_value(struct i2c_client *client, u16 reg, u8 value);
290 static int w83793_probe(struct i2c_client *client,
291 const struct i2c_device_id *id);
292 static int w83793_detect(struct i2c_client *client,
293 struct i2c_board_info *info);
294 static int w83793_remove(struct i2c_client *client);
295 static void w83793_init_client(struct i2c_client *client);
296 static void w83793_update_nonvolatile(struct device *dev);
297 static struct w83793_data *w83793_update_device(struct device *dev);
299 static const struct i2c_device_id w83793_id[] = {
300 { "w83793", 0 },
303 MODULE_DEVICE_TABLE(i2c, w83793_id);
305 static struct i2c_driver w83793_driver = {
306 .class = I2C_CLASS_HWMON,
307 .driver = {
308 .name = "w83793",
310 .probe = w83793_probe,
311 .remove = w83793_remove,
312 .id_table = w83793_id,
313 .detect = w83793_detect,
314 .address_list = normal_i2c,
317 static ssize_t
318 show_vrm(struct device *dev, struct device_attribute *attr, char *buf)
320 struct w83793_data *data = dev_get_drvdata(dev);
321 return sprintf(buf, "%d\n", data->vrm);
324 static ssize_t
325 show_vid(struct device *dev, struct device_attribute *attr, char *buf)
327 struct w83793_data *data = w83793_update_device(dev);
328 struct sensor_device_attribute_2 *sensor_attr =
329 to_sensor_dev_attr_2(attr);
330 int index = sensor_attr->index;
332 return sprintf(buf, "%d\n", vid_from_reg(data->vid[index], data->vrm));
335 static ssize_t
336 store_vrm(struct device *dev, struct device_attribute *attr,
337 const char *buf, size_t count)
339 struct w83793_data *data = dev_get_drvdata(dev);
340 data->vrm = simple_strtoul(buf, NULL, 10);
341 return count;
344 #define ALARM_STATUS 0
345 #define BEEP_ENABLE 1
346 static ssize_t
347 show_alarm_beep(struct device *dev, struct device_attribute *attr, char *buf)
349 struct w83793_data *data = w83793_update_device(dev);
350 struct sensor_device_attribute_2 *sensor_attr =
351 to_sensor_dev_attr_2(attr);
352 int nr = sensor_attr->nr;
353 int index = sensor_attr->index >> 3;
354 int bit = sensor_attr->index & 0x07;
355 u8 val;
357 if (ALARM_STATUS == nr) {
358 val = (data->alarms[index] >> (bit)) & 1;
359 } else { /* BEEP_ENABLE */
360 val = (data->beeps[index] >> (bit)) & 1;
363 return sprintf(buf, "%u\n", val);
366 static ssize_t
367 store_beep(struct device *dev, struct device_attribute *attr,
368 const char *buf, size_t count)
370 struct i2c_client *client = to_i2c_client(dev);
371 struct w83793_data *data = i2c_get_clientdata(client);
372 struct sensor_device_attribute_2 *sensor_attr =
373 to_sensor_dev_attr_2(attr);
374 int index = sensor_attr->index >> 3;
375 int shift = sensor_attr->index & 0x07;
376 u8 beep_bit = 1 << shift;
377 u8 val;
379 val = simple_strtoul(buf, NULL, 10);
380 if (val != 0 && val != 1)
381 return -EINVAL;
383 mutex_lock(&data->update_lock);
384 data->beeps[index] = w83793_read_value(client, W83793_REG_BEEP(index));
385 data->beeps[index] &= ~beep_bit;
386 data->beeps[index] |= val << shift;
387 w83793_write_value(client, W83793_REG_BEEP(index), data->beeps[index]);
388 mutex_unlock(&data->update_lock);
390 return count;
393 static ssize_t
394 show_beep_enable(struct device *dev, struct device_attribute *attr, char *buf)
396 struct w83793_data *data = w83793_update_device(dev);
397 return sprintf(buf, "%u\n", (data->beep_enable >> 1) & 0x01);
400 static ssize_t
401 store_beep_enable(struct device *dev, struct device_attribute *attr,
402 const char *buf, size_t count)
404 struct i2c_client *client = to_i2c_client(dev);
405 struct w83793_data *data = i2c_get_clientdata(client);
406 u8 val = simple_strtoul(buf, NULL, 10);
408 if (val != 0 && val != 1)
409 return -EINVAL;
411 mutex_lock(&data->update_lock);
412 data->beep_enable = w83793_read_value(client, W83793_REG_OVT_BEEP)
413 & 0xfd;
414 data->beep_enable |= val << 1;
415 w83793_write_value(client, W83793_REG_OVT_BEEP, data->beep_enable);
416 mutex_unlock(&data->update_lock);
418 return count;
421 /* Write any value to clear chassis alarm */
422 static ssize_t
423 store_chassis_clear(struct device *dev,
424 struct device_attribute *attr, const char *buf,
425 size_t count)
427 struct i2c_client *client = to_i2c_client(dev);
428 struct w83793_data *data = i2c_get_clientdata(client);
429 u8 val;
431 mutex_lock(&data->update_lock);
432 val = w83793_read_value(client, W83793_REG_CLR_CHASSIS);
433 val |= 0x80;
434 w83793_write_value(client, W83793_REG_CLR_CHASSIS, val);
435 mutex_unlock(&data->update_lock);
436 return count;
439 #define FAN_INPUT 0
440 #define FAN_MIN 1
441 static ssize_t
442 show_fan(struct device *dev, struct device_attribute *attr, char *buf)
444 struct sensor_device_attribute_2 *sensor_attr =
445 to_sensor_dev_attr_2(attr);
446 int nr = sensor_attr->nr;
447 int index = sensor_attr->index;
448 struct w83793_data *data = w83793_update_device(dev);
449 u16 val;
451 if (FAN_INPUT == nr) {
452 val = data->fan[index] & 0x0fff;
453 } else {
454 val = data->fan_min[index] & 0x0fff;
457 return sprintf(buf, "%lu\n", FAN_FROM_REG(val));
460 static ssize_t
461 store_fan_min(struct device *dev, struct device_attribute *attr,
462 const char *buf, size_t count)
464 struct sensor_device_attribute_2 *sensor_attr =
465 to_sensor_dev_attr_2(attr);
466 int index = sensor_attr->index;
467 struct i2c_client *client = to_i2c_client(dev);
468 struct w83793_data *data = i2c_get_clientdata(client);
469 u16 val = FAN_TO_REG(simple_strtoul(buf, NULL, 10));
471 mutex_lock(&data->update_lock);
472 data->fan_min[index] = val;
473 w83793_write_value(client, W83793_REG_FAN_MIN(index),
474 (val >> 8) & 0xff);
475 w83793_write_value(client, W83793_REG_FAN_MIN(index) + 1, val & 0xff);
476 mutex_unlock(&data->update_lock);
478 return count;
481 static ssize_t
482 show_pwm(struct device *dev, struct device_attribute *attr, char *buf)
484 struct sensor_device_attribute_2 *sensor_attr =
485 to_sensor_dev_attr_2(attr);
486 struct w83793_data *data = w83793_update_device(dev);
487 u16 val;
488 int nr = sensor_attr->nr;
489 int index = sensor_attr->index;
491 if (PWM_STOP_TIME == nr)
492 val = TIME_FROM_REG(data->pwm_stop_time[index]);
493 else
494 val = (data->pwm[index][nr] & 0x3f) << 2;
496 return sprintf(buf, "%d\n", val);
499 static ssize_t
500 store_pwm(struct device *dev, struct device_attribute *attr,
501 const char *buf, size_t count)
503 struct i2c_client *client = to_i2c_client(dev);
504 struct w83793_data *data = i2c_get_clientdata(client);
505 struct sensor_device_attribute_2 *sensor_attr =
506 to_sensor_dev_attr_2(attr);
507 int nr = sensor_attr->nr;
508 int index = sensor_attr->index;
509 u8 val;
511 mutex_lock(&data->update_lock);
512 if (PWM_STOP_TIME == nr) {
513 val = TIME_TO_REG(simple_strtoul(buf, NULL, 10));
514 data->pwm_stop_time[index] = val;
515 w83793_write_value(client, W83793_REG_PWM_STOP_TIME(index),
516 val);
517 } else {
518 val = SENSORS_LIMIT(simple_strtoul(buf, NULL, 10), 0, 0xff)
519 >> 2;
520 data->pwm[index][nr] =
521 w83793_read_value(client, W83793_REG_PWM(index, nr)) & 0xc0;
522 data->pwm[index][nr] |= val;
523 w83793_write_value(client, W83793_REG_PWM(index, nr),
524 data->pwm[index][nr]);
527 mutex_unlock(&data->update_lock);
528 return count;
531 static ssize_t
532 show_temp(struct device *dev, struct device_attribute *attr, char *buf)
534 struct sensor_device_attribute_2 *sensor_attr =
535 to_sensor_dev_attr_2(attr);
536 int nr = sensor_attr->nr;
537 int index = sensor_attr->index;
538 struct w83793_data *data = w83793_update_device(dev);
539 long temp = TEMP_FROM_REG(data->temp[index][nr]);
541 if (TEMP_READ == nr && index < 4) { /* Only TD1-TD4 have low bits */
542 int low = ((data->temp_low_bits >> (index * 2)) & 0x03) * 250;
543 temp += temp > 0 ? low : -low;
545 return sprintf(buf, "%ld\n", temp);
548 static ssize_t
549 store_temp(struct device *dev, struct device_attribute *attr,
550 const char *buf, size_t count)
552 struct sensor_device_attribute_2 *sensor_attr =
553 to_sensor_dev_attr_2(attr);
554 int nr = sensor_attr->nr;
555 int index = sensor_attr->index;
556 struct i2c_client *client = to_i2c_client(dev);
557 struct w83793_data *data = i2c_get_clientdata(client);
558 long tmp = simple_strtol(buf, NULL, 10);
560 mutex_lock(&data->update_lock);
561 data->temp[index][nr] = TEMP_TO_REG(tmp, -128, 127);
562 w83793_write_value(client, W83793_REG_TEMP[index][nr],
563 data->temp[index][nr]);
564 mutex_unlock(&data->update_lock);
565 return count;
569 TD1-TD4
570 each has 4 mode:(2 bits)
571 0: Stop monitor
572 1: Use internal temp sensor(default)
573 2: Reserved
574 3: Use sensor in Intel CPU and get result by PECI
576 TR1-TR2
577 each has 2 mode:(1 bit)
578 0: Disable temp sensor monitor
579 1: To enable temp sensors monitor
582 /* 0 disable, 6 PECI */
583 static u8 TO_TEMP_MODE[] = { 0, 0, 0, 6 };
585 static ssize_t
586 show_temp_mode(struct device *dev, struct device_attribute *attr, char *buf)
588 struct w83793_data *data = w83793_update_device(dev);
589 struct sensor_device_attribute_2 *sensor_attr =
590 to_sensor_dev_attr_2(attr);
591 int index = sensor_attr->index;
592 u8 mask = (index < 4) ? 0x03 : 0x01;
593 u8 shift = (index < 4) ? (2 * index) : (index - 4);
594 u8 tmp;
595 index = (index < 4) ? 0 : 1;
597 tmp = (data->temp_mode[index] >> shift) & mask;
599 /* for the internal sensor, found out if diode or thermistor */
600 if (tmp == 1) {
601 tmp = index == 0 ? 3 : 4;
602 } else {
603 tmp = TO_TEMP_MODE[tmp];
606 return sprintf(buf, "%d\n", tmp);
609 static ssize_t
610 store_temp_mode(struct device *dev, struct device_attribute *attr,
611 const char *buf, size_t count)
613 struct i2c_client *client = to_i2c_client(dev);
614 struct w83793_data *data = i2c_get_clientdata(client);
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);
620 u8 val = simple_strtoul(buf, NULL, 10);
622 /* transform the sysfs interface values into table above */
623 if ((val == 6) && (index < 4)) {
624 val -= 3;
625 } else if ((val == 3 && index < 4)
626 || (val == 4 && index >= 4)) {
627 /* transform diode or thermistor into internal enable */
628 val = !!val;
629 } else {
630 return -EINVAL;
633 index = (index < 4) ? 0 : 1;
634 mutex_lock(&data->update_lock);
635 data->temp_mode[index] =
636 w83793_read_value(client, W83793_REG_TEMP_MODE[index]);
637 data->temp_mode[index] &= ~(mask << shift);
638 data->temp_mode[index] |= val << shift;
639 w83793_write_value(client, W83793_REG_TEMP_MODE[index],
640 data->temp_mode[index]);
641 mutex_unlock(&data->update_lock);
643 return count;
646 #define SETUP_PWM_DEFAULT 0
647 #define SETUP_PWM_UPTIME 1 /* Unit in 0.1s */
648 #define SETUP_PWM_DOWNTIME 2 /* Unit in 0.1s */
649 #define SETUP_TEMP_CRITICAL 3
650 static ssize_t
651 show_sf_setup(struct device *dev, struct device_attribute *attr, char *buf)
653 struct sensor_device_attribute_2 *sensor_attr =
654 to_sensor_dev_attr_2(attr);
655 int nr = sensor_attr->nr;
656 struct w83793_data *data = w83793_update_device(dev);
657 u32 val = 0;
659 if (SETUP_PWM_DEFAULT == nr) {
660 val = (data->pwm_default & 0x3f) << 2;
661 } else if (SETUP_PWM_UPTIME == nr) {
662 val = TIME_FROM_REG(data->pwm_uptime);
663 } else if (SETUP_PWM_DOWNTIME == nr) {
664 val = TIME_FROM_REG(data->pwm_downtime);
665 } else if (SETUP_TEMP_CRITICAL == nr) {
666 val = TEMP_FROM_REG(data->temp_critical & 0x7f);
669 return sprintf(buf, "%d\n", val);
672 static ssize_t
673 store_sf_setup(struct device *dev, struct device_attribute *attr,
674 const char *buf, size_t count)
676 struct sensor_device_attribute_2 *sensor_attr =
677 to_sensor_dev_attr_2(attr);
678 int nr = sensor_attr->nr;
679 struct i2c_client *client = to_i2c_client(dev);
680 struct w83793_data *data = i2c_get_clientdata(client);
682 mutex_lock(&data->update_lock);
683 if (SETUP_PWM_DEFAULT == nr) {
684 data->pwm_default =
685 w83793_read_value(client, W83793_REG_PWM_DEFAULT) & 0xc0;
686 data->pwm_default |= SENSORS_LIMIT(simple_strtoul(buf, NULL,
687 10),
688 0, 0xff) >> 2;
689 w83793_write_value(client, W83793_REG_PWM_DEFAULT,
690 data->pwm_default);
691 } else if (SETUP_PWM_UPTIME == nr) {
692 data->pwm_uptime = TIME_TO_REG(simple_strtoul(buf, NULL, 10));
693 data->pwm_uptime += data->pwm_uptime == 0 ? 1 : 0;
694 w83793_write_value(client, W83793_REG_PWM_UPTIME,
695 data->pwm_uptime);
696 } else if (SETUP_PWM_DOWNTIME == nr) {
697 data->pwm_downtime = TIME_TO_REG(simple_strtoul(buf, NULL, 10));
698 data->pwm_downtime += data->pwm_downtime == 0 ? 1 : 0;
699 w83793_write_value(client, W83793_REG_PWM_DOWNTIME,
700 data->pwm_downtime);
701 } else { /* SETUP_TEMP_CRITICAL */
702 data->temp_critical =
703 w83793_read_value(client, W83793_REG_TEMP_CRITICAL) & 0x80;
704 data->temp_critical |= TEMP_TO_REG(simple_strtol(buf, NULL, 10),
705 0, 0x7f);
706 w83793_write_value(client, W83793_REG_TEMP_CRITICAL,
707 data->temp_critical);
710 mutex_unlock(&data->update_lock);
711 return count;
715 Temp SmartFan control
716 TEMP_FAN_MAP
717 Temp channel control which pwm fan, bitfield, bit 0 indicate pwm1...
718 It's possible two or more temp channels control the same fan, w83793
719 always prefers to pick the most critical request and applies it to
720 the related Fan.
721 It's possible one fan is not in any mapping of 6 temp channels, this
722 means the fan is manual mode
724 TEMP_PWM_ENABLE
725 Each temp channel has its own SmartFan mode, and temp channel
726 control fans that are set by TEMP_FAN_MAP
727 0: SmartFanII mode
728 1: Thermal Cruise Mode
730 TEMP_CRUISE
731 Target temperature in thermal cruise mode, w83793 will try to turn
732 fan speed to keep the temperature of target device around this
733 temperature.
735 TEMP_TOLERANCE
736 If Temp higher or lower than target with this tolerance, w83793
737 will take actions to speed up or slow down the fan to keep the
738 temperature within the tolerance range.
741 #define TEMP_FAN_MAP 0
742 #define TEMP_PWM_ENABLE 1
743 #define TEMP_CRUISE 2
744 #define TEMP_TOLERANCE 3
745 static ssize_t
746 show_sf_ctrl(struct device *dev, struct device_attribute *attr, char *buf)
748 struct sensor_device_attribute_2 *sensor_attr =
749 to_sensor_dev_attr_2(attr);
750 int nr = sensor_attr->nr;
751 int index = sensor_attr->index;
752 struct w83793_data *data = w83793_update_device(dev);
753 u32 val;
755 if (TEMP_FAN_MAP == nr) {
756 val = data->temp_fan_map[index];
757 } else if (TEMP_PWM_ENABLE == nr) {
758 /* +2 to transfrom into 2 and 3 to conform with sysfs intf */
759 val = ((data->pwm_enable >> index) & 0x01) + 2;
760 } else if (TEMP_CRUISE == nr) {
761 val = TEMP_FROM_REG(data->temp_cruise[index] & 0x7f);
762 } else { /* TEMP_TOLERANCE */
763 val = data->tolerance[index >> 1] >> ((index & 0x01) ? 4 : 0);
764 val = TEMP_FROM_REG(val & 0x0f);
766 return sprintf(buf, "%d\n", val);
769 static ssize_t
770 store_sf_ctrl(struct device *dev, struct device_attribute *attr,
771 const char *buf, size_t count)
773 struct sensor_device_attribute_2 *sensor_attr =
774 to_sensor_dev_attr_2(attr);
775 int nr = sensor_attr->nr;
776 int index = sensor_attr->index;
777 struct i2c_client *client = to_i2c_client(dev);
778 struct w83793_data *data = i2c_get_clientdata(client);
779 u32 val;
781 mutex_lock(&data->update_lock);
782 if (TEMP_FAN_MAP == nr) {
783 val = simple_strtoul(buf, NULL, 10) & 0xff;
784 w83793_write_value(client, W83793_REG_TEMP_FAN_MAP(index), val);
785 data->temp_fan_map[index] = val;
786 } else if (TEMP_PWM_ENABLE == nr) {
787 val = simple_strtoul(buf, NULL, 10);
788 if (2 == val || 3 == val) {
789 data->pwm_enable =
790 w83793_read_value(client, W83793_REG_PWM_ENABLE);
791 if (val - 2)
792 data->pwm_enable |= 1 << index;
793 else
794 data->pwm_enable &= ~(1 << index);
795 w83793_write_value(client, W83793_REG_PWM_ENABLE,
796 data->pwm_enable);
797 } else {
798 mutex_unlock(&data->update_lock);
799 return -EINVAL;
801 } else if (TEMP_CRUISE == nr) {
802 data->temp_cruise[index] =
803 w83793_read_value(client, W83793_REG_TEMP_CRUISE(index));
804 val = TEMP_TO_REG(simple_strtol(buf, NULL, 10), 0, 0x7f);
805 data->temp_cruise[index] &= 0x80;
806 data->temp_cruise[index] |= val;
808 w83793_write_value(client, W83793_REG_TEMP_CRUISE(index),
809 data->temp_cruise[index]);
810 } else { /* TEMP_TOLERANCE */
811 int i = index >> 1;
812 u8 shift = (index & 0x01) ? 4 : 0;
813 data->tolerance[i] =
814 w83793_read_value(client, W83793_REG_TEMP_TOL(i));
816 val = TEMP_TO_REG(simple_strtol(buf, NULL, 10), 0, 0x0f);
817 data->tolerance[i] &= ~(0x0f << shift);
818 data->tolerance[i] |= val << shift;
819 w83793_write_value(client, W83793_REG_TEMP_TOL(i),
820 data->tolerance[i]);
823 mutex_unlock(&data->update_lock);
824 return count;
827 static ssize_t
828 show_sf2_pwm(struct device *dev, struct device_attribute *attr, char *buf)
830 struct sensor_device_attribute_2 *sensor_attr =
831 to_sensor_dev_attr_2(attr);
832 int nr = sensor_attr->nr;
833 int index = sensor_attr->index;
834 struct w83793_data *data = w83793_update_device(dev);
836 return sprintf(buf, "%d\n", (data->sf2_pwm[index][nr] & 0x3f) << 2);
839 static ssize_t
840 store_sf2_pwm(struct device *dev, struct device_attribute *attr,
841 const char *buf, size_t count)
843 struct i2c_client *client = to_i2c_client(dev);
844 struct w83793_data *data = i2c_get_clientdata(client);
845 struct sensor_device_attribute_2 *sensor_attr =
846 to_sensor_dev_attr_2(attr);
847 int nr = sensor_attr->nr;
848 int index = sensor_attr->index;
849 u8 val = SENSORS_LIMIT(simple_strtoul(buf, NULL, 10), 0, 0xff) >> 2;
851 mutex_lock(&data->update_lock);
852 data->sf2_pwm[index][nr] =
853 w83793_read_value(client, W83793_REG_SF2_PWM(index, nr)) & 0xc0;
854 data->sf2_pwm[index][nr] |= val;
855 w83793_write_value(client, W83793_REG_SF2_PWM(index, nr),
856 data->sf2_pwm[index][nr]);
857 mutex_unlock(&data->update_lock);
858 return count;
861 static ssize_t
862 show_sf2_temp(struct device *dev, struct device_attribute *attr, char *buf)
864 struct sensor_device_attribute_2 *sensor_attr =
865 to_sensor_dev_attr_2(attr);
866 int nr = sensor_attr->nr;
867 int index = sensor_attr->index;
868 struct w83793_data *data = w83793_update_device(dev);
870 return sprintf(buf, "%ld\n",
871 TEMP_FROM_REG(data->sf2_temp[index][nr] & 0x7f));
874 static ssize_t
875 store_sf2_temp(struct device *dev, struct device_attribute *attr,
876 const char *buf, size_t count)
878 struct i2c_client *client = to_i2c_client(dev);
879 struct w83793_data *data = i2c_get_clientdata(client);
880 struct sensor_device_attribute_2 *sensor_attr =
881 to_sensor_dev_attr_2(attr);
882 int nr = sensor_attr->nr;
883 int index = sensor_attr->index;
884 u8 val = TEMP_TO_REG(simple_strtol(buf, NULL, 10), 0, 0x7f);
886 mutex_lock(&data->update_lock);
887 data->sf2_temp[index][nr] =
888 w83793_read_value(client, W83793_REG_SF2_TEMP(index, nr)) & 0x80;
889 data->sf2_temp[index][nr] |= val;
890 w83793_write_value(client, W83793_REG_SF2_TEMP(index, nr),
891 data->sf2_temp[index][nr]);
892 mutex_unlock(&data->update_lock);
893 return count;
896 /* only Vcore A/B and Vtt have additional 2 bits precision */
897 static ssize_t
898 show_in(struct device *dev, struct device_attribute *attr, char *buf)
900 struct sensor_device_attribute_2 *sensor_attr =
901 to_sensor_dev_attr_2(attr);
902 int nr = sensor_attr->nr;
903 int index = sensor_attr->index;
904 struct w83793_data *data = w83793_update_device(dev);
905 u16 val = data->in[index][nr];
907 if (index < 3) {
908 val <<= 2;
909 val += (data->in_low_bits[nr] >> (index * 2)) & 0x3;
911 /* voltage inputs 5VDD and 5VSB needs 150mV offset */
912 val = val * scale_in[index] + scale_in_add[index];
913 return sprintf(buf, "%d\n", val);
916 static ssize_t
917 store_in(struct device *dev, struct device_attribute *attr,
918 const char *buf, size_t count)
920 struct sensor_device_attribute_2 *sensor_attr =
921 to_sensor_dev_attr_2(attr);
922 int nr = sensor_attr->nr;
923 int index = sensor_attr->index;
924 struct i2c_client *client = to_i2c_client(dev);
925 struct w83793_data *data = i2c_get_clientdata(client);
926 u32 val;
928 val =
929 (simple_strtoul(buf, NULL, 10) +
930 scale_in[index] / 2) / scale_in[index];
931 mutex_lock(&data->update_lock);
932 if (index > 2) {
933 /* fix the limit values of 5VDD and 5VSB to ALARM mechanism */
934 if (1 == nr || 2 == nr) {
935 val -= scale_in_add[index] / scale_in[index];
937 val = SENSORS_LIMIT(val, 0, 255);
938 } else {
939 val = SENSORS_LIMIT(val, 0, 0x3FF);
940 data->in_low_bits[nr] =
941 w83793_read_value(client, W83793_REG_IN_LOW_BITS[nr]);
942 data->in_low_bits[nr] &= ~(0x03 << (2 * index));
943 data->in_low_bits[nr] |= (val & 0x03) << (2 * index);
944 w83793_write_value(client, W83793_REG_IN_LOW_BITS[nr],
945 data->in_low_bits[nr]);
946 val >>= 2;
948 data->in[index][nr] = val;
949 w83793_write_value(client, W83793_REG_IN[index][nr],
950 data->in[index][nr]);
951 mutex_unlock(&data->update_lock);
952 return count;
955 #define NOT_USED -1
957 #define SENSOR_ATTR_IN(index) \
958 SENSOR_ATTR_2(in##index##_input, S_IRUGO, show_in, NULL, \
959 IN_READ, index), \
960 SENSOR_ATTR_2(in##index##_max, S_IRUGO | S_IWUSR, show_in, \
961 store_in, IN_MAX, index), \
962 SENSOR_ATTR_2(in##index##_min, S_IRUGO | S_IWUSR, show_in, \
963 store_in, IN_LOW, index), \
964 SENSOR_ATTR_2(in##index##_alarm, S_IRUGO, show_alarm_beep, \
965 NULL, ALARM_STATUS, index + ((index > 2) ? 1 : 0)), \
966 SENSOR_ATTR_2(in##index##_beep, S_IWUSR | S_IRUGO, \
967 show_alarm_beep, store_beep, BEEP_ENABLE, \
968 index + ((index > 2) ? 1 : 0))
970 #define SENSOR_ATTR_FAN(index) \
971 SENSOR_ATTR_2(fan##index##_alarm, S_IRUGO, show_alarm_beep, \
972 NULL, ALARM_STATUS, index + 17), \
973 SENSOR_ATTR_2(fan##index##_beep, S_IWUSR | S_IRUGO, \
974 show_alarm_beep, store_beep, BEEP_ENABLE, index + 17), \
975 SENSOR_ATTR_2(fan##index##_input, S_IRUGO, show_fan, \
976 NULL, FAN_INPUT, index - 1), \
977 SENSOR_ATTR_2(fan##index##_min, S_IWUSR | S_IRUGO, \
978 show_fan, store_fan_min, FAN_MIN, index - 1)
980 #define SENSOR_ATTR_PWM(index) \
981 SENSOR_ATTR_2(pwm##index, S_IWUSR | S_IRUGO, show_pwm, \
982 store_pwm, PWM_DUTY, index - 1), \
983 SENSOR_ATTR_2(pwm##index##_nonstop, S_IWUSR | S_IRUGO, \
984 show_pwm, store_pwm, PWM_NONSTOP, index - 1), \
985 SENSOR_ATTR_2(pwm##index##_start, S_IWUSR | S_IRUGO, \
986 show_pwm, store_pwm, PWM_START, index - 1), \
987 SENSOR_ATTR_2(pwm##index##_stop_time, S_IWUSR | S_IRUGO, \
988 show_pwm, store_pwm, PWM_STOP_TIME, index - 1)
990 #define SENSOR_ATTR_TEMP(index) \
991 SENSOR_ATTR_2(temp##index##_type, S_IRUGO | S_IWUSR, \
992 show_temp_mode, store_temp_mode, NOT_USED, index - 1), \
993 SENSOR_ATTR_2(temp##index##_input, S_IRUGO, show_temp, \
994 NULL, TEMP_READ, index - 1), \
995 SENSOR_ATTR_2(temp##index##_max, S_IRUGO | S_IWUSR, show_temp, \
996 store_temp, TEMP_CRIT, index - 1), \
997 SENSOR_ATTR_2(temp##index##_max_hyst, S_IRUGO | S_IWUSR, \
998 show_temp, store_temp, TEMP_CRIT_HYST, index - 1), \
999 SENSOR_ATTR_2(temp##index##_warn, S_IRUGO | S_IWUSR, show_temp, \
1000 store_temp, TEMP_WARN, index - 1), \
1001 SENSOR_ATTR_2(temp##index##_warn_hyst, S_IRUGO | S_IWUSR, \
1002 show_temp, store_temp, TEMP_WARN_HYST, index - 1), \
1003 SENSOR_ATTR_2(temp##index##_alarm, S_IRUGO, \
1004 show_alarm_beep, NULL, ALARM_STATUS, index + 11), \
1005 SENSOR_ATTR_2(temp##index##_beep, S_IWUSR | S_IRUGO, \
1006 show_alarm_beep, store_beep, BEEP_ENABLE, index + 11), \
1007 SENSOR_ATTR_2(temp##index##_auto_channels_pwm, \
1008 S_IRUGO | S_IWUSR, show_sf_ctrl, store_sf_ctrl, \
1009 TEMP_FAN_MAP, index - 1), \
1010 SENSOR_ATTR_2(temp##index##_pwm_enable, S_IWUSR | S_IRUGO, \
1011 show_sf_ctrl, store_sf_ctrl, TEMP_PWM_ENABLE, \
1012 index - 1), \
1013 SENSOR_ATTR_2(thermal_cruise##index, S_IRUGO | S_IWUSR, \
1014 show_sf_ctrl, store_sf_ctrl, TEMP_CRUISE, index - 1), \
1015 SENSOR_ATTR_2(tolerance##index, S_IRUGO | S_IWUSR, show_sf_ctrl,\
1016 store_sf_ctrl, TEMP_TOLERANCE, index - 1), \
1017 SENSOR_ATTR_2(temp##index##_auto_point1_pwm, S_IRUGO | S_IWUSR, \
1018 show_sf2_pwm, store_sf2_pwm, 0, index - 1), \
1019 SENSOR_ATTR_2(temp##index##_auto_point2_pwm, S_IRUGO | S_IWUSR, \
1020 show_sf2_pwm, store_sf2_pwm, 1, index - 1), \
1021 SENSOR_ATTR_2(temp##index##_auto_point3_pwm, S_IRUGO | S_IWUSR, \
1022 show_sf2_pwm, store_sf2_pwm, 2, index - 1), \
1023 SENSOR_ATTR_2(temp##index##_auto_point4_pwm, S_IRUGO | S_IWUSR, \
1024 show_sf2_pwm, store_sf2_pwm, 3, index - 1), \
1025 SENSOR_ATTR_2(temp##index##_auto_point5_pwm, S_IRUGO | S_IWUSR, \
1026 show_sf2_pwm, store_sf2_pwm, 4, index - 1), \
1027 SENSOR_ATTR_2(temp##index##_auto_point6_pwm, S_IRUGO | S_IWUSR, \
1028 show_sf2_pwm, store_sf2_pwm, 5, index - 1), \
1029 SENSOR_ATTR_2(temp##index##_auto_point7_pwm, S_IRUGO | S_IWUSR, \
1030 show_sf2_pwm, store_sf2_pwm, 6, index - 1), \
1031 SENSOR_ATTR_2(temp##index##_auto_point1_temp, S_IRUGO | S_IWUSR,\
1032 show_sf2_temp, store_sf2_temp, 0, index - 1), \
1033 SENSOR_ATTR_2(temp##index##_auto_point2_temp, S_IRUGO | S_IWUSR,\
1034 show_sf2_temp, store_sf2_temp, 1, index - 1), \
1035 SENSOR_ATTR_2(temp##index##_auto_point3_temp, S_IRUGO | S_IWUSR,\
1036 show_sf2_temp, store_sf2_temp, 2, index - 1), \
1037 SENSOR_ATTR_2(temp##index##_auto_point4_temp, S_IRUGO | S_IWUSR,\
1038 show_sf2_temp, store_sf2_temp, 3, index - 1), \
1039 SENSOR_ATTR_2(temp##index##_auto_point5_temp, S_IRUGO | S_IWUSR,\
1040 show_sf2_temp, store_sf2_temp, 4, index - 1), \
1041 SENSOR_ATTR_2(temp##index##_auto_point6_temp, S_IRUGO | S_IWUSR,\
1042 show_sf2_temp, store_sf2_temp, 5, index - 1), \
1043 SENSOR_ATTR_2(temp##index##_auto_point7_temp, S_IRUGO | S_IWUSR,\
1044 show_sf2_temp, store_sf2_temp, 6, index - 1)
1046 static struct sensor_device_attribute_2 w83793_sensor_attr_2[] = {
1047 SENSOR_ATTR_IN(0),
1048 SENSOR_ATTR_IN(1),
1049 SENSOR_ATTR_IN(2),
1050 SENSOR_ATTR_IN(3),
1051 SENSOR_ATTR_IN(4),
1052 SENSOR_ATTR_IN(5),
1053 SENSOR_ATTR_IN(6),
1054 SENSOR_ATTR_IN(7),
1055 SENSOR_ATTR_IN(8),
1056 SENSOR_ATTR_IN(9),
1057 SENSOR_ATTR_FAN(1),
1058 SENSOR_ATTR_FAN(2),
1059 SENSOR_ATTR_FAN(3),
1060 SENSOR_ATTR_FAN(4),
1061 SENSOR_ATTR_FAN(5),
1062 SENSOR_ATTR_PWM(1),
1063 SENSOR_ATTR_PWM(2),
1064 SENSOR_ATTR_PWM(3),
1067 static struct sensor_device_attribute_2 w83793_temp[] = {
1068 SENSOR_ATTR_TEMP(1),
1069 SENSOR_ATTR_TEMP(2),
1070 SENSOR_ATTR_TEMP(3),
1071 SENSOR_ATTR_TEMP(4),
1072 SENSOR_ATTR_TEMP(5),
1073 SENSOR_ATTR_TEMP(6),
1076 /* Fan6-Fan12 */
1077 static struct sensor_device_attribute_2 w83793_left_fan[] = {
1078 SENSOR_ATTR_FAN(6),
1079 SENSOR_ATTR_FAN(7),
1080 SENSOR_ATTR_FAN(8),
1081 SENSOR_ATTR_FAN(9),
1082 SENSOR_ATTR_FAN(10),
1083 SENSOR_ATTR_FAN(11),
1084 SENSOR_ATTR_FAN(12),
1087 /* Pwm4-Pwm8 */
1088 static struct sensor_device_attribute_2 w83793_left_pwm[] = {
1089 SENSOR_ATTR_PWM(4),
1090 SENSOR_ATTR_PWM(5),
1091 SENSOR_ATTR_PWM(6),
1092 SENSOR_ATTR_PWM(7),
1093 SENSOR_ATTR_PWM(8),
1096 static struct sensor_device_attribute_2 w83793_vid[] = {
1097 SENSOR_ATTR_2(cpu0_vid, S_IRUGO, show_vid, NULL, NOT_USED, 0),
1098 SENSOR_ATTR_2(cpu1_vid, S_IRUGO, show_vid, NULL, NOT_USED, 1),
1100 static DEVICE_ATTR(vrm, S_IWUSR | S_IRUGO, show_vrm, store_vrm);
1102 static struct sensor_device_attribute_2 sda_single_files[] = {
1103 SENSOR_ATTR_2(chassis, S_IWUSR | S_IRUGO, show_alarm_beep,
1104 store_chassis_clear, ALARM_STATUS, 30),
1105 SENSOR_ATTR_2(beep_enable, S_IWUSR | S_IRUGO, show_beep_enable,
1106 store_beep_enable, NOT_USED, NOT_USED),
1107 SENSOR_ATTR_2(pwm_default, S_IWUSR | S_IRUGO, show_sf_setup,
1108 store_sf_setup, SETUP_PWM_DEFAULT, NOT_USED),
1109 SENSOR_ATTR_2(pwm_uptime, S_IWUSR | S_IRUGO, show_sf_setup,
1110 store_sf_setup, SETUP_PWM_UPTIME, NOT_USED),
1111 SENSOR_ATTR_2(pwm_downtime, S_IWUSR | S_IRUGO, show_sf_setup,
1112 store_sf_setup, SETUP_PWM_DOWNTIME, NOT_USED),
1113 SENSOR_ATTR_2(temp_critical, S_IWUSR | S_IRUGO, show_sf_setup,
1114 store_sf_setup, SETUP_TEMP_CRITICAL, NOT_USED),
1117 static void w83793_init_client(struct i2c_client *client)
1119 if (reset) {
1120 w83793_write_value(client, W83793_REG_CONFIG, 0x80);
1123 /* Start monitoring */
1124 w83793_write_value(client, W83793_REG_CONFIG,
1125 w83793_read_value(client, W83793_REG_CONFIG) | 0x01);
1129 * Watchdog routines
1132 static int watchdog_set_timeout(struct w83793_data *data, int timeout)
1134 int ret, mtimeout;
1136 mtimeout = DIV_ROUND_UP(timeout, 60);
1138 if (mtimeout > 255)
1139 return -EINVAL;
1141 mutex_lock(&data->watchdog_lock);
1142 if (!data->client) {
1143 ret = -ENODEV;
1144 goto leave;
1147 data->watchdog_timeout = mtimeout;
1149 /* Set Timeout value (in Minutes) */
1150 w83793_write_value(data->client, W83793_REG_WDT_TIMEOUT,
1151 data->watchdog_timeout);
1153 ret = mtimeout * 60;
1155 leave:
1156 mutex_unlock(&data->watchdog_lock);
1157 return ret;
1160 static int watchdog_get_timeout(struct w83793_data *data)
1162 int timeout;
1164 mutex_lock(&data->watchdog_lock);
1165 timeout = data->watchdog_timeout * 60;
1166 mutex_unlock(&data->watchdog_lock);
1168 return timeout;
1171 static int watchdog_trigger(struct w83793_data *data)
1173 int ret = 0;
1175 mutex_lock(&data->watchdog_lock);
1176 if (!data->client) {
1177 ret = -ENODEV;
1178 goto leave;
1181 /* Set Timeout value (in Minutes) */
1182 w83793_write_value(data->client, W83793_REG_WDT_TIMEOUT,
1183 data->watchdog_timeout);
1185 leave:
1186 mutex_unlock(&data->watchdog_lock);
1187 return ret;
1190 static int watchdog_enable(struct w83793_data *data)
1192 int ret = 0;
1194 mutex_lock(&data->watchdog_lock);
1195 if (!data->client) {
1196 ret = -ENODEV;
1197 goto leave;
1200 /* Set initial timeout */
1201 w83793_write_value(data->client, W83793_REG_WDT_TIMEOUT,
1202 data->watchdog_timeout);
1204 /* Enable Soft Watchdog */
1205 w83793_write_value(data->client, W83793_REG_WDT_LOCK, 0x55);
1207 leave:
1208 mutex_unlock(&data->watchdog_lock);
1209 return ret;
1212 static int watchdog_disable(struct w83793_data *data)
1214 int ret = 0;
1216 mutex_lock(&data->watchdog_lock);
1217 if (!data->client) {
1218 ret = -ENODEV;
1219 goto leave;
1222 /* Disable Soft Watchdog */
1223 w83793_write_value(data->client, W83793_REG_WDT_LOCK, 0xAA);
1225 leave:
1226 mutex_unlock(&data->watchdog_lock);
1227 return ret;
1230 static int watchdog_open(struct inode *inode, struct file *filp)
1232 struct w83793_data *pos, *data = NULL;
1233 int watchdog_is_open;
1235 /* We get called from drivers/char/misc.c with misc_mtx hold, and we
1236 call misc_register() from w83793_probe() with watchdog_data_mutex
1237 hold, as misc_register() takes the misc_mtx lock, this is a possible
1238 deadlock, so we use mutex_trylock here. */
1239 if (!mutex_trylock(&watchdog_data_mutex))
1240 return -ERESTARTSYS;
1241 list_for_each_entry(pos, &watchdog_data_list, list) {
1242 if (pos->watchdog_miscdev.minor == iminor(inode)) {
1243 data = pos;
1244 break;
1248 /* Check, if device is already open */
1249 watchdog_is_open = test_and_set_bit(0, &data->watchdog_is_open);
1251 /* Increase data reference counter (if not already done).
1252 Note we can never not have found data, so we don't check for this */
1253 if (!watchdog_is_open)
1254 kref_get(&data->kref);
1256 mutex_unlock(&watchdog_data_mutex);
1258 /* Check, if device is already open and possibly issue error */
1259 if (watchdog_is_open)
1260 return -EBUSY;
1262 /* Enable Soft Watchdog */
1263 watchdog_enable(data);
1265 /* Store pointer to data into filp's private data */
1266 filp->private_data = data;
1268 return nonseekable_open(inode, filp);
1271 static int watchdog_close(struct inode *inode, struct file *filp)
1273 struct w83793_data *data = filp->private_data;
1275 if (data->watchdog_expect_close) {
1276 watchdog_disable(data);
1277 data->watchdog_expect_close = 0;
1278 } else {
1279 watchdog_trigger(data);
1280 dev_crit(&data->client->dev,
1281 "unexpected close, not stopping watchdog!\n");
1284 clear_bit(0, &data->watchdog_is_open);
1286 /* Decrease data reference counter */
1287 mutex_lock(&watchdog_data_mutex);
1288 kref_put(&data->kref, w83793_release_resources);
1289 mutex_unlock(&watchdog_data_mutex);
1291 return 0;
1294 static ssize_t watchdog_write(struct file *filp, const char __user *buf,
1295 size_t count, loff_t *offset)
1297 ssize_t ret;
1298 struct w83793_data *data = filp->private_data;
1300 if (count) {
1301 if (!nowayout) {
1302 size_t i;
1304 /* Clear it in case it was set with a previous write */
1305 data->watchdog_expect_close = 0;
1307 for (i = 0; i != count; i++) {
1308 char c;
1309 if (get_user(c, buf + i))
1310 return -EFAULT;
1311 if (c == 'V')
1312 data->watchdog_expect_close = 1;
1315 ret = watchdog_trigger(data);
1316 if (ret < 0)
1317 return ret;
1319 return count;
1322 static int watchdog_ioctl(struct inode *inode, struct file *filp,
1323 unsigned int cmd, unsigned long arg)
1325 static struct watchdog_info ident = {
1326 .options = WDIOF_KEEPALIVEPING |
1327 WDIOF_SETTIMEOUT |
1328 WDIOF_CARDRESET,
1329 .identity = "w83793 watchdog"
1332 int val, ret = 0;
1333 struct w83793_data *data = filp->private_data;
1335 switch (cmd) {
1336 case WDIOC_GETSUPPORT:
1337 if (!nowayout)
1338 ident.options |= WDIOF_MAGICCLOSE;
1339 if (copy_to_user((void __user *)arg, &ident, sizeof(ident)))
1340 ret = -EFAULT;
1341 break;
1343 case WDIOC_GETSTATUS:
1344 val = data->watchdog_caused_reboot ? WDIOF_CARDRESET : 0;
1345 ret = put_user(val, (int __user *)arg);
1346 break;
1348 case WDIOC_GETBOOTSTATUS:
1349 ret = put_user(0, (int __user *)arg);
1350 break;
1352 case WDIOC_KEEPALIVE:
1353 ret = watchdog_trigger(data);
1354 break;
1356 case WDIOC_GETTIMEOUT:
1357 val = watchdog_get_timeout(data);
1358 ret = put_user(val, (int __user *)arg);
1359 break;
1361 case WDIOC_SETTIMEOUT:
1362 if (get_user(val, (int __user *)arg)) {
1363 ret = -EFAULT;
1364 break;
1366 ret = watchdog_set_timeout(data, val);
1367 if (ret > 0)
1368 ret = put_user(ret, (int __user *)arg);
1369 break;
1371 case WDIOC_SETOPTIONS:
1372 if (get_user(val, (int __user *)arg)) {
1373 ret = -EFAULT;
1374 break;
1377 if (val & WDIOS_DISABLECARD)
1378 ret = watchdog_disable(data);
1379 else if (val & WDIOS_ENABLECARD)
1380 ret = watchdog_enable(data);
1381 else
1382 ret = -EINVAL;
1384 break;
1385 default:
1386 ret = -ENOTTY;
1389 return ret;
1392 static const struct file_operations watchdog_fops = {
1393 .owner = THIS_MODULE,
1394 .llseek = no_llseek,
1395 .open = watchdog_open,
1396 .release = watchdog_close,
1397 .write = watchdog_write,
1398 .ioctl = watchdog_ioctl,
1402 * Notifier for system down
1405 static int watchdog_notify_sys(struct notifier_block *this, unsigned long code,
1406 void *unused)
1408 struct w83793_data *data = NULL;
1410 if (code == SYS_DOWN || code == SYS_HALT) {
1412 /* Disable each registered watchdog */
1413 mutex_lock(&watchdog_data_mutex);
1414 list_for_each_entry(data, &watchdog_data_list, list) {
1415 if (data->watchdog_miscdev.minor)
1416 watchdog_disable(data);
1418 mutex_unlock(&watchdog_data_mutex);
1421 return NOTIFY_DONE;
1425 * The WDT needs to learn about soft shutdowns in order to
1426 * turn the timebomb registers off.
1429 static struct notifier_block watchdog_notifier = {
1430 .notifier_call = watchdog_notify_sys,
1434 * Init / remove routines
1437 static int w83793_remove(struct i2c_client *client)
1439 struct w83793_data *data = i2c_get_clientdata(client);
1440 struct device *dev = &client->dev;
1441 int i, tmp;
1443 /* Unregister the watchdog (if registered) */
1444 if (data->watchdog_miscdev.minor) {
1445 misc_deregister(&data->watchdog_miscdev);
1447 if (data->watchdog_is_open) {
1448 dev_warn(&client->dev,
1449 "i2c client detached with watchdog open! "
1450 "Stopping watchdog.\n");
1451 watchdog_disable(data);
1454 mutex_lock(&watchdog_data_mutex);
1455 list_del(&data->list);
1456 mutex_unlock(&watchdog_data_mutex);
1458 /* Tell the watchdog code the client is gone */
1459 mutex_lock(&data->watchdog_lock);
1460 data->client = NULL;
1461 mutex_unlock(&data->watchdog_lock);
1464 /* Reset Configuration Register to Disable Watch Dog Registers */
1465 tmp = w83793_read_value(client, W83793_REG_CONFIG);
1466 w83793_write_value(client, W83793_REG_CONFIG, tmp & ~0x04);
1468 unregister_reboot_notifier(&watchdog_notifier);
1470 hwmon_device_unregister(data->hwmon_dev);
1472 for (i = 0; i < ARRAY_SIZE(w83793_sensor_attr_2); i++)
1473 device_remove_file(dev,
1474 &w83793_sensor_attr_2[i].dev_attr);
1476 for (i = 0; i < ARRAY_SIZE(sda_single_files); i++)
1477 device_remove_file(dev, &sda_single_files[i].dev_attr);
1479 for (i = 0; i < ARRAY_SIZE(w83793_vid); i++)
1480 device_remove_file(dev, &w83793_vid[i].dev_attr);
1481 device_remove_file(dev, &dev_attr_vrm);
1483 for (i = 0; i < ARRAY_SIZE(w83793_left_fan); i++)
1484 device_remove_file(dev, &w83793_left_fan[i].dev_attr);
1486 for (i = 0; i < ARRAY_SIZE(w83793_left_pwm); i++)
1487 device_remove_file(dev, &w83793_left_pwm[i].dev_attr);
1489 for (i = 0; i < ARRAY_SIZE(w83793_temp); i++)
1490 device_remove_file(dev, &w83793_temp[i].dev_attr);
1492 if (data->lm75[0] != NULL)
1493 i2c_unregister_device(data->lm75[0]);
1494 if (data->lm75[1] != NULL)
1495 i2c_unregister_device(data->lm75[1]);
1497 /* Decrease data reference counter */
1498 mutex_lock(&watchdog_data_mutex);
1499 kref_put(&data->kref, w83793_release_resources);
1500 mutex_unlock(&watchdog_data_mutex);
1502 return 0;
1505 static int
1506 w83793_detect_subclients(struct i2c_client *client)
1508 int i, id, err;
1509 int address = client->addr;
1510 u8 tmp;
1511 struct i2c_adapter *adapter = client->adapter;
1512 struct w83793_data *data = i2c_get_clientdata(client);
1514 id = i2c_adapter_id(adapter);
1515 if (force_subclients[0] == id && force_subclients[1] == address) {
1516 for (i = 2; i <= 3; i++) {
1517 if (force_subclients[i] < 0x48
1518 || force_subclients[i] > 0x4f) {
1519 dev_err(&client->dev,
1520 "invalid subclient "
1521 "address %d; must be 0x48-0x4f\n",
1522 force_subclients[i]);
1523 err = -EINVAL;
1524 goto ERROR_SC_0;
1527 w83793_write_value(client, W83793_REG_I2C_SUBADDR,
1528 (force_subclients[2] & 0x07) |
1529 ((force_subclients[3] & 0x07) << 4));
1532 tmp = w83793_read_value(client, W83793_REG_I2C_SUBADDR);
1533 if (!(tmp & 0x08)) {
1534 data->lm75[0] = i2c_new_dummy(adapter, 0x48 + (tmp & 0x7));
1536 if (!(tmp & 0x80)) {
1537 if ((data->lm75[0] != NULL)
1538 && ((tmp & 0x7) == ((tmp >> 4) & 0x7))) {
1539 dev_err(&client->dev,
1540 "duplicate addresses 0x%x, "
1541 "use force_subclients\n", data->lm75[0]->addr);
1542 err = -ENODEV;
1543 goto ERROR_SC_1;
1545 data->lm75[1] = i2c_new_dummy(adapter,
1546 0x48 + ((tmp >> 4) & 0x7));
1549 return 0;
1551 /* Undo inits in case of errors */
1553 ERROR_SC_1:
1554 if (data->lm75[0] != NULL)
1555 i2c_unregister_device(data->lm75[0]);
1556 ERROR_SC_0:
1557 return err;
1560 /* Return 0 if detection is successful, -ENODEV otherwise */
1561 static int w83793_detect(struct i2c_client *client,
1562 struct i2c_board_info *info)
1564 u8 tmp, bank, chip_id;
1565 struct i2c_adapter *adapter = client->adapter;
1566 unsigned short address = client->addr;
1568 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA)) {
1569 return -ENODEV;
1572 bank = i2c_smbus_read_byte_data(client, W83793_REG_BANKSEL);
1574 tmp = bank & 0x80 ? 0x5c : 0xa3;
1575 /* Check Winbond vendor ID */
1576 if (tmp != i2c_smbus_read_byte_data(client, W83793_REG_VENDORID)) {
1577 pr_debug("w83793: Detection failed at check vendor id\n");
1578 return -ENODEV;
1581 /* If Winbond chip, address of chip and W83793_REG_I2C_ADDR
1582 should match */
1583 if ((bank & 0x07) == 0
1584 && i2c_smbus_read_byte_data(client, W83793_REG_I2C_ADDR) !=
1585 (address << 1)) {
1586 pr_debug("w83793: Detection failed at check i2c addr\n");
1587 return -ENODEV;
1590 /* Determine the chip type now */
1591 chip_id = i2c_smbus_read_byte_data(client, W83793_REG_CHIPID);
1592 if (chip_id != 0x7b)
1593 return -ENODEV;
1595 strlcpy(info->type, "w83793", I2C_NAME_SIZE);
1597 return 0;
1600 static int w83793_probe(struct i2c_client *client,
1601 const struct i2c_device_id *id)
1603 struct device *dev = &client->dev;
1604 const int watchdog_minors[] = { WATCHDOG_MINOR, 212, 213, 214, 215 };
1605 struct w83793_data *data;
1606 int i, tmp, val, err;
1607 int files_fan = ARRAY_SIZE(w83793_left_fan) / 7;
1608 int files_pwm = ARRAY_SIZE(w83793_left_pwm) / 5;
1609 int files_temp = ARRAY_SIZE(w83793_temp) / 6;
1611 data = kzalloc(sizeof(struct w83793_data), GFP_KERNEL);
1612 if (!data) {
1613 err = -ENOMEM;
1614 goto exit;
1617 i2c_set_clientdata(client, data);
1618 data->bank = i2c_smbus_read_byte_data(client, W83793_REG_BANKSEL);
1619 mutex_init(&data->update_lock);
1620 mutex_init(&data->watchdog_lock);
1621 INIT_LIST_HEAD(&data->list);
1622 kref_init(&data->kref);
1624 /* Store client pointer in our data struct for watchdog usage
1625 (where the client is found through a data ptr instead of the
1626 otherway around) */
1627 data->client = client;
1629 err = w83793_detect_subclients(client);
1630 if (err)
1631 goto free_mem;
1633 /* Initialize the chip */
1634 w83793_init_client(client);
1637 Only fan 1-5 has their own input pins,
1638 Pwm 1-3 has their own pins
1640 data->has_fan = 0x1f;
1641 data->has_pwm = 0x07;
1642 tmp = w83793_read_value(client, W83793_REG_MFC);
1643 val = w83793_read_value(client, W83793_REG_FANIN_CTRL);
1645 /* check the function of pins 49-56 */
1646 if (tmp & 0x80) {
1647 data->has_vid |= 0x2; /* has VIDB */
1648 } else {
1649 data->has_pwm |= 0x18; /* pwm 4,5 */
1650 if (val & 0x01) { /* fan 6 */
1651 data->has_fan |= 0x20;
1652 data->has_pwm |= 0x20;
1654 if (val & 0x02) { /* fan 7 */
1655 data->has_fan |= 0x40;
1656 data->has_pwm |= 0x40;
1658 if (!(tmp & 0x40) && (val & 0x04)) { /* fan 8 */
1659 data->has_fan |= 0x80;
1660 data->has_pwm |= 0x80;
1664 /* check the function of pins 37-40 */
1665 if (!(tmp & 0x29))
1666 data->has_vid |= 0x1; /* has VIDA */
1667 if (0x08 == (tmp & 0x0c)) {
1668 if (val & 0x08) /* fan 9 */
1669 data->has_fan |= 0x100;
1670 if (val & 0x10) /* fan 10 */
1671 data->has_fan |= 0x200;
1673 if (0x20 == (tmp & 0x30)) {
1674 if (val & 0x20) /* fan 11 */
1675 data->has_fan |= 0x400;
1676 if (val & 0x40) /* fan 12 */
1677 data->has_fan |= 0x800;
1680 if ((tmp & 0x01) && (val & 0x04)) { /* fan 8, second location */
1681 data->has_fan |= 0x80;
1682 data->has_pwm |= 0x80;
1685 tmp = w83793_read_value(client, W83793_REG_FANIN_SEL);
1686 if ((tmp & 0x01) && (val & 0x08)) { /* fan 9, second location */
1687 data->has_fan |= 0x100;
1689 if ((tmp & 0x02) && (val & 0x10)) { /* fan 10, second location */
1690 data->has_fan |= 0x200;
1692 if ((tmp & 0x04) && (val & 0x20)) { /* fan 11, second location */
1693 data->has_fan |= 0x400;
1695 if ((tmp & 0x08) && (val & 0x40)) { /* fan 12, second location */
1696 data->has_fan |= 0x800;
1699 /* check the temp1-6 mode, ignore former AMDSI selected inputs */
1700 tmp = w83793_read_value(client,W83793_REG_TEMP_MODE[0]);
1701 if (tmp & 0x01)
1702 data->has_temp |= 0x01;
1703 if (tmp & 0x04)
1704 data->has_temp |= 0x02;
1705 if (tmp & 0x10)
1706 data->has_temp |= 0x04;
1707 if (tmp & 0x40)
1708 data->has_temp |= 0x08;
1710 tmp = w83793_read_value(client,W83793_REG_TEMP_MODE[1]);
1711 if (tmp & 0x01)
1712 data->has_temp |= 0x10;
1713 if (tmp & 0x02)
1714 data->has_temp |= 0x20;
1716 /* Register sysfs hooks */
1717 for (i = 0; i < ARRAY_SIZE(w83793_sensor_attr_2); i++) {
1718 err = device_create_file(dev,
1719 &w83793_sensor_attr_2[i].dev_attr);
1720 if (err)
1721 goto exit_remove;
1724 for (i = 0; i < ARRAY_SIZE(w83793_vid); i++) {
1725 if (!(data->has_vid & (1 << i)))
1726 continue;
1727 err = device_create_file(dev, &w83793_vid[i].dev_attr);
1728 if (err)
1729 goto exit_remove;
1731 if (data->has_vid) {
1732 data->vrm = vid_which_vrm();
1733 err = device_create_file(dev, &dev_attr_vrm);
1734 if (err)
1735 goto exit_remove;
1738 for (i = 0; i < ARRAY_SIZE(sda_single_files); i++) {
1739 err = device_create_file(dev, &sda_single_files[i].dev_attr);
1740 if (err)
1741 goto exit_remove;
1745 for (i = 0; i < 6; i++) {
1746 int j;
1747 if (!(data->has_temp & (1 << i)))
1748 continue;
1749 for (j = 0; j < files_temp; j++) {
1750 err = device_create_file(dev,
1751 &w83793_temp[(i) * files_temp
1752 + j].dev_attr);
1753 if (err)
1754 goto exit_remove;
1758 for (i = 5; i < 12; i++) {
1759 int j;
1760 if (!(data->has_fan & (1 << i)))
1761 continue;
1762 for (j = 0; j < files_fan; j++) {
1763 err = device_create_file(dev,
1764 &w83793_left_fan[(i - 5) * files_fan
1765 + j].dev_attr);
1766 if (err)
1767 goto exit_remove;
1771 for (i = 3; i < 8; i++) {
1772 int j;
1773 if (!(data->has_pwm & (1 << i)))
1774 continue;
1775 for (j = 0; j < files_pwm; j++) {
1776 err = device_create_file(dev,
1777 &w83793_left_pwm[(i - 3) * files_pwm
1778 + j].dev_attr);
1779 if (err)
1780 goto exit_remove;
1784 data->hwmon_dev = hwmon_device_register(dev);
1785 if (IS_ERR(data->hwmon_dev)) {
1786 err = PTR_ERR(data->hwmon_dev);
1787 goto exit_remove;
1790 /* Watchdog initialization */
1792 /* Register boot notifier */
1793 err = register_reboot_notifier(&watchdog_notifier);
1794 if (err != 0) {
1795 dev_err(&client->dev,
1796 "cannot register reboot notifier (err=%d)\n", err);
1797 goto exit_devunreg;
1800 /* Enable Watchdog registers.
1801 Set Configuration Register to Enable Watch Dog Registers
1802 (Bit 2) = XXXX, X1XX. */
1803 tmp = w83793_read_value(client, W83793_REG_CONFIG);
1804 w83793_write_value(client, W83793_REG_CONFIG, tmp | 0x04);
1806 /* Set the default watchdog timeout */
1807 data->watchdog_timeout = timeout;
1809 /* Check, if last reboot was caused by watchdog */
1810 data->watchdog_caused_reboot =
1811 w83793_read_value(data->client, W83793_REG_WDT_STATUS) & 0x01;
1813 /* Disable Soft Watchdog during initialiation */
1814 watchdog_disable(data);
1816 /* We take the data_mutex lock early so that watchdog_open() cannot
1817 run when misc_register() has completed, but we've not yet added
1818 our data to the watchdog_data_list (and set the default timeout) */
1819 mutex_lock(&watchdog_data_mutex);
1820 for (i = 0; i < ARRAY_SIZE(watchdog_minors); i++) {
1821 /* Register our watchdog part */
1822 snprintf(data->watchdog_name, sizeof(data->watchdog_name),
1823 "watchdog%c", (i == 0) ? '\0' : ('0' + i));
1824 data->watchdog_miscdev.name = data->watchdog_name;
1825 data->watchdog_miscdev.fops = &watchdog_fops;
1826 data->watchdog_miscdev.minor = watchdog_minors[i];
1828 err = misc_register(&data->watchdog_miscdev);
1829 if (err == -EBUSY)
1830 continue;
1831 if (err) {
1832 data->watchdog_miscdev.minor = 0;
1833 dev_err(&client->dev,
1834 "Registering watchdog chardev: %d\n", err);
1835 break;
1838 list_add(&data->list, &watchdog_data_list);
1840 dev_info(&client->dev,
1841 "Registered watchdog chardev major 10, minor: %d\n",
1842 watchdog_minors[i]);
1843 break;
1845 if (i == ARRAY_SIZE(watchdog_minors)) {
1846 data->watchdog_miscdev.minor = 0;
1847 dev_warn(&client->dev, "Couldn't register watchdog chardev "
1848 "(due to no free minor)\n");
1851 mutex_unlock(&watchdog_data_mutex);
1853 return 0;
1855 /* Unregister hwmon device */
1857 exit_devunreg:
1859 hwmon_device_unregister(data->hwmon_dev);
1861 /* Unregister sysfs hooks */
1863 exit_remove:
1864 for (i = 0; i < ARRAY_SIZE(w83793_sensor_attr_2); i++)
1865 device_remove_file(dev, &w83793_sensor_attr_2[i].dev_attr);
1867 for (i = 0; i < ARRAY_SIZE(sda_single_files); i++)
1868 device_remove_file(dev, &sda_single_files[i].dev_attr);
1870 for (i = 0; i < ARRAY_SIZE(w83793_vid); i++)
1871 device_remove_file(dev, &w83793_vid[i].dev_attr);
1873 for (i = 0; i < ARRAY_SIZE(w83793_left_fan); i++)
1874 device_remove_file(dev, &w83793_left_fan[i].dev_attr);
1876 for (i = 0; i < ARRAY_SIZE(w83793_left_pwm); i++)
1877 device_remove_file(dev, &w83793_left_pwm[i].dev_attr);
1879 for (i = 0; i < ARRAY_SIZE(w83793_temp); i++)
1880 device_remove_file(dev, &w83793_temp[i].dev_attr);
1882 if (data->lm75[0] != NULL)
1883 i2c_unregister_device(data->lm75[0]);
1884 if (data->lm75[1] != NULL)
1885 i2c_unregister_device(data->lm75[1]);
1886 free_mem:
1887 kfree(data);
1888 exit:
1889 return err;
1892 static void w83793_update_nonvolatile(struct device *dev)
1894 struct i2c_client *client = to_i2c_client(dev);
1895 struct w83793_data *data = i2c_get_clientdata(client);
1896 int i, j;
1898 They are somewhat "stable" registers, and to update them everytime
1899 takes so much time, it's just not worthy. Update them in a long
1900 interval to avoid exception.
1902 if (!(time_after(jiffies, data->last_nonvolatile + HZ * 300)
1903 || !data->valid))
1904 return;
1905 /* update voltage limits */
1906 for (i = 1; i < 3; i++) {
1907 for (j = 0; j < ARRAY_SIZE(data->in); j++) {
1908 data->in[j][i] =
1909 w83793_read_value(client, W83793_REG_IN[j][i]);
1911 data->in_low_bits[i] =
1912 w83793_read_value(client, W83793_REG_IN_LOW_BITS[i]);
1915 for (i = 0; i < ARRAY_SIZE(data->fan_min); i++) {
1916 /* Update the Fan measured value and limits */
1917 if (!(data->has_fan & (1 << i))) {
1918 continue;
1920 data->fan_min[i] =
1921 w83793_read_value(client, W83793_REG_FAN_MIN(i)) << 8;
1922 data->fan_min[i] |=
1923 w83793_read_value(client, W83793_REG_FAN_MIN(i) + 1);
1926 for (i = 0; i < ARRAY_SIZE(data->temp_fan_map); i++) {
1927 if (!(data->has_temp & (1 << i)))
1928 continue;
1929 data->temp_fan_map[i] =
1930 w83793_read_value(client, W83793_REG_TEMP_FAN_MAP(i));
1931 for (j = 1; j < 5; j++) {
1932 data->temp[i][j] =
1933 w83793_read_value(client, W83793_REG_TEMP[i][j]);
1935 data->temp_cruise[i] =
1936 w83793_read_value(client, W83793_REG_TEMP_CRUISE(i));
1937 for (j = 0; j < 7; j++) {
1938 data->sf2_pwm[i][j] =
1939 w83793_read_value(client, W83793_REG_SF2_PWM(i, j));
1940 data->sf2_temp[i][j] =
1941 w83793_read_value(client,
1942 W83793_REG_SF2_TEMP(i, j));
1946 for (i = 0; i < ARRAY_SIZE(data->temp_mode); i++)
1947 data->temp_mode[i] =
1948 w83793_read_value(client, W83793_REG_TEMP_MODE[i]);
1950 for (i = 0; i < ARRAY_SIZE(data->tolerance); i++) {
1951 data->tolerance[i] =
1952 w83793_read_value(client, W83793_REG_TEMP_TOL(i));
1955 for (i = 0; i < ARRAY_SIZE(data->pwm); i++) {
1956 if (!(data->has_pwm & (1 << i)))
1957 continue;
1958 data->pwm[i][PWM_NONSTOP] =
1959 w83793_read_value(client, W83793_REG_PWM(i, PWM_NONSTOP));
1960 data->pwm[i][PWM_START] =
1961 w83793_read_value(client, W83793_REG_PWM(i, PWM_START));
1962 data->pwm_stop_time[i] =
1963 w83793_read_value(client, W83793_REG_PWM_STOP_TIME(i));
1966 data->pwm_default = w83793_read_value(client, W83793_REG_PWM_DEFAULT);
1967 data->pwm_enable = w83793_read_value(client, W83793_REG_PWM_ENABLE);
1968 data->pwm_uptime = w83793_read_value(client, W83793_REG_PWM_UPTIME);
1969 data->pwm_downtime = w83793_read_value(client, W83793_REG_PWM_DOWNTIME);
1970 data->temp_critical =
1971 w83793_read_value(client, W83793_REG_TEMP_CRITICAL);
1972 data->beep_enable = w83793_read_value(client, W83793_REG_OVT_BEEP);
1974 for (i = 0; i < ARRAY_SIZE(data->beeps); i++) {
1975 data->beeps[i] = w83793_read_value(client, W83793_REG_BEEP(i));
1978 data->last_nonvolatile = jiffies;
1981 static struct w83793_data *w83793_update_device(struct device *dev)
1983 struct i2c_client *client = to_i2c_client(dev);
1984 struct w83793_data *data = i2c_get_clientdata(client);
1985 int i;
1987 mutex_lock(&data->update_lock);
1989 if (!(time_after(jiffies, data->last_updated + HZ * 2)
1990 || !data->valid))
1991 goto END;
1993 /* Update the voltages measured value and limits */
1994 for (i = 0; i < ARRAY_SIZE(data->in); i++)
1995 data->in[i][IN_READ] =
1996 w83793_read_value(client, W83793_REG_IN[i][IN_READ]);
1998 data->in_low_bits[IN_READ] =
1999 w83793_read_value(client, W83793_REG_IN_LOW_BITS[IN_READ]);
2001 for (i = 0; i < ARRAY_SIZE(data->fan); i++) {
2002 if (!(data->has_fan & (1 << i))) {
2003 continue;
2005 data->fan[i] =
2006 w83793_read_value(client, W83793_REG_FAN(i)) << 8;
2007 data->fan[i] |=
2008 w83793_read_value(client, W83793_REG_FAN(i) + 1);
2011 for (i = 0; i < ARRAY_SIZE(data->temp); i++) {
2012 if (!(data->has_temp & (1 << i)))
2013 continue;
2014 data->temp[i][TEMP_READ] =
2015 w83793_read_value(client, W83793_REG_TEMP[i][TEMP_READ]);
2018 data->temp_low_bits =
2019 w83793_read_value(client, W83793_REG_TEMP_LOW_BITS);
2021 for (i = 0; i < ARRAY_SIZE(data->pwm); i++) {
2022 if (data->has_pwm & (1 << i))
2023 data->pwm[i][PWM_DUTY] =
2024 w83793_read_value(client,
2025 W83793_REG_PWM(i, PWM_DUTY));
2028 for (i = 0; i < ARRAY_SIZE(data->alarms); i++)
2029 data->alarms[i] =
2030 w83793_read_value(client, W83793_REG_ALARM(i));
2031 if (data->has_vid & 0x01)
2032 data->vid[0] = w83793_read_value(client, W83793_REG_VID_INA);
2033 if (data->has_vid & 0x02)
2034 data->vid[1] = w83793_read_value(client, W83793_REG_VID_INB);
2035 w83793_update_nonvolatile(dev);
2036 data->last_updated = jiffies;
2037 data->valid = 1;
2039 END:
2040 mutex_unlock(&data->update_lock);
2041 return data;
2044 /* Ignore the possibility that somebody change bank outside the driver
2045 Must be called with data->update_lock held, except during initialization */
2046 static u8 w83793_read_value(struct i2c_client *client, u16 reg)
2048 struct w83793_data *data = i2c_get_clientdata(client);
2049 u8 res = 0xff;
2050 u8 new_bank = reg >> 8;
2052 new_bank |= data->bank & 0xfc;
2053 if (data->bank != new_bank) {
2054 if (i2c_smbus_write_byte_data
2055 (client, W83793_REG_BANKSEL, new_bank) >= 0)
2056 data->bank = new_bank;
2057 else {
2058 dev_err(&client->dev,
2059 "set bank to %d failed, fall back "
2060 "to bank %d, read reg 0x%x error\n",
2061 new_bank, data->bank, reg);
2062 res = 0x0; /* read 0x0 from the chip */
2063 goto END;
2066 res = i2c_smbus_read_byte_data(client, reg & 0xff);
2067 END:
2068 return res;
2071 /* Must be called with data->update_lock held, except during initialization */
2072 static int w83793_write_value(struct i2c_client *client, u16 reg, u8 value)
2074 struct w83793_data *data = i2c_get_clientdata(client);
2075 int res;
2076 u8 new_bank = reg >> 8;
2078 new_bank |= data->bank & 0xfc;
2079 if (data->bank != new_bank) {
2080 if ((res = i2c_smbus_write_byte_data
2081 (client, W83793_REG_BANKSEL, new_bank)) >= 0)
2082 data->bank = new_bank;
2083 else {
2084 dev_err(&client->dev,
2085 "set bank to %d failed, fall back "
2086 "to bank %d, write reg 0x%x error\n",
2087 new_bank, data->bank, reg);
2088 goto END;
2092 res = i2c_smbus_write_byte_data(client, reg & 0xff, value);
2093 END:
2094 return res;
2097 static int __init sensors_w83793_init(void)
2099 return i2c_add_driver(&w83793_driver);
2102 static void __exit sensors_w83793_exit(void)
2104 i2c_del_driver(&w83793_driver);
2107 MODULE_AUTHOR("Yuan Mu, Sven Anders");
2108 MODULE_DESCRIPTION("w83793 driver");
2109 MODULE_LICENSE("GPL");
2111 module_init(sensors_w83793_init);
2112 module_exit(sensors_w83793_exit);