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 };
120 static u8 scale_in_add[] = { 0, 0, 0, 0, 0, 0, 0, 150, 150, 0 };
122 #define W83793_REG_FAN(index) (0x23 + 2 * (index)) /* High byte */
123 #define W83793_REG_FAN_MIN(index) (0x90 + 2 * (index)) /* High byte */
125 #define W83793_REG_PWM_DEFAULT 0xb2
126 #define W83793_REG_PWM_ENABLE 0x207
127 #define W83793_REG_PWM_UPTIME 0xc3 /* Unit in 0.1 second */
128 #define W83793_REG_PWM_DOWNTIME 0xc4 /* Unit in 0.1 second */
129 #define W83793_REG_TEMP_CRITICAL 0xc5
133 #define PWM_NONSTOP 2
134 #define W83793_REG_PWM(index, nr) (((nr) == 0 ? 0xb3 : \
135 (nr) == 1 ? 0x220 : 0x218) + (index))
137 /* bit field, fan1 is bit0, fan2 is bit1 ... */
138 #define W83793_REG_TEMP_FAN_MAP(index) (0x201 + (index))
139 #define W83793_REG_TEMP_TOL(index) (0x208 + (index))
140 #define W83793_REG_TEMP_CRUISE(index) (0x210 + (index))
141 #define W83793_REG_PWM_STOP_TIME(index) (0x228 + (index))
142 #define W83793_REG_SF2_TEMP(index, nr) (0x230 + ((index) << 4) + (nr))
143 #define W83793_REG_SF2_PWM(index, nr) (0x238 + ((index) << 4) + (nr))
145 static inline unsigned long FAN_FROM_REG(u16 val)
147 if ((val >= 0xfff) || (val == 0))
149 return (1350000UL / val);
152 static inline u16 FAN_TO_REG(long rpm)
156 return SENSORS_LIMIT((1350000 + (rpm >> 1)) / rpm, 1, 0xffe);
159 static inline unsigned long TIME_FROM_REG(u8 reg)
164 static inline u8 TIME_TO_REG(unsigned long val)
166 return SENSORS_LIMIT((val + 50) / 100, 0, 0xff);
169 static inline long TEMP_FROM_REG(s8 reg)
174 static inline s8 TEMP_TO_REG(long val, s8 min, s8 max)
176 return SENSORS_LIMIT((val + (val < 0 ? -500 : 500)) / 1000, min, max);
180 struct i2c_client client;
181 struct i2c_client *lm75[2];
182 struct class_device *class_dev;
183 struct mutex update_lock;
184 char valid; /* !=0 if following fields are valid */
185 unsigned long last_updated; /* In jiffies */
186 unsigned long last_nonvolatile; /* In jiffies, last time we update the
187 nonvolatile registers */
192 u8 in[10][3]; /* Register value, read/high/low */
193 u8 in_low_bits[3]; /* Additional resolution for VCore A/B Vtt */
195 u16 has_fan; /* Only fan1- fan5 has own pins */
196 u16 fan[12]; /* Register value combine */
197 u16 fan_min[12]; /* Register value combine */
199 s8 temp[6][5]; /* current, crit, crit_hyst,warn, warn_hyst */
200 u8 temp_low_bits; /* Additional resolution TD1-TD4 */
201 u8 temp_mode[2]; /* byte 0: Temp D1-D4 mode each has 2 bits
202 byte 1: Temp R1,R2 mode, each has 1 bit */
203 u8 temp_critical; /* If reached all fan will be at full speed */
204 u8 temp_fan_map[6]; /* Temp controls which pwm fan, bit field */
207 u8 pwm_enable; /* Register value, each Temp has 1 bit */
208 u8 pwm_uptime; /* Register value */
209 u8 pwm_downtime; /* Register value */
210 u8 pwm_default; /* All fan default pwm, next poweron valid */
211 u8 pwm[8][3]; /* Register value */
215 u8 alarms[5]; /* realtime status registers */
218 u8 tolerance[3]; /* Temp tolerance(Smart Fan I/II) */
219 u8 sf2_pwm[6][7]; /* Smart FanII: Fan duty cycle */
220 u8 sf2_temp[6][7]; /* Smart FanII: Temp level point */
223 static u8 w83793_read_value(struct i2c_client *client, u16 reg);
224 static int w83793_write_value(struct i2c_client *client, u16 reg, u8 value);
225 static int w83793_attach_adapter(struct i2c_adapter *adapter);
226 static int w83793_detect(struct i2c_adapter *adapter, int address, int kind);
227 static int w83793_detach_client(struct i2c_client *client);
228 static void w83793_init_client(struct i2c_client *client);
229 static void w83793_update_nonvolatile(struct device *dev);
230 static struct w83793_data *w83793_update_device(struct device *dev);
232 static struct i2c_driver w83793_driver = {
236 .attach_adapter = w83793_attach_adapter,
237 .detach_client = w83793_detach_client,
241 show_vrm(struct device *dev, struct device_attribute *attr, char *buf)
243 struct i2c_client *client = to_i2c_client(dev);
244 struct w83793_data *data = i2c_get_clientdata(client);
246 return sprintf(buf, "%d\n", data->vrm);
250 show_vid(struct device *dev, struct device_attribute *attr, char *buf)
252 struct w83793_data *data = w83793_update_device(dev);
253 struct sensor_device_attribute_2 *sensor_attr =
254 to_sensor_dev_attr_2(attr);
255 int index = sensor_attr->index;
257 return sprintf(buf, "%d\n", vid_from_reg(data->vid[index], data->vrm));
261 store_vrm(struct device *dev, struct device_attribute *attr,
262 const char *buf, size_t count)
264 struct i2c_client *client = to_i2c_client(dev);
265 struct w83793_data *data = i2c_get_clientdata(client);
267 data->vrm = simple_strtoul(buf, NULL, 10);
271 #define ALARM_STATUS 0
272 #define BEEP_ENABLE 1
274 show_alarm_beep(struct device *dev, struct device_attribute *attr, char *buf)
276 struct w83793_data *data = w83793_update_device(dev);
277 struct sensor_device_attribute_2 *sensor_attr =
278 to_sensor_dev_attr_2(attr);
279 int nr = sensor_attr->nr;
280 int index = sensor_attr->index >> 3;
281 int bit = sensor_attr->index & 0x07;
284 if (ALARM_STATUS == nr) {
285 val = (data->alarms[index] >> (bit)) & 1;
286 } else { /* BEEP_ENABLE */
287 val = (data->beeps[index] >> (bit)) & 1;
290 return sprintf(buf, "%u\n", val);
294 store_beep(struct device *dev, struct device_attribute *attr,
295 const char *buf, size_t count)
297 struct i2c_client *client = to_i2c_client(dev);
298 struct w83793_data *data = i2c_get_clientdata(client);
299 struct sensor_device_attribute_2 *sensor_attr =
300 to_sensor_dev_attr_2(attr);
301 int index = sensor_attr->index >> 3;
302 int shift = sensor_attr->index & 0x07;
303 u8 beep_bit = 1 << shift;
306 val = simple_strtoul(buf, NULL, 10);
307 if (val != 0 && val != 1)
310 mutex_lock(&data->update_lock);
311 data->beeps[index] = w83793_read_value(client, W83793_REG_BEEP(index));
312 data->beeps[index] &= ~beep_bit;
313 data->beeps[index] |= val << shift;
314 w83793_write_value(client, W83793_REG_BEEP(index), data->beeps[index]);
315 mutex_unlock(&data->update_lock);
321 show_beep_enable(struct device *dev, struct device_attribute *attr, char *buf)
323 struct w83793_data *data = w83793_update_device(dev);
324 return sprintf(buf, "%u\n", (data->beep_enable >> 1) & 0x01);
328 store_beep_enable(struct device *dev, struct device_attribute *attr,
329 const char *buf, size_t count)
331 struct i2c_client *client = to_i2c_client(dev);
332 struct w83793_data *data = i2c_get_clientdata(client);
333 u8 val = simple_strtoul(buf, NULL, 10);
335 if (val != 0 && val != 1)
338 mutex_lock(&data->update_lock);
339 data->beep_enable = w83793_read_value(client, W83793_REG_OVT_BEEP)
341 data->beep_enable |= val << 1;
342 w83793_write_value(client, W83793_REG_OVT_BEEP, data->beep_enable);
343 mutex_unlock(&data->update_lock);
348 /* Write any value to clear chassis alarm */
350 store_chassis_clear(struct device *dev,
351 struct device_attribute *attr, const char *buf,
354 struct i2c_client *client = to_i2c_client(dev);
355 struct w83793_data *data = i2c_get_clientdata(client);
358 mutex_lock(&data->update_lock);
359 val = w83793_read_value(client, W83793_REG_CLR_CHASSIS);
361 w83793_write_value(client, W83793_REG_CLR_CHASSIS, val);
362 mutex_unlock(&data->update_lock);
369 show_fan(struct device *dev, struct device_attribute *attr, char *buf)
371 struct sensor_device_attribute_2 *sensor_attr =
372 to_sensor_dev_attr_2(attr);
373 int nr = sensor_attr->nr;
374 int index = sensor_attr->index;
375 struct w83793_data *data = w83793_update_device(dev);
378 if (FAN_INPUT == nr) {
379 val = data->fan[index] & 0x0fff;
381 val = data->fan_min[index] & 0x0fff;
384 return sprintf(buf, "%lu\n", FAN_FROM_REG(val));
388 store_fan_min(struct device *dev, struct device_attribute *attr,
389 const char *buf, size_t count)
391 struct sensor_device_attribute_2 *sensor_attr =
392 to_sensor_dev_attr_2(attr);
393 int index = sensor_attr->index;
394 struct i2c_client *client = to_i2c_client(dev);
395 struct w83793_data *data = i2c_get_clientdata(client);
396 u16 val = FAN_TO_REG(simple_strtoul(buf, NULL, 10));
398 mutex_lock(&data->update_lock);
399 data->fan_min[index] = val;
400 w83793_write_value(client, W83793_REG_FAN_MIN(index),
402 w83793_write_value(client, W83793_REG_FAN_MIN(index) + 1, val & 0xff);
403 mutex_unlock(&data->update_lock);
410 #define PWM_NONSTOP 2
411 #define PWM_STOP_TIME 3
413 show_pwm(struct device *dev, struct device_attribute *attr, char *buf)
415 struct sensor_device_attribute_2 *sensor_attr =
416 to_sensor_dev_attr_2(attr);
417 struct w83793_data *data = w83793_update_device(dev);
419 int nr = sensor_attr->nr;
420 int index = sensor_attr->index;
422 if (PWM_STOP_TIME == nr)
423 val = TIME_FROM_REG(data->pwm_stop_time[index]);
425 val = (data->pwm[index][nr] & 0x3f) << 2;
427 return sprintf(buf, "%d\n", val);
431 store_pwm(struct device *dev, struct device_attribute *attr,
432 const char *buf, size_t count)
434 struct i2c_client *client = to_i2c_client(dev);
435 struct w83793_data *data = i2c_get_clientdata(client);
436 struct sensor_device_attribute_2 *sensor_attr =
437 to_sensor_dev_attr_2(attr);
438 int nr = sensor_attr->nr;
439 int index = sensor_attr->index;
442 mutex_lock(&data->update_lock);
443 if (PWM_STOP_TIME == nr) {
444 val = TIME_TO_REG(simple_strtoul(buf, NULL, 10));
445 data->pwm_stop_time[index] = val;
446 w83793_write_value(client, W83793_REG_PWM_STOP_TIME(index),
449 val = SENSORS_LIMIT(simple_strtoul(buf, NULL, 10), 0, 0xff)
451 data->pwm[index][nr] =
452 w83793_read_value(client, W83793_REG_PWM(index, nr)) & 0xc0;
453 data->pwm[index][nr] |= val;
454 w83793_write_value(client, W83793_REG_PWM(index, nr),
455 data->pwm[index][nr]);
458 mutex_unlock(&data->update_lock);
463 show_temp(struct device *dev, struct device_attribute *attr, char *buf)
465 struct sensor_device_attribute_2 *sensor_attr =
466 to_sensor_dev_attr_2(attr);
467 int nr = sensor_attr->nr;
468 int index = sensor_attr->index;
469 struct w83793_data *data = w83793_update_device(dev);
470 long temp = TEMP_FROM_REG(data->temp[index][nr]);
472 if (TEMP_READ == nr && index < 4) { /* Only TD1-TD4 have low bits */
473 int low = ((data->temp_low_bits >> (index * 2)) & 0x03) * 250;
474 temp += temp > 0 ? low : -low;
476 return sprintf(buf, "%ld\n", temp);
480 store_temp(struct device *dev, struct device_attribute *attr,
481 const char *buf, size_t count)
483 struct sensor_device_attribute_2 *sensor_attr =
484 to_sensor_dev_attr_2(attr);
485 int nr = sensor_attr->nr;
486 int index = sensor_attr->index;
487 struct i2c_client *client = to_i2c_client(dev);
488 struct w83793_data *data = i2c_get_clientdata(client);
489 long tmp = simple_strtol(buf, NULL, 10);
491 mutex_lock(&data->update_lock);
492 data->temp[index][nr] = TEMP_TO_REG(tmp, -128, 127);
493 w83793_write_value(client, W83793_REG_TEMP[index][nr],
494 data->temp[index][nr]);
495 mutex_unlock(&data->update_lock);
501 each has 4 mode:(2 bits)
503 1: Use internal temp sensor(default)
505 3: Use sensor in Intel CPU and get result by PECI
508 each has 2 mode:(1 bit)
509 0: Disable temp sensor monitor
510 1: To enable temp sensors monitor
513 /* 0 disable, 6 PECI */
514 static u8 TO_TEMP_MODE[] = { 0, 0, 0, 6 };
517 show_temp_mode(struct device *dev, struct device_attribute *attr, char *buf)
519 struct w83793_data *data = w83793_update_device(dev);
520 struct sensor_device_attribute_2 *sensor_attr =
521 to_sensor_dev_attr_2(attr);
522 int index = sensor_attr->index;
523 u8 mask = (index < 4) ? 0x03 : 0x01;
524 u8 shift = (index < 4) ? (2 * index) : (index - 4);
526 index = (index < 4) ? 0 : 1;
528 tmp = (data->temp_mode[index] >> shift) & mask;
530 /* for the internal sensor, found out if diode or thermistor */
532 tmp = index == 0 ? 3 : 4;
534 tmp = TO_TEMP_MODE[tmp];
537 return sprintf(buf, "%d\n", tmp);
541 store_temp_mode(struct device *dev, struct device_attribute *attr,
542 const char *buf, size_t count)
544 struct i2c_client *client = to_i2c_client(dev);
545 struct w83793_data *data = i2c_get_clientdata(client);
546 struct sensor_device_attribute_2 *sensor_attr =
547 to_sensor_dev_attr_2(attr);
548 int index = sensor_attr->index;
549 u8 mask = (index < 4) ? 0x03 : 0x01;
550 u8 shift = (index < 4) ? (2 * index) : (index - 4);
551 u8 val = simple_strtoul(buf, NULL, 10);
553 /* transform the sysfs interface values into table above */
554 if ((val == 6) && (index < 4)) {
556 } else if ((val == 3 && index < 4)
557 || (val == 4 && index >= 4)
559 /* transform diode or thermistor into internal enable */
565 index = (index < 4) ? 0 : 1;
566 mutex_lock(&data->update_lock);
567 data->temp_mode[index] =
568 w83793_read_value(client, W83793_REG_TEMP_MODE[index]);
569 data->temp_mode[index] &= ~(mask << shift);
570 data->temp_mode[index] |= val << shift;
571 w83793_write_value(client, W83793_REG_TEMP_MODE[index],
572 data->temp_mode[index]);
573 mutex_unlock(&data->update_lock);
578 #define SETUP_PWM_DEFAULT 0
579 #define SETUP_PWM_UPTIME 1 /* Unit in 0.1s */
580 #define SETUP_PWM_DOWNTIME 2 /* Unit in 0.1s */
581 #define SETUP_TEMP_CRITICAL 3
583 show_sf_setup(struct device *dev, struct device_attribute *attr, char *buf)
585 struct sensor_device_attribute_2 *sensor_attr =
586 to_sensor_dev_attr_2(attr);
587 int nr = sensor_attr->nr;
588 struct w83793_data *data = w83793_update_device(dev);
591 if (SETUP_PWM_DEFAULT == nr) {
592 val = (data->pwm_default & 0x3f) << 2;
593 } else if (SETUP_PWM_UPTIME == nr) {
594 val = TIME_FROM_REG(data->pwm_uptime);
595 } else if (SETUP_PWM_DOWNTIME == nr) {
596 val = TIME_FROM_REG(data->pwm_downtime);
597 } else if (SETUP_TEMP_CRITICAL == nr) {
598 val = TEMP_FROM_REG(data->temp_critical & 0x7f);
601 return sprintf(buf, "%d\n", val);
605 store_sf_setup(struct device *dev, struct device_attribute *attr,
606 const char *buf, size_t count)
608 struct sensor_device_attribute_2 *sensor_attr =
609 to_sensor_dev_attr_2(attr);
610 int nr = sensor_attr->nr;
611 struct i2c_client *client = to_i2c_client(dev);
612 struct w83793_data *data = i2c_get_clientdata(client);
614 mutex_lock(&data->update_lock);
615 if (SETUP_PWM_DEFAULT == nr) {
617 w83793_read_value(client, W83793_REG_PWM_DEFAULT) & 0xc0;
618 data->pwm_default |= SENSORS_LIMIT(simple_strtoul(buf, NULL,
621 w83793_write_value(client, W83793_REG_PWM_DEFAULT,
623 } else if (SETUP_PWM_UPTIME == nr) {
624 data->pwm_uptime = TIME_TO_REG(simple_strtoul(buf, NULL, 10));
625 data->pwm_uptime += data->pwm_uptime == 0 ? 1 : 0;
626 w83793_write_value(client, W83793_REG_PWM_UPTIME,
628 } else if (SETUP_PWM_DOWNTIME == nr) {
629 data->pwm_downtime = TIME_TO_REG(simple_strtoul(buf, NULL, 10));
630 data->pwm_downtime += data->pwm_downtime == 0 ? 1 : 0;
631 w83793_write_value(client, W83793_REG_PWM_DOWNTIME,
633 } else { /* SETUP_TEMP_CRITICAL */
634 data->temp_critical =
635 w83793_read_value(client, W83793_REG_TEMP_CRITICAL) & 0x80;
636 data->temp_critical |= TEMP_TO_REG(simple_strtol(buf, NULL, 10),
638 w83793_write_value(client, W83793_REG_TEMP_CRITICAL,
639 data->temp_critical);
642 mutex_unlock(&data->update_lock);
647 Temp SmartFan control
649 Temp channel control which pwm fan, bitfield, bit 0 indicate pwm1...
650 It's possible two or more temp channels control the same fan, w83793
651 always prefers to pick the most critical request and applies it to
653 It's possible one fan is not in any mapping of 6 temp channels, this
654 means the fan is manual mode
657 Each temp channel has its own SmartFan mode, and temp channel
658 control fans that are set by TEMP_FAN_MAP
660 1: Thermal Cruise Mode
663 Target temperature in thermal cruise mode, w83793 will try to turn
664 fan speed to keep the temperature of target device around this
668 If Temp higher or lower than target with this tolerance, w83793
669 will take actions to speed up or slow down the fan to keep the
670 temperature within the tolerance range.
673 #define TEMP_FAN_MAP 0
674 #define TEMP_PWM_ENABLE 1
675 #define TEMP_CRUISE 2
676 #define TEMP_TOLERANCE 3
678 show_sf_ctrl(struct device *dev, struct device_attribute *attr, char *buf)
680 struct sensor_device_attribute_2 *sensor_attr =
681 to_sensor_dev_attr_2(attr);
682 int nr = sensor_attr->nr;
683 int index = sensor_attr->index;
684 struct w83793_data *data = w83793_update_device(dev);
687 if (TEMP_FAN_MAP == nr) {
688 val = data->temp_fan_map[index];
689 } else if (TEMP_PWM_ENABLE == nr) {
690 /* +2 to transfrom into 2 and 3 to conform with sysfs intf */
691 val = ((data->pwm_enable >> index) & 0x01) + 2;
692 } else if (TEMP_CRUISE == nr) {
693 val = TEMP_FROM_REG(data->temp_cruise[index] & 0x7f);
694 } else { /* TEMP_TOLERANCE */
695 val = data->tolerance[index >> 1] >> ((index & 0x01) ? 4 : 0);
696 val = TEMP_FROM_REG(val & 0x0f);
698 return sprintf(buf, "%d\n", val);
702 store_sf_ctrl(struct device *dev, struct device_attribute *attr,
703 const char *buf, size_t count)
705 struct sensor_device_attribute_2 *sensor_attr =
706 to_sensor_dev_attr_2(attr);
707 int nr = sensor_attr->nr;
708 int index = sensor_attr->index;
709 struct i2c_client *client = to_i2c_client(dev);
710 struct w83793_data *data = i2c_get_clientdata(client);
713 mutex_lock(&data->update_lock);
714 if (TEMP_FAN_MAP == nr) {
715 val = simple_strtoul(buf, NULL, 10) & 0xff;
716 w83793_write_value(client, W83793_REG_TEMP_FAN_MAP(index), val);
717 data->temp_fan_map[index] = val;
718 } else if (TEMP_PWM_ENABLE == nr) {
719 val = simple_strtoul(buf, NULL, 10);
720 if (2 == val || 3 == val) {
722 w83793_read_value(client, W83793_REG_PWM_ENABLE);
724 data->pwm_enable |= 1 << index;
726 data->pwm_enable &= ~(1 << index);
727 w83793_write_value(client, W83793_REG_PWM_ENABLE,
730 mutex_unlock(&data->update_lock);
733 } else if (TEMP_CRUISE == nr) {
734 data->temp_cruise[index] =
735 w83793_read_value(client, W83793_REG_TEMP_CRUISE(index));
736 val = TEMP_TO_REG(simple_strtol(buf, NULL, 10), 0, 0x7f);
737 data->temp_cruise[index] &= 0x80;
738 data->temp_cruise[index] |= val;
740 w83793_write_value(client, W83793_REG_TEMP_CRUISE(index),
741 data->temp_cruise[index]);
742 } else { /* TEMP_TOLERANCE */
744 u8 shift = (index & 0x01) ? 4 : 0;
746 w83793_read_value(client, W83793_REG_TEMP_TOL(i));
748 val = TEMP_TO_REG(simple_strtol(buf, NULL, 10), 0, 0x0f);
749 data->tolerance[i] &= ~(0x0f << shift);
750 data->tolerance[i] |= val << shift;
751 w83793_write_value(client, W83793_REG_TEMP_TOL(i),
755 mutex_unlock(&data->update_lock);
760 show_sf2_pwm(struct device *dev, struct device_attribute *attr, char *buf)
762 struct sensor_device_attribute_2 *sensor_attr =
763 to_sensor_dev_attr_2(attr);
764 int nr = sensor_attr->nr;
765 int index = sensor_attr->index;
766 struct w83793_data *data = w83793_update_device(dev);
768 return sprintf(buf, "%d\n", (data->sf2_pwm[index][nr] & 0x3f) << 2);
772 store_sf2_pwm(struct device *dev, struct device_attribute *attr,
773 const char *buf, size_t count)
775 struct i2c_client *client = to_i2c_client(dev);
776 struct w83793_data *data = i2c_get_clientdata(client);
777 struct sensor_device_attribute_2 *sensor_attr =
778 to_sensor_dev_attr_2(attr);
779 int nr = sensor_attr->nr;
780 int index = sensor_attr->index;
781 u8 val = SENSORS_LIMIT(simple_strtoul(buf, NULL, 10), 0, 0xff) >> 2;
783 mutex_lock(&data->update_lock);
784 data->sf2_pwm[index][nr] =
785 w83793_read_value(client, W83793_REG_SF2_PWM(index, nr)) & 0xc0;
786 data->sf2_pwm[index][nr] |= val;
787 w83793_write_value(client, W83793_REG_SF2_PWM(index, nr),
788 data->sf2_pwm[index][nr]);
789 mutex_unlock(&data->update_lock);
794 show_sf2_temp(struct device *dev, struct device_attribute *attr, char *buf)
796 struct sensor_device_attribute_2 *sensor_attr =
797 to_sensor_dev_attr_2(attr);
798 int nr = sensor_attr->nr;
799 int index = sensor_attr->index;
800 struct w83793_data *data = w83793_update_device(dev);
802 return sprintf(buf, "%ld\n",
803 TEMP_FROM_REG(data->sf2_temp[index][nr] & 0x7f));
807 store_sf2_temp(struct device *dev, struct device_attribute *attr,
808 const char *buf, size_t count)
810 struct i2c_client *client = to_i2c_client(dev);
811 struct w83793_data *data = i2c_get_clientdata(client);
812 struct sensor_device_attribute_2 *sensor_attr =
813 to_sensor_dev_attr_2(attr);
814 int nr = sensor_attr->nr;
815 int index = sensor_attr->index;
816 u8 val = TEMP_TO_REG(simple_strtol(buf, NULL, 10), 0, 0x7f);
818 mutex_lock(&data->update_lock);
819 data->sf2_temp[index][nr] =
820 w83793_read_value(client, W83793_REG_SF2_TEMP(index, nr)) & 0x80;
821 data->sf2_temp[index][nr] |= val;
822 w83793_write_value(client, W83793_REG_SF2_TEMP(index, nr),
823 data->sf2_temp[index][nr]);
824 mutex_unlock(&data->update_lock);
828 /* only Vcore A/B and Vtt have additional 2 bits precision */
830 show_in(struct device *dev, struct device_attribute *attr, char *buf)
832 struct sensor_device_attribute_2 *sensor_attr =
833 to_sensor_dev_attr_2(attr);
834 int nr = sensor_attr->nr;
835 int index = sensor_attr->index;
836 struct w83793_data *data = w83793_update_device(dev);
837 u16 val = data->in[index][nr];
841 val += (data->in_low_bits[nr] >> (index * 2)) & 0x3;
843 /* voltage inputs 5VDD and 5VSB needs 150mV offset */
844 val = val * scale_in[index] + scale_in_add[index];
845 return sprintf(buf, "%d\n", val);
849 store_in(struct device *dev, struct device_attribute *attr,
850 const char *buf, size_t count)
852 struct sensor_device_attribute_2 *sensor_attr =
853 to_sensor_dev_attr_2(attr);
854 int nr = sensor_attr->nr;
855 int index = sensor_attr->index;
856 struct i2c_client *client = to_i2c_client(dev);
857 struct w83793_data *data = i2c_get_clientdata(client);
861 (simple_strtoul(buf, NULL, 10) +
862 scale_in[index] / 2) / scale_in[index];
863 mutex_lock(&data->update_lock);
865 /* fix the limit values of 5VDD and 5VSB to ALARM mechanism */
866 if (1 == nr || 2 == nr) {
867 val -= scale_in_add[index] / scale_in[index];
869 val = SENSORS_LIMIT(val, 0, 255);
871 val = SENSORS_LIMIT(val, 0, 0x3FF);
872 data->in_low_bits[nr] =
873 w83793_read_value(client, W83793_REG_IN_LOW_BITS[nr]);
874 data->in_low_bits[nr] &= ~(0x03 << (2 * index));
875 data->in_low_bits[nr] |= (val & 0x03) << (2 * index);
876 w83793_write_value(client, W83793_REG_IN_LOW_BITS[nr],
877 data->in_low_bits[nr]);
880 data->in[index][nr] = val;
881 w83793_write_value(client, W83793_REG_IN[index][nr],
882 data->in[index][nr]);
883 mutex_unlock(&data->update_lock);
889 #define SENSOR_ATTR_IN(index) \
890 SENSOR_ATTR_2(in##index##_input, S_IRUGO, show_in, NULL, \
892 SENSOR_ATTR_2(in##index##_max, S_IRUGO | S_IWUSR, show_in, \
893 store_in, IN_MAX, index), \
894 SENSOR_ATTR_2(in##index##_min, S_IRUGO | S_IWUSR, show_in, \
895 store_in, IN_LOW, index), \
896 SENSOR_ATTR_2(in##index##_alarm, S_IRUGO, show_alarm_beep, \
897 NULL, ALARM_STATUS, index + ((index > 2) ? 1 : 0)), \
898 SENSOR_ATTR_2(in##index##_beep, S_IWUSR | S_IRUGO, \
899 show_alarm_beep, store_beep, BEEP_ENABLE, \
900 index + ((index > 2) ? 1 : 0))
902 #define SENSOR_ATTR_FAN(index) \
903 SENSOR_ATTR_2(fan##index##_alarm, S_IRUGO, show_alarm_beep, \
904 NULL, ALARM_STATUS, index + 17), \
905 SENSOR_ATTR_2(fan##index##_beep, S_IWUSR | S_IRUGO, \
906 show_alarm_beep, store_beep, BEEP_ENABLE, index + 17), \
907 SENSOR_ATTR_2(fan##index##_input, S_IRUGO, show_fan, \
908 NULL, FAN_INPUT, index - 1), \
909 SENSOR_ATTR_2(fan##index##_min, S_IWUSR | S_IRUGO, \
910 show_fan, store_fan_min, FAN_MIN, index - 1)
912 #define SENSOR_ATTR_PWM(index) \
913 SENSOR_ATTR_2(pwm##index, S_IWUSR | S_IRUGO, show_pwm, \
914 store_pwm, PWM_DUTY, index - 1), \
915 SENSOR_ATTR_2(pwm##index##_nonstop, S_IWUSR | S_IRUGO, \
916 show_pwm, store_pwm, PWM_NONSTOP, index - 1), \
917 SENSOR_ATTR_2(pwm##index##_start, S_IWUSR | S_IRUGO, \
918 show_pwm, store_pwm, PWM_START, index - 1), \
919 SENSOR_ATTR_2(pwm##index##_stop_time, S_IWUSR | S_IRUGO, \
920 show_pwm, store_pwm, PWM_STOP_TIME, index - 1)
922 #define SENSOR_ATTR_TEMP(index) \
923 SENSOR_ATTR_2(temp##index##_type, S_IRUGO | S_IWUSR, \
924 show_temp_mode, store_temp_mode, NOT_USED, index - 1), \
925 SENSOR_ATTR_2(temp##index##_input, S_IRUGO, show_temp, \
926 NULL, TEMP_READ, index - 1), \
927 SENSOR_ATTR_2(temp##index##_max, S_IRUGO | S_IWUSR, show_temp, \
928 store_temp, TEMP_CRIT, index - 1), \
929 SENSOR_ATTR_2(temp##index##_max_hyst, S_IRUGO | S_IWUSR, \
930 show_temp, store_temp, TEMP_CRIT_HYST, index - 1), \
931 SENSOR_ATTR_2(temp##index##_warn, S_IRUGO | S_IWUSR, show_temp, \
932 store_temp, TEMP_WARN, index - 1), \
933 SENSOR_ATTR_2(temp##index##_warn_hyst, S_IRUGO | S_IWUSR, \
934 show_temp, store_temp, TEMP_WARN_HYST, index - 1), \
935 SENSOR_ATTR_2(temp##index##_alarm, S_IRUGO, \
936 show_alarm_beep, NULL, ALARM_STATUS, index + 11), \
937 SENSOR_ATTR_2(temp##index##_beep, S_IWUSR | S_IRUGO, \
938 show_alarm_beep, store_beep, BEEP_ENABLE, index + 11), \
939 SENSOR_ATTR_2(temp##index##_auto_channels_pwm, \
940 S_IRUGO | S_IWUSR, show_sf_ctrl, store_sf_ctrl, \
941 TEMP_FAN_MAP, index - 1), \
942 SENSOR_ATTR_2(temp##index##_pwm_enable, S_IWUSR | S_IRUGO, \
943 show_sf_ctrl, store_sf_ctrl, TEMP_PWM_ENABLE, \
945 SENSOR_ATTR_2(thermal_cruise##index, S_IRUGO | S_IWUSR, \
946 show_sf_ctrl, store_sf_ctrl, TEMP_CRUISE, index - 1), \
947 SENSOR_ATTR_2(tolerance##index, S_IRUGO | S_IWUSR, show_sf_ctrl,\
948 store_sf_ctrl, TEMP_TOLERANCE, index - 1), \
949 SENSOR_ATTR_2(temp##index##_auto_point1_pwm, S_IRUGO | S_IWUSR, \
950 show_sf2_pwm, store_sf2_pwm, 0, index - 1), \
951 SENSOR_ATTR_2(temp##index##_auto_point2_pwm, S_IRUGO | S_IWUSR, \
952 show_sf2_pwm, store_sf2_pwm, 1, index - 1), \
953 SENSOR_ATTR_2(temp##index##_auto_point3_pwm, S_IRUGO | S_IWUSR, \
954 show_sf2_pwm, store_sf2_pwm, 2, index - 1), \
955 SENSOR_ATTR_2(temp##index##_auto_point4_pwm, S_IRUGO | S_IWUSR, \
956 show_sf2_pwm, store_sf2_pwm, 3, index - 1), \
957 SENSOR_ATTR_2(temp##index##_auto_point5_pwm, S_IRUGO | S_IWUSR, \
958 show_sf2_pwm, store_sf2_pwm, 4, index - 1), \
959 SENSOR_ATTR_2(temp##index##_auto_point6_pwm, S_IRUGO | S_IWUSR, \
960 show_sf2_pwm, store_sf2_pwm, 5, index - 1), \
961 SENSOR_ATTR_2(temp##index##_auto_point7_pwm, S_IRUGO | S_IWUSR, \
962 show_sf2_pwm, store_sf2_pwm, 6, index - 1), \
963 SENSOR_ATTR_2(temp##index##_auto_point1_temp, S_IRUGO | S_IWUSR,\
964 show_sf2_temp, store_sf2_temp, 0, index - 1), \
965 SENSOR_ATTR_2(temp##index##_auto_point2_temp, S_IRUGO | S_IWUSR,\
966 show_sf2_temp, store_sf2_temp, 1, index - 1), \
967 SENSOR_ATTR_2(temp##index##_auto_point3_temp, S_IRUGO | S_IWUSR,\
968 show_sf2_temp, store_sf2_temp, 2, index - 1), \
969 SENSOR_ATTR_2(temp##index##_auto_point4_temp, S_IRUGO | S_IWUSR,\
970 show_sf2_temp, store_sf2_temp, 3, index - 1), \
971 SENSOR_ATTR_2(temp##index##_auto_point5_temp, S_IRUGO | S_IWUSR,\
972 show_sf2_temp, store_sf2_temp, 4, index - 1), \
973 SENSOR_ATTR_2(temp##index##_auto_point6_temp, S_IRUGO | S_IWUSR,\
974 show_sf2_temp, store_sf2_temp, 5, index - 1), \
975 SENSOR_ATTR_2(temp##index##_auto_point7_temp, S_IRUGO | S_IWUSR,\
976 show_sf2_temp, store_sf2_temp, 6, index - 1)
978 static struct sensor_device_attribute_2 w83793_sensor_attr_2[] = {
1006 static struct sensor_device_attribute_2 w83793_left_fan[] = {
1011 SENSOR_ATTR_FAN(10),
1012 SENSOR_ATTR_FAN(11),
1013 SENSOR_ATTR_FAN(12),
1017 static struct sensor_device_attribute_2 w83793_left_pwm[] = {
1025 static struct sensor_device_attribute_2 sda_single_files[] = {
1026 SENSOR_ATTR_2(cpu0_vid, S_IRUGO, show_vid, NULL, NOT_USED, 0),
1027 SENSOR_ATTR_2(cpu1_vid, S_IRUGO, show_vid, NULL, NOT_USED, 1),
1028 SENSOR_ATTR_2(vrm, S_IWUSR | S_IRUGO, show_vrm, store_vrm,
1029 NOT_USED, NOT_USED),
1030 SENSOR_ATTR_2(chassis, S_IWUSR | S_IRUGO, show_alarm_beep,
1031 store_chassis_clear, ALARM_STATUS, 30),
1032 SENSOR_ATTR_2(beep_enable, S_IWUSR | S_IRUGO, show_beep_enable,
1033 store_beep_enable, NOT_USED, NOT_USED),
1034 SENSOR_ATTR_2(pwm_default, S_IWUSR | S_IRUGO, show_sf_setup,
1035 store_sf_setup, SETUP_PWM_DEFAULT, NOT_USED),
1036 SENSOR_ATTR_2(pwm_uptime, S_IWUSR | S_IRUGO, show_sf_setup,
1037 store_sf_setup, SETUP_PWM_UPTIME, NOT_USED),
1038 SENSOR_ATTR_2(pwm_downtime, S_IWUSR | S_IRUGO, show_sf_setup,
1039 store_sf_setup, SETUP_PWM_DOWNTIME, NOT_USED),
1040 SENSOR_ATTR_2(temp_critical, S_IWUSR | S_IRUGO, show_sf_setup,
1041 store_sf_setup, SETUP_TEMP_CRITICAL, NOT_USED),
1044 static void w83793_init_client(struct i2c_client *client)
1047 w83793_write_value(client, W83793_REG_CONFIG, 0x80);
1050 /* Start monitoring */
1051 w83793_write_value(client, W83793_REG_CONFIG,
1052 w83793_read_value(client, W83793_REG_CONFIG) | 0x01);
1056 static int w83793_attach_adapter(struct i2c_adapter *adapter)
1058 if (!(adapter->class & I2C_CLASS_HWMON))
1060 return i2c_probe(adapter, &addr_data, w83793_detect);
1063 static int w83793_detach_client(struct i2c_client *client)
1065 struct w83793_data *data = i2c_get_clientdata(client);
1066 struct device *dev = &client->dev;
1071 hwmon_device_unregister(data->class_dev);
1073 for (i = 0; i < ARRAY_SIZE(w83793_sensor_attr_2); i++)
1074 device_remove_file(dev,
1075 &w83793_sensor_attr_2[i].dev_attr);
1077 for (i = 0; i < ARRAY_SIZE(sda_single_files); i++)
1078 device_remove_file(dev, &sda_single_files[i].dev_attr);
1080 for (i = 0; i < ARRAY_SIZE(w83793_left_fan); i++)
1081 device_remove_file(dev, &w83793_left_fan[i].dev_attr);
1083 for (i = 0; i < ARRAY_SIZE(w83793_left_pwm); i++)
1084 device_remove_file(dev, &w83793_left_pwm[i].dev_attr);
1087 if ((err = i2c_detach_client(client)))
1101 w83793_create_subclient(struct i2c_adapter *adapter,
1102 struct i2c_client *client, int addr,
1103 struct i2c_client **sub_cli)
1106 struct i2c_client *sub_client;
1108 (*sub_cli) = sub_client =
1109 kzalloc(sizeof(struct i2c_client), GFP_KERNEL);
1110 if (!(sub_client)) {
1113 sub_client->addr = 0x48 + addr;
1114 i2c_set_clientdata(sub_client, NULL);
1115 sub_client->adapter = adapter;
1116 sub_client->driver = &w83793_driver;
1117 strlcpy(sub_client->name, "w83793 subclient", I2C_NAME_SIZE);
1118 if ((err = i2c_attach_client(sub_client))) {
1119 dev_err(&client->dev, "subclient registration "
1120 "at address 0x%x failed\n", sub_client->addr);
1127 w83793_detect_subclients(struct i2c_adapter *adapter, int address,
1128 int kind, struct i2c_client *client)
1132 struct w83793_data *data = i2c_get_clientdata(client);
1134 id = i2c_adapter_id(adapter);
1135 if (force_subclients[0] == id && force_subclients[1] == address) {
1136 for (i = 2; i <= 3; i++) {
1137 if (force_subclients[i] < 0x48
1138 || force_subclients[i] > 0x4f) {
1139 dev_err(&client->dev,
1140 "invalid subclient "
1141 "address %d; must be 0x48-0x4f\n",
1142 force_subclients[i]);
1147 w83793_write_value(client, W83793_REG_I2C_SUBADDR,
1148 (force_subclients[2] & 0x07) |
1149 ((force_subclients[3] & 0x07) << 4));
1152 tmp = w83793_read_value(client, W83793_REG_I2C_SUBADDR);
1153 if (!(tmp & 0x08)) {
1155 w83793_create_subclient(adapter, client, tmp & 0x7,
1160 if (!(tmp & 0x80)) {
1161 if ((data->lm75[0] != NULL)
1162 && ((tmp & 0x7) == ((tmp >> 4) & 0x7))) {
1163 dev_err(&client->dev,
1164 "duplicate addresses 0x%x, "
1165 "use force_subclients\n", data->lm75[0]->addr);
1169 err = w83793_create_subclient(adapter, client,
1170 (tmp >> 4) & 0x7, &data->lm75[1]);
1177 /* Undo inits in case of errors */
1180 if (data->lm75[0] != NULL) {
1181 i2c_detach_client(data->lm75[0]);
1182 kfree(data->lm75[0]);
1188 static int w83793_detect(struct i2c_adapter *adapter, int address, int kind)
1192 struct i2c_client *client;
1194 struct w83793_data *data;
1195 int files_fan = ARRAY_SIZE(w83793_left_fan) / 7;
1196 int files_pwm = ARRAY_SIZE(w83793_left_pwm) / 5;
1199 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA)) {
1203 /* OK. For now, we presume we have a valid client. We now create the
1204 client structure, even though we cannot fill it completely yet.
1205 But it allows us to access w83793_{read,write}_value. */
1207 if (!(data = kzalloc(sizeof(struct w83793_data), GFP_KERNEL))) {
1212 client = &data->client;
1214 i2c_set_clientdata(client, data);
1215 client->addr = address;
1216 client->adapter = adapter;
1217 client->driver = &w83793_driver;
1219 data->bank = i2c_smbus_read_byte_data(client, W83793_REG_BANKSEL);
1221 /* Now, we do the remaining detection. */
1223 tmp = data->bank & 0x80 ? 0x5c : 0xa3;
1224 /* Check Winbond vendor ID */
1225 if (tmp != i2c_smbus_read_byte_data(client,
1226 W83793_REG_VENDORID)) {
1227 pr_debug("w83793: Detection failed at check "
1233 /* If Winbond chip, address of chip and W83793_REG_I2C_ADDR
1235 if ((data->bank & 0x07) == 0
1236 && i2c_smbus_read_byte_data(client, W83793_REG_I2C_ADDR) !=
1238 pr_debug("w83793: Detection failed at check "
1246 /* We have either had a force parameter, or we have already detected the
1247 Winbond. Determine the chip type now */
1250 if (0x7b == w83793_read_value(client, W83793_REG_CHIPID)) {
1254 dev_warn(&adapter->dev, "w83793: Ignoring "
1255 "'force' parameter for unknown chip "
1256 "at address 0x%02x\n", address);
1262 /* Fill in the remaining client fields and put into the global list */
1263 strlcpy(client->name, "w83793", I2C_NAME_SIZE);
1265 mutex_init(&data->update_lock);
1267 /* Tell the I2C layer a new client has arrived */
1268 if ((err = i2c_attach_client(client)))
1271 if ((err = w83793_detect_subclients(adapter, address, kind, client)))
1274 /* Initialize the chip */
1275 w83793_init_client(client);
1277 data->vrm = vid_which_vrm();
1279 Only fan 1-5 has their own input pins,
1280 Pwm 1-3 has their own pins
1282 data->has_fan = 0x1f;
1283 data->has_pwm = 0x07;
1284 tmp = w83793_read_value(client, W83793_REG_MFC);
1285 val = w83793_read_value(client, W83793_REG_FANIN_CTRL);
1287 /* check the function of pins 49-56 */
1288 if (!(tmp & 0x80)) {
1289 data->has_pwm |= 0x18; /* pwm 4,5 */
1290 if (val & 0x01) { /* fan 6 */
1291 data->has_fan |= 0x20;
1292 data->has_pwm |= 0x20;
1294 if (val & 0x02) { /* fan 7 */
1295 data->has_fan |= 0x40;
1296 data->has_pwm |= 0x40;
1298 if (!(tmp & 0x40) && (val & 0x04)) { /* fan 8 */
1299 data->has_fan |= 0x80;
1300 data->has_pwm |= 0x80;
1304 if (0x08 == (tmp & 0x0c)) {
1305 if (val & 0x08) /* fan 9 */
1306 data->has_fan |= 0x100;
1307 if (val & 0x10) /* fan 10 */
1308 data->has_fan |= 0x200;
1311 if (0x20 == (tmp & 0x30)) {
1312 if (val & 0x20) /* fan 11 */
1313 data->has_fan |= 0x400;
1314 if (val & 0x40) /* fan 12 */
1315 data->has_fan |= 0x800;
1318 if ((tmp & 0x01) && (val & 0x04)) { /* fan 8, second location */
1319 data->has_fan |= 0x80;
1320 data->has_pwm |= 0x80;
1323 /* Register sysfs hooks */
1324 for (i = 0; i < ARRAY_SIZE(w83793_sensor_attr_2); i++) {
1325 err = device_create_file(dev,
1326 &w83793_sensor_attr_2[i].dev_attr);
1331 for (i = 0; i < ARRAY_SIZE(sda_single_files); i++) {
1332 err = device_create_file(dev, &sda_single_files[i].dev_attr);
1338 for (i = 5; i < 12; i++) {
1340 if (!(data->has_fan & (1 << i)))
1342 for (j = 0; j < files_fan; j++) {
1343 err = device_create_file(dev,
1344 &w83793_left_fan[(i - 5) * files_fan
1351 for (i = 3; i < 8; i++) {
1353 if (!(data->has_pwm & (1 << i)))
1355 for (j = 0; j < files_pwm; j++) {
1356 err = device_create_file(dev,
1357 &w83793_left_pwm[(i - 3) * files_pwm
1364 data->class_dev = hwmon_device_register(dev);
1365 if (IS_ERR(data->class_dev)) {
1366 err = PTR_ERR(data->class_dev);
1372 /* Unregister sysfs hooks */
1375 for (i = 0; i < ARRAY_SIZE(w83793_sensor_attr_2); i++)
1376 device_remove_file(dev, &w83793_sensor_attr_2[i].dev_attr);
1378 for (i = 0; i < ARRAY_SIZE(sda_single_files); i++)
1379 device_remove_file(dev, &sda_single_files[i].dev_attr);
1381 for (i = 0; i < ARRAY_SIZE(w83793_left_fan); i++)
1382 device_remove_file(dev, &w83793_left_fan[i].dev_attr);
1384 for (i = 0; i < ARRAY_SIZE(w83793_left_pwm); i++)
1385 device_remove_file(dev, &w83793_left_pwm[i].dev_attr);
1387 if (data->lm75[0] != NULL) {
1388 i2c_detach_client(data->lm75[0]);
1389 kfree(data->lm75[0]);
1391 if (data->lm75[1] != NULL) {
1392 i2c_detach_client(data->lm75[1]);
1393 kfree(data->lm75[1]);
1396 i2c_detach_client(client);
1403 static void w83793_update_nonvolatile(struct device *dev)
1405 struct i2c_client *client = to_i2c_client(dev);
1406 struct w83793_data *data = i2c_get_clientdata(client);
1409 They are somewhat "stable" registers, and to update them everytime
1410 takes so much time, it's just not worthy. Update them in a long
1411 interval to avoid exception.
1413 if (!(time_after(jiffies, data->last_nonvolatile + HZ * 300)
1416 /* update voltage limits */
1417 for (i = 1; i < 3; i++) {
1418 for (j = 0; j < ARRAY_SIZE(data->in); j++) {
1420 w83793_read_value(client, W83793_REG_IN[j][i]);
1422 data->in_low_bits[i] =
1423 w83793_read_value(client, W83793_REG_IN_LOW_BITS[i]);
1426 for (i = 0; i < ARRAY_SIZE(data->fan_min); i++) {
1427 /* Update the Fan measured value and limits */
1428 if (!(data->has_fan & (1 << i))) {
1432 w83793_read_value(client, W83793_REG_FAN_MIN(i)) << 8;
1434 w83793_read_value(client, W83793_REG_FAN_MIN(i) + 1);
1437 for (i = 0; i < ARRAY_SIZE(data->temp_fan_map); i++) {
1438 data->temp_fan_map[i] =
1439 w83793_read_value(client, W83793_REG_TEMP_FAN_MAP(i));
1440 for (j = 1; j < 5; j++) {
1442 w83793_read_value(client, W83793_REG_TEMP[i][j]);
1444 data->temp_cruise[i] =
1445 w83793_read_value(client, W83793_REG_TEMP_CRUISE(i));
1446 for (j = 0; j < 7; j++) {
1447 data->sf2_pwm[i][j] =
1448 w83793_read_value(client, W83793_REG_SF2_PWM(i, j));
1449 data->sf2_temp[i][j] =
1450 w83793_read_value(client,
1451 W83793_REG_SF2_TEMP(i, j));
1455 for (i = 0; i < ARRAY_SIZE(data->temp_mode); i++)
1456 data->temp_mode[i] =
1457 w83793_read_value(client, W83793_REG_TEMP_MODE[i]);
1459 for (i = 0; i < ARRAY_SIZE(data->tolerance); i++) {
1460 data->tolerance[i] =
1461 w83793_read_value(client, W83793_REG_TEMP_TOL(i));
1464 for (i = 0; i < ARRAY_SIZE(data->pwm); i++) {
1465 if (!(data->has_pwm & (1 << i)))
1467 data->pwm[i][PWM_NONSTOP] =
1468 w83793_read_value(client, W83793_REG_PWM(i, PWM_NONSTOP));
1469 data->pwm[i][PWM_START] =
1470 w83793_read_value(client, W83793_REG_PWM(i, PWM_START));
1471 data->pwm_stop_time[i] =
1472 w83793_read_value(client, W83793_REG_PWM_STOP_TIME(i));
1475 data->pwm_default = w83793_read_value(client, W83793_REG_PWM_DEFAULT);
1476 data->pwm_enable = w83793_read_value(client, W83793_REG_PWM_ENABLE);
1477 data->pwm_uptime = w83793_read_value(client, W83793_REG_PWM_UPTIME);
1478 data->pwm_downtime = w83793_read_value(client, W83793_REG_PWM_DOWNTIME);
1479 data->temp_critical =
1480 w83793_read_value(client, W83793_REG_TEMP_CRITICAL);
1481 data->beep_enable = w83793_read_value(client, W83793_REG_OVT_BEEP);
1483 for (i = 0; i < ARRAY_SIZE(data->beeps); i++) {
1484 data->beeps[i] = w83793_read_value(client, W83793_REG_BEEP(i));
1487 data->last_nonvolatile = jiffies;
1490 static struct w83793_data *w83793_update_device(struct device *dev)
1492 struct i2c_client *client = to_i2c_client(dev);
1493 struct w83793_data *data = i2c_get_clientdata(client);
1496 mutex_lock(&data->update_lock);
1498 if (!(time_after(jiffies, data->last_updated + HZ * 2)
1502 /* Update the voltages measured value and limits */
1503 for (i = 0; i < ARRAY_SIZE(data->in); i++)
1504 data->in[i][IN_READ] =
1505 w83793_read_value(client, W83793_REG_IN[i][IN_READ]);
1507 data->in_low_bits[IN_READ] =
1508 w83793_read_value(client, W83793_REG_IN_LOW_BITS[IN_READ]);
1510 for (i = 0; i < ARRAY_SIZE(data->fan); i++) {
1511 if (!(data->has_fan & (1 << i))) {
1515 w83793_read_value(client, W83793_REG_FAN(i)) << 8;
1517 w83793_read_value(client, W83793_REG_FAN(i) + 1);
1520 for (i = 0; i < ARRAY_SIZE(data->temp); i++)
1521 data->temp[i][TEMP_READ] =
1522 w83793_read_value(client, W83793_REG_TEMP[i][TEMP_READ]);
1524 data->temp_low_bits =
1525 w83793_read_value(client, W83793_REG_TEMP_LOW_BITS);
1527 for (i = 0; i < ARRAY_SIZE(data->pwm); i++) {
1528 if (data->has_pwm & (1 << i))
1529 data->pwm[i][PWM_DUTY] =
1530 w83793_read_value(client,
1531 W83793_REG_PWM(i, PWM_DUTY));
1534 for (i = 0; i < ARRAY_SIZE(data->alarms); i++)
1536 w83793_read_value(client, W83793_REG_ALARM(i));
1537 data->vid[0] = w83793_read_value(client, W83793_REG_VID_INA);
1538 data->vid[1] = w83793_read_value(client, W83793_REG_VID_INB);
1539 w83793_update_nonvolatile(dev);
1540 data->last_updated = jiffies;
1544 mutex_unlock(&data->update_lock);
1548 /* Ignore the possibility that somebody change bank outside the driver
1549 Must be called with data->update_lock held, except during initialization */
1550 static u8 w83793_read_value(struct i2c_client *client, u16 reg)
1552 struct w83793_data *data = i2c_get_clientdata(client);
1554 u8 new_bank = reg >> 8;
1556 new_bank |= data->bank & 0xfc;
1557 if (data->bank != new_bank) {
1558 if (i2c_smbus_write_byte_data
1559 (client, W83793_REG_BANKSEL, new_bank) >= 0)
1560 data->bank = new_bank;
1562 dev_err(&client->dev,
1563 "set bank to %d failed, fall back "
1564 "to bank %d, read reg 0x%x error\n",
1565 new_bank, data->bank, reg);
1566 res = 0x0; /* read 0x0 from the chip */
1570 res = i2c_smbus_read_byte_data(client, reg & 0xff);
1575 /* Must be called with data->update_lock held, except during initialization */
1576 static int w83793_write_value(struct i2c_client *client, u16 reg, u8 value)
1578 struct w83793_data *data = i2c_get_clientdata(client);
1580 u8 new_bank = reg >> 8;
1582 new_bank |= data->bank & 0xfc;
1583 if (data->bank != new_bank) {
1584 if ((res = i2c_smbus_write_byte_data
1585 (client, W83793_REG_BANKSEL, new_bank)) >= 0)
1586 data->bank = new_bank;
1588 dev_err(&client->dev,
1589 "set bank to %d failed, fall back "
1590 "to bank %d, write reg 0x%x error\n",
1591 new_bank, data->bank, reg);
1596 res = i2c_smbus_write_byte_data(client, reg & 0xff, value);
1601 static int __init sensors_w83793_init(void)
1603 return i2c_add_driver(&w83793_driver);
1606 static void __exit sensors_w83793_exit(void)
1608 i2c_del_driver(&w83793_driver);
1611 MODULE_AUTHOR("Yuan Mu");
1612 MODULE_DESCRIPTION("w83793 driver");
1613 MODULE_LICENSE("GPL");
1615 module_init(sensors_w83793_init);
1616 module_exit(sensors_w83793_exit);