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