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 const unsigned short normal_i2c
[] = { 0x2c, 0x2d, 0x2e, 0x2f,
43 /* Insmod parameters */
44 I2C_CLIENT_INSMOD_1(w83793
);
45 I2C_CLIENT_MODULE_PARM(force_subclients
, "List of subclient addresses: "
46 "{bus, clientaddr, subclientaddr1, subclientaddr2}");
49 module_param(reset
, bool, 0);
50 MODULE_PARM_DESC(reset
, "Set to 1 to reset chip, not recommended");
53 Address 0x00, 0x0d, 0x0e, 0x0f in all three banks are reserved
54 as ID, Bank Select registers
56 #define W83793_REG_BANKSEL 0x00
57 #define W83793_REG_VENDORID 0x0d
58 #define W83793_REG_CHIPID 0x0e
59 #define W83793_REG_DEVICEID 0x0f
61 #define W83793_REG_CONFIG 0x40
62 #define W83793_REG_MFC 0x58
63 #define W83793_REG_FANIN_CTRL 0x5c
64 #define W83793_REG_FANIN_SEL 0x5d
65 #define W83793_REG_I2C_ADDR 0x0b
66 #define W83793_REG_I2C_SUBADDR 0x0c
67 #define W83793_REG_VID_INA 0x05
68 #define W83793_REG_VID_INB 0x06
69 #define W83793_REG_VID_LATCHA 0x07
70 #define W83793_REG_VID_LATCHB 0x08
71 #define W83793_REG_VID_CTRL 0x59
73 static u16 W83793_REG_TEMP_MODE
[2] = { 0x5e, 0x5f };
77 #define TEMP_CRIT_HYST 2
79 #define TEMP_WARN_HYST 4
80 /* only crit and crit_hyst affect real-time alarm status
81 current crit crit_hyst warn warn_hyst */
82 static u16 W83793_REG_TEMP
[][5] = {
83 {0x1c, 0x78, 0x79, 0x7a, 0x7b},
84 {0x1d, 0x7c, 0x7d, 0x7e, 0x7f},
85 {0x1e, 0x80, 0x81, 0x82, 0x83},
86 {0x1f, 0x84, 0x85, 0x86, 0x87},
87 {0x20, 0x88, 0x89, 0x8a, 0x8b},
88 {0x21, 0x8c, 0x8d, 0x8e, 0x8f},
91 #define W83793_REG_TEMP_LOW_BITS 0x22
93 #define W83793_REG_BEEP(index) (0x53 + (index))
94 #define W83793_REG_ALARM(index) (0x4b + (index))
96 #define W83793_REG_CLR_CHASSIS 0x4a /* SMI MASK4 */
97 #define W83793_REG_IRQ_CTRL 0x50
98 #define W83793_REG_OVT_CTRL 0x51
99 #define W83793_REG_OVT_BEEP 0x52
104 static const u16 W83793_REG_IN
[][3] = {
105 /* Current, High, Low */
106 {0x10, 0x60, 0x61}, /* Vcore A */
107 {0x11, 0x62, 0x63}, /* Vcore B */
108 {0x12, 0x64, 0x65}, /* Vtt */
109 {0x14, 0x6a, 0x6b}, /* VSEN1 */
110 {0x15, 0x6c, 0x6d}, /* VSEN2 */
111 {0x16, 0x6e, 0x6f}, /* +3VSEN */
112 {0x17, 0x70, 0x71}, /* +12VSEN */
113 {0x18, 0x72, 0x73}, /* 5VDD */
114 {0x19, 0x74, 0x75}, /* 5VSB */
115 {0x1a, 0x76, 0x77}, /* VBAT */
118 /* Low Bits of Vcore A/B Vtt Read/High/Low */
119 static const u16 W83793_REG_IN_LOW_BITS
[] = { 0x1b, 0x68, 0x69 };
120 static u8 scale_in
[] = { 2, 2, 2, 16, 16, 16, 8, 24, 24, 16 };
121 static u8 scale_in_add
[] = { 0, 0, 0, 0, 0, 0, 0, 150, 150, 0 };
123 #define W83793_REG_FAN(index) (0x23 + 2 * (index)) /* High byte */
124 #define W83793_REG_FAN_MIN(index) (0x90 + 2 * (index)) /* High byte */
126 #define W83793_REG_PWM_DEFAULT 0xb2
127 #define W83793_REG_PWM_ENABLE 0x207
128 #define W83793_REG_PWM_UPTIME 0xc3 /* Unit in 0.1 second */
129 #define W83793_REG_PWM_DOWNTIME 0xc4 /* Unit in 0.1 second */
130 #define W83793_REG_TEMP_CRITICAL 0xc5
134 #define PWM_NONSTOP 2
135 #define PWM_STOP_TIME 3
136 #define W83793_REG_PWM(index, nr) (((nr) == 0 ? 0xb3 : \
137 (nr) == 1 ? 0x220 : 0x218) + (index))
139 /* bit field, fan1 is bit0, fan2 is bit1 ... */
140 #define W83793_REG_TEMP_FAN_MAP(index) (0x201 + (index))
141 #define W83793_REG_TEMP_TOL(index) (0x208 + (index))
142 #define W83793_REG_TEMP_CRUISE(index) (0x210 + (index))
143 #define W83793_REG_PWM_STOP_TIME(index) (0x228 + (index))
144 #define W83793_REG_SF2_TEMP(index, nr) (0x230 + ((index) << 4) + (nr))
145 #define W83793_REG_SF2_PWM(index, nr) (0x238 + ((index) << 4) + (nr))
147 static inline unsigned long FAN_FROM_REG(u16 val
)
149 if ((val
>= 0xfff) || (val
== 0))
151 return (1350000UL / val
);
154 static inline u16
FAN_TO_REG(long rpm
)
158 return SENSORS_LIMIT((1350000 + (rpm
>> 1)) / rpm
, 1, 0xffe);
161 static inline unsigned long TIME_FROM_REG(u8 reg
)
166 static inline u8
TIME_TO_REG(unsigned long val
)
168 return SENSORS_LIMIT((val
+ 50) / 100, 0, 0xff);
171 static inline long TEMP_FROM_REG(s8 reg
)
176 static inline s8
TEMP_TO_REG(long val
, s8 min
, s8 max
)
178 return SENSORS_LIMIT((val
+ (val
< 0 ? -500 : 500)) / 1000, min
, max
);
182 struct i2c_client client
;
183 struct i2c_client
*lm75
[2];
184 struct device
*hwmon_dev
;
185 struct mutex update_lock
;
186 char valid
; /* !=0 if following fields are valid */
187 unsigned long last_updated
; /* In jiffies */
188 unsigned long last_nonvolatile
; /* In jiffies, last time we update the
189 nonvolatile registers */
194 u8 in
[10][3]; /* Register value, read/high/low */
195 u8 in_low_bits
[3]; /* Additional resolution for VCore A/B Vtt */
197 u16 has_fan
; /* Only fan1- fan5 has own pins */
198 u16 fan
[12]; /* Register value combine */
199 u16 fan_min
[12]; /* Register value combine */
201 s8 temp
[6][5]; /* current, crit, crit_hyst,warn, warn_hyst */
202 u8 temp_low_bits
; /* Additional resolution TD1-TD4 */
203 u8 temp_mode
[2]; /* byte 0: Temp D1-D4 mode each has 2 bits
204 byte 1: Temp R1,R2 mode, each has 1 bit */
205 u8 temp_critical
; /* If reached all fan will be at full speed */
206 u8 temp_fan_map
[6]; /* Temp controls which pwm fan, bit field */
211 u8 pwm_enable
; /* Register value, each Temp has 1 bit */
212 u8 pwm_uptime
; /* Register value */
213 u8 pwm_downtime
; /* Register value */
214 u8 pwm_default
; /* All fan default pwm, next poweron valid */
215 u8 pwm
[8][3]; /* Register value */
219 u8 alarms
[5]; /* realtime status registers */
222 u8 tolerance
[3]; /* Temp tolerance(Smart Fan I/II) */
223 u8 sf2_pwm
[6][7]; /* Smart FanII: Fan duty cycle */
224 u8 sf2_temp
[6][7]; /* Smart FanII: Temp level point */
227 static u8
w83793_read_value(struct i2c_client
*client
, u16 reg
);
228 static int w83793_write_value(struct i2c_client
*client
, u16 reg
, u8 value
);
229 static int w83793_attach_adapter(struct i2c_adapter
*adapter
);
230 static int w83793_detect(struct i2c_adapter
*adapter
, int address
, int kind
);
231 static int w83793_detach_client(struct i2c_client
*client
);
232 static void w83793_init_client(struct i2c_client
*client
);
233 static void w83793_update_nonvolatile(struct device
*dev
);
234 static struct w83793_data
*w83793_update_device(struct device
*dev
);
236 static struct i2c_driver w83793_driver
= {
240 .attach_adapter
= w83793_attach_adapter
,
241 .detach_client
= w83793_detach_client
,
245 show_vrm(struct device
*dev
, struct device_attribute
*attr
, char *buf
)
247 struct w83793_data
*data
= dev_get_drvdata(dev
);
248 return sprintf(buf
, "%d\n", data
->vrm
);
252 show_vid(struct device
*dev
, struct device_attribute
*attr
, char *buf
)
254 struct w83793_data
*data
= w83793_update_device(dev
);
255 struct sensor_device_attribute_2
*sensor_attr
=
256 to_sensor_dev_attr_2(attr
);
257 int index
= sensor_attr
->index
;
259 return sprintf(buf
, "%d\n", vid_from_reg(data
->vid
[index
], data
->vrm
));
263 store_vrm(struct device
*dev
, struct device_attribute
*attr
,
264 const char *buf
, size_t count
)
266 struct w83793_data
*data
= dev_get_drvdata(dev
);
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
);
409 show_pwm(struct device
*dev
, struct device_attribute
*attr
, char *buf
)
411 struct sensor_device_attribute_2
*sensor_attr
=
412 to_sensor_dev_attr_2(attr
);
413 struct w83793_data
*data
= w83793_update_device(dev
);
415 int nr
= sensor_attr
->nr
;
416 int index
= sensor_attr
->index
;
418 if (PWM_STOP_TIME
== nr
)
419 val
= TIME_FROM_REG(data
->pwm_stop_time
[index
]);
421 val
= (data
->pwm
[index
][nr
] & 0x3f) << 2;
423 return sprintf(buf
, "%d\n", val
);
427 store_pwm(struct device
*dev
, struct device_attribute
*attr
,
428 const char *buf
, size_t count
)
430 struct i2c_client
*client
= to_i2c_client(dev
);
431 struct w83793_data
*data
= i2c_get_clientdata(client
);
432 struct sensor_device_attribute_2
*sensor_attr
=
433 to_sensor_dev_attr_2(attr
);
434 int nr
= sensor_attr
->nr
;
435 int index
= sensor_attr
->index
;
438 mutex_lock(&data
->update_lock
);
439 if (PWM_STOP_TIME
== nr
) {
440 val
= TIME_TO_REG(simple_strtoul(buf
, NULL
, 10));
441 data
->pwm_stop_time
[index
] = val
;
442 w83793_write_value(client
, W83793_REG_PWM_STOP_TIME(index
),
445 val
= SENSORS_LIMIT(simple_strtoul(buf
, NULL
, 10), 0, 0xff)
447 data
->pwm
[index
][nr
] =
448 w83793_read_value(client
, W83793_REG_PWM(index
, nr
)) & 0xc0;
449 data
->pwm
[index
][nr
] |= val
;
450 w83793_write_value(client
, W83793_REG_PWM(index
, nr
),
451 data
->pwm
[index
][nr
]);
454 mutex_unlock(&data
->update_lock
);
459 show_temp(struct device
*dev
, struct device_attribute
*attr
, char *buf
)
461 struct sensor_device_attribute_2
*sensor_attr
=
462 to_sensor_dev_attr_2(attr
);
463 int nr
= sensor_attr
->nr
;
464 int index
= sensor_attr
->index
;
465 struct w83793_data
*data
= w83793_update_device(dev
);
466 long temp
= TEMP_FROM_REG(data
->temp
[index
][nr
]);
468 if (TEMP_READ
== nr
&& index
< 4) { /* Only TD1-TD4 have low bits */
469 int low
= ((data
->temp_low_bits
>> (index
* 2)) & 0x03) * 250;
470 temp
+= temp
> 0 ? low
: -low
;
472 return sprintf(buf
, "%ld\n", temp
);
476 store_temp(struct device
*dev
, struct device_attribute
*attr
,
477 const char *buf
, size_t count
)
479 struct sensor_device_attribute_2
*sensor_attr
=
480 to_sensor_dev_attr_2(attr
);
481 int nr
= sensor_attr
->nr
;
482 int index
= sensor_attr
->index
;
483 struct i2c_client
*client
= to_i2c_client(dev
);
484 struct w83793_data
*data
= i2c_get_clientdata(client
);
485 long tmp
= simple_strtol(buf
, NULL
, 10);
487 mutex_lock(&data
->update_lock
);
488 data
->temp
[index
][nr
] = TEMP_TO_REG(tmp
, -128, 127);
489 w83793_write_value(client
, W83793_REG_TEMP
[index
][nr
],
490 data
->temp
[index
][nr
]);
491 mutex_unlock(&data
->update_lock
);
497 each has 4 mode:(2 bits)
499 1: Use internal temp sensor(default)
501 3: Use sensor in Intel CPU and get result by PECI
504 each has 2 mode:(1 bit)
505 0: Disable temp sensor monitor
506 1: To enable temp sensors monitor
509 /* 0 disable, 6 PECI */
510 static u8 TO_TEMP_MODE
[] = { 0, 0, 0, 6 };
513 show_temp_mode(struct device
*dev
, struct device_attribute
*attr
, char *buf
)
515 struct w83793_data
*data
= w83793_update_device(dev
);
516 struct sensor_device_attribute_2
*sensor_attr
=
517 to_sensor_dev_attr_2(attr
);
518 int index
= sensor_attr
->index
;
519 u8 mask
= (index
< 4) ? 0x03 : 0x01;
520 u8 shift
= (index
< 4) ? (2 * index
) : (index
- 4);
522 index
= (index
< 4) ? 0 : 1;
524 tmp
= (data
->temp_mode
[index
] >> shift
) & mask
;
526 /* for the internal sensor, found out if diode or thermistor */
528 tmp
= index
== 0 ? 3 : 4;
530 tmp
= TO_TEMP_MODE
[tmp
];
533 return sprintf(buf
, "%d\n", tmp
);
537 store_temp_mode(struct device
*dev
, struct device_attribute
*attr
,
538 const char *buf
, size_t count
)
540 struct i2c_client
*client
= to_i2c_client(dev
);
541 struct w83793_data
*data
= i2c_get_clientdata(client
);
542 struct sensor_device_attribute_2
*sensor_attr
=
543 to_sensor_dev_attr_2(attr
);
544 int index
= sensor_attr
->index
;
545 u8 mask
= (index
< 4) ? 0x03 : 0x01;
546 u8 shift
= (index
< 4) ? (2 * index
) : (index
- 4);
547 u8 val
= simple_strtoul(buf
, NULL
, 10);
549 /* transform the sysfs interface values into table above */
550 if ((val
== 6) && (index
< 4)) {
552 } else if ((val
== 3 && index
< 4)
553 || (val
== 4 && index
>= 4)) {
554 /* transform diode or thermistor into internal enable */
560 index
= (index
< 4) ? 0 : 1;
561 mutex_lock(&data
->update_lock
);
562 data
->temp_mode
[index
] =
563 w83793_read_value(client
, W83793_REG_TEMP_MODE
[index
]);
564 data
->temp_mode
[index
] &= ~(mask
<< shift
);
565 data
->temp_mode
[index
] |= val
<< shift
;
566 w83793_write_value(client
, W83793_REG_TEMP_MODE
[index
],
567 data
->temp_mode
[index
]);
568 mutex_unlock(&data
->update_lock
);
573 #define SETUP_PWM_DEFAULT 0
574 #define SETUP_PWM_UPTIME 1 /* Unit in 0.1s */
575 #define SETUP_PWM_DOWNTIME 2 /* Unit in 0.1s */
576 #define SETUP_TEMP_CRITICAL 3
578 show_sf_setup(struct device
*dev
, struct device_attribute
*attr
, char *buf
)
580 struct sensor_device_attribute_2
*sensor_attr
=
581 to_sensor_dev_attr_2(attr
);
582 int nr
= sensor_attr
->nr
;
583 struct w83793_data
*data
= w83793_update_device(dev
);
586 if (SETUP_PWM_DEFAULT
== nr
) {
587 val
= (data
->pwm_default
& 0x3f) << 2;
588 } else if (SETUP_PWM_UPTIME
== nr
) {
589 val
= TIME_FROM_REG(data
->pwm_uptime
);
590 } else if (SETUP_PWM_DOWNTIME
== nr
) {
591 val
= TIME_FROM_REG(data
->pwm_downtime
);
592 } else if (SETUP_TEMP_CRITICAL
== nr
) {
593 val
= TEMP_FROM_REG(data
->temp_critical
& 0x7f);
596 return sprintf(buf
, "%d\n", val
);
600 store_sf_setup(struct device
*dev
, struct device_attribute
*attr
,
601 const char *buf
, size_t count
)
603 struct sensor_device_attribute_2
*sensor_attr
=
604 to_sensor_dev_attr_2(attr
);
605 int nr
= sensor_attr
->nr
;
606 struct i2c_client
*client
= to_i2c_client(dev
);
607 struct w83793_data
*data
= i2c_get_clientdata(client
);
609 mutex_lock(&data
->update_lock
);
610 if (SETUP_PWM_DEFAULT
== nr
) {
612 w83793_read_value(client
, W83793_REG_PWM_DEFAULT
) & 0xc0;
613 data
->pwm_default
|= SENSORS_LIMIT(simple_strtoul(buf
, NULL
,
616 w83793_write_value(client
, W83793_REG_PWM_DEFAULT
,
618 } else if (SETUP_PWM_UPTIME
== nr
) {
619 data
->pwm_uptime
= TIME_TO_REG(simple_strtoul(buf
, NULL
, 10));
620 data
->pwm_uptime
+= data
->pwm_uptime
== 0 ? 1 : 0;
621 w83793_write_value(client
, W83793_REG_PWM_UPTIME
,
623 } else if (SETUP_PWM_DOWNTIME
== nr
) {
624 data
->pwm_downtime
= TIME_TO_REG(simple_strtoul(buf
, NULL
, 10));
625 data
->pwm_downtime
+= data
->pwm_downtime
== 0 ? 1 : 0;
626 w83793_write_value(client
, W83793_REG_PWM_DOWNTIME
,
628 } else { /* SETUP_TEMP_CRITICAL */
629 data
->temp_critical
=
630 w83793_read_value(client
, W83793_REG_TEMP_CRITICAL
) & 0x80;
631 data
->temp_critical
|= TEMP_TO_REG(simple_strtol(buf
, NULL
, 10),
633 w83793_write_value(client
, W83793_REG_TEMP_CRITICAL
,
634 data
->temp_critical
);
637 mutex_unlock(&data
->update_lock
);
642 Temp SmartFan control
644 Temp channel control which pwm fan, bitfield, bit 0 indicate pwm1...
645 It's possible two or more temp channels control the same fan, w83793
646 always prefers to pick the most critical request and applies it to
648 It's possible one fan is not in any mapping of 6 temp channels, this
649 means the fan is manual mode
652 Each temp channel has its own SmartFan mode, and temp channel
653 control fans that are set by TEMP_FAN_MAP
655 1: Thermal Cruise Mode
658 Target temperature in thermal cruise mode, w83793 will try to turn
659 fan speed to keep the temperature of target device around this
663 If Temp higher or lower than target with this tolerance, w83793
664 will take actions to speed up or slow down the fan to keep the
665 temperature within the tolerance range.
668 #define TEMP_FAN_MAP 0
669 #define TEMP_PWM_ENABLE 1
670 #define TEMP_CRUISE 2
671 #define TEMP_TOLERANCE 3
673 show_sf_ctrl(struct device
*dev
, struct device_attribute
*attr
, char *buf
)
675 struct sensor_device_attribute_2
*sensor_attr
=
676 to_sensor_dev_attr_2(attr
);
677 int nr
= sensor_attr
->nr
;
678 int index
= sensor_attr
->index
;
679 struct w83793_data
*data
= w83793_update_device(dev
);
682 if (TEMP_FAN_MAP
== nr
) {
683 val
= data
->temp_fan_map
[index
];
684 } else if (TEMP_PWM_ENABLE
== nr
) {
685 /* +2 to transfrom into 2 and 3 to conform with sysfs intf */
686 val
= ((data
->pwm_enable
>> index
) & 0x01) + 2;
687 } else if (TEMP_CRUISE
== nr
) {
688 val
= TEMP_FROM_REG(data
->temp_cruise
[index
] & 0x7f);
689 } else { /* TEMP_TOLERANCE */
690 val
= data
->tolerance
[index
>> 1] >> ((index
& 0x01) ? 4 : 0);
691 val
= TEMP_FROM_REG(val
& 0x0f);
693 return sprintf(buf
, "%d\n", val
);
697 store_sf_ctrl(struct device
*dev
, struct device_attribute
*attr
,
698 const char *buf
, size_t count
)
700 struct sensor_device_attribute_2
*sensor_attr
=
701 to_sensor_dev_attr_2(attr
);
702 int nr
= sensor_attr
->nr
;
703 int index
= sensor_attr
->index
;
704 struct i2c_client
*client
= to_i2c_client(dev
);
705 struct w83793_data
*data
= i2c_get_clientdata(client
);
708 mutex_lock(&data
->update_lock
);
709 if (TEMP_FAN_MAP
== nr
) {
710 val
= simple_strtoul(buf
, NULL
, 10) & 0xff;
711 w83793_write_value(client
, W83793_REG_TEMP_FAN_MAP(index
), val
);
712 data
->temp_fan_map
[index
] = val
;
713 } else if (TEMP_PWM_ENABLE
== nr
) {
714 val
= simple_strtoul(buf
, NULL
, 10);
715 if (2 == val
|| 3 == val
) {
717 w83793_read_value(client
, W83793_REG_PWM_ENABLE
);
719 data
->pwm_enable
|= 1 << index
;
721 data
->pwm_enable
&= ~(1 << index
);
722 w83793_write_value(client
, W83793_REG_PWM_ENABLE
,
725 mutex_unlock(&data
->update_lock
);
728 } else if (TEMP_CRUISE
== nr
) {
729 data
->temp_cruise
[index
] =
730 w83793_read_value(client
, W83793_REG_TEMP_CRUISE(index
));
731 val
= TEMP_TO_REG(simple_strtol(buf
, NULL
, 10), 0, 0x7f);
732 data
->temp_cruise
[index
] &= 0x80;
733 data
->temp_cruise
[index
] |= val
;
735 w83793_write_value(client
, W83793_REG_TEMP_CRUISE(index
),
736 data
->temp_cruise
[index
]);
737 } else { /* TEMP_TOLERANCE */
739 u8 shift
= (index
& 0x01) ? 4 : 0;
741 w83793_read_value(client
, W83793_REG_TEMP_TOL(i
));
743 val
= TEMP_TO_REG(simple_strtol(buf
, NULL
, 10), 0, 0x0f);
744 data
->tolerance
[i
] &= ~(0x0f << shift
);
745 data
->tolerance
[i
] |= val
<< shift
;
746 w83793_write_value(client
, W83793_REG_TEMP_TOL(i
),
750 mutex_unlock(&data
->update_lock
);
755 show_sf2_pwm(struct device
*dev
, struct device_attribute
*attr
, char *buf
)
757 struct sensor_device_attribute_2
*sensor_attr
=
758 to_sensor_dev_attr_2(attr
);
759 int nr
= sensor_attr
->nr
;
760 int index
= sensor_attr
->index
;
761 struct w83793_data
*data
= w83793_update_device(dev
);
763 return sprintf(buf
, "%d\n", (data
->sf2_pwm
[index
][nr
] & 0x3f) << 2);
767 store_sf2_pwm(struct device
*dev
, struct device_attribute
*attr
,
768 const char *buf
, size_t count
)
770 struct i2c_client
*client
= to_i2c_client(dev
);
771 struct w83793_data
*data
= i2c_get_clientdata(client
);
772 struct sensor_device_attribute_2
*sensor_attr
=
773 to_sensor_dev_attr_2(attr
);
774 int nr
= sensor_attr
->nr
;
775 int index
= sensor_attr
->index
;
776 u8 val
= SENSORS_LIMIT(simple_strtoul(buf
, NULL
, 10), 0, 0xff) >> 2;
778 mutex_lock(&data
->update_lock
);
779 data
->sf2_pwm
[index
][nr
] =
780 w83793_read_value(client
, W83793_REG_SF2_PWM(index
, nr
)) & 0xc0;
781 data
->sf2_pwm
[index
][nr
] |= val
;
782 w83793_write_value(client
, W83793_REG_SF2_PWM(index
, nr
),
783 data
->sf2_pwm
[index
][nr
]);
784 mutex_unlock(&data
->update_lock
);
789 show_sf2_temp(struct device
*dev
, struct device_attribute
*attr
, char *buf
)
791 struct sensor_device_attribute_2
*sensor_attr
=
792 to_sensor_dev_attr_2(attr
);
793 int nr
= sensor_attr
->nr
;
794 int index
= sensor_attr
->index
;
795 struct w83793_data
*data
= w83793_update_device(dev
);
797 return sprintf(buf
, "%ld\n",
798 TEMP_FROM_REG(data
->sf2_temp
[index
][nr
] & 0x7f));
802 store_sf2_temp(struct device
*dev
, struct device_attribute
*attr
,
803 const char *buf
, size_t count
)
805 struct i2c_client
*client
= to_i2c_client(dev
);
806 struct w83793_data
*data
= i2c_get_clientdata(client
);
807 struct sensor_device_attribute_2
*sensor_attr
=
808 to_sensor_dev_attr_2(attr
);
809 int nr
= sensor_attr
->nr
;
810 int index
= sensor_attr
->index
;
811 u8 val
= TEMP_TO_REG(simple_strtol(buf
, NULL
, 10), 0, 0x7f);
813 mutex_lock(&data
->update_lock
);
814 data
->sf2_temp
[index
][nr
] =
815 w83793_read_value(client
, W83793_REG_SF2_TEMP(index
, nr
)) & 0x80;
816 data
->sf2_temp
[index
][nr
] |= val
;
817 w83793_write_value(client
, W83793_REG_SF2_TEMP(index
, nr
),
818 data
->sf2_temp
[index
][nr
]);
819 mutex_unlock(&data
->update_lock
);
823 /* only Vcore A/B and Vtt have additional 2 bits precision */
825 show_in(struct device
*dev
, struct device_attribute
*attr
, char *buf
)
827 struct sensor_device_attribute_2
*sensor_attr
=
828 to_sensor_dev_attr_2(attr
);
829 int nr
= sensor_attr
->nr
;
830 int index
= sensor_attr
->index
;
831 struct w83793_data
*data
= w83793_update_device(dev
);
832 u16 val
= data
->in
[index
][nr
];
836 val
+= (data
->in_low_bits
[nr
] >> (index
* 2)) & 0x3;
838 /* voltage inputs 5VDD and 5VSB needs 150mV offset */
839 val
= val
* scale_in
[index
] + scale_in_add
[index
];
840 return sprintf(buf
, "%d\n", val
);
844 store_in(struct device
*dev
, struct device_attribute
*attr
,
845 const char *buf
, size_t count
)
847 struct sensor_device_attribute_2
*sensor_attr
=
848 to_sensor_dev_attr_2(attr
);
849 int nr
= sensor_attr
->nr
;
850 int index
= sensor_attr
->index
;
851 struct i2c_client
*client
= to_i2c_client(dev
);
852 struct w83793_data
*data
= i2c_get_clientdata(client
);
856 (simple_strtoul(buf
, NULL
, 10) +
857 scale_in
[index
] / 2) / scale_in
[index
];
858 mutex_lock(&data
->update_lock
);
860 /* fix the limit values of 5VDD and 5VSB to ALARM mechanism */
861 if (1 == nr
|| 2 == nr
) {
862 val
-= scale_in_add
[index
] / scale_in
[index
];
864 val
= SENSORS_LIMIT(val
, 0, 255);
866 val
= SENSORS_LIMIT(val
, 0, 0x3FF);
867 data
->in_low_bits
[nr
] =
868 w83793_read_value(client
, W83793_REG_IN_LOW_BITS
[nr
]);
869 data
->in_low_bits
[nr
] &= ~(0x03 << (2 * index
));
870 data
->in_low_bits
[nr
] |= (val
& 0x03) << (2 * index
);
871 w83793_write_value(client
, W83793_REG_IN_LOW_BITS
[nr
],
872 data
->in_low_bits
[nr
]);
875 data
->in
[index
][nr
] = val
;
876 w83793_write_value(client
, W83793_REG_IN
[index
][nr
],
877 data
->in
[index
][nr
]);
878 mutex_unlock(&data
->update_lock
);
884 #define SENSOR_ATTR_IN(index) \
885 SENSOR_ATTR_2(in##index##_input, S_IRUGO, show_in, NULL, \
887 SENSOR_ATTR_2(in##index##_max, S_IRUGO | S_IWUSR, show_in, \
888 store_in, IN_MAX, index), \
889 SENSOR_ATTR_2(in##index##_min, S_IRUGO | S_IWUSR, show_in, \
890 store_in, IN_LOW, index), \
891 SENSOR_ATTR_2(in##index##_alarm, S_IRUGO, show_alarm_beep, \
892 NULL, ALARM_STATUS, index + ((index > 2) ? 1 : 0)), \
893 SENSOR_ATTR_2(in##index##_beep, S_IWUSR | S_IRUGO, \
894 show_alarm_beep, store_beep, BEEP_ENABLE, \
895 index + ((index > 2) ? 1 : 0))
897 #define SENSOR_ATTR_FAN(index) \
898 SENSOR_ATTR_2(fan##index##_alarm, S_IRUGO, show_alarm_beep, \
899 NULL, ALARM_STATUS, index + 17), \
900 SENSOR_ATTR_2(fan##index##_beep, S_IWUSR | S_IRUGO, \
901 show_alarm_beep, store_beep, BEEP_ENABLE, index + 17), \
902 SENSOR_ATTR_2(fan##index##_input, S_IRUGO, show_fan, \
903 NULL, FAN_INPUT, index - 1), \
904 SENSOR_ATTR_2(fan##index##_min, S_IWUSR | S_IRUGO, \
905 show_fan, store_fan_min, FAN_MIN, index - 1)
907 #define SENSOR_ATTR_PWM(index) \
908 SENSOR_ATTR_2(pwm##index, S_IWUSR | S_IRUGO, show_pwm, \
909 store_pwm, PWM_DUTY, index - 1), \
910 SENSOR_ATTR_2(pwm##index##_nonstop, S_IWUSR | S_IRUGO, \
911 show_pwm, store_pwm, PWM_NONSTOP, index - 1), \
912 SENSOR_ATTR_2(pwm##index##_start, S_IWUSR | S_IRUGO, \
913 show_pwm, store_pwm, PWM_START, index - 1), \
914 SENSOR_ATTR_2(pwm##index##_stop_time, S_IWUSR | S_IRUGO, \
915 show_pwm, store_pwm, PWM_STOP_TIME, index - 1)
917 #define SENSOR_ATTR_TEMP(index) \
918 SENSOR_ATTR_2(temp##index##_type, S_IRUGO | S_IWUSR, \
919 show_temp_mode, store_temp_mode, NOT_USED, index - 1), \
920 SENSOR_ATTR_2(temp##index##_input, S_IRUGO, show_temp, \
921 NULL, TEMP_READ, index - 1), \
922 SENSOR_ATTR_2(temp##index##_max, S_IRUGO | S_IWUSR, show_temp, \
923 store_temp, TEMP_CRIT, index - 1), \
924 SENSOR_ATTR_2(temp##index##_max_hyst, S_IRUGO | S_IWUSR, \
925 show_temp, store_temp, TEMP_CRIT_HYST, index - 1), \
926 SENSOR_ATTR_2(temp##index##_warn, S_IRUGO | S_IWUSR, show_temp, \
927 store_temp, TEMP_WARN, index - 1), \
928 SENSOR_ATTR_2(temp##index##_warn_hyst, S_IRUGO | S_IWUSR, \
929 show_temp, store_temp, TEMP_WARN_HYST, index - 1), \
930 SENSOR_ATTR_2(temp##index##_alarm, S_IRUGO, \
931 show_alarm_beep, NULL, ALARM_STATUS, index + 11), \
932 SENSOR_ATTR_2(temp##index##_beep, S_IWUSR | S_IRUGO, \
933 show_alarm_beep, store_beep, BEEP_ENABLE, index + 11), \
934 SENSOR_ATTR_2(temp##index##_auto_channels_pwm, \
935 S_IRUGO | S_IWUSR, show_sf_ctrl, store_sf_ctrl, \
936 TEMP_FAN_MAP, index - 1), \
937 SENSOR_ATTR_2(temp##index##_pwm_enable, S_IWUSR | S_IRUGO, \
938 show_sf_ctrl, store_sf_ctrl, TEMP_PWM_ENABLE, \
940 SENSOR_ATTR_2(thermal_cruise##index, S_IRUGO | S_IWUSR, \
941 show_sf_ctrl, store_sf_ctrl, TEMP_CRUISE, index - 1), \
942 SENSOR_ATTR_2(tolerance##index, S_IRUGO | S_IWUSR, show_sf_ctrl,\
943 store_sf_ctrl, TEMP_TOLERANCE, index - 1), \
944 SENSOR_ATTR_2(temp##index##_auto_point1_pwm, S_IRUGO | S_IWUSR, \
945 show_sf2_pwm, store_sf2_pwm, 0, index - 1), \
946 SENSOR_ATTR_2(temp##index##_auto_point2_pwm, S_IRUGO | S_IWUSR, \
947 show_sf2_pwm, store_sf2_pwm, 1, index - 1), \
948 SENSOR_ATTR_2(temp##index##_auto_point3_pwm, S_IRUGO | S_IWUSR, \
949 show_sf2_pwm, store_sf2_pwm, 2, index - 1), \
950 SENSOR_ATTR_2(temp##index##_auto_point4_pwm, S_IRUGO | S_IWUSR, \
951 show_sf2_pwm, store_sf2_pwm, 3, index - 1), \
952 SENSOR_ATTR_2(temp##index##_auto_point5_pwm, S_IRUGO | S_IWUSR, \
953 show_sf2_pwm, store_sf2_pwm, 4, index - 1), \
954 SENSOR_ATTR_2(temp##index##_auto_point6_pwm, S_IRUGO | S_IWUSR, \
955 show_sf2_pwm, store_sf2_pwm, 5, index - 1), \
956 SENSOR_ATTR_2(temp##index##_auto_point7_pwm, S_IRUGO | S_IWUSR, \
957 show_sf2_pwm, store_sf2_pwm, 6, index - 1), \
958 SENSOR_ATTR_2(temp##index##_auto_point1_temp, S_IRUGO | S_IWUSR,\
959 show_sf2_temp, store_sf2_temp, 0, index - 1), \
960 SENSOR_ATTR_2(temp##index##_auto_point2_temp, S_IRUGO | S_IWUSR,\
961 show_sf2_temp, store_sf2_temp, 1, index - 1), \
962 SENSOR_ATTR_2(temp##index##_auto_point3_temp, S_IRUGO | S_IWUSR,\
963 show_sf2_temp, store_sf2_temp, 2, index - 1), \
964 SENSOR_ATTR_2(temp##index##_auto_point4_temp, S_IRUGO | S_IWUSR,\
965 show_sf2_temp, store_sf2_temp, 3, index - 1), \
966 SENSOR_ATTR_2(temp##index##_auto_point5_temp, S_IRUGO | S_IWUSR,\
967 show_sf2_temp, store_sf2_temp, 4, index - 1), \
968 SENSOR_ATTR_2(temp##index##_auto_point6_temp, S_IRUGO | S_IWUSR,\
969 show_sf2_temp, store_sf2_temp, 5, index - 1), \
970 SENSOR_ATTR_2(temp##index##_auto_point7_temp, S_IRUGO | S_IWUSR,\
971 show_sf2_temp, store_sf2_temp, 6, index - 1)
973 static struct sensor_device_attribute_2 w83793_sensor_attr_2
[] = {
994 static struct sensor_device_attribute_2 w83793_temp
[] = {
1000 SENSOR_ATTR_TEMP(6),
1004 static struct sensor_device_attribute_2 w83793_left_fan
[] = {
1009 SENSOR_ATTR_FAN(10),
1010 SENSOR_ATTR_FAN(11),
1011 SENSOR_ATTR_FAN(12),
1015 static struct sensor_device_attribute_2 w83793_left_pwm
[] = {
1023 static struct sensor_device_attribute_2 w83793_vid
[] = {
1024 SENSOR_ATTR_2(cpu0_vid
, S_IRUGO
, show_vid
, NULL
, NOT_USED
, 0),
1025 SENSOR_ATTR_2(cpu1_vid
, S_IRUGO
, show_vid
, NULL
, NOT_USED
, 1),
1027 static DEVICE_ATTR(vrm
, S_IWUSR
| S_IRUGO
, show_vrm
, store_vrm
);
1029 static struct sensor_device_attribute_2 sda_single_files
[] = {
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
->hwmon_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_vid
); i
++)
1081 device_remove_file(dev
, &w83793_vid
[i
].dev_attr
);
1082 device_remove_file(dev
, &dev_attr_vrm
);
1084 for (i
= 0; i
< ARRAY_SIZE(w83793_left_fan
); i
++)
1085 device_remove_file(dev
, &w83793_left_fan
[i
].dev_attr
);
1087 for (i
= 0; i
< ARRAY_SIZE(w83793_left_pwm
); i
++)
1088 device_remove_file(dev
, &w83793_left_pwm
[i
].dev_attr
);
1090 for (i
= 0; i
< ARRAY_SIZE(w83793_temp
); i
++)
1091 device_remove_file(dev
, &w83793_temp
[i
].dev_attr
);
1094 if ((err
= i2c_detach_client(client
)))
1108 w83793_create_subclient(struct i2c_adapter
*adapter
,
1109 struct i2c_client
*client
, int addr
,
1110 struct i2c_client
**sub_cli
)
1113 struct i2c_client
*sub_client
;
1115 (*sub_cli
) = sub_client
=
1116 kzalloc(sizeof(struct i2c_client
), GFP_KERNEL
);
1117 if (!(sub_client
)) {
1120 sub_client
->addr
= 0x48 + addr
;
1121 i2c_set_clientdata(sub_client
, NULL
);
1122 sub_client
->adapter
= adapter
;
1123 sub_client
->driver
= &w83793_driver
;
1124 strlcpy(sub_client
->name
, "w83793 subclient", I2C_NAME_SIZE
);
1125 if ((err
= i2c_attach_client(sub_client
))) {
1126 dev_err(&client
->dev
, "subclient registration "
1127 "at address 0x%x failed\n", sub_client
->addr
);
1134 w83793_detect_subclients(struct i2c_adapter
*adapter
, int address
,
1135 int kind
, struct i2c_client
*client
)
1139 struct w83793_data
*data
= i2c_get_clientdata(client
);
1141 id
= i2c_adapter_id(adapter
);
1142 if (force_subclients
[0] == id
&& force_subclients
[1] == address
) {
1143 for (i
= 2; i
<= 3; i
++) {
1144 if (force_subclients
[i
] < 0x48
1145 || force_subclients
[i
] > 0x4f) {
1146 dev_err(&client
->dev
,
1147 "invalid subclient "
1148 "address %d; must be 0x48-0x4f\n",
1149 force_subclients
[i
]);
1154 w83793_write_value(client
, W83793_REG_I2C_SUBADDR
,
1155 (force_subclients
[2] & 0x07) |
1156 ((force_subclients
[3] & 0x07) << 4));
1159 tmp
= w83793_read_value(client
, W83793_REG_I2C_SUBADDR
);
1160 if (!(tmp
& 0x08)) {
1162 w83793_create_subclient(adapter
, client
, tmp
& 0x7,
1167 if (!(tmp
& 0x80)) {
1168 if ((data
->lm75
[0] != NULL
)
1169 && ((tmp
& 0x7) == ((tmp
>> 4) & 0x7))) {
1170 dev_err(&client
->dev
,
1171 "duplicate addresses 0x%x, "
1172 "use force_subclients\n", data
->lm75
[0]->addr
);
1176 err
= w83793_create_subclient(adapter
, client
,
1177 (tmp
>> 4) & 0x7, &data
->lm75
[1]);
1184 /* Undo inits in case of errors */
1187 if (data
->lm75
[0] != NULL
) {
1188 i2c_detach_client(data
->lm75
[0]);
1189 kfree(data
->lm75
[0]);
1195 static int w83793_detect(struct i2c_adapter
*adapter
, int address
, int kind
)
1199 struct i2c_client
*client
;
1201 struct w83793_data
*data
;
1202 int files_fan
= ARRAY_SIZE(w83793_left_fan
) / 7;
1203 int files_pwm
= ARRAY_SIZE(w83793_left_pwm
) / 5;
1204 int files_temp
= ARRAY_SIZE(w83793_temp
) / 6;
1207 if (!i2c_check_functionality(adapter
, I2C_FUNC_SMBUS_BYTE_DATA
)) {
1211 /* OK. For now, we presume we have a valid client. We now create the
1212 client structure, even though we cannot fill it completely yet.
1213 But it allows us to access w83793_{read,write}_value. */
1215 if (!(data
= kzalloc(sizeof(struct w83793_data
), GFP_KERNEL
))) {
1220 client
= &data
->client
;
1222 i2c_set_clientdata(client
, data
);
1223 client
->addr
= address
;
1224 client
->adapter
= adapter
;
1225 client
->driver
= &w83793_driver
;
1227 data
->bank
= i2c_smbus_read_byte_data(client
, W83793_REG_BANKSEL
);
1229 /* Now, we do the remaining detection. */
1231 tmp
= data
->bank
& 0x80 ? 0x5c : 0xa3;
1232 /* Check Winbond vendor ID */
1233 if (tmp
!= i2c_smbus_read_byte_data(client
,
1234 W83793_REG_VENDORID
)) {
1235 pr_debug("w83793: Detection failed at check "
1241 /* If Winbond chip, address of chip and W83793_REG_I2C_ADDR
1243 if ((data
->bank
& 0x07) == 0
1244 && i2c_smbus_read_byte_data(client
, W83793_REG_I2C_ADDR
) !=
1246 pr_debug("w83793: Detection failed at check "
1254 /* We have either had a force parameter, or we have already detected the
1255 Winbond. Determine the chip type now */
1258 if (0x7b == w83793_read_value(client
, W83793_REG_CHIPID
)) {
1262 dev_warn(&adapter
->dev
, "w83793: Ignoring "
1263 "'force' parameter for unknown chip "
1264 "at address 0x%02x\n", address
);
1270 /* Fill in the remaining client fields and put into the global list */
1271 strlcpy(client
->name
, "w83793", I2C_NAME_SIZE
);
1273 mutex_init(&data
->update_lock
);
1275 /* Tell the I2C layer a new client has arrived */
1276 if ((err
= i2c_attach_client(client
)))
1279 if ((err
= w83793_detect_subclients(adapter
, address
, kind
, client
)))
1282 /* Initialize the chip */
1283 w83793_init_client(client
);
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 */
1296 data
->has_vid
|= 0x2; /* has VIDB */
1298 data
->has_pwm
|= 0x18; /* pwm 4,5 */
1299 if (val
& 0x01) { /* fan 6 */
1300 data
->has_fan
|= 0x20;
1301 data
->has_pwm
|= 0x20;
1303 if (val
& 0x02) { /* fan 7 */
1304 data
->has_fan
|= 0x40;
1305 data
->has_pwm
|= 0x40;
1307 if (!(tmp
& 0x40) && (val
& 0x04)) { /* fan 8 */
1308 data
->has_fan
|= 0x80;
1309 data
->has_pwm
|= 0x80;
1313 /* check the function of pins 37-40 */
1315 data
->has_vid
|= 0x1; /* has VIDA */
1316 if (0x08 == (tmp
& 0x0c)) {
1317 if (val
& 0x08) /* fan 9 */
1318 data
->has_fan
|= 0x100;
1319 if (val
& 0x10) /* fan 10 */
1320 data
->has_fan
|= 0x200;
1322 if (0x20 == (tmp
& 0x30)) {
1323 if (val
& 0x20) /* fan 11 */
1324 data
->has_fan
|= 0x400;
1325 if (val
& 0x40) /* fan 12 */
1326 data
->has_fan
|= 0x800;
1329 if ((tmp
& 0x01) && (val
& 0x04)) { /* fan 8, second location */
1330 data
->has_fan
|= 0x80;
1331 data
->has_pwm
|= 0x80;
1334 tmp
= w83793_read_value(client
, W83793_REG_FANIN_SEL
);
1335 if ((tmp
& 0x01) && (val
& 0x08)) { /* fan 9, second location */
1336 data
->has_fan
|= 0x100;
1338 if ((tmp
& 0x02) && (val
& 0x10)) { /* fan 10, second location */
1339 data
->has_fan
|= 0x200;
1341 if ((tmp
& 0x04) && (val
& 0x20)) { /* fan 11, second location */
1342 data
->has_fan
|= 0x400;
1344 if ((tmp
& 0x08) && (val
& 0x40)) { /* fan 12, second location */
1345 data
->has_fan
|= 0x800;
1348 /* check the temp1-6 mode, ignore former AMDSI selected inputs */
1349 tmp
= w83793_read_value(client
,W83793_REG_TEMP_MODE
[0]);
1351 data
->has_temp
|= 0x01;
1353 data
->has_temp
|= 0x02;
1355 data
->has_temp
|= 0x04;
1357 data
->has_temp
|= 0x08;
1359 tmp
= w83793_read_value(client
,W83793_REG_TEMP_MODE
[1]);
1361 data
->has_temp
|= 0x10;
1363 data
->has_temp
|= 0x20;
1365 /* Register sysfs hooks */
1366 for (i
= 0; i
< ARRAY_SIZE(w83793_sensor_attr_2
); i
++) {
1367 err
= device_create_file(dev
,
1368 &w83793_sensor_attr_2
[i
].dev_attr
);
1373 for (i
= 0; i
< ARRAY_SIZE(w83793_vid
); i
++) {
1374 if (!(data
->has_vid
& (1 << i
)))
1376 err
= device_create_file(dev
, &w83793_vid
[i
].dev_attr
);
1380 if (data
->has_vid
) {
1381 data
->vrm
= vid_which_vrm();
1382 err
= device_create_file(dev
, &dev_attr_vrm
);
1387 for (i
= 0; i
< ARRAY_SIZE(sda_single_files
); i
++) {
1388 err
= device_create_file(dev
, &sda_single_files
[i
].dev_attr
);
1394 for (i
= 0; i
< 6; i
++) {
1396 if (!(data
->has_temp
& (1 << i
)))
1398 for (j
= 0; j
< files_temp
; j
++) {
1399 err
= device_create_file(dev
,
1400 &w83793_temp
[(i
) * files_temp
1407 for (i
= 5; i
< 12; i
++) {
1409 if (!(data
->has_fan
& (1 << i
)))
1411 for (j
= 0; j
< files_fan
; j
++) {
1412 err
= device_create_file(dev
,
1413 &w83793_left_fan
[(i
- 5) * files_fan
1420 for (i
= 3; i
< 8; i
++) {
1422 if (!(data
->has_pwm
& (1 << i
)))
1424 for (j
= 0; j
< files_pwm
; j
++) {
1425 err
= device_create_file(dev
,
1426 &w83793_left_pwm
[(i
- 3) * files_pwm
1433 data
->hwmon_dev
= hwmon_device_register(dev
);
1434 if (IS_ERR(data
->hwmon_dev
)) {
1435 err
= PTR_ERR(data
->hwmon_dev
);
1441 /* Unregister sysfs hooks */
1444 for (i
= 0; i
< ARRAY_SIZE(w83793_sensor_attr_2
); i
++)
1445 device_remove_file(dev
, &w83793_sensor_attr_2
[i
].dev_attr
);
1447 for (i
= 0; i
< ARRAY_SIZE(sda_single_files
); i
++)
1448 device_remove_file(dev
, &sda_single_files
[i
].dev_attr
);
1450 for (i
= 0; i
< ARRAY_SIZE(w83793_vid
); i
++)
1451 device_remove_file(dev
, &w83793_vid
[i
].dev_attr
);
1453 for (i
= 0; i
< ARRAY_SIZE(w83793_left_fan
); i
++)
1454 device_remove_file(dev
, &w83793_left_fan
[i
].dev_attr
);
1456 for (i
= 0; i
< ARRAY_SIZE(w83793_left_pwm
); i
++)
1457 device_remove_file(dev
, &w83793_left_pwm
[i
].dev_attr
);
1459 for (i
= 0; i
< ARRAY_SIZE(w83793_temp
); i
++)
1460 device_remove_file(dev
, &w83793_temp
[i
].dev_attr
);
1462 if (data
->lm75
[0] != NULL
) {
1463 i2c_detach_client(data
->lm75
[0]);
1464 kfree(data
->lm75
[0]);
1466 if (data
->lm75
[1] != NULL
) {
1467 i2c_detach_client(data
->lm75
[1]);
1468 kfree(data
->lm75
[1]);
1471 i2c_detach_client(client
);
1478 static void w83793_update_nonvolatile(struct device
*dev
)
1480 struct i2c_client
*client
= to_i2c_client(dev
);
1481 struct w83793_data
*data
= i2c_get_clientdata(client
);
1484 They are somewhat "stable" registers, and to update them everytime
1485 takes so much time, it's just not worthy. Update them in a long
1486 interval to avoid exception.
1488 if (!(time_after(jiffies
, data
->last_nonvolatile
+ HZ
* 300)
1491 /* update voltage limits */
1492 for (i
= 1; i
< 3; i
++) {
1493 for (j
= 0; j
< ARRAY_SIZE(data
->in
); j
++) {
1495 w83793_read_value(client
, W83793_REG_IN
[j
][i
]);
1497 data
->in_low_bits
[i
] =
1498 w83793_read_value(client
, W83793_REG_IN_LOW_BITS
[i
]);
1501 for (i
= 0; i
< ARRAY_SIZE(data
->fan_min
); i
++) {
1502 /* Update the Fan measured value and limits */
1503 if (!(data
->has_fan
& (1 << i
))) {
1507 w83793_read_value(client
, W83793_REG_FAN_MIN(i
)) << 8;
1509 w83793_read_value(client
, W83793_REG_FAN_MIN(i
) + 1);
1512 for (i
= 0; i
< ARRAY_SIZE(data
->temp_fan_map
); i
++) {
1513 if (!(data
->has_temp
& (1 << i
)))
1515 data
->temp_fan_map
[i
] =
1516 w83793_read_value(client
, W83793_REG_TEMP_FAN_MAP(i
));
1517 for (j
= 1; j
< 5; j
++) {
1519 w83793_read_value(client
, W83793_REG_TEMP
[i
][j
]);
1521 data
->temp_cruise
[i
] =
1522 w83793_read_value(client
, W83793_REG_TEMP_CRUISE(i
));
1523 for (j
= 0; j
< 7; j
++) {
1524 data
->sf2_pwm
[i
][j
] =
1525 w83793_read_value(client
, W83793_REG_SF2_PWM(i
, j
));
1526 data
->sf2_temp
[i
][j
] =
1527 w83793_read_value(client
,
1528 W83793_REG_SF2_TEMP(i
, j
));
1532 for (i
= 0; i
< ARRAY_SIZE(data
->temp_mode
); i
++)
1533 data
->temp_mode
[i
] =
1534 w83793_read_value(client
, W83793_REG_TEMP_MODE
[i
]);
1536 for (i
= 0; i
< ARRAY_SIZE(data
->tolerance
); i
++) {
1537 data
->tolerance
[i
] =
1538 w83793_read_value(client
, W83793_REG_TEMP_TOL(i
));
1541 for (i
= 0; i
< ARRAY_SIZE(data
->pwm
); i
++) {
1542 if (!(data
->has_pwm
& (1 << i
)))
1544 data
->pwm
[i
][PWM_NONSTOP
] =
1545 w83793_read_value(client
, W83793_REG_PWM(i
, PWM_NONSTOP
));
1546 data
->pwm
[i
][PWM_START
] =
1547 w83793_read_value(client
, W83793_REG_PWM(i
, PWM_START
));
1548 data
->pwm_stop_time
[i
] =
1549 w83793_read_value(client
, W83793_REG_PWM_STOP_TIME(i
));
1552 data
->pwm_default
= w83793_read_value(client
, W83793_REG_PWM_DEFAULT
);
1553 data
->pwm_enable
= w83793_read_value(client
, W83793_REG_PWM_ENABLE
);
1554 data
->pwm_uptime
= w83793_read_value(client
, W83793_REG_PWM_UPTIME
);
1555 data
->pwm_downtime
= w83793_read_value(client
, W83793_REG_PWM_DOWNTIME
);
1556 data
->temp_critical
=
1557 w83793_read_value(client
, W83793_REG_TEMP_CRITICAL
);
1558 data
->beep_enable
= w83793_read_value(client
, W83793_REG_OVT_BEEP
);
1560 for (i
= 0; i
< ARRAY_SIZE(data
->beeps
); i
++) {
1561 data
->beeps
[i
] = w83793_read_value(client
, W83793_REG_BEEP(i
));
1564 data
->last_nonvolatile
= jiffies
;
1567 static struct w83793_data
*w83793_update_device(struct device
*dev
)
1569 struct i2c_client
*client
= to_i2c_client(dev
);
1570 struct w83793_data
*data
= i2c_get_clientdata(client
);
1573 mutex_lock(&data
->update_lock
);
1575 if (!(time_after(jiffies
, data
->last_updated
+ HZ
* 2)
1579 /* Update the voltages measured value and limits */
1580 for (i
= 0; i
< ARRAY_SIZE(data
->in
); i
++)
1581 data
->in
[i
][IN_READ
] =
1582 w83793_read_value(client
, W83793_REG_IN
[i
][IN_READ
]);
1584 data
->in_low_bits
[IN_READ
] =
1585 w83793_read_value(client
, W83793_REG_IN_LOW_BITS
[IN_READ
]);
1587 for (i
= 0; i
< ARRAY_SIZE(data
->fan
); i
++) {
1588 if (!(data
->has_fan
& (1 << i
))) {
1592 w83793_read_value(client
, W83793_REG_FAN(i
)) << 8;
1594 w83793_read_value(client
, W83793_REG_FAN(i
) + 1);
1597 for (i
= 0; i
< ARRAY_SIZE(data
->temp
); i
++) {
1598 if (!(data
->has_temp
& (1 << i
)))
1600 data
->temp
[i
][TEMP_READ
] =
1601 w83793_read_value(client
, W83793_REG_TEMP
[i
][TEMP_READ
]);
1604 data
->temp_low_bits
=
1605 w83793_read_value(client
, W83793_REG_TEMP_LOW_BITS
);
1607 for (i
= 0; i
< ARRAY_SIZE(data
->pwm
); i
++) {
1608 if (data
->has_pwm
& (1 << i
))
1609 data
->pwm
[i
][PWM_DUTY
] =
1610 w83793_read_value(client
,
1611 W83793_REG_PWM(i
, PWM_DUTY
));
1614 for (i
= 0; i
< ARRAY_SIZE(data
->alarms
); i
++)
1616 w83793_read_value(client
, W83793_REG_ALARM(i
));
1617 if (data
->has_vid
& 0x01)
1618 data
->vid
[0] = w83793_read_value(client
, W83793_REG_VID_INA
);
1619 if (data
->has_vid
& 0x02)
1620 data
->vid
[1] = w83793_read_value(client
, W83793_REG_VID_INB
);
1621 w83793_update_nonvolatile(dev
);
1622 data
->last_updated
= jiffies
;
1626 mutex_unlock(&data
->update_lock
);
1630 /* Ignore the possibility that somebody change bank outside the driver
1631 Must be called with data->update_lock held, except during initialization */
1632 static u8
w83793_read_value(struct i2c_client
*client
, u16 reg
)
1634 struct w83793_data
*data
= i2c_get_clientdata(client
);
1636 u8 new_bank
= reg
>> 8;
1638 new_bank
|= data
->bank
& 0xfc;
1639 if (data
->bank
!= new_bank
) {
1640 if (i2c_smbus_write_byte_data
1641 (client
, W83793_REG_BANKSEL
, new_bank
) >= 0)
1642 data
->bank
= new_bank
;
1644 dev_err(&client
->dev
,
1645 "set bank to %d failed, fall back "
1646 "to bank %d, read reg 0x%x error\n",
1647 new_bank
, data
->bank
, reg
);
1648 res
= 0x0; /* read 0x0 from the chip */
1652 res
= i2c_smbus_read_byte_data(client
, reg
& 0xff);
1657 /* Must be called with data->update_lock held, except during initialization */
1658 static int w83793_write_value(struct i2c_client
*client
, u16 reg
, u8 value
)
1660 struct w83793_data
*data
= i2c_get_clientdata(client
);
1662 u8 new_bank
= reg
>> 8;
1664 new_bank
|= data
->bank
& 0xfc;
1665 if (data
->bank
!= new_bank
) {
1666 if ((res
= i2c_smbus_write_byte_data
1667 (client
, W83793_REG_BANKSEL
, new_bank
)) >= 0)
1668 data
->bank
= new_bank
;
1670 dev_err(&client
->dev
,
1671 "set bank to %d failed, fall back "
1672 "to bank %d, write reg 0x%x error\n",
1673 new_bank
, data
->bank
, reg
);
1678 res
= i2c_smbus_write_byte_data(client
, reg
& 0xff, value
);
1683 static int __init
sensors_w83793_init(void)
1685 return i2c_add_driver(&w83793_driver
);
1688 static void __exit
sensors_w83793_exit(void)
1690 i2c_del_driver(&w83793_driver
);
1693 MODULE_AUTHOR("Yuan Mu");
1694 MODULE_DESCRIPTION("w83793 driver");
1695 MODULE_LICENSE("GPL");
1697 module_init(sensors_w83793_init
);
1698 module_exit(sensors_w83793_exit
);