[PATCH] hrtimers: fix HRTIMER_CB_IRQSAFE_NO_SOFTIRQ description
[wrt350n-kernel.git] / drivers / hwmon / w83793.c
blob253ffaf1568a7baefdeb86468b3467ae23d5466c
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 static unsigned short normal_i2c[] = { 0x2c, 0x2d, 0x2e, 0x2f, I2C_CLIENT_END };
42 /* Insmod parameters */
43 I2C_CLIENT_INSMOD_1(w83793);
44 I2C_CLIENT_MODULE_PARM(force_subclients, "List of subclient addresses: "
45 "{bus, clientaddr, subclientaddr1, subclientaddr2}");
47 static int reset;
48 module_param(reset, bool, 0);
49 MODULE_PARM_DESC(reset, "Set to 1 to reset chip, not recommended");
52 Address 0x00, 0x0d, 0x0e, 0x0f in all three banks are reserved
53 as ID, Bank Select registers
55 #define W83793_REG_BANKSEL 0x00
56 #define W83793_REG_VENDORID 0x0d
57 #define W83793_REG_CHIPID 0x0e
58 #define W83793_REG_DEVICEID 0x0f
60 #define W83793_REG_CONFIG 0x40
61 #define W83793_REG_MFC 0x58
62 #define W83793_REG_FANIN_CTRL 0x5c
63 #define W83793_REG_FANIN_SEL 0x5d
64 #define W83793_REG_I2C_ADDR 0x0b
65 #define W83793_REG_I2C_SUBADDR 0x0c
66 #define W83793_REG_VID_INA 0x05
67 #define W83793_REG_VID_INB 0x06
68 #define W83793_REG_VID_LATCHA 0x07
69 #define W83793_REG_VID_LATCHB 0x08
70 #define W83793_REG_VID_CTRL 0x59
72 static u16 W83793_REG_TEMP_MODE[2] = { 0x5e, 0x5f };
74 #define TEMP_READ 0
75 #define TEMP_CRIT 1
76 #define TEMP_CRIT_HYST 2
77 #define TEMP_WARN 3
78 #define TEMP_WARN_HYST 4
79 /* only crit and crit_hyst affect real-time alarm status
80 current crit crit_hyst warn warn_hyst */
81 static u16 W83793_REG_TEMP[][5] = {
82 {0x1c, 0x78, 0x79, 0x7a, 0x7b},
83 {0x1d, 0x7c, 0x7d, 0x7e, 0x7f},
84 {0x1e, 0x80, 0x81, 0x82, 0x83},
85 {0x1f, 0x84, 0x85, 0x86, 0x87},
86 {0x20, 0x88, 0x89, 0x8a, 0x8b},
87 {0x21, 0x8c, 0x8d, 0x8e, 0x8f},
90 #define W83793_REG_TEMP_LOW_BITS 0x22
92 #define W83793_REG_BEEP(index) (0x53 + (index))
93 #define W83793_REG_ALARM(index) (0x4b + (index))
95 #define W83793_REG_CLR_CHASSIS 0x4a /* SMI MASK4 */
96 #define W83793_REG_IRQ_CTRL 0x50
97 #define W83793_REG_OVT_CTRL 0x51
98 #define W83793_REG_OVT_BEEP 0x52
100 #define IN_READ 0
101 #define IN_MAX 1
102 #define IN_LOW 2
103 static const u16 W83793_REG_IN[][3] = {
104 /* Current, High, Low */
105 {0x10, 0x60, 0x61}, /* Vcore A */
106 {0x11, 0x62, 0x63}, /* Vcore B */
107 {0x12, 0x64, 0x65}, /* Vtt */
108 {0x14, 0x6a, 0x6b}, /* VSEN1 */
109 {0x15, 0x6c, 0x6d}, /* VSEN2 */
110 {0x16, 0x6e, 0x6f}, /* +3VSEN */
111 {0x17, 0x70, 0x71}, /* +12VSEN */
112 {0x18, 0x72, 0x73}, /* 5VDD */
113 {0x19, 0x74, 0x75}, /* 5VSB */
114 {0x1a, 0x76, 0x77}, /* VBAT */
117 /* Low Bits of Vcore A/B Vtt Read/High/Low */
118 static const u16 W83793_REG_IN_LOW_BITS[] = { 0x1b, 0x68, 0x69 };
119 static u8 scale_in[] = { 2, 2, 2, 16, 16, 16, 8, 24, 24, 16 };
120 static u8 scale_in_add[] = { 0, 0, 0, 0, 0, 0, 0, 150, 150, 0 };
122 #define W83793_REG_FAN(index) (0x23 + 2 * (index)) /* High byte */
123 #define W83793_REG_FAN_MIN(index) (0x90 + 2 * (index)) /* High byte */
125 #define W83793_REG_PWM_DEFAULT 0xb2
126 #define W83793_REG_PWM_ENABLE 0x207
127 #define W83793_REG_PWM_UPTIME 0xc3 /* Unit in 0.1 second */
128 #define W83793_REG_PWM_DOWNTIME 0xc4 /* Unit in 0.1 second */
129 #define W83793_REG_TEMP_CRITICAL 0xc5
131 #define PWM_DUTY 0
132 #define PWM_START 1
133 #define PWM_NONSTOP 2
134 #define W83793_REG_PWM(index, nr) (((nr) == 0 ? 0xb3 : \
135 (nr) == 1 ? 0x220 : 0x218) + (index))
137 /* bit field, fan1 is bit0, fan2 is bit1 ... */
138 #define W83793_REG_TEMP_FAN_MAP(index) (0x201 + (index))
139 #define W83793_REG_TEMP_TOL(index) (0x208 + (index))
140 #define W83793_REG_TEMP_CRUISE(index) (0x210 + (index))
141 #define W83793_REG_PWM_STOP_TIME(index) (0x228 + (index))
142 #define W83793_REG_SF2_TEMP(index, nr) (0x230 + ((index) << 4) + (nr))
143 #define W83793_REG_SF2_PWM(index, nr) (0x238 + ((index) << 4) + (nr))
145 static inline unsigned long FAN_FROM_REG(u16 val)
147 if ((val >= 0xfff) || (val == 0))
148 return 0;
149 return (1350000UL / val);
152 static inline u16 FAN_TO_REG(long rpm)
154 if (rpm <= 0)
155 return 0x0fff;
156 return SENSORS_LIMIT((1350000 + (rpm >> 1)) / rpm, 1, 0xffe);
159 static inline unsigned long TIME_FROM_REG(u8 reg)
161 return (reg * 100);
164 static inline u8 TIME_TO_REG(unsigned long val)
166 return SENSORS_LIMIT((val + 50) / 100, 0, 0xff);
169 static inline long TEMP_FROM_REG(s8 reg)
171 return (reg * 1000);
174 static inline s8 TEMP_TO_REG(long val, s8 min, s8 max)
176 return SENSORS_LIMIT((val + (val < 0 ? -500 : 500)) / 1000, min, max);
179 struct w83793_data {
180 struct i2c_client client;
181 struct i2c_client *lm75[2];
182 struct class_device *class_dev;
183 struct mutex update_lock;
184 char valid; /* !=0 if following fields are valid */
185 unsigned long last_updated; /* In jiffies */
186 unsigned long last_nonvolatile; /* In jiffies, last time we update the
187 nonvolatile registers */
189 u8 bank;
190 u8 vrm;
191 u8 vid[2];
192 u8 in[10][3]; /* Register value, read/high/low */
193 u8 in_low_bits[3]; /* Additional resolution for VCore A/B Vtt */
195 u16 has_fan; /* Only fan1- fan5 has own pins */
196 u16 fan[12]; /* Register value combine */
197 u16 fan_min[12]; /* Register value combine */
199 s8 temp[6][5]; /* current, crit, crit_hyst,warn, warn_hyst */
200 u8 temp_low_bits; /* Additional resolution TD1-TD4 */
201 u8 temp_mode[2]; /* byte 0: Temp D1-D4 mode each has 2 bits
202 byte 1: Temp R1,R2 mode, each has 1 bit */
203 u8 temp_critical; /* If reached all fan will be at full speed */
204 u8 temp_fan_map[6]; /* Temp controls which pwm fan, bit field */
206 u8 has_pwm;
207 u8 has_temp;
208 u8 has_vid;
209 u8 pwm_enable; /* Register value, each Temp has 1 bit */
210 u8 pwm_uptime; /* Register value */
211 u8 pwm_downtime; /* Register value */
212 u8 pwm_default; /* All fan default pwm, next poweron valid */
213 u8 pwm[8][3]; /* Register value */
214 u8 pwm_stop_time[8];
215 u8 temp_cruise[6];
217 u8 alarms[5]; /* realtime status registers */
218 u8 beeps[5];
219 u8 beep_enable;
220 u8 tolerance[3]; /* Temp tolerance(Smart Fan I/II) */
221 u8 sf2_pwm[6][7]; /* Smart FanII: Fan duty cycle */
222 u8 sf2_temp[6][7]; /* Smart FanII: Temp level point */
225 static u8 w83793_read_value(struct i2c_client *client, u16 reg);
226 static int w83793_write_value(struct i2c_client *client, u16 reg, u8 value);
227 static int w83793_attach_adapter(struct i2c_adapter *adapter);
228 static int w83793_detect(struct i2c_adapter *adapter, int address, int kind);
229 static int w83793_detach_client(struct i2c_client *client);
230 static void w83793_init_client(struct i2c_client *client);
231 static void w83793_update_nonvolatile(struct device *dev);
232 static struct w83793_data *w83793_update_device(struct device *dev);
234 static struct i2c_driver w83793_driver = {
235 .driver = {
236 .name = "w83793",
238 .attach_adapter = w83793_attach_adapter,
239 .detach_client = w83793_detach_client,
242 static ssize_t
243 show_vrm(struct device *dev, struct device_attribute *attr, char *buf)
245 struct i2c_client *client = to_i2c_client(dev);
246 struct w83793_data *data = i2c_get_clientdata(client);
248 return sprintf(buf, "%d\n", data->vrm);
251 static ssize_t
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));
262 static ssize_t
263 store_vrm(struct device *dev, struct device_attribute *attr,
264 const char *buf, size_t count)
266 struct i2c_client *client = to_i2c_client(dev);
267 struct w83793_data *data = i2c_get_clientdata(client);
269 data->vrm = simple_strtoul(buf, NULL, 10);
270 return count;
273 #define ALARM_STATUS 0
274 #define BEEP_ENABLE 1
275 static ssize_t
276 show_alarm_beep(struct device *dev, struct device_attribute *attr, char *buf)
278 struct w83793_data *data = w83793_update_device(dev);
279 struct sensor_device_attribute_2 *sensor_attr =
280 to_sensor_dev_attr_2(attr);
281 int nr = sensor_attr->nr;
282 int index = sensor_attr->index >> 3;
283 int bit = sensor_attr->index & 0x07;
284 u8 val;
286 if (ALARM_STATUS == nr) {
287 val = (data->alarms[index] >> (bit)) & 1;
288 } else { /* BEEP_ENABLE */
289 val = (data->beeps[index] >> (bit)) & 1;
292 return sprintf(buf, "%u\n", val);
295 static ssize_t
296 store_beep(struct device *dev, struct device_attribute *attr,
297 const char *buf, size_t count)
299 struct i2c_client *client = to_i2c_client(dev);
300 struct w83793_data *data = i2c_get_clientdata(client);
301 struct sensor_device_attribute_2 *sensor_attr =
302 to_sensor_dev_attr_2(attr);
303 int index = sensor_attr->index >> 3;
304 int shift = sensor_attr->index & 0x07;
305 u8 beep_bit = 1 << shift;
306 u8 val;
308 val = simple_strtoul(buf, NULL, 10);
309 if (val != 0 && val != 1)
310 return -EINVAL;
312 mutex_lock(&data->update_lock);
313 data->beeps[index] = w83793_read_value(client, W83793_REG_BEEP(index));
314 data->beeps[index] &= ~beep_bit;
315 data->beeps[index] |= val << shift;
316 w83793_write_value(client, W83793_REG_BEEP(index), data->beeps[index]);
317 mutex_unlock(&data->update_lock);
319 return count;
322 static ssize_t
323 show_beep_enable(struct device *dev, struct device_attribute *attr, char *buf)
325 struct w83793_data *data = w83793_update_device(dev);
326 return sprintf(buf, "%u\n", (data->beep_enable >> 1) & 0x01);
329 static ssize_t
330 store_beep_enable(struct device *dev, struct device_attribute *attr,
331 const char *buf, size_t count)
333 struct i2c_client *client = to_i2c_client(dev);
334 struct w83793_data *data = i2c_get_clientdata(client);
335 u8 val = simple_strtoul(buf, NULL, 10);
337 if (val != 0 && val != 1)
338 return -EINVAL;
340 mutex_lock(&data->update_lock);
341 data->beep_enable = w83793_read_value(client, W83793_REG_OVT_BEEP)
342 & 0xfd;
343 data->beep_enable |= val << 1;
344 w83793_write_value(client, W83793_REG_OVT_BEEP, data->beep_enable);
345 mutex_unlock(&data->update_lock);
347 return count;
350 /* Write any value to clear chassis alarm */
351 static ssize_t
352 store_chassis_clear(struct device *dev,
353 struct device_attribute *attr, const char *buf,
354 size_t count)
356 struct i2c_client *client = to_i2c_client(dev);
357 struct w83793_data *data = i2c_get_clientdata(client);
358 u8 val;
360 mutex_lock(&data->update_lock);
361 val = w83793_read_value(client, W83793_REG_CLR_CHASSIS);
362 val |= 0x80;
363 w83793_write_value(client, W83793_REG_CLR_CHASSIS, val);
364 mutex_unlock(&data->update_lock);
365 return count;
368 #define FAN_INPUT 0
369 #define FAN_MIN 1
370 static ssize_t
371 show_fan(struct device *dev, struct device_attribute *attr, char *buf)
373 struct sensor_device_attribute_2 *sensor_attr =
374 to_sensor_dev_attr_2(attr);
375 int nr = sensor_attr->nr;
376 int index = sensor_attr->index;
377 struct w83793_data *data = w83793_update_device(dev);
378 u16 val;
380 if (FAN_INPUT == nr) {
381 val = data->fan[index] & 0x0fff;
382 } else {
383 val = data->fan_min[index] & 0x0fff;
386 return sprintf(buf, "%lu\n", FAN_FROM_REG(val));
389 static ssize_t
390 store_fan_min(struct device *dev, struct device_attribute *attr,
391 const char *buf, size_t count)
393 struct sensor_device_attribute_2 *sensor_attr =
394 to_sensor_dev_attr_2(attr);
395 int index = sensor_attr->index;
396 struct i2c_client *client = to_i2c_client(dev);
397 struct w83793_data *data = i2c_get_clientdata(client);
398 u16 val = FAN_TO_REG(simple_strtoul(buf, NULL, 10));
400 mutex_lock(&data->update_lock);
401 data->fan_min[index] = val;
402 w83793_write_value(client, W83793_REG_FAN_MIN(index),
403 (val >> 8) & 0xff);
404 w83793_write_value(client, W83793_REG_FAN_MIN(index) + 1, val & 0xff);
405 mutex_unlock(&data->update_lock);
407 return count;
410 #define PWM_DUTY 0
411 #define PWM_START 1
412 #define PWM_NONSTOP 2
413 #define PWM_STOP_TIME 3
414 static ssize_t
415 show_pwm(struct device *dev, struct device_attribute *attr, char *buf)
417 struct sensor_device_attribute_2 *sensor_attr =
418 to_sensor_dev_attr_2(attr);
419 struct w83793_data *data = w83793_update_device(dev);
420 u16 val;
421 int nr = sensor_attr->nr;
422 int index = sensor_attr->index;
424 if (PWM_STOP_TIME == nr)
425 val = TIME_FROM_REG(data->pwm_stop_time[index]);
426 else
427 val = (data->pwm[index][nr] & 0x3f) << 2;
429 return sprintf(buf, "%d\n", val);
432 static ssize_t
433 store_pwm(struct device *dev, struct device_attribute *attr,
434 const char *buf, size_t count)
436 struct i2c_client *client = to_i2c_client(dev);
437 struct w83793_data *data = i2c_get_clientdata(client);
438 struct sensor_device_attribute_2 *sensor_attr =
439 to_sensor_dev_attr_2(attr);
440 int nr = sensor_attr->nr;
441 int index = sensor_attr->index;
442 u8 val;
444 mutex_lock(&data->update_lock);
445 if (PWM_STOP_TIME == nr) {
446 val = TIME_TO_REG(simple_strtoul(buf, NULL, 10));
447 data->pwm_stop_time[index] = val;
448 w83793_write_value(client, W83793_REG_PWM_STOP_TIME(index),
449 val);
450 } else {
451 val = SENSORS_LIMIT(simple_strtoul(buf, NULL, 10), 0, 0xff)
452 >> 2;
453 data->pwm[index][nr] =
454 w83793_read_value(client, W83793_REG_PWM(index, nr)) & 0xc0;
455 data->pwm[index][nr] |= val;
456 w83793_write_value(client, W83793_REG_PWM(index, nr),
457 data->pwm[index][nr]);
460 mutex_unlock(&data->update_lock);
461 return count;
464 static ssize_t
465 show_temp(struct device *dev, struct device_attribute *attr, char *buf)
467 struct sensor_device_attribute_2 *sensor_attr =
468 to_sensor_dev_attr_2(attr);
469 int nr = sensor_attr->nr;
470 int index = sensor_attr->index;
471 struct w83793_data *data = w83793_update_device(dev);
472 long temp = TEMP_FROM_REG(data->temp[index][nr]);
474 if (TEMP_READ == nr && index < 4) { /* Only TD1-TD4 have low bits */
475 int low = ((data->temp_low_bits >> (index * 2)) & 0x03) * 250;
476 temp += temp > 0 ? low : -low;
478 return sprintf(buf, "%ld\n", temp);
481 static ssize_t
482 store_temp(struct device *dev, struct device_attribute *attr,
483 const char *buf, size_t count)
485 struct sensor_device_attribute_2 *sensor_attr =
486 to_sensor_dev_attr_2(attr);
487 int nr = sensor_attr->nr;
488 int index = sensor_attr->index;
489 struct i2c_client *client = to_i2c_client(dev);
490 struct w83793_data *data = i2c_get_clientdata(client);
491 long tmp = simple_strtol(buf, NULL, 10);
493 mutex_lock(&data->update_lock);
494 data->temp[index][nr] = TEMP_TO_REG(tmp, -128, 127);
495 w83793_write_value(client, W83793_REG_TEMP[index][nr],
496 data->temp[index][nr]);
497 mutex_unlock(&data->update_lock);
498 return count;
502 TD1-TD4
503 each has 4 mode:(2 bits)
504 0: Stop monitor
505 1: Use internal temp sensor(default)
506 2: Reserved
507 3: Use sensor in Intel CPU and get result by PECI
509 TR1-TR2
510 each has 2 mode:(1 bit)
511 0: Disable temp sensor monitor
512 1: To enable temp sensors monitor
515 /* 0 disable, 6 PECI */
516 static u8 TO_TEMP_MODE[] = { 0, 0, 0, 6 };
518 static ssize_t
519 show_temp_mode(struct device *dev, struct device_attribute *attr, char *buf)
521 struct w83793_data *data = w83793_update_device(dev);
522 struct sensor_device_attribute_2 *sensor_attr =
523 to_sensor_dev_attr_2(attr);
524 int index = sensor_attr->index;
525 u8 mask = (index < 4) ? 0x03 : 0x01;
526 u8 shift = (index < 4) ? (2 * index) : (index - 4);
527 u8 tmp;
528 index = (index < 4) ? 0 : 1;
530 tmp = (data->temp_mode[index] >> shift) & mask;
532 /* for the internal sensor, found out if diode or thermistor */
533 if (tmp == 1) {
534 tmp = index == 0 ? 3 : 4;
535 } else {
536 tmp = TO_TEMP_MODE[tmp];
539 return sprintf(buf, "%d\n", tmp);
542 static ssize_t
543 store_temp_mode(struct device *dev, struct device_attribute *attr,
544 const char *buf, size_t count)
546 struct i2c_client *client = to_i2c_client(dev);
547 struct w83793_data *data = i2c_get_clientdata(client);
548 struct sensor_device_attribute_2 *sensor_attr =
549 to_sensor_dev_attr_2(attr);
550 int index = sensor_attr->index;
551 u8 mask = (index < 4) ? 0x03 : 0x01;
552 u8 shift = (index < 4) ? (2 * index) : (index - 4);
553 u8 val = simple_strtoul(buf, NULL, 10);
555 /* transform the sysfs interface values into table above */
556 if ((val == 6) && (index < 4)) {
557 val -= 3;
558 } else if ((val == 3 && index < 4)
559 || (val == 4 && index >= 4)) {
560 /* transform diode or thermistor into internal enable */
561 val = !!val;
562 } else {
563 return -EINVAL;
566 index = (index < 4) ? 0 : 1;
567 mutex_lock(&data->update_lock);
568 data->temp_mode[index] =
569 w83793_read_value(client, W83793_REG_TEMP_MODE[index]);
570 data->temp_mode[index] &= ~(mask << shift);
571 data->temp_mode[index] |= val << shift;
572 w83793_write_value(client, W83793_REG_TEMP_MODE[index],
573 data->temp_mode[index]);
574 mutex_unlock(&data->update_lock);
576 return count;
579 #define SETUP_PWM_DEFAULT 0
580 #define SETUP_PWM_UPTIME 1 /* Unit in 0.1s */
581 #define SETUP_PWM_DOWNTIME 2 /* Unit in 0.1s */
582 #define SETUP_TEMP_CRITICAL 3
583 static ssize_t
584 show_sf_setup(struct device *dev, struct device_attribute *attr, char *buf)
586 struct sensor_device_attribute_2 *sensor_attr =
587 to_sensor_dev_attr_2(attr);
588 int nr = sensor_attr->nr;
589 struct w83793_data *data = w83793_update_device(dev);
590 u32 val = 0;
592 if (SETUP_PWM_DEFAULT == nr) {
593 val = (data->pwm_default & 0x3f) << 2;
594 } else if (SETUP_PWM_UPTIME == nr) {
595 val = TIME_FROM_REG(data->pwm_uptime);
596 } else if (SETUP_PWM_DOWNTIME == nr) {
597 val = TIME_FROM_REG(data->pwm_downtime);
598 } else if (SETUP_TEMP_CRITICAL == nr) {
599 val = TEMP_FROM_REG(data->temp_critical & 0x7f);
602 return sprintf(buf, "%d\n", val);
605 static ssize_t
606 store_sf_setup(struct device *dev, struct device_attribute *attr,
607 const char *buf, size_t count)
609 struct sensor_device_attribute_2 *sensor_attr =
610 to_sensor_dev_attr_2(attr);
611 int nr = sensor_attr->nr;
612 struct i2c_client *client = to_i2c_client(dev);
613 struct w83793_data *data = i2c_get_clientdata(client);
615 mutex_lock(&data->update_lock);
616 if (SETUP_PWM_DEFAULT == nr) {
617 data->pwm_default =
618 w83793_read_value(client, W83793_REG_PWM_DEFAULT) & 0xc0;
619 data->pwm_default |= SENSORS_LIMIT(simple_strtoul(buf, NULL,
620 10),
621 0, 0xff) >> 2;
622 w83793_write_value(client, W83793_REG_PWM_DEFAULT,
623 data->pwm_default);
624 } else if (SETUP_PWM_UPTIME == nr) {
625 data->pwm_uptime = TIME_TO_REG(simple_strtoul(buf, NULL, 10));
626 data->pwm_uptime += data->pwm_uptime == 0 ? 1 : 0;
627 w83793_write_value(client, W83793_REG_PWM_UPTIME,
628 data->pwm_uptime);
629 } else if (SETUP_PWM_DOWNTIME == nr) {
630 data->pwm_downtime = TIME_TO_REG(simple_strtoul(buf, NULL, 10));
631 data->pwm_downtime += data->pwm_downtime == 0 ? 1 : 0;
632 w83793_write_value(client, W83793_REG_PWM_DOWNTIME,
633 data->pwm_downtime);
634 } else { /* SETUP_TEMP_CRITICAL */
635 data->temp_critical =
636 w83793_read_value(client, W83793_REG_TEMP_CRITICAL) & 0x80;
637 data->temp_critical |= TEMP_TO_REG(simple_strtol(buf, NULL, 10),
638 0, 0x7f);
639 w83793_write_value(client, W83793_REG_TEMP_CRITICAL,
640 data->temp_critical);
643 mutex_unlock(&data->update_lock);
644 return count;
648 Temp SmartFan control
649 TEMP_FAN_MAP
650 Temp channel control which pwm fan, bitfield, bit 0 indicate pwm1...
651 It's possible two or more temp channels control the same fan, w83793
652 always prefers to pick the most critical request and applies it to
653 the related Fan.
654 It's possible one fan is not in any mapping of 6 temp channels, this
655 means the fan is manual mode
657 TEMP_PWM_ENABLE
658 Each temp channel has its own SmartFan mode, and temp channel
659 control fans that are set by TEMP_FAN_MAP
660 0: SmartFanII mode
661 1: Thermal Cruise Mode
663 TEMP_CRUISE
664 Target temperature in thermal cruise mode, w83793 will try to turn
665 fan speed to keep the temperature of target device around this
666 temperature.
668 TEMP_TOLERANCE
669 If Temp higher or lower than target with this tolerance, w83793
670 will take actions to speed up or slow down the fan to keep the
671 temperature within the tolerance range.
674 #define TEMP_FAN_MAP 0
675 #define TEMP_PWM_ENABLE 1
676 #define TEMP_CRUISE 2
677 #define TEMP_TOLERANCE 3
678 static ssize_t
679 show_sf_ctrl(struct device *dev, struct device_attribute *attr, char *buf)
681 struct sensor_device_attribute_2 *sensor_attr =
682 to_sensor_dev_attr_2(attr);
683 int nr = sensor_attr->nr;
684 int index = sensor_attr->index;
685 struct w83793_data *data = w83793_update_device(dev);
686 u32 val;
688 if (TEMP_FAN_MAP == nr) {
689 val = data->temp_fan_map[index];
690 } else if (TEMP_PWM_ENABLE == nr) {
691 /* +2 to transfrom into 2 and 3 to conform with sysfs intf */
692 val = ((data->pwm_enable >> index) & 0x01) + 2;
693 } else if (TEMP_CRUISE == nr) {
694 val = TEMP_FROM_REG(data->temp_cruise[index] & 0x7f);
695 } else { /* TEMP_TOLERANCE */
696 val = data->tolerance[index >> 1] >> ((index & 0x01) ? 4 : 0);
697 val = TEMP_FROM_REG(val & 0x0f);
699 return sprintf(buf, "%d\n", val);
702 static ssize_t
703 store_sf_ctrl(struct device *dev, struct device_attribute *attr,
704 const char *buf, size_t count)
706 struct sensor_device_attribute_2 *sensor_attr =
707 to_sensor_dev_attr_2(attr);
708 int nr = sensor_attr->nr;
709 int index = sensor_attr->index;
710 struct i2c_client *client = to_i2c_client(dev);
711 struct w83793_data *data = i2c_get_clientdata(client);
712 u32 val;
714 mutex_lock(&data->update_lock);
715 if (TEMP_FAN_MAP == nr) {
716 val = simple_strtoul(buf, NULL, 10) & 0xff;
717 w83793_write_value(client, W83793_REG_TEMP_FAN_MAP(index), val);
718 data->temp_fan_map[index] = val;
719 } else if (TEMP_PWM_ENABLE == nr) {
720 val = simple_strtoul(buf, NULL, 10);
721 if (2 == val || 3 == val) {
722 data->pwm_enable =
723 w83793_read_value(client, W83793_REG_PWM_ENABLE);
724 if (val - 2)
725 data->pwm_enable |= 1 << index;
726 else
727 data->pwm_enable &= ~(1 << index);
728 w83793_write_value(client, W83793_REG_PWM_ENABLE,
729 data->pwm_enable);
730 } else {
731 mutex_unlock(&data->update_lock);
732 return -EINVAL;
734 } else if (TEMP_CRUISE == nr) {
735 data->temp_cruise[index] =
736 w83793_read_value(client, W83793_REG_TEMP_CRUISE(index));
737 val = TEMP_TO_REG(simple_strtol(buf, NULL, 10), 0, 0x7f);
738 data->temp_cruise[index] &= 0x80;
739 data->temp_cruise[index] |= val;
741 w83793_write_value(client, W83793_REG_TEMP_CRUISE(index),
742 data->temp_cruise[index]);
743 } else { /* TEMP_TOLERANCE */
744 int i = index >> 1;
745 u8 shift = (index & 0x01) ? 4 : 0;
746 data->tolerance[i] =
747 w83793_read_value(client, W83793_REG_TEMP_TOL(i));
749 val = TEMP_TO_REG(simple_strtol(buf, NULL, 10), 0, 0x0f);
750 data->tolerance[i] &= ~(0x0f << shift);
751 data->tolerance[i] |= val << shift;
752 w83793_write_value(client, W83793_REG_TEMP_TOL(i),
753 data->tolerance[i]);
756 mutex_unlock(&data->update_lock);
757 return count;
760 static ssize_t
761 show_sf2_pwm(struct device *dev, struct device_attribute *attr, char *buf)
763 struct sensor_device_attribute_2 *sensor_attr =
764 to_sensor_dev_attr_2(attr);
765 int nr = sensor_attr->nr;
766 int index = sensor_attr->index;
767 struct w83793_data *data = w83793_update_device(dev);
769 return sprintf(buf, "%d\n", (data->sf2_pwm[index][nr] & 0x3f) << 2);
772 static ssize_t
773 store_sf2_pwm(struct device *dev, struct device_attribute *attr,
774 const char *buf, size_t count)
776 struct i2c_client *client = to_i2c_client(dev);
777 struct w83793_data *data = i2c_get_clientdata(client);
778 struct sensor_device_attribute_2 *sensor_attr =
779 to_sensor_dev_attr_2(attr);
780 int nr = sensor_attr->nr;
781 int index = sensor_attr->index;
782 u8 val = SENSORS_LIMIT(simple_strtoul(buf, NULL, 10), 0, 0xff) >> 2;
784 mutex_lock(&data->update_lock);
785 data->sf2_pwm[index][nr] =
786 w83793_read_value(client, W83793_REG_SF2_PWM(index, nr)) & 0xc0;
787 data->sf2_pwm[index][nr] |= val;
788 w83793_write_value(client, W83793_REG_SF2_PWM(index, nr),
789 data->sf2_pwm[index][nr]);
790 mutex_unlock(&data->update_lock);
791 return count;
794 static ssize_t
795 show_sf2_temp(struct device *dev, struct device_attribute *attr, char *buf)
797 struct sensor_device_attribute_2 *sensor_attr =
798 to_sensor_dev_attr_2(attr);
799 int nr = sensor_attr->nr;
800 int index = sensor_attr->index;
801 struct w83793_data *data = w83793_update_device(dev);
803 return sprintf(buf, "%ld\n",
804 TEMP_FROM_REG(data->sf2_temp[index][nr] & 0x7f));
807 static ssize_t
808 store_sf2_temp(struct device *dev, struct device_attribute *attr,
809 const char *buf, size_t count)
811 struct i2c_client *client = to_i2c_client(dev);
812 struct w83793_data *data = i2c_get_clientdata(client);
813 struct sensor_device_attribute_2 *sensor_attr =
814 to_sensor_dev_attr_2(attr);
815 int nr = sensor_attr->nr;
816 int index = sensor_attr->index;
817 u8 val = TEMP_TO_REG(simple_strtol(buf, NULL, 10), 0, 0x7f);
819 mutex_lock(&data->update_lock);
820 data->sf2_temp[index][nr] =
821 w83793_read_value(client, W83793_REG_SF2_TEMP(index, nr)) & 0x80;
822 data->sf2_temp[index][nr] |= val;
823 w83793_write_value(client, W83793_REG_SF2_TEMP(index, nr),
824 data->sf2_temp[index][nr]);
825 mutex_unlock(&data->update_lock);
826 return count;
829 /* only Vcore A/B and Vtt have additional 2 bits precision */
830 static ssize_t
831 show_in(struct device *dev, struct device_attribute *attr, char *buf)
833 struct sensor_device_attribute_2 *sensor_attr =
834 to_sensor_dev_attr_2(attr);
835 int nr = sensor_attr->nr;
836 int index = sensor_attr->index;
837 struct w83793_data *data = w83793_update_device(dev);
838 u16 val = data->in[index][nr];
840 if (index < 3) {
841 val <<= 2;
842 val += (data->in_low_bits[nr] >> (index * 2)) & 0x3;
844 /* voltage inputs 5VDD and 5VSB needs 150mV offset */
845 val = val * scale_in[index] + scale_in_add[index];
846 return sprintf(buf, "%d\n", val);
849 static ssize_t
850 store_in(struct device *dev, struct device_attribute *attr,
851 const char *buf, size_t count)
853 struct sensor_device_attribute_2 *sensor_attr =
854 to_sensor_dev_attr_2(attr);
855 int nr = sensor_attr->nr;
856 int index = sensor_attr->index;
857 struct i2c_client *client = to_i2c_client(dev);
858 struct w83793_data *data = i2c_get_clientdata(client);
859 u32 val;
861 val =
862 (simple_strtoul(buf, NULL, 10) +
863 scale_in[index] / 2) / scale_in[index];
864 mutex_lock(&data->update_lock);
865 if (index > 2) {
866 /* fix the limit values of 5VDD and 5VSB to ALARM mechanism */
867 if (1 == nr || 2 == nr) {
868 val -= scale_in_add[index] / scale_in[index];
870 val = SENSORS_LIMIT(val, 0, 255);
871 } else {
872 val = SENSORS_LIMIT(val, 0, 0x3FF);
873 data->in_low_bits[nr] =
874 w83793_read_value(client, W83793_REG_IN_LOW_BITS[nr]);
875 data->in_low_bits[nr] &= ~(0x03 << (2 * index));
876 data->in_low_bits[nr] |= (val & 0x03) << (2 * index);
877 w83793_write_value(client, W83793_REG_IN_LOW_BITS[nr],
878 data->in_low_bits[nr]);
879 val >>= 2;
881 data->in[index][nr] = val;
882 w83793_write_value(client, W83793_REG_IN[index][nr],
883 data->in[index][nr]);
884 mutex_unlock(&data->update_lock);
885 return count;
888 #define NOT_USED -1
890 #define SENSOR_ATTR_IN(index) \
891 SENSOR_ATTR_2(in##index##_input, S_IRUGO, show_in, NULL, \
892 IN_READ, index), \
893 SENSOR_ATTR_2(in##index##_max, S_IRUGO | S_IWUSR, show_in, \
894 store_in, IN_MAX, index), \
895 SENSOR_ATTR_2(in##index##_min, S_IRUGO | S_IWUSR, show_in, \
896 store_in, IN_LOW, index), \
897 SENSOR_ATTR_2(in##index##_alarm, S_IRUGO, show_alarm_beep, \
898 NULL, ALARM_STATUS, index + ((index > 2) ? 1 : 0)), \
899 SENSOR_ATTR_2(in##index##_beep, S_IWUSR | S_IRUGO, \
900 show_alarm_beep, store_beep, BEEP_ENABLE, \
901 index + ((index > 2) ? 1 : 0))
903 #define SENSOR_ATTR_FAN(index) \
904 SENSOR_ATTR_2(fan##index##_alarm, S_IRUGO, show_alarm_beep, \
905 NULL, ALARM_STATUS, index + 17), \
906 SENSOR_ATTR_2(fan##index##_beep, S_IWUSR | S_IRUGO, \
907 show_alarm_beep, store_beep, BEEP_ENABLE, index + 17), \
908 SENSOR_ATTR_2(fan##index##_input, S_IRUGO, show_fan, \
909 NULL, FAN_INPUT, index - 1), \
910 SENSOR_ATTR_2(fan##index##_min, S_IWUSR | S_IRUGO, \
911 show_fan, store_fan_min, FAN_MIN, index - 1)
913 #define SENSOR_ATTR_PWM(index) \
914 SENSOR_ATTR_2(pwm##index, S_IWUSR | S_IRUGO, show_pwm, \
915 store_pwm, PWM_DUTY, index - 1), \
916 SENSOR_ATTR_2(pwm##index##_nonstop, S_IWUSR | S_IRUGO, \
917 show_pwm, store_pwm, PWM_NONSTOP, index - 1), \
918 SENSOR_ATTR_2(pwm##index##_start, S_IWUSR | S_IRUGO, \
919 show_pwm, store_pwm, PWM_START, index - 1), \
920 SENSOR_ATTR_2(pwm##index##_stop_time, S_IWUSR | S_IRUGO, \
921 show_pwm, store_pwm, PWM_STOP_TIME, index - 1)
923 #define SENSOR_ATTR_TEMP(index) \
924 SENSOR_ATTR_2(temp##index##_type, S_IRUGO | S_IWUSR, \
925 show_temp_mode, store_temp_mode, NOT_USED, index - 1), \
926 SENSOR_ATTR_2(temp##index##_input, S_IRUGO, show_temp, \
927 NULL, TEMP_READ, index - 1), \
928 SENSOR_ATTR_2(temp##index##_max, S_IRUGO | S_IWUSR, show_temp, \
929 store_temp, TEMP_CRIT, index - 1), \
930 SENSOR_ATTR_2(temp##index##_max_hyst, S_IRUGO | S_IWUSR, \
931 show_temp, store_temp, TEMP_CRIT_HYST, index - 1), \
932 SENSOR_ATTR_2(temp##index##_warn, S_IRUGO | S_IWUSR, show_temp, \
933 store_temp, TEMP_WARN, index - 1), \
934 SENSOR_ATTR_2(temp##index##_warn_hyst, S_IRUGO | S_IWUSR, \
935 show_temp, store_temp, TEMP_WARN_HYST, index - 1), \
936 SENSOR_ATTR_2(temp##index##_alarm, S_IRUGO, \
937 show_alarm_beep, NULL, ALARM_STATUS, index + 11), \
938 SENSOR_ATTR_2(temp##index##_beep, S_IWUSR | S_IRUGO, \
939 show_alarm_beep, store_beep, BEEP_ENABLE, index + 11), \
940 SENSOR_ATTR_2(temp##index##_auto_channels_pwm, \
941 S_IRUGO | S_IWUSR, show_sf_ctrl, store_sf_ctrl, \
942 TEMP_FAN_MAP, index - 1), \
943 SENSOR_ATTR_2(temp##index##_pwm_enable, S_IWUSR | S_IRUGO, \
944 show_sf_ctrl, store_sf_ctrl, TEMP_PWM_ENABLE, \
945 index - 1), \
946 SENSOR_ATTR_2(thermal_cruise##index, S_IRUGO | S_IWUSR, \
947 show_sf_ctrl, store_sf_ctrl, TEMP_CRUISE, index - 1), \
948 SENSOR_ATTR_2(tolerance##index, S_IRUGO | S_IWUSR, show_sf_ctrl,\
949 store_sf_ctrl, TEMP_TOLERANCE, index - 1), \
950 SENSOR_ATTR_2(temp##index##_auto_point1_pwm, S_IRUGO | S_IWUSR, \
951 show_sf2_pwm, store_sf2_pwm, 0, index - 1), \
952 SENSOR_ATTR_2(temp##index##_auto_point2_pwm, S_IRUGO | S_IWUSR, \
953 show_sf2_pwm, store_sf2_pwm, 1, index - 1), \
954 SENSOR_ATTR_2(temp##index##_auto_point3_pwm, S_IRUGO | S_IWUSR, \
955 show_sf2_pwm, store_sf2_pwm, 2, index - 1), \
956 SENSOR_ATTR_2(temp##index##_auto_point4_pwm, S_IRUGO | S_IWUSR, \
957 show_sf2_pwm, store_sf2_pwm, 3, index - 1), \
958 SENSOR_ATTR_2(temp##index##_auto_point5_pwm, S_IRUGO | S_IWUSR, \
959 show_sf2_pwm, store_sf2_pwm, 4, index - 1), \
960 SENSOR_ATTR_2(temp##index##_auto_point6_pwm, S_IRUGO | S_IWUSR, \
961 show_sf2_pwm, store_sf2_pwm, 5, index - 1), \
962 SENSOR_ATTR_2(temp##index##_auto_point7_pwm, S_IRUGO | S_IWUSR, \
963 show_sf2_pwm, store_sf2_pwm, 6, index - 1), \
964 SENSOR_ATTR_2(temp##index##_auto_point1_temp, S_IRUGO | S_IWUSR,\
965 show_sf2_temp, store_sf2_temp, 0, index - 1), \
966 SENSOR_ATTR_2(temp##index##_auto_point2_temp, S_IRUGO | S_IWUSR,\
967 show_sf2_temp, store_sf2_temp, 1, index - 1), \
968 SENSOR_ATTR_2(temp##index##_auto_point3_temp, S_IRUGO | S_IWUSR,\
969 show_sf2_temp, store_sf2_temp, 2, index - 1), \
970 SENSOR_ATTR_2(temp##index##_auto_point4_temp, S_IRUGO | S_IWUSR,\
971 show_sf2_temp, store_sf2_temp, 3, index - 1), \
972 SENSOR_ATTR_2(temp##index##_auto_point5_temp, S_IRUGO | S_IWUSR,\
973 show_sf2_temp, store_sf2_temp, 4, index - 1), \
974 SENSOR_ATTR_2(temp##index##_auto_point6_temp, S_IRUGO | S_IWUSR,\
975 show_sf2_temp, store_sf2_temp, 5, index - 1), \
976 SENSOR_ATTR_2(temp##index##_auto_point7_temp, S_IRUGO | S_IWUSR,\
977 show_sf2_temp, store_sf2_temp, 6, index - 1)
979 static struct sensor_device_attribute_2 w83793_sensor_attr_2[] = {
980 SENSOR_ATTR_IN(0),
981 SENSOR_ATTR_IN(1),
982 SENSOR_ATTR_IN(2),
983 SENSOR_ATTR_IN(3),
984 SENSOR_ATTR_IN(4),
985 SENSOR_ATTR_IN(5),
986 SENSOR_ATTR_IN(6),
987 SENSOR_ATTR_IN(7),
988 SENSOR_ATTR_IN(8),
989 SENSOR_ATTR_IN(9),
990 SENSOR_ATTR_FAN(1),
991 SENSOR_ATTR_FAN(2),
992 SENSOR_ATTR_FAN(3),
993 SENSOR_ATTR_FAN(4),
994 SENSOR_ATTR_FAN(5),
995 SENSOR_ATTR_PWM(1),
996 SENSOR_ATTR_PWM(2),
997 SENSOR_ATTR_PWM(3),
1000 static struct sensor_device_attribute_2 w83793_temp[] = {
1001 SENSOR_ATTR_TEMP(1),
1002 SENSOR_ATTR_TEMP(2),
1003 SENSOR_ATTR_TEMP(3),
1004 SENSOR_ATTR_TEMP(4),
1005 SENSOR_ATTR_TEMP(5),
1006 SENSOR_ATTR_TEMP(6),
1009 /* Fan6-Fan12 */
1010 static struct sensor_device_attribute_2 w83793_left_fan[] = {
1011 SENSOR_ATTR_FAN(6),
1012 SENSOR_ATTR_FAN(7),
1013 SENSOR_ATTR_FAN(8),
1014 SENSOR_ATTR_FAN(9),
1015 SENSOR_ATTR_FAN(10),
1016 SENSOR_ATTR_FAN(11),
1017 SENSOR_ATTR_FAN(12),
1020 /* Pwm4-Pwm8 */
1021 static struct sensor_device_attribute_2 w83793_left_pwm[] = {
1022 SENSOR_ATTR_PWM(4),
1023 SENSOR_ATTR_PWM(5),
1024 SENSOR_ATTR_PWM(6),
1025 SENSOR_ATTR_PWM(7),
1026 SENSOR_ATTR_PWM(8),
1029 static struct sensor_device_attribute_2 w83793_vid[] = {
1030 SENSOR_ATTR_2(cpu0_vid, S_IRUGO, show_vid, NULL, NOT_USED, 0),
1031 SENSOR_ATTR_2(cpu1_vid, S_IRUGO, show_vid, NULL, NOT_USED, 1),
1034 static struct sensor_device_attribute_2 sda_single_files[] = {
1035 SENSOR_ATTR_2(vrm, S_IWUSR | S_IRUGO, show_vrm, store_vrm,
1036 NOT_USED, NOT_USED),
1037 SENSOR_ATTR_2(chassis, S_IWUSR | S_IRUGO, show_alarm_beep,
1038 store_chassis_clear, ALARM_STATUS, 30),
1039 SENSOR_ATTR_2(beep_enable, S_IWUSR | S_IRUGO, show_beep_enable,
1040 store_beep_enable, NOT_USED, NOT_USED),
1041 SENSOR_ATTR_2(pwm_default, S_IWUSR | S_IRUGO, show_sf_setup,
1042 store_sf_setup, SETUP_PWM_DEFAULT, NOT_USED),
1043 SENSOR_ATTR_2(pwm_uptime, S_IWUSR | S_IRUGO, show_sf_setup,
1044 store_sf_setup, SETUP_PWM_UPTIME, NOT_USED),
1045 SENSOR_ATTR_2(pwm_downtime, S_IWUSR | S_IRUGO, show_sf_setup,
1046 store_sf_setup, SETUP_PWM_DOWNTIME, NOT_USED),
1047 SENSOR_ATTR_2(temp_critical, S_IWUSR | S_IRUGO, show_sf_setup,
1048 store_sf_setup, SETUP_TEMP_CRITICAL, NOT_USED),
1051 static void w83793_init_client(struct i2c_client *client)
1053 if (reset) {
1054 w83793_write_value(client, W83793_REG_CONFIG, 0x80);
1057 /* Start monitoring */
1058 w83793_write_value(client, W83793_REG_CONFIG,
1059 w83793_read_value(client, W83793_REG_CONFIG) | 0x01);
1063 static int w83793_attach_adapter(struct i2c_adapter *adapter)
1065 if (!(adapter->class & I2C_CLASS_HWMON))
1066 return 0;
1067 return i2c_probe(adapter, &addr_data, w83793_detect);
1070 static int w83793_detach_client(struct i2c_client *client)
1072 struct w83793_data *data = i2c_get_clientdata(client);
1073 struct device *dev = &client->dev;
1074 int err, i;
1076 /* main client */
1077 if (data) {
1078 hwmon_device_unregister(data->class_dev);
1080 for (i = 0; i < ARRAY_SIZE(w83793_sensor_attr_2); i++)
1081 device_remove_file(dev,
1082 &w83793_sensor_attr_2[i].dev_attr);
1084 for (i = 0; i < ARRAY_SIZE(sda_single_files); i++)
1085 device_remove_file(dev, &sda_single_files[i].dev_attr);
1087 for (i = 0; i < ARRAY_SIZE(w83793_vid); i++)
1088 device_remove_file(dev, &w83793_vid[i].dev_attr);
1090 for (i = 0; i < ARRAY_SIZE(w83793_left_fan); i++)
1091 device_remove_file(dev, &w83793_left_fan[i].dev_attr);
1093 for (i = 0; i < ARRAY_SIZE(w83793_left_pwm); i++)
1094 device_remove_file(dev, &w83793_left_pwm[i].dev_attr);
1096 for (i = 0; i < ARRAY_SIZE(w83793_temp); i++)
1097 device_remove_file(dev, &w83793_temp[i].dev_attr);
1100 if ((err = i2c_detach_client(client)))
1101 return err;
1103 /* main client */
1104 if (data)
1105 kfree(data);
1106 /* subclient */
1107 else
1108 kfree(client);
1110 return 0;
1113 static int
1114 w83793_create_subclient(struct i2c_adapter *adapter,
1115 struct i2c_client *client, int addr,
1116 struct i2c_client **sub_cli)
1118 int err = 0;
1119 struct i2c_client *sub_client;
1121 (*sub_cli) = sub_client =
1122 kzalloc(sizeof(struct i2c_client), GFP_KERNEL);
1123 if (!(sub_client)) {
1124 return -ENOMEM;
1126 sub_client->addr = 0x48 + addr;
1127 i2c_set_clientdata(sub_client, NULL);
1128 sub_client->adapter = adapter;
1129 sub_client->driver = &w83793_driver;
1130 strlcpy(sub_client->name, "w83793 subclient", I2C_NAME_SIZE);
1131 if ((err = i2c_attach_client(sub_client))) {
1132 dev_err(&client->dev, "subclient registration "
1133 "at address 0x%x failed\n", sub_client->addr);
1134 kfree(sub_client);
1136 return err;
1139 static int
1140 w83793_detect_subclients(struct i2c_adapter *adapter, int address,
1141 int kind, struct i2c_client *client)
1143 int i, id, err;
1144 u8 tmp;
1145 struct w83793_data *data = i2c_get_clientdata(client);
1147 id = i2c_adapter_id(adapter);
1148 if (force_subclients[0] == id && force_subclients[1] == address) {
1149 for (i = 2; i <= 3; i++) {
1150 if (force_subclients[i] < 0x48
1151 || force_subclients[i] > 0x4f) {
1152 dev_err(&client->dev,
1153 "invalid subclient "
1154 "address %d; must be 0x48-0x4f\n",
1155 force_subclients[i]);
1156 err = -EINVAL;
1157 goto ERROR_SC_0;
1160 w83793_write_value(client, W83793_REG_I2C_SUBADDR,
1161 (force_subclients[2] & 0x07) |
1162 ((force_subclients[3] & 0x07) << 4));
1165 tmp = w83793_read_value(client, W83793_REG_I2C_SUBADDR);
1166 if (!(tmp & 0x08)) {
1167 err =
1168 w83793_create_subclient(adapter, client, tmp & 0x7,
1169 &data->lm75[0]);
1170 if (err < 0)
1171 goto ERROR_SC_0;
1173 if (!(tmp & 0x80)) {
1174 if ((data->lm75[0] != NULL)
1175 && ((tmp & 0x7) == ((tmp >> 4) & 0x7))) {
1176 dev_err(&client->dev,
1177 "duplicate addresses 0x%x, "
1178 "use force_subclients\n", data->lm75[0]->addr);
1179 err = -ENODEV;
1180 goto ERROR_SC_1;
1182 err = w83793_create_subclient(adapter, client,
1183 (tmp >> 4) & 0x7, &data->lm75[1]);
1184 if (err < 0)
1185 goto ERROR_SC_1;
1188 return 0;
1190 /* Undo inits in case of errors */
1192 ERROR_SC_1:
1193 if (data->lm75[0] != NULL) {
1194 i2c_detach_client(data->lm75[0]);
1195 kfree(data->lm75[0]);
1197 ERROR_SC_0:
1198 return err;
1201 static int w83793_detect(struct i2c_adapter *adapter, int address, int kind)
1203 int i;
1204 u8 tmp, val;
1205 struct i2c_client *client;
1206 struct device *dev;
1207 struct w83793_data *data;
1208 int files_fan = ARRAY_SIZE(w83793_left_fan) / 7;
1209 int files_pwm = ARRAY_SIZE(w83793_left_pwm) / 5;
1210 int files_temp = ARRAY_SIZE(w83793_temp) / 6;
1211 int err = 0;
1213 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA)) {
1214 goto exit;
1217 /* OK. For now, we presume we have a valid client. We now create the
1218 client structure, even though we cannot fill it completely yet.
1219 But it allows us to access w83793_{read,write}_value. */
1221 if (!(data = kzalloc(sizeof(struct w83793_data), GFP_KERNEL))) {
1222 err = -ENOMEM;
1223 goto exit;
1226 client = &data->client;
1227 dev = &client->dev;
1228 i2c_set_clientdata(client, data);
1229 client->addr = address;
1230 client->adapter = adapter;
1231 client->driver = &w83793_driver;
1233 data->bank = i2c_smbus_read_byte_data(client, W83793_REG_BANKSEL);
1235 /* Now, we do the remaining detection. */
1236 if (kind < 0) {
1237 tmp = data->bank & 0x80 ? 0x5c : 0xa3;
1238 /* Check Winbond vendor ID */
1239 if (tmp != i2c_smbus_read_byte_data(client,
1240 W83793_REG_VENDORID)) {
1241 pr_debug("w83793: Detection failed at check "
1242 "vendor id\n");
1243 err = -ENODEV;
1244 goto free_mem;
1247 /* If Winbond chip, address of chip and W83793_REG_I2C_ADDR
1248 should match */
1249 if ((data->bank & 0x07) == 0
1250 && i2c_smbus_read_byte_data(client, W83793_REG_I2C_ADDR) !=
1251 (address << 1)) {
1252 pr_debug("w83793: Detection failed at check "
1253 "i2c addr\n");
1254 err = -ENODEV;
1255 goto free_mem;
1260 /* We have either had a force parameter, or we have already detected the
1261 Winbond. Determine the chip type now */
1263 if (kind <= 0) {
1264 if (0x7b == w83793_read_value(client, W83793_REG_CHIPID)) {
1265 kind = w83793;
1266 } else {
1267 if (kind == 0)
1268 dev_warn(&adapter->dev, "w83793: Ignoring "
1269 "'force' parameter for unknown chip "
1270 "at address 0x%02x\n", address);
1271 err = -ENODEV;
1272 goto free_mem;
1276 /* Fill in the remaining client fields and put into the global list */
1277 strlcpy(client->name, "w83793", I2C_NAME_SIZE);
1279 mutex_init(&data->update_lock);
1281 /* Tell the I2C layer a new client has arrived */
1282 if ((err = i2c_attach_client(client)))
1283 goto free_mem;
1285 if ((err = w83793_detect_subclients(adapter, address, kind, client)))
1286 goto detach_client;
1288 /* Initialize the chip */
1289 w83793_init_client(client);
1291 data->vrm = vid_which_vrm();
1293 Only fan 1-5 has their own input pins,
1294 Pwm 1-3 has their own pins
1296 data->has_fan = 0x1f;
1297 data->has_pwm = 0x07;
1298 tmp = w83793_read_value(client, W83793_REG_MFC);
1299 val = w83793_read_value(client, W83793_REG_FANIN_CTRL);
1301 /* check the function of pins 49-56 */
1302 if (!(tmp & 0x80)) {
1303 data->has_pwm |= 0x18; /* pwm 4,5 */
1304 if (val & 0x01) { /* fan 6 */
1305 data->has_fan |= 0x20;
1306 data->has_pwm |= 0x20;
1308 if (val & 0x02) { /* fan 7 */
1309 data->has_fan |= 0x40;
1310 data->has_pwm |= 0x40;
1312 if (!(tmp & 0x40) && (val & 0x04)) { /* fan 8 */
1313 data->has_fan |= 0x80;
1314 data->has_pwm |= 0x80;
1318 if (0x08 == (tmp & 0x0c)) {
1319 if (val & 0x08) /* fan 9 */
1320 data->has_fan |= 0x100;
1321 if (val & 0x10) /* fan 10 */
1322 data->has_fan |= 0x200;
1325 if (0x20 == (tmp & 0x30)) {
1326 if (val & 0x20) /* fan 11 */
1327 data->has_fan |= 0x400;
1328 if (val & 0x40) /* fan 12 */
1329 data->has_fan |= 0x800;
1332 if ((tmp & 0x01) && (val & 0x04)) { /* fan 8, second location */
1333 data->has_fan |= 0x80;
1334 data->has_pwm |= 0x80;
1337 tmp = w83793_read_value(client, W83793_REG_FANIN_SEL);
1338 if ((tmp & 0x01) && (val & 0x08)) { /* fan 9, second location */
1339 data->has_fan |= 0x100;
1341 if ((tmp & 0x02) && (val & 0x10)) { /* fan 10, second location */
1342 data->has_fan |= 0x200;
1344 if ((tmp & 0x04) && (val & 0x20)) { /* fan 11, second location */
1345 data->has_fan |= 0x400;
1347 if ((tmp & 0x08) && (val & 0x40)) { /* fan 12, second location */
1348 data->has_fan |= 0x800;
1351 /* check the temp1-6 mode, ignore former AMDSI selected inputs */
1352 tmp = w83793_read_value(client,W83793_REG_TEMP_MODE[0]);
1353 if (tmp & 0x01)
1354 data->has_temp |= 0x01;
1355 if (tmp & 0x04)
1356 data->has_temp |= 0x02;
1357 if (tmp & 0x10)
1358 data->has_temp |= 0x04;
1359 if (tmp & 0x40)
1360 data->has_temp |= 0x08;
1362 tmp = w83793_read_value(client,W83793_REG_TEMP_MODE[1]);
1363 if (tmp & 0x01)
1364 data->has_temp |= 0x10;
1365 if (tmp & 0x02)
1366 data->has_temp |= 0x20;
1368 /* Detect the VID usage and ignore unused input */
1369 tmp = w83793_read_value(client, W83793_REG_MFC);
1370 if (!(tmp & 0x29))
1371 data->has_vid |= 0x1; /* has VIDA */
1372 if (tmp & 0x80)
1373 data->has_vid |= 0x2; /* has VIDB */
1375 /* Register sysfs hooks */
1376 for (i = 0; i < ARRAY_SIZE(w83793_sensor_attr_2); i++) {
1377 err = device_create_file(dev,
1378 &w83793_sensor_attr_2[i].dev_attr);
1379 if (err)
1380 goto exit_remove;
1383 for (i = 0; i < ARRAY_SIZE(w83793_vid); i++) {
1384 if (!(data->has_vid & (1 << i)))
1385 continue;
1386 err = device_create_file(dev, &w83793_vid[i].dev_attr);
1387 if (err)
1388 goto exit_remove;
1391 for (i = 0; i < ARRAY_SIZE(sda_single_files); i++) {
1392 err = device_create_file(dev, &sda_single_files[i].dev_attr);
1393 if (err)
1394 goto exit_remove;
1398 for (i = 0; i < 6; i++) {
1399 int j;
1400 if (!(data->has_temp & (1 << i)))
1401 continue;
1402 for (j = 0; j < files_temp; j++) {
1403 err = device_create_file(dev,
1404 &w83793_temp[(i) * files_temp
1405 + j].dev_attr);
1406 if (err)
1407 goto exit_remove;
1411 for (i = 5; i < 12; i++) {
1412 int j;
1413 if (!(data->has_fan & (1 << i)))
1414 continue;
1415 for (j = 0; j < files_fan; j++) {
1416 err = device_create_file(dev,
1417 &w83793_left_fan[(i - 5) * files_fan
1418 + j].dev_attr);
1419 if (err)
1420 goto exit_remove;
1424 for (i = 3; i < 8; i++) {
1425 int j;
1426 if (!(data->has_pwm & (1 << i)))
1427 continue;
1428 for (j = 0; j < files_pwm; j++) {
1429 err = device_create_file(dev,
1430 &w83793_left_pwm[(i - 3) * files_pwm
1431 + j].dev_attr);
1432 if (err)
1433 goto exit_remove;
1437 data->class_dev = hwmon_device_register(dev);
1438 if (IS_ERR(data->class_dev)) {
1439 err = PTR_ERR(data->class_dev);
1440 goto exit_remove;
1443 return 0;
1445 /* Unregister sysfs hooks */
1447 exit_remove:
1448 for (i = 0; i < ARRAY_SIZE(w83793_sensor_attr_2); i++)
1449 device_remove_file(dev, &w83793_sensor_attr_2[i].dev_attr);
1451 for (i = 0; i < ARRAY_SIZE(sda_single_files); i++)
1452 device_remove_file(dev, &sda_single_files[i].dev_attr);
1454 for (i = 0; i < ARRAY_SIZE(w83793_vid); i++)
1455 device_remove_file(dev, &w83793_vid[i].dev_attr);
1457 for (i = 0; i < ARRAY_SIZE(w83793_left_fan); i++)
1458 device_remove_file(dev, &w83793_left_fan[i].dev_attr);
1460 for (i = 0; i < ARRAY_SIZE(w83793_left_pwm); i++)
1461 device_remove_file(dev, &w83793_left_pwm[i].dev_attr);
1463 for (i = 0; i < ARRAY_SIZE(w83793_temp); i++)
1464 device_remove_file(dev, &w83793_temp[i].dev_attr);
1466 if (data->lm75[0] != NULL) {
1467 i2c_detach_client(data->lm75[0]);
1468 kfree(data->lm75[0]);
1470 if (data->lm75[1] != NULL) {
1471 i2c_detach_client(data->lm75[1]);
1472 kfree(data->lm75[1]);
1474 detach_client:
1475 i2c_detach_client(client);
1476 free_mem:
1477 kfree(data);
1478 exit:
1479 return err;
1482 static void w83793_update_nonvolatile(struct device *dev)
1484 struct i2c_client *client = to_i2c_client(dev);
1485 struct w83793_data *data = i2c_get_clientdata(client);
1486 int i, j;
1488 They are somewhat "stable" registers, and to update them everytime
1489 takes so much time, it's just not worthy. Update them in a long
1490 interval to avoid exception.
1492 if (!(time_after(jiffies, data->last_nonvolatile + HZ * 300)
1493 || !data->valid))
1494 return;
1495 /* update voltage limits */
1496 for (i = 1; i < 3; i++) {
1497 for (j = 0; j < ARRAY_SIZE(data->in); j++) {
1498 data->in[j][i] =
1499 w83793_read_value(client, W83793_REG_IN[j][i]);
1501 data->in_low_bits[i] =
1502 w83793_read_value(client, W83793_REG_IN_LOW_BITS[i]);
1505 for (i = 0; i < ARRAY_SIZE(data->fan_min); i++) {
1506 /* Update the Fan measured value and limits */
1507 if (!(data->has_fan & (1 << i))) {
1508 continue;
1510 data->fan_min[i] =
1511 w83793_read_value(client, W83793_REG_FAN_MIN(i)) << 8;
1512 data->fan_min[i] |=
1513 w83793_read_value(client, W83793_REG_FAN_MIN(i) + 1);
1516 for (i = 0; i < ARRAY_SIZE(data->temp_fan_map); i++) {
1517 if (!(data->has_temp & (1 << i)))
1518 continue;
1519 data->temp_fan_map[i] =
1520 w83793_read_value(client, W83793_REG_TEMP_FAN_MAP(i));
1521 for (j = 1; j < 5; j++) {
1522 data->temp[i][j] =
1523 w83793_read_value(client, W83793_REG_TEMP[i][j]);
1525 data->temp_cruise[i] =
1526 w83793_read_value(client, W83793_REG_TEMP_CRUISE(i));
1527 for (j = 0; j < 7; j++) {
1528 data->sf2_pwm[i][j] =
1529 w83793_read_value(client, W83793_REG_SF2_PWM(i, j));
1530 data->sf2_temp[i][j] =
1531 w83793_read_value(client,
1532 W83793_REG_SF2_TEMP(i, j));
1536 for (i = 0; i < ARRAY_SIZE(data->temp_mode); i++)
1537 data->temp_mode[i] =
1538 w83793_read_value(client, W83793_REG_TEMP_MODE[i]);
1540 for (i = 0; i < ARRAY_SIZE(data->tolerance); i++) {
1541 data->tolerance[i] =
1542 w83793_read_value(client, W83793_REG_TEMP_TOL(i));
1545 for (i = 0; i < ARRAY_SIZE(data->pwm); i++) {
1546 if (!(data->has_pwm & (1 << i)))
1547 continue;
1548 data->pwm[i][PWM_NONSTOP] =
1549 w83793_read_value(client, W83793_REG_PWM(i, PWM_NONSTOP));
1550 data->pwm[i][PWM_START] =
1551 w83793_read_value(client, W83793_REG_PWM(i, PWM_START));
1552 data->pwm_stop_time[i] =
1553 w83793_read_value(client, W83793_REG_PWM_STOP_TIME(i));
1556 data->pwm_default = w83793_read_value(client, W83793_REG_PWM_DEFAULT);
1557 data->pwm_enable = w83793_read_value(client, W83793_REG_PWM_ENABLE);
1558 data->pwm_uptime = w83793_read_value(client, W83793_REG_PWM_UPTIME);
1559 data->pwm_downtime = w83793_read_value(client, W83793_REG_PWM_DOWNTIME);
1560 data->temp_critical =
1561 w83793_read_value(client, W83793_REG_TEMP_CRITICAL);
1562 data->beep_enable = w83793_read_value(client, W83793_REG_OVT_BEEP);
1564 for (i = 0; i < ARRAY_SIZE(data->beeps); i++) {
1565 data->beeps[i] = w83793_read_value(client, W83793_REG_BEEP(i));
1568 data->last_nonvolatile = jiffies;
1571 static struct w83793_data *w83793_update_device(struct device *dev)
1573 struct i2c_client *client = to_i2c_client(dev);
1574 struct w83793_data *data = i2c_get_clientdata(client);
1575 int i;
1577 mutex_lock(&data->update_lock);
1579 if (!(time_after(jiffies, data->last_updated + HZ * 2)
1580 || !data->valid))
1581 goto END;
1583 /* Update the voltages measured value and limits */
1584 for (i = 0; i < ARRAY_SIZE(data->in); i++)
1585 data->in[i][IN_READ] =
1586 w83793_read_value(client, W83793_REG_IN[i][IN_READ]);
1588 data->in_low_bits[IN_READ] =
1589 w83793_read_value(client, W83793_REG_IN_LOW_BITS[IN_READ]);
1591 for (i = 0; i < ARRAY_SIZE(data->fan); i++) {
1592 if (!(data->has_fan & (1 << i))) {
1593 continue;
1595 data->fan[i] =
1596 w83793_read_value(client, W83793_REG_FAN(i)) << 8;
1597 data->fan[i] |=
1598 w83793_read_value(client, W83793_REG_FAN(i) + 1);
1601 for (i = 0; i < ARRAY_SIZE(data->temp); i++) {
1602 if (!(data->has_temp & (1 << i)))
1603 continue;
1604 data->temp[i][TEMP_READ] =
1605 w83793_read_value(client, W83793_REG_TEMP[i][TEMP_READ]);
1608 data->temp_low_bits =
1609 w83793_read_value(client, W83793_REG_TEMP_LOW_BITS);
1611 for (i = 0; i < ARRAY_SIZE(data->pwm); i++) {
1612 if (data->has_pwm & (1 << i))
1613 data->pwm[i][PWM_DUTY] =
1614 w83793_read_value(client,
1615 W83793_REG_PWM(i, PWM_DUTY));
1618 for (i = 0; i < ARRAY_SIZE(data->alarms); i++)
1619 data->alarms[i] =
1620 w83793_read_value(client, W83793_REG_ALARM(i));
1621 if (data->has_vid & 0x01)
1622 data->vid[0] = w83793_read_value(client, W83793_REG_VID_INA);
1623 if (data->has_vid & 0x02)
1624 data->vid[1] = w83793_read_value(client, W83793_REG_VID_INB);
1625 w83793_update_nonvolatile(dev);
1626 data->last_updated = jiffies;
1627 data->valid = 1;
1629 END:
1630 mutex_unlock(&data->update_lock);
1631 return data;
1634 /* Ignore the possibility that somebody change bank outside the driver
1635 Must be called with data->update_lock held, except during initialization */
1636 static u8 w83793_read_value(struct i2c_client *client, u16 reg)
1638 struct w83793_data *data = i2c_get_clientdata(client);
1639 u8 res = 0xff;
1640 u8 new_bank = reg >> 8;
1642 new_bank |= data->bank & 0xfc;
1643 if (data->bank != new_bank) {
1644 if (i2c_smbus_write_byte_data
1645 (client, W83793_REG_BANKSEL, new_bank) >= 0)
1646 data->bank = new_bank;
1647 else {
1648 dev_err(&client->dev,
1649 "set bank to %d failed, fall back "
1650 "to bank %d, read reg 0x%x error\n",
1651 new_bank, data->bank, reg);
1652 res = 0x0; /* read 0x0 from the chip */
1653 goto END;
1656 res = i2c_smbus_read_byte_data(client, reg & 0xff);
1657 END:
1658 return res;
1661 /* Must be called with data->update_lock held, except during initialization */
1662 static int w83793_write_value(struct i2c_client *client, u16 reg, u8 value)
1664 struct w83793_data *data = i2c_get_clientdata(client);
1665 int res;
1666 u8 new_bank = reg >> 8;
1668 new_bank |= data->bank & 0xfc;
1669 if (data->bank != new_bank) {
1670 if ((res = i2c_smbus_write_byte_data
1671 (client, W83793_REG_BANKSEL, new_bank)) >= 0)
1672 data->bank = new_bank;
1673 else {
1674 dev_err(&client->dev,
1675 "set bank to %d failed, fall back "
1676 "to bank %d, write reg 0x%x error\n",
1677 new_bank, data->bank, reg);
1678 goto END;
1682 res = i2c_smbus_write_byte_data(client, reg & 0xff, value);
1683 END:
1684 return res;
1687 static int __init sensors_w83793_init(void)
1689 return i2c_add_driver(&w83793_driver);
1692 static void __exit sensors_w83793_exit(void)
1694 i2c_del_driver(&w83793_driver);
1697 MODULE_AUTHOR("Yuan Mu");
1698 MODULE_DESCRIPTION("w83793 driver");
1699 MODULE_LICENSE("GPL");
1701 module_init(sensors_w83793_init);
1702 module_exit(sensors_w83793_exit);