Merge git://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux-2.6
[wrt350n-kernel.git] / drivers / hwmon / w83793.c
blob4bb222521759aab90a1fd3381013cb30507587ba
1 /*
2 w83793.c - Linux kernel driver for hardware monitoring
3 Copyright (C) 2006 Winbond Electronics Corp.
4 Yuan Mu
5 Rudolf Marek <r.marek@assembler.cz>
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
19 02110-1301 USA.
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 <<<<<<< HEAD:drivers/hwmon/w83793.c
41 static unsigned short normal_i2c[] = { 0x2c, 0x2d, 0x2e, 0x2f, I2C_CLIENT_END };
42 =======
43 static const unsigned short normal_i2c[] = { 0x2c, 0x2d, 0x2e, 0x2f,
44 I2C_CLIENT_END };
45 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/hwmon/w83793.c
47 /* Insmod parameters */
48 I2C_CLIENT_INSMOD_1(w83793);
49 I2C_CLIENT_MODULE_PARM(force_subclients, "List of subclient addresses: "
50 "{bus, clientaddr, subclientaddr1, subclientaddr2}");
52 static int reset;
53 module_param(reset, bool, 0);
54 MODULE_PARM_DESC(reset, "Set to 1 to reset chip, not recommended");
57 Address 0x00, 0x0d, 0x0e, 0x0f in all three banks are reserved
58 as ID, Bank Select registers
60 #define W83793_REG_BANKSEL 0x00
61 #define W83793_REG_VENDORID 0x0d
62 #define W83793_REG_CHIPID 0x0e
63 #define W83793_REG_DEVICEID 0x0f
65 #define W83793_REG_CONFIG 0x40
66 #define W83793_REG_MFC 0x58
67 #define W83793_REG_FANIN_CTRL 0x5c
68 #define W83793_REG_FANIN_SEL 0x5d
69 #define W83793_REG_I2C_ADDR 0x0b
70 #define W83793_REG_I2C_SUBADDR 0x0c
71 #define W83793_REG_VID_INA 0x05
72 #define W83793_REG_VID_INB 0x06
73 #define W83793_REG_VID_LATCHA 0x07
74 #define W83793_REG_VID_LATCHB 0x08
75 #define W83793_REG_VID_CTRL 0x59
77 static u16 W83793_REG_TEMP_MODE[2] = { 0x5e, 0x5f };
79 #define TEMP_READ 0
80 #define TEMP_CRIT 1
81 #define TEMP_CRIT_HYST 2
82 #define TEMP_WARN 3
83 #define TEMP_WARN_HYST 4
84 /* only crit and crit_hyst affect real-time alarm status
85 current crit crit_hyst warn warn_hyst */
86 static u16 W83793_REG_TEMP[][5] = {
87 {0x1c, 0x78, 0x79, 0x7a, 0x7b},
88 {0x1d, 0x7c, 0x7d, 0x7e, 0x7f},
89 {0x1e, 0x80, 0x81, 0x82, 0x83},
90 {0x1f, 0x84, 0x85, 0x86, 0x87},
91 {0x20, 0x88, 0x89, 0x8a, 0x8b},
92 {0x21, 0x8c, 0x8d, 0x8e, 0x8f},
95 #define W83793_REG_TEMP_LOW_BITS 0x22
97 #define W83793_REG_BEEP(index) (0x53 + (index))
98 #define W83793_REG_ALARM(index) (0x4b + (index))
100 #define W83793_REG_CLR_CHASSIS 0x4a /* SMI MASK4 */
101 #define W83793_REG_IRQ_CTRL 0x50
102 #define W83793_REG_OVT_CTRL 0x51
103 #define W83793_REG_OVT_BEEP 0x52
105 #define IN_READ 0
106 #define IN_MAX 1
107 #define IN_LOW 2
108 static const u16 W83793_REG_IN[][3] = {
109 /* Current, High, Low */
110 {0x10, 0x60, 0x61}, /* Vcore A */
111 {0x11, 0x62, 0x63}, /* Vcore B */
112 {0x12, 0x64, 0x65}, /* Vtt */
113 {0x14, 0x6a, 0x6b}, /* VSEN1 */
114 {0x15, 0x6c, 0x6d}, /* VSEN2 */
115 {0x16, 0x6e, 0x6f}, /* +3VSEN */
116 {0x17, 0x70, 0x71}, /* +12VSEN */
117 {0x18, 0x72, 0x73}, /* 5VDD */
118 {0x19, 0x74, 0x75}, /* 5VSB */
119 {0x1a, 0x76, 0x77}, /* VBAT */
122 /* Low Bits of Vcore A/B Vtt Read/High/Low */
123 static const u16 W83793_REG_IN_LOW_BITS[] = { 0x1b, 0x68, 0x69 };
124 static u8 scale_in[] = { 2, 2, 2, 16, 16, 16, 8, 24, 24, 16 };
125 static u8 scale_in_add[] = { 0, 0, 0, 0, 0, 0, 0, 150, 150, 0 };
127 #define W83793_REG_FAN(index) (0x23 + 2 * (index)) /* High byte */
128 #define W83793_REG_FAN_MIN(index) (0x90 + 2 * (index)) /* High byte */
130 #define W83793_REG_PWM_DEFAULT 0xb2
131 #define W83793_REG_PWM_ENABLE 0x207
132 #define W83793_REG_PWM_UPTIME 0xc3 /* Unit in 0.1 second */
133 #define W83793_REG_PWM_DOWNTIME 0xc4 /* Unit in 0.1 second */
134 #define W83793_REG_TEMP_CRITICAL 0xc5
136 #define PWM_DUTY 0
137 #define PWM_START 1
138 #define PWM_NONSTOP 2
139 #define PWM_STOP_TIME 3
140 #define W83793_REG_PWM(index, nr) (((nr) == 0 ? 0xb3 : \
141 (nr) == 1 ? 0x220 : 0x218) + (index))
143 /* bit field, fan1 is bit0, fan2 is bit1 ... */
144 #define W83793_REG_TEMP_FAN_MAP(index) (0x201 + (index))
145 #define W83793_REG_TEMP_TOL(index) (0x208 + (index))
146 #define W83793_REG_TEMP_CRUISE(index) (0x210 + (index))
147 #define W83793_REG_PWM_STOP_TIME(index) (0x228 + (index))
148 #define W83793_REG_SF2_TEMP(index, nr) (0x230 + ((index) << 4) + (nr))
149 #define W83793_REG_SF2_PWM(index, nr) (0x238 + ((index) << 4) + (nr))
151 static inline unsigned long FAN_FROM_REG(u16 val)
153 if ((val >= 0xfff) || (val == 0))
154 return 0;
155 return (1350000UL / val);
158 static inline u16 FAN_TO_REG(long rpm)
160 if (rpm <= 0)
161 return 0x0fff;
162 return SENSORS_LIMIT((1350000 + (rpm >> 1)) / rpm, 1, 0xffe);
165 static inline unsigned long TIME_FROM_REG(u8 reg)
167 return (reg * 100);
170 static inline u8 TIME_TO_REG(unsigned long val)
172 return SENSORS_LIMIT((val + 50) / 100, 0, 0xff);
175 static inline long TEMP_FROM_REG(s8 reg)
177 return (reg * 1000);
180 static inline s8 TEMP_TO_REG(long val, s8 min, s8 max)
182 return SENSORS_LIMIT((val + (val < 0 ? -500 : 500)) / 1000, min, max);
185 struct w83793_data {
186 struct i2c_client client;
187 struct i2c_client *lm75[2];
188 struct device *hwmon_dev;
189 struct mutex update_lock;
190 char valid; /* !=0 if following fields are valid */
191 unsigned long last_updated; /* In jiffies */
192 unsigned long last_nonvolatile; /* In jiffies, last time we update the
193 nonvolatile registers */
195 u8 bank;
196 u8 vrm;
197 u8 vid[2];
198 u8 in[10][3]; /* Register value, read/high/low */
199 u8 in_low_bits[3]; /* Additional resolution for VCore A/B Vtt */
201 u16 has_fan; /* Only fan1- fan5 has own pins */
202 u16 fan[12]; /* Register value combine */
203 u16 fan_min[12]; /* Register value combine */
205 s8 temp[6][5]; /* current, crit, crit_hyst,warn, warn_hyst */
206 u8 temp_low_bits; /* Additional resolution TD1-TD4 */
207 u8 temp_mode[2]; /* byte 0: Temp D1-D4 mode each has 2 bits
208 byte 1: Temp R1,R2 mode, each has 1 bit */
209 u8 temp_critical; /* If reached all fan will be at full speed */
210 u8 temp_fan_map[6]; /* Temp controls which pwm fan, bit field */
212 u8 has_pwm;
213 u8 has_temp;
214 u8 has_vid;
215 u8 pwm_enable; /* Register value, each Temp has 1 bit */
216 u8 pwm_uptime; /* Register value */
217 u8 pwm_downtime; /* Register value */
218 u8 pwm_default; /* All fan default pwm, next poweron valid */
219 u8 pwm[8][3]; /* Register value */
220 u8 pwm_stop_time[8];
221 u8 temp_cruise[6];
223 u8 alarms[5]; /* realtime status registers */
224 u8 beeps[5];
225 u8 beep_enable;
226 u8 tolerance[3]; /* Temp tolerance(Smart Fan I/II) */
227 u8 sf2_pwm[6][7]; /* Smart FanII: Fan duty cycle */
228 u8 sf2_temp[6][7]; /* Smart FanII: Temp level point */
231 static u8 w83793_read_value(struct i2c_client *client, u16 reg);
232 static int w83793_write_value(struct i2c_client *client, u16 reg, u8 value);
233 static int w83793_attach_adapter(struct i2c_adapter *adapter);
234 static int w83793_detect(struct i2c_adapter *adapter, int address, int kind);
235 static int w83793_detach_client(struct i2c_client *client);
236 static void w83793_init_client(struct i2c_client *client);
237 static void w83793_update_nonvolatile(struct device *dev);
238 static struct w83793_data *w83793_update_device(struct device *dev);
240 static struct i2c_driver w83793_driver = {
241 .driver = {
242 .name = "w83793",
244 .attach_adapter = w83793_attach_adapter,
245 .detach_client = w83793_detach_client,
248 static ssize_t
249 show_vrm(struct device *dev, struct device_attribute *attr, char *buf)
251 struct w83793_data *data = dev_get_drvdata(dev);
252 return sprintf(buf, "%d\n", data->vrm);
255 static ssize_t
256 show_vid(struct device *dev, struct device_attribute *attr, char *buf)
258 struct w83793_data *data = w83793_update_device(dev);
259 struct sensor_device_attribute_2 *sensor_attr =
260 to_sensor_dev_attr_2(attr);
261 int index = sensor_attr->index;
263 return sprintf(buf, "%d\n", vid_from_reg(data->vid[index], data->vrm));
266 static ssize_t
267 store_vrm(struct device *dev, struct device_attribute *attr,
268 const char *buf, size_t count)
270 struct w83793_data *data = dev_get_drvdata(dev);
271 data->vrm = simple_strtoul(buf, NULL, 10);
272 return count;
275 #define ALARM_STATUS 0
276 #define BEEP_ENABLE 1
277 static ssize_t
278 show_alarm_beep(struct device *dev, struct device_attribute *attr, char *buf)
280 struct w83793_data *data = w83793_update_device(dev);
281 struct sensor_device_attribute_2 *sensor_attr =
282 to_sensor_dev_attr_2(attr);
283 int nr = sensor_attr->nr;
284 int index = sensor_attr->index >> 3;
285 int bit = sensor_attr->index & 0x07;
286 u8 val;
288 if (ALARM_STATUS == nr) {
289 val = (data->alarms[index] >> (bit)) & 1;
290 } else { /* BEEP_ENABLE */
291 val = (data->beeps[index] >> (bit)) & 1;
294 return sprintf(buf, "%u\n", val);
297 static ssize_t
298 store_beep(struct device *dev, struct device_attribute *attr,
299 const char *buf, size_t count)
301 struct i2c_client *client = to_i2c_client(dev);
302 struct w83793_data *data = i2c_get_clientdata(client);
303 struct sensor_device_attribute_2 *sensor_attr =
304 to_sensor_dev_attr_2(attr);
305 int index = sensor_attr->index >> 3;
306 int shift = sensor_attr->index & 0x07;
307 u8 beep_bit = 1 << shift;
308 u8 val;
310 val = simple_strtoul(buf, NULL, 10);
311 if (val != 0 && val != 1)
312 return -EINVAL;
314 mutex_lock(&data->update_lock);
315 data->beeps[index] = w83793_read_value(client, W83793_REG_BEEP(index));
316 data->beeps[index] &= ~beep_bit;
317 data->beeps[index] |= val << shift;
318 w83793_write_value(client, W83793_REG_BEEP(index), data->beeps[index]);
319 mutex_unlock(&data->update_lock);
321 return count;
324 static ssize_t
325 show_beep_enable(struct device *dev, struct device_attribute *attr, char *buf)
327 struct w83793_data *data = w83793_update_device(dev);
328 return sprintf(buf, "%u\n", (data->beep_enable >> 1) & 0x01);
331 static ssize_t
332 store_beep_enable(struct device *dev, struct device_attribute *attr,
333 const char *buf, size_t count)
335 struct i2c_client *client = to_i2c_client(dev);
336 struct w83793_data *data = i2c_get_clientdata(client);
337 u8 val = simple_strtoul(buf, NULL, 10);
339 if (val != 0 && val != 1)
340 return -EINVAL;
342 mutex_lock(&data->update_lock);
343 data->beep_enable = w83793_read_value(client, W83793_REG_OVT_BEEP)
344 & 0xfd;
345 data->beep_enable |= val << 1;
346 w83793_write_value(client, W83793_REG_OVT_BEEP, data->beep_enable);
347 mutex_unlock(&data->update_lock);
349 return count;
352 /* Write any value to clear chassis alarm */
353 static ssize_t
354 store_chassis_clear(struct device *dev,
355 struct device_attribute *attr, const char *buf,
356 size_t count)
358 struct i2c_client *client = to_i2c_client(dev);
359 struct w83793_data *data = i2c_get_clientdata(client);
360 u8 val;
362 mutex_lock(&data->update_lock);
363 val = w83793_read_value(client, W83793_REG_CLR_CHASSIS);
364 val |= 0x80;
365 w83793_write_value(client, W83793_REG_CLR_CHASSIS, val);
366 mutex_unlock(&data->update_lock);
367 return count;
370 #define FAN_INPUT 0
371 #define FAN_MIN 1
372 static ssize_t
373 show_fan(struct device *dev, struct device_attribute *attr, char *buf)
375 struct sensor_device_attribute_2 *sensor_attr =
376 to_sensor_dev_attr_2(attr);
377 int nr = sensor_attr->nr;
378 int index = sensor_attr->index;
379 struct w83793_data *data = w83793_update_device(dev);
380 u16 val;
382 if (FAN_INPUT == nr) {
383 val = data->fan[index] & 0x0fff;
384 } else {
385 val = data->fan_min[index] & 0x0fff;
388 return sprintf(buf, "%lu\n", FAN_FROM_REG(val));
391 static ssize_t
392 store_fan_min(struct device *dev, struct device_attribute *attr,
393 const char *buf, size_t count)
395 struct sensor_device_attribute_2 *sensor_attr =
396 to_sensor_dev_attr_2(attr);
397 int index = sensor_attr->index;
398 struct i2c_client *client = to_i2c_client(dev);
399 struct w83793_data *data = i2c_get_clientdata(client);
400 u16 val = FAN_TO_REG(simple_strtoul(buf, NULL, 10));
402 mutex_lock(&data->update_lock);
403 data->fan_min[index] = val;
404 w83793_write_value(client, W83793_REG_FAN_MIN(index),
405 (val >> 8) & 0xff);
406 w83793_write_value(client, W83793_REG_FAN_MIN(index) + 1, val & 0xff);
407 mutex_unlock(&data->update_lock);
409 return count;
412 static ssize_t
413 show_pwm(struct device *dev, struct device_attribute *attr, char *buf)
415 struct sensor_device_attribute_2 *sensor_attr =
416 to_sensor_dev_attr_2(attr);
417 struct w83793_data *data = w83793_update_device(dev);
418 u16 val;
419 int nr = sensor_attr->nr;
420 int index = sensor_attr->index;
422 if (PWM_STOP_TIME == nr)
423 val = TIME_FROM_REG(data->pwm_stop_time[index]);
424 else
425 val = (data->pwm[index][nr] & 0x3f) << 2;
427 return sprintf(buf, "%d\n", val);
430 static ssize_t
431 store_pwm(struct device *dev, struct device_attribute *attr,
432 const char *buf, size_t count)
434 struct i2c_client *client = to_i2c_client(dev);
435 struct w83793_data *data = i2c_get_clientdata(client);
436 struct sensor_device_attribute_2 *sensor_attr =
437 to_sensor_dev_attr_2(attr);
438 int nr = sensor_attr->nr;
439 int index = sensor_attr->index;
440 u8 val;
442 mutex_lock(&data->update_lock);
443 if (PWM_STOP_TIME == nr) {
444 val = TIME_TO_REG(simple_strtoul(buf, NULL, 10));
445 data->pwm_stop_time[index] = val;
446 w83793_write_value(client, W83793_REG_PWM_STOP_TIME(index),
447 val);
448 } else {
449 val = SENSORS_LIMIT(simple_strtoul(buf, NULL, 10), 0, 0xff)
450 >> 2;
451 data->pwm[index][nr] =
452 w83793_read_value(client, W83793_REG_PWM(index, nr)) & 0xc0;
453 data->pwm[index][nr] |= val;
454 w83793_write_value(client, W83793_REG_PWM(index, nr),
455 data->pwm[index][nr]);
458 mutex_unlock(&data->update_lock);
459 return count;
462 static ssize_t
463 show_temp(struct device *dev, struct device_attribute *attr, char *buf)
465 struct sensor_device_attribute_2 *sensor_attr =
466 to_sensor_dev_attr_2(attr);
467 int nr = sensor_attr->nr;
468 int index = sensor_attr->index;
469 struct w83793_data *data = w83793_update_device(dev);
470 long temp = TEMP_FROM_REG(data->temp[index][nr]);
472 if (TEMP_READ == nr && index < 4) { /* Only TD1-TD4 have low bits */
473 int low = ((data->temp_low_bits >> (index * 2)) & 0x03) * 250;
474 temp += temp > 0 ? low : -low;
476 return sprintf(buf, "%ld\n", temp);
479 static ssize_t
480 store_temp(struct device *dev, struct device_attribute *attr,
481 const char *buf, size_t count)
483 struct sensor_device_attribute_2 *sensor_attr =
484 to_sensor_dev_attr_2(attr);
485 int nr = sensor_attr->nr;
486 int index = sensor_attr->index;
487 struct i2c_client *client = to_i2c_client(dev);
488 struct w83793_data *data = i2c_get_clientdata(client);
489 long tmp = simple_strtol(buf, NULL, 10);
491 mutex_lock(&data->update_lock);
492 data->temp[index][nr] = TEMP_TO_REG(tmp, -128, 127);
493 w83793_write_value(client, W83793_REG_TEMP[index][nr],
494 data->temp[index][nr]);
495 mutex_unlock(&data->update_lock);
496 return count;
500 TD1-TD4
501 each has 4 mode:(2 bits)
502 0: Stop monitor
503 1: Use internal temp sensor(default)
504 2: Reserved
505 3: Use sensor in Intel CPU and get result by PECI
507 TR1-TR2
508 each has 2 mode:(1 bit)
509 0: Disable temp sensor monitor
510 1: To enable temp sensors monitor
513 /* 0 disable, 6 PECI */
514 static u8 TO_TEMP_MODE[] = { 0, 0, 0, 6 };
516 static ssize_t
517 show_temp_mode(struct device *dev, struct device_attribute *attr, char *buf)
519 struct w83793_data *data = w83793_update_device(dev);
520 struct sensor_device_attribute_2 *sensor_attr =
521 to_sensor_dev_attr_2(attr);
522 int index = sensor_attr->index;
523 u8 mask = (index < 4) ? 0x03 : 0x01;
524 u8 shift = (index < 4) ? (2 * index) : (index - 4);
525 u8 tmp;
526 index = (index < 4) ? 0 : 1;
528 tmp = (data->temp_mode[index] >> shift) & mask;
530 /* for the internal sensor, found out if diode or thermistor */
531 if (tmp == 1) {
532 tmp = index == 0 ? 3 : 4;
533 } else {
534 tmp = TO_TEMP_MODE[tmp];
537 return sprintf(buf, "%d\n", tmp);
540 static ssize_t
541 store_temp_mode(struct device *dev, struct device_attribute *attr,
542 const char *buf, size_t count)
544 struct i2c_client *client = to_i2c_client(dev);
545 struct w83793_data *data = i2c_get_clientdata(client);
546 struct sensor_device_attribute_2 *sensor_attr =
547 to_sensor_dev_attr_2(attr);
548 int index = sensor_attr->index;
549 u8 mask = (index < 4) ? 0x03 : 0x01;
550 u8 shift = (index < 4) ? (2 * index) : (index - 4);
551 u8 val = simple_strtoul(buf, NULL, 10);
553 /* transform the sysfs interface values into table above */
554 if ((val == 6) && (index < 4)) {
555 val -= 3;
556 } else if ((val == 3 && index < 4)
557 || (val == 4 && index >= 4)) {
558 /* transform diode or thermistor into internal enable */
559 val = !!val;
560 } else {
561 return -EINVAL;
564 index = (index < 4) ? 0 : 1;
565 mutex_lock(&data->update_lock);
566 data->temp_mode[index] =
567 w83793_read_value(client, W83793_REG_TEMP_MODE[index]);
568 data->temp_mode[index] &= ~(mask << shift);
569 data->temp_mode[index] |= val << shift;
570 w83793_write_value(client, W83793_REG_TEMP_MODE[index],
571 data->temp_mode[index]);
572 mutex_unlock(&data->update_lock);
574 return count;
577 #define SETUP_PWM_DEFAULT 0
578 #define SETUP_PWM_UPTIME 1 /* Unit in 0.1s */
579 #define SETUP_PWM_DOWNTIME 2 /* Unit in 0.1s */
580 #define SETUP_TEMP_CRITICAL 3
581 static ssize_t
582 show_sf_setup(struct device *dev, struct device_attribute *attr, char *buf)
584 struct sensor_device_attribute_2 *sensor_attr =
585 to_sensor_dev_attr_2(attr);
586 int nr = sensor_attr->nr;
587 struct w83793_data *data = w83793_update_device(dev);
588 u32 val = 0;
590 if (SETUP_PWM_DEFAULT == nr) {
591 val = (data->pwm_default & 0x3f) << 2;
592 } else if (SETUP_PWM_UPTIME == nr) {
593 val = TIME_FROM_REG(data->pwm_uptime);
594 } else if (SETUP_PWM_DOWNTIME == nr) {
595 val = TIME_FROM_REG(data->pwm_downtime);
596 } else if (SETUP_TEMP_CRITICAL == nr) {
597 val = TEMP_FROM_REG(data->temp_critical & 0x7f);
600 return sprintf(buf, "%d\n", val);
603 static ssize_t
604 store_sf_setup(struct device *dev, struct device_attribute *attr,
605 const char *buf, size_t count)
607 struct sensor_device_attribute_2 *sensor_attr =
608 to_sensor_dev_attr_2(attr);
609 int nr = sensor_attr->nr;
610 struct i2c_client *client = to_i2c_client(dev);
611 struct w83793_data *data = i2c_get_clientdata(client);
613 mutex_lock(&data->update_lock);
614 if (SETUP_PWM_DEFAULT == nr) {
615 data->pwm_default =
616 w83793_read_value(client, W83793_REG_PWM_DEFAULT) & 0xc0;
617 data->pwm_default |= SENSORS_LIMIT(simple_strtoul(buf, NULL,
618 10),
619 0, 0xff) >> 2;
620 w83793_write_value(client, W83793_REG_PWM_DEFAULT,
621 data->pwm_default);
622 } else if (SETUP_PWM_UPTIME == nr) {
623 data->pwm_uptime = TIME_TO_REG(simple_strtoul(buf, NULL, 10));
624 data->pwm_uptime += data->pwm_uptime == 0 ? 1 : 0;
625 w83793_write_value(client, W83793_REG_PWM_UPTIME,
626 data->pwm_uptime);
627 } else if (SETUP_PWM_DOWNTIME == nr) {
628 data->pwm_downtime = TIME_TO_REG(simple_strtoul(buf, NULL, 10));
629 data->pwm_downtime += data->pwm_downtime == 0 ? 1 : 0;
630 w83793_write_value(client, W83793_REG_PWM_DOWNTIME,
631 data->pwm_downtime);
632 } else { /* SETUP_TEMP_CRITICAL */
633 data->temp_critical =
634 w83793_read_value(client, W83793_REG_TEMP_CRITICAL) & 0x80;
635 data->temp_critical |= TEMP_TO_REG(simple_strtol(buf, NULL, 10),
636 0, 0x7f);
637 w83793_write_value(client, W83793_REG_TEMP_CRITICAL,
638 data->temp_critical);
641 mutex_unlock(&data->update_lock);
642 return count;
646 Temp SmartFan control
647 TEMP_FAN_MAP
648 Temp channel control which pwm fan, bitfield, bit 0 indicate pwm1...
649 It's possible two or more temp channels control the same fan, w83793
650 always prefers to pick the most critical request and applies it to
651 the related Fan.
652 It's possible one fan is not in any mapping of 6 temp channels, this
653 means the fan is manual mode
655 TEMP_PWM_ENABLE
656 Each temp channel has its own SmartFan mode, and temp channel
657 control fans that are set by TEMP_FAN_MAP
658 0: SmartFanII mode
659 1: Thermal Cruise Mode
661 TEMP_CRUISE
662 Target temperature in thermal cruise mode, w83793 will try to turn
663 fan speed to keep the temperature of target device around this
664 temperature.
666 TEMP_TOLERANCE
667 If Temp higher or lower than target with this tolerance, w83793
668 will take actions to speed up or slow down the fan to keep the
669 temperature within the tolerance range.
672 #define TEMP_FAN_MAP 0
673 #define TEMP_PWM_ENABLE 1
674 #define TEMP_CRUISE 2
675 #define TEMP_TOLERANCE 3
676 static ssize_t
677 show_sf_ctrl(struct device *dev, struct device_attribute *attr, char *buf)
679 struct sensor_device_attribute_2 *sensor_attr =
680 to_sensor_dev_attr_2(attr);
681 int nr = sensor_attr->nr;
682 int index = sensor_attr->index;
683 struct w83793_data *data = w83793_update_device(dev);
684 u32 val;
686 if (TEMP_FAN_MAP == nr) {
687 val = data->temp_fan_map[index];
688 } else if (TEMP_PWM_ENABLE == nr) {
689 /* +2 to transfrom into 2 and 3 to conform with sysfs intf */
690 val = ((data->pwm_enable >> index) & 0x01) + 2;
691 } else if (TEMP_CRUISE == nr) {
692 val = TEMP_FROM_REG(data->temp_cruise[index] & 0x7f);
693 } else { /* TEMP_TOLERANCE */
694 val = data->tolerance[index >> 1] >> ((index & 0x01) ? 4 : 0);
695 val = TEMP_FROM_REG(val & 0x0f);
697 return sprintf(buf, "%d\n", val);
700 static ssize_t
701 store_sf_ctrl(struct device *dev, struct device_attribute *attr,
702 const char *buf, size_t count)
704 struct sensor_device_attribute_2 *sensor_attr =
705 to_sensor_dev_attr_2(attr);
706 int nr = sensor_attr->nr;
707 int index = sensor_attr->index;
708 struct i2c_client *client = to_i2c_client(dev);
709 struct w83793_data *data = i2c_get_clientdata(client);
710 u32 val;
712 mutex_lock(&data->update_lock);
713 if (TEMP_FAN_MAP == nr) {
714 val = simple_strtoul(buf, NULL, 10) & 0xff;
715 w83793_write_value(client, W83793_REG_TEMP_FAN_MAP(index), val);
716 data->temp_fan_map[index] = val;
717 } else if (TEMP_PWM_ENABLE == nr) {
718 val = simple_strtoul(buf, NULL, 10);
719 if (2 == val || 3 == val) {
720 data->pwm_enable =
721 w83793_read_value(client, W83793_REG_PWM_ENABLE);
722 if (val - 2)
723 data->pwm_enable |= 1 << index;
724 else
725 data->pwm_enable &= ~(1 << index);
726 w83793_write_value(client, W83793_REG_PWM_ENABLE,
727 data->pwm_enable);
728 } else {
729 mutex_unlock(&data->update_lock);
730 return -EINVAL;
732 } else if (TEMP_CRUISE == nr) {
733 data->temp_cruise[index] =
734 w83793_read_value(client, W83793_REG_TEMP_CRUISE(index));
735 val = TEMP_TO_REG(simple_strtol(buf, NULL, 10), 0, 0x7f);
736 data->temp_cruise[index] &= 0x80;
737 data->temp_cruise[index] |= val;
739 w83793_write_value(client, W83793_REG_TEMP_CRUISE(index),
740 data->temp_cruise[index]);
741 } else { /* TEMP_TOLERANCE */
742 int i = index >> 1;
743 u8 shift = (index & 0x01) ? 4 : 0;
744 data->tolerance[i] =
745 w83793_read_value(client, W83793_REG_TEMP_TOL(i));
747 val = TEMP_TO_REG(simple_strtol(buf, NULL, 10), 0, 0x0f);
748 data->tolerance[i] &= ~(0x0f << shift);
749 data->tolerance[i] |= val << shift;
750 w83793_write_value(client, W83793_REG_TEMP_TOL(i),
751 data->tolerance[i]);
754 mutex_unlock(&data->update_lock);
755 return count;
758 static ssize_t
759 show_sf2_pwm(struct device *dev, struct device_attribute *attr, char *buf)
761 struct sensor_device_attribute_2 *sensor_attr =
762 to_sensor_dev_attr_2(attr);
763 int nr = sensor_attr->nr;
764 int index = sensor_attr->index;
765 struct w83793_data *data = w83793_update_device(dev);
767 return sprintf(buf, "%d\n", (data->sf2_pwm[index][nr] & 0x3f) << 2);
770 static ssize_t
771 store_sf2_pwm(struct device *dev, struct device_attribute *attr,
772 const char *buf, size_t count)
774 struct i2c_client *client = to_i2c_client(dev);
775 struct w83793_data *data = i2c_get_clientdata(client);
776 struct sensor_device_attribute_2 *sensor_attr =
777 to_sensor_dev_attr_2(attr);
778 int nr = sensor_attr->nr;
779 int index = sensor_attr->index;
780 u8 val = SENSORS_LIMIT(simple_strtoul(buf, NULL, 10), 0, 0xff) >> 2;
782 mutex_lock(&data->update_lock);
783 data->sf2_pwm[index][nr] =
784 w83793_read_value(client, W83793_REG_SF2_PWM(index, nr)) & 0xc0;
785 data->sf2_pwm[index][nr] |= val;
786 w83793_write_value(client, W83793_REG_SF2_PWM(index, nr),
787 data->sf2_pwm[index][nr]);
788 mutex_unlock(&data->update_lock);
789 return count;
792 static ssize_t
793 show_sf2_temp(struct device *dev, struct device_attribute *attr, char *buf)
795 struct sensor_device_attribute_2 *sensor_attr =
796 to_sensor_dev_attr_2(attr);
797 int nr = sensor_attr->nr;
798 int index = sensor_attr->index;
799 struct w83793_data *data = w83793_update_device(dev);
801 return sprintf(buf, "%ld\n",
802 TEMP_FROM_REG(data->sf2_temp[index][nr] & 0x7f));
805 static ssize_t
806 store_sf2_temp(struct device *dev, struct device_attribute *attr,
807 const char *buf, size_t count)
809 struct i2c_client *client = to_i2c_client(dev);
810 struct w83793_data *data = i2c_get_clientdata(client);
811 struct sensor_device_attribute_2 *sensor_attr =
812 to_sensor_dev_attr_2(attr);
813 int nr = sensor_attr->nr;
814 int index = sensor_attr->index;
815 u8 val = TEMP_TO_REG(simple_strtol(buf, NULL, 10), 0, 0x7f);
817 mutex_lock(&data->update_lock);
818 data->sf2_temp[index][nr] =
819 w83793_read_value(client, W83793_REG_SF2_TEMP(index, nr)) & 0x80;
820 data->sf2_temp[index][nr] |= val;
821 w83793_write_value(client, W83793_REG_SF2_TEMP(index, nr),
822 data->sf2_temp[index][nr]);
823 mutex_unlock(&data->update_lock);
824 return count;
827 /* only Vcore A/B and Vtt have additional 2 bits precision */
828 static ssize_t
829 show_in(struct device *dev, struct device_attribute *attr, char *buf)
831 struct sensor_device_attribute_2 *sensor_attr =
832 to_sensor_dev_attr_2(attr);
833 int nr = sensor_attr->nr;
834 int index = sensor_attr->index;
835 struct w83793_data *data = w83793_update_device(dev);
836 u16 val = data->in[index][nr];
838 if (index < 3) {
839 val <<= 2;
840 val += (data->in_low_bits[nr] >> (index * 2)) & 0x3;
842 /* voltage inputs 5VDD and 5VSB needs 150mV offset */
843 val = val * scale_in[index] + scale_in_add[index];
844 return sprintf(buf, "%d\n", val);
847 static ssize_t
848 store_in(struct device *dev, struct device_attribute *attr,
849 const char *buf, size_t count)
851 struct sensor_device_attribute_2 *sensor_attr =
852 to_sensor_dev_attr_2(attr);
853 int nr = sensor_attr->nr;
854 int index = sensor_attr->index;
855 struct i2c_client *client = to_i2c_client(dev);
856 struct w83793_data *data = i2c_get_clientdata(client);
857 u32 val;
859 val =
860 (simple_strtoul(buf, NULL, 10) +
861 scale_in[index] / 2) / scale_in[index];
862 mutex_lock(&data->update_lock);
863 if (index > 2) {
864 /* fix the limit values of 5VDD and 5VSB to ALARM mechanism */
865 if (1 == nr || 2 == nr) {
866 val -= scale_in_add[index] / scale_in[index];
868 val = SENSORS_LIMIT(val, 0, 255);
869 } else {
870 val = SENSORS_LIMIT(val, 0, 0x3FF);
871 data->in_low_bits[nr] =
872 w83793_read_value(client, W83793_REG_IN_LOW_BITS[nr]);
873 data->in_low_bits[nr] &= ~(0x03 << (2 * index));
874 data->in_low_bits[nr] |= (val & 0x03) << (2 * index);
875 w83793_write_value(client, W83793_REG_IN_LOW_BITS[nr],
876 data->in_low_bits[nr]);
877 val >>= 2;
879 data->in[index][nr] = val;
880 w83793_write_value(client, W83793_REG_IN[index][nr],
881 data->in[index][nr]);
882 mutex_unlock(&data->update_lock);
883 return count;
886 #define NOT_USED -1
888 #define SENSOR_ATTR_IN(index) \
889 SENSOR_ATTR_2(in##index##_input, S_IRUGO, show_in, NULL, \
890 IN_READ, index), \
891 SENSOR_ATTR_2(in##index##_max, S_IRUGO | S_IWUSR, show_in, \
892 store_in, IN_MAX, index), \
893 SENSOR_ATTR_2(in##index##_min, S_IRUGO | S_IWUSR, show_in, \
894 store_in, IN_LOW, index), \
895 SENSOR_ATTR_2(in##index##_alarm, S_IRUGO, show_alarm_beep, \
896 NULL, ALARM_STATUS, index + ((index > 2) ? 1 : 0)), \
897 SENSOR_ATTR_2(in##index##_beep, S_IWUSR | S_IRUGO, \
898 show_alarm_beep, store_beep, BEEP_ENABLE, \
899 index + ((index > 2) ? 1 : 0))
901 #define SENSOR_ATTR_FAN(index) \
902 SENSOR_ATTR_2(fan##index##_alarm, S_IRUGO, show_alarm_beep, \
903 NULL, ALARM_STATUS, index + 17), \
904 SENSOR_ATTR_2(fan##index##_beep, S_IWUSR | S_IRUGO, \
905 show_alarm_beep, store_beep, BEEP_ENABLE, index + 17), \
906 SENSOR_ATTR_2(fan##index##_input, S_IRUGO, show_fan, \
907 NULL, FAN_INPUT, index - 1), \
908 SENSOR_ATTR_2(fan##index##_min, S_IWUSR | S_IRUGO, \
909 show_fan, store_fan_min, FAN_MIN, index - 1)
911 #define SENSOR_ATTR_PWM(index) \
912 SENSOR_ATTR_2(pwm##index, S_IWUSR | S_IRUGO, show_pwm, \
913 store_pwm, PWM_DUTY, index - 1), \
914 SENSOR_ATTR_2(pwm##index##_nonstop, S_IWUSR | S_IRUGO, \
915 show_pwm, store_pwm, PWM_NONSTOP, index - 1), \
916 SENSOR_ATTR_2(pwm##index##_start, S_IWUSR | S_IRUGO, \
917 show_pwm, store_pwm, PWM_START, index - 1), \
918 SENSOR_ATTR_2(pwm##index##_stop_time, S_IWUSR | S_IRUGO, \
919 show_pwm, store_pwm, PWM_STOP_TIME, index - 1)
921 #define SENSOR_ATTR_TEMP(index) \
922 SENSOR_ATTR_2(temp##index##_type, S_IRUGO | S_IWUSR, \
923 show_temp_mode, store_temp_mode, NOT_USED, index - 1), \
924 SENSOR_ATTR_2(temp##index##_input, S_IRUGO, show_temp, \
925 NULL, TEMP_READ, index - 1), \
926 SENSOR_ATTR_2(temp##index##_max, S_IRUGO | S_IWUSR, show_temp, \
927 store_temp, TEMP_CRIT, index - 1), \
928 SENSOR_ATTR_2(temp##index##_max_hyst, S_IRUGO | S_IWUSR, \
929 show_temp, store_temp, TEMP_CRIT_HYST, index - 1), \
930 SENSOR_ATTR_2(temp##index##_warn, S_IRUGO | S_IWUSR, show_temp, \
931 store_temp, TEMP_WARN, index - 1), \
932 SENSOR_ATTR_2(temp##index##_warn_hyst, S_IRUGO | S_IWUSR, \
933 show_temp, store_temp, TEMP_WARN_HYST, index - 1), \
934 SENSOR_ATTR_2(temp##index##_alarm, S_IRUGO, \
935 show_alarm_beep, NULL, ALARM_STATUS, index + 11), \
936 SENSOR_ATTR_2(temp##index##_beep, S_IWUSR | S_IRUGO, \
937 show_alarm_beep, store_beep, BEEP_ENABLE, index + 11), \
938 SENSOR_ATTR_2(temp##index##_auto_channels_pwm, \
939 S_IRUGO | S_IWUSR, show_sf_ctrl, store_sf_ctrl, \
940 TEMP_FAN_MAP, index - 1), \
941 SENSOR_ATTR_2(temp##index##_pwm_enable, S_IWUSR | S_IRUGO, \
942 show_sf_ctrl, store_sf_ctrl, TEMP_PWM_ENABLE, \
943 index - 1), \
944 SENSOR_ATTR_2(thermal_cruise##index, S_IRUGO | S_IWUSR, \
945 show_sf_ctrl, store_sf_ctrl, TEMP_CRUISE, index - 1), \
946 SENSOR_ATTR_2(tolerance##index, S_IRUGO | S_IWUSR, show_sf_ctrl,\
947 store_sf_ctrl, TEMP_TOLERANCE, index - 1), \
948 SENSOR_ATTR_2(temp##index##_auto_point1_pwm, S_IRUGO | S_IWUSR, \
949 show_sf2_pwm, store_sf2_pwm, 0, index - 1), \
950 SENSOR_ATTR_2(temp##index##_auto_point2_pwm, S_IRUGO | S_IWUSR, \
951 show_sf2_pwm, store_sf2_pwm, 1, index - 1), \
952 SENSOR_ATTR_2(temp##index##_auto_point3_pwm, S_IRUGO | S_IWUSR, \
953 show_sf2_pwm, store_sf2_pwm, 2, index - 1), \
954 SENSOR_ATTR_2(temp##index##_auto_point4_pwm, S_IRUGO | S_IWUSR, \
955 show_sf2_pwm, store_sf2_pwm, 3, index - 1), \
956 SENSOR_ATTR_2(temp##index##_auto_point5_pwm, S_IRUGO | S_IWUSR, \
957 show_sf2_pwm, store_sf2_pwm, 4, index - 1), \
958 SENSOR_ATTR_2(temp##index##_auto_point6_pwm, S_IRUGO | S_IWUSR, \
959 show_sf2_pwm, store_sf2_pwm, 5, index - 1), \
960 SENSOR_ATTR_2(temp##index##_auto_point7_pwm, S_IRUGO | S_IWUSR, \
961 show_sf2_pwm, store_sf2_pwm, 6, index - 1), \
962 SENSOR_ATTR_2(temp##index##_auto_point1_temp, S_IRUGO | S_IWUSR,\
963 show_sf2_temp, store_sf2_temp, 0, index - 1), \
964 SENSOR_ATTR_2(temp##index##_auto_point2_temp, S_IRUGO | S_IWUSR,\
965 show_sf2_temp, store_sf2_temp, 1, index - 1), \
966 SENSOR_ATTR_2(temp##index##_auto_point3_temp, S_IRUGO | S_IWUSR,\
967 show_sf2_temp, store_sf2_temp, 2, index - 1), \
968 SENSOR_ATTR_2(temp##index##_auto_point4_temp, S_IRUGO | S_IWUSR,\
969 show_sf2_temp, store_sf2_temp, 3, index - 1), \
970 SENSOR_ATTR_2(temp##index##_auto_point5_temp, S_IRUGO | S_IWUSR,\
971 show_sf2_temp, store_sf2_temp, 4, index - 1), \
972 SENSOR_ATTR_2(temp##index##_auto_point6_temp, S_IRUGO | S_IWUSR,\
973 show_sf2_temp, store_sf2_temp, 5, index - 1), \
974 SENSOR_ATTR_2(temp##index##_auto_point7_temp, S_IRUGO | S_IWUSR,\
975 show_sf2_temp, store_sf2_temp, 6, index - 1)
977 static struct sensor_device_attribute_2 w83793_sensor_attr_2[] = {
978 SENSOR_ATTR_IN(0),
979 SENSOR_ATTR_IN(1),
980 SENSOR_ATTR_IN(2),
981 SENSOR_ATTR_IN(3),
982 SENSOR_ATTR_IN(4),
983 SENSOR_ATTR_IN(5),
984 SENSOR_ATTR_IN(6),
985 SENSOR_ATTR_IN(7),
986 SENSOR_ATTR_IN(8),
987 SENSOR_ATTR_IN(9),
988 SENSOR_ATTR_FAN(1),
989 SENSOR_ATTR_FAN(2),
990 SENSOR_ATTR_FAN(3),
991 SENSOR_ATTR_FAN(4),
992 SENSOR_ATTR_FAN(5),
993 SENSOR_ATTR_PWM(1),
994 SENSOR_ATTR_PWM(2),
995 SENSOR_ATTR_PWM(3),
998 static struct sensor_device_attribute_2 w83793_temp[] = {
999 SENSOR_ATTR_TEMP(1),
1000 SENSOR_ATTR_TEMP(2),
1001 SENSOR_ATTR_TEMP(3),
1002 SENSOR_ATTR_TEMP(4),
1003 SENSOR_ATTR_TEMP(5),
1004 SENSOR_ATTR_TEMP(6),
1007 /* Fan6-Fan12 */
1008 static struct sensor_device_attribute_2 w83793_left_fan[] = {
1009 SENSOR_ATTR_FAN(6),
1010 SENSOR_ATTR_FAN(7),
1011 SENSOR_ATTR_FAN(8),
1012 SENSOR_ATTR_FAN(9),
1013 SENSOR_ATTR_FAN(10),
1014 SENSOR_ATTR_FAN(11),
1015 SENSOR_ATTR_FAN(12),
1018 /* Pwm4-Pwm8 */
1019 static struct sensor_device_attribute_2 w83793_left_pwm[] = {
1020 SENSOR_ATTR_PWM(4),
1021 SENSOR_ATTR_PWM(5),
1022 SENSOR_ATTR_PWM(6),
1023 SENSOR_ATTR_PWM(7),
1024 SENSOR_ATTR_PWM(8),
1027 static struct sensor_device_attribute_2 w83793_vid[] = {
1028 SENSOR_ATTR_2(cpu0_vid, S_IRUGO, show_vid, NULL, NOT_USED, 0),
1029 SENSOR_ATTR_2(cpu1_vid, S_IRUGO, show_vid, NULL, NOT_USED, 1),
1032 static struct sensor_device_attribute_2 sda_single_files[] = {
1033 SENSOR_ATTR_2(vrm, S_IWUSR | S_IRUGO, show_vrm, store_vrm,
1034 NOT_USED, NOT_USED),
1035 SENSOR_ATTR_2(chassis, S_IWUSR | S_IRUGO, show_alarm_beep,
1036 store_chassis_clear, ALARM_STATUS, 30),
1037 SENSOR_ATTR_2(beep_enable, S_IWUSR | S_IRUGO, show_beep_enable,
1038 store_beep_enable, NOT_USED, NOT_USED),
1039 SENSOR_ATTR_2(pwm_default, S_IWUSR | S_IRUGO, show_sf_setup,
1040 store_sf_setup, SETUP_PWM_DEFAULT, NOT_USED),
1041 SENSOR_ATTR_2(pwm_uptime, S_IWUSR | S_IRUGO, show_sf_setup,
1042 store_sf_setup, SETUP_PWM_UPTIME, NOT_USED),
1043 SENSOR_ATTR_2(pwm_downtime, S_IWUSR | S_IRUGO, show_sf_setup,
1044 store_sf_setup, SETUP_PWM_DOWNTIME, NOT_USED),
1045 SENSOR_ATTR_2(temp_critical, S_IWUSR | S_IRUGO, show_sf_setup,
1046 store_sf_setup, SETUP_TEMP_CRITICAL, NOT_USED),
1049 static void w83793_init_client(struct i2c_client *client)
1051 if (reset) {
1052 w83793_write_value(client, W83793_REG_CONFIG, 0x80);
1055 /* Start monitoring */
1056 w83793_write_value(client, W83793_REG_CONFIG,
1057 w83793_read_value(client, W83793_REG_CONFIG) | 0x01);
1061 static int w83793_attach_adapter(struct i2c_adapter *adapter)
1063 if (!(adapter->class & I2C_CLASS_HWMON))
1064 return 0;
1065 return i2c_probe(adapter, &addr_data, w83793_detect);
1068 static int w83793_detach_client(struct i2c_client *client)
1070 struct w83793_data *data = i2c_get_clientdata(client);
1071 struct device *dev = &client->dev;
1072 int err, i;
1074 /* main client */
1075 if (data) {
1076 hwmon_device_unregister(data->hwmon_dev);
1078 for (i = 0; i < ARRAY_SIZE(w83793_sensor_attr_2); i++)
1079 device_remove_file(dev,
1080 &w83793_sensor_attr_2[i].dev_attr);
1082 for (i = 0; i < ARRAY_SIZE(sda_single_files); i++)
1083 device_remove_file(dev, &sda_single_files[i].dev_attr);
1085 for (i = 0; i < ARRAY_SIZE(w83793_vid); i++)
1086 device_remove_file(dev, &w83793_vid[i].dev_attr);
1088 for (i = 0; i < ARRAY_SIZE(w83793_left_fan); i++)
1089 device_remove_file(dev, &w83793_left_fan[i].dev_attr);
1091 for (i = 0; i < ARRAY_SIZE(w83793_left_pwm); i++)
1092 device_remove_file(dev, &w83793_left_pwm[i].dev_attr);
1094 for (i = 0; i < ARRAY_SIZE(w83793_temp); i++)
1095 device_remove_file(dev, &w83793_temp[i].dev_attr);
1098 if ((err = i2c_detach_client(client)))
1099 return err;
1101 /* main client */
1102 if (data)
1103 kfree(data);
1104 /* subclient */
1105 else
1106 kfree(client);
1108 return 0;
1111 static int
1112 w83793_create_subclient(struct i2c_adapter *adapter,
1113 struct i2c_client *client, int addr,
1114 struct i2c_client **sub_cli)
1116 int err = 0;
1117 struct i2c_client *sub_client;
1119 (*sub_cli) = sub_client =
1120 kzalloc(sizeof(struct i2c_client), GFP_KERNEL);
1121 if (!(sub_client)) {
1122 return -ENOMEM;
1124 sub_client->addr = 0x48 + addr;
1125 i2c_set_clientdata(sub_client, NULL);
1126 sub_client->adapter = adapter;
1127 sub_client->driver = &w83793_driver;
1128 strlcpy(sub_client->name, "w83793 subclient", I2C_NAME_SIZE);
1129 if ((err = i2c_attach_client(sub_client))) {
1130 dev_err(&client->dev, "subclient registration "
1131 "at address 0x%x failed\n", sub_client->addr);
1132 kfree(sub_client);
1134 return err;
1137 static int
1138 w83793_detect_subclients(struct i2c_adapter *adapter, int address,
1139 int kind, struct i2c_client *client)
1141 int i, id, err;
1142 u8 tmp;
1143 struct w83793_data *data = i2c_get_clientdata(client);
1145 id = i2c_adapter_id(adapter);
1146 if (force_subclients[0] == id && force_subclients[1] == address) {
1147 for (i = 2; i <= 3; i++) {
1148 if (force_subclients[i] < 0x48
1149 || force_subclients[i] > 0x4f) {
1150 dev_err(&client->dev,
1151 "invalid subclient "
1152 "address %d; must be 0x48-0x4f\n",
1153 force_subclients[i]);
1154 err = -EINVAL;
1155 goto ERROR_SC_0;
1158 w83793_write_value(client, W83793_REG_I2C_SUBADDR,
1159 (force_subclients[2] & 0x07) |
1160 ((force_subclients[3] & 0x07) << 4));
1163 tmp = w83793_read_value(client, W83793_REG_I2C_SUBADDR);
1164 if (!(tmp & 0x08)) {
1165 err =
1166 w83793_create_subclient(adapter, client, tmp & 0x7,
1167 &data->lm75[0]);
1168 if (err < 0)
1169 goto ERROR_SC_0;
1171 if (!(tmp & 0x80)) {
1172 if ((data->lm75[0] != NULL)
1173 && ((tmp & 0x7) == ((tmp >> 4) & 0x7))) {
1174 dev_err(&client->dev,
1175 "duplicate addresses 0x%x, "
1176 "use force_subclients\n", data->lm75[0]->addr);
1177 err = -ENODEV;
1178 goto ERROR_SC_1;
1180 err = w83793_create_subclient(adapter, client,
1181 (tmp >> 4) & 0x7, &data->lm75[1]);
1182 if (err < 0)
1183 goto ERROR_SC_1;
1186 return 0;
1188 /* Undo inits in case of errors */
1190 ERROR_SC_1:
1191 if (data->lm75[0] != NULL) {
1192 i2c_detach_client(data->lm75[0]);
1193 kfree(data->lm75[0]);
1195 ERROR_SC_0:
1196 return err;
1199 static int w83793_detect(struct i2c_adapter *adapter, int address, int kind)
1201 int i;
1202 u8 tmp, val;
1203 struct i2c_client *client;
1204 struct device *dev;
1205 struct w83793_data *data;
1206 int files_fan = ARRAY_SIZE(w83793_left_fan) / 7;
1207 int files_pwm = ARRAY_SIZE(w83793_left_pwm) / 5;
1208 int files_temp = ARRAY_SIZE(w83793_temp) / 6;
1209 int err = 0;
1211 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA)) {
1212 goto exit;
1215 /* OK. For now, we presume we have a valid client. We now create the
1216 client structure, even though we cannot fill it completely yet.
1217 But it allows us to access w83793_{read,write}_value. */
1219 if (!(data = kzalloc(sizeof(struct w83793_data), GFP_KERNEL))) {
1220 err = -ENOMEM;
1221 goto exit;
1224 client = &data->client;
1225 dev = &client->dev;
1226 i2c_set_clientdata(client, data);
1227 client->addr = address;
1228 client->adapter = adapter;
1229 client->driver = &w83793_driver;
1231 data->bank = i2c_smbus_read_byte_data(client, W83793_REG_BANKSEL);
1233 /* Now, we do the remaining detection. */
1234 if (kind < 0) {
1235 tmp = data->bank & 0x80 ? 0x5c : 0xa3;
1236 /* Check Winbond vendor ID */
1237 if (tmp != i2c_smbus_read_byte_data(client,
1238 W83793_REG_VENDORID)) {
1239 pr_debug("w83793: Detection failed at check "
1240 "vendor id\n");
1241 err = -ENODEV;
1242 goto free_mem;
1245 /* If Winbond chip, address of chip and W83793_REG_I2C_ADDR
1246 should match */
1247 if ((data->bank & 0x07) == 0
1248 && i2c_smbus_read_byte_data(client, W83793_REG_I2C_ADDR) !=
1249 (address << 1)) {
1250 pr_debug("w83793: Detection failed at check "
1251 "i2c addr\n");
1252 err = -ENODEV;
1253 goto free_mem;
1258 /* We have either had a force parameter, or we have already detected the
1259 Winbond. Determine the chip type now */
1261 if (kind <= 0) {
1262 if (0x7b == w83793_read_value(client, W83793_REG_CHIPID)) {
1263 kind = w83793;
1264 } else {
1265 if (kind == 0)
1266 dev_warn(&adapter->dev, "w83793: Ignoring "
1267 "'force' parameter for unknown chip "
1268 "at address 0x%02x\n", address);
1269 err = -ENODEV;
1270 goto free_mem;
1274 /* Fill in the remaining client fields and put into the global list */
1275 strlcpy(client->name, "w83793", I2C_NAME_SIZE);
1277 mutex_init(&data->update_lock);
1279 /* Tell the I2C layer a new client has arrived */
1280 if ((err = i2c_attach_client(client)))
1281 goto free_mem;
1283 if ((err = w83793_detect_subclients(adapter, address, kind, client)))
1284 goto detach_client;
1286 /* Initialize the chip */
1287 w83793_init_client(client);
1289 data->vrm = vid_which_vrm();
1291 Only fan 1-5 has their own input pins,
1292 Pwm 1-3 has their own pins
1294 data->has_fan = 0x1f;
1295 data->has_pwm = 0x07;
1296 tmp = w83793_read_value(client, W83793_REG_MFC);
1297 val = w83793_read_value(client, W83793_REG_FANIN_CTRL);
1299 /* check the function of pins 49-56 */
1300 if (!(tmp & 0x80)) {
1301 data->has_pwm |= 0x18; /* pwm 4,5 */
1302 if (val & 0x01) { /* fan 6 */
1303 data->has_fan |= 0x20;
1304 data->has_pwm |= 0x20;
1306 if (val & 0x02) { /* fan 7 */
1307 data->has_fan |= 0x40;
1308 data->has_pwm |= 0x40;
1310 if (!(tmp & 0x40) && (val & 0x04)) { /* fan 8 */
1311 data->has_fan |= 0x80;
1312 data->has_pwm |= 0x80;
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;
1323 if (0x20 == (tmp & 0x30)) {
1324 if (val & 0x20) /* fan 11 */
1325 data->has_fan |= 0x400;
1326 if (val & 0x40) /* fan 12 */
1327 data->has_fan |= 0x800;
1330 if ((tmp & 0x01) && (val & 0x04)) { /* fan 8, second location */
1331 data->has_fan |= 0x80;
1332 data->has_pwm |= 0x80;
1335 tmp = w83793_read_value(client, W83793_REG_FANIN_SEL);
1336 if ((tmp & 0x01) && (val & 0x08)) { /* fan 9, second location */
1337 data->has_fan |= 0x100;
1339 if ((tmp & 0x02) && (val & 0x10)) { /* fan 10, second location */
1340 data->has_fan |= 0x200;
1342 if ((tmp & 0x04) && (val & 0x20)) { /* fan 11, second location */
1343 data->has_fan |= 0x400;
1345 if ((tmp & 0x08) && (val & 0x40)) { /* fan 12, second location */
1346 data->has_fan |= 0x800;
1349 /* check the temp1-6 mode, ignore former AMDSI selected inputs */
1350 tmp = w83793_read_value(client,W83793_REG_TEMP_MODE[0]);
1351 if (tmp & 0x01)
1352 data->has_temp |= 0x01;
1353 if (tmp & 0x04)
1354 data->has_temp |= 0x02;
1355 if (tmp & 0x10)
1356 data->has_temp |= 0x04;
1357 if (tmp & 0x40)
1358 data->has_temp |= 0x08;
1360 tmp = w83793_read_value(client,W83793_REG_TEMP_MODE[1]);
1361 if (tmp & 0x01)
1362 data->has_temp |= 0x10;
1363 if (tmp & 0x02)
1364 data->has_temp |= 0x20;
1366 /* Detect the VID usage and ignore unused input */
1367 tmp = w83793_read_value(client, W83793_REG_MFC);
1368 if (!(tmp & 0x29))
1369 data->has_vid |= 0x1; /* has VIDA */
1370 if (tmp & 0x80)
1371 data->has_vid |= 0x2; /* has VIDB */
1373 /* Register sysfs hooks */
1374 for (i = 0; i < ARRAY_SIZE(w83793_sensor_attr_2); i++) {
1375 err = device_create_file(dev,
1376 &w83793_sensor_attr_2[i].dev_attr);
1377 if (err)
1378 goto exit_remove;
1381 for (i = 0; i < ARRAY_SIZE(w83793_vid); i++) {
1382 if (!(data->has_vid & (1 << i)))
1383 continue;
1384 err = device_create_file(dev, &w83793_vid[i].dev_attr);
1385 if (err)
1386 goto exit_remove;
1389 for (i = 0; i < ARRAY_SIZE(sda_single_files); i++) {
1390 err = device_create_file(dev, &sda_single_files[i].dev_attr);
1391 if (err)
1392 goto exit_remove;
1396 for (i = 0; i < 6; i++) {
1397 int j;
1398 if (!(data->has_temp & (1 << i)))
1399 continue;
1400 for (j = 0; j < files_temp; j++) {
1401 err = device_create_file(dev,
1402 &w83793_temp[(i) * files_temp
1403 + j].dev_attr);
1404 if (err)
1405 goto exit_remove;
1409 for (i = 5; i < 12; i++) {
1410 int j;
1411 if (!(data->has_fan & (1 << i)))
1412 continue;
1413 for (j = 0; j < files_fan; j++) {
1414 err = device_create_file(dev,
1415 &w83793_left_fan[(i - 5) * files_fan
1416 + j].dev_attr);
1417 if (err)
1418 goto exit_remove;
1422 for (i = 3; i < 8; i++) {
1423 int j;
1424 if (!(data->has_pwm & (1 << i)))
1425 continue;
1426 for (j = 0; j < files_pwm; j++) {
1427 err = device_create_file(dev,
1428 &w83793_left_pwm[(i - 3) * files_pwm
1429 + j].dev_attr);
1430 if (err)
1431 goto exit_remove;
1435 data->hwmon_dev = hwmon_device_register(dev);
1436 if (IS_ERR(data->hwmon_dev)) {
1437 err = PTR_ERR(data->hwmon_dev);
1438 goto exit_remove;
1441 return 0;
1443 /* Unregister sysfs hooks */
1445 exit_remove:
1446 for (i = 0; i < ARRAY_SIZE(w83793_sensor_attr_2); i++)
1447 device_remove_file(dev, &w83793_sensor_attr_2[i].dev_attr);
1449 for (i = 0; i < ARRAY_SIZE(sda_single_files); i++)
1450 device_remove_file(dev, &sda_single_files[i].dev_attr);
1452 for (i = 0; i < ARRAY_SIZE(w83793_vid); i++)
1453 device_remove_file(dev, &w83793_vid[i].dev_attr);
1455 for (i = 0; i < ARRAY_SIZE(w83793_left_fan); i++)
1456 device_remove_file(dev, &w83793_left_fan[i].dev_attr);
1458 for (i = 0; i < ARRAY_SIZE(w83793_left_pwm); i++)
1459 device_remove_file(dev, &w83793_left_pwm[i].dev_attr);
1461 for (i = 0; i < ARRAY_SIZE(w83793_temp); i++)
1462 device_remove_file(dev, &w83793_temp[i].dev_attr);
1464 if (data->lm75[0] != NULL) {
1465 i2c_detach_client(data->lm75[0]);
1466 kfree(data->lm75[0]);
1468 if (data->lm75[1] != NULL) {
1469 i2c_detach_client(data->lm75[1]);
1470 kfree(data->lm75[1]);
1472 detach_client:
1473 i2c_detach_client(client);
1474 free_mem:
1475 kfree(data);
1476 exit:
1477 return err;
1480 static void w83793_update_nonvolatile(struct device *dev)
1482 struct i2c_client *client = to_i2c_client(dev);
1483 struct w83793_data *data = i2c_get_clientdata(client);
1484 int i, j;
1486 They are somewhat "stable" registers, and to update them everytime
1487 takes so much time, it's just not worthy. Update them in a long
1488 interval to avoid exception.
1490 if (!(time_after(jiffies, data->last_nonvolatile + HZ * 300)
1491 || !data->valid))
1492 return;
1493 /* update voltage limits */
1494 for (i = 1; i < 3; i++) {
1495 for (j = 0; j < ARRAY_SIZE(data->in); j++) {
1496 data->in[j][i] =
1497 w83793_read_value(client, W83793_REG_IN[j][i]);
1499 data->in_low_bits[i] =
1500 w83793_read_value(client, W83793_REG_IN_LOW_BITS[i]);
1503 for (i = 0; i < ARRAY_SIZE(data->fan_min); i++) {
1504 /* Update the Fan measured value and limits */
1505 if (!(data->has_fan & (1 << i))) {
1506 continue;
1508 data->fan_min[i] =
1509 w83793_read_value(client, W83793_REG_FAN_MIN(i)) << 8;
1510 data->fan_min[i] |=
1511 w83793_read_value(client, W83793_REG_FAN_MIN(i) + 1);
1514 for (i = 0; i < ARRAY_SIZE(data->temp_fan_map); i++) {
1515 if (!(data->has_temp & (1 << i)))
1516 continue;
1517 data->temp_fan_map[i] =
1518 w83793_read_value(client, W83793_REG_TEMP_FAN_MAP(i));
1519 for (j = 1; j < 5; j++) {
1520 data->temp[i][j] =
1521 w83793_read_value(client, W83793_REG_TEMP[i][j]);
1523 data->temp_cruise[i] =
1524 w83793_read_value(client, W83793_REG_TEMP_CRUISE(i));
1525 for (j = 0; j < 7; j++) {
1526 data->sf2_pwm[i][j] =
1527 w83793_read_value(client, W83793_REG_SF2_PWM(i, j));
1528 data->sf2_temp[i][j] =
1529 w83793_read_value(client,
1530 W83793_REG_SF2_TEMP(i, j));
1534 for (i = 0; i < ARRAY_SIZE(data->temp_mode); i++)
1535 data->temp_mode[i] =
1536 w83793_read_value(client, W83793_REG_TEMP_MODE[i]);
1538 for (i = 0; i < ARRAY_SIZE(data->tolerance); i++) {
1539 data->tolerance[i] =
1540 w83793_read_value(client, W83793_REG_TEMP_TOL(i));
1543 for (i = 0; i < ARRAY_SIZE(data->pwm); i++) {
1544 if (!(data->has_pwm & (1 << i)))
1545 continue;
1546 data->pwm[i][PWM_NONSTOP] =
1547 w83793_read_value(client, W83793_REG_PWM(i, PWM_NONSTOP));
1548 data->pwm[i][PWM_START] =
1549 w83793_read_value(client, W83793_REG_PWM(i, PWM_START));
1550 data->pwm_stop_time[i] =
1551 w83793_read_value(client, W83793_REG_PWM_STOP_TIME(i));
1554 data->pwm_default = w83793_read_value(client, W83793_REG_PWM_DEFAULT);
1555 data->pwm_enable = w83793_read_value(client, W83793_REG_PWM_ENABLE);
1556 data->pwm_uptime = w83793_read_value(client, W83793_REG_PWM_UPTIME);
1557 data->pwm_downtime = w83793_read_value(client, W83793_REG_PWM_DOWNTIME);
1558 data->temp_critical =
1559 w83793_read_value(client, W83793_REG_TEMP_CRITICAL);
1560 data->beep_enable = w83793_read_value(client, W83793_REG_OVT_BEEP);
1562 for (i = 0; i < ARRAY_SIZE(data->beeps); i++) {
1563 data->beeps[i] = w83793_read_value(client, W83793_REG_BEEP(i));
1566 data->last_nonvolatile = jiffies;
1569 static struct w83793_data *w83793_update_device(struct device *dev)
1571 struct i2c_client *client = to_i2c_client(dev);
1572 struct w83793_data *data = i2c_get_clientdata(client);
1573 int i;
1575 mutex_lock(&data->update_lock);
1577 if (!(time_after(jiffies, data->last_updated + HZ * 2)
1578 || !data->valid))
1579 goto END;
1581 /* Update the voltages measured value and limits */
1582 for (i = 0; i < ARRAY_SIZE(data->in); i++)
1583 data->in[i][IN_READ] =
1584 w83793_read_value(client, W83793_REG_IN[i][IN_READ]);
1586 data->in_low_bits[IN_READ] =
1587 w83793_read_value(client, W83793_REG_IN_LOW_BITS[IN_READ]);
1589 for (i = 0; i < ARRAY_SIZE(data->fan); i++) {
1590 if (!(data->has_fan & (1 << i))) {
1591 continue;
1593 data->fan[i] =
1594 w83793_read_value(client, W83793_REG_FAN(i)) << 8;
1595 data->fan[i] |=
1596 w83793_read_value(client, W83793_REG_FAN(i) + 1);
1599 for (i = 0; i < ARRAY_SIZE(data->temp); i++) {
1600 if (!(data->has_temp & (1 << i)))
1601 continue;
1602 data->temp[i][TEMP_READ] =
1603 w83793_read_value(client, W83793_REG_TEMP[i][TEMP_READ]);
1606 data->temp_low_bits =
1607 w83793_read_value(client, W83793_REG_TEMP_LOW_BITS);
1609 for (i = 0; i < ARRAY_SIZE(data->pwm); i++) {
1610 if (data->has_pwm & (1 << i))
1611 data->pwm[i][PWM_DUTY] =
1612 w83793_read_value(client,
1613 W83793_REG_PWM(i, PWM_DUTY));
1616 for (i = 0; i < ARRAY_SIZE(data->alarms); i++)
1617 data->alarms[i] =
1618 w83793_read_value(client, W83793_REG_ALARM(i));
1619 if (data->has_vid & 0x01)
1620 data->vid[0] = w83793_read_value(client, W83793_REG_VID_INA);
1621 if (data->has_vid & 0x02)
1622 data->vid[1] = w83793_read_value(client, W83793_REG_VID_INB);
1623 w83793_update_nonvolatile(dev);
1624 data->last_updated = jiffies;
1625 data->valid = 1;
1627 END:
1628 mutex_unlock(&data->update_lock);
1629 return data;
1632 /* Ignore the possibility that somebody change bank outside the driver
1633 Must be called with data->update_lock held, except during initialization */
1634 static u8 w83793_read_value(struct i2c_client *client, u16 reg)
1636 struct w83793_data *data = i2c_get_clientdata(client);
1637 u8 res = 0xff;
1638 u8 new_bank = reg >> 8;
1640 new_bank |= data->bank & 0xfc;
1641 if (data->bank != new_bank) {
1642 if (i2c_smbus_write_byte_data
1643 (client, W83793_REG_BANKSEL, new_bank) >= 0)
1644 data->bank = new_bank;
1645 else {
1646 dev_err(&client->dev,
1647 "set bank to %d failed, fall back "
1648 "to bank %d, read reg 0x%x error\n",
1649 new_bank, data->bank, reg);
1650 res = 0x0; /* read 0x0 from the chip */
1651 goto END;
1654 res = i2c_smbus_read_byte_data(client, reg & 0xff);
1655 END:
1656 return res;
1659 /* Must be called with data->update_lock held, except during initialization */
1660 static int w83793_write_value(struct i2c_client *client, u16 reg, u8 value)
1662 struct w83793_data *data = i2c_get_clientdata(client);
1663 int res;
1664 u8 new_bank = reg >> 8;
1666 new_bank |= data->bank & 0xfc;
1667 if (data->bank != new_bank) {
1668 if ((res = i2c_smbus_write_byte_data
1669 (client, W83793_REG_BANKSEL, new_bank)) >= 0)
1670 data->bank = new_bank;
1671 else {
1672 dev_err(&client->dev,
1673 "set bank to %d failed, fall back "
1674 "to bank %d, write reg 0x%x error\n",
1675 new_bank, data->bank, reg);
1676 goto END;
1680 res = i2c_smbus_write_byte_data(client, reg & 0xff, value);
1681 END:
1682 return res;
1685 static int __init sensors_w83793_init(void)
1687 return i2c_add_driver(&w83793_driver);
1690 static void __exit sensors_w83793_exit(void)
1692 i2c_del_driver(&w83793_driver);
1695 MODULE_AUTHOR("Yuan Mu");
1696 MODULE_DESCRIPTION("w83793 driver");
1697 MODULE_LICENSE("GPL");
1699 module_init(sensors_w83793_init);
1700 module_exit(sensors_w83793_exit);