blob: 99e603a6a667dfd12fe8d35fb54a8bc28b860022 [file] [log] [blame]
Rudolf Marek6800c3d2006-12-12 18:18:30 +01001/*
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>
6
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.
10
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.
15
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.
20*/
21
22/*
23 Supports following chips:
24
25 Chip #vin #fanin #pwm #temp wchipid vendid i2c ISA
26 w83793 10 12 8 6 0x7b 0x5ca3 yes no
27*/
28
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>
38
39/* Addresses to scan */
40static unsigned short normal_i2c[] = { 0x2c, 0x2d, 0x2e, 0x2f, I2C_CLIENT_END };
41
42/* Insmod parameters */
43I2C_CLIENT_INSMOD_1(w83793);
44I2C_CLIENT_MODULE_PARM(force_subclients, "List of subclient addresses: "
45 "{bus, clientaddr, subclientaddr1, subclientaddr2}");
46
47static int reset;
48module_param(reset, bool, 0);
49MODULE_PARM_DESC(reset, "Set to 1 to reset chip, not recommended");
50
51/*
52 Address 0x00, 0x0d, 0x0e, 0x0f in all three banks are reserved
53 as ID, Bank Select registers
54*/
55#define W83793_REG_BANKSEL 0x00
56#define W83793_REG_VENDORID 0x0d
57#define W83793_REG_CHIPID 0x0e
58#define W83793_REG_DEVICEID 0x0f
59
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
71
72static u16 W83793_REG_TEMP_MODE[2] = { 0x5e, 0x5f };
73
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 */
81static 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},
88};
89
90#define W83793_REG_TEMP_LOW_BITS 0x22
91
92#define W83793_REG_BEEP(index) (0x53 + (index))
93#define W83793_REG_ALARM(index) (0x4b + (index))
94
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
99
100#define IN_READ 0
101#define IN_MAX 1
102#define IN_LOW 2
103static 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 */
115};
116
117/* Low Bits of Vcore A/B Vtt Read/High/Low */
118static const u16 W83793_REG_IN_LOW_BITS[] = { 0x1b, 0x68, 0x69 };
119static u8 scale_in[] = { 2, 2, 2, 16, 16, 16, 8, 24, 24, 16 };
Gong Junddca9332007-01-18 22:14:23 +0100120static u8 scale_in_add[] = { 0, 0, 0, 0, 0, 0, 0, 150, 150, 0 };
Rudolf Marek6800c3d2006-12-12 18:18:30 +0100121
122#define W83793_REG_FAN(index) (0x23 + 2 * (index)) /* High byte */
123#define W83793_REG_FAN_MIN(index) (0x90 + 2 * (index)) /* High byte */
124
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
130
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))
136
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))
144
145static inline unsigned long FAN_FROM_REG(u16 val)
146{
147 if ((val >= 0xfff) || (val == 0))
148 return 0;
149 return (1350000UL / val);
150}
151
152static inline u16 FAN_TO_REG(long rpm)
153{
154 if (rpm <= 0)
155 return 0x0fff;
156 return SENSORS_LIMIT((1350000 + (rpm >> 1)) / rpm, 1, 0xffe);
157}
158
159static inline unsigned long TIME_FROM_REG(u8 reg)
160{
161 return (reg * 100);
162}
163
164static inline u8 TIME_TO_REG(unsigned long val)
165{
166 return SENSORS_LIMIT((val + 50) / 100, 0, 0xff);
167}
168
169static inline long TEMP_FROM_REG(s8 reg)
170{
171 return (reg * 1000);
172}
173
174static inline s8 TEMP_TO_REG(long val, s8 min, s8 max)
175{
176 return SENSORS_LIMIT((val + (val < 0 ? -500 : 500)) / 1000, min, max);
177}
178
179struct 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 */
188
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 */
194
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 */
198
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 */
205
206 u8 has_pwm;
207 u8 pwm_enable; /* Register value, each Temp has 1 bit */
208 u8 pwm_uptime; /* Register value */
209 u8 pwm_downtime; /* Register value */
210 u8 pwm_default; /* All fan default pwm, next poweron valid */
211 u8 pwm[8][3]; /* Register value */
212 u8 pwm_stop_time[8];
213 u8 temp_cruise[6];
214
215 u8 alarms[5]; /* realtime status registers */
216 u8 beeps[5];
217 u8 beep_enable;
218 u8 tolerance[3]; /* Temp tolerance(Smart Fan I/II) */
219 u8 sf2_pwm[6][7]; /* Smart FanII: Fan duty cycle */
220 u8 sf2_temp[6][7]; /* Smart FanII: Temp level point */
221};
222
223static u8 w83793_read_value(struct i2c_client *client, u16 reg);
224static int w83793_write_value(struct i2c_client *client, u16 reg, u8 value);
225static int w83793_attach_adapter(struct i2c_adapter *adapter);
226static int w83793_detect(struct i2c_adapter *adapter, int address, int kind);
227static int w83793_detach_client(struct i2c_client *client);
228static void w83793_init_client(struct i2c_client *client);
229static void w83793_update_nonvolatile(struct device *dev);
230static struct w83793_data *w83793_update_device(struct device *dev);
231
232static struct i2c_driver w83793_driver = {
233 .driver = {
234 .name = "w83793",
235 },
236 .attach_adapter = w83793_attach_adapter,
237 .detach_client = w83793_detach_client,
238};
239
240static ssize_t
241show_vrm(struct device *dev, struct device_attribute *attr, char *buf)
242{
243 struct i2c_client *client = to_i2c_client(dev);
244 struct w83793_data *data = i2c_get_clientdata(client);
245
246 return sprintf(buf, "%d\n", data->vrm);
247}
248
249static ssize_t
250show_vid(struct device *dev, struct device_attribute *attr, char *buf)
251{
252 struct w83793_data *data = w83793_update_device(dev);
253 struct sensor_device_attribute_2 *sensor_attr =
254 to_sensor_dev_attr_2(attr);
255 int index = sensor_attr->index;
256
257 return sprintf(buf, "%d\n", vid_from_reg(data->vid[index], data->vrm));
258}
259
260static ssize_t
261store_vrm(struct device *dev, struct device_attribute *attr,
262 const char *buf, size_t count)
263{
264 struct i2c_client *client = to_i2c_client(dev);
265 struct w83793_data *data = i2c_get_clientdata(client);
266
267 data->vrm = simple_strtoul(buf, NULL, 10);
268 return count;
269}
270
271#define ALARM_STATUS 0
272#define BEEP_ENABLE 1
273static ssize_t
274show_alarm_beep(struct device *dev, struct device_attribute *attr, char *buf)
275{
276 struct w83793_data *data = w83793_update_device(dev);
277 struct sensor_device_attribute_2 *sensor_attr =
278 to_sensor_dev_attr_2(attr);
279 int nr = sensor_attr->nr;
280 int index = sensor_attr->index >> 3;
281 int bit = sensor_attr->index & 0x07;
282 u8 val;
283
284 if (ALARM_STATUS == nr) {
285 val = (data->alarms[index] >> (bit)) & 1;
286 } else { /* BEEP_ENABLE */
287 val = (data->beeps[index] >> (bit)) & 1;
288 }
289
290 return sprintf(buf, "%u\n", val);
291}
292
293static ssize_t
294store_beep(struct device *dev, struct device_attribute *attr,
295 const char *buf, size_t count)
296{
297 struct i2c_client *client = to_i2c_client(dev);
298 struct w83793_data *data = i2c_get_clientdata(client);
299 struct sensor_device_attribute_2 *sensor_attr =
300 to_sensor_dev_attr_2(attr);
301 int index = sensor_attr->index >> 3;
302 int shift = sensor_attr->index & 0x07;
303 u8 beep_bit = 1 << shift;
304 u8 val;
305
306 val = simple_strtoul(buf, NULL, 10);
307 if (val != 0 && val != 1)
308 return -EINVAL;
309
310 mutex_lock(&data->update_lock);
311 data->beeps[index] = w83793_read_value(client, W83793_REG_BEEP(index));
312 data->beeps[index] &= ~beep_bit;
313 data->beeps[index] |= val << shift;
314 w83793_write_value(client, W83793_REG_BEEP(index), data->beeps[index]);
315 mutex_unlock(&data->update_lock);
316
317 return count;
318}
319
320static ssize_t
321show_beep_enable(struct device *dev, struct device_attribute *attr, char *buf)
322{
323 struct w83793_data *data = w83793_update_device(dev);
324 return sprintf(buf, "%u\n", (data->beep_enable >> 1) & 0x01);
325}
326
327static ssize_t
328store_beep_enable(struct device *dev, struct device_attribute *attr,
329 const char *buf, size_t count)
330{
331 struct i2c_client *client = to_i2c_client(dev);
332 struct w83793_data *data = i2c_get_clientdata(client);
333 u8 val = simple_strtoul(buf, NULL, 10);
334
335 if (val != 0 && val != 1)
336 return -EINVAL;
337
338 mutex_lock(&data->update_lock);
339 data->beep_enable = w83793_read_value(client, W83793_REG_OVT_BEEP)
340 & 0xfd;
341 data->beep_enable |= val << 1;
342 w83793_write_value(client, W83793_REG_OVT_BEEP, data->beep_enable);
343 mutex_unlock(&data->update_lock);
344
345 return count;
346}
347
348/* Write any value to clear chassis alarm */
349static ssize_t
350store_chassis_clear(struct device *dev,
351 struct device_attribute *attr, const char *buf,
352 size_t count)
353{
354 struct i2c_client *client = to_i2c_client(dev);
355 struct w83793_data *data = i2c_get_clientdata(client);
356 u8 val;
357
358 mutex_lock(&data->update_lock);
359 val = w83793_read_value(client, W83793_REG_CLR_CHASSIS);
360 val |= 0x80;
361 w83793_write_value(client, W83793_REG_CLR_CHASSIS, val);
362 mutex_unlock(&data->update_lock);
363 return count;
364}
365
366#define FAN_INPUT 0
367#define FAN_MIN 1
368static ssize_t
369show_fan(struct device *dev, struct device_attribute *attr, char *buf)
370{
371 struct sensor_device_attribute_2 *sensor_attr =
372 to_sensor_dev_attr_2(attr);
373 int nr = sensor_attr->nr;
374 int index = sensor_attr->index;
375 struct w83793_data *data = w83793_update_device(dev);
376 u16 val;
377
378 if (FAN_INPUT == nr) {
379 val = data->fan[index] & 0x0fff;
380 } else {
381 val = data->fan_min[index] & 0x0fff;
382 }
383
384 return sprintf(buf, "%lu\n", FAN_FROM_REG(val));
385}
386
387static ssize_t
388store_fan_min(struct device *dev, struct device_attribute *attr,
389 const char *buf, size_t count)
390{
391 struct sensor_device_attribute_2 *sensor_attr =
392 to_sensor_dev_attr_2(attr);
393 int index = sensor_attr->index;
394 struct i2c_client *client = to_i2c_client(dev);
395 struct w83793_data *data = i2c_get_clientdata(client);
396 u16 val = FAN_TO_REG(simple_strtoul(buf, NULL, 10));
397
398 mutex_lock(&data->update_lock);
399 data->fan_min[index] = val;
400 w83793_write_value(client, W83793_REG_FAN_MIN(index),
401 (val >> 8) & 0xff);
402 w83793_write_value(client, W83793_REG_FAN_MIN(index) + 1, val & 0xff);
403 mutex_unlock(&data->update_lock);
404
405 return count;
406}
407
408#define PWM_DUTY 0
409#define PWM_START 1
410#define PWM_NONSTOP 2
411#define PWM_STOP_TIME 3
412static ssize_t
413show_pwm(struct device *dev, struct device_attribute *attr, char *buf)
414{
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;
421
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;
426
427 return sprintf(buf, "%d\n", val);
428}
429
430static ssize_t
431store_pwm(struct device *dev, struct device_attribute *attr,
432 const char *buf, size_t count)
433{
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;
441
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]);
456 }
457
458 mutex_unlock(&data->update_lock);
459 return count;
460}
461
462static ssize_t
463show_temp(struct device *dev, struct device_attribute *attr, char *buf)
464{
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]);
471
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;
475 }
476 return sprintf(buf, "%ld\n", temp);
477}
478
479static ssize_t
480store_temp(struct device *dev, struct device_attribute *attr,
481 const char *buf, size_t count)
482{
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);
490
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;
497}
498
499/*
500 TD1-TD4
501 each has 4 mode:(2 bits)
502 0: Stop monitor
503 1: Use internal temp sensor(default)
Gong Junddca9332007-01-18 22:14:23 +0100504 2: Reserved
Rudolf Marek6800c3d2006-12-12 18:18:30 +0100505 3: Use sensor in Intel CPU and get result by PECI
506
507 TR1-TR2
508 each has 2 mode:(1 bit)
509 0: Disable temp sensor monitor
510 1: To enable temp sensors monitor
511*/
512
Gong Junddca9332007-01-18 22:14:23 +0100513/* 0 disable, 6 PECI */
514static u8 TO_TEMP_MODE[] = { 0, 0, 0, 6 };
Rudolf Marek6800c3d2006-12-12 18:18:30 +0100515
516static ssize_t
517show_temp_mode(struct device *dev, struct device_attribute *attr, char *buf)
518{
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;
527
528 tmp = (data->temp_mode[index] >> shift) & mask;
529
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];
535 }
536
537 return sprintf(buf, "%d\n", tmp);
538}
539
540static ssize_t
541store_temp_mode(struct device *dev, struct device_attribute *attr,
542 const char *buf, size_t count)
543{
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);
552
553 /* transform the sysfs interface values into table above */
Gong Junddca9332007-01-18 22:14:23 +0100554 if ((val == 6) && (index < 4)) {
Rudolf Marek6800c3d2006-12-12 18:18:30 +0100555 val -= 3;
556 } else if ((val == 3 && index < 4)
557 || (val == 4 && index >= 4)
558 || val == 0) {
559 /* transform diode or thermistor into internal enable */
560 val = !!val;
561 } else {
562 return -EINVAL;
563 }
564
565 index = (index < 4) ? 0 : 1;
566 mutex_lock(&data->update_lock);
567 data->temp_mode[index] =
568 w83793_read_value(client, W83793_REG_TEMP_MODE[index]);
569 data->temp_mode[index] &= ~(mask << shift);
570 data->temp_mode[index] |= val << shift;
571 w83793_write_value(client, W83793_REG_TEMP_MODE[index],
572 data->temp_mode[index]);
573 mutex_unlock(&data->update_lock);
574
575 return count;
576}
577
578#define SETUP_PWM_DEFAULT 0
579#define SETUP_PWM_UPTIME 1 /* Unit in 0.1s */
580#define SETUP_PWM_DOWNTIME 2 /* Unit in 0.1s */
581#define SETUP_TEMP_CRITICAL 3
582static ssize_t
583show_sf_setup(struct device *dev, struct device_attribute *attr, char *buf)
584{
585 struct sensor_device_attribute_2 *sensor_attr =
586 to_sensor_dev_attr_2(attr);
587 int nr = sensor_attr->nr;
588 struct w83793_data *data = w83793_update_device(dev);
589 u32 val = 0;
590
591 if (SETUP_PWM_DEFAULT == nr) {
592 val = (data->pwm_default & 0x3f) << 2;
593 } else if (SETUP_PWM_UPTIME == nr) {
594 val = TIME_FROM_REG(data->pwm_uptime);
595 } else if (SETUP_PWM_DOWNTIME == nr) {
596 val = TIME_FROM_REG(data->pwm_downtime);
597 } else if (SETUP_TEMP_CRITICAL == nr) {
598 val = TEMP_FROM_REG(data->temp_critical & 0x7f);
599 }
600
601 return sprintf(buf, "%d\n", val);
602}
603
604static ssize_t
605store_sf_setup(struct device *dev, struct device_attribute *attr,
606 const char *buf, size_t count)
607{
608 struct sensor_device_attribute_2 *sensor_attr =
609 to_sensor_dev_attr_2(attr);
610 int nr = sensor_attr->nr;
611 struct i2c_client *client = to_i2c_client(dev);
612 struct w83793_data *data = i2c_get_clientdata(client);
613
614 mutex_lock(&data->update_lock);
615 if (SETUP_PWM_DEFAULT == nr) {
616 data->pwm_default =
617 w83793_read_value(client, W83793_REG_PWM_DEFAULT) & 0xc0;
618 data->pwm_default |= SENSORS_LIMIT(simple_strtoul(buf, NULL,
619 10),
620 0, 0xff) >> 2;
621 w83793_write_value(client, W83793_REG_PWM_DEFAULT,
622 data->pwm_default);
623 } else if (SETUP_PWM_UPTIME == nr) {
624 data->pwm_uptime = TIME_TO_REG(simple_strtoul(buf, NULL, 10));
625 data->pwm_uptime += data->pwm_uptime == 0 ? 1 : 0;
626 w83793_write_value(client, W83793_REG_PWM_UPTIME,
627 data->pwm_uptime);
628 } else if (SETUP_PWM_DOWNTIME == nr) {
629 data->pwm_downtime = TIME_TO_REG(simple_strtoul(buf, NULL, 10));
630 data->pwm_downtime += data->pwm_downtime == 0 ? 1 : 0;
631 w83793_write_value(client, W83793_REG_PWM_DOWNTIME,
632 data->pwm_downtime);
633 } else { /* SETUP_TEMP_CRITICAL */
634 data->temp_critical =
635 w83793_read_value(client, W83793_REG_TEMP_CRITICAL) & 0x80;
636 data->temp_critical |= TEMP_TO_REG(simple_strtol(buf, NULL, 10),
637 0, 0x7f);
638 w83793_write_value(client, W83793_REG_TEMP_CRITICAL,
639 data->temp_critical);
640 }
641
642 mutex_unlock(&data->update_lock);
643 return count;
644}
645
646/*
647 Temp SmartFan control
648 TEMP_FAN_MAP
649 Temp channel control which pwm fan, bitfield, bit 0 indicate pwm1...
650 It's possible two or more temp channels control the same fan, w83793
651 always prefers to pick the most critical request and applies it to
652 the related Fan.
653 It's possible one fan is not in any mapping of 6 temp channels, this
654 means the fan is manual mode
655
656 TEMP_PWM_ENABLE
657 Each temp channel has its own SmartFan mode, and temp channel
658 control fans that are set by TEMP_FAN_MAP
659 0: SmartFanII mode
660 1: Thermal Cruise Mode
661
662 TEMP_CRUISE
663 Target temperature in thermal cruise mode, w83793 will try to turn
664 fan speed to keep the temperature of target device around this
665 temperature.
666
667 TEMP_TOLERANCE
668 If Temp higher or lower than target with this tolerance, w83793
669 will take actions to speed up or slow down the fan to keep the
670 temperature within the tolerance range.
671*/
672
673#define TEMP_FAN_MAP 0
674#define TEMP_PWM_ENABLE 1
675#define TEMP_CRUISE 2
676#define TEMP_TOLERANCE 3
677static ssize_t
678show_sf_ctrl(struct device *dev, struct device_attribute *attr, char *buf)
679{
680 struct sensor_device_attribute_2 *sensor_attr =
681 to_sensor_dev_attr_2(attr);
682 int nr = sensor_attr->nr;
683 int index = sensor_attr->index;
684 struct w83793_data *data = w83793_update_device(dev);
685 u32 val;
686
687 if (TEMP_FAN_MAP == nr) {
688 val = data->temp_fan_map[index];
689 } else if (TEMP_PWM_ENABLE == nr) {
690 /* +2 to transfrom into 2 and 3 to conform with sysfs intf */
691 val = ((data->pwm_enable >> index) & 0x01) + 2;
692 } else if (TEMP_CRUISE == nr) {
693 val = TEMP_FROM_REG(data->temp_cruise[index] & 0x7f);
694 } else { /* TEMP_TOLERANCE */
695 val = data->tolerance[index >> 1] >> ((index & 0x01) ? 4 : 0);
696 val = TEMP_FROM_REG(val & 0x0f);
697 }
698 return sprintf(buf, "%d\n", val);
699}
700
701static ssize_t
702store_sf_ctrl(struct device *dev, struct device_attribute *attr,
703 const char *buf, size_t count)
704{
705 struct sensor_device_attribute_2 *sensor_attr =
706 to_sensor_dev_attr_2(attr);
707 int nr = sensor_attr->nr;
708 int index = sensor_attr->index;
709 struct i2c_client *client = to_i2c_client(dev);
710 struct w83793_data *data = i2c_get_clientdata(client);
711 u32 val;
712
713 mutex_lock(&data->update_lock);
714 if (TEMP_FAN_MAP == nr) {
715 val = simple_strtoul(buf, NULL, 10) & 0xff;
716 w83793_write_value(client, W83793_REG_TEMP_FAN_MAP(index), val);
717 data->temp_fan_map[index] = val;
718 } else if (TEMP_PWM_ENABLE == nr) {
719 val = simple_strtoul(buf, NULL, 10);
720 if (2 == val || 3 == val) {
721 data->pwm_enable =
722 w83793_read_value(client, W83793_REG_PWM_ENABLE);
723 if (val - 2)
724 data->pwm_enable |= 1 << index;
725 else
726 data->pwm_enable &= ~(1 << index);
727 w83793_write_value(client, W83793_REG_PWM_ENABLE,
728 data->pwm_enable);
729 } else {
730 mutex_unlock(&data->update_lock);
731 return -EINVAL;
732 }
733 } else if (TEMP_CRUISE == nr) {
734 data->temp_cruise[index] =
735 w83793_read_value(client, W83793_REG_TEMP_CRUISE(index));
736 val = TEMP_TO_REG(simple_strtol(buf, NULL, 10), 0, 0x7f);
737 data->temp_cruise[index] &= 0x80;
738 data->temp_cruise[index] |= val;
739
740 w83793_write_value(client, W83793_REG_TEMP_CRUISE(index),
741 data->temp_cruise[index]);
742 } else { /* TEMP_TOLERANCE */
743 int i = index >> 1;
744 u8 shift = (index & 0x01) ? 4 : 0;
745 data->tolerance[i] =
746 w83793_read_value(client, W83793_REG_TEMP_TOL(i));
747
748 val = TEMP_TO_REG(simple_strtol(buf, NULL, 10), 0, 0x0f);
749 data->tolerance[i] &= ~(0x0f << shift);
750 data->tolerance[i] |= val << shift;
751 w83793_write_value(client, W83793_REG_TEMP_TOL(i),
752 data->tolerance[i]);
753 }
754
755 mutex_unlock(&data->update_lock);
756 return count;
757}
758
759static ssize_t
760show_sf2_pwm(struct device *dev, struct device_attribute *attr, char *buf)
761{
762 struct sensor_device_attribute_2 *sensor_attr =
763 to_sensor_dev_attr_2(attr);
764 int nr = sensor_attr->nr;
765 int index = sensor_attr->index;
766 struct w83793_data *data = w83793_update_device(dev);
767
768 return sprintf(buf, "%d\n", (data->sf2_pwm[index][nr] & 0x3f) << 2);
769}
770
771static ssize_t
772store_sf2_pwm(struct device *dev, struct device_attribute *attr,
773 const char *buf, size_t count)
774{
775 struct i2c_client *client = to_i2c_client(dev);
776 struct w83793_data *data = i2c_get_clientdata(client);
777 struct sensor_device_attribute_2 *sensor_attr =
778 to_sensor_dev_attr_2(attr);
779 int nr = sensor_attr->nr;
780 int index = sensor_attr->index;
781 u8 val = SENSORS_LIMIT(simple_strtoul(buf, NULL, 10), 0, 0xff) >> 2;
782
783 mutex_lock(&data->update_lock);
784 data->sf2_pwm[index][nr] =
785 w83793_read_value(client, W83793_REG_SF2_PWM(index, nr)) & 0xc0;
786 data->sf2_pwm[index][nr] |= val;
787 w83793_write_value(client, W83793_REG_SF2_PWM(index, nr),
788 data->sf2_pwm[index][nr]);
789 mutex_unlock(&data->update_lock);
790 return count;
791}
792
793static ssize_t
794show_sf2_temp(struct device *dev, struct device_attribute *attr, char *buf)
795{
796 struct sensor_device_attribute_2 *sensor_attr =
797 to_sensor_dev_attr_2(attr);
798 int nr = sensor_attr->nr;
799 int index = sensor_attr->index;
800 struct w83793_data *data = w83793_update_device(dev);
801
802 return sprintf(buf, "%ld\n",
803 TEMP_FROM_REG(data->sf2_temp[index][nr] & 0x7f));
804}
805
806static ssize_t
807store_sf2_temp(struct device *dev, struct device_attribute *attr,
808 const char *buf, size_t count)
809{
810 struct i2c_client *client = to_i2c_client(dev);
811 struct w83793_data *data = i2c_get_clientdata(client);
812 struct sensor_device_attribute_2 *sensor_attr =
813 to_sensor_dev_attr_2(attr);
814 int nr = sensor_attr->nr;
815 int index = sensor_attr->index;
816 u8 val = TEMP_TO_REG(simple_strtol(buf, NULL, 10), 0, 0x7f);
817
818 mutex_lock(&data->update_lock);
819 data->sf2_temp[index][nr] =
820 w83793_read_value(client, W83793_REG_SF2_TEMP(index, nr)) & 0x80;
821 data->sf2_temp[index][nr] |= val;
822 w83793_write_value(client, W83793_REG_SF2_TEMP(index, nr),
823 data->sf2_temp[index][nr]);
824 mutex_unlock(&data->update_lock);
825 return count;
826}
827
828/* only Vcore A/B and Vtt have additional 2 bits precision */
829static ssize_t
830show_in(struct device *dev, struct device_attribute *attr, char *buf)
831{
832 struct sensor_device_attribute_2 *sensor_attr =
833 to_sensor_dev_attr_2(attr);
834 int nr = sensor_attr->nr;
835 int index = sensor_attr->index;
836 struct w83793_data *data = w83793_update_device(dev);
837 u16 val = data->in[index][nr];
838
839 if (index < 3) {
840 val <<= 2;
841 val += (data->in_low_bits[nr] >> (index * 2)) & 0x3;
842 }
Gong Junddca9332007-01-18 22:14:23 +0100843 /* voltage inputs 5VDD and 5VSB needs 150mV offset */
844 val = val * scale_in[index] + scale_in_add[index];
845 return sprintf(buf, "%d\n", val);
Rudolf Marek6800c3d2006-12-12 18:18:30 +0100846}
847
848static ssize_t
849store_in(struct device *dev, struct device_attribute *attr,
850 const char *buf, size_t count)
851{
852 struct sensor_device_attribute_2 *sensor_attr =
853 to_sensor_dev_attr_2(attr);
854 int nr = sensor_attr->nr;
855 int index = sensor_attr->index;
856 struct i2c_client *client = to_i2c_client(dev);
857 struct w83793_data *data = i2c_get_clientdata(client);
858 u32 val;
859
860 val =
861 (simple_strtoul(buf, NULL, 10) +
862 scale_in[index] / 2) / scale_in[index];
863 mutex_lock(&data->update_lock);
864 if (index > 2) {
Gong Junddca9332007-01-18 22:14:23 +0100865 /* fix the limit values of 5VDD and 5VSB to ALARM mechanism */
866 if (1 == nr || 2 == nr) {
867 val -= scale_in_add[index] / scale_in[index];
868 }
Rudolf Marek6800c3d2006-12-12 18:18:30 +0100869 val = SENSORS_LIMIT(val, 0, 255);
870 } else {
871 val = SENSORS_LIMIT(val, 0, 0x3FF);
872 data->in_low_bits[nr] =
873 w83793_read_value(client, W83793_REG_IN_LOW_BITS[nr]);
874 data->in_low_bits[nr] &= ~(0x03 << (2 * index));
875 data->in_low_bits[nr] |= (val & 0x03) << (2 * index);
876 w83793_write_value(client, W83793_REG_IN_LOW_BITS[nr],
877 data->in_low_bits[nr]);
878 val >>= 2;
879 }
880 data->in[index][nr] = val;
881 w83793_write_value(client, W83793_REG_IN[index][nr],
882 data->in[index][nr]);
883 mutex_unlock(&data->update_lock);
884 return count;
885}
886
887#define NOT_USED -1
888
889#define SENSOR_ATTR_IN(index) \
890 SENSOR_ATTR_2(in##index##_input, S_IRUGO, show_in, NULL, \
891 IN_READ, index), \
892 SENSOR_ATTR_2(in##index##_max, S_IRUGO | S_IWUSR, show_in, \
893 store_in, IN_MAX, index), \
894 SENSOR_ATTR_2(in##index##_min, S_IRUGO | S_IWUSR, show_in, \
895 store_in, IN_LOW, index), \
896 SENSOR_ATTR_2(in##index##_alarm, S_IRUGO, show_alarm_beep, \
897 NULL, ALARM_STATUS, index + ((index > 2) ? 1 : 0)), \
898 SENSOR_ATTR_2(in##index##_beep, S_IWUSR | S_IRUGO, \
899 show_alarm_beep, store_beep, BEEP_ENABLE, \
900 index + ((index > 2) ? 1 : 0))
901
902#define SENSOR_ATTR_FAN(index) \
903 SENSOR_ATTR_2(fan##index##_alarm, S_IRUGO, show_alarm_beep, \
904 NULL, ALARM_STATUS, index + 17), \
905 SENSOR_ATTR_2(fan##index##_beep, S_IWUSR | S_IRUGO, \
906 show_alarm_beep, store_beep, BEEP_ENABLE, index + 17), \
907 SENSOR_ATTR_2(fan##index##_input, S_IRUGO, show_fan, \
908 NULL, FAN_INPUT, index - 1), \
909 SENSOR_ATTR_2(fan##index##_min, S_IWUSR | S_IRUGO, \
910 show_fan, store_fan_min, FAN_MIN, index - 1)
911
912#define SENSOR_ATTR_PWM(index) \
913 SENSOR_ATTR_2(pwm##index, S_IWUSR | S_IRUGO, show_pwm, \
914 store_pwm, PWM_DUTY, index - 1), \
915 SENSOR_ATTR_2(pwm##index##_nonstop, S_IWUSR | S_IRUGO, \
916 show_pwm, store_pwm, PWM_NONSTOP, index - 1), \
917 SENSOR_ATTR_2(pwm##index##_start, S_IWUSR | S_IRUGO, \
918 show_pwm, store_pwm, PWM_START, index - 1), \
919 SENSOR_ATTR_2(pwm##index##_stop_time, S_IWUSR | S_IRUGO, \
920 show_pwm, store_pwm, PWM_STOP_TIME, index - 1)
921
922#define SENSOR_ATTR_TEMP(index) \
923 SENSOR_ATTR_2(temp##index##_type, S_IRUGO | S_IWUSR, \
924 show_temp_mode, store_temp_mode, NOT_USED, index - 1), \
925 SENSOR_ATTR_2(temp##index##_input, S_IRUGO, show_temp, \
926 NULL, TEMP_READ, index - 1), \
927 SENSOR_ATTR_2(temp##index##_max, S_IRUGO | S_IWUSR, show_temp, \
928 store_temp, TEMP_CRIT, index - 1), \
929 SENSOR_ATTR_2(temp##index##_max_hyst, S_IRUGO | S_IWUSR, \
930 show_temp, store_temp, TEMP_CRIT_HYST, index - 1), \
931 SENSOR_ATTR_2(temp##index##_warn, S_IRUGO | S_IWUSR, show_temp, \
932 store_temp, TEMP_WARN, index - 1), \
933 SENSOR_ATTR_2(temp##index##_warn_hyst, S_IRUGO | S_IWUSR, \
934 show_temp, store_temp, TEMP_WARN_HYST, index - 1), \
935 SENSOR_ATTR_2(temp##index##_alarm, S_IRUGO, \
936 show_alarm_beep, NULL, ALARM_STATUS, index + 11), \
937 SENSOR_ATTR_2(temp##index##_beep, S_IWUSR | S_IRUGO, \
938 show_alarm_beep, store_beep, BEEP_ENABLE, index + 11), \
939 SENSOR_ATTR_2(temp##index##_auto_channels_pwm, \
940 S_IRUGO | S_IWUSR, show_sf_ctrl, store_sf_ctrl, \
941 TEMP_FAN_MAP, index - 1), \
942 SENSOR_ATTR_2(temp##index##_pwm_enable, S_IWUSR | S_IRUGO, \
943 show_sf_ctrl, store_sf_ctrl, TEMP_PWM_ENABLE, \
944 index - 1), \
945 SENSOR_ATTR_2(thermal_cruise##index, S_IRUGO | S_IWUSR, \
946 show_sf_ctrl, store_sf_ctrl, TEMP_CRUISE, index - 1), \
947 SENSOR_ATTR_2(tolerance##index, S_IRUGO | S_IWUSR, show_sf_ctrl,\
948 store_sf_ctrl, TEMP_TOLERANCE, index - 1), \
949 SENSOR_ATTR_2(temp##index##_auto_point1_pwm, S_IRUGO | S_IWUSR, \
950 show_sf2_pwm, store_sf2_pwm, 0, index - 1), \
951 SENSOR_ATTR_2(temp##index##_auto_point2_pwm, S_IRUGO | S_IWUSR, \
952 show_sf2_pwm, store_sf2_pwm, 1, index - 1), \
953 SENSOR_ATTR_2(temp##index##_auto_point3_pwm, S_IRUGO | S_IWUSR, \
954 show_sf2_pwm, store_sf2_pwm, 2, index - 1), \
955 SENSOR_ATTR_2(temp##index##_auto_point4_pwm, S_IRUGO | S_IWUSR, \
956 show_sf2_pwm, store_sf2_pwm, 3, index - 1), \
957 SENSOR_ATTR_2(temp##index##_auto_point5_pwm, S_IRUGO | S_IWUSR, \
958 show_sf2_pwm, store_sf2_pwm, 4, index - 1), \
959 SENSOR_ATTR_2(temp##index##_auto_point6_pwm, S_IRUGO | S_IWUSR, \
960 show_sf2_pwm, store_sf2_pwm, 5, index - 1), \
961 SENSOR_ATTR_2(temp##index##_auto_point7_pwm, S_IRUGO | S_IWUSR, \
962 show_sf2_pwm, store_sf2_pwm, 6, index - 1), \
963 SENSOR_ATTR_2(temp##index##_auto_point1_temp, S_IRUGO | S_IWUSR,\
964 show_sf2_temp, store_sf2_temp, 0, index - 1), \
965 SENSOR_ATTR_2(temp##index##_auto_point2_temp, S_IRUGO | S_IWUSR,\
966 show_sf2_temp, store_sf2_temp, 1, index - 1), \
967 SENSOR_ATTR_2(temp##index##_auto_point3_temp, S_IRUGO | S_IWUSR,\
968 show_sf2_temp, store_sf2_temp, 2, index - 1), \
969 SENSOR_ATTR_2(temp##index##_auto_point4_temp, S_IRUGO | S_IWUSR,\
970 show_sf2_temp, store_sf2_temp, 3, index - 1), \
971 SENSOR_ATTR_2(temp##index##_auto_point5_temp, S_IRUGO | S_IWUSR,\
972 show_sf2_temp, store_sf2_temp, 4, index - 1), \
973 SENSOR_ATTR_2(temp##index##_auto_point6_temp, S_IRUGO | S_IWUSR,\
974 show_sf2_temp, store_sf2_temp, 5, index - 1), \
975 SENSOR_ATTR_2(temp##index##_auto_point7_temp, S_IRUGO | S_IWUSR,\
976 show_sf2_temp, store_sf2_temp, 6, index - 1)
977
978static struct sensor_device_attribute_2 w83793_sensor_attr_2[] = {
979 SENSOR_ATTR_IN(0),
980 SENSOR_ATTR_IN(1),
981 SENSOR_ATTR_IN(2),
982 SENSOR_ATTR_IN(3),
983 SENSOR_ATTR_IN(4),
984 SENSOR_ATTR_IN(5),
985 SENSOR_ATTR_IN(6),
986 SENSOR_ATTR_IN(7),
987 SENSOR_ATTR_IN(8),
988 SENSOR_ATTR_IN(9),
989 SENSOR_ATTR_TEMP(1),
990 SENSOR_ATTR_TEMP(2),
991 SENSOR_ATTR_TEMP(3),
992 SENSOR_ATTR_TEMP(4),
993 SENSOR_ATTR_TEMP(5),
994 SENSOR_ATTR_TEMP(6),
995 SENSOR_ATTR_FAN(1),
996 SENSOR_ATTR_FAN(2),
997 SENSOR_ATTR_FAN(3),
998 SENSOR_ATTR_FAN(4),
999 SENSOR_ATTR_FAN(5),
1000 SENSOR_ATTR_PWM(1),
1001 SENSOR_ATTR_PWM(2),
1002 SENSOR_ATTR_PWM(3),
1003};
1004
1005/* Fan6-Fan12 */
1006static struct sensor_device_attribute_2 w83793_left_fan[] = {
1007 SENSOR_ATTR_FAN(6),
1008 SENSOR_ATTR_FAN(7),
1009 SENSOR_ATTR_FAN(8),
1010 SENSOR_ATTR_FAN(9),
1011 SENSOR_ATTR_FAN(10),
1012 SENSOR_ATTR_FAN(11),
1013 SENSOR_ATTR_FAN(12),
1014};
1015
1016/* Pwm4-Pwm8 */
1017static struct sensor_device_attribute_2 w83793_left_pwm[] = {
1018 SENSOR_ATTR_PWM(4),
1019 SENSOR_ATTR_PWM(5),
1020 SENSOR_ATTR_PWM(6),
1021 SENSOR_ATTR_PWM(7),
1022 SENSOR_ATTR_PWM(8),
1023};
1024
1025static struct sensor_device_attribute_2 sda_single_files[] = {
1026 SENSOR_ATTR_2(cpu0_vid, S_IRUGO, show_vid, NULL, NOT_USED, 0),
1027 SENSOR_ATTR_2(cpu1_vid, S_IRUGO, show_vid, NULL, NOT_USED, 1),
1028 SENSOR_ATTR_2(vrm, S_IWUSR | S_IRUGO, show_vrm, store_vrm,
1029 NOT_USED, NOT_USED),
1030 SENSOR_ATTR_2(chassis, S_IWUSR | S_IRUGO, show_alarm_beep,
1031 store_chassis_clear, ALARM_STATUS, 30),
1032 SENSOR_ATTR_2(beep_enable, S_IWUSR | S_IRUGO, show_beep_enable,
1033 store_beep_enable, NOT_USED, NOT_USED),
1034 SENSOR_ATTR_2(pwm_default, S_IWUSR | S_IRUGO, show_sf_setup,
1035 store_sf_setup, SETUP_PWM_DEFAULT, NOT_USED),
1036 SENSOR_ATTR_2(pwm_uptime, S_IWUSR | S_IRUGO, show_sf_setup,
1037 store_sf_setup, SETUP_PWM_UPTIME, NOT_USED),
1038 SENSOR_ATTR_2(pwm_downtime, S_IWUSR | S_IRUGO, show_sf_setup,
1039 store_sf_setup, SETUP_PWM_DOWNTIME, NOT_USED),
1040 SENSOR_ATTR_2(temp_critical, S_IWUSR | S_IRUGO, show_sf_setup,
1041 store_sf_setup, SETUP_TEMP_CRITICAL, NOT_USED),
1042};
1043
1044static void w83793_init_client(struct i2c_client *client)
1045{
1046 if (reset) {
1047 w83793_write_value(client, W83793_REG_CONFIG, 0x80);
1048 }
1049
1050 /* Start monitoring */
1051 w83793_write_value(client, W83793_REG_CONFIG,
1052 w83793_read_value(client, W83793_REG_CONFIG) | 0x01);
1053
1054}
1055
1056static int w83793_attach_adapter(struct i2c_adapter *adapter)
1057{
1058 if (!(adapter->class & I2C_CLASS_HWMON))
1059 return 0;
1060 return i2c_probe(adapter, &addr_data, w83793_detect);
1061}
1062
1063static int w83793_detach_client(struct i2c_client *client)
1064{
1065 struct w83793_data *data = i2c_get_clientdata(client);
1066 struct device *dev = &client->dev;
1067 int err, i;
1068
1069 /* main client */
1070 if (data) {
1071 hwmon_device_unregister(data->class_dev);
1072
1073 for (i = 0; i < ARRAY_SIZE(w83793_sensor_attr_2); i++)
1074 device_remove_file(dev,
1075 &w83793_sensor_attr_2[i].dev_attr);
1076
1077 for (i = 0; i < ARRAY_SIZE(sda_single_files); i++)
1078 device_remove_file(dev, &sda_single_files[i].dev_attr);
1079
1080 for (i = 0; i < ARRAY_SIZE(w83793_left_fan); i++)
1081 device_remove_file(dev, &w83793_left_fan[i].dev_attr);
1082
1083 for (i = 0; i < ARRAY_SIZE(w83793_left_pwm); i++)
1084 device_remove_file(dev, &w83793_left_pwm[i].dev_attr);
1085 }
1086
1087 if ((err = i2c_detach_client(client)))
1088 return err;
1089
1090 /* main client */
1091 if (data)
1092 kfree(data);
1093 /* subclient */
1094 else
1095 kfree(client);
1096
1097 return 0;
1098}
1099
1100static int
1101w83793_create_subclient(struct i2c_adapter *adapter,
1102 struct i2c_client *client, int addr,
1103 struct i2c_client **sub_cli)
1104{
1105 int err = 0;
1106 struct i2c_client *sub_client;
1107
1108 (*sub_cli) = sub_client =
1109 kzalloc(sizeof(struct i2c_client), GFP_KERNEL);
1110 if (!(sub_client)) {
1111 return -ENOMEM;
1112 }
1113 sub_client->addr = 0x48 + addr;
1114 i2c_set_clientdata(sub_client, NULL);
1115 sub_client->adapter = adapter;
1116 sub_client->driver = &w83793_driver;
1117 strlcpy(sub_client->name, "w83793 subclient", I2C_NAME_SIZE);
1118 if ((err = i2c_attach_client(sub_client))) {
1119 dev_err(&client->dev, "subclient registration "
1120 "at address 0x%x failed\n", sub_client->addr);
1121 kfree(sub_client);
1122 }
1123 return err;
1124}
1125
1126static int
1127w83793_detect_subclients(struct i2c_adapter *adapter, int address,
1128 int kind, struct i2c_client *client)
1129{
1130 int i, id, err;
1131 u8 tmp;
1132 struct w83793_data *data = i2c_get_clientdata(client);
1133
1134 id = i2c_adapter_id(adapter);
1135 if (force_subclients[0] == id && force_subclients[1] == address) {
1136 for (i = 2; i <= 3; i++) {
1137 if (force_subclients[i] < 0x48
1138 || force_subclients[i] > 0x4f) {
1139 dev_err(&client->dev,
1140 "invalid subclient "
1141 "address %d; must be 0x48-0x4f\n",
1142 force_subclients[i]);
1143 err = -EINVAL;
1144 goto ERROR_SC_0;
1145 }
1146 }
1147 w83793_write_value(client, W83793_REG_I2C_SUBADDR,
1148 (force_subclients[2] & 0x07) |
1149 ((force_subclients[3] & 0x07) << 4));
1150 }
1151
1152 tmp = w83793_read_value(client, W83793_REG_I2C_SUBADDR);
1153 if (!(tmp & 0x08)) {
1154 err =
1155 w83793_create_subclient(adapter, client, tmp & 0x7,
1156 &data->lm75[0]);
1157 if (err < 0)
1158 goto ERROR_SC_0;
1159 }
1160 if (!(tmp & 0x80)) {
1161 if ((data->lm75[0] != NULL)
1162 && ((tmp & 0x7) == ((tmp >> 4) & 0x7))) {
1163 dev_err(&client->dev,
1164 "duplicate addresses 0x%x, "
1165 "use force_subclients\n", data->lm75[0]->addr);
1166 err = -ENODEV;
1167 goto ERROR_SC_1;
1168 }
1169 err = w83793_create_subclient(adapter, client,
1170 (tmp >> 4) & 0x7, &data->lm75[1]);
1171 if (err < 0)
1172 goto ERROR_SC_1;
1173 }
1174
1175 return 0;
1176
1177 /* Undo inits in case of errors */
1178
1179ERROR_SC_1:
1180 if (data->lm75[0] != NULL) {
1181 i2c_detach_client(data->lm75[0]);
1182 kfree(data->lm75[0]);
1183 }
1184ERROR_SC_0:
1185 return err;
1186}
1187
1188static int w83793_detect(struct i2c_adapter *adapter, int address, int kind)
1189{
1190 int i;
1191 u8 tmp, val;
1192 struct i2c_client *client;
1193 struct device *dev;
1194 struct w83793_data *data;
1195 int files_fan = ARRAY_SIZE(w83793_left_fan) / 7;
1196 int files_pwm = ARRAY_SIZE(w83793_left_pwm) / 5;
1197 int err = 0;
1198
1199 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA)) {
1200 goto exit;
1201 }
1202
1203 /* OK. For now, we presume we have a valid client. We now create the
1204 client structure, even though we cannot fill it completely yet.
1205 But it allows us to access w83793_{read,write}_value. */
1206
1207 if (!(data = kzalloc(sizeof(struct w83793_data), GFP_KERNEL))) {
1208 err = -ENOMEM;
1209 goto exit;
1210 }
1211
1212 client = &data->client;
1213 dev = &client->dev;
1214 i2c_set_clientdata(client, data);
1215 client->addr = address;
1216 client->adapter = adapter;
1217 client->driver = &w83793_driver;
1218
1219 data->bank = i2c_smbus_read_byte_data(client, W83793_REG_BANKSEL);
1220
1221 /* Now, we do the remaining detection. */
1222 if (kind < 0) {
1223 tmp = data->bank & 0x80 ? 0x5c : 0xa3;
1224 /* Check Winbond vendor ID */
1225 if (tmp != i2c_smbus_read_byte_data(client,
1226 W83793_REG_VENDORID)) {
1227 pr_debug("w83793: Detection failed at check "
1228 "vendor id\n");
1229 err = -ENODEV;
1230 goto free_mem;
1231 }
1232
1233 /* If Winbond chip, address of chip and W83793_REG_I2C_ADDR
1234 should match */
1235 if ((data->bank & 0x07) == 0
1236 && i2c_smbus_read_byte_data(client, W83793_REG_I2C_ADDR) !=
1237 (address << 1)) {
1238 pr_debug("w83793: Detection failed at check "
1239 "i2c addr\n");
1240 err = -ENODEV;
1241 goto free_mem;
1242 }
1243
1244 }
1245
1246 /* We have either had a force parameter, or we have already detected the
1247 Winbond. Determine the chip type now */
1248
1249 if (kind <= 0) {
1250 if (0x7b == w83793_read_value(client, W83793_REG_CHIPID)) {
1251 kind = w83793;
1252 } else {
1253 if (kind == 0)
1254 dev_warn(&adapter->dev, "w83793: Ignoring "
1255 "'force' parameter for unknown chip "
1256 "at address 0x%02x\n", address);
1257 err = -ENODEV;
1258 goto free_mem;
1259 }
1260 }
1261
1262 /* Fill in the remaining client fields and put into the global list */
1263 strlcpy(client->name, "w83793", I2C_NAME_SIZE);
1264
1265 mutex_init(&data->update_lock);
1266
1267 /* Tell the I2C layer a new client has arrived */
1268 if ((err = i2c_attach_client(client)))
1269 goto free_mem;
1270
1271 if ((err = w83793_detect_subclients(adapter, address, kind, client)))
1272 goto detach_client;
1273
1274 /* Initialize the chip */
1275 w83793_init_client(client);
1276
1277 data->vrm = vid_which_vrm();
1278 /*
1279 Only fan 1-5 has their own input pins,
1280 Pwm 1-3 has their own pins
1281 */
1282 data->has_fan = 0x1f;
1283 data->has_pwm = 0x07;
1284 tmp = w83793_read_value(client, W83793_REG_MFC);
1285 val = w83793_read_value(client, W83793_REG_FANIN_CTRL);
1286
1287 /* check the function of pins 49-56 */
1288 if (!(tmp & 0x80)) {
1289 data->has_pwm |= 0x18; /* pwm 4,5 */
1290 if (val & 0x01) { /* fan 6 */
1291 data->has_fan |= 0x20;
1292 data->has_pwm |= 0x20;
1293 }
1294 if (val & 0x02) { /* fan 7 */
1295 data->has_fan |= 0x40;
1296 data->has_pwm |= 0x40;
1297 }
1298 if (!(tmp & 0x40) && (val & 0x04)) { /* fan 8 */
1299 data->has_fan |= 0x80;
1300 data->has_pwm |= 0x80;
1301 }
1302 }
1303
1304 if (0x08 == (tmp & 0x0c)) {
1305 if (val & 0x08) /* fan 9 */
1306 data->has_fan |= 0x100;
1307 if (val & 0x10) /* fan 10 */
1308 data->has_fan |= 0x200;
1309 }
1310
1311 if (0x20 == (tmp & 0x30)) {
1312 if (val & 0x20) /* fan 11 */
1313 data->has_fan |= 0x400;
1314 if (val & 0x40) /* fan 12 */
1315 data->has_fan |= 0x800;
1316 }
1317
1318 if ((tmp & 0x01) && (val & 0x04)) { /* fan 8, second location */
1319 data->has_fan |= 0x80;
1320 data->has_pwm |= 0x80;
1321 }
1322
1323 /* Register sysfs hooks */
1324 for (i = 0; i < ARRAY_SIZE(w83793_sensor_attr_2); i++) {
1325 err = device_create_file(dev,
1326 &w83793_sensor_attr_2[i].dev_attr);
1327 if (err)
1328 goto exit_remove;
1329 }
1330
1331 for (i = 0; i < ARRAY_SIZE(sda_single_files); i++) {
1332 err = device_create_file(dev, &sda_single_files[i].dev_attr);
1333 if (err)
1334 goto exit_remove;
1335
1336 }
1337
1338 for (i = 5; i < 12; i++) {
1339 int j;
1340 if (!(data->has_fan & (1 << i)))
1341 continue;
1342 for (j = 0; j < files_fan; j++) {
1343 err = device_create_file(dev,
1344 &w83793_left_fan[(i - 5) * files_fan
1345 + j].dev_attr);
1346 if (err)
1347 goto exit_remove;
1348 }
1349 }
1350
1351 for (i = 3; i < 8; i++) {
1352 int j;
1353 if (!(data->has_pwm & (1 << i)))
1354 continue;
1355 for (j = 0; j < files_pwm; j++) {
1356 err = device_create_file(dev,
1357 &w83793_left_pwm[(i - 3) * files_pwm
1358 + j].dev_attr);
1359 if (err)
1360 goto exit_remove;
1361 }
1362 }
1363
1364 data->class_dev = hwmon_device_register(dev);
1365 if (IS_ERR(data->class_dev)) {
1366 err = PTR_ERR(data->class_dev);
1367 goto exit_remove;
1368 }
1369
1370 return 0;
1371
1372 /* Unregister sysfs hooks */
1373
1374exit_remove:
1375 for (i = 0; i < ARRAY_SIZE(w83793_sensor_attr_2); i++)
1376 device_remove_file(dev, &w83793_sensor_attr_2[i].dev_attr);
1377
1378 for (i = 0; i < ARRAY_SIZE(sda_single_files); i++)
1379 device_remove_file(dev, &sda_single_files[i].dev_attr);
1380
1381 for (i = 0; i < ARRAY_SIZE(w83793_left_fan); i++)
1382 device_remove_file(dev, &w83793_left_fan[i].dev_attr);
1383
1384 for (i = 0; i < ARRAY_SIZE(w83793_left_pwm); i++)
1385 device_remove_file(dev, &w83793_left_pwm[i].dev_attr);
1386
1387 if (data->lm75[0] != NULL) {
1388 i2c_detach_client(data->lm75[0]);
1389 kfree(data->lm75[0]);
1390 }
1391 if (data->lm75[1] != NULL) {
1392 i2c_detach_client(data->lm75[1]);
1393 kfree(data->lm75[1]);
1394 }
1395detach_client:
1396 i2c_detach_client(client);
1397free_mem:
1398 kfree(data);
1399exit:
1400 return err;
1401}
1402
1403static void w83793_update_nonvolatile(struct device *dev)
1404{
1405 struct i2c_client *client = to_i2c_client(dev);
1406 struct w83793_data *data = i2c_get_clientdata(client);
1407 int i, j;
1408 /*
1409 They are somewhat "stable" registers, and to update them everytime
1410 takes so much time, it's just not worthy. Update them in a long
1411 interval to avoid exception.
1412 */
1413 if (!(time_after(jiffies, data->last_nonvolatile + HZ * 300)
1414 || !data->valid))
1415 return;
1416 /* update voltage limits */
1417 for (i = 1; i < 3; i++) {
1418 for (j = 0; j < ARRAY_SIZE(data->in); j++) {
1419 data->in[j][i] =
1420 w83793_read_value(client, W83793_REG_IN[j][i]);
1421 }
1422 data->in_low_bits[i] =
1423 w83793_read_value(client, W83793_REG_IN_LOW_BITS[i]);
1424 }
1425
1426 for (i = 0; i < ARRAY_SIZE(data->fan_min); i++) {
1427 /* Update the Fan measured value and limits */
1428 if (!(data->has_fan & (1 << i))) {
1429 continue;
1430 }
1431 data->fan_min[i] =
1432 w83793_read_value(client, W83793_REG_FAN_MIN(i)) << 8;
1433 data->fan_min[i] |=
1434 w83793_read_value(client, W83793_REG_FAN_MIN(i) + 1);
1435 }
1436
1437 for (i = 0; i < ARRAY_SIZE(data->temp_fan_map); i++) {
1438 data->temp_fan_map[i] =
1439 w83793_read_value(client, W83793_REG_TEMP_FAN_MAP(i));
1440 for (j = 1; j < 5; j++) {
1441 data->temp[i][j] =
1442 w83793_read_value(client, W83793_REG_TEMP[i][j]);
1443 }
1444 data->temp_cruise[i] =
1445 w83793_read_value(client, W83793_REG_TEMP_CRUISE(i));
1446 for (j = 0; j < 7; j++) {
1447 data->sf2_pwm[i][j] =
1448 w83793_read_value(client, W83793_REG_SF2_PWM(i, j));
1449 data->sf2_temp[i][j] =
1450 w83793_read_value(client,
1451 W83793_REG_SF2_TEMP(i, j));
1452 }
1453 }
1454
1455 for (i = 0; i < ARRAY_SIZE(data->temp_mode); i++)
1456 data->temp_mode[i] =
1457 w83793_read_value(client, W83793_REG_TEMP_MODE[i]);
1458
1459 for (i = 0; i < ARRAY_SIZE(data->tolerance); i++) {
1460 data->tolerance[i] =
1461 w83793_read_value(client, W83793_REG_TEMP_TOL(i));
1462 }
1463
1464 for (i = 0; i < ARRAY_SIZE(data->pwm); i++) {
1465 if (!(data->has_pwm & (1 << i)))
1466 continue;
1467 data->pwm[i][PWM_NONSTOP] =
1468 w83793_read_value(client, W83793_REG_PWM(i, PWM_NONSTOP));
1469 data->pwm[i][PWM_START] =
1470 w83793_read_value(client, W83793_REG_PWM(i, PWM_START));
1471 data->pwm_stop_time[i] =
1472 w83793_read_value(client, W83793_REG_PWM_STOP_TIME(i));
1473 }
1474
1475 data->pwm_default = w83793_read_value(client, W83793_REG_PWM_DEFAULT);
1476 data->pwm_enable = w83793_read_value(client, W83793_REG_PWM_ENABLE);
1477 data->pwm_uptime = w83793_read_value(client, W83793_REG_PWM_UPTIME);
1478 data->pwm_downtime = w83793_read_value(client, W83793_REG_PWM_DOWNTIME);
1479 data->temp_critical =
1480 w83793_read_value(client, W83793_REG_TEMP_CRITICAL);
1481 data->beep_enable = w83793_read_value(client, W83793_REG_OVT_BEEP);
1482
1483 for (i = 0; i < ARRAY_SIZE(data->beeps); i++) {
1484 data->beeps[i] = w83793_read_value(client, W83793_REG_BEEP(i));
1485 }
1486
1487 data->last_nonvolatile = jiffies;
1488}
1489
1490static struct w83793_data *w83793_update_device(struct device *dev)
1491{
1492 struct i2c_client *client = to_i2c_client(dev);
1493 struct w83793_data *data = i2c_get_clientdata(client);
1494 int i;
1495
1496 mutex_lock(&data->update_lock);
1497
1498 if (!(time_after(jiffies, data->last_updated + HZ * 2)
1499 || !data->valid))
1500 goto END;
1501
1502 /* Update the voltages measured value and limits */
1503 for (i = 0; i < ARRAY_SIZE(data->in); i++)
1504 data->in[i][IN_READ] =
1505 w83793_read_value(client, W83793_REG_IN[i][IN_READ]);
1506
1507 data->in_low_bits[IN_READ] =
1508 w83793_read_value(client, W83793_REG_IN_LOW_BITS[IN_READ]);
1509
1510 for (i = 0; i < ARRAY_SIZE(data->fan); i++) {
1511 if (!(data->has_fan & (1 << i))) {
1512 continue;
1513 }
1514 data->fan[i] =
1515 w83793_read_value(client, W83793_REG_FAN(i)) << 8;
1516 data->fan[i] |=
1517 w83793_read_value(client, W83793_REG_FAN(i) + 1);
1518 }
1519
1520 for (i = 0; i < ARRAY_SIZE(data->temp); i++)
1521 data->temp[i][TEMP_READ] =
1522 w83793_read_value(client, W83793_REG_TEMP[i][TEMP_READ]);
1523
1524 data->temp_low_bits =
1525 w83793_read_value(client, W83793_REG_TEMP_LOW_BITS);
1526
1527 for (i = 0; i < ARRAY_SIZE(data->pwm); i++) {
1528 if (data->has_pwm & (1 << i))
1529 data->pwm[i][PWM_DUTY] =
1530 w83793_read_value(client,
1531 W83793_REG_PWM(i, PWM_DUTY));
1532 }
1533
1534 for (i = 0; i < ARRAY_SIZE(data->alarms); i++)
1535 data->alarms[i] =
1536 w83793_read_value(client, W83793_REG_ALARM(i));
1537 data->vid[0] = w83793_read_value(client, W83793_REG_VID_INA);
1538 data->vid[1] = w83793_read_value(client, W83793_REG_VID_INB);
1539 w83793_update_nonvolatile(dev);
1540 data->last_updated = jiffies;
1541 data->valid = 1;
1542
1543END:
1544 mutex_unlock(&data->update_lock);
1545 return data;
1546}
1547
1548/* Ignore the possibility that somebody change bank outside the driver
1549 Must be called with data->update_lock held, except during initialization */
1550static u8 w83793_read_value(struct i2c_client *client, u16 reg)
1551{
1552 struct w83793_data *data = i2c_get_clientdata(client);
1553 u8 res = 0xff;
1554 u8 new_bank = reg >> 8;
1555
1556 new_bank |= data->bank & 0xfc;
1557 if (data->bank != new_bank) {
1558 if (i2c_smbus_write_byte_data
1559 (client, W83793_REG_BANKSEL, new_bank) >= 0)
1560 data->bank = new_bank;
1561 else {
1562 dev_err(&client->dev,
1563 "set bank to %d failed, fall back "
1564 "to bank %d, read reg 0x%x error\n",
1565 new_bank, data->bank, reg);
1566 res = 0x0; /* read 0x0 from the chip */
1567 goto END;
1568 }
1569 }
1570 res = i2c_smbus_read_byte_data(client, reg & 0xff);
1571END:
1572 return res;
1573}
1574
1575/* Must be called with data->update_lock held, except during initialization */
1576static int w83793_write_value(struct i2c_client *client, u16 reg, u8 value)
1577{
1578 struct w83793_data *data = i2c_get_clientdata(client);
1579 int res;
1580 u8 new_bank = reg >> 8;
1581
1582 new_bank |= data->bank & 0xfc;
1583 if (data->bank != new_bank) {
1584 if ((res = i2c_smbus_write_byte_data
1585 (client, W83793_REG_BANKSEL, new_bank)) >= 0)
1586 data->bank = new_bank;
1587 else {
1588 dev_err(&client->dev,
1589 "set bank to %d failed, fall back "
1590 "to bank %d, write reg 0x%x error\n",
1591 new_bank, data->bank, reg);
1592 goto END;
1593 }
1594 }
1595
1596 res = i2c_smbus_write_byte_data(client, reg & 0xff, value);
1597END:
1598 return res;
1599}
1600
1601static int __init sensors_w83793_init(void)
1602{
1603 return i2c_add_driver(&w83793_driver);
1604}
1605
1606static void __exit sensors_w83793_exit(void)
1607{
1608 i2c_del_driver(&w83793_driver);
1609}
1610
1611MODULE_AUTHOR("Yuan Mu");
1612MODULE_DESCRIPTION("w83793 driver");
1613MODULE_LICENSE("GPL");
1614
1615module_init(sensors_w83793_init);
1616module_exit(sensors_w83793_exit);