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 */
208 u8 pwm_enable; /* Register value, each Temp has 1 bit */
209 u8 pwm_uptime; /* Register value */
210 u8 pwm_downtime; /* Register value */
211 u8 pwm_default; /* All fan default pwm, next poweron valid */
212 u8 pwm[8][3]; /* Register value */
216 u8 alarms[5]; /* realtime status registers */
219 u8 tolerance[3]; /* Temp tolerance(Smart Fan I/II) */
220 u8 sf2_pwm[6][7]; /* Smart FanII: Fan duty cycle */
221 u8 sf2_temp[6][7]; /* Smart FanII: Temp level point */
224 static u8 w83793_read_value(struct i2c_client *client, u16 reg);
225 static int w83793_write_value(struct i2c_client *client, u16 reg, u8 value);
226 static int w83793_attach_adapter(struct i2c_adapter *adapter);
227 static int w83793_detect(struct i2c_adapter *adapter, int address, int kind);
228 static int w83793_detach_client(struct i2c_client *client);
229 static void w83793_init_client(struct i2c_client *client);
230 static void w83793_update_nonvolatile(struct device *dev);
231 static struct w83793_data *w83793_update_device(struct device *dev);
233 static struct i2c_driver w83793_driver = {
237 .attach_adapter = w83793_attach_adapter,
238 .detach_client = w83793_detach_client,
242 show_vrm(struct device *dev, struct device_attribute *attr, char *buf)
244 struct i2c_client *client = to_i2c_client(dev);
245 struct w83793_data *data = i2c_get_clientdata(client);
247 return sprintf(buf, "%d\n", data->vrm);
251 show_vid(struct device *dev, struct device_attribute *attr, char *buf)
253 struct w83793_data *data = w83793_update_device(dev);
254 struct sensor_device_attribute_2 *sensor_attr =
255 to_sensor_dev_attr_2(attr);
256 int index = sensor_attr->index;
258 return sprintf(buf, "%d\n", vid_from_reg(data->vid[index], data->vrm));
262 store_vrm(struct device *dev, struct device_attribute *attr,
263 const char *buf, size_t count)
265 struct i2c_client *client = to_i2c_client(dev);
266 struct w83793_data *data = i2c_get_clientdata(client);
268 data->vrm = simple_strtoul(buf, NULL, 10);
272 #define ALARM_STATUS 0
273 #define BEEP_ENABLE 1
275 show_alarm_beep(struct device *dev, struct device_attribute *attr, char *buf)
277 struct w83793_data *data = w83793_update_device(dev);
278 struct sensor_device_attribute_2 *sensor_attr =
279 to_sensor_dev_attr_2(attr);
280 int nr = sensor_attr->nr;
281 int index = sensor_attr->index >> 3;
282 int bit = sensor_attr->index & 0x07;
285 if (ALARM_STATUS == nr) {
286 val = (data->alarms[index] >> (bit)) & 1;
287 } else { /* BEEP_ENABLE */
288 val = (data->beeps[index] >> (bit)) & 1;
291 return sprintf(buf, "%u\n", val);
295 store_beep(struct device *dev, struct device_attribute *attr,
296 const char *buf, size_t count)
298 struct i2c_client *client = to_i2c_client(dev);
299 struct w83793_data *data = i2c_get_clientdata(client);
300 struct sensor_device_attribute_2 *sensor_attr =
301 to_sensor_dev_attr_2(attr);
302 int index = sensor_attr->index >> 3;
303 int shift = sensor_attr->index & 0x07;
304 u8 beep_bit = 1 << shift;
307 val = simple_strtoul(buf, NULL, 10);
308 if (val != 0 && val != 1)
311 mutex_lock(&data->update_lock);
312 data->beeps[index] = w83793_read_value(client, W83793_REG_BEEP(index));
313 data->beeps[index] &= ~beep_bit;
314 data->beeps[index] |= val << shift;
315 w83793_write_value(client, W83793_REG_BEEP(index), data->beeps[index]);
316 mutex_unlock(&data->update_lock);
322 show_beep_enable(struct device *dev, struct device_attribute *attr, char *buf)
324 struct w83793_data *data = w83793_update_device(dev);
325 return sprintf(buf, "%u\n", (data->beep_enable >> 1) & 0x01);
329 store_beep_enable(struct device *dev, struct device_attribute *attr,
330 const char *buf, size_t count)
332 struct i2c_client *client = to_i2c_client(dev);
333 struct w83793_data *data = i2c_get_clientdata(client);
334 u8 val = simple_strtoul(buf, NULL, 10);
336 if (val != 0 && val != 1)
339 mutex_lock(&data->update_lock);
340 data->beep_enable = w83793_read_value(client, W83793_REG_OVT_BEEP)
342 data->beep_enable |= val << 1;
343 w83793_write_value(client, W83793_REG_OVT_BEEP, data->beep_enable);
344 mutex_unlock(&data->update_lock);
349 /* Write any value to clear chassis alarm */
351 store_chassis_clear(struct device *dev,
352 struct device_attribute *attr, const char *buf,
355 struct i2c_client *client = to_i2c_client(dev);
356 struct w83793_data *data = i2c_get_clientdata(client);
359 mutex_lock(&data->update_lock);
360 val = w83793_read_value(client, W83793_REG_CLR_CHASSIS);
362 w83793_write_value(client, W83793_REG_CLR_CHASSIS, val);
363 mutex_unlock(&data->update_lock);
370 show_fan(struct device *dev, struct device_attribute *attr, char *buf)
372 struct sensor_device_attribute_2 *sensor_attr =
373 to_sensor_dev_attr_2(attr);
374 int nr = sensor_attr->nr;
375 int index = sensor_attr->index;
376 struct w83793_data *data = w83793_update_device(dev);
379 if (FAN_INPUT == nr) {
380 val = data->fan[index] & 0x0fff;
382 val = data->fan_min[index] & 0x0fff;
385 return sprintf(buf, "%lu\n", FAN_FROM_REG(val));
389 store_fan_min(struct device *dev, struct device_attribute *attr,
390 const char *buf, size_t count)
392 struct sensor_device_attribute_2 *sensor_attr =
393 to_sensor_dev_attr_2(attr);
394 int index = sensor_attr->index;
395 struct i2c_client *client = to_i2c_client(dev);
396 struct w83793_data *data = i2c_get_clientdata(client);
397 u16 val = FAN_TO_REG(simple_strtoul(buf, NULL, 10));
399 mutex_lock(&data->update_lock);
400 data->fan_min[index] = val;
401 w83793_write_value(client, W83793_REG_FAN_MIN(index),
403 w83793_write_value(client, W83793_REG_FAN_MIN(index) + 1, val & 0xff);
404 mutex_unlock(&data->update_lock);
411 #define PWM_NONSTOP 2
412 #define PWM_STOP_TIME 3
414 show_pwm(struct device *dev, struct device_attribute *attr, char *buf)
416 struct sensor_device_attribute_2 *sensor_attr =
417 to_sensor_dev_attr_2(attr);
418 struct w83793_data *data = w83793_update_device(dev);
420 int nr = sensor_attr->nr;
421 int index = sensor_attr->index;
423 if (PWM_STOP_TIME == nr)
424 val = TIME_FROM_REG(data->pwm_stop_time[index]);
426 val = (data->pwm[index][nr] & 0x3f) << 2;
428 return sprintf(buf, "%d\n", val);
432 store_pwm(struct device *dev, struct device_attribute *attr,
433 const char *buf, size_t count)
435 struct i2c_client *client = to_i2c_client(dev);
436 struct w83793_data *data = i2c_get_clientdata(client);
437 struct sensor_device_attribute_2 *sensor_attr =
438 to_sensor_dev_attr_2(attr);
439 int nr = sensor_attr->nr;
440 int index = sensor_attr->index;
443 mutex_lock(&data->update_lock);
444 if (PWM_STOP_TIME == nr) {
445 val = TIME_TO_REG(simple_strtoul(buf, NULL, 10));
446 data->pwm_stop_time[index] = val;
447 w83793_write_value(client, W83793_REG_PWM_STOP_TIME(index),
450 val = SENSORS_LIMIT(simple_strtoul(buf, NULL, 10), 0, 0xff)
452 data->pwm[index][nr] =
453 w83793_read_value(client, W83793_REG_PWM(index, nr)) & 0xc0;
454 data->pwm[index][nr] |= val;
455 w83793_write_value(client, W83793_REG_PWM(index, nr),
456 data->pwm[index][nr]);
459 mutex_unlock(&data->update_lock);
464 show_temp(struct device *dev, struct device_attribute *attr, char *buf)
466 struct sensor_device_attribute_2 *sensor_attr =
467 to_sensor_dev_attr_2(attr);
468 int nr = sensor_attr->nr;
469 int index = sensor_attr->index;
470 struct w83793_data *data = w83793_update_device(dev);
471 long temp = TEMP_FROM_REG(data->temp[index][nr]);
473 if (TEMP_READ == nr && index < 4) { /* Only TD1-TD4 have low bits */
474 int low = ((data->temp_low_bits >> (index * 2)) & 0x03) * 250;
475 temp += temp > 0 ? low : -low;
477 return sprintf(buf, "%ld\n", temp);
481 store_temp(struct device *dev, struct device_attribute *attr,
482 const char *buf, size_t count)
484 struct sensor_device_attribute_2 *sensor_attr =
485 to_sensor_dev_attr_2(attr);
486 int nr = sensor_attr->nr;
487 int index = sensor_attr->index;
488 struct i2c_client *client = to_i2c_client(dev);
489 struct w83793_data *data = i2c_get_clientdata(client);
490 long tmp = simple_strtol(buf, NULL, 10);
492 mutex_lock(&data->update_lock);
493 data->temp[index][nr] = TEMP_TO_REG(tmp, -128, 127);
494 w83793_write_value(client, W83793_REG_TEMP[index][nr],
495 data->temp[index][nr]);
496 mutex_unlock(&data->update_lock);
502 each has 4 mode:(2 bits)
504 1: Use internal temp sensor(default)
506 3: Use sensor in Intel CPU and get result by PECI
509 each has 2 mode:(1 bit)
510 0: Disable temp sensor monitor
511 1: To enable temp sensors monitor
514 /* 0 disable, 6 PECI */
515 static u8 TO_TEMP_MODE[] = { 0, 0, 0, 6 };
518 show_temp_mode(struct device *dev, struct device_attribute *attr, char *buf)
520 struct w83793_data *data = w83793_update_device(dev);
521 struct sensor_device_attribute_2 *sensor_attr =
522 to_sensor_dev_attr_2(attr);
523 int index = sensor_attr->index;
524 u8 mask = (index < 4) ? 0x03 : 0x01;
525 u8 shift = (index < 4) ? (2 * index) : (index - 4);
527 index = (index < 4) ? 0 : 1;
529 tmp = (data->temp_mode[index] >> shift) & mask;
531 /* for the internal sensor, found out if diode or thermistor */
533 tmp = index == 0 ? 3 : 4;
535 tmp = TO_TEMP_MODE[tmp];
538 return sprintf(buf, "%d\n", tmp);
542 store_temp_mode(struct device *dev, struct device_attribute *attr,
543 const char *buf, size_t count)
545 struct i2c_client *client = to_i2c_client(dev);
546 struct w83793_data *data = i2c_get_clientdata(client);
547 struct sensor_device_attribute_2 *sensor_attr =
548 to_sensor_dev_attr_2(attr);
549 int index = sensor_attr->index;
550 u8 mask = (index < 4) ? 0x03 : 0x01;
551 u8 shift = (index < 4) ? (2 * index) : (index - 4);
552 u8 val = simple_strtoul(buf, NULL, 10);
554 /* transform the sysfs interface values into table above */
555 if ((val == 6) && (index < 4)) {
557 } else if ((val == 3 && index < 4)
558 || (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[] = {
999 static struct sensor_device_attribute_2 w83793_temp[] = {
1000 SENSOR_ATTR_TEMP(1),
1001 SENSOR_ATTR_TEMP(2),
1002 SENSOR_ATTR_TEMP(3),
1003 SENSOR_ATTR_TEMP(4),
1004 SENSOR_ATTR_TEMP(5),
1005 SENSOR_ATTR_TEMP(6),
1009 static struct sensor_device_attribute_2 w83793_left_fan[] = {
1014 SENSOR_ATTR_FAN(10),
1015 SENSOR_ATTR_FAN(11),
1016 SENSOR_ATTR_FAN(12),
1020 static struct sensor_device_attribute_2 w83793_left_pwm[] = {
1028 static struct sensor_device_attribute_2 sda_single_files[] = {
1029 SENSOR_ATTR_2(cpu0_vid, S_IRUGO, show_vid, NULL, NOT_USED, 0),
1030 SENSOR_ATTR_2(cpu1_vid, S_IRUGO, show_vid, NULL, NOT_USED, 1),
1031 SENSOR_ATTR_2(vrm, S_IWUSR | S_IRUGO, show_vrm, store_vrm,
1032 NOT_USED, NOT_USED),
1033 SENSOR_ATTR_2(chassis, S_IWUSR | S_IRUGO, show_alarm_beep,
1034 store_chassis_clear, ALARM_STATUS, 30),
1035 SENSOR_ATTR_2(beep_enable, S_IWUSR | S_IRUGO, show_beep_enable,
1036 store_beep_enable, NOT_USED, NOT_USED),
1037 SENSOR_ATTR_2(pwm_default, S_IWUSR | S_IRUGO, show_sf_setup,
1038 store_sf_setup, SETUP_PWM_DEFAULT, NOT_USED),
1039 SENSOR_ATTR_2(pwm_uptime, S_IWUSR | S_IRUGO, show_sf_setup,
1040 store_sf_setup, SETUP_PWM_UPTIME, NOT_USED),
1041 SENSOR_ATTR_2(pwm_downtime, S_IWUSR | S_IRUGO, show_sf_setup,
1042 store_sf_setup, SETUP_PWM_DOWNTIME, NOT_USED),
1043 SENSOR_ATTR_2(temp_critical, S_IWUSR | S_IRUGO, show_sf_setup,
1044 store_sf_setup, SETUP_TEMP_CRITICAL, NOT_USED),
1047 static void w83793_init_client(struct i2c_client *client)
1050 w83793_write_value(client, W83793_REG_CONFIG, 0x80);
1053 /* Start monitoring */
1054 w83793_write_value(client, W83793_REG_CONFIG,
1055 w83793_read_value(client, W83793_REG_CONFIG) | 0x01);
1059 static int w83793_attach_adapter(struct i2c_adapter *adapter)
1061 if (!(adapter->class & I2C_CLASS_HWMON))
1063 return i2c_probe(adapter, &addr_data, w83793_detect);
1066 static int w83793_detach_client(struct i2c_client *client)
1068 struct w83793_data *data = i2c_get_clientdata(client);
1069 struct device *dev = &client->dev;
1074 hwmon_device_unregister(data->class_dev);
1076 for (i = 0; i < ARRAY_SIZE(w83793_sensor_attr_2); i++)
1077 device_remove_file(dev,
1078 &w83793_sensor_attr_2[i].dev_attr);
1080 for (i = 0; i < ARRAY_SIZE(sda_single_files); i++)
1081 device_remove_file(dev, &sda_single_files[i].dev_attr);
1083 for (i = 0; i < ARRAY_SIZE(w83793_left_fan); i++)
1084 device_remove_file(dev, &w83793_left_fan[i].dev_attr);
1086 for (i = 0; i < ARRAY_SIZE(w83793_left_pwm); i++)
1087 device_remove_file(dev, &w83793_left_pwm[i].dev_attr);
1089 for (i = 0; i < ARRAY_SIZE(w83793_temp); i++)
1090 device_remove_file(dev, &w83793_temp[i].dev_attr);
1093 if ((err = i2c_detach_client(client)))
1107 w83793_create_subclient(struct i2c_adapter *adapter,
1108 struct i2c_client *client, int addr,
1109 struct i2c_client **sub_cli)
1112 struct i2c_client *sub_client;
1114 (*sub_cli) = sub_client =
1115 kzalloc(sizeof(struct i2c_client), GFP_KERNEL);
1116 if (!(sub_client)) {
1119 sub_client->addr = 0x48 + addr;
1120 i2c_set_clientdata(sub_client, NULL);
1121 sub_client->adapter = adapter;
1122 sub_client->driver = &w83793_driver;
1123 strlcpy(sub_client->name, "w83793 subclient", I2C_NAME_SIZE);
1124 if ((err = i2c_attach_client(sub_client))) {
1125 dev_err(&client->dev, "subclient registration "
1126 "at address 0x%x failed\n", sub_client->addr);
1133 w83793_detect_subclients(struct i2c_adapter *adapter, int address,
1134 int kind, struct i2c_client *client)
1138 struct w83793_data *data = i2c_get_clientdata(client);
1140 id = i2c_adapter_id(adapter);
1141 if (force_subclients[0] == id && force_subclients[1] == address) {
1142 for (i = 2; i <= 3; i++) {
1143 if (force_subclients[i] < 0x48
1144 || force_subclients[i] > 0x4f) {
1145 dev_err(&client->dev,
1146 "invalid subclient "
1147 "address %d; must be 0x48-0x4f\n",
1148 force_subclients[i]);
1153 w83793_write_value(client, W83793_REG_I2C_SUBADDR,
1154 (force_subclients[2] & 0x07) |
1155 ((force_subclients[3] & 0x07) << 4));
1158 tmp = w83793_read_value(client, W83793_REG_I2C_SUBADDR);
1159 if (!(tmp & 0x08)) {
1161 w83793_create_subclient(adapter, client, tmp & 0x7,
1166 if (!(tmp & 0x80)) {
1167 if ((data->lm75[0] != NULL)
1168 && ((tmp & 0x7) == ((tmp >> 4) & 0x7))) {
1169 dev_err(&client->dev,
1170 "duplicate addresses 0x%x, "
1171 "use force_subclients\n", data->lm75[0]->addr);
1175 err = w83793_create_subclient(adapter, client,
1176 (tmp >> 4) & 0x7, &data->lm75[1]);
1183 /* Undo inits in case of errors */
1186 if (data->lm75[0] != NULL) {
1187 i2c_detach_client(data->lm75[0]);
1188 kfree(data->lm75[0]);
1194 static int w83793_detect(struct i2c_adapter *adapter, int address, int kind)
1198 struct i2c_client *client;
1200 struct w83793_data *data;
1201 int files_fan = ARRAY_SIZE(w83793_left_fan) / 7;
1202 int files_pwm = ARRAY_SIZE(w83793_left_pwm) / 5;
1203 int files_temp = ARRAY_SIZE(w83793_temp) / 6;
1206 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA)) {
1210 /* OK. For now, we presume we have a valid client. We now create the
1211 client structure, even though we cannot fill it completely yet.
1212 But it allows us to access w83793_{read,write}_value. */
1214 if (!(data = kzalloc(sizeof(struct w83793_data), GFP_KERNEL))) {
1219 client = &data->client;
1221 i2c_set_clientdata(client, data);
1222 client->addr = address;
1223 client->adapter = adapter;
1224 client->driver = &w83793_driver;
1226 data->bank = i2c_smbus_read_byte_data(client, W83793_REG_BANKSEL);
1228 /* Now, we do the remaining detection. */
1230 tmp = data->bank & 0x80 ? 0x5c : 0xa3;
1231 /* Check Winbond vendor ID */
1232 if (tmp != i2c_smbus_read_byte_data(client,
1233 W83793_REG_VENDORID)) {
1234 pr_debug("w83793: Detection failed at check "
1240 /* If Winbond chip, address of chip and W83793_REG_I2C_ADDR
1242 if ((data->bank & 0x07) == 0
1243 && i2c_smbus_read_byte_data(client, W83793_REG_I2C_ADDR) !=
1245 pr_debug("w83793: Detection failed at check "
1253 /* We have either had a force parameter, or we have already detected the
1254 Winbond. Determine the chip type now */
1257 if (0x7b == w83793_read_value(client, W83793_REG_CHIPID)) {
1261 dev_warn(&adapter->dev, "w83793: Ignoring "
1262 "'force' parameter for unknown chip "
1263 "at address 0x%02x\n", address);
1269 /* Fill in the remaining client fields and put into the global list */
1270 strlcpy(client->name, "w83793", I2C_NAME_SIZE);
1272 mutex_init(&data->update_lock);
1274 /* Tell the I2C layer a new client has arrived */
1275 if ((err = i2c_attach_client(client)))
1278 if ((err = w83793_detect_subclients(adapter, address, kind, client)))
1281 /* Initialize the chip */
1282 w83793_init_client(client);
1284 data->vrm = vid_which_vrm();
1286 Only fan 1-5 has their own input pins,
1287 Pwm 1-3 has their own pins
1289 data->has_fan = 0x1f;
1290 data->has_pwm = 0x07;
1291 tmp = w83793_read_value(client, W83793_REG_MFC);
1292 val = w83793_read_value(client, W83793_REG_FANIN_CTRL);
1294 /* check the function of pins 49-56 */
1295 if (!(tmp & 0x80)) {
1296 data->has_pwm |= 0x18; /* pwm 4,5 */
1297 if (val & 0x01) { /* fan 6 */
1298 data->has_fan |= 0x20;
1299 data->has_pwm |= 0x20;
1301 if (val & 0x02) { /* fan 7 */
1302 data->has_fan |= 0x40;
1303 data->has_pwm |= 0x40;
1305 if (!(tmp & 0x40) && (val & 0x04)) { /* fan 8 */
1306 data->has_fan |= 0x80;
1307 data->has_pwm |= 0x80;
1311 if (0x08 == (tmp & 0x0c)) {
1312 if (val & 0x08) /* fan 9 */
1313 data->has_fan |= 0x100;
1314 if (val & 0x10) /* fan 10 */
1315 data->has_fan |= 0x200;
1318 if (0x20 == (tmp & 0x30)) {
1319 if (val & 0x20) /* fan 11 */
1320 data->has_fan |= 0x400;
1321 if (val & 0x40) /* fan 12 */
1322 data->has_fan |= 0x800;
1325 if ((tmp & 0x01) && (val & 0x04)) { /* fan 8, second location */
1326 data->has_fan |= 0x80;
1327 data->has_pwm |= 0x80;
1330 /* check the temp1-6 mode, ignore former AMDSI selected inputs */
1331 tmp = w83793_read_value(client,W83793_REG_TEMP_MODE[0]);
1333 data->has_temp |= 0x01;
1335 data->has_temp |= 0x02;
1337 data->has_temp |= 0x04;
1339 data->has_temp |= 0x08;
1341 tmp = w83793_read_value(client,W83793_REG_TEMP_MODE[1]);
1343 data->has_temp |= 0x10;
1345 data->has_temp |= 0x20;
1347 /* Register sysfs hooks */
1348 for (i = 0; i < ARRAY_SIZE(w83793_sensor_attr_2); i++) {
1349 err = device_create_file(dev,
1350 &w83793_sensor_attr_2[i].dev_attr);
1355 for (i = 0; i < ARRAY_SIZE(sda_single_files); i++) {
1356 err = device_create_file(dev, &sda_single_files[i].dev_attr);
1362 for (i = 0; i < 6; i++) {
1364 if (!(data->has_temp & (1 << i)))
1366 for (j = 0; j < files_temp; j++) {
1367 err = device_create_file(dev,
1368 &w83793_temp[(i) * files_temp
1375 for (i = 5; i < 12; i++) {
1377 if (!(data->has_fan & (1 << i)))
1379 for (j = 0; j < files_fan; j++) {
1380 err = device_create_file(dev,
1381 &w83793_left_fan[(i - 5) * files_fan
1388 for (i = 3; i < 8; i++) {
1390 if (!(data->has_pwm & (1 << i)))
1392 for (j = 0; j < files_pwm; j++) {
1393 err = device_create_file(dev,
1394 &w83793_left_pwm[(i - 3) * files_pwm
1401 data->class_dev = hwmon_device_register(dev);
1402 if (IS_ERR(data->class_dev)) {
1403 err = PTR_ERR(data->class_dev);
1409 /* Unregister sysfs hooks */
1412 for (i = 0; i < ARRAY_SIZE(w83793_sensor_attr_2); i++)
1413 device_remove_file(dev, &w83793_sensor_attr_2[i].dev_attr);
1415 for (i = 0; i < ARRAY_SIZE(sda_single_files); i++)
1416 device_remove_file(dev, &sda_single_files[i].dev_attr);
1418 for (i = 0; i < ARRAY_SIZE(w83793_left_fan); i++)
1419 device_remove_file(dev, &w83793_left_fan[i].dev_attr);
1421 for (i = 0; i < ARRAY_SIZE(w83793_left_pwm); i++)
1422 device_remove_file(dev, &w83793_left_pwm[i].dev_attr);
1424 for (i = 0; i < ARRAY_SIZE(w83793_temp); i++)
1425 device_remove_file(dev, &w83793_temp[i].dev_attr);
1427 if (data->lm75[0] != NULL) {
1428 i2c_detach_client(data->lm75[0]);
1429 kfree(data->lm75[0]);
1431 if (data->lm75[1] != NULL) {
1432 i2c_detach_client(data->lm75[1]);
1433 kfree(data->lm75[1]);
1436 i2c_detach_client(client);
1443 static void w83793_update_nonvolatile(struct device *dev)
1445 struct i2c_client *client = to_i2c_client(dev);
1446 struct w83793_data *data = i2c_get_clientdata(client);
1449 They are somewhat "stable" registers, and to update them everytime
1450 takes so much time, it's just not worthy. Update them in a long
1451 interval to avoid exception.
1453 if (!(time_after(jiffies, data->last_nonvolatile + HZ * 300)
1456 /* update voltage limits */
1457 for (i = 1; i < 3; i++) {
1458 for (j = 0; j < ARRAY_SIZE(data->in); j++) {
1460 w83793_read_value(client, W83793_REG_IN[j][i]);
1462 data->in_low_bits[i] =
1463 w83793_read_value(client, W83793_REG_IN_LOW_BITS[i]);
1466 for (i = 0; i < ARRAY_SIZE(data->fan_min); i++) {
1467 /* Update the Fan measured value and limits */
1468 if (!(data->has_fan & (1 << i))) {
1472 w83793_read_value(client, W83793_REG_FAN_MIN(i)) << 8;
1474 w83793_read_value(client, W83793_REG_FAN_MIN(i) + 1);
1477 for (i = 0; i < ARRAY_SIZE(data->temp_fan_map); i++) {
1478 if (!(data->has_temp & (1 << i)))
1480 data->temp_fan_map[i] =
1481 w83793_read_value(client, W83793_REG_TEMP_FAN_MAP(i));
1482 for (j = 1; j < 5; j++) {
1484 w83793_read_value(client, W83793_REG_TEMP[i][j]);
1486 data->temp_cruise[i] =
1487 w83793_read_value(client, W83793_REG_TEMP_CRUISE(i));
1488 for (j = 0; j < 7; j++) {
1489 data->sf2_pwm[i][j] =
1490 w83793_read_value(client, W83793_REG_SF2_PWM(i, j));
1491 data->sf2_temp[i][j] =
1492 w83793_read_value(client,
1493 W83793_REG_SF2_TEMP(i, j));
1497 for (i = 0; i < ARRAY_SIZE(data->temp_mode); i++)
1498 data->temp_mode[i] =
1499 w83793_read_value(client, W83793_REG_TEMP_MODE[i]);
1501 for (i = 0; i < ARRAY_SIZE(data->tolerance); i++) {
1502 data->tolerance[i] =
1503 w83793_read_value(client, W83793_REG_TEMP_TOL(i));
1506 for (i = 0; i < ARRAY_SIZE(data->pwm); i++) {
1507 if (!(data->has_pwm & (1 << i)))
1509 data->pwm[i][PWM_NONSTOP] =
1510 w83793_read_value(client, W83793_REG_PWM(i, PWM_NONSTOP));
1511 data->pwm[i][PWM_START] =
1512 w83793_read_value(client, W83793_REG_PWM(i, PWM_START));
1513 data->pwm_stop_time[i] =
1514 w83793_read_value(client, W83793_REG_PWM_STOP_TIME(i));
1517 data->pwm_default = w83793_read_value(client, W83793_REG_PWM_DEFAULT);
1518 data->pwm_enable = w83793_read_value(client, W83793_REG_PWM_ENABLE);
1519 data->pwm_uptime = w83793_read_value(client, W83793_REG_PWM_UPTIME);
1520 data->pwm_downtime = w83793_read_value(client, W83793_REG_PWM_DOWNTIME);
1521 data->temp_critical =
1522 w83793_read_value(client, W83793_REG_TEMP_CRITICAL);
1523 data->beep_enable = w83793_read_value(client, W83793_REG_OVT_BEEP);
1525 for (i = 0; i < ARRAY_SIZE(data->beeps); i++) {
1526 data->beeps[i] = w83793_read_value(client, W83793_REG_BEEP(i));
1529 data->last_nonvolatile = jiffies;
1532 static struct w83793_data *w83793_update_device(struct device *dev)
1534 struct i2c_client *client = to_i2c_client(dev);
1535 struct w83793_data *data = i2c_get_clientdata(client);
1538 mutex_lock(&data->update_lock);
1540 if (!(time_after(jiffies, data->last_updated + HZ * 2)
1544 /* Update the voltages measured value and limits */
1545 for (i = 0; i < ARRAY_SIZE(data->in); i++)
1546 data->in[i][IN_READ] =
1547 w83793_read_value(client, W83793_REG_IN[i][IN_READ]);
1549 data->in_low_bits[IN_READ] =
1550 w83793_read_value(client, W83793_REG_IN_LOW_BITS[IN_READ]);
1552 for (i = 0; i < ARRAY_SIZE(data->fan); i++) {
1553 if (!(data->has_fan & (1 << i))) {
1557 w83793_read_value(client, W83793_REG_FAN(i)) << 8;
1559 w83793_read_value(client, W83793_REG_FAN(i) + 1);
1562 for (i = 0; i < ARRAY_SIZE(data->temp); i++) {
1563 if (!(data->has_temp & (1 << i)))
1565 data->temp[i][TEMP_READ] =
1566 w83793_read_value(client, W83793_REG_TEMP[i][TEMP_READ]);
1569 data->temp_low_bits =
1570 w83793_read_value(client, W83793_REG_TEMP_LOW_BITS);
1572 for (i = 0; i < ARRAY_SIZE(data->pwm); i++) {
1573 if (data->has_pwm & (1 << i))
1574 data->pwm[i][PWM_DUTY] =
1575 w83793_read_value(client,
1576 W83793_REG_PWM(i, PWM_DUTY));
1579 for (i = 0; i < ARRAY_SIZE(data->alarms); i++)
1581 w83793_read_value(client, W83793_REG_ALARM(i));
1582 data->vid[0] = w83793_read_value(client, W83793_REG_VID_INA);
1583 data->vid[1] = w83793_read_value(client, W83793_REG_VID_INB);
1584 w83793_update_nonvolatile(dev);
1585 data->last_updated = jiffies;
1589 mutex_unlock(&data->update_lock);
1593 /* Ignore the possibility that somebody change bank outside the driver
1594 Must be called with data->update_lock held, except during initialization */
1595 static u8 w83793_read_value(struct i2c_client *client, u16 reg)
1597 struct w83793_data *data = i2c_get_clientdata(client);
1599 u8 new_bank = reg >> 8;
1601 new_bank |= data->bank & 0xfc;
1602 if (data->bank != new_bank) {
1603 if (i2c_smbus_write_byte_data
1604 (client, W83793_REG_BANKSEL, new_bank) >= 0)
1605 data->bank = new_bank;
1607 dev_err(&client->dev,
1608 "set bank to %d failed, fall back "
1609 "to bank %d, read reg 0x%x error\n",
1610 new_bank, data->bank, reg);
1611 res = 0x0; /* read 0x0 from the chip */
1615 res = i2c_smbus_read_byte_data(client, reg & 0xff);
1620 /* Must be called with data->update_lock held, except during initialization */
1621 static int w83793_write_value(struct i2c_client *client, u16 reg, u8 value)
1623 struct w83793_data *data = i2c_get_clientdata(client);
1625 u8 new_bank = reg >> 8;
1627 new_bank |= data->bank & 0xfc;
1628 if (data->bank != new_bank) {
1629 if ((res = i2c_smbus_write_byte_data
1630 (client, W83793_REG_BANKSEL, new_bank)) >= 0)
1631 data->bank = new_bank;
1633 dev_err(&client->dev,
1634 "set bank to %d failed, fall back "
1635 "to bank %d, write reg 0x%x error\n",
1636 new_bank, data->bank, reg);
1641 res = i2c_smbus_write_byte_data(client, reg & 0xff, value);
1646 static int __init sensors_w83793_init(void)
1648 return i2c_add_driver(&w83793_driver);
1651 static void __exit sensors_w83793_exit(void)
1653 i2c_del_driver(&w83793_driver);
1656 MODULE_AUTHOR("Yuan Mu");
1657 MODULE_DESCRIPTION("w83793 driver");
1658 MODULE_LICENSE("GPL");
1660 module_init(sensors_w83793_init);
1661 module_exit(sensors_w83793_exit);