2 w83793.c - Linux kernel driver for hardware monitoring
3 Copyright (C) 2006 Winbond Electronics Corp.
5 Rudolf Marek <r.marek@assembler.cz>
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation - version 2.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
23 Supports following chips:
25 Chip #vin #fanin #pwm #temp wchipid vendid i2c ISA
26 w83793 10 12 8 6 0x7b 0x5ca3 yes no
29 #include <linux/module.h>
30 #include <linux/init.h>
31 #include <linux/slab.h>
32 #include <linux/i2c.h>
33 #include <linux/hwmon.h>
34 #include <linux/hwmon-vid.h>
35 #include <linux/hwmon-sysfs.h>
36 #include <linux/err.h>
37 #include <linux/mutex.h>
39 /* Addresses to scan */
40 static unsigned short normal_i2c[] = { 0x2c, 0x2d, 0x2e, 0x2f, I2C_CLIENT_END };
42 /* Insmod parameters */
43 I2C_CLIENT_INSMOD_1(w83793);
44 I2C_CLIENT_MODULE_PARM(force_subclients, "List of subclient addresses: "
45 "{bus, clientaddr, subclientaddr1, subclientaddr2}");
48 module_param(reset, bool, 0);
49 MODULE_PARM_DESC(reset, "Set to 1 to reset chip, not recommended");
52 Address 0x00, 0x0d, 0x0e, 0x0f in all three banks are reserved
53 as ID, Bank Select registers
55 #define W83793_REG_BANKSEL 0x00
56 #define W83793_REG_VENDORID 0x0d
57 #define W83793_REG_CHIPID 0x0e
58 #define W83793_REG_DEVICEID 0x0f
60 #define W83793_REG_CONFIG 0x40
61 #define W83793_REG_MFC 0x58
62 #define W83793_REG_FANIN_CTRL 0x5c
63 #define W83793_REG_FANIN_SEL 0x5d
64 #define W83793_REG_I2C_ADDR 0x0b
65 #define W83793_REG_I2C_SUBADDR 0x0c
66 #define W83793_REG_VID_INA 0x05
67 #define W83793_REG_VID_INB 0x06
68 #define W83793_REG_VID_LATCHA 0x07
69 #define W83793_REG_VID_LATCHB 0x08
70 #define W83793_REG_VID_CTRL 0x59
72 static u16 W83793_REG_TEMP_MODE[2] = { 0x5e, 0x5f };
76 #define TEMP_CRIT_HYST 2
78 #define TEMP_WARN_HYST 4
79 /* only crit and crit_hyst affect real-time alarm status
80 current crit crit_hyst warn warn_hyst */
81 static u16 W83793_REG_TEMP[][5] = {
82 {0x1c, 0x78, 0x79, 0x7a, 0x7b},
83 {0x1d, 0x7c, 0x7d, 0x7e, 0x7f},
84 {0x1e, 0x80, 0x81, 0x82, 0x83},
85 {0x1f, 0x84, 0x85, 0x86, 0x87},
86 {0x20, 0x88, 0x89, 0x8a, 0x8b},
87 {0x21, 0x8c, 0x8d, 0x8e, 0x8f},
90 #define W83793_REG_TEMP_LOW_BITS 0x22
92 #define W83793_REG_BEEP(index) (0x53 + (index))
93 #define W83793_REG_ALARM(index) (0x4b + (index))
95 #define W83793_REG_CLR_CHASSIS 0x4a /* SMI MASK4 */
96 #define W83793_REG_IRQ_CTRL 0x50
97 #define W83793_REG_OVT_CTRL 0x51
98 #define W83793_REG_OVT_BEEP 0x52
103 static const u16 W83793_REG_IN[][3] = {
104 /* Current, High, Low */
105 {0x10, 0x60, 0x61}, /* Vcore A */
106 {0x11, 0x62, 0x63}, /* Vcore B */
107 {0x12, 0x64, 0x65}, /* Vtt */
108 {0x14, 0x6a, 0x6b}, /* VSEN1 */
109 {0x15, 0x6c, 0x6d}, /* VSEN2 */
110 {0x16, 0x6e, 0x6f}, /* +3VSEN */
111 {0x17, 0x70, 0x71}, /* +12VSEN */
112 {0x18, 0x72, 0x73}, /* 5VDD */
113 {0x19, 0x74, 0x75}, /* 5VSB */
114 {0x1a, 0x76, 0x77}, /* VBAT */
117 /* Low Bits of Vcore A/B Vtt Read/High/Low */
118 static const u16 W83793_REG_IN_LOW_BITS[] = { 0x1b, 0x68, 0x69 };
119 static u8 scale_in[] = { 2, 2, 2, 16, 16, 16, 8, 24, 24, 16 };
121 #define W83793_REG_FAN(index) (0x23 + 2 * (index)) /* High byte */
122 #define W83793_REG_FAN_MIN(index) (0x90 + 2 * (index)) /* High byte */
124 #define W83793_REG_PWM_DEFAULT 0xb2
125 #define W83793_REG_PWM_ENABLE 0x207
126 #define W83793_REG_PWM_UPTIME 0xc3 /* Unit in 0.1 second */
127 #define W83793_REG_PWM_DOWNTIME 0xc4 /* Unit in 0.1 second */
128 #define W83793_REG_TEMP_CRITICAL 0xc5
132 #define PWM_NONSTOP 2
133 #define W83793_REG_PWM(index, nr) (((nr) == 0 ? 0xb3 : \
134 (nr) == 1 ? 0x220 : 0x218) + (index))
136 /* bit field, fan1 is bit0, fan2 is bit1 ... */
137 #define W83793_REG_TEMP_FAN_MAP(index) (0x201 + (index))
138 #define W83793_REG_TEMP_TOL(index) (0x208 + (index))
139 #define W83793_REG_TEMP_CRUISE(index) (0x210 + (index))
140 #define W83793_REG_PWM_STOP_TIME(index) (0x228 + (index))
141 #define W83793_REG_SF2_TEMP(index, nr) (0x230 + ((index) << 4) + (nr))
142 #define W83793_REG_SF2_PWM(index, nr) (0x238 + ((index) << 4) + (nr))
144 static inline unsigned long FAN_FROM_REG(u16 val)
146 if ((val >= 0xfff) || (val == 0))
148 return (1350000UL / val);
151 static inline u16 FAN_TO_REG(long rpm)
155 return SENSORS_LIMIT((1350000 + (rpm >> 1)) / rpm, 1, 0xffe);
158 static inline unsigned long TIME_FROM_REG(u8 reg)
163 static inline u8 TIME_TO_REG(unsigned long val)
165 return SENSORS_LIMIT((val + 50) / 100, 0, 0xff);
168 static inline long TEMP_FROM_REG(s8 reg)
173 static inline s8 TEMP_TO_REG(long val, s8 min, s8 max)
175 return SENSORS_LIMIT((val + (val < 0 ? -500 : 500)) / 1000, min, max);
179 struct i2c_client client;
180 struct i2c_client *lm75[2];
181 struct class_device *class_dev;
182 struct mutex update_lock;
183 char valid; /* !=0 if following fields are valid */
184 unsigned long last_updated; /* In jiffies */
185 unsigned long last_nonvolatile; /* In jiffies, last time we update the
186 nonvolatile registers */
191 u8 in[10][3]; /* Register value, read/high/low */
192 u8 in_low_bits[3]; /* Additional resolution for VCore A/B Vtt */
194 u16 has_fan; /* Only fan1- fan5 has own pins */
195 u16 fan[12]; /* Register value combine */
196 u16 fan_min[12]; /* Register value combine */
198 s8 temp[6][5]; /* current, crit, crit_hyst,warn, warn_hyst */
199 u8 temp_low_bits; /* Additional resolution TD1-TD4 */
200 u8 temp_mode[2]; /* byte 0: Temp D1-D4 mode each has 2 bits
201 byte 1: Temp R1,R2 mode, each has 1 bit */
202 u8 temp_critical; /* If reached all fan will be at full speed */
203 u8 temp_fan_map[6]; /* Temp controls which pwm fan, bit field */
206 u8 pwm_enable; /* Register value, each Temp has 1 bit */
207 u8 pwm_uptime; /* Register value */
208 u8 pwm_downtime; /* Register value */
209 u8 pwm_default; /* All fan default pwm, next poweron valid */
210 u8 pwm[8][3]; /* Register value */
214 u8 alarms[5]; /* realtime status registers */
217 u8 tolerance[3]; /* Temp tolerance(Smart Fan I/II) */
218 u8 sf2_pwm[6][7]; /* Smart FanII: Fan duty cycle */
219 u8 sf2_temp[6][7]; /* Smart FanII: Temp level point */
222 static u8 w83793_read_value(struct i2c_client *client, u16 reg);
223 static int w83793_write_value(struct i2c_client *client, u16 reg, u8 value);
224 static int w83793_attach_adapter(struct i2c_adapter *adapter);
225 static int w83793_detect(struct i2c_adapter *adapter, int address, int kind);
226 static int w83793_detach_client(struct i2c_client *client);
227 static void w83793_init_client(struct i2c_client *client);
228 static void w83793_update_nonvolatile(struct device *dev);
229 static struct w83793_data *w83793_update_device(struct device *dev);
231 static struct i2c_driver w83793_driver = {
235 .attach_adapter = w83793_attach_adapter,
236 .detach_client = w83793_detach_client,
240 show_vrm(struct device *dev, struct device_attribute *attr, char *buf)
242 struct i2c_client *client = to_i2c_client(dev);
243 struct w83793_data *data = i2c_get_clientdata(client);
245 return sprintf(buf, "%d\n", data->vrm);
249 show_vid(struct device *dev, struct device_attribute *attr, char *buf)
251 struct w83793_data *data = w83793_update_device(dev);
252 struct sensor_device_attribute_2 *sensor_attr =
253 to_sensor_dev_attr_2(attr);
254 int index = sensor_attr->index;
256 return sprintf(buf, "%d\n", vid_from_reg(data->vid[index], data->vrm));
260 store_vrm(struct device *dev, struct device_attribute *attr,
261 const char *buf, size_t count)
263 struct i2c_client *client = to_i2c_client(dev);
264 struct w83793_data *data = i2c_get_clientdata(client);
266 data->vrm = simple_strtoul(buf, NULL, 10);
270 #define ALARM_STATUS 0
271 #define BEEP_ENABLE 1
273 show_alarm_beep(struct device *dev, struct device_attribute *attr, char *buf)
275 struct w83793_data *data = w83793_update_device(dev);
276 struct sensor_device_attribute_2 *sensor_attr =
277 to_sensor_dev_attr_2(attr);
278 int nr = sensor_attr->nr;
279 int index = sensor_attr->index >> 3;
280 int bit = sensor_attr->index & 0x07;
283 if (ALARM_STATUS == nr) {
284 val = (data->alarms[index] >> (bit)) & 1;
285 } else { /* BEEP_ENABLE */
286 val = (data->beeps[index] >> (bit)) & 1;
289 return sprintf(buf, "%u\n", val);
293 store_beep(struct device *dev, struct device_attribute *attr,
294 const char *buf, size_t count)
296 struct i2c_client *client = to_i2c_client(dev);
297 struct w83793_data *data = i2c_get_clientdata(client);
298 struct sensor_device_attribute_2 *sensor_attr =
299 to_sensor_dev_attr_2(attr);
300 int index = sensor_attr->index >> 3;
301 int shift = sensor_attr->index & 0x07;
302 u8 beep_bit = 1 << shift;
305 val = simple_strtoul(buf, NULL, 10);
306 if (val != 0 && val != 1)
309 mutex_lock(&data->update_lock);
310 data->beeps[index] = w83793_read_value(client, W83793_REG_BEEP(index));
311 data->beeps[index] &= ~beep_bit;
312 data->beeps[index] |= val << shift;
313 w83793_write_value(client, W83793_REG_BEEP(index), data->beeps[index]);
314 mutex_unlock(&data->update_lock);
320 show_beep_enable(struct device *dev, struct device_attribute *attr, char *buf)
322 struct w83793_data *data = w83793_update_device(dev);
323 return sprintf(buf, "%u\n", (data->beep_enable >> 1) & 0x01);
327 store_beep_enable(struct device *dev, struct device_attribute *attr,
328 const char *buf, size_t count)
330 struct i2c_client *client = to_i2c_client(dev);
331 struct w83793_data *data = i2c_get_clientdata(client);
332 u8 val = simple_strtoul(buf, NULL, 10);
334 if (val != 0 && val != 1)
337 mutex_lock(&data->update_lock);
338 data->beep_enable = w83793_read_value(client, W83793_REG_OVT_BEEP)
340 data->beep_enable |= val << 1;
341 w83793_write_value(client, W83793_REG_OVT_BEEP, data->beep_enable);
342 mutex_unlock(&data->update_lock);
347 /* Write any value to clear chassis alarm */
349 store_chassis_clear(struct device *dev,
350 struct device_attribute *attr, const char *buf,
353 struct i2c_client *client = to_i2c_client(dev);
354 struct w83793_data *data = i2c_get_clientdata(client);
357 mutex_lock(&data->update_lock);
358 val = w83793_read_value(client, W83793_REG_CLR_CHASSIS);
360 w83793_write_value(client, W83793_REG_CLR_CHASSIS, val);
361 mutex_unlock(&data->update_lock);
368 show_fan(struct device *dev, struct device_attribute *attr, char *buf)
370 struct sensor_device_attribute_2 *sensor_attr =
371 to_sensor_dev_attr_2(attr);
372 int nr = sensor_attr->nr;
373 int index = sensor_attr->index;
374 struct w83793_data *data = w83793_update_device(dev);
377 if (FAN_INPUT == nr) {
378 val = data->fan[index] & 0x0fff;
380 val = data->fan_min[index] & 0x0fff;
383 return sprintf(buf, "%lu\n", FAN_FROM_REG(val));
387 store_fan_min(struct device *dev, struct device_attribute *attr,
388 const char *buf, size_t count)
390 struct sensor_device_attribute_2 *sensor_attr =
391 to_sensor_dev_attr_2(attr);
392 int index = sensor_attr->index;
393 struct i2c_client *client = to_i2c_client(dev);
394 struct w83793_data *data = i2c_get_clientdata(client);
395 u16 val = FAN_TO_REG(simple_strtoul(buf, NULL, 10));
397 mutex_lock(&data->update_lock);
398 data->fan_min[index] = val;
399 w83793_write_value(client, W83793_REG_FAN_MIN(index),
401 w83793_write_value(client, W83793_REG_FAN_MIN(index) + 1, val & 0xff);
402 mutex_unlock(&data->update_lock);
409 #define PWM_NONSTOP 2
410 #define PWM_STOP_TIME 3
412 show_pwm(struct device *dev, struct device_attribute *attr, char *buf)
414 struct sensor_device_attribute_2 *sensor_attr =
415 to_sensor_dev_attr_2(attr);
416 struct w83793_data *data = w83793_update_device(dev);
418 int nr = sensor_attr->nr;
419 int index = sensor_attr->index;
421 if (PWM_STOP_TIME == nr)
422 val = TIME_FROM_REG(data->pwm_stop_time[index]);
424 val = (data->pwm[index][nr] & 0x3f) << 2;
426 return sprintf(buf, "%d\n", val);
430 store_pwm(struct device *dev, struct device_attribute *attr,
431 const char *buf, size_t count)
433 struct i2c_client *client = to_i2c_client(dev);
434 struct w83793_data *data = i2c_get_clientdata(client);
435 struct sensor_device_attribute_2 *sensor_attr =
436 to_sensor_dev_attr_2(attr);
437 int nr = sensor_attr->nr;
438 int index = sensor_attr->index;
441 mutex_lock(&data->update_lock);
442 if (PWM_STOP_TIME == nr) {
443 val = TIME_TO_REG(simple_strtoul(buf, NULL, 10));
444 data->pwm_stop_time[index] = val;
445 w83793_write_value(client, W83793_REG_PWM_STOP_TIME(index),
448 val = SENSORS_LIMIT(simple_strtoul(buf, NULL, 10), 0, 0xff)
450 data->pwm[index][nr] =
451 w83793_read_value(client, W83793_REG_PWM(index, nr)) & 0xc0;
452 data->pwm[index][nr] |= val;
453 w83793_write_value(client, W83793_REG_PWM(index, nr),
454 data->pwm[index][nr]);
457 mutex_unlock(&data->update_lock);
462 show_temp(struct device *dev, struct device_attribute *attr, char *buf)
464 struct sensor_device_attribute_2 *sensor_attr =
465 to_sensor_dev_attr_2(attr);
466 int nr = sensor_attr->nr;
467 int index = sensor_attr->index;
468 struct w83793_data *data = w83793_update_device(dev);
469 long temp = TEMP_FROM_REG(data->temp[index][nr]);
471 if (TEMP_READ == nr && index < 4) { /* Only TD1-TD4 have low bits */
472 int low = ((data->temp_low_bits >> (index * 2)) & 0x03) * 250;
473 temp += temp > 0 ? low : -low;
475 return sprintf(buf, "%ld\n", temp);
479 store_temp(struct device *dev, struct device_attribute *attr,
480 const char *buf, size_t count)
482 struct sensor_device_attribute_2 *sensor_attr =
483 to_sensor_dev_attr_2(attr);
484 int nr = sensor_attr->nr;
485 int index = sensor_attr->index;
486 struct i2c_client *client = to_i2c_client(dev);
487 struct w83793_data *data = i2c_get_clientdata(client);
488 long tmp = simple_strtol(buf, NULL, 10);
490 mutex_lock(&data->update_lock);
491 data->temp[index][nr] = TEMP_TO_REG(tmp, -128, 127);
492 w83793_write_value(client, W83793_REG_TEMP[index][nr],
493 data->temp[index][nr]);
494 mutex_unlock(&data->update_lock);
500 each has 4 mode:(2 bits)
502 1: Use internal temp sensor(default)
503 2: Use sensor in AMD CPU and get result by AMDSI
504 3: Use sensor in Intel CPU and get result by PECI
507 each has 2 mode:(1 bit)
508 0: Disable temp sensor monitor
509 1: To enable temp sensors monitor
512 /* 0 disable, 5 AMDSI, 6 PECI */
513 static u8 TO_TEMP_MODE[] = { 0, 0, 5, 6 };
516 show_temp_mode(struct device *dev, struct device_attribute *attr, char *buf)
518 struct w83793_data *data = w83793_update_device(dev);
519 struct sensor_device_attribute_2 *sensor_attr =
520 to_sensor_dev_attr_2(attr);
521 int index = sensor_attr->index;
522 u8 mask = (index < 4) ? 0x03 : 0x01;
523 u8 shift = (index < 4) ? (2 * index) : (index - 4);
525 index = (index < 4) ? 0 : 1;
527 tmp = (data->temp_mode[index] >> shift) & mask;
529 /* for the internal sensor, found out if diode or thermistor */
531 tmp = index == 0 ? 3 : 4;
533 tmp = TO_TEMP_MODE[tmp];
536 return sprintf(buf, "%d\n", tmp);
540 store_temp_mode(struct device *dev, struct device_attribute *attr,
541 const char *buf, size_t count)
543 struct i2c_client *client = to_i2c_client(dev);
544 struct w83793_data *data = i2c_get_clientdata(client);
545 struct sensor_device_attribute_2 *sensor_attr =
546 to_sensor_dev_attr_2(attr);
547 int index = sensor_attr->index;
548 u8 mask = (index < 4) ? 0x03 : 0x01;
549 u8 shift = (index < 4) ? (2 * index) : (index - 4);
550 u8 val = simple_strtoul(buf, NULL, 10);
552 /* transform the sysfs interface values into table above */
553 if ((val == 5 || val == 6) && (index < 4)) {
555 } else if ((val == 3 && index < 4)
556 || (val == 4 && index >= 4)
558 /* transform diode or thermistor into internal enable */
564 index = (index < 4) ? 0 : 1;
565 mutex_lock(&data->update_lock);
566 data->temp_mode[index] =
567 w83793_read_value(client, W83793_REG_TEMP_MODE[index]);
568 data->temp_mode[index] &= ~(mask << shift);
569 data->temp_mode[index] |= val << shift;
570 w83793_write_value(client, W83793_REG_TEMP_MODE[index],
571 data->temp_mode[index]);
572 mutex_unlock(&data->update_lock);
577 #define SETUP_PWM_DEFAULT 0
578 #define SETUP_PWM_UPTIME 1 /* Unit in 0.1s */
579 #define SETUP_PWM_DOWNTIME 2 /* Unit in 0.1s */
580 #define SETUP_TEMP_CRITICAL 3
582 show_sf_setup(struct device *dev, struct device_attribute *attr, char *buf)
584 struct sensor_device_attribute_2 *sensor_attr =
585 to_sensor_dev_attr_2(attr);
586 int nr = sensor_attr->nr;
587 struct w83793_data *data = w83793_update_device(dev);
590 if (SETUP_PWM_DEFAULT == nr) {
591 val = (data->pwm_default & 0x3f) << 2;
592 } else if (SETUP_PWM_UPTIME == nr) {
593 val = TIME_FROM_REG(data->pwm_uptime);
594 } else if (SETUP_PWM_DOWNTIME == nr) {
595 val = TIME_FROM_REG(data->pwm_downtime);
596 } else if (SETUP_TEMP_CRITICAL == nr) {
597 val = TEMP_FROM_REG(data->temp_critical & 0x7f);
600 return sprintf(buf, "%d\n", val);
604 store_sf_setup(struct device *dev, struct device_attribute *attr,
605 const char *buf, size_t count)
607 struct sensor_device_attribute_2 *sensor_attr =
608 to_sensor_dev_attr_2(attr);
609 int nr = sensor_attr->nr;
610 struct i2c_client *client = to_i2c_client(dev);
611 struct w83793_data *data = i2c_get_clientdata(client);
613 mutex_lock(&data->update_lock);
614 if (SETUP_PWM_DEFAULT == nr) {
616 w83793_read_value(client, W83793_REG_PWM_DEFAULT) & 0xc0;
617 data->pwm_default |= SENSORS_LIMIT(simple_strtoul(buf, NULL,
620 w83793_write_value(client, W83793_REG_PWM_DEFAULT,
622 } else if (SETUP_PWM_UPTIME == nr) {
623 data->pwm_uptime = TIME_TO_REG(simple_strtoul(buf, NULL, 10));
624 data->pwm_uptime += data->pwm_uptime == 0 ? 1 : 0;
625 w83793_write_value(client, W83793_REG_PWM_UPTIME,
627 } else if (SETUP_PWM_DOWNTIME == nr) {
628 data->pwm_downtime = TIME_TO_REG(simple_strtoul(buf, NULL, 10));
629 data->pwm_downtime += data->pwm_downtime == 0 ? 1 : 0;
630 w83793_write_value(client, W83793_REG_PWM_DOWNTIME,
632 } else { /* SETUP_TEMP_CRITICAL */
633 data->temp_critical =
634 w83793_read_value(client, W83793_REG_TEMP_CRITICAL) & 0x80;
635 data->temp_critical |= TEMP_TO_REG(simple_strtol(buf, NULL, 10),
637 w83793_write_value(client, W83793_REG_TEMP_CRITICAL,
638 data->temp_critical);
641 mutex_unlock(&data->update_lock);
646 Temp SmartFan control
648 Temp channel control which pwm fan, bitfield, bit 0 indicate pwm1...
649 It's possible two or more temp channels control the same fan, w83793
650 always prefers to pick the most critical request and applies it to
652 It's possible one fan is not in any mapping of 6 temp channels, this
653 means the fan is manual mode
656 Each temp channel has its own SmartFan mode, and temp channel
657 control fans that are set by TEMP_FAN_MAP
659 1: Thermal Cruise Mode
662 Target temperature in thermal cruise mode, w83793 will try to turn
663 fan speed to keep the temperature of target device around this
667 If Temp higher or lower than target with this tolerance, w83793
668 will take actions to speed up or slow down the fan to keep the
669 temperature within the tolerance range.
672 #define TEMP_FAN_MAP 0
673 #define TEMP_PWM_ENABLE 1
674 #define TEMP_CRUISE 2
675 #define TEMP_TOLERANCE 3
677 show_sf_ctrl(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 int index = sensor_attr->index;
683 struct w83793_data *data = w83793_update_device(dev);
686 if (TEMP_FAN_MAP == nr) {
687 val = data->temp_fan_map[index];
688 } else if (TEMP_PWM_ENABLE == nr) {
689 /* +2 to transfrom into 2 and 3 to conform with sysfs intf */
690 val = ((data->pwm_enable >> index) & 0x01) + 2;
691 } else if (TEMP_CRUISE == nr) {
692 val = TEMP_FROM_REG(data->temp_cruise[index] & 0x7f);
693 } else { /* TEMP_TOLERANCE */
694 val = data->tolerance[index >> 1] >> ((index & 0x01) ? 4 : 0);
695 val = TEMP_FROM_REG(val & 0x0f);
697 return sprintf(buf, "%d\n", val);
701 store_sf_ctrl(struct device *dev, struct device_attribute *attr,
702 const char *buf, size_t count)
704 struct sensor_device_attribute_2 *sensor_attr =
705 to_sensor_dev_attr_2(attr);
706 int nr = sensor_attr->nr;
707 int index = sensor_attr->index;
708 struct i2c_client *client = to_i2c_client(dev);
709 struct w83793_data *data = i2c_get_clientdata(client);
712 mutex_lock(&data->update_lock);
713 if (TEMP_FAN_MAP == nr) {
714 val = simple_strtoul(buf, NULL, 10) & 0xff;
715 w83793_write_value(client, W83793_REG_TEMP_FAN_MAP(index), val);
716 data->temp_fan_map[index] = val;
717 } else if (TEMP_PWM_ENABLE == nr) {
718 val = simple_strtoul(buf, NULL, 10);
719 if (2 == val || 3 == val) {
721 w83793_read_value(client, W83793_REG_PWM_ENABLE);
723 data->pwm_enable |= 1 << index;
725 data->pwm_enable &= ~(1 << index);
726 w83793_write_value(client, W83793_REG_PWM_ENABLE,
729 mutex_unlock(&data->update_lock);
732 } else if (TEMP_CRUISE == nr) {
733 data->temp_cruise[index] =
734 w83793_read_value(client, W83793_REG_TEMP_CRUISE(index));
735 val = TEMP_TO_REG(simple_strtol(buf, NULL, 10), 0, 0x7f);
736 data->temp_cruise[index] &= 0x80;
737 data->temp_cruise[index] |= val;
739 w83793_write_value(client, W83793_REG_TEMP_CRUISE(index),
740 data->temp_cruise[index]);
741 } else { /* TEMP_TOLERANCE */
743 u8 shift = (index & 0x01) ? 4 : 0;
745 w83793_read_value(client, W83793_REG_TEMP_TOL(i));
747 val = TEMP_TO_REG(simple_strtol(buf, NULL, 10), 0, 0x0f);
748 data->tolerance[i] &= ~(0x0f << shift);
749 data->tolerance[i] |= val << shift;
750 w83793_write_value(client, W83793_REG_TEMP_TOL(i),
754 mutex_unlock(&data->update_lock);
759 show_sf2_pwm(struct device *dev, struct device_attribute *attr, char *buf)
761 struct sensor_device_attribute_2 *sensor_attr =
762 to_sensor_dev_attr_2(attr);
763 int nr = sensor_attr->nr;
764 int index = sensor_attr->index;
765 struct w83793_data *data = w83793_update_device(dev);
767 return sprintf(buf, "%d\n", (data->sf2_pwm[index][nr] & 0x3f) << 2);
771 store_sf2_pwm(struct device *dev, struct device_attribute *attr,
772 const char *buf, size_t count)
774 struct i2c_client *client = to_i2c_client(dev);
775 struct w83793_data *data = i2c_get_clientdata(client);
776 struct sensor_device_attribute_2 *sensor_attr =
777 to_sensor_dev_attr_2(attr);
778 int nr = sensor_attr->nr;
779 int index = sensor_attr->index;
780 u8 val = SENSORS_LIMIT(simple_strtoul(buf, NULL, 10), 0, 0xff) >> 2;
782 mutex_lock(&data->update_lock);
783 data->sf2_pwm[index][nr] =
784 w83793_read_value(client, W83793_REG_SF2_PWM(index, nr)) & 0xc0;
785 data->sf2_pwm[index][nr] |= val;
786 w83793_write_value(client, W83793_REG_SF2_PWM(index, nr),
787 data->sf2_pwm[index][nr]);
788 mutex_unlock(&data->update_lock);
793 show_sf2_temp(struct device *dev, struct device_attribute *attr, char *buf)
795 struct sensor_device_attribute_2 *sensor_attr =
796 to_sensor_dev_attr_2(attr);
797 int nr = sensor_attr->nr;
798 int index = sensor_attr->index;
799 struct w83793_data *data = w83793_update_device(dev);
801 return sprintf(buf, "%ld\n",
802 TEMP_FROM_REG(data->sf2_temp[index][nr] & 0x7f));
806 store_sf2_temp(struct device *dev, struct device_attribute *attr,
807 const char *buf, size_t count)
809 struct i2c_client *client = to_i2c_client(dev);
810 struct w83793_data *data = i2c_get_clientdata(client);
811 struct sensor_device_attribute_2 *sensor_attr =
812 to_sensor_dev_attr_2(attr);
813 int nr = sensor_attr->nr;
814 int index = sensor_attr->index;
815 u8 val = TEMP_TO_REG(simple_strtol(buf, NULL, 10), 0, 0x7f);
817 mutex_lock(&data->update_lock);
818 data->sf2_temp[index][nr] =
819 w83793_read_value(client, W83793_REG_SF2_TEMP(index, nr)) & 0x80;
820 data->sf2_temp[index][nr] |= val;
821 w83793_write_value(client, W83793_REG_SF2_TEMP(index, nr),
822 data->sf2_temp[index][nr]);
823 mutex_unlock(&data->update_lock);
827 /* only Vcore A/B and Vtt have additional 2 bits precision */
829 show_in(struct device *dev, struct device_attribute *attr, char *buf)
831 struct sensor_device_attribute_2 *sensor_attr =
832 to_sensor_dev_attr_2(attr);
833 int nr = sensor_attr->nr;
834 int index = sensor_attr->index;
835 struct w83793_data *data = w83793_update_device(dev);
836 u16 val = data->in[index][nr];
840 val += (data->in_low_bits[nr] >> (index * 2)) & 0x3;
842 return sprintf(buf, "%d\n", val * scale_in[index]);
846 store_in(struct device *dev, struct device_attribute *attr,
847 const char *buf, size_t count)
849 struct sensor_device_attribute_2 *sensor_attr =
850 to_sensor_dev_attr_2(attr);
851 int nr = sensor_attr->nr;
852 int index = sensor_attr->index;
853 struct i2c_client *client = to_i2c_client(dev);
854 struct w83793_data *data = i2c_get_clientdata(client);
858 (simple_strtoul(buf, NULL, 10) +
859 scale_in[index] / 2) / scale_in[index];
860 mutex_lock(&data->update_lock);
862 val = SENSORS_LIMIT(val, 0, 255);
864 val = SENSORS_LIMIT(val, 0, 0x3FF);
865 data->in_low_bits[nr] =
866 w83793_read_value(client, W83793_REG_IN_LOW_BITS[nr]);
867 data->in_low_bits[nr] &= ~(0x03 << (2 * index));
868 data->in_low_bits[nr] |= (val & 0x03) << (2 * index);
869 w83793_write_value(client, W83793_REG_IN_LOW_BITS[nr],
870 data->in_low_bits[nr]);
873 data->in[index][nr] = val;
874 w83793_write_value(client, W83793_REG_IN[index][nr],
875 data->in[index][nr]);
876 mutex_unlock(&data->update_lock);
882 #define SENSOR_ATTR_IN(index) \
883 SENSOR_ATTR_2(in##index##_input, S_IRUGO, show_in, NULL, \
885 SENSOR_ATTR_2(in##index##_max, S_IRUGO | S_IWUSR, show_in, \
886 store_in, IN_MAX, index), \
887 SENSOR_ATTR_2(in##index##_min, S_IRUGO | S_IWUSR, show_in, \
888 store_in, IN_LOW, index), \
889 SENSOR_ATTR_2(in##index##_alarm, S_IRUGO, show_alarm_beep, \
890 NULL, ALARM_STATUS, index + ((index > 2) ? 1 : 0)), \
891 SENSOR_ATTR_2(in##index##_beep, S_IWUSR | S_IRUGO, \
892 show_alarm_beep, store_beep, BEEP_ENABLE, \
893 index + ((index > 2) ? 1 : 0))
895 #define SENSOR_ATTR_FAN(index) \
896 SENSOR_ATTR_2(fan##index##_alarm, S_IRUGO, show_alarm_beep, \
897 NULL, ALARM_STATUS, index + 17), \
898 SENSOR_ATTR_2(fan##index##_beep, S_IWUSR | S_IRUGO, \
899 show_alarm_beep, store_beep, BEEP_ENABLE, index + 17), \
900 SENSOR_ATTR_2(fan##index##_input, S_IRUGO, show_fan, \
901 NULL, FAN_INPUT, index - 1), \
902 SENSOR_ATTR_2(fan##index##_min, S_IWUSR | S_IRUGO, \
903 show_fan, store_fan_min, FAN_MIN, index - 1)
905 #define SENSOR_ATTR_PWM(index) \
906 SENSOR_ATTR_2(pwm##index, S_IWUSR | S_IRUGO, show_pwm, \
907 store_pwm, PWM_DUTY, index - 1), \
908 SENSOR_ATTR_2(pwm##index##_nonstop, S_IWUSR | S_IRUGO, \
909 show_pwm, store_pwm, PWM_NONSTOP, index - 1), \
910 SENSOR_ATTR_2(pwm##index##_start, S_IWUSR | S_IRUGO, \
911 show_pwm, store_pwm, PWM_START, index - 1), \
912 SENSOR_ATTR_2(pwm##index##_stop_time, S_IWUSR | S_IRUGO, \
913 show_pwm, store_pwm, PWM_STOP_TIME, index - 1)
915 #define SENSOR_ATTR_TEMP(index) \
916 SENSOR_ATTR_2(temp##index##_type, S_IRUGO | S_IWUSR, \
917 show_temp_mode, store_temp_mode, NOT_USED, index - 1), \
918 SENSOR_ATTR_2(temp##index##_input, S_IRUGO, show_temp, \
919 NULL, TEMP_READ, index - 1), \
920 SENSOR_ATTR_2(temp##index##_max, S_IRUGO | S_IWUSR, show_temp, \
921 store_temp, TEMP_CRIT, index - 1), \
922 SENSOR_ATTR_2(temp##index##_max_hyst, S_IRUGO | S_IWUSR, \
923 show_temp, store_temp, TEMP_CRIT_HYST, index - 1), \
924 SENSOR_ATTR_2(temp##index##_warn, S_IRUGO | S_IWUSR, show_temp, \
925 store_temp, TEMP_WARN, index - 1), \
926 SENSOR_ATTR_2(temp##index##_warn_hyst, S_IRUGO | S_IWUSR, \
927 show_temp, store_temp, TEMP_WARN_HYST, index - 1), \
928 SENSOR_ATTR_2(temp##index##_alarm, S_IRUGO, \
929 show_alarm_beep, NULL, ALARM_STATUS, index + 11), \
930 SENSOR_ATTR_2(temp##index##_beep, S_IWUSR | S_IRUGO, \
931 show_alarm_beep, store_beep, BEEP_ENABLE, index + 11), \
932 SENSOR_ATTR_2(temp##index##_auto_channels_pwm, \
933 S_IRUGO | S_IWUSR, show_sf_ctrl, store_sf_ctrl, \
934 TEMP_FAN_MAP, index - 1), \
935 SENSOR_ATTR_2(temp##index##_pwm_enable, S_IWUSR | S_IRUGO, \
936 show_sf_ctrl, store_sf_ctrl, TEMP_PWM_ENABLE, \
938 SENSOR_ATTR_2(thermal_cruise##index, S_IRUGO | S_IWUSR, \
939 show_sf_ctrl, store_sf_ctrl, TEMP_CRUISE, index - 1), \
940 SENSOR_ATTR_2(tolerance##index, S_IRUGO | S_IWUSR, show_sf_ctrl,\
941 store_sf_ctrl, TEMP_TOLERANCE, index - 1), \
942 SENSOR_ATTR_2(temp##index##_auto_point1_pwm, S_IRUGO | S_IWUSR, \
943 show_sf2_pwm, store_sf2_pwm, 0, index - 1), \
944 SENSOR_ATTR_2(temp##index##_auto_point2_pwm, S_IRUGO | S_IWUSR, \
945 show_sf2_pwm, store_sf2_pwm, 1, index - 1), \
946 SENSOR_ATTR_2(temp##index##_auto_point3_pwm, S_IRUGO | S_IWUSR, \
947 show_sf2_pwm, store_sf2_pwm, 2, index - 1), \
948 SENSOR_ATTR_2(temp##index##_auto_point4_pwm, S_IRUGO | S_IWUSR, \
949 show_sf2_pwm, store_sf2_pwm, 3, index - 1), \
950 SENSOR_ATTR_2(temp##index##_auto_point5_pwm, S_IRUGO | S_IWUSR, \
951 show_sf2_pwm, store_sf2_pwm, 4, index - 1), \
952 SENSOR_ATTR_2(temp##index##_auto_point6_pwm, S_IRUGO | S_IWUSR, \
953 show_sf2_pwm, store_sf2_pwm, 5, index - 1), \
954 SENSOR_ATTR_2(temp##index##_auto_point7_pwm, S_IRUGO | S_IWUSR, \
955 show_sf2_pwm, store_sf2_pwm, 6, index - 1), \
956 SENSOR_ATTR_2(temp##index##_auto_point1_temp, S_IRUGO | S_IWUSR,\
957 show_sf2_temp, store_sf2_temp, 0, index - 1), \
958 SENSOR_ATTR_2(temp##index##_auto_point2_temp, S_IRUGO | S_IWUSR,\
959 show_sf2_temp, store_sf2_temp, 1, index - 1), \
960 SENSOR_ATTR_2(temp##index##_auto_point3_temp, S_IRUGO | S_IWUSR,\
961 show_sf2_temp, store_sf2_temp, 2, index - 1), \
962 SENSOR_ATTR_2(temp##index##_auto_point4_temp, S_IRUGO | S_IWUSR,\
963 show_sf2_temp, store_sf2_temp, 3, index - 1), \
964 SENSOR_ATTR_2(temp##index##_auto_point5_temp, S_IRUGO | S_IWUSR,\
965 show_sf2_temp, store_sf2_temp, 4, index - 1), \
966 SENSOR_ATTR_2(temp##index##_auto_point6_temp, S_IRUGO | S_IWUSR,\
967 show_sf2_temp, store_sf2_temp, 5, index - 1), \
968 SENSOR_ATTR_2(temp##index##_auto_point7_temp, S_IRUGO | S_IWUSR,\
969 show_sf2_temp, store_sf2_temp, 6, index - 1)
971 static struct sensor_device_attribute_2 w83793_sensor_attr_2[] = {
999 static struct sensor_device_attribute_2 w83793_left_fan[] = {
1004 SENSOR_ATTR_FAN(10),
1005 SENSOR_ATTR_FAN(11),
1006 SENSOR_ATTR_FAN(12),
1010 static struct sensor_device_attribute_2 w83793_left_pwm[] = {
1018 static struct sensor_device_attribute_2 sda_single_files[] = {
1019 SENSOR_ATTR_2(cpu0_vid, S_IRUGO, show_vid, NULL, NOT_USED, 0),
1020 SENSOR_ATTR_2(cpu1_vid, S_IRUGO, show_vid, NULL, NOT_USED, 1),
1021 SENSOR_ATTR_2(vrm, S_IWUSR | S_IRUGO, show_vrm, store_vrm,
1022 NOT_USED, NOT_USED),
1023 SENSOR_ATTR_2(chassis, S_IWUSR | S_IRUGO, show_alarm_beep,
1024 store_chassis_clear, ALARM_STATUS, 30),
1025 SENSOR_ATTR_2(beep_enable, S_IWUSR | S_IRUGO, show_beep_enable,
1026 store_beep_enable, NOT_USED, NOT_USED),
1027 SENSOR_ATTR_2(pwm_default, S_IWUSR | S_IRUGO, show_sf_setup,
1028 store_sf_setup, SETUP_PWM_DEFAULT, NOT_USED),
1029 SENSOR_ATTR_2(pwm_uptime, S_IWUSR | S_IRUGO, show_sf_setup,
1030 store_sf_setup, SETUP_PWM_UPTIME, NOT_USED),
1031 SENSOR_ATTR_2(pwm_downtime, S_IWUSR | S_IRUGO, show_sf_setup,
1032 store_sf_setup, SETUP_PWM_DOWNTIME, NOT_USED),
1033 SENSOR_ATTR_2(temp_critical, S_IWUSR | S_IRUGO, show_sf_setup,
1034 store_sf_setup, SETUP_TEMP_CRITICAL, NOT_USED),
1037 static void w83793_init_client(struct i2c_client *client)
1040 w83793_write_value(client, W83793_REG_CONFIG, 0x80);
1043 /* Start monitoring */
1044 w83793_write_value(client, W83793_REG_CONFIG,
1045 w83793_read_value(client, W83793_REG_CONFIG) | 0x01);
1049 static int w83793_attach_adapter(struct i2c_adapter *adapter)
1051 if (!(adapter->class & I2C_CLASS_HWMON))
1053 return i2c_probe(adapter, &addr_data, w83793_detect);
1056 static int w83793_detach_client(struct i2c_client *client)
1058 struct w83793_data *data = i2c_get_clientdata(client);
1059 struct device *dev = &client->dev;
1064 hwmon_device_unregister(data->class_dev);
1066 for (i = 0; i < ARRAY_SIZE(w83793_sensor_attr_2); i++)
1067 device_remove_file(dev,
1068 &w83793_sensor_attr_2[i].dev_attr);
1070 for (i = 0; i < ARRAY_SIZE(sda_single_files); i++)
1071 device_remove_file(dev, &sda_single_files[i].dev_attr);
1073 for (i = 0; i < ARRAY_SIZE(w83793_left_fan); i++)
1074 device_remove_file(dev, &w83793_left_fan[i].dev_attr);
1076 for (i = 0; i < ARRAY_SIZE(w83793_left_pwm); i++)
1077 device_remove_file(dev, &w83793_left_pwm[i].dev_attr);
1080 if ((err = i2c_detach_client(client)))
1094 w83793_create_subclient(struct i2c_adapter *adapter,
1095 struct i2c_client *client, int addr,
1096 struct i2c_client **sub_cli)
1099 struct i2c_client *sub_client;
1101 (*sub_cli) = sub_client =
1102 kzalloc(sizeof(struct i2c_client), GFP_KERNEL);
1103 if (!(sub_client)) {
1106 sub_client->addr = 0x48 + addr;
1107 i2c_set_clientdata(sub_client, NULL);
1108 sub_client->adapter = adapter;
1109 sub_client->driver = &w83793_driver;
1110 strlcpy(sub_client->name, "w83793 subclient", I2C_NAME_SIZE);
1111 if ((err = i2c_attach_client(sub_client))) {
1112 dev_err(&client->dev, "subclient registration "
1113 "at address 0x%x failed\n", sub_client->addr);
1120 w83793_detect_subclients(struct i2c_adapter *adapter, int address,
1121 int kind, struct i2c_client *client)
1125 struct w83793_data *data = i2c_get_clientdata(client);
1127 id = i2c_adapter_id(adapter);
1128 if (force_subclients[0] == id && force_subclients[1] == address) {
1129 for (i = 2; i <= 3; i++) {
1130 if (force_subclients[i] < 0x48
1131 || force_subclients[i] > 0x4f) {
1132 dev_err(&client->dev,
1133 "invalid subclient "
1134 "address %d; must be 0x48-0x4f\n",
1135 force_subclients[i]);
1140 w83793_write_value(client, W83793_REG_I2C_SUBADDR,
1141 (force_subclients[2] & 0x07) |
1142 ((force_subclients[3] & 0x07) << 4));
1145 tmp = w83793_read_value(client, W83793_REG_I2C_SUBADDR);
1146 if (!(tmp & 0x08)) {
1148 w83793_create_subclient(adapter, client, tmp & 0x7,
1153 if (!(tmp & 0x80)) {
1154 if ((data->lm75[0] != NULL)
1155 && ((tmp & 0x7) == ((tmp >> 4) & 0x7))) {
1156 dev_err(&client->dev,
1157 "duplicate addresses 0x%x, "
1158 "use force_subclients\n", data->lm75[0]->addr);
1162 err = w83793_create_subclient(adapter, client,
1163 (tmp >> 4) & 0x7, &data->lm75[1]);
1170 /* Undo inits in case of errors */
1173 if (data->lm75[0] != NULL) {
1174 i2c_detach_client(data->lm75[0]);
1175 kfree(data->lm75[0]);
1181 static int w83793_detect(struct i2c_adapter *adapter, int address, int kind)
1185 struct i2c_client *client;
1187 struct w83793_data *data;
1188 int files_fan = ARRAY_SIZE(w83793_left_fan) / 7;
1189 int files_pwm = ARRAY_SIZE(w83793_left_pwm) / 5;
1192 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA)) {
1196 /* OK. For now, we presume we have a valid client. We now create the
1197 client structure, even though we cannot fill it completely yet.
1198 But it allows us to access w83793_{read,write}_value. */
1200 if (!(data = kzalloc(sizeof(struct w83793_data), GFP_KERNEL))) {
1205 client = &data->client;
1207 i2c_set_clientdata(client, data);
1208 client->addr = address;
1209 client->adapter = adapter;
1210 client->driver = &w83793_driver;
1212 data->bank = i2c_smbus_read_byte_data(client, W83793_REG_BANKSEL);
1214 /* Now, we do the remaining detection. */
1216 tmp = data->bank & 0x80 ? 0x5c : 0xa3;
1217 /* Check Winbond vendor ID */
1218 if (tmp != i2c_smbus_read_byte_data(client,
1219 W83793_REG_VENDORID)) {
1220 pr_debug("w83793: Detection failed at check "
1226 /* If Winbond chip, address of chip and W83793_REG_I2C_ADDR
1228 if ((data->bank & 0x07) == 0
1229 && i2c_smbus_read_byte_data(client, W83793_REG_I2C_ADDR) !=
1231 pr_debug("w83793: Detection failed at check "
1239 /* We have either had a force parameter, or we have already detected the
1240 Winbond. Determine the chip type now */
1243 if (0x7b == w83793_read_value(client, W83793_REG_CHIPID)) {
1247 dev_warn(&adapter->dev, "w83793: Ignoring "
1248 "'force' parameter for unknown chip "
1249 "at address 0x%02x\n", address);
1255 /* Fill in the remaining client fields and put into the global list */
1256 strlcpy(client->name, "w83793", I2C_NAME_SIZE);
1258 mutex_init(&data->update_lock);
1260 /* Tell the I2C layer a new client has arrived */
1261 if ((err = i2c_attach_client(client)))
1264 if ((err = w83793_detect_subclients(adapter, address, kind, client)))
1267 /* Initialize the chip */
1268 w83793_init_client(client);
1270 data->vrm = vid_which_vrm();
1272 Only fan 1-5 has their own input pins,
1273 Pwm 1-3 has their own pins
1275 data->has_fan = 0x1f;
1276 data->has_pwm = 0x07;
1277 tmp = w83793_read_value(client, W83793_REG_MFC);
1278 val = w83793_read_value(client, W83793_REG_FANIN_CTRL);
1280 /* check the function of pins 49-56 */
1281 if (!(tmp & 0x80)) {
1282 data->has_pwm |= 0x18; /* pwm 4,5 */
1283 if (val & 0x01) { /* fan 6 */
1284 data->has_fan |= 0x20;
1285 data->has_pwm |= 0x20;
1287 if (val & 0x02) { /* fan 7 */
1288 data->has_fan |= 0x40;
1289 data->has_pwm |= 0x40;
1291 if (!(tmp & 0x40) && (val & 0x04)) { /* fan 8 */
1292 data->has_fan |= 0x80;
1293 data->has_pwm |= 0x80;
1297 if (0x08 == (tmp & 0x0c)) {
1298 if (val & 0x08) /* fan 9 */
1299 data->has_fan |= 0x100;
1300 if (val & 0x10) /* fan 10 */
1301 data->has_fan |= 0x200;
1304 if (0x20 == (tmp & 0x30)) {
1305 if (val & 0x20) /* fan 11 */
1306 data->has_fan |= 0x400;
1307 if (val & 0x40) /* fan 12 */
1308 data->has_fan |= 0x800;
1311 if ((tmp & 0x01) && (val & 0x04)) { /* fan 8, second location */
1312 data->has_fan |= 0x80;
1313 data->has_pwm |= 0x80;
1316 /* Register sysfs hooks */
1317 for (i = 0; i < ARRAY_SIZE(w83793_sensor_attr_2); i++) {
1318 err = device_create_file(dev,
1319 &w83793_sensor_attr_2[i].dev_attr);
1324 for (i = 0; i < ARRAY_SIZE(sda_single_files); i++) {
1325 err = device_create_file(dev, &sda_single_files[i].dev_attr);
1331 for (i = 5; i < 12; i++) {
1333 if (!(data->has_fan & (1 << i)))
1335 for (j = 0; j < files_fan; j++) {
1336 err = device_create_file(dev,
1337 &w83793_left_fan[(i - 5) * files_fan
1344 for (i = 3; i < 8; i++) {
1346 if (!(data->has_pwm & (1 << i)))
1348 for (j = 0; j < files_pwm; j++) {
1349 err = device_create_file(dev,
1350 &w83793_left_pwm[(i - 3) * files_pwm
1357 data->class_dev = hwmon_device_register(dev);
1358 if (IS_ERR(data->class_dev)) {
1359 err = PTR_ERR(data->class_dev);
1365 /* Unregister sysfs hooks */
1368 for (i = 0; i < ARRAY_SIZE(w83793_sensor_attr_2); i++)
1369 device_remove_file(dev, &w83793_sensor_attr_2[i].dev_attr);
1371 for (i = 0; i < ARRAY_SIZE(sda_single_files); i++)
1372 device_remove_file(dev, &sda_single_files[i].dev_attr);
1374 for (i = 0; i < ARRAY_SIZE(w83793_left_fan); i++)
1375 device_remove_file(dev, &w83793_left_fan[i].dev_attr);
1377 for (i = 0; i < ARRAY_SIZE(w83793_left_pwm); i++)
1378 device_remove_file(dev, &w83793_left_pwm[i].dev_attr);
1380 if (data->lm75[0] != NULL) {
1381 i2c_detach_client(data->lm75[0]);
1382 kfree(data->lm75[0]);
1384 if (data->lm75[1] != NULL) {
1385 i2c_detach_client(data->lm75[1]);
1386 kfree(data->lm75[1]);
1389 i2c_detach_client(client);
1396 static void w83793_update_nonvolatile(struct device *dev)
1398 struct i2c_client *client = to_i2c_client(dev);
1399 struct w83793_data *data = i2c_get_clientdata(client);
1402 They are somewhat "stable" registers, and to update them everytime
1403 takes so much time, it's just not worthy. Update them in a long
1404 interval to avoid exception.
1406 if (!(time_after(jiffies, data->last_nonvolatile + HZ * 300)
1409 /* update voltage limits */
1410 for (i = 1; i < 3; i++) {
1411 for (j = 0; j < ARRAY_SIZE(data->in); j++) {
1413 w83793_read_value(client, W83793_REG_IN[j][i]);
1415 data->in_low_bits[i] =
1416 w83793_read_value(client, W83793_REG_IN_LOW_BITS[i]);
1419 for (i = 0; i < ARRAY_SIZE(data->fan_min); i++) {
1420 /* Update the Fan measured value and limits */
1421 if (!(data->has_fan & (1 << i))) {
1425 w83793_read_value(client, W83793_REG_FAN_MIN(i)) << 8;
1427 w83793_read_value(client, W83793_REG_FAN_MIN(i) + 1);
1430 for (i = 0; i < ARRAY_SIZE(data->temp_fan_map); i++) {
1431 data->temp_fan_map[i] =
1432 w83793_read_value(client, W83793_REG_TEMP_FAN_MAP(i));
1433 for (j = 1; j < 5; j++) {
1435 w83793_read_value(client, W83793_REG_TEMP[i][j]);
1437 data->temp_cruise[i] =
1438 w83793_read_value(client, W83793_REG_TEMP_CRUISE(i));
1439 for (j = 0; j < 7; j++) {
1440 data->sf2_pwm[i][j] =
1441 w83793_read_value(client, W83793_REG_SF2_PWM(i, j));
1442 data->sf2_temp[i][j] =
1443 w83793_read_value(client,
1444 W83793_REG_SF2_TEMP(i, j));
1448 for (i = 0; i < ARRAY_SIZE(data->temp_mode); i++)
1449 data->temp_mode[i] =
1450 w83793_read_value(client, W83793_REG_TEMP_MODE[i]);
1452 for (i = 0; i < ARRAY_SIZE(data->tolerance); i++) {
1453 data->tolerance[i] =
1454 w83793_read_value(client, W83793_REG_TEMP_TOL(i));
1457 for (i = 0; i < ARRAY_SIZE(data->pwm); i++) {
1458 if (!(data->has_pwm & (1 << i)))
1460 data->pwm[i][PWM_NONSTOP] =
1461 w83793_read_value(client, W83793_REG_PWM(i, PWM_NONSTOP));
1462 data->pwm[i][PWM_START] =
1463 w83793_read_value(client, W83793_REG_PWM(i, PWM_START));
1464 data->pwm_stop_time[i] =
1465 w83793_read_value(client, W83793_REG_PWM_STOP_TIME(i));
1468 data->pwm_default = w83793_read_value(client, W83793_REG_PWM_DEFAULT);
1469 data->pwm_enable = w83793_read_value(client, W83793_REG_PWM_ENABLE);
1470 data->pwm_uptime = w83793_read_value(client, W83793_REG_PWM_UPTIME);
1471 data->pwm_downtime = w83793_read_value(client, W83793_REG_PWM_DOWNTIME);
1472 data->temp_critical =
1473 w83793_read_value(client, W83793_REG_TEMP_CRITICAL);
1474 data->beep_enable = w83793_read_value(client, W83793_REG_OVT_BEEP);
1476 for (i = 0; i < ARRAY_SIZE(data->beeps); i++) {
1477 data->beeps[i] = w83793_read_value(client, W83793_REG_BEEP(i));
1480 data->last_nonvolatile = jiffies;
1483 static struct w83793_data *w83793_update_device(struct device *dev)
1485 struct i2c_client *client = to_i2c_client(dev);
1486 struct w83793_data *data = i2c_get_clientdata(client);
1489 mutex_lock(&data->update_lock);
1491 if (!(time_after(jiffies, data->last_updated + HZ * 2)
1495 /* Update the voltages measured value and limits */
1496 for (i = 0; i < ARRAY_SIZE(data->in); i++)
1497 data->in[i][IN_READ] =
1498 w83793_read_value(client, W83793_REG_IN[i][IN_READ]);
1500 data->in_low_bits[IN_READ] =
1501 w83793_read_value(client, W83793_REG_IN_LOW_BITS[IN_READ]);
1503 for (i = 0; i < ARRAY_SIZE(data->fan); i++) {
1504 if (!(data->has_fan & (1 << i))) {
1508 w83793_read_value(client, W83793_REG_FAN(i)) << 8;
1510 w83793_read_value(client, W83793_REG_FAN(i) + 1);
1513 for (i = 0; i < ARRAY_SIZE(data->temp); i++)
1514 data->temp[i][TEMP_READ] =
1515 w83793_read_value(client, W83793_REG_TEMP[i][TEMP_READ]);
1517 data->temp_low_bits =
1518 w83793_read_value(client, W83793_REG_TEMP_LOW_BITS);
1520 for (i = 0; i < ARRAY_SIZE(data->pwm); i++) {
1521 if (data->has_pwm & (1 << i))
1522 data->pwm[i][PWM_DUTY] =
1523 w83793_read_value(client,
1524 W83793_REG_PWM(i, PWM_DUTY));
1527 for (i = 0; i < ARRAY_SIZE(data->alarms); i++)
1529 w83793_read_value(client, W83793_REG_ALARM(i));
1530 data->vid[0] = w83793_read_value(client, W83793_REG_VID_INA);
1531 data->vid[1] = w83793_read_value(client, W83793_REG_VID_INB);
1532 w83793_update_nonvolatile(dev);
1533 data->last_updated = jiffies;
1537 mutex_unlock(&data->update_lock);
1541 /* Ignore the possibility that somebody change bank outside the driver
1542 Must be called with data->update_lock held, except during initialization */
1543 static u8 w83793_read_value(struct i2c_client *client, u16 reg)
1545 struct w83793_data *data = i2c_get_clientdata(client);
1547 u8 new_bank = reg >> 8;
1549 new_bank |= data->bank & 0xfc;
1550 if (data->bank != new_bank) {
1551 if (i2c_smbus_write_byte_data
1552 (client, W83793_REG_BANKSEL, new_bank) >= 0)
1553 data->bank = new_bank;
1555 dev_err(&client->dev,
1556 "set bank to %d failed, fall back "
1557 "to bank %d, read reg 0x%x error\n",
1558 new_bank, data->bank, reg);
1559 res = 0x0; /* read 0x0 from the chip */
1563 res = i2c_smbus_read_byte_data(client, reg & 0xff);
1568 /* Must be called with data->update_lock held, except during initialization */
1569 static int w83793_write_value(struct i2c_client *client, u16 reg, u8 value)
1571 struct w83793_data *data = i2c_get_clientdata(client);
1573 u8 new_bank = reg >> 8;
1575 new_bank |= data->bank & 0xfc;
1576 if (data->bank != new_bank) {
1577 if ((res = i2c_smbus_write_byte_data
1578 (client, W83793_REG_BANKSEL, new_bank)) >= 0)
1579 data->bank = new_bank;
1581 dev_err(&client->dev,
1582 "set bank to %d failed, fall back "
1583 "to bank %d, write reg 0x%x error\n",
1584 new_bank, data->bank, reg);
1589 res = i2c_smbus_write_byte_data(client, reg & 0xff, value);
1594 static int __init sensors_w83793_init(void)
1596 return i2c_add_driver(&w83793_driver);
1599 static void __exit sensors_w83793_exit(void)
1601 i2c_del_driver(&w83793_driver);
1604 MODULE_AUTHOR("Yuan Mu");
1605 MODULE_DESCRIPTION("w83793 driver");
1606 MODULE_LICENSE("GPL");
1608 module_init(sensors_w83793_init);
1609 module_exit(sensors_w83793_exit);